sata_mv.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2008: Marvell Corporation, all rights reserved.
  5. * Copyright 2005: EMC Corporation, all rights reserved.
  6. * Copyright 2005 Red Hat, Inc. All rights reserved.
  7. *
  8. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * sata_mv TODO list:
  26. *
  27. * --> Errata workaround for NCQ device errors.
  28. *
  29. * --> More errata workarounds for PCI-X.
  30. *
  31. * --> Complete a full errata audit for all chipsets to identify others.
  32. *
  33. * --> ATAPI support (Marvell claims the 60xx/70xx chips can do it).
  34. *
  35. * --> Investigate problems with PCI Message Signalled Interrupts (MSI).
  36. *
  37. * --> Cache frequently-accessed registers in mv_port_priv to reduce overhead.
  38. *
  39. * --> Develop a low-power-consumption strategy, and implement it.
  40. *
  41. * --> [Experiment, low priority] Investigate interrupt coalescing.
  42. * Quite often, especially with PCI Message Signalled Interrupts (MSI),
  43. * the overhead reduced by interrupt mitigation is quite often not
  44. * worth the latency cost.
  45. *
  46. * --> [Experiment, Marvell value added] Is it possible to use target
  47. * mode to cross-connect two Linux boxes with Marvell cards? If so,
  48. * creating LibATA target mode support would be very interesting.
  49. *
  50. * Target mode, for those without docs, is the ability to directly
  51. * connect two SATA ports.
  52. */
  53. #include <linux/kernel.h>
  54. #include <linux/module.h>
  55. #include <linux/pci.h>
  56. #include <linux/init.h>
  57. #include <linux/blkdev.h>
  58. #include <linux/delay.h>
  59. #include <linux/interrupt.h>
  60. #include <linux/dmapool.h>
  61. #include <linux/dma-mapping.h>
  62. #include <linux/device.h>
  63. #include <linux/platform_device.h>
  64. #include <linux/ata_platform.h>
  65. #include <linux/mbus.h>
  66. #include <linux/bitops.h>
  67. #include <scsi/scsi_host.h>
  68. #include <scsi/scsi_cmnd.h>
  69. #include <scsi/scsi_device.h>
  70. #include <linux/libata.h>
  71. #define DRV_NAME "sata_mv"
  72. #define DRV_VERSION "1.20"
  73. enum {
  74. /* BAR's are enumerated in terms of pci_resource_start() terms */
  75. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  76. MV_IO_BAR = 2, /* offset 0x18: IO space */
  77. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  78. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  79. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  80. MV_PCI_REG_BASE = 0,
  81. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  82. MV_IRQ_COAL_CAUSE = (MV_IRQ_COAL_REG_BASE + 0x08),
  83. MV_IRQ_COAL_CAUSE_LO = (MV_IRQ_COAL_REG_BASE + 0x88),
  84. MV_IRQ_COAL_CAUSE_HI = (MV_IRQ_COAL_REG_BASE + 0x8c),
  85. MV_IRQ_COAL_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xcc),
  86. MV_IRQ_COAL_TIME_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xd0),
  87. MV_SATAHC0_REG_BASE = 0x20000,
  88. MV_FLASH_CTL_OFS = 0x1046c,
  89. MV_GPIO_PORT_CTL_OFS = 0x104f0,
  90. MV_RESET_CFG_OFS = 0x180d8,
  91. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  92. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  93. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  94. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  95. MV_MAX_Q_DEPTH = 32,
  96. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  97. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  98. * CRPB needs alignment on a 256B boundary. Size == 256B
  99. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  100. */
  101. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  102. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  103. MV_MAX_SG_CT = 256,
  104. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  105. /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
  106. MV_PORT_HC_SHIFT = 2,
  107. MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */
  108. /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
  109. MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */
  110. /* Host Flags */
  111. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  112. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  113. /* SoC integrated controllers, no PCI interface */
  114. MV_FLAG_SOC = (1 << 28),
  115. MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  116. ATA_FLAG_MMIO | ATA_FLAG_NO_ATAPI |
  117. ATA_FLAG_PIO_POLLING,
  118. MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
  119. MV_GENIIE_FLAGS = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  120. ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
  121. ATA_FLAG_NCQ | ATA_FLAG_AN,
  122. CRQB_FLAG_READ = (1 << 0),
  123. CRQB_TAG_SHIFT = 1,
  124. CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
  125. CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */
  126. CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
  127. CRQB_CMD_ADDR_SHIFT = 8,
  128. CRQB_CMD_CS = (0x2 << 11),
  129. CRQB_CMD_LAST = (1 << 15),
  130. CRPB_FLAG_STATUS_SHIFT = 8,
  131. CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
  132. CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
  133. EPRD_FLAG_END_OF_TBL = (1 << 31),
  134. /* PCI interface registers */
  135. PCI_COMMAND_OFS = 0xc00,
  136. PCI_COMMAND_MRDTRIG = (1 << 7), /* PCI Master Read Trigger */
  137. PCI_MAIN_CMD_STS_OFS = 0xd30,
  138. STOP_PCI_MASTER = (1 << 2),
  139. PCI_MASTER_EMPTY = (1 << 3),
  140. GLOB_SFT_RST = (1 << 4),
  141. MV_PCI_MODE_OFS = 0xd00,
  142. MV_PCI_MODE_MASK = 0x30,
  143. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  144. MV_PCI_DISC_TIMER = 0xd04,
  145. MV_PCI_MSI_TRIGGER = 0xc38,
  146. MV_PCI_SERR_MASK = 0xc28,
  147. MV_PCI_XBAR_TMOUT_OFS = 0x1d04,
  148. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  149. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  150. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  151. MV_PCI_ERR_COMMAND = 0x1d50,
  152. PCI_IRQ_CAUSE_OFS = 0x1d58,
  153. PCI_IRQ_MASK_OFS = 0x1d5c,
  154. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  155. PCIE_IRQ_CAUSE_OFS = 0x1900,
  156. PCIE_IRQ_MASK_OFS = 0x1910,
  157. PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
  158. /* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
  159. PCI_HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  160. PCI_HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  161. SOC_HC_MAIN_IRQ_CAUSE_OFS = 0x20020,
  162. SOC_HC_MAIN_IRQ_MASK_OFS = 0x20024,
  163. ERR_IRQ = (1 << 0), /* shift by port # */
  164. DONE_IRQ = (1 << 1), /* shift by port # */
  165. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  166. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  167. PCI_ERR = (1 << 18),
  168. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  169. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  170. PORTS_0_3_COAL_DONE = (1 << 8),
  171. PORTS_4_7_COAL_DONE = (1 << 17),
  172. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  173. GPIO_INT = (1 << 22),
  174. SELF_INT = (1 << 23),
  175. TWSI_INT = (1 << 24),
  176. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  177. HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
  178. HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
  179. /* SATAHC registers */
  180. HC_CFG_OFS = 0,
  181. HC_IRQ_CAUSE_OFS = 0x14,
  182. DMA_IRQ = (1 << 0), /* shift by port # */
  183. HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */
  184. DEV_IRQ = (1 << 8), /* shift by port # */
  185. /* Shadow block registers */
  186. SHD_BLK_OFS = 0x100,
  187. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  188. /* SATA registers */
  189. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  190. SATA_ACTIVE_OFS = 0x350,
  191. SATA_FIS_IRQ_CAUSE_OFS = 0x364,
  192. SATA_FIS_IRQ_AN = (1 << 9), /* async notification */
  193. LTMODE_OFS = 0x30c,
  194. LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */
  195. PHY_MODE3 = 0x310,
  196. PHY_MODE4 = 0x314,
  197. PHY_MODE2 = 0x330,
  198. SATA_IFCTL_OFS = 0x344,
  199. SATA_TESTCTL_OFS = 0x348,
  200. SATA_IFSTAT_OFS = 0x34c,
  201. VENDOR_UNIQUE_FIS_OFS = 0x35c,
  202. FISCFG_OFS = 0x360,
  203. FISCFG_WAIT_DEV_ERR = (1 << 8), /* wait for host on DevErr */
  204. FISCFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */
  205. MV5_PHY_MODE = 0x74,
  206. MV5_LTMODE_OFS = 0x30,
  207. MV5_PHY_CTL_OFS = 0x0C,
  208. SATA_INTERFACE_CFG_OFS = 0x050,
  209. MV_M2_PREAMP_MASK = 0x7e0,
  210. /* Port registers */
  211. EDMA_CFG_OFS = 0,
  212. EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
  213. EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
  214. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  215. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  216. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  217. EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */
  218. EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */
  219. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  220. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  221. EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
  222. EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
  223. EDMA_ERR_DEV = (1 << 2), /* device error */
  224. EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
  225. EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
  226. EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
  227. EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
  228. EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
  229. EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
  230. EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
  231. EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
  232. EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
  233. EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
  234. EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
  235. EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
  236. EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
  237. EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
  238. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
  239. EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
  240. EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
  241. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
  242. EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
  243. EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
  244. EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
  245. EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
  246. EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
  247. EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
  248. EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
  249. EDMA_ERR_OVERRUN_5 = (1 << 5),
  250. EDMA_ERR_UNDERRUN_5 = (1 << 6),
  251. EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
  252. EDMA_ERR_LNK_CTRL_RX_1 |
  253. EDMA_ERR_LNK_CTRL_RX_3 |
  254. EDMA_ERR_LNK_CTRL_TX,
  255. EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
  256. EDMA_ERR_PRD_PAR |
  257. EDMA_ERR_DEV_DCON |
  258. EDMA_ERR_DEV_CON |
  259. EDMA_ERR_SERR |
  260. EDMA_ERR_SELF_DIS |
  261. EDMA_ERR_CRQB_PAR |
  262. EDMA_ERR_CRPB_PAR |
  263. EDMA_ERR_INTRL_PAR |
  264. EDMA_ERR_IORDY |
  265. EDMA_ERR_LNK_CTRL_RX_2 |
  266. EDMA_ERR_LNK_DATA_RX |
  267. EDMA_ERR_LNK_DATA_TX |
  268. EDMA_ERR_TRANS_PROTO,
  269. EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
  270. EDMA_ERR_PRD_PAR |
  271. EDMA_ERR_DEV_DCON |
  272. EDMA_ERR_DEV_CON |
  273. EDMA_ERR_OVERRUN_5 |
  274. EDMA_ERR_UNDERRUN_5 |
  275. EDMA_ERR_SELF_DIS_5 |
  276. EDMA_ERR_CRQB_PAR |
  277. EDMA_ERR_CRPB_PAR |
  278. EDMA_ERR_INTRL_PAR |
  279. EDMA_ERR_IORDY,
  280. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  281. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  282. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  283. EDMA_REQ_Q_PTR_SHIFT = 5,
  284. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  285. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  286. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  287. EDMA_RSP_Q_PTR_SHIFT = 3,
  288. EDMA_CMD_OFS = 0x28, /* EDMA command register */
  289. EDMA_EN = (1 << 0), /* enable EDMA */
  290. EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
  291. EDMA_RESET = (1 << 2), /* reset eng/trans/link/phy */
  292. EDMA_STATUS_OFS = 0x30, /* EDMA engine status */
  293. EDMA_STATUS_CACHE_EMPTY = (1 << 6), /* GenIIe command cache empty */
  294. EDMA_STATUS_IDLE = (1 << 7), /* GenIIe EDMA enabled/idle */
  295. EDMA_IORDY_TMOUT_OFS = 0x34,
  296. EDMA_ARB_CFG_OFS = 0x38,
  297. EDMA_HALTCOND_OFS = 0x60, /* GenIIe halt conditions */
  298. GEN_II_NCQ_MAX_SECTORS = 256, /* max sects/io on Gen2 w/NCQ */
  299. /* Host private flags (hp_flags) */
  300. MV_HP_FLAG_MSI = (1 << 0),
  301. MV_HP_ERRATA_50XXB0 = (1 << 1),
  302. MV_HP_ERRATA_50XXB2 = (1 << 2),
  303. MV_HP_ERRATA_60X1B2 = (1 << 3),
  304. MV_HP_ERRATA_60X1C0 = (1 << 4),
  305. MV_HP_ERRATA_XX42A0 = (1 << 5),
  306. MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
  307. MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
  308. MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
  309. MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
  310. MV_HP_CUT_THROUGH = (1 << 10), /* can use EDMA cut-through */
  311. /* Port private flags (pp_flags) */
  312. MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
  313. MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
  314. MV_PP_FLAG_FBS_EN = (1 << 2), /* is EDMA set up for FBS? */
  315. MV_PP_FLAG_DELAYED_EH = (1 << 3), /* delayed dev err handling */
  316. };
  317. #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
  318. #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
  319. #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
  320. #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
  321. #define HAS_PCI(host) (!((host)->ports[0]->flags & MV_FLAG_SOC))
  322. #define WINDOW_CTRL(i) (0x20030 + ((i) << 4))
  323. #define WINDOW_BASE(i) (0x20034 + ((i) << 4))
  324. enum {
  325. /* DMA boundary 0xffff is required by the s/g splitting
  326. * we need on /length/ in mv_fill-sg().
  327. */
  328. MV_DMA_BOUNDARY = 0xffffU,
  329. /* mask of register bits containing lower 32 bits
  330. * of EDMA request queue DMA address
  331. */
  332. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  333. /* ditto, for response queue */
  334. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  335. };
  336. enum chip_type {
  337. chip_504x,
  338. chip_508x,
  339. chip_5080,
  340. chip_604x,
  341. chip_608x,
  342. chip_6042,
  343. chip_7042,
  344. chip_soc,
  345. };
  346. /* Command ReQuest Block: 32B */
  347. struct mv_crqb {
  348. __le32 sg_addr;
  349. __le32 sg_addr_hi;
  350. __le16 ctrl_flags;
  351. __le16 ata_cmd[11];
  352. };
  353. struct mv_crqb_iie {
  354. __le32 addr;
  355. __le32 addr_hi;
  356. __le32 flags;
  357. __le32 len;
  358. __le32 ata_cmd[4];
  359. };
  360. /* Command ResPonse Block: 8B */
  361. struct mv_crpb {
  362. __le16 id;
  363. __le16 flags;
  364. __le32 tmstmp;
  365. };
  366. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  367. struct mv_sg {
  368. __le32 addr;
  369. __le32 flags_size;
  370. __le32 addr_hi;
  371. __le32 reserved;
  372. };
  373. struct mv_port_priv {
  374. struct mv_crqb *crqb;
  375. dma_addr_t crqb_dma;
  376. struct mv_crpb *crpb;
  377. dma_addr_t crpb_dma;
  378. struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
  379. dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
  380. unsigned int req_idx;
  381. unsigned int resp_idx;
  382. u32 pp_flags;
  383. unsigned int delayed_eh_pmp_map;
  384. };
  385. struct mv_port_signal {
  386. u32 amps;
  387. u32 pre;
  388. };
  389. struct mv_host_priv {
  390. u32 hp_flags;
  391. struct mv_port_signal signal[8];
  392. const struct mv_hw_ops *ops;
  393. int n_ports;
  394. void __iomem *base;
  395. void __iomem *main_irq_cause_addr;
  396. void __iomem *main_irq_mask_addr;
  397. u32 irq_cause_ofs;
  398. u32 irq_mask_ofs;
  399. u32 unmask_all_irqs;
  400. /*
  401. * These consistent DMA memory pools give us guaranteed
  402. * alignment for hardware-accessed data structures,
  403. * and less memory waste in accomplishing the alignment.
  404. */
  405. struct dma_pool *crqb_pool;
  406. struct dma_pool *crpb_pool;
  407. struct dma_pool *sg_tbl_pool;
  408. };
  409. struct mv_hw_ops {
  410. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  411. unsigned int port);
  412. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  413. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  414. void __iomem *mmio);
  415. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  416. unsigned int n_hc);
  417. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  418. void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
  419. };
  420. static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
  421. static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  422. static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
  423. static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  424. static int mv_port_start(struct ata_port *ap);
  425. static void mv_port_stop(struct ata_port *ap);
  426. static int mv_qc_defer(struct ata_queued_cmd *qc);
  427. static void mv_qc_prep(struct ata_queued_cmd *qc);
  428. static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
  429. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
  430. static int mv_hardreset(struct ata_link *link, unsigned int *class,
  431. unsigned long deadline);
  432. static void mv_eh_freeze(struct ata_port *ap);
  433. static void mv_eh_thaw(struct ata_port *ap);
  434. static void mv6_dev_config(struct ata_device *dev);
  435. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  436. unsigned int port);
  437. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  438. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  439. void __iomem *mmio);
  440. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  441. unsigned int n_hc);
  442. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  443. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
  444. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  445. unsigned int port);
  446. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  447. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  448. void __iomem *mmio);
  449. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  450. unsigned int n_hc);
  451. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  452. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  453. void __iomem *mmio);
  454. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  455. void __iomem *mmio);
  456. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  457. void __iomem *mmio, unsigned int n_hc);
  458. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  459. void __iomem *mmio);
  460. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
  461. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
  462. static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
  463. unsigned int port_no);
  464. static int mv_stop_edma(struct ata_port *ap);
  465. static int mv_stop_edma_engine(void __iomem *port_mmio);
  466. static void mv_edma_cfg(struct ata_port *ap, int want_ncq);
  467. static void mv_pmp_select(struct ata_port *ap, int pmp);
  468. static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
  469. unsigned long deadline);
  470. static int mv_softreset(struct ata_link *link, unsigned int *class,
  471. unsigned long deadline);
  472. static void mv_pmp_error_handler(struct ata_port *ap);
  473. static void mv_process_crpb_entries(struct ata_port *ap,
  474. struct mv_port_priv *pp);
  475. /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
  476. * because we have to allow room for worst case splitting of
  477. * PRDs for 64K boundaries in mv_fill_sg().
  478. */
  479. static struct scsi_host_template mv5_sht = {
  480. ATA_BASE_SHT(DRV_NAME),
  481. .sg_tablesize = MV_MAX_SG_CT / 2,
  482. .dma_boundary = MV_DMA_BOUNDARY,
  483. };
  484. static struct scsi_host_template mv6_sht = {
  485. ATA_NCQ_SHT(DRV_NAME),
  486. .can_queue = MV_MAX_Q_DEPTH - 1,
  487. .sg_tablesize = MV_MAX_SG_CT / 2,
  488. .dma_boundary = MV_DMA_BOUNDARY,
  489. };
  490. static struct ata_port_operations mv5_ops = {
  491. .inherits = &ata_sff_port_ops,
  492. .qc_defer = mv_qc_defer,
  493. .qc_prep = mv_qc_prep,
  494. .qc_issue = mv_qc_issue,
  495. .freeze = mv_eh_freeze,
  496. .thaw = mv_eh_thaw,
  497. .hardreset = mv_hardreset,
  498. .error_handler = ata_std_error_handler, /* avoid SFF EH */
  499. .post_internal_cmd = ATA_OP_NULL,
  500. .scr_read = mv5_scr_read,
  501. .scr_write = mv5_scr_write,
  502. .port_start = mv_port_start,
  503. .port_stop = mv_port_stop,
  504. };
  505. static struct ata_port_operations mv6_ops = {
  506. .inherits = &mv5_ops,
  507. .dev_config = mv6_dev_config,
  508. .scr_read = mv_scr_read,
  509. .scr_write = mv_scr_write,
  510. .pmp_hardreset = mv_pmp_hardreset,
  511. .pmp_softreset = mv_softreset,
  512. .softreset = mv_softreset,
  513. .error_handler = mv_pmp_error_handler,
  514. };
  515. static struct ata_port_operations mv_iie_ops = {
  516. .inherits = &mv6_ops,
  517. .dev_config = ATA_OP_NULL,
  518. .qc_prep = mv_qc_prep_iie,
  519. };
  520. static const struct ata_port_info mv_port_info[] = {
  521. { /* chip_504x */
  522. .flags = MV_COMMON_FLAGS,
  523. .pio_mask = 0x1f, /* pio0-4 */
  524. .udma_mask = ATA_UDMA6,
  525. .port_ops = &mv5_ops,
  526. },
  527. { /* chip_508x */
  528. .flags = MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
  529. .pio_mask = 0x1f, /* pio0-4 */
  530. .udma_mask = ATA_UDMA6,
  531. .port_ops = &mv5_ops,
  532. },
  533. { /* chip_5080 */
  534. .flags = MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
  535. .pio_mask = 0x1f, /* pio0-4 */
  536. .udma_mask = ATA_UDMA6,
  537. .port_ops = &mv5_ops,
  538. },
  539. { /* chip_604x */
  540. .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  541. ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
  542. ATA_FLAG_NCQ,
  543. .pio_mask = 0x1f, /* pio0-4 */
  544. .udma_mask = ATA_UDMA6,
  545. .port_ops = &mv6_ops,
  546. },
  547. { /* chip_608x */
  548. .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  549. ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
  550. ATA_FLAG_NCQ | MV_FLAG_DUAL_HC,
  551. .pio_mask = 0x1f, /* pio0-4 */
  552. .udma_mask = ATA_UDMA6,
  553. .port_ops = &mv6_ops,
  554. },
  555. { /* chip_6042 */
  556. .flags = MV_GENIIE_FLAGS,
  557. .pio_mask = 0x1f, /* pio0-4 */
  558. .udma_mask = ATA_UDMA6,
  559. .port_ops = &mv_iie_ops,
  560. },
  561. { /* chip_7042 */
  562. .flags = MV_GENIIE_FLAGS,
  563. .pio_mask = 0x1f, /* pio0-4 */
  564. .udma_mask = ATA_UDMA6,
  565. .port_ops = &mv_iie_ops,
  566. },
  567. { /* chip_soc */
  568. .flags = MV_GENIIE_FLAGS | MV_FLAG_SOC,
  569. .pio_mask = 0x1f, /* pio0-4 */
  570. .udma_mask = ATA_UDMA6,
  571. .port_ops = &mv_iie_ops,
  572. },
  573. };
  574. static const struct pci_device_id mv_pci_tbl[] = {
  575. { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
  576. { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
  577. { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
  578. { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
  579. /* RocketRAID 1740/174x have different identifiers */
  580. { PCI_VDEVICE(TTI, 0x1740), chip_508x },
  581. { PCI_VDEVICE(TTI, 0x1742), chip_508x },
  582. { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
  583. { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
  584. { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
  585. { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
  586. { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
  587. { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
  588. /* Adaptec 1430SA */
  589. { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
  590. /* Marvell 7042 support */
  591. { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
  592. /* Highpoint RocketRAID PCIe series */
  593. { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
  594. { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
  595. { } /* terminate list */
  596. };
  597. static const struct mv_hw_ops mv5xxx_ops = {
  598. .phy_errata = mv5_phy_errata,
  599. .enable_leds = mv5_enable_leds,
  600. .read_preamp = mv5_read_preamp,
  601. .reset_hc = mv5_reset_hc,
  602. .reset_flash = mv5_reset_flash,
  603. .reset_bus = mv5_reset_bus,
  604. };
  605. static const struct mv_hw_ops mv6xxx_ops = {
  606. .phy_errata = mv6_phy_errata,
  607. .enable_leds = mv6_enable_leds,
  608. .read_preamp = mv6_read_preamp,
  609. .reset_hc = mv6_reset_hc,
  610. .reset_flash = mv6_reset_flash,
  611. .reset_bus = mv_reset_pci_bus,
  612. };
  613. static const struct mv_hw_ops mv_soc_ops = {
  614. .phy_errata = mv6_phy_errata,
  615. .enable_leds = mv_soc_enable_leds,
  616. .read_preamp = mv_soc_read_preamp,
  617. .reset_hc = mv_soc_reset_hc,
  618. .reset_flash = mv_soc_reset_flash,
  619. .reset_bus = mv_soc_reset_bus,
  620. };
  621. /*
  622. * Functions
  623. */
  624. static inline void writelfl(unsigned long data, void __iomem *addr)
  625. {
  626. writel(data, addr);
  627. (void) readl(addr); /* flush to avoid PCI posted write */
  628. }
  629. static inline unsigned int mv_hc_from_port(unsigned int port)
  630. {
  631. return port >> MV_PORT_HC_SHIFT;
  632. }
  633. static inline unsigned int mv_hardport_from_port(unsigned int port)
  634. {
  635. return port & MV_PORT_MASK;
  636. }
  637. /*
  638. * Consolidate some rather tricky bit shift calculations.
  639. * This is hot-path stuff, so not a function.
  640. * Simple code, with two return values, so macro rather than inline.
  641. *
  642. * port is the sole input, in range 0..7.
  643. * shift is one output, for use with main_irq_cause / main_irq_mask registers.
  644. * hardport is the other output, in range 0..3.
  645. *
  646. * Note that port and hardport may be the same variable in some cases.
  647. */
  648. #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \
  649. { \
  650. shift = mv_hc_from_port(port) * HC_SHIFT; \
  651. hardport = mv_hardport_from_port(port); \
  652. shift += hardport * 2; \
  653. }
  654. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  655. {
  656. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  657. }
  658. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  659. unsigned int port)
  660. {
  661. return mv_hc_base(base, mv_hc_from_port(port));
  662. }
  663. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  664. {
  665. return mv_hc_base_from_port(base, port) +
  666. MV_SATAHC_ARBTR_REG_SZ +
  667. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  668. }
  669. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  670. {
  671. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  672. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  673. return hc_mmio + ofs;
  674. }
  675. static inline void __iomem *mv_host_base(struct ata_host *host)
  676. {
  677. struct mv_host_priv *hpriv = host->private_data;
  678. return hpriv->base;
  679. }
  680. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  681. {
  682. return mv_port_base(mv_host_base(ap->host), ap->port_no);
  683. }
  684. static inline int mv_get_hc_count(unsigned long port_flags)
  685. {
  686. return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  687. }
  688. static void mv_set_edma_ptrs(void __iomem *port_mmio,
  689. struct mv_host_priv *hpriv,
  690. struct mv_port_priv *pp)
  691. {
  692. u32 index;
  693. /*
  694. * initialize request queue
  695. */
  696. pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
  697. index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
  698. WARN_ON(pp->crqb_dma & 0x3ff);
  699. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  700. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
  701. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  702. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  703. writelfl((pp->crqb_dma & 0xffffffff) | index,
  704. port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  705. else
  706. writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  707. /*
  708. * initialize response queue
  709. */
  710. pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
  711. index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
  712. WARN_ON(pp->crpb_dma & 0xff);
  713. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  714. if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
  715. writelfl((pp->crpb_dma & 0xffffffff) | index,
  716. port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  717. else
  718. writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  719. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
  720. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  721. }
  722. static void mv_set_main_irq_mask(struct ata_host *host,
  723. u32 disable_bits, u32 enable_bits)
  724. {
  725. struct mv_host_priv *hpriv = host->private_data;
  726. u32 old_mask, new_mask;
  727. old_mask = readl(hpriv->main_irq_mask_addr);
  728. new_mask = (old_mask & ~disable_bits) | enable_bits;
  729. if (new_mask != old_mask)
  730. writelfl(new_mask, hpriv->main_irq_mask_addr);
  731. }
  732. static void mv_enable_port_irqs(struct ata_port *ap,
  733. unsigned int port_bits)
  734. {
  735. unsigned int shift, hardport, port = ap->port_no;
  736. u32 disable_bits, enable_bits;
  737. MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
  738. disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
  739. enable_bits = port_bits << shift;
  740. mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
  741. }
  742. /**
  743. * mv_start_dma - Enable eDMA engine
  744. * @base: port base address
  745. * @pp: port private data
  746. *
  747. * Verify the local cache of the eDMA state is accurate with a
  748. * WARN_ON.
  749. *
  750. * LOCKING:
  751. * Inherited from caller.
  752. */
  753. static void mv_start_dma(struct ata_port *ap, void __iomem *port_mmio,
  754. struct mv_port_priv *pp, u8 protocol)
  755. {
  756. int want_ncq = (protocol == ATA_PROT_NCQ);
  757. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  758. int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
  759. if (want_ncq != using_ncq)
  760. mv_stop_edma(ap);
  761. }
  762. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
  763. struct mv_host_priv *hpriv = ap->host->private_data;
  764. int hardport = mv_hardport_from_port(ap->port_no);
  765. void __iomem *hc_mmio = mv_hc_base_from_port(
  766. mv_host_base(ap->host), hardport);
  767. u32 hc_irq_cause, ipending;
  768. /* clear EDMA event indicators, if any */
  769. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  770. /* clear EDMA interrupt indicator, if any */
  771. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  772. ipending = (DEV_IRQ | DMA_IRQ) << hardport;
  773. if (hc_irq_cause & ipending) {
  774. writelfl(hc_irq_cause & ~ipending,
  775. hc_mmio + HC_IRQ_CAUSE_OFS);
  776. }
  777. mv_edma_cfg(ap, want_ncq);
  778. /* clear FIS IRQ Cause */
  779. if (IS_GEN_IIE(hpriv))
  780. writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
  781. mv_set_edma_ptrs(port_mmio, hpriv, pp);
  782. writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
  783. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  784. }
  785. }
  786. static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
  787. {
  788. void __iomem *port_mmio = mv_ap_base(ap);
  789. const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
  790. const int per_loop = 5, timeout = (15 * 1000 / per_loop);
  791. int i;
  792. /*
  793. * Wait for the EDMA engine to finish transactions in progress.
  794. * No idea what a good "timeout" value might be, but measurements
  795. * indicate that it often requires hundreds of microseconds
  796. * with two drives in-use. So we use the 15msec value above
  797. * as a rough guess at what even more drives might require.
  798. */
  799. for (i = 0; i < timeout; ++i) {
  800. u32 edma_stat = readl(port_mmio + EDMA_STATUS_OFS);
  801. if ((edma_stat & empty_idle) == empty_idle)
  802. break;
  803. udelay(per_loop);
  804. }
  805. /* ata_port_printk(ap, KERN_INFO, "%s: %u+ usecs\n", __func__, i); */
  806. }
  807. /**
  808. * mv_stop_edma_engine - Disable eDMA engine
  809. * @port_mmio: io base address
  810. *
  811. * LOCKING:
  812. * Inherited from caller.
  813. */
  814. static int mv_stop_edma_engine(void __iomem *port_mmio)
  815. {
  816. int i;
  817. /* Disable eDMA. The disable bit auto clears. */
  818. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  819. /* Wait for the chip to confirm eDMA is off. */
  820. for (i = 10000; i > 0; i--) {
  821. u32 reg = readl(port_mmio + EDMA_CMD_OFS);
  822. if (!(reg & EDMA_EN))
  823. return 0;
  824. udelay(10);
  825. }
  826. return -EIO;
  827. }
  828. static int mv_stop_edma(struct ata_port *ap)
  829. {
  830. void __iomem *port_mmio = mv_ap_base(ap);
  831. struct mv_port_priv *pp = ap->private_data;
  832. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
  833. return 0;
  834. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  835. mv_wait_for_edma_empty_idle(ap);
  836. if (mv_stop_edma_engine(port_mmio)) {
  837. ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
  838. return -EIO;
  839. }
  840. return 0;
  841. }
  842. #ifdef ATA_DEBUG
  843. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  844. {
  845. int b, w;
  846. for (b = 0; b < bytes; ) {
  847. DPRINTK("%p: ", start + b);
  848. for (w = 0; b < bytes && w < 4; w++) {
  849. printk("%08x ", readl(start + b));
  850. b += sizeof(u32);
  851. }
  852. printk("\n");
  853. }
  854. }
  855. #endif
  856. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  857. {
  858. #ifdef ATA_DEBUG
  859. int b, w;
  860. u32 dw;
  861. for (b = 0; b < bytes; ) {
  862. DPRINTK("%02x: ", b);
  863. for (w = 0; b < bytes && w < 4; w++) {
  864. (void) pci_read_config_dword(pdev, b, &dw);
  865. printk("%08x ", dw);
  866. b += sizeof(u32);
  867. }
  868. printk("\n");
  869. }
  870. #endif
  871. }
  872. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  873. struct pci_dev *pdev)
  874. {
  875. #ifdef ATA_DEBUG
  876. void __iomem *hc_base = mv_hc_base(mmio_base,
  877. port >> MV_PORT_HC_SHIFT);
  878. void __iomem *port_base;
  879. int start_port, num_ports, p, start_hc, num_hcs, hc;
  880. if (0 > port) {
  881. start_hc = start_port = 0;
  882. num_ports = 8; /* shld be benign for 4 port devs */
  883. num_hcs = 2;
  884. } else {
  885. start_hc = port >> MV_PORT_HC_SHIFT;
  886. start_port = port;
  887. num_ports = num_hcs = 1;
  888. }
  889. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  890. num_ports > 1 ? num_ports - 1 : start_port);
  891. if (NULL != pdev) {
  892. DPRINTK("PCI config space regs:\n");
  893. mv_dump_pci_cfg(pdev, 0x68);
  894. }
  895. DPRINTK("PCI regs:\n");
  896. mv_dump_mem(mmio_base+0xc00, 0x3c);
  897. mv_dump_mem(mmio_base+0xd00, 0x34);
  898. mv_dump_mem(mmio_base+0xf00, 0x4);
  899. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  900. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  901. hc_base = mv_hc_base(mmio_base, hc);
  902. DPRINTK("HC regs (HC %i):\n", hc);
  903. mv_dump_mem(hc_base, 0x1c);
  904. }
  905. for (p = start_port; p < start_port + num_ports; p++) {
  906. port_base = mv_port_base(mmio_base, p);
  907. DPRINTK("EDMA regs (port %i):\n", p);
  908. mv_dump_mem(port_base, 0x54);
  909. DPRINTK("SATA regs (port %i):\n", p);
  910. mv_dump_mem(port_base+0x300, 0x60);
  911. }
  912. #endif
  913. }
  914. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  915. {
  916. unsigned int ofs;
  917. switch (sc_reg_in) {
  918. case SCR_STATUS:
  919. case SCR_CONTROL:
  920. case SCR_ERROR:
  921. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  922. break;
  923. case SCR_ACTIVE:
  924. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  925. break;
  926. default:
  927. ofs = 0xffffffffU;
  928. break;
  929. }
  930. return ofs;
  931. }
  932. static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
  933. {
  934. unsigned int ofs = mv_scr_offset(sc_reg_in);
  935. if (ofs != 0xffffffffU) {
  936. *val = readl(mv_ap_base(ap) + ofs);
  937. return 0;
  938. } else
  939. return -EINVAL;
  940. }
  941. static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  942. {
  943. unsigned int ofs = mv_scr_offset(sc_reg_in);
  944. if (ofs != 0xffffffffU) {
  945. writelfl(val, mv_ap_base(ap) + ofs);
  946. return 0;
  947. } else
  948. return -EINVAL;
  949. }
  950. static void mv6_dev_config(struct ata_device *adev)
  951. {
  952. /*
  953. * Deal with Gen-II ("mv6") hardware quirks/restrictions:
  954. *
  955. * Gen-II does not support NCQ over a port multiplier
  956. * (no FIS-based switching).
  957. *
  958. * We don't have hob_nsect when doing NCQ commands on Gen-II.
  959. * See mv_qc_prep() for more info.
  960. */
  961. if (adev->flags & ATA_DFLAG_NCQ) {
  962. if (sata_pmp_attached(adev->link->ap)) {
  963. adev->flags &= ~ATA_DFLAG_NCQ;
  964. ata_dev_printk(adev, KERN_INFO,
  965. "NCQ disabled for command-based switching\n");
  966. } else if (adev->max_sectors > GEN_II_NCQ_MAX_SECTORS) {
  967. adev->max_sectors = GEN_II_NCQ_MAX_SECTORS;
  968. ata_dev_printk(adev, KERN_INFO,
  969. "max_sectors limited to %u for NCQ\n",
  970. adev->max_sectors);
  971. }
  972. }
  973. }
  974. static int mv_qc_defer(struct ata_queued_cmd *qc)
  975. {
  976. struct ata_link *link = qc->dev->link;
  977. struct ata_port *ap = link->ap;
  978. struct mv_port_priv *pp = ap->private_data;
  979. /*
  980. * Don't allow new commands if we're in a delayed EH state
  981. * for NCQ and/or FIS-based switching.
  982. */
  983. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
  984. return ATA_DEFER_PORT;
  985. /*
  986. * If the port is completely idle, then allow the new qc.
  987. */
  988. if (ap->nr_active_links == 0)
  989. return 0;
  990. if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
  991. /*
  992. * The port is operating in host queuing mode (EDMA).
  993. * It can accomodate a new qc if the qc protocol
  994. * is compatible with the current host queue mode.
  995. */
  996. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
  997. /*
  998. * The host queue (EDMA) is in NCQ mode.
  999. * If the new qc is also an NCQ command,
  1000. * then allow the new qc.
  1001. */
  1002. if (qc->tf.protocol == ATA_PROT_NCQ)
  1003. return 0;
  1004. } else {
  1005. /*
  1006. * The host queue (EDMA) is in non-NCQ, DMA mode.
  1007. * If the new qc is also a non-NCQ, DMA command,
  1008. * then allow the new qc.
  1009. */
  1010. if (qc->tf.protocol == ATA_PROT_DMA)
  1011. return 0;
  1012. }
  1013. }
  1014. return ATA_DEFER_PORT;
  1015. }
  1016. static void mv_config_fbs(void __iomem *port_mmio, int want_ncq, int want_fbs)
  1017. {
  1018. u32 new_fiscfg, old_fiscfg;
  1019. u32 new_ltmode, old_ltmode;
  1020. u32 new_haltcond, old_haltcond;
  1021. old_fiscfg = readl(port_mmio + FISCFG_OFS);
  1022. old_ltmode = readl(port_mmio + LTMODE_OFS);
  1023. old_haltcond = readl(port_mmio + EDMA_HALTCOND_OFS);
  1024. new_fiscfg = old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
  1025. new_ltmode = old_ltmode & ~LTMODE_BIT8;
  1026. new_haltcond = old_haltcond | EDMA_ERR_DEV;
  1027. if (want_fbs) {
  1028. new_fiscfg = old_fiscfg | FISCFG_SINGLE_SYNC;
  1029. new_ltmode = old_ltmode | LTMODE_BIT8;
  1030. if (want_ncq)
  1031. new_haltcond &= ~EDMA_ERR_DEV;
  1032. else
  1033. new_fiscfg |= FISCFG_WAIT_DEV_ERR;
  1034. }
  1035. if (new_fiscfg != old_fiscfg)
  1036. writelfl(new_fiscfg, port_mmio + FISCFG_OFS);
  1037. if (new_ltmode != old_ltmode)
  1038. writelfl(new_ltmode, port_mmio + LTMODE_OFS);
  1039. if (new_haltcond != old_haltcond)
  1040. writelfl(new_haltcond, port_mmio + EDMA_HALTCOND_OFS);
  1041. }
  1042. static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
  1043. {
  1044. struct mv_host_priv *hpriv = ap->host->private_data;
  1045. u32 old, new;
  1046. /* workaround for 88SX60x1 FEr SATA#25 (part 1) */
  1047. old = readl(hpriv->base + MV_GPIO_PORT_CTL_OFS);
  1048. if (want_ncq)
  1049. new = old | (1 << 22);
  1050. else
  1051. new = old & ~(1 << 22);
  1052. if (new != old)
  1053. writel(new, hpriv->base + MV_GPIO_PORT_CTL_OFS);
  1054. }
  1055. static void mv_edma_cfg(struct ata_port *ap, int want_ncq)
  1056. {
  1057. u32 cfg;
  1058. struct mv_port_priv *pp = ap->private_data;
  1059. struct mv_host_priv *hpriv = ap->host->private_data;
  1060. void __iomem *port_mmio = mv_ap_base(ap);
  1061. /* set up non-NCQ EDMA configuration */
  1062. cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
  1063. pp->pp_flags &= ~MV_PP_FLAG_FBS_EN;
  1064. if (IS_GEN_I(hpriv))
  1065. cfg |= (1 << 8); /* enab config burst size mask */
  1066. else if (IS_GEN_II(hpriv)) {
  1067. cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
  1068. mv_60x1_errata_sata25(ap, want_ncq);
  1069. } else if (IS_GEN_IIE(hpriv)) {
  1070. int want_fbs = sata_pmp_attached(ap);
  1071. /*
  1072. * Possible future enhancement:
  1073. *
  1074. * The chip can use FBS with non-NCQ, if we allow it,
  1075. * But first we need to have the error handling in place
  1076. * for this mode (datasheet section 7.3.15.4.2.3).
  1077. * So disallow non-NCQ FBS for now.
  1078. */
  1079. want_fbs &= want_ncq;
  1080. mv_config_fbs(port_mmio, want_ncq, want_fbs);
  1081. if (want_fbs) {
  1082. pp->pp_flags |= MV_PP_FLAG_FBS_EN;
  1083. cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
  1084. }
  1085. cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
  1086. cfg |= (1 << 22); /* enab 4-entry host queue cache */
  1087. if (HAS_PCI(ap->host))
  1088. cfg |= (1 << 18); /* enab early completion */
  1089. if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
  1090. cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
  1091. }
  1092. if (want_ncq) {
  1093. cfg |= EDMA_CFG_NCQ;
  1094. pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
  1095. } else
  1096. pp->pp_flags &= ~MV_PP_FLAG_NCQ_EN;
  1097. writelfl(cfg, port_mmio + EDMA_CFG_OFS);
  1098. }
  1099. static void mv_port_free_dma_mem(struct ata_port *ap)
  1100. {
  1101. struct mv_host_priv *hpriv = ap->host->private_data;
  1102. struct mv_port_priv *pp = ap->private_data;
  1103. int tag;
  1104. if (pp->crqb) {
  1105. dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
  1106. pp->crqb = NULL;
  1107. }
  1108. if (pp->crpb) {
  1109. dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
  1110. pp->crpb = NULL;
  1111. }
  1112. /*
  1113. * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
  1114. * For later hardware, we have one unique sg_tbl per NCQ tag.
  1115. */
  1116. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  1117. if (pp->sg_tbl[tag]) {
  1118. if (tag == 0 || !IS_GEN_I(hpriv))
  1119. dma_pool_free(hpriv->sg_tbl_pool,
  1120. pp->sg_tbl[tag],
  1121. pp->sg_tbl_dma[tag]);
  1122. pp->sg_tbl[tag] = NULL;
  1123. }
  1124. }
  1125. }
  1126. /**
  1127. * mv_port_start - Port specific init/start routine.
  1128. * @ap: ATA channel to manipulate
  1129. *
  1130. * Allocate and point to DMA memory, init port private memory,
  1131. * zero indices.
  1132. *
  1133. * LOCKING:
  1134. * Inherited from caller.
  1135. */
  1136. static int mv_port_start(struct ata_port *ap)
  1137. {
  1138. struct device *dev = ap->host->dev;
  1139. struct mv_host_priv *hpriv = ap->host->private_data;
  1140. struct mv_port_priv *pp;
  1141. int tag;
  1142. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1143. if (!pp)
  1144. return -ENOMEM;
  1145. ap->private_data = pp;
  1146. pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
  1147. if (!pp->crqb)
  1148. return -ENOMEM;
  1149. memset(pp->crqb, 0, MV_CRQB_Q_SZ);
  1150. pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
  1151. if (!pp->crpb)
  1152. goto out_port_free_dma_mem;
  1153. memset(pp->crpb, 0, MV_CRPB_Q_SZ);
  1154. /*
  1155. * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
  1156. * For later hardware, we need one unique sg_tbl per NCQ tag.
  1157. */
  1158. for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
  1159. if (tag == 0 || !IS_GEN_I(hpriv)) {
  1160. pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
  1161. GFP_KERNEL, &pp->sg_tbl_dma[tag]);
  1162. if (!pp->sg_tbl[tag])
  1163. goto out_port_free_dma_mem;
  1164. } else {
  1165. pp->sg_tbl[tag] = pp->sg_tbl[0];
  1166. pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
  1167. }
  1168. }
  1169. return 0;
  1170. out_port_free_dma_mem:
  1171. mv_port_free_dma_mem(ap);
  1172. return -ENOMEM;
  1173. }
  1174. /**
  1175. * mv_port_stop - Port specific cleanup/stop routine.
  1176. * @ap: ATA channel to manipulate
  1177. *
  1178. * Stop DMA, cleanup port memory.
  1179. *
  1180. * LOCKING:
  1181. * This routine uses the host lock to protect the DMA stop.
  1182. */
  1183. static void mv_port_stop(struct ata_port *ap)
  1184. {
  1185. mv_stop_edma(ap);
  1186. mv_port_free_dma_mem(ap);
  1187. }
  1188. /**
  1189. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  1190. * @qc: queued command whose SG list to source from
  1191. *
  1192. * Populate the SG list and mark the last entry.
  1193. *
  1194. * LOCKING:
  1195. * Inherited from caller.
  1196. */
  1197. static void mv_fill_sg(struct ata_queued_cmd *qc)
  1198. {
  1199. struct mv_port_priv *pp = qc->ap->private_data;
  1200. struct scatterlist *sg;
  1201. struct mv_sg *mv_sg, *last_sg = NULL;
  1202. unsigned int si;
  1203. mv_sg = pp->sg_tbl[qc->tag];
  1204. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1205. dma_addr_t addr = sg_dma_address(sg);
  1206. u32 sg_len = sg_dma_len(sg);
  1207. while (sg_len) {
  1208. u32 offset = addr & 0xffff;
  1209. u32 len = sg_len;
  1210. if ((offset + sg_len > 0x10000))
  1211. len = 0x10000 - offset;
  1212. mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1213. mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1214. mv_sg->flags_size = cpu_to_le32(len & 0xffff);
  1215. sg_len -= len;
  1216. addr += len;
  1217. last_sg = mv_sg;
  1218. mv_sg++;
  1219. }
  1220. }
  1221. if (likely(last_sg))
  1222. last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  1223. }
  1224. static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
  1225. {
  1226. u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  1227. (last ? CRQB_CMD_LAST : 0);
  1228. *cmdw = cpu_to_le16(tmp);
  1229. }
  1230. /**
  1231. * mv_qc_prep - Host specific command preparation.
  1232. * @qc: queued command to prepare
  1233. *
  1234. * This routine simply redirects to the general purpose routine
  1235. * if command is not DMA. Else, it handles prep of the CRQB
  1236. * (command request block), does some sanity checking, and calls
  1237. * the SG load routine.
  1238. *
  1239. * LOCKING:
  1240. * Inherited from caller.
  1241. */
  1242. static void mv_qc_prep(struct ata_queued_cmd *qc)
  1243. {
  1244. struct ata_port *ap = qc->ap;
  1245. struct mv_port_priv *pp = ap->private_data;
  1246. __le16 *cw;
  1247. struct ata_taskfile *tf;
  1248. u16 flags = 0;
  1249. unsigned in_index;
  1250. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1251. (qc->tf.protocol != ATA_PROT_NCQ))
  1252. return;
  1253. /* Fill in command request block
  1254. */
  1255. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1256. flags |= CRQB_FLAG_READ;
  1257. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1258. flags |= qc->tag << CRQB_TAG_SHIFT;
  1259. flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
  1260. /* get current queue index from software */
  1261. in_index = pp->req_idx;
  1262. pp->crqb[in_index].sg_addr =
  1263. cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1264. pp->crqb[in_index].sg_addr_hi =
  1265. cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1266. pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
  1267. cw = &pp->crqb[in_index].ata_cmd[0];
  1268. tf = &qc->tf;
  1269. /* Sadly, the CRQB cannot accomodate all registers--there are
  1270. * only 11 bytes...so we must pick and choose required
  1271. * registers based on the command. So, we drop feature and
  1272. * hob_feature for [RW] DMA commands, but they are needed for
  1273. * NCQ. NCQ will drop hob_nsect.
  1274. */
  1275. switch (tf->command) {
  1276. case ATA_CMD_READ:
  1277. case ATA_CMD_READ_EXT:
  1278. case ATA_CMD_WRITE:
  1279. case ATA_CMD_WRITE_EXT:
  1280. case ATA_CMD_WRITE_FUA_EXT:
  1281. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  1282. break;
  1283. case ATA_CMD_FPDMA_READ:
  1284. case ATA_CMD_FPDMA_WRITE:
  1285. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  1286. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  1287. break;
  1288. default:
  1289. /* The only other commands EDMA supports in non-queued and
  1290. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  1291. * of which are defined/used by Linux. If we get here, this
  1292. * driver needs work.
  1293. *
  1294. * FIXME: modify libata to give qc_prep a return value and
  1295. * return error here.
  1296. */
  1297. BUG_ON(tf->command);
  1298. break;
  1299. }
  1300. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  1301. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  1302. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  1303. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  1304. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  1305. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  1306. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  1307. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  1308. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  1309. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1310. return;
  1311. mv_fill_sg(qc);
  1312. }
  1313. /**
  1314. * mv_qc_prep_iie - Host specific command preparation.
  1315. * @qc: queued command to prepare
  1316. *
  1317. * This routine simply redirects to the general purpose routine
  1318. * if command is not DMA. Else, it handles prep of the CRQB
  1319. * (command request block), does some sanity checking, and calls
  1320. * the SG load routine.
  1321. *
  1322. * LOCKING:
  1323. * Inherited from caller.
  1324. */
  1325. static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
  1326. {
  1327. struct ata_port *ap = qc->ap;
  1328. struct mv_port_priv *pp = ap->private_data;
  1329. struct mv_crqb_iie *crqb;
  1330. struct ata_taskfile *tf;
  1331. unsigned in_index;
  1332. u32 flags = 0;
  1333. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1334. (qc->tf.protocol != ATA_PROT_NCQ))
  1335. return;
  1336. /* Fill in Gen IIE command request block */
  1337. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  1338. flags |= CRQB_FLAG_READ;
  1339. WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
  1340. flags |= qc->tag << CRQB_TAG_SHIFT;
  1341. flags |= qc->tag << CRQB_HOSTQ_SHIFT;
  1342. flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
  1343. /* get current queue index from software */
  1344. in_index = pp->req_idx;
  1345. crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
  1346. crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
  1347. crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
  1348. crqb->flags = cpu_to_le32(flags);
  1349. tf = &qc->tf;
  1350. crqb->ata_cmd[0] = cpu_to_le32(
  1351. (tf->command << 16) |
  1352. (tf->feature << 24)
  1353. );
  1354. crqb->ata_cmd[1] = cpu_to_le32(
  1355. (tf->lbal << 0) |
  1356. (tf->lbam << 8) |
  1357. (tf->lbah << 16) |
  1358. (tf->device << 24)
  1359. );
  1360. crqb->ata_cmd[2] = cpu_to_le32(
  1361. (tf->hob_lbal << 0) |
  1362. (tf->hob_lbam << 8) |
  1363. (tf->hob_lbah << 16) |
  1364. (tf->hob_feature << 24)
  1365. );
  1366. crqb->ata_cmd[3] = cpu_to_le32(
  1367. (tf->nsect << 0) |
  1368. (tf->hob_nsect << 8)
  1369. );
  1370. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1371. return;
  1372. mv_fill_sg(qc);
  1373. }
  1374. /**
  1375. * mv_qc_issue - Initiate a command to the host
  1376. * @qc: queued command to start
  1377. *
  1378. * This routine simply redirects to the general purpose routine
  1379. * if command is not DMA. Else, it sanity checks our local
  1380. * caches of the request producer/consumer indices then enables
  1381. * DMA and bumps the request producer index.
  1382. *
  1383. * LOCKING:
  1384. * Inherited from caller.
  1385. */
  1386. static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
  1387. {
  1388. struct ata_port *ap = qc->ap;
  1389. void __iomem *port_mmio = mv_ap_base(ap);
  1390. struct mv_port_priv *pp = ap->private_data;
  1391. u32 in_index;
  1392. if ((qc->tf.protocol != ATA_PROT_DMA) &&
  1393. (qc->tf.protocol != ATA_PROT_NCQ)) {
  1394. /*
  1395. * We're about to send a non-EDMA capable command to the
  1396. * port. Turn off EDMA so there won't be problems accessing
  1397. * shadow block, etc registers.
  1398. */
  1399. mv_stop_edma(ap);
  1400. mv_pmp_select(ap, qc->dev->link->pmp);
  1401. return ata_sff_qc_issue(qc);
  1402. }
  1403. mv_start_dma(ap, port_mmio, pp, qc->tf.protocol);
  1404. pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
  1405. in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
  1406. /* and write the request in pointer to kick the EDMA to life */
  1407. writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
  1408. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  1409. return 0;
  1410. }
  1411. static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
  1412. {
  1413. struct mv_port_priv *pp = ap->private_data;
  1414. struct ata_queued_cmd *qc;
  1415. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
  1416. return NULL;
  1417. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1418. if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
  1419. qc = NULL;
  1420. return qc;
  1421. }
  1422. static void mv_pmp_error_handler(struct ata_port *ap)
  1423. {
  1424. unsigned int pmp, pmp_map;
  1425. struct mv_port_priv *pp = ap->private_data;
  1426. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
  1427. /*
  1428. * Perform NCQ error analysis on failed PMPs
  1429. * before we freeze the port entirely.
  1430. *
  1431. * The failed PMPs are marked earlier by mv_pmp_eh_prep().
  1432. */
  1433. pmp_map = pp->delayed_eh_pmp_map;
  1434. pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
  1435. for (pmp = 0; pmp_map != 0; pmp++) {
  1436. unsigned int this_pmp = (1 << pmp);
  1437. if (pmp_map & this_pmp) {
  1438. struct ata_link *link = &ap->pmp_link[pmp];
  1439. pmp_map &= ~this_pmp;
  1440. ata_eh_analyze_ncq_error(link);
  1441. }
  1442. }
  1443. ata_port_freeze(ap);
  1444. }
  1445. sata_pmp_error_handler(ap);
  1446. }
  1447. static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
  1448. {
  1449. void __iomem *port_mmio = mv_ap_base(ap);
  1450. return readl(port_mmio + SATA_TESTCTL_OFS) >> 16;
  1451. }
  1452. static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
  1453. {
  1454. struct ata_eh_info *ehi;
  1455. unsigned int pmp;
  1456. /*
  1457. * Initialize EH info for PMPs which saw device errors
  1458. */
  1459. ehi = &ap->link.eh_info;
  1460. for (pmp = 0; pmp_map != 0; pmp++) {
  1461. unsigned int this_pmp = (1 << pmp);
  1462. if (pmp_map & this_pmp) {
  1463. struct ata_link *link = &ap->pmp_link[pmp];
  1464. pmp_map &= ~this_pmp;
  1465. ehi = &link->eh_info;
  1466. ata_ehi_clear_desc(ehi);
  1467. ata_ehi_push_desc(ehi, "dev err");
  1468. ehi->err_mask |= AC_ERR_DEV;
  1469. ehi->action |= ATA_EH_RESET;
  1470. ata_link_abort(link);
  1471. }
  1472. }
  1473. }
  1474. static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
  1475. {
  1476. struct mv_port_priv *pp = ap->private_data;
  1477. int failed_links;
  1478. unsigned int old_map, new_map;
  1479. /*
  1480. * Device error during FBS+NCQ operation:
  1481. *
  1482. * Set a port flag to prevent further I/O being enqueued.
  1483. * Leave the EDMA running to drain outstanding commands from this port.
  1484. * Perform the post-mortem/EH only when all responses are complete.
  1485. * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
  1486. */
  1487. if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
  1488. pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
  1489. pp->delayed_eh_pmp_map = 0;
  1490. }
  1491. old_map = pp->delayed_eh_pmp_map;
  1492. new_map = old_map | mv_get_err_pmp_map(ap);
  1493. if (old_map != new_map) {
  1494. pp->delayed_eh_pmp_map = new_map;
  1495. mv_pmp_eh_prep(ap, new_map & ~old_map);
  1496. }
  1497. failed_links = hweight16(new_map);
  1498. ata_port_printk(ap, KERN_INFO, "%s: pmp_map=%04x qc_map=%04x "
  1499. "failed_links=%d nr_active_links=%d\n",
  1500. __func__, pp->delayed_eh_pmp_map,
  1501. ap->qc_active, failed_links,
  1502. ap->nr_active_links);
  1503. if (ap->nr_active_links <= failed_links) {
  1504. mv_process_crpb_entries(ap, pp);
  1505. mv_stop_edma(ap);
  1506. mv_eh_freeze(ap);
  1507. ata_port_printk(ap, KERN_INFO, "%s: done\n", __func__);
  1508. return 1; /* handled */
  1509. }
  1510. ata_port_printk(ap, KERN_INFO, "%s: waiting\n", __func__);
  1511. return 1; /* handled */
  1512. }
  1513. static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
  1514. {
  1515. /*
  1516. * Possible future enhancement:
  1517. *
  1518. * FBS+non-NCQ operation is not yet implemented.
  1519. * See related notes in mv_edma_cfg().
  1520. *
  1521. * Device error during FBS+non-NCQ operation:
  1522. *
  1523. * We need to snapshot the shadow registers for each failed command.
  1524. * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
  1525. */
  1526. return 0; /* not handled */
  1527. }
  1528. static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
  1529. {
  1530. struct mv_port_priv *pp = ap->private_data;
  1531. if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
  1532. return 0; /* EDMA was not active: not handled */
  1533. if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
  1534. return 0; /* FBS was not active: not handled */
  1535. if (!(edma_err_cause & EDMA_ERR_DEV))
  1536. return 0; /* non DEV error: not handled */
  1537. edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
  1538. if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
  1539. return 0; /* other problems: not handled */
  1540. if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
  1541. /*
  1542. * EDMA should NOT have self-disabled for this case.
  1543. * If it did, then something is wrong elsewhere,
  1544. * and we cannot handle it here.
  1545. */
  1546. if (edma_err_cause & EDMA_ERR_SELF_DIS) {
  1547. ata_port_printk(ap, KERN_WARNING,
  1548. "%s: err_cause=0x%x pp_flags=0x%x\n",
  1549. __func__, edma_err_cause, pp->pp_flags);
  1550. return 0; /* not handled */
  1551. }
  1552. return mv_handle_fbs_ncq_dev_err(ap);
  1553. } else {
  1554. /*
  1555. * EDMA should have self-disabled for this case.
  1556. * If it did not, then something is wrong elsewhere,
  1557. * and we cannot handle it here.
  1558. */
  1559. if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
  1560. ata_port_printk(ap, KERN_WARNING,
  1561. "%s: err_cause=0x%x pp_flags=0x%x\n",
  1562. __func__, edma_err_cause, pp->pp_flags);
  1563. return 0; /* not handled */
  1564. }
  1565. return mv_handle_fbs_non_ncq_dev_err(ap);
  1566. }
  1567. return 0; /* not handled */
  1568. }
  1569. static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
  1570. {
  1571. struct ata_eh_info *ehi = &ap->link.eh_info;
  1572. char *when = "idle";
  1573. ata_ehi_clear_desc(ehi);
  1574. if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
  1575. when = "disabled";
  1576. } else if (edma_was_enabled) {
  1577. when = "EDMA enabled";
  1578. } else {
  1579. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1580. if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
  1581. when = "polling";
  1582. }
  1583. ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
  1584. ehi->err_mask |= AC_ERR_OTHER;
  1585. ehi->action |= ATA_EH_RESET;
  1586. ata_port_freeze(ap);
  1587. }
  1588. /**
  1589. * mv_err_intr - Handle error interrupts on the port
  1590. * @ap: ATA channel to manipulate
  1591. * @qc: affected command (non-NCQ), or NULL
  1592. *
  1593. * Most cases require a full reset of the chip's state machine,
  1594. * which also performs a COMRESET.
  1595. * Also, if the port disabled DMA, update our cached copy to match.
  1596. *
  1597. * LOCKING:
  1598. * Inherited from caller.
  1599. */
  1600. static void mv_err_intr(struct ata_port *ap)
  1601. {
  1602. void __iomem *port_mmio = mv_ap_base(ap);
  1603. u32 edma_err_cause, eh_freeze_mask, serr = 0;
  1604. u32 fis_cause = 0;
  1605. struct mv_port_priv *pp = ap->private_data;
  1606. struct mv_host_priv *hpriv = ap->host->private_data;
  1607. unsigned int action = 0, err_mask = 0;
  1608. struct ata_eh_info *ehi = &ap->link.eh_info;
  1609. struct ata_queued_cmd *qc;
  1610. int abort = 0;
  1611. /*
  1612. * Read and clear the SError and err_cause bits.
  1613. * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
  1614. * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
  1615. */
  1616. sata_scr_read(&ap->link, SCR_ERROR, &serr);
  1617. sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
  1618. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1619. if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
  1620. fis_cause = readl(port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
  1621. writelfl(~fis_cause, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
  1622. }
  1623. writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1624. if (edma_err_cause & EDMA_ERR_DEV) {
  1625. /*
  1626. * Device errors during FIS-based switching operation
  1627. * require special handling.
  1628. */
  1629. if (mv_handle_dev_err(ap, edma_err_cause))
  1630. return;
  1631. }
  1632. qc = mv_get_active_qc(ap);
  1633. ata_ehi_clear_desc(ehi);
  1634. ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
  1635. edma_err_cause, pp->pp_flags);
  1636. if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
  1637. ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
  1638. if (fis_cause & SATA_FIS_IRQ_AN) {
  1639. u32 ec = edma_err_cause &
  1640. ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
  1641. sata_async_notification(ap);
  1642. if (!ec)
  1643. return; /* Just an AN; no need for the nukes */
  1644. ata_ehi_push_desc(ehi, "SDB notify");
  1645. }
  1646. }
  1647. /*
  1648. * All generations share these EDMA error cause bits:
  1649. */
  1650. if (edma_err_cause & EDMA_ERR_DEV) {
  1651. err_mask |= AC_ERR_DEV;
  1652. action |= ATA_EH_RESET;
  1653. ata_ehi_push_desc(ehi, "dev error");
  1654. }
  1655. if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  1656. EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
  1657. EDMA_ERR_INTRL_PAR)) {
  1658. err_mask |= AC_ERR_ATA_BUS;
  1659. action |= ATA_EH_RESET;
  1660. ata_ehi_push_desc(ehi, "parity error");
  1661. }
  1662. if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
  1663. ata_ehi_hotplugged(ehi);
  1664. ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
  1665. "dev disconnect" : "dev connect");
  1666. action |= ATA_EH_RESET;
  1667. }
  1668. /*
  1669. * Gen-I has a different SELF_DIS bit,
  1670. * different FREEZE bits, and no SERR bit:
  1671. */
  1672. if (IS_GEN_I(hpriv)) {
  1673. eh_freeze_mask = EDMA_EH_FREEZE_5;
  1674. if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
  1675. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1676. ata_ehi_push_desc(ehi, "EDMA self-disable");
  1677. }
  1678. } else {
  1679. eh_freeze_mask = EDMA_EH_FREEZE;
  1680. if (edma_err_cause & EDMA_ERR_SELF_DIS) {
  1681. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1682. ata_ehi_push_desc(ehi, "EDMA self-disable");
  1683. }
  1684. if (edma_err_cause & EDMA_ERR_SERR) {
  1685. ata_ehi_push_desc(ehi, "SError=%08x", serr);
  1686. err_mask |= AC_ERR_ATA_BUS;
  1687. action |= ATA_EH_RESET;
  1688. }
  1689. }
  1690. if (!err_mask) {
  1691. err_mask = AC_ERR_OTHER;
  1692. action |= ATA_EH_RESET;
  1693. }
  1694. ehi->serror |= serr;
  1695. ehi->action |= action;
  1696. if (qc)
  1697. qc->err_mask |= err_mask;
  1698. else
  1699. ehi->err_mask |= err_mask;
  1700. if (err_mask == AC_ERR_DEV) {
  1701. /*
  1702. * Cannot do ata_port_freeze() here,
  1703. * because it would kill PIO access,
  1704. * which is needed for further diagnosis.
  1705. */
  1706. mv_eh_freeze(ap);
  1707. abort = 1;
  1708. } else if (edma_err_cause & eh_freeze_mask) {
  1709. /*
  1710. * Note to self: ata_port_freeze() calls ata_port_abort()
  1711. */
  1712. ata_port_freeze(ap);
  1713. } else {
  1714. abort = 1;
  1715. }
  1716. if (abort) {
  1717. if (qc)
  1718. ata_link_abort(qc->dev->link);
  1719. else
  1720. ata_port_abort(ap);
  1721. }
  1722. }
  1723. static void mv_process_crpb_response(struct ata_port *ap,
  1724. struct mv_crpb *response, unsigned int tag, int ncq_enabled)
  1725. {
  1726. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  1727. if (qc) {
  1728. u8 ata_status;
  1729. u16 edma_status = le16_to_cpu(response->flags);
  1730. /*
  1731. * edma_status from a response queue entry:
  1732. * LSB is from EDMA_ERR_IRQ_CAUSE_OFS (non-NCQ only).
  1733. * MSB is saved ATA status from command completion.
  1734. */
  1735. if (!ncq_enabled) {
  1736. u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
  1737. if (err_cause) {
  1738. /*
  1739. * Error will be seen/handled by mv_err_intr().
  1740. * So do nothing at all here.
  1741. */
  1742. return;
  1743. }
  1744. }
  1745. ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
  1746. if (!ac_err_mask(ata_status))
  1747. ata_qc_complete(qc);
  1748. /* else: leave it for mv_err_intr() */
  1749. } else {
  1750. ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
  1751. __func__, tag);
  1752. }
  1753. }
  1754. static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
  1755. {
  1756. void __iomem *port_mmio = mv_ap_base(ap);
  1757. struct mv_host_priv *hpriv = ap->host->private_data;
  1758. u32 in_index;
  1759. bool work_done = false;
  1760. int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
  1761. /* Get the hardware queue position index */
  1762. in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
  1763. >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
  1764. /* Process new responses from since the last time we looked */
  1765. while (in_index != pp->resp_idx) {
  1766. unsigned int tag;
  1767. struct mv_crpb *response = &pp->crpb[pp->resp_idx];
  1768. pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
  1769. if (IS_GEN_I(hpriv)) {
  1770. /* 50xx: no NCQ, only one command active at a time */
  1771. tag = ap->link.active_tag;
  1772. } else {
  1773. /* Gen II/IIE: get command tag from CRPB entry */
  1774. tag = le16_to_cpu(response->id) & 0x1f;
  1775. }
  1776. mv_process_crpb_response(ap, response, tag, ncq_enabled);
  1777. work_done = true;
  1778. }
  1779. /* Update the software queue position index in hardware */
  1780. if (work_done)
  1781. writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
  1782. (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
  1783. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  1784. }
  1785. static void mv_port_intr(struct ata_port *ap, u32 port_cause)
  1786. {
  1787. struct mv_port_priv *pp;
  1788. int edma_was_enabled;
  1789. if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
  1790. mv_unexpected_intr(ap, 0);
  1791. return;
  1792. }
  1793. /*
  1794. * Grab a snapshot of the EDMA_EN flag setting,
  1795. * so that we have a consistent view for this port,
  1796. * even if something we call of our routines changes it.
  1797. */
  1798. pp = ap->private_data;
  1799. edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
  1800. /*
  1801. * Process completed CRPB response(s) before other events.
  1802. */
  1803. if (edma_was_enabled && (port_cause & DONE_IRQ)) {
  1804. mv_process_crpb_entries(ap, pp);
  1805. if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
  1806. mv_handle_fbs_ncq_dev_err(ap);
  1807. }
  1808. /*
  1809. * Handle chip-reported errors, or continue on to handle PIO.
  1810. */
  1811. if (unlikely(port_cause & ERR_IRQ)) {
  1812. mv_err_intr(ap);
  1813. } else if (!edma_was_enabled) {
  1814. struct ata_queued_cmd *qc = mv_get_active_qc(ap);
  1815. if (qc)
  1816. ata_sff_host_intr(ap, qc);
  1817. else
  1818. mv_unexpected_intr(ap, edma_was_enabled);
  1819. }
  1820. }
  1821. /**
  1822. * mv_host_intr - Handle all interrupts on the given host controller
  1823. * @host: host specific structure
  1824. * @main_irq_cause: Main interrupt cause register for the chip.
  1825. *
  1826. * LOCKING:
  1827. * Inherited from caller.
  1828. */
  1829. static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
  1830. {
  1831. struct mv_host_priv *hpriv = host->private_data;
  1832. void __iomem *mmio = hpriv->base, *hc_mmio;
  1833. unsigned int handled = 0, port;
  1834. for (port = 0; port < hpriv->n_ports; port++) {
  1835. struct ata_port *ap = host->ports[port];
  1836. unsigned int p, shift, hardport, port_cause;
  1837. MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
  1838. /*
  1839. * Each hc within the host has its own hc_irq_cause register,
  1840. * where the interrupting ports bits get ack'd.
  1841. */
  1842. if (hardport == 0) { /* first port on this hc ? */
  1843. u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
  1844. u32 port_mask, ack_irqs;
  1845. /*
  1846. * Skip this entire hc if nothing pending for any ports
  1847. */
  1848. if (!hc_cause) {
  1849. port += MV_PORTS_PER_HC - 1;
  1850. continue;
  1851. }
  1852. /*
  1853. * We don't need/want to read the hc_irq_cause register,
  1854. * because doing so hurts performance, and
  1855. * main_irq_cause already gives us everything we need.
  1856. *
  1857. * But we do have to *write* to the hc_irq_cause to ack
  1858. * the ports that we are handling this time through.
  1859. *
  1860. * This requires that we create a bitmap for those
  1861. * ports which interrupted us, and use that bitmap
  1862. * to ack (only) those ports via hc_irq_cause.
  1863. */
  1864. ack_irqs = 0;
  1865. for (p = 0; p < MV_PORTS_PER_HC; ++p) {
  1866. if ((port + p) >= hpriv->n_ports)
  1867. break;
  1868. port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
  1869. if (hc_cause & port_mask)
  1870. ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
  1871. }
  1872. hc_mmio = mv_hc_base_from_port(mmio, port);
  1873. writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE_OFS);
  1874. handled = 1;
  1875. }
  1876. /*
  1877. * Handle interrupts signalled for this port:
  1878. */
  1879. port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
  1880. if (port_cause)
  1881. mv_port_intr(ap, port_cause);
  1882. }
  1883. return handled;
  1884. }
  1885. static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
  1886. {
  1887. struct mv_host_priv *hpriv = host->private_data;
  1888. struct ata_port *ap;
  1889. struct ata_queued_cmd *qc;
  1890. struct ata_eh_info *ehi;
  1891. unsigned int i, err_mask, printed = 0;
  1892. u32 err_cause;
  1893. err_cause = readl(mmio + hpriv->irq_cause_ofs);
  1894. dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
  1895. err_cause);
  1896. DPRINTK("All regs @ PCI error\n");
  1897. mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
  1898. writelfl(0, mmio + hpriv->irq_cause_ofs);
  1899. for (i = 0; i < host->n_ports; i++) {
  1900. ap = host->ports[i];
  1901. if (!ata_link_offline(&ap->link)) {
  1902. ehi = &ap->link.eh_info;
  1903. ata_ehi_clear_desc(ehi);
  1904. if (!printed++)
  1905. ata_ehi_push_desc(ehi,
  1906. "PCI err cause 0x%08x", err_cause);
  1907. err_mask = AC_ERR_HOST_BUS;
  1908. ehi->action = ATA_EH_RESET;
  1909. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1910. if (qc)
  1911. qc->err_mask |= err_mask;
  1912. else
  1913. ehi->err_mask |= err_mask;
  1914. ata_port_freeze(ap);
  1915. }
  1916. }
  1917. return 1; /* handled */
  1918. }
  1919. /**
  1920. * mv_interrupt - Main interrupt event handler
  1921. * @irq: unused
  1922. * @dev_instance: private data; in this case the host structure
  1923. *
  1924. * Read the read only register to determine if any host
  1925. * controllers have pending interrupts. If so, call lower level
  1926. * routine to handle. Also check for PCI errors which are only
  1927. * reported here.
  1928. *
  1929. * LOCKING:
  1930. * This routine holds the host lock while processing pending
  1931. * interrupts.
  1932. */
  1933. static irqreturn_t mv_interrupt(int irq, void *dev_instance)
  1934. {
  1935. struct ata_host *host = dev_instance;
  1936. struct mv_host_priv *hpriv = host->private_data;
  1937. unsigned int handled = 0;
  1938. u32 main_irq_cause, main_irq_mask;
  1939. spin_lock(&host->lock);
  1940. main_irq_cause = readl(hpriv->main_irq_cause_addr);
  1941. main_irq_mask = readl(hpriv->main_irq_mask_addr);
  1942. /*
  1943. * Deal with cases where we either have nothing pending, or have read
  1944. * a bogus register value which can indicate HW removal or PCI fault.
  1945. */
  1946. if ((main_irq_cause & main_irq_mask) && (main_irq_cause != 0xffffffffU)) {
  1947. if (unlikely((main_irq_cause & PCI_ERR) && HAS_PCI(host)))
  1948. handled = mv_pci_error(host, hpriv->base);
  1949. else
  1950. handled = mv_host_intr(host, main_irq_cause);
  1951. }
  1952. spin_unlock(&host->lock);
  1953. return IRQ_RETVAL(handled);
  1954. }
  1955. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  1956. {
  1957. unsigned int ofs;
  1958. switch (sc_reg_in) {
  1959. case SCR_STATUS:
  1960. case SCR_ERROR:
  1961. case SCR_CONTROL:
  1962. ofs = sc_reg_in * sizeof(u32);
  1963. break;
  1964. default:
  1965. ofs = 0xffffffffU;
  1966. break;
  1967. }
  1968. return ofs;
  1969. }
  1970. static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
  1971. {
  1972. struct mv_host_priv *hpriv = ap->host->private_data;
  1973. void __iomem *mmio = hpriv->base;
  1974. void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
  1975. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1976. if (ofs != 0xffffffffU) {
  1977. *val = readl(addr + ofs);
  1978. return 0;
  1979. } else
  1980. return -EINVAL;
  1981. }
  1982. static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  1983. {
  1984. struct mv_host_priv *hpriv = ap->host->private_data;
  1985. void __iomem *mmio = hpriv->base;
  1986. void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
  1987. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1988. if (ofs != 0xffffffffU) {
  1989. writelfl(val, addr + ofs);
  1990. return 0;
  1991. } else
  1992. return -EINVAL;
  1993. }
  1994. static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
  1995. {
  1996. struct pci_dev *pdev = to_pci_dev(host->dev);
  1997. int early_5080;
  1998. early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
  1999. if (!early_5080) {
  2000. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2001. tmp |= (1 << 0);
  2002. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2003. }
  2004. mv_reset_pci_bus(host, mmio);
  2005. }
  2006. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  2007. {
  2008. writel(0x0fcfffff, mmio + MV_FLASH_CTL_OFS);
  2009. }
  2010. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  2011. void __iomem *mmio)
  2012. {
  2013. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  2014. u32 tmp;
  2015. tmp = readl(phy_mmio + MV5_PHY_MODE);
  2016. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  2017. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  2018. }
  2019. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  2020. {
  2021. u32 tmp;
  2022. writel(0, mmio + MV_GPIO_PORT_CTL_OFS);
  2023. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  2024. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2025. tmp |= ~(1 << 0);
  2026. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  2027. }
  2028. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  2029. unsigned int port)
  2030. {
  2031. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  2032. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  2033. u32 tmp;
  2034. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  2035. if (fix_apm_sq) {
  2036. tmp = readl(phy_mmio + MV5_LTMODE_OFS);
  2037. tmp |= (1 << 19);
  2038. writel(tmp, phy_mmio + MV5_LTMODE_OFS);
  2039. tmp = readl(phy_mmio + MV5_PHY_CTL_OFS);
  2040. tmp &= ~0x3;
  2041. tmp |= 0x1;
  2042. writel(tmp, phy_mmio + MV5_PHY_CTL_OFS);
  2043. }
  2044. tmp = readl(phy_mmio + MV5_PHY_MODE);
  2045. tmp &= ~mask;
  2046. tmp |= hpriv->signal[port].pre;
  2047. tmp |= hpriv->signal[port].amps;
  2048. writel(tmp, phy_mmio + MV5_PHY_MODE);
  2049. }
  2050. #undef ZERO
  2051. #define ZERO(reg) writel(0, port_mmio + (reg))
  2052. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  2053. unsigned int port)
  2054. {
  2055. void __iomem *port_mmio = mv_port_base(mmio, port);
  2056. mv_reset_channel(hpriv, mmio, port);
  2057. ZERO(0x028); /* command */
  2058. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  2059. ZERO(0x004); /* timer */
  2060. ZERO(0x008); /* irq err cause */
  2061. ZERO(0x00c); /* irq err mask */
  2062. ZERO(0x010); /* rq bah */
  2063. ZERO(0x014); /* rq inp */
  2064. ZERO(0x018); /* rq outp */
  2065. ZERO(0x01c); /* respq bah */
  2066. ZERO(0x024); /* respq outp */
  2067. ZERO(0x020); /* respq inp */
  2068. ZERO(0x02c); /* test control */
  2069. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
  2070. }
  2071. #undef ZERO
  2072. #define ZERO(reg) writel(0, hc_mmio + (reg))
  2073. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2074. unsigned int hc)
  2075. {
  2076. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  2077. u32 tmp;
  2078. ZERO(0x00c);
  2079. ZERO(0x010);
  2080. ZERO(0x014);
  2081. ZERO(0x018);
  2082. tmp = readl(hc_mmio + 0x20);
  2083. tmp &= 0x1c1c1c1c;
  2084. tmp |= 0x03030303;
  2085. writel(tmp, hc_mmio + 0x20);
  2086. }
  2087. #undef ZERO
  2088. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2089. unsigned int n_hc)
  2090. {
  2091. unsigned int hc, port;
  2092. for (hc = 0; hc < n_hc; hc++) {
  2093. for (port = 0; port < MV_PORTS_PER_HC; port++)
  2094. mv5_reset_hc_port(hpriv, mmio,
  2095. (hc * MV_PORTS_PER_HC) + port);
  2096. mv5_reset_one_hc(hpriv, mmio, hc);
  2097. }
  2098. return 0;
  2099. }
  2100. #undef ZERO
  2101. #define ZERO(reg) writel(0, mmio + (reg))
  2102. static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
  2103. {
  2104. struct mv_host_priv *hpriv = host->private_data;
  2105. u32 tmp;
  2106. tmp = readl(mmio + MV_PCI_MODE_OFS);
  2107. tmp &= 0xff00ffff;
  2108. writel(tmp, mmio + MV_PCI_MODE_OFS);
  2109. ZERO(MV_PCI_DISC_TIMER);
  2110. ZERO(MV_PCI_MSI_TRIGGER);
  2111. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT_OFS);
  2112. ZERO(MV_PCI_SERR_MASK);
  2113. ZERO(hpriv->irq_cause_ofs);
  2114. ZERO(hpriv->irq_mask_ofs);
  2115. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  2116. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  2117. ZERO(MV_PCI_ERR_ATTRIBUTE);
  2118. ZERO(MV_PCI_ERR_COMMAND);
  2119. }
  2120. #undef ZERO
  2121. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  2122. {
  2123. u32 tmp;
  2124. mv5_reset_flash(hpriv, mmio);
  2125. tmp = readl(mmio + MV_GPIO_PORT_CTL_OFS);
  2126. tmp &= 0x3;
  2127. tmp |= (1 << 5) | (1 << 6);
  2128. writel(tmp, mmio + MV_GPIO_PORT_CTL_OFS);
  2129. }
  2130. /**
  2131. * mv6_reset_hc - Perform the 6xxx global soft reset
  2132. * @mmio: base address of the HBA
  2133. *
  2134. * This routine only applies to 6xxx parts.
  2135. *
  2136. * LOCKING:
  2137. * Inherited from caller.
  2138. */
  2139. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  2140. unsigned int n_hc)
  2141. {
  2142. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  2143. int i, rc = 0;
  2144. u32 t;
  2145. /* Following procedure defined in PCI "main command and status
  2146. * register" table.
  2147. */
  2148. t = readl(reg);
  2149. writel(t | STOP_PCI_MASTER, reg);
  2150. for (i = 0; i < 1000; i++) {
  2151. udelay(1);
  2152. t = readl(reg);
  2153. if (PCI_MASTER_EMPTY & t)
  2154. break;
  2155. }
  2156. if (!(PCI_MASTER_EMPTY & t)) {
  2157. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  2158. rc = 1;
  2159. goto done;
  2160. }
  2161. /* set reset */
  2162. i = 5;
  2163. do {
  2164. writel(t | GLOB_SFT_RST, reg);
  2165. t = readl(reg);
  2166. udelay(1);
  2167. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  2168. if (!(GLOB_SFT_RST & t)) {
  2169. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  2170. rc = 1;
  2171. goto done;
  2172. }
  2173. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  2174. i = 5;
  2175. do {
  2176. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  2177. t = readl(reg);
  2178. udelay(1);
  2179. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  2180. if (GLOB_SFT_RST & t) {
  2181. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  2182. rc = 1;
  2183. }
  2184. done:
  2185. return rc;
  2186. }
  2187. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  2188. void __iomem *mmio)
  2189. {
  2190. void __iomem *port_mmio;
  2191. u32 tmp;
  2192. tmp = readl(mmio + MV_RESET_CFG_OFS);
  2193. if ((tmp & (1 << 0)) == 0) {
  2194. hpriv->signal[idx].amps = 0x7 << 8;
  2195. hpriv->signal[idx].pre = 0x1 << 5;
  2196. return;
  2197. }
  2198. port_mmio = mv_port_base(mmio, idx);
  2199. tmp = readl(port_mmio + PHY_MODE2);
  2200. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  2201. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  2202. }
  2203. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  2204. {
  2205. writel(0x00000060, mmio + MV_GPIO_PORT_CTL_OFS);
  2206. }
  2207. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  2208. unsigned int port)
  2209. {
  2210. void __iomem *port_mmio = mv_port_base(mmio, port);
  2211. u32 hp_flags = hpriv->hp_flags;
  2212. int fix_phy_mode2 =
  2213. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  2214. int fix_phy_mode4 =
  2215. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  2216. u32 m2, tmp;
  2217. if (fix_phy_mode2) {
  2218. m2 = readl(port_mmio + PHY_MODE2);
  2219. m2 &= ~(1 << 16);
  2220. m2 |= (1 << 31);
  2221. writel(m2, port_mmio + PHY_MODE2);
  2222. udelay(200);
  2223. m2 = readl(port_mmio + PHY_MODE2);
  2224. m2 &= ~((1 << 16) | (1 << 31));
  2225. writel(m2, port_mmio + PHY_MODE2);
  2226. udelay(200);
  2227. }
  2228. /* who knows what this magic does */
  2229. tmp = readl(port_mmio + PHY_MODE3);
  2230. tmp &= ~0x7F800000;
  2231. tmp |= 0x2A800000;
  2232. writel(tmp, port_mmio + PHY_MODE3);
  2233. if (fix_phy_mode4) {
  2234. u32 m4;
  2235. m4 = readl(port_mmio + PHY_MODE4);
  2236. if (hp_flags & MV_HP_ERRATA_60X1B2)
  2237. tmp = readl(port_mmio + PHY_MODE3);
  2238. /* workaround for errata FEr SATA#10 (part 1) */
  2239. m4 = (m4 & ~(1 << 1)) | (1 << 0);
  2240. writel(m4, port_mmio + PHY_MODE4);
  2241. if (hp_flags & MV_HP_ERRATA_60X1B2)
  2242. writel(tmp, port_mmio + PHY_MODE3);
  2243. }
  2244. /* Revert values of pre-emphasis and signal amps to the saved ones */
  2245. m2 = readl(port_mmio + PHY_MODE2);
  2246. m2 &= ~MV_M2_PREAMP_MASK;
  2247. m2 |= hpriv->signal[port].amps;
  2248. m2 |= hpriv->signal[port].pre;
  2249. m2 &= ~(1 << 16);
  2250. /* according to mvSata 3.6.1, some IIE values are fixed */
  2251. if (IS_GEN_IIE(hpriv)) {
  2252. m2 &= ~0xC30FF01F;
  2253. m2 |= 0x0000900F;
  2254. }
  2255. writel(m2, port_mmio + PHY_MODE2);
  2256. }
  2257. /* TODO: use the generic LED interface to configure the SATA Presence */
  2258. /* & Acitivy LEDs on the board */
  2259. static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
  2260. void __iomem *mmio)
  2261. {
  2262. return;
  2263. }
  2264. static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
  2265. void __iomem *mmio)
  2266. {
  2267. void __iomem *port_mmio;
  2268. u32 tmp;
  2269. port_mmio = mv_port_base(mmio, idx);
  2270. tmp = readl(port_mmio + PHY_MODE2);
  2271. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  2272. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  2273. }
  2274. #undef ZERO
  2275. #define ZERO(reg) writel(0, port_mmio + (reg))
  2276. static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
  2277. void __iomem *mmio, unsigned int port)
  2278. {
  2279. void __iomem *port_mmio = mv_port_base(mmio, port);
  2280. mv_reset_channel(hpriv, mmio, port);
  2281. ZERO(0x028); /* command */
  2282. writel(0x101f, port_mmio + EDMA_CFG_OFS);
  2283. ZERO(0x004); /* timer */
  2284. ZERO(0x008); /* irq err cause */
  2285. ZERO(0x00c); /* irq err mask */
  2286. ZERO(0x010); /* rq bah */
  2287. ZERO(0x014); /* rq inp */
  2288. ZERO(0x018); /* rq outp */
  2289. ZERO(0x01c); /* respq bah */
  2290. ZERO(0x024); /* respq outp */
  2291. ZERO(0x020); /* respq inp */
  2292. ZERO(0x02c); /* test control */
  2293. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
  2294. }
  2295. #undef ZERO
  2296. #define ZERO(reg) writel(0, hc_mmio + (reg))
  2297. static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
  2298. void __iomem *mmio)
  2299. {
  2300. void __iomem *hc_mmio = mv_hc_base(mmio, 0);
  2301. ZERO(0x00c);
  2302. ZERO(0x010);
  2303. ZERO(0x014);
  2304. }
  2305. #undef ZERO
  2306. static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
  2307. void __iomem *mmio, unsigned int n_hc)
  2308. {
  2309. unsigned int port;
  2310. for (port = 0; port < hpriv->n_ports; port++)
  2311. mv_soc_reset_hc_port(hpriv, mmio, port);
  2312. mv_soc_reset_one_hc(hpriv, mmio);
  2313. return 0;
  2314. }
  2315. static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
  2316. void __iomem *mmio)
  2317. {
  2318. return;
  2319. }
  2320. static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
  2321. {
  2322. return;
  2323. }
  2324. static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
  2325. {
  2326. u32 ifcfg = readl(port_mmio + SATA_INTERFACE_CFG_OFS);
  2327. ifcfg = (ifcfg & 0xf7f) | 0x9b1000; /* from chip spec */
  2328. if (want_gen2i)
  2329. ifcfg |= (1 << 7); /* enable gen2i speed */
  2330. writelfl(ifcfg, port_mmio + SATA_INTERFACE_CFG_OFS);
  2331. }
  2332. static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
  2333. unsigned int port_no)
  2334. {
  2335. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  2336. /*
  2337. * The datasheet warns against setting EDMA_RESET when EDMA is active
  2338. * (but doesn't say what the problem might be). So we first try
  2339. * to disable the EDMA engine before doing the EDMA_RESET operation.
  2340. */
  2341. mv_stop_edma_engine(port_mmio);
  2342. writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
  2343. if (!IS_GEN_I(hpriv)) {
  2344. /* Enable 3.0gb/s link speed: this survives EDMA_RESET */
  2345. mv_setup_ifcfg(port_mmio, 1);
  2346. }
  2347. /*
  2348. * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
  2349. * link, and physical layers. It resets all SATA interface registers
  2350. * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
  2351. */
  2352. writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
  2353. udelay(25); /* allow reset propagation */
  2354. writelfl(0, port_mmio + EDMA_CMD_OFS);
  2355. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  2356. if (IS_GEN_I(hpriv))
  2357. mdelay(1);
  2358. }
  2359. static void mv_pmp_select(struct ata_port *ap, int pmp)
  2360. {
  2361. if (sata_pmp_supported(ap)) {
  2362. void __iomem *port_mmio = mv_ap_base(ap);
  2363. u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
  2364. int old = reg & 0xf;
  2365. if (old != pmp) {
  2366. reg = (reg & ~0xf) | pmp;
  2367. writelfl(reg, port_mmio + SATA_IFCTL_OFS);
  2368. }
  2369. }
  2370. }
  2371. static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
  2372. unsigned long deadline)
  2373. {
  2374. mv_pmp_select(link->ap, sata_srst_pmp(link));
  2375. return sata_std_hardreset(link, class, deadline);
  2376. }
  2377. static int mv_softreset(struct ata_link *link, unsigned int *class,
  2378. unsigned long deadline)
  2379. {
  2380. mv_pmp_select(link->ap, sata_srst_pmp(link));
  2381. return ata_sff_softreset(link, class, deadline);
  2382. }
  2383. static int mv_hardreset(struct ata_link *link, unsigned int *class,
  2384. unsigned long deadline)
  2385. {
  2386. struct ata_port *ap = link->ap;
  2387. struct mv_host_priv *hpriv = ap->host->private_data;
  2388. struct mv_port_priv *pp = ap->private_data;
  2389. void __iomem *mmio = hpriv->base;
  2390. int rc, attempts = 0, extra = 0;
  2391. u32 sstatus;
  2392. bool online;
  2393. mv_reset_channel(hpriv, mmio, ap->port_no);
  2394. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  2395. /* Workaround for errata FEr SATA#10 (part 2) */
  2396. do {
  2397. const unsigned long *timing =
  2398. sata_ehc_deb_timing(&link->eh_context);
  2399. rc = sata_link_hardreset(link, timing, deadline + extra,
  2400. &online, NULL);
  2401. rc = online ? -EAGAIN : rc;
  2402. if (rc)
  2403. return rc;
  2404. sata_scr_read(link, SCR_STATUS, &sstatus);
  2405. if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
  2406. /* Force 1.5gb/s link speed and try again */
  2407. mv_setup_ifcfg(mv_ap_base(ap), 0);
  2408. if (time_after(jiffies + HZ, deadline))
  2409. extra = HZ; /* only extend it once, max */
  2410. }
  2411. } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
  2412. return rc;
  2413. }
  2414. static void mv_eh_freeze(struct ata_port *ap)
  2415. {
  2416. mv_stop_edma(ap);
  2417. mv_enable_port_irqs(ap, 0);
  2418. }
  2419. static void mv_eh_thaw(struct ata_port *ap)
  2420. {
  2421. struct mv_host_priv *hpriv = ap->host->private_data;
  2422. unsigned int port = ap->port_no;
  2423. unsigned int hardport = mv_hardport_from_port(port);
  2424. void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
  2425. void __iomem *port_mmio = mv_ap_base(ap);
  2426. u32 hc_irq_cause;
  2427. /* clear EDMA errors on this port */
  2428. writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  2429. /* clear pending irq events */
  2430. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  2431. hc_irq_cause &= ~((DEV_IRQ | DMA_IRQ) << hardport);
  2432. writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  2433. mv_enable_port_irqs(ap, DONE_IRQ | ERR_IRQ);
  2434. }
  2435. /**
  2436. * mv_port_init - Perform some early initialization on a single port.
  2437. * @port: libata data structure storing shadow register addresses
  2438. * @port_mmio: base address of the port
  2439. *
  2440. * Initialize shadow register mmio addresses, clear outstanding
  2441. * interrupts on the port, and unmask interrupts for the future
  2442. * start of the port.
  2443. *
  2444. * LOCKING:
  2445. * Inherited from caller.
  2446. */
  2447. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  2448. {
  2449. void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
  2450. unsigned serr_ofs;
  2451. /* PIO related setup
  2452. */
  2453. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  2454. port->error_addr =
  2455. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  2456. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  2457. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  2458. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  2459. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  2460. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  2461. port->status_addr =
  2462. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  2463. /* special case: control/altstatus doesn't have ATA_REG_ address */
  2464. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  2465. /* unused: */
  2466. port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
  2467. /* Clear any currently outstanding port interrupt conditions */
  2468. serr_ofs = mv_scr_offset(SCR_ERROR);
  2469. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  2470. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  2471. /* unmask all non-transient EDMA error interrupts */
  2472. writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  2473. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  2474. readl(port_mmio + EDMA_CFG_OFS),
  2475. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  2476. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  2477. }
  2478. static unsigned int mv_in_pcix_mode(struct ata_host *host)
  2479. {
  2480. struct mv_host_priv *hpriv = host->private_data;
  2481. void __iomem *mmio = hpriv->base;
  2482. u32 reg;
  2483. if (!HAS_PCI(host) || !IS_PCIE(hpriv))
  2484. return 0; /* not PCI-X capable */
  2485. reg = readl(mmio + MV_PCI_MODE_OFS);
  2486. if ((reg & MV_PCI_MODE_MASK) == 0)
  2487. return 0; /* conventional PCI mode */
  2488. return 1; /* chip is in PCI-X mode */
  2489. }
  2490. static int mv_pci_cut_through_okay(struct ata_host *host)
  2491. {
  2492. struct mv_host_priv *hpriv = host->private_data;
  2493. void __iomem *mmio = hpriv->base;
  2494. u32 reg;
  2495. if (!mv_in_pcix_mode(host)) {
  2496. reg = readl(mmio + PCI_COMMAND_OFS);
  2497. if (reg & PCI_COMMAND_MRDTRIG)
  2498. return 0; /* not okay */
  2499. }
  2500. return 1; /* okay */
  2501. }
  2502. static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
  2503. {
  2504. struct pci_dev *pdev = to_pci_dev(host->dev);
  2505. struct mv_host_priv *hpriv = host->private_data;
  2506. u32 hp_flags = hpriv->hp_flags;
  2507. switch (board_idx) {
  2508. case chip_5080:
  2509. hpriv->ops = &mv5xxx_ops;
  2510. hp_flags |= MV_HP_GEN_I;
  2511. switch (pdev->revision) {
  2512. case 0x1:
  2513. hp_flags |= MV_HP_ERRATA_50XXB0;
  2514. break;
  2515. case 0x3:
  2516. hp_flags |= MV_HP_ERRATA_50XXB2;
  2517. break;
  2518. default:
  2519. dev_printk(KERN_WARNING, &pdev->dev,
  2520. "Applying 50XXB2 workarounds to unknown rev\n");
  2521. hp_flags |= MV_HP_ERRATA_50XXB2;
  2522. break;
  2523. }
  2524. break;
  2525. case chip_504x:
  2526. case chip_508x:
  2527. hpriv->ops = &mv5xxx_ops;
  2528. hp_flags |= MV_HP_GEN_I;
  2529. switch (pdev->revision) {
  2530. case 0x0:
  2531. hp_flags |= MV_HP_ERRATA_50XXB0;
  2532. break;
  2533. case 0x3:
  2534. hp_flags |= MV_HP_ERRATA_50XXB2;
  2535. break;
  2536. default:
  2537. dev_printk(KERN_WARNING, &pdev->dev,
  2538. "Applying B2 workarounds to unknown rev\n");
  2539. hp_flags |= MV_HP_ERRATA_50XXB2;
  2540. break;
  2541. }
  2542. break;
  2543. case chip_604x:
  2544. case chip_608x:
  2545. hpriv->ops = &mv6xxx_ops;
  2546. hp_flags |= MV_HP_GEN_II;
  2547. switch (pdev->revision) {
  2548. case 0x7:
  2549. hp_flags |= MV_HP_ERRATA_60X1B2;
  2550. break;
  2551. case 0x9:
  2552. hp_flags |= MV_HP_ERRATA_60X1C0;
  2553. break;
  2554. default:
  2555. dev_printk(KERN_WARNING, &pdev->dev,
  2556. "Applying B2 workarounds to unknown rev\n");
  2557. hp_flags |= MV_HP_ERRATA_60X1B2;
  2558. break;
  2559. }
  2560. break;
  2561. case chip_7042:
  2562. hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
  2563. if (pdev->vendor == PCI_VENDOR_ID_TTI &&
  2564. (pdev->device == 0x2300 || pdev->device == 0x2310))
  2565. {
  2566. /*
  2567. * Highpoint RocketRAID PCIe 23xx series cards:
  2568. *
  2569. * Unconfigured drives are treated as "Legacy"
  2570. * by the BIOS, and it overwrites sector 8 with
  2571. * a "Lgcy" metadata block prior to Linux boot.
  2572. *
  2573. * Configured drives (RAID or JBOD) leave sector 8
  2574. * alone, but instead overwrite a high numbered
  2575. * sector for the RAID metadata. This sector can
  2576. * be determined exactly, by truncating the physical
  2577. * drive capacity to a nice even GB value.
  2578. *
  2579. * RAID metadata is at: (dev->n_sectors & ~0xfffff)
  2580. *
  2581. * Warn the user, lest they think we're just buggy.
  2582. */
  2583. printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
  2584. " BIOS CORRUPTS DATA on all attached drives,"
  2585. " regardless of if/how they are configured."
  2586. " BEWARE!\n");
  2587. printk(KERN_WARNING DRV_NAME ": For data safety, do not"
  2588. " use sectors 8-9 on \"Legacy\" drives,"
  2589. " and avoid the final two gigabytes on"
  2590. " all RocketRAID BIOS initialized drives.\n");
  2591. }
  2592. /* drop through */
  2593. case chip_6042:
  2594. hpriv->ops = &mv6xxx_ops;
  2595. hp_flags |= MV_HP_GEN_IIE;
  2596. if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
  2597. hp_flags |= MV_HP_CUT_THROUGH;
  2598. switch (pdev->revision) {
  2599. case 0x0:
  2600. hp_flags |= MV_HP_ERRATA_XX42A0;
  2601. break;
  2602. case 0x1:
  2603. hp_flags |= MV_HP_ERRATA_60X1C0;
  2604. break;
  2605. default:
  2606. dev_printk(KERN_WARNING, &pdev->dev,
  2607. "Applying 60X1C0 workarounds to unknown rev\n");
  2608. hp_flags |= MV_HP_ERRATA_60X1C0;
  2609. break;
  2610. }
  2611. break;
  2612. case chip_soc:
  2613. hpriv->ops = &mv_soc_ops;
  2614. hp_flags |= MV_HP_ERRATA_60X1C0;
  2615. break;
  2616. default:
  2617. dev_printk(KERN_ERR, host->dev,
  2618. "BUG: invalid board index %u\n", board_idx);
  2619. return 1;
  2620. }
  2621. hpriv->hp_flags = hp_flags;
  2622. if (hp_flags & MV_HP_PCIE) {
  2623. hpriv->irq_cause_ofs = PCIE_IRQ_CAUSE_OFS;
  2624. hpriv->irq_mask_ofs = PCIE_IRQ_MASK_OFS;
  2625. hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
  2626. } else {
  2627. hpriv->irq_cause_ofs = PCI_IRQ_CAUSE_OFS;
  2628. hpriv->irq_mask_ofs = PCI_IRQ_MASK_OFS;
  2629. hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
  2630. }
  2631. return 0;
  2632. }
  2633. /**
  2634. * mv_init_host - Perform some early initialization of the host.
  2635. * @host: ATA host to initialize
  2636. * @board_idx: controller index
  2637. *
  2638. * If possible, do an early global reset of the host. Then do
  2639. * our port init and clear/unmask all/relevant host interrupts.
  2640. *
  2641. * LOCKING:
  2642. * Inherited from caller.
  2643. */
  2644. static int mv_init_host(struct ata_host *host, unsigned int board_idx)
  2645. {
  2646. int rc = 0, n_hc, port, hc;
  2647. struct mv_host_priv *hpriv = host->private_data;
  2648. void __iomem *mmio = hpriv->base;
  2649. rc = mv_chip_id(host, board_idx);
  2650. if (rc)
  2651. goto done;
  2652. if (HAS_PCI(host)) {
  2653. hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE_OFS;
  2654. hpriv->main_irq_mask_addr = mmio + PCI_HC_MAIN_IRQ_MASK_OFS;
  2655. } else {
  2656. hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE_OFS;
  2657. hpriv->main_irq_mask_addr = mmio + SOC_HC_MAIN_IRQ_MASK_OFS;
  2658. }
  2659. /* global interrupt mask: 0 == mask everything */
  2660. mv_set_main_irq_mask(host, ~0, 0);
  2661. n_hc = mv_get_hc_count(host->ports[0]->flags);
  2662. for (port = 0; port < host->n_ports; port++)
  2663. hpriv->ops->read_preamp(hpriv, port, mmio);
  2664. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  2665. if (rc)
  2666. goto done;
  2667. hpriv->ops->reset_flash(hpriv, mmio);
  2668. hpriv->ops->reset_bus(host, mmio);
  2669. hpriv->ops->enable_leds(hpriv, mmio);
  2670. for (port = 0; port < host->n_ports; port++) {
  2671. struct ata_port *ap = host->ports[port];
  2672. void __iomem *port_mmio = mv_port_base(mmio, port);
  2673. mv_port_init(&ap->ioaddr, port_mmio);
  2674. #ifdef CONFIG_PCI
  2675. if (HAS_PCI(host)) {
  2676. unsigned int offset = port_mmio - mmio;
  2677. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
  2678. ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
  2679. }
  2680. #endif
  2681. }
  2682. for (hc = 0; hc < n_hc; hc++) {
  2683. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  2684. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  2685. "(before clear)=0x%08x\n", hc,
  2686. readl(hc_mmio + HC_CFG_OFS),
  2687. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  2688. /* Clear any currently outstanding hc interrupt conditions */
  2689. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  2690. }
  2691. if (HAS_PCI(host)) {
  2692. /* Clear any currently outstanding host interrupt conditions */
  2693. writelfl(0, mmio + hpriv->irq_cause_ofs);
  2694. /* and unmask interrupt generation for host regs */
  2695. writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
  2696. /*
  2697. * enable only global host interrupts for now.
  2698. * The per-port interrupts get done later as ports are set up.
  2699. */
  2700. mv_set_main_irq_mask(host, 0, PCI_ERR);
  2701. }
  2702. done:
  2703. return rc;
  2704. }
  2705. static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
  2706. {
  2707. hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
  2708. MV_CRQB_Q_SZ, 0);
  2709. if (!hpriv->crqb_pool)
  2710. return -ENOMEM;
  2711. hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
  2712. MV_CRPB_Q_SZ, 0);
  2713. if (!hpriv->crpb_pool)
  2714. return -ENOMEM;
  2715. hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
  2716. MV_SG_TBL_SZ, 0);
  2717. if (!hpriv->sg_tbl_pool)
  2718. return -ENOMEM;
  2719. return 0;
  2720. }
  2721. static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
  2722. struct mbus_dram_target_info *dram)
  2723. {
  2724. int i;
  2725. for (i = 0; i < 4; i++) {
  2726. writel(0, hpriv->base + WINDOW_CTRL(i));
  2727. writel(0, hpriv->base + WINDOW_BASE(i));
  2728. }
  2729. for (i = 0; i < dram->num_cs; i++) {
  2730. struct mbus_dram_window *cs = dram->cs + i;
  2731. writel(((cs->size - 1) & 0xffff0000) |
  2732. (cs->mbus_attr << 8) |
  2733. (dram->mbus_dram_target_id << 4) | 1,
  2734. hpriv->base + WINDOW_CTRL(i));
  2735. writel(cs->base, hpriv->base + WINDOW_BASE(i));
  2736. }
  2737. }
  2738. /**
  2739. * mv_platform_probe - handle a positive probe of an soc Marvell
  2740. * host
  2741. * @pdev: platform device found
  2742. *
  2743. * LOCKING:
  2744. * Inherited from caller.
  2745. */
  2746. static int mv_platform_probe(struct platform_device *pdev)
  2747. {
  2748. static int printed_version;
  2749. const struct mv_sata_platform_data *mv_platform_data;
  2750. const struct ata_port_info *ppi[] =
  2751. { &mv_port_info[chip_soc], NULL };
  2752. struct ata_host *host;
  2753. struct mv_host_priv *hpriv;
  2754. struct resource *res;
  2755. int n_ports, rc;
  2756. if (!printed_version++)
  2757. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  2758. /*
  2759. * Simple resource validation ..
  2760. */
  2761. if (unlikely(pdev->num_resources != 2)) {
  2762. dev_err(&pdev->dev, "invalid number of resources\n");
  2763. return -EINVAL;
  2764. }
  2765. /*
  2766. * Get the register base first
  2767. */
  2768. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2769. if (res == NULL)
  2770. return -EINVAL;
  2771. /* allocate host */
  2772. mv_platform_data = pdev->dev.platform_data;
  2773. n_ports = mv_platform_data->n_ports;
  2774. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  2775. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  2776. if (!host || !hpriv)
  2777. return -ENOMEM;
  2778. host->private_data = hpriv;
  2779. hpriv->n_ports = n_ports;
  2780. host->iomap = NULL;
  2781. hpriv->base = devm_ioremap(&pdev->dev, res->start,
  2782. res->end - res->start + 1);
  2783. hpriv->base -= MV_SATAHC0_REG_BASE;
  2784. /*
  2785. * (Re-)program MBUS remapping windows if we are asked to.
  2786. */
  2787. if (mv_platform_data->dram != NULL)
  2788. mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
  2789. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  2790. if (rc)
  2791. return rc;
  2792. /* initialize adapter */
  2793. rc = mv_init_host(host, chip_soc);
  2794. if (rc)
  2795. return rc;
  2796. dev_printk(KERN_INFO, &pdev->dev,
  2797. "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
  2798. host->n_ports);
  2799. return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
  2800. IRQF_SHARED, &mv6_sht);
  2801. }
  2802. /*
  2803. *
  2804. * mv_platform_remove - unplug a platform interface
  2805. * @pdev: platform device
  2806. *
  2807. * A platform bus SATA device has been unplugged. Perform the needed
  2808. * cleanup. Also called on module unload for any active devices.
  2809. */
  2810. static int __devexit mv_platform_remove(struct platform_device *pdev)
  2811. {
  2812. struct device *dev = &pdev->dev;
  2813. struct ata_host *host = dev_get_drvdata(dev);
  2814. ata_host_detach(host);
  2815. return 0;
  2816. }
  2817. static struct platform_driver mv_platform_driver = {
  2818. .probe = mv_platform_probe,
  2819. .remove = __devexit_p(mv_platform_remove),
  2820. .driver = {
  2821. .name = DRV_NAME,
  2822. .owner = THIS_MODULE,
  2823. },
  2824. };
  2825. #ifdef CONFIG_PCI
  2826. static int mv_pci_init_one(struct pci_dev *pdev,
  2827. const struct pci_device_id *ent);
  2828. static struct pci_driver mv_pci_driver = {
  2829. .name = DRV_NAME,
  2830. .id_table = mv_pci_tbl,
  2831. .probe = mv_pci_init_one,
  2832. .remove = ata_pci_remove_one,
  2833. };
  2834. /*
  2835. * module options
  2836. */
  2837. static int msi; /* Use PCI msi; either zero (off, default) or non-zero */
  2838. /* move to PCI layer or libata core? */
  2839. static int pci_go_64(struct pci_dev *pdev)
  2840. {
  2841. int rc;
  2842. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  2843. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  2844. if (rc) {
  2845. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  2846. if (rc) {
  2847. dev_printk(KERN_ERR, &pdev->dev,
  2848. "64-bit DMA enable failed\n");
  2849. return rc;
  2850. }
  2851. }
  2852. } else {
  2853. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  2854. if (rc) {
  2855. dev_printk(KERN_ERR, &pdev->dev,
  2856. "32-bit DMA enable failed\n");
  2857. return rc;
  2858. }
  2859. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  2860. if (rc) {
  2861. dev_printk(KERN_ERR, &pdev->dev,
  2862. "32-bit consistent DMA enable failed\n");
  2863. return rc;
  2864. }
  2865. }
  2866. return rc;
  2867. }
  2868. /**
  2869. * mv_print_info - Dump key info to kernel log for perusal.
  2870. * @host: ATA host to print info about
  2871. *
  2872. * FIXME: complete this.
  2873. *
  2874. * LOCKING:
  2875. * Inherited from caller.
  2876. */
  2877. static void mv_print_info(struct ata_host *host)
  2878. {
  2879. struct pci_dev *pdev = to_pci_dev(host->dev);
  2880. struct mv_host_priv *hpriv = host->private_data;
  2881. u8 scc;
  2882. const char *scc_s, *gen;
  2883. /* Use this to determine the HW stepping of the chip so we know
  2884. * what errata to workaround
  2885. */
  2886. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  2887. if (scc == 0)
  2888. scc_s = "SCSI";
  2889. else if (scc == 0x01)
  2890. scc_s = "RAID";
  2891. else
  2892. scc_s = "?";
  2893. if (IS_GEN_I(hpriv))
  2894. gen = "I";
  2895. else if (IS_GEN_II(hpriv))
  2896. gen = "II";
  2897. else if (IS_GEN_IIE(hpriv))
  2898. gen = "IIE";
  2899. else
  2900. gen = "?";
  2901. dev_printk(KERN_INFO, &pdev->dev,
  2902. "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
  2903. gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
  2904. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  2905. }
  2906. /**
  2907. * mv_pci_init_one - handle a positive probe of a PCI Marvell host
  2908. * @pdev: PCI device found
  2909. * @ent: PCI device ID entry for the matched host
  2910. *
  2911. * LOCKING:
  2912. * Inherited from caller.
  2913. */
  2914. static int mv_pci_init_one(struct pci_dev *pdev,
  2915. const struct pci_device_id *ent)
  2916. {
  2917. static int printed_version;
  2918. unsigned int board_idx = (unsigned int)ent->driver_data;
  2919. const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
  2920. struct ata_host *host;
  2921. struct mv_host_priv *hpriv;
  2922. int n_ports, rc;
  2923. if (!printed_version++)
  2924. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  2925. /* allocate host */
  2926. n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
  2927. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  2928. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  2929. if (!host || !hpriv)
  2930. return -ENOMEM;
  2931. host->private_data = hpriv;
  2932. hpriv->n_ports = n_ports;
  2933. /* acquire resources */
  2934. rc = pcim_enable_device(pdev);
  2935. if (rc)
  2936. return rc;
  2937. rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
  2938. if (rc == -EBUSY)
  2939. pcim_pin_device(pdev);
  2940. if (rc)
  2941. return rc;
  2942. host->iomap = pcim_iomap_table(pdev);
  2943. hpriv->base = host->iomap[MV_PRIMARY_BAR];
  2944. rc = pci_go_64(pdev);
  2945. if (rc)
  2946. return rc;
  2947. rc = mv_create_dma_pools(hpriv, &pdev->dev);
  2948. if (rc)
  2949. return rc;
  2950. /* initialize adapter */
  2951. rc = mv_init_host(host, board_idx);
  2952. if (rc)
  2953. return rc;
  2954. /* Enable interrupts */
  2955. if (msi && pci_enable_msi(pdev))
  2956. pci_intx(pdev, 1);
  2957. mv_dump_pci_cfg(pdev, 0x68);
  2958. mv_print_info(host);
  2959. pci_set_master(pdev);
  2960. pci_try_set_mwi(pdev);
  2961. return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
  2962. IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
  2963. }
  2964. #endif
  2965. static int mv_platform_probe(struct platform_device *pdev);
  2966. static int __devexit mv_platform_remove(struct platform_device *pdev);
  2967. static int __init mv_init(void)
  2968. {
  2969. int rc = -ENODEV;
  2970. #ifdef CONFIG_PCI
  2971. rc = pci_register_driver(&mv_pci_driver);
  2972. if (rc < 0)
  2973. return rc;
  2974. #endif
  2975. rc = platform_driver_register(&mv_platform_driver);
  2976. #ifdef CONFIG_PCI
  2977. if (rc < 0)
  2978. pci_unregister_driver(&mv_pci_driver);
  2979. #endif
  2980. return rc;
  2981. }
  2982. static void __exit mv_exit(void)
  2983. {
  2984. #ifdef CONFIG_PCI
  2985. pci_unregister_driver(&mv_pci_driver);
  2986. #endif
  2987. platform_driver_unregister(&mv_platform_driver);
  2988. }
  2989. MODULE_AUTHOR("Brett Russ");
  2990. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  2991. MODULE_LICENSE("GPL");
  2992. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  2993. MODULE_VERSION(DRV_VERSION);
  2994. MODULE_ALIAS("platform:" DRV_NAME);
  2995. #ifdef CONFIG_PCI
  2996. module_param(msi, int, 0444);
  2997. MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
  2998. #endif
  2999. module_init(mv_init);
  3000. module_exit(mv_exit);