mpt2sas_scsih.c 166 KB

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