mpt2sas_scsih.c 192 KB

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