megaraid_sas_base.c 197 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2003-2013 LSI Corporation
  5. * Copyright (c) 2013-2014 Avago Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * Authors: Avago Technologies
  21. * Sreenivas Bagalkote
  22. * Sumant Patro
  23. * Bo Yang
  24. * Adam Radford
  25. * Kashyap Desai <kashyap.desai@avagotech.com>
  26. * Sumit Saxena <sumit.saxena@avagotech.com>
  27. *
  28. * Send feedback to: megaraidlinux.pdl@avagotech.com
  29. *
  30. * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
  31. * San Jose, California 95131
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/list.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/uio.h>
  43. #include <linux/slab.h>
  44. #include <asm/uaccess.h>
  45. #include <linux/fs.h>
  46. #include <linux/compat.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/mutex.h>
  49. #include <linux/poll.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_tcq.h>
  55. #include "megaraid_sas_fusion.h"
  56. #include "megaraid_sas.h"
  57. /*
  58. * Number of sectors per IO command
  59. * Will be set in megasas_init_mfi if user does not provide
  60. */
  61. static unsigned int max_sectors;
  62. module_param_named(max_sectors, max_sectors, int, 0);
  63. MODULE_PARM_DESC(max_sectors,
  64. "Maximum number of sectors per IO command");
  65. static int msix_disable;
  66. module_param(msix_disable, int, S_IRUGO);
  67. MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  68. static unsigned int msix_vectors;
  69. module_param(msix_vectors, int, S_IRUGO);
  70. MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
  71. static int allow_vf_ioctls;
  72. module_param(allow_vf_ioctls, int, S_IRUGO);
  73. MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
  74. static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
  75. module_param(throttlequeuedepth, int, S_IRUGO);
  76. MODULE_PARM_DESC(throttlequeuedepth,
  77. "Adapter queue depth when throttled due to I/O timeout. Default: 16");
  78. unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
  79. module_param(resetwaittime, int, S_IRUGO);
  80. MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
  81. "before resetting adapter. Default: 180");
  82. int smp_affinity_enable = 1;
  83. module_param(smp_affinity_enable, int, S_IRUGO);
  84. MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
  85. int rdpq_enable = 1;
  86. module_param(rdpq_enable, int, S_IRUGO);
  87. MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
  88. unsigned int dual_qdepth_disable;
  89. module_param(dual_qdepth_disable, int, S_IRUGO);
  90. MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
  91. unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  92. module_param(scmd_timeout, int, S_IRUGO);
  93. MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
  94. MODULE_LICENSE("GPL");
  95. MODULE_VERSION(MEGASAS_VERSION);
  96. MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
  97. MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
  98. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  99. static int megasas_get_pd_list(struct megasas_instance *instance);
  100. static int megasas_ld_list_query(struct megasas_instance *instance,
  101. u8 query_type);
  102. static int megasas_issue_init_mfi(struct megasas_instance *instance);
  103. static int megasas_register_aen(struct megasas_instance *instance,
  104. u32 seq_num, u32 class_locale_word);
  105. static int
  106. megasas_get_pd_info(struct megasas_instance *instance, u16 device_id);
  107. /*
  108. * PCI ID table for all supported controllers
  109. */
  110. static struct pci_device_id megasas_pci_table[] = {
  111. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  112. /* xscale IOP */
  113. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  114. /* ppc IOP */
  115. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
  116. /* ppc IOP */
  117. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
  118. /* gen2*/
  119. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
  120. /* gen2*/
  121. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
  122. /* skinny*/
  123. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
  124. /* skinny*/
  125. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  126. /* xscale IOP, vega */
  127. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  128. /* xscale IOP */
  129. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
  130. /* Fusion */
  131. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
  132. /* Plasma */
  133. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
  134. /* Invader */
  135. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
  136. /* Fury */
  137. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
  138. /* Intruder */
  139. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
  140. /* Intruder 24 port*/
  141. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
  142. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
  143. {}
  144. };
  145. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  146. static int megasas_mgmt_majorno;
  147. struct megasas_mgmt_info megasas_mgmt_info;
  148. static struct fasync_struct *megasas_async_queue;
  149. static DEFINE_MUTEX(megasas_async_queue_mutex);
  150. static int megasas_poll_wait_aen;
  151. static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
  152. static u32 support_poll_for_event;
  153. u32 megasas_dbg_lvl;
  154. static u32 support_device_change;
  155. /* define lock for aen poll */
  156. spinlock_t poll_aen_lock;
  157. void
  158. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  159. u8 alt_status);
  160. static u32
  161. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
  162. static int
  163. megasas_adp_reset_gen2(struct megasas_instance *instance,
  164. struct megasas_register_set __iomem *reg_set);
  165. static irqreturn_t megasas_isr(int irq, void *devp);
  166. static u32
  167. megasas_init_adapter_mfi(struct megasas_instance *instance);
  168. u32
  169. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  170. struct scsi_cmnd *scmd);
  171. static void megasas_complete_cmd_dpc(unsigned long instance_addr);
  172. int
  173. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  174. int seconds);
  175. void megasas_fusion_ocr_wq(struct work_struct *work);
  176. static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
  177. int initial);
  178. int
  179. megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  180. {
  181. instance->instancet->fire_cmd(instance,
  182. cmd->frame_phys_addr, 0, instance->reg_set);
  183. return 0;
  184. }
  185. /**
  186. * megasas_get_cmd - Get a command from the free pool
  187. * @instance: Adapter soft state
  188. *
  189. * Returns a free command from the pool
  190. */
  191. struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  192. *instance)
  193. {
  194. unsigned long flags;
  195. struct megasas_cmd *cmd = NULL;
  196. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  197. if (!list_empty(&instance->cmd_pool)) {
  198. cmd = list_entry((&instance->cmd_pool)->next,
  199. struct megasas_cmd, list);
  200. list_del_init(&cmd->list);
  201. } else {
  202. dev_err(&instance->pdev->dev, "Command pool empty!\n");
  203. }
  204. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  205. return cmd;
  206. }
  207. /**
  208. * megasas_return_cmd - Return a cmd to free command pool
  209. * @instance: Adapter soft state
  210. * @cmd: Command packet to be returned to free command pool
  211. */
  212. inline void
  213. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  214. {
  215. unsigned long flags;
  216. u32 blk_tags;
  217. struct megasas_cmd_fusion *cmd_fusion;
  218. struct fusion_context *fusion = instance->ctrl_context;
  219. /* This flag is used only for fusion adapter.
  220. * Wait for Interrupt for Polled mode DCMD
  221. */
  222. if (cmd->flags & DRV_DCMD_POLLED_MODE)
  223. return;
  224. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  225. if (fusion) {
  226. blk_tags = instance->max_scsi_cmds + cmd->index;
  227. cmd_fusion = fusion->cmd_list[blk_tags];
  228. megasas_return_cmd_fusion(instance, cmd_fusion);
  229. }
  230. cmd->scmd = NULL;
  231. cmd->frame_count = 0;
  232. cmd->flags = 0;
  233. if (!fusion && reset_devices)
  234. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  235. list_add(&cmd->list, (&instance->cmd_pool)->next);
  236. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  237. }
  238. static const char *
  239. format_timestamp(uint32_t timestamp)
  240. {
  241. static char buffer[32];
  242. if ((timestamp & 0xff000000) == 0xff000000)
  243. snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
  244. 0x00ffffff);
  245. else
  246. snprintf(buffer, sizeof(buffer), "%us", timestamp);
  247. return buffer;
  248. }
  249. static const char *
  250. format_class(int8_t class)
  251. {
  252. static char buffer[6];
  253. switch (class) {
  254. case MFI_EVT_CLASS_DEBUG:
  255. return "debug";
  256. case MFI_EVT_CLASS_PROGRESS:
  257. return "progress";
  258. case MFI_EVT_CLASS_INFO:
  259. return "info";
  260. case MFI_EVT_CLASS_WARNING:
  261. return "WARN";
  262. case MFI_EVT_CLASS_CRITICAL:
  263. return "CRIT";
  264. case MFI_EVT_CLASS_FATAL:
  265. return "FATAL";
  266. case MFI_EVT_CLASS_DEAD:
  267. return "DEAD";
  268. default:
  269. snprintf(buffer, sizeof(buffer), "%d", class);
  270. return buffer;
  271. }
  272. }
  273. /**
  274. * megasas_decode_evt: Decode FW AEN event and print critical event
  275. * for information.
  276. * @instance: Adapter soft state
  277. */
  278. static void
  279. megasas_decode_evt(struct megasas_instance *instance)
  280. {
  281. struct megasas_evt_detail *evt_detail = instance->evt_detail;
  282. union megasas_evt_class_locale class_locale;
  283. class_locale.word = le32_to_cpu(evt_detail->cl.word);
  284. if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
  285. dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
  286. le32_to_cpu(evt_detail->seq_num),
  287. format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
  288. (class_locale.members.locale),
  289. format_class(class_locale.members.class),
  290. evt_detail->description);
  291. }
  292. /**
  293. * The following functions are defined for xscale
  294. * (deviceid : 1064R, PERC5) controllers
  295. */
  296. /**
  297. * megasas_enable_intr_xscale - Enables interrupts
  298. * @regs: MFI register set
  299. */
  300. static inline void
  301. megasas_enable_intr_xscale(struct megasas_instance *instance)
  302. {
  303. struct megasas_register_set __iomem *regs;
  304. regs = instance->reg_set;
  305. writel(0, &(regs)->outbound_intr_mask);
  306. /* Dummy readl to force pci flush */
  307. readl(&regs->outbound_intr_mask);
  308. }
  309. /**
  310. * megasas_disable_intr_xscale -Disables interrupt
  311. * @regs: MFI register set
  312. */
  313. static inline void
  314. megasas_disable_intr_xscale(struct megasas_instance *instance)
  315. {
  316. struct megasas_register_set __iomem *regs;
  317. u32 mask = 0x1f;
  318. regs = instance->reg_set;
  319. writel(mask, &regs->outbound_intr_mask);
  320. /* Dummy readl to force pci flush */
  321. readl(&regs->outbound_intr_mask);
  322. }
  323. /**
  324. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  325. * @regs: MFI register set
  326. */
  327. static u32
  328. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  329. {
  330. return readl(&(regs)->outbound_msg_0);
  331. }
  332. /**
  333. * megasas_clear_interrupt_xscale - Check & clear interrupt
  334. * @regs: MFI register set
  335. */
  336. static int
  337. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  338. {
  339. u32 status;
  340. u32 mfiStatus = 0;
  341. /*
  342. * Check if it is our interrupt
  343. */
  344. status = readl(&regs->outbound_intr_status);
  345. if (status & MFI_OB_INTR_STATUS_MASK)
  346. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  347. if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
  348. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  349. /*
  350. * Clear the interrupt by writing back the same value
  351. */
  352. if (mfiStatus)
  353. writel(status, &regs->outbound_intr_status);
  354. /* Dummy readl to force pci flush */
  355. readl(&regs->outbound_intr_status);
  356. return mfiStatus;
  357. }
  358. /**
  359. * megasas_fire_cmd_xscale - Sends command to the FW
  360. * @frame_phys_addr : Physical address of cmd
  361. * @frame_count : Number of frames for the command
  362. * @regs : MFI register set
  363. */
  364. static inline void
  365. megasas_fire_cmd_xscale(struct megasas_instance *instance,
  366. dma_addr_t frame_phys_addr,
  367. u32 frame_count,
  368. struct megasas_register_set __iomem *regs)
  369. {
  370. unsigned long flags;
  371. spin_lock_irqsave(&instance->hba_lock, flags);
  372. writel((frame_phys_addr >> 3)|(frame_count),
  373. &(regs)->inbound_queue_port);
  374. spin_unlock_irqrestore(&instance->hba_lock, flags);
  375. }
  376. /**
  377. * megasas_adp_reset_xscale - For controller reset
  378. * @regs: MFI register set
  379. */
  380. static int
  381. megasas_adp_reset_xscale(struct megasas_instance *instance,
  382. struct megasas_register_set __iomem *regs)
  383. {
  384. u32 i;
  385. u32 pcidata;
  386. writel(MFI_ADP_RESET, &regs->inbound_doorbell);
  387. for (i = 0; i < 3; i++)
  388. msleep(1000); /* sleep for 3 secs */
  389. pcidata = 0;
  390. pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
  391. dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
  392. if (pcidata & 0x2) {
  393. dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
  394. pcidata &= ~0x2;
  395. pci_write_config_dword(instance->pdev,
  396. MFI_1068_PCSR_OFFSET, pcidata);
  397. for (i = 0; i < 2; i++)
  398. msleep(1000); /* need to wait 2 secs again */
  399. pcidata = 0;
  400. pci_read_config_dword(instance->pdev,
  401. MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
  402. dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
  403. if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
  404. dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
  405. pcidata = 0;
  406. pci_write_config_dword(instance->pdev,
  407. MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
  408. }
  409. }
  410. return 0;
  411. }
  412. /**
  413. * megasas_check_reset_xscale - For controller reset check
  414. * @regs: MFI register set
  415. */
  416. static int
  417. megasas_check_reset_xscale(struct megasas_instance *instance,
  418. struct megasas_register_set __iomem *regs)
  419. {
  420. if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
  421. (le32_to_cpu(*instance->consumer) ==
  422. MEGASAS_ADPRESET_INPROG_SIGN))
  423. return 1;
  424. return 0;
  425. }
  426. static struct megasas_instance_template megasas_instance_template_xscale = {
  427. .fire_cmd = megasas_fire_cmd_xscale,
  428. .enable_intr = megasas_enable_intr_xscale,
  429. .disable_intr = megasas_disable_intr_xscale,
  430. .clear_intr = megasas_clear_intr_xscale,
  431. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  432. .adp_reset = megasas_adp_reset_xscale,
  433. .check_reset = megasas_check_reset_xscale,
  434. .service_isr = megasas_isr,
  435. .tasklet = megasas_complete_cmd_dpc,
  436. .init_adapter = megasas_init_adapter_mfi,
  437. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  438. .issue_dcmd = megasas_issue_dcmd,
  439. };
  440. /**
  441. * This is the end of set of functions & definitions specific
  442. * to xscale (deviceid : 1064R, PERC5) controllers
  443. */
  444. /**
  445. * The following functions are defined for ppc (deviceid : 0x60)
  446. * controllers
  447. */
  448. /**
  449. * megasas_enable_intr_ppc - Enables interrupts
  450. * @regs: MFI register set
  451. */
  452. static inline void
  453. megasas_enable_intr_ppc(struct megasas_instance *instance)
  454. {
  455. struct megasas_register_set __iomem *regs;
  456. regs = instance->reg_set;
  457. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  458. writel(~0x80000000, &(regs)->outbound_intr_mask);
  459. /* Dummy readl to force pci flush */
  460. readl(&regs->outbound_intr_mask);
  461. }
  462. /**
  463. * megasas_disable_intr_ppc - Disable interrupt
  464. * @regs: MFI register set
  465. */
  466. static inline void
  467. megasas_disable_intr_ppc(struct megasas_instance *instance)
  468. {
  469. struct megasas_register_set __iomem *regs;
  470. u32 mask = 0xFFFFFFFF;
  471. regs = instance->reg_set;
  472. writel(mask, &regs->outbound_intr_mask);
  473. /* Dummy readl to force pci flush */
  474. readl(&regs->outbound_intr_mask);
  475. }
  476. /**
  477. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  478. * @regs: MFI register set
  479. */
  480. static u32
  481. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  482. {
  483. return readl(&(regs)->outbound_scratch_pad);
  484. }
  485. /**
  486. * megasas_clear_interrupt_ppc - Check & clear interrupt
  487. * @regs: MFI register set
  488. */
  489. static int
  490. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  491. {
  492. u32 status, mfiStatus = 0;
  493. /*
  494. * Check if it is our interrupt
  495. */
  496. status = readl(&regs->outbound_intr_status);
  497. if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
  498. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  499. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
  500. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  501. /*
  502. * Clear the interrupt by writing back the same value
  503. */
  504. writel(status, &regs->outbound_doorbell_clear);
  505. /* Dummy readl to force pci flush */
  506. readl(&regs->outbound_doorbell_clear);
  507. return mfiStatus;
  508. }
  509. /**
  510. * megasas_fire_cmd_ppc - Sends command to the FW
  511. * @frame_phys_addr : Physical address of cmd
  512. * @frame_count : Number of frames for the command
  513. * @regs : MFI register set
  514. */
  515. static inline void
  516. megasas_fire_cmd_ppc(struct megasas_instance *instance,
  517. dma_addr_t frame_phys_addr,
  518. u32 frame_count,
  519. struct megasas_register_set __iomem *regs)
  520. {
  521. unsigned long flags;
  522. spin_lock_irqsave(&instance->hba_lock, flags);
  523. writel((frame_phys_addr | (frame_count<<1))|1,
  524. &(regs)->inbound_queue_port);
  525. spin_unlock_irqrestore(&instance->hba_lock, flags);
  526. }
  527. /**
  528. * megasas_check_reset_ppc - For controller reset check
  529. * @regs: MFI register set
  530. */
  531. static int
  532. megasas_check_reset_ppc(struct megasas_instance *instance,
  533. struct megasas_register_set __iomem *regs)
  534. {
  535. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  536. return 1;
  537. return 0;
  538. }
  539. static struct megasas_instance_template megasas_instance_template_ppc = {
  540. .fire_cmd = megasas_fire_cmd_ppc,
  541. .enable_intr = megasas_enable_intr_ppc,
  542. .disable_intr = megasas_disable_intr_ppc,
  543. .clear_intr = megasas_clear_intr_ppc,
  544. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  545. .adp_reset = megasas_adp_reset_xscale,
  546. .check_reset = megasas_check_reset_ppc,
  547. .service_isr = megasas_isr,
  548. .tasklet = megasas_complete_cmd_dpc,
  549. .init_adapter = megasas_init_adapter_mfi,
  550. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  551. .issue_dcmd = megasas_issue_dcmd,
  552. };
  553. /**
  554. * megasas_enable_intr_skinny - Enables interrupts
  555. * @regs: MFI register set
  556. */
  557. static inline void
  558. megasas_enable_intr_skinny(struct megasas_instance *instance)
  559. {
  560. struct megasas_register_set __iomem *regs;
  561. regs = instance->reg_set;
  562. writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
  563. writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  564. /* Dummy readl to force pci flush */
  565. readl(&regs->outbound_intr_mask);
  566. }
  567. /**
  568. * megasas_disable_intr_skinny - Disables interrupt
  569. * @regs: MFI register set
  570. */
  571. static inline void
  572. megasas_disable_intr_skinny(struct megasas_instance *instance)
  573. {
  574. struct megasas_register_set __iomem *regs;
  575. u32 mask = 0xFFFFFFFF;
  576. regs = instance->reg_set;
  577. writel(mask, &regs->outbound_intr_mask);
  578. /* Dummy readl to force pci flush */
  579. readl(&regs->outbound_intr_mask);
  580. }
  581. /**
  582. * megasas_read_fw_status_reg_skinny - returns the current FW status value
  583. * @regs: MFI register set
  584. */
  585. static u32
  586. megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
  587. {
  588. return readl(&(regs)->outbound_scratch_pad);
  589. }
  590. /**
  591. * megasas_clear_interrupt_skinny - Check & clear interrupt
  592. * @regs: MFI register set
  593. */
  594. static int
  595. megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
  596. {
  597. u32 status;
  598. u32 mfiStatus = 0;
  599. /*
  600. * Check if it is our interrupt
  601. */
  602. status = readl(&regs->outbound_intr_status);
  603. if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
  604. return 0;
  605. }
  606. /*
  607. * Check if it is our interrupt
  608. */
  609. if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
  610. MFI_STATE_FAULT) {
  611. mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  612. } else
  613. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  614. /*
  615. * Clear the interrupt by writing back the same value
  616. */
  617. writel(status, &regs->outbound_intr_status);
  618. /*
  619. * dummy read to flush PCI
  620. */
  621. readl(&regs->outbound_intr_status);
  622. return mfiStatus;
  623. }
  624. /**
  625. * megasas_fire_cmd_skinny - Sends command to the FW
  626. * @frame_phys_addr : Physical address of cmd
  627. * @frame_count : Number of frames for the command
  628. * @regs : MFI register set
  629. */
  630. static inline void
  631. megasas_fire_cmd_skinny(struct megasas_instance *instance,
  632. dma_addr_t frame_phys_addr,
  633. u32 frame_count,
  634. struct megasas_register_set __iomem *regs)
  635. {
  636. unsigned long flags;
  637. spin_lock_irqsave(&instance->hba_lock, flags);
  638. writel(upper_32_bits(frame_phys_addr),
  639. &(regs)->inbound_high_queue_port);
  640. writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
  641. &(regs)->inbound_low_queue_port);
  642. mmiowb();
  643. spin_unlock_irqrestore(&instance->hba_lock, flags);
  644. }
  645. /**
  646. * megasas_check_reset_skinny - For controller reset check
  647. * @regs: MFI register set
  648. */
  649. static int
  650. megasas_check_reset_skinny(struct megasas_instance *instance,
  651. struct megasas_register_set __iomem *regs)
  652. {
  653. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  654. return 1;
  655. return 0;
  656. }
  657. static struct megasas_instance_template megasas_instance_template_skinny = {
  658. .fire_cmd = megasas_fire_cmd_skinny,
  659. .enable_intr = megasas_enable_intr_skinny,
  660. .disable_intr = megasas_disable_intr_skinny,
  661. .clear_intr = megasas_clear_intr_skinny,
  662. .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
  663. .adp_reset = megasas_adp_reset_gen2,
  664. .check_reset = megasas_check_reset_skinny,
  665. .service_isr = megasas_isr,
  666. .tasklet = megasas_complete_cmd_dpc,
  667. .init_adapter = megasas_init_adapter_mfi,
  668. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  669. .issue_dcmd = megasas_issue_dcmd,
  670. };
  671. /**
  672. * The following functions are defined for gen2 (deviceid : 0x78 0x79)
  673. * controllers
  674. */
  675. /**
  676. * megasas_enable_intr_gen2 - Enables interrupts
  677. * @regs: MFI register set
  678. */
  679. static inline void
  680. megasas_enable_intr_gen2(struct megasas_instance *instance)
  681. {
  682. struct megasas_register_set __iomem *regs;
  683. regs = instance->reg_set;
  684. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  685. /* write ~0x00000005 (4 & 1) to the intr mask*/
  686. writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  687. /* Dummy readl to force pci flush */
  688. readl(&regs->outbound_intr_mask);
  689. }
  690. /**
  691. * megasas_disable_intr_gen2 - Disables interrupt
  692. * @regs: MFI register set
  693. */
  694. static inline void
  695. megasas_disable_intr_gen2(struct megasas_instance *instance)
  696. {
  697. struct megasas_register_set __iomem *regs;
  698. u32 mask = 0xFFFFFFFF;
  699. regs = instance->reg_set;
  700. writel(mask, &regs->outbound_intr_mask);
  701. /* Dummy readl to force pci flush */
  702. readl(&regs->outbound_intr_mask);
  703. }
  704. /**
  705. * megasas_read_fw_status_reg_gen2 - returns the current FW status value
  706. * @regs: MFI register set
  707. */
  708. static u32
  709. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
  710. {
  711. return readl(&(regs)->outbound_scratch_pad);
  712. }
  713. /**
  714. * megasas_clear_interrupt_gen2 - Check & clear interrupt
  715. * @regs: MFI register set
  716. */
  717. static int
  718. megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
  719. {
  720. u32 status;
  721. u32 mfiStatus = 0;
  722. /*
  723. * Check if it is our interrupt
  724. */
  725. status = readl(&regs->outbound_intr_status);
  726. if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
  727. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  728. }
  729. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
  730. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  731. }
  732. /*
  733. * Clear the interrupt by writing back the same value
  734. */
  735. if (mfiStatus)
  736. writel(status, &regs->outbound_doorbell_clear);
  737. /* Dummy readl to force pci flush */
  738. readl(&regs->outbound_intr_status);
  739. return mfiStatus;
  740. }
  741. /**
  742. * megasas_fire_cmd_gen2 - Sends command to the FW
  743. * @frame_phys_addr : Physical address of cmd
  744. * @frame_count : Number of frames for the command
  745. * @regs : MFI register set
  746. */
  747. static inline void
  748. megasas_fire_cmd_gen2(struct megasas_instance *instance,
  749. dma_addr_t frame_phys_addr,
  750. u32 frame_count,
  751. struct megasas_register_set __iomem *regs)
  752. {
  753. unsigned long flags;
  754. spin_lock_irqsave(&instance->hba_lock, flags);
  755. writel((frame_phys_addr | (frame_count<<1))|1,
  756. &(regs)->inbound_queue_port);
  757. spin_unlock_irqrestore(&instance->hba_lock, flags);
  758. }
  759. /**
  760. * megasas_adp_reset_gen2 - For controller reset
  761. * @regs: MFI register set
  762. */
  763. static int
  764. megasas_adp_reset_gen2(struct megasas_instance *instance,
  765. struct megasas_register_set __iomem *reg_set)
  766. {
  767. u32 retry = 0 ;
  768. u32 HostDiag;
  769. u32 __iomem *seq_offset = &reg_set->seq_offset;
  770. u32 __iomem *hostdiag_offset = &reg_set->host_diag;
  771. if (instance->instancet == &megasas_instance_template_skinny) {
  772. seq_offset = &reg_set->fusion_seq_offset;
  773. hostdiag_offset = &reg_set->fusion_host_diag;
  774. }
  775. writel(0, seq_offset);
  776. writel(4, seq_offset);
  777. writel(0xb, seq_offset);
  778. writel(2, seq_offset);
  779. writel(7, seq_offset);
  780. writel(0xd, seq_offset);
  781. msleep(1000);
  782. HostDiag = (u32)readl(hostdiag_offset);
  783. while (!(HostDiag & DIAG_WRITE_ENABLE)) {
  784. msleep(100);
  785. HostDiag = (u32)readl(hostdiag_offset);
  786. dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
  787. retry, HostDiag);
  788. if (retry++ >= 100)
  789. return 1;
  790. }
  791. dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
  792. writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
  793. ssleep(10);
  794. HostDiag = (u32)readl(hostdiag_offset);
  795. while (HostDiag & DIAG_RESET_ADAPTER) {
  796. msleep(100);
  797. HostDiag = (u32)readl(hostdiag_offset);
  798. dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
  799. retry, HostDiag);
  800. if (retry++ >= 1000)
  801. return 1;
  802. }
  803. return 0;
  804. }
  805. /**
  806. * megasas_check_reset_gen2 - For controller reset check
  807. * @regs: MFI register set
  808. */
  809. static int
  810. megasas_check_reset_gen2(struct megasas_instance *instance,
  811. struct megasas_register_set __iomem *regs)
  812. {
  813. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  814. return 1;
  815. return 0;
  816. }
  817. static struct megasas_instance_template megasas_instance_template_gen2 = {
  818. .fire_cmd = megasas_fire_cmd_gen2,
  819. .enable_intr = megasas_enable_intr_gen2,
  820. .disable_intr = megasas_disable_intr_gen2,
  821. .clear_intr = megasas_clear_intr_gen2,
  822. .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
  823. .adp_reset = megasas_adp_reset_gen2,
  824. .check_reset = megasas_check_reset_gen2,
  825. .service_isr = megasas_isr,
  826. .tasklet = megasas_complete_cmd_dpc,
  827. .init_adapter = megasas_init_adapter_mfi,
  828. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  829. .issue_dcmd = megasas_issue_dcmd,
  830. };
  831. /**
  832. * This is the end of set of functions & definitions
  833. * specific to gen2 (deviceid : 0x78, 0x79) controllers
  834. */
  835. /*
  836. * Template added for TB (Fusion)
  837. */
  838. extern struct megasas_instance_template megasas_instance_template_fusion;
  839. /**
  840. * megasas_issue_polled - Issues a polling command
  841. * @instance: Adapter soft state
  842. * @cmd: Command packet to be issued
  843. *
  844. * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
  845. */
  846. int
  847. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  848. {
  849. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  850. frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
  851. frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  852. if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
  853. (instance->instancet->issue_dcmd(instance, cmd))) {
  854. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  855. __func__, __LINE__);
  856. return DCMD_NOT_FIRED;
  857. }
  858. return wait_and_poll(instance, cmd, instance->requestorId ?
  859. MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
  860. }
  861. /**
  862. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  863. * @instance: Adapter soft state
  864. * @cmd: Command to be issued
  865. * @timeout: Timeout in seconds
  866. *
  867. * This function waits on an event for the command to be returned from ISR.
  868. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  869. * Used to issue ioctl commands.
  870. */
  871. int
  872. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  873. struct megasas_cmd *cmd, int timeout)
  874. {
  875. int ret = 0;
  876. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  877. if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
  878. (instance->instancet->issue_dcmd(instance, cmd))) {
  879. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  880. __func__, __LINE__);
  881. return DCMD_NOT_FIRED;
  882. }
  883. if (timeout) {
  884. ret = wait_event_timeout(instance->int_cmd_wait_q,
  885. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
  886. if (!ret) {
  887. dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
  888. __func__, __LINE__);
  889. return DCMD_TIMEOUT;
  890. }
  891. } else
  892. wait_event(instance->int_cmd_wait_q,
  893. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
  894. return (cmd->cmd_status_drv == MFI_STAT_OK) ?
  895. DCMD_SUCCESS : DCMD_FAILED;
  896. }
  897. /**
  898. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  899. * @instance: Adapter soft state
  900. * @cmd_to_abort: Previously issued cmd to be aborted
  901. * @timeout: Timeout in seconds
  902. *
  903. * MFI firmware can abort previously issued AEN comamnd (automatic event
  904. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  905. * cmd and waits for return status.
  906. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  907. */
  908. static int
  909. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  910. struct megasas_cmd *cmd_to_abort, int timeout)
  911. {
  912. struct megasas_cmd *cmd;
  913. struct megasas_abort_frame *abort_fr;
  914. int ret = 0;
  915. cmd = megasas_get_cmd(instance);
  916. if (!cmd)
  917. return -1;
  918. abort_fr = &cmd->frame->abort;
  919. /*
  920. * Prepare and issue the abort frame
  921. */
  922. abort_fr->cmd = MFI_CMD_ABORT;
  923. abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
  924. abort_fr->flags = cpu_to_le16(0);
  925. abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
  926. abort_fr->abort_mfi_phys_addr_lo =
  927. cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
  928. abort_fr->abort_mfi_phys_addr_hi =
  929. cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
  930. cmd->sync_cmd = 1;
  931. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  932. if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
  933. (instance->instancet->issue_dcmd(instance, cmd))) {
  934. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  935. __func__, __LINE__);
  936. return DCMD_NOT_FIRED;
  937. }
  938. if (timeout) {
  939. ret = wait_event_timeout(instance->abort_cmd_wait_q,
  940. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
  941. if (!ret) {
  942. dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
  943. __func__, __LINE__);
  944. return DCMD_TIMEOUT;
  945. }
  946. } else
  947. wait_event(instance->abort_cmd_wait_q,
  948. cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
  949. cmd->sync_cmd = 0;
  950. megasas_return_cmd(instance, cmd);
  951. return (cmd->cmd_status_drv == MFI_STAT_OK) ?
  952. DCMD_SUCCESS : DCMD_FAILED;
  953. }
  954. /**
  955. * megasas_make_sgl32 - Prepares 32-bit SGL
  956. * @instance: Adapter soft state
  957. * @scp: SCSI command from the mid-layer
  958. * @mfi_sgl: SGL to be filled in
  959. *
  960. * If successful, this function returns the number of SG elements. Otherwise,
  961. * it returnes -1.
  962. */
  963. static int
  964. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  965. union megasas_sgl *mfi_sgl)
  966. {
  967. int i;
  968. int sge_count;
  969. struct scatterlist *os_sgl;
  970. sge_count = scsi_dma_map(scp);
  971. BUG_ON(sge_count < 0);
  972. if (sge_count) {
  973. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  974. mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  975. mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
  976. }
  977. }
  978. return sge_count;
  979. }
  980. /**
  981. * megasas_make_sgl64 - Prepares 64-bit SGL
  982. * @instance: Adapter soft state
  983. * @scp: SCSI command from the mid-layer
  984. * @mfi_sgl: SGL to be filled in
  985. *
  986. * If successful, this function returns the number of SG elements. Otherwise,
  987. * it returnes -1.
  988. */
  989. static int
  990. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  991. union megasas_sgl *mfi_sgl)
  992. {
  993. int i;
  994. int sge_count;
  995. struct scatterlist *os_sgl;
  996. sge_count = scsi_dma_map(scp);
  997. BUG_ON(sge_count < 0);
  998. if (sge_count) {
  999. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1000. mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
  1001. mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
  1002. }
  1003. }
  1004. return sge_count;
  1005. }
  1006. /**
  1007. * megasas_make_sgl_skinny - Prepares IEEE SGL
  1008. * @instance: Adapter soft state
  1009. * @scp: SCSI command from the mid-layer
  1010. * @mfi_sgl: SGL to be filled in
  1011. *
  1012. * If successful, this function returns the number of SG elements. Otherwise,
  1013. * it returnes -1.
  1014. */
  1015. static int
  1016. megasas_make_sgl_skinny(struct megasas_instance *instance,
  1017. struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
  1018. {
  1019. int i;
  1020. int sge_count;
  1021. struct scatterlist *os_sgl;
  1022. sge_count = scsi_dma_map(scp);
  1023. if (sge_count) {
  1024. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1025. mfi_sgl->sge_skinny[i].length =
  1026. cpu_to_le32(sg_dma_len(os_sgl));
  1027. mfi_sgl->sge_skinny[i].phys_addr =
  1028. cpu_to_le64(sg_dma_address(os_sgl));
  1029. mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
  1030. }
  1031. }
  1032. return sge_count;
  1033. }
  1034. /**
  1035. * megasas_get_frame_count - Computes the number of frames
  1036. * @frame_type : type of frame- io or pthru frame
  1037. * @sge_count : number of sg elements
  1038. *
  1039. * Returns the number of frames required for numnber of sge's (sge_count)
  1040. */
  1041. static u32 megasas_get_frame_count(struct megasas_instance *instance,
  1042. u8 sge_count, u8 frame_type)
  1043. {
  1044. int num_cnt;
  1045. int sge_bytes;
  1046. u32 sge_sz;
  1047. u32 frame_count = 0;
  1048. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1049. sizeof(struct megasas_sge32);
  1050. if (instance->flag_ieee) {
  1051. sge_sz = sizeof(struct megasas_sge_skinny);
  1052. }
  1053. /*
  1054. * Main frame can contain 2 SGEs for 64-bit SGLs and
  1055. * 3 SGEs for 32-bit SGLs for ldio &
  1056. * 1 SGEs for 64-bit SGLs and
  1057. * 2 SGEs for 32-bit SGLs for pthru frame
  1058. */
  1059. if (unlikely(frame_type == PTHRU_FRAME)) {
  1060. if (instance->flag_ieee == 1) {
  1061. num_cnt = sge_count - 1;
  1062. } else if (IS_DMA64)
  1063. num_cnt = sge_count - 1;
  1064. else
  1065. num_cnt = sge_count - 2;
  1066. } else {
  1067. if (instance->flag_ieee == 1) {
  1068. num_cnt = sge_count - 1;
  1069. } else if (IS_DMA64)
  1070. num_cnt = sge_count - 2;
  1071. else
  1072. num_cnt = sge_count - 3;
  1073. }
  1074. if (num_cnt > 0) {
  1075. sge_bytes = sge_sz * num_cnt;
  1076. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  1077. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  1078. }
  1079. /* Main frame */
  1080. frame_count += 1;
  1081. if (frame_count > 7)
  1082. frame_count = 8;
  1083. return frame_count;
  1084. }
  1085. /**
  1086. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  1087. * @instance: Adapter soft state
  1088. * @scp: SCSI command
  1089. * @cmd: Command to be prepared in
  1090. *
  1091. * This function prepares CDB commands. These are typcially pass-through
  1092. * commands to the devices.
  1093. */
  1094. static int
  1095. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1096. struct megasas_cmd *cmd)
  1097. {
  1098. u32 is_logical;
  1099. u32 device_id;
  1100. u16 flags = 0;
  1101. struct megasas_pthru_frame *pthru;
  1102. is_logical = MEGASAS_IS_LOGICAL(scp);
  1103. device_id = MEGASAS_DEV_INDEX(scp);
  1104. pthru = (struct megasas_pthru_frame *)cmd->frame;
  1105. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1106. flags = MFI_FRAME_DIR_WRITE;
  1107. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1108. flags = MFI_FRAME_DIR_READ;
  1109. else if (scp->sc_data_direction == PCI_DMA_NONE)
  1110. flags = MFI_FRAME_DIR_NONE;
  1111. if (instance->flag_ieee == 1) {
  1112. flags |= MFI_FRAME_IEEE;
  1113. }
  1114. /*
  1115. * Prepare the DCDB frame
  1116. */
  1117. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  1118. pthru->cmd_status = 0x0;
  1119. pthru->scsi_status = 0x0;
  1120. pthru->target_id = device_id;
  1121. pthru->lun = scp->device->lun;
  1122. pthru->cdb_len = scp->cmd_len;
  1123. pthru->timeout = 0;
  1124. pthru->pad_0 = 0;
  1125. pthru->flags = cpu_to_le16(flags);
  1126. pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
  1127. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1128. /*
  1129. * If the command is for the tape device, set the
  1130. * pthru timeout to the os layer timeout value.
  1131. */
  1132. if (scp->device->type == TYPE_TAPE) {
  1133. if ((scp->request->timeout / HZ) > 0xFFFF)
  1134. pthru->timeout = cpu_to_le16(0xFFFF);
  1135. else
  1136. pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
  1137. }
  1138. /*
  1139. * Construct SGL
  1140. */
  1141. if (instance->flag_ieee == 1) {
  1142. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1143. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  1144. &pthru->sgl);
  1145. } else if (IS_DMA64) {
  1146. pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1147. pthru->sge_count = megasas_make_sgl64(instance, scp,
  1148. &pthru->sgl);
  1149. } else
  1150. pthru->sge_count = megasas_make_sgl32(instance, scp,
  1151. &pthru->sgl);
  1152. if (pthru->sge_count > instance->max_num_sge) {
  1153. dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
  1154. pthru->sge_count);
  1155. return 0;
  1156. }
  1157. /*
  1158. * Sense info specific
  1159. */
  1160. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  1161. pthru->sense_buf_phys_addr_hi =
  1162. cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
  1163. pthru->sense_buf_phys_addr_lo =
  1164. cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
  1165. /*
  1166. * Compute the total number of frames this command consumes. FW uses
  1167. * this number to pull sufficient number of frames from host memory.
  1168. */
  1169. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  1170. PTHRU_FRAME);
  1171. return cmd->frame_count;
  1172. }
  1173. /**
  1174. * megasas_build_ldio - Prepares IOs to logical devices
  1175. * @instance: Adapter soft state
  1176. * @scp: SCSI command
  1177. * @cmd: Command to be prepared
  1178. *
  1179. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  1180. */
  1181. static int
  1182. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1183. struct megasas_cmd *cmd)
  1184. {
  1185. u32 device_id;
  1186. u8 sc = scp->cmnd[0];
  1187. u16 flags = 0;
  1188. struct megasas_io_frame *ldio;
  1189. device_id = MEGASAS_DEV_INDEX(scp);
  1190. ldio = (struct megasas_io_frame *)cmd->frame;
  1191. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1192. flags = MFI_FRAME_DIR_WRITE;
  1193. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1194. flags = MFI_FRAME_DIR_READ;
  1195. if (instance->flag_ieee == 1) {
  1196. flags |= MFI_FRAME_IEEE;
  1197. }
  1198. /*
  1199. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  1200. */
  1201. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  1202. ldio->cmd_status = 0x0;
  1203. ldio->scsi_status = 0x0;
  1204. ldio->target_id = device_id;
  1205. ldio->timeout = 0;
  1206. ldio->reserved_0 = 0;
  1207. ldio->pad_0 = 0;
  1208. ldio->flags = cpu_to_le16(flags);
  1209. ldio->start_lba_hi = 0;
  1210. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  1211. /*
  1212. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1213. */
  1214. if (scp->cmd_len == 6) {
  1215. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
  1216. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
  1217. ((u32) scp->cmnd[2] << 8) |
  1218. (u32) scp->cmnd[3]);
  1219. ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
  1220. }
  1221. /*
  1222. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1223. */
  1224. else if (scp->cmd_len == 10) {
  1225. ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
  1226. ((u32) scp->cmnd[7] << 8));
  1227. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1228. ((u32) scp->cmnd[3] << 16) |
  1229. ((u32) scp->cmnd[4] << 8) |
  1230. (u32) scp->cmnd[5]);
  1231. }
  1232. /*
  1233. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1234. */
  1235. else if (scp->cmd_len == 12) {
  1236. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1237. ((u32) scp->cmnd[7] << 16) |
  1238. ((u32) scp->cmnd[8] << 8) |
  1239. (u32) scp->cmnd[9]);
  1240. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1241. ((u32) scp->cmnd[3] << 16) |
  1242. ((u32) scp->cmnd[4] << 8) |
  1243. (u32) scp->cmnd[5]);
  1244. }
  1245. /*
  1246. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1247. */
  1248. else if (scp->cmd_len == 16) {
  1249. ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
  1250. ((u32) scp->cmnd[11] << 16) |
  1251. ((u32) scp->cmnd[12] << 8) |
  1252. (u32) scp->cmnd[13]);
  1253. ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
  1254. ((u32) scp->cmnd[7] << 16) |
  1255. ((u32) scp->cmnd[8] << 8) |
  1256. (u32) scp->cmnd[9]);
  1257. ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
  1258. ((u32) scp->cmnd[3] << 16) |
  1259. ((u32) scp->cmnd[4] << 8) |
  1260. (u32) scp->cmnd[5]);
  1261. }
  1262. /*
  1263. * Construct SGL
  1264. */
  1265. if (instance->flag_ieee) {
  1266. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1267. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  1268. &ldio->sgl);
  1269. } else if (IS_DMA64) {
  1270. ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
  1271. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  1272. } else
  1273. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  1274. if (ldio->sge_count > instance->max_num_sge) {
  1275. dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
  1276. ldio->sge_count);
  1277. return 0;
  1278. }
  1279. /*
  1280. * Sense info specific
  1281. */
  1282. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  1283. ldio->sense_buf_phys_addr_hi = 0;
  1284. ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
  1285. /*
  1286. * Compute the total number of frames this command consumes. FW uses
  1287. * this number to pull sufficient number of frames from host memory.
  1288. */
  1289. cmd->frame_count = megasas_get_frame_count(instance,
  1290. ldio->sge_count, IO_FRAME);
  1291. return cmd->frame_count;
  1292. }
  1293. /**
  1294. * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
  1295. * and whether it's RW or non RW
  1296. * @scmd: SCSI command
  1297. *
  1298. */
  1299. inline int megasas_cmd_type(struct scsi_cmnd *cmd)
  1300. {
  1301. int ret;
  1302. switch (cmd->cmnd[0]) {
  1303. case READ_10:
  1304. case WRITE_10:
  1305. case READ_12:
  1306. case WRITE_12:
  1307. case READ_6:
  1308. case WRITE_6:
  1309. case READ_16:
  1310. case WRITE_16:
  1311. ret = (MEGASAS_IS_LOGICAL(cmd)) ?
  1312. READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
  1313. break;
  1314. default:
  1315. ret = (MEGASAS_IS_LOGICAL(cmd)) ?
  1316. NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
  1317. }
  1318. return ret;
  1319. }
  1320. /**
  1321. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  1322. * in FW
  1323. * @instance: Adapter soft state
  1324. */
  1325. static inline void
  1326. megasas_dump_pending_frames(struct megasas_instance *instance)
  1327. {
  1328. struct megasas_cmd *cmd;
  1329. int i,n;
  1330. union megasas_sgl *mfi_sgl;
  1331. struct megasas_io_frame *ldio;
  1332. struct megasas_pthru_frame *pthru;
  1333. u32 sgcount;
  1334. u32 max_cmd = instance->max_fw_cmds;
  1335. dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  1336. dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  1337. if (IS_DMA64)
  1338. dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  1339. else
  1340. dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  1341. dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  1342. for (i = 0; i < max_cmd; i++) {
  1343. cmd = instance->cmd_list[i];
  1344. if (!cmd->scmd)
  1345. continue;
  1346. dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  1347. if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
  1348. ldio = (struct megasas_io_frame *)cmd->frame;
  1349. mfi_sgl = &ldio->sgl;
  1350. sgcount = ldio->sge_count;
  1351. dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
  1352. " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1353. instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
  1354. le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
  1355. le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
  1356. } else {
  1357. pthru = (struct megasas_pthru_frame *) cmd->frame;
  1358. mfi_sgl = &pthru->sgl;
  1359. sgcount = pthru->sge_count;
  1360. dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
  1361. "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
  1362. instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
  1363. pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
  1364. le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
  1365. }
  1366. if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
  1367. for (n = 0; n < sgcount; n++) {
  1368. if (IS_DMA64)
  1369. dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
  1370. le32_to_cpu(mfi_sgl->sge64[n].length),
  1371. le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
  1372. else
  1373. dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
  1374. le32_to_cpu(mfi_sgl->sge32[n].length),
  1375. le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
  1376. }
  1377. }
  1378. } /*for max_cmd*/
  1379. dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  1380. for (i = 0; i < max_cmd; i++) {
  1381. cmd = instance->cmd_list[i];
  1382. if (cmd->sync_cmd == 1)
  1383. dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  1384. }
  1385. dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
  1386. }
  1387. u32
  1388. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  1389. struct scsi_cmnd *scmd)
  1390. {
  1391. struct megasas_cmd *cmd;
  1392. u32 frame_count;
  1393. cmd = megasas_get_cmd(instance);
  1394. if (!cmd)
  1395. return SCSI_MLQUEUE_HOST_BUSY;
  1396. /*
  1397. * Logical drive command
  1398. */
  1399. if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
  1400. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1401. else
  1402. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1403. if (!frame_count)
  1404. goto out_return_cmd;
  1405. cmd->scmd = scmd;
  1406. scmd->SCp.ptr = (char *)cmd;
  1407. /*
  1408. * Issue the command to the FW
  1409. */
  1410. atomic_inc(&instance->fw_outstanding);
  1411. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1412. cmd->frame_count-1, instance->reg_set);
  1413. return 0;
  1414. out_return_cmd:
  1415. megasas_return_cmd(instance, cmd);
  1416. return SCSI_MLQUEUE_HOST_BUSY;
  1417. }
  1418. /**
  1419. * megasas_queue_command - Queue entry point
  1420. * @scmd: SCSI command to be queued
  1421. * @done: Callback entry point
  1422. */
  1423. static int
  1424. megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
  1425. {
  1426. struct megasas_instance *instance;
  1427. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1428. instance = (struct megasas_instance *)
  1429. scmd->device->host->hostdata;
  1430. if (instance->unload == 1) {
  1431. scmd->result = DID_NO_CONNECT << 16;
  1432. scmd->scsi_done(scmd);
  1433. return 0;
  1434. }
  1435. if (instance->issuepend_done == 0)
  1436. return SCSI_MLQUEUE_HOST_BUSY;
  1437. /* Check for an mpio path and adjust behavior */
  1438. if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
  1439. if (megasas_check_mpio_paths(instance, scmd) ==
  1440. (DID_RESET << 16)) {
  1441. return SCSI_MLQUEUE_HOST_BUSY;
  1442. } else {
  1443. scmd->result = DID_NO_CONNECT << 16;
  1444. scmd->scsi_done(scmd);
  1445. return 0;
  1446. }
  1447. }
  1448. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  1449. scmd->result = DID_NO_CONNECT << 16;
  1450. scmd->scsi_done(scmd);
  1451. return 0;
  1452. }
  1453. mr_device_priv_data = scmd->device->hostdata;
  1454. if (!mr_device_priv_data) {
  1455. scmd->result = DID_NO_CONNECT << 16;
  1456. scmd->scsi_done(scmd);
  1457. return 0;
  1458. }
  1459. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
  1460. return SCSI_MLQUEUE_HOST_BUSY;
  1461. if (mr_device_priv_data->tm_busy)
  1462. return SCSI_MLQUEUE_DEVICE_BUSY;
  1463. scmd->result = 0;
  1464. if (MEGASAS_IS_LOGICAL(scmd) &&
  1465. (scmd->device->id >= instance->fw_supported_vd_count ||
  1466. scmd->device->lun)) {
  1467. scmd->result = DID_BAD_TARGET << 16;
  1468. goto out_done;
  1469. }
  1470. /*
  1471. * FW takes care of flush cache on its own for Virtual Disk.
  1472. * No need to send it down for VD. For JBOD send SYNCHRONIZE_CACHE to FW.
  1473. */
  1474. if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) && MEGASAS_IS_LOGICAL(scmd)) {
  1475. scmd->result = DID_OK << 16;
  1476. goto out_done;
  1477. }
  1478. return instance->instancet->build_and_issue_cmd(instance, scmd);
  1479. out_done:
  1480. scmd->scsi_done(scmd);
  1481. return 0;
  1482. }
  1483. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1484. {
  1485. int i;
  1486. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1487. if ((megasas_mgmt_info.instance[i]) &&
  1488. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1489. return megasas_mgmt_info.instance[i];
  1490. }
  1491. return NULL;
  1492. }
  1493. /*
  1494. * megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
  1495. *
  1496. * @sdev: OS provided scsi device
  1497. *
  1498. * Returns void
  1499. */
  1500. void megasas_update_sdev_properties(struct scsi_device *sdev)
  1501. {
  1502. u16 pd_index = 0;
  1503. u32 device_id, ld;
  1504. struct megasas_instance *instance;
  1505. struct fusion_context *fusion;
  1506. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1507. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  1508. struct MR_LD_RAID *raid;
  1509. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1510. instance = megasas_lookup_instance(sdev->host->host_no);
  1511. fusion = instance->ctrl_context;
  1512. mr_device_priv_data = sdev->hostdata;
  1513. if (!fusion)
  1514. return;
  1515. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1516. instance->use_seqnum_jbod_fp) {
  1517. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1518. sdev->id;
  1519. pd_sync = (void *)fusion->pd_seq_sync
  1520. [(instance->pd_seq_map_id - 1) & 1];
  1521. mr_device_priv_data->is_tm_capable =
  1522. pd_sync->seq[pd_index].capability.tmCapable;
  1523. } else {
  1524. device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
  1525. + sdev->id;
  1526. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1527. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  1528. raid = MR_LdRaidGet(ld, local_map_ptr);
  1529. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
  1530. blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
  1531. mr_device_priv_data->is_tm_capable =
  1532. raid->capability.tmCapable;
  1533. }
  1534. }
  1535. static void megasas_set_device_queue_depth(struct scsi_device *sdev)
  1536. {
  1537. u16 pd_index = 0;
  1538. int ret = DCMD_FAILED;
  1539. struct megasas_instance *instance;
  1540. instance = megasas_lookup_instance(sdev->host->host_no);
  1541. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
  1542. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
  1543. if (instance->pd_info) {
  1544. mutex_lock(&instance->hba_mutex);
  1545. ret = megasas_get_pd_info(instance, pd_index);
  1546. mutex_unlock(&instance->hba_mutex);
  1547. }
  1548. if (ret != DCMD_SUCCESS)
  1549. return;
  1550. if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
  1551. switch (instance->pd_list[pd_index].interface) {
  1552. case SAS_PD:
  1553. scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
  1554. break;
  1555. case SATA_PD:
  1556. scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
  1557. break;
  1558. default:
  1559. scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
  1560. }
  1561. }
  1562. }
  1563. }
  1564. static int megasas_slave_configure(struct scsi_device *sdev)
  1565. {
  1566. u16 pd_index = 0;
  1567. struct megasas_instance *instance;
  1568. instance = megasas_lookup_instance(sdev->host->host_no);
  1569. if (instance->pd_list_not_supported) {
  1570. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1571. sdev->type == TYPE_DISK) {
  1572. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1573. sdev->id;
  1574. if (instance->pd_list[pd_index].driveState !=
  1575. MR_PD_STATE_SYSTEM)
  1576. return -ENXIO;
  1577. }
  1578. }
  1579. megasas_set_device_queue_depth(sdev);
  1580. megasas_update_sdev_properties(sdev);
  1581. /*
  1582. * The RAID firmware may require extended timeouts.
  1583. */
  1584. blk_queue_rq_timeout(sdev->request_queue,
  1585. scmd_timeout * HZ);
  1586. return 0;
  1587. }
  1588. static int megasas_slave_alloc(struct scsi_device *sdev)
  1589. {
  1590. u16 pd_index = 0;
  1591. struct megasas_instance *instance ;
  1592. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1593. instance = megasas_lookup_instance(sdev->host->host_no);
  1594. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
  1595. /*
  1596. * Open the OS scan to the SYSTEM PD
  1597. */
  1598. pd_index =
  1599. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1600. sdev->id;
  1601. if ((instance->pd_list_not_supported ||
  1602. instance->pd_list[pd_index].driveState ==
  1603. MR_PD_STATE_SYSTEM)) {
  1604. goto scan_target;
  1605. }
  1606. return -ENXIO;
  1607. }
  1608. scan_target:
  1609. mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
  1610. GFP_KERNEL);
  1611. if (!mr_device_priv_data)
  1612. return -ENOMEM;
  1613. sdev->hostdata = mr_device_priv_data;
  1614. return 0;
  1615. }
  1616. static void megasas_slave_destroy(struct scsi_device *sdev)
  1617. {
  1618. kfree(sdev->hostdata);
  1619. sdev->hostdata = NULL;
  1620. }
  1621. /*
  1622. * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
  1623. * kill adapter
  1624. * @instance: Adapter soft state
  1625. *
  1626. */
  1627. static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
  1628. {
  1629. int i;
  1630. struct megasas_cmd *cmd_mfi;
  1631. struct megasas_cmd_fusion *cmd_fusion;
  1632. struct fusion_context *fusion = instance->ctrl_context;
  1633. /* Find all outstanding ioctls */
  1634. if (fusion) {
  1635. for (i = 0; i < instance->max_fw_cmds; i++) {
  1636. cmd_fusion = fusion->cmd_list[i];
  1637. if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
  1638. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  1639. if (cmd_mfi->sync_cmd &&
  1640. (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
  1641. cmd_mfi->frame->hdr.cmd_status =
  1642. MFI_STAT_WRONG_STATE;
  1643. megasas_complete_cmd(instance,
  1644. cmd_mfi, DID_OK);
  1645. }
  1646. }
  1647. }
  1648. } else {
  1649. for (i = 0; i < instance->max_fw_cmds; i++) {
  1650. cmd_mfi = instance->cmd_list[i];
  1651. if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
  1652. MFI_CMD_ABORT)
  1653. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  1654. }
  1655. }
  1656. }
  1657. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1658. {
  1659. /* Set critical error to block I/O & ioctls in case caller didn't */
  1660. atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
  1661. /* Wait 1 second to ensure IO or ioctls in build have posted */
  1662. msleep(1000);
  1663. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1664. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1665. (instance->ctrl_context)) {
  1666. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1667. /* Flush */
  1668. readl(&instance->reg_set->doorbell);
  1669. if (instance->requestorId && instance->peerIsPresent)
  1670. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  1671. } else {
  1672. writel(MFI_STOP_ADP,
  1673. &instance->reg_set->inbound_doorbell);
  1674. }
  1675. /* Complete outstanding ioctls when adapter is killed */
  1676. megasas_complete_outstanding_ioctls(instance);
  1677. }
  1678. /**
  1679. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1680. * restored to max value
  1681. * @instance: Adapter soft state
  1682. *
  1683. */
  1684. void
  1685. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1686. {
  1687. unsigned long flags;
  1688. if (instance->flag & MEGASAS_FW_BUSY
  1689. && time_after(jiffies, instance->last_time + 5 * HZ)
  1690. && atomic_read(&instance->fw_outstanding) <
  1691. instance->throttlequeuedepth + 1) {
  1692. spin_lock_irqsave(instance->host->host_lock, flags);
  1693. instance->flag &= ~MEGASAS_FW_BUSY;
  1694. instance->host->can_queue = instance->cur_can_queue;
  1695. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1696. }
  1697. }
  1698. /**
  1699. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1700. * @instance_addr: Address of adapter soft state
  1701. *
  1702. * Tasklet to complete cmds
  1703. */
  1704. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1705. {
  1706. u32 producer;
  1707. u32 consumer;
  1708. u32 context;
  1709. struct megasas_cmd *cmd;
  1710. struct megasas_instance *instance =
  1711. (struct megasas_instance *)instance_addr;
  1712. unsigned long flags;
  1713. /* If we have already declared adapter dead, donot complete cmds */
  1714. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  1715. return;
  1716. spin_lock_irqsave(&instance->completion_lock, flags);
  1717. producer = le32_to_cpu(*instance->producer);
  1718. consumer = le32_to_cpu(*instance->consumer);
  1719. while (consumer != producer) {
  1720. context = le32_to_cpu(instance->reply_queue[consumer]);
  1721. if (context >= instance->max_fw_cmds) {
  1722. dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
  1723. context);
  1724. BUG();
  1725. }
  1726. cmd = instance->cmd_list[context];
  1727. megasas_complete_cmd(instance, cmd, DID_OK);
  1728. consumer++;
  1729. if (consumer == (instance->max_fw_cmds + 1)) {
  1730. consumer = 0;
  1731. }
  1732. }
  1733. *instance->consumer = cpu_to_le32(producer);
  1734. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1735. /*
  1736. * Check if we can restore can_queue
  1737. */
  1738. megasas_check_and_restore_queue_depth(instance);
  1739. }
  1740. /**
  1741. * megasas_start_timer - Initializes a timer object
  1742. * @instance: Adapter soft state
  1743. * @timer: timer object to be initialized
  1744. * @fn: timer function
  1745. * @interval: time interval between timer function call
  1746. *
  1747. */
  1748. void megasas_start_timer(struct megasas_instance *instance,
  1749. struct timer_list *timer,
  1750. void *fn, unsigned long interval)
  1751. {
  1752. init_timer(timer);
  1753. timer->expires = jiffies + interval;
  1754. timer->data = (unsigned long)instance;
  1755. timer->function = fn;
  1756. add_timer(timer);
  1757. }
  1758. static void
  1759. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1760. static void
  1761. process_fw_state_change_wq(struct work_struct *work);
  1762. void megasas_do_ocr(struct megasas_instance *instance)
  1763. {
  1764. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1765. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1766. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1767. *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  1768. }
  1769. instance->instancet->disable_intr(instance);
  1770. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  1771. instance->issuepend_done = 0;
  1772. atomic_set(&instance->fw_outstanding, 0);
  1773. megasas_internal_reset_defer_cmds(instance);
  1774. process_fw_state_change_wq(&instance->work_init);
  1775. }
  1776. static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
  1777. int initial)
  1778. {
  1779. struct megasas_cmd *cmd;
  1780. struct megasas_dcmd_frame *dcmd;
  1781. struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
  1782. dma_addr_t new_affiliation_111_h;
  1783. int ld, retval = 0;
  1784. u8 thisVf;
  1785. cmd = megasas_get_cmd(instance);
  1786. if (!cmd) {
  1787. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
  1788. "Failed to get cmd for scsi%d\n",
  1789. instance->host->host_no);
  1790. return -ENOMEM;
  1791. }
  1792. dcmd = &cmd->frame->dcmd;
  1793. if (!instance->vf_affiliation_111) {
  1794. dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
  1795. "affiliation for scsi%d\n", instance->host->host_no);
  1796. megasas_return_cmd(instance, cmd);
  1797. return -ENOMEM;
  1798. }
  1799. if (initial)
  1800. memset(instance->vf_affiliation_111, 0,
  1801. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1802. else {
  1803. new_affiliation_111 =
  1804. pci_alloc_consistent(instance->pdev,
  1805. sizeof(struct MR_LD_VF_AFFILIATION_111),
  1806. &new_affiliation_111_h);
  1807. if (!new_affiliation_111) {
  1808. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
  1809. "memory for new affiliation for scsi%d\n",
  1810. instance->host->host_no);
  1811. megasas_return_cmd(instance, cmd);
  1812. return -ENOMEM;
  1813. }
  1814. memset(new_affiliation_111, 0,
  1815. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1816. }
  1817. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1818. dcmd->cmd = MFI_CMD_DCMD;
  1819. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  1820. dcmd->sge_count = 1;
  1821. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  1822. dcmd->timeout = 0;
  1823. dcmd->pad_0 = 0;
  1824. dcmd->data_xfer_len =
  1825. cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
  1826. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
  1827. if (initial)
  1828. dcmd->sgl.sge32[0].phys_addr =
  1829. cpu_to_le32(instance->vf_affiliation_111_h);
  1830. else
  1831. dcmd->sgl.sge32[0].phys_addr =
  1832. cpu_to_le32(new_affiliation_111_h);
  1833. dcmd->sgl.sge32[0].length = cpu_to_le32(
  1834. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1835. dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
  1836. "scsi%d\n", instance->host->host_no);
  1837. if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
  1838. dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
  1839. " failed with status 0x%x for scsi%d\n",
  1840. dcmd->cmd_status, instance->host->host_no);
  1841. retval = 1; /* Do a scan if we couldn't get affiliation */
  1842. goto out;
  1843. }
  1844. if (!initial) {
  1845. thisVf = new_affiliation_111->thisVf;
  1846. for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
  1847. if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
  1848. new_affiliation_111->map[ld].policy[thisVf]) {
  1849. dev_warn(&instance->pdev->dev, "SR-IOV: "
  1850. "Got new LD/VF affiliation for scsi%d\n",
  1851. instance->host->host_no);
  1852. memcpy(instance->vf_affiliation_111,
  1853. new_affiliation_111,
  1854. sizeof(struct MR_LD_VF_AFFILIATION_111));
  1855. retval = 1;
  1856. goto out;
  1857. }
  1858. }
  1859. out:
  1860. if (new_affiliation_111) {
  1861. pci_free_consistent(instance->pdev,
  1862. sizeof(struct MR_LD_VF_AFFILIATION_111),
  1863. new_affiliation_111,
  1864. new_affiliation_111_h);
  1865. }
  1866. megasas_return_cmd(instance, cmd);
  1867. return retval;
  1868. }
  1869. static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
  1870. int initial)
  1871. {
  1872. struct megasas_cmd *cmd;
  1873. struct megasas_dcmd_frame *dcmd;
  1874. struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
  1875. struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
  1876. dma_addr_t new_affiliation_h;
  1877. int i, j, retval = 0, found = 0, doscan = 0;
  1878. u8 thisVf;
  1879. cmd = megasas_get_cmd(instance);
  1880. if (!cmd) {
  1881. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
  1882. "Failed to get cmd for scsi%d\n",
  1883. instance->host->host_no);
  1884. return -ENOMEM;
  1885. }
  1886. dcmd = &cmd->frame->dcmd;
  1887. if (!instance->vf_affiliation) {
  1888. dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
  1889. "affiliation for scsi%d\n", instance->host->host_no);
  1890. megasas_return_cmd(instance, cmd);
  1891. return -ENOMEM;
  1892. }
  1893. if (initial)
  1894. memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
  1895. sizeof(struct MR_LD_VF_AFFILIATION));
  1896. else {
  1897. new_affiliation =
  1898. pci_alloc_consistent(instance->pdev,
  1899. (MAX_LOGICAL_DRIVES + 1) *
  1900. sizeof(struct MR_LD_VF_AFFILIATION),
  1901. &new_affiliation_h);
  1902. if (!new_affiliation) {
  1903. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
  1904. "memory for new affiliation for scsi%d\n",
  1905. instance->host->host_no);
  1906. megasas_return_cmd(instance, cmd);
  1907. return -ENOMEM;
  1908. }
  1909. memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
  1910. sizeof(struct MR_LD_VF_AFFILIATION));
  1911. }
  1912. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1913. dcmd->cmd = MFI_CMD_DCMD;
  1914. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  1915. dcmd->sge_count = 1;
  1916. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  1917. dcmd->timeout = 0;
  1918. dcmd->pad_0 = 0;
  1919. dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
  1920. sizeof(struct MR_LD_VF_AFFILIATION));
  1921. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
  1922. if (initial)
  1923. dcmd->sgl.sge32[0].phys_addr =
  1924. cpu_to_le32(instance->vf_affiliation_h);
  1925. else
  1926. dcmd->sgl.sge32[0].phys_addr =
  1927. cpu_to_le32(new_affiliation_h);
  1928. dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
  1929. sizeof(struct MR_LD_VF_AFFILIATION));
  1930. dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
  1931. "scsi%d\n", instance->host->host_no);
  1932. if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
  1933. dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
  1934. " failed with status 0x%x for scsi%d\n",
  1935. dcmd->cmd_status, instance->host->host_no);
  1936. retval = 1; /* Do a scan if we couldn't get affiliation */
  1937. goto out;
  1938. }
  1939. if (!initial) {
  1940. if (!new_affiliation->ldCount) {
  1941. dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
  1942. "affiliation for passive path for scsi%d\n",
  1943. instance->host->host_no);
  1944. retval = 1;
  1945. goto out;
  1946. }
  1947. newmap = new_affiliation->map;
  1948. savedmap = instance->vf_affiliation->map;
  1949. thisVf = new_affiliation->thisVf;
  1950. for (i = 0 ; i < new_affiliation->ldCount; i++) {
  1951. found = 0;
  1952. for (j = 0; j < instance->vf_affiliation->ldCount;
  1953. j++) {
  1954. if (newmap->ref.targetId ==
  1955. savedmap->ref.targetId) {
  1956. found = 1;
  1957. if (newmap->policy[thisVf] !=
  1958. savedmap->policy[thisVf]) {
  1959. doscan = 1;
  1960. goto out;
  1961. }
  1962. }
  1963. savedmap = (struct MR_LD_VF_MAP *)
  1964. ((unsigned char *)savedmap +
  1965. savedmap->size);
  1966. }
  1967. if (!found && newmap->policy[thisVf] !=
  1968. MR_LD_ACCESS_HIDDEN) {
  1969. doscan = 1;
  1970. goto out;
  1971. }
  1972. newmap = (struct MR_LD_VF_MAP *)
  1973. ((unsigned char *)newmap + newmap->size);
  1974. }
  1975. newmap = new_affiliation->map;
  1976. savedmap = instance->vf_affiliation->map;
  1977. for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
  1978. found = 0;
  1979. for (j = 0 ; j < new_affiliation->ldCount; j++) {
  1980. if (savedmap->ref.targetId ==
  1981. newmap->ref.targetId) {
  1982. found = 1;
  1983. if (savedmap->policy[thisVf] !=
  1984. newmap->policy[thisVf]) {
  1985. doscan = 1;
  1986. goto out;
  1987. }
  1988. }
  1989. newmap = (struct MR_LD_VF_MAP *)
  1990. ((unsigned char *)newmap +
  1991. newmap->size);
  1992. }
  1993. if (!found && savedmap->policy[thisVf] !=
  1994. MR_LD_ACCESS_HIDDEN) {
  1995. doscan = 1;
  1996. goto out;
  1997. }
  1998. savedmap = (struct MR_LD_VF_MAP *)
  1999. ((unsigned char *)savedmap +
  2000. savedmap->size);
  2001. }
  2002. }
  2003. out:
  2004. if (doscan) {
  2005. dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
  2006. "affiliation for scsi%d\n", instance->host->host_no);
  2007. memcpy(instance->vf_affiliation, new_affiliation,
  2008. new_affiliation->size);
  2009. retval = 1;
  2010. }
  2011. if (new_affiliation)
  2012. pci_free_consistent(instance->pdev,
  2013. (MAX_LOGICAL_DRIVES + 1) *
  2014. sizeof(struct MR_LD_VF_AFFILIATION),
  2015. new_affiliation, new_affiliation_h);
  2016. megasas_return_cmd(instance, cmd);
  2017. return retval;
  2018. }
  2019. /* This function will get the current SR-IOV LD/VF affiliation */
  2020. static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
  2021. int initial)
  2022. {
  2023. int retval;
  2024. if (instance->PlasmaFW111)
  2025. retval = megasas_get_ld_vf_affiliation_111(instance, initial);
  2026. else
  2027. retval = megasas_get_ld_vf_affiliation_12(instance, initial);
  2028. return retval;
  2029. }
  2030. /* This function will tell FW to start the SR-IOV heartbeat */
  2031. int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
  2032. int initial)
  2033. {
  2034. struct megasas_cmd *cmd;
  2035. struct megasas_dcmd_frame *dcmd;
  2036. int retval = 0;
  2037. cmd = megasas_get_cmd(instance);
  2038. if (!cmd) {
  2039. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
  2040. "Failed to get cmd for scsi%d\n",
  2041. instance->host->host_no);
  2042. return -ENOMEM;
  2043. }
  2044. dcmd = &cmd->frame->dcmd;
  2045. if (initial) {
  2046. instance->hb_host_mem =
  2047. pci_zalloc_consistent(instance->pdev,
  2048. sizeof(struct MR_CTRL_HB_HOST_MEM),
  2049. &instance->hb_host_mem_h);
  2050. if (!instance->hb_host_mem) {
  2051. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
  2052. " memory for heartbeat host memory for scsi%d\n",
  2053. instance->host->host_no);
  2054. retval = -ENOMEM;
  2055. goto out;
  2056. }
  2057. }
  2058. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2059. dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2060. dcmd->cmd = MFI_CMD_DCMD;
  2061. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  2062. dcmd->sge_count = 1;
  2063. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
  2064. dcmd->timeout = 0;
  2065. dcmd->pad_0 = 0;
  2066. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2067. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
  2068. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
  2069. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
  2070. dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
  2071. instance->host->host_no);
  2072. if (instance->ctrl_context && !instance->mask_interrupts)
  2073. retval = megasas_issue_blocked_cmd(instance, cmd,
  2074. MEGASAS_ROUTINE_WAIT_TIME_VF);
  2075. else
  2076. retval = megasas_issue_polled(instance, cmd);
  2077. if (retval) {
  2078. dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
  2079. "_MEM_ALLOC DCMD %s for scsi%d\n",
  2080. (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
  2081. "timed out" : "failed", instance->host->host_no);
  2082. retval = 1;
  2083. }
  2084. out:
  2085. megasas_return_cmd(instance, cmd);
  2086. return retval;
  2087. }
  2088. /* Handler for SR-IOV heartbeat */
  2089. void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
  2090. {
  2091. struct megasas_instance *instance =
  2092. (struct megasas_instance *)instance_addr;
  2093. if (instance->hb_host_mem->HB.fwCounter !=
  2094. instance->hb_host_mem->HB.driverCounter) {
  2095. instance->hb_host_mem->HB.driverCounter =
  2096. instance->hb_host_mem->HB.fwCounter;
  2097. mod_timer(&instance->sriov_heartbeat_timer,
  2098. jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  2099. } else {
  2100. dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
  2101. "completed for scsi%d\n", instance->host->host_no);
  2102. schedule_work(&instance->work_init);
  2103. }
  2104. }
  2105. /**
  2106. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  2107. * @instance: Adapter soft state
  2108. *
  2109. * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
  2110. * complete all its outstanding commands. Returns error if one or more IOs
  2111. * are pending after this time period. It also marks the controller dead.
  2112. */
  2113. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  2114. {
  2115. int i, sl, outstanding;
  2116. u32 reset_index;
  2117. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  2118. unsigned long flags;
  2119. struct list_head clist_local;
  2120. struct megasas_cmd *reset_cmd;
  2121. u32 fw_state;
  2122. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2123. dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
  2124. __func__, __LINE__);
  2125. return FAILED;
  2126. }
  2127. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2128. INIT_LIST_HEAD(&clist_local);
  2129. spin_lock_irqsave(&instance->hba_lock, flags);
  2130. list_splice_init(&instance->internal_reset_pending_q,
  2131. &clist_local);
  2132. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2133. dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
  2134. for (i = 0; i < wait_time; i++) {
  2135. msleep(1000);
  2136. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
  2137. break;
  2138. }
  2139. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2140. dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
  2141. atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
  2142. return FAILED;
  2143. }
  2144. reset_index = 0;
  2145. while (!list_empty(&clist_local)) {
  2146. reset_cmd = list_entry((&clist_local)->next,
  2147. struct megasas_cmd, list);
  2148. list_del_init(&reset_cmd->list);
  2149. if (reset_cmd->scmd) {
  2150. reset_cmd->scmd->result = DID_RESET << 16;
  2151. dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
  2152. reset_index, reset_cmd,
  2153. reset_cmd->scmd->cmnd[0]);
  2154. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  2155. megasas_return_cmd(instance, reset_cmd);
  2156. } else if (reset_cmd->sync_cmd) {
  2157. dev_notice(&instance->pdev->dev, "%p synch cmds"
  2158. "reset queue\n",
  2159. reset_cmd);
  2160. reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  2161. instance->instancet->fire_cmd(instance,
  2162. reset_cmd->frame_phys_addr,
  2163. 0, instance->reg_set);
  2164. } else {
  2165. dev_notice(&instance->pdev->dev, "%p unexpected"
  2166. "cmds lst\n",
  2167. reset_cmd);
  2168. }
  2169. reset_index++;
  2170. }
  2171. return SUCCESS;
  2172. }
  2173. for (i = 0; i < resetwaittime; i++) {
  2174. outstanding = atomic_read(&instance->fw_outstanding);
  2175. if (!outstanding)
  2176. break;
  2177. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  2178. dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
  2179. "commands to complete\n",i,outstanding);
  2180. /*
  2181. * Call cmd completion routine. Cmd to be
  2182. * be completed directly without depending on isr.
  2183. */
  2184. megasas_complete_cmd_dpc((unsigned long)instance);
  2185. }
  2186. msleep(1000);
  2187. }
  2188. i = 0;
  2189. outstanding = atomic_read(&instance->fw_outstanding);
  2190. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2191. if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
  2192. goto no_outstanding;
  2193. if (instance->disableOnlineCtrlReset)
  2194. goto kill_hba_and_failed;
  2195. do {
  2196. if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
  2197. dev_info(&instance->pdev->dev,
  2198. "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
  2199. __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
  2200. if (i == 3)
  2201. goto kill_hba_and_failed;
  2202. megasas_do_ocr(instance);
  2203. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2204. dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
  2205. __func__, __LINE__);
  2206. return FAILED;
  2207. }
  2208. dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
  2209. __func__, __LINE__);
  2210. for (sl = 0; sl < 10; sl++)
  2211. msleep(500);
  2212. outstanding = atomic_read(&instance->fw_outstanding);
  2213. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2214. if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
  2215. goto no_outstanding;
  2216. }
  2217. i++;
  2218. } while (i <= 3);
  2219. no_outstanding:
  2220. dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
  2221. __func__, __LINE__);
  2222. return SUCCESS;
  2223. kill_hba_and_failed:
  2224. /* Reset not supported, kill adapter */
  2225. dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
  2226. " disableOnlineCtrlReset %d fw_outstanding %d \n",
  2227. __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
  2228. atomic_read(&instance->fw_outstanding));
  2229. megasas_dump_pending_frames(instance);
  2230. megaraid_sas_kill_hba(instance);
  2231. return FAILED;
  2232. }
  2233. /**
  2234. * megasas_generic_reset - Generic reset routine
  2235. * @scmd: Mid-layer SCSI command
  2236. *
  2237. * This routine implements a generic reset handler for device, bus and host
  2238. * reset requests. Device, bus and host specific reset handlers can use this
  2239. * function after they do their specific tasks.
  2240. */
  2241. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  2242. {
  2243. int ret_val;
  2244. struct megasas_instance *instance;
  2245. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2246. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
  2247. scmd->cmnd[0], scmd->retries);
  2248. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2249. dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
  2250. return FAILED;
  2251. }
  2252. ret_val = megasas_wait_for_outstanding(instance);
  2253. if (ret_val == SUCCESS)
  2254. dev_notice(&instance->pdev->dev, "reset successful\n");
  2255. else
  2256. dev_err(&instance->pdev->dev, "failed to do reset\n");
  2257. return ret_val;
  2258. }
  2259. /**
  2260. * megasas_reset_timer - quiesce the adapter if required
  2261. * @scmd: scsi cmnd
  2262. *
  2263. * Sets the FW busy flag and reduces the host->can_queue if the
  2264. * cmd has not been completed within the timeout period.
  2265. */
  2266. static enum
  2267. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  2268. {
  2269. struct megasas_instance *instance;
  2270. unsigned long flags;
  2271. if (time_after(jiffies, scmd->jiffies_at_alloc +
  2272. (scmd_timeout * 2) * HZ)) {
  2273. return BLK_EH_NOT_HANDLED;
  2274. }
  2275. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2276. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  2277. /* FW is busy, throttle IO */
  2278. spin_lock_irqsave(instance->host->host_lock, flags);
  2279. instance->host->can_queue = instance->throttlequeuedepth;
  2280. instance->last_time = jiffies;
  2281. instance->flag |= MEGASAS_FW_BUSY;
  2282. spin_unlock_irqrestore(instance->host->host_lock, flags);
  2283. }
  2284. return BLK_EH_RESET_TIMER;
  2285. }
  2286. /**
  2287. * megasas_reset_bus_host - Bus & host reset handler entry point
  2288. */
  2289. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  2290. {
  2291. int ret;
  2292. struct megasas_instance *instance;
  2293. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2294. /*
  2295. * First wait for all commands to complete
  2296. */
  2297. if (instance->ctrl_context)
  2298. ret = megasas_reset_fusion(scmd->device->host, 1);
  2299. else
  2300. ret = megasas_generic_reset(scmd);
  2301. return ret;
  2302. }
  2303. /**
  2304. * megasas_task_abort - Issues task abort request to firmware
  2305. * (supported only for fusion adapters)
  2306. * @scmd: SCSI command pointer
  2307. */
  2308. static int megasas_task_abort(struct scsi_cmnd *scmd)
  2309. {
  2310. int ret;
  2311. struct megasas_instance *instance;
  2312. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2313. if (instance->ctrl_context)
  2314. ret = megasas_task_abort_fusion(scmd);
  2315. else {
  2316. sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
  2317. ret = FAILED;
  2318. }
  2319. return ret;
  2320. }
  2321. /**
  2322. * megasas_reset_target: Issues target reset request to firmware
  2323. * (supported only for fusion adapters)
  2324. * @scmd: SCSI command pointer
  2325. */
  2326. static int megasas_reset_target(struct scsi_cmnd *scmd)
  2327. {
  2328. int ret;
  2329. struct megasas_instance *instance;
  2330. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2331. if (instance->ctrl_context)
  2332. ret = megasas_reset_target_fusion(scmd);
  2333. else {
  2334. sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
  2335. ret = FAILED;
  2336. }
  2337. return ret;
  2338. }
  2339. /**
  2340. * megasas_bios_param - Returns disk geometry for a disk
  2341. * @sdev: device handle
  2342. * @bdev: block device
  2343. * @capacity: drive capacity
  2344. * @geom: geometry parameters
  2345. */
  2346. static int
  2347. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  2348. sector_t capacity, int geom[])
  2349. {
  2350. int heads;
  2351. int sectors;
  2352. sector_t cylinders;
  2353. unsigned long tmp;
  2354. /* Default heads (64) & sectors (32) */
  2355. heads = 64;
  2356. sectors = 32;
  2357. tmp = heads * sectors;
  2358. cylinders = capacity;
  2359. sector_div(cylinders, tmp);
  2360. /*
  2361. * Handle extended translation size for logical drives > 1Gb
  2362. */
  2363. if (capacity >= 0x200000) {
  2364. heads = 255;
  2365. sectors = 63;
  2366. tmp = heads*sectors;
  2367. cylinders = capacity;
  2368. sector_div(cylinders, tmp);
  2369. }
  2370. geom[0] = heads;
  2371. geom[1] = sectors;
  2372. geom[2] = cylinders;
  2373. return 0;
  2374. }
  2375. static void megasas_aen_polling(struct work_struct *work);
  2376. /**
  2377. * megasas_service_aen - Processes an event notification
  2378. * @instance: Adapter soft state
  2379. * @cmd: AEN command completed by the ISR
  2380. *
  2381. * For AEN, driver sends a command down to FW that is held by the FW till an
  2382. * event occurs. When an event of interest occurs, FW completes the command
  2383. * that it was previously holding.
  2384. *
  2385. * This routines sends SIGIO signal to processes that have registered with the
  2386. * driver for AEN.
  2387. */
  2388. static void
  2389. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  2390. {
  2391. unsigned long flags;
  2392. /*
  2393. * Don't signal app if it is just an aborted previously registered aen
  2394. */
  2395. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  2396. spin_lock_irqsave(&poll_aen_lock, flags);
  2397. megasas_poll_wait_aen = 1;
  2398. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2399. wake_up(&megasas_poll_wait);
  2400. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  2401. }
  2402. else
  2403. cmd->abort_aen = 0;
  2404. instance->aen_cmd = NULL;
  2405. megasas_return_cmd(instance, cmd);
  2406. if ((instance->unload == 0) &&
  2407. ((instance->issuepend_done == 1))) {
  2408. struct megasas_aen_event *ev;
  2409. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2410. if (!ev) {
  2411. dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
  2412. } else {
  2413. ev->instance = instance;
  2414. instance->ev = ev;
  2415. INIT_DELAYED_WORK(&ev->hotplug_work,
  2416. megasas_aen_polling);
  2417. schedule_delayed_work(&ev->hotplug_work, 0);
  2418. }
  2419. }
  2420. }
  2421. static ssize_t
  2422. megasas_fw_crash_buffer_store(struct device *cdev,
  2423. struct device_attribute *attr, const char *buf, size_t count)
  2424. {
  2425. struct Scsi_Host *shost = class_to_shost(cdev);
  2426. struct megasas_instance *instance =
  2427. (struct megasas_instance *) shost->hostdata;
  2428. int val = 0;
  2429. unsigned long flags;
  2430. if (kstrtoint(buf, 0, &val) != 0)
  2431. return -EINVAL;
  2432. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2433. instance->fw_crash_buffer_offset = val;
  2434. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2435. return strlen(buf);
  2436. }
  2437. static ssize_t
  2438. megasas_fw_crash_buffer_show(struct device *cdev,
  2439. struct device_attribute *attr, char *buf)
  2440. {
  2441. struct Scsi_Host *shost = class_to_shost(cdev);
  2442. struct megasas_instance *instance =
  2443. (struct megasas_instance *) shost->hostdata;
  2444. u32 size;
  2445. unsigned long buff_addr;
  2446. unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
  2447. unsigned long src_addr;
  2448. unsigned long flags;
  2449. u32 buff_offset;
  2450. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2451. buff_offset = instance->fw_crash_buffer_offset;
  2452. if (!instance->crash_dump_buf &&
  2453. !((instance->fw_crash_state == AVAILABLE) ||
  2454. (instance->fw_crash_state == COPYING))) {
  2455. dev_err(&instance->pdev->dev,
  2456. "Firmware crash dump is not available\n");
  2457. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2458. return -EINVAL;
  2459. }
  2460. buff_addr = (unsigned long) buf;
  2461. if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
  2462. dev_err(&instance->pdev->dev,
  2463. "Firmware crash dump offset is out of range\n");
  2464. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2465. return 0;
  2466. }
  2467. size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
  2468. size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
  2469. src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
  2470. (buff_offset % dmachunk);
  2471. memcpy(buf, (void *)src_addr, size);
  2472. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2473. return size;
  2474. }
  2475. static ssize_t
  2476. megasas_fw_crash_buffer_size_show(struct device *cdev,
  2477. struct device_attribute *attr, char *buf)
  2478. {
  2479. struct Scsi_Host *shost = class_to_shost(cdev);
  2480. struct megasas_instance *instance =
  2481. (struct megasas_instance *) shost->hostdata;
  2482. return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
  2483. ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
  2484. }
  2485. static ssize_t
  2486. megasas_fw_crash_state_store(struct device *cdev,
  2487. struct device_attribute *attr, const char *buf, size_t count)
  2488. {
  2489. struct Scsi_Host *shost = class_to_shost(cdev);
  2490. struct megasas_instance *instance =
  2491. (struct megasas_instance *) shost->hostdata;
  2492. int val = 0;
  2493. unsigned long flags;
  2494. if (kstrtoint(buf, 0, &val) != 0)
  2495. return -EINVAL;
  2496. if ((val <= AVAILABLE || val > COPY_ERROR)) {
  2497. dev_err(&instance->pdev->dev, "application updates invalid "
  2498. "firmware crash state\n");
  2499. return -EINVAL;
  2500. }
  2501. instance->fw_crash_state = val;
  2502. if ((val == COPIED) || (val == COPY_ERROR)) {
  2503. spin_lock_irqsave(&instance->crashdump_lock, flags);
  2504. megasas_free_host_crash_buffer(instance);
  2505. spin_unlock_irqrestore(&instance->crashdump_lock, flags);
  2506. if (val == COPY_ERROR)
  2507. dev_info(&instance->pdev->dev, "application failed to "
  2508. "copy Firmware crash dump\n");
  2509. else
  2510. dev_info(&instance->pdev->dev, "Firmware crash dump "
  2511. "copied successfully\n");
  2512. }
  2513. return strlen(buf);
  2514. }
  2515. static ssize_t
  2516. megasas_fw_crash_state_show(struct device *cdev,
  2517. struct device_attribute *attr, char *buf)
  2518. {
  2519. struct Scsi_Host *shost = class_to_shost(cdev);
  2520. struct megasas_instance *instance =
  2521. (struct megasas_instance *) shost->hostdata;
  2522. return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
  2523. }
  2524. static ssize_t
  2525. megasas_page_size_show(struct device *cdev,
  2526. struct device_attribute *attr, char *buf)
  2527. {
  2528. return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
  2529. }
  2530. static ssize_t
  2531. megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
  2532. char *buf)
  2533. {
  2534. struct Scsi_Host *shost = class_to_shost(cdev);
  2535. struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
  2536. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
  2537. }
  2538. static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
  2539. megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
  2540. static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
  2541. megasas_fw_crash_buffer_size_show, NULL);
  2542. static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
  2543. megasas_fw_crash_state_show, megasas_fw_crash_state_store);
  2544. static DEVICE_ATTR(page_size, S_IRUGO,
  2545. megasas_page_size_show, NULL);
  2546. static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
  2547. megasas_ldio_outstanding_show, NULL);
  2548. struct device_attribute *megaraid_host_attrs[] = {
  2549. &dev_attr_fw_crash_buffer_size,
  2550. &dev_attr_fw_crash_buffer,
  2551. &dev_attr_fw_crash_state,
  2552. &dev_attr_page_size,
  2553. &dev_attr_ldio_outstanding,
  2554. NULL,
  2555. };
  2556. /*
  2557. * Scsi host template for megaraid_sas driver
  2558. */
  2559. static struct scsi_host_template megasas_template = {
  2560. .module = THIS_MODULE,
  2561. .name = "Avago SAS based MegaRAID driver",
  2562. .proc_name = "megaraid_sas",
  2563. .slave_configure = megasas_slave_configure,
  2564. .slave_alloc = megasas_slave_alloc,
  2565. .slave_destroy = megasas_slave_destroy,
  2566. .queuecommand = megasas_queue_command,
  2567. .eh_target_reset_handler = megasas_reset_target,
  2568. .eh_abort_handler = megasas_task_abort,
  2569. .eh_host_reset_handler = megasas_reset_bus_host,
  2570. .eh_timed_out = megasas_reset_timer,
  2571. .shost_attrs = megaraid_host_attrs,
  2572. .bios_param = megasas_bios_param,
  2573. .use_clustering = ENABLE_CLUSTERING,
  2574. .change_queue_depth = scsi_change_queue_depth,
  2575. .no_write_same = 1,
  2576. };
  2577. /**
  2578. * megasas_complete_int_cmd - Completes an internal command
  2579. * @instance: Adapter soft state
  2580. * @cmd: Command to be completed
  2581. *
  2582. * The megasas_issue_blocked_cmd() function waits for a command to complete
  2583. * after it issues a command. This function wakes up that waiting routine by
  2584. * calling wake_up() on the wait queue.
  2585. */
  2586. static void
  2587. megasas_complete_int_cmd(struct megasas_instance *instance,
  2588. struct megasas_cmd *cmd)
  2589. {
  2590. cmd->cmd_status_drv = cmd->frame->io.cmd_status;
  2591. wake_up(&instance->int_cmd_wait_q);
  2592. }
  2593. /**
  2594. * megasas_complete_abort - Completes aborting a command
  2595. * @instance: Adapter soft state
  2596. * @cmd: Cmd that was issued to abort another cmd
  2597. *
  2598. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  2599. * after it issues an abort on a previously issued command. This function
  2600. * wakes up all functions waiting on the same wait queue.
  2601. */
  2602. static void
  2603. megasas_complete_abort(struct megasas_instance *instance,
  2604. struct megasas_cmd *cmd)
  2605. {
  2606. if (cmd->sync_cmd) {
  2607. cmd->sync_cmd = 0;
  2608. cmd->cmd_status_drv = 0;
  2609. wake_up(&instance->abort_cmd_wait_q);
  2610. }
  2611. }
  2612. /**
  2613. * megasas_complete_cmd - Completes a command
  2614. * @instance: Adapter soft state
  2615. * @cmd: Command to be completed
  2616. * @alt_status: If non-zero, use this value as status to
  2617. * SCSI mid-layer instead of the value returned
  2618. * by the FW. This should be used if caller wants
  2619. * an alternate status (as in the case of aborted
  2620. * commands)
  2621. */
  2622. void
  2623. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  2624. u8 alt_status)
  2625. {
  2626. int exception = 0;
  2627. struct megasas_header *hdr = &cmd->frame->hdr;
  2628. unsigned long flags;
  2629. struct fusion_context *fusion = instance->ctrl_context;
  2630. u32 opcode, status;
  2631. /* flag for the retry reset */
  2632. cmd->retry_for_fw_reset = 0;
  2633. if (cmd->scmd)
  2634. cmd->scmd->SCp.ptr = NULL;
  2635. switch (hdr->cmd) {
  2636. case MFI_CMD_INVALID:
  2637. /* Some older 1068 controller FW may keep a pended
  2638. MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
  2639. when booting the kdump kernel. Ignore this command to
  2640. prevent a kernel panic on shutdown of the kdump kernel. */
  2641. dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
  2642. "completed\n");
  2643. dev_warn(&instance->pdev->dev, "If you have a controller "
  2644. "other than PERC5, please upgrade your firmware\n");
  2645. break;
  2646. case MFI_CMD_PD_SCSI_IO:
  2647. case MFI_CMD_LD_SCSI_IO:
  2648. /*
  2649. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  2650. * issued either through an IO path or an IOCTL path. If it
  2651. * was via IOCTL, we will send it to internal completion.
  2652. */
  2653. if (cmd->sync_cmd) {
  2654. cmd->sync_cmd = 0;
  2655. megasas_complete_int_cmd(instance, cmd);
  2656. break;
  2657. }
  2658. case MFI_CMD_LD_READ:
  2659. case MFI_CMD_LD_WRITE:
  2660. if (alt_status) {
  2661. cmd->scmd->result = alt_status << 16;
  2662. exception = 1;
  2663. }
  2664. if (exception) {
  2665. atomic_dec(&instance->fw_outstanding);
  2666. scsi_dma_unmap(cmd->scmd);
  2667. cmd->scmd->scsi_done(cmd->scmd);
  2668. megasas_return_cmd(instance, cmd);
  2669. break;
  2670. }
  2671. switch (hdr->cmd_status) {
  2672. case MFI_STAT_OK:
  2673. cmd->scmd->result = DID_OK << 16;
  2674. break;
  2675. case MFI_STAT_SCSI_IO_FAILED:
  2676. case MFI_STAT_LD_INIT_IN_PROGRESS:
  2677. cmd->scmd->result =
  2678. (DID_ERROR << 16) | hdr->scsi_status;
  2679. break;
  2680. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  2681. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  2682. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  2683. memset(cmd->scmd->sense_buffer, 0,
  2684. SCSI_SENSE_BUFFERSIZE);
  2685. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  2686. hdr->sense_len);
  2687. cmd->scmd->result |= DRIVER_SENSE << 24;
  2688. }
  2689. break;
  2690. case MFI_STAT_LD_OFFLINE:
  2691. case MFI_STAT_DEVICE_NOT_FOUND:
  2692. cmd->scmd->result = DID_BAD_TARGET << 16;
  2693. break;
  2694. default:
  2695. dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
  2696. hdr->cmd_status);
  2697. cmd->scmd->result = DID_ERROR << 16;
  2698. break;
  2699. }
  2700. atomic_dec(&instance->fw_outstanding);
  2701. scsi_dma_unmap(cmd->scmd);
  2702. cmd->scmd->scsi_done(cmd->scmd);
  2703. megasas_return_cmd(instance, cmd);
  2704. break;
  2705. case MFI_CMD_SMP:
  2706. case MFI_CMD_STP:
  2707. case MFI_CMD_DCMD:
  2708. opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
  2709. /* Check for LD map update */
  2710. if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
  2711. && (cmd->frame->dcmd.mbox.b[1] == 1)) {
  2712. fusion->fast_path_io = 0;
  2713. spin_lock_irqsave(instance->host->host_lock, flags);
  2714. instance->map_update_cmd = NULL;
  2715. if (cmd->frame->hdr.cmd_status != 0) {
  2716. if (cmd->frame->hdr.cmd_status !=
  2717. MFI_STAT_NOT_FOUND)
  2718. dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
  2719. cmd->frame->hdr.cmd_status);
  2720. else {
  2721. megasas_return_cmd(instance, cmd);
  2722. spin_unlock_irqrestore(
  2723. instance->host->host_lock,
  2724. flags);
  2725. break;
  2726. }
  2727. } else
  2728. instance->map_id++;
  2729. megasas_return_cmd(instance, cmd);
  2730. /*
  2731. * Set fast path IO to ZERO.
  2732. * Validate Map will set proper value.
  2733. * Meanwhile all IOs will go as LD IO.
  2734. */
  2735. if (MR_ValidateMapInfo(instance))
  2736. fusion->fast_path_io = 1;
  2737. else
  2738. fusion->fast_path_io = 0;
  2739. megasas_sync_map_info(instance);
  2740. spin_unlock_irqrestore(instance->host->host_lock,
  2741. flags);
  2742. break;
  2743. }
  2744. if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  2745. opcode == MR_DCMD_CTRL_EVENT_GET) {
  2746. spin_lock_irqsave(&poll_aen_lock, flags);
  2747. megasas_poll_wait_aen = 0;
  2748. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2749. }
  2750. /* FW has an updated PD sequence */
  2751. if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
  2752. (cmd->frame->dcmd.mbox.b[0] == 1)) {
  2753. spin_lock_irqsave(instance->host->host_lock, flags);
  2754. status = cmd->frame->hdr.cmd_status;
  2755. instance->jbod_seq_cmd = NULL;
  2756. megasas_return_cmd(instance, cmd);
  2757. if (status == MFI_STAT_OK) {
  2758. instance->pd_seq_map_id++;
  2759. /* Re-register a pd sync seq num cmd */
  2760. if (megasas_sync_pd_seq_num(instance, true))
  2761. instance->use_seqnum_jbod_fp = false;
  2762. } else
  2763. instance->use_seqnum_jbod_fp = false;
  2764. spin_unlock_irqrestore(instance->host->host_lock, flags);
  2765. break;
  2766. }
  2767. /*
  2768. * See if got an event notification
  2769. */
  2770. if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
  2771. megasas_service_aen(instance, cmd);
  2772. else
  2773. megasas_complete_int_cmd(instance, cmd);
  2774. break;
  2775. case MFI_CMD_ABORT:
  2776. /*
  2777. * Cmd issued to abort another cmd returned
  2778. */
  2779. megasas_complete_abort(instance, cmd);
  2780. break;
  2781. default:
  2782. dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
  2783. hdr->cmd);
  2784. break;
  2785. }
  2786. }
  2787. /**
  2788. * megasas_issue_pending_cmds_again - issue all pending cmds
  2789. * in FW again because of the fw reset
  2790. * @instance: Adapter soft state
  2791. */
  2792. static inline void
  2793. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  2794. {
  2795. struct megasas_cmd *cmd;
  2796. struct list_head clist_local;
  2797. union megasas_evt_class_locale class_locale;
  2798. unsigned long flags;
  2799. u32 seq_num;
  2800. INIT_LIST_HEAD(&clist_local);
  2801. spin_lock_irqsave(&instance->hba_lock, flags);
  2802. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  2803. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2804. while (!list_empty(&clist_local)) {
  2805. cmd = list_entry((&clist_local)->next,
  2806. struct megasas_cmd, list);
  2807. list_del_init(&cmd->list);
  2808. if (cmd->sync_cmd || cmd->scmd) {
  2809. dev_notice(&instance->pdev->dev, "command %p, %p:%d"
  2810. "detected to be pending while HBA reset\n",
  2811. cmd, cmd->scmd, cmd->sync_cmd);
  2812. cmd->retry_for_fw_reset++;
  2813. if (cmd->retry_for_fw_reset == 3) {
  2814. dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
  2815. "was tried multiple times during reset."
  2816. "Shutting down the HBA\n",
  2817. cmd, cmd->scmd, cmd->sync_cmd);
  2818. instance->instancet->disable_intr(instance);
  2819. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2820. megaraid_sas_kill_hba(instance);
  2821. return;
  2822. }
  2823. }
  2824. if (cmd->sync_cmd == 1) {
  2825. if (cmd->scmd) {
  2826. dev_notice(&instance->pdev->dev, "unexpected"
  2827. "cmd attached to internal command!\n");
  2828. }
  2829. dev_notice(&instance->pdev->dev, "%p synchronous cmd"
  2830. "on the internal reset queue,"
  2831. "issue it again.\n", cmd);
  2832. cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
  2833. instance->instancet->fire_cmd(instance,
  2834. cmd->frame_phys_addr,
  2835. 0, instance->reg_set);
  2836. } else if (cmd->scmd) {
  2837. dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
  2838. "detected on the internal queue, issue again.\n",
  2839. cmd, cmd->scmd->cmnd[0]);
  2840. atomic_inc(&instance->fw_outstanding);
  2841. instance->instancet->fire_cmd(instance,
  2842. cmd->frame_phys_addr,
  2843. cmd->frame_count-1, instance->reg_set);
  2844. } else {
  2845. dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
  2846. "internal reset defer list while re-issue!!\n",
  2847. cmd);
  2848. }
  2849. }
  2850. if (instance->aen_cmd) {
  2851. dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
  2852. megasas_return_cmd(instance, instance->aen_cmd);
  2853. instance->aen_cmd = NULL;
  2854. }
  2855. /*
  2856. * Initiate AEN (Asynchronous Event Notification)
  2857. */
  2858. seq_num = instance->last_seq_num;
  2859. class_locale.members.reserved = 0;
  2860. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2861. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2862. megasas_register_aen(instance, seq_num, class_locale.word);
  2863. }
  2864. /**
  2865. * Move the internal reset pending commands to a deferred queue.
  2866. *
  2867. * We move the commands pending at internal reset time to a
  2868. * pending queue. This queue would be flushed after successful
  2869. * completion of the internal reset sequence. if the internal reset
  2870. * did not complete in time, the kernel reset handler would flush
  2871. * these commands.
  2872. **/
  2873. static void
  2874. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2875. {
  2876. struct megasas_cmd *cmd;
  2877. int i;
  2878. u32 max_cmd = instance->max_fw_cmds;
  2879. u32 defer_index;
  2880. unsigned long flags;
  2881. defer_index = 0;
  2882. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  2883. for (i = 0; i < max_cmd; i++) {
  2884. cmd = instance->cmd_list[i];
  2885. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2886. dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
  2887. "on the defer queue as internal\n",
  2888. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2889. if (!list_empty(&cmd->list)) {
  2890. dev_notice(&instance->pdev->dev, "ERROR while"
  2891. " moving this cmd:%p, %d %p, it was"
  2892. "discovered on some list?\n",
  2893. cmd, cmd->sync_cmd, cmd->scmd);
  2894. list_del_init(&cmd->list);
  2895. }
  2896. defer_index++;
  2897. list_add_tail(&cmd->list,
  2898. &instance->internal_reset_pending_q);
  2899. }
  2900. }
  2901. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  2902. }
  2903. static void
  2904. process_fw_state_change_wq(struct work_struct *work)
  2905. {
  2906. struct megasas_instance *instance =
  2907. container_of(work, struct megasas_instance, work_init);
  2908. u32 wait;
  2909. unsigned long flags;
  2910. if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
  2911. dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
  2912. atomic_read(&instance->adprecovery));
  2913. return ;
  2914. }
  2915. if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
  2916. dev_notice(&instance->pdev->dev, "FW detected to be in fault"
  2917. "state, restarting it...\n");
  2918. instance->instancet->disable_intr(instance);
  2919. atomic_set(&instance->fw_outstanding, 0);
  2920. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2921. instance->instancet->adp_reset(instance, instance->reg_set);
  2922. atomic_set(&instance->fw_reset_no_pci_access, 0);
  2923. dev_notice(&instance->pdev->dev, "FW restarted successfully,"
  2924. "initiating next stage...\n");
  2925. dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
  2926. "state 2 starting...\n");
  2927. /* waiting for about 20 second before start the second init */
  2928. for (wait = 0; wait < 30; wait++) {
  2929. msleep(1000);
  2930. }
  2931. if (megasas_transition_to_ready(instance, 1)) {
  2932. dev_notice(&instance->pdev->dev, "adapter not ready\n");
  2933. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2934. megaraid_sas_kill_hba(instance);
  2935. return ;
  2936. }
  2937. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2938. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2939. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2940. ) {
  2941. *instance->consumer = *instance->producer;
  2942. } else {
  2943. *instance->consumer = 0;
  2944. *instance->producer = 0;
  2945. }
  2946. megasas_issue_init_mfi(instance);
  2947. spin_lock_irqsave(&instance->hba_lock, flags);
  2948. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  2949. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2950. instance->instancet->enable_intr(instance);
  2951. megasas_issue_pending_cmds_again(instance);
  2952. instance->issuepend_done = 1;
  2953. }
  2954. }
  2955. /**
  2956. * megasas_deplete_reply_queue - Processes all completed commands
  2957. * @instance: Adapter soft state
  2958. * @alt_status: Alternate status to be returned to
  2959. * SCSI mid-layer instead of the status
  2960. * returned by the FW
  2961. * Note: this must be called with hba lock held
  2962. */
  2963. static int
  2964. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2965. u8 alt_status)
  2966. {
  2967. u32 mfiStatus;
  2968. u32 fw_state;
  2969. if ((mfiStatus = instance->instancet->check_reset(instance,
  2970. instance->reg_set)) == 1) {
  2971. return IRQ_HANDLED;
  2972. }
  2973. if ((mfiStatus = instance->instancet->clear_intr(
  2974. instance->reg_set)
  2975. ) == 0) {
  2976. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2977. if (!instance->msix_vectors)
  2978. return IRQ_NONE;
  2979. }
  2980. instance->mfiStatus = mfiStatus;
  2981. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2982. fw_state = instance->instancet->read_fw_status_reg(
  2983. instance->reg_set) & MFI_STATE_MASK;
  2984. if (fw_state != MFI_STATE_FAULT) {
  2985. dev_notice(&instance->pdev->dev, "fw state:%x\n",
  2986. fw_state);
  2987. }
  2988. if ((fw_state == MFI_STATE_FAULT) &&
  2989. (instance->disableOnlineCtrlReset == 0)) {
  2990. dev_notice(&instance->pdev->dev, "wait adp restart\n");
  2991. if ((instance->pdev->device ==
  2992. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2993. (instance->pdev->device ==
  2994. PCI_DEVICE_ID_DELL_PERC5) ||
  2995. (instance->pdev->device ==
  2996. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2997. *instance->consumer =
  2998. cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
  2999. }
  3000. instance->instancet->disable_intr(instance);
  3001. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  3002. instance->issuepend_done = 0;
  3003. atomic_set(&instance->fw_outstanding, 0);
  3004. megasas_internal_reset_defer_cmds(instance);
  3005. dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
  3006. fw_state, atomic_read(&instance->adprecovery));
  3007. schedule_work(&instance->work_init);
  3008. return IRQ_HANDLED;
  3009. } else {
  3010. dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
  3011. fw_state, instance->disableOnlineCtrlReset);
  3012. }
  3013. }
  3014. tasklet_schedule(&instance->isr_tasklet);
  3015. return IRQ_HANDLED;
  3016. }
  3017. /**
  3018. * megasas_isr - isr entry point
  3019. */
  3020. static irqreturn_t megasas_isr(int irq, void *devp)
  3021. {
  3022. struct megasas_irq_context *irq_context = devp;
  3023. struct megasas_instance *instance = irq_context->instance;
  3024. unsigned long flags;
  3025. irqreturn_t rc;
  3026. if (atomic_read(&instance->fw_reset_no_pci_access))
  3027. return IRQ_HANDLED;
  3028. spin_lock_irqsave(&instance->hba_lock, flags);
  3029. rc = megasas_deplete_reply_queue(instance, DID_OK);
  3030. spin_unlock_irqrestore(&instance->hba_lock, flags);
  3031. return rc;
  3032. }
  3033. /**
  3034. * megasas_transition_to_ready - Move the FW to READY state
  3035. * @instance: Adapter soft state
  3036. *
  3037. * During the initialization, FW passes can potentially be in any one of
  3038. * several possible states. If the FW in operational, waiting-for-handshake
  3039. * states, driver must take steps to bring it to ready state. Otherwise, it
  3040. * has to wait for the ready state.
  3041. */
  3042. int
  3043. megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
  3044. {
  3045. int i;
  3046. u8 max_wait;
  3047. u32 fw_state;
  3048. u32 cur_state;
  3049. u32 abs_state, curr_abs_state;
  3050. abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
  3051. fw_state = abs_state & MFI_STATE_MASK;
  3052. if (fw_state != MFI_STATE_READY)
  3053. dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
  3054. " state\n");
  3055. while (fw_state != MFI_STATE_READY) {
  3056. switch (fw_state) {
  3057. case MFI_STATE_FAULT:
  3058. dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
  3059. if (ocr) {
  3060. max_wait = MEGASAS_RESET_WAIT_TIME;
  3061. cur_state = MFI_STATE_FAULT;
  3062. break;
  3063. } else
  3064. return -ENODEV;
  3065. case MFI_STATE_WAIT_HANDSHAKE:
  3066. /*
  3067. * Set the CLR bit in inbound doorbell
  3068. */
  3069. if ((instance->pdev->device ==
  3070. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3071. (instance->pdev->device ==
  3072. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3073. (instance->ctrl_context))
  3074. writel(
  3075. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  3076. &instance->reg_set->doorbell);
  3077. else
  3078. writel(
  3079. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  3080. &instance->reg_set->inbound_doorbell);
  3081. max_wait = MEGASAS_RESET_WAIT_TIME;
  3082. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  3083. break;
  3084. case MFI_STATE_BOOT_MESSAGE_PENDING:
  3085. if ((instance->pdev->device ==
  3086. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3087. (instance->pdev->device ==
  3088. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3089. (instance->ctrl_context))
  3090. writel(MFI_INIT_HOTPLUG,
  3091. &instance->reg_set->doorbell);
  3092. else
  3093. writel(MFI_INIT_HOTPLUG,
  3094. &instance->reg_set->inbound_doorbell);
  3095. max_wait = MEGASAS_RESET_WAIT_TIME;
  3096. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  3097. break;
  3098. case MFI_STATE_OPERATIONAL:
  3099. /*
  3100. * Bring it to READY state; assuming max wait 10 secs
  3101. */
  3102. instance->instancet->disable_intr(instance);
  3103. if ((instance->pdev->device ==
  3104. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3105. (instance->pdev->device ==
  3106. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  3107. (instance->ctrl_context)) {
  3108. writel(MFI_RESET_FLAGS,
  3109. &instance->reg_set->doorbell);
  3110. if (instance->ctrl_context) {
  3111. for (i = 0; i < (10 * 1000); i += 20) {
  3112. if (readl(
  3113. &instance->
  3114. reg_set->
  3115. doorbell) & 1)
  3116. msleep(20);
  3117. else
  3118. break;
  3119. }
  3120. }
  3121. } else
  3122. writel(MFI_RESET_FLAGS,
  3123. &instance->reg_set->inbound_doorbell);
  3124. max_wait = MEGASAS_RESET_WAIT_TIME;
  3125. cur_state = MFI_STATE_OPERATIONAL;
  3126. break;
  3127. case MFI_STATE_UNDEFINED:
  3128. /*
  3129. * This state should not last for more than 2 seconds
  3130. */
  3131. max_wait = MEGASAS_RESET_WAIT_TIME;
  3132. cur_state = MFI_STATE_UNDEFINED;
  3133. break;
  3134. case MFI_STATE_BB_INIT:
  3135. max_wait = MEGASAS_RESET_WAIT_TIME;
  3136. cur_state = MFI_STATE_BB_INIT;
  3137. break;
  3138. case MFI_STATE_FW_INIT:
  3139. max_wait = MEGASAS_RESET_WAIT_TIME;
  3140. cur_state = MFI_STATE_FW_INIT;
  3141. break;
  3142. case MFI_STATE_FW_INIT_2:
  3143. max_wait = MEGASAS_RESET_WAIT_TIME;
  3144. cur_state = MFI_STATE_FW_INIT_2;
  3145. break;
  3146. case MFI_STATE_DEVICE_SCAN:
  3147. max_wait = MEGASAS_RESET_WAIT_TIME;
  3148. cur_state = MFI_STATE_DEVICE_SCAN;
  3149. break;
  3150. case MFI_STATE_FLUSH_CACHE:
  3151. max_wait = MEGASAS_RESET_WAIT_TIME;
  3152. cur_state = MFI_STATE_FLUSH_CACHE;
  3153. break;
  3154. default:
  3155. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
  3156. fw_state);
  3157. return -ENODEV;
  3158. }
  3159. /*
  3160. * The cur_state should not last for more than max_wait secs
  3161. */
  3162. for (i = 0; i < (max_wait * 1000); i++) {
  3163. curr_abs_state = instance->instancet->
  3164. read_fw_status_reg(instance->reg_set);
  3165. if (abs_state == curr_abs_state) {
  3166. msleep(1);
  3167. } else
  3168. break;
  3169. }
  3170. /*
  3171. * Return error if fw_state hasn't changed after max_wait
  3172. */
  3173. if (curr_abs_state == abs_state) {
  3174. dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
  3175. "in %d secs\n", fw_state, max_wait);
  3176. return -ENODEV;
  3177. }
  3178. abs_state = curr_abs_state;
  3179. fw_state = curr_abs_state & MFI_STATE_MASK;
  3180. }
  3181. dev_info(&instance->pdev->dev, "FW now in Ready state\n");
  3182. return 0;
  3183. }
  3184. /**
  3185. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  3186. * @instance: Adapter soft state
  3187. */
  3188. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  3189. {
  3190. int i;
  3191. u32 max_cmd = instance->max_mfi_cmds;
  3192. struct megasas_cmd *cmd;
  3193. if (!instance->frame_dma_pool)
  3194. return;
  3195. /*
  3196. * Return all frames to pool
  3197. */
  3198. for (i = 0; i < max_cmd; i++) {
  3199. cmd = instance->cmd_list[i];
  3200. if (cmd->frame)
  3201. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  3202. cmd->frame_phys_addr);
  3203. if (cmd->sense)
  3204. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  3205. cmd->sense_phys_addr);
  3206. }
  3207. /*
  3208. * Now destroy the pool itself
  3209. */
  3210. pci_pool_destroy(instance->frame_dma_pool);
  3211. pci_pool_destroy(instance->sense_dma_pool);
  3212. instance->frame_dma_pool = NULL;
  3213. instance->sense_dma_pool = NULL;
  3214. }
  3215. /**
  3216. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  3217. * @instance: Adapter soft state
  3218. *
  3219. * Each command packet has an embedded DMA memory buffer that is used for
  3220. * filling MFI frame and the SG list that immediately follows the frame. This
  3221. * function creates those DMA memory buffers for each command packet by using
  3222. * PCI pool facility.
  3223. */
  3224. static int megasas_create_frame_pool(struct megasas_instance *instance)
  3225. {
  3226. int i;
  3227. u32 max_cmd;
  3228. u32 sge_sz;
  3229. u32 total_sz;
  3230. u32 frame_count;
  3231. struct megasas_cmd *cmd;
  3232. max_cmd = instance->max_mfi_cmds;
  3233. /*
  3234. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  3235. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  3236. */
  3237. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  3238. sizeof(struct megasas_sge32);
  3239. if (instance->flag_ieee)
  3240. sge_sz = sizeof(struct megasas_sge_skinny);
  3241. /*
  3242. * For MFI controllers.
  3243. * max_num_sge = 60
  3244. * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
  3245. * Total 960 byte (15 MFI frame of 64 byte)
  3246. *
  3247. * Fusion adapter require only 3 extra frame.
  3248. * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
  3249. * max_sge_sz = 12 byte (sizeof megasas_sge64)
  3250. * Total 192 byte (3 MFI frame of 64 byte)
  3251. */
  3252. frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
  3253. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  3254. /*
  3255. * Use DMA pool facility provided by PCI layer
  3256. */
  3257. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  3258. instance->pdev, total_sz, 256, 0);
  3259. if (!instance->frame_dma_pool) {
  3260. dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
  3261. return -ENOMEM;
  3262. }
  3263. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  3264. instance->pdev, 128, 4, 0);
  3265. if (!instance->sense_dma_pool) {
  3266. dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
  3267. pci_pool_destroy(instance->frame_dma_pool);
  3268. instance->frame_dma_pool = NULL;
  3269. return -ENOMEM;
  3270. }
  3271. /*
  3272. * Allocate and attach a frame to each of the commands in cmd_list.
  3273. * By making cmd->index as the context instead of the &cmd, we can
  3274. * always use 32bit context regardless of the architecture
  3275. */
  3276. for (i = 0; i < max_cmd; i++) {
  3277. cmd = instance->cmd_list[i];
  3278. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  3279. GFP_KERNEL, &cmd->frame_phys_addr);
  3280. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  3281. GFP_KERNEL, &cmd->sense_phys_addr);
  3282. /*
  3283. * megasas_teardown_frame_pool() takes care of freeing
  3284. * whatever has been allocated
  3285. */
  3286. if (!cmd->frame || !cmd->sense) {
  3287. dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
  3288. megasas_teardown_frame_pool(instance);
  3289. return -ENOMEM;
  3290. }
  3291. memset(cmd->frame, 0, total_sz);
  3292. cmd->frame->io.context = cpu_to_le32(cmd->index);
  3293. cmd->frame->io.pad_0 = 0;
  3294. if (!instance->ctrl_context && reset_devices)
  3295. cmd->frame->hdr.cmd = MFI_CMD_INVALID;
  3296. }
  3297. return 0;
  3298. }
  3299. /**
  3300. * megasas_free_cmds - Free all the cmds in the free cmd pool
  3301. * @instance: Adapter soft state
  3302. */
  3303. void megasas_free_cmds(struct megasas_instance *instance)
  3304. {
  3305. int i;
  3306. /* First free the MFI frame pool */
  3307. megasas_teardown_frame_pool(instance);
  3308. /* Free all the commands in the cmd_list */
  3309. for (i = 0; i < instance->max_mfi_cmds; i++)
  3310. kfree(instance->cmd_list[i]);
  3311. /* Free the cmd_list buffer itself */
  3312. kfree(instance->cmd_list);
  3313. instance->cmd_list = NULL;
  3314. INIT_LIST_HEAD(&instance->cmd_pool);
  3315. }
  3316. /**
  3317. * megasas_alloc_cmds - Allocates the command packets
  3318. * @instance: Adapter soft state
  3319. *
  3320. * Each command that is issued to the FW, whether IO commands from the OS or
  3321. * internal commands like IOCTLs, are wrapped in local data structure called
  3322. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  3323. * the FW.
  3324. *
  3325. * Each frame has a 32-bit field called context (tag). This context is used
  3326. * to get back the megasas_cmd from the frame when a frame gets completed in
  3327. * the ISR. Typically the address of the megasas_cmd itself would be used as
  3328. * the context. But we wanted to keep the differences between 32 and 64 bit
  3329. * systems to the mininum. We always use 32 bit integers for the context. In
  3330. * this driver, the 32 bit values are the indices into an array cmd_list.
  3331. * This array is used only to look up the megasas_cmd given the context. The
  3332. * free commands themselves are maintained in a linked list called cmd_pool.
  3333. */
  3334. int megasas_alloc_cmds(struct megasas_instance *instance)
  3335. {
  3336. int i;
  3337. int j;
  3338. u32 max_cmd;
  3339. struct megasas_cmd *cmd;
  3340. struct fusion_context *fusion;
  3341. fusion = instance->ctrl_context;
  3342. max_cmd = instance->max_mfi_cmds;
  3343. /*
  3344. * instance->cmd_list is an array of struct megasas_cmd pointers.
  3345. * Allocate the dynamic array first and then allocate individual
  3346. * commands.
  3347. */
  3348. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  3349. if (!instance->cmd_list) {
  3350. dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
  3351. return -ENOMEM;
  3352. }
  3353. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  3354. for (i = 0; i < max_cmd; i++) {
  3355. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  3356. GFP_KERNEL);
  3357. if (!instance->cmd_list[i]) {
  3358. for (j = 0; j < i; j++)
  3359. kfree(instance->cmd_list[j]);
  3360. kfree(instance->cmd_list);
  3361. instance->cmd_list = NULL;
  3362. return -ENOMEM;
  3363. }
  3364. }
  3365. for (i = 0; i < max_cmd; i++) {
  3366. cmd = instance->cmd_list[i];
  3367. memset(cmd, 0, sizeof(struct megasas_cmd));
  3368. cmd->index = i;
  3369. cmd->scmd = NULL;
  3370. cmd->instance = instance;
  3371. list_add_tail(&cmd->list, &instance->cmd_pool);
  3372. }
  3373. /*
  3374. * Create a frame pool and assign one frame to each cmd
  3375. */
  3376. if (megasas_create_frame_pool(instance)) {
  3377. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
  3378. megasas_free_cmds(instance);
  3379. }
  3380. return 0;
  3381. }
  3382. /*
  3383. * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
  3384. * @instance: Adapter soft state
  3385. *
  3386. * Return 0 for only Fusion adapter, if driver load/unload is not in progress
  3387. * or FW is not under OCR.
  3388. */
  3389. inline int
  3390. dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
  3391. if (!instance->ctrl_context)
  3392. return KILL_ADAPTER;
  3393. else if (instance->unload ||
  3394. test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
  3395. return IGNORE_TIMEOUT;
  3396. else
  3397. return INITIATE_OCR;
  3398. }
  3399. static int
  3400. megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
  3401. {
  3402. int ret;
  3403. struct megasas_cmd *cmd;
  3404. struct megasas_dcmd_frame *dcmd;
  3405. cmd = megasas_get_cmd(instance);
  3406. if (!cmd) {
  3407. dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
  3408. return -ENOMEM;
  3409. }
  3410. dcmd = &cmd->frame->dcmd;
  3411. memset(instance->pd_info, 0, sizeof(*instance->pd_info));
  3412. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3413. dcmd->mbox.s[0] = cpu_to_le16(device_id);
  3414. dcmd->cmd = MFI_CMD_DCMD;
  3415. dcmd->cmd_status = 0xFF;
  3416. dcmd->sge_count = 1;
  3417. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3418. dcmd->timeout = 0;
  3419. dcmd->pad_0 = 0;
  3420. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
  3421. dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
  3422. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
  3423. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
  3424. if (instance->ctrl_context && !instance->mask_interrupts)
  3425. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3426. else
  3427. ret = megasas_issue_polled(instance, cmd);
  3428. switch (ret) {
  3429. case DCMD_SUCCESS:
  3430. instance->pd_list[device_id].interface =
  3431. instance->pd_info->state.ddf.pdType.intf;
  3432. break;
  3433. case DCMD_TIMEOUT:
  3434. switch (dcmd_timeout_ocr_possible(instance)) {
  3435. case INITIATE_OCR:
  3436. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3437. megasas_reset_fusion(instance->host,
  3438. MFI_IO_TIMEOUT_OCR);
  3439. break;
  3440. case KILL_ADAPTER:
  3441. megaraid_sas_kill_hba(instance);
  3442. break;
  3443. case IGNORE_TIMEOUT:
  3444. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3445. __func__, __LINE__);
  3446. break;
  3447. }
  3448. break;
  3449. }
  3450. if (ret != DCMD_TIMEOUT)
  3451. megasas_return_cmd(instance, cmd);
  3452. return ret;
  3453. }
  3454. /*
  3455. * megasas_get_pd_list_info - Returns FW's pd_list structure
  3456. * @instance: Adapter soft state
  3457. * @pd_list: pd_list structure
  3458. *
  3459. * Issues an internal command (DCMD) to get the FW's controller PD
  3460. * list structure. This information is mainly used to find out SYSTEM
  3461. * supported by the FW.
  3462. */
  3463. static int
  3464. megasas_get_pd_list(struct megasas_instance *instance)
  3465. {
  3466. int ret = 0, pd_index = 0;
  3467. struct megasas_cmd *cmd;
  3468. struct megasas_dcmd_frame *dcmd;
  3469. struct MR_PD_LIST *ci;
  3470. struct MR_PD_ADDRESS *pd_addr;
  3471. dma_addr_t ci_h = 0;
  3472. if (instance->pd_list_not_supported) {
  3473. dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
  3474. "not supported by firmware\n");
  3475. return ret;
  3476. }
  3477. cmd = megasas_get_cmd(instance);
  3478. if (!cmd) {
  3479. dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
  3480. return -ENOMEM;
  3481. }
  3482. dcmd = &cmd->frame->dcmd;
  3483. ci = pci_alloc_consistent(instance->pdev,
  3484. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  3485. if (!ci) {
  3486. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
  3487. megasas_return_cmd(instance, cmd);
  3488. return -ENOMEM;
  3489. }
  3490. memset(ci, 0, sizeof(*ci));
  3491. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3492. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  3493. dcmd->mbox.b[1] = 0;
  3494. dcmd->cmd = MFI_CMD_DCMD;
  3495. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3496. dcmd->sge_count = 1;
  3497. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3498. dcmd->timeout = 0;
  3499. dcmd->pad_0 = 0;
  3500. dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
  3501. dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
  3502. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3503. dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
  3504. if (instance->ctrl_context && !instance->mask_interrupts)
  3505. ret = megasas_issue_blocked_cmd(instance, cmd,
  3506. MFI_IO_TIMEOUT_SECS);
  3507. else
  3508. ret = megasas_issue_polled(instance, cmd);
  3509. switch (ret) {
  3510. case DCMD_FAILED:
  3511. dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
  3512. "failed/not supported by firmware\n");
  3513. if (instance->ctrl_context)
  3514. megaraid_sas_kill_hba(instance);
  3515. else
  3516. instance->pd_list_not_supported = 1;
  3517. break;
  3518. case DCMD_TIMEOUT:
  3519. switch (dcmd_timeout_ocr_possible(instance)) {
  3520. case INITIATE_OCR:
  3521. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3522. /*
  3523. * DCMD failed from AEN path.
  3524. * AEN path already hold reset_mutex to avoid PCI access
  3525. * while OCR is in progress.
  3526. */
  3527. mutex_unlock(&instance->reset_mutex);
  3528. megasas_reset_fusion(instance->host,
  3529. MFI_IO_TIMEOUT_OCR);
  3530. mutex_lock(&instance->reset_mutex);
  3531. break;
  3532. case KILL_ADAPTER:
  3533. megaraid_sas_kill_hba(instance);
  3534. break;
  3535. case IGNORE_TIMEOUT:
  3536. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
  3537. __func__, __LINE__);
  3538. break;
  3539. }
  3540. break;
  3541. case DCMD_SUCCESS:
  3542. pd_addr = ci->addr;
  3543. if ((le32_to_cpu(ci->count) >
  3544. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
  3545. break;
  3546. memset(instance->local_pd_list, 0,
  3547. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  3548. for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
  3549. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
  3550. le16_to_cpu(pd_addr->deviceId);
  3551. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
  3552. pd_addr->scsiDevType;
  3553. instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
  3554. MR_PD_STATE_SYSTEM;
  3555. pd_addr++;
  3556. }
  3557. memcpy(instance->pd_list, instance->local_pd_list,
  3558. sizeof(instance->pd_list));
  3559. break;
  3560. }
  3561. pci_free_consistent(instance->pdev,
  3562. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  3563. ci, ci_h);
  3564. if (ret != DCMD_TIMEOUT)
  3565. megasas_return_cmd(instance, cmd);
  3566. return ret;
  3567. }
  3568. /*
  3569. * megasas_get_ld_list_info - Returns FW's ld_list structure
  3570. * @instance: Adapter soft state
  3571. * @ld_list: ld_list structure
  3572. *
  3573. * Issues an internal command (DCMD) to get the FW's controller PD
  3574. * list structure. This information is mainly used to find out SYSTEM
  3575. * supported by the FW.
  3576. */
  3577. static int
  3578. megasas_get_ld_list(struct megasas_instance *instance)
  3579. {
  3580. int ret = 0, ld_index = 0, ids = 0;
  3581. struct megasas_cmd *cmd;
  3582. struct megasas_dcmd_frame *dcmd;
  3583. struct MR_LD_LIST *ci;
  3584. dma_addr_t ci_h = 0;
  3585. u32 ld_count;
  3586. cmd = megasas_get_cmd(instance);
  3587. if (!cmd) {
  3588. dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
  3589. return -ENOMEM;
  3590. }
  3591. dcmd = &cmd->frame->dcmd;
  3592. ci = pci_alloc_consistent(instance->pdev,
  3593. sizeof(struct MR_LD_LIST),
  3594. &ci_h);
  3595. if (!ci) {
  3596. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
  3597. megasas_return_cmd(instance, cmd);
  3598. return -ENOMEM;
  3599. }
  3600. memset(ci, 0, sizeof(*ci));
  3601. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3602. if (instance->supportmax256vd)
  3603. dcmd->mbox.b[0] = 1;
  3604. dcmd->cmd = MFI_CMD_DCMD;
  3605. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3606. dcmd->sge_count = 1;
  3607. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3608. dcmd->timeout = 0;
  3609. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
  3610. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
  3611. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3612. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
  3613. dcmd->pad_0 = 0;
  3614. if (instance->ctrl_context && !instance->mask_interrupts)
  3615. ret = megasas_issue_blocked_cmd(instance, cmd,
  3616. MFI_IO_TIMEOUT_SECS);
  3617. else
  3618. ret = megasas_issue_polled(instance, cmd);
  3619. ld_count = le32_to_cpu(ci->ldCount);
  3620. switch (ret) {
  3621. case DCMD_FAILED:
  3622. megaraid_sas_kill_hba(instance);
  3623. break;
  3624. case DCMD_TIMEOUT:
  3625. switch (dcmd_timeout_ocr_possible(instance)) {
  3626. case INITIATE_OCR:
  3627. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3628. /*
  3629. * DCMD failed from AEN path.
  3630. * AEN path already hold reset_mutex to avoid PCI access
  3631. * while OCR is in progress.
  3632. */
  3633. mutex_unlock(&instance->reset_mutex);
  3634. megasas_reset_fusion(instance->host,
  3635. MFI_IO_TIMEOUT_OCR);
  3636. mutex_lock(&instance->reset_mutex);
  3637. break;
  3638. case KILL_ADAPTER:
  3639. megaraid_sas_kill_hba(instance);
  3640. break;
  3641. case IGNORE_TIMEOUT:
  3642. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3643. __func__, __LINE__);
  3644. break;
  3645. }
  3646. break;
  3647. case DCMD_SUCCESS:
  3648. if (ld_count > instance->fw_supported_vd_count)
  3649. break;
  3650. memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
  3651. for (ld_index = 0; ld_index < ld_count; ld_index++) {
  3652. if (ci->ldList[ld_index].state != 0) {
  3653. ids = ci->ldList[ld_index].ref.targetId;
  3654. instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
  3655. }
  3656. }
  3657. break;
  3658. }
  3659. pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
  3660. if (ret != DCMD_TIMEOUT)
  3661. megasas_return_cmd(instance, cmd);
  3662. return ret;
  3663. }
  3664. /**
  3665. * megasas_ld_list_query - Returns FW's ld_list structure
  3666. * @instance: Adapter soft state
  3667. * @ld_list: ld_list structure
  3668. *
  3669. * Issues an internal command (DCMD) to get the FW's controller PD
  3670. * list structure. This information is mainly used to find out SYSTEM
  3671. * supported by the FW.
  3672. */
  3673. static int
  3674. megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
  3675. {
  3676. int ret = 0, ld_index = 0, ids = 0;
  3677. struct megasas_cmd *cmd;
  3678. struct megasas_dcmd_frame *dcmd;
  3679. struct MR_LD_TARGETID_LIST *ci;
  3680. dma_addr_t ci_h = 0;
  3681. u32 tgtid_count;
  3682. cmd = megasas_get_cmd(instance);
  3683. if (!cmd) {
  3684. dev_warn(&instance->pdev->dev,
  3685. "megasas_ld_list_query: Failed to get cmd\n");
  3686. return -ENOMEM;
  3687. }
  3688. dcmd = &cmd->frame->dcmd;
  3689. ci = pci_alloc_consistent(instance->pdev,
  3690. sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
  3691. if (!ci) {
  3692. dev_warn(&instance->pdev->dev,
  3693. "Failed to alloc mem for ld_list_query\n");
  3694. megasas_return_cmd(instance, cmd);
  3695. return -ENOMEM;
  3696. }
  3697. memset(ci, 0, sizeof(*ci));
  3698. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3699. dcmd->mbox.b[0] = query_type;
  3700. if (instance->supportmax256vd)
  3701. dcmd->mbox.b[2] = 1;
  3702. dcmd->cmd = MFI_CMD_DCMD;
  3703. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3704. dcmd->sge_count = 1;
  3705. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3706. dcmd->timeout = 0;
  3707. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
  3708. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
  3709. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3710. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
  3711. dcmd->pad_0 = 0;
  3712. if (instance->ctrl_context && !instance->mask_interrupts)
  3713. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3714. else
  3715. ret = megasas_issue_polled(instance, cmd);
  3716. switch (ret) {
  3717. case DCMD_FAILED:
  3718. dev_info(&instance->pdev->dev,
  3719. "DCMD not supported by firmware - %s %d\n",
  3720. __func__, __LINE__);
  3721. ret = megasas_get_ld_list(instance);
  3722. break;
  3723. case DCMD_TIMEOUT:
  3724. switch (dcmd_timeout_ocr_possible(instance)) {
  3725. case INITIATE_OCR:
  3726. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3727. /*
  3728. * DCMD failed from AEN path.
  3729. * AEN path already hold reset_mutex to avoid PCI access
  3730. * while OCR is in progress.
  3731. */
  3732. mutex_unlock(&instance->reset_mutex);
  3733. megasas_reset_fusion(instance->host,
  3734. MFI_IO_TIMEOUT_OCR);
  3735. mutex_lock(&instance->reset_mutex);
  3736. break;
  3737. case KILL_ADAPTER:
  3738. megaraid_sas_kill_hba(instance);
  3739. break;
  3740. case IGNORE_TIMEOUT:
  3741. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3742. __func__, __LINE__);
  3743. break;
  3744. }
  3745. break;
  3746. case DCMD_SUCCESS:
  3747. tgtid_count = le32_to_cpu(ci->count);
  3748. if ((tgtid_count > (instance->fw_supported_vd_count)))
  3749. break;
  3750. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3751. for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
  3752. ids = ci->targetId[ld_index];
  3753. instance->ld_ids[ids] = ci->targetId[ld_index];
  3754. }
  3755. break;
  3756. }
  3757. pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
  3758. ci, ci_h);
  3759. if (ret != DCMD_TIMEOUT)
  3760. megasas_return_cmd(instance, cmd);
  3761. return ret;
  3762. }
  3763. /*
  3764. * megasas_update_ext_vd_details : Update details w.r.t Extended VD
  3765. * instance : Controller's instance
  3766. */
  3767. static void megasas_update_ext_vd_details(struct megasas_instance *instance)
  3768. {
  3769. struct fusion_context *fusion;
  3770. u32 old_map_sz;
  3771. u32 new_map_sz;
  3772. fusion = instance->ctrl_context;
  3773. /* For MFI based controllers return dummy success */
  3774. if (!fusion)
  3775. return;
  3776. instance->supportmax256vd =
  3777. instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
  3778. /* Below is additional check to address future FW enhancement */
  3779. if (instance->ctrl_info->max_lds > 64)
  3780. instance->supportmax256vd = 1;
  3781. instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
  3782. * MEGASAS_MAX_DEV_PER_CHANNEL;
  3783. instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
  3784. * MEGASAS_MAX_DEV_PER_CHANNEL;
  3785. if (instance->supportmax256vd) {
  3786. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
  3787. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  3788. } else {
  3789. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
  3790. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  3791. }
  3792. dev_info(&instance->pdev->dev,
  3793. "firmware type\t: %s\n",
  3794. instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
  3795. "Legacy(64 VD) firmware");
  3796. old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
  3797. (sizeof(struct MR_LD_SPAN_MAP) *
  3798. (instance->fw_supported_vd_count - 1));
  3799. new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
  3800. fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
  3801. (sizeof(struct MR_LD_SPAN_MAP) *
  3802. (instance->drv_supported_vd_count - 1));
  3803. fusion->max_map_sz = max(old_map_sz, new_map_sz);
  3804. if (instance->supportmax256vd)
  3805. fusion->current_map_sz = new_map_sz;
  3806. else
  3807. fusion->current_map_sz = old_map_sz;
  3808. }
  3809. /**
  3810. * megasas_get_controller_info - Returns FW's controller structure
  3811. * @instance: Adapter soft state
  3812. *
  3813. * Issues an internal command (DCMD) to get the FW's controller structure.
  3814. * This information is mainly used to find out the maximum IO transfer per
  3815. * command supported by the FW.
  3816. */
  3817. int
  3818. megasas_get_ctrl_info(struct megasas_instance *instance)
  3819. {
  3820. int ret = 0;
  3821. struct megasas_cmd *cmd;
  3822. struct megasas_dcmd_frame *dcmd;
  3823. struct megasas_ctrl_info *ci;
  3824. struct megasas_ctrl_info *ctrl_info;
  3825. dma_addr_t ci_h = 0;
  3826. ctrl_info = instance->ctrl_info;
  3827. cmd = megasas_get_cmd(instance);
  3828. if (!cmd) {
  3829. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
  3830. return -ENOMEM;
  3831. }
  3832. dcmd = &cmd->frame->dcmd;
  3833. ci = pci_alloc_consistent(instance->pdev,
  3834. sizeof(struct megasas_ctrl_info), &ci_h);
  3835. if (!ci) {
  3836. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
  3837. megasas_return_cmd(instance, cmd);
  3838. return -ENOMEM;
  3839. }
  3840. memset(ci, 0, sizeof(*ci));
  3841. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3842. dcmd->cmd = MFI_CMD_DCMD;
  3843. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3844. dcmd->sge_count = 1;
  3845. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  3846. dcmd->timeout = 0;
  3847. dcmd->pad_0 = 0;
  3848. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
  3849. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
  3850. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  3851. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
  3852. dcmd->mbox.b[0] = 1;
  3853. if (instance->ctrl_context && !instance->mask_interrupts)
  3854. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3855. else
  3856. ret = megasas_issue_polled(instance, cmd);
  3857. switch (ret) {
  3858. case DCMD_SUCCESS:
  3859. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  3860. /* Save required controller information in
  3861. * CPU endianness format.
  3862. */
  3863. le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
  3864. le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
  3865. le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
  3866. /* Update the latest Ext VD info.
  3867. * From Init path, store current firmware details.
  3868. * From OCR path, detect any firmware properties changes.
  3869. * in case of Firmware upgrade without system reboot.
  3870. */
  3871. megasas_update_ext_vd_details(instance);
  3872. instance->use_seqnum_jbod_fp =
  3873. ctrl_info->adapterOperations3.useSeqNumJbodFP;
  3874. /*Check whether controller is iMR or MR */
  3875. instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
  3876. dev_info(&instance->pdev->dev,
  3877. "controller type\t: %s(%dMB)\n",
  3878. instance->is_imr ? "iMR" : "MR",
  3879. le16_to_cpu(ctrl_info->memory_size));
  3880. instance->disableOnlineCtrlReset =
  3881. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3882. instance->secure_jbod_support =
  3883. ctrl_info->adapterOperations3.supportSecurityonJBOD;
  3884. dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
  3885. instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
  3886. dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
  3887. instance->secure_jbod_support ? "Yes" : "No");
  3888. break;
  3889. case DCMD_TIMEOUT:
  3890. switch (dcmd_timeout_ocr_possible(instance)) {
  3891. case INITIATE_OCR:
  3892. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3893. megasas_reset_fusion(instance->host,
  3894. MFI_IO_TIMEOUT_OCR);
  3895. break;
  3896. case KILL_ADAPTER:
  3897. megaraid_sas_kill_hba(instance);
  3898. break;
  3899. case IGNORE_TIMEOUT:
  3900. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3901. __func__, __LINE__);
  3902. break;
  3903. }
  3904. case DCMD_FAILED:
  3905. megaraid_sas_kill_hba(instance);
  3906. break;
  3907. }
  3908. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  3909. ci, ci_h);
  3910. megasas_return_cmd(instance, cmd);
  3911. return ret;
  3912. }
  3913. /*
  3914. * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
  3915. * to firmware
  3916. *
  3917. * @instance: Adapter soft state
  3918. * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
  3919. MR_CRASH_BUF_TURN_OFF = 0
  3920. MR_CRASH_BUF_TURN_ON = 1
  3921. * @return 0 on success non-zero on failure.
  3922. * Issues an internal command (DCMD) to set parameters for crash dump feature.
  3923. * Driver will send address of crash dump DMA buffer and set mbox to tell FW
  3924. * that driver supports crash dump feature. This DCMD will be sent only if
  3925. * crash dump feature is supported by the FW.
  3926. *
  3927. */
  3928. int megasas_set_crash_dump_params(struct megasas_instance *instance,
  3929. u8 crash_buf_state)
  3930. {
  3931. int ret = 0;
  3932. struct megasas_cmd *cmd;
  3933. struct megasas_dcmd_frame *dcmd;
  3934. cmd = megasas_get_cmd(instance);
  3935. if (!cmd) {
  3936. dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
  3937. return -ENOMEM;
  3938. }
  3939. dcmd = &cmd->frame->dcmd;
  3940. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3941. dcmd->mbox.b[0] = crash_buf_state;
  3942. dcmd->cmd = MFI_CMD_DCMD;
  3943. dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
  3944. dcmd->sge_count = 1;
  3945. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  3946. dcmd->timeout = 0;
  3947. dcmd->pad_0 = 0;
  3948. dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
  3949. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
  3950. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
  3951. dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
  3952. if (instance->ctrl_context && !instance->mask_interrupts)
  3953. ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
  3954. else
  3955. ret = megasas_issue_polled(instance, cmd);
  3956. if (ret == DCMD_TIMEOUT) {
  3957. switch (dcmd_timeout_ocr_possible(instance)) {
  3958. case INITIATE_OCR:
  3959. cmd->flags |= DRV_DCMD_SKIP_REFIRE;
  3960. megasas_reset_fusion(instance->host,
  3961. MFI_IO_TIMEOUT_OCR);
  3962. break;
  3963. case KILL_ADAPTER:
  3964. megaraid_sas_kill_hba(instance);
  3965. break;
  3966. case IGNORE_TIMEOUT:
  3967. dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
  3968. __func__, __LINE__);
  3969. break;
  3970. }
  3971. } else
  3972. megasas_return_cmd(instance, cmd);
  3973. return ret;
  3974. }
  3975. /**
  3976. * megasas_issue_init_mfi - Initializes the FW
  3977. * @instance: Adapter soft state
  3978. *
  3979. * Issues the INIT MFI cmd
  3980. */
  3981. static int
  3982. megasas_issue_init_mfi(struct megasas_instance *instance)
  3983. {
  3984. __le32 context;
  3985. struct megasas_cmd *cmd;
  3986. struct megasas_init_frame *init_frame;
  3987. struct megasas_init_queue_info *initq_info;
  3988. dma_addr_t init_frame_h;
  3989. dma_addr_t initq_info_h;
  3990. /*
  3991. * Prepare a init frame. Note the init frame points to queue info
  3992. * structure. Each frame has SGL allocated after first 64 bytes. For
  3993. * this frame - since we don't need any SGL - we use SGL's space as
  3994. * queue info structure
  3995. *
  3996. * We will not get a NULL command below. We just created the pool.
  3997. */
  3998. cmd = megasas_get_cmd(instance);
  3999. init_frame = (struct megasas_init_frame *)cmd->frame;
  4000. initq_info = (struct megasas_init_queue_info *)
  4001. ((unsigned long)init_frame + 64);
  4002. init_frame_h = cmd->frame_phys_addr;
  4003. initq_info_h = init_frame_h + 64;
  4004. context = init_frame->context;
  4005. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  4006. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  4007. init_frame->context = context;
  4008. initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
  4009. initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
  4010. initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
  4011. initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
  4012. init_frame->cmd = MFI_CMD_INIT;
  4013. init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
  4014. init_frame->queue_info_new_phys_addr_lo =
  4015. cpu_to_le32(lower_32_bits(initq_info_h));
  4016. init_frame->queue_info_new_phys_addr_hi =
  4017. cpu_to_le32(upper_32_bits(initq_info_h));
  4018. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
  4019. /*
  4020. * disable the intr before firing the init frame to FW
  4021. */
  4022. instance->instancet->disable_intr(instance);
  4023. /*
  4024. * Issue the init frame in polled mode
  4025. */
  4026. if (megasas_issue_polled(instance, cmd)) {
  4027. dev_err(&instance->pdev->dev, "Failed to init firmware\n");
  4028. megasas_return_cmd(instance, cmd);
  4029. goto fail_fw_init;
  4030. }
  4031. megasas_return_cmd(instance, cmd);
  4032. return 0;
  4033. fail_fw_init:
  4034. return -EINVAL;
  4035. }
  4036. static u32
  4037. megasas_init_adapter_mfi(struct megasas_instance *instance)
  4038. {
  4039. struct megasas_register_set __iomem *reg_set;
  4040. u32 context_sz;
  4041. u32 reply_q_sz;
  4042. reg_set = instance->reg_set;
  4043. /*
  4044. * Get various operational parameters from status register
  4045. */
  4046. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  4047. /*
  4048. * Reduce the max supported cmds by 1. This is to ensure that the
  4049. * reply_q_sz (1 more than the max cmd that driver may send)
  4050. * does not exceed max cmds that the FW can support
  4051. */
  4052. instance->max_fw_cmds = instance->max_fw_cmds-1;
  4053. instance->max_mfi_cmds = instance->max_fw_cmds;
  4054. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  4055. 0x10;
  4056. /*
  4057. * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
  4058. * are reserved for IOCTL + driver's internal DCMDs.
  4059. */
  4060. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  4061. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  4062. instance->max_scsi_cmds = (instance->max_fw_cmds -
  4063. MEGASAS_SKINNY_INT_CMDS);
  4064. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  4065. } else {
  4066. instance->max_scsi_cmds = (instance->max_fw_cmds -
  4067. MEGASAS_INT_CMDS);
  4068. sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
  4069. }
  4070. instance->cur_can_queue = instance->max_scsi_cmds;
  4071. /*
  4072. * Create a pool of commands
  4073. */
  4074. if (megasas_alloc_cmds(instance))
  4075. goto fail_alloc_cmds;
  4076. /*
  4077. * Allocate memory for reply queue. Length of reply queue should
  4078. * be _one_ more than the maximum commands handled by the firmware.
  4079. *
  4080. * Note: When FW completes commands, it places corresponding contex
  4081. * values in this circular reply queue. This circular queue is a fairly
  4082. * typical producer-consumer queue. FW is the producer (of completed
  4083. * commands) and the driver is the consumer.
  4084. */
  4085. context_sz = sizeof(u32);
  4086. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  4087. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  4088. reply_q_sz,
  4089. &instance->reply_queue_h);
  4090. if (!instance->reply_queue) {
  4091. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
  4092. goto fail_reply_queue;
  4093. }
  4094. if (megasas_issue_init_mfi(instance))
  4095. goto fail_fw_init;
  4096. if (megasas_get_ctrl_info(instance)) {
  4097. dev_err(&instance->pdev->dev, "(%d): Could get controller info "
  4098. "Fail from %s %d\n", instance->unique_id,
  4099. __func__, __LINE__);
  4100. goto fail_fw_init;
  4101. }
  4102. instance->fw_support_ieee = 0;
  4103. instance->fw_support_ieee =
  4104. (instance->instancet->read_fw_status_reg(reg_set) &
  4105. 0x04000000);
  4106. dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
  4107. instance->fw_support_ieee);
  4108. if (instance->fw_support_ieee)
  4109. instance->flag_ieee = 1;
  4110. return 0;
  4111. fail_fw_init:
  4112. pci_free_consistent(instance->pdev, reply_q_sz,
  4113. instance->reply_queue, instance->reply_queue_h);
  4114. fail_reply_queue:
  4115. megasas_free_cmds(instance);
  4116. fail_alloc_cmds:
  4117. return 1;
  4118. }
  4119. /*
  4120. * megasas_setup_irqs_msix - register legacy interrupts.
  4121. * @instance: Adapter soft state
  4122. *
  4123. * Do not enable interrupt, only setup ISRs.
  4124. *
  4125. * Return 0 on success.
  4126. */
  4127. static int
  4128. megasas_setup_irqs_ioapic(struct megasas_instance *instance)
  4129. {
  4130. struct pci_dev *pdev;
  4131. pdev = instance->pdev;
  4132. instance->irq_context[0].instance = instance;
  4133. instance->irq_context[0].MSIxIndex = 0;
  4134. if (request_irq(pdev->irq, instance->instancet->service_isr,
  4135. IRQF_SHARED, "megasas", &instance->irq_context[0])) {
  4136. dev_err(&instance->pdev->dev,
  4137. "Failed to register IRQ from %s %d\n",
  4138. __func__, __LINE__);
  4139. return -1;
  4140. }
  4141. return 0;
  4142. }
  4143. /**
  4144. * megasas_setup_irqs_msix - register MSI-x interrupts.
  4145. * @instance: Adapter soft state
  4146. * @is_probe: Driver probe check
  4147. *
  4148. * Do not enable interrupt, only setup ISRs.
  4149. *
  4150. * Return 0 on success.
  4151. */
  4152. static int
  4153. megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
  4154. {
  4155. int i, j, cpu;
  4156. struct pci_dev *pdev;
  4157. pdev = instance->pdev;
  4158. /* Try MSI-x */
  4159. cpu = cpumask_first(cpu_online_mask);
  4160. for (i = 0; i < instance->msix_vectors; i++) {
  4161. instance->irq_context[i].instance = instance;
  4162. instance->irq_context[i].MSIxIndex = i;
  4163. if (request_irq(instance->msixentry[i].vector,
  4164. instance->instancet->service_isr, 0, "megasas",
  4165. &instance->irq_context[i])) {
  4166. dev_err(&instance->pdev->dev,
  4167. "Failed to register IRQ for vector %d.\n", i);
  4168. for (j = 0; j < i; j++) {
  4169. if (smp_affinity_enable)
  4170. irq_set_affinity_hint(
  4171. instance->msixentry[j].vector, NULL);
  4172. free_irq(instance->msixentry[j].vector,
  4173. &instance->irq_context[j]);
  4174. }
  4175. /* Retry irq register for IO_APIC*/
  4176. instance->msix_vectors = 0;
  4177. if (is_probe)
  4178. return megasas_setup_irqs_ioapic(instance);
  4179. else
  4180. return -1;
  4181. }
  4182. if (smp_affinity_enable) {
  4183. if (irq_set_affinity_hint(instance->msixentry[i].vector,
  4184. get_cpu_mask(cpu)))
  4185. dev_err(&instance->pdev->dev,
  4186. "Failed to set affinity hint"
  4187. " for cpu %d\n", cpu);
  4188. cpu = cpumask_next(cpu, cpu_online_mask);
  4189. }
  4190. }
  4191. return 0;
  4192. }
  4193. /*
  4194. * megasas_destroy_irqs- unregister interrupts.
  4195. * @instance: Adapter soft state
  4196. * return: void
  4197. */
  4198. static void
  4199. megasas_destroy_irqs(struct megasas_instance *instance) {
  4200. int i;
  4201. if (instance->msix_vectors)
  4202. for (i = 0; i < instance->msix_vectors; i++) {
  4203. if (smp_affinity_enable)
  4204. irq_set_affinity_hint(
  4205. instance->msixentry[i].vector, NULL);
  4206. free_irq(instance->msixentry[i].vector,
  4207. &instance->irq_context[i]);
  4208. }
  4209. else
  4210. free_irq(instance->pdev->irq, &instance->irq_context[0]);
  4211. }
  4212. /**
  4213. * megasas_setup_jbod_map - setup jbod map for FP seq_number.
  4214. * @instance: Adapter soft state
  4215. * @is_probe: Driver probe check
  4216. *
  4217. * Return 0 on success.
  4218. */
  4219. void
  4220. megasas_setup_jbod_map(struct megasas_instance *instance)
  4221. {
  4222. int i;
  4223. struct fusion_context *fusion = instance->ctrl_context;
  4224. u32 pd_seq_map_sz;
  4225. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  4226. (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
  4227. if (reset_devices || !fusion ||
  4228. !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
  4229. dev_info(&instance->pdev->dev,
  4230. "Jbod map is not supported %s %d\n",
  4231. __func__, __LINE__);
  4232. instance->use_seqnum_jbod_fp = false;
  4233. return;
  4234. }
  4235. if (fusion->pd_seq_sync[0])
  4236. goto skip_alloc;
  4237. for (i = 0; i < JBOD_MAPS_COUNT; i++) {
  4238. fusion->pd_seq_sync[i] = dma_alloc_coherent
  4239. (&instance->pdev->dev, pd_seq_map_sz,
  4240. &fusion->pd_seq_phys[i], GFP_KERNEL);
  4241. if (!fusion->pd_seq_sync[i]) {
  4242. dev_err(&instance->pdev->dev,
  4243. "Failed to allocate memory from %s %d\n",
  4244. __func__, __LINE__);
  4245. if (i == 1) {
  4246. dma_free_coherent(&instance->pdev->dev,
  4247. pd_seq_map_sz, fusion->pd_seq_sync[0],
  4248. fusion->pd_seq_phys[0]);
  4249. fusion->pd_seq_sync[0] = NULL;
  4250. }
  4251. instance->use_seqnum_jbod_fp = false;
  4252. return;
  4253. }
  4254. }
  4255. skip_alloc:
  4256. if (!megasas_sync_pd_seq_num(instance, false) &&
  4257. !megasas_sync_pd_seq_num(instance, true))
  4258. instance->use_seqnum_jbod_fp = true;
  4259. else
  4260. instance->use_seqnum_jbod_fp = false;
  4261. }
  4262. /**
  4263. * megasas_init_fw - Initializes the FW
  4264. * @instance: Adapter soft state
  4265. *
  4266. * This is the main function for initializing firmware
  4267. */
  4268. static int megasas_init_fw(struct megasas_instance *instance)
  4269. {
  4270. u32 max_sectors_1;
  4271. u32 max_sectors_2;
  4272. u32 tmp_sectors, msix_enable, scratch_pad_2;
  4273. resource_size_t base_addr;
  4274. struct megasas_register_set __iomem *reg_set;
  4275. struct megasas_ctrl_info *ctrl_info = NULL;
  4276. unsigned long bar_list;
  4277. int i, loop, fw_msix_count = 0;
  4278. struct IOV_111 *iovPtr;
  4279. struct fusion_context *fusion;
  4280. fusion = instance->ctrl_context;
  4281. /* Find first memory bar */
  4282. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  4283. instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
  4284. if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
  4285. "megasas: LSI")) {
  4286. dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
  4287. return -EBUSY;
  4288. }
  4289. base_addr = pci_resource_start(instance->pdev, instance->bar);
  4290. instance->reg_set = ioremap_nocache(base_addr, 8192);
  4291. if (!instance->reg_set) {
  4292. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
  4293. goto fail_ioremap;
  4294. }
  4295. reg_set = instance->reg_set;
  4296. switch (instance->pdev->device) {
  4297. case PCI_DEVICE_ID_LSI_FUSION:
  4298. case PCI_DEVICE_ID_LSI_PLASMA:
  4299. case PCI_DEVICE_ID_LSI_INVADER:
  4300. case PCI_DEVICE_ID_LSI_FURY:
  4301. case PCI_DEVICE_ID_LSI_INTRUDER:
  4302. case PCI_DEVICE_ID_LSI_INTRUDER_24:
  4303. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  4304. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  4305. instance->instancet = &megasas_instance_template_fusion;
  4306. break;
  4307. case PCI_DEVICE_ID_LSI_SAS1078R:
  4308. case PCI_DEVICE_ID_LSI_SAS1078DE:
  4309. instance->instancet = &megasas_instance_template_ppc;
  4310. break;
  4311. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  4312. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  4313. instance->instancet = &megasas_instance_template_gen2;
  4314. break;
  4315. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  4316. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  4317. instance->instancet = &megasas_instance_template_skinny;
  4318. break;
  4319. case PCI_DEVICE_ID_LSI_SAS1064R:
  4320. case PCI_DEVICE_ID_DELL_PERC5:
  4321. default:
  4322. instance->instancet = &megasas_instance_template_xscale;
  4323. break;
  4324. }
  4325. if (megasas_transition_to_ready(instance, 0)) {
  4326. atomic_set(&instance->fw_reset_no_pci_access, 1);
  4327. instance->instancet->adp_reset
  4328. (instance, instance->reg_set);
  4329. atomic_set(&instance->fw_reset_no_pci_access, 0);
  4330. dev_info(&instance->pdev->dev,
  4331. "FW restarted successfully from %s!\n",
  4332. __func__);
  4333. /*waitting for about 30 second before retry*/
  4334. ssleep(30);
  4335. if (megasas_transition_to_ready(instance, 0))
  4336. goto fail_ready_state;
  4337. }
  4338. /*
  4339. * MSI-X host index 0 is common for all adapter.
  4340. * It is used for all MPT based Adapters.
  4341. */
  4342. instance->reply_post_host_index_addr[0] =
  4343. (u32 __iomem *)((u8 __iomem *)instance->reg_set +
  4344. MPI2_REPLY_POST_HOST_INDEX_OFFSET);
  4345. /* Check if MSI-X is supported while in ready state */
  4346. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  4347. 0x4000000) >> 0x1a;
  4348. if (msix_enable && !msix_disable) {
  4349. scratch_pad_2 = readl
  4350. (&instance->reg_set->outbound_scratch_pad_2);
  4351. /* Check max MSI-X vectors */
  4352. if (fusion) {
  4353. if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
  4354. instance->msix_vectors = (scratch_pad_2
  4355. & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
  4356. fw_msix_count = instance->msix_vectors;
  4357. } else { /* Invader series supports more than 8 MSI-x vectors*/
  4358. instance->msix_vectors = ((scratch_pad_2
  4359. & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
  4360. >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
  4361. if (rdpq_enable)
  4362. instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
  4363. 1 : 0;
  4364. fw_msix_count = instance->msix_vectors;
  4365. /* Save 1-15 reply post index address to local memory
  4366. * Index 0 is already saved from reg offset
  4367. * MPI2_REPLY_POST_HOST_INDEX_OFFSET
  4368. */
  4369. for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
  4370. instance->reply_post_host_index_addr[loop] =
  4371. (u32 __iomem *)
  4372. ((u8 __iomem *)instance->reg_set +
  4373. MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
  4374. + (loop * 0x10));
  4375. }
  4376. }
  4377. if (msix_vectors)
  4378. instance->msix_vectors = min(msix_vectors,
  4379. instance->msix_vectors);
  4380. } else /* MFI adapters */
  4381. instance->msix_vectors = 1;
  4382. /* Don't bother allocating more MSI-X vectors than cpus */
  4383. instance->msix_vectors = min(instance->msix_vectors,
  4384. (unsigned int)num_online_cpus());
  4385. for (i = 0; i < instance->msix_vectors; i++)
  4386. instance->msixentry[i].entry = i;
  4387. i = pci_enable_msix_range(instance->pdev, instance->msixentry,
  4388. 1, instance->msix_vectors);
  4389. if (i > 0)
  4390. instance->msix_vectors = i;
  4391. else
  4392. instance->msix_vectors = 0;
  4393. }
  4394. dev_info(&instance->pdev->dev,
  4395. "firmware supports msix\t: (%d)", fw_msix_count);
  4396. dev_info(&instance->pdev->dev,
  4397. "current msix/online cpus\t: (%d/%d)\n",
  4398. instance->msix_vectors, (unsigned int)num_online_cpus());
  4399. dev_info(&instance->pdev->dev,
  4400. "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
  4401. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  4402. (unsigned long)instance);
  4403. if (instance->msix_vectors ?
  4404. megasas_setup_irqs_msix(instance, 1) :
  4405. megasas_setup_irqs_ioapic(instance))
  4406. goto fail_setup_irqs;
  4407. instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
  4408. GFP_KERNEL);
  4409. if (instance->ctrl_info == NULL)
  4410. goto fail_init_adapter;
  4411. /*
  4412. * Below are default value for legacy Firmware.
  4413. * non-fusion based controllers
  4414. */
  4415. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
  4416. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  4417. /* Get operational params, sge flags, send init cmd to controller */
  4418. if (instance->instancet->init_adapter(instance))
  4419. goto fail_init_adapter;
  4420. instance->instancet->enable_intr(instance);
  4421. dev_info(&instance->pdev->dev, "INIT adapter done\n");
  4422. megasas_setup_jbod_map(instance);
  4423. /** for passthrough
  4424. * the following function will get the PD LIST.
  4425. */
  4426. memset(instance->pd_list, 0,
  4427. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  4428. if (megasas_get_pd_list(instance) < 0) {
  4429. dev_err(&instance->pdev->dev, "failed to get PD list\n");
  4430. goto fail_get_pd_list;
  4431. }
  4432. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  4433. if (megasas_ld_list_query(instance,
  4434. MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
  4435. megasas_get_ld_list(instance);
  4436. /*
  4437. * Compute the max allowed sectors per IO: The controller info has two
  4438. * limits on max sectors. Driver should use the minimum of these two.
  4439. *
  4440. * 1 << stripe_sz_ops.min = max sectors per strip
  4441. *
  4442. * Note that older firmwares ( < FW ver 30) didn't report information
  4443. * to calculate max_sectors_1. So the number ended up as zero always.
  4444. */
  4445. tmp_sectors = 0;
  4446. ctrl_info = instance->ctrl_info;
  4447. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  4448. le16_to_cpu(ctrl_info->max_strips_per_io);
  4449. max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
  4450. tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
  4451. instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
  4452. instance->passive = ctrl_info->cluster.passive;
  4453. memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
  4454. instance->UnevenSpanSupport =
  4455. ctrl_info->adapterOperations2.supportUnevenSpans;
  4456. if (instance->UnevenSpanSupport) {
  4457. struct fusion_context *fusion = instance->ctrl_context;
  4458. if (MR_ValidateMapInfo(instance))
  4459. fusion->fast_path_io = 1;
  4460. else
  4461. fusion->fast_path_io = 0;
  4462. }
  4463. if (ctrl_info->host_interface.SRIOV) {
  4464. instance->requestorId = ctrl_info->iov.requestorId;
  4465. if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
  4466. if (!ctrl_info->adapterOperations2.activePassive)
  4467. instance->PlasmaFW111 = 1;
  4468. dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
  4469. instance->PlasmaFW111 ? "1.11" : "new");
  4470. if (instance->PlasmaFW111) {
  4471. iovPtr = (struct IOV_111 *)
  4472. ((unsigned char *)ctrl_info + IOV_111_OFFSET);
  4473. instance->requestorId = iovPtr->requestorId;
  4474. }
  4475. }
  4476. dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
  4477. instance->requestorId);
  4478. }
  4479. instance->crash_dump_fw_support =
  4480. ctrl_info->adapterOperations3.supportCrashDump;
  4481. instance->crash_dump_drv_support =
  4482. (instance->crash_dump_fw_support &&
  4483. instance->crash_dump_buf);
  4484. if (instance->crash_dump_drv_support)
  4485. megasas_set_crash_dump_params(instance,
  4486. MR_CRASH_BUF_TURN_OFF);
  4487. else {
  4488. if (instance->crash_dump_buf)
  4489. pci_free_consistent(instance->pdev,
  4490. CRASH_DMA_BUF_SIZE,
  4491. instance->crash_dump_buf,
  4492. instance->crash_dump_h);
  4493. instance->crash_dump_buf = NULL;
  4494. }
  4495. dev_info(&instance->pdev->dev,
  4496. "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
  4497. le16_to_cpu(ctrl_info->pci.vendor_id),
  4498. le16_to_cpu(ctrl_info->pci.device_id),
  4499. le16_to_cpu(ctrl_info->pci.sub_vendor_id),
  4500. le16_to_cpu(ctrl_info->pci.sub_device_id));
  4501. dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
  4502. instance->UnevenSpanSupport ? "yes" : "no");
  4503. dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
  4504. instance->crash_dump_drv_support ? "yes" : "no");
  4505. dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
  4506. instance->use_seqnum_jbod_fp ? "yes" : "no");
  4507. instance->max_sectors_per_req = instance->max_num_sge *
  4508. SGE_BUFFER_SIZE / 512;
  4509. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  4510. instance->max_sectors_per_req = tmp_sectors;
  4511. /* Check for valid throttlequeuedepth module parameter */
  4512. if (throttlequeuedepth &&
  4513. throttlequeuedepth <= instance->max_scsi_cmds)
  4514. instance->throttlequeuedepth = throttlequeuedepth;
  4515. else
  4516. instance->throttlequeuedepth =
  4517. MEGASAS_THROTTLE_QUEUE_DEPTH;
  4518. if ((resetwaittime < 1) ||
  4519. (resetwaittime > MEGASAS_RESET_WAIT_TIME))
  4520. resetwaittime = MEGASAS_RESET_WAIT_TIME;
  4521. if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
  4522. scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  4523. /* Launch SR-IOV heartbeat timer */
  4524. if (instance->requestorId) {
  4525. if (!megasas_sriov_start_heartbeat(instance, 1))
  4526. megasas_start_timer(instance,
  4527. &instance->sriov_heartbeat_timer,
  4528. megasas_sriov_heartbeat_handler,
  4529. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  4530. else
  4531. instance->skip_heartbeat_timer_del = 1;
  4532. }
  4533. return 0;
  4534. fail_get_pd_list:
  4535. instance->instancet->disable_intr(instance);
  4536. fail_init_adapter:
  4537. megasas_destroy_irqs(instance);
  4538. fail_setup_irqs:
  4539. if (instance->msix_vectors)
  4540. pci_disable_msix(instance->pdev);
  4541. instance->msix_vectors = 0;
  4542. fail_ready_state:
  4543. kfree(instance->ctrl_info);
  4544. instance->ctrl_info = NULL;
  4545. iounmap(instance->reg_set);
  4546. fail_ioremap:
  4547. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  4548. return -EINVAL;
  4549. }
  4550. /**
  4551. * megasas_release_mfi - Reverses the FW initialization
  4552. * @instance: Adapter soft state
  4553. */
  4554. static void megasas_release_mfi(struct megasas_instance *instance)
  4555. {
  4556. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  4557. if (instance->reply_queue)
  4558. pci_free_consistent(instance->pdev, reply_q_sz,
  4559. instance->reply_queue, instance->reply_queue_h);
  4560. megasas_free_cmds(instance);
  4561. iounmap(instance->reg_set);
  4562. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  4563. }
  4564. /**
  4565. * megasas_get_seq_num - Gets latest event sequence numbers
  4566. * @instance: Adapter soft state
  4567. * @eli: FW event log sequence numbers information
  4568. *
  4569. * FW maintains a log of all events in a non-volatile area. Upper layers would
  4570. * usually find out the latest sequence number of the events, the seq number at
  4571. * the boot etc. They would "read" all the events below the latest seq number
  4572. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  4573. * number), they would subsribe to AEN (asynchronous event notification) and
  4574. * wait for the events to happen.
  4575. */
  4576. static int
  4577. megasas_get_seq_num(struct megasas_instance *instance,
  4578. struct megasas_evt_log_info *eli)
  4579. {
  4580. struct megasas_cmd *cmd;
  4581. struct megasas_dcmd_frame *dcmd;
  4582. struct megasas_evt_log_info *el_info;
  4583. dma_addr_t el_info_h = 0;
  4584. cmd = megasas_get_cmd(instance);
  4585. if (!cmd) {
  4586. return -ENOMEM;
  4587. }
  4588. dcmd = &cmd->frame->dcmd;
  4589. el_info = pci_alloc_consistent(instance->pdev,
  4590. sizeof(struct megasas_evt_log_info),
  4591. &el_info_h);
  4592. if (!el_info) {
  4593. megasas_return_cmd(instance, cmd);
  4594. return -ENOMEM;
  4595. }
  4596. memset(el_info, 0, sizeof(*el_info));
  4597. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4598. dcmd->cmd = MFI_CMD_DCMD;
  4599. dcmd->cmd_status = 0x0;
  4600. dcmd->sge_count = 1;
  4601. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  4602. dcmd->timeout = 0;
  4603. dcmd->pad_0 = 0;
  4604. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
  4605. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
  4606. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
  4607. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
  4608. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
  4609. DCMD_SUCCESS) {
  4610. /*
  4611. * Copy the data back into callers buffer
  4612. */
  4613. eli->newest_seq_num = el_info->newest_seq_num;
  4614. eli->oldest_seq_num = el_info->oldest_seq_num;
  4615. eli->clear_seq_num = el_info->clear_seq_num;
  4616. eli->shutdown_seq_num = el_info->shutdown_seq_num;
  4617. eli->boot_seq_num = el_info->boot_seq_num;
  4618. } else
  4619. dev_err(&instance->pdev->dev, "DCMD failed "
  4620. "from %s\n", __func__);
  4621. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  4622. el_info, el_info_h);
  4623. megasas_return_cmd(instance, cmd);
  4624. return 0;
  4625. }
  4626. /**
  4627. * megasas_register_aen - Registers for asynchronous event notification
  4628. * @instance: Adapter soft state
  4629. * @seq_num: The starting sequence number
  4630. * @class_locale: Class of the event
  4631. *
  4632. * This function subscribes for AEN for events beyond the @seq_num. It requests
  4633. * to be notified if and only if the event is of type @class_locale
  4634. */
  4635. static int
  4636. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  4637. u32 class_locale_word)
  4638. {
  4639. int ret_val;
  4640. struct megasas_cmd *cmd;
  4641. struct megasas_dcmd_frame *dcmd;
  4642. union megasas_evt_class_locale curr_aen;
  4643. union megasas_evt_class_locale prev_aen;
  4644. /*
  4645. * If there an AEN pending already (aen_cmd), check if the
  4646. * class_locale of that pending AEN is inclusive of the new
  4647. * AEN request we currently have. If it is, then we don't have
  4648. * to do anything. In other words, whichever events the current
  4649. * AEN request is subscribing to, have already been subscribed
  4650. * to.
  4651. *
  4652. * If the old_cmd is _not_ inclusive, then we have to abort
  4653. * that command, form a class_locale that is superset of both
  4654. * old and current and re-issue to the FW
  4655. */
  4656. curr_aen.word = class_locale_word;
  4657. if (instance->aen_cmd) {
  4658. prev_aen.word =
  4659. le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
  4660. if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
  4661. (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
  4662. dev_info(&instance->pdev->dev,
  4663. "%s %d out of range class %d send by application\n",
  4664. __func__, __LINE__, curr_aen.members.class);
  4665. return 0;
  4666. }
  4667. /*
  4668. * A class whose enum value is smaller is inclusive of all
  4669. * higher values. If a PROGRESS (= -1) was previously
  4670. * registered, then a new registration requests for higher
  4671. * classes need not be sent to FW. They are automatically
  4672. * included.
  4673. *
  4674. * Locale numbers don't have such hierarchy. They are bitmap
  4675. * values
  4676. */
  4677. if ((prev_aen.members.class <= curr_aen.members.class) &&
  4678. !((prev_aen.members.locale & curr_aen.members.locale) ^
  4679. curr_aen.members.locale)) {
  4680. /*
  4681. * Previously issued event registration includes
  4682. * current request. Nothing to do.
  4683. */
  4684. return 0;
  4685. } else {
  4686. curr_aen.members.locale |= prev_aen.members.locale;
  4687. if (prev_aen.members.class < curr_aen.members.class)
  4688. curr_aen.members.class = prev_aen.members.class;
  4689. instance->aen_cmd->abort_aen = 1;
  4690. ret_val = megasas_issue_blocked_abort_cmd(instance,
  4691. instance->
  4692. aen_cmd, 30);
  4693. if (ret_val) {
  4694. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
  4695. "previous AEN command\n");
  4696. return ret_val;
  4697. }
  4698. }
  4699. }
  4700. cmd = megasas_get_cmd(instance);
  4701. if (!cmd)
  4702. return -ENOMEM;
  4703. dcmd = &cmd->frame->dcmd;
  4704. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  4705. /*
  4706. * Prepare DCMD for aen registration
  4707. */
  4708. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  4709. dcmd->cmd = MFI_CMD_DCMD;
  4710. dcmd->cmd_status = 0x0;
  4711. dcmd->sge_count = 1;
  4712. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  4713. dcmd->timeout = 0;
  4714. dcmd->pad_0 = 0;
  4715. dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
  4716. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
  4717. dcmd->mbox.w[0] = cpu_to_le32(seq_num);
  4718. instance->last_seq_num = seq_num;
  4719. dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
  4720. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
  4721. dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
  4722. if (instance->aen_cmd != NULL) {
  4723. megasas_return_cmd(instance, cmd);
  4724. return 0;
  4725. }
  4726. /*
  4727. * Store reference to the cmd used to register for AEN. When an
  4728. * application wants us to register for AEN, we have to abort this
  4729. * cmd and re-register with a new EVENT LOCALE supplied by that app
  4730. */
  4731. instance->aen_cmd = cmd;
  4732. /*
  4733. * Issue the aen registration frame
  4734. */
  4735. instance->instancet->issue_dcmd(instance, cmd);
  4736. return 0;
  4737. }
  4738. /**
  4739. * megasas_start_aen - Subscribes to AEN during driver load time
  4740. * @instance: Adapter soft state
  4741. */
  4742. static int megasas_start_aen(struct megasas_instance *instance)
  4743. {
  4744. struct megasas_evt_log_info eli;
  4745. union megasas_evt_class_locale class_locale;
  4746. /*
  4747. * Get the latest sequence number from FW
  4748. */
  4749. memset(&eli, 0, sizeof(eli));
  4750. if (megasas_get_seq_num(instance, &eli))
  4751. return -1;
  4752. /*
  4753. * Register AEN with FW for latest sequence number plus 1
  4754. */
  4755. class_locale.members.reserved = 0;
  4756. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4757. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4758. return megasas_register_aen(instance,
  4759. le32_to_cpu(eli.newest_seq_num) + 1,
  4760. class_locale.word);
  4761. }
  4762. /**
  4763. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  4764. * @instance: Adapter soft state
  4765. */
  4766. static int megasas_io_attach(struct megasas_instance *instance)
  4767. {
  4768. struct Scsi_Host *host = instance->host;
  4769. /*
  4770. * Export parameters required by SCSI mid-layer
  4771. */
  4772. host->irq = instance->pdev->irq;
  4773. host->unique_id = instance->unique_id;
  4774. host->can_queue = instance->max_scsi_cmds;
  4775. host->this_id = instance->init_id;
  4776. host->sg_tablesize = instance->max_num_sge;
  4777. if (instance->fw_support_ieee)
  4778. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  4779. /*
  4780. * Check if the module parameter value for max_sectors can be used
  4781. */
  4782. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  4783. instance->max_sectors_per_req = max_sectors;
  4784. else {
  4785. if (max_sectors) {
  4786. if (((instance->pdev->device ==
  4787. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  4788. (instance->pdev->device ==
  4789. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  4790. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  4791. instance->max_sectors_per_req = max_sectors;
  4792. } else {
  4793. dev_info(&instance->pdev->dev, "max_sectors should be > 0"
  4794. "and <= %d (or < 1MB for GEN2 controller)\n",
  4795. instance->max_sectors_per_req);
  4796. }
  4797. }
  4798. }
  4799. host->max_sectors = instance->max_sectors_per_req;
  4800. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  4801. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  4802. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  4803. host->max_lun = MEGASAS_MAX_LUN;
  4804. host->max_cmd_len = 16;
  4805. /*
  4806. * Notify the mid-layer about the new controller
  4807. */
  4808. if (scsi_add_host(host, &instance->pdev->dev)) {
  4809. dev_err(&instance->pdev->dev,
  4810. "Failed to add host from %s %d\n",
  4811. __func__, __LINE__);
  4812. return -ENODEV;
  4813. }
  4814. return 0;
  4815. }
  4816. static int
  4817. megasas_set_dma_mask(struct pci_dev *pdev)
  4818. {
  4819. /*
  4820. * All our controllers are capable of performing 64-bit DMA
  4821. */
  4822. if (IS_DMA64) {
  4823. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  4824. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  4825. goto fail_set_dma_mask;
  4826. }
  4827. } else {
  4828. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  4829. goto fail_set_dma_mask;
  4830. }
  4831. /*
  4832. * Ensure that all data structures are allocated in 32-bit
  4833. * memory.
  4834. */
  4835. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
  4836. /* Try 32bit DMA mask and 32 bit Consistent dma mask */
  4837. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  4838. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  4839. dev_info(&pdev->dev, "set 32bit DMA mask"
  4840. "and 32 bit consistent mask\n");
  4841. else
  4842. goto fail_set_dma_mask;
  4843. }
  4844. return 0;
  4845. fail_set_dma_mask:
  4846. return 1;
  4847. }
  4848. /**
  4849. * megasas_probe_one - PCI hotplug entry point
  4850. * @pdev: PCI device structure
  4851. * @id: PCI ids of supported hotplugged adapter
  4852. */
  4853. static int megasas_probe_one(struct pci_dev *pdev,
  4854. const struct pci_device_id *id)
  4855. {
  4856. int rval, pos;
  4857. struct Scsi_Host *host;
  4858. struct megasas_instance *instance;
  4859. u16 control = 0;
  4860. struct fusion_context *fusion = NULL;
  4861. /* Reset MSI-X in the kdump kernel */
  4862. if (reset_devices) {
  4863. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  4864. if (pos) {
  4865. pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
  4866. &control);
  4867. if (control & PCI_MSIX_FLAGS_ENABLE) {
  4868. dev_info(&pdev->dev, "resetting MSI-X\n");
  4869. pci_write_config_word(pdev,
  4870. pos + PCI_MSIX_FLAGS,
  4871. control &
  4872. ~PCI_MSIX_FLAGS_ENABLE);
  4873. }
  4874. }
  4875. }
  4876. /*
  4877. * PCI prepping: enable device set bus mastering and dma mask
  4878. */
  4879. rval = pci_enable_device_mem(pdev);
  4880. if (rval) {
  4881. return rval;
  4882. }
  4883. pci_set_master(pdev);
  4884. if (megasas_set_dma_mask(pdev))
  4885. goto fail_set_dma_mask;
  4886. host = scsi_host_alloc(&megasas_template,
  4887. sizeof(struct megasas_instance));
  4888. if (!host) {
  4889. dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
  4890. goto fail_alloc_instance;
  4891. }
  4892. instance = (struct megasas_instance *)host->hostdata;
  4893. memset(instance, 0, sizeof(*instance));
  4894. atomic_set(&instance->fw_reset_no_pci_access, 0);
  4895. instance->pdev = pdev;
  4896. switch (instance->pdev->device) {
  4897. case PCI_DEVICE_ID_LSI_FUSION:
  4898. case PCI_DEVICE_ID_LSI_PLASMA:
  4899. case PCI_DEVICE_ID_LSI_INVADER:
  4900. case PCI_DEVICE_ID_LSI_FURY:
  4901. case PCI_DEVICE_ID_LSI_INTRUDER:
  4902. case PCI_DEVICE_ID_LSI_INTRUDER_24:
  4903. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  4904. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  4905. {
  4906. instance->ctrl_context_pages =
  4907. get_order(sizeof(struct fusion_context));
  4908. instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
  4909. instance->ctrl_context_pages);
  4910. if (!instance->ctrl_context) {
  4911. dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
  4912. "memory for Fusion context info\n");
  4913. goto fail_alloc_dma_buf;
  4914. }
  4915. fusion = instance->ctrl_context;
  4916. memset(fusion, 0,
  4917. ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
  4918. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
  4919. (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
  4920. fusion->adapter_type = THUNDERBOLT_SERIES;
  4921. else
  4922. fusion->adapter_type = INVADER_SERIES;
  4923. }
  4924. break;
  4925. default: /* For all other supported controllers */
  4926. instance->producer =
  4927. pci_alloc_consistent(pdev, sizeof(u32),
  4928. &instance->producer_h);
  4929. instance->consumer =
  4930. pci_alloc_consistent(pdev, sizeof(u32),
  4931. &instance->consumer_h);
  4932. if (!instance->producer || !instance->consumer) {
  4933. dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
  4934. "memory for producer, consumer\n");
  4935. goto fail_alloc_dma_buf;
  4936. }
  4937. *instance->producer = 0;
  4938. *instance->consumer = 0;
  4939. break;
  4940. }
  4941. /* Crash dump feature related initialisation*/
  4942. instance->drv_buf_index = 0;
  4943. instance->drv_buf_alloc = 0;
  4944. instance->crash_dump_fw_support = 0;
  4945. instance->crash_dump_app_support = 0;
  4946. instance->fw_crash_state = UNAVAILABLE;
  4947. spin_lock_init(&instance->crashdump_lock);
  4948. instance->crash_dump_buf = NULL;
  4949. megasas_poll_wait_aen = 0;
  4950. instance->flag_ieee = 0;
  4951. instance->ev = NULL;
  4952. instance->issuepend_done = 1;
  4953. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  4954. instance->is_imr = 0;
  4955. instance->evt_detail = pci_alloc_consistent(pdev,
  4956. sizeof(struct
  4957. megasas_evt_detail),
  4958. &instance->evt_detail_h);
  4959. if (!instance->evt_detail) {
  4960. dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
  4961. "event detail structure\n");
  4962. goto fail_alloc_dma_buf;
  4963. }
  4964. if (!reset_devices) {
  4965. instance->system_info_buf = pci_zalloc_consistent(pdev,
  4966. sizeof(struct MR_DRV_SYSTEM_INFO),
  4967. &instance->system_info_h);
  4968. if (!instance->system_info_buf)
  4969. dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
  4970. instance->pd_info = pci_alloc_consistent(pdev,
  4971. sizeof(struct MR_PD_INFO), &instance->pd_info_h);
  4972. if (!instance->pd_info)
  4973. dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
  4974. instance->crash_dump_buf = pci_alloc_consistent(pdev,
  4975. CRASH_DMA_BUF_SIZE,
  4976. &instance->crash_dump_h);
  4977. if (!instance->crash_dump_buf)
  4978. dev_err(&pdev->dev, "Can't allocate Firmware "
  4979. "crash dump DMA buffer\n");
  4980. }
  4981. /*
  4982. * Initialize locks and queues
  4983. */
  4984. INIT_LIST_HEAD(&instance->cmd_pool);
  4985. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  4986. atomic_set(&instance->fw_outstanding,0);
  4987. init_waitqueue_head(&instance->int_cmd_wait_q);
  4988. init_waitqueue_head(&instance->abort_cmd_wait_q);
  4989. spin_lock_init(&instance->mfi_pool_lock);
  4990. spin_lock_init(&instance->hba_lock);
  4991. spin_lock_init(&instance->completion_lock);
  4992. mutex_init(&instance->reset_mutex);
  4993. mutex_init(&instance->hba_mutex);
  4994. /*
  4995. * Initialize PCI related and misc parameters
  4996. */
  4997. instance->host = host;
  4998. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  4999. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  5000. instance->ctrl_info = NULL;
  5001. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  5002. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
  5003. instance->flag_ieee = 1;
  5004. megasas_dbg_lvl = 0;
  5005. instance->flag = 0;
  5006. instance->unload = 1;
  5007. instance->last_time = 0;
  5008. instance->disableOnlineCtrlReset = 1;
  5009. instance->UnevenSpanSupport = 0;
  5010. if (instance->ctrl_context) {
  5011. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  5012. INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
  5013. } else
  5014. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  5015. /*
  5016. * Initialize MFI Firmware
  5017. */
  5018. if (megasas_init_fw(instance))
  5019. goto fail_init_mfi;
  5020. if (instance->requestorId) {
  5021. if (instance->PlasmaFW111) {
  5022. instance->vf_affiliation_111 =
  5023. pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
  5024. &instance->vf_affiliation_111_h);
  5025. if (!instance->vf_affiliation_111)
  5026. dev_warn(&pdev->dev, "Can't allocate "
  5027. "memory for VF affiliation buffer\n");
  5028. } else {
  5029. instance->vf_affiliation =
  5030. pci_alloc_consistent(pdev,
  5031. (MAX_LOGICAL_DRIVES + 1) *
  5032. sizeof(struct MR_LD_VF_AFFILIATION),
  5033. &instance->vf_affiliation_h);
  5034. if (!instance->vf_affiliation)
  5035. dev_warn(&pdev->dev, "Can't allocate "
  5036. "memory for VF affiliation buffer\n");
  5037. }
  5038. }
  5039. /*
  5040. * Store instance in PCI softstate
  5041. */
  5042. pci_set_drvdata(pdev, instance);
  5043. /*
  5044. * Add this controller to megasas_mgmt_info structure so that it
  5045. * can be exported to management applications
  5046. */
  5047. megasas_mgmt_info.count++;
  5048. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  5049. megasas_mgmt_info.max_index++;
  5050. /*
  5051. * Register with SCSI mid-layer
  5052. */
  5053. if (megasas_io_attach(instance))
  5054. goto fail_io_attach;
  5055. instance->unload = 0;
  5056. /*
  5057. * Trigger SCSI to scan our drives
  5058. */
  5059. scsi_scan_host(host);
  5060. /*
  5061. * Initiate AEN (Asynchronous Event Notification)
  5062. */
  5063. if (megasas_start_aen(instance)) {
  5064. dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
  5065. goto fail_start_aen;
  5066. }
  5067. /* Get current SR-IOV LD/VF affiliation */
  5068. if (instance->requestorId)
  5069. megasas_get_ld_vf_affiliation(instance, 1);
  5070. return 0;
  5071. fail_start_aen:
  5072. fail_io_attach:
  5073. megasas_mgmt_info.count--;
  5074. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  5075. megasas_mgmt_info.max_index--;
  5076. instance->instancet->disable_intr(instance);
  5077. megasas_destroy_irqs(instance);
  5078. if (instance->ctrl_context)
  5079. megasas_release_fusion(instance);
  5080. else
  5081. megasas_release_mfi(instance);
  5082. if (instance->msix_vectors)
  5083. pci_disable_msix(instance->pdev);
  5084. fail_init_mfi:
  5085. fail_alloc_dma_buf:
  5086. if (instance->evt_detail)
  5087. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  5088. instance->evt_detail,
  5089. instance->evt_detail_h);
  5090. if (instance->pd_info)
  5091. pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
  5092. instance->pd_info,
  5093. instance->pd_info_h);
  5094. if (instance->producer)
  5095. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  5096. instance->producer_h);
  5097. if (instance->consumer)
  5098. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  5099. instance->consumer_h);
  5100. scsi_host_put(host);
  5101. fail_alloc_instance:
  5102. fail_set_dma_mask:
  5103. pci_disable_device(pdev);
  5104. return -ENODEV;
  5105. }
  5106. /**
  5107. * megasas_flush_cache - Requests FW to flush all its caches
  5108. * @instance: Adapter soft state
  5109. */
  5110. static void megasas_flush_cache(struct megasas_instance *instance)
  5111. {
  5112. struct megasas_cmd *cmd;
  5113. struct megasas_dcmd_frame *dcmd;
  5114. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  5115. return;
  5116. cmd = megasas_get_cmd(instance);
  5117. if (!cmd)
  5118. return;
  5119. dcmd = &cmd->frame->dcmd;
  5120. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  5121. dcmd->cmd = MFI_CMD_DCMD;
  5122. dcmd->cmd_status = 0x0;
  5123. dcmd->sge_count = 0;
  5124. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  5125. dcmd->timeout = 0;
  5126. dcmd->pad_0 = 0;
  5127. dcmd->data_xfer_len = 0;
  5128. dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
  5129. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  5130. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
  5131. != DCMD_SUCCESS) {
  5132. dev_err(&instance->pdev->dev,
  5133. "return from %s %d\n", __func__, __LINE__);
  5134. return;
  5135. }
  5136. megasas_return_cmd(instance, cmd);
  5137. }
  5138. /**
  5139. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  5140. * @instance: Adapter soft state
  5141. * @opcode: Shutdown/Hibernate
  5142. */
  5143. static void megasas_shutdown_controller(struct megasas_instance *instance,
  5144. u32 opcode)
  5145. {
  5146. struct megasas_cmd *cmd;
  5147. struct megasas_dcmd_frame *dcmd;
  5148. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  5149. return;
  5150. cmd = megasas_get_cmd(instance);
  5151. if (!cmd)
  5152. return;
  5153. if (instance->aen_cmd)
  5154. megasas_issue_blocked_abort_cmd(instance,
  5155. instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
  5156. if (instance->map_update_cmd)
  5157. megasas_issue_blocked_abort_cmd(instance,
  5158. instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
  5159. if (instance->jbod_seq_cmd)
  5160. megasas_issue_blocked_abort_cmd(instance,
  5161. instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
  5162. dcmd = &cmd->frame->dcmd;
  5163. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  5164. dcmd->cmd = MFI_CMD_DCMD;
  5165. dcmd->cmd_status = 0x0;
  5166. dcmd->sge_count = 0;
  5167. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
  5168. dcmd->timeout = 0;
  5169. dcmd->pad_0 = 0;
  5170. dcmd->data_xfer_len = 0;
  5171. dcmd->opcode = cpu_to_le32(opcode);
  5172. if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
  5173. != DCMD_SUCCESS) {
  5174. dev_err(&instance->pdev->dev,
  5175. "return from %s %d\n", __func__, __LINE__);
  5176. return;
  5177. }
  5178. megasas_return_cmd(instance, cmd);
  5179. }
  5180. #ifdef CONFIG_PM
  5181. /**
  5182. * megasas_suspend - driver suspend entry point
  5183. * @pdev: PCI device structure
  5184. * @state: PCI power state to suspend routine
  5185. */
  5186. static int
  5187. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  5188. {
  5189. struct Scsi_Host *host;
  5190. struct megasas_instance *instance;
  5191. instance = pci_get_drvdata(pdev);
  5192. host = instance->host;
  5193. instance->unload = 1;
  5194. /* Shutdown SR-IOV heartbeat timer */
  5195. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  5196. del_timer_sync(&instance->sriov_heartbeat_timer);
  5197. megasas_flush_cache(instance);
  5198. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  5199. /* cancel the delayed work if this work still in queue */
  5200. if (instance->ev != NULL) {
  5201. struct megasas_aen_event *ev = instance->ev;
  5202. cancel_delayed_work_sync(&ev->hotplug_work);
  5203. instance->ev = NULL;
  5204. }
  5205. tasklet_kill(&instance->isr_tasklet);
  5206. pci_set_drvdata(instance->pdev, instance);
  5207. instance->instancet->disable_intr(instance);
  5208. megasas_destroy_irqs(instance);
  5209. if (instance->msix_vectors)
  5210. pci_disable_msix(instance->pdev);
  5211. pci_save_state(pdev);
  5212. pci_disable_device(pdev);
  5213. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  5214. return 0;
  5215. }
  5216. /**
  5217. * megasas_resume- driver resume entry point
  5218. * @pdev: PCI device structure
  5219. */
  5220. static int
  5221. megasas_resume(struct pci_dev *pdev)
  5222. {
  5223. int rval;
  5224. struct Scsi_Host *host;
  5225. struct megasas_instance *instance;
  5226. instance = pci_get_drvdata(pdev);
  5227. host = instance->host;
  5228. pci_set_power_state(pdev, PCI_D0);
  5229. pci_enable_wake(pdev, PCI_D0, 0);
  5230. pci_restore_state(pdev);
  5231. /*
  5232. * PCI prepping: enable device set bus mastering and dma mask
  5233. */
  5234. rval = pci_enable_device_mem(pdev);
  5235. if (rval) {
  5236. dev_err(&pdev->dev, "Enable device failed\n");
  5237. return rval;
  5238. }
  5239. pci_set_master(pdev);
  5240. if (megasas_set_dma_mask(pdev))
  5241. goto fail_set_dma_mask;
  5242. /*
  5243. * Initialize MFI Firmware
  5244. */
  5245. atomic_set(&instance->fw_outstanding, 0);
  5246. /*
  5247. * We expect the FW state to be READY
  5248. */
  5249. if (megasas_transition_to_ready(instance, 0))
  5250. goto fail_ready_state;
  5251. /* Now re-enable MSI-X */
  5252. if (instance->msix_vectors &&
  5253. pci_enable_msix_exact(instance->pdev, instance->msixentry,
  5254. instance->msix_vectors))
  5255. goto fail_reenable_msix;
  5256. if (instance->ctrl_context) {
  5257. megasas_reset_reply_desc(instance);
  5258. if (megasas_ioc_init_fusion(instance)) {
  5259. megasas_free_cmds(instance);
  5260. megasas_free_cmds_fusion(instance);
  5261. goto fail_init_mfi;
  5262. }
  5263. if (!megasas_get_map_info(instance))
  5264. megasas_sync_map_info(instance);
  5265. } else {
  5266. *instance->producer = 0;
  5267. *instance->consumer = 0;
  5268. if (megasas_issue_init_mfi(instance))
  5269. goto fail_init_mfi;
  5270. }
  5271. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  5272. (unsigned long)instance);
  5273. if (instance->msix_vectors ?
  5274. megasas_setup_irqs_msix(instance, 0) :
  5275. megasas_setup_irqs_ioapic(instance))
  5276. goto fail_init_mfi;
  5277. /* Re-launch SR-IOV heartbeat timer */
  5278. if (instance->requestorId) {
  5279. if (!megasas_sriov_start_heartbeat(instance, 0))
  5280. megasas_start_timer(instance,
  5281. &instance->sriov_heartbeat_timer,
  5282. megasas_sriov_heartbeat_handler,
  5283. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  5284. else {
  5285. instance->skip_heartbeat_timer_del = 1;
  5286. goto fail_init_mfi;
  5287. }
  5288. }
  5289. instance->instancet->enable_intr(instance);
  5290. megasas_setup_jbod_map(instance);
  5291. instance->unload = 0;
  5292. /*
  5293. * Initiate AEN (Asynchronous Event Notification)
  5294. */
  5295. if (megasas_start_aen(instance))
  5296. dev_err(&instance->pdev->dev, "Start AEN failed\n");
  5297. return 0;
  5298. fail_init_mfi:
  5299. if (instance->evt_detail)
  5300. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  5301. instance->evt_detail,
  5302. instance->evt_detail_h);
  5303. if (instance->pd_info)
  5304. pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
  5305. instance->pd_info,
  5306. instance->pd_info_h);
  5307. if (instance->producer)
  5308. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  5309. instance->producer_h);
  5310. if (instance->consumer)
  5311. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  5312. instance->consumer_h);
  5313. scsi_host_put(host);
  5314. fail_set_dma_mask:
  5315. fail_ready_state:
  5316. fail_reenable_msix:
  5317. pci_disable_device(pdev);
  5318. return -ENODEV;
  5319. }
  5320. #else
  5321. #define megasas_suspend NULL
  5322. #define megasas_resume NULL
  5323. #endif
  5324. /**
  5325. * megasas_detach_one - PCI hot"un"plug entry point
  5326. * @pdev: PCI device structure
  5327. */
  5328. static void megasas_detach_one(struct pci_dev *pdev)
  5329. {
  5330. int i;
  5331. struct Scsi_Host *host;
  5332. struct megasas_instance *instance;
  5333. struct fusion_context *fusion;
  5334. u32 pd_seq_map_sz;
  5335. instance = pci_get_drvdata(pdev);
  5336. instance->unload = 1;
  5337. host = instance->host;
  5338. fusion = instance->ctrl_context;
  5339. /* Shutdown SR-IOV heartbeat timer */
  5340. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  5341. del_timer_sync(&instance->sriov_heartbeat_timer);
  5342. if (instance->fw_crash_state != UNAVAILABLE)
  5343. megasas_free_host_crash_buffer(instance);
  5344. scsi_remove_host(instance->host);
  5345. megasas_flush_cache(instance);
  5346. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  5347. /* cancel the delayed work if this work still in queue*/
  5348. if (instance->ev != NULL) {
  5349. struct megasas_aen_event *ev = instance->ev;
  5350. cancel_delayed_work_sync(&ev->hotplug_work);
  5351. instance->ev = NULL;
  5352. }
  5353. /* cancel all wait events */
  5354. wake_up_all(&instance->int_cmd_wait_q);
  5355. tasklet_kill(&instance->isr_tasklet);
  5356. /*
  5357. * Take the instance off the instance array. Note that we will not
  5358. * decrement the max_index. We let this array be sparse array
  5359. */
  5360. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  5361. if (megasas_mgmt_info.instance[i] == instance) {
  5362. megasas_mgmt_info.count--;
  5363. megasas_mgmt_info.instance[i] = NULL;
  5364. break;
  5365. }
  5366. }
  5367. instance->instancet->disable_intr(instance);
  5368. megasas_destroy_irqs(instance);
  5369. if (instance->msix_vectors)
  5370. pci_disable_msix(instance->pdev);
  5371. if (instance->ctrl_context) {
  5372. megasas_release_fusion(instance);
  5373. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  5374. (sizeof(struct MR_PD_CFG_SEQ) *
  5375. (MAX_PHYSICAL_DEVICES - 1));
  5376. for (i = 0; i < 2 ; i++) {
  5377. if (fusion->ld_map[i])
  5378. dma_free_coherent(&instance->pdev->dev,
  5379. fusion->max_map_sz,
  5380. fusion->ld_map[i],
  5381. fusion->ld_map_phys[i]);
  5382. if (fusion->ld_drv_map[i])
  5383. free_pages((ulong)fusion->ld_drv_map[i],
  5384. fusion->drv_map_pages);
  5385. if (fusion->pd_seq_sync[i])
  5386. dma_free_coherent(&instance->pdev->dev,
  5387. pd_seq_map_sz,
  5388. fusion->pd_seq_sync[i],
  5389. fusion->pd_seq_phys[i]);
  5390. }
  5391. free_pages((ulong)instance->ctrl_context,
  5392. instance->ctrl_context_pages);
  5393. } else {
  5394. megasas_release_mfi(instance);
  5395. pci_free_consistent(pdev, sizeof(u32),
  5396. instance->producer,
  5397. instance->producer_h);
  5398. pci_free_consistent(pdev, sizeof(u32),
  5399. instance->consumer,
  5400. instance->consumer_h);
  5401. }
  5402. kfree(instance->ctrl_info);
  5403. if (instance->evt_detail)
  5404. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  5405. instance->evt_detail, instance->evt_detail_h);
  5406. if (instance->pd_info)
  5407. pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
  5408. instance->pd_info,
  5409. instance->pd_info_h);
  5410. if (instance->vf_affiliation)
  5411. pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
  5412. sizeof(struct MR_LD_VF_AFFILIATION),
  5413. instance->vf_affiliation,
  5414. instance->vf_affiliation_h);
  5415. if (instance->vf_affiliation_111)
  5416. pci_free_consistent(pdev,
  5417. sizeof(struct MR_LD_VF_AFFILIATION_111),
  5418. instance->vf_affiliation_111,
  5419. instance->vf_affiliation_111_h);
  5420. if (instance->hb_host_mem)
  5421. pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
  5422. instance->hb_host_mem,
  5423. instance->hb_host_mem_h);
  5424. if (instance->crash_dump_buf)
  5425. pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
  5426. instance->crash_dump_buf, instance->crash_dump_h);
  5427. if (instance->system_info_buf)
  5428. pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
  5429. instance->system_info_buf, instance->system_info_h);
  5430. scsi_host_put(host);
  5431. pci_disable_device(pdev);
  5432. }
  5433. /**
  5434. * megasas_shutdown - Shutdown entry point
  5435. * @device: Generic device structure
  5436. */
  5437. static void megasas_shutdown(struct pci_dev *pdev)
  5438. {
  5439. struct megasas_instance *instance = pci_get_drvdata(pdev);
  5440. instance->unload = 1;
  5441. megasas_flush_cache(instance);
  5442. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  5443. instance->instancet->disable_intr(instance);
  5444. megasas_destroy_irqs(instance);
  5445. if (instance->msix_vectors)
  5446. pci_disable_msix(instance->pdev);
  5447. }
  5448. /**
  5449. * megasas_mgmt_open - char node "open" entry point
  5450. */
  5451. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  5452. {
  5453. /*
  5454. * Allow only those users with admin rights
  5455. */
  5456. if (!capable(CAP_SYS_ADMIN))
  5457. return -EACCES;
  5458. return 0;
  5459. }
  5460. /**
  5461. * megasas_mgmt_fasync - Async notifier registration from applications
  5462. *
  5463. * This function adds the calling process to a driver global queue. When an
  5464. * event occurs, SIGIO will be sent to all processes in this queue.
  5465. */
  5466. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  5467. {
  5468. int rc;
  5469. mutex_lock(&megasas_async_queue_mutex);
  5470. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  5471. mutex_unlock(&megasas_async_queue_mutex);
  5472. if (rc >= 0) {
  5473. /* For sanity check when we get ioctl */
  5474. filep->private_data = filep;
  5475. return 0;
  5476. }
  5477. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  5478. return rc;
  5479. }
  5480. /**
  5481. * megasas_mgmt_poll - char node "poll" entry point
  5482. * */
  5483. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  5484. {
  5485. unsigned int mask;
  5486. unsigned long flags;
  5487. poll_wait(file, &megasas_poll_wait, wait);
  5488. spin_lock_irqsave(&poll_aen_lock, flags);
  5489. if (megasas_poll_wait_aen)
  5490. mask = (POLLIN | POLLRDNORM);
  5491. else
  5492. mask = 0;
  5493. megasas_poll_wait_aen = 0;
  5494. spin_unlock_irqrestore(&poll_aen_lock, flags);
  5495. return mask;
  5496. }
  5497. /*
  5498. * megasas_set_crash_dump_params_ioctl:
  5499. * Send CRASH_DUMP_MODE DCMD to all controllers
  5500. * @cmd: MFI command frame
  5501. */
  5502. static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
  5503. {
  5504. struct megasas_instance *local_instance;
  5505. int i, error = 0;
  5506. int crash_support;
  5507. crash_support = cmd->frame->dcmd.mbox.w[0];
  5508. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  5509. local_instance = megasas_mgmt_info.instance[i];
  5510. if (local_instance && local_instance->crash_dump_drv_support) {
  5511. if ((atomic_read(&local_instance->adprecovery) ==
  5512. MEGASAS_HBA_OPERATIONAL) &&
  5513. !megasas_set_crash_dump_params(local_instance,
  5514. crash_support)) {
  5515. local_instance->crash_dump_app_support =
  5516. crash_support;
  5517. dev_info(&local_instance->pdev->dev,
  5518. "Application firmware crash "
  5519. "dump mode set success\n");
  5520. error = 0;
  5521. } else {
  5522. dev_info(&local_instance->pdev->dev,
  5523. "Application firmware crash "
  5524. "dump mode set failed\n");
  5525. error = -1;
  5526. }
  5527. }
  5528. }
  5529. return error;
  5530. }
  5531. /**
  5532. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  5533. * @instance: Adapter soft state
  5534. * @argp: User's ioctl packet
  5535. */
  5536. static int
  5537. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  5538. struct megasas_iocpacket __user * user_ioc,
  5539. struct megasas_iocpacket *ioc)
  5540. {
  5541. struct megasas_sge32 *kern_sge32;
  5542. struct megasas_cmd *cmd;
  5543. void *kbuff_arr[MAX_IOCTL_SGE];
  5544. dma_addr_t buf_handle = 0;
  5545. int error = 0, i;
  5546. void *sense = NULL;
  5547. dma_addr_t sense_handle;
  5548. unsigned long *sense_ptr;
  5549. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  5550. if (ioc->sge_count > MAX_IOCTL_SGE) {
  5551. dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
  5552. ioc->sge_count, MAX_IOCTL_SGE);
  5553. return -EINVAL;
  5554. }
  5555. cmd = megasas_get_cmd(instance);
  5556. if (!cmd) {
  5557. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
  5558. return -ENOMEM;
  5559. }
  5560. /*
  5561. * User's IOCTL packet has 2 frames (maximum). Copy those two
  5562. * frames into our cmd's frames. cmd->frame's context will get
  5563. * overwritten when we copy from user's frames. So set that value
  5564. * alone separately
  5565. */
  5566. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  5567. cmd->frame->hdr.context = cpu_to_le32(cmd->index);
  5568. cmd->frame->hdr.pad_0 = 0;
  5569. cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
  5570. MFI_FRAME_SGL64 |
  5571. MFI_FRAME_SENSE64));
  5572. if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
  5573. error = megasas_set_crash_dump_params_ioctl(cmd);
  5574. megasas_return_cmd(instance, cmd);
  5575. return error;
  5576. }
  5577. /*
  5578. * The management interface between applications and the fw uses
  5579. * MFI frames. E.g, RAID configuration changes, LD property changes
  5580. * etc are accomplishes through different kinds of MFI frames. The
  5581. * driver needs to care only about substituting user buffers with
  5582. * kernel buffers in SGLs. The location of SGL is embedded in the
  5583. * struct iocpacket itself.
  5584. */
  5585. kern_sge32 = (struct megasas_sge32 *)
  5586. ((unsigned long)cmd->frame + ioc->sgl_off);
  5587. /*
  5588. * For each user buffer, create a mirror buffer and copy in
  5589. */
  5590. for (i = 0; i < ioc->sge_count; i++) {
  5591. if (!ioc->sgl[i].iov_len)
  5592. continue;
  5593. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  5594. ioc->sgl[i].iov_len,
  5595. &buf_handle, GFP_KERNEL);
  5596. if (!kbuff_arr[i]) {
  5597. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
  5598. "kernel SGL buffer for IOCTL\n");
  5599. error = -ENOMEM;
  5600. goto out;
  5601. }
  5602. /*
  5603. * We don't change the dma_coherent_mask, so
  5604. * pci_alloc_consistent only returns 32bit addresses
  5605. */
  5606. kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
  5607. kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
  5608. /*
  5609. * We created a kernel buffer corresponding to the
  5610. * user buffer. Now copy in from the user buffer
  5611. */
  5612. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  5613. (u32) (ioc->sgl[i].iov_len))) {
  5614. error = -EFAULT;
  5615. goto out;
  5616. }
  5617. }
  5618. if (ioc->sense_len) {
  5619. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  5620. &sense_handle, GFP_KERNEL);
  5621. if (!sense) {
  5622. error = -ENOMEM;
  5623. goto out;
  5624. }
  5625. sense_ptr =
  5626. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  5627. *sense_ptr = cpu_to_le32(sense_handle);
  5628. }
  5629. /*
  5630. * Set the sync_cmd flag so that the ISR knows not to complete this
  5631. * cmd to the SCSI mid-layer
  5632. */
  5633. cmd->sync_cmd = 1;
  5634. if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
  5635. cmd->sync_cmd = 0;
  5636. dev_err(&instance->pdev->dev,
  5637. "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
  5638. __func__, __LINE__, cmd->frame->dcmd.opcode,
  5639. cmd->cmd_status_drv);
  5640. return -EBUSY;
  5641. }
  5642. cmd->sync_cmd = 0;
  5643. if (instance->unload == 1) {
  5644. dev_info(&instance->pdev->dev, "Driver unload is in progress "
  5645. "don't submit data to application\n");
  5646. goto out;
  5647. }
  5648. /*
  5649. * copy out the kernel buffers to user buffers
  5650. */
  5651. for (i = 0; i < ioc->sge_count; i++) {
  5652. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  5653. ioc->sgl[i].iov_len)) {
  5654. error = -EFAULT;
  5655. goto out;
  5656. }
  5657. }
  5658. /*
  5659. * copy out the sense
  5660. */
  5661. if (ioc->sense_len) {
  5662. /*
  5663. * sense_ptr points to the location that has the user
  5664. * sense buffer address
  5665. */
  5666. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  5667. ioc->sense_off);
  5668. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  5669. sense, ioc->sense_len)) {
  5670. dev_err(&instance->pdev->dev, "Failed to copy out to user "
  5671. "sense data\n");
  5672. error = -EFAULT;
  5673. goto out;
  5674. }
  5675. }
  5676. /*
  5677. * copy the status codes returned by the fw
  5678. */
  5679. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  5680. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  5681. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
  5682. error = -EFAULT;
  5683. }
  5684. out:
  5685. if (sense) {
  5686. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  5687. sense, sense_handle);
  5688. }
  5689. for (i = 0; i < ioc->sge_count; i++) {
  5690. if (kbuff_arr[i]) {
  5691. dma_free_coherent(&instance->pdev->dev,
  5692. le32_to_cpu(kern_sge32[i].length),
  5693. kbuff_arr[i],
  5694. le32_to_cpu(kern_sge32[i].phys_addr));
  5695. kbuff_arr[i] = NULL;
  5696. }
  5697. }
  5698. megasas_return_cmd(instance, cmd);
  5699. return error;
  5700. }
  5701. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  5702. {
  5703. struct megasas_iocpacket __user *user_ioc =
  5704. (struct megasas_iocpacket __user *)arg;
  5705. struct megasas_iocpacket *ioc;
  5706. struct megasas_instance *instance;
  5707. int error;
  5708. int i;
  5709. unsigned long flags;
  5710. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  5711. ioc = memdup_user(user_ioc, sizeof(*ioc));
  5712. if (IS_ERR(ioc))
  5713. return PTR_ERR(ioc);
  5714. instance = megasas_lookup_instance(ioc->host_no);
  5715. if (!instance) {
  5716. error = -ENODEV;
  5717. goto out_kfree_ioc;
  5718. }
  5719. /* Adjust ioctl wait time for VF mode */
  5720. if (instance->requestorId)
  5721. wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
  5722. /* Block ioctls in VF mode */
  5723. if (instance->requestorId && !allow_vf_ioctls) {
  5724. error = -ENODEV;
  5725. goto out_kfree_ioc;
  5726. }
  5727. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  5728. dev_err(&instance->pdev->dev, "Controller in crit error\n");
  5729. error = -ENODEV;
  5730. goto out_kfree_ioc;
  5731. }
  5732. if (instance->unload == 1) {
  5733. error = -ENODEV;
  5734. goto out_kfree_ioc;
  5735. }
  5736. if (down_interruptible(&instance->ioctl_sem)) {
  5737. error = -ERESTARTSYS;
  5738. goto out_kfree_ioc;
  5739. }
  5740. for (i = 0; i < wait_time; i++) {
  5741. spin_lock_irqsave(&instance->hba_lock, flags);
  5742. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
  5743. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5744. break;
  5745. }
  5746. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5747. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  5748. dev_notice(&instance->pdev->dev, "waiting"
  5749. "for controller reset to finish\n");
  5750. }
  5751. msleep(1000);
  5752. }
  5753. spin_lock_irqsave(&instance->hba_lock, flags);
  5754. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  5755. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5756. dev_err(&instance->pdev->dev, "timed out while"
  5757. "waiting for HBA to recover\n");
  5758. error = -ENODEV;
  5759. goto out_up;
  5760. }
  5761. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5762. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  5763. out_up:
  5764. up(&instance->ioctl_sem);
  5765. out_kfree_ioc:
  5766. kfree(ioc);
  5767. return error;
  5768. }
  5769. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  5770. {
  5771. struct megasas_instance *instance;
  5772. struct megasas_aen aen;
  5773. int error;
  5774. int i;
  5775. unsigned long flags;
  5776. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  5777. if (file->private_data != file) {
  5778. printk(KERN_DEBUG "megasas: fasync_helper was not "
  5779. "called first\n");
  5780. return -EINVAL;
  5781. }
  5782. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  5783. return -EFAULT;
  5784. instance = megasas_lookup_instance(aen.host_no);
  5785. if (!instance)
  5786. return -ENODEV;
  5787. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  5788. return -ENODEV;
  5789. }
  5790. if (instance->unload == 1) {
  5791. return -ENODEV;
  5792. }
  5793. for (i = 0; i < wait_time; i++) {
  5794. spin_lock_irqsave(&instance->hba_lock, flags);
  5795. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
  5796. spin_unlock_irqrestore(&instance->hba_lock,
  5797. flags);
  5798. break;
  5799. }
  5800. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5801. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  5802. dev_notice(&instance->pdev->dev, "waiting for"
  5803. "controller reset to finish\n");
  5804. }
  5805. msleep(1000);
  5806. }
  5807. spin_lock_irqsave(&instance->hba_lock, flags);
  5808. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  5809. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5810. dev_err(&instance->pdev->dev, "timed out while waiting"
  5811. "for HBA to recover\n");
  5812. return -ENODEV;
  5813. }
  5814. spin_unlock_irqrestore(&instance->hba_lock, flags);
  5815. mutex_lock(&instance->reset_mutex);
  5816. error = megasas_register_aen(instance, aen.seq_num,
  5817. aen.class_locale_word);
  5818. mutex_unlock(&instance->reset_mutex);
  5819. return error;
  5820. }
  5821. /**
  5822. * megasas_mgmt_ioctl - char node ioctl entry point
  5823. */
  5824. static long
  5825. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  5826. {
  5827. switch (cmd) {
  5828. case MEGASAS_IOC_FIRMWARE:
  5829. return megasas_mgmt_ioctl_fw(file, arg);
  5830. case MEGASAS_IOC_GET_AEN:
  5831. return megasas_mgmt_ioctl_aen(file, arg);
  5832. }
  5833. return -ENOTTY;
  5834. }
  5835. #ifdef CONFIG_COMPAT
  5836. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  5837. {
  5838. struct compat_megasas_iocpacket __user *cioc =
  5839. (struct compat_megasas_iocpacket __user *)arg;
  5840. struct megasas_iocpacket __user *ioc =
  5841. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  5842. int i;
  5843. int error = 0;
  5844. compat_uptr_t ptr;
  5845. u32 local_sense_off;
  5846. u32 local_sense_len;
  5847. u32 user_sense_off;
  5848. if (clear_user(ioc, sizeof(*ioc)))
  5849. return -EFAULT;
  5850. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  5851. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  5852. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  5853. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  5854. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  5855. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  5856. return -EFAULT;
  5857. /*
  5858. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  5859. * sense_len is not null, so prepare the 64bit value under
  5860. * the same condition.
  5861. */
  5862. if (get_user(local_sense_off, &ioc->sense_off) ||
  5863. get_user(local_sense_len, &ioc->sense_len) ||
  5864. get_user(user_sense_off, &cioc->sense_off))
  5865. return -EFAULT;
  5866. if (local_sense_len) {
  5867. void __user **sense_ioc_ptr =
  5868. (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
  5869. compat_uptr_t *sense_cioc_ptr =
  5870. (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
  5871. if (get_user(ptr, sense_cioc_ptr) ||
  5872. put_user(compat_ptr(ptr), sense_ioc_ptr))
  5873. return -EFAULT;
  5874. }
  5875. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  5876. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  5877. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  5878. copy_in_user(&ioc->sgl[i].iov_len,
  5879. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  5880. return -EFAULT;
  5881. }
  5882. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  5883. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  5884. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  5885. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  5886. return -EFAULT;
  5887. }
  5888. return error;
  5889. }
  5890. static long
  5891. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  5892. unsigned long arg)
  5893. {
  5894. switch (cmd) {
  5895. case MEGASAS_IOC_FIRMWARE32:
  5896. return megasas_mgmt_compat_ioctl_fw(file, arg);
  5897. case MEGASAS_IOC_GET_AEN:
  5898. return megasas_mgmt_ioctl_aen(file, arg);
  5899. }
  5900. return -ENOTTY;
  5901. }
  5902. #endif
  5903. /*
  5904. * File operations structure for management interface
  5905. */
  5906. static const struct file_operations megasas_mgmt_fops = {
  5907. .owner = THIS_MODULE,
  5908. .open = megasas_mgmt_open,
  5909. .fasync = megasas_mgmt_fasync,
  5910. .unlocked_ioctl = megasas_mgmt_ioctl,
  5911. .poll = megasas_mgmt_poll,
  5912. #ifdef CONFIG_COMPAT
  5913. .compat_ioctl = megasas_mgmt_compat_ioctl,
  5914. #endif
  5915. .llseek = noop_llseek,
  5916. };
  5917. /*
  5918. * PCI hotplug support registration structure
  5919. */
  5920. static struct pci_driver megasas_pci_driver = {
  5921. .name = "megaraid_sas",
  5922. .id_table = megasas_pci_table,
  5923. .probe = megasas_probe_one,
  5924. .remove = megasas_detach_one,
  5925. .suspend = megasas_suspend,
  5926. .resume = megasas_resume,
  5927. .shutdown = megasas_shutdown,
  5928. };
  5929. /*
  5930. * Sysfs driver attributes
  5931. */
  5932. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  5933. {
  5934. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  5935. MEGASAS_VERSION);
  5936. }
  5937. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  5938. static ssize_t
  5939. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  5940. {
  5941. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  5942. MEGASAS_RELDATE);
  5943. }
  5944. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
  5945. static ssize_t
  5946. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  5947. {
  5948. return sprintf(buf, "%u\n", support_poll_for_event);
  5949. }
  5950. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  5951. megasas_sysfs_show_support_poll_for_event, NULL);
  5952. static ssize_t
  5953. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  5954. {
  5955. return sprintf(buf, "%u\n", support_device_change);
  5956. }
  5957. static DRIVER_ATTR(support_device_change, S_IRUGO,
  5958. megasas_sysfs_show_support_device_change, NULL);
  5959. static ssize_t
  5960. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  5961. {
  5962. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  5963. }
  5964. static ssize_t
  5965. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  5966. {
  5967. int retval = count;
  5968. if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
  5969. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  5970. retval = -EINVAL;
  5971. }
  5972. return retval;
  5973. }
  5974. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  5975. megasas_sysfs_set_dbg_lvl);
  5976. static void
  5977. megasas_aen_polling(struct work_struct *work)
  5978. {
  5979. struct megasas_aen_event *ev =
  5980. container_of(work, struct megasas_aen_event, hotplug_work.work);
  5981. struct megasas_instance *instance = ev->instance;
  5982. union megasas_evt_class_locale class_locale;
  5983. struct Scsi_Host *host;
  5984. struct scsi_device *sdev1;
  5985. u16 pd_index = 0;
  5986. u16 ld_index = 0;
  5987. int i, j, doscan = 0;
  5988. u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
  5989. int error;
  5990. u8 dcmd_ret = DCMD_SUCCESS;
  5991. if (!instance) {
  5992. printk(KERN_ERR "invalid instance!\n");
  5993. kfree(ev);
  5994. return;
  5995. }
  5996. /* Adjust event workqueue thread wait time for VF mode */
  5997. if (instance->requestorId)
  5998. wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
  5999. /* Don't run the event workqueue thread if OCR is running */
  6000. mutex_lock(&instance->reset_mutex);
  6001. instance->ev = NULL;
  6002. host = instance->host;
  6003. if (instance->evt_detail) {
  6004. megasas_decode_evt(instance);
  6005. switch (le32_to_cpu(instance->evt_detail->code)) {
  6006. case MR_EVT_PD_INSERTED:
  6007. case MR_EVT_PD_REMOVED:
  6008. dcmd_ret = megasas_get_pd_list(instance);
  6009. if (dcmd_ret == DCMD_SUCCESS)
  6010. doscan = SCAN_PD_CHANNEL;
  6011. break;
  6012. case MR_EVT_LD_OFFLINE:
  6013. case MR_EVT_CFG_CLEARED:
  6014. case MR_EVT_LD_DELETED:
  6015. case MR_EVT_LD_CREATED:
  6016. if (!instance->requestorId ||
  6017. (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
  6018. dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
  6019. if (dcmd_ret == DCMD_SUCCESS)
  6020. doscan = SCAN_VD_CHANNEL;
  6021. break;
  6022. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  6023. case MR_EVT_FOREIGN_CFG_IMPORTED:
  6024. case MR_EVT_LD_STATE_CHANGE:
  6025. dcmd_ret = megasas_get_pd_list(instance);
  6026. if (dcmd_ret != DCMD_SUCCESS)
  6027. break;
  6028. if (!instance->requestorId ||
  6029. (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
  6030. dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
  6031. if (dcmd_ret != DCMD_SUCCESS)
  6032. break;
  6033. doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
  6034. dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
  6035. instance->host->host_no);
  6036. break;
  6037. case MR_EVT_CTRL_PROP_CHANGED:
  6038. dcmd_ret = megasas_get_ctrl_info(instance);
  6039. break;
  6040. default:
  6041. doscan = 0;
  6042. break;
  6043. }
  6044. } else {
  6045. dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
  6046. mutex_unlock(&instance->reset_mutex);
  6047. kfree(ev);
  6048. return;
  6049. }
  6050. mutex_unlock(&instance->reset_mutex);
  6051. if (doscan & SCAN_PD_CHANNEL) {
  6052. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  6053. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  6054. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  6055. sdev1 = scsi_device_lookup(host, i, j, 0);
  6056. if (instance->pd_list[pd_index].driveState ==
  6057. MR_PD_STATE_SYSTEM) {
  6058. if (!sdev1)
  6059. scsi_add_device(host, i, j, 0);
  6060. else
  6061. scsi_device_put(sdev1);
  6062. } else {
  6063. if (sdev1) {
  6064. scsi_remove_device(sdev1);
  6065. scsi_device_put(sdev1);
  6066. }
  6067. }
  6068. }
  6069. }
  6070. }
  6071. if (doscan & SCAN_VD_CHANNEL) {
  6072. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  6073. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  6074. ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  6075. sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
  6076. if (instance->ld_ids[ld_index] != 0xff) {
  6077. if (!sdev1)
  6078. scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
  6079. else
  6080. scsi_device_put(sdev1);
  6081. } else {
  6082. if (sdev1) {
  6083. scsi_remove_device(sdev1);
  6084. scsi_device_put(sdev1);
  6085. }
  6086. }
  6087. }
  6088. }
  6089. }
  6090. if (dcmd_ret == DCMD_SUCCESS)
  6091. seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
  6092. else
  6093. seq_num = instance->last_seq_num;
  6094. /* Register AEN with FW for latest sequence number plus 1 */
  6095. class_locale.members.reserved = 0;
  6096. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  6097. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  6098. if (instance->aen_cmd != NULL) {
  6099. kfree(ev);
  6100. return;
  6101. }
  6102. mutex_lock(&instance->reset_mutex);
  6103. error = megasas_register_aen(instance, seq_num,
  6104. class_locale.word);
  6105. if (error)
  6106. dev_err(&instance->pdev->dev,
  6107. "register aen failed error %x\n", error);
  6108. mutex_unlock(&instance->reset_mutex);
  6109. kfree(ev);
  6110. }
  6111. /**
  6112. * megasas_init - Driver load entry point
  6113. */
  6114. static int __init megasas_init(void)
  6115. {
  6116. int rval;
  6117. /*
  6118. * Booted in kdump kernel, minimize memory footprints by
  6119. * disabling few features
  6120. */
  6121. if (reset_devices) {
  6122. msix_vectors = 1;
  6123. rdpq_enable = 0;
  6124. dual_qdepth_disable = 1;
  6125. }
  6126. /*
  6127. * Announce driver version and other information
  6128. */
  6129. pr_info("megasas: %s\n", MEGASAS_VERSION);
  6130. spin_lock_init(&poll_aen_lock);
  6131. support_poll_for_event = 2;
  6132. support_device_change = 1;
  6133. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  6134. /*
  6135. * Register character device node
  6136. */
  6137. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  6138. if (rval < 0) {
  6139. printk(KERN_DEBUG "megasas: failed to open device node\n");
  6140. return rval;
  6141. }
  6142. megasas_mgmt_majorno = rval;
  6143. /*
  6144. * Register ourselves as PCI hotplug module
  6145. */
  6146. rval = pci_register_driver(&megasas_pci_driver);
  6147. if (rval) {
  6148. printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
  6149. goto err_pcidrv;
  6150. }
  6151. rval = driver_create_file(&megasas_pci_driver.driver,
  6152. &driver_attr_version);
  6153. if (rval)
  6154. goto err_dcf_attr_ver;
  6155. rval = driver_create_file(&megasas_pci_driver.driver,
  6156. &driver_attr_release_date);
  6157. if (rval)
  6158. goto err_dcf_rel_date;
  6159. rval = driver_create_file(&megasas_pci_driver.driver,
  6160. &driver_attr_support_poll_for_event);
  6161. if (rval)
  6162. goto err_dcf_support_poll_for_event;
  6163. rval = driver_create_file(&megasas_pci_driver.driver,
  6164. &driver_attr_dbg_lvl);
  6165. if (rval)
  6166. goto err_dcf_dbg_lvl;
  6167. rval = driver_create_file(&megasas_pci_driver.driver,
  6168. &driver_attr_support_device_change);
  6169. if (rval)
  6170. goto err_dcf_support_device_change;
  6171. return rval;
  6172. err_dcf_support_device_change:
  6173. driver_remove_file(&megasas_pci_driver.driver,
  6174. &driver_attr_dbg_lvl);
  6175. err_dcf_dbg_lvl:
  6176. driver_remove_file(&megasas_pci_driver.driver,
  6177. &driver_attr_support_poll_for_event);
  6178. err_dcf_support_poll_for_event:
  6179. driver_remove_file(&megasas_pci_driver.driver,
  6180. &driver_attr_release_date);
  6181. err_dcf_rel_date:
  6182. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  6183. err_dcf_attr_ver:
  6184. pci_unregister_driver(&megasas_pci_driver);
  6185. err_pcidrv:
  6186. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  6187. return rval;
  6188. }
  6189. /**
  6190. * megasas_exit - Driver unload entry point
  6191. */
  6192. static void __exit megasas_exit(void)
  6193. {
  6194. driver_remove_file(&megasas_pci_driver.driver,
  6195. &driver_attr_dbg_lvl);
  6196. driver_remove_file(&megasas_pci_driver.driver,
  6197. &driver_attr_support_poll_for_event);
  6198. driver_remove_file(&megasas_pci_driver.driver,
  6199. &driver_attr_support_device_change);
  6200. driver_remove_file(&megasas_pci_driver.driver,
  6201. &driver_attr_release_date);
  6202. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  6203. pci_unregister_driver(&megasas_pci_driver);
  6204. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  6205. }
  6206. module_init(megasas_init);
  6207. module_exit(megasas_exit);