mpt2sas_scsih.c 208 KB

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