mpt2sas_scsih.c 239 KB

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