mpt2sas_scsih.c 183 KB

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