mpt2sas_scsih.c 226 KB

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