mpt2sas_scsih.c 245 KB

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