hpsa.c 280 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2016 Microsemi Corporation
  4. * Copyright 2014-2015 PMC-Sierra, Inc.
  5. * Copyright 2000,2009-2015 Hewlett-Packard Development Company, L.P.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more details.
  15. *
  16. * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/pci-aspm.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/fs.h>
  28. #include <linux/timer.h>
  29. #include <linux/init.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/compat.h>
  32. #include <linux/blktrace_api.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/completion.h>
  37. #include <linux/moduleparam.h>
  38. #include <scsi/scsi.h>
  39. #include <scsi/scsi_cmnd.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_tcq.h>
  43. #include <scsi/scsi_eh.h>
  44. #include <scsi/scsi_transport_sas.h>
  45. #include <scsi/scsi_dbg.h>
  46. #include <linux/cciss_ioctl.h>
  47. #include <linux/string.h>
  48. #include <linux/bitmap.h>
  49. #include <linux/atomic.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/percpu-defs.h>
  52. #include <linux/percpu.h>
  53. #include <asm/unaligned.h>
  54. #include <asm/div64.h>
  55. #include "hpsa_cmd.h"
  56. #include "hpsa.h"
  57. /*
  58. * HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.'
  59. * with an optional trailing '-' followed by a byte value (0-255).
  60. */
  61. #define HPSA_DRIVER_VERSION "3.4.16-0"
  62. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  63. #define HPSA "hpsa"
  64. /* How long to wait for CISS doorbell communication */
  65. #define CLEAR_EVENT_WAIT_INTERVAL 20 /* ms for each msleep() call */
  66. #define MODE_CHANGE_WAIT_INTERVAL 10 /* ms for each msleep() call */
  67. #define MAX_CLEAR_EVENT_WAIT 30000 /* times 20 ms = 600 s */
  68. #define MAX_MODE_CHANGE_WAIT 2000 /* times 10 ms = 20 s */
  69. #define MAX_IOCTL_CONFIG_WAIT 1000
  70. /*define how many times we will try a command because of bus resets */
  71. #define MAX_CMD_RETRIES 3
  72. /* Embedded module documentation macros - see modules.h */
  73. MODULE_AUTHOR("Hewlett-Packard Company");
  74. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  75. HPSA_DRIVER_VERSION);
  76. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  77. MODULE_VERSION(HPSA_DRIVER_VERSION);
  78. MODULE_LICENSE("GPL");
  79. static int hpsa_allow_any;
  80. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  81. MODULE_PARM_DESC(hpsa_allow_any,
  82. "Allow hpsa driver to access unknown HP Smart Array hardware");
  83. static int hpsa_simple_mode;
  84. module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
  85. MODULE_PARM_DESC(hpsa_simple_mode,
  86. "Use 'simple mode' rather than 'performant mode'");
  87. /* define the PCI info for the cards we can control */
  88. static const struct pci_device_id hpsa_pci_device_id[] = {
  89. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  90. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  91. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  92. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  93. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  94. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
  95. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
  96. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
  97. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
  98. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
  99. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
  100. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
  101. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
  102. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
  103. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
  104. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
  105. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
  106. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
  107. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
  108. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
  109. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
  110. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
  111. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
  112. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
  113. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
  114. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
  115. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
  116. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
  117. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
  118. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
  119. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
  120. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
  121. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
  122. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
  123. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
  124. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
  125. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
  126. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
  127. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
  128. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
  129. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0580},
  130. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0581},
  131. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0582},
  132. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0583},
  133. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0584},
  134. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0585},
  135. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
  136. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
  137. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
  138. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
  139. {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
  140. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  141. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  142. {0,}
  143. };
  144. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  145. /* board_id = Subsystem Device ID & Vendor ID
  146. * product = Marketing Name for the board
  147. * access = Address of the struct of function pointers
  148. */
  149. static struct board_type products[] = {
  150. {0x3241103C, "Smart Array P212", &SA5_access},
  151. {0x3243103C, "Smart Array P410", &SA5_access},
  152. {0x3245103C, "Smart Array P410i", &SA5_access},
  153. {0x3247103C, "Smart Array P411", &SA5_access},
  154. {0x3249103C, "Smart Array P812", &SA5_access},
  155. {0x324A103C, "Smart Array P712m", &SA5_access},
  156. {0x324B103C, "Smart Array P711m", &SA5_access},
  157. {0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
  158. {0x3350103C, "Smart Array P222", &SA5_access},
  159. {0x3351103C, "Smart Array P420", &SA5_access},
  160. {0x3352103C, "Smart Array P421", &SA5_access},
  161. {0x3353103C, "Smart Array P822", &SA5_access},
  162. {0x3354103C, "Smart Array P420i", &SA5_access},
  163. {0x3355103C, "Smart Array P220i", &SA5_access},
  164. {0x3356103C, "Smart Array P721m", &SA5_access},
  165. {0x1921103C, "Smart Array P830i", &SA5_access},
  166. {0x1922103C, "Smart Array P430", &SA5_access},
  167. {0x1923103C, "Smart Array P431", &SA5_access},
  168. {0x1924103C, "Smart Array P830", &SA5_access},
  169. {0x1926103C, "Smart Array P731m", &SA5_access},
  170. {0x1928103C, "Smart Array P230i", &SA5_access},
  171. {0x1929103C, "Smart Array P530", &SA5_access},
  172. {0x21BD103C, "Smart Array P244br", &SA5_access},
  173. {0x21BE103C, "Smart Array P741m", &SA5_access},
  174. {0x21BF103C, "Smart HBA H240ar", &SA5_access},
  175. {0x21C0103C, "Smart Array P440ar", &SA5_access},
  176. {0x21C1103C, "Smart Array P840ar", &SA5_access},
  177. {0x21C2103C, "Smart Array P440", &SA5_access},
  178. {0x21C3103C, "Smart Array P441", &SA5_access},
  179. {0x21C4103C, "Smart Array", &SA5_access},
  180. {0x21C5103C, "Smart Array P841", &SA5_access},
  181. {0x21C6103C, "Smart HBA H244br", &SA5_access},
  182. {0x21C7103C, "Smart HBA H240", &SA5_access},
  183. {0x21C8103C, "Smart HBA H241", &SA5_access},
  184. {0x21C9103C, "Smart Array", &SA5_access},
  185. {0x21CA103C, "Smart Array P246br", &SA5_access},
  186. {0x21CB103C, "Smart Array P840", &SA5_access},
  187. {0x21CC103C, "Smart Array", &SA5_access},
  188. {0x21CD103C, "Smart Array", &SA5_access},
  189. {0x21CE103C, "Smart HBA", &SA5_access},
  190. {0x05809005, "SmartHBA-SA", &SA5_access},
  191. {0x05819005, "SmartHBA-SA 8i", &SA5_access},
  192. {0x05829005, "SmartHBA-SA 8i8e", &SA5_access},
  193. {0x05839005, "SmartHBA-SA 8e", &SA5_access},
  194. {0x05849005, "SmartHBA-SA 16i", &SA5_access},
  195. {0x05859005, "SmartHBA-SA 4i4e", &SA5_access},
  196. {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
  197. {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
  198. {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
  199. {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
  200. {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
  201. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  202. };
  203. static struct scsi_transport_template *hpsa_sas_transport_template;
  204. static int hpsa_add_sas_host(struct ctlr_info *h);
  205. static void hpsa_delete_sas_host(struct ctlr_info *h);
  206. static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
  207. struct hpsa_scsi_dev_t *device);
  208. static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device);
  209. static struct hpsa_scsi_dev_t
  210. *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
  211. struct sas_rphy *rphy);
  212. #define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
  213. static const struct scsi_cmnd hpsa_cmd_busy;
  214. #define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
  215. static const struct scsi_cmnd hpsa_cmd_idle;
  216. static int number_of_controllers;
  217. static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
  218. static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
  219. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
  220. #ifdef CONFIG_COMPAT
  221. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
  222. void __user *arg);
  223. #endif
  224. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  225. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  226. static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
  227. static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
  228. struct scsi_cmnd *scmd);
  229. static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  230. void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
  231. int cmd_type);
  232. static void hpsa_free_cmd_pool(struct ctlr_info *h);
  233. #define VPD_PAGE (1 << 8)
  234. #define HPSA_SIMPLE_ERROR_BITS 0x03
  235. static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  236. static void hpsa_scan_start(struct Scsi_Host *);
  237. static int hpsa_scan_finished(struct Scsi_Host *sh,
  238. unsigned long elapsed_time);
  239. static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
  240. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  241. static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
  242. static int hpsa_slave_alloc(struct scsi_device *sdev);
  243. static int hpsa_slave_configure(struct scsi_device *sdev);
  244. static void hpsa_slave_destroy(struct scsi_device *sdev);
  245. static void hpsa_update_scsi_devices(struct ctlr_info *h);
  246. static int check_for_unit_attention(struct ctlr_info *h,
  247. struct CommandList *c);
  248. static void check_ioctl_unit_attention(struct ctlr_info *h,
  249. struct CommandList *c);
  250. /* performant mode helper functions */
  251. static void calc_bucket_map(int *bucket, int num_buckets,
  252. int nsgs, int min_blocks, u32 *bucket_map);
  253. static void hpsa_free_performant_mode(struct ctlr_info *h);
  254. static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
  255. static inline u32 next_command(struct ctlr_info *h, u8 q);
  256. static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
  257. u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  258. u64 *cfg_offset);
  259. static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  260. unsigned long *memory_bar);
  261. static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
  262. static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
  263. int wait_for_ready);
  264. static inline void finish_cmd(struct CommandList *c);
  265. static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
  266. #define BOARD_NOT_READY 0
  267. #define BOARD_READY 1
  268. static void hpsa_drain_accel_commands(struct ctlr_info *h);
  269. static void hpsa_flush_cache(struct ctlr_info *h);
  270. static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
  271. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  272. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
  273. static void hpsa_command_resubmit_worker(struct work_struct *work);
  274. static u32 lockup_detected(struct ctlr_info *h);
  275. static int detect_controller_lockup(struct ctlr_info *h);
  276. static void hpsa_disable_rld_caching(struct ctlr_info *h);
  277. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  278. struct ReportExtendedLUNdata *buf, int bufsize);
  279. static bool hpsa_vpd_page_supported(struct ctlr_info *h,
  280. unsigned char scsi3addr[], u8 page);
  281. static int hpsa_luns_changed(struct ctlr_info *h);
  282. static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
  283. struct hpsa_scsi_dev_t *dev,
  284. unsigned char *scsi3addr);
  285. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  286. {
  287. unsigned long *priv = shost_priv(sdev->host);
  288. return (struct ctlr_info *) *priv;
  289. }
  290. static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
  291. {
  292. unsigned long *priv = shost_priv(sh);
  293. return (struct ctlr_info *) *priv;
  294. }
  295. static inline bool hpsa_is_cmd_idle(struct CommandList *c)
  296. {
  297. return c->scsi_cmd == SCSI_CMD_IDLE;
  298. }
  299. static inline bool hpsa_is_pending_event(struct CommandList *c)
  300. {
  301. return c->abort_pending || c->reset_pending;
  302. }
  303. /* extract sense key, asc, and ascq from sense data. -1 means invalid. */
  304. static void decode_sense_data(const u8 *sense_data, int sense_data_len,
  305. u8 *sense_key, u8 *asc, u8 *ascq)
  306. {
  307. struct scsi_sense_hdr sshdr;
  308. bool rc;
  309. *sense_key = -1;
  310. *asc = -1;
  311. *ascq = -1;
  312. if (sense_data_len < 1)
  313. return;
  314. rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
  315. if (rc) {
  316. *sense_key = sshdr.sense_key;
  317. *asc = sshdr.asc;
  318. *ascq = sshdr.ascq;
  319. }
  320. }
  321. static int check_for_unit_attention(struct ctlr_info *h,
  322. struct CommandList *c)
  323. {
  324. u8 sense_key, asc, ascq;
  325. int sense_len;
  326. if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
  327. sense_len = sizeof(c->err_info->SenseInfo);
  328. else
  329. sense_len = c->err_info->SenseLen;
  330. decode_sense_data(c->err_info->SenseInfo, sense_len,
  331. &sense_key, &asc, &ascq);
  332. if (sense_key != UNIT_ATTENTION || asc == 0xff)
  333. return 0;
  334. switch (asc) {
  335. case STATE_CHANGED:
  336. dev_warn(&h->pdev->dev,
  337. "%s: a state change detected, command retried\n",
  338. h->devname);
  339. break;
  340. case LUN_FAILED:
  341. dev_warn(&h->pdev->dev,
  342. "%s: LUN failure detected\n", h->devname);
  343. break;
  344. case REPORT_LUNS_CHANGED:
  345. dev_warn(&h->pdev->dev,
  346. "%s: report LUN data changed\n", h->devname);
  347. /*
  348. * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
  349. * target (array) devices.
  350. */
  351. break;
  352. case POWER_OR_RESET:
  353. dev_warn(&h->pdev->dev,
  354. "%s: a power on or device reset detected\n",
  355. h->devname);
  356. break;
  357. case UNIT_ATTENTION_CLEARED:
  358. dev_warn(&h->pdev->dev,
  359. "%s: unit attention cleared by another initiator\n",
  360. h->devname);
  361. break;
  362. default:
  363. dev_warn(&h->pdev->dev,
  364. "%s: unknown unit attention detected\n",
  365. h->devname);
  366. break;
  367. }
  368. return 1;
  369. }
  370. static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
  371. {
  372. if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
  373. (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
  374. c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
  375. return 0;
  376. dev_warn(&h->pdev->dev, HPSA "device busy");
  377. return 1;
  378. }
  379. static u32 lockup_detected(struct ctlr_info *h);
  380. static ssize_t host_show_lockup_detected(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. int ld;
  384. struct ctlr_info *h;
  385. struct Scsi_Host *shost = class_to_shost(dev);
  386. h = shost_to_hba(shost);
  387. ld = lockup_detected(h);
  388. return sprintf(buf, "ld=%d\n", ld);
  389. }
  390. static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
  391. struct device_attribute *attr,
  392. const char *buf, size_t count)
  393. {
  394. int status, len;
  395. struct ctlr_info *h;
  396. struct Scsi_Host *shost = class_to_shost(dev);
  397. char tmpbuf[10];
  398. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  399. return -EACCES;
  400. len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
  401. strncpy(tmpbuf, buf, len);
  402. tmpbuf[len] = '\0';
  403. if (sscanf(tmpbuf, "%d", &status) != 1)
  404. return -EINVAL;
  405. h = shost_to_hba(shost);
  406. h->acciopath_status = !!status;
  407. dev_warn(&h->pdev->dev,
  408. "hpsa: HP SSD Smart Path %s via sysfs update.\n",
  409. h->acciopath_status ? "enabled" : "disabled");
  410. return count;
  411. }
  412. static ssize_t host_store_raid_offload_debug(struct device *dev,
  413. struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. int debug_level, len;
  417. struct ctlr_info *h;
  418. struct Scsi_Host *shost = class_to_shost(dev);
  419. char tmpbuf[10];
  420. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  421. return -EACCES;
  422. len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
  423. strncpy(tmpbuf, buf, len);
  424. tmpbuf[len] = '\0';
  425. if (sscanf(tmpbuf, "%d", &debug_level) != 1)
  426. return -EINVAL;
  427. if (debug_level < 0)
  428. debug_level = 0;
  429. h = shost_to_hba(shost);
  430. h->raid_offload_debug = debug_level;
  431. dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
  432. h->raid_offload_debug);
  433. return count;
  434. }
  435. static ssize_t host_store_rescan(struct device *dev,
  436. struct device_attribute *attr,
  437. const char *buf, size_t count)
  438. {
  439. struct ctlr_info *h;
  440. struct Scsi_Host *shost = class_to_shost(dev);
  441. h = shost_to_hba(shost);
  442. hpsa_scan_start(h->scsi_host);
  443. return count;
  444. }
  445. static ssize_t host_show_firmware_revision(struct device *dev,
  446. struct device_attribute *attr, char *buf)
  447. {
  448. struct ctlr_info *h;
  449. struct Scsi_Host *shost = class_to_shost(dev);
  450. unsigned char *fwrev;
  451. h = shost_to_hba(shost);
  452. if (!h->hba_inquiry_data)
  453. return 0;
  454. fwrev = &h->hba_inquiry_data[32];
  455. return snprintf(buf, 20, "%c%c%c%c\n",
  456. fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
  457. }
  458. static ssize_t host_show_commands_outstanding(struct device *dev,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. struct Scsi_Host *shost = class_to_shost(dev);
  462. struct ctlr_info *h = shost_to_hba(shost);
  463. return snprintf(buf, 20, "%d\n",
  464. atomic_read(&h->commands_outstanding));
  465. }
  466. static ssize_t host_show_transport_mode(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct ctlr_info *h;
  470. struct Scsi_Host *shost = class_to_shost(dev);
  471. h = shost_to_hba(shost);
  472. return snprintf(buf, 20, "%s\n",
  473. h->transMethod & CFGTBL_Trans_Performant ?
  474. "performant" : "simple");
  475. }
  476. static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
  477. struct device_attribute *attr, char *buf)
  478. {
  479. struct ctlr_info *h;
  480. struct Scsi_Host *shost = class_to_shost(dev);
  481. h = shost_to_hba(shost);
  482. return snprintf(buf, 30, "HP SSD Smart Path %s\n",
  483. (h->acciopath_status == 1) ? "enabled" : "disabled");
  484. }
  485. /* List of controllers which cannot be hard reset on kexec with reset_devices */
  486. static u32 unresettable_controller[] = {
  487. 0x324a103C, /* Smart Array P712m */
  488. 0x324b103C, /* Smart Array P711m */
  489. 0x3223103C, /* Smart Array P800 */
  490. 0x3234103C, /* Smart Array P400 */
  491. 0x3235103C, /* Smart Array P400i */
  492. 0x3211103C, /* Smart Array E200i */
  493. 0x3212103C, /* Smart Array E200 */
  494. 0x3213103C, /* Smart Array E200i */
  495. 0x3214103C, /* Smart Array E200i */
  496. 0x3215103C, /* Smart Array E200i */
  497. 0x3237103C, /* Smart Array E500 */
  498. 0x323D103C, /* Smart Array P700m */
  499. 0x40800E11, /* Smart Array 5i */
  500. 0x409C0E11, /* Smart Array 6400 */
  501. 0x409D0E11, /* Smart Array 6400 EM */
  502. 0x40700E11, /* Smart Array 5300 */
  503. 0x40820E11, /* Smart Array 532 */
  504. 0x40830E11, /* Smart Array 5312 */
  505. 0x409A0E11, /* Smart Array 641 */
  506. 0x409B0E11, /* Smart Array 642 */
  507. 0x40910E11, /* Smart Array 6i */
  508. };
  509. /* List of controllers which cannot even be soft reset */
  510. static u32 soft_unresettable_controller[] = {
  511. 0x40800E11, /* Smart Array 5i */
  512. 0x40700E11, /* Smart Array 5300 */
  513. 0x40820E11, /* Smart Array 532 */
  514. 0x40830E11, /* Smart Array 5312 */
  515. 0x409A0E11, /* Smart Array 641 */
  516. 0x409B0E11, /* Smart Array 642 */
  517. 0x40910E11, /* Smart Array 6i */
  518. /* Exclude 640x boards. These are two pci devices in one slot
  519. * which share a battery backed cache module. One controls the
  520. * cache, the other accesses the cache through the one that controls
  521. * it. If we reset the one controlling the cache, the other will
  522. * likely not be happy. Just forbid resetting this conjoined mess.
  523. * The 640x isn't really supported by hpsa anyway.
  524. */
  525. 0x409C0E11, /* Smart Array 6400 */
  526. 0x409D0E11, /* Smart Array 6400 EM */
  527. };
  528. static u32 needs_abort_tags_swizzled[] = {
  529. 0x323D103C, /* Smart Array P700m */
  530. 0x324a103C, /* Smart Array P712m */
  531. 0x324b103C, /* SmartArray P711m */
  532. };
  533. static int board_id_in_array(u32 a[], int nelems, u32 board_id)
  534. {
  535. int i;
  536. for (i = 0; i < nelems; i++)
  537. if (a[i] == board_id)
  538. return 1;
  539. return 0;
  540. }
  541. static int ctlr_is_hard_resettable(u32 board_id)
  542. {
  543. return !board_id_in_array(unresettable_controller,
  544. ARRAY_SIZE(unresettable_controller), board_id);
  545. }
  546. static int ctlr_is_soft_resettable(u32 board_id)
  547. {
  548. return !board_id_in_array(soft_unresettable_controller,
  549. ARRAY_SIZE(soft_unresettable_controller), board_id);
  550. }
  551. static int ctlr_is_resettable(u32 board_id)
  552. {
  553. return ctlr_is_hard_resettable(board_id) ||
  554. ctlr_is_soft_resettable(board_id);
  555. }
  556. static int ctlr_needs_abort_tags_swizzled(u32 board_id)
  557. {
  558. return board_id_in_array(needs_abort_tags_swizzled,
  559. ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
  560. }
  561. static ssize_t host_show_resettable(struct device *dev,
  562. struct device_attribute *attr, char *buf)
  563. {
  564. struct ctlr_info *h;
  565. struct Scsi_Host *shost = class_to_shost(dev);
  566. h = shost_to_hba(shost);
  567. return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
  568. }
  569. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  570. {
  571. return (scsi3addr[3] & 0xC0) == 0x40;
  572. }
  573. static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
  574. "1(+0)ADM", "UNKNOWN", "PHYS DRV"
  575. };
  576. #define HPSA_RAID_0 0
  577. #define HPSA_RAID_4 1
  578. #define HPSA_RAID_1 2 /* also used for RAID 10 */
  579. #define HPSA_RAID_5 3 /* also used for RAID 50 */
  580. #define HPSA_RAID_51 4
  581. #define HPSA_RAID_6 5 /* also used for RAID 60 */
  582. #define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
  583. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 2)
  584. #define PHYSICAL_DRIVE (ARRAY_SIZE(raid_label) - 1)
  585. static inline bool is_logical_device(struct hpsa_scsi_dev_t *device)
  586. {
  587. return !device->physical_device;
  588. }
  589. static ssize_t raid_level_show(struct device *dev,
  590. struct device_attribute *attr, char *buf)
  591. {
  592. ssize_t l = 0;
  593. unsigned char rlevel;
  594. struct ctlr_info *h;
  595. struct scsi_device *sdev;
  596. struct hpsa_scsi_dev_t *hdev;
  597. unsigned long flags;
  598. sdev = to_scsi_device(dev);
  599. h = sdev_to_hba(sdev);
  600. spin_lock_irqsave(&h->lock, flags);
  601. hdev = sdev->hostdata;
  602. if (!hdev) {
  603. spin_unlock_irqrestore(&h->lock, flags);
  604. return -ENODEV;
  605. }
  606. /* Is this even a logical drive? */
  607. if (!is_logical_device(hdev)) {
  608. spin_unlock_irqrestore(&h->lock, flags);
  609. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  610. return l;
  611. }
  612. rlevel = hdev->raid_level;
  613. spin_unlock_irqrestore(&h->lock, flags);
  614. if (rlevel > RAID_UNKNOWN)
  615. rlevel = RAID_UNKNOWN;
  616. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  617. return l;
  618. }
  619. static ssize_t lunid_show(struct device *dev,
  620. struct device_attribute *attr, char *buf)
  621. {
  622. struct ctlr_info *h;
  623. struct scsi_device *sdev;
  624. struct hpsa_scsi_dev_t *hdev;
  625. unsigned long flags;
  626. unsigned char lunid[8];
  627. sdev = to_scsi_device(dev);
  628. h = sdev_to_hba(sdev);
  629. spin_lock_irqsave(&h->lock, flags);
  630. hdev = sdev->hostdata;
  631. if (!hdev) {
  632. spin_unlock_irqrestore(&h->lock, flags);
  633. return -ENODEV;
  634. }
  635. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  636. spin_unlock_irqrestore(&h->lock, flags);
  637. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  638. lunid[0], lunid[1], lunid[2], lunid[3],
  639. lunid[4], lunid[5], lunid[6], lunid[7]);
  640. }
  641. static ssize_t unique_id_show(struct device *dev,
  642. struct device_attribute *attr, char *buf)
  643. {
  644. struct ctlr_info *h;
  645. struct scsi_device *sdev;
  646. struct hpsa_scsi_dev_t *hdev;
  647. unsigned long flags;
  648. unsigned char sn[16];
  649. sdev = to_scsi_device(dev);
  650. h = sdev_to_hba(sdev);
  651. spin_lock_irqsave(&h->lock, flags);
  652. hdev = sdev->hostdata;
  653. if (!hdev) {
  654. spin_unlock_irqrestore(&h->lock, flags);
  655. return -ENODEV;
  656. }
  657. memcpy(sn, hdev->device_id, sizeof(sn));
  658. spin_unlock_irqrestore(&h->lock, flags);
  659. return snprintf(buf, 16 * 2 + 2,
  660. "%02X%02X%02X%02X%02X%02X%02X%02X"
  661. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  662. sn[0], sn[1], sn[2], sn[3],
  663. sn[4], sn[5], sn[6], sn[7],
  664. sn[8], sn[9], sn[10], sn[11],
  665. sn[12], sn[13], sn[14], sn[15]);
  666. }
  667. static ssize_t sas_address_show(struct device *dev,
  668. struct device_attribute *attr, char *buf)
  669. {
  670. struct ctlr_info *h;
  671. struct scsi_device *sdev;
  672. struct hpsa_scsi_dev_t *hdev;
  673. unsigned long flags;
  674. u64 sas_address;
  675. sdev = to_scsi_device(dev);
  676. h = sdev_to_hba(sdev);
  677. spin_lock_irqsave(&h->lock, flags);
  678. hdev = sdev->hostdata;
  679. if (!hdev || is_logical_device(hdev) || !hdev->expose_device) {
  680. spin_unlock_irqrestore(&h->lock, flags);
  681. return -ENODEV;
  682. }
  683. sas_address = hdev->sas_address;
  684. spin_unlock_irqrestore(&h->lock, flags);
  685. return snprintf(buf, PAGE_SIZE, "0x%016llx\n", sas_address);
  686. }
  687. static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
  688. struct device_attribute *attr, char *buf)
  689. {
  690. struct ctlr_info *h;
  691. struct scsi_device *sdev;
  692. struct hpsa_scsi_dev_t *hdev;
  693. unsigned long flags;
  694. int offload_enabled;
  695. sdev = to_scsi_device(dev);
  696. h = sdev_to_hba(sdev);
  697. spin_lock_irqsave(&h->lock, flags);
  698. hdev = sdev->hostdata;
  699. if (!hdev) {
  700. spin_unlock_irqrestore(&h->lock, flags);
  701. return -ENODEV;
  702. }
  703. offload_enabled = hdev->offload_enabled;
  704. spin_unlock_irqrestore(&h->lock, flags);
  705. return snprintf(buf, 20, "%d\n", offload_enabled);
  706. }
  707. #define MAX_PATHS 8
  708. static ssize_t path_info_show(struct device *dev,
  709. struct device_attribute *attr, char *buf)
  710. {
  711. struct ctlr_info *h;
  712. struct scsi_device *sdev;
  713. struct hpsa_scsi_dev_t *hdev;
  714. unsigned long flags;
  715. int i;
  716. int output_len = 0;
  717. u8 box;
  718. u8 bay;
  719. u8 path_map_index = 0;
  720. char *active;
  721. unsigned char phys_connector[2];
  722. sdev = to_scsi_device(dev);
  723. h = sdev_to_hba(sdev);
  724. spin_lock_irqsave(&h->devlock, flags);
  725. hdev = sdev->hostdata;
  726. if (!hdev) {
  727. spin_unlock_irqrestore(&h->devlock, flags);
  728. return -ENODEV;
  729. }
  730. bay = hdev->bay;
  731. for (i = 0; i < MAX_PATHS; i++) {
  732. path_map_index = 1<<i;
  733. if (i == hdev->active_path_index)
  734. active = "Active";
  735. else if (hdev->path_map & path_map_index)
  736. active = "Inactive";
  737. else
  738. continue;
  739. output_len += scnprintf(buf + output_len,
  740. PAGE_SIZE - output_len,
  741. "[%d:%d:%d:%d] %20.20s ",
  742. h->scsi_host->host_no,
  743. hdev->bus, hdev->target, hdev->lun,
  744. scsi_device_type(hdev->devtype));
  745. if (hdev->devtype == TYPE_RAID || is_logical_device(hdev)) {
  746. output_len += scnprintf(buf + output_len,
  747. PAGE_SIZE - output_len,
  748. "%s\n", active);
  749. continue;
  750. }
  751. box = hdev->box[i];
  752. memcpy(&phys_connector, &hdev->phys_connector[i],
  753. sizeof(phys_connector));
  754. if (phys_connector[0] < '0')
  755. phys_connector[0] = '0';
  756. if (phys_connector[1] < '0')
  757. phys_connector[1] = '0';
  758. output_len += scnprintf(buf + output_len,
  759. PAGE_SIZE - output_len,
  760. "PORT: %.2s ",
  761. phys_connector);
  762. if ((hdev->devtype == TYPE_DISK || hdev->devtype == TYPE_ZBC) &&
  763. hdev->expose_device) {
  764. if (box == 0 || box == 0xFF) {
  765. output_len += scnprintf(buf + output_len,
  766. PAGE_SIZE - output_len,
  767. "BAY: %hhu %s\n",
  768. bay, active);
  769. } else {
  770. output_len += scnprintf(buf + output_len,
  771. PAGE_SIZE - output_len,
  772. "BOX: %hhu BAY: %hhu %s\n",
  773. box, bay, active);
  774. }
  775. } else if (box != 0 && box != 0xFF) {
  776. output_len += scnprintf(buf + output_len,
  777. PAGE_SIZE - output_len, "BOX: %hhu %s\n",
  778. box, active);
  779. } else
  780. output_len += scnprintf(buf + output_len,
  781. PAGE_SIZE - output_len, "%s\n", active);
  782. }
  783. spin_unlock_irqrestore(&h->devlock, flags);
  784. return output_len;
  785. }
  786. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  787. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  788. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  789. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  790. static DEVICE_ATTR(sas_address, S_IRUGO, sas_address_show, NULL);
  791. static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
  792. host_show_hp_ssd_smart_path_enabled, NULL);
  793. static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
  794. static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
  795. host_show_hp_ssd_smart_path_status,
  796. host_store_hp_ssd_smart_path_status);
  797. static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
  798. host_store_raid_offload_debug);
  799. static DEVICE_ATTR(firmware_revision, S_IRUGO,
  800. host_show_firmware_revision, NULL);
  801. static DEVICE_ATTR(commands_outstanding, S_IRUGO,
  802. host_show_commands_outstanding, NULL);
  803. static DEVICE_ATTR(transport_mode, S_IRUGO,
  804. host_show_transport_mode, NULL);
  805. static DEVICE_ATTR(resettable, S_IRUGO,
  806. host_show_resettable, NULL);
  807. static DEVICE_ATTR(lockup_detected, S_IRUGO,
  808. host_show_lockup_detected, NULL);
  809. static struct device_attribute *hpsa_sdev_attrs[] = {
  810. &dev_attr_raid_level,
  811. &dev_attr_lunid,
  812. &dev_attr_unique_id,
  813. &dev_attr_hp_ssd_smart_path_enabled,
  814. &dev_attr_path_info,
  815. &dev_attr_sas_address,
  816. NULL,
  817. };
  818. static struct device_attribute *hpsa_shost_attrs[] = {
  819. &dev_attr_rescan,
  820. &dev_attr_firmware_revision,
  821. &dev_attr_commands_outstanding,
  822. &dev_attr_transport_mode,
  823. &dev_attr_resettable,
  824. &dev_attr_hp_ssd_smart_path_status,
  825. &dev_attr_raid_offload_debug,
  826. &dev_attr_lockup_detected,
  827. NULL,
  828. };
  829. #define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
  830. HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
  831. static struct scsi_host_template hpsa_driver_template = {
  832. .module = THIS_MODULE,
  833. .name = HPSA,
  834. .proc_name = HPSA,
  835. .queuecommand = hpsa_scsi_queue_command,
  836. .scan_start = hpsa_scan_start,
  837. .scan_finished = hpsa_scan_finished,
  838. .change_queue_depth = hpsa_change_queue_depth,
  839. .this_id = -1,
  840. .use_clustering = ENABLE_CLUSTERING,
  841. .eh_abort_handler = hpsa_eh_abort_handler,
  842. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  843. .ioctl = hpsa_ioctl,
  844. .slave_alloc = hpsa_slave_alloc,
  845. .slave_configure = hpsa_slave_configure,
  846. .slave_destroy = hpsa_slave_destroy,
  847. #ifdef CONFIG_COMPAT
  848. .compat_ioctl = hpsa_compat_ioctl,
  849. #endif
  850. .sdev_attrs = hpsa_sdev_attrs,
  851. .shost_attrs = hpsa_shost_attrs,
  852. .max_sectors = 8192,
  853. .no_write_same = 1,
  854. };
  855. static inline u32 next_command(struct ctlr_info *h, u8 q)
  856. {
  857. u32 a;
  858. struct reply_queue_buffer *rq = &h->reply_queue[q];
  859. if (h->transMethod & CFGTBL_Trans_io_accel1)
  860. return h->access.command_completed(h, q);
  861. if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
  862. return h->access.command_completed(h, q);
  863. if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
  864. a = rq->head[rq->current_entry];
  865. rq->current_entry++;
  866. atomic_dec(&h->commands_outstanding);
  867. } else {
  868. a = FIFO_EMPTY;
  869. }
  870. /* Check for wraparound */
  871. if (rq->current_entry == h->max_commands) {
  872. rq->current_entry = 0;
  873. rq->wraparound ^= 1;
  874. }
  875. return a;
  876. }
  877. /*
  878. * There are some special bits in the bus address of the
  879. * command that we have to set for the controller to know
  880. * how to process the command:
  881. *
  882. * Normal performant mode:
  883. * bit 0: 1 means performant mode, 0 means simple mode.
  884. * bits 1-3 = block fetch table entry
  885. * bits 4-6 = command type (== 0)
  886. *
  887. * ioaccel1 mode:
  888. * bit 0 = "performant mode" bit.
  889. * bits 1-3 = block fetch table entry
  890. * bits 4-6 = command type (== 110)
  891. * (command type is needed because ioaccel1 mode
  892. * commands are submitted through the same register as normal
  893. * mode commands, so this is how the controller knows whether
  894. * the command is normal mode or ioaccel1 mode.)
  895. *
  896. * ioaccel2 mode:
  897. * bit 0 = "performant mode" bit.
  898. * bits 1-4 = block fetch table entry (note extra bit)
  899. * bits 4-6 = not needed, because ioaccel2 mode has
  900. * a separate special register for submitting commands.
  901. */
  902. /*
  903. * set_performant_mode: Modify the tag for cciss performant
  904. * set bit 0 for pull model, bits 3-1 for block fetch
  905. * register number
  906. */
  907. #define DEFAULT_REPLY_QUEUE (-1)
  908. static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
  909. int reply_queue)
  910. {
  911. if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
  912. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  913. if (unlikely(!h->msix_vector))
  914. return;
  915. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  916. c->Header.ReplyQueue =
  917. raw_smp_processor_id() % h->nreply_queues;
  918. else
  919. c->Header.ReplyQueue = reply_queue % h->nreply_queues;
  920. }
  921. }
  922. static void set_ioaccel1_performant_mode(struct ctlr_info *h,
  923. struct CommandList *c,
  924. int reply_queue)
  925. {
  926. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
  927. /*
  928. * Tell the controller to post the reply to the queue for this
  929. * processor. This seems to give the best I/O throughput.
  930. */
  931. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  932. cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
  933. else
  934. cp->ReplyQueue = reply_queue % h->nreply_queues;
  935. /*
  936. * Set the bits in the address sent down to include:
  937. * - performant mode bit (bit 0)
  938. * - pull count (bits 1-3)
  939. * - command type (bits 4-6)
  940. */
  941. c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
  942. IOACCEL1_BUSADDR_CMDTYPE;
  943. }
  944. static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
  945. struct CommandList *c,
  946. int reply_queue)
  947. {
  948. struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
  949. &h->ioaccel2_cmd_pool[c->cmdindex];
  950. /* Tell the controller to post the reply to the queue for this
  951. * processor. This seems to give the best I/O throughput.
  952. */
  953. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  954. cp->reply_queue = smp_processor_id() % h->nreply_queues;
  955. else
  956. cp->reply_queue = reply_queue % h->nreply_queues;
  957. /* Set the bits in the address sent down to include:
  958. * - performant mode bit not used in ioaccel mode 2
  959. * - pull count (bits 0-3)
  960. * - command type isn't needed for ioaccel2
  961. */
  962. c->busaddr |= h->ioaccel2_blockFetchTable[0];
  963. }
  964. static void set_ioaccel2_performant_mode(struct ctlr_info *h,
  965. struct CommandList *c,
  966. int reply_queue)
  967. {
  968. struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
  969. /*
  970. * Tell the controller to post the reply to the queue for this
  971. * processor. This seems to give the best I/O throughput.
  972. */
  973. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  974. cp->reply_queue = smp_processor_id() % h->nreply_queues;
  975. else
  976. cp->reply_queue = reply_queue % h->nreply_queues;
  977. /*
  978. * Set the bits in the address sent down to include:
  979. * - performant mode bit not used in ioaccel mode 2
  980. * - pull count (bits 0-3)
  981. * - command type isn't needed for ioaccel2
  982. */
  983. c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
  984. }
  985. static int is_firmware_flash_cmd(u8 *cdb)
  986. {
  987. return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
  988. }
  989. /*
  990. * During firmware flash, the heartbeat register may not update as frequently
  991. * as it should. So we dial down lockup detection during firmware flash. and
  992. * dial it back up when firmware flash completes.
  993. */
  994. #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
  995. #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
  996. static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
  997. struct CommandList *c)
  998. {
  999. if (!is_firmware_flash_cmd(c->Request.CDB))
  1000. return;
  1001. atomic_inc(&h->firmware_flash_in_progress);
  1002. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
  1003. }
  1004. static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
  1005. struct CommandList *c)
  1006. {
  1007. if (is_firmware_flash_cmd(c->Request.CDB) &&
  1008. atomic_dec_and_test(&h->firmware_flash_in_progress))
  1009. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
  1010. }
  1011. static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
  1012. struct CommandList *c, int reply_queue)
  1013. {
  1014. dial_down_lockup_detection_during_fw_flash(h, c);
  1015. atomic_inc(&h->commands_outstanding);
  1016. switch (c->cmd_type) {
  1017. case CMD_IOACCEL1:
  1018. set_ioaccel1_performant_mode(h, c, reply_queue);
  1019. writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
  1020. break;
  1021. case CMD_IOACCEL2:
  1022. set_ioaccel2_performant_mode(h, c, reply_queue);
  1023. writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
  1024. break;
  1025. case IOACCEL2_TMF:
  1026. set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
  1027. writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
  1028. break;
  1029. default:
  1030. set_performant_mode(h, c, reply_queue);
  1031. h->access.submit_command(h, c);
  1032. }
  1033. }
  1034. static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
  1035. {
  1036. if (unlikely(hpsa_is_pending_event(c)))
  1037. return finish_cmd(c);
  1038. __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
  1039. }
  1040. static inline int is_hba_lunid(unsigned char scsi3addr[])
  1041. {
  1042. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  1043. }
  1044. static inline int is_scsi_rev_5(struct ctlr_info *h)
  1045. {
  1046. if (!h->hba_inquiry_data)
  1047. return 0;
  1048. if ((h->hba_inquiry_data[2] & 0x07) == 5)
  1049. return 1;
  1050. return 0;
  1051. }
  1052. static int hpsa_find_target_lun(struct ctlr_info *h,
  1053. unsigned char scsi3addr[], int bus, int *target, int *lun)
  1054. {
  1055. /* finds an unused bus, target, lun for a new physical device
  1056. * assumes h->devlock is held
  1057. */
  1058. int i, found = 0;
  1059. DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
  1060. bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
  1061. for (i = 0; i < h->ndevices; i++) {
  1062. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  1063. __set_bit(h->dev[i]->target, lun_taken);
  1064. }
  1065. i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
  1066. if (i < HPSA_MAX_DEVICES) {
  1067. /* *bus = 1; */
  1068. *target = i;
  1069. *lun = 0;
  1070. found = 1;
  1071. }
  1072. return !found;
  1073. }
  1074. static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
  1075. struct hpsa_scsi_dev_t *dev, char *description)
  1076. {
  1077. #define LABEL_SIZE 25
  1078. char label[LABEL_SIZE];
  1079. if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
  1080. return;
  1081. switch (dev->devtype) {
  1082. case TYPE_RAID:
  1083. snprintf(label, LABEL_SIZE, "controller");
  1084. break;
  1085. case TYPE_ENCLOSURE:
  1086. snprintf(label, LABEL_SIZE, "enclosure");
  1087. break;
  1088. case TYPE_DISK:
  1089. case TYPE_ZBC:
  1090. if (dev->external)
  1091. snprintf(label, LABEL_SIZE, "external");
  1092. else if (!is_logical_dev_addr_mode(dev->scsi3addr))
  1093. snprintf(label, LABEL_SIZE, "%s",
  1094. raid_label[PHYSICAL_DRIVE]);
  1095. else
  1096. snprintf(label, LABEL_SIZE, "RAID-%s",
  1097. dev->raid_level > RAID_UNKNOWN ? "?" :
  1098. raid_label[dev->raid_level]);
  1099. break;
  1100. case TYPE_ROM:
  1101. snprintf(label, LABEL_SIZE, "rom");
  1102. break;
  1103. case TYPE_TAPE:
  1104. snprintf(label, LABEL_SIZE, "tape");
  1105. break;
  1106. case TYPE_MEDIUM_CHANGER:
  1107. snprintf(label, LABEL_SIZE, "changer");
  1108. break;
  1109. default:
  1110. snprintf(label, LABEL_SIZE, "UNKNOWN");
  1111. break;
  1112. }
  1113. dev_printk(level, &h->pdev->dev,
  1114. "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
  1115. h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
  1116. description,
  1117. scsi_device_type(dev->devtype),
  1118. dev->vendor,
  1119. dev->model,
  1120. label,
  1121. dev->offload_config ? '+' : '-',
  1122. dev->offload_enabled ? '+' : '-',
  1123. dev->expose_device);
  1124. }
  1125. /* Add an entry into h->dev[] array. */
  1126. static int hpsa_scsi_add_entry(struct ctlr_info *h,
  1127. struct hpsa_scsi_dev_t *device,
  1128. struct hpsa_scsi_dev_t *added[], int *nadded)
  1129. {
  1130. /* assumes h->devlock is held */
  1131. int n = h->ndevices;
  1132. int i;
  1133. unsigned char addr1[8], addr2[8];
  1134. struct hpsa_scsi_dev_t *sd;
  1135. if (n >= HPSA_MAX_DEVICES) {
  1136. dev_err(&h->pdev->dev, "too many devices, some will be "
  1137. "inaccessible.\n");
  1138. return -1;
  1139. }
  1140. /* physical devices do not have lun or target assigned until now. */
  1141. if (device->lun != -1)
  1142. /* Logical device, lun is already assigned. */
  1143. goto lun_assigned;
  1144. /* If this device a non-zero lun of a multi-lun device
  1145. * byte 4 of the 8-byte LUN addr will contain the logical
  1146. * unit no, zero otherwise.
  1147. */
  1148. if (device->scsi3addr[4] == 0) {
  1149. /* This is not a non-zero lun of a multi-lun device */
  1150. if (hpsa_find_target_lun(h, device->scsi3addr,
  1151. device->bus, &device->target, &device->lun) != 0)
  1152. return -1;
  1153. goto lun_assigned;
  1154. }
  1155. /* This is a non-zero lun of a multi-lun device.
  1156. * Search through our list and find the device which
  1157. * has the same 8 byte LUN address, excepting byte 4 and 5.
  1158. * Assign the same bus and target for this new LUN.
  1159. * Use the logical unit number from the firmware.
  1160. */
  1161. memcpy(addr1, device->scsi3addr, 8);
  1162. addr1[4] = 0;
  1163. addr1[5] = 0;
  1164. for (i = 0; i < n; i++) {
  1165. sd = h->dev[i];
  1166. memcpy(addr2, sd->scsi3addr, 8);
  1167. addr2[4] = 0;
  1168. addr2[5] = 0;
  1169. /* differ only in byte 4 and 5? */
  1170. if (memcmp(addr1, addr2, 8) == 0) {
  1171. device->bus = sd->bus;
  1172. device->target = sd->target;
  1173. device->lun = device->scsi3addr[4];
  1174. break;
  1175. }
  1176. }
  1177. if (device->lun == -1) {
  1178. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  1179. " suspect firmware bug or unsupported hardware "
  1180. "configuration.\n");
  1181. return -1;
  1182. }
  1183. lun_assigned:
  1184. h->dev[n] = device;
  1185. h->ndevices++;
  1186. added[*nadded] = device;
  1187. (*nadded)++;
  1188. hpsa_show_dev_msg(KERN_INFO, h, device,
  1189. device->expose_device ? "added" : "masked");
  1190. device->offload_to_be_enabled = device->offload_enabled;
  1191. device->offload_enabled = 0;
  1192. return 0;
  1193. }
  1194. /* Update an entry in h->dev[] array. */
  1195. static void hpsa_scsi_update_entry(struct ctlr_info *h,
  1196. int entry, struct hpsa_scsi_dev_t *new_entry)
  1197. {
  1198. int offload_enabled;
  1199. /* assumes h->devlock is held */
  1200. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  1201. /* Raid level changed. */
  1202. h->dev[entry]->raid_level = new_entry->raid_level;
  1203. /* Raid offload parameters changed. Careful about the ordering. */
  1204. if (new_entry->offload_config && new_entry->offload_enabled) {
  1205. /*
  1206. * if drive is newly offload_enabled, we want to copy the
  1207. * raid map data first. If previously offload_enabled and
  1208. * offload_config were set, raid map data had better be
  1209. * the same as it was before. if raid map data is changed
  1210. * then it had better be the case that
  1211. * h->dev[entry]->offload_enabled is currently 0.
  1212. */
  1213. h->dev[entry]->raid_map = new_entry->raid_map;
  1214. h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
  1215. }
  1216. if (new_entry->hba_ioaccel_enabled) {
  1217. h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
  1218. wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
  1219. }
  1220. h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
  1221. h->dev[entry]->offload_config = new_entry->offload_config;
  1222. h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
  1223. h->dev[entry]->queue_depth = new_entry->queue_depth;
  1224. /*
  1225. * We can turn off ioaccel offload now, but need to delay turning
  1226. * it on until we can update h->dev[entry]->phys_disk[], but we
  1227. * can't do that until all the devices are updated.
  1228. */
  1229. h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
  1230. if (!new_entry->offload_enabled)
  1231. h->dev[entry]->offload_enabled = 0;
  1232. offload_enabled = h->dev[entry]->offload_enabled;
  1233. h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
  1234. hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
  1235. h->dev[entry]->offload_enabled = offload_enabled;
  1236. }
  1237. /* Replace an entry from h->dev[] array. */
  1238. static void hpsa_scsi_replace_entry(struct ctlr_info *h,
  1239. int entry, struct hpsa_scsi_dev_t *new_entry,
  1240. struct hpsa_scsi_dev_t *added[], int *nadded,
  1241. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  1242. {
  1243. /* assumes h->devlock is held */
  1244. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  1245. removed[*nremoved] = h->dev[entry];
  1246. (*nremoved)++;
  1247. /*
  1248. * New physical devices won't have target/lun assigned yet
  1249. * so we need to preserve the values in the slot we are replacing.
  1250. */
  1251. if (new_entry->target == -1) {
  1252. new_entry->target = h->dev[entry]->target;
  1253. new_entry->lun = h->dev[entry]->lun;
  1254. }
  1255. h->dev[entry] = new_entry;
  1256. added[*nadded] = new_entry;
  1257. (*nadded)++;
  1258. hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
  1259. new_entry->offload_to_be_enabled = new_entry->offload_enabled;
  1260. new_entry->offload_enabled = 0;
  1261. }
  1262. /* Remove an entry from h->dev[] array. */
  1263. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
  1264. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  1265. {
  1266. /* assumes h->devlock is held */
  1267. int i;
  1268. struct hpsa_scsi_dev_t *sd;
  1269. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  1270. sd = h->dev[entry];
  1271. removed[*nremoved] = h->dev[entry];
  1272. (*nremoved)++;
  1273. for (i = entry; i < h->ndevices-1; i++)
  1274. h->dev[i] = h->dev[i+1];
  1275. h->ndevices--;
  1276. hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
  1277. }
  1278. #define SCSI3ADDR_EQ(a, b) ( \
  1279. (a)[7] == (b)[7] && \
  1280. (a)[6] == (b)[6] && \
  1281. (a)[5] == (b)[5] && \
  1282. (a)[4] == (b)[4] && \
  1283. (a)[3] == (b)[3] && \
  1284. (a)[2] == (b)[2] && \
  1285. (a)[1] == (b)[1] && \
  1286. (a)[0] == (b)[0])
  1287. static void fixup_botched_add(struct ctlr_info *h,
  1288. struct hpsa_scsi_dev_t *added)
  1289. {
  1290. /* called when scsi_add_device fails in order to re-adjust
  1291. * h->dev[] to match the mid layer's view.
  1292. */
  1293. unsigned long flags;
  1294. int i, j;
  1295. spin_lock_irqsave(&h->lock, flags);
  1296. for (i = 0; i < h->ndevices; i++) {
  1297. if (h->dev[i] == added) {
  1298. for (j = i; j < h->ndevices-1; j++)
  1299. h->dev[j] = h->dev[j+1];
  1300. h->ndevices--;
  1301. break;
  1302. }
  1303. }
  1304. spin_unlock_irqrestore(&h->lock, flags);
  1305. kfree(added);
  1306. }
  1307. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  1308. struct hpsa_scsi_dev_t *dev2)
  1309. {
  1310. /* we compare everything except lun and target as these
  1311. * are not yet assigned. Compare parts likely
  1312. * to differ first
  1313. */
  1314. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  1315. sizeof(dev1->scsi3addr)) != 0)
  1316. return 0;
  1317. if (memcmp(dev1->device_id, dev2->device_id,
  1318. sizeof(dev1->device_id)) != 0)
  1319. return 0;
  1320. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  1321. return 0;
  1322. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  1323. return 0;
  1324. if (dev1->devtype != dev2->devtype)
  1325. return 0;
  1326. if (dev1->bus != dev2->bus)
  1327. return 0;
  1328. return 1;
  1329. }
  1330. static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
  1331. struct hpsa_scsi_dev_t *dev2)
  1332. {
  1333. /* Device attributes that can change, but don't mean
  1334. * that the device is a different device, nor that the OS
  1335. * needs to be told anything about the change.
  1336. */
  1337. if (dev1->raid_level != dev2->raid_level)
  1338. return 1;
  1339. if (dev1->offload_config != dev2->offload_config)
  1340. return 1;
  1341. if (dev1->offload_enabled != dev2->offload_enabled)
  1342. return 1;
  1343. if (!is_logical_dev_addr_mode(dev1->scsi3addr))
  1344. if (dev1->queue_depth != dev2->queue_depth)
  1345. return 1;
  1346. return 0;
  1347. }
  1348. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  1349. * and return needle location in *index. If scsi3addr matches, but not
  1350. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  1351. * location in *index.
  1352. * In the case of a minor device attribute change, such as RAID level, just
  1353. * return DEVICE_UPDATED, along with the updated device's location in index.
  1354. * If needle not found, return DEVICE_NOT_FOUND.
  1355. */
  1356. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  1357. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  1358. int *index)
  1359. {
  1360. int i;
  1361. #define DEVICE_NOT_FOUND 0
  1362. #define DEVICE_CHANGED 1
  1363. #define DEVICE_SAME 2
  1364. #define DEVICE_UPDATED 3
  1365. if (needle == NULL)
  1366. return DEVICE_NOT_FOUND;
  1367. for (i = 0; i < haystack_size; i++) {
  1368. if (haystack[i] == NULL) /* previously removed. */
  1369. continue;
  1370. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  1371. *index = i;
  1372. if (device_is_the_same(needle, haystack[i])) {
  1373. if (device_updated(needle, haystack[i]))
  1374. return DEVICE_UPDATED;
  1375. return DEVICE_SAME;
  1376. } else {
  1377. /* Keep offline devices offline */
  1378. if (needle->volume_offline)
  1379. return DEVICE_NOT_FOUND;
  1380. return DEVICE_CHANGED;
  1381. }
  1382. }
  1383. }
  1384. *index = -1;
  1385. return DEVICE_NOT_FOUND;
  1386. }
  1387. static void hpsa_monitor_offline_device(struct ctlr_info *h,
  1388. unsigned char scsi3addr[])
  1389. {
  1390. struct offline_device_entry *device;
  1391. unsigned long flags;
  1392. /* Check to see if device is already on the list */
  1393. spin_lock_irqsave(&h->offline_device_lock, flags);
  1394. list_for_each_entry(device, &h->offline_device_list, offline_list) {
  1395. if (memcmp(device->scsi3addr, scsi3addr,
  1396. sizeof(device->scsi3addr)) == 0) {
  1397. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1398. return;
  1399. }
  1400. }
  1401. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1402. /* Device is not on the list, add it. */
  1403. device = kmalloc(sizeof(*device), GFP_KERNEL);
  1404. if (!device) {
  1405. dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
  1406. return;
  1407. }
  1408. memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
  1409. spin_lock_irqsave(&h->offline_device_lock, flags);
  1410. list_add_tail(&device->offline_list, &h->offline_device_list);
  1411. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1412. }
  1413. /* Print a message explaining various offline volume states */
  1414. static void hpsa_show_volume_status(struct ctlr_info *h,
  1415. struct hpsa_scsi_dev_t *sd)
  1416. {
  1417. if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
  1418. dev_info(&h->pdev->dev,
  1419. "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
  1420. h->scsi_host->host_no,
  1421. sd->bus, sd->target, sd->lun);
  1422. switch (sd->volume_offline) {
  1423. case HPSA_LV_OK:
  1424. break;
  1425. case HPSA_LV_UNDERGOING_ERASE:
  1426. dev_info(&h->pdev->dev,
  1427. "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
  1428. h->scsi_host->host_no,
  1429. sd->bus, sd->target, sd->lun);
  1430. break;
  1431. case HPSA_LV_NOT_AVAILABLE:
  1432. dev_info(&h->pdev->dev,
  1433. "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
  1434. h->scsi_host->host_no,
  1435. sd->bus, sd->target, sd->lun);
  1436. break;
  1437. case HPSA_LV_UNDERGOING_RPI:
  1438. dev_info(&h->pdev->dev,
  1439. "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
  1440. h->scsi_host->host_no,
  1441. sd->bus, sd->target, sd->lun);
  1442. break;
  1443. case HPSA_LV_PENDING_RPI:
  1444. dev_info(&h->pdev->dev,
  1445. "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
  1446. h->scsi_host->host_no,
  1447. sd->bus, sd->target, sd->lun);
  1448. break;
  1449. case HPSA_LV_ENCRYPTED_NO_KEY:
  1450. dev_info(&h->pdev->dev,
  1451. "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
  1452. h->scsi_host->host_no,
  1453. sd->bus, sd->target, sd->lun);
  1454. break;
  1455. case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
  1456. dev_info(&h->pdev->dev,
  1457. "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
  1458. h->scsi_host->host_no,
  1459. sd->bus, sd->target, sd->lun);
  1460. break;
  1461. case HPSA_LV_UNDERGOING_ENCRYPTION:
  1462. dev_info(&h->pdev->dev,
  1463. "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
  1464. h->scsi_host->host_no,
  1465. sd->bus, sd->target, sd->lun);
  1466. break;
  1467. case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
  1468. dev_info(&h->pdev->dev,
  1469. "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
  1470. h->scsi_host->host_no,
  1471. sd->bus, sd->target, sd->lun);
  1472. break;
  1473. case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
  1474. dev_info(&h->pdev->dev,
  1475. "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
  1476. h->scsi_host->host_no,
  1477. sd->bus, sd->target, sd->lun);
  1478. break;
  1479. case HPSA_LV_PENDING_ENCRYPTION:
  1480. dev_info(&h->pdev->dev,
  1481. "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
  1482. h->scsi_host->host_no,
  1483. sd->bus, sd->target, sd->lun);
  1484. break;
  1485. case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
  1486. dev_info(&h->pdev->dev,
  1487. "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
  1488. h->scsi_host->host_no,
  1489. sd->bus, sd->target, sd->lun);
  1490. break;
  1491. }
  1492. }
  1493. /*
  1494. * Figure the list of physical drive pointers for a logical drive with
  1495. * raid offload configured.
  1496. */
  1497. static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
  1498. struct hpsa_scsi_dev_t *dev[], int ndevices,
  1499. struct hpsa_scsi_dev_t *logical_drive)
  1500. {
  1501. struct raid_map_data *map = &logical_drive->raid_map;
  1502. struct raid_map_disk_data *dd = &map->data[0];
  1503. int i, j;
  1504. int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
  1505. le16_to_cpu(map->metadata_disks_per_row);
  1506. int nraid_map_entries = le16_to_cpu(map->row_cnt) *
  1507. le16_to_cpu(map->layout_map_count) *
  1508. total_disks_per_row;
  1509. int nphys_disk = le16_to_cpu(map->layout_map_count) *
  1510. total_disks_per_row;
  1511. int qdepth;
  1512. if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
  1513. nraid_map_entries = RAID_MAP_MAX_ENTRIES;
  1514. logical_drive->nphysical_disks = nraid_map_entries;
  1515. qdepth = 0;
  1516. for (i = 0; i < nraid_map_entries; i++) {
  1517. logical_drive->phys_disk[i] = NULL;
  1518. if (!logical_drive->offload_config)
  1519. continue;
  1520. for (j = 0; j < ndevices; j++) {
  1521. if (dev[j] == NULL)
  1522. continue;
  1523. if (dev[j]->devtype != TYPE_DISK &&
  1524. dev[j]->devtype != TYPE_ZBC)
  1525. continue;
  1526. if (is_logical_device(dev[j]))
  1527. continue;
  1528. if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
  1529. continue;
  1530. logical_drive->phys_disk[i] = dev[j];
  1531. if (i < nphys_disk)
  1532. qdepth = min(h->nr_cmds, qdepth +
  1533. logical_drive->phys_disk[i]->queue_depth);
  1534. break;
  1535. }
  1536. /*
  1537. * This can happen if a physical drive is removed and
  1538. * the logical drive is degraded. In that case, the RAID
  1539. * map data will refer to a physical disk which isn't actually
  1540. * present. And in that case offload_enabled should already
  1541. * be 0, but we'll turn it off here just in case
  1542. */
  1543. if (!logical_drive->phys_disk[i]) {
  1544. logical_drive->offload_enabled = 0;
  1545. logical_drive->offload_to_be_enabled = 0;
  1546. logical_drive->queue_depth = 8;
  1547. }
  1548. }
  1549. if (nraid_map_entries)
  1550. /*
  1551. * This is correct for reads, too high for full stripe writes,
  1552. * way too high for partial stripe writes
  1553. */
  1554. logical_drive->queue_depth = qdepth;
  1555. else
  1556. logical_drive->queue_depth = h->nr_cmds;
  1557. }
  1558. static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
  1559. struct hpsa_scsi_dev_t *dev[], int ndevices)
  1560. {
  1561. int i;
  1562. for (i = 0; i < ndevices; i++) {
  1563. if (dev[i] == NULL)
  1564. continue;
  1565. if (dev[i]->devtype != TYPE_DISK &&
  1566. dev[i]->devtype != TYPE_ZBC)
  1567. continue;
  1568. if (!is_logical_device(dev[i]))
  1569. continue;
  1570. /*
  1571. * If offload is currently enabled, the RAID map and
  1572. * phys_disk[] assignment *better* not be changing
  1573. * and since it isn't changing, we do not need to
  1574. * update it.
  1575. */
  1576. if (dev[i]->offload_enabled)
  1577. continue;
  1578. hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
  1579. }
  1580. }
  1581. static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  1582. {
  1583. int rc = 0;
  1584. if (!h->scsi_host)
  1585. return 1;
  1586. if (is_logical_device(device)) /* RAID */
  1587. rc = scsi_add_device(h->scsi_host, device->bus,
  1588. device->target, device->lun);
  1589. else /* HBA */
  1590. rc = hpsa_add_sas_device(h->sas_host, device);
  1591. return rc;
  1592. }
  1593. static int hpsa_find_outstanding_commands_for_dev(struct ctlr_info *h,
  1594. struct hpsa_scsi_dev_t *dev)
  1595. {
  1596. int i;
  1597. int count = 0;
  1598. for (i = 0; i < h->nr_cmds; i++) {
  1599. struct CommandList *c = h->cmd_pool + i;
  1600. int refcount = atomic_inc_return(&c->refcount);
  1601. if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev,
  1602. dev->scsi3addr)) {
  1603. unsigned long flags;
  1604. spin_lock_irqsave(&h->lock, flags); /* Implied MB */
  1605. if (!hpsa_is_cmd_idle(c))
  1606. ++count;
  1607. spin_unlock_irqrestore(&h->lock, flags);
  1608. }
  1609. cmd_free(h, c);
  1610. }
  1611. return count;
  1612. }
  1613. static void hpsa_wait_for_outstanding_commands_for_dev(struct ctlr_info *h,
  1614. struct hpsa_scsi_dev_t *device)
  1615. {
  1616. int cmds = 0;
  1617. int waits = 0;
  1618. while (1) {
  1619. cmds = hpsa_find_outstanding_commands_for_dev(h, device);
  1620. if (cmds == 0)
  1621. break;
  1622. if (++waits > 20)
  1623. break;
  1624. dev_warn(&h->pdev->dev,
  1625. "%s: removing device with %d outstanding commands!\n",
  1626. __func__, cmds);
  1627. msleep(1000);
  1628. }
  1629. }
  1630. static void hpsa_remove_device(struct ctlr_info *h,
  1631. struct hpsa_scsi_dev_t *device)
  1632. {
  1633. struct scsi_device *sdev = NULL;
  1634. if (!h->scsi_host)
  1635. return;
  1636. if (is_logical_device(device)) { /* RAID */
  1637. sdev = scsi_device_lookup(h->scsi_host, device->bus,
  1638. device->target, device->lun);
  1639. if (sdev) {
  1640. scsi_remove_device(sdev);
  1641. scsi_device_put(sdev);
  1642. } else {
  1643. /*
  1644. * We don't expect to get here. Future commands
  1645. * to this device will get a selection timeout as
  1646. * if the device were gone.
  1647. */
  1648. hpsa_show_dev_msg(KERN_WARNING, h, device,
  1649. "didn't find device for removal.");
  1650. }
  1651. } else { /* HBA */
  1652. device->removed = 1;
  1653. hpsa_wait_for_outstanding_commands_for_dev(h, device);
  1654. hpsa_remove_sas_device(device);
  1655. }
  1656. }
  1657. static void adjust_hpsa_scsi_table(struct ctlr_info *h,
  1658. struct hpsa_scsi_dev_t *sd[], int nsds)
  1659. {
  1660. /* sd contains scsi3 addresses and devtypes, and inquiry
  1661. * data. This function takes what's in sd to be the current
  1662. * reality and updates h->dev[] to reflect that reality.
  1663. */
  1664. int i, entry, device_change, changes = 0;
  1665. struct hpsa_scsi_dev_t *csd;
  1666. unsigned long flags;
  1667. struct hpsa_scsi_dev_t **added, **removed;
  1668. int nadded, nremoved;
  1669. /*
  1670. * A reset can cause a device status to change
  1671. * re-schedule the scan to see what happened.
  1672. */
  1673. if (h->reset_in_progress) {
  1674. h->drv_req_rescan = 1;
  1675. return;
  1676. }
  1677. added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
  1678. removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
  1679. if (!added || !removed) {
  1680. dev_warn(&h->pdev->dev, "out of memory in "
  1681. "adjust_hpsa_scsi_table\n");
  1682. goto free_and_out;
  1683. }
  1684. spin_lock_irqsave(&h->devlock, flags);
  1685. /* find any devices in h->dev[] that are not in
  1686. * sd[] and remove them from h->dev[], and for any
  1687. * devices which have changed, remove the old device
  1688. * info and add the new device info.
  1689. * If minor device attributes change, just update
  1690. * the existing device structure.
  1691. */
  1692. i = 0;
  1693. nremoved = 0;
  1694. nadded = 0;
  1695. while (i < h->ndevices) {
  1696. csd = h->dev[i];
  1697. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  1698. if (device_change == DEVICE_NOT_FOUND) {
  1699. changes++;
  1700. hpsa_scsi_remove_entry(h, i, removed, &nremoved);
  1701. continue; /* remove ^^^, hence i not incremented */
  1702. } else if (device_change == DEVICE_CHANGED) {
  1703. changes++;
  1704. hpsa_scsi_replace_entry(h, i, sd[entry],
  1705. added, &nadded, removed, &nremoved);
  1706. /* Set it to NULL to prevent it from being freed
  1707. * at the bottom of hpsa_update_scsi_devices()
  1708. */
  1709. sd[entry] = NULL;
  1710. } else if (device_change == DEVICE_UPDATED) {
  1711. hpsa_scsi_update_entry(h, i, sd[entry]);
  1712. }
  1713. i++;
  1714. }
  1715. /* Now, make sure every device listed in sd[] is also
  1716. * listed in h->dev[], adding them if they aren't found
  1717. */
  1718. for (i = 0; i < nsds; i++) {
  1719. if (!sd[i]) /* if already added above. */
  1720. continue;
  1721. /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
  1722. * as the SCSI mid-layer does not handle such devices well.
  1723. * It relentlessly loops sending TUR at 3Hz, then READ(10)
  1724. * at 160Hz, and prevents the system from coming up.
  1725. */
  1726. if (sd[i]->volume_offline) {
  1727. hpsa_show_volume_status(h, sd[i]);
  1728. hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
  1729. continue;
  1730. }
  1731. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  1732. h->ndevices, &entry);
  1733. if (device_change == DEVICE_NOT_FOUND) {
  1734. changes++;
  1735. if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
  1736. break;
  1737. sd[i] = NULL; /* prevent from being freed later. */
  1738. } else if (device_change == DEVICE_CHANGED) {
  1739. /* should never happen... */
  1740. changes++;
  1741. dev_warn(&h->pdev->dev,
  1742. "device unexpectedly changed.\n");
  1743. /* but if it does happen, we just ignore that device */
  1744. }
  1745. }
  1746. hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
  1747. /* Now that h->dev[]->phys_disk[] is coherent, we can enable
  1748. * any logical drives that need it enabled.
  1749. */
  1750. for (i = 0; i < h->ndevices; i++) {
  1751. if (h->dev[i] == NULL)
  1752. continue;
  1753. h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
  1754. }
  1755. spin_unlock_irqrestore(&h->devlock, flags);
  1756. /* Monitor devices which are in one of several NOT READY states to be
  1757. * brought online later. This must be done without holding h->devlock,
  1758. * so don't touch h->dev[]
  1759. */
  1760. for (i = 0; i < nsds; i++) {
  1761. if (!sd[i]) /* if already added above. */
  1762. continue;
  1763. if (sd[i]->volume_offline)
  1764. hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
  1765. }
  1766. /* Don't notify scsi mid layer of any changes the first time through
  1767. * (or if there are no changes) scsi_scan_host will do it later the
  1768. * first time through.
  1769. */
  1770. if (!changes)
  1771. goto free_and_out;
  1772. /* Notify scsi mid layer of any removed devices */
  1773. for (i = 0; i < nremoved; i++) {
  1774. if (removed[i] == NULL)
  1775. continue;
  1776. if (removed[i]->expose_device)
  1777. hpsa_remove_device(h, removed[i]);
  1778. kfree(removed[i]);
  1779. removed[i] = NULL;
  1780. }
  1781. /* Notify scsi mid layer of any added devices */
  1782. for (i = 0; i < nadded; i++) {
  1783. int rc = 0;
  1784. if (added[i] == NULL)
  1785. continue;
  1786. if (!(added[i]->expose_device))
  1787. continue;
  1788. rc = hpsa_add_device(h, added[i]);
  1789. if (!rc)
  1790. continue;
  1791. dev_warn(&h->pdev->dev,
  1792. "addition failed %d, device not added.", rc);
  1793. /* now we have to remove it from h->dev,
  1794. * since it didn't get added to scsi mid layer
  1795. */
  1796. fixup_botched_add(h, added[i]);
  1797. h->drv_req_rescan = 1;
  1798. }
  1799. free_and_out:
  1800. kfree(added);
  1801. kfree(removed);
  1802. }
  1803. /*
  1804. * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
  1805. * Assume's h->devlock is held.
  1806. */
  1807. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  1808. int bus, int target, int lun)
  1809. {
  1810. int i;
  1811. struct hpsa_scsi_dev_t *sd;
  1812. for (i = 0; i < h->ndevices; i++) {
  1813. sd = h->dev[i];
  1814. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  1815. return sd;
  1816. }
  1817. return NULL;
  1818. }
  1819. static int hpsa_slave_alloc(struct scsi_device *sdev)
  1820. {
  1821. struct hpsa_scsi_dev_t *sd = NULL;
  1822. unsigned long flags;
  1823. struct ctlr_info *h;
  1824. h = sdev_to_hba(sdev);
  1825. spin_lock_irqsave(&h->devlock, flags);
  1826. if (sdev_channel(sdev) == HPSA_PHYSICAL_DEVICE_BUS) {
  1827. struct scsi_target *starget;
  1828. struct sas_rphy *rphy;
  1829. starget = scsi_target(sdev);
  1830. rphy = target_to_rphy(starget);
  1831. sd = hpsa_find_device_by_sas_rphy(h, rphy);
  1832. if (sd) {
  1833. sd->target = sdev_id(sdev);
  1834. sd->lun = sdev->lun;
  1835. }
  1836. }
  1837. if (!sd)
  1838. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  1839. sdev_id(sdev), sdev->lun);
  1840. if (sd && sd->expose_device) {
  1841. atomic_set(&sd->ioaccel_cmds_out, 0);
  1842. sdev->hostdata = sd;
  1843. } else
  1844. sdev->hostdata = NULL;
  1845. spin_unlock_irqrestore(&h->devlock, flags);
  1846. return 0;
  1847. }
  1848. /* configure scsi device based on internal per-device structure */
  1849. static int hpsa_slave_configure(struct scsi_device *sdev)
  1850. {
  1851. struct hpsa_scsi_dev_t *sd;
  1852. int queue_depth;
  1853. sd = sdev->hostdata;
  1854. sdev->no_uld_attach = !sd || !sd->expose_device;
  1855. if (sd)
  1856. queue_depth = sd->queue_depth != 0 ?
  1857. sd->queue_depth : sdev->host->can_queue;
  1858. else
  1859. queue_depth = sdev->host->can_queue;
  1860. scsi_change_queue_depth(sdev, queue_depth);
  1861. return 0;
  1862. }
  1863. static void hpsa_slave_destroy(struct scsi_device *sdev)
  1864. {
  1865. /* nothing to do. */
  1866. }
  1867. static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
  1868. {
  1869. int i;
  1870. if (!h->ioaccel2_cmd_sg_list)
  1871. return;
  1872. for (i = 0; i < h->nr_cmds; i++) {
  1873. kfree(h->ioaccel2_cmd_sg_list[i]);
  1874. h->ioaccel2_cmd_sg_list[i] = NULL;
  1875. }
  1876. kfree(h->ioaccel2_cmd_sg_list);
  1877. h->ioaccel2_cmd_sg_list = NULL;
  1878. }
  1879. static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
  1880. {
  1881. int i;
  1882. if (h->chainsize <= 0)
  1883. return 0;
  1884. h->ioaccel2_cmd_sg_list =
  1885. kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
  1886. GFP_KERNEL);
  1887. if (!h->ioaccel2_cmd_sg_list)
  1888. return -ENOMEM;
  1889. for (i = 0; i < h->nr_cmds; i++) {
  1890. h->ioaccel2_cmd_sg_list[i] =
  1891. kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
  1892. h->maxsgentries, GFP_KERNEL);
  1893. if (!h->ioaccel2_cmd_sg_list[i])
  1894. goto clean;
  1895. }
  1896. return 0;
  1897. clean:
  1898. hpsa_free_ioaccel2_sg_chain_blocks(h);
  1899. return -ENOMEM;
  1900. }
  1901. static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
  1902. {
  1903. int i;
  1904. if (!h->cmd_sg_list)
  1905. return;
  1906. for (i = 0; i < h->nr_cmds; i++) {
  1907. kfree(h->cmd_sg_list[i]);
  1908. h->cmd_sg_list[i] = NULL;
  1909. }
  1910. kfree(h->cmd_sg_list);
  1911. h->cmd_sg_list = NULL;
  1912. }
  1913. static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
  1914. {
  1915. int i;
  1916. if (h->chainsize <= 0)
  1917. return 0;
  1918. h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
  1919. GFP_KERNEL);
  1920. if (!h->cmd_sg_list) {
  1921. dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
  1922. return -ENOMEM;
  1923. }
  1924. for (i = 0; i < h->nr_cmds; i++) {
  1925. h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
  1926. h->chainsize, GFP_KERNEL);
  1927. if (!h->cmd_sg_list[i]) {
  1928. dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
  1929. goto clean;
  1930. }
  1931. }
  1932. return 0;
  1933. clean:
  1934. hpsa_free_sg_chain_blocks(h);
  1935. return -ENOMEM;
  1936. }
  1937. static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
  1938. struct io_accel2_cmd *cp, struct CommandList *c)
  1939. {
  1940. struct ioaccel2_sg_element *chain_block;
  1941. u64 temp64;
  1942. u32 chain_size;
  1943. chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
  1944. chain_size = le32_to_cpu(cp->sg[0].length);
  1945. temp64 = pci_map_single(h->pdev, chain_block, chain_size,
  1946. PCI_DMA_TODEVICE);
  1947. if (dma_mapping_error(&h->pdev->dev, temp64)) {
  1948. /* prevent subsequent unmapping */
  1949. cp->sg->address = 0;
  1950. return -1;
  1951. }
  1952. cp->sg->address = cpu_to_le64(temp64);
  1953. return 0;
  1954. }
  1955. static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
  1956. struct io_accel2_cmd *cp)
  1957. {
  1958. struct ioaccel2_sg_element *chain_sg;
  1959. u64 temp64;
  1960. u32 chain_size;
  1961. chain_sg = cp->sg;
  1962. temp64 = le64_to_cpu(chain_sg->address);
  1963. chain_size = le32_to_cpu(cp->sg[0].length);
  1964. pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
  1965. }
  1966. static int hpsa_map_sg_chain_block(struct ctlr_info *h,
  1967. struct CommandList *c)
  1968. {
  1969. struct SGDescriptor *chain_sg, *chain_block;
  1970. u64 temp64;
  1971. u32 chain_len;
  1972. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  1973. chain_block = h->cmd_sg_list[c->cmdindex];
  1974. chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
  1975. chain_len = sizeof(*chain_sg) *
  1976. (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
  1977. chain_sg->Len = cpu_to_le32(chain_len);
  1978. temp64 = pci_map_single(h->pdev, chain_block, chain_len,
  1979. PCI_DMA_TODEVICE);
  1980. if (dma_mapping_error(&h->pdev->dev, temp64)) {
  1981. /* prevent subsequent unmapping */
  1982. chain_sg->Addr = cpu_to_le64(0);
  1983. return -1;
  1984. }
  1985. chain_sg->Addr = cpu_to_le64(temp64);
  1986. return 0;
  1987. }
  1988. static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
  1989. struct CommandList *c)
  1990. {
  1991. struct SGDescriptor *chain_sg;
  1992. if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
  1993. return;
  1994. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  1995. pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
  1996. le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
  1997. }
  1998. /* Decode the various types of errors on ioaccel2 path.
  1999. * Return 1 for any error that should generate a RAID path retry.
  2000. * Return 0 for errors that don't require a RAID path retry.
  2001. */
  2002. static int handle_ioaccel_mode2_error(struct ctlr_info *h,
  2003. struct CommandList *c,
  2004. struct scsi_cmnd *cmd,
  2005. struct io_accel2_cmd *c2,
  2006. struct hpsa_scsi_dev_t *dev)
  2007. {
  2008. int data_len;
  2009. int retry = 0;
  2010. u32 ioaccel2_resid = 0;
  2011. switch (c2->error_data.serv_response) {
  2012. case IOACCEL2_SERV_RESPONSE_COMPLETE:
  2013. switch (c2->error_data.status) {
  2014. case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
  2015. break;
  2016. case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
  2017. cmd->result |= SAM_STAT_CHECK_CONDITION;
  2018. if (c2->error_data.data_present !=
  2019. IOACCEL2_SENSE_DATA_PRESENT) {
  2020. memset(cmd->sense_buffer, 0,
  2021. SCSI_SENSE_BUFFERSIZE);
  2022. break;
  2023. }
  2024. /* copy the sense data */
  2025. data_len = c2->error_data.sense_data_len;
  2026. if (data_len > SCSI_SENSE_BUFFERSIZE)
  2027. data_len = SCSI_SENSE_BUFFERSIZE;
  2028. if (data_len > sizeof(c2->error_data.sense_data_buff))
  2029. data_len =
  2030. sizeof(c2->error_data.sense_data_buff);
  2031. memcpy(cmd->sense_buffer,
  2032. c2->error_data.sense_data_buff, data_len);
  2033. retry = 1;
  2034. break;
  2035. case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
  2036. retry = 1;
  2037. break;
  2038. case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
  2039. retry = 1;
  2040. break;
  2041. case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
  2042. retry = 1;
  2043. break;
  2044. case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
  2045. retry = 1;
  2046. break;
  2047. default:
  2048. retry = 1;
  2049. break;
  2050. }
  2051. break;
  2052. case IOACCEL2_SERV_RESPONSE_FAILURE:
  2053. switch (c2->error_data.status) {
  2054. case IOACCEL2_STATUS_SR_IO_ERROR:
  2055. case IOACCEL2_STATUS_SR_IO_ABORTED:
  2056. case IOACCEL2_STATUS_SR_OVERRUN:
  2057. retry = 1;
  2058. break;
  2059. case IOACCEL2_STATUS_SR_UNDERRUN:
  2060. cmd->result = (DID_OK << 16); /* host byte */
  2061. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  2062. ioaccel2_resid = get_unaligned_le32(
  2063. &c2->error_data.resid_cnt[0]);
  2064. scsi_set_resid(cmd, ioaccel2_resid);
  2065. break;
  2066. case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
  2067. case IOACCEL2_STATUS_SR_INVALID_DEVICE:
  2068. case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
  2069. /*
  2070. * Did an HBA disk disappear? We will eventually
  2071. * get a state change event from the controller but
  2072. * in the meantime, we need to tell the OS that the
  2073. * HBA disk is no longer there and stop I/O
  2074. * from going down. This allows the potential re-insert
  2075. * of the disk to get the same device node.
  2076. */
  2077. if (dev->physical_device && dev->expose_device) {
  2078. cmd->result = DID_NO_CONNECT << 16;
  2079. dev->removed = 1;
  2080. h->drv_req_rescan = 1;
  2081. dev_warn(&h->pdev->dev,
  2082. "%s: device is gone!\n", __func__);
  2083. } else
  2084. /*
  2085. * Retry by sending down the RAID path.
  2086. * We will get an event from ctlr to
  2087. * trigger rescan regardless.
  2088. */
  2089. retry = 1;
  2090. break;
  2091. default:
  2092. retry = 1;
  2093. }
  2094. break;
  2095. case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
  2096. break;
  2097. case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
  2098. break;
  2099. case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
  2100. retry = 1;
  2101. break;
  2102. case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
  2103. break;
  2104. default:
  2105. retry = 1;
  2106. break;
  2107. }
  2108. return retry; /* retry on raid path? */
  2109. }
  2110. static void hpsa_cmd_resolve_events(struct ctlr_info *h,
  2111. struct CommandList *c)
  2112. {
  2113. bool do_wake = false;
  2114. /*
  2115. * Prevent the following race in the abort handler:
  2116. *
  2117. * 1. LLD is requested to abort a SCSI command
  2118. * 2. The SCSI command completes
  2119. * 3. The struct CommandList associated with step 2 is made available
  2120. * 4. New I/O request to LLD to another LUN re-uses struct CommandList
  2121. * 5. Abort handler follows scsi_cmnd->host_scribble and
  2122. * finds struct CommandList and tries to aborts it
  2123. * Now we have aborted the wrong command.
  2124. *
  2125. * Reset c->scsi_cmd here so that the abort or reset handler will know
  2126. * this command has completed. Then, check to see if the handler is
  2127. * waiting for this command, and, if so, wake it.
  2128. */
  2129. c->scsi_cmd = SCSI_CMD_IDLE;
  2130. mb(); /* Declare command idle before checking for pending events. */
  2131. if (c->abort_pending) {
  2132. do_wake = true;
  2133. c->abort_pending = false;
  2134. }
  2135. if (c->reset_pending) {
  2136. unsigned long flags;
  2137. struct hpsa_scsi_dev_t *dev;
  2138. /*
  2139. * There appears to be a reset pending; lock the lock and
  2140. * reconfirm. If so, then decrement the count of outstanding
  2141. * commands and wake the reset command if this is the last one.
  2142. */
  2143. spin_lock_irqsave(&h->lock, flags);
  2144. dev = c->reset_pending; /* Re-fetch under the lock. */
  2145. if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
  2146. do_wake = true;
  2147. c->reset_pending = NULL;
  2148. spin_unlock_irqrestore(&h->lock, flags);
  2149. }
  2150. if (do_wake)
  2151. wake_up_all(&h->event_sync_wait_queue);
  2152. }
  2153. static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
  2154. struct CommandList *c)
  2155. {
  2156. hpsa_cmd_resolve_events(h, c);
  2157. cmd_tagged_free(h, c);
  2158. }
  2159. static void hpsa_cmd_free_and_done(struct ctlr_info *h,
  2160. struct CommandList *c, struct scsi_cmnd *cmd)
  2161. {
  2162. hpsa_cmd_resolve_and_free(h, c);
  2163. if (cmd && cmd->scsi_done)
  2164. cmd->scsi_done(cmd);
  2165. }
  2166. static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
  2167. {
  2168. INIT_WORK(&c->work, hpsa_command_resubmit_worker);
  2169. queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
  2170. }
  2171. static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
  2172. {
  2173. cmd->result = DID_ABORT << 16;
  2174. }
  2175. static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
  2176. struct scsi_cmnd *cmd)
  2177. {
  2178. hpsa_set_scsi_cmd_aborted(cmd);
  2179. dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
  2180. c->Request.CDB, c->err_info->ScsiStatus);
  2181. hpsa_cmd_resolve_and_free(h, c);
  2182. }
  2183. static void process_ioaccel2_completion(struct ctlr_info *h,
  2184. struct CommandList *c, struct scsi_cmnd *cmd,
  2185. struct hpsa_scsi_dev_t *dev)
  2186. {
  2187. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  2188. /* check for good status */
  2189. if (likely(c2->error_data.serv_response == 0 &&
  2190. c2->error_data.status == 0))
  2191. return hpsa_cmd_free_and_done(h, c, cmd);
  2192. /*
  2193. * Any RAID offload error results in retry which will use
  2194. * the normal I/O path so the controller can handle whatever's
  2195. * wrong.
  2196. */
  2197. if (is_logical_device(dev) &&
  2198. c2->error_data.serv_response ==
  2199. IOACCEL2_SERV_RESPONSE_FAILURE) {
  2200. if (c2->error_data.status ==
  2201. IOACCEL2_STATUS_SR_IOACCEL_DISABLED) {
  2202. dev->offload_enabled = 0;
  2203. dev->offload_to_be_enabled = 0;
  2204. }
  2205. return hpsa_retry_cmd(h, c);
  2206. }
  2207. if (handle_ioaccel_mode2_error(h, c, cmd, c2, dev))
  2208. return hpsa_retry_cmd(h, c);
  2209. return hpsa_cmd_free_and_done(h, c, cmd);
  2210. }
  2211. /* Returns 0 on success, < 0 otherwise. */
  2212. static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
  2213. struct CommandList *cp)
  2214. {
  2215. u8 tmf_status = cp->err_info->ScsiStatus;
  2216. switch (tmf_status) {
  2217. case CISS_TMF_COMPLETE:
  2218. /*
  2219. * CISS_TMF_COMPLETE never happens, instead,
  2220. * ei->CommandStatus == 0 for this case.
  2221. */
  2222. case CISS_TMF_SUCCESS:
  2223. return 0;
  2224. case CISS_TMF_INVALID_FRAME:
  2225. case CISS_TMF_NOT_SUPPORTED:
  2226. case CISS_TMF_FAILED:
  2227. case CISS_TMF_WRONG_LUN:
  2228. case CISS_TMF_OVERLAPPED_TAG:
  2229. break;
  2230. default:
  2231. dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
  2232. tmf_status);
  2233. break;
  2234. }
  2235. return -tmf_status;
  2236. }
  2237. static void complete_scsi_command(struct CommandList *cp)
  2238. {
  2239. struct scsi_cmnd *cmd;
  2240. struct ctlr_info *h;
  2241. struct ErrorInfo *ei;
  2242. struct hpsa_scsi_dev_t *dev;
  2243. struct io_accel2_cmd *c2;
  2244. u8 sense_key;
  2245. u8 asc; /* additional sense code */
  2246. u8 ascq; /* additional sense code qualifier */
  2247. unsigned long sense_data_size;
  2248. ei = cp->err_info;
  2249. cmd = cp->scsi_cmd;
  2250. h = cp->h;
  2251. if (!cmd->device) {
  2252. cmd->result = DID_NO_CONNECT << 16;
  2253. return hpsa_cmd_free_and_done(h, cp, cmd);
  2254. }
  2255. dev = cmd->device->hostdata;
  2256. if (!dev) {
  2257. cmd->result = DID_NO_CONNECT << 16;
  2258. return hpsa_cmd_free_and_done(h, cp, cmd);
  2259. }
  2260. c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
  2261. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  2262. if ((cp->cmd_type == CMD_SCSI) &&
  2263. (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
  2264. hpsa_unmap_sg_chain_block(h, cp);
  2265. if ((cp->cmd_type == CMD_IOACCEL2) &&
  2266. (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
  2267. hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
  2268. cmd->result = (DID_OK << 16); /* host byte */
  2269. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  2270. if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1) {
  2271. if (dev->physical_device && dev->expose_device &&
  2272. dev->removed) {
  2273. cmd->result = DID_NO_CONNECT << 16;
  2274. return hpsa_cmd_free_and_done(h, cp, cmd);
  2275. }
  2276. if (likely(cp->phys_disk != NULL))
  2277. atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
  2278. }
  2279. /*
  2280. * We check for lockup status here as it may be set for
  2281. * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
  2282. * fail_all_oustanding_cmds()
  2283. */
  2284. if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
  2285. /* DID_NO_CONNECT will prevent a retry */
  2286. cmd->result = DID_NO_CONNECT << 16;
  2287. return hpsa_cmd_free_and_done(h, cp, cmd);
  2288. }
  2289. if ((unlikely(hpsa_is_pending_event(cp)))) {
  2290. if (cp->reset_pending)
  2291. return hpsa_cmd_resolve_and_free(h, cp);
  2292. if (cp->abort_pending)
  2293. return hpsa_cmd_abort_and_free(h, cp, cmd);
  2294. }
  2295. if (cp->cmd_type == CMD_IOACCEL2)
  2296. return process_ioaccel2_completion(h, cp, cmd, dev);
  2297. scsi_set_resid(cmd, ei->ResidualCnt);
  2298. if (ei->CommandStatus == 0)
  2299. return hpsa_cmd_free_and_done(h, cp, cmd);
  2300. /* For I/O accelerator commands, copy over some fields to the normal
  2301. * CISS header used below for error handling.
  2302. */
  2303. if (cp->cmd_type == CMD_IOACCEL1) {
  2304. struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
  2305. cp->Header.SGList = scsi_sg_count(cmd);
  2306. cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
  2307. cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
  2308. IOACCEL1_IOFLAGS_CDBLEN_MASK;
  2309. cp->Header.tag = c->tag;
  2310. memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
  2311. memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
  2312. /* Any RAID offload error results in retry which will use
  2313. * the normal I/O path so the controller can handle whatever's
  2314. * wrong.
  2315. */
  2316. if (is_logical_device(dev)) {
  2317. if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
  2318. dev->offload_enabled = 0;
  2319. return hpsa_retry_cmd(h, cp);
  2320. }
  2321. }
  2322. /* an error has occurred */
  2323. switch (ei->CommandStatus) {
  2324. case CMD_TARGET_STATUS:
  2325. cmd->result |= ei->ScsiStatus;
  2326. /* copy the sense data */
  2327. if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
  2328. sense_data_size = SCSI_SENSE_BUFFERSIZE;
  2329. else
  2330. sense_data_size = sizeof(ei->SenseInfo);
  2331. if (ei->SenseLen < sense_data_size)
  2332. sense_data_size = ei->SenseLen;
  2333. memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
  2334. if (ei->ScsiStatus)
  2335. decode_sense_data(ei->SenseInfo, sense_data_size,
  2336. &sense_key, &asc, &ascq);
  2337. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  2338. if (sense_key == ABORTED_COMMAND) {
  2339. cmd->result |= DID_SOFT_ERROR << 16;
  2340. break;
  2341. }
  2342. break;
  2343. }
  2344. /* Problem was not a check condition
  2345. * Pass it up to the upper layers...
  2346. */
  2347. if (ei->ScsiStatus) {
  2348. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  2349. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  2350. "Returning result: 0x%x\n",
  2351. cp, ei->ScsiStatus,
  2352. sense_key, asc, ascq,
  2353. cmd->result);
  2354. } else { /* scsi status is zero??? How??? */
  2355. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  2356. "Returning no connection.\n", cp),
  2357. /* Ordinarily, this case should never happen,
  2358. * but there is a bug in some released firmware
  2359. * revisions that allows it to happen if, for
  2360. * example, a 4100 backplane loses power and
  2361. * the tape drive is in it. We assume that
  2362. * it's a fatal error of some kind because we
  2363. * can't show that it wasn't. We will make it
  2364. * look like selection timeout since that is
  2365. * the most common reason for this to occur,
  2366. * and it's severe enough.
  2367. */
  2368. cmd->result = DID_NO_CONNECT << 16;
  2369. }
  2370. break;
  2371. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  2372. break;
  2373. case CMD_DATA_OVERRUN:
  2374. dev_warn(&h->pdev->dev,
  2375. "CDB %16phN data overrun\n", cp->Request.CDB);
  2376. break;
  2377. case CMD_INVALID: {
  2378. /* print_bytes(cp, sizeof(*cp), 1, 0);
  2379. print_cmd(cp); */
  2380. /* We get CMD_INVALID if you address a non-existent device
  2381. * instead of a selection timeout (no response). You will
  2382. * see this if you yank out a drive, then try to access it.
  2383. * This is kind of a shame because it means that any other
  2384. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  2385. * missing target. */
  2386. cmd->result = DID_NO_CONNECT << 16;
  2387. }
  2388. break;
  2389. case CMD_PROTOCOL_ERR:
  2390. cmd->result = DID_ERROR << 16;
  2391. dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
  2392. cp->Request.CDB);
  2393. break;
  2394. case CMD_HARDWARE_ERR:
  2395. cmd->result = DID_ERROR << 16;
  2396. dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
  2397. cp->Request.CDB);
  2398. break;
  2399. case CMD_CONNECTION_LOST:
  2400. cmd->result = DID_ERROR << 16;
  2401. dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
  2402. cp->Request.CDB);
  2403. break;
  2404. case CMD_ABORTED:
  2405. /* Return now to avoid calling scsi_done(). */
  2406. return hpsa_cmd_abort_and_free(h, cp, cmd);
  2407. case CMD_ABORT_FAILED:
  2408. cmd->result = DID_ERROR << 16;
  2409. dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
  2410. cp->Request.CDB);
  2411. break;
  2412. case CMD_UNSOLICITED_ABORT:
  2413. cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
  2414. dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
  2415. cp->Request.CDB);
  2416. break;
  2417. case CMD_TIMEOUT:
  2418. cmd->result = DID_TIME_OUT << 16;
  2419. dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
  2420. cp->Request.CDB);
  2421. break;
  2422. case CMD_UNABORTABLE:
  2423. cmd->result = DID_ERROR << 16;
  2424. dev_warn(&h->pdev->dev, "Command unabortable\n");
  2425. break;
  2426. case CMD_TMF_STATUS:
  2427. if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
  2428. cmd->result = DID_ERROR << 16;
  2429. break;
  2430. case CMD_IOACCEL_DISABLED:
  2431. /* This only handles the direct pass-through case since RAID
  2432. * offload is handled above. Just attempt a retry.
  2433. */
  2434. cmd->result = DID_SOFT_ERROR << 16;
  2435. dev_warn(&h->pdev->dev,
  2436. "cp %p had HP SSD Smart Path error\n", cp);
  2437. break;
  2438. default:
  2439. cmd->result = DID_ERROR << 16;
  2440. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  2441. cp, ei->CommandStatus);
  2442. }
  2443. return hpsa_cmd_free_and_done(h, cp, cmd);
  2444. }
  2445. static void hpsa_pci_unmap(struct pci_dev *pdev,
  2446. struct CommandList *c, int sg_used, int data_direction)
  2447. {
  2448. int i;
  2449. for (i = 0; i < sg_used; i++)
  2450. pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
  2451. le32_to_cpu(c->SG[i].Len),
  2452. data_direction);
  2453. }
  2454. static int hpsa_map_one(struct pci_dev *pdev,
  2455. struct CommandList *cp,
  2456. unsigned char *buf,
  2457. size_t buflen,
  2458. int data_direction)
  2459. {
  2460. u64 addr64;
  2461. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  2462. cp->Header.SGList = 0;
  2463. cp->Header.SGTotal = cpu_to_le16(0);
  2464. return 0;
  2465. }
  2466. addr64 = pci_map_single(pdev, buf, buflen, data_direction);
  2467. if (dma_mapping_error(&pdev->dev, addr64)) {
  2468. /* Prevent subsequent unmap of something never mapped */
  2469. cp->Header.SGList = 0;
  2470. cp->Header.SGTotal = cpu_to_le16(0);
  2471. return -1;
  2472. }
  2473. cp->SG[0].Addr = cpu_to_le64(addr64);
  2474. cp->SG[0].Len = cpu_to_le32(buflen);
  2475. cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
  2476. cp->Header.SGList = 1; /* no. SGs contig in this cmd */
  2477. cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
  2478. return 0;
  2479. }
  2480. #define NO_TIMEOUT ((unsigned long) -1)
  2481. #define DEFAULT_TIMEOUT 30000 /* milliseconds */
  2482. static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  2483. struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
  2484. {
  2485. DECLARE_COMPLETION_ONSTACK(wait);
  2486. c->waiting = &wait;
  2487. __enqueue_cmd_and_start_io(h, c, reply_queue);
  2488. if (timeout_msecs == NO_TIMEOUT) {
  2489. /* TODO: get rid of this no-timeout thing */
  2490. wait_for_completion_io(&wait);
  2491. return IO_OK;
  2492. }
  2493. if (!wait_for_completion_io_timeout(&wait,
  2494. msecs_to_jiffies(timeout_msecs))) {
  2495. dev_warn(&h->pdev->dev, "Command timed out.\n");
  2496. return -ETIMEDOUT;
  2497. }
  2498. return IO_OK;
  2499. }
  2500. static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
  2501. int reply_queue, unsigned long timeout_msecs)
  2502. {
  2503. if (unlikely(lockup_detected(h))) {
  2504. c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
  2505. return IO_OK;
  2506. }
  2507. return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
  2508. }
  2509. static u32 lockup_detected(struct ctlr_info *h)
  2510. {
  2511. int cpu;
  2512. u32 rc, *lockup_detected;
  2513. cpu = get_cpu();
  2514. lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
  2515. rc = *lockup_detected;
  2516. put_cpu();
  2517. return rc;
  2518. }
  2519. #define MAX_DRIVER_CMD_RETRIES 25
  2520. static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  2521. struct CommandList *c, int data_direction, unsigned long timeout_msecs)
  2522. {
  2523. int backoff_time = 10, retry_count = 0;
  2524. int rc;
  2525. do {
  2526. memset(c->err_info, 0, sizeof(*c->err_info));
  2527. rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  2528. timeout_msecs);
  2529. if (rc)
  2530. break;
  2531. retry_count++;
  2532. if (retry_count > 3) {
  2533. msleep(backoff_time);
  2534. if (backoff_time < 1000)
  2535. backoff_time *= 2;
  2536. }
  2537. } while ((check_for_unit_attention(h, c) ||
  2538. check_for_busy(h, c)) &&
  2539. retry_count <= MAX_DRIVER_CMD_RETRIES);
  2540. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  2541. if (retry_count > MAX_DRIVER_CMD_RETRIES)
  2542. rc = -EIO;
  2543. return rc;
  2544. }
  2545. static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
  2546. struct CommandList *c)
  2547. {
  2548. const u8 *cdb = c->Request.CDB;
  2549. const u8 *lun = c->Header.LUN.LunAddrBytes;
  2550. dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
  2551. " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  2552. txt, lun[0], lun[1], lun[2], lun[3],
  2553. lun[4], lun[5], lun[6], lun[7],
  2554. cdb[0], cdb[1], cdb[2], cdb[3],
  2555. cdb[4], cdb[5], cdb[6], cdb[7],
  2556. cdb[8], cdb[9], cdb[10], cdb[11],
  2557. cdb[12], cdb[13], cdb[14], cdb[15]);
  2558. }
  2559. static void hpsa_scsi_interpret_error(struct ctlr_info *h,
  2560. struct CommandList *cp)
  2561. {
  2562. const struct ErrorInfo *ei = cp->err_info;
  2563. struct device *d = &cp->h->pdev->dev;
  2564. u8 sense_key, asc, ascq;
  2565. int sense_len;
  2566. switch (ei->CommandStatus) {
  2567. case CMD_TARGET_STATUS:
  2568. if (ei->SenseLen > sizeof(ei->SenseInfo))
  2569. sense_len = sizeof(ei->SenseInfo);
  2570. else
  2571. sense_len = ei->SenseLen;
  2572. decode_sense_data(ei->SenseInfo, sense_len,
  2573. &sense_key, &asc, &ascq);
  2574. hpsa_print_cmd(h, "SCSI status", cp);
  2575. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
  2576. dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
  2577. sense_key, asc, ascq);
  2578. else
  2579. dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
  2580. if (ei->ScsiStatus == 0)
  2581. dev_warn(d, "SCSI status is abnormally zero. "
  2582. "(probably indicates selection timeout "
  2583. "reported incorrectly due to a known "
  2584. "firmware bug, circa July, 2001.)\n");
  2585. break;
  2586. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  2587. break;
  2588. case CMD_DATA_OVERRUN:
  2589. hpsa_print_cmd(h, "overrun condition", cp);
  2590. break;
  2591. case CMD_INVALID: {
  2592. /* controller unfortunately reports SCSI passthru's
  2593. * to non-existent targets as invalid commands.
  2594. */
  2595. hpsa_print_cmd(h, "invalid command", cp);
  2596. dev_warn(d, "probably means device no longer present\n");
  2597. }
  2598. break;
  2599. case CMD_PROTOCOL_ERR:
  2600. hpsa_print_cmd(h, "protocol error", cp);
  2601. break;
  2602. case CMD_HARDWARE_ERR:
  2603. hpsa_print_cmd(h, "hardware error", cp);
  2604. break;
  2605. case CMD_CONNECTION_LOST:
  2606. hpsa_print_cmd(h, "connection lost", cp);
  2607. break;
  2608. case CMD_ABORTED:
  2609. hpsa_print_cmd(h, "aborted", cp);
  2610. break;
  2611. case CMD_ABORT_FAILED:
  2612. hpsa_print_cmd(h, "abort failed", cp);
  2613. break;
  2614. case CMD_UNSOLICITED_ABORT:
  2615. hpsa_print_cmd(h, "unsolicited abort", cp);
  2616. break;
  2617. case CMD_TIMEOUT:
  2618. hpsa_print_cmd(h, "timed out", cp);
  2619. break;
  2620. case CMD_UNABORTABLE:
  2621. hpsa_print_cmd(h, "unabortable", cp);
  2622. break;
  2623. case CMD_CTLR_LOCKUP:
  2624. hpsa_print_cmd(h, "controller lockup detected", cp);
  2625. break;
  2626. default:
  2627. hpsa_print_cmd(h, "unknown status", cp);
  2628. dev_warn(d, "Unknown command status %x\n",
  2629. ei->CommandStatus);
  2630. }
  2631. }
  2632. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  2633. u16 page, unsigned char *buf,
  2634. unsigned char bufsize)
  2635. {
  2636. int rc = IO_OK;
  2637. struct CommandList *c;
  2638. struct ErrorInfo *ei;
  2639. c = cmd_alloc(h);
  2640. if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
  2641. page, scsi3addr, TYPE_CMD)) {
  2642. rc = -1;
  2643. goto out;
  2644. }
  2645. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2646. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2647. if (rc)
  2648. goto out;
  2649. ei = c->err_info;
  2650. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2651. hpsa_scsi_interpret_error(h, c);
  2652. rc = -1;
  2653. }
  2654. out:
  2655. cmd_free(h, c);
  2656. return rc;
  2657. }
  2658. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
  2659. u8 reset_type, int reply_queue)
  2660. {
  2661. int rc = IO_OK;
  2662. struct CommandList *c;
  2663. struct ErrorInfo *ei;
  2664. c = cmd_alloc(h);
  2665. /* fill_cmd can't fail here, no data buffer to map. */
  2666. (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
  2667. scsi3addr, TYPE_MSG);
  2668. rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, NO_TIMEOUT);
  2669. if (rc) {
  2670. dev_warn(&h->pdev->dev, "Failed to send reset command\n");
  2671. goto out;
  2672. }
  2673. /* no unmap needed here because no data xfer. */
  2674. ei = c->err_info;
  2675. if (ei->CommandStatus != 0) {
  2676. hpsa_scsi_interpret_error(h, c);
  2677. rc = -1;
  2678. }
  2679. out:
  2680. cmd_free(h, c);
  2681. return rc;
  2682. }
  2683. static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
  2684. struct hpsa_scsi_dev_t *dev,
  2685. unsigned char *scsi3addr)
  2686. {
  2687. int i;
  2688. bool match = false;
  2689. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  2690. struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
  2691. if (hpsa_is_cmd_idle(c))
  2692. return false;
  2693. switch (c->cmd_type) {
  2694. case CMD_SCSI:
  2695. case CMD_IOCTL_PEND:
  2696. match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
  2697. sizeof(c->Header.LUN.LunAddrBytes));
  2698. break;
  2699. case CMD_IOACCEL1:
  2700. case CMD_IOACCEL2:
  2701. if (c->phys_disk == dev) {
  2702. /* HBA mode match */
  2703. match = true;
  2704. } else {
  2705. /* Possible RAID mode -- check each phys dev. */
  2706. /* FIXME: Do we need to take out a lock here? If
  2707. * so, we could just call hpsa_get_pdisk_of_ioaccel2()
  2708. * instead. */
  2709. for (i = 0; i < dev->nphysical_disks && !match; i++) {
  2710. /* FIXME: an alternate test might be
  2711. *
  2712. * match = dev->phys_disk[i]->ioaccel_handle
  2713. * == c2->scsi_nexus; */
  2714. match = dev->phys_disk[i] == c->phys_disk;
  2715. }
  2716. }
  2717. break;
  2718. case IOACCEL2_TMF:
  2719. for (i = 0; i < dev->nphysical_disks && !match; i++) {
  2720. match = dev->phys_disk[i]->ioaccel_handle ==
  2721. le32_to_cpu(ac->it_nexus);
  2722. }
  2723. break;
  2724. case 0: /* The command is in the middle of being initialized. */
  2725. match = false;
  2726. break;
  2727. default:
  2728. dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
  2729. c->cmd_type);
  2730. BUG();
  2731. }
  2732. return match;
  2733. }
  2734. static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
  2735. unsigned char *scsi3addr, u8 reset_type, int reply_queue)
  2736. {
  2737. int i;
  2738. int rc = 0;
  2739. /* We can really only handle one reset at a time */
  2740. if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
  2741. dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
  2742. return -EINTR;
  2743. }
  2744. BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
  2745. for (i = 0; i < h->nr_cmds; i++) {
  2746. struct CommandList *c = h->cmd_pool + i;
  2747. int refcount = atomic_inc_return(&c->refcount);
  2748. if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
  2749. unsigned long flags;
  2750. /*
  2751. * Mark the target command as having a reset pending,
  2752. * then lock a lock so that the command cannot complete
  2753. * while we're considering it. If the command is not
  2754. * idle then count it; otherwise revoke the event.
  2755. */
  2756. c->reset_pending = dev;
  2757. spin_lock_irqsave(&h->lock, flags); /* Implied MB */
  2758. if (!hpsa_is_cmd_idle(c))
  2759. atomic_inc(&dev->reset_cmds_out);
  2760. else
  2761. c->reset_pending = NULL;
  2762. spin_unlock_irqrestore(&h->lock, flags);
  2763. }
  2764. cmd_free(h, c);
  2765. }
  2766. rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
  2767. if (!rc)
  2768. wait_event(h->event_sync_wait_queue,
  2769. atomic_read(&dev->reset_cmds_out) == 0 ||
  2770. lockup_detected(h));
  2771. if (unlikely(lockup_detected(h))) {
  2772. dev_warn(&h->pdev->dev,
  2773. "Controller lockup detected during reset wait\n");
  2774. rc = -ENODEV;
  2775. }
  2776. if (unlikely(rc))
  2777. atomic_set(&dev->reset_cmds_out, 0);
  2778. mutex_unlock(&h->reset_mutex);
  2779. return rc;
  2780. }
  2781. static void hpsa_get_raid_level(struct ctlr_info *h,
  2782. unsigned char *scsi3addr, unsigned char *raid_level)
  2783. {
  2784. int rc;
  2785. unsigned char *buf;
  2786. *raid_level = RAID_UNKNOWN;
  2787. buf = kzalloc(64, GFP_KERNEL);
  2788. if (!buf)
  2789. return;
  2790. if (!hpsa_vpd_page_supported(h, scsi3addr,
  2791. HPSA_VPD_LV_DEVICE_GEOMETRY))
  2792. goto exit;
  2793. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
  2794. HPSA_VPD_LV_DEVICE_GEOMETRY, buf, 64);
  2795. if (rc == 0)
  2796. *raid_level = buf[8];
  2797. if (*raid_level > RAID_UNKNOWN)
  2798. *raid_level = RAID_UNKNOWN;
  2799. exit:
  2800. kfree(buf);
  2801. return;
  2802. }
  2803. #define HPSA_MAP_DEBUG
  2804. #ifdef HPSA_MAP_DEBUG
  2805. static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
  2806. struct raid_map_data *map_buff)
  2807. {
  2808. struct raid_map_disk_data *dd = &map_buff->data[0];
  2809. int map, row, col;
  2810. u16 map_cnt, row_cnt, disks_per_row;
  2811. if (rc != 0)
  2812. return;
  2813. /* Show details only if debugging has been activated. */
  2814. if (h->raid_offload_debug < 2)
  2815. return;
  2816. dev_info(&h->pdev->dev, "structure_size = %u\n",
  2817. le32_to_cpu(map_buff->structure_size));
  2818. dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
  2819. le32_to_cpu(map_buff->volume_blk_size));
  2820. dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
  2821. le64_to_cpu(map_buff->volume_blk_cnt));
  2822. dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
  2823. map_buff->phys_blk_shift);
  2824. dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
  2825. map_buff->parity_rotation_shift);
  2826. dev_info(&h->pdev->dev, "strip_size = %u\n",
  2827. le16_to_cpu(map_buff->strip_size));
  2828. dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
  2829. le64_to_cpu(map_buff->disk_starting_blk));
  2830. dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
  2831. le64_to_cpu(map_buff->disk_blk_cnt));
  2832. dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
  2833. le16_to_cpu(map_buff->data_disks_per_row));
  2834. dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
  2835. le16_to_cpu(map_buff->metadata_disks_per_row));
  2836. dev_info(&h->pdev->dev, "row_cnt = %u\n",
  2837. le16_to_cpu(map_buff->row_cnt));
  2838. dev_info(&h->pdev->dev, "layout_map_count = %u\n",
  2839. le16_to_cpu(map_buff->layout_map_count));
  2840. dev_info(&h->pdev->dev, "flags = 0x%x\n",
  2841. le16_to_cpu(map_buff->flags));
  2842. dev_info(&h->pdev->dev, "encrypytion = %s\n",
  2843. le16_to_cpu(map_buff->flags) &
  2844. RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
  2845. dev_info(&h->pdev->dev, "dekindex = %u\n",
  2846. le16_to_cpu(map_buff->dekindex));
  2847. map_cnt = le16_to_cpu(map_buff->layout_map_count);
  2848. for (map = 0; map < map_cnt; map++) {
  2849. dev_info(&h->pdev->dev, "Map%u:\n", map);
  2850. row_cnt = le16_to_cpu(map_buff->row_cnt);
  2851. for (row = 0; row < row_cnt; row++) {
  2852. dev_info(&h->pdev->dev, " Row%u:\n", row);
  2853. disks_per_row =
  2854. le16_to_cpu(map_buff->data_disks_per_row);
  2855. for (col = 0; col < disks_per_row; col++, dd++)
  2856. dev_info(&h->pdev->dev,
  2857. " D%02u: h=0x%04x xor=%u,%u\n",
  2858. col, dd->ioaccel_handle,
  2859. dd->xor_mult[0], dd->xor_mult[1]);
  2860. disks_per_row =
  2861. le16_to_cpu(map_buff->metadata_disks_per_row);
  2862. for (col = 0; col < disks_per_row; col++, dd++)
  2863. dev_info(&h->pdev->dev,
  2864. " M%02u: h=0x%04x xor=%u,%u\n",
  2865. col, dd->ioaccel_handle,
  2866. dd->xor_mult[0], dd->xor_mult[1]);
  2867. }
  2868. }
  2869. }
  2870. #else
  2871. static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
  2872. __attribute__((unused)) int rc,
  2873. __attribute__((unused)) struct raid_map_data *map_buff)
  2874. {
  2875. }
  2876. #endif
  2877. static int hpsa_get_raid_map(struct ctlr_info *h,
  2878. unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
  2879. {
  2880. int rc = 0;
  2881. struct CommandList *c;
  2882. struct ErrorInfo *ei;
  2883. c = cmd_alloc(h);
  2884. if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
  2885. sizeof(this_device->raid_map), 0,
  2886. scsi3addr, TYPE_CMD)) {
  2887. dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
  2888. cmd_free(h, c);
  2889. return -1;
  2890. }
  2891. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2892. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2893. if (rc)
  2894. goto out;
  2895. ei = c->err_info;
  2896. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2897. hpsa_scsi_interpret_error(h, c);
  2898. rc = -1;
  2899. goto out;
  2900. }
  2901. cmd_free(h, c);
  2902. /* @todo in the future, dynamically allocate RAID map memory */
  2903. if (le32_to_cpu(this_device->raid_map.structure_size) >
  2904. sizeof(this_device->raid_map)) {
  2905. dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
  2906. rc = -1;
  2907. }
  2908. hpsa_debug_map_buff(h, rc, &this_device->raid_map);
  2909. return rc;
  2910. out:
  2911. cmd_free(h, c);
  2912. return rc;
  2913. }
  2914. static int hpsa_bmic_sense_subsystem_information(struct ctlr_info *h,
  2915. unsigned char scsi3addr[], u16 bmic_device_index,
  2916. struct bmic_sense_subsystem_info *buf, size_t bufsize)
  2917. {
  2918. int rc = IO_OK;
  2919. struct CommandList *c;
  2920. struct ErrorInfo *ei;
  2921. c = cmd_alloc(h);
  2922. rc = fill_cmd(c, BMIC_SENSE_SUBSYSTEM_INFORMATION, h, buf, bufsize,
  2923. 0, RAID_CTLR_LUNID, TYPE_CMD);
  2924. if (rc)
  2925. goto out;
  2926. c->Request.CDB[2] = bmic_device_index & 0xff;
  2927. c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
  2928. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2929. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2930. if (rc)
  2931. goto out;
  2932. ei = c->err_info;
  2933. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2934. hpsa_scsi_interpret_error(h, c);
  2935. rc = -1;
  2936. }
  2937. out:
  2938. cmd_free(h, c);
  2939. return rc;
  2940. }
  2941. static int hpsa_bmic_id_controller(struct ctlr_info *h,
  2942. struct bmic_identify_controller *buf, size_t bufsize)
  2943. {
  2944. int rc = IO_OK;
  2945. struct CommandList *c;
  2946. struct ErrorInfo *ei;
  2947. c = cmd_alloc(h);
  2948. rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
  2949. 0, RAID_CTLR_LUNID, TYPE_CMD);
  2950. if (rc)
  2951. goto out;
  2952. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2953. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2954. if (rc)
  2955. goto out;
  2956. ei = c->err_info;
  2957. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2958. hpsa_scsi_interpret_error(h, c);
  2959. rc = -1;
  2960. }
  2961. out:
  2962. cmd_free(h, c);
  2963. return rc;
  2964. }
  2965. static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
  2966. unsigned char scsi3addr[], u16 bmic_device_index,
  2967. struct bmic_identify_physical_device *buf, size_t bufsize)
  2968. {
  2969. int rc = IO_OK;
  2970. struct CommandList *c;
  2971. struct ErrorInfo *ei;
  2972. c = cmd_alloc(h);
  2973. rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
  2974. 0, RAID_CTLR_LUNID, TYPE_CMD);
  2975. if (rc)
  2976. goto out;
  2977. c->Request.CDB[2] = bmic_device_index & 0xff;
  2978. c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
  2979. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
  2980. DEFAULT_TIMEOUT);
  2981. ei = c->err_info;
  2982. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2983. hpsa_scsi_interpret_error(h, c);
  2984. rc = -1;
  2985. }
  2986. out:
  2987. cmd_free(h, c);
  2988. return rc;
  2989. }
  2990. /*
  2991. * get enclosure information
  2992. * struct ReportExtendedLUNdata *rlep - Used for BMIC drive number
  2993. * struct hpsa_scsi_dev_t *encl_dev - device entry for enclosure
  2994. * Uses id_physical_device to determine the box_index.
  2995. */
  2996. static void hpsa_get_enclosure_info(struct ctlr_info *h,
  2997. unsigned char *scsi3addr,
  2998. struct ReportExtendedLUNdata *rlep, int rle_index,
  2999. struct hpsa_scsi_dev_t *encl_dev)
  3000. {
  3001. int rc = -1;
  3002. struct CommandList *c = NULL;
  3003. struct ErrorInfo *ei = NULL;
  3004. struct bmic_sense_storage_box_params *bssbp = NULL;
  3005. struct bmic_identify_physical_device *id_phys = NULL;
  3006. struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
  3007. u16 bmic_device_index = 0;
  3008. bmic_device_index = GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]);
  3009. if (bmic_device_index == 0xFF00 || MASKED_DEVICE(&rle->lunid[0])) {
  3010. rc = IO_OK;
  3011. goto out;
  3012. }
  3013. bssbp = kzalloc(sizeof(*bssbp), GFP_KERNEL);
  3014. if (!bssbp)
  3015. goto out;
  3016. id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
  3017. if (!id_phys)
  3018. goto out;
  3019. rc = hpsa_bmic_id_physical_device(h, scsi3addr, bmic_device_index,
  3020. id_phys, sizeof(*id_phys));
  3021. if (rc) {
  3022. dev_warn(&h->pdev->dev, "%s: id_phys failed %d bdi[0x%x]\n",
  3023. __func__, encl_dev->external, bmic_device_index);
  3024. goto out;
  3025. }
  3026. c = cmd_alloc(h);
  3027. rc = fill_cmd(c, BMIC_SENSE_STORAGE_BOX_PARAMS, h, bssbp,
  3028. sizeof(*bssbp), 0, RAID_CTLR_LUNID, TYPE_CMD);
  3029. if (rc)
  3030. goto out;
  3031. if (id_phys->phys_connector[1] == 'E')
  3032. c->Request.CDB[5] = id_phys->box_index;
  3033. else
  3034. c->Request.CDB[5] = 0;
  3035. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
  3036. DEFAULT_TIMEOUT);
  3037. if (rc)
  3038. goto out;
  3039. ei = c->err_info;
  3040. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  3041. rc = -1;
  3042. goto out;
  3043. }
  3044. encl_dev->box[id_phys->active_path_number] = bssbp->phys_box_on_port;
  3045. memcpy(&encl_dev->phys_connector[id_phys->active_path_number],
  3046. bssbp->phys_connector, sizeof(bssbp->phys_connector));
  3047. rc = IO_OK;
  3048. out:
  3049. kfree(bssbp);
  3050. kfree(id_phys);
  3051. if (c)
  3052. cmd_free(h, c);
  3053. if (rc != IO_OK)
  3054. hpsa_show_dev_msg(KERN_INFO, h, encl_dev,
  3055. "Error, could not get enclosure information\n");
  3056. }
  3057. static u64 hpsa_get_sas_address_from_report_physical(struct ctlr_info *h,
  3058. unsigned char *scsi3addr)
  3059. {
  3060. struct ReportExtendedLUNdata *physdev;
  3061. u32 nphysicals;
  3062. u64 sa = 0;
  3063. int i;
  3064. physdev = kzalloc(sizeof(*physdev), GFP_KERNEL);
  3065. if (!physdev)
  3066. return 0;
  3067. if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
  3068. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  3069. kfree(physdev);
  3070. return 0;
  3071. }
  3072. nphysicals = get_unaligned_be32(physdev->LUNListLength) / 24;
  3073. for (i = 0; i < nphysicals; i++)
  3074. if (!memcmp(&physdev->LUN[i].lunid[0], scsi3addr, 8)) {
  3075. sa = get_unaligned_be64(&physdev->LUN[i].wwid[0]);
  3076. break;
  3077. }
  3078. kfree(physdev);
  3079. return sa;
  3080. }
  3081. static void hpsa_get_sas_address(struct ctlr_info *h, unsigned char *scsi3addr,
  3082. struct hpsa_scsi_dev_t *dev)
  3083. {
  3084. int rc;
  3085. u64 sa = 0;
  3086. if (is_hba_lunid(scsi3addr)) {
  3087. struct bmic_sense_subsystem_info *ssi;
  3088. ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
  3089. if (ssi == NULL) {
  3090. dev_warn(&h->pdev->dev,
  3091. "%s: out of memory\n", __func__);
  3092. return;
  3093. }
  3094. rc = hpsa_bmic_sense_subsystem_information(h,
  3095. scsi3addr, 0, ssi, sizeof(*ssi));
  3096. if (rc == 0) {
  3097. sa = get_unaligned_be64(ssi->primary_world_wide_id);
  3098. h->sas_address = sa;
  3099. }
  3100. kfree(ssi);
  3101. } else
  3102. sa = hpsa_get_sas_address_from_report_physical(h, scsi3addr);
  3103. dev->sas_address = sa;
  3104. }
  3105. /* Get a device id from inquiry page 0x83 */
  3106. static bool hpsa_vpd_page_supported(struct ctlr_info *h,
  3107. unsigned char scsi3addr[], u8 page)
  3108. {
  3109. int rc;
  3110. int i;
  3111. int pages;
  3112. unsigned char *buf, bufsize;
  3113. buf = kzalloc(256, GFP_KERNEL);
  3114. if (!buf)
  3115. return false;
  3116. /* Get the size of the page list first */
  3117. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  3118. VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
  3119. buf, HPSA_VPD_HEADER_SZ);
  3120. if (rc != 0)
  3121. goto exit_unsupported;
  3122. pages = buf[3];
  3123. if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
  3124. bufsize = pages + HPSA_VPD_HEADER_SZ;
  3125. else
  3126. bufsize = 255;
  3127. /* Get the whole VPD page list */
  3128. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  3129. VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
  3130. buf, bufsize);
  3131. if (rc != 0)
  3132. goto exit_unsupported;
  3133. pages = buf[3];
  3134. for (i = 1; i <= pages; i++)
  3135. if (buf[3 + i] == page)
  3136. goto exit_supported;
  3137. exit_unsupported:
  3138. kfree(buf);
  3139. return false;
  3140. exit_supported:
  3141. kfree(buf);
  3142. return true;
  3143. }
  3144. static void hpsa_get_ioaccel_status(struct ctlr_info *h,
  3145. unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
  3146. {
  3147. int rc;
  3148. unsigned char *buf;
  3149. u8 ioaccel_status;
  3150. this_device->offload_config = 0;
  3151. this_device->offload_enabled = 0;
  3152. this_device->offload_to_be_enabled = 0;
  3153. buf = kzalloc(64, GFP_KERNEL);
  3154. if (!buf)
  3155. return;
  3156. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
  3157. goto out;
  3158. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  3159. VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
  3160. if (rc != 0)
  3161. goto out;
  3162. #define IOACCEL_STATUS_BYTE 4
  3163. #define OFFLOAD_CONFIGURED_BIT 0x01
  3164. #define OFFLOAD_ENABLED_BIT 0x02
  3165. ioaccel_status = buf[IOACCEL_STATUS_BYTE];
  3166. this_device->offload_config =
  3167. !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
  3168. if (this_device->offload_config) {
  3169. this_device->offload_enabled =
  3170. !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
  3171. if (hpsa_get_raid_map(h, scsi3addr, this_device))
  3172. this_device->offload_enabled = 0;
  3173. }
  3174. this_device->offload_to_be_enabled = this_device->offload_enabled;
  3175. out:
  3176. kfree(buf);
  3177. return;
  3178. }
  3179. /* Get the device id from inquiry page 0x83 */
  3180. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  3181. unsigned char *device_id, int index, int buflen)
  3182. {
  3183. int rc;
  3184. unsigned char *buf;
  3185. /* Does controller have VPD for device id? */
  3186. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_DEVICE_ID))
  3187. return 1; /* not supported */
  3188. buf = kzalloc(64, GFP_KERNEL);
  3189. if (!buf)
  3190. return -ENOMEM;
  3191. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
  3192. HPSA_VPD_LV_DEVICE_ID, buf, 64);
  3193. if (rc == 0) {
  3194. if (buflen > 16)
  3195. buflen = 16;
  3196. memcpy(device_id, &buf[8], buflen);
  3197. }
  3198. kfree(buf);
  3199. return rc; /*0 - got id, otherwise, didn't */
  3200. }
  3201. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  3202. void *buf, int bufsize,
  3203. int extended_response)
  3204. {
  3205. int rc = IO_OK;
  3206. struct CommandList *c;
  3207. unsigned char scsi3addr[8];
  3208. struct ErrorInfo *ei;
  3209. c = cmd_alloc(h);
  3210. /* address the controller */
  3211. memset(scsi3addr, 0, sizeof(scsi3addr));
  3212. if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  3213. buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
  3214. rc = -1;
  3215. goto out;
  3216. }
  3217. if (extended_response)
  3218. c->Request.CDB[1] = extended_response;
  3219. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  3220. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  3221. if (rc)
  3222. goto out;
  3223. ei = c->err_info;
  3224. if (ei->CommandStatus != 0 &&
  3225. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  3226. hpsa_scsi_interpret_error(h, c);
  3227. rc = -1;
  3228. } else {
  3229. struct ReportLUNdata *rld = buf;
  3230. if (rld->extended_response_flag != extended_response) {
  3231. dev_err(&h->pdev->dev,
  3232. "report luns requested format %u, got %u\n",
  3233. extended_response,
  3234. rld->extended_response_flag);
  3235. rc = -1;
  3236. }
  3237. }
  3238. out:
  3239. cmd_free(h, c);
  3240. return rc;
  3241. }
  3242. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  3243. struct ReportExtendedLUNdata *buf, int bufsize)
  3244. {
  3245. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
  3246. HPSA_REPORT_PHYS_EXTENDED);
  3247. }
  3248. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  3249. struct ReportLUNdata *buf, int bufsize)
  3250. {
  3251. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  3252. }
  3253. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  3254. int bus, int target, int lun)
  3255. {
  3256. device->bus = bus;
  3257. device->target = target;
  3258. device->lun = lun;
  3259. }
  3260. /* Use VPD inquiry to get details of volume status */
  3261. static int hpsa_get_volume_status(struct ctlr_info *h,
  3262. unsigned char scsi3addr[])
  3263. {
  3264. int rc;
  3265. int status;
  3266. int size;
  3267. unsigned char *buf;
  3268. buf = kzalloc(64, GFP_KERNEL);
  3269. if (!buf)
  3270. return HPSA_VPD_LV_STATUS_UNSUPPORTED;
  3271. /* Does controller have VPD for logical volume status? */
  3272. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
  3273. goto exit_failed;
  3274. /* Get the size of the VPD return buffer */
  3275. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
  3276. buf, HPSA_VPD_HEADER_SZ);
  3277. if (rc != 0)
  3278. goto exit_failed;
  3279. size = buf[3];
  3280. /* Now get the whole VPD buffer */
  3281. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
  3282. buf, size + HPSA_VPD_HEADER_SZ);
  3283. if (rc != 0)
  3284. goto exit_failed;
  3285. status = buf[4]; /* status byte */
  3286. kfree(buf);
  3287. return status;
  3288. exit_failed:
  3289. kfree(buf);
  3290. return HPSA_VPD_LV_STATUS_UNSUPPORTED;
  3291. }
  3292. /* Determine offline status of a volume.
  3293. * Return either:
  3294. * 0 (not offline)
  3295. * 0xff (offline for unknown reasons)
  3296. * # (integer code indicating one of several NOT READY states
  3297. * describing why a volume is to be kept offline)
  3298. */
  3299. static unsigned char hpsa_volume_offline(struct ctlr_info *h,
  3300. unsigned char scsi3addr[])
  3301. {
  3302. struct CommandList *c;
  3303. unsigned char *sense;
  3304. u8 sense_key, asc, ascq;
  3305. int sense_len;
  3306. int rc, ldstat = 0;
  3307. u16 cmd_status;
  3308. u8 scsi_status;
  3309. #define ASC_LUN_NOT_READY 0x04
  3310. #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
  3311. #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
  3312. c = cmd_alloc(h);
  3313. (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
  3314. rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  3315. DEFAULT_TIMEOUT);
  3316. if (rc) {
  3317. cmd_free(h, c);
  3318. return HPSA_VPD_LV_STATUS_UNSUPPORTED;
  3319. }
  3320. sense = c->err_info->SenseInfo;
  3321. if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
  3322. sense_len = sizeof(c->err_info->SenseInfo);
  3323. else
  3324. sense_len = c->err_info->SenseLen;
  3325. decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
  3326. cmd_status = c->err_info->CommandStatus;
  3327. scsi_status = c->err_info->ScsiStatus;
  3328. cmd_free(h, c);
  3329. /* Determine the reason for not ready state */
  3330. ldstat = hpsa_get_volume_status(h, scsi3addr);
  3331. /* Keep volume offline in certain cases: */
  3332. switch (ldstat) {
  3333. case HPSA_LV_FAILED:
  3334. case HPSA_LV_UNDERGOING_ERASE:
  3335. case HPSA_LV_NOT_AVAILABLE:
  3336. case HPSA_LV_UNDERGOING_RPI:
  3337. case HPSA_LV_PENDING_RPI:
  3338. case HPSA_LV_ENCRYPTED_NO_KEY:
  3339. case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
  3340. case HPSA_LV_UNDERGOING_ENCRYPTION:
  3341. case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
  3342. case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
  3343. return ldstat;
  3344. case HPSA_VPD_LV_STATUS_UNSUPPORTED:
  3345. /* If VPD status page isn't available,
  3346. * use ASC/ASCQ to determine state
  3347. */
  3348. if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
  3349. (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
  3350. return ldstat;
  3351. break;
  3352. default:
  3353. break;
  3354. }
  3355. return HPSA_LV_OK;
  3356. }
  3357. /*
  3358. * Find out if a logical device supports aborts by simply trying one.
  3359. * Smart Array may claim not to support aborts on logical drives, but
  3360. * if a MSA2000 * is connected, the drives on that will be presented
  3361. * by the Smart Array as logical drives, and aborts may be sent to
  3362. * those devices successfully. So the simplest way to find out is
  3363. * to simply try an abort and see how the device responds.
  3364. */
  3365. static int hpsa_device_supports_aborts(struct ctlr_info *h,
  3366. unsigned char *scsi3addr)
  3367. {
  3368. struct CommandList *c;
  3369. struct ErrorInfo *ei;
  3370. int rc = 0;
  3371. u64 tag = (u64) -1; /* bogus tag */
  3372. /* Assume that physical devices support aborts */
  3373. if (!is_logical_dev_addr_mode(scsi3addr))
  3374. return 1;
  3375. c = cmd_alloc(h);
  3376. (void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
  3377. (void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  3378. DEFAULT_TIMEOUT);
  3379. /* no unmap needed here because no data xfer. */
  3380. ei = c->err_info;
  3381. switch (ei->CommandStatus) {
  3382. case CMD_INVALID:
  3383. rc = 0;
  3384. break;
  3385. case CMD_UNABORTABLE:
  3386. case CMD_ABORT_FAILED:
  3387. rc = 1;
  3388. break;
  3389. case CMD_TMF_STATUS:
  3390. rc = hpsa_evaluate_tmf_status(h, c);
  3391. break;
  3392. default:
  3393. rc = 0;
  3394. break;
  3395. }
  3396. cmd_free(h, c);
  3397. return rc;
  3398. }
  3399. static int hpsa_update_device_info(struct ctlr_info *h,
  3400. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
  3401. unsigned char *is_OBDR_device)
  3402. {
  3403. #define OBDR_SIG_OFFSET 43
  3404. #define OBDR_TAPE_SIG "$DR-10"
  3405. #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
  3406. #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
  3407. unsigned char *inq_buff;
  3408. unsigned char *obdr_sig;
  3409. int rc = 0;
  3410. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  3411. if (!inq_buff) {
  3412. rc = -ENOMEM;
  3413. goto bail_out;
  3414. }
  3415. /* Do an inquiry to the device to see what it is. */
  3416. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  3417. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  3418. dev_err(&h->pdev->dev,
  3419. "%s: inquiry failed, device will be skipped.\n",
  3420. __func__);
  3421. rc = HPSA_INQUIRY_FAILED;
  3422. goto bail_out;
  3423. }
  3424. scsi_sanitize_inquiry_string(&inq_buff[8], 8);
  3425. scsi_sanitize_inquiry_string(&inq_buff[16], 16);
  3426. this_device->devtype = (inq_buff[0] & 0x1f);
  3427. memcpy(this_device->scsi3addr, scsi3addr, 8);
  3428. memcpy(this_device->vendor, &inq_buff[8],
  3429. sizeof(this_device->vendor));
  3430. memcpy(this_device->model, &inq_buff[16],
  3431. sizeof(this_device->model));
  3432. this_device->rev = inq_buff[2];
  3433. memset(this_device->device_id, 0,
  3434. sizeof(this_device->device_id));
  3435. if (hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
  3436. sizeof(this_device->device_id)))
  3437. dev_err(&h->pdev->dev,
  3438. "hpsa%d: %s: can't get device id for host %d:C0:T%d:L%d\t%s\t%.16s\n",
  3439. h->ctlr, __func__,
  3440. h->scsi_host->host_no,
  3441. this_device->target, this_device->lun,
  3442. scsi_device_type(this_device->devtype),
  3443. this_device->model);
  3444. if ((this_device->devtype == TYPE_DISK ||
  3445. this_device->devtype == TYPE_ZBC) &&
  3446. is_logical_dev_addr_mode(scsi3addr)) {
  3447. unsigned char volume_offline;
  3448. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  3449. if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
  3450. hpsa_get_ioaccel_status(h, scsi3addr, this_device);
  3451. volume_offline = hpsa_volume_offline(h, scsi3addr);
  3452. this_device->volume_offline = volume_offline;
  3453. if (volume_offline == HPSA_LV_FAILED) {
  3454. rc = HPSA_LV_FAILED;
  3455. dev_err(&h->pdev->dev,
  3456. "%s: LV failed, device will be skipped.\n",
  3457. __func__);
  3458. goto bail_out;
  3459. }
  3460. } else {
  3461. this_device->raid_level = RAID_UNKNOWN;
  3462. this_device->offload_config = 0;
  3463. this_device->offload_enabled = 0;
  3464. this_device->offload_to_be_enabled = 0;
  3465. this_device->hba_ioaccel_enabled = 0;
  3466. this_device->volume_offline = 0;
  3467. this_device->queue_depth = h->nr_cmds;
  3468. }
  3469. if (is_OBDR_device) {
  3470. /* See if this is a One-Button-Disaster-Recovery device
  3471. * by looking for "$DR-10" at offset 43 in inquiry data.
  3472. */
  3473. obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
  3474. *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
  3475. strncmp(obdr_sig, OBDR_TAPE_SIG,
  3476. OBDR_SIG_LEN) == 0);
  3477. }
  3478. kfree(inq_buff);
  3479. return 0;
  3480. bail_out:
  3481. kfree(inq_buff);
  3482. return rc;
  3483. }
  3484. static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
  3485. struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
  3486. {
  3487. unsigned long flags;
  3488. int rc, entry;
  3489. /*
  3490. * See if this device supports aborts. If we already know
  3491. * the device, we already know if it supports aborts, otherwise
  3492. * we have to find out if it supports aborts by trying one.
  3493. */
  3494. spin_lock_irqsave(&h->devlock, flags);
  3495. rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
  3496. if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
  3497. entry >= 0 && entry < h->ndevices) {
  3498. dev->supports_aborts = h->dev[entry]->supports_aborts;
  3499. spin_unlock_irqrestore(&h->devlock, flags);
  3500. } else {
  3501. spin_unlock_irqrestore(&h->devlock, flags);
  3502. dev->supports_aborts =
  3503. hpsa_device_supports_aborts(h, scsi3addr);
  3504. if (dev->supports_aborts < 0)
  3505. dev->supports_aborts = 0;
  3506. }
  3507. }
  3508. /*
  3509. * Helper function to assign bus, target, lun mapping of devices.
  3510. * Logical drive target and lun are assigned at this time, but
  3511. * physical device lun and target assignment are deferred (assigned
  3512. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  3513. */
  3514. static void figure_bus_target_lun(struct ctlr_info *h,
  3515. u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
  3516. {
  3517. u32 lunid = get_unaligned_le32(lunaddrbytes);
  3518. if (!is_logical_dev_addr_mode(lunaddrbytes)) {
  3519. /* physical device, target and lun filled in later */
  3520. if (is_hba_lunid(lunaddrbytes)) {
  3521. int bus = HPSA_HBA_BUS;
  3522. if (!device->rev)
  3523. bus = HPSA_LEGACY_HBA_BUS;
  3524. hpsa_set_bus_target_lun(device,
  3525. bus, 0, lunid & 0x3fff);
  3526. } else
  3527. /* defer target, lun assignment for physical devices */
  3528. hpsa_set_bus_target_lun(device,
  3529. HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
  3530. return;
  3531. }
  3532. /* It's a logical device */
  3533. if (device->external) {
  3534. hpsa_set_bus_target_lun(device,
  3535. HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
  3536. lunid & 0x00ff);
  3537. return;
  3538. }
  3539. hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
  3540. 0, lunid & 0x3fff);
  3541. }
  3542. /*
  3543. * Get address of physical disk used for an ioaccel2 mode command:
  3544. * 1. Extract ioaccel2 handle from the command.
  3545. * 2. Find a matching ioaccel2 handle from list of physical disks.
  3546. * 3. Return:
  3547. * 1 and set scsi3addr to address of matching physical
  3548. * 0 if no matching physical disk was found.
  3549. */
  3550. static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
  3551. struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
  3552. {
  3553. struct io_accel2_cmd *c2 =
  3554. &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
  3555. unsigned long flags;
  3556. int i;
  3557. spin_lock_irqsave(&h->devlock, flags);
  3558. for (i = 0; i < h->ndevices; i++)
  3559. if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
  3560. memcpy(scsi3addr, h->dev[i]->scsi3addr,
  3561. sizeof(h->dev[i]->scsi3addr));
  3562. spin_unlock_irqrestore(&h->devlock, flags);
  3563. return 1;
  3564. }
  3565. spin_unlock_irqrestore(&h->devlock, flags);
  3566. return 0;
  3567. }
  3568. static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
  3569. int i, int nphysicals, int nlocal_logicals)
  3570. {
  3571. /* In report logicals, local logicals are listed first,
  3572. * then any externals.
  3573. */
  3574. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  3575. if (i == raid_ctlr_position)
  3576. return 0;
  3577. if (i < logicals_start)
  3578. return 0;
  3579. /* i is in logicals range, but still within local logicals */
  3580. if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
  3581. return 0;
  3582. return 1; /* it's an external lun */
  3583. }
  3584. /*
  3585. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  3586. * logdev. The number of luns in physdev and logdev are returned in
  3587. * *nphysicals and *nlogicals, respectively.
  3588. * Returns 0 on success, -1 otherwise.
  3589. */
  3590. static int hpsa_gather_lun_info(struct ctlr_info *h,
  3591. struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
  3592. struct ReportLUNdata *logdev, u32 *nlogicals)
  3593. {
  3594. if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
  3595. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  3596. return -1;
  3597. }
  3598. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
  3599. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  3600. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
  3601. HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
  3602. *nphysicals = HPSA_MAX_PHYS_LUN;
  3603. }
  3604. if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
  3605. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  3606. return -1;
  3607. }
  3608. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  3609. /* Reject Logicals in excess of our max capability. */
  3610. if (*nlogicals > HPSA_MAX_LUN) {
  3611. dev_warn(&h->pdev->dev,
  3612. "maximum logical LUNs (%d) exceeded. "
  3613. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  3614. *nlogicals - HPSA_MAX_LUN);
  3615. *nlogicals = HPSA_MAX_LUN;
  3616. }
  3617. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  3618. dev_warn(&h->pdev->dev,
  3619. "maximum logical + physical LUNs (%d) exceeded. "
  3620. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  3621. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  3622. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  3623. }
  3624. return 0;
  3625. }
  3626. static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
  3627. int i, int nphysicals, int nlogicals,
  3628. struct ReportExtendedLUNdata *physdev_list,
  3629. struct ReportLUNdata *logdev_list)
  3630. {
  3631. /* Helper function, figure out where the LUN ID info is coming from
  3632. * given index i, lists of physical and logical devices, where in
  3633. * the list the raid controller is supposed to appear (first or last)
  3634. */
  3635. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  3636. int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
  3637. if (i == raid_ctlr_position)
  3638. return RAID_CTLR_LUNID;
  3639. if (i < logicals_start)
  3640. return &physdev_list->LUN[i -
  3641. (raid_ctlr_position == 0)].lunid[0];
  3642. if (i < last_device)
  3643. return &logdev_list->LUN[i - nphysicals -
  3644. (raid_ctlr_position == 0)][0];
  3645. BUG();
  3646. return NULL;
  3647. }
  3648. /* get physical drive ioaccel handle and queue depth */
  3649. static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
  3650. struct hpsa_scsi_dev_t *dev,
  3651. struct ReportExtendedLUNdata *rlep, int rle_index,
  3652. struct bmic_identify_physical_device *id_phys)
  3653. {
  3654. int rc;
  3655. struct ext_report_lun_entry *rle;
  3656. /*
  3657. * external targets don't support BMIC
  3658. */
  3659. if (dev->external) {
  3660. dev->queue_depth = 7;
  3661. return;
  3662. }
  3663. rle = &rlep->LUN[rle_index];
  3664. dev->ioaccel_handle = rle->ioaccel_handle;
  3665. if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
  3666. dev->hba_ioaccel_enabled = 1;
  3667. memset(id_phys, 0, sizeof(*id_phys));
  3668. rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
  3669. GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
  3670. sizeof(*id_phys));
  3671. if (!rc)
  3672. /* Reserve space for FW operations */
  3673. #define DRIVE_CMDS_RESERVED_FOR_FW 2
  3674. #define DRIVE_QUEUE_DEPTH 7
  3675. dev->queue_depth =
  3676. le16_to_cpu(id_phys->current_queue_depth_limit) -
  3677. DRIVE_CMDS_RESERVED_FOR_FW;
  3678. else
  3679. dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
  3680. }
  3681. static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
  3682. struct ReportExtendedLUNdata *rlep, int rle_index,
  3683. struct bmic_identify_physical_device *id_phys)
  3684. {
  3685. struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
  3686. if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
  3687. this_device->hba_ioaccel_enabled = 1;
  3688. memcpy(&this_device->active_path_index,
  3689. &id_phys->active_path_number,
  3690. sizeof(this_device->active_path_index));
  3691. memcpy(&this_device->path_map,
  3692. &id_phys->redundant_path_present_map,
  3693. sizeof(this_device->path_map));
  3694. memcpy(&this_device->box,
  3695. &id_phys->alternate_paths_phys_box_on_port,
  3696. sizeof(this_device->box));
  3697. memcpy(&this_device->phys_connector,
  3698. &id_phys->alternate_paths_phys_connector,
  3699. sizeof(this_device->phys_connector));
  3700. memcpy(&this_device->bay,
  3701. &id_phys->phys_bay_in_box,
  3702. sizeof(this_device->bay));
  3703. }
  3704. /* get number of local logical disks. */
  3705. static int hpsa_set_local_logical_count(struct ctlr_info *h,
  3706. struct bmic_identify_controller *id_ctlr,
  3707. u32 *nlocals)
  3708. {
  3709. int rc;
  3710. if (!id_ctlr) {
  3711. dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
  3712. __func__);
  3713. return -ENOMEM;
  3714. }
  3715. memset(id_ctlr, 0, sizeof(*id_ctlr));
  3716. rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
  3717. if (!rc)
  3718. if (id_ctlr->configured_logical_drive_count < 256)
  3719. *nlocals = id_ctlr->configured_logical_drive_count;
  3720. else
  3721. *nlocals = le16_to_cpu(
  3722. id_ctlr->extended_logical_unit_count);
  3723. else
  3724. *nlocals = -1;
  3725. return rc;
  3726. }
  3727. static bool hpsa_is_disk_spare(struct ctlr_info *h, u8 *lunaddrbytes)
  3728. {
  3729. struct bmic_identify_physical_device *id_phys;
  3730. bool is_spare = false;
  3731. int rc;
  3732. id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
  3733. if (!id_phys)
  3734. return false;
  3735. rc = hpsa_bmic_id_physical_device(h,
  3736. lunaddrbytes,
  3737. GET_BMIC_DRIVE_NUMBER(lunaddrbytes),
  3738. id_phys, sizeof(*id_phys));
  3739. if (rc == 0)
  3740. is_spare = (id_phys->more_flags >> 6) & 0x01;
  3741. kfree(id_phys);
  3742. return is_spare;
  3743. }
  3744. #define RPL_DEV_FLAG_NON_DISK 0x1
  3745. #define RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED 0x2
  3746. #define RPL_DEV_FLAG_UNCONFIG_DISK 0x4
  3747. #define BMIC_DEVICE_TYPE_ENCLOSURE 6
  3748. static bool hpsa_skip_device(struct ctlr_info *h, u8 *lunaddrbytes,
  3749. struct ext_report_lun_entry *rle)
  3750. {
  3751. u8 device_flags;
  3752. u8 device_type;
  3753. if (!MASKED_DEVICE(lunaddrbytes))
  3754. return false;
  3755. device_flags = rle->device_flags;
  3756. device_type = rle->device_type;
  3757. if (device_flags & RPL_DEV_FLAG_NON_DISK) {
  3758. if (device_type == BMIC_DEVICE_TYPE_ENCLOSURE)
  3759. return false;
  3760. return true;
  3761. }
  3762. if (!(device_flags & RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED))
  3763. return false;
  3764. if (device_flags & RPL_DEV_FLAG_UNCONFIG_DISK)
  3765. return false;
  3766. /*
  3767. * Spares may be spun down, we do not want to
  3768. * do an Inquiry to a RAID set spare drive as
  3769. * that would have them spun up, that is a
  3770. * performance hit because I/O to the RAID device
  3771. * stops while the spin up occurs which can take
  3772. * over 50 seconds.
  3773. */
  3774. if (hpsa_is_disk_spare(h, lunaddrbytes))
  3775. return true;
  3776. return false;
  3777. }
  3778. static void hpsa_update_scsi_devices(struct ctlr_info *h)
  3779. {
  3780. /* the idea here is we could get notified
  3781. * that some devices have changed, so we do a report
  3782. * physical luns and report logical luns cmd, and adjust
  3783. * our list of devices accordingly.
  3784. *
  3785. * The scsi3addr's of devices won't change so long as the
  3786. * adapter is not reset. That means we can rescan and
  3787. * tell which devices we already know about, vs. new
  3788. * devices, vs. disappearing devices.
  3789. */
  3790. struct ReportExtendedLUNdata *physdev_list = NULL;
  3791. struct ReportLUNdata *logdev_list = NULL;
  3792. struct bmic_identify_physical_device *id_phys = NULL;
  3793. struct bmic_identify_controller *id_ctlr = NULL;
  3794. u32 nphysicals = 0;
  3795. u32 nlogicals = 0;
  3796. u32 nlocal_logicals = 0;
  3797. u32 ndev_allocated = 0;
  3798. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  3799. int ncurrent = 0;
  3800. int i, n_ext_target_devs, ndevs_to_allocate;
  3801. int raid_ctlr_position;
  3802. bool physical_device;
  3803. DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
  3804. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
  3805. physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
  3806. logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
  3807. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  3808. id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
  3809. id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
  3810. if (!currentsd || !physdev_list || !logdev_list ||
  3811. !tmpdevice || !id_phys || !id_ctlr) {
  3812. dev_err(&h->pdev->dev, "out of memory\n");
  3813. goto out;
  3814. }
  3815. memset(lunzerobits, 0, sizeof(lunzerobits));
  3816. h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
  3817. if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
  3818. logdev_list, &nlogicals)) {
  3819. h->drv_req_rescan = 1;
  3820. goto out;
  3821. }
  3822. /* Set number of local logicals (non PTRAID) */
  3823. if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
  3824. dev_warn(&h->pdev->dev,
  3825. "%s: Can't determine number of local logical devices.\n",
  3826. __func__);
  3827. }
  3828. /* We might see up to the maximum number of logical and physical disks
  3829. * plus external target devices, and a device for the local RAID
  3830. * controller.
  3831. */
  3832. ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
  3833. /* Allocate the per device structures */
  3834. for (i = 0; i < ndevs_to_allocate; i++) {
  3835. if (i >= HPSA_MAX_DEVICES) {
  3836. dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
  3837. " %d devices ignored.\n", HPSA_MAX_DEVICES,
  3838. ndevs_to_allocate - HPSA_MAX_DEVICES);
  3839. break;
  3840. }
  3841. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  3842. if (!currentsd[i]) {
  3843. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  3844. __FILE__, __LINE__);
  3845. h->drv_req_rescan = 1;
  3846. goto out;
  3847. }
  3848. ndev_allocated++;
  3849. }
  3850. if (is_scsi_rev_5(h))
  3851. raid_ctlr_position = 0;
  3852. else
  3853. raid_ctlr_position = nphysicals + nlogicals;
  3854. /* adjust our table of devices */
  3855. n_ext_target_devs = 0;
  3856. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  3857. u8 *lunaddrbytes, is_OBDR = 0;
  3858. int rc = 0;
  3859. int phys_dev_index = i - (raid_ctlr_position == 0);
  3860. bool skip_device = false;
  3861. physical_device = i < nphysicals + (raid_ctlr_position == 0);
  3862. /* Figure out where the LUN ID info is coming from */
  3863. lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
  3864. i, nphysicals, nlogicals, physdev_list, logdev_list);
  3865. /* Determine if this is a lun from an external target array */
  3866. tmpdevice->external =
  3867. figure_external_status(h, raid_ctlr_position, i,
  3868. nphysicals, nlocal_logicals);
  3869. /*
  3870. * Skip over some devices such as a spare.
  3871. */
  3872. if (!tmpdevice->external && physical_device) {
  3873. skip_device = hpsa_skip_device(h, lunaddrbytes,
  3874. &physdev_list->LUN[phys_dev_index]);
  3875. if (skip_device)
  3876. continue;
  3877. }
  3878. /* Get device type, vendor, model, device id */
  3879. rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
  3880. &is_OBDR);
  3881. if (rc == -ENOMEM) {
  3882. dev_warn(&h->pdev->dev,
  3883. "Out of memory, rescan deferred.\n");
  3884. h->drv_req_rescan = 1;
  3885. goto out;
  3886. }
  3887. if (rc) {
  3888. h->drv_req_rescan = 1;
  3889. continue;
  3890. }
  3891. figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
  3892. hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
  3893. this_device = currentsd[ncurrent];
  3894. /* Turn on discovery_polling if there are ext target devices.
  3895. * Event-based change notification is unreliable for those.
  3896. */
  3897. if (!h->discovery_polling) {
  3898. if (tmpdevice->external) {
  3899. h->discovery_polling = 1;
  3900. dev_info(&h->pdev->dev,
  3901. "External target, activate discovery polling.\n");
  3902. }
  3903. }
  3904. *this_device = *tmpdevice;
  3905. this_device->physical_device = physical_device;
  3906. /*
  3907. * Expose all devices except for physical devices that
  3908. * are masked.
  3909. */
  3910. if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
  3911. this_device->expose_device = 0;
  3912. else
  3913. this_device->expose_device = 1;
  3914. /*
  3915. * Get the SAS address for physical devices that are exposed.
  3916. */
  3917. if (this_device->physical_device && this_device->expose_device)
  3918. hpsa_get_sas_address(h, lunaddrbytes, this_device);
  3919. switch (this_device->devtype) {
  3920. case TYPE_ROM:
  3921. /* We don't *really* support actual CD-ROM devices,
  3922. * just "One Button Disaster Recovery" tape drive
  3923. * which temporarily pretends to be a CD-ROM drive.
  3924. * So we check that the device is really an OBDR tape
  3925. * device by checking for "$DR-10" in bytes 43-48 of
  3926. * the inquiry data.
  3927. */
  3928. if (is_OBDR)
  3929. ncurrent++;
  3930. break;
  3931. case TYPE_DISK:
  3932. case TYPE_ZBC:
  3933. if (this_device->physical_device) {
  3934. /* The disk is in HBA mode. */
  3935. /* Never use RAID mapper in HBA mode. */
  3936. this_device->offload_enabled = 0;
  3937. hpsa_get_ioaccel_drive_info(h, this_device,
  3938. physdev_list, phys_dev_index, id_phys);
  3939. hpsa_get_path_info(this_device,
  3940. physdev_list, phys_dev_index, id_phys);
  3941. }
  3942. ncurrent++;
  3943. break;
  3944. case TYPE_TAPE:
  3945. case TYPE_MEDIUM_CHANGER:
  3946. ncurrent++;
  3947. break;
  3948. case TYPE_ENCLOSURE:
  3949. if (!this_device->external)
  3950. hpsa_get_enclosure_info(h, lunaddrbytes,
  3951. physdev_list, phys_dev_index,
  3952. this_device);
  3953. ncurrent++;
  3954. break;
  3955. case TYPE_RAID:
  3956. /* Only present the Smartarray HBA as a RAID controller.
  3957. * If it's a RAID controller other than the HBA itself
  3958. * (an external RAID controller, MSA500 or similar)
  3959. * don't present it.
  3960. */
  3961. if (!is_hba_lunid(lunaddrbytes))
  3962. break;
  3963. ncurrent++;
  3964. break;
  3965. default:
  3966. break;
  3967. }
  3968. if (ncurrent >= HPSA_MAX_DEVICES)
  3969. break;
  3970. }
  3971. if (h->sas_host == NULL) {
  3972. int rc = 0;
  3973. rc = hpsa_add_sas_host(h);
  3974. if (rc) {
  3975. dev_warn(&h->pdev->dev,
  3976. "Could not add sas host %d\n", rc);
  3977. goto out;
  3978. }
  3979. }
  3980. adjust_hpsa_scsi_table(h, currentsd, ncurrent);
  3981. out:
  3982. kfree(tmpdevice);
  3983. for (i = 0; i < ndev_allocated; i++)
  3984. kfree(currentsd[i]);
  3985. kfree(currentsd);
  3986. kfree(physdev_list);
  3987. kfree(logdev_list);
  3988. kfree(id_ctlr);
  3989. kfree(id_phys);
  3990. }
  3991. static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
  3992. struct scatterlist *sg)
  3993. {
  3994. u64 addr64 = (u64) sg_dma_address(sg);
  3995. unsigned int len = sg_dma_len(sg);
  3996. desc->Addr = cpu_to_le64(addr64);
  3997. desc->Len = cpu_to_le32(len);
  3998. desc->Ext = 0;
  3999. }
  4000. /*
  4001. * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  4002. * dma mapping and fills in the scatter gather entries of the
  4003. * hpsa command, cp.
  4004. */
  4005. static int hpsa_scatter_gather(struct ctlr_info *h,
  4006. struct CommandList *cp,
  4007. struct scsi_cmnd *cmd)
  4008. {
  4009. struct scatterlist *sg;
  4010. int use_sg, i, sg_limit, chained, last_sg;
  4011. struct SGDescriptor *curr_sg;
  4012. BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
  4013. use_sg = scsi_dma_map(cmd);
  4014. if (use_sg < 0)
  4015. return use_sg;
  4016. if (!use_sg)
  4017. goto sglist_finished;
  4018. /*
  4019. * If the number of entries is greater than the max for a single list,
  4020. * then we have a chained list; we will set up all but one entry in the
  4021. * first list (the last entry is saved for link information);
  4022. * otherwise, we don't have a chained list and we'll set up at each of
  4023. * the entries in the one list.
  4024. */
  4025. curr_sg = cp->SG;
  4026. chained = use_sg > h->max_cmd_sg_entries;
  4027. sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
  4028. last_sg = scsi_sg_count(cmd) - 1;
  4029. scsi_for_each_sg(cmd, sg, sg_limit, i) {
  4030. hpsa_set_sg_descriptor(curr_sg, sg);
  4031. curr_sg++;
  4032. }
  4033. if (chained) {
  4034. /*
  4035. * Continue with the chained list. Set curr_sg to the chained
  4036. * list. Modify the limit to the total count less the entries
  4037. * we've already set up. Resume the scan at the list entry
  4038. * where the previous loop left off.
  4039. */
  4040. curr_sg = h->cmd_sg_list[cp->cmdindex];
  4041. sg_limit = use_sg - sg_limit;
  4042. for_each_sg(sg, sg, sg_limit, i) {
  4043. hpsa_set_sg_descriptor(curr_sg, sg);
  4044. curr_sg++;
  4045. }
  4046. }
  4047. /* Back the pointer up to the last entry and mark it as "last". */
  4048. (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
  4049. if (use_sg + chained > h->maxSG)
  4050. h->maxSG = use_sg + chained;
  4051. if (chained) {
  4052. cp->Header.SGList = h->max_cmd_sg_entries;
  4053. cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
  4054. if (hpsa_map_sg_chain_block(h, cp)) {
  4055. scsi_dma_unmap(cmd);
  4056. return -1;
  4057. }
  4058. return 0;
  4059. }
  4060. sglist_finished:
  4061. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  4062. cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
  4063. return 0;
  4064. }
  4065. #define IO_ACCEL_INELIGIBLE (1)
  4066. static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
  4067. {
  4068. int is_write = 0;
  4069. u32 block;
  4070. u32 block_cnt;
  4071. /* Perform some CDB fixups if needed using 10 byte reads/writes only */
  4072. switch (cdb[0]) {
  4073. case WRITE_6:
  4074. case WRITE_12:
  4075. is_write = 1;
  4076. case READ_6:
  4077. case READ_12:
  4078. if (*cdb_len == 6) {
  4079. block = (((cdb[1] & 0x1F) << 16) |
  4080. (cdb[2] << 8) |
  4081. cdb[3]);
  4082. block_cnt = cdb[4];
  4083. if (block_cnt == 0)
  4084. block_cnt = 256;
  4085. } else {
  4086. BUG_ON(*cdb_len != 12);
  4087. block = get_unaligned_be32(&cdb[2]);
  4088. block_cnt = get_unaligned_be32(&cdb[6]);
  4089. }
  4090. if (block_cnt > 0xffff)
  4091. return IO_ACCEL_INELIGIBLE;
  4092. cdb[0] = is_write ? WRITE_10 : READ_10;
  4093. cdb[1] = 0;
  4094. cdb[2] = (u8) (block >> 24);
  4095. cdb[3] = (u8) (block >> 16);
  4096. cdb[4] = (u8) (block >> 8);
  4097. cdb[5] = (u8) (block);
  4098. cdb[6] = 0;
  4099. cdb[7] = (u8) (block_cnt >> 8);
  4100. cdb[8] = (u8) (block_cnt);
  4101. cdb[9] = 0;
  4102. *cdb_len = 10;
  4103. break;
  4104. }
  4105. return 0;
  4106. }
  4107. static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
  4108. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  4109. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
  4110. {
  4111. struct scsi_cmnd *cmd = c->scsi_cmd;
  4112. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
  4113. unsigned int len;
  4114. unsigned int total_len = 0;
  4115. struct scatterlist *sg;
  4116. u64 addr64;
  4117. int use_sg, i;
  4118. struct SGDescriptor *curr_sg;
  4119. u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
  4120. /* TODO: implement chaining support */
  4121. if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
  4122. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4123. return IO_ACCEL_INELIGIBLE;
  4124. }
  4125. BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
  4126. if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
  4127. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4128. return IO_ACCEL_INELIGIBLE;
  4129. }
  4130. c->cmd_type = CMD_IOACCEL1;
  4131. /* Adjust the DMA address to point to the accelerated command buffer */
  4132. c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
  4133. (c->cmdindex * sizeof(*cp));
  4134. BUG_ON(c->busaddr & 0x0000007F);
  4135. use_sg = scsi_dma_map(cmd);
  4136. if (use_sg < 0) {
  4137. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4138. return use_sg;
  4139. }
  4140. if (use_sg) {
  4141. curr_sg = cp->SG;
  4142. scsi_for_each_sg(cmd, sg, use_sg, i) {
  4143. addr64 = (u64) sg_dma_address(sg);
  4144. len = sg_dma_len(sg);
  4145. total_len += len;
  4146. curr_sg->Addr = cpu_to_le64(addr64);
  4147. curr_sg->Len = cpu_to_le32(len);
  4148. curr_sg->Ext = cpu_to_le32(0);
  4149. curr_sg++;
  4150. }
  4151. (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
  4152. switch (cmd->sc_data_direction) {
  4153. case DMA_TO_DEVICE:
  4154. control |= IOACCEL1_CONTROL_DATA_OUT;
  4155. break;
  4156. case DMA_FROM_DEVICE:
  4157. control |= IOACCEL1_CONTROL_DATA_IN;
  4158. break;
  4159. case DMA_NONE:
  4160. control |= IOACCEL1_CONTROL_NODATAXFER;
  4161. break;
  4162. default:
  4163. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  4164. cmd->sc_data_direction);
  4165. BUG();
  4166. break;
  4167. }
  4168. } else {
  4169. control |= IOACCEL1_CONTROL_NODATAXFER;
  4170. }
  4171. c->Header.SGList = use_sg;
  4172. /* Fill out the command structure to submit */
  4173. cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
  4174. cp->transfer_len = cpu_to_le32(total_len);
  4175. cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
  4176. (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
  4177. cp->control = cpu_to_le32(control);
  4178. memcpy(cp->CDB, cdb, cdb_len);
  4179. memcpy(cp->CISS_LUN, scsi3addr, 8);
  4180. /* Tag was already set at init time. */
  4181. enqueue_cmd_and_start_io(h, c);
  4182. return 0;
  4183. }
  4184. /*
  4185. * Queue a command directly to a device behind the controller using the
  4186. * I/O accelerator path.
  4187. */
  4188. static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
  4189. struct CommandList *c)
  4190. {
  4191. struct scsi_cmnd *cmd = c->scsi_cmd;
  4192. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4193. if (!dev)
  4194. return -1;
  4195. c->phys_disk = dev;
  4196. return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
  4197. cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
  4198. }
  4199. /*
  4200. * Set encryption parameters for the ioaccel2 request
  4201. */
  4202. static void set_encrypt_ioaccel2(struct ctlr_info *h,
  4203. struct CommandList *c, struct io_accel2_cmd *cp)
  4204. {
  4205. struct scsi_cmnd *cmd = c->scsi_cmd;
  4206. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4207. struct raid_map_data *map = &dev->raid_map;
  4208. u64 first_block;
  4209. /* Are we doing encryption on this device */
  4210. if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
  4211. return;
  4212. /* Set the data encryption key index. */
  4213. cp->dekindex = map->dekindex;
  4214. /* Set the encryption enable flag, encoded into direction field. */
  4215. cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
  4216. /* Set encryption tweak values based on logical block address
  4217. * If block size is 512, tweak value is LBA.
  4218. * For other block sizes, tweak is (LBA * block size)/ 512)
  4219. */
  4220. switch (cmd->cmnd[0]) {
  4221. /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
  4222. case READ_6:
  4223. case WRITE_6:
  4224. first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
  4225. (cmd->cmnd[2] << 8) |
  4226. cmd->cmnd[3]);
  4227. break;
  4228. case WRITE_10:
  4229. case READ_10:
  4230. /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
  4231. case WRITE_12:
  4232. case READ_12:
  4233. first_block = get_unaligned_be32(&cmd->cmnd[2]);
  4234. break;
  4235. case WRITE_16:
  4236. case READ_16:
  4237. first_block = get_unaligned_be64(&cmd->cmnd[2]);
  4238. break;
  4239. default:
  4240. dev_err(&h->pdev->dev,
  4241. "ERROR: %s: size (0x%x) not supported for encryption\n",
  4242. __func__, cmd->cmnd[0]);
  4243. BUG();
  4244. break;
  4245. }
  4246. if (le32_to_cpu(map->volume_blk_size) != 512)
  4247. first_block = first_block *
  4248. le32_to_cpu(map->volume_blk_size)/512;
  4249. cp->tweak_lower = cpu_to_le32(first_block);
  4250. cp->tweak_upper = cpu_to_le32(first_block >> 32);
  4251. }
  4252. static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
  4253. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  4254. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
  4255. {
  4256. struct scsi_cmnd *cmd = c->scsi_cmd;
  4257. struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
  4258. struct ioaccel2_sg_element *curr_sg;
  4259. int use_sg, i;
  4260. struct scatterlist *sg;
  4261. u64 addr64;
  4262. u32 len;
  4263. u32 total_len = 0;
  4264. if (!cmd->device)
  4265. return -1;
  4266. if (!cmd->device->hostdata)
  4267. return -1;
  4268. BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
  4269. if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
  4270. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4271. return IO_ACCEL_INELIGIBLE;
  4272. }
  4273. c->cmd_type = CMD_IOACCEL2;
  4274. /* Adjust the DMA address to point to the accelerated command buffer */
  4275. c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
  4276. (c->cmdindex * sizeof(*cp));
  4277. BUG_ON(c->busaddr & 0x0000007F);
  4278. memset(cp, 0, sizeof(*cp));
  4279. cp->IU_type = IOACCEL2_IU_TYPE;
  4280. use_sg = scsi_dma_map(cmd);
  4281. if (use_sg < 0) {
  4282. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4283. return use_sg;
  4284. }
  4285. if (use_sg) {
  4286. curr_sg = cp->sg;
  4287. if (use_sg > h->ioaccel_maxsg) {
  4288. addr64 = le64_to_cpu(
  4289. h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
  4290. curr_sg->address = cpu_to_le64(addr64);
  4291. curr_sg->length = 0;
  4292. curr_sg->reserved[0] = 0;
  4293. curr_sg->reserved[1] = 0;
  4294. curr_sg->reserved[2] = 0;
  4295. curr_sg->chain_indicator = 0x80;
  4296. curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
  4297. }
  4298. scsi_for_each_sg(cmd, sg, use_sg, i) {
  4299. addr64 = (u64) sg_dma_address(sg);
  4300. len = sg_dma_len(sg);
  4301. total_len += len;
  4302. curr_sg->address = cpu_to_le64(addr64);
  4303. curr_sg->length = cpu_to_le32(len);
  4304. curr_sg->reserved[0] = 0;
  4305. curr_sg->reserved[1] = 0;
  4306. curr_sg->reserved[2] = 0;
  4307. curr_sg->chain_indicator = 0;
  4308. curr_sg++;
  4309. }
  4310. switch (cmd->sc_data_direction) {
  4311. case DMA_TO_DEVICE:
  4312. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4313. cp->direction |= IOACCEL2_DIR_DATA_OUT;
  4314. break;
  4315. case DMA_FROM_DEVICE:
  4316. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4317. cp->direction |= IOACCEL2_DIR_DATA_IN;
  4318. break;
  4319. case DMA_NONE:
  4320. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4321. cp->direction |= IOACCEL2_DIR_NO_DATA;
  4322. break;
  4323. default:
  4324. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  4325. cmd->sc_data_direction);
  4326. BUG();
  4327. break;
  4328. }
  4329. } else {
  4330. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4331. cp->direction |= IOACCEL2_DIR_NO_DATA;
  4332. }
  4333. /* Set encryption parameters, if necessary */
  4334. set_encrypt_ioaccel2(h, c, cp);
  4335. cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
  4336. cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
  4337. memcpy(cp->cdb, cdb, sizeof(cp->cdb));
  4338. cp->data_len = cpu_to_le32(total_len);
  4339. cp->err_ptr = cpu_to_le64(c->busaddr +
  4340. offsetof(struct io_accel2_cmd, error_data));
  4341. cp->err_len = cpu_to_le32(sizeof(cp->error_data));
  4342. /* fill in sg elements */
  4343. if (use_sg > h->ioaccel_maxsg) {
  4344. cp->sg_count = 1;
  4345. cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
  4346. if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
  4347. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4348. scsi_dma_unmap(cmd);
  4349. return -1;
  4350. }
  4351. } else
  4352. cp->sg_count = (u8) use_sg;
  4353. enqueue_cmd_and_start_io(h, c);
  4354. return 0;
  4355. }
  4356. /*
  4357. * Queue a command to the correct I/O accelerator path.
  4358. */
  4359. static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
  4360. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  4361. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
  4362. {
  4363. if (!c->scsi_cmd->device)
  4364. return -1;
  4365. if (!c->scsi_cmd->device->hostdata)
  4366. return -1;
  4367. /* Try to honor the device's queue depth */
  4368. if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
  4369. phys_disk->queue_depth) {
  4370. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4371. return IO_ACCEL_INELIGIBLE;
  4372. }
  4373. if (h->transMethod & CFGTBL_Trans_io_accel1)
  4374. return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
  4375. cdb, cdb_len, scsi3addr,
  4376. phys_disk);
  4377. else
  4378. return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
  4379. cdb, cdb_len, scsi3addr,
  4380. phys_disk);
  4381. }
  4382. static void raid_map_helper(struct raid_map_data *map,
  4383. int offload_to_mirror, u32 *map_index, u32 *current_group)
  4384. {
  4385. if (offload_to_mirror == 0) {
  4386. /* use physical disk in the first mirrored group. */
  4387. *map_index %= le16_to_cpu(map->data_disks_per_row);
  4388. return;
  4389. }
  4390. do {
  4391. /* determine mirror group that *map_index indicates */
  4392. *current_group = *map_index /
  4393. le16_to_cpu(map->data_disks_per_row);
  4394. if (offload_to_mirror == *current_group)
  4395. continue;
  4396. if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
  4397. /* select map index from next group */
  4398. *map_index += le16_to_cpu(map->data_disks_per_row);
  4399. (*current_group)++;
  4400. } else {
  4401. /* select map index from first group */
  4402. *map_index %= le16_to_cpu(map->data_disks_per_row);
  4403. *current_group = 0;
  4404. }
  4405. } while (offload_to_mirror != *current_group);
  4406. }
  4407. /*
  4408. * Attempt to perform offload RAID mapping for a logical volume I/O.
  4409. */
  4410. static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
  4411. struct CommandList *c)
  4412. {
  4413. struct scsi_cmnd *cmd = c->scsi_cmd;
  4414. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4415. struct raid_map_data *map = &dev->raid_map;
  4416. struct raid_map_disk_data *dd = &map->data[0];
  4417. int is_write = 0;
  4418. u32 map_index;
  4419. u64 first_block, last_block;
  4420. u32 block_cnt;
  4421. u32 blocks_per_row;
  4422. u64 first_row, last_row;
  4423. u32 first_row_offset, last_row_offset;
  4424. u32 first_column, last_column;
  4425. u64 r0_first_row, r0_last_row;
  4426. u32 r5or6_blocks_per_row;
  4427. u64 r5or6_first_row, r5or6_last_row;
  4428. u32 r5or6_first_row_offset, r5or6_last_row_offset;
  4429. u32 r5or6_first_column, r5or6_last_column;
  4430. u32 total_disks_per_row;
  4431. u32 stripesize;
  4432. u32 first_group, last_group, current_group;
  4433. u32 map_row;
  4434. u32 disk_handle;
  4435. u64 disk_block;
  4436. u32 disk_block_cnt;
  4437. u8 cdb[16];
  4438. u8 cdb_len;
  4439. u16 strip_size;
  4440. #if BITS_PER_LONG == 32
  4441. u64 tmpdiv;
  4442. #endif
  4443. int offload_to_mirror;
  4444. if (!dev)
  4445. return -1;
  4446. /* check for valid opcode, get LBA and block count */
  4447. switch (cmd->cmnd[0]) {
  4448. case WRITE_6:
  4449. is_write = 1;
  4450. case READ_6:
  4451. first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
  4452. (cmd->cmnd[2] << 8) |
  4453. cmd->cmnd[3]);
  4454. block_cnt = cmd->cmnd[4];
  4455. if (block_cnt == 0)
  4456. block_cnt = 256;
  4457. break;
  4458. case WRITE_10:
  4459. is_write = 1;
  4460. case READ_10:
  4461. first_block =
  4462. (((u64) cmd->cmnd[2]) << 24) |
  4463. (((u64) cmd->cmnd[3]) << 16) |
  4464. (((u64) cmd->cmnd[4]) << 8) |
  4465. cmd->cmnd[5];
  4466. block_cnt =
  4467. (((u32) cmd->cmnd[7]) << 8) |
  4468. cmd->cmnd[8];
  4469. break;
  4470. case WRITE_12:
  4471. is_write = 1;
  4472. case READ_12:
  4473. first_block =
  4474. (((u64) cmd->cmnd[2]) << 24) |
  4475. (((u64) cmd->cmnd[3]) << 16) |
  4476. (((u64) cmd->cmnd[4]) << 8) |
  4477. cmd->cmnd[5];
  4478. block_cnt =
  4479. (((u32) cmd->cmnd[6]) << 24) |
  4480. (((u32) cmd->cmnd[7]) << 16) |
  4481. (((u32) cmd->cmnd[8]) << 8) |
  4482. cmd->cmnd[9];
  4483. break;
  4484. case WRITE_16:
  4485. is_write = 1;
  4486. case READ_16:
  4487. first_block =
  4488. (((u64) cmd->cmnd[2]) << 56) |
  4489. (((u64) cmd->cmnd[3]) << 48) |
  4490. (((u64) cmd->cmnd[4]) << 40) |
  4491. (((u64) cmd->cmnd[5]) << 32) |
  4492. (((u64) cmd->cmnd[6]) << 24) |
  4493. (((u64) cmd->cmnd[7]) << 16) |
  4494. (((u64) cmd->cmnd[8]) << 8) |
  4495. cmd->cmnd[9];
  4496. block_cnt =
  4497. (((u32) cmd->cmnd[10]) << 24) |
  4498. (((u32) cmd->cmnd[11]) << 16) |
  4499. (((u32) cmd->cmnd[12]) << 8) |
  4500. cmd->cmnd[13];
  4501. break;
  4502. default:
  4503. return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
  4504. }
  4505. last_block = first_block + block_cnt - 1;
  4506. /* check for write to non-RAID-0 */
  4507. if (is_write && dev->raid_level != 0)
  4508. return IO_ACCEL_INELIGIBLE;
  4509. /* check for invalid block or wraparound */
  4510. if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
  4511. last_block < first_block)
  4512. return IO_ACCEL_INELIGIBLE;
  4513. /* calculate stripe information for the request */
  4514. blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
  4515. le16_to_cpu(map->strip_size);
  4516. strip_size = le16_to_cpu(map->strip_size);
  4517. #if BITS_PER_LONG == 32
  4518. tmpdiv = first_block;
  4519. (void) do_div(tmpdiv, blocks_per_row);
  4520. first_row = tmpdiv;
  4521. tmpdiv = last_block;
  4522. (void) do_div(tmpdiv, blocks_per_row);
  4523. last_row = tmpdiv;
  4524. first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
  4525. last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
  4526. tmpdiv = first_row_offset;
  4527. (void) do_div(tmpdiv, strip_size);
  4528. first_column = tmpdiv;
  4529. tmpdiv = last_row_offset;
  4530. (void) do_div(tmpdiv, strip_size);
  4531. last_column = tmpdiv;
  4532. #else
  4533. first_row = first_block / blocks_per_row;
  4534. last_row = last_block / blocks_per_row;
  4535. first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
  4536. last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
  4537. first_column = first_row_offset / strip_size;
  4538. last_column = last_row_offset / strip_size;
  4539. #endif
  4540. /* if this isn't a single row/column then give to the controller */
  4541. if ((first_row != last_row) || (first_column != last_column))
  4542. return IO_ACCEL_INELIGIBLE;
  4543. /* proceeding with driver mapping */
  4544. total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
  4545. le16_to_cpu(map->metadata_disks_per_row);
  4546. map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
  4547. le16_to_cpu(map->row_cnt);
  4548. map_index = (map_row * total_disks_per_row) + first_column;
  4549. switch (dev->raid_level) {
  4550. case HPSA_RAID_0:
  4551. break; /* nothing special to do */
  4552. case HPSA_RAID_1:
  4553. /* Handles load balance across RAID 1 members.
  4554. * (2-drive R1 and R10 with even # of drives.)
  4555. * Appropriate for SSDs, not optimal for HDDs
  4556. */
  4557. BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
  4558. if (dev->offload_to_mirror)
  4559. map_index += le16_to_cpu(map->data_disks_per_row);
  4560. dev->offload_to_mirror = !dev->offload_to_mirror;
  4561. break;
  4562. case HPSA_RAID_ADM:
  4563. /* Handles N-way mirrors (R1-ADM)
  4564. * and R10 with # of drives divisible by 3.)
  4565. */
  4566. BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
  4567. offload_to_mirror = dev->offload_to_mirror;
  4568. raid_map_helper(map, offload_to_mirror,
  4569. &map_index, &current_group);
  4570. /* set mirror group to use next time */
  4571. offload_to_mirror =
  4572. (offload_to_mirror >=
  4573. le16_to_cpu(map->layout_map_count) - 1)
  4574. ? 0 : offload_to_mirror + 1;
  4575. dev->offload_to_mirror = offload_to_mirror;
  4576. /* Avoid direct use of dev->offload_to_mirror within this
  4577. * function since multiple threads might simultaneously
  4578. * increment it beyond the range of dev->layout_map_count -1.
  4579. */
  4580. break;
  4581. case HPSA_RAID_5:
  4582. case HPSA_RAID_6:
  4583. if (le16_to_cpu(map->layout_map_count) <= 1)
  4584. break;
  4585. /* Verify first and last block are in same RAID group */
  4586. r5or6_blocks_per_row =
  4587. le16_to_cpu(map->strip_size) *
  4588. le16_to_cpu(map->data_disks_per_row);
  4589. BUG_ON(r5or6_blocks_per_row == 0);
  4590. stripesize = r5or6_blocks_per_row *
  4591. le16_to_cpu(map->layout_map_count);
  4592. #if BITS_PER_LONG == 32
  4593. tmpdiv = first_block;
  4594. first_group = do_div(tmpdiv, stripesize);
  4595. tmpdiv = first_group;
  4596. (void) do_div(tmpdiv, r5or6_blocks_per_row);
  4597. first_group = tmpdiv;
  4598. tmpdiv = last_block;
  4599. last_group = do_div(tmpdiv, stripesize);
  4600. tmpdiv = last_group;
  4601. (void) do_div(tmpdiv, r5or6_blocks_per_row);
  4602. last_group = tmpdiv;
  4603. #else
  4604. first_group = (first_block % stripesize) / r5or6_blocks_per_row;
  4605. last_group = (last_block % stripesize) / r5or6_blocks_per_row;
  4606. #endif
  4607. if (first_group != last_group)
  4608. return IO_ACCEL_INELIGIBLE;
  4609. /* Verify request is in a single row of RAID 5/6 */
  4610. #if BITS_PER_LONG == 32
  4611. tmpdiv = first_block;
  4612. (void) do_div(tmpdiv, stripesize);
  4613. first_row = r5or6_first_row = r0_first_row = tmpdiv;
  4614. tmpdiv = last_block;
  4615. (void) do_div(tmpdiv, stripesize);
  4616. r5or6_last_row = r0_last_row = tmpdiv;
  4617. #else
  4618. first_row = r5or6_first_row = r0_first_row =
  4619. first_block / stripesize;
  4620. r5or6_last_row = r0_last_row = last_block / stripesize;
  4621. #endif
  4622. if (r5or6_first_row != r5or6_last_row)
  4623. return IO_ACCEL_INELIGIBLE;
  4624. /* Verify request is in a single column */
  4625. #if BITS_PER_LONG == 32
  4626. tmpdiv = first_block;
  4627. first_row_offset = do_div(tmpdiv, stripesize);
  4628. tmpdiv = first_row_offset;
  4629. first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
  4630. r5or6_first_row_offset = first_row_offset;
  4631. tmpdiv = last_block;
  4632. r5or6_last_row_offset = do_div(tmpdiv, stripesize);
  4633. tmpdiv = r5or6_last_row_offset;
  4634. r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
  4635. tmpdiv = r5or6_first_row_offset;
  4636. (void) do_div(tmpdiv, map->strip_size);
  4637. first_column = r5or6_first_column = tmpdiv;
  4638. tmpdiv = r5or6_last_row_offset;
  4639. (void) do_div(tmpdiv, map->strip_size);
  4640. r5or6_last_column = tmpdiv;
  4641. #else
  4642. first_row_offset = r5or6_first_row_offset =
  4643. (u32)((first_block % stripesize) %
  4644. r5or6_blocks_per_row);
  4645. r5or6_last_row_offset =
  4646. (u32)((last_block % stripesize) %
  4647. r5or6_blocks_per_row);
  4648. first_column = r5or6_first_column =
  4649. r5or6_first_row_offset / le16_to_cpu(map->strip_size);
  4650. r5or6_last_column =
  4651. r5or6_last_row_offset / le16_to_cpu(map->strip_size);
  4652. #endif
  4653. if (r5or6_first_column != r5or6_last_column)
  4654. return IO_ACCEL_INELIGIBLE;
  4655. /* Request is eligible */
  4656. map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
  4657. le16_to_cpu(map->row_cnt);
  4658. map_index = (first_group *
  4659. (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
  4660. (map_row * total_disks_per_row) + first_column;
  4661. break;
  4662. default:
  4663. return IO_ACCEL_INELIGIBLE;
  4664. }
  4665. if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
  4666. return IO_ACCEL_INELIGIBLE;
  4667. c->phys_disk = dev->phys_disk[map_index];
  4668. if (!c->phys_disk)
  4669. return IO_ACCEL_INELIGIBLE;
  4670. disk_handle = dd[map_index].ioaccel_handle;
  4671. disk_block = le64_to_cpu(map->disk_starting_blk) +
  4672. first_row * le16_to_cpu(map->strip_size) +
  4673. (first_row_offset - first_column *
  4674. le16_to_cpu(map->strip_size));
  4675. disk_block_cnt = block_cnt;
  4676. /* handle differing logical/physical block sizes */
  4677. if (map->phys_blk_shift) {
  4678. disk_block <<= map->phys_blk_shift;
  4679. disk_block_cnt <<= map->phys_blk_shift;
  4680. }
  4681. BUG_ON(disk_block_cnt > 0xffff);
  4682. /* build the new CDB for the physical disk I/O */
  4683. if (disk_block > 0xffffffff) {
  4684. cdb[0] = is_write ? WRITE_16 : READ_16;
  4685. cdb[1] = 0;
  4686. cdb[2] = (u8) (disk_block >> 56);
  4687. cdb[3] = (u8) (disk_block >> 48);
  4688. cdb[4] = (u8) (disk_block >> 40);
  4689. cdb[5] = (u8) (disk_block >> 32);
  4690. cdb[6] = (u8) (disk_block >> 24);
  4691. cdb[7] = (u8) (disk_block >> 16);
  4692. cdb[8] = (u8) (disk_block >> 8);
  4693. cdb[9] = (u8) (disk_block);
  4694. cdb[10] = (u8) (disk_block_cnt >> 24);
  4695. cdb[11] = (u8) (disk_block_cnt >> 16);
  4696. cdb[12] = (u8) (disk_block_cnt >> 8);
  4697. cdb[13] = (u8) (disk_block_cnt);
  4698. cdb[14] = 0;
  4699. cdb[15] = 0;
  4700. cdb_len = 16;
  4701. } else {
  4702. cdb[0] = is_write ? WRITE_10 : READ_10;
  4703. cdb[1] = 0;
  4704. cdb[2] = (u8) (disk_block >> 24);
  4705. cdb[3] = (u8) (disk_block >> 16);
  4706. cdb[4] = (u8) (disk_block >> 8);
  4707. cdb[5] = (u8) (disk_block);
  4708. cdb[6] = 0;
  4709. cdb[7] = (u8) (disk_block_cnt >> 8);
  4710. cdb[8] = (u8) (disk_block_cnt);
  4711. cdb[9] = 0;
  4712. cdb_len = 10;
  4713. }
  4714. return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
  4715. dev->scsi3addr,
  4716. dev->phys_disk[map_index]);
  4717. }
  4718. /*
  4719. * Submit commands down the "normal" RAID stack path
  4720. * All callers to hpsa_ciss_submit must check lockup_detected
  4721. * beforehand, before (opt.) and after calling cmd_alloc
  4722. */
  4723. static int hpsa_ciss_submit(struct ctlr_info *h,
  4724. struct CommandList *c, struct scsi_cmnd *cmd,
  4725. unsigned char scsi3addr[])
  4726. {
  4727. cmd->host_scribble = (unsigned char *) c;
  4728. c->cmd_type = CMD_SCSI;
  4729. c->scsi_cmd = cmd;
  4730. c->Header.ReplyQueue = 0; /* unused in simple mode */
  4731. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  4732. c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
  4733. /* Fill in the request block... */
  4734. c->Request.Timeout = 0;
  4735. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  4736. c->Request.CDBLen = cmd->cmd_len;
  4737. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  4738. switch (cmd->sc_data_direction) {
  4739. case DMA_TO_DEVICE:
  4740. c->Request.type_attr_dir =
  4741. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
  4742. break;
  4743. case DMA_FROM_DEVICE:
  4744. c->Request.type_attr_dir =
  4745. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
  4746. break;
  4747. case DMA_NONE:
  4748. c->Request.type_attr_dir =
  4749. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
  4750. break;
  4751. case DMA_BIDIRECTIONAL:
  4752. /* This can happen if a buggy application does a scsi passthru
  4753. * and sets both inlen and outlen to non-zero. ( see
  4754. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  4755. */
  4756. c->Request.type_attr_dir =
  4757. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
  4758. /* This is technically wrong, and hpsa controllers should
  4759. * reject it with CMD_INVALID, which is the most correct
  4760. * response, but non-fibre backends appear to let it
  4761. * slide by, and give the same results as if this field
  4762. * were set correctly. Either way is acceptable for
  4763. * our purposes here.
  4764. */
  4765. break;
  4766. default:
  4767. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  4768. cmd->sc_data_direction);
  4769. BUG();
  4770. break;
  4771. }
  4772. if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
  4773. hpsa_cmd_resolve_and_free(h, c);
  4774. return SCSI_MLQUEUE_HOST_BUSY;
  4775. }
  4776. enqueue_cmd_and_start_io(h, c);
  4777. /* the cmd'll come back via intr handler in complete_scsi_command() */
  4778. return 0;
  4779. }
  4780. static void hpsa_cmd_init(struct ctlr_info *h, int index,
  4781. struct CommandList *c)
  4782. {
  4783. dma_addr_t cmd_dma_handle, err_dma_handle;
  4784. /* Zero out all of commandlist except the last field, refcount */
  4785. memset(c, 0, offsetof(struct CommandList, refcount));
  4786. c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
  4787. cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
  4788. c->err_info = h->errinfo_pool + index;
  4789. memset(c->err_info, 0, sizeof(*c->err_info));
  4790. err_dma_handle = h->errinfo_pool_dhandle
  4791. + index * sizeof(*c->err_info);
  4792. c->cmdindex = index;
  4793. c->busaddr = (u32) cmd_dma_handle;
  4794. c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
  4795. c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
  4796. c->h = h;
  4797. c->scsi_cmd = SCSI_CMD_IDLE;
  4798. }
  4799. static void hpsa_preinitialize_commands(struct ctlr_info *h)
  4800. {
  4801. int i;
  4802. for (i = 0; i < h->nr_cmds; i++) {
  4803. struct CommandList *c = h->cmd_pool + i;
  4804. hpsa_cmd_init(h, i, c);
  4805. atomic_set(&c->refcount, 0);
  4806. }
  4807. }
  4808. static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
  4809. struct CommandList *c)
  4810. {
  4811. dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
  4812. BUG_ON(c->cmdindex != index);
  4813. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  4814. memset(c->err_info, 0, sizeof(*c->err_info));
  4815. c->busaddr = (u32) cmd_dma_handle;
  4816. }
  4817. static int hpsa_ioaccel_submit(struct ctlr_info *h,
  4818. struct CommandList *c, struct scsi_cmnd *cmd,
  4819. unsigned char *scsi3addr)
  4820. {
  4821. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4822. int rc = IO_ACCEL_INELIGIBLE;
  4823. if (!dev)
  4824. return SCSI_MLQUEUE_HOST_BUSY;
  4825. cmd->host_scribble = (unsigned char *) c;
  4826. if (dev->offload_enabled) {
  4827. hpsa_cmd_init(h, c->cmdindex, c);
  4828. c->cmd_type = CMD_SCSI;
  4829. c->scsi_cmd = cmd;
  4830. rc = hpsa_scsi_ioaccel_raid_map(h, c);
  4831. if (rc < 0) /* scsi_dma_map failed. */
  4832. rc = SCSI_MLQUEUE_HOST_BUSY;
  4833. } else if (dev->hba_ioaccel_enabled) {
  4834. hpsa_cmd_init(h, c->cmdindex, c);
  4835. c->cmd_type = CMD_SCSI;
  4836. c->scsi_cmd = cmd;
  4837. rc = hpsa_scsi_ioaccel_direct_map(h, c);
  4838. if (rc < 0) /* scsi_dma_map failed. */
  4839. rc = SCSI_MLQUEUE_HOST_BUSY;
  4840. }
  4841. return rc;
  4842. }
  4843. static void hpsa_command_resubmit_worker(struct work_struct *work)
  4844. {
  4845. struct scsi_cmnd *cmd;
  4846. struct hpsa_scsi_dev_t *dev;
  4847. struct CommandList *c = container_of(work, struct CommandList, work);
  4848. cmd = c->scsi_cmd;
  4849. dev = cmd->device->hostdata;
  4850. if (!dev) {
  4851. cmd->result = DID_NO_CONNECT << 16;
  4852. return hpsa_cmd_free_and_done(c->h, c, cmd);
  4853. }
  4854. if (c->reset_pending)
  4855. return hpsa_cmd_resolve_and_free(c->h, c);
  4856. if (c->abort_pending)
  4857. return hpsa_cmd_abort_and_free(c->h, c, cmd);
  4858. if (c->cmd_type == CMD_IOACCEL2) {
  4859. struct ctlr_info *h = c->h;
  4860. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  4861. int rc;
  4862. if (c2->error_data.serv_response ==
  4863. IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
  4864. rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
  4865. if (rc == 0)
  4866. return;
  4867. if (rc == SCSI_MLQUEUE_HOST_BUSY) {
  4868. /*
  4869. * If we get here, it means dma mapping failed.
  4870. * Try again via scsi mid layer, which will
  4871. * then get SCSI_MLQUEUE_HOST_BUSY.
  4872. */
  4873. cmd->result = DID_IMM_RETRY << 16;
  4874. return hpsa_cmd_free_and_done(h, c, cmd);
  4875. }
  4876. /* else, fall thru and resubmit down CISS path */
  4877. }
  4878. }
  4879. hpsa_cmd_partial_init(c->h, c->cmdindex, c);
  4880. if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
  4881. /*
  4882. * If we get here, it means dma mapping failed. Try
  4883. * again via scsi mid layer, which will then get
  4884. * SCSI_MLQUEUE_HOST_BUSY.
  4885. *
  4886. * hpsa_ciss_submit will have already freed c
  4887. * if it encountered a dma mapping failure.
  4888. */
  4889. cmd->result = DID_IMM_RETRY << 16;
  4890. cmd->scsi_done(cmd);
  4891. }
  4892. }
  4893. /* Running in struct Scsi_Host->host_lock less mode */
  4894. static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
  4895. {
  4896. struct ctlr_info *h;
  4897. struct hpsa_scsi_dev_t *dev;
  4898. unsigned char scsi3addr[8];
  4899. struct CommandList *c;
  4900. int rc = 0;
  4901. /* Get the ptr to our adapter structure out of cmd->host. */
  4902. h = sdev_to_hba(cmd->device);
  4903. BUG_ON(cmd->request->tag < 0);
  4904. dev = cmd->device->hostdata;
  4905. if (!dev) {
  4906. cmd->result = NOT_READY << 16; /* host byte */
  4907. cmd->scsi_done(cmd);
  4908. return 0;
  4909. }
  4910. if (dev->removed) {
  4911. cmd->result = DID_NO_CONNECT << 16;
  4912. cmd->scsi_done(cmd);
  4913. return 0;
  4914. }
  4915. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  4916. if (unlikely(lockup_detected(h))) {
  4917. cmd->result = DID_NO_CONNECT << 16;
  4918. cmd->scsi_done(cmd);
  4919. return 0;
  4920. }
  4921. c = cmd_tagged_alloc(h, cmd);
  4922. /*
  4923. * Call alternate submit routine for I/O accelerated commands.
  4924. * Retries always go down the normal I/O path.
  4925. */
  4926. if (likely(cmd->retries == 0 &&
  4927. cmd->request->cmd_type == REQ_TYPE_FS &&
  4928. h->acciopath_status)) {
  4929. rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
  4930. if (rc == 0)
  4931. return 0;
  4932. if (rc == SCSI_MLQUEUE_HOST_BUSY) {
  4933. hpsa_cmd_resolve_and_free(h, c);
  4934. return SCSI_MLQUEUE_HOST_BUSY;
  4935. }
  4936. }
  4937. return hpsa_ciss_submit(h, c, cmd, scsi3addr);
  4938. }
  4939. static void hpsa_scan_complete(struct ctlr_info *h)
  4940. {
  4941. unsigned long flags;
  4942. spin_lock_irqsave(&h->scan_lock, flags);
  4943. h->scan_finished = 1;
  4944. wake_up(&h->scan_wait_queue);
  4945. spin_unlock_irqrestore(&h->scan_lock, flags);
  4946. }
  4947. static void hpsa_scan_start(struct Scsi_Host *sh)
  4948. {
  4949. struct ctlr_info *h = shost_to_hba(sh);
  4950. unsigned long flags;
  4951. /*
  4952. * Don't let rescans be initiated on a controller known to be locked
  4953. * up. If the controller locks up *during* a rescan, that thread is
  4954. * probably hosed, but at least we can prevent new rescan threads from
  4955. * piling up on a locked up controller.
  4956. */
  4957. if (unlikely(lockup_detected(h)))
  4958. return hpsa_scan_complete(h);
  4959. /*
  4960. * If a scan is already waiting to run, no need to add another
  4961. */
  4962. spin_lock_irqsave(&h->scan_lock, flags);
  4963. if (h->scan_waiting) {
  4964. spin_unlock_irqrestore(&h->scan_lock, flags);
  4965. return;
  4966. }
  4967. spin_unlock_irqrestore(&h->scan_lock, flags);
  4968. /* wait until any scan already in progress is finished. */
  4969. while (1) {
  4970. spin_lock_irqsave(&h->scan_lock, flags);
  4971. if (h->scan_finished)
  4972. break;
  4973. h->scan_waiting = 1;
  4974. spin_unlock_irqrestore(&h->scan_lock, flags);
  4975. wait_event(h->scan_wait_queue, h->scan_finished);
  4976. /* Note: We don't need to worry about a race between this
  4977. * thread and driver unload because the midlayer will
  4978. * have incremented the reference count, so unload won't
  4979. * happen if we're in here.
  4980. */
  4981. }
  4982. h->scan_finished = 0; /* mark scan as in progress */
  4983. h->scan_waiting = 0;
  4984. spin_unlock_irqrestore(&h->scan_lock, flags);
  4985. if (unlikely(lockup_detected(h)))
  4986. return hpsa_scan_complete(h);
  4987. hpsa_update_scsi_devices(h);
  4988. hpsa_scan_complete(h);
  4989. }
  4990. static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
  4991. {
  4992. struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
  4993. if (!logical_drive)
  4994. return -ENODEV;
  4995. if (qdepth < 1)
  4996. qdepth = 1;
  4997. else if (qdepth > logical_drive->queue_depth)
  4998. qdepth = logical_drive->queue_depth;
  4999. return scsi_change_queue_depth(sdev, qdepth);
  5000. }
  5001. static int hpsa_scan_finished(struct Scsi_Host *sh,
  5002. unsigned long elapsed_time)
  5003. {
  5004. struct ctlr_info *h = shost_to_hba(sh);
  5005. unsigned long flags;
  5006. int finished;
  5007. spin_lock_irqsave(&h->scan_lock, flags);
  5008. finished = h->scan_finished;
  5009. spin_unlock_irqrestore(&h->scan_lock, flags);
  5010. return finished;
  5011. }
  5012. static int hpsa_scsi_host_alloc(struct ctlr_info *h)
  5013. {
  5014. struct Scsi_Host *sh;
  5015. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  5016. if (sh == NULL) {
  5017. dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
  5018. return -ENOMEM;
  5019. }
  5020. sh->io_port = 0;
  5021. sh->n_io_port = 0;
  5022. sh->this_id = -1;
  5023. sh->max_channel = 3;
  5024. sh->max_cmd_len = MAX_COMMAND_SIZE;
  5025. sh->max_lun = HPSA_MAX_LUN;
  5026. sh->max_id = HPSA_MAX_LUN;
  5027. sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
  5028. sh->cmd_per_lun = sh->can_queue;
  5029. sh->sg_tablesize = h->maxsgentries;
  5030. sh->transportt = hpsa_sas_transport_template;
  5031. sh->hostdata[0] = (unsigned long) h;
  5032. sh->irq = h->intr[h->intr_mode];
  5033. sh->unique_id = sh->irq;
  5034. h->scsi_host = sh;
  5035. return 0;
  5036. }
  5037. static int hpsa_scsi_add_host(struct ctlr_info *h)
  5038. {
  5039. int rv;
  5040. rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
  5041. if (rv) {
  5042. dev_err(&h->pdev->dev, "scsi_add_host failed\n");
  5043. return rv;
  5044. }
  5045. scsi_scan_host(h->scsi_host);
  5046. return 0;
  5047. }
  5048. /*
  5049. * The block layer has already gone to the trouble of picking out a unique,
  5050. * small-integer tag for this request. We use an offset from that value as
  5051. * an index to select our command block. (The offset allows us to reserve the
  5052. * low-numbered entries for our own uses.)
  5053. */
  5054. static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
  5055. {
  5056. int idx = scmd->request->tag;
  5057. if (idx < 0)
  5058. return idx;
  5059. /* Offset to leave space for internal cmds. */
  5060. return idx += HPSA_NRESERVED_CMDS;
  5061. }
  5062. /*
  5063. * Send a TEST_UNIT_READY command to the specified LUN using the specified
  5064. * reply queue; returns zero if the unit is ready, and non-zero otherwise.
  5065. */
  5066. static int hpsa_send_test_unit_ready(struct ctlr_info *h,
  5067. struct CommandList *c, unsigned char lunaddr[],
  5068. int reply_queue)
  5069. {
  5070. int rc;
  5071. /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
  5072. (void) fill_cmd(c, TEST_UNIT_READY, h,
  5073. NULL, 0, 0, lunaddr, TYPE_CMD);
  5074. rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
  5075. if (rc)
  5076. return rc;
  5077. /* no unmap needed here because no data xfer. */
  5078. /* Check if the unit is already ready. */
  5079. if (c->err_info->CommandStatus == CMD_SUCCESS)
  5080. return 0;
  5081. /*
  5082. * The first command sent after reset will receive "unit attention" to
  5083. * indicate that the LUN has been reset...this is actually what we're
  5084. * looking for (but, success is good too).
  5085. */
  5086. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  5087. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  5088. (c->err_info->SenseInfo[2] == NO_SENSE ||
  5089. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  5090. return 0;
  5091. return 1;
  5092. }
  5093. /*
  5094. * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
  5095. * returns zero when the unit is ready, and non-zero when giving up.
  5096. */
  5097. static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
  5098. struct CommandList *c,
  5099. unsigned char lunaddr[], int reply_queue)
  5100. {
  5101. int rc;
  5102. int count = 0;
  5103. int waittime = 1; /* seconds */
  5104. /* Send test unit ready until device ready, or give up. */
  5105. for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
  5106. /*
  5107. * Wait for a bit. do this first, because if we send
  5108. * the TUR right away, the reset will just abort it.
  5109. */
  5110. msleep(1000 * waittime);
  5111. rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
  5112. if (!rc)
  5113. break;
  5114. /* Increase wait time with each try, up to a point. */
  5115. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  5116. waittime *= 2;
  5117. dev_warn(&h->pdev->dev,
  5118. "waiting %d secs for device to become ready.\n",
  5119. waittime);
  5120. }
  5121. return rc;
  5122. }
  5123. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  5124. unsigned char lunaddr[],
  5125. int reply_queue)
  5126. {
  5127. int first_queue;
  5128. int last_queue;
  5129. int rq;
  5130. int rc = 0;
  5131. struct CommandList *c;
  5132. c = cmd_alloc(h);
  5133. /*
  5134. * If no specific reply queue was requested, then send the TUR
  5135. * repeatedly, requesting a reply on each reply queue; otherwise execute
  5136. * the loop exactly once using only the specified queue.
  5137. */
  5138. if (reply_queue == DEFAULT_REPLY_QUEUE) {
  5139. first_queue = 0;
  5140. last_queue = h->nreply_queues - 1;
  5141. } else {
  5142. first_queue = reply_queue;
  5143. last_queue = reply_queue;
  5144. }
  5145. for (rq = first_queue; rq <= last_queue; rq++) {
  5146. rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
  5147. if (rc)
  5148. break;
  5149. }
  5150. if (rc)
  5151. dev_warn(&h->pdev->dev, "giving up on device.\n");
  5152. else
  5153. dev_warn(&h->pdev->dev, "device is ready.\n");
  5154. cmd_free(h, c);
  5155. return rc;
  5156. }
  5157. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  5158. * complaining. Doing a host- or bus-reset can't do anything good here.
  5159. */
  5160. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  5161. {
  5162. int rc;
  5163. struct ctlr_info *h;
  5164. struct hpsa_scsi_dev_t *dev;
  5165. u8 reset_type;
  5166. char msg[48];
  5167. /* find the controller to which the command to be aborted was sent */
  5168. h = sdev_to_hba(scsicmd->device);
  5169. if (h == NULL) /* paranoia */
  5170. return FAILED;
  5171. if (lockup_detected(h))
  5172. return FAILED;
  5173. dev = scsicmd->device->hostdata;
  5174. if (!dev) {
  5175. dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
  5176. return FAILED;
  5177. }
  5178. /* if controller locked up, we can guarantee command won't complete */
  5179. if (lockup_detected(h)) {
  5180. snprintf(msg, sizeof(msg),
  5181. "cmd %d RESET FAILED, lockup detected",
  5182. hpsa_get_cmd_index(scsicmd));
  5183. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5184. return FAILED;
  5185. }
  5186. /* this reset request might be the result of a lockup; check */
  5187. if (detect_controller_lockup(h)) {
  5188. snprintf(msg, sizeof(msg),
  5189. "cmd %d RESET FAILED, new lockup detected",
  5190. hpsa_get_cmd_index(scsicmd));
  5191. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5192. return FAILED;
  5193. }
  5194. /* Do not attempt on controller */
  5195. if (is_hba_lunid(dev->scsi3addr))
  5196. return SUCCESS;
  5197. if (is_logical_dev_addr_mode(dev->scsi3addr))
  5198. reset_type = HPSA_DEVICE_RESET_MSG;
  5199. else
  5200. reset_type = HPSA_PHYS_TARGET_RESET;
  5201. sprintf(msg, "resetting %s",
  5202. reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
  5203. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5204. h->reset_in_progress = 1;
  5205. /* send a reset to the SCSI LUN which the command was sent to */
  5206. rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
  5207. DEFAULT_REPLY_QUEUE);
  5208. sprintf(msg, "reset %s %s",
  5209. reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
  5210. rc == 0 ? "completed successfully" : "failed");
  5211. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5212. h->reset_in_progress = 0;
  5213. return rc == 0 ? SUCCESS : FAILED;
  5214. }
  5215. static void swizzle_abort_tag(u8 *tag)
  5216. {
  5217. u8 original_tag[8];
  5218. memcpy(original_tag, tag, 8);
  5219. tag[0] = original_tag[3];
  5220. tag[1] = original_tag[2];
  5221. tag[2] = original_tag[1];
  5222. tag[3] = original_tag[0];
  5223. tag[4] = original_tag[7];
  5224. tag[5] = original_tag[6];
  5225. tag[6] = original_tag[5];
  5226. tag[7] = original_tag[4];
  5227. }
  5228. static void hpsa_get_tag(struct ctlr_info *h,
  5229. struct CommandList *c, __le32 *taglower, __le32 *tagupper)
  5230. {
  5231. u64 tag;
  5232. if (c->cmd_type == CMD_IOACCEL1) {
  5233. struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
  5234. &h->ioaccel_cmd_pool[c->cmdindex];
  5235. tag = le64_to_cpu(cm1->tag);
  5236. *tagupper = cpu_to_le32(tag >> 32);
  5237. *taglower = cpu_to_le32(tag);
  5238. return;
  5239. }
  5240. if (c->cmd_type == CMD_IOACCEL2) {
  5241. struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
  5242. &h->ioaccel2_cmd_pool[c->cmdindex];
  5243. /* upper tag not used in ioaccel2 mode */
  5244. memset(tagupper, 0, sizeof(*tagupper));
  5245. *taglower = cm2->Tag;
  5246. return;
  5247. }
  5248. tag = le64_to_cpu(c->Header.tag);
  5249. *tagupper = cpu_to_le32(tag >> 32);
  5250. *taglower = cpu_to_le32(tag);
  5251. }
  5252. static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
  5253. struct CommandList *abort, int reply_queue)
  5254. {
  5255. int rc = IO_OK;
  5256. struct CommandList *c;
  5257. struct ErrorInfo *ei;
  5258. __le32 tagupper, taglower;
  5259. c = cmd_alloc(h);
  5260. /* fill_cmd can't fail here, no buffer to map */
  5261. (void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
  5262. 0, 0, scsi3addr, TYPE_MSG);
  5263. if (h->needs_abort_tags_swizzled)
  5264. swizzle_abort_tag(&c->Request.CDB[4]);
  5265. (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
  5266. hpsa_get_tag(h, abort, &taglower, &tagupper);
  5267. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
  5268. __func__, tagupper, taglower);
  5269. /* no unmap needed here because no data xfer. */
  5270. ei = c->err_info;
  5271. switch (ei->CommandStatus) {
  5272. case CMD_SUCCESS:
  5273. break;
  5274. case CMD_TMF_STATUS:
  5275. rc = hpsa_evaluate_tmf_status(h, c);
  5276. break;
  5277. case CMD_UNABORTABLE: /* Very common, don't make noise. */
  5278. rc = -1;
  5279. break;
  5280. default:
  5281. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
  5282. __func__, tagupper, taglower);
  5283. hpsa_scsi_interpret_error(h, c);
  5284. rc = -1;
  5285. break;
  5286. }
  5287. cmd_free(h, c);
  5288. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
  5289. __func__, tagupper, taglower);
  5290. return rc;
  5291. }
  5292. static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
  5293. struct CommandList *command_to_abort, int reply_queue)
  5294. {
  5295. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  5296. struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
  5297. struct io_accel2_cmd *c2a =
  5298. &h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
  5299. struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
  5300. struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
  5301. if (!dev)
  5302. return;
  5303. /*
  5304. * We're overlaying struct hpsa_tmf_struct on top of something which
  5305. * was allocated as a struct io_accel2_cmd, so we better be sure it
  5306. * actually fits, and doesn't overrun the error info space.
  5307. */
  5308. BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
  5309. sizeof(struct io_accel2_cmd));
  5310. BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
  5311. offsetof(struct hpsa_tmf_struct, error_len) +
  5312. sizeof(ac->error_len));
  5313. c->cmd_type = IOACCEL2_TMF;
  5314. c->scsi_cmd = SCSI_CMD_BUSY;
  5315. /* Adjust the DMA address to point to the accelerated command buffer */
  5316. c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
  5317. (c->cmdindex * sizeof(struct io_accel2_cmd));
  5318. BUG_ON(c->busaddr & 0x0000007F);
  5319. memset(ac, 0, sizeof(*c2)); /* yes this is correct */
  5320. ac->iu_type = IOACCEL2_IU_TMF_TYPE;
  5321. ac->reply_queue = reply_queue;
  5322. ac->tmf = IOACCEL2_TMF_ABORT;
  5323. ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
  5324. memset(ac->lun_id, 0, sizeof(ac->lun_id));
  5325. ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
  5326. ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
  5327. ac->error_ptr = cpu_to_le64(c->busaddr +
  5328. offsetof(struct io_accel2_cmd, error_data));
  5329. ac->error_len = cpu_to_le32(sizeof(c2->error_data));
  5330. }
  5331. /* ioaccel2 path firmware cannot handle abort task requests.
  5332. * Change abort requests to physical target reset, and send to the
  5333. * address of the physical disk used for the ioaccel 2 command.
  5334. * Return 0 on success (IO_OK)
  5335. * -1 on failure
  5336. */
  5337. static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
  5338. unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
  5339. {
  5340. int rc = IO_OK;
  5341. struct scsi_cmnd *scmd; /* scsi command within request being aborted */
  5342. struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
  5343. unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
  5344. unsigned char *psa = &phys_scsi3addr[0];
  5345. /* Get a pointer to the hpsa logical device. */
  5346. scmd = abort->scsi_cmd;
  5347. dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
  5348. if (dev == NULL) {
  5349. dev_warn(&h->pdev->dev,
  5350. "Cannot abort: no device pointer for command.\n");
  5351. return -1; /* not abortable */
  5352. }
  5353. if (h->raid_offload_debug > 0)
  5354. dev_info(&h->pdev->dev,
  5355. "scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5356. h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
  5357. "Reset as abort",
  5358. scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
  5359. scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
  5360. if (!dev->offload_enabled) {
  5361. dev_warn(&h->pdev->dev,
  5362. "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
  5363. return -1; /* not abortable */
  5364. }
  5365. /* Incoming scsi3addr is logical addr. We need physical disk addr. */
  5366. if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
  5367. dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
  5368. return -1; /* not abortable */
  5369. }
  5370. /* send the reset */
  5371. if (h->raid_offload_debug > 0)
  5372. dev_info(&h->pdev->dev,
  5373. "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5374. psa[0], psa[1], psa[2], psa[3],
  5375. psa[4], psa[5], psa[6], psa[7]);
  5376. rc = hpsa_do_reset(h, dev, psa, HPSA_PHYS_TARGET_RESET, reply_queue);
  5377. if (rc != 0) {
  5378. dev_warn(&h->pdev->dev,
  5379. "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5380. psa[0], psa[1], psa[2], psa[3],
  5381. psa[4], psa[5], psa[6], psa[7]);
  5382. return rc; /* failed to reset */
  5383. }
  5384. /* wait for device to recover */
  5385. if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
  5386. dev_warn(&h->pdev->dev,
  5387. "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5388. psa[0], psa[1], psa[2], psa[3],
  5389. psa[4], psa[5], psa[6], psa[7]);
  5390. return -1; /* failed to recover */
  5391. }
  5392. /* device recovered */
  5393. dev_info(&h->pdev->dev,
  5394. "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5395. psa[0], psa[1], psa[2], psa[3],
  5396. psa[4], psa[5], psa[6], psa[7]);
  5397. return rc; /* success */
  5398. }
  5399. static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
  5400. struct CommandList *abort, int reply_queue)
  5401. {
  5402. int rc = IO_OK;
  5403. struct CommandList *c;
  5404. __le32 taglower, tagupper;
  5405. struct hpsa_scsi_dev_t *dev;
  5406. struct io_accel2_cmd *c2;
  5407. dev = abort->scsi_cmd->device->hostdata;
  5408. if (!dev)
  5409. return -1;
  5410. if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
  5411. return -1;
  5412. c = cmd_alloc(h);
  5413. setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
  5414. c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  5415. (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
  5416. hpsa_get_tag(h, abort, &taglower, &tagupper);
  5417. dev_dbg(&h->pdev->dev,
  5418. "%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
  5419. __func__, tagupper, taglower);
  5420. /* no unmap needed here because no data xfer. */
  5421. dev_dbg(&h->pdev->dev,
  5422. "%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
  5423. __func__, tagupper, taglower, c2->error_data.serv_response);
  5424. switch (c2->error_data.serv_response) {
  5425. case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
  5426. case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
  5427. rc = 0;
  5428. break;
  5429. case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
  5430. case IOACCEL2_SERV_RESPONSE_FAILURE:
  5431. case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
  5432. rc = -1;
  5433. break;
  5434. default:
  5435. dev_warn(&h->pdev->dev,
  5436. "%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
  5437. __func__, tagupper, taglower,
  5438. c2->error_data.serv_response);
  5439. rc = -1;
  5440. }
  5441. cmd_free(h, c);
  5442. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
  5443. tagupper, taglower);
  5444. return rc;
  5445. }
  5446. static int hpsa_send_abort_both_ways(struct ctlr_info *h,
  5447. struct hpsa_scsi_dev_t *dev, struct CommandList *abort, int reply_queue)
  5448. {
  5449. /*
  5450. * ioccelerator mode 2 commands should be aborted via the
  5451. * accelerated path, since RAID path is unaware of these commands,
  5452. * but not all underlying firmware can handle abort TMF.
  5453. * Change abort to physical device reset when abort TMF is unsupported.
  5454. */
  5455. if (abort->cmd_type == CMD_IOACCEL2) {
  5456. if ((HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags) ||
  5457. dev->physical_device)
  5458. return hpsa_send_abort_ioaccel2(h, abort,
  5459. reply_queue);
  5460. else
  5461. return hpsa_send_reset_as_abort_ioaccel2(h,
  5462. dev->scsi3addr,
  5463. abort, reply_queue);
  5464. }
  5465. return hpsa_send_abort(h, dev->scsi3addr, abort, reply_queue);
  5466. }
  5467. /* Find out which reply queue a command was meant to return on */
  5468. static int hpsa_extract_reply_queue(struct ctlr_info *h,
  5469. struct CommandList *c)
  5470. {
  5471. if (c->cmd_type == CMD_IOACCEL2)
  5472. return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
  5473. return c->Header.ReplyQueue;
  5474. }
  5475. /*
  5476. * Limit concurrency of abort commands to prevent
  5477. * over-subscription of commands
  5478. */
  5479. static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
  5480. {
  5481. #define ABORT_CMD_WAIT_MSECS 5000
  5482. return !wait_event_timeout(h->abort_cmd_wait_queue,
  5483. atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
  5484. msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
  5485. }
  5486. /* Send an abort for the specified command.
  5487. * If the device and controller support it,
  5488. * send a task abort request.
  5489. */
  5490. static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
  5491. {
  5492. int rc;
  5493. struct ctlr_info *h;
  5494. struct hpsa_scsi_dev_t *dev;
  5495. struct CommandList *abort; /* pointer to command to be aborted */
  5496. struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
  5497. char msg[256]; /* For debug messaging. */
  5498. int ml = 0;
  5499. __le32 tagupper, taglower;
  5500. int refcount, reply_queue;
  5501. if (sc == NULL)
  5502. return FAILED;
  5503. if (sc->device == NULL)
  5504. return FAILED;
  5505. /* Find the controller of the command to be aborted */
  5506. h = sdev_to_hba(sc->device);
  5507. if (h == NULL)
  5508. return FAILED;
  5509. /* Find the device of the command to be aborted */
  5510. dev = sc->device->hostdata;
  5511. if (!dev) {
  5512. dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
  5513. msg);
  5514. return FAILED;
  5515. }
  5516. /* If controller locked up, we can guarantee command won't complete */
  5517. if (lockup_detected(h)) {
  5518. hpsa_show_dev_msg(KERN_WARNING, h, dev,
  5519. "ABORT FAILED, lockup detected");
  5520. return FAILED;
  5521. }
  5522. /* This is a good time to check if controller lockup has occurred */
  5523. if (detect_controller_lockup(h)) {
  5524. hpsa_show_dev_msg(KERN_WARNING, h, dev,
  5525. "ABORT FAILED, new lockup detected");
  5526. return FAILED;
  5527. }
  5528. /* Check that controller supports some kind of task abort */
  5529. if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
  5530. !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
  5531. return FAILED;
  5532. memset(msg, 0, sizeof(msg));
  5533. ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
  5534. h->scsi_host->host_no, sc->device->channel,
  5535. sc->device->id, sc->device->lun,
  5536. "Aborting command", sc);
  5537. /* Get SCSI command to be aborted */
  5538. abort = (struct CommandList *) sc->host_scribble;
  5539. if (abort == NULL) {
  5540. /* This can happen if the command already completed. */
  5541. return SUCCESS;
  5542. }
  5543. refcount = atomic_inc_return(&abort->refcount);
  5544. if (refcount == 1) { /* Command is done already. */
  5545. cmd_free(h, abort);
  5546. return SUCCESS;
  5547. }
  5548. /* Don't bother trying the abort if we know it won't work. */
  5549. if (abort->cmd_type != CMD_IOACCEL2 &&
  5550. abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
  5551. cmd_free(h, abort);
  5552. return FAILED;
  5553. }
  5554. /*
  5555. * Check that we're aborting the right command.
  5556. * It's possible the CommandList already completed and got re-used.
  5557. */
  5558. if (abort->scsi_cmd != sc) {
  5559. cmd_free(h, abort);
  5560. return SUCCESS;
  5561. }
  5562. abort->abort_pending = true;
  5563. hpsa_get_tag(h, abort, &taglower, &tagupper);
  5564. reply_queue = hpsa_extract_reply_queue(h, abort);
  5565. ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
  5566. as = abort->scsi_cmd;
  5567. if (as != NULL)
  5568. ml += sprintf(msg+ml,
  5569. "CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
  5570. as->cmd_len, as->cmnd[0], as->cmnd[1],
  5571. as->serial_number);
  5572. dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
  5573. hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
  5574. /*
  5575. * Command is in flight, or possibly already completed
  5576. * by the firmware (but not to the scsi mid layer) but we can't
  5577. * distinguish which. Send the abort down.
  5578. */
  5579. if (wait_for_available_abort_cmd(h)) {
  5580. dev_warn(&h->pdev->dev,
  5581. "%s FAILED, timeout waiting for an abort command to become available.\n",
  5582. msg);
  5583. cmd_free(h, abort);
  5584. return FAILED;
  5585. }
  5586. rc = hpsa_send_abort_both_ways(h, dev, abort, reply_queue);
  5587. atomic_inc(&h->abort_cmds_available);
  5588. wake_up_all(&h->abort_cmd_wait_queue);
  5589. if (rc != 0) {
  5590. dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
  5591. hpsa_show_dev_msg(KERN_WARNING, h, dev,
  5592. "FAILED to abort command");
  5593. cmd_free(h, abort);
  5594. return FAILED;
  5595. }
  5596. dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
  5597. wait_event(h->event_sync_wait_queue,
  5598. abort->scsi_cmd != sc || lockup_detected(h));
  5599. cmd_free(h, abort);
  5600. return !lockup_detected(h) ? SUCCESS : FAILED;
  5601. }
  5602. /*
  5603. * For operations with an associated SCSI command, a command block is allocated
  5604. * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
  5605. * block request tag as an index into a table of entries. cmd_tagged_free() is
  5606. * the complement, although cmd_free() may be called instead.
  5607. */
  5608. static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
  5609. struct scsi_cmnd *scmd)
  5610. {
  5611. int idx = hpsa_get_cmd_index(scmd);
  5612. struct CommandList *c = h->cmd_pool + idx;
  5613. if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
  5614. dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
  5615. idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
  5616. /* The index value comes from the block layer, so if it's out of
  5617. * bounds, it's probably not our bug.
  5618. */
  5619. BUG();
  5620. }
  5621. atomic_inc(&c->refcount);
  5622. if (unlikely(!hpsa_is_cmd_idle(c))) {
  5623. /*
  5624. * We expect that the SCSI layer will hand us a unique tag
  5625. * value. Thus, there should never be a collision here between
  5626. * two requests...because if the selected command isn't idle
  5627. * then someone is going to be very disappointed.
  5628. */
  5629. dev_err(&h->pdev->dev,
  5630. "tag collision (tag=%d) in cmd_tagged_alloc().\n",
  5631. idx);
  5632. if (c->scsi_cmd != NULL)
  5633. scsi_print_command(c->scsi_cmd);
  5634. scsi_print_command(scmd);
  5635. }
  5636. hpsa_cmd_partial_init(h, idx, c);
  5637. return c;
  5638. }
  5639. static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
  5640. {
  5641. /*
  5642. * Release our reference to the block. We don't need to do anything
  5643. * else to free it, because it is accessed by index. (There's no point
  5644. * in checking the result of the decrement, since we cannot guarantee
  5645. * that there isn't a concurrent abort which is also accessing it.)
  5646. */
  5647. (void)atomic_dec(&c->refcount);
  5648. }
  5649. /*
  5650. * For operations that cannot sleep, a command block is allocated at init,
  5651. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  5652. * which ones are free or in use. Lock must be held when calling this.
  5653. * cmd_free() is the complement.
  5654. * This function never gives up and returns NULL. If it hangs,
  5655. * another thread must call cmd_free() to free some tags.
  5656. */
  5657. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  5658. {
  5659. struct CommandList *c;
  5660. int refcount, i;
  5661. int offset = 0;
  5662. /*
  5663. * There is some *extremely* small but non-zero chance that that
  5664. * multiple threads could get in here, and one thread could
  5665. * be scanning through the list of bits looking for a free
  5666. * one, but the free ones are always behind him, and other
  5667. * threads sneak in behind him and eat them before he can
  5668. * get to them, so that while there is always a free one, a
  5669. * very unlucky thread might be starved anyway, never able to
  5670. * beat the other threads. In reality, this happens so
  5671. * infrequently as to be indistinguishable from never.
  5672. *
  5673. * Note that we start allocating commands before the SCSI host structure
  5674. * is initialized. Since the search starts at bit zero, this
  5675. * all works, since we have at least one command structure available;
  5676. * however, it means that the structures with the low indexes have to be
  5677. * reserved for driver-initiated requests, while requests from the block
  5678. * layer will use the higher indexes.
  5679. */
  5680. for (;;) {
  5681. i = find_next_zero_bit(h->cmd_pool_bits,
  5682. HPSA_NRESERVED_CMDS,
  5683. offset);
  5684. if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
  5685. offset = 0;
  5686. continue;
  5687. }
  5688. c = h->cmd_pool + i;
  5689. refcount = atomic_inc_return(&c->refcount);
  5690. if (unlikely(refcount > 1)) {
  5691. cmd_free(h, c); /* already in use */
  5692. offset = (i + 1) % HPSA_NRESERVED_CMDS;
  5693. continue;
  5694. }
  5695. set_bit(i & (BITS_PER_LONG - 1),
  5696. h->cmd_pool_bits + (i / BITS_PER_LONG));
  5697. break; /* it's ours now. */
  5698. }
  5699. hpsa_cmd_partial_init(h, i, c);
  5700. return c;
  5701. }
  5702. /*
  5703. * This is the complementary operation to cmd_alloc(). Note, however, in some
  5704. * corner cases it may also be used to free blocks allocated by
  5705. * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
  5706. * the clear-bit is harmless.
  5707. */
  5708. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  5709. {
  5710. if (atomic_dec_and_test(&c->refcount)) {
  5711. int i;
  5712. i = c - h->cmd_pool;
  5713. clear_bit(i & (BITS_PER_LONG - 1),
  5714. h->cmd_pool_bits + (i / BITS_PER_LONG));
  5715. }
  5716. }
  5717. #ifdef CONFIG_COMPAT
  5718. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
  5719. void __user *arg)
  5720. {
  5721. IOCTL32_Command_struct __user *arg32 =
  5722. (IOCTL32_Command_struct __user *) arg;
  5723. IOCTL_Command_struct arg64;
  5724. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  5725. int err;
  5726. u32 cp;
  5727. memset(&arg64, 0, sizeof(arg64));
  5728. err = 0;
  5729. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  5730. sizeof(arg64.LUN_info));
  5731. err |= copy_from_user(&arg64.Request, &arg32->Request,
  5732. sizeof(arg64.Request));
  5733. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  5734. sizeof(arg64.error_info));
  5735. err |= get_user(arg64.buf_size, &arg32->buf_size);
  5736. err |= get_user(cp, &arg32->buf);
  5737. arg64.buf = compat_ptr(cp);
  5738. err |= copy_to_user(p, &arg64, sizeof(arg64));
  5739. if (err)
  5740. return -EFAULT;
  5741. err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
  5742. if (err)
  5743. return err;
  5744. err |= copy_in_user(&arg32->error_info, &p->error_info,
  5745. sizeof(arg32->error_info));
  5746. if (err)
  5747. return -EFAULT;
  5748. return err;
  5749. }
  5750. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  5751. int cmd, void __user *arg)
  5752. {
  5753. BIG_IOCTL32_Command_struct __user *arg32 =
  5754. (BIG_IOCTL32_Command_struct __user *) arg;
  5755. BIG_IOCTL_Command_struct arg64;
  5756. BIG_IOCTL_Command_struct __user *p =
  5757. compat_alloc_user_space(sizeof(arg64));
  5758. int err;
  5759. u32 cp;
  5760. memset(&arg64, 0, sizeof(arg64));
  5761. err = 0;
  5762. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  5763. sizeof(arg64.LUN_info));
  5764. err |= copy_from_user(&arg64.Request, &arg32->Request,
  5765. sizeof(arg64.Request));
  5766. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  5767. sizeof(arg64.error_info));
  5768. err |= get_user(arg64.buf_size, &arg32->buf_size);
  5769. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  5770. err |= get_user(cp, &arg32->buf);
  5771. arg64.buf = compat_ptr(cp);
  5772. err |= copy_to_user(p, &arg64, sizeof(arg64));
  5773. if (err)
  5774. return -EFAULT;
  5775. err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
  5776. if (err)
  5777. return err;
  5778. err |= copy_in_user(&arg32->error_info, &p->error_info,
  5779. sizeof(arg32->error_info));
  5780. if (err)
  5781. return -EFAULT;
  5782. return err;
  5783. }
  5784. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  5785. {
  5786. switch (cmd) {
  5787. case CCISS_GETPCIINFO:
  5788. case CCISS_GETINTINFO:
  5789. case CCISS_SETINTINFO:
  5790. case CCISS_GETNODENAME:
  5791. case CCISS_SETNODENAME:
  5792. case CCISS_GETHEARTBEAT:
  5793. case CCISS_GETBUSTYPES:
  5794. case CCISS_GETFIRMVER:
  5795. case CCISS_GETDRIVVER:
  5796. case CCISS_REVALIDVOLS:
  5797. case CCISS_DEREGDISK:
  5798. case CCISS_REGNEWDISK:
  5799. case CCISS_REGNEWD:
  5800. case CCISS_RESCANDISK:
  5801. case CCISS_GETLUNINFO:
  5802. return hpsa_ioctl(dev, cmd, arg);
  5803. case CCISS_PASSTHRU32:
  5804. return hpsa_ioctl32_passthru(dev, cmd, arg);
  5805. case CCISS_BIG_PASSTHRU32:
  5806. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  5807. default:
  5808. return -ENOIOCTLCMD;
  5809. }
  5810. }
  5811. #endif
  5812. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  5813. {
  5814. struct hpsa_pci_info pciinfo;
  5815. if (!argp)
  5816. return -EINVAL;
  5817. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  5818. pciinfo.bus = h->pdev->bus->number;
  5819. pciinfo.dev_fn = h->pdev->devfn;
  5820. pciinfo.board_id = h->board_id;
  5821. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  5822. return -EFAULT;
  5823. return 0;
  5824. }
  5825. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  5826. {
  5827. DriverVer_type DriverVer;
  5828. unsigned char vmaj, vmin, vsubmin;
  5829. int rc;
  5830. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  5831. &vmaj, &vmin, &vsubmin);
  5832. if (rc != 3) {
  5833. dev_info(&h->pdev->dev, "driver version string '%s' "
  5834. "unrecognized.", HPSA_DRIVER_VERSION);
  5835. vmaj = 0;
  5836. vmin = 0;
  5837. vsubmin = 0;
  5838. }
  5839. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  5840. if (!argp)
  5841. return -EINVAL;
  5842. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  5843. return -EFAULT;
  5844. return 0;
  5845. }
  5846. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  5847. {
  5848. IOCTL_Command_struct iocommand;
  5849. struct CommandList *c;
  5850. char *buff = NULL;
  5851. u64 temp64;
  5852. int rc = 0;
  5853. if (!argp)
  5854. return -EINVAL;
  5855. if (!capable(CAP_SYS_RAWIO))
  5856. return -EPERM;
  5857. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  5858. return -EFAULT;
  5859. if ((iocommand.buf_size < 1) &&
  5860. (iocommand.Request.Type.Direction != XFER_NONE)) {
  5861. return -EINVAL;
  5862. }
  5863. if (iocommand.buf_size > 0) {
  5864. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  5865. if (buff == NULL)
  5866. return -ENOMEM;
  5867. if (iocommand.Request.Type.Direction & XFER_WRITE) {
  5868. /* Copy the data into the buffer we created */
  5869. if (copy_from_user(buff, iocommand.buf,
  5870. iocommand.buf_size)) {
  5871. rc = -EFAULT;
  5872. goto out_kfree;
  5873. }
  5874. } else {
  5875. memset(buff, 0, iocommand.buf_size);
  5876. }
  5877. }
  5878. c = cmd_alloc(h);
  5879. /* Fill in the command type */
  5880. c->cmd_type = CMD_IOCTL_PEND;
  5881. c->scsi_cmd = SCSI_CMD_BUSY;
  5882. /* Fill in Command Header */
  5883. c->Header.ReplyQueue = 0; /* unused in simple mode */
  5884. if (iocommand.buf_size > 0) { /* buffer to fill */
  5885. c->Header.SGList = 1;
  5886. c->Header.SGTotal = cpu_to_le16(1);
  5887. } else { /* no buffers to fill */
  5888. c->Header.SGList = 0;
  5889. c->Header.SGTotal = cpu_to_le16(0);
  5890. }
  5891. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  5892. /* Fill in Request block */
  5893. memcpy(&c->Request, &iocommand.Request,
  5894. sizeof(c->Request));
  5895. /* Fill in the scatter gather information */
  5896. if (iocommand.buf_size > 0) {
  5897. temp64 = pci_map_single(h->pdev, buff,
  5898. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  5899. if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
  5900. c->SG[0].Addr = cpu_to_le64(0);
  5901. c->SG[0].Len = cpu_to_le32(0);
  5902. rc = -ENOMEM;
  5903. goto out;
  5904. }
  5905. c->SG[0].Addr = cpu_to_le64(temp64);
  5906. c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
  5907. c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
  5908. }
  5909. rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  5910. NO_TIMEOUT);
  5911. if (iocommand.buf_size > 0)
  5912. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  5913. check_ioctl_unit_attention(h, c);
  5914. if (rc) {
  5915. rc = -EIO;
  5916. goto out;
  5917. }
  5918. /* Copy the error information out */
  5919. memcpy(&iocommand.error_info, c->err_info,
  5920. sizeof(iocommand.error_info));
  5921. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  5922. rc = -EFAULT;
  5923. goto out;
  5924. }
  5925. if ((iocommand.Request.Type.Direction & XFER_READ) &&
  5926. iocommand.buf_size > 0) {
  5927. /* Copy the data out of the buffer we created */
  5928. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  5929. rc = -EFAULT;
  5930. goto out;
  5931. }
  5932. }
  5933. out:
  5934. cmd_free(h, c);
  5935. out_kfree:
  5936. kfree(buff);
  5937. return rc;
  5938. }
  5939. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  5940. {
  5941. BIG_IOCTL_Command_struct *ioc;
  5942. struct CommandList *c;
  5943. unsigned char **buff = NULL;
  5944. int *buff_size = NULL;
  5945. u64 temp64;
  5946. BYTE sg_used = 0;
  5947. int status = 0;
  5948. u32 left;
  5949. u32 sz;
  5950. BYTE __user *data_ptr;
  5951. if (!argp)
  5952. return -EINVAL;
  5953. if (!capable(CAP_SYS_RAWIO))
  5954. return -EPERM;
  5955. ioc = (BIG_IOCTL_Command_struct *)
  5956. kmalloc(sizeof(*ioc), GFP_KERNEL);
  5957. if (!ioc) {
  5958. status = -ENOMEM;
  5959. goto cleanup1;
  5960. }
  5961. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  5962. status = -EFAULT;
  5963. goto cleanup1;
  5964. }
  5965. if ((ioc->buf_size < 1) &&
  5966. (ioc->Request.Type.Direction != XFER_NONE)) {
  5967. status = -EINVAL;
  5968. goto cleanup1;
  5969. }
  5970. /* Check kmalloc limits using all SGs */
  5971. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  5972. status = -EINVAL;
  5973. goto cleanup1;
  5974. }
  5975. if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
  5976. status = -EINVAL;
  5977. goto cleanup1;
  5978. }
  5979. buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
  5980. if (!buff) {
  5981. status = -ENOMEM;
  5982. goto cleanup1;
  5983. }
  5984. buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
  5985. if (!buff_size) {
  5986. status = -ENOMEM;
  5987. goto cleanup1;
  5988. }
  5989. left = ioc->buf_size;
  5990. data_ptr = ioc->buf;
  5991. while (left) {
  5992. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  5993. buff_size[sg_used] = sz;
  5994. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  5995. if (buff[sg_used] == NULL) {
  5996. status = -ENOMEM;
  5997. goto cleanup1;
  5998. }
  5999. if (ioc->Request.Type.Direction & XFER_WRITE) {
  6000. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  6001. status = -EFAULT;
  6002. goto cleanup1;
  6003. }
  6004. } else
  6005. memset(buff[sg_used], 0, sz);
  6006. left -= sz;
  6007. data_ptr += sz;
  6008. sg_used++;
  6009. }
  6010. c = cmd_alloc(h);
  6011. c->cmd_type = CMD_IOCTL_PEND;
  6012. c->scsi_cmd = SCSI_CMD_BUSY;
  6013. c->Header.ReplyQueue = 0;
  6014. c->Header.SGList = (u8) sg_used;
  6015. c->Header.SGTotal = cpu_to_le16(sg_used);
  6016. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  6017. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  6018. if (ioc->buf_size > 0) {
  6019. int i;
  6020. for (i = 0; i < sg_used; i++) {
  6021. temp64 = pci_map_single(h->pdev, buff[i],
  6022. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  6023. if (dma_mapping_error(&h->pdev->dev,
  6024. (dma_addr_t) temp64)) {
  6025. c->SG[i].Addr = cpu_to_le64(0);
  6026. c->SG[i].Len = cpu_to_le32(0);
  6027. hpsa_pci_unmap(h->pdev, c, i,
  6028. PCI_DMA_BIDIRECTIONAL);
  6029. status = -ENOMEM;
  6030. goto cleanup0;
  6031. }
  6032. c->SG[i].Addr = cpu_to_le64(temp64);
  6033. c->SG[i].Len = cpu_to_le32(buff_size[i]);
  6034. c->SG[i].Ext = cpu_to_le32(0);
  6035. }
  6036. c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
  6037. }
  6038. status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  6039. NO_TIMEOUT);
  6040. if (sg_used)
  6041. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  6042. check_ioctl_unit_attention(h, c);
  6043. if (status) {
  6044. status = -EIO;
  6045. goto cleanup0;
  6046. }
  6047. /* Copy the error information out */
  6048. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  6049. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  6050. status = -EFAULT;
  6051. goto cleanup0;
  6052. }
  6053. if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
  6054. int i;
  6055. /* Copy the data out of the buffer we created */
  6056. BYTE __user *ptr = ioc->buf;
  6057. for (i = 0; i < sg_used; i++) {
  6058. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  6059. status = -EFAULT;
  6060. goto cleanup0;
  6061. }
  6062. ptr += buff_size[i];
  6063. }
  6064. }
  6065. status = 0;
  6066. cleanup0:
  6067. cmd_free(h, c);
  6068. cleanup1:
  6069. if (buff) {
  6070. int i;
  6071. for (i = 0; i < sg_used; i++)
  6072. kfree(buff[i]);
  6073. kfree(buff);
  6074. }
  6075. kfree(buff_size);
  6076. kfree(ioc);
  6077. return status;
  6078. }
  6079. static void check_ioctl_unit_attention(struct ctlr_info *h,
  6080. struct CommandList *c)
  6081. {
  6082. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  6083. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  6084. (void) check_for_unit_attention(h, c);
  6085. }
  6086. /*
  6087. * ioctl
  6088. */
  6089. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  6090. {
  6091. struct ctlr_info *h;
  6092. void __user *argp = (void __user *)arg;
  6093. int rc;
  6094. h = sdev_to_hba(dev);
  6095. switch (cmd) {
  6096. case CCISS_DEREGDISK:
  6097. case CCISS_REGNEWDISK:
  6098. case CCISS_REGNEWD:
  6099. hpsa_scan_start(h->scsi_host);
  6100. return 0;
  6101. case CCISS_GETPCIINFO:
  6102. return hpsa_getpciinfo_ioctl(h, argp);
  6103. case CCISS_GETDRIVVER:
  6104. return hpsa_getdrivver_ioctl(h, argp);
  6105. case CCISS_PASSTHRU:
  6106. if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
  6107. return -EAGAIN;
  6108. rc = hpsa_passthru_ioctl(h, argp);
  6109. atomic_inc(&h->passthru_cmds_avail);
  6110. return rc;
  6111. case CCISS_BIG_PASSTHRU:
  6112. if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
  6113. return -EAGAIN;
  6114. rc = hpsa_big_passthru_ioctl(h, argp);
  6115. atomic_inc(&h->passthru_cmds_avail);
  6116. return rc;
  6117. default:
  6118. return -ENOTTY;
  6119. }
  6120. }
  6121. static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
  6122. u8 reset_type)
  6123. {
  6124. struct CommandList *c;
  6125. c = cmd_alloc(h);
  6126. /* fill_cmd can't fail here, no data buffer to map */
  6127. (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
  6128. RAID_CTLR_LUNID, TYPE_MSG);
  6129. c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
  6130. c->waiting = NULL;
  6131. enqueue_cmd_and_start_io(h, c);
  6132. /* Don't wait for completion, the reset won't complete. Don't free
  6133. * the command either. This is the last command we will send before
  6134. * re-initializing everything, so it doesn't matter and won't leak.
  6135. */
  6136. return;
  6137. }
  6138. static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  6139. void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
  6140. int cmd_type)
  6141. {
  6142. int pci_dir = XFER_NONE;
  6143. u64 tag; /* for commands to be aborted */
  6144. c->cmd_type = CMD_IOCTL_PEND;
  6145. c->scsi_cmd = SCSI_CMD_BUSY;
  6146. c->Header.ReplyQueue = 0;
  6147. if (buff != NULL && size > 0) {
  6148. c->Header.SGList = 1;
  6149. c->Header.SGTotal = cpu_to_le16(1);
  6150. } else {
  6151. c->Header.SGList = 0;
  6152. c->Header.SGTotal = cpu_to_le16(0);
  6153. }
  6154. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  6155. if (cmd_type == TYPE_CMD) {
  6156. switch (cmd) {
  6157. case HPSA_INQUIRY:
  6158. /* are we trying to read a vital product page */
  6159. if (page_code & VPD_PAGE) {
  6160. c->Request.CDB[1] = 0x01;
  6161. c->Request.CDB[2] = (page_code & 0xff);
  6162. }
  6163. c->Request.CDBLen = 6;
  6164. c->Request.type_attr_dir =
  6165. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6166. c->Request.Timeout = 0;
  6167. c->Request.CDB[0] = HPSA_INQUIRY;
  6168. c->Request.CDB[4] = size & 0xFF;
  6169. break;
  6170. case HPSA_REPORT_LOG:
  6171. case HPSA_REPORT_PHYS:
  6172. /* Talking to controller so It's a physical command
  6173. mode = 00 target = 0. Nothing to write.
  6174. */
  6175. c->Request.CDBLen = 12;
  6176. c->Request.type_attr_dir =
  6177. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6178. c->Request.Timeout = 0;
  6179. c->Request.CDB[0] = cmd;
  6180. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  6181. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6182. c->Request.CDB[8] = (size >> 8) & 0xFF;
  6183. c->Request.CDB[9] = size & 0xFF;
  6184. break;
  6185. case BMIC_SENSE_DIAG_OPTIONS:
  6186. c->Request.CDBLen = 16;
  6187. c->Request.type_attr_dir =
  6188. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6189. c->Request.Timeout = 0;
  6190. /* Spec says this should be BMIC_WRITE */
  6191. c->Request.CDB[0] = BMIC_READ;
  6192. c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
  6193. break;
  6194. case BMIC_SET_DIAG_OPTIONS:
  6195. c->Request.CDBLen = 16;
  6196. c->Request.type_attr_dir =
  6197. TYPE_ATTR_DIR(cmd_type,
  6198. ATTR_SIMPLE, XFER_WRITE);
  6199. c->Request.Timeout = 0;
  6200. c->Request.CDB[0] = BMIC_WRITE;
  6201. c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
  6202. break;
  6203. case HPSA_CACHE_FLUSH:
  6204. c->Request.CDBLen = 12;
  6205. c->Request.type_attr_dir =
  6206. TYPE_ATTR_DIR(cmd_type,
  6207. ATTR_SIMPLE, XFER_WRITE);
  6208. c->Request.Timeout = 0;
  6209. c->Request.CDB[0] = BMIC_WRITE;
  6210. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  6211. c->Request.CDB[7] = (size >> 8) & 0xFF;
  6212. c->Request.CDB[8] = size & 0xFF;
  6213. break;
  6214. case TEST_UNIT_READY:
  6215. c->Request.CDBLen = 6;
  6216. c->Request.type_attr_dir =
  6217. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
  6218. c->Request.Timeout = 0;
  6219. break;
  6220. case HPSA_GET_RAID_MAP:
  6221. c->Request.CDBLen = 12;
  6222. c->Request.type_attr_dir =
  6223. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6224. c->Request.Timeout = 0;
  6225. c->Request.CDB[0] = HPSA_CISS_READ;
  6226. c->Request.CDB[1] = cmd;
  6227. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  6228. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6229. c->Request.CDB[8] = (size >> 8) & 0xFF;
  6230. c->Request.CDB[9] = size & 0xFF;
  6231. break;
  6232. case BMIC_SENSE_CONTROLLER_PARAMETERS:
  6233. c->Request.CDBLen = 10;
  6234. c->Request.type_attr_dir =
  6235. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6236. c->Request.Timeout = 0;
  6237. c->Request.CDB[0] = BMIC_READ;
  6238. c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
  6239. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6240. c->Request.CDB[8] = (size >> 8) & 0xFF;
  6241. break;
  6242. case BMIC_IDENTIFY_PHYSICAL_DEVICE:
  6243. c->Request.CDBLen = 10;
  6244. c->Request.type_attr_dir =
  6245. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6246. c->Request.Timeout = 0;
  6247. c->Request.CDB[0] = BMIC_READ;
  6248. c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
  6249. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6250. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6251. break;
  6252. case BMIC_SENSE_SUBSYSTEM_INFORMATION:
  6253. c->Request.CDBLen = 10;
  6254. c->Request.type_attr_dir =
  6255. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6256. c->Request.Timeout = 0;
  6257. c->Request.CDB[0] = BMIC_READ;
  6258. c->Request.CDB[6] = BMIC_SENSE_SUBSYSTEM_INFORMATION;
  6259. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6260. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6261. break;
  6262. case BMIC_SENSE_STORAGE_BOX_PARAMS:
  6263. c->Request.CDBLen = 10;
  6264. c->Request.type_attr_dir =
  6265. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6266. c->Request.Timeout = 0;
  6267. c->Request.CDB[0] = BMIC_READ;
  6268. c->Request.CDB[6] = BMIC_SENSE_STORAGE_BOX_PARAMS;
  6269. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6270. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6271. break;
  6272. case BMIC_IDENTIFY_CONTROLLER:
  6273. c->Request.CDBLen = 10;
  6274. c->Request.type_attr_dir =
  6275. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6276. c->Request.Timeout = 0;
  6277. c->Request.CDB[0] = BMIC_READ;
  6278. c->Request.CDB[1] = 0;
  6279. c->Request.CDB[2] = 0;
  6280. c->Request.CDB[3] = 0;
  6281. c->Request.CDB[4] = 0;
  6282. c->Request.CDB[5] = 0;
  6283. c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
  6284. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6285. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6286. c->Request.CDB[9] = 0;
  6287. break;
  6288. default:
  6289. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  6290. BUG();
  6291. return -1;
  6292. }
  6293. } else if (cmd_type == TYPE_MSG) {
  6294. switch (cmd) {
  6295. case HPSA_PHYS_TARGET_RESET:
  6296. c->Request.CDBLen = 16;
  6297. c->Request.type_attr_dir =
  6298. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
  6299. c->Request.Timeout = 0; /* Don't time out */
  6300. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  6301. c->Request.CDB[0] = HPSA_RESET;
  6302. c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
  6303. /* Physical target reset needs no control bytes 4-7*/
  6304. c->Request.CDB[4] = 0x00;
  6305. c->Request.CDB[5] = 0x00;
  6306. c->Request.CDB[6] = 0x00;
  6307. c->Request.CDB[7] = 0x00;
  6308. break;
  6309. case HPSA_DEVICE_RESET_MSG:
  6310. c->Request.CDBLen = 16;
  6311. c->Request.type_attr_dir =
  6312. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
  6313. c->Request.Timeout = 0; /* Don't time out */
  6314. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  6315. c->Request.CDB[0] = cmd;
  6316. c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
  6317. /* If bytes 4-7 are zero, it means reset the */
  6318. /* LunID device */
  6319. c->Request.CDB[4] = 0x00;
  6320. c->Request.CDB[5] = 0x00;
  6321. c->Request.CDB[6] = 0x00;
  6322. c->Request.CDB[7] = 0x00;
  6323. break;
  6324. case HPSA_ABORT_MSG:
  6325. memcpy(&tag, buff, sizeof(tag));
  6326. dev_dbg(&h->pdev->dev,
  6327. "Abort Tag:0x%016llx using rqst Tag:0x%016llx",
  6328. tag, c->Header.tag);
  6329. c->Request.CDBLen = 16;
  6330. c->Request.type_attr_dir =
  6331. TYPE_ATTR_DIR(cmd_type,
  6332. ATTR_SIMPLE, XFER_WRITE);
  6333. c->Request.Timeout = 0; /* Don't time out */
  6334. c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
  6335. c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
  6336. c->Request.CDB[2] = 0x00; /* reserved */
  6337. c->Request.CDB[3] = 0x00; /* reserved */
  6338. /* Tag to abort goes in CDB[4]-CDB[11] */
  6339. memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
  6340. c->Request.CDB[12] = 0x00; /* reserved */
  6341. c->Request.CDB[13] = 0x00; /* reserved */
  6342. c->Request.CDB[14] = 0x00; /* reserved */
  6343. c->Request.CDB[15] = 0x00; /* reserved */
  6344. break;
  6345. default:
  6346. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  6347. cmd);
  6348. BUG();
  6349. }
  6350. } else {
  6351. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  6352. BUG();
  6353. }
  6354. switch (GET_DIR(c->Request.type_attr_dir)) {
  6355. case XFER_READ:
  6356. pci_dir = PCI_DMA_FROMDEVICE;
  6357. break;
  6358. case XFER_WRITE:
  6359. pci_dir = PCI_DMA_TODEVICE;
  6360. break;
  6361. case XFER_NONE:
  6362. pci_dir = PCI_DMA_NONE;
  6363. break;
  6364. default:
  6365. pci_dir = PCI_DMA_BIDIRECTIONAL;
  6366. }
  6367. if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
  6368. return -1;
  6369. return 0;
  6370. }
  6371. /*
  6372. * Map (physical) PCI mem into (virtual) kernel space
  6373. */
  6374. static void __iomem *remap_pci_mem(ulong base, ulong size)
  6375. {
  6376. ulong page_base = ((ulong) base) & PAGE_MASK;
  6377. ulong page_offs = ((ulong) base) - page_base;
  6378. void __iomem *page_remapped = ioremap_nocache(page_base,
  6379. page_offs + size);
  6380. return page_remapped ? (page_remapped + page_offs) : NULL;
  6381. }
  6382. static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
  6383. {
  6384. return h->access.command_completed(h, q);
  6385. }
  6386. static inline bool interrupt_pending(struct ctlr_info *h)
  6387. {
  6388. return h->access.intr_pending(h);
  6389. }
  6390. static inline long interrupt_not_for_us(struct ctlr_info *h)
  6391. {
  6392. return (h->access.intr_pending(h) == 0) ||
  6393. (h->interrupts_enabled == 0);
  6394. }
  6395. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  6396. u32 raw_tag)
  6397. {
  6398. if (unlikely(tag_index >= h->nr_cmds)) {
  6399. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  6400. return 1;
  6401. }
  6402. return 0;
  6403. }
  6404. static inline void finish_cmd(struct CommandList *c)
  6405. {
  6406. dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
  6407. if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
  6408. || c->cmd_type == CMD_IOACCEL2))
  6409. complete_scsi_command(c);
  6410. else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
  6411. complete(c->waiting);
  6412. }
  6413. /* process completion of an indexed ("direct lookup") command */
  6414. static inline void process_indexed_cmd(struct ctlr_info *h,
  6415. u32 raw_tag)
  6416. {
  6417. u32 tag_index;
  6418. struct CommandList *c;
  6419. tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
  6420. if (!bad_tag(h, tag_index, raw_tag)) {
  6421. c = h->cmd_pool + tag_index;
  6422. finish_cmd(c);
  6423. }
  6424. }
  6425. /* Some controllers, like p400, will give us one interrupt
  6426. * after a soft reset, even if we turned interrupts off.
  6427. * Only need to check for this in the hpsa_xxx_discard_completions
  6428. * functions.
  6429. */
  6430. static int ignore_bogus_interrupt(struct ctlr_info *h)
  6431. {
  6432. if (likely(!reset_devices))
  6433. return 0;
  6434. if (likely(h->interrupts_enabled))
  6435. return 0;
  6436. dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
  6437. "(known firmware bug.) Ignoring.\n");
  6438. return 1;
  6439. }
  6440. /*
  6441. * Convert &h->q[x] (passed to interrupt handlers) back to h.
  6442. * Relies on (h-q[x] == x) being true for x such that
  6443. * 0 <= x < MAX_REPLY_QUEUES.
  6444. */
  6445. static struct ctlr_info *queue_to_hba(u8 *queue)
  6446. {
  6447. return container_of((queue - *queue), struct ctlr_info, q[0]);
  6448. }
  6449. static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
  6450. {
  6451. struct ctlr_info *h = queue_to_hba(queue);
  6452. u8 q = *(u8 *) queue;
  6453. u32 raw_tag;
  6454. if (ignore_bogus_interrupt(h))
  6455. return IRQ_NONE;
  6456. if (interrupt_not_for_us(h))
  6457. return IRQ_NONE;
  6458. h->last_intr_timestamp = get_jiffies_64();
  6459. while (interrupt_pending(h)) {
  6460. raw_tag = get_next_completion(h, q);
  6461. while (raw_tag != FIFO_EMPTY)
  6462. raw_tag = next_command(h, q);
  6463. }
  6464. return IRQ_HANDLED;
  6465. }
  6466. static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
  6467. {
  6468. struct ctlr_info *h = queue_to_hba(queue);
  6469. u32 raw_tag;
  6470. u8 q = *(u8 *) queue;
  6471. if (ignore_bogus_interrupt(h))
  6472. return IRQ_NONE;
  6473. h->last_intr_timestamp = get_jiffies_64();
  6474. raw_tag = get_next_completion(h, q);
  6475. while (raw_tag != FIFO_EMPTY)
  6476. raw_tag = next_command(h, q);
  6477. return IRQ_HANDLED;
  6478. }
  6479. static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
  6480. {
  6481. struct ctlr_info *h = queue_to_hba((u8 *) queue);
  6482. u32 raw_tag;
  6483. u8 q = *(u8 *) queue;
  6484. if (interrupt_not_for_us(h))
  6485. return IRQ_NONE;
  6486. h->last_intr_timestamp = get_jiffies_64();
  6487. while (interrupt_pending(h)) {
  6488. raw_tag = get_next_completion(h, q);
  6489. while (raw_tag != FIFO_EMPTY) {
  6490. process_indexed_cmd(h, raw_tag);
  6491. raw_tag = next_command(h, q);
  6492. }
  6493. }
  6494. return IRQ_HANDLED;
  6495. }
  6496. static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
  6497. {
  6498. struct ctlr_info *h = queue_to_hba(queue);
  6499. u32 raw_tag;
  6500. u8 q = *(u8 *) queue;
  6501. h->last_intr_timestamp = get_jiffies_64();
  6502. raw_tag = get_next_completion(h, q);
  6503. while (raw_tag != FIFO_EMPTY) {
  6504. process_indexed_cmd(h, raw_tag);
  6505. raw_tag = next_command(h, q);
  6506. }
  6507. return IRQ_HANDLED;
  6508. }
  6509. /* Send a message CDB to the firmware. Careful, this only works
  6510. * in simple mode, not performant mode due to the tag lookup.
  6511. * We only ever use this immediately after a controller reset.
  6512. */
  6513. static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  6514. unsigned char type)
  6515. {
  6516. struct Command {
  6517. struct CommandListHeader CommandHeader;
  6518. struct RequestBlock Request;
  6519. struct ErrDescriptor ErrorDescriptor;
  6520. };
  6521. struct Command *cmd;
  6522. static const size_t cmd_sz = sizeof(*cmd) +
  6523. sizeof(cmd->ErrorDescriptor);
  6524. dma_addr_t paddr64;
  6525. __le32 paddr32;
  6526. u32 tag;
  6527. void __iomem *vaddr;
  6528. int i, err;
  6529. vaddr = pci_ioremap_bar(pdev, 0);
  6530. if (vaddr == NULL)
  6531. return -ENOMEM;
  6532. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  6533. * CCISS commands, so they must be allocated from the lower 4GiB of
  6534. * memory.
  6535. */
  6536. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  6537. if (err) {
  6538. iounmap(vaddr);
  6539. return err;
  6540. }
  6541. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  6542. if (cmd == NULL) {
  6543. iounmap(vaddr);
  6544. return -ENOMEM;
  6545. }
  6546. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  6547. * although there's no guarantee, we assume that the address is at
  6548. * least 4-byte aligned (most likely, it's page-aligned).
  6549. */
  6550. paddr32 = cpu_to_le32(paddr64);
  6551. cmd->CommandHeader.ReplyQueue = 0;
  6552. cmd->CommandHeader.SGList = 0;
  6553. cmd->CommandHeader.SGTotal = cpu_to_le16(0);
  6554. cmd->CommandHeader.tag = cpu_to_le64(paddr64);
  6555. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  6556. cmd->Request.CDBLen = 16;
  6557. cmd->Request.type_attr_dir =
  6558. TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
  6559. cmd->Request.Timeout = 0; /* Don't time out */
  6560. cmd->Request.CDB[0] = opcode;
  6561. cmd->Request.CDB[1] = type;
  6562. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  6563. cmd->ErrorDescriptor.Addr =
  6564. cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
  6565. cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
  6566. writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
  6567. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  6568. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  6569. if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
  6570. break;
  6571. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  6572. }
  6573. iounmap(vaddr);
  6574. /* we leak the DMA buffer here ... no choice since the controller could
  6575. * still complete the command.
  6576. */
  6577. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  6578. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  6579. opcode, type);
  6580. return -ETIMEDOUT;
  6581. }
  6582. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  6583. if (tag & HPSA_ERROR_BIT) {
  6584. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  6585. opcode, type);
  6586. return -EIO;
  6587. }
  6588. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  6589. opcode, type);
  6590. return 0;
  6591. }
  6592. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  6593. static int hpsa_controller_hard_reset(struct pci_dev *pdev,
  6594. void __iomem *vaddr, u32 use_doorbell)
  6595. {
  6596. if (use_doorbell) {
  6597. /* For everything after the P600, the PCI power state method
  6598. * of resetting the controller doesn't work, so we have this
  6599. * other way using the doorbell register.
  6600. */
  6601. dev_info(&pdev->dev, "using doorbell to reset controller\n");
  6602. writel(use_doorbell, vaddr + SA5_DOORBELL);
  6603. /* PMC hardware guys tell us we need a 10 second delay after
  6604. * doorbell reset and before any attempt to talk to the board
  6605. * at all to ensure that this actually works and doesn't fall
  6606. * over in some weird corner cases.
  6607. */
  6608. msleep(10000);
  6609. } else { /* Try to do it the PCI power state way */
  6610. /* Quoting from the Open CISS Specification: "The Power
  6611. * Management Control/Status Register (CSR) controls the power
  6612. * state of the device. The normal operating state is D0,
  6613. * CSR=00h. The software off state is D3, CSR=03h. To reset
  6614. * the controller, place the interface device in D3 then to D0,
  6615. * this causes a secondary PCI reset which will reset the
  6616. * controller." */
  6617. int rc = 0;
  6618. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  6619. /* enter the D3hot power management state */
  6620. rc = pci_set_power_state(pdev, PCI_D3hot);
  6621. if (rc)
  6622. return rc;
  6623. msleep(500);
  6624. /* enter the D0 power management state */
  6625. rc = pci_set_power_state(pdev, PCI_D0);
  6626. if (rc)
  6627. return rc;
  6628. /*
  6629. * The P600 requires a small delay when changing states.
  6630. * Otherwise we may think the board did not reset and we bail.
  6631. * This for kdump only and is particular to the P600.
  6632. */
  6633. msleep(500);
  6634. }
  6635. return 0;
  6636. }
  6637. static void init_driver_version(char *driver_version, int len)
  6638. {
  6639. memset(driver_version, 0, len);
  6640. strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
  6641. }
  6642. static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
  6643. {
  6644. char *driver_version;
  6645. int i, size = sizeof(cfgtable->driver_version);
  6646. driver_version = kmalloc(size, GFP_KERNEL);
  6647. if (!driver_version)
  6648. return -ENOMEM;
  6649. init_driver_version(driver_version, size);
  6650. for (i = 0; i < size; i++)
  6651. writeb(driver_version[i], &cfgtable->driver_version[i]);
  6652. kfree(driver_version);
  6653. return 0;
  6654. }
  6655. static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
  6656. unsigned char *driver_ver)
  6657. {
  6658. int i;
  6659. for (i = 0; i < sizeof(cfgtable->driver_version); i++)
  6660. driver_ver[i] = readb(&cfgtable->driver_version[i]);
  6661. }
  6662. static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
  6663. {
  6664. char *driver_ver, *old_driver_ver;
  6665. int rc, size = sizeof(cfgtable->driver_version);
  6666. old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
  6667. if (!old_driver_ver)
  6668. return -ENOMEM;
  6669. driver_ver = old_driver_ver + size;
  6670. /* After a reset, the 32 bytes of "driver version" in the cfgtable
  6671. * should have been changed, otherwise we know the reset failed.
  6672. */
  6673. init_driver_version(old_driver_ver, size);
  6674. read_driver_ver_from_cfgtable(cfgtable, driver_ver);
  6675. rc = !memcmp(driver_ver, old_driver_ver, size);
  6676. kfree(old_driver_ver);
  6677. return rc;
  6678. }
  6679. /* This does a hard reset of the controller using PCI power management
  6680. * states or the using the doorbell register.
  6681. */
  6682. static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
  6683. {
  6684. u64 cfg_offset;
  6685. u32 cfg_base_addr;
  6686. u64 cfg_base_addr_index;
  6687. void __iomem *vaddr;
  6688. unsigned long paddr;
  6689. u32 misc_fw_support;
  6690. int rc;
  6691. struct CfgTable __iomem *cfgtable;
  6692. u32 use_doorbell;
  6693. u16 command_register;
  6694. /* For controllers as old as the P600, this is very nearly
  6695. * the same thing as
  6696. *
  6697. * pci_save_state(pci_dev);
  6698. * pci_set_power_state(pci_dev, PCI_D3hot);
  6699. * pci_set_power_state(pci_dev, PCI_D0);
  6700. * pci_restore_state(pci_dev);
  6701. *
  6702. * For controllers newer than the P600, the pci power state
  6703. * method of resetting doesn't work so we have another way
  6704. * using the doorbell register.
  6705. */
  6706. if (!ctlr_is_resettable(board_id)) {
  6707. dev_warn(&pdev->dev, "Controller not resettable\n");
  6708. return -ENODEV;
  6709. }
  6710. /* if controller is soft- but not hard resettable... */
  6711. if (!ctlr_is_hard_resettable(board_id))
  6712. return -ENOTSUPP; /* try soft reset later. */
  6713. /* Save the PCI command register */
  6714. pci_read_config_word(pdev, 4, &command_register);
  6715. pci_save_state(pdev);
  6716. /* find the first memory BAR, so we can find the cfg table */
  6717. rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
  6718. if (rc)
  6719. return rc;
  6720. vaddr = remap_pci_mem(paddr, 0x250);
  6721. if (!vaddr)
  6722. return -ENOMEM;
  6723. /* find cfgtable in order to check if reset via doorbell is supported */
  6724. rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
  6725. &cfg_base_addr_index, &cfg_offset);
  6726. if (rc)
  6727. goto unmap_vaddr;
  6728. cfgtable = remap_pci_mem(pci_resource_start(pdev,
  6729. cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
  6730. if (!cfgtable) {
  6731. rc = -ENOMEM;
  6732. goto unmap_vaddr;
  6733. }
  6734. rc = write_driver_ver_to_cfgtable(cfgtable);
  6735. if (rc)
  6736. goto unmap_cfgtable;
  6737. /* If reset via doorbell register is supported, use that.
  6738. * There are two such methods. Favor the newest method.
  6739. */
  6740. misc_fw_support = readl(&cfgtable->misc_fw_support);
  6741. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
  6742. if (use_doorbell) {
  6743. use_doorbell = DOORBELL_CTLR_RESET2;
  6744. } else {
  6745. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
  6746. if (use_doorbell) {
  6747. dev_warn(&pdev->dev,
  6748. "Soft reset not supported. Firmware update is required.\n");
  6749. rc = -ENOTSUPP; /* try soft reset */
  6750. goto unmap_cfgtable;
  6751. }
  6752. }
  6753. rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
  6754. if (rc)
  6755. goto unmap_cfgtable;
  6756. pci_restore_state(pdev);
  6757. pci_write_config_word(pdev, 4, command_register);
  6758. /* Some devices (notably the HP Smart Array 5i Controller)
  6759. need a little pause here */
  6760. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  6761. rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
  6762. if (rc) {
  6763. dev_warn(&pdev->dev,
  6764. "Failed waiting for board to become ready after hard reset\n");
  6765. goto unmap_cfgtable;
  6766. }
  6767. rc = controller_reset_failed(vaddr);
  6768. if (rc < 0)
  6769. goto unmap_cfgtable;
  6770. if (rc) {
  6771. dev_warn(&pdev->dev, "Unable to successfully reset "
  6772. "controller. Will try soft reset.\n");
  6773. rc = -ENOTSUPP;
  6774. } else {
  6775. dev_info(&pdev->dev, "board ready after hard reset.\n");
  6776. }
  6777. unmap_cfgtable:
  6778. iounmap(cfgtable);
  6779. unmap_vaddr:
  6780. iounmap(vaddr);
  6781. return rc;
  6782. }
  6783. /*
  6784. * We cannot read the structure directly, for portability we must use
  6785. * the io functions.
  6786. * This is for debug only.
  6787. */
  6788. static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
  6789. {
  6790. #ifdef HPSA_DEBUG
  6791. int i;
  6792. char temp_name[17];
  6793. dev_info(dev, "Controller Configuration information\n");
  6794. dev_info(dev, "------------------------------------\n");
  6795. for (i = 0; i < 4; i++)
  6796. temp_name[i] = readb(&(tb->Signature[i]));
  6797. temp_name[4] = '\0';
  6798. dev_info(dev, " Signature = %s\n", temp_name);
  6799. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  6800. dev_info(dev, " Transport methods supported = 0x%x\n",
  6801. readl(&(tb->TransportSupport)));
  6802. dev_info(dev, " Transport methods active = 0x%x\n",
  6803. readl(&(tb->TransportActive)));
  6804. dev_info(dev, " Requested transport Method = 0x%x\n",
  6805. readl(&(tb->HostWrite.TransportRequest)));
  6806. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  6807. readl(&(tb->HostWrite.CoalIntDelay)));
  6808. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  6809. readl(&(tb->HostWrite.CoalIntCount)));
  6810. dev_info(dev, " Max outstanding commands = %d\n",
  6811. readl(&(tb->CmdsOutMax)));
  6812. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  6813. for (i = 0; i < 16; i++)
  6814. temp_name[i] = readb(&(tb->ServerName[i]));
  6815. temp_name[16] = '\0';
  6816. dev_info(dev, " Server Name = %s\n", temp_name);
  6817. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  6818. readl(&(tb->HeartBeat)));
  6819. #endif /* HPSA_DEBUG */
  6820. }
  6821. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  6822. {
  6823. int i, offset, mem_type, bar_type;
  6824. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  6825. return 0;
  6826. offset = 0;
  6827. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  6828. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  6829. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  6830. offset += 4;
  6831. else {
  6832. mem_type = pci_resource_flags(pdev, i) &
  6833. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  6834. switch (mem_type) {
  6835. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  6836. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  6837. offset += 4; /* 32 bit */
  6838. break;
  6839. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  6840. offset += 8;
  6841. break;
  6842. default: /* reserved in PCI 2.2 */
  6843. dev_warn(&pdev->dev,
  6844. "base address is invalid\n");
  6845. return -1;
  6846. break;
  6847. }
  6848. }
  6849. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  6850. return i + 1;
  6851. }
  6852. return -1;
  6853. }
  6854. static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
  6855. {
  6856. if (h->msix_vector) {
  6857. if (h->pdev->msix_enabled)
  6858. pci_disable_msix(h->pdev);
  6859. h->msix_vector = 0;
  6860. } else if (h->msi_vector) {
  6861. if (h->pdev->msi_enabled)
  6862. pci_disable_msi(h->pdev);
  6863. h->msi_vector = 0;
  6864. }
  6865. }
  6866. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  6867. * controllers that are capable. If not, we use legacy INTx mode.
  6868. */
  6869. static void hpsa_interrupt_mode(struct ctlr_info *h)
  6870. {
  6871. #ifdef CONFIG_PCI_MSI
  6872. int err, i;
  6873. struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
  6874. for (i = 0; i < MAX_REPLY_QUEUES; i++) {
  6875. hpsa_msix_entries[i].vector = 0;
  6876. hpsa_msix_entries[i].entry = i;
  6877. }
  6878. /* Some boards advertise MSI but don't really support it */
  6879. if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
  6880. (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
  6881. goto default_int_mode;
  6882. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
  6883. dev_info(&h->pdev->dev, "MSI-X capable controller\n");
  6884. h->msix_vector = MAX_REPLY_QUEUES;
  6885. if (h->msix_vector > num_online_cpus())
  6886. h->msix_vector = num_online_cpus();
  6887. err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
  6888. 1, h->msix_vector);
  6889. if (err < 0) {
  6890. dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
  6891. h->msix_vector = 0;
  6892. goto single_msi_mode;
  6893. } else if (err < h->msix_vector) {
  6894. dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
  6895. "available\n", err);
  6896. }
  6897. h->msix_vector = err;
  6898. for (i = 0; i < h->msix_vector; i++)
  6899. h->intr[i] = hpsa_msix_entries[i].vector;
  6900. return;
  6901. }
  6902. single_msi_mode:
  6903. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
  6904. dev_info(&h->pdev->dev, "MSI capable controller\n");
  6905. if (!pci_enable_msi(h->pdev))
  6906. h->msi_vector = 1;
  6907. else
  6908. dev_warn(&h->pdev->dev, "MSI init failed\n");
  6909. }
  6910. default_int_mode:
  6911. #endif /* CONFIG_PCI_MSI */
  6912. /* if we get here we're going to use the default interrupt mode */
  6913. h->intr[h->intr_mode] = h->pdev->irq;
  6914. }
  6915. static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
  6916. {
  6917. int i;
  6918. u32 subsystem_vendor_id, subsystem_device_id;
  6919. subsystem_vendor_id = pdev->subsystem_vendor;
  6920. subsystem_device_id = pdev->subsystem_device;
  6921. *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
  6922. subsystem_vendor_id;
  6923. for (i = 0; i < ARRAY_SIZE(products); i++)
  6924. if (*board_id == products[i].board_id)
  6925. return i;
  6926. if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
  6927. subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
  6928. !hpsa_allow_any) {
  6929. dev_warn(&pdev->dev, "unrecognized board ID: "
  6930. "0x%08x, ignoring.\n", *board_id);
  6931. return -ENODEV;
  6932. }
  6933. return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
  6934. }
  6935. static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  6936. unsigned long *memory_bar)
  6937. {
  6938. int i;
  6939. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  6940. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  6941. /* addressing mode bits already removed */
  6942. *memory_bar = pci_resource_start(pdev, i);
  6943. dev_dbg(&pdev->dev, "memory BAR = %lx\n",
  6944. *memory_bar);
  6945. return 0;
  6946. }
  6947. dev_warn(&pdev->dev, "no memory BAR found\n");
  6948. return -ENODEV;
  6949. }
  6950. static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
  6951. int wait_for_ready)
  6952. {
  6953. int i, iterations;
  6954. u32 scratchpad;
  6955. if (wait_for_ready)
  6956. iterations = HPSA_BOARD_READY_ITERATIONS;
  6957. else
  6958. iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
  6959. for (i = 0; i < iterations; i++) {
  6960. scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
  6961. if (wait_for_ready) {
  6962. if (scratchpad == HPSA_FIRMWARE_READY)
  6963. return 0;
  6964. } else {
  6965. if (scratchpad != HPSA_FIRMWARE_READY)
  6966. return 0;
  6967. }
  6968. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  6969. }
  6970. dev_warn(&pdev->dev, "board not ready, timed out.\n");
  6971. return -ENODEV;
  6972. }
  6973. static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
  6974. u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  6975. u64 *cfg_offset)
  6976. {
  6977. *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
  6978. *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
  6979. *cfg_base_addr &= (u32) 0x0000ffff;
  6980. *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
  6981. if (*cfg_base_addr_index == -1) {
  6982. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  6983. return -ENODEV;
  6984. }
  6985. return 0;
  6986. }
  6987. static void hpsa_free_cfgtables(struct ctlr_info *h)
  6988. {
  6989. if (h->transtable) {
  6990. iounmap(h->transtable);
  6991. h->transtable = NULL;
  6992. }
  6993. if (h->cfgtable) {
  6994. iounmap(h->cfgtable);
  6995. h->cfgtable = NULL;
  6996. }
  6997. }
  6998. /* Find and map CISS config table and transfer table
  6999. + * several items must be unmapped (freed) later
  7000. + * */
  7001. static int hpsa_find_cfgtables(struct ctlr_info *h)
  7002. {
  7003. u64 cfg_offset;
  7004. u32 cfg_base_addr;
  7005. u64 cfg_base_addr_index;
  7006. u32 trans_offset;
  7007. int rc;
  7008. rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  7009. &cfg_base_addr_index, &cfg_offset);
  7010. if (rc)
  7011. return rc;
  7012. h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
  7013. cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
  7014. if (!h->cfgtable) {
  7015. dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
  7016. return -ENOMEM;
  7017. }
  7018. rc = write_driver_ver_to_cfgtable(h->cfgtable);
  7019. if (rc)
  7020. return rc;
  7021. /* Find performant mode table. */
  7022. trans_offset = readl(&h->cfgtable->TransMethodOffset);
  7023. h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
  7024. cfg_base_addr_index)+cfg_offset+trans_offset,
  7025. sizeof(*h->transtable));
  7026. if (!h->transtable) {
  7027. dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
  7028. hpsa_free_cfgtables(h);
  7029. return -ENOMEM;
  7030. }
  7031. return 0;
  7032. }
  7033. static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
  7034. {
  7035. #define MIN_MAX_COMMANDS 16
  7036. BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
  7037. h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
  7038. /* Limit commands in memory limited kdump scenario. */
  7039. if (reset_devices && h->max_commands > 32)
  7040. h->max_commands = 32;
  7041. if (h->max_commands < MIN_MAX_COMMANDS) {
  7042. dev_warn(&h->pdev->dev,
  7043. "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
  7044. h->max_commands,
  7045. MIN_MAX_COMMANDS);
  7046. h->max_commands = MIN_MAX_COMMANDS;
  7047. }
  7048. }
  7049. /* If the controller reports that the total max sg entries is greater than 512,
  7050. * then we know that chained SG blocks work. (Original smart arrays did not
  7051. * support chained SG blocks and would return zero for max sg entries.)
  7052. */
  7053. static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
  7054. {
  7055. return h->maxsgentries > 512;
  7056. }
  7057. /* Interrogate the hardware for some limits:
  7058. * max commands, max SG elements without chaining, and with chaining,
  7059. * SG chain block size, etc.
  7060. */
  7061. static void hpsa_find_board_params(struct ctlr_info *h)
  7062. {
  7063. hpsa_get_max_perf_mode_cmds(h);
  7064. h->nr_cmds = h->max_commands;
  7065. h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
  7066. h->fw_support = readl(&(h->cfgtable->misc_fw_support));
  7067. if (hpsa_supports_chained_sg_blocks(h)) {
  7068. /* Limit in-command s/g elements to 32 save dma'able memory. */
  7069. h->max_cmd_sg_entries = 32;
  7070. h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
  7071. h->maxsgentries--; /* save one for chain pointer */
  7072. } else {
  7073. /*
  7074. * Original smart arrays supported at most 31 s/g entries
  7075. * embedded inline in the command (trying to use more
  7076. * would lock up the controller)
  7077. */
  7078. h->max_cmd_sg_entries = 31;
  7079. h->maxsgentries = 31; /* default to traditional values */
  7080. h->chainsize = 0;
  7081. }
  7082. /* Find out what task management functions are supported and cache */
  7083. h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
  7084. if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
  7085. dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
  7086. if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
  7087. dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
  7088. if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
  7089. dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
  7090. }
  7091. static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
  7092. {
  7093. if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
  7094. dev_err(&h->pdev->dev, "not a valid CISS config table\n");
  7095. return false;
  7096. }
  7097. return true;
  7098. }
  7099. static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
  7100. {
  7101. u32 driver_support;
  7102. driver_support = readl(&(h->cfgtable->driver_support));
  7103. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  7104. #ifdef CONFIG_X86
  7105. driver_support |= ENABLE_SCSI_PREFETCH;
  7106. #endif
  7107. driver_support |= ENABLE_UNIT_ATTN;
  7108. writel(driver_support, &(h->cfgtable->driver_support));
  7109. }
  7110. /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
  7111. * in a prefetch beyond physical memory.
  7112. */
  7113. static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
  7114. {
  7115. u32 dma_prefetch;
  7116. if (h->board_id != 0x3225103C)
  7117. return;
  7118. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  7119. dma_prefetch |= 0x8000;
  7120. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  7121. }
  7122. static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
  7123. {
  7124. int i;
  7125. u32 doorbell_value;
  7126. unsigned long flags;
  7127. /* wait until the clear_event_notify bit 6 is cleared by controller. */
  7128. for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
  7129. spin_lock_irqsave(&h->lock, flags);
  7130. doorbell_value = readl(h->vaddr + SA5_DOORBELL);
  7131. spin_unlock_irqrestore(&h->lock, flags);
  7132. if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
  7133. goto done;
  7134. /* delay and try again */
  7135. msleep(CLEAR_EVENT_WAIT_INTERVAL);
  7136. }
  7137. return -ENODEV;
  7138. done:
  7139. return 0;
  7140. }
  7141. static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
  7142. {
  7143. int i;
  7144. u32 doorbell_value;
  7145. unsigned long flags;
  7146. /* under certain very rare conditions, this can take awhile.
  7147. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  7148. * as we enter this code.)
  7149. */
  7150. for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
  7151. if (h->remove_in_progress)
  7152. goto done;
  7153. spin_lock_irqsave(&h->lock, flags);
  7154. doorbell_value = readl(h->vaddr + SA5_DOORBELL);
  7155. spin_unlock_irqrestore(&h->lock, flags);
  7156. if (!(doorbell_value & CFGTBL_ChangeReq))
  7157. goto done;
  7158. /* delay and try again */
  7159. msleep(MODE_CHANGE_WAIT_INTERVAL);
  7160. }
  7161. return -ENODEV;
  7162. done:
  7163. return 0;
  7164. }
  7165. /* return -ENODEV or other reason on error, 0 on success */
  7166. static int hpsa_enter_simple_mode(struct ctlr_info *h)
  7167. {
  7168. u32 trans_support;
  7169. trans_support = readl(&(h->cfgtable->TransportSupport));
  7170. if (!(trans_support & SIMPLE_MODE))
  7171. return -ENOTSUPP;
  7172. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  7173. /* Update the field, and then ring the doorbell */
  7174. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  7175. writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
  7176. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  7177. if (hpsa_wait_for_mode_change_ack(h))
  7178. goto error;
  7179. print_cfg_table(&h->pdev->dev, h->cfgtable);
  7180. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
  7181. goto error;
  7182. h->transMethod = CFGTBL_Trans_Simple;
  7183. return 0;
  7184. error:
  7185. dev_err(&h->pdev->dev, "failed to enter simple mode\n");
  7186. return -ENODEV;
  7187. }
  7188. /* free items allocated or mapped by hpsa_pci_init */
  7189. static void hpsa_free_pci_init(struct ctlr_info *h)
  7190. {
  7191. hpsa_free_cfgtables(h); /* pci_init 4 */
  7192. iounmap(h->vaddr); /* pci_init 3 */
  7193. h->vaddr = NULL;
  7194. hpsa_disable_interrupt_mode(h); /* pci_init 2 */
  7195. /*
  7196. * call pci_disable_device before pci_release_regions per
  7197. * Documentation/PCI/pci.txt
  7198. */
  7199. pci_disable_device(h->pdev); /* pci_init 1 */
  7200. pci_release_regions(h->pdev); /* pci_init 2 */
  7201. }
  7202. /* several items must be freed later */
  7203. static int hpsa_pci_init(struct ctlr_info *h)
  7204. {
  7205. int prod_index, err;
  7206. prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
  7207. if (prod_index < 0)
  7208. return prod_index;
  7209. h->product_name = products[prod_index].product_name;
  7210. h->access = *(products[prod_index].access);
  7211. h->needs_abort_tags_swizzled =
  7212. ctlr_needs_abort_tags_swizzled(h->board_id);
  7213. pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
  7214. PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
  7215. err = pci_enable_device(h->pdev);
  7216. if (err) {
  7217. dev_err(&h->pdev->dev, "failed to enable PCI device\n");
  7218. pci_disable_device(h->pdev);
  7219. return err;
  7220. }
  7221. err = pci_request_regions(h->pdev, HPSA);
  7222. if (err) {
  7223. dev_err(&h->pdev->dev,
  7224. "failed to obtain PCI resources\n");
  7225. pci_disable_device(h->pdev);
  7226. return err;
  7227. }
  7228. pci_set_master(h->pdev);
  7229. hpsa_interrupt_mode(h);
  7230. err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
  7231. if (err)
  7232. goto clean2; /* intmode+region, pci */
  7233. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  7234. if (!h->vaddr) {
  7235. dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
  7236. err = -ENOMEM;
  7237. goto clean2; /* intmode+region, pci */
  7238. }
  7239. err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
  7240. if (err)
  7241. goto clean3; /* vaddr, intmode+region, pci */
  7242. err = hpsa_find_cfgtables(h);
  7243. if (err)
  7244. goto clean3; /* vaddr, intmode+region, pci */
  7245. hpsa_find_board_params(h);
  7246. if (!hpsa_CISS_signature_present(h)) {
  7247. err = -ENODEV;
  7248. goto clean4; /* cfgtables, vaddr, intmode+region, pci */
  7249. }
  7250. hpsa_set_driver_support_bits(h);
  7251. hpsa_p600_dma_prefetch_quirk(h);
  7252. err = hpsa_enter_simple_mode(h);
  7253. if (err)
  7254. goto clean4; /* cfgtables, vaddr, intmode+region, pci */
  7255. return 0;
  7256. clean4: /* cfgtables, vaddr, intmode+region, pci */
  7257. hpsa_free_cfgtables(h);
  7258. clean3: /* vaddr, intmode+region, pci */
  7259. iounmap(h->vaddr);
  7260. h->vaddr = NULL;
  7261. clean2: /* intmode+region, pci */
  7262. hpsa_disable_interrupt_mode(h);
  7263. /*
  7264. * call pci_disable_device before pci_release_regions per
  7265. * Documentation/PCI/pci.txt
  7266. */
  7267. pci_disable_device(h->pdev);
  7268. pci_release_regions(h->pdev);
  7269. return err;
  7270. }
  7271. static void hpsa_hba_inquiry(struct ctlr_info *h)
  7272. {
  7273. int rc;
  7274. #define HBA_INQUIRY_BYTE_COUNT 64
  7275. h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
  7276. if (!h->hba_inquiry_data)
  7277. return;
  7278. rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
  7279. h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
  7280. if (rc != 0) {
  7281. kfree(h->hba_inquiry_data);
  7282. h->hba_inquiry_data = NULL;
  7283. }
  7284. }
  7285. static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
  7286. {
  7287. int rc, i;
  7288. void __iomem *vaddr;
  7289. if (!reset_devices)
  7290. return 0;
  7291. /* kdump kernel is loading, we don't know in which state is
  7292. * the pci interface. The dev->enable_cnt is equal zero
  7293. * so we call enable+disable, wait a while and switch it on.
  7294. */
  7295. rc = pci_enable_device(pdev);
  7296. if (rc) {
  7297. dev_warn(&pdev->dev, "Failed to enable PCI device\n");
  7298. return -ENODEV;
  7299. }
  7300. pci_disable_device(pdev);
  7301. msleep(260); /* a randomly chosen number */
  7302. rc = pci_enable_device(pdev);
  7303. if (rc) {
  7304. dev_warn(&pdev->dev, "failed to enable device.\n");
  7305. return -ENODEV;
  7306. }
  7307. pci_set_master(pdev);
  7308. vaddr = pci_ioremap_bar(pdev, 0);
  7309. if (vaddr == NULL) {
  7310. rc = -ENOMEM;
  7311. goto out_disable;
  7312. }
  7313. writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
  7314. iounmap(vaddr);
  7315. /* Reset the controller with a PCI power-cycle or via doorbell */
  7316. rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
  7317. /* -ENOTSUPP here means we cannot reset the controller
  7318. * but it's already (and still) up and running in
  7319. * "performant mode". Or, it might be 640x, which can't reset
  7320. * due to concerns about shared bbwc between 6402/6404 pair.
  7321. */
  7322. if (rc)
  7323. goto out_disable;
  7324. /* Now try to get the controller to respond to a no-op */
  7325. dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
  7326. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  7327. if (hpsa_noop(pdev) == 0)
  7328. break;
  7329. else
  7330. dev_warn(&pdev->dev, "no-op failed%s\n",
  7331. (i < 11 ? "; re-trying" : ""));
  7332. }
  7333. out_disable:
  7334. pci_disable_device(pdev);
  7335. return rc;
  7336. }
  7337. static void hpsa_free_cmd_pool(struct ctlr_info *h)
  7338. {
  7339. kfree(h->cmd_pool_bits);
  7340. h->cmd_pool_bits = NULL;
  7341. if (h->cmd_pool) {
  7342. pci_free_consistent(h->pdev,
  7343. h->nr_cmds * sizeof(struct CommandList),
  7344. h->cmd_pool,
  7345. h->cmd_pool_dhandle);
  7346. h->cmd_pool = NULL;
  7347. h->cmd_pool_dhandle = 0;
  7348. }
  7349. if (h->errinfo_pool) {
  7350. pci_free_consistent(h->pdev,
  7351. h->nr_cmds * sizeof(struct ErrorInfo),
  7352. h->errinfo_pool,
  7353. h->errinfo_pool_dhandle);
  7354. h->errinfo_pool = NULL;
  7355. h->errinfo_pool_dhandle = 0;
  7356. }
  7357. }
  7358. static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
  7359. {
  7360. h->cmd_pool_bits = kzalloc(
  7361. DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
  7362. sizeof(unsigned long), GFP_KERNEL);
  7363. h->cmd_pool = pci_alloc_consistent(h->pdev,
  7364. h->nr_cmds * sizeof(*h->cmd_pool),
  7365. &(h->cmd_pool_dhandle));
  7366. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  7367. h->nr_cmds * sizeof(*h->errinfo_pool),
  7368. &(h->errinfo_pool_dhandle));
  7369. if ((h->cmd_pool_bits == NULL)
  7370. || (h->cmd_pool == NULL)
  7371. || (h->errinfo_pool == NULL)) {
  7372. dev_err(&h->pdev->dev, "out of memory in %s", __func__);
  7373. goto clean_up;
  7374. }
  7375. hpsa_preinitialize_commands(h);
  7376. return 0;
  7377. clean_up:
  7378. hpsa_free_cmd_pool(h);
  7379. return -ENOMEM;
  7380. }
  7381. static void hpsa_irq_affinity_hints(struct ctlr_info *h)
  7382. {
  7383. int i, cpu;
  7384. cpu = cpumask_first(cpu_online_mask);
  7385. for (i = 0; i < h->msix_vector; i++) {
  7386. irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
  7387. cpu = cpumask_next(cpu, cpu_online_mask);
  7388. }
  7389. }
  7390. /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
  7391. static void hpsa_free_irqs(struct ctlr_info *h)
  7392. {
  7393. int i;
  7394. if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
  7395. /* Single reply queue, only one irq to free */
  7396. i = h->intr_mode;
  7397. irq_set_affinity_hint(h->intr[i], NULL);
  7398. free_irq(h->intr[i], &h->q[i]);
  7399. h->q[i] = 0;
  7400. return;
  7401. }
  7402. for (i = 0; i < h->msix_vector; i++) {
  7403. irq_set_affinity_hint(h->intr[i], NULL);
  7404. free_irq(h->intr[i], &h->q[i]);
  7405. h->q[i] = 0;
  7406. }
  7407. for (; i < MAX_REPLY_QUEUES; i++)
  7408. h->q[i] = 0;
  7409. }
  7410. /* returns 0 on success; cleans up and returns -Enn on error */
  7411. static int hpsa_request_irqs(struct ctlr_info *h,
  7412. irqreturn_t (*msixhandler)(int, void *),
  7413. irqreturn_t (*intxhandler)(int, void *))
  7414. {
  7415. int rc, i;
  7416. /*
  7417. * initialize h->q[x] = x so that interrupt handlers know which
  7418. * queue to process.
  7419. */
  7420. for (i = 0; i < MAX_REPLY_QUEUES; i++)
  7421. h->q[i] = (u8) i;
  7422. if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
  7423. /* If performant mode and MSI-X, use multiple reply queues */
  7424. for (i = 0; i < h->msix_vector; i++) {
  7425. sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
  7426. rc = request_irq(h->intr[i], msixhandler,
  7427. 0, h->intrname[i],
  7428. &h->q[i]);
  7429. if (rc) {
  7430. int j;
  7431. dev_err(&h->pdev->dev,
  7432. "failed to get irq %d for %s\n",
  7433. h->intr[i], h->devname);
  7434. for (j = 0; j < i; j++) {
  7435. free_irq(h->intr[j], &h->q[j]);
  7436. h->q[j] = 0;
  7437. }
  7438. for (; j < MAX_REPLY_QUEUES; j++)
  7439. h->q[j] = 0;
  7440. return rc;
  7441. }
  7442. }
  7443. hpsa_irq_affinity_hints(h);
  7444. } else {
  7445. /* Use single reply pool */
  7446. if (h->msix_vector > 0 || h->msi_vector) {
  7447. if (h->msix_vector)
  7448. sprintf(h->intrname[h->intr_mode],
  7449. "%s-msix", h->devname);
  7450. else
  7451. sprintf(h->intrname[h->intr_mode],
  7452. "%s-msi", h->devname);
  7453. rc = request_irq(h->intr[h->intr_mode],
  7454. msixhandler, 0,
  7455. h->intrname[h->intr_mode],
  7456. &h->q[h->intr_mode]);
  7457. } else {
  7458. sprintf(h->intrname[h->intr_mode],
  7459. "%s-intx", h->devname);
  7460. rc = request_irq(h->intr[h->intr_mode],
  7461. intxhandler, IRQF_SHARED,
  7462. h->intrname[h->intr_mode],
  7463. &h->q[h->intr_mode]);
  7464. }
  7465. irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
  7466. }
  7467. if (rc) {
  7468. dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
  7469. h->intr[h->intr_mode], h->devname);
  7470. hpsa_free_irqs(h);
  7471. return -ENODEV;
  7472. }
  7473. return 0;
  7474. }
  7475. static int hpsa_kdump_soft_reset(struct ctlr_info *h)
  7476. {
  7477. int rc;
  7478. hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
  7479. dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
  7480. rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
  7481. if (rc) {
  7482. dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
  7483. return rc;
  7484. }
  7485. dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
  7486. rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
  7487. if (rc) {
  7488. dev_warn(&h->pdev->dev, "Board failed to become ready "
  7489. "after soft reset.\n");
  7490. return rc;
  7491. }
  7492. return 0;
  7493. }
  7494. static void hpsa_free_reply_queues(struct ctlr_info *h)
  7495. {
  7496. int i;
  7497. for (i = 0; i < h->nreply_queues; i++) {
  7498. if (!h->reply_queue[i].head)
  7499. continue;
  7500. pci_free_consistent(h->pdev,
  7501. h->reply_queue_size,
  7502. h->reply_queue[i].head,
  7503. h->reply_queue[i].busaddr);
  7504. h->reply_queue[i].head = NULL;
  7505. h->reply_queue[i].busaddr = 0;
  7506. }
  7507. h->reply_queue_size = 0;
  7508. }
  7509. static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
  7510. {
  7511. hpsa_free_performant_mode(h); /* init_one 7 */
  7512. hpsa_free_sg_chain_blocks(h); /* init_one 6 */
  7513. hpsa_free_cmd_pool(h); /* init_one 5 */
  7514. hpsa_free_irqs(h); /* init_one 4 */
  7515. scsi_host_put(h->scsi_host); /* init_one 3 */
  7516. h->scsi_host = NULL; /* init_one 3 */
  7517. hpsa_free_pci_init(h); /* init_one 2_5 */
  7518. free_percpu(h->lockup_detected); /* init_one 2 */
  7519. h->lockup_detected = NULL; /* init_one 2 */
  7520. if (h->resubmit_wq) {
  7521. destroy_workqueue(h->resubmit_wq); /* init_one 1 */
  7522. h->resubmit_wq = NULL;
  7523. }
  7524. if (h->rescan_ctlr_wq) {
  7525. destroy_workqueue(h->rescan_ctlr_wq);
  7526. h->rescan_ctlr_wq = NULL;
  7527. }
  7528. kfree(h); /* init_one 1 */
  7529. }
  7530. /* Called when controller lockup detected. */
  7531. static void fail_all_outstanding_cmds(struct ctlr_info *h)
  7532. {
  7533. int i, refcount;
  7534. struct CommandList *c;
  7535. int failcount = 0;
  7536. flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
  7537. for (i = 0; i < h->nr_cmds; i++) {
  7538. c = h->cmd_pool + i;
  7539. refcount = atomic_inc_return(&c->refcount);
  7540. if (refcount > 1) {
  7541. c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
  7542. finish_cmd(c);
  7543. atomic_dec(&h->commands_outstanding);
  7544. failcount++;
  7545. }
  7546. cmd_free(h, c);
  7547. }
  7548. dev_warn(&h->pdev->dev,
  7549. "failed %d commands in fail_all\n", failcount);
  7550. }
  7551. static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
  7552. {
  7553. int cpu;
  7554. for_each_online_cpu(cpu) {
  7555. u32 *lockup_detected;
  7556. lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
  7557. *lockup_detected = value;
  7558. }
  7559. wmb(); /* be sure the per-cpu variables are out to memory */
  7560. }
  7561. static void controller_lockup_detected(struct ctlr_info *h)
  7562. {
  7563. unsigned long flags;
  7564. u32 lockup_detected;
  7565. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7566. spin_lock_irqsave(&h->lock, flags);
  7567. lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  7568. if (!lockup_detected) {
  7569. /* no heartbeat, but controller gave us a zero. */
  7570. dev_warn(&h->pdev->dev,
  7571. "lockup detected after %d but scratchpad register is zero\n",
  7572. h->heartbeat_sample_interval / HZ);
  7573. lockup_detected = 0xffffffff;
  7574. }
  7575. set_lockup_detected_for_all_cpus(h, lockup_detected);
  7576. spin_unlock_irqrestore(&h->lock, flags);
  7577. dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
  7578. lockup_detected, h->heartbeat_sample_interval / HZ);
  7579. pci_disable_device(h->pdev);
  7580. fail_all_outstanding_cmds(h);
  7581. }
  7582. static int detect_controller_lockup(struct ctlr_info *h)
  7583. {
  7584. u64 now;
  7585. u32 heartbeat;
  7586. unsigned long flags;
  7587. now = get_jiffies_64();
  7588. /* If we've received an interrupt recently, we're ok. */
  7589. if (time_after64(h->last_intr_timestamp +
  7590. (h->heartbeat_sample_interval), now))
  7591. return false;
  7592. /*
  7593. * If we've already checked the heartbeat recently, we're ok.
  7594. * This could happen if someone sends us a signal. We
  7595. * otherwise don't care about signals in this thread.
  7596. */
  7597. if (time_after64(h->last_heartbeat_timestamp +
  7598. (h->heartbeat_sample_interval), now))
  7599. return false;
  7600. /* If heartbeat has not changed since we last looked, we're not ok. */
  7601. spin_lock_irqsave(&h->lock, flags);
  7602. heartbeat = readl(&h->cfgtable->HeartBeat);
  7603. spin_unlock_irqrestore(&h->lock, flags);
  7604. if (h->last_heartbeat == heartbeat) {
  7605. controller_lockup_detected(h);
  7606. return true;
  7607. }
  7608. /* We're ok. */
  7609. h->last_heartbeat = heartbeat;
  7610. h->last_heartbeat_timestamp = now;
  7611. return false;
  7612. }
  7613. static void hpsa_ack_ctlr_events(struct ctlr_info *h)
  7614. {
  7615. int i;
  7616. char *event_type;
  7617. if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
  7618. return;
  7619. /* Ask the controller to clear the events we're handling. */
  7620. if ((h->transMethod & (CFGTBL_Trans_io_accel1
  7621. | CFGTBL_Trans_io_accel2)) &&
  7622. (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
  7623. h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
  7624. if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
  7625. event_type = "state change";
  7626. if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
  7627. event_type = "configuration change";
  7628. /* Stop sending new RAID offload reqs via the IO accelerator */
  7629. scsi_block_requests(h->scsi_host);
  7630. for (i = 0; i < h->ndevices; i++) {
  7631. h->dev[i]->offload_enabled = 0;
  7632. h->dev[i]->offload_to_be_enabled = 0;
  7633. }
  7634. hpsa_drain_accel_commands(h);
  7635. /* Set 'accelerator path config change' bit */
  7636. dev_warn(&h->pdev->dev,
  7637. "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
  7638. h->events, event_type);
  7639. writel(h->events, &(h->cfgtable->clear_event_notify));
  7640. /* Set the "clear event notify field update" bit 6 */
  7641. writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
  7642. /* Wait until ctlr clears 'clear event notify field', bit 6 */
  7643. hpsa_wait_for_clear_event_notify_ack(h);
  7644. scsi_unblock_requests(h->scsi_host);
  7645. } else {
  7646. /* Acknowledge controller notification events. */
  7647. writel(h->events, &(h->cfgtable->clear_event_notify));
  7648. writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
  7649. hpsa_wait_for_clear_event_notify_ack(h);
  7650. #if 0
  7651. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  7652. hpsa_wait_for_mode_change_ack(h);
  7653. #endif
  7654. }
  7655. return;
  7656. }
  7657. /* Check a register on the controller to see if there are configuration
  7658. * changes (added/changed/removed logical drives, etc.) which mean that
  7659. * we should rescan the controller for devices.
  7660. * Also check flag for driver-initiated rescan.
  7661. */
  7662. static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
  7663. {
  7664. if (h->drv_req_rescan) {
  7665. h->drv_req_rescan = 0;
  7666. return 1;
  7667. }
  7668. if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
  7669. return 0;
  7670. h->events = readl(&(h->cfgtable->event_notify));
  7671. return h->events & RESCAN_REQUIRED_EVENT_BITS;
  7672. }
  7673. /*
  7674. * Check if any of the offline devices have become ready
  7675. */
  7676. static int hpsa_offline_devices_ready(struct ctlr_info *h)
  7677. {
  7678. unsigned long flags;
  7679. struct offline_device_entry *d;
  7680. struct list_head *this, *tmp;
  7681. spin_lock_irqsave(&h->offline_device_lock, flags);
  7682. list_for_each_safe(this, tmp, &h->offline_device_list) {
  7683. d = list_entry(this, struct offline_device_entry,
  7684. offline_list);
  7685. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  7686. if (!hpsa_volume_offline(h, d->scsi3addr)) {
  7687. spin_lock_irqsave(&h->offline_device_lock, flags);
  7688. list_del(&d->offline_list);
  7689. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  7690. return 1;
  7691. }
  7692. spin_lock_irqsave(&h->offline_device_lock, flags);
  7693. }
  7694. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  7695. return 0;
  7696. }
  7697. static int hpsa_luns_changed(struct ctlr_info *h)
  7698. {
  7699. int rc = 1; /* assume there are changes */
  7700. struct ReportLUNdata *logdev = NULL;
  7701. /* if we can't find out if lun data has changed,
  7702. * assume that it has.
  7703. */
  7704. if (!h->lastlogicals)
  7705. goto out;
  7706. logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
  7707. if (!logdev) {
  7708. dev_warn(&h->pdev->dev,
  7709. "Out of memory, can't track lun changes.\n");
  7710. goto out;
  7711. }
  7712. if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
  7713. dev_warn(&h->pdev->dev,
  7714. "report luns failed, can't track lun changes.\n");
  7715. goto out;
  7716. }
  7717. if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
  7718. dev_info(&h->pdev->dev,
  7719. "Lun changes detected.\n");
  7720. memcpy(h->lastlogicals, logdev, sizeof(*logdev));
  7721. goto out;
  7722. } else
  7723. rc = 0; /* no changes detected. */
  7724. out:
  7725. kfree(logdev);
  7726. return rc;
  7727. }
  7728. static void hpsa_rescan_ctlr_worker(struct work_struct *work)
  7729. {
  7730. unsigned long flags;
  7731. struct ctlr_info *h = container_of(to_delayed_work(work),
  7732. struct ctlr_info, rescan_ctlr_work);
  7733. if (h->remove_in_progress)
  7734. return;
  7735. if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
  7736. scsi_host_get(h->scsi_host);
  7737. hpsa_ack_ctlr_events(h);
  7738. hpsa_scan_start(h->scsi_host);
  7739. scsi_host_put(h->scsi_host);
  7740. } else if (h->discovery_polling) {
  7741. hpsa_disable_rld_caching(h);
  7742. if (hpsa_luns_changed(h)) {
  7743. struct Scsi_Host *sh = NULL;
  7744. dev_info(&h->pdev->dev,
  7745. "driver discovery polling rescan.\n");
  7746. sh = scsi_host_get(h->scsi_host);
  7747. if (sh != NULL) {
  7748. hpsa_scan_start(sh);
  7749. scsi_host_put(sh);
  7750. }
  7751. }
  7752. }
  7753. spin_lock_irqsave(&h->lock, flags);
  7754. if (!h->remove_in_progress)
  7755. queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
  7756. h->heartbeat_sample_interval);
  7757. spin_unlock_irqrestore(&h->lock, flags);
  7758. }
  7759. static void hpsa_monitor_ctlr_worker(struct work_struct *work)
  7760. {
  7761. unsigned long flags;
  7762. struct ctlr_info *h = container_of(to_delayed_work(work),
  7763. struct ctlr_info, monitor_ctlr_work);
  7764. detect_controller_lockup(h);
  7765. if (lockup_detected(h))
  7766. return;
  7767. spin_lock_irqsave(&h->lock, flags);
  7768. if (!h->remove_in_progress)
  7769. schedule_delayed_work(&h->monitor_ctlr_work,
  7770. h->heartbeat_sample_interval);
  7771. spin_unlock_irqrestore(&h->lock, flags);
  7772. }
  7773. static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
  7774. char *name)
  7775. {
  7776. struct workqueue_struct *wq = NULL;
  7777. wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
  7778. if (!wq)
  7779. dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
  7780. return wq;
  7781. }
  7782. static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  7783. {
  7784. int dac, rc;
  7785. struct ctlr_info *h;
  7786. int try_soft_reset = 0;
  7787. unsigned long flags;
  7788. u32 board_id;
  7789. if (number_of_controllers == 0)
  7790. printk(KERN_INFO DRIVER_NAME "\n");
  7791. rc = hpsa_lookup_board_id(pdev, &board_id);
  7792. if (rc < 0) {
  7793. dev_warn(&pdev->dev, "Board ID not found\n");
  7794. return rc;
  7795. }
  7796. rc = hpsa_init_reset_devices(pdev, board_id);
  7797. if (rc) {
  7798. if (rc != -ENOTSUPP)
  7799. return rc;
  7800. /* If the reset fails in a particular way (it has no way to do
  7801. * a proper hard reset, so returns -ENOTSUPP) we can try to do
  7802. * a soft reset once we get the controller configured up to the
  7803. * point that it can accept a command.
  7804. */
  7805. try_soft_reset = 1;
  7806. rc = 0;
  7807. }
  7808. reinit_after_soft_reset:
  7809. /* Command structures must be aligned on a 32-byte boundary because
  7810. * the 5 lower bits of the address are used by the hardware. and by
  7811. * the driver. See comments in hpsa.h for more info.
  7812. */
  7813. BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
  7814. h = kzalloc(sizeof(*h), GFP_KERNEL);
  7815. if (!h) {
  7816. dev_err(&pdev->dev, "Failed to allocate controller head\n");
  7817. return -ENOMEM;
  7818. }
  7819. h->pdev = pdev;
  7820. h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
  7821. INIT_LIST_HEAD(&h->offline_device_list);
  7822. spin_lock_init(&h->lock);
  7823. spin_lock_init(&h->offline_device_lock);
  7824. spin_lock_init(&h->scan_lock);
  7825. atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
  7826. atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
  7827. /* Allocate and clear per-cpu variable lockup_detected */
  7828. h->lockup_detected = alloc_percpu(u32);
  7829. if (!h->lockup_detected) {
  7830. dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
  7831. rc = -ENOMEM;
  7832. goto clean1; /* aer/h */
  7833. }
  7834. set_lockup_detected_for_all_cpus(h, 0);
  7835. rc = hpsa_pci_init(h);
  7836. if (rc)
  7837. goto clean2; /* lu, aer/h */
  7838. /* relies on h-> settings made by hpsa_pci_init, including
  7839. * interrupt_mode h->intr */
  7840. rc = hpsa_scsi_host_alloc(h);
  7841. if (rc)
  7842. goto clean2_5; /* pci, lu, aer/h */
  7843. sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
  7844. h->ctlr = number_of_controllers;
  7845. number_of_controllers++;
  7846. /* configure PCI DMA stuff */
  7847. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  7848. if (rc == 0) {
  7849. dac = 1;
  7850. } else {
  7851. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  7852. if (rc == 0) {
  7853. dac = 0;
  7854. } else {
  7855. dev_err(&pdev->dev, "no suitable DMA available\n");
  7856. goto clean3; /* shost, pci, lu, aer/h */
  7857. }
  7858. }
  7859. /* make sure the board interrupts are off */
  7860. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7861. rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
  7862. if (rc)
  7863. goto clean3; /* shost, pci, lu, aer/h */
  7864. rc = hpsa_alloc_cmd_pool(h);
  7865. if (rc)
  7866. goto clean4; /* irq, shost, pci, lu, aer/h */
  7867. rc = hpsa_alloc_sg_chain_blocks(h);
  7868. if (rc)
  7869. goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
  7870. init_waitqueue_head(&h->scan_wait_queue);
  7871. init_waitqueue_head(&h->abort_cmd_wait_queue);
  7872. init_waitqueue_head(&h->event_sync_wait_queue);
  7873. mutex_init(&h->reset_mutex);
  7874. h->scan_finished = 1; /* no scan currently in progress */
  7875. h->scan_waiting = 0;
  7876. pci_set_drvdata(pdev, h);
  7877. h->ndevices = 0;
  7878. spin_lock_init(&h->devlock);
  7879. rc = hpsa_put_ctlr_into_performant_mode(h);
  7880. if (rc)
  7881. goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
  7882. /* create the resubmit workqueue */
  7883. h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
  7884. if (!h->rescan_ctlr_wq) {
  7885. rc = -ENOMEM;
  7886. goto clean7;
  7887. }
  7888. h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
  7889. if (!h->resubmit_wq) {
  7890. rc = -ENOMEM;
  7891. goto clean7; /* aer/h */
  7892. }
  7893. /*
  7894. * At this point, the controller is ready to take commands.
  7895. * Now, if reset_devices and the hard reset didn't work, try
  7896. * the soft reset and see if that works.
  7897. */
  7898. if (try_soft_reset) {
  7899. /* This is kind of gross. We may or may not get a completion
  7900. * from the soft reset command, and if we do, then the value
  7901. * from the fifo may or may not be valid. So, we wait 10 secs
  7902. * after the reset throwing away any completions we get during
  7903. * that time. Unregister the interrupt handler and register
  7904. * fake ones to scoop up any residual completions.
  7905. */
  7906. spin_lock_irqsave(&h->lock, flags);
  7907. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7908. spin_unlock_irqrestore(&h->lock, flags);
  7909. hpsa_free_irqs(h);
  7910. rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
  7911. hpsa_intx_discard_completions);
  7912. if (rc) {
  7913. dev_warn(&h->pdev->dev,
  7914. "Failed to request_irq after soft reset.\n");
  7915. /*
  7916. * cannot goto clean7 or free_irqs will be called
  7917. * again. Instead, do its work
  7918. */
  7919. hpsa_free_performant_mode(h); /* clean7 */
  7920. hpsa_free_sg_chain_blocks(h); /* clean6 */
  7921. hpsa_free_cmd_pool(h); /* clean5 */
  7922. /*
  7923. * skip hpsa_free_irqs(h) clean4 since that
  7924. * was just called before request_irqs failed
  7925. */
  7926. goto clean3;
  7927. }
  7928. rc = hpsa_kdump_soft_reset(h);
  7929. if (rc)
  7930. /* Neither hard nor soft reset worked, we're hosed. */
  7931. goto clean7;
  7932. dev_info(&h->pdev->dev, "Board READY.\n");
  7933. dev_info(&h->pdev->dev,
  7934. "Waiting for stale completions to drain.\n");
  7935. h->access.set_intr_mask(h, HPSA_INTR_ON);
  7936. msleep(10000);
  7937. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7938. rc = controller_reset_failed(h->cfgtable);
  7939. if (rc)
  7940. dev_info(&h->pdev->dev,
  7941. "Soft reset appears to have failed.\n");
  7942. /* since the controller's reset, we have to go back and re-init
  7943. * everything. Easiest to just forget what we've done and do it
  7944. * all over again.
  7945. */
  7946. hpsa_undo_allocations_after_kdump_soft_reset(h);
  7947. try_soft_reset = 0;
  7948. if (rc)
  7949. /* don't goto clean, we already unallocated */
  7950. return -ENODEV;
  7951. goto reinit_after_soft_reset;
  7952. }
  7953. /* Enable Accelerated IO path at driver layer */
  7954. h->acciopath_status = 1;
  7955. /* Disable discovery polling.*/
  7956. h->discovery_polling = 0;
  7957. /* Turn the interrupts on so we can service requests */
  7958. h->access.set_intr_mask(h, HPSA_INTR_ON);
  7959. hpsa_hba_inquiry(h);
  7960. h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
  7961. if (!h->lastlogicals)
  7962. dev_info(&h->pdev->dev,
  7963. "Can't track change to report lun data\n");
  7964. /* hook into SCSI subsystem */
  7965. rc = hpsa_scsi_add_host(h);
  7966. if (rc)
  7967. goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
  7968. /* Monitor the controller for firmware lockups */
  7969. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
  7970. INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
  7971. schedule_delayed_work(&h->monitor_ctlr_work,
  7972. h->heartbeat_sample_interval);
  7973. INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
  7974. queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
  7975. h->heartbeat_sample_interval);
  7976. return 0;
  7977. clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
  7978. hpsa_free_performant_mode(h);
  7979. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7980. clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
  7981. hpsa_free_sg_chain_blocks(h);
  7982. clean5: /* cmd, irq, shost, pci, lu, aer/h */
  7983. hpsa_free_cmd_pool(h);
  7984. clean4: /* irq, shost, pci, lu, aer/h */
  7985. hpsa_free_irqs(h);
  7986. clean3: /* shost, pci, lu, aer/h */
  7987. scsi_host_put(h->scsi_host);
  7988. h->scsi_host = NULL;
  7989. clean2_5: /* pci, lu, aer/h */
  7990. hpsa_free_pci_init(h);
  7991. clean2: /* lu, aer/h */
  7992. if (h->lockup_detected) {
  7993. free_percpu(h->lockup_detected);
  7994. h->lockup_detected = NULL;
  7995. }
  7996. clean1: /* wq/aer/h */
  7997. if (h->resubmit_wq) {
  7998. destroy_workqueue(h->resubmit_wq);
  7999. h->resubmit_wq = NULL;
  8000. }
  8001. if (h->rescan_ctlr_wq) {
  8002. destroy_workqueue(h->rescan_ctlr_wq);
  8003. h->rescan_ctlr_wq = NULL;
  8004. }
  8005. kfree(h);
  8006. return rc;
  8007. }
  8008. static void hpsa_flush_cache(struct ctlr_info *h)
  8009. {
  8010. char *flush_buf;
  8011. struct CommandList *c;
  8012. int rc;
  8013. if (unlikely(lockup_detected(h)))
  8014. return;
  8015. flush_buf = kzalloc(4, GFP_KERNEL);
  8016. if (!flush_buf)
  8017. return;
  8018. c = cmd_alloc(h);
  8019. if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  8020. RAID_CTLR_LUNID, TYPE_CMD)) {
  8021. goto out;
  8022. }
  8023. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8024. PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
  8025. if (rc)
  8026. goto out;
  8027. if (c->err_info->CommandStatus != 0)
  8028. out:
  8029. dev_warn(&h->pdev->dev,
  8030. "error flushing cache on controller\n");
  8031. cmd_free(h, c);
  8032. kfree(flush_buf);
  8033. }
  8034. /* Make controller gather fresh report lun data each time we
  8035. * send down a report luns request
  8036. */
  8037. static void hpsa_disable_rld_caching(struct ctlr_info *h)
  8038. {
  8039. u32 *options;
  8040. struct CommandList *c;
  8041. int rc;
  8042. /* Don't bother trying to set diag options if locked up */
  8043. if (unlikely(h->lockup_detected))
  8044. return;
  8045. options = kzalloc(sizeof(*options), GFP_KERNEL);
  8046. if (!options) {
  8047. dev_err(&h->pdev->dev,
  8048. "Error: failed to disable rld caching, during alloc.\n");
  8049. return;
  8050. }
  8051. c = cmd_alloc(h);
  8052. /* first, get the current diag options settings */
  8053. if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
  8054. RAID_CTLR_LUNID, TYPE_CMD))
  8055. goto errout;
  8056. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8057. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  8058. if ((rc != 0) || (c->err_info->CommandStatus != 0))
  8059. goto errout;
  8060. /* Now, set the bit for disabling the RLD caching */
  8061. *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
  8062. if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
  8063. RAID_CTLR_LUNID, TYPE_CMD))
  8064. goto errout;
  8065. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8066. PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
  8067. if ((rc != 0) || (c->err_info->CommandStatus != 0))
  8068. goto errout;
  8069. /* Now verify that it got set: */
  8070. if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
  8071. RAID_CTLR_LUNID, TYPE_CMD))
  8072. goto errout;
  8073. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8074. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  8075. if ((rc != 0) || (c->err_info->CommandStatus != 0))
  8076. goto errout;
  8077. if (*options & HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
  8078. goto out;
  8079. errout:
  8080. dev_err(&h->pdev->dev,
  8081. "Error: failed to disable report lun data caching.\n");
  8082. out:
  8083. cmd_free(h, c);
  8084. kfree(options);
  8085. }
  8086. static void hpsa_shutdown(struct pci_dev *pdev)
  8087. {
  8088. struct ctlr_info *h;
  8089. h = pci_get_drvdata(pdev);
  8090. /* Turn board interrupts off and send the flush cache command
  8091. * sendcmd will turn off interrupt, and send the flush...
  8092. * To write all data in the battery backed cache to disks
  8093. */
  8094. hpsa_flush_cache(h);
  8095. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  8096. hpsa_free_irqs(h); /* init_one 4 */
  8097. hpsa_disable_interrupt_mode(h); /* pci_init 2 */
  8098. }
  8099. static void hpsa_free_device_info(struct ctlr_info *h)
  8100. {
  8101. int i;
  8102. for (i = 0; i < h->ndevices; i++) {
  8103. kfree(h->dev[i]);
  8104. h->dev[i] = NULL;
  8105. }
  8106. }
  8107. static void hpsa_remove_one(struct pci_dev *pdev)
  8108. {
  8109. struct ctlr_info *h;
  8110. unsigned long flags;
  8111. if (pci_get_drvdata(pdev) == NULL) {
  8112. dev_err(&pdev->dev, "unable to remove device\n");
  8113. return;
  8114. }
  8115. h = pci_get_drvdata(pdev);
  8116. /* Get rid of any controller monitoring work items */
  8117. spin_lock_irqsave(&h->lock, flags);
  8118. h->remove_in_progress = 1;
  8119. spin_unlock_irqrestore(&h->lock, flags);
  8120. cancel_delayed_work_sync(&h->monitor_ctlr_work);
  8121. cancel_delayed_work_sync(&h->rescan_ctlr_work);
  8122. destroy_workqueue(h->rescan_ctlr_wq);
  8123. destroy_workqueue(h->resubmit_wq);
  8124. hpsa_delete_sas_host(h);
  8125. /*
  8126. * Call before disabling interrupts.
  8127. * scsi_remove_host can trigger I/O operations especially
  8128. * when multipath is enabled. There can be SYNCHRONIZE CACHE
  8129. * operations which cannot complete and will hang the system.
  8130. */
  8131. if (h->scsi_host)
  8132. scsi_remove_host(h->scsi_host); /* init_one 8 */
  8133. /* includes hpsa_free_irqs - init_one 4 */
  8134. /* includes hpsa_disable_interrupt_mode - pci_init 2 */
  8135. hpsa_shutdown(pdev);
  8136. hpsa_free_device_info(h); /* scan */
  8137. kfree(h->hba_inquiry_data); /* init_one 10 */
  8138. h->hba_inquiry_data = NULL; /* init_one 10 */
  8139. hpsa_free_ioaccel2_sg_chain_blocks(h);
  8140. hpsa_free_performant_mode(h); /* init_one 7 */
  8141. hpsa_free_sg_chain_blocks(h); /* init_one 6 */
  8142. hpsa_free_cmd_pool(h); /* init_one 5 */
  8143. kfree(h->lastlogicals);
  8144. /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
  8145. scsi_host_put(h->scsi_host); /* init_one 3 */
  8146. h->scsi_host = NULL; /* init_one 3 */
  8147. /* includes hpsa_disable_interrupt_mode - pci_init 2 */
  8148. hpsa_free_pci_init(h); /* init_one 2.5 */
  8149. free_percpu(h->lockup_detected); /* init_one 2 */
  8150. h->lockup_detected = NULL; /* init_one 2 */
  8151. /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
  8152. kfree(h); /* init_one 1 */
  8153. }
  8154. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  8155. __attribute__((unused)) pm_message_t state)
  8156. {
  8157. return -ENOSYS;
  8158. }
  8159. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  8160. {
  8161. return -ENOSYS;
  8162. }
  8163. static struct pci_driver hpsa_pci_driver = {
  8164. .name = HPSA,
  8165. .probe = hpsa_init_one,
  8166. .remove = hpsa_remove_one,
  8167. .id_table = hpsa_pci_device_id, /* id_table */
  8168. .shutdown = hpsa_shutdown,
  8169. .suspend = hpsa_suspend,
  8170. .resume = hpsa_resume,
  8171. };
  8172. /* Fill in bucket_map[], given nsgs (the max number of
  8173. * scatter gather elements supported) and bucket[],
  8174. * which is an array of 8 integers. The bucket[] array
  8175. * contains 8 different DMA transfer sizes (in 16
  8176. * byte increments) which the controller uses to fetch
  8177. * commands. This function fills in bucket_map[], which
  8178. * maps a given number of scatter gather elements to one of
  8179. * the 8 DMA transfer sizes. The point of it is to allow the
  8180. * controller to only do as much DMA as needed to fetch the
  8181. * command, with the DMA transfer size encoded in the lower
  8182. * bits of the command address.
  8183. */
  8184. static void calc_bucket_map(int bucket[], int num_buckets,
  8185. int nsgs, int min_blocks, u32 *bucket_map)
  8186. {
  8187. int i, j, b, size;
  8188. /* Note, bucket_map must have nsgs+1 entries. */
  8189. for (i = 0; i <= nsgs; i++) {
  8190. /* Compute size of a command with i SG entries */
  8191. size = i + min_blocks;
  8192. b = num_buckets; /* Assume the biggest bucket */
  8193. /* Find the bucket that is just big enough */
  8194. for (j = 0; j < num_buckets; j++) {
  8195. if (bucket[j] >= size) {
  8196. b = j;
  8197. break;
  8198. }
  8199. }
  8200. /* for a command with i SG entries, use bucket b. */
  8201. bucket_map[i] = b;
  8202. }
  8203. }
  8204. /*
  8205. * return -ENODEV on err, 0 on success (or no action)
  8206. * allocates numerous items that must be freed later
  8207. */
  8208. static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
  8209. {
  8210. int i;
  8211. unsigned long register_value;
  8212. unsigned long transMethod = CFGTBL_Trans_Performant |
  8213. (trans_support & CFGTBL_Trans_use_short_tags) |
  8214. CFGTBL_Trans_enable_directed_msix |
  8215. (trans_support & (CFGTBL_Trans_io_accel1 |
  8216. CFGTBL_Trans_io_accel2));
  8217. struct access_method access = SA5_performant_access;
  8218. /* This is a bit complicated. There are 8 registers on
  8219. * the controller which we write to to tell it 8 different
  8220. * sizes of commands which there may be. It's a way of
  8221. * reducing the DMA done to fetch each command. Encoded into
  8222. * each command's tag are 3 bits which communicate to the controller
  8223. * which of the eight sizes that command fits within. The size of
  8224. * each command depends on how many scatter gather entries there are.
  8225. * Each SG entry requires 16 bytes. The eight registers are programmed
  8226. * with the number of 16-byte blocks a command of that size requires.
  8227. * The smallest command possible requires 5 such 16 byte blocks.
  8228. * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
  8229. * blocks. Note, this only extends to the SG entries contained
  8230. * within the command block, and does not extend to chained blocks
  8231. * of SG elements. bft[] contains the eight values we write to
  8232. * the registers. They are not evenly distributed, but have more
  8233. * sizes for small commands, and fewer sizes for larger commands.
  8234. */
  8235. int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
  8236. #define MIN_IOACCEL2_BFT_ENTRY 5
  8237. #define HPSA_IOACCEL2_HEADER_SZ 4
  8238. int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
  8239. 13, 14, 15, 16, 17, 18, 19,
  8240. HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
  8241. BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
  8242. BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
  8243. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
  8244. 16 * MIN_IOACCEL2_BFT_ENTRY);
  8245. BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
  8246. BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
  8247. /* 5 = 1 s/g entry or 4k
  8248. * 6 = 2 s/g entry or 8k
  8249. * 8 = 4 s/g entry or 16k
  8250. * 10 = 6 s/g entry or 24k
  8251. */
  8252. /* If the controller supports either ioaccel method then
  8253. * we can also use the RAID stack submit path that does not
  8254. * perform the superfluous readl() after each command submission.
  8255. */
  8256. if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
  8257. access = SA5_performant_access_no_read;
  8258. /* Controller spec: zero out this buffer. */
  8259. for (i = 0; i < h->nreply_queues; i++)
  8260. memset(h->reply_queue[i].head, 0, h->reply_queue_size);
  8261. bft[7] = SG_ENTRIES_IN_CMD + 4;
  8262. calc_bucket_map(bft, ARRAY_SIZE(bft),
  8263. SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
  8264. for (i = 0; i < 8; i++)
  8265. writel(bft[i], &h->transtable->BlockFetch[i]);
  8266. /* size of controller ring buffer */
  8267. writel(h->max_commands, &h->transtable->RepQSize);
  8268. writel(h->nreply_queues, &h->transtable->RepQCount);
  8269. writel(0, &h->transtable->RepQCtrAddrLow32);
  8270. writel(0, &h->transtable->RepQCtrAddrHigh32);
  8271. for (i = 0; i < h->nreply_queues; i++) {
  8272. writel(0, &h->transtable->RepQAddr[i].upper);
  8273. writel(h->reply_queue[i].busaddr,
  8274. &h->transtable->RepQAddr[i].lower);
  8275. }
  8276. writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
  8277. writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
  8278. /*
  8279. * enable outbound interrupt coalescing in accelerator mode;
  8280. */
  8281. if (trans_support & CFGTBL_Trans_io_accel1) {
  8282. access = SA5_ioaccel_mode1_access;
  8283. writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
  8284. writel(4, &h->cfgtable->HostWrite.CoalIntCount);
  8285. } else {
  8286. if (trans_support & CFGTBL_Trans_io_accel2) {
  8287. access = SA5_ioaccel_mode2_access;
  8288. writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
  8289. writel(4, &h->cfgtable->HostWrite.CoalIntCount);
  8290. }
  8291. }
  8292. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  8293. if (hpsa_wait_for_mode_change_ack(h)) {
  8294. dev_err(&h->pdev->dev,
  8295. "performant mode problem - doorbell timeout\n");
  8296. return -ENODEV;
  8297. }
  8298. register_value = readl(&(h->cfgtable->TransportActive));
  8299. if (!(register_value & CFGTBL_Trans_Performant)) {
  8300. dev_err(&h->pdev->dev,
  8301. "performant mode problem - transport not active\n");
  8302. return -ENODEV;
  8303. }
  8304. /* Change the access methods to the performant access methods */
  8305. h->access = access;
  8306. h->transMethod = transMethod;
  8307. if (!((trans_support & CFGTBL_Trans_io_accel1) ||
  8308. (trans_support & CFGTBL_Trans_io_accel2)))
  8309. return 0;
  8310. if (trans_support & CFGTBL_Trans_io_accel1) {
  8311. /* Set up I/O accelerator mode */
  8312. for (i = 0; i < h->nreply_queues; i++) {
  8313. writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
  8314. h->reply_queue[i].current_entry =
  8315. readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
  8316. }
  8317. bft[7] = h->ioaccel_maxsg + 8;
  8318. calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
  8319. h->ioaccel1_blockFetchTable);
  8320. /* initialize all reply queue entries to unused */
  8321. for (i = 0; i < h->nreply_queues; i++)
  8322. memset(h->reply_queue[i].head,
  8323. (u8) IOACCEL_MODE1_REPLY_UNUSED,
  8324. h->reply_queue_size);
  8325. /* set all the constant fields in the accelerator command
  8326. * frames once at init time to save CPU cycles later.
  8327. */
  8328. for (i = 0; i < h->nr_cmds; i++) {
  8329. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
  8330. cp->function = IOACCEL1_FUNCTION_SCSIIO;
  8331. cp->err_info = (u32) (h->errinfo_pool_dhandle +
  8332. (i * sizeof(struct ErrorInfo)));
  8333. cp->err_info_len = sizeof(struct ErrorInfo);
  8334. cp->sgl_offset = IOACCEL1_SGLOFFSET;
  8335. cp->host_context_flags =
  8336. cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
  8337. cp->timeout_sec = 0;
  8338. cp->ReplyQueue = 0;
  8339. cp->tag =
  8340. cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
  8341. cp->host_addr =
  8342. cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
  8343. (i * sizeof(struct io_accel1_cmd)));
  8344. }
  8345. } else if (trans_support & CFGTBL_Trans_io_accel2) {
  8346. u64 cfg_offset, cfg_base_addr_index;
  8347. u32 bft2_offset, cfg_base_addr;
  8348. int rc;
  8349. rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  8350. &cfg_base_addr_index, &cfg_offset);
  8351. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
  8352. bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
  8353. calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
  8354. 4, h->ioaccel2_blockFetchTable);
  8355. bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
  8356. BUILD_BUG_ON(offsetof(struct CfgTable,
  8357. io_accel_request_size_offset) != 0xb8);
  8358. h->ioaccel2_bft2_regs =
  8359. remap_pci_mem(pci_resource_start(h->pdev,
  8360. cfg_base_addr_index) +
  8361. cfg_offset + bft2_offset,
  8362. ARRAY_SIZE(bft2) *
  8363. sizeof(*h->ioaccel2_bft2_regs));
  8364. for (i = 0; i < ARRAY_SIZE(bft2); i++)
  8365. writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
  8366. }
  8367. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  8368. if (hpsa_wait_for_mode_change_ack(h)) {
  8369. dev_err(&h->pdev->dev,
  8370. "performant mode problem - enabling ioaccel mode\n");
  8371. return -ENODEV;
  8372. }
  8373. return 0;
  8374. }
  8375. /* Free ioaccel1 mode command blocks and block fetch table */
  8376. static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
  8377. {
  8378. if (h->ioaccel_cmd_pool) {
  8379. pci_free_consistent(h->pdev,
  8380. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
  8381. h->ioaccel_cmd_pool,
  8382. h->ioaccel_cmd_pool_dhandle);
  8383. h->ioaccel_cmd_pool = NULL;
  8384. h->ioaccel_cmd_pool_dhandle = 0;
  8385. }
  8386. kfree(h->ioaccel1_blockFetchTable);
  8387. h->ioaccel1_blockFetchTable = NULL;
  8388. }
  8389. /* Allocate ioaccel1 mode command blocks and block fetch table */
  8390. static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
  8391. {
  8392. h->ioaccel_maxsg =
  8393. readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
  8394. if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
  8395. h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
  8396. /* Command structures must be aligned on a 128-byte boundary
  8397. * because the 7 lower bits of the address are used by the
  8398. * hardware.
  8399. */
  8400. BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
  8401. IOACCEL1_COMMANDLIST_ALIGNMENT);
  8402. h->ioaccel_cmd_pool =
  8403. pci_alloc_consistent(h->pdev,
  8404. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
  8405. &(h->ioaccel_cmd_pool_dhandle));
  8406. h->ioaccel1_blockFetchTable =
  8407. kmalloc(((h->ioaccel_maxsg + 1) *
  8408. sizeof(u32)), GFP_KERNEL);
  8409. if ((h->ioaccel_cmd_pool == NULL) ||
  8410. (h->ioaccel1_blockFetchTable == NULL))
  8411. goto clean_up;
  8412. memset(h->ioaccel_cmd_pool, 0,
  8413. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
  8414. return 0;
  8415. clean_up:
  8416. hpsa_free_ioaccel1_cmd_and_bft(h);
  8417. return -ENOMEM;
  8418. }
  8419. /* Free ioaccel2 mode command blocks and block fetch table */
  8420. static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
  8421. {
  8422. hpsa_free_ioaccel2_sg_chain_blocks(h);
  8423. if (h->ioaccel2_cmd_pool) {
  8424. pci_free_consistent(h->pdev,
  8425. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
  8426. h->ioaccel2_cmd_pool,
  8427. h->ioaccel2_cmd_pool_dhandle);
  8428. h->ioaccel2_cmd_pool = NULL;
  8429. h->ioaccel2_cmd_pool_dhandle = 0;
  8430. }
  8431. kfree(h->ioaccel2_blockFetchTable);
  8432. h->ioaccel2_blockFetchTable = NULL;
  8433. }
  8434. /* Allocate ioaccel2 mode command blocks and block fetch table */
  8435. static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
  8436. {
  8437. int rc;
  8438. /* Allocate ioaccel2 mode command blocks and block fetch table */
  8439. h->ioaccel_maxsg =
  8440. readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
  8441. if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
  8442. h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
  8443. BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
  8444. IOACCEL2_COMMANDLIST_ALIGNMENT);
  8445. h->ioaccel2_cmd_pool =
  8446. pci_alloc_consistent(h->pdev,
  8447. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
  8448. &(h->ioaccel2_cmd_pool_dhandle));
  8449. h->ioaccel2_blockFetchTable =
  8450. kmalloc(((h->ioaccel_maxsg + 1) *
  8451. sizeof(u32)), GFP_KERNEL);
  8452. if ((h->ioaccel2_cmd_pool == NULL) ||
  8453. (h->ioaccel2_blockFetchTable == NULL)) {
  8454. rc = -ENOMEM;
  8455. goto clean_up;
  8456. }
  8457. rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
  8458. if (rc)
  8459. goto clean_up;
  8460. memset(h->ioaccel2_cmd_pool, 0,
  8461. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
  8462. return 0;
  8463. clean_up:
  8464. hpsa_free_ioaccel2_cmd_and_bft(h);
  8465. return rc;
  8466. }
  8467. /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
  8468. static void hpsa_free_performant_mode(struct ctlr_info *h)
  8469. {
  8470. kfree(h->blockFetchTable);
  8471. h->blockFetchTable = NULL;
  8472. hpsa_free_reply_queues(h);
  8473. hpsa_free_ioaccel1_cmd_and_bft(h);
  8474. hpsa_free_ioaccel2_cmd_and_bft(h);
  8475. }
  8476. /* return -ENODEV on error, 0 on success (or no action)
  8477. * allocates numerous items that must be freed later
  8478. */
  8479. static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
  8480. {
  8481. u32 trans_support;
  8482. unsigned long transMethod = CFGTBL_Trans_Performant |
  8483. CFGTBL_Trans_use_short_tags;
  8484. int i, rc;
  8485. if (hpsa_simple_mode)
  8486. return 0;
  8487. trans_support = readl(&(h->cfgtable->TransportSupport));
  8488. if (!(trans_support & PERFORMANT_MODE))
  8489. return 0;
  8490. /* Check for I/O accelerator mode support */
  8491. if (trans_support & CFGTBL_Trans_io_accel1) {
  8492. transMethod |= CFGTBL_Trans_io_accel1 |
  8493. CFGTBL_Trans_enable_directed_msix;
  8494. rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
  8495. if (rc)
  8496. return rc;
  8497. } else if (trans_support & CFGTBL_Trans_io_accel2) {
  8498. transMethod |= CFGTBL_Trans_io_accel2 |
  8499. CFGTBL_Trans_enable_directed_msix;
  8500. rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
  8501. if (rc)
  8502. return rc;
  8503. }
  8504. h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
  8505. hpsa_get_max_perf_mode_cmds(h);
  8506. /* Performant mode ring buffer and supporting data structures */
  8507. h->reply_queue_size = h->max_commands * sizeof(u64);
  8508. for (i = 0; i < h->nreply_queues; i++) {
  8509. h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
  8510. h->reply_queue_size,
  8511. &(h->reply_queue[i].busaddr));
  8512. if (!h->reply_queue[i].head) {
  8513. rc = -ENOMEM;
  8514. goto clean1; /* rq, ioaccel */
  8515. }
  8516. h->reply_queue[i].size = h->max_commands;
  8517. h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
  8518. h->reply_queue[i].current_entry = 0;
  8519. }
  8520. /* Need a block fetch table for performant mode */
  8521. h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
  8522. sizeof(u32)), GFP_KERNEL);
  8523. if (!h->blockFetchTable) {
  8524. rc = -ENOMEM;
  8525. goto clean1; /* rq, ioaccel */
  8526. }
  8527. rc = hpsa_enter_performant_mode(h, trans_support);
  8528. if (rc)
  8529. goto clean2; /* bft, rq, ioaccel */
  8530. return 0;
  8531. clean2: /* bft, rq, ioaccel */
  8532. kfree(h->blockFetchTable);
  8533. h->blockFetchTable = NULL;
  8534. clean1: /* rq, ioaccel */
  8535. hpsa_free_reply_queues(h);
  8536. hpsa_free_ioaccel1_cmd_and_bft(h);
  8537. hpsa_free_ioaccel2_cmd_and_bft(h);
  8538. return rc;
  8539. }
  8540. static int is_accelerated_cmd(struct CommandList *c)
  8541. {
  8542. return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
  8543. }
  8544. static void hpsa_drain_accel_commands(struct ctlr_info *h)
  8545. {
  8546. struct CommandList *c = NULL;
  8547. int i, accel_cmds_out;
  8548. int refcount;
  8549. do { /* wait for all outstanding ioaccel commands to drain out */
  8550. accel_cmds_out = 0;
  8551. for (i = 0; i < h->nr_cmds; i++) {
  8552. c = h->cmd_pool + i;
  8553. refcount = atomic_inc_return(&c->refcount);
  8554. if (refcount > 1) /* Command is allocated */
  8555. accel_cmds_out += is_accelerated_cmd(c);
  8556. cmd_free(h, c);
  8557. }
  8558. if (accel_cmds_out <= 0)
  8559. break;
  8560. msleep(100);
  8561. } while (1);
  8562. }
  8563. static struct hpsa_sas_phy *hpsa_alloc_sas_phy(
  8564. struct hpsa_sas_port *hpsa_sas_port)
  8565. {
  8566. struct hpsa_sas_phy *hpsa_sas_phy;
  8567. struct sas_phy *phy;
  8568. hpsa_sas_phy = kzalloc(sizeof(*hpsa_sas_phy), GFP_KERNEL);
  8569. if (!hpsa_sas_phy)
  8570. return NULL;
  8571. phy = sas_phy_alloc(hpsa_sas_port->parent_node->parent_dev,
  8572. hpsa_sas_port->next_phy_index);
  8573. if (!phy) {
  8574. kfree(hpsa_sas_phy);
  8575. return NULL;
  8576. }
  8577. hpsa_sas_port->next_phy_index++;
  8578. hpsa_sas_phy->phy = phy;
  8579. hpsa_sas_phy->parent_port = hpsa_sas_port;
  8580. return hpsa_sas_phy;
  8581. }
  8582. static void hpsa_free_sas_phy(struct hpsa_sas_phy *hpsa_sas_phy)
  8583. {
  8584. struct sas_phy *phy = hpsa_sas_phy->phy;
  8585. sas_port_delete_phy(hpsa_sas_phy->parent_port->port, phy);
  8586. if (hpsa_sas_phy->added_to_port)
  8587. list_del(&hpsa_sas_phy->phy_list_entry);
  8588. sas_phy_delete(phy);
  8589. kfree(hpsa_sas_phy);
  8590. }
  8591. static int hpsa_sas_port_add_phy(struct hpsa_sas_phy *hpsa_sas_phy)
  8592. {
  8593. int rc;
  8594. struct hpsa_sas_port *hpsa_sas_port;
  8595. struct sas_phy *phy;
  8596. struct sas_identify *identify;
  8597. hpsa_sas_port = hpsa_sas_phy->parent_port;
  8598. phy = hpsa_sas_phy->phy;
  8599. identify = &phy->identify;
  8600. memset(identify, 0, sizeof(*identify));
  8601. identify->sas_address = hpsa_sas_port->sas_address;
  8602. identify->device_type = SAS_END_DEVICE;
  8603. identify->initiator_port_protocols = SAS_PROTOCOL_STP;
  8604. identify->target_port_protocols = SAS_PROTOCOL_STP;
  8605. phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
  8606. phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
  8607. phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
  8608. phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
  8609. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  8610. rc = sas_phy_add(hpsa_sas_phy->phy);
  8611. if (rc)
  8612. return rc;
  8613. sas_port_add_phy(hpsa_sas_port->port, hpsa_sas_phy->phy);
  8614. list_add_tail(&hpsa_sas_phy->phy_list_entry,
  8615. &hpsa_sas_port->phy_list_head);
  8616. hpsa_sas_phy->added_to_port = true;
  8617. return 0;
  8618. }
  8619. static int
  8620. hpsa_sas_port_add_rphy(struct hpsa_sas_port *hpsa_sas_port,
  8621. struct sas_rphy *rphy)
  8622. {
  8623. struct sas_identify *identify;
  8624. identify = &rphy->identify;
  8625. identify->sas_address = hpsa_sas_port->sas_address;
  8626. identify->initiator_port_protocols = SAS_PROTOCOL_STP;
  8627. identify->target_port_protocols = SAS_PROTOCOL_STP;
  8628. return sas_rphy_add(rphy);
  8629. }
  8630. static struct hpsa_sas_port
  8631. *hpsa_alloc_sas_port(struct hpsa_sas_node *hpsa_sas_node,
  8632. u64 sas_address)
  8633. {
  8634. int rc;
  8635. struct hpsa_sas_port *hpsa_sas_port;
  8636. struct sas_port *port;
  8637. hpsa_sas_port = kzalloc(sizeof(*hpsa_sas_port), GFP_KERNEL);
  8638. if (!hpsa_sas_port)
  8639. return NULL;
  8640. INIT_LIST_HEAD(&hpsa_sas_port->phy_list_head);
  8641. hpsa_sas_port->parent_node = hpsa_sas_node;
  8642. port = sas_port_alloc_num(hpsa_sas_node->parent_dev);
  8643. if (!port)
  8644. goto free_hpsa_port;
  8645. rc = sas_port_add(port);
  8646. if (rc)
  8647. goto free_sas_port;
  8648. hpsa_sas_port->port = port;
  8649. hpsa_sas_port->sas_address = sas_address;
  8650. list_add_tail(&hpsa_sas_port->port_list_entry,
  8651. &hpsa_sas_node->port_list_head);
  8652. return hpsa_sas_port;
  8653. free_sas_port:
  8654. sas_port_free(port);
  8655. free_hpsa_port:
  8656. kfree(hpsa_sas_port);
  8657. return NULL;
  8658. }
  8659. static void hpsa_free_sas_port(struct hpsa_sas_port *hpsa_sas_port)
  8660. {
  8661. struct hpsa_sas_phy *hpsa_sas_phy;
  8662. struct hpsa_sas_phy *next;
  8663. list_for_each_entry_safe(hpsa_sas_phy, next,
  8664. &hpsa_sas_port->phy_list_head, phy_list_entry)
  8665. hpsa_free_sas_phy(hpsa_sas_phy);
  8666. sas_port_delete(hpsa_sas_port->port);
  8667. list_del(&hpsa_sas_port->port_list_entry);
  8668. kfree(hpsa_sas_port);
  8669. }
  8670. static struct hpsa_sas_node *hpsa_alloc_sas_node(struct device *parent_dev)
  8671. {
  8672. struct hpsa_sas_node *hpsa_sas_node;
  8673. hpsa_sas_node = kzalloc(sizeof(*hpsa_sas_node), GFP_KERNEL);
  8674. if (hpsa_sas_node) {
  8675. hpsa_sas_node->parent_dev = parent_dev;
  8676. INIT_LIST_HEAD(&hpsa_sas_node->port_list_head);
  8677. }
  8678. return hpsa_sas_node;
  8679. }
  8680. static void hpsa_free_sas_node(struct hpsa_sas_node *hpsa_sas_node)
  8681. {
  8682. struct hpsa_sas_port *hpsa_sas_port;
  8683. struct hpsa_sas_port *next;
  8684. if (!hpsa_sas_node)
  8685. return;
  8686. list_for_each_entry_safe(hpsa_sas_port, next,
  8687. &hpsa_sas_node->port_list_head, port_list_entry)
  8688. hpsa_free_sas_port(hpsa_sas_port);
  8689. kfree(hpsa_sas_node);
  8690. }
  8691. static struct hpsa_scsi_dev_t
  8692. *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
  8693. struct sas_rphy *rphy)
  8694. {
  8695. int i;
  8696. struct hpsa_scsi_dev_t *device;
  8697. for (i = 0; i < h->ndevices; i++) {
  8698. device = h->dev[i];
  8699. if (!device->sas_port)
  8700. continue;
  8701. if (device->sas_port->rphy == rphy)
  8702. return device;
  8703. }
  8704. return NULL;
  8705. }
  8706. static int hpsa_add_sas_host(struct ctlr_info *h)
  8707. {
  8708. int rc;
  8709. struct device *parent_dev;
  8710. struct hpsa_sas_node *hpsa_sas_node;
  8711. struct hpsa_sas_port *hpsa_sas_port;
  8712. struct hpsa_sas_phy *hpsa_sas_phy;
  8713. parent_dev = &h->scsi_host->shost_gendev;
  8714. hpsa_sas_node = hpsa_alloc_sas_node(parent_dev);
  8715. if (!hpsa_sas_node)
  8716. return -ENOMEM;
  8717. hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, h->sas_address);
  8718. if (!hpsa_sas_port) {
  8719. rc = -ENODEV;
  8720. goto free_sas_node;
  8721. }
  8722. hpsa_sas_phy = hpsa_alloc_sas_phy(hpsa_sas_port);
  8723. if (!hpsa_sas_phy) {
  8724. rc = -ENODEV;
  8725. goto free_sas_port;
  8726. }
  8727. rc = hpsa_sas_port_add_phy(hpsa_sas_phy);
  8728. if (rc)
  8729. goto free_sas_phy;
  8730. h->sas_host = hpsa_sas_node;
  8731. return 0;
  8732. free_sas_phy:
  8733. hpsa_free_sas_phy(hpsa_sas_phy);
  8734. free_sas_port:
  8735. hpsa_free_sas_port(hpsa_sas_port);
  8736. free_sas_node:
  8737. hpsa_free_sas_node(hpsa_sas_node);
  8738. return rc;
  8739. }
  8740. static void hpsa_delete_sas_host(struct ctlr_info *h)
  8741. {
  8742. hpsa_free_sas_node(h->sas_host);
  8743. }
  8744. static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
  8745. struct hpsa_scsi_dev_t *device)
  8746. {
  8747. int rc;
  8748. struct hpsa_sas_port *hpsa_sas_port;
  8749. struct sas_rphy *rphy;
  8750. hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, device->sas_address);
  8751. if (!hpsa_sas_port)
  8752. return -ENOMEM;
  8753. rphy = sas_end_device_alloc(hpsa_sas_port->port);
  8754. if (!rphy) {
  8755. rc = -ENODEV;
  8756. goto free_sas_port;
  8757. }
  8758. hpsa_sas_port->rphy = rphy;
  8759. device->sas_port = hpsa_sas_port;
  8760. rc = hpsa_sas_port_add_rphy(hpsa_sas_port, rphy);
  8761. if (rc)
  8762. goto free_sas_port;
  8763. return 0;
  8764. free_sas_port:
  8765. hpsa_free_sas_port(hpsa_sas_port);
  8766. device->sas_port = NULL;
  8767. return rc;
  8768. }
  8769. static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device)
  8770. {
  8771. if (device->sas_port) {
  8772. hpsa_free_sas_port(device->sas_port);
  8773. device->sas_port = NULL;
  8774. }
  8775. }
  8776. static int
  8777. hpsa_sas_get_linkerrors(struct sas_phy *phy)
  8778. {
  8779. return 0;
  8780. }
  8781. static int
  8782. hpsa_sas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  8783. {
  8784. *identifier = 0;
  8785. return 0;
  8786. }
  8787. static int
  8788. hpsa_sas_get_bay_identifier(struct sas_rphy *rphy)
  8789. {
  8790. return -ENXIO;
  8791. }
  8792. static int
  8793. hpsa_sas_phy_reset(struct sas_phy *phy, int hard_reset)
  8794. {
  8795. return 0;
  8796. }
  8797. static int
  8798. hpsa_sas_phy_enable(struct sas_phy *phy, int enable)
  8799. {
  8800. return 0;
  8801. }
  8802. static int
  8803. hpsa_sas_phy_setup(struct sas_phy *phy)
  8804. {
  8805. return 0;
  8806. }
  8807. static void
  8808. hpsa_sas_phy_release(struct sas_phy *phy)
  8809. {
  8810. }
  8811. static int
  8812. hpsa_sas_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  8813. {
  8814. return -EINVAL;
  8815. }
  8816. /* SMP = Serial Management Protocol */
  8817. static int
  8818. hpsa_sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  8819. struct request *req)
  8820. {
  8821. return -EINVAL;
  8822. }
  8823. static struct sas_function_template hpsa_sas_transport_functions = {
  8824. .get_linkerrors = hpsa_sas_get_linkerrors,
  8825. .get_enclosure_identifier = hpsa_sas_get_enclosure_identifier,
  8826. .get_bay_identifier = hpsa_sas_get_bay_identifier,
  8827. .phy_reset = hpsa_sas_phy_reset,
  8828. .phy_enable = hpsa_sas_phy_enable,
  8829. .phy_setup = hpsa_sas_phy_setup,
  8830. .phy_release = hpsa_sas_phy_release,
  8831. .set_phy_speed = hpsa_sas_phy_speed,
  8832. .smp_handler = hpsa_sas_smp_handler,
  8833. };
  8834. /*
  8835. * This is it. Register the PCI driver information for the cards we control
  8836. * the OS will call our registered routines when it finds one of our cards.
  8837. */
  8838. static int __init hpsa_init(void)
  8839. {
  8840. int rc;
  8841. hpsa_sas_transport_template =
  8842. sas_attach_transport(&hpsa_sas_transport_functions);
  8843. if (!hpsa_sas_transport_template)
  8844. return -ENODEV;
  8845. rc = pci_register_driver(&hpsa_pci_driver);
  8846. if (rc)
  8847. sas_release_transport(hpsa_sas_transport_template);
  8848. return rc;
  8849. }
  8850. static void __exit hpsa_cleanup(void)
  8851. {
  8852. pci_unregister_driver(&hpsa_pci_driver);
  8853. sas_release_transport(hpsa_sas_transport_template);
  8854. }
  8855. static void __attribute__((unused)) verify_offsets(void)
  8856. {
  8857. #define VERIFY_OFFSET(member, offset) \
  8858. BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
  8859. VERIFY_OFFSET(structure_size, 0);
  8860. VERIFY_OFFSET(volume_blk_size, 4);
  8861. VERIFY_OFFSET(volume_blk_cnt, 8);
  8862. VERIFY_OFFSET(phys_blk_shift, 16);
  8863. VERIFY_OFFSET(parity_rotation_shift, 17);
  8864. VERIFY_OFFSET(strip_size, 18);
  8865. VERIFY_OFFSET(disk_starting_blk, 20);
  8866. VERIFY_OFFSET(disk_blk_cnt, 28);
  8867. VERIFY_OFFSET(data_disks_per_row, 36);
  8868. VERIFY_OFFSET(metadata_disks_per_row, 38);
  8869. VERIFY_OFFSET(row_cnt, 40);
  8870. VERIFY_OFFSET(layout_map_count, 42);
  8871. VERIFY_OFFSET(flags, 44);
  8872. VERIFY_OFFSET(dekindex, 46);
  8873. /* VERIFY_OFFSET(reserved, 48 */
  8874. VERIFY_OFFSET(data, 64);
  8875. #undef VERIFY_OFFSET
  8876. #define VERIFY_OFFSET(member, offset) \
  8877. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
  8878. VERIFY_OFFSET(IU_type, 0);
  8879. VERIFY_OFFSET(direction, 1);
  8880. VERIFY_OFFSET(reply_queue, 2);
  8881. /* VERIFY_OFFSET(reserved1, 3); */
  8882. VERIFY_OFFSET(scsi_nexus, 4);
  8883. VERIFY_OFFSET(Tag, 8);
  8884. VERIFY_OFFSET(cdb, 16);
  8885. VERIFY_OFFSET(cciss_lun, 32);
  8886. VERIFY_OFFSET(data_len, 40);
  8887. VERIFY_OFFSET(cmd_priority_task_attr, 44);
  8888. VERIFY_OFFSET(sg_count, 45);
  8889. /* VERIFY_OFFSET(reserved3 */
  8890. VERIFY_OFFSET(err_ptr, 48);
  8891. VERIFY_OFFSET(err_len, 56);
  8892. /* VERIFY_OFFSET(reserved4 */
  8893. VERIFY_OFFSET(sg, 64);
  8894. #undef VERIFY_OFFSET
  8895. #define VERIFY_OFFSET(member, offset) \
  8896. BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
  8897. VERIFY_OFFSET(dev_handle, 0x00);
  8898. VERIFY_OFFSET(reserved1, 0x02);
  8899. VERIFY_OFFSET(function, 0x03);
  8900. VERIFY_OFFSET(reserved2, 0x04);
  8901. VERIFY_OFFSET(err_info, 0x0C);
  8902. VERIFY_OFFSET(reserved3, 0x10);
  8903. VERIFY_OFFSET(err_info_len, 0x12);
  8904. VERIFY_OFFSET(reserved4, 0x13);
  8905. VERIFY_OFFSET(sgl_offset, 0x14);
  8906. VERIFY_OFFSET(reserved5, 0x15);
  8907. VERIFY_OFFSET(transfer_len, 0x1C);
  8908. VERIFY_OFFSET(reserved6, 0x20);
  8909. VERIFY_OFFSET(io_flags, 0x24);
  8910. VERIFY_OFFSET(reserved7, 0x26);
  8911. VERIFY_OFFSET(LUN, 0x34);
  8912. VERIFY_OFFSET(control, 0x3C);
  8913. VERIFY_OFFSET(CDB, 0x40);
  8914. VERIFY_OFFSET(reserved8, 0x50);
  8915. VERIFY_OFFSET(host_context_flags, 0x60);
  8916. VERIFY_OFFSET(timeout_sec, 0x62);
  8917. VERIFY_OFFSET(ReplyQueue, 0x64);
  8918. VERIFY_OFFSET(reserved9, 0x65);
  8919. VERIFY_OFFSET(tag, 0x68);
  8920. VERIFY_OFFSET(host_addr, 0x70);
  8921. VERIFY_OFFSET(CISS_LUN, 0x78);
  8922. VERIFY_OFFSET(SG, 0x78 + 8);
  8923. #undef VERIFY_OFFSET
  8924. }
  8925. module_init(hpsa_init);
  8926. module_exit(hpsa_cleanup);