qla_nx.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2011 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/delay.h>
  9. #include <linux/pci.h>
  10. #include <linux/ratelimit.h>
  11. #include <linux/vmalloc.h>
  12. #include <scsi/scsi_tcq.h>
  13. #define MASK(n) ((1ULL<<(n))-1)
  14. #define MN_WIN(addr) (((addr & 0x1fc0000) >> 1) | \
  15. ((addr >> 25) & 0x3ff))
  16. #define OCM_WIN(addr) (((addr & 0x1ff0000) >> 1) | \
  17. ((addr >> 25) & 0x3ff))
  18. #define MS_WIN(addr) (addr & 0x0ffc0000)
  19. #define QLA82XX_PCI_MN_2M (0)
  20. #define QLA82XX_PCI_MS_2M (0x80000)
  21. #define QLA82XX_PCI_OCM0_2M (0xc0000)
  22. #define VALID_OCM_ADDR(addr) (((addr) & 0x3f800) != 0x3f800)
  23. #define GET_MEM_OFFS_2M(addr) (addr & MASK(18))
  24. #define BLOCK_PROTECT_BITS 0x0F
  25. /* CRB window related */
  26. #define CRB_BLK(off) ((off >> 20) & 0x3f)
  27. #define CRB_SUBBLK(off) ((off >> 16) & 0xf)
  28. #define CRB_WINDOW_2M (0x130060)
  29. #define QLA82XX_PCI_CAMQM_2M_END (0x04800800UL)
  30. #define CRB_HI(off) ((qla82xx_crb_hub_agt[CRB_BLK(off)] << 20) | \
  31. ((off) & 0xf0000))
  32. #define QLA82XX_PCI_CAMQM_2M_BASE (0x000ff800UL)
  33. #define CRB_INDIRECT_2M (0x1e0000UL)
  34. #define MAX_CRB_XFORM 60
  35. static unsigned long crb_addr_xform[MAX_CRB_XFORM];
  36. int qla82xx_crb_table_initialized;
  37. #define qla82xx_crb_addr_transform(name) \
  38. (crb_addr_xform[QLA82XX_HW_PX_MAP_CRB_##name] = \
  39. QLA82XX_HW_CRB_HUB_AGT_ADR_##name << 20)
  40. static void qla82xx_crb_addr_transform_setup(void)
  41. {
  42. qla82xx_crb_addr_transform(XDMA);
  43. qla82xx_crb_addr_transform(TIMR);
  44. qla82xx_crb_addr_transform(SRE);
  45. qla82xx_crb_addr_transform(SQN3);
  46. qla82xx_crb_addr_transform(SQN2);
  47. qla82xx_crb_addr_transform(SQN1);
  48. qla82xx_crb_addr_transform(SQN0);
  49. qla82xx_crb_addr_transform(SQS3);
  50. qla82xx_crb_addr_transform(SQS2);
  51. qla82xx_crb_addr_transform(SQS1);
  52. qla82xx_crb_addr_transform(SQS0);
  53. qla82xx_crb_addr_transform(RPMX7);
  54. qla82xx_crb_addr_transform(RPMX6);
  55. qla82xx_crb_addr_transform(RPMX5);
  56. qla82xx_crb_addr_transform(RPMX4);
  57. qla82xx_crb_addr_transform(RPMX3);
  58. qla82xx_crb_addr_transform(RPMX2);
  59. qla82xx_crb_addr_transform(RPMX1);
  60. qla82xx_crb_addr_transform(RPMX0);
  61. qla82xx_crb_addr_transform(ROMUSB);
  62. qla82xx_crb_addr_transform(SN);
  63. qla82xx_crb_addr_transform(QMN);
  64. qla82xx_crb_addr_transform(QMS);
  65. qla82xx_crb_addr_transform(PGNI);
  66. qla82xx_crb_addr_transform(PGND);
  67. qla82xx_crb_addr_transform(PGN3);
  68. qla82xx_crb_addr_transform(PGN2);
  69. qla82xx_crb_addr_transform(PGN1);
  70. qla82xx_crb_addr_transform(PGN0);
  71. qla82xx_crb_addr_transform(PGSI);
  72. qla82xx_crb_addr_transform(PGSD);
  73. qla82xx_crb_addr_transform(PGS3);
  74. qla82xx_crb_addr_transform(PGS2);
  75. qla82xx_crb_addr_transform(PGS1);
  76. qla82xx_crb_addr_transform(PGS0);
  77. qla82xx_crb_addr_transform(PS);
  78. qla82xx_crb_addr_transform(PH);
  79. qla82xx_crb_addr_transform(NIU);
  80. qla82xx_crb_addr_transform(I2Q);
  81. qla82xx_crb_addr_transform(EG);
  82. qla82xx_crb_addr_transform(MN);
  83. qla82xx_crb_addr_transform(MS);
  84. qla82xx_crb_addr_transform(CAS2);
  85. qla82xx_crb_addr_transform(CAS1);
  86. qla82xx_crb_addr_transform(CAS0);
  87. qla82xx_crb_addr_transform(CAM);
  88. qla82xx_crb_addr_transform(C2C1);
  89. qla82xx_crb_addr_transform(C2C0);
  90. qla82xx_crb_addr_transform(SMB);
  91. qla82xx_crb_addr_transform(OCM0);
  92. /*
  93. * Used only in P3 just define it for P2 also.
  94. */
  95. qla82xx_crb_addr_transform(I2C0);
  96. qla82xx_crb_table_initialized = 1;
  97. }
  98. struct crb_128M_2M_block_map crb_128M_2M_map[64] = {
  99. {{{0, 0, 0, 0} } },
  100. {{{1, 0x0100000, 0x0102000, 0x120000},
  101. {1, 0x0110000, 0x0120000, 0x130000},
  102. {1, 0x0120000, 0x0122000, 0x124000},
  103. {1, 0x0130000, 0x0132000, 0x126000},
  104. {1, 0x0140000, 0x0142000, 0x128000},
  105. {1, 0x0150000, 0x0152000, 0x12a000},
  106. {1, 0x0160000, 0x0170000, 0x110000},
  107. {1, 0x0170000, 0x0172000, 0x12e000},
  108. {0, 0x0000000, 0x0000000, 0x000000},
  109. {0, 0x0000000, 0x0000000, 0x000000},
  110. {0, 0x0000000, 0x0000000, 0x000000},
  111. {0, 0x0000000, 0x0000000, 0x000000},
  112. {0, 0x0000000, 0x0000000, 0x000000},
  113. {0, 0x0000000, 0x0000000, 0x000000},
  114. {1, 0x01e0000, 0x01e0800, 0x122000},
  115. {0, 0x0000000, 0x0000000, 0x000000} } } ,
  116. {{{1, 0x0200000, 0x0210000, 0x180000} } },
  117. {{{0, 0, 0, 0} } },
  118. {{{1, 0x0400000, 0x0401000, 0x169000} } },
  119. {{{1, 0x0500000, 0x0510000, 0x140000} } },
  120. {{{1, 0x0600000, 0x0610000, 0x1c0000} } },
  121. {{{1, 0x0700000, 0x0704000, 0x1b8000} } },
  122. {{{1, 0x0800000, 0x0802000, 0x170000},
  123. {0, 0x0000000, 0x0000000, 0x000000},
  124. {0, 0x0000000, 0x0000000, 0x000000},
  125. {0, 0x0000000, 0x0000000, 0x000000},
  126. {0, 0x0000000, 0x0000000, 0x000000},
  127. {0, 0x0000000, 0x0000000, 0x000000},
  128. {0, 0x0000000, 0x0000000, 0x000000},
  129. {0, 0x0000000, 0x0000000, 0x000000},
  130. {0, 0x0000000, 0x0000000, 0x000000},
  131. {0, 0x0000000, 0x0000000, 0x000000},
  132. {0, 0x0000000, 0x0000000, 0x000000},
  133. {0, 0x0000000, 0x0000000, 0x000000},
  134. {0, 0x0000000, 0x0000000, 0x000000},
  135. {0, 0x0000000, 0x0000000, 0x000000},
  136. {0, 0x0000000, 0x0000000, 0x000000},
  137. {1, 0x08f0000, 0x08f2000, 0x172000} } },
  138. {{{1, 0x0900000, 0x0902000, 0x174000},
  139. {0, 0x0000000, 0x0000000, 0x000000},
  140. {0, 0x0000000, 0x0000000, 0x000000},
  141. {0, 0x0000000, 0x0000000, 0x000000},
  142. {0, 0x0000000, 0x0000000, 0x000000},
  143. {0, 0x0000000, 0x0000000, 0x000000},
  144. {0, 0x0000000, 0x0000000, 0x000000},
  145. {0, 0x0000000, 0x0000000, 0x000000},
  146. {0, 0x0000000, 0x0000000, 0x000000},
  147. {0, 0x0000000, 0x0000000, 0x000000},
  148. {0, 0x0000000, 0x0000000, 0x000000},
  149. {0, 0x0000000, 0x0000000, 0x000000},
  150. {0, 0x0000000, 0x0000000, 0x000000},
  151. {0, 0x0000000, 0x0000000, 0x000000},
  152. {0, 0x0000000, 0x0000000, 0x000000},
  153. {1, 0x09f0000, 0x09f2000, 0x176000} } },
  154. {{{0, 0x0a00000, 0x0a02000, 0x178000},
  155. {0, 0x0000000, 0x0000000, 0x000000},
  156. {0, 0x0000000, 0x0000000, 0x000000},
  157. {0, 0x0000000, 0x0000000, 0x000000},
  158. {0, 0x0000000, 0x0000000, 0x000000},
  159. {0, 0x0000000, 0x0000000, 0x000000},
  160. {0, 0x0000000, 0x0000000, 0x000000},
  161. {0, 0x0000000, 0x0000000, 0x000000},
  162. {0, 0x0000000, 0x0000000, 0x000000},
  163. {0, 0x0000000, 0x0000000, 0x000000},
  164. {0, 0x0000000, 0x0000000, 0x000000},
  165. {0, 0x0000000, 0x0000000, 0x000000},
  166. {0, 0x0000000, 0x0000000, 0x000000},
  167. {0, 0x0000000, 0x0000000, 0x000000},
  168. {0, 0x0000000, 0x0000000, 0x000000},
  169. {1, 0x0af0000, 0x0af2000, 0x17a000} } },
  170. {{{0, 0x0b00000, 0x0b02000, 0x17c000},
  171. {0, 0x0000000, 0x0000000, 0x000000},
  172. {0, 0x0000000, 0x0000000, 0x000000},
  173. {0, 0x0000000, 0x0000000, 0x000000},
  174. {0, 0x0000000, 0x0000000, 0x000000},
  175. {0, 0x0000000, 0x0000000, 0x000000},
  176. {0, 0x0000000, 0x0000000, 0x000000},
  177. {0, 0x0000000, 0x0000000, 0x000000},
  178. {0, 0x0000000, 0x0000000, 0x000000},
  179. {0, 0x0000000, 0x0000000, 0x000000},
  180. {0, 0x0000000, 0x0000000, 0x000000},
  181. {0, 0x0000000, 0x0000000, 0x000000},
  182. {0, 0x0000000, 0x0000000, 0x000000},
  183. {0, 0x0000000, 0x0000000, 0x000000},
  184. {0, 0x0000000, 0x0000000, 0x000000},
  185. {1, 0x0bf0000, 0x0bf2000, 0x17e000} } },
  186. {{{1, 0x0c00000, 0x0c04000, 0x1d4000} } },
  187. {{{1, 0x0d00000, 0x0d04000, 0x1a4000} } },
  188. {{{1, 0x0e00000, 0x0e04000, 0x1a0000} } },
  189. {{{1, 0x0f00000, 0x0f01000, 0x164000} } },
  190. {{{0, 0x1000000, 0x1004000, 0x1a8000} } },
  191. {{{1, 0x1100000, 0x1101000, 0x160000} } },
  192. {{{1, 0x1200000, 0x1201000, 0x161000} } },
  193. {{{1, 0x1300000, 0x1301000, 0x162000} } },
  194. {{{1, 0x1400000, 0x1401000, 0x163000} } },
  195. {{{1, 0x1500000, 0x1501000, 0x165000} } },
  196. {{{1, 0x1600000, 0x1601000, 0x166000} } },
  197. {{{0, 0, 0, 0} } },
  198. {{{0, 0, 0, 0} } },
  199. {{{0, 0, 0, 0} } },
  200. {{{0, 0, 0, 0} } },
  201. {{{0, 0, 0, 0} } },
  202. {{{0, 0, 0, 0} } },
  203. {{{1, 0x1d00000, 0x1d10000, 0x190000} } },
  204. {{{1, 0x1e00000, 0x1e01000, 0x16a000} } },
  205. {{{1, 0x1f00000, 0x1f10000, 0x150000} } },
  206. {{{0} } },
  207. {{{1, 0x2100000, 0x2102000, 0x120000},
  208. {1, 0x2110000, 0x2120000, 0x130000},
  209. {1, 0x2120000, 0x2122000, 0x124000},
  210. {1, 0x2130000, 0x2132000, 0x126000},
  211. {1, 0x2140000, 0x2142000, 0x128000},
  212. {1, 0x2150000, 0x2152000, 0x12a000},
  213. {1, 0x2160000, 0x2170000, 0x110000},
  214. {1, 0x2170000, 0x2172000, 0x12e000},
  215. {0, 0x0000000, 0x0000000, 0x000000},
  216. {0, 0x0000000, 0x0000000, 0x000000},
  217. {0, 0x0000000, 0x0000000, 0x000000},
  218. {0, 0x0000000, 0x0000000, 0x000000},
  219. {0, 0x0000000, 0x0000000, 0x000000},
  220. {0, 0x0000000, 0x0000000, 0x000000},
  221. {0, 0x0000000, 0x0000000, 0x000000},
  222. {0, 0x0000000, 0x0000000, 0x000000} } },
  223. {{{1, 0x2200000, 0x2204000, 0x1b0000} } },
  224. {{{0} } },
  225. {{{0} } },
  226. {{{0} } },
  227. {{{0} } },
  228. {{{0} } },
  229. {{{1, 0x2800000, 0x2804000, 0x1a4000} } },
  230. {{{1, 0x2900000, 0x2901000, 0x16b000} } },
  231. {{{1, 0x2a00000, 0x2a00400, 0x1ac400} } },
  232. {{{1, 0x2b00000, 0x2b00400, 0x1ac800} } },
  233. {{{1, 0x2c00000, 0x2c00400, 0x1acc00} } },
  234. {{{1, 0x2d00000, 0x2d00400, 0x1ad000} } },
  235. {{{1, 0x2e00000, 0x2e00400, 0x1ad400} } },
  236. {{{1, 0x2f00000, 0x2f00400, 0x1ad800} } },
  237. {{{1, 0x3000000, 0x3000400, 0x1adc00} } },
  238. {{{0, 0x3100000, 0x3104000, 0x1a8000} } },
  239. {{{1, 0x3200000, 0x3204000, 0x1d4000} } },
  240. {{{1, 0x3300000, 0x3304000, 0x1a0000} } },
  241. {{{0} } },
  242. {{{1, 0x3500000, 0x3500400, 0x1ac000} } },
  243. {{{1, 0x3600000, 0x3600400, 0x1ae000} } },
  244. {{{1, 0x3700000, 0x3700400, 0x1ae400} } },
  245. {{{1, 0x3800000, 0x3804000, 0x1d0000} } },
  246. {{{1, 0x3900000, 0x3904000, 0x1b4000} } },
  247. {{{1, 0x3a00000, 0x3a04000, 0x1d8000} } },
  248. {{{0} } },
  249. {{{0} } },
  250. {{{1, 0x3d00000, 0x3d04000, 0x1dc000} } },
  251. {{{1, 0x3e00000, 0x3e01000, 0x167000} } },
  252. {{{1, 0x3f00000, 0x3f01000, 0x168000} } }
  253. };
  254. /*
  255. * top 12 bits of crb internal address (hub, agent)
  256. */
  257. unsigned qla82xx_crb_hub_agt[64] = {
  258. 0,
  259. QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
  260. QLA82XX_HW_CRB_HUB_AGT_ADR_MN,
  261. QLA82XX_HW_CRB_HUB_AGT_ADR_MS,
  262. 0,
  263. QLA82XX_HW_CRB_HUB_AGT_ADR_SRE,
  264. QLA82XX_HW_CRB_HUB_AGT_ADR_NIU,
  265. QLA82XX_HW_CRB_HUB_AGT_ADR_QMN,
  266. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN0,
  267. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN1,
  268. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN2,
  269. QLA82XX_HW_CRB_HUB_AGT_ADR_SQN3,
  270. QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
  271. QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
  272. QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
  273. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN4,
  274. QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
  275. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN0,
  276. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN1,
  277. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN2,
  278. QLA82XX_HW_CRB_HUB_AGT_ADR_PGN3,
  279. QLA82XX_HW_CRB_HUB_AGT_ADR_PGND,
  280. QLA82XX_HW_CRB_HUB_AGT_ADR_PGNI,
  281. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS0,
  282. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS1,
  283. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS2,
  284. QLA82XX_HW_CRB_HUB_AGT_ADR_PGS3,
  285. 0,
  286. QLA82XX_HW_CRB_HUB_AGT_ADR_PGSI,
  287. QLA82XX_HW_CRB_HUB_AGT_ADR_SN,
  288. 0,
  289. QLA82XX_HW_CRB_HUB_AGT_ADR_EG,
  290. 0,
  291. QLA82XX_HW_CRB_HUB_AGT_ADR_PS,
  292. QLA82XX_HW_CRB_HUB_AGT_ADR_CAM,
  293. 0,
  294. 0,
  295. 0,
  296. 0,
  297. 0,
  298. QLA82XX_HW_CRB_HUB_AGT_ADR_TIMR,
  299. 0,
  300. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX1,
  301. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX2,
  302. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX3,
  303. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX4,
  304. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX5,
  305. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX6,
  306. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX7,
  307. QLA82XX_HW_CRB_HUB_AGT_ADR_XDMA,
  308. QLA82XX_HW_CRB_HUB_AGT_ADR_I2Q,
  309. QLA82XX_HW_CRB_HUB_AGT_ADR_ROMUSB,
  310. 0,
  311. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX0,
  312. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX8,
  313. QLA82XX_HW_CRB_HUB_AGT_ADR_RPMX9,
  314. QLA82XX_HW_CRB_HUB_AGT_ADR_OCM0,
  315. 0,
  316. QLA82XX_HW_CRB_HUB_AGT_ADR_SMB,
  317. QLA82XX_HW_CRB_HUB_AGT_ADR_I2C0,
  318. QLA82XX_HW_CRB_HUB_AGT_ADR_I2C1,
  319. 0,
  320. QLA82XX_HW_CRB_HUB_AGT_ADR_PGNC,
  321. 0,
  322. };
  323. /* Device states */
  324. char *q_dev_state[] = {
  325. "Unknown",
  326. "Cold",
  327. "Initializing",
  328. "Ready",
  329. "Need Reset",
  330. "Need Quiescent",
  331. "Failed",
  332. "Quiescent",
  333. };
  334. char *qdev_state(uint32_t dev_state)
  335. {
  336. return q_dev_state[dev_state];
  337. }
  338. /*
  339. * In: 'off' is offset from CRB space in 128M pci map
  340. * Out: 'off' is 2M pci map addr
  341. * side effect: lock crb window
  342. */
  343. static void
  344. qla82xx_pci_set_crbwindow_2M(struct qla_hw_data *ha, ulong *off)
  345. {
  346. u32 win_read;
  347. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  348. ha->crb_win = CRB_HI(*off);
  349. writel(ha->crb_win,
  350. (void *)(CRB_WINDOW_2M + ha->nx_pcibase));
  351. /* Read back value to make sure write has gone through before trying
  352. * to use it.
  353. */
  354. win_read = RD_REG_DWORD((void *)(CRB_WINDOW_2M + ha->nx_pcibase));
  355. if (win_read != ha->crb_win) {
  356. ql_dbg(ql_dbg_p3p, vha, 0xb000,
  357. "%s: Written crbwin (0x%x) "
  358. "!= Read crbwin (0x%x), off=0x%lx.\n",
  359. __func__, ha->crb_win, win_read, *off);
  360. }
  361. *off = (*off & MASK(16)) + CRB_INDIRECT_2M + ha->nx_pcibase;
  362. }
  363. static inline unsigned long
  364. qla82xx_pci_set_crbwindow(struct qla_hw_data *ha, u64 off)
  365. {
  366. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  367. /* See if we are currently pointing to the region we want to use next */
  368. if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_DDR_NET)) {
  369. /* No need to change window. PCIX and PCIEregs are in both
  370. * regs are in both windows.
  371. */
  372. return off;
  373. }
  374. if ((off >= QLA82XX_CRB_PCIX_HOST) && (off < QLA82XX_CRB_PCIX_HOST2)) {
  375. /* We are in first CRB window */
  376. if (ha->curr_window != 0)
  377. WARN_ON(1);
  378. return off;
  379. }
  380. if ((off > QLA82XX_CRB_PCIX_HOST2) && (off < QLA82XX_CRB_MAX)) {
  381. /* We are in second CRB window */
  382. off = off - QLA82XX_CRB_PCIX_HOST2 + QLA82XX_CRB_PCIX_HOST;
  383. if (ha->curr_window != 1)
  384. return off;
  385. /* We are in the QM or direct access
  386. * register region - do nothing
  387. */
  388. if ((off >= QLA82XX_PCI_DIRECT_CRB) &&
  389. (off < QLA82XX_PCI_CAMQM_MAX))
  390. return off;
  391. }
  392. /* strange address given */
  393. ql_dbg(ql_dbg_p3p, vha, 0xb001,
  394. "%s: Warning: unm_nic_pci_set_crbwindow "
  395. "called with an unknown address(%llx).\n",
  396. QLA2XXX_DRIVER_NAME, off);
  397. return off;
  398. }
  399. static int
  400. qla82xx_pci_get_crb_addr_2M(struct qla_hw_data *ha, ulong *off)
  401. {
  402. struct crb_128M_2M_sub_block_map *m;
  403. if (*off >= QLA82XX_CRB_MAX)
  404. return -1;
  405. if (*off >= QLA82XX_PCI_CAMQM && (*off < QLA82XX_PCI_CAMQM_2M_END)) {
  406. *off = (*off - QLA82XX_PCI_CAMQM) +
  407. QLA82XX_PCI_CAMQM_2M_BASE + ha->nx_pcibase;
  408. return 0;
  409. }
  410. if (*off < QLA82XX_PCI_CRBSPACE)
  411. return -1;
  412. *off -= QLA82XX_PCI_CRBSPACE;
  413. /* Try direct map */
  414. m = &crb_128M_2M_map[CRB_BLK(*off)].sub_block[CRB_SUBBLK(*off)];
  415. if (m->valid && (m->start_128M <= *off) && (m->end_128M > *off)) {
  416. *off = *off + m->start_2M - m->start_128M + ha->nx_pcibase;
  417. return 0;
  418. }
  419. /* Not in direct map, use crb window */
  420. return 1;
  421. }
  422. #define CRB_WIN_LOCK_TIMEOUT 100000000
  423. static int qla82xx_crb_win_lock(struct qla_hw_data *ha)
  424. {
  425. int done = 0, timeout = 0;
  426. while (!done) {
  427. /* acquire semaphore3 from PCI HW block */
  428. done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_LOCK));
  429. if (done == 1)
  430. break;
  431. if (timeout >= CRB_WIN_LOCK_TIMEOUT)
  432. return -1;
  433. timeout++;
  434. }
  435. qla82xx_wr_32(ha, QLA82XX_CRB_WIN_LOCK_ID, ha->portnum);
  436. return 0;
  437. }
  438. int
  439. qla82xx_wr_32(struct qla_hw_data *ha, ulong off, u32 data)
  440. {
  441. unsigned long flags = 0;
  442. int rv;
  443. rv = qla82xx_pci_get_crb_addr_2M(ha, &off);
  444. BUG_ON(rv == -1);
  445. if (rv == 1) {
  446. write_lock_irqsave(&ha->hw_lock, flags);
  447. qla82xx_crb_win_lock(ha);
  448. qla82xx_pci_set_crbwindow_2M(ha, &off);
  449. }
  450. writel(data, (void __iomem *)off);
  451. if (rv == 1) {
  452. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
  453. write_unlock_irqrestore(&ha->hw_lock, flags);
  454. }
  455. return 0;
  456. }
  457. int
  458. qla82xx_rd_32(struct qla_hw_data *ha, ulong off)
  459. {
  460. unsigned long flags = 0;
  461. int rv;
  462. u32 data;
  463. rv = qla82xx_pci_get_crb_addr_2M(ha, &off);
  464. BUG_ON(rv == -1);
  465. if (rv == 1) {
  466. write_lock_irqsave(&ha->hw_lock, flags);
  467. qla82xx_crb_win_lock(ha);
  468. qla82xx_pci_set_crbwindow_2M(ha, &off);
  469. }
  470. data = RD_REG_DWORD((void __iomem *)off);
  471. if (rv == 1) {
  472. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM7_UNLOCK));
  473. write_unlock_irqrestore(&ha->hw_lock, flags);
  474. }
  475. return data;
  476. }
  477. #define IDC_LOCK_TIMEOUT 100000000
  478. int qla82xx_idc_lock(struct qla_hw_data *ha)
  479. {
  480. int i;
  481. int done = 0, timeout = 0;
  482. while (!done) {
  483. /* acquire semaphore5 from PCI HW block */
  484. done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_LOCK));
  485. if (done == 1)
  486. break;
  487. if (timeout >= IDC_LOCK_TIMEOUT)
  488. return -1;
  489. timeout++;
  490. /* Yield CPU */
  491. if (!in_interrupt())
  492. schedule();
  493. else {
  494. for (i = 0; i < 20; i++)
  495. cpu_relax();
  496. }
  497. }
  498. return 0;
  499. }
  500. void qla82xx_idc_unlock(struct qla_hw_data *ha)
  501. {
  502. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM5_UNLOCK));
  503. }
  504. /* PCI Windowing for DDR regions. */
  505. #define QLA82XX_ADDR_IN_RANGE(addr, low, high) \
  506. (((addr) <= (high)) && ((addr) >= (low)))
  507. /*
  508. * check memory access boundary.
  509. * used by test agent. support ddr access only for now
  510. */
  511. static unsigned long
  512. qla82xx_pci_mem_bound_check(struct qla_hw_data *ha,
  513. unsigned long long addr, int size)
  514. {
  515. if (!QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
  516. QLA82XX_ADDR_DDR_NET_MAX) ||
  517. !QLA82XX_ADDR_IN_RANGE(addr + size - 1, QLA82XX_ADDR_DDR_NET,
  518. QLA82XX_ADDR_DDR_NET_MAX) ||
  519. ((size != 1) && (size != 2) && (size != 4) && (size != 8)))
  520. return 0;
  521. else
  522. return 1;
  523. }
  524. int qla82xx_pci_set_window_warning_count;
  525. static unsigned long
  526. qla82xx_pci_set_window(struct qla_hw_data *ha, unsigned long long addr)
  527. {
  528. int window;
  529. u32 win_read;
  530. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  531. if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
  532. QLA82XX_ADDR_DDR_NET_MAX)) {
  533. /* DDR network side */
  534. window = MN_WIN(addr);
  535. ha->ddr_mn_window = window;
  536. qla82xx_wr_32(ha,
  537. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
  538. win_read = qla82xx_rd_32(ha,
  539. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
  540. if ((win_read << 17) != window) {
  541. ql_dbg(ql_dbg_p3p, vha, 0xb003,
  542. "%s: Written MNwin (0x%x) != Read MNwin (0x%x).\n",
  543. __func__, window, win_read);
  544. }
  545. addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_DDR_NET;
  546. } else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM0,
  547. QLA82XX_ADDR_OCM0_MAX)) {
  548. unsigned int temp1;
  549. if ((addr & 0x00ff800) == 0xff800) {
  550. ql_log(ql_log_warn, vha, 0xb004,
  551. "%s: QM access not handled.\n", __func__);
  552. addr = -1UL;
  553. }
  554. window = OCM_WIN(addr);
  555. ha->ddr_mn_window = window;
  556. qla82xx_wr_32(ha,
  557. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE, window);
  558. win_read = qla82xx_rd_32(ha,
  559. ha->mn_win_crb | QLA82XX_PCI_CRBSPACE);
  560. temp1 = ((window & 0x1FF) << 7) |
  561. ((window & 0x0FFFE0000) >> 17);
  562. if (win_read != temp1) {
  563. ql_log(ql_log_warn, vha, 0xb005,
  564. "%s: Written OCMwin (0x%x) != Read OCMwin (0x%x).\n",
  565. __func__, temp1, win_read);
  566. }
  567. addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_OCM0_2M;
  568. } else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_QDR_NET,
  569. QLA82XX_P3_ADDR_QDR_NET_MAX)) {
  570. /* QDR network side */
  571. window = MS_WIN(addr);
  572. ha->qdr_sn_window = window;
  573. qla82xx_wr_32(ha,
  574. ha->ms_win_crb | QLA82XX_PCI_CRBSPACE, window);
  575. win_read = qla82xx_rd_32(ha,
  576. ha->ms_win_crb | QLA82XX_PCI_CRBSPACE);
  577. if (win_read != window) {
  578. ql_log(ql_log_warn, vha, 0xb006,
  579. "%s: Written MSwin (0x%x) != Read MSwin (0x%x).\n",
  580. __func__, window, win_read);
  581. }
  582. addr = GET_MEM_OFFS_2M(addr) + QLA82XX_PCI_QDR_NET;
  583. } else {
  584. /*
  585. * peg gdb frequently accesses memory that doesn't exist,
  586. * this limits the chit chat so debugging isn't slowed down.
  587. */
  588. if ((qla82xx_pci_set_window_warning_count++ < 8) ||
  589. (qla82xx_pci_set_window_warning_count%64 == 0)) {
  590. ql_log(ql_log_warn, vha, 0xb007,
  591. "%s: Warning:%s Unknown address range!.\n",
  592. __func__, QLA2XXX_DRIVER_NAME);
  593. }
  594. addr = -1UL;
  595. }
  596. return addr;
  597. }
  598. /* check if address is in the same windows as the previous access */
  599. static int qla82xx_pci_is_same_window(struct qla_hw_data *ha,
  600. unsigned long long addr)
  601. {
  602. int window;
  603. unsigned long long qdr_max;
  604. qdr_max = QLA82XX_P3_ADDR_QDR_NET_MAX;
  605. /* DDR network side */
  606. if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_DDR_NET,
  607. QLA82XX_ADDR_DDR_NET_MAX))
  608. BUG();
  609. else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM0,
  610. QLA82XX_ADDR_OCM0_MAX))
  611. return 1;
  612. else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_OCM1,
  613. QLA82XX_ADDR_OCM1_MAX))
  614. return 1;
  615. else if (QLA82XX_ADDR_IN_RANGE(addr, QLA82XX_ADDR_QDR_NET, qdr_max)) {
  616. /* QDR network side */
  617. window = ((addr - QLA82XX_ADDR_QDR_NET) >> 22) & 0x3f;
  618. if (ha->qdr_sn_window == window)
  619. return 1;
  620. }
  621. return 0;
  622. }
  623. static int qla82xx_pci_mem_read_direct(struct qla_hw_data *ha,
  624. u64 off, void *data, int size)
  625. {
  626. unsigned long flags;
  627. void *addr = NULL;
  628. int ret = 0;
  629. u64 start;
  630. uint8_t *mem_ptr = NULL;
  631. unsigned long mem_base;
  632. unsigned long mem_page;
  633. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  634. write_lock_irqsave(&ha->hw_lock, flags);
  635. /*
  636. * If attempting to access unknown address or straddle hw windows,
  637. * do not access.
  638. */
  639. start = qla82xx_pci_set_window(ha, off);
  640. if ((start == -1UL) ||
  641. (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
  642. write_unlock_irqrestore(&ha->hw_lock, flags);
  643. ql_log(ql_log_fatal, vha, 0xb008,
  644. "%s out of bound pci memory "
  645. "access, offset is 0x%llx.\n",
  646. QLA2XXX_DRIVER_NAME, off);
  647. return -1;
  648. }
  649. write_unlock_irqrestore(&ha->hw_lock, flags);
  650. mem_base = pci_resource_start(ha->pdev, 0);
  651. mem_page = start & PAGE_MASK;
  652. /* Map two pages whenever user tries to access addresses in two
  653. * consecutive pages.
  654. */
  655. if (mem_page != ((start + size - 1) & PAGE_MASK))
  656. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE * 2);
  657. else
  658. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
  659. if (mem_ptr == 0UL) {
  660. *(u8 *)data = 0;
  661. return -1;
  662. }
  663. addr = mem_ptr;
  664. addr += start & (PAGE_SIZE - 1);
  665. write_lock_irqsave(&ha->hw_lock, flags);
  666. switch (size) {
  667. case 1:
  668. *(u8 *)data = readb(addr);
  669. break;
  670. case 2:
  671. *(u16 *)data = readw(addr);
  672. break;
  673. case 4:
  674. *(u32 *)data = readl(addr);
  675. break;
  676. case 8:
  677. *(u64 *)data = readq(addr);
  678. break;
  679. default:
  680. ret = -1;
  681. break;
  682. }
  683. write_unlock_irqrestore(&ha->hw_lock, flags);
  684. if (mem_ptr)
  685. iounmap(mem_ptr);
  686. return ret;
  687. }
  688. static int
  689. qla82xx_pci_mem_write_direct(struct qla_hw_data *ha,
  690. u64 off, void *data, int size)
  691. {
  692. unsigned long flags;
  693. void *addr = NULL;
  694. int ret = 0;
  695. u64 start;
  696. uint8_t *mem_ptr = NULL;
  697. unsigned long mem_base;
  698. unsigned long mem_page;
  699. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  700. write_lock_irqsave(&ha->hw_lock, flags);
  701. /*
  702. * If attempting to access unknown address or straddle hw windows,
  703. * do not access.
  704. */
  705. start = qla82xx_pci_set_window(ha, off);
  706. if ((start == -1UL) ||
  707. (qla82xx_pci_is_same_window(ha, off + size - 1) == 0)) {
  708. write_unlock_irqrestore(&ha->hw_lock, flags);
  709. ql_log(ql_log_fatal, vha, 0xb009,
  710. "%s out of bount memory "
  711. "access, offset is 0x%llx.\n",
  712. QLA2XXX_DRIVER_NAME, off);
  713. return -1;
  714. }
  715. write_unlock_irqrestore(&ha->hw_lock, flags);
  716. mem_base = pci_resource_start(ha->pdev, 0);
  717. mem_page = start & PAGE_MASK;
  718. /* Map two pages whenever user tries to access addresses in two
  719. * consecutive pages.
  720. */
  721. if (mem_page != ((start + size - 1) & PAGE_MASK))
  722. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE*2);
  723. else
  724. mem_ptr = ioremap(mem_base + mem_page, PAGE_SIZE);
  725. if (mem_ptr == 0UL)
  726. return -1;
  727. addr = mem_ptr;
  728. addr += start & (PAGE_SIZE - 1);
  729. write_lock_irqsave(&ha->hw_lock, flags);
  730. switch (size) {
  731. case 1:
  732. writeb(*(u8 *)data, addr);
  733. break;
  734. case 2:
  735. writew(*(u16 *)data, addr);
  736. break;
  737. case 4:
  738. writel(*(u32 *)data, addr);
  739. break;
  740. case 8:
  741. writeq(*(u64 *)data, addr);
  742. break;
  743. default:
  744. ret = -1;
  745. break;
  746. }
  747. write_unlock_irqrestore(&ha->hw_lock, flags);
  748. if (mem_ptr)
  749. iounmap(mem_ptr);
  750. return ret;
  751. }
  752. #define MTU_FUDGE_FACTOR 100
  753. static unsigned long
  754. qla82xx_decode_crb_addr(unsigned long addr)
  755. {
  756. int i;
  757. unsigned long base_addr, offset, pci_base;
  758. if (!qla82xx_crb_table_initialized)
  759. qla82xx_crb_addr_transform_setup();
  760. pci_base = ADDR_ERROR;
  761. base_addr = addr & 0xfff00000;
  762. offset = addr & 0x000fffff;
  763. for (i = 0; i < MAX_CRB_XFORM; i++) {
  764. if (crb_addr_xform[i] == base_addr) {
  765. pci_base = i << 20;
  766. break;
  767. }
  768. }
  769. if (pci_base == ADDR_ERROR)
  770. return pci_base;
  771. return pci_base + offset;
  772. }
  773. static long rom_max_timeout = 100;
  774. static long qla82xx_rom_lock_timeout = 100;
  775. static int
  776. qla82xx_rom_lock(struct qla_hw_data *ha)
  777. {
  778. int done = 0, timeout = 0;
  779. while (!done) {
  780. /* acquire semaphore2 from PCI HW block */
  781. done = qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_LOCK));
  782. if (done == 1)
  783. break;
  784. if (timeout >= qla82xx_rom_lock_timeout)
  785. return -1;
  786. timeout++;
  787. }
  788. qla82xx_wr_32(ha, QLA82XX_ROM_LOCK_ID, ROM_LOCK_DRIVER);
  789. return 0;
  790. }
  791. static void
  792. qla82xx_rom_unlock(struct qla_hw_data *ha)
  793. {
  794. qla82xx_rd_32(ha, QLA82XX_PCIE_REG(PCIE_SEM2_UNLOCK));
  795. }
  796. static int
  797. qla82xx_wait_rom_busy(struct qla_hw_data *ha)
  798. {
  799. long timeout = 0;
  800. long done = 0 ;
  801. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  802. while (done == 0) {
  803. done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
  804. done &= 4;
  805. timeout++;
  806. if (timeout >= rom_max_timeout) {
  807. ql_dbg(ql_dbg_p3p, vha, 0xb00a,
  808. "%s: Timeout reached waiting for rom busy.\n",
  809. QLA2XXX_DRIVER_NAME);
  810. return -1;
  811. }
  812. }
  813. return 0;
  814. }
  815. static int
  816. qla82xx_wait_rom_done(struct qla_hw_data *ha)
  817. {
  818. long timeout = 0;
  819. long done = 0 ;
  820. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  821. while (done == 0) {
  822. done = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_STATUS);
  823. done &= 2;
  824. timeout++;
  825. if (timeout >= rom_max_timeout) {
  826. ql_dbg(ql_dbg_p3p, vha, 0xb00b,
  827. "%s: Timeout reached waiting for rom done.\n",
  828. QLA2XXX_DRIVER_NAME);
  829. return -1;
  830. }
  831. }
  832. return 0;
  833. }
  834. static int
  835. qla82xx_do_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
  836. {
  837. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  838. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
  839. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
  840. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
  841. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0xb);
  842. qla82xx_wait_rom_busy(ha);
  843. if (qla82xx_wait_rom_done(ha)) {
  844. ql_log(ql_log_fatal, vha, 0x00ba,
  845. "Error waiting for rom done.\n");
  846. return -1;
  847. }
  848. /* Reset abyte_cnt and dummy_byte_cnt */
  849. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_DUMMY_BYTE_CNT, 0);
  850. udelay(10);
  851. cond_resched();
  852. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
  853. *valp = qla82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
  854. return 0;
  855. }
  856. static int
  857. qla82xx_rom_fast_read(struct qla_hw_data *ha, int addr, int *valp)
  858. {
  859. int ret, loops = 0;
  860. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  861. while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
  862. udelay(100);
  863. schedule();
  864. loops++;
  865. }
  866. if (loops >= 50000) {
  867. ql_log(ql_log_fatal, vha, 0x00b9,
  868. "Failed to aquire SEM2 lock.\n");
  869. return -1;
  870. }
  871. ret = qla82xx_do_rom_fast_read(ha, addr, valp);
  872. qla82xx_rom_unlock(ha);
  873. return ret;
  874. }
  875. static int
  876. qla82xx_read_status_reg(struct qla_hw_data *ha, uint32_t *val)
  877. {
  878. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  879. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_RDSR);
  880. qla82xx_wait_rom_busy(ha);
  881. if (qla82xx_wait_rom_done(ha)) {
  882. ql_log(ql_log_warn, vha, 0xb00c,
  883. "Error waiting for rom done.\n");
  884. return -1;
  885. }
  886. *val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_ROM_RDATA);
  887. return 0;
  888. }
  889. static int
  890. qla82xx_flash_wait_write_finish(struct qla_hw_data *ha)
  891. {
  892. long timeout = 0;
  893. uint32_t done = 1 ;
  894. uint32_t val;
  895. int ret = 0;
  896. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  897. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
  898. while ((done != 0) && (ret == 0)) {
  899. ret = qla82xx_read_status_reg(ha, &val);
  900. done = val & 1;
  901. timeout++;
  902. udelay(10);
  903. cond_resched();
  904. if (timeout >= 50000) {
  905. ql_log(ql_log_warn, vha, 0xb00d,
  906. "Timeout reached waiting for write finish.\n");
  907. return -1;
  908. }
  909. }
  910. return ret;
  911. }
  912. static int
  913. qla82xx_flash_set_write_enable(struct qla_hw_data *ha)
  914. {
  915. uint32_t val;
  916. qla82xx_wait_rom_busy(ha);
  917. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 0);
  918. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WREN);
  919. qla82xx_wait_rom_busy(ha);
  920. if (qla82xx_wait_rom_done(ha))
  921. return -1;
  922. if (qla82xx_read_status_reg(ha, &val) != 0)
  923. return -1;
  924. if ((val & 2) != 2)
  925. return -1;
  926. return 0;
  927. }
  928. static int
  929. qla82xx_write_status_reg(struct qla_hw_data *ha, uint32_t val)
  930. {
  931. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  932. if (qla82xx_flash_set_write_enable(ha))
  933. return -1;
  934. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, val);
  935. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, 0x1);
  936. if (qla82xx_wait_rom_done(ha)) {
  937. ql_log(ql_log_warn, vha, 0xb00e,
  938. "Error waiting for rom done.\n");
  939. return -1;
  940. }
  941. return qla82xx_flash_wait_write_finish(ha);
  942. }
  943. static int
  944. qla82xx_write_disable_flash(struct qla_hw_data *ha)
  945. {
  946. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  947. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_WRDI);
  948. if (qla82xx_wait_rom_done(ha)) {
  949. ql_log(ql_log_warn, vha, 0xb00f,
  950. "Error waiting for rom done.\n");
  951. return -1;
  952. }
  953. return 0;
  954. }
  955. static int
  956. ql82xx_rom_lock_d(struct qla_hw_data *ha)
  957. {
  958. int loops = 0;
  959. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  960. while ((qla82xx_rom_lock(ha) != 0) && (loops < 50000)) {
  961. udelay(100);
  962. cond_resched();
  963. loops++;
  964. }
  965. if (loops >= 50000) {
  966. ql_log(ql_log_warn, vha, 0xb010,
  967. "ROM lock failed.\n");
  968. return -1;
  969. }
  970. return 0;;
  971. }
  972. static int
  973. qla82xx_write_flash_dword(struct qla_hw_data *ha, uint32_t flashaddr,
  974. uint32_t data)
  975. {
  976. int ret = 0;
  977. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  978. ret = ql82xx_rom_lock_d(ha);
  979. if (ret < 0) {
  980. ql_log(ql_log_warn, vha, 0xb011,
  981. "ROM lock failed.\n");
  982. return ret;
  983. }
  984. if (qla82xx_flash_set_write_enable(ha))
  985. goto done_write;
  986. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_WDATA, data);
  987. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, flashaddr);
  988. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
  989. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_PP);
  990. qla82xx_wait_rom_busy(ha);
  991. if (qla82xx_wait_rom_done(ha)) {
  992. ql_log(ql_log_warn, vha, 0xb012,
  993. "Error waiting for rom done.\n");
  994. ret = -1;
  995. goto done_write;
  996. }
  997. ret = qla82xx_flash_wait_write_finish(ha);
  998. done_write:
  999. qla82xx_rom_unlock(ha);
  1000. return ret;
  1001. }
  1002. /* This routine does CRB initialize sequence
  1003. * to put the ISP into operational state
  1004. */
  1005. static int
  1006. qla82xx_pinit_from_rom(scsi_qla_host_t *vha)
  1007. {
  1008. int addr, val;
  1009. int i ;
  1010. struct crb_addr_pair *buf;
  1011. unsigned long off;
  1012. unsigned offset, n;
  1013. struct qla_hw_data *ha = vha->hw;
  1014. struct crb_addr_pair {
  1015. long addr;
  1016. long data;
  1017. };
  1018. /* Halt all the indiviual PEGs and other blocks of the ISP */
  1019. qla82xx_rom_lock(ha);
  1020. /* disable all I2Q */
  1021. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x10, 0x0);
  1022. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x14, 0x0);
  1023. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x18, 0x0);
  1024. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x1c, 0x0);
  1025. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x20, 0x0);
  1026. qla82xx_wr_32(ha, QLA82XX_CRB_I2Q + 0x24, 0x0);
  1027. /* disable all niu interrupts */
  1028. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x40, 0xff);
  1029. /* disable xge rx/tx */
  1030. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x70000, 0x00);
  1031. /* disable xg1 rx/tx */
  1032. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x80000, 0x00);
  1033. /* disable sideband mac */
  1034. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x90000, 0x00);
  1035. /* disable ap0 mac */
  1036. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xa0000, 0x00);
  1037. /* disable ap1 mac */
  1038. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0xb0000, 0x00);
  1039. /* halt sre */
  1040. val = qla82xx_rd_32(ha, QLA82XX_CRB_SRE + 0x1000);
  1041. qla82xx_wr_32(ha, QLA82XX_CRB_SRE + 0x1000, val & (~(0x1)));
  1042. /* halt epg */
  1043. qla82xx_wr_32(ha, QLA82XX_CRB_EPG + 0x1300, 0x1);
  1044. /* halt timers */
  1045. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x0, 0x0);
  1046. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x8, 0x0);
  1047. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x10, 0x0);
  1048. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x18, 0x0);
  1049. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x100, 0x0);
  1050. qla82xx_wr_32(ha, QLA82XX_CRB_TIMER + 0x200, 0x0);
  1051. /* halt pegs */
  1052. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x3c, 1);
  1053. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1 + 0x3c, 1);
  1054. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2 + 0x3c, 1);
  1055. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3 + 0x3c, 1);
  1056. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_4 + 0x3c, 1);
  1057. msleep(20);
  1058. /* big hammer */
  1059. if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
  1060. /* don't reset CAM block on reset */
  1061. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xfeffffff);
  1062. else
  1063. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0xffffffff);
  1064. /* reset ms */
  1065. val = qla82xx_rd_32(ha, QLA82XX_CRB_QDR_NET + 0xe4);
  1066. val |= (1 << 1);
  1067. qla82xx_wr_32(ha, QLA82XX_CRB_QDR_NET + 0xe4, val);
  1068. msleep(20);
  1069. /* unreset ms */
  1070. val = qla82xx_rd_32(ha, QLA82XX_CRB_QDR_NET + 0xe4);
  1071. val &= ~(1 << 1);
  1072. qla82xx_wr_32(ha, QLA82XX_CRB_QDR_NET + 0xe4, val);
  1073. msleep(20);
  1074. qla82xx_rom_unlock(ha);
  1075. /* Read the signature value from the flash.
  1076. * Offset 0: Contain signature (0xcafecafe)
  1077. * Offset 4: Offset and number of addr/value pairs
  1078. * that present in CRB initialize sequence
  1079. */
  1080. if (qla82xx_rom_fast_read(ha, 0, &n) != 0 || n != 0xcafecafeUL ||
  1081. qla82xx_rom_fast_read(ha, 4, &n) != 0) {
  1082. ql_log(ql_log_fatal, vha, 0x006e,
  1083. "Error Reading crb_init area: n: %08x.\n", n);
  1084. return -1;
  1085. }
  1086. /* Offset in flash = lower 16 bits
  1087. * Number of enteries = upper 16 bits
  1088. */
  1089. offset = n & 0xffffU;
  1090. n = (n >> 16) & 0xffffU;
  1091. /* number of addr/value pair should not exceed 1024 enteries */
  1092. if (n >= 1024) {
  1093. ql_log(ql_log_fatal, vha, 0x0071,
  1094. "Card flash not initialized:n=0x%x.\n", n);
  1095. return -1;
  1096. }
  1097. ql_log(ql_log_info, vha, 0x0072,
  1098. "%d CRB init values found in ROM.\n", n);
  1099. buf = kmalloc(n * sizeof(struct crb_addr_pair), GFP_KERNEL);
  1100. if (buf == NULL) {
  1101. ql_log(ql_log_fatal, vha, 0x010c,
  1102. "Unable to allocate memory.\n");
  1103. return -1;
  1104. }
  1105. for (i = 0; i < n; i++) {
  1106. if (qla82xx_rom_fast_read(ha, 8*i + 4*offset, &val) != 0 ||
  1107. qla82xx_rom_fast_read(ha, 8*i + 4*offset + 4, &addr) != 0) {
  1108. kfree(buf);
  1109. return -1;
  1110. }
  1111. buf[i].addr = addr;
  1112. buf[i].data = val;
  1113. }
  1114. for (i = 0; i < n; i++) {
  1115. /* Translate internal CRB initialization
  1116. * address to PCI bus address
  1117. */
  1118. off = qla82xx_decode_crb_addr((unsigned long)buf[i].addr) +
  1119. QLA82XX_PCI_CRBSPACE;
  1120. /* Not all CRB addr/value pair to be written,
  1121. * some of them are skipped
  1122. */
  1123. /* skipping cold reboot MAGIC */
  1124. if (off == QLA82XX_CAM_RAM(0x1fc))
  1125. continue;
  1126. /* do not reset PCI */
  1127. if (off == (ROMUSB_GLB + 0xbc))
  1128. continue;
  1129. /* skip core clock, so that firmware can increase the clock */
  1130. if (off == (ROMUSB_GLB + 0xc8))
  1131. continue;
  1132. /* skip the function enable register */
  1133. if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION))
  1134. continue;
  1135. if (off == QLA82XX_PCIE_REG(PCIE_SETUP_FUNCTION2))
  1136. continue;
  1137. if ((off & 0x0ff00000) == QLA82XX_CRB_SMB)
  1138. continue;
  1139. if ((off & 0x0ff00000) == QLA82XX_CRB_DDR_NET)
  1140. continue;
  1141. if (off == ADDR_ERROR) {
  1142. ql_log(ql_log_fatal, vha, 0x0116,
  1143. "Unknow addr: 0x%08lx.\n", buf[i].addr);
  1144. continue;
  1145. }
  1146. qla82xx_wr_32(ha, off, buf[i].data);
  1147. /* ISP requires much bigger delay to settle down,
  1148. * else crb_window returns 0xffffffff
  1149. */
  1150. if (off == QLA82XX_ROMUSB_GLB_SW_RESET)
  1151. msleep(1000);
  1152. /* ISP requires millisec delay between
  1153. * successive CRB register updation
  1154. */
  1155. msleep(1);
  1156. }
  1157. kfree(buf);
  1158. /* Resetting the data and instruction cache */
  1159. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0xec, 0x1e);
  1160. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_D+0x4c, 8);
  1161. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_I+0x4c, 8);
  1162. /* Clear all protocol processing engines */
  1163. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0x8, 0);
  1164. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0+0xc, 0);
  1165. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0x8, 0);
  1166. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_1+0xc, 0);
  1167. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0x8, 0);
  1168. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_2+0xc, 0);
  1169. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0x8, 0);
  1170. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_3+0xc, 0);
  1171. return 0;
  1172. }
  1173. static int
  1174. qla82xx_pci_mem_write_2M(struct qla_hw_data *ha,
  1175. u64 off, void *data, int size)
  1176. {
  1177. int i, j, ret = 0, loop, sz[2], off0;
  1178. int scale, shift_amount, startword;
  1179. uint32_t temp;
  1180. uint64_t off8, mem_crb, tmpw, word[2] = {0, 0};
  1181. /*
  1182. * If not MN, go check for MS or invalid.
  1183. */
  1184. if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
  1185. mem_crb = QLA82XX_CRB_QDR_NET;
  1186. else {
  1187. mem_crb = QLA82XX_CRB_DDR_NET;
  1188. if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
  1189. return qla82xx_pci_mem_write_direct(ha,
  1190. off, data, size);
  1191. }
  1192. off0 = off & 0x7;
  1193. sz[0] = (size < (8 - off0)) ? size : (8 - off0);
  1194. sz[1] = size - sz[0];
  1195. off8 = off & 0xfffffff0;
  1196. loop = (((off & 0xf) + size - 1) >> 4) + 1;
  1197. shift_amount = 4;
  1198. scale = 2;
  1199. startword = (off & 0xf)/8;
  1200. for (i = 0; i < loop; i++) {
  1201. if (qla82xx_pci_mem_read_2M(ha, off8 +
  1202. (i << shift_amount), &word[i * scale], 8))
  1203. return -1;
  1204. }
  1205. switch (size) {
  1206. case 1:
  1207. tmpw = *((uint8_t *)data);
  1208. break;
  1209. case 2:
  1210. tmpw = *((uint16_t *)data);
  1211. break;
  1212. case 4:
  1213. tmpw = *((uint32_t *)data);
  1214. break;
  1215. case 8:
  1216. default:
  1217. tmpw = *((uint64_t *)data);
  1218. break;
  1219. }
  1220. if (sz[0] == 8) {
  1221. word[startword] = tmpw;
  1222. } else {
  1223. word[startword] &=
  1224. ~((~(~0ULL << (sz[0] * 8))) << (off0 * 8));
  1225. word[startword] |= tmpw << (off0 * 8);
  1226. }
  1227. if (sz[1] != 0) {
  1228. word[startword+1] &= ~(~0ULL << (sz[1] * 8));
  1229. word[startword+1] |= tmpw >> (sz[0] * 8);
  1230. }
  1231. for (i = 0; i < loop; i++) {
  1232. temp = off8 + (i << shift_amount);
  1233. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_LO, temp);
  1234. temp = 0;
  1235. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_ADDR_HI, temp);
  1236. temp = word[i * scale] & 0xffffffff;
  1237. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_LO, temp);
  1238. temp = (word[i * scale] >> 32) & 0xffffffff;
  1239. qla82xx_wr_32(ha, mem_crb+MIU_TEST_AGT_WRDATA_HI, temp);
  1240. temp = word[i*scale + 1] & 0xffffffff;
  1241. qla82xx_wr_32(ha, mem_crb +
  1242. MIU_TEST_AGT_WRDATA_UPPER_LO, temp);
  1243. temp = (word[i*scale + 1] >> 32) & 0xffffffff;
  1244. qla82xx_wr_32(ha, mem_crb +
  1245. MIU_TEST_AGT_WRDATA_UPPER_HI, temp);
  1246. temp = MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
  1247. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1248. temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE | MIU_TA_CTL_WRITE;
  1249. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1250. for (j = 0; j < MAX_CTL_CHECK; j++) {
  1251. temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
  1252. if ((temp & MIU_TA_CTL_BUSY) == 0)
  1253. break;
  1254. }
  1255. if (j >= MAX_CTL_CHECK) {
  1256. if (printk_ratelimit())
  1257. dev_err(&ha->pdev->dev,
  1258. "failed to write through agent.\n");
  1259. ret = -1;
  1260. break;
  1261. }
  1262. }
  1263. return ret;
  1264. }
  1265. static int
  1266. qla82xx_fw_load_from_flash(struct qla_hw_data *ha)
  1267. {
  1268. int i;
  1269. long size = 0;
  1270. long flashaddr = ha->flt_region_bootload << 2;
  1271. long memaddr = BOOTLD_START;
  1272. u64 data;
  1273. u32 high, low;
  1274. size = (IMAGE_START - BOOTLD_START) / 8;
  1275. for (i = 0; i < size; i++) {
  1276. if ((qla82xx_rom_fast_read(ha, flashaddr, (int *)&low)) ||
  1277. (qla82xx_rom_fast_read(ha, flashaddr + 4, (int *)&high))) {
  1278. return -1;
  1279. }
  1280. data = ((u64)high << 32) | low ;
  1281. qla82xx_pci_mem_write_2M(ha, memaddr, &data, 8);
  1282. flashaddr += 8;
  1283. memaddr += 8;
  1284. if (i % 0x1000 == 0)
  1285. msleep(1);
  1286. }
  1287. udelay(100);
  1288. read_lock(&ha->hw_lock);
  1289. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
  1290. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
  1291. read_unlock(&ha->hw_lock);
  1292. return 0;
  1293. }
  1294. int
  1295. qla82xx_pci_mem_read_2M(struct qla_hw_data *ha,
  1296. u64 off, void *data, int size)
  1297. {
  1298. int i, j = 0, k, start, end, loop, sz[2], off0[2];
  1299. int shift_amount;
  1300. uint32_t temp;
  1301. uint64_t off8, val, mem_crb, word[2] = {0, 0};
  1302. /*
  1303. * If not MN, go check for MS or invalid.
  1304. */
  1305. if (off >= QLA82XX_ADDR_QDR_NET && off <= QLA82XX_P3_ADDR_QDR_NET_MAX)
  1306. mem_crb = QLA82XX_CRB_QDR_NET;
  1307. else {
  1308. mem_crb = QLA82XX_CRB_DDR_NET;
  1309. if (qla82xx_pci_mem_bound_check(ha, off, size) == 0)
  1310. return qla82xx_pci_mem_read_direct(ha,
  1311. off, data, size);
  1312. }
  1313. off8 = off & 0xfffffff0;
  1314. off0[0] = off & 0xf;
  1315. sz[0] = (size < (16 - off0[0])) ? size : (16 - off0[0]);
  1316. shift_amount = 4;
  1317. loop = ((off0[0] + size - 1) >> shift_amount) + 1;
  1318. off0[1] = 0;
  1319. sz[1] = size - sz[0];
  1320. for (i = 0; i < loop; i++) {
  1321. temp = off8 + (i << shift_amount);
  1322. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_LO, temp);
  1323. temp = 0;
  1324. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_ADDR_HI, temp);
  1325. temp = MIU_TA_CTL_ENABLE;
  1326. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1327. temp = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
  1328. qla82xx_wr_32(ha, mem_crb + MIU_TEST_AGT_CTRL, temp);
  1329. for (j = 0; j < MAX_CTL_CHECK; j++) {
  1330. temp = qla82xx_rd_32(ha, mem_crb + MIU_TEST_AGT_CTRL);
  1331. if ((temp & MIU_TA_CTL_BUSY) == 0)
  1332. break;
  1333. }
  1334. if (j >= MAX_CTL_CHECK) {
  1335. if (printk_ratelimit())
  1336. dev_err(&ha->pdev->dev,
  1337. "failed to read through agent.\n");
  1338. break;
  1339. }
  1340. start = off0[i] >> 2;
  1341. end = (off0[i] + sz[i] - 1) >> 2;
  1342. for (k = start; k <= end; k++) {
  1343. temp = qla82xx_rd_32(ha,
  1344. mem_crb + MIU_TEST_AGT_RDDATA(k));
  1345. word[i] |= ((uint64_t)temp << (32 * (k & 1)));
  1346. }
  1347. }
  1348. if (j >= MAX_CTL_CHECK)
  1349. return -1;
  1350. if ((off0[0] & 7) == 0) {
  1351. val = word[0];
  1352. } else {
  1353. val = ((word[0] >> (off0[0] * 8)) & (~(~0ULL << (sz[0] * 8)))) |
  1354. ((word[1] & (~(~0ULL << (sz[1] * 8)))) << (sz[0] * 8));
  1355. }
  1356. switch (size) {
  1357. case 1:
  1358. *(uint8_t *)data = val;
  1359. break;
  1360. case 2:
  1361. *(uint16_t *)data = val;
  1362. break;
  1363. case 4:
  1364. *(uint32_t *)data = val;
  1365. break;
  1366. case 8:
  1367. *(uint64_t *)data = val;
  1368. break;
  1369. }
  1370. return 0;
  1371. }
  1372. static struct qla82xx_uri_table_desc *
  1373. qla82xx_get_table_desc(const u8 *unirom, int section)
  1374. {
  1375. uint32_t i;
  1376. struct qla82xx_uri_table_desc *directory =
  1377. (struct qla82xx_uri_table_desc *)&unirom[0];
  1378. __le32 offset;
  1379. __le32 tab_type;
  1380. __le32 entries = cpu_to_le32(directory->num_entries);
  1381. for (i = 0; i < entries; i++) {
  1382. offset = cpu_to_le32(directory->findex) +
  1383. (i * cpu_to_le32(directory->entry_size));
  1384. tab_type = cpu_to_le32(*((u32 *)&unirom[offset] + 8));
  1385. if (tab_type == section)
  1386. return (struct qla82xx_uri_table_desc *)&unirom[offset];
  1387. }
  1388. return NULL;
  1389. }
  1390. static struct qla82xx_uri_data_desc *
  1391. qla82xx_get_data_desc(struct qla_hw_data *ha,
  1392. u32 section, u32 idx_offset)
  1393. {
  1394. const u8 *unirom = ha->hablob->fw->data;
  1395. int idx = cpu_to_le32(*((int *)&unirom[ha->file_prd_off] + idx_offset));
  1396. struct qla82xx_uri_table_desc *tab_desc = NULL;
  1397. __le32 offset;
  1398. tab_desc = qla82xx_get_table_desc(unirom, section);
  1399. if (!tab_desc)
  1400. return NULL;
  1401. offset = cpu_to_le32(tab_desc->findex) +
  1402. (cpu_to_le32(tab_desc->entry_size) * idx);
  1403. return (struct qla82xx_uri_data_desc *)&unirom[offset];
  1404. }
  1405. static u8 *
  1406. qla82xx_get_bootld_offset(struct qla_hw_data *ha)
  1407. {
  1408. u32 offset = BOOTLD_START;
  1409. struct qla82xx_uri_data_desc *uri_desc = NULL;
  1410. if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1411. uri_desc = qla82xx_get_data_desc(ha,
  1412. QLA82XX_URI_DIR_SECT_BOOTLD, QLA82XX_URI_BOOTLD_IDX_OFF);
  1413. if (uri_desc)
  1414. offset = cpu_to_le32(uri_desc->findex);
  1415. }
  1416. return (u8 *)&ha->hablob->fw->data[offset];
  1417. }
  1418. static __le32
  1419. qla82xx_get_fw_size(struct qla_hw_data *ha)
  1420. {
  1421. struct qla82xx_uri_data_desc *uri_desc = NULL;
  1422. if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1423. uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
  1424. QLA82XX_URI_FIRMWARE_IDX_OFF);
  1425. if (uri_desc)
  1426. return cpu_to_le32(uri_desc->size);
  1427. }
  1428. return cpu_to_le32(*(u32 *)&ha->hablob->fw->data[FW_SIZE_OFFSET]);
  1429. }
  1430. static u8 *
  1431. qla82xx_get_fw_offs(struct qla_hw_data *ha)
  1432. {
  1433. u32 offset = IMAGE_START;
  1434. struct qla82xx_uri_data_desc *uri_desc = NULL;
  1435. if (ha->fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1436. uri_desc = qla82xx_get_data_desc(ha, QLA82XX_URI_DIR_SECT_FW,
  1437. QLA82XX_URI_FIRMWARE_IDX_OFF);
  1438. if (uri_desc)
  1439. offset = cpu_to_le32(uri_desc->findex);
  1440. }
  1441. return (u8 *)&ha->hablob->fw->data[offset];
  1442. }
  1443. /* PCI related functions */
  1444. char *
  1445. qla82xx_pci_info_str(struct scsi_qla_host *vha, char *str)
  1446. {
  1447. int pcie_reg;
  1448. struct qla_hw_data *ha = vha->hw;
  1449. char lwstr[6];
  1450. uint16_t lnk;
  1451. pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  1452. pci_read_config_word(ha->pdev, pcie_reg + PCI_EXP_LNKSTA, &lnk);
  1453. ha->link_width = (lnk >> 4) & 0x3f;
  1454. strcpy(str, "PCIe (");
  1455. strcat(str, "2.5Gb/s ");
  1456. snprintf(lwstr, sizeof(lwstr), "x%d)", ha->link_width);
  1457. strcat(str, lwstr);
  1458. return str;
  1459. }
  1460. int qla82xx_pci_region_offset(struct pci_dev *pdev, int region)
  1461. {
  1462. unsigned long val = 0;
  1463. u32 control;
  1464. switch (region) {
  1465. case 0:
  1466. val = 0;
  1467. break;
  1468. case 1:
  1469. pci_read_config_dword(pdev, QLA82XX_PCI_REG_MSIX_TBL, &control);
  1470. val = control + QLA82XX_MSIX_TBL_SPACE;
  1471. break;
  1472. }
  1473. return val;
  1474. }
  1475. int
  1476. qla82xx_iospace_config(struct qla_hw_data *ha)
  1477. {
  1478. uint32_t len = 0;
  1479. if (pci_request_regions(ha->pdev, QLA2XXX_DRIVER_NAME)) {
  1480. ql_log_pci(ql_log_fatal, ha->pdev, 0x000c,
  1481. "Failed to reserver selected regions.\n");
  1482. goto iospace_error_exit;
  1483. }
  1484. /* Use MMIO operations for all accesses. */
  1485. if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
  1486. ql_log_pci(ql_log_fatal, ha->pdev, 0x000d,
  1487. "Region #0 not an MMIO resource, aborting.\n");
  1488. goto iospace_error_exit;
  1489. }
  1490. len = pci_resource_len(ha->pdev, 0);
  1491. ha->nx_pcibase =
  1492. (unsigned long)ioremap(pci_resource_start(ha->pdev, 0), len);
  1493. if (!ha->nx_pcibase) {
  1494. ql_log_pci(ql_log_fatal, ha->pdev, 0x000e,
  1495. "Cannot remap pcibase MMIO, aborting.\n");
  1496. pci_release_regions(ha->pdev);
  1497. goto iospace_error_exit;
  1498. }
  1499. /* Mapping of IO base pointer */
  1500. ha->iobase = (device_reg_t __iomem *)((uint8_t *)ha->nx_pcibase +
  1501. 0xbc000 + (ha->pdev->devfn << 11));
  1502. if (!ql2xdbwr) {
  1503. ha->nxdb_wr_ptr =
  1504. (unsigned long)ioremap((pci_resource_start(ha->pdev, 4) +
  1505. (ha->pdev->devfn << 12)), 4);
  1506. if (!ha->nxdb_wr_ptr) {
  1507. ql_log_pci(ql_log_fatal, ha->pdev, 0x000f,
  1508. "Cannot remap MMIO, aborting.\n");
  1509. pci_release_regions(ha->pdev);
  1510. goto iospace_error_exit;
  1511. }
  1512. /* Mapping of IO base pointer,
  1513. * door bell read and write pointer
  1514. */
  1515. ha->nxdb_rd_ptr = (uint8_t *) ha->nx_pcibase + (512 * 1024) +
  1516. (ha->pdev->devfn * 8);
  1517. } else {
  1518. ha->nxdb_wr_ptr = (ha->pdev->devfn == 6 ?
  1519. QLA82XX_CAMRAM_DB1 :
  1520. QLA82XX_CAMRAM_DB2);
  1521. }
  1522. ha->max_req_queues = ha->max_rsp_queues = 1;
  1523. ha->msix_count = ha->max_rsp_queues + 1;
  1524. ql_dbg_pci(ql_dbg_multiq, ha->pdev, 0xc006,
  1525. "nx_pci_base=%p iobase=%p "
  1526. "max_req_queues=%d msix_count=%d.\n",
  1527. (void *)ha->nx_pcibase, ha->iobase,
  1528. ha->max_req_queues, ha->msix_count);
  1529. ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0010,
  1530. "nx_pci_base=%p iobase=%p "
  1531. "max_req_queues=%d msix_count=%d.\n",
  1532. (void *)ha->nx_pcibase, ha->iobase,
  1533. ha->max_req_queues, ha->msix_count);
  1534. return 0;
  1535. iospace_error_exit:
  1536. return -ENOMEM;
  1537. }
  1538. /* GS related functions */
  1539. /* Initialization related functions */
  1540. /**
  1541. * qla82xx_pci_config() - Setup ISP82xx PCI configuration registers.
  1542. * @ha: HA context
  1543. *
  1544. * Returns 0 on success.
  1545. */
  1546. int
  1547. qla82xx_pci_config(scsi_qla_host_t *vha)
  1548. {
  1549. struct qla_hw_data *ha = vha->hw;
  1550. int ret;
  1551. pci_set_master(ha->pdev);
  1552. ret = pci_set_mwi(ha->pdev);
  1553. ha->chip_revision = ha->pdev->revision;
  1554. ql_dbg(ql_dbg_init, vha, 0x0043,
  1555. "Chip revision:%d.\n",
  1556. ha->chip_revision);
  1557. return 0;
  1558. }
  1559. /**
  1560. * qla82xx_reset_chip() - Setup ISP82xx PCI configuration registers.
  1561. * @ha: HA context
  1562. *
  1563. * Returns 0 on success.
  1564. */
  1565. void
  1566. qla82xx_reset_chip(scsi_qla_host_t *vha)
  1567. {
  1568. struct qla_hw_data *ha = vha->hw;
  1569. ha->isp_ops->disable_intrs(ha);
  1570. }
  1571. void qla82xx_config_rings(struct scsi_qla_host *vha)
  1572. {
  1573. struct qla_hw_data *ha = vha->hw;
  1574. struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
  1575. struct init_cb_81xx *icb;
  1576. struct req_que *req = ha->req_q_map[0];
  1577. struct rsp_que *rsp = ha->rsp_q_map[0];
  1578. /* Setup ring parameters in initialization control block. */
  1579. icb = (struct init_cb_81xx *)ha->init_cb;
  1580. icb->request_q_outpointer = __constant_cpu_to_le16(0);
  1581. icb->response_q_inpointer = __constant_cpu_to_le16(0);
  1582. icb->request_q_length = cpu_to_le16(req->length);
  1583. icb->response_q_length = cpu_to_le16(rsp->length);
  1584. icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
  1585. icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
  1586. icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
  1587. icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
  1588. WRT_REG_DWORD((unsigned long __iomem *)&reg->req_q_out[0], 0);
  1589. WRT_REG_DWORD((unsigned long __iomem *)&reg->rsp_q_in[0], 0);
  1590. WRT_REG_DWORD((unsigned long __iomem *)&reg->rsp_q_out[0], 0);
  1591. }
  1592. void qla82xx_reset_adapter(struct scsi_qla_host *vha)
  1593. {
  1594. struct qla_hw_data *ha = vha->hw;
  1595. vha->flags.online = 0;
  1596. qla2x00_try_to_stop_firmware(vha);
  1597. ha->isp_ops->disable_intrs(ha);
  1598. }
  1599. static int
  1600. qla82xx_fw_load_from_blob(struct qla_hw_data *ha)
  1601. {
  1602. u64 *ptr64;
  1603. u32 i, flashaddr, size;
  1604. __le64 data;
  1605. size = (IMAGE_START - BOOTLD_START) / 8;
  1606. ptr64 = (u64 *)qla82xx_get_bootld_offset(ha);
  1607. flashaddr = BOOTLD_START;
  1608. for (i = 0; i < size; i++) {
  1609. data = cpu_to_le64(ptr64[i]);
  1610. if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
  1611. return -EIO;
  1612. flashaddr += 8;
  1613. }
  1614. flashaddr = FLASH_ADDR_START;
  1615. size = (__force u32)qla82xx_get_fw_size(ha) / 8;
  1616. ptr64 = (u64 *)qla82xx_get_fw_offs(ha);
  1617. for (i = 0; i < size; i++) {
  1618. data = cpu_to_le64(ptr64[i]);
  1619. if (qla82xx_pci_mem_write_2M(ha, flashaddr, &data, 8))
  1620. return -EIO;
  1621. flashaddr += 8;
  1622. }
  1623. udelay(100);
  1624. /* Write a magic value to CAMRAM register
  1625. * at a specified offset to indicate
  1626. * that all data is written and
  1627. * ready for firmware to initialize.
  1628. */
  1629. qla82xx_wr_32(ha, QLA82XX_CAM_RAM(0x1fc), QLA82XX_BDINFO_MAGIC);
  1630. read_lock(&ha->hw_lock);
  1631. qla82xx_wr_32(ha, QLA82XX_CRB_PEG_NET_0 + 0x18, 0x1020);
  1632. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, 0x80001e);
  1633. read_unlock(&ha->hw_lock);
  1634. return 0;
  1635. }
  1636. static int
  1637. qla82xx_set_product_offset(struct qla_hw_data *ha)
  1638. {
  1639. struct qla82xx_uri_table_desc *ptab_desc = NULL;
  1640. const uint8_t *unirom = ha->hablob->fw->data;
  1641. uint32_t i;
  1642. __le32 entries;
  1643. __le32 flags, file_chiprev, offset;
  1644. uint8_t chiprev = ha->chip_revision;
  1645. /* Hardcoding mn_present flag for P3P */
  1646. int mn_present = 0;
  1647. uint32_t flagbit;
  1648. ptab_desc = qla82xx_get_table_desc(unirom,
  1649. QLA82XX_URI_DIR_SECT_PRODUCT_TBL);
  1650. if (!ptab_desc)
  1651. return -1;
  1652. entries = cpu_to_le32(ptab_desc->num_entries);
  1653. for (i = 0; i < entries; i++) {
  1654. offset = cpu_to_le32(ptab_desc->findex) +
  1655. (i * cpu_to_le32(ptab_desc->entry_size));
  1656. flags = cpu_to_le32(*((int *)&unirom[offset] +
  1657. QLA82XX_URI_FLAGS_OFF));
  1658. file_chiprev = cpu_to_le32(*((int *)&unirom[offset] +
  1659. QLA82XX_URI_CHIP_REV_OFF));
  1660. flagbit = mn_present ? 1 : 2;
  1661. if ((chiprev == file_chiprev) && ((1ULL << flagbit) & flags)) {
  1662. ha->file_prd_off = offset;
  1663. return 0;
  1664. }
  1665. }
  1666. return -1;
  1667. }
  1668. int
  1669. qla82xx_validate_firmware_blob(scsi_qla_host_t *vha, uint8_t fw_type)
  1670. {
  1671. __le32 val;
  1672. uint32_t min_size;
  1673. struct qla_hw_data *ha = vha->hw;
  1674. const struct firmware *fw = ha->hablob->fw;
  1675. ha->fw_type = fw_type;
  1676. if (fw_type == QLA82XX_UNIFIED_ROMIMAGE) {
  1677. if (qla82xx_set_product_offset(ha))
  1678. return -EINVAL;
  1679. min_size = QLA82XX_URI_FW_MIN_SIZE;
  1680. } else {
  1681. val = cpu_to_le32(*(u32 *)&fw->data[QLA82XX_FW_MAGIC_OFFSET]);
  1682. if ((__force u32)val != QLA82XX_BDINFO_MAGIC)
  1683. return -EINVAL;
  1684. min_size = QLA82XX_FW_MIN_SIZE;
  1685. }
  1686. if (fw->size < min_size)
  1687. return -EINVAL;
  1688. return 0;
  1689. }
  1690. static int
  1691. qla82xx_check_cmdpeg_state(struct qla_hw_data *ha)
  1692. {
  1693. u32 val = 0;
  1694. int retries = 60;
  1695. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  1696. do {
  1697. read_lock(&ha->hw_lock);
  1698. val = qla82xx_rd_32(ha, CRB_CMDPEG_STATE);
  1699. read_unlock(&ha->hw_lock);
  1700. switch (val) {
  1701. case PHAN_INITIALIZE_COMPLETE:
  1702. case PHAN_INITIALIZE_ACK:
  1703. return QLA_SUCCESS;
  1704. case PHAN_INITIALIZE_FAILED:
  1705. break;
  1706. default:
  1707. break;
  1708. }
  1709. ql_log(ql_log_info, vha, 0x00a8,
  1710. "CRB_CMDPEG_STATE: 0x%x and retries:0x%x.\n",
  1711. val, retries);
  1712. msleep(500);
  1713. } while (--retries);
  1714. ql_log(ql_log_fatal, vha, 0x00a9,
  1715. "Cmd Peg initialization failed: 0x%x.\n", val);
  1716. val = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_PEGTUNE_DONE);
  1717. read_lock(&ha->hw_lock);
  1718. qla82xx_wr_32(ha, CRB_CMDPEG_STATE, PHAN_INITIALIZE_FAILED);
  1719. read_unlock(&ha->hw_lock);
  1720. return QLA_FUNCTION_FAILED;
  1721. }
  1722. static int
  1723. qla82xx_check_rcvpeg_state(struct qla_hw_data *ha)
  1724. {
  1725. u32 val = 0;
  1726. int retries = 60;
  1727. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  1728. do {
  1729. read_lock(&ha->hw_lock);
  1730. val = qla82xx_rd_32(ha, CRB_RCVPEG_STATE);
  1731. read_unlock(&ha->hw_lock);
  1732. switch (val) {
  1733. case PHAN_INITIALIZE_COMPLETE:
  1734. case PHAN_INITIALIZE_ACK:
  1735. return QLA_SUCCESS;
  1736. case PHAN_INITIALIZE_FAILED:
  1737. break;
  1738. default:
  1739. break;
  1740. }
  1741. ql_log(ql_log_info, vha, 0x00ab,
  1742. "CRB_RCVPEG_STATE: 0x%x and retries: 0x%x.\n",
  1743. val, retries);
  1744. msleep(500);
  1745. } while (--retries);
  1746. ql_log(ql_log_fatal, vha, 0x00ac,
  1747. "Rcv Peg initializatin failed: 0x%x.\n", val);
  1748. read_lock(&ha->hw_lock);
  1749. qla82xx_wr_32(ha, CRB_RCVPEG_STATE, PHAN_INITIALIZE_FAILED);
  1750. read_unlock(&ha->hw_lock);
  1751. return QLA_FUNCTION_FAILED;
  1752. }
  1753. /* ISR related functions */
  1754. uint32_t qla82xx_isr_int_target_mask_enable[8] = {
  1755. ISR_INT_TARGET_MASK, ISR_INT_TARGET_MASK_F1,
  1756. ISR_INT_TARGET_MASK_F2, ISR_INT_TARGET_MASK_F3,
  1757. ISR_INT_TARGET_MASK_F4, ISR_INT_TARGET_MASK_F5,
  1758. ISR_INT_TARGET_MASK_F7, ISR_INT_TARGET_MASK_F7
  1759. };
  1760. uint32_t qla82xx_isr_int_target_status[8] = {
  1761. ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
  1762. ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
  1763. ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
  1764. ISR_INT_TARGET_STATUS_F7, ISR_INT_TARGET_STATUS_F7
  1765. };
  1766. static struct qla82xx_legacy_intr_set legacy_intr[] = \
  1767. QLA82XX_LEGACY_INTR_CONFIG;
  1768. /*
  1769. * qla82xx_mbx_completion() - Process mailbox command completions.
  1770. * @ha: SCSI driver HA context
  1771. * @mb0: Mailbox0 register
  1772. */
  1773. static void
  1774. qla82xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
  1775. {
  1776. uint16_t cnt;
  1777. uint16_t __iomem *wptr;
  1778. struct qla_hw_data *ha = vha->hw;
  1779. struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
  1780. wptr = (uint16_t __iomem *)&reg->mailbox_out[1];
  1781. /* Load return mailbox registers. */
  1782. ha->flags.mbox_int = 1;
  1783. ha->mailbox_out[0] = mb0;
  1784. for (cnt = 1; cnt < ha->mbx_count; cnt++) {
  1785. ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
  1786. wptr++;
  1787. }
  1788. if (ha->mcp) {
  1789. ql_dbg(ql_dbg_async, vha, 0x5052,
  1790. "Got mailbox completion. cmd=%x.\n", ha->mcp->mb[0]);
  1791. } else {
  1792. ql_dbg(ql_dbg_async, vha, 0x5053,
  1793. "MBX pointer ERROR.\n");
  1794. }
  1795. }
  1796. /*
  1797. * qla82xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
  1798. * @irq:
  1799. * @dev_id: SCSI driver HA context
  1800. * @regs:
  1801. *
  1802. * Called by system whenever the host adapter generates an interrupt.
  1803. *
  1804. * Returns handled flag.
  1805. */
  1806. irqreturn_t
  1807. qla82xx_intr_handler(int irq, void *dev_id)
  1808. {
  1809. scsi_qla_host_t *vha;
  1810. struct qla_hw_data *ha;
  1811. struct rsp_que *rsp;
  1812. struct device_reg_82xx __iomem *reg;
  1813. int status = 0, status1 = 0;
  1814. unsigned long flags;
  1815. unsigned long iter;
  1816. uint32_t stat = 0;
  1817. uint16_t mb[4];
  1818. rsp = (struct rsp_que *) dev_id;
  1819. if (!rsp) {
  1820. printk(KERN_INFO
  1821. "%s(): NULL response queue pointer.\n", __func__);
  1822. return IRQ_NONE;
  1823. }
  1824. ha = rsp->hw;
  1825. if (!ha->flags.msi_enabled) {
  1826. status = qla82xx_rd_32(ha, ISR_INT_VECTOR);
  1827. if (!(status & ha->nx_legacy_intr.int_vec_bit))
  1828. return IRQ_NONE;
  1829. status1 = qla82xx_rd_32(ha, ISR_INT_STATE_REG);
  1830. if (!ISR_IS_LEGACY_INTR_TRIGGERED(status1))
  1831. return IRQ_NONE;
  1832. }
  1833. /* clear the interrupt */
  1834. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
  1835. /* read twice to ensure write is flushed */
  1836. qla82xx_rd_32(ha, ISR_INT_VECTOR);
  1837. qla82xx_rd_32(ha, ISR_INT_VECTOR);
  1838. reg = &ha->iobase->isp82;
  1839. spin_lock_irqsave(&ha->hardware_lock, flags);
  1840. vha = pci_get_drvdata(ha->pdev);
  1841. for (iter = 1; iter--; ) {
  1842. if (RD_REG_DWORD(&reg->host_int)) {
  1843. stat = RD_REG_DWORD(&reg->host_status);
  1844. switch (stat & 0xff) {
  1845. case 0x1:
  1846. case 0x2:
  1847. case 0x10:
  1848. case 0x11:
  1849. qla82xx_mbx_completion(vha, MSW(stat));
  1850. status |= MBX_INTERRUPT;
  1851. break;
  1852. case 0x12:
  1853. mb[0] = MSW(stat);
  1854. mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
  1855. mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
  1856. mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
  1857. qla2x00_async_event(vha, rsp, mb);
  1858. break;
  1859. case 0x13:
  1860. qla24xx_process_response_queue(vha, rsp);
  1861. break;
  1862. default:
  1863. ql_dbg(ql_dbg_async, vha, 0x5054,
  1864. "Unrecognized interrupt type (%d).\n",
  1865. stat & 0xff);
  1866. break;
  1867. }
  1868. }
  1869. WRT_REG_DWORD(&reg->host_int, 0);
  1870. }
  1871. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1872. if (!ha->flags.msi_enabled)
  1873. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
  1874. #ifdef QL_DEBUG_LEVEL_17
  1875. if (!irq && ha->flags.eeh_busy)
  1876. ql_log(ql_log_warn, vha, 0x503d,
  1877. "isr:status %x, cmd_flags %lx, mbox_int %x, stat %x.\n",
  1878. status, ha->mbx_cmd_flags, ha->flags.mbox_int, stat);
  1879. #endif
  1880. if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
  1881. (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
  1882. set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
  1883. complete(&ha->mbx_intr_comp);
  1884. }
  1885. return IRQ_HANDLED;
  1886. }
  1887. irqreturn_t
  1888. qla82xx_msix_default(int irq, void *dev_id)
  1889. {
  1890. scsi_qla_host_t *vha;
  1891. struct qla_hw_data *ha;
  1892. struct rsp_que *rsp;
  1893. struct device_reg_82xx __iomem *reg;
  1894. int status = 0;
  1895. unsigned long flags;
  1896. uint32_t stat = 0;
  1897. uint16_t mb[4];
  1898. rsp = (struct rsp_que *) dev_id;
  1899. if (!rsp) {
  1900. printk(KERN_INFO
  1901. "%s(): NULL response queue pointer.\n", __func__);
  1902. return IRQ_NONE;
  1903. }
  1904. ha = rsp->hw;
  1905. reg = &ha->iobase->isp82;
  1906. spin_lock_irqsave(&ha->hardware_lock, flags);
  1907. vha = pci_get_drvdata(ha->pdev);
  1908. do {
  1909. if (RD_REG_DWORD(&reg->host_int)) {
  1910. stat = RD_REG_DWORD(&reg->host_status);
  1911. switch (stat & 0xff) {
  1912. case 0x1:
  1913. case 0x2:
  1914. case 0x10:
  1915. case 0x11:
  1916. qla82xx_mbx_completion(vha, MSW(stat));
  1917. status |= MBX_INTERRUPT;
  1918. break;
  1919. case 0x12:
  1920. mb[0] = MSW(stat);
  1921. mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
  1922. mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
  1923. mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
  1924. qla2x00_async_event(vha, rsp, mb);
  1925. break;
  1926. case 0x13:
  1927. qla24xx_process_response_queue(vha, rsp);
  1928. break;
  1929. default:
  1930. ql_dbg(ql_dbg_async, vha, 0x5041,
  1931. "Unrecognized interrupt type (%d).\n",
  1932. stat & 0xff);
  1933. break;
  1934. }
  1935. }
  1936. WRT_REG_DWORD(&reg->host_int, 0);
  1937. } while (0);
  1938. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1939. #ifdef QL_DEBUG_LEVEL_17
  1940. if (!irq && ha->flags.eeh_busy)
  1941. ql_log(ql_log_warn, vha, 0x5044,
  1942. "isr:status %x, cmd_flags %lx, mbox_int %x, stat %x.\n",
  1943. status, ha->mbx_cmd_flags, ha->flags.mbox_int, stat);
  1944. #endif
  1945. if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
  1946. (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
  1947. set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
  1948. complete(&ha->mbx_intr_comp);
  1949. }
  1950. return IRQ_HANDLED;
  1951. }
  1952. irqreturn_t
  1953. qla82xx_msix_rsp_q(int irq, void *dev_id)
  1954. {
  1955. scsi_qla_host_t *vha;
  1956. struct qla_hw_data *ha;
  1957. struct rsp_que *rsp;
  1958. struct device_reg_82xx __iomem *reg;
  1959. unsigned long flags;
  1960. rsp = (struct rsp_que *) dev_id;
  1961. if (!rsp) {
  1962. printk(KERN_INFO
  1963. "%s(): NULL response queue pointer.\n", __func__);
  1964. return IRQ_NONE;
  1965. }
  1966. ha = rsp->hw;
  1967. reg = &ha->iobase->isp82;
  1968. spin_lock_irqsave(&ha->hardware_lock, flags);
  1969. vha = pci_get_drvdata(ha->pdev);
  1970. qla24xx_process_response_queue(vha, rsp);
  1971. WRT_REG_DWORD(&reg->host_int, 0);
  1972. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1973. return IRQ_HANDLED;
  1974. }
  1975. void
  1976. qla82xx_poll(int irq, void *dev_id)
  1977. {
  1978. scsi_qla_host_t *vha;
  1979. struct qla_hw_data *ha;
  1980. struct rsp_que *rsp;
  1981. struct device_reg_82xx __iomem *reg;
  1982. int status = 0;
  1983. uint32_t stat;
  1984. uint16_t mb[4];
  1985. unsigned long flags;
  1986. rsp = (struct rsp_que *) dev_id;
  1987. if (!rsp) {
  1988. printk(KERN_INFO
  1989. "%s(): NULL response queue pointer.\n", __func__);
  1990. return;
  1991. }
  1992. ha = rsp->hw;
  1993. reg = &ha->iobase->isp82;
  1994. spin_lock_irqsave(&ha->hardware_lock, flags);
  1995. vha = pci_get_drvdata(ha->pdev);
  1996. if (RD_REG_DWORD(&reg->host_int)) {
  1997. stat = RD_REG_DWORD(&reg->host_status);
  1998. switch (stat & 0xff) {
  1999. case 0x1:
  2000. case 0x2:
  2001. case 0x10:
  2002. case 0x11:
  2003. qla82xx_mbx_completion(vha, MSW(stat));
  2004. status |= MBX_INTERRUPT;
  2005. break;
  2006. case 0x12:
  2007. mb[0] = MSW(stat);
  2008. mb[1] = RD_REG_WORD(&reg->mailbox_out[1]);
  2009. mb[2] = RD_REG_WORD(&reg->mailbox_out[2]);
  2010. mb[3] = RD_REG_WORD(&reg->mailbox_out[3]);
  2011. qla2x00_async_event(vha, rsp, mb);
  2012. break;
  2013. case 0x13:
  2014. qla24xx_process_response_queue(vha, rsp);
  2015. break;
  2016. default:
  2017. ql_dbg(ql_dbg_p3p, vha, 0xb013,
  2018. "Unrecognized interrupt type (%d).\n",
  2019. stat * 0xff);
  2020. break;
  2021. }
  2022. }
  2023. WRT_REG_DWORD(&reg->host_int, 0);
  2024. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2025. }
  2026. void
  2027. qla82xx_enable_intrs(struct qla_hw_data *ha)
  2028. {
  2029. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2030. qla82xx_mbx_intr_enable(vha);
  2031. spin_lock_irq(&ha->hardware_lock);
  2032. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
  2033. spin_unlock_irq(&ha->hardware_lock);
  2034. ha->interrupts_on = 1;
  2035. }
  2036. void
  2037. qla82xx_disable_intrs(struct qla_hw_data *ha)
  2038. {
  2039. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2040. qla82xx_mbx_intr_disable(vha);
  2041. spin_lock_irq(&ha->hardware_lock);
  2042. qla82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0x0400);
  2043. spin_unlock_irq(&ha->hardware_lock);
  2044. ha->interrupts_on = 0;
  2045. }
  2046. void qla82xx_init_flags(struct qla_hw_data *ha)
  2047. {
  2048. struct qla82xx_legacy_intr_set *nx_legacy_intr;
  2049. /* ISP 8021 initializations */
  2050. rwlock_init(&ha->hw_lock);
  2051. ha->qdr_sn_window = -1;
  2052. ha->ddr_mn_window = -1;
  2053. ha->curr_window = 255;
  2054. ha->portnum = PCI_FUNC(ha->pdev->devfn);
  2055. nx_legacy_intr = &legacy_intr[ha->portnum];
  2056. ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  2057. ha->nx_legacy_intr.tgt_status_reg = nx_legacy_intr->tgt_status_reg;
  2058. ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  2059. ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  2060. }
  2061. inline void
  2062. qla82xx_set_drv_active(scsi_qla_host_t *vha)
  2063. {
  2064. uint32_t drv_active;
  2065. struct qla_hw_data *ha = vha->hw;
  2066. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  2067. /* If reset value is all FF's, initialize DRV_ACTIVE */
  2068. if (drv_active == 0xffffffff) {
  2069. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE,
  2070. QLA82XX_DRV_NOT_ACTIVE);
  2071. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  2072. }
  2073. drv_active |= (QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
  2074. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
  2075. }
  2076. inline void
  2077. qla82xx_clear_drv_active(struct qla_hw_data *ha)
  2078. {
  2079. uint32_t drv_active;
  2080. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  2081. drv_active &= ~(QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
  2082. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_ACTIVE, drv_active);
  2083. }
  2084. static inline int
  2085. qla82xx_need_reset(struct qla_hw_data *ha)
  2086. {
  2087. uint32_t drv_state;
  2088. int rval;
  2089. if (ha->flags.isp82xx_reset_owner)
  2090. return 1;
  2091. else {
  2092. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2093. rval = drv_state & (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
  2094. return rval;
  2095. }
  2096. }
  2097. static inline void
  2098. qla82xx_set_rst_ready(struct qla_hw_data *ha)
  2099. {
  2100. uint32_t drv_state;
  2101. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2102. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2103. /* If reset value is all FF's, initialize DRV_STATE */
  2104. if (drv_state == 0xffffffff) {
  2105. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, QLA82XX_DRVST_NOT_RDY);
  2106. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2107. }
  2108. drv_state |= (QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
  2109. ql_dbg(ql_dbg_init, vha, 0x00bb,
  2110. "drv_state = 0x%08x.\n", drv_state);
  2111. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
  2112. }
  2113. static inline void
  2114. qla82xx_clear_rst_ready(struct qla_hw_data *ha)
  2115. {
  2116. uint32_t drv_state;
  2117. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2118. drv_state &= ~(QLA82XX_DRVST_RST_RDY << (ha->portnum * 4));
  2119. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, drv_state);
  2120. }
  2121. static inline void
  2122. qla82xx_set_qsnt_ready(struct qla_hw_data *ha)
  2123. {
  2124. uint32_t qsnt_state;
  2125. qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2126. qsnt_state |= (QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
  2127. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
  2128. }
  2129. void
  2130. qla82xx_clear_qsnt_ready(scsi_qla_host_t *vha)
  2131. {
  2132. struct qla_hw_data *ha = vha->hw;
  2133. uint32_t qsnt_state;
  2134. qsnt_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  2135. qsnt_state &= ~(QLA82XX_DRVST_QSNT_RDY << (ha->portnum * 4));
  2136. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_STATE, qsnt_state);
  2137. }
  2138. static int
  2139. qla82xx_load_fw(scsi_qla_host_t *vha)
  2140. {
  2141. int rst;
  2142. struct fw_blob *blob;
  2143. struct qla_hw_data *ha = vha->hw;
  2144. if (qla82xx_pinit_from_rom(vha) != QLA_SUCCESS) {
  2145. ql_log(ql_log_fatal, vha, 0x009f,
  2146. "Error during CRB initialization.\n");
  2147. return QLA_FUNCTION_FAILED;
  2148. }
  2149. udelay(500);
  2150. /* Bring QM and CAMRAM out of reset */
  2151. rst = qla82xx_rd_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET);
  2152. rst &= ~((1 << 28) | (1 << 24));
  2153. qla82xx_wr_32(ha, QLA82XX_ROMUSB_GLB_SW_RESET, rst);
  2154. /*
  2155. * FW Load priority:
  2156. * 1) Operational firmware residing in flash.
  2157. * 2) Firmware via request-firmware interface (.bin file).
  2158. */
  2159. if (ql2xfwloadbin == 2)
  2160. goto try_blob_fw;
  2161. ql_log(ql_log_info, vha, 0x00a0,
  2162. "Attempting to load firmware from flash.\n");
  2163. if (qla82xx_fw_load_from_flash(ha) == QLA_SUCCESS) {
  2164. ql_log(ql_log_info, vha, 0x00a1,
  2165. "Firmware loaded successully from flash.\n");
  2166. return QLA_SUCCESS;
  2167. } else {
  2168. ql_log(ql_log_warn, vha, 0x0108,
  2169. "Firmware load from flash failed.\n");
  2170. }
  2171. try_blob_fw:
  2172. ql_log(ql_log_info, vha, 0x00a2,
  2173. "Attempting to load firmware from blob.\n");
  2174. /* Load firmware blob. */
  2175. blob = ha->hablob = qla2x00_request_firmware(vha);
  2176. if (!blob) {
  2177. ql_log(ql_log_fatal, vha, 0x00a3,
  2178. "Firmware image not preset.\n");
  2179. goto fw_load_failed;
  2180. }
  2181. /* Validating firmware blob */
  2182. if (qla82xx_validate_firmware_blob(vha,
  2183. QLA82XX_FLASH_ROMIMAGE)) {
  2184. /* Fallback to URI format */
  2185. if (qla82xx_validate_firmware_blob(vha,
  2186. QLA82XX_UNIFIED_ROMIMAGE)) {
  2187. ql_log(ql_log_fatal, vha, 0x00a4,
  2188. "No valid firmware image found.\n");
  2189. return QLA_FUNCTION_FAILED;
  2190. }
  2191. }
  2192. if (qla82xx_fw_load_from_blob(ha) == QLA_SUCCESS) {
  2193. ql_log(ql_log_info, vha, 0x00a5,
  2194. "Firmware loaded successfully from binary blob.\n");
  2195. return QLA_SUCCESS;
  2196. } else {
  2197. ql_log(ql_log_fatal, vha, 0x00a6,
  2198. "Firmware load failed for binary blob.\n");
  2199. blob->fw = NULL;
  2200. blob = NULL;
  2201. goto fw_load_failed;
  2202. }
  2203. return QLA_SUCCESS;
  2204. fw_load_failed:
  2205. return QLA_FUNCTION_FAILED;
  2206. }
  2207. int
  2208. qla82xx_start_firmware(scsi_qla_host_t *vha)
  2209. {
  2210. int pcie_cap;
  2211. uint16_t lnk;
  2212. struct qla_hw_data *ha = vha->hw;
  2213. /* scrub dma mask expansion register */
  2214. qla82xx_wr_32(ha, CRB_DMA_SHIFT, QLA82XX_DMA_SHIFT_VALUE);
  2215. /* Put both the PEG CMD and RCV PEG to default state
  2216. * of 0 before resetting the hardware
  2217. */
  2218. qla82xx_wr_32(ha, CRB_CMDPEG_STATE, 0);
  2219. qla82xx_wr_32(ha, CRB_RCVPEG_STATE, 0);
  2220. /* Overwrite stale initialization register values */
  2221. qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS1, 0);
  2222. qla82xx_wr_32(ha, QLA82XX_PEG_HALT_STATUS2, 0);
  2223. if (qla82xx_load_fw(vha) != QLA_SUCCESS) {
  2224. ql_log(ql_log_fatal, vha, 0x00a7,
  2225. "Error trying to start fw.\n");
  2226. return QLA_FUNCTION_FAILED;
  2227. }
  2228. /* Handshake with the card before we register the devices. */
  2229. if (qla82xx_check_cmdpeg_state(ha) != QLA_SUCCESS) {
  2230. ql_log(ql_log_fatal, vha, 0x00aa,
  2231. "Error during card handshake.\n");
  2232. return QLA_FUNCTION_FAILED;
  2233. }
  2234. /* Negotiated Link width */
  2235. pcie_cap = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
  2236. pci_read_config_word(ha->pdev, pcie_cap + PCI_EXP_LNKSTA, &lnk);
  2237. ha->link_width = (lnk >> 4) & 0x3f;
  2238. /* Synchronize with Receive peg */
  2239. return qla82xx_check_rcvpeg_state(ha);
  2240. }
  2241. static inline int
  2242. qla2xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
  2243. uint16_t tot_dsds)
  2244. {
  2245. uint32_t *cur_dsd = NULL;
  2246. scsi_qla_host_t *vha;
  2247. struct qla_hw_data *ha;
  2248. struct scsi_cmnd *cmd;
  2249. struct scatterlist *cur_seg;
  2250. uint32_t *dsd_seg;
  2251. void *next_dsd;
  2252. uint8_t avail_dsds;
  2253. uint8_t first_iocb = 1;
  2254. uint32_t dsd_list_len;
  2255. struct dsd_dma *dsd_ptr;
  2256. struct ct6_dsd *ctx;
  2257. cmd = sp->cmd;
  2258. /* Update entry type to indicate Command Type 3 IOCB */
  2259. *((uint32_t *)(&cmd_pkt->entry_type)) =
  2260. __constant_cpu_to_le32(COMMAND_TYPE_6);
  2261. /* No data transfer */
  2262. if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  2263. cmd_pkt->byte_count = __constant_cpu_to_le32(0);
  2264. return 0;
  2265. }
  2266. vha = sp->fcport->vha;
  2267. ha = vha->hw;
  2268. /* Set transfer direction */
  2269. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  2270. cmd_pkt->control_flags =
  2271. __constant_cpu_to_le16(CF_WRITE_DATA);
  2272. ha->qla_stats.output_bytes += scsi_bufflen(cmd);
  2273. } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  2274. cmd_pkt->control_flags =
  2275. __constant_cpu_to_le16(CF_READ_DATA);
  2276. ha->qla_stats.input_bytes += scsi_bufflen(cmd);
  2277. }
  2278. cur_seg = scsi_sglist(cmd);
  2279. ctx = sp->ctx;
  2280. while (tot_dsds) {
  2281. avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
  2282. QLA_DSDS_PER_IOCB : tot_dsds;
  2283. tot_dsds -= avail_dsds;
  2284. dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
  2285. dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
  2286. struct dsd_dma, list);
  2287. next_dsd = dsd_ptr->dsd_addr;
  2288. list_del(&dsd_ptr->list);
  2289. ha->gbl_dsd_avail--;
  2290. list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
  2291. ctx->dsd_use_cnt++;
  2292. ha->gbl_dsd_inuse++;
  2293. if (first_iocb) {
  2294. first_iocb = 0;
  2295. dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
  2296. *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  2297. *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  2298. cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
  2299. } else {
  2300. *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
  2301. *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
  2302. *cur_dsd++ = cpu_to_le32(dsd_list_len);
  2303. }
  2304. cur_dsd = (uint32_t *)next_dsd;
  2305. while (avail_dsds) {
  2306. dma_addr_t sle_dma;
  2307. sle_dma = sg_dma_address(cur_seg);
  2308. *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
  2309. *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
  2310. *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
  2311. cur_seg = sg_next(cur_seg);
  2312. avail_dsds--;
  2313. }
  2314. }
  2315. /* Null termination */
  2316. *cur_dsd++ = 0;
  2317. *cur_dsd++ = 0;
  2318. *cur_dsd++ = 0;
  2319. cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
  2320. return 0;
  2321. }
  2322. /*
  2323. * qla82xx_calc_dsd_lists() - Determine number of DSD list required
  2324. * for Command Type 6.
  2325. *
  2326. * @dsds: number of data segment decriptors needed
  2327. *
  2328. * Returns the number of dsd list needed to store @dsds.
  2329. */
  2330. inline uint16_t
  2331. qla82xx_calc_dsd_lists(uint16_t dsds)
  2332. {
  2333. uint16_t dsd_lists = 0;
  2334. dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
  2335. if (dsds % QLA_DSDS_PER_IOCB)
  2336. dsd_lists++;
  2337. return dsd_lists;
  2338. }
  2339. /*
  2340. * qla82xx_start_scsi() - Send a SCSI command to the ISP
  2341. * @sp: command to send to the ISP
  2342. *
  2343. * Returns non-zero if a failure occurred, else zero.
  2344. */
  2345. int
  2346. qla82xx_start_scsi(srb_t *sp)
  2347. {
  2348. int ret, nseg;
  2349. unsigned long flags;
  2350. struct scsi_cmnd *cmd;
  2351. uint32_t *clr_ptr;
  2352. uint32_t index;
  2353. uint32_t handle;
  2354. uint16_t cnt;
  2355. uint16_t req_cnt;
  2356. uint16_t tot_dsds;
  2357. struct device_reg_82xx __iomem *reg;
  2358. uint32_t dbval;
  2359. uint32_t *fcp_dl;
  2360. uint8_t additional_cdb_len;
  2361. struct ct6_dsd *ctx;
  2362. struct scsi_qla_host *vha = sp->fcport->vha;
  2363. struct qla_hw_data *ha = vha->hw;
  2364. struct req_que *req = NULL;
  2365. struct rsp_que *rsp = NULL;
  2366. char tag[2];
  2367. /* Setup device pointers. */
  2368. ret = 0;
  2369. reg = &ha->iobase->isp82;
  2370. cmd = sp->cmd;
  2371. req = vha->req;
  2372. rsp = ha->rsp_q_map[0];
  2373. /* So we know we haven't pci_map'ed anything yet */
  2374. tot_dsds = 0;
  2375. dbval = 0x04 | (ha->portnum << 5);
  2376. /* Send marker if required */
  2377. if (vha->marker_needed != 0) {
  2378. if (qla2x00_marker(vha, req,
  2379. rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
  2380. ql_log(ql_log_warn, vha, 0x300c,
  2381. "qla2x00_marker failed for cmd=%p.\n", cmd);
  2382. return QLA_FUNCTION_FAILED;
  2383. }
  2384. vha->marker_needed = 0;
  2385. }
  2386. /* Acquire ring specific lock */
  2387. spin_lock_irqsave(&ha->hardware_lock, flags);
  2388. /* Check for room in outstanding command list. */
  2389. handle = req->current_outstanding_cmd;
  2390. for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
  2391. handle++;
  2392. if (handle == MAX_OUTSTANDING_COMMANDS)
  2393. handle = 1;
  2394. if (!req->outstanding_cmds[handle])
  2395. break;
  2396. }
  2397. if (index == MAX_OUTSTANDING_COMMANDS)
  2398. goto queuing_error;
  2399. /* Map the sg table so we have an accurate count of sg entries needed */
  2400. if (scsi_sg_count(cmd)) {
  2401. nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
  2402. scsi_sg_count(cmd), cmd->sc_data_direction);
  2403. if (unlikely(!nseg))
  2404. goto queuing_error;
  2405. } else
  2406. nseg = 0;
  2407. tot_dsds = nseg;
  2408. if (tot_dsds > ql2xshiftctondsd) {
  2409. struct cmd_type_6 *cmd_pkt;
  2410. uint16_t more_dsd_lists = 0;
  2411. struct dsd_dma *dsd_ptr;
  2412. uint16_t i;
  2413. more_dsd_lists = qla82xx_calc_dsd_lists(tot_dsds);
  2414. if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
  2415. ql_dbg(ql_dbg_io, vha, 0x300d,
  2416. "Num of DSD list %d is than %d for cmd=%p.\n",
  2417. more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
  2418. cmd);
  2419. goto queuing_error;
  2420. }
  2421. if (more_dsd_lists <= ha->gbl_dsd_avail)
  2422. goto sufficient_dsds;
  2423. else
  2424. more_dsd_lists -= ha->gbl_dsd_avail;
  2425. for (i = 0; i < more_dsd_lists; i++) {
  2426. dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
  2427. if (!dsd_ptr) {
  2428. ql_log(ql_log_fatal, vha, 0x300e,
  2429. "Failed to allocate memory for dsd_dma "
  2430. "for cmd=%p.\n", cmd);
  2431. goto queuing_error;
  2432. }
  2433. dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
  2434. GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
  2435. if (!dsd_ptr->dsd_addr) {
  2436. kfree(dsd_ptr);
  2437. ql_log(ql_log_fatal, vha, 0x300f,
  2438. "Failed to allocate memory for dsd_addr "
  2439. "for cmd=%p.\n", cmd);
  2440. goto queuing_error;
  2441. }
  2442. list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
  2443. ha->gbl_dsd_avail++;
  2444. }
  2445. sufficient_dsds:
  2446. req_cnt = 1;
  2447. if (req->cnt < (req_cnt + 2)) {
  2448. cnt = (uint16_t)RD_REG_DWORD_RELAXED(
  2449. &reg->req_q_out[0]);
  2450. if (req->ring_index < cnt)
  2451. req->cnt = cnt - req->ring_index;
  2452. else
  2453. req->cnt = req->length -
  2454. (req->ring_index - cnt);
  2455. }
  2456. if (req->cnt < (req_cnt + 2))
  2457. goto queuing_error;
  2458. ctx = sp->ctx = mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
  2459. if (!sp->ctx) {
  2460. ql_log(ql_log_fatal, vha, 0x3010,
  2461. "Failed to allocate ctx for cmd=%p.\n", cmd);
  2462. goto queuing_error;
  2463. }
  2464. memset(ctx, 0, sizeof(struct ct6_dsd));
  2465. ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
  2466. GFP_ATOMIC, &ctx->fcp_cmnd_dma);
  2467. if (!ctx->fcp_cmnd) {
  2468. ql_log(ql_log_fatal, vha, 0x3011,
  2469. "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
  2470. goto queuing_error_fcp_cmnd;
  2471. }
  2472. /* Initialize the DSD list and dma handle */
  2473. INIT_LIST_HEAD(&ctx->dsd_list);
  2474. ctx->dsd_use_cnt = 0;
  2475. if (cmd->cmd_len > 16) {
  2476. additional_cdb_len = cmd->cmd_len - 16;
  2477. if ((cmd->cmd_len % 4) != 0) {
  2478. /* SCSI command bigger than 16 bytes must be
  2479. * multiple of 4
  2480. */
  2481. ql_log(ql_log_warn, vha, 0x3012,
  2482. "scsi cmd len %d not multiple of 4 "
  2483. "for cmd=%p.\n", cmd->cmd_len, cmd);
  2484. goto queuing_error_fcp_cmnd;
  2485. }
  2486. ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
  2487. } else {
  2488. additional_cdb_len = 0;
  2489. ctx->fcp_cmnd_len = 12 + 16 + 4;
  2490. }
  2491. cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
  2492. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  2493. /* Zero out remaining portion of packet. */
  2494. /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
  2495. clr_ptr = (uint32_t *)cmd_pkt + 2;
  2496. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  2497. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  2498. /* Set NPORT-ID and LUN number*/
  2499. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  2500. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  2501. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  2502. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  2503. cmd_pkt->vp_index = sp->fcport->vp_idx;
  2504. /* Build IOCB segments */
  2505. if (qla2xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
  2506. goto queuing_error_fcp_cmnd;
  2507. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  2508. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
  2509. /* build FCP_CMND IU */
  2510. memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  2511. int_to_scsilun(sp->cmd->device->lun, &ctx->fcp_cmnd->lun);
  2512. ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
  2513. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  2514. ctx->fcp_cmnd->additional_cdb_len |= 1;
  2515. else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  2516. ctx->fcp_cmnd->additional_cdb_len |= 2;
  2517. /*
  2518. * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
  2519. */
  2520. if (scsi_populate_tag_msg(cmd, tag)) {
  2521. switch (tag[0]) {
  2522. case HEAD_OF_QUEUE_TAG:
  2523. ctx->fcp_cmnd->task_attribute =
  2524. TSK_HEAD_OF_QUEUE;
  2525. break;
  2526. case ORDERED_QUEUE_TAG:
  2527. ctx->fcp_cmnd->task_attribute =
  2528. TSK_ORDERED;
  2529. break;
  2530. }
  2531. }
  2532. memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
  2533. fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
  2534. additional_cdb_len);
  2535. *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
  2536. cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
  2537. cmd_pkt->fcp_cmnd_dseg_address[0] =
  2538. cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
  2539. cmd_pkt->fcp_cmnd_dseg_address[1] =
  2540. cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
  2541. sp->flags |= SRB_FCP_CMND_DMA_VALID;
  2542. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  2543. /* Set total data segment count. */
  2544. cmd_pkt->entry_count = (uint8_t)req_cnt;
  2545. /* Specify response queue number where
  2546. * completion should happen
  2547. */
  2548. cmd_pkt->entry_status = (uint8_t) rsp->id;
  2549. } else {
  2550. struct cmd_type_7 *cmd_pkt;
  2551. req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
  2552. if (req->cnt < (req_cnt + 2)) {
  2553. cnt = (uint16_t)RD_REG_DWORD_RELAXED(
  2554. &reg->req_q_out[0]);
  2555. if (req->ring_index < cnt)
  2556. req->cnt = cnt - req->ring_index;
  2557. else
  2558. req->cnt = req->length -
  2559. (req->ring_index - cnt);
  2560. }
  2561. if (req->cnt < (req_cnt + 2))
  2562. goto queuing_error;
  2563. cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
  2564. cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
  2565. /* Zero out remaining portion of packet. */
  2566. /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
  2567. clr_ptr = (uint32_t *)cmd_pkt + 2;
  2568. memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
  2569. cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
  2570. /* Set NPORT-ID and LUN number*/
  2571. cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
  2572. cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
  2573. cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
  2574. cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
  2575. cmd_pkt->vp_index = sp->fcport->vp_idx;
  2576. int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
  2577. host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
  2578. sizeof(cmd_pkt->lun));
  2579. /*
  2580. * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
  2581. */
  2582. if (scsi_populate_tag_msg(cmd, tag)) {
  2583. switch (tag[0]) {
  2584. case HEAD_OF_QUEUE_TAG:
  2585. cmd_pkt->task = TSK_HEAD_OF_QUEUE;
  2586. break;
  2587. case ORDERED_QUEUE_TAG:
  2588. cmd_pkt->task = TSK_ORDERED;
  2589. break;
  2590. }
  2591. }
  2592. /* Load SCSI command packet. */
  2593. memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
  2594. host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
  2595. cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
  2596. /* Build IOCB segments */
  2597. qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
  2598. /* Set total data segment count. */
  2599. cmd_pkt->entry_count = (uint8_t)req_cnt;
  2600. /* Specify response queue number where
  2601. * completion should happen.
  2602. */
  2603. cmd_pkt->entry_status = (uint8_t) rsp->id;
  2604. }
  2605. /* Build command packet. */
  2606. req->current_outstanding_cmd = handle;
  2607. req->outstanding_cmds[handle] = sp;
  2608. sp->handle = handle;
  2609. sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
  2610. req->cnt -= req_cnt;
  2611. wmb();
  2612. /* Adjust ring index. */
  2613. req->ring_index++;
  2614. if (req->ring_index == req->length) {
  2615. req->ring_index = 0;
  2616. req->ring_ptr = req->ring;
  2617. } else
  2618. req->ring_ptr++;
  2619. sp->flags |= SRB_DMA_VALID;
  2620. /* Set chip new ring index. */
  2621. /* write, read and verify logic */
  2622. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  2623. if (ql2xdbwr)
  2624. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  2625. else {
  2626. WRT_REG_DWORD(
  2627. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  2628. dbval);
  2629. wmb();
  2630. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  2631. WRT_REG_DWORD(
  2632. (unsigned long __iomem *)ha->nxdb_wr_ptr,
  2633. dbval);
  2634. wmb();
  2635. }
  2636. }
  2637. /* Manage unprocessed RIO/ZIO commands in response queue. */
  2638. if (vha->flags.process_response_queue &&
  2639. rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  2640. qla24xx_process_response_queue(vha, rsp);
  2641. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2642. return QLA_SUCCESS;
  2643. queuing_error_fcp_cmnd:
  2644. dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
  2645. queuing_error:
  2646. if (tot_dsds)
  2647. scsi_dma_unmap(cmd);
  2648. if (sp->ctx) {
  2649. mempool_free(sp->ctx, ha->ctx_mempool);
  2650. sp->ctx = NULL;
  2651. }
  2652. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2653. return QLA_FUNCTION_FAILED;
  2654. }
  2655. static uint32_t *
  2656. qla82xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  2657. uint32_t length)
  2658. {
  2659. uint32_t i;
  2660. uint32_t val;
  2661. struct qla_hw_data *ha = vha->hw;
  2662. /* Dword reads to flash. */
  2663. for (i = 0; i < length/4; i++, faddr += 4) {
  2664. if (qla82xx_rom_fast_read(ha, faddr, &val)) {
  2665. ql_log(ql_log_warn, vha, 0x0106,
  2666. "Do ROM fast read failed.\n");
  2667. goto done_read;
  2668. }
  2669. dwptr[i] = __constant_cpu_to_le32(val);
  2670. }
  2671. done_read:
  2672. return dwptr;
  2673. }
  2674. static int
  2675. qla82xx_unprotect_flash(struct qla_hw_data *ha)
  2676. {
  2677. int ret;
  2678. uint32_t val;
  2679. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2680. ret = ql82xx_rom_lock_d(ha);
  2681. if (ret < 0) {
  2682. ql_log(ql_log_warn, vha, 0xb014,
  2683. "ROM Lock failed.\n");
  2684. return ret;
  2685. }
  2686. ret = qla82xx_read_status_reg(ha, &val);
  2687. if (ret < 0)
  2688. goto done_unprotect;
  2689. val &= ~(BLOCK_PROTECT_BITS << 2);
  2690. ret = qla82xx_write_status_reg(ha, val);
  2691. if (ret < 0) {
  2692. val |= (BLOCK_PROTECT_BITS << 2);
  2693. qla82xx_write_status_reg(ha, val);
  2694. }
  2695. if (qla82xx_write_disable_flash(ha) != 0)
  2696. ql_log(ql_log_warn, vha, 0xb015,
  2697. "Write disable failed.\n");
  2698. done_unprotect:
  2699. qla82xx_rom_unlock(ha);
  2700. return ret;
  2701. }
  2702. static int
  2703. qla82xx_protect_flash(struct qla_hw_data *ha)
  2704. {
  2705. int ret;
  2706. uint32_t val;
  2707. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2708. ret = ql82xx_rom_lock_d(ha);
  2709. if (ret < 0) {
  2710. ql_log(ql_log_warn, vha, 0xb016,
  2711. "ROM Lock failed.\n");
  2712. return ret;
  2713. }
  2714. ret = qla82xx_read_status_reg(ha, &val);
  2715. if (ret < 0)
  2716. goto done_protect;
  2717. val |= (BLOCK_PROTECT_BITS << 2);
  2718. /* LOCK all sectors */
  2719. ret = qla82xx_write_status_reg(ha, val);
  2720. if (ret < 0)
  2721. ql_log(ql_log_warn, vha, 0xb017,
  2722. "Write status register failed.\n");
  2723. if (qla82xx_write_disable_flash(ha) != 0)
  2724. ql_log(ql_log_warn, vha, 0xb018,
  2725. "Write disable failed.\n");
  2726. done_protect:
  2727. qla82xx_rom_unlock(ha);
  2728. return ret;
  2729. }
  2730. static int
  2731. qla82xx_erase_sector(struct qla_hw_data *ha, int addr)
  2732. {
  2733. int ret = 0;
  2734. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2735. ret = ql82xx_rom_lock_d(ha);
  2736. if (ret < 0) {
  2737. ql_log(ql_log_warn, vha, 0xb019,
  2738. "ROM Lock failed.\n");
  2739. return ret;
  2740. }
  2741. qla82xx_flash_set_write_enable(ha);
  2742. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ADDRESS, addr);
  2743. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_ABYTE_CNT, 3);
  2744. qla82xx_wr_32(ha, QLA82XX_ROMUSB_ROM_INSTR_OPCODE, M25P_INSTR_SE);
  2745. if (qla82xx_wait_rom_done(ha)) {
  2746. ql_log(ql_log_warn, vha, 0xb01a,
  2747. "Error waiting for rom done.\n");
  2748. ret = -1;
  2749. goto done;
  2750. }
  2751. ret = qla82xx_flash_wait_write_finish(ha);
  2752. done:
  2753. qla82xx_rom_unlock(ha);
  2754. return ret;
  2755. }
  2756. /*
  2757. * Address and length are byte address
  2758. */
  2759. uint8_t *
  2760. qla82xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  2761. uint32_t offset, uint32_t length)
  2762. {
  2763. scsi_block_requests(vha->host);
  2764. qla82xx_read_flash_data(vha, (uint32_t *)buf, offset, length);
  2765. scsi_unblock_requests(vha->host);
  2766. return buf;
  2767. }
  2768. static int
  2769. qla82xx_write_flash_data(struct scsi_qla_host *vha, uint32_t *dwptr,
  2770. uint32_t faddr, uint32_t dwords)
  2771. {
  2772. int ret;
  2773. uint32_t liter;
  2774. uint32_t sec_mask, rest_addr;
  2775. dma_addr_t optrom_dma;
  2776. void *optrom = NULL;
  2777. int page_mode = 0;
  2778. struct qla_hw_data *ha = vha->hw;
  2779. ret = -1;
  2780. /* Prepare burst-capable write on supported ISPs. */
  2781. if (page_mode && !(faddr & 0xfff) &&
  2782. dwords > OPTROM_BURST_DWORDS) {
  2783. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2784. &optrom_dma, GFP_KERNEL);
  2785. if (!optrom) {
  2786. ql_log(ql_log_warn, vha, 0xb01b,
  2787. "Unable to allocate memory "
  2788. "for optron burst write (%x KB).\n",
  2789. OPTROM_BURST_SIZE / 1024);
  2790. }
  2791. }
  2792. rest_addr = ha->fdt_block_size - 1;
  2793. sec_mask = ~rest_addr;
  2794. ret = qla82xx_unprotect_flash(ha);
  2795. if (ret) {
  2796. ql_log(ql_log_warn, vha, 0xb01c,
  2797. "Unable to unprotect flash for update.\n");
  2798. goto write_done;
  2799. }
  2800. for (liter = 0; liter < dwords; liter++, faddr += 4, dwptr++) {
  2801. /* Are we at the beginning of a sector? */
  2802. if ((faddr & rest_addr) == 0) {
  2803. ret = qla82xx_erase_sector(ha, faddr);
  2804. if (ret) {
  2805. ql_log(ql_log_warn, vha, 0xb01d,
  2806. "Unable to erase sector: address=%x.\n",
  2807. faddr);
  2808. break;
  2809. }
  2810. }
  2811. /* Go with burst-write. */
  2812. if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
  2813. /* Copy data to DMA'ble buffer. */
  2814. memcpy(optrom, dwptr, OPTROM_BURST_SIZE);
  2815. ret = qla2x00_load_ram(vha, optrom_dma,
  2816. (ha->flash_data_off | faddr),
  2817. OPTROM_BURST_DWORDS);
  2818. if (ret != QLA_SUCCESS) {
  2819. ql_log(ql_log_warn, vha, 0xb01e,
  2820. "Unable to burst-write optrom segment "
  2821. "(%x/%x/%llx).\n", ret,
  2822. (ha->flash_data_off | faddr),
  2823. (unsigned long long)optrom_dma);
  2824. ql_log(ql_log_warn, vha, 0xb01f,
  2825. "Reverting to slow-write.\n");
  2826. dma_free_coherent(&ha->pdev->dev,
  2827. OPTROM_BURST_SIZE, optrom, optrom_dma);
  2828. optrom = NULL;
  2829. } else {
  2830. liter += OPTROM_BURST_DWORDS - 1;
  2831. faddr += OPTROM_BURST_DWORDS - 1;
  2832. dwptr += OPTROM_BURST_DWORDS - 1;
  2833. continue;
  2834. }
  2835. }
  2836. ret = qla82xx_write_flash_dword(ha, faddr,
  2837. cpu_to_le32(*dwptr));
  2838. if (ret) {
  2839. ql_dbg(ql_dbg_p3p, vha, 0xb020,
  2840. "Unable to program flash address=%x data=%x.\n",
  2841. faddr, *dwptr);
  2842. break;
  2843. }
  2844. }
  2845. ret = qla82xx_protect_flash(ha);
  2846. if (ret)
  2847. ql_log(ql_log_warn, vha, 0xb021,
  2848. "Unable to protect flash after update.\n");
  2849. write_done:
  2850. if (optrom)
  2851. dma_free_coherent(&ha->pdev->dev,
  2852. OPTROM_BURST_SIZE, optrom, optrom_dma);
  2853. return ret;
  2854. }
  2855. int
  2856. qla82xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  2857. uint32_t offset, uint32_t length)
  2858. {
  2859. int rval;
  2860. /* Suspend HBA. */
  2861. scsi_block_requests(vha->host);
  2862. rval = qla82xx_write_flash_data(vha, (uint32_t *)buf, offset,
  2863. length >> 2);
  2864. scsi_unblock_requests(vha->host);
  2865. /* Convert return ISP82xx to generic */
  2866. if (rval)
  2867. rval = QLA_FUNCTION_FAILED;
  2868. else
  2869. rval = QLA_SUCCESS;
  2870. return rval;
  2871. }
  2872. void
  2873. qla82xx_start_iocbs(srb_t *sp)
  2874. {
  2875. struct qla_hw_data *ha = sp->fcport->vha->hw;
  2876. struct req_que *req = ha->req_q_map[0];
  2877. struct device_reg_82xx __iomem *reg;
  2878. uint32_t dbval;
  2879. /* Adjust ring index. */
  2880. req->ring_index++;
  2881. if (req->ring_index == req->length) {
  2882. req->ring_index = 0;
  2883. req->ring_ptr = req->ring;
  2884. } else
  2885. req->ring_ptr++;
  2886. reg = &ha->iobase->isp82;
  2887. dbval = 0x04 | (ha->portnum << 5);
  2888. dbval = dbval | (req->id << 8) | (req->ring_index << 16);
  2889. if (ql2xdbwr)
  2890. qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
  2891. else {
  2892. WRT_REG_DWORD((unsigned long __iomem *)ha->nxdb_wr_ptr, dbval);
  2893. wmb();
  2894. while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
  2895. WRT_REG_DWORD((unsigned long __iomem *)ha->nxdb_wr_ptr,
  2896. dbval);
  2897. wmb();
  2898. }
  2899. }
  2900. }
  2901. void qla82xx_rom_lock_recovery(struct qla_hw_data *ha)
  2902. {
  2903. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  2904. if (qla82xx_rom_lock(ha))
  2905. /* Someone else is holding the lock. */
  2906. ql_log(ql_log_info, vha, 0xb022,
  2907. "Resetting rom_lock.\n");
  2908. /*
  2909. * Either we got the lock, or someone
  2910. * else died while holding it.
  2911. * In either case, unlock.
  2912. */
  2913. qla82xx_rom_unlock(ha);
  2914. }
  2915. /*
  2916. * qla82xx_device_bootstrap
  2917. * Initialize device, set DEV_READY, start fw
  2918. *
  2919. * Note:
  2920. * IDC lock must be held upon entry
  2921. *
  2922. * Return:
  2923. * Success : 0
  2924. * Failed : 1
  2925. */
  2926. static int
  2927. qla82xx_device_bootstrap(scsi_qla_host_t *vha)
  2928. {
  2929. int rval = QLA_SUCCESS;
  2930. int i, timeout;
  2931. uint32_t old_count, count;
  2932. struct qla_hw_data *ha = vha->hw;
  2933. int need_reset = 0, peg_stuck = 1;
  2934. need_reset = qla82xx_need_reset(ha);
  2935. old_count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  2936. for (i = 0; i < 10; i++) {
  2937. timeout = msleep_interruptible(200);
  2938. if (timeout) {
  2939. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  2940. QLA82XX_DEV_FAILED);
  2941. return QLA_FUNCTION_FAILED;
  2942. }
  2943. count = qla82xx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
  2944. if (count != old_count)
  2945. peg_stuck = 0;
  2946. }
  2947. if (need_reset) {
  2948. /* We are trying to perform a recovery here. */
  2949. if (peg_stuck)
  2950. qla82xx_rom_lock_recovery(ha);
  2951. goto dev_initialize;
  2952. } else {
  2953. /* Start of day for this ha context. */
  2954. if (peg_stuck) {
  2955. /* Either we are the first or recovery in progress. */
  2956. qla82xx_rom_lock_recovery(ha);
  2957. goto dev_initialize;
  2958. } else
  2959. /* Firmware already running. */
  2960. goto dev_ready;
  2961. }
  2962. return rval;
  2963. dev_initialize:
  2964. /* set to DEV_INITIALIZING */
  2965. ql_log(ql_log_info, vha, 0x009e,
  2966. "HW State: INITIALIZING.\n");
  2967. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_INITIALIZING);
  2968. /* Driver that sets device state to initializating sets IDC version */
  2969. qla82xx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION, QLA82XX_IDC_VERSION);
  2970. qla82xx_idc_unlock(ha);
  2971. rval = qla82xx_start_firmware(vha);
  2972. qla82xx_idc_lock(ha);
  2973. if (rval != QLA_SUCCESS) {
  2974. ql_log(ql_log_fatal, vha, 0x00ad,
  2975. "HW State: FAILED.\n");
  2976. qla82xx_clear_drv_active(ha);
  2977. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_FAILED);
  2978. return rval;
  2979. }
  2980. dev_ready:
  2981. ql_log(ql_log_info, vha, 0x00ae,
  2982. "HW State: READY.\n");
  2983. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_READY);
  2984. return QLA_SUCCESS;
  2985. }
  2986. /*
  2987. * qla82xx_need_qsnt_handler
  2988. * Code to start quiescence sequence
  2989. *
  2990. * Note:
  2991. * IDC lock must be held upon entry
  2992. *
  2993. * Return: void
  2994. */
  2995. static void
  2996. qla82xx_need_qsnt_handler(scsi_qla_host_t *vha)
  2997. {
  2998. struct qla_hw_data *ha = vha->hw;
  2999. uint32_t dev_state, drv_state, drv_active;
  3000. unsigned long reset_timeout;
  3001. if (vha->flags.online) {
  3002. /*Block any further I/O and wait for pending cmnds to complete*/
  3003. qla82xx_quiescent_state_cleanup(vha);
  3004. }
  3005. /* Set the quiescence ready bit */
  3006. qla82xx_set_qsnt_ready(ha);
  3007. /*wait for 30 secs for other functions to ack */
  3008. reset_timeout = jiffies + (30 * HZ);
  3009. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3010. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3011. /* Its 2 that is written when qsnt is acked, moving one bit */
  3012. drv_active = drv_active << 0x01;
  3013. while (drv_state != drv_active) {
  3014. if (time_after_eq(jiffies, reset_timeout)) {
  3015. /* quiescence timeout, other functions didn't ack
  3016. * changing the state to DEV_READY
  3017. */
  3018. ql_log(ql_log_info, vha, 0xb023,
  3019. "%s : QUIESCENT TIMEOUT.\n", QLA2XXX_DRIVER_NAME);
  3020. ql_log(ql_log_info, vha, 0xb024,
  3021. "DRV_ACTIVE:%d DRV_STATE:%d.\n",
  3022. drv_active, drv_state);
  3023. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3024. QLA82XX_DEV_READY);
  3025. ql_log(ql_log_info, vha, 0xb025,
  3026. "HW State: DEV_READY.\n");
  3027. qla82xx_idc_unlock(ha);
  3028. qla2x00_perform_loop_resync(vha);
  3029. qla82xx_idc_lock(ha);
  3030. qla82xx_clear_qsnt_ready(vha);
  3031. return;
  3032. }
  3033. qla82xx_idc_unlock(ha);
  3034. msleep(1000);
  3035. qla82xx_idc_lock(ha);
  3036. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3037. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3038. drv_active = drv_active << 0x01;
  3039. }
  3040. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3041. /* everyone acked so set the state to DEV_QUIESCENCE */
  3042. if (dev_state == QLA82XX_DEV_NEED_QUIESCENT) {
  3043. ql_log(ql_log_info, vha, 0xb026,
  3044. "HW State: DEV_QUIESCENT.\n");
  3045. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_QUIESCENT);
  3046. }
  3047. }
  3048. /*
  3049. * qla82xx_wait_for_state_change
  3050. * Wait for device state to change from given current state
  3051. *
  3052. * Note:
  3053. * IDC lock must not be held upon entry
  3054. *
  3055. * Return:
  3056. * Changed device state.
  3057. */
  3058. uint32_t
  3059. qla82xx_wait_for_state_change(scsi_qla_host_t *vha, uint32_t curr_state)
  3060. {
  3061. struct qla_hw_data *ha = vha->hw;
  3062. uint32_t dev_state;
  3063. do {
  3064. msleep(1000);
  3065. qla82xx_idc_lock(ha);
  3066. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3067. qla82xx_idc_unlock(ha);
  3068. } while (dev_state == curr_state);
  3069. return dev_state;
  3070. }
  3071. static void
  3072. qla82xx_dev_failed_handler(scsi_qla_host_t *vha)
  3073. {
  3074. struct qla_hw_data *ha = vha->hw;
  3075. /* Disable the board */
  3076. ql_log(ql_log_fatal, vha, 0x00b8,
  3077. "Disabling the board.\n");
  3078. qla82xx_idc_lock(ha);
  3079. qla82xx_clear_drv_active(ha);
  3080. qla82xx_idc_unlock(ha);
  3081. /* Set DEV_FAILED flag to disable timer */
  3082. vha->device_flags |= DFLG_DEV_FAILED;
  3083. qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
  3084. qla2x00_mark_all_devices_lost(vha, 0);
  3085. vha->flags.online = 0;
  3086. vha->flags.init_done = 0;
  3087. }
  3088. /*
  3089. * qla82xx_need_reset_handler
  3090. * Code to start reset sequence
  3091. *
  3092. * Note:
  3093. * IDC lock must be held upon entry
  3094. *
  3095. * Return:
  3096. * Success : 0
  3097. * Failed : 1
  3098. */
  3099. static void
  3100. qla82xx_need_reset_handler(scsi_qla_host_t *vha)
  3101. {
  3102. uint32_t dev_state, drv_state, drv_active;
  3103. uint32_t active_mask = 0;
  3104. unsigned long reset_timeout;
  3105. struct qla_hw_data *ha = vha->hw;
  3106. struct req_que *req = ha->req_q_map[0];
  3107. if (vha->flags.online) {
  3108. qla82xx_idc_unlock(ha);
  3109. qla2x00_abort_isp_cleanup(vha);
  3110. ha->isp_ops->get_flash_version(vha, req->ring);
  3111. ha->isp_ops->nvram_config(vha);
  3112. qla82xx_idc_lock(ha);
  3113. }
  3114. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3115. if (!ha->flags.isp82xx_reset_owner) {
  3116. ql_dbg(ql_dbg_p3p, vha, 0xb028,
  3117. "reset_acknowledged by 0x%x\n", ha->portnum);
  3118. qla82xx_set_rst_ready(ha);
  3119. } else {
  3120. active_mask = ~(QLA82XX_DRV_ACTIVE << (ha->portnum * 4));
  3121. drv_active &= active_mask;
  3122. ql_dbg(ql_dbg_p3p, vha, 0xb029,
  3123. "active_mask: 0x%08x\n", active_mask);
  3124. }
  3125. /* wait for 10 seconds for reset ack from all functions */
  3126. reset_timeout = jiffies + (ha->nx_reset_timeout * HZ);
  3127. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3128. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3129. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3130. ql_dbg(ql_dbg_p3p, vha, 0xb02a,
  3131. "drv_state: 0x%08x, drv_active: 0x%08x, "
  3132. "dev_state: 0x%08x, active_mask: 0x%08x\n",
  3133. drv_state, drv_active, dev_state, active_mask);
  3134. while (drv_state != drv_active &&
  3135. dev_state != QLA82XX_DEV_INITIALIZING) {
  3136. if (time_after_eq(jiffies, reset_timeout)) {
  3137. ql_log(ql_log_warn, vha, 0x00b5,
  3138. "Reset timeout.\n");
  3139. break;
  3140. }
  3141. qla82xx_idc_unlock(ha);
  3142. msleep(1000);
  3143. qla82xx_idc_lock(ha);
  3144. drv_state = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_STATE);
  3145. drv_active = qla82xx_rd_32(ha, QLA82XX_CRB_DRV_ACTIVE);
  3146. if (ha->flags.isp82xx_reset_owner)
  3147. drv_active &= active_mask;
  3148. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3149. }
  3150. ql_dbg(ql_dbg_p3p, vha, 0xb02b,
  3151. "drv_state: 0x%08x, drv_active: 0x%08x, "
  3152. "dev_state: 0x%08x, active_mask: 0x%08x\n",
  3153. drv_state, drv_active, dev_state, active_mask);
  3154. ql_log(ql_log_info, vha, 0x00b6,
  3155. "Device state is 0x%x = %s.\n",
  3156. dev_state,
  3157. dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
  3158. /* Force to DEV_COLD unless someone else is starting a reset */
  3159. if (dev_state != QLA82XX_DEV_INITIALIZING &&
  3160. dev_state != QLA82XX_DEV_COLD) {
  3161. ql_log(ql_log_info, vha, 0x00b7,
  3162. "HW State: COLD/RE-INIT.\n");
  3163. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE, QLA82XX_DEV_COLD);
  3164. if (ql2xmdenable) {
  3165. if (qla82xx_md_collect(vha))
  3166. ql_log(ql_log_warn, vha, 0xb02c,
  3167. "Not able to collect minidump.\n");
  3168. } else
  3169. ql_log(ql_log_warn, vha, 0xb04f,
  3170. "Minidump disabled.\n");
  3171. }
  3172. }
  3173. int
  3174. qla82xx_check_md_needed(scsi_qla_host_t *vha)
  3175. {
  3176. struct qla_hw_data *ha = vha->hw;
  3177. uint16_t fw_major_version, fw_minor_version, fw_subminor_version;
  3178. int rval = QLA_SUCCESS;
  3179. fw_major_version = ha->fw_major_version;
  3180. fw_minor_version = ha->fw_minor_version;
  3181. fw_subminor_version = ha->fw_subminor_version;
  3182. rval = qla2x00_get_fw_version(vha, &ha->fw_major_version,
  3183. &ha->fw_minor_version, &ha->fw_subminor_version,
  3184. &ha->fw_attributes, &ha->fw_memory_size,
  3185. ha->mpi_version, &ha->mpi_capabilities,
  3186. ha->phy_version);
  3187. if (rval != QLA_SUCCESS)
  3188. return rval;
  3189. if (ql2xmdenable) {
  3190. if (!ha->fw_dumped) {
  3191. if (fw_major_version != ha->fw_major_version ||
  3192. fw_minor_version != ha->fw_minor_version ||
  3193. fw_subminor_version != ha->fw_subminor_version) {
  3194. ql_log(ql_log_info, vha, 0xb02d,
  3195. "Firmware version differs "
  3196. "Previous version: %d:%d:%d - "
  3197. "New version: %d:%d:%d\n",
  3198. ha->fw_major_version,
  3199. ha->fw_minor_version,
  3200. ha->fw_subminor_version,
  3201. fw_major_version, fw_minor_version,
  3202. fw_subminor_version);
  3203. /* Release MiniDump resources */
  3204. qla82xx_md_free(vha);
  3205. /* ALlocate MiniDump resources */
  3206. qla82xx_md_prep(vha);
  3207. } else
  3208. ql_log(ql_log_info, vha, 0xb02e,
  3209. "Firmware dump available to retrieve\n");
  3210. }
  3211. }
  3212. return rval;
  3213. }
  3214. int
  3215. qla82xx_check_fw_alive(scsi_qla_host_t *vha)
  3216. {
  3217. uint32_t fw_heartbeat_counter;
  3218. int status = 0;
  3219. fw_heartbeat_counter = qla82xx_rd_32(vha->hw,
  3220. QLA82XX_PEG_ALIVE_COUNTER);
  3221. /* all 0xff, assume AER/EEH in progress, ignore */
  3222. if (fw_heartbeat_counter == 0xffffffff) {
  3223. ql_dbg(ql_dbg_timer, vha, 0x6003,
  3224. "FW heartbeat counter is 0xffffffff, "
  3225. "returning status=%d.\n", status);
  3226. return status;
  3227. }
  3228. if (vha->fw_heartbeat_counter == fw_heartbeat_counter) {
  3229. vha->seconds_since_last_heartbeat++;
  3230. /* FW not alive after 2 seconds */
  3231. if (vha->seconds_since_last_heartbeat == 2) {
  3232. vha->seconds_since_last_heartbeat = 0;
  3233. status = 1;
  3234. }
  3235. } else
  3236. vha->seconds_since_last_heartbeat = 0;
  3237. vha->fw_heartbeat_counter = fw_heartbeat_counter;
  3238. if (status)
  3239. ql_dbg(ql_dbg_timer, vha, 0x6004,
  3240. "Returning status=%d.\n", status);
  3241. return status;
  3242. }
  3243. /*
  3244. * qla82xx_device_state_handler
  3245. * Main state handler
  3246. *
  3247. * Note:
  3248. * IDC lock must be held upon entry
  3249. *
  3250. * Return:
  3251. * Success : 0
  3252. * Failed : 1
  3253. */
  3254. int
  3255. qla82xx_device_state_handler(scsi_qla_host_t *vha)
  3256. {
  3257. uint32_t dev_state;
  3258. uint32_t old_dev_state;
  3259. int rval = QLA_SUCCESS;
  3260. unsigned long dev_init_timeout;
  3261. struct qla_hw_data *ha = vha->hw;
  3262. int loopcount = 0;
  3263. qla82xx_idc_lock(ha);
  3264. if (!vha->flags.init_done)
  3265. qla82xx_set_drv_active(vha);
  3266. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3267. old_dev_state = dev_state;
  3268. ql_log(ql_log_info, vha, 0x009b,
  3269. "Device state is 0x%x = %s.\n",
  3270. dev_state,
  3271. dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
  3272. /* wait for 30 seconds for device to go ready */
  3273. dev_init_timeout = jiffies + (ha->nx_dev_init_timeout * HZ);
  3274. while (1) {
  3275. if (time_after_eq(jiffies, dev_init_timeout)) {
  3276. ql_log(ql_log_fatal, vha, 0x009c,
  3277. "Device init failed.\n");
  3278. rval = QLA_FUNCTION_FAILED;
  3279. break;
  3280. }
  3281. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3282. if (old_dev_state != dev_state) {
  3283. loopcount = 0;
  3284. old_dev_state = dev_state;
  3285. }
  3286. if (loopcount < 5) {
  3287. ql_log(ql_log_info, vha, 0x009d,
  3288. "Device state is 0x%x = %s.\n",
  3289. dev_state,
  3290. dev_state < MAX_STATES ? qdev_state(dev_state) :
  3291. "Unknown");
  3292. }
  3293. switch (dev_state) {
  3294. case QLA82XX_DEV_READY:
  3295. qla82xx_check_md_needed(vha);
  3296. ha->flags.isp82xx_reset_owner = 0;
  3297. goto exit;
  3298. case QLA82XX_DEV_COLD:
  3299. rval = qla82xx_device_bootstrap(vha);
  3300. break;
  3301. case QLA82XX_DEV_INITIALIZING:
  3302. qla82xx_idc_unlock(ha);
  3303. msleep(1000);
  3304. qla82xx_idc_lock(ha);
  3305. break;
  3306. case QLA82XX_DEV_NEED_RESET:
  3307. if (!ql2xdontresethba)
  3308. qla82xx_need_reset_handler(vha);
  3309. else {
  3310. qla82xx_idc_unlock(ha);
  3311. msleep(1000);
  3312. qla82xx_idc_lock(ha);
  3313. }
  3314. dev_init_timeout = jiffies +
  3315. (ha->nx_dev_init_timeout * HZ);
  3316. break;
  3317. case QLA82XX_DEV_NEED_QUIESCENT:
  3318. qla82xx_need_qsnt_handler(vha);
  3319. /* Reset timeout value after quiescence handler */
  3320. dev_init_timeout = jiffies + (ha->nx_dev_init_timeout\
  3321. * HZ);
  3322. break;
  3323. case QLA82XX_DEV_QUIESCENT:
  3324. /* Owner will exit and other will wait for the state
  3325. * to get changed
  3326. */
  3327. if (ha->flags.quiesce_owner)
  3328. goto exit;
  3329. qla82xx_idc_unlock(ha);
  3330. msleep(1000);
  3331. qla82xx_idc_lock(ha);
  3332. /* Reset timeout value after quiescence handler */
  3333. dev_init_timeout = jiffies + (ha->nx_dev_init_timeout\
  3334. * HZ);
  3335. break;
  3336. case QLA82XX_DEV_FAILED:
  3337. qla82xx_dev_failed_handler(vha);
  3338. rval = QLA_FUNCTION_FAILED;
  3339. goto exit;
  3340. default:
  3341. qla82xx_idc_unlock(ha);
  3342. msleep(1000);
  3343. qla82xx_idc_lock(ha);
  3344. }
  3345. loopcount++;
  3346. }
  3347. exit:
  3348. qla82xx_idc_unlock(ha);
  3349. return rval;
  3350. }
  3351. void qla82xx_clear_pending_mbx(scsi_qla_host_t *vha)
  3352. {
  3353. struct qla_hw_data *ha = vha->hw;
  3354. if (ha->flags.mbox_busy) {
  3355. ha->flags.mbox_int = 1;
  3356. ha->flags.mbox_busy = 0;
  3357. ql_log(ql_log_warn, vha, 0x6010,
  3358. "Doing premature completion of mbx command.\n");
  3359. if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags))
  3360. complete(&ha->mbx_intr_comp);
  3361. }
  3362. }
  3363. void qla82xx_watchdog(scsi_qla_host_t *vha)
  3364. {
  3365. uint32_t dev_state, halt_status;
  3366. struct qla_hw_data *ha = vha->hw;
  3367. /* don't poll if reset is going on */
  3368. if (!ha->flags.isp82xx_reset_hdlr_active) {
  3369. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3370. if (dev_state == QLA82XX_DEV_NEED_RESET &&
  3371. !test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags)) {
  3372. ql_log(ql_log_warn, vha, 0x6001,
  3373. "Adapter reset needed.\n");
  3374. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  3375. qla2xxx_wake_dpc(vha);
  3376. } else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  3377. !test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags)) {
  3378. ql_log(ql_log_warn, vha, 0x6002,
  3379. "Quiescent needed.\n");
  3380. set_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags);
  3381. qla2xxx_wake_dpc(vha);
  3382. } else {
  3383. if (qla82xx_check_fw_alive(vha)) {
  3384. ql_dbg(ql_dbg_timer, vha, 0x6011,
  3385. "disabling pause transmit on port 0 & 1.\n");
  3386. qla82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
  3387. CRB_NIU_XG_PAUSE_CTL_P0|CRB_NIU_XG_PAUSE_CTL_P1);
  3388. halt_status = qla82xx_rd_32(ha,
  3389. QLA82XX_PEG_HALT_STATUS1);
  3390. ql_log(ql_log_info, vha, 0x6005,
  3391. "dumping hw/fw registers:.\n "
  3392. " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,.\n "
  3393. " PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,.\n "
  3394. " PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,.\n "
  3395. " PEG_NET_4_PC: 0x%x.\n", halt_status,
  3396. qla82xx_rd_32(ha, QLA82XX_PEG_HALT_STATUS2),
  3397. qla82xx_rd_32(ha,
  3398. QLA82XX_CRB_PEG_NET_0 + 0x3c),
  3399. qla82xx_rd_32(ha,
  3400. QLA82XX_CRB_PEG_NET_1 + 0x3c),
  3401. qla82xx_rd_32(ha,
  3402. QLA82XX_CRB_PEG_NET_2 + 0x3c),
  3403. qla82xx_rd_32(ha,
  3404. QLA82XX_CRB_PEG_NET_3 + 0x3c),
  3405. qla82xx_rd_32(ha,
  3406. QLA82XX_CRB_PEG_NET_4 + 0x3c));
  3407. if (LSW(MSB(halt_status)) == 0x67)
  3408. ql_log(ql_log_warn, vha, 0xb052,
  3409. "Firmware aborted with "
  3410. "error code 0x00006700. Device is "
  3411. "being reset.\n");
  3412. if (halt_status & HALT_STATUS_UNRECOVERABLE) {
  3413. set_bit(ISP_UNRECOVERABLE,
  3414. &vha->dpc_flags);
  3415. } else {
  3416. ql_log(ql_log_info, vha, 0x6006,
  3417. "Detect abort needed.\n");
  3418. set_bit(ISP_ABORT_NEEDED,
  3419. &vha->dpc_flags);
  3420. }
  3421. qla2xxx_wake_dpc(vha);
  3422. ha->flags.isp82xx_fw_hung = 1;
  3423. ql_log(ql_log_warn, vha, 0x6007, "Firmware hung.\n");
  3424. qla82xx_clear_pending_mbx(vha);
  3425. }
  3426. }
  3427. }
  3428. }
  3429. int qla82xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
  3430. {
  3431. int rval;
  3432. rval = qla82xx_device_state_handler(vha);
  3433. return rval;
  3434. }
  3435. void
  3436. qla82xx_set_reset_owner(scsi_qla_host_t *vha)
  3437. {
  3438. struct qla_hw_data *ha = vha->hw;
  3439. uint32_t dev_state;
  3440. dev_state = qla82xx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  3441. if (dev_state == QLA82XX_DEV_READY) {
  3442. ql_log(ql_log_info, vha, 0xb02f,
  3443. "HW State: NEED RESET\n");
  3444. qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  3445. QLA82XX_DEV_NEED_RESET);
  3446. ha->flags.isp82xx_reset_owner = 1;
  3447. ql_dbg(ql_dbg_p3p, vha, 0xb030,
  3448. "reset_owner is 0x%x\n", ha->portnum);
  3449. } else
  3450. ql_log(ql_log_info, vha, 0xb031,
  3451. "Device state is 0x%x = %s.\n",
  3452. dev_state,
  3453. dev_state < MAX_STATES ? qdev_state(dev_state) : "Unknown");
  3454. }
  3455. /*
  3456. * qla82xx_abort_isp
  3457. * Resets ISP and aborts all outstanding commands.
  3458. *
  3459. * Input:
  3460. * ha = adapter block pointer.
  3461. *
  3462. * Returns:
  3463. * 0 = success
  3464. */
  3465. int
  3466. qla82xx_abort_isp(scsi_qla_host_t *vha)
  3467. {
  3468. int rval;
  3469. struct qla_hw_data *ha = vha->hw;
  3470. if (vha->device_flags & DFLG_DEV_FAILED) {
  3471. ql_log(ql_log_warn, vha, 0x8024,
  3472. "Device in failed state, exiting.\n");
  3473. return QLA_SUCCESS;
  3474. }
  3475. ha->flags.isp82xx_reset_hdlr_active = 1;
  3476. qla82xx_idc_lock(ha);
  3477. qla82xx_set_reset_owner(vha);
  3478. qla82xx_idc_unlock(ha);
  3479. rval = qla82xx_device_state_handler(vha);
  3480. qla82xx_idc_lock(ha);
  3481. qla82xx_clear_rst_ready(ha);
  3482. qla82xx_idc_unlock(ha);
  3483. if (rval == QLA_SUCCESS) {
  3484. ha->flags.isp82xx_fw_hung = 0;
  3485. ha->flags.isp82xx_reset_hdlr_active = 0;
  3486. qla82xx_restart_isp(vha);
  3487. }
  3488. if (rval) {
  3489. vha->flags.online = 1;
  3490. if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
  3491. if (ha->isp_abort_cnt == 0) {
  3492. ql_log(ql_log_warn, vha, 0x8027,
  3493. "ISP error recover failed - board "
  3494. "disabled.\n");
  3495. /*
  3496. * The next call disables the board
  3497. * completely.
  3498. */
  3499. ha->isp_ops->reset_adapter(vha);
  3500. vha->flags.online = 0;
  3501. clear_bit(ISP_ABORT_RETRY,
  3502. &vha->dpc_flags);
  3503. rval = QLA_SUCCESS;
  3504. } else { /* schedule another ISP abort */
  3505. ha->isp_abort_cnt--;
  3506. ql_log(ql_log_warn, vha, 0x8036,
  3507. "ISP abort - retry remaining %d.\n",
  3508. ha->isp_abort_cnt);
  3509. rval = QLA_FUNCTION_FAILED;
  3510. }
  3511. } else {
  3512. ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
  3513. ql_dbg(ql_dbg_taskm, vha, 0x8029,
  3514. "ISP error recovery - retrying (%d) more times.\n",
  3515. ha->isp_abort_cnt);
  3516. set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
  3517. rval = QLA_FUNCTION_FAILED;
  3518. }
  3519. }
  3520. return rval;
  3521. }
  3522. /*
  3523. * qla82xx_fcoe_ctx_reset
  3524. * Perform a quick reset and aborts all outstanding commands.
  3525. * This will only perform an FCoE context reset and avoids a full blown
  3526. * chip reset.
  3527. *
  3528. * Input:
  3529. * ha = adapter block pointer.
  3530. * is_reset_path = flag for identifying the reset path.
  3531. *
  3532. * Returns:
  3533. * 0 = success
  3534. */
  3535. int qla82xx_fcoe_ctx_reset(scsi_qla_host_t *vha)
  3536. {
  3537. int rval = QLA_FUNCTION_FAILED;
  3538. if (vha->flags.online) {
  3539. /* Abort all outstanding commands, so as to be requeued later */
  3540. qla2x00_abort_isp_cleanup(vha);
  3541. }
  3542. /* Stop currently executing firmware.
  3543. * This will destroy existing FCoE context at the F/W end.
  3544. */
  3545. qla2x00_try_to_stop_firmware(vha);
  3546. /* Restart. Creates a new FCoE context on INIT_FIRMWARE. */
  3547. rval = qla82xx_restart_isp(vha);
  3548. return rval;
  3549. }
  3550. /*
  3551. * qla2x00_wait_for_fcoe_ctx_reset
  3552. * Wait till the FCoE context is reset.
  3553. *
  3554. * Note:
  3555. * Does context switching here.
  3556. * Release SPIN_LOCK (if any) before calling this routine.
  3557. *
  3558. * Return:
  3559. * Success (fcoe_ctx reset is done) : 0
  3560. * Failed (fcoe_ctx reset not completed within max loop timout ) : 1
  3561. */
  3562. int qla2x00_wait_for_fcoe_ctx_reset(scsi_qla_host_t *vha)
  3563. {
  3564. int status = QLA_FUNCTION_FAILED;
  3565. unsigned long wait_reset;
  3566. wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
  3567. while ((test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
  3568. test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
  3569. && time_before(jiffies, wait_reset)) {
  3570. set_current_state(TASK_UNINTERRUPTIBLE);
  3571. schedule_timeout(HZ);
  3572. if (!test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) &&
  3573. !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
  3574. status = QLA_SUCCESS;
  3575. break;
  3576. }
  3577. }
  3578. ql_dbg(ql_dbg_p3p, vha, 0xb027,
  3579. "%s: status=%d.\n", __func__, status);
  3580. return status;
  3581. }
  3582. void
  3583. qla82xx_chip_reset_cleanup(scsi_qla_host_t *vha)
  3584. {
  3585. int i;
  3586. unsigned long flags;
  3587. struct qla_hw_data *ha = vha->hw;
  3588. /* Check if 82XX firmware is alive or not
  3589. * We may have arrived here from NEED_RESET
  3590. * detection only
  3591. */
  3592. if (!ha->flags.isp82xx_fw_hung) {
  3593. for (i = 0; i < 2; i++) {
  3594. msleep(1000);
  3595. if (qla82xx_check_fw_alive(vha)) {
  3596. ha->flags.isp82xx_fw_hung = 1;
  3597. qla82xx_clear_pending_mbx(vha);
  3598. break;
  3599. }
  3600. }
  3601. }
  3602. ql_dbg(ql_dbg_init, vha, 0x00b0,
  3603. "Entered %s fw_hung=%d.\n",
  3604. __func__, ha->flags.isp82xx_fw_hung);
  3605. /* Abort all commands gracefully if fw NOT hung */
  3606. if (!ha->flags.isp82xx_fw_hung) {
  3607. int cnt, que;
  3608. srb_t *sp;
  3609. struct req_que *req;
  3610. spin_lock_irqsave(&ha->hardware_lock, flags);
  3611. for (que = 0; que < ha->max_req_queues; que++) {
  3612. req = ha->req_q_map[que];
  3613. if (!req)
  3614. continue;
  3615. for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
  3616. sp = req->outstanding_cmds[cnt];
  3617. if (sp) {
  3618. if (!sp->ctx ||
  3619. (sp->flags & SRB_FCP_CMND_DMA_VALID)) {
  3620. spin_unlock_irqrestore(
  3621. &ha->hardware_lock, flags);
  3622. if (ha->isp_ops->abort_command(sp)) {
  3623. ql_log(ql_log_info, vha,
  3624. 0x00b1,
  3625. "mbx abort failed.\n");
  3626. } else {
  3627. ql_log(ql_log_info, vha,
  3628. 0x00b2,
  3629. "mbx abort success.\n");
  3630. }
  3631. spin_lock_irqsave(&ha->hardware_lock, flags);
  3632. }
  3633. }
  3634. }
  3635. }
  3636. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  3637. /* Wait for pending cmds (physical and virtual) to complete */
  3638. if (!qla2x00_eh_wait_for_pending_commands(vha, 0, 0,
  3639. WAIT_HOST) == QLA_SUCCESS) {
  3640. ql_dbg(ql_dbg_init, vha, 0x00b3,
  3641. "Done wait for "
  3642. "pending commands.\n");
  3643. }
  3644. }
  3645. }
  3646. /* Minidump related functions */
  3647. int
  3648. qla82xx_md_rw_32(struct qla_hw_data *ha, uint32_t off, u32 data, uint8_t flag)
  3649. {
  3650. uint32_t off_value, rval = 0;
  3651. WRT_REG_DWORD((void *)(CRB_WINDOW_2M + ha->nx_pcibase),
  3652. (off & 0xFFFF0000));
  3653. /* Read back value to make sure write has gone through */
  3654. RD_REG_DWORD((void *)(CRB_WINDOW_2M + ha->nx_pcibase));
  3655. off_value = (off & 0x0000FFFF);
  3656. if (flag)
  3657. WRT_REG_DWORD((void *)
  3658. (off_value + CRB_INDIRECT_2M + ha->nx_pcibase),
  3659. data);
  3660. else
  3661. rval = RD_REG_DWORD((void *)
  3662. (off_value + CRB_INDIRECT_2M + ha->nx_pcibase));
  3663. return rval;
  3664. }
  3665. static int
  3666. qla82xx_minidump_process_control(scsi_qla_host_t *vha,
  3667. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3668. {
  3669. struct qla_hw_data *ha = vha->hw;
  3670. struct qla82xx_md_entry_crb *crb_entry;
  3671. uint32_t read_value, opcode, poll_time;
  3672. uint32_t addr, index, crb_addr;
  3673. unsigned long wtime;
  3674. struct qla82xx_md_template_hdr *tmplt_hdr;
  3675. uint32_t rval = QLA_SUCCESS;
  3676. int i;
  3677. tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
  3678. crb_entry = (struct qla82xx_md_entry_crb *)entry_hdr;
  3679. crb_addr = crb_entry->addr;
  3680. for (i = 0; i < crb_entry->op_count; i++) {
  3681. opcode = crb_entry->crb_ctrl.opcode;
  3682. if (opcode & QLA82XX_DBG_OPCODE_WR) {
  3683. qla82xx_md_rw_32(ha, crb_addr,
  3684. crb_entry->value_1, 1);
  3685. opcode &= ~QLA82XX_DBG_OPCODE_WR;
  3686. }
  3687. if (opcode & QLA82XX_DBG_OPCODE_RW) {
  3688. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3689. qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
  3690. opcode &= ~QLA82XX_DBG_OPCODE_RW;
  3691. }
  3692. if (opcode & QLA82XX_DBG_OPCODE_AND) {
  3693. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3694. read_value &= crb_entry->value_2;
  3695. opcode &= ~QLA82XX_DBG_OPCODE_AND;
  3696. if (opcode & QLA82XX_DBG_OPCODE_OR) {
  3697. read_value |= crb_entry->value_3;
  3698. opcode &= ~QLA82XX_DBG_OPCODE_OR;
  3699. }
  3700. qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
  3701. }
  3702. if (opcode & QLA82XX_DBG_OPCODE_OR) {
  3703. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3704. read_value |= crb_entry->value_3;
  3705. qla82xx_md_rw_32(ha, crb_addr, read_value, 1);
  3706. opcode &= ~QLA82XX_DBG_OPCODE_OR;
  3707. }
  3708. if (opcode & QLA82XX_DBG_OPCODE_POLL) {
  3709. poll_time = crb_entry->crb_strd.poll_timeout;
  3710. wtime = jiffies + poll_time;
  3711. read_value = qla82xx_md_rw_32(ha, crb_addr, 0, 0);
  3712. do {
  3713. if ((read_value & crb_entry->value_2)
  3714. == crb_entry->value_1)
  3715. break;
  3716. else if (time_after_eq(jiffies, wtime)) {
  3717. /* capturing dump failed */
  3718. rval = QLA_FUNCTION_FAILED;
  3719. break;
  3720. } else
  3721. read_value = qla82xx_md_rw_32(ha,
  3722. crb_addr, 0, 0);
  3723. } while (1);
  3724. opcode &= ~QLA82XX_DBG_OPCODE_POLL;
  3725. }
  3726. if (opcode & QLA82XX_DBG_OPCODE_RDSTATE) {
  3727. if (crb_entry->crb_strd.state_index_a) {
  3728. index = crb_entry->crb_strd.state_index_a;
  3729. addr = tmplt_hdr->saved_state_array[index];
  3730. } else
  3731. addr = crb_addr;
  3732. read_value = qla82xx_md_rw_32(ha, addr, 0, 0);
  3733. index = crb_entry->crb_ctrl.state_index_v;
  3734. tmplt_hdr->saved_state_array[index] = read_value;
  3735. opcode &= ~QLA82XX_DBG_OPCODE_RDSTATE;
  3736. }
  3737. if (opcode & QLA82XX_DBG_OPCODE_WRSTATE) {
  3738. if (crb_entry->crb_strd.state_index_a) {
  3739. index = crb_entry->crb_strd.state_index_a;
  3740. addr = tmplt_hdr->saved_state_array[index];
  3741. } else
  3742. addr = crb_addr;
  3743. if (crb_entry->crb_ctrl.state_index_v) {
  3744. index = crb_entry->crb_ctrl.state_index_v;
  3745. read_value =
  3746. tmplt_hdr->saved_state_array[index];
  3747. } else
  3748. read_value = crb_entry->value_1;
  3749. qla82xx_md_rw_32(ha, addr, read_value, 1);
  3750. opcode &= ~QLA82XX_DBG_OPCODE_WRSTATE;
  3751. }
  3752. if (opcode & QLA82XX_DBG_OPCODE_MDSTATE) {
  3753. index = crb_entry->crb_ctrl.state_index_v;
  3754. read_value = tmplt_hdr->saved_state_array[index];
  3755. read_value <<= crb_entry->crb_ctrl.shl;
  3756. read_value >>= crb_entry->crb_ctrl.shr;
  3757. if (crb_entry->value_2)
  3758. read_value &= crb_entry->value_2;
  3759. read_value |= crb_entry->value_3;
  3760. read_value += crb_entry->value_1;
  3761. tmplt_hdr->saved_state_array[index] = read_value;
  3762. opcode &= ~QLA82XX_DBG_OPCODE_MDSTATE;
  3763. }
  3764. crb_addr += crb_entry->crb_strd.addr_stride;
  3765. }
  3766. return rval;
  3767. }
  3768. static void
  3769. qla82xx_minidump_process_rdocm(scsi_qla_host_t *vha,
  3770. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3771. {
  3772. struct qla_hw_data *ha = vha->hw;
  3773. uint32_t r_addr, r_stride, loop_cnt, i, r_value;
  3774. struct qla82xx_md_entry_rdocm *ocm_hdr;
  3775. uint32_t *data_ptr = *d_ptr;
  3776. ocm_hdr = (struct qla82xx_md_entry_rdocm *)entry_hdr;
  3777. r_addr = ocm_hdr->read_addr;
  3778. r_stride = ocm_hdr->read_addr_stride;
  3779. loop_cnt = ocm_hdr->op_count;
  3780. for (i = 0; i < loop_cnt; i++) {
  3781. r_value = RD_REG_DWORD((void *)(r_addr + ha->nx_pcibase));
  3782. *data_ptr++ = cpu_to_le32(r_value);
  3783. r_addr += r_stride;
  3784. }
  3785. *d_ptr = data_ptr;
  3786. }
  3787. static void
  3788. qla82xx_minidump_process_rdmux(scsi_qla_host_t *vha,
  3789. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3790. {
  3791. struct qla_hw_data *ha = vha->hw;
  3792. uint32_t r_addr, s_stride, s_addr, s_value, loop_cnt, i, r_value;
  3793. struct qla82xx_md_entry_mux *mux_hdr;
  3794. uint32_t *data_ptr = *d_ptr;
  3795. mux_hdr = (struct qla82xx_md_entry_mux *)entry_hdr;
  3796. r_addr = mux_hdr->read_addr;
  3797. s_addr = mux_hdr->select_addr;
  3798. s_stride = mux_hdr->select_value_stride;
  3799. s_value = mux_hdr->select_value;
  3800. loop_cnt = mux_hdr->op_count;
  3801. for (i = 0; i < loop_cnt; i++) {
  3802. qla82xx_md_rw_32(ha, s_addr, s_value, 1);
  3803. r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
  3804. *data_ptr++ = cpu_to_le32(s_value);
  3805. *data_ptr++ = cpu_to_le32(r_value);
  3806. s_value += s_stride;
  3807. }
  3808. *d_ptr = data_ptr;
  3809. }
  3810. static void
  3811. qla82xx_minidump_process_rdcrb(scsi_qla_host_t *vha,
  3812. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3813. {
  3814. struct qla_hw_data *ha = vha->hw;
  3815. uint32_t r_addr, r_stride, loop_cnt, i, r_value;
  3816. struct qla82xx_md_entry_crb *crb_hdr;
  3817. uint32_t *data_ptr = *d_ptr;
  3818. crb_hdr = (struct qla82xx_md_entry_crb *)entry_hdr;
  3819. r_addr = crb_hdr->addr;
  3820. r_stride = crb_hdr->crb_strd.addr_stride;
  3821. loop_cnt = crb_hdr->op_count;
  3822. for (i = 0; i < loop_cnt; i++) {
  3823. r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
  3824. *data_ptr++ = cpu_to_le32(r_addr);
  3825. *data_ptr++ = cpu_to_le32(r_value);
  3826. r_addr += r_stride;
  3827. }
  3828. *d_ptr = data_ptr;
  3829. }
  3830. static int
  3831. qla82xx_minidump_process_l2tag(scsi_qla_host_t *vha,
  3832. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3833. {
  3834. struct qla_hw_data *ha = vha->hw;
  3835. uint32_t addr, r_addr, c_addr, t_r_addr;
  3836. uint32_t i, k, loop_count, t_value, r_cnt, r_value;
  3837. unsigned long p_wait, w_time, p_mask;
  3838. uint32_t c_value_w, c_value_r;
  3839. struct qla82xx_md_entry_cache *cache_hdr;
  3840. int rval = QLA_FUNCTION_FAILED;
  3841. uint32_t *data_ptr = *d_ptr;
  3842. cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
  3843. loop_count = cache_hdr->op_count;
  3844. r_addr = cache_hdr->read_addr;
  3845. c_addr = cache_hdr->control_addr;
  3846. c_value_w = cache_hdr->cache_ctrl.write_value;
  3847. t_r_addr = cache_hdr->tag_reg_addr;
  3848. t_value = cache_hdr->addr_ctrl.init_tag_value;
  3849. r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
  3850. p_wait = cache_hdr->cache_ctrl.poll_wait;
  3851. p_mask = cache_hdr->cache_ctrl.poll_mask;
  3852. for (i = 0; i < loop_count; i++) {
  3853. qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
  3854. if (c_value_w)
  3855. qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
  3856. if (p_mask) {
  3857. w_time = jiffies + p_wait;
  3858. do {
  3859. c_value_r = qla82xx_md_rw_32(ha, c_addr, 0, 0);
  3860. if ((c_value_r & p_mask) == 0)
  3861. break;
  3862. else if (time_after_eq(jiffies, w_time)) {
  3863. /* capturing dump failed */
  3864. ql_dbg(ql_dbg_p3p, vha, 0xb032,
  3865. "c_value_r: 0x%x, poll_mask: 0x%lx, "
  3866. "w_time: 0x%lx\n",
  3867. c_value_r, p_mask, w_time);
  3868. return rval;
  3869. }
  3870. } while (1);
  3871. }
  3872. addr = r_addr;
  3873. for (k = 0; k < r_cnt; k++) {
  3874. r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
  3875. *data_ptr++ = cpu_to_le32(r_value);
  3876. addr += cache_hdr->read_ctrl.read_addr_stride;
  3877. }
  3878. t_value += cache_hdr->addr_ctrl.tag_value_stride;
  3879. }
  3880. *d_ptr = data_ptr;
  3881. return QLA_SUCCESS;
  3882. }
  3883. static void
  3884. qla82xx_minidump_process_l1cache(scsi_qla_host_t *vha,
  3885. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3886. {
  3887. struct qla_hw_data *ha = vha->hw;
  3888. uint32_t addr, r_addr, c_addr, t_r_addr;
  3889. uint32_t i, k, loop_count, t_value, r_cnt, r_value;
  3890. uint32_t c_value_w;
  3891. struct qla82xx_md_entry_cache *cache_hdr;
  3892. uint32_t *data_ptr = *d_ptr;
  3893. cache_hdr = (struct qla82xx_md_entry_cache *)entry_hdr;
  3894. loop_count = cache_hdr->op_count;
  3895. r_addr = cache_hdr->read_addr;
  3896. c_addr = cache_hdr->control_addr;
  3897. c_value_w = cache_hdr->cache_ctrl.write_value;
  3898. t_r_addr = cache_hdr->tag_reg_addr;
  3899. t_value = cache_hdr->addr_ctrl.init_tag_value;
  3900. r_cnt = cache_hdr->read_ctrl.read_addr_cnt;
  3901. for (i = 0; i < loop_count; i++) {
  3902. qla82xx_md_rw_32(ha, t_r_addr, t_value, 1);
  3903. qla82xx_md_rw_32(ha, c_addr, c_value_w, 1);
  3904. addr = r_addr;
  3905. for (k = 0; k < r_cnt; k++) {
  3906. r_value = qla82xx_md_rw_32(ha, addr, 0, 0);
  3907. *data_ptr++ = cpu_to_le32(r_value);
  3908. addr += cache_hdr->read_ctrl.read_addr_stride;
  3909. }
  3910. t_value += cache_hdr->addr_ctrl.tag_value_stride;
  3911. }
  3912. *d_ptr = data_ptr;
  3913. }
  3914. static void
  3915. qla82xx_minidump_process_queue(scsi_qla_host_t *vha,
  3916. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3917. {
  3918. struct qla_hw_data *ha = vha->hw;
  3919. uint32_t s_addr, r_addr;
  3920. uint32_t r_stride, r_value, r_cnt, qid = 0;
  3921. uint32_t i, k, loop_cnt;
  3922. struct qla82xx_md_entry_queue *q_hdr;
  3923. uint32_t *data_ptr = *d_ptr;
  3924. q_hdr = (struct qla82xx_md_entry_queue *)entry_hdr;
  3925. s_addr = q_hdr->select_addr;
  3926. r_cnt = q_hdr->rd_strd.read_addr_cnt;
  3927. r_stride = q_hdr->rd_strd.read_addr_stride;
  3928. loop_cnt = q_hdr->op_count;
  3929. for (i = 0; i < loop_cnt; i++) {
  3930. qla82xx_md_rw_32(ha, s_addr, qid, 1);
  3931. r_addr = q_hdr->read_addr;
  3932. for (k = 0; k < r_cnt; k++) {
  3933. r_value = qla82xx_md_rw_32(ha, r_addr, 0, 0);
  3934. *data_ptr++ = cpu_to_le32(r_value);
  3935. r_addr += r_stride;
  3936. }
  3937. qid += q_hdr->q_strd.queue_id_stride;
  3938. }
  3939. *d_ptr = data_ptr;
  3940. }
  3941. static void
  3942. qla82xx_minidump_process_rdrom(scsi_qla_host_t *vha,
  3943. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3944. {
  3945. struct qla_hw_data *ha = vha->hw;
  3946. uint32_t r_addr, r_value;
  3947. uint32_t i, loop_cnt;
  3948. struct qla82xx_md_entry_rdrom *rom_hdr;
  3949. uint32_t *data_ptr = *d_ptr;
  3950. rom_hdr = (struct qla82xx_md_entry_rdrom *)entry_hdr;
  3951. r_addr = rom_hdr->read_addr;
  3952. loop_cnt = rom_hdr->read_data_size/sizeof(uint32_t);
  3953. for (i = 0; i < loop_cnt; i++) {
  3954. qla82xx_md_rw_32(ha, MD_DIRECT_ROM_WINDOW,
  3955. (r_addr & 0xFFFF0000), 1);
  3956. r_value = qla82xx_md_rw_32(ha,
  3957. MD_DIRECT_ROM_READ_BASE +
  3958. (r_addr & 0x0000FFFF), 0, 0);
  3959. *data_ptr++ = cpu_to_le32(r_value);
  3960. r_addr += sizeof(uint32_t);
  3961. }
  3962. *d_ptr = data_ptr;
  3963. }
  3964. static int
  3965. qla82xx_minidump_process_rdmem(scsi_qla_host_t *vha,
  3966. qla82xx_md_entry_hdr_t *entry_hdr, uint32_t **d_ptr)
  3967. {
  3968. struct qla_hw_data *ha = vha->hw;
  3969. uint32_t r_addr, r_value, r_data;
  3970. uint32_t i, j, loop_cnt;
  3971. struct qla82xx_md_entry_rdmem *m_hdr;
  3972. unsigned long flags;
  3973. int rval = QLA_FUNCTION_FAILED;
  3974. uint32_t *data_ptr = *d_ptr;
  3975. m_hdr = (struct qla82xx_md_entry_rdmem *)entry_hdr;
  3976. r_addr = m_hdr->read_addr;
  3977. loop_cnt = m_hdr->read_data_size/16;
  3978. if (r_addr & 0xf) {
  3979. ql_log(ql_log_warn, vha, 0xb033,
  3980. "Read addr 0x%x not 16 bytes alligned\n", r_addr);
  3981. return rval;
  3982. }
  3983. if (m_hdr->read_data_size % 16) {
  3984. ql_log(ql_log_warn, vha, 0xb034,
  3985. "Read data[0x%x] not multiple of 16 bytes\n",
  3986. m_hdr->read_data_size);
  3987. return rval;
  3988. }
  3989. ql_dbg(ql_dbg_p3p, vha, 0xb035,
  3990. "[%s]: rdmem_addr: 0x%x, read_data_size: 0x%x, loop_cnt: 0x%x\n",
  3991. __func__, r_addr, m_hdr->read_data_size, loop_cnt);
  3992. write_lock_irqsave(&ha->hw_lock, flags);
  3993. for (i = 0; i < loop_cnt; i++) {
  3994. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_LO, r_addr, 1);
  3995. r_value = 0;
  3996. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_ADDR_HI, r_value, 1);
  3997. r_value = MIU_TA_CTL_ENABLE;
  3998. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
  3999. r_value = MIU_TA_CTL_START | MIU_TA_CTL_ENABLE;
  4000. qla82xx_md_rw_32(ha, MD_MIU_TEST_AGT_CTRL, r_value, 1);
  4001. for (j = 0; j < MAX_CTL_CHECK; j++) {
  4002. r_value = qla82xx_md_rw_32(ha,
  4003. MD_MIU_TEST_AGT_CTRL, 0, 0);
  4004. if ((r_value & MIU_TA_CTL_BUSY) == 0)
  4005. break;
  4006. }
  4007. if (j >= MAX_CTL_CHECK) {
  4008. printk_ratelimited(KERN_ERR
  4009. "failed to read through agent\n");
  4010. write_unlock_irqrestore(&ha->hw_lock, flags);
  4011. return rval;
  4012. }
  4013. for (j = 0; j < 4; j++) {
  4014. r_data = qla82xx_md_rw_32(ha,
  4015. MD_MIU_TEST_AGT_RDDATA[j], 0, 0);
  4016. *data_ptr++ = cpu_to_le32(r_data);
  4017. }
  4018. r_addr += 16;
  4019. }
  4020. write_unlock_irqrestore(&ha->hw_lock, flags);
  4021. *d_ptr = data_ptr;
  4022. return QLA_SUCCESS;
  4023. }
  4024. static int
  4025. qla82xx_validate_template_chksum(scsi_qla_host_t *vha)
  4026. {
  4027. struct qla_hw_data *ha = vha->hw;
  4028. uint64_t chksum = 0;
  4029. uint32_t *d_ptr = (uint32_t *)ha->md_tmplt_hdr;
  4030. int count = ha->md_template_size/sizeof(uint32_t);
  4031. while (count-- > 0)
  4032. chksum += *d_ptr++;
  4033. while (chksum >> 32)
  4034. chksum = (chksum & 0xFFFFFFFF) + (chksum >> 32);
  4035. return ~chksum;
  4036. }
  4037. static void
  4038. qla82xx_mark_entry_skipped(scsi_qla_host_t *vha,
  4039. qla82xx_md_entry_hdr_t *entry_hdr, int index)
  4040. {
  4041. entry_hdr->d_ctrl.driver_flags |= QLA82XX_DBG_SKIPPED_FLAG;
  4042. ql_dbg(ql_dbg_p3p, vha, 0xb036,
  4043. "Skipping entry[%d]: "
  4044. "ETYPE[0x%x]-ELEVEL[0x%x]\n",
  4045. index, entry_hdr->entry_type,
  4046. entry_hdr->d_ctrl.entry_capture_mask);
  4047. }
  4048. int
  4049. qla82xx_md_collect(scsi_qla_host_t *vha)
  4050. {
  4051. struct qla_hw_data *ha = vha->hw;
  4052. int no_entry_hdr = 0;
  4053. qla82xx_md_entry_hdr_t *entry_hdr;
  4054. struct qla82xx_md_template_hdr *tmplt_hdr;
  4055. uint32_t *data_ptr;
  4056. uint32_t total_data_size = 0, f_capture_mask, data_collected = 0;
  4057. int i = 0, rval = QLA_FUNCTION_FAILED;
  4058. tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
  4059. data_ptr = (uint32_t *)ha->md_dump;
  4060. if (ha->fw_dumped) {
  4061. ql_log(ql_log_info, vha, 0xb037,
  4062. "Firmware dump available to retrive\n");
  4063. goto md_failed;
  4064. }
  4065. ha->fw_dumped = 0;
  4066. if (!ha->md_tmplt_hdr || !ha->md_dump) {
  4067. ql_log(ql_log_warn, vha, 0xb038,
  4068. "Memory not allocated for minidump capture\n");
  4069. goto md_failed;
  4070. }
  4071. if (qla82xx_validate_template_chksum(vha)) {
  4072. ql_log(ql_log_info, vha, 0xb039,
  4073. "Template checksum validation error\n");
  4074. goto md_failed;
  4075. }
  4076. no_entry_hdr = tmplt_hdr->num_of_entries;
  4077. ql_dbg(ql_dbg_p3p, vha, 0xb03a,
  4078. "No of entry headers in Template: 0x%x\n", no_entry_hdr);
  4079. ql_dbg(ql_dbg_p3p, vha, 0xb03b,
  4080. "Capture Mask obtained: 0x%x\n", tmplt_hdr->capture_debug_level);
  4081. f_capture_mask = tmplt_hdr->capture_debug_level & 0xFF;
  4082. /* Validate whether required debug level is set */
  4083. if ((f_capture_mask & 0x3) != 0x3) {
  4084. ql_log(ql_log_warn, vha, 0xb03c,
  4085. "Minimum required capture mask[0x%x] level not set\n",
  4086. f_capture_mask);
  4087. goto md_failed;
  4088. }
  4089. tmplt_hdr->driver_capture_mask = ql2xmdcapmask;
  4090. tmplt_hdr->driver_info[0] = vha->host_no;
  4091. tmplt_hdr->driver_info[1] = (QLA_DRIVER_MAJOR_VER << 24) |
  4092. (QLA_DRIVER_MINOR_VER << 16) | (QLA_DRIVER_PATCH_VER << 8) |
  4093. QLA_DRIVER_BETA_VER;
  4094. total_data_size = ha->md_dump_size;
  4095. ql_dbg(ql_log_info, vha, 0xb03d,
  4096. "Total minidump data_size 0x%x to be captured\n", total_data_size);
  4097. /* Check whether template obtained is valid */
  4098. if (tmplt_hdr->entry_type != QLA82XX_TLHDR) {
  4099. ql_log(ql_log_warn, vha, 0xb04e,
  4100. "Bad template header entry type: 0x%x obtained\n",
  4101. tmplt_hdr->entry_type);
  4102. goto md_failed;
  4103. }
  4104. entry_hdr = (qla82xx_md_entry_hdr_t *) \
  4105. (((uint8_t *)ha->md_tmplt_hdr) + tmplt_hdr->first_entry_offset);
  4106. /* Walk through the entry headers */
  4107. for (i = 0; i < no_entry_hdr; i++) {
  4108. if (data_collected > total_data_size) {
  4109. ql_log(ql_log_warn, vha, 0xb03e,
  4110. "More MiniDump data collected: [0x%x]\n",
  4111. data_collected);
  4112. goto md_failed;
  4113. }
  4114. if (!(entry_hdr->d_ctrl.entry_capture_mask &
  4115. ql2xmdcapmask)) {
  4116. entry_hdr->d_ctrl.driver_flags |=
  4117. QLA82XX_DBG_SKIPPED_FLAG;
  4118. ql_dbg(ql_dbg_p3p, vha, 0xb03f,
  4119. "Skipping entry[%d]: "
  4120. "ETYPE[0x%x]-ELEVEL[0x%x]\n",
  4121. i, entry_hdr->entry_type,
  4122. entry_hdr->d_ctrl.entry_capture_mask);
  4123. goto skip_nxt_entry;
  4124. }
  4125. ql_dbg(ql_dbg_p3p, vha, 0xb040,
  4126. "[%s]: data ptr[%d]: %p, entry_hdr: %p\n"
  4127. "entry_type: 0x%x, captrue_mask: 0x%x\n",
  4128. __func__, i, data_ptr, entry_hdr,
  4129. entry_hdr->entry_type,
  4130. entry_hdr->d_ctrl.entry_capture_mask);
  4131. ql_dbg(ql_dbg_p3p, vha, 0xb041,
  4132. "Data collected: [0x%x], Dump size left:[0x%x]\n",
  4133. data_collected, (ha->md_dump_size - data_collected));
  4134. /* Decode the entry type and take
  4135. * required action to capture debug data */
  4136. switch (entry_hdr->entry_type) {
  4137. case QLA82XX_RDEND:
  4138. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4139. break;
  4140. case QLA82XX_CNTRL:
  4141. rval = qla82xx_minidump_process_control(vha,
  4142. entry_hdr, &data_ptr);
  4143. if (rval != QLA_SUCCESS) {
  4144. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4145. goto md_failed;
  4146. }
  4147. break;
  4148. case QLA82XX_RDCRB:
  4149. qla82xx_minidump_process_rdcrb(vha,
  4150. entry_hdr, &data_ptr);
  4151. break;
  4152. case QLA82XX_RDMEM:
  4153. rval = qla82xx_minidump_process_rdmem(vha,
  4154. entry_hdr, &data_ptr);
  4155. if (rval != QLA_SUCCESS) {
  4156. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4157. goto md_failed;
  4158. }
  4159. break;
  4160. case QLA82XX_BOARD:
  4161. case QLA82XX_RDROM:
  4162. qla82xx_minidump_process_rdrom(vha,
  4163. entry_hdr, &data_ptr);
  4164. break;
  4165. case QLA82XX_L2DTG:
  4166. case QLA82XX_L2ITG:
  4167. case QLA82XX_L2DAT:
  4168. case QLA82XX_L2INS:
  4169. rval = qla82xx_minidump_process_l2tag(vha,
  4170. entry_hdr, &data_ptr);
  4171. if (rval != QLA_SUCCESS) {
  4172. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4173. goto md_failed;
  4174. }
  4175. break;
  4176. case QLA82XX_L1DAT:
  4177. case QLA82XX_L1INS:
  4178. qla82xx_minidump_process_l1cache(vha,
  4179. entry_hdr, &data_ptr);
  4180. break;
  4181. case QLA82XX_RDOCM:
  4182. qla82xx_minidump_process_rdocm(vha,
  4183. entry_hdr, &data_ptr);
  4184. break;
  4185. case QLA82XX_RDMUX:
  4186. qla82xx_minidump_process_rdmux(vha,
  4187. entry_hdr, &data_ptr);
  4188. break;
  4189. case QLA82XX_QUEUE:
  4190. qla82xx_minidump_process_queue(vha,
  4191. entry_hdr, &data_ptr);
  4192. break;
  4193. case QLA82XX_RDNOP:
  4194. default:
  4195. qla82xx_mark_entry_skipped(vha, entry_hdr, i);
  4196. break;
  4197. }
  4198. ql_dbg(ql_dbg_p3p, vha, 0xb042,
  4199. "[%s]: data ptr[%d]: %p\n", __func__, i, data_ptr);
  4200. data_collected = (uint8_t *)data_ptr -
  4201. (uint8_t *)ha->md_dump;
  4202. skip_nxt_entry:
  4203. entry_hdr = (qla82xx_md_entry_hdr_t *) \
  4204. (((uint8_t *)entry_hdr) + entry_hdr->entry_size);
  4205. }
  4206. if (data_collected != total_data_size) {
  4207. ql_dbg(ql_log_warn, vha, 0xb043,
  4208. "MiniDump data mismatch: Data collected: [0x%x],"
  4209. "total_data_size:[0x%x]\n",
  4210. data_collected, total_data_size);
  4211. goto md_failed;
  4212. }
  4213. ql_log(ql_log_info, vha, 0xb044,
  4214. "Firmware dump saved to temp buffer (%ld/%p %ld/%p).\n",
  4215. vha->host_no, ha->md_tmplt_hdr, vha->host_no, ha->md_dump);
  4216. ha->fw_dumped = 1;
  4217. qla2x00_post_uevent_work(vha, QLA_UEVENT_CODE_FW_DUMP);
  4218. md_failed:
  4219. return rval;
  4220. }
  4221. int
  4222. qla82xx_md_alloc(scsi_qla_host_t *vha)
  4223. {
  4224. struct qla_hw_data *ha = vha->hw;
  4225. int i, k;
  4226. struct qla82xx_md_template_hdr *tmplt_hdr;
  4227. tmplt_hdr = (struct qla82xx_md_template_hdr *)ha->md_tmplt_hdr;
  4228. if (ql2xmdcapmask < 0x3 || ql2xmdcapmask > 0x7F) {
  4229. ql2xmdcapmask = tmplt_hdr->capture_debug_level & 0xFF;
  4230. ql_log(ql_log_info, vha, 0xb045,
  4231. "Forcing driver capture mask to firmware default capture mask: 0x%x.\n",
  4232. ql2xmdcapmask);
  4233. }
  4234. for (i = 0x2, k = 1; (i & QLA82XX_DEFAULT_CAP_MASK); i <<= 1, k++) {
  4235. if (i & ql2xmdcapmask)
  4236. ha->md_dump_size += tmplt_hdr->capture_size_array[k];
  4237. }
  4238. if (ha->md_dump) {
  4239. ql_log(ql_log_warn, vha, 0xb046,
  4240. "Firmware dump previously allocated.\n");
  4241. return 1;
  4242. }
  4243. ha->md_dump = vmalloc(ha->md_dump_size);
  4244. if (ha->md_dump == NULL) {
  4245. ql_log(ql_log_warn, vha, 0xb047,
  4246. "Unable to allocate memory for Minidump size "
  4247. "(0x%x).\n", ha->md_dump_size);
  4248. return 1;
  4249. }
  4250. return 0;
  4251. }
  4252. void
  4253. qla82xx_md_free(scsi_qla_host_t *vha)
  4254. {
  4255. struct qla_hw_data *ha = vha->hw;
  4256. /* Release the template header allocated */
  4257. if (ha->md_tmplt_hdr) {
  4258. ql_log(ql_log_info, vha, 0xb048,
  4259. "Free MiniDump template: %p, size (%d KB)\n",
  4260. ha->md_tmplt_hdr, ha->md_template_size / 1024);
  4261. dma_free_coherent(&ha->pdev->dev, ha->md_template_size,
  4262. ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
  4263. ha->md_tmplt_hdr = 0;
  4264. }
  4265. /* Release the template data buffer allocated */
  4266. if (ha->md_dump) {
  4267. ql_log(ql_log_info, vha, 0xb049,
  4268. "Free MiniDump memory: %p, size (%d KB)\n",
  4269. ha->md_dump, ha->md_dump_size / 1024);
  4270. vfree(ha->md_dump);
  4271. ha->md_dump_size = 0;
  4272. ha->md_dump = 0;
  4273. }
  4274. }
  4275. void
  4276. qla82xx_md_prep(scsi_qla_host_t *vha)
  4277. {
  4278. struct qla_hw_data *ha = vha->hw;
  4279. int rval;
  4280. /* Get Minidump template size */
  4281. rval = qla82xx_md_get_template_size(vha);
  4282. if (rval == QLA_SUCCESS) {
  4283. ql_log(ql_log_info, vha, 0xb04a,
  4284. "MiniDump Template size obtained (%d KB)\n",
  4285. ha->md_template_size / 1024);
  4286. /* Get Minidump template */
  4287. rval = qla82xx_md_get_template(vha);
  4288. if (rval == QLA_SUCCESS) {
  4289. ql_dbg(ql_dbg_p3p, vha, 0xb04b,
  4290. "MiniDump Template obtained\n");
  4291. /* Allocate memory for minidump */
  4292. rval = qla82xx_md_alloc(vha);
  4293. if (rval == QLA_SUCCESS)
  4294. ql_log(ql_log_info, vha, 0xb04c,
  4295. "MiniDump memory allocated (%d KB)\n",
  4296. ha->md_dump_size / 1024);
  4297. else {
  4298. ql_log(ql_log_info, vha, 0xb04d,
  4299. "Free MiniDump template: %p, size: (%d KB)\n",
  4300. ha->md_tmplt_hdr,
  4301. ha->md_template_size / 1024);
  4302. dma_free_coherent(&ha->pdev->dev,
  4303. ha->md_template_size,
  4304. ha->md_tmplt_hdr, ha->md_tmplt_hdr_dma);
  4305. ha->md_tmplt_hdr = 0;
  4306. }
  4307. }
  4308. }
  4309. }
  4310. int
  4311. qla82xx_beacon_on(struct scsi_qla_host *vha)
  4312. {
  4313. int rval;
  4314. struct qla_hw_data *ha = vha->hw;
  4315. qla82xx_idc_lock(ha);
  4316. rval = qla82xx_mbx_beacon_ctl(vha, 1);
  4317. if (rval) {
  4318. ql_log(ql_log_warn, vha, 0xb050,
  4319. "mbx set led config failed in %s\n", __func__);
  4320. goto exit;
  4321. }
  4322. ha->beacon_blink_led = 1;
  4323. exit:
  4324. qla82xx_idc_unlock(ha);
  4325. return rval;
  4326. }
  4327. int
  4328. qla82xx_beacon_off(struct scsi_qla_host *vha)
  4329. {
  4330. int rval;
  4331. struct qla_hw_data *ha = vha->hw;
  4332. qla82xx_idc_lock(ha);
  4333. rval = qla82xx_mbx_beacon_ctl(vha, 0);
  4334. if (rval) {
  4335. ql_log(ql_log_warn, vha, 0xb051,
  4336. "mbx set led config failed in %s\n", __func__);
  4337. goto exit;
  4338. }
  4339. ha->beacon_blink_led = 0;
  4340. exit:
  4341. qla82xx_idc_unlock(ha);
  4342. return rval;
  4343. }