pmcraid.c 169 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155
  1. /*
  2. * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
  3. *
  4. * Written By: Anil Ravindranath<anil_ravindranath@pmc-sierra.com>
  5. * PMC-Sierra Inc
  6. *
  7. * Copyright (C) 2008, 2009 PMC Sierra Inc
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
  22. * USA
  23. *
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/ioport.h>
  31. #include <linux/delay.h>
  32. #include <linux/pci.h>
  33. #include <linux/wait.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/sched.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/firmware.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/hdreg.h>
  42. #include <linux/version.h>
  43. #include <linux/io.h>
  44. #include <linux/slab.h>
  45. #include <asm/irq.h>
  46. #include <asm/processor.h>
  47. #include <linux/libata.h>
  48. #include <linux/mutex.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_host.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_tcq.h>
  53. #include <scsi/scsi_eh.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsicam.h>
  56. #include "pmcraid.h"
  57. /*
  58. * Module configuration parameters
  59. */
  60. static unsigned int pmcraid_debug_log;
  61. static unsigned int pmcraid_disable_aen;
  62. static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
  63. /*
  64. * Data structures to support multiple adapters by the LLD.
  65. * pmcraid_adapter_count - count of configured adapters
  66. */
  67. static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
  68. /*
  69. * Supporting user-level control interface through IOCTL commands.
  70. * pmcraid_major - major number to use
  71. * pmcraid_minor - minor number(s) to use
  72. */
  73. static unsigned int pmcraid_major;
  74. static struct class *pmcraid_class;
  75. DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  76. /*
  77. * Module parameters
  78. */
  79. MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
  80. MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
  81. MODULE_LICENSE("GPL");
  82. MODULE_VERSION(PMCRAID_DRIVER_VERSION);
  83. module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
  84. MODULE_PARM_DESC(log_level,
  85. "Enables firmware error code logging, default :1 high-severity"
  86. " errors, 2: all errors including high-severity errors,"
  87. " 0: disables logging");
  88. module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
  89. MODULE_PARM_DESC(debug,
  90. "Enable driver verbose message logging. Set 1 to enable."
  91. "(default: 0)");
  92. module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
  93. MODULE_PARM_DESC(disable_aen,
  94. "Disable driver aen notifications to apps. Set 1 to disable."
  95. "(default: 0)");
  96. /* chip specific constants for PMC MaxRAID controllers (same for
  97. * 0x5220 and 0x8010
  98. */
  99. static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
  100. {
  101. .ioastatus = 0x0,
  102. .ioarrin = 0x00040,
  103. .mailbox = 0x7FC30,
  104. .global_intr_mask = 0x00034,
  105. .ioa_host_intr = 0x0009C,
  106. .ioa_host_intr_clr = 0x000A0,
  107. .ioa_host_msix_intr = 0x7FC40,
  108. .ioa_host_mask = 0x7FC28,
  109. .ioa_host_mask_clr = 0x7FC28,
  110. .host_ioa_intr = 0x00020,
  111. .host_ioa_intr_clr = 0x00020,
  112. .transop_timeout = 300
  113. }
  114. };
  115. /*
  116. * PCI device ids supported by pmcraid driver
  117. */
  118. static struct pci_device_id pmcraid_pci_table[] __devinitdata = {
  119. { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
  120. 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
  121. },
  122. {}
  123. };
  124. MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
  125. /**
  126. * pmcraid_slave_alloc - Prepare for commands to a device
  127. * @scsi_dev: scsi device struct
  128. *
  129. * This function is called by mid-layer prior to sending any command to the new
  130. * device. Stores resource entry details of the device in scsi_device struct.
  131. * Queuecommand uses the resource handle and other details to fill up IOARCB
  132. * while sending commands to the device.
  133. *
  134. * Return value:
  135. * 0 on success / -ENXIO if device does not exist
  136. */
  137. static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
  138. {
  139. struct pmcraid_resource_entry *temp, *res = NULL;
  140. struct pmcraid_instance *pinstance;
  141. u8 target, bus, lun;
  142. unsigned long lock_flags;
  143. int rc = -ENXIO;
  144. u16 fw_version;
  145. pinstance = shost_priv(scsi_dev->host);
  146. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  147. /* Driver exposes VSET and GSCSI resources only; all other device types
  148. * are not exposed. Resource list is synchronized using resource lock
  149. * so any traversal or modifications to the list should be done inside
  150. * this lock
  151. */
  152. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  153. list_for_each_entry(temp, &pinstance->used_res_q, queue) {
  154. /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
  155. if (RES_IS_VSET(temp->cfg_entry)) {
  156. if (fw_version <= PMCRAID_FW_VERSION_1)
  157. target = temp->cfg_entry.unique_flags1;
  158. else
  159. target = temp->cfg_entry.array_id & 0xFF;
  160. if (target > PMCRAID_MAX_VSET_TARGETS)
  161. continue;
  162. bus = PMCRAID_VSET_BUS_ID;
  163. lun = 0;
  164. } else if (RES_IS_GSCSI(temp->cfg_entry)) {
  165. target = RES_TARGET(temp->cfg_entry.resource_address);
  166. bus = PMCRAID_PHYS_BUS_ID;
  167. lun = RES_LUN(temp->cfg_entry.resource_address);
  168. } else {
  169. continue;
  170. }
  171. if (bus == scsi_dev->channel &&
  172. target == scsi_dev->id &&
  173. lun == scsi_dev->lun) {
  174. res = temp;
  175. break;
  176. }
  177. }
  178. if (res) {
  179. res->scsi_dev = scsi_dev;
  180. scsi_dev->hostdata = res;
  181. res->change_detected = 0;
  182. atomic_set(&res->read_failures, 0);
  183. atomic_set(&res->write_failures, 0);
  184. rc = 0;
  185. }
  186. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  187. return rc;
  188. }
  189. /**
  190. * pmcraid_slave_configure - Configures a SCSI device
  191. * @scsi_dev: scsi device struct
  192. *
  193. * This fucntion is executed by SCSI mid layer just after a device is first
  194. * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
  195. * timeout value (default 30s) will be over-written to a higher value (60s)
  196. * and max_sectors value will be over-written to 512. It also sets queue depth
  197. * to host->cmd_per_lun value
  198. *
  199. * Return value:
  200. * 0 on success
  201. */
  202. static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
  203. {
  204. struct pmcraid_resource_entry *res = scsi_dev->hostdata;
  205. if (!res)
  206. return 0;
  207. /* LLD exposes VSETs and Enclosure devices only */
  208. if (RES_IS_GSCSI(res->cfg_entry) &&
  209. scsi_dev->type != TYPE_ENCLOSURE)
  210. return -ENXIO;
  211. pmcraid_info("configuring %x:%x:%x:%x\n",
  212. scsi_dev->host->unique_id,
  213. scsi_dev->channel,
  214. scsi_dev->id,
  215. scsi_dev->lun);
  216. if (RES_IS_GSCSI(res->cfg_entry)) {
  217. scsi_dev->allow_restart = 1;
  218. } else if (RES_IS_VSET(res->cfg_entry)) {
  219. scsi_dev->allow_restart = 1;
  220. blk_queue_rq_timeout(scsi_dev->request_queue,
  221. PMCRAID_VSET_IO_TIMEOUT);
  222. blk_queue_max_hw_sectors(scsi_dev->request_queue,
  223. PMCRAID_VSET_MAX_SECTORS);
  224. }
  225. if (scsi_dev->tagged_supported &&
  226. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
  227. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  228. scsi_adjust_queue_depth(scsi_dev, MSG_SIMPLE_TAG,
  229. scsi_dev->host->cmd_per_lun);
  230. } else {
  231. scsi_adjust_queue_depth(scsi_dev, 0,
  232. scsi_dev->host->cmd_per_lun);
  233. }
  234. return 0;
  235. }
  236. /**
  237. * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
  238. *
  239. * @scsi_dev: scsi device struct
  240. *
  241. * This is called by mid-layer before removing a device. Pointer assignments
  242. * done in pmcraid_slave_alloc will be reset to NULL here.
  243. *
  244. * Return value
  245. * none
  246. */
  247. static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
  248. {
  249. struct pmcraid_resource_entry *res;
  250. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  251. if (res)
  252. res->scsi_dev = NULL;
  253. scsi_dev->hostdata = NULL;
  254. }
  255. /**
  256. * pmcraid_change_queue_depth - Change the device's queue depth
  257. * @scsi_dev: scsi device struct
  258. * @depth: depth to set
  259. * @reason: calling context
  260. *
  261. * Return value
  262. * actual depth set
  263. */
  264. static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth,
  265. int reason)
  266. {
  267. if (reason != SCSI_QDEPTH_DEFAULT)
  268. return -EOPNOTSUPP;
  269. if (depth > PMCRAID_MAX_CMD_PER_LUN)
  270. depth = PMCRAID_MAX_CMD_PER_LUN;
  271. scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
  272. return scsi_dev->queue_depth;
  273. }
  274. /**
  275. * pmcraid_change_queue_type - Change the device's queue type
  276. * @scsi_dev: scsi device struct
  277. * @tag: type of tags to use
  278. *
  279. * Return value:
  280. * actual queue type set
  281. */
  282. static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
  283. {
  284. struct pmcraid_resource_entry *res;
  285. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  286. if ((res) && scsi_dev->tagged_supported &&
  287. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
  288. scsi_set_tag_type(scsi_dev, tag);
  289. if (tag)
  290. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  291. else
  292. scsi_deactivate_tcq(scsi_dev, scsi_dev->queue_depth);
  293. } else
  294. tag = 0;
  295. return tag;
  296. }
  297. /**
  298. * pmcraid_init_cmdblk - initializes a command block
  299. *
  300. * @cmd: pointer to struct pmcraid_cmd to be initialized
  301. * @index: if >=0 first time initialization; otherwise reinitialization
  302. *
  303. * Return Value
  304. * None
  305. */
  306. void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
  307. {
  308. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  309. dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
  310. if (index >= 0) {
  311. /* first time initialization (called from probe) */
  312. u32 ioasa_offset =
  313. offsetof(struct pmcraid_control_block, ioasa);
  314. cmd->index = index;
  315. ioarcb->response_handle = cpu_to_le32(index << 2);
  316. ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
  317. ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
  318. ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
  319. } else {
  320. /* re-initialization of various lengths, called once command is
  321. * processed by IOA
  322. */
  323. memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
  324. ioarcb->hrrq_id = 0;
  325. ioarcb->request_flags0 = 0;
  326. ioarcb->request_flags1 = 0;
  327. ioarcb->cmd_timeout = 0;
  328. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  329. ioarcb->ioadl_bus_addr = 0;
  330. ioarcb->ioadl_length = 0;
  331. ioarcb->data_transfer_length = 0;
  332. ioarcb->add_cmd_param_length = 0;
  333. ioarcb->add_cmd_param_offset = 0;
  334. cmd->ioa_cb->ioasa.ioasc = 0;
  335. cmd->ioa_cb->ioasa.residual_data_length = 0;
  336. cmd->time_left = 0;
  337. }
  338. cmd->cmd_done = NULL;
  339. cmd->scsi_cmd = NULL;
  340. cmd->release = 0;
  341. cmd->completion_req = 0;
  342. cmd->sense_buffer = 0;
  343. cmd->sense_buffer_dma = 0;
  344. cmd->dma_handle = 0;
  345. init_timer(&cmd->timer);
  346. }
  347. /**
  348. * pmcraid_reinit_cmdblk - reinitialize a command block
  349. *
  350. * @cmd: pointer to struct pmcraid_cmd to be reinitialized
  351. *
  352. * Return Value
  353. * None
  354. */
  355. static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
  356. {
  357. pmcraid_init_cmdblk(cmd, -1);
  358. }
  359. /**
  360. * pmcraid_get_free_cmd - get a free cmd block from command block pool
  361. * @pinstance: adapter instance structure
  362. *
  363. * Return Value:
  364. * returns pointer to cmd block or NULL if no blocks are available
  365. */
  366. static struct pmcraid_cmd *pmcraid_get_free_cmd(
  367. struct pmcraid_instance *pinstance
  368. )
  369. {
  370. struct pmcraid_cmd *cmd = NULL;
  371. unsigned long lock_flags;
  372. /* free cmd block list is protected by free_pool_lock */
  373. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  374. if (!list_empty(&pinstance->free_cmd_pool)) {
  375. cmd = list_entry(pinstance->free_cmd_pool.next,
  376. struct pmcraid_cmd, free_list);
  377. list_del(&cmd->free_list);
  378. }
  379. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  380. /* Initialize the command block before giving it the caller */
  381. if (cmd != NULL)
  382. pmcraid_reinit_cmdblk(cmd);
  383. return cmd;
  384. }
  385. /**
  386. * pmcraid_return_cmd - return a completed command block back into free pool
  387. * @cmd: pointer to the command block
  388. *
  389. * Return Value:
  390. * nothing
  391. */
  392. void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
  393. {
  394. struct pmcraid_instance *pinstance = cmd->drv_inst;
  395. unsigned long lock_flags;
  396. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  397. list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
  398. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  399. }
  400. /**
  401. * pmcraid_read_interrupts - reads IOA interrupts
  402. *
  403. * @pinstance: pointer to adapter instance structure
  404. *
  405. * Return value
  406. * interrupts read from IOA
  407. */
  408. static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
  409. {
  410. return (pinstance->interrupt_mode) ?
  411. ioread32(pinstance->int_regs.ioa_host_msix_interrupt_reg) :
  412. ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  413. }
  414. /**
  415. * pmcraid_disable_interrupts - Masks and clears all specified interrupts
  416. *
  417. * @pinstance: pointer to per adapter instance structure
  418. * @intrs: interrupts to disable
  419. *
  420. * Return Value
  421. * None
  422. */
  423. static void pmcraid_disable_interrupts(
  424. struct pmcraid_instance *pinstance,
  425. u32 intrs
  426. )
  427. {
  428. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  429. u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
  430. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
  431. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  432. ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  433. if (!pinstance->interrupt_mode) {
  434. iowrite32(intrs,
  435. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  436. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  437. }
  438. }
  439. /**
  440. * pmcraid_enable_interrupts - Enables specified interrupts
  441. *
  442. * @pinstance: pointer to per adapter instance structure
  443. * @intr: interrupts to enable
  444. *
  445. * Return Value
  446. * None
  447. */
  448. static void pmcraid_enable_interrupts(
  449. struct pmcraid_instance *pinstance,
  450. u32 intrs
  451. )
  452. {
  453. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  454. u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
  455. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  456. if (!pinstance->interrupt_mode) {
  457. iowrite32(~intrs,
  458. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  459. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  460. }
  461. pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
  462. ioread32(pinstance->int_regs.global_interrupt_mask_reg),
  463. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
  464. }
  465. /**
  466. * pmcraid_clr_trans_op - clear trans to op interrupt
  467. *
  468. * @pinstance: pointer to per adapter instance structure
  469. *
  470. * Return Value
  471. * None
  472. */
  473. static void pmcraid_clr_trans_op(
  474. struct pmcraid_instance *pinstance
  475. )
  476. {
  477. unsigned long lock_flags;
  478. if (!pinstance->interrupt_mode) {
  479. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  480. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  481. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  482. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  483. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  484. ioread32(pinstance->int_regs.ioa_host_interrupt_clr_reg);
  485. }
  486. if (pinstance->reset_cmd != NULL) {
  487. del_timer(&pinstance->reset_cmd->timer);
  488. spin_lock_irqsave(
  489. pinstance->host->host_lock, lock_flags);
  490. pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
  491. spin_unlock_irqrestore(
  492. pinstance->host->host_lock, lock_flags);
  493. }
  494. }
  495. /**
  496. * pmcraid_reset_type - Determine the required reset type
  497. * @pinstance: pointer to adapter instance structure
  498. *
  499. * IOA requires hard reset if any of the following conditions is true.
  500. * 1. If HRRQ valid interrupt is not masked
  501. * 2. IOA reset alert doorbell is set
  502. * 3. If there are any error interrupts
  503. */
  504. static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
  505. {
  506. u32 mask;
  507. u32 intrs;
  508. u32 alerts;
  509. mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  510. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  511. alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  512. if ((mask & INTRS_HRRQ_VALID) == 0 ||
  513. (alerts & DOORBELL_IOA_RESET_ALERT) ||
  514. (intrs & PMCRAID_ERROR_INTERRUPTS)) {
  515. pmcraid_info("IOA requires hard reset\n");
  516. pinstance->ioa_hard_reset = 1;
  517. }
  518. /* If unit check is active, trigger the dump */
  519. if (intrs & INTRS_IOA_UNIT_CHECK)
  520. pinstance->ioa_unit_check = 1;
  521. }
  522. /**
  523. * pmcraid_bist_done - completion function for PCI BIST
  524. * @cmd: pointer to reset command
  525. * Return Value
  526. * none
  527. */
  528. static void pmcraid_ioa_reset(struct pmcraid_cmd *);
  529. static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
  530. {
  531. struct pmcraid_instance *pinstance = cmd->drv_inst;
  532. unsigned long lock_flags;
  533. int rc;
  534. u16 pci_reg;
  535. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  536. /* If PCI config space can't be accessed wait for another two secs */
  537. if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
  538. cmd->time_left > 0) {
  539. pmcraid_info("BIST not complete, waiting another 2 secs\n");
  540. cmd->timer.expires = jiffies + cmd->time_left;
  541. cmd->time_left = 0;
  542. cmd->timer.data = (unsigned long)cmd;
  543. cmd->timer.function =
  544. (void (*)(unsigned long))pmcraid_bist_done;
  545. add_timer(&cmd->timer);
  546. } else {
  547. cmd->time_left = 0;
  548. pmcraid_info("BIST is complete, proceeding with reset\n");
  549. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  550. pmcraid_ioa_reset(cmd);
  551. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  552. }
  553. }
  554. /**
  555. * pmcraid_start_bist - starts BIST
  556. * @cmd: pointer to reset cmd
  557. * Return Value
  558. * none
  559. */
  560. static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
  561. {
  562. struct pmcraid_instance *pinstance = cmd->drv_inst;
  563. u32 doorbells, intrs;
  564. /* proceed with bist and wait for 2 seconds */
  565. iowrite32(DOORBELL_IOA_START_BIST,
  566. pinstance->int_regs.host_ioa_interrupt_reg);
  567. doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  568. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  569. pmcraid_info("doorbells after start bist: %x intrs: %x\n",
  570. doorbells, intrs);
  571. cmd->time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  572. cmd->timer.data = (unsigned long)cmd;
  573. cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  574. cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
  575. add_timer(&cmd->timer);
  576. }
  577. /**
  578. * pmcraid_reset_alert_done - completion routine for reset_alert
  579. * @cmd: pointer to command block used in reset sequence
  580. * Return value
  581. * None
  582. */
  583. static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
  584. {
  585. struct pmcraid_instance *pinstance = cmd->drv_inst;
  586. u32 status = ioread32(pinstance->ioa_status);
  587. unsigned long lock_flags;
  588. /* if the critical operation in progress bit is set or the wait times
  589. * out, invoke reset engine to proceed with hard reset. If there is
  590. * some more time to wait, restart the timer
  591. */
  592. if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
  593. cmd->time_left <= 0) {
  594. pmcraid_info("critical op is reset proceeding with reset\n");
  595. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  596. pmcraid_ioa_reset(cmd);
  597. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  598. } else {
  599. pmcraid_info("critical op is not yet reset waiting again\n");
  600. /* restart timer if some more time is available to wait */
  601. cmd->time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  602. cmd->timer.data = (unsigned long)cmd;
  603. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  604. cmd->timer.function =
  605. (void (*)(unsigned long))pmcraid_reset_alert_done;
  606. add_timer(&cmd->timer);
  607. }
  608. }
  609. /**
  610. * pmcraid_reset_alert - alerts IOA for a possible reset
  611. * @cmd : command block to be used for reset sequence.
  612. *
  613. * Return Value
  614. * returns 0 if pci config-space is accessible and RESET_DOORBELL is
  615. * successfully written to IOA. Returns non-zero in case pci_config_space
  616. * is not accessible
  617. */
  618. static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
  619. static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
  620. {
  621. struct pmcraid_instance *pinstance = cmd->drv_inst;
  622. u32 doorbells;
  623. int rc;
  624. u16 pci_reg;
  625. /* If we are able to access IOA PCI config space, alert IOA that we are
  626. * going to reset it soon. This enables IOA to preserv persistent error
  627. * data if any. In case memory space is not accessible, proceed with
  628. * BIST or slot_reset
  629. */
  630. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  631. if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
  632. /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
  633. * reset IOA doesn't generate any interrupts when CRITICAL
  634. * OPERATION bit is reset. A timer is started to wait for this
  635. * bit to be reset.
  636. */
  637. cmd->time_left = PMCRAID_RESET_TIMEOUT;
  638. cmd->timer.data = (unsigned long)cmd;
  639. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  640. cmd->timer.function =
  641. (void (*)(unsigned long))pmcraid_reset_alert_done;
  642. add_timer(&cmd->timer);
  643. iowrite32(DOORBELL_IOA_RESET_ALERT,
  644. pinstance->int_regs.host_ioa_interrupt_reg);
  645. doorbells =
  646. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  647. pmcraid_info("doorbells after reset alert: %x\n", doorbells);
  648. } else {
  649. pmcraid_info("PCI config is not accessible starting BIST\n");
  650. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  651. pmcraid_start_bist(cmd);
  652. }
  653. }
  654. /**
  655. * pmcraid_timeout_handler - Timeout handler for internally generated ops
  656. *
  657. * @cmd : pointer to command structure, that got timedout
  658. *
  659. * This function blocks host requests and initiates an adapter reset.
  660. *
  661. * Return value:
  662. * None
  663. */
  664. static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
  665. {
  666. struct pmcraid_instance *pinstance = cmd->drv_inst;
  667. unsigned long lock_flags;
  668. dev_info(&pinstance->pdev->dev,
  669. "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
  670. cmd->ioa_cb->ioarcb.cdb[0]);
  671. /* Command timeouts result in hard reset sequence. The command that got
  672. * timed out may be the one used as part of reset sequence. In this
  673. * case restart reset sequence using the same command block even if
  674. * reset is in progress. Otherwise fail this command and get a free
  675. * command block to restart the reset sequence.
  676. */
  677. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  678. if (!pinstance->ioa_reset_in_progress) {
  679. pinstance->ioa_reset_attempts = 0;
  680. cmd = pmcraid_get_free_cmd(pinstance);
  681. /* If we are out of command blocks, just return here itself.
  682. * Some other command's timeout handler can do the reset job
  683. */
  684. if (cmd == NULL) {
  685. spin_unlock_irqrestore(pinstance->host->host_lock,
  686. lock_flags);
  687. pmcraid_err("no free cmnd block for timeout handler\n");
  688. return;
  689. }
  690. pinstance->reset_cmd = cmd;
  691. pinstance->ioa_reset_in_progress = 1;
  692. } else {
  693. pmcraid_info("reset is already in progress\n");
  694. if (pinstance->reset_cmd != cmd) {
  695. /* This command should have been given to IOA, this
  696. * command will be completed by fail_outstanding_cmds
  697. * anyway
  698. */
  699. pmcraid_err("cmd is pending but reset in progress\n");
  700. }
  701. /* If this command was being used as part of the reset
  702. * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
  703. * causes fail_outstanding_commands not to return the command
  704. * block back to free pool
  705. */
  706. if (cmd == pinstance->reset_cmd)
  707. cmd->cmd_done = pmcraid_ioa_reset;
  708. }
  709. /* Notify apps of important IOA bringup/bringdown sequences */
  710. if (pinstance->scn.ioa_state != PMC_DEVICE_EVENT_RESET_START &&
  711. pinstance->scn.ioa_state != PMC_DEVICE_EVENT_SHUTDOWN_START)
  712. pmcraid_notify_ioastate(pinstance,
  713. PMC_DEVICE_EVENT_RESET_START);
  714. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  715. scsi_block_requests(pinstance->host);
  716. pmcraid_reset_alert(cmd);
  717. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  718. }
  719. /**
  720. * pmcraid_internal_done - completion routine for internally generated cmds
  721. *
  722. * @cmd: command that got response from IOA
  723. *
  724. * Return Value:
  725. * none
  726. */
  727. static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
  728. {
  729. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  730. cmd->ioa_cb->ioarcb.cdb[0],
  731. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  732. /* Some of the internal commands are sent with callers blocking for the
  733. * response. Same will be indicated as part of cmd->completion_req
  734. * field. Response path needs to wake up any waiters waiting for cmd
  735. * completion if this flag is set.
  736. */
  737. if (cmd->completion_req) {
  738. cmd->completion_req = 0;
  739. complete(&cmd->wait_for_completion);
  740. }
  741. /* most of the internal commands are completed by caller itself, so
  742. * no need to return the command block back to free pool until we are
  743. * required to do so (e.g once done with initialization).
  744. */
  745. if (cmd->release) {
  746. cmd->release = 0;
  747. pmcraid_return_cmd(cmd);
  748. }
  749. }
  750. /**
  751. * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
  752. *
  753. * @cmd: command that got response from IOA
  754. *
  755. * This routine is called after driver re-reads configuration table due to a
  756. * lost CCN. It returns the command block back to free pool and schedules
  757. * worker thread to add/delete devices into the system.
  758. *
  759. * Return Value:
  760. * none
  761. */
  762. static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
  763. {
  764. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  765. cmd->ioa_cb->ioarcb.cdb[0],
  766. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  767. if (cmd->release) {
  768. cmd->release = 0;
  769. pmcraid_return_cmd(cmd);
  770. }
  771. pmcraid_info("scheduling worker for config table reinitialization\n");
  772. schedule_work(&cmd->drv_inst->worker_q);
  773. }
  774. /**
  775. * pmcraid_erp_done - Process completion of SCSI error response from device
  776. * @cmd: pmcraid_command
  777. *
  778. * This function copies the sense buffer into the scsi_cmd struct and completes
  779. * scsi_cmd by calling scsi_done function.
  780. *
  781. * Return value:
  782. * none
  783. */
  784. static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
  785. {
  786. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  787. struct pmcraid_instance *pinstance = cmd->drv_inst;
  788. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  789. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
  790. scsi_cmd->result |= (DID_ERROR << 16);
  791. scmd_printk(KERN_INFO, scsi_cmd,
  792. "command CDB[0] = %x failed with IOASC: 0x%08X\n",
  793. cmd->ioa_cb->ioarcb.cdb[0], ioasc);
  794. }
  795. /* if we had allocated sense buffers for request sense, copy the sense
  796. * release the buffers
  797. */
  798. if (cmd->sense_buffer != NULL) {
  799. memcpy(scsi_cmd->sense_buffer,
  800. cmd->sense_buffer,
  801. SCSI_SENSE_BUFFERSIZE);
  802. pci_free_consistent(pinstance->pdev,
  803. SCSI_SENSE_BUFFERSIZE,
  804. cmd->sense_buffer, cmd->sense_buffer_dma);
  805. cmd->sense_buffer = NULL;
  806. cmd->sense_buffer_dma = 0;
  807. }
  808. scsi_dma_unmap(scsi_cmd);
  809. pmcraid_return_cmd(cmd);
  810. scsi_cmd->scsi_done(scsi_cmd);
  811. }
  812. /**
  813. * pmcraid_fire_command - sends an IOA command to adapter
  814. *
  815. * This function adds the given block into pending command list
  816. * and returns without waiting
  817. *
  818. * @cmd : command to be sent to the device
  819. *
  820. * Return Value
  821. * None
  822. */
  823. static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
  824. {
  825. struct pmcraid_instance *pinstance = cmd->drv_inst;
  826. unsigned long lock_flags;
  827. /* Add this command block to pending cmd pool. We do this prior to
  828. * writting IOARCB to ioarrin because IOA might complete the command
  829. * by the time we are about to add it to the list. Response handler
  830. * (isr/tasklet) looks for cmd block in the pending pending list.
  831. */
  832. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  833. list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
  834. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  835. atomic_inc(&pinstance->outstanding_cmds);
  836. /* driver writes lower 32-bit value of IOARCB address only */
  837. mb();
  838. iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
  839. pinstance->ioarrin);
  840. }
  841. /**
  842. * pmcraid_send_cmd - fires a command to IOA
  843. *
  844. * This function also sets up timeout function, and command completion
  845. * function
  846. *
  847. * @cmd: pointer to the command block to be fired to IOA
  848. * @cmd_done: command completion function, called once IOA responds
  849. * @timeout: timeout to wait for this command completion
  850. * @timeout_func: timeout handler
  851. *
  852. * Return value
  853. * none
  854. */
  855. static void pmcraid_send_cmd(
  856. struct pmcraid_cmd *cmd,
  857. void (*cmd_done) (struct pmcraid_cmd *),
  858. unsigned long timeout,
  859. void (*timeout_func) (struct pmcraid_cmd *)
  860. )
  861. {
  862. /* initialize done function */
  863. cmd->cmd_done = cmd_done;
  864. if (timeout_func) {
  865. /* setup timeout handler */
  866. cmd->timer.data = (unsigned long)cmd;
  867. cmd->timer.expires = jiffies + timeout;
  868. cmd->timer.function = (void (*)(unsigned long))timeout_func;
  869. add_timer(&cmd->timer);
  870. }
  871. /* fire the command to IOA */
  872. _pmcraid_fire_command(cmd);
  873. }
  874. /**
  875. * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
  876. * @cmd: pointer to the command block used for sending IOA shutdown command
  877. *
  878. * Return value
  879. * None
  880. */
  881. static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd *cmd)
  882. {
  883. struct pmcraid_instance *pinstance = cmd->drv_inst;
  884. unsigned long lock_flags;
  885. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  886. pmcraid_ioa_reset(cmd);
  887. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  888. }
  889. /**
  890. * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
  891. *
  892. * @cmd: pointer to the command block used as part of reset sequence
  893. *
  894. * Return Value
  895. * None
  896. */
  897. static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
  898. {
  899. pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
  900. cmd->ioa_cb->ioarcb.cdb[0],
  901. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  902. /* Note that commands sent during reset require next command to be sent
  903. * to IOA. Hence reinit the done function as well as timeout function
  904. */
  905. pmcraid_reinit_cmdblk(cmd);
  906. cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
  907. cmd->ioa_cb->ioarcb.resource_handle =
  908. cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  909. cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
  910. cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
  911. /* fire shutdown command to hardware. */
  912. pmcraid_info("firing normal shutdown command (%d) to IOA\n",
  913. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
  914. pmcraid_notify_ioastate(cmd->drv_inst, PMC_DEVICE_EVENT_SHUTDOWN_START);
  915. pmcraid_send_cmd(cmd, pmcraid_ioa_shutdown_done,
  916. PMCRAID_SHUTDOWN_TIMEOUT,
  917. pmcraid_timeout_handler);
  918. }
  919. /**
  920. * pmcraid_get_fwversion_done - completion function for get_fwversion
  921. *
  922. * @cmd: pointer to command block used to send INQUIRY command
  923. *
  924. * Return Value
  925. * none
  926. */
  927. static void pmcraid_querycfg(struct pmcraid_cmd *);
  928. static void pmcraid_get_fwversion_done(struct pmcraid_cmd *cmd)
  929. {
  930. struct pmcraid_instance *pinstance = cmd->drv_inst;
  931. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  932. unsigned long lock_flags;
  933. /* configuration table entry size depends on firmware version. If fw
  934. * version is not known, it is not possible to interpret IOA config
  935. * table
  936. */
  937. if (ioasc) {
  938. pmcraid_err("IOA Inquiry failed with %x\n", ioasc);
  939. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  940. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  941. pmcraid_reset_alert(cmd);
  942. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  943. } else {
  944. pmcraid_querycfg(cmd);
  945. }
  946. }
  947. /**
  948. * pmcraid_get_fwversion - reads firmware version information
  949. *
  950. * @cmd: pointer to command block used to send INQUIRY command
  951. *
  952. * Return Value
  953. * none
  954. */
  955. static void pmcraid_get_fwversion(struct pmcraid_cmd *cmd)
  956. {
  957. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  958. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  959. struct pmcraid_instance *pinstance = cmd->drv_inst;
  960. u16 data_size = sizeof(struct pmcraid_inquiry_data);
  961. pmcraid_reinit_cmdblk(cmd);
  962. ioarcb->request_type = REQ_TYPE_SCSI;
  963. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  964. ioarcb->cdb[0] = INQUIRY;
  965. ioarcb->cdb[1] = 1;
  966. ioarcb->cdb[2] = 0xD0;
  967. ioarcb->cdb[3] = (data_size >> 8) & 0xFF;
  968. ioarcb->cdb[4] = data_size & 0xFF;
  969. /* Since entire inquiry data it can be part of IOARCB itself
  970. */
  971. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  972. offsetof(struct pmcraid_ioarcb,
  973. add_data.u.ioadl[0]));
  974. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  975. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  976. ioarcb->request_flags0 |= NO_LINK_DESCS;
  977. ioarcb->data_transfer_length = cpu_to_le32(data_size);
  978. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  979. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  980. ioadl->address = cpu_to_le64(pinstance->inq_data_baddr);
  981. ioadl->data_len = cpu_to_le32(data_size);
  982. pmcraid_send_cmd(cmd, pmcraid_get_fwversion_done,
  983. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  984. }
  985. /**
  986. * pmcraid_identify_hrrq - registers host rrq buffers with IOA
  987. * @cmd: pointer to command block to be used for identify hrrq
  988. *
  989. * Return Value
  990. * none
  991. */
  992. static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
  993. {
  994. struct pmcraid_instance *pinstance = cmd->drv_inst;
  995. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  996. int index = cmd->hrrq_index;
  997. __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
  998. u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
  999. void (*done_function)(struct pmcraid_cmd *);
  1000. pmcraid_reinit_cmdblk(cmd);
  1001. cmd->hrrq_index = index + 1;
  1002. if (cmd->hrrq_index < pinstance->num_hrrq) {
  1003. done_function = pmcraid_identify_hrrq;
  1004. } else {
  1005. cmd->hrrq_index = 0;
  1006. done_function = pmcraid_get_fwversion;
  1007. }
  1008. /* Initialize ioarcb */
  1009. ioarcb->request_type = REQ_TYPE_IOACMD;
  1010. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1011. /* initialize the hrrq number where IOA will respond to this command */
  1012. ioarcb->hrrq_id = index;
  1013. ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
  1014. ioarcb->cdb[1] = index;
  1015. /* IOA expects 64-bit pci address to be written in B.E format
  1016. * (i.e cdb[2]=MSByte..cdb[9]=LSB.
  1017. */
  1018. pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
  1019. hrrq_addr, ioarcb->ioarcb_bus_addr, index);
  1020. memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
  1021. memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
  1022. /* Subsequent commands require HRRQ identification to be successful.
  1023. * Note that this gets called even during reset from SCSI mid-layer
  1024. * or tasklet
  1025. */
  1026. pmcraid_send_cmd(cmd, done_function,
  1027. PMCRAID_INTERNAL_TIMEOUT,
  1028. pmcraid_timeout_handler);
  1029. }
  1030. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
  1031. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
  1032. /**
  1033. * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
  1034. *
  1035. * @cmd: initialized command block pointer
  1036. *
  1037. * Return Value
  1038. * none
  1039. */
  1040. static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
  1041. {
  1042. if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
  1043. atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
  1044. else
  1045. atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
  1046. pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
  1047. }
  1048. /**
  1049. * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
  1050. *
  1051. * @pinstance: pointer to adapter instance structure
  1052. * @type: HCAM type
  1053. *
  1054. * Return Value
  1055. * pointer to initialized pmcraid_cmd structure or NULL
  1056. */
  1057. static struct pmcraid_cmd *pmcraid_init_hcam
  1058. (
  1059. struct pmcraid_instance *pinstance,
  1060. u8 type
  1061. )
  1062. {
  1063. struct pmcraid_cmd *cmd;
  1064. struct pmcraid_ioarcb *ioarcb;
  1065. struct pmcraid_ioadl_desc *ioadl;
  1066. struct pmcraid_hostrcb *hcam;
  1067. void (*cmd_done) (struct pmcraid_cmd *);
  1068. dma_addr_t dma;
  1069. int rcb_size;
  1070. cmd = pmcraid_get_free_cmd(pinstance);
  1071. if (!cmd) {
  1072. pmcraid_err("no free command blocks for hcam\n");
  1073. return cmd;
  1074. }
  1075. if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
  1076. rcb_size = sizeof(struct pmcraid_hcam_ccn_ext);
  1077. cmd_done = pmcraid_process_ccn;
  1078. dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
  1079. hcam = &pinstance->ccn;
  1080. } else {
  1081. rcb_size = sizeof(struct pmcraid_hcam_ldn);
  1082. cmd_done = pmcraid_process_ldn;
  1083. dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
  1084. hcam = &pinstance->ldn;
  1085. }
  1086. /* initialize command pointer used for HCAM registration */
  1087. hcam->cmd = cmd;
  1088. ioarcb = &cmd->ioa_cb->ioarcb;
  1089. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  1090. offsetof(struct pmcraid_ioarcb,
  1091. add_data.u.ioadl[0]));
  1092. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  1093. ioadl = ioarcb->add_data.u.ioadl;
  1094. /* Initialize ioarcb */
  1095. ioarcb->request_type = REQ_TYPE_HCAM;
  1096. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1097. ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
  1098. ioarcb->cdb[1] = type;
  1099. ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
  1100. ioarcb->cdb[8] = (rcb_size) & 0xFF;
  1101. ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
  1102. ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
  1103. ioadl[0].data_len = cpu_to_le32(rcb_size);
  1104. ioadl[0].address = cpu_to_le32(dma);
  1105. cmd->cmd_done = cmd_done;
  1106. return cmd;
  1107. }
  1108. /**
  1109. * pmcraid_send_hcam - Send an HCAM to IOA
  1110. * @pinstance: ioa config struct
  1111. * @type: HCAM type
  1112. *
  1113. * This function will send a Host Controlled Async command to IOA.
  1114. *
  1115. * Return value:
  1116. * none
  1117. */
  1118. static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
  1119. {
  1120. struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
  1121. pmcraid_send_hcam_cmd(cmd);
  1122. }
  1123. /**
  1124. * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
  1125. *
  1126. * @cmd: pointer to cmd that is used as cancelling command
  1127. * @cmd_to_cancel: pointer to the command that needs to be cancelled
  1128. */
  1129. static void pmcraid_prepare_cancel_cmd(
  1130. struct pmcraid_cmd *cmd,
  1131. struct pmcraid_cmd *cmd_to_cancel
  1132. )
  1133. {
  1134. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  1135. __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
  1136. /* Get the resource handle to where the command to be aborted has been
  1137. * sent.
  1138. */
  1139. ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
  1140. ioarcb->request_type = REQ_TYPE_IOACMD;
  1141. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  1142. ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
  1143. /* IOARCB address of the command to be cancelled is given in
  1144. * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
  1145. * IOARCB address are not masked.
  1146. */
  1147. ioarcb_addr = cpu_to_be64(ioarcb_addr);
  1148. memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
  1149. }
  1150. /**
  1151. * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
  1152. *
  1153. * @cmd: command to be used as cancelling command
  1154. * @type: HCAM type
  1155. * @cmd_done: op done function for the cancelling command
  1156. */
  1157. static void pmcraid_cancel_hcam(
  1158. struct pmcraid_cmd *cmd,
  1159. u8 type,
  1160. void (*cmd_done) (struct pmcraid_cmd *)
  1161. )
  1162. {
  1163. struct pmcraid_instance *pinstance;
  1164. struct pmcraid_hostrcb *hcam;
  1165. pinstance = cmd->drv_inst;
  1166. hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
  1167. &pinstance->ldn : &pinstance->ccn;
  1168. /* prepare for cancelling previous hcam command. If the HCAM is
  1169. * currently not pending with IOA, we would have hcam->cmd as non-null
  1170. */
  1171. if (hcam->cmd == NULL)
  1172. return;
  1173. pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
  1174. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  1175. * schedule queuecommand while we are doing this
  1176. */
  1177. pmcraid_send_cmd(cmd, cmd_done,
  1178. PMCRAID_INTERNAL_TIMEOUT,
  1179. pmcraid_timeout_handler);
  1180. }
  1181. /**
  1182. * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
  1183. *
  1184. * @cmd: command block to be used for cancelling the HCAM
  1185. */
  1186. static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
  1187. {
  1188. pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
  1189. cmd->ioa_cb->ioarcb.cdb[0],
  1190. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  1191. pmcraid_reinit_cmdblk(cmd);
  1192. pmcraid_cancel_hcam(cmd,
  1193. PMCRAID_HCAM_CODE_CONFIG_CHANGE,
  1194. pmcraid_ioa_shutdown);
  1195. }
  1196. /**
  1197. * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
  1198. *
  1199. * @cmd: command block to be used for cancelling the HCAM
  1200. */
  1201. static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
  1202. {
  1203. pmcraid_cancel_hcam(cmd,
  1204. PMCRAID_HCAM_CODE_LOG_DATA,
  1205. pmcraid_cancel_ccn);
  1206. }
  1207. /**
  1208. * pmcraid_expose_resource - check if the resource can be exposed to OS
  1209. *
  1210. * @fw_version: firmware version code
  1211. * @cfgte: pointer to configuration table entry of the resource
  1212. *
  1213. * Return value:
  1214. * true if resource can be added to midlayer, false(0) otherwise
  1215. */
  1216. static int pmcraid_expose_resource(u16 fw_version,
  1217. struct pmcraid_config_table_entry *cfgte)
  1218. {
  1219. int retval = 0;
  1220. if (cfgte->resource_type == RES_TYPE_VSET) {
  1221. if (fw_version <= PMCRAID_FW_VERSION_1)
  1222. retval = ((cfgte->unique_flags1 & 0x80) == 0);
  1223. else
  1224. retval = ((cfgte->unique_flags0 & 0x80) == 0 &&
  1225. (cfgte->unique_flags1 & 0x80) == 0);
  1226. } else if (cfgte->resource_type == RES_TYPE_GSCSI)
  1227. retval = (RES_BUS(cfgte->resource_address) !=
  1228. PMCRAID_VIRTUAL_ENCL_BUS_ID);
  1229. return retval;
  1230. }
  1231. /* attributes supported by pmcraid_event_family */
  1232. enum {
  1233. PMCRAID_AEN_ATTR_UNSPEC,
  1234. PMCRAID_AEN_ATTR_EVENT,
  1235. __PMCRAID_AEN_ATTR_MAX,
  1236. };
  1237. #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
  1238. /* commands supported by pmcraid_event_family */
  1239. enum {
  1240. PMCRAID_AEN_CMD_UNSPEC,
  1241. PMCRAID_AEN_CMD_EVENT,
  1242. __PMCRAID_AEN_CMD_MAX,
  1243. };
  1244. #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
  1245. static struct genl_family pmcraid_event_family = {
  1246. .id = GENL_ID_GENERATE,
  1247. .name = "pmcraid",
  1248. .version = 1,
  1249. .maxattr = PMCRAID_AEN_ATTR_MAX
  1250. };
  1251. /**
  1252. * pmcraid_netlink_init - registers pmcraid_event_family
  1253. *
  1254. * Return value:
  1255. * 0 if the pmcraid_event_family is successfully registered
  1256. * with netlink generic, non-zero otherwise
  1257. */
  1258. static int pmcraid_netlink_init(void)
  1259. {
  1260. int result;
  1261. result = genl_register_family(&pmcraid_event_family);
  1262. if (result)
  1263. return result;
  1264. pmcraid_info("registered NETLINK GENERIC group: %d\n",
  1265. pmcraid_event_family.id);
  1266. return result;
  1267. }
  1268. /**
  1269. * pmcraid_netlink_release - unregisters pmcraid_event_family
  1270. *
  1271. * Return value:
  1272. * none
  1273. */
  1274. static void pmcraid_netlink_release(void)
  1275. {
  1276. genl_unregister_family(&pmcraid_event_family);
  1277. }
  1278. /**
  1279. * pmcraid_notify_aen - sends event msg to user space application
  1280. * @pinstance: pointer to adapter instance structure
  1281. * @type: HCAM type
  1282. *
  1283. * Return value:
  1284. * 0 if success, error value in case of any failure.
  1285. */
  1286. static int pmcraid_notify_aen(
  1287. struct pmcraid_instance *pinstance,
  1288. struct pmcraid_aen_msg *aen_msg,
  1289. u32 data_size
  1290. )
  1291. {
  1292. struct sk_buff *skb;
  1293. void *msg_header;
  1294. u32 total_size, nla_genl_hdr_total_size;
  1295. int result;
  1296. aen_msg->hostno = (pinstance->host->unique_id << 16 |
  1297. MINOR(pinstance->cdev.dev));
  1298. aen_msg->length = data_size;
  1299. data_size += sizeof(*aen_msg);
  1300. total_size = nla_total_size(data_size);
  1301. /* Add GENL_HDR to total_size */
  1302. nla_genl_hdr_total_size =
  1303. (total_size + (GENL_HDRLEN +
  1304. ((struct genl_family *)&pmcraid_event_family)->hdrsize)
  1305. + NLMSG_HDRLEN);
  1306. skb = genlmsg_new(nla_genl_hdr_total_size, GFP_ATOMIC);
  1307. if (!skb) {
  1308. pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
  1309. total_size);
  1310. return -ENOMEM;
  1311. }
  1312. /* add the genetlink message header */
  1313. msg_header = genlmsg_put(skb, 0, 0,
  1314. &pmcraid_event_family, 0,
  1315. PMCRAID_AEN_CMD_EVENT);
  1316. if (!msg_header) {
  1317. pmcraid_err("failed to copy command details\n");
  1318. nlmsg_free(skb);
  1319. return -ENOMEM;
  1320. }
  1321. result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
  1322. if (result) {
  1323. pmcraid_err("failed to copy AEN attribute data\n");
  1324. nlmsg_free(skb);
  1325. return -EINVAL;
  1326. }
  1327. /* send genetlink multicast message to notify appplications */
  1328. result = genlmsg_end(skb, msg_header);
  1329. if (result < 0) {
  1330. pmcraid_err("genlmsg_end failed\n");
  1331. nlmsg_free(skb);
  1332. return result;
  1333. }
  1334. result =
  1335. genlmsg_multicast(skb, 0, pmcraid_event_family.id, GFP_ATOMIC);
  1336. /* If there are no listeners, genlmsg_multicast may return non-zero
  1337. * value.
  1338. */
  1339. if (result)
  1340. pmcraid_info("error (%x) sending aen event message\n", result);
  1341. return result;
  1342. }
  1343. /**
  1344. * pmcraid_notify_ccn - notifies about CCN event msg to user space
  1345. * @pinstance: pointer adapter instance structure
  1346. *
  1347. * Return value:
  1348. * 0 if success, error value in case of any failure
  1349. */
  1350. static int pmcraid_notify_ccn(struct pmcraid_instance *pinstance)
  1351. {
  1352. return pmcraid_notify_aen(pinstance,
  1353. pinstance->ccn.msg,
  1354. pinstance->ccn.hcam->data_len +
  1355. sizeof(struct pmcraid_hcam_hdr));
  1356. }
  1357. /**
  1358. * pmcraid_notify_ldn - notifies about CCN event msg to user space
  1359. * @pinstance: pointer adapter instance structure
  1360. *
  1361. * Return value:
  1362. * 0 if success, error value in case of any failure
  1363. */
  1364. static int pmcraid_notify_ldn(struct pmcraid_instance *pinstance)
  1365. {
  1366. return pmcraid_notify_aen(pinstance,
  1367. pinstance->ldn.msg,
  1368. pinstance->ldn.hcam->data_len +
  1369. sizeof(struct pmcraid_hcam_hdr));
  1370. }
  1371. /**
  1372. * pmcraid_notify_ioastate - sends IOA state event msg to user space
  1373. * @pinstance: pointer adapter instance structure
  1374. * @evt: controller state event to be sent
  1375. *
  1376. * Return value:
  1377. * 0 if success, error value in case of any failure
  1378. */
  1379. static void pmcraid_notify_ioastate(struct pmcraid_instance *pinstance, u32 evt)
  1380. {
  1381. pinstance->scn.ioa_state = evt;
  1382. pmcraid_notify_aen(pinstance,
  1383. &pinstance->scn.msg,
  1384. sizeof(u32));
  1385. }
  1386. /**
  1387. * pmcraid_handle_config_change - Handle a config change from the adapter
  1388. * @pinstance: pointer to per adapter instance structure
  1389. *
  1390. * Return value:
  1391. * none
  1392. */
  1393. static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
  1394. {
  1395. struct pmcraid_config_table_entry *cfg_entry;
  1396. struct pmcraid_hcam_ccn *ccn_hcam;
  1397. struct pmcraid_cmd *cmd;
  1398. struct pmcraid_cmd *cfgcmd;
  1399. struct pmcraid_resource_entry *res = NULL;
  1400. unsigned long lock_flags;
  1401. unsigned long host_lock_flags;
  1402. u32 new_entry = 1;
  1403. u32 hidden_entry = 0;
  1404. u16 fw_version;
  1405. int rc;
  1406. ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
  1407. cfg_entry = &ccn_hcam->cfg_entry;
  1408. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  1409. pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
  1410. res: %x:%x:%x:%x\n",
  1411. pinstance->ccn.hcam->ilid,
  1412. pinstance->ccn.hcam->op_code,
  1413. ((pinstance->ccn.hcam->timestamp1) |
  1414. ((pinstance->ccn.hcam->timestamp2 & 0xffffffffLL) << 32)),
  1415. pinstance->ccn.hcam->notification_type,
  1416. pinstance->ccn.hcam->notification_lost,
  1417. pinstance->ccn.hcam->flags,
  1418. pinstance->host->unique_id,
  1419. RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
  1420. (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
  1421. RES_BUS(cfg_entry->resource_address)),
  1422. RES_IS_VSET(*cfg_entry) ?
  1423. (fw_version <= PMCRAID_FW_VERSION_1 ?
  1424. cfg_entry->unique_flags1 :
  1425. cfg_entry->array_id & 0xFF) :
  1426. RES_TARGET(cfg_entry->resource_address),
  1427. RES_LUN(cfg_entry->resource_address));
  1428. /* If this HCAM indicates a lost notification, read the config table */
  1429. if (pinstance->ccn.hcam->notification_lost) {
  1430. cfgcmd = pmcraid_get_free_cmd(pinstance);
  1431. if (cfgcmd) {
  1432. pmcraid_info("lost CCN, reading config table\b");
  1433. pinstance->reinit_cfg_table = 1;
  1434. pmcraid_querycfg(cfgcmd);
  1435. } else {
  1436. pmcraid_err("lost CCN, no free cmd for querycfg\n");
  1437. }
  1438. goto out_notify_apps;
  1439. }
  1440. /* If this resource is not going to be added to mid-layer, just notify
  1441. * applications and return. If this notification is about hiding a VSET
  1442. * resource, check if it was exposed already.
  1443. */
  1444. if (pinstance->ccn.hcam->notification_type ==
  1445. NOTIFICATION_TYPE_ENTRY_CHANGED &&
  1446. cfg_entry->resource_type == RES_TYPE_VSET) {
  1447. if (fw_version <= PMCRAID_FW_VERSION_1)
  1448. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1449. else
  1450. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1451. } else if (!pmcraid_expose_resource(fw_version, cfg_entry)) {
  1452. goto out_notify_apps;
  1453. }
  1454. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  1455. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  1456. rc = memcmp(&res->cfg_entry.resource_address,
  1457. &cfg_entry->resource_address,
  1458. sizeof(cfg_entry->resource_address));
  1459. if (!rc) {
  1460. new_entry = 0;
  1461. break;
  1462. }
  1463. }
  1464. if (new_entry) {
  1465. if (hidden_entry) {
  1466. spin_unlock_irqrestore(&pinstance->resource_lock,
  1467. lock_flags);
  1468. goto out_notify_apps;
  1469. }
  1470. /* If there are more number of resources than what driver can
  1471. * manage, do not notify the applications about the CCN. Just
  1472. * ignore this notifications and re-register the same HCAM
  1473. */
  1474. if (list_empty(&pinstance->free_res_q)) {
  1475. spin_unlock_irqrestore(&pinstance->resource_lock,
  1476. lock_flags);
  1477. pmcraid_err("too many resources attached\n");
  1478. spin_lock_irqsave(pinstance->host->host_lock,
  1479. host_lock_flags);
  1480. pmcraid_send_hcam(pinstance,
  1481. PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1482. spin_unlock_irqrestore(pinstance->host->host_lock,
  1483. host_lock_flags);
  1484. return;
  1485. }
  1486. res = list_entry(pinstance->free_res_q.next,
  1487. struct pmcraid_resource_entry, queue);
  1488. list_del(&res->queue);
  1489. res->scsi_dev = NULL;
  1490. res->reset_progress = 0;
  1491. list_add_tail(&res->queue, &pinstance->used_res_q);
  1492. }
  1493. memcpy(&res->cfg_entry, cfg_entry, pinstance->config_table_entry_size);
  1494. if (pinstance->ccn.hcam->notification_type ==
  1495. NOTIFICATION_TYPE_ENTRY_DELETED || hidden_entry) {
  1496. if (res->scsi_dev) {
  1497. if (fw_version <= PMCRAID_FW_VERSION_1)
  1498. res->cfg_entry.unique_flags1 &= 0x7F;
  1499. else
  1500. res->cfg_entry.array_id &= 0xFF;
  1501. res->change_detected = RES_CHANGE_DEL;
  1502. res->cfg_entry.resource_handle =
  1503. PMCRAID_INVALID_RES_HANDLE;
  1504. schedule_work(&pinstance->worker_q);
  1505. } else {
  1506. /* This may be one of the non-exposed resources */
  1507. list_move_tail(&res->queue, &pinstance->free_res_q);
  1508. }
  1509. } else if (!res->scsi_dev) {
  1510. res->change_detected = RES_CHANGE_ADD;
  1511. schedule_work(&pinstance->worker_q);
  1512. }
  1513. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  1514. out_notify_apps:
  1515. /* Notify configuration changes to registered applications.*/
  1516. if (!pmcraid_disable_aen)
  1517. pmcraid_notify_ccn(pinstance);
  1518. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1519. if (cmd)
  1520. pmcraid_send_hcam_cmd(cmd);
  1521. }
  1522. /**
  1523. * pmcraid_get_error_info - return error string for an ioasc
  1524. * @ioasc: ioasc code
  1525. * Return Value
  1526. * none
  1527. */
  1528. static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
  1529. {
  1530. int i;
  1531. for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
  1532. if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
  1533. return &pmcraid_ioasc_error_table[i];
  1534. }
  1535. return NULL;
  1536. }
  1537. /**
  1538. * pmcraid_ioasc_logger - log IOASC information based user-settings
  1539. * @ioasc: ioasc code
  1540. * @cmd: pointer to command that resulted in 'ioasc'
  1541. */
  1542. void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
  1543. {
  1544. struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
  1545. if (error_info == NULL ||
  1546. cmd->drv_inst->current_log_level < error_info->log_level)
  1547. return;
  1548. /* log the error string */
  1549. pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
  1550. cmd->ioa_cb->ioarcb.cdb[0],
  1551. cmd->ioa_cb->ioarcb.resource_handle,
  1552. le32_to_cpu(ioasc), error_info->error_string);
  1553. }
  1554. /**
  1555. * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
  1556. *
  1557. * @pinstance: pointer to per adapter instance structure
  1558. *
  1559. * Return value:
  1560. * none
  1561. */
  1562. static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
  1563. {
  1564. struct pmcraid_hcam_ldn *hcam_ldn;
  1565. u32 ioasc;
  1566. hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1567. pmcraid_info
  1568. ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
  1569. pinstance->ldn.hcam->ilid,
  1570. pinstance->ldn.hcam->op_code,
  1571. pinstance->ldn.hcam->notification_type,
  1572. pinstance->ldn.hcam->notification_lost,
  1573. pinstance->ldn.hcam->flags,
  1574. pinstance->ldn.hcam->overlay_id);
  1575. /* log only the errors, no need to log informational log entries */
  1576. if (pinstance->ldn.hcam->notification_type !=
  1577. NOTIFICATION_TYPE_ERROR_LOG)
  1578. return;
  1579. if (pinstance->ldn.hcam->notification_lost ==
  1580. HOSTRCB_NOTIFICATIONS_LOST)
  1581. dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
  1582. ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
  1583. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  1584. ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
  1585. dev_info(&pinstance->pdev->dev,
  1586. "UnitAttention due to IOA Bus Reset\n");
  1587. scsi_report_bus_reset(
  1588. pinstance->host,
  1589. RES_BUS(hcam_ldn->error_log.fd_ra));
  1590. }
  1591. return;
  1592. }
  1593. /**
  1594. * pmcraid_process_ccn - Op done function for a CCN.
  1595. * @cmd: pointer to command struct
  1596. *
  1597. * This function is the op done function for a configuration
  1598. * change notification
  1599. *
  1600. * Return value:
  1601. * none
  1602. */
  1603. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
  1604. {
  1605. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1606. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1607. unsigned long lock_flags;
  1608. pinstance->ccn.cmd = NULL;
  1609. pmcraid_return_cmd(cmd);
  1610. /* If driver initiated IOA reset happened while this hcam was pending
  1611. * with IOA, or IOA bringdown sequence is in progress, no need to
  1612. * re-register the hcam
  1613. */
  1614. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1615. atomic_read(&pinstance->ccn.ignore) == 1) {
  1616. return;
  1617. } else if (ioasc) {
  1618. dev_info(&pinstance->pdev->dev,
  1619. "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
  1620. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1621. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1622. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1623. } else {
  1624. pmcraid_handle_config_change(pinstance);
  1625. }
  1626. }
  1627. /**
  1628. * pmcraid_process_ldn - op done function for an LDN
  1629. * @cmd: pointer to command block
  1630. *
  1631. * Return value
  1632. * none
  1633. */
  1634. static void pmcraid_initiate_reset(struct pmcraid_instance *);
  1635. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd);
  1636. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
  1637. {
  1638. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1639. struct pmcraid_hcam_ldn *ldn_hcam =
  1640. (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1641. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1642. u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
  1643. unsigned long lock_flags;
  1644. /* return the command block back to freepool */
  1645. pinstance->ldn.cmd = NULL;
  1646. pmcraid_return_cmd(cmd);
  1647. /* If driver initiated IOA reset happened while this hcam was pending
  1648. * with IOA, no need to re-register the hcam as reset engine will do it
  1649. * once reset sequence is complete
  1650. */
  1651. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1652. atomic_read(&pinstance->ccn.ignore) == 1) {
  1653. return;
  1654. } else if (!ioasc) {
  1655. pmcraid_handle_error_log(pinstance);
  1656. if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
  1657. spin_lock_irqsave(pinstance->host->host_lock,
  1658. lock_flags);
  1659. pmcraid_initiate_reset(pinstance);
  1660. spin_unlock_irqrestore(pinstance->host->host_lock,
  1661. lock_flags);
  1662. return;
  1663. }
  1664. if (fd_ioasc == PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC) {
  1665. pinstance->timestamp_error = 1;
  1666. pmcraid_set_timestamp(cmd);
  1667. }
  1668. } else {
  1669. dev_info(&pinstance->pdev->dev,
  1670. "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
  1671. }
  1672. /* send netlink message for HCAM notification if enabled */
  1673. if (!pmcraid_disable_aen)
  1674. pmcraid_notify_ldn(pinstance);
  1675. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1676. if (cmd)
  1677. pmcraid_send_hcam_cmd(cmd);
  1678. }
  1679. /**
  1680. * pmcraid_register_hcams - register HCAMs for CCN and LDN
  1681. *
  1682. * @pinstance: pointer per adapter instance structure
  1683. *
  1684. * Return Value
  1685. * none
  1686. */
  1687. static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
  1688. {
  1689. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1690. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1691. }
  1692. /**
  1693. * pmcraid_unregister_hcams - cancel HCAMs registered already
  1694. * @cmd: pointer to command used as part of reset sequence
  1695. */
  1696. static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
  1697. {
  1698. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1699. /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
  1700. * handling hcam response though it is not necessary. In order to
  1701. * prevent this, set 'ignore', so that bring-down sequence doesn't
  1702. * re-send any more hcams
  1703. */
  1704. atomic_set(&pinstance->ccn.ignore, 1);
  1705. atomic_set(&pinstance->ldn.ignore, 1);
  1706. /* If adapter reset was forced as part of runtime reset sequence,
  1707. * start the reset sequence. Reset will be triggered even in case
  1708. * IOA unit_check.
  1709. */
  1710. if ((pinstance->force_ioa_reset && !pinstance->ioa_bringdown) ||
  1711. pinstance->ioa_unit_check) {
  1712. pinstance->force_ioa_reset = 0;
  1713. pinstance->ioa_unit_check = 0;
  1714. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1715. pmcraid_reset_alert(cmd);
  1716. return;
  1717. }
  1718. /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
  1719. * one after the other. So CCN cancellation will be triggered by
  1720. * pmcraid_cancel_ldn itself.
  1721. */
  1722. pmcraid_cancel_ldn(cmd);
  1723. }
  1724. /**
  1725. * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
  1726. * @pinstance: pointer to adapter instance structure
  1727. * Return Value
  1728. * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
  1729. */
  1730. static void pmcraid_reinit_buffers(struct pmcraid_instance *);
  1731. static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
  1732. {
  1733. u32 intrs;
  1734. pmcraid_reinit_buffers(pinstance);
  1735. intrs = pmcraid_read_interrupts(pinstance);
  1736. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  1737. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  1738. if (!pinstance->interrupt_mode) {
  1739. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1740. pinstance->int_regs.
  1741. ioa_host_interrupt_mask_reg);
  1742. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1743. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  1744. }
  1745. return 1;
  1746. } else {
  1747. return 0;
  1748. }
  1749. }
  1750. /**
  1751. * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
  1752. * @cmd : pointer to reset command block
  1753. *
  1754. * Return Value
  1755. * none
  1756. */
  1757. static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
  1758. {
  1759. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1760. u32 int_reg;
  1761. u32 doorbell;
  1762. /* There will be an interrupt when Transition to Operational bit is
  1763. * set so tasklet would execute next reset task. The timeout handler
  1764. * would re-initiate a reset
  1765. */
  1766. cmd->cmd_done = pmcraid_ioa_reset;
  1767. cmd->timer.data = (unsigned long)cmd;
  1768. cmd->timer.expires = jiffies +
  1769. msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
  1770. cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
  1771. if (!timer_pending(&cmd->timer))
  1772. add_timer(&cmd->timer);
  1773. /* Enable destructive diagnostics on IOA if it is not yet in
  1774. * operational state
  1775. */
  1776. doorbell = DOORBELL_RUNTIME_RESET |
  1777. DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
  1778. /* Since we do RESET_ALERT and Start BIST we have to again write
  1779. * MSIX Doorbell to indicate the interrupt mode
  1780. */
  1781. if (pinstance->interrupt_mode) {
  1782. iowrite32(DOORBELL_INTR_MODE_MSIX,
  1783. pinstance->int_regs.host_ioa_interrupt_reg);
  1784. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  1785. }
  1786. iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
  1787. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1788. int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  1789. pmcraid_info("Waiting for IOA to become operational %x:%x\n",
  1790. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1791. int_reg);
  1792. }
  1793. /**
  1794. * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
  1795. *
  1796. * @pinstance: pointer to adapter instance structure
  1797. *
  1798. * Return Value
  1799. * none
  1800. */
  1801. static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
  1802. {
  1803. pmcraid_info("%s is not yet implemented\n", __func__);
  1804. }
  1805. /**
  1806. * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
  1807. * @pinstance: pointer to adapter instance structure
  1808. *
  1809. * This function fails all outstanding ops. If they are submitted to IOA
  1810. * already, it sends cancel all messages if IOA is still accepting IOARCBs,
  1811. * otherwise just completes the commands and returns the cmd blocks to free
  1812. * pool.
  1813. *
  1814. * Return value:
  1815. * none
  1816. */
  1817. static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
  1818. {
  1819. struct pmcraid_cmd *cmd, *temp;
  1820. unsigned long lock_flags;
  1821. /* pending command list is protected by pending_pool_lock. Its
  1822. * traversal must be done as within this lock
  1823. */
  1824. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1825. list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
  1826. free_list) {
  1827. list_del(&cmd->free_list);
  1828. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  1829. lock_flags);
  1830. cmd->ioa_cb->ioasa.ioasc =
  1831. cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
  1832. cmd->ioa_cb->ioasa.ilid =
  1833. cpu_to_be32(PMCRAID_DRIVER_ILID);
  1834. /* In case the command timer is still running */
  1835. del_timer(&cmd->timer);
  1836. /* If this is an IO command, complete it by invoking scsi_done
  1837. * function. If this is one of the internal commands other
  1838. * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
  1839. * complete it
  1840. */
  1841. if (cmd->scsi_cmd) {
  1842. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  1843. __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
  1844. scsi_cmd->result |= DID_ERROR << 16;
  1845. scsi_dma_unmap(scsi_cmd);
  1846. pmcraid_return_cmd(cmd);
  1847. pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
  1848. le32_to_cpu(resp) >> 2,
  1849. cmd->ioa_cb->ioarcb.cdb[0],
  1850. scsi_cmd->result);
  1851. scsi_cmd->scsi_done(scsi_cmd);
  1852. } else if (cmd->cmd_done == pmcraid_internal_done ||
  1853. cmd->cmd_done == pmcraid_erp_done) {
  1854. cmd->cmd_done(cmd);
  1855. } else if (cmd->cmd_done != pmcraid_ioa_reset &&
  1856. cmd->cmd_done != pmcraid_ioa_shutdown_done) {
  1857. pmcraid_return_cmd(cmd);
  1858. }
  1859. atomic_dec(&pinstance->outstanding_cmds);
  1860. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1861. }
  1862. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  1863. }
  1864. /**
  1865. * pmcraid_ioa_reset - Implementation of IOA reset logic
  1866. *
  1867. * @cmd: pointer to the cmd block to be used for entire reset process
  1868. *
  1869. * This function executes most of the steps required for IOA reset. This gets
  1870. * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
  1871. * 'eh_' thread. Access to variables used for controling the reset sequence is
  1872. * synchronized using host lock. Various functions called during reset process
  1873. * would make use of a single command block, pointer to which is also stored in
  1874. * adapter instance structure.
  1875. *
  1876. * Return Value
  1877. * None
  1878. */
  1879. static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
  1880. {
  1881. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1882. u8 reset_complete = 0;
  1883. pinstance->ioa_reset_in_progress = 1;
  1884. if (pinstance->reset_cmd != cmd) {
  1885. pmcraid_err("reset is called with different command block\n");
  1886. pinstance->reset_cmd = cmd;
  1887. }
  1888. pmcraid_info("reset_engine: state = %d, command = %p\n",
  1889. pinstance->ioa_state, cmd);
  1890. switch (pinstance->ioa_state) {
  1891. case IOA_STATE_DEAD:
  1892. /* If IOA is offline, whatever may be the reset reason, just
  1893. * return. callers might be waiting on the reset wait_q, wake
  1894. * up them
  1895. */
  1896. pmcraid_err("IOA is offline no reset is possible\n");
  1897. reset_complete = 1;
  1898. break;
  1899. case IOA_STATE_IN_BRINGDOWN:
  1900. /* we enter here, once ioa shutdown command is processed by IOA
  1901. * Alert IOA for a possible reset. If reset alert fails, IOA
  1902. * goes through hard-reset
  1903. */
  1904. pmcraid_disable_interrupts(pinstance, ~0);
  1905. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1906. pmcraid_reset_alert(cmd);
  1907. break;
  1908. case IOA_STATE_UNKNOWN:
  1909. /* We may be called during probe or resume. Some pre-processing
  1910. * is required for prior to reset
  1911. */
  1912. scsi_block_requests(pinstance->host);
  1913. /* If asked to reset while IOA was processing responses or
  1914. * there are any error responses then IOA may require
  1915. * hard-reset.
  1916. */
  1917. if (pinstance->ioa_hard_reset == 0) {
  1918. if (ioread32(pinstance->ioa_status) &
  1919. INTRS_TRANSITION_TO_OPERATIONAL) {
  1920. pmcraid_info("sticky bit set, bring-up\n");
  1921. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1922. pmcraid_reinit_cmdblk(cmd);
  1923. pmcraid_identify_hrrq(cmd);
  1924. } else {
  1925. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1926. pmcraid_soft_reset(cmd);
  1927. }
  1928. } else {
  1929. /* Alert IOA of a possible reset and wait for critical
  1930. * operation in progress bit to reset
  1931. */
  1932. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1933. pmcraid_reset_alert(cmd);
  1934. }
  1935. break;
  1936. case IOA_STATE_IN_RESET_ALERT:
  1937. /* If critical operation in progress bit is reset or wait gets
  1938. * timed out, reset proceeds with starting BIST on the IOA.
  1939. * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
  1940. * they are 3 or more, reset engine marks IOA dead and returns
  1941. */
  1942. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  1943. pmcraid_start_bist(cmd);
  1944. break;
  1945. case IOA_STATE_IN_HARD_RESET:
  1946. pinstance->ioa_reset_attempts++;
  1947. /* retry reset if we haven't reached maximum allowed limit */
  1948. if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
  1949. pinstance->ioa_reset_attempts = 0;
  1950. pmcraid_err("IOA didn't respond marking it as dead\n");
  1951. pinstance->ioa_state = IOA_STATE_DEAD;
  1952. if (pinstance->ioa_bringdown)
  1953. pmcraid_notify_ioastate(pinstance,
  1954. PMC_DEVICE_EVENT_SHUTDOWN_FAILED);
  1955. else
  1956. pmcraid_notify_ioastate(pinstance,
  1957. PMC_DEVICE_EVENT_RESET_FAILED);
  1958. reset_complete = 1;
  1959. break;
  1960. }
  1961. /* Once either bist or pci reset is done, restore PCI config
  1962. * space. If this fails, proceed with hard reset again
  1963. */
  1964. if (pci_restore_state(pinstance->pdev)) {
  1965. pmcraid_info("config-space error resetting again\n");
  1966. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1967. pmcraid_reset_alert(cmd);
  1968. break;
  1969. }
  1970. /* fail all pending commands */
  1971. pmcraid_fail_outstanding_cmds(pinstance);
  1972. /* check if unit check is active, if so extract dump */
  1973. if (pinstance->ioa_unit_check) {
  1974. pmcraid_info("unit check is active\n");
  1975. pinstance->ioa_unit_check = 0;
  1976. pmcraid_get_dump(pinstance);
  1977. pinstance->ioa_reset_attempts--;
  1978. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1979. pmcraid_reset_alert(cmd);
  1980. break;
  1981. }
  1982. /* if the reset reason is to bring-down the ioa, we might be
  1983. * done with the reset restore pci_config_space and complete
  1984. * the reset
  1985. */
  1986. if (pinstance->ioa_bringdown) {
  1987. pmcraid_info("bringing down the adapter\n");
  1988. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1989. pinstance->ioa_bringdown = 0;
  1990. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  1991. pmcraid_notify_ioastate(pinstance,
  1992. PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS);
  1993. reset_complete = 1;
  1994. } else {
  1995. /* bring-up IOA, so proceed with soft reset
  1996. * Reinitialize hrrq_buffers and their indices also
  1997. * enable interrupts after a pci_restore_state
  1998. */
  1999. if (pmcraid_reset_enable_ioa(pinstance)) {
  2000. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  2001. pmcraid_info("bringing up the adapter\n");
  2002. pmcraid_reinit_cmdblk(cmd);
  2003. pmcraid_identify_hrrq(cmd);
  2004. } else {
  2005. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  2006. pmcraid_soft_reset(cmd);
  2007. }
  2008. }
  2009. break;
  2010. case IOA_STATE_IN_SOFT_RESET:
  2011. /* TRANSITION TO OPERATIONAL is on so start initialization
  2012. * sequence
  2013. */
  2014. pmcraid_info("In softreset proceeding with bring-up\n");
  2015. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  2016. /* Initialization commands start with HRRQ identification. From
  2017. * now on tasklet completes most of the commands as IOA is up
  2018. * and intrs are enabled
  2019. */
  2020. pmcraid_identify_hrrq(cmd);
  2021. break;
  2022. case IOA_STATE_IN_BRINGUP:
  2023. /* we are done with bringing up of IOA, change the ioa_state to
  2024. * operational and wake up any waiters
  2025. */
  2026. pinstance->ioa_state = IOA_STATE_OPERATIONAL;
  2027. reset_complete = 1;
  2028. break;
  2029. case IOA_STATE_OPERATIONAL:
  2030. default:
  2031. /* When IOA is operational and a reset is requested, check for
  2032. * the reset reason. If reset is to bring down IOA, unregister
  2033. * HCAMs and initiate shutdown; if adapter reset is forced then
  2034. * restart reset sequence again
  2035. */
  2036. if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
  2037. pinstance->force_ioa_reset == 0) {
  2038. pmcraid_notify_ioastate(pinstance,
  2039. PMC_DEVICE_EVENT_RESET_SUCCESS);
  2040. reset_complete = 1;
  2041. } else {
  2042. if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
  2043. pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
  2044. pmcraid_reinit_cmdblk(cmd);
  2045. pmcraid_unregister_hcams(cmd);
  2046. }
  2047. break;
  2048. }
  2049. /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
  2050. * OPERATIONAL. Reset all control variables used during reset, wake up
  2051. * any waiting threads and let the SCSI mid-layer send commands. Note
  2052. * that host_lock must be held before invoking scsi_report_bus_reset.
  2053. */
  2054. if (reset_complete) {
  2055. pinstance->ioa_reset_in_progress = 0;
  2056. pinstance->ioa_reset_attempts = 0;
  2057. pinstance->reset_cmd = NULL;
  2058. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2059. pinstance->ioa_bringdown = 0;
  2060. pmcraid_return_cmd(cmd);
  2061. /* If target state is to bring up the adapter, proceed with
  2062. * hcam registration and resource exposure to mid-layer.
  2063. */
  2064. if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
  2065. pmcraid_register_hcams(pinstance);
  2066. wake_up_all(&pinstance->reset_wait_q);
  2067. }
  2068. return;
  2069. }
  2070. /**
  2071. * pmcraid_initiate_reset - initiates reset sequence. This is called from
  2072. * ISR/tasklet during error interrupts including IOA unit check. If reset
  2073. * is already in progress, it just returns, otherwise initiates IOA reset
  2074. * to bring IOA up to operational state.
  2075. *
  2076. * @pinstance: pointer to adapter instance structure
  2077. *
  2078. * Return value
  2079. * none
  2080. */
  2081. static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
  2082. {
  2083. struct pmcraid_cmd *cmd;
  2084. /* If the reset is already in progress, just return, otherwise start
  2085. * reset sequence and return
  2086. */
  2087. if (!pinstance->ioa_reset_in_progress) {
  2088. scsi_block_requests(pinstance->host);
  2089. cmd = pmcraid_get_free_cmd(pinstance);
  2090. if (cmd == NULL) {
  2091. pmcraid_err("no cmnd blocks for initiate_reset\n");
  2092. return;
  2093. }
  2094. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2095. pinstance->reset_cmd = cmd;
  2096. pinstance->force_ioa_reset = 1;
  2097. pmcraid_notify_ioastate(pinstance,
  2098. PMC_DEVICE_EVENT_RESET_START);
  2099. pmcraid_ioa_reset(cmd);
  2100. }
  2101. }
  2102. /**
  2103. * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
  2104. * or bringdown IOA
  2105. * @pinstance: pointer adapter instance structure
  2106. * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
  2107. * @target_state: expected target state after reset
  2108. *
  2109. * Note: This command initiates reset and waits for its completion. Hence this
  2110. * should not be called from isr/timer/tasklet functions (timeout handlers,
  2111. * error response handlers and interrupt handlers).
  2112. *
  2113. * Return Value
  2114. * 1 in case ioa_state is not target_state, 0 otherwise.
  2115. */
  2116. static int pmcraid_reset_reload(
  2117. struct pmcraid_instance *pinstance,
  2118. u8 shutdown_type,
  2119. u8 target_state
  2120. )
  2121. {
  2122. struct pmcraid_cmd *reset_cmd = NULL;
  2123. unsigned long lock_flags;
  2124. int reset = 1;
  2125. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2126. if (pinstance->ioa_reset_in_progress) {
  2127. pmcraid_info("reset_reload: reset is already in progress\n");
  2128. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2129. wait_event(pinstance->reset_wait_q,
  2130. !pinstance->ioa_reset_in_progress);
  2131. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2132. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2133. spin_unlock_irqrestore(pinstance->host->host_lock,
  2134. lock_flags);
  2135. pmcraid_info("reset_reload: IOA is dead\n");
  2136. return reset;
  2137. } else if (pinstance->ioa_state == target_state) {
  2138. reset = 0;
  2139. }
  2140. }
  2141. if (reset) {
  2142. pmcraid_info("reset_reload: proceeding with reset\n");
  2143. scsi_block_requests(pinstance->host);
  2144. reset_cmd = pmcraid_get_free_cmd(pinstance);
  2145. if (reset_cmd == NULL) {
  2146. pmcraid_err("no free cmnd for reset_reload\n");
  2147. spin_unlock_irqrestore(pinstance->host->host_lock,
  2148. lock_flags);
  2149. return reset;
  2150. }
  2151. if (shutdown_type == SHUTDOWN_NORMAL)
  2152. pinstance->ioa_bringdown = 1;
  2153. pinstance->ioa_shutdown_type = shutdown_type;
  2154. pinstance->reset_cmd = reset_cmd;
  2155. pinstance->force_ioa_reset = reset;
  2156. pmcraid_info("reset_reload: initiating reset\n");
  2157. pmcraid_ioa_reset(reset_cmd);
  2158. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2159. pmcraid_info("reset_reload: waiting for reset to complete\n");
  2160. wait_event(pinstance->reset_wait_q,
  2161. !pinstance->ioa_reset_in_progress);
  2162. pmcraid_info("reset_reload: reset is complete !!\n");
  2163. scsi_unblock_requests(pinstance->host);
  2164. if (pinstance->ioa_state == target_state)
  2165. reset = 0;
  2166. }
  2167. return reset;
  2168. }
  2169. /**
  2170. * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
  2171. *
  2172. * @pinstance: pointer to adapter instance structure
  2173. *
  2174. * Return Value
  2175. * whatever is returned from pmcraid_reset_reload
  2176. */
  2177. static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
  2178. {
  2179. return pmcraid_reset_reload(pinstance,
  2180. SHUTDOWN_NORMAL,
  2181. IOA_STATE_UNKNOWN);
  2182. }
  2183. /**
  2184. * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
  2185. *
  2186. * @pinstance: pointer to adapter instance structure
  2187. *
  2188. * Return Value
  2189. * whatever is returned from pmcraid_reset_reload
  2190. */
  2191. static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
  2192. {
  2193. pmcraid_notify_ioastate(pinstance, PMC_DEVICE_EVENT_RESET_START);
  2194. return pmcraid_reset_reload(pinstance,
  2195. SHUTDOWN_NONE,
  2196. IOA_STATE_OPERATIONAL);
  2197. }
  2198. /**
  2199. * pmcraid_request_sense - Send request sense to a device
  2200. * @cmd: pmcraid command struct
  2201. *
  2202. * This function sends a request sense to a device as a result of a check
  2203. * condition. This method re-uses the same command block that failed earlier.
  2204. */
  2205. static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
  2206. {
  2207. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2208. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2209. /* allocate DMAable memory for sense buffers */
  2210. cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
  2211. SCSI_SENSE_BUFFERSIZE,
  2212. &cmd->sense_buffer_dma);
  2213. if (cmd->sense_buffer == NULL) {
  2214. pmcraid_err
  2215. ("couldn't allocate sense buffer for request sense\n");
  2216. pmcraid_erp_done(cmd);
  2217. return;
  2218. }
  2219. /* re-use the command block */
  2220. memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
  2221. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2222. ioarcb->request_flags0 = (SYNC_COMPLETE |
  2223. NO_LINK_DESCS |
  2224. INHIBIT_UL_CHECK);
  2225. ioarcb->request_type = REQ_TYPE_SCSI;
  2226. ioarcb->cdb[0] = REQUEST_SENSE;
  2227. ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2228. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2229. offsetof(struct pmcraid_ioarcb,
  2230. add_data.u.ioadl[0]));
  2231. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  2232. ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2233. ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
  2234. ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2235. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  2236. /* request sense might be called as part of error response processing
  2237. * which runs in tasklets context. It is possible that mid-layer might
  2238. * schedule queuecommand during this time, hence, writting to IOARRIN
  2239. * must be protect by host_lock
  2240. */
  2241. pmcraid_send_cmd(cmd, pmcraid_erp_done,
  2242. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2243. pmcraid_timeout_handler);
  2244. }
  2245. /**
  2246. * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
  2247. * @cmd: command that failed
  2248. * @sense: true if request_sense is required after cancel all
  2249. *
  2250. * This function sends a cancel all to a device to clear the queue.
  2251. */
  2252. static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
  2253. {
  2254. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2255. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2256. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2257. void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
  2258. : pmcraid_request_sense;
  2259. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2260. ioarcb->request_flags0 = SYNC_OVERRIDE;
  2261. ioarcb->request_type = REQ_TYPE_IOACMD;
  2262. ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
  2263. if (RES_IS_GSCSI(res->cfg_entry))
  2264. ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
  2265. ioarcb->ioadl_bus_addr = 0;
  2266. ioarcb->ioadl_length = 0;
  2267. ioarcb->data_transfer_length = 0;
  2268. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  2269. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  2270. * schedule queuecommand while we are doing this
  2271. */
  2272. pmcraid_send_cmd(cmd, cmd_done,
  2273. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2274. pmcraid_timeout_handler);
  2275. }
  2276. /**
  2277. * pmcraid_frame_auto_sense: frame fixed format sense information
  2278. *
  2279. * @cmd: pointer to failing command block
  2280. *
  2281. * Return value
  2282. * none
  2283. */
  2284. static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
  2285. {
  2286. u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
  2287. struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
  2288. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2289. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2290. u32 failing_lba = 0;
  2291. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  2292. cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
  2293. if (RES_IS_VSET(res->cfg_entry) &&
  2294. ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
  2295. ioasa->u.vset.failing_lba_hi != 0) {
  2296. sense_buf[0] = 0x72;
  2297. sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2298. sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2299. sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2300. sense_buf[7] = 12;
  2301. sense_buf[8] = 0;
  2302. sense_buf[9] = 0x0A;
  2303. sense_buf[10] = 0x80;
  2304. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
  2305. sense_buf[12] = (failing_lba & 0xff000000) >> 24;
  2306. sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
  2307. sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
  2308. sense_buf[15] = failing_lba & 0x000000ff;
  2309. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
  2310. sense_buf[16] = (failing_lba & 0xff000000) >> 24;
  2311. sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
  2312. sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
  2313. sense_buf[19] = failing_lba & 0x000000ff;
  2314. } else {
  2315. sense_buf[0] = 0x70;
  2316. sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2317. sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2318. sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2319. if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
  2320. if (RES_IS_VSET(res->cfg_entry))
  2321. failing_lba =
  2322. le32_to_cpu(ioasa->u.
  2323. vset.failing_lba_lo);
  2324. sense_buf[0] |= 0x80;
  2325. sense_buf[3] = (failing_lba >> 24) & 0xff;
  2326. sense_buf[4] = (failing_lba >> 16) & 0xff;
  2327. sense_buf[5] = (failing_lba >> 8) & 0xff;
  2328. sense_buf[6] = failing_lba & 0xff;
  2329. }
  2330. sense_buf[7] = 6; /* additional length */
  2331. }
  2332. }
  2333. /**
  2334. * pmcraid_error_handler - Error response handlers for a SCSI op
  2335. * @cmd: pointer to pmcraid_cmd that has failed
  2336. *
  2337. * This function determines whether or not to initiate ERP on the affected
  2338. * device. This is called from a tasklet, which doesn't hold any locks.
  2339. *
  2340. * Return value:
  2341. * 0 it caller can complete the request, otherwise 1 where in error
  2342. * handler itself completes the request and returns the command block
  2343. * back to free-pool
  2344. */
  2345. static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
  2346. {
  2347. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2348. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2349. struct pmcraid_instance *pinstance = cmd->drv_inst;
  2350. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2351. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2352. u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
  2353. u32 sense_copied = 0;
  2354. if (!res) {
  2355. pmcraid_info("resource pointer is NULL\n");
  2356. return 0;
  2357. }
  2358. /* If this was a SCSI read/write command keep count of errors */
  2359. if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
  2360. atomic_inc(&res->read_failures);
  2361. else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
  2362. atomic_inc(&res->write_failures);
  2363. if (!RES_IS_GSCSI(res->cfg_entry) &&
  2364. masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
  2365. pmcraid_frame_auto_sense(cmd);
  2366. }
  2367. /* Log IOASC/IOASA information based on user settings */
  2368. pmcraid_ioasc_logger(ioasc, cmd);
  2369. switch (masked_ioasc) {
  2370. case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
  2371. scsi_cmd->result |= (DID_ABORT << 16);
  2372. break;
  2373. case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
  2374. case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
  2375. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  2376. break;
  2377. case PMCRAID_IOASC_NR_SYNC_REQUIRED:
  2378. res->sync_reqd = 1;
  2379. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  2380. break;
  2381. case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
  2382. scsi_cmd->result |= (DID_PASSTHROUGH << 16);
  2383. break;
  2384. case PMCRAID_IOASC_UA_BUS_WAS_RESET:
  2385. case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
  2386. if (!res->reset_progress)
  2387. scsi_report_bus_reset(pinstance->host,
  2388. scsi_cmd->device->channel);
  2389. scsi_cmd->result |= (DID_ERROR << 16);
  2390. break;
  2391. case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
  2392. scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
  2393. res->sync_reqd = 1;
  2394. /* if check_condition is not active return with error otherwise
  2395. * get/frame the sense buffer
  2396. */
  2397. if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
  2398. SAM_STAT_CHECK_CONDITION &&
  2399. PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
  2400. return 0;
  2401. /* If we have auto sense data as part of IOASA pass it to
  2402. * mid-layer
  2403. */
  2404. if (ioasa->auto_sense_length != 0) {
  2405. short sense_len = ioasa->auto_sense_length;
  2406. int data_size = min_t(u16, le16_to_cpu(sense_len),
  2407. SCSI_SENSE_BUFFERSIZE);
  2408. memcpy(scsi_cmd->sense_buffer,
  2409. ioasa->sense_data,
  2410. data_size);
  2411. sense_copied = 1;
  2412. }
  2413. if (RES_IS_GSCSI(res->cfg_entry))
  2414. pmcraid_cancel_all(cmd, sense_copied);
  2415. else if (sense_copied)
  2416. pmcraid_erp_done(cmd);
  2417. else
  2418. pmcraid_request_sense(cmd);
  2419. return 1;
  2420. case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
  2421. break;
  2422. default:
  2423. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  2424. scsi_cmd->result |= (DID_ERROR << 16);
  2425. break;
  2426. }
  2427. return 0;
  2428. }
  2429. /**
  2430. * pmcraid_reset_device - device reset handler functions
  2431. *
  2432. * @scsi_cmd: scsi command struct
  2433. * @modifier: reset modifier indicating the reset sequence to be performed
  2434. *
  2435. * This function issues a device reset to the affected device.
  2436. * A LUN reset will be sent to the device first. If that does
  2437. * not work, a target reset will be sent.
  2438. *
  2439. * Return value:
  2440. * SUCCESS / FAILED
  2441. */
  2442. static int pmcraid_reset_device(
  2443. struct scsi_cmnd *scsi_cmd,
  2444. unsigned long timeout,
  2445. u8 modifier
  2446. )
  2447. {
  2448. struct pmcraid_cmd *cmd;
  2449. struct pmcraid_instance *pinstance;
  2450. struct pmcraid_resource_entry *res;
  2451. struct pmcraid_ioarcb *ioarcb;
  2452. unsigned long lock_flags;
  2453. u32 ioasc;
  2454. pinstance =
  2455. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2456. res = scsi_cmd->device->hostdata;
  2457. if (!res) {
  2458. sdev_printk(KERN_ERR, scsi_cmd->device,
  2459. "reset_device: NULL resource pointer\n");
  2460. return FAILED;
  2461. }
  2462. /* If adapter is currently going through reset/reload, return failed.
  2463. * This will force the mid-layer to call _eh_bus/host reset, which
  2464. * will then go to sleep and wait for the reset to complete
  2465. */
  2466. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2467. if (pinstance->ioa_reset_in_progress ||
  2468. pinstance->ioa_state == IOA_STATE_DEAD) {
  2469. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2470. return FAILED;
  2471. }
  2472. res->reset_progress = 1;
  2473. pmcraid_info("Resetting %s resource with addr %x\n",
  2474. ((modifier & RESET_DEVICE_LUN) ? "LUN" :
  2475. ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
  2476. le32_to_cpu(res->cfg_entry.resource_address));
  2477. /* get a free cmd block */
  2478. cmd = pmcraid_get_free_cmd(pinstance);
  2479. if (cmd == NULL) {
  2480. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2481. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2482. return FAILED;
  2483. }
  2484. ioarcb = &cmd->ioa_cb->ioarcb;
  2485. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2486. ioarcb->request_type = REQ_TYPE_IOACMD;
  2487. ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
  2488. /* Initialize reset modifier bits */
  2489. if (modifier)
  2490. modifier = ENABLE_RESET_MODIFIER | modifier;
  2491. ioarcb->cdb[1] = modifier;
  2492. init_completion(&cmd->wait_for_completion);
  2493. cmd->completion_req = 1;
  2494. pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
  2495. cmd->ioa_cb->ioarcb.cdb[0],
  2496. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
  2497. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
  2498. pmcraid_send_cmd(cmd,
  2499. pmcraid_internal_done,
  2500. timeout,
  2501. pmcraid_timeout_handler);
  2502. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2503. /* RESET_DEVICE command completes after all pending IOARCBs are
  2504. * completed. Once this command is completed, pmcraind_internal_done
  2505. * will wake up the 'completion' queue.
  2506. */
  2507. wait_for_completion(&cmd->wait_for_completion);
  2508. /* complete the command here itself and return the command block
  2509. * to free list
  2510. */
  2511. pmcraid_return_cmd(cmd);
  2512. res->reset_progress = 0;
  2513. ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2514. /* set the return value based on the returned ioasc */
  2515. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2516. }
  2517. /**
  2518. * _pmcraid_io_done - helper for pmcraid_io_done function
  2519. *
  2520. * @cmd: pointer to pmcraid command struct
  2521. * @reslen: residual data length to be set in the ioasa
  2522. * @ioasc: ioasc either returned by IOA or set by driver itself.
  2523. *
  2524. * This function is invoked by pmcraid_io_done to complete mid-layer
  2525. * scsi ops.
  2526. *
  2527. * Return value:
  2528. * 0 if caller is required to return it to free_pool. Returns 1 if
  2529. * caller need not worry about freeing command block as error handler
  2530. * will take care of that.
  2531. */
  2532. static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
  2533. {
  2534. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2535. int rc = 0;
  2536. scsi_set_resid(scsi_cmd, reslen);
  2537. pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
  2538. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2539. cmd->ioa_cb->ioarcb.cdb[0],
  2540. ioasc, scsi_cmd->result);
  2541. if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
  2542. rc = pmcraid_error_handler(cmd);
  2543. if (rc == 0) {
  2544. scsi_dma_unmap(scsi_cmd);
  2545. scsi_cmd->scsi_done(scsi_cmd);
  2546. }
  2547. return rc;
  2548. }
  2549. /**
  2550. * pmcraid_io_done - SCSI completion function
  2551. *
  2552. * @cmd: pointer to pmcraid command struct
  2553. *
  2554. * This function is invoked by tasklet/mid-layer error handler to completing
  2555. * the SCSI ops sent from mid-layer.
  2556. *
  2557. * Return value
  2558. * none
  2559. */
  2560. static void pmcraid_io_done(struct pmcraid_cmd *cmd)
  2561. {
  2562. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2563. u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
  2564. if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
  2565. pmcraid_return_cmd(cmd);
  2566. }
  2567. /**
  2568. * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
  2569. *
  2570. * @cmd: command block of the command to be aborted
  2571. *
  2572. * Return Value:
  2573. * returns pointer to command structure used as cancelling cmd
  2574. */
  2575. static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
  2576. {
  2577. struct pmcraid_cmd *cancel_cmd;
  2578. struct pmcraid_instance *pinstance;
  2579. struct pmcraid_resource_entry *res;
  2580. pinstance = (struct pmcraid_instance *)cmd->drv_inst;
  2581. res = cmd->scsi_cmd->device->hostdata;
  2582. cancel_cmd = pmcraid_get_free_cmd(pinstance);
  2583. if (cancel_cmd == NULL) {
  2584. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2585. return NULL;
  2586. }
  2587. pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
  2588. pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
  2589. cmd->ioa_cb->ioarcb.cdb[0],
  2590. cmd->ioa_cb->ioarcb.response_handle >> 2);
  2591. init_completion(&cancel_cmd->wait_for_completion);
  2592. cancel_cmd->completion_req = 1;
  2593. pmcraid_info("command (%d) CDB[0] = %x for %x\n",
  2594. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2595. cancel_cmd->ioa_cb->ioarcb.cdb[0],
  2596. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
  2597. pmcraid_send_cmd(cancel_cmd,
  2598. pmcraid_internal_done,
  2599. PMCRAID_INTERNAL_TIMEOUT,
  2600. pmcraid_timeout_handler);
  2601. return cancel_cmd;
  2602. }
  2603. /**
  2604. * pmcraid_abort_complete - Waits for ABORT TASK completion
  2605. *
  2606. * @cancel_cmd: command block use as cancelling command
  2607. *
  2608. * Return Value:
  2609. * returns SUCCESS if ABORT TASK has good completion
  2610. * otherwise FAILED
  2611. */
  2612. static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
  2613. {
  2614. struct pmcraid_resource_entry *res;
  2615. u32 ioasc;
  2616. wait_for_completion(&cancel_cmd->wait_for_completion);
  2617. res = cancel_cmd->res;
  2618. cancel_cmd->res = NULL;
  2619. ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
  2620. /* If the abort task is not timed out we will get a Good completion
  2621. * as sense_key, otherwise we may get one the following responses
  2622. * due to subsquent bus reset or device reset. In case IOASC is
  2623. * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
  2624. */
  2625. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  2626. ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
  2627. if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
  2628. res->sync_reqd = 1;
  2629. ioasc = 0;
  2630. }
  2631. /* complete the command here itself */
  2632. pmcraid_return_cmd(cancel_cmd);
  2633. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2634. }
  2635. /**
  2636. * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
  2637. *
  2638. * @scsi_cmd: scsi command struct given by mid-layer. When this is called
  2639. * mid-layer ensures that no other commands are queued. This
  2640. * never gets called under interrupt, but a separate eh thread.
  2641. *
  2642. * Return value:
  2643. * SUCCESS / FAILED
  2644. */
  2645. static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
  2646. {
  2647. struct pmcraid_instance *pinstance;
  2648. struct pmcraid_cmd *cmd;
  2649. struct pmcraid_resource_entry *res;
  2650. unsigned long host_lock_flags;
  2651. unsigned long pending_lock_flags;
  2652. struct pmcraid_cmd *cancel_cmd = NULL;
  2653. int cmd_found = 0;
  2654. int rc = FAILED;
  2655. pinstance =
  2656. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2657. scmd_printk(KERN_INFO, scsi_cmd,
  2658. "I/O command timed out, aborting it.\n");
  2659. res = scsi_cmd->device->hostdata;
  2660. if (res == NULL)
  2661. return rc;
  2662. /* If we are currently going through reset/reload, return failed.
  2663. * This will force the mid-layer to eventually call
  2664. * pmcraid_eh_host_reset which will then go to sleep and wait for the
  2665. * reset to complete
  2666. */
  2667. spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
  2668. if (pinstance->ioa_reset_in_progress ||
  2669. pinstance->ioa_state == IOA_STATE_DEAD) {
  2670. spin_unlock_irqrestore(pinstance->host->host_lock,
  2671. host_lock_flags);
  2672. return rc;
  2673. }
  2674. /* loop over pending cmd list to find cmd corresponding to this
  2675. * scsi_cmd. Note that this command might not have been completed
  2676. * already. locking: all pending commands are protected with
  2677. * pending_pool_lock.
  2678. */
  2679. spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
  2680. list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
  2681. if (cmd->scsi_cmd == scsi_cmd) {
  2682. cmd_found = 1;
  2683. break;
  2684. }
  2685. }
  2686. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  2687. pending_lock_flags);
  2688. /* If the command to be aborted was given to IOA and still pending with
  2689. * it, send ABORT_TASK to abort this and wait for its completion
  2690. */
  2691. if (cmd_found)
  2692. cancel_cmd = pmcraid_abort_cmd(cmd);
  2693. spin_unlock_irqrestore(pinstance->host->host_lock,
  2694. host_lock_flags);
  2695. if (cancel_cmd) {
  2696. cancel_cmd->res = cmd->scsi_cmd->device->hostdata;
  2697. rc = pmcraid_abort_complete(cancel_cmd);
  2698. }
  2699. return cmd_found ? rc : SUCCESS;
  2700. }
  2701. /**
  2702. * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
  2703. *
  2704. * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
  2705. *
  2706. * All these routines invokve pmcraid_reset_device with appropriate parameters.
  2707. * Since these are called from mid-layer EH thread, no other IO will be queued
  2708. * to the resource being reset. However, control path (IOCTL) may be active so
  2709. * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
  2710. * takes care by locking/unlocking host_lock.
  2711. *
  2712. * Return value
  2713. * SUCCESS or FAILED
  2714. */
  2715. static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
  2716. {
  2717. scmd_printk(KERN_INFO, scmd,
  2718. "resetting device due to an I/O command timeout.\n");
  2719. return pmcraid_reset_device(scmd,
  2720. PMCRAID_INTERNAL_TIMEOUT,
  2721. RESET_DEVICE_LUN);
  2722. }
  2723. static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
  2724. {
  2725. scmd_printk(KERN_INFO, scmd,
  2726. "Doing bus reset due to an I/O command timeout.\n");
  2727. return pmcraid_reset_device(scmd,
  2728. PMCRAID_RESET_BUS_TIMEOUT,
  2729. RESET_DEVICE_BUS);
  2730. }
  2731. static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
  2732. {
  2733. scmd_printk(KERN_INFO, scmd,
  2734. "Doing target reset due to an I/O command timeout.\n");
  2735. return pmcraid_reset_device(scmd,
  2736. PMCRAID_INTERNAL_TIMEOUT,
  2737. RESET_DEVICE_TARGET);
  2738. }
  2739. /**
  2740. * pmcraid_eh_host_reset_handler - adapter reset handler callback
  2741. *
  2742. * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
  2743. *
  2744. * Initiates adapter reset to bring it up to operational state
  2745. *
  2746. * Return value
  2747. * SUCCESS or FAILED
  2748. */
  2749. static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
  2750. {
  2751. unsigned long interval = 10000; /* 10 seconds interval */
  2752. int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
  2753. struct pmcraid_instance *pinstance =
  2754. (struct pmcraid_instance *)(scmd->device->host->hostdata);
  2755. /* wait for an additional 150 seconds just in case firmware could come
  2756. * up and if it could complete all the pending commands excluding the
  2757. * two HCAM (CCN and LDN).
  2758. */
  2759. while (waits--) {
  2760. if (atomic_read(&pinstance->outstanding_cmds) <=
  2761. PMCRAID_MAX_HCAM_CMD)
  2762. return SUCCESS;
  2763. msleep(interval);
  2764. }
  2765. dev_err(&pinstance->pdev->dev,
  2766. "Adapter being reset due to an I/O command timeout.\n");
  2767. return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
  2768. }
  2769. /**
  2770. * pmcraid_task_attributes - Translate SPI Q-Tags to task attributes
  2771. * @scsi_cmd: scsi command struct
  2772. *
  2773. * Return value
  2774. * number of tags or 0 if the task is not tagged
  2775. */
  2776. static u8 pmcraid_task_attributes(struct scsi_cmnd *scsi_cmd)
  2777. {
  2778. char tag[2];
  2779. u8 rc = 0;
  2780. if (scsi_populate_tag_msg(scsi_cmd, tag)) {
  2781. switch (tag[0]) {
  2782. case MSG_SIMPLE_TAG:
  2783. rc = TASK_TAG_SIMPLE;
  2784. break;
  2785. case MSG_HEAD_TAG:
  2786. rc = TASK_TAG_QUEUE_HEAD;
  2787. break;
  2788. case MSG_ORDERED_TAG:
  2789. rc = TASK_TAG_ORDERED;
  2790. break;
  2791. };
  2792. }
  2793. return rc;
  2794. }
  2795. /**
  2796. * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
  2797. * @cmd: pmcraid command struct
  2798. * @sgcount: count of scatter-gather elements
  2799. *
  2800. * Return value
  2801. * returns pointer pmcraid_ioadl_desc, initialized to point to internal
  2802. * or external IOADLs
  2803. */
  2804. struct pmcraid_ioadl_desc *
  2805. pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
  2806. {
  2807. struct pmcraid_ioadl_desc *ioadl;
  2808. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2809. int ioadl_count = 0;
  2810. if (ioarcb->add_cmd_param_length)
  2811. ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
  2812. ioarcb->ioadl_length =
  2813. sizeof(struct pmcraid_ioadl_desc) * sgcount;
  2814. if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
  2815. /* external ioadls start at offset 0x80 from control_block
  2816. * structure, re-using 24 out of 27 ioadls part of IOARCB.
  2817. * It is necessary to indicate to firmware that driver is
  2818. * using ioadls to be treated as external to IOARCB.
  2819. */
  2820. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  2821. ioarcb->ioadl_bus_addr =
  2822. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2823. offsetof(struct pmcraid_ioarcb,
  2824. add_data.u.ioadl[3]));
  2825. ioadl = &ioarcb->add_data.u.ioadl[3];
  2826. } else {
  2827. ioarcb->ioadl_bus_addr =
  2828. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2829. offsetof(struct pmcraid_ioarcb,
  2830. add_data.u.ioadl[ioadl_count]));
  2831. ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
  2832. ioarcb->ioarcb_bus_addr |=
  2833. DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
  2834. }
  2835. return ioadl;
  2836. }
  2837. /**
  2838. * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
  2839. * @pinstance: pointer to adapter instance structure
  2840. * @cmd: pmcraid command struct
  2841. *
  2842. * This function is invoked by queuecommand entry point while sending a command
  2843. * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
  2844. *
  2845. * Return value:
  2846. * 0 on success or -1 on failure
  2847. */
  2848. static int pmcraid_build_ioadl(
  2849. struct pmcraid_instance *pinstance,
  2850. struct pmcraid_cmd *cmd
  2851. )
  2852. {
  2853. int i, nseg;
  2854. struct scatterlist *sglist;
  2855. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2856. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  2857. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2858. u32 length = scsi_bufflen(scsi_cmd);
  2859. if (!length)
  2860. return 0;
  2861. nseg = scsi_dma_map(scsi_cmd);
  2862. if (nseg < 0) {
  2863. scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
  2864. return -1;
  2865. } else if (nseg > PMCRAID_MAX_IOADLS) {
  2866. scsi_dma_unmap(scsi_cmd);
  2867. scmd_printk(KERN_ERR, scsi_cmd,
  2868. "sg count is (%d) more than allowed!\n", nseg);
  2869. return -1;
  2870. }
  2871. /* Initialize IOARCB data transfer length fields */
  2872. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
  2873. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  2874. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2875. ioarcb->data_transfer_length = cpu_to_le32(length);
  2876. ioadl = pmcraid_init_ioadls(cmd, nseg);
  2877. /* Initialize IOADL descriptor addresses */
  2878. scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
  2879. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
  2880. ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
  2881. ioadl[i].flags = 0;
  2882. }
  2883. /* setup last descriptor */
  2884. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  2885. return 0;
  2886. }
  2887. /**
  2888. * pmcraid_free_sglist - Frees an allocated SG buffer list
  2889. * @sglist: scatter/gather list pointer
  2890. *
  2891. * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
  2892. *
  2893. * Return value:
  2894. * none
  2895. */
  2896. static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
  2897. {
  2898. int i;
  2899. for (i = 0; i < sglist->num_sg; i++)
  2900. __free_pages(sg_page(&(sglist->scatterlist[i])),
  2901. sglist->order);
  2902. kfree(sglist);
  2903. }
  2904. /**
  2905. * pmcraid_alloc_sglist - Allocates memory for a SG list
  2906. * @buflen: buffer length
  2907. *
  2908. * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
  2909. * list.
  2910. *
  2911. * Return value
  2912. * pointer to sglist / NULL on failure
  2913. */
  2914. static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
  2915. {
  2916. struct pmcraid_sglist *sglist;
  2917. struct scatterlist *scatterlist;
  2918. struct page *page;
  2919. int num_elem, i, j;
  2920. int sg_size;
  2921. int order;
  2922. int bsize_elem;
  2923. sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
  2924. order = (sg_size > 0) ? get_order(sg_size) : 0;
  2925. bsize_elem = PAGE_SIZE * (1 << order);
  2926. /* Determine the actual number of sg entries needed */
  2927. if (buflen % bsize_elem)
  2928. num_elem = (buflen / bsize_elem) + 1;
  2929. else
  2930. num_elem = buflen / bsize_elem;
  2931. /* Allocate a scatter/gather list for the DMA */
  2932. sglist = kzalloc(sizeof(struct pmcraid_sglist) +
  2933. (sizeof(struct scatterlist) * (num_elem - 1)),
  2934. GFP_KERNEL);
  2935. if (sglist == NULL)
  2936. return NULL;
  2937. scatterlist = sglist->scatterlist;
  2938. sg_init_table(scatterlist, num_elem);
  2939. sglist->order = order;
  2940. sglist->num_sg = num_elem;
  2941. sg_size = buflen;
  2942. for (i = 0; i < num_elem; i++) {
  2943. page = alloc_pages(GFP_KERNEL|GFP_DMA|__GFP_ZERO, order);
  2944. if (!page) {
  2945. for (j = i - 1; j >= 0; j--)
  2946. __free_pages(sg_page(&scatterlist[j]), order);
  2947. kfree(sglist);
  2948. return NULL;
  2949. }
  2950. sg_set_page(&scatterlist[i], page,
  2951. sg_size < bsize_elem ? sg_size : bsize_elem, 0);
  2952. sg_size -= bsize_elem;
  2953. }
  2954. return sglist;
  2955. }
  2956. /**
  2957. * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
  2958. * @sglist: scatter/gather list pointer
  2959. * @buffer: buffer pointer
  2960. * @len: buffer length
  2961. * @direction: data transfer direction
  2962. *
  2963. * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
  2964. *
  2965. * Return value:
  2966. * 0 on success / other on failure
  2967. */
  2968. static int pmcraid_copy_sglist(
  2969. struct pmcraid_sglist *sglist,
  2970. unsigned long buffer,
  2971. u32 len,
  2972. int direction
  2973. )
  2974. {
  2975. struct scatterlist *scatterlist;
  2976. void *kaddr;
  2977. int bsize_elem;
  2978. int i;
  2979. int rc = 0;
  2980. /* Determine the actual number of bytes per element */
  2981. bsize_elem = PAGE_SIZE * (1 << sglist->order);
  2982. scatterlist = sglist->scatterlist;
  2983. for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
  2984. struct page *page = sg_page(&scatterlist[i]);
  2985. kaddr = kmap(page);
  2986. if (direction == DMA_TO_DEVICE)
  2987. rc = __copy_from_user(kaddr,
  2988. (void *)buffer,
  2989. bsize_elem);
  2990. else
  2991. rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
  2992. kunmap(page);
  2993. if (rc) {
  2994. pmcraid_err("failed to copy user data into sg list\n");
  2995. return -EFAULT;
  2996. }
  2997. scatterlist[i].length = bsize_elem;
  2998. }
  2999. if (len % bsize_elem) {
  3000. struct page *page = sg_page(&scatterlist[i]);
  3001. kaddr = kmap(page);
  3002. if (direction == DMA_TO_DEVICE)
  3003. rc = __copy_from_user(kaddr,
  3004. (void *)buffer,
  3005. len % bsize_elem);
  3006. else
  3007. rc = __copy_to_user((void *)buffer,
  3008. kaddr,
  3009. len % bsize_elem);
  3010. kunmap(page);
  3011. scatterlist[i].length = len % bsize_elem;
  3012. }
  3013. if (rc) {
  3014. pmcraid_err("failed to copy user data into sg list\n");
  3015. rc = -EFAULT;
  3016. }
  3017. return rc;
  3018. }
  3019. /**
  3020. * pmcraid_queuecommand - Queue a mid-layer request
  3021. * @scsi_cmd: scsi command struct
  3022. * @done: done function
  3023. *
  3024. * This function queues a request generated by the mid-layer. Midlayer calls
  3025. * this routine within host->lock. Some of the functions called by queuecommand
  3026. * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
  3027. *
  3028. * Return value:
  3029. * 0 on success
  3030. * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
  3031. * SCSI_MLQUEUE_HOST_BUSY if host is busy
  3032. */
  3033. static int pmcraid_queuecommand(
  3034. struct scsi_cmnd *scsi_cmd,
  3035. void (*done) (struct scsi_cmnd *)
  3036. )
  3037. {
  3038. struct pmcraid_instance *pinstance;
  3039. struct pmcraid_resource_entry *res;
  3040. struct pmcraid_ioarcb *ioarcb;
  3041. struct pmcraid_cmd *cmd;
  3042. u32 fw_version;
  3043. int rc = 0;
  3044. pinstance =
  3045. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  3046. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3047. scsi_cmd->scsi_done = done;
  3048. res = scsi_cmd->device->hostdata;
  3049. scsi_cmd->result = (DID_OK << 16);
  3050. /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
  3051. * the command
  3052. */
  3053. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  3054. pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
  3055. scsi_cmd->result = (DID_NO_CONNECT << 16);
  3056. scsi_cmd->scsi_done(scsi_cmd);
  3057. return 0;
  3058. }
  3059. /* If IOA reset is in progress, can't queue the commands */
  3060. if (pinstance->ioa_reset_in_progress)
  3061. return SCSI_MLQUEUE_HOST_BUSY;
  3062. /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
  3063. * the command here itself with success return
  3064. */
  3065. if (scsi_cmd->cmnd[0] == SYNCHRONIZE_CACHE) {
  3066. pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
  3067. scsi_cmd->scsi_done(scsi_cmd);
  3068. return 0;
  3069. }
  3070. /* initialize the command and IOARCB to be sent to IOA */
  3071. cmd = pmcraid_get_free_cmd(pinstance);
  3072. if (cmd == NULL) {
  3073. pmcraid_err("free command block is not available\n");
  3074. return SCSI_MLQUEUE_HOST_BUSY;
  3075. }
  3076. cmd->scsi_cmd = scsi_cmd;
  3077. ioarcb = &(cmd->ioa_cb->ioarcb);
  3078. memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
  3079. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  3080. ioarcb->request_type = REQ_TYPE_SCSI;
  3081. /* set hrrq number where the IOA should respond to. Note that all cmds
  3082. * generated internally uses hrrq_id 0, exception to this is the cmd
  3083. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3084. * hrrq_id assigned here in queuecommand
  3085. */
  3086. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3087. pinstance->num_hrrq;
  3088. cmd->cmd_done = pmcraid_io_done;
  3089. if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
  3090. if (scsi_cmd->underflow == 0)
  3091. ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
  3092. if (res->sync_reqd) {
  3093. ioarcb->request_flags0 |= SYNC_COMPLETE;
  3094. res->sync_reqd = 0;
  3095. }
  3096. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3097. ioarcb->request_flags1 |= pmcraid_task_attributes(scsi_cmd);
  3098. if (RES_IS_GSCSI(res->cfg_entry))
  3099. ioarcb->request_flags1 |= DELAY_AFTER_RESET;
  3100. }
  3101. rc = pmcraid_build_ioadl(pinstance, cmd);
  3102. pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
  3103. le32_to_cpu(ioarcb->response_handle) >> 2,
  3104. scsi_cmd->cmnd[0], pinstance->host->unique_id,
  3105. RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
  3106. PMCRAID_PHYS_BUS_ID,
  3107. RES_IS_VSET(res->cfg_entry) ?
  3108. (fw_version <= PMCRAID_FW_VERSION_1 ?
  3109. res->cfg_entry.unique_flags1 :
  3110. res->cfg_entry.array_id & 0xFF) :
  3111. RES_TARGET(res->cfg_entry.resource_address),
  3112. RES_LUN(res->cfg_entry.resource_address));
  3113. if (likely(rc == 0)) {
  3114. _pmcraid_fire_command(cmd);
  3115. } else {
  3116. pmcraid_err("queuecommand could not build ioadl\n");
  3117. pmcraid_return_cmd(cmd);
  3118. rc = SCSI_MLQUEUE_HOST_BUSY;
  3119. }
  3120. return rc;
  3121. }
  3122. /**
  3123. * pmcraid_open -char node "open" entry, allowed only users with admin access
  3124. */
  3125. static int pmcraid_chr_open(struct inode *inode, struct file *filep)
  3126. {
  3127. struct pmcraid_instance *pinstance;
  3128. if (!capable(CAP_SYS_ADMIN))
  3129. return -EACCES;
  3130. /* Populate adapter instance * pointer for use by ioctl */
  3131. pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
  3132. filep->private_data = pinstance;
  3133. return 0;
  3134. }
  3135. /**
  3136. * pmcraid_release - char node "release" entry point
  3137. */
  3138. static int pmcraid_chr_release(struct inode *inode, struct file *filep)
  3139. {
  3140. struct pmcraid_instance *pinstance = filep->private_data;
  3141. filep->private_data = NULL;
  3142. fasync_helper(-1, filep, 0, &pinstance->aen_queue);
  3143. return 0;
  3144. }
  3145. /**
  3146. * pmcraid_fasync - Async notifier registration from applications
  3147. *
  3148. * This function adds the calling process to a driver global queue. When an
  3149. * event occurs, SIGIO will be sent to all processes in this queue.
  3150. */
  3151. static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
  3152. {
  3153. struct pmcraid_instance *pinstance;
  3154. int rc;
  3155. pinstance = filep->private_data;
  3156. mutex_lock(&pinstance->aen_queue_lock);
  3157. rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
  3158. mutex_unlock(&pinstance->aen_queue_lock);
  3159. return rc;
  3160. }
  3161. /**
  3162. * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
  3163. * commands sent over IOCTL interface
  3164. *
  3165. * @cmd : pointer to struct pmcraid_cmd
  3166. * @buflen : length of the request buffer
  3167. * @direction : data transfer direction
  3168. *
  3169. * Return value
  3170. * 0 on success, non-zero error code on failure
  3171. */
  3172. static int pmcraid_build_passthrough_ioadls(
  3173. struct pmcraid_cmd *cmd,
  3174. int buflen,
  3175. int direction
  3176. )
  3177. {
  3178. struct pmcraid_sglist *sglist = NULL;
  3179. struct scatterlist *sg = NULL;
  3180. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  3181. struct pmcraid_ioadl_desc *ioadl;
  3182. int i;
  3183. sglist = pmcraid_alloc_sglist(buflen);
  3184. if (!sglist) {
  3185. pmcraid_err("can't allocate memory for passthrough SGls\n");
  3186. return -ENOMEM;
  3187. }
  3188. sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
  3189. sglist->scatterlist,
  3190. sglist->num_sg, direction);
  3191. if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
  3192. dev_err(&cmd->drv_inst->pdev->dev,
  3193. "Failed to map passthrough buffer!\n");
  3194. pmcraid_free_sglist(sglist);
  3195. return -EIO;
  3196. }
  3197. cmd->sglist = sglist;
  3198. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3199. ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
  3200. /* Initialize IOADL descriptor addresses */
  3201. for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
  3202. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
  3203. ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
  3204. ioadl[i].flags = 0;
  3205. }
  3206. /* setup the last descriptor */
  3207. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  3208. return 0;
  3209. }
  3210. /**
  3211. * pmcraid_release_passthrough_ioadls - release passthrough ioadls
  3212. *
  3213. * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
  3214. * @buflen: size of the request buffer
  3215. * @direction: data transfer direction
  3216. *
  3217. * Return value
  3218. * 0 on success, non-zero error code on failure
  3219. */
  3220. static void pmcraid_release_passthrough_ioadls(
  3221. struct pmcraid_cmd *cmd,
  3222. int buflen,
  3223. int direction
  3224. )
  3225. {
  3226. struct pmcraid_sglist *sglist = cmd->sglist;
  3227. if (buflen > 0) {
  3228. pci_unmap_sg(cmd->drv_inst->pdev,
  3229. sglist->scatterlist,
  3230. sglist->num_sg,
  3231. direction);
  3232. pmcraid_free_sglist(sglist);
  3233. cmd->sglist = NULL;
  3234. }
  3235. }
  3236. /**
  3237. * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
  3238. *
  3239. * @pinstance: pointer to adapter instance structure
  3240. * @cmd: ioctl code
  3241. * @arg: pointer to pmcraid_passthrough_buffer user buffer
  3242. *
  3243. * Return value
  3244. * 0 on success, non-zero error code on failure
  3245. */
  3246. static long pmcraid_ioctl_passthrough(
  3247. struct pmcraid_instance *pinstance,
  3248. unsigned int ioctl_cmd,
  3249. unsigned int buflen,
  3250. unsigned long arg
  3251. )
  3252. {
  3253. struct pmcraid_passthrough_ioctl_buffer *buffer;
  3254. struct pmcraid_ioarcb *ioarcb;
  3255. struct pmcraid_cmd *cmd;
  3256. struct pmcraid_cmd *cancel_cmd;
  3257. unsigned long request_buffer;
  3258. unsigned long request_offset;
  3259. unsigned long lock_flags;
  3260. void *ioasa;
  3261. u32 ioasc;
  3262. int request_size;
  3263. int buffer_size;
  3264. u8 access, direction;
  3265. int rc = 0;
  3266. /* If IOA reset is in progress, wait 10 secs for reset to complete */
  3267. if (pinstance->ioa_reset_in_progress) {
  3268. rc = wait_event_interruptible_timeout(
  3269. pinstance->reset_wait_q,
  3270. !pinstance->ioa_reset_in_progress,
  3271. msecs_to_jiffies(10000));
  3272. if (!rc)
  3273. return -ETIMEDOUT;
  3274. else if (rc < 0)
  3275. return -ERESTARTSYS;
  3276. }
  3277. /* If adapter is not in operational state, return error */
  3278. if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
  3279. pmcraid_err("IOA is not operational\n");
  3280. return -ENOTTY;
  3281. }
  3282. buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
  3283. buffer = kmalloc(buffer_size, GFP_KERNEL);
  3284. if (!buffer) {
  3285. pmcraid_err("no memory for passthrough buffer\n");
  3286. return -ENOMEM;
  3287. }
  3288. request_offset =
  3289. offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
  3290. request_buffer = arg + request_offset;
  3291. rc = __copy_from_user(buffer,
  3292. (struct pmcraid_passthrough_ioctl_buffer *) arg,
  3293. sizeof(struct pmcraid_passthrough_ioctl_buffer));
  3294. ioasa =
  3295. (void *)(arg +
  3296. offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
  3297. if (rc) {
  3298. pmcraid_err("ioctl: can't copy passthrough buffer\n");
  3299. rc = -EFAULT;
  3300. goto out_free_buffer;
  3301. }
  3302. request_size = buffer->ioarcb.data_transfer_length;
  3303. if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
  3304. access = VERIFY_READ;
  3305. direction = DMA_TO_DEVICE;
  3306. } else {
  3307. access = VERIFY_WRITE;
  3308. direction = DMA_FROM_DEVICE;
  3309. }
  3310. if (request_size > 0) {
  3311. rc = access_ok(access, arg, request_offset + request_size);
  3312. if (!rc) {
  3313. rc = -EFAULT;
  3314. goto out_free_buffer;
  3315. }
  3316. }
  3317. /* check if we have any additional command parameters */
  3318. if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
  3319. rc = -EINVAL;
  3320. goto out_free_buffer;
  3321. }
  3322. cmd = pmcraid_get_free_cmd(pinstance);
  3323. if (!cmd) {
  3324. pmcraid_err("free command block is not available\n");
  3325. rc = -ENOMEM;
  3326. goto out_free_buffer;
  3327. }
  3328. cmd->scsi_cmd = NULL;
  3329. ioarcb = &(cmd->ioa_cb->ioarcb);
  3330. /* Copy the user-provided IOARCB stuff field by field */
  3331. ioarcb->resource_handle = buffer->ioarcb.resource_handle;
  3332. ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
  3333. ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
  3334. ioarcb->request_type = buffer->ioarcb.request_type;
  3335. ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
  3336. ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
  3337. memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
  3338. if (buffer->ioarcb.add_cmd_param_length) {
  3339. ioarcb->add_cmd_param_length =
  3340. buffer->ioarcb.add_cmd_param_length;
  3341. ioarcb->add_cmd_param_offset =
  3342. buffer->ioarcb.add_cmd_param_offset;
  3343. memcpy(ioarcb->add_data.u.add_cmd_params,
  3344. buffer->ioarcb.add_data.u.add_cmd_params,
  3345. buffer->ioarcb.add_cmd_param_length);
  3346. }
  3347. /* set hrrq number where the IOA should respond to. Note that all cmds
  3348. * generated internally uses hrrq_id 0, exception to this is the cmd
  3349. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3350. * hrrq_id assigned here in queuecommand
  3351. */
  3352. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3353. pinstance->num_hrrq;
  3354. if (request_size) {
  3355. rc = pmcraid_build_passthrough_ioadls(cmd,
  3356. request_size,
  3357. direction);
  3358. if (rc) {
  3359. pmcraid_err("couldn't build passthrough ioadls\n");
  3360. goto out_free_buffer;
  3361. }
  3362. }
  3363. /* If data is being written into the device, copy the data from user
  3364. * buffers
  3365. */
  3366. if (direction == DMA_TO_DEVICE && request_size > 0) {
  3367. rc = pmcraid_copy_sglist(cmd->sglist,
  3368. request_buffer,
  3369. request_size,
  3370. direction);
  3371. if (rc) {
  3372. pmcraid_err("failed to copy user buffer\n");
  3373. goto out_free_sglist;
  3374. }
  3375. }
  3376. /* passthrough ioctl is a blocking command so, put the user to sleep
  3377. * until timeout. Note that a timeout value of 0 means, do timeout.
  3378. */
  3379. cmd->cmd_done = pmcraid_internal_done;
  3380. init_completion(&cmd->wait_for_completion);
  3381. cmd->completion_req = 1;
  3382. pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
  3383. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  3384. cmd->ioa_cb->ioarcb.cdb[0],
  3385. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
  3386. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3387. _pmcraid_fire_command(cmd);
  3388. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3389. /* NOTE ! Remove the below line once abort_task is implemented
  3390. * in firmware. This line disables ioctl command timeout handling logic
  3391. * similar to IO command timeout handling, making ioctl commands to wait
  3392. * until the command completion regardless of timeout value specified in
  3393. * ioarcb
  3394. */
  3395. buffer->ioarcb.cmd_timeout = 0;
  3396. /* If command timeout is specified put caller to wait till that time,
  3397. * otherwise it would be blocking wait. If command gets timed out, it
  3398. * will be aborted.
  3399. */
  3400. if (buffer->ioarcb.cmd_timeout == 0) {
  3401. wait_for_completion(&cmd->wait_for_completion);
  3402. } else if (!wait_for_completion_timeout(
  3403. &cmd->wait_for_completion,
  3404. msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
  3405. pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
  3406. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
  3407. cmd->ioa_cb->ioarcb.cdb[0]);
  3408. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3409. cancel_cmd = pmcraid_abort_cmd(cmd);
  3410. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3411. if (cancel_cmd) {
  3412. wait_for_completion(&cancel_cmd->wait_for_completion);
  3413. ioasc = cancel_cmd->ioa_cb->ioasa.ioasc;
  3414. pmcraid_return_cmd(cancel_cmd);
  3415. /* if abort task couldn't find the command i.e it got
  3416. * completed prior to aborting, return good completion.
  3417. * if command got aborted succesfully or there was IOA
  3418. * reset due to abort task itself getting timedout then
  3419. * return -ETIMEDOUT
  3420. */
  3421. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  3422. PMCRAID_IOASC_SENSE_KEY(ioasc) == 0x00) {
  3423. if (ioasc != PMCRAID_IOASC_GC_IOARCB_NOTFOUND)
  3424. rc = -ETIMEDOUT;
  3425. goto out_handle_response;
  3426. }
  3427. }
  3428. /* no command block for abort task or abort task failed to abort
  3429. * the IOARCB, then wait for 150 more seconds and initiate reset
  3430. * sequence after timeout
  3431. */
  3432. if (!wait_for_completion_timeout(
  3433. &cmd->wait_for_completion,
  3434. msecs_to_jiffies(150 * 1000))) {
  3435. pmcraid_reset_bringup(cmd->drv_inst);
  3436. rc = -ETIMEDOUT;
  3437. }
  3438. }
  3439. out_handle_response:
  3440. /* copy entire IOASA buffer and return IOCTL success.
  3441. * If copying IOASA to user-buffer fails, return
  3442. * EFAULT
  3443. */
  3444. if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
  3445. sizeof(struct pmcraid_ioasa))) {
  3446. pmcraid_err("failed to copy ioasa buffer to user\n");
  3447. rc = -EFAULT;
  3448. }
  3449. /* If the data transfer was from device, copy the data onto user
  3450. * buffers
  3451. */
  3452. else if (direction == DMA_FROM_DEVICE && request_size > 0) {
  3453. rc = pmcraid_copy_sglist(cmd->sglist,
  3454. request_buffer,
  3455. request_size,
  3456. direction);
  3457. if (rc) {
  3458. pmcraid_err("failed to copy user buffer\n");
  3459. rc = -EFAULT;
  3460. }
  3461. }
  3462. out_free_sglist:
  3463. pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
  3464. pmcraid_return_cmd(cmd);
  3465. out_free_buffer:
  3466. kfree(buffer);
  3467. return rc;
  3468. }
  3469. /**
  3470. * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
  3471. *
  3472. * @pinstance: pointer to adapter instance structure
  3473. * @cmd: ioctl command passed in
  3474. * @buflen: length of user_buffer
  3475. * @user_buffer: user buffer pointer
  3476. *
  3477. * Return Value
  3478. * 0 in case of success, otherwise appropriate error code
  3479. */
  3480. static long pmcraid_ioctl_driver(
  3481. struct pmcraid_instance *pinstance,
  3482. unsigned int cmd,
  3483. unsigned int buflen,
  3484. void __user *user_buffer
  3485. )
  3486. {
  3487. int rc = -ENOSYS;
  3488. if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
  3489. pmcraid_err("ioctl_driver: access fault in request buffer\n");
  3490. return -EFAULT;
  3491. }
  3492. switch (cmd) {
  3493. case PMCRAID_IOCTL_RESET_ADAPTER:
  3494. pmcraid_reset_bringup(pinstance);
  3495. rc = 0;
  3496. break;
  3497. default:
  3498. break;
  3499. }
  3500. return rc;
  3501. }
  3502. /**
  3503. * pmcraid_check_ioctl_buffer - check for proper access to user buffer
  3504. *
  3505. * @cmd: ioctl command
  3506. * @arg: user buffer
  3507. * @hdr: pointer to kernel memory for pmcraid_ioctl_header
  3508. *
  3509. * Return Value
  3510. * negetive error code if there are access issues, otherwise zero.
  3511. * Upon success, returns ioctl header copied out of user buffer.
  3512. */
  3513. static int pmcraid_check_ioctl_buffer(
  3514. int cmd,
  3515. void __user *arg,
  3516. struct pmcraid_ioctl_header *hdr
  3517. )
  3518. {
  3519. int rc = 0;
  3520. int access = VERIFY_READ;
  3521. if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
  3522. pmcraid_err("couldn't copy ioctl header from user buffer\n");
  3523. return -EFAULT;
  3524. }
  3525. /* check for valid driver signature */
  3526. rc = memcmp(hdr->signature,
  3527. PMCRAID_IOCTL_SIGNATURE,
  3528. sizeof(hdr->signature));
  3529. if (rc) {
  3530. pmcraid_err("signature verification failed\n");
  3531. return -EINVAL;
  3532. }
  3533. /* check for appropriate buffer access */
  3534. if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
  3535. access = VERIFY_WRITE;
  3536. rc = access_ok(access,
  3537. (arg + sizeof(struct pmcraid_ioctl_header)),
  3538. hdr->buffer_length);
  3539. if (!rc) {
  3540. pmcraid_err("access failed for user buffer of size %d\n",
  3541. hdr->buffer_length);
  3542. return -EFAULT;
  3543. }
  3544. return 0;
  3545. }
  3546. /**
  3547. * pmcraid_ioctl - char node ioctl entry point
  3548. */
  3549. static long pmcraid_chr_ioctl(
  3550. struct file *filep,
  3551. unsigned int cmd,
  3552. unsigned long arg
  3553. )
  3554. {
  3555. struct pmcraid_instance *pinstance = NULL;
  3556. struct pmcraid_ioctl_header *hdr = NULL;
  3557. int retval = -ENOTTY;
  3558. hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
  3559. if (!hdr) {
  3560. pmcraid_err("faile to allocate memory for ioctl header\n");
  3561. return -ENOMEM;
  3562. }
  3563. retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
  3564. if (retval) {
  3565. pmcraid_info("chr_ioctl: header check failed\n");
  3566. kfree(hdr);
  3567. return retval;
  3568. }
  3569. pinstance = filep->private_data;
  3570. if (!pinstance) {
  3571. pmcraid_info("adapter instance is not found\n");
  3572. kfree(hdr);
  3573. return -ENOTTY;
  3574. }
  3575. switch (_IOC_TYPE(cmd)) {
  3576. case PMCRAID_PASSTHROUGH_IOCTL:
  3577. /* If ioctl code is to download microcode, we need to block
  3578. * mid-layer requests.
  3579. */
  3580. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3581. scsi_block_requests(pinstance->host);
  3582. retval = pmcraid_ioctl_passthrough(pinstance,
  3583. cmd,
  3584. hdr->buffer_length,
  3585. arg);
  3586. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3587. scsi_unblock_requests(pinstance->host);
  3588. break;
  3589. case PMCRAID_DRIVER_IOCTL:
  3590. arg += sizeof(struct pmcraid_ioctl_header);
  3591. retval = pmcraid_ioctl_driver(pinstance,
  3592. cmd,
  3593. hdr->buffer_length,
  3594. (void __user *)arg);
  3595. break;
  3596. default:
  3597. retval = -ENOTTY;
  3598. break;
  3599. }
  3600. kfree(hdr);
  3601. return retval;
  3602. }
  3603. /**
  3604. * File operations structure for management interface
  3605. */
  3606. static const struct file_operations pmcraid_fops = {
  3607. .owner = THIS_MODULE,
  3608. .open = pmcraid_chr_open,
  3609. .release = pmcraid_chr_release,
  3610. .fasync = pmcraid_chr_fasync,
  3611. .unlocked_ioctl = pmcraid_chr_ioctl,
  3612. #ifdef CONFIG_COMPAT
  3613. .compat_ioctl = pmcraid_chr_ioctl,
  3614. #endif
  3615. .llseek = noop_llseek,
  3616. };
  3617. /**
  3618. * pmcraid_show_log_level - Display adapter's error logging level
  3619. * @dev: class device struct
  3620. * @buf: buffer
  3621. *
  3622. * Return value:
  3623. * number of bytes printed to buffer
  3624. */
  3625. static ssize_t pmcraid_show_log_level(
  3626. struct device *dev,
  3627. struct device_attribute *attr,
  3628. char *buf)
  3629. {
  3630. struct Scsi_Host *shost = class_to_shost(dev);
  3631. struct pmcraid_instance *pinstance =
  3632. (struct pmcraid_instance *)shost->hostdata;
  3633. return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
  3634. }
  3635. /**
  3636. * pmcraid_store_log_level - Change the adapter's error logging level
  3637. * @dev: class device struct
  3638. * @buf: buffer
  3639. * @count: not used
  3640. *
  3641. * Return value:
  3642. * number of bytes printed to buffer
  3643. */
  3644. static ssize_t pmcraid_store_log_level(
  3645. struct device *dev,
  3646. struct device_attribute *attr,
  3647. const char *buf,
  3648. size_t count
  3649. )
  3650. {
  3651. struct Scsi_Host *shost;
  3652. struct pmcraid_instance *pinstance;
  3653. unsigned long val;
  3654. if (strict_strtoul(buf, 10, &val))
  3655. return -EINVAL;
  3656. /* log-level should be from 0 to 2 */
  3657. if (val > 2)
  3658. return -EINVAL;
  3659. shost = class_to_shost(dev);
  3660. pinstance = (struct pmcraid_instance *)shost->hostdata;
  3661. pinstance->current_log_level = val;
  3662. return strlen(buf);
  3663. }
  3664. static struct device_attribute pmcraid_log_level_attr = {
  3665. .attr = {
  3666. .name = "log_level",
  3667. .mode = S_IRUGO | S_IWUSR,
  3668. },
  3669. .show = pmcraid_show_log_level,
  3670. .store = pmcraid_store_log_level,
  3671. };
  3672. /**
  3673. * pmcraid_show_drv_version - Display driver version
  3674. * @dev: class device struct
  3675. * @buf: buffer
  3676. *
  3677. * Return value:
  3678. * number of bytes printed to buffer
  3679. */
  3680. static ssize_t pmcraid_show_drv_version(
  3681. struct device *dev,
  3682. struct device_attribute *attr,
  3683. char *buf
  3684. )
  3685. {
  3686. return snprintf(buf, PAGE_SIZE, "version: %s, build date: %s\n",
  3687. PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
  3688. }
  3689. static struct device_attribute pmcraid_driver_version_attr = {
  3690. .attr = {
  3691. .name = "drv_version",
  3692. .mode = S_IRUGO,
  3693. },
  3694. .show = pmcraid_show_drv_version,
  3695. };
  3696. /**
  3697. * pmcraid_show_io_adapter_id - Display driver assigned adapter id
  3698. * @dev: class device struct
  3699. * @buf: buffer
  3700. *
  3701. * Return value:
  3702. * number of bytes printed to buffer
  3703. */
  3704. static ssize_t pmcraid_show_adapter_id(
  3705. struct device *dev,
  3706. struct device_attribute *attr,
  3707. char *buf
  3708. )
  3709. {
  3710. struct Scsi_Host *shost = class_to_shost(dev);
  3711. struct pmcraid_instance *pinstance =
  3712. (struct pmcraid_instance *)shost->hostdata;
  3713. u32 adapter_id = (pinstance->pdev->bus->number << 8) |
  3714. pinstance->pdev->devfn;
  3715. u32 aen_group = pmcraid_event_family.id;
  3716. return snprintf(buf, PAGE_SIZE,
  3717. "adapter id: %d\nminor: %d\naen group: %d\n",
  3718. adapter_id, MINOR(pinstance->cdev.dev), aen_group);
  3719. }
  3720. static struct device_attribute pmcraid_adapter_id_attr = {
  3721. .attr = {
  3722. .name = "adapter_id",
  3723. .mode = S_IRUGO | S_IWUSR,
  3724. },
  3725. .show = pmcraid_show_adapter_id,
  3726. };
  3727. static struct device_attribute *pmcraid_host_attrs[] = {
  3728. &pmcraid_log_level_attr,
  3729. &pmcraid_driver_version_attr,
  3730. &pmcraid_adapter_id_attr,
  3731. NULL,
  3732. };
  3733. /* host template structure for pmcraid driver */
  3734. static struct scsi_host_template pmcraid_host_template = {
  3735. .module = THIS_MODULE,
  3736. .name = PMCRAID_DRIVER_NAME,
  3737. .queuecommand = pmcraid_queuecommand,
  3738. .eh_abort_handler = pmcraid_eh_abort_handler,
  3739. .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
  3740. .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
  3741. .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
  3742. .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
  3743. .slave_alloc = pmcraid_slave_alloc,
  3744. .slave_configure = pmcraid_slave_configure,
  3745. .slave_destroy = pmcraid_slave_destroy,
  3746. .change_queue_depth = pmcraid_change_queue_depth,
  3747. .change_queue_type = pmcraid_change_queue_type,
  3748. .can_queue = PMCRAID_MAX_IO_CMD,
  3749. .this_id = -1,
  3750. .sg_tablesize = PMCRAID_MAX_IOADLS,
  3751. .max_sectors = PMCRAID_IOA_MAX_SECTORS,
  3752. .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
  3753. .use_clustering = ENABLE_CLUSTERING,
  3754. .shost_attrs = pmcraid_host_attrs,
  3755. .proc_name = PMCRAID_DRIVER_NAME
  3756. };
  3757. /*
  3758. * pmcraid_isr_msix - implements MSI-X interrupt handling routine
  3759. * @irq: interrupt vector number
  3760. * @dev_id: pointer hrrq_vector
  3761. *
  3762. * Return Value
  3763. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3764. */
  3765. static irqreturn_t pmcraid_isr_msix(int irq, void *dev_id)
  3766. {
  3767. struct pmcraid_isr_param *hrrq_vector;
  3768. struct pmcraid_instance *pinstance;
  3769. unsigned long lock_flags;
  3770. u32 intrs_val;
  3771. int hrrq_id;
  3772. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3773. hrrq_id = hrrq_vector->hrrq_id;
  3774. pinstance = hrrq_vector->drv_inst;
  3775. if (!hrrq_id) {
  3776. /* Read the interrupt */
  3777. intrs_val = pmcraid_read_interrupts(pinstance);
  3778. if (intrs_val &&
  3779. ((ioread32(pinstance->int_regs.host_ioa_interrupt_reg)
  3780. & DOORBELL_INTR_MSIX_CLR) == 0)) {
  3781. /* Any error interrupts including unit_check,
  3782. * initiate IOA reset.In case of unit check indicate
  3783. * to reset_sequence that IOA unit checked and prepare
  3784. * for a dump during reset sequence
  3785. */
  3786. if (intrs_val & PMCRAID_ERROR_INTERRUPTS) {
  3787. if (intrs_val & INTRS_IOA_UNIT_CHECK)
  3788. pinstance->ioa_unit_check = 1;
  3789. pmcraid_err("ISR: error interrupts: %x \
  3790. initiating reset\n", intrs_val);
  3791. spin_lock_irqsave(pinstance->host->host_lock,
  3792. lock_flags);
  3793. pmcraid_initiate_reset(pinstance);
  3794. spin_unlock_irqrestore(
  3795. pinstance->host->host_lock,
  3796. lock_flags);
  3797. }
  3798. /* If interrupt was as part of the ioa initialization,
  3799. * clear it. Delete the timer and wakeup the
  3800. * reset engine to proceed with reset sequence
  3801. */
  3802. if (intrs_val & INTRS_TRANSITION_TO_OPERATIONAL)
  3803. pmcraid_clr_trans_op(pinstance);
  3804. /* Clear the interrupt register by writing
  3805. * to host to ioa doorbell. Once done
  3806. * FW will clear the interrupt.
  3807. */
  3808. iowrite32(DOORBELL_INTR_MSIX_CLR,
  3809. pinstance->int_regs.host_ioa_interrupt_reg);
  3810. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  3811. }
  3812. }
  3813. tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
  3814. return IRQ_HANDLED;
  3815. }
  3816. /**
  3817. * pmcraid_isr - implements legacy interrupt handling routine
  3818. *
  3819. * @irq: interrupt vector number
  3820. * @dev_id: pointer hrrq_vector
  3821. *
  3822. * Return Value
  3823. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3824. */
  3825. static irqreturn_t pmcraid_isr(int irq, void *dev_id)
  3826. {
  3827. struct pmcraid_isr_param *hrrq_vector;
  3828. struct pmcraid_instance *pinstance;
  3829. u32 intrs;
  3830. unsigned long lock_flags;
  3831. int hrrq_id = 0;
  3832. /* In case of legacy interrupt mode where interrupts are shared across
  3833. * isrs, it may be possible that the current interrupt is not from IOA
  3834. */
  3835. if (!dev_id) {
  3836. printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
  3837. return IRQ_NONE;
  3838. }
  3839. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3840. pinstance = hrrq_vector->drv_inst;
  3841. intrs = pmcraid_read_interrupts(pinstance);
  3842. if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0))
  3843. return IRQ_NONE;
  3844. /* Any error interrupts including unit_check, initiate IOA reset.
  3845. * In case of unit check indicate to reset_sequence that IOA unit
  3846. * checked and prepare for a dump during reset sequence
  3847. */
  3848. if (intrs & PMCRAID_ERROR_INTERRUPTS) {
  3849. if (intrs & INTRS_IOA_UNIT_CHECK)
  3850. pinstance->ioa_unit_check = 1;
  3851. iowrite32(intrs,
  3852. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3853. pmcraid_err("ISR: error interrupts: %x initiating reset\n",
  3854. intrs);
  3855. intrs = ioread32(
  3856. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3857. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3858. pmcraid_initiate_reset(pinstance);
  3859. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3860. } else {
  3861. /* If interrupt was as part of the ioa initialization,
  3862. * clear. Delete the timer and wakeup the
  3863. * reset engine to proceed with reset sequence
  3864. */
  3865. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  3866. pmcraid_clr_trans_op(pinstance);
  3867. } else {
  3868. iowrite32(intrs,
  3869. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3870. ioread32(
  3871. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3872. tasklet_schedule(
  3873. &(pinstance->isr_tasklet[hrrq_id]));
  3874. }
  3875. }
  3876. return IRQ_HANDLED;
  3877. }
  3878. /**
  3879. * pmcraid_worker_function - worker thread function
  3880. *
  3881. * @workp: pointer to struct work queue
  3882. *
  3883. * Return Value
  3884. * None
  3885. */
  3886. static void pmcraid_worker_function(struct work_struct *workp)
  3887. {
  3888. struct pmcraid_instance *pinstance;
  3889. struct pmcraid_resource_entry *res;
  3890. struct pmcraid_resource_entry *temp;
  3891. struct scsi_device *sdev;
  3892. unsigned long lock_flags;
  3893. unsigned long host_lock_flags;
  3894. u16 fw_version;
  3895. u8 bus, target, lun;
  3896. pinstance = container_of(workp, struct pmcraid_instance, worker_q);
  3897. /* add resources only after host is added into system */
  3898. if (!atomic_read(&pinstance->expose_resources))
  3899. return;
  3900. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3901. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  3902. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
  3903. if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
  3904. sdev = res->scsi_dev;
  3905. /* host_lock must be held before calling
  3906. * scsi_device_get
  3907. */
  3908. spin_lock_irqsave(pinstance->host->host_lock,
  3909. host_lock_flags);
  3910. if (!scsi_device_get(sdev)) {
  3911. spin_unlock_irqrestore(
  3912. pinstance->host->host_lock,
  3913. host_lock_flags);
  3914. pmcraid_info("deleting %x from midlayer\n",
  3915. res->cfg_entry.resource_address);
  3916. list_move_tail(&res->queue,
  3917. &pinstance->free_res_q);
  3918. spin_unlock_irqrestore(
  3919. &pinstance->resource_lock,
  3920. lock_flags);
  3921. scsi_remove_device(sdev);
  3922. scsi_device_put(sdev);
  3923. spin_lock_irqsave(&pinstance->resource_lock,
  3924. lock_flags);
  3925. res->change_detected = 0;
  3926. } else {
  3927. spin_unlock_irqrestore(
  3928. pinstance->host->host_lock,
  3929. host_lock_flags);
  3930. }
  3931. }
  3932. }
  3933. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  3934. if (res->change_detected == RES_CHANGE_ADD) {
  3935. if (!pmcraid_expose_resource(fw_version,
  3936. &res->cfg_entry))
  3937. continue;
  3938. if (RES_IS_VSET(res->cfg_entry)) {
  3939. bus = PMCRAID_VSET_BUS_ID;
  3940. if (fw_version <= PMCRAID_FW_VERSION_1)
  3941. target = res->cfg_entry.unique_flags1;
  3942. else
  3943. target = res->cfg_entry.array_id & 0xFF;
  3944. lun = PMCRAID_VSET_LUN_ID;
  3945. } else {
  3946. bus = PMCRAID_PHYS_BUS_ID;
  3947. target =
  3948. RES_TARGET(
  3949. res->cfg_entry.resource_address);
  3950. lun = RES_LUN(res->cfg_entry.resource_address);
  3951. }
  3952. res->change_detected = 0;
  3953. spin_unlock_irqrestore(&pinstance->resource_lock,
  3954. lock_flags);
  3955. scsi_add_device(pinstance->host, bus, target, lun);
  3956. spin_lock_irqsave(&pinstance->resource_lock,
  3957. lock_flags);
  3958. }
  3959. }
  3960. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  3961. }
  3962. /**
  3963. * pmcraid_tasklet_function - Tasklet function
  3964. *
  3965. * @instance: pointer to msix param structure
  3966. *
  3967. * Return Value
  3968. * None
  3969. */
  3970. static void pmcraid_tasklet_function(unsigned long instance)
  3971. {
  3972. struct pmcraid_isr_param *hrrq_vector;
  3973. struct pmcraid_instance *pinstance;
  3974. unsigned long hrrq_lock_flags;
  3975. unsigned long pending_lock_flags;
  3976. unsigned long host_lock_flags;
  3977. spinlock_t *lockp; /* hrrq buffer lock */
  3978. int id;
  3979. __le32 resp;
  3980. hrrq_vector = (struct pmcraid_isr_param *)instance;
  3981. pinstance = hrrq_vector->drv_inst;
  3982. id = hrrq_vector->hrrq_id;
  3983. lockp = &(pinstance->hrrq_lock[id]);
  3984. /* loop through each of the commands responded by IOA. Each HRRQ buf is
  3985. * protected by its own lock. Traversals must be done within this lock
  3986. * as there may be multiple tasklets running on multiple CPUs. Note
  3987. * that the lock is held just for picking up the response handle and
  3988. * manipulating hrrq_curr/toggle_bit values.
  3989. */
  3990. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3991. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3992. while ((resp & HRRQ_TOGGLE_BIT) ==
  3993. pinstance->host_toggle_bit[id]) {
  3994. int cmd_index = resp >> 2;
  3995. struct pmcraid_cmd *cmd = NULL;
  3996. if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
  3997. pinstance->hrrq_curr[id]++;
  3998. } else {
  3999. pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
  4000. pinstance->host_toggle_bit[id] ^= 1u;
  4001. }
  4002. if (cmd_index >= PMCRAID_MAX_CMD) {
  4003. /* In case of invalid response handle, log message */
  4004. pmcraid_err("Invalid response handle %d\n", cmd_index);
  4005. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  4006. continue;
  4007. }
  4008. cmd = pinstance->cmd_list[cmd_index];
  4009. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  4010. spin_lock_irqsave(&pinstance->pending_pool_lock,
  4011. pending_lock_flags);
  4012. list_del(&cmd->free_list);
  4013. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  4014. pending_lock_flags);
  4015. del_timer(&cmd->timer);
  4016. atomic_dec(&pinstance->outstanding_cmds);
  4017. if (cmd->cmd_done == pmcraid_ioa_reset) {
  4018. spin_lock_irqsave(pinstance->host->host_lock,
  4019. host_lock_flags);
  4020. cmd->cmd_done(cmd);
  4021. spin_unlock_irqrestore(pinstance->host->host_lock,
  4022. host_lock_flags);
  4023. } else if (cmd->cmd_done != NULL) {
  4024. cmd->cmd_done(cmd);
  4025. }
  4026. /* loop over until we are done with all responses */
  4027. spin_lock_irqsave(lockp, hrrq_lock_flags);
  4028. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  4029. }
  4030. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  4031. }
  4032. /**
  4033. * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
  4034. * @pinstance: pointer to adapter instance structure
  4035. *
  4036. * This routine un-registers registered interrupt handler and
  4037. * also frees irqs/vectors.
  4038. *
  4039. * Retun Value
  4040. * None
  4041. */
  4042. static
  4043. void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
  4044. {
  4045. int i;
  4046. for (i = 0; i < pinstance->num_hrrq; i++)
  4047. free_irq(pinstance->hrrq_vector[i].vector,
  4048. &(pinstance->hrrq_vector[i]));
  4049. if (pinstance->interrupt_mode) {
  4050. pci_disable_msix(pinstance->pdev);
  4051. pinstance->interrupt_mode = 0;
  4052. }
  4053. }
  4054. /**
  4055. * pmcraid_register_interrupt_handler - registers interrupt handler
  4056. * @pinstance: pointer to per-adapter instance structure
  4057. *
  4058. * Return Value
  4059. * 0 on success, non-zero error code otherwise.
  4060. */
  4061. static int
  4062. pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
  4063. {
  4064. int rc;
  4065. struct pci_dev *pdev = pinstance->pdev;
  4066. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  4067. int num_hrrq = PMCRAID_NUM_MSIX_VECTORS;
  4068. struct msix_entry entries[PMCRAID_NUM_MSIX_VECTORS];
  4069. int i;
  4070. for (i = 0; i < PMCRAID_NUM_MSIX_VECTORS; i++)
  4071. entries[i].entry = i;
  4072. rc = pci_enable_msix(pdev, entries, num_hrrq);
  4073. if (rc < 0)
  4074. goto pmcraid_isr_legacy;
  4075. /* Check how many MSIX vectors are allocated and register
  4076. * msi-x handlers for each of them giving appropriate buffer
  4077. */
  4078. if (rc > 0) {
  4079. num_hrrq = rc;
  4080. if (pci_enable_msix(pdev, entries, num_hrrq))
  4081. goto pmcraid_isr_legacy;
  4082. }
  4083. for (i = 0; i < num_hrrq; i++) {
  4084. pinstance->hrrq_vector[i].hrrq_id = i;
  4085. pinstance->hrrq_vector[i].drv_inst = pinstance;
  4086. pinstance->hrrq_vector[i].vector = entries[i].vector;
  4087. rc = request_irq(pinstance->hrrq_vector[i].vector,
  4088. pmcraid_isr_msix, 0,
  4089. PMCRAID_DRIVER_NAME,
  4090. &(pinstance->hrrq_vector[i]));
  4091. if (rc) {
  4092. int j;
  4093. for (j = 0; j < i; j++)
  4094. free_irq(entries[j].vector,
  4095. &(pinstance->hrrq_vector[j]));
  4096. pci_disable_msix(pdev);
  4097. goto pmcraid_isr_legacy;
  4098. }
  4099. }
  4100. pinstance->num_hrrq = num_hrrq;
  4101. pinstance->interrupt_mode = 1;
  4102. iowrite32(DOORBELL_INTR_MODE_MSIX,
  4103. pinstance->int_regs.host_ioa_interrupt_reg);
  4104. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  4105. goto pmcraid_isr_out;
  4106. }
  4107. pmcraid_isr_legacy:
  4108. /* If MSI-X registration failed fallback to legacy mode, where
  4109. * only one hrrq entry will be used
  4110. */
  4111. pinstance->hrrq_vector[0].hrrq_id = 0;
  4112. pinstance->hrrq_vector[0].drv_inst = pinstance;
  4113. pinstance->hrrq_vector[0].vector = pdev->irq;
  4114. pinstance->num_hrrq = 1;
  4115. rc = 0;
  4116. rc = request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
  4117. PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
  4118. pmcraid_isr_out:
  4119. return rc;
  4120. }
  4121. /**
  4122. * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
  4123. * @pinstance: per adapter instance structure pointer
  4124. * @max_index: number of buffer blocks to release
  4125. *
  4126. * Return Value
  4127. * None
  4128. */
  4129. static void
  4130. pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
  4131. {
  4132. int i;
  4133. for (i = 0; i < max_index; i++) {
  4134. kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
  4135. pinstance->cmd_list[i] = NULL;
  4136. }
  4137. kmem_cache_destroy(pinstance->cmd_cachep);
  4138. pinstance->cmd_cachep = NULL;
  4139. }
  4140. /**
  4141. * pmcraid_release_control_blocks - releases buffers alloced for control blocks
  4142. * @pinstance: pointer to per adapter instance structure
  4143. * @max_index: number of buffers (from 0 onwards) to release
  4144. *
  4145. * This function assumes that the command blocks for which control blocks are
  4146. * linked are not released.
  4147. *
  4148. * Return Value
  4149. * None
  4150. */
  4151. static void
  4152. pmcraid_release_control_blocks(
  4153. struct pmcraid_instance *pinstance,
  4154. int max_index
  4155. )
  4156. {
  4157. int i;
  4158. if (pinstance->control_pool == NULL)
  4159. return;
  4160. for (i = 0; i < max_index; i++) {
  4161. pci_pool_free(pinstance->control_pool,
  4162. pinstance->cmd_list[i]->ioa_cb,
  4163. pinstance->cmd_list[i]->ioa_cb_bus_addr);
  4164. pinstance->cmd_list[i]->ioa_cb = NULL;
  4165. pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
  4166. }
  4167. pci_pool_destroy(pinstance->control_pool);
  4168. pinstance->control_pool = NULL;
  4169. }
  4170. /**
  4171. * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
  4172. * @pinstance - pointer to per adapter instance structure
  4173. *
  4174. * Allocates memory for command blocks using kernel slab allocator.
  4175. *
  4176. * Return Value
  4177. * 0 in case of success; -ENOMEM in case of failure
  4178. */
  4179. static int __devinit
  4180. pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
  4181. {
  4182. int i;
  4183. sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
  4184. pinstance->host->unique_id);
  4185. pinstance->cmd_cachep = kmem_cache_create(
  4186. pinstance->cmd_pool_name,
  4187. sizeof(struct pmcraid_cmd), 0,
  4188. SLAB_HWCACHE_ALIGN, NULL);
  4189. if (!pinstance->cmd_cachep)
  4190. return -ENOMEM;
  4191. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4192. pinstance->cmd_list[i] =
  4193. kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
  4194. if (!pinstance->cmd_list[i]) {
  4195. pmcraid_release_cmd_blocks(pinstance, i);
  4196. return -ENOMEM;
  4197. }
  4198. }
  4199. return 0;
  4200. }
  4201. /**
  4202. * pmcraid_allocate_control_blocks - allocates memory control blocks
  4203. * @pinstance : pointer to per adapter instance structure
  4204. *
  4205. * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
  4206. * and IOASAs. This is called after command blocks are already allocated.
  4207. *
  4208. * Return Value
  4209. * 0 in case it can allocate all control blocks, otherwise -ENOMEM
  4210. */
  4211. static int __devinit
  4212. pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
  4213. {
  4214. int i;
  4215. sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
  4216. pinstance->host->unique_id);
  4217. pinstance->control_pool =
  4218. pci_pool_create(pinstance->ctl_pool_name,
  4219. pinstance->pdev,
  4220. sizeof(struct pmcraid_control_block),
  4221. PMCRAID_IOARCB_ALIGNMENT, 0);
  4222. if (!pinstance->control_pool)
  4223. return -ENOMEM;
  4224. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4225. pinstance->cmd_list[i]->ioa_cb =
  4226. pci_pool_alloc(
  4227. pinstance->control_pool,
  4228. GFP_KERNEL,
  4229. &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
  4230. if (!pinstance->cmd_list[i]->ioa_cb) {
  4231. pmcraid_release_control_blocks(pinstance, i);
  4232. return -ENOMEM;
  4233. }
  4234. memset(pinstance->cmd_list[i]->ioa_cb, 0,
  4235. sizeof(struct pmcraid_control_block));
  4236. }
  4237. return 0;
  4238. }
  4239. /**
  4240. * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
  4241. * @pinstance: pointer to per adapter instance structure
  4242. * @maxindex: size of hrrq buffer pointer array
  4243. *
  4244. * Return Value
  4245. * None
  4246. */
  4247. static void
  4248. pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
  4249. {
  4250. int i;
  4251. for (i = 0; i < maxindex; i++) {
  4252. pci_free_consistent(pinstance->pdev,
  4253. HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
  4254. pinstance->hrrq_start[i],
  4255. pinstance->hrrq_start_bus_addr[i]);
  4256. /* reset pointers and toggle bit to zeros */
  4257. pinstance->hrrq_start[i] = NULL;
  4258. pinstance->hrrq_start_bus_addr[i] = 0;
  4259. pinstance->host_toggle_bit[i] = 0;
  4260. }
  4261. }
  4262. /**
  4263. * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
  4264. * @pinstance: pointer to per adapter instance structure
  4265. *
  4266. * Return value
  4267. * 0 hrrq buffers are allocated, -ENOMEM otherwise.
  4268. */
  4269. static int __devinit
  4270. pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
  4271. {
  4272. int i, buffer_size;
  4273. buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4274. for (i = 0; i < pinstance->num_hrrq; i++) {
  4275. pinstance->hrrq_start[i] =
  4276. pci_alloc_consistent(
  4277. pinstance->pdev,
  4278. buffer_size,
  4279. &(pinstance->hrrq_start_bus_addr[i]));
  4280. if (pinstance->hrrq_start[i] == 0) {
  4281. pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
  4282. i);
  4283. pmcraid_release_host_rrqs(pinstance, i);
  4284. return -ENOMEM;
  4285. }
  4286. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4287. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4288. pinstance->hrrq_end[i] =
  4289. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4290. pinstance->host_toggle_bit[i] = 1;
  4291. spin_lock_init(&pinstance->hrrq_lock[i]);
  4292. }
  4293. return 0;
  4294. }
  4295. /**
  4296. * pmcraid_release_hcams - release HCAM buffers
  4297. *
  4298. * @pinstance: pointer to per adapter instance structure
  4299. *
  4300. * Return value
  4301. * none
  4302. */
  4303. static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
  4304. {
  4305. if (pinstance->ccn.msg != NULL) {
  4306. pci_free_consistent(pinstance->pdev,
  4307. PMCRAID_AEN_HDR_SIZE +
  4308. sizeof(struct pmcraid_hcam_ccn_ext),
  4309. pinstance->ccn.msg,
  4310. pinstance->ccn.baddr);
  4311. pinstance->ccn.msg = NULL;
  4312. pinstance->ccn.hcam = NULL;
  4313. pinstance->ccn.baddr = 0;
  4314. }
  4315. if (pinstance->ldn.msg != NULL) {
  4316. pci_free_consistent(pinstance->pdev,
  4317. PMCRAID_AEN_HDR_SIZE +
  4318. sizeof(struct pmcraid_hcam_ldn),
  4319. pinstance->ldn.msg,
  4320. pinstance->ldn.baddr);
  4321. pinstance->ldn.msg = NULL;
  4322. pinstance->ldn.hcam = NULL;
  4323. pinstance->ldn.baddr = 0;
  4324. }
  4325. }
  4326. /**
  4327. * pmcraid_allocate_hcams - allocates HCAM buffers
  4328. * @pinstance : pointer to per adapter instance structure
  4329. *
  4330. * Return Value:
  4331. * 0 in case of successful allocation, non-zero otherwise
  4332. */
  4333. static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
  4334. {
  4335. pinstance->ccn.msg = pci_alloc_consistent(
  4336. pinstance->pdev,
  4337. PMCRAID_AEN_HDR_SIZE +
  4338. sizeof(struct pmcraid_hcam_ccn_ext),
  4339. &(pinstance->ccn.baddr));
  4340. pinstance->ldn.msg = pci_alloc_consistent(
  4341. pinstance->pdev,
  4342. PMCRAID_AEN_HDR_SIZE +
  4343. sizeof(struct pmcraid_hcam_ldn),
  4344. &(pinstance->ldn.baddr));
  4345. if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
  4346. pmcraid_release_hcams(pinstance);
  4347. } else {
  4348. pinstance->ccn.hcam =
  4349. (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
  4350. pinstance->ldn.hcam =
  4351. (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
  4352. atomic_set(&pinstance->ccn.ignore, 0);
  4353. atomic_set(&pinstance->ldn.ignore, 0);
  4354. }
  4355. return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
  4356. }
  4357. /**
  4358. * pmcraid_release_config_buffers - release config.table buffers
  4359. * @pinstance: pointer to per adapter instance structure
  4360. *
  4361. * Return Value
  4362. * none
  4363. */
  4364. static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
  4365. {
  4366. if (pinstance->cfg_table != NULL &&
  4367. pinstance->cfg_table_bus_addr != 0) {
  4368. pci_free_consistent(pinstance->pdev,
  4369. sizeof(struct pmcraid_config_table),
  4370. pinstance->cfg_table,
  4371. pinstance->cfg_table_bus_addr);
  4372. pinstance->cfg_table = NULL;
  4373. pinstance->cfg_table_bus_addr = 0;
  4374. }
  4375. if (pinstance->res_entries != NULL) {
  4376. int i;
  4377. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4378. list_del(&pinstance->res_entries[i].queue);
  4379. kfree(pinstance->res_entries);
  4380. pinstance->res_entries = NULL;
  4381. }
  4382. pmcraid_release_hcams(pinstance);
  4383. }
  4384. /**
  4385. * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
  4386. * @pinstance : pointer to per adapter instance structure
  4387. *
  4388. * Return Value
  4389. * 0 for successful allocation, -ENOMEM for any failure
  4390. */
  4391. static int __devinit
  4392. pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
  4393. {
  4394. int i;
  4395. pinstance->res_entries =
  4396. kzalloc(sizeof(struct pmcraid_resource_entry) *
  4397. PMCRAID_MAX_RESOURCES, GFP_KERNEL);
  4398. if (NULL == pinstance->res_entries) {
  4399. pmcraid_err("failed to allocate memory for resource table\n");
  4400. return -ENOMEM;
  4401. }
  4402. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4403. list_add_tail(&pinstance->res_entries[i].queue,
  4404. &pinstance->free_res_q);
  4405. pinstance->cfg_table =
  4406. pci_alloc_consistent(pinstance->pdev,
  4407. sizeof(struct pmcraid_config_table),
  4408. &pinstance->cfg_table_bus_addr);
  4409. if (NULL == pinstance->cfg_table) {
  4410. pmcraid_err("couldn't alloc DMA memory for config table\n");
  4411. pmcraid_release_config_buffers(pinstance);
  4412. return -ENOMEM;
  4413. }
  4414. if (pmcraid_allocate_hcams(pinstance)) {
  4415. pmcraid_err("could not alloc DMA memory for HCAMS\n");
  4416. pmcraid_release_config_buffers(pinstance);
  4417. return -ENOMEM;
  4418. }
  4419. return 0;
  4420. }
  4421. /**
  4422. * pmcraid_init_tasklets - registers tasklets for response handling
  4423. *
  4424. * @pinstance: pointer adapter instance structure
  4425. *
  4426. * Return value
  4427. * none
  4428. */
  4429. static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
  4430. {
  4431. int i;
  4432. for (i = 0; i < pinstance->num_hrrq; i++)
  4433. tasklet_init(&pinstance->isr_tasklet[i],
  4434. pmcraid_tasklet_function,
  4435. (unsigned long)&pinstance->hrrq_vector[i]);
  4436. }
  4437. /**
  4438. * pmcraid_kill_tasklets - destroys tasklets registered for response handling
  4439. *
  4440. * @pinstance: pointer to adapter instance structure
  4441. *
  4442. * Return value
  4443. * none
  4444. */
  4445. static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
  4446. {
  4447. int i;
  4448. for (i = 0; i < pinstance->num_hrrq; i++)
  4449. tasklet_kill(&pinstance->isr_tasklet[i]);
  4450. }
  4451. /**
  4452. * pmcraid_release_buffers - release per-adapter buffers allocated
  4453. *
  4454. * @pinstance: pointer to adapter soft state
  4455. *
  4456. * Return Value
  4457. * none
  4458. */
  4459. static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
  4460. {
  4461. pmcraid_release_config_buffers(pinstance);
  4462. pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
  4463. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4464. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4465. if (pinstance->inq_data != NULL) {
  4466. pci_free_consistent(pinstance->pdev,
  4467. sizeof(struct pmcraid_inquiry_data),
  4468. pinstance->inq_data,
  4469. pinstance->inq_data_baddr);
  4470. pinstance->inq_data = NULL;
  4471. pinstance->inq_data_baddr = 0;
  4472. }
  4473. if (pinstance->timestamp_data != NULL) {
  4474. pci_free_consistent(pinstance->pdev,
  4475. sizeof(struct pmcraid_timestamp_data),
  4476. pinstance->timestamp_data,
  4477. pinstance->timestamp_data_baddr);
  4478. pinstance->timestamp_data = NULL;
  4479. pinstance->timestamp_data_baddr = 0;
  4480. }
  4481. }
  4482. /**
  4483. * pmcraid_init_buffers - allocates memory and initializes various structures
  4484. * @pinstance: pointer to per adapter instance structure
  4485. *
  4486. * This routine pre-allocates memory based on the type of block as below:
  4487. * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
  4488. * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
  4489. * config-table entries : DMAable memory using pci_alloc_consistent
  4490. * HostRRQs : DMAable memory, using pci_alloc_consistent
  4491. *
  4492. * Return Value
  4493. * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
  4494. */
  4495. static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
  4496. {
  4497. int i;
  4498. if (pmcraid_allocate_host_rrqs(pinstance)) {
  4499. pmcraid_err("couldn't allocate memory for %d host rrqs\n",
  4500. pinstance->num_hrrq);
  4501. return -ENOMEM;
  4502. }
  4503. if (pmcraid_allocate_config_buffers(pinstance)) {
  4504. pmcraid_err("couldn't allocate memory for config buffers\n");
  4505. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4506. return -ENOMEM;
  4507. }
  4508. if (pmcraid_allocate_cmd_blocks(pinstance)) {
  4509. pmcraid_err("couldn't allocate memory for cmd blocks\n");
  4510. pmcraid_release_config_buffers(pinstance);
  4511. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4512. return -ENOMEM;
  4513. }
  4514. if (pmcraid_allocate_control_blocks(pinstance)) {
  4515. pmcraid_err("couldn't allocate memory control blocks\n");
  4516. pmcraid_release_config_buffers(pinstance);
  4517. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4518. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4519. return -ENOMEM;
  4520. }
  4521. /* allocate DMAable memory for page D0 INQUIRY buffer */
  4522. pinstance->inq_data = pci_alloc_consistent(
  4523. pinstance->pdev,
  4524. sizeof(struct pmcraid_inquiry_data),
  4525. &pinstance->inq_data_baddr);
  4526. if (pinstance->inq_data == NULL) {
  4527. pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
  4528. pmcraid_release_buffers(pinstance);
  4529. return -ENOMEM;
  4530. }
  4531. /* allocate DMAable memory for set timestamp data buffer */
  4532. pinstance->timestamp_data = pci_alloc_consistent(
  4533. pinstance->pdev,
  4534. sizeof(struct pmcraid_timestamp_data),
  4535. &pinstance->timestamp_data_baddr);
  4536. if (pinstance->timestamp_data == NULL) {
  4537. pmcraid_err("couldn't allocate DMA memory for \
  4538. set time_stamp \n");
  4539. pmcraid_release_buffers(pinstance);
  4540. return -ENOMEM;
  4541. }
  4542. /* Initialize all the command blocks and add them to free pool. No
  4543. * need to lock (free_pool_lock) as this is done in initialization
  4544. * itself
  4545. */
  4546. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4547. struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
  4548. pmcraid_init_cmdblk(cmdp, i);
  4549. cmdp->drv_inst = pinstance;
  4550. list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
  4551. }
  4552. return 0;
  4553. }
  4554. /**
  4555. * pmcraid_reinit_buffers - resets various buffer pointers
  4556. * @pinstance: pointer to adapter instance
  4557. * Return value
  4558. * none
  4559. */
  4560. static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
  4561. {
  4562. int i;
  4563. int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4564. for (i = 0; i < pinstance->num_hrrq; i++) {
  4565. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4566. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4567. pinstance->hrrq_end[i] =
  4568. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4569. pinstance->host_toggle_bit[i] = 1;
  4570. }
  4571. }
  4572. /**
  4573. * pmcraid_init_instance - initialize per instance data structure
  4574. * @pdev: pointer to pci device structure
  4575. * @host: pointer to Scsi_Host structure
  4576. * @mapped_pci_addr: memory mapped IOA configuration registers
  4577. *
  4578. * Return Value
  4579. * 0 on success, non-zero in case of any failure
  4580. */
  4581. static int __devinit pmcraid_init_instance(
  4582. struct pci_dev *pdev,
  4583. struct Scsi_Host *host,
  4584. void __iomem *mapped_pci_addr
  4585. )
  4586. {
  4587. struct pmcraid_instance *pinstance =
  4588. (struct pmcraid_instance *)host->hostdata;
  4589. pinstance->host = host;
  4590. pinstance->pdev = pdev;
  4591. /* Initialize register addresses */
  4592. pinstance->mapped_dma_addr = mapped_pci_addr;
  4593. /* Initialize chip-specific details */
  4594. {
  4595. struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
  4596. struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
  4597. pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
  4598. pint_regs->ioa_host_interrupt_reg =
  4599. mapped_pci_addr + chip_cfg->ioa_host_intr;
  4600. pint_regs->ioa_host_interrupt_clr_reg =
  4601. mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
  4602. pint_regs->ioa_host_msix_interrupt_reg =
  4603. mapped_pci_addr + chip_cfg->ioa_host_msix_intr;
  4604. pint_regs->host_ioa_interrupt_reg =
  4605. mapped_pci_addr + chip_cfg->host_ioa_intr;
  4606. pint_regs->host_ioa_interrupt_clr_reg =
  4607. mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
  4608. /* Current version of firmware exposes interrupt mask set
  4609. * and mask clr registers through memory mapped bar0.
  4610. */
  4611. pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
  4612. pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
  4613. pint_regs->ioa_host_interrupt_mask_reg =
  4614. mapped_pci_addr + chip_cfg->ioa_host_mask;
  4615. pint_regs->ioa_host_interrupt_mask_clr_reg =
  4616. mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
  4617. pint_regs->global_interrupt_mask_reg =
  4618. mapped_pci_addr + chip_cfg->global_intr_mask;
  4619. };
  4620. pinstance->ioa_reset_attempts = 0;
  4621. init_waitqueue_head(&pinstance->reset_wait_q);
  4622. atomic_set(&pinstance->outstanding_cmds, 0);
  4623. atomic_set(&pinstance->last_message_id, 0);
  4624. atomic_set(&pinstance->expose_resources, 0);
  4625. INIT_LIST_HEAD(&pinstance->free_res_q);
  4626. INIT_LIST_HEAD(&pinstance->used_res_q);
  4627. INIT_LIST_HEAD(&pinstance->free_cmd_pool);
  4628. INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
  4629. spin_lock_init(&pinstance->free_pool_lock);
  4630. spin_lock_init(&pinstance->pending_pool_lock);
  4631. spin_lock_init(&pinstance->resource_lock);
  4632. mutex_init(&pinstance->aen_queue_lock);
  4633. /* Work-queue (Shared) for deferred processing error handling */
  4634. INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
  4635. /* Initialize the default log_level */
  4636. pinstance->current_log_level = pmcraid_log_level;
  4637. /* Setup variables required for reset engine */
  4638. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  4639. pinstance->reset_cmd = NULL;
  4640. return 0;
  4641. }
  4642. /**
  4643. * pmcraid_shutdown - shutdown adapter controller.
  4644. * @pdev: pci device struct
  4645. *
  4646. * Issues an adapter shutdown to the card waits for its completion
  4647. *
  4648. * Return value
  4649. * none
  4650. */
  4651. static void pmcraid_shutdown(struct pci_dev *pdev)
  4652. {
  4653. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4654. pmcraid_reset_bringdown(pinstance);
  4655. }
  4656. /**
  4657. * pmcraid_get_minor - returns unused minor number from minor number bitmap
  4658. */
  4659. static unsigned short pmcraid_get_minor(void)
  4660. {
  4661. int minor;
  4662. minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
  4663. __set_bit(minor, pmcraid_minor);
  4664. return minor;
  4665. }
  4666. /**
  4667. * pmcraid_release_minor - releases given minor back to minor number bitmap
  4668. */
  4669. static void pmcraid_release_minor(unsigned short minor)
  4670. {
  4671. __clear_bit(minor, pmcraid_minor);
  4672. }
  4673. /**
  4674. * pmcraid_setup_chrdev - allocates a minor number and registers a char device
  4675. *
  4676. * @pinstance: pointer to adapter instance for which to register device
  4677. *
  4678. * Return value
  4679. * 0 in case of success, otherwise non-zero
  4680. */
  4681. static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
  4682. {
  4683. int minor;
  4684. int error;
  4685. minor = pmcraid_get_minor();
  4686. cdev_init(&pinstance->cdev, &pmcraid_fops);
  4687. pinstance->cdev.owner = THIS_MODULE;
  4688. error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
  4689. if (error)
  4690. pmcraid_release_minor(minor);
  4691. else
  4692. device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
  4693. NULL, "%s%u", PMCRAID_DEVFILE, minor);
  4694. return error;
  4695. }
  4696. /**
  4697. * pmcraid_release_chrdev - unregisters per-adapter management interface
  4698. *
  4699. * @pinstance: pointer to adapter instance structure
  4700. *
  4701. * Return value
  4702. * none
  4703. */
  4704. static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
  4705. {
  4706. pmcraid_release_minor(MINOR(pinstance->cdev.dev));
  4707. device_destroy(pmcraid_class,
  4708. MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
  4709. cdev_del(&pinstance->cdev);
  4710. }
  4711. /**
  4712. * pmcraid_remove - IOA hot plug remove entry point
  4713. * @pdev: pci device struct
  4714. *
  4715. * Return value
  4716. * none
  4717. */
  4718. static void __devexit pmcraid_remove(struct pci_dev *pdev)
  4719. {
  4720. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4721. /* remove the management interface (/dev file) for this device */
  4722. pmcraid_release_chrdev(pinstance);
  4723. /* remove host template from scsi midlayer */
  4724. scsi_remove_host(pinstance->host);
  4725. /* block requests from mid-layer */
  4726. scsi_block_requests(pinstance->host);
  4727. /* initiate shutdown adapter */
  4728. pmcraid_shutdown(pdev);
  4729. pmcraid_disable_interrupts(pinstance, ~0);
  4730. flush_scheduled_work();
  4731. pmcraid_kill_tasklets(pinstance);
  4732. pmcraid_unregister_interrupt_handler(pinstance);
  4733. pmcraid_release_buffers(pinstance);
  4734. iounmap(pinstance->mapped_dma_addr);
  4735. pci_release_regions(pdev);
  4736. scsi_host_put(pinstance->host);
  4737. pci_disable_device(pdev);
  4738. return;
  4739. }
  4740. #ifdef CONFIG_PM
  4741. /**
  4742. * pmcraid_suspend - driver suspend entry point for power management
  4743. * @pdev: PCI device structure
  4744. * @state: PCI power state to suspend routine
  4745. *
  4746. * Return Value - 0 always
  4747. */
  4748. static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
  4749. {
  4750. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4751. pmcraid_shutdown(pdev);
  4752. pmcraid_disable_interrupts(pinstance, ~0);
  4753. pmcraid_kill_tasklets(pinstance);
  4754. pci_set_drvdata(pinstance->pdev, pinstance);
  4755. pmcraid_unregister_interrupt_handler(pinstance);
  4756. pci_save_state(pdev);
  4757. pci_disable_device(pdev);
  4758. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  4759. return 0;
  4760. }
  4761. /**
  4762. * pmcraid_resume - driver resume entry point PCI power management
  4763. * @pdev: PCI device structure
  4764. *
  4765. * Return Value - 0 in case of success. Error code in case of any failure
  4766. */
  4767. static int pmcraid_resume(struct pci_dev *pdev)
  4768. {
  4769. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4770. struct Scsi_Host *host = pinstance->host;
  4771. int rc;
  4772. pci_set_power_state(pdev, PCI_D0);
  4773. pci_enable_wake(pdev, PCI_D0, 0);
  4774. pci_restore_state(pdev);
  4775. rc = pci_enable_device(pdev);
  4776. if (rc) {
  4777. dev_err(&pdev->dev, "resume: Enable device failed\n");
  4778. return rc;
  4779. }
  4780. pci_set_master(pdev);
  4781. if ((sizeof(dma_addr_t) == 4) ||
  4782. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4783. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4784. if (rc == 0)
  4785. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4786. if (rc != 0) {
  4787. dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
  4788. goto disable_device;
  4789. }
  4790. pmcraid_disable_interrupts(pinstance, ~0);
  4791. atomic_set(&pinstance->outstanding_cmds, 0);
  4792. rc = pmcraid_register_interrupt_handler(pinstance);
  4793. if (rc) {
  4794. dev_err(&pdev->dev,
  4795. "resume: couldn't register interrupt handlers\n");
  4796. rc = -ENODEV;
  4797. goto release_host;
  4798. }
  4799. pmcraid_init_tasklets(pinstance);
  4800. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4801. /* Start with hard reset sequence which brings up IOA to operational
  4802. * state as well as completes the reset sequence.
  4803. */
  4804. pinstance->ioa_hard_reset = 1;
  4805. /* Start IOA firmware initialization and bring card to Operational
  4806. * state.
  4807. */
  4808. if (pmcraid_reset_bringup(pinstance)) {
  4809. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  4810. rc = -ENODEV;
  4811. goto release_tasklets;
  4812. }
  4813. return 0;
  4814. release_tasklets:
  4815. pmcraid_disable_interrupts(pinstance, ~0);
  4816. pmcraid_kill_tasklets(pinstance);
  4817. pmcraid_unregister_interrupt_handler(pinstance);
  4818. release_host:
  4819. scsi_host_put(host);
  4820. disable_device:
  4821. pci_disable_device(pdev);
  4822. return rc;
  4823. }
  4824. #else
  4825. #define pmcraid_suspend NULL
  4826. #define pmcraid_resume NULL
  4827. #endif /* CONFIG_PM */
  4828. /**
  4829. * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
  4830. * completion of the ioa reset
  4831. * @cmd: pointer to reset command block
  4832. */
  4833. static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
  4834. {
  4835. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4836. unsigned long flags;
  4837. spin_lock_irqsave(pinstance->host->host_lock, flags);
  4838. pmcraid_ioa_reset(cmd);
  4839. spin_unlock_irqrestore(pinstance->host->host_lock, flags);
  4840. scsi_unblock_requests(pinstance->host);
  4841. schedule_work(&pinstance->worker_q);
  4842. }
  4843. /**
  4844. * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
  4845. *
  4846. * @cmd: pointer to pmcraid_cmd structure
  4847. *
  4848. * Return Value
  4849. * 0 for success or non-zero for failure cases
  4850. */
  4851. static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
  4852. {
  4853. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4854. void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
  4855. pmcraid_reinit_cmdblk(cmd);
  4856. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4857. ioarcb->request_type = REQ_TYPE_IOACMD;
  4858. ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
  4859. ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
  4860. /* If this was called as part of resource table reinitialization due to
  4861. * lost CCN, it is enough to return the command block back to free pool
  4862. * as part of set_supported_devs completion function.
  4863. */
  4864. if (cmd->drv_inst->reinit_cfg_table) {
  4865. cmd->drv_inst->reinit_cfg_table = 0;
  4866. cmd->release = 1;
  4867. cmd_done = pmcraid_reinit_cfgtable_done;
  4868. }
  4869. /* we will be done with the reset sequence after set supported devices,
  4870. * setup the done function to return the command block back to free
  4871. * pool
  4872. */
  4873. pmcraid_send_cmd(cmd,
  4874. cmd_done,
  4875. PMCRAID_SET_SUP_DEV_TIMEOUT,
  4876. pmcraid_timeout_handler);
  4877. return;
  4878. }
  4879. /**
  4880. * pmcraid_set_timestamp - set the timestamp to IOAFP
  4881. *
  4882. * @cmd: pointer to pmcraid_cmd structure
  4883. *
  4884. * Return Value
  4885. * 0 for success or non-zero for failure cases
  4886. */
  4887. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd)
  4888. {
  4889. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4890. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4891. __be32 time_stamp_len = cpu_to_be32(PMCRAID_TIMESTAMP_LEN);
  4892. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4893. struct timeval tv;
  4894. __le64 timestamp;
  4895. do_gettimeofday(&tv);
  4896. timestamp = tv.tv_sec * 1000;
  4897. pinstance->timestamp_data->timestamp[0] = (__u8)(timestamp);
  4898. pinstance->timestamp_data->timestamp[1] = (__u8)((timestamp) >> 8);
  4899. pinstance->timestamp_data->timestamp[2] = (__u8)((timestamp) >> 16);
  4900. pinstance->timestamp_data->timestamp[3] = (__u8)((timestamp) >> 24);
  4901. pinstance->timestamp_data->timestamp[4] = (__u8)((timestamp) >> 32);
  4902. pinstance->timestamp_data->timestamp[5] = (__u8)((timestamp) >> 40);
  4903. pmcraid_reinit_cmdblk(cmd);
  4904. ioarcb->request_type = REQ_TYPE_SCSI;
  4905. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4906. ioarcb->cdb[0] = PMCRAID_SCSI_SET_TIMESTAMP;
  4907. ioarcb->cdb[1] = PMCRAID_SCSI_SERVICE_ACTION;
  4908. memcpy(&(ioarcb->cdb[6]), &time_stamp_len, sizeof(time_stamp_len));
  4909. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4910. offsetof(struct pmcraid_ioarcb,
  4911. add_data.u.ioadl[0]));
  4912. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4913. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  4914. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4915. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  4916. ioarcb->data_transfer_length =
  4917. cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4918. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4919. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4920. ioadl->address = cpu_to_le64(pinstance->timestamp_data_baddr);
  4921. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4922. if (!pinstance->timestamp_error) {
  4923. pinstance->timestamp_error = 0;
  4924. pmcraid_send_cmd(cmd, pmcraid_set_supported_devs,
  4925. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4926. } else {
  4927. pmcraid_send_cmd(cmd, pmcraid_return_cmd,
  4928. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4929. return;
  4930. }
  4931. }
  4932. /**
  4933. * pmcraid_init_res_table - Initialize the resource table
  4934. * @cmd: pointer to pmcraid command struct
  4935. *
  4936. * This function looks through the existing resource table, comparing
  4937. * it with the config table. This function will take care of old/new
  4938. * devices and schedule adding/removing them from the mid-layer
  4939. * as appropriate.
  4940. *
  4941. * Return value
  4942. * None
  4943. */
  4944. static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
  4945. {
  4946. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4947. struct pmcraid_resource_entry *res, *temp;
  4948. struct pmcraid_config_table_entry *cfgte;
  4949. unsigned long lock_flags;
  4950. int found, rc, i;
  4951. u16 fw_version;
  4952. LIST_HEAD(old_res);
  4953. if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
  4954. pmcraid_err("IOA requires microcode download\n");
  4955. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  4956. /* resource list is protected by pinstance->resource_lock.
  4957. * init_res_table can be called from probe (user-thread) or runtime
  4958. * reset (timer/tasklet)
  4959. */
  4960. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  4961. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
  4962. list_move_tail(&res->queue, &old_res);
  4963. for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
  4964. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4965. PMCRAID_FW_VERSION_1)
  4966. cfgte = &pinstance->cfg_table->entries[i];
  4967. else
  4968. cfgte = (struct pmcraid_config_table_entry *)
  4969. &pinstance->cfg_table->entries_ext[i];
  4970. if (!pmcraid_expose_resource(fw_version, cfgte))
  4971. continue;
  4972. found = 0;
  4973. /* If this entry was already detected and initialized */
  4974. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4975. rc = memcmp(&res->cfg_entry.resource_address,
  4976. &cfgte->resource_address,
  4977. sizeof(cfgte->resource_address));
  4978. if (!rc) {
  4979. list_move_tail(&res->queue,
  4980. &pinstance->used_res_q);
  4981. found = 1;
  4982. break;
  4983. }
  4984. }
  4985. /* If this is new entry, initialize it and add it the queue */
  4986. if (!found) {
  4987. if (list_empty(&pinstance->free_res_q)) {
  4988. pmcraid_err("Too many devices attached\n");
  4989. break;
  4990. }
  4991. found = 1;
  4992. res = list_entry(pinstance->free_res_q.next,
  4993. struct pmcraid_resource_entry, queue);
  4994. res->scsi_dev = NULL;
  4995. res->change_detected = RES_CHANGE_ADD;
  4996. res->reset_progress = 0;
  4997. list_move_tail(&res->queue, &pinstance->used_res_q);
  4998. }
  4999. /* copy new configuration table entry details into driver
  5000. * maintained resource entry
  5001. */
  5002. if (found) {
  5003. memcpy(&res->cfg_entry, cfgte,
  5004. pinstance->config_table_entry_size);
  5005. pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
  5006. res->cfg_entry.resource_type,
  5007. (fw_version <= PMCRAID_FW_VERSION_1 ?
  5008. res->cfg_entry.unique_flags1 :
  5009. res->cfg_entry.array_id & 0xFF),
  5010. le32_to_cpu(res->cfg_entry.resource_address));
  5011. }
  5012. }
  5013. /* Detect any deleted entries, mark them for deletion from mid-layer */
  5014. list_for_each_entry_safe(res, temp, &old_res, queue) {
  5015. if (res->scsi_dev) {
  5016. res->change_detected = RES_CHANGE_DEL;
  5017. res->cfg_entry.resource_handle =
  5018. PMCRAID_INVALID_RES_HANDLE;
  5019. list_move_tail(&res->queue, &pinstance->used_res_q);
  5020. } else {
  5021. list_move_tail(&res->queue, &pinstance->free_res_q);
  5022. }
  5023. }
  5024. /* release the resource list lock */
  5025. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  5026. pmcraid_set_timestamp(cmd);
  5027. }
  5028. /**
  5029. * pmcraid_querycfg - Send a Query IOA Config to the adapter.
  5030. * @cmd: pointer pmcraid_cmd struct
  5031. *
  5032. * This function sends a Query IOA Configuration command to the adapter to
  5033. * retrieve the IOA configuration table.
  5034. *
  5035. * Return value:
  5036. * none
  5037. */
  5038. static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
  5039. {
  5040. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  5041. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  5042. struct pmcraid_instance *pinstance = cmd->drv_inst;
  5043. int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
  5044. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  5045. PMCRAID_FW_VERSION_1)
  5046. pinstance->config_table_entry_size =
  5047. sizeof(struct pmcraid_config_table_entry);
  5048. else
  5049. pinstance->config_table_entry_size =
  5050. sizeof(struct pmcraid_config_table_entry_ext);
  5051. ioarcb->request_type = REQ_TYPE_IOACMD;
  5052. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  5053. ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
  5054. /* firmware requires 4-byte length field, specified in B.E format */
  5055. memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
  5056. /* Since entire config table can be described by single IOADL, it can
  5057. * be part of IOARCB itself
  5058. */
  5059. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  5060. offsetof(struct pmcraid_ioarcb,
  5061. add_data.u.ioadl[0]));
  5062. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  5063. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  5064. ioarcb->request_flags0 |= NO_LINK_DESCS;
  5065. ioarcb->data_transfer_length =
  5066. cpu_to_le32(sizeof(struct pmcraid_config_table));
  5067. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  5068. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  5069. ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
  5070. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
  5071. pmcraid_send_cmd(cmd, pmcraid_init_res_table,
  5072. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  5073. }
  5074. /**
  5075. * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
  5076. * @pdev: pointer to pci device structure
  5077. * @dev_id: pointer to device ids structure
  5078. *
  5079. * Return Value
  5080. * returns 0 if the device is claimed and successfully configured.
  5081. * returns non-zero error code in case of any failure
  5082. */
  5083. static int __devinit pmcraid_probe(
  5084. struct pci_dev *pdev,
  5085. const struct pci_device_id *dev_id
  5086. )
  5087. {
  5088. struct pmcraid_instance *pinstance;
  5089. struct Scsi_Host *host;
  5090. void __iomem *mapped_pci_addr;
  5091. int rc = PCIBIOS_SUCCESSFUL;
  5092. if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
  5093. pmcraid_err
  5094. ("maximum number(%d) of supported adapters reached\n",
  5095. atomic_read(&pmcraid_adapter_count));
  5096. return -ENOMEM;
  5097. }
  5098. atomic_inc(&pmcraid_adapter_count);
  5099. rc = pci_enable_device(pdev);
  5100. if (rc) {
  5101. dev_err(&pdev->dev, "Cannot enable adapter\n");
  5102. atomic_dec(&pmcraid_adapter_count);
  5103. return rc;
  5104. }
  5105. dev_info(&pdev->dev,
  5106. "Found new IOA(%x:%x), Total IOA count: %d\n",
  5107. pdev->vendor, pdev->device,
  5108. atomic_read(&pmcraid_adapter_count));
  5109. rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
  5110. if (rc < 0) {
  5111. dev_err(&pdev->dev,
  5112. "Couldn't register memory range of registers\n");
  5113. goto out_disable_device;
  5114. }
  5115. mapped_pci_addr = pci_iomap(pdev, 0, 0);
  5116. if (!mapped_pci_addr) {
  5117. dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
  5118. rc = -ENOMEM;
  5119. goto out_release_regions;
  5120. }
  5121. pci_set_master(pdev);
  5122. /* Firmware requires the system bus address of IOARCB to be within
  5123. * 32-bit addressable range though it has 64-bit IOARRIN register.
  5124. * However, firmware supports 64-bit streaming DMA buffers, whereas
  5125. * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
  5126. * returns memory within 4GB (if not, change this logic), coherent
  5127. * buffers are within firmware acceptible address ranges.
  5128. */
  5129. if ((sizeof(dma_addr_t) == 4) ||
  5130. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  5131. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  5132. /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
  5133. * bit mask for pci_alloc_consistent to return addresses within 4GB
  5134. */
  5135. if (rc == 0)
  5136. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  5137. if (rc != 0) {
  5138. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  5139. goto cleanup_nomem;
  5140. }
  5141. host = scsi_host_alloc(&pmcraid_host_template,
  5142. sizeof(struct pmcraid_instance));
  5143. if (!host) {
  5144. dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
  5145. rc = -ENOMEM;
  5146. goto cleanup_nomem;
  5147. }
  5148. host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
  5149. host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
  5150. host->unique_id = host->host_no;
  5151. host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
  5152. host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
  5153. /* zero out entire instance structure */
  5154. pinstance = (struct pmcraid_instance *)host->hostdata;
  5155. memset(pinstance, 0, sizeof(*pinstance));
  5156. pinstance->chip_cfg =
  5157. (struct pmcraid_chip_details *)(dev_id->driver_data);
  5158. rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
  5159. if (rc < 0) {
  5160. dev_err(&pdev->dev, "failed to initialize adapter instance\n");
  5161. goto out_scsi_host_put;
  5162. }
  5163. pci_set_drvdata(pdev, pinstance);
  5164. /* Save PCI config-space for use following the reset */
  5165. rc = pci_save_state(pinstance->pdev);
  5166. if (rc != 0) {
  5167. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  5168. goto out_scsi_host_put;
  5169. }
  5170. pmcraid_disable_interrupts(pinstance, ~0);
  5171. rc = pmcraid_register_interrupt_handler(pinstance);
  5172. if (rc) {
  5173. dev_err(&pdev->dev, "couldn't register interrupt handler\n");
  5174. goto out_scsi_host_put;
  5175. }
  5176. pmcraid_init_tasklets(pinstance);
  5177. /* allocate verious buffers used by LLD.*/
  5178. rc = pmcraid_init_buffers(pinstance);
  5179. if (rc) {
  5180. pmcraid_err("couldn't allocate memory blocks\n");
  5181. goto out_unregister_isr;
  5182. }
  5183. /* check the reset type required */
  5184. pmcraid_reset_type(pinstance);
  5185. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  5186. /* Start IOA firmware initialization and bring card to Operational
  5187. * state.
  5188. */
  5189. pmcraid_info("starting IOA initialization sequence\n");
  5190. if (pmcraid_reset_bringup(pinstance)) {
  5191. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  5192. rc = 1;
  5193. goto out_release_bufs;
  5194. }
  5195. /* Add adapter instance into mid-layer list */
  5196. rc = scsi_add_host(pinstance->host, &pdev->dev);
  5197. if (rc != 0) {
  5198. pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
  5199. goto out_release_bufs;
  5200. }
  5201. scsi_scan_host(pinstance->host);
  5202. rc = pmcraid_setup_chrdev(pinstance);
  5203. if (rc != 0) {
  5204. pmcraid_err("couldn't create mgmt interface, error: %x\n",
  5205. rc);
  5206. goto out_remove_host;
  5207. }
  5208. /* Schedule worker thread to handle CCN and take care of adding and
  5209. * removing devices to OS
  5210. */
  5211. atomic_set(&pinstance->expose_resources, 1);
  5212. schedule_work(&pinstance->worker_q);
  5213. return rc;
  5214. out_remove_host:
  5215. scsi_remove_host(host);
  5216. out_release_bufs:
  5217. pmcraid_release_buffers(pinstance);
  5218. out_unregister_isr:
  5219. pmcraid_kill_tasklets(pinstance);
  5220. pmcraid_unregister_interrupt_handler(pinstance);
  5221. out_scsi_host_put:
  5222. scsi_host_put(host);
  5223. cleanup_nomem:
  5224. iounmap(mapped_pci_addr);
  5225. out_release_regions:
  5226. pci_release_regions(pdev);
  5227. out_disable_device:
  5228. atomic_dec(&pmcraid_adapter_count);
  5229. pci_set_drvdata(pdev, NULL);
  5230. pci_disable_device(pdev);
  5231. return -ENODEV;
  5232. }
  5233. /*
  5234. * PCI driver structure of pcmraid driver
  5235. */
  5236. static struct pci_driver pmcraid_driver = {
  5237. .name = PMCRAID_DRIVER_NAME,
  5238. .id_table = pmcraid_pci_table,
  5239. .probe = pmcraid_probe,
  5240. .remove = pmcraid_remove,
  5241. .suspend = pmcraid_suspend,
  5242. .resume = pmcraid_resume,
  5243. .shutdown = pmcraid_shutdown
  5244. };
  5245. /**
  5246. * pmcraid_init - module load entry point
  5247. */
  5248. static int __init pmcraid_init(void)
  5249. {
  5250. dev_t dev;
  5251. int error;
  5252. pmcraid_info("%s Device Driver version: %s %s\n",
  5253. PMCRAID_DRIVER_NAME,
  5254. PMCRAID_DRIVER_VERSION, PMCRAID_DRIVER_DATE);
  5255. error = alloc_chrdev_region(&dev, 0,
  5256. PMCRAID_MAX_ADAPTERS,
  5257. PMCRAID_DEVFILE);
  5258. if (error) {
  5259. pmcraid_err("failed to get a major number for adapters\n");
  5260. goto out_init;
  5261. }
  5262. pmcraid_major = MAJOR(dev);
  5263. pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
  5264. if (IS_ERR(pmcraid_class)) {
  5265. error = PTR_ERR(pmcraid_class);
  5266. pmcraid_err("failed to register with with sysfs, error = %x\n",
  5267. error);
  5268. goto out_unreg_chrdev;
  5269. }
  5270. error = pmcraid_netlink_init();
  5271. if (error)
  5272. goto out_unreg_chrdev;
  5273. error = pci_register_driver(&pmcraid_driver);
  5274. if (error == 0)
  5275. goto out_init;
  5276. pmcraid_err("failed to register pmcraid driver, error = %x\n",
  5277. error);
  5278. class_destroy(pmcraid_class);
  5279. pmcraid_netlink_release();
  5280. out_unreg_chrdev:
  5281. unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
  5282. out_init:
  5283. return error;
  5284. }
  5285. /**
  5286. * pmcraid_exit - module unload entry point
  5287. */
  5288. static void __exit pmcraid_exit(void)
  5289. {
  5290. pmcraid_netlink_release();
  5291. unregister_chrdev_region(MKDEV(pmcraid_major, 0),
  5292. PMCRAID_MAX_ADAPTERS);
  5293. pci_unregister_driver(&pmcraid_driver);
  5294. class_destroy(pmcraid_class);
  5295. }
  5296. module_init(pmcraid_init);
  5297. module_exit(pmcraid_exit);