sata_mv.c 102 KB

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