mpt2sas_scsih.c 241 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465
  1. /*
  2. * Scsi Host Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
  5. * Copyright (C) 2007-2012 LSI Corporation
  6. * (mailto:DL-MPTFusionLinux@lsi.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. * DISCLAIMER OF LIABILITY
  29. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  35. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  39. * USA.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/init.h>
  44. #include <linux/errno.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/sched.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/delay.h>
  49. #include <linux/pci.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/aer.h>
  52. #include <linux/raid_class.h>
  53. #include <linux/slab.h>
  54. #include "mpt2sas_base.h"
  55. MODULE_AUTHOR(MPT2SAS_AUTHOR);
  56. MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
  57. MODULE_LICENSE("GPL");
  58. MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
  59. #define RAID_CHANNEL 1
  60. /* forward proto's */
  61. static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  62. struct _sas_node *sas_expander);
  63. static void _firmware_event_work(struct work_struct *work);
  64. static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
  65. static void _scsih_scan_start(struct Scsi_Host *shost);
  66. static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
  67. /* global parameters */
  68. LIST_HEAD(mpt2sas_ioc_list);
  69. /* local parameters */
  70. static u8 scsi_io_cb_idx = -1;
  71. static u8 tm_cb_idx = -1;
  72. static u8 ctl_cb_idx = -1;
  73. static u8 base_cb_idx = -1;
  74. static u8 port_enable_cb_idx = -1;
  75. static u8 transport_cb_idx = -1;
  76. static u8 scsih_cb_idx = -1;
  77. static u8 config_cb_idx = -1;
  78. static int mpt_ids;
  79. static u8 tm_tr_cb_idx = -1 ;
  80. static u8 tm_tr_volume_cb_idx = -1 ;
  81. static u8 tm_sas_control_cb_idx = -1;
  82. /* command line options */
  83. static u32 logging_level;
  84. MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
  85. "(default=0)");
  86. static ushort max_sectors = 0xFFFF;
  87. module_param(max_sectors, ushort, 0);
  88. MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
  89. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  90. #define MPT2SAS_MAX_LUN (16895)
  91. static int max_lun = MPT2SAS_MAX_LUN;
  92. module_param(max_lun, int, 0);
  93. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  94. /* diag_buffer_enable is bitwise
  95. * bit 0 set = TRACE
  96. * bit 1 set = SNAPSHOT
  97. * bit 2 set = EXTENDED
  98. *
  99. * Either bit can be set, or both
  100. */
  101. static int diag_buffer_enable = -1;
  102. module_param(diag_buffer_enable, int, 0);
  103. MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
  104. "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
  105. static int disable_discovery = -1;
  106. module_param(disable_discovery, int, 0);
  107. MODULE_PARM_DESC(disable_discovery, " disable discovery ");
  108. /**
  109. * struct sense_info - common structure for obtaining sense keys
  110. * @skey: sense key
  111. * @asc: additional sense code
  112. * @ascq: additional sense code qualifier
  113. */
  114. struct sense_info {
  115. u8 skey;
  116. u8 asc;
  117. u8 ascq;
  118. };
  119. #define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
  120. #define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
  121. #define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
  122. /**
  123. * struct fw_event_work - firmware event struct
  124. * @list: link list framework
  125. * @work: work object (ioc->fault_reset_work_q)
  126. * @cancel_pending_work: flag set during reset handling
  127. * @ioc: per adapter object
  128. * @device_handle: device handle
  129. * @VF_ID: virtual function id
  130. * @VP_ID: virtual port id
  131. * @ignore: flag meaning this event has been marked to ignore
  132. * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
  133. * @event_data: reply event data payload follows
  134. *
  135. * This object stored on ioc->fw_event_list.
  136. */
  137. struct fw_event_work {
  138. struct list_head list;
  139. u8 cancel_pending_work;
  140. struct delayed_work delayed_work;
  141. struct MPT2SAS_ADAPTER *ioc;
  142. u16 device_handle;
  143. u8 VF_ID;
  144. u8 VP_ID;
  145. u8 ignore;
  146. u16 event;
  147. void *event_data;
  148. };
  149. /* raid transport support */
  150. static struct raid_template *mpt2sas_raid_template;
  151. /**
  152. * struct _scsi_io_transfer - scsi io transfer
  153. * @handle: sas device handle (assigned by firmware)
  154. * @is_raid: flag set for hidden raid components
  155. * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
  156. * @data_length: data transfer length
  157. * @data_dma: dma pointer to data
  158. * @sense: sense data
  159. * @lun: lun number
  160. * @cdb_length: cdb length
  161. * @cdb: cdb contents
  162. * @timeout: timeout for this command
  163. * @VF_ID: virtual function id
  164. * @VP_ID: virtual port id
  165. * @valid_reply: flag set for reply message
  166. * @sense_length: sense length
  167. * @ioc_status: ioc status
  168. * @scsi_state: scsi state
  169. * @scsi_status: scsi staus
  170. * @log_info: log information
  171. * @transfer_length: data length transfer when there is a reply message
  172. *
  173. * Used for sending internal scsi commands to devices within this module.
  174. * Refer to _scsi_send_scsi_io().
  175. */
  176. struct _scsi_io_transfer {
  177. u16 handle;
  178. u8 is_raid;
  179. enum dma_data_direction dir;
  180. u32 data_length;
  181. dma_addr_t data_dma;
  182. u8 sense[SCSI_SENSE_BUFFERSIZE];
  183. u32 lun;
  184. u8 cdb_length;
  185. u8 cdb[32];
  186. u8 timeout;
  187. u8 VF_ID;
  188. u8 VP_ID;
  189. u8 valid_reply;
  190. /* the following bits are only valid when 'valid_reply = 1' */
  191. u32 sense_length;
  192. u16 ioc_status;
  193. u8 scsi_state;
  194. u8 scsi_status;
  195. u32 log_info;
  196. u32 transfer_length;
  197. };
  198. /*
  199. * The pci device ids are defined in mpi/mpi2_cnfg.h.
  200. */
  201. static struct pci_device_id scsih_pci_table[] = {
  202. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
  203. PCI_ANY_ID, PCI_ANY_ID },
  204. /* Falcon ~ 2008*/
  205. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
  206. PCI_ANY_ID, PCI_ANY_ID },
  207. /* Liberator ~ 2108 */
  208. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
  209. PCI_ANY_ID, PCI_ANY_ID },
  210. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
  211. PCI_ANY_ID, PCI_ANY_ID },
  212. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
  213. PCI_ANY_ID, PCI_ANY_ID },
  214. /* Meteor ~ 2116 */
  215. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
  216. PCI_ANY_ID, PCI_ANY_ID },
  217. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
  218. PCI_ANY_ID, PCI_ANY_ID },
  219. /* Thunderbolt ~ 2208 */
  220. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
  221. PCI_ANY_ID, PCI_ANY_ID },
  222. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
  223. PCI_ANY_ID, PCI_ANY_ID },
  224. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
  225. PCI_ANY_ID, PCI_ANY_ID },
  226. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
  227. PCI_ANY_ID, PCI_ANY_ID },
  228. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
  229. PCI_ANY_ID, PCI_ANY_ID },
  230. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
  231. PCI_ANY_ID, PCI_ANY_ID },
  232. /* Mustang ~ 2308 */
  233. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
  234. PCI_ANY_ID, PCI_ANY_ID },
  235. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
  236. PCI_ANY_ID, PCI_ANY_ID },
  237. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
  238. PCI_ANY_ID, PCI_ANY_ID },
  239. /* SSS6200 */
  240. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
  241. PCI_ANY_ID, PCI_ANY_ID },
  242. {0} /* Terminating entry */
  243. };
  244. MODULE_DEVICE_TABLE(pci, scsih_pci_table);
  245. /**
  246. * _scsih_set_debug_level - global setting of ioc->logging_level.
  247. *
  248. * Note: The logging levels are defined in mpt2sas_debug.h.
  249. */
  250. static int
  251. _scsih_set_debug_level(const char *val, struct kernel_param *kp)
  252. {
  253. int ret = param_set_int(val, kp);
  254. struct MPT2SAS_ADAPTER *ioc;
  255. if (ret)
  256. return ret;
  257. printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
  258. list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
  259. ioc->logging_level = logging_level;
  260. return 0;
  261. }
  262. module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
  263. &logging_level, 0644);
  264. /**
  265. * _scsih_srch_boot_sas_address - search based on sas_address
  266. * @sas_address: sas address
  267. * @boot_device: boot device object from bios page 2
  268. *
  269. * Returns 1 when there's a match, 0 means no match.
  270. */
  271. static inline int
  272. _scsih_srch_boot_sas_address(u64 sas_address,
  273. Mpi2BootDeviceSasWwid_t *boot_device)
  274. {
  275. return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
  276. }
  277. /**
  278. * _scsih_srch_boot_device_name - search based on device name
  279. * @device_name: device name specified in INDENTIFY fram
  280. * @boot_device: boot device object from bios page 2
  281. *
  282. * Returns 1 when there's a match, 0 means no match.
  283. */
  284. static inline int
  285. _scsih_srch_boot_device_name(u64 device_name,
  286. Mpi2BootDeviceDeviceName_t *boot_device)
  287. {
  288. return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
  289. }
  290. /**
  291. * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
  292. * @enclosure_logical_id: enclosure logical id
  293. * @slot_number: slot number
  294. * @boot_device: boot device object from bios page 2
  295. *
  296. * Returns 1 when there's a match, 0 means no match.
  297. */
  298. static inline int
  299. _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
  300. Mpi2BootDeviceEnclosureSlot_t *boot_device)
  301. {
  302. return (enclosure_logical_id == le64_to_cpu(boot_device->
  303. EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
  304. SlotNumber)) ? 1 : 0;
  305. }
  306. /**
  307. * _scsih_is_boot_device - search for matching boot device.
  308. * @sas_address: sas address
  309. * @device_name: device name specified in INDENTIFY fram
  310. * @enclosure_logical_id: enclosure logical id
  311. * @slot_number: slot number
  312. * @form: specifies boot device form
  313. * @boot_device: boot device object from bios page 2
  314. *
  315. * Returns 1 when there's a match, 0 means no match.
  316. */
  317. static int
  318. _scsih_is_boot_device(u64 sas_address, u64 device_name,
  319. u64 enclosure_logical_id, u16 slot, u8 form,
  320. Mpi2BiosPage2BootDevice_t *boot_device)
  321. {
  322. int rc = 0;
  323. switch (form) {
  324. case MPI2_BIOSPAGE2_FORM_SAS_WWID:
  325. if (!sas_address)
  326. break;
  327. rc = _scsih_srch_boot_sas_address(
  328. sas_address, &boot_device->SasWwid);
  329. break;
  330. case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
  331. if (!enclosure_logical_id)
  332. break;
  333. rc = _scsih_srch_boot_encl_slot(
  334. enclosure_logical_id,
  335. slot, &boot_device->EnclosureSlot);
  336. break;
  337. case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
  338. if (!device_name)
  339. break;
  340. rc = _scsih_srch_boot_device_name(
  341. device_name, &boot_device->DeviceName);
  342. break;
  343. case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
  344. break;
  345. }
  346. return rc;
  347. }
  348. /**
  349. * _scsih_get_sas_address - set the sas_address for given device handle
  350. * @handle: device handle
  351. * @sas_address: sas address
  352. *
  353. * Returns 0 success, non-zero when failure
  354. */
  355. static int
  356. _scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  357. u64 *sas_address)
  358. {
  359. Mpi2SasDevicePage0_t sas_device_pg0;
  360. Mpi2ConfigReply_t mpi_reply;
  361. u32 ioc_status;
  362. *sas_address = 0;
  363. if (handle <= ioc->sas_hba.num_phys) {
  364. *sas_address = ioc->sas_hba.sas_address;
  365. return 0;
  366. }
  367. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  368. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  369. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
  370. __FILE__, __LINE__, __func__);
  371. return -ENXIO;
  372. }
  373. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  374. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  375. *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  376. return 0;
  377. }
  378. /* we hit this becuase the given parent handle doesn't exist */
  379. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  380. return -ENXIO;
  381. /* else error case */
  382. printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
  383. "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
  384. __FILE__, __LINE__, __func__);
  385. return -EIO;
  386. }
  387. /**
  388. * _scsih_determine_boot_device - determine boot device.
  389. * @ioc: per adapter object
  390. * @device: either sas_device or raid_device object
  391. * @is_raid: [flag] 1 = raid object, 0 = sas object
  392. *
  393. * Determines whether this device should be first reported device to
  394. * to scsi-ml or sas transport, this purpose is for persistent boot device.
  395. * There are primary, alternate, and current entries in bios page 2. The order
  396. * priority is primary, alternate, then current. This routine saves
  397. * the corresponding device object and is_raid flag in the ioc object.
  398. * The saved data to be used later in _scsih_probe_boot_devices().
  399. */
  400. static void
  401. _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
  402. void *device, u8 is_raid)
  403. {
  404. struct _sas_device *sas_device;
  405. struct _raid_device *raid_device;
  406. u64 sas_address;
  407. u64 device_name;
  408. u64 enclosure_logical_id;
  409. u16 slot;
  410. /* only process this function when driver loads */
  411. if (!ioc->is_driver_loading)
  412. return;
  413. /* no Bios, return immediately */
  414. if (!ioc->bios_pg3.BiosVersion)
  415. return;
  416. if (!is_raid) {
  417. sas_device = device;
  418. sas_address = sas_device->sas_address;
  419. device_name = sas_device->device_name;
  420. enclosure_logical_id = sas_device->enclosure_logical_id;
  421. slot = sas_device->slot;
  422. } else {
  423. raid_device = device;
  424. sas_address = raid_device->wwid;
  425. device_name = 0;
  426. enclosure_logical_id = 0;
  427. slot = 0;
  428. }
  429. if (!ioc->req_boot_device.device) {
  430. if (_scsih_is_boot_device(sas_address, device_name,
  431. enclosure_logical_id, slot,
  432. (ioc->bios_pg2.ReqBootDeviceForm &
  433. MPI2_BIOSPAGE2_FORM_MASK),
  434. &ioc->bios_pg2.RequestedBootDevice)) {
  435. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
  436. "%s: req_boot_device(0x%016llx)\n",
  437. ioc->name, __func__,
  438. (unsigned long long)sas_address));
  439. ioc->req_boot_device.device = device;
  440. ioc->req_boot_device.is_raid = is_raid;
  441. }
  442. }
  443. if (!ioc->req_alt_boot_device.device) {
  444. if (_scsih_is_boot_device(sas_address, device_name,
  445. enclosure_logical_id, slot,
  446. (ioc->bios_pg2.ReqAltBootDeviceForm &
  447. MPI2_BIOSPAGE2_FORM_MASK),
  448. &ioc->bios_pg2.RequestedAltBootDevice)) {
  449. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
  450. "%s: req_alt_boot_device(0x%016llx)\n",
  451. ioc->name, __func__,
  452. (unsigned long long)sas_address));
  453. ioc->req_alt_boot_device.device = device;
  454. ioc->req_alt_boot_device.is_raid = is_raid;
  455. }
  456. }
  457. if (!ioc->current_boot_device.device) {
  458. if (_scsih_is_boot_device(sas_address, device_name,
  459. enclosure_logical_id, slot,
  460. (ioc->bios_pg2.CurrentBootDeviceForm &
  461. MPI2_BIOSPAGE2_FORM_MASK),
  462. &ioc->bios_pg2.CurrentBootDevice)) {
  463. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
  464. "%s: current_boot_device(0x%016llx)\n",
  465. ioc->name, __func__,
  466. (unsigned long long)sas_address));
  467. ioc->current_boot_device.device = device;
  468. ioc->current_boot_device.is_raid = is_raid;
  469. }
  470. }
  471. }
  472. /**
  473. * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
  474. * @ioc: per adapter object
  475. * @sas_address: sas address
  476. * Context: Calling function should acquire ioc->sas_device_lock
  477. *
  478. * This searches for sas_device based on sas_address, then return sas_device
  479. * object.
  480. */
  481. struct _sas_device *
  482. mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  483. u64 sas_address)
  484. {
  485. struct _sas_device *sas_device;
  486. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  487. if (sas_device->sas_address == sas_address)
  488. return sas_device;
  489. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  490. if (sas_device->sas_address == sas_address)
  491. return sas_device;
  492. return NULL;
  493. }
  494. /**
  495. * _scsih_sas_device_find_by_handle - sas device search
  496. * @ioc: per adapter object
  497. * @handle: sas device handle (assigned by firmware)
  498. * Context: Calling function should acquire ioc->sas_device_lock
  499. *
  500. * This searches for sas_device based on sas_address, then return sas_device
  501. * object.
  502. */
  503. static struct _sas_device *
  504. _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  505. {
  506. struct _sas_device *sas_device;
  507. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  508. if (sas_device->handle == handle)
  509. return sas_device;
  510. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  511. if (sas_device->handle == handle)
  512. return sas_device;
  513. return NULL;
  514. }
  515. /**
  516. * _scsih_sas_device_remove - remove sas_device from list.
  517. * @ioc: per adapter object
  518. * @sas_device: the sas_device object
  519. * Context: This function will acquire ioc->sas_device_lock.
  520. *
  521. * Removing object and freeing associated memory from the ioc->sas_device_list.
  522. */
  523. static void
  524. _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
  525. struct _sas_device *sas_device)
  526. {
  527. unsigned long flags;
  528. if (!sas_device)
  529. return;
  530. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  531. list_del(&sas_device->list);
  532. kfree(sas_device);
  533. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  534. }
  535. /**
  536. * _scsih_sas_device_add - insert sas_device to the list.
  537. * @ioc: per adapter object
  538. * @sas_device: the sas_device object
  539. * Context: This function will acquire ioc->sas_device_lock.
  540. *
  541. * Adding new object to the ioc->sas_device_list.
  542. */
  543. static void
  544. _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
  545. struct _sas_device *sas_device)
  546. {
  547. unsigned long flags;
  548. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
  549. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  550. sas_device->handle, (unsigned long long)sas_device->sas_address));
  551. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  552. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  553. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  554. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  555. sas_device->sas_address_parent)) {
  556. _scsih_sas_device_remove(ioc, sas_device);
  557. } else if (!sas_device->starget) {
  558. /* When asyn scanning is enabled, its not possible to remove
  559. * devices while scanning is turned on due to an oops in
  560. * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
  561. */
  562. if (!ioc->is_driver_loading)
  563. mpt2sas_transport_port_remove(ioc,
  564. sas_device->sas_address,
  565. sas_device->sas_address_parent);
  566. _scsih_sas_device_remove(ioc, sas_device);
  567. }
  568. }
  569. /**
  570. * _scsih_sas_device_init_add - insert sas_device to the list.
  571. * @ioc: per adapter object
  572. * @sas_device: the sas_device object
  573. * Context: This function will acquire ioc->sas_device_lock.
  574. *
  575. * Adding new object at driver load time to the ioc->sas_device_init_list.
  576. */
  577. static void
  578. _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
  579. struct _sas_device *sas_device)
  580. {
  581. unsigned long flags;
  582. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
  583. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  584. sas_device->handle, (unsigned long long)sas_device->sas_address));
  585. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  586. list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
  587. _scsih_determine_boot_device(ioc, sas_device, 0);
  588. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  589. }
  590. /**
  591. * _scsih_raid_device_find_by_id - raid device search
  592. * @ioc: per adapter object
  593. * @id: sas device target id
  594. * @channel: sas device channel
  595. * Context: Calling function should acquire ioc->raid_device_lock
  596. *
  597. * This searches for raid_device based on target id, then return raid_device
  598. * object.
  599. */
  600. static struct _raid_device *
  601. _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
  602. {
  603. struct _raid_device *raid_device, *r;
  604. r = NULL;
  605. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  606. if (raid_device->id == id && raid_device->channel == channel) {
  607. r = raid_device;
  608. goto out;
  609. }
  610. }
  611. out:
  612. return r;
  613. }
  614. /**
  615. * _scsih_raid_device_find_by_handle - raid device search
  616. * @ioc: per adapter object
  617. * @handle: sas device handle (assigned by firmware)
  618. * Context: Calling function should acquire ioc->raid_device_lock
  619. *
  620. * This searches for raid_device based on handle, then return raid_device
  621. * object.
  622. */
  623. static struct _raid_device *
  624. _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  625. {
  626. struct _raid_device *raid_device, *r;
  627. r = NULL;
  628. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  629. if (raid_device->handle != handle)
  630. continue;
  631. r = raid_device;
  632. goto out;
  633. }
  634. out:
  635. return r;
  636. }
  637. /**
  638. * _scsih_raid_device_find_by_wwid - raid device search
  639. * @ioc: per adapter object
  640. * @handle: sas device handle (assigned by firmware)
  641. * Context: Calling function should acquire ioc->raid_device_lock
  642. *
  643. * This searches for raid_device based on wwid, then return raid_device
  644. * object.
  645. */
  646. static struct _raid_device *
  647. _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
  648. {
  649. struct _raid_device *raid_device, *r;
  650. r = NULL;
  651. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  652. if (raid_device->wwid != wwid)
  653. continue;
  654. r = raid_device;
  655. goto out;
  656. }
  657. out:
  658. return r;
  659. }
  660. /**
  661. * _scsih_raid_device_add - add raid_device object
  662. * @ioc: per adapter object
  663. * @raid_device: raid_device object
  664. *
  665. * This is added to the raid_device_list link list.
  666. */
  667. static void
  668. _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
  669. struct _raid_device *raid_device)
  670. {
  671. unsigned long flags;
  672. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
  673. "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  674. raid_device->handle, (unsigned long long)raid_device->wwid));
  675. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  676. list_add_tail(&raid_device->list, &ioc->raid_device_list);
  677. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  678. }
  679. /**
  680. * _scsih_raid_device_remove - delete raid_device object
  681. * @ioc: per adapter object
  682. * @raid_device: raid_device object
  683. *
  684. */
  685. static void
  686. _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
  687. struct _raid_device *raid_device)
  688. {
  689. unsigned long flags;
  690. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  691. list_del(&raid_device->list);
  692. kfree(raid_device);
  693. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  694. }
  695. /**
  696. * mpt2sas_scsih_expander_find_by_handle - expander device search
  697. * @ioc: per adapter object
  698. * @handle: expander handle (assigned by firmware)
  699. * Context: Calling function should acquire ioc->sas_device_lock
  700. *
  701. * This searches for expander device based on handle, then returns the
  702. * sas_node object.
  703. */
  704. struct _sas_node *
  705. mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  706. {
  707. struct _sas_node *sas_expander, *r;
  708. r = NULL;
  709. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  710. if (sas_expander->handle != handle)
  711. continue;
  712. r = sas_expander;
  713. goto out;
  714. }
  715. out:
  716. return r;
  717. }
  718. /**
  719. * mpt2sas_scsih_expander_find_by_sas_address - expander device search
  720. * @ioc: per adapter object
  721. * @sas_address: sas address
  722. * Context: Calling function should acquire ioc->sas_node_lock.
  723. *
  724. * This searches for expander device based on sas_address, then returns the
  725. * sas_node object.
  726. */
  727. struct _sas_node *
  728. mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  729. u64 sas_address)
  730. {
  731. struct _sas_node *sas_expander, *r;
  732. r = NULL;
  733. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  734. if (sas_expander->sas_address != sas_address)
  735. continue;
  736. r = sas_expander;
  737. goto out;
  738. }
  739. out:
  740. return r;
  741. }
  742. /**
  743. * _scsih_expander_node_add - insert expander device to the list.
  744. * @ioc: per adapter object
  745. * @sas_expander: the sas_device object
  746. * Context: This function will acquire ioc->sas_node_lock.
  747. *
  748. * Adding new object to the ioc->sas_expander_list.
  749. *
  750. * Return nothing.
  751. */
  752. static void
  753. _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
  754. struct _sas_node *sas_expander)
  755. {
  756. unsigned long flags;
  757. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  758. list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
  759. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  760. }
  761. /**
  762. * _scsih_is_end_device - determines if device is an end device
  763. * @device_info: bitfield providing information about the device.
  764. * Context: none
  765. *
  766. * Returns 1 if end device.
  767. */
  768. static int
  769. _scsih_is_end_device(u32 device_info)
  770. {
  771. if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
  772. ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
  773. (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
  774. (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
  775. return 1;
  776. else
  777. return 0;
  778. }
  779. /**
  780. * _scsih_scsi_lookup_get - returns scmd entry
  781. * @ioc: per adapter object
  782. * @smid: system request message index
  783. *
  784. * Returns the smid stored scmd pointer.
  785. */
  786. static struct scsi_cmnd *
  787. _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  788. {
  789. return ioc->scsi_lookup[smid - 1].scmd;
  790. }
  791. /**
  792. * _scsih_scsi_lookup_get_clear - returns scmd entry
  793. * @ioc: per adapter object
  794. * @smid: system request message index
  795. *
  796. * Returns the smid stored scmd pointer.
  797. * Then will derefrence the stored scmd pointer.
  798. */
  799. static inline struct scsi_cmnd *
  800. _scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  801. {
  802. unsigned long flags;
  803. struct scsi_cmnd *scmd;
  804. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  805. scmd = ioc->scsi_lookup[smid - 1].scmd;
  806. ioc->scsi_lookup[smid - 1].scmd = NULL;
  807. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  808. return scmd;
  809. }
  810. /**
  811. * _scsih_scsi_lookup_find_by_scmd - scmd lookup
  812. * @ioc: per adapter object
  813. * @smid: system request message index
  814. * @scmd: pointer to scsi command object
  815. * Context: This function will acquire ioc->scsi_lookup_lock.
  816. *
  817. * This will search for a scmd pointer in the scsi_lookup array,
  818. * returning the revelent smid. A returned value of zero means invalid.
  819. */
  820. static u16
  821. _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
  822. *scmd)
  823. {
  824. u16 smid;
  825. unsigned long flags;
  826. int i;
  827. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  828. smid = 0;
  829. for (i = 0; i < ioc->scsiio_depth; i++) {
  830. if (ioc->scsi_lookup[i].scmd == scmd) {
  831. smid = ioc->scsi_lookup[i].smid;
  832. goto out;
  833. }
  834. }
  835. out:
  836. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  837. return smid;
  838. }
  839. /**
  840. * _scsih_scsi_lookup_find_by_target - search for matching channel:id
  841. * @ioc: per adapter object
  842. * @id: target id
  843. * @channel: channel
  844. * Context: This function will acquire ioc->scsi_lookup_lock.
  845. *
  846. * This will search for a matching channel:id in the scsi_lookup array,
  847. * returning 1 if found.
  848. */
  849. static u8
  850. _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
  851. int channel)
  852. {
  853. u8 found;
  854. unsigned long flags;
  855. int i;
  856. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  857. found = 0;
  858. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  859. if (ioc->scsi_lookup[i].scmd &&
  860. (ioc->scsi_lookup[i].scmd->device->id == id &&
  861. ioc->scsi_lookup[i].scmd->device->channel == channel)) {
  862. found = 1;
  863. goto out;
  864. }
  865. }
  866. out:
  867. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  868. return found;
  869. }
  870. /**
  871. * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
  872. * @ioc: per adapter object
  873. * @id: target id
  874. * @lun: lun number
  875. * @channel: channel
  876. * Context: This function will acquire ioc->scsi_lookup_lock.
  877. *
  878. * This will search for a matching channel:id:lun in the scsi_lookup array,
  879. * returning 1 if found.
  880. */
  881. static u8
  882. _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
  883. unsigned int lun, int channel)
  884. {
  885. u8 found;
  886. unsigned long flags;
  887. int i;
  888. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  889. found = 0;
  890. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  891. if (ioc->scsi_lookup[i].scmd &&
  892. (ioc->scsi_lookup[i].scmd->device->id == id &&
  893. ioc->scsi_lookup[i].scmd->device->channel == channel &&
  894. ioc->scsi_lookup[i].scmd->device->lun == lun)) {
  895. found = 1;
  896. goto out;
  897. }
  898. }
  899. out:
  900. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  901. return found;
  902. }
  903. /**
  904. * _scsih_get_chain_buffer_tracker - obtain chain tracker
  905. * @ioc: per adapter object
  906. * @smid: smid associated to an IO request
  907. *
  908. * Returns chain tracker(from ioc->free_chain_list)
  909. */
  910. static struct chain_tracker *
  911. _scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  912. {
  913. struct chain_tracker *chain_req;
  914. unsigned long flags;
  915. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  916. if (list_empty(&ioc->free_chain_list)) {
  917. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  918. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
  919. "available\n", ioc->name));
  920. return NULL;
  921. }
  922. chain_req = list_entry(ioc->free_chain_list.next,
  923. struct chain_tracker, tracker_list);
  924. list_del_init(&chain_req->tracker_list);
  925. list_add_tail(&chain_req->tracker_list,
  926. &ioc->scsi_lookup[smid - 1].chain_list);
  927. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  928. return chain_req;
  929. }
  930. /**
  931. * _scsih_build_scatter_gather - main sg creation routine
  932. * @ioc: per adapter object
  933. * @scmd: scsi command
  934. * @smid: system request message index
  935. * Context: none.
  936. *
  937. * The main routine that builds scatter gather table from a given
  938. * scsi request sent via the .queuecommand main handler.
  939. *
  940. * Returns 0 success, anything else error
  941. */
  942. static int
  943. _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
  944. struct scsi_cmnd *scmd, u16 smid)
  945. {
  946. Mpi2SCSIIORequest_t *mpi_request;
  947. dma_addr_t chain_dma;
  948. struct scatterlist *sg_scmd;
  949. void *sg_local, *chain;
  950. u32 chain_offset;
  951. u32 chain_length;
  952. u32 chain_flags;
  953. int sges_left;
  954. u32 sges_in_segment;
  955. u32 sgl_flags;
  956. u32 sgl_flags_last_element;
  957. u32 sgl_flags_end_buffer;
  958. struct chain_tracker *chain_req;
  959. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  960. /* init scatter gather flags */
  961. sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
  962. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  963. sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
  964. sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
  965. << MPI2_SGE_FLAGS_SHIFT;
  966. sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
  967. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
  968. << MPI2_SGE_FLAGS_SHIFT;
  969. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  970. sg_scmd = scsi_sglist(scmd);
  971. sges_left = scsi_dma_map(scmd);
  972. if (sges_left < 0) {
  973. sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
  974. " failed: request for %d bytes!\n", scsi_bufflen(scmd));
  975. return -ENOMEM;
  976. }
  977. sg_local = &mpi_request->SGL;
  978. sges_in_segment = ioc->max_sges_in_main_message;
  979. if (sges_left <= sges_in_segment)
  980. goto fill_in_last_segment;
  981. mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
  982. (sges_in_segment * ioc->sge_size))/4;
  983. /* fill in main message segment when there is a chain following */
  984. while (sges_in_segment) {
  985. if (sges_in_segment == 1)
  986. ioc->base_add_sg_single(sg_local,
  987. sgl_flags_last_element | sg_dma_len(sg_scmd),
  988. sg_dma_address(sg_scmd));
  989. else
  990. ioc->base_add_sg_single(sg_local, sgl_flags |
  991. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  992. sg_scmd = sg_next(sg_scmd);
  993. sg_local += ioc->sge_size;
  994. sges_left--;
  995. sges_in_segment--;
  996. }
  997. /* initializing the chain flags and pointers */
  998. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  999. chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
  1000. if (!chain_req)
  1001. return -1;
  1002. chain = chain_req->chain_buffer;
  1003. chain_dma = chain_req->chain_buffer_dma;
  1004. do {
  1005. sges_in_segment = (sges_left <=
  1006. ioc->max_sges_in_chain_message) ? sges_left :
  1007. ioc->max_sges_in_chain_message;
  1008. chain_offset = (sges_left == sges_in_segment) ?
  1009. 0 : (sges_in_segment * ioc->sge_size)/4;
  1010. chain_length = sges_in_segment * ioc->sge_size;
  1011. if (chain_offset) {
  1012. chain_offset = chain_offset <<
  1013. MPI2_SGE_CHAIN_OFFSET_SHIFT;
  1014. chain_length += ioc->sge_size;
  1015. }
  1016. ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
  1017. chain_length, chain_dma);
  1018. sg_local = chain;
  1019. if (!chain_offset)
  1020. goto fill_in_last_segment;
  1021. /* fill in chain segments */
  1022. while (sges_in_segment) {
  1023. if (sges_in_segment == 1)
  1024. ioc->base_add_sg_single(sg_local,
  1025. sgl_flags_last_element |
  1026. sg_dma_len(sg_scmd),
  1027. sg_dma_address(sg_scmd));
  1028. else
  1029. ioc->base_add_sg_single(sg_local, sgl_flags |
  1030. sg_dma_len(sg_scmd),
  1031. sg_dma_address(sg_scmd));
  1032. sg_scmd = sg_next(sg_scmd);
  1033. sg_local += ioc->sge_size;
  1034. sges_left--;
  1035. sges_in_segment--;
  1036. }
  1037. chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
  1038. if (!chain_req)
  1039. return -1;
  1040. chain = chain_req->chain_buffer;
  1041. chain_dma = chain_req->chain_buffer_dma;
  1042. } while (1);
  1043. fill_in_last_segment:
  1044. /* fill the last segment */
  1045. while (sges_left) {
  1046. if (sges_left == 1)
  1047. ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
  1048. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1049. else
  1050. ioc->base_add_sg_single(sg_local, sgl_flags |
  1051. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1052. sg_scmd = sg_next(sg_scmd);
  1053. sg_local += ioc->sge_size;
  1054. sges_left--;
  1055. }
  1056. return 0;
  1057. }
  1058. /**
  1059. * _scsih_adjust_queue_depth - setting device queue depth
  1060. * @sdev: scsi device struct
  1061. * @qdepth: requested queue depth
  1062. *
  1063. *
  1064. * Returns nothing
  1065. */
  1066. static void
  1067. _scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
  1068. {
  1069. struct Scsi_Host *shost = sdev->host;
  1070. int max_depth;
  1071. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1072. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1073. struct MPT2SAS_TARGET *sas_target_priv_data;
  1074. struct _sas_device *sas_device;
  1075. unsigned long flags;
  1076. max_depth = shost->can_queue;
  1077. /* limit max device queue for SATA to 32 */
  1078. sas_device_priv_data = sdev->hostdata;
  1079. if (!sas_device_priv_data)
  1080. goto not_sata;
  1081. sas_target_priv_data = sas_device_priv_data->sas_target;
  1082. if (!sas_target_priv_data)
  1083. goto not_sata;
  1084. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
  1085. goto not_sata;
  1086. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1087. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1088. sas_device_priv_data->sas_target->sas_address);
  1089. if (sas_device && sas_device->device_info &
  1090. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1091. max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
  1092. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1093. not_sata:
  1094. if (!sdev->tagged_supported)
  1095. max_depth = 1;
  1096. if (qdepth > max_depth)
  1097. qdepth = max_depth;
  1098. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1099. }
  1100. /**
  1101. * _scsih_change_queue_depth - setting device queue depth
  1102. * @sdev: scsi device struct
  1103. * @qdepth: requested queue depth
  1104. * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
  1105. * (see include/scsi/scsi_host.h for definition)
  1106. *
  1107. * Returns queue depth.
  1108. */
  1109. static int
  1110. _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  1111. {
  1112. if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
  1113. _scsih_adjust_queue_depth(sdev, qdepth);
  1114. else if (reason == SCSI_QDEPTH_QFULL)
  1115. scsi_track_queue_full(sdev, qdepth);
  1116. else
  1117. return -EOPNOTSUPP;
  1118. if (sdev->inquiry_len > 7)
  1119. sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
  1120. "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
  1121. sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
  1122. sdev->ordered_tags, sdev->scsi_level,
  1123. (sdev->inquiry[7] & 2) >> 1);
  1124. return sdev->queue_depth;
  1125. }
  1126. /**
  1127. * _scsih_change_queue_type - changing device queue tag type
  1128. * @sdev: scsi device struct
  1129. * @tag_type: requested tag type
  1130. *
  1131. * Returns queue tag type.
  1132. */
  1133. static int
  1134. _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
  1135. {
  1136. if (sdev->tagged_supported) {
  1137. scsi_set_tag_type(sdev, tag_type);
  1138. if (tag_type)
  1139. scsi_activate_tcq(sdev, sdev->queue_depth);
  1140. else
  1141. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1142. } else
  1143. tag_type = 0;
  1144. return tag_type;
  1145. }
  1146. /**
  1147. * _scsih_target_alloc - target add routine
  1148. * @starget: scsi target struct
  1149. *
  1150. * Returns 0 if ok. Any other return is assumed to be an error and
  1151. * the device is ignored.
  1152. */
  1153. static int
  1154. _scsih_target_alloc(struct scsi_target *starget)
  1155. {
  1156. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1157. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1158. struct MPT2SAS_TARGET *sas_target_priv_data;
  1159. struct _sas_device *sas_device;
  1160. struct _raid_device *raid_device;
  1161. unsigned long flags;
  1162. struct sas_rphy *rphy;
  1163. sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
  1164. if (!sas_target_priv_data)
  1165. return -ENOMEM;
  1166. starget->hostdata = sas_target_priv_data;
  1167. sas_target_priv_data->starget = starget;
  1168. sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
  1169. /* RAID volumes */
  1170. if (starget->channel == RAID_CHANNEL) {
  1171. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1172. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1173. starget->channel);
  1174. if (raid_device) {
  1175. sas_target_priv_data->handle = raid_device->handle;
  1176. sas_target_priv_data->sas_address = raid_device->wwid;
  1177. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
  1178. if (ioc->is_warpdrive)
  1179. sas_target_priv_data->raid_device = raid_device;
  1180. raid_device->starget = starget;
  1181. }
  1182. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1183. return 0;
  1184. }
  1185. /* sas/sata devices */
  1186. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1187. rphy = dev_to_rphy(starget->dev.parent);
  1188. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1189. rphy->identify.sas_address);
  1190. if (sas_device) {
  1191. sas_target_priv_data->handle = sas_device->handle;
  1192. sas_target_priv_data->sas_address = sas_device->sas_address;
  1193. sas_device->starget = starget;
  1194. sas_device->id = starget->id;
  1195. sas_device->channel = starget->channel;
  1196. if (test_bit(sas_device->handle, ioc->pd_handles))
  1197. sas_target_priv_data->flags |=
  1198. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1199. }
  1200. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1201. return 0;
  1202. }
  1203. /**
  1204. * _scsih_target_destroy - target destroy routine
  1205. * @starget: scsi target struct
  1206. *
  1207. * Returns nothing.
  1208. */
  1209. static void
  1210. _scsih_target_destroy(struct scsi_target *starget)
  1211. {
  1212. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1213. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1214. struct MPT2SAS_TARGET *sas_target_priv_data;
  1215. struct _sas_device *sas_device;
  1216. struct _raid_device *raid_device;
  1217. unsigned long flags;
  1218. struct sas_rphy *rphy;
  1219. sas_target_priv_data = starget->hostdata;
  1220. if (!sas_target_priv_data)
  1221. return;
  1222. if (starget->channel == RAID_CHANNEL) {
  1223. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1224. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1225. starget->channel);
  1226. if (raid_device) {
  1227. raid_device->starget = NULL;
  1228. raid_device->sdev = NULL;
  1229. }
  1230. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1231. goto out;
  1232. }
  1233. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1234. rphy = dev_to_rphy(starget->dev.parent);
  1235. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1236. rphy->identify.sas_address);
  1237. if (sas_device && (sas_device->starget == starget) &&
  1238. (sas_device->id == starget->id) &&
  1239. (sas_device->channel == starget->channel))
  1240. sas_device->starget = NULL;
  1241. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1242. out:
  1243. kfree(sas_target_priv_data);
  1244. starget->hostdata = NULL;
  1245. }
  1246. /**
  1247. * _scsih_slave_alloc - device add routine
  1248. * @sdev: scsi device struct
  1249. *
  1250. * Returns 0 if ok. Any other return is assumed to be an error and
  1251. * the device is ignored.
  1252. */
  1253. static int
  1254. _scsih_slave_alloc(struct scsi_device *sdev)
  1255. {
  1256. struct Scsi_Host *shost;
  1257. struct MPT2SAS_ADAPTER *ioc;
  1258. struct MPT2SAS_TARGET *sas_target_priv_data;
  1259. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1260. struct scsi_target *starget;
  1261. struct _raid_device *raid_device;
  1262. unsigned long flags;
  1263. sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
  1264. if (!sas_device_priv_data)
  1265. return -ENOMEM;
  1266. sas_device_priv_data->lun = sdev->lun;
  1267. sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
  1268. starget = scsi_target(sdev);
  1269. sas_target_priv_data = starget->hostdata;
  1270. sas_target_priv_data->num_luns++;
  1271. sas_device_priv_data->sas_target = sas_target_priv_data;
  1272. sdev->hostdata = sas_device_priv_data;
  1273. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
  1274. sdev->no_uld_attach = 1;
  1275. shost = dev_to_shost(&starget->dev);
  1276. ioc = shost_priv(shost);
  1277. if (starget->channel == RAID_CHANNEL) {
  1278. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1279. raid_device = _scsih_raid_device_find_by_id(ioc,
  1280. starget->id, starget->channel);
  1281. if (raid_device)
  1282. raid_device->sdev = sdev; /* raid is single lun */
  1283. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1284. }
  1285. return 0;
  1286. }
  1287. /**
  1288. * _scsih_slave_destroy - device destroy routine
  1289. * @sdev: scsi device struct
  1290. *
  1291. * Returns nothing.
  1292. */
  1293. static void
  1294. _scsih_slave_destroy(struct scsi_device *sdev)
  1295. {
  1296. struct MPT2SAS_TARGET *sas_target_priv_data;
  1297. struct scsi_target *starget;
  1298. struct Scsi_Host *shost;
  1299. struct MPT2SAS_ADAPTER *ioc;
  1300. struct _sas_device *sas_device;
  1301. unsigned long flags;
  1302. if (!sdev->hostdata)
  1303. return;
  1304. starget = scsi_target(sdev);
  1305. sas_target_priv_data = starget->hostdata;
  1306. sas_target_priv_data->num_luns--;
  1307. shost = dev_to_shost(&starget->dev);
  1308. ioc = shost_priv(shost);
  1309. if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
  1310. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1311. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1312. sas_target_priv_data->sas_address);
  1313. if (sas_device && !sas_target_priv_data->num_luns)
  1314. sas_device->starget = NULL;
  1315. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1316. }
  1317. kfree(sdev->hostdata);
  1318. sdev->hostdata = NULL;
  1319. }
  1320. /**
  1321. * _scsih_display_sata_capabilities - sata capabilities
  1322. * @ioc: per adapter object
  1323. * @handle: device handle
  1324. * @sdev: scsi device struct
  1325. */
  1326. static void
  1327. _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1328. u16 handle, struct scsi_device *sdev)
  1329. {
  1330. Mpi2ConfigReply_t mpi_reply;
  1331. Mpi2SasDevicePage0_t sas_device_pg0;
  1332. u32 ioc_status;
  1333. u16 flags;
  1334. u32 device_info;
  1335. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  1336. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  1337. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1338. ioc->name, __FILE__, __LINE__, __func__);
  1339. return;
  1340. }
  1341. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1342. MPI2_IOCSTATUS_MASK;
  1343. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1344. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1345. ioc->name, __FILE__, __LINE__, __func__);
  1346. return;
  1347. }
  1348. flags = le16_to_cpu(sas_device_pg0.Flags);
  1349. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  1350. sdev_printk(KERN_INFO, sdev,
  1351. "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
  1352. "sw_preserve(%s)\n",
  1353. (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
  1354. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
  1355. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
  1356. "n",
  1357. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
  1358. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
  1359. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
  1360. }
  1361. /**
  1362. * _scsih_is_raid - return boolean indicating device is raid volume
  1363. * @dev the device struct object
  1364. */
  1365. static int
  1366. _scsih_is_raid(struct device *dev)
  1367. {
  1368. struct scsi_device *sdev = to_scsi_device(dev);
  1369. struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1370. if (ioc->is_warpdrive)
  1371. return 0;
  1372. return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
  1373. }
  1374. /**
  1375. * _scsih_get_resync - get raid volume resync percent complete
  1376. * @dev the device struct object
  1377. */
  1378. static void
  1379. _scsih_get_resync(struct device *dev)
  1380. {
  1381. struct scsi_device *sdev = to_scsi_device(dev);
  1382. struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1383. static struct _raid_device *raid_device;
  1384. unsigned long flags;
  1385. Mpi2RaidVolPage0_t vol_pg0;
  1386. Mpi2ConfigReply_t mpi_reply;
  1387. u32 volume_status_flags;
  1388. u8 percent_complete;
  1389. u16 handle;
  1390. percent_complete = 0;
  1391. handle = 0;
  1392. if (ioc->is_warpdrive)
  1393. goto out;
  1394. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1395. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1396. sdev->channel);
  1397. if (raid_device) {
  1398. handle = raid_device->handle;
  1399. percent_complete = raid_device->percent_complete;
  1400. }
  1401. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1402. if (!handle)
  1403. goto out;
  1404. if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1405. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  1406. sizeof(Mpi2RaidVolPage0_t))) {
  1407. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1408. ioc->name, __FILE__, __LINE__, __func__);
  1409. percent_complete = 0;
  1410. goto out;
  1411. }
  1412. volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1413. if (!(volume_status_flags &
  1414. MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
  1415. percent_complete = 0;
  1416. out:
  1417. raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
  1418. }
  1419. /**
  1420. * _scsih_get_state - get raid volume level
  1421. * @dev the device struct object
  1422. */
  1423. static void
  1424. _scsih_get_state(struct device *dev)
  1425. {
  1426. struct scsi_device *sdev = to_scsi_device(dev);
  1427. struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1428. static struct _raid_device *raid_device;
  1429. unsigned long flags;
  1430. Mpi2RaidVolPage0_t vol_pg0;
  1431. Mpi2ConfigReply_t mpi_reply;
  1432. u32 volstate;
  1433. enum raid_state state = RAID_STATE_UNKNOWN;
  1434. u16 handle = 0;
  1435. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1436. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1437. sdev->channel);
  1438. if (raid_device)
  1439. handle = raid_device->handle;
  1440. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1441. if (!raid_device)
  1442. goto out;
  1443. if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1444. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  1445. sizeof(Mpi2RaidVolPage0_t))) {
  1446. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1447. ioc->name, __FILE__, __LINE__, __func__);
  1448. goto out;
  1449. }
  1450. volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1451. if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
  1452. state = RAID_STATE_RESYNCING;
  1453. goto out;
  1454. }
  1455. switch (vol_pg0.VolumeState) {
  1456. case MPI2_RAID_VOL_STATE_OPTIMAL:
  1457. case MPI2_RAID_VOL_STATE_ONLINE:
  1458. state = RAID_STATE_ACTIVE;
  1459. break;
  1460. case MPI2_RAID_VOL_STATE_DEGRADED:
  1461. state = RAID_STATE_DEGRADED;
  1462. break;
  1463. case MPI2_RAID_VOL_STATE_FAILED:
  1464. case MPI2_RAID_VOL_STATE_MISSING:
  1465. state = RAID_STATE_OFFLINE;
  1466. break;
  1467. }
  1468. out:
  1469. raid_set_state(mpt2sas_raid_template, dev, state);
  1470. }
  1471. /**
  1472. * _scsih_set_level - set raid level
  1473. * @sdev: scsi device struct
  1474. * @volume_type: volume type
  1475. */
  1476. static void
  1477. _scsih_set_level(struct scsi_device *sdev, u8 volume_type)
  1478. {
  1479. enum raid_level level = RAID_LEVEL_UNKNOWN;
  1480. switch (volume_type) {
  1481. case MPI2_RAID_VOL_TYPE_RAID0:
  1482. level = RAID_LEVEL_0;
  1483. break;
  1484. case MPI2_RAID_VOL_TYPE_RAID10:
  1485. level = RAID_LEVEL_10;
  1486. break;
  1487. case MPI2_RAID_VOL_TYPE_RAID1E:
  1488. level = RAID_LEVEL_1E;
  1489. break;
  1490. case MPI2_RAID_VOL_TYPE_RAID1:
  1491. level = RAID_LEVEL_1;
  1492. break;
  1493. }
  1494. raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
  1495. }
  1496. /**
  1497. * _scsih_get_volume_capabilities - volume capabilities
  1498. * @ioc: per adapter object
  1499. * @sas_device: the raid_device object
  1500. *
  1501. * Returns 0 for success, else 1
  1502. */
  1503. static int
  1504. _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1505. struct _raid_device *raid_device)
  1506. {
  1507. Mpi2RaidVolPage0_t *vol_pg0;
  1508. Mpi2RaidPhysDiskPage0_t pd_pg0;
  1509. Mpi2SasDevicePage0_t sas_device_pg0;
  1510. Mpi2ConfigReply_t mpi_reply;
  1511. u16 sz;
  1512. u8 num_pds;
  1513. if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
  1514. &num_pds)) || !num_pds) {
  1515. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1516. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1517. __func__));
  1518. return 1;
  1519. }
  1520. raid_device->num_pds = num_pds;
  1521. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  1522. sizeof(Mpi2RaidVol0PhysDisk_t));
  1523. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  1524. if (!vol_pg0) {
  1525. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1526. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1527. __func__));
  1528. return 1;
  1529. }
  1530. if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  1531. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  1532. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1533. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1534. __func__));
  1535. kfree(vol_pg0);
  1536. return 1;
  1537. }
  1538. raid_device->volume_type = vol_pg0->VolumeType;
  1539. /* figure out what the underlying devices are by
  1540. * obtaining the device_info bits for the 1st device
  1541. */
  1542. if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  1543. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  1544. vol_pg0->PhysDisk[0].PhysDiskNum))) {
  1545. if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  1546. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  1547. le16_to_cpu(pd_pg0.DevHandle)))) {
  1548. raid_device->device_info =
  1549. le32_to_cpu(sas_device_pg0.DeviceInfo);
  1550. }
  1551. }
  1552. kfree(vol_pg0);
  1553. return 0;
  1554. }
  1555. /**
  1556. * _scsih_disable_ddio - Disable direct I/O for all the volumes
  1557. * @ioc: per adapter object
  1558. */
  1559. static void
  1560. _scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
  1561. {
  1562. Mpi2RaidVolPage1_t vol_pg1;
  1563. Mpi2ConfigReply_t mpi_reply;
  1564. struct _raid_device *raid_device;
  1565. u16 handle;
  1566. u16 ioc_status;
  1567. unsigned long flags;
  1568. handle = 0xFFFF;
  1569. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  1570. &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  1571. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1572. MPI2_IOCSTATUS_MASK;
  1573. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  1574. break;
  1575. handle = le16_to_cpu(vol_pg1.DevHandle);
  1576. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1577. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  1578. if (raid_device)
  1579. raid_device->direct_io_enabled = 0;
  1580. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1581. }
  1582. return;
  1583. }
  1584. /**
  1585. * _scsih_get_num_volumes - Get number of volumes in the ioc
  1586. * @ioc: per adapter object
  1587. */
  1588. static u8
  1589. _scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
  1590. {
  1591. Mpi2RaidVolPage1_t vol_pg1;
  1592. Mpi2ConfigReply_t mpi_reply;
  1593. u16 handle;
  1594. u8 vol_cnt = 0;
  1595. u16 ioc_status;
  1596. handle = 0xFFFF;
  1597. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  1598. &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  1599. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1600. MPI2_IOCSTATUS_MASK;
  1601. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  1602. break;
  1603. vol_cnt++;
  1604. handle = le16_to_cpu(vol_pg1.DevHandle);
  1605. }
  1606. return vol_cnt;
  1607. }
  1608. /**
  1609. * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
  1610. * @ioc: per adapter object
  1611. * @raid_device: the raid_device object
  1612. */
  1613. static void
  1614. _scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
  1615. struct _raid_device *raid_device)
  1616. {
  1617. Mpi2RaidVolPage0_t *vol_pg0;
  1618. Mpi2RaidPhysDiskPage0_t pd_pg0;
  1619. Mpi2ConfigReply_t mpi_reply;
  1620. u16 sz;
  1621. u8 num_pds, count;
  1622. unsigned long stripe_sz, block_sz;
  1623. u8 stripe_exp, block_exp;
  1624. u64 dev_max_lba;
  1625. if (!ioc->is_warpdrive)
  1626. return;
  1627. if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
  1628. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1629. "globally as drives are exposed\n", ioc->name);
  1630. return;
  1631. }
  1632. if (_scsih_get_num_volumes(ioc) > 1) {
  1633. _scsih_disable_ddio(ioc);
  1634. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1635. "globally as number of drives > 1\n", ioc->name);
  1636. return;
  1637. }
  1638. if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
  1639. &num_pds)) || !num_pds) {
  1640. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1641. "Failure in computing number of drives\n", ioc->name);
  1642. return;
  1643. }
  1644. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  1645. sizeof(Mpi2RaidVol0PhysDisk_t));
  1646. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  1647. if (!vol_pg0) {
  1648. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1649. "Memory allocation failure for RVPG0\n", ioc->name);
  1650. return;
  1651. }
  1652. if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  1653. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  1654. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1655. "Failure in retrieving RVPG0\n", ioc->name);
  1656. kfree(vol_pg0);
  1657. return;
  1658. }
  1659. /*
  1660. * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
  1661. * assumed for WARPDRIVE, disable direct I/O
  1662. */
  1663. if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
  1664. printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
  1665. "for the drive with handle(0x%04x): num_mem=%d, "
  1666. "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
  1667. num_pds, MPT_MAX_WARPDRIVE_PDS);
  1668. kfree(vol_pg0);
  1669. return;
  1670. }
  1671. for (count = 0; count < num_pds; count++) {
  1672. if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  1673. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  1674. vol_pg0->PhysDisk[count].PhysDiskNum) ||
  1675. le16_to_cpu(pd_pg0.DevHandle) ==
  1676. MPT2SAS_INVALID_DEVICE_HANDLE) {
  1677. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
  1678. "disabled for the drive with handle(0x%04x) member"
  1679. "handle retrieval failed for member number=%d\n",
  1680. ioc->name, raid_device->handle,
  1681. vol_pg0->PhysDisk[count].PhysDiskNum);
  1682. goto out_error;
  1683. }
  1684. /* Disable direct I/O if member drive lba exceeds 4 bytes */
  1685. dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
  1686. if (dev_max_lba >> 32) {
  1687. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
  1688. "disabled for the drive with handle(0x%04x) member"
  1689. "handle (0x%04x) unsupported max lba 0x%016llx\n",
  1690. ioc->name, raid_device->handle,
  1691. le16_to_cpu(pd_pg0.DevHandle),
  1692. (unsigned long long)dev_max_lba);
  1693. goto out_error;
  1694. }
  1695. raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
  1696. }
  1697. /*
  1698. * Assumption for WD: Direct I/O is not supported if the volume is
  1699. * not RAID0
  1700. */
  1701. if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
  1702. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1703. "for the drive with handle(0x%04x): type=%d, "
  1704. "s_sz=%uK, blk_size=%u\n", ioc->name,
  1705. raid_device->handle, raid_device->volume_type,
  1706. (le32_to_cpu(vol_pg0->StripeSize) *
  1707. le16_to_cpu(vol_pg0->BlockSize)) / 1024,
  1708. le16_to_cpu(vol_pg0->BlockSize));
  1709. goto out_error;
  1710. }
  1711. stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
  1712. stripe_exp = find_first_bit(&stripe_sz, 32);
  1713. if (stripe_exp == 32) {
  1714. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1715. "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
  1716. ioc->name, raid_device->handle,
  1717. (le32_to_cpu(vol_pg0->StripeSize) *
  1718. le16_to_cpu(vol_pg0->BlockSize)) / 1024);
  1719. goto out_error;
  1720. }
  1721. raid_device->stripe_exponent = stripe_exp;
  1722. block_sz = le16_to_cpu(vol_pg0->BlockSize);
  1723. block_exp = find_first_bit(&block_sz, 16);
  1724. if (block_exp == 16) {
  1725. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
  1726. "for the drive with handle(0x%04x) invalid block sz %u\n",
  1727. ioc->name, raid_device->handle,
  1728. le16_to_cpu(vol_pg0->BlockSize));
  1729. goto out_error;
  1730. }
  1731. raid_device->block_exponent = block_exp;
  1732. raid_device->direct_io_enabled = 1;
  1733. printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
  1734. " with handle(0x%04x)\n", ioc->name, raid_device->handle);
  1735. /*
  1736. * WARPDRIVE: Though the following fields are not used for direct IO,
  1737. * stored for future purpose:
  1738. */
  1739. raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
  1740. raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
  1741. raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
  1742. kfree(vol_pg0);
  1743. return;
  1744. out_error:
  1745. raid_device->direct_io_enabled = 0;
  1746. for (count = 0; count < num_pds; count++)
  1747. raid_device->pd_handle[count] = 0;
  1748. kfree(vol_pg0);
  1749. return;
  1750. }
  1751. /**
  1752. * _scsih_enable_tlr - setting TLR flags
  1753. * @ioc: per adapter object
  1754. * @sdev: scsi device struct
  1755. *
  1756. * Enabling Transaction Layer Retries for tape devices when
  1757. * vpd page 0x90 is present
  1758. *
  1759. */
  1760. static void
  1761. _scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
  1762. {
  1763. /* only for TAPE */
  1764. if (sdev->type != TYPE_TAPE)
  1765. return;
  1766. if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
  1767. return;
  1768. sas_enable_tlr(sdev);
  1769. sdev_printk(KERN_INFO, sdev, "TLR %s\n",
  1770. sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
  1771. return;
  1772. }
  1773. /**
  1774. * _scsih_slave_configure - device configure routine.
  1775. * @sdev: scsi device struct
  1776. *
  1777. * Returns 0 if ok. Any other return is assumed to be an error and
  1778. * the device is ignored.
  1779. */
  1780. static int
  1781. _scsih_slave_configure(struct scsi_device *sdev)
  1782. {
  1783. struct Scsi_Host *shost = sdev->host;
  1784. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1785. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1786. struct MPT2SAS_TARGET *sas_target_priv_data;
  1787. struct _sas_device *sas_device;
  1788. struct _raid_device *raid_device;
  1789. unsigned long flags;
  1790. int qdepth;
  1791. u8 ssp_target = 0;
  1792. char *ds = "";
  1793. char *r_level = "";
  1794. u16 handle, volume_handle = 0;
  1795. u64 volume_wwid = 0;
  1796. qdepth = 1;
  1797. sas_device_priv_data = sdev->hostdata;
  1798. sas_device_priv_data->configured_lun = 1;
  1799. sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
  1800. sas_target_priv_data = sas_device_priv_data->sas_target;
  1801. handle = sas_target_priv_data->handle;
  1802. /* raid volume handling */
  1803. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
  1804. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1805. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  1806. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1807. if (!raid_device) {
  1808. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1809. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  1810. __LINE__, __func__));
  1811. return 1;
  1812. }
  1813. if (_scsih_get_volume_capabilities(ioc, raid_device)) {
  1814. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1815. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  1816. __LINE__, __func__));
  1817. return 1;
  1818. }
  1819. /*
  1820. * WARPDRIVE: Initialize the required data for Direct IO
  1821. */
  1822. _scsih_init_warpdrive_properties(ioc, raid_device);
  1823. /* RAID Queue Depth Support
  1824. * IS volume = underlying qdepth of drive type, either
  1825. * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
  1826. * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
  1827. */
  1828. if (raid_device->device_info &
  1829. MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1830. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1831. ds = "SSP";
  1832. } else {
  1833. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1834. if (raid_device->device_info &
  1835. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1836. ds = "SATA";
  1837. else
  1838. ds = "STP";
  1839. }
  1840. switch (raid_device->volume_type) {
  1841. case MPI2_RAID_VOL_TYPE_RAID0:
  1842. r_level = "RAID0";
  1843. break;
  1844. case MPI2_RAID_VOL_TYPE_RAID1E:
  1845. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1846. if (ioc->manu_pg10.OEMIdentifier &&
  1847. (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
  1848. MFG10_GF0_R10_DISPLAY) &&
  1849. !(raid_device->num_pds % 2))
  1850. r_level = "RAID10";
  1851. else
  1852. r_level = "RAID1E";
  1853. break;
  1854. case MPI2_RAID_VOL_TYPE_RAID1:
  1855. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1856. r_level = "RAID1";
  1857. break;
  1858. case MPI2_RAID_VOL_TYPE_RAID10:
  1859. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1860. r_level = "RAID10";
  1861. break;
  1862. case MPI2_RAID_VOL_TYPE_UNKNOWN:
  1863. default:
  1864. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1865. r_level = "RAIDX";
  1866. break;
  1867. }
  1868. if (!ioc->hide_ir_msg)
  1869. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
  1870. "wwid(0x%016llx), pd_count(%d), type(%s)\n",
  1871. r_level, raid_device->handle,
  1872. (unsigned long long)raid_device->wwid,
  1873. raid_device->num_pds, ds);
  1874. _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
  1875. /* raid transport support */
  1876. if (!ioc->is_warpdrive)
  1877. _scsih_set_level(sdev, raid_device->volume_type);
  1878. return 0;
  1879. }
  1880. /* non-raid handling */
  1881. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1882. if (mpt2sas_config_get_volume_handle(ioc, handle,
  1883. &volume_handle)) {
  1884. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1885. "failure at %s:%d/%s()!\n", ioc->name,
  1886. __FILE__, __LINE__, __func__));
  1887. return 1;
  1888. }
  1889. if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
  1890. volume_handle, &volume_wwid)) {
  1891. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1892. "failure at %s:%d/%s()!\n", ioc->name,
  1893. __FILE__, __LINE__, __func__));
  1894. return 1;
  1895. }
  1896. }
  1897. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1898. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1899. sas_device_priv_data->sas_target->sas_address);
  1900. if (!sas_device) {
  1901. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1902. dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
  1903. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  1904. __LINE__, __func__));
  1905. return 1;
  1906. }
  1907. sas_device->volume_handle = volume_handle;
  1908. sas_device->volume_wwid = volume_wwid;
  1909. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1910. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1911. ssp_target = 1;
  1912. ds = "SSP";
  1913. } else {
  1914. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1915. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  1916. ds = "STP";
  1917. else if (sas_device->device_info &
  1918. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1919. ds = "SATA";
  1920. }
  1921. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
  1922. "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
  1923. ds, sas_device->handle,
  1924. (unsigned long long)sas_device->sas_address,
  1925. sas_device->phy,
  1926. (unsigned long long)sas_device->device_name);
  1927. sdev_printk(KERN_INFO, sdev, "%s: "
  1928. "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
  1929. (unsigned long long) sas_device->enclosure_logical_id,
  1930. sas_device->slot);
  1931. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1932. if (!ssp_target)
  1933. _scsih_display_sata_capabilities(ioc, handle, sdev);
  1934. _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
  1935. if (ssp_target) {
  1936. sas_read_port_mode_page(sdev);
  1937. _scsih_enable_tlr(ioc, sdev);
  1938. }
  1939. return 0;
  1940. }
  1941. /**
  1942. * _scsih_bios_param - fetch head, sector, cylinder info for a disk
  1943. * @sdev: scsi device struct
  1944. * @bdev: pointer to block device context
  1945. * @capacity: device size (in 512 byte sectors)
  1946. * @params: three element array to place output:
  1947. * params[0] number of heads (max 255)
  1948. * params[1] number of sectors (max 63)
  1949. * params[2] number of cylinders
  1950. *
  1951. * Return nothing.
  1952. */
  1953. static int
  1954. _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1955. sector_t capacity, int params[])
  1956. {
  1957. int heads;
  1958. int sectors;
  1959. sector_t cylinders;
  1960. ulong dummy;
  1961. heads = 64;
  1962. sectors = 32;
  1963. dummy = heads * sectors;
  1964. cylinders = capacity;
  1965. sector_div(cylinders, dummy);
  1966. /*
  1967. * Handle extended translation size for logical drives
  1968. * > 1Gb
  1969. */
  1970. if ((ulong)capacity >= 0x200000) {
  1971. heads = 255;
  1972. sectors = 63;
  1973. dummy = heads * sectors;
  1974. cylinders = capacity;
  1975. sector_div(cylinders, dummy);
  1976. }
  1977. /* return result */
  1978. params[0] = heads;
  1979. params[1] = sectors;
  1980. params[2] = cylinders;
  1981. return 0;
  1982. }
  1983. /**
  1984. * _scsih_response_code - translation of device response code
  1985. * @ioc: per adapter object
  1986. * @response_code: response code returned by the device
  1987. *
  1988. * Return nothing.
  1989. */
  1990. static void
  1991. _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
  1992. {
  1993. char *desc;
  1994. switch (response_code) {
  1995. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  1996. desc = "task management request completed";
  1997. break;
  1998. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  1999. desc = "invalid frame";
  2000. break;
  2001. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  2002. desc = "task management request not supported";
  2003. break;
  2004. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  2005. desc = "task management request failed";
  2006. break;
  2007. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  2008. desc = "task management request succeeded";
  2009. break;
  2010. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  2011. desc = "invalid lun";
  2012. break;
  2013. case 0xA:
  2014. desc = "overlapped tag attempted";
  2015. break;
  2016. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  2017. desc = "task queued, however not sent to target";
  2018. break;
  2019. default:
  2020. desc = "unknown";
  2021. break;
  2022. }
  2023. printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
  2024. ioc->name, response_code, desc);
  2025. }
  2026. /**
  2027. * _scsih_tm_done - tm completion routine
  2028. * @ioc: per adapter object
  2029. * @smid: system request message index
  2030. * @msix_index: MSIX table index supplied by the OS
  2031. * @reply: reply message frame(lower 32bit addr)
  2032. * Context: none.
  2033. *
  2034. * The callback handler when using scsih_issue_tm.
  2035. *
  2036. * Return 1 meaning mf should be freed from _base_interrupt
  2037. * 0 means the mf is freed from this function.
  2038. */
  2039. static u8
  2040. _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  2041. {
  2042. MPI2DefaultReply_t *mpi_reply;
  2043. if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
  2044. return 1;
  2045. if (ioc->tm_cmds.smid != smid)
  2046. return 1;
  2047. mpt2sas_base_flush_reply_queues(ioc);
  2048. ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
  2049. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2050. if (mpi_reply) {
  2051. memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  2052. ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
  2053. }
  2054. ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
  2055. complete(&ioc->tm_cmds.done);
  2056. return 1;
  2057. }
  2058. /**
  2059. * mpt2sas_scsih_set_tm_flag - set per target tm_busy
  2060. * @ioc: per adapter object
  2061. * @handle: device handle
  2062. *
  2063. * During taskmangement request, we need to freeze the device queue.
  2064. */
  2065. void
  2066. mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2067. {
  2068. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2069. struct scsi_device *sdev;
  2070. u8 skip = 0;
  2071. shost_for_each_device(sdev, ioc->shost) {
  2072. if (skip)
  2073. continue;
  2074. sas_device_priv_data = sdev->hostdata;
  2075. if (!sas_device_priv_data)
  2076. continue;
  2077. if (sas_device_priv_data->sas_target->handle == handle) {
  2078. sas_device_priv_data->sas_target->tm_busy = 1;
  2079. skip = 1;
  2080. ioc->ignore_loginfos = 1;
  2081. }
  2082. }
  2083. }
  2084. /**
  2085. * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
  2086. * @ioc: per adapter object
  2087. * @handle: device handle
  2088. *
  2089. * During taskmangement request, we need to freeze the device queue.
  2090. */
  2091. void
  2092. mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2093. {
  2094. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2095. struct scsi_device *sdev;
  2096. u8 skip = 0;
  2097. shost_for_each_device(sdev, ioc->shost) {
  2098. if (skip)
  2099. continue;
  2100. sas_device_priv_data = sdev->hostdata;
  2101. if (!sas_device_priv_data)
  2102. continue;
  2103. if (sas_device_priv_data->sas_target->handle == handle) {
  2104. sas_device_priv_data->sas_target->tm_busy = 0;
  2105. skip = 1;
  2106. ioc->ignore_loginfos = 0;
  2107. }
  2108. }
  2109. }
  2110. /**
  2111. * mpt2sas_scsih_issue_tm - main routine for sending tm requests
  2112. * @ioc: per adapter struct
  2113. * @device_handle: device handle
  2114. * @channel: the channel assigned by the OS
  2115. * @id: the id assigned by the OS
  2116. * @lun: lun number
  2117. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
  2118. * @smid_task: smid assigned to the task
  2119. * @timeout: timeout in seconds
  2120. * @serial_number: the serial_number from scmd
  2121. * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
  2122. * Context: user
  2123. *
  2124. * A generic API for sending task management requests to firmware.
  2125. *
  2126. * The callback index is set inside `ioc->tm_cb_idx`.
  2127. *
  2128. * Return SUCCESS or FAILED.
  2129. */
  2130. int
  2131. mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
  2132. uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
  2133. unsigned long serial_number, enum mutex_type m_type)
  2134. {
  2135. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2136. Mpi2SCSITaskManagementReply_t *mpi_reply;
  2137. u16 smid = 0;
  2138. u32 ioc_state;
  2139. unsigned long timeleft;
  2140. struct scsiio_tracker *scsi_lookup = NULL;
  2141. int rc;
  2142. if (m_type == TM_MUTEX_ON)
  2143. mutex_lock(&ioc->tm_cmds.mutex);
  2144. if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
  2145. printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
  2146. __func__, ioc->name);
  2147. rc = FAILED;
  2148. goto err_out;
  2149. }
  2150. if (ioc->shost_recovery || ioc->remove_host ||
  2151. ioc->pci_error_recovery) {
  2152. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  2153. __func__, ioc->name);
  2154. rc = FAILED;
  2155. goto err_out;
  2156. }
  2157. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  2158. if (ioc_state & MPI2_DOORBELL_USED) {
  2159. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
  2160. "active!\n", ioc->name));
  2161. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2162. FORCE_BIG_HAMMER);
  2163. rc = (!rc) ? SUCCESS : FAILED;
  2164. goto err_out;
  2165. }
  2166. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  2167. mpt2sas_base_fault_info(ioc, ioc_state &
  2168. MPI2_DOORBELL_DATA_MASK);
  2169. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2170. FORCE_BIG_HAMMER);
  2171. rc = (!rc) ? SUCCESS : FAILED;
  2172. goto err_out;
  2173. }
  2174. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
  2175. if (!smid) {
  2176. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2177. ioc->name, __func__);
  2178. rc = FAILED;
  2179. goto err_out;
  2180. }
  2181. if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  2182. scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
  2183. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
  2184. " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
  2185. smid_task));
  2186. ioc->tm_cmds.status = MPT2_CMD_PENDING;
  2187. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2188. ioc->tm_cmds.smid = smid;
  2189. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2190. memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
  2191. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2192. mpi_request->DevHandle = cpu_to_le16(handle);
  2193. mpi_request->TaskType = type;
  2194. mpi_request->TaskMID = cpu_to_le16(smid_task);
  2195. int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
  2196. mpt2sas_scsih_set_tm_flag(ioc, handle);
  2197. init_completion(&ioc->tm_cmds.done);
  2198. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  2199. timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
  2200. if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
  2201. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2202. ioc->name, __func__);
  2203. _debug_dump_mf(mpi_request,
  2204. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  2205. if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
  2206. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2207. FORCE_BIG_HAMMER);
  2208. rc = (!rc) ? SUCCESS : FAILED;
  2209. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  2210. mpt2sas_scsih_clear_tm_flag(ioc, handle);
  2211. goto err_out;
  2212. }
  2213. }
  2214. if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
  2215. mpi_reply = ioc->tm_cmds.reply;
  2216. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
  2217. "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
  2218. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  2219. le32_to_cpu(mpi_reply->IOCLogInfo),
  2220. le32_to_cpu(mpi_reply->TerminationCount)));
  2221. if (ioc->logging_level & MPT_DEBUG_TM) {
  2222. _scsih_response_code(ioc, mpi_reply->ResponseCode);
  2223. if (mpi_reply->IOCStatus)
  2224. _debug_dump_mf(mpi_request,
  2225. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  2226. }
  2227. }
  2228. switch (type) {
  2229. case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  2230. rc = SUCCESS;
  2231. if (scsi_lookup->scmd == NULL)
  2232. break;
  2233. rc = FAILED;
  2234. break;
  2235. case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  2236. if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
  2237. rc = FAILED;
  2238. else
  2239. rc = SUCCESS;
  2240. break;
  2241. case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
  2242. case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
  2243. if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
  2244. rc = FAILED;
  2245. else
  2246. rc = SUCCESS;
  2247. break;
  2248. case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
  2249. rc = SUCCESS;
  2250. break;
  2251. default:
  2252. rc = FAILED;
  2253. break;
  2254. }
  2255. mpt2sas_scsih_clear_tm_flag(ioc, handle);
  2256. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  2257. if (m_type == TM_MUTEX_ON)
  2258. mutex_unlock(&ioc->tm_cmds.mutex);
  2259. return rc;
  2260. err_out:
  2261. if (m_type == TM_MUTEX_ON)
  2262. mutex_unlock(&ioc->tm_cmds.mutex);
  2263. return rc;
  2264. }
  2265. /**
  2266. * _scsih_tm_display_info - displays info about the device
  2267. * @ioc: per adapter struct
  2268. * @scmd: pointer to scsi command object
  2269. *
  2270. * Called by task management callback handlers.
  2271. */
  2272. static void
  2273. _scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
  2274. {
  2275. struct scsi_target *starget = scmd->device->sdev_target;
  2276. struct MPT2SAS_TARGET *priv_target = starget->hostdata;
  2277. struct _sas_device *sas_device = NULL;
  2278. unsigned long flags;
  2279. char *device_str = NULL;
  2280. if (!priv_target)
  2281. return;
  2282. if (ioc->hide_ir_msg)
  2283. device_str = "WarpDrive";
  2284. else
  2285. device_str = "volume";
  2286. scsi_print_command(scmd);
  2287. if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  2288. starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
  2289. "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
  2290. device_str, (unsigned long long)priv_target->sas_address);
  2291. } else {
  2292. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2293. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  2294. priv_target->sas_address);
  2295. if (sas_device) {
  2296. if (priv_target->flags &
  2297. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2298. starget_printk(KERN_INFO, starget,
  2299. "volume handle(0x%04x), "
  2300. "volume wwid(0x%016llx)\n",
  2301. sas_device->volume_handle,
  2302. (unsigned long long)sas_device->volume_wwid);
  2303. }
  2304. starget_printk(KERN_INFO, starget,
  2305. "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
  2306. sas_device->handle,
  2307. (unsigned long long)sas_device->sas_address,
  2308. sas_device->phy);
  2309. starget_printk(KERN_INFO, starget,
  2310. "enclosure_logical_id(0x%016llx), slot(%d)\n",
  2311. (unsigned long long)sas_device->enclosure_logical_id,
  2312. sas_device->slot);
  2313. }
  2314. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2315. }
  2316. }
  2317. /**
  2318. * _scsih_abort - eh threads main abort routine
  2319. * @scmd: pointer to scsi command object
  2320. *
  2321. * Returns SUCCESS if command aborted else FAILED
  2322. */
  2323. static int
  2324. _scsih_abort(struct scsi_cmnd *scmd)
  2325. {
  2326. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2327. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2328. u16 smid;
  2329. u16 handle;
  2330. int r;
  2331. sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
  2332. "scmd(%p)\n", scmd);
  2333. _scsih_tm_display_info(ioc, scmd);
  2334. sas_device_priv_data = scmd->device->hostdata;
  2335. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2336. sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
  2337. "scmd(%p)\n", scmd);
  2338. scmd->result = DID_NO_CONNECT << 16;
  2339. scmd->scsi_done(scmd);
  2340. r = SUCCESS;
  2341. goto out;
  2342. }
  2343. /* search for the command */
  2344. smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
  2345. if (!smid) {
  2346. scmd->result = DID_RESET << 16;
  2347. r = SUCCESS;
  2348. goto out;
  2349. }
  2350. /* for hidden raid components and volumes this is not supported */
  2351. if (sas_device_priv_data->sas_target->flags &
  2352. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  2353. sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  2354. scmd->result = DID_RESET << 16;
  2355. r = FAILED;
  2356. goto out;
  2357. }
  2358. mpt2sas_halt_firmware(ioc);
  2359. handle = sas_device_priv_data->sas_target->handle;
  2360. r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
  2361. scmd->device->id, scmd->device->lun,
  2362. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
  2363. scmd->serial_number, TM_MUTEX_ON);
  2364. out:
  2365. sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
  2366. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2367. return r;
  2368. }
  2369. /**
  2370. * _scsih_dev_reset - eh threads main device reset routine
  2371. * @scmd: pointer to scsi command object
  2372. *
  2373. * Returns SUCCESS if command aborted else FAILED
  2374. */
  2375. static int
  2376. _scsih_dev_reset(struct scsi_cmnd *scmd)
  2377. {
  2378. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2379. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2380. struct _sas_device *sas_device;
  2381. unsigned long flags;
  2382. u16 handle;
  2383. int r;
  2384. struct scsi_target *starget = scmd->device->sdev_target;
  2385. starget_printk(KERN_INFO, starget, "attempting device reset! "
  2386. "scmd(%p)\n", scmd);
  2387. _scsih_tm_display_info(ioc, scmd);
  2388. sas_device_priv_data = scmd->device->hostdata;
  2389. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2390. starget_printk(KERN_INFO, starget, "device been deleted! "
  2391. "scmd(%p)\n", scmd);
  2392. scmd->result = DID_NO_CONNECT << 16;
  2393. scmd->scsi_done(scmd);
  2394. r = SUCCESS;
  2395. goto out;
  2396. }
  2397. /* for hidden raid components obtain the volume_handle */
  2398. handle = 0;
  2399. if (sas_device_priv_data->sas_target->flags &
  2400. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2401. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2402. sas_device = _scsih_sas_device_find_by_handle(ioc,
  2403. sas_device_priv_data->sas_target->handle);
  2404. if (sas_device)
  2405. handle = sas_device->volume_handle;
  2406. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2407. } else
  2408. handle = sas_device_priv_data->sas_target->handle;
  2409. if (!handle) {
  2410. scmd->result = DID_RESET << 16;
  2411. r = FAILED;
  2412. goto out;
  2413. }
  2414. r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
  2415. scmd->device->id, scmd->device->lun,
  2416. MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
  2417. TM_MUTEX_ON);
  2418. out:
  2419. sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
  2420. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2421. return r;
  2422. }
  2423. /**
  2424. * _scsih_target_reset - eh threads main target reset routine
  2425. * @scmd: pointer to scsi command object
  2426. *
  2427. * Returns SUCCESS if command aborted else FAILED
  2428. */
  2429. static int
  2430. _scsih_target_reset(struct scsi_cmnd *scmd)
  2431. {
  2432. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2433. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2434. struct _sas_device *sas_device;
  2435. unsigned long flags;
  2436. u16 handle;
  2437. int r;
  2438. struct scsi_target *starget = scmd->device->sdev_target;
  2439. starget_printk(KERN_INFO, starget, "attempting target reset! "
  2440. "scmd(%p)\n", scmd);
  2441. _scsih_tm_display_info(ioc, scmd);
  2442. sas_device_priv_data = scmd->device->hostdata;
  2443. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2444. starget_printk(KERN_INFO, starget, "target been deleted! "
  2445. "scmd(%p)\n", scmd);
  2446. scmd->result = DID_NO_CONNECT << 16;
  2447. scmd->scsi_done(scmd);
  2448. r = SUCCESS;
  2449. goto out;
  2450. }
  2451. /* for hidden raid components obtain the volume_handle */
  2452. handle = 0;
  2453. if (sas_device_priv_data->sas_target->flags &
  2454. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2455. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2456. sas_device = _scsih_sas_device_find_by_handle(ioc,
  2457. sas_device_priv_data->sas_target->handle);
  2458. if (sas_device)
  2459. handle = sas_device->volume_handle;
  2460. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2461. } else
  2462. handle = sas_device_priv_data->sas_target->handle;
  2463. if (!handle) {
  2464. scmd->result = DID_RESET << 16;
  2465. r = FAILED;
  2466. goto out;
  2467. }
  2468. r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
  2469. scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  2470. 30, 0, TM_MUTEX_ON);
  2471. out:
  2472. starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
  2473. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2474. return r;
  2475. }
  2476. /**
  2477. * _scsih_host_reset - eh threads main host reset routine
  2478. * @scmd: pointer to scsi command object
  2479. *
  2480. * Returns SUCCESS if command aborted else FAILED
  2481. */
  2482. static int
  2483. _scsih_host_reset(struct scsi_cmnd *scmd)
  2484. {
  2485. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2486. int r, retval;
  2487. printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
  2488. ioc->name, scmd);
  2489. scsi_print_command(scmd);
  2490. retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2491. FORCE_BIG_HAMMER);
  2492. r = (retval < 0) ? FAILED : SUCCESS;
  2493. printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
  2494. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2495. return r;
  2496. }
  2497. /**
  2498. * _scsih_fw_event_add - insert and queue up fw_event
  2499. * @ioc: per adapter object
  2500. * @fw_event: object describing the event
  2501. * Context: This function will acquire ioc->fw_event_lock.
  2502. *
  2503. * This adds the firmware event object into link list, then queues it up to
  2504. * be processed from user context.
  2505. *
  2506. * Return nothing.
  2507. */
  2508. static void
  2509. _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  2510. {
  2511. unsigned long flags;
  2512. if (ioc->firmware_event_thread == NULL)
  2513. return;
  2514. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2515. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  2516. INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
  2517. queue_delayed_work(ioc->firmware_event_thread,
  2518. &fw_event->delayed_work, 0);
  2519. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2520. }
  2521. /**
  2522. * _scsih_fw_event_free - delete fw_event
  2523. * @ioc: per adapter object
  2524. * @fw_event: object describing the event
  2525. * Context: This function will acquire ioc->fw_event_lock.
  2526. *
  2527. * This removes firmware event object from link list, frees associated memory.
  2528. *
  2529. * Return nothing.
  2530. */
  2531. static void
  2532. _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  2533. *fw_event)
  2534. {
  2535. unsigned long flags;
  2536. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2537. list_del(&fw_event->list);
  2538. kfree(fw_event->event_data);
  2539. kfree(fw_event);
  2540. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2541. }
  2542. /**
  2543. * _scsih_error_recovery_delete_devices - remove devices not responding
  2544. * @ioc: per adapter object
  2545. *
  2546. * Return nothing.
  2547. */
  2548. static void
  2549. _scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
  2550. {
  2551. struct fw_event_work *fw_event;
  2552. if (ioc->is_driver_loading)
  2553. return;
  2554. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  2555. if (!fw_event)
  2556. return;
  2557. fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
  2558. fw_event->ioc = ioc;
  2559. _scsih_fw_event_add(ioc, fw_event);
  2560. }
  2561. /**
  2562. * mpt2sas_port_enable_complete - port enable completed (fake event)
  2563. * @ioc: per adapter object
  2564. *
  2565. * Return nothing.
  2566. */
  2567. void
  2568. mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
  2569. {
  2570. struct fw_event_work *fw_event;
  2571. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  2572. if (!fw_event)
  2573. return;
  2574. fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
  2575. fw_event->ioc = ioc;
  2576. _scsih_fw_event_add(ioc, fw_event);
  2577. }
  2578. /**
  2579. * _scsih_fw_event_cleanup_queue - cleanup event queue
  2580. * @ioc: per adapter object
  2581. *
  2582. * Walk the firmware event queue, either killing timers, or waiting
  2583. * for outstanding events to complete
  2584. *
  2585. * Return nothing.
  2586. */
  2587. static void
  2588. _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
  2589. {
  2590. struct fw_event_work *fw_event, *next;
  2591. if (list_empty(&ioc->fw_event_list) ||
  2592. !ioc->firmware_event_thread || in_interrupt())
  2593. return;
  2594. list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
  2595. if (cancel_delayed_work(&fw_event->delayed_work)) {
  2596. _scsih_fw_event_free(ioc, fw_event);
  2597. continue;
  2598. }
  2599. fw_event->cancel_pending_work = 1;
  2600. }
  2601. }
  2602. /**
  2603. * _scsih_ublock_io_all_device - unblock every device
  2604. * @ioc: per adapter object
  2605. *
  2606. * change the device state from block to running
  2607. */
  2608. static void
  2609. _scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
  2610. {
  2611. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2612. struct scsi_device *sdev;
  2613. shost_for_each_device(sdev, ioc->shost) {
  2614. sas_device_priv_data = sdev->hostdata;
  2615. if (!sas_device_priv_data)
  2616. continue;
  2617. if (!sas_device_priv_data->block)
  2618. continue;
  2619. sas_device_priv_data->block = 0;
  2620. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
  2621. "handle(0x%04x)\n",
  2622. sas_device_priv_data->sas_target->handle));
  2623. scsi_internal_device_unblock(sdev, SDEV_RUNNING);
  2624. }
  2625. }
  2626. /**
  2627. * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
  2628. * @ioc: per adapter object
  2629. * @handle: device handle
  2630. *
  2631. * During device pull we need to appropiately set the sdev state.
  2632. */
  2633. static void
  2634. _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
  2635. {
  2636. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2637. struct scsi_device *sdev;
  2638. shost_for_each_device(sdev, ioc->shost) {
  2639. sas_device_priv_data = sdev->hostdata;
  2640. if (!sas_device_priv_data)
  2641. continue;
  2642. if (!sas_device_priv_data->block)
  2643. continue;
  2644. if (sas_device_priv_data->sas_target->sas_address ==
  2645. sas_address) {
  2646. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2647. MPT2SAS_INFO_FMT "SDEV_RUNNING: "
  2648. "sas address(0x%016llx)\n", ioc->name,
  2649. (unsigned long long)sas_address));
  2650. sas_device_priv_data->block = 0;
  2651. scsi_internal_device_unblock(sdev, SDEV_RUNNING);
  2652. }
  2653. }
  2654. }
  2655. /**
  2656. * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
  2657. * @ioc: per adapter object
  2658. * @handle: device handle
  2659. *
  2660. * During device pull we need to appropiately set the sdev state.
  2661. */
  2662. static void
  2663. _scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
  2664. {
  2665. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2666. struct scsi_device *sdev;
  2667. shost_for_each_device(sdev, ioc->shost) {
  2668. sas_device_priv_data = sdev->hostdata;
  2669. if (!sas_device_priv_data)
  2670. continue;
  2671. if (sas_device_priv_data->block)
  2672. continue;
  2673. sas_device_priv_data->block = 1;
  2674. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
  2675. "handle(0x%04x)\n",
  2676. sas_device_priv_data->sas_target->handle));
  2677. scsi_internal_device_block(sdev);
  2678. }
  2679. }
  2680. /**
  2681. * _scsih_block_io_device - set the device state to SDEV_BLOCK
  2682. * @ioc: per adapter object
  2683. * @handle: device handle
  2684. *
  2685. * During device pull we need to appropiately set the sdev state.
  2686. */
  2687. static void
  2688. _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2689. {
  2690. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2691. struct scsi_device *sdev;
  2692. shost_for_each_device(sdev, ioc->shost) {
  2693. sas_device_priv_data = sdev->hostdata;
  2694. if (!sas_device_priv_data)
  2695. continue;
  2696. if (sas_device_priv_data->block)
  2697. continue;
  2698. if (sas_device_priv_data->sas_target->handle == handle) {
  2699. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2700. MPT2SAS_INFO_FMT "SDEV_BLOCK: "
  2701. "handle(0x%04x)\n", ioc->name, handle));
  2702. sas_device_priv_data->block = 1;
  2703. scsi_internal_device_block(sdev);
  2704. }
  2705. }
  2706. }
  2707. /**
  2708. * _scsih_block_io_to_children_attached_to_ex
  2709. * @ioc: per adapter object
  2710. * @sas_expander: the sas_device object
  2711. *
  2712. * This routine set sdev state to SDEV_BLOCK for all devices
  2713. * attached to this expander. This function called when expander is
  2714. * pulled.
  2715. */
  2716. static void
  2717. _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
  2718. struct _sas_node *sas_expander)
  2719. {
  2720. struct _sas_port *mpt2sas_port;
  2721. struct _sas_device *sas_device;
  2722. struct _sas_node *expander_sibling;
  2723. unsigned long flags;
  2724. if (!sas_expander)
  2725. return;
  2726. list_for_each_entry(mpt2sas_port,
  2727. &sas_expander->sas_port_list, port_list) {
  2728. if (mpt2sas_port->remote_identify.device_type ==
  2729. SAS_END_DEVICE) {
  2730. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2731. sas_device =
  2732. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  2733. mpt2sas_port->remote_identify.sas_address);
  2734. if (sas_device)
  2735. set_bit(sas_device->handle,
  2736. ioc->blocking_handles);
  2737. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2738. }
  2739. }
  2740. list_for_each_entry(mpt2sas_port,
  2741. &sas_expander->sas_port_list, port_list) {
  2742. if (mpt2sas_port->remote_identify.device_type ==
  2743. SAS_EDGE_EXPANDER_DEVICE ||
  2744. mpt2sas_port->remote_identify.device_type ==
  2745. SAS_FANOUT_EXPANDER_DEVICE) {
  2746. expander_sibling =
  2747. mpt2sas_scsih_expander_find_by_sas_address(
  2748. ioc, mpt2sas_port->remote_identify.sas_address);
  2749. _scsih_block_io_to_children_attached_to_ex(ioc,
  2750. expander_sibling);
  2751. }
  2752. }
  2753. }
  2754. /**
  2755. * _scsih_block_io_to_children_attached_directly
  2756. * @ioc: per adapter object
  2757. * @event_data: topology change event data
  2758. *
  2759. * This routine set sdev state to SDEV_BLOCK for all devices
  2760. * direct attached during device pull.
  2761. */
  2762. static void
  2763. _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
  2764. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2765. {
  2766. int i;
  2767. u16 handle;
  2768. u16 reason_code;
  2769. u8 phy_number;
  2770. for (i = 0; i < event_data->NumEntries; i++) {
  2771. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2772. if (!handle)
  2773. continue;
  2774. phy_number = event_data->StartPhyNum + i;
  2775. reason_code = event_data->PHY[i].PhyStatus &
  2776. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2777. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
  2778. _scsih_block_io_device(ioc, handle);
  2779. }
  2780. }
  2781. /**
  2782. * _scsih_tm_tr_send - send task management request
  2783. * @ioc: per adapter object
  2784. * @handle: device handle
  2785. * Context: interrupt time.
  2786. *
  2787. * This code is to initiate the device removal handshake protocol
  2788. * with controller firmware. This function will issue target reset
  2789. * using high priority request queue. It will send a sas iounit
  2790. * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
  2791. *
  2792. * This is designed to send muliple task management request at the same
  2793. * time to the fifo. If the fifo is full, we will append the request,
  2794. * and process it in a future completion.
  2795. */
  2796. static void
  2797. _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2798. {
  2799. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2800. u16 smid;
  2801. struct _sas_device *sas_device;
  2802. struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
  2803. u64 sas_address = 0;
  2804. unsigned long flags;
  2805. struct _tr_list *delayed_tr;
  2806. u32 ioc_state;
  2807. if (ioc->remove_host) {
  2808. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
  2809. "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
  2810. return;
  2811. } else if (ioc->pci_error_recovery) {
  2812. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
  2813. "error recovery: handle(0x%04x)\n", __func__, ioc->name,
  2814. handle));
  2815. return;
  2816. }
  2817. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2818. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2819. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
  2820. "operational: handle(0x%04x)\n", __func__, ioc->name,
  2821. handle));
  2822. return;
  2823. }
  2824. /* if PD, then return */
  2825. if (test_bit(handle, ioc->pd_handles))
  2826. return;
  2827. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2828. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  2829. if (sas_device && sas_device->starget &&
  2830. sas_device->starget->hostdata) {
  2831. sas_target_priv_data = sas_device->starget->hostdata;
  2832. sas_target_priv_data->deleted = 1;
  2833. sas_address = sas_device->sas_address;
  2834. }
  2835. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2836. if (sas_target_priv_data) {
  2837. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
  2838. "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
  2839. (unsigned long long)sas_address));
  2840. _scsih_ublock_io_device(ioc, sas_address);
  2841. sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
  2842. }
  2843. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
  2844. if (!smid) {
  2845. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  2846. if (!delayed_tr)
  2847. return;
  2848. INIT_LIST_HEAD(&delayed_tr->list);
  2849. delayed_tr->handle = handle;
  2850. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  2851. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2852. "DELAYED:tr:handle(0x%04x), (open)\n",
  2853. ioc->name, handle));
  2854. return;
  2855. }
  2856. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
  2857. "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
  2858. ioc->tm_tr_cb_idx));
  2859. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2860. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2861. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2862. mpi_request->DevHandle = cpu_to_le16(handle);
  2863. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  2864. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  2865. }
  2866. /**
  2867. * _scsih_sas_control_complete - completion routine
  2868. * @ioc: per adapter object
  2869. * @smid: system request message index
  2870. * @msix_index: MSIX table index supplied by the OS
  2871. * @reply: reply message frame(lower 32bit addr)
  2872. * Context: interrupt time.
  2873. *
  2874. * This is the sas iounit control completion routine.
  2875. * This code is part of the code to initiate the device removal
  2876. * handshake protocol with controller firmware.
  2877. *
  2878. * Return 1 meaning mf should be freed from _base_interrupt
  2879. * 0 means the mf is freed from this function.
  2880. */
  2881. static u8
  2882. _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  2883. u8 msix_index, u32 reply)
  2884. {
  2885. Mpi2SasIoUnitControlReply_t *mpi_reply =
  2886. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2887. if (likely(mpi_reply)) {
  2888. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2889. "sc_complete:handle(0x%04x), (open) "
  2890. "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  2891. ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
  2892. le16_to_cpu(mpi_reply->IOCStatus),
  2893. le32_to_cpu(mpi_reply->IOCLogInfo)));
  2894. } else {
  2895. printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  2896. ioc->name, __FILE__, __LINE__, __func__);
  2897. }
  2898. return 1;
  2899. }
  2900. /**
  2901. * _scsih_tm_tr_volume_send - send target reset request for volumes
  2902. * @ioc: per adapter object
  2903. * @handle: device handle
  2904. * Context: interrupt time.
  2905. *
  2906. * This is designed to send muliple task management request at the same
  2907. * time to the fifo. If the fifo is full, we will append the request,
  2908. * and process it in a future completion.
  2909. */
  2910. static void
  2911. _scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2912. {
  2913. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2914. u16 smid;
  2915. struct _tr_list *delayed_tr;
  2916. if (ioc->shost_recovery || ioc->remove_host ||
  2917. ioc->pci_error_recovery) {
  2918. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
  2919. "progress!\n", __func__, ioc->name));
  2920. return;
  2921. }
  2922. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
  2923. if (!smid) {
  2924. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  2925. if (!delayed_tr)
  2926. return;
  2927. INIT_LIST_HEAD(&delayed_tr->list);
  2928. delayed_tr->handle = handle;
  2929. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
  2930. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2931. "DELAYED:tr:handle(0x%04x), (open)\n",
  2932. ioc->name, handle));
  2933. return;
  2934. }
  2935. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
  2936. "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
  2937. ioc->tm_tr_volume_cb_idx));
  2938. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2939. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2940. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2941. mpi_request->DevHandle = cpu_to_le16(handle);
  2942. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  2943. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  2944. }
  2945. /**
  2946. * _scsih_tm_volume_tr_complete - target reset completion
  2947. * @ioc: per adapter object
  2948. * @smid: system request message index
  2949. * @msix_index: MSIX table index supplied by the OS
  2950. * @reply: reply message frame(lower 32bit addr)
  2951. * Context: interrupt time.
  2952. *
  2953. * Return 1 meaning mf should be freed from _base_interrupt
  2954. * 0 means the mf is freed from this function.
  2955. */
  2956. static u8
  2957. _scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  2958. u8 msix_index, u32 reply)
  2959. {
  2960. u16 handle;
  2961. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  2962. Mpi2SCSITaskManagementReply_t *mpi_reply =
  2963. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2964. if (ioc->shost_recovery || ioc->remove_host ||
  2965. ioc->pci_error_recovery) {
  2966. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
  2967. "progress!\n", __func__, ioc->name));
  2968. return 1;
  2969. }
  2970. if (unlikely(!mpi_reply)) {
  2971. printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  2972. ioc->name, __FILE__, __LINE__, __func__);
  2973. return 1;
  2974. }
  2975. mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
  2976. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  2977. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  2978. dewtprintk(ioc, printk("spurious interrupt: "
  2979. "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
  2980. le16_to_cpu(mpi_reply->DevHandle), smid));
  2981. return 0;
  2982. }
  2983. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2984. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  2985. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  2986. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  2987. le32_to_cpu(mpi_reply->IOCLogInfo),
  2988. le32_to_cpu(mpi_reply->TerminationCount)));
  2989. return _scsih_check_for_pending_tm(ioc, smid);
  2990. }
  2991. /**
  2992. * _scsih_tm_tr_complete -
  2993. * @ioc: per adapter object
  2994. * @smid: system request message index
  2995. * @msix_index: MSIX table index supplied by the OS
  2996. * @reply: reply message frame(lower 32bit addr)
  2997. * Context: interrupt time.
  2998. *
  2999. * This is the target reset completion routine.
  3000. * This code is part of the code to initiate the device removal
  3001. * handshake protocol with controller firmware.
  3002. * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
  3003. *
  3004. * Return 1 meaning mf should be freed from _base_interrupt
  3005. * 0 means the mf is freed from this function.
  3006. */
  3007. static u8
  3008. _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  3009. u32 reply)
  3010. {
  3011. u16 handle;
  3012. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  3013. Mpi2SCSITaskManagementReply_t *mpi_reply =
  3014. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  3015. Mpi2SasIoUnitControlRequest_t *mpi_request;
  3016. u16 smid_sas_ctrl;
  3017. u32 ioc_state;
  3018. if (ioc->remove_host) {
  3019. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
  3020. "removed\n", __func__, ioc->name));
  3021. return 1;
  3022. } else if (ioc->pci_error_recovery) {
  3023. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
  3024. "error recovery\n", __func__, ioc->name));
  3025. return 1;
  3026. }
  3027. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  3028. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3029. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
  3030. "operational\n", __func__, ioc->name));
  3031. return 1;
  3032. }
  3033. if (unlikely(!mpi_reply)) {
  3034. printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  3035. ioc->name, __FILE__, __LINE__, __func__);
  3036. return 1;
  3037. }
  3038. mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
  3039. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  3040. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  3041. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
  3042. "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
  3043. le16_to_cpu(mpi_reply->DevHandle), smid));
  3044. return 0;
  3045. }
  3046. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  3047. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  3048. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  3049. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  3050. le32_to_cpu(mpi_reply->IOCLogInfo),
  3051. le32_to_cpu(mpi_reply->TerminationCount)));
  3052. smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
  3053. if (!smid_sas_ctrl) {
  3054. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3055. ioc->name, __func__);
  3056. return 1;
  3057. }
  3058. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
  3059. "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
  3060. ioc->tm_sas_control_cb_idx));
  3061. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
  3062. memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3063. mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3064. mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3065. mpi_request->DevHandle = mpi_request_tm->DevHandle;
  3066. mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
  3067. return _scsih_check_for_pending_tm(ioc, smid);
  3068. }
  3069. /**
  3070. * _scsih_check_for_pending_tm - check for pending task management
  3071. * @ioc: per adapter object
  3072. * @smid: system request message index
  3073. *
  3074. * This will check delayed target reset list, and feed the
  3075. * next reqeust.
  3076. *
  3077. * Return 1 meaning mf should be freed from _base_interrupt
  3078. * 0 means the mf is freed from this function.
  3079. */
  3080. static u8
  3081. _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  3082. {
  3083. struct _tr_list *delayed_tr;
  3084. if (!list_empty(&ioc->delayed_tr_volume_list)) {
  3085. delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
  3086. struct _tr_list, list);
  3087. mpt2sas_base_free_smid(ioc, smid);
  3088. _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
  3089. list_del(&delayed_tr->list);
  3090. kfree(delayed_tr);
  3091. return 0;
  3092. }
  3093. if (!list_empty(&ioc->delayed_tr_list)) {
  3094. delayed_tr = list_entry(ioc->delayed_tr_list.next,
  3095. struct _tr_list, list);
  3096. mpt2sas_base_free_smid(ioc, smid);
  3097. _scsih_tm_tr_send(ioc, delayed_tr->handle);
  3098. list_del(&delayed_tr->list);
  3099. kfree(delayed_tr);
  3100. return 0;
  3101. }
  3102. return 1;
  3103. }
  3104. /**
  3105. * _scsih_check_topo_delete_events - sanity check on topo events
  3106. * @ioc: per adapter object
  3107. * @event_data: the event data payload
  3108. *
  3109. * This routine added to better handle cable breaker.
  3110. *
  3111. * This handles the case where driver receives multiple expander
  3112. * add and delete events in a single shot. When there is a delete event
  3113. * the routine will void any pending add events waiting in the event queue.
  3114. *
  3115. * Return nothing.
  3116. */
  3117. static void
  3118. _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
  3119. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3120. {
  3121. struct fw_event_work *fw_event;
  3122. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  3123. u16 expander_handle;
  3124. struct _sas_node *sas_expander;
  3125. unsigned long flags;
  3126. int i, reason_code;
  3127. u16 handle;
  3128. for (i = 0 ; i < event_data->NumEntries; i++) {
  3129. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3130. if (!handle)
  3131. continue;
  3132. reason_code = event_data->PHY[i].PhyStatus &
  3133. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3134. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
  3135. _scsih_tm_tr_send(ioc, handle);
  3136. }
  3137. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  3138. if (expander_handle < ioc->sas_hba.num_phys) {
  3139. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  3140. return;
  3141. }
  3142. if (event_data->ExpStatus ==
  3143. MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
  3144. /* put expander attached devices into blocking state */
  3145. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3146. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  3147. expander_handle);
  3148. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  3149. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3150. do {
  3151. handle = find_first_bit(ioc->blocking_handles,
  3152. ioc->facts.MaxDevHandle);
  3153. if (handle < ioc->facts.MaxDevHandle)
  3154. _scsih_block_io_device(ioc, handle);
  3155. } while (test_and_clear_bit(handle, ioc->blocking_handles));
  3156. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  3157. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  3158. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  3159. return;
  3160. /* mark ignore flag for pending events */
  3161. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  3162. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  3163. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  3164. fw_event->ignore)
  3165. continue;
  3166. local_event_data = fw_event->event_data;
  3167. if (local_event_data->ExpStatus ==
  3168. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  3169. local_event_data->ExpStatus ==
  3170. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  3171. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  3172. expander_handle) {
  3173. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  3174. "setting ignoring flag\n", ioc->name));
  3175. fw_event->ignore = 1;
  3176. }
  3177. }
  3178. }
  3179. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  3180. }
  3181. /**
  3182. * _scsih_set_volume_delete_flag - setting volume delete flag
  3183. * @ioc: per adapter object
  3184. * @handle: device handle
  3185. *
  3186. * This
  3187. * Return nothing.
  3188. */
  3189. static void
  3190. _scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3191. {
  3192. struct _raid_device *raid_device;
  3193. struct MPT2SAS_TARGET *sas_target_priv_data;
  3194. unsigned long flags;
  3195. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3196. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  3197. if (raid_device && raid_device->starget &&
  3198. raid_device->starget->hostdata) {
  3199. sas_target_priv_data =
  3200. raid_device->starget->hostdata;
  3201. sas_target_priv_data->deleted = 1;
  3202. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  3203. "setting delete flag: handle(0x%04x), "
  3204. "wwid(0x%016llx)\n", ioc->name, handle,
  3205. (unsigned long long) raid_device->wwid));
  3206. }
  3207. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3208. }
  3209. /**
  3210. * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
  3211. * @handle: input handle
  3212. * @a: handle for volume a
  3213. * @b: handle for volume b
  3214. *
  3215. * IR firmware only supports two raid volumes. The purpose of this
  3216. * routine is to set the volume handle in either a or b. When the given
  3217. * input handle is non-zero, or when a and b have not been set before.
  3218. */
  3219. static void
  3220. _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
  3221. {
  3222. if (!handle || handle == *a || handle == *b)
  3223. return;
  3224. if (!*a)
  3225. *a = handle;
  3226. else if (!*b)
  3227. *b = handle;
  3228. }
  3229. /**
  3230. * _scsih_check_ir_config_unhide_events - check for UNHIDE events
  3231. * @ioc: per adapter object
  3232. * @event_data: the event data payload
  3233. * Context: interrupt time.
  3234. *
  3235. * This routine will send target reset to volume, followed by target
  3236. * resets to the PDs. This is called when a PD has been removed, or
  3237. * volume has been deleted or removed. When the target reset is sent
  3238. * to volume, the PD target resets need to be queued to start upon
  3239. * completion of the volume target reset.
  3240. *
  3241. * Return nothing.
  3242. */
  3243. static void
  3244. _scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
  3245. Mpi2EventDataIrConfigChangeList_t *event_data)
  3246. {
  3247. Mpi2EventIrConfigElement_t *element;
  3248. int i;
  3249. u16 handle, volume_handle, a, b;
  3250. struct _tr_list *delayed_tr;
  3251. a = 0;
  3252. b = 0;
  3253. if (ioc->is_warpdrive)
  3254. return;
  3255. /* Volume Resets for Deleted or Removed */
  3256. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3257. for (i = 0; i < event_data->NumElements; i++, element++) {
  3258. if (element->ReasonCode ==
  3259. MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
  3260. element->ReasonCode ==
  3261. MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
  3262. volume_handle = le16_to_cpu(element->VolDevHandle);
  3263. _scsih_set_volume_delete_flag(ioc, volume_handle);
  3264. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  3265. }
  3266. }
  3267. /* Volume Resets for UNHIDE events */
  3268. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3269. for (i = 0; i < event_data->NumElements; i++, element++) {
  3270. if (le32_to_cpu(event_data->Flags) &
  3271. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  3272. continue;
  3273. if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
  3274. volume_handle = le16_to_cpu(element->VolDevHandle);
  3275. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  3276. }
  3277. }
  3278. if (a)
  3279. _scsih_tm_tr_volume_send(ioc, a);
  3280. if (b)
  3281. _scsih_tm_tr_volume_send(ioc, b);
  3282. /* PD target resets */
  3283. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3284. for (i = 0; i < event_data->NumElements; i++, element++) {
  3285. if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
  3286. continue;
  3287. handle = le16_to_cpu(element->PhysDiskDevHandle);
  3288. volume_handle = le16_to_cpu(element->VolDevHandle);
  3289. clear_bit(handle, ioc->pd_handles);
  3290. if (!volume_handle)
  3291. _scsih_tm_tr_send(ioc, handle);
  3292. else if (volume_handle == a || volume_handle == b) {
  3293. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  3294. BUG_ON(!delayed_tr);
  3295. INIT_LIST_HEAD(&delayed_tr->list);
  3296. delayed_tr->handle = handle;
  3297. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  3298. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  3299. "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
  3300. handle));
  3301. } else
  3302. _scsih_tm_tr_send(ioc, handle);
  3303. }
  3304. }
  3305. /**
  3306. * _scsih_check_volume_delete_events - set delete flag for volumes
  3307. * @ioc: per adapter object
  3308. * @event_data: the event data payload
  3309. * Context: interrupt time.
  3310. *
  3311. * This will handle the case when the cable connected to entire volume is
  3312. * pulled. We will take care of setting the deleted flag so normal IO will
  3313. * not be sent.
  3314. *
  3315. * Return nothing.
  3316. */
  3317. static void
  3318. _scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
  3319. Mpi2EventDataIrVolume_t *event_data)
  3320. {
  3321. u32 state;
  3322. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  3323. return;
  3324. state = le32_to_cpu(event_data->NewValue);
  3325. if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
  3326. MPI2_RAID_VOL_STATE_FAILED)
  3327. _scsih_set_volume_delete_flag(ioc,
  3328. le16_to_cpu(event_data->VolDevHandle));
  3329. }
  3330. /**
  3331. * _scsih_flush_running_cmds - completing outstanding commands.
  3332. * @ioc: per adapter object
  3333. *
  3334. * The flushing out of all pending scmd commands following host reset,
  3335. * where all IO is dropped to the floor.
  3336. *
  3337. * Return nothing.
  3338. */
  3339. static void
  3340. _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
  3341. {
  3342. struct scsi_cmnd *scmd;
  3343. u16 smid;
  3344. u16 count = 0;
  3345. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  3346. scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
  3347. if (!scmd)
  3348. continue;
  3349. count++;
  3350. mpt2sas_base_free_smid(ioc, smid);
  3351. scsi_dma_unmap(scmd);
  3352. if (ioc->pci_error_recovery)
  3353. scmd->result = DID_NO_CONNECT << 16;
  3354. else
  3355. scmd->result = DID_RESET << 16;
  3356. scmd->scsi_done(scmd);
  3357. }
  3358. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
  3359. ioc->name, count));
  3360. }
  3361. /**
  3362. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  3363. * @scmd: pointer to scsi command object
  3364. * @mpi_request: pointer to the SCSI_IO reqest message frame
  3365. *
  3366. * Supporting protection 1 and 3.
  3367. *
  3368. * Returns nothing
  3369. */
  3370. static void
  3371. _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
  3372. {
  3373. u16 eedp_flags;
  3374. unsigned char prot_op = scsi_get_prot_op(scmd);
  3375. unsigned char prot_type = scsi_get_prot_type(scmd);
  3376. if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
  3377. return;
  3378. if (prot_op == SCSI_PROT_READ_STRIP)
  3379. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  3380. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  3381. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  3382. else
  3383. return;
  3384. switch (prot_type) {
  3385. case SCSI_PROT_DIF_TYPE1:
  3386. case SCSI_PROT_DIF_TYPE2:
  3387. /*
  3388. * enable ref/guard checking
  3389. * auto increment ref tag
  3390. */
  3391. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  3392. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  3393. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  3394. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  3395. cpu_to_be32(scsi_get_lba(scmd));
  3396. break;
  3397. case SCSI_PROT_DIF_TYPE3:
  3398. /*
  3399. * enable guard checking
  3400. */
  3401. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  3402. break;
  3403. }
  3404. mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
  3405. mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
  3406. }
  3407. /**
  3408. * _scsih_eedp_error_handling - return sense code for EEDP errors
  3409. * @scmd: pointer to scsi command object
  3410. * @ioc_status: ioc status
  3411. *
  3412. * Returns nothing
  3413. */
  3414. static void
  3415. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  3416. {
  3417. u8 ascq;
  3418. u8 sk;
  3419. u8 host_byte;
  3420. switch (ioc_status) {
  3421. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  3422. ascq = 0x01;
  3423. break;
  3424. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  3425. ascq = 0x02;
  3426. break;
  3427. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  3428. ascq = 0x03;
  3429. break;
  3430. default:
  3431. ascq = 0x00;
  3432. break;
  3433. }
  3434. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  3435. sk = ILLEGAL_REQUEST;
  3436. host_byte = DID_ABORT;
  3437. } else {
  3438. sk = ABORTED_COMMAND;
  3439. host_byte = DID_OK;
  3440. }
  3441. scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
  3442. scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
  3443. SAM_STAT_CHECK_CONDITION;
  3444. }
  3445. /**
  3446. * _scsih_scsi_direct_io_get - returns direct io flag
  3447. * @ioc: per adapter object
  3448. * @smid: system request message index
  3449. *
  3450. * Returns the smid stored scmd pointer.
  3451. */
  3452. static inline u8
  3453. _scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  3454. {
  3455. return ioc->scsi_lookup[smid - 1].direct_io;
  3456. }
  3457. /**
  3458. * _scsih_scsi_direct_io_set - sets direct io flag
  3459. * @ioc: per adapter object
  3460. * @smid: system request message index
  3461. * @direct_io: Zero or non-zero value to set in the direct_io flag
  3462. *
  3463. * Returns Nothing.
  3464. */
  3465. static inline void
  3466. _scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
  3467. {
  3468. ioc->scsi_lookup[smid - 1].direct_io = direct_io;
  3469. }
  3470. /**
  3471. * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
  3472. * @ioc: per adapter object
  3473. * @scmd: pointer to scsi command object
  3474. * @raid_device: pointer to raid device data structure
  3475. * @mpi_request: pointer to the SCSI_IO reqest message frame
  3476. * @smid: system request message index
  3477. *
  3478. * Returns nothing
  3479. */
  3480. static void
  3481. _scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  3482. struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
  3483. u16 smid)
  3484. {
  3485. u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
  3486. u32 stripe_sz, stripe_exp;
  3487. u8 num_pds, *cdb_ptr, i;
  3488. u8 cdb0 = scmd->cmnd[0];
  3489. u64 v_llba;
  3490. /*
  3491. * Try Direct I/O to RAID memeber disks
  3492. */
  3493. if (cdb0 == READ_16 || cdb0 == READ_10 ||
  3494. cdb0 == WRITE_16 || cdb0 == WRITE_10) {
  3495. cdb_ptr = mpi_request->CDB.CDB32;
  3496. if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
  3497. | cdb_ptr[5])) {
  3498. io_size = scsi_bufflen(scmd) >>
  3499. raid_device->block_exponent;
  3500. i = (cdb0 < READ_16) ? 2 : 6;
  3501. /* get virtual lba */
  3502. v_lba = be32_to_cpu(*(__be32 *)(&cdb_ptr[i]));
  3503. if (((u64)v_lba + (u64)io_size - 1) <=
  3504. (u32)raid_device->max_lba) {
  3505. stripe_sz = raid_device->stripe_sz;
  3506. stripe_exp = raid_device->stripe_exponent;
  3507. stripe_off = v_lba & (stripe_sz - 1);
  3508. /* Check whether IO falls within a stripe */
  3509. if ((stripe_off + io_size) <= stripe_sz) {
  3510. num_pds = raid_device->num_pds;
  3511. p_lba = v_lba >> stripe_exp;
  3512. stripe_unit = p_lba / num_pds;
  3513. column = p_lba % num_pds;
  3514. p_lba = (stripe_unit << stripe_exp) +
  3515. stripe_off;
  3516. mpi_request->DevHandle =
  3517. cpu_to_le16(raid_device->
  3518. pd_handle[column]);
  3519. (*(__be32 *)(&cdb_ptr[i])) =
  3520. cpu_to_be32(p_lba);
  3521. /*
  3522. * WD: To indicate this I/O is directI/O
  3523. */
  3524. _scsih_scsi_direct_io_set(ioc, smid, 1);
  3525. }
  3526. }
  3527. } else {
  3528. io_size = scsi_bufflen(scmd) >>
  3529. raid_device->block_exponent;
  3530. /* get virtual lba */
  3531. v_llba = be64_to_cpu(*(__be64 *)(&cdb_ptr[2]));
  3532. if ((v_llba + (u64)io_size - 1) <=
  3533. raid_device->max_lba) {
  3534. stripe_sz = raid_device->stripe_sz;
  3535. stripe_exp = raid_device->stripe_exponent;
  3536. stripe_off = (u32) (v_llba & (stripe_sz - 1));
  3537. /* Check whether IO falls within a stripe */
  3538. if ((stripe_off + io_size) <= stripe_sz) {
  3539. num_pds = raid_device->num_pds;
  3540. p_lba = (u32)(v_llba >> stripe_exp);
  3541. stripe_unit = p_lba / num_pds;
  3542. column = p_lba % num_pds;
  3543. p_lba = (stripe_unit << stripe_exp) +
  3544. stripe_off;
  3545. mpi_request->DevHandle =
  3546. cpu_to_le16(raid_device->
  3547. pd_handle[column]);
  3548. (*(__be64 *)(&cdb_ptr[2])) =
  3549. cpu_to_be64((u64)p_lba);
  3550. /*
  3551. * WD: To indicate this I/O is directI/O
  3552. */
  3553. _scsih_scsi_direct_io_set(ioc, smid, 1);
  3554. }
  3555. }
  3556. }
  3557. }
  3558. }
  3559. /**
  3560. * _scsih_qcmd - main scsi request entry point
  3561. * @scmd: pointer to scsi command object
  3562. * @done: function pointer to be invoked on completion
  3563. *
  3564. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  3565. *
  3566. * Returns 0 on success. If there's a failure, return either:
  3567. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  3568. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  3569. */
  3570. static int
  3571. _scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
  3572. {
  3573. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  3574. struct MPT2SAS_DEVICE *sas_device_priv_data;
  3575. struct MPT2SAS_TARGET *sas_target_priv_data;
  3576. struct _raid_device *raid_device;
  3577. Mpi2SCSIIORequest_t *mpi_request;
  3578. u32 mpi_control;
  3579. u16 smid;
  3580. scmd->scsi_done = done;
  3581. sas_device_priv_data = scmd->device->hostdata;
  3582. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  3583. scmd->result = DID_NO_CONNECT << 16;
  3584. scmd->scsi_done(scmd);
  3585. return 0;
  3586. }
  3587. if (ioc->pci_error_recovery || ioc->remove_host) {
  3588. scmd->result = DID_NO_CONNECT << 16;
  3589. scmd->scsi_done(scmd);
  3590. return 0;
  3591. }
  3592. sas_target_priv_data = sas_device_priv_data->sas_target;
  3593. /* invalid device handle */
  3594. if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
  3595. scmd->result = DID_NO_CONNECT << 16;
  3596. scmd->scsi_done(scmd);
  3597. return 0;
  3598. }
  3599. /* host recovery or link resets sent via IOCTLs */
  3600. if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
  3601. return SCSI_MLQUEUE_HOST_BUSY;
  3602. /* device busy with task management */
  3603. else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
  3604. return SCSI_MLQUEUE_DEVICE_BUSY;
  3605. /* device has been deleted */
  3606. else if (sas_target_priv_data->deleted) {
  3607. scmd->result = DID_NO_CONNECT << 16;
  3608. scmd->scsi_done(scmd);
  3609. return 0;
  3610. }
  3611. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  3612. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  3613. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  3614. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  3615. else
  3616. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  3617. /* set tags */
  3618. if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
  3619. if (scmd->device->tagged_supported) {
  3620. if (scmd->device->ordered_tags)
  3621. mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
  3622. else
  3623. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  3624. } else
  3625. /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
  3626. /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
  3627. */
  3628. mpi_control |= (0x500);
  3629. } else
  3630. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  3631. /* Make sure Device is not raid volume.
  3632. * We do not expose raid functionality to upper layer for warpdrive.
  3633. */
  3634. if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
  3635. sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
  3636. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  3637. smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
  3638. if (!smid) {
  3639. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3640. ioc->name, __func__);
  3641. goto out;
  3642. }
  3643. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3644. memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
  3645. _scsih_setup_eedp(scmd, mpi_request);
  3646. if (scmd->cmd_len == 32)
  3647. mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
  3648. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  3649. if (sas_device_priv_data->sas_target->flags &
  3650. MPT_TARGET_FLAGS_RAID_COMPONENT)
  3651. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  3652. else
  3653. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  3654. mpi_request->DevHandle =
  3655. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  3656. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  3657. mpi_request->Control = cpu_to_le32(mpi_control);
  3658. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  3659. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  3660. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  3661. mpi_request->SenseBufferLowAddress =
  3662. mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  3663. mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
  3664. mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
  3665. MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
  3666. mpi_request->VF_ID = 0; /* TODO */
  3667. mpi_request->VP_ID = 0;
  3668. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  3669. mpi_request->LUN);
  3670. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  3671. if (!mpi_request->DataLength) {
  3672. mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
  3673. } else {
  3674. if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
  3675. mpt2sas_base_free_smid(ioc, smid);
  3676. goto out;
  3677. }
  3678. }
  3679. raid_device = sas_target_priv_data->raid_device;
  3680. if (raid_device && raid_device->direct_io_enabled)
  3681. _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
  3682. smid);
  3683. if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
  3684. mpt2sas_base_put_smid_scsi_io(ioc, smid,
  3685. le16_to_cpu(mpi_request->DevHandle));
  3686. else
  3687. mpt2sas_base_put_smid_default(ioc, smid);
  3688. return 0;
  3689. out:
  3690. return SCSI_MLQUEUE_HOST_BUSY;
  3691. }
  3692. static DEF_SCSI_QCMD(_scsih_qcmd)
  3693. /**
  3694. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  3695. * @sense_buffer: sense data returned by target
  3696. * @data: normalized skey/asc/ascq
  3697. *
  3698. * Return nothing.
  3699. */
  3700. static void
  3701. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  3702. {
  3703. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  3704. /* descriptor format */
  3705. data->skey = sense_buffer[1] & 0x0F;
  3706. data->asc = sense_buffer[2];
  3707. data->ascq = sense_buffer[3];
  3708. } else {
  3709. /* fixed format */
  3710. data->skey = sense_buffer[2] & 0x0F;
  3711. data->asc = sense_buffer[12];
  3712. data->ascq = sense_buffer[13];
  3713. }
  3714. }
  3715. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3716. /**
  3717. * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
  3718. * @ioc: per adapter object
  3719. * @scmd: pointer to scsi command object
  3720. * @mpi_reply: reply mf payload returned from firmware
  3721. *
  3722. * scsi_status - SCSI Status code returned from target device
  3723. * scsi_state - state info associated with SCSI_IO determined by ioc
  3724. * ioc_status - ioc supplied status info
  3725. *
  3726. * Return nothing.
  3727. */
  3728. static void
  3729. _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  3730. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  3731. {
  3732. u32 response_info;
  3733. u8 *response_bytes;
  3734. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  3735. MPI2_IOCSTATUS_MASK;
  3736. u8 scsi_state = mpi_reply->SCSIState;
  3737. u8 scsi_status = mpi_reply->SCSIStatus;
  3738. char *desc_ioc_state = NULL;
  3739. char *desc_scsi_status = NULL;
  3740. char *desc_scsi_state = ioc->tmp_string;
  3741. u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  3742. struct _sas_device *sas_device = NULL;
  3743. unsigned long flags;
  3744. struct scsi_target *starget = scmd->device->sdev_target;
  3745. struct MPT2SAS_TARGET *priv_target = starget->hostdata;
  3746. char *device_str = NULL;
  3747. if (!priv_target)
  3748. return;
  3749. if (ioc->hide_ir_msg)
  3750. device_str = "WarpDrive";
  3751. else
  3752. device_str = "volume";
  3753. if (log_info == 0x31170000)
  3754. return;
  3755. switch (ioc_status) {
  3756. case MPI2_IOCSTATUS_SUCCESS:
  3757. desc_ioc_state = "success";
  3758. break;
  3759. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  3760. desc_ioc_state = "invalid function";
  3761. break;
  3762. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  3763. desc_ioc_state = "scsi recovered error";
  3764. break;
  3765. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  3766. desc_ioc_state = "scsi invalid dev handle";
  3767. break;
  3768. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  3769. desc_ioc_state = "scsi device not there";
  3770. break;
  3771. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  3772. desc_ioc_state = "scsi data overrun";
  3773. break;
  3774. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  3775. desc_ioc_state = "scsi data underrun";
  3776. break;
  3777. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  3778. desc_ioc_state = "scsi io data error";
  3779. break;
  3780. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  3781. desc_ioc_state = "scsi protocol error";
  3782. break;
  3783. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  3784. desc_ioc_state = "scsi task terminated";
  3785. break;
  3786. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  3787. desc_ioc_state = "scsi residual mismatch";
  3788. break;
  3789. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  3790. desc_ioc_state = "scsi task mgmt failed";
  3791. break;
  3792. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  3793. desc_ioc_state = "scsi ioc terminated";
  3794. break;
  3795. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  3796. desc_ioc_state = "scsi ext terminated";
  3797. break;
  3798. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  3799. desc_ioc_state = "eedp guard error";
  3800. break;
  3801. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  3802. desc_ioc_state = "eedp ref tag error";
  3803. break;
  3804. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  3805. desc_ioc_state = "eedp app tag error";
  3806. break;
  3807. default:
  3808. desc_ioc_state = "unknown";
  3809. break;
  3810. }
  3811. switch (scsi_status) {
  3812. case MPI2_SCSI_STATUS_GOOD:
  3813. desc_scsi_status = "good";
  3814. break;
  3815. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  3816. desc_scsi_status = "check condition";
  3817. break;
  3818. case MPI2_SCSI_STATUS_CONDITION_MET:
  3819. desc_scsi_status = "condition met";
  3820. break;
  3821. case MPI2_SCSI_STATUS_BUSY:
  3822. desc_scsi_status = "busy";
  3823. break;
  3824. case MPI2_SCSI_STATUS_INTERMEDIATE:
  3825. desc_scsi_status = "intermediate";
  3826. break;
  3827. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  3828. desc_scsi_status = "intermediate condmet";
  3829. break;
  3830. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  3831. desc_scsi_status = "reservation conflict";
  3832. break;
  3833. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  3834. desc_scsi_status = "command terminated";
  3835. break;
  3836. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  3837. desc_scsi_status = "task set full";
  3838. break;
  3839. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  3840. desc_scsi_status = "aca active";
  3841. break;
  3842. case MPI2_SCSI_STATUS_TASK_ABORTED:
  3843. desc_scsi_status = "task aborted";
  3844. break;
  3845. default:
  3846. desc_scsi_status = "unknown";
  3847. break;
  3848. }
  3849. desc_scsi_state[0] = '\0';
  3850. if (!scsi_state)
  3851. desc_scsi_state = " ";
  3852. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  3853. strcat(desc_scsi_state, "response info ");
  3854. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  3855. strcat(desc_scsi_state, "state terminated ");
  3856. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  3857. strcat(desc_scsi_state, "no status ");
  3858. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  3859. strcat(desc_scsi_state, "autosense failed ");
  3860. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  3861. strcat(desc_scsi_state, "autosense valid ");
  3862. scsi_print_command(scmd);
  3863. if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  3864. printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
  3865. device_str, (unsigned long long)priv_target->sas_address);
  3866. } else {
  3867. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3868. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  3869. priv_target->sas_address);
  3870. if (sas_device) {
  3871. printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
  3872. "phy(%d)\n", ioc->name, sas_device->sas_address,
  3873. sas_device->phy);
  3874. printk(MPT2SAS_WARN_FMT
  3875. "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
  3876. ioc->name, sas_device->enclosure_logical_id,
  3877. sas_device->slot);
  3878. }
  3879. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3880. }
  3881. printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
  3882. "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
  3883. desc_ioc_state, ioc_status, smid);
  3884. printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
  3885. "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
  3886. scsi_get_resid(scmd));
  3887. printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
  3888. "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  3889. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  3890. printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
  3891. "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
  3892. scsi_status, desc_scsi_state, scsi_state);
  3893. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  3894. struct sense_info data;
  3895. _scsih_normalize_sense(scmd->sense_buffer, &data);
  3896. printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
  3897. "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
  3898. data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
  3899. }
  3900. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  3901. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  3902. response_bytes = (u8 *)&response_info;
  3903. _scsih_response_code(ioc, response_bytes[0]);
  3904. }
  3905. }
  3906. #endif
  3907. /**
  3908. * _scsih_turn_on_fault_led - illuminate Fault LED
  3909. * @ioc: per adapter object
  3910. * @handle: device handle
  3911. * Context: process
  3912. *
  3913. * Return nothing.
  3914. */
  3915. static void
  3916. _scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3917. {
  3918. Mpi2SepReply_t mpi_reply;
  3919. Mpi2SepRequest_t mpi_request;
  3920. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  3921. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  3922. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  3923. mpi_request.SlotStatus =
  3924. cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
  3925. mpi_request.DevHandle = cpu_to_le16(handle);
  3926. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  3927. if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  3928. &mpi_request)) != 0) {
  3929. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
  3930. __FILE__, __LINE__, __func__);
  3931. return;
  3932. }
  3933. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  3934. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
  3935. "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
  3936. le16_to_cpu(mpi_reply.IOCStatus),
  3937. le32_to_cpu(mpi_reply.IOCLogInfo)));
  3938. return;
  3939. }
  3940. }
  3941. /**
  3942. * _scsih_send_event_to_turn_on_fault_led - fire delayed event
  3943. * @ioc: per adapter object
  3944. * @handle: device handle
  3945. * Context: interrupt.
  3946. *
  3947. * Return nothing.
  3948. */
  3949. static void
  3950. _scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3951. {
  3952. struct fw_event_work *fw_event;
  3953. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  3954. if (!fw_event)
  3955. return;
  3956. fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
  3957. fw_event->device_handle = handle;
  3958. fw_event->ioc = ioc;
  3959. _scsih_fw_event_add(ioc, fw_event);
  3960. }
  3961. /**
  3962. * _scsih_smart_predicted_fault - process smart errors
  3963. * @ioc: per adapter object
  3964. * @handle: device handle
  3965. * Context: interrupt.
  3966. *
  3967. * Return nothing.
  3968. */
  3969. static void
  3970. _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3971. {
  3972. struct scsi_target *starget;
  3973. struct MPT2SAS_TARGET *sas_target_priv_data;
  3974. Mpi2EventNotificationReply_t *event_reply;
  3975. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  3976. struct _sas_device *sas_device;
  3977. ssize_t sz;
  3978. unsigned long flags;
  3979. /* only handle non-raid devices */
  3980. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3981. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3982. if (!sas_device) {
  3983. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3984. return;
  3985. }
  3986. starget = sas_device->starget;
  3987. sas_target_priv_data = starget->hostdata;
  3988. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  3989. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
  3990. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3991. return;
  3992. }
  3993. starget_printk(KERN_WARNING, starget, "predicted fault\n");
  3994. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3995. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
  3996. _scsih_send_event_to_turn_on_fault_led(ioc, handle);
  3997. /* insert into event log */
  3998. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  3999. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  4000. event_reply = kzalloc(sz, GFP_ATOMIC);
  4001. if (!event_reply) {
  4002. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4003. ioc->name, __FILE__, __LINE__, __func__);
  4004. return;
  4005. }
  4006. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  4007. event_reply->Event =
  4008. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  4009. event_reply->MsgLength = sz/4;
  4010. event_reply->EventDataLength =
  4011. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  4012. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  4013. event_reply->EventData;
  4014. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  4015. event_data->ASC = 0x5D;
  4016. event_data->DevHandle = cpu_to_le16(handle);
  4017. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  4018. mpt2sas_ctl_add_to_event_log(ioc, event_reply);
  4019. kfree(event_reply);
  4020. }
  4021. /**
  4022. * _scsih_io_done - scsi request callback
  4023. * @ioc: per adapter object
  4024. * @smid: system request message index
  4025. * @msix_index: MSIX table index supplied by the OS
  4026. * @reply: reply message frame(lower 32bit addr)
  4027. *
  4028. * Callback handler when using _scsih_qcmd.
  4029. *
  4030. * Return 1 meaning mf should be freed from _base_interrupt
  4031. * 0 means the mf is freed from this function.
  4032. */
  4033. static u8
  4034. _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  4035. {
  4036. Mpi2SCSIIORequest_t *mpi_request;
  4037. Mpi2SCSIIOReply_t *mpi_reply;
  4038. struct scsi_cmnd *scmd;
  4039. u16 ioc_status;
  4040. u32 xfer_cnt;
  4041. u8 scsi_state;
  4042. u8 scsi_status;
  4043. u32 log_info;
  4044. struct MPT2SAS_DEVICE *sas_device_priv_data;
  4045. u32 response_code = 0;
  4046. unsigned long flags;
  4047. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  4048. scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
  4049. if (scmd == NULL)
  4050. return 1;
  4051. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  4052. if (mpi_reply == NULL) {
  4053. scmd->result = DID_OK << 16;
  4054. goto out;
  4055. }
  4056. sas_device_priv_data = scmd->device->hostdata;
  4057. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  4058. sas_device_priv_data->sas_target->deleted) {
  4059. scmd->result = DID_NO_CONNECT << 16;
  4060. goto out;
  4061. }
  4062. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  4063. /*
  4064. * WARPDRIVE: If direct_io is set then it is directIO,
  4065. * the failed direct I/O should be redirected to volume
  4066. */
  4067. if (_scsih_scsi_direct_io_get(ioc, smid) &&
  4068. ((ioc_status & MPI2_IOCSTATUS_MASK)
  4069. != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
  4070. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4071. ioc->scsi_lookup[smid - 1].scmd = scmd;
  4072. _scsih_scsi_direct_io_set(ioc, smid, 0);
  4073. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4074. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  4075. mpi_request->DevHandle =
  4076. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  4077. mpt2sas_base_put_smid_scsi_io(ioc, smid,
  4078. sas_device_priv_data->sas_target->handle);
  4079. return 0;
  4080. }
  4081. /* turning off TLR */
  4082. scsi_state = mpi_reply->SCSIState;
  4083. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  4084. response_code =
  4085. le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
  4086. if (!sas_device_priv_data->tlr_snoop_check) {
  4087. sas_device_priv_data->tlr_snoop_check++;
  4088. /* Make sure Device is not raid volume.
  4089. * We do not expose raid functionality to upper layer for warpdrive.
  4090. */
  4091. if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
  4092. sas_is_tlr_enabled(scmd->device) &&
  4093. response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
  4094. sas_disable_tlr(scmd->device);
  4095. sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
  4096. }
  4097. }
  4098. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  4099. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  4100. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  4101. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  4102. else
  4103. log_info = 0;
  4104. ioc_status &= MPI2_IOCSTATUS_MASK;
  4105. scsi_status = mpi_reply->SCSIStatus;
  4106. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  4107. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  4108. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  4109. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  4110. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  4111. }
  4112. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  4113. struct sense_info data;
  4114. const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
  4115. smid);
  4116. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  4117. le32_to_cpu(mpi_reply->SenseCount));
  4118. memcpy(scmd->sense_buffer, sense_data, sz);
  4119. _scsih_normalize_sense(scmd->sense_buffer, &data);
  4120. /* failure prediction threshold exceeded */
  4121. if (data.asc == 0x5D)
  4122. _scsih_smart_predicted_fault(ioc,
  4123. le16_to_cpu(mpi_reply->DevHandle));
  4124. }
  4125. switch (ioc_status) {
  4126. case MPI2_IOCSTATUS_BUSY:
  4127. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  4128. scmd->result = SAM_STAT_BUSY;
  4129. break;
  4130. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  4131. scmd->result = DID_NO_CONNECT << 16;
  4132. break;
  4133. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  4134. if (sas_device_priv_data->block) {
  4135. scmd->result = DID_TRANSPORT_DISRUPTED << 16;
  4136. goto out;
  4137. }
  4138. scmd->result = DID_SOFT_ERROR << 16;
  4139. break;
  4140. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  4141. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  4142. scmd->result = DID_RESET << 16;
  4143. break;
  4144. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  4145. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  4146. scmd->result = DID_SOFT_ERROR << 16;
  4147. else
  4148. scmd->result = (DID_OK << 16) | scsi_status;
  4149. break;
  4150. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  4151. scmd->result = (DID_OK << 16) | scsi_status;
  4152. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  4153. break;
  4154. if (xfer_cnt < scmd->underflow) {
  4155. if (scsi_status == SAM_STAT_BUSY)
  4156. scmd->result = SAM_STAT_BUSY;
  4157. else
  4158. scmd->result = DID_SOFT_ERROR << 16;
  4159. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  4160. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  4161. scmd->result = DID_SOFT_ERROR << 16;
  4162. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  4163. scmd->result = DID_RESET << 16;
  4164. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  4165. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  4166. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  4167. scmd->result = (DRIVER_SENSE << 24) |
  4168. SAM_STAT_CHECK_CONDITION;
  4169. scmd->sense_buffer[0] = 0x70;
  4170. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  4171. scmd->sense_buffer[12] = 0x20;
  4172. scmd->sense_buffer[13] = 0;
  4173. }
  4174. break;
  4175. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  4176. scsi_set_resid(scmd, 0);
  4177. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  4178. case MPI2_IOCSTATUS_SUCCESS:
  4179. scmd->result = (DID_OK << 16) | scsi_status;
  4180. if (response_code ==
  4181. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
  4182. (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  4183. MPI2_SCSI_STATE_NO_SCSI_STATUS)))
  4184. scmd->result = DID_SOFT_ERROR << 16;
  4185. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  4186. scmd->result = DID_RESET << 16;
  4187. break;
  4188. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  4189. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  4190. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  4191. _scsih_eedp_error_handling(scmd, ioc_status);
  4192. break;
  4193. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  4194. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  4195. case MPI2_IOCSTATUS_INVALID_SGL:
  4196. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  4197. case MPI2_IOCSTATUS_INVALID_FIELD:
  4198. case MPI2_IOCSTATUS_INVALID_STATE:
  4199. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  4200. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  4201. default:
  4202. scmd->result = DID_SOFT_ERROR << 16;
  4203. break;
  4204. }
  4205. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4206. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  4207. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  4208. #endif
  4209. out:
  4210. scsi_dma_unmap(scmd);
  4211. scmd->scsi_done(scmd);
  4212. return 1;
  4213. }
  4214. /**
  4215. * _scsih_sas_host_refresh - refreshing sas host object contents
  4216. * @ioc: per adapter object
  4217. * Context: user
  4218. *
  4219. * During port enable, fw will send topology events for every device. Its
  4220. * possible that the handles may change from the previous setting, so this
  4221. * code keeping handles updating if changed.
  4222. *
  4223. * Return nothing.
  4224. */
  4225. static void
  4226. _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
  4227. {
  4228. u16 sz;
  4229. u16 ioc_status;
  4230. int i;
  4231. Mpi2ConfigReply_t mpi_reply;
  4232. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  4233. u16 attached_handle;
  4234. u8 link_rate;
  4235. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
  4236. "updating handles for sas_host(0x%016llx)\n",
  4237. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  4238. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  4239. * sizeof(Mpi2SasIOUnit0PhyData_t));
  4240. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  4241. if (!sas_iounit_pg0) {
  4242. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4243. ioc->name, __FILE__, __LINE__, __func__);
  4244. return;
  4245. }
  4246. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  4247. sas_iounit_pg0, sz)) != 0)
  4248. goto out;
  4249. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  4250. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  4251. goto out;
  4252. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  4253. link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
  4254. if (i == 0)
  4255. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  4256. PhyData[0].ControllerDevHandle);
  4257. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  4258. attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
  4259. AttachedDevHandle);
  4260. if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  4261. link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
  4262. mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
  4263. attached_handle, i, link_rate);
  4264. }
  4265. out:
  4266. kfree(sas_iounit_pg0);
  4267. }
  4268. /**
  4269. * _scsih_sas_host_add - create sas host object
  4270. * @ioc: per adapter object
  4271. *
  4272. * Creating host side data object, stored in ioc->sas_hba
  4273. *
  4274. * Return nothing.
  4275. */
  4276. static void
  4277. _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
  4278. {
  4279. int i;
  4280. Mpi2ConfigReply_t mpi_reply;
  4281. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  4282. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  4283. Mpi2SasPhyPage0_t phy_pg0;
  4284. Mpi2SasDevicePage0_t sas_device_pg0;
  4285. Mpi2SasEnclosurePage0_t enclosure_pg0;
  4286. u16 ioc_status;
  4287. u16 sz;
  4288. u16 device_missing_delay;
  4289. mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
  4290. if (!ioc->sas_hba.num_phys) {
  4291. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4292. ioc->name, __FILE__, __LINE__, __func__);
  4293. return;
  4294. }
  4295. /* sas_iounit page 0 */
  4296. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  4297. sizeof(Mpi2SasIOUnit0PhyData_t));
  4298. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  4299. if (!sas_iounit_pg0) {
  4300. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4301. ioc->name, __FILE__, __LINE__, __func__);
  4302. return;
  4303. }
  4304. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  4305. sas_iounit_pg0, sz))) {
  4306. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4307. ioc->name, __FILE__, __LINE__, __func__);
  4308. goto out;
  4309. }
  4310. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4311. MPI2_IOCSTATUS_MASK;
  4312. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4313. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4314. ioc->name, __FILE__, __LINE__, __func__);
  4315. goto out;
  4316. }
  4317. /* sas_iounit page 1 */
  4318. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  4319. sizeof(Mpi2SasIOUnit1PhyData_t));
  4320. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  4321. if (!sas_iounit_pg1) {
  4322. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4323. ioc->name, __FILE__, __LINE__, __func__);
  4324. goto out;
  4325. }
  4326. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  4327. sas_iounit_pg1, sz))) {
  4328. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4329. ioc->name, __FILE__, __LINE__, __func__);
  4330. goto out;
  4331. }
  4332. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4333. MPI2_IOCSTATUS_MASK;
  4334. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4335. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4336. ioc->name, __FILE__, __LINE__, __func__);
  4337. goto out;
  4338. }
  4339. ioc->io_missing_delay =
  4340. le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
  4341. device_missing_delay =
  4342. le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
  4343. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  4344. ioc->device_missing_delay = (device_missing_delay &
  4345. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  4346. else
  4347. ioc->device_missing_delay = device_missing_delay &
  4348. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  4349. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  4350. ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
  4351. sizeof(struct _sas_phy), GFP_KERNEL);
  4352. if (!ioc->sas_hba.phy) {
  4353. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4354. ioc->name, __FILE__, __LINE__, __func__);
  4355. goto out;
  4356. }
  4357. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  4358. if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  4359. i))) {
  4360. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4361. ioc->name, __FILE__, __LINE__, __func__);
  4362. goto out;
  4363. }
  4364. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4365. MPI2_IOCSTATUS_MASK;
  4366. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4367. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4368. ioc->name, __FILE__, __LINE__, __func__);
  4369. goto out;
  4370. }
  4371. if (i == 0)
  4372. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  4373. PhyData[0].ControllerDevHandle);
  4374. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  4375. ioc->sas_hba.phy[i].phy_id = i;
  4376. mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  4377. phy_pg0, ioc->sas_hba.parent_dev);
  4378. }
  4379. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4380. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
  4381. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4382. ioc->name, __FILE__, __LINE__, __func__);
  4383. goto out;
  4384. }
  4385. ioc->sas_hba.enclosure_handle =
  4386. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  4387. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4388. printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
  4389. "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
  4390. (unsigned long long) ioc->sas_hba.sas_address,
  4391. ioc->sas_hba.num_phys) ;
  4392. if (ioc->sas_hba.enclosure_handle) {
  4393. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  4394. &enclosure_pg0,
  4395. MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  4396. ioc->sas_hba.enclosure_handle))) {
  4397. ioc->sas_hba.enclosure_logical_id =
  4398. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  4399. }
  4400. }
  4401. out:
  4402. kfree(sas_iounit_pg1);
  4403. kfree(sas_iounit_pg0);
  4404. }
  4405. /**
  4406. * _scsih_expander_add - creating expander object
  4407. * @ioc: per adapter object
  4408. * @handle: expander handle
  4409. *
  4410. * Creating expander object, stored in ioc->sas_expander_list.
  4411. *
  4412. * Return 0 for success, else error.
  4413. */
  4414. static int
  4415. _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  4416. {
  4417. struct _sas_node *sas_expander;
  4418. Mpi2ConfigReply_t mpi_reply;
  4419. Mpi2ExpanderPage0_t expander_pg0;
  4420. Mpi2ExpanderPage1_t expander_pg1;
  4421. Mpi2SasEnclosurePage0_t enclosure_pg0;
  4422. u32 ioc_status;
  4423. u16 parent_handle;
  4424. u64 sas_address, sas_address_parent = 0;
  4425. int i;
  4426. unsigned long flags;
  4427. struct _sas_port *mpt2sas_port = NULL;
  4428. int rc = 0;
  4429. if (!handle)
  4430. return -1;
  4431. if (ioc->shost_recovery || ioc->pci_error_recovery)
  4432. return -1;
  4433. if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  4434. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  4435. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4436. ioc->name, __FILE__, __LINE__, __func__);
  4437. return -1;
  4438. }
  4439. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4440. MPI2_IOCSTATUS_MASK;
  4441. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4442. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4443. ioc->name, __FILE__, __LINE__, __func__);
  4444. return -1;
  4445. }
  4446. /* handle out of order topology events */
  4447. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  4448. if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
  4449. != 0) {
  4450. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4451. ioc->name, __FILE__, __LINE__, __func__);
  4452. return -1;
  4453. }
  4454. if (sas_address_parent != ioc->sas_hba.sas_address) {
  4455. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4456. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  4457. sas_address_parent);
  4458. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4459. if (!sas_expander) {
  4460. rc = _scsih_expander_add(ioc, parent_handle);
  4461. if (rc != 0)
  4462. return rc;
  4463. }
  4464. }
  4465. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4466. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  4467. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  4468. sas_address);
  4469. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4470. if (sas_expander)
  4471. return 0;
  4472. sas_expander = kzalloc(sizeof(struct _sas_node),
  4473. GFP_KERNEL);
  4474. if (!sas_expander) {
  4475. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4476. ioc->name, __FILE__, __LINE__, __func__);
  4477. return -1;
  4478. }
  4479. sas_expander->handle = handle;
  4480. sas_expander->num_phys = expander_pg0.NumPhys;
  4481. sas_expander->sas_address_parent = sas_address_parent;
  4482. sas_expander->sas_address = sas_address;
  4483. printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
  4484. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  4485. handle, parent_handle, (unsigned long long)
  4486. sas_expander->sas_address, sas_expander->num_phys);
  4487. if (!sas_expander->num_phys)
  4488. goto out_fail;
  4489. sas_expander->phy = kcalloc(sas_expander->num_phys,
  4490. sizeof(struct _sas_phy), GFP_KERNEL);
  4491. if (!sas_expander->phy) {
  4492. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4493. ioc->name, __FILE__, __LINE__, __func__);
  4494. rc = -1;
  4495. goto out_fail;
  4496. }
  4497. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  4498. mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
  4499. sas_address_parent);
  4500. if (!mpt2sas_port) {
  4501. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4502. ioc->name, __FILE__, __LINE__, __func__);
  4503. rc = -1;
  4504. goto out_fail;
  4505. }
  4506. sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
  4507. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  4508. if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
  4509. &expander_pg1, i, handle))) {
  4510. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4511. ioc->name, __FILE__, __LINE__, __func__);
  4512. rc = -1;
  4513. goto out_fail;
  4514. }
  4515. sas_expander->phy[i].handle = handle;
  4516. sas_expander->phy[i].phy_id = i;
  4517. if ((mpt2sas_transport_add_expander_phy(ioc,
  4518. &sas_expander->phy[i], expander_pg1,
  4519. sas_expander->parent_dev))) {
  4520. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4521. ioc->name, __FILE__, __LINE__, __func__);
  4522. rc = -1;
  4523. goto out_fail;
  4524. }
  4525. }
  4526. if (sas_expander->enclosure_handle) {
  4527. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  4528. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  4529. sas_expander->enclosure_handle))) {
  4530. sas_expander->enclosure_logical_id =
  4531. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  4532. }
  4533. }
  4534. _scsih_expander_node_add(ioc, sas_expander);
  4535. return 0;
  4536. out_fail:
  4537. if (mpt2sas_port)
  4538. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  4539. sas_address_parent);
  4540. kfree(sas_expander);
  4541. return rc;
  4542. }
  4543. /**
  4544. * _scsih_done - scsih callback handler.
  4545. * @ioc: per adapter object
  4546. * @smid: system request message index
  4547. * @msix_index: MSIX table index supplied by the OS
  4548. * @reply: reply message frame(lower 32bit addr)
  4549. *
  4550. * Callback handler when sending internal generated message frames.
  4551. * The callback index passed is `ioc->scsih_cb_idx`
  4552. *
  4553. * Return 1 meaning mf should be freed from _base_interrupt
  4554. * 0 means the mf is freed from this function.
  4555. */
  4556. static u8
  4557. _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  4558. {
  4559. MPI2DefaultReply_t *mpi_reply;
  4560. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  4561. if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
  4562. return 1;
  4563. if (ioc->scsih_cmds.smid != smid)
  4564. return 1;
  4565. ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
  4566. if (mpi_reply) {
  4567. memcpy(ioc->scsih_cmds.reply, mpi_reply,
  4568. mpi_reply->MsgLength*4);
  4569. ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
  4570. }
  4571. ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
  4572. complete(&ioc->scsih_cmds.done);
  4573. return 1;
  4574. }
  4575. /**
  4576. * mpt2sas_expander_remove - removing expander object
  4577. * @ioc: per adapter object
  4578. * @sas_address: expander sas_address
  4579. *
  4580. * Return nothing.
  4581. */
  4582. void
  4583. mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
  4584. {
  4585. struct _sas_node *sas_expander;
  4586. unsigned long flags;
  4587. if (ioc->shost_recovery)
  4588. return;
  4589. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4590. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  4591. sas_address);
  4592. if (sas_expander)
  4593. list_del(&sas_expander->list);
  4594. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4595. if (sas_expander)
  4596. _scsih_expander_node_remove(ioc, sas_expander);
  4597. }
  4598. /**
  4599. * _scsih_check_access_status - check access flags
  4600. * @ioc: per adapter object
  4601. * @sas_address: sas address
  4602. * @handle: sas device handle
  4603. * @access_flags: errors returned during discovery of the device
  4604. *
  4605. * Return 0 for success, else failure
  4606. */
  4607. static u8
  4608. _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  4609. u16 handle, u8 access_status)
  4610. {
  4611. u8 rc = 1;
  4612. char *desc = NULL;
  4613. switch (access_status) {
  4614. case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
  4615. case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
  4616. rc = 0;
  4617. break;
  4618. case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
  4619. desc = "sata capability failed";
  4620. break;
  4621. case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
  4622. desc = "sata affiliation conflict";
  4623. break;
  4624. case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
  4625. desc = "route not addressable";
  4626. break;
  4627. case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
  4628. desc = "smp error not addressable";
  4629. break;
  4630. case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
  4631. desc = "device blocked";
  4632. break;
  4633. case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
  4634. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
  4635. case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
  4636. case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
  4637. case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
  4638. case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
  4639. case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
  4640. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
  4641. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
  4642. case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
  4643. case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
  4644. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
  4645. desc = "sata initialization failed";
  4646. break;
  4647. default:
  4648. desc = "unknown";
  4649. break;
  4650. }
  4651. if (!rc)
  4652. return 0;
  4653. printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
  4654. "handle(0x%04x)\n", ioc->name, desc,
  4655. (unsigned long long)sas_address, handle);
  4656. return rc;
  4657. }
  4658. static void
  4659. _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  4660. {
  4661. Mpi2ConfigReply_t mpi_reply;
  4662. Mpi2SasDevicePage0_t sas_device_pg0;
  4663. struct _sas_device *sas_device;
  4664. u32 ioc_status;
  4665. unsigned long flags;
  4666. u64 sas_address;
  4667. struct scsi_target *starget;
  4668. struct MPT2SAS_TARGET *sas_target_priv_data;
  4669. u32 device_info;
  4670. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4671. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
  4672. return;
  4673. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  4674. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  4675. return;
  4676. /* check if this is end device */
  4677. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4678. if (!(_scsih_is_end_device(device_info)))
  4679. return;
  4680. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4681. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4682. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4683. sas_address);
  4684. if (!sas_device) {
  4685. printk(MPT2SAS_ERR_FMT "device is not present "
  4686. "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
  4687. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4688. return;
  4689. }
  4690. if (unlikely(sas_device->handle != handle)) {
  4691. starget = sas_device->starget;
  4692. sas_target_priv_data = starget->hostdata;
  4693. starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
  4694. " to (0x%04x)!!!\n", sas_device->handle, handle);
  4695. sas_target_priv_data->handle = handle;
  4696. sas_device->handle = handle;
  4697. }
  4698. /* check if device is present */
  4699. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  4700. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  4701. printk(MPT2SAS_ERR_FMT "device is not present "
  4702. "handle(0x%04x), flags!!!\n", ioc->name, handle);
  4703. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4704. return;
  4705. }
  4706. /* check if there were any issues with discovery */
  4707. if (_scsih_check_access_status(ioc, sas_address, handle,
  4708. sas_device_pg0.AccessStatus)) {
  4709. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4710. return;
  4711. }
  4712. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4713. _scsih_ublock_io_device(ioc, sas_address);
  4714. }
  4715. /**
  4716. * _scsih_add_device - creating sas device object
  4717. * @ioc: per adapter object
  4718. * @handle: sas device handle
  4719. * @phy_num: phy number end device attached to
  4720. * @is_pd: is this hidden raid component
  4721. *
  4722. * Creating end device object, stored in ioc->sas_device_list.
  4723. *
  4724. * Returns 0 for success, non-zero for failure.
  4725. */
  4726. static int
  4727. _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
  4728. {
  4729. Mpi2ConfigReply_t mpi_reply;
  4730. Mpi2SasDevicePage0_t sas_device_pg0;
  4731. Mpi2SasEnclosurePage0_t enclosure_pg0;
  4732. struct _sas_device *sas_device;
  4733. u32 ioc_status;
  4734. __le64 sas_address;
  4735. u32 device_info;
  4736. unsigned long flags;
  4737. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4738. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  4739. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4740. ioc->name, __FILE__, __LINE__, __func__);
  4741. return -1;
  4742. }
  4743. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4744. MPI2_IOCSTATUS_MASK;
  4745. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4746. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4747. ioc->name, __FILE__, __LINE__, __func__);
  4748. return -1;
  4749. }
  4750. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4751. /* check if device is present */
  4752. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  4753. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  4754. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4755. ioc->name, __FILE__, __LINE__, __func__);
  4756. printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
  4757. ioc->name, le16_to_cpu(sas_device_pg0.Flags));
  4758. return -1;
  4759. }
  4760. /* check if there were any issues with discovery */
  4761. if (_scsih_check_access_status(ioc, sas_address, handle,
  4762. sas_device_pg0.AccessStatus))
  4763. return -1;
  4764. /* check if this is end device */
  4765. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4766. if (!(_scsih_is_end_device(device_info))) {
  4767. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4768. ioc->name, __FILE__, __LINE__, __func__);
  4769. return -1;
  4770. }
  4771. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4772. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4773. sas_address);
  4774. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4775. if (sas_device)
  4776. return 0;
  4777. sas_device = kzalloc(sizeof(struct _sas_device),
  4778. GFP_KERNEL);
  4779. if (!sas_device) {
  4780. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4781. ioc->name, __FILE__, __LINE__, __func__);
  4782. return -1;
  4783. }
  4784. sas_device->handle = handle;
  4785. if (_scsih_get_sas_address(ioc, le16_to_cpu
  4786. (sas_device_pg0.ParentDevHandle),
  4787. &sas_device->sas_address_parent) != 0)
  4788. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4789. ioc->name, __FILE__, __LINE__, __func__);
  4790. sas_device->enclosure_handle =
  4791. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  4792. sas_device->slot =
  4793. le16_to_cpu(sas_device_pg0.Slot);
  4794. sas_device->device_info = device_info;
  4795. sas_device->sas_address = sas_address;
  4796. sas_device->phy = sas_device_pg0.PhyNum;
  4797. /* get enclosure_logical_id */
  4798. if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
  4799. ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  4800. sas_device->enclosure_handle)))
  4801. sas_device->enclosure_logical_id =
  4802. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  4803. /* get device name */
  4804. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  4805. if (ioc->wait_for_discovery_to_complete)
  4806. _scsih_sas_device_init_add(ioc, sas_device);
  4807. else
  4808. _scsih_sas_device_add(ioc, sas_device);
  4809. return 0;
  4810. }
  4811. /**
  4812. * _scsih_remove_device - removing sas device object
  4813. * @ioc: per adapter object
  4814. * @sas_device_delete: the sas_device object
  4815. *
  4816. * Return nothing.
  4817. */
  4818. static void
  4819. _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
  4820. struct _sas_device *sas_device)
  4821. {
  4822. struct MPT2SAS_TARGET *sas_target_priv_data;
  4823. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
  4824. "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  4825. sas_device->handle, (unsigned long long)
  4826. sas_device->sas_address));
  4827. if (sas_device->starget && sas_device->starget->hostdata) {
  4828. sas_target_priv_data = sas_device->starget->hostdata;
  4829. sas_target_priv_data->deleted = 1;
  4830. _scsih_ublock_io_device(ioc, sas_device->sas_address);
  4831. sas_target_priv_data->handle =
  4832. MPT2SAS_INVALID_DEVICE_HANDLE;
  4833. }
  4834. if (!ioc->hide_drives)
  4835. mpt2sas_transport_port_remove(ioc,
  4836. sas_device->sas_address,
  4837. sas_device->sas_address_parent);
  4838. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
  4839. "(0x%016llx)\n", ioc->name, sas_device->handle,
  4840. (unsigned long long) sas_device->sas_address);
  4841. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
  4842. "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  4843. sas_device->handle, (unsigned long long)
  4844. sas_device->sas_address));
  4845. kfree(sas_device);
  4846. }
  4847. /**
  4848. * _scsih_device_remove_by_handle - removing device object by handle
  4849. * @ioc: per adapter object
  4850. * @handle: device handle
  4851. *
  4852. * Return nothing.
  4853. */
  4854. static void
  4855. _scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  4856. {
  4857. struct _sas_device *sas_device;
  4858. unsigned long flags;
  4859. if (ioc->shost_recovery)
  4860. return;
  4861. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4862. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4863. if (sas_device)
  4864. list_del(&sas_device->list);
  4865. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4866. if (sas_device)
  4867. _scsih_remove_device(ioc, sas_device);
  4868. }
  4869. /**
  4870. * mpt2sas_device_remove_by_sas_address - removing device object by sas address
  4871. * @ioc: per adapter object
  4872. * @sas_address: device sas_address
  4873. *
  4874. * Return nothing.
  4875. */
  4876. void
  4877. mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  4878. u64 sas_address)
  4879. {
  4880. struct _sas_device *sas_device;
  4881. unsigned long flags;
  4882. if (ioc->shost_recovery)
  4883. return;
  4884. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4885. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4886. sas_address);
  4887. if (sas_device)
  4888. list_del(&sas_device->list);
  4889. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4890. if (sas_device)
  4891. _scsih_remove_device(ioc, sas_device);
  4892. }
  4893. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4894. /**
  4895. * _scsih_sas_topology_change_event_debug - debug for topology event
  4896. * @ioc: per adapter object
  4897. * @event_data: event data payload
  4898. * Context: user.
  4899. */
  4900. static void
  4901. _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4902. Mpi2EventDataSasTopologyChangeList_t *event_data)
  4903. {
  4904. int i;
  4905. u16 handle;
  4906. u16 reason_code;
  4907. u8 phy_number;
  4908. char *status_str = NULL;
  4909. u8 link_rate, prev_link_rate;
  4910. switch (event_data->ExpStatus) {
  4911. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  4912. status_str = "add";
  4913. break;
  4914. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  4915. status_str = "remove";
  4916. break;
  4917. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  4918. case 0:
  4919. status_str = "responding";
  4920. break;
  4921. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  4922. status_str = "remove delay";
  4923. break;
  4924. default:
  4925. status_str = "unknown status";
  4926. break;
  4927. }
  4928. printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
  4929. ioc->name, status_str);
  4930. printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
  4931. "start_phy(%02d), count(%d)\n",
  4932. le16_to_cpu(event_data->ExpanderDevHandle),
  4933. le16_to_cpu(event_data->EnclosureHandle),
  4934. event_data->StartPhyNum, event_data->NumEntries);
  4935. for (i = 0; i < event_data->NumEntries; i++) {
  4936. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  4937. if (!handle)
  4938. continue;
  4939. phy_number = event_data->StartPhyNum + i;
  4940. reason_code = event_data->PHY[i].PhyStatus &
  4941. MPI2_EVENT_SAS_TOPO_RC_MASK;
  4942. switch (reason_code) {
  4943. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  4944. status_str = "target add";
  4945. break;
  4946. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  4947. status_str = "target remove";
  4948. break;
  4949. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  4950. status_str = "delay target remove";
  4951. break;
  4952. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  4953. status_str = "link rate change";
  4954. break;
  4955. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  4956. status_str = "target responding";
  4957. break;
  4958. default:
  4959. status_str = "unknown";
  4960. break;
  4961. }
  4962. link_rate = event_data->PHY[i].LinkRate >> 4;
  4963. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  4964. printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
  4965. " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
  4966. handle, status_str, link_rate, prev_link_rate);
  4967. }
  4968. }
  4969. #endif
  4970. /**
  4971. * _scsih_sas_topology_change_event - handle topology changes
  4972. * @ioc: per adapter object
  4973. * @fw_event: The fw_event_work object
  4974. * Context: user.
  4975. *
  4976. */
  4977. static void
  4978. _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
  4979. struct fw_event_work *fw_event)
  4980. {
  4981. int i;
  4982. u16 parent_handle, handle;
  4983. u16 reason_code;
  4984. u8 phy_number, max_phys;
  4985. struct _sas_node *sas_expander;
  4986. u64 sas_address;
  4987. unsigned long flags;
  4988. u8 link_rate, prev_link_rate;
  4989. Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
  4990. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4991. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4992. _scsih_sas_topology_change_event_debug(ioc, event_data);
  4993. #endif
  4994. if (ioc->remove_host || ioc->pci_error_recovery)
  4995. return;
  4996. if (!ioc->sas_hba.num_phys)
  4997. _scsih_sas_host_add(ioc);
  4998. else
  4999. _scsih_sas_host_refresh(ioc);
  5000. if (fw_event->ignore) {
  5001. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
  5002. "event\n", ioc->name));
  5003. return;
  5004. }
  5005. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  5006. /* handle expander add */
  5007. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  5008. if (_scsih_expander_add(ioc, parent_handle) != 0)
  5009. return;
  5010. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5011. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  5012. parent_handle);
  5013. if (sas_expander) {
  5014. sas_address = sas_expander->sas_address;
  5015. max_phys = sas_expander->num_phys;
  5016. } else if (parent_handle < ioc->sas_hba.num_phys) {
  5017. sas_address = ioc->sas_hba.sas_address;
  5018. max_phys = ioc->sas_hba.num_phys;
  5019. } else {
  5020. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5021. return;
  5022. }
  5023. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5024. /* handle siblings events */
  5025. for (i = 0; i < event_data->NumEntries; i++) {
  5026. if (fw_event->ignore) {
  5027. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
  5028. "expander event\n", ioc->name));
  5029. return;
  5030. }
  5031. if (ioc->shost_recovery || ioc->remove_host ||
  5032. ioc->pci_error_recovery)
  5033. return;
  5034. phy_number = event_data->StartPhyNum + i;
  5035. if (phy_number >= max_phys)
  5036. continue;
  5037. reason_code = event_data->PHY[i].PhyStatus &
  5038. MPI2_EVENT_SAS_TOPO_RC_MASK;
  5039. if ((event_data->PHY[i].PhyStatus &
  5040. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
  5041. MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
  5042. continue;
  5043. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  5044. if (!handle)
  5045. continue;
  5046. link_rate = event_data->PHY[i].LinkRate >> 4;
  5047. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  5048. switch (reason_code) {
  5049. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  5050. if (ioc->shost_recovery)
  5051. break;
  5052. if (link_rate == prev_link_rate)
  5053. break;
  5054. mpt2sas_transport_update_links(ioc, sas_address,
  5055. handle, phy_number, link_rate);
  5056. if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  5057. break;
  5058. _scsih_check_device(ioc, handle);
  5059. break;
  5060. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  5061. if (ioc->shost_recovery)
  5062. break;
  5063. mpt2sas_transport_update_links(ioc, sas_address,
  5064. handle, phy_number, link_rate);
  5065. _scsih_add_device(ioc, handle, phy_number, 0);
  5066. break;
  5067. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  5068. _scsih_device_remove_by_handle(ioc, handle);
  5069. break;
  5070. }
  5071. }
  5072. /* handle expander removal */
  5073. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
  5074. sas_expander)
  5075. mpt2sas_expander_remove(ioc, sas_address);
  5076. }
  5077. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5078. /**
  5079. * _scsih_sas_device_status_change_event_debug - debug for device event
  5080. * @event_data: event data payload
  5081. * Context: user.
  5082. *
  5083. * Return nothing.
  5084. */
  5085. static void
  5086. _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  5087. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  5088. {
  5089. char *reason_str = NULL;
  5090. switch (event_data->ReasonCode) {
  5091. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  5092. reason_str = "smart data";
  5093. break;
  5094. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  5095. reason_str = "unsupported device discovered";
  5096. break;
  5097. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  5098. reason_str = "internal device reset";
  5099. break;
  5100. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  5101. reason_str = "internal task abort";
  5102. break;
  5103. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  5104. reason_str = "internal task abort set";
  5105. break;
  5106. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  5107. reason_str = "internal clear task set";
  5108. break;
  5109. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  5110. reason_str = "internal query task";
  5111. break;
  5112. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  5113. reason_str = "sata init failure";
  5114. break;
  5115. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  5116. reason_str = "internal device reset complete";
  5117. break;
  5118. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  5119. reason_str = "internal task abort complete";
  5120. break;
  5121. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  5122. reason_str = "internal async notification";
  5123. break;
  5124. case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
  5125. reason_str = "expander reduced functionality";
  5126. break;
  5127. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
  5128. reason_str = "expander reduced functionality complete";
  5129. break;
  5130. default:
  5131. reason_str = "unknown reason";
  5132. break;
  5133. }
  5134. printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
  5135. "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
  5136. ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
  5137. (unsigned long long)le64_to_cpu(event_data->SASAddress),
  5138. le16_to_cpu(event_data->TaskTag));
  5139. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  5140. printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  5141. event_data->ASC, event_data->ASCQ);
  5142. printk(KERN_INFO "\n");
  5143. }
  5144. #endif
  5145. /**
  5146. * _scsih_sas_device_status_change_event - handle device status change
  5147. * @ioc: per adapter object
  5148. * @fw_event: The fw_event_work object
  5149. * Context: user.
  5150. *
  5151. * Return nothing.
  5152. */
  5153. static void
  5154. _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  5155. struct fw_event_work *fw_event)
  5156. {
  5157. struct MPT2SAS_TARGET *target_priv_data;
  5158. struct _sas_device *sas_device;
  5159. u64 sas_address;
  5160. unsigned long flags;
  5161. Mpi2EventDataSasDeviceStatusChange_t *event_data =
  5162. fw_event->event_data;
  5163. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5164. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5165. _scsih_sas_device_status_change_event_debug(ioc,
  5166. event_data);
  5167. #endif
  5168. /* In MPI Revision K (0xC), the internal device reset complete was
  5169. * implemented, so avoid setting tm_busy flag for older firmware.
  5170. */
  5171. if ((ioc->facts.HeaderVersion >> 8) < 0xC)
  5172. return;
  5173. if (event_data->ReasonCode !=
  5174. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  5175. event_data->ReasonCode !=
  5176. MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
  5177. return;
  5178. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5179. sas_address = le64_to_cpu(event_data->SASAddress);
  5180. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  5181. sas_address);
  5182. if (!sas_device || !sas_device->starget) {
  5183. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5184. return;
  5185. }
  5186. target_priv_data = sas_device->starget->hostdata;
  5187. if (!target_priv_data) {
  5188. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5189. return;
  5190. }
  5191. if (event_data->ReasonCode ==
  5192. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
  5193. target_priv_data->tm_busy = 1;
  5194. else
  5195. target_priv_data->tm_busy = 0;
  5196. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5197. }
  5198. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5199. /**
  5200. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
  5201. * @ioc: per adapter object
  5202. * @event_data: event data payload
  5203. * Context: user.
  5204. *
  5205. * Return nothing.
  5206. */
  5207. static void
  5208. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  5209. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  5210. {
  5211. char *reason_str = NULL;
  5212. switch (event_data->ReasonCode) {
  5213. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  5214. reason_str = "enclosure add";
  5215. break;
  5216. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  5217. reason_str = "enclosure remove";
  5218. break;
  5219. default:
  5220. reason_str = "unknown reason";
  5221. break;
  5222. }
  5223. printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
  5224. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  5225. " number slots(%d)\n", ioc->name, reason_str,
  5226. le16_to_cpu(event_data->EnclosureHandle),
  5227. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  5228. le16_to_cpu(event_data->StartSlot));
  5229. }
  5230. #endif
  5231. /**
  5232. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  5233. * @ioc: per adapter object
  5234. * @fw_event: The fw_event_work object
  5235. * Context: user.
  5236. *
  5237. * Return nothing.
  5238. */
  5239. static void
  5240. _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  5241. struct fw_event_work *fw_event)
  5242. {
  5243. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5244. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5245. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  5246. fw_event->event_data);
  5247. #endif
  5248. }
  5249. /**
  5250. * _scsih_sas_broadcast_primitive_event - handle broadcast events
  5251. * @ioc: per adapter object
  5252. * @fw_event: The fw_event_work object
  5253. * Context: user.
  5254. *
  5255. * Return nothing.
  5256. */
  5257. static void
  5258. _scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
  5259. struct fw_event_work *fw_event)
  5260. {
  5261. struct scsi_cmnd *scmd;
  5262. struct scsi_device *sdev;
  5263. u16 smid, handle;
  5264. u32 lun;
  5265. struct MPT2SAS_DEVICE *sas_device_priv_data;
  5266. u32 termination_count;
  5267. u32 query_count;
  5268. Mpi2SCSITaskManagementReply_t *mpi_reply;
  5269. Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
  5270. u16 ioc_status;
  5271. unsigned long flags;
  5272. int r;
  5273. u8 max_retries = 0;
  5274. u8 task_abort_retries;
  5275. mutex_lock(&ioc->tm_cmds.mutex);
  5276. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
  5277. "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
  5278. event_data->PortWidth));
  5279. _scsih_block_io_all_device(ioc);
  5280. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5281. mpi_reply = ioc->tm_cmds.reply;
  5282. broadcast_aen_retry:
  5283. /* sanity checks for retrying this loop */
  5284. if (max_retries++ == 5) {
  5285. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
  5286. ioc->name, __func__));
  5287. goto out;
  5288. } else if (max_retries > 1)
  5289. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
  5290. ioc->name, __func__, max_retries - 1));
  5291. termination_count = 0;
  5292. query_count = 0;
  5293. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  5294. if (ioc->shost_recovery)
  5295. goto out;
  5296. scmd = _scsih_scsi_lookup_get(ioc, smid);
  5297. if (!scmd)
  5298. continue;
  5299. sdev = scmd->device;
  5300. sas_device_priv_data = sdev->hostdata;
  5301. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  5302. continue;
  5303. /* skip hidden raid components */
  5304. if (sas_device_priv_data->sas_target->flags &
  5305. MPT_TARGET_FLAGS_RAID_COMPONENT)
  5306. continue;
  5307. /* skip volumes */
  5308. if (sas_device_priv_data->sas_target->flags &
  5309. MPT_TARGET_FLAGS_VOLUME)
  5310. continue;
  5311. handle = sas_device_priv_data->sas_target->handle;
  5312. lun = sas_device_priv_data->lun;
  5313. query_count++;
  5314. if (ioc->shost_recovery)
  5315. goto out;
  5316. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  5317. r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
  5318. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
  5319. TM_MUTEX_OFF);
  5320. if (r == FAILED) {
  5321. sdev_printk(KERN_WARNING, sdev,
  5322. "mpt2sas_scsih_issue_tm: FAILED when sending "
  5323. "QUERY_TASK: scmd(%p)\n", scmd);
  5324. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5325. goto broadcast_aen_retry;
  5326. }
  5327. ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
  5328. & MPI2_IOCSTATUS_MASK;
  5329. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5330. sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
  5331. "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
  5332. scmd);
  5333. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5334. goto broadcast_aen_retry;
  5335. }
  5336. /* see if IO is still owned by IOC and target */
  5337. if (mpi_reply->ResponseCode ==
  5338. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  5339. mpi_reply->ResponseCode ==
  5340. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
  5341. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5342. continue;
  5343. }
  5344. task_abort_retries = 0;
  5345. tm_retry:
  5346. if (task_abort_retries++ == 60) {
  5347. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  5348. "%s: ABORT_TASK: giving up\n", ioc->name,
  5349. __func__));
  5350. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5351. goto broadcast_aen_retry;
  5352. }
  5353. if (ioc->shost_recovery)
  5354. goto out_no_lock;
  5355. r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
  5356. sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
  5357. scmd->serial_number, TM_MUTEX_OFF);
  5358. if (r == FAILED) {
  5359. sdev_printk(KERN_WARNING, sdev,
  5360. "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
  5361. "scmd(%p)\n", scmd);
  5362. goto tm_retry;
  5363. }
  5364. if (task_abort_retries > 1)
  5365. sdev_printk(KERN_WARNING, sdev,
  5366. "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
  5367. " scmd(%p)\n",
  5368. task_abort_retries - 1, scmd);
  5369. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  5370. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5371. }
  5372. if (ioc->broadcast_aen_pending) {
  5373. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
  5374. " pending AEN\n", ioc->name, __func__));
  5375. ioc->broadcast_aen_pending = 0;
  5376. goto broadcast_aen_retry;
  5377. }
  5378. out:
  5379. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  5380. out_no_lock:
  5381. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  5382. "%s - exit, query_count = %d termination_count = %d\n",
  5383. ioc->name, __func__, query_count, termination_count));
  5384. ioc->broadcast_aen_busy = 0;
  5385. if (!ioc->shost_recovery)
  5386. _scsih_ublock_io_all_device(ioc);
  5387. mutex_unlock(&ioc->tm_cmds.mutex);
  5388. }
  5389. /**
  5390. * _scsih_sas_discovery_event - handle discovery events
  5391. * @ioc: per adapter object
  5392. * @fw_event: The fw_event_work object
  5393. * Context: user.
  5394. *
  5395. * Return nothing.
  5396. */
  5397. static void
  5398. _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
  5399. struct fw_event_work *fw_event)
  5400. {
  5401. Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
  5402. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5403. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  5404. printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
  5405. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  5406. "start" : "stop");
  5407. if (event_data->DiscoveryStatus)
  5408. printk("discovery_status(0x%08x)",
  5409. le32_to_cpu(event_data->DiscoveryStatus));
  5410. printk("\n");
  5411. }
  5412. #endif
  5413. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  5414. !ioc->sas_hba.num_phys) {
  5415. if (disable_discovery > 0 && ioc->shost_recovery) {
  5416. /* Wait for the reset to complete */
  5417. while (ioc->shost_recovery)
  5418. ssleep(1);
  5419. }
  5420. _scsih_sas_host_add(ioc);
  5421. }
  5422. }
  5423. /**
  5424. * _scsih_reprobe_lun - reprobing lun
  5425. * @sdev: scsi device struct
  5426. * @no_uld_attach: sdev->no_uld_attach flag setting
  5427. *
  5428. **/
  5429. static void
  5430. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  5431. {
  5432. int rc;
  5433. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  5434. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  5435. sdev->no_uld_attach ? "hidding" : "exposing");
  5436. rc = scsi_device_reprobe(sdev);
  5437. }
  5438. /**
  5439. * _scsih_sas_volume_add - add new volume
  5440. * @ioc: per adapter object
  5441. * @element: IR config element data
  5442. * Context: user.
  5443. *
  5444. * Return nothing.
  5445. */
  5446. static void
  5447. _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
  5448. Mpi2EventIrConfigElement_t *element)
  5449. {
  5450. struct _raid_device *raid_device;
  5451. unsigned long flags;
  5452. u64 wwid;
  5453. u16 handle = le16_to_cpu(element->VolDevHandle);
  5454. int rc;
  5455. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  5456. if (!wwid) {
  5457. printk(MPT2SAS_ERR_FMT
  5458. "failure at %s:%d/%s()!\n", ioc->name,
  5459. __FILE__, __LINE__, __func__);
  5460. return;
  5461. }
  5462. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5463. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  5464. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5465. if (raid_device)
  5466. return;
  5467. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  5468. if (!raid_device) {
  5469. printk(MPT2SAS_ERR_FMT
  5470. "failure at %s:%d/%s()!\n", ioc->name,
  5471. __FILE__, __LINE__, __func__);
  5472. return;
  5473. }
  5474. raid_device->id = ioc->sas_id++;
  5475. raid_device->channel = RAID_CHANNEL;
  5476. raid_device->handle = handle;
  5477. raid_device->wwid = wwid;
  5478. _scsih_raid_device_add(ioc, raid_device);
  5479. if (!ioc->wait_for_discovery_to_complete) {
  5480. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5481. raid_device->id, 0);
  5482. if (rc)
  5483. _scsih_raid_device_remove(ioc, raid_device);
  5484. } else {
  5485. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5486. _scsih_determine_boot_device(ioc, raid_device, 1);
  5487. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5488. }
  5489. }
  5490. /**
  5491. * _scsih_sas_volume_delete - delete volume
  5492. * @ioc: per adapter object
  5493. * @handle: volume device handle
  5494. * Context: user.
  5495. *
  5496. * Return nothing.
  5497. */
  5498. static void
  5499. _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  5500. {
  5501. struct _raid_device *raid_device;
  5502. unsigned long flags;
  5503. struct MPT2SAS_TARGET *sas_target_priv_data;
  5504. struct scsi_target *starget = NULL;
  5505. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5506. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  5507. if (raid_device) {
  5508. if (raid_device->starget) {
  5509. starget = raid_device->starget;
  5510. sas_target_priv_data = starget->hostdata;
  5511. sas_target_priv_data->deleted = 1;
  5512. }
  5513. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
  5514. "(0x%016llx)\n", ioc->name, raid_device->handle,
  5515. (unsigned long long) raid_device->wwid);
  5516. list_del(&raid_device->list);
  5517. kfree(raid_device);
  5518. }
  5519. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5520. if (starget)
  5521. scsi_remove_target(&starget->dev);
  5522. }
  5523. /**
  5524. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  5525. * @ioc: per adapter object
  5526. * @element: IR config element data
  5527. * Context: user.
  5528. *
  5529. * Return nothing.
  5530. */
  5531. static void
  5532. _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
  5533. Mpi2EventIrConfigElement_t *element)
  5534. {
  5535. struct _sas_device *sas_device;
  5536. struct scsi_target *starget = NULL;
  5537. struct MPT2SAS_TARGET *sas_target_priv_data;
  5538. unsigned long flags;
  5539. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5540. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5541. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  5542. if (sas_device) {
  5543. sas_device->volume_handle = 0;
  5544. sas_device->volume_wwid = 0;
  5545. clear_bit(handle, ioc->pd_handles);
  5546. if (sas_device->starget && sas_device->starget->hostdata) {
  5547. starget = sas_device->starget;
  5548. sas_target_priv_data = starget->hostdata;
  5549. sas_target_priv_data->flags &=
  5550. ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  5551. }
  5552. }
  5553. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5554. if (!sas_device)
  5555. return;
  5556. /* exposing raid component */
  5557. if (starget)
  5558. starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
  5559. }
  5560. /**
  5561. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  5562. * @ioc: per adapter object
  5563. * @element: IR config element data
  5564. * Context: user.
  5565. *
  5566. * Return nothing.
  5567. */
  5568. static void
  5569. _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
  5570. Mpi2EventIrConfigElement_t *element)
  5571. {
  5572. struct _sas_device *sas_device;
  5573. struct scsi_target *starget = NULL;
  5574. struct MPT2SAS_TARGET *sas_target_priv_data;
  5575. unsigned long flags;
  5576. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5577. u16 volume_handle = 0;
  5578. u64 volume_wwid = 0;
  5579. mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
  5580. if (volume_handle)
  5581. mpt2sas_config_get_volume_wwid(ioc, volume_handle,
  5582. &volume_wwid);
  5583. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5584. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  5585. if (sas_device) {
  5586. set_bit(handle, ioc->pd_handles);
  5587. if (sas_device->starget && sas_device->starget->hostdata) {
  5588. starget = sas_device->starget;
  5589. sas_target_priv_data = starget->hostdata;
  5590. sas_target_priv_data->flags |=
  5591. MPT_TARGET_FLAGS_RAID_COMPONENT;
  5592. sas_device->volume_handle = volume_handle;
  5593. sas_device->volume_wwid = volume_wwid;
  5594. }
  5595. }
  5596. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5597. if (!sas_device)
  5598. return;
  5599. /* hiding raid component */
  5600. if (starget)
  5601. starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
  5602. }
  5603. /**
  5604. * _scsih_sas_pd_delete - delete pd component
  5605. * @ioc: per adapter object
  5606. * @element: IR config element data
  5607. * Context: user.
  5608. *
  5609. * Return nothing.
  5610. */
  5611. static void
  5612. _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
  5613. Mpi2EventIrConfigElement_t *element)
  5614. {
  5615. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5616. _scsih_device_remove_by_handle(ioc, handle);
  5617. }
  5618. /**
  5619. * _scsih_sas_pd_add - remove pd component
  5620. * @ioc: per adapter object
  5621. * @element: IR config element data
  5622. * Context: user.
  5623. *
  5624. * Return nothing.
  5625. */
  5626. static void
  5627. _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
  5628. Mpi2EventIrConfigElement_t *element)
  5629. {
  5630. struct _sas_device *sas_device;
  5631. unsigned long flags;
  5632. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5633. Mpi2ConfigReply_t mpi_reply;
  5634. Mpi2SasDevicePage0_t sas_device_pg0;
  5635. u32 ioc_status;
  5636. u64 sas_address;
  5637. u16 parent_handle;
  5638. set_bit(handle, ioc->pd_handles);
  5639. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5640. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  5641. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5642. if (sas_device)
  5643. return;
  5644. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  5645. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  5646. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5647. ioc->name, __FILE__, __LINE__, __func__);
  5648. return;
  5649. }
  5650. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5651. MPI2_IOCSTATUS_MASK;
  5652. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5653. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5654. ioc->name, __FILE__, __LINE__, __func__);
  5655. return;
  5656. }
  5657. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  5658. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  5659. mpt2sas_transport_update_links(ioc, sas_address, handle,
  5660. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  5661. _scsih_add_device(ioc, handle, 0, 1);
  5662. }
  5663. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5664. /**
  5665. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  5666. * @ioc: per adapter object
  5667. * @event_data: event data payload
  5668. * Context: user.
  5669. *
  5670. * Return nothing.
  5671. */
  5672. static void
  5673. _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  5674. Mpi2EventDataIrConfigChangeList_t *event_data)
  5675. {
  5676. Mpi2EventIrConfigElement_t *element;
  5677. u8 element_type;
  5678. int i;
  5679. char *reason_str = NULL, *element_str = NULL;
  5680. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  5681. printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
  5682. ioc->name, (le32_to_cpu(event_data->Flags) &
  5683. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  5684. "foreign" : "native", event_data->NumElements);
  5685. for (i = 0; i < event_data->NumElements; i++, element++) {
  5686. switch (element->ReasonCode) {
  5687. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  5688. reason_str = "add";
  5689. break;
  5690. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  5691. reason_str = "remove";
  5692. break;
  5693. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  5694. reason_str = "no change";
  5695. break;
  5696. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  5697. reason_str = "hide";
  5698. break;
  5699. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  5700. reason_str = "unhide";
  5701. break;
  5702. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  5703. reason_str = "volume_created";
  5704. break;
  5705. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  5706. reason_str = "volume_deleted";
  5707. break;
  5708. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  5709. reason_str = "pd_created";
  5710. break;
  5711. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  5712. reason_str = "pd_deleted";
  5713. break;
  5714. default:
  5715. reason_str = "unknown reason";
  5716. break;
  5717. }
  5718. element_type = le16_to_cpu(element->ElementFlags) &
  5719. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  5720. switch (element_type) {
  5721. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  5722. element_str = "volume";
  5723. break;
  5724. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  5725. element_str = "phys disk";
  5726. break;
  5727. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  5728. element_str = "hot spare";
  5729. break;
  5730. default:
  5731. element_str = "unknown element";
  5732. break;
  5733. }
  5734. printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
  5735. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  5736. reason_str, le16_to_cpu(element->VolDevHandle),
  5737. le16_to_cpu(element->PhysDiskDevHandle),
  5738. element->PhysDiskNum);
  5739. }
  5740. }
  5741. #endif
  5742. /**
  5743. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  5744. * @ioc: per adapter object
  5745. * @fw_event: The fw_event_work object
  5746. * Context: user.
  5747. *
  5748. * Return nothing.
  5749. */
  5750. static void
  5751. _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
  5752. struct fw_event_work *fw_event)
  5753. {
  5754. Mpi2EventIrConfigElement_t *element;
  5755. int i;
  5756. u8 foreign_config;
  5757. Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
  5758. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5759. if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5760. && !ioc->hide_ir_msg)
  5761. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  5762. #endif
  5763. if (ioc->shost_recovery)
  5764. return;
  5765. foreign_config = (le32_to_cpu(event_data->Flags) &
  5766. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
  5767. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  5768. for (i = 0; i < event_data->NumElements; i++, element++) {
  5769. switch (element->ReasonCode) {
  5770. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  5771. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  5772. if (!foreign_config)
  5773. _scsih_sas_volume_add(ioc, element);
  5774. break;
  5775. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  5776. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  5777. if (!foreign_config)
  5778. _scsih_sas_volume_delete(ioc,
  5779. le16_to_cpu(element->VolDevHandle));
  5780. break;
  5781. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  5782. if (!ioc->is_warpdrive)
  5783. _scsih_sas_pd_hide(ioc, element);
  5784. break;
  5785. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  5786. if (!ioc->is_warpdrive)
  5787. _scsih_sas_pd_expose(ioc, element);
  5788. break;
  5789. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  5790. if (!ioc->is_warpdrive)
  5791. _scsih_sas_pd_add(ioc, element);
  5792. break;
  5793. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  5794. if (!ioc->is_warpdrive)
  5795. _scsih_sas_pd_delete(ioc, element);
  5796. break;
  5797. }
  5798. }
  5799. }
  5800. /**
  5801. * _scsih_sas_ir_volume_event - IR volume event
  5802. * @ioc: per adapter object
  5803. * @fw_event: The fw_event_work object
  5804. * Context: user.
  5805. *
  5806. * Return nothing.
  5807. */
  5808. static void
  5809. _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
  5810. struct fw_event_work *fw_event)
  5811. {
  5812. u64 wwid;
  5813. unsigned long flags;
  5814. struct _raid_device *raid_device;
  5815. u16 handle;
  5816. u32 state;
  5817. int rc;
  5818. Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
  5819. if (ioc->shost_recovery)
  5820. return;
  5821. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  5822. return;
  5823. handle = le16_to_cpu(event_data->VolDevHandle);
  5824. state = le32_to_cpu(event_data->NewValue);
  5825. if (!ioc->hide_ir_msg)
  5826. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
  5827. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  5828. le32_to_cpu(event_data->PreviousValue), state));
  5829. switch (state) {
  5830. case MPI2_RAID_VOL_STATE_MISSING:
  5831. case MPI2_RAID_VOL_STATE_FAILED:
  5832. _scsih_sas_volume_delete(ioc, handle);
  5833. break;
  5834. case MPI2_RAID_VOL_STATE_ONLINE:
  5835. case MPI2_RAID_VOL_STATE_DEGRADED:
  5836. case MPI2_RAID_VOL_STATE_OPTIMAL:
  5837. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5838. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  5839. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5840. if (raid_device)
  5841. break;
  5842. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  5843. if (!wwid) {
  5844. printk(MPT2SAS_ERR_FMT
  5845. "failure at %s:%d/%s()!\n", ioc->name,
  5846. __FILE__, __LINE__, __func__);
  5847. break;
  5848. }
  5849. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  5850. if (!raid_device) {
  5851. printk(MPT2SAS_ERR_FMT
  5852. "failure at %s:%d/%s()!\n", ioc->name,
  5853. __FILE__, __LINE__, __func__);
  5854. break;
  5855. }
  5856. raid_device->id = ioc->sas_id++;
  5857. raid_device->channel = RAID_CHANNEL;
  5858. raid_device->handle = handle;
  5859. raid_device->wwid = wwid;
  5860. _scsih_raid_device_add(ioc, raid_device);
  5861. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5862. raid_device->id, 0);
  5863. if (rc)
  5864. _scsih_raid_device_remove(ioc, raid_device);
  5865. break;
  5866. case MPI2_RAID_VOL_STATE_INITIALIZING:
  5867. default:
  5868. break;
  5869. }
  5870. }
  5871. /**
  5872. * _scsih_sas_ir_physical_disk_event - PD event
  5873. * @ioc: per adapter object
  5874. * @fw_event: The fw_event_work object
  5875. * Context: user.
  5876. *
  5877. * Return nothing.
  5878. */
  5879. static void
  5880. _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
  5881. struct fw_event_work *fw_event)
  5882. {
  5883. u16 handle, parent_handle;
  5884. u32 state;
  5885. struct _sas_device *sas_device;
  5886. unsigned long flags;
  5887. Mpi2ConfigReply_t mpi_reply;
  5888. Mpi2SasDevicePage0_t sas_device_pg0;
  5889. u32 ioc_status;
  5890. Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
  5891. u64 sas_address;
  5892. if (ioc->shost_recovery)
  5893. return;
  5894. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  5895. return;
  5896. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  5897. state = le32_to_cpu(event_data->NewValue);
  5898. if (!ioc->hide_ir_msg)
  5899. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
  5900. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  5901. le32_to_cpu(event_data->PreviousValue), state));
  5902. switch (state) {
  5903. case MPI2_RAID_PD_STATE_ONLINE:
  5904. case MPI2_RAID_PD_STATE_DEGRADED:
  5905. case MPI2_RAID_PD_STATE_REBUILDING:
  5906. case MPI2_RAID_PD_STATE_OPTIMAL:
  5907. case MPI2_RAID_PD_STATE_HOT_SPARE:
  5908. if (!ioc->is_warpdrive)
  5909. set_bit(handle, ioc->pd_handles);
  5910. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5911. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  5912. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5913. if (sas_device)
  5914. return;
  5915. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  5916. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  5917. handle))) {
  5918. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5919. ioc->name, __FILE__, __LINE__, __func__);
  5920. return;
  5921. }
  5922. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5923. MPI2_IOCSTATUS_MASK;
  5924. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5925. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5926. ioc->name, __FILE__, __LINE__, __func__);
  5927. return;
  5928. }
  5929. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  5930. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  5931. mpt2sas_transport_update_links(ioc, sas_address, handle,
  5932. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  5933. _scsih_add_device(ioc, handle, 0, 1);
  5934. break;
  5935. case MPI2_RAID_PD_STATE_OFFLINE:
  5936. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  5937. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  5938. default:
  5939. break;
  5940. }
  5941. }
  5942. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5943. /**
  5944. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  5945. * @ioc: per adapter object
  5946. * @event_data: event data payload
  5947. * Context: user.
  5948. *
  5949. * Return nothing.
  5950. */
  5951. static void
  5952. _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
  5953. Mpi2EventDataIrOperationStatus_t *event_data)
  5954. {
  5955. char *reason_str = NULL;
  5956. switch (event_data->RAIDOperation) {
  5957. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  5958. reason_str = "resync";
  5959. break;
  5960. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  5961. reason_str = "online capacity expansion";
  5962. break;
  5963. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  5964. reason_str = "consistency check";
  5965. break;
  5966. case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
  5967. reason_str = "background init";
  5968. break;
  5969. case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
  5970. reason_str = "make data consistent";
  5971. break;
  5972. }
  5973. if (!reason_str)
  5974. return;
  5975. printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
  5976. "\thandle(0x%04x), percent complete(%d)\n",
  5977. ioc->name, reason_str,
  5978. le16_to_cpu(event_data->VolDevHandle),
  5979. event_data->PercentComplete);
  5980. }
  5981. #endif
  5982. /**
  5983. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  5984. * @ioc: per adapter object
  5985. * @fw_event: The fw_event_work object
  5986. * Context: user.
  5987. *
  5988. * Return nothing.
  5989. */
  5990. static void
  5991. _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
  5992. struct fw_event_work *fw_event)
  5993. {
  5994. Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
  5995. static struct _raid_device *raid_device;
  5996. unsigned long flags;
  5997. u16 handle;
  5998. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5999. if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  6000. && !ioc->hide_ir_msg)
  6001. _scsih_sas_ir_operation_status_event_debug(ioc,
  6002. event_data);
  6003. #endif
  6004. /* code added for raid transport support */
  6005. if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
  6006. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6007. handle = le16_to_cpu(event_data->VolDevHandle);
  6008. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  6009. if (raid_device)
  6010. raid_device->percent_complete =
  6011. event_data->PercentComplete;
  6012. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6013. }
  6014. }
  6015. /**
  6016. * _scsih_prep_device_scan - initialize parameters prior to device scan
  6017. * @ioc: per adapter object
  6018. *
  6019. * Set the deleted flag prior to device scan. If the device is found during
  6020. * the scan, then we clear the deleted flag.
  6021. */
  6022. static void
  6023. _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
  6024. {
  6025. struct MPT2SAS_DEVICE *sas_device_priv_data;
  6026. struct scsi_device *sdev;
  6027. shost_for_each_device(sdev, ioc->shost) {
  6028. sas_device_priv_data = sdev->hostdata;
  6029. if (sas_device_priv_data && sas_device_priv_data->sas_target)
  6030. sas_device_priv_data->sas_target->deleted = 1;
  6031. }
  6032. }
  6033. /**
  6034. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  6035. * @ioc: per adapter object
  6036. * @sas_address: sas address
  6037. * @slot: enclosure slot id
  6038. * @handle: device handle
  6039. *
  6040. * After host reset, find out whether devices are still responding.
  6041. * Used in _scsi_remove_unresponsive_sas_devices.
  6042. *
  6043. * Return nothing.
  6044. */
  6045. static void
  6046. _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  6047. u16 slot, u16 handle)
  6048. {
  6049. struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
  6050. struct scsi_target *starget;
  6051. struct _sas_device *sas_device;
  6052. unsigned long flags;
  6053. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6054. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  6055. if (sas_device->sas_address == sas_address &&
  6056. sas_device->slot == slot) {
  6057. sas_device->responding = 1;
  6058. starget = sas_device->starget;
  6059. if (starget && starget->hostdata) {
  6060. sas_target_priv_data = starget->hostdata;
  6061. sas_target_priv_data->tm_busy = 0;
  6062. sas_target_priv_data->deleted = 0;
  6063. } else
  6064. sas_target_priv_data = NULL;
  6065. if (starget)
  6066. starget_printk(KERN_INFO, starget,
  6067. "handle(0x%04x), sas_addr(0x%016llx), "
  6068. "enclosure logical id(0x%016llx), "
  6069. "slot(%d)\n", handle,
  6070. (unsigned long long)sas_device->sas_address,
  6071. (unsigned long long)
  6072. sas_device->enclosure_logical_id,
  6073. sas_device->slot);
  6074. if (sas_device->handle == handle)
  6075. goto out;
  6076. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  6077. sas_device->handle);
  6078. sas_device->handle = handle;
  6079. if (sas_target_priv_data)
  6080. sas_target_priv_data->handle = handle;
  6081. goto out;
  6082. }
  6083. }
  6084. out:
  6085. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6086. }
  6087. /**
  6088. * _scsih_search_responding_sas_devices -
  6089. * @ioc: per adapter object
  6090. *
  6091. * After host reset, find out whether devices are still responding.
  6092. * If not remove.
  6093. *
  6094. * Return nothing.
  6095. */
  6096. static void
  6097. _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  6098. {
  6099. Mpi2SasDevicePage0_t sas_device_pg0;
  6100. Mpi2ConfigReply_t mpi_reply;
  6101. u16 ioc_status;
  6102. __le64 sas_address;
  6103. u16 handle;
  6104. u32 device_info;
  6105. u16 slot;
  6106. printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
  6107. if (list_empty(&ioc->sas_device_list))
  6108. goto out;
  6109. handle = 0xFFFF;
  6110. while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  6111. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  6112. handle))) {
  6113. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6114. MPI2_IOCSTATUS_MASK;
  6115. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6116. break;
  6117. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  6118. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  6119. if (!(_scsih_is_end_device(device_info)))
  6120. continue;
  6121. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  6122. slot = le16_to_cpu(sas_device_pg0.Slot);
  6123. _scsih_mark_responding_sas_device(ioc, sas_address, slot,
  6124. handle);
  6125. }
  6126. out:
  6127. printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
  6128. ioc->name);
  6129. }
  6130. /**
  6131. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  6132. * @ioc: per adapter object
  6133. * @wwid: world wide identifier for raid volume
  6134. * @handle: device handle
  6135. *
  6136. * After host reset, find out whether devices are still responding.
  6137. * Used in _scsi_remove_unresponsive_raid_devices.
  6138. *
  6139. * Return nothing.
  6140. */
  6141. static void
  6142. _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
  6143. u16 handle)
  6144. {
  6145. struct MPT2SAS_TARGET *sas_target_priv_data;
  6146. struct scsi_target *starget;
  6147. struct _raid_device *raid_device;
  6148. unsigned long flags;
  6149. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6150. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  6151. if (raid_device->wwid == wwid && raid_device->starget) {
  6152. starget = raid_device->starget;
  6153. if (starget && starget->hostdata) {
  6154. sas_target_priv_data = starget->hostdata;
  6155. sas_target_priv_data->deleted = 0;
  6156. } else
  6157. sas_target_priv_data = NULL;
  6158. raid_device->responding = 1;
  6159. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6160. starget_printk(KERN_INFO, raid_device->starget,
  6161. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  6162. (unsigned long long)raid_device->wwid);
  6163. /*
  6164. * WARPDRIVE: The handles of the PDs might have changed
  6165. * across the host reset so re-initialize the
  6166. * required data for Direct IO
  6167. */
  6168. _scsih_init_warpdrive_properties(ioc, raid_device);
  6169. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6170. if (raid_device->handle == handle) {
  6171. spin_unlock_irqrestore(&ioc->raid_device_lock,
  6172. flags);
  6173. return;
  6174. }
  6175. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  6176. raid_device->handle);
  6177. raid_device->handle = handle;
  6178. if (sas_target_priv_data)
  6179. sas_target_priv_data->handle = handle;
  6180. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6181. return;
  6182. }
  6183. }
  6184. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6185. }
  6186. /**
  6187. * _scsih_search_responding_raid_devices -
  6188. * @ioc: per adapter object
  6189. *
  6190. * After host reset, find out whether devices are still responding.
  6191. * If not remove.
  6192. *
  6193. * Return nothing.
  6194. */
  6195. static void
  6196. _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
  6197. {
  6198. Mpi2RaidVolPage1_t volume_pg1;
  6199. Mpi2RaidVolPage0_t volume_pg0;
  6200. Mpi2RaidPhysDiskPage0_t pd_pg0;
  6201. Mpi2ConfigReply_t mpi_reply;
  6202. u16 ioc_status;
  6203. u16 handle;
  6204. u8 phys_disk_num;
  6205. if (!ioc->ir_firmware)
  6206. return;
  6207. printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
  6208. ioc->name);
  6209. if (list_empty(&ioc->raid_device_list))
  6210. goto out;
  6211. handle = 0xFFFF;
  6212. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  6213. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  6214. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6215. MPI2_IOCSTATUS_MASK;
  6216. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6217. break;
  6218. handle = le16_to_cpu(volume_pg1.DevHandle);
  6219. if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
  6220. &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  6221. sizeof(Mpi2RaidVolPage0_t)))
  6222. continue;
  6223. if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
  6224. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
  6225. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
  6226. _scsih_mark_responding_raid_device(ioc,
  6227. le64_to_cpu(volume_pg1.WWID), handle);
  6228. }
  6229. /* refresh the pd_handles */
  6230. if (!ioc->is_warpdrive) {
  6231. phys_disk_num = 0xFF;
  6232. memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
  6233. while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  6234. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
  6235. phys_disk_num))) {
  6236. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6237. MPI2_IOCSTATUS_MASK;
  6238. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6239. break;
  6240. phys_disk_num = pd_pg0.PhysDiskNum;
  6241. handle = le16_to_cpu(pd_pg0.DevHandle);
  6242. set_bit(handle, ioc->pd_handles);
  6243. }
  6244. }
  6245. out:
  6246. printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
  6247. "complete\n", ioc->name);
  6248. }
  6249. /**
  6250. * _scsih_mark_responding_expander - mark a expander as responding
  6251. * @ioc: per adapter object
  6252. * @sas_address: sas address
  6253. * @handle:
  6254. *
  6255. * After host reset, find out whether devices are still responding.
  6256. * Used in _scsi_remove_unresponsive_expanders.
  6257. *
  6258. * Return nothing.
  6259. */
  6260. static void
  6261. _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  6262. u16 handle)
  6263. {
  6264. struct _sas_node *sas_expander;
  6265. unsigned long flags;
  6266. int i;
  6267. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  6268. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  6269. if (sas_expander->sas_address != sas_address)
  6270. continue;
  6271. sas_expander->responding = 1;
  6272. if (sas_expander->handle == handle)
  6273. goto out;
  6274. printk(KERN_INFO "\texpander(0x%016llx): handle changed"
  6275. " from(0x%04x) to (0x%04x)!!!\n",
  6276. (unsigned long long)sas_expander->sas_address,
  6277. sas_expander->handle, handle);
  6278. sas_expander->handle = handle;
  6279. for (i = 0 ; i < sas_expander->num_phys ; i++)
  6280. sas_expander->phy[i].handle = handle;
  6281. goto out;
  6282. }
  6283. out:
  6284. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  6285. }
  6286. /**
  6287. * _scsih_search_responding_expanders -
  6288. * @ioc: per adapter object
  6289. *
  6290. * After host reset, find out whether devices are still responding.
  6291. * If not remove.
  6292. *
  6293. * Return nothing.
  6294. */
  6295. static void
  6296. _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
  6297. {
  6298. Mpi2ExpanderPage0_t expander_pg0;
  6299. Mpi2ConfigReply_t mpi_reply;
  6300. u16 ioc_status;
  6301. u64 sas_address;
  6302. u16 handle;
  6303. printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
  6304. if (list_empty(&ioc->sas_expander_list))
  6305. goto out;
  6306. handle = 0xFFFF;
  6307. while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  6308. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  6309. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6310. MPI2_IOCSTATUS_MASK;
  6311. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6312. break;
  6313. handle = le16_to_cpu(expander_pg0.DevHandle);
  6314. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  6315. printk(KERN_INFO "\texpander present: handle(0x%04x), "
  6316. "sas_addr(0x%016llx)\n", handle,
  6317. (unsigned long long)sas_address);
  6318. _scsih_mark_responding_expander(ioc, sas_address, handle);
  6319. }
  6320. out:
  6321. printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
  6322. }
  6323. /**
  6324. * _scsih_remove_unresponding_sas_devices - removing unresponding devices
  6325. * @ioc: per adapter object
  6326. *
  6327. * Return nothing.
  6328. */
  6329. static void
  6330. _scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  6331. {
  6332. struct _sas_device *sas_device, *sas_device_next;
  6333. struct _sas_node *sas_expander, *sas_expander_next;
  6334. struct _raid_device *raid_device, *raid_device_next;
  6335. struct list_head tmp_list;
  6336. unsigned long flags;
  6337. printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
  6338. ioc->name);
  6339. /* removing unresponding end devices */
  6340. printk(MPT2SAS_INFO_FMT "removing unresponding devices: end-devices\n",
  6341. ioc->name);
  6342. list_for_each_entry_safe(sas_device, sas_device_next,
  6343. &ioc->sas_device_list, list) {
  6344. if (!sas_device->responding)
  6345. mpt2sas_device_remove_by_sas_address(ioc,
  6346. sas_device->sas_address);
  6347. else
  6348. sas_device->responding = 0;
  6349. }
  6350. /* removing unresponding volumes */
  6351. if (ioc->ir_firmware) {
  6352. printk(MPT2SAS_INFO_FMT "removing unresponding devices: "
  6353. "volumes\n", ioc->name);
  6354. list_for_each_entry_safe(raid_device, raid_device_next,
  6355. &ioc->raid_device_list, list) {
  6356. if (!raid_device->responding)
  6357. _scsih_sas_volume_delete(ioc,
  6358. raid_device->handle);
  6359. else
  6360. raid_device->responding = 0;
  6361. }
  6362. }
  6363. /* removing unresponding expanders */
  6364. printk(MPT2SAS_INFO_FMT "removing unresponding devices: expanders\n",
  6365. ioc->name);
  6366. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  6367. INIT_LIST_HEAD(&tmp_list);
  6368. list_for_each_entry_safe(sas_expander, sas_expander_next,
  6369. &ioc->sas_expander_list, list) {
  6370. if (!sas_expander->responding)
  6371. list_move_tail(&sas_expander->list, &tmp_list);
  6372. else
  6373. sas_expander->responding = 0;
  6374. }
  6375. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  6376. list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
  6377. list) {
  6378. list_del(&sas_expander->list);
  6379. _scsih_expander_node_remove(ioc, sas_expander);
  6380. }
  6381. printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
  6382. ioc->name);
  6383. /* unblock devices */
  6384. _scsih_ublock_io_all_device(ioc);
  6385. }
  6386. static void
  6387. _scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
  6388. struct _sas_node *sas_expander, u16 handle)
  6389. {
  6390. Mpi2ExpanderPage1_t expander_pg1;
  6391. Mpi2ConfigReply_t mpi_reply;
  6392. int i;
  6393. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  6394. if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
  6395. &expander_pg1, i, handle))) {
  6396. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  6397. ioc->name, __FILE__, __LINE__, __func__);
  6398. return;
  6399. }
  6400. mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
  6401. le16_to_cpu(expander_pg1.AttachedDevHandle), i,
  6402. expander_pg1.NegotiatedLinkRate >> 4);
  6403. }
  6404. }
  6405. /**
  6406. * _scsih_scan_for_devices_after_reset - scan for devices after host reset
  6407. * @ioc: per adapter object
  6408. *
  6409. * Return nothing.
  6410. */
  6411. static void
  6412. _scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
  6413. {
  6414. Mpi2ExpanderPage0_t expander_pg0;
  6415. Mpi2SasDevicePage0_t sas_device_pg0;
  6416. Mpi2RaidVolPage1_t volume_pg1;
  6417. Mpi2RaidVolPage0_t volume_pg0;
  6418. Mpi2RaidPhysDiskPage0_t pd_pg0;
  6419. Mpi2EventIrConfigElement_t element;
  6420. Mpi2ConfigReply_t mpi_reply;
  6421. u8 phys_disk_num;
  6422. u16 ioc_status;
  6423. u16 handle, parent_handle;
  6424. u64 sas_address;
  6425. struct _sas_device *sas_device;
  6426. struct _sas_node *expander_device;
  6427. static struct _raid_device *raid_device;
  6428. unsigned long flags;
  6429. printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
  6430. _scsih_sas_host_refresh(ioc);
  6431. /* expanders */
  6432. handle = 0xFFFF;
  6433. while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  6434. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  6435. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6436. MPI2_IOCSTATUS_MASK;
  6437. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6438. break;
  6439. handle = le16_to_cpu(expander_pg0.DevHandle);
  6440. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  6441. expander_device = mpt2sas_scsih_expander_find_by_sas_address(
  6442. ioc, le64_to_cpu(expander_pg0.SASAddress));
  6443. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  6444. if (expander_device)
  6445. _scsih_refresh_expander_links(ioc, expander_device,
  6446. handle);
  6447. else
  6448. _scsih_expander_add(ioc, handle);
  6449. }
  6450. if (!ioc->ir_firmware)
  6451. goto skip_to_sas;
  6452. /* phys disk */
  6453. phys_disk_num = 0xFF;
  6454. while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  6455. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
  6456. phys_disk_num))) {
  6457. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6458. MPI2_IOCSTATUS_MASK;
  6459. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6460. break;
  6461. phys_disk_num = pd_pg0.PhysDiskNum;
  6462. handle = le16_to_cpu(pd_pg0.DevHandle);
  6463. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6464. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  6465. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6466. if (sas_device)
  6467. continue;
  6468. if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  6469. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  6470. handle) != 0)
  6471. continue;
  6472. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  6473. if (!_scsih_get_sas_address(ioc, parent_handle,
  6474. &sas_address)) {
  6475. mpt2sas_transport_update_links(ioc, sas_address,
  6476. handle, sas_device_pg0.PhyNum,
  6477. MPI2_SAS_NEG_LINK_RATE_1_5);
  6478. set_bit(handle, ioc->pd_handles);
  6479. _scsih_add_device(ioc, handle, 0, 1);
  6480. }
  6481. }
  6482. /* volumes */
  6483. handle = 0xFFFF;
  6484. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  6485. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  6486. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6487. MPI2_IOCSTATUS_MASK;
  6488. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6489. break;
  6490. handle = le16_to_cpu(volume_pg1.DevHandle);
  6491. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6492. raid_device = _scsih_raid_device_find_by_wwid(ioc,
  6493. le64_to_cpu(volume_pg1.WWID));
  6494. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6495. if (raid_device)
  6496. continue;
  6497. if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
  6498. &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  6499. sizeof(Mpi2RaidVolPage0_t)))
  6500. continue;
  6501. if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
  6502. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
  6503. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
  6504. memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
  6505. element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
  6506. element.VolDevHandle = volume_pg1.DevHandle;
  6507. _scsih_sas_volume_add(ioc, &element);
  6508. }
  6509. }
  6510. skip_to_sas:
  6511. /* sas devices */
  6512. handle = 0xFFFF;
  6513. while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  6514. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  6515. handle))) {
  6516. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6517. MPI2_IOCSTATUS_MASK;
  6518. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  6519. break;
  6520. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  6521. if (!(_scsih_is_end_device(
  6522. le32_to_cpu(sas_device_pg0.DeviceInfo))))
  6523. continue;
  6524. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6525. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  6526. le64_to_cpu(sas_device_pg0.SASAddress));
  6527. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6528. if (sas_device)
  6529. continue;
  6530. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  6531. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
  6532. mpt2sas_transport_update_links(ioc, sas_address, handle,
  6533. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  6534. _scsih_add_device(ioc, handle, 0, 0);
  6535. }
  6536. }
  6537. printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
  6538. }
  6539. /**
  6540. * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
  6541. * @ioc: per adapter object
  6542. * @reset_phase: phase
  6543. *
  6544. * The handler for doing any required cleanup or initialization.
  6545. *
  6546. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  6547. * MPT2_IOC_DONE_RESET
  6548. *
  6549. * Return nothing.
  6550. */
  6551. void
  6552. mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  6553. {
  6554. switch (reset_phase) {
  6555. case MPT2_IOC_PRE_RESET:
  6556. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  6557. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  6558. break;
  6559. case MPT2_IOC_AFTER_RESET:
  6560. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  6561. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  6562. if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
  6563. ioc->scsih_cmds.status |= MPT2_CMD_RESET;
  6564. mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
  6565. complete(&ioc->scsih_cmds.done);
  6566. }
  6567. if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
  6568. ioc->tm_cmds.status |= MPT2_CMD_RESET;
  6569. mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  6570. complete(&ioc->tm_cmds.done);
  6571. }
  6572. _scsih_fw_event_cleanup_queue(ioc);
  6573. _scsih_flush_running_cmds(ioc);
  6574. break;
  6575. case MPT2_IOC_DONE_RESET:
  6576. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  6577. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  6578. _scsih_sas_host_refresh(ioc);
  6579. _scsih_prep_device_scan(ioc);
  6580. _scsih_search_responding_sas_devices(ioc);
  6581. _scsih_search_responding_raid_devices(ioc);
  6582. _scsih_search_responding_expanders(ioc);
  6583. if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
  6584. !ioc->sas_hba.num_phys)) {
  6585. _scsih_prep_device_scan(ioc);
  6586. _scsih_search_responding_sas_devices(ioc);
  6587. _scsih_search_responding_raid_devices(ioc);
  6588. _scsih_search_responding_expanders(ioc);
  6589. _scsih_error_recovery_delete_devices(ioc);
  6590. }
  6591. break;
  6592. }
  6593. }
  6594. /**
  6595. * _firmware_event_work - delayed task for processing firmware events
  6596. * @ioc: per adapter object
  6597. * @work: equal to the fw_event_work object
  6598. * Context: user.
  6599. *
  6600. * Return nothing.
  6601. */
  6602. static void
  6603. _firmware_event_work(struct work_struct *work)
  6604. {
  6605. struct fw_event_work *fw_event = container_of(work,
  6606. struct fw_event_work, delayed_work.work);
  6607. struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
  6608. /* the queue is being flushed so ignore this event */
  6609. if (ioc->remove_host || fw_event->cancel_pending_work ||
  6610. ioc->pci_error_recovery) {
  6611. _scsih_fw_event_free(ioc, fw_event);
  6612. return;
  6613. }
  6614. switch (fw_event->event) {
  6615. case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
  6616. while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
  6617. ssleep(1);
  6618. _scsih_remove_unresponding_sas_devices(ioc);
  6619. _scsih_scan_for_devices_after_reset(ioc);
  6620. break;
  6621. case MPT2SAS_PORT_ENABLE_COMPLETE:
  6622. ioc->start_scan = 0;
  6623. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
  6624. "from worker thread\n", ioc->name));
  6625. break;
  6626. case MPT2SAS_TURN_ON_FAULT_LED:
  6627. _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
  6628. break;
  6629. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  6630. _scsih_sas_topology_change_event(ioc, fw_event);
  6631. break;
  6632. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  6633. _scsih_sas_device_status_change_event(ioc,
  6634. fw_event);
  6635. break;
  6636. case MPI2_EVENT_SAS_DISCOVERY:
  6637. _scsih_sas_discovery_event(ioc,
  6638. fw_event);
  6639. break;
  6640. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  6641. _scsih_sas_broadcast_primitive_event(ioc,
  6642. fw_event);
  6643. break;
  6644. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  6645. _scsih_sas_enclosure_dev_status_change_event(ioc,
  6646. fw_event);
  6647. break;
  6648. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  6649. _scsih_sas_ir_config_change_event(ioc, fw_event);
  6650. break;
  6651. case MPI2_EVENT_IR_VOLUME:
  6652. _scsih_sas_ir_volume_event(ioc, fw_event);
  6653. break;
  6654. case MPI2_EVENT_IR_PHYSICAL_DISK:
  6655. _scsih_sas_ir_physical_disk_event(ioc, fw_event);
  6656. break;
  6657. case MPI2_EVENT_IR_OPERATION_STATUS:
  6658. _scsih_sas_ir_operation_status_event(ioc, fw_event);
  6659. break;
  6660. }
  6661. _scsih_fw_event_free(ioc, fw_event);
  6662. }
  6663. /**
  6664. * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
  6665. * @ioc: per adapter object
  6666. * @msix_index: MSIX table index supplied by the OS
  6667. * @reply: reply message frame(lower 32bit addr)
  6668. * Context: interrupt.
  6669. *
  6670. * This function merely adds a new work task into ioc->firmware_event_thread.
  6671. * The tasks are worked from _firmware_event_work in user context.
  6672. *
  6673. * Return 1 meaning mf should be freed from _base_interrupt
  6674. * 0 means the mf is freed from this function.
  6675. */
  6676. u8
  6677. mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
  6678. u32 reply)
  6679. {
  6680. struct fw_event_work *fw_event;
  6681. Mpi2EventNotificationReply_t *mpi_reply;
  6682. u16 event;
  6683. u16 sz;
  6684. /* events turned off due to host reset or driver unloading */
  6685. if (ioc->remove_host || ioc->pci_error_recovery)
  6686. return 1;
  6687. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  6688. if (unlikely(!mpi_reply)) {
  6689. printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  6690. ioc->name, __FILE__, __LINE__, __func__);
  6691. return 1;
  6692. }
  6693. event = le16_to_cpu(mpi_reply->Event);
  6694. switch (event) {
  6695. /* handle these */
  6696. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  6697. {
  6698. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  6699. (Mpi2EventDataSasBroadcastPrimitive_t *)
  6700. mpi_reply->EventData;
  6701. if (baen_data->Primitive !=
  6702. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
  6703. return 1;
  6704. if (ioc->broadcast_aen_busy) {
  6705. ioc->broadcast_aen_pending++;
  6706. return 1;
  6707. } else
  6708. ioc->broadcast_aen_busy = 1;
  6709. break;
  6710. }
  6711. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  6712. _scsih_check_topo_delete_events(ioc,
  6713. (Mpi2EventDataSasTopologyChangeList_t *)
  6714. mpi_reply->EventData);
  6715. break;
  6716. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  6717. _scsih_check_ir_config_unhide_events(ioc,
  6718. (Mpi2EventDataIrConfigChangeList_t *)
  6719. mpi_reply->EventData);
  6720. break;
  6721. case MPI2_EVENT_IR_VOLUME:
  6722. _scsih_check_volume_delete_events(ioc,
  6723. (Mpi2EventDataIrVolume_t *)
  6724. mpi_reply->EventData);
  6725. break;
  6726. case MPI2_EVENT_LOG_ENTRY_ADDED:
  6727. {
  6728. Mpi2EventDataLogEntryAdded_t *log_entry;
  6729. __le32 *log_code;
  6730. if (!ioc->is_warpdrive)
  6731. break;
  6732. log_entry = (Mpi2EventDataLogEntryAdded_t *)
  6733. mpi_reply->EventData;
  6734. log_code = (__le32 *)log_entry->LogData;
  6735. if (le16_to_cpu(log_entry->LogEntryQualifier)
  6736. != MPT2_WARPDRIVE_LOGENTRY)
  6737. break;
  6738. switch (le32_to_cpu(*log_code)) {
  6739. case MPT2_WARPDRIVE_LC_SSDT:
  6740. printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
  6741. "IO Throttling has occurred in the WarpDrive "
  6742. "subsystem. Check WarpDrive documentation for "
  6743. "additional details.\n", ioc->name);
  6744. break;
  6745. case MPT2_WARPDRIVE_LC_SSDLW:
  6746. printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
  6747. "Program/Erase Cycles for the WarpDrive subsystem "
  6748. "in degraded range. Check WarpDrive documentation "
  6749. "for additional details.\n", ioc->name);
  6750. break;
  6751. case MPT2_WARPDRIVE_LC_SSDLF:
  6752. printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
  6753. "There are no Program/Erase Cycles for the "
  6754. "WarpDrive subsystem. The storage device will be "
  6755. "in read-only mode. Check WarpDrive documentation "
  6756. "for additional details.\n", ioc->name);
  6757. break;
  6758. case MPT2_WARPDRIVE_LC_BRMF:
  6759. printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
  6760. "The Backup Rail Monitor has failed on the "
  6761. "WarpDrive subsystem. Check WarpDrive "
  6762. "documentation for additional details.\n",
  6763. ioc->name);
  6764. break;
  6765. }
  6766. break;
  6767. }
  6768. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  6769. case MPI2_EVENT_IR_OPERATION_STATUS:
  6770. case MPI2_EVENT_SAS_DISCOVERY:
  6771. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  6772. case MPI2_EVENT_IR_PHYSICAL_DISK:
  6773. break;
  6774. default: /* ignore the rest */
  6775. return 1;
  6776. }
  6777. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  6778. if (!fw_event) {
  6779. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  6780. ioc->name, __FILE__, __LINE__, __func__);
  6781. return 1;
  6782. }
  6783. sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
  6784. fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
  6785. if (!fw_event->event_data) {
  6786. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  6787. ioc->name, __FILE__, __LINE__, __func__);
  6788. kfree(fw_event);
  6789. return 1;
  6790. }
  6791. memcpy(fw_event->event_data, mpi_reply->EventData,
  6792. sz);
  6793. fw_event->ioc = ioc;
  6794. fw_event->VF_ID = mpi_reply->VF_ID;
  6795. fw_event->VP_ID = mpi_reply->VP_ID;
  6796. fw_event->event = event;
  6797. _scsih_fw_event_add(ioc, fw_event);
  6798. return 1;
  6799. }
  6800. /* shost template */
  6801. static struct scsi_host_template scsih_driver_template = {
  6802. .module = THIS_MODULE,
  6803. .name = "Fusion MPT SAS Host",
  6804. .proc_name = MPT2SAS_DRIVER_NAME,
  6805. .queuecommand = _scsih_qcmd,
  6806. .target_alloc = _scsih_target_alloc,
  6807. .slave_alloc = _scsih_slave_alloc,
  6808. .slave_configure = _scsih_slave_configure,
  6809. .target_destroy = _scsih_target_destroy,
  6810. .slave_destroy = _scsih_slave_destroy,
  6811. .scan_finished = _scsih_scan_finished,
  6812. .scan_start = _scsih_scan_start,
  6813. .change_queue_depth = _scsih_change_queue_depth,
  6814. .change_queue_type = _scsih_change_queue_type,
  6815. .eh_abort_handler = _scsih_abort,
  6816. .eh_device_reset_handler = _scsih_dev_reset,
  6817. .eh_target_reset_handler = _scsih_target_reset,
  6818. .eh_host_reset_handler = _scsih_host_reset,
  6819. .bios_param = _scsih_bios_param,
  6820. .can_queue = 1,
  6821. .this_id = -1,
  6822. .sg_tablesize = MPT2SAS_SG_DEPTH,
  6823. .max_sectors = 32767,
  6824. .cmd_per_lun = 7,
  6825. .use_clustering = ENABLE_CLUSTERING,
  6826. .shost_attrs = mpt2sas_host_attrs,
  6827. .sdev_attrs = mpt2sas_dev_attrs,
  6828. };
  6829. /**
  6830. * _scsih_expander_node_remove - removing expander device from list.
  6831. * @ioc: per adapter object
  6832. * @sas_expander: the sas_device object
  6833. * Context: Calling function should acquire ioc->sas_node_lock.
  6834. *
  6835. * Removing object and freeing associated memory from the
  6836. * ioc->sas_expander_list.
  6837. *
  6838. * Return nothing.
  6839. */
  6840. static void
  6841. _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  6842. struct _sas_node *sas_expander)
  6843. {
  6844. struct _sas_port *mpt2sas_port, *next;
  6845. /* remove sibling ports attached to this expander */
  6846. list_for_each_entry_safe(mpt2sas_port, next,
  6847. &sas_expander->sas_port_list, port_list) {
  6848. if (ioc->shost_recovery)
  6849. return;
  6850. if (mpt2sas_port->remote_identify.device_type ==
  6851. SAS_END_DEVICE)
  6852. mpt2sas_device_remove_by_sas_address(ioc,
  6853. mpt2sas_port->remote_identify.sas_address);
  6854. else if (mpt2sas_port->remote_identify.device_type ==
  6855. SAS_EDGE_EXPANDER_DEVICE ||
  6856. mpt2sas_port->remote_identify.device_type ==
  6857. SAS_FANOUT_EXPANDER_DEVICE)
  6858. mpt2sas_expander_remove(ioc,
  6859. mpt2sas_port->remote_identify.sas_address);
  6860. }
  6861. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  6862. sas_expander->sas_address_parent);
  6863. printk(MPT2SAS_INFO_FMT "expander_remove: handle"
  6864. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  6865. sas_expander->handle, (unsigned long long)
  6866. sas_expander->sas_address);
  6867. kfree(sas_expander->phy);
  6868. kfree(sas_expander);
  6869. }
  6870. /**
  6871. * _scsih_ir_shutdown - IR shutdown notification
  6872. * @ioc: per adapter object
  6873. *
  6874. * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
  6875. * the host system is shutting down.
  6876. *
  6877. * Return nothing.
  6878. */
  6879. static void
  6880. _scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
  6881. {
  6882. Mpi2RaidActionRequest_t *mpi_request;
  6883. Mpi2RaidActionReply_t *mpi_reply;
  6884. u16 smid;
  6885. /* is IR firmware build loaded ? */
  6886. if (!ioc->ir_firmware)
  6887. return;
  6888. /* are there any volumes ? */
  6889. if (list_empty(&ioc->raid_device_list))
  6890. return;
  6891. mutex_lock(&ioc->scsih_cmds.mutex);
  6892. if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
  6893. printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
  6894. ioc->name, __func__);
  6895. goto out;
  6896. }
  6897. ioc->scsih_cmds.status = MPT2_CMD_PENDING;
  6898. smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  6899. if (!smid) {
  6900. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  6901. ioc->name, __func__);
  6902. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  6903. goto out;
  6904. }
  6905. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  6906. ioc->scsih_cmds.smid = smid;
  6907. memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
  6908. mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
  6909. mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
  6910. if (!ioc->hide_ir_msg)
  6911. printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
  6912. init_completion(&ioc->scsih_cmds.done);
  6913. mpt2sas_base_put_smid_default(ioc, smid);
  6914. wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
  6915. if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
  6916. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  6917. ioc->name, __func__);
  6918. goto out;
  6919. }
  6920. if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
  6921. mpi_reply = ioc->scsih_cmds.reply;
  6922. if (!ioc->hide_ir_msg)
  6923. printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
  6924. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  6925. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  6926. le32_to_cpu(mpi_reply->IOCLogInfo));
  6927. }
  6928. out:
  6929. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  6930. mutex_unlock(&ioc->scsih_cmds.mutex);
  6931. }
  6932. /**
  6933. * _scsih_shutdown - routine call during system shutdown
  6934. * @pdev: PCI device struct
  6935. *
  6936. * Return nothing.
  6937. */
  6938. static void
  6939. _scsih_shutdown(struct pci_dev *pdev)
  6940. {
  6941. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6942. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6943. struct workqueue_struct *wq;
  6944. unsigned long flags;
  6945. ioc->remove_host = 1;
  6946. _scsih_fw_event_cleanup_queue(ioc);
  6947. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  6948. wq = ioc->firmware_event_thread;
  6949. ioc->firmware_event_thread = NULL;
  6950. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  6951. if (wq)
  6952. destroy_workqueue(wq);
  6953. _scsih_ir_shutdown(ioc);
  6954. mpt2sas_base_detach(ioc);
  6955. }
  6956. /**
  6957. * _scsih_remove - detach and remove add host
  6958. * @pdev: PCI device struct
  6959. *
  6960. * Routine called when unloading the driver.
  6961. * Return nothing.
  6962. */
  6963. static void __devexit
  6964. _scsih_remove(struct pci_dev *pdev)
  6965. {
  6966. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6967. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6968. struct _sas_port *mpt2sas_port, *next_port;
  6969. struct _raid_device *raid_device, *next;
  6970. struct MPT2SAS_TARGET *sas_target_priv_data;
  6971. struct workqueue_struct *wq;
  6972. unsigned long flags;
  6973. ioc->remove_host = 1;
  6974. _scsih_fw_event_cleanup_queue(ioc);
  6975. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  6976. wq = ioc->firmware_event_thread;
  6977. ioc->firmware_event_thread = NULL;
  6978. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  6979. if (wq)
  6980. destroy_workqueue(wq);
  6981. /* release all the volumes */
  6982. _scsih_ir_shutdown(ioc);
  6983. list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
  6984. list) {
  6985. if (raid_device->starget) {
  6986. sas_target_priv_data =
  6987. raid_device->starget->hostdata;
  6988. sas_target_priv_data->deleted = 1;
  6989. scsi_remove_target(&raid_device->starget->dev);
  6990. }
  6991. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
  6992. "(0x%016llx)\n", ioc->name, raid_device->handle,
  6993. (unsigned long long) raid_device->wwid);
  6994. _scsih_raid_device_remove(ioc, raid_device);
  6995. }
  6996. /* free ports attached to the sas_host */
  6997. list_for_each_entry_safe(mpt2sas_port, next_port,
  6998. &ioc->sas_hba.sas_port_list, port_list) {
  6999. if (mpt2sas_port->remote_identify.device_type ==
  7000. SAS_END_DEVICE)
  7001. mpt2sas_device_remove_by_sas_address(ioc,
  7002. mpt2sas_port->remote_identify.sas_address);
  7003. else if (mpt2sas_port->remote_identify.device_type ==
  7004. SAS_EDGE_EXPANDER_DEVICE ||
  7005. mpt2sas_port->remote_identify.device_type ==
  7006. SAS_FANOUT_EXPANDER_DEVICE)
  7007. mpt2sas_expander_remove(ioc,
  7008. mpt2sas_port->remote_identify.sas_address);
  7009. }
  7010. /* free phys attached to the sas_host */
  7011. if (ioc->sas_hba.num_phys) {
  7012. kfree(ioc->sas_hba.phy);
  7013. ioc->sas_hba.phy = NULL;
  7014. ioc->sas_hba.num_phys = 0;
  7015. }
  7016. sas_remove_host(shost);
  7017. mpt2sas_base_detach(ioc);
  7018. list_del(&ioc->list);
  7019. scsi_remove_host(shost);
  7020. scsi_host_put(shost);
  7021. }
  7022. /**
  7023. * _scsih_probe_boot_devices - reports 1st device
  7024. * @ioc: per adapter object
  7025. *
  7026. * If specified in bios page 2, this routine reports the 1st
  7027. * device scsi-ml or sas transport for persistent boot device
  7028. * purposes. Please refer to function _scsih_determine_boot_device()
  7029. */
  7030. static void
  7031. _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
  7032. {
  7033. u8 is_raid;
  7034. void *device;
  7035. struct _sas_device *sas_device;
  7036. struct _raid_device *raid_device;
  7037. u16 handle;
  7038. u64 sas_address_parent;
  7039. u64 sas_address;
  7040. unsigned long flags;
  7041. int rc;
  7042. /* no Bios, return immediately */
  7043. if (!ioc->bios_pg3.BiosVersion)
  7044. return;
  7045. device = NULL;
  7046. is_raid = 0;
  7047. if (ioc->req_boot_device.device) {
  7048. device = ioc->req_boot_device.device;
  7049. is_raid = ioc->req_boot_device.is_raid;
  7050. } else if (ioc->req_alt_boot_device.device) {
  7051. device = ioc->req_alt_boot_device.device;
  7052. is_raid = ioc->req_alt_boot_device.is_raid;
  7053. } else if (ioc->current_boot_device.device) {
  7054. device = ioc->current_boot_device.device;
  7055. is_raid = ioc->current_boot_device.is_raid;
  7056. }
  7057. if (!device)
  7058. return;
  7059. if (is_raid) {
  7060. raid_device = device;
  7061. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  7062. raid_device->id, 0);
  7063. if (rc)
  7064. _scsih_raid_device_remove(ioc, raid_device);
  7065. } else {
  7066. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7067. sas_device = device;
  7068. handle = sas_device->handle;
  7069. sas_address_parent = sas_device->sas_address_parent;
  7070. sas_address = sas_device->sas_address;
  7071. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  7072. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7073. if (ioc->hide_drives)
  7074. return;
  7075. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  7076. sas_device->sas_address_parent)) {
  7077. _scsih_sas_device_remove(ioc, sas_device);
  7078. } else if (!sas_device->starget) {
  7079. if (!ioc->is_driver_loading)
  7080. mpt2sas_transport_port_remove(ioc, sas_address,
  7081. sas_address_parent);
  7082. _scsih_sas_device_remove(ioc, sas_device);
  7083. }
  7084. }
  7085. }
  7086. /**
  7087. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  7088. * @ioc: per adapter object
  7089. *
  7090. * Called during initial loading of the driver.
  7091. */
  7092. static void
  7093. _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
  7094. {
  7095. struct _raid_device *raid_device, *raid_next;
  7096. int rc;
  7097. list_for_each_entry_safe(raid_device, raid_next,
  7098. &ioc->raid_device_list, list) {
  7099. if (raid_device->starget)
  7100. continue;
  7101. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  7102. raid_device->id, 0);
  7103. if (rc)
  7104. _scsih_raid_device_remove(ioc, raid_device);
  7105. }
  7106. }
  7107. /**
  7108. * _scsih_probe_sas - reporting sas devices to sas transport
  7109. * @ioc: per adapter object
  7110. *
  7111. * Called during initial loading of the driver.
  7112. */
  7113. static void
  7114. _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
  7115. {
  7116. struct _sas_device *sas_device, *next;
  7117. unsigned long flags;
  7118. /* SAS Device List */
  7119. list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
  7120. list) {
  7121. if (ioc->hide_drives)
  7122. continue;
  7123. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  7124. sas_device->sas_address_parent)) {
  7125. list_del(&sas_device->list);
  7126. kfree(sas_device);
  7127. continue;
  7128. } else if (!sas_device->starget) {
  7129. if (!ioc->is_driver_loading)
  7130. mpt2sas_transport_port_remove(ioc,
  7131. sas_device->sas_address,
  7132. sas_device->sas_address_parent);
  7133. list_del(&sas_device->list);
  7134. kfree(sas_device);
  7135. continue;
  7136. }
  7137. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7138. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  7139. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7140. }
  7141. }
  7142. /**
  7143. * _scsih_probe_devices - probing for devices
  7144. * @ioc: per adapter object
  7145. *
  7146. * Called during initial loading of the driver.
  7147. */
  7148. static void
  7149. _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
  7150. {
  7151. u16 volume_mapping_flags;
  7152. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  7153. return; /* return when IOC doesn't support initiator mode */
  7154. _scsih_probe_boot_devices(ioc);
  7155. if (ioc->ir_firmware) {
  7156. volume_mapping_flags =
  7157. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  7158. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  7159. if (volume_mapping_flags ==
  7160. MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
  7161. _scsih_probe_raid(ioc);
  7162. _scsih_probe_sas(ioc);
  7163. } else {
  7164. _scsih_probe_sas(ioc);
  7165. _scsih_probe_raid(ioc);
  7166. }
  7167. } else
  7168. _scsih_probe_sas(ioc);
  7169. }
  7170. /**
  7171. * _scsih_scan_start - scsi lld callback for .scan_start
  7172. * @shost: SCSI host pointer
  7173. *
  7174. * The shost has the ability to discover targets on its own instead
  7175. * of scanning the entire bus. In our implemention, we will kick off
  7176. * firmware discovery.
  7177. */
  7178. static void
  7179. _scsih_scan_start(struct Scsi_Host *shost)
  7180. {
  7181. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7182. int rc;
  7183. if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
  7184. mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
  7185. if (disable_discovery > 0)
  7186. return;
  7187. ioc->start_scan = 1;
  7188. rc = mpt2sas_port_enable(ioc);
  7189. if (rc != 0)
  7190. printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
  7191. }
  7192. /**
  7193. * _scsih_scan_finished - scsi lld callback for .scan_finished
  7194. * @shost: SCSI host pointer
  7195. * @time: elapsed time of the scan in jiffies
  7196. *
  7197. * This function will be called periodically until it returns 1 with the
  7198. * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
  7199. * we wait for firmware discovery to complete, then return 1.
  7200. */
  7201. static int
  7202. _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
  7203. {
  7204. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7205. if (disable_discovery > 0) {
  7206. ioc->is_driver_loading = 0;
  7207. ioc->wait_for_discovery_to_complete = 0;
  7208. return 1;
  7209. }
  7210. if (time >= (300 * HZ)) {
  7211. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  7212. printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
  7213. "(timeout=300s)\n", ioc->name);
  7214. ioc->is_driver_loading = 0;
  7215. return 1;
  7216. }
  7217. if (ioc->start_scan)
  7218. return 0;
  7219. if (ioc->start_scan_failed) {
  7220. printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
  7221. "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
  7222. ioc->is_driver_loading = 0;
  7223. ioc->wait_for_discovery_to_complete = 0;
  7224. ioc->remove_host = 1;
  7225. return 1;
  7226. }
  7227. printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
  7228. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  7229. if (ioc->wait_for_discovery_to_complete) {
  7230. ioc->wait_for_discovery_to_complete = 0;
  7231. _scsih_probe_devices(ioc);
  7232. }
  7233. mpt2sas_base_start_watchdog(ioc);
  7234. ioc->is_driver_loading = 0;
  7235. return 1;
  7236. }
  7237. /**
  7238. * _scsih_probe - attach and add scsi host
  7239. * @pdev: PCI device struct
  7240. * @id: pci device id
  7241. *
  7242. * Returns 0 success, anything else error.
  7243. */
  7244. static int
  7245. _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  7246. {
  7247. struct MPT2SAS_ADAPTER *ioc;
  7248. struct Scsi_Host *shost;
  7249. shost = scsi_host_alloc(&scsih_driver_template,
  7250. sizeof(struct MPT2SAS_ADAPTER));
  7251. if (!shost)
  7252. return -ENODEV;
  7253. /* init local params */
  7254. ioc = shost_priv(shost);
  7255. memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
  7256. INIT_LIST_HEAD(&ioc->list);
  7257. list_add_tail(&ioc->list, &mpt2sas_ioc_list);
  7258. ioc->shost = shost;
  7259. ioc->id = mpt_ids++;
  7260. sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
  7261. ioc->pdev = pdev;
  7262. if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
  7263. ioc->is_warpdrive = 1;
  7264. ioc->hide_ir_msg = 1;
  7265. } else
  7266. ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
  7267. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  7268. ioc->tm_cb_idx = tm_cb_idx;
  7269. ioc->ctl_cb_idx = ctl_cb_idx;
  7270. ioc->base_cb_idx = base_cb_idx;
  7271. ioc->port_enable_cb_idx = port_enable_cb_idx;
  7272. ioc->transport_cb_idx = transport_cb_idx;
  7273. ioc->scsih_cb_idx = scsih_cb_idx;
  7274. ioc->config_cb_idx = config_cb_idx;
  7275. ioc->tm_tr_cb_idx = tm_tr_cb_idx;
  7276. ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
  7277. ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
  7278. ioc->logging_level = logging_level;
  7279. ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
  7280. /* misc semaphores and spin locks */
  7281. mutex_init(&ioc->reset_in_progress_mutex);
  7282. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  7283. spin_lock_init(&ioc->scsi_lookup_lock);
  7284. spin_lock_init(&ioc->sas_device_lock);
  7285. spin_lock_init(&ioc->sas_node_lock);
  7286. spin_lock_init(&ioc->fw_event_lock);
  7287. spin_lock_init(&ioc->raid_device_lock);
  7288. INIT_LIST_HEAD(&ioc->sas_device_list);
  7289. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  7290. INIT_LIST_HEAD(&ioc->sas_expander_list);
  7291. INIT_LIST_HEAD(&ioc->fw_event_list);
  7292. INIT_LIST_HEAD(&ioc->raid_device_list);
  7293. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  7294. INIT_LIST_HEAD(&ioc->delayed_tr_list);
  7295. INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
  7296. /* init shost parameters */
  7297. shost->max_cmd_len = 32;
  7298. shost->max_lun = max_lun;
  7299. shost->transportt = mpt2sas_transport_template;
  7300. shost->unique_id = ioc->id;
  7301. if (max_sectors != 0xFFFF) {
  7302. if (max_sectors < 64) {
  7303. shost->max_sectors = 64;
  7304. printk(MPT2SAS_WARN_FMT "Invalid value %d passed "\
  7305. "for max_sectors, range is 64 to 32767. Assigning "\
  7306. "value of 64.\n", ioc->name, max_sectors);
  7307. } else if (max_sectors > 32767) {
  7308. shost->max_sectors = 32767;
  7309. printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
  7310. "for max_sectors, range is 64 to 8192. Assigning "
  7311. "default value of 32767.\n", ioc->name,
  7312. max_sectors);
  7313. } else {
  7314. shost->max_sectors = max_sectors & 0xFFFE;
  7315. printk(MPT2SAS_INFO_FMT "The max_sectors value is "
  7316. "set to %d\n", ioc->name, shost->max_sectors);
  7317. }
  7318. }
  7319. if ((scsi_add_host(shost, &pdev->dev))) {
  7320. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  7321. ioc->name, __FILE__, __LINE__, __func__);
  7322. list_del(&ioc->list);
  7323. goto out_add_shost_fail;
  7324. }
  7325. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  7326. | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
  7327. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  7328. /* event thread */
  7329. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  7330. "fw_event%d", ioc->id);
  7331. ioc->firmware_event_thread = create_singlethread_workqueue(
  7332. ioc->firmware_event_name);
  7333. if (!ioc->firmware_event_thread) {
  7334. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  7335. ioc->name, __FILE__, __LINE__, __func__);
  7336. goto out_thread_fail;
  7337. }
  7338. ioc->is_driver_loading = 1;
  7339. if ((mpt2sas_base_attach(ioc))) {
  7340. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  7341. ioc->name, __FILE__, __LINE__, __func__);
  7342. goto out_attach_fail;
  7343. }
  7344. if (ioc->is_warpdrive) {
  7345. if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
  7346. ioc->hide_drives = 0;
  7347. else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
  7348. ioc->hide_drives = 1;
  7349. else {
  7350. if (_scsih_get_num_volumes(ioc))
  7351. ioc->hide_drives = 1;
  7352. else
  7353. ioc->hide_drives = 0;
  7354. }
  7355. } else
  7356. ioc->hide_drives = 0;
  7357. scsi_scan_host(shost);
  7358. return 0;
  7359. out_attach_fail:
  7360. destroy_workqueue(ioc->firmware_event_thread);
  7361. out_thread_fail:
  7362. list_del(&ioc->list);
  7363. scsi_remove_host(shost);
  7364. out_add_shost_fail:
  7365. scsi_host_put(shost);
  7366. return -ENODEV;
  7367. }
  7368. #ifdef CONFIG_PM
  7369. /**
  7370. * _scsih_suspend - power management suspend main entry point
  7371. * @pdev: PCI device struct
  7372. * @state: PM state change to (usually PCI_D3)
  7373. *
  7374. * Returns 0 success, anything else error.
  7375. */
  7376. static int
  7377. _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  7378. {
  7379. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7380. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7381. pci_power_t device_state;
  7382. mpt2sas_base_stop_watchdog(ioc);
  7383. scsi_block_requests(shost);
  7384. device_state = pci_choose_state(pdev, state);
  7385. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
  7386. "operating state [D%d]\n", ioc->name, pdev,
  7387. pci_name(pdev), device_state);
  7388. mpt2sas_base_free_resources(ioc);
  7389. pci_save_state(pdev);
  7390. pci_disable_device(pdev);
  7391. pci_set_power_state(pdev, device_state);
  7392. return 0;
  7393. }
  7394. /**
  7395. * _scsih_resume - power management resume main entry point
  7396. * @pdev: PCI device struct
  7397. *
  7398. * Returns 0 success, anything else error.
  7399. */
  7400. static int
  7401. _scsih_resume(struct pci_dev *pdev)
  7402. {
  7403. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7404. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7405. pci_power_t device_state = pdev->current_state;
  7406. int r;
  7407. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
  7408. "operating state [D%d]\n", ioc->name, pdev,
  7409. pci_name(pdev), device_state);
  7410. pci_set_power_state(pdev, PCI_D0);
  7411. pci_enable_wake(pdev, PCI_D0, 0);
  7412. pci_restore_state(pdev);
  7413. ioc->pdev = pdev;
  7414. r = mpt2sas_base_map_resources(ioc);
  7415. if (r)
  7416. return r;
  7417. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
  7418. scsi_unblock_requests(shost);
  7419. mpt2sas_base_start_watchdog(ioc);
  7420. return 0;
  7421. }
  7422. #endif /* CONFIG_PM */
  7423. /**
  7424. * _scsih_pci_error_detected - Called when a PCI error is detected.
  7425. * @pdev: PCI device struct
  7426. * @state: PCI channel state
  7427. *
  7428. * Description: Called when a PCI error is detected.
  7429. *
  7430. * Return value:
  7431. * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  7432. */
  7433. static pci_ers_result_t
  7434. _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  7435. {
  7436. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7437. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7438. printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
  7439. ioc->name, state);
  7440. switch (state) {
  7441. case pci_channel_io_normal:
  7442. return PCI_ERS_RESULT_CAN_RECOVER;
  7443. case pci_channel_io_frozen:
  7444. /* Fatal error, prepare for slot reset */
  7445. ioc->pci_error_recovery = 1;
  7446. scsi_block_requests(ioc->shost);
  7447. mpt2sas_base_stop_watchdog(ioc);
  7448. mpt2sas_base_free_resources(ioc);
  7449. return PCI_ERS_RESULT_NEED_RESET;
  7450. case pci_channel_io_perm_failure:
  7451. /* Permanent error, prepare for device removal */
  7452. ioc->pci_error_recovery = 1;
  7453. mpt2sas_base_stop_watchdog(ioc);
  7454. _scsih_flush_running_cmds(ioc);
  7455. return PCI_ERS_RESULT_DISCONNECT;
  7456. }
  7457. return PCI_ERS_RESULT_NEED_RESET;
  7458. }
  7459. /**
  7460. * _scsih_pci_slot_reset - Called when PCI slot has been reset.
  7461. * @pdev: PCI device struct
  7462. *
  7463. * Description: This routine is called by the pci error recovery
  7464. * code after the PCI slot has been reset, just before we
  7465. * should resume normal operations.
  7466. */
  7467. static pci_ers_result_t
  7468. _scsih_pci_slot_reset(struct pci_dev *pdev)
  7469. {
  7470. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7471. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7472. int rc;
  7473. printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
  7474. ioc->name);
  7475. ioc->pci_error_recovery = 0;
  7476. ioc->pdev = pdev;
  7477. pci_restore_state(pdev);
  7478. rc = mpt2sas_base_map_resources(ioc);
  7479. if (rc)
  7480. return PCI_ERS_RESULT_DISCONNECT;
  7481. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  7482. FORCE_BIG_HAMMER);
  7483. printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
  7484. (rc == 0) ? "success" : "failed");
  7485. if (!rc)
  7486. return PCI_ERS_RESULT_RECOVERED;
  7487. else
  7488. return PCI_ERS_RESULT_DISCONNECT;
  7489. }
  7490. /**
  7491. * _scsih_pci_resume() - resume normal ops after PCI reset
  7492. * @pdev: pointer to PCI device
  7493. *
  7494. * Called when the error recovery driver tells us that its
  7495. * OK to resume normal operation. Use completion to allow
  7496. * halted scsi ops to resume.
  7497. */
  7498. static void
  7499. _scsih_pci_resume(struct pci_dev *pdev)
  7500. {
  7501. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7502. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7503. printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
  7504. pci_cleanup_aer_uncorrect_error_status(pdev);
  7505. mpt2sas_base_start_watchdog(ioc);
  7506. scsi_unblock_requests(ioc->shost);
  7507. }
  7508. /**
  7509. * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
  7510. * @pdev: pointer to PCI device
  7511. */
  7512. static pci_ers_result_t
  7513. _scsih_pci_mmio_enabled(struct pci_dev *pdev)
  7514. {
  7515. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7516. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  7517. printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
  7518. ioc->name);
  7519. /* TODO - dump whatever for debugging purposes */
  7520. /* Request a slot reset. */
  7521. return PCI_ERS_RESULT_NEED_RESET;
  7522. }
  7523. static struct pci_error_handlers _scsih_err_handler = {
  7524. .error_detected = _scsih_pci_error_detected,
  7525. .mmio_enabled = _scsih_pci_mmio_enabled,
  7526. .slot_reset = _scsih_pci_slot_reset,
  7527. .resume = _scsih_pci_resume,
  7528. };
  7529. static struct pci_driver scsih_driver = {
  7530. .name = MPT2SAS_DRIVER_NAME,
  7531. .id_table = scsih_pci_table,
  7532. .probe = _scsih_probe,
  7533. .remove = __devexit_p(_scsih_remove),
  7534. .shutdown = _scsih_shutdown,
  7535. .err_handler = &_scsih_err_handler,
  7536. #ifdef CONFIG_PM
  7537. .suspend = _scsih_suspend,
  7538. .resume = _scsih_resume,
  7539. #endif
  7540. };
  7541. /* raid transport support */
  7542. static struct raid_function_template mpt2sas_raid_functions = {
  7543. .cookie = &scsih_driver_template,
  7544. .is_raid = _scsih_is_raid,
  7545. .get_resync = _scsih_get_resync,
  7546. .get_state = _scsih_get_state,
  7547. };
  7548. /**
  7549. * _scsih_init - main entry point for this driver.
  7550. *
  7551. * Returns 0 success, anything else error.
  7552. */
  7553. static int __init
  7554. _scsih_init(void)
  7555. {
  7556. int error;
  7557. mpt_ids = 0;
  7558. printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
  7559. MPT2SAS_DRIVER_VERSION);
  7560. mpt2sas_transport_template =
  7561. sas_attach_transport(&mpt2sas_transport_functions);
  7562. if (!mpt2sas_transport_template)
  7563. return -ENODEV;
  7564. /* raid transport support */
  7565. mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
  7566. if (!mpt2sas_raid_template) {
  7567. sas_release_transport(mpt2sas_transport_template);
  7568. return -ENODEV;
  7569. }
  7570. mpt2sas_base_initialize_callback_handler();
  7571. /* queuecommand callback hander */
  7572. scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
  7573. /* task management callback handler */
  7574. tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
  7575. /* base internal commands callback handler */
  7576. base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
  7577. port_enable_cb_idx = mpt2sas_base_register_callback_handler(
  7578. mpt2sas_port_enable_done);
  7579. /* transport internal commands callback handler */
  7580. transport_cb_idx = mpt2sas_base_register_callback_handler(
  7581. mpt2sas_transport_done);
  7582. /* scsih internal commands callback handler */
  7583. scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
  7584. /* configuration page API internal commands callback handler */
  7585. config_cb_idx = mpt2sas_base_register_callback_handler(
  7586. mpt2sas_config_done);
  7587. /* ctl module callback handler */
  7588. ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
  7589. tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
  7590. _scsih_tm_tr_complete);
  7591. tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
  7592. _scsih_tm_volume_tr_complete);
  7593. tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
  7594. _scsih_sas_control_complete);
  7595. mpt2sas_ctl_init();
  7596. error = pci_register_driver(&scsih_driver);
  7597. if (error) {
  7598. /* raid transport support */
  7599. raid_class_release(mpt2sas_raid_template);
  7600. sas_release_transport(mpt2sas_transport_template);
  7601. }
  7602. return error;
  7603. }
  7604. /**
  7605. * _scsih_exit - exit point for this driver (when it is a module).
  7606. *
  7607. * Returns 0 success, anything else error.
  7608. */
  7609. static void __exit
  7610. _scsih_exit(void)
  7611. {
  7612. printk(KERN_INFO "mpt2sas version %s unloading\n",
  7613. MPT2SAS_DRIVER_VERSION);
  7614. pci_unregister_driver(&scsih_driver);
  7615. mpt2sas_ctl_exit();
  7616. mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
  7617. mpt2sas_base_release_callback_handler(tm_cb_idx);
  7618. mpt2sas_base_release_callback_handler(base_cb_idx);
  7619. mpt2sas_base_release_callback_handler(port_enable_cb_idx);
  7620. mpt2sas_base_release_callback_handler(transport_cb_idx);
  7621. mpt2sas_base_release_callback_handler(scsih_cb_idx);
  7622. mpt2sas_base_release_callback_handler(config_cb_idx);
  7623. mpt2sas_base_release_callback_handler(ctl_cb_idx);
  7624. mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
  7625. mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
  7626. mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
  7627. /* raid transport support */
  7628. raid_class_release(mpt2sas_raid_template);
  7629. sas_release_transport(mpt2sas_transport_template);
  7630. }
  7631. module_init(_scsih_init);
  7632. module_exit(_scsih_exit);