mpt2sas_scsih.c 174 KB

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