mptscsih.c 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021
  1. /*
  2. * linux/drivers/message/fusion/mptscsih.c
  3. * High performance SCSI / Fibre Channel SCSI Host device driver.
  4. * For use with PCI chip/adapter(s):
  5. * LSIFC9xx/LSI409xx Fibre Channel
  6. * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
  7. *
  8. * Credits:
  9. * This driver would not exist if not for Alan Cox's development
  10. * of the linux i2o driver.
  11. *
  12. * A special thanks to Pamela Delaney (LSI Logic) for tons of work
  13. * and countless enhancements while adding support for the 1030
  14. * chip family. Pam has been instrumental in the development of
  15. * of the 2.xx.xx series fusion drivers, and her contributions are
  16. * far too numerous to hope to list in one place.
  17. *
  18. * A huge debt of gratitude is owed to David S. Miller (DaveM)
  19. * for fixing much of the stupid and broken stuff in the early
  20. * driver while porting to sparc64 platform. THANK YOU!
  21. *
  22. * (see mptbase.c)
  23. *
  24. * Copyright (c) 1999-2004 LSI Logic Corporation
  25. * Original author: Steven J. Ralston
  26. * (mailto:sjralston1@netscape.net)
  27. * (mailto:mpt_linux_developer@lsil.com)
  28. *
  29. * $Id: mptscsih.c,v 1.104 2002/12/03 21:26:34 pdelaney Exp $
  30. */
  31. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  32. /*
  33. This program is free software; you can redistribute it and/or modify
  34. it under the terms of the GNU General Public License as published by
  35. the Free Software Foundation; version 2 of the License.
  36. This program is distributed in the hope that it will be useful,
  37. but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. GNU General Public License for more details.
  40. NO WARRANTY
  41. THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  42. CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  43. LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  44. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  45. solely responsible for determining the appropriateness of using and
  46. distributing the Program and assumes all risks associated with its
  47. exercise of rights under this Agreement, including but not limited to
  48. the risks and costs of program errors, damage to or loss of data,
  49. programs or equipment, and unavailability or interruption of operations.
  50. DISCLAIMER OF LIABILITY
  51. NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  52. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53. DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  54. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  55. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  56. USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  57. HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  58. You should have received a copy of the GNU General Public License
  59. along with this program; if not, write to the Free Software
  60. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  61. */
  62. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  63. #include "linux_compat.h" /* linux-2.6 tweaks */
  64. #include <linux/module.h>
  65. #include <linux/kernel.h>
  66. #include <linux/init.h>
  67. #include <linux/errno.h>
  68. #include <linux/kdev_t.h>
  69. #include <linux/blkdev.h>
  70. #include <linux/delay.h> /* for mdelay */
  71. #include <linux/interrupt.h> /* needed for in_interrupt() proto */
  72. #include <linux/reboot.h> /* notifier code */
  73. #include <linux/sched.h>
  74. #include <linux/workqueue.h>
  75. #include <scsi/scsi.h>
  76. #include <scsi/scsi_cmnd.h>
  77. #include <scsi/scsi_device.h>
  78. #include <scsi/scsi_host.h>
  79. #include <scsi/scsi_tcq.h>
  80. #include "mptbase.h"
  81. #include "mptscsih.h"
  82. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  83. #define my_NAME "Fusion MPT SCSI Host driver"
  84. #define my_VERSION MPT_LINUX_VERSION_COMMON
  85. #define MYNAM "mptscsih"
  86. MODULE_AUTHOR(MODULEAUTHOR);
  87. MODULE_DESCRIPTION(my_NAME);
  88. MODULE_LICENSE("GPL");
  89. /* Command line args */
  90. static int mpt_dv = MPTSCSIH_DOMAIN_VALIDATION;
  91. MODULE_PARM(mpt_dv, "i");
  92. MODULE_PARM_DESC(mpt_dv, " DV Algorithm: enhanced=1, basic=0 (default=MPTSCSIH_DOMAIN_VALIDATION=1)");
  93. static int mpt_width = MPTSCSIH_MAX_WIDTH;
  94. MODULE_PARM(mpt_width, "i");
  95. MODULE_PARM_DESC(mpt_width, " Max Bus Width: wide=1, narrow=0 (default=MPTSCSIH_MAX_WIDTH=1)");
  96. static int mpt_factor = MPTSCSIH_MIN_SYNC;
  97. MODULE_PARM(mpt_factor, "h");
  98. MODULE_PARM_DESC(mpt_factor, " Min Sync Factor (default=MPTSCSIH_MIN_SYNC=0x08)");
  99. static int mpt_saf_te = MPTSCSIH_SAF_TE;
  100. MODULE_PARM(mpt_saf_te, "i");
  101. MODULE_PARM_DESC(mpt_saf_te, " Force enabling SEP Processor: enable=1 (default=MPTSCSIH_SAF_TE=0)");
  102. static int mpt_pq_filter = 0;
  103. MODULE_PARM(mpt_pq_filter, "i");
  104. MODULE_PARM_DESC(mpt_pq_filter, " Enable peripheral qualifier filter: enable=1 (default=0)");
  105. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  106. typedef struct _BIG_SENSE_BUF {
  107. u8 data[MPT_SENSE_BUFFER_ALLOC];
  108. } BIG_SENSE_BUF;
  109. #define MPT_SCANDV_GOOD (0x00000000) /* must be 0 */
  110. #define MPT_SCANDV_DID_RESET (0x00000001)
  111. #define MPT_SCANDV_SENSE (0x00000002)
  112. #define MPT_SCANDV_SOME_ERROR (0x00000004)
  113. #define MPT_SCANDV_SELECTION_TIMEOUT (0x00000008)
  114. #define MPT_SCANDV_ISSUE_SENSE (0x00000010)
  115. #define MPT_SCANDV_FALLBACK (0x00000020)
  116. #define MPT_SCANDV_MAX_RETRIES (10)
  117. #define MPT_ICFLAG_BUF_CAP 0x01 /* ReadBuffer Read Capacity format */
  118. #define MPT_ICFLAG_ECHO 0x02 /* ReadBuffer Echo buffer format */
  119. #define MPT_ICFLAG_PHYS_DISK 0x04 /* Any SCSI IO but do Phys Disk Format */
  120. #define MPT_ICFLAG_TAGGED_CMD 0x08 /* Do tagged IO */
  121. #define MPT_ICFLAG_DID_RESET 0x20 /* Bus Reset occurred with this command */
  122. #define MPT_ICFLAG_RESERVED 0x40 /* Reserved has been issued */
  123. typedef struct _internal_cmd {
  124. char *data; /* data pointer */
  125. dma_addr_t data_dma; /* data dma address */
  126. int size; /* transfer size */
  127. u8 cmd; /* SCSI Op Code */
  128. u8 bus; /* bus number */
  129. u8 id; /* SCSI ID (virtual) */
  130. u8 lun;
  131. u8 flags; /* Bit Field - See above */
  132. u8 physDiskNum; /* Phys disk number, -1 else */
  133. u8 rsvd2;
  134. u8 rsvd;
  135. } INTERNAL_CMD;
  136. typedef struct _negoparms {
  137. u8 width;
  138. u8 offset;
  139. u8 factor;
  140. u8 flags;
  141. } NEGOPARMS;
  142. typedef struct _dv_parameters {
  143. NEGOPARMS max;
  144. NEGOPARMS now;
  145. u8 cmd;
  146. u8 id;
  147. u16 pad1;
  148. } DVPARAMETERS;
  149. /*
  150. * Other private/forward protos...
  151. */
  152. static int mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
  153. static void mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq);
  154. static int mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
  155. static int mptscsih_AddSGE(MPT_ADAPTER *ioc, struct scsi_cmnd *SCpnt,
  156. SCSIIORequest_t *pReq, int req_idx);
  157. static void mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx);
  158. static void copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply);
  159. static int mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd);
  160. static int mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout );
  161. static u32 SCPNT_TO_LOOKUP_IDX(struct scsi_cmnd *sc);
  162. static int mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout);
  163. static int mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout);
  164. static int mptscsih_ioc_reset(MPT_ADAPTER *ioc, int post_reset);
  165. static int mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
  166. static void mptscsih_initTarget(MPT_SCSI_HOST *hd, int bus_id, int target_id, u8 lun, char *data, int dlen);
  167. static void mptscsih_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtDevice *target, char byte56);
  168. static void mptscsih_set_dvflags(MPT_SCSI_HOST *hd, SCSIIORequest_t *pReq);
  169. static void mptscsih_setDevicePage1Flags (u8 width, u8 factor, u8 offset, int *requestedPtr, int *configurationPtr, u8 flags);
  170. static void mptscsih_no_negotiate(MPT_SCSI_HOST *hd, int target_id);
  171. static int mptscsih_writeSDP1(MPT_SCSI_HOST *hd, int portnum, int target, int flags);
  172. static int mptscsih_writeIOCPage4(MPT_SCSI_HOST *hd, int target_id, int bus);
  173. static int mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *r);
  174. static void mptscsih_timer_expired(unsigned long data);
  175. static int mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *iocmd);
  176. static int mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, int portnum);
  177. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  178. static int mptscsih_do_raid(MPT_SCSI_HOST *hd, u8 action, INTERNAL_CMD *io);
  179. static void mptscsih_domainValidation(void *hd);
  180. static int mptscsih_is_phys_disk(MPT_ADAPTER *ioc, int id);
  181. static void mptscsih_qas_check(MPT_SCSI_HOST *hd, int id);
  182. static int mptscsih_doDv(MPT_SCSI_HOST *hd, int channel, int target);
  183. static void mptscsih_dv_parms(MPT_SCSI_HOST *hd, DVPARAMETERS *dv,void *pPage);
  184. static void mptscsih_fillbuf(char *buffer, int size, int index, int width);
  185. #endif
  186. /* module entry point */
  187. static int __init mptscsih_init (void);
  188. static void __exit mptscsih_exit (void);
  189. static int mptscsih_probe (struct pci_dev *, const struct pci_device_id *);
  190. static void mptscsih_remove(struct pci_dev *);
  191. static void mptscsih_shutdown(struct device *);
  192. #ifdef CONFIG_PM
  193. static int mptscsih_suspend(struct pci_dev *pdev, pm_message_t state);
  194. static int mptscsih_resume(struct pci_dev *pdev);
  195. #endif
  196. /*
  197. * Private data...
  198. */
  199. static int mpt_scsi_hosts = 0;
  200. static int ScsiDoneCtx = -1;
  201. static int ScsiTaskCtx = -1;
  202. static int ScsiScanDvCtx = -1; /* Used only for bus scan and dv */
  203. #define SNS_LEN(scp) sizeof((scp)->sense_buffer)
  204. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  205. /*
  206. * Domain Validation task structure
  207. */
  208. static DEFINE_SPINLOCK(dvtaskQ_lock);
  209. static int dvtaskQ_active = 0;
  210. static int dvtaskQ_release = 0;
  211. static struct work_struct mptscsih_dvTask;
  212. #endif
  213. /*
  214. * Wait Queue setup
  215. */
  216. static DECLARE_WAIT_QUEUE_HEAD (scandv_waitq);
  217. static int scandv_wait_done = 1;
  218. /* Driver command line structure
  219. */
  220. static struct scsi_host_template driver_template;
  221. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  222. /**
  223. * mptscsih_add_sge - Place a simple SGE at address pAddr.
  224. * @pAddr: virtual address for SGE
  225. * @flagslength: SGE flags and data transfer length
  226. * @dma_addr: Physical address
  227. *
  228. * This routine places a MPT request frame back on the MPT adapter's
  229. * FreeQ.
  230. */
  231. static inline void
  232. mptscsih_add_sge(char *pAddr, u32 flagslength, dma_addr_t dma_addr)
  233. {
  234. if (sizeof(dma_addr_t) == sizeof(u64)) {
  235. SGESimple64_t *pSge = (SGESimple64_t *) pAddr;
  236. u32 tmp = dma_addr & 0xFFFFFFFF;
  237. pSge->FlagsLength = cpu_to_le32(flagslength);
  238. pSge->Address.Low = cpu_to_le32(tmp);
  239. tmp = (u32) ((u64)dma_addr >> 32);
  240. pSge->Address.High = cpu_to_le32(tmp);
  241. } else {
  242. SGESimple32_t *pSge = (SGESimple32_t *) pAddr;
  243. pSge->FlagsLength = cpu_to_le32(flagslength);
  244. pSge->Address = cpu_to_le32(dma_addr);
  245. }
  246. } /* mptscsih_add_sge() */
  247. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  248. /**
  249. * mptscsih_add_chain - Place a chain SGE at address pAddr.
  250. * @pAddr: virtual address for SGE
  251. * @next: nextChainOffset value (u32's)
  252. * @length: length of next SGL segment
  253. * @dma_addr: Physical address
  254. *
  255. * This routine places a MPT request frame back on the MPT adapter's
  256. * FreeQ.
  257. */
  258. static inline void
  259. mptscsih_add_chain(char *pAddr, u8 next, u16 length, dma_addr_t dma_addr)
  260. {
  261. if (sizeof(dma_addr_t) == sizeof(u64)) {
  262. SGEChain64_t *pChain = (SGEChain64_t *) pAddr;
  263. u32 tmp = dma_addr & 0xFFFFFFFF;
  264. pChain->Length = cpu_to_le16(length);
  265. pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
  266. pChain->NextChainOffset = next;
  267. pChain->Address.Low = cpu_to_le32(tmp);
  268. tmp = (u32) ((u64)dma_addr >> 32);
  269. pChain->Address.High = cpu_to_le32(tmp);
  270. } else {
  271. SGEChain32_t *pChain = (SGEChain32_t *) pAddr;
  272. pChain->Length = cpu_to_le16(length);
  273. pChain->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT | mpt_addr_size();
  274. pChain->NextChainOffset = next;
  275. pChain->Address = cpu_to_le32(dma_addr);
  276. }
  277. } /* mptscsih_add_chain() */
  278. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  279. /*
  280. * mptscsih_getFreeChainBuffer - Function to get a free chain
  281. * from the MPT_SCSI_HOST FreeChainQ.
  282. * @ioc: Pointer to MPT_ADAPTER structure
  283. * @req_idx: Index of the SCSI IO request frame. (output)
  284. *
  285. * return SUCCESS or FAILED
  286. */
  287. static inline int
  288. mptscsih_getFreeChainBuffer(MPT_ADAPTER *ioc, int *retIndex)
  289. {
  290. MPT_FRAME_HDR *chainBuf;
  291. unsigned long flags;
  292. int rc;
  293. int chain_idx;
  294. dsgprintk((MYIOC_s_INFO_FMT "getFreeChainBuffer called\n",
  295. ioc->name));
  296. spin_lock_irqsave(&ioc->FreeQlock, flags);
  297. if (!list_empty(&ioc->FreeChainQ)) {
  298. int offset;
  299. chainBuf = list_entry(ioc->FreeChainQ.next, MPT_FRAME_HDR,
  300. u.frame.linkage.list);
  301. list_del(&chainBuf->u.frame.linkage.list);
  302. offset = (u8 *)chainBuf - (u8 *)ioc->ChainBuffer;
  303. chain_idx = offset / ioc->req_sz;
  304. rc = SUCCESS;
  305. dsgprintk((MYIOC_s_INFO_FMT "getFreeChainBuffer (index %d), got buf=%p\n",
  306. ioc->name, *retIndex, chainBuf));
  307. } else {
  308. rc = FAILED;
  309. chain_idx = MPT_HOST_NO_CHAIN;
  310. dfailprintk((MYIOC_s_ERR_FMT "getFreeChainBuffer failed\n",
  311. ioc->name));
  312. }
  313. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  314. *retIndex = chain_idx;
  315. return rc;
  316. } /* mptscsih_getFreeChainBuffer() */
  317. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  318. /*
  319. * mptscsih_AddSGE - Add a SGE (plus chain buffers) to the
  320. * SCSIIORequest_t Message Frame.
  321. * @ioc: Pointer to MPT_ADAPTER structure
  322. * @SCpnt: Pointer to scsi_cmnd structure
  323. * @pReq: Pointer to SCSIIORequest_t structure
  324. *
  325. * Returns ...
  326. */
  327. static int
  328. mptscsih_AddSGE(MPT_ADAPTER *ioc, struct scsi_cmnd *SCpnt,
  329. SCSIIORequest_t *pReq, int req_idx)
  330. {
  331. char *psge;
  332. char *chainSge;
  333. struct scatterlist *sg;
  334. int frm_sz;
  335. int sges_left, sg_done;
  336. int chain_idx = MPT_HOST_NO_CHAIN;
  337. int sgeOffset;
  338. int numSgeSlots, numSgeThisFrame;
  339. u32 sgflags, sgdir, thisxfer = 0;
  340. int chain_dma_off = 0;
  341. int newIndex;
  342. int ii;
  343. dma_addr_t v2;
  344. u32 RequestNB;
  345. sgdir = le32_to_cpu(pReq->Control) & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK;
  346. if (sgdir == MPI_SCSIIO_CONTROL_WRITE) {
  347. sgdir = MPT_TRANSFER_HOST_TO_IOC;
  348. } else {
  349. sgdir = MPT_TRANSFER_IOC_TO_HOST;
  350. }
  351. psge = (char *) &pReq->SGL;
  352. frm_sz = ioc->req_sz;
  353. /* Map the data portion, if any.
  354. * sges_left = 0 if no data transfer.
  355. */
  356. if ( (sges_left = SCpnt->use_sg) ) {
  357. sges_left = pci_map_sg(ioc->pcidev,
  358. (struct scatterlist *) SCpnt->request_buffer,
  359. SCpnt->use_sg,
  360. SCpnt->sc_data_direction);
  361. if (sges_left == 0)
  362. return FAILED;
  363. } else if (SCpnt->request_bufflen) {
  364. SCpnt->SCp.dma_handle = pci_map_single(ioc->pcidev,
  365. SCpnt->request_buffer,
  366. SCpnt->request_bufflen,
  367. SCpnt->sc_data_direction);
  368. dsgprintk((MYIOC_s_INFO_FMT "SG: non-SG for %p, len=%d\n",
  369. ioc->name, SCpnt, SCpnt->request_bufflen));
  370. mptscsih_add_sge((char *) &pReq->SGL,
  371. 0xD1000000|MPT_SGE_FLAGS_ADDRESSING|sgdir|SCpnt->request_bufflen,
  372. SCpnt->SCp.dma_handle);
  373. return SUCCESS;
  374. }
  375. /* Handle the SG case.
  376. */
  377. sg = (struct scatterlist *) SCpnt->request_buffer;
  378. sg_done = 0;
  379. sgeOffset = sizeof(SCSIIORequest_t) - sizeof(SGE_IO_UNION);
  380. chainSge = NULL;
  381. /* Prior to entering this loop - the following must be set
  382. * current MF: sgeOffset (bytes)
  383. * chainSge (Null if original MF is not a chain buffer)
  384. * sg_done (num SGE done for this MF)
  385. */
  386. nextSGEset:
  387. numSgeSlots = ((frm_sz - sgeOffset) / (sizeof(u32) + sizeof(dma_addr_t)) );
  388. numSgeThisFrame = (sges_left < numSgeSlots) ? sges_left : numSgeSlots;
  389. sgflags = MPT_SGE_FLAGS_SIMPLE_ELEMENT | MPT_SGE_FLAGS_ADDRESSING | sgdir;
  390. /* Get first (num - 1) SG elements
  391. * Skip any SG entries with a length of 0
  392. * NOTE: at finish, sg and psge pointed to NEXT data/location positions
  393. */
  394. for (ii=0; ii < (numSgeThisFrame-1); ii++) {
  395. thisxfer = sg_dma_len(sg);
  396. if (thisxfer == 0) {
  397. sg ++; /* Get next SG element from the OS */
  398. sg_done++;
  399. continue;
  400. }
  401. v2 = sg_dma_address(sg);
  402. mptscsih_add_sge(psge, sgflags | thisxfer, v2);
  403. sg++; /* Get next SG element from the OS */
  404. psge += (sizeof(u32) + sizeof(dma_addr_t));
  405. sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
  406. sg_done++;
  407. }
  408. if (numSgeThisFrame == sges_left) {
  409. /* Add last element, end of buffer and end of list flags.
  410. */
  411. sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT |
  412. MPT_SGE_FLAGS_END_OF_BUFFER |
  413. MPT_SGE_FLAGS_END_OF_LIST;
  414. /* Add last SGE and set termination flags.
  415. * Note: Last SGE may have a length of 0 - which should be ok.
  416. */
  417. thisxfer = sg_dma_len(sg);
  418. v2 = sg_dma_address(sg);
  419. mptscsih_add_sge(psge, sgflags | thisxfer, v2);
  420. /*
  421. sg++;
  422. psge += (sizeof(u32) + sizeof(dma_addr_t));
  423. */
  424. sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
  425. sg_done++;
  426. if (chainSge) {
  427. /* The current buffer is a chain buffer,
  428. * but there is not another one.
  429. * Update the chain element
  430. * Offset and Length fields.
  431. */
  432. mptscsih_add_chain((char *)chainSge, 0, sgeOffset, ioc->ChainBufferDMA + chain_dma_off);
  433. } else {
  434. /* The current buffer is the original MF
  435. * and there is no Chain buffer.
  436. */
  437. pReq->ChainOffset = 0;
  438. RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor) + 1) & 0x03;
  439. dsgprintk((MYIOC_s_ERR_FMT
  440. "Single Buffer RequestNB=%x, sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
  441. ioc->RequestNB[req_idx] = RequestNB;
  442. }
  443. } else {
  444. /* At least one chain buffer is needed.
  445. * Complete the first MF
  446. * - last SGE element, set the LastElement bit
  447. * - set ChainOffset (words) for orig MF
  448. * (OR finish previous MF chain buffer)
  449. * - update MFStructPtr ChainIndex
  450. * - Populate chain element
  451. * Also
  452. * Loop until done.
  453. */
  454. dsgprintk((MYIOC_s_INFO_FMT "SG: Chain Required! sg done %d\n",
  455. ioc->name, sg_done));
  456. /* Set LAST_ELEMENT flag for last non-chain element
  457. * in the buffer. Since psge points at the NEXT
  458. * SGE element, go back one SGE element, update the flags
  459. * and reset the pointer. (Note: sgflags & thisxfer are already
  460. * set properly).
  461. */
  462. if (sg_done) {
  463. u32 *ptmp = (u32 *) (psge - (sizeof(u32) + sizeof(dma_addr_t)));
  464. sgflags = le32_to_cpu(*ptmp);
  465. sgflags |= MPT_SGE_FLAGS_LAST_ELEMENT;
  466. *ptmp = cpu_to_le32(sgflags);
  467. }
  468. if (chainSge) {
  469. /* The current buffer is a chain buffer.
  470. * chainSge points to the previous Chain Element.
  471. * Update its chain element Offset and Length (must
  472. * include chain element size) fields.
  473. * Old chain element is now complete.
  474. */
  475. u8 nextChain = (u8) (sgeOffset >> 2);
  476. sgeOffset += (sizeof(u32) + sizeof(dma_addr_t));
  477. mptscsih_add_chain((char *)chainSge, nextChain, sgeOffset, ioc->ChainBufferDMA + chain_dma_off);
  478. } else {
  479. /* The original MF buffer requires a chain buffer -
  480. * set the offset.
  481. * Last element in this MF is a chain element.
  482. */
  483. pReq->ChainOffset = (u8) (sgeOffset >> 2);
  484. RequestNB = (((sgeOffset - 1) >> ioc->NBShiftFactor) + 1) & 0x03;
  485. dsgprintk((MYIOC_s_ERR_FMT "Chain Buffer Needed, RequestNB=%x sgeOffset=%d\n", ioc->name, RequestNB, sgeOffset));
  486. ioc->RequestNB[req_idx] = RequestNB;
  487. }
  488. sges_left -= sg_done;
  489. /* NOTE: psge points to the beginning of the chain element
  490. * in current buffer. Get a chain buffer.
  491. */
  492. dsgprintk((MYIOC_s_INFO_FMT
  493. "calling getFreeChainBuffer SCSI cmd=%02x (%p)\n",
  494. ioc->name, pReq->CDB[0], SCpnt));
  495. if ((mptscsih_getFreeChainBuffer(ioc, &newIndex)) == FAILED)
  496. return FAILED;
  497. /* Update the tracking arrays.
  498. * If chainSge == NULL, update ReqToChain, else ChainToChain
  499. */
  500. if (chainSge) {
  501. ioc->ChainToChain[chain_idx] = newIndex;
  502. } else {
  503. ioc->ReqToChain[req_idx] = newIndex;
  504. }
  505. chain_idx = newIndex;
  506. chain_dma_off = ioc->req_sz * chain_idx;
  507. /* Populate the chainSGE for the current buffer.
  508. * - Set chain buffer pointer to psge and fill
  509. * out the Address and Flags fields.
  510. */
  511. chainSge = (char *) psge;
  512. dsgprintk((KERN_INFO " Current buff @ %p (index 0x%x)",
  513. psge, req_idx));
  514. /* Start the SGE for the next buffer
  515. */
  516. psge = (char *) (ioc->ChainBuffer + chain_dma_off);
  517. sgeOffset = 0;
  518. sg_done = 0;
  519. dsgprintk((KERN_INFO " Chain buff @ %p (index 0x%x)\n",
  520. psge, chain_idx));
  521. /* Start the SGE for the next buffer
  522. */
  523. goto nextSGEset;
  524. }
  525. return SUCCESS;
  526. } /* mptscsih_AddSGE() */
  527. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  528. /*
  529. * mptscsih_io_done - Main SCSI IO callback routine registered to
  530. * Fusion MPT (base) driver
  531. * @ioc: Pointer to MPT_ADAPTER structure
  532. * @mf: Pointer to original MPT request frame
  533. * @r: Pointer to MPT reply frame (NULL if TurboReply)
  534. *
  535. * This routine is called from mpt.c::mpt_interrupt() at the completion
  536. * of any SCSI IO request.
  537. * This routine is registered with the Fusion MPT (base) driver at driver
  538. * load/init time via the mpt_register() API call.
  539. *
  540. * Returns 1 indicating alloc'd request frame ptr should be freed.
  541. */
  542. static int
  543. mptscsih_io_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  544. {
  545. struct scsi_cmnd *sc;
  546. MPT_SCSI_HOST *hd;
  547. SCSIIORequest_t *pScsiReq;
  548. SCSIIOReply_t *pScsiReply;
  549. u16 req_idx;
  550. hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
  551. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  552. sc = hd->ScsiLookup[req_idx];
  553. if (sc == NULL) {
  554. MPIHeader_t *hdr = (MPIHeader_t *)mf;
  555. /* Remark: writeSDP1 will use the ScsiDoneCtx
  556. * If a SCSI I/O cmd, device disabled by OS and
  557. * completion done. Cannot touch sc struct. Just free mem.
  558. */
  559. if (hdr->Function == MPI_FUNCTION_SCSI_IO_REQUEST)
  560. printk(MYIOC_s_ERR_FMT "NULL ScsiCmd ptr!\n",
  561. ioc->name);
  562. mptscsih_freeChainBuffers(ioc, req_idx);
  563. return 1;
  564. }
  565. dmfprintk((MYIOC_s_INFO_FMT
  566. "ScsiDone (mf=%p,mr=%p,sc=%p,idx=%d)\n",
  567. ioc->name, mf, mr, sc, req_idx));
  568. sc->result = DID_OK << 16; /* Set default reply as OK */
  569. pScsiReq = (SCSIIORequest_t *) mf;
  570. pScsiReply = (SCSIIOReply_t *) mr;
  571. if (pScsiReply == NULL) {
  572. /* special context reply handling */
  573. ;
  574. } else {
  575. u32 xfer_cnt;
  576. u16 status;
  577. u8 scsi_state, scsi_status;
  578. status = le16_to_cpu(pScsiReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  579. scsi_state = pScsiReply->SCSIState;
  580. scsi_status = pScsiReply->SCSIStatus;
  581. xfer_cnt = le32_to_cpu(pScsiReply->TransferCount);
  582. sc->resid = sc->request_bufflen - xfer_cnt;
  583. dreplyprintk((KERN_NOTICE "Reply ha=%d id=%d lun=%d:\n"
  584. "IOCStatus=%04xh SCSIState=%02xh SCSIStatus=%02xh\n"
  585. "resid=%d bufflen=%d xfer_cnt=%d\n",
  586. ioc->id, pScsiReq->TargetID, pScsiReq->LUN[1],
  587. status, scsi_state, scsi_status, sc->resid,
  588. sc->request_bufflen, xfer_cnt));
  589. if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID)
  590. copy_sense_data(sc, hd, mf, pScsiReply);
  591. /*
  592. * Look for + dump FCP ResponseInfo[]!
  593. */
  594. if (scsi_state & MPI_SCSI_STATE_RESPONSE_INFO_VALID) {
  595. printk(KERN_NOTICE " FCP_ResponseInfo=%08xh\n",
  596. le32_to_cpu(pScsiReply->ResponseInfo));
  597. }
  598. switch(status) {
  599. case MPI_IOCSTATUS_BUSY: /* 0x0002 */
  600. /* CHECKME!
  601. * Maybe: DRIVER_BUSY | SUGGEST_RETRY | DID_SOFT_ERROR (retry)
  602. * But not: DID_BUS_BUSY lest one risk
  603. * killing interrupt handler:-(
  604. */
  605. sc->result = SAM_STAT_BUSY;
  606. break;
  607. case MPI_IOCSTATUS_SCSI_INVALID_BUS: /* 0x0041 */
  608. case MPI_IOCSTATUS_SCSI_INVALID_TARGETID: /* 0x0042 */
  609. sc->result = DID_BAD_TARGET << 16;
  610. break;
  611. case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE: /* 0x0043 */
  612. /* Spoof to SCSI Selection Timeout! */
  613. sc->result = DID_NO_CONNECT << 16;
  614. if (hd->sel_timeout[pScsiReq->TargetID] < 0xFFFF)
  615. hd->sel_timeout[pScsiReq->TargetID]++;
  616. break;
  617. case MPI_IOCSTATUS_SCSI_TASK_TERMINATED: /* 0x0048 */
  618. case MPI_IOCSTATUS_SCSI_IOC_TERMINATED: /* 0x004B */
  619. case MPI_IOCSTATUS_SCSI_EXT_TERMINATED: /* 0x004C */
  620. /* Linux handles an unsolicited DID_RESET better
  621. * than an unsolicited DID_ABORT.
  622. */
  623. sc->result = DID_RESET << 16;
  624. /* GEM Workaround. */
  625. if (ioc->bus_type == SCSI)
  626. mptscsih_no_negotiate(hd, sc->device->id);
  627. break;
  628. case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH: /* 0x0049 */
  629. if ( xfer_cnt >= sc->underflow ) {
  630. /* Sufficient data transfer occurred */
  631. sc->result = (DID_OK << 16) | scsi_status;
  632. } else if ( xfer_cnt == 0 ) {
  633. /* A CRC Error causes this condition; retry */
  634. sc->result = (DRIVER_SENSE << 24) | (DID_OK << 16) |
  635. (CHECK_CONDITION << 1);
  636. sc->sense_buffer[0] = 0x70;
  637. sc->sense_buffer[2] = NO_SENSE;
  638. sc->sense_buffer[12] = 0;
  639. sc->sense_buffer[13] = 0;
  640. } else {
  641. sc->result = DID_SOFT_ERROR << 16;
  642. }
  643. dreplyprintk((KERN_NOTICE "RESIDUAL_MISMATCH: result=%x on id=%d\n", sc->result, sc->target));
  644. break;
  645. case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN: /* 0x0045 */
  646. /*
  647. * Do upfront check for valid SenseData and give it
  648. * precedence!
  649. */
  650. sc->result = (DID_OK << 16) | scsi_status;
  651. if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  652. /* Have already saved the status and sense data
  653. */
  654. ;
  655. } else {
  656. if (xfer_cnt < sc->underflow) {
  657. sc->result = DID_SOFT_ERROR << 16;
  658. }
  659. if (scsi_state & (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)) {
  660. /* What to do?
  661. */
  662. sc->result = DID_SOFT_ERROR << 16;
  663. }
  664. else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
  665. /* Not real sure here either... */
  666. sc->result = DID_RESET << 16;
  667. }
  668. }
  669. dreplyprintk((KERN_NOTICE " sc->underflow={report ERR if < %02xh bytes xfer'd}\n",
  670. sc->underflow));
  671. dreplyprintk((KERN_NOTICE " ActBytesXferd=%02xh\n", xfer_cnt));
  672. /* Report Queue Full
  673. */
  674. if (scsi_status == MPI_SCSI_STATUS_TASK_SET_FULL)
  675. mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
  676. break;
  677. case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR: /* 0x0040 */
  678. case MPI_IOCSTATUS_SUCCESS: /* 0x0000 */
  679. scsi_status = pScsiReply->SCSIStatus;
  680. sc->result = (DID_OK << 16) | scsi_status;
  681. if (scsi_state == 0) {
  682. ;
  683. } else if (scsi_state & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  684. /*
  685. * If running against circa 200003dd 909 MPT f/w,
  686. * may get this (AUTOSENSE_VALID) for actual TASK_SET_FULL
  687. * (QUEUE_FULL) returned from device! --> get 0x0000?128
  688. * and with SenseBytes set to 0.
  689. */
  690. if (pScsiReply->SCSIStatus == MPI_SCSI_STATUS_TASK_SET_FULL)
  691. mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
  692. }
  693. else if (scsi_state &
  694. (MPI_SCSI_STATE_AUTOSENSE_FAILED | MPI_SCSI_STATE_NO_SCSI_STATUS)
  695. ) {
  696. /*
  697. * What to do?
  698. */
  699. sc->result = DID_SOFT_ERROR << 16;
  700. }
  701. else if (scsi_state & MPI_SCSI_STATE_TERMINATED) {
  702. /* Not real sure here either... */
  703. sc->result = DID_RESET << 16;
  704. }
  705. else if (scsi_state & MPI_SCSI_STATE_QUEUE_TAG_REJECTED) {
  706. /* Device Inq. data indicates that it supports
  707. * QTags, but rejects QTag messages.
  708. * This command completed OK.
  709. *
  710. * Not real sure here either so do nothing... */
  711. }
  712. if (sc->result == MPI_SCSI_STATUS_TASK_SET_FULL)
  713. mptscsih_report_queue_full(sc, pScsiReply, pScsiReq);
  714. /* Add handling of:
  715. * Reservation Conflict, Busy,
  716. * Command Terminated, CHECK
  717. */
  718. break;
  719. case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR: /* 0x0047 */
  720. sc->result = DID_SOFT_ERROR << 16;
  721. break;
  722. case MPI_IOCSTATUS_INVALID_FUNCTION: /* 0x0001 */
  723. case MPI_IOCSTATUS_INVALID_SGL: /* 0x0003 */
  724. case MPI_IOCSTATUS_INTERNAL_ERROR: /* 0x0004 */
  725. case MPI_IOCSTATUS_RESERVED: /* 0x0005 */
  726. case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES: /* 0x0006 */
  727. case MPI_IOCSTATUS_INVALID_FIELD: /* 0x0007 */
  728. case MPI_IOCSTATUS_INVALID_STATE: /* 0x0008 */
  729. case MPI_IOCSTATUS_SCSI_DATA_OVERRUN: /* 0x0044 */
  730. case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR: /* 0x0046 */
  731. case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED: /* 0x004A */
  732. default:
  733. /*
  734. * What to do?
  735. */
  736. sc->result = DID_SOFT_ERROR << 16;
  737. break;
  738. } /* switch(status) */
  739. dreplyprintk((KERN_NOTICE " sc->result is %08xh\n", sc->result));
  740. } /* end of address reply case */
  741. /* Unmap the DMA buffers, if any. */
  742. if (sc->use_sg) {
  743. pci_unmap_sg(ioc->pcidev, (struct scatterlist *) sc->request_buffer,
  744. sc->use_sg, sc->sc_data_direction);
  745. } else if (sc->request_bufflen) {
  746. pci_unmap_single(ioc->pcidev, sc->SCp.dma_handle,
  747. sc->request_bufflen, sc->sc_data_direction);
  748. }
  749. hd->ScsiLookup[req_idx] = NULL;
  750. sc->scsi_done(sc); /* Issue the command callback */
  751. /* Free Chain buffers */
  752. mptscsih_freeChainBuffers(ioc, req_idx);
  753. return 1;
  754. }
  755. /*
  756. * mptscsih_flush_running_cmds - For each command found, search
  757. * Scsi_Host instance taskQ and reply to OS.
  758. * Called only if recovering from a FW reload.
  759. * @hd: Pointer to a SCSI HOST structure
  760. *
  761. * Returns: None.
  762. *
  763. * Must be called while new I/Os are being queued.
  764. */
  765. static void
  766. mptscsih_flush_running_cmds(MPT_SCSI_HOST *hd)
  767. {
  768. MPT_ADAPTER *ioc = hd->ioc;
  769. struct scsi_cmnd *SCpnt;
  770. MPT_FRAME_HDR *mf;
  771. int ii;
  772. int max = ioc->req_depth;
  773. dprintk((KERN_INFO MYNAM ": flush_ScsiLookup called\n"));
  774. for (ii= 0; ii < max; ii++) {
  775. if ((SCpnt = hd->ScsiLookup[ii]) != NULL) {
  776. /* Command found.
  777. */
  778. /* Null ScsiLookup index
  779. */
  780. hd->ScsiLookup[ii] = NULL;
  781. mf = MPT_INDEX_2_MFPTR(ioc, ii);
  782. dmfprintk(( "flush: ScsiDone (mf=%p,sc=%p)\n",
  783. mf, SCpnt));
  784. /* Set status, free OS resources (SG DMA buffers)
  785. * Do OS callback
  786. * Free driver resources (chain, msg buffers)
  787. */
  788. if (scsi_device_online(SCpnt->device)) {
  789. if (SCpnt->use_sg) {
  790. pci_unmap_sg(ioc->pcidev,
  791. (struct scatterlist *) SCpnt->request_buffer,
  792. SCpnt->use_sg,
  793. SCpnt->sc_data_direction);
  794. } else if (SCpnt->request_bufflen) {
  795. pci_unmap_single(ioc->pcidev,
  796. SCpnt->SCp.dma_handle,
  797. SCpnt->request_bufflen,
  798. SCpnt->sc_data_direction);
  799. }
  800. }
  801. SCpnt->result = DID_RESET << 16;
  802. SCpnt->host_scribble = NULL;
  803. /* Free Chain buffers */
  804. mptscsih_freeChainBuffers(ioc, ii);
  805. /* Free Message frames */
  806. mpt_free_msg_frame(ioc, mf);
  807. SCpnt->scsi_done(SCpnt); /* Issue the command callback */
  808. }
  809. }
  810. return;
  811. }
  812. /*
  813. * mptscsih_search_running_cmds - Delete any commands associated
  814. * with the specified target and lun. Function called only
  815. * when a lun is disable by mid-layer.
  816. * Do NOT access the referenced scsi_cmnd structure or
  817. * members. Will cause either a paging or NULL ptr error.
  818. * @hd: Pointer to a SCSI HOST structure
  819. * @target: target id
  820. * @lun: lun
  821. *
  822. * Returns: None.
  823. *
  824. * Called from slave_destroy.
  825. */
  826. static void
  827. mptscsih_search_running_cmds(MPT_SCSI_HOST *hd, uint target, uint lun)
  828. {
  829. SCSIIORequest_t *mf = NULL;
  830. int ii;
  831. int max = hd->ioc->req_depth;
  832. dsprintk((KERN_INFO MYNAM ": search_running target %d lun %d max %d\n",
  833. target, lun, max));
  834. for (ii=0; ii < max; ii++) {
  835. if (hd->ScsiLookup[ii] != NULL) {
  836. mf = (SCSIIORequest_t *)MPT_INDEX_2_MFPTR(hd->ioc, ii);
  837. dsprintk(( "search_running: found (sc=%p, mf = %p) target %d, lun %d \n",
  838. hd->ScsiLookup[ii], mf, mf->TargetID, mf->LUN[1]));
  839. if ((mf->TargetID != ((u8)target)) || (mf->LUN[1] != ((u8) lun)))
  840. continue;
  841. /* Cleanup
  842. */
  843. hd->ScsiLookup[ii] = NULL;
  844. mptscsih_freeChainBuffers(hd->ioc, ii);
  845. mpt_free_msg_frame(hd->ioc, (MPT_FRAME_HDR *)mf);
  846. }
  847. }
  848. return;
  849. }
  850. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  851. /*
  852. * Hack! It might be nice to report if a device is returning QUEUE_FULL
  853. * but maybe not each and every time...
  854. */
  855. static long last_queue_full = 0;
  856. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  857. /*
  858. * mptscsih_report_queue_full - Report QUEUE_FULL status returned
  859. * from a SCSI target device.
  860. * @sc: Pointer to scsi_cmnd structure
  861. * @pScsiReply: Pointer to SCSIIOReply_t
  862. * @pScsiReq: Pointer to original SCSI request
  863. *
  864. * This routine periodically reports QUEUE_FULL status returned from a
  865. * SCSI target device. It reports this to the console via kernel
  866. * printk() API call, not more than once every 10 seconds.
  867. */
  868. static void
  869. mptscsih_report_queue_full(struct scsi_cmnd *sc, SCSIIOReply_t *pScsiReply, SCSIIORequest_t *pScsiReq)
  870. {
  871. long time = jiffies;
  872. if (time - last_queue_full > 10 * HZ) {
  873. char *ioc_str = "ioc?";
  874. if (sc->device && sc->device->host != NULL && sc->device->host->hostdata != NULL)
  875. ioc_str = ((MPT_SCSI_HOST *)sc->device->host->hostdata)->ioc->name;
  876. dprintk((MYIOC_s_WARN_FMT "Device (%d:%d:%d) reported QUEUE_FULL!\n",
  877. ioc_str, 0, sc->device->id, sc->device->lun));
  878. last_queue_full = time;
  879. }
  880. }
  881. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  882. static char *info_kbuf = NULL;
  883. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  884. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  885. /*
  886. * mptscsih_probe - Installs scsi devices per bus.
  887. * @pdev: Pointer to pci_dev structure
  888. *
  889. * Returns 0 for success, non-zero for failure.
  890. *
  891. */
  892. static int
  893. mptscsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  894. {
  895. struct Scsi_Host *sh;
  896. MPT_SCSI_HOST *hd;
  897. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  898. unsigned long flags;
  899. int sz, ii;
  900. int numSGE = 0;
  901. int scale;
  902. int ioc_cap;
  903. u8 *mem;
  904. int error=0;
  905. /* 20010202 -sralston
  906. * Added sanity check on readiness of the MPT adapter.
  907. */
  908. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  909. printk(MYIOC_s_WARN_FMT
  910. "Skipping because it's not operational!\n",
  911. ioc->name);
  912. return -ENODEV;
  913. }
  914. if (!ioc->active) {
  915. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  916. ioc->name);
  917. return -ENODEV;
  918. }
  919. /* Sanity check - ensure at least 1 port is INITIATOR capable
  920. */
  921. ioc_cap = 0;
  922. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  923. if (ioc->pfacts[ii].ProtocolFlags &
  924. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  925. ioc_cap ++;
  926. }
  927. if (!ioc_cap) {
  928. printk(MYIOC_s_WARN_FMT
  929. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  930. ioc->name, ioc);
  931. return -ENODEV;
  932. }
  933. sh = scsi_host_alloc(&driver_template, sizeof(MPT_SCSI_HOST));
  934. if (!sh) {
  935. printk(MYIOC_s_WARN_FMT
  936. "Unable to register controller with SCSI subsystem\n",
  937. ioc->name);
  938. return -1;
  939. }
  940. spin_lock_irqsave(&ioc->FreeQlock, flags);
  941. /* Attach the SCSI Host to the IOC structure
  942. */
  943. ioc->sh = sh;
  944. sh->io_port = 0;
  945. sh->n_io_port = 0;
  946. sh->irq = 0;
  947. /* set 16 byte cdb's */
  948. sh->max_cmd_len = 16;
  949. /* Yikes! This is important!
  950. * Otherwise, by default, linux
  951. * only scans target IDs 0-7!
  952. * pfactsN->MaxDevices unreliable
  953. * (not supported in early
  954. * versions of the FW).
  955. * max_id = 1 + actual max id,
  956. * max_lun = 1 + actual last lun,
  957. * see hosts.h :o(
  958. */
  959. if (ioc->bus_type == SCSI) {
  960. sh->max_id = MPT_MAX_SCSI_DEVICES;
  961. } else {
  962. /* For FC, increase the queue depth
  963. * from MPT_SCSI_CAN_QUEUE (31)
  964. * to MPT_FC_CAN_QUEUE (63).
  965. */
  966. sh->can_queue = MPT_FC_CAN_QUEUE;
  967. sh->max_id =
  968. MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
  969. }
  970. sh->max_lun = MPT_LAST_LUN + 1;
  971. sh->max_channel = 0;
  972. sh->this_id = ioc->pfacts[0].PortSCSIID;
  973. /* Required entry.
  974. */
  975. sh->unique_id = ioc->id;
  976. /* Verify that we won't exceed the maximum
  977. * number of chain buffers
  978. * We can optimize: ZZ = req_sz/sizeof(SGE)
  979. * For 32bit SGE's:
  980. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  981. * + (req_sz - 64)/sizeof(SGE)
  982. * A slightly different algorithm is required for
  983. * 64bit SGEs.
  984. */
  985. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  986. if (sizeof(dma_addr_t) == sizeof(u64)) {
  987. numSGE = (scale - 1) *
  988. (ioc->facts.MaxChainDepth-1) + scale +
  989. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  990. sizeof(u32));
  991. } else {
  992. numSGE = 1 + (scale - 1) *
  993. (ioc->facts.MaxChainDepth-1) + scale +
  994. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  995. sizeof(u32));
  996. }
  997. if (numSGE < sh->sg_tablesize) {
  998. /* Reset this value */
  999. dprintk((MYIOC_s_INFO_FMT
  1000. "Resetting sg_tablesize to %d from %d\n",
  1001. ioc->name, numSGE, sh->sg_tablesize));
  1002. sh->sg_tablesize = numSGE;
  1003. }
  1004. /* Set the pci device pointer in Scsi_Host structure.
  1005. */
  1006. scsi_set_device(sh, &ioc->pcidev->dev);
  1007. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1008. hd = (MPT_SCSI_HOST *) sh->hostdata;
  1009. hd->ioc = ioc;
  1010. /* SCSI needs scsi_cmnd lookup table!
  1011. * (with size equal to req_depth*PtrSz!)
  1012. */
  1013. sz = ioc->req_depth * sizeof(void *);
  1014. mem = kmalloc(sz, GFP_ATOMIC);
  1015. if (mem == NULL) {
  1016. error = -ENOMEM;
  1017. goto mptscsih_probe_failed;
  1018. }
  1019. memset(mem, 0, sz);
  1020. hd->ScsiLookup = (struct scsi_cmnd **) mem;
  1021. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p, sz=%d\n",
  1022. ioc->name, hd->ScsiLookup, sz));
  1023. /* Allocate memory for the device structures.
  1024. * A non-Null pointer at an offset
  1025. * indicates a device exists.
  1026. * max_id = 1 + maximum id (hosts.h)
  1027. */
  1028. sz = sh->max_id * sizeof(void *);
  1029. mem = kmalloc(sz, GFP_ATOMIC);
  1030. if (mem == NULL) {
  1031. error = -ENOMEM;
  1032. goto mptscsih_probe_failed;
  1033. }
  1034. memset(mem, 0, sz);
  1035. hd->Targets = (VirtDevice **) mem;
  1036. dprintk((KERN_INFO
  1037. " Targets @ %p, sz=%d\n", hd->Targets, sz));
  1038. /* Clear the TM flags
  1039. */
  1040. hd->tmPending = 0;
  1041. hd->tmState = TM_STATE_NONE;
  1042. hd->resetPending = 0;
  1043. hd->abortSCpnt = NULL;
  1044. /* Clear the pointer used to store
  1045. * single-threaded commands, i.e., those
  1046. * issued during a bus scan, dv and
  1047. * configuration pages.
  1048. */
  1049. hd->cmdPtr = NULL;
  1050. /* Initialize this SCSI Hosts' timers
  1051. * To use, set the timer expires field
  1052. * and add_timer
  1053. */
  1054. init_timer(&hd->timer);
  1055. hd->timer.data = (unsigned long) hd;
  1056. hd->timer.function = mptscsih_timer_expired;
  1057. if (ioc->bus_type == SCSI) {
  1058. /* Update with the driver setup
  1059. * values.
  1060. */
  1061. if (ioc->spi_data.maxBusWidth > mpt_width)
  1062. ioc->spi_data.maxBusWidth = mpt_width;
  1063. if (ioc->spi_data.minSyncFactor < mpt_factor)
  1064. ioc->spi_data.minSyncFactor = mpt_factor;
  1065. if (ioc->spi_data.minSyncFactor == MPT_ASYNC) {
  1066. ioc->spi_data.maxSyncOffset = 0;
  1067. }
  1068. ioc->spi_data.Saf_Te = mpt_saf_te;
  1069. hd->negoNvram = 0;
  1070. #ifndef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  1071. hd->negoNvram = MPT_SCSICFG_USE_NVRAM;
  1072. #endif
  1073. ioc->spi_data.forceDv = 0;
  1074. ioc->spi_data.noQas = 0;
  1075. for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
  1076. ioc->spi_data.dvStatus[ii] =
  1077. MPT_SCSICFG_NEGOTIATE;
  1078. }
  1079. for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++)
  1080. ioc->spi_data.dvStatus[ii] |=
  1081. MPT_SCSICFG_DV_NOT_DONE;
  1082. dinitprintk((MYIOC_s_INFO_FMT
  1083. "dv %x width %x factor %x saf_te %x\n",
  1084. ioc->name, mpt_dv,
  1085. mpt_width,
  1086. mpt_factor,
  1087. mpt_saf_te));
  1088. }
  1089. mpt_scsi_hosts++;
  1090. error = scsi_add_host (sh, &ioc->pcidev->dev);
  1091. if(error) {
  1092. dprintk((KERN_ERR MYNAM
  1093. "scsi_add_host failed\n"));
  1094. goto mptscsih_probe_failed;
  1095. }
  1096. scsi_scan_host(sh);
  1097. return 0;
  1098. mptscsih_probe_failed:
  1099. mptscsih_remove(pdev);
  1100. return error;
  1101. }
  1102. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1103. /*
  1104. * mptscsih_remove - Removed scsi devices
  1105. * @pdev: Pointer to pci_dev structure
  1106. *
  1107. *
  1108. */
  1109. static void
  1110. mptscsih_remove(struct pci_dev *pdev)
  1111. {
  1112. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1113. struct Scsi_Host *host = ioc->sh;
  1114. MPT_SCSI_HOST *hd;
  1115. int count;
  1116. unsigned long flags;
  1117. if(!host)
  1118. return;
  1119. scsi_remove_host(host);
  1120. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  1121. /* Check DV thread active */
  1122. count = 10 * HZ;
  1123. spin_lock_irqsave(&dvtaskQ_lock, flags);
  1124. if (dvtaskQ_active) {
  1125. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  1126. while(dvtaskQ_active && --count) {
  1127. set_current_state(TASK_INTERRUPTIBLE);
  1128. schedule_timeout(1);
  1129. }
  1130. } else {
  1131. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  1132. }
  1133. if (!count)
  1134. printk(KERN_ERR MYNAM ": ERROR - DV thread still active!\n");
  1135. #if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
  1136. else
  1137. printk(KERN_ERR MYNAM ": DV thread orig %d, count %d\n", 10 * HZ, count);
  1138. #endif
  1139. #endif
  1140. hd = (MPT_SCSI_HOST *)host->hostdata;
  1141. if (hd != NULL) {
  1142. int sz1;
  1143. mptscsih_shutdown(&pdev->dev);
  1144. sz1=0;
  1145. if (hd->ScsiLookup != NULL) {
  1146. sz1 = hd->ioc->req_depth * sizeof(void *);
  1147. kfree(hd->ScsiLookup);
  1148. hd->ScsiLookup = NULL;
  1149. }
  1150. if (hd->Targets != NULL) {
  1151. /*
  1152. * Free pointer array.
  1153. */
  1154. kfree(hd->Targets);
  1155. hd->Targets = NULL;
  1156. }
  1157. dprintk((MYIOC_s_INFO_FMT
  1158. "Free'd ScsiLookup (%d) memory\n",
  1159. hd->ioc->name, sz1));
  1160. /* NULL the Scsi_Host pointer
  1161. */
  1162. hd->ioc->sh = NULL;
  1163. }
  1164. scsi_host_put(host);
  1165. mpt_scsi_hosts--;
  1166. }
  1167. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1168. /*
  1169. * mptscsih_shutdown - reboot notifier
  1170. *
  1171. */
  1172. static void
  1173. mptscsih_shutdown(struct device * dev)
  1174. {
  1175. MPT_ADAPTER *ioc = pci_get_drvdata(to_pci_dev(dev));
  1176. struct Scsi_Host *host = ioc->sh;
  1177. MPT_SCSI_HOST *hd;
  1178. if(!host)
  1179. return;
  1180. hd = (MPT_SCSI_HOST *)host->hostdata;
  1181. /* Flush the cache of this adapter
  1182. */
  1183. if(hd != NULL)
  1184. mptscsih_synchronize_cache(hd, 0);
  1185. }
  1186. #ifdef CONFIG_PM
  1187. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1188. /*
  1189. * mptscsih_suspend - Fusion MPT scsie driver suspend routine.
  1190. *
  1191. *
  1192. */
  1193. static int
  1194. mptscsih_suspend(struct pci_dev *pdev, pm_message_t state)
  1195. {
  1196. mptscsih_shutdown(&pdev->dev);
  1197. return 0;
  1198. }
  1199. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1200. /*
  1201. * mptscsih_resume - Fusion MPT scsi driver resume routine.
  1202. *
  1203. *
  1204. */
  1205. static int
  1206. mptscsih_resume(struct pci_dev *pdev)
  1207. {
  1208. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1209. struct Scsi_Host *host = ioc->sh;
  1210. MPT_SCSI_HOST *hd;
  1211. if(!host)
  1212. return 0;
  1213. hd = (MPT_SCSI_HOST *)host->hostdata;
  1214. if(!hd)
  1215. return 0;
  1216. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  1217. {
  1218. unsigned long lflags;
  1219. spin_lock_irqsave(&dvtaskQ_lock, lflags);
  1220. if (!dvtaskQ_active) {
  1221. dvtaskQ_active = 1;
  1222. spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
  1223. INIT_WORK(&mptscsih_dvTask,
  1224. mptscsih_domainValidation, (void *) hd);
  1225. schedule_work(&mptscsih_dvTask);
  1226. } else {
  1227. spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
  1228. }
  1229. }
  1230. #endif
  1231. return 0;
  1232. }
  1233. #endif
  1234. static struct mpt_pci_driver mptscsih_driver = {
  1235. .probe = mptscsih_probe,
  1236. .remove = mptscsih_remove,
  1237. .shutdown = mptscsih_shutdown,
  1238. #ifdef CONFIG_PM
  1239. .suspend = mptscsih_suspend,
  1240. .resume = mptscsih_resume,
  1241. #endif
  1242. };
  1243. /* SCSI host fops start here... */
  1244. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1245. /**
  1246. * mptscsih_init - Register MPT adapter(s) as SCSI host(s) with
  1247. * linux scsi mid-layer.
  1248. *
  1249. * Returns 0 for success, non-zero for failure.
  1250. */
  1251. static int __init
  1252. mptscsih_init(void)
  1253. {
  1254. show_mptmod_ver(my_NAME, my_VERSION);
  1255. ScsiDoneCtx = mpt_register(mptscsih_io_done, MPTSCSIH_DRIVER);
  1256. ScsiTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSCSIH_DRIVER);
  1257. ScsiScanDvCtx = mpt_register(mptscsih_scandv_complete, MPTSCSIH_DRIVER);
  1258. if (mpt_event_register(ScsiDoneCtx, mptscsih_event_process) == 0) {
  1259. devtprintk((KERN_INFO MYNAM
  1260. ": Registered for IOC event notifications\n"));
  1261. }
  1262. if (mpt_reset_register(ScsiDoneCtx, mptscsih_ioc_reset) == 0) {
  1263. dprintk((KERN_INFO MYNAM
  1264. ": Registered for IOC reset notifications\n"));
  1265. }
  1266. if(mpt_device_driver_register(&mptscsih_driver,
  1267. MPTSCSIH_DRIVER) != 0 ) {
  1268. dprintk((KERN_INFO MYNAM
  1269. ": failed to register dd callbacks\n"));
  1270. }
  1271. return 0;
  1272. }
  1273. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1274. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1275. /**
  1276. * mptscsih_exit - Unregisters MPT adapter(s)
  1277. *
  1278. */
  1279. static void __exit
  1280. mptscsih_exit(void)
  1281. {
  1282. mpt_device_driver_deregister(MPTSCSIH_DRIVER);
  1283. mpt_reset_deregister(ScsiDoneCtx);
  1284. dprintk((KERN_INFO MYNAM
  1285. ": Deregistered for IOC reset notifications\n"));
  1286. mpt_event_deregister(ScsiDoneCtx);
  1287. dprintk((KERN_INFO MYNAM
  1288. ": Deregistered for IOC event notifications\n"));
  1289. mpt_deregister(ScsiScanDvCtx);
  1290. mpt_deregister(ScsiTaskCtx);
  1291. mpt_deregister(ScsiDoneCtx);
  1292. if (info_kbuf != NULL)
  1293. kfree(info_kbuf);
  1294. }
  1295. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1296. /**
  1297. * mptscsih_info - Return information about MPT adapter
  1298. * @SChost: Pointer to Scsi_Host structure
  1299. *
  1300. * (linux scsi_host_template.info routine)
  1301. *
  1302. * Returns pointer to buffer where information was written.
  1303. */
  1304. static const char *
  1305. mptscsih_info(struct Scsi_Host *SChost)
  1306. {
  1307. MPT_SCSI_HOST *h;
  1308. int size = 0;
  1309. if (info_kbuf == NULL)
  1310. if ((info_kbuf = kmalloc(0x1000 /* 4Kb */, GFP_KERNEL)) == NULL)
  1311. return info_kbuf;
  1312. h = (MPT_SCSI_HOST *)SChost->hostdata;
  1313. info_kbuf[0] = '\0';
  1314. if (h) {
  1315. mpt_print_ioc_summary(h->ioc, info_kbuf, &size, 0, 0);
  1316. info_kbuf[size-1] = '\0';
  1317. }
  1318. return info_kbuf;
  1319. }
  1320. struct info_str {
  1321. char *buffer;
  1322. int length;
  1323. int offset;
  1324. int pos;
  1325. };
  1326. static void copy_mem_info(struct info_str *info, char *data, int len)
  1327. {
  1328. if (info->pos + len > info->length)
  1329. len = info->length - info->pos;
  1330. if (info->pos + len < info->offset) {
  1331. info->pos += len;
  1332. return;
  1333. }
  1334. if (info->pos < info->offset) {
  1335. data += (info->offset - info->pos);
  1336. len -= (info->offset - info->pos);
  1337. }
  1338. if (len > 0) {
  1339. memcpy(info->buffer + info->pos, data, len);
  1340. info->pos += len;
  1341. }
  1342. }
  1343. static int copy_info(struct info_str *info, char *fmt, ...)
  1344. {
  1345. va_list args;
  1346. char buf[81];
  1347. int len;
  1348. va_start(args, fmt);
  1349. len = vsprintf(buf, fmt, args);
  1350. va_end(args);
  1351. copy_mem_info(info, buf, len);
  1352. return len;
  1353. }
  1354. static int mptscsih_host_info(MPT_ADAPTER *ioc, char *pbuf, off_t offset, int len)
  1355. {
  1356. struct info_str info;
  1357. info.buffer = pbuf;
  1358. info.length = len;
  1359. info.offset = offset;
  1360. info.pos = 0;
  1361. copy_info(&info, "%s: %s, ", ioc->name, ioc->prod_name);
  1362. copy_info(&info, "%s%08xh, ", MPT_FW_REV_MAGIC_ID_STRING, ioc->facts.FWVersion.Word);
  1363. copy_info(&info, "Ports=%d, ", ioc->facts.NumberOfPorts);
  1364. copy_info(&info, "MaxQ=%d\n", ioc->req_depth);
  1365. return ((info.pos > info.offset) ? info.pos - info.offset : 0);
  1366. }
  1367. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1368. /**
  1369. * mptscsih_proc_info - Return information about MPT adapter
  1370. *
  1371. * (linux scsi_host_template.info routine)
  1372. *
  1373. * buffer: if write, user data; if read, buffer for user
  1374. * length: if write, return length;
  1375. * offset: if write, 0; if read, the current offset into the buffer from
  1376. * the previous read.
  1377. * hostno: scsi host number
  1378. * func: if write = 1; if read = 0
  1379. */
  1380. static int
  1381. mptscsih_proc_info(struct Scsi_Host *host, char *buffer, char **start, off_t offset,
  1382. int length, int func)
  1383. {
  1384. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  1385. MPT_ADAPTER *ioc = hd->ioc;
  1386. int size = 0;
  1387. if (func) {
  1388. /*
  1389. * write is not supported
  1390. */
  1391. } else {
  1392. if (start)
  1393. *start = buffer;
  1394. size = mptscsih_host_info(ioc, buffer, offset, length);
  1395. }
  1396. return size;
  1397. }
  1398. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1399. #define ADD_INDEX_LOG(req_ent) do { } while(0)
  1400. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1401. /**
  1402. * mptscsih_qcmd - Primary Fusion MPT SCSI initiator IO start routine.
  1403. * @SCpnt: Pointer to scsi_cmnd structure
  1404. * @done: Pointer SCSI mid-layer IO completion function
  1405. *
  1406. * (linux scsi_host_template.queuecommand routine)
  1407. * This is the primary SCSI IO start routine. Create a MPI SCSIIORequest
  1408. * from a linux scsi_cmnd request and send it to the IOC.
  1409. *
  1410. * Returns 0. (rtn value discarded by linux scsi mid-layer)
  1411. */
  1412. static int
  1413. mptscsih_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  1414. {
  1415. MPT_SCSI_HOST *hd;
  1416. MPT_FRAME_HDR *mf;
  1417. SCSIIORequest_t *pScsiReq;
  1418. VirtDevice *pTarget;
  1419. int target;
  1420. int lun;
  1421. u32 datalen;
  1422. u32 scsictl;
  1423. u32 scsidir;
  1424. u32 cmd_len;
  1425. int my_idx;
  1426. int ii;
  1427. hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata;
  1428. target = SCpnt->device->id;
  1429. lun = SCpnt->device->lun;
  1430. SCpnt->scsi_done = done;
  1431. pTarget = hd->Targets[target];
  1432. dmfprintk((MYIOC_s_INFO_FMT "qcmd: SCpnt=%p, done()=%p\n",
  1433. (hd && hd->ioc) ? hd->ioc->name : "ioc?", SCpnt, done));
  1434. if (hd->resetPending) {
  1435. dtmprintk((MYIOC_s_WARN_FMT "qcmd: SCpnt=%p timeout + 60HZ\n",
  1436. (hd && hd->ioc) ? hd->ioc->name : "ioc?", SCpnt));
  1437. return SCSI_MLQUEUE_HOST_BUSY;
  1438. }
  1439. /*
  1440. * Put together a MPT SCSI request...
  1441. */
  1442. if ((mf = mpt_get_msg_frame(ScsiDoneCtx, hd->ioc)) == NULL) {
  1443. dprintk((MYIOC_s_WARN_FMT "QueueCmd, no msg frames!!\n",
  1444. hd->ioc->name));
  1445. return SCSI_MLQUEUE_HOST_BUSY;
  1446. }
  1447. pScsiReq = (SCSIIORequest_t *) mf;
  1448. my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  1449. ADD_INDEX_LOG(my_idx);
  1450. /* BUG FIX! 19991030 -sralston
  1451. * TUR's being issued with scsictl=0x02000000 (DATA_IN)!
  1452. * Seems we may receive a buffer (datalen>0) even when there
  1453. * will be no data transfer! GRRRRR...
  1454. */
  1455. if (SCpnt->sc_data_direction == DMA_FROM_DEVICE) {
  1456. datalen = SCpnt->request_bufflen;
  1457. scsidir = MPI_SCSIIO_CONTROL_READ; /* DATA IN (host<--ioc<--dev) */
  1458. } else if (SCpnt->sc_data_direction == DMA_TO_DEVICE) {
  1459. datalen = SCpnt->request_bufflen;
  1460. scsidir = MPI_SCSIIO_CONTROL_WRITE; /* DATA OUT (host-->ioc-->dev) */
  1461. } else {
  1462. datalen = 0;
  1463. scsidir = MPI_SCSIIO_CONTROL_NODATATRANSFER;
  1464. }
  1465. /* Default to untagged. Once a target structure has been allocated,
  1466. * use the Inquiry data to determine if device supports tagged.
  1467. */
  1468. if ( pTarget
  1469. && (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)
  1470. && (SCpnt->device->tagged_supported)) {
  1471. scsictl = scsidir | MPI_SCSIIO_CONTROL_SIMPLEQ;
  1472. } else {
  1473. scsictl = scsidir | MPI_SCSIIO_CONTROL_UNTAGGED;
  1474. }
  1475. /* Use the above information to set up the message frame
  1476. */
  1477. pScsiReq->TargetID = (u8) target;
  1478. pScsiReq->Bus = (u8) SCpnt->device->channel;
  1479. pScsiReq->ChainOffset = 0;
  1480. pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
  1481. pScsiReq->CDBLength = SCpnt->cmd_len;
  1482. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  1483. pScsiReq->Reserved = 0;
  1484. pScsiReq->MsgFlags = mpt_msg_flags();
  1485. pScsiReq->LUN[0] = 0;
  1486. pScsiReq->LUN[1] = lun;
  1487. pScsiReq->LUN[2] = 0;
  1488. pScsiReq->LUN[3] = 0;
  1489. pScsiReq->LUN[4] = 0;
  1490. pScsiReq->LUN[5] = 0;
  1491. pScsiReq->LUN[6] = 0;
  1492. pScsiReq->LUN[7] = 0;
  1493. pScsiReq->Control = cpu_to_le32(scsictl);
  1494. /*
  1495. * Write SCSI CDB into the message
  1496. */
  1497. cmd_len = SCpnt->cmd_len;
  1498. for (ii=0; ii < cmd_len; ii++)
  1499. pScsiReq->CDB[ii] = SCpnt->cmnd[ii];
  1500. for (ii=cmd_len; ii < 16; ii++)
  1501. pScsiReq->CDB[ii] = 0;
  1502. /* DataLength */
  1503. pScsiReq->DataLength = cpu_to_le32(datalen);
  1504. /* SenseBuffer low address */
  1505. pScsiReq->SenseBufferLowAddr = cpu_to_le32(hd->ioc->sense_buf_low_dma
  1506. + (my_idx * MPT_SENSE_BUFFER_ALLOC));
  1507. /* Now add the SG list
  1508. * Always have a SGE even if null length.
  1509. */
  1510. if (datalen == 0) {
  1511. /* Add a NULL SGE */
  1512. mptscsih_add_sge((char *)&pScsiReq->SGL, MPT_SGE_FLAGS_SSIMPLE_READ | 0,
  1513. (dma_addr_t) -1);
  1514. } else {
  1515. /* Add a 32 or 64 bit SGE */
  1516. if (mptscsih_AddSGE(hd->ioc, SCpnt, pScsiReq, my_idx) != SUCCESS)
  1517. goto fail;
  1518. }
  1519. hd->ScsiLookup[my_idx] = SCpnt;
  1520. SCpnt->host_scribble = NULL;
  1521. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  1522. if (hd->ioc->bus_type == SCSI) {
  1523. int dvStatus = hd->ioc->spi_data.dvStatus[target];
  1524. int issueCmd = 1;
  1525. if (dvStatus || hd->ioc->spi_data.forceDv) {
  1526. if ((dvStatus & MPT_SCSICFG_NEED_DV) ||
  1527. (hd->ioc->spi_data.forceDv & MPT_SCSICFG_NEED_DV)) {
  1528. unsigned long lflags;
  1529. /* Schedule DV if necessary */
  1530. spin_lock_irqsave(&dvtaskQ_lock, lflags);
  1531. if (!dvtaskQ_active) {
  1532. dvtaskQ_active = 1;
  1533. spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
  1534. INIT_WORK(&mptscsih_dvTask, mptscsih_domainValidation, (void *) hd);
  1535. schedule_work(&mptscsih_dvTask);
  1536. } else {
  1537. spin_unlock_irqrestore(&dvtaskQ_lock, lflags);
  1538. }
  1539. hd->ioc->spi_data.forceDv &= ~MPT_SCSICFG_NEED_DV;
  1540. }
  1541. /* Trying to do DV to this target, extend timeout.
  1542. * Wait to issue until flag is clear
  1543. */
  1544. if (dvStatus & MPT_SCSICFG_DV_PENDING) {
  1545. mod_timer(&SCpnt->eh_timeout, jiffies + 40 * HZ);
  1546. issueCmd = 0;
  1547. }
  1548. /* Set the DV flags.
  1549. */
  1550. if (dvStatus & MPT_SCSICFG_DV_NOT_DONE)
  1551. mptscsih_set_dvflags(hd, pScsiReq);
  1552. if (!issueCmd)
  1553. goto fail;
  1554. }
  1555. }
  1556. #endif
  1557. mpt_put_msg_frame(ScsiDoneCtx, hd->ioc, mf);
  1558. dmfprintk((MYIOC_s_INFO_FMT "Issued SCSI cmd (%p) mf=%p idx=%d\n",
  1559. hd->ioc->name, SCpnt, mf, my_idx));
  1560. DBG_DUMP_REQUEST_FRAME(mf)
  1561. return 0;
  1562. fail:
  1563. mptscsih_freeChainBuffers(hd->ioc, my_idx);
  1564. mpt_free_msg_frame(hd->ioc, mf);
  1565. return SCSI_MLQUEUE_HOST_BUSY;
  1566. }
  1567. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1568. /*
  1569. * mptscsih_freeChainBuffers - Function to free chain buffers associated
  1570. * with a SCSI IO request
  1571. * @hd: Pointer to the MPT_SCSI_HOST instance
  1572. * @req_idx: Index of the SCSI IO request frame.
  1573. *
  1574. * Called if SG chain buffer allocation fails and mptscsih callbacks.
  1575. * No return.
  1576. */
  1577. static void
  1578. mptscsih_freeChainBuffers(MPT_ADAPTER *ioc, int req_idx)
  1579. {
  1580. MPT_FRAME_HDR *chain;
  1581. unsigned long flags;
  1582. int chain_idx;
  1583. int next;
  1584. /* Get the first chain index and reset
  1585. * tracker state.
  1586. */
  1587. chain_idx = ioc->ReqToChain[req_idx];
  1588. ioc->ReqToChain[req_idx] = MPT_HOST_NO_CHAIN;
  1589. while (chain_idx != MPT_HOST_NO_CHAIN) {
  1590. /* Save the next chain buffer index */
  1591. next = ioc->ChainToChain[chain_idx];
  1592. /* Free this chain buffer and reset
  1593. * tracker
  1594. */
  1595. ioc->ChainToChain[chain_idx] = MPT_HOST_NO_CHAIN;
  1596. chain = (MPT_FRAME_HDR *) (ioc->ChainBuffer
  1597. + (chain_idx * ioc->req_sz));
  1598. spin_lock_irqsave(&ioc->FreeQlock, flags);
  1599. list_add_tail(&chain->u.frame.linkage.list, &ioc->FreeChainQ);
  1600. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  1601. dmfprintk((MYIOC_s_INFO_FMT "FreeChainBuffers (index %d)\n",
  1602. ioc->name, chain_idx));
  1603. /* handle next */
  1604. chain_idx = next;
  1605. }
  1606. return;
  1607. }
  1608. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1609. /*
  1610. * Reset Handling
  1611. */
  1612. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1613. /*
  1614. * mptscsih_TMHandler - Generic handler for SCSI Task Management.
  1615. * Fall through to mpt_HardResetHandler if: not operational, too many
  1616. * failed TM requests or handshake failure.
  1617. *
  1618. * @ioc: Pointer to MPT_ADAPTER structure
  1619. * @type: Task Management type
  1620. * @target: Logical Target ID for reset (if appropriate)
  1621. * @lun: Logical Unit for reset (if appropriate)
  1622. * @ctx2abort: Context for the task to be aborted (if appropriate)
  1623. *
  1624. * Remark: Currently invoked from a non-interrupt thread (_bh).
  1625. *
  1626. * Remark: With old EH code, at most 1 SCSI TaskMgmt function per IOC
  1627. * will be active.
  1628. *
  1629. * Returns 0 for SUCCESS or -1 if FAILED.
  1630. */
  1631. static int
  1632. mptscsih_TMHandler(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout)
  1633. {
  1634. MPT_ADAPTER *ioc;
  1635. int rc = -1;
  1636. int doTask = 1;
  1637. u32 ioc_raw_state;
  1638. unsigned long flags;
  1639. /* If FW is being reloaded currently, return success to
  1640. * the calling function.
  1641. */
  1642. if (hd == NULL)
  1643. return 0;
  1644. ioc = hd->ioc;
  1645. if (ioc == NULL) {
  1646. printk(KERN_ERR MYNAM " TMHandler" " NULL ioc!\n");
  1647. return FAILED;
  1648. }
  1649. dtmprintk((MYIOC_s_INFO_FMT "TMHandler Entered!\n", ioc->name));
  1650. // SJR - CHECKME - Can we avoid this here?
  1651. // (mpt_HardResetHandler has this check...)
  1652. spin_lock_irqsave(&ioc->diagLock, flags);
  1653. if ((ioc->diagPending) || (ioc->alt_ioc && ioc->alt_ioc->diagPending)) {
  1654. spin_unlock_irqrestore(&ioc->diagLock, flags);
  1655. return FAILED;
  1656. }
  1657. spin_unlock_irqrestore(&ioc->diagLock, flags);
  1658. /* Wait a fixed amount of time for the TM pending flag to be cleared.
  1659. * If we time out and not bus reset, then we return a FAILED status to the caller.
  1660. * The call to mptscsih_tm_pending_wait() will set the pending flag if we are
  1661. * successful. Otherwise, reload the FW.
  1662. */
  1663. if (mptscsih_tm_pending_wait(hd) == FAILED) {
  1664. if (type == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
  1665. dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler abort: "
  1666. "Timed out waiting for last TM (%d) to complete! \n",
  1667. hd->ioc->name, hd->tmPending));
  1668. return FAILED;
  1669. } else if (type == MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
  1670. dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler target reset: "
  1671. "Timed out waiting for last TM (%d) to complete! \n",
  1672. hd->ioc->name, hd->tmPending));
  1673. return FAILED;
  1674. } else if (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
  1675. dtmprintk((KERN_WARNING MYNAM ": %s: TMHandler bus reset: "
  1676. "Timed out waiting for last TM (%d) to complete! \n",
  1677. hd->ioc->name, hd->tmPending));
  1678. if (hd->tmPending & (1 << MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS))
  1679. return FAILED;
  1680. doTask = 0;
  1681. }
  1682. } else {
  1683. spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
  1684. hd->tmPending |= (1 << type);
  1685. spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
  1686. }
  1687. /* Is operational?
  1688. */
  1689. ioc_raw_state = mpt_GetIocState(hd->ioc, 0);
  1690. #ifdef MPT_DEBUG_RESET
  1691. if ((ioc_raw_state & MPI_IOC_STATE_MASK) != MPI_IOC_STATE_OPERATIONAL) {
  1692. printk(MYIOC_s_WARN_FMT
  1693. "TM Handler: IOC Not operational(0x%x)!\n",
  1694. hd->ioc->name, ioc_raw_state);
  1695. }
  1696. #endif
  1697. if (doTask && ((ioc_raw_state & MPI_IOC_STATE_MASK) == MPI_IOC_STATE_OPERATIONAL)
  1698. && !(ioc_raw_state & MPI_DOORBELL_ACTIVE)) {
  1699. /* Isse the Task Mgmt request.
  1700. */
  1701. if (hd->hard_resets < -1)
  1702. hd->hard_resets++;
  1703. rc = mptscsih_IssueTaskMgmt(hd, type, channel, target, lun, ctx2abort, timeout);
  1704. if (rc) {
  1705. printk(MYIOC_s_INFO_FMT "Issue of TaskMgmt failed!\n", hd->ioc->name);
  1706. } else {
  1707. dtmprintk((MYIOC_s_INFO_FMT "Issue of TaskMgmt Successful!\n", hd->ioc->name));
  1708. }
  1709. }
  1710. /* Only fall through to the HRH if this is a bus reset
  1711. */
  1712. if ((type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) && (rc ||
  1713. ioc->reload_fw || (ioc->alt_ioc && ioc->alt_ioc->reload_fw))) {
  1714. dtmprintk((MYIOC_s_INFO_FMT "Calling HardReset! \n",
  1715. hd->ioc->name));
  1716. rc = mpt_HardResetHandler(hd->ioc, CAN_SLEEP);
  1717. }
  1718. dtmprintk((MYIOC_s_INFO_FMT "TMHandler rc = %d!\n", hd->ioc->name, rc));
  1719. return rc;
  1720. }
  1721. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1722. /*
  1723. * mptscsih_IssueTaskMgmt - Generic send Task Management function.
  1724. * @hd: Pointer to MPT_SCSI_HOST structure
  1725. * @type: Task Management type
  1726. * @target: Logical Target ID for reset (if appropriate)
  1727. * @lun: Logical Unit for reset (if appropriate)
  1728. * @ctx2abort: Context for the task to be aborted (if appropriate)
  1729. *
  1730. * Remark: _HardResetHandler can be invoked from an interrupt thread (timer)
  1731. * or a non-interrupt thread. In the former, must not call schedule().
  1732. *
  1733. * Not all fields are meaningfull for all task types.
  1734. *
  1735. * Returns 0 for SUCCESS, -999 for "no msg frames",
  1736. * else other non-zero value returned.
  1737. */
  1738. static int
  1739. mptscsih_IssueTaskMgmt(MPT_SCSI_HOST *hd, u8 type, u8 channel, u8 target, u8 lun, int ctx2abort, ulong timeout)
  1740. {
  1741. MPT_FRAME_HDR *mf;
  1742. SCSITaskMgmt_t *pScsiTm;
  1743. int ii;
  1744. int retval;
  1745. /* Return Fail to calling function if no message frames available.
  1746. */
  1747. if ((mf = mpt_get_msg_frame(ScsiTaskCtx, hd->ioc)) == NULL) {
  1748. dfailprintk((MYIOC_s_ERR_FMT "IssueTaskMgmt, no msg frames!!\n",
  1749. hd->ioc->name));
  1750. //return FAILED;
  1751. return -999;
  1752. }
  1753. dtmprintk((MYIOC_s_INFO_FMT "IssueTaskMgmt request @ %p\n",
  1754. hd->ioc->name, mf));
  1755. /* Format the Request
  1756. */
  1757. pScsiTm = (SCSITaskMgmt_t *) mf;
  1758. pScsiTm->TargetID = target;
  1759. pScsiTm->Bus = channel;
  1760. pScsiTm->ChainOffset = 0;
  1761. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  1762. pScsiTm->Reserved = 0;
  1763. pScsiTm->TaskType = type;
  1764. pScsiTm->Reserved1 = 0;
  1765. pScsiTm->MsgFlags = (type == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS)
  1766. ? MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION : 0;
  1767. for (ii= 0; ii < 8; ii++) {
  1768. pScsiTm->LUN[ii] = 0;
  1769. }
  1770. pScsiTm->LUN[1] = lun;
  1771. for (ii=0; ii < 7; ii++)
  1772. pScsiTm->Reserved2[ii] = 0;
  1773. pScsiTm->TaskMsgContext = ctx2abort;
  1774. dtmprintk((MYIOC_s_INFO_FMT
  1775. "IssueTaskMgmt: ctx2abort (0x%08x) type=%d\n",
  1776. hd->ioc->name, ctx2abort, type));
  1777. DBG_DUMP_TM_REQUEST_FRAME((u32 *)pScsiTm);
  1778. if ((retval = mpt_send_handshake_request(ScsiTaskCtx, hd->ioc,
  1779. sizeof(SCSITaskMgmt_t), (u32*)pScsiTm,
  1780. CAN_SLEEP)) != 0) {
  1781. dfailprintk((MYIOC_s_ERR_FMT "_send_handshake FAILED!"
  1782. " (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd,
  1783. hd->ioc, mf));
  1784. mpt_free_msg_frame(hd->ioc, mf);
  1785. return retval;
  1786. }
  1787. if(mptscsih_tm_wait_for_completion(hd, timeout) == FAILED) {
  1788. dfailprintk((MYIOC_s_ERR_FMT "_wait_for_completion FAILED!"
  1789. " (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd,
  1790. hd->ioc, mf));
  1791. mpt_free_msg_frame(hd->ioc, mf);
  1792. dtmprintk((MYIOC_s_INFO_FMT "Calling HardReset! \n",
  1793. hd->ioc->name));
  1794. retval = mpt_HardResetHandler(hd->ioc, CAN_SLEEP);
  1795. }
  1796. return retval;
  1797. }
  1798. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1799. /**
  1800. * mptscsih_abort - Abort linux scsi_cmnd routine, new_eh variant
  1801. * @SCpnt: Pointer to scsi_cmnd structure, IO to be aborted
  1802. *
  1803. * (linux scsi_host_template.eh_abort_handler routine)
  1804. *
  1805. * Returns SUCCESS or FAILED.
  1806. */
  1807. static int
  1808. mptscsih_abort(struct scsi_cmnd * SCpnt)
  1809. {
  1810. MPT_SCSI_HOST *hd;
  1811. MPT_ADAPTER *ioc;
  1812. MPT_FRAME_HDR *mf;
  1813. u32 ctx2abort;
  1814. int scpnt_idx;
  1815. spinlock_t *host_lock = SCpnt->device->host->host_lock;
  1816. /* If we can't locate our host adapter structure, return FAILED status.
  1817. */
  1818. if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL) {
  1819. SCpnt->result = DID_RESET << 16;
  1820. SCpnt->scsi_done(SCpnt);
  1821. dfailprintk((KERN_WARNING MYNAM ": mptscsih_abort: "
  1822. "Can't locate host! (sc=%p)\n",
  1823. SCpnt));
  1824. return FAILED;
  1825. }
  1826. ioc = hd->ioc;
  1827. if (hd->resetPending)
  1828. return FAILED;
  1829. printk(KERN_WARNING MYNAM ": %s: >> Attempting task abort! (sc=%p)\n",
  1830. hd->ioc->name, SCpnt);
  1831. if (hd->timeouts < -1)
  1832. hd->timeouts++;
  1833. /* Find this command
  1834. */
  1835. if ((scpnt_idx = SCPNT_TO_LOOKUP_IDX(SCpnt)) < 0) {
  1836. /* Cmd not found in ScsiLookup.
  1837. * Do OS callback.
  1838. */
  1839. SCpnt->result = DID_RESET << 16;
  1840. dtmprintk((KERN_WARNING MYNAM ": %s: mptscsih_abort: "
  1841. "Command not in the active list! (sc=%p)\n",
  1842. hd->ioc->name, SCpnt));
  1843. return SUCCESS;
  1844. }
  1845. /* Most important! Set TaskMsgContext to SCpnt's MsgContext!
  1846. * (the IO to be ABORT'd)
  1847. *
  1848. * NOTE: Since we do not byteswap MsgContext, we do not
  1849. * swap it here either. It is an opaque cookie to
  1850. * the controller, so it does not matter. -DaveM
  1851. */
  1852. mf = MPT_INDEX_2_MFPTR(hd->ioc, scpnt_idx);
  1853. ctx2abort = mf->u.frame.hwhdr.msgctxu.MsgContext;
  1854. hd->abortSCpnt = SCpnt;
  1855. spin_unlock_irq(host_lock);
  1856. if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
  1857. SCpnt->device->channel, SCpnt->device->id, SCpnt->device->lun,
  1858. ctx2abort, 2 /* 2 second timeout */)
  1859. < 0) {
  1860. /* The TM request failed and the subsequent FW-reload failed!
  1861. * Fatal error case.
  1862. */
  1863. printk(MYIOC_s_WARN_FMT "Error issuing abort task! (sc=%p)\n",
  1864. hd->ioc->name, SCpnt);
  1865. /* We must clear our pending flag before clearing our state.
  1866. */
  1867. hd->tmPending = 0;
  1868. hd->tmState = TM_STATE_NONE;
  1869. spin_lock_irq(host_lock);
  1870. /* Unmap the DMA buffers, if any. */
  1871. if (SCpnt->use_sg) {
  1872. pci_unmap_sg(ioc->pcidev, (struct scatterlist *) SCpnt->request_buffer,
  1873. SCpnt->use_sg, SCpnt->sc_data_direction);
  1874. } else if (SCpnt->request_bufflen) {
  1875. pci_unmap_single(ioc->pcidev, SCpnt->SCp.dma_handle,
  1876. SCpnt->request_bufflen, SCpnt->sc_data_direction);
  1877. }
  1878. hd->ScsiLookup[scpnt_idx] = NULL;
  1879. SCpnt->result = DID_RESET << 16;
  1880. SCpnt->scsi_done(SCpnt); /* Issue the command callback */
  1881. mptscsih_freeChainBuffers(ioc, scpnt_idx);
  1882. mpt_free_msg_frame(ioc, mf);
  1883. return FAILED;
  1884. }
  1885. spin_lock_irq(host_lock);
  1886. return SUCCESS;
  1887. }
  1888. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1889. /**
  1890. * mptscsih_dev_reset - Perform a SCSI TARGET_RESET! new_eh variant
  1891. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1892. *
  1893. * (linux scsi_host_template.eh_dev_reset_handler routine)
  1894. *
  1895. * Returns SUCCESS or FAILED.
  1896. */
  1897. static int
  1898. mptscsih_dev_reset(struct scsi_cmnd * SCpnt)
  1899. {
  1900. MPT_SCSI_HOST *hd;
  1901. spinlock_t *host_lock = SCpnt->device->host->host_lock;
  1902. /* If we can't locate our host adapter structure, return FAILED status.
  1903. */
  1904. if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
  1905. dtmprintk((KERN_WARNING MYNAM ": mptscsih_dev_reset: "
  1906. "Can't locate host! (sc=%p)\n",
  1907. SCpnt));
  1908. return FAILED;
  1909. }
  1910. if (hd->resetPending)
  1911. return FAILED;
  1912. printk(KERN_WARNING MYNAM ": %s: >> Attempting target reset! (sc=%p)\n",
  1913. hd->ioc->name, SCpnt);
  1914. spin_unlock_irq(host_lock);
  1915. if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
  1916. SCpnt->device->channel, SCpnt->device->id,
  1917. 0, 0, 5 /* 5 second timeout */)
  1918. < 0){
  1919. /* The TM request failed and the subsequent FW-reload failed!
  1920. * Fatal error case.
  1921. */
  1922. printk(MYIOC_s_WARN_FMT "Error processing TaskMgmt request (sc=%p)\n",
  1923. hd->ioc->name, SCpnt);
  1924. hd->tmPending = 0;
  1925. hd->tmState = TM_STATE_NONE;
  1926. spin_lock_irq(host_lock);
  1927. return FAILED;
  1928. }
  1929. spin_lock_irq(host_lock);
  1930. return SUCCESS;
  1931. }
  1932. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1933. /**
  1934. * mptscsih_bus_reset - Perform a SCSI BUS_RESET! new_eh variant
  1935. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1936. *
  1937. * (linux scsi_host_template.eh_bus_reset_handler routine)
  1938. *
  1939. * Returns SUCCESS or FAILED.
  1940. */
  1941. static int
  1942. mptscsih_bus_reset(struct scsi_cmnd * SCpnt)
  1943. {
  1944. MPT_SCSI_HOST *hd;
  1945. spinlock_t *host_lock = SCpnt->device->host->host_lock;
  1946. /* If we can't locate our host adapter structure, return FAILED status.
  1947. */
  1948. if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
  1949. dtmprintk((KERN_WARNING MYNAM ": mptscsih_bus_reset: "
  1950. "Can't locate host! (sc=%p)\n",
  1951. SCpnt ) );
  1952. return FAILED;
  1953. }
  1954. printk(KERN_WARNING MYNAM ": %s: >> Attempting bus reset! (sc=%p)\n",
  1955. hd->ioc->name, SCpnt);
  1956. if (hd->timeouts < -1)
  1957. hd->timeouts++;
  1958. /* We are now ready to execute the task management request. */
  1959. spin_unlock_irq(host_lock);
  1960. if (mptscsih_TMHandler(hd, MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
  1961. SCpnt->device->channel, 0, 0, 0, 5 /* 5 second timeout */)
  1962. < 0){
  1963. /* The TM request failed and the subsequent FW-reload failed!
  1964. * Fatal error case.
  1965. */
  1966. printk(MYIOC_s_WARN_FMT
  1967. "Error processing TaskMgmt request (sc=%p)\n",
  1968. hd->ioc->name, SCpnt);
  1969. hd->tmPending = 0;
  1970. hd->tmState = TM_STATE_NONE;
  1971. spin_lock_irq(host_lock);
  1972. return FAILED;
  1973. }
  1974. spin_lock_irq(host_lock);
  1975. return SUCCESS;
  1976. }
  1977. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1978. /**
  1979. * mptscsih_host_reset - Perform a SCSI host adapter RESET!
  1980. * new_eh variant
  1981. * @SCpnt: Pointer to scsi_cmnd structure, IO which reset is due to
  1982. *
  1983. * (linux scsi_host_template.eh_host_reset_handler routine)
  1984. *
  1985. * Returns SUCCESS or FAILED.
  1986. */
  1987. static int
  1988. mptscsih_host_reset(struct scsi_cmnd *SCpnt)
  1989. {
  1990. MPT_SCSI_HOST * hd;
  1991. int status = SUCCESS;
  1992. spinlock_t *host_lock = SCpnt->device->host->host_lock;
  1993. /* If we can't locate the host to reset, then we failed. */
  1994. if ((hd = (MPT_SCSI_HOST *) SCpnt->device->host->hostdata) == NULL){
  1995. dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
  1996. "Can't locate host! (sc=%p)\n",
  1997. SCpnt ) );
  1998. return FAILED;
  1999. }
  2000. printk(KERN_WARNING MYNAM ": %s: >> Attempting host reset! (sc=%p)\n",
  2001. hd->ioc->name, SCpnt);
  2002. /* If our attempts to reset the host failed, then return a failed
  2003. * status. The host will be taken off line by the SCSI mid-layer.
  2004. */
  2005. spin_unlock_irq(host_lock);
  2006. if (mpt_HardResetHandler(hd->ioc, CAN_SLEEP) < 0){
  2007. status = FAILED;
  2008. } else {
  2009. /* Make sure TM pending is cleared and TM state is set to
  2010. * NONE.
  2011. */
  2012. hd->tmPending = 0;
  2013. hd->tmState = TM_STATE_NONE;
  2014. }
  2015. spin_lock_irq(host_lock);
  2016. dtmprintk( ( KERN_WARNING MYNAM ": mptscsih_host_reset: "
  2017. "Status = %s\n",
  2018. (status == SUCCESS) ? "SUCCESS" : "FAILED" ) );
  2019. return status;
  2020. }
  2021. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2022. /**
  2023. * mptscsih_tm_pending_wait - wait for pending task management request to
  2024. * complete.
  2025. * @hd: Pointer to MPT host structure.
  2026. *
  2027. * Returns {SUCCESS,FAILED}.
  2028. */
  2029. static int
  2030. mptscsih_tm_pending_wait(MPT_SCSI_HOST * hd)
  2031. {
  2032. unsigned long flags;
  2033. int loop_count = 4 * 10; /* Wait 10 seconds */
  2034. int status = FAILED;
  2035. do {
  2036. spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
  2037. if (hd->tmState == TM_STATE_NONE) {
  2038. hd->tmState = TM_STATE_IN_PROGRESS;
  2039. hd->tmPending = 1;
  2040. status = SUCCESS;
  2041. spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
  2042. break;
  2043. }
  2044. spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
  2045. msleep(250);
  2046. } while (--loop_count);
  2047. return status;
  2048. }
  2049. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2050. /**
  2051. * mptscsih_tm_wait_for_completion - wait for completion of TM task
  2052. * @hd: Pointer to MPT host structure.
  2053. *
  2054. * Returns {SUCCESS,FAILED}.
  2055. */
  2056. static int
  2057. mptscsih_tm_wait_for_completion(MPT_SCSI_HOST * hd, ulong timeout )
  2058. {
  2059. unsigned long flags;
  2060. int loop_count = 4 * timeout;
  2061. int status = FAILED;
  2062. do {
  2063. spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
  2064. if(hd->tmPending == 0) {
  2065. status = SUCCESS;
  2066. spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
  2067. break;
  2068. }
  2069. spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
  2070. msleep_interruptible(250);
  2071. } while (--loop_count);
  2072. return status;
  2073. }
  2074. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2075. /**
  2076. * mptscsih_taskmgmt_complete - Registered with Fusion MPT base driver
  2077. * @ioc: Pointer to MPT_ADAPTER structure
  2078. * @mf: Pointer to SCSI task mgmt request frame
  2079. * @mr: Pointer to SCSI task mgmt reply frame
  2080. *
  2081. * This routine is called from mptbase.c::mpt_interrupt() at the completion
  2082. * of any SCSI task management request.
  2083. * This routine is registered with the MPT (base) driver at driver
  2084. * load/init time via the mpt_register() API call.
  2085. *
  2086. * Returns 1 indicating alloc'd request frame ptr should be freed.
  2087. */
  2088. static int
  2089. mptscsih_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  2090. {
  2091. SCSITaskMgmtReply_t *pScsiTmReply;
  2092. SCSITaskMgmt_t *pScsiTmReq;
  2093. MPT_SCSI_HOST *hd;
  2094. unsigned long flags;
  2095. u16 iocstatus;
  2096. u8 tmType;
  2097. dtmprintk((MYIOC_s_WARN_FMT "TaskMgmt completed (mf=%p,mr=%p)\n",
  2098. ioc->name, mf, mr));
  2099. if (ioc->sh) {
  2100. /* Depending on the thread, a timer is activated for
  2101. * the TM request. Delete this timer on completion of TM.
  2102. * Decrement count of outstanding TM requests.
  2103. */
  2104. hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
  2105. } else {
  2106. dtmprintk((MYIOC_s_WARN_FMT "TaskMgmt Complete: NULL Scsi Host Ptr\n",
  2107. ioc->name));
  2108. return 1;
  2109. }
  2110. if (mr == NULL) {
  2111. dtmprintk((MYIOC_s_WARN_FMT "ERROR! TaskMgmt Reply: NULL Request %p\n",
  2112. ioc->name, mf));
  2113. return 1;
  2114. } else {
  2115. pScsiTmReply = (SCSITaskMgmtReply_t*)mr;
  2116. pScsiTmReq = (SCSITaskMgmt_t*)mf;
  2117. /* Figure out if this was ABORT_TASK, TARGET_RESET, or BUS_RESET! */
  2118. tmType = pScsiTmReq->TaskType;
  2119. dtmprintk((MYIOC_s_WARN_FMT " TaskType = %d, TerminationCount=%d\n",
  2120. ioc->name, tmType, le32_to_cpu(pScsiTmReply->TerminationCount)));
  2121. DBG_DUMP_TM_REPLY_FRAME((u32 *)pScsiTmReply);
  2122. iocstatus = le16_to_cpu(pScsiTmReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  2123. dtmprintk((MYIOC_s_WARN_FMT " SCSI TaskMgmt (%d) IOCStatus=%04x IOCLogInfo=%08x\n",
  2124. ioc->name, tmType, iocstatus, le32_to_cpu(pScsiTmReply->IOCLogInfo)));
  2125. /* Error? (anything non-zero?) */
  2126. if (iocstatus) {
  2127. /* clear flags and continue.
  2128. */
  2129. if (tmType == MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  2130. hd->abortSCpnt = NULL;
  2131. /* If an internal command is present
  2132. * or the TM failed - reload the FW.
  2133. * FC FW may respond FAILED to an ABORT
  2134. */
  2135. if (tmType == MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS) {
  2136. if ((hd->cmdPtr) ||
  2137. (iocstatus == MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED)) {
  2138. if (mpt_HardResetHandler(ioc, NO_SLEEP) < 0) {
  2139. printk((KERN_WARNING
  2140. " Firmware Reload FAILED!!\n"));
  2141. }
  2142. }
  2143. }
  2144. } else {
  2145. dtmprintk((MYIOC_s_WARN_FMT " TaskMgmt SUCCESS\n", ioc->name));
  2146. hd->abortSCpnt = NULL;
  2147. }
  2148. }
  2149. spin_lock_irqsave(&ioc->FreeQlock, flags);
  2150. hd->tmPending = 0;
  2151. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2152. hd->tmState = TM_STATE_NONE;
  2153. return 1;
  2154. }
  2155. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2156. /*
  2157. * This is anyones guess quite frankly.
  2158. */
  2159. static int
  2160. mptscsih_bios_param(struct scsi_device * sdev, struct block_device *bdev,
  2161. sector_t capacity, int geom[])
  2162. {
  2163. int heads;
  2164. int sectors;
  2165. sector_t cylinders;
  2166. ulong dummy;
  2167. heads = 64;
  2168. sectors = 32;
  2169. dummy = heads * sectors;
  2170. cylinders = capacity;
  2171. sector_div(cylinders,dummy);
  2172. /*
  2173. * Handle extended translation size for logical drives
  2174. * > 1Gb
  2175. */
  2176. if ((ulong)capacity >= 0x200000) {
  2177. heads = 255;
  2178. sectors = 63;
  2179. dummy = heads * sectors;
  2180. cylinders = capacity;
  2181. sector_div(cylinders,dummy);
  2182. }
  2183. /* return result */
  2184. geom[0] = heads;
  2185. geom[1] = sectors;
  2186. geom[2] = cylinders;
  2187. dprintk((KERN_NOTICE
  2188. ": bios_param: Id=%i Lun=%i Channel=%i CHS=%i/%i/%i\n",
  2189. sdev->id, sdev->lun,sdev->channel,(int)cylinders,heads,sectors));
  2190. return 0;
  2191. }
  2192. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2193. /*
  2194. * OS entry point to allow host driver to alloc memory
  2195. * for each scsi device. Called once per device the bus scan.
  2196. * Return non-zero if allocation fails.
  2197. * Init memory once per id (not LUN).
  2198. */
  2199. static int
  2200. mptscsih_slave_alloc(struct scsi_device *device)
  2201. {
  2202. struct Scsi_Host *host = device->host;
  2203. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  2204. VirtDevice *vdev;
  2205. uint target = device->id;
  2206. if (hd == NULL)
  2207. return -ENODEV;
  2208. if ((vdev = hd->Targets[target]) != NULL)
  2209. goto out;
  2210. vdev = kmalloc(sizeof(VirtDevice), GFP_KERNEL);
  2211. if (!vdev) {
  2212. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  2213. hd->ioc->name, sizeof(VirtDevice));
  2214. return -ENOMEM;
  2215. }
  2216. memset(vdev, 0, sizeof(VirtDevice));
  2217. vdev->tflags = MPT_TARGET_FLAGS_Q_YES;
  2218. vdev->ioc_id = hd->ioc->id;
  2219. vdev->target_id = device->id;
  2220. vdev->bus_id = device->channel;
  2221. vdev->raidVolume = 0;
  2222. hd->Targets[device->id] = vdev;
  2223. if (hd->ioc->bus_type == SCSI) {
  2224. if (hd->ioc->spi_data.isRaid & (1 << device->id)) {
  2225. vdev->raidVolume = 1;
  2226. ddvtprintk((KERN_INFO
  2227. "RAID Volume @ id %d\n", device->id));
  2228. }
  2229. } else {
  2230. vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
  2231. }
  2232. out:
  2233. vdev->num_luns++;
  2234. return 0;
  2235. }
  2236. static int mptscsih_is_raid_volume(MPT_SCSI_HOST *hd, uint id)
  2237. {
  2238. int i;
  2239. if (!hd->ioc->spi_data.isRaid || !hd->ioc->spi_data.pIocPg3)
  2240. return 0;
  2241. for (i = 0; i < hd->ioc->spi_data.pIocPg3->NumPhysDisks; i++) {
  2242. if (id == hd->ioc->spi_data.pIocPg3->PhysDisk[i].PhysDiskID)
  2243. return 1;
  2244. }
  2245. return 0;
  2246. }
  2247. /*
  2248. * OS entry point to allow for host driver to free allocated memory
  2249. * Called if no device present or device being unloaded
  2250. */
  2251. static void
  2252. mptscsih_slave_destroy(struct scsi_device *device)
  2253. {
  2254. struct Scsi_Host *host = device->host;
  2255. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  2256. VirtDevice *vdev;
  2257. uint target = device->id;
  2258. uint lun = device->lun;
  2259. if (hd == NULL)
  2260. return;
  2261. mptscsih_search_running_cmds(hd, target, lun);
  2262. vdev = hd->Targets[target];
  2263. vdev->luns[0] &= ~(1 << lun);
  2264. if (--vdev->num_luns)
  2265. return;
  2266. kfree(hd->Targets[target]);
  2267. hd->Targets[target] = NULL;
  2268. if (hd->ioc->bus_type == SCSI) {
  2269. if (mptscsih_is_raid_volume(hd, target)) {
  2270. hd->ioc->spi_data.forceDv |= MPT_SCSICFG_RELOAD_IOC_PG3;
  2271. } else {
  2272. hd->ioc->spi_data.dvStatus[target] =
  2273. MPT_SCSICFG_NEGOTIATE;
  2274. if (!hd->negoNvram) {
  2275. hd->ioc->spi_data.dvStatus[target] |=
  2276. MPT_SCSICFG_DV_NOT_DONE;
  2277. }
  2278. }
  2279. }
  2280. }
  2281. static void
  2282. mptscsih_set_queue_depth(struct scsi_device *device, MPT_SCSI_HOST *hd,
  2283. VirtDevice *pTarget, int qdepth)
  2284. {
  2285. int max_depth;
  2286. int tagged;
  2287. if (hd->ioc->bus_type == SCSI) {
  2288. if (pTarget->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY) {
  2289. if (!(pTarget->tflags & MPT_TARGET_FLAGS_Q_YES))
  2290. max_depth = 1;
  2291. else if (((pTarget->inq_data[0] & 0x1f) == 0x00) &&
  2292. (pTarget->minSyncFactor <= MPT_ULTRA160 ))
  2293. max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
  2294. else
  2295. max_depth = MPT_SCSI_CMD_PER_DEV_LOW;
  2296. } else {
  2297. /* error case - No Inq. Data */
  2298. max_depth = 1;
  2299. }
  2300. } else
  2301. max_depth = MPT_SCSI_CMD_PER_DEV_HIGH;
  2302. if (qdepth > max_depth)
  2303. qdepth = max_depth;
  2304. if (qdepth == 1)
  2305. tagged = 0;
  2306. else
  2307. tagged = MSG_SIMPLE_TAG;
  2308. scsi_adjust_queue_depth(device, tagged, qdepth);
  2309. }
  2310. /*
  2311. * OS entry point to adjust the queue_depths on a per-device basis.
  2312. * Called once per device the bus scan. Use it to force the queue_depth
  2313. * member to 1 if a device does not support Q tags.
  2314. * Return non-zero if fails.
  2315. */
  2316. static int
  2317. mptscsih_slave_configure(struct scsi_device *device)
  2318. {
  2319. struct Scsi_Host *sh = device->host;
  2320. VirtDevice *pTarget;
  2321. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)sh->hostdata;
  2322. if ((hd == NULL) || (hd->Targets == NULL)) {
  2323. return 0;
  2324. }
  2325. dsprintk((MYIOC_s_INFO_FMT
  2326. "device @ %p, id=%d, LUN=%d, channel=%d\n",
  2327. hd->ioc->name, device, device->id, device->lun, device->channel));
  2328. dsprintk((MYIOC_s_INFO_FMT
  2329. "sdtr %d wdtr %d ppr %d inq length=%d\n",
  2330. hd->ioc->name, device->sdtr, device->wdtr,
  2331. device->ppr, device->inquiry_len));
  2332. if (device->id > sh->max_id) {
  2333. /* error case, should never happen */
  2334. scsi_adjust_queue_depth(device, 0, 1);
  2335. goto slave_configure_exit;
  2336. }
  2337. pTarget = hd->Targets[device->id];
  2338. if (pTarget == NULL) {
  2339. /* Driver doesn't know about this device.
  2340. * Kernel may generate a "Dummy Lun 0" which
  2341. * may become a real Lun if a
  2342. * "scsi add-single-device" command is executed
  2343. * while the driver is active (hot-plug a
  2344. * device). LSI Raid controllers need
  2345. * queue_depth set to DEV_HIGH for this reason.
  2346. */
  2347. scsi_adjust_queue_depth(device, MSG_SIMPLE_TAG,
  2348. MPT_SCSI_CMD_PER_DEV_HIGH);
  2349. goto slave_configure_exit;
  2350. }
  2351. mptscsih_initTarget(hd, device->channel, device->id, device->lun,
  2352. device->inquiry, device->inquiry_len );
  2353. mptscsih_set_queue_depth(device, hd, pTarget, MPT_SCSI_CMD_PER_DEV_HIGH);
  2354. dsprintk((MYIOC_s_INFO_FMT
  2355. "Queue depth=%d, tflags=%x\n",
  2356. hd->ioc->name, device->queue_depth, pTarget->tflags));
  2357. dsprintk((MYIOC_s_INFO_FMT
  2358. "negoFlags=%x, maxOffset=%x, SyncFactor=%x\n",
  2359. hd->ioc->name, pTarget->negoFlags, pTarget->maxOffset, pTarget->minSyncFactor));
  2360. slave_configure_exit:
  2361. dsprintk((MYIOC_s_INFO_FMT
  2362. "tagged %d, simple %d, ordered %d\n",
  2363. hd->ioc->name,device->tagged_supported, device->simple_tags,
  2364. device->ordered_tags));
  2365. return 0;
  2366. }
  2367. static ssize_t
  2368. mptscsih_store_queue_depth(struct device *dev, const char *buf, size_t count)
  2369. {
  2370. int depth;
  2371. struct scsi_device *sdev = to_scsi_device(dev);
  2372. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) sdev->host->hostdata;
  2373. VirtDevice *pTarget;
  2374. depth = simple_strtoul(buf, NULL, 0);
  2375. if (depth == 0)
  2376. return -EINVAL;
  2377. pTarget = hd->Targets[sdev->id];
  2378. if (pTarget == NULL)
  2379. return -EINVAL;
  2380. mptscsih_set_queue_depth(sdev, (MPT_SCSI_HOST *) sdev->host->hostdata,
  2381. pTarget, depth);
  2382. return count;
  2383. }
  2384. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2385. /*
  2386. * Private routines...
  2387. */
  2388. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2389. /* Utility function to copy sense data from the scsi_cmnd buffer
  2390. * to the FC and SCSI target structures.
  2391. *
  2392. */
  2393. static void
  2394. copy_sense_data(struct scsi_cmnd *sc, MPT_SCSI_HOST *hd, MPT_FRAME_HDR *mf, SCSIIOReply_t *pScsiReply)
  2395. {
  2396. VirtDevice *target;
  2397. SCSIIORequest_t *pReq;
  2398. u32 sense_count = le32_to_cpu(pScsiReply->SenseCount);
  2399. int index;
  2400. /* Get target structure
  2401. */
  2402. pReq = (SCSIIORequest_t *) mf;
  2403. index = (int) pReq->TargetID;
  2404. target = hd->Targets[index];
  2405. if (sense_count) {
  2406. u8 *sense_data;
  2407. int req_index;
  2408. /* Copy the sense received into the scsi command block. */
  2409. req_index = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  2410. sense_data = ((u8 *)hd->ioc->sense_buf_pool + (req_index * MPT_SENSE_BUFFER_ALLOC));
  2411. memcpy(sc->sense_buffer, sense_data, SNS_LEN(sc));
  2412. /* Log SMART data (asc = 0x5D, non-IM case only) if required.
  2413. */
  2414. if ((hd->ioc->events) && (hd->ioc->eventTypes & (1 << MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE))) {
  2415. if ((sense_data[12] == 0x5D) && (target->raidVolume == 0)) {
  2416. int idx;
  2417. MPT_ADAPTER *ioc = hd->ioc;
  2418. idx = ioc->eventContext % ioc->eventLogSize;
  2419. ioc->events[idx].event = MPI_EVENT_SCSI_DEVICE_STATUS_CHANGE;
  2420. ioc->events[idx].eventContext = ioc->eventContext;
  2421. ioc->events[idx].data[0] = (pReq->LUN[1] << 24) ||
  2422. (MPI_EVENT_SCSI_DEV_STAT_RC_SMART_DATA << 16) ||
  2423. (pReq->Bus << 8) || pReq->TargetID;
  2424. ioc->events[idx].data[1] = (sense_data[13] << 8) || sense_data[12];
  2425. ioc->eventContext++;
  2426. }
  2427. }
  2428. } else {
  2429. dprintk((MYIOC_s_INFO_FMT "Hmmm... SenseData len=0! (?)\n",
  2430. hd->ioc->name));
  2431. }
  2432. }
  2433. static u32
  2434. SCPNT_TO_LOOKUP_IDX(struct scsi_cmnd *sc)
  2435. {
  2436. MPT_SCSI_HOST *hd;
  2437. int i;
  2438. hd = (MPT_SCSI_HOST *) sc->device->host->hostdata;
  2439. for (i = 0; i < hd->ioc->req_depth; i++) {
  2440. if (hd->ScsiLookup[i] == sc) {
  2441. return i;
  2442. }
  2443. }
  2444. return -1;
  2445. }
  2446. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2447. static int
  2448. mptscsih_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  2449. {
  2450. MPT_SCSI_HOST *hd;
  2451. unsigned long flags;
  2452. dtmprintk((KERN_WARNING MYNAM
  2453. ": IOC %s_reset routed to SCSI host driver!\n",
  2454. reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
  2455. reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
  2456. /* If a FW reload request arrives after base installed but
  2457. * before all scsi hosts have been attached, then an alt_ioc
  2458. * may have a NULL sh pointer.
  2459. */
  2460. if ((ioc->sh == NULL) || (ioc->sh->hostdata == NULL))
  2461. return 0;
  2462. else
  2463. hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
  2464. if (reset_phase == MPT_IOC_SETUP_RESET) {
  2465. dtmprintk((MYIOC_s_WARN_FMT "Setup-Diag Reset\n", ioc->name));
  2466. /* Clean Up:
  2467. * 1. Set Hard Reset Pending Flag
  2468. * All new commands go to doneQ
  2469. */
  2470. hd->resetPending = 1;
  2471. } else if (reset_phase == MPT_IOC_PRE_RESET) {
  2472. dtmprintk((MYIOC_s_WARN_FMT "Pre-Diag Reset\n", ioc->name));
  2473. /* 2. Flush running commands
  2474. * Clean ScsiLookup (and associated memory)
  2475. * AND clean mytaskQ
  2476. */
  2477. /* 2b. Reply to OS all known outstanding I/O commands.
  2478. */
  2479. mptscsih_flush_running_cmds(hd);
  2480. /* 2c. If there was an internal command that
  2481. * has not completed, configuration or io request,
  2482. * free these resources.
  2483. */
  2484. if (hd->cmdPtr) {
  2485. del_timer(&hd->timer);
  2486. mpt_free_msg_frame(ioc, hd->cmdPtr);
  2487. }
  2488. dtmprintk((MYIOC_s_WARN_FMT "Pre-Reset complete.\n", ioc->name));
  2489. } else {
  2490. dtmprintk((MYIOC_s_WARN_FMT "Post-Diag Reset\n", ioc->name));
  2491. /* Once a FW reload begins, all new OS commands are
  2492. * redirected to the doneQ w/ a reset status.
  2493. * Init all control structures.
  2494. */
  2495. /* ScsiLookup initialization
  2496. */
  2497. {
  2498. int ii;
  2499. for (ii=0; ii < hd->ioc->req_depth; ii++)
  2500. hd->ScsiLookup[ii] = NULL;
  2501. }
  2502. /* 2. Chain Buffer initialization
  2503. */
  2504. /* 4. Renegotiate to all devices, if SCSI
  2505. */
  2506. if (ioc->bus_type == SCSI) {
  2507. dnegoprintk(("writeSDP1: ALL_IDS USE_NVRAM\n"));
  2508. mptscsih_writeSDP1(hd, 0, 0, MPT_SCSICFG_ALL_IDS | MPT_SCSICFG_USE_NVRAM);
  2509. }
  2510. /* 5. Enable new commands to be posted
  2511. */
  2512. spin_lock_irqsave(&ioc->FreeQlock, flags);
  2513. hd->tmPending = 0;
  2514. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2515. hd->resetPending = 0;
  2516. hd->tmState = TM_STATE_NONE;
  2517. /* 6. If there was an internal command,
  2518. * wake this process up.
  2519. */
  2520. if (hd->cmdPtr) {
  2521. /*
  2522. * Wake up the original calling thread
  2523. */
  2524. hd->pLocal = &hd->localReply;
  2525. hd->pLocal->completion = MPT_SCANDV_DID_RESET;
  2526. scandv_wait_done = 1;
  2527. wake_up(&scandv_waitq);
  2528. hd->cmdPtr = NULL;
  2529. }
  2530. /* 7. Set flag to force DV and re-read IOC Page 3
  2531. */
  2532. if (ioc->bus_type == SCSI) {
  2533. ioc->spi_data.forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
  2534. ddvtprintk(("Set reload IOC Pg3 Flag\n"));
  2535. }
  2536. dtmprintk((MYIOC_s_WARN_FMT "Post-Reset complete.\n", ioc->name));
  2537. }
  2538. return 1; /* currently means nothing really */
  2539. }
  2540. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2541. static int
  2542. mptscsih_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
  2543. {
  2544. MPT_SCSI_HOST *hd;
  2545. u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
  2546. devtprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
  2547. ioc->name, event));
  2548. switch (event) {
  2549. case MPI_EVENT_UNIT_ATTENTION: /* 03 */
  2550. /* FIXME! */
  2551. break;
  2552. case MPI_EVENT_IOC_BUS_RESET: /* 04 */
  2553. case MPI_EVENT_EXT_BUS_RESET: /* 05 */
  2554. hd = NULL;
  2555. if (ioc->sh) {
  2556. hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
  2557. if (hd && (ioc->bus_type == SCSI) && (hd->soft_resets < -1))
  2558. hd->soft_resets++;
  2559. }
  2560. break;
  2561. case MPI_EVENT_LOGOUT: /* 09 */
  2562. /* FIXME! */
  2563. break;
  2564. /*
  2565. * CHECKME! Don't think we need to do
  2566. * anything for these, but...
  2567. */
  2568. case MPI_EVENT_RESCAN: /* 06 */
  2569. case MPI_EVENT_LINK_STATUS_CHANGE: /* 07 */
  2570. case MPI_EVENT_LOOP_STATE_CHANGE: /* 08 */
  2571. /*
  2572. * CHECKME! Falling thru...
  2573. */
  2574. break;
  2575. case MPI_EVENT_INTEGRATED_RAID: /* 0B */
  2576. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  2577. /* negoNvram set to 0 if DV enabled and to USE_NVRAM if
  2578. * if DV disabled. Need to check for target mode.
  2579. */
  2580. hd = NULL;
  2581. if (ioc->sh)
  2582. hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
  2583. if (hd && (ioc->bus_type == SCSI) && (hd->negoNvram == 0)) {
  2584. ScsiCfgData *pSpi;
  2585. Ioc3PhysDisk_t *pPDisk;
  2586. int numPDisk;
  2587. u8 reason;
  2588. u8 physDiskNum;
  2589. reason = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
  2590. if (reason == MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED) {
  2591. /* New or replaced disk.
  2592. * Set DV flag and schedule DV.
  2593. */
  2594. pSpi = &ioc->spi_data;
  2595. physDiskNum = (le32_to_cpu(pEvReply->Data[0]) & 0xFF000000) >> 24;
  2596. ddvtprintk(("DV requested for phys disk id %d\n", physDiskNum));
  2597. if (pSpi->pIocPg3) {
  2598. pPDisk = pSpi->pIocPg3->PhysDisk;
  2599. numPDisk =pSpi->pIocPg3->NumPhysDisks;
  2600. while (numPDisk) {
  2601. if (physDiskNum == pPDisk->PhysDiskNum) {
  2602. pSpi->dvStatus[pPDisk->PhysDiskID] = (MPT_SCSICFG_NEED_DV | MPT_SCSICFG_DV_NOT_DONE);
  2603. pSpi->forceDv = MPT_SCSICFG_NEED_DV;
  2604. ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
  2605. break;
  2606. }
  2607. pPDisk++;
  2608. numPDisk--;
  2609. }
  2610. if (numPDisk == 0) {
  2611. /* The physical disk that needs DV was not found
  2612. * in the stored IOC Page 3. The driver must reload
  2613. * this page. DV routine will set the NEED_DV flag for
  2614. * all phys disks that have DV_NOT_DONE set.
  2615. */
  2616. pSpi->forceDv = MPT_SCSICFG_NEED_DV | MPT_SCSICFG_RELOAD_IOC_PG3;
  2617. ddvtprintk(("phys disk %d not found. Setting reload IOC Pg3 Flag\n", physDiskNum));
  2618. }
  2619. }
  2620. }
  2621. }
  2622. #endif
  2623. #if defined(MPT_DEBUG_DV) || defined(MPT_DEBUG_DV_TINY)
  2624. printk("Raid Event RF: ");
  2625. {
  2626. u32 *m = (u32 *)pEvReply;
  2627. int ii;
  2628. int n = (int)pEvReply->MsgLength;
  2629. for (ii=6; ii < n; ii++)
  2630. printk(" %08x", le32_to_cpu(m[ii]));
  2631. printk("\n");
  2632. }
  2633. #endif
  2634. break;
  2635. case MPI_EVENT_NONE: /* 00 */
  2636. case MPI_EVENT_LOG_DATA: /* 01 */
  2637. case MPI_EVENT_STATE_CHANGE: /* 02 */
  2638. case MPI_EVENT_EVENT_CHANGE: /* 0A */
  2639. default:
  2640. dprintk((KERN_INFO " Ignoring event (=%02Xh)\n", event));
  2641. break;
  2642. }
  2643. return 1; /* currently means nothing really */
  2644. }
  2645. static struct device_attribute mptscsih_queue_depth_attr = {
  2646. .attr = {
  2647. .name = "queue_depth",
  2648. .mode = S_IWUSR,
  2649. },
  2650. .store = mptscsih_store_queue_depth,
  2651. };
  2652. static struct device_attribute *mptscsih_dev_attrs[] = {
  2653. &mptscsih_queue_depth_attr,
  2654. NULL,
  2655. };
  2656. static struct scsi_host_template driver_template = {
  2657. .proc_name = "mptscsih",
  2658. .proc_info = mptscsih_proc_info,
  2659. .name = "MPT SCSI Host",
  2660. .info = mptscsih_info,
  2661. .queuecommand = mptscsih_qcmd,
  2662. .slave_alloc = mptscsih_slave_alloc,
  2663. .slave_configure = mptscsih_slave_configure,
  2664. .slave_destroy = mptscsih_slave_destroy,
  2665. .eh_abort_handler = mptscsih_abort,
  2666. .eh_device_reset_handler = mptscsih_dev_reset,
  2667. .eh_bus_reset_handler = mptscsih_bus_reset,
  2668. .eh_host_reset_handler = mptscsih_host_reset,
  2669. .bios_param = mptscsih_bios_param,
  2670. .can_queue = MPT_SCSI_CAN_QUEUE,
  2671. .this_id = -1,
  2672. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  2673. .max_sectors = 8192,
  2674. .cmd_per_lun = 7,
  2675. .use_clustering = ENABLE_CLUSTERING,
  2676. .sdev_attrs = mptscsih_dev_attrs,
  2677. };
  2678. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2679. /*
  2680. * mptscsih_initTarget - Target, LUN alloc/free functionality.
  2681. * @hd: Pointer to MPT_SCSI_HOST structure
  2682. * @bus_id: Bus number (?)
  2683. * @target_id: SCSI target id
  2684. * @lun: SCSI LUN id
  2685. * @data: Pointer to data
  2686. * @dlen: Number of INQUIRY bytes
  2687. *
  2688. * NOTE: It's only SAFE to call this routine if data points to
  2689. * sane & valid STANDARD INQUIRY data!
  2690. *
  2691. * Allocate and initialize memory for this target.
  2692. * Save inquiry data.
  2693. *
  2694. */
  2695. static void
  2696. mptscsih_initTarget(MPT_SCSI_HOST *hd, int bus_id, int target_id, u8 lun, char *data, int dlen)
  2697. {
  2698. int indexed_lun, lun_index;
  2699. VirtDevice *vdev;
  2700. ScsiCfgData *pSpi;
  2701. char data_56;
  2702. dinitprintk((MYIOC_s_INFO_FMT "initTarget bus=%d id=%d lun=%d hd=%p\n",
  2703. hd->ioc->name, bus_id, target_id, lun, hd));
  2704. /*
  2705. * If the peripheral qualifier filter is enabled then if the target reports a 0x1
  2706. * (i.e. The targer is capable of supporting the specified peripheral device type
  2707. * on this logical unit; however, the physical device is not currently connected
  2708. * to this logical unit) it will be converted to a 0x3 (i.e. The target is not
  2709. * capable of supporting a physical device on this logical unit). This is to work
  2710. * around a bug in th emid-layer in some distributions in which the mid-layer will
  2711. * continue to try to communicate to the LUN and evntually create a dummy LUN.
  2712. */
  2713. if (mpt_pq_filter && dlen && (data[0] & 0xE0))
  2714. data[0] |= 0x40;
  2715. /* Is LUN supported? If so, upper 2 bits will be 0
  2716. * in first byte of inquiry data.
  2717. */
  2718. if (data[0] & 0xe0)
  2719. return;
  2720. if ((vdev = hd->Targets[target_id]) == NULL) {
  2721. return;
  2722. }
  2723. lun_index = (lun >> 5); /* 32 luns per lun_index */
  2724. indexed_lun = (lun % 32);
  2725. vdev->luns[lun_index] |= (1 << indexed_lun);
  2726. if (hd->ioc->bus_type == SCSI) {
  2727. if ((data[0] == TYPE_PROCESSOR) && (hd->ioc->spi_data.Saf_Te)) {
  2728. /* Treat all Processors as SAF-TE if
  2729. * command line option is set */
  2730. vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
  2731. mptscsih_writeIOCPage4(hd, target_id, bus_id);
  2732. }else if ((data[0] == TYPE_PROCESSOR) &&
  2733. !(vdev->tflags & MPT_TARGET_FLAGS_SAF_TE_ISSUED )) {
  2734. if ( dlen > 49 ) {
  2735. vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
  2736. if ( data[44] == 'S' &&
  2737. data[45] == 'A' &&
  2738. data[46] == 'F' &&
  2739. data[47] == '-' &&
  2740. data[48] == 'T' &&
  2741. data[49] == 'E' ) {
  2742. vdev->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
  2743. mptscsih_writeIOCPage4(hd, target_id, bus_id);
  2744. }
  2745. }
  2746. }
  2747. if (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_INQUIRY)) {
  2748. if ( dlen > 8 ) {
  2749. memcpy (vdev->inq_data, data, 8);
  2750. } else {
  2751. memcpy (vdev->inq_data, data, dlen);
  2752. }
  2753. /* If have not done DV, set the DV flag.
  2754. */
  2755. pSpi = &hd->ioc->spi_data;
  2756. if ((data[0] == TYPE_TAPE) || (data[0] == TYPE_PROCESSOR)) {
  2757. if (pSpi->dvStatus[target_id] & MPT_SCSICFG_DV_NOT_DONE)
  2758. pSpi->dvStatus[target_id] |= MPT_SCSICFG_NEED_DV;
  2759. }
  2760. vdev->tflags |= MPT_TARGET_FLAGS_VALID_INQUIRY;
  2761. data_56 = 0x0F; /* Default to full capabilities if Inq data length is < 57 */
  2762. if (dlen > 56) {
  2763. if ( (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_56))) {
  2764. /* Update the target capabilities
  2765. */
  2766. data_56 = data[56];
  2767. vdev->tflags |= MPT_TARGET_FLAGS_VALID_56;
  2768. }
  2769. }
  2770. mptscsih_setTargetNegoParms(hd, vdev, data_56);
  2771. } else {
  2772. /* Initial Inquiry may not request enough data bytes to
  2773. * obtain byte 57. DV will; if target doesn't return
  2774. * at least 57 bytes, data[56] will be zero. */
  2775. if (dlen > 56) {
  2776. if ( (!(vdev->tflags & MPT_TARGET_FLAGS_VALID_56))) {
  2777. /* Update the target capabilities
  2778. */
  2779. data_56 = data[56];
  2780. vdev->tflags |= MPT_TARGET_FLAGS_VALID_56;
  2781. mptscsih_setTargetNegoParms(hd, vdev, data_56);
  2782. }
  2783. }
  2784. }
  2785. }
  2786. }
  2787. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2788. /*
  2789. * Update the target negotiation parameters based on the
  2790. * the Inquiry data, adapter capabilities, and NVRAM settings.
  2791. *
  2792. */
  2793. static void
  2794. mptscsih_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtDevice *target, char byte56)
  2795. {
  2796. ScsiCfgData *pspi_data = &hd->ioc->spi_data;
  2797. int id = (int) target->target_id;
  2798. int nvram;
  2799. VirtDevice *vdev;
  2800. int ii;
  2801. u8 width = MPT_NARROW;
  2802. u8 factor = MPT_ASYNC;
  2803. u8 offset = 0;
  2804. u8 version, nfactor;
  2805. u8 noQas = 1;
  2806. target->negoFlags = pspi_data->noQas;
  2807. /* noQas == 0 => device supports QAS. Need byte 56 of Inq to determine
  2808. * support. If available, default QAS to off and allow enabling.
  2809. * If not available, default QAS to on, turn off for non-disks.
  2810. */
  2811. /* Set flags based on Inquiry data
  2812. */
  2813. version = target->inq_data[2] & 0x07;
  2814. if (version < 2) {
  2815. width = 0;
  2816. factor = MPT_ULTRA2;
  2817. offset = pspi_data->maxSyncOffset;
  2818. target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
  2819. } else {
  2820. if (target->inq_data[7] & 0x20) {
  2821. width = 1;
  2822. }
  2823. if (target->inq_data[7] & 0x10) {
  2824. factor = pspi_data->minSyncFactor;
  2825. if (target->tflags & MPT_TARGET_FLAGS_VALID_56) {
  2826. /* bits 2 & 3 show Clocking support */
  2827. if ((byte56 & 0x0C) == 0)
  2828. factor = MPT_ULTRA2;
  2829. else {
  2830. if ((byte56 & 0x03) == 0)
  2831. factor = MPT_ULTRA160;
  2832. else {
  2833. factor = MPT_ULTRA320;
  2834. if (byte56 & 0x02)
  2835. {
  2836. ddvtprintk((KERN_INFO "Enabling QAS due to byte56=%02x on id=%d!\n", byte56, id));
  2837. noQas = 0;
  2838. }
  2839. if (target->inq_data[0] == TYPE_TAPE) {
  2840. if (byte56 & 0x01)
  2841. target->negoFlags |= MPT_TAPE_NEGO_IDP;
  2842. }
  2843. }
  2844. }
  2845. } else {
  2846. ddvtprintk((KERN_INFO "Enabling QAS on id=%d due to ~TARGET_FLAGS_VALID_56!\n", id));
  2847. noQas = 0;
  2848. }
  2849. offset = pspi_data->maxSyncOffset;
  2850. /* If RAID, never disable QAS
  2851. * else if non RAID, do not disable
  2852. * QAS if bit 1 is set
  2853. * bit 1 QAS support, non-raid only
  2854. * bit 0 IU support
  2855. */
  2856. if (target->raidVolume == 1) {
  2857. noQas = 0;
  2858. }
  2859. } else {
  2860. factor = MPT_ASYNC;
  2861. offset = 0;
  2862. }
  2863. }
  2864. if ( (target->inq_data[7] & 0x02) == 0) {
  2865. target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
  2866. }
  2867. /* Update tflags based on NVRAM settings. (SCSI only)
  2868. */
  2869. if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
  2870. nvram = pspi_data->nvram[id];
  2871. nfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
  2872. if (width)
  2873. width = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
  2874. if (offset > 0) {
  2875. /* Ensure factor is set to the
  2876. * maximum of: adapter, nvram, inquiry
  2877. */
  2878. if (nfactor) {
  2879. if (nfactor < pspi_data->minSyncFactor )
  2880. nfactor = pspi_data->minSyncFactor;
  2881. factor = max(factor, nfactor);
  2882. if (factor == MPT_ASYNC)
  2883. offset = 0;
  2884. } else {
  2885. offset = 0;
  2886. factor = MPT_ASYNC;
  2887. }
  2888. } else {
  2889. factor = MPT_ASYNC;
  2890. }
  2891. }
  2892. /* Make sure data is consistent
  2893. */
  2894. if ((!width) && (factor < MPT_ULTRA2)) {
  2895. factor = MPT_ULTRA2;
  2896. }
  2897. /* Save the data to the target structure.
  2898. */
  2899. target->minSyncFactor = factor;
  2900. target->maxOffset = offset;
  2901. target->maxWidth = width;
  2902. target->tflags |= MPT_TARGET_FLAGS_VALID_NEGO;
  2903. /* Disable unused features.
  2904. */
  2905. if (!width)
  2906. target->negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
  2907. if (!offset)
  2908. target->negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
  2909. if ( factor > MPT_ULTRA320 )
  2910. noQas = 0;
  2911. /* GEM, processor WORKAROUND
  2912. */
  2913. if ((target->inq_data[0] == TYPE_PROCESSOR) || (target->inq_data[0] > 0x08)) {
  2914. target->negoFlags |= (MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC);
  2915. pspi_data->dvStatus[id] |= MPT_SCSICFG_BLK_NEGO;
  2916. } else {
  2917. if (noQas && (pspi_data->noQas == 0)) {
  2918. pspi_data->noQas |= MPT_TARGET_NO_NEGO_QAS;
  2919. target->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
  2920. /* Disable QAS in a mixed configuration case
  2921. */
  2922. ddvtprintk((KERN_INFO "Disabling QAS due to noQas=%02x on id=%d!\n", noQas, id));
  2923. for (ii = 0; ii < id; ii++) {
  2924. if ( (vdev = hd->Targets[ii]) ) {
  2925. vdev->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
  2926. mptscsih_writeSDP1(hd, 0, ii, vdev->negoFlags);
  2927. }
  2928. }
  2929. }
  2930. }
  2931. /* Write SDP1 on this I/O to this target */
  2932. if (pspi_data->dvStatus[id] & MPT_SCSICFG_NEGOTIATE) {
  2933. ddvtprintk((KERN_INFO "MPT_SCSICFG_NEGOTIATE on id=%d!\n", id));
  2934. mptscsih_writeSDP1(hd, 0, id, hd->negoNvram);
  2935. pspi_data->dvStatus[id] &= ~MPT_SCSICFG_NEGOTIATE;
  2936. } else if (pspi_data->dvStatus[id] & MPT_SCSICFG_BLK_NEGO) {
  2937. ddvtprintk((KERN_INFO "MPT_SCSICFG_BLK_NEGO on id=%d!\n", id));
  2938. mptscsih_writeSDP1(hd, 0, id, MPT_SCSICFG_BLK_NEGO);
  2939. pspi_data->dvStatus[id] &= ~MPT_SCSICFG_BLK_NEGO;
  2940. }
  2941. }
  2942. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2943. /* If DV disabled (negoNvram set to USE_NVARM) or if not LUN 0, return.
  2944. * Else set the NEED_DV flag after Read Capacity Issued (disks)
  2945. * or Mode Sense (cdroms).
  2946. *
  2947. * Tapes, initTarget will set this flag on completion of Inquiry command.
  2948. * Called only if DV_NOT_DONE flag is set
  2949. */
  2950. static void mptscsih_set_dvflags(MPT_SCSI_HOST *hd, SCSIIORequest_t *pReq)
  2951. {
  2952. u8 cmd;
  2953. ScsiCfgData *pSpi;
  2954. ddvtprintk((" set_dvflags: id=%d lun=%d negoNvram=%x cmd=%x\n",
  2955. pReq->TargetID, pReq->LUN[1], hd->negoNvram, pReq->CDB[0]));
  2956. if ((pReq->LUN[1] != 0) || (hd->negoNvram != 0))
  2957. return;
  2958. cmd = pReq->CDB[0];
  2959. if ((cmd == READ_CAPACITY) || (cmd == MODE_SENSE)) {
  2960. pSpi = &hd->ioc->spi_data;
  2961. if ((pSpi->isRaid & (1 << pReq->TargetID)) && pSpi->pIocPg3) {
  2962. /* Set NEED_DV for all hidden disks
  2963. */
  2964. Ioc3PhysDisk_t *pPDisk = pSpi->pIocPg3->PhysDisk;
  2965. int numPDisk = pSpi->pIocPg3->NumPhysDisks;
  2966. while (numPDisk) {
  2967. pSpi->dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;
  2968. ddvtprintk(("NEED_DV set for phys disk id %d\n", pPDisk->PhysDiskID));
  2969. pPDisk++;
  2970. numPDisk--;
  2971. }
  2972. }
  2973. pSpi->dvStatus[pReq->TargetID] |= MPT_SCSICFG_NEED_DV;
  2974. ddvtprintk(("NEED_DV set for visible disk id %d\n", pReq->TargetID));
  2975. }
  2976. }
  2977. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2978. /*
  2979. * If no Target, bus reset on 1st I/O. Set the flag to
  2980. * prevent any future negotiations to this device.
  2981. */
  2982. static void mptscsih_no_negotiate(MPT_SCSI_HOST *hd, int target_id)
  2983. {
  2984. if ((hd->Targets) && (hd->Targets[target_id] == NULL))
  2985. hd->ioc->spi_data.dvStatus[target_id] |= MPT_SCSICFG_BLK_NEGO;
  2986. return;
  2987. }
  2988. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2989. /*
  2990. * SCSI Config Page functionality ...
  2991. */
  2992. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  2993. /* mptscsih_setDevicePage1Flags - add Requested and Configuration fields flags
  2994. * based on width, factor and offset parameters.
  2995. * @width: bus width
  2996. * @factor: sync factor
  2997. * @offset: sync offset
  2998. * @requestedPtr: pointer to requested values (updated)
  2999. * @configurationPtr: pointer to configuration values (updated)
  3000. * @flags: flags to block WDTR or SDTR negotiation
  3001. *
  3002. * Return: None.
  3003. *
  3004. * Remark: Called by writeSDP1 and _dv_params
  3005. */
  3006. static void
  3007. mptscsih_setDevicePage1Flags (u8 width, u8 factor, u8 offset, int *requestedPtr, int *configurationPtr, u8 flags)
  3008. {
  3009. u8 nowide = flags & MPT_TARGET_NO_NEGO_WIDE;
  3010. u8 nosync = flags & MPT_TARGET_NO_NEGO_SYNC;
  3011. *configurationPtr = 0;
  3012. *requestedPtr = width ? MPI_SCSIDEVPAGE1_RP_WIDE : 0;
  3013. *requestedPtr |= (offset << 16) | (factor << 8);
  3014. if (width && offset && !nowide && !nosync) {
  3015. if (factor < MPT_ULTRA160) {
  3016. *requestedPtr |= (MPI_SCSIDEVPAGE1_RP_IU + MPI_SCSIDEVPAGE1_RP_DT);
  3017. if ((flags & MPT_TARGET_NO_NEGO_QAS) == 0)
  3018. *requestedPtr |= MPI_SCSIDEVPAGE1_RP_QAS;
  3019. if (flags & MPT_TAPE_NEGO_IDP)
  3020. *requestedPtr |= 0x08000000;
  3021. } else if (factor < MPT_ULTRA2) {
  3022. *requestedPtr |= MPI_SCSIDEVPAGE1_RP_DT;
  3023. }
  3024. }
  3025. if (nowide)
  3026. *configurationPtr |= MPI_SCSIDEVPAGE1_CONF_WDTR_DISALLOWED;
  3027. if (nosync)
  3028. *configurationPtr |= MPI_SCSIDEVPAGE1_CONF_SDTR_DISALLOWED;
  3029. return;
  3030. }
  3031. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3032. /* mptscsih_writeSDP1 - write SCSI Device Page 1
  3033. * @hd: Pointer to a SCSI Host Strucutre
  3034. * @portnum: IOC port number
  3035. * @target_id: writeSDP1 for single ID
  3036. * @flags: MPT_SCSICFG_ALL_IDS, MPT_SCSICFG_USE_NVRAM, MPT_SCSICFG_BLK_NEGO
  3037. *
  3038. * Return: -EFAULT if read of config page header fails
  3039. * or 0 if success.
  3040. *
  3041. * Remark: If a target has been found, the settings from the
  3042. * target structure are used, else the device is set
  3043. * to async/narrow.
  3044. *
  3045. * Remark: Called during init and after a FW reload.
  3046. * Remark: We do not wait for a return, write pages sequentially.
  3047. */
  3048. static int
  3049. mptscsih_writeSDP1(MPT_SCSI_HOST *hd, int portnum, int target_id, int flags)
  3050. {
  3051. MPT_ADAPTER *ioc = hd->ioc;
  3052. Config_t *pReq;
  3053. SCSIDevicePage1_t *pData;
  3054. VirtDevice *pTarget;
  3055. MPT_FRAME_HDR *mf;
  3056. dma_addr_t dataDma;
  3057. u16 req_idx;
  3058. u32 frameOffset;
  3059. u32 requested, configuration, flagsLength;
  3060. int ii, nvram;
  3061. int id = 0, maxid = 0;
  3062. u8 width;
  3063. u8 factor;
  3064. u8 offset;
  3065. u8 bus = 0;
  3066. u8 negoFlags;
  3067. u8 maxwidth, maxoffset, maxfactor;
  3068. if (ioc->spi_data.sdp1length == 0)
  3069. return 0;
  3070. if (flags & MPT_SCSICFG_ALL_IDS) {
  3071. id = 0;
  3072. maxid = ioc->sh->max_id - 1;
  3073. } else if (ioc->sh) {
  3074. id = target_id;
  3075. maxid = min_t(int, id, ioc->sh->max_id - 1);
  3076. }
  3077. for (; id <= maxid; id++) {
  3078. if (id == ioc->pfacts[portnum].PortSCSIID)
  3079. continue;
  3080. /* Use NVRAM to get adapter and target maximums
  3081. * Data over-riden by target structure information, if present
  3082. */
  3083. maxwidth = ioc->spi_data.maxBusWidth;
  3084. maxoffset = ioc->spi_data.maxSyncOffset;
  3085. maxfactor = ioc->spi_data.minSyncFactor;
  3086. if (ioc->spi_data.nvram && (ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
  3087. nvram = ioc->spi_data.nvram[id];
  3088. if (maxwidth)
  3089. maxwidth = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
  3090. if (maxoffset > 0) {
  3091. maxfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
  3092. if (maxfactor == 0) {
  3093. /* Key for async */
  3094. maxfactor = MPT_ASYNC;
  3095. maxoffset = 0;
  3096. } else if (maxfactor < ioc->spi_data.minSyncFactor) {
  3097. maxfactor = ioc->spi_data.minSyncFactor;
  3098. }
  3099. } else
  3100. maxfactor = MPT_ASYNC;
  3101. }
  3102. /* Set the negotiation flags.
  3103. */
  3104. negoFlags = ioc->spi_data.noQas;
  3105. if (!maxwidth)
  3106. negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
  3107. if (!maxoffset)
  3108. negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
  3109. if (flags & MPT_SCSICFG_USE_NVRAM) {
  3110. width = maxwidth;
  3111. factor = maxfactor;
  3112. offset = maxoffset;
  3113. } else {
  3114. width = 0;
  3115. factor = MPT_ASYNC;
  3116. offset = 0;
  3117. //negoFlags = 0;
  3118. //negoFlags = MPT_TARGET_NO_NEGO_SYNC;
  3119. }
  3120. /* If id is not a raid volume, get the updated
  3121. * transmission settings from the target structure.
  3122. */
  3123. if (hd->Targets && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
  3124. width = pTarget->maxWidth;
  3125. factor = pTarget->minSyncFactor;
  3126. offset = pTarget->maxOffset;
  3127. negoFlags = pTarget->negoFlags;
  3128. }
  3129. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  3130. /* Force to async and narrow if DV has not been executed
  3131. * for this ID
  3132. */
  3133. if ((hd->ioc->spi_data.dvStatus[id] & MPT_SCSICFG_DV_NOT_DONE) != 0) {
  3134. width = 0;
  3135. factor = MPT_ASYNC;
  3136. offset = 0;
  3137. }
  3138. #endif
  3139. if (flags & MPT_SCSICFG_BLK_NEGO)
  3140. negoFlags = MPT_TARGET_NO_NEGO_WIDE | MPT_TARGET_NO_NEGO_SYNC;
  3141. mptscsih_setDevicePage1Flags(width, factor, offset,
  3142. &requested, &configuration, negoFlags);
  3143. dnegoprintk(("writeSDP1: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
  3144. target_id, width, factor, offset, negoFlags, requested, configuration));
  3145. /* Get a MF for this command.
  3146. */
  3147. if ((mf = mpt_get_msg_frame(ScsiDoneCtx, ioc)) == NULL) {
  3148. dprintk((MYIOC_s_WARN_FMT "write SDP1: no msg frames!\n",
  3149. ioc->name));
  3150. return -EAGAIN;
  3151. }
  3152. ddvprintk((MYIOC_s_INFO_FMT "WriteSDP1 (mf=%p, id=%d, req=0x%x, cfg=0x%x)\n",
  3153. hd->ioc->name, mf, id, requested, configuration));
  3154. /* Set the request and the data pointers.
  3155. * Request takes: 36 bytes (32 bit SGE)
  3156. * SCSI Device Page 1 requires 16 bytes
  3157. * 40 + 16 <= size of SCSI IO Request = 56 bytes
  3158. * and MF size >= 64 bytes.
  3159. * Place data at end of MF.
  3160. */
  3161. pReq = (Config_t *)mf;
  3162. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  3163. frameOffset = ioc->req_sz - sizeof(SCSIDevicePage1_t);
  3164. pData = (SCSIDevicePage1_t *)((u8 *) mf + frameOffset);
  3165. dataDma = ioc->req_frames_dma + (req_idx * ioc->req_sz) + frameOffset;
  3166. /* Complete the request frame (same for all requests).
  3167. */
  3168. pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  3169. pReq->Reserved = 0;
  3170. pReq->ChainOffset = 0;
  3171. pReq->Function = MPI_FUNCTION_CONFIG;
  3172. pReq->ExtPageLength = 0;
  3173. pReq->ExtPageType = 0;
  3174. pReq->MsgFlags = 0;
  3175. for (ii=0; ii < 8; ii++) {
  3176. pReq->Reserved2[ii] = 0;
  3177. }
  3178. pReq->Header.PageVersion = ioc->spi_data.sdp1version;
  3179. pReq->Header.PageLength = ioc->spi_data.sdp1length;
  3180. pReq->Header.PageNumber = 1;
  3181. pReq->Header.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  3182. pReq->PageAddress = cpu_to_le32(id | (bus << 8 ));
  3183. /* Add a SGE to the config request.
  3184. */
  3185. flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE | ioc->spi_data.sdp1length * 4;
  3186. mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
  3187. /* Set up the common data portion
  3188. */
  3189. pData->Header.PageVersion = pReq->Header.PageVersion;
  3190. pData->Header.PageLength = pReq->Header.PageLength;
  3191. pData->Header.PageNumber = pReq->Header.PageNumber;
  3192. pData->Header.PageType = pReq->Header.PageType;
  3193. pData->RequestedParameters = cpu_to_le32(requested);
  3194. pData->Reserved = 0;
  3195. pData->Configuration = cpu_to_le32(configuration);
  3196. dprintk((MYIOC_s_INFO_FMT
  3197. "write SDP1: id %d pgaddr 0x%x req 0x%x config 0x%x\n",
  3198. ioc->name, id, (id | (bus<<8)),
  3199. requested, configuration));
  3200. mpt_put_msg_frame(ScsiDoneCtx, ioc, mf);
  3201. }
  3202. return 0;
  3203. }
  3204. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3205. /* mptscsih_writeIOCPage4 - write IOC Page 4
  3206. * @hd: Pointer to a SCSI Host Structure
  3207. * @target_id: write IOC Page4 for this ID & Bus
  3208. *
  3209. * Return: -EAGAIN if unable to obtain a Message Frame
  3210. * or 0 if success.
  3211. *
  3212. * Remark: We do not wait for a return, write pages sequentially.
  3213. */
  3214. static int
  3215. mptscsih_writeIOCPage4(MPT_SCSI_HOST *hd, int target_id, int bus)
  3216. {
  3217. MPT_ADAPTER *ioc = hd->ioc;
  3218. Config_t *pReq;
  3219. IOCPage4_t *IOCPage4Ptr;
  3220. MPT_FRAME_HDR *mf;
  3221. dma_addr_t dataDma;
  3222. u16 req_idx;
  3223. u32 frameOffset;
  3224. u32 flagsLength;
  3225. int ii;
  3226. /* Get a MF for this command.
  3227. */
  3228. if ((mf = mpt_get_msg_frame(ScsiDoneCtx, ioc)) == NULL) {
  3229. dprintk((MYIOC_s_WARN_FMT "writeIOCPage4 : no msg frames!\n",
  3230. ioc->name));
  3231. return -EAGAIN;
  3232. }
  3233. /* Set the request and the data pointers.
  3234. * Place data at end of MF.
  3235. */
  3236. pReq = (Config_t *)mf;
  3237. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  3238. frameOffset = ioc->req_sz - sizeof(IOCPage4_t);
  3239. /* Complete the request frame (same for all requests).
  3240. */
  3241. pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  3242. pReq->Reserved = 0;
  3243. pReq->ChainOffset = 0;
  3244. pReq->Function = MPI_FUNCTION_CONFIG;
  3245. pReq->ExtPageLength = 0;
  3246. pReq->ExtPageType = 0;
  3247. pReq->MsgFlags = 0;
  3248. for (ii=0; ii < 8; ii++) {
  3249. pReq->Reserved2[ii] = 0;
  3250. }
  3251. IOCPage4Ptr = ioc->spi_data.pIocPg4;
  3252. dataDma = ioc->spi_data.IocPg4_dma;
  3253. ii = IOCPage4Ptr->ActiveSEP++;
  3254. IOCPage4Ptr->SEP[ii].SEPTargetID = target_id;
  3255. IOCPage4Ptr->SEP[ii].SEPBus = bus;
  3256. pReq->Header = IOCPage4Ptr->Header;
  3257. pReq->PageAddress = cpu_to_le32(target_id | (bus << 8 ));
  3258. /* Add a SGE to the config request.
  3259. */
  3260. flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE |
  3261. (IOCPage4Ptr->Header.PageLength + ii) * 4;
  3262. mpt_add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
  3263. dinitprintk((MYIOC_s_INFO_FMT
  3264. "writeIOCPage4: MaxSEP=%d ActiveSEP=%d id=%d bus=%d\n",
  3265. ioc->name, IOCPage4Ptr->MaxSEP, IOCPage4Ptr->ActiveSEP, target_id, bus));
  3266. mpt_put_msg_frame(ScsiDoneCtx, ioc, mf);
  3267. return 0;
  3268. }
  3269. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3270. /*
  3271. * Bus Scan and Domain Validation functionality ...
  3272. */
  3273. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3274. /*
  3275. * mptscsih_scandv_complete - Scan and DV callback routine registered
  3276. * to Fustion MPT (base) driver.
  3277. *
  3278. * @ioc: Pointer to MPT_ADAPTER structure
  3279. * @mf: Pointer to original MPT request frame
  3280. * @mr: Pointer to MPT reply frame (NULL if TurboReply)
  3281. *
  3282. * This routine is called from mpt.c::mpt_interrupt() at the completion
  3283. * of any SCSI IO request.
  3284. * This routine is registered with the Fusion MPT (base) driver at driver
  3285. * load/init time via the mpt_register() API call.
  3286. *
  3287. * Returns 1 indicating alloc'd request frame ptr should be freed.
  3288. *
  3289. * Remark: Sets a completion code and (possibly) saves sense data
  3290. * in the IOC member localReply structure.
  3291. * Used ONLY for DV and other internal commands.
  3292. */
  3293. static int
  3294. mptscsih_scandv_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  3295. {
  3296. MPT_SCSI_HOST *hd;
  3297. SCSIIORequest_t *pReq;
  3298. int completionCode;
  3299. u16 req_idx;
  3300. if ((mf == NULL) ||
  3301. (mf >= MPT_INDEX_2_MFPTR(ioc, ioc->req_depth))) {
  3302. printk(MYIOC_s_ERR_FMT
  3303. "ScanDvComplete, %s req frame ptr! (=%p)\n",
  3304. ioc->name, mf?"BAD":"NULL", (void *) mf);
  3305. goto wakeup;
  3306. }
  3307. hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
  3308. del_timer(&hd->timer);
  3309. req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  3310. hd->ScsiLookup[req_idx] = NULL;
  3311. pReq = (SCSIIORequest_t *) mf;
  3312. if (mf != hd->cmdPtr) {
  3313. printk(MYIOC_s_WARN_FMT "ScanDvComplete (mf=%p, cmdPtr=%p, idx=%d)\n",
  3314. hd->ioc->name, (void *)mf, (void *) hd->cmdPtr, req_idx);
  3315. }
  3316. hd->cmdPtr = NULL;
  3317. ddvprintk((MYIOC_s_INFO_FMT "ScanDvComplete (mf=%p,mr=%p,idx=%d)\n",
  3318. hd->ioc->name, mf, mr, req_idx));
  3319. hd->pLocal = &hd->localReply;
  3320. hd->pLocal->scsiStatus = 0;
  3321. /* If target struct exists, clear sense valid flag.
  3322. */
  3323. if (mr == NULL) {
  3324. completionCode = MPT_SCANDV_GOOD;
  3325. } else {
  3326. SCSIIOReply_t *pReply;
  3327. u16 status;
  3328. u8 scsi_status;
  3329. pReply = (SCSIIOReply_t *) mr;
  3330. status = le16_to_cpu(pReply->IOCStatus) & MPI_IOCSTATUS_MASK;
  3331. scsi_status = pReply->SCSIStatus;
  3332. ddvtprintk((KERN_NOTICE " IOCStatus=%04xh, SCSIState=%02xh, SCSIStatus=%02xh, IOCLogInfo=%08xh\n",
  3333. status, pReply->SCSIState, scsi_status,
  3334. le32_to_cpu(pReply->IOCLogInfo)));
  3335. switch(status) {
  3336. case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE: /* 0x0043 */
  3337. completionCode = MPT_SCANDV_SELECTION_TIMEOUT;
  3338. break;
  3339. case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR: /* 0x0046 */
  3340. case MPI_IOCSTATUS_SCSI_TASK_TERMINATED: /* 0x0048 */
  3341. case MPI_IOCSTATUS_SCSI_IOC_TERMINATED: /* 0x004B */
  3342. case MPI_IOCSTATUS_SCSI_EXT_TERMINATED: /* 0x004C */
  3343. completionCode = MPT_SCANDV_DID_RESET;
  3344. break;
  3345. case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN: /* 0x0045 */
  3346. case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR: /* 0x0040 */
  3347. case MPI_IOCSTATUS_SUCCESS: /* 0x0000 */
  3348. if (pReply->Function == MPI_FUNCTION_CONFIG) {
  3349. ConfigReply_t *pr = (ConfigReply_t *)mr;
  3350. completionCode = MPT_SCANDV_GOOD;
  3351. hd->pLocal->header.PageVersion = pr->Header.PageVersion;
  3352. hd->pLocal->header.PageLength = pr->Header.PageLength;
  3353. hd->pLocal->header.PageNumber = pr->Header.PageNumber;
  3354. hd->pLocal->header.PageType = pr->Header.PageType;
  3355. } else if (pReply->Function == MPI_FUNCTION_RAID_ACTION) {
  3356. /* If the RAID Volume request is successful,
  3357. * return GOOD, else indicate that
  3358. * some type of error occurred.
  3359. */
  3360. MpiRaidActionReply_t *pr = (MpiRaidActionReply_t *)mr;
  3361. if (pr->ActionStatus == MPI_RAID_ACTION_ASTATUS_SUCCESS)
  3362. completionCode = MPT_SCANDV_GOOD;
  3363. else
  3364. completionCode = MPT_SCANDV_SOME_ERROR;
  3365. } else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_VALID) {
  3366. u8 *sense_data;
  3367. int sz;
  3368. /* save sense data in global structure
  3369. */
  3370. completionCode = MPT_SCANDV_SENSE;
  3371. hd->pLocal->scsiStatus = scsi_status;
  3372. sense_data = ((u8 *)hd->ioc->sense_buf_pool +
  3373. (req_idx * MPT_SENSE_BUFFER_ALLOC));
  3374. sz = min_t(int, pReq->SenseBufferLength,
  3375. SCSI_STD_SENSE_BYTES);
  3376. memcpy(hd->pLocal->sense, sense_data, sz);
  3377. ddvprintk((KERN_NOTICE " Check Condition, sense ptr %p\n",
  3378. sense_data));
  3379. } else if (pReply->SCSIState & MPI_SCSI_STATE_AUTOSENSE_FAILED) {
  3380. if (pReq->CDB[0] == INQUIRY)
  3381. completionCode = MPT_SCANDV_ISSUE_SENSE;
  3382. else
  3383. completionCode = MPT_SCANDV_DID_RESET;
  3384. }
  3385. else if (pReply->SCSIState & MPI_SCSI_STATE_NO_SCSI_STATUS)
  3386. completionCode = MPT_SCANDV_DID_RESET;
  3387. else if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
  3388. completionCode = MPT_SCANDV_DID_RESET;
  3389. else {
  3390. completionCode = MPT_SCANDV_GOOD;
  3391. hd->pLocal->scsiStatus = scsi_status;
  3392. }
  3393. break;
  3394. case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR: /* 0x0047 */
  3395. if (pReply->SCSIState & MPI_SCSI_STATE_TERMINATED)
  3396. completionCode = MPT_SCANDV_DID_RESET;
  3397. else
  3398. completionCode = MPT_SCANDV_SOME_ERROR;
  3399. break;
  3400. default:
  3401. completionCode = MPT_SCANDV_SOME_ERROR;
  3402. break;
  3403. } /* switch(status) */
  3404. ddvtprintk((KERN_NOTICE " completionCode set to %08xh\n",
  3405. completionCode));
  3406. } /* end of address reply case */
  3407. hd->pLocal->completion = completionCode;
  3408. /* MF and RF are freed in mpt_interrupt
  3409. */
  3410. wakeup:
  3411. /* Free Chain buffers (will never chain) in scan or dv */
  3412. //mptscsih_freeChainBuffers(ioc, req_idx);
  3413. /*
  3414. * Wake up the original calling thread
  3415. */
  3416. scandv_wait_done = 1;
  3417. wake_up(&scandv_waitq);
  3418. return 1;
  3419. }
  3420. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3421. /* mptscsih_timer_expired - Call back for timer process.
  3422. * Used only for dv functionality.
  3423. * @data: Pointer to MPT_SCSI_HOST recast as an unsigned long
  3424. *
  3425. */
  3426. static void mptscsih_timer_expired(unsigned long data)
  3427. {
  3428. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *) data;
  3429. ddvprintk((MYIOC_s_WARN_FMT "Timer Expired! Cmd %p\n", hd->ioc->name, hd->cmdPtr));
  3430. if (hd->cmdPtr) {
  3431. MPIHeader_t *cmd = (MPIHeader_t *)hd->cmdPtr;
  3432. if (cmd->Function == MPI_FUNCTION_SCSI_IO_REQUEST) {
  3433. /* Desire to issue a task management request here.
  3434. * TM requests MUST be single threaded.
  3435. * If old eh code and no TM current, issue request.
  3436. * If new eh code, do nothing. Wait for OS cmd timeout
  3437. * for bus reset.
  3438. */
  3439. ddvtprintk((MYIOC_s_NOTE_FMT "DV Cmd Timeout: NoOp\n", hd->ioc->name));
  3440. } else {
  3441. /* Perform a FW reload */
  3442. if (mpt_HardResetHandler(hd->ioc, NO_SLEEP) < 0) {
  3443. printk(MYIOC_s_WARN_FMT "Firmware Reload FAILED!\n", hd->ioc->name);
  3444. }
  3445. }
  3446. } else {
  3447. /* This should NEVER happen */
  3448. printk(MYIOC_s_WARN_FMT "Null cmdPtr!!!!\n", hd->ioc->name);
  3449. }
  3450. /* No more processing.
  3451. * TM call will generate an interrupt for SCSI TM Management.
  3452. * The FW will reply to all outstanding commands, callback will finish cleanup.
  3453. * Hard reset clean-up will free all resources.
  3454. */
  3455. ddvprintk((MYIOC_s_WARN_FMT "Timer Expired Complete!\n", hd->ioc->name));
  3456. return;
  3457. }
  3458. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  3459. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3460. /* mptscsih_do_raid - Format and Issue a RAID volume request message.
  3461. * @hd: Pointer to scsi host structure
  3462. * @action: What do be done.
  3463. * @id: Logical target id.
  3464. * @bus: Target locations bus.
  3465. *
  3466. * Returns: < 0 on a fatal error
  3467. * 0 on success
  3468. *
  3469. * Remark: Wait to return until reply processed by the ISR.
  3470. */
  3471. static int
  3472. mptscsih_do_raid(MPT_SCSI_HOST *hd, u8 action, INTERNAL_CMD *io)
  3473. {
  3474. MpiRaidActionRequest_t *pReq;
  3475. MPT_FRAME_HDR *mf;
  3476. int in_isr;
  3477. in_isr = in_interrupt();
  3478. if (in_isr) {
  3479. dprintk((MYIOC_s_WARN_FMT "Internal raid request not allowed in ISR context!\n",
  3480. hd->ioc->name));
  3481. return -EPERM;
  3482. }
  3483. /* Get and Populate a free Frame
  3484. */
  3485. if ((mf = mpt_get_msg_frame(ScsiScanDvCtx, hd->ioc)) == NULL) {
  3486. ddvprintk((MYIOC_s_WARN_FMT "_do_raid: no msg frames!\n",
  3487. hd->ioc->name));
  3488. return -EAGAIN;
  3489. }
  3490. pReq = (MpiRaidActionRequest_t *)mf;
  3491. pReq->Action = action;
  3492. pReq->Reserved1 = 0;
  3493. pReq->ChainOffset = 0;
  3494. pReq->Function = MPI_FUNCTION_RAID_ACTION;
  3495. pReq->VolumeID = io->id;
  3496. pReq->VolumeBus = io->bus;
  3497. pReq->PhysDiskNum = io->physDiskNum;
  3498. pReq->MsgFlags = 0;
  3499. pReq->Reserved2 = 0;
  3500. pReq->ActionDataWord = 0; /* Reserved for this action */
  3501. //pReq->ActionDataSGE = 0;
  3502. mpt_add_sge((char *)&pReq->ActionDataSGE,
  3503. MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
  3504. ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action %x id %d\n",
  3505. hd->ioc->name, action, io->id));
  3506. hd->pLocal = NULL;
  3507. hd->timer.expires = jiffies + HZ*10; /* 10 second timeout */
  3508. scandv_wait_done = 0;
  3509. /* Save cmd pointer, for resource free if timeout or
  3510. * FW reload occurs
  3511. */
  3512. hd->cmdPtr = mf;
  3513. add_timer(&hd->timer);
  3514. mpt_put_msg_frame(ScsiScanDvCtx, hd->ioc, mf);
  3515. wait_event(scandv_waitq, scandv_wait_done);
  3516. if ((hd->pLocal == NULL) || (hd->pLocal->completion != MPT_SCANDV_GOOD))
  3517. return -1;
  3518. return 0;
  3519. }
  3520. #endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
  3521. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3522. /**
  3523. * mptscsih_do_cmd - Do internal command.
  3524. * @hd: MPT_SCSI_HOST pointer
  3525. * @io: INTERNAL_CMD pointer.
  3526. *
  3527. * Issue the specified internally generated command and do command
  3528. * specific cleanup. For bus scan / DV only.
  3529. * NOTES: If command is Inquiry and status is good,
  3530. * initialize a target structure, save the data
  3531. *
  3532. * Remark: Single threaded access only.
  3533. *
  3534. * Return:
  3535. * < 0 if an illegal command or no resources
  3536. *
  3537. * 0 if good
  3538. *
  3539. * > 0 if command complete but some type of completion error.
  3540. */
  3541. static int
  3542. mptscsih_do_cmd(MPT_SCSI_HOST *hd, INTERNAL_CMD *io)
  3543. {
  3544. MPT_FRAME_HDR *mf;
  3545. SCSIIORequest_t *pScsiReq;
  3546. SCSIIORequest_t ReqCopy;
  3547. int my_idx, ii, dir;
  3548. int rc, cmdTimeout;
  3549. int in_isr;
  3550. char cmdLen;
  3551. char CDB[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  3552. char cmd = io->cmd;
  3553. in_isr = in_interrupt();
  3554. if (in_isr) {
  3555. dprintk((MYIOC_s_WARN_FMT "Internal SCSI IO request not allowed in ISR context!\n",
  3556. hd->ioc->name));
  3557. return -EPERM;
  3558. }
  3559. /* Set command specific information
  3560. */
  3561. switch (cmd) {
  3562. case INQUIRY:
  3563. cmdLen = 6;
  3564. dir = MPI_SCSIIO_CONTROL_READ;
  3565. CDB[0] = cmd;
  3566. CDB[4] = io->size;
  3567. cmdTimeout = 10;
  3568. break;
  3569. case TEST_UNIT_READY:
  3570. cmdLen = 6;
  3571. dir = MPI_SCSIIO_CONTROL_READ;
  3572. cmdTimeout = 10;
  3573. break;
  3574. case START_STOP:
  3575. cmdLen = 6;
  3576. dir = MPI_SCSIIO_CONTROL_READ;
  3577. CDB[0] = cmd;
  3578. CDB[4] = 1; /*Spin up the disk */
  3579. cmdTimeout = 15;
  3580. break;
  3581. case REQUEST_SENSE:
  3582. cmdLen = 6;
  3583. CDB[0] = cmd;
  3584. CDB[4] = io->size;
  3585. dir = MPI_SCSIIO_CONTROL_READ;
  3586. cmdTimeout = 10;
  3587. break;
  3588. case READ_BUFFER:
  3589. cmdLen = 10;
  3590. dir = MPI_SCSIIO_CONTROL_READ;
  3591. CDB[0] = cmd;
  3592. if (io->flags & MPT_ICFLAG_ECHO) {
  3593. CDB[1] = 0x0A;
  3594. } else {
  3595. CDB[1] = 0x02;
  3596. }
  3597. if (io->flags & MPT_ICFLAG_BUF_CAP) {
  3598. CDB[1] |= 0x01;
  3599. }
  3600. CDB[6] = (io->size >> 16) & 0xFF;
  3601. CDB[7] = (io->size >> 8) & 0xFF;
  3602. CDB[8] = io->size & 0xFF;
  3603. cmdTimeout = 10;
  3604. break;
  3605. case WRITE_BUFFER:
  3606. cmdLen = 10;
  3607. dir = MPI_SCSIIO_CONTROL_WRITE;
  3608. CDB[0] = cmd;
  3609. if (io->flags & MPT_ICFLAG_ECHO) {
  3610. CDB[1] = 0x0A;
  3611. } else {
  3612. CDB[1] = 0x02;
  3613. }
  3614. CDB[6] = (io->size >> 16) & 0xFF;
  3615. CDB[7] = (io->size >> 8) & 0xFF;
  3616. CDB[8] = io->size & 0xFF;
  3617. cmdTimeout = 10;
  3618. break;
  3619. case RESERVE:
  3620. cmdLen = 6;
  3621. dir = MPI_SCSIIO_CONTROL_READ;
  3622. CDB[0] = cmd;
  3623. cmdTimeout = 10;
  3624. break;
  3625. case RELEASE:
  3626. cmdLen = 6;
  3627. dir = MPI_SCSIIO_CONTROL_READ;
  3628. CDB[0] = cmd;
  3629. cmdTimeout = 10;
  3630. break;
  3631. case SYNCHRONIZE_CACHE:
  3632. cmdLen = 10;
  3633. dir = MPI_SCSIIO_CONTROL_READ;
  3634. CDB[0] = cmd;
  3635. // CDB[1] = 0x02; /* set immediate bit */
  3636. cmdTimeout = 10;
  3637. break;
  3638. default:
  3639. /* Error Case */
  3640. return -EFAULT;
  3641. }
  3642. /* Get and Populate a free Frame
  3643. */
  3644. if ((mf = mpt_get_msg_frame(ScsiScanDvCtx, hd->ioc)) == NULL) {
  3645. ddvprintk((MYIOC_s_WARN_FMT "No msg frames!\n",
  3646. hd->ioc->name));
  3647. return -EBUSY;
  3648. }
  3649. pScsiReq = (SCSIIORequest_t *) mf;
  3650. /* Get the request index */
  3651. my_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
  3652. ADD_INDEX_LOG(my_idx); /* for debug */
  3653. if (io->flags & MPT_ICFLAG_PHYS_DISK) {
  3654. pScsiReq->TargetID = io->physDiskNum;
  3655. pScsiReq->Bus = 0;
  3656. pScsiReq->ChainOffset = 0;
  3657. pScsiReq->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  3658. } else {
  3659. pScsiReq->TargetID = io->id;
  3660. pScsiReq->Bus = io->bus;
  3661. pScsiReq->ChainOffset = 0;
  3662. pScsiReq->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
  3663. }
  3664. pScsiReq->CDBLength = cmdLen;
  3665. pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
  3666. pScsiReq->Reserved = 0;
  3667. pScsiReq->MsgFlags = mpt_msg_flags();
  3668. /* MsgContext set in mpt_get_msg_fram call */
  3669. for (ii=0; ii < 8; ii++)
  3670. pScsiReq->LUN[ii] = 0;
  3671. pScsiReq->LUN[1] = io->lun;
  3672. if (io->flags & MPT_ICFLAG_TAGGED_CMD)
  3673. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_SIMPLEQ);
  3674. else
  3675. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
  3676. if (cmd == REQUEST_SENSE) {
  3677. pScsiReq->Control = cpu_to_le32(dir | MPI_SCSIIO_CONTROL_UNTAGGED);
  3678. ddvprintk((MYIOC_s_INFO_FMT "Untagged! 0x%2x\n",
  3679. hd->ioc->name, cmd));
  3680. }
  3681. for (ii=0; ii < 16; ii++)
  3682. pScsiReq->CDB[ii] = CDB[ii];
  3683. pScsiReq->DataLength = cpu_to_le32(io->size);
  3684. pScsiReq->SenseBufferLowAddr = cpu_to_le32(hd->ioc->sense_buf_low_dma
  3685. + (my_idx * MPT_SENSE_BUFFER_ALLOC));
  3686. ddvprintk((MYIOC_s_INFO_FMT "Sending Command 0x%x for (%d:%d:%d)\n",
  3687. hd->ioc->name, cmd, io->bus, io->id, io->lun));
  3688. if (dir == MPI_SCSIIO_CONTROL_READ) {
  3689. mpt_add_sge((char *) &pScsiReq->SGL,
  3690. MPT_SGE_FLAGS_SSIMPLE_READ | io->size,
  3691. io->data_dma);
  3692. } else {
  3693. mpt_add_sge((char *) &pScsiReq->SGL,
  3694. MPT_SGE_FLAGS_SSIMPLE_WRITE | io->size,
  3695. io->data_dma);
  3696. }
  3697. /* The ISR will free the request frame, but we need
  3698. * the information to initialize the target. Duplicate.
  3699. */
  3700. memcpy(&ReqCopy, pScsiReq, sizeof(SCSIIORequest_t));
  3701. /* Issue this command after:
  3702. * finish init
  3703. * add timer
  3704. * Wait until the reply has been received
  3705. * ScsiScanDvCtx callback function will
  3706. * set hd->pLocal;
  3707. * set scandv_wait_done and call wake_up
  3708. */
  3709. hd->pLocal = NULL;
  3710. hd->timer.expires = jiffies + HZ*cmdTimeout;
  3711. scandv_wait_done = 0;
  3712. /* Save cmd pointer, for resource free if timeout or
  3713. * FW reload occurs
  3714. */
  3715. hd->cmdPtr = mf;
  3716. add_timer(&hd->timer);
  3717. mpt_put_msg_frame(ScsiScanDvCtx, hd->ioc, mf);
  3718. wait_event(scandv_waitq, scandv_wait_done);
  3719. if (hd->pLocal) {
  3720. rc = hd->pLocal->completion;
  3721. hd->pLocal->skip = 0;
  3722. /* Always set fatal error codes in some cases.
  3723. */
  3724. if (rc == MPT_SCANDV_SELECTION_TIMEOUT)
  3725. rc = -ENXIO;
  3726. else if (rc == MPT_SCANDV_SOME_ERROR)
  3727. rc = -rc;
  3728. } else {
  3729. rc = -EFAULT;
  3730. /* This should never happen. */
  3731. ddvprintk((MYIOC_s_INFO_FMT "_do_cmd: Null pLocal!!!\n",
  3732. hd->ioc->name));
  3733. }
  3734. return rc;
  3735. }
  3736. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3737. /**
  3738. * mptscsih_synchronize_cache - Send SYNCHRONIZE_CACHE to all disks.
  3739. * @hd: Pointer to MPT_SCSI_HOST structure
  3740. * @portnum: IOC port number
  3741. *
  3742. * Uses the ISR, but with special processing.
  3743. * MUST be single-threaded.
  3744. *
  3745. * Return: 0 on completion
  3746. */
  3747. static int
  3748. mptscsih_synchronize_cache(MPT_SCSI_HOST *hd, int portnum)
  3749. {
  3750. MPT_ADAPTER *ioc= hd->ioc;
  3751. VirtDevice *pTarget;
  3752. SCSIDevicePage1_t *pcfg1Data = NULL;
  3753. INTERNAL_CMD iocmd;
  3754. CONFIGPARMS cfg;
  3755. dma_addr_t cfg1_dma_addr = -1;
  3756. ConfigPageHeader_t header1;
  3757. int bus = 0;
  3758. int id = 0;
  3759. int lun;
  3760. int indexed_lun, lun_index;
  3761. int hostId = ioc->pfacts[portnum].PortSCSIID;
  3762. int max_id;
  3763. int requested, configuration, data;
  3764. int doConfig = 0;
  3765. u8 flags, factor;
  3766. max_id = ioc->sh->max_id - 1;
  3767. /* Following parameters will not change
  3768. * in this routine.
  3769. */
  3770. iocmd.cmd = SYNCHRONIZE_CACHE;
  3771. iocmd.flags = 0;
  3772. iocmd.physDiskNum = -1;
  3773. iocmd.data = NULL;
  3774. iocmd.data_dma = -1;
  3775. iocmd.size = 0;
  3776. iocmd.rsvd = iocmd.rsvd2 = 0;
  3777. /* No SCSI hosts
  3778. */
  3779. if (hd->Targets == NULL)
  3780. return 0;
  3781. /* Skip the host
  3782. */
  3783. if (id == hostId)
  3784. id++;
  3785. /* Write SDP1 for all SCSI devices
  3786. * Alloc memory and set up config buffer
  3787. */
  3788. if (ioc->bus_type == SCSI) {
  3789. if (ioc->spi_data.sdp1length > 0) {
  3790. pcfg1Data = (SCSIDevicePage1_t *)pci_alloc_consistent(ioc->pcidev,
  3791. ioc->spi_data.sdp1length * 4, &cfg1_dma_addr);
  3792. if (pcfg1Data != NULL) {
  3793. doConfig = 1;
  3794. header1.PageVersion = ioc->spi_data.sdp1version;
  3795. header1.PageLength = ioc->spi_data.sdp1length;
  3796. header1.PageNumber = 1;
  3797. header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  3798. cfg.hdr = &header1;
  3799. cfg.physAddr = cfg1_dma_addr;
  3800. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  3801. cfg.dir = 1;
  3802. cfg.timeout = 0;
  3803. }
  3804. }
  3805. }
  3806. /* loop through all devices on this port
  3807. */
  3808. while (bus < MPT_MAX_BUS) {
  3809. iocmd.bus = bus;
  3810. iocmd.id = id;
  3811. pTarget = hd->Targets[(int)id];
  3812. if (doConfig) {
  3813. /* Set the negotiation flags */
  3814. if (pTarget && (pTarget = hd->Targets[id]) && !pTarget->raidVolume) {
  3815. flags = pTarget->negoFlags;
  3816. } else {
  3817. flags = hd->ioc->spi_data.noQas;
  3818. if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
  3819. data = hd->ioc->spi_data.nvram[id];
  3820. if (data & MPT_NVRAM_WIDE_DISABLE)
  3821. flags |= MPT_TARGET_NO_NEGO_WIDE;
  3822. factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
  3823. if ((factor == 0) || (factor == MPT_ASYNC))
  3824. flags |= MPT_TARGET_NO_NEGO_SYNC;
  3825. }
  3826. }
  3827. /* Force to async, narrow */
  3828. mptscsih_setDevicePage1Flags(0, MPT_ASYNC, 0, &requested,
  3829. &configuration, flags);
  3830. dnegoprintk(("syncronize cache: id=%d width=0 factor=MPT_ASYNC "
  3831. "offset=0 negoFlags=%x request=%x config=%x\n",
  3832. id, flags, requested, configuration));
  3833. pcfg1Data->RequestedParameters = le32_to_cpu(requested);
  3834. pcfg1Data->Reserved = 0;
  3835. pcfg1Data->Configuration = le32_to_cpu(configuration);
  3836. cfg.pageAddr = (bus<<8) | id;
  3837. mpt_config(hd->ioc, &cfg);
  3838. }
  3839. /* If target Ptr NULL or if this target is NOT a disk, skip.
  3840. */
  3841. if ((pTarget) && (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)){
  3842. for (lun=0; lun <= MPT_LAST_LUN; lun++) {
  3843. /* If LUN present, issue the command
  3844. */
  3845. lun_index = (lun >> 5); /* 32 luns per lun_index */
  3846. indexed_lun = (lun % 32);
  3847. if (pTarget->luns[lun_index] & (1<<indexed_lun)) {
  3848. iocmd.lun = lun;
  3849. (void) mptscsih_do_cmd(hd, &iocmd);
  3850. }
  3851. }
  3852. }
  3853. /* get next relevant device */
  3854. id++;
  3855. if (id == hostId)
  3856. id++;
  3857. if (id > max_id) {
  3858. id = 0;
  3859. bus++;
  3860. }
  3861. }
  3862. if (pcfg1Data) {
  3863. pci_free_consistent(ioc->pcidev, header1.PageLength * 4, pcfg1Data, cfg1_dma_addr);
  3864. }
  3865. return 0;
  3866. }
  3867. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  3868. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  3869. /**
  3870. * mptscsih_domainValidation - Top level handler for domain validation.
  3871. * @hd: Pointer to MPT_SCSI_HOST structure.
  3872. *
  3873. * Uses the ISR, but with special processing.
  3874. * Called from schedule, should not be in interrupt mode.
  3875. * While thread alive, do dv for all devices needing dv
  3876. *
  3877. * Return: None.
  3878. */
  3879. static void
  3880. mptscsih_domainValidation(void *arg)
  3881. {
  3882. MPT_SCSI_HOST *hd;
  3883. MPT_ADAPTER *ioc;
  3884. unsigned long flags;
  3885. int id, maxid, dvStatus, did;
  3886. int ii, isPhysDisk;
  3887. spin_lock_irqsave(&dvtaskQ_lock, flags);
  3888. dvtaskQ_active = 1;
  3889. if (dvtaskQ_release) {
  3890. dvtaskQ_active = 0;
  3891. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  3892. return;
  3893. }
  3894. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  3895. /* For this ioc, loop through all devices and do dv to each device.
  3896. * When complete with this ioc, search through the ioc list, and
  3897. * for each scsi ioc found, do dv for all devices. Exit when no
  3898. * device needs dv.
  3899. */
  3900. did = 1;
  3901. while (did) {
  3902. did = 0;
  3903. list_for_each_entry(ioc, &ioc_list, list) {
  3904. spin_lock_irqsave(&dvtaskQ_lock, flags);
  3905. if (dvtaskQ_release) {
  3906. dvtaskQ_active = 0;
  3907. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  3908. return;
  3909. }
  3910. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  3911. msleep(250);
  3912. /* DV only to SCSI adapters */
  3913. if (ioc->bus_type != SCSI)
  3914. continue;
  3915. /* Make sure everything looks ok */
  3916. if (ioc->sh == NULL)
  3917. continue;
  3918. hd = (MPT_SCSI_HOST *) ioc->sh->hostdata;
  3919. if (hd == NULL)
  3920. continue;
  3921. if ((ioc->spi_data.forceDv & MPT_SCSICFG_RELOAD_IOC_PG3) != 0) {
  3922. mpt_read_ioc_pg_3(ioc);
  3923. if (ioc->spi_data.pIocPg3) {
  3924. Ioc3PhysDisk_t *pPDisk = ioc->spi_data.pIocPg3->PhysDisk;
  3925. int numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
  3926. while (numPDisk) {
  3927. if (ioc->spi_data.dvStatus[pPDisk->PhysDiskID] & MPT_SCSICFG_DV_NOT_DONE)
  3928. ioc->spi_data.dvStatus[pPDisk->PhysDiskID] |= MPT_SCSICFG_NEED_DV;
  3929. pPDisk++;
  3930. numPDisk--;
  3931. }
  3932. }
  3933. ioc->spi_data.forceDv &= ~MPT_SCSICFG_RELOAD_IOC_PG3;
  3934. }
  3935. maxid = min_t(int, ioc->sh->max_id, MPT_MAX_SCSI_DEVICES);
  3936. for (id = 0; id < maxid; id++) {
  3937. spin_lock_irqsave(&dvtaskQ_lock, flags);
  3938. if (dvtaskQ_release) {
  3939. dvtaskQ_active = 0;
  3940. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  3941. return;
  3942. }
  3943. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  3944. dvStatus = hd->ioc->spi_data.dvStatus[id];
  3945. if (dvStatus & MPT_SCSICFG_NEED_DV) {
  3946. did++;
  3947. hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_DV_PENDING;
  3948. hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_NEED_DV;
  3949. msleep(250);
  3950. /* If hidden phys disk, block IO's to all
  3951. * raid volumes
  3952. * else, process normally
  3953. */
  3954. isPhysDisk = mptscsih_is_phys_disk(ioc, id);
  3955. if (isPhysDisk) {
  3956. for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
  3957. if (hd->ioc->spi_data.isRaid & (1 << ii)) {
  3958. hd->ioc->spi_data.dvStatus[ii] |= MPT_SCSICFG_DV_PENDING;
  3959. }
  3960. }
  3961. }
  3962. if (mptscsih_doDv(hd, 0, id) == 1) {
  3963. /* Untagged device was busy, try again
  3964. */
  3965. hd->ioc->spi_data.dvStatus[id] |= MPT_SCSICFG_NEED_DV;
  3966. hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_DV_PENDING;
  3967. } else {
  3968. /* DV is complete. Clear flags.
  3969. */
  3970. hd->ioc->spi_data.dvStatus[id] &= ~(MPT_SCSICFG_DV_NOT_DONE | MPT_SCSICFG_DV_PENDING);
  3971. }
  3972. if (isPhysDisk) {
  3973. for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
  3974. if (hd->ioc->spi_data.isRaid & (1 << ii)) {
  3975. hd->ioc->spi_data.dvStatus[ii] &= ~MPT_SCSICFG_DV_PENDING;
  3976. }
  3977. }
  3978. }
  3979. if (hd->ioc->spi_data.noQas)
  3980. mptscsih_qas_check(hd, id);
  3981. }
  3982. }
  3983. }
  3984. }
  3985. spin_lock_irqsave(&dvtaskQ_lock, flags);
  3986. dvtaskQ_active = 0;
  3987. spin_unlock_irqrestore(&dvtaskQ_lock, flags);
  3988. return;
  3989. }
  3990. /* Search IOC page 3 to determine if this is hidden physical disk
  3991. */
  3992. static int mptscsih_is_phys_disk(MPT_ADAPTER *ioc, int id)
  3993. {
  3994. if (ioc->spi_data.pIocPg3) {
  3995. Ioc3PhysDisk_t *pPDisk = ioc->spi_data.pIocPg3->PhysDisk;
  3996. int numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
  3997. while (numPDisk) {
  3998. if (pPDisk->PhysDiskID == id) {
  3999. return 1;
  4000. }
  4001. pPDisk++;
  4002. numPDisk--;
  4003. }
  4004. }
  4005. return 0;
  4006. }
  4007. /* Write SDP1 if no QAS has been enabled
  4008. */
  4009. static void mptscsih_qas_check(MPT_SCSI_HOST *hd, int id)
  4010. {
  4011. VirtDevice *pTarget;
  4012. int ii;
  4013. if (hd->Targets == NULL)
  4014. return;
  4015. for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++) {
  4016. if (ii == id)
  4017. continue;
  4018. if ((hd->ioc->spi_data.dvStatus[ii] & MPT_SCSICFG_DV_NOT_DONE) != 0)
  4019. continue;
  4020. pTarget = hd->Targets[ii];
  4021. if ((pTarget != NULL) && (!pTarget->raidVolume)) {
  4022. if ((pTarget->negoFlags & hd->ioc->spi_data.noQas) == 0) {
  4023. pTarget->negoFlags |= hd->ioc->spi_data.noQas;
  4024. dnegoprintk(("writeSDP1: id=%d flags=0\n", id));
  4025. mptscsih_writeSDP1(hd, 0, ii, 0);
  4026. }
  4027. } else {
  4028. if (mptscsih_is_phys_disk(hd->ioc, ii) == 1) {
  4029. dnegoprintk(("writeSDP1: id=%d SCSICFG_USE_NVRAM\n", id));
  4030. mptscsih_writeSDP1(hd, 0, ii, MPT_SCSICFG_USE_NVRAM);
  4031. }
  4032. }
  4033. }
  4034. return;
  4035. }
  4036. #define MPT_GET_NVRAM_VALS 0x01
  4037. #define MPT_UPDATE_MAX 0x02
  4038. #define MPT_SET_MAX 0x04
  4039. #define MPT_SET_MIN 0x08
  4040. #define MPT_FALLBACK 0x10
  4041. #define MPT_SAVE 0x20
  4042. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  4043. /**
  4044. * mptscsih_doDv - Perform domain validation to a target.
  4045. * @hd: Pointer to MPT_SCSI_HOST structure.
  4046. * @portnum: IOC port number.
  4047. * @target: Physical ID of this target
  4048. *
  4049. * Uses the ISR, but with special processing.
  4050. * MUST be single-threaded.
  4051. * Test will exit if target is at async & narrow.
  4052. *
  4053. * Return: None.
  4054. */
  4055. static int
  4056. mptscsih_doDv(MPT_SCSI_HOST *hd, int bus_number, int id)
  4057. {
  4058. MPT_ADAPTER *ioc = hd->ioc;
  4059. VirtDevice *pTarget;
  4060. SCSIDevicePage1_t *pcfg1Data;
  4061. SCSIDevicePage0_t *pcfg0Data;
  4062. u8 *pbuf1;
  4063. u8 *pbuf2;
  4064. u8 *pDvBuf;
  4065. dma_addr_t dvbuf_dma = -1;
  4066. dma_addr_t buf1_dma = -1;
  4067. dma_addr_t buf2_dma = -1;
  4068. dma_addr_t cfg1_dma_addr = -1;
  4069. dma_addr_t cfg0_dma_addr = -1;
  4070. ConfigPageHeader_t header1;
  4071. ConfigPageHeader_t header0;
  4072. DVPARAMETERS dv;
  4073. INTERNAL_CMD iocmd;
  4074. CONFIGPARMS cfg;
  4075. int dv_alloc = 0;
  4076. int rc, sz = 0;
  4077. int bufsize = 0;
  4078. int dataBufSize = 0;
  4079. int echoBufSize = 0;
  4080. int notDone;
  4081. int patt;
  4082. int repeat;
  4083. int retcode = 0;
  4084. int nfactor = MPT_ULTRA320;
  4085. char firstPass = 1;
  4086. char doFallback = 0;
  4087. char readPage0;
  4088. char bus, lun;
  4089. char inq0 = 0;
  4090. if (ioc->spi_data.sdp1length == 0)
  4091. return 0;
  4092. if (ioc->spi_data.sdp0length == 0)
  4093. return 0;
  4094. /* If multiple buses are used, require that the initiator
  4095. * id be the same on all buses.
  4096. */
  4097. if (id == ioc->pfacts[0].PortSCSIID)
  4098. return 0;
  4099. lun = 0;
  4100. bus = (u8) bus_number;
  4101. ddvtprintk((MYIOC_s_NOTE_FMT
  4102. "DV started: bus=%d, id=%d dv @ %p\n",
  4103. ioc->name, bus, id, &dv));
  4104. /* Prep DV structure
  4105. */
  4106. memset (&dv, 0, sizeof(DVPARAMETERS));
  4107. dv.id = id;
  4108. /* Populate tmax with the current maximum
  4109. * transfer parameters for this target.
  4110. * Exit if narrow and async.
  4111. */
  4112. dv.cmd = MPT_GET_NVRAM_VALS;
  4113. mptscsih_dv_parms(hd, &dv, NULL);
  4114. /* Prep SCSI IO structure
  4115. */
  4116. iocmd.id = id;
  4117. iocmd.bus = bus;
  4118. iocmd.lun = lun;
  4119. iocmd.flags = 0;
  4120. iocmd.physDiskNum = -1;
  4121. iocmd.rsvd = iocmd.rsvd2 = 0;
  4122. pTarget = hd->Targets[id];
  4123. /* Use tagged commands if possible.
  4124. */
  4125. if (pTarget) {
  4126. if (pTarget->tflags & MPT_TARGET_FLAGS_Q_YES)
  4127. iocmd.flags |= MPT_ICFLAG_TAGGED_CMD;
  4128. else {
  4129. if (hd->ioc->facts.FWVersion.Word < 0x01000600)
  4130. return 0;
  4131. if ((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
  4132. (hd->ioc->facts.FWVersion.Word < 0x01010B00))
  4133. return 0;
  4134. }
  4135. }
  4136. /* Prep cfg structure
  4137. */
  4138. cfg.pageAddr = (bus<<8) | id;
  4139. cfg.hdr = NULL;
  4140. /* Prep SDP0 header
  4141. */
  4142. header0.PageVersion = ioc->spi_data.sdp0version;
  4143. header0.PageLength = ioc->spi_data.sdp0length;
  4144. header0.PageNumber = 0;
  4145. header0.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  4146. /* Prep SDP1 header
  4147. */
  4148. header1.PageVersion = ioc->spi_data.sdp1version;
  4149. header1.PageLength = ioc->spi_data.sdp1length;
  4150. header1.PageNumber = 1;
  4151. header1.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
  4152. if (header0.PageLength & 1)
  4153. dv_alloc = (header0.PageLength * 4) + 4;
  4154. dv_alloc += (2048 + (header1.PageLength * 4));
  4155. pDvBuf = pci_alloc_consistent(ioc->pcidev, dv_alloc, &dvbuf_dma);
  4156. if (pDvBuf == NULL)
  4157. return 0;
  4158. sz = 0;
  4159. pbuf1 = (u8 *)pDvBuf;
  4160. buf1_dma = dvbuf_dma;
  4161. sz +=1024;
  4162. pbuf2 = (u8 *) (pDvBuf + sz);
  4163. buf2_dma = dvbuf_dma + sz;
  4164. sz +=1024;
  4165. pcfg0Data = (SCSIDevicePage0_t *) (pDvBuf + sz);
  4166. cfg0_dma_addr = dvbuf_dma + sz;
  4167. sz += header0.PageLength * 4;
  4168. /* 8-byte alignment
  4169. */
  4170. if (header0.PageLength & 1)
  4171. sz += 4;
  4172. pcfg1Data = (SCSIDevicePage1_t *) (pDvBuf + sz);
  4173. cfg1_dma_addr = dvbuf_dma + sz;
  4174. /* Skip this ID? Set cfg.hdr to force config page write
  4175. */
  4176. {
  4177. ScsiCfgData *pspi_data = &hd->ioc->spi_data;
  4178. if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
  4179. /* Set the factor from nvram */
  4180. nfactor = (pspi_data->nvram[id] & MPT_NVRAM_SYNC_MASK) >> 8;
  4181. if (nfactor < pspi_data->minSyncFactor )
  4182. nfactor = pspi_data->minSyncFactor;
  4183. if (!(pspi_data->nvram[id] & MPT_NVRAM_ID_SCAN_ENABLE) ||
  4184. (pspi_data->PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_OFF_DV) ) {
  4185. ddvprintk((MYIOC_s_NOTE_FMT "DV Skipped: bus, id, lun (%d, %d, %d)\n",
  4186. ioc->name, bus, id, lun));
  4187. dv.cmd = MPT_SET_MAX;
  4188. mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
  4189. cfg.hdr = &header1;
  4190. /* Save the final negotiated settings to
  4191. * SCSI device page 1.
  4192. */
  4193. cfg.physAddr = cfg1_dma_addr;
  4194. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  4195. cfg.dir = 1;
  4196. mpt_config(hd->ioc, &cfg);
  4197. goto target_done;
  4198. }
  4199. }
  4200. }
  4201. /* Finish iocmd inititialization - hidden or visible disk? */
  4202. if (ioc->spi_data.pIocPg3) {
  4203. /* Search IOC page 3 for matching id
  4204. */
  4205. Ioc3PhysDisk_t *pPDisk = ioc->spi_data.pIocPg3->PhysDisk;
  4206. int numPDisk = ioc->spi_data.pIocPg3->NumPhysDisks;
  4207. while (numPDisk) {
  4208. if (pPDisk->PhysDiskID == id) {
  4209. /* match */
  4210. iocmd.flags |= MPT_ICFLAG_PHYS_DISK;
  4211. iocmd.physDiskNum = pPDisk->PhysDiskNum;
  4212. /* Quiesce the IM
  4213. */
  4214. if (mptscsih_do_raid(hd, MPI_RAID_ACTION_QUIESCE_PHYS_IO, &iocmd) < 0) {
  4215. ddvprintk((MYIOC_s_ERR_FMT "RAID Queisce FAILED!\n", ioc->name));
  4216. goto target_done;
  4217. }
  4218. break;
  4219. }
  4220. pPDisk++;
  4221. numPDisk--;
  4222. }
  4223. }
  4224. /* RAID Volume ID's may double for a physical device. If RAID but
  4225. * not a physical ID as well, skip DV.
  4226. */
  4227. if ((hd->ioc->spi_data.isRaid & (1 << id)) && !(iocmd.flags & MPT_ICFLAG_PHYS_DISK))
  4228. goto target_done;
  4229. /* Basic Test.
  4230. * Async & Narrow - Inquiry
  4231. * Async & Narrow - Inquiry
  4232. * Maximum transfer rate - Inquiry
  4233. * Compare buffers:
  4234. * If compare, test complete.
  4235. * If miscompare and first pass, repeat
  4236. * If miscompare and not first pass, fall back and repeat
  4237. */
  4238. hd->pLocal = NULL;
  4239. readPage0 = 0;
  4240. sz = SCSI_MAX_INQUIRY_BYTES;
  4241. rc = MPT_SCANDV_GOOD;
  4242. while (1) {
  4243. ddvprintk((MYIOC_s_NOTE_FMT "DV: Start Basic test on id=%d\n", ioc->name, id));
  4244. retcode = 0;
  4245. dv.cmd = MPT_SET_MIN;
  4246. mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
  4247. cfg.hdr = &header1;
  4248. cfg.physAddr = cfg1_dma_addr;
  4249. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  4250. cfg.dir = 1;
  4251. if (mpt_config(hd->ioc, &cfg) != 0)
  4252. goto target_done;
  4253. /* Wide - narrow - wide workaround case
  4254. */
  4255. if ((rc == MPT_SCANDV_ISSUE_SENSE) && dv.max.width) {
  4256. /* Send an untagged command to reset disk Qs corrupted
  4257. * when a parity error occurs on a Request Sense.
  4258. */
  4259. if ((hd->ioc->facts.FWVersion.Word >= 0x01000600) ||
  4260. ((hd->ioc->facts.FWVersion.Word >= 0x01010000) &&
  4261. (hd->ioc->facts.FWVersion.Word < 0x01010B00)) ) {
  4262. iocmd.cmd = REQUEST_SENSE;
  4263. iocmd.data_dma = buf1_dma;
  4264. iocmd.data = pbuf1;
  4265. iocmd.size = 0x12;
  4266. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4267. goto target_done;
  4268. else {
  4269. if (hd->pLocal == NULL)
  4270. goto target_done;
  4271. rc = hd->pLocal->completion;
  4272. if ((rc == MPT_SCANDV_GOOD) || (rc == MPT_SCANDV_SENSE)) {
  4273. dv.max.width = 0;
  4274. doFallback = 0;
  4275. } else
  4276. goto target_done;
  4277. }
  4278. } else
  4279. goto target_done;
  4280. }
  4281. iocmd.cmd = INQUIRY;
  4282. iocmd.data_dma = buf1_dma;
  4283. iocmd.data = pbuf1;
  4284. iocmd.size = sz;
  4285. memset(pbuf1, 0x00, sz);
  4286. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4287. goto target_done;
  4288. else {
  4289. if (hd->pLocal == NULL)
  4290. goto target_done;
  4291. rc = hd->pLocal->completion;
  4292. if (rc == MPT_SCANDV_GOOD) {
  4293. if (hd->pLocal->scsiStatus == SAM_STAT_BUSY) {
  4294. if ((iocmd.flags & MPT_ICFLAG_TAGGED_CMD) == 0)
  4295. retcode = 1;
  4296. else
  4297. retcode = 0;
  4298. goto target_done;
  4299. }
  4300. } else if (rc == MPT_SCANDV_SENSE) {
  4301. ;
  4302. } else {
  4303. /* If first command doesn't complete
  4304. * with a good status or with a check condition,
  4305. * exit.
  4306. */
  4307. goto target_done;
  4308. }
  4309. }
  4310. /* Reset the size for disks
  4311. */
  4312. inq0 = (*pbuf1) & 0x1F;
  4313. if ((inq0 == 0) && pTarget && !pTarget->raidVolume) {
  4314. sz = 0x40;
  4315. iocmd.size = sz;
  4316. }
  4317. /* Another GEM workaround. Check peripheral device type,
  4318. * if PROCESSOR, quit DV.
  4319. */
  4320. if (inq0 == TYPE_PROCESSOR) {
  4321. mptscsih_initTarget(hd,
  4322. bus,
  4323. id,
  4324. lun,
  4325. pbuf1,
  4326. sz);
  4327. goto target_done;
  4328. }
  4329. if (inq0 > 0x08)
  4330. goto target_done;
  4331. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4332. goto target_done;
  4333. if (sz == 0x40) {
  4334. if ((pTarget->maxWidth == 1) && (pTarget->maxOffset) && (nfactor < 0x0A)
  4335. && (pTarget->minSyncFactor > 0x09)) {
  4336. if ((pbuf1[56] & 0x04) == 0)
  4337. ;
  4338. else if ((pbuf1[56] & 0x01) == 1) {
  4339. pTarget->minSyncFactor =
  4340. nfactor > MPT_ULTRA320 ? nfactor : MPT_ULTRA320;
  4341. } else {
  4342. pTarget->minSyncFactor =
  4343. nfactor > MPT_ULTRA160 ? nfactor : MPT_ULTRA160;
  4344. }
  4345. dv.max.factor = pTarget->minSyncFactor;
  4346. if ((pbuf1[56] & 0x02) == 0) {
  4347. pTarget->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
  4348. hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;
  4349. ddvprintk((MYIOC_s_NOTE_FMT
  4350. "DV: Start Basic noQas on id=%d due to pbuf1[56]=%x\n",
  4351. ioc->name, id, pbuf1[56]));
  4352. }
  4353. }
  4354. }
  4355. if (doFallback)
  4356. dv.cmd = MPT_FALLBACK;
  4357. else
  4358. dv.cmd = MPT_SET_MAX;
  4359. mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
  4360. if (mpt_config(hd->ioc, &cfg) != 0)
  4361. goto target_done;
  4362. if ((!dv.now.width) && (!dv.now.offset))
  4363. goto target_done;
  4364. iocmd.cmd = INQUIRY;
  4365. iocmd.data_dma = buf2_dma;
  4366. iocmd.data = pbuf2;
  4367. iocmd.size = sz;
  4368. memset(pbuf2, 0x00, sz);
  4369. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4370. goto target_done;
  4371. else if (hd->pLocal == NULL)
  4372. goto target_done;
  4373. else {
  4374. /* Save the return code.
  4375. * If this is the first pass,
  4376. * read SCSI Device Page 0
  4377. * and update the target max parameters.
  4378. */
  4379. rc = hd->pLocal->completion;
  4380. doFallback = 0;
  4381. if (rc == MPT_SCANDV_GOOD) {
  4382. if (!readPage0) {
  4383. u32 sdp0_info;
  4384. u32 sdp0_nego;
  4385. cfg.hdr = &header0;
  4386. cfg.physAddr = cfg0_dma_addr;
  4387. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  4388. cfg.dir = 0;
  4389. if (mpt_config(hd->ioc, &cfg) != 0)
  4390. goto target_done;
  4391. sdp0_info = le32_to_cpu(pcfg0Data->Information) & 0x0E;
  4392. sdp0_nego = (le32_to_cpu(pcfg0Data->NegotiatedParameters) & 0xFF00 ) >> 8;
  4393. /* Quantum and Fujitsu workarounds.
  4394. * Quantum: PPR U320 -> PPR reply with Ultra2 and wide
  4395. * Fujitsu: PPR U320 -> Msg Reject and Ultra2 and wide
  4396. * Resetart with a request for U160.
  4397. */
  4398. if ((dv.now.factor == MPT_ULTRA320) && (sdp0_nego == MPT_ULTRA2)) {
  4399. doFallback = 1;
  4400. } else {
  4401. dv.cmd = MPT_UPDATE_MAX;
  4402. mptscsih_dv_parms(hd, &dv, (void *)pcfg0Data);
  4403. /* Update the SCSI device page 1 area
  4404. */
  4405. pcfg1Data->RequestedParameters = pcfg0Data->NegotiatedParameters;
  4406. readPage0 = 1;
  4407. }
  4408. }
  4409. /* Quantum workaround. Restart this test will the fallback
  4410. * flag set.
  4411. */
  4412. if (doFallback == 0) {
  4413. if (memcmp(pbuf1, pbuf2, sz) != 0) {
  4414. if (!firstPass)
  4415. doFallback = 1;
  4416. } else {
  4417. ddvprintk((MYIOC_s_NOTE_FMT
  4418. "DV:Inquiry compared id=%d, calling initTarget\n", ioc->name, id));
  4419. hd->ioc->spi_data.dvStatus[id] &= ~MPT_SCSICFG_DV_NOT_DONE;
  4420. mptscsih_initTarget(hd,
  4421. bus,
  4422. id,
  4423. lun,
  4424. pbuf1,
  4425. sz);
  4426. break; /* test complete */
  4427. }
  4428. }
  4429. } else if (rc == MPT_SCANDV_ISSUE_SENSE)
  4430. doFallback = 1; /* set fallback flag */
  4431. else if ((rc == MPT_SCANDV_DID_RESET) ||
  4432. (rc == MPT_SCANDV_SENSE) ||
  4433. (rc == MPT_SCANDV_FALLBACK))
  4434. doFallback = 1; /* set fallback flag */
  4435. else
  4436. goto target_done;
  4437. firstPass = 0;
  4438. }
  4439. }
  4440. ddvprintk((MYIOC_s_NOTE_FMT "DV: Basic test on id=%d completed OK.\n", ioc->name, id));
  4441. if (mpt_dv == 0)
  4442. goto target_done;
  4443. inq0 = (*pbuf1) & 0x1F;
  4444. /* Continue only for disks
  4445. */
  4446. if (inq0 != 0)
  4447. goto target_done;
  4448. if ( ioc->spi_data.PortFlags == MPI_SCSIPORTPAGE2_PORT_FLAGS_BASIC_DV_ONLY )
  4449. goto target_done;
  4450. /* Start the Enhanced Test.
  4451. * 0) issue TUR to clear out check conditions
  4452. * 1) read capacity of echo (regular) buffer
  4453. * 2) reserve device
  4454. * 3) do write-read-compare data pattern test
  4455. * 4) release
  4456. * 5) update nego parms to target struct
  4457. */
  4458. cfg.hdr = &header1;
  4459. cfg.physAddr = cfg1_dma_addr;
  4460. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  4461. cfg.dir = 1;
  4462. iocmd.cmd = TEST_UNIT_READY;
  4463. iocmd.data_dma = -1;
  4464. iocmd.data = NULL;
  4465. iocmd.size = 0;
  4466. notDone = 1;
  4467. while (notDone) {
  4468. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4469. goto target_done;
  4470. if (hd->pLocal == NULL)
  4471. goto target_done;
  4472. rc = hd->pLocal->completion;
  4473. if (rc == MPT_SCANDV_GOOD)
  4474. notDone = 0;
  4475. else if (rc == MPT_SCANDV_SENSE) {
  4476. u8 skey = hd->pLocal->sense[2] & 0x0F;
  4477. u8 asc = hd->pLocal->sense[12];
  4478. u8 ascq = hd->pLocal->sense[13];
  4479. ddvprintk((MYIOC_s_INFO_FMT
  4480. "SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
  4481. ioc->name, skey, asc, ascq));
  4482. if (skey == UNIT_ATTENTION)
  4483. notDone++; /* repeat */
  4484. else if ((skey == NOT_READY) &&
  4485. (asc == 0x04)&&(ascq == 0x01)) {
  4486. /* wait then repeat */
  4487. mdelay (2000);
  4488. notDone++;
  4489. } else if ((skey == NOT_READY) && (asc == 0x3A)) {
  4490. /* no medium, try read test anyway */
  4491. notDone = 0;
  4492. } else {
  4493. /* All other errors are fatal.
  4494. */
  4495. ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
  4496. ioc->name));
  4497. goto target_done;
  4498. }
  4499. } else
  4500. goto target_done;
  4501. }
  4502. iocmd.cmd = READ_BUFFER;
  4503. iocmd.data_dma = buf1_dma;
  4504. iocmd.data = pbuf1;
  4505. iocmd.size = 4;
  4506. iocmd.flags |= MPT_ICFLAG_BUF_CAP;
  4507. dataBufSize = 0;
  4508. echoBufSize = 0;
  4509. for (patt = 0; patt < 2; patt++) {
  4510. if (patt == 0)
  4511. iocmd.flags |= MPT_ICFLAG_ECHO;
  4512. else
  4513. iocmd.flags &= ~MPT_ICFLAG_ECHO;
  4514. notDone = 1;
  4515. while (notDone) {
  4516. bufsize = 0;
  4517. /* If not ready after 8 trials,
  4518. * give up on this device.
  4519. */
  4520. if (notDone > 8)
  4521. goto target_done;
  4522. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4523. goto target_done;
  4524. else if (hd->pLocal == NULL)
  4525. goto target_done;
  4526. else {
  4527. rc = hd->pLocal->completion;
  4528. ddvprintk(("ReadBuffer Comp Code %d", rc));
  4529. ddvprintk((" buff: %0x %0x %0x %0x\n",
  4530. pbuf1[0], pbuf1[1], pbuf1[2], pbuf1[3]));
  4531. if (rc == MPT_SCANDV_GOOD) {
  4532. notDone = 0;
  4533. if (iocmd.flags & MPT_ICFLAG_ECHO) {
  4534. bufsize = ((pbuf1[2] & 0x1F) <<8) | pbuf1[3];
  4535. } else {
  4536. bufsize = pbuf1[1]<<16 | pbuf1[2]<<8 | pbuf1[3];
  4537. }
  4538. } else if (rc == MPT_SCANDV_SENSE) {
  4539. u8 skey = hd->pLocal->sense[2] & 0x0F;
  4540. u8 asc = hd->pLocal->sense[12];
  4541. u8 ascq = hd->pLocal->sense[13];
  4542. ddvprintk((MYIOC_s_INFO_FMT
  4543. "SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
  4544. ioc->name, skey, asc, ascq));
  4545. if (skey == ILLEGAL_REQUEST) {
  4546. notDone = 0;
  4547. } else if (skey == UNIT_ATTENTION) {
  4548. notDone++; /* repeat */
  4549. } else if ((skey == NOT_READY) &&
  4550. (asc == 0x04)&&(ascq == 0x01)) {
  4551. /* wait then repeat */
  4552. mdelay (2000);
  4553. notDone++;
  4554. } else {
  4555. /* All other errors are fatal.
  4556. */
  4557. ddvprintk((MYIOC_s_INFO_FMT "DV: fatal error.",
  4558. ioc->name));
  4559. goto target_done;
  4560. }
  4561. } else {
  4562. /* All other errors are fatal
  4563. */
  4564. goto target_done;
  4565. }
  4566. }
  4567. }
  4568. if (iocmd.flags & MPT_ICFLAG_ECHO)
  4569. echoBufSize = bufsize;
  4570. else
  4571. dataBufSize = bufsize;
  4572. }
  4573. sz = 0;
  4574. iocmd.flags &= ~MPT_ICFLAG_BUF_CAP;
  4575. /* Use echo buffers if possible,
  4576. * Exit if both buffers are 0.
  4577. */
  4578. if (echoBufSize > 0) {
  4579. iocmd.flags |= MPT_ICFLAG_ECHO;
  4580. if (dataBufSize > 0)
  4581. bufsize = min(echoBufSize, dataBufSize);
  4582. else
  4583. bufsize = echoBufSize;
  4584. } else if (dataBufSize == 0)
  4585. goto target_done;
  4586. ddvprintk((MYIOC_s_INFO_FMT "%s Buffer Capacity %d\n", ioc->name,
  4587. (iocmd.flags & MPT_ICFLAG_ECHO) ? "Echo" : " ", bufsize));
  4588. /* Data buffers for write-read-compare test max 1K.
  4589. */
  4590. sz = min(bufsize, 1024);
  4591. /* --- loop ----
  4592. * On first pass, always issue a reserve.
  4593. * On additional loops, only if a reset has occurred.
  4594. * iocmd.flags indicates if echo or regular buffer
  4595. */
  4596. for (patt = 0; patt < 4; patt++) {
  4597. ddvprintk(("Pattern %d\n", patt));
  4598. if ((iocmd.flags & MPT_ICFLAG_RESERVED) && (iocmd.flags & MPT_ICFLAG_DID_RESET)) {
  4599. iocmd.cmd = TEST_UNIT_READY;
  4600. iocmd.data_dma = -1;
  4601. iocmd.data = NULL;
  4602. iocmd.size = 0;
  4603. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4604. goto target_done;
  4605. iocmd.cmd = RELEASE;
  4606. iocmd.data_dma = -1;
  4607. iocmd.data = NULL;
  4608. iocmd.size = 0;
  4609. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4610. goto target_done;
  4611. else if (hd->pLocal == NULL)
  4612. goto target_done;
  4613. else {
  4614. rc = hd->pLocal->completion;
  4615. ddvprintk(("Release rc %d\n", rc));
  4616. if (rc == MPT_SCANDV_GOOD)
  4617. iocmd.flags &= ~MPT_ICFLAG_RESERVED;
  4618. else
  4619. goto target_done;
  4620. }
  4621. iocmd.flags &= ~MPT_ICFLAG_RESERVED;
  4622. }
  4623. iocmd.flags &= ~MPT_ICFLAG_DID_RESET;
  4624. repeat = 5;
  4625. while (repeat && (!(iocmd.flags & MPT_ICFLAG_RESERVED))) {
  4626. iocmd.cmd = RESERVE;
  4627. iocmd.data_dma = -1;
  4628. iocmd.data = NULL;
  4629. iocmd.size = 0;
  4630. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4631. goto target_done;
  4632. else if (hd->pLocal == NULL)
  4633. goto target_done;
  4634. else {
  4635. rc = hd->pLocal->completion;
  4636. if (rc == MPT_SCANDV_GOOD) {
  4637. iocmd.flags |= MPT_ICFLAG_RESERVED;
  4638. } else if (rc == MPT_SCANDV_SENSE) {
  4639. /* Wait if coming ready
  4640. */
  4641. u8 skey = hd->pLocal->sense[2] & 0x0F;
  4642. u8 asc = hd->pLocal->sense[12];
  4643. u8 ascq = hd->pLocal->sense[13];
  4644. ddvprintk((MYIOC_s_INFO_FMT
  4645. "DV: Reserve Failed: ", ioc->name));
  4646. ddvprintk(("SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n",
  4647. skey, asc, ascq));
  4648. if ((skey == NOT_READY) && (asc == 0x04)&&
  4649. (ascq == 0x01)) {
  4650. /* wait then repeat */
  4651. mdelay (2000);
  4652. notDone++;
  4653. } else {
  4654. ddvprintk((MYIOC_s_INFO_FMT
  4655. "DV: Reserved Failed.", ioc->name));
  4656. goto target_done;
  4657. }
  4658. } else {
  4659. ddvprintk((MYIOC_s_INFO_FMT "DV: Reserved Failed.",
  4660. ioc->name));
  4661. goto target_done;
  4662. }
  4663. }
  4664. }
  4665. mptscsih_fillbuf(pbuf1, sz, patt, 1);
  4666. iocmd.cmd = WRITE_BUFFER;
  4667. iocmd.data_dma = buf1_dma;
  4668. iocmd.data = pbuf1;
  4669. iocmd.size = sz;
  4670. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4671. goto target_done;
  4672. else if (hd->pLocal == NULL)
  4673. goto target_done;
  4674. else {
  4675. rc = hd->pLocal->completion;
  4676. if (rc == MPT_SCANDV_GOOD)
  4677. ; /* Issue read buffer */
  4678. else if (rc == MPT_SCANDV_DID_RESET) {
  4679. /* If using echo buffers, reset to data buffers.
  4680. * Else do Fallback and restart
  4681. * this test (re-issue reserve
  4682. * because of bus reset).
  4683. */
  4684. if ((iocmd.flags & MPT_ICFLAG_ECHO) && (dataBufSize >= bufsize)) {
  4685. iocmd.flags &= ~MPT_ICFLAG_ECHO;
  4686. } else {
  4687. dv.cmd = MPT_FALLBACK;
  4688. mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
  4689. if (mpt_config(hd->ioc, &cfg) != 0)
  4690. goto target_done;
  4691. if ((!dv.now.width) && (!dv.now.offset))
  4692. goto target_done;
  4693. }
  4694. iocmd.flags |= MPT_ICFLAG_DID_RESET;
  4695. patt = -1;
  4696. continue;
  4697. } else if (rc == MPT_SCANDV_SENSE) {
  4698. /* Restart data test if UA, else quit.
  4699. */
  4700. u8 skey = hd->pLocal->sense[2] & 0x0F;
  4701. ddvprintk((MYIOC_s_INFO_FMT
  4702. "SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
  4703. hd->pLocal->sense[12], hd->pLocal->sense[13]));
  4704. if (skey == UNIT_ATTENTION) {
  4705. patt = -1;
  4706. continue;
  4707. } else if (skey == ILLEGAL_REQUEST) {
  4708. if (iocmd.flags & MPT_ICFLAG_ECHO) {
  4709. if (dataBufSize >= bufsize) {
  4710. iocmd.flags &= ~MPT_ICFLAG_ECHO;
  4711. patt = -1;
  4712. continue;
  4713. }
  4714. }
  4715. goto target_done;
  4716. }
  4717. else
  4718. goto target_done;
  4719. } else {
  4720. /* fatal error */
  4721. goto target_done;
  4722. }
  4723. }
  4724. iocmd.cmd = READ_BUFFER;
  4725. iocmd.data_dma = buf2_dma;
  4726. iocmd.data = pbuf2;
  4727. iocmd.size = sz;
  4728. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4729. goto target_done;
  4730. else if (hd->pLocal == NULL)
  4731. goto target_done;
  4732. else {
  4733. rc = hd->pLocal->completion;
  4734. if (rc == MPT_SCANDV_GOOD) {
  4735. /* If buffers compare,
  4736. * go to next pattern,
  4737. * else, do a fallback and restart
  4738. * data transfer test.
  4739. */
  4740. if (memcmp (pbuf1, pbuf2, sz) == 0) {
  4741. ; /* goto next pattern */
  4742. } else {
  4743. /* Miscompare with Echo buffer, go to data buffer,
  4744. * if that buffer exists.
  4745. * Miscompare with Data buffer, check first 4 bytes,
  4746. * some devices return capacity. Exit in this case.
  4747. */
  4748. if (iocmd.flags & MPT_ICFLAG_ECHO) {
  4749. if (dataBufSize >= bufsize)
  4750. iocmd.flags &= ~MPT_ICFLAG_ECHO;
  4751. else
  4752. goto target_done;
  4753. } else {
  4754. if (dataBufSize == (pbuf2[1]<<16 | pbuf2[2]<<8 | pbuf2[3])) {
  4755. /* Argh. Device returning wrong data.
  4756. * Quit DV for this device.
  4757. */
  4758. goto target_done;
  4759. }
  4760. /* Had an actual miscompare. Slow down.*/
  4761. dv.cmd = MPT_FALLBACK;
  4762. mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
  4763. if (mpt_config(hd->ioc, &cfg) != 0)
  4764. goto target_done;
  4765. if ((!dv.now.width) && (!dv.now.offset))
  4766. goto target_done;
  4767. }
  4768. patt = -1;
  4769. continue;
  4770. }
  4771. } else if (rc == MPT_SCANDV_DID_RESET) {
  4772. /* Do Fallback and restart
  4773. * this test (re-issue reserve
  4774. * because of bus reset).
  4775. */
  4776. dv.cmd = MPT_FALLBACK;
  4777. mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
  4778. if (mpt_config(hd->ioc, &cfg) != 0)
  4779. goto target_done;
  4780. if ((!dv.now.width) && (!dv.now.offset))
  4781. goto target_done;
  4782. iocmd.flags |= MPT_ICFLAG_DID_RESET;
  4783. patt = -1;
  4784. continue;
  4785. } else if (rc == MPT_SCANDV_SENSE) {
  4786. /* Restart data test if UA, else quit.
  4787. */
  4788. u8 skey = hd->pLocal->sense[2] & 0x0F;
  4789. ddvprintk((MYIOC_s_INFO_FMT
  4790. "SenseKey:ASC:ASCQ = (%x:%02x:%02x)\n", ioc->name, skey,
  4791. hd->pLocal->sense[12], hd->pLocal->sense[13]));
  4792. if (skey == UNIT_ATTENTION) {
  4793. patt = -1;
  4794. continue;
  4795. }
  4796. else
  4797. goto target_done;
  4798. } else {
  4799. /* fatal error */
  4800. goto target_done;
  4801. }
  4802. }
  4803. } /* --- end of patt loop ---- */
  4804. target_done:
  4805. if (iocmd.flags & MPT_ICFLAG_RESERVED) {
  4806. iocmd.cmd = RELEASE;
  4807. iocmd.data_dma = -1;
  4808. iocmd.data = NULL;
  4809. iocmd.size = 0;
  4810. if (mptscsih_do_cmd(hd, &iocmd) < 0)
  4811. printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
  4812. ioc->name, id);
  4813. else if (hd->pLocal) {
  4814. if (hd->pLocal->completion == MPT_SCANDV_GOOD)
  4815. iocmd.flags &= ~MPT_ICFLAG_RESERVED;
  4816. } else {
  4817. printk(MYIOC_s_INFO_FMT "DV: Release failed. id %d",
  4818. ioc->name, id);
  4819. }
  4820. }
  4821. /* Set if cfg1_dma_addr contents is valid
  4822. */
  4823. if ((cfg.hdr != NULL) && (retcode == 0)){
  4824. /* If disk, not U320, disable QAS
  4825. */
  4826. if ((inq0 == 0) && (dv.now.factor > MPT_ULTRA320)) {
  4827. hd->ioc->spi_data.noQas = MPT_TARGET_NO_NEGO_QAS;
  4828. ddvprintk((MYIOC_s_NOTE_FMT
  4829. "noQas set due to id=%d has factor=%x\n", ioc->name, id, dv.now.factor));
  4830. }
  4831. dv.cmd = MPT_SAVE;
  4832. mptscsih_dv_parms(hd, &dv, (void *)pcfg1Data);
  4833. /* Double writes to SDP1 can cause problems,
  4834. * skip save of the final negotiated settings to
  4835. * SCSI device page 1.
  4836. *
  4837. cfg.hdr = &header1;
  4838. cfg.physAddr = cfg1_dma_addr;
  4839. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  4840. cfg.dir = 1;
  4841. mpt_config(hd->ioc, &cfg);
  4842. */
  4843. }
  4844. /* If this is a RAID Passthrough, enable internal IOs
  4845. */
  4846. if (iocmd.flags & MPT_ICFLAG_PHYS_DISK) {
  4847. if (mptscsih_do_raid(hd, MPI_RAID_ACTION_ENABLE_PHYS_IO, &iocmd) < 0)
  4848. ddvprintk((MYIOC_s_ERR_FMT "RAID Enable FAILED!\n", ioc->name));
  4849. }
  4850. /* Done with the DV scan of the current target
  4851. */
  4852. if (pDvBuf)
  4853. pci_free_consistent(ioc->pcidev, dv_alloc, pDvBuf, dvbuf_dma);
  4854. ddvtprintk((MYIOC_s_INFO_FMT "DV Done id=%d\n",
  4855. ioc->name, id));
  4856. return retcode;
  4857. }
  4858. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  4859. /* mptscsih_dv_parms - perform a variety of operations on the
  4860. * parameters used for negotiation.
  4861. * @hd: Pointer to a SCSI host.
  4862. * @dv: Pointer to a structure that contains the maximum and current
  4863. * negotiated parameters.
  4864. */
  4865. static void
  4866. mptscsih_dv_parms(MPT_SCSI_HOST *hd, DVPARAMETERS *dv,void *pPage)
  4867. {
  4868. VirtDevice *pTarget;
  4869. SCSIDevicePage0_t *pPage0;
  4870. SCSIDevicePage1_t *pPage1;
  4871. int val = 0, data, configuration;
  4872. u8 width = 0;
  4873. u8 offset = 0;
  4874. u8 factor = 0;
  4875. u8 negoFlags = 0;
  4876. u8 cmd = dv->cmd;
  4877. u8 id = dv->id;
  4878. switch (cmd) {
  4879. case MPT_GET_NVRAM_VALS:
  4880. ddvprintk((MYIOC_s_NOTE_FMT "Getting NVRAM: ",
  4881. hd->ioc->name));
  4882. /* Get the NVRAM values and save in tmax
  4883. * If not an LVD bus, the adapter minSyncFactor has been
  4884. * already throttled back.
  4885. */
  4886. if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume) {
  4887. width = pTarget->maxWidth;
  4888. offset = pTarget->maxOffset;
  4889. factor = pTarget->minSyncFactor;
  4890. negoFlags = pTarget->negoFlags;
  4891. } else {
  4892. if (hd->ioc->spi_data.nvram && (hd->ioc->spi_data.nvram[id] != MPT_HOST_NVRAM_INVALID)) {
  4893. data = hd->ioc->spi_data.nvram[id];
  4894. width = data & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
  4895. if ((offset = hd->ioc->spi_data.maxSyncOffset) == 0)
  4896. factor = MPT_ASYNC;
  4897. else {
  4898. factor = (data & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
  4899. if ((factor == 0) || (factor == MPT_ASYNC)){
  4900. factor = MPT_ASYNC;
  4901. offset = 0;
  4902. }
  4903. }
  4904. } else {
  4905. width = MPT_NARROW;
  4906. offset = 0;
  4907. factor = MPT_ASYNC;
  4908. }
  4909. /* Set the negotiation flags */
  4910. negoFlags = hd->ioc->spi_data.noQas;
  4911. if (!width)
  4912. negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
  4913. if (!offset)
  4914. negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
  4915. }
  4916. /* limit by adapter capabilities */
  4917. width = min(width, hd->ioc->spi_data.maxBusWidth);
  4918. offset = min(offset, hd->ioc->spi_data.maxSyncOffset);
  4919. factor = max(factor, hd->ioc->spi_data.minSyncFactor);
  4920. /* Check Consistency */
  4921. if (offset && (factor < MPT_ULTRA2) && !width)
  4922. factor = MPT_ULTRA2;
  4923. dv->max.width = width;
  4924. dv->max.offset = offset;
  4925. dv->max.factor = factor;
  4926. dv->max.flags = negoFlags;
  4927. ddvprintk((" id=%d width=%d factor=%x offset=%x flags=%x\n",
  4928. id, width, factor, offset, negoFlags));
  4929. break;
  4930. case MPT_UPDATE_MAX:
  4931. ddvprintk((MYIOC_s_NOTE_FMT
  4932. "Updating with SDP0 Data: ", hd->ioc->name));
  4933. /* Update tmax values with those from Device Page 0.*/
  4934. pPage0 = (SCSIDevicePage0_t *) pPage;
  4935. if (pPage0) {
  4936. val = cpu_to_le32(pPage0->NegotiatedParameters);
  4937. dv->max.width = val & MPI_SCSIDEVPAGE0_NP_WIDE ? 1 : 0;
  4938. dv->max.offset = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> 16;
  4939. dv->max.factor = (val&MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
  4940. }
  4941. dv->now.width = dv->max.width;
  4942. dv->now.offset = dv->max.offset;
  4943. dv->now.factor = dv->max.factor;
  4944. ddvprintk(("id=%d width=%d factor=%x offset=%x flags=%x\n",
  4945. id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags));
  4946. break;
  4947. case MPT_SET_MAX:
  4948. ddvprintk((MYIOC_s_NOTE_FMT "Setting Max: ",
  4949. hd->ioc->name));
  4950. /* Set current to the max values. Update the config page.*/
  4951. dv->now.width = dv->max.width;
  4952. dv->now.offset = dv->max.offset;
  4953. dv->now.factor = dv->max.factor;
  4954. dv->now.flags = dv->max.flags;
  4955. pPage1 = (SCSIDevicePage1_t *)pPage;
  4956. if (pPage1) {
  4957. mptscsih_setDevicePage1Flags (dv->now.width, dv->now.factor,
  4958. dv->now.offset, &val, &configuration, dv->now.flags);
  4959. dnegoprintk(("Setting Max: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
  4960. id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags, val, configuration));
  4961. pPage1->RequestedParameters = le32_to_cpu(val);
  4962. pPage1->Reserved = 0;
  4963. pPage1->Configuration = le32_to_cpu(configuration);
  4964. }
  4965. ddvprintk(("id=%d width=%d factor=%x offset=%x flags=%x request=%x configuration=%x\n",
  4966. id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags, val, configuration));
  4967. break;
  4968. case MPT_SET_MIN:
  4969. ddvprintk((MYIOC_s_NOTE_FMT "Setting Min: ",
  4970. hd->ioc->name));
  4971. /* Set page to asynchronous and narrow
  4972. * Do not update now, breaks fallback routine. */
  4973. width = MPT_NARROW;
  4974. offset = 0;
  4975. factor = MPT_ASYNC;
  4976. negoFlags = dv->max.flags;
  4977. pPage1 = (SCSIDevicePage1_t *)pPage;
  4978. if (pPage1) {
  4979. mptscsih_setDevicePage1Flags (width, factor,
  4980. offset, &val, &configuration, negoFlags);
  4981. dnegoprintk(("Setting Min: id=%d width=%d factor=%x offset=%x negoFlags=%x request=%x config=%x\n",
  4982. id, width, factor, offset, negoFlags, val, configuration));
  4983. pPage1->RequestedParameters = le32_to_cpu(val);
  4984. pPage1->Reserved = 0;
  4985. pPage1->Configuration = le32_to_cpu(configuration);
  4986. }
  4987. ddvprintk(("id=%d width=%d factor=%x offset=%x request=%x config=%x negoFlags=%x\n",
  4988. id, width, factor, offset, val, configuration, negoFlags));
  4989. break;
  4990. case MPT_FALLBACK:
  4991. ddvprintk((MYIOC_s_NOTE_FMT
  4992. "Fallback: Start: offset %d, factor %x, width %d \n",
  4993. hd->ioc->name, dv->now.offset,
  4994. dv->now.factor, dv->now.width));
  4995. width = dv->now.width;
  4996. offset = dv->now.offset;
  4997. factor = dv->now.factor;
  4998. if ((offset) && (dv->max.width)) {
  4999. if (factor < MPT_ULTRA160)
  5000. factor = MPT_ULTRA160;
  5001. else if (factor < MPT_ULTRA2) {
  5002. factor = MPT_ULTRA2;
  5003. width = MPT_WIDE;
  5004. } else if ((factor == MPT_ULTRA2) && width) {
  5005. factor = MPT_ULTRA2;
  5006. width = MPT_NARROW;
  5007. } else if (factor < MPT_ULTRA) {
  5008. factor = MPT_ULTRA;
  5009. width = MPT_WIDE;
  5010. } else if ((factor == MPT_ULTRA) && width) {
  5011. width = MPT_NARROW;
  5012. } else if (factor < MPT_FAST) {
  5013. factor = MPT_FAST;
  5014. width = MPT_WIDE;
  5015. } else if ((factor == MPT_FAST) && width) {
  5016. factor = MPT_FAST;
  5017. width = MPT_NARROW;
  5018. } else if (factor < MPT_SCSI) {
  5019. factor = MPT_SCSI;
  5020. width = MPT_WIDE;
  5021. } else if ((factor == MPT_SCSI) && width) {
  5022. factor = MPT_SCSI;
  5023. width = MPT_NARROW;
  5024. } else {
  5025. factor = MPT_ASYNC;
  5026. offset = 0;
  5027. }
  5028. } else if (offset) {
  5029. width = MPT_NARROW;
  5030. if (factor < MPT_ULTRA)
  5031. factor = MPT_ULTRA;
  5032. else if (factor < MPT_FAST)
  5033. factor = MPT_FAST;
  5034. else if (factor < MPT_SCSI)
  5035. factor = MPT_SCSI;
  5036. else {
  5037. factor = MPT_ASYNC;
  5038. offset = 0;
  5039. }
  5040. } else {
  5041. width = MPT_NARROW;
  5042. factor = MPT_ASYNC;
  5043. }
  5044. dv->max.flags |= MPT_TARGET_NO_NEGO_QAS;
  5045. dv->max.flags &= ~MPT_TAPE_NEGO_IDP;
  5046. dv->now.width = width;
  5047. dv->now.offset = offset;
  5048. dv->now.factor = factor;
  5049. dv->now.flags = dv->max.flags;
  5050. pPage1 = (SCSIDevicePage1_t *)pPage;
  5051. if (pPage1) {
  5052. mptscsih_setDevicePage1Flags (width, factor, offset, &val,
  5053. &configuration, dv->now.flags);
  5054. dnegoprintk(("Finish: id=%d width=%d offset=%d factor=%x flags=%x request=%x config=%x\n",
  5055. id, width, offset, factor, dv->now.flags, val, configuration));
  5056. pPage1->RequestedParameters = le32_to_cpu(val);
  5057. pPage1->Reserved = 0;
  5058. pPage1->Configuration = le32_to_cpu(configuration);
  5059. }
  5060. ddvprintk(("Finish: id=%d offset=%d factor=%x width=%d request=%x config=%x\n",
  5061. id, dv->now.offset, dv->now.factor, dv->now.width, val, configuration));
  5062. break;
  5063. case MPT_SAVE:
  5064. ddvprintk((MYIOC_s_NOTE_FMT
  5065. "Saving to Target structure: ", hd->ioc->name));
  5066. ddvprintk(("id=%d width=%x factor=%x offset=%d flags=%x\n",
  5067. id, dv->now.width, dv->now.factor, dv->now.offset, dv->now.flags));
  5068. /* Save these values to target structures
  5069. * or overwrite nvram (phys disks only).
  5070. */
  5071. if ((hd->Targets)&&((pTarget = hd->Targets[(int)id]) != NULL) && !pTarget->raidVolume ) {
  5072. pTarget->maxWidth = dv->now.width;
  5073. pTarget->maxOffset = dv->now.offset;
  5074. pTarget->minSyncFactor = dv->now.factor;
  5075. pTarget->negoFlags = dv->now.flags;
  5076. } else {
  5077. /* Preserv all flags, use
  5078. * read-modify-write algorithm
  5079. */
  5080. if (hd->ioc->spi_data.nvram) {
  5081. data = hd->ioc->spi_data.nvram[id];
  5082. if (dv->now.width)
  5083. data &= ~MPT_NVRAM_WIDE_DISABLE;
  5084. else
  5085. data |= MPT_NVRAM_WIDE_DISABLE;
  5086. if (!dv->now.offset)
  5087. factor = MPT_ASYNC;
  5088. data &= ~MPT_NVRAM_SYNC_MASK;
  5089. data |= (dv->now.factor << MPT_NVRAM_SYNC_SHIFT) & MPT_NVRAM_SYNC_MASK;
  5090. hd->ioc->spi_data.nvram[id] = data;
  5091. }
  5092. }
  5093. break;
  5094. }
  5095. }
  5096. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  5097. /* mptscsih_fillbuf - fill a buffer with a special data pattern
  5098. * cleanup. For bus scan only.
  5099. *
  5100. * @buffer: Pointer to data buffer to be filled.
  5101. * @size: Number of bytes to fill
  5102. * @index: Pattern index
  5103. * @width: bus width, 0 (8 bits) or 1 (16 bits)
  5104. */
  5105. static void
  5106. mptscsih_fillbuf(char *buffer, int size, int index, int width)
  5107. {
  5108. char *ptr = buffer;
  5109. int ii;
  5110. char byte;
  5111. short val;
  5112. switch (index) {
  5113. case 0:
  5114. if (width) {
  5115. /* Pattern: 0000 FFFF 0000 FFFF
  5116. */
  5117. for (ii=0; ii < size; ii++, ptr++) {
  5118. if (ii & 0x02)
  5119. *ptr = 0xFF;
  5120. else
  5121. *ptr = 0x00;
  5122. }
  5123. } else {
  5124. /* Pattern: 00 FF 00 FF
  5125. */
  5126. for (ii=0; ii < size; ii++, ptr++) {
  5127. if (ii & 0x01)
  5128. *ptr = 0xFF;
  5129. else
  5130. *ptr = 0x00;
  5131. }
  5132. }
  5133. break;
  5134. case 1:
  5135. if (width) {
  5136. /* Pattern: 5555 AAAA 5555 AAAA 5555
  5137. */
  5138. for (ii=0; ii < size; ii++, ptr++) {
  5139. if (ii & 0x02)
  5140. *ptr = 0xAA;
  5141. else
  5142. *ptr = 0x55;
  5143. }
  5144. } else {
  5145. /* Pattern: 55 AA 55 AA 55
  5146. */
  5147. for (ii=0; ii < size; ii++, ptr++) {
  5148. if (ii & 0x01)
  5149. *ptr = 0xAA;
  5150. else
  5151. *ptr = 0x55;
  5152. }
  5153. }
  5154. break;
  5155. case 2:
  5156. /* Pattern: 00 01 02 03 04 05
  5157. * ... FE FF 00 01..
  5158. */
  5159. for (ii=0; ii < size; ii++, ptr++)
  5160. *ptr = (char) ii;
  5161. break;
  5162. case 3:
  5163. if (width) {
  5164. /* Wide Pattern: FFFE 0001 FFFD 0002
  5165. * ... 4000 DFFF 8000 EFFF
  5166. */
  5167. byte = 0;
  5168. for (ii=0; ii < size/2; ii++) {
  5169. /* Create the base pattern
  5170. */
  5171. val = (1 << byte);
  5172. /* every 64 (0x40) bytes flip the pattern
  5173. * since we fill 2 bytes / iteration,
  5174. * test for ii = 0x20
  5175. */
  5176. if (ii & 0x20)
  5177. val = ~(val);
  5178. if (ii & 0x01) {
  5179. *ptr = (char)( (val & 0xFF00) >> 8);
  5180. ptr++;
  5181. *ptr = (char)(val & 0xFF);
  5182. byte++;
  5183. byte &= 0x0F;
  5184. } else {
  5185. val = ~val;
  5186. *ptr = (char)( (val & 0xFF00) >> 8);
  5187. ptr++;
  5188. *ptr = (char)(val & 0xFF);
  5189. }
  5190. ptr++;
  5191. }
  5192. } else {
  5193. /* Narrow Pattern: FE 01 FD 02 FB 04
  5194. * .. 7F 80 01 FE 02 FD ... 80 7F
  5195. */
  5196. byte = 0;
  5197. for (ii=0; ii < size; ii++, ptr++) {
  5198. /* Base pattern - first 32 bytes
  5199. */
  5200. if (ii & 0x01) {
  5201. *ptr = (1 << byte);
  5202. byte++;
  5203. byte &= 0x07;
  5204. } else {
  5205. *ptr = (char) (~(1 << byte));
  5206. }
  5207. /* Flip the pattern every 32 bytes
  5208. */
  5209. if (ii & 0x20)
  5210. *ptr = ~(*ptr);
  5211. }
  5212. }
  5213. break;
  5214. }
  5215. }
  5216. #endif /* ~MPTSCSIH_ENABLE_DOMAIN_VALIDATION */
  5217. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  5218. module_init(mptscsih_init);
  5219. module_exit(mptscsih_exit);