mpt2sas_scsih.c 188 KB

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