mpt2sas_scsih.c 183 KB

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