mpt2sas_scsih.c 166 KB

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