mpt2sas_scsih.c 207 KB

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