mpt2sas_scsih.c 178 KB

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