mpt2sas_scsih.c 165 KB

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