mpt2sas_scsih.c 167 KB

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