hpsa.c 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2000, 2014 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/pci-aspm.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/delay.h>
  29. #include <linux/fs.h>
  30. #include <linux/timer.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/compat.h>
  34. #include <linux/blktrace_api.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/io.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/completion.h>
  39. #include <linux/moduleparam.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_cmnd.h>
  42. #include <scsi/scsi_device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_tcq.h>
  45. #include <linux/cciss_ioctl.h>
  46. #include <linux/string.h>
  47. #include <linux/bitmap.h>
  48. #include <linux/atomic.h>
  49. #include <linux/jiffies.h>
  50. #include <linux/percpu.h>
  51. #include <asm/div64.h>
  52. #include "hpsa_cmd.h"
  53. #include "hpsa.h"
  54. /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
  55. #define HPSA_DRIVER_VERSION "3.4.4-1"
  56. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  57. #define HPSA "hpsa"
  58. /* How long to wait (in milliseconds) for board to go into simple mode */
  59. #define MAX_CONFIG_WAIT 30000
  60. #define MAX_IOCTL_CONFIG_WAIT 1000
  61. /*define how many times we will try a command because of bus resets */
  62. #define MAX_CMD_RETRIES 3
  63. /* Embedded module documentation macros - see modules.h */
  64. MODULE_AUTHOR("Hewlett-Packard Company");
  65. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  66. HPSA_DRIVER_VERSION);
  67. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  68. MODULE_VERSION(HPSA_DRIVER_VERSION);
  69. MODULE_LICENSE("GPL");
  70. static int hpsa_allow_any;
  71. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  72. MODULE_PARM_DESC(hpsa_allow_any,
  73. "Allow hpsa driver to access unknown HP Smart Array hardware");
  74. static int hpsa_simple_mode;
  75. module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
  76. MODULE_PARM_DESC(hpsa_simple_mode,
  77. "Use 'simple mode' rather than 'performant mode'");
  78. /* define the PCI info for the cards we can control */
  79. static const struct pci_device_id hpsa_pci_device_id[] = {
  80. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  81. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  82. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  83. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  84. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  85. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
  86. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
  87. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
  88. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
  89. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
  90. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
  91. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
  92. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
  93. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
  94. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
  95. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
  96. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
  97. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
  98. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
  99. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1925},
  100. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
  101. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
  102. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
  103. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
  104. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
  105. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
  106. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
  107. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
  108. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
  109. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
  110. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
  111. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
  112. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
  113. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
  114. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
  115. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
  116. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
  117. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
  118. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
  119. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
  120. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
  121. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
  122. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
  123. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
  124. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
  125. {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
  126. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  127. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  128. {0,}
  129. };
  130. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  131. /* board_id = Subsystem Device ID & Vendor ID
  132. * product = Marketing Name for the board
  133. * access = Address of the struct of function pointers
  134. */
  135. static struct board_type products[] = {
  136. {0x3241103C, "Smart Array P212", &SA5_access},
  137. {0x3243103C, "Smart Array P410", &SA5_access},
  138. {0x3245103C, "Smart Array P410i", &SA5_access},
  139. {0x3247103C, "Smart Array P411", &SA5_access},
  140. {0x3249103C, "Smart Array P812", &SA5_access},
  141. {0x324A103C, "Smart Array P712m", &SA5_access},
  142. {0x324B103C, "Smart Array P711m", &SA5_access},
  143. {0x3350103C, "Smart Array P222", &SA5_access},
  144. {0x3351103C, "Smart Array P420", &SA5_access},
  145. {0x3352103C, "Smart Array P421", &SA5_access},
  146. {0x3353103C, "Smart Array P822", &SA5_access},
  147. {0x3354103C, "Smart Array P420i", &SA5_access},
  148. {0x3355103C, "Smart Array P220i", &SA5_access},
  149. {0x3356103C, "Smart Array P721m", &SA5_access},
  150. {0x1921103C, "Smart Array P830i", &SA5_access},
  151. {0x1922103C, "Smart Array P430", &SA5_access},
  152. {0x1923103C, "Smart Array P431", &SA5_access},
  153. {0x1924103C, "Smart Array P830", &SA5_access},
  154. {0x1926103C, "Smart Array P731m", &SA5_access},
  155. {0x1928103C, "Smart Array P230i", &SA5_access},
  156. {0x1929103C, "Smart Array P530", &SA5_access},
  157. {0x21BD103C, "Smart Array", &SA5_access},
  158. {0x21BE103C, "Smart Array", &SA5_access},
  159. {0x21BF103C, "Smart Array", &SA5_access},
  160. {0x21C0103C, "Smart Array", &SA5_access},
  161. {0x21C1103C, "Smart Array", &SA5_access},
  162. {0x21C2103C, "Smart Array", &SA5_access},
  163. {0x21C3103C, "Smart Array", &SA5_access},
  164. {0x21C4103C, "Smart Array", &SA5_access},
  165. {0x21C5103C, "Smart Array", &SA5_access},
  166. {0x21C6103C, "Smart Array", &SA5_access},
  167. {0x21C7103C, "Smart Array", &SA5_access},
  168. {0x21C8103C, "Smart Array", &SA5_access},
  169. {0x21C9103C, "Smart Array", &SA5_access},
  170. {0x21CA103C, "Smart Array", &SA5_access},
  171. {0x21CB103C, "Smart Array", &SA5_access},
  172. {0x21CC103C, "Smart Array", &SA5_access},
  173. {0x21CD103C, "Smart Array", &SA5_access},
  174. {0x21CE103C, "Smart Array", &SA5_access},
  175. {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
  176. {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
  177. {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
  178. {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
  179. {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
  180. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  181. };
  182. static int number_of_controllers;
  183. static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
  184. static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
  185. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
  186. static void lock_and_start_io(struct ctlr_info *h);
  187. static void start_io(struct ctlr_info *h, unsigned long *flags);
  188. #ifdef CONFIG_COMPAT
  189. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
  190. #endif
  191. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  192. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
  193. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  194. static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
  195. static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  196. void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
  197. int cmd_type);
  198. #define VPD_PAGE (1 << 8)
  199. static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  200. static void hpsa_scan_start(struct Scsi_Host *);
  201. static int hpsa_scan_finished(struct Scsi_Host *sh,
  202. unsigned long elapsed_time);
  203. static int hpsa_change_queue_depth(struct scsi_device *sdev,
  204. int qdepth, int reason);
  205. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  206. static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
  207. static int hpsa_slave_alloc(struct scsi_device *sdev);
  208. static void hpsa_slave_destroy(struct scsi_device *sdev);
  209. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
  210. static int check_for_unit_attention(struct ctlr_info *h,
  211. struct CommandList *c);
  212. static void check_ioctl_unit_attention(struct ctlr_info *h,
  213. struct CommandList *c);
  214. /* performant mode helper functions */
  215. static void calc_bucket_map(int *bucket, int num_buckets,
  216. int nsgs, int min_blocks, int *bucket_map);
  217. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
  218. static inline u32 next_command(struct ctlr_info *h, u8 q);
  219. static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
  220. u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  221. u64 *cfg_offset);
  222. static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  223. unsigned long *memory_bar);
  224. static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
  225. static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
  226. int wait_for_ready);
  227. static inline void finish_cmd(struct CommandList *c);
  228. static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
  229. #define BOARD_NOT_READY 0
  230. #define BOARD_READY 1
  231. static void hpsa_drain_accel_commands(struct ctlr_info *h);
  232. static void hpsa_flush_cache(struct ctlr_info *h);
  233. static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
  234. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  235. u8 *scsi3addr);
  236. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  237. {
  238. unsigned long *priv = shost_priv(sdev->host);
  239. return (struct ctlr_info *) *priv;
  240. }
  241. static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
  242. {
  243. unsigned long *priv = shost_priv(sh);
  244. return (struct ctlr_info *) *priv;
  245. }
  246. static int check_for_unit_attention(struct ctlr_info *h,
  247. struct CommandList *c)
  248. {
  249. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  250. return 0;
  251. switch (c->err_info->SenseInfo[12]) {
  252. case STATE_CHANGED:
  253. dev_warn(&h->pdev->dev, HPSA "%d: a state change "
  254. "detected, command retried\n", h->ctlr);
  255. break;
  256. case LUN_FAILED:
  257. dev_warn(&h->pdev->dev, HPSA "%d: LUN failure "
  258. "detected, action required\n", h->ctlr);
  259. break;
  260. case REPORT_LUNS_CHANGED:
  261. dev_warn(&h->pdev->dev, HPSA "%d: report LUN data "
  262. "changed, action required\n", h->ctlr);
  263. /*
  264. * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
  265. * target (array) devices.
  266. */
  267. break;
  268. case POWER_OR_RESET:
  269. dev_warn(&h->pdev->dev, HPSA "%d: a power on "
  270. "or device reset detected\n", h->ctlr);
  271. break;
  272. case UNIT_ATTENTION_CLEARED:
  273. dev_warn(&h->pdev->dev, HPSA "%d: unit attention "
  274. "cleared by another initiator\n", h->ctlr);
  275. break;
  276. default:
  277. dev_warn(&h->pdev->dev, HPSA "%d: unknown "
  278. "unit attention detected\n", h->ctlr);
  279. break;
  280. }
  281. return 1;
  282. }
  283. static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
  284. {
  285. if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
  286. (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
  287. c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
  288. return 0;
  289. dev_warn(&h->pdev->dev, HPSA "device busy");
  290. return 1;
  291. }
  292. static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
  293. struct device_attribute *attr,
  294. const char *buf, size_t count)
  295. {
  296. int status, len;
  297. struct ctlr_info *h;
  298. struct Scsi_Host *shost = class_to_shost(dev);
  299. char tmpbuf[10];
  300. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  301. return -EACCES;
  302. len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
  303. strncpy(tmpbuf, buf, len);
  304. tmpbuf[len] = '\0';
  305. if (sscanf(tmpbuf, "%d", &status) != 1)
  306. return -EINVAL;
  307. h = shost_to_hba(shost);
  308. h->acciopath_status = !!status;
  309. dev_warn(&h->pdev->dev,
  310. "hpsa: HP SSD Smart Path %s via sysfs update.\n",
  311. h->acciopath_status ? "enabled" : "disabled");
  312. return count;
  313. }
  314. static ssize_t host_store_raid_offload_debug(struct device *dev,
  315. struct device_attribute *attr,
  316. const char *buf, size_t count)
  317. {
  318. int debug_level, len;
  319. struct ctlr_info *h;
  320. struct Scsi_Host *shost = class_to_shost(dev);
  321. char tmpbuf[10];
  322. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  323. return -EACCES;
  324. len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
  325. strncpy(tmpbuf, buf, len);
  326. tmpbuf[len] = '\0';
  327. if (sscanf(tmpbuf, "%d", &debug_level) != 1)
  328. return -EINVAL;
  329. if (debug_level < 0)
  330. debug_level = 0;
  331. h = shost_to_hba(shost);
  332. h->raid_offload_debug = debug_level;
  333. dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
  334. h->raid_offload_debug);
  335. return count;
  336. }
  337. static ssize_t host_store_rescan(struct device *dev,
  338. struct device_attribute *attr,
  339. const char *buf, size_t count)
  340. {
  341. struct ctlr_info *h;
  342. struct Scsi_Host *shost = class_to_shost(dev);
  343. h = shost_to_hba(shost);
  344. hpsa_scan_start(h->scsi_host);
  345. return count;
  346. }
  347. static ssize_t host_show_firmware_revision(struct device *dev,
  348. struct device_attribute *attr, char *buf)
  349. {
  350. struct ctlr_info *h;
  351. struct Scsi_Host *shost = class_to_shost(dev);
  352. unsigned char *fwrev;
  353. h = shost_to_hba(shost);
  354. if (!h->hba_inquiry_data)
  355. return 0;
  356. fwrev = &h->hba_inquiry_data[32];
  357. return snprintf(buf, 20, "%c%c%c%c\n",
  358. fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
  359. }
  360. static ssize_t host_show_commands_outstanding(struct device *dev,
  361. struct device_attribute *attr, char *buf)
  362. {
  363. struct Scsi_Host *shost = class_to_shost(dev);
  364. struct ctlr_info *h = shost_to_hba(shost);
  365. return snprintf(buf, 20, "%d\n", h->commands_outstanding);
  366. }
  367. static ssize_t host_show_transport_mode(struct device *dev,
  368. struct device_attribute *attr, char *buf)
  369. {
  370. struct ctlr_info *h;
  371. struct Scsi_Host *shost = class_to_shost(dev);
  372. h = shost_to_hba(shost);
  373. return snprintf(buf, 20, "%s\n",
  374. h->transMethod & CFGTBL_Trans_Performant ?
  375. "performant" : "simple");
  376. }
  377. static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
  378. struct device_attribute *attr, char *buf)
  379. {
  380. struct ctlr_info *h;
  381. struct Scsi_Host *shost = class_to_shost(dev);
  382. h = shost_to_hba(shost);
  383. return snprintf(buf, 30, "HP SSD Smart Path %s\n",
  384. (h->acciopath_status == 1) ? "enabled" : "disabled");
  385. }
  386. /* List of controllers which cannot be hard reset on kexec with reset_devices */
  387. static u32 unresettable_controller[] = {
  388. 0x324a103C, /* Smart Array P712m */
  389. 0x324b103C, /* SmartArray P711m */
  390. 0x3223103C, /* Smart Array P800 */
  391. 0x3234103C, /* Smart Array P400 */
  392. 0x3235103C, /* Smart Array P400i */
  393. 0x3211103C, /* Smart Array E200i */
  394. 0x3212103C, /* Smart Array E200 */
  395. 0x3213103C, /* Smart Array E200i */
  396. 0x3214103C, /* Smart Array E200i */
  397. 0x3215103C, /* Smart Array E200i */
  398. 0x3237103C, /* Smart Array E500 */
  399. 0x323D103C, /* Smart Array P700m */
  400. 0x40800E11, /* Smart Array 5i */
  401. 0x409C0E11, /* Smart Array 6400 */
  402. 0x409D0E11, /* Smart Array 6400 EM */
  403. 0x40700E11, /* Smart Array 5300 */
  404. 0x40820E11, /* Smart Array 532 */
  405. 0x40830E11, /* Smart Array 5312 */
  406. 0x409A0E11, /* Smart Array 641 */
  407. 0x409B0E11, /* Smart Array 642 */
  408. 0x40910E11, /* Smart Array 6i */
  409. };
  410. /* List of controllers which cannot even be soft reset */
  411. static u32 soft_unresettable_controller[] = {
  412. 0x40800E11, /* Smart Array 5i */
  413. 0x40700E11, /* Smart Array 5300 */
  414. 0x40820E11, /* Smart Array 532 */
  415. 0x40830E11, /* Smart Array 5312 */
  416. 0x409A0E11, /* Smart Array 641 */
  417. 0x409B0E11, /* Smart Array 642 */
  418. 0x40910E11, /* Smart Array 6i */
  419. /* Exclude 640x boards. These are two pci devices in one slot
  420. * which share a battery backed cache module. One controls the
  421. * cache, the other accesses the cache through the one that controls
  422. * it. If we reset the one controlling the cache, the other will
  423. * likely not be happy. Just forbid resetting this conjoined mess.
  424. * The 640x isn't really supported by hpsa anyway.
  425. */
  426. 0x409C0E11, /* Smart Array 6400 */
  427. 0x409D0E11, /* Smart Array 6400 EM */
  428. };
  429. static int ctlr_is_hard_resettable(u32 board_id)
  430. {
  431. int i;
  432. for (i = 0; i < ARRAY_SIZE(unresettable_controller); i++)
  433. if (unresettable_controller[i] == board_id)
  434. return 0;
  435. return 1;
  436. }
  437. static int ctlr_is_soft_resettable(u32 board_id)
  438. {
  439. int i;
  440. for (i = 0; i < ARRAY_SIZE(soft_unresettable_controller); i++)
  441. if (soft_unresettable_controller[i] == board_id)
  442. return 0;
  443. return 1;
  444. }
  445. static int ctlr_is_resettable(u32 board_id)
  446. {
  447. return ctlr_is_hard_resettable(board_id) ||
  448. ctlr_is_soft_resettable(board_id);
  449. }
  450. static ssize_t host_show_resettable(struct device *dev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. struct ctlr_info *h;
  454. struct Scsi_Host *shost = class_to_shost(dev);
  455. h = shost_to_hba(shost);
  456. return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
  457. }
  458. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  459. {
  460. return (scsi3addr[3] & 0xC0) == 0x40;
  461. }
  462. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  463. "1(ADM)", "UNKNOWN"
  464. };
  465. #define HPSA_RAID_0 0
  466. #define HPSA_RAID_4 1
  467. #define HPSA_RAID_1 2 /* also used for RAID 10 */
  468. #define HPSA_RAID_5 3 /* also used for RAID 50 */
  469. #define HPSA_RAID_51 4
  470. #define HPSA_RAID_6 5 /* also used for RAID 60 */
  471. #define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
  472. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
  473. static ssize_t raid_level_show(struct device *dev,
  474. struct device_attribute *attr, char *buf)
  475. {
  476. ssize_t l = 0;
  477. unsigned char rlevel;
  478. struct ctlr_info *h;
  479. struct scsi_device *sdev;
  480. struct hpsa_scsi_dev_t *hdev;
  481. unsigned long flags;
  482. sdev = to_scsi_device(dev);
  483. h = sdev_to_hba(sdev);
  484. spin_lock_irqsave(&h->lock, flags);
  485. hdev = sdev->hostdata;
  486. if (!hdev) {
  487. spin_unlock_irqrestore(&h->lock, flags);
  488. return -ENODEV;
  489. }
  490. /* Is this even a logical drive? */
  491. if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
  492. spin_unlock_irqrestore(&h->lock, flags);
  493. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  494. return l;
  495. }
  496. rlevel = hdev->raid_level;
  497. spin_unlock_irqrestore(&h->lock, flags);
  498. if (rlevel > RAID_UNKNOWN)
  499. rlevel = RAID_UNKNOWN;
  500. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  501. return l;
  502. }
  503. static ssize_t lunid_show(struct device *dev,
  504. struct device_attribute *attr, char *buf)
  505. {
  506. struct ctlr_info *h;
  507. struct scsi_device *sdev;
  508. struct hpsa_scsi_dev_t *hdev;
  509. unsigned long flags;
  510. unsigned char lunid[8];
  511. sdev = to_scsi_device(dev);
  512. h = sdev_to_hba(sdev);
  513. spin_lock_irqsave(&h->lock, flags);
  514. hdev = sdev->hostdata;
  515. if (!hdev) {
  516. spin_unlock_irqrestore(&h->lock, flags);
  517. return -ENODEV;
  518. }
  519. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  520. spin_unlock_irqrestore(&h->lock, flags);
  521. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  522. lunid[0], lunid[1], lunid[2], lunid[3],
  523. lunid[4], lunid[5], lunid[6], lunid[7]);
  524. }
  525. static ssize_t unique_id_show(struct device *dev,
  526. struct device_attribute *attr, char *buf)
  527. {
  528. struct ctlr_info *h;
  529. struct scsi_device *sdev;
  530. struct hpsa_scsi_dev_t *hdev;
  531. unsigned long flags;
  532. unsigned char sn[16];
  533. sdev = to_scsi_device(dev);
  534. h = sdev_to_hba(sdev);
  535. spin_lock_irqsave(&h->lock, flags);
  536. hdev = sdev->hostdata;
  537. if (!hdev) {
  538. spin_unlock_irqrestore(&h->lock, flags);
  539. return -ENODEV;
  540. }
  541. memcpy(sn, hdev->device_id, sizeof(sn));
  542. spin_unlock_irqrestore(&h->lock, flags);
  543. return snprintf(buf, 16 * 2 + 2,
  544. "%02X%02X%02X%02X%02X%02X%02X%02X"
  545. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  546. sn[0], sn[1], sn[2], sn[3],
  547. sn[4], sn[5], sn[6], sn[7],
  548. sn[8], sn[9], sn[10], sn[11],
  549. sn[12], sn[13], sn[14], sn[15]);
  550. }
  551. static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
  552. struct device_attribute *attr, char *buf)
  553. {
  554. struct ctlr_info *h;
  555. struct scsi_device *sdev;
  556. struct hpsa_scsi_dev_t *hdev;
  557. unsigned long flags;
  558. int offload_enabled;
  559. sdev = to_scsi_device(dev);
  560. h = sdev_to_hba(sdev);
  561. spin_lock_irqsave(&h->lock, flags);
  562. hdev = sdev->hostdata;
  563. if (!hdev) {
  564. spin_unlock_irqrestore(&h->lock, flags);
  565. return -ENODEV;
  566. }
  567. offload_enabled = hdev->offload_enabled;
  568. spin_unlock_irqrestore(&h->lock, flags);
  569. return snprintf(buf, 20, "%d\n", offload_enabled);
  570. }
  571. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  572. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  573. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  574. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  575. static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
  576. host_show_hp_ssd_smart_path_enabled, NULL);
  577. static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
  578. host_show_hp_ssd_smart_path_status,
  579. host_store_hp_ssd_smart_path_status);
  580. static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
  581. host_store_raid_offload_debug);
  582. static DEVICE_ATTR(firmware_revision, S_IRUGO,
  583. host_show_firmware_revision, NULL);
  584. static DEVICE_ATTR(commands_outstanding, S_IRUGO,
  585. host_show_commands_outstanding, NULL);
  586. static DEVICE_ATTR(transport_mode, S_IRUGO,
  587. host_show_transport_mode, NULL);
  588. static DEVICE_ATTR(resettable, S_IRUGO,
  589. host_show_resettable, NULL);
  590. static struct device_attribute *hpsa_sdev_attrs[] = {
  591. &dev_attr_raid_level,
  592. &dev_attr_lunid,
  593. &dev_attr_unique_id,
  594. &dev_attr_hp_ssd_smart_path_enabled,
  595. NULL,
  596. };
  597. static struct device_attribute *hpsa_shost_attrs[] = {
  598. &dev_attr_rescan,
  599. &dev_attr_firmware_revision,
  600. &dev_attr_commands_outstanding,
  601. &dev_attr_transport_mode,
  602. &dev_attr_resettable,
  603. &dev_attr_hp_ssd_smart_path_status,
  604. &dev_attr_raid_offload_debug,
  605. NULL,
  606. };
  607. static struct scsi_host_template hpsa_driver_template = {
  608. .module = THIS_MODULE,
  609. .name = HPSA,
  610. .proc_name = HPSA,
  611. .queuecommand = hpsa_scsi_queue_command,
  612. .scan_start = hpsa_scan_start,
  613. .scan_finished = hpsa_scan_finished,
  614. .change_queue_depth = hpsa_change_queue_depth,
  615. .this_id = -1,
  616. .use_clustering = ENABLE_CLUSTERING,
  617. .eh_abort_handler = hpsa_eh_abort_handler,
  618. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  619. .ioctl = hpsa_ioctl,
  620. .slave_alloc = hpsa_slave_alloc,
  621. .slave_destroy = hpsa_slave_destroy,
  622. #ifdef CONFIG_COMPAT
  623. .compat_ioctl = hpsa_compat_ioctl,
  624. #endif
  625. .sdev_attrs = hpsa_sdev_attrs,
  626. .shost_attrs = hpsa_shost_attrs,
  627. .max_sectors = 8192,
  628. .no_write_same = 1,
  629. };
  630. /* Enqueuing and dequeuing functions for cmdlists. */
  631. static inline void addQ(struct list_head *list, struct CommandList *c)
  632. {
  633. list_add_tail(&c->list, list);
  634. }
  635. static inline u32 next_command(struct ctlr_info *h, u8 q)
  636. {
  637. u32 a;
  638. struct reply_queue_buffer *rq = &h->reply_queue[q];
  639. unsigned long flags;
  640. if (h->transMethod & CFGTBL_Trans_io_accel1)
  641. return h->access.command_completed(h, q);
  642. if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
  643. return h->access.command_completed(h, q);
  644. if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
  645. a = rq->head[rq->current_entry];
  646. rq->current_entry++;
  647. spin_lock_irqsave(&h->lock, flags);
  648. h->commands_outstanding--;
  649. spin_unlock_irqrestore(&h->lock, flags);
  650. } else {
  651. a = FIFO_EMPTY;
  652. }
  653. /* Check for wraparound */
  654. if (rq->current_entry == h->max_commands) {
  655. rq->current_entry = 0;
  656. rq->wraparound ^= 1;
  657. }
  658. return a;
  659. }
  660. /*
  661. * There are some special bits in the bus address of the
  662. * command that we have to set for the controller to know
  663. * how to process the command:
  664. *
  665. * Normal performant mode:
  666. * bit 0: 1 means performant mode, 0 means simple mode.
  667. * bits 1-3 = block fetch table entry
  668. * bits 4-6 = command type (== 0)
  669. *
  670. * ioaccel1 mode:
  671. * bit 0 = "performant mode" bit.
  672. * bits 1-3 = block fetch table entry
  673. * bits 4-6 = command type (== 110)
  674. * (command type is needed because ioaccel1 mode
  675. * commands are submitted through the same register as normal
  676. * mode commands, so this is how the controller knows whether
  677. * the command is normal mode or ioaccel1 mode.)
  678. *
  679. * ioaccel2 mode:
  680. * bit 0 = "performant mode" bit.
  681. * bits 1-4 = block fetch table entry (note extra bit)
  682. * bits 4-6 = not needed, because ioaccel2 mode has
  683. * a separate special register for submitting commands.
  684. */
  685. /* set_performant_mode: Modify the tag for cciss performant
  686. * set bit 0 for pull model, bits 3-1 for block fetch
  687. * register number
  688. */
  689. static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
  690. {
  691. if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
  692. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  693. if (likely(h->msix_vector > 0))
  694. c->Header.ReplyQueue =
  695. raw_smp_processor_id() % h->nreply_queues;
  696. }
  697. }
  698. static void set_ioaccel1_performant_mode(struct ctlr_info *h,
  699. struct CommandList *c)
  700. {
  701. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
  702. /* Tell the controller to post the reply to the queue for this
  703. * processor. This seems to give the best I/O throughput.
  704. */
  705. cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
  706. /* Set the bits in the address sent down to include:
  707. * - performant mode bit (bit 0)
  708. * - pull count (bits 1-3)
  709. * - command type (bits 4-6)
  710. */
  711. c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
  712. IOACCEL1_BUSADDR_CMDTYPE;
  713. }
  714. static void set_ioaccel2_performant_mode(struct ctlr_info *h,
  715. struct CommandList *c)
  716. {
  717. struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
  718. /* Tell the controller to post the reply to the queue for this
  719. * processor. This seems to give the best I/O throughput.
  720. */
  721. cp->reply_queue = smp_processor_id() % h->nreply_queues;
  722. /* Set the bits in the address sent down to include:
  723. * - performant mode bit not used in ioaccel mode 2
  724. * - pull count (bits 0-3)
  725. * - command type isn't needed for ioaccel2
  726. */
  727. c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
  728. }
  729. static int is_firmware_flash_cmd(u8 *cdb)
  730. {
  731. return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
  732. }
  733. /*
  734. * During firmware flash, the heartbeat register may not update as frequently
  735. * as it should. So we dial down lockup detection during firmware flash. and
  736. * dial it back up when firmware flash completes.
  737. */
  738. #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
  739. #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
  740. static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
  741. struct CommandList *c)
  742. {
  743. if (!is_firmware_flash_cmd(c->Request.CDB))
  744. return;
  745. atomic_inc(&h->firmware_flash_in_progress);
  746. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
  747. }
  748. static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
  749. struct CommandList *c)
  750. {
  751. if (is_firmware_flash_cmd(c->Request.CDB) &&
  752. atomic_dec_and_test(&h->firmware_flash_in_progress))
  753. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
  754. }
  755. static void enqueue_cmd_and_start_io(struct ctlr_info *h,
  756. struct CommandList *c)
  757. {
  758. unsigned long flags;
  759. switch (c->cmd_type) {
  760. case CMD_IOACCEL1:
  761. set_ioaccel1_performant_mode(h, c);
  762. break;
  763. case CMD_IOACCEL2:
  764. set_ioaccel2_performant_mode(h, c);
  765. break;
  766. default:
  767. set_performant_mode(h, c);
  768. }
  769. dial_down_lockup_detection_during_fw_flash(h, c);
  770. spin_lock_irqsave(&h->lock, flags);
  771. addQ(&h->reqQ, c);
  772. h->Qdepth++;
  773. start_io(h, &flags);
  774. spin_unlock_irqrestore(&h->lock, flags);
  775. }
  776. static inline void removeQ(struct CommandList *c)
  777. {
  778. if (WARN_ON(list_empty(&c->list)))
  779. return;
  780. list_del_init(&c->list);
  781. }
  782. static inline int is_hba_lunid(unsigned char scsi3addr[])
  783. {
  784. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  785. }
  786. static inline int is_scsi_rev_5(struct ctlr_info *h)
  787. {
  788. if (!h->hba_inquiry_data)
  789. return 0;
  790. if ((h->hba_inquiry_data[2] & 0x07) == 5)
  791. return 1;
  792. return 0;
  793. }
  794. static int hpsa_find_target_lun(struct ctlr_info *h,
  795. unsigned char scsi3addr[], int bus, int *target, int *lun)
  796. {
  797. /* finds an unused bus, target, lun for a new physical device
  798. * assumes h->devlock is held
  799. */
  800. int i, found = 0;
  801. DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
  802. bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
  803. for (i = 0; i < h->ndevices; i++) {
  804. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  805. __set_bit(h->dev[i]->target, lun_taken);
  806. }
  807. i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
  808. if (i < HPSA_MAX_DEVICES) {
  809. /* *bus = 1; */
  810. *target = i;
  811. *lun = 0;
  812. found = 1;
  813. }
  814. return !found;
  815. }
  816. /* Add an entry into h->dev[] array. */
  817. static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
  818. struct hpsa_scsi_dev_t *device,
  819. struct hpsa_scsi_dev_t *added[], int *nadded)
  820. {
  821. /* assumes h->devlock is held */
  822. int n = h->ndevices;
  823. int i;
  824. unsigned char addr1[8], addr2[8];
  825. struct hpsa_scsi_dev_t *sd;
  826. if (n >= HPSA_MAX_DEVICES) {
  827. dev_err(&h->pdev->dev, "too many devices, some will be "
  828. "inaccessible.\n");
  829. return -1;
  830. }
  831. /* physical devices do not have lun or target assigned until now. */
  832. if (device->lun != -1)
  833. /* Logical device, lun is already assigned. */
  834. goto lun_assigned;
  835. /* If this device a non-zero lun of a multi-lun device
  836. * byte 4 of the 8-byte LUN addr will contain the logical
  837. * unit no, zero otherise.
  838. */
  839. if (device->scsi3addr[4] == 0) {
  840. /* This is not a non-zero lun of a multi-lun device */
  841. if (hpsa_find_target_lun(h, device->scsi3addr,
  842. device->bus, &device->target, &device->lun) != 0)
  843. return -1;
  844. goto lun_assigned;
  845. }
  846. /* This is a non-zero lun of a multi-lun device.
  847. * Search through our list and find the device which
  848. * has the same 8 byte LUN address, excepting byte 4.
  849. * Assign the same bus and target for this new LUN.
  850. * Use the logical unit number from the firmware.
  851. */
  852. memcpy(addr1, device->scsi3addr, 8);
  853. addr1[4] = 0;
  854. for (i = 0; i < n; i++) {
  855. sd = h->dev[i];
  856. memcpy(addr2, sd->scsi3addr, 8);
  857. addr2[4] = 0;
  858. /* differ only in byte 4? */
  859. if (memcmp(addr1, addr2, 8) == 0) {
  860. device->bus = sd->bus;
  861. device->target = sd->target;
  862. device->lun = device->scsi3addr[4];
  863. break;
  864. }
  865. }
  866. if (device->lun == -1) {
  867. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  868. " suspect firmware bug or unsupported hardware "
  869. "configuration.\n");
  870. return -1;
  871. }
  872. lun_assigned:
  873. h->dev[n] = device;
  874. h->ndevices++;
  875. added[*nadded] = device;
  876. (*nadded)++;
  877. /* initially, (before registering with scsi layer) we don't
  878. * know our hostno and we don't want to print anything first
  879. * time anyway (the scsi layer's inquiries will show that info)
  880. */
  881. /* if (hostno != -1) */
  882. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
  883. scsi_device_type(device->devtype), hostno,
  884. device->bus, device->target, device->lun);
  885. return 0;
  886. }
  887. /* Update an entry in h->dev[] array. */
  888. static void hpsa_scsi_update_entry(struct ctlr_info *h, int hostno,
  889. int entry, struct hpsa_scsi_dev_t *new_entry)
  890. {
  891. /* assumes h->devlock is held */
  892. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  893. /* Raid level changed. */
  894. h->dev[entry]->raid_level = new_entry->raid_level;
  895. /* Raid offload parameters changed. */
  896. h->dev[entry]->offload_config = new_entry->offload_config;
  897. h->dev[entry]->offload_enabled = new_entry->offload_enabled;
  898. h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
  899. h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
  900. h->dev[entry]->raid_map = new_entry->raid_map;
  901. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d updated.\n",
  902. scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
  903. new_entry->target, new_entry->lun);
  904. }
  905. /* Replace an entry from h->dev[] array. */
  906. static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
  907. int entry, struct hpsa_scsi_dev_t *new_entry,
  908. struct hpsa_scsi_dev_t *added[], int *nadded,
  909. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  910. {
  911. /* assumes h->devlock is held */
  912. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  913. removed[*nremoved] = h->dev[entry];
  914. (*nremoved)++;
  915. /*
  916. * New physical devices won't have target/lun assigned yet
  917. * so we need to preserve the values in the slot we are replacing.
  918. */
  919. if (new_entry->target == -1) {
  920. new_entry->target = h->dev[entry]->target;
  921. new_entry->lun = h->dev[entry]->lun;
  922. }
  923. h->dev[entry] = new_entry;
  924. added[*nadded] = new_entry;
  925. (*nadded)++;
  926. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
  927. scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
  928. new_entry->target, new_entry->lun);
  929. }
  930. /* Remove an entry from h->dev[] array. */
  931. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
  932. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  933. {
  934. /* assumes h->devlock is held */
  935. int i;
  936. struct hpsa_scsi_dev_t *sd;
  937. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  938. sd = h->dev[entry];
  939. removed[*nremoved] = h->dev[entry];
  940. (*nremoved)++;
  941. for (i = entry; i < h->ndevices-1; i++)
  942. h->dev[i] = h->dev[i+1];
  943. h->ndevices--;
  944. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
  945. scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
  946. sd->lun);
  947. }
  948. #define SCSI3ADDR_EQ(a, b) ( \
  949. (a)[7] == (b)[7] && \
  950. (a)[6] == (b)[6] && \
  951. (a)[5] == (b)[5] && \
  952. (a)[4] == (b)[4] && \
  953. (a)[3] == (b)[3] && \
  954. (a)[2] == (b)[2] && \
  955. (a)[1] == (b)[1] && \
  956. (a)[0] == (b)[0])
  957. static void fixup_botched_add(struct ctlr_info *h,
  958. struct hpsa_scsi_dev_t *added)
  959. {
  960. /* called when scsi_add_device fails in order to re-adjust
  961. * h->dev[] to match the mid layer's view.
  962. */
  963. unsigned long flags;
  964. int i, j;
  965. spin_lock_irqsave(&h->lock, flags);
  966. for (i = 0; i < h->ndevices; i++) {
  967. if (h->dev[i] == added) {
  968. for (j = i; j < h->ndevices-1; j++)
  969. h->dev[j] = h->dev[j+1];
  970. h->ndevices--;
  971. break;
  972. }
  973. }
  974. spin_unlock_irqrestore(&h->lock, flags);
  975. kfree(added);
  976. }
  977. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  978. struct hpsa_scsi_dev_t *dev2)
  979. {
  980. /* we compare everything except lun and target as these
  981. * are not yet assigned. Compare parts likely
  982. * to differ first
  983. */
  984. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  985. sizeof(dev1->scsi3addr)) != 0)
  986. return 0;
  987. if (memcmp(dev1->device_id, dev2->device_id,
  988. sizeof(dev1->device_id)) != 0)
  989. return 0;
  990. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  991. return 0;
  992. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  993. return 0;
  994. if (dev1->devtype != dev2->devtype)
  995. return 0;
  996. if (dev1->bus != dev2->bus)
  997. return 0;
  998. return 1;
  999. }
  1000. static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
  1001. struct hpsa_scsi_dev_t *dev2)
  1002. {
  1003. /* Device attributes that can change, but don't mean
  1004. * that the device is a different device, nor that the OS
  1005. * needs to be told anything about the change.
  1006. */
  1007. if (dev1->raid_level != dev2->raid_level)
  1008. return 1;
  1009. if (dev1->offload_config != dev2->offload_config)
  1010. return 1;
  1011. if (dev1->offload_enabled != dev2->offload_enabled)
  1012. return 1;
  1013. return 0;
  1014. }
  1015. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  1016. * and return needle location in *index. If scsi3addr matches, but not
  1017. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  1018. * location in *index.
  1019. * In the case of a minor device attribute change, such as RAID level, just
  1020. * return DEVICE_UPDATED, along with the updated device's location in index.
  1021. * If needle not found, return DEVICE_NOT_FOUND.
  1022. */
  1023. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  1024. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  1025. int *index)
  1026. {
  1027. int i;
  1028. #define DEVICE_NOT_FOUND 0
  1029. #define DEVICE_CHANGED 1
  1030. #define DEVICE_SAME 2
  1031. #define DEVICE_UPDATED 3
  1032. for (i = 0; i < haystack_size; i++) {
  1033. if (haystack[i] == NULL) /* previously removed. */
  1034. continue;
  1035. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  1036. *index = i;
  1037. if (device_is_the_same(needle, haystack[i])) {
  1038. if (device_updated(needle, haystack[i]))
  1039. return DEVICE_UPDATED;
  1040. return DEVICE_SAME;
  1041. } else {
  1042. /* Keep offline devices offline */
  1043. if (needle->volume_offline)
  1044. return DEVICE_NOT_FOUND;
  1045. return DEVICE_CHANGED;
  1046. }
  1047. }
  1048. }
  1049. *index = -1;
  1050. return DEVICE_NOT_FOUND;
  1051. }
  1052. static void hpsa_monitor_offline_device(struct ctlr_info *h,
  1053. unsigned char scsi3addr[])
  1054. {
  1055. struct offline_device_entry *device;
  1056. unsigned long flags;
  1057. /* Check to see if device is already on the list */
  1058. spin_lock_irqsave(&h->offline_device_lock, flags);
  1059. list_for_each_entry(device, &h->offline_device_list, offline_list) {
  1060. if (memcmp(device->scsi3addr, scsi3addr,
  1061. sizeof(device->scsi3addr)) == 0) {
  1062. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1063. return;
  1064. }
  1065. }
  1066. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1067. /* Device is not on the list, add it. */
  1068. device = kmalloc(sizeof(*device), GFP_KERNEL);
  1069. if (!device) {
  1070. dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
  1071. return;
  1072. }
  1073. memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
  1074. spin_lock_irqsave(&h->offline_device_lock, flags);
  1075. list_add_tail(&device->offline_list, &h->offline_device_list);
  1076. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1077. }
  1078. /* Print a message explaining various offline volume states */
  1079. static void hpsa_show_volume_status(struct ctlr_info *h,
  1080. struct hpsa_scsi_dev_t *sd)
  1081. {
  1082. if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
  1083. dev_info(&h->pdev->dev,
  1084. "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
  1085. h->scsi_host->host_no,
  1086. sd->bus, sd->target, sd->lun);
  1087. switch (sd->volume_offline) {
  1088. case HPSA_LV_OK:
  1089. break;
  1090. case HPSA_LV_UNDERGOING_ERASE:
  1091. dev_info(&h->pdev->dev,
  1092. "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
  1093. h->scsi_host->host_no,
  1094. sd->bus, sd->target, sd->lun);
  1095. break;
  1096. case HPSA_LV_UNDERGOING_RPI:
  1097. dev_info(&h->pdev->dev,
  1098. "C%d:B%d:T%d:L%d Volume is undergoing rapid parity initialization process.\n",
  1099. h->scsi_host->host_no,
  1100. sd->bus, sd->target, sd->lun);
  1101. break;
  1102. case HPSA_LV_PENDING_RPI:
  1103. dev_info(&h->pdev->dev,
  1104. "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
  1105. h->scsi_host->host_no,
  1106. sd->bus, sd->target, sd->lun);
  1107. break;
  1108. case HPSA_LV_ENCRYPTED_NO_KEY:
  1109. dev_info(&h->pdev->dev,
  1110. "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
  1111. h->scsi_host->host_no,
  1112. sd->bus, sd->target, sd->lun);
  1113. break;
  1114. case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
  1115. dev_info(&h->pdev->dev,
  1116. "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
  1117. h->scsi_host->host_no,
  1118. sd->bus, sd->target, sd->lun);
  1119. break;
  1120. case HPSA_LV_UNDERGOING_ENCRYPTION:
  1121. dev_info(&h->pdev->dev,
  1122. "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
  1123. h->scsi_host->host_no,
  1124. sd->bus, sd->target, sd->lun);
  1125. break;
  1126. case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
  1127. dev_info(&h->pdev->dev,
  1128. "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
  1129. h->scsi_host->host_no,
  1130. sd->bus, sd->target, sd->lun);
  1131. break;
  1132. case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
  1133. dev_info(&h->pdev->dev,
  1134. "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
  1135. h->scsi_host->host_no,
  1136. sd->bus, sd->target, sd->lun);
  1137. break;
  1138. case HPSA_LV_PENDING_ENCRYPTION:
  1139. dev_info(&h->pdev->dev,
  1140. "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
  1141. h->scsi_host->host_no,
  1142. sd->bus, sd->target, sd->lun);
  1143. break;
  1144. case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
  1145. dev_info(&h->pdev->dev,
  1146. "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
  1147. h->scsi_host->host_no,
  1148. sd->bus, sd->target, sd->lun);
  1149. break;
  1150. }
  1151. }
  1152. static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
  1153. struct hpsa_scsi_dev_t *sd[], int nsds)
  1154. {
  1155. /* sd contains scsi3 addresses and devtypes, and inquiry
  1156. * data. This function takes what's in sd to be the current
  1157. * reality and updates h->dev[] to reflect that reality.
  1158. */
  1159. int i, entry, device_change, changes = 0;
  1160. struct hpsa_scsi_dev_t *csd;
  1161. unsigned long flags;
  1162. struct hpsa_scsi_dev_t **added, **removed;
  1163. int nadded, nremoved;
  1164. struct Scsi_Host *sh = NULL;
  1165. added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
  1166. removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
  1167. if (!added || !removed) {
  1168. dev_warn(&h->pdev->dev, "out of memory in "
  1169. "adjust_hpsa_scsi_table\n");
  1170. goto free_and_out;
  1171. }
  1172. spin_lock_irqsave(&h->devlock, flags);
  1173. /* find any devices in h->dev[] that are not in
  1174. * sd[] and remove them from h->dev[], and for any
  1175. * devices which have changed, remove the old device
  1176. * info and add the new device info.
  1177. * If minor device attributes change, just update
  1178. * the existing device structure.
  1179. */
  1180. i = 0;
  1181. nremoved = 0;
  1182. nadded = 0;
  1183. while (i < h->ndevices) {
  1184. csd = h->dev[i];
  1185. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  1186. if (device_change == DEVICE_NOT_FOUND) {
  1187. changes++;
  1188. hpsa_scsi_remove_entry(h, hostno, i,
  1189. removed, &nremoved);
  1190. continue; /* remove ^^^, hence i not incremented */
  1191. } else if (device_change == DEVICE_CHANGED) {
  1192. changes++;
  1193. hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
  1194. added, &nadded, removed, &nremoved);
  1195. /* Set it to NULL to prevent it from being freed
  1196. * at the bottom of hpsa_update_scsi_devices()
  1197. */
  1198. sd[entry] = NULL;
  1199. } else if (device_change == DEVICE_UPDATED) {
  1200. hpsa_scsi_update_entry(h, hostno, i, sd[entry]);
  1201. }
  1202. i++;
  1203. }
  1204. /* Now, make sure every device listed in sd[] is also
  1205. * listed in h->dev[], adding them if they aren't found
  1206. */
  1207. for (i = 0; i < nsds; i++) {
  1208. if (!sd[i]) /* if already added above. */
  1209. continue;
  1210. /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
  1211. * as the SCSI mid-layer does not handle such devices well.
  1212. * It relentlessly loops sending TUR at 3Hz, then READ(10)
  1213. * at 160Hz, and prevents the system from coming up.
  1214. */
  1215. if (sd[i]->volume_offline) {
  1216. hpsa_show_volume_status(h, sd[i]);
  1217. dev_info(&h->pdev->dev, "c%db%dt%dl%d: temporarily offline\n",
  1218. h->scsi_host->host_no,
  1219. sd[i]->bus, sd[i]->target, sd[i]->lun);
  1220. continue;
  1221. }
  1222. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  1223. h->ndevices, &entry);
  1224. if (device_change == DEVICE_NOT_FOUND) {
  1225. changes++;
  1226. if (hpsa_scsi_add_entry(h, hostno, sd[i],
  1227. added, &nadded) != 0)
  1228. break;
  1229. sd[i] = NULL; /* prevent from being freed later. */
  1230. } else if (device_change == DEVICE_CHANGED) {
  1231. /* should never happen... */
  1232. changes++;
  1233. dev_warn(&h->pdev->dev,
  1234. "device unexpectedly changed.\n");
  1235. /* but if it does happen, we just ignore that device */
  1236. }
  1237. }
  1238. spin_unlock_irqrestore(&h->devlock, flags);
  1239. /* Monitor devices which are in one of several NOT READY states to be
  1240. * brought online later. This must be done without holding h->devlock,
  1241. * so don't touch h->dev[]
  1242. */
  1243. for (i = 0; i < nsds; i++) {
  1244. if (!sd[i]) /* if already added above. */
  1245. continue;
  1246. if (sd[i]->volume_offline)
  1247. hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
  1248. }
  1249. /* Don't notify scsi mid layer of any changes the first time through
  1250. * (or if there are no changes) scsi_scan_host will do it later the
  1251. * first time through.
  1252. */
  1253. if (hostno == -1 || !changes)
  1254. goto free_and_out;
  1255. sh = h->scsi_host;
  1256. /* Notify scsi mid layer of any removed devices */
  1257. for (i = 0; i < nremoved; i++) {
  1258. struct scsi_device *sdev =
  1259. scsi_device_lookup(sh, removed[i]->bus,
  1260. removed[i]->target, removed[i]->lun);
  1261. if (sdev != NULL) {
  1262. scsi_remove_device(sdev);
  1263. scsi_device_put(sdev);
  1264. } else {
  1265. /* We don't expect to get here.
  1266. * future cmds to this device will get selection
  1267. * timeout as if the device was gone.
  1268. */
  1269. dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
  1270. " for removal.", hostno, removed[i]->bus,
  1271. removed[i]->target, removed[i]->lun);
  1272. }
  1273. kfree(removed[i]);
  1274. removed[i] = NULL;
  1275. }
  1276. /* Notify scsi mid layer of any added devices */
  1277. for (i = 0; i < nadded; i++) {
  1278. if (scsi_add_device(sh, added[i]->bus,
  1279. added[i]->target, added[i]->lun) == 0)
  1280. continue;
  1281. dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
  1282. "device not added.\n", hostno, added[i]->bus,
  1283. added[i]->target, added[i]->lun);
  1284. /* now we have to remove it from h->dev,
  1285. * since it didn't get added to scsi mid layer
  1286. */
  1287. fixup_botched_add(h, added[i]);
  1288. }
  1289. free_and_out:
  1290. kfree(added);
  1291. kfree(removed);
  1292. }
  1293. /*
  1294. * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
  1295. * Assume's h->devlock is held.
  1296. */
  1297. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  1298. int bus, int target, int lun)
  1299. {
  1300. int i;
  1301. struct hpsa_scsi_dev_t *sd;
  1302. for (i = 0; i < h->ndevices; i++) {
  1303. sd = h->dev[i];
  1304. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  1305. return sd;
  1306. }
  1307. return NULL;
  1308. }
  1309. /* link sdev->hostdata to our per-device structure. */
  1310. static int hpsa_slave_alloc(struct scsi_device *sdev)
  1311. {
  1312. struct hpsa_scsi_dev_t *sd;
  1313. unsigned long flags;
  1314. struct ctlr_info *h;
  1315. h = sdev_to_hba(sdev);
  1316. spin_lock_irqsave(&h->devlock, flags);
  1317. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  1318. sdev_id(sdev), sdev->lun);
  1319. if (sd != NULL)
  1320. sdev->hostdata = sd;
  1321. spin_unlock_irqrestore(&h->devlock, flags);
  1322. return 0;
  1323. }
  1324. static void hpsa_slave_destroy(struct scsi_device *sdev)
  1325. {
  1326. /* nothing to do. */
  1327. }
  1328. static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
  1329. {
  1330. int i;
  1331. if (!h->cmd_sg_list)
  1332. return;
  1333. for (i = 0; i < h->nr_cmds; i++) {
  1334. kfree(h->cmd_sg_list[i]);
  1335. h->cmd_sg_list[i] = NULL;
  1336. }
  1337. kfree(h->cmd_sg_list);
  1338. h->cmd_sg_list = NULL;
  1339. }
  1340. static int hpsa_allocate_sg_chain_blocks(struct ctlr_info *h)
  1341. {
  1342. int i;
  1343. if (h->chainsize <= 0)
  1344. return 0;
  1345. h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
  1346. GFP_KERNEL);
  1347. if (!h->cmd_sg_list)
  1348. return -ENOMEM;
  1349. for (i = 0; i < h->nr_cmds; i++) {
  1350. h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
  1351. h->chainsize, GFP_KERNEL);
  1352. if (!h->cmd_sg_list[i])
  1353. goto clean;
  1354. }
  1355. return 0;
  1356. clean:
  1357. hpsa_free_sg_chain_blocks(h);
  1358. return -ENOMEM;
  1359. }
  1360. static int hpsa_map_sg_chain_block(struct ctlr_info *h,
  1361. struct CommandList *c)
  1362. {
  1363. struct SGDescriptor *chain_sg, *chain_block;
  1364. u64 temp64;
  1365. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  1366. chain_block = h->cmd_sg_list[c->cmdindex];
  1367. chain_sg->Ext = HPSA_SG_CHAIN;
  1368. chain_sg->Len = sizeof(*chain_sg) *
  1369. (c->Header.SGTotal - h->max_cmd_sg_entries);
  1370. temp64 = pci_map_single(h->pdev, chain_block, chain_sg->Len,
  1371. PCI_DMA_TODEVICE);
  1372. if (dma_mapping_error(&h->pdev->dev, temp64)) {
  1373. /* prevent subsequent unmapping */
  1374. chain_sg->Addr.lower = 0;
  1375. chain_sg->Addr.upper = 0;
  1376. return -1;
  1377. }
  1378. chain_sg->Addr.lower = (u32) (temp64 & 0x0FFFFFFFFULL);
  1379. chain_sg->Addr.upper = (u32) ((temp64 >> 32) & 0x0FFFFFFFFULL);
  1380. return 0;
  1381. }
  1382. static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
  1383. struct CommandList *c)
  1384. {
  1385. struct SGDescriptor *chain_sg;
  1386. union u64bit temp64;
  1387. if (c->Header.SGTotal <= h->max_cmd_sg_entries)
  1388. return;
  1389. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  1390. temp64.val32.lower = chain_sg->Addr.lower;
  1391. temp64.val32.upper = chain_sg->Addr.upper;
  1392. pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
  1393. }
  1394. /* Decode the various types of errors on ioaccel2 path.
  1395. * Return 1 for any error that should generate a RAID path retry.
  1396. * Return 0 for errors that don't require a RAID path retry.
  1397. */
  1398. static int handle_ioaccel_mode2_error(struct ctlr_info *h,
  1399. struct CommandList *c,
  1400. struct scsi_cmnd *cmd,
  1401. struct io_accel2_cmd *c2)
  1402. {
  1403. int data_len;
  1404. int retry = 0;
  1405. switch (c2->error_data.serv_response) {
  1406. case IOACCEL2_SERV_RESPONSE_COMPLETE:
  1407. switch (c2->error_data.status) {
  1408. case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
  1409. break;
  1410. case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
  1411. dev_warn(&h->pdev->dev,
  1412. "%s: task complete with check condition.\n",
  1413. "HP SSD Smart Path");
  1414. cmd->result |= SAM_STAT_CHECK_CONDITION;
  1415. if (c2->error_data.data_present !=
  1416. IOACCEL2_SENSE_DATA_PRESENT) {
  1417. memset(cmd->sense_buffer, 0,
  1418. SCSI_SENSE_BUFFERSIZE);
  1419. break;
  1420. }
  1421. /* copy the sense data */
  1422. data_len = c2->error_data.sense_data_len;
  1423. if (data_len > SCSI_SENSE_BUFFERSIZE)
  1424. data_len = SCSI_SENSE_BUFFERSIZE;
  1425. if (data_len > sizeof(c2->error_data.sense_data_buff))
  1426. data_len =
  1427. sizeof(c2->error_data.sense_data_buff);
  1428. memcpy(cmd->sense_buffer,
  1429. c2->error_data.sense_data_buff, data_len);
  1430. retry = 1;
  1431. break;
  1432. case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
  1433. dev_warn(&h->pdev->dev,
  1434. "%s: task complete with BUSY status.\n",
  1435. "HP SSD Smart Path");
  1436. retry = 1;
  1437. break;
  1438. case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
  1439. dev_warn(&h->pdev->dev,
  1440. "%s: task complete with reservation conflict.\n",
  1441. "HP SSD Smart Path");
  1442. retry = 1;
  1443. break;
  1444. case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
  1445. /* Make scsi midlayer do unlimited retries */
  1446. cmd->result = DID_IMM_RETRY << 16;
  1447. break;
  1448. case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
  1449. dev_warn(&h->pdev->dev,
  1450. "%s: task complete with aborted status.\n",
  1451. "HP SSD Smart Path");
  1452. retry = 1;
  1453. break;
  1454. default:
  1455. dev_warn(&h->pdev->dev,
  1456. "%s: task complete with unrecognized status: 0x%02x\n",
  1457. "HP SSD Smart Path", c2->error_data.status);
  1458. retry = 1;
  1459. break;
  1460. }
  1461. break;
  1462. case IOACCEL2_SERV_RESPONSE_FAILURE:
  1463. /* don't expect to get here. */
  1464. dev_warn(&h->pdev->dev,
  1465. "unexpected delivery or target failure, status = 0x%02x\n",
  1466. c2->error_data.status);
  1467. retry = 1;
  1468. break;
  1469. case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
  1470. break;
  1471. case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
  1472. break;
  1473. case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
  1474. dev_warn(&h->pdev->dev, "task management function rejected.\n");
  1475. retry = 1;
  1476. break;
  1477. case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
  1478. dev_warn(&h->pdev->dev, "task management function invalid LUN\n");
  1479. break;
  1480. default:
  1481. dev_warn(&h->pdev->dev,
  1482. "%s: Unrecognized server response: 0x%02x\n",
  1483. "HP SSD Smart Path",
  1484. c2->error_data.serv_response);
  1485. retry = 1;
  1486. break;
  1487. }
  1488. return retry; /* retry on raid path? */
  1489. }
  1490. static void process_ioaccel2_completion(struct ctlr_info *h,
  1491. struct CommandList *c, struct scsi_cmnd *cmd,
  1492. struct hpsa_scsi_dev_t *dev)
  1493. {
  1494. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  1495. int raid_retry = 0;
  1496. /* check for good status */
  1497. if (likely(c2->error_data.serv_response == 0 &&
  1498. c2->error_data.status == 0)) {
  1499. cmd_free(h, c);
  1500. cmd->scsi_done(cmd);
  1501. return;
  1502. }
  1503. /* Any RAID offload error results in retry which will use
  1504. * the normal I/O path so the controller can handle whatever's
  1505. * wrong.
  1506. */
  1507. if (is_logical_dev_addr_mode(dev->scsi3addr) &&
  1508. c2->error_data.serv_response ==
  1509. IOACCEL2_SERV_RESPONSE_FAILURE) {
  1510. dev->offload_enabled = 0;
  1511. h->drv_req_rescan = 1; /* schedule controller for a rescan */
  1512. cmd->result = DID_SOFT_ERROR << 16;
  1513. cmd_free(h, c);
  1514. cmd->scsi_done(cmd);
  1515. return;
  1516. }
  1517. raid_retry = handle_ioaccel_mode2_error(h, c, cmd, c2);
  1518. /* If error found, disable Smart Path, schedule a rescan,
  1519. * and force a retry on the standard path.
  1520. */
  1521. if (raid_retry) {
  1522. dev_warn(&h->pdev->dev, "%s: Retrying on standard path.\n",
  1523. "HP SSD Smart Path");
  1524. dev->offload_enabled = 0; /* Disable Smart Path */
  1525. h->drv_req_rescan = 1; /* schedule controller rescan */
  1526. cmd->result = DID_SOFT_ERROR << 16;
  1527. }
  1528. cmd_free(h, c);
  1529. cmd->scsi_done(cmd);
  1530. }
  1531. static void complete_scsi_command(struct CommandList *cp)
  1532. {
  1533. struct scsi_cmnd *cmd;
  1534. struct ctlr_info *h;
  1535. struct ErrorInfo *ei;
  1536. struct hpsa_scsi_dev_t *dev;
  1537. unsigned char sense_key;
  1538. unsigned char asc; /* additional sense code */
  1539. unsigned char ascq; /* additional sense code qualifier */
  1540. unsigned long sense_data_size;
  1541. ei = cp->err_info;
  1542. cmd = (struct scsi_cmnd *) cp->scsi_cmd;
  1543. h = cp->h;
  1544. dev = cmd->device->hostdata;
  1545. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  1546. if ((cp->cmd_type == CMD_SCSI) &&
  1547. (cp->Header.SGTotal > h->max_cmd_sg_entries))
  1548. hpsa_unmap_sg_chain_block(h, cp);
  1549. cmd->result = (DID_OK << 16); /* host byte */
  1550. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  1551. if (cp->cmd_type == CMD_IOACCEL2)
  1552. return process_ioaccel2_completion(h, cp, cmd, dev);
  1553. cmd->result |= ei->ScsiStatus;
  1554. scsi_set_resid(cmd, ei->ResidualCnt);
  1555. if (ei->CommandStatus == 0) {
  1556. cmd_free(h, cp);
  1557. cmd->scsi_done(cmd);
  1558. return;
  1559. }
  1560. /* copy the sense data */
  1561. if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
  1562. sense_data_size = SCSI_SENSE_BUFFERSIZE;
  1563. else
  1564. sense_data_size = sizeof(ei->SenseInfo);
  1565. if (ei->SenseLen < sense_data_size)
  1566. sense_data_size = ei->SenseLen;
  1567. memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
  1568. /* For I/O accelerator commands, copy over some fields to the normal
  1569. * CISS header used below for error handling.
  1570. */
  1571. if (cp->cmd_type == CMD_IOACCEL1) {
  1572. struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
  1573. cp->Header.SGList = cp->Header.SGTotal = scsi_sg_count(cmd);
  1574. cp->Request.CDBLen = c->io_flags & IOACCEL1_IOFLAGS_CDBLEN_MASK;
  1575. cp->Header.Tag.lower = c->Tag.lower;
  1576. cp->Header.Tag.upper = c->Tag.upper;
  1577. memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
  1578. memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
  1579. /* Any RAID offload error results in retry which will use
  1580. * the normal I/O path so the controller can handle whatever's
  1581. * wrong.
  1582. */
  1583. if (is_logical_dev_addr_mode(dev->scsi3addr)) {
  1584. if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
  1585. dev->offload_enabled = 0;
  1586. cmd->result = DID_SOFT_ERROR << 16;
  1587. cmd_free(h, cp);
  1588. cmd->scsi_done(cmd);
  1589. return;
  1590. }
  1591. }
  1592. /* an error has occurred */
  1593. switch (ei->CommandStatus) {
  1594. case CMD_TARGET_STATUS:
  1595. if (ei->ScsiStatus) {
  1596. /* Get sense key */
  1597. sense_key = 0xf & ei->SenseInfo[2];
  1598. /* Get additional sense code */
  1599. asc = ei->SenseInfo[12];
  1600. /* Get addition sense code qualifier */
  1601. ascq = ei->SenseInfo[13];
  1602. }
  1603. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  1604. if (check_for_unit_attention(h, cp))
  1605. break;
  1606. if (sense_key == ILLEGAL_REQUEST) {
  1607. /*
  1608. * SCSI REPORT_LUNS is commonly unsupported on
  1609. * Smart Array. Suppress noisy complaint.
  1610. */
  1611. if (cp->Request.CDB[0] == REPORT_LUNS)
  1612. break;
  1613. /* If ASC/ASCQ indicate Logical Unit
  1614. * Not Supported condition,
  1615. */
  1616. if ((asc == 0x25) && (ascq == 0x0)) {
  1617. dev_warn(&h->pdev->dev, "cp %p "
  1618. "has check condition\n", cp);
  1619. break;
  1620. }
  1621. }
  1622. if (sense_key == NOT_READY) {
  1623. /* If Sense is Not Ready, Logical Unit
  1624. * Not ready, Manual Intervention
  1625. * required
  1626. */
  1627. if ((asc == 0x04) && (ascq == 0x03)) {
  1628. dev_warn(&h->pdev->dev, "cp %p "
  1629. "has check condition: unit "
  1630. "not ready, manual "
  1631. "intervention required\n", cp);
  1632. break;
  1633. }
  1634. }
  1635. if (sense_key == ABORTED_COMMAND) {
  1636. /* Aborted command is retryable */
  1637. dev_warn(&h->pdev->dev, "cp %p "
  1638. "has check condition: aborted command: "
  1639. "ASC: 0x%x, ASCQ: 0x%x\n",
  1640. cp, asc, ascq);
  1641. cmd->result |= DID_SOFT_ERROR << 16;
  1642. break;
  1643. }
  1644. /* Must be some other type of check condition */
  1645. dev_dbg(&h->pdev->dev, "cp %p has check condition: "
  1646. "unknown type: "
  1647. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  1648. "Returning result: 0x%x, "
  1649. "cmd=[%02x %02x %02x %02x %02x "
  1650. "%02x %02x %02x %02x %02x %02x "
  1651. "%02x %02x %02x %02x %02x]\n",
  1652. cp, sense_key, asc, ascq,
  1653. cmd->result,
  1654. cmd->cmnd[0], cmd->cmnd[1],
  1655. cmd->cmnd[2], cmd->cmnd[3],
  1656. cmd->cmnd[4], cmd->cmnd[5],
  1657. cmd->cmnd[6], cmd->cmnd[7],
  1658. cmd->cmnd[8], cmd->cmnd[9],
  1659. cmd->cmnd[10], cmd->cmnd[11],
  1660. cmd->cmnd[12], cmd->cmnd[13],
  1661. cmd->cmnd[14], cmd->cmnd[15]);
  1662. break;
  1663. }
  1664. /* Problem was not a check condition
  1665. * Pass it up to the upper layers...
  1666. */
  1667. if (ei->ScsiStatus) {
  1668. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  1669. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  1670. "Returning result: 0x%x\n",
  1671. cp, ei->ScsiStatus,
  1672. sense_key, asc, ascq,
  1673. cmd->result);
  1674. } else { /* scsi status is zero??? How??? */
  1675. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  1676. "Returning no connection.\n", cp),
  1677. /* Ordinarily, this case should never happen,
  1678. * but there is a bug in some released firmware
  1679. * revisions that allows it to happen if, for
  1680. * example, a 4100 backplane loses power and
  1681. * the tape drive is in it. We assume that
  1682. * it's a fatal error of some kind because we
  1683. * can't show that it wasn't. We will make it
  1684. * look like selection timeout since that is
  1685. * the most common reason for this to occur,
  1686. * and it's severe enough.
  1687. */
  1688. cmd->result = DID_NO_CONNECT << 16;
  1689. }
  1690. break;
  1691. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1692. break;
  1693. case CMD_DATA_OVERRUN:
  1694. dev_warn(&h->pdev->dev, "cp %p has"
  1695. " completed with data overrun "
  1696. "reported\n", cp);
  1697. break;
  1698. case CMD_INVALID: {
  1699. /* print_bytes(cp, sizeof(*cp), 1, 0);
  1700. print_cmd(cp); */
  1701. /* We get CMD_INVALID if you address a non-existent device
  1702. * instead of a selection timeout (no response). You will
  1703. * see this if you yank out a drive, then try to access it.
  1704. * This is kind of a shame because it means that any other
  1705. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  1706. * missing target. */
  1707. cmd->result = DID_NO_CONNECT << 16;
  1708. }
  1709. break;
  1710. case CMD_PROTOCOL_ERR:
  1711. cmd->result = DID_ERROR << 16;
  1712. dev_warn(&h->pdev->dev, "cp %p has "
  1713. "protocol error\n", cp);
  1714. break;
  1715. case CMD_HARDWARE_ERR:
  1716. cmd->result = DID_ERROR << 16;
  1717. dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
  1718. break;
  1719. case CMD_CONNECTION_LOST:
  1720. cmd->result = DID_ERROR << 16;
  1721. dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
  1722. break;
  1723. case CMD_ABORTED:
  1724. cmd->result = DID_ABORT << 16;
  1725. dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
  1726. cp, ei->ScsiStatus);
  1727. break;
  1728. case CMD_ABORT_FAILED:
  1729. cmd->result = DID_ERROR << 16;
  1730. dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
  1731. break;
  1732. case CMD_UNSOLICITED_ABORT:
  1733. cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
  1734. dev_warn(&h->pdev->dev, "cp %p aborted due to an unsolicited "
  1735. "abort\n", cp);
  1736. break;
  1737. case CMD_TIMEOUT:
  1738. cmd->result = DID_TIME_OUT << 16;
  1739. dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
  1740. break;
  1741. case CMD_UNABORTABLE:
  1742. cmd->result = DID_ERROR << 16;
  1743. dev_warn(&h->pdev->dev, "Command unabortable\n");
  1744. break;
  1745. case CMD_IOACCEL_DISABLED:
  1746. /* This only handles the direct pass-through case since RAID
  1747. * offload is handled above. Just attempt a retry.
  1748. */
  1749. cmd->result = DID_SOFT_ERROR << 16;
  1750. dev_warn(&h->pdev->dev,
  1751. "cp %p had HP SSD Smart Path error\n", cp);
  1752. break;
  1753. default:
  1754. cmd->result = DID_ERROR << 16;
  1755. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  1756. cp, ei->CommandStatus);
  1757. }
  1758. cmd_free(h, cp);
  1759. cmd->scsi_done(cmd);
  1760. }
  1761. static void hpsa_pci_unmap(struct pci_dev *pdev,
  1762. struct CommandList *c, int sg_used, int data_direction)
  1763. {
  1764. int i;
  1765. union u64bit addr64;
  1766. for (i = 0; i < sg_used; i++) {
  1767. addr64.val32.lower = c->SG[i].Addr.lower;
  1768. addr64.val32.upper = c->SG[i].Addr.upper;
  1769. pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
  1770. data_direction);
  1771. }
  1772. }
  1773. static int hpsa_map_one(struct pci_dev *pdev,
  1774. struct CommandList *cp,
  1775. unsigned char *buf,
  1776. size_t buflen,
  1777. int data_direction)
  1778. {
  1779. u64 addr64;
  1780. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  1781. cp->Header.SGList = 0;
  1782. cp->Header.SGTotal = 0;
  1783. return 0;
  1784. }
  1785. addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
  1786. if (dma_mapping_error(&pdev->dev, addr64)) {
  1787. /* Prevent subsequent unmap of something never mapped */
  1788. cp->Header.SGList = 0;
  1789. cp->Header.SGTotal = 0;
  1790. return -1;
  1791. }
  1792. cp->SG[0].Addr.lower =
  1793. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1794. cp->SG[0].Addr.upper =
  1795. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1796. cp->SG[0].Len = buflen;
  1797. cp->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining */
  1798. cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
  1799. cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
  1800. return 0;
  1801. }
  1802. static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  1803. struct CommandList *c)
  1804. {
  1805. DECLARE_COMPLETION_ONSTACK(wait);
  1806. c->waiting = &wait;
  1807. enqueue_cmd_and_start_io(h, c);
  1808. wait_for_completion(&wait);
  1809. }
  1810. static u32 lockup_detected(struct ctlr_info *h)
  1811. {
  1812. int cpu;
  1813. u32 rc, *lockup_detected;
  1814. cpu = get_cpu();
  1815. lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
  1816. rc = *lockup_detected;
  1817. put_cpu();
  1818. return rc;
  1819. }
  1820. static void hpsa_scsi_do_simple_cmd_core_if_no_lockup(struct ctlr_info *h,
  1821. struct CommandList *c)
  1822. {
  1823. /* If controller lockup detected, fake a hardware error. */
  1824. if (unlikely(lockup_detected(h)))
  1825. c->err_info->CommandStatus = CMD_HARDWARE_ERR;
  1826. else
  1827. hpsa_scsi_do_simple_cmd_core(h, c);
  1828. }
  1829. #define MAX_DRIVER_CMD_RETRIES 25
  1830. static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  1831. struct CommandList *c, int data_direction)
  1832. {
  1833. int backoff_time = 10, retry_count = 0;
  1834. do {
  1835. memset(c->err_info, 0, sizeof(*c->err_info));
  1836. hpsa_scsi_do_simple_cmd_core(h, c);
  1837. retry_count++;
  1838. if (retry_count > 3) {
  1839. msleep(backoff_time);
  1840. if (backoff_time < 1000)
  1841. backoff_time *= 2;
  1842. }
  1843. } while ((check_for_unit_attention(h, c) ||
  1844. check_for_busy(h, c)) &&
  1845. retry_count <= MAX_DRIVER_CMD_RETRIES);
  1846. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  1847. }
  1848. static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
  1849. struct CommandList *c)
  1850. {
  1851. const u8 *cdb = c->Request.CDB;
  1852. const u8 *lun = c->Header.LUN.LunAddrBytes;
  1853. dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
  1854. " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  1855. txt, lun[0], lun[1], lun[2], lun[3],
  1856. lun[4], lun[5], lun[6], lun[7],
  1857. cdb[0], cdb[1], cdb[2], cdb[3],
  1858. cdb[4], cdb[5], cdb[6], cdb[7],
  1859. cdb[8], cdb[9], cdb[10], cdb[11],
  1860. cdb[12], cdb[13], cdb[14], cdb[15]);
  1861. }
  1862. static void hpsa_scsi_interpret_error(struct ctlr_info *h,
  1863. struct CommandList *cp)
  1864. {
  1865. const struct ErrorInfo *ei = cp->err_info;
  1866. struct device *d = &cp->h->pdev->dev;
  1867. const u8 *sd = ei->SenseInfo;
  1868. switch (ei->CommandStatus) {
  1869. case CMD_TARGET_STATUS:
  1870. hpsa_print_cmd(h, "SCSI status", cp);
  1871. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
  1872. dev_warn(d, "SCSI Status = 02, Sense key = %02x, ASC = %02x, ASCQ = %02x\n",
  1873. sd[2] & 0x0f, sd[12], sd[13]);
  1874. else
  1875. dev_warn(d, "SCSI Status = %02x\n", ei->ScsiStatus);
  1876. if (ei->ScsiStatus == 0)
  1877. dev_warn(d, "SCSI status is abnormally zero. "
  1878. "(probably indicates selection timeout "
  1879. "reported incorrectly due to a known "
  1880. "firmware bug, circa July, 2001.)\n");
  1881. break;
  1882. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1883. break;
  1884. case CMD_DATA_OVERRUN:
  1885. hpsa_print_cmd(h, "overrun condition", cp);
  1886. break;
  1887. case CMD_INVALID: {
  1888. /* controller unfortunately reports SCSI passthru's
  1889. * to non-existent targets as invalid commands.
  1890. */
  1891. hpsa_print_cmd(h, "invalid command", cp);
  1892. dev_warn(d, "probably means device no longer present\n");
  1893. }
  1894. break;
  1895. case CMD_PROTOCOL_ERR:
  1896. hpsa_print_cmd(h, "protocol error", cp);
  1897. break;
  1898. case CMD_HARDWARE_ERR:
  1899. hpsa_print_cmd(h, "hardware error", cp);
  1900. break;
  1901. case CMD_CONNECTION_LOST:
  1902. hpsa_print_cmd(h, "connection lost", cp);
  1903. break;
  1904. case CMD_ABORTED:
  1905. hpsa_print_cmd(h, "aborted", cp);
  1906. break;
  1907. case CMD_ABORT_FAILED:
  1908. hpsa_print_cmd(h, "abort failed", cp);
  1909. break;
  1910. case CMD_UNSOLICITED_ABORT:
  1911. hpsa_print_cmd(h, "unsolicited abort", cp);
  1912. break;
  1913. case CMD_TIMEOUT:
  1914. hpsa_print_cmd(h, "timed out", cp);
  1915. break;
  1916. case CMD_UNABORTABLE:
  1917. hpsa_print_cmd(h, "unabortable", cp);
  1918. break;
  1919. default:
  1920. hpsa_print_cmd(h, "unknown status", cp);
  1921. dev_warn(d, "Unknown command status %x\n",
  1922. ei->CommandStatus);
  1923. }
  1924. }
  1925. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  1926. u16 page, unsigned char *buf,
  1927. unsigned char bufsize)
  1928. {
  1929. int rc = IO_OK;
  1930. struct CommandList *c;
  1931. struct ErrorInfo *ei;
  1932. c = cmd_special_alloc(h);
  1933. if (c == NULL) { /* trouble... */
  1934. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1935. return -ENOMEM;
  1936. }
  1937. if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
  1938. page, scsi3addr, TYPE_CMD)) {
  1939. rc = -1;
  1940. goto out;
  1941. }
  1942. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1943. ei = c->err_info;
  1944. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1945. hpsa_scsi_interpret_error(h, c);
  1946. rc = -1;
  1947. }
  1948. out:
  1949. cmd_special_free(h, c);
  1950. return rc;
  1951. }
  1952. static int hpsa_bmic_ctrl_mode_sense(struct ctlr_info *h,
  1953. unsigned char *scsi3addr, unsigned char page,
  1954. struct bmic_controller_parameters *buf, size_t bufsize)
  1955. {
  1956. int rc = IO_OK;
  1957. struct CommandList *c;
  1958. struct ErrorInfo *ei;
  1959. c = cmd_special_alloc(h);
  1960. if (c == NULL) { /* trouble... */
  1961. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1962. return -ENOMEM;
  1963. }
  1964. if (fill_cmd(c, BMIC_SENSE_CONTROLLER_PARAMETERS, h, buf, bufsize,
  1965. page, scsi3addr, TYPE_CMD)) {
  1966. rc = -1;
  1967. goto out;
  1968. }
  1969. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1970. ei = c->err_info;
  1971. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1972. hpsa_scsi_interpret_error(h, c);
  1973. rc = -1;
  1974. }
  1975. out:
  1976. cmd_special_free(h, c);
  1977. return rc;
  1978. }
  1979. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
  1980. u8 reset_type)
  1981. {
  1982. int rc = IO_OK;
  1983. struct CommandList *c;
  1984. struct ErrorInfo *ei;
  1985. c = cmd_special_alloc(h);
  1986. if (c == NULL) { /* trouble... */
  1987. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1988. return -ENOMEM;
  1989. }
  1990. /* fill_cmd can't fail here, no data buffer to map. */
  1991. (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
  1992. scsi3addr, TYPE_MSG);
  1993. c->Request.CDB[1] = reset_type; /* fill_cmd defaults to LUN reset */
  1994. hpsa_scsi_do_simple_cmd_core(h, c);
  1995. /* no unmap needed here because no data xfer. */
  1996. ei = c->err_info;
  1997. if (ei->CommandStatus != 0) {
  1998. hpsa_scsi_interpret_error(h, c);
  1999. rc = -1;
  2000. }
  2001. cmd_special_free(h, c);
  2002. return rc;
  2003. }
  2004. static void hpsa_get_raid_level(struct ctlr_info *h,
  2005. unsigned char *scsi3addr, unsigned char *raid_level)
  2006. {
  2007. int rc;
  2008. unsigned char *buf;
  2009. *raid_level = RAID_UNKNOWN;
  2010. buf = kzalloc(64, GFP_KERNEL);
  2011. if (!buf)
  2012. return;
  2013. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0xC1, buf, 64);
  2014. if (rc == 0)
  2015. *raid_level = buf[8];
  2016. if (*raid_level > RAID_UNKNOWN)
  2017. *raid_level = RAID_UNKNOWN;
  2018. kfree(buf);
  2019. return;
  2020. }
  2021. #define HPSA_MAP_DEBUG
  2022. #ifdef HPSA_MAP_DEBUG
  2023. static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
  2024. struct raid_map_data *map_buff)
  2025. {
  2026. struct raid_map_disk_data *dd = &map_buff->data[0];
  2027. int map, row, col;
  2028. u16 map_cnt, row_cnt, disks_per_row;
  2029. if (rc != 0)
  2030. return;
  2031. /* Show details only if debugging has been activated. */
  2032. if (h->raid_offload_debug < 2)
  2033. return;
  2034. dev_info(&h->pdev->dev, "structure_size = %u\n",
  2035. le32_to_cpu(map_buff->structure_size));
  2036. dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
  2037. le32_to_cpu(map_buff->volume_blk_size));
  2038. dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
  2039. le64_to_cpu(map_buff->volume_blk_cnt));
  2040. dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
  2041. map_buff->phys_blk_shift);
  2042. dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
  2043. map_buff->parity_rotation_shift);
  2044. dev_info(&h->pdev->dev, "strip_size = %u\n",
  2045. le16_to_cpu(map_buff->strip_size));
  2046. dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
  2047. le64_to_cpu(map_buff->disk_starting_blk));
  2048. dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
  2049. le64_to_cpu(map_buff->disk_blk_cnt));
  2050. dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
  2051. le16_to_cpu(map_buff->data_disks_per_row));
  2052. dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
  2053. le16_to_cpu(map_buff->metadata_disks_per_row));
  2054. dev_info(&h->pdev->dev, "row_cnt = %u\n",
  2055. le16_to_cpu(map_buff->row_cnt));
  2056. dev_info(&h->pdev->dev, "layout_map_count = %u\n",
  2057. le16_to_cpu(map_buff->layout_map_count));
  2058. dev_info(&h->pdev->dev, "flags = %u\n",
  2059. le16_to_cpu(map_buff->flags));
  2060. if (map_buff->flags & RAID_MAP_FLAG_ENCRYPT_ON)
  2061. dev_info(&h->pdev->dev, "encrypytion = ON\n");
  2062. else
  2063. dev_info(&h->pdev->dev, "encrypytion = OFF\n");
  2064. dev_info(&h->pdev->dev, "dekindex = %u\n",
  2065. le16_to_cpu(map_buff->dekindex));
  2066. map_cnt = le16_to_cpu(map_buff->layout_map_count);
  2067. for (map = 0; map < map_cnt; map++) {
  2068. dev_info(&h->pdev->dev, "Map%u:\n", map);
  2069. row_cnt = le16_to_cpu(map_buff->row_cnt);
  2070. for (row = 0; row < row_cnt; row++) {
  2071. dev_info(&h->pdev->dev, " Row%u:\n", row);
  2072. disks_per_row =
  2073. le16_to_cpu(map_buff->data_disks_per_row);
  2074. for (col = 0; col < disks_per_row; col++, dd++)
  2075. dev_info(&h->pdev->dev,
  2076. " D%02u: h=0x%04x xor=%u,%u\n",
  2077. col, dd->ioaccel_handle,
  2078. dd->xor_mult[0], dd->xor_mult[1]);
  2079. disks_per_row =
  2080. le16_to_cpu(map_buff->metadata_disks_per_row);
  2081. for (col = 0; col < disks_per_row; col++, dd++)
  2082. dev_info(&h->pdev->dev,
  2083. " M%02u: h=0x%04x xor=%u,%u\n",
  2084. col, dd->ioaccel_handle,
  2085. dd->xor_mult[0], dd->xor_mult[1]);
  2086. }
  2087. }
  2088. }
  2089. #else
  2090. static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
  2091. __attribute__((unused)) int rc,
  2092. __attribute__((unused)) struct raid_map_data *map_buff)
  2093. {
  2094. }
  2095. #endif
  2096. static int hpsa_get_raid_map(struct ctlr_info *h,
  2097. unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
  2098. {
  2099. int rc = 0;
  2100. struct CommandList *c;
  2101. struct ErrorInfo *ei;
  2102. c = cmd_special_alloc(h);
  2103. if (c == NULL) {
  2104. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  2105. return -ENOMEM;
  2106. }
  2107. if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
  2108. sizeof(this_device->raid_map), 0,
  2109. scsi3addr, TYPE_CMD)) {
  2110. dev_warn(&h->pdev->dev, "Out of memory in hpsa_get_raid_map()\n");
  2111. cmd_special_free(h, c);
  2112. return -ENOMEM;
  2113. }
  2114. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  2115. ei = c->err_info;
  2116. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2117. hpsa_scsi_interpret_error(h, c);
  2118. cmd_special_free(h, c);
  2119. return -1;
  2120. }
  2121. cmd_special_free(h, c);
  2122. /* @todo in the future, dynamically allocate RAID map memory */
  2123. if (le32_to_cpu(this_device->raid_map.structure_size) >
  2124. sizeof(this_device->raid_map)) {
  2125. dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
  2126. rc = -1;
  2127. }
  2128. hpsa_debug_map_buff(h, rc, &this_device->raid_map);
  2129. return rc;
  2130. }
  2131. static int hpsa_vpd_page_supported(struct ctlr_info *h,
  2132. unsigned char scsi3addr[], u8 page)
  2133. {
  2134. int rc;
  2135. int i;
  2136. int pages;
  2137. unsigned char *buf, bufsize;
  2138. buf = kzalloc(256, GFP_KERNEL);
  2139. if (!buf)
  2140. return 0;
  2141. /* Get the size of the page list first */
  2142. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  2143. VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
  2144. buf, HPSA_VPD_HEADER_SZ);
  2145. if (rc != 0)
  2146. goto exit_unsupported;
  2147. pages = buf[3];
  2148. if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
  2149. bufsize = pages + HPSA_VPD_HEADER_SZ;
  2150. else
  2151. bufsize = 255;
  2152. /* Get the whole VPD page list */
  2153. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  2154. VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
  2155. buf, bufsize);
  2156. if (rc != 0)
  2157. goto exit_unsupported;
  2158. pages = buf[3];
  2159. for (i = 1; i <= pages; i++)
  2160. if (buf[3 + i] == page)
  2161. goto exit_supported;
  2162. exit_unsupported:
  2163. kfree(buf);
  2164. return 0;
  2165. exit_supported:
  2166. kfree(buf);
  2167. return 1;
  2168. }
  2169. static void hpsa_get_ioaccel_status(struct ctlr_info *h,
  2170. unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
  2171. {
  2172. int rc;
  2173. unsigned char *buf;
  2174. u8 ioaccel_status;
  2175. this_device->offload_config = 0;
  2176. this_device->offload_enabled = 0;
  2177. buf = kzalloc(64, GFP_KERNEL);
  2178. if (!buf)
  2179. return;
  2180. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
  2181. goto out;
  2182. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  2183. VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
  2184. if (rc != 0)
  2185. goto out;
  2186. #define IOACCEL_STATUS_BYTE 4
  2187. #define OFFLOAD_CONFIGURED_BIT 0x01
  2188. #define OFFLOAD_ENABLED_BIT 0x02
  2189. ioaccel_status = buf[IOACCEL_STATUS_BYTE];
  2190. this_device->offload_config =
  2191. !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
  2192. if (this_device->offload_config) {
  2193. this_device->offload_enabled =
  2194. !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
  2195. if (hpsa_get_raid_map(h, scsi3addr, this_device))
  2196. this_device->offload_enabled = 0;
  2197. }
  2198. out:
  2199. kfree(buf);
  2200. return;
  2201. }
  2202. /* Get the device id from inquiry page 0x83 */
  2203. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  2204. unsigned char *device_id, int buflen)
  2205. {
  2206. int rc;
  2207. unsigned char *buf;
  2208. if (buflen > 16)
  2209. buflen = 16;
  2210. buf = kzalloc(64, GFP_KERNEL);
  2211. if (!buf)
  2212. return -ENOMEM;
  2213. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | 0x83, buf, 64);
  2214. if (rc == 0)
  2215. memcpy(device_id, &buf[8], buflen);
  2216. kfree(buf);
  2217. return rc != 0;
  2218. }
  2219. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  2220. struct ReportLUNdata *buf, int bufsize,
  2221. int extended_response)
  2222. {
  2223. int rc = IO_OK;
  2224. struct CommandList *c;
  2225. unsigned char scsi3addr[8];
  2226. struct ErrorInfo *ei;
  2227. c = cmd_special_alloc(h);
  2228. if (c == NULL) { /* trouble... */
  2229. dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  2230. return -1;
  2231. }
  2232. /* address the controller */
  2233. memset(scsi3addr, 0, sizeof(scsi3addr));
  2234. if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  2235. buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
  2236. rc = -1;
  2237. goto out;
  2238. }
  2239. if (extended_response)
  2240. c->Request.CDB[1] = extended_response;
  2241. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  2242. ei = c->err_info;
  2243. if (ei->CommandStatus != 0 &&
  2244. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2245. hpsa_scsi_interpret_error(h, c);
  2246. rc = -1;
  2247. } else {
  2248. if (buf->extended_response_flag != extended_response) {
  2249. dev_err(&h->pdev->dev,
  2250. "report luns requested format %u, got %u\n",
  2251. extended_response,
  2252. buf->extended_response_flag);
  2253. rc = -1;
  2254. }
  2255. }
  2256. out:
  2257. cmd_special_free(h, c);
  2258. return rc;
  2259. }
  2260. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  2261. struct ReportLUNdata *buf,
  2262. int bufsize, int extended_response)
  2263. {
  2264. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
  2265. }
  2266. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  2267. struct ReportLUNdata *buf, int bufsize)
  2268. {
  2269. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  2270. }
  2271. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  2272. int bus, int target, int lun)
  2273. {
  2274. device->bus = bus;
  2275. device->target = target;
  2276. device->lun = lun;
  2277. }
  2278. /* Use VPD inquiry to get details of volume status */
  2279. static int hpsa_get_volume_status(struct ctlr_info *h,
  2280. unsigned char scsi3addr[])
  2281. {
  2282. int rc;
  2283. int status;
  2284. int size;
  2285. unsigned char *buf;
  2286. buf = kzalloc(64, GFP_KERNEL);
  2287. if (!buf)
  2288. return HPSA_VPD_LV_STATUS_UNSUPPORTED;
  2289. /* Does controller have VPD for logical volume status? */
  2290. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
  2291. goto exit_failed;
  2292. /* Get the size of the VPD return buffer */
  2293. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
  2294. buf, HPSA_VPD_HEADER_SZ);
  2295. if (rc != 0)
  2296. goto exit_failed;
  2297. size = buf[3];
  2298. /* Now get the whole VPD buffer */
  2299. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
  2300. buf, size + HPSA_VPD_HEADER_SZ);
  2301. if (rc != 0)
  2302. goto exit_failed;
  2303. status = buf[4]; /* status byte */
  2304. kfree(buf);
  2305. return status;
  2306. exit_failed:
  2307. kfree(buf);
  2308. return HPSA_VPD_LV_STATUS_UNSUPPORTED;
  2309. }
  2310. /* Determine offline status of a volume.
  2311. * Return either:
  2312. * 0 (not offline)
  2313. * 0xff (offline for unknown reasons)
  2314. * # (integer code indicating one of several NOT READY states
  2315. * describing why a volume is to be kept offline)
  2316. */
  2317. static int hpsa_volume_offline(struct ctlr_info *h,
  2318. unsigned char scsi3addr[])
  2319. {
  2320. struct CommandList *c;
  2321. unsigned char *sense, sense_key, asc, ascq;
  2322. int ldstat = 0;
  2323. u16 cmd_status;
  2324. u8 scsi_status;
  2325. #define ASC_LUN_NOT_READY 0x04
  2326. #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
  2327. #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
  2328. c = cmd_alloc(h);
  2329. if (!c)
  2330. return 0;
  2331. (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
  2332. hpsa_scsi_do_simple_cmd_core(h, c);
  2333. sense = c->err_info->SenseInfo;
  2334. sense_key = sense[2];
  2335. asc = sense[12];
  2336. ascq = sense[13];
  2337. cmd_status = c->err_info->CommandStatus;
  2338. scsi_status = c->err_info->ScsiStatus;
  2339. cmd_free(h, c);
  2340. /* Is the volume 'not ready'? */
  2341. if (cmd_status != CMD_TARGET_STATUS ||
  2342. scsi_status != SAM_STAT_CHECK_CONDITION ||
  2343. sense_key != NOT_READY ||
  2344. asc != ASC_LUN_NOT_READY) {
  2345. return 0;
  2346. }
  2347. /* Determine the reason for not ready state */
  2348. ldstat = hpsa_get_volume_status(h, scsi3addr);
  2349. /* Keep volume offline in certain cases: */
  2350. switch (ldstat) {
  2351. case HPSA_LV_UNDERGOING_ERASE:
  2352. case HPSA_LV_UNDERGOING_RPI:
  2353. case HPSA_LV_PENDING_RPI:
  2354. case HPSA_LV_ENCRYPTED_NO_KEY:
  2355. case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
  2356. case HPSA_LV_UNDERGOING_ENCRYPTION:
  2357. case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
  2358. case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
  2359. return ldstat;
  2360. case HPSA_VPD_LV_STATUS_UNSUPPORTED:
  2361. /* If VPD status page isn't available,
  2362. * use ASC/ASCQ to determine state
  2363. */
  2364. if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
  2365. (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
  2366. return ldstat;
  2367. break;
  2368. default:
  2369. break;
  2370. }
  2371. return 0;
  2372. }
  2373. static int hpsa_update_device_info(struct ctlr_info *h,
  2374. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
  2375. unsigned char *is_OBDR_device)
  2376. {
  2377. #define OBDR_SIG_OFFSET 43
  2378. #define OBDR_TAPE_SIG "$DR-10"
  2379. #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
  2380. #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
  2381. unsigned char *inq_buff;
  2382. unsigned char *obdr_sig;
  2383. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  2384. if (!inq_buff)
  2385. goto bail_out;
  2386. /* Do an inquiry to the device to see what it is. */
  2387. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  2388. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  2389. /* Inquiry failed (msg printed already) */
  2390. dev_err(&h->pdev->dev,
  2391. "hpsa_update_device_info: inquiry failed\n");
  2392. goto bail_out;
  2393. }
  2394. this_device->devtype = (inq_buff[0] & 0x1f);
  2395. memcpy(this_device->scsi3addr, scsi3addr, 8);
  2396. memcpy(this_device->vendor, &inq_buff[8],
  2397. sizeof(this_device->vendor));
  2398. memcpy(this_device->model, &inq_buff[16],
  2399. sizeof(this_device->model));
  2400. memset(this_device->device_id, 0,
  2401. sizeof(this_device->device_id));
  2402. hpsa_get_device_id(h, scsi3addr, this_device->device_id,
  2403. sizeof(this_device->device_id));
  2404. if (this_device->devtype == TYPE_DISK &&
  2405. is_logical_dev_addr_mode(scsi3addr)) {
  2406. int volume_offline;
  2407. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  2408. if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
  2409. hpsa_get_ioaccel_status(h, scsi3addr, this_device);
  2410. volume_offline = hpsa_volume_offline(h, scsi3addr);
  2411. if (volume_offline < 0 || volume_offline > 0xff)
  2412. volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
  2413. this_device->volume_offline = volume_offline & 0xff;
  2414. } else {
  2415. this_device->raid_level = RAID_UNKNOWN;
  2416. this_device->offload_config = 0;
  2417. this_device->offload_enabled = 0;
  2418. this_device->volume_offline = 0;
  2419. }
  2420. if (is_OBDR_device) {
  2421. /* See if this is a One-Button-Disaster-Recovery device
  2422. * by looking for "$DR-10" at offset 43 in inquiry data.
  2423. */
  2424. obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
  2425. *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
  2426. strncmp(obdr_sig, OBDR_TAPE_SIG,
  2427. OBDR_SIG_LEN) == 0);
  2428. }
  2429. kfree(inq_buff);
  2430. return 0;
  2431. bail_out:
  2432. kfree(inq_buff);
  2433. return 1;
  2434. }
  2435. static unsigned char *ext_target_model[] = {
  2436. "MSA2012",
  2437. "MSA2024",
  2438. "MSA2312",
  2439. "MSA2324",
  2440. "P2000 G3 SAS",
  2441. "MSA 2040 SAS",
  2442. NULL,
  2443. };
  2444. static int is_ext_target(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  2445. {
  2446. int i;
  2447. for (i = 0; ext_target_model[i]; i++)
  2448. if (strncmp(device->model, ext_target_model[i],
  2449. strlen(ext_target_model[i])) == 0)
  2450. return 1;
  2451. return 0;
  2452. }
  2453. /* Helper function to assign bus, target, lun mapping of devices.
  2454. * Puts non-external target logical volumes on bus 0, external target logical
  2455. * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
  2456. * Logical drive target and lun are assigned at this time, but
  2457. * physical device lun and target assignment are deferred (assigned
  2458. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  2459. */
  2460. static void figure_bus_target_lun(struct ctlr_info *h,
  2461. u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
  2462. {
  2463. u32 lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  2464. if (!is_logical_dev_addr_mode(lunaddrbytes)) {
  2465. /* physical device, target and lun filled in later */
  2466. if (is_hba_lunid(lunaddrbytes))
  2467. hpsa_set_bus_target_lun(device, 3, 0, lunid & 0x3fff);
  2468. else
  2469. /* defer target, lun assignment for physical devices */
  2470. hpsa_set_bus_target_lun(device, 2, -1, -1);
  2471. return;
  2472. }
  2473. /* It's a logical device */
  2474. if (is_ext_target(h, device)) {
  2475. /* external target way, put logicals on bus 1
  2476. * and match target/lun numbers box
  2477. * reports, other smart array, bus 0, target 0, match lunid
  2478. */
  2479. hpsa_set_bus_target_lun(device,
  2480. 1, (lunid >> 16) & 0x3fff, lunid & 0x00ff);
  2481. return;
  2482. }
  2483. hpsa_set_bus_target_lun(device, 0, 0, lunid & 0x3fff);
  2484. }
  2485. /*
  2486. * If there is no lun 0 on a target, linux won't find any devices.
  2487. * For the external targets (arrays), we have to manually detect the enclosure
  2488. * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
  2489. * it for some reason. *tmpdevice is the target we're adding,
  2490. * this_device is a pointer into the current element of currentsd[]
  2491. * that we're building up in update_scsi_devices(), below.
  2492. * lunzerobits is a bitmap that tracks which targets already have a
  2493. * lun 0 assigned.
  2494. * Returns 1 if an enclosure was added, 0 if not.
  2495. */
  2496. static int add_ext_target_dev(struct ctlr_info *h,
  2497. struct hpsa_scsi_dev_t *tmpdevice,
  2498. struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
  2499. unsigned long lunzerobits[], int *n_ext_target_devs)
  2500. {
  2501. unsigned char scsi3addr[8];
  2502. if (test_bit(tmpdevice->target, lunzerobits))
  2503. return 0; /* There is already a lun 0 on this target. */
  2504. if (!is_logical_dev_addr_mode(lunaddrbytes))
  2505. return 0; /* It's the logical targets that may lack lun 0. */
  2506. if (!is_ext_target(h, tmpdevice))
  2507. return 0; /* Only external target devices have this problem. */
  2508. if (tmpdevice->lun == 0) /* if lun is 0, then we have a lun 0. */
  2509. return 0;
  2510. memset(scsi3addr, 0, 8);
  2511. scsi3addr[3] = tmpdevice->target;
  2512. if (is_hba_lunid(scsi3addr))
  2513. return 0; /* Don't add the RAID controller here. */
  2514. if (is_scsi_rev_5(h))
  2515. return 0; /* p1210m doesn't need to do this. */
  2516. if (*n_ext_target_devs >= MAX_EXT_TARGETS) {
  2517. dev_warn(&h->pdev->dev, "Maximum number of external "
  2518. "target devices exceeded. Check your hardware "
  2519. "configuration.");
  2520. return 0;
  2521. }
  2522. if (hpsa_update_device_info(h, scsi3addr, this_device, NULL))
  2523. return 0;
  2524. (*n_ext_target_devs)++;
  2525. hpsa_set_bus_target_lun(this_device,
  2526. tmpdevice->bus, tmpdevice->target, 0);
  2527. set_bit(tmpdevice->target, lunzerobits);
  2528. return 1;
  2529. }
  2530. /*
  2531. * Get address of physical disk used for an ioaccel2 mode command:
  2532. * 1. Extract ioaccel2 handle from the command.
  2533. * 2. Find a matching ioaccel2 handle from list of physical disks.
  2534. * 3. Return:
  2535. * 1 and set scsi3addr to address of matching physical
  2536. * 0 if no matching physical disk was found.
  2537. */
  2538. static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
  2539. struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
  2540. {
  2541. struct ReportExtendedLUNdata *physicals = NULL;
  2542. int responsesize = 24; /* size of physical extended response */
  2543. int extended = 2; /* flag forces reporting 'other dev info'. */
  2544. int reportsize = sizeof(*physicals) + HPSA_MAX_PHYS_LUN * responsesize;
  2545. u32 nphysicals = 0; /* number of reported physical devs */
  2546. int found = 0; /* found match (1) or not (0) */
  2547. u32 find; /* handle we need to match */
  2548. int i;
  2549. struct scsi_cmnd *scmd; /* scsi command within request being aborted */
  2550. struct hpsa_scsi_dev_t *d; /* device of request being aborted */
  2551. struct io_accel2_cmd *c2a; /* ioaccel2 command to abort */
  2552. u32 it_nexus; /* 4 byte device handle for the ioaccel2 cmd */
  2553. u32 scsi_nexus; /* 4 byte device handle for the ioaccel2 cmd */
  2554. if (ioaccel2_cmd_to_abort->cmd_type != CMD_IOACCEL2)
  2555. return 0; /* no match */
  2556. /* point to the ioaccel2 device handle */
  2557. c2a = &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
  2558. if (c2a == NULL)
  2559. return 0; /* no match */
  2560. scmd = (struct scsi_cmnd *) ioaccel2_cmd_to_abort->scsi_cmd;
  2561. if (scmd == NULL)
  2562. return 0; /* no match */
  2563. d = scmd->device->hostdata;
  2564. if (d == NULL)
  2565. return 0; /* no match */
  2566. it_nexus = cpu_to_le32((u32) d->ioaccel_handle);
  2567. scsi_nexus = cpu_to_le32((u32) c2a->scsi_nexus);
  2568. find = c2a->scsi_nexus;
  2569. if (h->raid_offload_debug > 0)
  2570. dev_info(&h->pdev->dev,
  2571. "%s: scsi_nexus:0x%08x device id: 0x%02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
  2572. __func__, scsi_nexus,
  2573. d->device_id[0], d->device_id[1], d->device_id[2],
  2574. d->device_id[3], d->device_id[4], d->device_id[5],
  2575. d->device_id[6], d->device_id[7], d->device_id[8],
  2576. d->device_id[9], d->device_id[10], d->device_id[11],
  2577. d->device_id[12], d->device_id[13], d->device_id[14],
  2578. d->device_id[15]);
  2579. /* Get the list of physical devices */
  2580. physicals = kzalloc(reportsize, GFP_KERNEL);
  2581. if (physicals == NULL)
  2582. return 0;
  2583. if (hpsa_scsi_do_report_phys_luns(h, (struct ReportLUNdata *) physicals,
  2584. reportsize, extended)) {
  2585. dev_err(&h->pdev->dev,
  2586. "Can't lookup %s device handle: report physical LUNs failed.\n",
  2587. "HP SSD Smart Path");
  2588. kfree(physicals);
  2589. return 0;
  2590. }
  2591. nphysicals = be32_to_cpu(*((__be32 *)physicals->LUNListLength)) /
  2592. responsesize;
  2593. /* find ioaccel2 handle in list of physicals: */
  2594. for (i = 0; i < nphysicals; i++) {
  2595. struct ext_report_lun_entry *entry = &physicals->LUN[i];
  2596. /* handle is in bytes 28-31 of each lun */
  2597. if (entry->ioaccel_handle != find)
  2598. continue; /* didn't match */
  2599. found = 1;
  2600. memcpy(scsi3addr, entry->lunid, 8);
  2601. if (h->raid_offload_debug > 0)
  2602. dev_info(&h->pdev->dev,
  2603. "%s: Searched h=0x%08x, Found h=0x%08x, scsiaddr 0x%8phN\n",
  2604. __func__, find,
  2605. entry->ioaccel_handle, scsi3addr);
  2606. break; /* found it */
  2607. }
  2608. kfree(physicals);
  2609. if (found)
  2610. return 1;
  2611. else
  2612. return 0;
  2613. }
  2614. /*
  2615. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  2616. * logdev. The number of luns in physdev and logdev are returned in
  2617. * *nphysicals and *nlogicals, respectively.
  2618. * Returns 0 on success, -1 otherwise.
  2619. */
  2620. static int hpsa_gather_lun_info(struct ctlr_info *h,
  2621. int reportlunsize,
  2622. struct ReportLUNdata *physdev, u32 *nphysicals, int *physical_mode,
  2623. struct ReportLUNdata *logdev, u32 *nlogicals)
  2624. {
  2625. int physical_entry_size = 8;
  2626. *physical_mode = 0;
  2627. /* For I/O accelerator mode we need to read physical device handles */
  2628. if (h->transMethod & CFGTBL_Trans_io_accel1 ||
  2629. h->transMethod & CFGTBL_Trans_io_accel2) {
  2630. *physical_mode = HPSA_REPORT_PHYS_EXTENDED;
  2631. physical_entry_size = 24;
  2632. }
  2633. if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize,
  2634. *physical_mode)) {
  2635. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  2636. return -1;
  2637. }
  2638. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) /
  2639. physical_entry_size;
  2640. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  2641. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
  2642. " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  2643. *nphysicals - HPSA_MAX_PHYS_LUN);
  2644. *nphysicals = HPSA_MAX_PHYS_LUN;
  2645. }
  2646. if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
  2647. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  2648. return -1;
  2649. }
  2650. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  2651. /* Reject Logicals in excess of our max capability. */
  2652. if (*nlogicals > HPSA_MAX_LUN) {
  2653. dev_warn(&h->pdev->dev,
  2654. "maximum logical LUNs (%d) exceeded. "
  2655. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  2656. *nlogicals - HPSA_MAX_LUN);
  2657. *nlogicals = HPSA_MAX_LUN;
  2658. }
  2659. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  2660. dev_warn(&h->pdev->dev,
  2661. "maximum logical + physical LUNs (%d) exceeded. "
  2662. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  2663. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  2664. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  2665. }
  2666. return 0;
  2667. }
  2668. u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
  2669. int nphysicals, int nlogicals,
  2670. struct ReportExtendedLUNdata *physdev_list,
  2671. struct ReportLUNdata *logdev_list)
  2672. {
  2673. /* Helper function, figure out where the LUN ID info is coming from
  2674. * given index i, lists of physical and logical devices, where in
  2675. * the list the raid controller is supposed to appear (first or last)
  2676. */
  2677. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  2678. int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
  2679. if (i == raid_ctlr_position)
  2680. return RAID_CTLR_LUNID;
  2681. if (i < logicals_start)
  2682. return &physdev_list->LUN[i -
  2683. (raid_ctlr_position == 0)].lunid[0];
  2684. if (i < last_device)
  2685. return &logdev_list->LUN[i - nphysicals -
  2686. (raid_ctlr_position == 0)][0];
  2687. BUG();
  2688. return NULL;
  2689. }
  2690. static int hpsa_hba_mode_enabled(struct ctlr_info *h)
  2691. {
  2692. int rc;
  2693. int hba_mode_enabled;
  2694. struct bmic_controller_parameters *ctlr_params;
  2695. ctlr_params = kzalloc(sizeof(struct bmic_controller_parameters),
  2696. GFP_KERNEL);
  2697. if (!ctlr_params)
  2698. return -ENOMEM;
  2699. rc = hpsa_bmic_ctrl_mode_sense(h, RAID_CTLR_LUNID, 0, ctlr_params,
  2700. sizeof(struct bmic_controller_parameters));
  2701. if (rc) {
  2702. kfree(ctlr_params);
  2703. return rc;
  2704. }
  2705. hba_mode_enabled =
  2706. ((ctlr_params->nvram_flags & HBA_MODE_ENABLED_FLAG) != 0);
  2707. kfree(ctlr_params);
  2708. return hba_mode_enabled;
  2709. }
  2710. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
  2711. {
  2712. /* the idea here is we could get notified
  2713. * that some devices have changed, so we do a report
  2714. * physical luns and report logical luns cmd, and adjust
  2715. * our list of devices accordingly.
  2716. *
  2717. * The scsi3addr's of devices won't change so long as the
  2718. * adapter is not reset. That means we can rescan and
  2719. * tell which devices we already know about, vs. new
  2720. * devices, vs. disappearing devices.
  2721. */
  2722. struct ReportExtendedLUNdata *physdev_list = NULL;
  2723. struct ReportLUNdata *logdev_list = NULL;
  2724. u32 nphysicals = 0;
  2725. u32 nlogicals = 0;
  2726. int physical_mode = 0;
  2727. u32 ndev_allocated = 0;
  2728. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  2729. int ncurrent = 0;
  2730. int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 24;
  2731. int i, n_ext_target_devs, ndevs_to_allocate;
  2732. int raid_ctlr_position;
  2733. int rescan_hba_mode;
  2734. DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
  2735. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
  2736. physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  2737. logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  2738. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  2739. if (!currentsd || !physdev_list || !logdev_list || !tmpdevice) {
  2740. dev_err(&h->pdev->dev, "out of memory\n");
  2741. goto out;
  2742. }
  2743. memset(lunzerobits, 0, sizeof(lunzerobits));
  2744. rescan_hba_mode = hpsa_hba_mode_enabled(h);
  2745. if (rescan_hba_mode < 0)
  2746. goto out;
  2747. if (!h->hba_mode_enabled && rescan_hba_mode)
  2748. dev_warn(&h->pdev->dev, "HBA mode enabled\n");
  2749. else if (h->hba_mode_enabled && !rescan_hba_mode)
  2750. dev_warn(&h->pdev->dev, "HBA mode disabled\n");
  2751. h->hba_mode_enabled = rescan_hba_mode;
  2752. if (hpsa_gather_lun_info(h, reportlunsize,
  2753. (struct ReportLUNdata *) physdev_list, &nphysicals,
  2754. &physical_mode, logdev_list, &nlogicals))
  2755. goto out;
  2756. /* We might see up to the maximum number of logical and physical disks
  2757. * plus external target devices, and a device for the local RAID
  2758. * controller.
  2759. */
  2760. ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
  2761. /* Allocate the per device structures */
  2762. for (i = 0; i < ndevs_to_allocate; i++) {
  2763. if (i >= HPSA_MAX_DEVICES) {
  2764. dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
  2765. " %d devices ignored.\n", HPSA_MAX_DEVICES,
  2766. ndevs_to_allocate - HPSA_MAX_DEVICES);
  2767. break;
  2768. }
  2769. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  2770. if (!currentsd[i]) {
  2771. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  2772. __FILE__, __LINE__);
  2773. goto out;
  2774. }
  2775. ndev_allocated++;
  2776. }
  2777. if (is_scsi_rev_5(h))
  2778. raid_ctlr_position = 0;
  2779. else
  2780. raid_ctlr_position = nphysicals + nlogicals;
  2781. /* adjust our table of devices */
  2782. n_ext_target_devs = 0;
  2783. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  2784. u8 *lunaddrbytes, is_OBDR = 0;
  2785. /* Figure out where the LUN ID info is coming from */
  2786. lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
  2787. i, nphysicals, nlogicals, physdev_list, logdev_list);
  2788. /* skip masked physical devices. */
  2789. if (lunaddrbytes[3] & 0xC0 &&
  2790. i < nphysicals + (raid_ctlr_position == 0))
  2791. continue;
  2792. /* Get device type, vendor, model, device id */
  2793. if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
  2794. &is_OBDR))
  2795. continue; /* skip it if we can't talk to it. */
  2796. figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
  2797. this_device = currentsd[ncurrent];
  2798. /*
  2799. * For external target devices, we have to insert a LUN 0 which
  2800. * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
  2801. * is nonetheless an enclosure device there. We have to
  2802. * present that otherwise linux won't find anything if
  2803. * there is no lun 0.
  2804. */
  2805. if (add_ext_target_dev(h, tmpdevice, this_device,
  2806. lunaddrbytes, lunzerobits,
  2807. &n_ext_target_devs)) {
  2808. ncurrent++;
  2809. this_device = currentsd[ncurrent];
  2810. }
  2811. *this_device = *tmpdevice;
  2812. switch (this_device->devtype) {
  2813. case TYPE_ROM:
  2814. /* We don't *really* support actual CD-ROM devices,
  2815. * just "One Button Disaster Recovery" tape drive
  2816. * which temporarily pretends to be a CD-ROM drive.
  2817. * So we check that the device is really an OBDR tape
  2818. * device by checking for "$DR-10" in bytes 43-48 of
  2819. * the inquiry data.
  2820. */
  2821. if (is_OBDR)
  2822. ncurrent++;
  2823. break;
  2824. case TYPE_DISK:
  2825. if (h->hba_mode_enabled) {
  2826. /* never use raid mapper in HBA mode */
  2827. this_device->offload_enabled = 0;
  2828. ncurrent++;
  2829. break;
  2830. } else if (h->acciopath_status) {
  2831. if (i >= nphysicals) {
  2832. ncurrent++;
  2833. break;
  2834. }
  2835. } else {
  2836. if (i < nphysicals)
  2837. break;
  2838. ncurrent++;
  2839. break;
  2840. }
  2841. if (physical_mode == HPSA_REPORT_PHYS_EXTENDED) {
  2842. memcpy(&this_device->ioaccel_handle,
  2843. &lunaddrbytes[20],
  2844. sizeof(this_device->ioaccel_handle));
  2845. ncurrent++;
  2846. }
  2847. break;
  2848. case TYPE_TAPE:
  2849. case TYPE_MEDIUM_CHANGER:
  2850. ncurrent++;
  2851. break;
  2852. case TYPE_RAID:
  2853. /* Only present the Smartarray HBA as a RAID controller.
  2854. * If it's a RAID controller other than the HBA itself
  2855. * (an external RAID controller, MSA500 or similar)
  2856. * don't present it.
  2857. */
  2858. if (!is_hba_lunid(lunaddrbytes))
  2859. break;
  2860. ncurrent++;
  2861. break;
  2862. default:
  2863. break;
  2864. }
  2865. if (ncurrent >= HPSA_MAX_DEVICES)
  2866. break;
  2867. }
  2868. adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
  2869. out:
  2870. kfree(tmpdevice);
  2871. for (i = 0; i < ndev_allocated; i++)
  2872. kfree(currentsd[i]);
  2873. kfree(currentsd);
  2874. kfree(physdev_list);
  2875. kfree(logdev_list);
  2876. }
  2877. /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  2878. * dma mapping and fills in the scatter gather entries of the
  2879. * hpsa command, cp.
  2880. */
  2881. static int hpsa_scatter_gather(struct ctlr_info *h,
  2882. struct CommandList *cp,
  2883. struct scsi_cmnd *cmd)
  2884. {
  2885. unsigned int len;
  2886. struct scatterlist *sg;
  2887. u64 addr64;
  2888. int use_sg, i, sg_index, chained;
  2889. struct SGDescriptor *curr_sg;
  2890. BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
  2891. use_sg = scsi_dma_map(cmd);
  2892. if (use_sg < 0)
  2893. return use_sg;
  2894. if (!use_sg)
  2895. goto sglist_finished;
  2896. curr_sg = cp->SG;
  2897. chained = 0;
  2898. sg_index = 0;
  2899. scsi_for_each_sg(cmd, sg, use_sg, i) {
  2900. if (i == h->max_cmd_sg_entries - 1 &&
  2901. use_sg > h->max_cmd_sg_entries) {
  2902. chained = 1;
  2903. curr_sg = h->cmd_sg_list[cp->cmdindex];
  2904. sg_index = 0;
  2905. }
  2906. addr64 = (u64) sg_dma_address(sg);
  2907. len = sg_dma_len(sg);
  2908. curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
  2909. curr_sg->Addr.upper = (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
  2910. curr_sg->Len = len;
  2911. curr_sg->Ext = (i < scsi_sg_count(cmd) - 1) ? 0 : HPSA_SG_LAST;
  2912. curr_sg++;
  2913. }
  2914. if (use_sg + chained > h->maxSG)
  2915. h->maxSG = use_sg + chained;
  2916. if (chained) {
  2917. cp->Header.SGList = h->max_cmd_sg_entries;
  2918. cp->Header.SGTotal = (u16) (use_sg + 1);
  2919. if (hpsa_map_sg_chain_block(h, cp)) {
  2920. scsi_dma_unmap(cmd);
  2921. return -1;
  2922. }
  2923. return 0;
  2924. }
  2925. sglist_finished:
  2926. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  2927. cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
  2928. return 0;
  2929. }
  2930. #define IO_ACCEL_INELIGIBLE (1)
  2931. static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
  2932. {
  2933. int is_write = 0;
  2934. u32 block;
  2935. u32 block_cnt;
  2936. /* Perform some CDB fixups if needed using 10 byte reads/writes only */
  2937. switch (cdb[0]) {
  2938. case WRITE_6:
  2939. case WRITE_12:
  2940. is_write = 1;
  2941. case READ_6:
  2942. case READ_12:
  2943. if (*cdb_len == 6) {
  2944. block = (((u32) cdb[2]) << 8) | cdb[3];
  2945. block_cnt = cdb[4];
  2946. } else {
  2947. BUG_ON(*cdb_len != 12);
  2948. block = (((u32) cdb[2]) << 24) |
  2949. (((u32) cdb[3]) << 16) |
  2950. (((u32) cdb[4]) << 8) |
  2951. cdb[5];
  2952. block_cnt =
  2953. (((u32) cdb[6]) << 24) |
  2954. (((u32) cdb[7]) << 16) |
  2955. (((u32) cdb[8]) << 8) |
  2956. cdb[9];
  2957. }
  2958. if (block_cnt > 0xffff)
  2959. return IO_ACCEL_INELIGIBLE;
  2960. cdb[0] = is_write ? WRITE_10 : READ_10;
  2961. cdb[1] = 0;
  2962. cdb[2] = (u8) (block >> 24);
  2963. cdb[3] = (u8) (block >> 16);
  2964. cdb[4] = (u8) (block >> 8);
  2965. cdb[5] = (u8) (block);
  2966. cdb[6] = 0;
  2967. cdb[7] = (u8) (block_cnt >> 8);
  2968. cdb[8] = (u8) (block_cnt);
  2969. cdb[9] = 0;
  2970. *cdb_len = 10;
  2971. break;
  2972. }
  2973. return 0;
  2974. }
  2975. static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
  2976. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  2977. u8 *scsi3addr)
  2978. {
  2979. struct scsi_cmnd *cmd = c->scsi_cmd;
  2980. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
  2981. unsigned int len;
  2982. unsigned int total_len = 0;
  2983. struct scatterlist *sg;
  2984. u64 addr64;
  2985. int use_sg, i;
  2986. struct SGDescriptor *curr_sg;
  2987. u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
  2988. /* TODO: implement chaining support */
  2989. if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
  2990. return IO_ACCEL_INELIGIBLE;
  2991. BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
  2992. if (fixup_ioaccel_cdb(cdb, &cdb_len))
  2993. return IO_ACCEL_INELIGIBLE;
  2994. c->cmd_type = CMD_IOACCEL1;
  2995. /* Adjust the DMA address to point to the accelerated command buffer */
  2996. c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
  2997. (c->cmdindex * sizeof(*cp));
  2998. BUG_ON(c->busaddr & 0x0000007F);
  2999. use_sg = scsi_dma_map(cmd);
  3000. if (use_sg < 0)
  3001. return use_sg;
  3002. if (use_sg) {
  3003. curr_sg = cp->SG;
  3004. scsi_for_each_sg(cmd, sg, use_sg, i) {
  3005. addr64 = (u64) sg_dma_address(sg);
  3006. len = sg_dma_len(sg);
  3007. total_len += len;
  3008. curr_sg->Addr.lower = (u32) (addr64 & 0x0FFFFFFFFULL);
  3009. curr_sg->Addr.upper =
  3010. (u32) ((addr64 >> 32) & 0x0FFFFFFFFULL);
  3011. curr_sg->Len = len;
  3012. if (i == (scsi_sg_count(cmd) - 1))
  3013. curr_sg->Ext = HPSA_SG_LAST;
  3014. else
  3015. curr_sg->Ext = 0; /* we are not chaining */
  3016. curr_sg++;
  3017. }
  3018. switch (cmd->sc_data_direction) {
  3019. case DMA_TO_DEVICE:
  3020. control |= IOACCEL1_CONTROL_DATA_OUT;
  3021. break;
  3022. case DMA_FROM_DEVICE:
  3023. control |= IOACCEL1_CONTROL_DATA_IN;
  3024. break;
  3025. case DMA_NONE:
  3026. control |= IOACCEL1_CONTROL_NODATAXFER;
  3027. break;
  3028. default:
  3029. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  3030. cmd->sc_data_direction);
  3031. BUG();
  3032. break;
  3033. }
  3034. } else {
  3035. control |= IOACCEL1_CONTROL_NODATAXFER;
  3036. }
  3037. c->Header.SGList = use_sg;
  3038. /* Fill out the command structure to submit */
  3039. cp->dev_handle = ioaccel_handle & 0xFFFF;
  3040. cp->transfer_len = total_len;
  3041. cp->io_flags = IOACCEL1_IOFLAGS_IO_REQ |
  3042. (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK);
  3043. cp->control = control;
  3044. memcpy(cp->CDB, cdb, cdb_len);
  3045. memcpy(cp->CISS_LUN, scsi3addr, 8);
  3046. /* Tag was already set at init time. */
  3047. enqueue_cmd_and_start_io(h, c);
  3048. return 0;
  3049. }
  3050. /*
  3051. * Queue a command directly to a device behind the controller using the
  3052. * I/O accelerator path.
  3053. */
  3054. static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
  3055. struct CommandList *c)
  3056. {
  3057. struct scsi_cmnd *cmd = c->scsi_cmd;
  3058. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  3059. return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
  3060. cmd->cmnd, cmd->cmd_len, dev->scsi3addr);
  3061. }
  3062. /*
  3063. * Set encryption parameters for the ioaccel2 request
  3064. */
  3065. static void set_encrypt_ioaccel2(struct ctlr_info *h,
  3066. struct CommandList *c, struct io_accel2_cmd *cp)
  3067. {
  3068. struct scsi_cmnd *cmd = c->scsi_cmd;
  3069. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  3070. struct raid_map_data *map = &dev->raid_map;
  3071. u64 first_block;
  3072. BUG_ON(!(dev->offload_config && dev->offload_enabled));
  3073. /* Are we doing encryption on this device */
  3074. if (!(map->flags & RAID_MAP_FLAG_ENCRYPT_ON))
  3075. return;
  3076. /* Set the data encryption key index. */
  3077. cp->dekindex = map->dekindex;
  3078. /* Set the encryption enable flag, encoded into direction field. */
  3079. cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
  3080. /* Set encryption tweak values based on logical block address
  3081. * If block size is 512, tweak value is LBA.
  3082. * For other block sizes, tweak is (LBA * block size)/ 512)
  3083. */
  3084. switch (cmd->cmnd[0]) {
  3085. /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
  3086. case WRITE_6:
  3087. case READ_6:
  3088. if (map->volume_blk_size == 512) {
  3089. cp->tweak_lower =
  3090. (((u32) cmd->cmnd[2]) << 8) |
  3091. cmd->cmnd[3];
  3092. cp->tweak_upper = 0;
  3093. } else {
  3094. first_block =
  3095. (((u64) cmd->cmnd[2]) << 8) |
  3096. cmd->cmnd[3];
  3097. first_block = (first_block * map->volume_blk_size)/512;
  3098. cp->tweak_lower = (u32)first_block;
  3099. cp->tweak_upper = (u32)(first_block >> 32);
  3100. }
  3101. break;
  3102. case WRITE_10:
  3103. case READ_10:
  3104. if (map->volume_blk_size == 512) {
  3105. cp->tweak_lower =
  3106. (((u32) cmd->cmnd[2]) << 24) |
  3107. (((u32) cmd->cmnd[3]) << 16) |
  3108. (((u32) cmd->cmnd[4]) << 8) |
  3109. cmd->cmnd[5];
  3110. cp->tweak_upper = 0;
  3111. } else {
  3112. first_block =
  3113. (((u64) cmd->cmnd[2]) << 24) |
  3114. (((u64) cmd->cmnd[3]) << 16) |
  3115. (((u64) cmd->cmnd[4]) << 8) |
  3116. cmd->cmnd[5];
  3117. first_block = (first_block * map->volume_blk_size)/512;
  3118. cp->tweak_lower = (u32)first_block;
  3119. cp->tweak_upper = (u32)(first_block >> 32);
  3120. }
  3121. break;
  3122. /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
  3123. case WRITE_12:
  3124. case READ_12:
  3125. if (map->volume_blk_size == 512) {
  3126. cp->tweak_lower =
  3127. (((u32) cmd->cmnd[2]) << 24) |
  3128. (((u32) cmd->cmnd[3]) << 16) |
  3129. (((u32) cmd->cmnd[4]) << 8) |
  3130. cmd->cmnd[5];
  3131. cp->tweak_upper = 0;
  3132. } else {
  3133. first_block =
  3134. (((u64) cmd->cmnd[2]) << 24) |
  3135. (((u64) cmd->cmnd[3]) << 16) |
  3136. (((u64) cmd->cmnd[4]) << 8) |
  3137. cmd->cmnd[5];
  3138. first_block = (first_block * map->volume_blk_size)/512;
  3139. cp->tweak_lower = (u32)first_block;
  3140. cp->tweak_upper = (u32)(first_block >> 32);
  3141. }
  3142. break;
  3143. case WRITE_16:
  3144. case READ_16:
  3145. if (map->volume_blk_size == 512) {
  3146. cp->tweak_lower =
  3147. (((u32) cmd->cmnd[6]) << 24) |
  3148. (((u32) cmd->cmnd[7]) << 16) |
  3149. (((u32) cmd->cmnd[8]) << 8) |
  3150. cmd->cmnd[9];
  3151. cp->tweak_upper =
  3152. (((u32) cmd->cmnd[2]) << 24) |
  3153. (((u32) cmd->cmnd[3]) << 16) |
  3154. (((u32) cmd->cmnd[4]) << 8) |
  3155. cmd->cmnd[5];
  3156. } else {
  3157. first_block =
  3158. (((u64) cmd->cmnd[2]) << 56) |
  3159. (((u64) cmd->cmnd[3]) << 48) |
  3160. (((u64) cmd->cmnd[4]) << 40) |
  3161. (((u64) cmd->cmnd[5]) << 32) |
  3162. (((u64) cmd->cmnd[6]) << 24) |
  3163. (((u64) cmd->cmnd[7]) << 16) |
  3164. (((u64) cmd->cmnd[8]) << 8) |
  3165. cmd->cmnd[9];
  3166. first_block = (first_block * map->volume_blk_size)/512;
  3167. cp->tweak_lower = (u32)first_block;
  3168. cp->tweak_upper = (u32)(first_block >> 32);
  3169. }
  3170. break;
  3171. default:
  3172. dev_err(&h->pdev->dev,
  3173. "ERROR: %s: IOACCEL request CDB size not supported for encryption\n",
  3174. __func__);
  3175. BUG();
  3176. break;
  3177. }
  3178. }
  3179. static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
  3180. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  3181. u8 *scsi3addr)
  3182. {
  3183. struct scsi_cmnd *cmd = c->scsi_cmd;
  3184. struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
  3185. struct ioaccel2_sg_element *curr_sg;
  3186. int use_sg, i;
  3187. struct scatterlist *sg;
  3188. u64 addr64;
  3189. u32 len;
  3190. u32 total_len = 0;
  3191. if (scsi_sg_count(cmd) > h->ioaccel_maxsg)
  3192. return IO_ACCEL_INELIGIBLE;
  3193. if (fixup_ioaccel_cdb(cdb, &cdb_len))
  3194. return IO_ACCEL_INELIGIBLE;
  3195. c->cmd_type = CMD_IOACCEL2;
  3196. /* Adjust the DMA address to point to the accelerated command buffer */
  3197. c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
  3198. (c->cmdindex * sizeof(*cp));
  3199. BUG_ON(c->busaddr & 0x0000007F);
  3200. memset(cp, 0, sizeof(*cp));
  3201. cp->IU_type = IOACCEL2_IU_TYPE;
  3202. use_sg = scsi_dma_map(cmd);
  3203. if (use_sg < 0)
  3204. return use_sg;
  3205. if (use_sg) {
  3206. BUG_ON(use_sg > IOACCEL2_MAXSGENTRIES);
  3207. curr_sg = cp->sg;
  3208. scsi_for_each_sg(cmd, sg, use_sg, i) {
  3209. addr64 = (u64) sg_dma_address(sg);
  3210. len = sg_dma_len(sg);
  3211. total_len += len;
  3212. curr_sg->address = cpu_to_le64(addr64);
  3213. curr_sg->length = cpu_to_le32(len);
  3214. curr_sg->reserved[0] = 0;
  3215. curr_sg->reserved[1] = 0;
  3216. curr_sg->reserved[2] = 0;
  3217. curr_sg->chain_indicator = 0;
  3218. curr_sg++;
  3219. }
  3220. switch (cmd->sc_data_direction) {
  3221. case DMA_TO_DEVICE:
  3222. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  3223. cp->direction |= IOACCEL2_DIR_DATA_OUT;
  3224. break;
  3225. case DMA_FROM_DEVICE:
  3226. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  3227. cp->direction |= IOACCEL2_DIR_DATA_IN;
  3228. break;
  3229. case DMA_NONE:
  3230. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  3231. cp->direction |= IOACCEL2_DIR_NO_DATA;
  3232. break;
  3233. default:
  3234. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  3235. cmd->sc_data_direction);
  3236. BUG();
  3237. break;
  3238. }
  3239. } else {
  3240. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  3241. cp->direction |= IOACCEL2_DIR_NO_DATA;
  3242. }
  3243. /* Set encryption parameters, if necessary */
  3244. set_encrypt_ioaccel2(h, c, cp);
  3245. cp->scsi_nexus = ioaccel_handle;
  3246. cp->Tag = (c->cmdindex << DIRECT_LOOKUP_SHIFT) |
  3247. DIRECT_LOOKUP_BIT;
  3248. memcpy(cp->cdb, cdb, sizeof(cp->cdb));
  3249. /* fill in sg elements */
  3250. cp->sg_count = (u8) use_sg;
  3251. cp->data_len = cpu_to_le32(total_len);
  3252. cp->err_ptr = cpu_to_le64(c->busaddr +
  3253. offsetof(struct io_accel2_cmd, error_data));
  3254. cp->err_len = cpu_to_le32((u32) sizeof(cp->error_data));
  3255. enqueue_cmd_and_start_io(h, c);
  3256. return 0;
  3257. }
  3258. /*
  3259. * Queue a command to the correct I/O accelerator path.
  3260. */
  3261. static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
  3262. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  3263. u8 *scsi3addr)
  3264. {
  3265. if (h->transMethod & CFGTBL_Trans_io_accel1)
  3266. return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
  3267. cdb, cdb_len, scsi3addr);
  3268. else
  3269. return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
  3270. cdb, cdb_len, scsi3addr);
  3271. }
  3272. static void raid_map_helper(struct raid_map_data *map,
  3273. int offload_to_mirror, u32 *map_index, u32 *current_group)
  3274. {
  3275. if (offload_to_mirror == 0) {
  3276. /* use physical disk in the first mirrored group. */
  3277. *map_index %= map->data_disks_per_row;
  3278. return;
  3279. }
  3280. do {
  3281. /* determine mirror group that *map_index indicates */
  3282. *current_group = *map_index / map->data_disks_per_row;
  3283. if (offload_to_mirror == *current_group)
  3284. continue;
  3285. if (*current_group < (map->layout_map_count - 1)) {
  3286. /* select map index from next group */
  3287. *map_index += map->data_disks_per_row;
  3288. (*current_group)++;
  3289. } else {
  3290. /* select map index from first group */
  3291. *map_index %= map->data_disks_per_row;
  3292. *current_group = 0;
  3293. }
  3294. } while (offload_to_mirror != *current_group);
  3295. }
  3296. /*
  3297. * Attempt to perform offload RAID mapping for a logical volume I/O.
  3298. */
  3299. static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
  3300. struct CommandList *c)
  3301. {
  3302. struct scsi_cmnd *cmd = c->scsi_cmd;
  3303. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  3304. struct raid_map_data *map = &dev->raid_map;
  3305. struct raid_map_disk_data *dd = &map->data[0];
  3306. int is_write = 0;
  3307. u32 map_index;
  3308. u64 first_block, last_block;
  3309. u32 block_cnt;
  3310. u32 blocks_per_row;
  3311. u64 first_row, last_row;
  3312. u32 first_row_offset, last_row_offset;
  3313. u32 first_column, last_column;
  3314. u64 r0_first_row, r0_last_row;
  3315. u32 r5or6_blocks_per_row;
  3316. u64 r5or6_first_row, r5or6_last_row;
  3317. u32 r5or6_first_row_offset, r5or6_last_row_offset;
  3318. u32 r5or6_first_column, r5or6_last_column;
  3319. u32 total_disks_per_row;
  3320. u32 stripesize;
  3321. u32 first_group, last_group, current_group;
  3322. u32 map_row;
  3323. u32 disk_handle;
  3324. u64 disk_block;
  3325. u32 disk_block_cnt;
  3326. u8 cdb[16];
  3327. u8 cdb_len;
  3328. #if BITS_PER_LONG == 32
  3329. u64 tmpdiv;
  3330. #endif
  3331. int offload_to_mirror;
  3332. BUG_ON(!(dev->offload_config && dev->offload_enabled));
  3333. /* check for valid opcode, get LBA and block count */
  3334. switch (cmd->cmnd[0]) {
  3335. case WRITE_6:
  3336. is_write = 1;
  3337. case READ_6:
  3338. first_block =
  3339. (((u64) cmd->cmnd[2]) << 8) |
  3340. cmd->cmnd[3];
  3341. block_cnt = cmd->cmnd[4];
  3342. if (block_cnt == 0)
  3343. block_cnt = 256;
  3344. break;
  3345. case WRITE_10:
  3346. is_write = 1;
  3347. case READ_10:
  3348. first_block =
  3349. (((u64) cmd->cmnd[2]) << 24) |
  3350. (((u64) cmd->cmnd[3]) << 16) |
  3351. (((u64) cmd->cmnd[4]) << 8) |
  3352. cmd->cmnd[5];
  3353. block_cnt =
  3354. (((u32) cmd->cmnd[7]) << 8) |
  3355. cmd->cmnd[8];
  3356. break;
  3357. case WRITE_12:
  3358. is_write = 1;
  3359. case READ_12:
  3360. first_block =
  3361. (((u64) cmd->cmnd[2]) << 24) |
  3362. (((u64) cmd->cmnd[3]) << 16) |
  3363. (((u64) cmd->cmnd[4]) << 8) |
  3364. cmd->cmnd[5];
  3365. block_cnt =
  3366. (((u32) cmd->cmnd[6]) << 24) |
  3367. (((u32) cmd->cmnd[7]) << 16) |
  3368. (((u32) cmd->cmnd[8]) << 8) |
  3369. cmd->cmnd[9];
  3370. break;
  3371. case WRITE_16:
  3372. is_write = 1;
  3373. case READ_16:
  3374. first_block =
  3375. (((u64) cmd->cmnd[2]) << 56) |
  3376. (((u64) cmd->cmnd[3]) << 48) |
  3377. (((u64) cmd->cmnd[4]) << 40) |
  3378. (((u64) cmd->cmnd[5]) << 32) |
  3379. (((u64) cmd->cmnd[6]) << 24) |
  3380. (((u64) cmd->cmnd[7]) << 16) |
  3381. (((u64) cmd->cmnd[8]) << 8) |
  3382. cmd->cmnd[9];
  3383. block_cnt =
  3384. (((u32) cmd->cmnd[10]) << 24) |
  3385. (((u32) cmd->cmnd[11]) << 16) |
  3386. (((u32) cmd->cmnd[12]) << 8) |
  3387. cmd->cmnd[13];
  3388. break;
  3389. default:
  3390. return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
  3391. }
  3392. last_block = first_block + block_cnt - 1;
  3393. /* check for write to non-RAID-0 */
  3394. if (is_write && dev->raid_level != 0)
  3395. return IO_ACCEL_INELIGIBLE;
  3396. /* check for invalid block or wraparound */
  3397. if (last_block >= map->volume_blk_cnt || last_block < first_block)
  3398. return IO_ACCEL_INELIGIBLE;
  3399. /* calculate stripe information for the request */
  3400. blocks_per_row = map->data_disks_per_row * map->strip_size;
  3401. #if BITS_PER_LONG == 32
  3402. tmpdiv = first_block;
  3403. (void) do_div(tmpdiv, blocks_per_row);
  3404. first_row = tmpdiv;
  3405. tmpdiv = last_block;
  3406. (void) do_div(tmpdiv, blocks_per_row);
  3407. last_row = tmpdiv;
  3408. first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
  3409. last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
  3410. tmpdiv = first_row_offset;
  3411. (void) do_div(tmpdiv, map->strip_size);
  3412. first_column = tmpdiv;
  3413. tmpdiv = last_row_offset;
  3414. (void) do_div(tmpdiv, map->strip_size);
  3415. last_column = tmpdiv;
  3416. #else
  3417. first_row = first_block / blocks_per_row;
  3418. last_row = last_block / blocks_per_row;
  3419. first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
  3420. last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
  3421. first_column = first_row_offset / map->strip_size;
  3422. last_column = last_row_offset / map->strip_size;
  3423. #endif
  3424. /* if this isn't a single row/column then give to the controller */
  3425. if ((first_row != last_row) || (first_column != last_column))
  3426. return IO_ACCEL_INELIGIBLE;
  3427. /* proceeding with driver mapping */
  3428. total_disks_per_row = map->data_disks_per_row +
  3429. map->metadata_disks_per_row;
  3430. map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
  3431. map->row_cnt;
  3432. map_index = (map_row * total_disks_per_row) + first_column;
  3433. switch (dev->raid_level) {
  3434. case HPSA_RAID_0:
  3435. break; /* nothing special to do */
  3436. case HPSA_RAID_1:
  3437. /* Handles load balance across RAID 1 members.
  3438. * (2-drive R1 and R10 with even # of drives.)
  3439. * Appropriate for SSDs, not optimal for HDDs
  3440. */
  3441. BUG_ON(map->layout_map_count != 2);
  3442. if (dev->offload_to_mirror)
  3443. map_index += map->data_disks_per_row;
  3444. dev->offload_to_mirror = !dev->offload_to_mirror;
  3445. break;
  3446. case HPSA_RAID_ADM:
  3447. /* Handles N-way mirrors (R1-ADM)
  3448. * and R10 with # of drives divisible by 3.)
  3449. */
  3450. BUG_ON(map->layout_map_count != 3);
  3451. offload_to_mirror = dev->offload_to_mirror;
  3452. raid_map_helper(map, offload_to_mirror,
  3453. &map_index, &current_group);
  3454. /* set mirror group to use next time */
  3455. offload_to_mirror =
  3456. (offload_to_mirror >= map->layout_map_count - 1)
  3457. ? 0 : offload_to_mirror + 1;
  3458. /* FIXME: remove after debug/dev */
  3459. BUG_ON(offload_to_mirror >= map->layout_map_count);
  3460. dev_warn(&h->pdev->dev,
  3461. "DEBUG: Using physical disk map index %d from mirror group %d\n",
  3462. map_index, offload_to_mirror);
  3463. dev->offload_to_mirror = offload_to_mirror;
  3464. /* Avoid direct use of dev->offload_to_mirror within this
  3465. * function since multiple threads might simultaneously
  3466. * increment it beyond the range of dev->layout_map_count -1.
  3467. */
  3468. break;
  3469. case HPSA_RAID_5:
  3470. case HPSA_RAID_6:
  3471. if (map->layout_map_count <= 1)
  3472. break;
  3473. /* Verify first and last block are in same RAID group */
  3474. r5or6_blocks_per_row =
  3475. map->strip_size * map->data_disks_per_row;
  3476. BUG_ON(r5or6_blocks_per_row == 0);
  3477. stripesize = r5or6_blocks_per_row * map->layout_map_count;
  3478. #if BITS_PER_LONG == 32
  3479. tmpdiv = first_block;
  3480. first_group = do_div(tmpdiv, stripesize);
  3481. tmpdiv = first_group;
  3482. (void) do_div(tmpdiv, r5or6_blocks_per_row);
  3483. first_group = tmpdiv;
  3484. tmpdiv = last_block;
  3485. last_group = do_div(tmpdiv, stripesize);
  3486. tmpdiv = last_group;
  3487. (void) do_div(tmpdiv, r5or6_blocks_per_row);
  3488. last_group = tmpdiv;
  3489. #else
  3490. first_group = (first_block % stripesize) / r5or6_blocks_per_row;
  3491. last_group = (last_block % stripesize) / r5or6_blocks_per_row;
  3492. #endif
  3493. if (first_group != last_group)
  3494. return IO_ACCEL_INELIGIBLE;
  3495. /* Verify request is in a single row of RAID 5/6 */
  3496. #if BITS_PER_LONG == 32
  3497. tmpdiv = first_block;
  3498. (void) do_div(tmpdiv, stripesize);
  3499. first_row = r5or6_first_row = r0_first_row = tmpdiv;
  3500. tmpdiv = last_block;
  3501. (void) do_div(tmpdiv, stripesize);
  3502. r5or6_last_row = r0_last_row = tmpdiv;
  3503. #else
  3504. first_row = r5or6_first_row = r0_first_row =
  3505. first_block / stripesize;
  3506. r5or6_last_row = r0_last_row = last_block / stripesize;
  3507. #endif
  3508. if (r5or6_first_row != r5or6_last_row)
  3509. return IO_ACCEL_INELIGIBLE;
  3510. /* Verify request is in a single column */
  3511. #if BITS_PER_LONG == 32
  3512. tmpdiv = first_block;
  3513. first_row_offset = do_div(tmpdiv, stripesize);
  3514. tmpdiv = first_row_offset;
  3515. first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
  3516. r5or6_first_row_offset = first_row_offset;
  3517. tmpdiv = last_block;
  3518. r5or6_last_row_offset = do_div(tmpdiv, stripesize);
  3519. tmpdiv = r5or6_last_row_offset;
  3520. r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
  3521. tmpdiv = r5or6_first_row_offset;
  3522. (void) do_div(tmpdiv, map->strip_size);
  3523. first_column = r5or6_first_column = tmpdiv;
  3524. tmpdiv = r5or6_last_row_offset;
  3525. (void) do_div(tmpdiv, map->strip_size);
  3526. r5or6_last_column = tmpdiv;
  3527. #else
  3528. first_row_offset = r5or6_first_row_offset =
  3529. (u32)((first_block % stripesize) %
  3530. r5or6_blocks_per_row);
  3531. r5or6_last_row_offset =
  3532. (u32)((last_block % stripesize) %
  3533. r5or6_blocks_per_row);
  3534. first_column = r5or6_first_column =
  3535. r5or6_first_row_offset / map->strip_size;
  3536. r5or6_last_column =
  3537. r5or6_last_row_offset / map->strip_size;
  3538. #endif
  3539. if (r5or6_first_column != r5or6_last_column)
  3540. return IO_ACCEL_INELIGIBLE;
  3541. /* Request is eligible */
  3542. map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
  3543. map->row_cnt;
  3544. map_index = (first_group *
  3545. (map->row_cnt * total_disks_per_row)) +
  3546. (map_row * total_disks_per_row) + first_column;
  3547. break;
  3548. default:
  3549. return IO_ACCEL_INELIGIBLE;
  3550. }
  3551. disk_handle = dd[map_index].ioaccel_handle;
  3552. disk_block = map->disk_starting_blk + (first_row * map->strip_size) +
  3553. (first_row_offset - (first_column * map->strip_size));
  3554. disk_block_cnt = block_cnt;
  3555. /* handle differing logical/physical block sizes */
  3556. if (map->phys_blk_shift) {
  3557. disk_block <<= map->phys_blk_shift;
  3558. disk_block_cnt <<= map->phys_blk_shift;
  3559. }
  3560. BUG_ON(disk_block_cnt > 0xffff);
  3561. /* build the new CDB for the physical disk I/O */
  3562. if (disk_block > 0xffffffff) {
  3563. cdb[0] = is_write ? WRITE_16 : READ_16;
  3564. cdb[1] = 0;
  3565. cdb[2] = (u8) (disk_block >> 56);
  3566. cdb[3] = (u8) (disk_block >> 48);
  3567. cdb[4] = (u8) (disk_block >> 40);
  3568. cdb[5] = (u8) (disk_block >> 32);
  3569. cdb[6] = (u8) (disk_block >> 24);
  3570. cdb[7] = (u8) (disk_block >> 16);
  3571. cdb[8] = (u8) (disk_block >> 8);
  3572. cdb[9] = (u8) (disk_block);
  3573. cdb[10] = (u8) (disk_block_cnt >> 24);
  3574. cdb[11] = (u8) (disk_block_cnt >> 16);
  3575. cdb[12] = (u8) (disk_block_cnt >> 8);
  3576. cdb[13] = (u8) (disk_block_cnt);
  3577. cdb[14] = 0;
  3578. cdb[15] = 0;
  3579. cdb_len = 16;
  3580. } else {
  3581. cdb[0] = is_write ? WRITE_10 : READ_10;
  3582. cdb[1] = 0;
  3583. cdb[2] = (u8) (disk_block >> 24);
  3584. cdb[3] = (u8) (disk_block >> 16);
  3585. cdb[4] = (u8) (disk_block >> 8);
  3586. cdb[5] = (u8) (disk_block);
  3587. cdb[6] = 0;
  3588. cdb[7] = (u8) (disk_block_cnt >> 8);
  3589. cdb[8] = (u8) (disk_block_cnt);
  3590. cdb[9] = 0;
  3591. cdb_len = 10;
  3592. }
  3593. return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
  3594. dev->scsi3addr);
  3595. }
  3596. static int hpsa_scsi_queue_command_lck(struct scsi_cmnd *cmd,
  3597. void (*done)(struct scsi_cmnd *))
  3598. {
  3599. struct ctlr_info *h;
  3600. struct hpsa_scsi_dev_t *dev;
  3601. unsigned char scsi3addr[8];
  3602. struct CommandList *c;
  3603. int rc = 0;
  3604. /* Get the ptr to our adapter structure out of cmd->host. */
  3605. h = sdev_to_hba(cmd->device);
  3606. dev = cmd->device->hostdata;
  3607. if (!dev) {
  3608. cmd->result = DID_NO_CONNECT << 16;
  3609. done(cmd);
  3610. return 0;
  3611. }
  3612. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  3613. if (unlikely(lockup_detected(h))) {
  3614. cmd->result = DID_ERROR << 16;
  3615. done(cmd);
  3616. return 0;
  3617. }
  3618. c = cmd_alloc(h);
  3619. if (c == NULL) { /* trouble... */
  3620. dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
  3621. return SCSI_MLQUEUE_HOST_BUSY;
  3622. }
  3623. /* Fill in the command list header */
  3624. cmd->scsi_done = done; /* save this for use by completion code */
  3625. /* save c in case we have to abort it */
  3626. cmd->host_scribble = (unsigned char *) c;
  3627. c->cmd_type = CMD_SCSI;
  3628. c->scsi_cmd = cmd;
  3629. /* Call alternate submit routine for I/O accelerated commands.
  3630. * Retries always go down the normal I/O path.
  3631. */
  3632. if (likely(cmd->retries == 0 &&
  3633. cmd->request->cmd_type == REQ_TYPE_FS &&
  3634. h->acciopath_status)) {
  3635. if (dev->offload_enabled) {
  3636. rc = hpsa_scsi_ioaccel_raid_map(h, c);
  3637. if (rc == 0)
  3638. return 0; /* Sent on ioaccel path */
  3639. if (rc < 0) { /* scsi_dma_map failed. */
  3640. cmd_free(h, c);
  3641. return SCSI_MLQUEUE_HOST_BUSY;
  3642. }
  3643. } else if (dev->ioaccel_handle) {
  3644. rc = hpsa_scsi_ioaccel_direct_map(h, c);
  3645. if (rc == 0)
  3646. return 0; /* Sent on direct map path */
  3647. if (rc < 0) { /* scsi_dma_map failed. */
  3648. cmd_free(h, c);
  3649. return SCSI_MLQUEUE_HOST_BUSY;
  3650. }
  3651. }
  3652. }
  3653. c->Header.ReplyQueue = 0; /* unused in simple mode */
  3654. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  3655. c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
  3656. c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
  3657. /* Fill in the request block... */
  3658. c->Request.Timeout = 0;
  3659. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  3660. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  3661. c->Request.CDBLen = cmd->cmd_len;
  3662. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  3663. c->Request.Type.Type = TYPE_CMD;
  3664. c->Request.Type.Attribute = ATTR_SIMPLE;
  3665. switch (cmd->sc_data_direction) {
  3666. case DMA_TO_DEVICE:
  3667. c->Request.Type.Direction = XFER_WRITE;
  3668. break;
  3669. case DMA_FROM_DEVICE:
  3670. c->Request.Type.Direction = XFER_READ;
  3671. break;
  3672. case DMA_NONE:
  3673. c->Request.Type.Direction = XFER_NONE;
  3674. break;
  3675. case DMA_BIDIRECTIONAL:
  3676. /* This can happen if a buggy application does a scsi passthru
  3677. * and sets both inlen and outlen to non-zero. ( see
  3678. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  3679. */
  3680. c->Request.Type.Direction = XFER_RSVD;
  3681. /* This is technically wrong, and hpsa controllers should
  3682. * reject it with CMD_INVALID, which is the most correct
  3683. * response, but non-fibre backends appear to let it
  3684. * slide by, and give the same results as if this field
  3685. * were set correctly. Either way is acceptable for
  3686. * our purposes here.
  3687. */
  3688. break;
  3689. default:
  3690. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  3691. cmd->sc_data_direction);
  3692. BUG();
  3693. break;
  3694. }
  3695. if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
  3696. cmd_free(h, c);
  3697. return SCSI_MLQUEUE_HOST_BUSY;
  3698. }
  3699. enqueue_cmd_and_start_io(h, c);
  3700. /* the cmd'll come back via intr handler in complete_scsi_command() */
  3701. return 0;
  3702. }
  3703. static DEF_SCSI_QCMD(hpsa_scsi_queue_command)
  3704. static int do_not_scan_if_controller_locked_up(struct ctlr_info *h)
  3705. {
  3706. unsigned long flags;
  3707. /*
  3708. * Don't let rescans be initiated on a controller known
  3709. * to be locked up. If the controller locks up *during*
  3710. * a rescan, that thread is probably hosed, but at least
  3711. * we can prevent new rescan threads from piling up on a
  3712. * locked up controller.
  3713. */
  3714. if (unlikely(lockup_detected(h))) {
  3715. spin_lock_irqsave(&h->scan_lock, flags);
  3716. h->scan_finished = 1;
  3717. wake_up_all(&h->scan_wait_queue);
  3718. spin_unlock_irqrestore(&h->scan_lock, flags);
  3719. return 1;
  3720. }
  3721. return 0;
  3722. }
  3723. static void hpsa_scan_start(struct Scsi_Host *sh)
  3724. {
  3725. struct ctlr_info *h = shost_to_hba(sh);
  3726. unsigned long flags;
  3727. if (do_not_scan_if_controller_locked_up(h))
  3728. return;
  3729. /* wait until any scan already in progress is finished. */
  3730. while (1) {
  3731. spin_lock_irqsave(&h->scan_lock, flags);
  3732. if (h->scan_finished)
  3733. break;
  3734. spin_unlock_irqrestore(&h->scan_lock, flags);
  3735. wait_event(h->scan_wait_queue, h->scan_finished);
  3736. /* Note: We don't need to worry about a race between this
  3737. * thread and driver unload because the midlayer will
  3738. * have incremented the reference count, so unload won't
  3739. * happen if we're in here.
  3740. */
  3741. }
  3742. h->scan_finished = 0; /* mark scan as in progress */
  3743. spin_unlock_irqrestore(&h->scan_lock, flags);
  3744. if (do_not_scan_if_controller_locked_up(h))
  3745. return;
  3746. hpsa_update_scsi_devices(h, h->scsi_host->host_no);
  3747. spin_lock_irqsave(&h->scan_lock, flags);
  3748. h->scan_finished = 1; /* mark scan as finished. */
  3749. wake_up_all(&h->scan_wait_queue);
  3750. spin_unlock_irqrestore(&h->scan_lock, flags);
  3751. }
  3752. static int hpsa_scan_finished(struct Scsi_Host *sh,
  3753. unsigned long elapsed_time)
  3754. {
  3755. struct ctlr_info *h = shost_to_hba(sh);
  3756. unsigned long flags;
  3757. int finished;
  3758. spin_lock_irqsave(&h->scan_lock, flags);
  3759. finished = h->scan_finished;
  3760. spin_unlock_irqrestore(&h->scan_lock, flags);
  3761. return finished;
  3762. }
  3763. static int hpsa_change_queue_depth(struct scsi_device *sdev,
  3764. int qdepth, int reason)
  3765. {
  3766. struct ctlr_info *h = sdev_to_hba(sdev);
  3767. if (reason != SCSI_QDEPTH_DEFAULT)
  3768. return -ENOTSUPP;
  3769. if (qdepth < 1)
  3770. qdepth = 1;
  3771. else
  3772. if (qdepth > h->nr_cmds)
  3773. qdepth = h->nr_cmds;
  3774. scsi_adjust_queue_depth(sdev, qdepth);
  3775. return sdev->queue_depth;
  3776. }
  3777. static void hpsa_unregister_scsi(struct ctlr_info *h)
  3778. {
  3779. /* we are being forcibly unloaded, and may not refuse. */
  3780. scsi_remove_host(h->scsi_host);
  3781. scsi_host_put(h->scsi_host);
  3782. h->scsi_host = NULL;
  3783. }
  3784. static int hpsa_register_scsi(struct ctlr_info *h)
  3785. {
  3786. struct Scsi_Host *sh;
  3787. int error;
  3788. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  3789. if (sh == NULL)
  3790. goto fail;
  3791. sh->io_port = 0;
  3792. sh->n_io_port = 0;
  3793. sh->this_id = -1;
  3794. sh->max_channel = 3;
  3795. sh->max_cmd_len = MAX_COMMAND_SIZE;
  3796. sh->max_lun = HPSA_MAX_LUN;
  3797. sh->max_id = HPSA_MAX_LUN;
  3798. sh->can_queue = h->nr_cmds;
  3799. if (h->hba_mode_enabled)
  3800. sh->cmd_per_lun = 7;
  3801. else
  3802. sh->cmd_per_lun = h->nr_cmds;
  3803. sh->sg_tablesize = h->maxsgentries;
  3804. h->scsi_host = sh;
  3805. sh->hostdata[0] = (unsigned long) h;
  3806. sh->irq = h->intr[h->intr_mode];
  3807. sh->unique_id = sh->irq;
  3808. error = scsi_add_host(sh, &h->pdev->dev);
  3809. if (error)
  3810. goto fail_host_put;
  3811. scsi_scan_host(sh);
  3812. return 0;
  3813. fail_host_put:
  3814. dev_err(&h->pdev->dev, "%s: scsi_add_host"
  3815. " failed for controller %d\n", __func__, h->ctlr);
  3816. scsi_host_put(sh);
  3817. return error;
  3818. fail:
  3819. dev_err(&h->pdev->dev, "%s: scsi_host_alloc"
  3820. " failed for controller %d\n", __func__, h->ctlr);
  3821. return -ENOMEM;
  3822. }
  3823. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  3824. unsigned char lunaddr[])
  3825. {
  3826. int rc;
  3827. int count = 0;
  3828. int waittime = 1; /* seconds */
  3829. struct CommandList *c;
  3830. c = cmd_special_alloc(h);
  3831. if (!c) {
  3832. dev_warn(&h->pdev->dev, "out of memory in "
  3833. "wait_for_device_to_become_ready.\n");
  3834. return IO_ERROR;
  3835. }
  3836. /* Send test unit ready until device ready, or give up. */
  3837. while (count < HPSA_TUR_RETRY_LIMIT) {
  3838. /* Wait for a bit. do this first, because if we send
  3839. * the TUR right away, the reset will just abort it.
  3840. */
  3841. msleep(1000 * waittime);
  3842. count++;
  3843. rc = 0; /* Device ready. */
  3844. /* Increase wait time with each try, up to a point. */
  3845. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  3846. waittime = waittime * 2;
  3847. /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
  3848. (void) fill_cmd(c, TEST_UNIT_READY, h,
  3849. NULL, 0, 0, lunaddr, TYPE_CMD);
  3850. hpsa_scsi_do_simple_cmd_core(h, c);
  3851. /* no unmap needed here because no data xfer. */
  3852. if (c->err_info->CommandStatus == CMD_SUCCESS)
  3853. break;
  3854. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  3855. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  3856. (c->err_info->SenseInfo[2] == NO_SENSE ||
  3857. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  3858. break;
  3859. dev_warn(&h->pdev->dev, "waiting %d secs "
  3860. "for device to become ready.\n", waittime);
  3861. rc = 1; /* device not ready. */
  3862. }
  3863. if (rc)
  3864. dev_warn(&h->pdev->dev, "giving up on device.\n");
  3865. else
  3866. dev_warn(&h->pdev->dev, "device is ready.\n");
  3867. cmd_special_free(h, c);
  3868. return rc;
  3869. }
  3870. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  3871. * complaining. Doing a host- or bus-reset can't do anything good here.
  3872. */
  3873. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  3874. {
  3875. int rc;
  3876. struct ctlr_info *h;
  3877. struct hpsa_scsi_dev_t *dev;
  3878. /* find the controller to which the command to be aborted was sent */
  3879. h = sdev_to_hba(scsicmd->device);
  3880. if (h == NULL) /* paranoia */
  3881. return FAILED;
  3882. dev = scsicmd->device->hostdata;
  3883. if (!dev) {
  3884. dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
  3885. "device lookup failed.\n");
  3886. return FAILED;
  3887. }
  3888. dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
  3889. h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
  3890. /* send a reset to the SCSI LUN which the command was sent to */
  3891. rc = hpsa_send_reset(h, dev->scsi3addr, HPSA_RESET_TYPE_LUN);
  3892. if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
  3893. return SUCCESS;
  3894. dev_warn(&h->pdev->dev, "resetting device failed.\n");
  3895. return FAILED;
  3896. }
  3897. static void swizzle_abort_tag(u8 *tag)
  3898. {
  3899. u8 original_tag[8];
  3900. memcpy(original_tag, tag, 8);
  3901. tag[0] = original_tag[3];
  3902. tag[1] = original_tag[2];
  3903. tag[2] = original_tag[1];
  3904. tag[3] = original_tag[0];
  3905. tag[4] = original_tag[7];
  3906. tag[5] = original_tag[6];
  3907. tag[6] = original_tag[5];
  3908. tag[7] = original_tag[4];
  3909. }
  3910. static void hpsa_get_tag(struct ctlr_info *h,
  3911. struct CommandList *c, u32 *taglower, u32 *tagupper)
  3912. {
  3913. if (c->cmd_type == CMD_IOACCEL1) {
  3914. struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
  3915. &h->ioaccel_cmd_pool[c->cmdindex];
  3916. *tagupper = cm1->Tag.upper;
  3917. *taglower = cm1->Tag.lower;
  3918. return;
  3919. }
  3920. if (c->cmd_type == CMD_IOACCEL2) {
  3921. struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
  3922. &h->ioaccel2_cmd_pool[c->cmdindex];
  3923. /* upper tag not used in ioaccel2 mode */
  3924. memset(tagupper, 0, sizeof(*tagupper));
  3925. *taglower = cm2->Tag;
  3926. return;
  3927. }
  3928. *tagupper = c->Header.Tag.upper;
  3929. *taglower = c->Header.Tag.lower;
  3930. }
  3931. static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
  3932. struct CommandList *abort, int swizzle)
  3933. {
  3934. int rc = IO_OK;
  3935. struct CommandList *c;
  3936. struct ErrorInfo *ei;
  3937. u32 tagupper, taglower;
  3938. c = cmd_special_alloc(h);
  3939. if (c == NULL) { /* trouble... */
  3940. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  3941. return -ENOMEM;
  3942. }
  3943. /* fill_cmd can't fail here, no buffer to map */
  3944. (void) fill_cmd(c, HPSA_ABORT_MSG, h, abort,
  3945. 0, 0, scsi3addr, TYPE_MSG);
  3946. if (swizzle)
  3947. swizzle_abort_tag(&c->Request.CDB[4]);
  3948. hpsa_scsi_do_simple_cmd_core(h, c);
  3949. hpsa_get_tag(h, abort, &taglower, &tagupper);
  3950. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd_core completed.\n",
  3951. __func__, tagupper, taglower);
  3952. /* no unmap needed here because no data xfer. */
  3953. ei = c->err_info;
  3954. switch (ei->CommandStatus) {
  3955. case CMD_SUCCESS:
  3956. break;
  3957. case CMD_UNABORTABLE: /* Very common, don't make noise. */
  3958. rc = -1;
  3959. break;
  3960. default:
  3961. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
  3962. __func__, tagupper, taglower);
  3963. hpsa_scsi_interpret_error(h, c);
  3964. rc = -1;
  3965. break;
  3966. }
  3967. cmd_special_free(h, c);
  3968. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
  3969. __func__, tagupper, taglower);
  3970. return rc;
  3971. }
  3972. /*
  3973. * hpsa_find_cmd_in_queue
  3974. *
  3975. * Used to determine whether a command (find) is still present
  3976. * in queue_head. Optionally excludes the last element of queue_head.
  3977. *
  3978. * This is used to avoid unnecessary aborts. Commands in h->reqQ have
  3979. * not yet been submitted, and so can be aborted by the driver without
  3980. * sending an abort to the hardware.
  3981. *
  3982. * Returns pointer to command if found in queue, NULL otherwise.
  3983. */
  3984. static struct CommandList *hpsa_find_cmd_in_queue(struct ctlr_info *h,
  3985. struct scsi_cmnd *find, struct list_head *queue_head)
  3986. {
  3987. unsigned long flags;
  3988. struct CommandList *c = NULL; /* ptr into cmpQ */
  3989. if (!find)
  3990. return 0;
  3991. spin_lock_irqsave(&h->lock, flags);
  3992. list_for_each_entry(c, queue_head, list) {
  3993. if (c->scsi_cmd == NULL) /* e.g.: passthru ioctl */
  3994. continue;
  3995. if (c->scsi_cmd == find) {
  3996. spin_unlock_irqrestore(&h->lock, flags);
  3997. return c;
  3998. }
  3999. }
  4000. spin_unlock_irqrestore(&h->lock, flags);
  4001. return NULL;
  4002. }
  4003. static struct CommandList *hpsa_find_cmd_in_queue_by_tag(struct ctlr_info *h,
  4004. u8 *tag, struct list_head *queue_head)
  4005. {
  4006. unsigned long flags;
  4007. struct CommandList *c;
  4008. spin_lock_irqsave(&h->lock, flags);
  4009. list_for_each_entry(c, queue_head, list) {
  4010. if (memcmp(&c->Header.Tag, tag, 8) != 0)
  4011. continue;
  4012. spin_unlock_irqrestore(&h->lock, flags);
  4013. return c;
  4014. }
  4015. spin_unlock_irqrestore(&h->lock, flags);
  4016. return NULL;
  4017. }
  4018. /* ioaccel2 path firmware cannot handle abort task requests.
  4019. * Change abort requests to physical target reset, and send to the
  4020. * address of the physical disk used for the ioaccel 2 command.
  4021. * Return 0 on success (IO_OK)
  4022. * -1 on failure
  4023. */
  4024. static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
  4025. unsigned char *scsi3addr, struct CommandList *abort)
  4026. {
  4027. int rc = IO_OK;
  4028. struct scsi_cmnd *scmd; /* scsi command within request being aborted */
  4029. struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
  4030. unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
  4031. unsigned char *psa = &phys_scsi3addr[0];
  4032. /* Get a pointer to the hpsa logical device. */
  4033. scmd = (struct scsi_cmnd *) abort->scsi_cmd;
  4034. dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
  4035. if (dev == NULL) {
  4036. dev_warn(&h->pdev->dev,
  4037. "Cannot abort: no device pointer for command.\n");
  4038. return -1; /* not abortable */
  4039. }
  4040. if (h->raid_offload_debug > 0)
  4041. dev_info(&h->pdev->dev,
  4042. "Reset as abort: Abort requested on C%d:B%d:T%d:L%d scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  4043. h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
  4044. scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
  4045. scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
  4046. if (!dev->offload_enabled) {
  4047. dev_warn(&h->pdev->dev,
  4048. "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
  4049. return -1; /* not abortable */
  4050. }
  4051. /* Incoming scsi3addr is logical addr. We need physical disk addr. */
  4052. if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
  4053. dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
  4054. return -1; /* not abortable */
  4055. }
  4056. /* send the reset */
  4057. if (h->raid_offload_debug > 0)
  4058. dev_info(&h->pdev->dev,
  4059. "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  4060. psa[0], psa[1], psa[2], psa[3],
  4061. psa[4], psa[5], psa[6], psa[7]);
  4062. rc = hpsa_send_reset(h, psa, HPSA_RESET_TYPE_TARGET);
  4063. if (rc != 0) {
  4064. dev_warn(&h->pdev->dev,
  4065. "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  4066. psa[0], psa[1], psa[2], psa[3],
  4067. psa[4], psa[5], psa[6], psa[7]);
  4068. return rc; /* failed to reset */
  4069. }
  4070. /* wait for device to recover */
  4071. if (wait_for_device_to_become_ready(h, psa) != 0) {
  4072. dev_warn(&h->pdev->dev,
  4073. "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  4074. psa[0], psa[1], psa[2], psa[3],
  4075. psa[4], psa[5], psa[6], psa[7]);
  4076. return -1; /* failed to recover */
  4077. }
  4078. /* device recovered */
  4079. dev_info(&h->pdev->dev,
  4080. "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  4081. psa[0], psa[1], psa[2], psa[3],
  4082. psa[4], psa[5], psa[6], psa[7]);
  4083. return rc; /* success */
  4084. }
  4085. /* Some Smart Arrays need the abort tag swizzled, and some don't. It's hard to
  4086. * tell which kind we're dealing with, so we send the abort both ways. There
  4087. * shouldn't be any collisions between swizzled and unswizzled tags due to the
  4088. * way we construct our tags but we check anyway in case the assumptions which
  4089. * make this true someday become false.
  4090. */
  4091. static int hpsa_send_abort_both_ways(struct ctlr_info *h,
  4092. unsigned char *scsi3addr, struct CommandList *abort)
  4093. {
  4094. u8 swizzled_tag[8];
  4095. struct CommandList *c;
  4096. int rc = 0, rc2 = 0;
  4097. /* ioccelerator mode 2 commands should be aborted via the
  4098. * accelerated path, since RAID path is unaware of these commands,
  4099. * but underlying firmware can't handle abort TMF.
  4100. * Change abort to physical device reset.
  4101. */
  4102. if (abort->cmd_type == CMD_IOACCEL2)
  4103. return hpsa_send_reset_as_abort_ioaccel2(h, scsi3addr, abort);
  4104. /* we do not expect to find the swizzled tag in our queue, but
  4105. * check anyway just to be sure the assumptions which make this
  4106. * the case haven't become wrong.
  4107. */
  4108. memcpy(swizzled_tag, &abort->Request.CDB[4], 8);
  4109. swizzle_abort_tag(swizzled_tag);
  4110. c = hpsa_find_cmd_in_queue_by_tag(h, swizzled_tag, &h->cmpQ);
  4111. if (c != NULL) {
  4112. dev_warn(&h->pdev->dev, "Unexpectedly found byte-swapped tag in completion queue.\n");
  4113. return hpsa_send_abort(h, scsi3addr, abort, 0);
  4114. }
  4115. rc = hpsa_send_abort(h, scsi3addr, abort, 0);
  4116. /* if the command is still in our queue, we can't conclude that it was
  4117. * aborted (it might have just completed normally) but in any case
  4118. * we don't need to try to abort it another way.
  4119. */
  4120. c = hpsa_find_cmd_in_queue(h, abort->scsi_cmd, &h->cmpQ);
  4121. if (c)
  4122. rc2 = hpsa_send_abort(h, scsi3addr, abort, 1);
  4123. return rc && rc2;
  4124. }
  4125. /* Send an abort for the specified command.
  4126. * If the device and controller support it,
  4127. * send a task abort request.
  4128. */
  4129. static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
  4130. {
  4131. int i, rc;
  4132. struct ctlr_info *h;
  4133. struct hpsa_scsi_dev_t *dev;
  4134. struct CommandList *abort; /* pointer to command to be aborted */
  4135. struct CommandList *found;
  4136. struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
  4137. char msg[256]; /* For debug messaging. */
  4138. int ml = 0;
  4139. u32 tagupper, taglower;
  4140. /* Find the controller of the command to be aborted */
  4141. h = sdev_to_hba(sc->device);
  4142. if (WARN(h == NULL,
  4143. "ABORT REQUEST FAILED, Controller lookup failed.\n"))
  4144. return FAILED;
  4145. /* Check that controller supports some kind of task abort */
  4146. if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
  4147. !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
  4148. return FAILED;
  4149. memset(msg, 0, sizeof(msg));
  4150. ml += sprintf(msg+ml, "ABORT REQUEST on C%d:B%d:T%d:L%llu ",
  4151. h->scsi_host->host_no, sc->device->channel,
  4152. sc->device->id, sc->device->lun);
  4153. /* Find the device of the command to be aborted */
  4154. dev = sc->device->hostdata;
  4155. if (!dev) {
  4156. dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
  4157. msg);
  4158. return FAILED;
  4159. }
  4160. /* Get SCSI command to be aborted */
  4161. abort = (struct CommandList *) sc->host_scribble;
  4162. if (abort == NULL) {
  4163. dev_err(&h->pdev->dev, "%s FAILED, Command to abort is NULL.\n",
  4164. msg);
  4165. return FAILED;
  4166. }
  4167. hpsa_get_tag(h, abort, &taglower, &tagupper);
  4168. ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
  4169. as = (struct scsi_cmnd *) abort->scsi_cmd;
  4170. if (as != NULL)
  4171. ml += sprintf(msg+ml, "Command:0x%x SN:0x%lx ",
  4172. as->cmnd[0], as->serial_number);
  4173. dev_dbg(&h->pdev->dev, "%s\n", msg);
  4174. dev_warn(&h->pdev->dev, "Abort request on C%d:B%d:T%d:L%d\n",
  4175. h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
  4176. /* Search reqQ to See if command is queued but not submitted,
  4177. * if so, complete the command with aborted status and remove
  4178. * it from the reqQ.
  4179. */
  4180. found = hpsa_find_cmd_in_queue(h, sc, &h->reqQ);
  4181. if (found) {
  4182. found->err_info->CommandStatus = CMD_ABORTED;
  4183. finish_cmd(found);
  4184. dev_info(&h->pdev->dev, "%s Request SUCCEEDED (driver queue).\n",
  4185. msg);
  4186. return SUCCESS;
  4187. }
  4188. /* not in reqQ, if also not in cmpQ, must have already completed */
  4189. found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
  4190. if (!found) {
  4191. dev_dbg(&h->pdev->dev, "%s Request SUCCEEDED (not known to driver).\n",
  4192. msg);
  4193. return SUCCESS;
  4194. }
  4195. /*
  4196. * Command is in flight, or possibly already completed
  4197. * by the firmware (but not to the scsi mid layer) but we can't
  4198. * distinguish which. Send the abort down.
  4199. */
  4200. rc = hpsa_send_abort_both_ways(h, dev->scsi3addr, abort);
  4201. if (rc != 0) {
  4202. dev_dbg(&h->pdev->dev, "%s Request FAILED.\n", msg);
  4203. dev_warn(&h->pdev->dev, "FAILED abort on device C%d:B%d:T%d:L%d\n",
  4204. h->scsi_host->host_no,
  4205. dev->bus, dev->target, dev->lun);
  4206. return FAILED;
  4207. }
  4208. dev_info(&h->pdev->dev, "%s REQUEST SUCCEEDED.\n", msg);
  4209. /* If the abort(s) above completed and actually aborted the
  4210. * command, then the command to be aborted should already be
  4211. * completed. If not, wait around a bit more to see if they
  4212. * manage to complete normally.
  4213. */
  4214. #define ABORT_COMPLETE_WAIT_SECS 30
  4215. for (i = 0; i < ABORT_COMPLETE_WAIT_SECS * 10; i++) {
  4216. found = hpsa_find_cmd_in_queue(h, sc, &h->cmpQ);
  4217. if (!found)
  4218. return SUCCESS;
  4219. msleep(100);
  4220. }
  4221. dev_warn(&h->pdev->dev, "%s FAILED. Aborted command has not completed after %d seconds.\n",
  4222. msg, ABORT_COMPLETE_WAIT_SECS);
  4223. return FAILED;
  4224. }
  4225. /*
  4226. * For operations that cannot sleep, a command block is allocated at init,
  4227. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  4228. * which ones are free or in use. Lock must be held when calling this.
  4229. * cmd_free() is the complement.
  4230. */
  4231. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  4232. {
  4233. struct CommandList *c;
  4234. int i;
  4235. union u64bit temp64;
  4236. dma_addr_t cmd_dma_handle, err_dma_handle;
  4237. unsigned long flags;
  4238. spin_lock_irqsave(&h->lock, flags);
  4239. do {
  4240. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  4241. if (i == h->nr_cmds) {
  4242. spin_unlock_irqrestore(&h->lock, flags);
  4243. return NULL;
  4244. }
  4245. } while (test_and_set_bit
  4246. (i & (BITS_PER_LONG - 1),
  4247. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  4248. spin_unlock_irqrestore(&h->lock, flags);
  4249. c = h->cmd_pool + i;
  4250. memset(c, 0, sizeof(*c));
  4251. cmd_dma_handle = h->cmd_pool_dhandle
  4252. + i * sizeof(*c);
  4253. c->err_info = h->errinfo_pool + i;
  4254. memset(c->err_info, 0, sizeof(*c->err_info));
  4255. err_dma_handle = h->errinfo_pool_dhandle
  4256. + i * sizeof(*c->err_info);
  4257. c->cmdindex = i;
  4258. INIT_LIST_HEAD(&c->list);
  4259. c->busaddr = (u32) cmd_dma_handle;
  4260. temp64.val = (u64) err_dma_handle;
  4261. c->ErrDesc.Addr.lower = temp64.val32.lower;
  4262. c->ErrDesc.Addr.upper = temp64.val32.upper;
  4263. c->ErrDesc.Len = sizeof(*c->err_info);
  4264. c->h = h;
  4265. return c;
  4266. }
  4267. /* For operations that can wait for kmalloc to possibly sleep,
  4268. * this routine can be called. Lock need not be held to call
  4269. * cmd_special_alloc. cmd_special_free() is the complement.
  4270. */
  4271. static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
  4272. {
  4273. struct CommandList *c;
  4274. union u64bit temp64;
  4275. dma_addr_t cmd_dma_handle, err_dma_handle;
  4276. c = pci_zalloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
  4277. if (c == NULL)
  4278. return NULL;
  4279. c->cmd_type = CMD_SCSI;
  4280. c->cmdindex = -1;
  4281. c->err_info = pci_zalloc_consistent(h->pdev, sizeof(*c->err_info),
  4282. &err_dma_handle);
  4283. if (c->err_info == NULL) {
  4284. pci_free_consistent(h->pdev,
  4285. sizeof(*c), c, cmd_dma_handle);
  4286. return NULL;
  4287. }
  4288. INIT_LIST_HEAD(&c->list);
  4289. c->busaddr = (u32) cmd_dma_handle;
  4290. temp64.val = (u64) err_dma_handle;
  4291. c->ErrDesc.Addr.lower = temp64.val32.lower;
  4292. c->ErrDesc.Addr.upper = temp64.val32.upper;
  4293. c->ErrDesc.Len = sizeof(*c->err_info);
  4294. c->h = h;
  4295. return c;
  4296. }
  4297. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  4298. {
  4299. int i;
  4300. unsigned long flags;
  4301. i = c - h->cmd_pool;
  4302. spin_lock_irqsave(&h->lock, flags);
  4303. clear_bit(i & (BITS_PER_LONG - 1),
  4304. h->cmd_pool_bits + (i / BITS_PER_LONG));
  4305. spin_unlock_irqrestore(&h->lock, flags);
  4306. }
  4307. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
  4308. {
  4309. union u64bit temp64;
  4310. temp64.val32.lower = c->ErrDesc.Addr.lower;
  4311. temp64.val32.upper = c->ErrDesc.Addr.upper;
  4312. pci_free_consistent(h->pdev, sizeof(*c->err_info),
  4313. c->err_info, (dma_addr_t) temp64.val);
  4314. pci_free_consistent(h->pdev, sizeof(*c),
  4315. c, (dma_addr_t) (c->busaddr & DIRECT_LOOKUP_MASK));
  4316. }
  4317. #ifdef CONFIG_COMPAT
  4318. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
  4319. {
  4320. IOCTL32_Command_struct __user *arg32 =
  4321. (IOCTL32_Command_struct __user *) arg;
  4322. IOCTL_Command_struct arg64;
  4323. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  4324. int err;
  4325. u32 cp;
  4326. memset(&arg64, 0, sizeof(arg64));
  4327. err = 0;
  4328. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  4329. sizeof(arg64.LUN_info));
  4330. err |= copy_from_user(&arg64.Request, &arg32->Request,
  4331. sizeof(arg64.Request));
  4332. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  4333. sizeof(arg64.error_info));
  4334. err |= get_user(arg64.buf_size, &arg32->buf_size);
  4335. err |= get_user(cp, &arg32->buf);
  4336. arg64.buf = compat_ptr(cp);
  4337. err |= copy_to_user(p, &arg64, sizeof(arg64));
  4338. if (err)
  4339. return -EFAULT;
  4340. err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
  4341. if (err)
  4342. return err;
  4343. err |= copy_in_user(&arg32->error_info, &p->error_info,
  4344. sizeof(arg32->error_info));
  4345. if (err)
  4346. return -EFAULT;
  4347. return err;
  4348. }
  4349. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  4350. int cmd, void *arg)
  4351. {
  4352. BIG_IOCTL32_Command_struct __user *arg32 =
  4353. (BIG_IOCTL32_Command_struct __user *) arg;
  4354. BIG_IOCTL_Command_struct arg64;
  4355. BIG_IOCTL_Command_struct __user *p =
  4356. compat_alloc_user_space(sizeof(arg64));
  4357. int err;
  4358. u32 cp;
  4359. memset(&arg64, 0, sizeof(arg64));
  4360. err = 0;
  4361. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  4362. sizeof(arg64.LUN_info));
  4363. err |= copy_from_user(&arg64.Request, &arg32->Request,
  4364. sizeof(arg64.Request));
  4365. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  4366. sizeof(arg64.error_info));
  4367. err |= get_user(arg64.buf_size, &arg32->buf_size);
  4368. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  4369. err |= get_user(cp, &arg32->buf);
  4370. arg64.buf = compat_ptr(cp);
  4371. err |= copy_to_user(p, &arg64, sizeof(arg64));
  4372. if (err)
  4373. return -EFAULT;
  4374. err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
  4375. if (err)
  4376. return err;
  4377. err |= copy_in_user(&arg32->error_info, &p->error_info,
  4378. sizeof(arg32->error_info));
  4379. if (err)
  4380. return -EFAULT;
  4381. return err;
  4382. }
  4383. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
  4384. {
  4385. switch (cmd) {
  4386. case CCISS_GETPCIINFO:
  4387. case CCISS_GETINTINFO:
  4388. case CCISS_SETINTINFO:
  4389. case CCISS_GETNODENAME:
  4390. case CCISS_SETNODENAME:
  4391. case CCISS_GETHEARTBEAT:
  4392. case CCISS_GETBUSTYPES:
  4393. case CCISS_GETFIRMVER:
  4394. case CCISS_GETDRIVVER:
  4395. case CCISS_REVALIDVOLS:
  4396. case CCISS_DEREGDISK:
  4397. case CCISS_REGNEWDISK:
  4398. case CCISS_REGNEWD:
  4399. case CCISS_RESCANDISK:
  4400. case CCISS_GETLUNINFO:
  4401. return hpsa_ioctl(dev, cmd, arg);
  4402. case CCISS_PASSTHRU32:
  4403. return hpsa_ioctl32_passthru(dev, cmd, arg);
  4404. case CCISS_BIG_PASSTHRU32:
  4405. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  4406. default:
  4407. return -ENOIOCTLCMD;
  4408. }
  4409. }
  4410. #endif
  4411. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  4412. {
  4413. struct hpsa_pci_info pciinfo;
  4414. if (!argp)
  4415. return -EINVAL;
  4416. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  4417. pciinfo.bus = h->pdev->bus->number;
  4418. pciinfo.dev_fn = h->pdev->devfn;
  4419. pciinfo.board_id = h->board_id;
  4420. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  4421. return -EFAULT;
  4422. return 0;
  4423. }
  4424. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  4425. {
  4426. DriverVer_type DriverVer;
  4427. unsigned char vmaj, vmin, vsubmin;
  4428. int rc;
  4429. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  4430. &vmaj, &vmin, &vsubmin);
  4431. if (rc != 3) {
  4432. dev_info(&h->pdev->dev, "driver version string '%s' "
  4433. "unrecognized.", HPSA_DRIVER_VERSION);
  4434. vmaj = 0;
  4435. vmin = 0;
  4436. vsubmin = 0;
  4437. }
  4438. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  4439. if (!argp)
  4440. return -EINVAL;
  4441. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  4442. return -EFAULT;
  4443. return 0;
  4444. }
  4445. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  4446. {
  4447. IOCTL_Command_struct iocommand;
  4448. struct CommandList *c;
  4449. char *buff = NULL;
  4450. union u64bit temp64;
  4451. int rc = 0;
  4452. if (!argp)
  4453. return -EINVAL;
  4454. if (!capable(CAP_SYS_RAWIO))
  4455. return -EPERM;
  4456. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  4457. return -EFAULT;
  4458. if ((iocommand.buf_size < 1) &&
  4459. (iocommand.Request.Type.Direction != XFER_NONE)) {
  4460. return -EINVAL;
  4461. }
  4462. if (iocommand.buf_size > 0) {
  4463. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  4464. if (buff == NULL)
  4465. return -EFAULT;
  4466. if (iocommand.Request.Type.Direction & XFER_WRITE) {
  4467. /* Copy the data into the buffer we created */
  4468. if (copy_from_user(buff, iocommand.buf,
  4469. iocommand.buf_size)) {
  4470. rc = -EFAULT;
  4471. goto out_kfree;
  4472. }
  4473. } else {
  4474. memset(buff, 0, iocommand.buf_size);
  4475. }
  4476. }
  4477. c = cmd_special_alloc(h);
  4478. if (c == NULL) {
  4479. rc = -ENOMEM;
  4480. goto out_kfree;
  4481. }
  4482. /* Fill in the command type */
  4483. c->cmd_type = CMD_IOCTL_PEND;
  4484. /* Fill in Command Header */
  4485. c->Header.ReplyQueue = 0; /* unused in simple mode */
  4486. if (iocommand.buf_size > 0) { /* buffer to fill */
  4487. c->Header.SGList = 1;
  4488. c->Header.SGTotal = 1;
  4489. } else { /* no buffers to fill */
  4490. c->Header.SGList = 0;
  4491. c->Header.SGTotal = 0;
  4492. }
  4493. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  4494. /* use the kernel address the cmd block for tag */
  4495. c->Header.Tag.lower = c->busaddr;
  4496. /* Fill in Request block */
  4497. memcpy(&c->Request, &iocommand.Request,
  4498. sizeof(c->Request));
  4499. /* Fill in the scatter gather information */
  4500. if (iocommand.buf_size > 0) {
  4501. temp64.val = pci_map_single(h->pdev, buff,
  4502. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  4503. if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
  4504. c->SG[0].Addr.lower = 0;
  4505. c->SG[0].Addr.upper = 0;
  4506. c->SG[0].Len = 0;
  4507. rc = -ENOMEM;
  4508. goto out;
  4509. }
  4510. c->SG[0].Addr.lower = temp64.val32.lower;
  4511. c->SG[0].Addr.upper = temp64.val32.upper;
  4512. c->SG[0].Len = iocommand.buf_size;
  4513. c->SG[0].Ext = HPSA_SG_LAST; /* we are not chaining*/
  4514. }
  4515. hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
  4516. if (iocommand.buf_size > 0)
  4517. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  4518. check_ioctl_unit_attention(h, c);
  4519. /* Copy the error information out */
  4520. memcpy(&iocommand.error_info, c->err_info,
  4521. sizeof(iocommand.error_info));
  4522. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  4523. rc = -EFAULT;
  4524. goto out;
  4525. }
  4526. if ((iocommand.Request.Type.Direction & XFER_READ) &&
  4527. iocommand.buf_size > 0) {
  4528. /* Copy the data out of the buffer we created */
  4529. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  4530. rc = -EFAULT;
  4531. goto out;
  4532. }
  4533. }
  4534. out:
  4535. cmd_special_free(h, c);
  4536. out_kfree:
  4537. kfree(buff);
  4538. return rc;
  4539. }
  4540. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  4541. {
  4542. BIG_IOCTL_Command_struct *ioc;
  4543. struct CommandList *c;
  4544. unsigned char **buff = NULL;
  4545. int *buff_size = NULL;
  4546. union u64bit temp64;
  4547. BYTE sg_used = 0;
  4548. int status = 0;
  4549. int i;
  4550. u32 left;
  4551. u32 sz;
  4552. BYTE __user *data_ptr;
  4553. if (!argp)
  4554. return -EINVAL;
  4555. if (!capable(CAP_SYS_RAWIO))
  4556. return -EPERM;
  4557. ioc = (BIG_IOCTL_Command_struct *)
  4558. kmalloc(sizeof(*ioc), GFP_KERNEL);
  4559. if (!ioc) {
  4560. status = -ENOMEM;
  4561. goto cleanup1;
  4562. }
  4563. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  4564. status = -EFAULT;
  4565. goto cleanup1;
  4566. }
  4567. if ((ioc->buf_size < 1) &&
  4568. (ioc->Request.Type.Direction != XFER_NONE)) {
  4569. status = -EINVAL;
  4570. goto cleanup1;
  4571. }
  4572. /* Check kmalloc limits using all SGs */
  4573. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  4574. status = -EINVAL;
  4575. goto cleanup1;
  4576. }
  4577. if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
  4578. status = -EINVAL;
  4579. goto cleanup1;
  4580. }
  4581. buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
  4582. if (!buff) {
  4583. status = -ENOMEM;
  4584. goto cleanup1;
  4585. }
  4586. buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
  4587. if (!buff_size) {
  4588. status = -ENOMEM;
  4589. goto cleanup1;
  4590. }
  4591. left = ioc->buf_size;
  4592. data_ptr = ioc->buf;
  4593. while (left) {
  4594. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  4595. buff_size[sg_used] = sz;
  4596. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  4597. if (buff[sg_used] == NULL) {
  4598. status = -ENOMEM;
  4599. goto cleanup1;
  4600. }
  4601. if (ioc->Request.Type.Direction & XFER_WRITE) {
  4602. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  4603. status = -EFAULT;
  4604. goto cleanup1;
  4605. }
  4606. } else
  4607. memset(buff[sg_used], 0, sz);
  4608. left -= sz;
  4609. data_ptr += sz;
  4610. sg_used++;
  4611. }
  4612. c = cmd_special_alloc(h);
  4613. if (c == NULL) {
  4614. status = -ENOMEM;
  4615. goto cleanup1;
  4616. }
  4617. c->cmd_type = CMD_IOCTL_PEND;
  4618. c->Header.ReplyQueue = 0;
  4619. c->Header.SGList = c->Header.SGTotal = sg_used;
  4620. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  4621. c->Header.Tag.lower = c->busaddr;
  4622. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  4623. if (ioc->buf_size > 0) {
  4624. int i;
  4625. for (i = 0; i < sg_used; i++) {
  4626. temp64.val = pci_map_single(h->pdev, buff[i],
  4627. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  4628. if (dma_mapping_error(&h->pdev->dev, temp64.val)) {
  4629. c->SG[i].Addr.lower = 0;
  4630. c->SG[i].Addr.upper = 0;
  4631. c->SG[i].Len = 0;
  4632. hpsa_pci_unmap(h->pdev, c, i,
  4633. PCI_DMA_BIDIRECTIONAL);
  4634. status = -ENOMEM;
  4635. goto cleanup0;
  4636. }
  4637. c->SG[i].Addr.lower = temp64.val32.lower;
  4638. c->SG[i].Addr.upper = temp64.val32.upper;
  4639. c->SG[i].Len = buff_size[i];
  4640. c->SG[i].Ext = i < sg_used - 1 ? 0 : HPSA_SG_LAST;
  4641. }
  4642. }
  4643. hpsa_scsi_do_simple_cmd_core_if_no_lockup(h, c);
  4644. if (sg_used)
  4645. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  4646. check_ioctl_unit_attention(h, c);
  4647. /* Copy the error information out */
  4648. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  4649. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  4650. status = -EFAULT;
  4651. goto cleanup0;
  4652. }
  4653. if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
  4654. /* Copy the data out of the buffer we created */
  4655. BYTE __user *ptr = ioc->buf;
  4656. for (i = 0; i < sg_used; i++) {
  4657. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  4658. status = -EFAULT;
  4659. goto cleanup0;
  4660. }
  4661. ptr += buff_size[i];
  4662. }
  4663. }
  4664. status = 0;
  4665. cleanup0:
  4666. cmd_special_free(h, c);
  4667. cleanup1:
  4668. if (buff) {
  4669. for (i = 0; i < sg_used; i++)
  4670. kfree(buff[i]);
  4671. kfree(buff);
  4672. }
  4673. kfree(buff_size);
  4674. kfree(ioc);
  4675. return status;
  4676. }
  4677. static void check_ioctl_unit_attention(struct ctlr_info *h,
  4678. struct CommandList *c)
  4679. {
  4680. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  4681. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  4682. (void) check_for_unit_attention(h, c);
  4683. }
  4684. static int increment_passthru_count(struct ctlr_info *h)
  4685. {
  4686. unsigned long flags;
  4687. spin_lock_irqsave(&h->passthru_count_lock, flags);
  4688. if (h->passthru_count >= HPSA_MAX_CONCURRENT_PASSTHRUS) {
  4689. spin_unlock_irqrestore(&h->passthru_count_lock, flags);
  4690. return -1;
  4691. }
  4692. h->passthru_count++;
  4693. spin_unlock_irqrestore(&h->passthru_count_lock, flags);
  4694. return 0;
  4695. }
  4696. static void decrement_passthru_count(struct ctlr_info *h)
  4697. {
  4698. unsigned long flags;
  4699. spin_lock_irqsave(&h->passthru_count_lock, flags);
  4700. if (h->passthru_count <= 0) {
  4701. spin_unlock_irqrestore(&h->passthru_count_lock, flags);
  4702. /* not expecting to get here. */
  4703. dev_warn(&h->pdev->dev, "Bug detected, passthru_count seems to be incorrect.\n");
  4704. return;
  4705. }
  4706. h->passthru_count--;
  4707. spin_unlock_irqrestore(&h->passthru_count_lock, flags);
  4708. }
  4709. /*
  4710. * ioctl
  4711. */
  4712. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
  4713. {
  4714. struct ctlr_info *h;
  4715. void __user *argp = (void __user *)arg;
  4716. int rc;
  4717. h = sdev_to_hba(dev);
  4718. switch (cmd) {
  4719. case CCISS_DEREGDISK:
  4720. case CCISS_REGNEWDISK:
  4721. case CCISS_REGNEWD:
  4722. hpsa_scan_start(h->scsi_host);
  4723. return 0;
  4724. case CCISS_GETPCIINFO:
  4725. return hpsa_getpciinfo_ioctl(h, argp);
  4726. case CCISS_GETDRIVVER:
  4727. return hpsa_getdrivver_ioctl(h, argp);
  4728. case CCISS_PASSTHRU:
  4729. if (increment_passthru_count(h))
  4730. return -EAGAIN;
  4731. rc = hpsa_passthru_ioctl(h, argp);
  4732. decrement_passthru_count(h);
  4733. return rc;
  4734. case CCISS_BIG_PASSTHRU:
  4735. if (increment_passthru_count(h))
  4736. return -EAGAIN;
  4737. rc = hpsa_big_passthru_ioctl(h, argp);
  4738. decrement_passthru_count(h);
  4739. return rc;
  4740. default:
  4741. return -ENOTTY;
  4742. }
  4743. }
  4744. static int hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
  4745. u8 reset_type)
  4746. {
  4747. struct CommandList *c;
  4748. c = cmd_alloc(h);
  4749. if (!c)
  4750. return -ENOMEM;
  4751. /* fill_cmd can't fail here, no data buffer to map */
  4752. (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
  4753. RAID_CTLR_LUNID, TYPE_MSG);
  4754. c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
  4755. c->waiting = NULL;
  4756. enqueue_cmd_and_start_io(h, c);
  4757. /* Don't wait for completion, the reset won't complete. Don't free
  4758. * the command either. This is the last command we will send before
  4759. * re-initializing everything, so it doesn't matter and won't leak.
  4760. */
  4761. return 0;
  4762. }
  4763. static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  4764. void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
  4765. int cmd_type)
  4766. {
  4767. int pci_dir = XFER_NONE;
  4768. struct CommandList *a; /* for commands to be aborted */
  4769. c->cmd_type = CMD_IOCTL_PEND;
  4770. c->Header.ReplyQueue = 0;
  4771. if (buff != NULL && size > 0) {
  4772. c->Header.SGList = 1;
  4773. c->Header.SGTotal = 1;
  4774. } else {
  4775. c->Header.SGList = 0;
  4776. c->Header.SGTotal = 0;
  4777. }
  4778. c->Header.Tag.lower = c->busaddr;
  4779. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  4780. c->Request.Type.Type = cmd_type;
  4781. if (cmd_type == TYPE_CMD) {
  4782. switch (cmd) {
  4783. case HPSA_INQUIRY:
  4784. /* are we trying to read a vital product page */
  4785. if (page_code & VPD_PAGE) {
  4786. c->Request.CDB[1] = 0x01;
  4787. c->Request.CDB[2] = (page_code & 0xff);
  4788. }
  4789. c->Request.CDBLen = 6;
  4790. c->Request.Type.Attribute = ATTR_SIMPLE;
  4791. c->Request.Type.Direction = XFER_READ;
  4792. c->Request.Timeout = 0;
  4793. c->Request.CDB[0] = HPSA_INQUIRY;
  4794. c->Request.CDB[4] = size & 0xFF;
  4795. break;
  4796. case HPSA_REPORT_LOG:
  4797. case HPSA_REPORT_PHYS:
  4798. /* Talking to controller so It's a physical command
  4799. mode = 00 target = 0. Nothing to write.
  4800. */
  4801. c->Request.CDBLen = 12;
  4802. c->Request.Type.Attribute = ATTR_SIMPLE;
  4803. c->Request.Type.Direction = XFER_READ;
  4804. c->Request.Timeout = 0;
  4805. c->Request.CDB[0] = cmd;
  4806. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  4807. c->Request.CDB[7] = (size >> 16) & 0xFF;
  4808. c->Request.CDB[8] = (size >> 8) & 0xFF;
  4809. c->Request.CDB[9] = size & 0xFF;
  4810. break;
  4811. case HPSA_CACHE_FLUSH:
  4812. c->Request.CDBLen = 12;
  4813. c->Request.Type.Attribute = ATTR_SIMPLE;
  4814. c->Request.Type.Direction = XFER_WRITE;
  4815. c->Request.Timeout = 0;
  4816. c->Request.CDB[0] = BMIC_WRITE;
  4817. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  4818. c->Request.CDB[7] = (size >> 8) & 0xFF;
  4819. c->Request.CDB[8] = size & 0xFF;
  4820. break;
  4821. case TEST_UNIT_READY:
  4822. c->Request.CDBLen = 6;
  4823. c->Request.Type.Attribute = ATTR_SIMPLE;
  4824. c->Request.Type.Direction = XFER_NONE;
  4825. c->Request.Timeout = 0;
  4826. break;
  4827. case HPSA_GET_RAID_MAP:
  4828. c->Request.CDBLen = 12;
  4829. c->Request.Type.Attribute = ATTR_SIMPLE;
  4830. c->Request.Type.Direction = XFER_READ;
  4831. c->Request.Timeout = 0;
  4832. c->Request.CDB[0] = HPSA_CISS_READ;
  4833. c->Request.CDB[1] = cmd;
  4834. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  4835. c->Request.CDB[7] = (size >> 16) & 0xFF;
  4836. c->Request.CDB[8] = (size >> 8) & 0xFF;
  4837. c->Request.CDB[9] = size & 0xFF;
  4838. break;
  4839. case BMIC_SENSE_CONTROLLER_PARAMETERS:
  4840. c->Request.CDBLen = 10;
  4841. c->Request.Type.Attribute = ATTR_SIMPLE;
  4842. c->Request.Type.Direction = XFER_READ;
  4843. c->Request.Timeout = 0;
  4844. c->Request.CDB[0] = BMIC_READ;
  4845. c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
  4846. c->Request.CDB[7] = (size >> 16) & 0xFF;
  4847. c->Request.CDB[8] = (size >> 8) & 0xFF;
  4848. break;
  4849. default:
  4850. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  4851. BUG();
  4852. return -1;
  4853. }
  4854. } else if (cmd_type == TYPE_MSG) {
  4855. switch (cmd) {
  4856. case HPSA_DEVICE_RESET_MSG:
  4857. c->Request.CDBLen = 16;
  4858. c->Request.Type.Type = 1; /* It is a MSG not a CMD */
  4859. c->Request.Type.Attribute = ATTR_SIMPLE;
  4860. c->Request.Type.Direction = XFER_NONE;
  4861. c->Request.Timeout = 0; /* Don't time out */
  4862. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  4863. c->Request.CDB[0] = cmd;
  4864. c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
  4865. /* If bytes 4-7 are zero, it means reset the */
  4866. /* LunID device */
  4867. c->Request.CDB[4] = 0x00;
  4868. c->Request.CDB[5] = 0x00;
  4869. c->Request.CDB[6] = 0x00;
  4870. c->Request.CDB[7] = 0x00;
  4871. break;
  4872. case HPSA_ABORT_MSG:
  4873. a = buff; /* point to command to be aborted */
  4874. dev_dbg(&h->pdev->dev, "Abort Tag:0x%08x:%08x using request Tag:0x%08x:%08x\n",
  4875. a->Header.Tag.upper, a->Header.Tag.lower,
  4876. c->Header.Tag.upper, c->Header.Tag.lower);
  4877. c->Request.CDBLen = 16;
  4878. c->Request.Type.Type = TYPE_MSG;
  4879. c->Request.Type.Attribute = ATTR_SIMPLE;
  4880. c->Request.Type.Direction = XFER_WRITE;
  4881. c->Request.Timeout = 0; /* Don't time out */
  4882. c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
  4883. c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
  4884. c->Request.CDB[2] = 0x00; /* reserved */
  4885. c->Request.CDB[3] = 0x00; /* reserved */
  4886. /* Tag to abort goes in CDB[4]-CDB[11] */
  4887. c->Request.CDB[4] = a->Header.Tag.lower & 0xFF;
  4888. c->Request.CDB[5] = (a->Header.Tag.lower >> 8) & 0xFF;
  4889. c->Request.CDB[6] = (a->Header.Tag.lower >> 16) & 0xFF;
  4890. c->Request.CDB[7] = (a->Header.Tag.lower >> 24) & 0xFF;
  4891. c->Request.CDB[8] = a->Header.Tag.upper & 0xFF;
  4892. c->Request.CDB[9] = (a->Header.Tag.upper >> 8) & 0xFF;
  4893. c->Request.CDB[10] = (a->Header.Tag.upper >> 16) & 0xFF;
  4894. c->Request.CDB[11] = (a->Header.Tag.upper >> 24) & 0xFF;
  4895. c->Request.CDB[12] = 0x00; /* reserved */
  4896. c->Request.CDB[13] = 0x00; /* reserved */
  4897. c->Request.CDB[14] = 0x00; /* reserved */
  4898. c->Request.CDB[15] = 0x00; /* reserved */
  4899. break;
  4900. default:
  4901. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  4902. cmd);
  4903. BUG();
  4904. }
  4905. } else {
  4906. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  4907. BUG();
  4908. }
  4909. switch (c->Request.Type.Direction) {
  4910. case XFER_READ:
  4911. pci_dir = PCI_DMA_FROMDEVICE;
  4912. break;
  4913. case XFER_WRITE:
  4914. pci_dir = PCI_DMA_TODEVICE;
  4915. break;
  4916. case XFER_NONE:
  4917. pci_dir = PCI_DMA_NONE;
  4918. break;
  4919. default:
  4920. pci_dir = PCI_DMA_BIDIRECTIONAL;
  4921. }
  4922. if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
  4923. return -1;
  4924. return 0;
  4925. }
  4926. /*
  4927. * Map (physical) PCI mem into (virtual) kernel space
  4928. */
  4929. static void __iomem *remap_pci_mem(ulong base, ulong size)
  4930. {
  4931. ulong page_base = ((ulong) base) & PAGE_MASK;
  4932. ulong page_offs = ((ulong) base) - page_base;
  4933. void __iomem *page_remapped = ioremap_nocache(page_base,
  4934. page_offs + size);
  4935. return page_remapped ? (page_remapped + page_offs) : NULL;
  4936. }
  4937. /* Takes cmds off the submission queue and sends them to the hardware,
  4938. * then puts them on the queue of cmds waiting for completion.
  4939. * Assumes h->lock is held
  4940. */
  4941. static void start_io(struct ctlr_info *h, unsigned long *flags)
  4942. {
  4943. struct CommandList *c;
  4944. while (!list_empty(&h->reqQ)) {
  4945. c = list_entry(h->reqQ.next, struct CommandList, list);
  4946. /* can't do anything if fifo is full */
  4947. if ((h->access.fifo_full(h))) {
  4948. h->fifo_recently_full = 1;
  4949. dev_warn(&h->pdev->dev, "fifo full\n");
  4950. break;
  4951. }
  4952. h->fifo_recently_full = 0;
  4953. /* Get the first entry from the Request Q */
  4954. removeQ(c);
  4955. h->Qdepth--;
  4956. /* Put job onto the completed Q */
  4957. addQ(&h->cmpQ, c);
  4958. /* Must increment commands_outstanding before unlocking
  4959. * and submitting to avoid race checking for fifo full
  4960. * condition.
  4961. */
  4962. h->commands_outstanding++;
  4963. /* Tell the controller execute command */
  4964. spin_unlock_irqrestore(&h->lock, *flags);
  4965. h->access.submit_command(h, c);
  4966. spin_lock_irqsave(&h->lock, *flags);
  4967. }
  4968. }
  4969. static void lock_and_start_io(struct ctlr_info *h)
  4970. {
  4971. unsigned long flags;
  4972. spin_lock_irqsave(&h->lock, flags);
  4973. start_io(h, &flags);
  4974. spin_unlock_irqrestore(&h->lock, flags);
  4975. }
  4976. static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
  4977. {
  4978. return h->access.command_completed(h, q);
  4979. }
  4980. static inline bool interrupt_pending(struct ctlr_info *h)
  4981. {
  4982. return h->access.intr_pending(h);
  4983. }
  4984. static inline long interrupt_not_for_us(struct ctlr_info *h)
  4985. {
  4986. return (h->access.intr_pending(h) == 0) ||
  4987. (h->interrupts_enabled == 0);
  4988. }
  4989. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  4990. u32 raw_tag)
  4991. {
  4992. if (unlikely(tag_index >= h->nr_cmds)) {
  4993. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  4994. return 1;
  4995. }
  4996. return 0;
  4997. }
  4998. static inline void finish_cmd(struct CommandList *c)
  4999. {
  5000. unsigned long flags;
  5001. int io_may_be_stalled = 0;
  5002. struct ctlr_info *h = c->h;
  5003. spin_lock_irqsave(&h->lock, flags);
  5004. removeQ(c);
  5005. /*
  5006. * Check for possibly stalled i/o.
  5007. *
  5008. * If a fifo_full condition is encountered, requests will back up
  5009. * in h->reqQ. This queue is only emptied out by start_io which is
  5010. * only called when a new i/o request comes in. If no i/o's are
  5011. * forthcoming, the i/o's in h->reqQ can get stuck. So we call
  5012. * start_io from here if we detect such a danger.
  5013. *
  5014. * Normally, we shouldn't hit this case, but pounding on the
  5015. * CCISS_PASSTHRU ioctl can provoke it. Only call start_io if
  5016. * commands_outstanding is low. We want to avoid calling
  5017. * start_io from in here as much as possible, and esp. don't
  5018. * want to get in a cycle where we call start_io every time
  5019. * through here.
  5020. */
  5021. if (unlikely(h->fifo_recently_full) &&
  5022. h->commands_outstanding < 5)
  5023. io_may_be_stalled = 1;
  5024. spin_unlock_irqrestore(&h->lock, flags);
  5025. dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
  5026. if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
  5027. || c->cmd_type == CMD_IOACCEL2))
  5028. complete_scsi_command(c);
  5029. else if (c->cmd_type == CMD_IOCTL_PEND)
  5030. complete(c->waiting);
  5031. if (unlikely(io_may_be_stalled))
  5032. lock_and_start_io(h);
  5033. }
  5034. static inline u32 hpsa_tag_contains_index(u32 tag)
  5035. {
  5036. return tag & DIRECT_LOOKUP_BIT;
  5037. }
  5038. static inline u32 hpsa_tag_to_index(u32 tag)
  5039. {
  5040. return tag >> DIRECT_LOOKUP_SHIFT;
  5041. }
  5042. static inline u32 hpsa_tag_discard_error_bits(struct ctlr_info *h, u32 tag)
  5043. {
  5044. #define HPSA_PERF_ERROR_BITS ((1 << DIRECT_LOOKUP_SHIFT) - 1)
  5045. #define HPSA_SIMPLE_ERROR_BITS 0x03
  5046. if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
  5047. return tag & ~HPSA_SIMPLE_ERROR_BITS;
  5048. return tag & ~HPSA_PERF_ERROR_BITS;
  5049. }
  5050. /* process completion of an indexed ("direct lookup") command */
  5051. static inline void process_indexed_cmd(struct ctlr_info *h,
  5052. u32 raw_tag)
  5053. {
  5054. u32 tag_index;
  5055. struct CommandList *c;
  5056. tag_index = hpsa_tag_to_index(raw_tag);
  5057. if (!bad_tag(h, tag_index, raw_tag)) {
  5058. c = h->cmd_pool + tag_index;
  5059. finish_cmd(c);
  5060. }
  5061. }
  5062. /* process completion of a non-indexed command */
  5063. static inline void process_nonindexed_cmd(struct ctlr_info *h,
  5064. u32 raw_tag)
  5065. {
  5066. u32 tag;
  5067. struct CommandList *c = NULL;
  5068. unsigned long flags;
  5069. tag = hpsa_tag_discard_error_bits(h, raw_tag);
  5070. spin_lock_irqsave(&h->lock, flags);
  5071. list_for_each_entry(c, &h->cmpQ, list) {
  5072. if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
  5073. spin_unlock_irqrestore(&h->lock, flags);
  5074. finish_cmd(c);
  5075. return;
  5076. }
  5077. }
  5078. spin_unlock_irqrestore(&h->lock, flags);
  5079. bad_tag(h, h->nr_cmds + 1, raw_tag);
  5080. }
  5081. /* Some controllers, like p400, will give us one interrupt
  5082. * after a soft reset, even if we turned interrupts off.
  5083. * Only need to check for this in the hpsa_xxx_discard_completions
  5084. * functions.
  5085. */
  5086. static int ignore_bogus_interrupt(struct ctlr_info *h)
  5087. {
  5088. if (likely(!reset_devices))
  5089. return 0;
  5090. if (likely(h->interrupts_enabled))
  5091. return 0;
  5092. dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
  5093. "(known firmware bug.) Ignoring.\n");
  5094. return 1;
  5095. }
  5096. /*
  5097. * Convert &h->q[x] (passed to interrupt handlers) back to h.
  5098. * Relies on (h-q[x] == x) being true for x such that
  5099. * 0 <= x < MAX_REPLY_QUEUES.
  5100. */
  5101. static struct ctlr_info *queue_to_hba(u8 *queue)
  5102. {
  5103. return container_of((queue - *queue), struct ctlr_info, q[0]);
  5104. }
  5105. static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
  5106. {
  5107. struct ctlr_info *h = queue_to_hba(queue);
  5108. u8 q = *(u8 *) queue;
  5109. u32 raw_tag;
  5110. if (ignore_bogus_interrupt(h))
  5111. return IRQ_NONE;
  5112. if (interrupt_not_for_us(h))
  5113. return IRQ_NONE;
  5114. h->last_intr_timestamp = get_jiffies_64();
  5115. while (interrupt_pending(h)) {
  5116. raw_tag = get_next_completion(h, q);
  5117. while (raw_tag != FIFO_EMPTY)
  5118. raw_tag = next_command(h, q);
  5119. }
  5120. return IRQ_HANDLED;
  5121. }
  5122. static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
  5123. {
  5124. struct ctlr_info *h = queue_to_hba(queue);
  5125. u32 raw_tag;
  5126. u8 q = *(u8 *) queue;
  5127. if (ignore_bogus_interrupt(h))
  5128. return IRQ_NONE;
  5129. h->last_intr_timestamp = get_jiffies_64();
  5130. raw_tag = get_next_completion(h, q);
  5131. while (raw_tag != FIFO_EMPTY)
  5132. raw_tag = next_command(h, q);
  5133. return IRQ_HANDLED;
  5134. }
  5135. static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
  5136. {
  5137. struct ctlr_info *h = queue_to_hba((u8 *) queue);
  5138. u32 raw_tag;
  5139. u8 q = *(u8 *) queue;
  5140. if (interrupt_not_for_us(h))
  5141. return IRQ_NONE;
  5142. h->last_intr_timestamp = get_jiffies_64();
  5143. while (interrupt_pending(h)) {
  5144. raw_tag = get_next_completion(h, q);
  5145. while (raw_tag != FIFO_EMPTY) {
  5146. if (likely(hpsa_tag_contains_index(raw_tag)))
  5147. process_indexed_cmd(h, raw_tag);
  5148. else
  5149. process_nonindexed_cmd(h, raw_tag);
  5150. raw_tag = next_command(h, q);
  5151. }
  5152. }
  5153. return IRQ_HANDLED;
  5154. }
  5155. static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
  5156. {
  5157. struct ctlr_info *h = queue_to_hba(queue);
  5158. u32 raw_tag;
  5159. u8 q = *(u8 *) queue;
  5160. h->last_intr_timestamp = get_jiffies_64();
  5161. raw_tag = get_next_completion(h, q);
  5162. while (raw_tag != FIFO_EMPTY) {
  5163. if (likely(hpsa_tag_contains_index(raw_tag)))
  5164. process_indexed_cmd(h, raw_tag);
  5165. else
  5166. process_nonindexed_cmd(h, raw_tag);
  5167. raw_tag = next_command(h, q);
  5168. }
  5169. return IRQ_HANDLED;
  5170. }
  5171. /* Send a message CDB to the firmware. Careful, this only works
  5172. * in simple mode, not performant mode due to the tag lookup.
  5173. * We only ever use this immediately after a controller reset.
  5174. */
  5175. static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  5176. unsigned char type)
  5177. {
  5178. struct Command {
  5179. struct CommandListHeader CommandHeader;
  5180. struct RequestBlock Request;
  5181. struct ErrDescriptor ErrorDescriptor;
  5182. };
  5183. struct Command *cmd;
  5184. static const size_t cmd_sz = sizeof(*cmd) +
  5185. sizeof(cmd->ErrorDescriptor);
  5186. dma_addr_t paddr64;
  5187. uint32_t paddr32, tag;
  5188. void __iomem *vaddr;
  5189. int i, err;
  5190. vaddr = pci_ioremap_bar(pdev, 0);
  5191. if (vaddr == NULL)
  5192. return -ENOMEM;
  5193. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  5194. * CCISS commands, so they must be allocated from the lower 4GiB of
  5195. * memory.
  5196. */
  5197. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  5198. if (err) {
  5199. iounmap(vaddr);
  5200. return -ENOMEM;
  5201. }
  5202. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  5203. if (cmd == NULL) {
  5204. iounmap(vaddr);
  5205. return -ENOMEM;
  5206. }
  5207. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  5208. * although there's no guarantee, we assume that the address is at
  5209. * least 4-byte aligned (most likely, it's page-aligned).
  5210. */
  5211. paddr32 = paddr64;
  5212. cmd->CommandHeader.ReplyQueue = 0;
  5213. cmd->CommandHeader.SGList = 0;
  5214. cmd->CommandHeader.SGTotal = 0;
  5215. cmd->CommandHeader.Tag.lower = paddr32;
  5216. cmd->CommandHeader.Tag.upper = 0;
  5217. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  5218. cmd->Request.CDBLen = 16;
  5219. cmd->Request.Type.Type = TYPE_MSG;
  5220. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  5221. cmd->Request.Type.Direction = XFER_NONE;
  5222. cmd->Request.Timeout = 0; /* Don't time out */
  5223. cmd->Request.CDB[0] = opcode;
  5224. cmd->Request.CDB[1] = type;
  5225. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  5226. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
  5227. cmd->ErrorDescriptor.Addr.upper = 0;
  5228. cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
  5229. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  5230. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  5231. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  5232. if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr32)
  5233. break;
  5234. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  5235. }
  5236. iounmap(vaddr);
  5237. /* we leak the DMA buffer here ... no choice since the controller could
  5238. * still complete the command.
  5239. */
  5240. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  5241. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  5242. opcode, type);
  5243. return -ETIMEDOUT;
  5244. }
  5245. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  5246. if (tag & HPSA_ERROR_BIT) {
  5247. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  5248. opcode, type);
  5249. return -EIO;
  5250. }
  5251. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  5252. opcode, type);
  5253. return 0;
  5254. }
  5255. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  5256. static int hpsa_controller_hard_reset(struct pci_dev *pdev,
  5257. void * __iomem vaddr, u32 use_doorbell)
  5258. {
  5259. u16 pmcsr;
  5260. int pos;
  5261. if (use_doorbell) {
  5262. /* For everything after the P600, the PCI power state method
  5263. * of resetting the controller doesn't work, so we have this
  5264. * other way using the doorbell register.
  5265. */
  5266. dev_info(&pdev->dev, "using doorbell to reset controller\n");
  5267. writel(use_doorbell, vaddr + SA5_DOORBELL);
  5268. /* PMC hardware guys tell us we need a 10 second delay after
  5269. * doorbell reset and before any attempt to talk to the board
  5270. * at all to ensure that this actually works and doesn't fall
  5271. * over in some weird corner cases.
  5272. */
  5273. msleep(10000);
  5274. } else { /* Try to do it the PCI power state way */
  5275. /* Quoting from the Open CISS Specification: "The Power
  5276. * Management Control/Status Register (CSR) controls the power
  5277. * state of the device. The normal operating state is D0,
  5278. * CSR=00h. The software off state is D3, CSR=03h. To reset
  5279. * the controller, place the interface device in D3 then to D0,
  5280. * this causes a secondary PCI reset which will reset the
  5281. * controller." */
  5282. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  5283. if (pos == 0) {
  5284. dev_err(&pdev->dev,
  5285. "hpsa_reset_controller: "
  5286. "PCI PM not supported\n");
  5287. return -ENODEV;
  5288. }
  5289. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  5290. /* enter the D3hot power management state */
  5291. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  5292. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  5293. pmcsr |= PCI_D3hot;
  5294. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  5295. msleep(500);
  5296. /* enter the D0 power management state */
  5297. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  5298. pmcsr |= PCI_D0;
  5299. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  5300. /*
  5301. * The P600 requires a small delay when changing states.
  5302. * Otherwise we may think the board did not reset and we bail.
  5303. * This for kdump only and is particular to the P600.
  5304. */
  5305. msleep(500);
  5306. }
  5307. return 0;
  5308. }
  5309. static void init_driver_version(char *driver_version, int len)
  5310. {
  5311. memset(driver_version, 0, len);
  5312. strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
  5313. }
  5314. static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
  5315. {
  5316. char *driver_version;
  5317. int i, size = sizeof(cfgtable->driver_version);
  5318. driver_version = kmalloc(size, GFP_KERNEL);
  5319. if (!driver_version)
  5320. return -ENOMEM;
  5321. init_driver_version(driver_version, size);
  5322. for (i = 0; i < size; i++)
  5323. writeb(driver_version[i], &cfgtable->driver_version[i]);
  5324. kfree(driver_version);
  5325. return 0;
  5326. }
  5327. static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
  5328. unsigned char *driver_ver)
  5329. {
  5330. int i;
  5331. for (i = 0; i < sizeof(cfgtable->driver_version); i++)
  5332. driver_ver[i] = readb(&cfgtable->driver_version[i]);
  5333. }
  5334. static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
  5335. {
  5336. char *driver_ver, *old_driver_ver;
  5337. int rc, size = sizeof(cfgtable->driver_version);
  5338. old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
  5339. if (!old_driver_ver)
  5340. return -ENOMEM;
  5341. driver_ver = old_driver_ver + size;
  5342. /* After a reset, the 32 bytes of "driver version" in the cfgtable
  5343. * should have been changed, otherwise we know the reset failed.
  5344. */
  5345. init_driver_version(old_driver_ver, size);
  5346. read_driver_ver_from_cfgtable(cfgtable, driver_ver);
  5347. rc = !memcmp(driver_ver, old_driver_ver, size);
  5348. kfree(old_driver_ver);
  5349. return rc;
  5350. }
  5351. /* This does a hard reset of the controller using PCI power management
  5352. * states or the using the doorbell register.
  5353. */
  5354. static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev)
  5355. {
  5356. u64 cfg_offset;
  5357. u32 cfg_base_addr;
  5358. u64 cfg_base_addr_index;
  5359. void __iomem *vaddr;
  5360. unsigned long paddr;
  5361. u32 misc_fw_support;
  5362. int rc;
  5363. struct CfgTable __iomem *cfgtable;
  5364. u32 use_doorbell;
  5365. u32 board_id;
  5366. u16 command_register;
  5367. /* For controllers as old as the P600, this is very nearly
  5368. * the same thing as
  5369. *
  5370. * pci_save_state(pci_dev);
  5371. * pci_set_power_state(pci_dev, PCI_D3hot);
  5372. * pci_set_power_state(pci_dev, PCI_D0);
  5373. * pci_restore_state(pci_dev);
  5374. *
  5375. * For controllers newer than the P600, the pci power state
  5376. * method of resetting doesn't work so we have another way
  5377. * using the doorbell register.
  5378. */
  5379. rc = hpsa_lookup_board_id(pdev, &board_id);
  5380. if (rc < 0 || !ctlr_is_resettable(board_id)) {
  5381. dev_warn(&pdev->dev, "Not resetting device.\n");
  5382. return -ENODEV;
  5383. }
  5384. /* if controller is soft- but not hard resettable... */
  5385. if (!ctlr_is_hard_resettable(board_id))
  5386. return -ENOTSUPP; /* try soft reset later. */
  5387. /* Save the PCI command register */
  5388. pci_read_config_word(pdev, 4, &command_register);
  5389. pci_save_state(pdev);
  5390. /* find the first memory BAR, so we can find the cfg table */
  5391. rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
  5392. if (rc)
  5393. return rc;
  5394. vaddr = remap_pci_mem(paddr, 0x250);
  5395. if (!vaddr)
  5396. return -ENOMEM;
  5397. /* find cfgtable in order to check if reset via doorbell is supported */
  5398. rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
  5399. &cfg_base_addr_index, &cfg_offset);
  5400. if (rc)
  5401. goto unmap_vaddr;
  5402. cfgtable = remap_pci_mem(pci_resource_start(pdev,
  5403. cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
  5404. if (!cfgtable) {
  5405. rc = -ENOMEM;
  5406. goto unmap_vaddr;
  5407. }
  5408. rc = write_driver_ver_to_cfgtable(cfgtable);
  5409. if (rc)
  5410. goto unmap_vaddr;
  5411. /* If reset via doorbell register is supported, use that.
  5412. * There are two such methods. Favor the newest method.
  5413. */
  5414. misc_fw_support = readl(&cfgtable->misc_fw_support);
  5415. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
  5416. if (use_doorbell) {
  5417. use_doorbell = DOORBELL_CTLR_RESET2;
  5418. } else {
  5419. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
  5420. if (use_doorbell) {
  5421. dev_warn(&pdev->dev, "Soft reset not supported. "
  5422. "Firmware update is required.\n");
  5423. rc = -ENOTSUPP; /* try soft reset */
  5424. goto unmap_cfgtable;
  5425. }
  5426. }
  5427. rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
  5428. if (rc)
  5429. goto unmap_cfgtable;
  5430. pci_restore_state(pdev);
  5431. pci_write_config_word(pdev, 4, command_register);
  5432. /* Some devices (notably the HP Smart Array 5i Controller)
  5433. need a little pause here */
  5434. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  5435. rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
  5436. if (rc) {
  5437. dev_warn(&pdev->dev,
  5438. "failed waiting for board to become ready "
  5439. "after hard reset\n");
  5440. goto unmap_cfgtable;
  5441. }
  5442. rc = controller_reset_failed(vaddr);
  5443. if (rc < 0)
  5444. goto unmap_cfgtable;
  5445. if (rc) {
  5446. dev_warn(&pdev->dev, "Unable to successfully reset "
  5447. "controller. Will try soft reset.\n");
  5448. rc = -ENOTSUPP;
  5449. } else {
  5450. dev_info(&pdev->dev, "board ready after hard reset.\n");
  5451. }
  5452. unmap_cfgtable:
  5453. iounmap(cfgtable);
  5454. unmap_vaddr:
  5455. iounmap(vaddr);
  5456. return rc;
  5457. }
  5458. /*
  5459. * We cannot read the structure directly, for portability we must use
  5460. * the io functions.
  5461. * This is for debug only.
  5462. */
  5463. static void print_cfg_table(struct device *dev, struct CfgTable *tb)
  5464. {
  5465. #ifdef HPSA_DEBUG
  5466. int i;
  5467. char temp_name[17];
  5468. dev_info(dev, "Controller Configuration information\n");
  5469. dev_info(dev, "------------------------------------\n");
  5470. for (i = 0; i < 4; i++)
  5471. temp_name[i] = readb(&(tb->Signature[i]));
  5472. temp_name[4] = '\0';
  5473. dev_info(dev, " Signature = %s\n", temp_name);
  5474. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  5475. dev_info(dev, " Transport methods supported = 0x%x\n",
  5476. readl(&(tb->TransportSupport)));
  5477. dev_info(dev, " Transport methods active = 0x%x\n",
  5478. readl(&(tb->TransportActive)));
  5479. dev_info(dev, " Requested transport Method = 0x%x\n",
  5480. readl(&(tb->HostWrite.TransportRequest)));
  5481. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  5482. readl(&(tb->HostWrite.CoalIntDelay)));
  5483. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  5484. readl(&(tb->HostWrite.CoalIntCount)));
  5485. dev_info(dev, " Max outstanding commands = 0x%d\n",
  5486. readl(&(tb->CmdsOutMax)));
  5487. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  5488. for (i = 0; i < 16; i++)
  5489. temp_name[i] = readb(&(tb->ServerName[i]));
  5490. temp_name[16] = '\0';
  5491. dev_info(dev, " Server Name = %s\n", temp_name);
  5492. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  5493. readl(&(tb->HeartBeat)));
  5494. #endif /* HPSA_DEBUG */
  5495. }
  5496. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  5497. {
  5498. int i, offset, mem_type, bar_type;
  5499. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  5500. return 0;
  5501. offset = 0;
  5502. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  5503. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  5504. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  5505. offset += 4;
  5506. else {
  5507. mem_type = pci_resource_flags(pdev, i) &
  5508. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  5509. switch (mem_type) {
  5510. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  5511. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  5512. offset += 4; /* 32 bit */
  5513. break;
  5514. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  5515. offset += 8;
  5516. break;
  5517. default: /* reserved in PCI 2.2 */
  5518. dev_warn(&pdev->dev,
  5519. "base address is invalid\n");
  5520. return -1;
  5521. break;
  5522. }
  5523. }
  5524. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  5525. return i + 1;
  5526. }
  5527. return -1;
  5528. }
  5529. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  5530. * controllers that are capable. If not, we use IO-APIC mode.
  5531. */
  5532. static void hpsa_interrupt_mode(struct ctlr_info *h)
  5533. {
  5534. #ifdef CONFIG_PCI_MSI
  5535. int err, i;
  5536. struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
  5537. for (i = 0; i < MAX_REPLY_QUEUES; i++) {
  5538. hpsa_msix_entries[i].vector = 0;
  5539. hpsa_msix_entries[i].entry = i;
  5540. }
  5541. /* Some boards advertise MSI but don't really support it */
  5542. if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
  5543. (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
  5544. goto default_int_mode;
  5545. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
  5546. dev_info(&h->pdev->dev, "MSIX\n");
  5547. h->msix_vector = MAX_REPLY_QUEUES;
  5548. if (h->msix_vector > num_online_cpus())
  5549. h->msix_vector = num_online_cpus();
  5550. err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
  5551. 1, h->msix_vector);
  5552. if (err < 0) {
  5553. dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
  5554. h->msix_vector = 0;
  5555. goto single_msi_mode;
  5556. } else if (err < h->msix_vector) {
  5557. dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
  5558. "available\n", err);
  5559. }
  5560. h->msix_vector = err;
  5561. for (i = 0; i < h->msix_vector; i++)
  5562. h->intr[i] = hpsa_msix_entries[i].vector;
  5563. return;
  5564. }
  5565. single_msi_mode:
  5566. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
  5567. dev_info(&h->pdev->dev, "MSI\n");
  5568. if (!pci_enable_msi(h->pdev))
  5569. h->msi_vector = 1;
  5570. else
  5571. dev_warn(&h->pdev->dev, "MSI init failed\n");
  5572. }
  5573. default_int_mode:
  5574. #endif /* CONFIG_PCI_MSI */
  5575. /* if we get here we're going to use the default interrupt mode */
  5576. h->intr[h->intr_mode] = h->pdev->irq;
  5577. }
  5578. static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
  5579. {
  5580. int i;
  5581. u32 subsystem_vendor_id, subsystem_device_id;
  5582. subsystem_vendor_id = pdev->subsystem_vendor;
  5583. subsystem_device_id = pdev->subsystem_device;
  5584. *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
  5585. subsystem_vendor_id;
  5586. for (i = 0; i < ARRAY_SIZE(products); i++)
  5587. if (*board_id == products[i].board_id)
  5588. return i;
  5589. if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
  5590. subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
  5591. !hpsa_allow_any) {
  5592. dev_warn(&pdev->dev, "unrecognized board ID: "
  5593. "0x%08x, ignoring.\n", *board_id);
  5594. return -ENODEV;
  5595. }
  5596. return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
  5597. }
  5598. static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  5599. unsigned long *memory_bar)
  5600. {
  5601. int i;
  5602. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  5603. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  5604. /* addressing mode bits already removed */
  5605. *memory_bar = pci_resource_start(pdev, i);
  5606. dev_dbg(&pdev->dev, "memory BAR = %lx\n",
  5607. *memory_bar);
  5608. return 0;
  5609. }
  5610. dev_warn(&pdev->dev, "no memory BAR found\n");
  5611. return -ENODEV;
  5612. }
  5613. static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
  5614. int wait_for_ready)
  5615. {
  5616. int i, iterations;
  5617. u32 scratchpad;
  5618. if (wait_for_ready)
  5619. iterations = HPSA_BOARD_READY_ITERATIONS;
  5620. else
  5621. iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
  5622. for (i = 0; i < iterations; i++) {
  5623. scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
  5624. if (wait_for_ready) {
  5625. if (scratchpad == HPSA_FIRMWARE_READY)
  5626. return 0;
  5627. } else {
  5628. if (scratchpad != HPSA_FIRMWARE_READY)
  5629. return 0;
  5630. }
  5631. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  5632. }
  5633. dev_warn(&pdev->dev, "board not ready, timed out.\n");
  5634. return -ENODEV;
  5635. }
  5636. static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
  5637. u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  5638. u64 *cfg_offset)
  5639. {
  5640. *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
  5641. *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
  5642. *cfg_base_addr &= (u32) 0x0000ffff;
  5643. *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
  5644. if (*cfg_base_addr_index == -1) {
  5645. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  5646. return -ENODEV;
  5647. }
  5648. return 0;
  5649. }
  5650. static int hpsa_find_cfgtables(struct ctlr_info *h)
  5651. {
  5652. u64 cfg_offset;
  5653. u32 cfg_base_addr;
  5654. u64 cfg_base_addr_index;
  5655. u32 trans_offset;
  5656. int rc;
  5657. rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  5658. &cfg_base_addr_index, &cfg_offset);
  5659. if (rc)
  5660. return rc;
  5661. h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
  5662. cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
  5663. if (!h->cfgtable)
  5664. return -ENOMEM;
  5665. rc = write_driver_ver_to_cfgtable(h->cfgtable);
  5666. if (rc)
  5667. return rc;
  5668. /* Find performant mode table. */
  5669. trans_offset = readl(&h->cfgtable->TransMethodOffset);
  5670. h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
  5671. cfg_base_addr_index)+cfg_offset+trans_offset,
  5672. sizeof(*h->transtable));
  5673. if (!h->transtable)
  5674. return -ENOMEM;
  5675. return 0;
  5676. }
  5677. static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
  5678. {
  5679. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  5680. /* Limit commands in memory limited kdump scenario. */
  5681. if (reset_devices && h->max_commands > 32)
  5682. h->max_commands = 32;
  5683. if (h->max_commands < 16) {
  5684. dev_warn(&h->pdev->dev, "Controller reports "
  5685. "max supported commands of %d, an obvious lie. "
  5686. "Using 16. Ensure that firmware is up to date.\n",
  5687. h->max_commands);
  5688. h->max_commands = 16;
  5689. }
  5690. }
  5691. /* Interrogate the hardware for some limits:
  5692. * max commands, max SG elements without chaining, and with chaining,
  5693. * SG chain block size, etc.
  5694. */
  5695. static void hpsa_find_board_params(struct ctlr_info *h)
  5696. {
  5697. hpsa_get_max_perf_mode_cmds(h);
  5698. h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
  5699. h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
  5700. h->fw_support = readl(&(h->cfgtable->misc_fw_support));
  5701. /*
  5702. * Limit in-command s/g elements to 32 save dma'able memory.
  5703. * Howvever spec says if 0, use 31
  5704. */
  5705. h->max_cmd_sg_entries = 31;
  5706. if (h->maxsgentries > 512) {
  5707. h->max_cmd_sg_entries = 32;
  5708. h->chainsize = h->maxsgentries - h->max_cmd_sg_entries + 1;
  5709. h->maxsgentries--; /* save one for chain pointer */
  5710. } else {
  5711. h->maxsgentries = 31; /* default to traditional values */
  5712. h->chainsize = 0;
  5713. }
  5714. /* Find out what task management functions are supported and cache */
  5715. h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
  5716. if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
  5717. dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
  5718. if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
  5719. dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
  5720. }
  5721. static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
  5722. {
  5723. if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
  5724. dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
  5725. return false;
  5726. }
  5727. return true;
  5728. }
  5729. static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
  5730. {
  5731. u32 driver_support;
  5732. driver_support = readl(&(h->cfgtable->driver_support));
  5733. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  5734. #ifdef CONFIG_X86
  5735. driver_support |= ENABLE_SCSI_PREFETCH;
  5736. #endif
  5737. driver_support |= ENABLE_UNIT_ATTN;
  5738. writel(driver_support, &(h->cfgtable->driver_support));
  5739. }
  5740. /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
  5741. * in a prefetch beyond physical memory.
  5742. */
  5743. static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
  5744. {
  5745. u32 dma_prefetch;
  5746. if (h->board_id != 0x3225103C)
  5747. return;
  5748. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  5749. dma_prefetch |= 0x8000;
  5750. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  5751. }
  5752. static void hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
  5753. {
  5754. int i;
  5755. u32 doorbell_value;
  5756. unsigned long flags;
  5757. /* wait until the clear_event_notify bit 6 is cleared by controller. */
  5758. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  5759. spin_lock_irqsave(&h->lock, flags);
  5760. doorbell_value = readl(h->vaddr + SA5_DOORBELL);
  5761. spin_unlock_irqrestore(&h->lock, flags);
  5762. if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
  5763. break;
  5764. /* delay and try again */
  5765. msleep(20);
  5766. }
  5767. }
  5768. static void hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
  5769. {
  5770. int i;
  5771. u32 doorbell_value;
  5772. unsigned long flags;
  5773. /* under certain very rare conditions, this can take awhile.
  5774. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  5775. * as we enter this code.)
  5776. */
  5777. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  5778. spin_lock_irqsave(&h->lock, flags);
  5779. doorbell_value = readl(h->vaddr + SA5_DOORBELL);
  5780. spin_unlock_irqrestore(&h->lock, flags);
  5781. if (!(doorbell_value & CFGTBL_ChangeReq))
  5782. break;
  5783. /* delay and try again */
  5784. usleep_range(10000, 20000);
  5785. }
  5786. }
  5787. static int hpsa_enter_simple_mode(struct ctlr_info *h)
  5788. {
  5789. u32 trans_support;
  5790. trans_support = readl(&(h->cfgtable->TransportSupport));
  5791. if (!(trans_support & SIMPLE_MODE))
  5792. return -ENOTSUPP;
  5793. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  5794. /* Update the field, and then ring the doorbell */
  5795. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  5796. writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
  5797. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  5798. hpsa_wait_for_mode_change_ack(h);
  5799. print_cfg_table(&h->pdev->dev, h->cfgtable);
  5800. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
  5801. goto error;
  5802. h->transMethod = CFGTBL_Trans_Simple;
  5803. return 0;
  5804. error:
  5805. dev_warn(&h->pdev->dev, "unable to get board into simple mode\n");
  5806. return -ENODEV;
  5807. }
  5808. static int hpsa_pci_init(struct ctlr_info *h)
  5809. {
  5810. int prod_index, err;
  5811. prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
  5812. if (prod_index < 0)
  5813. return -ENODEV;
  5814. h->product_name = products[prod_index].product_name;
  5815. h->access = *(products[prod_index].access);
  5816. pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
  5817. PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
  5818. err = pci_enable_device(h->pdev);
  5819. if (err) {
  5820. dev_warn(&h->pdev->dev, "unable to enable PCI device\n");
  5821. return err;
  5822. }
  5823. /* Enable bus mastering (pci_disable_device may disable this) */
  5824. pci_set_master(h->pdev);
  5825. err = pci_request_regions(h->pdev, HPSA);
  5826. if (err) {
  5827. dev_err(&h->pdev->dev,
  5828. "cannot obtain PCI resources, aborting\n");
  5829. return err;
  5830. }
  5831. hpsa_interrupt_mode(h);
  5832. err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
  5833. if (err)
  5834. goto err_out_free_res;
  5835. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  5836. if (!h->vaddr) {
  5837. err = -ENOMEM;
  5838. goto err_out_free_res;
  5839. }
  5840. err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
  5841. if (err)
  5842. goto err_out_free_res;
  5843. err = hpsa_find_cfgtables(h);
  5844. if (err)
  5845. goto err_out_free_res;
  5846. hpsa_find_board_params(h);
  5847. if (!hpsa_CISS_signature_present(h)) {
  5848. err = -ENODEV;
  5849. goto err_out_free_res;
  5850. }
  5851. hpsa_set_driver_support_bits(h);
  5852. hpsa_p600_dma_prefetch_quirk(h);
  5853. err = hpsa_enter_simple_mode(h);
  5854. if (err)
  5855. goto err_out_free_res;
  5856. return 0;
  5857. err_out_free_res:
  5858. if (h->transtable)
  5859. iounmap(h->transtable);
  5860. if (h->cfgtable)
  5861. iounmap(h->cfgtable);
  5862. if (h->vaddr)
  5863. iounmap(h->vaddr);
  5864. pci_disable_device(h->pdev);
  5865. pci_release_regions(h->pdev);
  5866. return err;
  5867. }
  5868. static void hpsa_hba_inquiry(struct ctlr_info *h)
  5869. {
  5870. int rc;
  5871. #define HBA_INQUIRY_BYTE_COUNT 64
  5872. h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
  5873. if (!h->hba_inquiry_data)
  5874. return;
  5875. rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
  5876. h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
  5877. if (rc != 0) {
  5878. kfree(h->hba_inquiry_data);
  5879. h->hba_inquiry_data = NULL;
  5880. }
  5881. }
  5882. static int hpsa_init_reset_devices(struct pci_dev *pdev)
  5883. {
  5884. int rc, i;
  5885. if (!reset_devices)
  5886. return 0;
  5887. /* kdump kernel is loading, we don't know in which state is
  5888. * the pci interface. The dev->enable_cnt is equal zero
  5889. * so we call enable+disable, wait a while and switch it on.
  5890. */
  5891. rc = pci_enable_device(pdev);
  5892. if (rc) {
  5893. dev_warn(&pdev->dev, "Failed to enable PCI device\n");
  5894. return -ENODEV;
  5895. }
  5896. pci_disable_device(pdev);
  5897. msleep(260); /* a randomly chosen number */
  5898. rc = pci_enable_device(pdev);
  5899. if (rc) {
  5900. dev_warn(&pdev->dev, "failed to enable device.\n");
  5901. return -ENODEV;
  5902. }
  5903. pci_set_master(pdev);
  5904. /* Reset the controller with a PCI power-cycle or via doorbell */
  5905. rc = hpsa_kdump_hard_reset_controller(pdev);
  5906. /* -ENOTSUPP here means we cannot reset the controller
  5907. * but it's already (and still) up and running in
  5908. * "performant mode". Or, it might be 640x, which can't reset
  5909. * due to concerns about shared bbwc between 6402/6404 pair.
  5910. */
  5911. if (rc) {
  5912. if (rc != -ENOTSUPP) /* just try to do the kdump anyhow. */
  5913. rc = -ENODEV;
  5914. goto out_disable;
  5915. }
  5916. /* Now try to get the controller to respond to a no-op */
  5917. dev_warn(&pdev->dev, "Waiting for controller to respond to no-op\n");
  5918. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  5919. if (hpsa_noop(pdev) == 0)
  5920. break;
  5921. else
  5922. dev_warn(&pdev->dev, "no-op failed%s\n",
  5923. (i < 11 ? "; re-trying" : ""));
  5924. }
  5925. out_disable:
  5926. pci_disable_device(pdev);
  5927. return rc;
  5928. }
  5929. static int hpsa_allocate_cmd_pool(struct ctlr_info *h)
  5930. {
  5931. h->cmd_pool_bits = kzalloc(
  5932. DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
  5933. sizeof(unsigned long), GFP_KERNEL);
  5934. h->cmd_pool = pci_alloc_consistent(h->pdev,
  5935. h->nr_cmds * sizeof(*h->cmd_pool),
  5936. &(h->cmd_pool_dhandle));
  5937. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  5938. h->nr_cmds * sizeof(*h->errinfo_pool),
  5939. &(h->errinfo_pool_dhandle));
  5940. if ((h->cmd_pool_bits == NULL)
  5941. || (h->cmd_pool == NULL)
  5942. || (h->errinfo_pool == NULL)) {
  5943. dev_err(&h->pdev->dev, "out of memory in %s", __func__);
  5944. return -ENOMEM;
  5945. }
  5946. return 0;
  5947. }
  5948. static void hpsa_free_cmd_pool(struct ctlr_info *h)
  5949. {
  5950. kfree(h->cmd_pool_bits);
  5951. if (h->cmd_pool)
  5952. pci_free_consistent(h->pdev,
  5953. h->nr_cmds * sizeof(struct CommandList),
  5954. h->cmd_pool, h->cmd_pool_dhandle);
  5955. if (h->ioaccel2_cmd_pool)
  5956. pci_free_consistent(h->pdev,
  5957. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
  5958. h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
  5959. if (h->errinfo_pool)
  5960. pci_free_consistent(h->pdev,
  5961. h->nr_cmds * sizeof(struct ErrorInfo),
  5962. h->errinfo_pool,
  5963. h->errinfo_pool_dhandle);
  5964. if (h->ioaccel_cmd_pool)
  5965. pci_free_consistent(h->pdev,
  5966. h->nr_cmds * sizeof(struct io_accel1_cmd),
  5967. h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
  5968. }
  5969. static void hpsa_irq_affinity_hints(struct ctlr_info *h)
  5970. {
  5971. int i, cpu, rc;
  5972. cpu = cpumask_first(cpu_online_mask);
  5973. for (i = 0; i < h->msix_vector; i++) {
  5974. rc = irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
  5975. cpu = cpumask_next(cpu, cpu_online_mask);
  5976. }
  5977. }
  5978. static int hpsa_request_irq(struct ctlr_info *h,
  5979. irqreturn_t (*msixhandler)(int, void *),
  5980. irqreturn_t (*intxhandler)(int, void *))
  5981. {
  5982. int rc, i;
  5983. /*
  5984. * initialize h->q[x] = x so that interrupt handlers know which
  5985. * queue to process.
  5986. */
  5987. for (i = 0; i < MAX_REPLY_QUEUES; i++)
  5988. h->q[i] = (u8) i;
  5989. if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
  5990. /* If performant mode and MSI-X, use multiple reply queues */
  5991. for (i = 0; i < h->msix_vector; i++)
  5992. rc = request_irq(h->intr[i], msixhandler,
  5993. 0, h->devname,
  5994. &h->q[i]);
  5995. hpsa_irq_affinity_hints(h);
  5996. } else {
  5997. /* Use single reply pool */
  5998. if (h->msix_vector > 0 || h->msi_vector) {
  5999. rc = request_irq(h->intr[h->intr_mode],
  6000. msixhandler, 0, h->devname,
  6001. &h->q[h->intr_mode]);
  6002. } else {
  6003. rc = request_irq(h->intr[h->intr_mode],
  6004. intxhandler, IRQF_SHARED, h->devname,
  6005. &h->q[h->intr_mode]);
  6006. }
  6007. }
  6008. if (rc) {
  6009. dev_err(&h->pdev->dev, "unable to get irq %d for %s\n",
  6010. h->intr[h->intr_mode], h->devname);
  6011. return -ENODEV;
  6012. }
  6013. return 0;
  6014. }
  6015. static int hpsa_kdump_soft_reset(struct ctlr_info *h)
  6016. {
  6017. if (hpsa_send_host_reset(h, RAID_CTLR_LUNID,
  6018. HPSA_RESET_TYPE_CONTROLLER)) {
  6019. dev_warn(&h->pdev->dev, "Resetting array controller failed.\n");
  6020. return -EIO;
  6021. }
  6022. dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
  6023. if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY)) {
  6024. dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
  6025. return -1;
  6026. }
  6027. dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
  6028. if (hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY)) {
  6029. dev_warn(&h->pdev->dev, "Board failed to become ready "
  6030. "after soft reset.\n");
  6031. return -1;
  6032. }
  6033. return 0;
  6034. }
  6035. static void free_irqs(struct ctlr_info *h)
  6036. {
  6037. int i;
  6038. if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
  6039. /* Single reply queue, only one irq to free */
  6040. i = h->intr_mode;
  6041. irq_set_affinity_hint(h->intr[i], NULL);
  6042. free_irq(h->intr[i], &h->q[i]);
  6043. return;
  6044. }
  6045. for (i = 0; i < h->msix_vector; i++) {
  6046. irq_set_affinity_hint(h->intr[i], NULL);
  6047. free_irq(h->intr[i], &h->q[i]);
  6048. }
  6049. }
  6050. static void hpsa_free_irqs_and_disable_msix(struct ctlr_info *h)
  6051. {
  6052. free_irqs(h);
  6053. #ifdef CONFIG_PCI_MSI
  6054. if (h->msix_vector) {
  6055. if (h->pdev->msix_enabled)
  6056. pci_disable_msix(h->pdev);
  6057. } else if (h->msi_vector) {
  6058. if (h->pdev->msi_enabled)
  6059. pci_disable_msi(h->pdev);
  6060. }
  6061. #endif /* CONFIG_PCI_MSI */
  6062. }
  6063. static void hpsa_free_reply_queues(struct ctlr_info *h)
  6064. {
  6065. int i;
  6066. for (i = 0; i < h->nreply_queues; i++) {
  6067. if (!h->reply_queue[i].head)
  6068. continue;
  6069. pci_free_consistent(h->pdev, h->reply_queue_size,
  6070. h->reply_queue[i].head, h->reply_queue[i].busaddr);
  6071. h->reply_queue[i].head = NULL;
  6072. h->reply_queue[i].busaddr = 0;
  6073. }
  6074. }
  6075. static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
  6076. {
  6077. hpsa_free_irqs_and_disable_msix(h);
  6078. hpsa_free_sg_chain_blocks(h);
  6079. hpsa_free_cmd_pool(h);
  6080. kfree(h->ioaccel1_blockFetchTable);
  6081. kfree(h->blockFetchTable);
  6082. hpsa_free_reply_queues(h);
  6083. if (h->vaddr)
  6084. iounmap(h->vaddr);
  6085. if (h->transtable)
  6086. iounmap(h->transtable);
  6087. if (h->cfgtable)
  6088. iounmap(h->cfgtable);
  6089. pci_disable_device(h->pdev);
  6090. pci_release_regions(h->pdev);
  6091. kfree(h);
  6092. }
  6093. /* Called when controller lockup detected. */
  6094. static void fail_all_cmds_on_list(struct ctlr_info *h, struct list_head *list)
  6095. {
  6096. struct CommandList *c = NULL;
  6097. assert_spin_locked(&h->lock);
  6098. /* Mark all outstanding commands as failed and complete them. */
  6099. while (!list_empty(list)) {
  6100. c = list_entry(list->next, struct CommandList, list);
  6101. c->err_info->CommandStatus = CMD_HARDWARE_ERR;
  6102. finish_cmd(c);
  6103. }
  6104. }
  6105. static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
  6106. {
  6107. int i, cpu;
  6108. cpu = cpumask_first(cpu_online_mask);
  6109. for (i = 0; i < num_online_cpus(); i++) {
  6110. u32 *lockup_detected;
  6111. lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
  6112. *lockup_detected = value;
  6113. cpu = cpumask_next(cpu, cpu_online_mask);
  6114. }
  6115. wmb(); /* be sure the per-cpu variables are out to memory */
  6116. }
  6117. static void controller_lockup_detected(struct ctlr_info *h)
  6118. {
  6119. unsigned long flags;
  6120. u32 lockup_detected;
  6121. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  6122. spin_lock_irqsave(&h->lock, flags);
  6123. lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  6124. if (!lockup_detected) {
  6125. /* no heartbeat, but controller gave us a zero. */
  6126. dev_warn(&h->pdev->dev,
  6127. "lockup detected but scratchpad register is zero\n");
  6128. lockup_detected = 0xffffffff;
  6129. }
  6130. set_lockup_detected_for_all_cpus(h, lockup_detected);
  6131. spin_unlock_irqrestore(&h->lock, flags);
  6132. dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x\n",
  6133. lockup_detected);
  6134. pci_disable_device(h->pdev);
  6135. spin_lock_irqsave(&h->lock, flags);
  6136. fail_all_cmds_on_list(h, &h->cmpQ);
  6137. fail_all_cmds_on_list(h, &h->reqQ);
  6138. spin_unlock_irqrestore(&h->lock, flags);
  6139. }
  6140. static void detect_controller_lockup(struct ctlr_info *h)
  6141. {
  6142. u64 now;
  6143. u32 heartbeat;
  6144. unsigned long flags;
  6145. now = get_jiffies_64();
  6146. /* If we've received an interrupt recently, we're ok. */
  6147. if (time_after64(h->last_intr_timestamp +
  6148. (h->heartbeat_sample_interval), now))
  6149. return;
  6150. /*
  6151. * If we've already checked the heartbeat recently, we're ok.
  6152. * This could happen if someone sends us a signal. We
  6153. * otherwise don't care about signals in this thread.
  6154. */
  6155. if (time_after64(h->last_heartbeat_timestamp +
  6156. (h->heartbeat_sample_interval), now))
  6157. return;
  6158. /* If heartbeat has not changed since we last looked, we're not ok. */
  6159. spin_lock_irqsave(&h->lock, flags);
  6160. heartbeat = readl(&h->cfgtable->HeartBeat);
  6161. spin_unlock_irqrestore(&h->lock, flags);
  6162. if (h->last_heartbeat == heartbeat) {
  6163. controller_lockup_detected(h);
  6164. return;
  6165. }
  6166. /* We're ok. */
  6167. h->last_heartbeat = heartbeat;
  6168. h->last_heartbeat_timestamp = now;
  6169. }
  6170. static void hpsa_ack_ctlr_events(struct ctlr_info *h)
  6171. {
  6172. int i;
  6173. char *event_type;
  6174. /* Clear the driver-requested rescan flag */
  6175. h->drv_req_rescan = 0;
  6176. /* Ask the controller to clear the events we're handling. */
  6177. if ((h->transMethod & (CFGTBL_Trans_io_accel1
  6178. | CFGTBL_Trans_io_accel2)) &&
  6179. (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
  6180. h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
  6181. if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
  6182. event_type = "state change";
  6183. if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
  6184. event_type = "configuration change";
  6185. /* Stop sending new RAID offload reqs via the IO accelerator */
  6186. scsi_block_requests(h->scsi_host);
  6187. for (i = 0; i < h->ndevices; i++)
  6188. h->dev[i]->offload_enabled = 0;
  6189. hpsa_drain_accel_commands(h);
  6190. /* Set 'accelerator path config change' bit */
  6191. dev_warn(&h->pdev->dev,
  6192. "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
  6193. h->events, event_type);
  6194. writel(h->events, &(h->cfgtable->clear_event_notify));
  6195. /* Set the "clear event notify field update" bit 6 */
  6196. writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
  6197. /* Wait until ctlr clears 'clear event notify field', bit 6 */
  6198. hpsa_wait_for_clear_event_notify_ack(h);
  6199. scsi_unblock_requests(h->scsi_host);
  6200. } else {
  6201. /* Acknowledge controller notification events. */
  6202. writel(h->events, &(h->cfgtable->clear_event_notify));
  6203. writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
  6204. hpsa_wait_for_clear_event_notify_ack(h);
  6205. #if 0
  6206. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  6207. hpsa_wait_for_mode_change_ack(h);
  6208. #endif
  6209. }
  6210. return;
  6211. }
  6212. /* Check a register on the controller to see if there are configuration
  6213. * changes (added/changed/removed logical drives, etc.) which mean that
  6214. * we should rescan the controller for devices.
  6215. * Also check flag for driver-initiated rescan.
  6216. */
  6217. static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
  6218. {
  6219. if (h->drv_req_rescan)
  6220. return 1;
  6221. if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
  6222. return 0;
  6223. h->events = readl(&(h->cfgtable->event_notify));
  6224. return h->events & RESCAN_REQUIRED_EVENT_BITS;
  6225. }
  6226. /*
  6227. * Check if any of the offline devices have become ready
  6228. */
  6229. static int hpsa_offline_devices_ready(struct ctlr_info *h)
  6230. {
  6231. unsigned long flags;
  6232. struct offline_device_entry *d;
  6233. struct list_head *this, *tmp;
  6234. spin_lock_irqsave(&h->offline_device_lock, flags);
  6235. list_for_each_safe(this, tmp, &h->offline_device_list) {
  6236. d = list_entry(this, struct offline_device_entry,
  6237. offline_list);
  6238. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  6239. if (!hpsa_volume_offline(h, d->scsi3addr)) {
  6240. spin_lock_irqsave(&h->offline_device_lock, flags);
  6241. list_del(&d->offline_list);
  6242. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  6243. return 1;
  6244. }
  6245. spin_lock_irqsave(&h->offline_device_lock, flags);
  6246. }
  6247. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  6248. return 0;
  6249. }
  6250. static void hpsa_monitor_ctlr_worker(struct work_struct *work)
  6251. {
  6252. unsigned long flags;
  6253. struct ctlr_info *h = container_of(to_delayed_work(work),
  6254. struct ctlr_info, monitor_ctlr_work);
  6255. detect_controller_lockup(h);
  6256. if (lockup_detected(h))
  6257. return;
  6258. if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
  6259. scsi_host_get(h->scsi_host);
  6260. h->drv_req_rescan = 0;
  6261. hpsa_ack_ctlr_events(h);
  6262. hpsa_scan_start(h->scsi_host);
  6263. scsi_host_put(h->scsi_host);
  6264. }
  6265. spin_lock_irqsave(&h->lock, flags);
  6266. if (h->remove_in_progress) {
  6267. spin_unlock_irqrestore(&h->lock, flags);
  6268. return;
  6269. }
  6270. schedule_delayed_work(&h->monitor_ctlr_work,
  6271. h->heartbeat_sample_interval);
  6272. spin_unlock_irqrestore(&h->lock, flags);
  6273. }
  6274. static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  6275. {
  6276. int dac, rc;
  6277. struct ctlr_info *h;
  6278. int try_soft_reset = 0;
  6279. unsigned long flags;
  6280. if (number_of_controllers == 0)
  6281. printk(KERN_INFO DRIVER_NAME "\n");
  6282. rc = hpsa_init_reset_devices(pdev);
  6283. if (rc) {
  6284. if (rc != -ENOTSUPP)
  6285. return rc;
  6286. /* If the reset fails in a particular way (it has no way to do
  6287. * a proper hard reset, so returns -ENOTSUPP) we can try to do
  6288. * a soft reset once we get the controller configured up to the
  6289. * point that it can accept a command.
  6290. */
  6291. try_soft_reset = 1;
  6292. rc = 0;
  6293. }
  6294. reinit_after_soft_reset:
  6295. /* Command structures must be aligned on a 32-byte boundary because
  6296. * the 5 lower bits of the address are used by the hardware. and by
  6297. * the driver. See comments in hpsa.h for more info.
  6298. */
  6299. BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
  6300. h = kzalloc(sizeof(*h), GFP_KERNEL);
  6301. if (!h)
  6302. return -ENOMEM;
  6303. h->pdev = pdev;
  6304. h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
  6305. INIT_LIST_HEAD(&h->cmpQ);
  6306. INIT_LIST_HEAD(&h->reqQ);
  6307. INIT_LIST_HEAD(&h->offline_device_list);
  6308. spin_lock_init(&h->lock);
  6309. spin_lock_init(&h->offline_device_lock);
  6310. spin_lock_init(&h->scan_lock);
  6311. spin_lock_init(&h->passthru_count_lock);
  6312. /* Allocate and clear per-cpu variable lockup_detected */
  6313. h->lockup_detected = alloc_percpu(u32);
  6314. if (!h->lockup_detected) {
  6315. rc = -ENOMEM;
  6316. goto clean1;
  6317. }
  6318. set_lockup_detected_for_all_cpus(h, 0);
  6319. rc = hpsa_pci_init(h);
  6320. if (rc != 0)
  6321. goto clean1;
  6322. sprintf(h->devname, HPSA "%d", number_of_controllers);
  6323. h->ctlr = number_of_controllers;
  6324. number_of_controllers++;
  6325. /* configure PCI DMA stuff */
  6326. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  6327. if (rc == 0) {
  6328. dac = 1;
  6329. } else {
  6330. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  6331. if (rc == 0) {
  6332. dac = 0;
  6333. } else {
  6334. dev_err(&pdev->dev, "no suitable DMA available\n");
  6335. goto clean1;
  6336. }
  6337. }
  6338. /* make sure the board interrupts are off */
  6339. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  6340. if (hpsa_request_irq(h, do_hpsa_intr_msi, do_hpsa_intr_intx))
  6341. goto clean2;
  6342. dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
  6343. h->devname, pdev->device,
  6344. h->intr[h->intr_mode], dac ? "" : " not");
  6345. if (hpsa_allocate_cmd_pool(h))
  6346. goto clean4;
  6347. if (hpsa_allocate_sg_chain_blocks(h))
  6348. goto clean4;
  6349. init_waitqueue_head(&h->scan_wait_queue);
  6350. h->scan_finished = 1; /* no scan currently in progress */
  6351. pci_set_drvdata(pdev, h);
  6352. h->ndevices = 0;
  6353. h->hba_mode_enabled = 0;
  6354. h->scsi_host = NULL;
  6355. spin_lock_init(&h->devlock);
  6356. hpsa_put_ctlr_into_performant_mode(h);
  6357. /* At this point, the controller is ready to take commands.
  6358. * Now, if reset_devices and the hard reset didn't work, try
  6359. * the soft reset and see if that works.
  6360. */
  6361. if (try_soft_reset) {
  6362. /* This is kind of gross. We may or may not get a completion
  6363. * from the soft reset command, and if we do, then the value
  6364. * from the fifo may or may not be valid. So, we wait 10 secs
  6365. * after the reset throwing away any completions we get during
  6366. * that time. Unregister the interrupt handler and register
  6367. * fake ones to scoop up any residual completions.
  6368. */
  6369. spin_lock_irqsave(&h->lock, flags);
  6370. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  6371. spin_unlock_irqrestore(&h->lock, flags);
  6372. free_irqs(h);
  6373. rc = hpsa_request_irq(h, hpsa_msix_discard_completions,
  6374. hpsa_intx_discard_completions);
  6375. if (rc) {
  6376. dev_warn(&h->pdev->dev, "Failed to request_irq after "
  6377. "soft reset.\n");
  6378. goto clean4;
  6379. }
  6380. rc = hpsa_kdump_soft_reset(h);
  6381. if (rc)
  6382. /* Neither hard nor soft reset worked, we're hosed. */
  6383. goto clean4;
  6384. dev_info(&h->pdev->dev, "Board READY.\n");
  6385. dev_info(&h->pdev->dev,
  6386. "Waiting for stale completions to drain.\n");
  6387. h->access.set_intr_mask(h, HPSA_INTR_ON);
  6388. msleep(10000);
  6389. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  6390. rc = controller_reset_failed(h->cfgtable);
  6391. if (rc)
  6392. dev_info(&h->pdev->dev,
  6393. "Soft reset appears to have failed.\n");
  6394. /* since the controller's reset, we have to go back and re-init
  6395. * everything. Easiest to just forget what we've done and do it
  6396. * all over again.
  6397. */
  6398. hpsa_undo_allocations_after_kdump_soft_reset(h);
  6399. try_soft_reset = 0;
  6400. if (rc)
  6401. /* don't go to clean4, we already unallocated */
  6402. return -ENODEV;
  6403. goto reinit_after_soft_reset;
  6404. }
  6405. /* Enable Accelerated IO path at driver layer */
  6406. h->acciopath_status = 1;
  6407. h->drv_req_rescan = 0;
  6408. /* Turn the interrupts on so we can service requests */
  6409. h->access.set_intr_mask(h, HPSA_INTR_ON);
  6410. hpsa_hba_inquiry(h);
  6411. hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
  6412. /* Monitor the controller for firmware lockups */
  6413. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
  6414. INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
  6415. schedule_delayed_work(&h->monitor_ctlr_work,
  6416. h->heartbeat_sample_interval);
  6417. return 0;
  6418. clean4:
  6419. hpsa_free_sg_chain_blocks(h);
  6420. hpsa_free_cmd_pool(h);
  6421. free_irqs(h);
  6422. clean2:
  6423. clean1:
  6424. if (h->lockup_detected)
  6425. free_percpu(h->lockup_detected);
  6426. kfree(h);
  6427. return rc;
  6428. }
  6429. static void hpsa_flush_cache(struct ctlr_info *h)
  6430. {
  6431. char *flush_buf;
  6432. struct CommandList *c;
  6433. /* Don't bother trying to flush the cache if locked up */
  6434. if (unlikely(lockup_detected(h)))
  6435. return;
  6436. flush_buf = kzalloc(4, GFP_KERNEL);
  6437. if (!flush_buf)
  6438. return;
  6439. c = cmd_special_alloc(h);
  6440. if (!c) {
  6441. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  6442. goto out_of_memory;
  6443. }
  6444. if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  6445. RAID_CTLR_LUNID, TYPE_CMD)) {
  6446. goto out;
  6447. }
  6448. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
  6449. if (c->err_info->CommandStatus != 0)
  6450. out:
  6451. dev_warn(&h->pdev->dev,
  6452. "error flushing cache on controller\n");
  6453. cmd_special_free(h, c);
  6454. out_of_memory:
  6455. kfree(flush_buf);
  6456. }
  6457. static void hpsa_shutdown(struct pci_dev *pdev)
  6458. {
  6459. struct ctlr_info *h;
  6460. h = pci_get_drvdata(pdev);
  6461. /* Turn board interrupts off and send the flush cache command
  6462. * sendcmd will turn off interrupt, and send the flush...
  6463. * To write all data in the battery backed cache to disks
  6464. */
  6465. hpsa_flush_cache(h);
  6466. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  6467. hpsa_free_irqs_and_disable_msix(h);
  6468. }
  6469. static void hpsa_free_device_info(struct ctlr_info *h)
  6470. {
  6471. int i;
  6472. for (i = 0; i < h->ndevices; i++)
  6473. kfree(h->dev[i]);
  6474. }
  6475. static void hpsa_remove_one(struct pci_dev *pdev)
  6476. {
  6477. struct ctlr_info *h;
  6478. unsigned long flags;
  6479. if (pci_get_drvdata(pdev) == NULL) {
  6480. dev_err(&pdev->dev, "unable to remove device\n");
  6481. return;
  6482. }
  6483. h = pci_get_drvdata(pdev);
  6484. /* Get rid of any controller monitoring work items */
  6485. spin_lock_irqsave(&h->lock, flags);
  6486. h->remove_in_progress = 1;
  6487. cancel_delayed_work(&h->monitor_ctlr_work);
  6488. spin_unlock_irqrestore(&h->lock, flags);
  6489. hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
  6490. hpsa_shutdown(pdev);
  6491. iounmap(h->vaddr);
  6492. iounmap(h->transtable);
  6493. iounmap(h->cfgtable);
  6494. hpsa_free_device_info(h);
  6495. hpsa_free_sg_chain_blocks(h);
  6496. pci_free_consistent(h->pdev,
  6497. h->nr_cmds * sizeof(struct CommandList),
  6498. h->cmd_pool, h->cmd_pool_dhandle);
  6499. pci_free_consistent(h->pdev,
  6500. h->nr_cmds * sizeof(struct ErrorInfo),
  6501. h->errinfo_pool, h->errinfo_pool_dhandle);
  6502. hpsa_free_reply_queues(h);
  6503. kfree(h->cmd_pool_bits);
  6504. kfree(h->blockFetchTable);
  6505. kfree(h->ioaccel1_blockFetchTable);
  6506. kfree(h->ioaccel2_blockFetchTable);
  6507. kfree(h->hba_inquiry_data);
  6508. pci_disable_device(pdev);
  6509. pci_release_regions(pdev);
  6510. free_percpu(h->lockup_detected);
  6511. kfree(h);
  6512. }
  6513. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  6514. __attribute__((unused)) pm_message_t state)
  6515. {
  6516. return -ENOSYS;
  6517. }
  6518. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  6519. {
  6520. return -ENOSYS;
  6521. }
  6522. static struct pci_driver hpsa_pci_driver = {
  6523. .name = HPSA,
  6524. .probe = hpsa_init_one,
  6525. .remove = hpsa_remove_one,
  6526. .id_table = hpsa_pci_device_id, /* id_table */
  6527. .shutdown = hpsa_shutdown,
  6528. .suspend = hpsa_suspend,
  6529. .resume = hpsa_resume,
  6530. };
  6531. /* Fill in bucket_map[], given nsgs (the max number of
  6532. * scatter gather elements supported) and bucket[],
  6533. * which is an array of 8 integers. The bucket[] array
  6534. * contains 8 different DMA transfer sizes (in 16
  6535. * byte increments) which the controller uses to fetch
  6536. * commands. This function fills in bucket_map[], which
  6537. * maps a given number of scatter gather elements to one of
  6538. * the 8 DMA transfer sizes. The point of it is to allow the
  6539. * controller to only do as much DMA as needed to fetch the
  6540. * command, with the DMA transfer size encoded in the lower
  6541. * bits of the command address.
  6542. */
  6543. static void calc_bucket_map(int bucket[], int num_buckets,
  6544. int nsgs, int min_blocks, int *bucket_map)
  6545. {
  6546. int i, j, b, size;
  6547. /* Note, bucket_map must have nsgs+1 entries. */
  6548. for (i = 0; i <= nsgs; i++) {
  6549. /* Compute size of a command with i SG entries */
  6550. size = i + min_blocks;
  6551. b = num_buckets; /* Assume the biggest bucket */
  6552. /* Find the bucket that is just big enough */
  6553. for (j = 0; j < num_buckets; j++) {
  6554. if (bucket[j] >= size) {
  6555. b = j;
  6556. break;
  6557. }
  6558. }
  6559. /* for a command with i SG entries, use bucket b. */
  6560. bucket_map[i] = b;
  6561. }
  6562. }
  6563. static void hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
  6564. {
  6565. int i;
  6566. unsigned long register_value;
  6567. unsigned long transMethod = CFGTBL_Trans_Performant |
  6568. (trans_support & CFGTBL_Trans_use_short_tags) |
  6569. CFGTBL_Trans_enable_directed_msix |
  6570. (trans_support & (CFGTBL_Trans_io_accel1 |
  6571. CFGTBL_Trans_io_accel2));
  6572. struct access_method access = SA5_performant_access;
  6573. /* This is a bit complicated. There are 8 registers on
  6574. * the controller which we write to to tell it 8 different
  6575. * sizes of commands which there may be. It's a way of
  6576. * reducing the DMA done to fetch each command. Encoded into
  6577. * each command's tag are 3 bits which communicate to the controller
  6578. * which of the eight sizes that command fits within. The size of
  6579. * each command depends on how many scatter gather entries there are.
  6580. * Each SG entry requires 16 bytes. The eight registers are programmed
  6581. * with the number of 16-byte blocks a command of that size requires.
  6582. * The smallest command possible requires 5 such 16 byte blocks.
  6583. * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
  6584. * blocks. Note, this only extends to the SG entries contained
  6585. * within the command block, and does not extend to chained blocks
  6586. * of SG elements. bft[] contains the eight values we write to
  6587. * the registers. They are not evenly distributed, but have more
  6588. * sizes for small commands, and fewer sizes for larger commands.
  6589. */
  6590. int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
  6591. #define MIN_IOACCEL2_BFT_ENTRY 5
  6592. #define HPSA_IOACCEL2_HEADER_SZ 4
  6593. int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
  6594. 13, 14, 15, 16, 17, 18, 19,
  6595. HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
  6596. BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
  6597. BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
  6598. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
  6599. 16 * MIN_IOACCEL2_BFT_ENTRY);
  6600. BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
  6601. BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
  6602. /* 5 = 1 s/g entry or 4k
  6603. * 6 = 2 s/g entry or 8k
  6604. * 8 = 4 s/g entry or 16k
  6605. * 10 = 6 s/g entry or 24k
  6606. */
  6607. /* If the controller supports either ioaccel method then
  6608. * we can also use the RAID stack submit path that does not
  6609. * perform the superfluous readl() after each command submission.
  6610. */
  6611. if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
  6612. access = SA5_performant_access_no_read;
  6613. /* Controller spec: zero out this buffer. */
  6614. for (i = 0; i < h->nreply_queues; i++)
  6615. memset(h->reply_queue[i].head, 0, h->reply_queue_size);
  6616. bft[7] = SG_ENTRIES_IN_CMD + 4;
  6617. calc_bucket_map(bft, ARRAY_SIZE(bft),
  6618. SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
  6619. for (i = 0; i < 8; i++)
  6620. writel(bft[i], &h->transtable->BlockFetch[i]);
  6621. /* size of controller ring buffer */
  6622. writel(h->max_commands, &h->transtable->RepQSize);
  6623. writel(h->nreply_queues, &h->transtable->RepQCount);
  6624. writel(0, &h->transtable->RepQCtrAddrLow32);
  6625. writel(0, &h->transtable->RepQCtrAddrHigh32);
  6626. for (i = 0; i < h->nreply_queues; i++) {
  6627. writel(0, &h->transtable->RepQAddr[i].upper);
  6628. writel(h->reply_queue[i].busaddr,
  6629. &h->transtable->RepQAddr[i].lower);
  6630. }
  6631. writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
  6632. writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
  6633. /*
  6634. * enable outbound interrupt coalescing in accelerator mode;
  6635. */
  6636. if (trans_support & CFGTBL_Trans_io_accel1) {
  6637. access = SA5_ioaccel_mode1_access;
  6638. writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
  6639. writel(4, &h->cfgtable->HostWrite.CoalIntCount);
  6640. } else {
  6641. if (trans_support & CFGTBL_Trans_io_accel2) {
  6642. access = SA5_ioaccel_mode2_access;
  6643. writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
  6644. writel(4, &h->cfgtable->HostWrite.CoalIntCount);
  6645. }
  6646. }
  6647. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  6648. hpsa_wait_for_mode_change_ack(h);
  6649. register_value = readl(&(h->cfgtable->TransportActive));
  6650. if (!(register_value & CFGTBL_Trans_Performant)) {
  6651. dev_warn(&h->pdev->dev, "unable to get board into"
  6652. " performant mode\n");
  6653. return;
  6654. }
  6655. /* Change the access methods to the performant access methods */
  6656. h->access = access;
  6657. h->transMethod = transMethod;
  6658. if (!((trans_support & CFGTBL_Trans_io_accel1) ||
  6659. (trans_support & CFGTBL_Trans_io_accel2)))
  6660. return;
  6661. if (trans_support & CFGTBL_Trans_io_accel1) {
  6662. /* Set up I/O accelerator mode */
  6663. for (i = 0; i < h->nreply_queues; i++) {
  6664. writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
  6665. h->reply_queue[i].current_entry =
  6666. readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
  6667. }
  6668. bft[7] = h->ioaccel_maxsg + 8;
  6669. calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
  6670. h->ioaccel1_blockFetchTable);
  6671. /* initialize all reply queue entries to unused */
  6672. for (i = 0; i < h->nreply_queues; i++)
  6673. memset(h->reply_queue[i].head,
  6674. (u8) IOACCEL_MODE1_REPLY_UNUSED,
  6675. h->reply_queue_size);
  6676. /* set all the constant fields in the accelerator command
  6677. * frames once at init time to save CPU cycles later.
  6678. */
  6679. for (i = 0; i < h->nr_cmds; i++) {
  6680. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
  6681. cp->function = IOACCEL1_FUNCTION_SCSIIO;
  6682. cp->err_info = (u32) (h->errinfo_pool_dhandle +
  6683. (i * sizeof(struct ErrorInfo)));
  6684. cp->err_info_len = sizeof(struct ErrorInfo);
  6685. cp->sgl_offset = IOACCEL1_SGLOFFSET;
  6686. cp->host_context_flags = IOACCEL1_HCFLAGS_CISS_FORMAT;
  6687. cp->timeout_sec = 0;
  6688. cp->ReplyQueue = 0;
  6689. cp->Tag.lower = (i << DIRECT_LOOKUP_SHIFT) |
  6690. DIRECT_LOOKUP_BIT;
  6691. cp->Tag.upper = 0;
  6692. cp->host_addr.lower =
  6693. (u32) (h->ioaccel_cmd_pool_dhandle +
  6694. (i * sizeof(struct io_accel1_cmd)));
  6695. cp->host_addr.upper = 0;
  6696. }
  6697. } else if (trans_support & CFGTBL_Trans_io_accel2) {
  6698. u64 cfg_offset, cfg_base_addr_index;
  6699. u32 bft2_offset, cfg_base_addr;
  6700. int rc;
  6701. rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  6702. &cfg_base_addr_index, &cfg_offset);
  6703. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
  6704. bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
  6705. calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
  6706. 4, h->ioaccel2_blockFetchTable);
  6707. bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
  6708. BUILD_BUG_ON(offsetof(struct CfgTable,
  6709. io_accel_request_size_offset) != 0xb8);
  6710. h->ioaccel2_bft2_regs =
  6711. remap_pci_mem(pci_resource_start(h->pdev,
  6712. cfg_base_addr_index) +
  6713. cfg_offset + bft2_offset,
  6714. ARRAY_SIZE(bft2) *
  6715. sizeof(*h->ioaccel2_bft2_regs));
  6716. for (i = 0; i < ARRAY_SIZE(bft2); i++)
  6717. writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
  6718. }
  6719. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  6720. hpsa_wait_for_mode_change_ack(h);
  6721. }
  6722. static int hpsa_alloc_ioaccel_cmd_and_bft(struct ctlr_info *h)
  6723. {
  6724. h->ioaccel_maxsg =
  6725. readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
  6726. if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
  6727. h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
  6728. /* Command structures must be aligned on a 128-byte boundary
  6729. * because the 7 lower bits of the address are used by the
  6730. * hardware.
  6731. */
  6732. BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
  6733. IOACCEL1_COMMANDLIST_ALIGNMENT);
  6734. h->ioaccel_cmd_pool =
  6735. pci_alloc_consistent(h->pdev,
  6736. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
  6737. &(h->ioaccel_cmd_pool_dhandle));
  6738. h->ioaccel1_blockFetchTable =
  6739. kmalloc(((h->ioaccel_maxsg + 1) *
  6740. sizeof(u32)), GFP_KERNEL);
  6741. if ((h->ioaccel_cmd_pool == NULL) ||
  6742. (h->ioaccel1_blockFetchTable == NULL))
  6743. goto clean_up;
  6744. memset(h->ioaccel_cmd_pool, 0,
  6745. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
  6746. return 0;
  6747. clean_up:
  6748. if (h->ioaccel_cmd_pool)
  6749. pci_free_consistent(h->pdev,
  6750. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
  6751. h->ioaccel_cmd_pool, h->ioaccel_cmd_pool_dhandle);
  6752. kfree(h->ioaccel1_blockFetchTable);
  6753. return 1;
  6754. }
  6755. static int ioaccel2_alloc_cmds_and_bft(struct ctlr_info *h)
  6756. {
  6757. /* Allocate ioaccel2 mode command blocks and block fetch table */
  6758. h->ioaccel_maxsg =
  6759. readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
  6760. if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
  6761. h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
  6762. BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
  6763. IOACCEL2_COMMANDLIST_ALIGNMENT);
  6764. h->ioaccel2_cmd_pool =
  6765. pci_alloc_consistent(h->pdev,
  6766. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
  6767. &(h->ioaccel2_cmd_pool_dhandle));
  6768. h->ioaccel2_blockFetchTable =
  6769. kmalloc(((h->ioaccel_maxsg + 1) *
  6770. sizeof(u32)), GFP_KERNEL);
  6771. if ((h->ioaccel2_cmd_pool == NULL) ||
  6772. (h->ioaccel2_blockFetchTable == NULL))
  6773. goto clean_up;
  6774. memset(h->ioaccel2_cmd_pool, 0,
  6775. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
  6776. return 0;
  6777. clean_up:
  6778. if (h->ioaccel2_cmd_pool)
  6779. pci_free_consistent(h->pdev,
  6780. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
  6781. h->ioaccel2_cmd_pool, h->ioaccel2_cmd_pool_dhandle);
  6782. kfree(h->ioaccel2_blockFetchTable);
  6783. return 1;
  6784. }
  6785. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
  6786. {
  6787. u32 trans_support;
  6788. unsigned long transMethod = CFGTBL_Trans_Performant |
  6789. CFGTBL_Trans_use_short_tags;
  6790. int i;
  6791. if (hpsa_simple_mode)
  6792. return;
  6793. trans_support = readl(&(h->cfgtable->TransportSupport));
  6794. if (!(trans_support & PERFORMANT_MODE))
  6795. return;
  6796. /* Check for I/O accelerator mode support */
  6797. if (trans_support & CFGTBL_Trans_io_accel1) {
  6798. transMethod |= CFGTBL_Trans_io_accel1 |
  6799. CFGTBL_Trans_enable_directed_msix;
  6800. if (hpsa_alloc_ioaccel_cmd_and_bft(h))
  6801. goto clean_up;
  6802. } else {
  6803. if (trans_support & CFGTBL_Trans_io_accel2) {
  6804. transMethod |= CFGTBL_Trans_io_accel2 |
  6805. CFGTBL_Trans_enable_directed_msix;
  6806. if (ioaccel2_alloc_cmds_and_bft(h))
  6807. goto clean_up;
  6808. }
  6809. }
  6810. h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
  6811. hpsa_get_max_perf_mode_cmds(h);
  6812. /* Performant mode ring buffer and supporting data structures */
  6813. h->reply_queue_size = h->max_commands * sizeof(u64);
  6814. for (i = 0; i < h->nreply_queues; i++) {
  6815. h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
  6816. h->reply_queue_size,
  6817. &(h->reply_queue[i].busaddr));
  6818. if (!h->reply_queue[i].head)
  6819. goto clean_up;
  6820. h->reply_queue[i].size = h->max_commands;
  6821. h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
  6822. h->reply_queue[i].current_entry = 0;
  6823. }
  6824. /* Need a block fetch table for performant mode */
  6825. h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
  6826. sizeof(u32)), GFP_KERNEL);
  6827. if (!h->blockFetchTable)
  6828. goto clean_up;
  6829. hpsa_enter_performant_mode(h, trans_support);
  6830. return;
  6831. clean_up:
  6832. hpsa_free_reply_queues(h);
  6833. kfree(h->blockFetchTable);
  6834. }
  6835. static int is_accelerated_cmd(struct CommandList *c)
  6836. {
  6837. return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
  6838. }
  6839. static void hpsa_drain_accel_commands(struct ctlr_info *h)
  6840. {
  6841. struct CommandList *c = NULL;
  6842. unsigned long flags;
  6843. int accel_cmds_out;
  6844. do { /* wait for all outstanding commands to drain out */
  6845. accel_cmds_out = 0;
  6846. spin_lock_irqsave(&h->lock, flags);
  6847. list_for_each_entry(c, &h->cmpQ, list)
  6848. accel_cmds_out += is_accelerated_cmd(c);
  6849. list_for_each_entry(c, &h->reqQ, list)
  6850. accel_cmds_out += is_accelerated_cmd(c);
  6851. spin_unlock_irqrestore(&h->lock, flags);
  6852. if (accel_cmds_out <= 0)
  6853. break;
  6854. msleep(100);
  6855. } while (1);
  6856. }
  6857. /*
  6858. * This is it. Register the PCI driver information for the cards we control
  6859. * the OS will call our registered routines when it finds one of our cards.
  6860. */
  6861. static int __init hpsa_init(void)
  6862. {
  6863. return pci_register_driver(&hpsa_pci_driver);
  6864. }
  6865. static void __exit hpsa_cleanup(void)
  6866. {
  6867. pci_unregister_driver(&hpsa_pci_driver);
  6868. }
  6869. static void __attribute__((unused)) verify_offsets(void)
  6870. {
  6871. #define VERIFY_OFFSET(member, offset) \
  6872. BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
  6873. VERIFY_OFFSET(structure_size, 0);
  6874. VERIFY_OFFSET(volume_blk_size, 4);
  6875. VERIFY_OFFSET(volume_blk_cnt, 8);
  6876. VERIFY_OFFSET(phys_blk_shift, 16);
  6877. VERIFY_OFFSET(parity_rotation_shift, 17);
  6878. VERIFY_OFFSET(strip_size, 18);
  6879. VERIFY_OFFSET(disk_starting_blk, 20);
  6880. VERIFY_OFFSET(disk_blk_cnt, 28);
  6881. VERIFY_OFFSET(data_disks_per_row, 36);
  6882. VERIFY_OFFSET(metadata_disks_per_row, 38);
  6883. VERIFY_OFFSET(row_cnt, 40);
  6884. VERIFY_OFFSET(layout_map_count, 42);
  6885. VERIFY_OFFSET(flags, 44);
  6886. VERIFY_OFFSET(dekindex, 46);
  6887. /* VERIFY_OFFSET(reserved, 48 */
  6888. VERIFY_OFFSET(data, 64);
  6889. #undef VERIFY_OFFSET
  6890. #define VERIFY_OFFSET(member, offset) \
  6891. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
  6892. VERIFY_OFFSET(IU_type, 0);
  6893. VERIFY_OFFSET(direction, 1);
  6894. VERIFY_OFFSET(reply_queue, 2);
  6895. /* VERIFY_OFFSET(reserved1, 3); */
  6896. VERIFY_OFFSET(scsi_nexus, 4);
  6897. VERIFY_OFFSET(Tag, 8);
  6898. VERIFY_OFFSET(cdb, 16);
  6899. VERIFY_OFFSET(cciss_lun, 32);
  6900. VERIFY_OFFSET(data_len, 40);
  6901. VERIFY_OFFSET(cmd_priority_task_attr, 44);
  6902. VERIFY_OFFSET(sg_count, 45);
  6903. /* VERIFY_OFFSET(reserved3 */
  6904. VERIFY_OFFSET(err_ptr, 48);
  6905. VERIFY_OFFSET(err_len, 56);
  6906. /* VERIFY_OFFSET(reserved4 */
  6907. VERIFY_OFFSET(sg, 64);
  6908. #undef VERIFY_OFFSET
  6909. #define VERIFY_OFFSET(member, offset) \
  6910. BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
  6911. VERIFY_OFFSET(dev_handle, 0x00);
  6912. VERIFY_OFFSET(reserved1, 0x02);
  6913. VERIFY_OFFSET(function, 0x03);
  6914. VERIFY_OFFSET(reserved2, 0x04);
  6915. VERIFY_OFFSET(err_info, 0x0C);
  6916. VERIFY_OFFSET(reserved3, 0x10);
  6917. VERIFY_OFFSET(err_info_len, 0x12);
  6918. VERIFY_OFFSET(reserved4, 0x13);
  6919. VERIFY_OFFSET(sgl_offset, 0x14);
  6920. VERIFY_OFFSET(reserved5, 0x15);
  6921. VERIFY_OFFSET(transfer_len, 0x1C);
  6922. VERIFY_OFFSET(reserved6, 0x20);
  6923. VERIFY_OFFSET(io_flags, 0x24);
  6924. VERIFY_OFFSET(reserved7, 0x26);
  6925. VERIFY_OFFSET(LUN, 0x34);
  6926. VERIFY_OFFSET(control, 0x3C);
  6927. VERIFY_OFFSET(CDB, 0x40);
  6928. VERIFY_OFFSET(reserved8, 0x50);
  6929. VERIFY_OFFSET(host_context_flags, 0x60);
  6930. VERIFY_OFFSET(timeout_sec, 0x62);
  6931. VERIFY_OFFSET(ReplyQueue, 0x64);
  6932. VERIFY_OFFSET(reserved9, 0x65);
  6933. VERIFY_OFFSET(Tag, 0x68);
  6934. VERIFY_OFFSET(host_addr, 0x70);
  6935. VERIFY_OFFSET(CISS_LUN, 0x78);
  6936. VERIFY_OFFSET(SG, 0x78 + 8);
  6937. #undef VERIFY_OFFSET
  6938. }
  6939. module_init(hpsa_init);
  6940. module_exit(hpsa_cleanup);