mpt2sas_scsih.c 208 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307
  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. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2448. Mpi2SasIoUnitControlReply_t *mpi_reply =
  2449. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2450. #endif
  2451. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2452. "sc_complete:handle(0x%04x), (open) "
  2453. "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  2454. ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
  2455. le16_to_cpu(mpi_reply->IOCStatus),
  2456. le32_to_cpu(mpi_reply->IOCLogInfo)));
  2457. return 1;
  2458. }
  2459. /**
  2460. * _scsih_tm_tr_volume_send - send target reset request for volumes
  2461. * @ioc: per adapter object
  2462. * @handle: device handle
  2463. * Context: interrupt time.
  2464. *
  2465. * This is designed to send muliple task management request at the same
  2466. * time to the fifo. If the fifo is full, we will append the request,
  2467. * and process it in a future completion.
  2468. */
  2469. static void
  2470. _scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2471. {
  2472. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2473. u16 smid;
  2474. struct _tr_list *delayed_tr;
  2475. if (ioc->shost_recovery || ioc->remove_host ||
  2476. ioc->pci_error_recovery) {
  2477. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
  2478. "progress!\n", __func__, ioc->name));
  2479. return;
  2480. }
  2481. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
  2482. if (!smid) {
  2483. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  2484. if (!delayed_tr)
  2485. return;
  2486. INIT_LIST_HEAD(&delayed_tr->list);
  2487. delayed_tr->handle = handle;
  2488. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
  2489. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2490. "DELAYED:tr:handle(0x%04x), (open)\n",
  2491. ioc->name, handle));
  2492. return;
  2493. }
  2494. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
  2495. "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
  2496. ioc->tm_tr_volume_cb_idx));
  2497. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2498. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2499. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2500. mpi_request->DevHandle = cpu_to_le16(handle);
  2501. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  2502. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  2503. }
  2504. /**
  2505. * _scsih_tm_volume_tr_complete - target reset completion
  2506. * @ioc: per adapter object
  2507. * @smid: system request message index
  2508. * @msix_index: MSIX table index supplied by the OS
  2509. * @reply: reply message frame(lower 32bit addr)
  2510. * Context: interrupt time.
  2511. *
  2512. * Return 1 meaning mf should be freed from _base_interrupt
  2513. * 0 means the mf is freed from this function.
  2514. */
  2515. static u8
  2516. _scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  2517. u8 msix_index, u32 reply)
  2518. {
  2519. u16 handle;
  2520. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  2521. Mpi2SCSITaskManagementReply_t *mpi_reply =
  2522. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2523. if (ioc->shost_recovery || ioc->remove_host ||
  2524. ioc->pci_error_recovery) {
  2525. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
  2526. "progress!\n", __func__, ioc->name));
  2527. return 1;
  2528. }
  2529. mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
  2530. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  2531. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  2532. dewtprintk(ioc, printk("spurious interrupt: "
  2533. "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
  2534. le16_to_cpu(mpi_reply->DevHandle), smid));
  2535. return 0;
  2536. }
  2537. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2538. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  2539. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  2540. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  2541. le32_to_cpu(mpi_reply->IOCLogInfo),
  2542. le32_to_cpu(mpi_reply->TerminationCount)));
  2543. return _scsih_check_for_pending_tm(ioc, smid);
  2544. }
  2545. /**
  2546. * _scsih_tm_tr_complete -
  2547. * @ioc: per adapter object
  2548. * @smid: system request message index
  2549. * @msix_index: MSIX table index supplied by the OS
  2550. * @reply: reply message frame(lower 32bit addr)
  2551. * Context: interrupt time.
  2552. *
  2553. * This is the target reset completion routine.
  2554. * This code is part of the code to initiate the device removal
  2555. * handshake protocal with controller firmware.
  2556. * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
  2557. *
  2558. * Return 1 meaning mf should be freed from _base_interrupt
  2559. * 0 means the mf is freed from this function.
  2560. */
  2561. static u8
  2562. _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  2563. u32 reply)
  2564. {
  2565. u16 handle;
  2566. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  2567. Mpi2SCSITaskManagementReply_t *mpi_reply =
  2568. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2569. Mpi2SasIoUnitControlRequest_t *mpi_request;
  2570. u16 smid_sas_ctrl;
  2571. if (ioc->shost_recovery || ioc->remove_host ||
  2572. ioc->pci_error_recovery) {
  2573. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
  2574. "progress!\n", __func__, ioc->name));
  2575. return 1;
  2576. }
  2577. mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
  2578. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  2579. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  2580. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
  2581. "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
  2582. le16_to_cpu(mpi_reply->DevHandle), smid));
  2583. return 0;
  2584. }
  2585. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2586. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  2587. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  2588. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  2589. le32_to_cpu(mpi_reply->IOCLogInfo),
  2590. le32_to_cpu(mpi_reply->TerminationCount)));
  2591. smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
  2592. if (!smid_sas_ctrl) {
  2593. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2594. ioc->name, __func__);
  2595. return 1;
  2596. }
  2597. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
  2598. "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
  2599. ioc->tm_sas_control_cb_idx));
  2600. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
  2601. memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  2602. mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  2603. mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  2604. mpi_request->DevHandle = mpi_request_tm->DevHandle;
  2605. mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
  2606. return _scsih_check_for_pending_tm(ioc, smid);
  2607. }
  2608. /**
  2609. * _scsih_check_for_pending_tm - check for pending task management
  2610. * @ioc: per adapter object
  2611. * @smid: system request message index
  2612. *
  2613. * This will check delayed target reset list, and feed the
  2614. * next reqeust.
  2615. *
  2616. * Return 1 meaning mf should be freed from _base_interrupt
  2617. * 0 means the mf is freed from this function.
  2618. */
  2619. static u8
  2620. _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  2621. {
  2622. struct _tr_list *delayed_tr;
  2623. if (!list_empty(&ioc->delayed_tr_volume_list)) {
  2624. delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
  2625. struct _tr_list, list);
  2626. mpt2sas_base_free_smid(ioc, smid);
  2627. _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
  2628. list_del(&delayed_tr->list);
  2629. kfree(delayed_tr);
  2630. return 0;
  2631. }
  2632. if (!list_empty(&ioc->delayed_tr_list)) {
  2633. delayed_tr = list_entry(ioc->delayed_tr_list.next,
  2634. struct _tr_list, list);
  2635. mpt2sas_base_free_smid(ioc, smid);
  2636. _scsih_tm_tr_send(ioc, delayed_tr->handle);
  2637. list_del(&delayed_tr->list);
  2638. kfree(delayed_tr);
  2639. return 0;
  2640. }
  2641. return 1;
  2642. }
  2643. /**
  2644. * _scsih_check_topo_delete_events - sanity check on topo events
  2645. * @ioc: per adapter object
  2646. * @event_data: the event data payload
  2647. *
  2648. * This routine added to better handle cable breaker.
  2649. *
  2650. * This handles the case where driver recieves multiple expander
  2651. * add and delete events in a single shot. When there is a delete event
  2652. * the routine will void any pending add events waiting in the event queue.
  2653. *
  2654. * Return nothing.
  2655. */
  2656. static void
  2657. _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
  2658. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2659. {
  2660. struct fw_event_work *fw_event;
  2661. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  2662. u16 expander_handle;
  2663. struct _sas_node *sas_expander;
  2664. unsigned long flags;
  2665. int i, reason_code;
  2666. u16 handle;
  2667. for (i = 0 ; i < event_data->NumEntries; i++) {
  2668. if (event_data->PHY[i].PhyStatus &
  2669. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
  2670. continue;
  2671. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2672. if (!handle)
  2673. continue;
  2674. reason_code = event_data->PHY[i].PhyStatus &
  2675. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2676. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
  2677. _scsih_tm_tr_send(ioc, handle);
  2678. }
  2679. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  2680. if (expander_handle < ioc->sas_hba.num_phys) {
  2681. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2682. return;
  2683. }
  2684. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
  2685. || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
  2686. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2687. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  2688. expander_handle);
  2689. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2690. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  2691. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  2692. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2693. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  2694. return;
  2695. /* mark ignore flag for pending events */
  2696. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2697. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  2698. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  2699. fw_event->ignore)
  2700. continue;
  2701. local_event_data = fw_event->event_data;
  2702. if (local_event_data->ExpStatus ==
  2703. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  2704. local_event_data->ExpStatus ==
  2705. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  2706. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  2707. expander_handle) {
  2708. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2709. "setting ignoring flag\n", ioc->name));
  2710. fw_event->ignore = 1;
  2711. }
  2712. }
  2713. }
  2714. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2715. }
  2716. /**
  2717. * _scsih_set_volume_delete_flag - setting volume delete flag
  2718. * @ioc: per adapter object
  2719. * @handle: device handle
  2720. *
  2721. * This
  2722. * Return nothing.
  2723. */
  2724. static void
  2725. _scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2726. {
  2727. struct _raid_device *raid_device;
  2728. struct MPT2SAS_TARGET *sas_target_priv_data;
  2729. unsigned long flags;
  2730. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  2731. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  2732. if (raid_device && raid_device->starget &&
  2733. raid_device->starget->hostdata) {
  2734. sas_target_priv_data =
  2735. raid_device->starget->hostdata;
  2736. sas_target_priv_data->deleted = 1;
  2737. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2738. "setting delete flag: handle(0x%04x), "
  2739. "wwid(0x%016llx)\n", ioc->name, handle,
  2740. (unsigned long long) raid_device->wwid));
  2741. }
  2742. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  2743. }
  2744. /**
  2745. * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
  2746. * @handle: input handle
  2747. * @a: handle for volume a
  2748. * @b: handle for volume b
  2749. *
  2750. * IR firmware only supports two raid volumes. The purpose of this
  2751. * routine is to set the volume handle in either a or b. When the given
  2752. * input handle is non-zero, or when a and b have not been set before.
  2753. */
  2754. static void
  2755. _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
  2756. {
  2757. if (!handle || handle == *a || handle == *b)
  2758. return;
  2759. if (!*a)
  2760. *a = handle;
  2761. else if (!*b)
  2762. *b = handle;
  2763. }
  2764. /**
  2765. * _scsih_check_ir_config_unhide_events - check for UNHIDE events
  2766. * @ioc: per adapter object
  2767. * @event_data: the event data payload
  2768. * Context: interrupt time.
  2769. *
  2770. * This routine will send target reset to volume, followed by target
  2771. * resets to the PDs. This is called when a PD has been removed, or
  2772. * volume has been deleted or removed. When the target reset is sent
  2773. * to volume, the PD target resets need to be queued to start upon
  2774. * completion of the volume target reset.
  2775. *
  2776. * Return nothing.
  2777. */
  2778. static void
  2779. _scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
  2780. Mpi2EventDataIrConfigChangeList_t *event_data)
  2781. {
  2782. Mpi2EventIrConfigElement_t *element;
  2783. int i;
  2784. u16 handle, volume_handle, a, b;
  2785. struct _tr_list *delayed_tr;
  2786. a = 0;
  2787. b = 0;
  2788. /* Volume Resets for Deleted or Removed */
  2789. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  2790. for (i = 0; i < event_data->NumElements; i++, element++) {
  2791. if (element->ReasonCode ==
  2792. MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
  2793. element->ReasonCode ==
  2794. MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
  2795. volume_handle = le16_to_cpu(element->VolDevHandle);
  2796. _scsih_set_volume_delete_flag(ioc, volume_handle);
  2797. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  2798. }
  2799. }
  2800. /* Volume Resets for UNHIDE events */
  2801. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  2802. for (i = 0; i < event_data->NumElements; i++, element++) {
  2803. if (le32_to_cpu(event_data->Flags) &
  2804. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  2805. continue;
  2806. if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
  2807. volume_handle = le16_to_cpu(element->VolDevHandle);
  2808. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  2809. }
  2810. }
  2811. if (a)
  2812. _scsih_tm_tr_volume_send(ioc, a);
  2813. if (b)
  2814. _scsih_tm_tr_volume_send(ioc, b);
  2815. /* PD target resets */
  2816. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  2817. for (i = 0; i < event_data->NumElements; i++, element++) {
  2818. if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
  2819. continue;
  2820. handle = le16_to_cpu(element->PhysDiskDevHandle);
  2821. volume_handle = le16_to_cpu(element->VolDevHandle);
  2822. clear_bit(handle, ioc->pd_handles);
  2823. if (!volume_handle)
  2824. _scsih_tm_tr_send(ioc, handle);
  2825. else if (volume_handle == a || volume_handle == b) {
  2826. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  2827. BUG_ON(!delayed_tr);
  2828. INIT_LIST_HEAD(&delayed_tr->list);
  2829. delayed_tr->handle = handle;
  2830. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  2831. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2832. "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
  2833. handle));
  2834. } else
  2835. _scsih_tm_tr_send(ioc, handle);
  2836. }
  2837. }
  2838. /**
  2839. * _scsih_check_volume_delete_events - set delete flag for volumes
  2840. * @ioc: per adapter object
  2841. * @event_data: the event data payload
  2842. * Context: interrupt time.
  2843. *
  2844. * This will handle the case when the cable connected to entire volume is
  2845. * pulled. We will take care of setting the deleted flag so normal IO will
  2846. * not be sent.
  2847. *
  2848. * Return nothing.
  2849. */
  2850. static void
  2851. _scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
  2852. Mpi2EventDataIrVolume_t *event_data)
  2853. {
  2854. u32 state;
  2855. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  2856. return;
  2857. state = le32_to_cpu(event_data->NewValue);
  2858. if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
  2859. MPI2_RAID_VOL_STATE_FAILED)
  2860. _scsih_set_volume_delete_flag(ioc,
  2861. le16_to_cpu(event_data->VolDevHandle));
  2862. }
  2863. /**
  2864. * _scsih_flush_running_cmds - completing outstanding commands.
  2865. * @ioc: per adapter object
  2866. *
  2867. * The flushing out of all pending scmd commands following host reset,
  2868. * where all IO is dropped to the floor.
  2869. *
  2870. * Return nothing.
  2871. */
  2872. static void
  2873. _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
  2874. {
  2875. struct scsi_cmnd *scmd;
  2876. u16 smid;
  2877. u16 count = 0;
  2878. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  2879. scmd = _scsih_scsi_lookup_get(ioc, smid);
  2880. if (!scmd)
  2881. continue;
  2882. count++;
  2883. mpt2sas_base_free_smid(ioc, smid);
  2884. scsi_dma_unmap(scmd);
  2885. if (ioc->pci_error_recovery)
  2886. scmd->result = DID_NO_CONNECT << 16;
  2887. else
  2888. scmd->result = DID_RESET << 16;
  2889. scmd->scsi_done(scmd);
  2890. }
  2891. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
  2892. ioc->name, count));
  2893. }
  2894. /**
  2895. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  2896. * @scmd: pointer to scsi command object
  2897. * @mpi_request: pointer to the SCSI_IO reqest message frame
  2898. *
  2899. * Supporting protection 1 and 3.
  2900. *
  2901. * Returns nothing
  2902. */
  2903. static void
  2904. _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
  2905. {
  2906. u16 eedp_flags;
  2907. unsigned char prot_op = scsi_get_prot_op(scmd);
  2908. unsigned char prot_type = scsi_get_prot_type(scmd);
  2909. if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
  2910. return;
  2911. if (prot_op == SCSI_PROT_READ_STRIP)
  2912. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  2913. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  2914. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  2915. else
  2916. return;
  2917. switch (prot_type) {
  2918. case SCSI_PROT_DIF_TYPE1:
  2919. /*
  2920. * enable ref/guard checking
  2921. * auto increment ref tag
  2922. */
  2923. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  2924. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  2925. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2926. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  2927. cpu_to_be32(scsi_get_lba(scmd));
  2928. break;
  2929. case SCSI_PROT_DIF_TYPE2:
  2930. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  2931. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  2932. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2933. break;
  2934. case SCSI_PROT_DIF_TYPE3:
  2935. /*
  2936. * enable guard checking
  2937. */
  2938. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2939. break;
  2940. }
  2941. mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
  2942. mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
  2943. }
  2944. /**
  2945. * _scsih_eedp_error_handling - return sense code for EEDP errors
  2946. * @scmd: pointer to scsi command object
  2947. * @ioc_status: ioc status
  2948. *
  2949. * Returns nothing
  2950. */
  2951. static void
  2952. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  2953. {
  2954. u8 ascq;
  2955. u8 sk;
  2956. u8 host_byte;
  2957. switch (ioc_status) {
  2958. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2959. ascq = 0x01;
  2960. break;
  2961. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2962. ascq = 0x02;
  2963. break;
  2964. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2965. ascq = 0x03;
  2966. break;
  2967. default:
  2968. ascq = 0x00;
  2969. break;
  2970. }
  2971. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2972. sk = ILLEGAL_REQUEST;
  2973. host_byte = DID_ABORT;
  2974. } else {
  2975. sk = ABORTED_COMMAND;
  2976. host_byte = DID_OK;
  2977. }
  2978. scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
  2979. scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
  2980. SAM_STAT_CHECK_CONDITION;
  2981. }
  2982. /**
  2983. * _scsih_qcmd - main scsi request entry point
  2984. * @scmd: pointer to scsi command object
  2985. * @done: function pointer to be invoked on completion
  2986. *
  2987. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  2988. *
  2989. * Returns 0 on success. If there's a failure, return either:
  2990. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  2991. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  2992. */
  2993. static int
  2994. _scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
  2995. {
  2996. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2997. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2998. struct MPT2SAS_TARGET *sas_target_priv_data;
  2999. Mpi2SCSIIORequest_t *mpi_request;
  3000. u32 mpi_control;
  3001. u16 smid;
  3002. scmd->scsi_done = done;
  3003. sas_device_priv_data = scmd->device->hostdata;
  3004. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  3005. scmd->result = DID_NO_CONNECT << 16;
  3006. scmd->scsi_done(scmd);
  3007. return 0;
  3008. }
  3009. if (ioc->pci_error_recovery) {
  3010. scmd->result = DID_NO_CONNECT << 16;
  3011. scmd->scsi_done(scmd);
  3012. return 0;
  3013. }
  3014. sas_target_priv_data = sas_device_priv_data->sas_target;
  3015. /* invalid device handle */
  3016. if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
  3017. scmd->result = DID_NO_CONNECT << 16;
  3018. scmd->scsi_done(scmd);
  3019. return 0;
  3020. }
  3021. /* host recovery or link resets sent via IOCTLs */
  3022. if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
  3023. return SCSI_MLQUEUE_HOST_BUSY;
  3024. /* device busy with task management */
  3025. else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
  3026. return SCSI_MLQUEUE_DEVICE_BUSY;
  3027. /* device has been deleted */
  3028. else if (sas_target_priv_data->deleted) {
  3029. scmd->result = DID_NO_CONNECT << 16;
  3030. scmd->scsi_done(scmd);
  3031. return 0;
  3032. }
  3033. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  3034. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  3035. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  3036. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  3037. else
  3038. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  3039. /* set tags */
  3040. if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
  3041. if (scmd->device->tagged_supported) {
  3042. if (scmd->device->ordered_tags)
  3043. mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
  3044. else
  3045. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  3046. } else
  3047. /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
  3048. /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
  3049. */
  3050. mpi_control |= (0x500);
  3051. } else
  3052. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  3053. /* Make sure Device is not raid volume */
  3054. if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
  3055. sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
  3056. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  3057. smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
  3058. if (!smid) {
  3059. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3060. ioc->name, __func__);
  3061. goto out;
  3062. }
  3063. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3064. memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
  3065. _scsih_setup_eedp(scmd, mpi_request);
  3066. if (scmd->cmd_len == 32)
  3067. mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
  3068. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  3069. if (sas_device_priv_data->sas_target->flags &
  3070. MPT_TARGET_FLAGS_RAID_COMPONENT)
  3071. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  3072. else
  3073. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  3074. mpi_request->DevHandle =
  3075. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  3076. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  3077. mpi_request->Control = cpu_to_le32(mpi_control);
  3078. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  3079. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  3080. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  3081. mpi_request->SenseBufferLowAddress =
  3082. mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  3083. mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
  3084. mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
  3085. MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
  3086. mpi_request->VF_ID = 0; /* TODO */
  3087. mpi_request->VP_ID = 0;
  3088. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  3089. mpi_request->LUN);
  3090. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  3091. if (!mpi_request->DataLength) {
  3092. mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
  3093. } else {
  3094. if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
  3095. mpt2sas_base_free_smid(ioc, smid);
  3096. goto out;
  3097. }
  3098. }
  3099. if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
  3100. mpt2sas_base_put_smid_scsi_io(ioc, smid,
  3101. sas_device_priv_data->sas_target->handle);
  3102. else
  3103. mpt2sas_base_put_smid_default(ioc, smid);
  3104. return 0;
  3105. out:
  3106. return SCSI_MLQUEUE_HOST_BUSY;
  3107. }
  3108. static DEF_SCSI_QCMD(_scsih_qcmd)
  3109. /**
  3110. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  3111. * @sense_buffer: sense data returned by target
  3112. * @data: normalized skey/asc/ascq
  3113. *
  3114. * Return nothing.
  3115. */
  3116. static void
  3117. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  3118. {
  3119. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  3120. /* descriptor format */
  3121. data->skey = sense_buffer[1] & 0x0F;
  3122. data->asc = sense_buffer[2];
  3123. data->ascq = sense_buffer[3];
  3124. } else {
  3125. /* fixed format */
  3126. data->skey = sense_buffer[2] & 0x0F;
  3127. data->asc = sense_buffer[12];
  3128. data->ascq = sense_buffer[13];
  3129. }
  3130. }
  3131. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3132. /**
  3133. * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
  3134. * @ioc: per adapter object
  3135. * @scmd: pointer to scsi command object
  3136. * @mpi_reply: reply mf payload returned from firmware
  3137. *
  3138. * scsi_status - SCSI Status code returned from target device
  3139. * scsi_state - state info associated with SCSI_IO determined by ioc
  3140. * ioc_status - ioc supplied status info
  3141. *
  3142. * Return nothing.
  3143. */
  3144. static void
  3145. _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  3146. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  3147. {
  3148. u32 response_info;
  3149. u8 *response_bytes;
  3150. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  3151. MPI2_IOCSTATUS_MASK;
  3152. u8 scsi_state = mpi_reply->SCSIState;
  3153. u8 scsi_status = mpi_reply->SCSIStatus;
  3154. char *desc_ioc_state = NULL;
  3155. char *desc_scsi_status = NULL;
  3156. char *desc_scsi_state = ioc->tmp_string;
  3157. u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  3158. struct _sas_device *sas_device = NULL;
  3159. unsigned long flags;
  3160. struct scsi_target *starget = scmd->device->sdev_target;
  3161. struct MPT2SAS_TARGET *priv_target = starget->hostdata;
  3162. if (!priv_target)
  3163. return;
  3164. if (log_info == 0x31170000)
  3165. return;
  3166. switch (ioc_status) {
  3167. case MPI2_IOCSTATUS_SUCCESS:
  3168. desc_ioc_state = "success";
  3169. break;
  3170. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  3171. desc_ioc_state = "invalid function";
  3172. break;
  3173. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  3174. desc_ioc_state = "scsi recovered error";
  3175. break;
  3176. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  3177. desc_ioc_state = "scsi invalid dev handle";
  3178. break;
  3179. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  3180. desc_ioc_state = "scsi device not there";
  3181. break;
  3182. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  3183. desc_ioc_state = "scsi data overrun";
  3184. break;
  3185. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  3186. desc_ioc_state = "scsi data underrun";
  3187. break;
  3188. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  3189. desc_ioc_state = "scsi io data error";
  3190. break;
  3191. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  3192. desc_ioc_state = "scsi protocol error";
  3193. break;
  3194. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  3195. desc_ioc_state = "scsi task terminated";
  3196. break;
  3197. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  3198. desc_ioc_state = "scsi residual mismatch";
  3199. break;
  3200. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  3201. desc_ioc_state = "scsi task mgmt failed";
  3202. break;
  3203. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  3204. desc_ioc_state = "scsi ioc terminated";
  3205. break;
  3206. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  3207. desc_ioc_state = "scsi ext terminated";
  3208. break;
  3209. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  3210. desc_ioc_state = "eedp guard error";
  3211. break;
  3212. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  3213. desc_ioc_state = "eedp ref tag error";
  3214. break;
  3215. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  3216. desc_ioc_state = "eedp app tag error";
  3217. break;
  3218. default:
  3219. desc_ioc_state = "unknown";
  3220. break;
  3221. }
  3222. switch (scsi_status) {
  3223. case MPI2_SCSI_STATUS_GOOD:
  3224. desc_scsi_status = "good";
  3225. break;
  3226. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  3227. desc_scsi_status = "check condition";
  3228. break;
  3229. case MPI2_SCSI_STATUS_CONDITION_MET:
  3230. desc_scsi_status = "condition met";
  3231. break;
  3232. case MPI2_SCSI_STATUS_BUSY:
  3233. desc_scsi_status = "busy";
  3234. break;
  3235. case MPI2_SCSI_STATUS_INTERMEDIATE:
  3236. desc_scsi_status = "intermediate";
  3237. break;
  3238. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  3239. desc_scsi_status = "intermediate condmet";
  3240. break;
  3241. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  3242. desc_scsi_status = "reservation conflict";
  3243. break;
  3244. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  3245. desc_scsi_status = "command terminated";
  3246. break;
  3247. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  3248. desc_scsi_status = "task set full";
  3249. break;
  3250. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  3251. desc_scsi_status = "aca active";
  3252. break;
  3253. case MPI2_SCSI_STATUS_TASK_ABORTED:
  3254. desc_scsi_status = "task aborted";
  3255. break;
  3256. default:
  3257. desc_scsi_status = "unknown";
  3258. break;
  3259. }
  3260. desc_scsi_state[0] = '\0';
  3261. if (!scsi_state)
  3262. desc_scsi_state = " ";
  3263. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  3264. strcat(desc_scsi_state, "response info ");
  3265. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  3266. strcat(desc_scsi_state, "state terminated ");
  3267. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  3268. strcat(desc_scsi_state, "no status ");
  3269. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  3270. strcat(desc_scsi_state, "autosense failed ");
  3271. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  3272. strcat(desc_scsi_state, "autosense valid ");
  3273. scsi_print_command(scmd);
  3274. if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  3275. printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
  3276. (unsigned long long)priv_target->sas_address);
  3277. } else {
  3278. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3279. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  3280. priv_target->sas_address);
  3281. if (sas_device) {
  3282. printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
  3283. "phy(%d)\n", ioc->name, sas_device->sas_address,
  3284. sas_device->phy);
  3285. printk(MPT2SAS_WARN_FMT
  3286. "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
  3287. ioc->name, sas_device->enclosure_logical_id,
  3288. sas_device->slot);
  3289. }
  3290. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3291. }
  3292. printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
  3293. "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
  3294. desc_ioc_state, ioc_status, smid);
  3295. printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
  3296. "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
  3297. scsi_get_resid(scmd));
  3298. printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
  3299. "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  3300. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  3301. printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
  3302. "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
  3303. scsi_status, desc_scsi_state, scsi_state);
  3304. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  3305. struct sense_info data;
  3306. _scsih_normalize_sense(scmd->sense_buffer, &data);
  3307. printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
  3308. "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
  3309. data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
  3310. }
  3311. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  3312. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  3313. response_bytes = (u8 *)&response_info;
  3314. _scsih_response_code(ioc, response_bytes[0]);
  3315. }
  3316. }
  3317. #endif
  3318. /**
  3319. * _scsih_smart_predicted_fault - illuminate Fault LED
  3320. * @ioc: per adapter object
  3321. * @handle: device handle
  3322. *
  3323. * Return nothing.
  3324. */
  3325. static void
  3326. _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3327. {
  3328. Mpi2SepReply_t mpi_reply;
  3329. Mpi2SepRequest_t mpi_request;
  3330. struct scsi_target *starget;
  3331. struct MPT2SAS_TARGET *sas_target_priv_data;
  3332. Mpi2EventNotificationReply_t *event_reply;
  3333. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  3334. struct _sas_device *sas_device;
  3335. ssize_t sz;
  3336. unsigned long flags;
  3337. /* only handle non-raid devices */
  3338. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3339. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  3340. if (!sas_device) {
  3341. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3342. return;
  3343. }
  3344. starget = sas_device->starget;
  3345. sas_target_priv_data = starget->hostdata;
  3346. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  3347. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
  3348. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3349. return;
  3350. }
  3351. starget_printk(KERN_WARNING, starget, "predicted fault\n");
  3352. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3353. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
  3354. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  3355. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  3356. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  3357. mpi_request.SlotStatus =
  3358. cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
  3359. mpi_request.DevHandle = cpu_to_le16(handle);
  3360. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  3361. if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  3362. &mpi_request)) != 0) {
  3363. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3364. ioc->name, __FILE__, __LINE__, __func__);
  3365. return;
  3366. }
  3367. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  3368. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  3369. "enclosure_processor: ioc_status (0x%04x), "
  3370. "loginfo(0x%08x)\n", ioc->name,
  3371. le16_to_cpu(mpi_reply.IOCStatus),
  3372. le32_to_cpu(mpi_reply.IOCLogInfo)));
  3373. return;
  3374. }
  3375. }
  3376. /* insert into event log */
  3377. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  3378. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  3379. event_reply = kzalloc(sz, GFP_KERNEL);
  3380. if (!event_reply) {
  3381. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3382. ioc->name, __FILE__, __LINE__, __func__);
  3383. return;
  3384. }
  3385. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  3386. event_reply->Event =
  3387. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  3388. event_reply->MsgLength = sz/4;
  3389. event_reply->EventDataLength =
  3390. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  3391. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  3392. event_reply->EventData;
  3393. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  3394. event_data->ASC = 0x5D;
  3395. event_data->DevHandle = cpu_to_le16(handle);
  3396. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  3397. mpt2sas_ctl_add_to_event_log(ioc, event_reply);
  3398. kfree(event_reply);
  3399. }
  3400. /**
  3401. * _scsih_io_done - scsi request callback
  3402. * @ioc: per adapter object
  3403. * @smid: system request message index
  3404. * @msix_index: MSIX table index supplied by the OS
  3405. * @reply: reply message frame(lower 32bit addr)
  3406. *
  3407. * Callback handler when using _scsih_qcmd.
  3408. *
  3409. * Return 1 meaning mf should be freed from _base_interrupt
  3410. * 0 means the mf is freed from this function.
  3411. */
  3412. static u8
  3413. _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  3414. {
  3415. Mpi2SCSIIORequest_t *mpi_request;
  3416. Mpi2SCSIIOReply_t *mpi_reply;
  3417. struct scsi_cmnd *scmd;
  3418. u16 ioc_status;
  3419. u32 xfer_cnt;
  3420. u8 scsi_state;
  3421. u8 scsi_status;
  3422. u32 log_info;
  3423. struct MPT2SAS_DEVICE *sas_device_priv_data;
  3424. u32 response_code = 0;
  3425. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  3426. scmd = _scsih_scsi_lookup_get(ioc, smid);
  3427. if (scmd == NULL)
  3428. return 1;
  3429. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3430. if (mpi_reply == NULL) {
  3431. scmd->result = DID_OK << 16;
  3432. goto out;
  3433. }
  3434. sas_device_priv_data = scmd->device->hostdata;
  3435. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  3436. sas_device_priv_data->sas_target->deleted) {
  3437. scmd->result = DID_NO_CONNECT << 16;
  3438. goto out;
  3439. }
  3440. /* turning off TLR */
  3441. scsi_state = mpi_reply->SCSIState;
  3442. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  3443. response_code =
  3444. le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
  3445. if (!sas_device_priv_data->tlr_snoop_check) {
  3446. sas_device_priv_data->tlr_snoop_check++;
  3447. if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
  3448. sas_is_tlr_enabled(scmd->device) &&
  3449. response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
  3450. sas_disable_tlr(scmd->device);
  3451. sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
  3452. }
  3453. }
  3454. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  3455. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  3456. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  3457. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  3458. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  3459. else
  3460. log_info = 0;
  3461. ioc_status &= MPI2_IOCSTATUS_MASK;
  3462. scsi_status = mpi_reply->SCSIStatus;
  3463. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  3464. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  3465. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  3466. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  3467. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  3468. }
  3469. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  3470. struct sense_info data;
  3471. const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
  3472. smid);
  3473. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  3474. le32_to_cpu(mpi_reply->SenseCount));
  3475. memcpy(scmd->sense_buffer, sense_data, sz);
  3476. _scsih_normalize_sense(scmd->sense_buffer, &data);
  3477. /* failure prediction threshold exceeded */
  3478. if (data.asc == 0x5D)
  3479. _scsih_smart_predicted_fault(ioc,
  3480. le16_to_cpu(mpi_reply->DevHandle));
  3481. }
  3482. switch (ioc_status) {
  3483. case MPI2_IOCSTATUS_BUSY:
  3484. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  3485. scmd->result = SAM_STAT_BUSY;
  3486. break;
  3487. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  3488. scmd->result = DID_NO_CONNECT << 16;
  3489. break;
  3490. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  3491. if (sas_device_priv_data->block) {
  3492. scmd->result = DID_TRANSPORT_DISRUPTED << 16;
  3493. goto out;
  3494. }
  3495. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  3496. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  3497. scmd->result = DID_RESET << 16;
  3498. break;
  3499. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  3500. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  3501. scmd->result = DID_SOFT_ERROR << 16;
  3502. else
  3503. scmd->result = (DID_OK << 16) | scsi_status;
  3504. break;
  3505. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  3506. scmd->result = (DID_OK << 16) | scsi_status;
  3507. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  3508. break;
  3509. if (xfer_cnt < scmd->underflow) {
  3510. if (scsi_status == SAM_STAT_BUSY)
  3511. scmd->result = SAM_STAT_BUSY;
  3512. else
  3513. scmd->result = DID_SOFT_ERROR << 16;
  3514. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  3515. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  3516. scmd->result = DID_SOFT_ERROR << 16;
  3517. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  3518. scmd->result = DID_RESET << 16;
  3519. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  3520. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  3521. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  3522. scmd->result = (DRIVER_SENSE << 24) |
  3523. SAM_STAT_CHECK_CONDITION;
  3524. scmd->sense_buffer[0] = 0x70;
  3525. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  3526. scmd->sense_buffer[12] = 0x20;
  3527. scmd->sense_buffer[13] = 0;
  3528. }
  3529. break;
  3530. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  3531. scsi_set_resid(scmd, 0);
  3532. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  3533. case MPI2_IOCSTATUS_SUCCESS:
  3534. scmd->result = (DID_OK << 16) | scsi_status;
  3535. if (response_code ==
  3536. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
  3537. (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  3538. MPI2_SCSI_STATE_NO_SCSI_STATUS)))
  3539. scmd->result = DID_SOFT_ERROR << 16;
  3540. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  3541. scmd->result = DID_RESET << 16;
  3542. break;
  3543. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  3544. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  3545. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  3546. _scsih_eedp_error_handling(scmd, ioc_status);
  3547. break;
  3548. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  3549. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  3550. case MPI2_IOCSTATUS_INVALID_SGL:
  3551. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  3552. case MPI2_IOCSTATUS_INVALID_FIELD:
  3553. case MPI2_IOCSTATUS_INVALID_STATE:
  3554. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  3555. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  3556. default:
  3557. scmd->result = DID_SOFT_ERROR << 16;
  3558. break;
  3559. }
  3560. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3561. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  3562. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  3563. #endif
  3564. out:
  3565. scsi_dma_unmap(scmd);
  3566. scmd->scsi_done(scmd);
  3567. return 1;
  3568. }
  3569. /**
  3570. * _scsih_sas_host_refresh - refreshing sas host object contents
  3571. * @ioc: per adapter object
  3572. * Context: user
  3573. *
  3574. * During port enable, fw will send topology events for every device. Its
  3575. * possible that the handles may change from the previous setting, so this
  3576. * code keeping handles updating if changed.
  3577. *
  3578. * Return nothing.
  3579. */
  3580. static void
  3581. _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
  3582. {
  3583. u16 sz;
  3584. u16 ioc_status;
  3585. int i;
  3586. Mpi2ConfigReply_t mpi_reply;
  3587. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  3588. u16 attached_handle;
  3589. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
  3590. "updating handles for sas_host(0x%016llx)\n",
  3591. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  3592. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  3593. * sizeof(Mpi2SasIOUnit0PhyData_t));
  3594. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  3595. if (!sas_iounit_pg0) {
  3596. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3597. ioc->name, __FILE__, __LINE__, __func__);
  3598. return;
  3599. }
  3600. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  3601. sas_iounit_pg0, sz)) != 0)
  3602. goto out;
  3603. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  3604. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  3605. goto out;
  3606. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  3607. if (i == 0)
  3608. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  3609. PhyData[0].ControllerDevHandle);
  3610. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  3611. attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
  3612. AttachedDevHandle);
  3613. mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
  3614. attached_handle, i, sas_iounit_pg0->PhyData[i].
  3615. NegotiatedLinkRate >> 4);
  3616. }
  3617. out:
  3618. kfree(sas_iounit_pg0);
  3619. }
  3620. /**
  3621. * _scsih_sas_host_add - create sas host object
  3622. * @ioc: per adapter object
  3623. *
  3624. * Creating host side data object, stored in ioc->sas_hba
  3625. *
  3626. * Return nothing.
  3627. */
  3628. static void
  3629. _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
  3630. {
  3631. int i;
  3632. Mpi2ConfigReply_t mpi_reply;
  3633. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  3634. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  3635. Mpi2SasPhyPage0_t phy_pg0;
  3636. Mpi2SasDevicePage0_t sas_device_pg0;
  3637. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3638. u16 ioc_status;
  3639. u16 sz;
  3640. u16 device_missing_delay;
  3641. mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
  3642. if (!ioc->sas_hba.num_phys) {
  3643. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3644. ioc->name, __FILE__, __LINE__, __func__);
  3645. return;
  3646. }
  3647. /* sas_iounit page 0 */
  3648. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  3649. sizeof(Mpi2SasIOUnit0PhyData_t));
  3650. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  3651. if (!sas_iounit_pg0) {
  3652. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3653. ioc->name, __FILE__, __LINE__, __func__);
  3654. return;
  3655. }
  3656. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  3657. sas_iounit_pg0, sz))) {
  3658. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3659. ioc->name, __FILE__, __LINE__, __func__);
  3660. goto out;
  3661. }
  3662. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3663. MPI2_IOCSTATUS_MASK;
  3664. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3665. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3666. ioc->name, __FILE__, __LINE__, __func__);
  3667. goto out;
  3668. }
  3669. /* sas_iounit page 1 */
  3670. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  3671. sizeof(Mpi2SasIOUnit1PhyData_t));
  3672. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  3673. if (!sas_iounit_pg1) {
  3674. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3675. ioc->name, __FILE__, __LINE__, __func__);
  3676. goto out;
  3677. }
  3678. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  3679. sas_iounit_pg1, sz))) {
  3680. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3681. ioc->name, __FILE__, __LINE__, __func__);
  3682. goto out;
  3683. }
  3684. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3685. MPI2_IOCSTATUS_MASK;
  3686. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3687. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3688. ioc->name, __FILE__, __LINE__, __func__);
  3689. goto out;
  3690. }
  3691. ioc->io_missing_delay =
  3692. le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
  3693. device_missing_delay =
  3694. le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
  3695. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  3696. ioc->device_missing_delay = (device_missing_delay &
  3697. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  3698. else
  3699. ioc->device_missing_delay = device_missing_delay &
  3700. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  3701. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  3702. ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
  3703. sizeof(struct _sas_phy), GFP_KERNEL);
  3704. if (!ioc->sas_hba.phy) {
  3705. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3706. ioc->name, __FILE__, __LINE__, __func__);
  3707. goto out;
  3708. }
  3709. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  3710. if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  3711. i))) {
  3712. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3713. ioc->name, __FILE__, __LINE__, __func__);
  3714. goto out;
  3715. }
  3716. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3717. MPI2_IOCSTATUS_MASK;
  3718. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3719. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3720. ioc->name, __FILE__, __LINE__, __func__);
  3721. goto out;
  3722. }
  3723. if (i == 0)
  3724. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  3725. PhyData[0].ControllerDevHandle);
  3726. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  3727. ioc->sas_hba.phy[i].phy_id = i;
  3728. mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  3729. phy_pg0, ioc->sas_hba.parent_dev);
  3730. }
  3731. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3732. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
  3733. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3734. ioc->name, __FILE__, __LINE__, __func__);
  3735. goto out;
  3736. }
  3737. ioc->sas_hba.enclosure_handle =
  3738. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3739. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3740. printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
  3741. "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
  3742. (unsigned long long) ioc->sas_hba.sas_address,
  3743. ioc->sas_hba.num_phys) ;
  3744. if (ioc->sas_hba.enclosure_handle) {
  3745. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3746. &enclosure_pg0,
  3747. MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3748. ioc->sas_hba.enclosure_handle))) {
  3749. ioc->sas_hba.enclosure_logical_id =
  3750. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3751. }
  3752. }
  3753. out:
  3754. kfree(sas_iounit_pg1);
  3755. kfree(sas_iounit_pg0);
  3756. }
  3757. /**
  3758. * _scsih_expander_add - creating expander object
  3759. * @ioc: per adapter object
  3760. * @handle: expander handle
  3761. *
  3762. * Creating expander object, stored in ioc->sas_expander_list.
  3763. *
  3764. * Return 0 for success, else error.
  3765. */
  3766. static int
  3767. _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3768. {
  3769. struct _sas_node *sas_expander;
  3770. Mpi2ConfigReply_t mpi_reply;
  3771. Mpi2ExpanderPage0_t expander_pg0;
  3772. Mpi2ExpanderPage1_t expander_pg1;
  3773. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3774. u32 ioc_status;
  3775. u16 parent_handle;
  3776. __le64 sas_address, sas_address_parent = 0;
  3777. int i;
  3778. unsigned long flags;
  3779. struct _sas_port *mpt2sas_port = NULL;
  3780. int rc = 0;
  3781. if (!handle)
  3782. return -1;
  3783. if (ioc->shost_recovery || ioc->pci_error_recovery)
  3784. return -1;
  3785. if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  3786. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  3787. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3788. ioc->name, __FILE__, __LINE__, __func__);
  3789. return -1;
  3790. }
  3791. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3792. MPI2_IOCSTATUS_MASK;
  3793. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3794. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3795. ioc->name, __FILE__, __LINE__, __func__);
  3796. return -1;
  3797. }
  3798. /* handle out of order topology events */
  3799. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  3800. if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
  3801. != 0) {
  3802. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3803. ioc->name, __FILE__, __LINE__, __func__);
  3804. return -1;
  3805. }
  3806. if (sas_address_parent != ioc->sas_hba.sas_address) {
  3807. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3808. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3809. sas_address_parent);
  3810. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3811. if (!sas_expander) {
  3812. rc = _scsih_expander_add(ioc, parent_handle);
  3813. if (rc != 0)
  3814. return rc;
  3815. }
  3816. }
  3817. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3818. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  3819. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3820. sas_address);
  3821. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3822. if (sas_expander)
  3823. return 0;
  3824. sas_expander = kzalloc(sizeof(struct _sas_node),
  3825. GFP_KERNEL);
  3826. if (!sas_expander) {
  3827. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3828. ioc->name, __FILE__, __LINE__, __func__);
  3829. return -1;
  3830. }
  3831. sas_expander->handle = handle;
  3832. sas_expander->num_phys = expander_pg0.NumPhys;
  3833. sas_expander->sas_address_parent = sas_address_parent;
  3834. sas_expander->sas_address = sas_address;
  3835. printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
  3836. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  3837. handle, parent_handle, (unsigned long long)
  3838. sas_expander->sas_address, sas_expander->num_phys);
  3839. if (!sas_expander->num_phys)
  3840. goto out_fail;
  3841. sas_expander->phy = kcalloc(sas_expander->num_phys,
  3842. sizeof(struct _sas_phy), GFP_KERNEL);
  3843. if (!sas_expander->phy) {
  3844. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3845. ioc->name, __FILE__, __LINE__, __func__);
  3846. rc = -1;
  3847. goto out_fail;
  3848. }
  3849. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  3850. mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
  3851. sas_address_parent);
  3852. if (!mpt2sas_port) {
  3853. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3854. ioc->name, __FILE__, __LINE__, __func__);
  3855. rc = -1;
  3856. goto out_fail;
  3857. }
  3858. sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
  3859. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  3860. if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
  3861. &expander_pg1, i, handle))) {
  3862. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3863. ioc->name, __FILE__, __LINE__, __func__);
  3864. rc = -1;
  3865. goto out_fail;
  3866. }
  3867. sas_expander->phy[i].handle = handle;
  3868. sas_expander->phy[i].phy_id = i;
  3869. if ((mpt2sas_transport_add_expander_phy(ioc,
  3870. &sas_expander->phy[i], expander_pg1,
  3871. sas_expander->parent_dev))) {
  3872. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3873. ioc->name, __FILE__, __LINE__, __func__);
  3874. rc = -1;
  3875. goto out_fail;
  3876. }
  3877. }
  3878. if (sas_expander->enclosure_handle) {
  3879. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3880. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3881. sas_expander->enclosure_handle))) {
  3882. sas_expander->enclosure_logical_id =
  3883. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3884. }
  3885. }
  3886. _scsih_expander_node_add(ioc, sas_expander);
  3887. return 0;
  3888. out_fail:
  3889. if (mpt2sas_port)
  3890. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  3891. sas_address_parent);
  3892. kfree(sas_expander);
  3893. return rc;
  3894. }
  3895. /**
  3896. * _scsih_done - scsih callback handler.
  3897. * @ioc: per adapter object
  3898. * @smid: system request message index
  3899. * @msix_index: MSIX table index supplied by the OS
  3900. * @reply: reply message frame(lower 32bit addr)
  3901. *
  3902. * Callback handler when sending internal generated message frames.
  3903. * The callback index passed is `ioc->scsih_cb_idx`
  3904. *
  3905. * Return 1 meaning mf should be freed from _base_interrupt
  3906. * 0 means the mf is freed from this function.
  3907. */
  3908. static u8
  3909. _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  3910. {
  3911. MPI2DefaultReply_t *mpi_reply;
  3912. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  3913. if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
  3914. return 1;
  3915. if (ioc->scsih_cmds.smid != smid)
  3916. return 1;
  3917. ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
  3918. if (mpi_reply) {
  3919. memcpy(ioc->scsih_cmds.reply, mpi_reply,
  3920. mpi_reply->MsgLength*4);
  3921. ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
  3922. }
  3923. ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
  3924. complete(&ioc->scsih_cmds.done);
  3925. return 1;
  3926. }
  3927. /**
  3928. * _scsih_expander_remove - removing expander object
  3929. * @ioc: per adapter object
  3930. * @sas_address: expander sas_address
  3931. *
  3932. * Return nothing.
  3933. */
  3934. static void
  3935. _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
  3936. {
  3937. struct _sas_node *sas_expander;
  3938. unsigned long flags;
  3939. if (ioc->shost_recovery)
  3940. return;
  3941. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3942. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3943. sas_address);
  3944. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3945. _scsih_expander_node_remove(ioc, sas_expander);
  3946. }
  3947. /**
  3948. * _scsih_check_access_status - check access flags
  3949. * @ioc: per adapter object
  3950. * @sas_address: sas address
  3951. * @handle: sas device handle
  3952. * @access_flags: errors returned during discovery of the device
  3953. *
  3954. * Return 0 for success, else failure
  3955. */
  3956. static u8
  3957. _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  3958. u16 handle, u8 access_status)
  3959. {
  3960. u8 rc = 1;
  3961. char *desc = NULL;
  3962. switch (access_status) {
  3963. case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
  3964. case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
  3965. rc = 0;
  3966. break;
  3967. case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
  3968. desc = "sata capability failed";
  3969. break;
  3970. case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
  3971. desc = "sata affiliation conflict";
  3972. break;
  3973. case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
  3974. desc = "route not addressable";
  3975. break;
  3976. case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
  3977. desc = "smp error not addressable";
  3978. break;
  3979. case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
  3980. desc = "device blocked";
  3981. break;
  3982. case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
  3983. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
  3984. case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
  3985. case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
  3986. case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
  3987. case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
  3988. case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
  3989. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
  3990. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
  3991. case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
  3992. case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
  3993. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
  3994. desc = "sata initialization failed";
  3995. break;
  3996. default:
  3997. desc = "unknown";
  3998. break;
  3999. }
  4000. if (!rc)
  4001. return 0;
  4002. printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
  4003. "handle(0x%04x)\n", ioc->name, desc,
  4004. (unsigned long long)sas_address, handle);
  4005. return rc;
  4006. }
  4007. static void
  4008. _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  4009. {
  4010. Mpi2ConfigReply_t mpi_reply;
  4011. Mpi2SasDevicePage0_t sas_device_pg0;
  4012. struct _sas_device *sas_device;
  4013. u32 ioc_status;
  4014. unsigned long flags;
  4015. u64 sas_address;
  4016. struct scsi_target *starget;
  4017. struct MPT2SAS_TARGET *sas_target_priv_data;
  4018. u32 device_info;
  4019. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4020. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
  4021. return;
  4022. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  4023. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  4024. return;
  4025. /* check if this is end device */
  4026. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4027. if (!(_scsih_is_end_device(device_info)))
  4028. return;
  4029. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4030. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4031. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4032. sas_address);
  4033. if (!sas_device) {
  4034. printk(MPT2SAS_ERR_FMT "device is not present "
  4035. "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
  4036. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4037. return;
  4038. }
  4039. if (unlikely(sas_device->handle != handle)) {
  4040. starget = sas_device->starget;
  4041. sas_target_priv_data = starget->hostdata;
  4042. starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
  4043. " to (0x%04x)!!!\n", sas_device->handle, handle);
  4044. sas_target_priv_data->handle = handle;
  4045. sas_device->handle = handle;
  4046. }
  4047. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4048. /* check if device is present */
  4049. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  4050. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  4051. printk(MPT2SAS_ERR_FMT "device is not present "
  4052. "handle(0x%04x), flags!!!\n", ioc->name, handle);
  4053. return;
  4054. }
  4055. /* check if there were any issues with discovery */
  4056. if (_scsih_check_access_status(ioc, sas_address, handle,
  4057. sas_device_pg0.AccessStatus))
  4058. return;
  4059. _scsih_ublock_io_device(ioc, handle);
  4060. }
  4061. /**
  4062. * _scsih_add_device - creating sas device object
  4063. * @ioc: per adapter object
  4064. * @handle: sas device handle
  4065. * @phy_num: phy number end device attached to
  4066. * @is_pd: is this hidden raid component
  4067. *
  4068. * Creating end device object, stored in ioc->sas_device_list.
  4069. *
  4070. * Returns 0 for success, non-zero for failure.
  4071. */
  4072. static int
  4073. _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
  4074. {
  4075. Mpi2ConfigReply_t mpi_reply;
  4076. Mpi2SasDevicePage0_t sas_device_pg0;
  4077. Mpi2SasEnclosurePage0_t enclosure_pg0;
  4078. struct _sas_device *sas_device;
  4079. u32 ioc_status;
  4080. __le64 sas_address;
  4081. u32 device_info;
  4082. unsigned long flags;
  4083. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4084. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  4085. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4086. ioc->name, __FILE__, __LINE__, __func__);
  4087. return -1;
  4088. }
  4089. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4090. MPI2_IOCSTATUS_MASK;
  4091. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4092. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4093. ioc->name, __FILE__, __LINE__, __func__);
  4094. return -1;
  4095. }
  4096. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4097. /* check if device is present */
  4098. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  4099. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  4100. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4101. ioc->name, __FILE__, __LINE__, __func__);
  4102. printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
  4103. ioc->name, le16_to_cpu(sas_device_pg0.Flags));
  4104. return -1;
  4105. }
  4106. /* check if there were any issues with discovery */
  4107. if (_scsih_check_access_status(ioc, sas_address, handle,
  4108. sas_device_pg0.AccessStatus))
  4109. return -1;
  4110. /* check if this is end device */
  4111. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4112. if (!(_scsih_is_end_device(device_info))) {
  4113. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4114. ioc->name, __FILE__, __LINE__, __func__);
  4115. return -1;
  4116. }
  4117. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4118. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4119. sas_address);
  4120. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4121. if (sas_device)
  4122. return 0;
  4123. sas_device = kzalloc(sizeof(struct _sas_device),
  4124. GFP_KERNEL);
  4125. if (!sas_device) {
  4126. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4127. ioc->name, __FILE__, __LINE__, __func__);
  4128. return -1;
  4129. }
  4130. sas_device->handle = handle;
  4131. if (_scsih_get_sas_address(ioc, le16_to_cpu
  4132. (sas_device_pg0.ParentDevHandle),
  4133. &sas_device->sas_address_parent) != 0)
  4134. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4135. ioc->name, __FILE__, __LINE__, __func__);
  4136. sas_device->enclosure_handle =
  4137. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  4138. sas_device->slot =
  4139. le16_to_cpu(sas_device_pg0.Slot);
  4140. sas_device->device_info = device_info;
  4141. sas_device->sas_address = sas_address;
  4142. sas_device->phy = sas_device_pg0.PhyNum;
  4143. /* get enclosure_logical_id */
  4144. if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
  4145. ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  4146. sas_device->enclosure_handle)))
  4147. sas_device->enclosure_logical_id =
  4148. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  4149. /* get device name */
  4150. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  4151. if (ioc->wait_for_port_enable_to_complete)
  4152. _scsih_sas_device_init_add(ioc, sas_device);
  4153. else
  4154. _scsih_sas_device_add(ioc, sas_device);
  4155. return 0;
  4156. }
  4157. /**
  4158. * _scsih_remove_device - removing sas device object
  4159. * @ioc: per adapter object
  4160. * @sas_device_delete: the sas_device object
  4161. *
  4162. * Return nothing.
  4163. */
  4164. static void
  4165. _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
  4166. struct _sas_device *sas_device)
  4167. {
  4168. struct _sas_device sas_device_backup;
  4169. struct MPT2SAS_TARGET *sas_target_priv_data;
  4170. if (!sas_device)
  4171. return;
  4172. memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
  4173. _scsih_sas_device_remove(ioc, sas_device);
  4174. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
  4175. "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  4176. sas_device_backup.handle, (unsigned long long)
  4177. sas_device_backup.sas_address));
  4178. if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
  4179. sas_target_priv_data = sas_device_backup.starget->hostdata;
  4180. sas_target_priv_data->deleted = 1;
  4181. }
  4182. _scsih_ublock_io_device(ioc, sas_device_backup.handle);
  4183. mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
  4184. sas_device_backup.sas_address_parent);
  4185. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
  4186. "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
  4187. (unsigned long long) sas_device_backup.sas_address);
  4188. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
  4189. "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  4190. sas_device_backup.handle, (unsigned long long)
  4191. sas_device_backup.sas_address));
  4192. }
  4193. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4194. /**
  4195. * _scsih_sas_topology_change_event_debug - debug for topology event
  4196. * @ioc: per adapter object
  4197. * @event_data: event data payload
  4198. * Context: user.
  4199. */
  4200. static void
  4201. _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4202. Mpi2EventDataSasTopologyChangeList_t *event_data)
  4203. {
  4204. int i;
  4205. u16 handle;
  4206. u16 reason_code;
  4207. u8 phy_number;
  4208. char *status_str = NULL;
  4209. u8 link_rate, prev_link_rate;
  4210. switch (event_data->ExpStatus) {
  4211. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  4212. status_str = "add";
  4213. break;
  4214. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  4215. status_str = "remove";
  4216. break;
  4217. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  4218. case 0:
  4219. status_str = "responding";
  4220. break;
  4221. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  4222. status_str = "remove delay";
  4223. break;
  4224. default:
  4225. status_str = "unknown status";
  4226. break;
  4227. }
  4228. printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
  4229. ioc->name, status_str);
  4230. printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
  4231. "start_phy(%02d), count(%d)\n",
  4232. le16_to_cpu(event_data->ExpanderDevHandle),
  4233. le16_to_cpu(event_data->EnclosureHandle),
  4234. event_data->StartPhyNum, event_data->NumEntries);
  4235. for (i = 0; i < event_data->NumEntries; i++) {
  4236. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  4237. if (!handle)
  4238. continue;
  4239. phy_number = event_data->StartPhyNum + i;
  4240. reason_code = event_data->PHY[i].PhyStatus &
  4241. MPI2_EVENT_SAS_TOPO_RC_MASK;
  4242. switch (reason_code) {
  4243. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  4244. status_str = "target add";
  4245. break;
  4246. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  4247. status_str = "target remove";
  4248. break;
  4249. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  4250. status_str = "delay target remove";
  4251. break;
  4252. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  4253. status_str = "link rate change";
  4254. break;
  4255. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  4256. status_str = "target responding";
  4257. break;
  4258. default:
  4259. status_str = "unknown";
  4260. break;
  4261. }
  4262. link_rate = event_data->PHY[i].LinkRate >> 4;
  4263. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  4264. printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
  4265. " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
  4266. handle, status_str, link_rate, prev_link_rate);
  4267. }
  4268. }
  4269. #endif
  4270. /**
  4271. * _scsih_sas_topology_change_event - handle topology changes
  4272. * @ioc: per adapter object
  4273. * @fw_event: The fw_event_work object
  4274. * Context: user.
  4275. *
  4276. */
  4277. static void
  4278. _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
  4279. struct fw_event_work *fw_event)
  4280. {
  4281. int i;
  4282. u16 parent_handle, handle;
  4283. u16 reason_code;
  4284. u8 phy_number;
  4285. struct _sas_node *sas_expander;
  4286. struct _sas_device *sas_device;
  4287. u64 sas_address;
  4288. unsigned long flags;
  4289. u8 link_rate, prev_link_rate;
  4290. Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
  4291. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4292. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4293. _scsih_sas_topology_change_event_debug(ioc, event_data);
  4294. #endif
  4295. if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
  4296. return;
  4297. if (!ioc->sas_hba.num_phys)
  4298. _scsih_sas_host_add(ioc);
  4299. else
  4300. _scsih_sas_host_refresh(ioc);
  4301. if (fw_event->ignore) {
  4302. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
  4303. "event\n", ioc->name));
  4304. return;
  4305. }
  4306. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  4307. /* handle expander add */
  4308. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  4309. if (_scsih_expander_add(ioc, parent_handle) != 0)
  4310. return;
  4311. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4312. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  4313. parent_handle);
  4314. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4315. if (sas_expander)
  4316. sas_address = sas_expander->sas_address;
  4317. else if (parent_handle < ioc->sas_hba.num_phys)
  4318. sas_address = ioc->sas_hba.sas_address;
  4319. else
  4320. return;
  4321. /* handle siblings events */
  4322. for (i = 0; i < event_data->NumEntries; i++) {
  4323. if (fw_event->ignore) {
  4324. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
  4325. "expander event\n", ioc->name));
  4326. return;
  4327. }
  4328. if (ioc->shost_recovery || ioc->remove_host ||
  4329. ioc->pci_error_recovery)
  4330. return;
  4331. phy_number = event_data->StartPhyNum + i;
  4332. reason_code = event_data->PHY[i].PhyStatus &
  4333. MPI2_EVENT_SAS_TOPO_RC_MASK;
  4334. if ((event_data->PHY[i].PhyStatus &
  4335. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
  4336. MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
  4337. continue;
  4338. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  4339. if (!handle)
  4340. continue;
  4341. link_rate = event_data->PHY[i].LinkRate >> 4;
  4342. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  4343. switch (reason_code) {
  4344. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  4345. if (link_rate == prev_link_rate)
  4346. break;
  4347. mpt2sas_transport_update_links(ioc, sas_address,
  4348. handle, phy_number, link_rate);
  4349. if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  4350. break;
  4351. _scsih_check_device(ioc, handle);
  4352. break;
  4353. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  4354. mpt2sas_transport_update_links(ioc, sas_address,
  4355. handle, phy_number, link_rate);
  4356. _scsih_add_device(ioc, handle, phy_number, 0);
  4357. break;
  4358. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  4359. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4360. sas_device = _scsih_sas_device_find_by_handle(ioc,
  4361. handle);
  4362. if (!sas_device) {
  4363. spin_unlock_irqrestore(&ioc->sas_device_lock,
  4364. flags);
  4365. break;
  4366. }
  4367. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4368. _scsih_remove_device(ioc, sas_device);
  4369. break;
  4370. }
  4371. }
  4372. /* handle expander removal */
  4373. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
  4374. sas_expander)
  4375. _scsih_expander_remove(ioc, sas_address);
  4376. }
  4377. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4378. /**
  4379. * _scsih_sas_device_status_change_event_debug - debug for device event
  4380. * @event_data: event data payload
  4381. * Context: user.
  4382. *
  4383. * Return nothing.
  4384. */
  4385. static void
  4386. _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4387. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  4388. {
  4389. char *reason_str = NULL;
  4390. switch (event_data->ReasonCode) {
  4391. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  4392. reason_str = "smart data";
  4393. break;
  4394. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  4395. reason_str = "unsupported device discovered";
  4396. break;
  4397. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  4398. reason_str = "internal device reset";
  4399. break;
  4400. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  4401. reason_str = "internal task abort";
  4402. break;
  4403. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  4404. reason_str = "internal task abort set";
  4405. break;
  4406. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  4407. reason_str = "internal clear task set";
  4408. break;
  4409. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  4410. reason_str = "internal query task";
  4411. break;
  4412. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  4413. reason_str = "sata init failure";
  4414. break;
  4415. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  4416. reason_str = "internal device reset complete";
  4417. break;
  4418. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  4419. reason_str = "internal task abort complete";
  4420. break;
  4421. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  4422. reason_str = "internal async notification";
  4423. break;
  4424. case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
  4425. reason_str = "expander reduced functionality";
  4426. break;
  4427. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
  4428. reason_str = "expander reduced functionality complete";
  4429. break;
  4430. default:
  4431. reason_str = "unknown reason";
  4432. break;
  4433. }
  4434. printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
  4435. "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
  4436. reason_str, le16_to_cpu(event_data->DevHandle),
  4437. (unsigned long long)le64_to_cpu(event_data->SASAddress));
  4438. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  4439. printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  4440. event_data->ASC, event_data->ASCQ);
  4441. printk(KERN_INFO "\n");
  4442. }
  4443. #endif
  4444. /**
  4445. * _scsih_sas_device_status_change_event - handle device status change
  4446. * @ioc: per adapter object
  4447. * @fw_event: The fw_event_work object
  4448. * Context: user.
  4449. *
  4450. * Return nothing.
  4451. */
  4452. static void
  4453. _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  4454. struct fw_event_work *fw_event)
  4455. {
  4456. struct MPT2SAS_TARGET *target_priv_data;
  4457. struct _sas_device *sas_device;
  4458. __le64 sas_address;
  4459. unsigned long flags;
  4460. Mpi2EventDataSasDeviceStatusChange_t *event_data =
  4461. fw_event->event_data;
  4462. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4463. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4464. _scsih_sas_device_status_change_event_debug(ioc,
  4465. event_data);
  4466. #endif
  4467. if (event_data->ReasonCode !=
  4468. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  4469. event_data->ReasonCode !=
  4470. MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
  4471. return;
  4472. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4473. sas_address = le64_to_cpu(event_data->SASAddress);
  4474. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4475. sas_address);
  4476. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4477. if (!sas_device || !sas_device->starget)
  4478. return;
  4479. target_priv_data = sas_device->starget->hostdata;
  4480. if (!target_priv_data)
  4481. return;
  4482. if (event_data->ReasonCode ==
  4483. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
  4484. target_priv_data->tm_busy = 1;
  4485. else
  4486. target_priv_data->tm_busy = 0;
  4487. }
  4488. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4489. /**
  4490. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
  4491. * @ioc: per adapter object
  4492. * @event_data: event data payload
  4493. * Context: user.
  4494. *
  4495. * Return nothing.
  4496. */
  4497. static void
  4498. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4499. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  4500. {
  4501. char *reason_str = NULL;
  4502. switch (event_data->ReasonCode) {
  4503. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  4504. reason_str = "enclosure add";
  4505. break;
  4506. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  4507. reason_str = "enclosure remove";
  4508. break;
  4509. default:
  4510. reason_str = "unknown reason";
  4511. break;
  4512. }
  4513. printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
  4514. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  4515. " number slots(%d)\n", ioc->name, reason_str,
  4516. le16_to_cpu(event_data->EnclosureHandle),
  4517. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  4518. le16_to_cpu(event_data->StartSlot));
  4519. }
  4520. #endif
  4521. /**
  4522. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  4523. * @ioc: per adapter object
  4524. * @fw_event: The fw_event_work object
  4525. * Context: user.
  4526. *
  4527. * Return nothing.
  4528. */
  4529. static void
  4530. _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  4531. struct fw_event_work *fw_event)
  4532. {
  4533. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4534. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4535. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  4536. fw_event->event_data);
  4537. #endif
  4538. }
  4539. /**
  4540. * _scsih_sas_broadcast_primative_event - handle broadcast events
  4541. * @ioc: per adapter object
  4542. * @fw_event: The fw_event_work object
  4543. * Context: user.
  4544. *
  4545. * Return nothing.
  4546. */
  4547. static void
  4548. _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
  4549. struct fw_event_work *fw_event)
  4550. {
  4551. struct scsi_cmnd *scmd;
  4552. u16 smid, handle;
  4553. u32 lun;
  4554. struct MPT2SAS_DEVICE *sas_device_priv_data;
  4555. u32 termination_count;
  4556. u32 query_count;
  4557. Mpi2SCSITaskManagementReply_t *mpi_reply;
  4558. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4559. Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
  4560. #endif
  4561. u16 ioc_status;
  4562. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
  4563. "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
  4564. event_data->PortWidth));
  4565. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  4566. __func__));
  4567. termination_count = 0;
  4568. query_count = 0;
  4569. mpi_reply = ioc->tm_cmds.reply;
  4570. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  4571. scmd = _scsih_scsi_lookup_get(ioc, smid);
  4572. if (!scmd)
  4573. continue;
  4574. sas_device_priv_data = scmd->device->hostdata;
  4575. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  4576. continue;
  4577. /* skip hidden raid components */
  4578. if (sas_device_priv_data->sas_target->flags &
  4579. MPT_TARGET_FLAGS_RAID_COMPONENT)
  4580. continue;
  4581. /* skip volumes */
  4582. if (sas_device_priv_data->sas_target->flags &
  4583. MPT_TARGET_FLAGS_VOLUME)
  4584. continue;
  4585. handle = sas_device_priv_data->sas_target->handle;
  4586. lun = sas_device_priv_data->lun;
  4587. query_count++;
  4588. mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
  4589. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
  4590. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  4591. ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
  4592. & MPI2_IOCSTATUS_MASK;
  4593. if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
  4594. (mpi_reply->ResponseCode ==
  4595. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  4596. mpi_reply->ResponseCode ==
  4597. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  4598. continue;
  4599. mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
  4600. MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30, NULL);
  4601. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  4602. }
  4603. ioc->broadcast_aen_busy = 0;
  4604. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
  4605. "%s - exit, query_count = %d termination_count = %d\n",
  4606. ioc->name, __func__, query_count, termination_count));
  4607. }
  4608. /**
  4609. * _scsih_sas_discovery_event - handle discovery events
  4610. * @ioc: per adapter object
  4611. * @fw_event: The fw_event_work object
  4612. * Context: user.
  4613. *
  4614. * Return nothing.
  4615. */
  4616. static void
  4617. _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
  4618. struct fw_event_work *fw_event)
  4619. {
  4620. Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
  4621. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4622. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  4623. printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
  4624. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  4625. "start" : "stop");
  4626. if (event_data->DiscoveryStatus)
  4627. printk("discovery_status(0x%08x)",
  4628. le32_to_cpu(event_data->DiscoveryStatus));
  4629. printk("\n");
  4630. }
  4631. #endif
  4632. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  4633. !ioc->sas_hba.num_phys)
  4634. _scsih_sas_host_add(ioc);
  4635. }
  4636. /**
  4637. * _scsih_reprobe_lun - reprobing lun
  4638. * @sdev: scsi device struct
  4639. * @no_uld_attach: sdev->no_uld_attach flag setting
  4640. *
  4641. **/
  4642. static void
  4643. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  4644. {
  4645. int rc;
  4646. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  4647. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  4648. sdev->no_uld_attach ? "hidding" : "exposing");
  4649. rc = scsi_device_reprobe(sdev);
  4650. }
  4651. /**
  4652. * _scsih_reprobe_target - reprobing target
  4653. * @starget: scsi target struct
  4654. * @no_uld_attach: sdev->no_uld_attach flag setting
  4655. *
  4656. * Note: no_uld_attach flag determines whether the disk device is attached
  4657. * to block layer. A value of `1` means to not attach.
  4658. **/
  4659. static void
  4660. _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
  4661. {
  4662. struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
  4663. if (no_uld_attach)
  4664. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  4665. else
  4666. sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  4667. starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
  4668. _scsih_reprobe_lun);
  4669. }
  4670. /**
  4671. * _scsih_sas_volume_add - add new volume
  4672. * @ioc: per adapter object
  4673. * @element: IR config element data
  4674. * Context: user.
  4675. *
  4676. * Return nothing.
  4677. */
  4678. static void
  4679. _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
  4680. Mpi2EventIrConfigElement_t *element)
  4681. {
  4682. struct _raid_device *raid_device;
  4683. unsigned long flags;
  4684. u64 wwid;
  4685. u16 handle = le16_to_cpu(element->VolDevHandle);
  4686. int rc;
  4687. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  4688. if (!wwid) {
  4689. printk(MPT2SAS_ERR_FMT
  4690. "failure at %s:%d/%s()!\n", ioc->name,
  4691. __FILE__, __LINE__, __func__);
  4692. return;
  4693. }
  4694. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4695. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  4696. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4697. if (raid_device)
  4698. return;
  4699. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  4700. if (!raid_device) {
  4701. printk(MPT2SAS_ERR_FMT
  4702. "failure at %s:%d/%s()!\n", ioc->name,
  4703. __FILE__, __LINE__, __func__);
  4704. return;
  4705. }
  4706. raid_device->id = ioc->sas_id++;
  4707. raid_device->channel = RAID_CHANNEL;
  4708. raid_device->handle = handle;
  4709. raid_device->wwid = wwid;
  4710. _scsih_raid_device_add(ioc, raid_device);
  4711. if (!ioc->wait_for_port_enable_to_complete) {
  4712. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4713. raid_device->id, 0);
  4714. if (rc)
  4715. _scsih_raid_device_remove(ioc, raid_device);
  4716. } else
  4717. _scsih_determine_boot_device(ioc, raid_device, 1);
  4718. }
  4719. /**
  4720. * _scsih_sas_volume_delete - delete volume
  4721. * @ioc: per adapter object
  4722. * @handle: volume device handle
  4723. * Context: user.
  4724. *
  4725. * Return nothing.
  4726. */
  4727. static void
  4728. _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  4729. {
  4730. struct _raid_device *raid_device;
  4731. unsigned long flags;
  4732. struct MPT2SAS_TARGET *sas_target_priv_data;
  4733. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4734. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  4735. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4736. if (!raid_device)
  4737. return;
  4738. if (raid_device->starget) {
  4739. sas_target_priv_data = raid_device->starget->hostdata;
  4740. sas_target_priv_data->deleted = 1;
  4741. scsi_remove_target(&raid_device->starget->dev);
  4742. }
  4743. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
  4744. "(0x%016llx)\n", ioc->name, raid_device->handle,
  4745. (unsigned long long) raid_device->wwid);
  4746. _scsih_raid_device_remove(ioc, raid_device);
  4747. }
  4748. /**
  4749. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  4750. * @ioc: per adapter object
  4751. * @element: IR config element data
  4752. * Context: user.
  4753. *
  4754. * Return nothing.
  4755. */
  4756. static void
  4757. _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
  4758. Mpi2EventIrConfigElement_t *element)
  4759. {
  4760. struct _sas_device *sas_device;
  4761. unsigned long flags;
  4762. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4763. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4764. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4765. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4766. if (!sas_device)
  4767. return;
  4768. /* exposing raid component */
  4769. sas_device->volume_handle = 0;
  4770. sas_device->volume_wwid = 0;
  4771. clear_bit(handle, ioc->pd_handles);
  4772. _scsih_reprobe_target(sas_device->starget, 0);
  4773. }
  4774. /**
  4775. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  4776. * @ioc: per adapter object
  4777. * @element: IR config element data
  4778. * Context: user.
  4779. *
  4780. * Return nothing.
  4781. */
  4782. static void
  4783. _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
  4784. Mpi2EventIrConfigElement_t *element)
  4785. {
  4786. struct _sas_device *sas_device;
  4787. unsigned long flags;
  4788. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4789. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4790. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4791. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4792. if (!sas_device)
  4793. return;
  4794. /* hiding raid component */
  4795. mpt2sas_config_get_volume_handle(ioc, handle,
  4796. &sas_device->volume_handle);
  4797. mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
  4798. &sas_device->volume_wwid);
  4799. set_bit(handle, ioc->pd_handles);
  4800. _scsih_reprobe_target(sas_device->starget, 1);
  4801. }
  4802. /**
  4803. * _scsih_sas_pd_delete - delete pd component
  4804. * @ioc: per adapter object
  4805. * @element: IR config element data
  4806. * Context: user.
  4807. *
  4808. * Return nothing.
  4809. */
  4810. static void
  4811. _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
  4812. Mpi2EventIrConfigElement_t *element)
  4813. {
  4814. struct _sas_device *sas_device;
  4815. unsigned long flags;
  4816. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4817. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4818. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4819. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4820. if (!sas_device)
  4821. return;
  4822. _scsih_remove_device(ioc, sas_device);
  4823. }
  4824. /**
  4825. * _scsih_sas_pd_add - remove pd component
  4826. * @ioc: per adapter object
  4827. * @element: IR config element data
  4828. * Context: user.
  4829. *
  4830. * Return nothing.
  4831. */
  4832. static void
  4833. _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
  4834. Mpi2EventIrConfigElement_t *element)
  4835. {
  4836. struct _sas_device *sas_device;
  4837. unsigned long flags;
  4838. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4839. Mpi2ConfigReply_t mpi_reply;
  4840. Mpi2SasDevicePage0_t sas_device_pg0;
  4841. u32 ioc_status;
  4842. u64 sas_address;
  4843. u16 parent_handle;
  4844. set_bit(handle, ioc->pd_handles);
  4845. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4846. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4847. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4848. if (sas_device)
  4849. return;
  4850. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4851. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  4852. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4853. ioc->name, __FILE__, __LINE__, __func__);
  4854. return;
  4855. }
  4856. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4857. MPI2_IOCSTATUS_MASK;
  4858. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4859. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4860. ioc->name, __FILE__, __LINE__, __func__);
  4861. return;
  4862. }
  4863. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  4864. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  4865. mpt2sas_transport_update_links(ioc, sas_address, handle,
  4866. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  4867. _scsih_add_device(ioc, handle, 0, 1);
  4868. }
  4869. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4870. /**
  4871. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  4872. * @ioc: per adapter object
  4873. * @event_data: event data payload
  4874. * Context: user.
  4875. *
  4876. * Return nothing.
  4877. */
  4878. static void
  4879. _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4880. Mpi2EventDataIrConfigChangeList_t *event_data)
  4881. {
  4882. Mpi2EventIrConfigElement_t *element;
  4883. u8 element_type;
  4884. int i;
  4885. char *reason_str = NULL, *element_str = NULL;
  4886. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4887. printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
  4888. ioc->name, (le32_to_cpu(event_data->Flags) &
  4889. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  4890. "foreign" : "native", event_data->NumElements);
  4891. for (i = 0; i < event_data->NumElements; i++, element++) {
  4892. switch (element->ReasonCode) {
  4893. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4894. reason_str = "add";
  4895. break;
  4896. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4897. reason_str = "remove";
  4898. break;
  4899. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  4900. reason_str = "no change";
  4901. break;
  4902. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4903. reason_str = "hide";
  4904. break;
  4905. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4906. reason_str = "unhide";
  4907. break;
  4908. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4909. reason_str = "volume_created";
  4910. break;
  4911. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4912. reason_str = "volume_deleted";
  4913. break;
  4914. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4915. reason_str = "pd_created";
  4916. break;
  4917. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4918. reason_str = "pd_deleted";
  4919. break;
  4920. default:
  4921. reason_str = "unknown reason";
  4922. break;
  4923. }
  4924. element_type = le16_to_cpu(element->ElementFlags) &
  4925. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  4926. switch (element_type) {
  4927. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  4928. element_str = "volume";
  4929. break;
  4930. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  4931. element_str = "phys disk";
  4932. break;
  4933. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  4934. element_str = "hot spare";
  4935. break;
  4936. default:
  4937. element_str = "unknown element";
  4938. break;
  4939. }
  4940. printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
  4941. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  4942. reason_str, le16_to_cpu(element->VolDevHandle),
  4943. le16_to_cpu(element->PhysDiskDevHandle),
  4944. element->PhysDiskNum);
  4945. }
  4946. }
  4947. #endif
  4948. /**
  4949. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  4950. * @ioc: per adapter object
  4951. * @fw_event: The fw_event_work object
  4952. * Context: user.
  4953. *
  4954. * Return nothing.
  4955. */
  4956. static void
  4957. _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
  4958. struct fw_event_work *fw_event)
  4959. {
  4960. Mpi2EventIrConfigElement_t *element;
  4961. int i;
  4962. u8 foreign_config;
  4963. Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
  4964. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4965. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4966. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  4967. #endif
  4968. foreign_config = (le32_to_cpu(event_data->Flags) &
  4969. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
  4970. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4971. for (i = 0; i < event_data->NumElements; i++, element++) {
  4972. switch (element->ReasonCode) {
  4973. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4974. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4975. if (!foreign_config)
  4976. _scsih_sas_volume_add(ioc, element);
  4977. break;
  4978. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4979. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4980. if (!foreign_config)
  4981. _scsih_sas_volume_delete(ioc,
  4982. le16_to_cpu(element->VolDevHandle));
  4983. break;
  4984. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4985. _scsih_sas_pd_hide(ioc, element);
  4986. break;
  4987. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4988. _scsih_sas_pd_expose(ioc, element);
  4989. break;
  4990. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4991. _scsih_sas_pd_add(ioc, element);
  4992. break;
  4993. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4994. _scsih_sas_pd_delete(ioc, element);
  4995. break;
  4996. }
  4997. }
  4998. }
  4999. /**
  5000. * _scsih_sas_ir_volume_event - IR volume event
  5001. * @ioc: per adapter object
  5002. * @fw_event: The fw_event_work object
  5003. * Context: user.
  5004. *
  5005. * Return nothing.
  5006. */
  5007. static void
  5008. _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
  5009. struct fw_event_work *fw_event)
  5010. {
  5011. u64 wwid;
  5012. unsigned long flags;
  5013. struct _raid_device *raid_device;
  5014. u16 handle;
  5015. u32 state;
  5016. int rc;
  5017. Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
  5018. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  5019. return;
  5020. handle = le16_to_cpu(event_data->VolDevHandle);
  5021. state = le32_to_cpu(event_data->NewValue);
  5022. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
  5023. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  5024. le32_to_cpu(event_data->PreviousValue), state));
  5025. switch (state) {
  5026. case MPI2_RAID_VOL_STATE_MISSING:
  5027. case MPI2_RAID_VOL_STATE_FAILED:
  5028. _scsih_sas_volume_delete(ioc, handle);
  5029. break;
  5030. case MPI2_RAID_VOL_STATE_ONLINE:
  5031. case MPI2_RAID_VOL_STATE_DEGRADED:
  5032. case MPI2_RAID_VOL_STATE_OPTIMAL:
  5033. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5034. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  5035. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5036. if (raid_device)
  5037. break;
  5038. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  5039. if (!wwid) {
  5040. printk(MPT2SAS_ERR_FMT
  5041. "failure at %s:%d/%s()!\n", ioc->name,
  5042. __FILE__, __LINE__, __func__);
  5043. break;
  5044. }
  5045. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  5046. if (!raid_device) {
  5047. printk(MPT2SAS_ERR_FMT
  5048. "failure at %s:%d/%s()!\n", ioc->name,
  5049. __FILE__, __LINE__, __func__);
  5050. break;
  5051. }
  5052. raid_device->id = ioc->sas_id++;
  5053. raid_device->channel = RAID_CHANNEL;
  5054. raid_device->handle = handle;
  5055. raid_device->wwid = wwid;
  5056. _scsih_raid_device_add(ioc, raid_device);
  5057. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5058. raid_device->id, 0);
  5059. if (rc)
  5060. _scsih_raid_device_remove(ioc, raid_device);
  5061. break;
  5062. case MPI2_RAID_VOL_STATE_INITIALIZING:
  5063. default:
  5064. break;
  5065. }
  5066. }
  5067. /**
  5068. * _scsih_sas_ir_physical_disk_event - PD event
  5069. * @ioc: per adapter object
  5070. * @fw_event: The fw_event_work object
  5071. * Context: user.
  5072. *
  5073. * Return nothing.
  5074. */
  5075. static void
  5076. _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
  5077. struct fw_event_work *fw_event)
  5078. {
  5079. u16 handle, parent_handle;
  5080. u32 state;
  5081. struct _sas_device *sas_device;
  5082. unsigned long flags;
  5083. Mpi2ConfigReply_t mpi_reply;
  5084. Mpi2SasDevicePage0_t sas_device_pg0;
  5085. u32 ioc_status;
  5086. Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
  5087. u64 sas_address;
  5088. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  5089. return;
  5090. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  5091. state = le32_to_cpu(event_data->NewValue);
  5092. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
  5093. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  5094. le32_to_cpu(event_data->PreviousValue), state));
  5095. switch (state) {
  5096. case MPI2_RAID_PD_STATE_ONLINE:
  5097. case MPI2_RAID_PD_STATE_DEGRADED:
  5098. case MPI2_RAID_PD_STATE_REBUILDING:
  5099. case MPI2_RAID_PD_STATE_OPTIMAL:
  5100. case MPI2_RAID_PD_STATE_HOT_SPARE:
  5101. set_bit(handle, ioc->pd_handles);
  5102. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5103. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  5104. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5105. if (sas_device)
  5106. return;
  5107. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  5108. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  5109. handle))) {
  5110. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5111. ioc->name, __FILE__, __LINE__, __func__);
  5112. return;
  5113. }
  5114. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5115. MPI2_IOCSTATUS_MASK;
  5116. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5117. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5118. ioc->name, __FILE__, __LINE__, __func__);
  5119. return;
  5120. }
  5121. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  5122. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  5123. mpt2sas_transport_update_links(ioc, sas_address, handle,
  5124. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  5125. _scsih_add_device(ioc, handle, 0, 1);
  5126. break;
  5127. case MPI2_RAID_PD_STATE_OFFLINE:
  5128. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  5129. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  5130. default:
  5131. break;
  5132. }
  5133. }
  5134. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5135. /**
  5136. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  5137. * @ioc: per adapter object
  5138. * @event_data: event data payload
  5139. * Context: user.
  5140. *
  5141. * Return nothing.
  5142. */
  5143. static void
  5144. _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
  5145. Mpi2EventDataIrOperationStatus_t *event_data)
  5146. {
  5147. char *reason_str = NULL;
  5148. switch (event_data->RAIDOperation) {
  5149. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  5150. reason_str = "resync";
  5151. break;
  5152. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  5153. reason_str = "online capacity expansion";
  5154. break;
  5155. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  5156. reason_str = "consistency check";
  5157. break;
  5158. case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
  5159. reason_str = "background init";
  5160. break;
  5161. case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
  5162. reason_str = "make data consistent";
  5163. break;
  5164. }
  5165. if (!reason_str)
  5166. return;
  5167. printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
  5168. "\thandle(0x%04x), percent complete(%d)\n",
  5169. ioc->name, reason_str,
  5170. le16_to_cpu(event_data->VolDevHandle),
  5171. event_data->PercentComplete);
  5172. }
  5173. #endif
  5174. /**
  5175. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  5176. * @ioc: per adapter object
  5177. * @fw_event: The fw_event_work object
  5178. * Context: user.
  5179. *
  5180. * Return nothing.
  5181. */
  5182. static void
  5183. _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
  5184. struct fw_event_work *fw_event)
  5185. {
  5186. Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
  5187. static struct _raid_device *raid_device;
  5188. unsigned long flags;
  5189. u16 handle;
  5190. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  5191. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5192. _scsih_sas_ir_operation_status_event_debug(ioc,
  5193. event_data);
  5194. #endif
  5195. /* code added for raid transport support */
  5196. if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
  5197. handle = le16_to_cpu(event_data->VolDevHandle);
  5198. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5199. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  5200. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5201. if (!raid_device)
  5202. return;
  5203. if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
  5204. raid_device->percent_complete =
  5205. event_data->PercentComplete;
  5206. }
  5207. }
  5208. /**
  5209. * _scsih_task_set_full - handle task set full
  5210. * @ioc: per adapter object
  5211. * @fw_event: The fw_event_work object
  5212. * Context: user.
  5213. *
  5214. * Throttle back qdepth.
  5215. */
  5216. static void
  5217. _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  5218. *fw_event)
  5219. {
  5220. unsigned long flags;
  5221. struct _sas_device *sas_device;
  5222. static struct _raid_device *raid_device;
  5223. struct scsi_device *sdev;
  5224. int depth;
  5225. u16 current_depth;
  5226. u16 handle;
  5227. int id, channel;
  5228. u64 sas_address;
  5229. Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
  5230. current_depth = le16_to_cpu(event_data->CurrentDepth);
  5231. handle = le16_to_cpu(event_data->DevHandle);
  5232. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5233. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  5234. if (!sas_device) {
  5235. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5236. return;
  5237. }
  5238. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5239. id = sas_device->id;
  5240. channel = sas_device->channel;
  5241. sas_address = sas_device->sas_address;
  5242. /* if hidden raid component, then change to volume characteristics */
  5243. if (test_bit(handle, ioc->pd_handles) && sas_device->volume_handle) {
  5244. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5245. raid_device = _scsih_raid_device_find_by_handle(
  5246. ioc, sas_device->volume_handle);
  5247. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5248. if (raid_device) {
  5249. id = raid_device->id;
  5250. channel = raid_device->channel;
  5251. handle = raid_device->handle;
  5252. sas_address = raid_device->wwid;
  5253. }
  5254. }
  5255. if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
  5256. starget_printk(KERN_INFO, sas_device->starget, "task set "
  5257. "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
  5258. handle, (unsigned long long)sas_address, current_depth);
  5259. shost_for_each_device(sdev, ioc->shost) {
  5260. if (sdev->id == id && sdev->channel == channel) {
  5261. if (current_depth > sdev->queue_depth) {
  5262. if (ioc->logging_level &
  5263. MPT_DEBUG_TASK_SET_FULL)
  5264. sdev_printk(KERN_INFO, sdev, "strange "
  5265. "observation, the queue depth is"
  5266. " (%d) meanwhile fw queue depth "
  5267. "is (%d)\n", sdev->queue_depth,
  5268. current_depth);
  5269. continue;
  5270. }
  5271. depth = scsi_track_queue_full(sdev,
  5272. current_depth - 1);
  5273. if (depth > 0)
  5274. sdev_printk(KERN_INFO, sdev, "Queue depth "
  5275. "reduced to (%d)\n", depth);
  5276. else if (depth < 0)
  5277. sdev_printk(KERN_INFO, sdev, "Tagged Command "
  5278. "Queueing is being disabled\n");
  5279. else if (depth == 0)
  5280. if (ioc->logging_level &
  5281. MPT_DEBUG_TASK_SET_FULL)
  5282. sdev_printk(KERN_INFO, sdev,
  5283. "Queue depth not changed yet\n");
  5284. }
  5285. }
  5286. }
  5287. /**
  5288. * _scsih_prep_device_scan - initialize parameters prior to device scan
  5289. * @ioc: per adapter object
  5290. *
  5291. * Set the deleted flag prior to device scan. If the device is found during
  5292. * the scan, then we clear the deleted flag.
  5293. */
  5294. static void
  5295. _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
  5296. {
  5297. struct MPT2SAS_DEVICE *sas_device_priv_data;
  5298. struct scsi_device *sdev;
  5299. shost_for_each_device(sdev, ioc->shost) {
  5300. sas_device_priv_data = sdev->hostdata;
  5301. if (sas_device_priv_data && sas_device_priv_data->sas_target)
  5302. sas_device_priv_data->sas_target->deleted = 1;
  5303. }
  5304. }
  5305. /**
  5306. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  5307. * @ioc: per adapter object
  5308. * @sas_address: sas address
  5309. * @slot: enclosure slot id
  5310. * @handle: device handle
  5311. *
  5312. * After host reset, find out whether devices are still responding.
  5313. * Used in _scsi_remove_unresponsive_sas_devices.
  5314. *
  5315. * Return nothing.
  5316. */
  5317. static void
  5318. _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  5319. u16 slot, u16 handle)
  5320. {
  5321. struct MPT2SAS_TARGET *sas_target_priv_data;
  5322. struct scsi_target *starget;
  5323. struct _sas_device *sas_device;
  5324. unsigned long flags;
  5325. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5326. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  5327. if (sas_device->sas_address == sas_address &&
  5328. sas_device->slot == slot && sas_device->starget) {
  5329. sas_device->responding = 1;
  5330. starget = sas_device->starget;
  5331. if (starget && starget->hostdata) {
  5332. sas_target_priv_data = starget->hostdata;
  5333. sas_target_priv_data->tm_busy = 0;
  5334. sas_target_priv_data->deleted = 0;
  5335. } else
  5336. sas_target_priv_data = NULL;
  5337. starget_printk(KERN_INFO, sas_device->starget,
  5338. "handle(0x%04x), sas_addr(0x%016llx), enclosure "
  5339. "logical id(0x%016llx), slot(%d)\n", handle,
  5340. (unsigned long long)sas_device->sas_address,
  5341. (unsigned long long)
  5342. sas_device->enclosure_logical_id,
  5343. sas_device->slot);
  5344. if (sas_device->handle == handle)
  5345. goto out;
  5346. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  5347. sas_device->handle);
  5348. sas_device->handle = handle;
  5349. if (sas_target_priv_data)
  5350. sas_target_priv_data->handle = handle;
  5351. goto out;
  5352. }
  5353. }
  5354. out:
  5355. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5356. }
  5357. /**
  5358. * _scsih_search_responding_sas_devices -
  5359. * @ioc: per adapter object
  5360. *
  5361. * After host reset, find out whether devices are still responding.
  5362. * If not remove.
  5363. *
  5364. * Return nothing.
  5365. */
  5366. static void
  5367. _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  5368. {
  5369. Mpi2SasDevicePage0_t sas_device_pg0;
  5370. Mpi2ConfigReply_t mpi_reply;
  5371. u16 ioc_status;
  5372. __le64 sas_address;
  5373. u16 handle;
  5374. u32 device_info;
  5375. u16 slot;
  5376. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  5377. if (list_empty(&ioc->sas_device_list))
  5378. return;
  5379. handle = 0xFFFF;
  5380. while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  5381. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  5382. handle))) {
  5383. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5384. MPI2_IOCSTATUS_MASK;
  5385. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  5386. break;
  5387. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  5388. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  5389. if (!(_scsih_is_end_device(device_info)))
  5390. continue;
  5391. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  5392. slot = le16_to_cpu(sas_device_pg0.Slot);
  5393. _scsih_mark_responding_sas_device(ioc, sas_address, slot,
  5394. handle);
  5395. }
  5396. }
  5397. /**
  5398. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  5399. * @ioc: per adapter object
  5400. * @wwid: world wide identifier for raid volume
  5401. * @handle: device handle
  5402. *
  5403. * After host reset, find out whether devices are still responding.
  5404. * Used in _scsi_remove_unresponsive_raid_devices.
  5405. *
  5406. * Return nothing.
  5407. */
  5408. static void
  5409. _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
  5410. u16 handle)
  5411. {
  5412. struct MPT2SAS_TARGET *sas_target_priv_data;
  5413. struct scsi_target *starget;
  5414. struct _raid_device *raid_device;
  5415. unsigned long flags;
  5416. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5417. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  5418. if (raid_device->wwid == wwid && raid_device->starget) {
  5419. starget = raid_device->starget;
  5420. if (starget && starget->hostdata) {
  5421. sas_target_priv_data = starget->hostdata;
  5422. sas_target_priv_data->deleted = 0;
  5423. } else
  5424. sas_target_priv_data = NULL;
  5425. raid_device->responding = 1;
  5426. starget_printk(KERN_INFO, raid_device->starget,
  5427. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  5428. (unsigned long long)raid_device->wwid);
  5429. if (raid_device->handle == handle)
  5430. goto out;
  5431. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  5432. raid_device->handle);
  5433. raid_device->handle = handle;
  5434. if (sas_target_priv_data)
  5435. sas_target_priv_data->handle = handle;
  5436. goto out;
  5437. }
  5438. }
  5439. out:
  5440. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5441. }
  5442. /**
  5443. * _scsih_search_responding_raid_devices -
  5444. * @ioc: per adapter object
  5445. *
  5446. * After host reset, find out whether devices are still responding.
  5447. * If not remove.
  5448. *
  5449. * Return nothing.
  5450. */
  5451. static void
  5452. _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
  5453. {
  5454. Mpi2RaidVolPage1_t volume_pg1;
  5455. Mpi2RaidVolPage0_t volume_pg0;
  5456. Mpi2RaidPhysDiskPage0_t pd_pg0;
  5457. Mpi2ConfigReply_t mpi_reply;
  5458. u16 ioc_status;
  5459. u16 handle;
  5460. u8 phys_disk_num;
  5461. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  5462. if (list_empty(&ioc->raid_device_list))
  5463. return;
  5464. handle = 0xFFFF;
  5465. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  5466. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  5467. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5468. MPI2_IOCSTATUS_MASK;
  5469. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  5470. break;
  5471. handle = le16_to_cpu(volume_pg1.DevHandle);
  5472. if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
  5473. &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  5474. sizeof(Mpi2RaidVolPage0_t)))
  5475. continue;
  5476. if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
  5477. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
  5478. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
  5479. _scsih_mark_responding_raid_device(ioc,
  5480. le64_to_cpu(volume_pg1.WWID), handle);
  5481. }
  5482. /* refresh the pd_handles */
  5483. phys_disk_num = 0xFF;
  5484. memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
  5485. while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  5486. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
  5487. phys_disk_num))) {
  5488. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5489. MPI2_IOCSTATUS_MASK;
  5490. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  5491. break;
  5492. phys_disk_num = pd_pg0.PhysDiskNum;
  5493. handle = le16_to_cpu(pd_pg0.DevHandle);
  5494. set_bit(handle, ioc->pd_handles);
  5495. }
  5496. }
  5497. /**
  5498. * _scsih_mark_responding_expander - mark a expander as responding
  5499. * @ioc: per adapter object
  5500. * @sas_address: sas address
  5501. * @handle:
  5502. *
  5503. * After host reset, find out whether devices are still responding.
  5504. * Used in _scsi_remove_unresponsive_expanders.
  5505. *
  5506. * Return nothing.
  5507. */
  5508. static void
  5509. _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  5510. u16 handle)
  5511. {
  5512. struct _sas_node *sas_expander;
  5513. unsigned long flags;
  5514. int i;
  5515. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5516. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  5517. if (sas_expander->sas_address != sas_address)
  5518. continue;
  5519. sas_expander->responding = 1;
  5520. if (sas_expander->handle == handle)
  5521. goto out;
  5522. printk(KERN_INFO "\texpander(0x%016llx): handle changed"
  5523. " from(0x%04x) to (0x%04x)!!!\n",
  5524. (unsigned long long)sas_expander->sas_address,
  5525. sas_expander->handle, handle);
  5526. sas_expander->handle = handle;
  5527. for (i = 0 ; i < sas_expander->num_phys ; i++)
  5528. sas_expander->phy[i].handle = handle;
  5529. goto out;
  5530. }
  5531. out:
  5532. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5533. }
  5534. /**
  5535. * _scsih_search_responding_expanders -
  5536. * @ioc: per adapter object
  5537. *
  5538. * After host reset, find out whether devices are still responding.
  5539. * If not remove.
  5540. *
  5541. * Return nothing.
  5542. */
  5543. static void
  5544. _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
  5545. {
  5546. Mpi2ExpanderPage0_t expander_pg0;
  5547. Mpi2ConfigReply_t mpi_reply;
  5548. u16 ioc_status;
  5549. __le64 sas_address;
  5550. u16 handle;
  5551. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  5552. if (list_empty(&ioc->sas_expander_list))
  5553. return;
  5554. handle = 0xFFFF;
  5555. while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  5556. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  5557. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5558. MPI2_IOCSTATUS_MASK;
  5559. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  5560. break;
  5561. handle = le16_to_cpu(expander_pg0.DevHandle);
  5562. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  5563. printk(KERN_INFO "\texpander present: handle(0x%04x), "
  5564. "sas_addr(0x%016llx)\n", handle,
  5565. (unsigned long long)sas_address);
  5566. _scsih_mark_responding_expander(ioc, sas_address, handle);
  5567. }
  5568. }
  5569. /**
  5570. * _scsih_remove_unresponding_sas_devices - removing unresponding devices
  5571. * @ioc: per adapter object
  5572. *
  5573. * Return nothing.
  5574. */
  5575. static void
  5576. _scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  5577. {
  5578. struct _sas_device *sas_device, *sas_device_next;
  5579. struct _sas_node *sas_expander;
  5580. struct _raid_device *raid_device, *raid_device_next;
  5581. list_for_each_entry_safe(sas_device, sas_device_next,
  5582. &ioc->sas_device_list, list) {
  5583. if (sas_device->responding) {
  5584. sas_device->responding = 0;
  5585. continue;
  5586. }
  5587. if (sas_device->starget)
  5588. starget_printk(KERN_INFO, sas_device->starget,
  5589. "removing: handle(0x%04x), sas_addr(0x%016llx), "
  5590. "enclosure logical id(0x%016llx), slot(%d)\n",
  5591. sas_device->handle,
  5592. (unsigned long long)sas_device->sas_address,
  5593. (unsigned long long)
  5594. sas_device->enclosure_logical_id,
  5595. sas_device->slot);
  5596. _scsih_remove_device(ioc, sas_device);
  5597. }
  5598. list_for_each_entry_safe(raid_device, raid_device_next,
  5599. &ioc->raid_device_list, list) {
  5600. if (raid_device->responding) {
  5601. raid_device->responding = 0;
  5602. continue;
  5603. }
  5604. if (raid_device->starget) {
  5605. starget_printk(KERN_INFO, raid_device->starget,
  5606. "removing: handle(0x%04x), wwid(0x%016llx)\n",
  5607. raid_device->handle,
  5608. (unsigned long long)raid_device->wwid);
  5609. scsi_remove_target(&raid_device->starget->dev);
  5610. }
  5611. _scsih_raid_device_remove(ioc, raid_device);
  5612. }
  5613. retry_expander_search:
  5614. sas_expander = NULL;
  5615. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  5616. if (sas_expander->responding) {
  5617. sas_expander->responding = 0;
  5618. continue;
  5619. }
  5620. _scsih_expander_remove(ioc, sas_expander->sas_address);
  5621. goto retry_expander_search;
  5622. }
  5623. }
  5624. /**
  5625. * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
  5626. * @ioc: per adapter object
  5627. * @reset_phase: phase
  5628. *
  5629. * The handler for doing any required cleanup or initialization.
  5630. *
  5631. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  5632. * MPT2_IOC_DONE_RESET
  5633. *
  5634. * Return nothing.
  5635. */
  5636. void
  5637. mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  5638. {
  5639. switch (reset_phase) {
  5640. case MPT2_IOC_PRE_RESET:
  5641. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  5642. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  5643. break;
  5644. case MPT2_IOC_AFTER_RESET:
  5645. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  5646. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  5647. if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
  5648. ioc->scsih_cmds.status |= MPT2_CMD_RESET;
  5649. mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
  5650. complete(&ioc->scsih_cmds.done);
  5651. }
  5652. if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
  5653. ioc->tm_cmds.status |= MPT2_CMD_RESET;
  5654. mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  5655. complete(&ioc->tm_cmds.done);
  5656. }
  5657. _scsih_fw_event_cleanup_queue(ioc);
  5658. _scsih_flush_running_cmds(ioc);
  5659. _scsih_queue_rescan(ioc);
  5660. break;
  5661. case MPT2_IOC_DONE_RESET:
  5662. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  5663. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  5664. _scsih_sas_host_refresh(ioc);
  5665. _scsih_prep_device_scan(ioc);
  5666. _scsih_search_responding_sas_devices(ioc);
  5667. _scsih_search_responding_raid_devices(ioc);
  5668. _scsih_search_responding_expanders(ioc);
  5669. break;
  5670. }
  5671. }
  5672. /**
  5673. * _firmware_event_work - delayed task for processing firmware events
  5674. * @ioc: per adapter object
  5675. * @work: equal to the fw_event_work object
  5676. * Context: user.
  5677. *
  5678. * Return nothing.
  5679. */
  5680. static void
  5681. _firmware_event_work(struct work_struct *work)
  5682. {
  5683. struct fw_event_work *fw_event = container_of(work,
  5684. struct fw_event_work, delayed_work.work);
  5685. unsigned long flags;
  5686. struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
  5687. /* the queue is being flushed so ignore this event */
  5688. if (ioc->remove_host || fw_event->cancel_pending_work ||
  5689. ioc->pci_error_recovery) {
  5690. _scsih_fw_event_free(ioc, fw_event);
  5691. return;
  5692. }
  5693. if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
  5694. _scsih_fw_event_free(ioc, fw_event);
  5695. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  5696. if (ioc->shost_recovery) {
  5697. init_completion(&ioc->shost_recovery_done);
  5698. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
  5699. flags);
  5700. wait_for_completion(&ioc->shost_recovery_done);
  5701. } else
  5702. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
  5703. flags);
  5704. _scsih_remove_unresponding_sas_devices(ioc);
  5705. return;
  5706. }
  5707. switch (fw_event->event) {
  5708. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  5709. _scsih_sas_topology_change_event(ioc, fw_event);
  5710. break;
  5711. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  5712. _scsih_sas_device_status_change_event(ioc,
  5713. fw_event);
  5714. break;
  5715. case MPI2_EVENT_SAS_DISCOVERY:
  5716. _scsih_sas_discovery_event(ioc,
  5717. fw_event);
  5718. break;
  5719. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  5720. _scsih_sas_broadcast_primative_event(ioc,
  5721. fw_event);
  5722. break;
  5723. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  5724. _scsih_sas_enclosure_dev_status_change_event(ioc,
  5725. fw_event);
  5726. break;
  5727. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  5728. _scsih_sas_ir_config_change_event(ioc, fw_event);
  5729. break;
  5730. case MPI2_EVENT_IR_VOLUME:
  5731. _scsih_sas_ir_volume_event(ioc, fw_event);
  5732. break;
  5733. case MPI2_EVENT_IR_PHYSICAL_DISK:
  5734. _scsih_sas_ir_physical_disk_event(ioc, fw_event);
  5735. break;
  5736. case MPI2_EVENT_IR_OPERATION_STATUS:
  5737. _scsih_sas_ir_operation_status_event(ioc, fw_event);
  5738. break;
  5739. case MPI2_EVENT_TASK_SET_FULL:
  5740. _scsih_task_set_full(ioc, fw_event);
  5741. break;
  5742. }
  5743. _scsih_fw_event_free(ioc, fw_event);
  5744. }
  5745. /**
  5746. * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
  5747. * @ioc: per adapter object
  5748. * @msix_index: MSIX table index supplied by the OS
  5749. * @reply: reply message frame(lower 32bit addr)
  5750. * Context: interrupt.
  5751. *
  5752. * This function merely adds a new work task into ioc->firmware_event_thread.
  5753. * The tasks are worked from _firmware_event_work in user context.
  5754. *
  5755. * Return 1 meaning mf should be freed from _base_interrupt
  5756. * 0 means the mf is freed from this function.
  5757. */
  5758. u8
  5759. mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
  5760. u32 reply)
  5761. {
  5762. struct fw_event_work *fw_event;
  5763. Mpi2EventNotificationReply_t *mpi_reply;
  5764. u16 event;
  5765. u16 sz;
  5766. /* events turned off due to host reset or driver unloading */
  5767. if (ioc->remove_host || ioc->pci_error_recovery)
  5768. return 1;
  5769. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  5770. event = le16_to_cpu(mpi_reply->Event);
  5771. switch (event) {
  5772. /* handle these */
  5773. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  5774. {
  5775. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  5776. (Mpi2EventDataSasBroadcastPrimitive_t *)
  5777. mpi_reply->EventData;
  5778. if (baen_data->Primitive !=
  5779. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
  5780. ioc->broadcast_aen_busy)
  5781. return 1;
  5782. ioc->broadcast_aen_busy = 1;
  5783. break;
  5784. }
  5785. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  5786. _scsih_check_topo_delete_events(ioc,
  5787. (Mpi2EventDataSasTopologyChangeList_t *)
  5788. mpi_reply->EventData);
  5789. break;
  5790. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  5791. _scsih_check_ir_config_unhide_events(ioc,
  5792. (Mpi2EventDataIrConfigChangeList_t *)
  5793. mpi_reply->EventData);
  5794. break;
  5795. case MPI2_EVENT_IR_VOLUME:
  5796. _scsih_check_volume_delete_events(ioc,
  5797. (Mpi2EventDataIrVolume_t *)
  5798. mpi_reply->EventData);
  5799. break;
  5800. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  5801. case MPI2_EVENT_IR_OPERATION_STATUS:
  5802. case MPI2_EVENT_SAS_DISCOVERY:
  5803. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  5804. case MPI2_EVENT_IR_PHYSICAL_DISK:
  5805. case MPI2_EVENT_TASK_SET_FULL:
  5806. break;
  5807. default: /* ignore the rest */
  5808. return 1;
  5809. }
  5810. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  5811. if (!fw_event) {
  5812. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5813. ioc->name, __FILE__, __LINE__, __func__);
  5814. return 1;
  5815. }
  5816. sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
  5817. fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
  5818. if (!fw_event->event_data) {
  5819. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5820. ioc->name, __FILE__, __LINE__, __func__);
  5821. kfree(fw_event);
  5822. return 1;
  5823. }
  5824. memcpy(fw_event->event_data, mpi_reply->EventData,
  5825. sz);
  5826. fw_event->ioc = ioc;
  5827. fw_event->VF_ID = mpi_reply->VF_ID;
  5828. fw_event->VP_ID = mpi_reply->VP_ID;
  5829. fw_event->event = event;
  5830. _scsih_fw_event_add(ioc, fw_event);
  5831. return 1;
  5832. }
  5833. /* shost template */
  5834. static struct scsi_host_template scsih_driver_template = {
  5835. .module = THIS_MODULE,
  5836. .name = "Fusion MPT SAS Host",
  5837. .proc_name = MPT2SAS_DRIVER_NAME,
  5838. .queuecommand = _scsih_qcmd,
  5839. .target_alloc = _scsih_target_alloc,
  5840. .slave_alloc = _scsih_slave_alloc,
  5841. .slave_configure = _scsih_slave_configure,
  5842. .target_destroy = _scsih_target_destroy,
  5843. .slave_destroy = _scsih_slave_destroy,
  5844. .change_queue_depth = _scsih_change_queue_depth,
  5845. .change_queue_type = _scsih_change_queue_type,
  5846. .eh_abort_handler = _scsih_abort,
  5847. .eh_device_reset_handler = _scsih_dev_reset,
  5848. .eh_target_reset_handler = _scsih_target_reset,
  5849. .eh_host_reset_handler = _scsih_host_reset,
  5850. .bios_param = _scsih_bios_param,
  5851. .can_queue = 1,
  5852. .this_id = -1,
  5853. .sg_tablesize = MPT2SAS_SG_DEPTH,
  5854. .max_sectors = 8192,
  5855. .cmd_per_lun = 7,
  5856. .use_clustering = ENABLE_CLUSTERING,
  5857. .shost_attrs = mpt2sas_host_attrs,
  5858. .sdev_attrs = mpt2sas_dev_attrs,
  5859. };
  5860. /**
  5861. * _scsih_expander_node_remove - removing expander device from list.
  5862. * @ioc: per adapter object
  5863. * @sas_expander: the sas_device object
  5864. * Context: Calling function should acquire ioc->sas_node_lock.
  5865. *
  5866. * Removing object and freeing associated memory from the
  5867. * ioc->sas_expander_list.
  5868. *
  5869. * Return nothing.
  5870. */
  5871. static void
  5872. _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  5873. struct _sas_node *sas_expander)
  5874. {
  5875. struct _sas_port *mpt2sas_port;
  5876. struct _sas_device *sas_device;
  5877. struct _sas_node *expander_sibling;
  5878. unsigned long flags;
  5879. if (!sas_expander)
  5880. return;
  5881. /* remove sibling ports attached to this expander */
  5882. retry_device_search:
  5883. list_for_each_entry(mpt2sas_port,
  5884. &sas_expander->sas_port_list, port_list) {
  5885. if (mpt2sas_port->remote_identify.device_type ==
  5886. SAS_END_DEVICE) {
  5887. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5888. sas_device =
  5889. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  5890. mpt2sas_port->remote_identify.sas_address);
  5891. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5892. if (!sas_device)
  5893. continue;
  5894. _scsih_remove_device(ioc, sas_device);
  5895. if (ioc->shost_recovery)
  5896. return;
  5897. goto retry_device_search;
  5898. }
  5899. }
  5900. retry_expander_search:
  5901. list_for_each_entry(mpt2sas_port,
  5902. &sas_expander->sas_port_list, port_list) {
  5903. if (mpt2sas_port->remote_identify.device_type ==
  5904. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  5905. mpt2sas_port->remote_identify.device_type ==
  5906. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  5907. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5908. expander_sibling =
  5909. mpt2sas_scsih_expander_find_by_sas_address(
  5910. ioc, mpt2sas_port->remote_identify.sas_address);
  5911. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5912. if (!expander_sibling)
  5913. continue;
  5914. _scsih_expander_remove(ioc,
  5915. expander_sibling->sas_address);
  5916. if (ioc->shost_recovery)
  5917. return;
  5918. goto retry_expander_search;
  5919. }
  5920. }
  5921. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  5922. sas_expander->sas_address_parent);
  5923. printk(MPT2SAS_INFO_FMT "expander_remove: handle"
  5924. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  5925. sas_expander->handle, (unsigned long long)
  5926. sas_expander->sas_address);
  5927. list_del(&sas_expander->list);
  5928. kfree(sas_expander->phy);
  5929. kfree(sas_expander);
  5930. }
  5931. /**
  5932. * _scsih_ir_shutdown - IR shutdown notification
  5933. * @ioc: per adapter object
  5934. *
  5935. * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
  5936. * the host system is shutting down.
  5937. *
  5938. * Return nothing.
  5939. */
  5940. static void
  5941. _scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
  5942. {
  5943. Mpi2RaidActionRequest_t *mpi_request;
  5944. Mpi2RaidActionReply_t *mpi_reply;
  5945. u16 smid;
  5946. /* is IR firmware build loaded ? */
  5947. if (!ioc->ir_firmware)
  5948. return;
  5949. /* are there any volumes ? */
  5950. if (list_empty(&ioc->raid_device_list))
  5951. return;
  5952. mutex_lock(&ioc->scsih_cmds.mutex);
  5953. if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
  5954. printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
  5955. ioc->name, __func__);
  5956. goto out;
  5957. }
  5958. ioc->scsih_cmds.status = MPT2_CMD_PENDING;
  5959. smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  5960. if (!smid) {
  5961. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  5962. ioc->name, __func__);
  5963. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  5964. goto out;
  5965. }
  5966. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  5967. ioc->scsih_cmds.smid = smid;
  5968. memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
  5969. mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
  5970. mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
  5971. printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
  5972. init_completion(&ioc->scsih_cmds.done);
  5973. mpt2sas_base_put_smid_default(ioc, smid);
  5974. wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
  5975. if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
  5976. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  5977. ioc->name, __func__);
  5978. goto out;
  5979. }
  5980. if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
  5981. mpi_reply = ioc->scsih_cmds.reply;
  5982. printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
  5983. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  5984. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  5985. le32_to_cpu(mpi_reply->IOCLogInfo));
  5986. }
  5987. out:
  5988. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  5989. mutex_unlock(&ioc->scsih_cmds.mutex);
  5990. }
  5991. /**
  5992. * _scsih_shutdown - routine call during system shutdown
  5993. * @pdev: PCI device struct
  5994. *
  5995. * Return nothing.
  5996. */
  5997. static void
  5998. _scsih_shutdown(struct pci_dev *pdev)
  5999. {
  6000. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6001. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6002. struct workqueue_struct *wq;
  6003. unsigned long flags;
  6004. ioc->remove_host = 1;
  6005. _scsih_fw_event_cleanup_queue(ioc);
  6006. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  6007. wq = ioc->firmware_event_thread;
  6008. ioc->firmware_event_thread = NULL;
  6009. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  6010. if (wq)
  6011. destroy_workqueue(wq);
  6012. _scsih_ir_shutdown(ioc);
  6013. mpt2sas_base_detach(ioc);
  6014. }
  6015. /**
  6016. * _scsih_remove - detach and remove add host
  6017. * @pdev: PCI device struct
  6018. *
  6019. * Routine called when unloading the driver.
  6020. * Return nothing.
  6021. */
  6022. static void __devexit
  6023. _scsih_remove(struct pci_dev *pdev)
  6024. {
  6025. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6026. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6027. struct _sas_port *mpt2sas_port;
  6028. struct _sas_device *sas_device;
  6029. struct _sas_node *expander_sibling;
  6030. struct _raid_device *raid_device, *next;
  6031. struct MPT2SAS_TARGET *sas_target_priv_data;
  6032. struct workqueue_struct *wq;
  6033. unsigned long flags;
  6034. ioc->remove_host = 1;
  6035. _scsih_fw_event_cleanup_queue(ioc);
  6036. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  6037. wq = ioc->firmware_event_thread;
  6038. ioc->firmware_event_thread = NULL;
  6039. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  6040. if (wq)
  6041. destroy_workqueue(wq);
  6042. /* release all the volumes */
  6043. list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
  6044. list) {
  6045. if (raid_device->starget) {
  6046. sas_target_priv_data =
  6047. raid_device->starget->hostdata;
  6048. sas_target_priv_data->deleted = 1;
  6049. scsi_remove_target(&raid_device->starget->dev);
  6050. }
  6051. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
  6052. "(0x%016llx)\n", ioc->name, raid_device->handle,
  6053. (unsigned long long) raid_device->wwid);
  6054. _scsih_raid_device_remove(ioc, raid_device);
  6055. }
  6056. /* free ports attached to the sas_host */
  6057. retry_again:
  6058. list_for_each_entry(mpt2sas_port,
  6059. &ioc->sas_hba.sas_port_list, port_list) {
  6060. if (mpt2sas_port->remote_identify.device_type ==
  6061. SAS_END_DEVICE) {
  6062. sas_device =
  6063. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  6064. mpt2sas_port->remote_identify.sas_address);
  6065. if (sas_device) {
  6066. _scsih_remove_device(ioc, sas_device);
  6067. goto retry_again;
  6068. }
  6069. } else {
  6070. expander_sibling =
  6071. mpt2sas_scsih_expander_find_by_sas_address(ioc,
  6072. mpt2sas_port->remote_identify.sas_address);
  6073. if (expander_sibling) {
  6074. _scsih_expander_remove(ioc,
  6075. expander_sibling->sas_address);
  6076. goto retry_again;
  6077. }
  6078. }
  6079. }
  6080. /* free phys attached to the sas_host */
  6081. if (ioc->sas_hba.num_phys) {
  6082. kfree(ioc->sas_hba.phy);
  6083. ioc->sas_hba.phy = NULL;
  6084. ioc->sas_hba.num_phys = 0;
  6085. }
  6086. sas_remove_host(shost);
  6087. _scsih_shutdown(pdev);
  6088. list_del(&ioc->list);
  6089. scsi_remove_host(shost);
  6090. scsi_host_put(shost);
  6091. }
  6092. /**
  6093. * _scsih_probe_boot_devices - reports 1st device
  6094. * @ioc: per adapter object
  6095. *
  6096. * If specified in bios page 2, this routine reports the 1st
  6097. * device scsi-ml or sas transport for persistent boot device
  6098. * purposes. Please refer to function _scsih_determine_boot_device()
  6099. */
  6100. static void
  6101. _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
  6102. {
  6103. u8 is_raid;
  6104. void *device;
  6105. struct _sas_device *sas_device;
  6106. struct _raid_device *raid_device;
  6107. u16 handle;
  6108. u64 sas_address_parent;
  6109. u64 sas_address;
  6110. unsigned long flags;
  6111. int rc;
  6112. device = NULL;
  6113. if (ioc->req_boot_device.device) {
  6114. device = ioc->req_boot_device.device;
  6115. is_raid = ioc->req_boot_device.is_raid;
  6116. } else if (ioc->req_alt_boot_device.device) {
  6117. device = ioc->req_alt_boot_device.device;
  6118. is_raid = ioc->req_alt_boot_device.is_raid;
  6119. } else if (ioc->current_boot_device.device) {
  6120. device = ioc->current_boot_device.device;
  6121. is_raid = ioc->current_boot_device.is_raid;
  6122. }
  6123. if (!device)
  6124. return;
  6125. if (is_raid) {
  6126. raid_device = device;
  6127. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  6128. raid_device->id, 0);
  6129. if (rc)
  6130. _scsih_raid_device_remove(ioc, raid_device);
  6131. } else {
  6132. sas_device = device;
  6133. handle = sas_device->handle;
  6134. sas_address_parent = sas_device->sas_address_parent;
  6135. sas_address = sas_device->sas_address;
  6136. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6137. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  6138. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6139. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  6140. sas_device->sas_address_parent)) {
  6141. _scsih_sas_device_remove(ioc, sas_device);
  6142. } else if (!sas_device->starget) {
  6143. mpt2sas_transport_port_remove(ioc, sas_address,
  6144. sas_address_parent);
  6145. _scsih_sas_device_remove(ioc, sas_device);
  6146. }
  6147. }
  6148. }
  6149. /**
  6150. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  6151. * @ioc: per adapter object
  6152. *
  6153. * Called during initial loading of the driver.
  6154. */
  6155. static void
  6156. _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
  6157. {
  6158. struct _raid_device *raid_device, *raid_next;
  6159. int rc;
  6160. list_for_each_entry_safe(raid_device, raid_next,
  6161. &ioc->raid_device_list, list) {
  6162. if (raid_device->starget)
  6163. continue;
  6164. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  6165. raid_device->id, 0);
  6166. if (rc)
  6167. _scsih_raid_device_remove(ioc, raid_device);
  6168. }
  6169. }
  6170. /**
  6171. * _scsih_probe_sas - reporting sas devices to sas transport
  6172. * @ioc: per adapter object
  6173. *
  6174. * Called during initial loading of the driver.
  6175. */
  6176. static void
  6177. _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
  6178. {
  6179. struct _sas_device *sas_device, *next;
  6180. unsigned long flags;
  6181. /* SAS Device List */
  6182. list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
  6183. list) {
  6184. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6185. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  6186. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6187. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  6188. sas_device->sas_address_parent)) {
  6189. _scsih_sas_device_remove(ioc, sas_device);
  6190. } else if (!sas_device->starget) {
  6191. mpt2sas_transport_port_remove(ioc,
  6192. sas_device->sas_address,
  6193. sas_device->sas_address_parent);
  6194. _scsih_sas_device_remove(ioc, sas_device);
  6195. }
  6196. }
  6197. }
  6198. /**
  6199. * _scsih_probe_devices - probing for devices
  6200. * @ioc: per adapter object
  6201. *
  6202. * Called during initial loading of the driver.
  6203. */
  6204. static void
  6205. _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
  6206. {
  6207. u16 volume_mapping_flags =
  6208. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  6209. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  6210. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  6211. return; /* return when IOC doesn't support initiator mode */
  6212. _scsih_probe_boot_devices(ioc);
  6213. if (ioc->ir_firmware) {
  6214. if ((volume_mapping_flags &
  6215. MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
  6216. _scsih_probe_sas(ioc);
  6217. _scsih_probe_raid(ioc);
  6218. } else {
  6219. _scsih_probe_raid(ioc);
  6220. _scsih_probe_sas(ioc);
  6221. }
  6222. } else
  6223. _scsih_probe_sas(ioc);
  6224. }
  6225. /**
  6226. * _scsih_probe - attach and add scsi host
  6227. * @pdev: PCI device struct
  6228. * @id: pci device id
  6229. *
  6230. * Returns 0 success, anything else error.
  6231. */
  6232. static int
  6233. _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  6234. {
  6235. struct MPT2SAS_ADAPTER *ioc;
  6236. struct Scsi_Host *shost;
  6237. shost = scsi_host_alloc(&scsih_driver_template,
  6238. sizeof(struct MPT2SAS_ADAPTER));
  6239. if (!shost)
  6240. return -ENODEV;
  6241. /* init local params */
  6242. ioc = shost_priv(shost);
  6243. memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
  6244. INIT_LIST_HEAD(&ioc->list);
  6245. list_add_tail(&ioc->list, &mpt2sas_ioc_list);
  6246. ioc->shost = shost;
  6247. ioc->id = mpt_ids++;
  6248. sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
  6249. ioc->pdev = pdev;
  6250. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  6251. ioc->tm_cb_idx = tm_cb_idx;
  6252. ioc->ctl_cb_idx = ctl_cb_idx;
  6253. ioc->base_cb_idx = base_cb_idx;
  6254. ioc->transport_cb_idx = transport_cb_idx;
  6255. ioc->scsih_cb_idx = scsih_cb_idx;
  6256. ioc->config_cb_idx = config_cb_idx;
  6257. ioc->tm_tr_cb_idx = tm_tr_cb_idx;
  6258. ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
  6259. ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
  6260. ioc->logging_level = logging_level;
  6261. /* misc semaphores and spin locks */
  6262. mutex_init(&ioc->reset_in_progress_mutex);
  6263. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  6264. spin_lock_init(&ioc->scsi_lookup_lock);
  6265. spin_lock_init(&ioc->sas_device_lock);
  6266. spin_lock_init(&ioc->sas_node_lock);
  6267. spin_lock_init(&ioc->fw_event_lock);
  6268. spin_lock_init(&ioc->raid_device_lock);
  6269. INIT_LIST_HEAD(&ioc->sas_device_list);
  6270. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  6271. INIT_LIST_HEAD(&ioc->sas_expander_list);
  6272. INIT_LIST_HEAD(&ioc->fw_event_list);
  6273. INIT_LIST_HEAD(&ioc->raid_device_list);
  6274. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  6275. INIT_LIST_HEAD(&ioc->delayed_tr_list);
  6276. INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
  6277. /* init shost parameters */
  6278. shost->max_cmd_len = 32;
  6279. shost->max_lun = max_lun;
  6280. shost->transportt = mpt2sas_transport_template;
  6281. shost->unique_id = ioc->id;
  6282. if ((scsi_add_host(shost, &pdev->dev))) {
  6283. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  6284. ioc->name, __FILE__, __LINE__, __func__);
  6285. list_del(&ioc->list);
  6286. goto out_add_shost_fail;
  6287. }
  6288. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  6289. | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
  6290. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  6291. /* event thread */
  6292. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  6293. "fw_event%d", ioc->id);
  6294. ioc->firmware_event_thread = create_singlethread_workqueue(
  6295. ioc->firmware_event_name);
  6296. if (!ioc->firmware_event_thread) {
  6297. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  6298. ioc->name, __FILE__, __LINE__, __func__);
  6299. goto out_thread_fail;
  6300. }
  6301. ioc->wait_for_port_enable_to_complete = 1;
  6302. if ((mpt2sas_base_attach(ioc))) {
  6303. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  6304. ioc->name, __FILE__, __LINE__, __func__);
  6305. goto out_attach_fail;
  6306. }
  6307. ioc->wait_for_port_enable_to_complete = 0;
  6308. _scsih_probe_devices(ioc);
  6309. return 0;
  6310. out_attach_fail:
  6311. destroy_workqueue(ioc->firmware_event_thread);
  6312. out_thread_fail:
  6313. list_del(&ioc->list);
  6314. scsi_remove_host(shost);
  6315. out_add_shost_fail:
  6316. return -ENODEV;
  6317. }
  6318. #ifdef CONFIG_PM
  6319. /**
  6320. * _scsih_suspend - power management suspend main entry point
  6321. * @pdev: PCI device struct
  6322. * @state: PM state change to (usually PCI_D3)
  6323. *
  6324. * Returns 0 success, anything else error.
  6325. */
  6326. static int
  6327. _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  6328. {
  6329. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6330. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6331. u32 device_state;
  6332. mpt2sas_base_stop_watchdog(ioc);
  6333. flush_scheduled_work();
  6334. scsi_block_requests(shost);
  6335. device_state = pci_choose_state(pdev, state);
  6336. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
  6337. "operating state [D%d]\n", ioc->name, pdev,
  6338. pci_name(pdev), device_state);
  6339. mpt2sas_base_free_resources(ioc);
  6340. pci_save_state(pdev);
  6341. pci_disable_device(pdev);
  6342. pci_set_power_state(pdev, device_state);
  6343. return 0;
  6344. }
  6345. /**
  6346. * _scsih_resume - power management resume main entry point
  6347. * @pdev: PCI device struct
  6348. *
  6349. * Returns 0 success, anything else error.
  6350. */
  6351. static int
  6352. _scsih_resume(struct pci_dev *pdev)
  6353. {
  6354. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6355. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6356. u32 device_state = pdev->current_state;
  6357. int r;
  6358. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
  6359. "operating state [D%d]\n", ioc->name, pdev,
  6360. pci_name(pdev), device_state);
  6361. pci_set_power_state(pdev, PCI_D0);
  6362. pci_enable_wake(pdev, PCI_D0, 0);
  6363. pci_restore_state(pdev);
  6364. ioc->pdev = pdev;
  6365. r = mpt2sas_base_map_resources(ioc);
  6366. if (r)
  6367. return r;
  6368. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
  6369. scsi_unblock_requests(shost);
  6370. mpt2sas_base_start_watchdog(ioc);
  6371. return 0;
  6372. }
  6373. #endif /* CONFIG_PM */
  6374. /**
  6375. * _scsih_pci_error_detected - Called when a PCI error is detected.
  6376. * @pdev: PCI device struct
  6377. * @state: PCI channel state
  6378. *
  6379. * Description: Called when a PCI error is detected.
  6380. *
  6381. * Return value:
  6382. * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  6383. */
  6384. static pci_ers_result_t
  6385. _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  6386. {
  6387. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6388. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6389. printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
  6390. ioc->name, state);
  6391. switch (state) {
  6392. case pci_channel_io_normal:
  6393. return PCI_ERS_RESULT_CAN_RECOVER;
  6394. case pci_channel_io_frozen:
  6395. /* Fatal error, prepare for slot reset */
  6396. ioc->pci_error_recovery = 1;
  6397. scsi_block_requests(ioc->shost);
  6398. mpt2sas_base_stop_watchdog(ioc);
  6399. mpt2sas_base_free_resources(ioc);
  6400. return PCI_ERS_RESULT_NEED_RESET;
  6401. case pci_channel_io_perm_failure:
  6402. /* Permanent error, prepare for device removal */
  6403. ioc->pci_error_recovery = 1;
  6404. mpt2sas_base_stop_watchdog(ioc);
  6405. _scsih_flush_running_cmds(ioc);
  6406. return PCI_ERS_RESULT_DISCONNECT;
  6407. }
  6408. return PCI_ERS_RESULT_NEED_RESET;
  6409. }
  6410. /**
  6411. * _scsih_pci_slot_reset - Called when PCI slot has been reset.
  6412. * @pdev: PCI device struct
  6413. *
  6414. * Description: This routine is called by the pci error recovery
  6415. * code after the PCI slot has been reset, just before we
  6416. * should resume normal operations.
  6417. */
  6418. static pci_ers_result_t
  6419. _scsih_pci_slot_reset(struct pci_dev *pdev)
  6420. {
  6421. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6422. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6423. int rc;
  6424. printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
  6425. ioc->name);
  6426. ioc->pci_error_recovery = 0;
  6427. ioc->pdev = pdev;
  6428. pci_restore_state(pdev);
  6429. rc = mpt2sas_base_map_resources(ioc);
  6430. if (rc)
  6431. return PCI_ERS_RESULT_DISCONNECT;
  6432. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  6433. FORCE_BIG_HAMMER);
  6434. printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
  6435. (rc == 0) ? "success" : "failed");
  6436. if (!rc)
  6437. return PCI_ERS_RESULT_RECOVERED;
  6438. else
  6439. return PCI_ERS_RESULT_DISCONNECT;
  6440. }
  6441. /**
  6442. * _scsih_pci_resume() - resume normal ops after PCI reset
  6443. * @pdev: pointer to PCI device
  6444. *
  6445. * Called when the error recovery driver tells us that its
  6446. * OK to resume normal operation. Use completion to allow
  6447. * halted scsi ops to resume.
  6448. */
  6449. static void
  6450. _scsih_pci_resume(struct pci_dev *pdev)
  6451. {
  6452. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6453. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6454. printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
  6455. pci_cleanup_aer_uncorrect_error_status(pdev);
  6456. mpt2sas_base_start_watchdog(ioc);
  6457. scsi_unblock_requests(ioc->shost);
  6458. }
  6459. /**
  6460. * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
  6461. * @pdev: pointer to PCI device
  6462. */
  6463. static pci_ers_result_t
  6464. _scsih_pci_mmio_enabled(struct pci_dev *pdev)
  6465. {
  6466. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6467. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6468. printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
  6469. ioc->name);
  6470. /* TODO - dump whatever for debugging purposes */
  6471. /* Request a slot reset. */
  6472. return PCI_ERS_RESULT_NEED_RESET;
  6473. }
  6474. static struct pci_error_handlers _scsih_err_handler = {
  6475. .error_detected = _scsih_pci_error_detected,
  6476. .mmio_enabled = _scsih_pci_mmio_enabled,
  6477. .slot_reset = _scsih_pci_slot_reset,
  6478. .resume = _scsih_pci_resume,
  6479. };
  6480. static struct pci_driver scsih_driver = {
  6481. .name = MPT2SAS_DRIVER_NAME,
  6482. .id_table = scsih_pci_table,
  6483. .probe = _scsih_probe,
  6484. .remove = __devexit_p(_scsih_remove),
  6485. .shutdown = _scsih_shutdown,
  6486. .err_handler = &_scsih_err_handler,
  6487. #ifdef CONFIG_PM
  6488. .suspend = _scsih_suspend,
  6489. .resume = _scsih_resume,
  6490. #endif
  6491. };
  6492. /* raid transport support */
  6493. static struct raid_function_template mpt2sas_raid_functions = {
  6494. .cookie = &scsih_driver_template,
  6495. .is_raid = _scsih_is_raid,
  6496. .get_resync = _scsih_get_resync,
  6497. .get_state = _scsih_get_state,
  6498. };
  6499. /**
  6500. * _scsih_init - main entry point for this driver.
  6501. *
  6502. * Returns 0 success, anything else error.
  6503. */
  6504. static int __init
  6505. _scsih_init(void)
  6506. {
  6507. int error;
  6508. mpt_ids = 0;
  6509. printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
  6510. MPT2SAS_DRIVER_VERSION);
  6511. mpt2sas_transport_template =
  6512. sas_attach_transport(&mpt2sas_transport_functions);
  6513. if (!mpt2sas_transport_template)
  6514. return -ENODEV;
  6515. /* raid transport support */
  6516. mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
  6517. if (!mpt2sas_raid_template) {
  6518. sas_release_transport(mpt2sas_transport_template);
  6519. return -ENODEV;
  6520. }
  6521. mpt2sas_base_initialize_callback_handler();
  6522. /* queuecommand callback hander */
  6523. scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
  6524. /* task management callback handler */
  6525. tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
  6526. /* base internal commands callback handler */
  6527. base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
  6528. /* transport internal commands callback handler */
  6529. transport_cb_idx = mpt2sas_base_register_callback_handler(
  6530. mpt2sas_transport_done);
  6531. /* scsih internal commands callback handler */
  6532. scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
  6533. /* configuration page API internal commands callback handler */
  6534. config_cb_idx = mpt2sas_base_register_callback_handler(
  6535. mpt2sas_config_done);
  6536. /* ctl module callback handler */
  6537. ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
  6538. tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
  6539. _scsih_tm_tr_complete);
  6540. tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
  6541. _scsih_tm_volume_tr_complete);
  6542. tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
  6543. _scsih_sas_control_complete);
  6544. mpt2sas_ctl_init();
  6545. error = pci_register_driver(&scsih_driver);
  6546. if (error) {
  6547. /* raid transport support */
  6548. raid_class_release(mpt2sas_raid_template);
  6549. sas_release_transport(mpt2sas_transport_template);
  6550. }
  6551. return error;
  6552. }
  6553. /**
  6554. * _scsih_exit - exit point for this driver (when it is a module).
  6555. *
  6556. * Returns 0 success, anything else error.
  6557. */
  6558. static void __exit
  6559. _scsih_exit(void)
  6560. {
  6561. printk(KERN_INFO "mpt2sas version %s unloading\n",
  6562. MPT2SAS_DRIVER_VERSION);
  6563. pci_unregister_driver(&scsih_driver);
  6564. mpt2sas_ctl_exit();
  6565. mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
  6566. mpt2sas_base_release_callback_handler(tm_cb_idx);
  6567. mpt2sas_base_release_callback_handler(base_cb_idx);
  6568. mpt2sas_base_release_callback_handler(transport_cb_idx);
  6569. mpt2sas_base_release_callback_handler(scsih_cb_idx);
  6570. mpt2sas_base_release_callback_handler(config_cb_idx);
  6571. mpt2sas_base_release_callback_handler(ctl_cb_idx);
  6572. mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
  6573. mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
  6574. mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
  6575. /* raid transport support */
  6576. raid_class_release(mpt2sas_raid_template);
  6577. sas_release_transport(mpt2sas_transport_template);
  6578. }
  6579. module_init(_scsih_init);
  6580. module_exit(_scsih_exit);