ipr.c 248 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964
  1. /*
  2. * ipr.c -- driver for IBM Power Linux RAID adapters
  3. *
  4. * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
  5. *
  6. * Copyright (C) 2003, 2004 IBM Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. /*
  24. * Notes:
  25. *
  26. * This driver is used to control the following SCSI adapters:
  27. *
  28. * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
  29. *
  30. * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
  31. * PCI-X Dual Channel Ultra 320 SCSI Adapter
  32. * PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
  33. * Embedded SCSI adapter on p615 and p655 systems
  34. *
  35. * Supported Hardware Features:
  36. * - Ultra 320 SCSI controller
  37. * - PCI-X host interface
  38. * - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
  39. * - Non-Volatile Write Cache
  40. * - Supports attachment of non-RAID disks, tape, and optical devices
  41. * - RAID Levels 0, 5, 10
  42. * - Hot spare
  43. * - Background Parity Checking
  44. * - Background Data Scrubbing
  45. * - Ability to increase the capacity of an existing RAID 5 disk array
  46. * by adding disks
  47. *
  48. * Driver Features:
  49. * - Tagged command queuing
  50. * - Adapter microcode download
  51. * - PCI hot plug
  52. * - SCSI device hot plug
  53. *
  54. */
  55. #include <linux/fs.h>
  56. #include <linux/init.h>
  57. #include <linux/types.h>
  58. #include <linux/errno.h>
  59. #include <linux/kernel.h>
  60. #include <linux/slab.h>
  61. #include <linux/ioport.h>
  62. #include <linux/delay.h>
  63. #include <linux/pci.h>
  64. #include <linux/wait.h>
  65. #include <linux/spinlock.h>
  66. #include <linux/sched.h>
  67. #include <linux/interrupt.h>
  68. #include <linux/blkdev.h>
  69. #include <linux/firmware.h>
  70. #include <linux/module.h>
  71. #include <linux/moduleparam.h>
  72. #include <linux/libata.h>
  73. #include <linux/hdreg.h>
  74. #include <linux/reboot.h>
  75. #include <linux/stringify.h>
  76. #include <asm/io.h>
  77. #include <asm/irq.h>
  78. #include <asm/processor.h>
  79. #include <scsi/scsi.h>
  80. #include <scsi/scsi_host.h>
  81. #include <scsi/scsi_tcq.h>
  82. #include <scsi/scsi_eh.h>
  83. #include <scsi/scsi_cmnd.h>
  84. #include "ipr.h"
  85. /*
  86. * Global Data
  87. */
  88. static LIST_HEAD(ipr_ioa_head);
  89. static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
  90. static unsigned int ipr_max_speed = 1;
  91. static int ipr_testmode = 0;
  92. static unsigned int ipr_fastfail = 0;
  93. static unsigned int ipr_transop_timeout = 0;
  94. static unsigned int ipr_debug = 0;
  95. static unsigned int ipr_max_devs = IPR_DEFAULT_SIS64_DEVS;
  96. static unsigned int ipr_dual_ioa_raid = 1;
  97. static DEFINE_SPINLOCK(ipr_driver_lock);
  98. /* This table describes the differences between DMA controller chips */
  99. static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
  100. { /* Gemstone, Citrine, Obsidian, and Obsidian-E */
  101. .mailbox = 0x0042C,
  102. .cache_line_size = 0x20,
  103. {
  104. .set_interrupt_mask_reg = 0x0022C,
  105. .clr_interrupt_mask_reg = 0x00230,
  106. .clr_interrupt_mask_reg32 = 0x00230,
  107. .sense_interrupt_mask_reg = 0x0022C,
  108. .sense_interrupt_mask_reg32 = 0x0022C,
  109. .clr_interrupt_reg = 0x00228,
  110. .clr_interrupt_reg32 = 0x00228,
  111. .sense_interrupt_reg = 0x00224,
  112. .sense_interrupt_reg32 = 0x00224,
  113. .ioarrin_reg = 0x00404,
  114. .sense_uproc_interrupt_reg = 0x00214,
  115. .sense_uproc_interrupt_reg32 = 0x00214,
  116. .set_uproc_interrupt_reg = 0x00214,
  117. .set_uproc_interrupt_reg32 = 0x00214,
  118. .clr_uproc_interrupt_reg = 0x00218,
  119. .clr_uproc_interrupt_reg32 = 0x00218
  120. }
  121. },
  122. { /* Snipe and Scamp */
  123. .mailbox = 0x0052C,
  124. .cache_line_size = 0x20,
  125. {
  126. .set_interrupt_mask_reg = 0x00288,
  127. .clr_interrupt_mask_reg = 0x0028C,
  128. .clr_interrupt_mask_reg32 = 0x0028C,
  129. .sense_interrupt_mask_reg = 0x00288,
  130. .sense_interrupt_mask_reg32 = 0x00288,
  131. .clr_interrupt_reg = 0x00284,
  132. .clr_interrupt_reg32 = 0x00284,
  133. .sense_interrupt_reg = 0x00280,
  134. .sense_interrupt_reg32 = 0x00280,
  135. .ioarrin_reg = 0x00504,
  136. .sense_uproc_interrupt_reg = 0x00290,
  137. .sense_uproc_interrupt_reg32 = 0x00290,
  138. .set_uproc_interrupt_reg = 0x00290,
  139. .set_uproc_interrupt_reg32 = 0x00290,
  140. .clr_uproc_interrupt_reg = 0x00294,
  141. .clr_uproc_interrupt_reg32 = 0x00294
  142. }
  143. },
  144. { /* CRoC */
  145. .mailbox = 0x00040,
  146. .cache_line_size = 0x20,
  147. {
  148. .set_interrupt_mask_reg = 0x00010,
  149. .clr_interrupt_mask_reg = 0x00018,
  150. .clr_interrupt_mask_reg32 = 0x0001C,
  151. .sense_interrupt_mask_reg = 0x00010,
  152. .sense_interrupt_mask_reg32 = 0x00014,
  153. .clr_interrupt_reg = 0x00008,
  154. .clr_interrupt_reg32 = 0x0000C,
  155. .sense_interrupt_reg = 0x00000,
  156. .sense_interrupt_reg32 = 0x00004,
  157. .ioarrin_reg = 0x00070,
  158. .sense_uproc_interrupt_reg = 0x00020,
  159. .sense_uproc_interrupt_reg32 = 0x00024,
  160. .set_uproc_interrupt_reg = 0x00020,
  161. .set_uproc_interrupt_reg32 = 0x00024,
  162. .clr_uproc_interrupt_reg = 0x00028,
  163. .clr_uproc_interrupt_reg32 = 0x0002C,
  164. .init_feedback_reg = 0x0005C,
  165. .dump_addr_reg = 0x00064,
  166. .dump_data_reg = 0x00068
  167. }
  168. },
  169. };
  170. static const struct ipr_chip_t ipr_chip[] = {
  171. { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
  172. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
  173. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
  174. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[0] },
  175. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E, IPR_USE_MSI, IPR_SIS32, &ipr_chip_cfg[0] },
  176. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[1] },
  177. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP, IPR_USE_LSI, IPR_SIS32, &ipr_chip_cfg[1] },
  178. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2, IPR_USE_MSI, IPR_SIS64, &ipr_chip_cfg[2] },
  179. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2, IPR_USE_MSI, IPR_SIS64, &ipr_chip_cfg[2] }
  180. };
  181. static int ipr_max_bus_speeds [] = {
  182. IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
  183. };
  184. MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
  185. MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
  186. module_param_named(max_speed, ipr_max_speed, uint, 0);
  187. MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
  188. module_param_named(log_level, ipr_log_level, uint, 0);
  189. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
  190. module_param_named(testmode, ipr_testmode, int, 0);
  191. MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
  192. module_param_named(fastfail, ipr_fastfail, int, S_IRUGO | S_IWUSR);
  193. MODULE_PARM_DESC(fastfail, "Reduce timeouts and retries");
  194. module_param_named(transop_timeout, ipr_transop_timeout, int, 0);
  195. MODULE_PARM_DESC(transop_timeout, "Time in seconds to wait for adapter to come operational (default: 300)");
  196. module_param_named(debug, ipr_debug, int, S_IRUGO | S_IWUSR);
  197. MODULE_PARM_DESC(debug, "Enable device driver debugging logging. Set to 1 to enable. (default: 0)");
  198. module_param_named(dual_ioa_raid, ipr_dual_ioa_raid, int, 0);
  199. MODULE_PARM_DESC(dual_ioa_raid, "Enable dual adapter RAID support. Set to 1 to enable. (default: 1)");
  200. module_param_named(max_devs, ipr_max_devs, int, 0);
  201. MODULE_PARM_DESC(max_devs, "Specify the maximum number of physical devices. "
  202. "[Default=" __stringify(IPR_DEFAULT_SIS64_DEVS) "]");
  203. MODULE_LICENSE("GPL");
  204. MODULE_VERSION(IPR_DRIVER_VERSION);
  205. /* A constant array of IOASCs/URCs/Error Messages */
  206. static const
  207. struct ipr_error_table_t ipr_error_table[] = {
  208. {0x00000000, 1, IPR_DEFAULT_LOG_LEVEL,
  209. "8155: An unknown error was received"},
  210. {0x00330000, 0, 0,
  211. "Soft underlength error"},
  212. {0x005A0000, 0, 0,
  213. "Command to be cancelled not found"},
  214. {0x00808000, 0, 0,
  215. "Qualified success"},
  216. {0x01080000, 1, IPR_DEFAULT_LOG_LEVEL,
  217. "FFFE: Soft device bus error recovered by the IOA"},
  218. {0x01088100, 0, IPR_DEFAULT_LOG_LEVEL,
  219. "4101: Soft device bus fabric error"},
  220. {0x01100100, 0, IPR_DEFAULT_LOG_LEVEL,
  221. "FFFC: Logical block guard error recovered by the device"},
  222. {0x01100300, 0, IPR_DEFAULT_LOG_LEVEL,
  223. "FFFC: Logical block reference tag error recovered by the device"},
  224. {0x01108300, 0, IPR_DEFAULT_LOG_LEVEL,
  225. "4171: Recovered scatter list tag / sequence number error"},
  226. {0x01109000, 0, IPR_DEFAULT_LOG_LEVEL,
  227. "FF3D: Recovered logical block CRC error on IOA to Host transfer"},
  228. {0x01109200, 0, IPR_DEFAULT_LOG_LEVEL,
  229. "4171: Recovered logical block sequence number error on IOA to Host transfer"},
  230. {0x0110A000, 0, IPR_DEFAULT_LOG_LEVEL,
  231. "FFFD: Recovered logical block reference tag error detected by the IOA"},
  232. {0x0110A100, 0, IPR_DEFAULT_LOG_LEVEL,
  233. "FFFD: Logical block guard error recovered by the IOA"},
  234. {0x01170600, 0, IPR_DEFAULT_LOG_LEVEL,
  235. "FFF9: Device sector reassign successful"},
  236. {0x01170900, 0, IPR_DEFAULT_LOG_LEVEL,
  237. "FFF7: Media error recovered by device rewrite procedures"},
  238. {0x01180200, 0, IPR_DEFAULT_LOG_LEVEL,
  239. "7001: IOA sector reassignment successful"},
  240. {0x01180500, 0, IPR_DEFAULT_LOG_LEVEL,
  241. "FFF9: Soft media error. Sector reassignment recommended"},
  242. {0x01180600, 0, IPR_DEFAULT_LOG_LEVEL,
  243. "FFF7: Media error recovered by IOA rewrite procedures"},
  244. {0x01418000, 0, IPR_DEFAULT_LOG_LEVEL,
  245. "FF3D: Soft PCI bus error recovered by the IOA"},
  246. {0x01440000, 1, IPR_DEFAULT_LOG_LEVEL,
  247. "FFF6: Device hardware error recovered by the IOA"},
  248. {0x01448100, 0, IPR_DEFAULT_LOG_LEVEL,
  249. "FFF6: Device hardware error recovered by the device"},
  250. {0x01448200, 1, IPR_DEFAULT_LOG_LEVEL,
  251. "FF3D: Soft IOA error recovered by the IOA"},
  252. {0x01448300, 0, IPR_DEFAULT_LOG_LEVEL,
  253. "FFFA: Undefined device response recovered by the IOA"},
  254. {0x014A0000, 1, IPR_DEFAULT_LOG_LEVEL,
  255. "FFF6: Device bus error, message or command phase"},
  256. {0x014A8000, 0, IPR_DEFAULT_LOG_LEVEL,
  257. "FFFE: Task Management Function failed"},
  258. {0x015D0000, 0, IPR_DEFAULT_LOG_LEVEL,
  259. "FFF6: Failure prediction threshold exceeded"},
  260. {0x015D9200, 0, IPR_DEFAULT_LOG_LEVEL,
  261. "8009: Impending cache battery pack failure"},
  262. {0x02040400, 0, 0,
  263. "34FF: Disk device format in progress"},
  264. {0x02048000, 0, IPR_DEFAULT_LOG_LEVEL,
  265. "9070: IOA requested reset"},
  266. {0x023F0000, 0, 0,
  267. "Synchronization required"},
  268. {0x024E0000, 0, 0,
  269. "No ready, IOA shutdown"},
  270. {0x025A0000, 0, 0,
  271. "Not ready, IOA has been shutdown"},
  272. {0x02670100, 0, IPR_DEFAULT_LOG_LEVEL,
  273. "3020: Storage subsystem configuration error"},
  274. {0x03110B00, 0, 0,
  275. "FFF5: Medium error, data unreadable, recommend reassign"},
  276. {0x03110C00, 0, 0,
  277. "7000: Medium error, data unreadable, do not reassign"},
  278. {0x03310000, 0, IPR_DEFAULT_LOG_LEVEL,
  279. "FFF3: Disk media format bad"},
  280. {0x04050000, 0, IPR_DEFAULT_LOG_LEVEL,
  281. "3002: Addressed device failed to respond to selection"},
  282. {0x04080000, 1, IPR_DEFAULT_LOG_LEVEL,
  283. "3100: Device bus error"},
  284. {0x04080100, 0, IPR_DEFAULT_LOG_LEVEL,
  285. "3109: IOA timed out a device command"},
  286. {0x04088000, 0, 0,
  287. "3120: SCSI bus is not operational"},
  288. {0x04088100, 0, IPR_DEFAULT_LOG_LEVEL,
  289. "4100: Hard device bus fabric error"},
  290. {0x04100100, 0, IPR_DEFAULT_LOG_LEVEL,
  291. "310C: Logical block guard error detected by the device"},
  292. {0x04100300, 0, IPR_DEFAULT_LOG_LEVEL,
  293. "310C: Logical block reference tag error detected by the device"},
  294. {0x04108300, 1, IPR_DEFAULT_LOG_LEVEL,
  295. "4170: Scatter list tag / sequence number error"},
  296. {0x04109000, 1, IPR_DEFAULT_LOG_LEVEL,
  297. "8150: Logical block CRC error on IOA to Host transfer"},
  298. {0x04109200, 1, IPR_DEFAULT_LOG_LEVEL,
  299. "4170: Logical block sequence number error on IOA to Host transfer"},
  300. {0x0410A000, 0, IPR_DEFAULT_LOG_LEVEL,
  301. "310D: Logical block reference tag error detected by the IOA"},
  302. {0x0410A100, 0, IPR_DEFAULT_LOG_LEVEL,
  303. "310D: Logical block guard error detected by the IOA"},
  304. {0x04118000, 0, IPR_DEFAULT_LOG_LEVEL,
  305. "9000: IOA reserved area data check"},
  306. {0x04118100, 0, IPR_DEFAULT_LOG_LEVEL,
  307. "9001: IOA reserved area invalid data pattern"},
  308. {0x04118200, 0, IPR_DEFAULT_LOG_LEVEL,
  309. "9002: IOA reserved area LRC error"},
  310. {0x04118300, 1, IPR_DEFAULT_LOG_LEVEL,
  311. "Hardware Error, IOA metadata access error"},
  312. {0x04320000, 0, IPR_DEFAULT_LOG_LEVEL,
  313. "102E: Out of alternate sectors for disk storage"},
  314. {0x04330000, 1, IPR_DEFAULT_LOG_LEVEL,
  315. "FFF4: Data transfer underlength error"},
  316. {0x04338000, 1, IPR_DEFAULT_LOG_LEVEL,
  317. "FFF4: Data transfer overlength error"},
  318. {0x043E0100, 0, IPR_DEFAULT_LOG_LEVEL,
  319. "3400: Logical unit failure"},
  320. {0x04408500, 0, IPR_DEFAULT_LOG_LEVEL,
  321. "FFF4: Device microcode is corrupt"},
  322. {0x04418000, 1, IPR_DEFAULT_LOG_LEVEL,
  323. "8150: PCI bus error"},
  324. {0x04430000, 1, 0,
  325. "Unsupported device bus message received"},
  326. {0x04440000, 1, IPR_DEFAULT_LOG_LEVEL,
  327. "FFF4: Disk device problem"},
  328. {0x04448200, 1, IPR_DEFAULT_LOG_LEVEL,
  329. "8150: Permanent IOA failure"},
  330. {0x04448300, 0, IPR_DEFAULT_LOG_LEVEL,
  331. "3010: Disk device returned wrong response to IOA"},
  332. {0x04448400, 0, IPR_DEFAULT_LOG_LEVEL,
  333. "8151: IOA microcode error"},
  334. {0x04448500, 0, 0,
  335. "Device bus status error"},
  336. {0x04448600, 0, IPR_DEFAULT_LOG_LEVEL,
  337. "8157: IOA error requiring IOA reset to recover"},
  338. {0x04448700, 0, 0,
  339. "ATA device status error"},
  340. {0x04490000, 0, 0,
  341. "Message reject received from the device"},
  342. {0x04449200, 0, IPR_DEFAULT_LOG_LEVEL,
  343. "8008: A permanent cache battery pack failure occurred"},
  344. {0x0444A000, 0, IPR_DEFAULT_LOG_LEVEL,
  345. "9090: Disk unit has been modified after the last known status"},
  346. {0x0444A200, 0, IPR_DEFAULT_LOG_LEVEL,
  347. "9081: IOA detected device error"},
  348. {0x0444A300, 0, IPR_DEFAULT_LOG_LEVEL,
  349. "9082: IOA detected device error"},
  350. {0x044A0000, 1, IPR_DEFAULT_LOG_LEVEL,
  351. "3110: Device bus error, message or command phase"},
  352. {0x044A8000, 1, IPR_DEFAULT_LOG_LEVEL,
  353. "3110: SAS Command / Task Management Function failed"},
  354. {0x04670400, 0, IPR_DEFAULT_LOG_LEVEL,
  355. "9091: Incorrect hardware configuration change has been detected"},
  356. {0x04678000, 0, IPR_DEFAULT_LOG_LEVEL,
  357. "9073: Invalid multi-adapter configuration"},
  358. {0x04678100, 0, IPR_DEFAULT_LOG_LEVEL,
  359. "4010: Incorrect connection between cascaded expanders"},
  360. {0x04678200, 0, IPR_DEFAULT_LOG_LEVEL,
  361. "4020: Connections exceed IOA design limits"},
  362. {0x04678300, 0, IPR_DEFAULT_LOG_LEVEL,
  363. "4030: Incorrect multipath connection"},
  364. {0x04679000, 0, IPR_DEFAULT_LOG_LEVEL,
  365. "4110: Unsupported enclosure function"},
  366. {0x046E0000, 0, IPR_DEFAULT_LOG_LEVEL,
  367. "FFF4: Command to logical unit failed"},
  368. {0x05240000, 1, 0,
  369. "Illegal request, invalid request type or request packet"},
  370. {0x05250000, 0, 0,
  371. "Illegal request, invalid resource handle"},
  372. {0x05258000, 0, 0,
  373. "Illegal request, commands not allowed to this device"},
  374. {0x05258100, 0, 0,
  375. "Illegal request, command not allowed to a secondary adapter"},
  376. {0x05258200, 0, 0,
  377. "Illegal request, command not allowed to a non-optimized resource"},
  378. {0x05260000, 0, 0,
  379. "Illegal request, invalid field in parameter list"},
  380. {0x05260100, 0, 0,
  381. "Illegal request, parameter not supported"},
  382. {0x05260200, 0, 0,
  383. "Illegal request, parameter value invalid"},
  384. {0x052C0000, 0, 0,
  385. "Illegal request, command sequence error"},
  386. {0x052C8000, 1, 0,
  387. "Illegal request, dual adapter support not enabled"},
  388. {0x06040500, 0, IPR_DEFAULT_LOG_LEVEL,
  389. "9031: Array protection temporarily suspended, protection resuming"},
  390. {0x06040600, 0, IPR_DEFAULT_LOG_LEVEL,
  391. "9040: Array protection temporarily suspended, protection resuming"},
  392. {0x06288000, 0, IPR_DEFAULT_LOG_LEVEL,
  393. "3140: Device bus not ready to ready transition"},
  394. {0x06290000, 0, IPR_DEFAULT_LOG_LEVEL,
  395. "FFFB: SCSI bus was reset"},
  396. {0x06290500, 0, 0,
  397. "FFFE: SCSI bus transition to single ended"},
  398. {0x06290600, 0, 0,
  399. "FFFE: SCSI bus transition to LVD"},
  400. {0x06298000, 0, IPR_DEFAULT_LOG_LEVEL,
  401. "FFFB: SCSI bus was reset by another initiator"},
  402. {0x063F0300, 0, IPR_DEFAULT_LOG_LEVEL,
  403. "3029: A device replacement has occurred"},
  404. {0x064C8000, 0, IPR_DEFAULT_LOG_LEVEL,
  405. "9051: IOA cache data exists for a missing or failed device"},
  406. {0x064C8100, 0, IPR_DEFAULT_LOG_LEVEL,
  407. "9055: Auxiliary cache IOA contains cache data needed by the primary IOA"},
  408. {0x06670100, 0, IPR_DEFAULT_LOG_LEVEL,
  409. "9025: Disk unit is not supported at its physical location"},
  410. {0x06670600, 0, IPR_DEFAULT_LOG_LEVEL,
  411. "3020: IOA detected a SCSI bus configuration error"},
  412. {0x06678000, 0, IPR_DEFAULT_LOG_LEVEL,
  413. "3150: SCSI bus configuration error"},
  414. {0x06678100, 0, IPR_DEFAULT_LOG_LEVEL,
  415. "9074: Asymmetric advanced function disk configuration"},
  416. {0x06678300, 0, IPR_DEFAULT_LOG_LEVEL,
  417. "4040: Incomplete multipath connection between IOA and enclosure"},
  418. {0x06678400, 0, IPR_DEFAULT_LOG_LEVEL,
  419. "4041: Incomplete multipath connection between enclosure and device"},
  420. {0x06678500, 0, IPR_DEFAULT_LOG_LEVEL,
  421. "9075: Incomplete multipath connection between IOA and remote IOA"},
  422. {0x06678600, 0, IPR_DEFAULT_LOG_LEVEL,
  423. "9076: Configuration error, missing remote IOA"},
  424. {0x06679100, 0, IPR_DEFAULT_LOG_LEVEL,
  425. "4050: Enclosure does not support a required multipath function"},
  426. {0x06690000, 0, IPR_DEFAULT_LOG_LEVEL,
  427. "4070: Logically bad block written on device"},
  428. {0x06690200, 0, IPR_DEFAULT_LOG_LEVEL,
  429. "9041: Array protection temporarily suspended"},
  430. {0x06698200, 0, IPR_DEFAULT_LOG_LEVEL,
  431. "9042: Corrupt array parity detected on specified device"},
  432. {0x066B0200, 0, IPR_DEFAULT_LOG_LEVEL,
  433. "9030: Array no longer protected due to missing or failed disk unit"},
  434. {0x066B8000, 0, IPR_DEFAULT_LOG_LEVEL,
  435. "9071: Link operational transition"},
  436. {0x066B8100, 0, IPR_DEFAULT_LOG_LEVEL,
  437. "9072: Link not operational transition"},
  438. {0x066B8200, 0, IPR_DEFAULT_LOG_LEVEL,
  439. "9032: Array exposed but still protected"},
  440. {0x066B8300, 0, IPR_DEFAULT_LOG_LEVEL + 1,
  441. "70DD: Device forced failed by disrupt device command"},
  442. {0x066B9100, 0, IPR_DEFAULT_LOG_LEVEL,
  443. "4061: Multipath redundancy level got better"},
  444. {0x066B9200, 0, IPR_DEFAULT_LOG_LEVEL,
  445. "4060: Multipath redundancy level got worse"},
  446. {0x07270000, 0, 0,
  447. "Failure due to other device"},
  448. {0x07278000, 0, IPR_DEFAULT_LOG_LEVEL,
  449. "9008: IOA does not support functions expected by devices"},
  450. {0x07278100, 0, IPR_DEFAULT_LOG_LEVEL,
  451. "9010: Cache data associated with attached devices cannot be found"},
  452. {0x07278200, 0, IPR_DEFAULT_LOG_LEVEL,
  453. "9011: Cache data belongs to devices other than those attached"},
  454. {0x07278400, 0, IPR_DEFAULT_LOG_LEVEL,
  455. "9020: Array missing 2 or more devices with only 1 device present"},
  456. {0x07278500, 0, IPR_DEFAULT_LOG_LEVEL,
  457. "9021: Array missing 2 or more devices with 2 or more devices present"},
  458. {0x07278600, 0, IPR_DEFAULT_LOG_LEVEL,
  459. "9022: Exposed array is missing a required device"},
  460. {0x07278700, 0, IPR_DEFAULT_LOG_LEVEL,
  461. "9023: Array member(s) not at required physical locations"},
  462. {0x07278800, 0, IPR_DEFAULT_LOG_LEVEL,
  463. "9024: Array not functional due to present hardware configuration"},
  464. {0x07278900, 0, IPR_DEFAULT_LOG_LEVEL,
  465. "9026: Array not functional due to present hardware configuration"},
  466. {0x07278A00, 0, IPR_DEFAULT_LOG_LEVEL,
  467. "9027: Array is missing a device and parity is out of sync"},
  468. {0x07278B00, 0, IPR_DEFAULT_LOG_LEVEL,
  469. "9028: Maximum number of arrays already exist"},
  470. {0x07278C00, 0, IPR_DEFAULT_LOG_LEVEL,
  471. "9050: Required cache data cannot be located for a disk unit"},
  472. {0x07278D00, 0, IPR_DEFAULT_LOG_LEVEL,
  473. "9052: Cache data exists for a device that has been modified"},
  474. {0x07278F00, 0, IPR_DEFAULT_LOG_LEVEL,
  475. "9054: IOA resources not available due to previous problems"},
  476. {0x07279100, 0, IPR_DEFAULT_LOG_LEVEL,
  477. "9092: Disk unit requires initialization before use"},
  478. {0x07279200, 0, IPR_DEFAULT_LOG_LEVEL,
  479. "9029: Incorrect hardware configuration change has been detected"},
  480. {0x07279600, 0, IPR_DEFAULT_LOG_LEVEL,
  481. "9060: One or more disk pairs are missing from an array"},
  482. {0x07279700, 0, IPR_DEFAULT_LOG_LEVEL,
  483. "9061: One or more disks are missing from an array"},
  484. {0x07279800, 0, IPR_DEFAULT_LOG_LEVEL,
  485. "9062: One or more disks are missing from an array"},
  486. {0x07279900, 0, IPR_DEFAULT_LOG_LEVEL,
  487. "9063: Maximum number of functional arrays has been exceeded"},
  488. {0x0B260000, 0, 0,
  489. "Aborted command, invalid descriptor"},
  490. {0x0B5A0000, 0, 0,
  491. "Command terminated by host"}
  492. };
  493. static const struct ipr_ses_table_entry ipr_ses_table[] = {
  494. { "2104-DL1 ", "XXXXXXXXXXXXXXXX", 80 },
  495. { "2104-TL1 ", "XXXXXXXXXXXXXXXX", 80 },
  496. { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
  497. { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
  498. { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
  499. { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
  500. { "2104-DU3 ", "XXXXXXXXXXXXXXXX", 160 },
  501. { "2104-TU3 ", "XXXXXXXXXXXXXXXX", 160 },
  502. { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
  503. { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
  504. { "St V1S2 ", "XXXXXXXXXXXXXXXX", 160 },
  505. { "HSBPD4M PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
  506. { "VSBPD1H U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
  507. };
  508. /*
  509. * Function Prototypes
  510. */
  511. static int ipr_reset_alert(struct ipr_cmnd *);
  512. static void ipr_process_ccn(struct ipr_cmnd *);
  513. static void ipr_process_error(struct ipr_cmnd *);
  514. static void ipr_reset_ioa_job(struct ipr_cmnd *);
  515. static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
  516. enum ipr_shutdown_type);
  517. #ifdef CONFIG_SCSI_IPR_TRACE
  518. /**
  519. * ipr_trc_hook - Add a trace entry to the driver trace
  520. * @ipr_cmd: ipr command struct
  521. * @type: trace type
  522. * @add_data: additional data
  523. *
  524. * Return value:
  525. * none
  526. **/
  527. static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
  528. u8 type, u32 add_data)
  529. {
  530. struct ipr_trace_entry *trace_entry;
  531. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  532. trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
  533. trace_entry->time = jiffies;
  534. trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
  535. trace_entry->type = type;
  536. if (ipr_cmd->ioa_cfg->sis64)
  537. trace_entry->ata_op_code = ipr_cmd->i.ata_ioadl.regs.command;
  538. else
  539. trace_entry->ata_op_code = ipr_cmd->ioarcb.u.add_data.u.regs.command;
  540. trace_entry->cmd_index = ipr_cmd->cmd_index & 0xff;
  541. trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
  542. trace_entry->u.add_data = add_data;
  543. }
  544. #else
  545. #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
  546. #endif
  547. /**
  548. * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
  549. * @ipr_cmd: ipr command struct
  550. *
  551. * Return value:
  552. * none
  553. **/
  554. static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
  555. {
  556. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  557. struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
  558. dma_addr_t dma_addr = ipr_cmd->dma_addr;
  559. memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
  560. ioarcb->data_transfer_length = 0;
  561. ioarcb->read_data_transfer_length = 0;
  562. ioarcb->ioadl_len = 0;
  563. ioarcb->read_ioadl_len = 0;
  564. if (ipr_cmd->ioa_cfg->sis64)
  565. ioarcb->u.sis64_addr_data.data_ioadl_addr =
  566. cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
  567. else {
  568. ioarcb->write_ioadl_addr =
  569. cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
  570. ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
  571. }
  572. ioasa->ioasc = 0;
  573. ioasa->residual_data_len = 0;
  574. ioasa->u.gata.status = 0;
  575. ipr_cmd->scsi_cmd = NULL;
  576. ipr_cmd->qc = NULL;
  577. ipr_cmd->sense_buffer[0] = 0;
  578. ipr_cmd->dma_use_sg = 0;
  579. }
  580. /**
  581. * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
  582. * @ipr_cmd: ipr command struct
  583. *
  584. * Return value:
  585. * none
  586. **/
  587. static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
  588. {
  589. ipr_reinit_ipr_cmnd(ipr_cmd);
  590. ipr_cmd->u.scratch = 0;
  591. ipr_cmd->sibling = NULL;
  592. init_timer(&ipr_cmd->timer);
  593. }
  594. /**
  595. * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
  596. * @ioa_cfg: ioa config struct
  597. *
  598. * Return value:
  599. * pointer to ipr command struct
  600. **/
  601. static
  602. struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
  603. {
  604. struct ipr_cmnd *ipr_cmd;
  605. ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
  606. list_del(&ipr_cmd->queue);
  607. ipr_init_ipr_cmnd(ipr_cmd);
  608. return ipr_cmd;
  609. }
  610. /**
  611. * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
  612. * @ioa_cfg: ioa config struct
  613. * @clr_ints: interrupts to clear
  614. *
  615. * This function masks all interrupts on the adapter, then clears the
  616. * interrupts specified in the mask
  617. *
  618. * Return value:
  619. * none
  620. **/
  621. static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
  622. u32 clr_ints)
  623. {
  624. volatile u32 int_reg;
  625. /* Stop new interrupts */
  626. ioa_cfg->allow_interrupts = 0;
  627. /* Set interrupt mask to stop all new interrupts */
  628. if (ioa_cfg->sis64)
  629. writeq(~0, ioa_cfg->regs.set_interrupt_mask_reg);
  630. else
  631. writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
  632. /* Clear any pending interrupts */
  633. if (ioa_cfg->sis64)
  634. writel(~0, ioa_cfg->regs.clr_interrupt_reg);
  635. writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg32);
  636. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
  637. }
  638. /**
  639. * ipr_save_pcix_cmd_reg - Save PCI-X command register
  640. * @ioa_cfg: ioa config struct
  641. *
  642. * Return value:
  643. * 0 on success / -EIO on failure
  644. **/
  645. static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
  646. {
  647. int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
  648. if (pcix_cmd_reg == 0)
  649. return 0;
  650. if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
  651. &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
  652. dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
  653. return -EIO;
  654. }
  655. ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
  656. return 0;
  657. }
  658. /**
  659. * ipr_set_pcix_cmd_reg - Setup PCI-X command register
  660. * @ioa_cfg: ioa config struct
  661. *
  662. * Return value:
  663. * 0 on success / -EIO on failure
  664. **/
  665. static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
  666. {
  667. int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
  668. if (pcix_cmd_reg) {
  669. if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg + PCI_X_CMD,
  670. ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
  671. dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
  672. return -EIO;
  673. }
  674. }
  675. return 0;
  676. }
  677. /**
  678. * ipr_sata_eh_done - done function for aborted SATA commands
  679. * @ipr_cmd: ipr command struct
  680. *
  681. * This function is invoked for ops generated to SATA
  682. * devices which are being aborted.
  683. *
  684. * Return value:
  685. * none
  686. **/
  687. static void ipr_sata_eh_done(struct ipr_cmnd *ipr_cmd)
  688. {
  689. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  690. struct ata_queued_cmd *qc = ipr_cmd->qc;
  691. struct ipr_sata_port *sata_port = qc->ap->private_data;
  692. qc->err_mask |= AC_ERR_OTHER;
  693. sata_port->ioasa.status |= ATA_BUSY;
  694. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  695. ata_qc_complete(qc);
  696. }
  697. /**
  698. * ipr_scsi_eh_done - mid-layer done function for aborted ops
  699. * @ipr_cmd: ipr command struct
  700. *
  701. * This function is invoked by the interrupt handler for
  702. * ops generated by the SCSI mid-layer which are being aborted.
  703. *
  704. * Return value:
  705. * none
  706. **/
  707. static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
  708. {
  709. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  710. struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
  711. scsi_cmd->result |= (DID_ERROR << 16);
  712. scsi_dma_unmap(ipr_cmd->scsi_cmd);
  713. scsi_cmd->scsi_done(scsi_cmd);
  714. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  715. }
  716. /**
  717. * ipr_fail_all_ops - Fails all outstanding ops.
  718. * @ioa_cfg: ioa config struct
  719. *
  720. * This function fails all outstanding ops.
  721. *
  722. * Return value:
  723. * none
  724. **/
  725. static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
  726. {
  727. struct ipr_cmnd *ipr_cmd, *temp;
  728. ENTER;
  729. list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
  730. list_del(&ipr_cmd->queue);
  731. ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
  732. ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
  733. if (ipr_cmd->scsi_cmd)
  734. ipr_cmd->done = ipr_scsi_eh_done;
  735. else if (ipr_cmd->qc)
  736. ipr_cmd->done = ipr_sata_eh_done;
  737. ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
  738. del_timer(&ipr_cmd->timer);
  739. ipr_cmd->done(ipr_cmd);
  740. }
  741. LEAVE;
  742. }
  743. /**
  744. * ipr_send_command - Send driver initiated requests.
  745. * @ipr_cmd: ipr command struct
  746. *
  747. * This function sends a command to the adapter using the correct write call.
  748. * In the case of sis64, calculate the ioarcb size required. Then or in the
  749. * appropriate bits.
  750. *
  751. * Return value:
  752. * none
  753. **/
  754. static void ipr_send_command(struct ipr_cmnd *ipr_cmd)
  755. {
  756. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  757. dma_addr_t send_dma_addr = ipr_cmd->dma_addr;
  758. if (ioa_cfg->sis64) {
  759. /* The default size is 256 bytes */
  760. send_dma_addr |= 0x1;
  761. /* If the number of ioadls * size of ioadl > 128 bytes,
  762. then use a 512 byte ioarcb */
  763. if (ipr_cmd->dma_use_sg * sizeof(struct ipr_ioadl64_desc) > 128 )
  764. send_dma_addr |= 0x4;
  765. writeq(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
  766. } else
  767. writel(send_dma_addr, ioa_cfg->regs.ioarrin_reg);
  768. }
  769. /**
  770. * ipr_do_req - Send driver initiated requests.
  771. * @ipr_cmd: ipr command struct
  772. * @done: done function
  773. * @timeout_func: timeout function
  774. * @timeout: timeout value
  775. *
  776. * This function sends the specified command to the adapter with the
  777. * timeout given. The done function is invoked on command completion.
  778. *
  779. * Return value:
  780. * none
  781. **/
  782. static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
  783. void (*done) (struct ipr_cmnd *),
  784. void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
  785. {
  786. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  787. list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
  788. ipr_cmd->done = done;
  789. ipr_cmd->timer.data = (unsigned long) ipr_cmd;
  790. ipr_cmd->timer.expires = jiffies + timeout;
  791. ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
  792. add_timer(&ipr_cmd->timer);
  793. ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
  794. mb();
  795. ipr_send_command(ipr_cmd);
  796. }
  797. /**
  798. * ipr_internal_cmd_done - Op done function for an internally generated op.
  799. * @ipr_cmd: ipr command struct
  800. *
  801. * This function is the op done function for an internally generated,
  802. * blocking op. It simply wakes the sleeping thread.
  803. *
  804. * Return value:
  805. * none
  806. **/
  807. static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
  808. {
  809. if (ipr_cmd->sibling)
  810. ipr_cmd->sibling = NULL;
  811. else
  812. complete(&ipr_cmd->completion);
  813. }
  814. /**
  815. * ipr_init_ioadl - initialize the ioadl for the correct SIS type
  816. * @ipr_cmd: ipr command struct
  817. * @dma_addr: dma address
  818. * @len: transfer length
  819. * @flags: ioadl flag value
  820. *
  821. * This function initializes an ioadl in the case where there is only a single
  822. * descriptor.
  823. *
  824. * Return value:
  825. * nothing
  826. **/
  827. static void ipr_init_ioadl(struct ipr_cmnd *ipr_cmd, dma_addr_t dma_addr,
  828. u32 len, int flags)
  829. {
  830. struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
  831. struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
  832. ipr_cmd->dma_use_sg = 1;
  833. if (ipr_cmd->ioa_cfg->sis64) {
  834. ioadl64->flags = cpu_to_be32(flags);
  835. ioadl64->data_len = cpu_to_be32(len);
  836. ioadl64->address = cpu_to_be64(dma_addr);
  837. ipr_cmd->ioarcb.ioadl_len =
  838. cpu_to_be32(sizeof(struct ipr_ioadl64_desc));
  839. ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
  840. } else {
  841. ioadl->flags_and_data_len = cpu_to_be32(flags | len);
  842. ioadl->address = cpu_to_be32(dma_addr);
  843. if (flags == IPR_IOADL_FLAGS_READ_LAST) {
  844. ipr_cmd->ioarcb.read_ioadl_len =
  845. cpu_to_be32(sizeof(struct ipr_ioadl_desc));
  846. ipr_cmd->ioarcb.read_data_transfer_length = cpu_to_be32(len);
  847. } else {
  848. ipr_cmd->ioarcb.ioadl_len =
  849. cpu_to_be32(sizeof(struct ipr_ioadl_desc));
  850. ipr_cmd->ioarcb.data_transfer_length = cpu_to_be32(len);
  851. }
  852. }
  853. }
  854. /**
  855. * ipr_send_blocking_cmd - Send command and sleep on its completion.
  856. * @ipr_cmd: ipr command struct
  857. * @timeout_func: function to invoke if command times out
  858. * @timeout: timeout
  859. *
  860. * Return value:
  861. * none
  862. **/
  863. static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
  864. void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
  865. u32 timeout)
  866. {
  867. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  868. init_completion(&ipr_cmd->completion);
  869. ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
  870. spin_unlock_irq(ioa_cfg->host->host_lock);
  871. wait_for_completion(&ipr_cmd->completion);
  872. spin_lock_irq(ioa_cfg->host->host_lock);
  873. }
  874. /**
  875. * ipr_send_hcam - Send an HCAM to the adapter.
  876. * @ioa_cfg: ioa config struct
  877. * @type: HCAM type
  878. * @hostrcb: hostrcb struct
  879. *
  880. * This function will send a Host Controlled Async command to the adapter.
  881. * If HCAMs are currently not allowed to be issued to the adapter, it will
  882. * place the hostrcb on the free queue.
  883. *
  884. * Return value:
  885. * none
  886. **/
  887. static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
  888. struct ipr_hostrcb *hostrcb)
  889. {
  890. struct ipr_cmnd *ipr_cmd;
  891. struct ipr_ioarcb *ioarcb;
  892. if (ioa_cfg->allow_cmds) {
  893. ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  894. list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
  895. list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
  896. ipr_cmd->u.hostrcb = hostrcb;
  897. ioarcb = &ipr_cmd->ioarcb;
  898. ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  899. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
  900. ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
  901. ioarcb->cmd_pkt.cdb[1] = type;
  902. ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
  903. ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
  904. ipr_init_ioadl(ipr_cmd, hostrcb->hostrcb_dma,
  905. sizeof(hostrcb->hcam), IPR_IOADL_FLAGS_READ_LAST);
  906. if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
  907. ipr_cmd->done = ipr_process_ccn;
  908. else
  909. ipr_cmd->done = ipr_process_error;
  910. ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
  911. mb();
  912. ipr_send_command(ipr_cmd);
  913. } else {
  914. list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
  915. }
  916. }
  917. /**
  918. * ipr_update_ata_class - Update the ata class in the resource entry
  919. * @res: resource entry struct
  920. * @proto: cfgte device bus protocol value
  921. *
  922. * Return value:
  923. * none
  924. **/
  925. static void ipr_update_ata_class(struct ipr_resource_entry *res, unsigned int proto)
  926. {
  927. switch(proto) {
  928. case IPR_PROTO_SATA:
  929. case IPR_PROTO_SAS_STP:
  930. res->ata_class = ATA_DEV_ATA;
  931. break;
  932. case IPR_PROTO_SATA_ATAPI:
  933. case IPR_PROTO_SAS_STP_ATAPI:
  934. res->ata_class = ATA_DEV_ATAPI;
  935. break;
  936. default:
  937. res->ata_class = ATA_DEV_UNKNOWN;
  938. break;
  939. };
  940. }
  941. /**
  942. * ipr_init_res_entry - Initialize a resource entry struct.
  943. * @res: resource entry struct
  944. * @cfgtew: config table entry wrapper struct
  945. *
  946. * Return value:
  947. * none
  948. **/
  949. static void ipr_init_res_entry(struct ipr_resource_entry *res,
  950. struct ipr_config_table_entry_wrapper *cfgtew)
  951. {
  952. int found = 0;
  953. unsigned int proto;
  954. struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
  955. struct ipr_resource_entry *gscsi_res = NULL;
  956. res->needs_sync_complete = 0;
  957. res->in_erp = 0;
  958. res->add_to_ml = 0;
  959. res->del_from_ml = 0;
  960. res->resetting_device = 0;
  961. res->sdev = NULL;
  962. res->sata_port = NULL;
  963. if (ioa_cfg->sis64) {
  964. proto = cfgtew->u.cfgte64->proto;
  965. res->res_flags = cfgtew->u.cfgte64->res_flags;
  966. res->qmodel = IPR_QUEUEING_MODEL64(res);
  967. res->type = cfgtew->u.cfgte64->res_type & 0x0f;
  968. memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
  969. sizeof(res->res_path));
  970. res->bus = 0;
  971. res->lun = scsilun_to_int(&res->dev_lun);
  972. if (res->type == IPR_RES_TYPE_GENERIC_SCSI) {
  973. list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue) {
  974. if (gscsi_res->dev_id == cfgtew->u.cfgte64->dev_id) {
  975. found = 1;
  976. res->target = gscsi_res->target;
  977. break;
  978. }
  979. }
  980. if (!found) {
  981. res->target = find_first_zero_bit(ioa_cfg->target_ids,
  982. ioa_cfg->max_devs_supported);
  983. set_bit(res->target, ioa_cfg->target_ids);
  984. }
  985. memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
  986. sizeof(res->dev_lun.scsi_lun));
  987. } else if (res->type == IPR_RES_TYPE_IOAFP) {
  988. res->bus = IPR_IOAFP_VIRTUAL_BUS;
  989. res->target = 0;
  990. } else if (res->type == IPR_RES_TYPE_ARRAY) {
  991. res->bus = IPR_ARRAY_VIRTUAL_BUS;
  992. res->target = find_first_zero_bit(ioa_cfg->array_ids,
  993. ioa_cfg->max_devs_supported);
  994. set_bit(res->target, ioa_cfg->array_ids);
  995. } else if (res->type == IPR_RES_TYPE_VOLUME_SET) {
  996. res->bus = IPR_VSET_VIRTUAL_BUS;
  997. res->target = find_first_zero_bit(ioa_cfg->vset_ids,
  998. ioa_cfg->max_devs_supported);
  999. set_bit(res->target, ioa_cfg->vset_ids);
  1000. } else {
  1001. res->target = find_first_zero_bit(ioa_cfg->target_ids,
  1002. ioa_cfg->max_devs_supported);
  1003. set_bit(res->target, ioa_cfg->target_ids);
  1004. }
  1005. } else {
  1006. proto = cfgtew->u.cfgte->proto;
  1007. res->qmodel = IPR_QUEUEING_MODEL(res);
  1008. res->flags = cfgtew->u.cfgte->flags;
  1009. if (res->flags & IPR_IS_IOA_RESOURCE)
  1010. res->type = IPR_RES_TYPE_IOAFP;
  1011. else
  1012. res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
  1013. res->bus = cfgtew->u.cfgte->res_addr.bus;
  1014. res->target = cfgtew->u.cfgte->res_addr.target;
  1015. res->lun = cfgtew->u.cfgte->res_addr.lun;
  1016. }
  1017. ipr_update_ata_class(res, proto);
  1018. }
  1019. /**
  1020. * ipr_is_same_device - Determine if two devices are the same.
  1021. * @res: resource entry struct
  1022. * @cfgtew: config table entry wrapper struct
  1023. *
  1024. * Return value:
  1025. * 1 if the devices are the same / 0 otherwise
  1026. **/
  1027. static int ipr_is_same_device(struct ipr_resource_entry *res,
  1028. struct ipr_config_table_entry_wrapper *cfgtew)
  1029. {
  1030. if (res->ioa_cfg->sis64) {
  1031. if (!memcmp(&res->dev_id, &cfgtew->u.cfgte64->dev_id,
  1032. sizeof(cfgtew->u.cfgte64->dev_id)) &&
  1033. !memcmp(&res->lun, &cfgtew->u.cfgte64->lun,
  1034. sizeof(cfgtew->u.cfgte64->lun))) {
  1035. return 1;
  1036. }
  1037. } else {
  1038. if (res->bus == cfgtew->u.cfgte->res_addr.bus &&
  1039. res->target == cfgtew->u.cfgte->res_addr.target &&
  1040. res->lun == cfgtew->u.cfgte->res_addr.lun)
  1041. return 1;
  1042. }
  1043. return 0;
  1044. }
  1045. /**
  1046. * ipr_format_resource_path - Format the resource path for printing.
  1047. * @res_path: resource path
  1048. * @buf: buffer
  1049. *
  1050. * Return value:
  1051. * pointer to buffer
  1052. **/
  1053. static char *ipr_format_resource_path(u8 *res_path, char *buffer)
  1054. {
  1055. int i;
  1056. sprintf(buffer, "%02X", res_path[0]);
  1057. for (i=1; res_path[i] != 0xff; i++)
  1058. sprintf(buffer, "%s-%02X", buffer, res_path[i]);
  1059. return buffer;
  1060. }
  1061. /**
  1062. * ipr_update_res_entry - Update the resource entry.
  1063. * @res: resource entry struct
  1064. * @cfgtew: config table entry wrapper struct
  1065. *
  1066. * Return value:
  1067. * none
  1068. **/
  1069. static void ipr_update_res_entry(struct ipr_resource_entry *res,
  1070. struct ipr_config_table_entry_wrapper *cfgtew)
  1071. {
  1072. char buffer[IPR_MAX_RES_PATH_LENGTH];
  1073. unsigned int proto;
  1074. int new_path = 0;
  1075. if (res->ioa_cfg->sis64) {
  1076. res->flags = cfgtew->u.cfgte64->flags;
  1077. res->res_flags = cfgtew->u.cfgte64->res_flags;
  1078. res->type = cfgtew->u.cfgte64->res_type & 0x0f;
  1079. memcpy(&res->std_inq_data, &cfgtew->u.cfgte64->std_inq_data,
  1080. sizeof(struct ipr_std_inq_data));
  1081. res->qmodel = IPR_QUEUEING_MODEL64(res);
  1082. proto = cfgtew->u.cfgte64->proto;
  1083. res->res_handle = cfgtew->u.cfgte64->res_handle;
  1084. res->dev_id = cfgtew->u.cfgte64->dev_id;
  1085. memcpy(&res->dev_lun.scsi_lun, &cfgtew->u.cfgte64->lun,
  1086. sizeof(res->dev_lun.scsi_lun));
  1087. if (memcmp(res->res_path, &cfgtew->u.cfgte64->res_path,
  1088. sizeof(res->res_path))) {
  1089. memcpy(res->res_path, &cfgtew->u.cfgte64->res_path,
  1090. sizeof(res->res_path));
  1091. new_path = 1;
  1092. }
  1093. if (res->sdev && new_path)
  1094. sdev_printk(KERN_INFO, res->sdev, "Resource path: %s\n",
  1095. ipr_format_resource_path(&res->res_path[0], &buffer[0]));
  1096. } else {
  1097. res->flags = cfgtew->u.cfgte->flags;
  1098. if (res->flags & IPR_IS_IOA_RESOURCE)
  1099. res->type = IPR_RES_TYPE_IOAFP;
  1100. else
  1101. res->type = cfgtew->u.cfgte->rsvd_subtype & 0x0f;
  1102. memcpy(&res->std_inq_data, &cfgtew->u.cfgte->std_inq_data,
  1103. sizeof(struct ipr_std_inq_data));
  1104. res->qmodel = IPR_QUEUEING_MODEL(res);
  1105. proto = cfgtew->u.cfgte->proto;
  1106. res->res_handle = cfgtew->u.cfgte->res_handle;
  1107. }
  1108. ipr_update_ata_class(res, proto);
  1109. }
  1110. /**
  1111. * ipr_clear_res_target - Clear the bit in the bit map representing the target
  1112. * for the resource.
  1113. * @res: resource entry struct
  1114. * @cfgtew: config table entry wrapper struct
  1115. *
  1116. * Return value:
  1117. * none
  1118. **/
  1119. static void ipr_clear_res_target(struct ipr_resource_entry *res)
  1120. {
  1121. struct ipr_resource_entry *gscsi_res = NULL;
  1122. struct ipr_ioa_cfg *ioa_cfg = res->ioa_cfg;
  1123. if (!ioa_cfg->sis64)
  1124. return;
  1125. if (res->bus == IPR_ARRAY_VIRTUAL_BUS)
  1126. clear_bit(res->target, ioa_cfg->array_ids);
  1127. else if (res->bus == IPR_VSET_VIRTUAL_BUS)
  1128. clear_bit(res->target, ioa_cfg->vset_ids);
  1129. else if (res->bus == 0 && res->type == IPR_RES_TYPE_GENERIC_SCSI) {
  1130. list_for_each_entry(gscsi_res, &ioa_cfg->used_res_q, queue)
  1131. if (gscsi_res->dev_id == res->dev_id && gscsi_res != res)
  1132. return;
  1133. clear_bit(res->target, ioa_cfg->target_ids);
  1134. } else if (res->bus == 0)
  1135. clear_bit(res->target, ioa_cfg->target_ids);
  1136. }
  1137. /**
  1138. * ipr_handle_config_change - Handle a config change from the adapter
  1139. * @ioa_cfg: ioa config struct
  1140. * @hostrcb: hostrcb
  1141. *
  1142. * Return value:
  1143. * none
  1144. **/
  1145. static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
  1146. struct ipr_hostrcb *hostrcb)
  1147. {
  1148. struct ipr_resource_entry *res = NULL;
  1149. struct ipr_config_table_entry_wrapper cfgtew;
  1150. __be32 cc_res_handle;
  1151. u32 is_ndn = 1;
  1152. if (ioa_cfg->sis64) {
  1153. cfgtew.u.cfgte64 = &hostrcb->hcam.u.ccn.u.cfgte64;
  1154. cc_res_handle = cfgtew.u.cfgte64->res_handle;
  1155. } else {
  1156. cfgtew.u.cfgte = &hostrcb->hcam.u.ccn.u.cfgte;
  1157. cc_res_handle = cfgtew.u.cfgte->res_handle;
  1158. }
  1159. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  1160. if (res->res_handle == cc_res_handle) {
  1161. is_ndn = 0;
  1162. break;
  1163. }
  1164. }
  1165. if (is_ndn) {
  1166. if (list_empty(&ioa_cfg->free_res_q)) {
  1167. ipr_send_hcam(ioa_cfg,
  1168. IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
  1169. hostrcb);
  1170. return;
  1171. }
  1172. res = list_entry(ioa_cfg->free_res_q.next,
  1173. struct ipr_resource_entry, queue);
  1174. list_del(&res->queue);
  1175. ipr_init_res_entry(res, &cfgtew);
  1176. list_add_tail(&res->queue, &ioa_cfg->used_res_q);
  1177. }
  1178. ipr_update_res_entry(res, &cfgtew);
  1179. if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
  1180. if (res->sdev) {
  1181. res->del_from_ml = 1;
  1182. res->res_handle = IPR_INVALID_RES_HANDLE;
  1183. if (ioa_cfg->allow_ml_add_del)
  1184. schedule_work(&ioa_cfg->work_q);
  1185. } else {
  1186. ipr_clear_res_target(res);
  1187. list_move_tail(&res->queue, &ioa_cfg->free_res_q);
  1188. }
  1189. } else if (!res->sdev) {
  1190. res->add_to_ml = 1;
  1191. if (ioa_cfg->allow_ml_add_del)
  1192. schedule_work(&ioa_cfg->work_q);
  1193. }
  1194. ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
  1195. }
  1196. /**
  1197. * ipr_process_ccn - Op done function for a CCN.
  1198. * @ipr_cmd: ipr command struct
  1199. *
  1200. * This function is the op done function for a configuration
  1201. * change notification host controlled async from the adapter.
  1202. *
  1203. * Return value:
  1204. * none
  1205. **/
  1206. static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
  1207. {
  1208. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  1209. struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
  1210. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  1211. list_del(&hostrcb->queue);
  1212. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  1213. if (ioasc) {
  1214. if (ioasc != IPR_IOASC_IOA_WAS_RESET)
  1215. dev_err(&ioa_cfg->pdev->dev,
  1216. "Host RCB failed with IOASC: 0x%08X\n", ioasc);
  1217. ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
  1218. } else {
  1219. ipr_handle_config_change(ioa_cfg, hostrcb);
  1220. }
  1221. }
  1222. /**
  1223. * strip_and_pad_whitespace - Strip and pad trailing whitespace.
  1224. * @i: index into buffer
  1225. * @buf: string to modify
  1226. *
  1227. * This function will strip all trailing whitespace, pad the end
  1228. * of the string with a single space, and NULL terminate the string.
  1229. *
  1230. * Return value:
  1231. * new length of string
  1232. **/
  1233. static int strip_and_pad_whitespace(int i, char *buf)
  1234. {
  1235. while (i && buf[i] == ' ')
  1236. i--;
  1237. buf[i+1] = ' ';
  1238. buf[i+2] = '\0';
  1239. return i + 2;
  1240. }
  1241. /**
  1242. * ipr_log_vpd_compact - Log the passed extended VPD compactly.
  1243. * @prefix: string to print at start of printk
  1244. * @hostrcb: hostrcb pointer
  1245. * @vpd: vendor/product id/sn struct
  1246. *
  1247. * Return value:
  1248. * none
  1249. **/
  1250. static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
  1251. struct ipr_vpd *vpd)
  1252. {
  1253. char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
  1254. int i = 0;
  1255. memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
  1256. i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
  1257. memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
  1258. i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
  1259. memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
  1260. buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
  1261. ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
  1262. }
  1263. /**
  1264. * ipr_log_vpd - Log the passed VPD to the error log.
  1265. * @vpd: vendor/product id/sn struct
  1266. *
  1267. * Return value:
  1268. * none
  1269. **/
  1270. static void ipr_log_vpd(struct ipr_vpd *vpd)
  1271. {
  1272. char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
  1273. + IPR_SERIAL_NUM_LEN];
  1274. memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
  1275. memcpy(buffer + IPR_VENDOR_ID_LEN, vpd->vpids.product_id,
  1276. IPR_PROD_ID_LEN);
  1277. buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
  1278. ipr_err("Vendor/Product ID: %s\n", buffer);
  1279. memcpy(buffer, vpd->sn, IPR_SERIAL_NUM_LEN);
  1280. buffer[IPR_SERIAL_NUM_LEN] = '\0';
  1281. ipr_err(" Serial Number: %s\n", buffer);
  1282. }
  1283. /**
  1284. * ipr_log_ext_vpd_compact - Log the passed extended VPD compactly.
  1285. * @prefix: string to print at start of printk
  1286. * @hostrcb: hostrcb pointer
  1287. * @vpd: vendor/product id/sn/wwn struct
  1288. *
  1289. * Return value:
  1290. * none
  1291. **/
  1292. static void ipr_log_ext_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
  1293. struct ipr_ext_vpd *vpd)
  1294. {
  1295. ipr_log_vpd_compact(prefix, hostrcb, &vpd->vpd);
  1296. ipr_hcam_err(hostrcb, "%s WWN: %08X%08X\n", prefix,
  1297. be32_to_cpu(vpd->wwid[0]), be32_to_cpu(vpd->wwid[1]));
  1298. }
  1299. /**
  1300. * ipr_log_ext_vpd - Log the passed extended VPD to the error log.
  1301. * @vpd: vendor/product id/sn/wwn struct
  1302. *
  1303. * Return value:
  1304. * none
  1305. **/
  1306. static void ipr_log_ext_vpd(struct ipr_ext_vpd *vpd)
  1307. {
  1308. ipr_log_vpd(&vpd->vpd);
  1309. ipr_err(" WWN: %08X%08X\n", be32_to_cpu(vpd->wwid[0]),
  1310. be32_to_cpu(vpd->wwid[1]));
  1311. }
  1312. /**
  1313. * ipr_log_enhanced_cache_error - Log a cache error.
  1314. * @ioa_cfg: ioa config struct
  1315. * @hostrcb: hostrcb struct
  1316. *
  1317. * Return value:
  1318. * none
  1319. **/
  1320. static void ipr_log_enhanced_cache_error(struct ipr_ioa_cfg *ioa_cfg,
  1321. struct ipr_hostrcb *hostrcb)
  1322. {
  1323. struct ipr_hostrcb_type_12_error *error;
  1324. if (ioa_cfg->sis64)
  1325. error = &hostrcb->hcam.u.error64.u.type_12_error;
  1326. else
  1327. error = &hostrcb->hcam.u.error.u.type_12_error;
  1328. ipr_err("-----Current Configuration-----\n");
  1329. ipr_err("Cache Directory Card Information:\n");
  1330. ipr_log_ext_vpd(&error->ioa_vpd);
  1331. ipr_err("Adapter Card Information:\n");
  1332. ipr_log_ext_vpd(&error->cfc_vpd);
  1333. ipr_err("-----Expected Configuration-----\n");
  1334. ipr_err("Cache Directory Card Information:\n");
  1335. ipr_log_ext_vpd(&error->ioa_last_attached_to_cfc_vpd);
  1336. ipr_err("Adapter Card Information:\n");
  1337. ipr_log_ext_vpd(&error->cfc_last_attached_to_ioa_vpd);
  1338. ipr_err("Additional IOA Data: %08X %08X %08X\n",
  1339. be32_to_cpu(error->ioa_data[0]),
  1340. be32_to_cpu(error->ioa_data[1]),
  1341. be32_to_cpu(error->ioa_data[2]));
  1342. }
  1343. /**
  1344. * ipr_log_cache_error - Log a cache error.
  1345. * @ioa_cfg: ioa config struct
  1346. * @hostrcb: hostrcb struct
  1347. *
  1348. * Return value:
  1349. * none
  1350. **/
  1351. static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
  1352. struct ipr_hostrcb *hostrcb)
  1353. {
  1354. struct ipr_hostrcb_type_02_error *error =
  1355. &hostrcb->hcam.u.error.u.type_02_error;
  1356. ipr_err("-----Current Configuration-----\n");
  1357. ipr_err("Cache Directory Card Information:\n");
  1358. ipr_log_vpd(&error->ioa_vpd);
  1359. ipr_err("Adapter Card Information:\n");
  1360. ipr_log_vpd(&error->cfc_vpd);
  1361. ipr_err("-----Expected Configuration-----\n");
  1362. ipr_err("Cache Directory Card Information:\n");
  1363. ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpd);
  1364. ipr_err("Adapter Card Information:\n");
  1365. ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpd);
  1366. ipr_err("Additional IOA Data: %08X %08X %08X\n",
  1367. be32_to_cpu(error->ioa_data[0]),
  1368. be32_to_cpu(error->ioa_data[1]),
  1369. be32_to_cpu(error->ioa_data[2]));
  1370. }
  1371. /**
  1372. * ipr_log_enhanced_config_error - Log a configuration error.
  1373. * @ioa_cfg: ioa config struct
  1374. * @hostrcb: hostrcb struct
  1375. *
  1376. * Return value:
  1377. * none
  1378. **/
  1379. static void ipr_log_enhanced_config_error(struct ipr_ioa_cfg *ioa_cfg,
  1380. struct ipr_hostrcb *hostrcb)
  1381. {
  1382. int errors_logged, i;
  1383. struct ipr_hostrcb_device_data_entry_enhanced *dev_entry;
  1384. struct ipr_hostrcb_type_13_error *error;
  1385. error = &hostrcb->hcam.u.error.u.type_13_error;
  1386. errors_logged = be32_to_cpu(error->errors_logged);
  1387. ipr_err("Device Errors Detected/Logged: %d/%d\n",
  1388. be32_to_cpu(error->errors_detected), errors_logged);
  1389. dev_entry = error->dev;
  1390. for (i = 0; i < errors_logged; i++, dev_entry++) {
  1391. ipr_err_separator;
  1392. ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
  1393. ipr_log_ext_vpd(&dev_entry->vpd);
  1394. ipr_err("-----New Device Information-----\n");
  1395. ipr_log_ext_vpd(&dev_entry->new_vpd);
  1396. ipr_err("Cache Directory Card Information:\n");
  1397. ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
  1398. ipr_err("Adapter Card Information:\n");
  1399. ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
  1400. }
  1401. }
  1402. /**
  1403. * ipr_log_sis64_config_error - Log a device error.
  1404. * @ioa_cfg: ioa config struct
  1405. * @hostrcb: hostrcb struct
  1406. *
  1407. * Return value:
  1408. * none
  1409. **/
  1410. static void ipr_log_sis64_config_error(struct ipr_ioa_cfg *ioa_cfg,
  1411. struct ipr_hostrcb *hostrcb)
  1412. {
  1413. int errors_logged, i;
  1414. struct ipr_hostrcb64_device_data_entry_enhanced *dev_entry;
  1415. struct ipr_hostrcb_type_23_error *error;
  1416. char buffer[IPR_MAX_RES_PATH_LENGTH];
  1417. error = &hostrcb->hcam.u.error64.u.type_23_error;
  1418. errors_logged = be32_to_cpu(error->errors_logged);
  1419. ipr_err("Device Errors Detected/Logged: %d/%d\n",
  1420. be32_to_cpu(error->errors_detected), errors_logged);
  1421. dev_entry = error->dev;
  1422. for (i = 0; i < errors_logged; i++, dev_entry++) {
  1423. ipr_err_separator;
  1424. ipr_err("Device %d : %s", i + 1,
  1425. ipr_format_resource_path(&dev_entry->res_path[0], &buffer[0]));
  1426. ipr_log_ext_vpd(&dev_entry->vpd);
  1427. ipr_err("-----New Device Information-----\n");
  1428. ipr_log_ext_vpd(&dev_entry->new_vpd);
  1429. ipr_err("Cache Directory Card Information:\n");
  1430. ipr_log_ext_vpd(&dev_entry->ioa_last_with_dev_vpd);
  1431. ipr_err("Adapter Card Information:\n");
  1432. ipr_log_ext_vpd(&dev_entry->cfc_last_with_dev_vpd);
  1433. }
  1434. }
  1435. /**
  1436. * ipr_log_config_error - Log a configuration error.
  1437. * @ioa_cfg: ioa config struct
  1438. * @hostrcb: hostrcb struct
  1439. *
  1440. * Return value:
  1441. * none
  1442. **/
  1443. static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
  1444. struct ipr_hostrcb *hostrcb)
  1445. {
  1446. int errors_logged, i;
  1447. struct ipr_hostrcb_device_data_entry *dev_entry;
  1448. struct ipr_hostrcb_type_03_error *error;
  1449. error = &hostrcb->hcam.u.error.u.type_03_error;
  1450. errors_logged = be32_to_cpu(error->errors_logged);
  1451. ipr_err("Device Errors Detected/Logged: %d/%d\n",
  1452. be32_to_cpu(error->errors_detected), errors_logged);
  1453. dev_entry = error->dev;
  1454. for (i = 0; i < errors_logged; i++, dev_entry++) {
  1455. ipr_err_separator;
  1456. ipr_phys_res_err(ioa_cfg, dev_entry->dev_res_addr, "Device %d", i + 1);
  1457. ipr_log_vpd(&dev_entry->vpd);
  1458. ipr_err("-----New Device Information-----\n");
  1459. ipr_log_vpd(&dev_entry->new_vpd);
  1460. ipr_err("Cache Directory Card Information:\n");
  1461. ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpd);
  1462. ipr_err("Adapter Card Information:\n");
  1463. ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpd);
  1464. ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
  1465. be32_to_cpu(dev_entry->ioa_data[0]),
  1466. be32_to_cpu(dev_entry->ioa_data[1]),
  1467. be32_to_cpu(dev_entry->ioa_data[2]),
  1468. be32_to_cpu(dev_entry->ioa_data[3]),
  1469. be32_to_cpu(dev_entry->ioa_data[4]));
  1470. }
  1471. }
  1472. /**
  1473. * ipr_log_enhanced_array_error - Log an array configuration error.
  1474. * @ioa_cfg: ioa config struct
  1475. * @hostrcb: hostrcb struct
  1476. *
  1477. * Return value:
  1478. * none
  1479. **/
  1480. static void ipr_log_enhanced_array_error(struct ipr_ioa_cfg *ioa_cfg,
  1481. struct ipr_hostrcb *hostrcb)
  1482. {
  1483. int i, num_entries;
  1484. struct ipr_hostrcb_type_14_error *error;
  1485. struct ipr_hostrcb_array_data_entry_enhanced *array_entry;
  1486. const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
  1487. error = &hostrcb->hcam.u.error.u.type_14_error;
  1488. ipr_err_separator;
  1489. ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
  1490. error->protection_level,
  1491. ioa_cfg->host->host_no,
  1492. error->last_func_vset_res_addr.bus,
  1493. error->last_func_vset_res_addr.target,
  1494. error->last_func_vset_res_addr.lun);
  1495. ipr_err_separator;
  1496. array_entry = error->array_member;
  1497. num_entries = min_t(u32, be32_to_cpu(error->num_entries),
  1498. sizeof(error->array_member));
  1499. for (i = 0; i < num_entries; i++, array_entry++) {
  1500. if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
  1501. continue;
  1502. if (be32_to_cpu(error->exposed_mode_adn) == i)
  1503. ipr_err("Exposed Array Member %d:\n", i);
  1504. else
  1505. ipr_err("Array Member %d:\n", i);
  1506. ipr_log_ext_vpd(&array_entry->vpd);
  1507. ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
  1508. ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
  1509. "Expected Location");
  1510. ipr_err_separator;
  1511. }
  1512. }
  1513. /**
  1514. * ipr_log_array_error - Log an array configuration error.
  1515. * @ioa_cfg: ioa config struct
  1516. * @hostrcb: hostrcb struct
  1517. *
  1518. * Return value:
  1519. * none
  1520. **/
  1521. static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
  1522. struct ipr_hostrcb *hostrcb)
  1523. {
  1524. int i;
  1525. struct ipr_hostrcb_type_04_error *error;
  1526. struct ipr_hostrcb_array_data_entry *array_entry;
  1527. const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
  1528. error = &hostrcb->hcam.u.error.u.type_04_error;
  1529. ipr_err_separator;
  1530. ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
  1531. error->protection_level,
  1532. ioa_cfg->host->host_no,
  1533. error->last_func_vset_res_addr.bus,
  1534. error->last_func_vset_res_addr.target,
  1535. error->last_func_vset_res_addr.lun);
  1536. ipr_err_separator;
  1537. array_entry = error->array_member;
  1538. for (i = 0; i < 18; i++) {
  1539. if (!memcmp(array_entry->vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
  1540. continue;
  1541. if (be32_to_cpu(error->exposed_mode_adn) == i)
  1542. ipr_err("Exposed Array Member %d:\n", i);
  1543. else
  1544. ipr_err("Array Member %d:\n", i);
  1545. ipr_log_vpd(&array_entry->vpd);
  1546. ipr_phys_res_err(ioa_cfg, array_entry->dev_res_addr, "Current Location");
  1547. ipr_phys_res_err(ioa_cfg, array_entry->expected_dev_res_addr,
  1548. "Expected Location");
  1549. ipr_err_separator;
  1550. if (i == 9)
  1551. array_entry = error->array_member2;
  1552. else
  1553. array_entry++;
  1554. }
  1555. }
  1556. /**
  1557. * ipr_log_hex_data - Log additional hex IOA error data.
  1558. * @ioa_cfg: ioa config struct
  1559. * @data: IOA error data
  1560. * @len: data length
  1561. *
  1562. * Return value:
  1563. * none
  1564. **/
  1565. static void ipr_log_hex_data(struct ipr_ioa_cfg *ioa_cfg, u32 *data, int len)
  1566. {
  1567. int i;
  1568. if (len == 0)
  1569. return;
  1570. if (ioa_cfg->log_level <= IPR_DEFAULT_LOG_LEVEL)
  1571. len = min_t(int, len, IPR_DEFAULT_MAX_ERROR_DUMP);
  1572. for (i = 0; i < len / 4; i += 4) {
  1573. ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
  1574. be32_to_cpu(data[i]),
  1575. be32_to_cpu(data[i+1]),
  1576. be32_to_cpu(data[i+2]),
  1577. be32_to_cpu(data[i+3]));
  1578. }
  1579. }
  1580. /**
  1581. * ipr_log_enhanced_dual_ioa_error - Log an enhanced dual adapter error.
  1582. * @ioa_cfg: ioa config struct
  1583. * @hostrcb: hostrcb struct
  1584. *
  1585. * Return value:
  1586. * none
  1587. **/
  1588. static void ipr_log_enhanced_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
  1589. struct ipr_hostrcb *hostrcb)
  1590. {
  1591. struct ipr_hostrcb_type_17_error *error;
  1592. if (ioa_cfg->sis64)
  1593. error = &hostrcb->hcam.u.error64.u.type_17_error;
  1594. else
  1595. error = &hostrcb->hcam.u.error.u.type_17_error;
  1596. error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
  1597. strim(error->failure_reason);
  1598. ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
  1599. be32_to_cpu(hostrcb->hcam.u.error.prc));
  1600. ipr_log_ext_vpd_compact("Remote IOA", hostrcb, &error->vpd);
  1601. ipr_log_hex_data(ioa_cfg, error->data,
  1602. be32_to_cpu(hostrcb->hcam.length) -
  1603. (offsetof(struct ipr_hostrcb_error, u) +
  1604. offsetof(struct ipr_hostrcb_type_17_error, data)));
  1605. }
  1606. /**
  1607. * ipr_log_dual_ioa_error - Log a dual adapter error.
  1608. * @ioa_cfg: ioa config struct
  1609. * @hostrcb: hostrcb struct
  1610. *
  1611. * Return value:
  1612. * none
  1613. **/
  1614. static void ipr_log_dual_ioa_error(struct ipr_ioa_cfg *ioa_cfg,
  1615. struct ipr_hostrcb *hostrcb)
  1616. {
  1617. struct ipr_hostrcb_type_07_error *error;
  1618. error = &hostrcb->hcam.u.error.u.type_07_error;
  1619. error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
  1620. strim(error->failure_reason);
  1621. ipr_hcam_err(hostrcb, "%s [PRC: %08X]\n", error->failure_reason,
  1622. be32_to_cpu(hostrcb->hcam.u.error.prc));
  1623. ipr_log_vpd_compact("Remote IOA", hostrcb, &error->vpd);
  1624. ipr_log_hex_data(ioa_cfg, error->data,
  1625. be32_to_cpu(hostrcb->hcam.length) -
  1626. (offsetof(struct ipr_hostrcb_error, u) +
  1627. offsetof(struct ipr_hostrcb_type_07_error, data)));
  1628. }
  1629. static const struct {
  1630. u8 active;
  1631. char *desc;
  1632. } path_active_desc[] = {
  1633. { IPR_PATH_NO_INFO, "Path" },
  1634. { IPR_PATH_ACTIVE, "Active path" },
  1635. { IPR_PATH_NOT_ACTIVE, "Inactive path" }
  1636. };
  1637. static const struct {
  1638. u8 state;
  1639. char *desc;
  1640. } path_state_desc[] = {
  1641. { IPR_PATH_STATE_NO_INFO, "has no path state information available" },
  1642. { IPR_PATH_HEALTHY, "is healthy" },
  1643. { IPR_PATH_DEGRADED, "is degraded" },
  1644. { IPR_PATH_FAILED, "is failed" }
  1645. };
  1646. /**
  1647. * ipr_log_fabric_path - Log a fabric path error
  1648. * @hostrcb: hostrcb struct
  1649. * @fabric: fabric descriptor
  1650. *
  1651. * Return value:
  1652. * none
  1653. **/
  1654. static void ipr_log_fabric_path(struct ipr_hostrcb *hostrcb,
  1655. struct ipr_hostrcb_fabric_desc *fabric)
  1656. {
  1657. int i, j;
  1658. u8 path_state = fabric->path_state;
  1659. u8 active = path_state & IPR_PATH_ACTIVE_MASK;
  1660. u8 state = path_state & IPR_PATH_STATE_MASK;
  1661. for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
  1662. if (path_active_desc[i].active != active)
  1663. continue;
  1664. for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
  1665. if (path_state_desc[j].state != state)
  1666. continue;
  1667. if (fabric->cascaded_expander == 0xff && fabric->phy == 0xff) {
  1668. ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d\n",
  1669. path_active_desc[i].desc, path_state_desc[j].desc,
  1670. fabric->ioa_port);
  1671. } else if (fabric->cascaded_expander == 0xff) {
  1672. ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Phy=%d\n",
  1673. path_active_desc[i].desc, path_state_desc[j].desc,
  1674. fabric->ioa_port, fabric->phy);
  1675. } else if (fabric->phy == 0xff) {
  1676. ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d\n",
  1677. path_active_desc[i].desc, path_state_desc[j].desc,
  1678. fabric->ioa_port, fabric->cascaded_expander);
  1679. } else {
  1680. ipr_hcam_err(hostrcb, "%s %s: IOA Port=%d, Cascade=%d, Phy=%d\n",
  1681. path_active_desc[i].desc, path_state_desc[j].desc,
  1682. fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
  1683. }
  1684. return;
  1685. }
  1686. }
  1687. ipr_err("Path state=%02X IOA Port=%d Cascade=%d Phy=%d\n", path_state,
  1688. fabric->ioa_port, fabric->cascaded_expander, fabric->phy);
  1689. }
  1690. /**
  1691. * ipr_log64_fabric_path - Log a fabric path error
  1692. * @hostrcb: hostrcb struct
  1693. * @fabric: fabric descriptor
  1694. *
  1695. * Return value:
  1696. * none
  1697. **/
  1698. static void ipr_log64_fabric_path(struct ipr_hostrcb *hostrcb,
  1699. struct ipr_hostrcb64_fabric_desc *fabric)
  1700. {
  1701. int i, j;
  1702. u8 path_state = fabric->path_state;
  1703. u8 active = path_state & IPR_PATH_ACTIVE_MASK;
  1704. u8 state = path_state & IPR_PATH_STATE_MASK;
  1705. char buffer[IPR_MAX_RES_PATH_LENGTH];
  1706. for (i = 0; i < ARRAY_SIZE(path_active_desc); i++) {
  1707. if (path_active_desc[i].active != active)
  1708. continue;
  1709. for (j = 0; j < ARRAY_SIZE(path_state_desc); j++) {
  1710. if (path_state_desc[j].state != state)
  1711. continue;
  1712. ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s\n",
  1713. path_active_desc[i].desc, path_state_desc[j].desc,
  1714. ipr_format_resource_path(&fabric->res_path[0], &buffer[0]));
  1715. return;
  1716. }
  1717. }
  1718. ipr_err("Path state=%02X Resource Path=%s\n", path_state,
  1719. ipr_format_resource_path(&fabric->res_path[0], &buffer[0]));
  1720. }
  1721. static const struct {
  1722. u8 type;
  1723. char *desc;
  1724. } path_type_desc[] = {
  1725. { IPR_PATH_CFG_IOA_PORT, "IOA port" },
  1726. { IPR_PATH_CFG_EXP_PORT, "Expander port" },
  1727. { IPR_PATH_CFG_DEVICE_PORT, "Device port" },
  1728. { IPR_PATH_CFG_DEVICE_LUN, "Device LUN" }
  1729. };
  1730. static const struct {
  1731. u8 status;
  1732. char *desc;
  1733. } path_status_desc[] = {
  1734. { IPR_PATH_CFG_NO_PROB, "Functional" },
  1735. { IPR_PATH_CFG_DEGRADED, "Degraded" },
  1736. { IPR_PATH_CFG_FAILED, "Failed" },
  1737. { IPR_PATH_CFG_SUSPECT, "Suspect" },
  1738. { IPR_PATH_NOT_DETECTED, "Missing" },
  1739. { IPR_PATH_INCORRECT_CONN, "Incorrectly connected" }
  1740. };
  1741. static const char *link_rate[] = {
  1742. "unknown",
  1743. "disabled",
  1744. "phy reset problem",
  1745. "spinup hold",
  1746. "port selector",
  1747. "unknown",
  1748. "unknown",
  1749. "unknown",
  1750. "1.5Gbps",
  1751. "3.0Gbps",
  1752. "unknown",
  1753. "unknown",
  1754. "unknown",
  1755. "unknown",
  1756. "unknown",
  1757. "unknown"
  1758. };
  1759. /**
  1760. * ipr_log_path_elem - Log a fabric path element.
  1761. * @hostrcb: hostrcb struct
  1762. * @cfg: fabric path element struct
  1763. *
  1764. * Return value:
  1765. * none
  1766. **/
  1767. static void ipr_log_path_elem(struct ipr_hostrcb *hostrcb,
  1768. struct ipr_hostrcb_config_element *cfg)
  1769. {
  1770. int i, j;
  1771. u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
  1772. u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
  1773. if (type == IPR_PATH_CFG_NOT_EXIST)
  1774. return;
  1775. for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
  1776. if (path_type_desc[i].type != type)
  1777. continue;
  1778. for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
  1779. if (path_status_desc[j].status != status)
  1780. continue;
  1781. if (type == IPR_PATH_CFG_IOA_PORT) {
  1782. ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, WWN=%08X%08X\n",
  1783. path_status_desc[j].desc, path_type_desc[i].desc,
  1784. cfg->phy, link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1785. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1786. } else {
  1787. if (cfg->cascaded_expander == 0xff && cfg->phy == 0xff) {
  1788. ipr_hcam_err(hostrcb, "%s %s: Link rate=%s, WWN=%08X%08X\n",
  1789. path_status_desc[j].desc, path_type_desc[i].desc,
  1790. link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1791. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1792. } else if (cfg->cascaded_expander == 0xff) {
  1793. ipr_hcam_err(hostrcb, "%s %s: Phy=%d, Link rate=%s, "
  1794. "WWN=%08X%08X\n", path_status_desc[j].desc,
  1795. path_type_desc[i].desc, cfg->phy,
  1796. link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1797. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1798. } else if (cfg->phy == 0xff) {
  1799. ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Link rate=%s, "
  1800. "WWN=%08X%08X\n", path_status_desc[j].desc,
  1801. path_type_desc[i].desc, cfg->cascaded_expander,
  1802. link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1803. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1804. } else {
  1805. ipr_hcam_err(hostrcb, "%s %s: Cascade=%d, Phy=%d, Link rate=%s "
  1806. "WWN=%08X%08X\n", path_status_desc[j].desc,
  1807. path_type_desc[i].desc, cfg->cascaded_expander, cfg->phy,
  1808. link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1809. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1810. }
  1811. }
  1812. return;
  1813. }
  1814. }
  1815. ipr_hcam_err(hostrcb, "Path element=%02X: Cascade=%d Phy=%d Link rate=%s "
  1816. "WWN=%08X%08X\n", cfg->type_status, cfg->cascaded_expander, cfg->phy,
  1817. link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1818. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1819. }
  1820. /**
  1821. * ipr_log64_path_elem - Log a fabric path element.
  1822. * @hostrcb: hostrcb struct
  1823. * @cfg: fabric path element struct
  1824. *
  1825. * Return value:
  1826. * none
  1827. **/
  1828. static void ipr_log64_path_elem(struct ipr_hostrcb *hostrcb,
  1829. struct ipr_hostrcb64_config_element *cfg)
  1830. {
  1831. int i, j;
  1832. u8 desc_id = cfg->descriptor_id & IPR_DESCRIPTOR_MASK;
  1833. u8 type = cfg->type_status & IPR_PATH_CFG_TYPE_MASK;
  1834. u8 status = cfg->type_status & IPR_PATH_CFG_STATUS_MASK;
  1835. char buffer[IPR_MAX_RES_PATH_LENGTH];
  1836. if (type == IPR_PATH_CFG_NOT_EXIST || desc_id != IPR_DESCRIPTOR_SIS64)
  1837. return;
  1838. for (i = 0; i < ARRAY_SIZE(path_type_desc); i++) {
  1839. if (path_type_desc[i].type != type)
  1840. continue;
  1841. for (j = 0; j < ARRAY_SIZE(path_status_desc); j++) {
  1842. if (path_status_desc[j].status != status)
  1843. continue;
  1844. ipr_hcam_err(hostrcb, "%s %s: Resource Path=%s, Link rate=%s, WWN=%08X%08X\n",
  1845. path_status_desc[j].desc, path_type_desc[i].desc,
  1846. ipr_format_resource_path(&cfg->res_path[0], &buffer[0]),
  1847. link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1848. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1849. return;
  1850. }
  1851. }
  1852. ipr_hcam_err(hostrcb, "Path element=%02X: Resource Path=%s, Link rate=%s "
  1853. "WWN=%08X%08X\n", cfg->type_status,
  1854. ipr_format_resource_path(&cfg->res_path[0], &buffer[0]),
  1855. link_rate[cfg->link_rate & IPR_PHY_LINK_RATE_MASK],
  1856. be32_to_cpu(cfg->wwid[0]), be32_to_cpu(cfg->wwid[1]));
  1857. }
  1858. /**
  1859. * ipr_log_fabric_error - Log a fabric error.
  1860. * @ioa_cfg: ioa config struct
  1861. * @hostrcb: hostrcb struct
  1862. *
  1863. * Return value:
  1864. * none
  1865. **/
  1866. static void ipr_log_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
  1867. struct ipr_hostrcb *hostrcb)
  1868. {
  1869. struct ipr_hostrcb_type_20_error *error;
  1870. struct ipr_hostrcb_fabric_desc *fabric;
  1871. struct ipr_hostrcb_config_element *cfg;
  1872. int i, add_len;
  1873. error = &hostrcb->hcam.u.error.u.type_20_error;
  1874. error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
  1875. ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
  1876. add_len = be32_to_cpu(hostrcb->hcam.length) -
  1877. (offsetof(struct ipr_hostrcb_error, u) +
  1878. offsetof(struct ipr_hostrcb_type_20_error, desc));
  1879. for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
  1880. ipr_log_fabric_path(hostrcb, fabric);
  1881. for_each_fabric_cfg(fabric, cfg)
  1882. ipr_log_path_elem(hostrcb, cfg);
  1883. add_len -= be16_to_cpu(fabric->length);
  1884. fabric = (struct ipr_hostrcb_fabric_desc *)
  1885. ((unsigned long)fabric + be16_to_cpu(fabric->length));
  1886. }
  1887. ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
  1888. }
  1889. /**
  1890. * ipr_log_sis64_array_error - Log a sis64 array error.
  1891. * @ioa_cfg: ioa config struct
  1892. * @hostrcb: hostrcb struct
  1893. *
  1894. * Return value:
  1895. * none
  1896. **/
  1897. static void ipr_log_sis64_array_error(struct ipr_ioa_cfg *ioa_cfg,
  1898. struct ipr_hostrcb *hostrcb)
  1899. {
  1900. int i, num_entries;
  1901. struct ipr_hostrcb_type_24_error *error;
  1902. struct ipr_hostrcb64_array_data_entry *array_entry;
  1903. char buffer[IPR_MAX_RES_PATH_LENGTH];
  1904. const u8 zero_sn[IPR_SERIAL_NUM_LEN] = { [0 ... IPR_SERIAL_NUM_LEN-1] = '0' };
  1905. error = &hostrcb->hcam.u.error64.u.type_24_error;
  1906. ipr_err_separator;
  1907. ipr_err("RAID %s Array Configuration: %s\n",
  1908. error->protection_level,
  1909. ipr_format_resource_path(&error->last_res_path[0], &buffer[0]));
  1910. ipr_err_separator;
  1911. array_entry = error->array_member;
  1912. num_entries = min_t(u32, be32_to_cpu(error->num_entries),
  1913. sizeof(error->array_member));
  1914. for (i = 0; i < num_entries; i++, array_entry++) {
  1915. if (!memcmp(array_entry->vpd.vpd.sn, zero_sn, IPR_SERIAL_NUM_LEN))
  1916. continue;
  1917. if (error->exposed_mode_adn == i)
  1918. ipr_err("Exposed Array Member %d:\n", i);
  1919. else
  1920. ipr_err("Array Member %d:\n", i);
  1921. ipr_err("Array Member %d:\n", i);
  1922. ipr_log_ext_vpd(&array_entry->vpd);
  1923. ipr_err("Current Location: %s",
  1924. ipr_format_resource_path(&array_entry->res_path[0], &buffer[0]));
  1925. ipr_err("Expected Location: %s",
  1926. ipr_format_resource_path(&array_entry->expected_res_path[0], &buffer[0]));
  1927. ipr_err_separator;
  1928. }
  1929. }
  1930. /**
  1931. * ipr_log_sis64_fabric_error - Log a sis64 fabric error.
  1932. * @ioa_cfg: ioa config struct
  1933. * @hostrcb: hostrcb struct
  1934. *
  1935. * Return value:
  1936. * none
  1937. **/
  1938. static void ipr_log_sis64_fabric_error(struct ipr_ioa_cfg *ioa_cfg,
  1939. struct ipr_hostrcb *hostrcb)
  1940. {
  1941. struct ipr_hostrcb_type_30_error *error;
  1942. struct ipr_hostrcb64_fabric_desc *fabric;
  1943. struct ipr_hostrcb64_config_element *cfg;
  1944. int i, add_len;
  1945. error = &hostrcb->hcam.u.error64.u.type_30_error;
  1946. error->failure_reason[sizeof(error->failure_reason) - 1] = '\0';
  1947. ipr_hcam_err(hostrcb, "%s\n", error->failure_reason);
  1948. add_len = be32_to_cpu(hostrcb->hcam.length) -
  1949. (offsetof(struct ipr_hostrcb64_error, u) +
  1950. offsetof(struct ipr_hostrcb_type_30_error, desc));
  1951. for (i = 0, fabric = error->desc; i < error->num_entries; i++) {
  1952. ipr_log64_fabric_path(hostrcb, fabric);
  1953. for_each_fabric_cfg(fabric, cfg)
  1954. ipr_log64_path_elem(hostrcb, cfg);
  1955. add_len -= be16_to_cpu(fabric->length);
  1956. fabric = (struct ipr_hostrcb64_fabric_desc *)
  1957. ((unsigned long)fabric + be16_to_cpu(fabric->length));
  1958. }
  1959. ipr_log_hex_data(ioa_cfg, (u32 *)fabric, add_len);
  1960. }
  1961. /**
  1962. * ipr_log_generic_error - Log an adapter error.
  1963. * @ioa_cfg: ioa config struct
  1964. * @hostrcb: hostrcb struct
  1965. *
  1966. * Return value:
  1967. * none
  1968. **/
  1969. static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
  1970. struct ipr_hostrcb *hostrcb)
  1971. {
  1972. ipr_log_hex_data(ioa_cfg, hostrcb->hcam.u.raw.data,
  1973. be32_to_cpu(hostrcb->hcam.length));
  1974. }
  1975. /**
  1976. * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
  1977. * @ioasc: IOASC
  1978. *
  1979. * This function will return the index of into the ipr_error_table
  1980. * for the specified IOASC. If the IOASC is not in the table,
  1981. * 0 will be returned, which points to the entry used for unknown errors.
  1982. *
  1983. * Return value:
  1984. * index into the ipr_error_table
  1985. **/
  1986. static u32 ipr_get_error(u32 ioasc)
  1987. {
  1988. int i;
  1989. for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
  1990. if (ipr_error_table[i].ioasc == (ioasc & IPR_IOASC_IOASC_MASK))
  1991. return i;
  1992. return 0;
  1993. }
  1994. /**
  1995. * ipr_handle_log_data - Log an adapter error.
  1996. * @ioa_cfg: ioa config struct
  1997. * @hostrcb: hostrcb struct
  1998. *
  1999. * This function logs an adapter error to the system.
  2000. *
  2001. * Return value:
  2002. * none
  2003. **/
  2004. static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
  2005. struct ipr_hostrcb *hostrcb)
  2006. {
  2007. u32 ioasc;
  2008. int error_index;
  2009. if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
  2010. return;
  2011. if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
  2012. dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
  2013. if (ioa_cfg->sis64)
  2014. ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
  2015. else
  2016. ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
  2017. if (!ioa_cfg->sis64 && (ioasc == IPR_IOASC_BUS_WAS_RESET ||
  2018. ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER)) {
  2019. /* Tell the midlayer we had a bus reset so it will handle the UA properly */
  2020. scsi_report_bus_reset(ioa_cfg->host,
  2021. hostrcb->hcam.u.error.fd_res_addr.bus);
  2022. }
  2023. error_index = ipr_get_error(ioasc);
  2024. if (!ipr_error_table[error_index].log_hcam)
  2025. return;
  2026. ipr_hcam_err(hostrcb, "%s\n", ipr_error_table[error_index].error);
  2027. /* Set indication we have logged an error */
  2028. ioa_cfg->errors_logged++;
  2029. if (ioa_cfg->log_level < ipr_error_table[error_index].log_hcam)
  2030. return;
  2031. if (be32_to_cpu(hostrcb->hcam.length) > sizeof(hostrcb->hcam.u.raw))
  2032. hostrcb->hcam.length = cpu_to_be32(sizeof(hostrcb->hcam.u.raw));
  2033. switch (hostrcb->hcam.overlay_id) {
  2034. case IPR_HOST_RCB_OVERLAY_ID_2:
  2035. ipr_log_cache_error(ioa_cfg, hostrcb);
  2036. break;
  2037. case IPR_HOST_RCB_OVERLAY_ID_3:
  2038. ipr_log_config_error(ioa_cfg, hostrcb);
  2039. break;
  2040. case IPR_HOST_RCB_OVERLAY_ID_4:
  2041. case IPR_HOST_RCB_OVERLAY_ID_6:
  2042. ipr_log_array_error(ioa_cfg, hostrcb);
  2043. break;
  2044. case IPR_HOST_RCB_OVERLAY_ID_7:
  2045. ipr_log_dual_ioa_error(ioa_cfg, hostrcb);
  2046. break;
  2047. case IPR_HOST_RCB_OVERLAY_ID_12:
  2048. ipr_log_enhanced_cache_error(ioa_cfg, hostrcb);
  2049. break;
  2050. case IPR_HOST_RCB_OVERLAY_ID_13:
  2051. ipr_log_enhanced_config_error(ioa_cfg, hostrcb);
  2052. break;
  2053. case IPR_HOST_RCB_OVERLAY_ID_14:
  2054. case IPR_HOST_RCB_OVERLAY_ID_16:
  2055. ipr_log_enhanced_array_error(ioa_cfg, hostrcb);
  2056. break;
  2057. case IPR_HOST_RCB_OVERLAY_ID_17:
  2058. ipr_log_enhanced_dual_ioa_error(ioa_cfg, hostrcb);
  2059. break;
  2060. case IPR_HOST_RCB_OVERLAY_ID_20:
  2061. ipr_log_fabric_error(ioa_cfg, hostrcb);
  2062. break;
  2063. case IPR_HOST_RCB_OVERLAY_ID_23:
  2064. ipr_log_sis64_config_error(ioa_cfg, hostrcb);
  2065. break;
  2066. case IPR_HOST_RCB_OVERLAY_ID_24:
  2067. case IPR_HOST_RCB_OVERLAY_ID_26:
  2068. ipr_log_sis64_array_error(ioa_cfg, hostrcb);
  2069. break;
  2070. case IPR_HOST_RCB_OVERLAY_ID_30:
  2071. ipr_log_sis64_fabric_error(ioa_cfg, hostrcb);
  2072. break;
  2073. case IPR_HOST_RCB_OVERLAY_ID_1:
  2074. case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
  2075. default:
  2076. ipr_log_generic_error(ioa_cfg, hostrcb);
  2077. break;
  2078. }
  2079. }
  2080. /**
  2081. * ipr_process_error - Op done function for an adapter error log.
  2082. * @ipr_cmd: ipr command struct
  2083. *
  2084. * This function is the op done function for an error log host
  2085. * controlled async from the adapter. It will log the error and
  2086. * send the HCAM back to the adapter.
  2087. *
  2088. * Return value:
  2089. * none
  2090. **/
  2091. static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
  2092. {
  2093. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  2094. struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
  2095. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  2096. u32 fd_ioasc;
  2097. if (ioa_cfg->sis64)
  2098. fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error64.fd_ioasc);
  2099. else
  2100. fd_ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
  2101. list_del(&hostrcb->queue);
  2102. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  2103. if (!ioasc) {
  2104. ipr_handle_log_data(ioa_cfg, hostrcb);
  2105. if (fd_ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED)
  2106. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_ABBREV);
  2107. } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
  2108. dev_err(&ioa_cfg->pdev->dev,
  2109. "Host RCB failed with IOASC: 0x%08X\n", ioasc);
  2110. }
  2111. ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
  2112. }
  2113. /**
  2114. * ipr_timeout - An internally generated op has timed out.
  2115. * @ipr_cmd: ipr command struct
  2116. *
  2117. * This function blocks host requests and initiates an
  2118. * adapter reset.
  2119. *
  2120. * Return value:
  2121. * none
  2122. **/
  2123. static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
  2124. {
  2125. unsigned long lock_flags = 0;
  2126. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  2127. ENTER;
  2128. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2129. ioa_cfg->errors_logged++;
  2130. dev_err(&ioa_cfg->pdev->dev,
  2131. "Adapter being reset due to command timeout.\n");
  2132. if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
  2133. ioa_cfg->sdt_state = GET_DUMP;
  2134. if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
  2135. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  2136. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2137. LEAVE;
  2138. }
  2139. /**
  2140. * ipr_oper_timeout - Adapter timed out transitioning to operational
  2141. * @ipr_cmd: ipr command struct
  2142. *
  2143. * This function blocks host requests and initiates an
  2144. * adapter reset.
  2145. *
  2146. * Return value:
  2147. * none
  2148. **/
  2149. static void ipr_oper_timeout(struct ipr_cmnd *ipr_cmd)
  2150. {
  2151. unsigned long lock_flags = 0;
  2152. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  2153. ENTER;
  2154. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2155. ioa_cfg->errors_logged++;
  2156. dev_err(&ioa_cfg->pdev->dev,
  2157. "Adapter timed out transitioning to operational.\n");
  2158. if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
  2159. ioa_cfg->sdt_state = GET_DUMP;
  2160. if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd) {
  2161. if (ipr_fastfail)
  2162. ioa_cfg->reset_retries += IPR_NUM_RESET_RELOAD_RETRIES;
  2163. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  2164. }
  2165. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2166. LEAVE;
  2167. }
  2168. /**
  2169. * ipr_reset_reload - Reset/Reload the IOA
  2170. * @ioa_cfg: ioa config struct
  2171. * @shutdown_type: shutdown type
  2172. *
  2173. * This function resets the adapter and re-initializes it.
  2174. * This function assumes that all new host commands have been stopped.
  2175. * Return value:
  2176. * SUCCESS / FAILED
  2177. **/
  2178. static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
  2179. enum ipr_shutdown_type shutdown_type)
  2180. {
  2181. if (!ioa_cfg->in_reset_reload)
  2182. ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
  2183. spin_unlock_irq(ioa_cfg->host->host_lock);
  2184. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  2185. spin_lock_irq(ioa_cfg->host->host_lock);
  2186. /* If we got hit with a host reset while we were already resetting
  2187. the adapter for some reason, and the reset failed. */
  2188. if (ioa_cfg->ioa_is_dead) {
  2189. ipr_trace;
  2190. return FAILED;
  2191. }
  2192. return SUCCESS;
  2193. }
  2194. /**
  2195. * ipr_find_ses_entry - Find matching SES in SES table
  2196. * @res: resource entry struct of SES
  2197. *
  2198. * Return value:
  2199. * pointer to SES table entry / NULL on failure
  2200. **/
  2201. static const struct ipr_ses_table_entry *
  2202. ipr_find_ses_entry(struct ipr_resource_entry *res)
  2203. {
  2204. int i, j, matches;
  2205. struct ipr_std_inq_vpids *vpids;
  2206. const struct ipr_ses_table_entry *ste = ipr_ses_table;
  2207. for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
  2208. for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
  2209. if (ste->compare_product_id_byte[j] == 'X') {
  2210. vpids = &res->std_inq_data.vpids;
  2211. if (vpids->product_id[j] == ste->product_id[j])
  2212. matches++;
  2213. else
  2214. break;
  2215. } else
  2216. matches++;
  2217. }
  2218. if (matches == IPR_PROD_ID_LEN)
  2219. return ste;
  2220. }
  2221. return NULL;
  2222. }
  2223. /**
  2224. * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
  2225. * @ioa_cfg: ioa config struct
  2226. * @bus: SCSI bus
  2227. * @bus_width: bus width
  2228. *
  2229. * Return value:
  2230. * SCSI bus speed in units of 100KHz, 1600 is 160 MHz
  2231. * For a 2-byte wide SCSI bus, the maximum transfer speed is
  2232. * twice the maximum transfer rate (e.g. for a wide enabled bus,
  2233. * max 160MHz = max 320MB/sec).
  2234. **/
  2235. static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
  2236. {
  2237. struct ipr_resource_entry *res;
  2238. const struct ipr_ses_table_entry *ste;
  2239. u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
  2240. /* Loop through each config table entry in the config table buffer */
  2241. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  2242. if (!(IPR_IS_SES_DEVICE(res->std_inq_data)))
  2243. continue;
  2244. if (bus != res->bus)
  2245. continue;
  2246. if (!(ste = ipr_find_ses_entry(res)))
  2247. continue;
  2248. max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
  2249. }
  2250. return max_xfer_rate;
  2251. }
  2252. /**
  2253. * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
  2254. * @ioa_cfg: ioa config struct
  2255. * @max_delay: max delay in micro-seconds to wait
  2256. *
  2257. * Waits for an IODEBUG ACK from the IOA, doing busy looping.
  2258. *
  2259. * Return value:
  2260. * 0 on success / other on failure
  2261. **/
  2262. static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
  2263. {
  2264. volatile u32 pcii_reg;
  2265. int delay = 1;
  2266. /* Read interrupt reg until IOA signals IO Debug Acknowledge */
  2267. while (delay < max_delay) {
  2268. pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
  2269. if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
  2270. return 0;
  2271. /* udelay cannot be used if delay is more than a few milliseconds */
  2272. if ((delay / 1000) > MAX_UDELAY_MS)
  2273. mdelay(delay / 1000);
  2274. else
  2275. udelay(delay);
  2276. delay += delay;
  2277. }
  2278. return -EIO;
  2279. }
  2280. /**
  2281. * ipr_get_sis64_dump_data_section - Dump IOA memory
  2282. * @ioa_cfg: ioa config struct
  2283. * @start_addr: adapter address to dump
  2284. * @dest: destination kernel buffer
  2285. * @length_in_words: length to dump in 4 byte words
  2286. *
  2287. * Return value:
  2288. * 0 on success
  2289. **/
  2290. static int ipr_get_sis64_dump_data_section(struct ipr_ioa_cfg *ioa_cfg,
  2291. u32 start_addr,
  2292. __be32 *dest, u32 length_in_words)
  2293. {
  2294. int i;
  2295. for (i = 0; i < length_in_words; i++) {
  2296. writel(start_addr+(i*4), ioa_cfg->regs.dump_addr_reg);
  2297. *dest = cpu_to_be32(readl(ioa_cfg->regs.dump_data_reg));
  2298. dest++;
  2299. }
  2300. return 0;
  2301. }
  2302. /**
  2303. * ipr_get_ldump_data_section - Dump IOA memory
  2304. * @ioa_cfg: ioa config struct
  2305. * @start_addr: adapter address to dump
  2306. * @dest: destination kernel buffer
  2307. * @length_in_words: length to dump in 4 byte words
  2308. *
  2309. * Return value:
  2310. * 0 on success / -EIO on failure
  2311. **/
  2312. static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
  2313. u32 start_addr,
  2314. __be32 *dest, u32 length_in_words)
  2315. {
  2316. volatile u32 temp_pcii_reg;
  2317. int i, delay = 0;
  2318. if (ioa_cfg->sis64)
  2319. return ipr_get_sis64_dump_data_section(ioa_cfg, start_addr,
  2320. dest, length_in_words);
  2321. /* Write IOA interrupt reg starting LDUMP state */
  2322. writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
  2323. ioa_cfg->regs.set_uproc_interrupt_reg32);
  2324. /* Wait for IO debug acknowledge */
  2325. if (ipr_wait_iodbg_ack(ioa_cfg,
  2326. IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
  2327. dev_err(&ioa_cfg->pdev->dev,
  2328. "IOA dump long data transfer timeout\n");
  2329. return -EIO;
  2330. }
  2331. /* Signal LDUMP interlocked - clear IO debug ack */
  2332. writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
  2333. ioa_cfg->regs.clr_interrupt_reg);
  2334. /* Write Mailbox with starting address */
  2335. writel(start_addr, ioa_cfg->ioa_mailbox);
  2336. /* Signal address valid - clear IOA Reset alert */
  2337. writel(IPR_UPROCI_RESET_ALERT,
  2338. ioa_cfg->regs.clr_uproc_interrupt_reg32);
  2339. for (i = 0; i < length_in_words; i++) {
  2340. /* Wait for IO debug acknowledge */
  2341. if (ipr_wait_iodbg_ack(ioa_cfg,
  2342. IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
  2343. dev_err(&ioa_cfg->pdev->dev,
  2344. "IOA dump short data transfer timeout\n");
  2345. return -EIO;
  2346. }
  2347. /* Read data from mailbox and increment destination pointer */
  2348. *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
  2349. dest++;
  2350. /* For all but the last word of data, signal data received */
  2351. if (i < (length_in_words - 1)) {
  2352. /* Signal dump data received - Clear IO debug Ack */
  2353. writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
  2354. ioa_cfg->regs.clr_interrupt_reg);
  2355. }
  2356. }
  2357. /* Signal end of block transfer. Set reset alert then clear IO debug ack */
  2358. writel(IPR_UPROCI_RESET_ALERT,
  2359. ioa_cfg->regs.set_uproc_interrupt_reg32);
  2360. writel(IPR_UPROCI_IO_DEBUG_ALERT,
  2361. ioa_cfg->regs.clr_uproc_interrupt_reg32);
  2362. /* Signal dump data received - Clear IO debug Ack */
  2363. writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
  2364. ioa_cfg->regs.clr_interrupt_reg);
  2365. /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
  2366. while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
  2367. temp_pcii_reg =
  2368. readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
  2369. if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
  2370. return 0;
  2371. udelay(10);
  2372. delay += 10;
  2373. }
  2374. return 0;
  2375. }
  2376. #ifdef CONFIG_SCSI_IPR_DUMP
  2377. /**
  2378. * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
  2379. * @ioa_cfg: ioa config struct
  2380. * @pci_address: adapter address
  2381. * @length: length of data to copy
  2382. *
  2383. * Copy data from PCI adapter to kernel buffer.
  2384. * Note: length MUST be a 4 byte multiple
  2385. * Return value:
  2386. * 0 on success / other on failure
  2387. **/
  2388. static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
  2389. unsigned long pci_address, u32 length)
  2390. {
  2391. int bytes_copied = 0;
  2392. int cur_len, rc, rem_len, rem_page_len;
  2393. __be32 *page;
  2394. unsigned long lock_flags = 0;
  2395. struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
  2396. while (bytes_copied < length &&
  2397. (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
  2398. if (ioa_dump->page_offset >= PAGE_SIZE ||
  2399. ioa_dump->page_offset == 0) {
  2400. page = (__be32 *)__get_free_page(GFP_ATOMIC);
  2401. if (!page) {
  2402. ipr_trace;
  2403. return bytes_copied;
  2404. }
  2405. ioa_dump->page_offset = 0;
  2406. ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
  2407. ioa_dump->next_page_index++;
  2408. } else
  2409. page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
  2410. rem_len = length - bytes_copied;
  2411. rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
  2412. cur_len = min(rem_len, rem_page_len);
  2413. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2414. if (ioa_cfg->sdt_state == ABORT_DUMP) {
  2415. rc = -EIO;
  2416. } else {
  2417. rc = ipr_get_ldump_data_section(ioa_cfg,
  2418. pci_address + bytes_copied,
  2419. &page[ioa_dump->page_offset / 4],
  2420. (cur_len / sizeof(u32)));
  2421. }
  2422. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2423. if (!rc) {
  2424. ioa_dump->page_offset += cur_len;
  2425. bytes_copied += cur_len;
  2426. } else {
  2427. ipr_trace;
  2428. break;
  2429. }
  2430. schedule();
  2431. }
  2432. return bytes_copied;
  2433. }
  2434. /**
  2435. * ipr_init_dump_entry_hdr - Initialize a dump entry header.
  2436. * @hdr: dump entry header struct
  2437. *
  2438. * Return value:
  2439. * nothing
  2440. **/
  2441. static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
  2442. {
  2443. hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
  2444. hdr->num_elems = 1;
  2445. hdr->offset = sizeof(*hdr);
  2446. hdr->status = IPR_DUMP_STATUS_SUCCESS;
  2447. }
  2448. /**
  2449. * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
  2450. * @ioa_cfg: ioa config struct
  2451. * @driver_dump: driver dump struct
  2452. *
  2453. * Return value:
  2454. * nothing
  2455. **/
  2456. static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
  2457. struct ipr_driver_dump *driver_dump)
  2458. {
  2459. struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
  2460. ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
  2461. driver_dump->ioa_type_entry.hdr.len =
  2462. sizeof(struct ipr_dump_ioa_type_entry) -
  2463. sizeof(struct ipr_dump_entry_header);
  2464. driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
  2465. driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
  2466. driver_dump->ioa_type_entry.type = ioa_cfg->type;
  2467. driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
  2468. (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
  2469. ucode_vpd->minor_release[1];
  2470. driver_dump->hdr.num_entries++;
  2471. }
  2472. /**
  2473. * ipr_dump_version_data - Fill in the driver version in the dump.
  2474. * @ioa_cfg: ioa config struct
  2475. * @driver_dump: driver dump struct
  2476. *
  2477. * Return value:
  2478. * nothing
  2479. **/
  2480. static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
  2481. struct ipr_driver_dump *driver_dump)
  2482. {
  2483. ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
  2484. driver_dump->version_entry.hdr.len =
  2485. sizeof(struct ipr_dump_version_entry) -
  2486. sizeof(struct ipr_dump_entry_header);
  2487. driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
  2488. driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
  2489. strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
  2490. driver_dump->hdr.num_entries++;
  2491. }
  2492. /**
  2493. * ipr_dump_trace_data - Fill in the IOA trace in the dump.
  2494. * @ioa_cfg: ioa config struct
  2495. * @driver_dump: driver dump struct
  2496. *
  2497. * Return value:
  2498. * nothing
  2499. **/
  2500. static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
  2501. struct ipr_driver_dump *driver_dump)
  2502. {
  2503. ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
  2504. driver_dump->trace_entry.hdr.len =
  2505. sizeof(struct ipr_dump_trace_entry) -
  2506. sizeof(struct ipr_dump_entry_header);
  2507. driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
  2508. driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
  2509. memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
  2510. driver_dump->hdr.num_entries++;
  2511. }
  2512. /**
  2513. * ipr_dump_location_data - Fill in the IOA location in the dump.
  2514. * @ioa_cfg: ioa config struct
  2515. * @driver_dump: driver dump struct
  2516. *
  2517. * Return value:
  2518. * nothing
  2519. **/
  2520. static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
  2521. struct ipr_driver_dump *driver_dump)
  2522. {
  2523. ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
  2524. driver_dump->location_entry.hdr.len =
  2525. sizeof(struct ipr_dump_location_entry) -
  2526. sizeof(struct ipr_dump_entry_header);
  2527. driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
  2528. driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
  2529. strcpy(driver_dump->location_entry.location, dev_name(&ioa_cfg->pdev->dev));
  2530. driver_dump->hdr.num_entries++;
  2531. }
  2532. /**
  2533. * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
  2534. * @ioa_cfg: ioa config struct
  2535. * @dump: dump struct
  2536. *
  2537. * Return value:
  2538. * nothing
  2539. **/
  2540. static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
  2541. {
  2542. unsigned long start_addr, sdt_word;
  2543. unsigned long lock_flags = 0;
  2544. struct ipr_driver_dump *driver_dump = &dump->driver_dump;
  2545. struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
  2546. u32 num_entries, start_off, end_off;
  2547. u32 bytes_to_copy, bytes_copied, rc;
  2548. struct ipr_sdt *sdt;
  2549. int valid = 1;
  2550. int i;
  2551. ENTER;
  2552. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2553. if (ioa_cfg->sdt_state != GET_DUMP) {
  2554. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2555. return;
  2556. }
  2557. start_addr = readl(ioa_cfg->ioa_mailbox);
  2558. if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(start_addr)) {
  2559. dev_err(&ioa_cfg->pdev->dev,
  2560. "Invalid dump table format: %lx\n", start_addr);
  2561. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2562. return;
  2563. }
  2564. dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
  2565. driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
  2566. /* Initialize the overall dump header */
  2567. driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
  2568. driver_dump->hdr.num_entries = 1;
  2569. driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
  2570. driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
  2571. driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
  2572. driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
  2573. ipr_dump_version_data(ioa_cfg, driver_dump);
  2574. ipr_dump_location_data(ioa_cfg, driver_dump);
  2575. ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
  2576. ipr_dump_trace_data(ioa_cfg, driver_dump);
  2577. /* Update dump_header */
  2578. driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
  2579. /* IOA Dump entry */
  2580. ipr_init_dump_entry_hdr(&ioa_dump->hdr);
  2581. ioa_dump->hdr.len = 0;
  2582. ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
  2583. ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
  2584. /* First entries in sdt are actually a list of dump addresses and
  2585. lengths to gather the real dump data. sdt represents the pointer
  2586. to the ioa generated dump table. Dump data will be extracted based
  2587. on entries in this table */
  2588. sdt = &ioa_dump->sdt;
  2589. rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (__be32 *)sdt,
  2590. sizeof(struct ipr_sdt) / sizeof(__be32));
  2591. /* Smart Dump table is ready to use and the first entry is valid */
  2592. if (rc || ((be32_to_cpu(sdt->hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
  2593. (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
  2594. dev_err(&ioa_cfg->pdev->dev,
  2595. "Dump of IOA failed. Dump table not valid: %d, %X.\n",
  2596. rc, be32_to_cpu(sdt->hdr.state));
  2597. driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
  2598. ioa_cfg->sdt_state = DUMP_OBTAINED;
  2599. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2600. return;
  2601. }
  2602. num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
  2603. if (num_entries > IPR_NUM_SDT_ENTRIES)
  2604. num_entries = IPR_NUM_SDT_ENTRIES;
  2605. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2606. for (i = 0; i < num_entries; i++) {
  2607. if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
  2608. driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
  2609. break;
  2610. }
  2611. if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
  2612. sdt_word = be32_to_cpu(sdt->entry[i].start_token);
  2613. if (ioa_cfg->sis64)
  2614. bytes_to_copy = be32_to_cpu(sdt->entry[i].end_token);
  2615. else {
  2616. start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
  2617. end_off = be32_to_cpu(sdt->entry[i].end_token);
  2618. if (ipr_sdt_is_fmt2(sdt_word) && sdt_word)
  2619. bytes_to_copy = end_off - start_off;
  2620. else
  2621. valid = 0;
  2622. }
  2623. if (valid) {
  2624. if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
  2625. sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
  2626. continue;
  2627. }
  2628. /* Copy data from adapter to driver buffers */
  2629. bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
  2630. bytes_to_copy);
  2631. ioa_dump->hdr.len += bytes_copied;
  2632. if (bytes_copied != bytes_to_copy) {
  2633. driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
  2634. break;
  2635. }
  2636. }
  2637. }
  2638. }
  2639. dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
  2640. /* Update dump_header */
  2641. driver_dump->hdr.len += ioa_dump->hdr.len;
  2642. wmb();
  2643. ioa_cfg->sdt_state = DUMP_OBTAINED;
  2644. LEAVE;
  2645. }
  2646. #else
  2647. #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
  2648. #endif
  2649. /**
  2650. * ipr_release_dump - Free adapter dump memory
  2651. * @kref: kref struct
  2652. *
  2653. * Return value:
  2654. * nothing
  2655. **/
  2656. static void ipr_release_dump(struct kref *kref)
  2657. {
  2658. struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
  2659. struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
  2660. unsigned long lock_flags = 0;
  2661. int i;
  2662. ENTER;
  2663. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2664. ioa_cfg->dump = NULL;
  2665. ioa_cfg->sdt_state = INACTIVE;
  2666. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2667. for (i = 0; i < dump->ioa_dump.next_page_index; i++)
  2668. free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
  2669. kfree(dump);
  2670. LEAVE;
  2671. }
  2672. /**
  2673. * ipr_worker_thread - Worker thread
  2674. * @work: ioa config struct
  2675. *
  2676. * Called at task level from a work thread. This function takes care
  2677. * of adding and removing device from the mid-layer as configuration
  2678. * changes are detected by the adapter.
  2679. *
  2680. * Return value:
  2681. * nothing
  2682. **/
  2683. static void ipr_worker_thread(struct work_struct *work)
  2684. {
  2685. unsigned long lock_flags;
  2686. struct ipr_resource_entry *res;
  2687. struct scsi_device *sdev;
  2688. struct ipr_dump *dump;
  2689. struct ipr_ioa_cfg *ioa_cfg =
  2690. container_of(work, struct ipr_ioa_cfg, work_q);
  2691. u8 bus, target, lun;
  2692. int did_work;
  2693. ENTER;
  2694. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2695. if (ioa_cfg->sdt_state == GET_DUMP) {
  2696. dump = ioa_cfg->dump;
  2697. if (!dump) {
  2698. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2699. return;
  2700. }
  2701. kref_get(&dump->kref);
  2702. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2703. ipr_get_ioa_dump(ioa_cfg, dump);
  2704. kref_put(&dump->kref, ipr_release_dump);
  2705. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2706. if (ioa_cfg->sdt_state == DUMP_OBTAINED)
  2707. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  2708. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2709. return;
  2710. }
  2711. restart:
  2712. do {
  2713. did_work = 0;
  2714. if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
  2715. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2716. return;
  2717. }
  2718. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  2719. if (res->del_from_ml && res->sdev) {
  2720. did_work = 1;
  2721. sdev = res->sdev;
  2722. if (!scsi_device_get(sdev)) {
  2723. list_move_tail(&res->queue, &ioa_cfg->free_res_q);
  2724. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2725. scsi_remove_device(sdev);
  2726. scsi_device_put(sdev);
  2727. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2728. }
  2729. break;
  2730. }
  2731. }
  2732. } while(did_work);
  2733. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  2734. if (res->add_to_ml) {
  2735. bus = res->bus;
  2736. target = res->target;
  2737. lun = res->lun;
  2738. res->add_to_ml = 0;
  2739. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2740. scsi_add_device(ioa_cfg->host, bus, target, lun);
  2741. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2742. goto restart;
  2743. }
  2744. }
  2745. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2746. kobject_uevent(&ioa_cfg->host->shost_dev.kobj, KOBJ_CHANGE);
  2747. LEAVE;
  2748. }
  2749. #ifdef CONFIG_SCSI_IPR_TRACE
  2750. /**
  2751. * ipr_read_trace - Dump the adapter trace
  2752. * @kobj: kobject struct
  2753. * @bin_attr: bin_attribute struct
  2754. * @buf: buffer
  2755. * @off: offset
  2756. * @count: buffer size
  2757. *
  2758. * Return value:
  2759. * number of bytes printed to buffer
  2760. **/
  2761. static ssize_t ipr_read_trace(struct kobject *kobj,
  2762. struct bin_attribute *bin_attr,
  2763. char *buf, loff_t off, size_t count)
  2764. {
  2765. struct device *dev = container_of(kobj, struct device, kobj);
  2766. struct Scsi_Host *shost = class_to_shost(dev);
  2767. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2768. unsigned long lock_flags = 0;
  2769. ssize_t ret;
  2770. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2771. ret = memory_read_from_buffer(buf, count, &off, ioa_cfg->trace,
  2772. IPR_TRACE_SIZE);
  2773. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2774. return ret;
  2775. }
  2776. static struct bin_attribute ipr_trace_attr = {
  2777. .attr = {
  2778. .name = "trace",
  2779. .mode = S_IRUGO,
  2780. },
  2781. .size = 0,
  2782. .read = ipr_read_trace,
  2783. };
  2784. #endif
  2785. /**
  2786. * ipr_show_fw_version - Show the firmware version
  2787. * @dev: class device struct
  2788. * @buf: buffer
  2789. *
  2790. * Return value:
  2791. * number of bytes printed to buffer
  2792. **/
  2793. static ssize_t ipr_show_fw_version(struct device *dev,
  2794. struct device_attribute *attr, char *buf)
  2795. {
  2796. struct Scsi_Host *shost = class_to_shost(dev);
  2797. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2798. struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
  2799. unsigned long lock_flags = 0;
  2800. int len;
  2801. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2802. len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
  2803. ucode_vpd->major_release, ucode_vpd->card_type,
  2804. ucode_vpd->minor_release[0],
  2805. ucode_vpd->minor_release[1]);
  2806. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2807. return len;
  2808. }
  2809. static struct device_attribute ipr_fw_version_attr = {
  2810. .attr = {
  2811. .name = "fw_version",
  2812. .mode = S_IRUGO,
  2813. },
  2814. .show = ipr_show_fw_version,
  2815. };
  2816. /**
  2817. * ipr_show_log_level - Show the adapter's error logging level
  2818. * @dev: class device struct
  2819. * @buf: buffer
  2820. *
  2821. * Return value:
  2822. * number of bytes printed to buffer
  2823. **/
  2824. static ssize_t ipr_show_log_level(struct device *dev,
  2825. struct device_attribute *attr, char *buf)
  2826. {
  2827. struct Scsi_Host *shost = class_to_shost(dev);
  2828. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2829. unsigned long lock_flags = 0;
  2830. int len;
  2831. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2832. len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
  2833. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2834. return len;
  2835. }
  2836. /**
  2837. * ipr_store_log_level - Change the adapter's error logging level
  2838. * @dev: class device struct
  2839. * @buf: buffer
  2840. *
  2841. * Return value:
  2842. * number of bytes printed to buffer
  2843. **/
  2844. static ssize_t ipr_store_log_level(struct device *dev,
  2845. struct device_attribute *attr,
  2846. const char *buf, size_t count)
  2847. {
  2848. struct Scsi_Host *shost = class_to_shost(dev);
  2849. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2850. unsigned long lock_flags = 0;
  2851. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2852. ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
  2853. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2854. return strlen(buf);
  2855. }
  2856. static struct device_attribute ipr_log_level_attr = {
  2857. .attr = {
  2858. .name = "log_level",
  2859. .mode = S_IRUGO | S_IWUSR,
  2860. },
  2861. .show = ipr_show_log_level,
  2862. .store = ipr_store_log_level
  2863. };
  2864. /**
  2865. * ipr_store_diagnostics - IOA Diagnostics interface
  2866. * @dev: device struct
  2867. * @buf: buffer
  2868. * @count: buffer size
  2869. *
  2870. * This function will reset the adapter and wait a reasonable
  2871. * amount of time for any errors that the adapter might log.
  2872. *
  2873. * Return value:
  2874. * count on success / other on failure
  2875. **/
  2876. static ssize_t ipr_store_diagnostics(struct device *dev,
  2877. struct device_attribute *attr,
  2878. const char *buf, size_t count)
  2879. {
  2880. struct Scsi_Host *shost = class_to_shost(dev);
  2881. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2882. unsigned long lock_flags = 0;
  2883. int rc = count;
  2884. if (!capable(CAP_SYS_ADMIN))
  2885. return -EACCES;
  2886. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2887. while(ioa_cfg->in_reset_reload) {
  2888. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2889. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  2890. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2891. }
  2892. ioa_cfg->errors_logged = 0;
  2893. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
  2894. if (ioa_cfg->in_reset_reload) {
  2895. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2896. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  2897. /* Wait for a second for any errors to be logged */
  2898. msleep(1000);
  2899. } else {
  2900. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2901. return -EIO;
  2902. }
  2903. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2904. if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
  2905. rc = -EIO;
  2906. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2907. return rc;
  2908. }
  2909. static struct device_attribute ipr_diagnostics_attr = {
  2910. .attr = {
  2911. .name = "run_diagnostics",
  2912. .mode = S_IWUSR,
  2913. },
  2914. .store = ipr_store_diagnostics
  2915. };
  2916. /**
  2917. * ipr_show_adapter_state - Show the adapter's state
  2918. * @class_dev: device struct
  2919. * @buf: buffer
  2920. *
  2921. * Return value:
  2922. * number of bytes printed to buffer
  2923. **/
  2924. static ssize_t ipr_show_adapter_state(struct device *dev,
  2925. struct device_attribute *attr, char *buf)
  2926. {
  2927. struct Scsi_Host *shost = class_to_shost(dev);
  2928. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2929. unsigned long lock_flags = 0;
  2930. int len;
  2931. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2932. if (ioa_cfg->ioa_is_dead)
  2933. len = snprintf(buf, PAGE_SIZE, "offline\n");
  2934. else
  2935. len = snprintf(buf, PAGE_SIZE, "online\n");
  2936. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2937. return len;
  2938. }
  2939. /**
  2940. * ipr_store_adapter_state - Change adapter state
  2941. * @dev: device struct
  2942. * @buf: buffer
  2943. * @count: buffer size
  2944. *
  2945. * This function will change the adapter's state.
  2946. *
  2947. * Return value:
  2948. * count on success / other on failure
  2949. **/
  2950. static ssize_t ipr_store_adapter_state(struct device *dev,
  2951. struct device_attribute *attr,
  2952. const char *buf, size_t count)
  2953. {
  2954. struct Scsi_Host *shost = class_to_shost(dev);
  2955. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2956. unsigned long lock_flags;
  2957. int result = count;
  2958. if (!capable(CAP_SYS_ADMIN))
  2959. return -EACCES;
  2960. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  2961. if (ioa_cfg->ioa_is_dead && !strncmp(buf, "online", 6)) {
  2962. ioa_cfg->ioa_is_dead = 0;
  2963. ioa_cfg->reset_retries = 0;
  2964. ioa_cfg->in_ioa_bringdown = 0;
  2965. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  2966. }
  2967. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  2968. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  2969. return result;
  2970. }
  2971. static struct device_attribute ipr_ioa_state_attr = {
  2972. .attr = {
  2973. .name = "online_state",
  2974. .mode = S_IRUGO | S_IWUSR,
  2975. },
  2976. .show = ipr_show_adapter_state,
  2977. .store = ipr_store_adapter_state
  2978. };
  2979. /**
  2980. * ipr_store_reset_adapter - Reset the adapter
  2981. * @dev: device struct
  2982. * @buf: buffer
  2983. * @count: buffer size
  2984. *
  2985. * This function will reset the adapter.
  2986. *
  2987. * Return value:
  2988. * count on success / other on failure
  2989. **/
  2990. static ssize_t ipr_store_reset_adapter(struct device *dev,
  2991. struct device_attribute *attr,
  2992. const char *buf, size_t count)
  2993. {
  2994. struct Scsi_Host *shost = class_to_shost(dev);
  2995. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  2996. unsigned long lock_flags;
  2997. int result = count;
  2998. if (!capable(CAP_SYS_ADMIN))
  2999. return -EACCES;
  3000. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3001. if (!ioa_cfg->in_reset_reload)
  3002. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
  3003. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3004. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  3005. return result;
  3006. }
  3007. static struct device_attribute ipr_ioa_reset_attr = {
  3008. .attr = {
  3009. .name = "reset_host",
  3010. .mode = S_IWUSR,
  3011. },
  3012. .store = ipr_store_reset_adapter
  3013. };
  3014. /**
  3015. * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
  3016. * @buf_len: buffer length
  3017. *
  3018. * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
  3019. * list to use for microcode download
  3020. *
  3021. * Return value:
  3022. * pointer to sglist / NULL on failure
  3023. **/
  3024. static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
  3025. {
  3026. int sg_size, order, bsize_elem, num_elem, i, j;
  3027. struct ipr_sglist *sglist;
  3028. struct scatterlist *scatterlist;
  3029. struct page *page;
  3030. /* Get the minimum size per scatter/gather element */
  3031. sg_size = buf_len / (IPR_MAX_SGLIST - 1);
  3032. /* Get the actual size per element */
  3033. order = get_order(sg_size);
  3034. /* Determine the actual number of bytes per element */
  3035. bsize_elem = PAGE_SIZE * (1 << order);
  3036. /* Determine the actual number of sg entries needed */
  3037. if (buf_len % bsize_elem)
  3038. num_elem = (buf_len / bsize_elem) + 1;
  3039. else
  3040. num_elem = buf_len / bsize_elem;
  3041. /* Allocate a scatter/gather list for the DMA */
  3042. sglist = kzalloc(sizeof(struct ipr_sglist) +
  3043. (sizeof(struct scatterlist) * (num_elem - 1)),
  3044. GFP_KERNEL);
  3045. if (sglist == NULL) {
  3046. ipr_trace;
  3047. return NULL;
  3048. }
  3049. scatterlist = sglist->scatterlist;
  3050. sg_init_table(scatterlist, num_elem);
  3051. sglist->order = order;
  3052. sglist->num_sg = num_elem;
  3053. /* Allocate a bunch of sg elements */
  3054. for (i = 0; i < num_elem; i++) {
  3055. page = alloc_pages(GFP_KERNEL, order);
  3056. if (!page) {
  3057. ipr_trace;
  3058. /* Free up what we already allocated */
  3059. for (j = i - 1; j >= 0; j--)
  3060. __free_pages(sg_page(&scatterlist[j]), order);
  3061. kfree(sglist);
  3062. return NULL;
  3063. }
  3064. sg_set_page(&scatterlist[i], page, 0, 0);
  3065. }
  3066. return sglist;
  3067. }
  3068. /**
  3069. * ipr_free_ucode_buffer - Frees a microcode download buffer
  3070. * @p_dnld: scatter/gather list pointer
  3071. *
  3072. * Free a DMA'able ucode download buffer previously allocated with
  3073. * ipr_alloc_ucode_buffer
  3074. *
  3075. * Return value:
  3076. * nothing
  3077. **/
  3078. static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
  3079. {
  3080. int i;
  3081. for (i = 0; i < sglist->num_sg; i++)
  3082. __free_pages(sg_page(&sglist->scatterlist[i]), sglist->order);
  3083. kfree(sglist);
  3084. }
  3085. /**
  3086. * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
  3087. * @sglist: scatter/gather list pointer
  3088. * @buffer: buffer pointer
  3089. * @len: buffer length
  3090. *
  3091. * Copy a microcode image from a user buffer into a buffer allocated by
  3092. * ipr_alloc_ucode_buffer
  3093. *
  3094. * Return value:
  3095. * 0 on success / other on failure
  3096. **/
  3097. static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
  3098. u8 *buffer, u32 len)
  3099. {
  3100. int bsize_elem, i, result = 0;
  3101. struct scatterlist *scatterlist;
  3102. void *kaddr;
  3103. /* Determine the actual number of bytes per element */
  3104. bsize_elem = PAGE_SIZE * (1 << sglist->order);
  3105. scatterlist = sglist->scatterlist;
  3106. for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
  3107. struct page *page = sg_page(&scatterlist[i]);
  3108. kaddr = kmap(page);
  3109. memcpy(kaddr, buffer, bsize_elem);
  3110. kunmap(page);
  3111. scatterlist[i].length = bsize_elem;
  3112. if (result != 0) {
  3113. ipr_trace;
  3114. return result;
  3115. }
  3116. }
  3117. if (len % bsize_elem) {
  3118. struct page *page = sg_page(&scatterlist[i]);
  3119. kaddr = kmap(page);
  3120. memcpy(kaddr, buffer, len % bsize_elem);
  3121. kunmap(page);
  3122. scatterlist[i].length = len % bsize_elem;
  3123. }
  3124. sglist->buffer_len = len;
  3125. return result;
  3126. }
  3127. /**
  3128. * ipr_build_ucode_ioadl64 - Build a microcode download IOADL
  3129. * @ipr_cmd: ipr command struct
  3130. * @sglist: scatter/gather list
  3131. *
  3132. * Builds a microcode download IOA data list (IOADL).
  3133. *
  3134. **/
  3135. static void ipr_build_ucode_ioadl64(struct ipr_cmnd *ipr_cmd,
  3136. struct ipr_sglist *sglist)
  3137. {
  3138. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  3139. struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
  3140. struct scatterlist *scatterlist = sglist->scatterlist;
  3141. int i;
  3142. ipr_cmd->dma_use_sg = sglist->num_dma_sg;
  3143. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  3144. ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
  3145. ioarcb->ioadl_len =
  3146. cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
  3147. for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
  3148. ioadl64[i].flags = cpu_to_be32(IPR_IOADL_FLAGS_WRITE);
  3149. ioadl64[i].data_len = cpu_to_be32(sg_dma_len(&scatterlist[i]));
  3150. ioadl64[i].address = cpu_to_be64(sg_dma_address(&scatterlist[i]));
  3151. }
  3152. ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
  3153. }
  3154. /**
  3155. * ipr_build_ucode_ioadl - Build a microcode download IOADL
  3156. * @ipr_cmd: ipr command struct
  3157. * @sglist: scatter/gather list
  3158. *
  3159. * Builds a microcode download IOA data list (IOADL).
  3160. *
  3161. **/
  3162. static void ipr_build_ucode_ioadl(struct ipr_cmnd *ipr_cmd,
  3163. struct ipr_sglist *sglist)
  3164. {
  3165. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  3166. struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
  3167. struct scatterlist *scatterlist = sglist->scatterlist;
  3168. int i;
  3169. ipr_cmd->dma_use_sg = sglist->num_dma_sg;
  3170. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  3171. ioarcb->data_transfer_length = cpu_to_be32(sglist->buffer_len);
  3172. ioarcb->ioadl_len =
  3173. cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
  3174. for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
  3175. ioadl[i].flags_and_data_len =
  3176. cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
  3177. ioadl[i].address =
  3178. cpu_to_be32(sg_dma_address(&scatterlist[i]));
  3179. }
  3180. ioadl[i-1].flags_and_data_len |=
  3181. cpu_to_be32(IPR_IOADL_FLAGS_LAST);
  3182. }
  3183. /**
  3184. * ipr_update_ioa_ucode - Update IOA's microcode
  3185. * @ioa_cfg: ioa config struct
  3186. * @sglist: scatter/gather list
  3187. *
  3188. * Initiate an adapter reset to update the IOA's microcode
  3189. *
  3190. * Return value:
  3191. * 0 on success / -EIO on failure
  3192. **/
  3193. static int ipr_update_ioa_ucode(struct ipr_ioa_cfg *ioa_cfg,
  3194. struct ipr_sglist *sglist)
  3195. {
  3196. unsigned long lock_flags;
  3197. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3198. while(ioa_cfg->in_reset_reload) {
  3199. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3200. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  3201. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3202. }
  3203. if (ioa_cfg->ucode_sglist) {
  3204. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3205. dev_err(&ioa_cfg->pdev->dev,
  3206. "Microcode download already in progress\n");
  3207. return -EIO;
  3208. }
  3209. sglist->num_dma_sg = pci_map_sg(ioa_cfg->pdev, sglist->scatterlist,
  3210. sglist->num_sg, DMA_TO_DEVICE);
  3211. if (!sglist->num_dma_sg) {
  3212. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3213. dev_err(&ioa_cfg->pdev->dev,
  3214. "Failed to map microcode download buffer!\n");
  3215. return -EIO;
  3216. }
  3217. ioa_cfg->ucode_sglist = sglist;
  3218. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
  3219. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3220. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  3221. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3222. ioa_cfg->ucode_sglist = NULL;
  3223. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3224. return 0;
  3225. }
  3226. /**
  3227. * ipr_store_update_fw - Update the firmware on the adapter
  3228. * @class_dev: device struct
  3229. * @buf: buffer
  3230. * @count: buffer size
  3231. *
  3232. * This function will update the firmware on the adapter.
  3233. *
  3234. * Return value:
  3235. * count on success / other on failure
  3236. **/
  3237. static ssize_t ipr_store_update_fw(struct device *dev,
  3238. struct device_attribute *attr,
  3239. const char *buf, size_t count)
  3240. {
  3241. struct Scsi_Host *shost = class_to_shost(dev);
  3242. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  3243. struct ipr_ucode_image_header *image_hdr;
  3244. const struct firmware *fw_entry;
  3245. struct ipr_sglist *sglist;
  3246. char fname[100];
  3247. char *src;
  3248. int len, result, dnld_size;
  3249. if (!capable(CAP_SYS_ADMIN))
  3250. return -EACCES;
  3251. len = snprintf(fname, 99, "%s", buf);
  3252. fname[len-1] = '\0';
  3253. if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
  3254. dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
  3255. return -EIO;
  3256. }
  3257. image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
  3258. if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
  3259. (ioa_cfg->vpd_cbs->page3_data.card_type &&
  3260. ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
  3261. dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
  3262. release_firmware(fw_entry);
  3263. return -EINVAL;
  3264. }
  3265. src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
  3266. dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
  3267. sglist = ipr_alloc_ucode_buffer(dnld_size);
  3268. if (!sglist) {
  3269. dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
  3270. release_firmware(fw_entry);
  3271. return -ENOMEM;
  3272. }
  3273. result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
  3274. if (result) {
  3275. dev_err(&ioa_cfg->pdev->dev,
  3276. "Microcode buffer copy to DMA buffer failed\n");
  3277. goto out;
  3278. }
  3279. result = ipr_update_ioa_ucode(ioa_cfg, sglist);
  3280. if (!result)
  3281. result = count;
  3282. out:
  3283. ipr_free_ucode_buffer(sglist);
  3284. release_firmware(fw_entry);
  3285. return result;
  3286. }
  3287. static struct device_attribute ipr_update_fw_attr = {
  3288. .attr = {
  3289. .name = "update_fw",
  3290. .mode = S_IWUSR,
  3291. },
  3292. .store = ipr_store_update_fw
  3293. };
  3294. static struct device_attribute *ipr_ioa_attrs[] = {
  3295. &ipr_fw_version_attr,
  3296. &ipr_log_level_attr,
  3297. &ipr_diagnostics_attr,
  3298. &ipr_ioa_state_attr,
  3299. &ipr_ioa_reset_attr,
  3300. &ipr_update_fw_attr,
  3301. NULL,
  3302. };
  3303. #ifdef CONFIG_SCSI_IPR_DUMP
  3304. /**
  3305. * ipr_read_dump - Dump the adapter
  3306. * @kobj: kobject struct
  3307. * @bin_attr: bin_attribute struct
  3308. * @buf: buffer
  3309. * @off: offset
  3310. * @count: buffer size
  3311. *
  3312. * Return value:
  3313. * number of bytes printed to buffer
  3314. **/
  3315. static ssize_t ipr_read_dump(struct kobject *kobj,
  3316. struct bin_attribute *bin_attr,
  3317. char *buf, loff_t off, size_t count)
  3318. {
  3319. struct device *cdev = container_of(kobj, struct device, kobj);
  3320. struct Scsi_Host *shost = class_to_shost(cdev);
  3321. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  3322. struct ipr_dump *dump;
  3323. unsigned long lock_flags = 0;
  3324. char *src;
  3325. int len;
  3326. size_t rc = count;
  3327. if (!capable(CAP_SYS_ADMIN))
  3328. return -EACCES;
  3329. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3330. dump = ioa_cfg->dump;
  3331. if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
  3332. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3333. return 0;
  3334. }
  3335. kref_get(&dump->kref);
  3336. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3337. if (off > dump->driver_dump.hdr.len) {
  3338. kref_put(&dump->kref, ipr_release_dump);
  3339. return 0;
  3340. }
  3341. if (off + count > dump->driver_dump.hdr.len) {
  3342. count = dump->driver_dump.hdr.len - off;
  3343. rc = count;
  3344. }
  3345. if (count && off < sizeof(dump->driver_dump)) {
  3346. if (off + count > sizeof(dump->driver_dump))
  3347. len = sizeof(dump->driver_dump) - off;
  3348. else
  3349. len = count;
  3350. src = (u8 *)&dump->driver_dump + off;
  3351. memcpy(buf, src, len);
  3352. buf += len;
  3353. off += len;
  3354. count -= len;
  3355. }
  3356. off -= sizeof(dump->driver_dump);
  3357. if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
  3358. if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
  3359. len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
  3360. else
  3361. len = count;
  3362. src = (u8 *)&dump->ioa_dump + off;
  3363. memcpy(buf, src, len);
  3364. buf += len;
  3365. off += len;
  3366. count -= len;
  3367. }
  3368. off -= offsetof(struct ipr_ioa_dump, ioa_data);
  3369. while (count) {
  3370. if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
  3371. len = PAGE_ALIGN(off) - off;
  3372. else
  3373. len = count;
  3374. src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
  3375. src += off & ~PAGE_MASK;
  3376. memcpy(buf, src, len);
  3377. buf += len;
  3378. off += len;
  3379. count -= len;
  3380. }
  3381. kref_put(&dump->kref, ipr_release_dump);
  3382. return rc;
  3383. }
  3384. /**
  3385. * ipr_alloc_dump - Prepare for adapter dump
  3386. * @ioa_cfg: ioa config struct
  3387. *
  3388. * Return value:
  3389. * 0 on success / other on failure
  3390. **/
  3391. static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
  3392. {
  3393. struct ipr_dump *dump;
  3394. unsigned long lock_flags = 0;
  3395. dump = kzalloc(sizeof(struct ipr_dump), GFP_KERNEL);
  3396. if (!dump) {
  3397. ipr_err("Dump memory allocation failed\n");
  3398. return -ENOMEM;
  3399. }
  3400. kref_init(&dump->kref);
  3401. dump->ioa_cfg = ioa_cfg;
  3402. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3403. if (INACTIVE != ioa_cfg->sdt_state) {
  3404. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3405. kfree(dump);
  3406. return 0;
  3407. }
  3408. ioa_cfg->dump = dump;
  3409. ioa_cfg->sdt_state = WAIT_FOR_DUMP;
  3410. if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
  3411. ioa_cfg->dump_taken = 1;
  3412. schedule_work(&ioa_cfg->work_q);
  3413. }
  3414. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3415. return 0;
  3416. }
  3417. /**
  3418. * ipr_free_dump - Free adapter dump memory
  3419. * @ioa_cfg: ioa config struct
  3420. *
  3421. * Return value:
  3422. * 0 on success / other on failure
  3423. **/
  3424. static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
  3425. {
  3426. struct ipr_dump *dump;
  3427. unsigned long lock_flags = 0;
  3428. ENTER;
  3429. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3430. dump = ioa_cfg->dump;
  3431. if (!dump) {
  3432. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3433. return 0;
  3434. }
  3435. ioa_cfg->dump = NULL;
  3436. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3437. kref_put(&dump->kref, ipr_release_dump);
  3438. LEAVE;
  3439. return 0;
  3440. }
  3441. /**
  3442. * ipr_write_dump - Setup dump state of adapter
  3443. * @kobj: kobject struct
  3444. * @bin_attr: bin_attribute struct
  3445. * @buf: buffer
  3446. * @off: offset
  3447. * @count: buffer size
  3448. *
  3449. * Return value:
  3450. * number of bytes printed to buffer
  3451. **/
  3452. static ssize_t ipr_write_dump(struct kobject *kobj,
  3453. struct bin_attribute *bin_attr,
  3454. char *buf, loff_t off, size_t count)
  3455. {
  3456. struct device *cdev = container_of(kobj, struct device, kobj);
  3457. struct Scsi_Host *shost = class_to_shost(cdev);
  3458. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
  3459. int rc;
  3460. if (!capable(CAP_SYS_ADMIN))
  3461. return -EACCES;
  3462. if (buf[0] == '1')
  3463. rc = ipr_alloc_dump(ioa_cfg);
  3464. else if (buf[0] == '0')
  3465. rc = ipr_free_dump(ioa_cfg);
  3466. else
  3467. return -EINVAL;
  3468. if (rc)
  3469. return rc;
  3470. else
  3471. return count;
  3472. }
  3473. static struct bin_attribute ipr_dump_attr = {
  3474. .attr = {
  3475. .name = "dump",
  3476. .mode = S_IRUSR | S_IWUSR,
  3477. },
  3478. .size = 0,
  3479. .read = ipr_read_dump,
  3480. .write = ipr_write_dump
  3481. };
  3482. #else
  3483. static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
  3484. #endif
  3485. /**
  3486. * ipr_change_queue_depth - Change the device's queue depth
  3487. * @sdev: scsi device struct
  3488. * @qdepth: depth to set
  3489. * @reason: calling context
  3490. *
  3491. * Return value:
  3492. * actual depth set
  3493. **/
  3494. static int ipr_change_queue_depth(struct scsi_device *sdev, int qdepth,
  3495. int reason)
  3496. {
  3497. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
  3498. struct ipr_resource_entry *res;
  3499. unsigned long lock_flags = 0;
  3500. if (reason != SCSI_QDEPTH_DEFAULT)
  3501. return -EOPNOTSUPP;
  3502. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3503. res = (struct ipr_resource_entry *)sdev->hostdata;
  3504. if (res && ipr_is_gata(res) && qdepth > IPR_MAX_CMD_PER_ATA_LUN)
  3505. qdepth = IPR_MAX_CMD_PER_ATA_LUN;
  3506. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3507. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  3508. return sdev->queue_depth;
  3509. }
  3510. /**
  3511. * ipr_change_queue_type - Change the device's queue type
  3512. * @dsev: scsi device struct
  3513. * @tag_type: type of tags to use
  3514. *
  3515. * Return value:
  3516. * actual queue type set
  3517. **/
  3518. static int ipr_change_queue_type(struct scsi_device *sdev, int tag_type)
  3519. {
  3520. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
  3521. struct ipr_resource_entry *res;
  3522. unsigned long lock_flags = 0;
  3523. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3524. res = (struct ipr_resource_entry *)sdev->hostdata;
  3525. if (res) {
  3526. if (ipr_is_gscsi(res) && sdev->tagged_supported) {
  3527. /*
  3528. * We don't bother quiescing the device here since the
  3529. * adapter firmware does it for us.
  3530. */
  3531. scsi_set_tag_type(sdev, tag_type);
  3532. if (tag_type)
  3533. scsi_activate_tcq(sdev, sdev->queue_depth);
  3534. else
  3535. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  3536. } else
  3537. tag_type = 0;
  3538. } else
  3539. tag_type = 0;
  3540. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3541. return tag_type;
  3542. }
  3543. /**
  3544. * ipr_show_adapter_handle - Show the adapter's resource handle for this device
  3545. * @dev: device struct
  3546. * @buf: buffer
  3547. *
  3548. * Return value:
  3549. * number of bytes printed to buffer
  3550. **/
  3551. static ssize_t ipr_show_adapter_handle(struct device *dev, struct device_attribute *attr, char *buf)
  3552. {
  3553. struct scsi_device *sdev = to_scsi_device(dev);
  3554. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
  3555. struct ipr_resource_entry *res;
  3556. unsigned long lock_flags = 0;
  3557. ssize_t len = -ENXIO;
  3558. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3559. res = (struct ipr_resource_entry *)sdev->hostdata;
  3560. if (res)
  3561. len = snprintf(buf, PAGE_SIZE, "%08X\n", res->res_handle);
  3562. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3563. return len;
  3564. }
  3565. static struct device_attribute ipr_adapter_handle_attr = {
  3566. .attr = {
  3567. .name = "adapter_handle",
  3568. .mode = S_IRUSR,
  3569. },
  3570. .show = ipr_show_adapter_handle
  3571. };
  3572. /**
  3573. * ipr_show_resource_path - Show the resource path for this device.
  3574. * @dev: device struct
  3575. * @buf: buffer
  3576. *
  3577. * Return value:
  3578. * number of bytes printed to buffer
  3579. **/
  3580. static ssize_t ipr_show_resource_path(struct device *dev, struct device_attribute *attr, char *buf)
  3581. {
  3582. struct scsi_device *sdev = to_scsi_device(dev);
  3583. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
  3584. struct ipr_resource_entry *res;
  3585. unsigned long lock_flags = 0;
  3586. ssize_t len = -ENXIO;
  3587. char buffer[IPR_MAX_RES_PATH_LENGTH];
  3588. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3589. res = (struct ipr_resource_entry *)sdev->hostdata;
  3590. if (res)
  3591. len = snprintf(buf, PAGE_SIZE, "%s\n",
  3592. ipr_format_resource_path(&res->res_path[0], &buffer[0]));
  3593. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3594. return len;
  3595. }
  3596. static struct device_attribute ipr_resource_path_attr = {
  3597. .attr = {
  3598. .name = "resource_path",
  3599. .mode = S_IRUSR,
  3600. },
  3601. .show = ipr_show_resource_path
  3602. };
  3603. static struct device_attribute *ipr_dev_attrs[] = {
  3604. &ipr_adapter_handle_attr,
  3605. &ipr_resource_path_attr,
  3606. NULL,
  3607. };
  3608. /**
  3609. * ipr_biosparam - Return the HSC mapping
  3610. * @sdev: scsi device struct
  3611. * @block_device: block device pointer
  3612. * @capacity: capacity of the device
  3613. * @parm: Array containing returned HSC values.
  3614. *
  3615. * This function generates the HSC parms that fdisk uses.
  3616. * We want to make sure we return something that places partitions
  3617. * on 4k boundaries for best performance with the IOA.
  3618. *
  3619. * Return value:
  3620. * 0 on success
  3621. **/
  3622. static int ipr_biosparam(struct scsi_device *sdev,
  3623. struct block_device *block_device,
  3624. sector_t capacity, int *parm)
  3625. {
  3626. int heads, sectors;
  3627. sector_t cylinders;
  3628. heads = 128;
  3629. sectors = 32;
  3630. cylinders = capacity;
  3631. sector_div(cylinders, (128 * 32));
  3632. /* return result */
  3633. parm[0] = heads;
  3634. parm[1] = sectors;
  3635. parm[2] = cylinders;
  3636. return 0;
  3637. }
  3638. /**
  3639. * ipr_find_starget - Find target based on bus/target.
  3640. * @starget: scsi target struct
  3641. *
  3642. * Return value:
  3643. * resource entry pointer if found / NULL if not found
  3644. **/
  3645. static struct ipr_resource_entry *ipr_find_starget(struct scsi_target *starget)
  3646. {
  3647. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  3648. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
  3649. struct ipr_resource_entry *res;
  3650. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  3651. if ((res->bus == starget->channel) &&
  3652. (res->target == starget->id) &&
  3653. (res->lun == 0)) {
  3654. return res;
  3655. }
  3656. }
  3657. return NULL;
  3658. }
  3659. static struct ata_port_info sata_port_info;
  3660. /**
  3661. * ipr_target_alloc - Prepare for commands to a SCSI target
  3662. * @starget: scsi target struct
  3663. *
  3664. * If the device is a SATA device, this function allocates an
  3665. * ATA port with libata, else it does nothing.
  3666. *
  3667. * Return value:
  3668. * 0 on success / non-0 on failure
  3669. **/
  3670. static int ipr_target_alloc(struct scsi_target *starget)
  3671. {
  3672. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  3673. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
  3674. struct ipr_sata_port *sata_port;
  3675. struct ata_port *ap;
  3676. struct ipr_resource_entry *res;
  3677. unsigned long lock_flags;
  3678. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3679. res = ipr_find_starget(starget);
  3680. starget->hostdata = NULL;
  3681. if (res && ipr_is_gata(res)) {
  3682. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3683. sata_port = kzalloc(sizeof(*sata_port), GFP_KERNEL);
  3684. if (!sata_port)
  3685. return -ENOMEM;
  3686. ap = ata_sas_port_alloc(&ioa_cfg->ata_host, &sata_port_info, shost);
  3687. if (ap) {
  3688. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3689. sata_port->ioa_cfg = ioa_cfg;
  3690. sata_port->ap = ap;
  3691. sata_port->res = res;
  3692. res->sata_port = sata_port;
  3693. ap->private_data = sata_port;
  3694. starget->hostdata = sata_port;
  3695. } else {
  3696. kfree(sata_port);
  3697. return -ENOMEM;
  3698. }
  3699. }
  3700. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3701. return 0;
  3702. }
  3703. /**
  3704. * ipr_target_destroy - Destroy a SCSI target
  3705. * @starget: scsi target struct
  3706. *
  3707. * If the device was a SATA device, this function frees the libata
  3708. * ATA port, else it does nothing.
  3709. *
  3710. **/
  3711. static void ipr_target_destroy(struct scsi_target *starget)
  3712. {
  3713. struct ipr_sata_port *sata_port = starget->hostdata;
  3714. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  3715. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) shost->hostdata;
  3716. if (ioa_cfg->sis64) {
  3717. if (starget->channel == IPR_ARRAY_VIRTUAL_BUS)
  3718. clear_bit(starget->id, ioa_cfg->array_ids);
  3719. else if (starget->channel == IPR_VSET_VIRTUAL_BUS)
  3720. clear_bit(starget->id, ioa_cfg->vset_ids);
  3721. else if (starget->channel == 0)
  3722. clear_bit(starget->id, ioa_cfg->target_ids);
  3723. }
  3724. if (sata_port) {
  3725. starget->hostdata = NULL;
  3726. ata_sas_port_destroy(sata_port->ap);
  3727. kfree(sata_port);
  3728. }
  3729. }
  3730. /**
  3731. * ipr_find_sdev - Find device based on bus/target/lun.
  3732. * @sdev: scsi device struct
  3733. *
  3734. * Return value:
  3735. * resource entry pointer if found / NULL if not found
  3736. **/
  3737. static struct ipr_resource_entry *ipr_find_sdev(struct scsi_device *sdev)
  3738. {
  3739. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
  3740. struct ipr_resource_entry *res;
  3741. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  3742. if ((res->bus == sdev->channel) &&
  3743. (res->target == sdev->id) &&
  3744. (res->lun == sdev->lun))
  3745. return res;
  3746. }
  3747. return NULL;
  3748. }
  3749. /**
  3750. * ipr_slave_destroy - Unconfigure a SCSI device
  3751. * @sdev: scsi device struct
  3752. *
  3753. * Return value:
  3754. * nothing
  3755. **/
  3756. static void ipr_slave_destroy(struct scsi_device *sdev)
  3757. {
  3758. struct ipr_resource_entry *res;
  3759. struct ipr_ioa_cfg *ioa_cfg;
  3760. unsigned long lock_flags = 0;
  3761. ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
  3762. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3763. res = (struct ipr_resource_entry *) sdev->hostdata;
  3764. if (res) {
  3765. if (res->sata_port)
  3766. ata_port_disable(res->sata_port->ap);
  3767. sdev->hostdata = NULL;
  3768. res->sdev = NULL;
  3769. res->sata_port = NULL;
  3770. }
  3771. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3772. }
  3773. /**
  3774. * ipr_slave_configure - Configure a SCSI device
  3775. * @sdev: scsi device struct
  3776. *
  3777. * This function configures the specified scsi device.
  3778. *
  3779. * Return value:
  3780. * 0 on success
  3781. **/
  3782. static int ipr_slave_configure(struct scsi_device *sdev)
  3783. {
  3784. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
  3785. struct ipr_resource_entry *res;
  3786. struct ata_port *ap = NULL;
  3787. unsigned long lock_flags = 0;
  3788. char buffer[IPR_MAX_RES_PATH_LENGTH];
  3789. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3790. res = sdev->hostdata;
  3791. if (res) {
  3792. if (ipr_is_af_dasd_device(res))
  3793. sdev->type = TYPE_RAID;
  3794. if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res)) {
  3795. sdev->scsi_level = 4;
  3796. sdev->no_uld_attach = 1;
  3797. }
  3798. if (ipr_is_vset_device(res)) {
  3799. blk_queue_rq_timeout(sdev->request_queue,
  3800. IPR_VSET_RW_TIMEOUT);
  3801. blk_queue_max_hw_sectors(sdev->request_queue, IPR_VSET_MAX_SECTORS);
  3802. }
  3803. if (ipr_is_vset_device(res) || ipr_is_scsi_disk(res))
  3804. sdev->allow_restart = 1;
  3805. if (ipr_is_gata(res) && res->sata_port)
  3806. ap = res->sata_port->ap;
  3807. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3808. if (ap) {
  3809. scsi_adjust_queue_depth(sdev, 0, IPR_MAX_CMD_PER_ATA_LUN);
  3810. ata_sas_slave_configure(sdev, ap);
  3811. } else
  3812. scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
  3813. if (ioa_cfg->sis64)
  3814. sdev_printk(KERN_INFO, sdev, "Resource path: %s\n",
  3815. ipr_format_resource_path(&res->res_path[0], &buffer[0]));
  3816. return 0;
  3817. }
  3818. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3819. return 0;
  3820. }
  3821. /**
  3822. * ipr_ata_slave_alloc - Prepare for commands to a SATA device
  3823. * @sdev: scsi device struct
  3824. *
  3825. * This function initializes an ATA port so that future commands
  3826. * sent through queuecommand will work.
  3827. *
  3828. * Return value:
  3829. * 0 on success
  3830. **/
  3831. static int ipr_ata_slave_alloc(struct scsi_device *sdev)
  3832. {
  3833. struct ipr_sata_port *sata_port = NULL;
  3834. int rc = -ENXIO;
  3835. ENTER;
  3836. if (sdev->sdev_target)
  3837. sata_port = sdev->sdev_target->hostdata;
  3838. if (sata_port)
  3839. rc = ata_sas_port_init(sata_port->ap);
  3840. if (rc)
  3841. ipr_slave_destroy(sdev);
  3842. LEAVE;
  3843. return rc;
  3844. }
  3845. /**
  3846. * ipr_slave_alloc - Prepare for commands to a device.
  3847. * @sdev: scsi device struct
  3848. *
  3849. * This function saves a pointer to the resource entry
  3850. * in the scsi device struct if the device exists. We
  3851. * can then use this pointer in ipr_queuecommand when
  3852. * handling new commands.
  3853. *
  3854. * Return value:
  3855. * 0 on success / -ENXIO if device does not exist
  3856. **/
  3857. static int ipr_slave_alloc(struct scsi_device *sdev)
  3858. {
  3859. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
  3860. struct ipr_resource_entry *res;
  3861. unsigned long lock_flags;
  3862. int rc = -ENXIO;
  3863. sdev->hostdata = NULL;
  3864. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3865. res = ipr_find_sdev(sdev);
  3866. if (res) {
  3867. res->sdev = sdev;
  3868. res->add_to_ml = 0;
  3869. res->in_erp = 0;
  3870. sdev->hostdata = res;
  3871. if (!ipr_is_naca_model(res))
  3872. res->needs_sync_complete = 1;
  3873. rc = 0;
  3874. if (ipr_is_gata(res)) {
  3875. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3876. return ipr_ata_slave_alloc(sdev);
  3877. }
  3878. }
  3879. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3880. return rc;
  3881. }
  3882. /**
  3883. * ipr_eh_host_reset - Reset the host adapter
  3884. * @scsi_cmd: scsi command struct
  3885. *
  3886. * Return value:
  3887. * SUCCESS / FAILED
  3888. **/
  3889. static int __ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
  3890. {
  3891. struct ipr_ioa_cfg *ioa_cfg;
  3892. int rc;
  3893. ENTER;
  3894. ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
  3895. dev_err(&ioa_cfg->pdev->dev,
  3896. "Adapter being reset as a result of error recovery.\n");
  3897. if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
  3898. ioa_cfg->sdt_state = GET_DUMP;
  3899. rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
  3900. LEAVE;
  3901. return rc;
  3902. }
  3903. static int ipr_eh_host_reset(struct scsi_cmnd * cmd)
  3904. {
  3905. int rc;
  3906. spin_lock_irq(cmd->device->host->host_lock);
  3907. rc = __ipr_eh_host_reset(cmd);
  3908. spin_unlock_irq(cmd->device->host->host_lock);
  3909. return rc;
  3910. }
  3911. /**
  3912. * ipr_device_reset - Reset the device
  3913. * @ioa_cfg: ioa config struct
  3914. * @res: resource entry struct
  3915. *
  3916. * This function issues a device reset to the affected device.
  3917. * If the device is a SCSI device, a LUN reset will be sent
  3918. * to the device first. If that does not work, a target reset
  3919. * will be sent. If the device is a SATA device, a PHY reset will
  3920. * be sent.
  3921. *
  3922. * Return value:
  3923. * 0 on success / non-zero on failure
  3924. **/
  3925. static int ipr_device_reset(struct ipr_ioa_cfg *ioa_cfg,
  3926. struct ipr_resource_entry *res)
  3927. {
  3928. struct ipr_cmnd *ipr_cmd;
  3929. struct ipr_ioarcb *ioarcb;
  3930. struct ipr_cmd_pkt *cmd_pkt;
  3931. struct ipr_ioarcb_ata_regs *regs;
  3932. u32 ioasc;
  3933. ENTER;
  3934. ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  3935. ioarcb = &ipr_cmd->ioarcb;
  3936. cmd_pkt = &ioarcb->cmd_pkt;
  3937. if (ipr_cmd->ioa_cfg->sis64) {
  3938. regs = &ipr_cmd->i.ata_ioadl.regs;
  3939. ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
  3940. } else
  3941. regs = &ioarcb->u.add_data.u.regs;
  3942. ioarcb->res_handle = res->res_handle;
  3943. cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
  3944. cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
  3945. if (ipr_is_gata(res)) {
  3946. cmd_pkt->cdb[2] = IPR_ATA_PHY_RESET;
  3947. ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(regs->flags));
  3948. regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
  3949. }
  3950. ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
  3951. ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  3952. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  3953. if (ipr_is_gata(res) && res->sata_port && ioasc != IPR_IOASC_IOA_WAS_RESET)
  3954. memcpy(&res->sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
  3955. sizeof(struct ipr_ioasa_gata));
  3956. LEAVE;
  3957. return (IPR_IOASC_SENSE_KEY(ioasc) ? -EIO : 0);
  3958. }
  3959. /**
  3960. * ipr_sata_reset - Reset the SATA port
  3961. * @link: SATA link to reset
  3962. * @classes: class of the attached device
  3963. *
  3964. * This function issues a SATA phy reset to the affected ATA link.
  3965. *
  3966. * Return value:
  3967. * 0 on success / non-zero on failure
  3968. **/
  3969. static int ipr_sata_reset(struct ata_link *link, unsigned int *classes,
  3970. unsigned long deadline)
  3971. {
  3972. struct ipr_sata_port *sata_port = link->ap->private_data;
  3973. struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
  3974. struct ipr_resource_entry *res;
  3975. unsigned long lock_flags = 0;
  3976. int rc = -ENXIO;
  3977. ENTER;
  3978. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3979. while(ioa_cfg->in_reset_reload) {
  3980. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3981. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  3982. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  3983. }
  3984. res = sata_port->res;
  3985. if (res) {
  3986. rc = ipr_device_reset(ioa_cfg, res);
  3987. *classes = res->ata_class;
  3988. }
  3989. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  3990. LEAVE;
  3991. return rc;
  3992. }
  3993. /**
  3994. * ipr_eh_dev_reset - Reset the device
  3995. * @scsi_cmd: scsi command struct
  3996. *
  3997. * This function issues a device reset to the affected device.
  3998. * A LUN reset will be sent to the device first. If that does
  3999. * not work, a target reset will be sent.
  4000. *
  4001. * Return value:
  4002. * SUCCESS / FAILED
  4003. **/
  4004. static int __ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
  4005. {
  4006. struct ipr_cmnd *ipr_cmd;
  4007. struct ipr_ioa_cfg *ioa_cfg;
  4008. struct ipr_resource_entry *res;
  4009. struct ata_port *ap;
  4010. int rc = 0;
  4011. ENTER;
  4012. ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
  4013. res = scsi_cmd->device->hostdata;
  4014. if (!res)
  4015. return FAILED;
  4016. /*
  4017. * If we are currently going through reset/reload, return failed. This will force the
  4018. * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
  4019. * reset to complete
  4020. */
  4021. if (ioa_cfg->in_reset_reload)
  4022. return FAILED;
  4023. if (ioa_cfg->ioa_is_dead)
  4024. return FAILED;
  4025. list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
  4026. if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
  4027. if (ipr_cmd->scsi_cmd)
  4028. ipr_cmd->done = ipr_scsi_eh_done;
  4029. if (ipr_cmd->qc)
  4030. ipr_cmd->done = ipr_sata_eh_done;
  4031. if (ipr_cmd->qc && !(ipr_cmd->qc->flags & ATA_QCFLAG_FAILED)) {
  4032. ipr_cmd->qc->err_mask |= AC_ERR_TIMEOUT;
  4033. ipr_cmd->qc->flags |= ATA_QCFLAG_FAILED;
  4034. }
  4035. }
  4036. }
  4037. res->resetting_device = 1;
  4038. scmd_printk(KERN_ERR, scsi_cmd, "Resetting device\n");
  4039. if (ipr_is_gata(res) && res->sata_port) {
  4040. ap = res->sata_port->ap;
  4041. spin_unlock_irq(scsi_cmd->device->host->host_lock);
  4042. ata_std_error_handler(ap);
  4043. spin_lock_irq(scsi_cmd->device->host->host_lock);
  4044. list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
  4045. if (ipr_cmd->ioarcb.res_handle == res->res_handle) {
  4046. rc = -EIO;
  4047. break;
  4048. }
  4049. }
  4050. } else
  4051. rc = ipr_device_reset(ioa_cfg, res);
  4052. res->resetting_device = 0;
  4053. LEAVE;
  4054. return (rc ? FAILED : SUCCESS);
  4055. }
  4056. static int ipr_eh_dev_reset(struct scsi_cmnd * cmd)
  4057. {
  4058. int rc;
  4059. spin_lock_irq(cmd->device->host->host_lock);
  4060. rc = __ipr_eh_dev_reset(cmd);
  4061. spin_unlock_irq(cmd->device->host->host_lock);
  4062. return rc;
  4063. }
  4064. /**
  4065. * ipr_bus_reset_done - Op done function for bus reset.
  4066. * @ipr_cmd: ipr command struct
  4067. *
  4068. * This function is the op done function for a bus reset
  4069. *
  4070. * Return value:
  4071. * none
  4072. **/
  4073. static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
  4074. {
  4075. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  4076. struct ipr_resource_entry *res;
  4077. ENTER;
  4078. if (!ioa_cfg->sis64)
  4079. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  4080. if (res->res_handle == ipr_cmd->ioarcb.res_handle) {
  4081. scsi_report_bus_reset(ioa_cfg->host, res->bus);
  4082. break;
  4083. }
  4084. }
  4085. /*
  4086. * If abort has not completed, indicate the reset has, else call the
  4087. * abort's done function to wake the sleeping eh thread
  4088. */
  4089. if (ipr_cmd->sibling->sibling)
  4090. ipr_cmd->sibling->sibling = NULL;
  4091. else
  4092. ipr_cmd->sibling->done(ipr_cmd->sibling);
  4093. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  4094. LEAVE;
  4095. }
  4096. /**
  4097. * ipr_abort_timeout - An abort task has timed out
  4098. * @ipr_cmd: ipr command struct
  4099. *
  4100. * This function handles when an abort task times out. If this
  4101. * happens we issue a bus reset since we have resources tied
  4102. * up that must be freed before returning to the midlayer.
  4103. *
  4104. * Return value:
  4105. * none
  4106. **/
  4107. static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
  4108. {
  4109. struct ipr_cmnd *reset_cmd;
  4110. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  4111. struct ipr_cmd_pkt *cmd_pkt;
  4112. unsigned long lock_flags = 0;
  4113. ENTER;
  4114. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  4115. if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
  4116. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4117. return;
  4118. }
  4119. sdev_printk(KERN_ERR, ipr_cmd->u.sdev, "Abort timed out. Resetting bus.\n");
  4120. reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  4121. ipr_cmd->sibling = reset_cmd;
  4122. reset_cmd->sibling = ipr_cmd;
  4123. reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
  4124. cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
  4125. cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
  4126. cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
  4127. cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
  4128. ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
  4129. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4130. LEAVE;
  4131. }
  4132. /**
  4133. * ipr_cancel_op - Cancel specified op
  4134. * @scsi_cmd: scsi command struct
  4135. *
  4136. * This function cancels specified op.
  4137. *
  4138. * Return value:
  4139. * SUCCESS / FAILED
  4140. **/
  4141. static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
  4142. {
  4143. struct ipr_cmnd *ipr_cmd;
  4144. struct ipr_ioa_cfg *ioa_cfg;
  4145. struct ipr_resource_entry *res;
  4146. struct ipr_cmd_pkt *cmd_pkt;
  4147. u32 ioasc;
  4148. int op_found = 0;
  4149. ENTER;
  4150. ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
  4151. res = scsi_cmd->device->hostdata;
  4152. /* If we are currently going through reset/reload, return failed.
  4153. * This will force the mid-layer to call ipr_eh_host_reset,
  4154. * which will then go to sleep and wait for the reset to complete
  4155. */
  4156. if (ioa_cfg->in_reset_reload || ioa_cfg->ioa_is_dead)
  4157. return FAILED;
  4158. if (!res || !ipr_is_gscsi(res))
  4159. return FAILED;
  4160. list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
  4161. if (ipr_cmd->scsi_cmd == scsi_cmd) {
  4162. ipr_cmd->done = ipr_scsi_eh_done;
  4163. op_found = 1;
  4164. break;
  4165. }
  4166. }
  4167. if (!op_found)
  4168. return SUCCESS;
  4169. ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  4170. ipr_cmd->ioarcb.res_handle = res->res_handle;
  4171. cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
  4172. cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
  4173. cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
  4174. ipr_cmd->u.sdev = scsi_cmd->device;
  4175. scmd_printk(KERN_ERR, scsi_cmd, "Aborting command: %02X\n",
  4176. scsi_cmd->cmnd[0]);
  4177. ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
  4178. ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  4179. /*
  4180. * If the abort task timed out and we sent a bus reset, we will get
  4181. * one the following responses to the abort
  4182. */
  4183. if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
  4184. ioasc = 0;
  4185. ipr_trace;
  4186. }
  4187. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  4188. if (!ipr_is_naca_model(res))
  4189. res->needs_sync_complete = 1;
  4190. LEAVE;
  4191. return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
  4192. }
  4193. /**
  4194. * ipr_eh_abort - Abort a single op
  4195. * @scsi_cmd: scsi command struct
  4196. *
  4197. * Return value:
  4198. * SUCCESS / FAILED
  4199. **/
  4200. static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
  4201. {
  4202. unsigned long flags;
  4203. int rc;
  4204. ENTER;
  4205. spin_lock_irqsave(scsi_cmd->device->host->host_lock, flags);
  4206. rc = ipr_cancel_op(scsi_cmd);
  4207. spin_unlock_irqrestore(scsi_cmd->device->host->host_lock, flags);
  4208. LEAVE;
  4209. return rc;
  4210. }
  4211. /**
  4212. * ipr_handle_other_interrupt - Handle "other" interrupts
  4213. * @ioa_cfg: ioa config struct
  4214. * @int_reg: interrupt register
  4215. *
  4216. * Return value:
  4217. * IRQ_NONE / IRQ_HANDLED
  4218. **/
  4219. static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
  4220. volatile u32 int_reg)
  4221. {
  4222. irqreturn_t rc = IRQ_HANDLED;
  4223. if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
  4224. /* Mask the interrupt */
  4225. writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
  4226. /* Clear the interrupt */
  4227. writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
  4228. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
  4229. list_del(&ioa_cfg->reset_cmd->queue);
  4230. del_timer(&ioa_cfg->reset_cmd->timer);
  4231. ipr_reset_ioa_job(ioa_cfg->reset_cmd);
  4232. } else {
  4233. if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
  4234. ioa_cfg->ioa_unit_checked = 1;
  4235. else
  4236. dev_err(&ioa_cfg->pdev->dev,
  4237. "Permanent IOA failure. 0x%08X\n", int_reg);
  4238. if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
  4239. ioa_cfg->sdt_state = GET_DUMP;
  4240. ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
  4241. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  4242. }
  4243. return rc;
  4244. }
  4245. /**
  4246. * ipr_isr_eh - Interrupt service routine error handler
  4247. * @ioa_cfg: ioa config struct
  4248. * @msg: message to log
  4249. *
  4250. * Return value:
  4251. * none
  4252. **/
  4253. static void ipr_isr_eh(struct ipr_ioa_cfg *ioa_cfg, char *msg)
  4254. {
  4255. ioa_cfg->errors_logged++;
  4256. dev_err(&ioa_cfg->pdev->dev, "%s\n", msg);
  4257. if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
  4258. ioa_cfg->sdt_state = GET_DUMP;
  4259. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  4260. }
  4261. /**
  4262. * ipr_isr - Interrupt service routine
  4263. * @irq: irq number
  4264. * @devp: pointer to ioa config struct
  4265. *
  4266. * Return value:
  4267. * IRQ_NONE / IRQ_HANDLED
  4268. **/
  4269. static irqreturn_t ipr_isr(int irq, void *devp)
  4270. {
  4271. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
  4272. unsigned long lock_flags = 0;
  4273. volatile u32 int_reg, int_mask_reg;
  4274. u32 ioasc;
  4275. u16 cmd_index;
  4276. int num_hrrq = 0;
  4277. struct ipr_cmnd *ipr_cmd;
  4278. irqreturn_t rc = IRQ_NONE;
  4279. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  4280. /* If interrupts are disabled, ignore the interrupt */
  4281. if (!ioa_cfg->allow_interrupts) {
  4282. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4283. return IRQ_NONE;
  4284. }
  4285. int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
  4286. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32) & ~int_mask_reg;
  4287. /* If an interrupt on the adapter did not occur, ignore it.
  4288. * Or in the case of SIS 64, check for a stage change interrupt.
  4289. */
  4290. if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
  4291. if (ioa_cfg->sis64) {
  4292. int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
  4293. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
  4294. if (int_reg & IPR_PCII_IPL_STAGE_CHANGE) {
  4295. /* clear stage change */
  4296. writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_reg);
  4297. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
  4298. list_del(&ioa_cfg->reset_cmd->queue);
  4299. del_timer(&ioa_cfg->reset_cmd->timer);
  4300. ipr_reset_ioa_job(ioa_cfg->reset_cmd);
  4301. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4302. return IRQ_HANDLED;
  4303. }
  4304. }
  4305. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4306. return IRQ_NONE;
  4307. }
  4308. while (1) {
  4309. ipr_cmd = NULL;
  4310. while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
  4311. ioa_cfg->toggle_bit) {
  4312. cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
  4313. IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
  4314. if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
  4315. ipr_isr_eh(ioa_cfg, "Invalid response handle from IOA");
  4316. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4317. return IRQ_HANDLED;
  4318. }
  4319. ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
  4320. ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  4321. ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
  4322. list_del(&ipr_cmd->queue);
  4323. del_timer(&ipr_cmd->timer);
  4324. ipr_cmd->done(ipr_cmd);
  4325. rc = IRQ_HANDLED;
  4326. if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
  4327. ioa_cfg->hrrq_curr++;
  4328. } else {
  4329. ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
  4330. ioa_cfg->toggle_bit ^= 1u;
  4331. }
  4332. }
  4333. if (ipr_cmd != NULL) {
  4334. /* Clear the PCI interrupt */
  4335. do {
  4336. writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg32);
  4337. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg32) & ~int_mask_reg;
  4338. } while (int_reg & IPR_PCII_HRRQ_UPDATED &&
  4339. num_hrrq++ < IPR_MAX_HRRQ_RETRIES);
  4340. if (int_reg & IPR_PCII_HRRQ_UPDATED) {
  4341. ipr_isr_eh(ioa_cfg, "Error clearing HRRQ");
  4342. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4343. return IRQ_HANDLED;
  4344. }
  4345. } else
  4346. break;
  4347. }
  4348. if (unlikely(rc == IRQ_NONE))
  4349. rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
  4350. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  4351. return rc;
  4352. }
  4353. /**
  4354. * ipr_build_ioadl64 - Build a scatter/gather list and map the buffer
  4355. * @ioa_cfg: ioa config struct
  4356. * @ipr_cmd: ipr command struct
  4357. *
  4358. * Return value:
  4359. * 0 on success / -1 on failure
  4360. **/
  4361. static int ipr_build_ioadl64(struct ipr_ioa_cfg *ioa_cfg,
  4362. struct ipr_cmnd *ipr_cmd)
  4363. {
  4364. int i, nseg;
  4365. struct scatterlist *sg;
  4366. u32 length;
  4367. u32 ioadl_flags = 0;
  4368. struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
  4369. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  4370. struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
  4371. length = scsi_bufflen(scsi_cmd);
  4372. if (!length)
  4373. return 0;
  4374. nseg = scsi_dma_map(scsi_cmd);
  4375. if (nseg < 0) {
  4376. dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
  4377. return -1;
  4378. }
  4379. ipr_cmd->dma_use_sg = nseg;
  4380. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
  4381. ioadl_flags = IPR_IOADL_FLAGS_WRITE;
  4382. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  4383. } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE)
  4384. ioadl_flags = IPR_IOADL_FLAGS_READ;
  4385. scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
  4386. ioadl64[i].flags = cpu_to_be32(ioadl_flags);
  4387. ioadl64[i].data_len = cpu_to_be32(sg_dma_len(sg));
  4388. ioadl64[i].address = cpu_to_be64(sg_dma_address(sg));
  4389. }
  4390. ioadl64[i-1].flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
  4391. return 0;
  4392. }
  4393. /**
  4394. * ipr_build_ioadl - Build a scatter/gather list and map the buffer
  4395. * @ioa_cfg: ioa config struct
  4396. * @ipr_cmd: ipr command struct
  4397. *
  4398. * Return value:
  4399. * 0 on success / -1 on failure
  4400. **/
  4401. static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
  4402. struct ipr_cmnd *ipr_cmd)
  4403. {
  4404. int i, nseg;
  4405. struct scatterlist *sg;
  4406. u32 length;
  4407. u32 ioadl_flags = 0;
  4408. struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
  4409. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  4410. struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
  4411. length = scsi_bufflen(scsi_cmd);
  4412. if (!length)
  4413. return 0;
  4414. nseg = scsi_dma_map(scsi_cmd);
  4415. if (nseg < 0) {
  4416. dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
  4417. return -1;
  4418. }
  4419. ipr_cmd->dma_use_sg = nseg;
  4420. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
  4421. ioadl_flags = IPR_IOADL_FLAGS_WRITE;
  4422. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  4423. ioarcb->data_transfer_length = cpu_to_be32(length);
  4424. ioarcb->ioadl_len =
  4425. cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
  4426. } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  4427. ioadl_flags = IPR_IOADL_FLAGS_READ;
  4428. ioarcb->read_data_transfer_length = cpu_to_be32(length);
  4429. ioarcb->read_ioadl_len =
  4430. cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
  4431. }
  4432. if (ipr_cmd->dma_use_sg <= ARRAY_SIZE(ioarcb->u.add_data.u.ioadl)) {
  4433. ioadl = ioarcb->u.add_data.u.ioadl;
  4434. ioarcb->write_ioadl_addr = cpu_to_be32((ipr_cmd->dma_addr) +
  4435. offsetof(struct ipr_ioarcb, u.add_data));
  4436. ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
  4437. }
  4438. scsi_for_each_sg(scsi_cmd, sg, ipr_cmd->dma_use_sg, i) {
  4439. ioadl[i].flags_and_data_len =
  4440. cpu_to_be32(ioadl_flags | sg_dma_len(sg));
  4441. ioadl[i].address = cpu_to_be32(sg_dma_address(sg));
  4442. }
  4443. ioadl[i-1].flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
  4444. return 0;
  4445. }
  4446. /**
  4447. * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
  4448. * @scsi_cmd: scsi command struct
  4449. *
  4450. * Return value:
  4451. * task attributes
  4452. **/
  4453. static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
  4454. {
  4455. u8 tag[2];
  4456. u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
  4457. if (scsi_populate_tag_msg(scsi_cmd, tag)) {
  4458. switch (tag[0]) {
  4459. case MSG_SIMPLE_TAG:
  4460. rc = IPR_FLAGS_LO_SIMPLE_TASK;
  4461. break;
  4462. case MSG_HEAD_TAG:
  4463. rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
  4464. break;
  4465. case MSG_ORDERED_TAG:
  4466. rc = IPR_FLAGS_LO_ORDERED_TASK;
  4467. break;
  4468. };
  4469. }
  4470. return rc;
  4471. }
  4472. /**
  4473. * ipr_erp_done - Process completion of ERP for a device
  4474. * @ipr_cmd: ipr command struct
  4475. *
  4476. * This function copies the sense buffer into the scsi_cmd
  4477. * struct and pushes the scsi_done function.
  4478. *
  4479. * Return value:
  4480. * nothing
  4481. **/
  4482. static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
  4483. {
  4484. struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
  4485. struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
  4486. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  4487. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  4488. if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
  4489. scsi_cmd->result |= (DID_ERROR << 16);
  4490. scmd_printk(KERN_ERR, scsi_cmd,
  4491. "Request Sense failed with IOASC: 0x%08X\n", ioasc);
  4492. } else {
  4493. memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
  4494. SCSI_SENSE_BUFFERSIZE);
  4495. }
  4496. if (res) {
  4497. if (!ipr_is_naca_model(res))
  4498. res->needs_sync_complete = 1;
  4499. res->in_erp = 0;
  4500. }
  4501. scsi_dma_unmap(ipr_cmd->scsi_cmd);
  4502. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  4503. scsi_cmd->scsi_done(scsi_cmd);
  4504. }
  4505. /**
  4506. * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
  4507. * @ipr_cmd: ipr command struct
  4508. *
  4509. * Return value:
  4510. * none
  4511. **/
  4512. static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
  4513. {
  4514. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  4515. struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
  4516. dma_addr_t dma_addr = ipr_cmd->dma_addr;
  4517. memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
  4518. ioarcb->data_transfer_length = 0;
  4519. ioarcb->read_data_transfer_length = 0;
  4520. ioarcb->ioadl_len = 0;
  4521. ioarcb->read_ioadl_len = 0;
  4522. ioasa->ioasc = 0;
  4523. ioasa->residual_data_len = 0;
  4524. if (ipr_cmd->ioa_cfg->sis64)
  4525. ioarcb->u.sis64_addr_data.data_ioadl_addr =
  4526. cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
  4527. else {
  4528. ioarcb->write_ioadl_addr =
  4529. cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
  4530. ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
  4531. }
  4532. }
  4533. /**
  4534. * ipr_erp_request_sense - Send request sense to a device
  4535. * @ipr_cmd: ipr command struct
  4536. *
  4537. * This function sends a request sense to a device as a result
  4538. * of a check condition.
  4539. *
  4540. * Return value:
  4541. * nothing
  4542. **/
  4543. static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
  4544. {
  4545. struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
  4546. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  4547. if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
  4548. ipr_erp_done(ipr_cmd);
  4549. return;
  4550. }
  4551. ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
  4552. cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
  4553. cmd_pkt->cdb[0] = REQUEST_SENSE;
  4554. cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  4555. cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
  4556. cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
  4557. cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
  4558. ipr_init_ioadl(ipr_cmd, ipr_cmd->sense_buffer_dma,
  4559. SCSI_SENSE_BUFFERSIZE, IPR_IOADL_FLAGS_READ_LAST);
  4560. ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
  4561. IPR_REQUEST_SENSE_TIMEOUT * 2);
  4562. }
  4563. /**
  4564. * ipr_erp_cancel_all - Send cancel all to a device
  4565. * @ipr_cmd: ipr command struct
  4566. *
  4567. * This function sends a cancel all to a device to clear the
  4568. * queue. If we are running TCQ on the device, QERR is set to 1,
  4569. * which means all outstanding ops have been dropped on the floor.
  4570. * Cancel all will return them to us.
  4571. *
  4572. * Return value:
  4573. * nothing
  4574. **/
  4575. static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
  4576. {
  4577. struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
  4578. struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
  4579. struct ipr_cmd_pkt *cmd_pkt;
  4580. res->in_erp = 1;
  4581. ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
  4582. if (!scsi_get_tag_type(scsi_cmd->device)) {
  4583. ipr_erp_request_sense(ipr_cmd);
  4584. return;
  4585. }
  4586. cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
  4587. cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
  4588. cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
  4589. ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
  4590. IPR_CANCEL_ALL_TIMEOUT);
  4591. }
  4592. /**
  4593. * ipr_dump_ioasa - Dump contents of IOASA
  4594. * @ioa_cfg: ioa config struct
  4595. * @ipr_cmd: ipr command struct
  4596. * @res: resource entry struct
  4597. *
  4598. * This function is invoked by the interrupt handler when ops
  4599. * fail. It will log the IOASA if appropriate. Only called
  4600. * for GPDD ops.
  4601. *
  4602. * Return value:
  4603. * none
  4604. **/
  4605. static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
  4606. struct ipr_cmnd *ipr_cmd, struct ipr_resource_entry *res)
  4607. {
  4608. int i;
  4609. u16 data_len;
  4610. u32 ioasc, fd_ioasc;
  4611. struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
  4612. __be32 *ioasa_data = (__be32 *)ioasa;
  4613. int error_index;
  4614. ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
  4615. fd_ioasc = be32_to_cpu(ioasa->fd_ioasc) & IPR_IOASC_IOASC_MASK;
  4616. if (0 == ioasc)
  4617. return;
  4618. if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
  4619. return;
  4620. if (ioasc == IPR_IOASC_BUS_WAS_RESET && fd_ioasc)
  4621. error_index = ipr_get_error(fd_ioasc);
  4622. else
  4623. error_index = ipr_get_error(ioasc);
  4624. if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
  4625. /* Don't log an error if the IOA already logged one */
  4626. if (ioasa->ilid != 0)
  4627. return;
  4628. if (!ipr_is_gscsi(res))
  4629. return;
  4630. if (ipr_error_table[error_index].log_ioasa == 0)
  4631. return;
  4632. }
  4633. ipr_res_err(ioa_cfg, res, "%s\n", ipr_error_table[error_index].error);
  4634. if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
  4635. data_len = sizeof(struct ipr_ioasa);
  4636. else
  4637. data_len = be16_to_cpu(ioasa->ret_stat_len);
  4638. ipr_err("IOASA Dump:\n");
  4639. for (i = 0; i < data_len / 4; i += 4) {
  4640. ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
  4641. be32_to_cpu(ioasa_data[i]),
  4642. be32_to_cpu(ioasa_data[i+1]),
  4643. be32_to_cpu(ioasa_data[i+2]),
  4644. be32_to_cpu(ioasa_data[i+3]));
  4645. }
  4646. }
  4647. /**
  4648. * ipr_gen_sense - Generate SCSI sense data from an IOASA
  4649. * @ioasa: IOASA
  4650. * @sense_buf: sense data buffer
  4651. *
  4652. * Return value:
  4653. * none
  4654. **/
  4655. static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
  4656. {
  4657. u32 failing_lba;
  4658. u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
  4659. struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
  4660. struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
  4661. u32 ioasc = be32_to_cpu(ioasa->ioasc);
  4662. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  4663. if (ioasc >= IPR_FIRST_DRIVER_IOASC)
  4664. return;
  4665. ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
  4666. if (ipr_is_vset_device(res) &&
  4667. ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
  4668. ioasa->u.vset.failing_lba_hi != 0) {
  4669. sense_buf[0] = 0x72;
  4670. sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
  4671. sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
  4672. sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
  4673. sense_buf[7] = 12;
  4674. sense_buf[8] = 0;
  4675. sense_buf[9] = 0x0A;
  4676. sense_buf[10] = 0x80;
  4677. failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
  4678. sense_buf[12] = (failing_lba & 0xff000000) >> 24;
  4679. sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
  4680. sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
  4681. sense_buf[15] = failing_lba & 0x000000ff;
  4682. failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
  4683. sense_buf[16] = (failing_lba & 0xff000000) >> 24;
  4684. sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
  4685. sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
  4686. sense_buf[19] = failing_lba & 0x000000ff;
  4687. } else {
  4688. sense_buf[0] = 0x70;
  4689. sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
  4690. sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
  4691. sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
  4692. /* Illegal request */
  4693. if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
  4694. (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
  4695. sense_buf[7] = 10; /* additional length */
  4696. /* IOARCB was in error */
  4697. if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
  4698. sense_buf[15] = 0xC0;
  4699. else /* Parameter data was invalid */
  4700. sense_buf[15] = 0x80;
  4701. sense_buf[16] =
  4702. ((IPR_FIELD_POINTER_MASK &
  4703. be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
  4704. sense_buf[17] =
  4705. (IPR_FIELD_POINTER_MASK &
  4706. be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
  4707. } else {
  4708. if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
  4709. if (ipr_is_vset_device(res))
  4710. failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
  4711. else
  4712. failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
  4713. sense_buf[0] |= 0x80; /* Or in the Valid bit */
  4714. sense_buf[3] = (failing_lba & 0xff000000) >> 24;
  4715. sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
  4716. sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
  4717. sense_buf[6] = failing_lba & 0x000000ff;
  4718. }
  4719. sense_buf[7] = 6; /* additional length */
  4720. }
  4721. }
  4722. }
  4723. /**
  4724. * ipr_get_autosense - Copy autosense data to sense buffer
  4725. * @ipr_cmd: ipr command struct
  4726. *
  4727. * This function copies the autosense buffer to the buffer
  4728. * in the scsi_cmd, if there is autosense available.
  4729. *
  4730. * Return value:
  4731. * 1 if autosense was available / 0 if not
  4732. **/
  4733. static int ipr_get_autosense(struct ipr_cmnd *ipr_cmd)
  4734. {
  4735. struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
  4736. if ((be32_to_cpu(ioasa->ioasc_specific) & IPR_AUTOSENSE_VALID) == 0)
  4737. return 0;
  4738. memcpy(ipr_cmd->scsi_cmd->sense_buffer, ioasa->auto_sense.data,
  4739. min_t(u16, be16_to_cpu(ioasa->auto_sense.auto_sense_len),
  4740. SCSI_SENSE_BUFFERSIZE));
  4741. return 1;
  4742. }
  4743. /**
  4744. * ipr_erp_start - Process an error response for a SCSI op
  4745. * @ioa_cfg: ioa config struct
  4746. * @ipr_cmd: ipr command struct
  4747. *
  4748. * This function determines whether or not to initiate ERP
  4749. * on the affected device.
  4750. *
  4751. * Return value:
  4752. * nothing
  4753. **/
  4754. static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
  4755. struct ipr_cmnd *ipr_cmd)
  4756. {
  4757. struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
  4758. struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
  4759. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  4760. u32 masked_ioasc = ioasc & IPR_IOASC_IOASC_MASK;
  4761. if (!res) {
  4762. ipr_scsi_eh_done(ipr_cmd);
  4763. return;
  4764. }
  4765. if (!ipr_is_gscsi(res) && masked_ioasc != IPR_IOASC_HW_DEV_BUS_STATUS)
  4766. ipr_gen_sense(ipr_cmd);
  4767. ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
  4768. switch (masked_ioasc) {
  4769. case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
  4770. if (ipr_is_naca_model(res))
  4771. scsi_cmd->result |= (DID_ABORT << 16);
  4772. else
  4773. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  4774. break;
  4775. case IPR_IOASC_IR_RESOURCE_HANDLE:
  4776. case IPR_IOASC_IR_NO_CMDS_TO_2ND_IOA:
  4777. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  4778. break;
  4779. case IPR_IOASC_HW_SEL_TIMEOUT:
  4780. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  4781. if (!ipr_is_naca_model(res))
  4782. res->needs_sync_complete = 1;
  4783. break;
  4784. case IPR_IOASC_SYNC_REQUIRED:
  4785. if (!res->in_erp)
  4786. res->needs_sync_complete = 1;
  4787. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  4788. break;
  4789. case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
  4790. case IPR_IOASA_IR_DUAL_IOA_DISABLED:
  4791. scsi_cmd->result |= (DID_PASSTHROUGH << 16);
  4792. break;
  4793. case IPR_IOASC_BUS_WAS_RESET:
  4794. case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
  4795. /*
  4796. * Report the bus reset and ask for a retry. The device
  4797. * will give CC/UA the next command.
  4798. */
  4799. if (!res->resetting_device)
  4800. scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
  4801. scsi_cmd->result |= (DID_ERROR << 16);
  4802. if (!ipr_is_naca_model(res))
  4803. res->needs_sync_complete = 1;
  4804. break;
  4805. case IPR_IOASC_HW_DEV_BUS_STATUS:
  4806. scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
  4807. if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
  4808. if (!ipr_get_autosense(ipr_cmd)) {
  4809. if (!ipr_is_naca_model(res)) {
  4810. ipr_erp_cancel_all(ipr_cmd);
  4811. return;
  4812. }
  4813. }
  4814. }
  4815. if (!ipr_is_naca_model(res))
  4816. res->needs_sync_complete = 1;
  4817. break;
  4818. case IPR_IOASC_NR_INIT_CMD_REQUIRED:
  4819. break;
  4820. default:
  4821. if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  4822. scsi_cmd->result |= (DID_ERROR << 16);
  4823. if (!ipr_is_vset_device(res) && !ipr_is_naca_model(res))
  4824. res->needs_sync_complete = 1;
  4825. break;
  4826. }
  4827. scsi_dma_unmap(ipr_cmd->scsi_cmd);
  4828. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  4829. scsi_cmd->scsi_done(scsi_cmd);
  4830. }
  4831. /**
  4832. * ipr_scsi_done - mid-layer done function
  4833. * @ipr_cmd: ipr command struct
  4834. *
  4835. * This function is invoked by the interrupt handler for
  4836. * ops generated by the SCSI mid-layer
  4837. *
  4838. * Return value:
  4839. * none
  4840. **/
  4841. static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
  4842. {
  4843. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  4844. struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
  4845. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  4846. scsi_set_resid(scsi_cmd, be32_to_cpu(ipr_cmd->ioasa.residual_data_len));
  4847. if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
  4848. scsi_dma_unmap(ipr_cmd->scsi_cmd);
  4849. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  4850. scsi_cmd->scsi_done(scsi_cmd);
  4851. } else
  4852. ipr_erp_start(ioa_cfg, ipr_cmd);
  4853. }
  4854. /**
  4855. * ipr_queuecommand - Queue a mid-layer request
  4856. * @scsi_cmd: scsi command struct
  4857. * @done: done function
  4858. *
  4859. * This function queues a request generated by the mid-layer.
  4860. *
  4861. * Return value:
  4862. * 0 on success
  4863. * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
  4864. * SCSI_MLQUEUE_HOST_BUSY if host is busy
  4865. **/
  4866. static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
  4867. void (*done) (struct scsi_cmnd *))
  4868. {
  4869. struct ipr_ioa_cfg *ioa_cfg;
  4870. struct ipr_resource_entry *res;
  4871. struct ipr_ioarcb *ioarcb;
  4872. struct ipr_cmnd *ipr_cmd;
  4873. int rc = 0;
  4874. scsi_cmd->scsi_done = done;
  4875. ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
  4876. res = scsi_cmd->device->hostdata;
  4877. scsi_cmd->result = (DID_OK << 16);
  4878. /*
  4879. * We are currently blocking all devices due to a host reset
  4880. * We have told the host to stop giving us new requests, but
  4881. * ERP ops don't count. FIXME
  4882. */
  4883. if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
  4884. return SCSI_MLQUEUE_HOST_BUSY;
  4885. /*
  4886. * FIXME - Create scsi_set_host_offline interface
  4887. * and the ioa_is_dead check can be removed
  4888. */
  4889. if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
  4890. memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  4891. scsi_cmd->result = (DID_NO_CONNECT << 16);
  4892. scsi_cmd->scsi_done(scsi_cmd);
  4893. return 0;
  4894. }
  4895. if (ipr_is_gata(res) && res->sata_port)
  4896. return ata_sas_queuecmd(scsi_cmd, done, res->sata_port->ap);
  4897. ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  4898. ioarcb = &ipr_cmd->ioarcb;
  4899. list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
  4900. memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
  4901. ipr_cmd->scsi_cmd = scsi_cmd;
  4902. ioarcb->res_handle = res->res_handle;
  4903. ipr_cmd->done = ipr_scsi_done;
  4904. ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
  4905. if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
  4906. if (scsi_cmd->underflow == 0)
  4907. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
  4908. if (res->needs_sync_complete) {
  4909. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
  4910. res->needs_sync_complete = 0;
  4911. }
  4912. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
  4913. ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
  4914. ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
  4915. ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
  4916. }
  4917. if (scsi_cmd->cmnd[0] >= 0xC0 &&
  4918. (!ipr_is_gscsi(res) || scsi_cmd->cmnd[0] == IPR_QUERY_RSRC_STATE))
  4919. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
  4920. if (likely(rc == 0)) {
  4921. if (ioa_cfg->sis64)
  4922. rc = ipr_build_ioadl64(ioa_cfg, ipr_cmd);
  4923. else
  4924. rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
  4925. }
  4926. if (likely(rc == 0)) {
  4927. mb();
  4928. ipr_send_command(ipr_cmd);
  4929. } else {
  4930. list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  4931. return SCSI_MLQUEUE_HOST_BUSY;
  4932. }
  4933. return 0;
  4934. }
  4935. /**
  4936. * ipr_ioctl - IOCTL handler
  4937. * @sdev: scsi device struct
  4938. * @cmd: IOCTL cmd
  4939. * @arg: IOCTL arg
  4940. *
  4941. * Return value:
  4942. * 0 on success / other on failure
  4943. **/
  4944. static int ipr_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  4945. {
  4946. struct ipr_resource_entry *res;
  4947. res = (struct ipr_resource_entry *)sdev->hostdata;
  4948. if (res && ipr_is_gata(res)) {
  4949. if (cmd == HDIO_GET_IDENTITY)
  4950. return -ENOTTY;
  4951. return ata_sas_scsi_ioctl(res->sata_port->ap, sdev, cmd, arg);
  4952. }
  4953. return -EINVAL;
  4954. }
  4955. /**
  4956. * ipr_info - Get information about the card/driver
  4957. * @scsi_host: scsi host struct
  4958. *
  4959. * Return value:
  4960. * pointer to buffer with description string
  4961. **/
  4962. static const char * ipr_ioa_info(struct Scsi_Host *host)
  4963. {
  4964. static char buffer[512];
  4965. struct ipr_ioa_cfg *ioa_cfg;
  4966. unsigned long lock_flags = 0;
  4967. ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
  4968. spin_lock_irqsave(host->host_lock, lock_flags);
  4969. sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
  4970. spin_unlock_irqrestore(host->host_lock, lock_flags);
  4971. return buffer;
  4972. }
  4973. static struct scsi_host_template driver_template = {
  4974. .module = THIS_MODULE,
  4975. .name = "IPR",
  4976. .info = ipr_ioa_info,
  4977. .ioctl = ipr_ioctl,
  4978. .queuecommand = ipr_queuecommand,
  4979. .eh_abort_handler = ipr_eh_abort,
  4980. .eh_device_reset_handler = ipr_eh_dev_reset,
  4981. .eh_host_reset_handler = ipr_eh_host_reset,
  4982. .slave_alloc = ipr_slave_alloc,
  4983. .slave_configure = ipr_slave_configure,
  4984. .slave_destroy = ipr_slave_destroy,
  4985. .target_alloc = ipr_target_alloc,
  4986. .target_destroy = ipr_target_destroy,
  4987. .change_queue_depth = ipr_change_queue_depth,
  4988. .change_queue_type = ipr_change_queue_type,
  4989. .bios_param = ipr_biosparam,
  4990. .can_queue = IPR_MAX_COMMANDS,
  4991. .this_id = -1,
  4992. .sg_tablesize = IPR_MAX_SGLIST,
  4993. .max_sectors = IPR_IOA_MAX_SECTORS,
  4994. .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
  4995. .use_clustering = ENABLE_CLUSTERING,
  4996. .shost_attrs = ipr_ioa_attrs,
  4997. .sdev_attrs = ipr_dev_attrs,
  4998. .proc_name = IPR_NAME
  4999. };
  5000. /**
  5001. * ipr_ata_phy_reset - libata phy_reset handler
  5002. * @ap: ata port to reset
  5003. *
  5004. **/
  5005. static void ipr_ata_phy_reset(struct ata_port *ap)
  5006. {
  5007. unsigned long flags;
  5008. struct ipr_sata_port *sata_port = ap->private_data;
  5009. struct ipr_resource_entry *res = sata_port->res;
  5010. struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
  5011. int rc;
  5012. ENTER;
  5013. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  5014. while(ioa_cfg->in_reset_reload) {
  5015. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  5016. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  5017. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  5018. }
  5019. if (!ioa_cfg->allow_cmds)
  5020. goto out_unlock;
  5021. rc = ipr_device_reset(ioa_cfg, res);
  5022. if (rc) {
  5023. ata_port_disable(ap);
  5024. goto out_unlock;
  5025. }
  5026. ap->link.device[0].class = res->ata_class;
  5027. if (ap->link.device[0].class == ATA_DEV_UNKNOWN)
  5028. ata_port_disable(ap);
  5029. out_unlock:
  5030. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  5031. LEAVE;
  5032. }
  5033. /**
  5034. * ipr_ata_post_internal - Cleanup after an internal command
  5035. * @qc: ATA queued command
  5036. *
  5037. * Return value:
  5038. * none
  5039. **/
  5040. static void ipr_ata_post_internal(struct ata_queued_cmd *qc)
  5041. {
  5042. struct ipr_sata_port *sata_port = qc->ap->private_data;
  5043. struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
  5044. struct ipr_cmnd *ipr_cmd;
  5045. unsigned long flags;
  5046. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  5047. while(ioa_cfg->in_reset_reload) {
  5048. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  5049. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  5050. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  5051. }
  5052. list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
  5053. if (ipr_cmd->qc == qc) {
  5054. ipr_device_reset(ioa_cfg, sata_port->res);
  5055. break;
  5056. }
  5057. }
  5058. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  5059. }
  5060. /**
  5061. * ipr_copy_sata_tf - Copy a SATA taskfile to an IOA data structure
  5062. * @regs: destination
  5063. * @tf: source ATA taskfile
  5064. *
  5065. * Return value:
  5066. * none
  5067. **/
  5068. static void ipr_copy_sata_tf(struct ipr_ioarcb_ata_regs *regs,
  5069. struct ata_taskfile *tf)
  5070. {
  5071. regs->feature = tf->feature;
  5072. regs->nsect = tf->nsect;
  5073. regs->lbal = tf->lbal;
  5074. regs->lbam = tf->lbam;
  5075. regs->lbah = tf->lbah;
  5076. regs->device = tf->device;
  5077. regs->command = tf->command;
  5078. regs->hob_feature = tf->hob_feature;
  5079. regs->hob_nsect = tf->hob_nsect;
  5080. regs->hob_lbal = tf->hob_lbal;
  5081. regs->hob_lbam = tf->hob_lbam;
  5082. regs->hob_lbah = tf->hob_lbah;
  5083. regs->ctl = tf->ctl;
  5084. }
  5085. /**
  5086. * ipr_sata_done - done function for SATA commands
  5087. * @ipr_cmd: ipr command struct
  5088. *
  5089. * This function is invoked by the interrupt handler for
  5090. * ops generated by the SCSI mid-layer to SATA devices
  5091. *
  5092. * Return value:
  5093. * none
  5094. **/
  5095. static void ipr_sata_done(struct ipr_cmnd *ipr_cmd)
  5096. {
  5097. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5098. struct ata_queued_cmd *qc = ipr_cmd->qc;
  5099. struct ipr_sata_port *sata_port = qc->ap->private_data;
  5100. struct ipr_resource_entry *res = sata_port->res;
  5101. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  5102. memcpy(&sata_port->ioasa, &ipr_cmd->ioasa.u.gata,
  5103. sizeof(struct ipr_ioasa_gata));
  5104. ipr_dump_ioasa(ioa_cfg, ipr_cmd, res);
  5105. if (be32_to_cpu(ipr_cmd->ioasa.ioasc_specific) & IPR_ATA_DEVICE_WAS_RESET)
  5106. scsi_report_device_reset(ioa_cfg->host, res->bus, res->target);
  5107. if (IPR_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  5108. qc->err_mask |= __ac_err_mask(ipr_cmd->ioasa.u.gata.status);
  5109. else
  5110. qc->err_mask |= ac_err_mask(ipr_cmd->ioasa.u.gata.status);
  5111. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  5112. ata_qc_complete(qc);
  5113. }
  5114. /**
  5115. * ipr_build_ata_ioadl64 - Build an ATA scatter/gather list
  5116. * @ipr_cmd: ipr command struct
  5117. * @qc: ATA queued command
  5118. *
  5119. **/
  5120. static void ipr_build_ata_ioadl64(struct ipr_cmnd *ipr_cmd,
  5121. struct ata_queued_cmd *qc)
  5122. {
  5123. u32 ioadl_flags = 0;
  5124. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  5125. struct ipr_ioadl64_desc *ioadl64 = ipr_cmd->i.ioadl64;
  5126. struct ipr_ioadl64_desc *last_ioadl64 = NULL;
  5127. int len = qc->nbytes;
  5128. struct scatterlist *sg;
  5129. unsigned int si;
  5130. dma_addr_t dma_addr = ipr_cmd->dma_addr;
  5131. if (len == 0)
  5132. return;
  5133. if (qc->dma_dir == DMA_TO_DEVICE) {
  5134. ioadl_flags = IPR_IOADL_FLAGS_WRITE;
  5135. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  5136. } else if (qc->dma_dir == DMA_FROM_DEVICE)
  5137. ioadl_flags = IPR_IOADL_FLAGS_READ;
  5138. ioarcb->data_transfer_length = cpu_to_be32(len);
  5139. ioarcb->ioadl_len =
  5140. cpu_to_be32(sizeof(struct ipr_ioadl64_desc) * ipr_cmd->dma_use_sg);
  5141. ioarcb->u.sis64_addr_data.data_ioadl_addr =
  5142. cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ata_ioadl));
  5143. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  5144. ioadl64->flags = cpu_to_be32(ioadl_flags);
  5145. ioadl64->data_len = cpu_to_be32(sg_dma_len(sg));
  5146. ioadl64->address = cpu_to_be64(sg_dma_address(sg));
  5147. last_ioadl64 = ioadl64;
  5148. ioadl64++;
  5149. }
  5150. if (likely(last_ioadl64))
  5151. last_ioadl64->flags |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
  5152. }
  5153. /**
  5154. * ipr_build_ata_ioadl - Build an ATA scatter/gather list
  5155. * @ipr_cmd: ipr command struct
  5156. * @qc: ATA queued command
  5157. *
  5158. **/
  5159. static void ipr_build_ata_ioadl(struct ipr_cmnd *ipr_cmd,
  5160. struct ata_queued_cmd *qc)
  5161. {
  5162. u32 ioadl_flags = 0;
  5163. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  5164. struct ipr_ioadl_desc *ioadl = ipr_cmd->i.ioadl;
  5165. struct ipr_ioadl_desc *last_ioadl = NULL;
  5166. int len = qc->nbytes;
  5167. struct scatterlist *sg;
  5168. unsigned int si;
  5169. if (len == 0)
  5170. return;
  5171. if (qc->dma_dir == DMA_TO_DEVICE) {
  5172. ioadl_flags = IPR_IOADL_FLAGS_WRITE;
  5173. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  5174. ioarcb->data_transfer_length = cpu_to_be32(len);
  5175. ioarcb->ioadl_len =
  5176. cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
  5177. } else if (qc->dma_dir == DMA_FROM_DEVICE) {
  5178. ioadl_flags = IPR_IOADL_FLAGS_READ;
  5179. ioarcb->read_data_transfer_length = cpu_to_be32(len);
  5180. ioarcb->read_ioadl_len =
  5181. cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
  5182. }
  5183. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  5184. ioadl->flags_and_data_len = cpu_to_be32(ioadl_flags | sg_dma_len(sg));
  5185. ioadl->address = cpu_to_be32(sg_dma_address(sg));
  5186. last_ioadl = ioadl;
  5187. ioadl++;
  5188. }
  5189. if (likely(last_ioadl))
  5190. last_ioadl->flags_and_data_len |= cpu_to_be32(IPR_IOADL_FLAGS_LAST);
  5191. }
  5192. /**
  5193. * ipr_qc_issue - Issue a SATA qc to a device
  5194. * @qc: queued command
  5195. *
  5196. * Return value:
  5197. * 0 if success
  5198. **/
  5199. static unsigned int ipr_qc_issue(struct ata_queued_cmd *qc)
  5200. {
  5201. struct ata_port *ap = qc->ap;
  5202. struct ipr_sata_port *sata_port = ap->private_data;
  5203. struct ipr_resource_entry *res = sata_port->res;
  5204. struct ipr_ioa_cfg *ioa_cfg = sata_port->ioa_cfg;
  5205. struct ipr_cmnd *ipr_cmd;
  5206. struct ipr_ioarcb *ioarcb;
  5207. struct ipr_ioarcb_ata_regs *regs;
  5208. if (unlikely(!ioa_cfg->allow_cmds || ioa_cfg->ioa_is_dead))
  5209. return AC_ERR_SYSTEM;
  5210. ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  5211. ioarcb = &ipr_cmd->ioarcb;
  5212. if (ioa_cfg->sis64) {
  5213. regs = &ipr_cmd->i.ata_ioadl.regs;
  5214. ioarcb->add_cmd_parms_offset = cpu_to_be16(sizeof(*ioarcb));
  5215. } else
  5216. regs = &ioarcb->u.add_data.u.regs;
  5217. memset(regs, 0, sizeof(*regs));
  5218. ioarcb->add_cmd_parms_len = cpu_to_be16(sizeof(*regs));
  5219. list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
  5220. ipr_cmd->qc = qc;
  5221. ipr_cmd->done = ipr_sata_done;
  5222. ipr_cmd->ioarcb.res_handle = res->res_handle;
  5223. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_ATA_PASSTHRU;
  5224. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
  5225. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
  5226. ipr_cmd->dma_use_sg = qc->n_elem;
  5227. if (ioa_cfg->sis64)
  5228. ipr_build_ata_ioadl64(ipr_cmd, qc);
  5229. else
  5230. ipr_build_ata_ioadl(ipr_cmd, qc);
  5231. regs->flags |= IPR_ATA_FLAG_STATUS_ON_GOOD_COMPLETION;
  5232. ipr_copy_sata_tf(regs, &qc->tf);
  5233. memcpy(ioarcb->cmd_pkt.cdb, qc->cdb, IPR_MAX_CDB_LEN);
  5234. ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_RES_PHYS_LOC(res));
  5235. switch (qc->tf.protocol) {
  5236. case ATA_PROT_NODATA:
  5237. case ATA_PROT_PIO:
  5238. break;
  5239. case ATA_PROT_DMA:
  5240. regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
  5241. break;
  5242. case ATAPI_PROT_PIO:
  5243. case ATAPI_PROT_NODATA:
  5244. regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
  5245. break;
  5246. case ATAPI_PROT_DMA:
  5247. regs->flags |= IPR_ATA_FLAG_PACKET_CMD;
  5248. regs->flags |= IPR_ATA_FLAG_XFER_TYPE_DMA;
  5249. break;
  5250. default:
  5251. WARN_ON(1);
  5252. return AC_ERR_INVALID;
  5253. }
  5254. mb();
  5255. ipr_send_command(ipr_cmd);
  5256. return 0;
  5257. }
  5258. /**
  5259. * ipr_qc_fill_rtf - Read result TF
  5260. * @qc: ATA queued command
  5261. *
  5262. * Return value:
  5263. * true
  5264. **/
  5265. static bool ipr_qc_fill_rtf(struct ata_queued_cmd *qc)
  5266. {
  5267. struct ipr_sata_port *sata_port = qc->ap->private_data;
  5268. struct ipr_ioasa_gata *g = &sata_port->ioasa;
  5269. struct ata_taskfile *tf = &qc->result_tf;
  5270. tf->feature = g->error;
  5271. tf->nsect = g->nsect;
  5272. tf->lbal = g->lbal;
  5273. tf->lbam = g->lbam;
  5274. tf->lbah = g->lbah;
  5275. tf->device = g->device;
  5276. tf->command = g->status;
  5277. tf->hob_nsect = g->hob_nsect;
  5278. tf->hob_lbal = g->hob_lbal;
  5279. tf->hob_lbam = g->hob_lbam;
  5280. tf->hob_lbah = g->hob_lbah;
  5281. tf->ctl = g->alt_status;
  5282. return true;
  5283. }
  5284. static struct ata_port_operations ipr_sata_ops = {
  5285. .phy_reset = ipr_ata_phy_reset,
  5286. .hardreset = ipr_sata_reset,
  5287. .post_internal_cmd = ipr_ata_post_internal,
  5288. .qc_prep = ata_noop_qc_prep,
  5289. .qc_issue = ipr_qc_issue,
  5290. .qc_fill_rtf = ipr_qc_fill_rtf,
  5291. .port_start = ata_sas_port_start,
  5292. .port_stop = ata_sas_port_stop
  5293. };
  5294. static struct ata_port_info sata_port_info = {
  5295. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | ATA_FLAG_SATA_RESET |
  5296. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
  5297. .pio_mask = 0x10, /* pio4 */
  5298. .mwdma_mask = 0x07,
  5299. .udma_mask = 0x7f, /* udma0-6 */
  5300. .port_ops = &ipr_sata_ops
  5301. };
  5302. #ifdef CONFIG_PPC_PSERIES
  5303. static const u16 ipr_blocked_processors[] = {
  5304. PV_NORTHSTAR,
  5305. PV_PULSAR,
  5306. PV_POWER4,
  5307. PV_ICESTAR,
  5308. PV_SSTAR,
  5309. PV_POWER4p,
  5310. PV_630,
  5311. PV_630p
  5312. };
  5313. /**
  5314. * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
  5315. * @ioa_cfg: ioa cfg struct
  5316. *
  5317. * Adapters that use Gemstone revision < 3.1 do not work reliably on
  5318. * certain pSeries hardware. This function determines if the given
  5319. * adapter is in one of these confgurations or not.
  5320. *
  5321. * Return value:
  5322. * 1 if adapter is not supported / 0 if adapter is supported
  5323. **/
  5324. static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
  5325. {
  5326. int i;
  5327. if ((ioa_cfg->type == 0x5702) && (ioa_cfg->pdev->revision < 4)) {
  5328. for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
  5329. if (__is_processor(ipr_blocked_processors[i]))
  5330. return 1;
  5331. }
  5332. }
  5333. return 0;
  5334. }
  5335. #else
  5336. #define ipr_invalid_adapter(ioa_cfg) 0
  5337. #endif
  5338. /**
  5339. * ipr_ioa_bringdown_done - IOA bring down completion.
  5340. * @ipr_cmd: ipr command struct
  5341. *
  5342. * This function processes the completion of an adapter bring down.
  5343. * It wakes any reset sleepers.
  5344. *
  5345. * Return value:
  5346. * IPR_RC_JOB_RETURN
  5347. **/
  5348. static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
  5349. {
  5350. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5351. ENTER;
  5352. ioa_cfg->in_reset_reload = 0;
  5353. ioa_cfg->reset_retries = 0;
  5354. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  5355. wake_up_all(&ioa_cfg->reset_wait_q);
  5356. spin_unlock_irq(ioa_cfg->host->host_lock);
  5357. scsi_unblock_requests(ioa_cfg->host);
  5358. spin_lock_irq(ioa_cfg->host->host_lock);
  5359. LEAVE;
  5360. return IPR_RC_JOB_RETURN;
  5361. }
  5362. /**
  5363. * ipr_ioa_reset_done - IOA reset completion.
  5364. * @ipr_cmd: ipr command struct
  5365. *
  5366. * This function processes the completion of an adapter reset.
  5367. * It schedules any necessary mid-layer add/removes and
  5368. * wakes any reset sleepers.
  5369. *
  5370. * Return value:
  5371. * IPR_RC_JOB_RETURN
  5372. **/
  5373. static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
  5374. {
  5375. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5376. struct ipr_resource_entry *res;
  5377. struct ipr_hostrcb *hostrcb, *temp;
  5378. int i = 0;
  5379. ENTER;
  5380. ioa_cfg->in_reset_reload = 0;
  5381. ioa_cfg->allow_cmds = 1;
  5382. ioa_cfg->reset_cmd = NULL;
  5383. ioa_cfg->doorbell |= IPR_RUNTIME_RESET;
  5384. list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
  5385. if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
  5386. ipr_trace;
  5387. break;
  5388. }
  5389. }
  5390. schedule_work(&ioa_cfg->work_q);
  5391. list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
  5392. list_del(&hostrcb->queue);
  5393. if (i++ < IPR_NUM_LOG_HCAMS)
  5394. ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
  5395. else
  5396. ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
  5397. }
  5398. scsi_report_bus_reset(ioa_cfg->host, IPR_VSET_BUS);
  5399. dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
  5400. ioa_cfg->reset_retries = 0;
  5401. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  5402. wake_up_all(&ioa_cfg->reset_wait_q);
  5403. spin_unlock(ioa_cfg->host->host_lock);
  5404. scsi_unblock_requests(ioa_cfg->host);
  5405. spin_lock(ioa_cfg->host->host_lock);
  5406. if (!ioa_cfg->allow_cmds)
  5407. scsi_block_requests(ioa_cfg->host);
  5408. LEAVE;
  5409. return IPR_RC_JOB_RETURN;
  5410. }
  5411. /**
  5412. * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
  5413. * @supported_dev: supported device struct
  5414. * @vpids: vendor product id struct
  5415. *
  5416. * Return value:
  5417. * none
  5418. **/
  5419. static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
  5420. struct ipr_std_inq_vpids *vpids)
  5421. {
  5422. memset(supported_dev, 0, sizeof(struct ipr_supported_device));
  5423. memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
  5424. supported_dev->num_records = 1;
  5425. supported_dev->data_length =
  5426. cpu_to_be16(sizeof(struct ipr_supported_device));
  5427. supported_dev->reserved = 0;
  5428. }
  5429. /**
  5430. * ipr_set_supported_devs - Send Set Supported Devices for a device
  5431. * @ipr_cmd: ipr command struct
  5432. *
  5433. * This function sends a Set Supported Devices to the adapter
  5434. *
  5435. * Return value:
  5436. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  5437. **/
  5438. static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
  5439. {
  5440. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5441. struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
  5442. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  5443. struct ipr_resource_entry *res = ipr_cmd->u.res;
  5444. ipr_cmd->job_step = ipr_ioa_reset_done;
  5445. list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
  5446. if (!ipr_is_scsi_disk(res))
  5447. continue;
  5448. ipr_cmd->u.res = res;
  5449. ipr_set_sup_dev_dflt(supp_dev, &res->std_inq_data.vpids);
  5450. ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  5451. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  5452. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
  5453. ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
  5454. ioarcb->cmd_pkt.cdb[1] = IPR_SET_ALL_SUPPORTED_DEVICES;
  5455. ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
  5456. ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
  5457. ipr_init_ioadl(ipr_cmd,
  5458. ioa_cfg->vpd_cbs_dma +
  5459. offsetof(struct ipr_misc_cbs, supp_dev),
  5460. sizeof(struct ipr_supported_device),
  5461. IPR_IOADL_FLAGS_WRITE_LAST);
  5462. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
  5463. IPR_SET_SUP_DEVICE_TIMEOUT);
  5464. if (!ioa_cfg->sis64)
  5465. ipr_cmd->job_step = ipr_set_supported_devs;
  5466. return IPR_RC_JOB_RETURN;
  5467. }
  5468. return IPR_RC_JOB_CONTINUE;
  5469. }
  5470. /**
  5471. * ipr_get_mode_page - Locate specified mode page
  5472. * @mode_pages: mode page buffer
  5473. * @page_code: page code to find
  5474. * @len: minimum required length for mode page
  5475. *
  5476. * Return value:
  5477. * pointer to mode page / NULL on failure
  5478. **/
  5479. static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
  5480. u32 page_code, u32 len)
  5481. {
  5482. struct ipr_mode_page_hdr *mode_hdr;
  5483. u32 page_length;
  5484. u32 length;
  5485. if (!mode_pages || (mode_pages->hdr.length == 0))
  5486. return NULL;
  5487. length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
  5488. mode_hdr = (struct ipr_mode_page_hdr *)
  5489. (mode_pages->data + mode_pages->hdr.block_desc_len);
  5490. while (length) {
  5491. if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
  5492. if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
  5493. return mode_hdr;
  5494. break;
  5495. } else {
  5496. page_length = (sizeof(struct ipr_mode_page_hdr) +
  5497. mode_hdr->page_length);
  5498. length -= page_length;
  5499. mode_hdr = (struct ipr_mode_page_hdr *)
  5500. ((unsigned long)mode_hdr + page_length);
  5501. }
  5502. }
  5503. return NULL;
  5504. }
  5505. /**
  5506. * ipr_check_term_power - Check for term power errors
  5507. * @ioa_cfg: ioa config struct
  5508. * @mode_pages: IOAFP mode pages buffer
  5509. *
  5510. * Check the IOAFP's mode page 28 for term power errors
  5511. *
  5512. * Return value:
  5513. * nothing
  5514. **/
  5515. static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
  5516. struct ipr_mode_pages *mode_pages)
  5517. {
  5518. int i;
  5519. int entry_length;
  5520. struct ipr_dev_bus_entry *bus;
  5521. struct ipr_mode_page28 *mode_page;
  5522. mode_page = ipr_get_mode_page(mode_pages, 0x28,
  5523. sizeof(struct ipr_mode_page28));
  5524. entry_length = mode_page->entry_length;
  5525. bus = mode_page->bus;
  5526. for (i = 0; i < mode_page->num_entries; i++) {
  5527. if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
  5528. dev_err(&ioa_cfg->pdev->dev,
  5529. "Term power is absent on scsi bus %d\n",
  5530. bus->res_addr.bus);
  5531. }
  5532. bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
  5533. }
  5534. }
  5535. /**
  5536. * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
  5537. * @ioa_cfg: ioa config struct
  5538. *
  5539. * Looks through the config table checking for SES devices. If
  5540. * the SES device is in the SES table indicating a maximum SCSI
  5541. * bus speed, the speed is limited for the bus.
  5542. *
  5543. * Return value:
  5544. * none
  5545. **/
  5546. static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
  5547. {
  5548. u32 max_xfer_rate;
  5549. int i;
  5550. for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
  5551. max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
  5552. ioa_cfg->bus_attr[i].bus_width);
  5553. if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
  5554. ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
  5555. }
  5556. }
  5557. /**
  5558. * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
  5559. * @ioa_cfg: ioa config struct
  5560. * @mode_pages: mode page 28 buffer
  5561. *
  5562. * Updates mode page 28 based on driver configuration
  5563. *
  5564. * Return value:
  5565. * none
  5566. **/
  5567. static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
  5568. struct ipr_mode_pages *mode_pages)
  5569. {
  5570. int i, entry_length;
  5571. struct ipr_dev_bus_entry *bus;
  5572. struct ipr_bus_attributes *bus_attr;
  5573. struct ipr_mode_page28 *mode_page;
  5574. mode_page = ipr_get_mode_page(mode_pages, 0x28,
  5575. sizeof(struct ipr_mode_page28));
  5576. entry_length = mode_page->entry_length;
  5577. /* Loop for each device bus entry */
  5578. for (i = 0, bus = mode_page->bus;
  5579. i < mode_page->num_entries;
  5580. i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
  5581. if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
  5582. dev_err(&ioa_cfg->pdev->dev,
  5583. "Invalid resource address reported: 0x%08X\n",
  5584. IPR_GET_PHYS_LOC(bus->res_addr));
  5585. continue;
  5586. }
  5587. bus_attr = &ioa_cfg->bus_attr[i];
  5588. bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
  5589. bus->bus_width = bus_attr->bus_width;
  5590. bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
  5591. bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
  5592. if (bus_attr->qas_enabled)
  5593. bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
  5594. else
  5595. bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
  5596. }
  5597. }
  5598. /**
  5599. * ipr_build_mode_select - Build a mode select command
  5600. * @ipr_cmd: ipr command struct
  5601. * @res_handle: resource handle to send command to
  5602. * @parm: Byte 2 of Mode Sense command
  5603. * @dma_addr: DMA buffer address
  5604. * @xfer_len: data transfer length
  5605. *
  5606. * Return value:
  5607. * none
  5608. **/
  5609. static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
  5610. __be32 res_handle, u8 parm,
  5611. dma_addr_t dma_addr, u8 xfer_len)
  5612. {
  5613. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  5614. ioarcb->res_handle = res_handle;
  5615. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
  5616. ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
  5617. ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
  5618. ioarcb->cmd_pkt.cdb[1] = parm;
  5619. ioarcb->cmd_pkt.cdb[4] = xfer_len;
  5620. ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_WRITE_LAST);
  5621. }
  5622. /**
  5623. * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
  5624. * @ipr_cmd: ipr command struct
  5625. *
  5626. * This function sets up the SCSI bus attributes and sends
  5627. * a Mode Select for Page 28 to activate them.
  5628. *
  5629. * Return value:
  5630. * IPR_RC_JOB_RETURN
  5631. **/
  5632. static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
  5633. {
  5634. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5635. struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
  5636. int length;
  5637. ENTER;
  5638. ipr_scsi_bus_speed_limit(ioa_cfg);
  5639. ipr_check_term_power(ioa_cfg, mode_pages);
  5640. ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
  5641. length = mode_pages->hdr.length + 1;
  5642. mode_pages->hdr.length = 0;
  5643. ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
  5644. ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
  5645. length);
  5646. ipr_cmd->job_step = ipr_set_supported_devs;
  5647. ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
  5648. struct ipr_resource_entry, queue);
  5649. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
  5650. LEAVE;
  5651. return IPR_RC_JOB_RETURN;
  5652. }
  5653. /**
  5654. * ipr_build_mode_sense - Builds a mode sense command
  5655. * @ipr_cmd: ipr command struct
  5656. * @res: resource entry struct
  5657. * @parm: Byte 2 of mode sense command
  5658. * @dma_addr: DMA address of mode sense buffer
  5659. * @xfer_len: Size of DMA buffer
  5660. *
  5661. * Return value:
  5662. * none
  5663. **/
  5664. static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
  5665. __be32 res_handle,
  5666. u8 parm, dma_addr_t dma_addr, u8 xfer_len)
  5667. {
  5668. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  5669. ioarcb->res_handle = res_handle;
  5670. ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
  5671. ioarcb->cmd_pkt.cdb[2] = parm;
  5672. ioarcb->cmd_pkt.cdb[4] = xfer_len;
  5673. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
  5674. ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
  5675. }
  5676. /**
  5677. * ipr_reset_cmd_failed - Handle failure of IOA reset command
  5678. * @ipr_cmd: ipr command struct
  5679. *
  5680. * This function handles the failure of an IOA bringup command.
  5681. *
  5682. * Return value:
  5683. * IPR_RC_JOB_RETURN
  5684. **/
  5685. static int ipr_reset_cmd_failed(struct ipr_cmnd *ipr_cmd)
  5686. {
  5687. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5688. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  5689. dev_err(&ioa_cfg->pdev->dev,
  5690. "0x%02X failed with IOASC: 0x%08X\n",
  5691. ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
  5692. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  5693. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  5694. return IPR_RC_JOB_RETURN;
  5695. }
  5696. /**
  5697. * ipr_reset_mode_sense_failed - Handle failure of IOAFP mode sense
  5698. * @ipr_cmd: ipr command struct
  5699. *
  5700. * This function handles the failure of a Mode Sense to the IOAFP.
  5701. * Some adapters do not handle all mode pages.
  5702. *
  5703. * Return value:
  5704. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  5705. **/
  5706. static int ipr_reset_mode_sense_failed(struct ipr_cmnd *ipr_cmd)
  5707. {
  5708. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5709. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  5710. if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
  5711. ipr_cmd->job_step = ipr_set_supported_devs;
  5712. ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
  5713. struct ipr_resource_entry, queue);
  5714. return IPR_RC_JOB_CONTINUE;
  5715. }
  5716. return ipr_reset_cmd_failed(ipr_cmd);
  5717. }
  5718. /**
  5719. * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
  5720. * @ipr_cmd: ipr command struct
  5721. *
  5722. * This function send a Page 28 mode sense to the IOA to
  5723. * retrieve SCSI bus attributes.
  5724. *
  5725. * Return value:
  5726. * IPR_RC_JOB_RETURN
  5727. **/
  5728. static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
  5729. {
  5730. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5731. ENTER;
  5732. ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
  5733. 0x28, ioa_cfg->vpd_cbs_dma +
  5734. offsetof(struct ipr_misc_cbs, mode_pages),
  5735. sizeof(struct ipr_mode_pages));
  5736. ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
  5737. ipr_cmd->job_step_failed = ipr_reset_mode_sense_failed;
  5738. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
  5739. LEAVE;
  5740. return IPR_RC_JOB_RETURN;
  5741. }
  5742. /**
  5743. * ipr_ioafp_mode_select_page24 - Issue Mode Select to IOA
  5744. * @ipr_cmd: ipr command struct
  5745. *
  5746. * This function enables dual IOA RAID support if possible.
  5747. *
  5748. * Return value:
  5749. * IPR_RC_JOB_RETURN
  5750. **/
  5751. static int ipr_ioafp_mode_select_page24(struct ipr_cmnd *ipr_cmd)
  5752. {
  5753. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5754. struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
  5755. struct ipr_mode_page24 *mode_page;
  5756. int length;
  5757. ENTER;
  5758. mode_page = ipr_get_mode_page(mode_pages, 0x24,
  5759. sizeof(struct ipr_mode_page24));
  5760. if (mode_page)
  5761. mode_page->flags |= IPR_ENABLE_DUAL_IOA_AF;
  5762. length = mode_pages->hdr.length + 1;
  5763. mode_pages->hdr.length = 0;
  5764. ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
  5765. ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
  5766. length);
  5767. ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
  5768. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
  5769. LEAVE;
  5770. return IPR_RC_JOB_RETURN;
  5771. }
  5772. /**
  5773. * ipr_reset_mode_sense_page24_failed - Handle failure of IOAFP mode sense
  5774. * @ipr_cmd: ipr command struct
  5775. *
  5776. * This function handles the failure of a Mode Sense to the IOAFP.
  5777. * Some adapters do not handle all mode pages.
  5778. *
  5779. * Return value:
  5780. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  5781. **/
  5782. static int ipr_reset_mode_sense_page24_failed(struct ipr_cmnd *ipr_cmd)
  5783. {
  5784. u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  5785. if (ioasc == IPR_IOASC_IR_INVALID_REQ_TYPE_OR_PKT) {
  5786. ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
  5787. return IPR_RC_JOB_CONTINUE;
  5788. }
  5789. return ipr_reset_cmd_failed(ipr_cmd);
  5790. }
  5791. /**
  5792. * ipr_ioafp_mode_sense_page24 - Issue Page 24 Mode Sense to IOA
  5793. * @ipr_cmd: ipr command struct
  5794. *
  5795. * This function send a mode sense to the IOA to retrieve
  5796. * the IOA Advanced Function Control mode page.
  5797. *
  5798. * Return value:
  5799. * IPR_RC_JOB_RETURN
  5800. **/
  5801. static int ipr_ioafp_mode_sense_page24(struct ipr_cmnd *ipr_cmd)
  5802. {
  5803. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5804. ENTER;
  5805. ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
  5806. 0x24, ioa_cfg->vpd_cbs_dma +
  5807. offsetof(struct ipr_misc_cbs, mode_pages),
  5808. sizeof(struct ipr_mode_pages));
  5809. ipr_cmd->job_step = ipr_ioafp_mode_select_page24;
  5810. ipr_cmd->job_step_failed = ipr_reset_mode_sense_page24_failed;
  5811. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
  5812. LEAVE;
  5813. return IPR_RC_JOB_RETURN;
  5814. }
  5815. /**
  5816. * ipr_init_res_table - Initialize the resource table
  5817. * @ipr_cmd: ipr command struct
  5818. *
  5819. * This function looks through the existing resource table, comparing
  5820. * it with the config table. This function will take care of old/new
  5821. * devices and schedule adding/removing them from the mid-layer
  5822. * as appropriate.
  5823. *
  5824. * Return value:
  5825. * IPR_RC_JOB_CONTINUE
  5826. **/
  5827. static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
  5828. {
  5829. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5830. struct ipr_resource_entry *res, *temp;
  5831. struct ipr_config_table_entry_wrapper cfgtew;
  5832. int entries, found, flag, i;
  5833. LIST_HEAD(old_res);
  5834. ENTER;
  5835. if (ioa_cfg->sis64)
  5836. flag = ioa_cfg->u.cfg_table64->hdr64.flags;
  5837. else
  5838. flag = ioa_cfg->u.cfg_table->hdr.flags;
  5839. if (flag & IPR_UCODE_DOWNLOAD_REQ)
  5840. dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
  5841. list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
  5842. list_move_tail(&res->queue, &old_res);
  5843. if (ioa_cfg->sis64)
  5844. entries = ioa_cfg->u.cfg_table64->hdr64.num_entries;
  5845. else
  5846. entries = ioa_cfg->u.cfg_table->hdr.num_entries;
  5847. for (i = 0; i < entries; i++) {
  5848. if (ioa_cfg->sis64)
  5849. cfgtew.u.cfgte64 = &ioa_cfg->u.cfg_table64->dev[i];
  5850. else
  5851. cfgtew.u.cfgte = &ioa_cfg->u.cfg_table->dev[i];
  5852. found = 0;
  5853. list_for_each_entry_safe(res, temp, &old_res, queue) {
  5854. if (ipr_is_same_device(res, &cfgtew)) {
  5855. list_move_tail(&res->queue, &ioa_cfg->used_res_q);
  5856. found = 1;
  5857. break;
  5858. }
  5859. }
  5860. if (!found) {
  5861. if (list_empty(&ioa_cfg->free_res_q)) {
  5862. dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
  5863. break;
  5864. }
  5865. found = 1;
  5866. res = list_entry(ioa_cfg->free_res_q.next,
  5867. struct ipr_resource_entry, queue);
  5868. list_move_tail(&res->queue, &ioa_cfg->used_res_q);
  5869. ipr_init_res_entry(res, &cfgtew);
  5870. res->add_to_ml = 1;
  5871. }
  5872. if (found)
  5873. ipr_update_res_entry(res, &cfgtew);
  5874. }
  5875. list_for_each_entry_safe(res, temp, &old_res, queue) {
  5876. if (res->sdev) {
  5877. res->del_from_ml = 1;
  5878. res->res_handle = IPR_INVALID_RES_HANDLE;
  5879. list_move_tail(&res->queue, &ioa_cfg->used_res_q);
  5880. }
  5881. }
  5882. list_for_each_entry_safe(res, temp, &old_res, queue) {
  5883. ipr_clear_res_target(res);
  5884. list_move_tail(&res->queue, &ioa_cfg->free_res_q);
  5885. }
  5886. if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
  5887. ipr_cmd->job_step = ipr_ioafp_mode_sense_page24;
  5888. else
  5889. ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
  5890. LEAVE;
  5891. return IPR_RC_JOB_CONTINUE;
  5892. }
  5893. /**
  5894. * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
  5895. * @ipr_cmd: ipr command struct
  5896. *
  5897. * This function sends a Query IOA Configuration command
  5898. * to the adapter to retrieve the IOA configuration table.
  5899. *
  5900. * Return value:
  5901. * IPR_RC_JOB_RETURN
  5902. **/
  5903. static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
  5904. {
  5905. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5906. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  5907. struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
  5908. struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
  5909. ENTER;
  5910. if (cap->cap & IPR_CAP_DUAL_IOA_RAID)
  5911. ioa_cfg->dual_raid = 1;
  5912. dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
  5913. ucode_vpd->major_release, ucode_vpd->card_type,
  5914. ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
  5915. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
  5916. ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  5917. ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
  5918. ioarcb->cmd_pkt.cdb[7] = (ioa_cfg->cfg_table_size >> 8) & 0xff;
  5919. ioarcb->cmd_pkt.cdb[8] = ioa_cfg->cfg_table_size & 0xff;
  5920. ipr_init_ioadl(ipr_cmd, ioa_cfg->cfg_table_dma, ioa_cfg->cfg_table_size,
  5921. IPR_IOADL_FLAGS_READ_LAST);
  5922. ipr_cmd->job_step = ipr_init_res_table;
  5923. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
  5924. LEAVE;
  5925. return IPR_RC_JOB_RETURN;
  5926. }
  5927. /**
  5928. * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
  5929. * @ipr_cmd: ipr command struct
  5930. *
  5931. * This utility function sends an inquiry to the adapter.
  5932. *
  5933. * Return value:
  5934. * none
  5935. **/
  5936. static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
  5937. dma_addr_t dma_addr, u8 xfer_len)
  5938. {
  5939. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  5940. ENTER;
  5941. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
  5942. ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  5943. ioarcb->cmd_pkt.cdb[0] = INQUIRY;
  5944. ioarcb->cmd_pkt.cdb[1] = flags;
  5945. ioarcb->cmd_pkt.cdb[2] = page;
  5946. ioarcb->cmd_pkt.cdb[4] = xfer_len;
  5947. ipr_init_ioadl(ipr_cmd, dma_addr, xfer_len, IPR_IOADL_FLAGS_READ_LAST);
  5948. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
  5949. LEAVE;
  5950. }
  5951. /**
  5952. * ipr_inquiry_page_supported - Is the given inquiry page supported
  5953. * @page0: inquiry page 0 buffer
  5954. * @page: page code.
  5955. *
  5956. * This function determines if the specified inquiry page is supported.
  5957. *
  5958. * Return value:
  5959. * 1 if page is supported / 0 if not
  5960. **/
  5961. static int ipr_inquiry_page_supported(struct ipr_inquiry_page0 *page0, u8 page)
  5962. {
  5963. int i;
  5964. for (i = 0; i < min_t(u8, page0->len, IPR_INQUIRY_PAGE0_ENTRIES); i++)
  5965. if (page0->page[i] == page)
  5966. return 1;
  5967. return 0;
  5968. }
  5969. /**
  5970. * ipr_ioafp_cap_inquiry - Send a Page 0xD0 Inquiry to the adapter.
  5971. * @ipr_cmd: ipr command struct
  5972. *
  5973. * This function sends a Page 0xD0 inquiry to the adapter
  5974. * to retrieve adapter capabilities.
  5975. *
  5976. * Return value:
  5977. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  5978. **/
  5979. static int ipr_ioafp_cap_inquiry(struct ipr_cmnd *ipr_cmd)
  5980. {
  5981. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  5982. struct ipr_inquiry_page0 *page0 = &ioa_cfg->vpd_cbs->page0_data;
  5983. struct ipr_inquiry_cap *cap = &ioa_cfg->vpd_cbs->cap;
  5984. ENTER;
  5985. ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
  5986. memset(cap, 0, sizeof(*cap));
  5987. if (ipr_inquiry_page_supported(page0, 0xD0)) {
  5988. ipr_ioafp_inquiry(ipr_cmd, 1, 0xD0,
  5989. ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, cap),
  5990. sizeof(struct ipr_inquiry_cap));
  5991. return IPR_RC_JOB_RETURN;
  5992. }
  5993. LEAVE;
  5994. return IPR_RC_JOB_CONTINUE;
  5995. }
  5996. /**
  5997. * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
  5998. * @ipr_cmd: ipr command struct
  5999. *
  6000. * This function sends a Page 3 inquiry to the adapter
  6001. * to retrieve software VPD information.
  6002. *
  6003. * Return value:
  6004. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6005. **/
  6006. static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
  6007. {
  6008. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6009. ENTER;
  6010. ipr_cmd->job_step = ipr_ioafp_cap_inquiry;
  6011. ipr_ioafp_inquiry(ipr_cmd, 1, 3,
  6012. ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
  6013. sizeof(struct ipr_inquiry_page3));
  6014. LEAVE;
  6015. return IPR_RC_JOB_RETURN;
  6016. }
  6017. /**
  6018. * ipr_ioafp_page0_inquiry - Send a Page 0 Inquiry to the adapter.
  6019. * @ipr_cmd: ipr command struct
  6020. *
  6021. * This function sends a Page 0 inquiry to the adapter
  6022. * to retrieve supported inquiry pages.
  6023. *
  6024. * Return value:
  6025. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6026. **/
  6027. static int ipr_ioafp_page0_inquiry(struct ipr_cmnd *ipr_cmd)
  6028. {
  6029. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6030. char type[5];
  6031. ENTER;
  6032. /* Grab the type out of the VPD and store it away */
  6033. memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
  6034. type[4] = '\0';
  6035. ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
  6036. ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
  6037. ipr_ioafp_inquiry(ipr_cmd, 1, 0,
  6038. ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page0_data),
  6039. sizeof(struct ipr_inquiry_page0));
  6040. LEAVE;
  6041. return IPR_RC_JOB_RETURN;
  6042. }
  6043. /**
  6044. * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
  6045. * @ipr_cmd: ipr command struct
  6046. *
  6047. * This function sends a standard inquiry to the adapter.
  6048. *
  6049. * Return value:
  6050. * IPR_RC_JOB_RETURN
  6051. **/
  6052. static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
  6053. {
  6054. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6055. ENTER;
  6056. ipr_cmd->job_step = ipr_ioafp_page0_inquiry;
  6057. ipr_ioafp_inquiry(ipr_cmd, 0, 0,
  6058. ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
  6059. sizeof(struct ipr_ioa_vpd));
  6060. LEAVE;
  6061. return IPR_RC_JOB_RETURN;
  6062. }
  6063. /**
  6064. * ipr_ioafp_identify_hrrq - Send Identify Host RRQ.
  6065. * @ipr_cmd: ipr command struct
  6066. *
  6067. * This function send an Identify Host Request Response Queue
  6068. * command to establish the HRRQ with the adapter.
  6069. *
  6070. * Return value:
  6071. * IPR_RC_JOB_RETURN
  6072. **/
  6073. static int ipr_ioafp_identify_hrrq(struct ipr_cmnd *ipr_cmd)
  6074. {
  6075. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6076. struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
  6077. ENTER;
  6078. dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
  6079. ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
  6080. ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  6081. ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
  6082. if (ioa_cfg->sis64)
  6083. ioarcb->cmd_pkt.cdb[1] = 0x1;
  6084. ioarcb->cmd_pkt.cdb[2] =
  6085. ((u64) ioa_cfg->host_rrq_dma >> 24) & 0xff;
  6086. ioarcb->cmd_pkt.cdb[3] =
  6087. ((u64) ioa_cfg->host_rrq_dma >> 16) & 0xff;
  6088. ioarcb->cmd_pkt.cdb[4] =
  6089. ((u64) ioa_cfg->host_rrq_dma >> 8) & 0xff;
  6090. ioarcb->cmd_pkt.cdb[5] =
  6091. ((u64) ioa_cfg->host_rrq_dma) & 0xff;
  6092. ioarcb->cmd_pkt.cdb[7] =
  6093. ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
  6094. ioarcb->cmd_pkt.cdb[8] =
  6095. (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
  6096. if (ioa_cfg->sis64) {
  6097. ioarcb->cmd_pkt.cdb[10] =
  6098. ((u64) ioa_cfg->host_rrq_dma >> 56) & 0xff;
  6099. ioarcb->cmd_pkt.cdb[11] =
  6100. ((u64) ioa_cfg->host_rrq_dma >> 48) & 0xff;
  6101. ioarcb->cmd_pkt.cdb[12] =
  6102. ((u64) ioa_cfg->host_rrq_dma >> 40) & 0xff;
  6103. ioarcb->cmd_pkt.cdb[13] =
  6104. ((u64) ioa_cfg->host_rrq_dma >> 32) & 0xff;
  6105. }
  6106. ipr_cmd->job_step = ipr_ioafp_std_inquiry;
  6107. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
  6108. LEAVE;
  6109. return IPR_RC_JOB_RETURN;
  6110. }
  6111. /**
  6112. * ipr_reset_timer_done - Adapter reset timer function
  6113. * @ipr_cmd: ipr command struct
  6114. *
  6115. * Description: This function is used in adapter reset processing
  6116. * for timing events. If the reset_cmd pointer in the IOA
  6117. * config struct is not this adapter's we are doing nested
  6118. * resets and fail_all_ops will take care of freeing the
  6119. * command block.
  6120. *
  6121. * Return value:
  6122. * none
  6123. **/
  6124. static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
  6125. {
  6126. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6127. unsigned long lock_flags = 0;
  6128. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  6129. if (ioa_cfg->reset_cmd == ipr_cmd) {
  6130. list_del(&ipr_cmd->queue);
  6131. ipr_cmd->done(ipr_cmd);
  6132. }
  6133. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  6134. }
  6135. /**
  6136. * ipr_reset_start_timer - Start a timer for adapter reset job
  6137. * @ipr_cmd: ipr command struct
  6138. * @timeout: timeout value
  6139. *
  6140. * Description: This function is used in adapter reset processing
  6141. * for timing events. If the reset_cmd pointer in the IOA
  6142. * config struct is not this adapter's we are doing nested
  6143. * resets and fail_all_ops will take care of freeing the
  6144. * command block.
  6145. *
  6146. * Return value:
  6147. * none
  6148. **/
  6149. static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
  6150. unsigned long timeout)
  6151. {
  6152. list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
  6153. ipr_cmd->done = ipr_reset_ioa_job;
  6154. ipr_cmd->timer.data = (unsigned long) ipr_cmd;
  6155. ipr_cmd->timer.expires = jiffies + timeout;
  6156. ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
  6157. add_timer(&ipr_cmd->timer);
  6158. }
  6159. /**
  6160. * ipr_init_ioa_mem - Initialize ioa_cfg control block
  6161. * @ioa_cfg: ioa cfg struct
  6162. *
  6163. * Return value:
  6164. * nothing
  6165. **/
  6166. static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
  6167. {
  6168. memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
  6169. /* Initialize Host RRQ pointers */
  6170. ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
  6171. ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
  6172. ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
  6173. ioa_cfg->toggle_bit = 1;
  6174. /* Zero out config table */
  6175. memset(ioa_cfg->u.cfg_table, 0, ioa_cfg->cfg_table_size);
  6176. }
  6177. /**
  6178. * ipr_reset_next_stage - Process IPL stage change based on feedback register.
  6179. * @ipr_cmd: ipr command struct
  6180. *
  6181. * Return value:
  6182. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6183. **/
  6184. static int ipr_reset_next_stage(struct ipr_cmnd *ipr_cmd)
  6185. {
  6186. unsigned long stage, stage_time;
  6187. u32 feedback;
  6188. volatile u32 int_reg;
  6189. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6190. u64 maskval = 0;
  6191. feedback = readl(ioa_cfg->regs.init_feedback_reg);
  6192. stage = feedback & IPR_IPL_INIT_STAGE_MASK;
  6193. stage_time = feedback & IPR_IPL_INIT_STAGE_TIME_MASK;
  6194. ipr_dbg("IPL stage = 0x%lx, IPL stage time = %ld\n", stage, stage_time);
  6195. /* sanity check the stage_time value */
  6196. if (stage_time < IPR_IPL_INIT_MIN_STAGE_TIME)
  6197. stage_time = IPR_IPL_INIT_MIN_STAGE_TIME;
  6198. else if (stage_time > IPR_LONG_OPERATIONAL_TIMEOUT)
  6199. stage_time = IPR_LONG_OPERATIONAL_TIMEOUT;
  6200. if (stage == IPR_IPL_INIT_STAGE_UNKNOWN) {
  6201. writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.set_interrupt_mask_reg);
  6202. int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
  6203. stage_time = ioa_cfg->transop_timeout;
  6204. ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
  6205. } else if (stage == IPR_IPL_INIT_STAGE_TRANSOP) {
  6206. ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
  6207. maskval = IPR_PCII_IPL_STAGE_CHANGE;
  6208. maskval = (maskval << 32) | IPR_PCII_IOA_TRANS_TO_OPER;
  6209. writeq(maskval, ioa_cfg->regs.set_interrupt_mask_reg);
  6210. int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
  6211. return IPR_RC_JOB_CONTINUE;
  6212. }
  6213. ipr_cmd->timer.data = (unsigned long) ipr_cmd;
  6214. ipr_cmd->timer.expires = jiffies + stage_time * HZ;
  6215. ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
  6216. ipr_cmd->done = ipr_reset_ioa_job;
  6217. add_timer(&ipr_cmd->timer);
  6218. list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
  6219. return IPR_RC_JOB_RETURN;
  6220. }
  6221. /**
  6222. * ipr_reset_enable_ioa - Enable the IOA following a reset.
  6223. * @ipr_cmd: ipr command struct
  6224. *
  6225. * This function reinitializes some control blocks and
  6226. * enables destructive diagnostics on the adapter.
  6227. *
  6228. * Return value:
  6229. * IPR_RC_JOB_RETURN
  6230. **/
  6231. static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
  6232. {
  6233. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6234. volatile u32 int_reg;
  6235. ENTER;
  6236. ipr_cmd->job_step = ipr_ioafp_identify_hrrq;
  6237. ipr_init_ioa_mem(ioa_cfg);
  6238. ioa_cfg->allow_interrupts = 1;
  6239. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
  6240. if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
  6241. writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
  6242. ioa_cfg->regs.clr_interrupt_mask_reg32);
  6243. int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
  6244. return IPR_RC_JOB_CONTINUE;
  6245. }
  6246. /* Enable destructive diagnostics on IOA */
  6247. writel(ioa_cfg->doorbell, ioa_cfg->regs.set_uproc_interrupt_reg32);
  6248. writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg32);
  6249. if (ioa_cfg->sis64)
  6250. writel(IPR_PCII_IPL_STAGE_CHANGE, ioa_cfg->regs.clr_interrupt_mask_reg);
  6251. int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
  6252. dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
  6253. if (ioa_cfg->sis64) {
  6254. ipr_cmd->job_step = ipr_reset_next_stage;
  6255. return IPR_RC_JOB_CONTINUE;
  6256. }
  6257. ipr_cmd->timer.data = (unsigned long) ipr_cmd;
  6258. ipr_cmd->timer.expires = jiffies + (ioa_cfg->transop_timeout * HZ);
  6259. ipr_cmd->timer.function = (void (*)(unsigned long))ipr_oper_timeout;
  6260. ipr_cmd->done = ipr_reset_ioa_job;
  6261. add_timer(&ipr_cmd->timer);
  6262. list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
  6263. LEAVE;
  6264. return IPR_RC_JOB_RETURN;
  6265. }
  6266. /**
  6267. * ipr_reset_wait_for_dump - Wait for a dump to timeout.
  6268. * @ipr_cmd: ipr command struct
  6269. *
  6270. * This function is invoked when an adapter dump has run out
  6271. * of processing time.
  6272. *
  6273. * Return value:
  6274. * IPR_RC_JOB_CONTINUE
  6275. **/
  6276. static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
  6277. {
  6278. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6279. if (ioa_cfg->sdt_state == GET_DUMP)
  6280. ioa_cfg->sdt_state = ABORT_DUMP;
  6281. ipr_cmd->job_step = ipr_reset_alert;
  6282. return IPR_RC_JOB_CONTINUE;
  6283. }
  6284. /**
  6285. * ipr_unit_check_no_data - Log a unit check/no data error log
  6286. * @ioa_cfg: ioa config struct
  6287. *
  6288. * Logs an error indicating the adapter unit checked, but for some
  6289. * reason, we were unable to fetch the unit check buffer.
  6290. *
  6291. * Return value:
  6292. * nothing
  6293. **/
  6294. static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
  6295. {
  6296. ioa_cfg->errors_logged++;
  6297. dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
  6298. }
  6299. /**
  6300. * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
  6301. * @ioa_cfg: ioa config struct
  6302. *
  6303. * Fetches the unit check buffer from the adapter by clocking the data
  6304. * through the mailbox register.
  6305. *
  6306. * Return value:
  6307. * nothing
  6308. **/
  6309. static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
  6310. {
  6311. unsigned long mailbox;
  6312. struct ipr_hostrcb *hostrcb;
  6313. struct ipr_uc_sdt sdt;
  6314. int rc, length;
  6315. u32 ioasc;
  6316. mailbox = readl(ioa_cfg->ioa_mailbox);
  6317. if (!ioa_cfg->sis64 && !ipr_sdt_is_fmt2(mailbox)) {
  6318. ipr_unit_check_no_data(ioa_cfg);
  6319. return;
  6320. }
  6321. memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
  6322. rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (__be32 *) &sdt,
  6323. (sizeof(struct ipr_uc_sdt)) / sizeof(__be32));
  6324. if (rc || !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY) ||
  6325. ((be32_to_cpu(sdt.hdr.state) != IPR_FMT3_SDT_READY_TO_USE) &&
  6326. (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE))) {
  6327. ipr_unit_check_no_data(ioa_cfg);
  6328. return;
  6329. }
  6330. /* Find length of the first sdt entry (UC buffer) */
  6331. if (be32_to_cpu(sdt.hdr.state) == IPR_FMT3_SDT_READY_TO_USE)
  6332. length = be32_to_cpu(sdt.entry[0].end_token);
  6333. else
  6334. length = (be32_to_cpu(sdt.entry[0].end_token) -
  6335. be32_to_cpu(sdt.entry[0].start_token)) &
  6336. IPR_FMT2_MBX_ADDR_MASK;
  6337. hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
  6338. struct ipr_hostrcb, queue);
  6339. list_del(&hostrcb->queue);
  6340. memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
  6341. rc = ipr_get_ldump_data_section(ioa_cfg,
  6342. be32_to_cpu(sdt.entry[0].start_token),
  6343. (__be32 *)&hostrcb->hcam,
  6344. min(length, (int)sizeof(hostrcb->hcam)) / sizeof(__be32));
  6345. if (!rc) {
  6346. ipr_handle_log_data(ioa_cfg, hostrcb);
  6347. ioasc = be32_to_cpu(hostrcb->hcam.u.error.fd_ioasc);
  6348. if (ioasc == IPR_IOASC_NR_IOA_RESET_REQUIRED &&
  6349. ioa_cfg->sdt_state == GET_DUMP)
  6350. ioa_cfg->sdt_state = WAIT_FOR_DUMP;
  6351. } else
  6352. ipr_unit_check_no_data(ioa_cfg);
  6353. list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
  6354. }
  6355. /**
  6356. * ipr_reset_restore_cfg_space - Restore PCI config space.
  6357. * @ipr_cmd: ipr command struct
  6358. *
  6359. * Description: This function restores the saved PCI config space of
  6360. * the adapter, fails all outstanding ops back to the callers, and
  6361. * fetches the dump/unit check if applicable to this reset.
  6362. *
  6363. * Return value:
  6364. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6365. **/
  6366. static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
  6367. {
  6368. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6369. int rc;
  6370. ENTER;
  6371. ioa_cfg->pdev->state_saved = true;
  6372. rc = pci_restore_state(ioa_cfg->pdev);
  6373. if (rc != PCIBIOS_SUCCESSFUL) {
  6374. ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
  6375. return IPR_RC_JOB_CONTINUE;
  6376. }
  6377. if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
  6378. ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
  6379. return IPR_RC_JOB_CONTINUE;
  6380. }
  6381. ipr_fail_all_ops(ioa_cfg);
  6382. if (ioa_cfg->ioa_unit_checked) {
  6383. ioa_cfg->ioa_unit_checked = 0;
  6384. ipr_get_unit_check_buffer(ioa_cfg);
  6385. ipr_cmd->job_step = ipr_reset_alert;
  6386. ipr_reset_start_timer(ipr_cmd, 0);
  6387. return IPR_RC_JOB_RETURN;
  6388. }
  6389. if (ioa_cfg->in_ioa_bringdown) {
  6390. ipr_cmd->job_step = ipr_ioa_bringdown_done;
  6391. } else {
  6392. ipr_cmd->job_step = ipr_reset_enable_ioa;
  6393. if (GET_DUMP == ioa_cfg->sdt_state) {
  6394. ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
  6395. ipr_cmd->job_step = ipr_reset_wait_for_dump;
  6396. schedule_work(&ioa_cfg->work_q);
  6397. return IPR_RC_JOB_RETURN;
  6398. }
  6399. }
  6400. ENTER;
  6401. return IPR_RC_JOB_CONTINUE;
  6402. }
  6403. /**
  6404. * ipr_reset_bist_done - BIST has completed on the adapter.
  6405. * @ipr_cmd: ipr command struct
  6406. *
  6407. * Description: Unblock config space and resume the reset process.
  6408. *
  6409. * Return value:
  6410. * IPR_RC_JOB_CONTINUE
  6411. **/
  6412. static int ipr_reset_bist_done(struct ipr_cmnd *ipr_cmd)
  6413. {
  6414. ENTER;
  6415. pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
  6416. ipr_cmd->job_step = ipr_reset_restore_cfg_space;
  6417. LEAVE;
  6418. return IPR_RC_JOB_CONTINUE;
  6419. }
  6420. /**
  6421. * ipr_reset_start_bist - Run BIST on the adapter.
  6422. * @ipr_cmd: ipr command struct
  6423. *
  6424. * Description: This function runs BIST on the adapter, then delays 2 seconds.
  6425. *
  6426. * Return value:
  6427. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6428. **/
  6429. static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
  6430. {
  6431. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6432. int rc;
  6433. ENTER;
  6434. pci_block_user_cfg_access(ioa_cfg->pdev);
  6435. rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
  6436. if (rc != PCIBIOS_SUCCESSFUL) {
  6437. pci_unblock_user_cfg_access(ipr_cmd->ioa_cfg->pdev);
  6438. ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
  6439. rc = IPR_RC_JOB_CONTINUE;
  6440. } else {
  6441. ipr_cmd->job_step = ipr_reset_bist_done;
  6442. ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
  6443. rc = IPR_RC_JOB_RETURN;
  6444. }
  6445. LEAVE;
  6446. return rc;
  6447. }
  6448. /**
  6449. * ipr_reset_slot_reset_done - Clear PCI reset to the adapter
  6450. * @ipr_cmd: ipr command struct
  6451. *
  6452. * Description: This clears PCI reset to the adapter and delays two seconds.
  6453. *
  6454. * Return value:
  6455. * IPR_RC_JOB_RETURN
  6456. **/
  6457. static int ipr_reset_slot_reset_done(struct ipr_cmnd *ipr_cmd)
  6458. {
  6459. ENTER;
  6460. pci_set_pcie_reset_state(ipr_cmd->ioa_cfg->pdev, pcie_deassert_reset);
  6461. ipr_cmd->job_step = ipr_reset_bist_done;
  6462. ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
  6463. LEAVE;
  6464. return IPR_RC_JOB_RETURN;
  6465. }
  6466. /**
  6467. * ipr_reset_slot_reset - Reset the PCI slot of the adapter.
  6468. * @ipr_cmd: ipr command struct
  6469. *
  6470. * Description: This asserts PCI reset to the adapter.
  6471. *
  6472. * Return value:
  6473. * IPR_RC_JOB_RETURN
  6474. **/
  6475. static int ipr_reset_slot_reset(struct ipr_cmnd *ipr_cmd)
  6476. {
  6477. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6478. struct pci_dev *pdev = ioa_cfg->pdev;
  6479. ENTER;
  6480. pci_block_user_cfg_access(pdev);
  6481. pci_set_pcie_reset_state(pdev, pcie_warm_reset);
  6482. ipr_cmd->job_step = ipr_reset_slot_reset_done;
  6483. ipr_reset_start_timer(ipr_cmd, IPR_PCI_RESET_TIMEOUT);
  6484. LEAVE;
  6485. return IPR_RC_JOB_RETURN;
  6486. }
  6487. /**
  6488. * ipr_reset_allowed - Query whether or not IOA can be reset
  6489. * @ioa_cfg: ioa config struct
  6490. *
  6491. * Return value:
  6492. * 0 if reset not allowed / non-zero if reset is allowed
  6493. **/
  6494. static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
  6495. {
  6496. volatile u32 temp_reg;
  6497. temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
  6498. return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
  6499. }
  6500. /**
  6501. * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
  6502. * @ipr_cmd: ipr command struct
  6503. *
  6504. * Description: This function waits for adapter permission to run BIST,
  6505. * then runs BIST. If the adapter does not give permission after a
  6506. * reasonable time, we will reset the adapter anyway. The impact of
  6507. * resetting the adapter without warning the adapter is the risk of
  6508. * losing the persistent error log on the adapter. If the adapter is
  6509. * reset while it is writing to the flash on the adapter, the flash
  6510. * segment will have bad ECC and be zeroed.
  6511. *
  6512. * Return value:
  6513. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6514. **/
  6515. static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
  6516. {
  6517. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6518. int rc = IPR_RC_JOB_RETURN;
  6519. if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
  6520. ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
  6521. ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
  6522. } else {
  6523. ipr_cmd->job_step = ioa_cfg->reset;
  6524. rc = IPR_RC_JOB_CONTINUE;
  6525. }
  6526. return rc;
  6527. }
  6528. /**
  6529. * ipr_reset_alert_part2 - Alert the adapter of a pending reset
  6530. * @ipr_cmd: ipr command struct
  6531. *
  6532. * Description: This function alerts the adapter that it will be reset.
  6533. * If memory space is not currently enabled, proceed directly
  6534. * to running BIST on the adapter. The timer must always be started
  6535. * so we guarantee we do not run BIST from ipr_isr.
  6536. *
  6537. * Return value:
  6538. * IPR_RC_JOB_RETURN
  6539. **/
  6540. static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
  6541. {
  6542. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6543. u16 cmd_reg;
  6544. int rc;
  6545. ENTER;
  6546. rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
  6547. if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
  6548. ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
  6549. writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg32);
  6550. ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
  6551. } else {
  6552. ipr_cmd->job_step = ioa_cfg->reset;
  6553. }
  6554. ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
  6555. ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
  6556. LEAVE;
  6557. return IPR_RC_JOB_RETURN;
  6558. }
  6559. /**
  6560. * ipr_reset_ucode_download_done - Microcode download completion
  6561. * @ipr_cmd: ipr command struct
  6562. *
  6563. * Description: This function unmaps the microcode download buffer.
  6564. *
  6565. * Return value:
  6566. * IPR_RC_JOB_CONTINUE
  6567. **/
  6568. static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
  6569. {
  6570. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6571. struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
  6572. pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
  6573. sglist->num_sg, DMA_TO_DEVICE);
  6574. ipr_cmd->job_step = ipr_reset_alert;
  6575. return IPR_RC_JOB_CONTINUE;
  6576. }
  6577. /**
  6578. * ipr_reset_ucode_download - Download microcode to the adapter
  6579. * @ipr_cmd: ipr command struct
  6580. *
  6581. * Description: This function checks to see if it there is microcode
  6582. * to download to the adapter. If there is, a download is performed.
  6583. *
  6584. * Return value:
  6585. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6586. **/
  6587. static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
  6588. {
  6589. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6590. struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
  6591. ENTER;
  6592. ipr_cmd->job_step = ipr_reset_alert;
  6593. if (!sglist)
  6594. return IPR_RC_JOB_CONTINUE;
  6595. ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  6596. ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
  6597. ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
  6598. ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
  6599. ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
  6600. ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
  6601. ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
  6602. if (ioa_cfg->sis64)
  6603. ipr_build_ucode_ioadl64(ipr_cmd, sglist);
  6604. else
  6605. ipr_build_ucode_ioadl(ipr_cmd, sglist);
  6606. ipr_cmd->job_step = ipr_reset_ucode_download_done;
  6607. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
  6608. IPR_WRITE_BUFFER_TIMEOUT);
  6609. LEAVE;
  6610. return IPR_RC_JOB_RETURN;
  6611. }
  6612. /**
  6613. * ipr_reset_shutdown_ioa - Shutdown the adapter
  6614. * @ipr_cmd: ipr command struct
  6615. *
  6616. * Description: This function issues an adapter shutdown of the
  6617. * specified type to the specified adapter as part of the
  6618. * adapter reset job.
  6619. *
  6620. * Return value:
  6621. * IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
  6622. **/
  6623. static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
  6624. {
  6625. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6626. enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
  6627. unsigned long timeout;
  6628. int rc = IPR_RC_JOB_CONTINUE;
  6629. ENTER;
  6630. if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
  6631. ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  6632. ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
  6633. ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
  6634. ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
  6635. if (shutdown_type == IPR_SHUTDOWN_NORMAL)
  6636. timeout = IPR_SHUTDOWN_TIMEOUT;
  6637. else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
  6638. timeout = IPR_INTERNAL_TIMEOUT;
  6639. else if (ioa_cfg->dual_raid && ipr_dual_ioa_raid)
  6640. timeout = IPR_DUAL_IOA_ABBR_SHUTDOWN_TO;
  6641. else
  6642. timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
  6643. ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
  6644. rc = IPR_RC_JOB_RETURN;
  6645. ipr_cmd->job_step = ipr_reset_ucode_download;
  6646. } else
  6647. ipr_cmd->job_step = ipr_reset_alert;
  6648. LEAVE;
  6649. return rc;
  6650. }
  6651. /**
  6652. * ipr_reset_ioa_job - Adapter reset job
  6653. * @ipr_cmd: ipr command struct
  6654. *
  6655. * Description: This function is the job router for the adapter reset job.
  6656. *
  6657. * Return value:
  6658. * none
  6659. **/
  6660. static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
  6661. {
  6662. u32 rc, ioasc;
  6663. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  6664. do {
  6665. ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
  6666. if (ioa_cfg->reset_cmd != ipr_cmd) {
  6667. /*
  6668. * We are doing nested adapter resets and this is
  6669. * not the current reset job.
  6670. */
  6671. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  6672. return;
  6673. }
  6674. if (IPR_IOASC_SENSE_KEY(ioasc)) {
  6675. rc = ipr_cmd->job_step_failed(ipr_cmd);
  6676. if (rc == IPR_RC_JOB_RETURN)
  6677. return;
  6678. }
  6679. ipr_reinit_ipr_cmnd(ipr_cmd);
  6680. ipr_cmd->job_step_failed = ipr_reset_cmd_failed;
  6681. rc = ipr_cmd->job_step(ipr_cmd);
  6682. } while(rc == IPR_RC_JOB_CONTINUE);
  6683. }
  6684. /**
  6685. * _ipr_initiate_ioa_reset - Initiate an adapter reset
  6686. * @ioa_cfg: ioa config struct
  6687. * @job_step: first job step of reset job
  6688. * @shutdown_type: shutdown type
  6689. *
  6690. * Description: This function will initiate the reset of the given adapter
  6691. * starting at the selected job step.
  6692. * If the caller needs to wait on the completion of the reset,
  6693. * the caller must sleep on the reset_wait_q.
  6694. *
  6695. * Return value:
  6696. * none
  6697. **/
  6698. static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
  6699. int (*job_step) (struct ipr_cmnd *),
  6700. enum ipr_shutdown_type shutdown_type)
  6701. {
  6702. struct ipr_cmnd *ipr_cmd;
  6703. ioa_cfg->in_reset_reload = 1;
  6704. ioa_cfg->allow_cmds = 0;
  6705. scsi_block_requests(ioa_cfg->host);
  6706. ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  6707. ioa_cfg->reset_cmd = ipr_cmd;
  6708. ipr_cmd->job_step = job_step;
  6709. ipr_cmd->u.shutdown_type = shutdown_type;
  6710. ipr_reset_ioa_job(ipr_cmd);
  6711. }
  6712. /**
  6713. * ipr_initiate_ioa_reset - Initiate an adapter reset
  6714. * @ioa_cfg: ioa config struct
  6715. * @shutdown_type: shutdown type
  6716. *
  6717. * Description: This function will initiate the reset of the given adapter.
  6718. * If the caller needs to wait on the completion of the reset,
  6719. * the caller must sleep on the reset_wait_q.
  6720. *
  6721. * Return value:
  6722. * none
  6723. **/
  6724. static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
  6725. enum ipr_shutdown_type shutdown_type)
  6726. {
  6727. if (ioa_cfg->ioa_is_dead)
  6728. return;
  6729. if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
  6730. ioa_cfg->sdt_state = ABORT_DUMP;
  6731. if (ioa_cfg->reset_retries++ >= IPR_NUM_RESET_RELOAD_RETRIES) {
  6732. dev_err(&ioa_cfg->pdev->dev,
  6733. "IOA taken offline - error recovery failed\n");
  6734. ioa_cfg->reset_retries = 0;
  6735. ioa_cfg->ioa_is_dead = 1;
  6736. if (ioa_cfg->in_ioa_bringdown) {
  6737. ioa_cfg->reset_cmd = NULL;
  6738. ioa_cfg->in_reset_reload = 0;
  6739. ipr_fail_all_ops(ioa_cfg);
  6740. wake_up_all(&ioa_cfg->reset_wait_q);
  6741. spin_unlock_irq(ioa_cfg->host->host_lock);
  6742. scsi_unblock_requests(ioa_cfg->host);
  6743. spin_lock_irq(ioa_cfg->host->host_lock);
  6744. return;
  6745. } else {
  6746. ioa_cfg->in_ioa_bringdown = 1;
  6747. shutdown_type = IPR_SHUTDOWN_NONE;
  6748. }
  6749. }
  6750. _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
  6751. shutdown_type);
  6752. }
  6753. /**
  6754. * ipr_reset_freeze - Hold off all I/O activity
  6755. * @ipr_cmd: ipr command struct
  6756. *
  6757. * Description: If the PCI slot is frozen, hold off all I/O
  6758. * activity; then, as soon as the slot is available again,
  6759. * initiate an adapter reset.
  6760. */
  6761. static int ipr_reset_freeze(struct ipr_cmnd *ipr_cmd)
  6762. {
  6763. /* Disallow new interrupts, avoid loop */
  6764. ipr_cmd->ioa_cfg->allow_interrupts = 0;
  6765. list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
  6766. ipr_cmd->done = ipr_reset_ioa_job;
  6767. return IPR_RC_JOB_RETURN;
  6768. }
  6769. /**
  6770. * ipr_pci_frozen - Called when slot has experienced a PCI bus error.
  6771. * @pdev: PCI device struct
  6772. *
  6773. * Description: This routine is called to tell us that the PCI bus
  6774. * is down. Can't do anything here, except put the device driver
  6775. * into a holding pattern, waiting for the PCI bus to come back.
  6776. */
  6777. static void ipr_pci_frozen(struct pci_dev *pdev)
  6778. {
  6779. unsigned long flags = 0;
  6780. struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
  6781. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  6782. _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_freeze, IPR_SHUTDOWN_NONE);
  6783. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  6784. }
  6785. /**
  6786. * ipr_pci_slot_reset - Called when PCI slot has been reset.
  6787. * @pdev: PCI device struct
  6788. *
  6789. * Description: This routine is called by the pci error recovery
  6790. * code after the PCI slot has been reset, just before we
  6791. * should resume normal operations.
  6792. */
  6793. static pci_ers_result_t ipr_pci_slot_reset(struct pci_dev *pdev)
  6794. {
  6795. unsigned long flags = 0;
  6796. struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
  6797. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  6798. if (ioa_cfg->needs_warm_reset)
  6799. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  6800. else
  6801. _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_restore_cfg_space,
  6802. IPR_SHUTDOWN_NONE);
  6803. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  6804. return PCI_ERS_RESULT_RECOVERED;
  6805. }
  6806. /**
  6807. * ipr_pci_perm_failure - Called when PCI slot is dead for good.
  6808. * @pdev: PCI device struct
  6809. *
  6810. * Description: This routine is called when the PCI bus has
  6811. * permanently failed.
  6812. */
  6813. static void ipr_pci_perm_failure(struct pci_dev *pdev)
  6814. {
  6815. unsigned long flags = 0;
  6816. struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
  6817. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  6818. if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
  6819. ioa_cfg->sdt_state = ABORT_DUMP;
  6820. ioa_cfg->reset_retries = IPR_NUM_RESET_RELOAD_RETRIES;
  6821. ioa_cfg->in_ioa_bringdown = 1;
  6822. ioa_cfg->allow_cmds = 0;
  6823. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  6824. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  6825. }
  6826. /**
  6827. * ipr_pci_error_detected - Called when a PCI error is detected.
  6828. * @pdev: PCI device struct
  6829. * @state: PCI channel state
  6830. *
  6831. * Description: Called when a PCI error is detected.
  6832. *
  6833. * Return value:
  6834. * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  6835. */
  6836. static pci_ers_result_t ipr_pci_error_detected(struct pci_dev *pdev,
  6837. pci_channel_state_t state)
  6838. {
  6839. switch (state) {
  6840. case pci_channel_io_frozen:
  6841. ipr_pci_frozen(pdev);
  6842. return PCI_ERS_RESULT_NEED_RESET;
  6843. case pci_channel_io_perm_failure:
  6844. ipr_pci_perm_failure(pdev);
  6845. return PCI_ERS_RESULT_DISCONNECT;
  6846. break;
  6847. default:
  6848. break;
  6849. }
  6850. return PCI_ERS_RESULT_NEED_RESET;
  6851. }
  6852. /**
  6853. * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
  6854. * @ioa_cfg: ioa cfg struct
  6855. *
  6856. * Description: This is the second phase of adapter intialization
  6857. * This function takes care of initilizing the adapter to the point
  6858. * where it can accept new commands.
  6859. * Return value:
  6860. * 0 on success / -EIO on failure
  6861. **/
  6862. static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
  6863. {
  6864. int rc = 0;
  6865. unsigned long host_lock_flags = 0;
  6866. ENTER;
  6867. spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
  6868. dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
  6869. if (ioa_cfg->needs_hard_reset) {
  6870. ioa_cfg->needs_hard_reset = 0;
  6871. ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
  6872. } else
  6873. _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa,
  6874. IPR_SHUTDOWN_NONE);
  6875. spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
  6876. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  6877. spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
  6878. if (ioa_cfg->ioa_is_dead) {
  6879. rc = -EIO;
  6880. } else if (ipr_invalid_adapter(ioa_cfg)) {
  6881. if (!ipr_testmode)
  6882. rc = -EIO;
  6883. dev_err(&ioa_cfg->pdev->dev,
  6884. "Adapter not supported in this hardware configuration.\n");
  6885. }
  6886. spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
  6887. LEAVE;
  6888. return rc;
  6889. }
  6890. /**
  6891. * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
  6892. * @ioa_cfg: ioa config struct
  6893. *
  6894. * Return value:
  6895. * none
  6896. **/
  6897. static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
  6898. {
  6899. int i;
  6900. for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
  6901. if (ioa_cfg->ipr_cmnd_list[i])
  6902. pci_pool_free(ioa_cfg->ipr_cmd_pool,
  6903. ioa_cfg->ipr_cmnd_list[i],
  6904. ioa_cfg->ipr_cmnd_list_dma[i]);
  6905. ioa_cfg->ipr_cmnd_list[i] = NULL;
  6906. }
  6907. if (ioa_cfg->ipr_cmd_pool)
  6908. pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
  6909. ioa_cfg->ipr_cmd_pool = NULL;
  6910. }
  6911. /**
  6912. * ipr_free_mem - Frees memory allocated for an adapter
  6913. * @ioa_cfg: ioa cfg struct
  6914. *
  6915. * Return value:
  6916. * nothing
  6917. **/
  6918. static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
  6919. {
  6920. int i;
  6921. kfree(ioa_cfg->res_entries);
  6922. pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
  6923. ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
  6924. ipr_free_cmd_blks(ioa_cfg);
  6925. pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
  6926. ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
  6927. pci_free_consistent(ioa_cfg->pdev, ioa_cfg->cfg_table_size,
  6928. ioa_cfg->u.cfg_table,
  6929. ioa_cfg->cfg_table_dma);
  6930. for (i = 0; i < IPR_NUM_HCAMS; i++) {
  6931. pci_free_consistent(ioa_cfg->pdev,
  6932. sizeof(struct ipr_hostrcb),
  6933. ioa_cfg->hostrcb[i],
  6934. ioa_cfg->hostrcb_dma[i]);
  6935. }
  6936. ipr_free_dump(ioa_cfg);
  6937. kfree(ioa_cfg->trace);
  6938. }
  6939. /**
  6940. * ipr_free_all_resources - Free all allocated resources for an adapter.
  6941. * @ipr_cmd: ipr command struct
  6942. *
  6943. * This function frees all allocated resources for the
  6944. * specified adapter.
  6945. *
  6946. * Return value:
  6947. * none
  6948. **/
  6949. static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
  6950. {
  6951. struct pci_dev *pdev = ioa_cfg->pdev;
  6952. ENTER;
  6953. free_irq(pdev->irq, ioa_cfg);
  6954. pci_disable_msi(pdev);
  6955. iounmap(ioa_cfg->hdw_dma_regs);
  6956. pci_release_regions(pdev);
  6957. ipr_free_mem(ioa_cfg);
  6958. scsi_host_put(ioa_cfg->host);
  6959. pci_disable_device(pdev);
  6960. LEAVE;
  6961. }
  6962. /**
  6963. * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
  6964. * @ioa_cfg: ioa config struct
  6965. *
  6966. * Return value:
  6967. * 0 on success / -ENOMEM on allocation failure
  6968. **/
  6969. static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
  6970. {
  6971. struct ipr_cmnd *ipr_cmd;
  6972. struct ipr_ioarcb *ioarcb;
  6973. dma_addr_t dma_addr;
  6974. int i;
  6975. ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
  6976. sizeof(struct ipr_cmnd), 16, 0);
  6977. if (!ioa_cfg->ipr_cmd_pool)
  6978. return -ENOMEM;
  6979. for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
  6980. ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, GFP_KERNEL, &dma_addr);
  6981. if (!ipr_cmd) {
  6982. ipr_free_cmd_blks(ioa_cfg);
  6983. return -ENOMEM;
  6984. }
  6985. memset(ipr_cmd, 0, sizeof(*ipr_cmd));
  6986. ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
  6987. ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
  6988. ioarcb = &ipr_cmd->ioarcb;
  6989. ipr_cmd->dma_addr = dma_addr;
  6990. if (ioa_cfg->sis64)
  6991. ioarcb->a.ioarcb_host_pci_addr64 = cpu_to_be64(dma_addr);
  6992. else
  6993. ioarcb->a.ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
  6994. ioarcb->host_response_handle = cpu_to_be32(i << 2);
  6995. if (ioa_cfg->sis64) {
  6996. ioarcb->u.sis64_addr_data.data_ioadl_addr =
  6997. cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, i.ioadl64));
  6998. ioarcb->u.sis64_addr_data.ioasa_host_pci_addr =
  6999. cpu_to_be64(dma_addr + offsetof(struct ipr_cmnd, ioasa));
  7000. } else {
  7001. ioarcb->write_ioadl_addr =
  7002. cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, i.ioadl));
  7003. ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
  7004. ioarcb->ioasa_host_pci_addr =
  7005. cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
  7006. }
  7007. ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
  7008. ipr_cmd->cmd_index = i;
  7009. ipr_cmd->ioa_cfg = ioa_cfg;
  7010. ipr_cmd->sense_buffer_dma = dma_addr +
  7011. offsetof(struct ipr_cmnd, sense_buffer);
  7012. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  7013. }
  7014. return 0;
  7015. }
  7016. /**
  7017. * ipr_alloc_mem - Allocate memory for an adapter
  7018. * @ioa_cfg: ioa config struct
  7019. *
  7020. * Return value:
  7021. * 0 on success / non-zero for error
  7022. **/
  7023. static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
  7024. {
  7025. struct pci_dev *pdev = ioa_cfg->pdev;
  7026. int i, rc = -ENOMEM;
  7027. ENTER;
  7028. ioa_cfg->res_entries = kzalloc(sizeof(struct ipr_resource_entry) *
  7029. ioa_cfg->max_devs_supported, GFP_KERNEL);
  7030. if (!ioa_cfg->res_entries)
  7031. goto out;
  7032. if (ioa_cfg->sis64) {
  7033. ioa_cfg->target_ids = kzalloc(sizeof(unsigned long) *
  7034. BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
  7035. ioa_cfg->array_ids = kzalloc(sizeof(unsigned long) *
  7036. BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
  7037. ioa_cfg->vset_ids = kzalloc(sizeof(unsigned long) *
  7038. BITS_TO_LONGS(ioa_cfg->max_devs_supported), GFP_KERNEL);
  7039. }
  7040. for (i = 0; i < ioa_cfg->max_devs_supported; i++) {
  7041. list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
  7042. ioa_cfg->res_entries[i].ioa_cfg = ioa_cfg;
  7043. }
  7044. ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
  7045. sizeof(struct ipr_misc_cbs),
  7046. &ioa_cfg->vpd_cbs_dma);
  7047. if (!ioa_cfg->vpd_cbs)
  7048. goto out_free_res_entries;
  7049. if (ipr_alloc_cmd_blks(ioa_cfg))
  7050. goto out_free_vpd_cbs;
  7051. ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
  7052. sizeof(u32) * IPR_NUM_CMD_BLKS,
  7053. &ioa_cfg->host_rrq_dma);
  7054. if (!ioa_cfg->host_rrq)
  7055. goto out_ipr_free_cmd_blocks;
  7056. ioa_cfg->u.cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
  7057. ioa_cfg->cfg_table_size,
  7058. &ioa_cfg->cfg_table_dma);
  7059. if (!ioa_cfg->u.cfg_table)
  7060. goto out_free_host_rrq;
  7061. for (i = 0; i < IPR_NUM_HCAMS; i++) {
  7062. ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
  7063. sizeof(struct ipr_hostrcb),
  7064. &ioa_cfg->hostrcb_dma[i]);
  7065. if (!ioa_cfg->hostrcb[i])
  7066. goto out_free_hostrcb_dma;
  7067. ioa_cfg->hostrcb[i]->hostrcb_dma =
  7068. ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
  7069. ioa_cfg->hostrcb[i]->ioa_cfg = ioa_cfg;
  7070. list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
  7071. }
  7072. ioa_cfg->trace = kzalloc(sizeof(struct ipr_trace_entry) *
  7073. IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
  7074. if (!ioa_cfg->trace)
  7075. goto out_free_hostrcb_dma;
  7076. rc = 0;
  7077. out:
  7078. LEAVE;
  7079. return rc;
  7080. out_free_hostrcb_dma:
  7081. while (i-- > 0) {
  7082. pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
  7083. ioa_cfg->hostrcb[i],
  7084. ioa_cfg->hostrcb_dma[i]);
  7085. }
  7086. pci_free_consistent(pdev, ioa_cfg->cfg_table_size,
  7087. ioa_cfg->u.cfg_table,
  7088. ioa_cfg->cfg_table_dma);
  7089. out_free_host_rrq:
  7090. pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
  7091. ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
  7092. out_ipr_free_cmd_blocks:
  7093. ipr_free_cmd_blks(ioa_cfg);
  7094. out_free_vpd_cbs:
  7095. pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
  7096. ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
  7097. out_free_res_entries:
  7098. kfree(ioa_cfg->res_entries);
  7099. goto out;
  7100. }
  7101. /**
  7102. * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
  7103. * @ioa_cfg: ioa config struct
  7104. *
  7105. * Return value:
  7106. * none
  7107. **/
  7108. static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
  7109. {
  7110. int i;
  7111. for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
  7112. ioa_cfg->bus_attr[i].bus = i;
  7113. ioa_cfg->bus_attr[i].qas_enabled = 0;
  7114. ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
  7115. if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
  7116. ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
  7117. else
  7118. ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
  7119. }
  7120. }
  7121. /**
  7122. * ipr_init_ioa_cfg - Initialize IOA config struct
  7123. * @ioa_cfg: ioa config struct
  7124. * @host: scsi host struct
  7125. * @pdev: PCI dev struct
  7126. *
  7127. * Return value:
  7128. * none
  7129. **/
  7130. static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
  7131. struct Scsi_Host *host, struct pci_dev *pdev)
  7132. {
  7133. const struct ipr_interrupt_offsets *p;
  7134. struct ipr_interrupts *t;
  7135. void __iomem *base;
  7136. ioa_cfg->host = host;
  7137. ioa_cfg->pdev = pdev;
  7138. ioa_cfg->log_level = ipr_log_level;
  7139. ioa_cfg->doorbell = IPR_DOORBELL;
  7140. sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
  7141. sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
  7142. sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
  7143. sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
  7144. sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
  7145. sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
  7146. sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
  7147. sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
  7148. INIT_LIST_HEAD(&ioa_cfg->free_q);
  7149. INIT_LIST_HEAD(&ioa_cfg->pending_q);
  7150. INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
  7151. INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
  7152. INIT_LIST_HEAD(&ioa_cfg->free_res_q);
  7153. INIT_LIST_HEAD(&ioa_cfg->used_res_q);
  7154. INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread);
  7155. init_waitqueue_head(&ioa_cfg->reset_wait_q);
  7156. init_waitqueue_head(&ioa_cfg->msi_wait_q);
  7157. ioa_cfg->sdt_state = INACTIVE;
  7158. ipr_initialize_bus_attr(ioa_cfg);
  7159. ioa_cfg->max_devs_supported = ipr_max_devs;
  7160. if (ioa_cfg->sis64) {
  7161. host->max_id = IPR_MAX_SIS64_TARGETS_PER_BUS;
  7162. host->max_lun = IPR_MAX_SIS64_LUNS_PER_TARGET;
  7163. if (ipr_max_devs > IPR_MAX_SIS64_DEVS)
  7164. ioa_cfg->max_devs_supported = IPR_MAX_SIS64_DEVS;
  7165. } else {
  7166. host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
  7167. host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
  7168. if (ipr_max_devs > IPR_MAX_PHYSICAL_DEVS)
  7169. ioa_cfg->max_devs_supported = IPR_MAX_PHYSICAL_DEVS;
  7170. }
  7171. host->max_channel = IPR_MAX_BUS_TO_SCAN;
  7172. host->unique_id = host->host_no;
  7173. host->max_cmd_len = IPR_MAX_CDB_LEN;
  7174. pci_set_drvdata(pdev, ioa_cfg);
  7175. p = &ioa_cfg->chip_cfg->regs;
  7176. t = &ioa_cfg->regs;
  7177. base = ioa_cfg->hdw_dma_regs;
  7178. t->set_interrupt_mask_reg = base + p->set_interrupt_mask_reg;
  7179. t->clr_interrupt_mask_reg = base + p->clr_interrupt_mask_reg;
  7180. t->clr_interrupt_mask_reg32 = base + p->clr_interrupt_mask_reg32;
  7181. t->sense_interrupt_mask_reg = base + p->sense_interrupt_mask_reg;
  7182. t->sense_interrupt_mask_reg32 = base + p->sense_interrupt_mask_reg32;
  7183. t->clr_interrupt_reg = base + p->clr_interrupt_reg;
  7184. t->clr_interrupt_reg32 = base + p->clr_interrupt_reg32;
  7185. t->sense_interrupt_reg = base + p->sense_interrupt_reg;
  7186. t->sense_interrupt_reg32 = base + p->sense_interrupt_reg32;
  7187. t->ioarrin_reg = base + p->ioarrin_reg;
  7188. t->sense_uproc_interrupt_reg = base + p->sense_uproc_interrupt_reg;
  7189. t->sense_uproc_interrupt_reg32 = base + p->sense_uproc_interrupt_reg32;
  7190. t->set_uproc_interrupt_reg = base + p->set_uproc_interrupt_reg;
  7191. t->set_uproc_interrupt_reg32 = base + p->set_uproc_interrupt_reg32;
  7192. t->clr_uproc_interrupt_reg = base + p->clr_uproc_interrupt_reg;
  7193. t->clr_uproc_interrupt_reg32 = base + p->clr_uproc_interrupt_reg32;
  7194. if (ioa_cfg->sis64) {
  7195. t->init_feedback_reg = base + p->init_feedback_reg;
  7196. t->dump_addr_reg = base + p->dump_addr_reg;
  7197. t->dump_data_reg = base + p->dump_data_reg;
  7198. }
  7199. }
  7200. /**
  7201. * ipr_get_chip_info - Find adapter chip information
  7202. * @dev_id: PCI device id struct
  7203. *
  7204. * Return value:
  7205. * ptr to chip information on success / NULL on failure
  7206. **/
  7207. static const struct ipr_chip_t * __devinit
  7208. ipr_get_chip_info(const struct pci_device_id *dev_id)
  7209. {
  7210. int i;
  7211. for (i = 0; i < ARRAY_SIZE(ipr_chip); i++)
  7212. if (ipr_chip[i].vendor == dev_id->vendor &&
  7213. ipr_chip[i].device == dev_id->device)
  7214. return &ipr_chip[i];
  7215. return NULL;
  7216. }
  7217. /**
  7218. * ipr_test_intr - Handle the interrupt generated in ipr_test_msi().
  7219. * @pdev: PCI device struct
  7220. *
  7221. * Description: Simply set the msi_received flag to 1 indicating that
  7222. * Message Signaled Interrupts are supported.
  7223. *
  7224. * Return value:
  7225. * 0 on success / non-zero on failure
  7226. **/
  7227. static irqreturn_t __devinit ipr_test_intr(int irq, void *devp)
  7228. {
  7229. struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
  7230. unsigned long lock_flags = 0;
  7231. irqreturn_t rc = IRQ_HANDLED;
  7232. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  7233. ioa_cfg->msi_received = 1;
  7234. wake_up(&ioa_cfg->msi_wait_q);
  7235. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  7236. return rc;
  7237. }
  7238. /**
  7239. * ipr_test_msi - Test for Message Signaled Interrupt (MSI) support.
  7240. * @pdev: PCI device struct
  7241. *
  7242. * Description: The return value from pci_enable_msi() can not always be
  7243. * trusted. This routine sets up and initiates a test interrupt to determine
  7244. * if the interrupt is received via the ipr_test_intr() service routine.
  7245. * If the tests fails, the driver will fall back to LSI.
  7246. *
  7247. * Return value:
  7248. * 0 on success / non-zero on failure
  7249. **/
  7250. static int __devinit ipr_test_msi(struct ipr_ioa_cfg *ioa_cfg,
  7251. struct pci_dev *pdev)
  7252. {
  7253. int rc;
  7254. volatile u32 int_reg;
  7255. unsigned long lock_flags = 0;
  7256. ENTER;
  7257. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  7258. init_waitqueue_head(&ioa_cfg->msi_wait_q);
  7259. ioa_cfg->msi_received = 0;
  7260. ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
  7261. writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.clr_interrupt_mask_reg32);
  7262. int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
  7263. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  7264. rc = request_irq(pdev->irq, ipr_test_intr, 0, IPR_NAME, ioa_cfg);
  7265. if (rc) {
  7266. dev_err(&pdev->dev, "Can not assign irq %d\n", pdev->irq);
  7267. return rc;
  7268. } else if (ipr_debug)
  7269. dev_info(&pdev->dev, "IRQ assigned: %d\n", pdev->irq);
  7270. writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE, ioa_cfg->regs.sense_interrupt_reg32);
  7271. int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
  7272. wait_event_timeout(ioa_cfg->msi_wait_q, ioa_cfg->msi_received, HZ);
  7273. ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
  7274. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  7275. if (!ioa_cfg->msi_received) {
  7276. /* MSI test failed */
  7277. dev_info(&pdev->dev, "MSI test failed. Falling back to LSI.\n");
  7278. rc = -EOPNOTSUPP;
  7279. } else if (ipr_debug)
  7280. dev_info(&pdev->dev, "MSI test succeeded.\n");
  7281. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  7282. free_irq(pdev->irq, ioa_cfg);
  7283. LEAVE;
  7284. return rc;
  7285. }
  7286. /**
  7287. * ipr_probe_ioa - Allocates memory and does first stage of initialization
  7288. * @pdev: PCI device struct
  7289. * @dev_id: PCI device id struct
  7290. *
  7291. * Return value:
  7292. * 0 on success / non-zero on failure
  7293. **/
  7294. static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
  7295. const struct pci_device_id *dev_id)
  7296. {
  7297. struct ipr_ioa_cfg *ioa_cfg;
  7298. struct Scsi_Host *host;
  7299. unsigned long ipr_regs_pci;
  7300. void __iomem *ipr_regs;
  7301. int rc = PCIBIOS_SUCCESSFUL;
  7302. volatile u32 mask, uproc, interrupts;
  7303. ENTER;
  7304. if ((rc = pci_enable_device(pdev))) {
  7305. dev_err(&pdev->dev, "Cannot enable adapter\n");
  7306. goto out;
  7307. }
  7308. dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
  7309. host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
  7310. if (!host) {
  7311. dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
  7312. rc = -ENOMEM;
  7313. goto out_disable;
  7314. }
  7315. ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
  7316. memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
  7317. ata_host_init(&ioa_cfg->ata_host, &pdev->dev,
  7318. sata_port_info.flags, &ipr_sata_ops);
  7319. ioa_cfg->ipr_chip = ipr_get_chip_info(dev_id);
  7320. if (!ioa_cfg->ipr_chip) {
  7321. dev_err(&pdev->dev, "Unknown adapter chipset 0x%04X 0x%04X\n",
  7322. dev_id->vendor, dev_id->device);
  7323. goto out_scsi_host_put;
  7324. }
  7325. /* set SIS 32 or SIS 64 */
  7326. ioa_cfg->sis64 = ioa_cfg->ipr_chip->sis_type == IPR_SIS64 ? 1 : 0;
  7327. ioa_cfg->chip_cfg = ioa_cfg->ipr_chip->cfg;
  7328. if (ipr_transop_timeout)
  7329. ioa_cfg->transop_timeout = ipr_transop_timeout;
  7330. else if (dev_id->driver_data & IPR_USE_LONG_TRANSOP_TIMEOUT)
  7331. ioa_cfg->transop_timeout = IPR_LONG_OPERATIONAL_TIMEOUT;
  7332. else
  7333. ioa_cfg->transop_timeout = IPR_OPERATIONAL_TIMEOUT;
  7334. ioa_cfg->revid = pdev->revision;
  7335. ipr_regs_pci = pci_resource_start(pdev, 0);
  7336. rc = pci_request_regions(pdev, IPR_NAME);
  7337. if (rc < 0) {
  7338. dev_err(&pdev->dev,
  7339. "Couldn't register memory range of registers\n");
  7340. goto out_scsi_host_put;
  7341. }
  7342. ipr_regs = pci_ioremap_bar(pdev, 0);
  7343. if (!ipr_regs) {
  7344. dev_err(&pdev->dev,
  7345. "Couldn't map memory range of registers\n");
  7346. rc = -ENOMEM;
  7347. goto out_release_regions;
  7348. }
  7349. ioa_cfg->hdw_dma_regs = ipr_regs;
  7350. ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
  7351. ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
  7352. ipr_init_ioa_cfg(ioa_cfg, host, pdev);
  7353. pci_set_master(pdev);
  7354. if (ioa_cfg->sis64) {
  7355. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  7356. if (rc < 0) {
  7357. dev_dbg(&pdev->dev, "Failed to set 64 bit PCI DMA mask\n");
  7358. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  7359. }
  7360. } else
  7361. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  7362. if (rc < 0) {
  7363. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  7364. goto cleanup_nomem;
  7365. }
  7366. rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  7367. ioa_cfg->chip_cfg->cache_line_size);
  7368. if (rc != PCIBIOS_SUCCESSFUL) {
  7369. dev_err(&pdev->dev, "Write of cache line size failed\n");
  7370. rc = -EIO;
  7371. goto cleanup_nomem;
  7372. }
  7373. /* Enable MSI style interrupts if they are supported. */
  7374. if (ioa_cfg->ipr_chip->intr_type == IPR_USE_MSI && !pci_enable_msi(pdev)) {
  7375. rc = ipr_test_msi(ioa_cfg, pdev);
  7376. if (rc == -EOPNOTSUPP)
  7377. pci_disable_msi(pdev);
  7378. else if (rc)
  7379. goto out_msi_disable;
  7380. else
  7381. dev_info(&pdev->dev, "MSI enabled with IRQ: %d\n", pdev->irq);
  7382. } else if (ipr_debug)
  7383. dev_info(&pdev->dev, "Cannot enable MSI.\n");
  7384. /* Save away PCI config space for use following IOA reset */
  7385. rc = pci_save_state(pdev);
  7386. if (rc != PCIBIOS_SUCCESSFUL) {
  7387. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  7388. rc = -EIO;
  7389. goto cleanup_nomem;
  7390. }
  7391. if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
  7392. goto cleanup_nomem;
  7393. if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
  7394. goto cleanup_nomem;
  7395. if (ioa_cfg->sis64)
  7396. ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr64)
  7397. + ((sizeof(struct ipr_config_table_entry64)
  7398. * ioa_cfg->max_devs_supported)));
  7399. else
  7400. ioa_cfg->cfg_table_size = (sizeof(struct ipr_config_table_hdr)
  7401. + ((sizeof(struct ipr_config_table_entry)
  7402. * ioa_cfg->max_devs_supported)));
  7403. rc = ipr_alloc_mem(ioa_cfg);
  7404. if (rc < 0) {
  7405. dev_err(&pdev->dev,
  7406. "Couldn't allocate enough memory for device driver!\n");
  7407. goto cleanup_nomem;
  7408. }
  7409. /*
  7410. * If HRRQ updated interrupt is not masked, or reset alert is set,
  7411. * the card is in an unknown state and needs a hard reset
  7412. */
  7413. mask = readl(ioa_cfg->regs.sense_interrupt_mask_reg32);
  7414. interrupts = readl(ioa_cfg->regs.sense_interrupt_reg32);
  7415. uproc = readl(ioa_cfg->regs.sense_uproc_interrupt_reg32);
  7416. if ((mask & IPR_PCII_HRRQ_UPDATED) == 0 || (uproc & IPR_UPROCI_RESET_ALERT))
  7417. ioa_cfg->needs_hard_reset = 1;
  7418. if (interrupts & IPR_PCII_ERROR_INTERRUPTS)
  7419. ioa_cfg->needs_hard_reset = 1;
  7420. if (interrupts & IPR_PCII_IOA_UNIT_CHECKED)
  7421. ioa_cfg->ioa_unit_checked = 1;
  7422. ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
  7423. rc = request_irq(pdev->irq, ipr_isr,
  7424. ioa_cfg->msi_received ? 0 : IRQF_SHARED,
  7425. IPR_NAME, ioa_cfg);
  7426. if (rc) {
  7427. dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
  7428. pdev->irq, rc);
  7429. goto cleanup_nolog;
  7430. }
  7431. if ((dev_id->driver_data & IPR_USE_PCI_WARM_RESET) ||
  7432. (dev_id->device == PCI_DEVICE_ID_IBM_OBSIDIAN_E && !ioa_cfg->revid)) {
  7433. ioa_cfg->needs_warm_reset = 1;
  7434. ioa_cfg->reset = ipr_reset_slot_reset;
  7435. } else
  7436. ioa_cfg->reset = ipr_reset_start_bist;
  7437. spin_lock(&ipr_driver_lock);
  7438. list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
  7439. spin_unlock(&ipr_driver_lock);
  7440. LEAVE;
  7441. out:
  7442. return rc;
  7443. cleanup_nolog:
  7444. ipr_free_mem(ioa_cfg);
  7445. cleanup_nomem:
  7446. iounmap(ipr_regs);
  7447. out_msi_disable:
  7448. pci_disable_msi(pdev);
  7449. out_release_regions:
  7450. pci_release_regions(pdev);
  7451. out_scsi_host_put:
  7452. scsi_host_put(host);
  7453. out_disable:
  7454. pci_disable_device(pdev);
  7455. goto out;
  7456. }
  7457. /**
  7458. * ipr_scan_vsets - Scans for VSET devices
  7459. * @ioa_cfg: ioa config struct
  7460. *
  7461. * Description: Since the VSET resources do not follow SAM in that we can have
  7462. * sparse LUNs with no LUN 0, we have to scan for these ourselves.
  7463. *
  7464. * Return value:
  7465. * none
  7466. **/
  7467. static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
  7468. {
  7469. int target, lun;
  7470. for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
  7471. for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
  7472. scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
  7473. }
  7474. /**
  7475. * ipr_initiate_ioa_bringdown - Bring down an adapter
  7476. * @ioa_cfg: ioa config struct
  7477. * @shutdown_type: shutdown type
  7478. *
  7479. * Description: This function will initiate bringing down the adapter.
  7480. * This consists of issuing an IOA shutdown to the adapter
  7481. * to flush the cache, and running BIST.
  7482. * If the caller needs to wait on the completion of the reset,
  7483. * the caller must sleep on the reset_wait_q.
  7484. *
  7485. * Return value:
  7486. * none
  7487. **/
  7488. static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
  7489. enum ipr_shutdown_type shutdown_type)
  7490. {
  7491. ENTER;
  7492. if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
  7493. ioa_cfg->sdt_state = ABORT_DUMP;
  7494. ioa_cfg->reset_retries = 0;
  7495. ioa_cfg->in_ioa_bringdown = 1;
  7496. ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
  7497. LEAVE;
  7498. }
  7499. /**
  7500. * __ipr_remove - Remove a single adapter
  7501. * @pdev: pci device struct
  7502. *
  7503. * Adapter hot plug remove entry point.
  7504. *
  7505. * Return value:
  7506. * none
  7507. **/
  7508. static void __ipr_remove(struct pci_dev *pdev)
  7509. {
  7510. unsigned long host_lock_flags = 0;
  7511. struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
  7512. ENTER;
  7513. spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
  7514. while(ioa_cfg->in_reset_reload) {
  7515. spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
  7516. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  7517. spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
  7518. }
  7519. ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
  7520. spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
  7521. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  7522. flush_scheduled_work();
  7523. spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
  7524. spin_lock(&ipr_driver_lock);
  7525. list_del(&ioa_cfg->queue);
  7526. spin_unlock(&ipr_driver_lock);
  7527. if (ioa_cfg->sdt_state == ABORT_DUMP)
  7528. ioa_cfg->sdt_state = WAIT_FOR_DUMP;
  7529. spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
  7530. ipr_free_all_resources(ioa_cfg);
  7531. LEAVE;
  7532. }
  7533. /**
  7534. * ipr_remove - IOA hot plug remove entry point
  7535. * @pdev: pci device struct
  7536. *
  7537. * Adapter hot plug remove entry point.
  7538. *
  7539. * Return value:
  7540. * none
  7541. **/
  7542. static void __devexit ipr_remove(struct pci_dev *pdev)
  7543. {
  7544. struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
  7545. ENTER;
  7546. ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
  7547. &ipr_trace_attr);
  7548. ipr_remove_dump_file(&ioa_cfg->host->shost_dev.kobj,
  7549. &ipr_dump_attr);
  7550. scsi_remove_host(ioa_cfg->host);
  7551. __ipr_remove(pdev);
  7552. LEAVE;
  7553. }
  7554. /**
  7555. * ipr_probe - Adapter hot plug add entry point
  7556. *
  7557. * Return value:
  7558. * 0 on success / non-zero on failure
  7559. **/
  7560. static int __devinit ipr_probe(struct pci_dev *pdev,
  7561. const struct pci_device_id *dev_id)
  7562. {
  7563. struct ipr_ioa_cfg *ioa_cfg;
  7564. int rc;
  7565. rc = ipr_probe_ioa(pdev, dev_id);
  7566. if (rc)
  7567. return rc;
  7568. ioa_cfg = pci_get_drvdata(pdev);
  7569. rc = ipr_probe_ioa_part2(ioa_cfg);
  7570. if (rc) {
  7571. __ipr_remove(pdev);
  7572. return rc;
  7573. }
  7574. rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
  7575. if (rc) {
  7576. __ipr_remove(pdev);
  7577. return rc;
  7578. }
  7579. rc = ipr_create_trace_file(&ioa_cfg->host->shost_dev.kobj,
  7580. &ipr_trace_attr);
  7581. if (rc) {
  7582. scsi_remove_host(ioa_cfg->host);
  7583. __ipr_remove(pdev);
  7584. return rc;
  7585. }
  7586. rc = ipr_create_dump_file(&ioa_cfg->host->shost_dev.kobj,
  7587. &ipr_dump_attr);
  7588. if (rc) {
  7589. ipr_remove_trace_file(&ioa_cfg->host->shost_dev.kobj,
  7590. &ipr_trace_attr);
  7591. scsi_remove_host(ioa_cfg->host);
  7592. __ipr_remove(pdev);
  7593. return rc;
  7594. }
  7595. scsi_scan_host(ioa_cfg->host);
  7596. ipr_scan_vsets(ioa_cfg);
  7597. scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
  7598. ioa_cfg->allow_ml_add_del = 1;
  7599. ioa_cfg->host->max_channel = IPR_VSET_BUS;
  7600. schedule_work(&ioa_cfg->work_q);
  7601. return 0;
  7602. }
  7603. /**
  7604. * ipr_shutdown - Shutdown handler.
  7605. * @pdev: pci device struct
  7606. *
  7607. * This function is invoked upon system shutdown/reboot. It will issue
  7608. * an adapter shutdown to the adapter to flush the write cache.
  7609. *
  7610. * Return value:
  7611. * none
  7612. **/
  7613. static void ipr_shutdown(struct pci_dev *pdev)
  7614. {
  7615. struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
  7616. unsigned long lock_flags = 0;
  7617. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  7618. while(ioa_cfg->in_reset_reload) {
  7619. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  7620. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  7621. spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
  7622. }
  7623. ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
  7624. spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
  7625. wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
  7626. }
  7627. static struct pci_device_id ipr_pci_table[] __devinitdata = {
  7628. { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
  7629. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702, 0, 0, 0 },
  7630. { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
  7631. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703, 0, 0, 0 },
  7632. { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
  7633. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D, 0, 0, 0 },
  7634. { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
  7635. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573E, 0, 0, 0 },
  7636. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
  7637. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B, 0, 0, 0 },
  7638. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
  7639. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E, 0, 0, 0 },
  7640. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
  7641. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571A, 0, 0, 0 },
  7642. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
  7643. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575B, 0, 0,
  7644. IPR_USE_LONG_TRANSOP_TIMEOUT },
  7645. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
  7646. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
  7647. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
  7648. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
  7649. IPR_USE_LONG_TRANSOP_TIMEOUT },
  7650. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_OBSIDIAN,
  7651. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
  7652. IPR_USE_LONG_TRANSOP_TIMEOUT },
  7653. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
  7654. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572A, 0, 0, 0 },
  7655. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
  7656. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572B, 0, 0,
  7657. IPR_USE_LONG_TRANSOP_TIMEOUT},
  7658. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,
  7659. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575C, 0, 0,
  7660. IPR_USE_LONG_TRANSOP_TIMEOUT },
  7661. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
  7662. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574E, 0, 0,
  7663. IPR_USE_LONG_TRANSOP_TIMEOUT },
  7664. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
  7665. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B3, 0, 0, 0 },
  7666. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN_E,
  7667. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B7, 0, 0,
  7668. IPR_USE_LONG_TRANSOP_TIMEOUT | IPR_USE_PCI_WARM_RESET },
  7669. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
  7670. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780, 0, 0, 0 },
  7671. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
  7672. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571E, 0, 0, 0 },
  7673. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
  7674. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571F, 0, 0,
  7675. IPR_USE_LONG_TRANSOP_TIMEOUT },
  7676. { PCI_VENDOR_ID_ADAPTEC2, PCI_DEVICE_ID_ADAPTEC2_SCAMP,
  7677. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572F, 0, 0,
  7678. IPR_USE_LONG_TRANSOP_TIMEOUT },
  7679. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
  7680. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B5, 0, 0, 0 },
  7681. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
  7682. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_574D, 0, 0, 0 },
  7683. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_FPGA_E2,
  7684. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B2, 0, 0, 0 },
  7685. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
  7686. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B4, 0, 0, 0 },
  7687. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
  7688. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57B1, 0, 0, 0 },
  7689. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
  7690. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57C6, 0, 0, 0 },
  7691. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
  7692. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_575D, 0, 0, 0 },
  7693. { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CROC_ASIC_E2,
  7694. PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_57CE, 0, 0, 0 },
  7695. { }
  7696. };
  7697. MODULE_DEVICE_TABLE(pci, ipr_pci_table);
  7698. static struct pci_error_handlers ipr_err_handler = {
  7699. .error_detected = ipr_pci_error_detected,
  7700. .slot_reset = ipr_pci_slot_reset,
  7701. };
  7702. static struct pci_driver ipr_driver = {
  7703. .name = IPR_NAME,
  7704. .id_table = ipr_pci_table,
  7705. .probe = ipr_probe,
  7706. .remove = __devexit_p(ipr_remove),
  7707. .shutdown = ipr_shutdown,
  7708. .err_handler = &ipr_err_handler,
  7709. };
  7710. /**
  7711. * ipr_halt_done - Shutdown prepare completion
  7712. *
  7713. * Return value:
  7714. * none
  7715. **/
  7716. static void ipr_halt_done(struct ipr_cmnd *ipr_cmd)
  7717. {
  7718. struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
  7719. list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
  7720. }
  7721. /**
  7722. * ipr_halt - Issue shutdown prepare to all adapters
  7723. *
  7724. * Return value:
  7725. * NOTIFY_OK on success / NOTIFY_DONE on failure
  7726. **/
  7727. static int ipr_halt(struct notifier_block *nb, ulong event, void *buf)
  7728. {
  7729. struct ipr_cmnd *ipr_cmd;
  7730. struct ipr_ioa_cfg *ioa_cfg;
  7731. unsigned long flags = 0;
  7732. if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
  7733. return NOTIFY_DONE;
  7734. spin_lock(&ipr_driver_lock);
  7735. list_for_each_entry(ioa_cfg, &ipr_ioa_head, queue) {
  7736. spin_lock_irqsave(ioa_cfg->host->host_lock, flags);
  7737. if (!ioa_cfg->allow_cmds) {
  7738. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  7739. continue;
  7740. }
  7741. ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
  7742. ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
  7743. ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
  7744. ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
  7745. ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_SHUTDOWN_PREPARE_FOR_NORMAL;
  7746. ipr_do_req(ipr_cmd, ipr_halt_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
  7747. spin_unlock_irqrestore(ioa_cfg->host->host_lock, flags);
  7748. }
  7749. spin_unlock(&ipr_driver_lock);
  7750. return NOTIFY_OK;
  7751. }
  7752. static struct notifier_block ipr_notifier = {
  7753. ipr_halt, NULL, 0
  7754. };
  7755. /**
  7756. * ipr_init - Module entry point
  7757. *
  7758. * Return value:
  7759. * 0 on success / negative value on failure
  7760. **/
  7761. static int __init ipr_init(void)
  7762. {
  7763. ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
  7764. IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
  7765. register_reboot_notifier(&ipr_notifier);
  7766. return pci_register_driver(&ipr_driver);
  7767. }
  7768. /**
  7769. * ipr_exit - Module unload
  7770. *
  7771. * Module unload entry point.
  7772. *
  7773. * Return value:
  7774. * none
  7775. **/
  7776. static void __exit ipr_exit(void)
  7777. {
  7778. unregister_reboot_notifier(&ipr_notifier);
  7779. pci_unregister_driver(&ipr_driver);
  7780. }
  7781. module_init(ipr_init);
  7782. module_exit(ipr_exit);