mpt3sas_scsih.c 264 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340
  1. /*
  2. * Scsi Host Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
  5. * Copyright (C) 2012-2014 LSI Corporation
  6. * Copyright (C) 2013-2014 Avago Technologies
  7. * (mailto: MPT-FusionLinux.pdl@avagotech.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * NO WARRANTY
  20. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. * solely responsible for determining the appropriateness of using and
  25. * distributing the Program and assumes all risks associated with its
  26. * exercise of rights under this Agreement, including but not limited to
  27. * the risks and costs of program errors, damage to or loss of data,
  28. * programs or equipment, and unavailability or interruption of operations.
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  40. * USA.
  41. */
  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/pci-aspm.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/aer.h>
  54. #include <linux/raid_class.h>
  55. #include <asm/unaligned.h>
  56. #include "mpt3sas_base.h"
  57. #define RAID_CHANNEL 1
  58. /* forward proto's */
  59. static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
  60. struct _sas_node *sas_expander);
  61. static void _firmware_event_work(struct work_struct *work);
  62. static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
  63. struct _sas_device *sas_device);
  64. static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  65. u8 retry_count, u8 is_pd);
  66. static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
  67. /* global parameters */
  68. LIST_HEAD(mpt3sas_ioc_list);
  69. /* global ioc lock for list operations */
  70. DEFINE_SPINLOCK(gioc_lock);
  71. MODULE_AUTHOR(MPT3SAS_AUTHOR);
  72. MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
  73. MODULE_LICENSE("GPL");
  74. MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
  75. MODULE_ALIAS("mpt2sas");
  76. /* local parameters */
  77. static u8 scsi_io_cb_idx = -1;
  78. static u8 tm_cb_idx = -1;
  79. static u8 ctl_cb_idx = -1;
  80. static u8 base_cb_idx = -1;
  81. static u8 port_enable_cb_idx = -1;
  82. static u8 transport_cb_idx = -1;
  83. static u8 scsih_cb_idx = -1;
  84. static u8 config_cb_idx = -1;
  85. static int mpt2_ids;
  86. static int mpt3_ids;
  87. static u8 tm_tr_cb_idx = -1 ;
  88. static u8 tm_tr_volume_cb_idx = -1 ;
  89. static u8 tm_sas_control_cb_idx = -1;
  90. /* command line options */
  91. static u32 logging_level;
  92. MODULE_PARM_DESC(logging_level,
  93. " bits for enabling additional logging info (default=0)");
  94. static ushort max_sectors = 0xFFFF;
  95. module_param(max_sectors, ushort, 0);
  96. MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
  97. static int missing_delay[2] = {-1, -1};
  98. module_param_array(missing_delay, int, NULL, 0);
  99. MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
  100. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  101. #define MPT3SAS_MAX_LUN (16895)
  102. static u64 max_lun = MPT3SAS_MAX_LUN;
  103. module_param(max_lun, ullong, 0);
  104. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  105. static ushort hbas_to_enumerate;
  106. module_param(hbas_to_enumerate, ushort, 0);
  107. MODULE_PARM_DESC(hbas_to_enumerate,
  108. " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
  109. 1 - enumerates only SAS 2.0 generation HBAs\n \
  110. 2 - enumerates only SAS 3.0 generation HBAs (default=0)");
  111. /* diag_buffer_enable is bitwise
  112. * bit 0 set = TRACE
  113. * bit 1 set = SNAPSHOT
  114. * bit 2 set = EXTENDED
  115. *
  116. * Either bit can be set, or both
  117. */
  118. static int diag_buffer_enable = -1;
  119. module_param(diag_buffer_enable, int, 0);
  120. MODULE_PARM_DESC(diag_buffer_enable,
  121. " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
  122. static int disable_discovery = -1;
  123. module_param(disable_discovery, int, 0);
  124. MODULE_PARM_DESC(disable_discovery, " disable discovery ");
  125. /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
  126. static int prot_mask = -1;
  127. module_param(prot_mask, int, 0);
  128. MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
  129. /* raid transport support */
  130. struct raid_template *mpt3sas_raid_template;
  131. struct raid_template *mpt2sas_raid_template;
  132. /**
  133. * struct sense_info - common structure for obtaining sense keys
  134. * @skey: sense key
  135. * @asc: additional sense code
  136. * @ascq: additional sense code qualifier
  137. */
  138. struct sense_info {
  139. u8 skey;
  140. u8 asc;
  141. u8 ascq;
  142. };
  143. #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
  144. #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
  145. #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
  146. #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
  147. #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
  148. /**
  149. * struct fw_event_work - firmware event struct
  150. * @list: link list framework
  151. * @work: work object (ioc->fault_reset_work_q)
  152. * @ioc: per adapter object
  153. * @device_handle: device handle
  154. * @VF_ID: virtual function id
  155. * @VP_ID: virtual port id
  156. * @ignore: flag meaning this event has been marked to ignore
  157. * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
  158. * @refcount: kref for this event
  159. * @event_data: reply event data payload follows
  160. *
  161. * This object stored on ioc->fw_event_list.
  162. */
  163. struct fw_event_work {
  164. struct list_head list;
  165. struct work_struct work;
  166. struct MPT3SAS_ADAPTER *ioc;
  167. u16 device_handle;
  168. u8 VF_ID;
  169. u8 VP_ID;
  170. u8 ignore;
  171. u16 event;
  172. struct kref refcount;
  173. char event_data[0] __aligned(4);
  174. };
  175. static void fw_event_work_free(struct kref *r)
  176. {
  177. kfree(container_of(r, struct fw_event_work, refcount));
  178. }
  179. static void fw_event_work_get(struct fw_event_work *fw_work)
  180. {
  181. kref_get(&fw_work->refcount);
  182. }
  183. static void fw_event_work_put(struct fw_event_work *fw_work)
  184. {
  185. kref_put(&fw_work->refcount, fw_event_work_free);
  186. }
  187. static struct fw_event_work *alloc_fw_event_work(int len)
  188. {
  189. struct fw_event_work *fw_event;
  190. fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
  191. if (!fw_event)
  192. return NULL;
  193. kref_init(&fw_event->refcount);
  194. return fw_event;
  195. }
  196. /**
  197. * struct _scsi_io_transfer - scsi io transfer
  198. * @handle: sas device handle (assigned by firmware)
  199. * @is_raid: flag set for hidden raid components
  200. * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
  201. * @data_length: data transfer length
  202. * @data_dma: dma pointer to data
  203. * @sense: sense data
  204. * @lun: lun number
  205. * @cdb_length: cdb length
  206. * @cdb: cdb contents
  207. * @timeout: timeout for this command
  208. * @VF_ID: virtual function id
  209. * @VP_ID: virtual port id
  210. * @valid_reply: flag set for reply message
  211. * @sense_length: sense length
  212. * @ioc_status: ioc status
  213. * @scsi_state: scsi state
  214. * @scsi_status: scsi staus
  215. * @log_info: log information
  216. * @transfer_length: data length transfer when there is a reply message
  217. *
  218. * Used for sending internal scsi commands to devices within this module.
  219. * Refer to _scsi_send_scsi_io().
  220. */
  221. struct _scsi_io_transfer {
  222. u16 handle;
  223. u8 is_raid;
  224. enum dma_data_direction dir;
  225. u32 data_length;
  226. dma_addr_t data_dma;
  227. u8 sense[SCSI_SENSE_BUFFERSIZE];
  228. u32 lun;
  229. u8 cdb_length;
  230. u8 cdb[32];
  231. u8 timeout;
  232. u8 VF_ID;
  233. u8 VP_ID;
  234. u8 valid_reply;
  235. /* the following bits are only valid when 'valid_reply = 1' */
  236. u32 sense_length;
  237. u16 ioc_status;
  238. u8 scsi_state;
  239. u8 scsi_status;
  240. u32 log_info;
  241. u32 transfer_length;
  242. };
  243. /**
  244. * _scsih_set_debug_level - global setting of ioc->logging_level.
  245. *
  246. * Note: The logging levels are defined in mpt3sas_debug.h.
  247. */
  248. static int
  249. _scsih_set_debug_level(const char *val, struct kernel_param *kp)
  250. {
  251. int ret = param_set_int(val, kp);
  252. struct MPT3SAS_ADAPTER *ioc;
  253. if (ret)
  254. return ret;
  255. pr_info("setting logging_level(0x%08x)\n", logging_level);
  256. spin_lock(&gioc_lock);
  257. list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  258. ioc->logging_level = logging_level;
  259. spin_unlock(&gioc_lock);
  260. return 0;
  261. }
  262. module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
  263. &logging_level, 0644);
  264. /**
  265. * _scsih_srch_boot_sas_address - search based on sas_address
  266. * @sas_address: sas address
  267. * @boot_device: boot device object from bios page 2
  268. *
  269. * Returns 1 when there's a match, 0 means no match.
  270. */
  271. static inline int
  272. _scsih_srch_boot_sas_address(u64 sas_address,
  273. Mpi2BootDeviceSasWwid_t *boot_device)
  274. {
  275. return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
  276. }
  277. /**
  278. * _scsih_srch_boot_device_name - search based on device name
  279. * @device_name: device name specified in INDENTIFY fram
  280. * @boot_device: boot device object from bios page 2
  281. *
  282. * Returns 1 when there's a match, 0 means no match.
  283. */
  284. static inline int
  285. _scsih_srch_boot_device_name(u64 device_name,
  286. Mpi2BootDeviceDeviceName_t *boot_device)
  287. {
  288. return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
  289. }
  290. /**
  291. * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
  292. * @enclosure_logical_id: enclosure logical id
  293. * @slot_number: slot number
  294. * @boot_device: boot device object from bios page 2
  295. *
  296. * Returns 1 when there's a match, 0 means no match.
  297. */
  298. static inline int
  299. _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
  300. Mpi2BootDeviceEnclosureSlot_t *boot_device)
  301. {
  302. return (enclosure_logical_id == le64_to_cpu(boot_device->
  303. EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
  304. SlotNumber)) ? 1 : 0;
  305. }
  306. /**
  307. * _scsih_is_boot_device - search for matching boot device.
  308. * @sas_address: sas address
  309. * @device_name: device name specified in INDENTIFY fram
  310. * @enclosure_logical_id: enclosure logical id
  311. * @slot_number: slot number
  312. * @form: specifies boot device form
  313. * @boot_device: boot device object from bios page 2
  314. *
  315. * Returns 1 when there's a match, 0 means no match.
  316. */
  317. static int
  318. _scsih_is_boot_device(u64 sas_address, u64 device_name,
  319. u64 enclosure_logical_id, u16 slot, u8 form,
  320. Mpi2BiosPage2BootDevice_t *boot_device)
  321. {
  322. int rc = 0;
  323. switch (form) {
  324. case MPI2_BIOSPAGE2_FORM_SAS_WWID:
  325. if (!sas_address)
  326. break;
  327. rc = _scsih_srch_boot_sas_address(
  328. sas_address, &boot_device->SasWwid);
  329. break;
  330. case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
  331. if (!enclosure_logical_id)
  332. break;
  333. rc = _scsih_srch_boot_encl_slot(
  334. enclosure_logical_id,
  335. slot, &boot_device->EnclosureSlot);
  336. break;
  337. case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
  338. if (!device_name)
  339. break;
  340. rc = _scsih_srch_boot_device_name(
  341. device_name, &boot_device->DeviceName);
  342. break;
  343. case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
  344. break;
  345. }
  346. return rc;
  347. }
  348. /**
  349. * _scsih_get_sas_address - set the sas_address for given device handle
  350. * @handle: device handle
  351. * @sas_address: sas address
  352. *
  353. * Returns 0 success, non-zero when failure
  354. */
  355. static int
  356. _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  357. u64 *sas_address)
  358. {
  359. Mpi2SasDevicePage0_t sas_device_pg0;
  360. Mpi2ConfigReply_t mpi_reply;
  361. u32 ioc_status;
  362. *sas_address = 0;
  363. if (handle <= ioc->sas_hba.num_phys) {
  364. *sas_address = ioc->sas_hba.sas_address;
  365. return 0;
  366. }
  367. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  368. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  369. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
  370. __FILE__, __LINE__, __func__);
  371. return -ENXIO;
  372. }
  373. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  374. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  375. *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  376. return 0;
  377. }
  378. /* we hit this becuase the given parent handle doesn't exist */
  379. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  380. return -ENXIO;
  381. /* else error case */
  382. pr_err(MPT3SAS_FMT
  383. "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
  384. ioc->name, handle, ioc_status,
  385. __FILE__, __LINE__, __func__);
  386. return -EIO;
  387. }
  388. /**
  389. * _scsih_determine_boot_device - determine boot device.
  390. * @ioc: per adapter object
  391. * @device: either sas_device or raid_device object
  392. * @is_raid: [flag] 1 = raid object, 0 = sas object
  393. *
  394. * Determines whether this device should be first reported device to
  395. * to scsi-ml or sas transport, this purpose is for persistent boot device.
  396. * There are primary, alternate, and current entries in bios page 2. The order
  397. * priority is primary, alternate, then current. This routine saves
  398. * the corresponding device object and is_raid flag in the ioc object.
  399. * The saved data to be used later in _scsih_probe_boot_devices().
  400. */
  401. static void
  402. _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
  403. void *device, u8 is_raid)
  404. {
  405. struct _sas_device *sas_device;
  406. struct _raid_device *raid_device;
  407. u64 sas_address;
  408. u64 device_name;
  409. u64 enclosure_logical_id;
  410. u16 slot;
  411. /* only process this function when driver loads */
  412. if (!ioc->is_driver_loading)
  413. return;
  414. /* no Bios, return immediately */
  415. if (!ioc->bios_pg3.BiosVersion)
  416. return;
  417. if (!is_raid) {
  418. sas_device = device;
  419. sas_address = sas_device->sas_address;
  420. device_name = sas_device->device_name;
  421. enclosure_logical_id = sas_device->enclosure_logical_id;
  422. slot = sas_device->slot;
  423. } else {
  424. raid_device = device;
  425. sas_address = raid_device->wwid;
  426. device_name = 0;
  427. enclosure_logical_id = 0;
  428. slot = 0;
  429. }
  430. if (!ioc->req_boot_device.device) {
  431. if (_scsih_is_boot_device(sas_address, device_name,
  432. enclosure_logical_id, slot,
  433. (ioc->bios_pg2.ReqBootDeviceForm &
  434. MPI2_BIOSPAGE2_FORM_MASK),
  435. &ioc->bios_pg2.RequestedBootDevice)) {
  436. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  437. "%s: req_boot_device(0x%016llx)\n",
  438. ioc->name, __func__,
  439. (unsigned long long)sas_address));
  440. ioc->req_boot_device.device = device;
  441. ioc->req_boot_device.is_raid = is_raid;
  442. }
  443. }
  444. if (!ioc->req_alt_boot_device.device) {
  445. if (_scsih_is_boot_device(sas_address, device_name,
  446. enclosure_logical_id, slot,
  447. (ioc->bios_pg2.ReqAltBootDeviceForm &
  448. MPI2_BIOSPAGE2_FORM_MASK),
  449. &ioc->bios_pg2.RequestedAltBootDevice)) {
  450. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  451. "%s: req_alt_boot_device(0x%016llx)\n",
  452. ioc->name, __func__,
  453. (unsigned long long)sas_address));
  454. ioc->req_alt_boot_device.device = device;
  455. ioc->req_alt_boot_device.is_raid = is_raid;
  456. }
  457. }
  458. if (!ioc->current_boot_device.device) {
  459. if (_scsih_is_boot_device(sas_address, device_name,
  460. enclosure_logical_id, slot,
  461. (ioc->bios_pg2.CurrentBootDeviceForm &
  462. MPI2_BIOSPAGE2_FORM_MASK),
  463. &ioc->bios_pg2.CurrentBootDevice)) {
  464. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  465. "%s: current_boot_device(0x%016llx)\n",
  466. ioc->name, __func__,
  467. (unsigned long long)sas_address));
  468. ioc->current_boot_device.device = device;
  469. ioc->current_boot_device.is_raid = is_raid;
  470. }
  471. }
  472. }
  473. static struct _sas_device *
  474. __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
  475. struct MPT3SAS_TARGET *tgt_priv)
  476. {
  477. struct _sas_device *ret;
  478. assert_spin_locked(&ioc->sas_device_lock);
  479. ret = tgt_priv->sdev;
  480. if (ret)
  481. sas_device_get(ret);
  482. return ret;
  483. }
  484. static struct _sas_device *
  485. mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
  486. struct MPT3SAS_TARGET *tgt_priv)
  487. {
  488. struct _sas_device *ret;
  489. unsigned long flags;
  490. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  491. ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
  492. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  493. return ret;
  494. }
  495. struct _sas_device *
  496. __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
  497. u64 sas_address)
  498. {
  499. struct _sas_device *sas_device;
  500. assert_spin_locked(&ioc->sas_device_lock);
  501. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  502. if (sas_device->sas_address == sas_address)
  503. goto found_device;
  504. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  505. if (sas_device->sas_address == sas_address)
  506. goto found_device;
  507. return NULL;
  508. found_device:
  509. sas_device_get(sas_device);
  510. return sas_device;
  511. }
  512. /**
  513. * mpt3sas_get_sdev_by_addr - sas device search
  514. * @ioc: per adapter object
  515. * @sas_address: sas address
  516. * Context: Calling function should acquire ioc->sas_device_lock
  517. *
  518. * This searches for sas_device based on sas_address, then return sas_device
  519. * object.
  520. */
  521. struct _sas_device *
  522. mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
  523. u64 sas_address)
  524. {
  525. struct _sas_device *sas_device;
  526. unsigned long flags;
  527. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  528. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  529. sas_address);
  530. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  531. return sas_device;
  532. }
  533. static struct _sas_device *
  534. __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  535. {
  536. struct _sas_device *sas_device;
  537. assert_spin_locked(&ioc->sas_device_lock);
  538. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  539. if (sas_device->handle == handle)
  540. goto found_device;
  541. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  542. if (sas_device->handle == handle)
  543. goto found_device;
  544. return NULL;
  545. found_device:
  546. sas_device_get(sas_device);
  547. return sas_device;
  548. }
  549. /**
  550. * mpt3sas_get_sdev_by_handle - sas device search
  551. * @ioc: per adapter object
  552. * @handle: sas device handle (assigned by firmware)
  553. * Context: Calling function should acquire ioc->sas_device_lock
  554. *
  555. * This searches for sas_device based on sas_address, then return sas_device
  556. * object.
  557. */
  558. static struct _sas_device *
  559. mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  560. {
  561. struct _sas_device *sas_device;
  562. unsigned long flags;
  563. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  564. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  565. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  566. return sas_device;
  567. }
  568. /**
  569. * _scsih_sas_device_remove - remove sas_device from list.
  570. * @ioc: per adapter object
  571. * @sas_device: the sas_device object
  572. * Context: This function will acquire ioc->sas_device_lock.
  573. *
  574. * If sas_device is on the list, remove it and decrement its reference count.
  575. */
  576. static void
  577. _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
  578. struct _sas_device *sas_device)
  579. {
  580. unsigned long flags;
  581. if (!sas_device)
  582. return;
  583. pr_info(MPT3SAS_FMT
  584. "removing handle(0x%04x), sas_addr(0x%016llx)\n",
  585. ioc->name, sas_device->handle,
  586. (unsigned long long) sas_device->sas_address);
  587. if (sas_device->enclosure_handle != 0)
  588. pr_info(MPT3SAS_FMT
  589. "removing enclosure logical id(0x%016llx), slot(%d)\n",
  590. ioc->name, (unsigned long long)
  591. sas_device->enclosure_logical_id, sas_device->slot);
  592. if (sas_device->connector_name[0] != '\0')
  593. pr_info(MPT3SAS_FMT
  594. "removing enclosure level(0x%04x), connector name( %s)\n",
  595. ioc->name, sas_device->enclosure_level,
  596. sas_device->connector_name);
  597. /*
  598. * The lock serializes access to the list, but we still need to verify
  599. * that nobody removed the entry while we were waiting on the lock.
  600. */
  601. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  602. if (!list_empty(&sas_device->list)) {
  603. list_del_init(&sas_device->list);
  604. sas_device_put(sas_device);
  605. }
  606. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  607. }
  608. /**
  609. * _scsih_device_remove_by_handle - removing device object by handle
  610. * @ioc: per adapter object
  611. * @handle: device handle
  612. *
  613. * Return nothing.
  614. */
  615. static void
  616. _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  617. {
  618. struct _sas_device *sas_device;
  619. unsigned long flags;
  620. if (ioc->shost_recovery)
  621. return;
  622. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  623. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  624. if (sas_device) {
  625. list_del_init(&sas_device->list);
  626. sas_device_put(sas_device);
  627. }
  628. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  629. if (sas_device) {
  630. _scsih_remove_device(ioc, sas_device);
  631. sas_device_put(sas_device);
  632. }
  633. }
  634. /**
  635. * mpt3sas_device_remove_by_sas_address - removing device object by sas address
  636. * @ioc: per adapter object
  637. * @sas_address: device sas_address
  638. *
  639. * Return nothing.
  640. */
  641. void
  642. mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
  643. u64 sas_address)
  644. {
  645. struct _sas_device *sas_device;
  646. unsigned long flags;
  647. if (ioc->shost_recovery)
  648. return;
  649. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  650. sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
  651. if (sas_device) {
  652. list_del_init(&sas_device->list);
  653. sas_device_put(sas_device);
  654. }
  655. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  656. if (sas_device) {
  657. _scsih_remove_device(ioc, sas_device);
  658. sas_device_put(sas_device);
  659. }
  660. }
  661. /**
  662. * _scsih_sas_device_add - insert sas_device to the list.
  663. * @ioc: per adapter object
  664. * @sas_device: the sas_device object
  665. * Context: This function will acquire ioc->sas_device_lock.
  666. *
  667. * Adding new object to the ioc->sas_device_list.
  668. */
  669. static void
  670. _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
  671. struct _sas_device *sas_device)
  672. {
  673. unsigned long flags;
  674. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  675. "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
  676. ioc->name, __func__, sas_device->handle,
  677. (unsigned long long)sas_device->sas_address));
  678. if (sas_device->enclosure_handle != 0)
  679. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  680. "%s: enclosure logical id(0x%016llx), slot( %d)\n",
  681. ioc->name, __func__, (unsigned long long)
  682. sas_device->enclosure_logical_id, sas_device->slot));
  683. if (sas_device->connector_name[0] != '\0')
  684. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  685. "%s: enclosure level(0x%04x), connector name( %s)\n",
  686. ioc->name, __func__,
  687. sas_device->enclosure_level, sas_device->connector_name));
  688. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  689. sas_device_get(sas_device);
  690. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  691. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  692. if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
  693. sas_device->sas_address_parent)) {
  694. _scsih_sas_device_remove(ioc, sas_device);
  695. } else if (!sas_device->starget) {
  696. /*
  697. * When asyn scanning is enabled, its not possible to remove
  698. * devices while scanning is turned on due to an oops in
  699. * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
  700. */
  701. if (!ioc->is_driver_loading) {
  702. mpt3sas_transport_port_remove(ioc,
  703. sas_device->sas_address,
  704. sas_device->sas_address_parent);
  705. _scsih_sas_device_remove(ioc, sas_device);
  706. }
  707. }
  708. }
  709. /**
  710. * _scsih_sas_device_init_add - insert sas_device to the list.
  711. * @ioc: per adapter object
  712. * @sas_device: the sas_device object
  713. * Context: This function will acquire ioc->sas_device_lock.
  714. *
  715. * Adding new object at driver load time to the ioc->sas_device_init_list.
  716. */
  717. static void
  718. _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
  719. struct _sas_device *sas_device)
  720. {
  721. unsigned long flags;
  722. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  723. "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  724. __func__, sas_device->handle,
  725. (unsigned long long)sas_device->sas_address));
  726. if (sas_device->enclosure_handle != 0)
  727. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  728. "%s: enclosure logical id(0x%016llx), slot( %d)\n",
  729. ioc->name, __func__, (unsigned long long)
  730. sas_device->enclosure_logical_id, sas_device->slot));
  731. if (sas_device->connector_name[0] != '\0')
  732. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  733. "%s: enclosure level(0x%04x), connector name( %s)\n",
  734. ioc->name, __func__, sas_device->enclosure_level,
  735. sas_device->connector_name));
  736. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  737. sas_device_get(sas_device);
  738. list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
  739. _scsih_determine_boot_device(ioc, sas_device, 0);
  740. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  741. }
  742. /**
  743. * _scsih_raid_device_find_by_id - raid device search
  744. * @ioc: per adapter object
  745. * @id: sas device target id
  746. * @channel: sas device channel
  747. * Context: Calling function should acquire ioc->raid_device_lock
  748. *
  749. * This searches for raid_device based on target id, then return raid_device
  750. * object.
  751. */
  752. static struct _raid_device *
  753. _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
  754. {
  755. struct _raid_device *raid_device, *r;
  756. r = NULL;
  757. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  758. if (raid_device->id == id && raid_device->channel == channel) {
  759. r = raid_device;
  760. goto out;
  761. }
  762. }
  763. out:
  764. return r;
  765. }
  766. /**
  767. * mpt3sas_raid_device_find_by_handle - raid device search
  768. * @ioc: per adapter object
  769. * @handle: sas device handle (assigned by firmware)
  770. * Context: Calling function should acquire ioc->raid_device_lock
  771. *
  772. * This searches for raid_device based on handle, then return raid_device
  773. * object.
  774. */
  775. struct _raid_device *
  776. mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  777. {
  778. struct _raid_device *raid_device, *r;
  779. r = NULL;
  780. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  781. if (raid_device->handle != handle)
  782. continue;
  783. r = raid_device;
  784. goto out;
  785. }
  786. out:
  787. return r;
  788. }
  789. /**
  790. * _scsih_raid_device_find_by_wwid - raid device search
  791. * @ioc: per adapter object
  792. * @handle: sas device handle (assigned by firmware)
  793. * Context: Calling function should acquire ioc->raid_device_lock
  794. *
  795. * This searches for raid_device based on wwid, then return raid_device
  796. * object.
  797. */
  798. static struct _raid_device *
  799. _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
  800. {
  801. struct _raid_device *raid_device, *r;
  802. r = NULL;
  803. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  804. if (raid_device->wwid != wwid)
  805. continue;
  806. r = raid_device;
  807. goto out;
  808. }
  809. out:
  810. return r;
  811. }
  812. /**
  813. * _scsih_raid_device_add - add raid_device object
  814. * @ioc: per adapter object
  815. * @raid_device: raid_device object
  816. *
  817. * This is added to the raid_device_list link list.
  818. */
  819. static void
  820. _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
  821. struct _raid_device *raid_device)
  822. {
  823. unsigned long flags;
  824. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  825. "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  826. raid_device->handle, (unsigned long long)raid_device->wwid));
  827. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  828. list_add_tail(&raid_device->list, &ioc->raid_device_list);
  829. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  830. }
  831. /**
  832. * _scsih_raid_device_remove - delete raid_device object
  833. * @ioc: per adapter object
  834. * @raid_device: raid_device object
  835. *
  836. */
  837. static void
  838. _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
  839. struct _raid_device *raid_device)
  840. {
  841. unsigned long flags;
  842. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  843. list_del(&raid_device->list);
  844. kfree(raid_device);
  845. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  846. }
  847. /**
  848. * mpt3sas_scsih_expander_find_by_handle - expander device search
  849. * @ioc: per adapter object
  850. * @handle: expander handle (assigned by firmware)
  851. * Context: Calling function should acquire ioc->sas_device_lock
  852. *
  853. * This searches for expander device based on handle, then returns the
  854. * sas_node object.
  855. */
  856. struct _sas_node *
  857. mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  858. {
  859. struct _sas_node *sas_expander, *r;
  860. r = NULL;
  861. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  862. if (sas_expander->handle != handle)
  863. continue;
  864. r = sas_expander;
  865. goto out;
  866. }
  867. out:
  868. return r;
  869. }
  870. /**
  871. * mpt3sas_scsih_expander_find_by_sas_address - expander device search
  872. * @ioc: per adapter object
  873. * @sas_address: sas address
  874. * Context: Calling function should acquire ioc->sas_node_lock.
  875. *
  876. * This searches for expander device based on sas_address, then returns the
  877. * sas_node object.
  878. */
  879. struct _sas_node *
  880. mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
  881. u64 sas_address)
  882. {
  883. struct _sas_node *sas_expander, *r;
  884. r = NULL;
  885. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  886. if (sas_expander->sas_address != sas_address)
  887. continue;
  888. r = sas_expander;
  889. goto out;
  890. }
  891. out:
  892. return r;
  893. }
  894. /**
  895. * _scsih_expander_node_add - insert expander device to the list.
  896. * @ioc: per adapter object
  897. * @sas_expander: the sas_device object
  898. * Context: This function will acquire ioc->sas_node_lock.
  899. *
  900. * Adding new object to the ioc->sas_expander_list.
  901. *
  902. * Return nothing.
  903. */
  904. static void
  905. _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
  906. struct _sas_node *sas_expander)
  907. {
  908. unsigned long flags;
  909. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  910. list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
  911. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  912. }
  913. /**
  914. * _scsih_is_end_device - determines if device is an end device
  915. * @device_info: bitfield providing information about the device.
  916. * Context: none
  917. *
  918. * Returns 1 if end device.
  919. */
  920. static int
  921. _scsih_is_end_device(u32 device_info)
  922. {
  923. if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
  924. ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
  925. (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
  926. (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
  927. return 1;
  928. else
  929. return 0;
  930. }
  931. /**
  932. * _scsih_scsi_lookup_get - returns scmd entry
  933. * @ioc: per adapter object
  934. * @smid: system request message index
  935. *
  936. * Returns the smid stored scmd pointer.
  937. */
  938. static struct scsi_cmnd *
  939. _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  940. {
  941. return ioc->scsi_lookup[smid - 1].scmd;
  942. }
  943. /**
  944. * _scsih_scsi_lookup_get_clear - returns scmd entry
  945. * @ioc: per adapter object
  946. * @smid: system request message index
  947. *
  948. * Returns the smid stored scmd pointer.
  949. * Then will derefrence the stored scmd pointer.
  950. */
  951. static inline struct scsi_cmnd *
  952. _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  953. {
  954. unsigned long flags;
  955. struct scsi_cmnd *scmd;
  956. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  957. scmd = ioc->scsi_lookup[smid - 1].scmd;
  958. ioc->scsi_lookup[smid - 1].scmd = NULL;
  959. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  960. return scmd;
  961. }
  962. /**
  963. * _scsih_scsi_lookup_find_by_scmd - scmd lookup
  964. * @ioc: per adapter object
  965. * @smid: system request message index
  966. * @scmd: pointer to scsi command object
  967. * Context: This function will acquire ioc->scsi_lookup_lock.
  968. *
  969. * This will search for a scmd pointer in the scsi_lookup array,
  970. * returning the revelent smid. A returned value of zero means invalid.
  971. */
  972. static u16
  973. _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
  974. *scmd)
  975. {
  976. u16 smid;
  977. unsigned long flags;
  978. int i;
  979. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  980. smid = 0;
  981. for (i = 0; i < ioc->scsiio_depth; i++) {
  982. if (ioc->scsi_lookup[i].scmd == scmd) {
  983. smid = ioc->scsi_lookup[i].smid;
  984. goto out;
  985. }
  986. }
  987. out:
  988. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  989. return smid;
  990. }
  991. /**
  992. * _scsih_scsi_lookup_find_by_target - search for matching channel:id
  993. * @ioc: per adapter object
  994. * @id: target id
  995. * @channel: channel
  996. * Context: This function will acquire ioc->scsi_lookup_lock.
  997. *
  998. * This will search for a matching channel:id in the scsi_lookup array,
  999. * returning 1 if found.
  1000. */
  1001. static u8
  1002. _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
  1003. int channel)
  1004. {
  1005. u8 found;
  1006. unsigned long flags;
  1007. int i;
  1008. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1009. found = 0;
  1010. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  1011. if (ioc->scsi_lookup[i].scmd &&
  1012. (ioc->scsi_lookup[i].scmd->device->id == id &&
  1013. ioc->scsi_lookup[i].scmd->device->channel == channel)) {
  1014. found = 1;
  1015. goto out;
  1016. }
  1017. }
  1018. out:
  1019. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1020. return found;
  1021. }
  1022. /**
  1023. * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
  1024. * @ioc: per adapter object
  1025. * @id: target id
  1026. * @lun: lun number
  1027. * @channel: channel
  1028. * Context: This function will acquire ioc->scsi_lookup_lock.
  1029. *
  1030. * This will search for a matching channel:id:lun in the scsi_lookup array,
  1031. * returning 1 if found.
  1032. */
  1033. static u8
  1034. _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
  1035. unsigned int lun, int channel)
  1036. {
  1037. u8 found;
  1038. unsigned long flags;
  1039. int i;
  1040. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1041. found = 0;
  1042. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  1043. if (ioc->scsi_lookup[i].scmd &&
  1044. (ioc->scsi_lookup[i].scmd->device->id == id &&
  1045. ioc->scsi_lookup[i].scmd->device->channel == channel &&
  1046. ioc->scsi_lookup[i].scmd->device->lun == lun)) {
  1047. found = 1;
  1048. goto out;
  1049. }
  1050. }
  1051. out:
  1052. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1053. return found;
  1054. }
  1055. /**
  1056. * scsih_change_queue_depth - setting device queue depth
  1057. * @sdev: scsi device struct
  1058. * @qdepth: requested queue depth
  1059. *
  1060. * Returns queue depth.
  1061. */
  1062. static int
  1063. scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
  1064. {
  1065. struct Scsi_Host *shost = sdev->host;
  1066. int max_depth;
  1067. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1068. struct MPT3SAS_DEVICE *sas_device_priv_data;
  1069. struct MPT3SAS_TARGET *sas_target_priv_data;
  1070. struct _sas_device *sas_device;
  1071. unsigned long flags;
  1072. max_depth = shost->can_queue;
  1073. /* limit max device queue for SATA to 32 */
  1074. sas_device_priv_data = sdev->hostdata;
  1075. if (!sas_device_priv_data)
  1076. goto not_sata;
  1077. sas_target_priv_data = sas_device_priv_data->sas_target;
  1078. if (!sas_target_priv_data)
  1079. goto not_sata;
  1080. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
  1081. goto not_sata;
  1082. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1083. sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
  1084. if (sas_device) {
  1085. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1086. max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
  1087. sas_device_put(sas_device);
  1088. }
  1089. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1090. not_sata:
  1091. if (!sdev->tagged_supported)
  1092. max_depth = 1;
  1093. if (qdepth > max_depth)
  1094. qdepth = max_depth;
  1095. return scsi_change_queue_depth(sdev, qdepth);
  1096. }
  1097. /**
  1098. * scsih_target_alloc - target add routine
  1099. * @starget: scsi target struct
  1100. *
  1101. * Returns 0 if ok. Any other return is assumed to be an error and
  1102. * the device is ignored.
  1103. */
  1104. static int
  1105. scsih_target_alloc(struct scsi_target *starget)
  1106. {
  1107. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1108. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1109. struct MPT3SAS_TARGET *sas_target_priv_data;
  1110. struct _sas_device *sas_device;
  1111. struct _raid_device *raid_device;
  1112. unsigned long flags;
  1113. struct sas_rphy *rphy;
  1114. sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
  1115. GFP_KERNEL);
  1116. if (!sas_target_priv_data)
  1117. return -ENOMEM;
  1118. starget->hostdata = sas_target_priv_data;
  1119. sas_target_priv_data->starget = starget;
  1120. sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
  1121. /* RAID volumes */
  1122. if (starget->channel == RAID_CHANNEL) {
  1123. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1124. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1125. starget->channel);
  1126. if (raid_device) {
  1127. sas_target_priv_data->handle = raid_device->handle;
  1128. sas_target_priv_data->sas_address = raid_device->wwid;
  1129. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
  1130. if (ioc->is_warpdrive)
  1131. sas_target_priv_data->raid_device = raid_device;
  1132. raid_device->starget = starget;
  1133. }
  1134. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1135. return 0;
  1136. }
  1137. /* sas/sata devices */
  1138. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1139. rphy = dev_to_rphy(starget->dev.parent);
  1140. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  1141. rphy->identify.sas_address);
  1142. if (sas_device) {
  1143. sas_target_priv_data->handle = sas_device->handle;
  1144. sas_target_priv_data->sas_address = sas_device->sas_address;
  1145. sas_target_priv_data->sdev = sas_device;
  1146. sas_device->starget = starget;
  1147. sas_device->id = starget->id;
  1148. sas_device->channel = starget->channel;
  1149. if (test_bit(sas_device->handle, ioc->pd_handles))
  1150. sas_target_priv_data->flags |=
  1151. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1152. if (sas_device->fast_path)
  1153. sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
  1154. }
  1155. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1156. return 0;
  1157. }
  1158. /**
  1159. * scsih_target_destroy - target destroy routine
  1160. * @starget: scsi target struct
  1161. *
  1162. * Returns nothing.
  1163. */
  1164. static void
  1165. scsih_target_destroy(struct scsi_target *starget)
  1166. {
  1167. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1168. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1169. struct MPT3SAS_TARGET *sas_target_priv_data;
  1170. struct _sas_device *sas_device;
  1171. struct _raid_device *raid_device;
  1172. unsigned long flags;
  1173. sas_target_priv_data = starget->hostdata;
  1174. if (!sas_target_priv_data)
  1175. return;
  1176. if (starget->channel == RAID_CHANNEL) {
  1177. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1178. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1179. starget->channel);
  1180. if (raid_device) {
  1181. raid_device->starget = NULL;
  1182. raid_device->sdev = NULL;
  1183. }
  1184. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1185. goto out;
  1186. }
  1187. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1188. sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
  1189. if (sas_device && (sas_device->starget == starget) &&
  1190. (sas_device->id == starget->id) &&
  1191. (sas_device->channel == starget->channel))
  1192. sas_device->starget = NULL;
  1193. if (sas_device) {
  1194. /*
  1195. * Corresponding get() is in _scsih_target_alloc()
  1196. */
  1197. sas_target_priv_data->sdev = NULL;
  1198. sas_device_put(sas_device);
  1199. sas_device_put(sas_device);
  1200. }
  1201. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1202. out:
  1203. kfree(sas_target_priv_data);
  1204. starget->hostdata = NULL;
  1205. }
  1206. /**
  1207. * scsih_slave_alloc - device add routine
  1208. * @sdev: scsi device struct
  1209. *
  1210. * Returns 0 if ok. Any other return is assumed to be an error and
  1211. * the device is ignored.
  1212. */
  1213. static int
  1214. scsih_slave_alloc(struct scsi_device *sdev)
  1215. {
  1216. struct Scsi_Host *shost;
  1217. struct MPT3SAS_ADAPTER *ioc;
  1218. struct MPT3SAS_TARGET *sas_target_priv_data;
  1219. struct MPT3SAS_DEVICE *sas_device_priv_data;
  1220. struct scsi_target *starget;
  1221. struct _raid_device *raid_device;
  1222. struct _sas_device *sas_device;
  1223. unsigned long flags;
  1224. sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
  1225. GFP_KERNEL);
  1226. if (!sas_device_priv_data)
  1227. return -ENOMEM;
  1228. sas_device_priv_data->lun = sdev->lun;
  1229. sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
  1230. starget = scsi_target(sdev);
  1231. sas_target_priv_data = starget->hostdata;
  1232. sas_target_priv_data->num_luns++;
  1233. sas_device_priv_data->sas_target = sas_target_priv_data;
  1234. sdev->hostdata = sas_device_priv_data;
  1235. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
  1236. sdev->no_uld_attach = 1;
  1237. shost = dev_to_shost(&starget->dev);
  1238. ioc = shost_priv(shost);
  1239. if (starget->channel == RAID_CHANNEL) {
  1240. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1241. raid_device = _scsih_raid_device_find_by_id(ioc,
  1242. starget->id, starget->channel);
  1243. if (raid_device)
  1244. raid_device->sdev = sdev; /* raid is single lun */
  1245. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1246. }
  1247. if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
  1248. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1249. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  1250. sas_target_priv_data->sas_address);
  1251. if (sas_device && (sas_device->starget == NULL)) {
  1252. sdev_printk(KERN_INFO, sdev,
  1253. "%s : sas_device->starget set to starget @ %d\n",
  1254. __func__, __LINE__);
  1255. sas_device->starget = starget;
  1256. }
  1257. if (sas_device)
  1258. sas_device_put(sas_device);
  1259. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1260. }
  1261. return 0;
  1262. }
  1263. /**
  1264. * scsih_slave_destroy - device destroy routine
  1265. * @sdev: scsi device struct
  1266. *
  1267. * Returns nothing.
  1268. */
  1269. static void
  1270. scsih_slave_destroy(struct scsi_device *sdev)
  1271. {
  1272. struct MPT3SAS_TARGET *sas_target_priv_data;
  1273. struct scsi_target *starget;
  1274. struct Scsi_Host *shost;
  1275. struct MPT3SAS_ADAPTER *ioc;
  1276. struct _sas_device *sas_device;
  1277. unsigned long flags;
  1278. if (!sdev->hostdata)
  1279. return;
  1280. starget = scsi_target(sdev);
  1281. sas_target_priv_data = starget->hostdata;
  1282. sas_target_priv_data->num_luns--;
  1283. shost = dev_to_shost(&starget->dev);
  1284. ioc = shost_priv(shost);
  1285. if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
  1286. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1287. sas_device = __mpt3sas_get_sdev_from_target(ioc,
  1288. sas_target_priv_data);
  1289. if (sas_device && !sas_target_priv_data->num_luns)
  1290. sas_device->starget = NULL;
  1291. if (sas_device)
  1292. sas_device_put(sas_device);
  1293. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1294. }
  1295. kfree(sdev->hostdata);
  1296. sdev->hostdata = NULL;
  1297. }
  1298. /**
  1299. * _scsih_display_sata_capabilities - sata capabilities
  1300. * @ioc: per adapter object
  1301. * @handle: device handle
  1302. * @sdev: scsi device struct
  1303. */
  1304. static void
  1305. _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
  1306. u16 handle, struct scsi_device *sdev)
  1307. {
  1308. Mpi2ConfigReply_t mpi_reply;
  1309. Mpi2SasDevicePage0_t sas_device_pg0;
  1310. u32 ioc_status;
  1311. u16 flags;
  1312. u32 device_info;
  1313. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  1314. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  1315. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1316. ioc->name, __FILE__, __LINE__, __func__);
  1317. return;
  1318. }
  1319. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1320. MPI2_IOCSTATUS_MASK;
  1321. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1322. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1323. ioc->name, __FILE__, __LINE__, __func__);
  1324. return;
  1325. }
  1326. flags = le16_to_cpu(sas_device_pg0.Flags);
  1327. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  1328. sdev_printk(KERN_INFO, sdev,
  1329. "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
  1330. "sw_preserve(%s)\n",
  1331. (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
  1332. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
  1333. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
  1334. "n",
  1335. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
  1336. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
  1337. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
  1338. }
  1339. /*
  1340. * raid transport support -
  1341. * Enabled for SLES11 and newer, in older kernels the driver will panic when
  1342. * unloading the driver followed by a load - I beleive that the subroutine
  1343. * raid_class_release() is not cleaning up properly.
  1344. */
  1345. /**
  1346. * scsih_is_raid - return boolean indicating device is raid volume
  1347. * @dev the device struct object
  1348. */
  1349. static int
  1350. scsih_is_raid(struct device *dev)
  1351. {
  1352. struct scsi_device *sdev = to_scsi_device(dev);
  1353. struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1354. if (ioc->is_warpdrive)
  1355. return 0;
  1356. return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
  1357. }
  1358. /**
  1359. * scsih_get_resync - get raid volume resync percent complete
  1360. * @dev the device struct object
  1361. */
  1362. static void
  1363. scsih_get_resync(struct device *dev)
  1364. {
  1365. struct scsi_device *sdev = to_scsi_device(dev);
  1366. struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1367. static struct _raid_device *raid_device;
  1368. unsigned long flags;
  1369. Mpi2RaidVolPage0_t vol_pg0;
  1370. Mpi2ConfigReply_t mpi_reply;
  1371. u32 volume_status_flags;
  1372. u8 percent_complete;
  1373. u16 handle;
  1374. percent_complete = 0;
  1375. handle = 0;
  1376. if (ioc->is_warpdrive)
  1377. goto out;
  1378. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1379. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1380. sdev->channel);
  1381. if (raid_device) {
  1382. handle = raid_device->handle;
  1383. percent_complete = raid_device->percent_complete;
  1384. }
  1385. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1386. if (!handle)
  1387. goto out;
  1388. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1389. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  1390. sizeof(Mpi2RaidVolPage0_t))) {
  1391. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1392. ioc->name, __FILE__, __LINE__, __func__);
  1393. percent_complete = 0;
  1394. goto out;
  1395. }
  1396. volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1397. if (!(volume_status_flags &
  1398. MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
  1399. percent_complete = 0;
  1400. out:
  1401. switch (ioc->hba_mpi_version_belonged) {
  1402. case MPI2_VERSION:
  1403. raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
  1404. break;
  1405. case MPI25_VERSION:
  1406. case MPI26_VERSION:
  1407. raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
  1408. break;
  1409. }
  1410. }
  1411. /**
  1412. * scsih_get_state - get raid volume level
  1413. * @dev the device struct object
  1414. */
  1415. static void
  1416. scsih_get_state(struct device *dev)
  1417. {
  1418. struct scsi_device *sdev = to_scsi_device(dev);
  1419. struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1420. static struct _raid_device *raid_device;
  1421. unsigned long flags;
  1422. Mpi2RaidVolPage0_t vol_pg0;
  1423. Mpi2ConfigReply_t mpi_reply;
  1424. u32 volstate;
  1425. enum raid_state state = RAID_STATE_UNKNOWN;
  1426. u16 handle = 0;
  1427. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1428. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1429. sdev->channel);
  1430. if (raid_device)
  1431. handle = raid_device->handle;
  1432. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1433. if (!raid_device)
  1434. goto out;
  1435. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1436. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  1437. sizeof(Mpi2RaidVolPage0_t))) {
  1438. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  1439. ioc->name, __FILE__, __LINE__, __func__);
  1440. goto out;
  1441. }
  1442. volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1443. if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
  1444. state = RAID_STATE_RESYNCING;
  1445. goto out;
  1446. }
  1447. switch (vol_pg0.VolumeState) {
  1448. case MPI2_RAID_VOL_STATE_OPTIMAL:
  1449. case MPI2_RAID_VOL_STATE_ONLINE:
  1450. state = RAID_STATE_ACTIVE;
  1451. break;
  1452. case MPI2_RAID_VOL_STATE_DEGRADED:
  1453. state = RAID_STATE_DEGRADED;
  1454. break;
  1455. case MPI2_RAID_VOL_STATE_FAILED:
  1456. case MPI2_RAID_VOL_STATE_MISSING:
  1457. state = RAID_STATE_OFFLINE;
  1458. break;
  1459. }
  1460. out:
  1461. switch (ioc->hba_mpi_version_belonged) {
  1462. case MPI2_VERSION:
  1463. raid_set_state(mpt2sas_raid_template, dev, state);
  1464. break;
  1465. case MPI25_VERSION:
  1466. case MPI26_VERSION:
  1467. raid_set_state(mpt3sas_raid_template, dev, state);
  1468. break;
  1469. }
  1470. }
  1471. /**
  1472. * _scsih_set_level - set raid level
  1473. * @sdev: scsi device struct
  1474. * @volume_type: volume type
  1475. */
  1476. static void
  1477. _scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
  1478. struct scsi_device *sdev, u8 volume_type)
  1479. {
  1480. enum raid_level level = RAID_LEVEL_UNKNOWN;
  1481. switch (volume_type) {
  1482. case MPI2_RAID_VOL_TYPE_RAID0:
  1483. level = RAID_LEVEL_0;
  1484. break;
  1485. case MPI2_RAID_VOL_TYPE_RAID10:
  1486. level = RAID_LEVEL_10;
  1487. break;
  1488. case MPI2_RAID_VOL_TYPE_RAID1E:
  1489. level = RAID_LEVEL_1E;
  1490. break;
  1491. case MPI2_RAID_VOL_TYPE_RAID1:
  1492. level = RAID_LEVEL_1;
  1493. break;
  1494. }
  1495. switch (ioc->hba_mpi_version_belonged) {
  1496. case MPI2_VERSION:
  1497. raid_set_level(mpt2sas_raid_template,
  1498. &sdev->sdev_gendev, level);
  1499. break;
  1500. case MPI25_VERSION:
  1501. case MPI26_VERSION:
  1502. raid_set_level(mpt3sas_raid_template,
  1503. &sdev->sdev_gendev, level);
  1504. break;
  1505. }
  1506. }
  1507. /**
  1508. * _scsih_get_volume_capabilities - volume capabilities
  1509. * @ioc: per adapter object
  1510. * @sas_device: the raid_device object
  1511. *
  1512. * Returns 0 for success, else 1
  1513. */
  1514. static int
  1515. _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
  1516. struct _raid_device *raid_device)
  1517. {
  1518. Mpi2RaidVolPage0_t *vol_pg0;
  1519. Mpi2RaidPhysDiskPage0_t pd_pg0;
  1520. Mpi2SasDevicePage0_t sas_device_pg0;
  1521. Mpi2ConfigReply_t mpi_reply;
  1522. u16 sz;
  1523. u8 num_pds;
  1524. if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
  1525. &num_pds)) || !num_pds) {
  1526. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1527. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1528. __func__));
  1529. return 1;
  1530. }
  1531. raid_device->num_pds = num_pds;
  1532. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  1533. sizeof(Mpi2RaidVol0PhysDisk_t));
  1534. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  1535. if (!vol_pg0) {
  1536. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1537. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1538. __func__));
  1539. return 1;
  1540. }
  1541. if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  1542. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  1543. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1544. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1545. __func__));
  1546. kfree(vol_pg0);
  1547. return 1;
  1548. }
  1549. raid_device->volume_type = vol_pg0->VolumeType;
  1550. /* figure out what the underlying devices are by
  1551. * obtaining the device_info bits for the 1st device
  1552. */
  1553. if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  1554. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  1555. vol_pg0->PhysDisk[0].PhysDiskNum))) {
  1556. if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  1557. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  1558. le16_to_cpu(pd_pg0.DevHandle)))) {
  1559. raid_device->device_info =
  1560. le32_to_cpu(sas_device_pg0.DeviceInfo);
  1561. }
  1562. }
  1563. kfree(vol_pg0);
  1564. return 0;
  1565. }
  1566. /**
  1567. * _scsih_enable_tlr - setting TLR flags
  1568. * @ioc: per adapter object
  1569. * @sdev: scsi device struct
  1570. *
  1571. * Enabling Transaction Layer Retries for tape devices when
  1572. * vpd page 0x90 is present
  1573. *
  1574. */
  1575. static void
  1576. _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
  1577. {
  1578. /* only for TAPE */
  1579. if (sdev->type != TYPE_TAPE)
  1580. return;
  1581. if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
  1582. return;
  1583. sas_enable_tlr(sdev);
  1584. sdev_printk(KERN_INFO, sdev, "TLR %s\n",
  1585. sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
  1586. return;
  1587. }
  1588. /**
  1589. * scsih_slave_configure - device configure routine.
  1590. * @sdev: scsi device struct
  1591. *
  1592. * Returns 0 if ok. Any other return is assumed to be an error and
  1593. * the device is ignored.
  1594. */
  1595. static int
  1596. scsih_slave_configure(struct scsi_device *sdev)
  1597. {
  1598. struct Scsi_Host *shost = sdev->host;
  1599. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  1600. struct MPT3SAS_DEVICE *sas_device_priv_data;
  1601. struct MPT3SAS_TARGET *sas_target_priv_data;
  1602. struct _sas_device *sas_device;
  1603. struct _raid_device *raid_device;
  1604. unsigned long flags;
  1605. int qdepth;
  1606. u8 ssp_target = 0;
  1607. char *ds = "";
  1608. char *r_level = "";
  1609. u16 handle, volume_handle = 0;
  1610. u64 volume_wwid = 0;
  1611. qdepth = 1;
  1612. sas_device_priv_data = sdev->hostdata;
  1613. sas_device_priv_data->configured_lun = 1;
  1614. sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
  1615. sas_target_priv_data = sas_device_priv_data->sas_target;
  1616. handle = sas_target_priv_data->handle;
  1617. /* raid volume handling */
  1618. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
  1619. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1620. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  1621. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1622. if (!raid_device) {
  1623. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1624. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  1625. __LINE__, __func__));
  1626. return 1;
  1627. }
  1628. if (_scsih_get_volume_capabilities(ioc, raid_device)) {
  1629. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1630. "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
  1631. __LINE__, __func__));
  1632. return 1;
  1633. }
  1634. /*
  1635. * WARPDRIVE: Initialize the required data for Direct IO
  1636. */
  1637. mpt3sas_init_warpdrive_properties(ioc, raid_device);
  1638. /* RAID Queue Depth Support
  1639. * IS volume = underlying qdepth of drive type, either
  1640. * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
  1641. * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
  1642. */
  1643. if (raid_device->device_info &
  1644. MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1645. qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
  1646. ds = "SSP";
  1647. } else {
  1648. qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
  1649. if (raid_device->device_info &
  1650. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1651. ds = "SATA";
  1652. else
  1653. ds = "STP";
  1654. }
  1655. switch (raid_device->volume_type) {
  1656. case MPI2_RAID_VOL_TYPE_RAID0:
  1657. r_level = "RAID0";
  1658. break;
  1659. case MPI2_RAID_VOL_TYPE_RAID1E:
  1660. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  1661. if (ioc->manu_pg10.OEMIdentifier &&
  1662. (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
  1663. MFG10_GF0_R10_DISPLAY) &&
  1664. !(raid_device->num_pds % 2))
  1665. r_level = "RAID10";
  1666. else
  1667. r_level = "RAID1E";
  1668. break;
  1669. case MPI2_RAID_VOL_TYPE_RAID1:
  1670. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  1671. r_level = "RAID1";
  1672. break;
  1673. case MPI2_RAID_VOL_TYPE_RAID10:
  1674. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  1675. r_level = "RAID10";
  1676. break;
  1677. case MPI2_RAID_VOL_TYPE_UNKNOWN:
  1678. default:
  1679. qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
  1680. r_level = "RAIDX";
  1681. break;
  1682. }
  1683. if (!ioc->hide_ir_msg)
  1684. sdev_printk(KERN_INFO, sdev,
  1685. "%s: handle(0x%04x), wwid(0x%016llx),"
  1686. " pd_count(%d), type(%s)\n",
  1687. r_level, raid_device->handle,
  1688. (unsigned long long)raid_device->wwid,
  1689. raid_device->num_pds, ds);
  1690. if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
  1691. blk_queue_max_hw_sectors(sdev->request_queue,
  1692. MPT3SAS_RAID_MAX_SECTORS);
  1693. sdev_printk(KERN_INFO, sdev,
  1694. "Set queue's max_sector to: %u\n",
  1695. MPT3SAS_RAID_MAX_SECTORS);
  1696. }
  1697. scsih_change_queue_depth(sdev, qdepth);
  1698. /* raid transport support */
  1699. if (!ioc->is_warpdrive)
  1700. _scsih_set_level(ioc, sdev, raid_device->volume_type);
  1701. return 0;
  1702. }
  1703. /* non-raid handling */
  1704. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1705. if (mpt3sas_config_get_volume_handle(ioc, handle,
  1706. &volume_handle)) {
  1707. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1708. "failure at %s:%d/%s()!\n", ioc->name,
  1709. __FILE__, __LINE__, __func__));
  1710. return 1;
  1711. }
  1712. if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
  1713. volume_handle, &volume_wwid)) {
  1714. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1715. "failure at %s:%d/%s()!\n", ioc->name,
  1716. __FILE__, __LINE__, __func__));
  1717. return 1;
  1718. }
  1719. }
  1720. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1721. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  1722. sas_device_priv_data->sas_target->sas_address);
  1723. if (!sas_device) {
  1724. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1725. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1726. "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
  1727. __func__));
  1728. return 1;
  1729. }
  1730. sas_device->volume_handle = volume_handle;
  1731. sas_device->volume_wwid = volume_wwid;
  1732. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1733. qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
  1734. ssp_target = 1;
  1735. if (sas_device->device_info &
  1736. MPI2_SAS_DEVICE_INFO_SEP) {
  1737. sdev_printk(KERN_WARNING, sdev,
  1738. "set ignore_delay_remove for handle(0x%04x)\n",
  1739. sas_device_priv_data->sas_target->handle);
  1740. sas_device_priv_data->ignore_delay_remove = 1;
  1741. ds = "SES";
  1742. } else
  1743. ds = "SSP";
  1744. } else {
  1745. qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
  1746. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  1747. ds = "STP";
  1748. else if (sas_device->device_info &
  1749. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1750. ds = "SATA";
  1751. }
  1752. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
  1753. "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
  1754. ds, handle, (unsigned long long)sas_device->sas_address,
  1755. sas_device->phy, (unsigned long long)sas_device->device_name);
  1756. if (sas_device->enclosure_handle != 0)
  1757. sdev_printk(KERN_INFO, sdev,
  1758. "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
  1759. ds, (unsigned long long)
  1760. sas_device->enclosure_logical_id, sas_device->slot);
  1761. if (sas_device->connector_name[0] != '\0')
  1762. sdev_printk(KERN_INFO, sdev,
  1763. "%s: enclosure level(0x%04x), connector name( %s)\n",
  1764. ds, sas_device->enclosure_level,
  1765. sas_device->connector_name);
  1766. sas_device_put(sas_device);
  1767. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1768. if (!ssp_target)
  1769. _scsih_display_sata_capabilities(ioc, handle, sdev);
  1770. scsih_change_queue_depth(sdev, qdepth);
  1771. if (ssp_target) {
  1772. sas_read_port_mode_page(sdev);
  1773. _scsih_enable_tlr(ioc, sdev);
  1774. }
  1775. return 0;
  1776. }
  1777. /**
  1778. * scsih_bios_param - fetch head, sector, cylinder info for a disk
  1779. * @sdev: scsi device struct
  1780. * @bdev: pointer to block device context
  1781. * @capacity: device size (in 512 byte sectors)
  1782. * @params: three element array to place output:
  1783. * params[0] number of heads (max 255)
  1784. * params[1] number of sectors (max 63)
  1785. * params[2] number of cylinders
  1786. *
  1787. * Return nothing.
  1788. */
  1789. static int
  1790. scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1791. sector_t capacity, int params[])
  1792. {
  1793. int heads;
  1794. int sectors;
  1795. sector_t cylinders;
  1796. ulong dummy;
  1797. heads = 64;
  1798. sectors = 32;
  1799. dummy = heads * sectors;
  1800. cylinders = capacity;
  1801. sector_div(cylinders, dummy);
  1802. /*
  1803. * Handle extended translation size for logical drives
  1804. * > 1Gb
  1805. */
  1806. if ((ulong)capacity >= 0x200000) {
  1807. heads = 255;
  1808. sectors = 63;
  1809. dummy = heads * sectors;
  1810. cylinders = capacity;
  1811. sector_div(cylinders, dummy);
  1812. }
  1813. /* return result */
  1814. params[0] = heads;
  1815. params[1] = sectors;
  1816. params[2] = cylinders;
  1817. return 0;
  1818. }
  1819. /**
  1820. * _scsih_response_code - translation of device response code
  1821. * @ioc: per adapter object
  1822. * @response_code: response code returned by the device
  1823. *
  1824. * Return nothing.
  1825. */
  1826. static void
  1827. _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
  1828. {
  1829. char *desc;
  1830. switch (response_code) {
  1831. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  1832. desc = "task management request completed";
  1833. break;
  1834. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  1835. desc = "invalid frame";
  1836. break;
  1837. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  1838. desc = "task management request not supported";
  1839. break;
  1840. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  1841. desc = "task management request failed";
  1842. break;
  1843. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  1844. desc = "task management request succeeded";
  1845. break;
  1846. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  1847. desc = "invalid lun";
  1848. break;
  1849. case 0xA:
  1850. desc = "overlapped tag attempted";
  1851. break;
  1852. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  1853. desc = "task queued, however not sent to target";
  1854. break;
  1855. default:
  1856. desc = "unknown";
  1857. break;
  1858. }
  1859. pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
  1860. ioc->name, response_code, desc);
  1861. }
  1862. /**
  1863. * _scsih_tm_done - tm completion routine
  1864. * @ioc: per adapter object
  1865. * @smid: system request message index
  1866. * @msix_index: MSIX table index supplied by the OS
  1867. * @reply: reply message frame(lower 32bit addr)
  1868. * Context: none.
  1869. *
  1870. * The callback handler when using scsih_issue_tm.
  1871. *
  1872. * Return 1 meaning mf should be freed from _base_interrupt
  1873. * 0 means the mf is freed from this function.
  1874. */
  1875. static u8
  1876. _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  1877. {
  1878. MPI2DefaultReply_t *mpi_reply;
  1879. if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
  1880. return 1;
  1881. if (ioc->tm_cmds.smid != smid)
  1882. return 1;
  1883. ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
  1884. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  1885. if (mpi_reply) {
  1886. memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  1887. ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
  1888. }
  1889. ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
  1890. complete(&ioc->tm_cmds.done);
  1891. return 1;
  1892. }
  1893. /**
  1894. * mpt3sas_scsih_set_tm_flag - set per target tm_busy
  1895. * @ioc: per adapter object
  1896. * @handle: device handle
  1897. *
  1898. * During taskmangement request, we need to freeze the device queue.
  1899. */
  1900. void
  1901. mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  1902. {
  1903. struct MPT3SAS_DEVICE *sas_device_priv_data;
  1904. struct scsi_device *sdev;
  1905. u8 skip = 0;
  1906. shost_for_each_device(sdev, ioc->shost) {
  1907. if (skip)
  1908. continue;
  1909. sas_device_priv_data = sdev->hostdata;
  1910. if (!sas_device_priv_data)
  1911. continue;
  1912. if (sas_device_priv_data->sas_target->handle == handle) {
  1913. sas_device_priv_data->sas_target->tm_busy = 1;
  1914. skip = 1;
  1915. ioc->ignore_loginfos = 1;
  1916. }
  1917. }
  1918. }
  1919. /**
  1920. * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
  1921. * @ioc: per adapter object
  1922. * @handle: device handle
  1923. *
  1924. * During taskmangement request, we need to freeze the device queue.
  1925. */
  1926. void
  1927. mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  1928. {
  1929. struct MPT3SAS_DEVICE *sas_device_priv_data;
  1930. struct scsi_device *sdev;
  1931. u8 skip = 0;
  1932. shost_for_each_device(sdev, ioc->shost) {
  1933. if (skip)
  1934. continue;
  1935. sas_device_priv_data = sdev->hostdata;
  1936. if (!sas_device_priv_data)
  1937. continue;
  1938. if (sas_device_priv_data->sas_target->handle == handle) {
  1939. sas_device_priv_data->sas_target->tm_busy = 0;
  1940. skip = 1;
  1941. ioc->ignore_loginfos = 0;
  1942. }
  1943. }
  1944. }
  1945. /**
  1946. * mpt3sas_scsih_issue_tm - main routine for sending tm requests
  1947. * @ioc: per adapter struct
  1948. * @device_handle: device handle
  1949. * @channel: the channel assigned by the OS
  1950. * @id: the id assigned by the OS
  1951. * @lun: lun number
  1952. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
  1953. * @smid_task: smid assigned to the task
  1954. * @timeout: timeout in seconds
  1955. * Context: user
  1956. *
  1957. * A generic API for sending task management requests to firmware.
  1958. *
  1959. * The callback index is set inside `ioc->tm_cb_idx`.
  1960. *
  1961. * Return SUCCESS or FAILED.
  1962. */
  1963. int
  1964. mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
  1965. uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
  1966. {
  1967. Mpi2SCSITaskManagementRequest_t *mpi_request;
  1968. Mpi2SCSITaskManagementReply_t *mpi_reply;
  1969. u16 smid = 0;
  1970. u32 ioc_state;
  1971. struct scsiio_tracker *scsi_lookup = NULL;
  1972. int rc;
  1973. u16 msix_task = 0;
  1974. lockdep_assert_held(&ioc->tm_cmds.mutex);
  1975. if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
  1976. pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
  1977. __func__, ioc->name);
  1978. return FAILED;
  1979. }
  1980. if (ioc->shost_recovery || ioc->remove_host ||
  1981. ioc->pci_error_recovery) {
  1982. pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
  1983. __func__, ioc->name);
  1984. return FAILED;
  1985. }
  1986. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  1987. if (ioc_state & MPI2_DOORBELL_USED) {
  1988. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  1989. "unexpected doorbell active!\n", ioc->name));
  1990. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1991. return (!rc) ? SUCCESS : FAILED;
  1992. }
  1993. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  1994. mpt3sas_base_fault_info(ioc, ioc_state &
  1995. MPI2_DOORBELL_DATA_MASK);
  1996. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  1997. return (!rc) ? SUCCESS : FAILED;
  1998. }
  1999. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
  2000. if (!smid) {
  2001. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  2002. ioc->name, __func__);
  2003. return FAILED;
  2004. }
  2005. if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
  2006. scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
  2007. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  2008. "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
  2009. ioc->name, handle, type, smid_task));
  2010. ioc->tm_cmds.status = MPT3_CMD_PENDING;
  2011. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  2012. ioc->tm_cmds.smid = smid;
  2013. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2014. memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
  2015. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2016. mpi_request->DevHandle = cpu_to_le16(handle);
  2017. mpi_request->TaskType = type;
  2018. mpi_request->TaskMID = cpu_to_le16(smid_task);
  2019. int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
  2020. mpt3sas_scsih_set_tm_flag(ioc, handle);
  2021. init_completion(&ioc->tm_cmds.done);
  2022. if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
  2023. (scsi_lookup->msix_io < ioc->reply_queue_count))
  2024. msix_task = scsi_lookup->msix_io;
  2025. else
  2026. msix_task = 0;
  2027. mpt3sas_base_put_smid_hi_priority(ioc, smid, msix_task);
  2028. wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
  2029. if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
  2030. pr_err(MPT3SAS_FMT "%s: timeout\n",
  2031. ioc->name, __func__);
  2032. _debug_dump_mf(mpi_request,
  2033. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  2034. if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
  2035. rc = mpt3sas_base_hard_reset_handler(ioc,
  2036. FORCE_BIG_HAMMER);
  2037. rc = (!rc) ? SUCCESS : FAILED;
  2038. goto out;
  2039. }
  2040. }
  2041. /* sync IRQs in case those were busy during flush. */
  2042. mpt3sas_base_sync_reply_irqs(ioc);
  2043. if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
  2044. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
  2045. mpi_reply = ioc->tm_cmds.reply;
  2046. dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
  2047. "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
  2048. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  2049. le32_to_cpu(mpi_reply->IOCLogInfo),
  2050. le32_to_cpu(mpi_reply->TerminationCount)));
  2051. if (ioc->logging_level & MPT_DEBUG_TM) {
  2052. _scsih_response_code(ioc, mpi_reply->ResponseCode);
  2053. if (mpi_reply->IOCStatus)
  2054. _debug_dump_mf(mpi_request,
  2055. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  2056. }
  2057. }
  2058. switch (type) {
  2059. case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  2060. rc = SUCCESS;
  2061. if (scsi_lookup->scmd == NULL)
  2062. break;
  2063. rc = FAILED;
  2064. break;
  2065. case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  2066. if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
  2067. rc = FAILED;
  2068. else
  2069. rc = SUCCESS;
  2070. break;
  2071. case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
  2072. case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
  2073. if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
  2074. rc = FAILED;
  2075. else
  2076. rc = SUCCESS;
  2077. break;
  2078. case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
  2079. rc = SUCCESS;
  2080. break;
  2081. default:
  2082. rc = FAILED;
  2083. break;
  2084. }
  2085. out:
  2086. mpt3sas_scsih_clear_tm_flag(ioc, handle);
  2087. ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
  2088. return rc;
  2089. }
  2090. int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
  2091. uint channel, uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
  2092. {
  2093. int ret;
  2094. mutex_lock(&ioc->tm_cmds.mutex);
  2095. ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
  2096. smid_task, timeout);
  2097. mutex_unlock(&ioc->tm_cmds.mutex);
  2098. return ret;
  2099. }
  2100. /**
  2101. * _scsih_tm_display_info - displays info about the device
  2102. * @ioc: per adapter struct
  2103. * @scmd: pointer to scsi command object
  2104. *
  2105. * Called by task management callback handlers.
  2106. */
  2107. static void
  2108. _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
  2109. {
  2110. struct scsi_target *starget = scmd->device->sdev_target;
  2111. struct MPT3SAS_TARGET *priv_target = starget->hostdata;
  2112. struct _sas_device *sas_device = NULL;
  2113. unsigned long flags;
  2114. char *device_str = NULL;
  2115. if (!priv_target)
  2116. return;
  2117. if (ioc->hide_ir_msg)
  2118. device_str = "WarpDrive";
  2119. else
  2120. device_str = "volume";
  2121. scsi_print_command(scmd);
  2122. if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  2123. starget_printk(KERN_INFO, starget,
  2124. "%s handle(0x%04x), %s wwid(0x%016llx)\n",
  2125. device_str, priv_target->handle,
  2126. device_str, (unsigned long long)priv_target->sas_address);
  2127. } else {
  2128. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2129. sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
  2130. if (sas_device) {
  2131. if (priv_target->flags &
  2132. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2133. starget_printk(KERN_INFO, starget,
  2134. "volume handle(0x%04x), "
  2135. "volume wwid(0x%016llx)\n",
  2136. sas_device->volume_handle,
  2137. (unsigned long long)sas_device->volume_wwid);
  2138. }
  2139. starget_printk(KERN_INFO, starget,
  2140. "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
  2141. sas_device->handle,
  2142. (unsigned long long)sas_device->sas_address,
  2143. sas_device->phy);
  2144. if (sas_device->enclosure_handle != 0)
  2145. starget_printk(KERN_INFO, starget,
  2146. "enclosure_logical_id(0x%016llx), slot(%d)\n",
  2147. (unsigned long long)
  2148. sas_device->enclosure_logical_id,
  2149. sas_device->slot);
  2150. if (sas_device->connector_name[0] != '\0')
  2151. starget_printk(KERN_INFO, starget,
  2152. "enclosure level(0x%04x),connector name(%s)\n",
  2153. sas_device->enclosure_level,
  2154. sas_device->connector_name);
  2155. sas_device_put(sas_device);
  2156. }
  2157. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2158. }
  2159. }
  2160. /**
  2161. * scsih_abort - eh threads main abort routine
  2162. * @scmd: pointer to scsi command object
  2163. *
  2164. * Returns SUCCESS if command aborted else FAILED
  2165. */
  2166. static int
  2167. scsih_abort(struct scsi_cmnd *scmd)
  2168. {
  2169. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2170. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2171. u16 smid;
  2172. u16 handle;
  2173. int r;
  2174. sdev_printk(KERN_INFO, scmd->device,
  2175. "attempting task abort! scmd(%p)\n", scmd);
  2176. _scsih_tm_display_info(ioc, scmd);
  2177. sas_device_priv_data = scmd->device->hostdata;
  2178. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2179. sdev_printk(KERN_INFO, scmd->device,
  2180. "device been deleted! scmd(%p)\n", scmd);
  2181. scmd->result = DID_NO_CONNECT << 16;
  2182. scmd->scsi_done(scmd);
  2183. r = SUCCESS;
  2184. goto out;
  2185. }
  2186. /* search for the command */
  2187. smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
  2188. if (!smid) {
  2189. scmd->result = DID_RESET << 16;
  2190. r = SUCCESS;
  2191. goto out;
  2192. }
  2193. /* for hidden raid components and volumes this is not supported */
  2194. if (sas_device_priv_data->sas_target->flags &
  2195. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  2196. sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  2197. scmd->result = DID_RESET << 16;
  2198. r = FAILED;
  2199. goto out;
  2200. }
  2201. mpt3sas_halt_firmware(ioc);
  2202. handle = sas_device_priv_data->sas_target->handle;
  2203. r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
  2204. scmd->device->id, scmd->device->lun,
  2205. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
  2206. out:
  2207. sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
  2208. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2209. return r;
  2210. }
  2211. /**
  2212. * scsih_dev_reset - eh threads main device reset routine
  2213. * @scmd: pointer to scsi command object
  2214. *
  2215. * Returns SUCCESS if command aborted else FAILED
  2216. */
  2217. static int
  2218. scsih_dev_reset(struct scsi_cmnd *scmd)
  2219. {
  2220. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2221. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2222. struct _sas_device *sas_device = NULL;
  2223. u16 handle;
  2224. int r;
  2225. struct scsi_target *starget = scmd->device->sdev_target;
  2226. struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
  2227. sdev_printk(KERN_INFO, scmd->device,
  2228. "attempting device reset! scmd(%p)\n", scmd);
  2229. _scsih_tm_display_info(ioc, scmd);
  2230. sas_device_priv_data = scmd->device->hostdata;
  2231. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2232. sdev_printk(KERN_INFO, scmd->device,
  2233. "device been deleted! scmd(%p)\n", scmd);
  2234. scmd->result = DID_NO_CONNECT << 16;
  2235. scmd->scsi_done(scmd);
  2236. r = SUCCESS;
  2237. goto out;
  2238. }
  2239. /* for hidden raid components obtain the volume_handle */
  2240. handle = 0;
  2241. if (sas_device_priv_data->sas_target->flags &
  2242. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2243. sas_device = mpt3sas_get_sdev_from_target(ioc,
  2244. target_priv_data);
  2245. if (sas_device)
  2246. handle = sas_device->volume_handle;
  2247. } else
  2248. handle = sas_device_priv_data->sas_target->handle;
  2249. if (!handle) {
  2250. scmd->result = DID_RESET << 16;
  2251. r = FAILED;
  2252. goto out;
  2253. }
  2254. r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
  2255. scmd->device->id, scmd->device->lun,
  2256. MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30);
  2257. out:
  2258. sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
  2259. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2260. if (sas_device)
  2261. sas_device_put(sas_device);
  2262. return r;
  2263. }
  2264. /**
  2265. * scsih_target_reset - eh threads main target reset routine
  2266. * @scmd: pointer to scsi command object
  2267. *
  2268. * Returns SUCCESS if command aborted else FAILED
  2269. */
  2270. static int
  2271. scsih_target_reset(struct scsi_cmnd *scmd)
  2272. {
  2273. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2274. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2275. struct _sas_device *sas_device = NULL;
  2276. u16 handle;
  2277. int r;
  2278. struct scsi_target *starget = scmd->device->sdev_target;
  2279. struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
  2280. starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
  2281. scmd);
  2282. _scsih_tm_display_info(ioc, scmd);
  2283. sas_device_priv_data = scmd->device->hostdata;
  2284. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2285. starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
  2286. scmd);
  2287. scmd->result = DID_NO_CONNECT << 16;
  2288. scmd->scsi_done(scmd);
  2289. r = SUCCESS;
  2290. goto out;
  2291. }
  2292. /* for hidden raid components obtain the volume_handle */
  2293. handle = 0;
  2294. if (sas_device_priv_data->sas_target->flags &
  2295. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2296. sas_device = mpt3sas_get_sdev_from_target(ioc,
  2297. target_priv_data);
  2298. if (sas_device)
  2299. handle = sas_device->volume_handle;
  2300. } else
  2301. handle = sas_device_priv_data->sas_target->handle;
  2302. if (!handle) {
  2303. scmd->result = DID_RESET << 16;
  2304. r = FAILED;
  2305. goto out;
  2306. }
  2307. r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
  2308. scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  2309. 30);
  2310. out:
  2311. starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
  2312. ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2313. if (sas_device)
  2314. sas_device_put(sas_device);
  2315. return r;
  2316. }
  2317. /**
  2318. * scsih_host_reset - eh threads main host reset routine
  2319. * @scmd: pointer to scsi command object
  2320. *
  2321. * Returns SUCCESS if command aborted else FAILED
  2322. */
  2323. static int
  2324. scsih_host_reset(struct scsi_cmnd *scmd)
  2325. {
  2326. struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2327. int r, retval;
  2328. pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
  2329. ioc->name, scmd);
  2330. scsi_print_command(scmd);
  2331. if (ioc->is_driver_loading) {
  2332. pr_info(MPT3SAS_FMT "Blocking the host reset\n",
  2333. ioc->name);
  2334. r = FAILED;
  2335. goto out;
  2336. }
  2337. retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  2338. r = (retval < 0) ? FAILED : SUCCESS;
  2339. out:
  2340. pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
  2341. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2342. return r;
  2343. }
  2344. /**
  2345. * _scsih_fw_event_add - insert and queue up fw_event
  2346. * @ioc: per adapter object
  2347. * @fw_event: object describing the event
  2348. * Context: This function will acquire ioc->fw_event_lock.
  2349. *
  2350. * This adds the firmware event object into link list, then queues it up to
  2351. * be processed from user context.
  2352. *
  2353. * Return nothing.
  2354. */
  2355. static void
  2356. _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  2357. {
  2358. unsigned long flags;
  2359. if (ioc->firmware_event_thread == NULL)
  2360. return;
  2361. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2362. fw_event_work_get(fw_event);
  2363. INIT_LIST_HEAD(&fw_event->list);
  2364. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  2365. INIT_WORK(&fw_event->work, _firmware_event_work);
  2366. fw_event_work_get(fw_event);
  2367. queue_work(ioc->firmware_event_thread, &fw_event->work);
  2368. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2369. }
  2370. /**
  2371. * _scsih_fw_event_del_from_list - delete fw_event from the list
  2372. * @ioc: per adapter object
  2373. * @fw_event: object describing the event
  2374. * Context: This function will acquire ioc->fw_event_lock.
  2375. *
  2376. * If the fw_event is on the fw_event_list, remove it and do a put.
  2377. *
  2378. * Return nothing.
  2379. */
  2380. static void
  2381. _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
  2382. *fw_event)
  2383. {
  2384. unsigned long flags;
  2385. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2386. if (!list_empty(&fw_event->list)) {
  2387. list_del_init(&fw_event->list);
  2388. fw_event_work_put(fw_event);
  2389. }
  2390. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2391. }
  2392. /**
  2393. * mpt3sas_send_trigger_data_event - send event for processing trigger data
  2394. * @ioc: per adapter object
  2395. * @event_data: trigger event data
  2396. *
  2397. * Return nothing.
  2398. */
  2399. void
  2400. mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
  2401. struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
  2402. {
  2403. struct fw_event_work *fw_event;
  2404. u16 sz;
  2405. if (ioc->is_driver_loading)
  2406. return;
  2407. sz = sizeof(*event_data);
  2408. fw_event = alloc_fw_event_work(sz);
  2409. if (!fw_event)
  2410. return;
  2411. fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
  2412. fw_event->ioc = ioc;
  2413. memcpy(fw_event->event_data, event_data, sizeof(*event_data));
  2414. _scsih_fw_event_add(ioc, fw_event);
  2415. fw_event_work_put(fw_event);
  2416. }
  2417. /**
  2418. * _scsih_error_recovery_delete_devices - remove devices not responding
  2419. * @ioc: per adapter object
  2420. *
  2421. * Return nothing.
  2422. */
  2423. static void
  2424. _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
  2425. {
  2426. struct fw_event_work *fw_event;
  2427. if (ioc->is_driver_loading)
  2428. return;
  2429. fw_event = alloc_fw_event_work(0);
  2430. if (!fw_event)
  2431. return;
  2432. fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
  2433. fw_event->ioc = ioc;
  2434. _scsih_fw_event_add(ioc, fw_event);
  2435. fw_event_work_put(fw_event);
  2436. }
  2437. /**
  2438. * mpt3sas_port_enable_complete - port enable completed (fake event)
  2439. * @ioc: per adapter object
  2440. *
  2441. * Return nothing.
  2442. */
  2443. void
  2444. mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
  2445. {
  2446. struct fw_event_work *fw_event;
  2447. fw_event = alloc_fw_event_work(0);
  2448. if (!fw_event)
  2449. return;
  2450. fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
  2451. fw_event->ioc = ioc;
  2452. _scsih_fw_event_add(ioc, fw_event);
  2453. fw_event_work_put(fw_event);
  2454. }
  2455. static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
  2456. {
  2457. unsigned long flags;
  2458. struct fw_event_work *fw_event = NULL;
  2459. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2460. if (!list_empty(&ioc->fw_event_list)) {
  2461. fw_event = list_first_entry(&ioc->fw_event_list,
  2462. struct fw_event_work, list);
  2463. list_del_init(&fw_event->list);
  2464. }
  2465. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2466. return fw_event;
  2467. }
  2468. /**
  2469. * _scsih_fw_event_cleanup_queue - cleanup event queue
  2470. * @ioc: per adapter object
  2471. *
  2472. * Walk the firmware event queue, either killing timers, or waiting
  2473. * for outstanding events to complete
  2474. *
  2475. * Return nothing.
  2476. */
  2477. static void
  2478. _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
  2479. {
  2480. struct fw_event_work *fw_event;
  2481. if (list_empty(&ioc->fw_event_list) ||
  2482. !ioc->firmware_event_thread || in_interrupt())
  2483. return;
  2484. while ((fw_event = dequeue_next_fw_event(ioc))) {
  2485. /*
  2486. * Wait on the fw_event to complete. If this returns 1, then
  2487. * the event was never executed, and we need a put for the
  2488. * reference the work had on the fw_event.
  2489. *
  2490. * If it did execute, we wait for it to finish, and the put will
  2491. * happen from _firmware_event_work()
  2492. */
  2493. if (cancel_work_sync(&fw_event->work))
  2494. fw_event_work_put(fw_event);
  2495. fw_event_work_put(fw_event);
  2496. }
  2497. }
  2498. /**
  2499. * _scsih_internal_device_block - block the sdev device
  2500. * @sdev: per device object
  2501. * @sas_device_priv_data : per device driver private data
  2502. *
  2503. * make sure device is blocked without error, if not
  2504. * print an error
  2505. */
  2506. static void
  2507. _scsih_internal_device_block(struct scsi_device *sdev,
  2508. struct MPT3SAS_DEVICE *sas_device_priv_data)
  2509. {
  2510. int r = 0;
  2511. sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
  2512. sas_device_priv_data->sas_target->handle);
  2513. sas_device_priv_data->block = 1;
  2514. r = scsi_internal_device_block(sdev);
  2515. if (r == -EINVAL)
  2516. sdev_printk(KERN_WARNING, sdev,
  2517. "device_block failed with return(%d) for handle(0x%04x)\n",
  2518. sas_device_priv_data->sas_target->handle, r);
  2519. }
  2520. /**
  2521. * _scsih_internal_device_unblock - unblock the sdev device
  2522. * @sdev: per device object
  2523. * @sas_device_priv_data : per device driver private data
  2524. * make sure device is unblocked without error, if not retry
  2525. * by blocking and then unblocking
  2526. */
  2527. static void
  2528. _scsih_internal_device_unblock(struct scsi_device *sdev,
  2529. struct MPT3SAS_DEVICE *sas_device_priv_data)
  2530. {
  2531. int r = 0;
  2532. sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
  2533. "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
  2534. sas_device_priv_data->block = 0;
  2535. r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
  2536. if (r == -EINVAL) {
  2537. /* The device has been set to SDEV_RUNNING by SD layer during
  2538. * device addition but the request queue is still stopped by
  2539. * our earlier block call. We need to perform a block again
  2540. * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
  2541. sdev_printk(KERN_WARNING, sdev,
  2542. "device_unblock failed with return(%d) for handle(0x%04x) "
  2543. "performing a block followed by an unblock\n",
  2544. sas_device_priv_data->sas_target->handle, r);
  2545. sas_device_priv_data->block = 1;
  2546. r = scsi_internal_device_block(sdev);
  2547. if (r)
  2548. sdev_printk(KERN_WARNING, sdev, "retried device_block "
  2549. "failed with return(%d) for handle(0x%04x)\n",
  2550. sas_device_priv_data->sas_target->handle, r);
  2551. sas_device_priv_data->block = 0;
  2552. r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
  2553. if (r)
  2554. sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
  2555. " failed with return(%d) for handle(0x%04x)\n",
  2556. sas_device_priv_data->sas_target->handle, r);
  2557. }
  2558. }
  2559. /**
  2560. * _scsih_ublock_io_all_device - unblock every device
  2561. * @ioc: per adapter object
  2562. *
  2563. * change the device state from block to running
  2564. */
  2565. static void
  2566. _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
  2567. {
  2568. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2569. struct scsi_device *sdev;
  2570. shost_for_each_device(sdev, ioc->shost) {
  2571. sas_device_priv_data = sdev->hostdata;
  2572. if (!sas_device_priv_data)
  2573. continue;
  2574. if (!sas_device_priv_data->block)
  2575. continue;
  2576. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2577. "device_running, handle(0x%04x)\n",
  2578. sas_device_priv_data->sas_target->handle));
  2579. _scsih_internal_device_unblock(sdev, sas_device_priv_data);
  2580. }
  2581. }
  2582. /**
  2583. * _scsih_ublock_io_device - prepare device to be deleted
  2584. * @ioc: per adapter object
  2585. * @sas_addr: sas address
  2586. *
  2587. * unblock then put device in offline state
  2588. */
  2589. static void
  2590. _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
  2591. {
  2592. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2593. struct scsi_device *sdev;
  2594. shost_for_each_device(sdev, ioc->shost) {
  2595. sas_device_priv_data = sdev->hostdata;
  2596. if (!sas_device_priv_data)
  2597. continue;
  2598. if (sas_device_priv_data->sas_target->sas_address
  2599. != sas_address)
  2600. continue;
  2601. if (sas_device_priv_data->block)
  2602. _scsih_internal_device_unblock(sdev,
  2603. sas_device_priv_data);
  2604. }
  2605. }
  2606. /**
  2607. * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
  2608. * @ioc: per adapter object
  2609. * @handle: device handle
  2610. *
  2611. * During device pull we need to appropiately set the sdev state.
  2612. */
  2613. static void
  2614. _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
  2615. {
  2616. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2617. struct scsi_device *sdev;
  2618. shost_for_each_device(sdev, ioc->shost) {
  2619. sas_device_priv_data = sdev->hostdata;
  2620. if (!sas_device_priv_data)
  2621. continue;
  2622. if (sas_device_priv_data->block)
  2623. continue;
  2624. if (sas_device_priv_data->ignore_delay_remove) {
  2625. sdev_printk(KERN_INFO, sdev,
  2626. "%s skip device_block for SES handle(0x%04x)\n",
  2627. __func__, sas_device_priv_data->sas_target->handle);
  2628. continue;
  2629. }
  2630. _scsih_internal_device_block(sdev, sas_device_priv_data);
  2631. }
  2632. }
  2633. /**
  2634. * _scsih_block_io_device - set the device state to SDEV_BLOCK
  2635. * @ioc: per adapter object
  2636. * @handle: device handle
  2637. *
  2638. * During device pull we need to appropiately set the sdev state.
  2639. */
  2640. static void
  2641. _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  2642. {
  2643. struct MPT3SAS_DEVICE *sas_device_priv_data;
  2644. struct scsi_device *sdev;
  2645. struct _sas_device *sas_device;
  2646. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  2647. if (!sas_device)
  2648. return;
  2649. shost_for_each_device(sdev, ioc->shost) {
  2650. sas_device_priv_data = sdev->hostdata;
  2651. if (!sas_device_priv_data)
  2652. continue;
  2653. if (sas_device_priv_data->sas_target->handle != handle)
  2654. continue;
  2655. if (sas_device_priv_data->block)
  2656. continue;
  2657. if (sas_device->pend_sas_rphy_add)
  2658. continue;
  2659. if (sas_device_priv_data->ignore_delay_remove) {
  2660. sdev_printk(KERN_INFO, sdev,
  2661. "%s skip device_block for SES handle(0x%04x)\n",
  2662. __func__, sas_device_priv_data->sas_target->handle);
  2663. continue;
  2664. }
  2665. _scsih_internal_device_block(sdev, sas_device_priv_data);
  2666. }
  2667. sas_device_put(sas_device);
  2668. }
  2669. /**
  2670. * _scsih_block_io_to_children_attached_to_ex
  2671. * @ioc: per adapter object
  2672. * @sas_expander: the sas_device object
  2673. *
  2674. * This routine set sdev state to SDEV_BLOCK for all devices
  2675. * attached to this expander. This function called when expander is
  2676. * pulled.
  2677. */
  2678. static void
  2679. _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
  2680. struct _sas_node *sas_expander)
  2681. {
  2682. struct _sas_port *mpt3sas_port;
  2683. struct _sas_device *sas_device;
  2684. struct _sas_node *expander_sibling;
  2685. unsigned long flags;
  2686. if (!sas_expander)
  2687. return;
  2688. list_for_each_entry(mpt3sas_port,
  2689. &sas_expander->sas_port_list, port_list) {
  2690. if (mpt3sas_port->remote_identify.device_type ==
  2691. SAS_END_DEVICE) {
  2692. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2693. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  2694. mpt3sas_port->remote_identify.sas_address);
  2695. if (sas_device) {
  2696. set_bit(sas_device->handle,
  2697. ioc->blocking_handles);
  2698. sas_device_put(sas_device);
  2699. }
  2700. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2701. }
  2702. }
  2703. list_for_each_entry(mpt3sas_port,
  2704. &sas_expander->sas_port_list, port_list) {
  2705. if (mpt3sas_port->remote_identify.device_type ==
  2706. SAS_EDGE_EXPANDER_DEVICE ||
  2707. mpt3sas_port->remote_identify.device_type ==
  2708. SAS_FANOUT_EXPANDER_DEVICE) {
  2709. expander_sibling =
  2710. mpt3sas_scsih_expander_find_by_sas_address(
  2711. ioc, mpt3sas_port->remote_identify.sas_address);
  2712. _scsih_block_io_to_children_attached_to_ex(ioc,
  2713. expander_sibling);
  2714. }
  2715. }
  2716. }
  2717. /**
  2718. * _scsih_block_io_to_children_attached_directly
  2719. * @ioc: per adapter object
  2720. * @event_data: topology change event data
  2721. *
  2722. * This routine set sdev state to SDEV_BLOCK for all devices
  2723. * direct attached during device pull.
  2724. */
  2725. static void
  2726. _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
  2727. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2728. {
  2729. int i;
  2730. u16 handle;
  2731. u16 reason_code;
  2732. for (i = 0; i < event_data->NumEntries; i++) {
  2733. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2734. if (!handle)
  2735. continue;
  2736. reason_code = event_data->PHY[i].PhyStatus &
  2737. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2738. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
  2739. _scsih_block_io_device(ioc, handle);
  2740. }
  2741. }
  2742. /**
  2743. * _scsih_tm_tr_send - send task management request
  2744. * @ioc: per adapter object
  2745. * @handle: device handle
  2746. * Context: interrupt time.
  2747. *
  2748. * This code is to initiate the device removal handshake protocol
  2749. * with controller firmware. This function will issue target reset
  2750. * using high priority request queue. It will send a sas iounit
  2751. * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
  2752. *
  2753. * This is designed to send muliple task management request at the same
  2754. * time to the fifo. If the fifo is full, we will append the request,
  2755. * and process it in a future completion.
  2756. */
  2757. static void
  2758. _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  2759. {
  2760. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2761. u16 smid;
  2762. struct _sas_device *sas_device = NULL;
  2763. struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
  2764. u64 sas_address = 0;
  2765. unsigned long flags;
  2766. struct _tr_list *delayed_tr;
  2767. u32 ioc_state;
  2768. if (ioc->remove_host) {
  2769. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2770. "%s: host has been removed: handle(0x%04x)\n",
  2771. __func__, ioc->name, handle));
  2772. return;
  2773. } else if (ioc->pci_error_recovery) {
  2774. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2775. "%s: host in pci error recovery: handle(0x%04x)\n",
  2776. __func__, ioc->name,
  2777. handle));
  2778. return;
  2779. }
  2780. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  2781. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2782. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2783. "%s: host is not operational: handle(0x%04x)\n",
  2784. __func__, ioc->name,
  2785. handle));
  2786. return;
  2787. }
  2788. /* if PD, then return */
  2789. if (test_bit(handle, ioc->pd_handles))
  2790. return;
  2791. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2792. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  2793. if (sas_device && sas_device->starget &&
  2794. sas_device->starget->hostdata) {
  2795. sas_target_priv_data = sas_device->starget->hostdata;
  2796. sas_target_priv_data->deleted = 1;
  2797. sas_address = sas_device->sas_address;
  2798. }
  2799. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2800. if (sas_target_priv_data) {
  2801. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2802. "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
  2803. ioc->name, handle,
  2804. (unsigned long long)sas_address));
  2805. if (sas_device->enclosure_handle != 0)
  2806. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2807. "setting delete flag:enclosure logical id(0x%016llx),"
  2808. " slot(%d)\n", ioc->name, (unsigned long long)
  2809. sas_device->enclosure_logical_id,
  2810. sas_device->slot));
  2811. if (sas_device->connector_name[0] != '\0')
  2812. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2813. "setting delete flag: enclosure level(0x%04x),"
  2814. " connector name( %s)\n", ioc->name,
  2815. sas_device->enclosure_level,
  2816. sas_device->connector_name));
  2817. _scsih_ublock_io_device(ioc, sas_address);
  2818. sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
  2819. }
  2820. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
  2821. if (!smid) {
  2822. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  2823. if (!delayed_tr)
  2824. goto out;
  2825. INIT_LIST_HEAD(&delayed_tr->list);
  2826. delayed_tr->handle = handle;
  2827. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  2828. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2829. "DELAYED:tr:handle(0x%04x), (open)\n",
  2830. ioc->name, handle));
  2831. goto out;
  2832. }
  2833. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2834. "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  2835. ioc->name, handle, smid,
  2836. ioc->tm_tr_cb_idx));
  2837. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  2838. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2839. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2840. mpi_request->DevHandle = cpu_to_le16(handle);
  2841. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  2842. mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
  2843. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
  2844. out:
  2845. if (sas_device)
  2846. sas_device_put(sas_device);
  2847. }
  2848. /**
  2849. * _scsih_tm_tr_complete -
  2850. * @ioc: per adapter object
  2851. * @smid: system request message index
  2852. * @msix_index: MSIX table index supplied by the OS
  2853. * @reply: reply message frame(lower 32bit addr)
  2854. * Context: interrupt time.
  2855. *
  2856. * This is the target reset completion routine.
  2857. * This code is part of the code to initiate the device removal
  2858. * handshake protocol with controller firmware.
  2859. * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
  2860. *
  2861. * Return 1 meaning mf should be freed from _base_interrupt
  2862. * 0 means the mf is freed from this function.
  2863. */
  2864. static u8
  2865. _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  2866. u32 reply)
  2867. {
  2868. u16 handle;
  2869. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  2870. Mpi2SCSITaskManagementReply_t *mpi_reply =
  2871. mpt3sas_base_get_reply_virt_addr(ioc, reply);
  2872. Mpi2SasIoUnitControlRequest_t *mpi_request;
  2873. u16 smid_sas_ctrl;
  2874. u32 ioc_state;
  2875. struct _sc_list *delayed_sc;
  2876. if (ioc->remove_host) {
  2877. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2878. "%s: host has been removed\n", __func__, ioc->name));
  2879. return 1;
  2880. } else if (ioc->pci_error_recovery) {
  2881. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2882. "%s: host in pci error recovery\n", __func__,
  2883. ioc->name));
  2884. return 1;
  2885. }
  2886. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  2887. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2888. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2889. "%s: host is not operational\n", __func__, ioc->name));
  2890. return 1;
  2891. }
  2892. if (unlikely(!mpi_reply)) {
  2893. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  2894. ioc->name, __FILE__, __LINE__, __func__);
  2895. return 1;
  2896. }
  2897. mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
  2898. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  2899. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  2900. dewtprintk(ioc, pr_err(MPT3SAS_FMT
  2901. "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
  2902. ioc->name, handle,
  2903. le16_to_cpu(mpi_reply->DevHandle), smid));
  2904. return 0;
  2905. }
  2906. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
  2907. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2908. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  2909. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  2910. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  2911. le32_to_cpu(mpi_reply->IOCLogInfo),
  2912. le32_to_cpu(mpi_reply->TerminationCount)));
  2913. smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
  2914. if (!smid_sas_ctrl) {
  2915. delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
  2916. if (!delayed_sc)
  2917. return _scsih_check_for_pending_tm(ioc, smid);
  2918. INIT_LIST_HEAD(&delayed_sc->list);
  2919. delayed_sc->handle = mpi_request_tm->DevHandle;
  2920. list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
  2921. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2922. "DELAYED:sc:handle(0x%04x), (open)\n",
  2923. ioc->name, handle));
  2924. return _scsih_check_for_pending_tm(ioc, smid);
  2925. }
  2926. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2927. "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  2928. ioc->name, handle, smid_sas_ctrl,
  2929. ioc->tm_sas_control_cb_idx));
  2930. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
  2931. memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  2932. mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  2933. mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  2934. mpi_request->DevHandle = mpi_request_tm->DevHandle;
  2935. mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
  2936. return _scsih_check_for_pending_tm(ioc, smid);
  2937. }
  2938. /**
  2939. * _scsih_sas_control_complete - completion routine
  2940. * @ioc: per adapter object
  2941. * @smid: system request message index
  2942. * @msix_index: MSIX table index supplied by the OS
  2943. * @reply: reply message frame(lower 32bit addr)
  2944. * Context: interrupt time.
  2945. *
  2946. * This is the sas iounit control completion routine.
  2947. * This code is part of the code to initiate the device removal
  2948. * handshake protocol with controller firmware.
  2949. *
  2950. * Return 1 meaning mf should be freed from _base_interrupt
  2951. * 0 means the mf is freed from this function.
  2952. */
  2953. static u8
  2954. _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  2955. u8 msix_index, u32 reply)
  2956. {
  2957. Mpi2SasIoUnitControlReply_t *mpi_reply =
  2958. mpt3sas_base_get_reply_virt_addr(ioc, reply);
  2959. if (likely(mpi_reply)) {
  2960. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2961. "sc_complete:handle(0x%04x), (open) "
  2962. "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  2963. ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
  2964. le16_to_cpu(mpi_reply->IOCStatus),
  2965. le32_to_cpu(mpi_reply->IOCLogInfo)));
  2966. } else {
  2967. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  2968. ioc->name, __FILE__, __LINE__, __func__);
  2969. }
  2970. return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
  2971. }
  2972. /**
  2973. * _scsih_tm_tr_volume_send - send target reset request for volumes
  2974. * @ioc: per adapter object
  2975. * @handle: device handle
  2976. * Context: interrupt time.
  2977. *
  2978. * This is designed to send muliple task management request at the same
  2979. * time to the fifo. If the fifo is full, we will append the request,
  2980. * and process it in a future completion.
  2981. */
  2982. static void
  2983. _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  2984. {
  2985. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2986. u16 smid;
  2987. struct _tr_list *delayed_tr;
  2988. if (ioc->shost_recovery || ioc->remove_host ||
  2989. ioc->pci_error_recovery) {
  2990. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  2991. "%s: host reset in progress!\n",
  2992. __func__, ioc->name));
  2993. return;
  2994. }
  2995. smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
  2996. if (!smid) {
  2997. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  2998. if (!delayed_tr)
  2999. return;
  3000. INIT_LIST_HEAD(&delayed_tr->list);
  3001. delayed_tr->handle = handle;
  3002. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
  3003. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3004. "DELAYED:tr:handle(0x%04x), (open)\n",
  3005. ioc->name, handle));
  3006. return;
  3007. }
  3008. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3009. "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  3010. ioc->name, handle, smid,
  3011. ioc->tm_tr_volume_cb_idx));
  3012. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3013. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  3014. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  3015. mpi_request->DevHandle = cpu_to_le16(handle);
  3016. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  3017. mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
  3018. }
  3019. /**
  3020. * _scsih_tm_volume_tr_complete - target reset completion
  3021. * @ioc: per adapter object
  3022. * @smid: system request message index
  3023. * @msix_index: MSIX table index supplied by the OS
  3024. * @reply: reply message frame(lower 32bit addr)
  3025. * Context: interrupt time.
  3026. *
  3027. * Return 1 meaning mf should be freed from _base_interrupt
  3028. * 0 means the mf is freed from this function.
  3029. */
  3030. static u8
  3031. _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  3032. u8 msix_index, u32 reply)
  3033. {
  3034. u16 handle;
  3035. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  3036. Mpi2SCSITaskManagementReply_t *mpi_reply =
  3037. mpt3sas_base_get_reply_virt_addr(ioc, reply);
  3038. if (ioc->shost_recovery || ioc->remove_host ||
  3039. ioc->pci_error_recovery) {
  3040. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3041. "%s: host reset in progress!\n",
  3042. __func__, ioc->name));
  3043. return 1;
  3044. }
  3045. if (unlikely(!mpi_reply)) {
  3046. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  3047. ioc->name, __FILE__, __LINE__, __func__);
  3048. return 1;
  3049. }
  3050. mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
  3051. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  3052. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  3053. dewtprintk(ioc, pr_err(MPT3SAS_FMT
  3054. "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
  3055. ioc->name, handle,
  3056. le16_to_cpu(mpi_reply->DevHandle), smid));
  3057. return 0;
  3058. }
  3059. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3060. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  3061. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  3062. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  3063. le32_to_cpu(mpi_reply->IOCLogInfo),
  3064. le32_to_cpu(mpi_reply->TerminationCount)));
  3065. return _scsih_check_for_pending_tm(ioc, smid);
  3066. }
  3067. /**
  3068. * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
  3069. * @ioc: per adapter object
  3070. * @smid: system request message index
  3071. * @event: Event ID
  3072. * @event_context: used to track events uniquely
  3073. *
  3074. * Context - processed in interrupt context.
  3075. */
  3076. static void
  3077. _scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
  3078. u32 event_context)
  3079. {
  3080. Mpi2EventAckRequest_t *ack_request;
  3081. int i = smid - ioc->internal_smid;
  3082. unsigned long flags;
  3083. /* Without releasing the smid just update the
  3084. * call back index and reuse the same smid for
  3085. * processing this delayed request
  3086. */
  3087. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3088. ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
  3089. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3090. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3091. "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
  3092. ioc->name, le16_to_cpu(event), smid,
  3093. ioc->base_cb_idx));
  3094. ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3095. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  3096. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  3097. ack_request->Event = event;
  3098. ack_request->EventContext = event_context;
  3099. ack_request->VF_ID = 0; /* TODO */
  3100. ack_request->VP_ID = 0;
  3101. mpt3sas_base_put_smid_default(ioc, smid);
  3102. }
  3103. /**
  3104. * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
  3105. * sas_io_unit_ctrl messages
  3106. * @ioc: per adapter object
  3107. * @smid: system request message index
  3108. * @handle: device handle
  3109. *
  3110. * Context - processed in interrupt context.
  3111. */
  3112. static void
  3113. _scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
  3114. u16 smid, u16 handle)
  3115. {
  3116. Mpi2SasIoUnitControlRequest_t *mpi_request;
  3117. u32 ioc_state;
  3118. int i = smid - ioc->internal_smid;
  3119. unsigned long flags;
  3120. if (ioc->remove_host) {
  3121. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3122. "%s: host has been removed\n",
  3123. __func__, ioc->name));
  3124. return;
  3125. } else if (ioc->pci_error_recovery) {
  3126. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3127. "%s: host in pci error recovery\n",
  3128. __func__, ioc->name));
  3129. return;
  3130. }
  3131. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3132. if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3133. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3134. "%s: host is not operational\n",
  3135. __func__, ioc->name));
  3136. return;
  3137. }
  3138. /* Without releasing the smid just update the
  3139. * call back index and reuse the same smid for
  3140. * processing this delayed request
  3141. */
  3142. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3143. ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
  3144. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3145. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3146. "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
  3147. ioc->name, le16_to_cpu(handle), smid,
  3148. ioc->tm_sas_control_cb_idx));
  3149. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3150. memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3151. mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3152. mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3153. mpi_request->DevHandle = handle;
  3154. mpt3sas_base_put_smid_default(ioc, smid);
  3155. }
  3156. /**
  3157. * _scsih_check_for_pending_internal_cmds - check for pending internal messages
  3158. * @ioc: per adapter object
  3159. * @smid: system request message index
  3160. *
  3161. * Context: Executed in interrupt context
  3162. *
  3163. * This will check delayed internal messages list, and process the
  3164. * next request.
  3165. *
  3166. * Return 1 meaning mf should be freed from _base_interrupt
  3167. * 0 means the mf is freed from this function.
  3168. */
  3169. u8
  3170. mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3171. {
  3172. struct _sc_list *delayed_sc;
  3173. struct _event_ack_list *delayed_event_ack;
  3174. if (!list_empty(&ioc->delayed_event_ack_list)) {
  3175. delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
  3176. struct _event_ack_list, list);
  3177. _scsih_issue_delayed_event_ack(ioc, smid,
  3178. delayed_event_ack->Event, delayed_event_ack->EventContext);
  3179. list_del(&delayed_event_ack->list);
  3180. kfree(delayed_event_ack);
  3181. return 0;
  3182. }
  3183. if (!list_empty(&ioc->delayed_sc_list)) {
  3184. delayed_sc = list_entry(ioc->delayed_sc_list.next,
  3185. struct _sc_list, list);
  3186. _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
  3187. delayed_sc->handle);
  3188. list_del(&delayed_sc->list);
  3189. kfree(delayed_sc);
  3190. return 0;
  3191. }
  3192. return 1;
  3193. }
  3194. /**
  3195. * _scsih_check_for_pending_tm - check for pending task management
  3196. * @ioc: per adapter object
  3197. * @smid: system request message index
  3198. *
  3199. * This will check delayed target reset list, and feed the
  3200. * next reqeust.
  3201. *
  3202. * Return 1 meaning mf should be freed from _base_interrupt
  3203. * 0 means the mf is freed from this function.
  3204. */
  3205. static u8
  3206. _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  3207. {
  3208. struct _tr_list *delayed_tr;
  3209. if (!list_empty(&ioc->delayed_tr_volume_list)) {
  3210. delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
  3211. struct _tr_list, list);
  3212. mpt3sas_base_free_smid(ioc, smid);
  3213. _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
  3214. list_del(&delayed_tr->list);
  3215. kfree(delayed_tr);
  3216. return 0;
  3217. }
  3218. if (!list_empty(&ioc->delayed_tr_list)) {
  3219. delayed_tr = list_entry(ioc->delayed_tr_list.next,
  3220. struct _tr_list, list);
  3221. mpt3sas_base_free_smid(ioc, smid);
  3222. _scsih_tm_tr_send(ioc, delayed_tr->handle);
  3223. list_del(&delayed_tr->list);
  3224. kfree(delayed_tr);
  3225. return 0;
  3226. }
  3227. return 1;
  3228. }
  3229. /**
  3230. * _scsih_check_topo_delete_events - sanity check on topo events
  3231. * @ioc: per adapter object
  3232. * @event_data: the event data payload
  3233. *
  3234. * This routine added to better handle cable breaker.
  3235. *
  3236. * This handles the case where driver receives multiple expander
  3237. * add and delete events in a single shot. When there is a delete event
  3238. * the routine will void any pending add events waiting in the event queue.
  3239. *
  3240. * Return nothing.
  3241. */
  3242. static void
  3243. _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
  3244. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3245. {
  3246. struct fw_event_work *fw_event;
  3247. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  3248. u16 expander_handle;
  3249. struct _sas_node *sas_expander;
  3250. unsigned long flags;
  3251. int i, reason_code;
  3252. u16 handle;
  3253. for (i = 0 ; i < event_data->NumEntries; i++) {
  3254. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3255. if (!handle)
  3256. continue;
  3257. reason_code = event_data->PHY[i].PhyStatus &
  3258. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3259. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
  3260. _scsih_tm_tr_send(ioc, handle);
  3261. }
  3262. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  3263. if (expander_handle < ioc->sas_hba.num_phys) {
  3264. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  3265. return;
  3266. }
  3267. if (event_data->ExpStatus ==
  3268. MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
  3269. /* put expander attached devices into blocking state */
  3270. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3271. sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
  3272. expander_handle);
  3273. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  3274. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3275. do {
  3276. handle = find_first_bit(ioc->blocking_handles,
  3277. ioc->facts.MaxDevHandle);
  3278. if (handle < ioc->facts.MaxDevHandle)
  3279. _scsih_block_io_device(ioc, handle);
  3280. } while (test_and_clear_bit(handle, ioc->blocking_handles));
  3281. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  3282. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  3283. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  3284. return;
  3285. /* mark ignore flag for pending events */
  3286. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  3287. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  3288. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  3289. fw_event->ignore)
  3290. continue;
  3291. local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
  3292. fw_event->event_data;
  3293. if (local_event_data->ExpStatus ==
  3294. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  3295. local_event_data->ExpStatus ==
  3296. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  3297. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  3298. expander_handle) {
  3299. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3300. "setting ignoring flag\n", ioc->name));
  3301. fw_event->ignore = 1;
  3302. }
  3303. }
  3304. }
  3305. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  3306. }
  3307. /**
  3308. * _scsih_set_volume_delete_flag - setting volume delete flag
  3309. * @ioc: per adapter object
  3310. * @handle: device handle
  3311. *
  3312. * This returns nothing.
  3313. */
  3314. static void
  3315. _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  3316. {
  3317. struct _raid_device *raid_device;
  3318. struct MPT3SAS_TARGET *sas_target_priv_data;
  3319. unsigned long flags;
  3320. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  3321. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  3322. if (raid_device && raid_device->starget &&
  3323. raid_device->starget->hostdata) {
  3324. sas_target_priv_data =
  3325. raid_device->starget->hostdata;
  3326. sas_target_priv_data->deleted = 1;
  3327. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3328. "setting delete flag: handle(0x%04x), "
  3329. "wwid(0x%016llx)\n", ioc->name, handle,
  3330. (unsigned long long) raid_device->wwid));
  3331. }
  3332. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  3333. }
  3334. /**
  3335. * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
  3336. * @handle: input handle
  3337. * @a: handle for volume a
  3338. * @b: handle for volume b
  3339. *
  3340. * IR firmware only supports two raid volumes. The purpose of this
  3341. * routine is to set the volume handle in either a or b. When the given
  3342. * input handle is non-zero, or when a and b have not been set before.
  3343. */
  3344. static void
  3345. _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
  3346. {
  3347. if (!handle || handle == *a || handle == *b)
  3348. return;
  3349. if (!*a)
  3350. *a = handle;
  3351. else if (!*b)
  3352. *b = handle;
  3353. }
  3354. /**
  3355. * _scsih_check_ir_config_unhide_events - check for UNHIDE events
  3356. * @ioc: per adapter object
  3357. * @event_data: the event data payload
  3358. * Context: interrupt time.
  3359. *
  3360. * This routine will send target reset to volume, followed by target
  3361. * resets to the PDs. This is called when a PD has been removed, or
  3362. * volume has been deleted or removed. When the target reset is sent
  3363. * to volume, the PD target resets need to be queued to start upon
  3364. * completion of the volume target reset.
  3365. *
  3366. * Return nothing.
  3367. */
  3368. static void
  3369. _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
  3370. Mpi2EventDataIrConfigChangeList_t *event_data)
  3371. {
  3372. Mpi2EventIrConfigElement_t *element;
  3373. int i;
  3374. u16 handle, volume_handle, a, b;
  3375. struct _tr_list *delayed_tr;
  3376. a = 0;
  3377. b = 0;
  3378. if (ioc->is_warpdrive)
  3379. return;
  3380. /* Volume Resets for Deleted or Removed */
  3381. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3382. for (i = 0; i < event_data->NumElements; i++, element++) {
  3383. if (le32_to_cpu(event_data->Flags) &
  3384. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  3385. continue;
  3386. if (element->ReasonCode ==
  3387. MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
  3388. element->ReasonCode ==
  3389. MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
  3390. volume_handle = le16_to_cpu(element->VolDevHandle);
  3391. _scsih_set_volume_delete_flag(ioc, volume_handle);
  3392. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  3393. }
  3394. }
  3395. /* Volume Resets for UNHIDE events */
  3396. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3397. for (i = 0; i < event_data->NumElements; i++, element++) {
  3398. if (le32_to_cpu(event_data->Flags) &
  3399. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
  3400. continue;
  3401. if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
  3402. volume_handle = le16_to_cpu(element->VolDevHandle);
  3403. _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
  3404. }
  3405. }
  3406. if (a)
  3407. _scsih_tm_tr_volume_send(ioc, a);
  3408. if (b)
  3409. _scsih_tm_tr_volume_send(ioc, b);
  3410. /* PD target resets */
  3411. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  3412. for (i = 0; i < event_data->NumElements; i++, element++) {
  3413. if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
  3414. continue;
  3415. handle = le16_to_cpu(element->PhysDiskDevHandle);
  3416. volume_handle = le16_to_cpu(element->VolDevHandle);
  3417. clear_bit(handle, ioc->pd_handles);
  3418. if (!volume_handle)
  3419. _scsih_tm_tr_send(ioc, handle);
  3420. else if (volume_handle == a || volume_handle == b) {
  3421. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  3422. BUG_ON(!delayed_tr);
  3423. INIT_LIST_HEAD(&delayed_tr->list);
  3424. delayed_tr->handle = handle;
  3425. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  3426. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  3427. "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
  3428. handle));
  3429. } else
  3430. _scsih_tm_tr_send(ioc, handle);
  3431. }
  3432. }
  3433. /**
  3434. * _scsih_check_volume_delete_events - set delete flag for volumes
  3435. * @ioc: per adapter object
  3436. * @event_data: the event data payload
  3437. * Context: interrupt time.
  3438. *
  3439. * This will handle the case when the cable connected to entire volume is
  3440. * pulled. We will take care of setting the deleted flag so normal IO will
  3441. * not be sent.
  3442. *
  3443. * Return nothing.
  3444. */
  3445. static void
  3446. _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
  3447. Mpi2EventDataIrVolume_t *event_data)
  3448. {
  3449. u32 state;
  3450. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  3451. return;
  3452. state = le32_to_cpu(event_data->NewValue);
  3453. if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
  3454. MPI2_RAID_VOL_STATE_FAILED)
  3455. _scsih_set_volume_delete_flag(ioc,
  3456. le16_to_cpu(event_data->VolDevHandle));
  3457. }
  3458. /**
  3459. * _scsih_temp_threshold_events - display temperature threshold exceeded events
  3460. * @ioc: per adapter object
  3461. * @event_data: the temp threshold event data
  3462. * Context: interrupt time.
  3463. *
  3464. * Return nothing.
  3465. */
  3466. static void
  3467. _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
  3468. Mpi2EventDataTemperature_t *event_data)
  3469. {
  3470. if (ioc->temp_sensors_count >= event_data->SensorNum) {
  3471. pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
  3472. " exceeded for Sensor: %d !!!\n", ioc->name,
  3473. ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
  3474. ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
  3475. ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
  3476. ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
  3477. event_data->SensorNum);
  3478. pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
  3479. ioc->name, event_data->CurrentTemperature);
  3480. }
  3481. }
  3482. static int _scsih_set_satl_pending(struct scsi_cmnd *scmd, bool pending)
  3483. {
  3484. struct MPT3SAS_DEVICE *priv = scmd->device->hostdata;
  3485. if (scmd->cmnd[0] != ATA_12 && scmd->cmnd[0] != ATA_16)
  3486. return 0;
  3487. if (pending)
  3488. return test_and_set_bit(0, &priv->ata_command_pending);
  3489. clear_bit(0, &priv->ata_command_pending);
  3490. return 0;
  3491. }
  3492. /**
  3493. * _scsih_flush_running_cmds - completing outstanding commands.
  3494. * @ioc: per adapter object
  3495. *
  3496. * The flushing out of all pending scmd commands following host reset,
  3497. * where all IO is dropped to the floor.
  3498. *
  3499. * Return nothing.
  3500. */
  3501. static void
  3502. _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
  3503. {
  3504. struct scsi_cmnd *scmd;
  3505. u16 smid;
  3506. u16 count = 0;
  3507. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  3508. scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
  3509. if (!scmd)
  3510. continue;
  3511. count++;
  3512. _scsih_set_satl_pending(scmd, false);
  3513. mpt3sas_base_free_smid(ioc, smid);
  3514. scsi_dma_unmap(scmd);
  3515. if (ioc->pci_error_recovery)
  3516. scmd->result = DID_NO_CONNECT << 16;
  3517. else
  3518. scmd->result = DID_RESET << 16;
  3519. scmd->scsi_done(scmd);
  3520. }
  3521. dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
  3522. ioc->name, count));
  3523. }
  3524. /**
  3525. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  3526. * @ioc: per adapter object
  3527. * @scmd: pointer to scsi command object
  3528. * @mpi_request: pointer to the SCSI_IO reqest message frame
  3529. *
  3530. * Supporting protection 1 and 3.
  3531. *
  3532. * Returns nothing
  3533. */
  3534. static void
  3535. _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  3536. Mpi2SCSIIORequest_t *mpi_request)
  3537. {
  3538. u16 eedp_flags;
  3539. unsigned char prot_op = scsi_get_prot_op(scmd);
  3540. unsigned char prot_type = scsi_get_prot_type(scmd);
  3541. Mpi25SCSIIORequest_t *mpi_request_3v =
  3542. (Mpi25SCSIIORequest_t *)mpi_request;
  3543. if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
  3544. return;
  3545. if (prot_op == SCSI_PROT_READ_STRIP)
  3546. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  3547. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  3548. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  3549. else
  3550. return;
  3551. switch (prot_type) {
  3552. case SCSI_PROT_DIF_TYPE1:
  3553. case SCSI_PROT_DIF_TYPE2:
  3554. /*
  3555. * enable ref/guard checking
  3556. * auto increment ref tag
  3557. */
  3558. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  3559. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  3560. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  3561. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  3562. cpu_to_be32(scsi_prot_ref_tag(scmd));
  3563. break;
  3564. case SCSI_PROT_DIF_TYPE3:
  3565. /*
  3566. * enable guard checking
  3567. */
  3568. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  3569. break;
  3570. }
  3571. mpi_request_3v->EEDPBlockSize =
  3572. cpu_to_le16(scmd->device->sector_size);
  3573. mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
  3574. }
  3575. /**
  3576. * _scsih_eedp_error_handling - return sense code for EEDP errors
  3577. * @scmd: pointer to scsi command object
  3578. * @ioc_status: ioc status
  3579. *
  3580. * Returns nothing
  3581. */
  3582. static void
  3583. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  3584. {
  3585. u8 ascq;
  3586. switch (ioc_status) {
  3587. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  3588. ascq = 0x01;
  3589. break;
  3590. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  3591. ascq = 0x02;
  3592. break;
  3593. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  3594. ascq = 0x03;
  3595. break;
  3596. default:
  3597. ascq = 0x00;
  3598. break;
  3599. }
  3600. scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
  3601. ascq);
  3602. scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
  3603. SAM_STAT_CHECK_CONDITION;
  3604. }
  3605. /**
  3606. * scsih_qcmd - main scsi request entry point
  3607. * @scmd: pointer to scsi command object
  3608. * @done: function pointer to be invoked on completion
  3609. *
  3610. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  3611. *
  3612. * Returns 0 on success. If there's a failure, return either:
  3613. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  3614. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  3615. */
  3616. static int
  3617. scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
  3618. {
  3619. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  3620. struct MPT3SAS_DEVICE *sas_device_priv_data;
  3621. struct MPT3SAS_TARGET *sas_target_priv_data;
  3622. struct _raid_device *raid_device;
  3623. Mpi2SCSIIORequest_t *mpi_request;
  3624. u32 mpi_control;
  3625. u16 smid;
  3626. u16 handle;
  3627. if (ioc->logging_level & MPT_DEBUG_SCSI)
  3628. scsi_print_command(scmd);
  3629. sas_device_priv_data = scmd->device->hostdata;
  3630. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  3631. scmd->result = DID_NO_CONNECT << 16;
  3632. scmd->scsi_done(scmd);
  3633. return 0;
  3634. }
  3635. if (ioc->pci_error_recovery || ioc->remove_host) {
  3636. scmd->result = DID_NO_CONNECT << 16;
  3637. scmd->scsi_done(scmd);
  3638. return 0;
  3639. }
  3640. /*
  3641. * Bug work around for firmware SATL handling. The loop
  3642. * is based on atomic operations and ensures consistency
  3643. * since we're lockless at this point
  3644. */
  3645. do {
  3646. if (test_bit(0, &sas_device_priv_data->ata_command_pending)) {
  3647. scmd->result = SAM_STAT_BUSY;
  3648. scmd->scsi_done(scmd);
  3649. return 0;
  3650. }
  3651. } while (_scsih_set_satl_pending(scmd, true));
  3652. sas_target_priv_data = sas_device_priv_data->sas_target;
  3653. /* invalid device handle */
  3654. handle = sas_target_priv_data->handle;
  3655. if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
  3656. scmd->result = DID_NO_CONNECT << 16;
  3657. scmd->scsi_done(scmd);
  3658. return 0;
  3659. }
  3660. /* host recovery or link resets sent via IOCTLs */
  3661. if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
  3662. return SCSI_MLQUEUE_HOST_BUSY;
  3663. /* device has been deleted */
  3664. else if (sas_target_priv_data->deleted) {
  3665. scmd->result = DID_NO_CONNECT << 16;
  3666. scmd->scsi_done(scmd);
  3667. return 0;
  3668. /* device busy with task managment */
  3669. } else if (sas_target_priv_data->tm_busy ||
  3670. sas_device_priv_data->block)
  3671. return SCSI_MLQUEUE_DEVICE_BUSY;
  3672. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  3673. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  3674. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  3675. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  3676. else
  3677. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  3678. /* set tags */
  3679. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  3680. /* Make sure Device is not raid volume.
  3681. * We do not expose raid functionality to upper layer for warpdrive.
  3682. */
  3683. if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
  3684. && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
  3685. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  3686. smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
  3687. if (!smid) {
  3688. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3689. ioc->name, __func__);
  3690. goto out;
  3691. }
  3692. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3693. memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
  3694. _scsih_setup_eedp(ioc, scmd, mpi_request);
  3695. if (scmd->cmd_len == 32)
  3696. mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
  3697. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  3698. if (sas_device_priv_data->sas_target->flags &
  3699. MPT_TARGET_FLAGS_RAID_COMPONENT)
  3700. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  3701. else
  3702. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  3703. mpi_request->DevHandle = cpu_to_le16(handle);
  3704. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  3705. mpi_request->Control = cpu_to_le32(mpi_control);
  3706. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  3707. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  3708. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  3709. mpi_request->SenseBufferLowAddress =
  3710. mpt3sas_base_get_sense_buffer_dma(ioc, smid);
  3711. mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
  3712. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  3713. mpi_request->LUN);
  3714. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  3715. if (mpi_request->DataLength) {
  3716. if (ioc->build_sg_scmd(ioc, scmd, smid)) {
  3717. mpt3sas_base_free_smid(ioc, smid);
  3718. goto out;
  3719. }
  3720. } else
  3721. ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
  3722. raid_device = sas_target_priv_data->raid_device;
  3723. if (raid_device && raid_device->direct_io_enabled)
  3724. mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
  3725. smid);
  3726. if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
  3727. if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
  3728. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
  3729. MPI25_SCSIIO_IOFLAGS_FAST_PATH);
  3730. mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
  3731. } else
  3732. mpt3sas_base_put_smid_scsi_io(ioc, smid,
  3733. le16_to_cpu(mpi_request->DevHandle));
  3734. } else
  3735. mpt3sas_base_put_smid_default(ioc, smid);
  3736. return 0;
  3737. out:
  3738. return SCSI_MLQUEUE_HOST_BUSY;
  3739. }
  3740. /**
  3741. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  3742. * @sense_buffer: sense data returned by target
  3743. * @data: normalized skey/asc/ascq
  3744. *
  3745. * Return nothing.
  3746. */
  3747. static void
  3748. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  3749. {
  3750. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  3751. /* descriptor format */
  3752. data->skey = sense_buffer[1] & 0x0F;
  3753. data->asc = sense_buffer[2];
  3754. data->ascq = sense_buffer[3];
  3755. } else {
  3756. /* fixed format */
  3757. data->skey = sense_buffer[2] & 0x0F;
  3758. data->asc = sense_buffer[12];
  3759. data->ascq = sense_buffer[13];
  3760. }
  3761. }
  3762. /**
  3763. * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
  3764. * @ioc: per adapter object
  3765. * @scmd: pointer to scsi command object
  3766. * @mpi_reply: reply mf payload returned from firmware
  3767. *
  3768. * scsi_status - SCSI Status code returned from target device
  3769. * scsi_state - state info associated with SCSI_IO determined by ioc
  3770. * ioc_status - ioc supplied status info
  3771. *
  3772. * Return nothing.
  3773. */
  3774. static void
  3775. _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  3776. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  3777. {
  3778. u32 response_info;
  3779. u8 *response_bytes;
  3780. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  3781. MPI2_IOCSTATUS_MASK;
  3782. u8 scsi_state = mpi_reply->SCSIState;
  3783. u8 scsi_status = mpi_reply->SCSIStatus;
  3784. char *desc_ioc_state = NULL;
  3785. char *desc_scsi_status = NULL;
  3786. char *desc_scsi_state = ioc->tmp_string;
  3787. u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  3788. struct _sas_device *sas_device = NULL;
  3789. struct scsi_target *starget = scmd->device->sdev_target;
  3790. struct MPT3SAS_TARGET *priv_target = starget->hostdata;
  3791. char *device_str = NULL;
  3792. if (!priv_target)
  3793. return;
  3794. if (ioc->hide_ir_msg)
  3795. device_str = "WarpDrive";
  3796. else
  3797. device_str = "volume";
  3798. if (log_info == 0x31170000)
  3799. return;
  3800. switch (ioc_status) {
  3801. case MPI2_IOCSTATUS_SUCCESS:
  3802. desc_ioc_state = "success";
  3803. break;
  3804. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  3805. desc_ioc_state = "invalid function";
  3806. break;
  3807. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  3808. desc_ioc_state = "scsi recovered error";
  3809. break;
  3810. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  3811. desc_ioc_state = "scsi invalid dev handle";
  3812. break;
  3813. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  3814. desc_ioc_state = "scsi device not there";
  3815. break;
  3816. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  3817. desc_ioc_state = "scsi data overrun";
  3818. break;
  3819. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  3820. desc_ioc_state = "scsi data underrun";
  3821. break;
  3822. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  3823. desc_ioc_state = "scsi io data error";
  3824. break;
  3825. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  3826. desc_ioc_state = "scsi protocol error";
  3827. break;
  3828. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  3829. desc_ioc_state = "scsi task terminated";
  3830. break;
  3831. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  3832. desc_ioc_state = "scsi residual mismatch";
  3833. break;
  3834. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  3835. desc_ioc_state = "scsi task mgmt failed";
  3836. break;
  3837. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  3838. desc_ioc_state = "scsi ioc terminated";
  3839. break;
  3840. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  3841. desc_ioc_state = "scsi ext terminated";
  3842. break;
  3843. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  3844. desc_ioc_state = "eedp guard error";
  3845. break;
  3846. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  3847. desc_ioc_state = "eedp ref tag error";
  3848. break;
  3849. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  3850. desc_ioc_state = "eedp app tag error";
  3851. break;
  3852. case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  3853. desc_ioc_state = "insufficient power";
  3854. break;
  3855. default:
  3856. desc_ioc_state = "unknown";
  3857. break;
  3858. }
  3859. switch (scsi_status) {
  3860. case MPI2_SCSI_STATUS_GOOD:
  3861. desc_scsi_status = "good";
  3862. break;
  3863. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  3864. desc_scsi_status = "check condition";
  3865. break;
  3866. case MPI2_SCSI_STATUS_CONDITION_MET:
  3867. desc_scsi_status = "condition met";
  3868. break;
  3869. case MPI2_SCSI_STATUS_BUSY:
  3870. desc_scsi_status = "busy";
  3871. break;
  3872. case MPI2_SCSI_STATUS_INTERMEDIATE:
  3873. desc_scsi_status = "intermediate";
  3874. break;
  3875. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  3876. desc_scsi_status = "intermediate condmet";
  3877. break;
  3878. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  3879. desc_scsi_status = "reservation conflict";
  3880. break;
  3881. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  3882. desc_scsi_status = "command terminated";
  3883. break;
  3884. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  3885. desc_scsi_status = "task set full";
  3886. break;
  3887. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  3888. desc_scsi_status = "aca active";
  3889. break;
  3890. case MPI2_SCSI_STATUS_TASK_ABORTED:
  3891. desc_scsi_status = "task aborted";
  3892. break;
  3893. default:
  3894. desc_scsi_status = "unknown";
  3895. break;
  3896. }
  3897. desc_scsi_state[0] = '\0';
  3898. if (!scsi_state)
  3899. desc_scsi_state = " ";
  3900. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  3901. strcat(desc_scsi_state, "response info ");
  3902. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  3903. strcat(desc_scsi_state, "state terminated ");
  3904. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  3905. strcat(desc_scsi_state, "no status ");
  3906. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  3907. strcat(desc_scsi_state, "autosense failed ");
  3908. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  3909. strcat(desc_scsi_state, "autosense valid ");
  3910. scsi_print_command(scmd);
  3911. if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  3912. pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
  3913. device_str, (unsigned long long)priv_target->sas_address);
  3914. } else {
  3915. sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
  3916. if (sas_device) {
  3917. pr_warn(MPT3SAS_FMT
  3918. "\tsas_address(0x%016llx), phy(%d)\n",
  3919. ioc->name, (unsigned long long)
  3920. sas_device->sas_address, sas_device->phy);
  3921. if (sas_device->enclosure_handle != 0)
  3922. pr_warn(MPT3SAS_FMT
  3923. "\tenclosure_logical_id(0x%016llx),"
  3924. "slot(%d)\n", ioc->name,
  3925. (unsigned long long)
  3926. sas_device->enclosure_logical_id,
  3927. sas_device->slot);
  3928. if (sas_device->connector_name[0])
  3929. pr_warn(MPT3SAS_FMT
  3930. "\tenclosure level(0x%04x),"
  3931. " connector name( %s)\n", ioc->name,
  3932. sas_device->enclosure_level,
  3933. sas_device->connector_name);
  3934. sas_device_put(sas_device);
  3935. }
  3936. }
  3937. pr_warn(MPT3SAS_FMT
  3938. "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
  3939. ioc->name, le16_to_cpu(mpi_reply->DevHandle),
  3940. desc_ioc_state, ioc_status, smid);
  3941. pr_warn(MPT3SAS_FMT
  3942. "\trequest_len(%d), underflow(%d), resid(%d)\n",
  3943. ioc->name, scsi_bufflen(scmd), scmd->underflow,
  3944. scsi_get_resid(scmd));
  3945. pr_warn(MPT3SAS_FMT
  3946. "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
  3947. ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  3948. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  3949. pr_warn(MPT3SAS_FMT
  3950. "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
  3951. ioc->name, desc_scsi_status,
  3952. scsi_status, desc_scsi_state, scsi_state);
  3953. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  3954. struct sense_info data;
  3955. _scsih_normalize_sense(scmd->sense_buffer, &data);
  3956. pr_warn(MPT3SAS_FMT
  3957. "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
  3958. ioc->name, data.skey,
  3959. data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
  3960. }
  3961. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  3962. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  3963. response_bytes = (u8 *)&response_info;
  3964. _scsih_response_code(ioc, response_bytes[0]);
  3965. }
  3966. }
  3967. /**
  3968. * _scsih_turn_on_pfa_led - illuminate PFA LED
  3969. * @ioc: per adapter object
  3970. * @handle: device handle
  3971. * Context: process
  3972. *
  3973. * Return nothing.
  3974. */
  3975. static void
  3976. _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  3977. {
  3978. Mpi2SepReply_t mpi_reply;
  3979. Mpi2SepRequest_t mpi_request;
  3980. struct _sas_device *sas_device;
  3981. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  3982. if (!sas_device)
  3983. return;
  3984. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  3985. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  3986. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  3987. mpi_request.SlotStatus =
  3988. cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
  3989. mpi_request.DevHandle = cpu_to_le16(handle);
  3990. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  3991. if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  3992. &mpi_request)) != 0) {
  3993. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
  3994. __FILE__, __LINE__, __func__);
  3995. goto out;
  3996. }
  3997. sas_device->pfa_led_on = 1;
  3998. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  3999. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  4000. "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
  4001. ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
  4002. le32_to_cpu(mpi_reply.IOCLogInfo)));
  4003. goto out;
  4004. }
  4005. out:
  4006. sas_device_put(sas_device);
  4007. }
  4008. /**
  4009. * _scsih_turn_off_pfa_led - turn off Fault LED
  4010. * @ioc: per adapter object
  4011. * @sas_device: sas device whose PFA LED has to turned off
  4012. * Context: process
  4013. *
  4014. * Return nothing.
  4015. */
  4016. static void
  4017. _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
  4018. struct _sas_device *sas_device)
  4019. {
  4020. Mpi2SepReply_t mpi_reply;
  4021. Mpi2SepRequest_t mpi_request;
  4022. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  4023. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  4024. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  4025. mpi_request.SlotStatus = 0;
  4026. mpi_request.Slot = cpu_to_le16(sas_device->slot);
  4027. mpi_request.DevHandle = 0;
  4028. mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
  4029. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
  4030. if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  4031. &mpi_request)) != 0) {
  4032. printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
  4033. __FILE__, __LINE__, __func__);
  4034. return;
  4035. }
  4036. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  4037. dewtprintk(ioc, printk(MPT3SAS_FMT
  4038. "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
  4039. ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
  4040. le32_to_cpu(mpi_reply.IOCLogInfo)));
  4041. return;
  4042. }
  4043. }
  4044. /**
  4045. * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
  4046. * @ioc: per adapter object
  4047. * @handle: device handle
  4048. * Context: interrupt.
  4049. *
  4050. * Return nothing.
  4051. */
  4052. static void
  4053. _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  4054. {
  4055. struct fw_event_work *fw_event;
  4056. fw_event = alloc_fw_event_work(0);
  4057. if (!fw_event)
  4058. return;
  4059. fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
  4060. fw_event->device_handle = handle;
  4061. fw_event->ioc = ioc;
  4062. _scsih_fw_event_add(ioc, fw_event);
  4063. fw_event_work_put(fw_event);
  4064. }
  4065. /**
  4066. * _scsih_smart_predicted_fault - process smart errors
  4067. * @ioc: per adapter object
  4068. * @handle: device handle
  4069. * Context: interrupt.
  4070. *
  4071. * Return nothing.
  4072. */
  4073. static void
  4074. _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  4075. {
  4076. struct scsi_target *starget;
  4077. struct MPT3SAS_TARGET *sas_target_priv_data;
  4078. Mpi2EventNotificationReply_t *event_reply;
  4079. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  4080. struct _sas_device *sas_device;
  4081. ssize_t sz;
  4082. unsigned long flags;
  4083. /* only handle non-raid devices */
  4084. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4085. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  4086. if (!sas_device)
  4087. goto out_unlock;
  4088. starget = sas_device->starget;
  4089. sas_target_priv_data = starget->hostdata;
  4090. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  4091. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
  4092. goto out_unlock;
  4093. if (sas_device->enclosure_handle != 0)
  4094. starget_printk(KERN_INFO, starget, "predicted fault, "
  4095. "enclosure logical id(0x%016llx), slot(%d)\n",
  4096. (unsigned long long)sas_device->enclosure_logical_id,
  4097. sas_device->slot);
  4098. if (sas_device->connector_name[0] != '\0')
  4099. starget_printk(KERN_WARNING, starget, "predicted fault, "
  4100. "enclosure level(0x%04x), connector name( %s)\n",
  4101. sas_device->enclosure_level,
  4102. sas_device->connector_name);
  4103. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4104. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
  4105. _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
  4106. /* insert into event log */
  4107. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  4108. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  4109. event_reply = kzalloc(sz, GFP_KERNEL);
  4110. if (!event_reply) {
  4111. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4112. ioc->name, __FILE__, __LINE__, __func__);
  4113. goto out;
  4114. }
  4115. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  4116. event_reply->Event =
  4117. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  4118. event_reply->MsgLength = sz/4;
  4119. event_reply->EventDataLength =
  4120. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  4121. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  4122. event_reply->EventData;
  4123. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  4124. event_data->ASC = 0x5D;
  4125. event_data->DevHandle = cpu_to_le16(handle);
  4126. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  4127. mpt3sas_ctl_add_to_event_log(ioc, event_reply);
  4128. kfree(event_reply);
  4129. out:
  4130. if (sas_device)
  4131. sas_device_put(sas_device);
  4132. return;
  4133. out_unlock:
  4134. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4135. goto out;
  4136. }
  4137. /**
  4138. * _scsih_io_done - scsi request callback
  4139. * @ioc: per adapter object
  4140. * @smid: system request message index
  4141. * @msix_index: MSIX table index supplied by the OS
  4142. * @reply: reply message frame(lower 32bit addr)
  4143. *
  4144. * Callback handler when using _scsih_qcmd.
  4145. *
  4146. * Return 1 meaning mf should be freed from _base_interrupt
  4147. * 0 means the mf is freed from this function.
  4148. */
  4149. static u8
  4150. _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  4151. {
  4152. Mpi2SCSIIORequest_t *mpi_request;
  4153. Mpi2SCSIIOReply_t *mpi_reply;
  4154. struct scsi_cmnd *scmd;
  4155. u16 ioc_status;
  4156. u32 xfer_cnt;
  4157. u8 scsi_state;
  4158. u8 scsi_status;
  4159. u32 log_info;
  4160. struct MPT3SAS_DEVICE *sas_device_priv_data;
  4161. u32 response_code = 0;
  4162. unsigned long flags;
  4163. unsigned int sector_sz;
  4164. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  4165. scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
  4166. if (scmd == NULL)
  4167. return 1;
  4168. _scsih_set_satl_pending(scmd, false);
  4169. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  4170. if (mpi_reply == NULL) {
  4171. scmd->result = DID_OK << 16;
  4172. goto out;
  4173. }
  4174. sas_device_priv_data = scmd->device->hostdata;
  4175. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  4176. sas_device_priv_data->sas_target->deleted) {
  4177. scmd->result = DID_NO_CONNECT << 16;
  4178. goto out;
  4179. }
  4180. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  4181. /*
  4182. * WARPDRIVE: If direct_io is set then it is directIO,
  4183. * the failed direct I/O should be redirected to volume
  4184. */
  4185. if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
  4186. ((ioc_status & MPI2_IOCSTATUS_MASK)
  4187. != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
  4188. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4189. ioc->scsi_lookup[smid - 1].scmd = scmd;
  4190. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4191. mpt3sas_scsi_direct_io_set(ioc, smid, 0);
  4192. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  4193. mpi_request->DevHandle =
  4194. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  4195. mpt3sas_base_put_smid_scsi_io(ioc, smid,
  4196. sas_device_priv_data->sas_target->handle);
  4197. return 0;
  4198. }
  4199. /* turning off TLR */
  4200. scsi_state = mpi_reply->SCSIState;
  4201. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  4202. response_code =
  4203. le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
  4204. if (!sas_device_priv_data->tlr_snoop_check) {
  4205. sas_device_priv_data->tlr_snoop_check++;
  4206. if (!ioc->is_warpdrive &&
  4207. !scsih_is_raid(&scmd->device->sdev_gendev) &&
  4208. sas_is_tlr_enabled(scmd->device) &&
  4209. response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
  4210. sas_disable_tlr(scmd->device);
  4211. sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
  4212. }
  4213. }
  4214. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  4215. /* In case of bogus fw or device, we could end up having
  4216. * unaligned partial completion. We can force alignment here,
  4217. * then scsi-ml does not need to handle this misbehavior.
  4218. */
  4219. sector_sz = scmd->device->sector_size;
  4220. if (unlikely(scmd->request->cmd_type == REQ_TYPE_FS && sector_sz &&
  4221. xfer_cnt % sector_sz)) {
  4222. sdev_printk(KERN_INFO, scmd->device,
  4223. "unaligned partial completion avoided (xfer_cnt=%u, sector_sz=%u)\n",
  4224. xfer_cnt, sector_sz);
  4225. xfer_cnt = round_down(xfer_cnt, sector_sz);
  4226. }
  4227. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  4228. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  4229. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  4230. else
  4231. log_info = 0;
  4232. ioc_status &= MPI2_IOCSTATUS_MASK;
  4233. scsi_status = mpi_reply->SCSIStatus;
  4234. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  4235. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  4236. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  4237. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  4238. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  4239. }
  4240. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  4241. struct sense_info data;
  4242. const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
  4243. smid);
  4244. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  4245. le32_to_cpu(mpi_reply->SenseCount));
  4246. memcpy(scmd->sense_buffer, sense_data, sz);
  4247. _scsih_normalize_sense(scmd->sense_buffer, &data);
  4248. /* failure prediction threshold exceeded */
  4249. if (data.asc == 0x5D)
  4250. _scsih_smart_predicted_fault(ioc,
  4251. le16_to_cpu(mpi_reply->DevHandle));
  4252. mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
  4253. if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
  4254. ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
  4255. (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
  4256. (scmd->sense_buffer[2] == HARDWARE_ERROR)))
  4257. _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
  4258. }
  4259. switch (ioc_status) {
  4260. case MPI2_IOCSTATUS_BUSY:
  4261. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  4262. scmd->result = SAM_STAT_BUSY;
  4263. break;
  4264. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  4265. scmd->result = DID_NO_CONNECT << 16;
  4266. break;
  4267. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  4268. if (sas_device_priv_data->block) {
  4269. scmd->result = DID_TRANSPORT_DISRUPTED << 16;
  4270. goto out;
  4271. }
  4272. if (log_info == 0x31110630) {
  4273. if (scmd->retries > 2) {
  4274. scmd->result = DID_NO_CONNECT << 16;
  4275. scsi_device_set_state(scmd->device,
  4276. SDEV_OFFLINE);
  4277. } else {
  4278. scmd->result = DID_SOFT_ERROR << 16;
  4279. scmd->device->expecting_cc_ua = 1;
  4280. }
  4281. break;
  4282. } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
  4283. scmd->result = DID_RESET << 16;
  4284. break;
  4285. }
  4286. scmd->result = DID_SOFT_ERROR << 16;
  4287. break;
  4288. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  4289. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  4290. scmd->result = DID_RESET << 16;
  4291. break;
  4292. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  4293. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  4294. scmd->result = DID_SOFT_ERROR << 16;
  4295. else
  4296. scmd->result = (DID_OK << 16) | scsi_status;
  4297. break;
  4298. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  4299. scmd->result = (DID_OK << 16) | scsi_status;
  4300. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  4301. break;
  4302. if (xfer_cnt < scmd->underflow) {
  4303. if (scsi_status == SAM_STAT_BUSY)
  4304. scmd->result = SAM_STAT_BUSY;
  4305. else
  4306. scmd->result = DID_SOFT_ERROR << 16;
  4307. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  4308. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  4309. scmd->result = DID_SOFT_ERROR << 16;
  4310. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  4311. scmd->result = DID_RESET << 16;
  4312. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  4313. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  4314. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  4315. scmd->result = (DRIVER_SENSE << 24) |
  4316. SAM_STAT_CHECK_CONDITION;
  4317. scmd->sense_buffer[0] = 0x70;
  4318. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  4319. scmd->sense_buffer[12] = 0x20;
  4320. scmd->sense_buffer[13] = 0;
  4321. }
  4322. break;
  4323. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  4324. scsi_set_resid(scmd, 0);
  4325. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  4326. case MPI2_IOCSTATUS_SUCCESS:
  4327. scmd->result = (DID_OK << 16) | scsi_status;
  4328. if (response_code ==
  4329. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
  4330. (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  4331. MPI2_SCSI_STATE_NO_SCSI_STATUS)))
  4332. scmd->result = DID_SOFT_ERROR << 16;
  4333. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  4334. scmd->result = DID_RESET << 16;
  4335. break;
  4336. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  4337. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  4338. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  4339. _scsih_eedp_error_handling(scmd, ioc_status);
  4340. break;
  4341. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  4342. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  4343. case MPI2_IOCSTATUS_INVALID_SGL:
  4344. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  4345. case MPI2_IOCSTATUS_INVALID_FIELD:
  4346. case MPI2_IOCSTATUS_INVALID_STATE:
  4347. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  4348. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  4349. case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  4350. default:
  4351. scmd->result = DID_SOFT_ERROR << 16;
  4352. break;
  4353. }
  4354. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  4355. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  4356. out:
  4357. scsi_dma_unmap(scmd);
  4358. scmd->scsi_done(scmd);
  4359. return 1;
  4360. }
  4361. /**
  4362. * _scsih_sas_host_refresh - refreshing sas host object contents
  4363. * @ioc: per adapter object
  4364. * Context: user
  4365. *
  4366. * During port enable, fw will send topology events for every device. Its
  4367. * possible that the handles may change from the previous setting, so this
  4368. * code keeping handles updating if changed.
  4369. *
  4370. * Return nothing.
  4371. */
  4372. static void
  4373. _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
  4374. {
  4375. u16 sz;
  4376. u16 ioc_status;
  4377. int i;
  4378. Mpi2ConfigReply_t mpi_reply;
  4379. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  4380. u16 attached_handle;
  4381. u8 link_rate;
  4382. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4383. "updating handles for sas_host(0x%016llx)\n",
  4384. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  4385. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  4386. * sizeof(Mpi2SasIOUnit0PhyData_t));
  4387. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  4388. if (!sas_iounit_pg0) {
  4389. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4390. ioc->name, __FILE__, __LINE__, __func__);
  4391. return;
  4392. }
  4393. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  4394. sas_iounit_pg0, sz)) != 0)
  4395. goto out;
  4396. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  4397. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  4398. goto out;
  4399. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  4400. link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
  4401. if (i == 0)
  4402. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  4403. PhyData[0].ControllerDevHandle);
  4404. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  4405. attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
  4406. AttachedDevHandle);
  4407. if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  4408. link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
  4409. mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
  4410. attached_handle, i, link_rate);
  4411. }
  4412. out:
  4413. kfree(sas_iounit_pg0);
  4414. }
  4415. /**
  4416. * _scsih_sas_host_add - create sas host object
  4417. * @ioc: per adapter object
  4418. *
  4419. * Creating host side data object, stored in ioc->sas_hba
  4420. *
  4421. * Return nothing.
  4422. */
  4423. static void
  4424. _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
  4425. {
  4426. int i;
  4427. Mpi2ConfigReply_t mpi_reply;
  4428. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  4429. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  4430. Mpi2SasPhyPage0_t phy_pg0;
  4431. Mpi2SasDevicePage0_t sas_device_pg0;
  4432. Mpi2SasEnclosurePage0_t enclosure_pg0;
  4433. u16 ioc_status;
  4434. u16 sz;
  4435. u8 device_missing_delay;
  4436. u8 num_phys;
  4437. mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
  4438. if (!num_phys) {
  4439. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4440. ioc->name, __FILE__, __LINE__, __func__);
  4441. return;
  4442. }
  4443. ioc->sas_hba.phy = kcalloc(num_phys,
  4444. sizeof(struct _sas_phy), GFP_KERNEL);
  4445. if (!ioc->sas_hba.phy) {
  4446. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4447. ioc->name, __FILE__, __LINE__, __func__);
  4448. goto out;
  4449. }
  4450. ioc->sas_hba.num_phys = num_phys;
  4451. /* sas_iounit page 0 */
  4452. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  4453. sizeof(Mpi2SasIOUnit0PhyData_t));
  4454. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  4455. if (!sas_iounit_pg0) {
  4456. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4457. ioc->name, __FILE__, __LINE__, __func__);
  4458. return;
  4459. }
  4460. if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  4461. sas_iounit_pg0, sz))) {
  4462. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4463. ioc->name, __FILE__, __LINE__, __func__);
  4464. goto out;
  4465. }
  4466. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4467. MPI2_IOCSTATUS_MASK;
  4468. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4469. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4470. ioc->name, __FILE__, __LINE__, __func__);
  4471. goto out;
  4472. }
  4473. /* sas_iounit page 1 */
  4474. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  4475. sizeof(Mpi2SasIOUnit1PhyData_t));
  4476. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  4477. if (!sas_iounit_pg1) {
  4478. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4479. ioc->name, __FILE__, __LINE__, __func__);
  4480. goto out;
  4481. }
  4482. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  4483. sas_iounit_pg1, sz))) {
  4484. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4485. ioc->name, __FILE__, __LINE__, __func__);
  4486. goto out;
  4487. }
  4488. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4489. MPI2_IOCSTATUS_MASK;
  4490. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4491. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4492. ioc->name, __FILE__, __LINE__, __func__);
  4493. goto out;
  4494. }
  4495. ioc->io_missing_delay =
  4496. sas_iounit_pg1->IODeviceMissingDelay;
  4497. device_missing_delay =
  4498. sas_iounit_pg1->ReportDeviceMissingDelay;
  4499. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  4500. ioc->device_missing_delay = (device_missing_delay &
  4501. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  4502. else
  4503. ioc->device_missing_delay = device_missing_delay &
  4504. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  4505. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  4506. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  4507. if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  4508. i))) {
  4509. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4510. ioc->name, __FILE__, __LINE__, __func__);
  4511. goto out;
  4512. }
  4513. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4514. MPI2_IOCSTATUS_MASK;
  4515. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4516. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4517. ioc->name, __FILE__, __LINE__, __func__);
  4518. goto out;
  4519. }
  4520. if (i == 0)
  4521. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  4522. PhyData[0].ControllerDevHandle);
  4523. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  4524. ioc->sas_hba.phy[i].phy_id = i;
  4525. mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  4526. phy_pg0, ioc->sas_hba.parent_dev);
  4527. }
  4528. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4529. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
  4530. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4531. ioc->name, __FILE__, __LINE__, __func__);
  4532. goto out;
  4533. }
  4534. ioc->sas_hba.enclosure_handle =
  4535. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  4536. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4537. pr_info(MPT3SAS_FMT
  4538. "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
  4539. ioc->name, ioc->sas_hba.handle,
  4540. (unsigned long long) ioc->sas_hba.sas_address,
  4541. ioc->sas_hba.num_phys) ;
  4542. if (ioc->sas_hba.enclosure_handle) {
  4543. if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  4544. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  4545. ioc->sas_hba.enclosure_handle)))
  4546. ioc->sas_hba.enclosure_logical_id =
  4547. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  4548. }
  4549. out:
  4550. kfree(sas_iounit_pg1);
  4551. kfree(sas_iounit_pg0);
  4552. }
  4553. /**
  4554. * _scsih_expander_add - creating expander object
  4555. * @ioc: per adapter object
  4556. * @handle: expander handle
  4557. *
  4558. * Creating expander object, stored in ioc->sas_expander_list.
  4559. *
  4560. * Return 0 for success, else error.
  4561. */
  4562. static int
  4563. _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  4564. {
  4565. struct _sas_node *sas_expander;
  4566. Mpi2ConfigReply_t mpi_reply;
  4567. Mpi2ExpanderPage0_t expander_pg0;
  4568. Mpi2ExpanderPage1_t expander_pg1;
  4569. Mpi2SasEnclosurePage0_t enclosure_pg0;
  4570. u32 ioc_status;
  4571. u16 parent_handle;
  4572. u64 sas_address, sas_address_parent = 0;
  4573. int i;
  4574. unsigned long flags;
  4575. struct _sas_port *mpt3sas_port = NULL;
  4576. int rc = 0;
  4577. if (!handle)
  4578. return -1;
  4579. if (ioc->shost_recovery || ioc->pci_error_recovery)
  4580. return -1;
  4581. if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  4582. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  4583. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4584. ioc->name, __FILE__, __LINE__, __func__);
  4585. return -1;
  4586. }
  4587. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4588. MPI2_IOCSTATUS_MASK;
  4589. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4590. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4591. ioc->name, __FILE__, __LINE__, __func__);
  4592. return -1;
  4593. }
  4594. /* handle out of order topology events */
  4595. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  4596. if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
  4597. != 0) {
  4598. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4599. ioc->name, __FILE__, __LINE__, __func__);
  4600. return -1;
  4601. }
  4602. if (sas_address_parent != ioc->sas_hba.sas_address) {
  4603. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4604. sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
  4605. sas_address_parent);
  4606. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4607. if (!sas_expander) {
  4608. rc = _scsih_expander_add(ioc, parent_handle);
  4609. if (rc != 0)
  4610. return rc;
  4611. }
  4612. }
  4613. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4614. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  4615. sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
  4616. sas_address);
  4617. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4618. if (sas_expander)
  4619. return 0;
  4620. sas_expander = kzalloc(sizeof(struct _sas_node),
  4621. GFP_KERNEL);
  4622. if (!sas_expander) {
  4623. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4624. ioc->name, __FILE__, __LINE__, __func__);
  4625. return -1;
  4626. }
  4627. sas_expander->handle = handle;
  4628. sas_expander->num_phys = expander_pg0.NumPhys;
  4629. sas_expander->sas_address_parent = sas_address_parent;
  4630. sas_expander->sas_address = sas_address;
  4631. pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
  4632. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  4633. handle, parent_handle, (unsigned long long)
  4634. sas_expander->sas_address, sas_expander->num_phys);
  4635. if (!sas_expander->num_phys)
  4636. goto out_fail;
  4637. sas_expander->phy = kcalloc(sas_expander->num_phys,
  4638. sizeof(struct _sas_phy), GFP_KERNEL);
  4639. if (!sas_expander->phy) {
  4640. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4641. ioc->name, __FILE__, __LINE__, __func__);
  4642. rc = -1;
  4643. goto out_fail;
  4644. }
  4645. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  4646. mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
  4647. sas_address_parent);
  4648. if (!mpt3sas_port) {
  4649. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4650. ioc->name, __FILE__, __LINE__, __func__);
  4651. rc = -1;
  4652. goto out_fail;
  4653. }
  4654. sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
  4655. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  4656. if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
  4657. &expander_pg1, i, handle))) {
  4658. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4659. ioc->name, __FILE__, __LINE__, __func__);
  4660. rc = -1;
  4661. goto out_fail;
  4662. }
  4663. sas_expander->phy[i].handle = handle;
  4664. sas_expander->phy[i].phy_id = i;
  4665. if ((mpt3sas_transport_add_expander_phy(ioc,
  4666. &sas_expander->phy[i], expander_pg1,
  4667. sas_expander->parent_dev))) {
  4668. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4669. ioc->name, __FILE__, __LINE__, __func__);
  4670. rc = -1;
  4671. goto out_fail;
  4672. }
  4673. }
  4674. if (sas_expander->enclosure_handle) {
  4675. if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  4676. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  4677. sas_expander->enclosure_handle)))
  4678. sas_expander->enclosure_logical_id =
  4679. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  4680. }
  4681. _scsih_expander_node_add(ioc, sas_expander);
  4682. return 0;
  4683. out_fail:
  4684. if (mpt3sas_port)
  4685. mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
  4686. sas_address_parent);
  4687. kfree(sas_expander);
  4688. return rc;
  4689. }
  4690. /**
  4691. * mpt3sas_expander_remove - removing expander object
  4692. * @ioc: per adapter object
  4693. * @sas_address: expander sas_address
  4694. *
  4695. * Return nothing.
  4696. */
  4697. void
  4698. mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
  4699. {
  4700. struct _sas_node *sas_expander;
  4701. unsigned long flags;
  4702. if (ioc->shost_recovery)
  4703. return;
  4704. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4705. sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
  4706. sas_address);
  4707. if (sas_expander)
  4708. list_del(&sas_expander->list);
  4709. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4710. if (sas_expander)
  4711. _scsih_expander_node_remove(ioc, sas_expander);
  4712. }
  4713. /**
  4714. * _scsih_done - internal SCSI_IO callback handler.
  4715. * @ioc: per adapter object
  4716. * @smid: system request message index
  4717. * @msix_index: MSIX table index supplied by the OS
  4718. * @reply: reply message frame(lower 32bit addr)
  4719. *
  4720. * Callback handler when sending internal generated SCSI_IO.
  4721. * The callback index passed is `ioc->scsih_cb_idx`
  4722. *
  4723. * Return 1 meaning mf should be freed from _base_interrupt
  4724. * 0 means the mf is freed from this function.
  4725. */
  4726. static u8
  4727. _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  4728. {
  4729. MPI2DefaultReply_t *mpi_reply;
  4730. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  4731. if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
  4732. return 1;
  4733. if (ioc->scsih_cmds.smid != smid)
  4734. return 1;
  4735. ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
  4736. if (mpi_reply) {
  4737. memcpy(ioc->scsih_cmds.reply, mpi_reply,
  4738. mpi_reply->MsgLength*4);
  4739. ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
  4740. }
  4741. ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
  4742. complete(&ioc->scsih_cmds.done);
  4743. return 1;
  4744. }
  4745. #define MPT3_MAX_LUNS (255)
  4746. /**
  4747. * _scsih_check_access_status - check access flags
  4748. * @ioc: per adapter object
  4749. * @sas_address: sas address
  4750. * @handle: sas device handle
  4751. * @access_flags: errors returned during discovery of the device
  4752. *
  4753. * Return 0 for success, else failure
  4754. */
  4755. static u8
  4756. _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
  4757. u16 handle, u8 access_status)
  4758. {
  4759. u8 rc = 1;
  4760. char *desc = NULL;
  4761. switch (access_status) {
  4762. case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
  4763. case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
  4764. rc = 0;
  4765. break;
  4766. case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
  4767. desc = "sata capability failed";
  4768. break;
  4769. case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
  4770. desc = "sata affiliation conflict";
  4771. break;
  4772. case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
  4773. desc = "route not addressable";
  4774. break;
  4775. case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
  4776. desc = "smp error not addressable";
  4777. break;
  4778. case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
  4779. desc = "device blocked";
  4780. break;
  4781. case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
  4782. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
  4783. case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
  4784. case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
  4785. case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
  4786. case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
  4787. case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
  4788. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
  4789. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
  4790. case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
  4791. case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
  4792. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
  4793. desc = "sata initialization failed";
  4794. break;
  4795. default:
  4796. desc = "unknown";
  4797. break;
  4798. }
  4799. if (!rc)
  4800. return 0;
  4801. pr_err(MPT3SAS_FMT
  4802. "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
  4803. ioc->name, desc, (unsigned long long)sas_address, handle);
  4804. return rc;
  4805. }
  4806. /**
  4807. * _scsih_check_device - checking device responsiveness
  4808. * @ioc: per adapter object
  4809. * @parent_sas_address: sas address of parent expander or sas host
  4810. * @handle: attached device handle
  4811. * @phy_numberv: phy number
  4812. * @link_rate: new link rate
  4813. *
  4814. * Returns nothing.
  4815. */
  4816. static void
  4817. _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
  4818. u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
  4819. {
  4820. Mpi2ConfigReply_t mpi_reply;
  4821. Mpi2SasDevicePage0_t sas_device_pg0;
  4822. struct _sas_device *sas_device;
  4823. u32 ioc_status;
  4824. unsigned long flags;
  4825. u64 sas_address;
  4826. struct scsi_target *starget;
  4827. struct MPT3SAS_TARGET *sas_target_priv_data;
  4828. u32 device_info;
  4829. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4830. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
  4831. return;
  4832. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  4833. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  4834. return;
  4835. /* wide port handling ~ we need only handle device once for the phy that
  4836. * is matched in sas device page zero
  4837. */
  4838. if (phy_number != sas_device_pg0.PhyNum)
  4839. return;
  4840. /* check if this is end device */
  4841. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4842. if (!(_scsih_is_end_device(device_info)))
  4843. return;
  4844. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4845. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4846. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  4847. sas_address);
  4848. if (!sas_device)
  4849. goto out_unlock;
  4850. if (unlikely(sas_device->handle != handle)) {
  4851. starget = sas_device->starget;
  4852. sas_target_priv_data = starget->hostdata;
  4853. starget_printk(KERN_INFO, starget,
  4854. "handle changed from(0x%04x) to (0x%04x)!!!\n",
  4855. sas_device->handle, handle);
  4856. sas_target_priv_data->handle = handle;
  4857. sas_device->handle = handle;
  4858. if (sas_device_pg0.Flags &
  4859. MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
  4860. sas_device->enclosure_level =
  4861. le16_to_cpu(sas_device_pg0.EnclosureLevel);
  4862. memcpy(sas_device->connector_name,
  4863. sas_device_pg0.ConnectorName, 4);
  4864. sas_device->connector_name[4] = '\0';
  4865. } else {
  4866. sas_device->enclosure_level = 0;
  4867. sas_device->connector_name[0] = '\0';
  4868. }
  4869. }
  4870. /* check if device is present */
  4871. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  4872. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  4873. pr_err(MPT3SAS_FMT
  4874. "device is not present handle(0x%04x), flags!!!\n",
  4875. ioc->name, handle);
  4876. goto out_unlock;
  4877. }
  4878. /* check if there were any issues with discovery */
  4879. if (_scsih_check_access_status(ioc, sas_address, handle,
  4880. sas_device_pg0.AccessStatus))
  4881. goto out_unlock;
  4882. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4883. _scsih_ublock_io_device(ioc, sas_address);
  4884. if (sas_device)
  4885. sas_device_put(sas_device);
  4886. return;
  4887. out_unlock:
  4888. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4889. if (sas_device)
  4890. sas_device_put(sas_device);
  4891. }
  4892. /**
  4893. * _scsih_add_device - creating sas device object
  4894. * @ioc: per adapter object
  4895. * @handle: sas device handle
  4896. * @phy_num: phy number end device attached to
  4897. * @is_pd: is this hidden raid component
  4898. *
  4899. * Creating end device object, stored in ioc->sas_device_list.
  4900. *
  4901. * Returns 0 for success, non-zero for failure.
  4902. */
  4903. static int
  4904. _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
  4905. u8 is_pd)
  4906. {
  4907. Mpi2ConfigReply_t mpi_reply;
  4908. Mpi2SasDevicePage0_t sas_device_pg0;
  4909. Mpi2SasEnclosurePage0_t enclosure_pg0;
  4910. struct _sas_device *sas_device;
  4911. u32 ioc_status;
  4912. u64 sas_address;
  4913. u32 device_info;
  4914. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4915. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  4916. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4917. ioc->name, __FILE__, __LINE__, __func__);
  4918. return -1;
  4919. }
  4920. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4921. MPI2_IOCSTATUS_MASK;
  4922. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4923. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4924. ioc->name, __FILE__, __LINE__, __func__);
  4925. return -1;
  4926. }
  4927. /* check if this is end device */
  4928. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  4929. if (!(_scsih_is_end_device(device_info)))
  4930. return -1;
  4931. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  4932. /* check if device is present */
  4933. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  4934. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  4935. pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
  4936. ioc->name, handle);
  4937. return -1;
  4938. }
  4939. /* check if there were any issues with discovery */
  4940. if (_scsih_check_access_status(ioc, sas_address, handle,
  4941. sas_device_pg0.AccessStatus))
  4942. return -1;
  4943. sas_device = mpt3sas_get_sdev_by_addr(ioc,
  4944. sas_address);
  4945. if (sas_device) {
  4946. sas_device_put(sas_device);
  4947. return -1;
  4948. }
  4949. sas_device = kzalloc(sizeof(struct _sas_device),
  4950. GFP_KERNEL);
  4951. if (!sas_device) {
  4952. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4953. ioc->name, __FILE__, __LINE__, __func__);
  4954. return 0;
  4955. }
  4956. kref_init(&sas_device->refcount);
  4957. sas_device->handle = handle;
  4958. if (_scsih_get_sas_address(ioc,
  4959. le16_to_cpu(sas_device_pg0.ParentDevHandle),
  4960. &sas_device->sas_address_parent) != 0)
  4961. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  4962. ioc->name, __FILE__, __LINE__, __func__);
  4963. sas_device->enclosure_handle =
  4964. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  4965. if (sas_device->enclosure_handle != 0)
  4966. sas_device->slot =
  4967. le16_to_cpu(sas_device_pg0.Slot);
  4968. sas_device->device_info = device_info;
  4969. sas_device->sas_address = sas_address;
  4970. sas_device->phy = sas_device_pg0.PhyNum;
  4971. sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
  4972. MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
  4973. if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
  4974. sas_device->enclosure_level =
  4975. le16_to_cpu(sas_device_pg0.EnclosureLevel);
  4976. memcpy(sas_device->connector_name,
  4977. sas_device_pg0.ConnectorName, 4);
  4978. sas_device->connector_name[4] = '\0';
  4979. } else {
  4980. sas_device->enclosure_level = 0;
  4981. sas_device->connector_name[0] = '\0';
  4982. }
  4983. /* get enclosure_logical_id */
  4984. if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
  4985. ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  4986. sas_device->enclosure_handle)))
  4987. sas_device->enclosure_logical_id =
  4988. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  4989. /* get device name */
  4990. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  4991. if (ioc->wait_for_discovery_to_complete)
  4992. _scsih_sas_device_init_add(ioc, sas_device);
  4993. else
  4994. _scsih_sas_device_add(ioc, sas_device);
  4995. sas_device_put(sas_device);
  4996. return 0;
  4997. }
  4998. /**
  4999. * _scsih_remove_device - removing sas device object
  5000. * @ioc: per adapter object
  5001. * @sas_device_delete: the sas_device object
  5002. *
  5003. * Return nothing.
  5004. */
  5005. static void
  5006. _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
  5007. struct _sas_device *sas_device)
  5008. {
  5009. struct MPT3SAS_TARGET *sas_target_priv_data;
  5010. if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
  5011. (sas_device->pfa_led_on)) {
  5012. _scsih_turn_off_pfa_led(ioc, sas_device);
  5013. sas_device->pfa_led_on = 0;
  5014. }
  5015. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5016. "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
  5017. ioc->name, __func__,
  5018. sas_device->handle, (unsigned long long)
  5019. sas_device->sas_address));
  5020. if (sas_device->enclosure_handle != 0)
  5021. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5022. "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
  5023. ioc->name, __func__,
  5024. (unsigned long long)sas_device->enclosure_logical_id,
  5025. sas_device->slot));
  5026. if (sas_device->connector_name[0] != '\0')
  5027. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5028. "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
  5029. ioc->name, __func__,
  5030. sas_device->enclosure_level,
  5031. sas_device->connector_name));
  5032. if (sas_device->starget && sas_device->starget->hostdata) {
  5033. sas_target_priv_data = sas_device->starget->hostdata;
  5034. sas_target_priv_data->deleted = 1;
  5035. _scsih_ublock_io_device(ioc, sas_device->sas_address);
  5036. sas_target_priv_data->handle =
  5037. MPT3SAS_INVALID_DEVICE_HANDLE;
  5038. }
  5039. if (!ioc->hide_drives)
  5040. mpt3sas_transport_port_remove(ioc,
  5041. sas_device->sas_address,
  5042. sas_device->sas_address_parent);
  5043. pr_info(MPT3SAS_FMT
  5044. "removing handle(0x%04x), sas_addr(0x%016llx)\n",
  5045. ioc->name, sas_device->handle,
  5046. (unsigned long long) sas_device->sas_address);
  5047. if (sas_device->enclosure_handle != 0)
  5048. pr_info(MPT3SAS_FMT
  5049. "removing : enclosure logical id(0x%016llx), slot(%d)\n",
  5050. ioc->name,
  5051. (unsigned long long)sas_device->enclosure_logical_id,
  5052. sas_device->slot);
  5053. if (sas_device->connector_name[0] != '\0')
  5054. pr_info(MPT3SAS_FMT
  5055. "removing enclosure level(0x%04x), connector name( %s)\n",
  5056. ioc->name, sas_device->enclosure_level,
  5057. sas_device->connector_name);
  5058. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5059. "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
  5060. ioc->name, __func__,
  5061. sas_device->handle, (unsigned long long)
  5062. sas_device->sas_address));
  5063. if (sas_device->enclosure_handle != 0)
  5064. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5065. "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
  5066. ioc->name, __func__,
  5067. (unsigned long long)sas_device->enclosure_logical_id,
  5068. sas_device->slot));
  5069. if (sas_device->connector_name[0] != '\0')
  5070. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5071. "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
  5072. ioc->name, __func__, sas_device->enclosure_level,
  5073. sas_device->connector_name));
  5074. }
  5075. /**
  5076. * _scsih_sas_topology_change_event_debug - debug for topology event
  5077. * @ioc: per adapter object
  5078. * @event_data: event data payload
  5079. * Context: user.
  5080. */
  5081. static void
  5082. _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  5083. Mpi2EventDataSasTopologyChangeList_t *event_data)
  5084. {
  5085. int i;
  5086. u16 handle;
  5087. u16 reason_code;
  5088. u8 phy_number;
  5089. char *status_str = NULL;
  5090. u8 link_rate, prev_link_rate;
  5091. switch (event_data->ExpStatus) {
  5092. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  5093. status_str = "add";
  5094. break;
  5095. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  5096. status_str = "remove";
  5097. break;
  5098. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  5099. case 0:
  5100. status_str = "responding";
  5101. break;
  5102. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  5103. status_str = "remove delay";
  5104. break;
  5105. default:
  5106. status_str = "unknown status";
  5107. break;
  5108. }
  5109. pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
  5110. ioc->name, status_str);
  5111. pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
  5112. "start_phy(%02d), count(%d)\n",
  5113. le16_to_cpu(event_data->ExpanderDevHandle),
  5114. le16_to_cpu(event_data->EnclosureHandle),
  5115. event_data->StartPhyNum, event_data->NumEntries);
  5116. for (i = 0; i < event_data->NumEntries; i++) {
  5117. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  5118. if (!handle)
  5119. continue;
  5120. phy_number = event_data->StartPhyNum + i;
  5121. reason_code = event_data->PHY[i].PhyStatus &
  5122. MPI2_EVENT_SAS_TOPO_RC_MASK;
  5123. switch (reason_code) {
  5124. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  5125. status_str = "target add";
  5126. break;
  5127. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  5128. status_str = "target remove";
  5129. break;
  5130. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  5131. status_str = "delay target remove";
  5132. break;
  5133. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  5134. status_str = "link rate change";
  5135. break;
  5136. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  5137. status_str = "target responding";
  5138. break;
  5139. default:
  5140. status_str = "unknown";
  5141. break;
  5142. }
  5143. link_rate = event_data->PHY[i].LinkRate >> 4;
  5144. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  5145. pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
  5146. " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
  5147. handle, status_str, link_rate, prev_link_rate);
  5148. }
  5149. }
  5150. /**
  5151. * _scsih_sas_topology_change_event - handle topology changes
  5152. * @ioc: per adapter object
  5153. * @fw_event: The fw_event_work object
  5154. * Context: user.
  5155. *
  5156. */
  5157. static int
  5158. _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
  5159. struct fw_event_work *fw_event)
  5160. {
  5161. int i;
  5162. u16 parent_handle, handle;
  5163. u16 reason_code;
  5164. u8 phy_number, max_phys;
  5165. struct _sas_node *sas_expander;
  5166. u64 sas_address;
  5167. unsigned long flags;
  5168. u8 link_rate, prev_link_rate;
  5169. Mpi2EventDataSasTopologyChangeList_t *event_data =
  5170. (Mpi2EventDataSasTopologyChangeList_t *)
  5171. fw_event->event_data;
  5172. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5173. _scsih_sas_topology_change_event_debug(ioc, event_data);
  5174. if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
  5175. return 0;
  5176. if (!ioc->sas_hba.num_phys)
  5177. _scsih_sas_host_add(ioc);
  5178. else
  5179. _scsih_sas_host_refresh(ioc);
  5180. if (fw_event->ignore) {
  5181. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5182. "ignoring expander event\n", ioc->name));
  5183. return 0;
  5184. }
  5185. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  5186. /* handle expander add */
  5187. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  5188. if (_scsih_expander_add(ioc, parent_handle) != 0)
  5189. return 0;
  5190. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5191. sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
  5192. parent_handle);
  5193. if (sas_expander) {
  5194. sas_address = sas_expander->sas_address;
  5195. max_phys = sas_expander->num_phys;
  5196. } else if (parent_handle < ioc->sas_hba.num_phys) {
  5197. sas_address = ioc->sas_hba.sas_address;
  5198. max_phys = ioc->sas_hba.num_phys;
  5199. } else {
  5200. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5201. return 0;
  5202. }
  5203. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5204. /* handle siblings events */
  5205. for (i = 0; i < event_data->NumEntries; i++) {
  5206. if (fw_event->ignore) {
  5207. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5208. "ignoring expander event\n", ioc->name));
  5209. return 0;
  5210. }
  5211. if (ioc->remove_host || ioc->pci_error_recovery)
  5212. return 0;
  5213. phy_number = event_data->StartPhyNum + i;
  5214. if (phy_number >= max_phys)
  5215. continue;
  5216. reason_code = event_data->PHY[i].PhyStatus &
  5217. MPI2_EVENT_SAS_TOPO_RC_MASK;
  5218. if ((event_data->PHY[i].PhyStatus &
  5219. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
  5220. MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
  5221. continue;
  5222. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  5223. if (!handle)
  5224. continue;
  5225. link_rate = event_data->PHY[i].LinkRate >> 4;
  5226. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  5227. switch (reason_code) {
  5228. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  5229. if (ioc->shost_recovery)
  5230. break;
  5231. if (link_rate == prev_link_rate)
  5232. break;
  5233. mpt3sas_transport_update_links(ioc, sas_address,
  5234. handle, phy_number, link_rate);
  5235. if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  5236. break;
  5237. _scsih_check_device(ioc, sas_address, handle,
  5238. phy_number, link_rate);
  5239. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  5240. if (ioc->shost_recovery)
  5241. break;
  5242. mpt3sas_transport_update_links(ioc, sas_address,
  5243. handle, phy_number, link_rate);
  5244. _scsih_add_device(ioc, handle, phy_number, 0);
  5245. break;
  5246. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  5247. _scsih_device_remove_by_handle(ioc, handle);
  5248. break;
  5249. }
  5250. }
  5251. /* handle expander removal */
  5252. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
  5253. sas_expander)
  5254. mpt3sas_expander_remove(ioc, sas_address);
  5255. return 0;
  5256. }
  5257. /**
  5258. * _scsih_sas_device_status_change_event_debug - debug for device event
  5259. * @event_data: event data payload
  5260. * Context: user.
  5261. *
  5262. * Return nothing.
  5263. */
  5264. static void
  5265. _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  5266. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  5267. {
  5268. char *reason_str = NULL;
  5269. switch (event_data->ReasonCode) {
  5270. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  5271. reason_str = "smart data";
  5272. break;
  5273. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  5274. reason_str = "unsupported device discovered";
  5275. break;
  5276. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  5277. reason_str = "internal device reset";
  5278. break;
  5279. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  5280. reason_str = "internal task abort";
  5281. break;
  5282. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  5283. reason_str = "internal task abort set";
  5284. break;
  5285. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  5286. reason_str = "internal clear task set";
  5287. break;
  5288. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  5289. reason_str = "internal query task";
  5290. break;
  5291. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  5292. reason_str = "sata init failure";
  5293. break;
  5294. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  5295. reason_str = "internal device reset complete";
  5296. break;
  5297. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  5298. reason_str = "internal task abort complete";
  5299. break;
  5300. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  5301. reason_str = "internal async notification";
  5302. break;
  5303. case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
  5304. reason_str = "expander reduced functionality";
  5305. break;
  5306. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
  5307. reason_str = "expander reduced functionality complete";
  5308. break;
  5309. default:
  5310. reason_str = "unknown reason";
  5311. break;
  5312. }
  5313. pr_info(MPT3SAS_FMT "device status change: (%s)\n"
  5314. "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
  5315. ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
  5316. (unsigned long long)le64_to_cpu(event_data->SASAddress),
  5317. le16_to_cpu(event_data->TaskTag));
  5318. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  5319. pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  5320. event_data->ASC, event_data->ASCQ);
  5321. pr_info("\n");
  5322. }
  5323. /**
  5324. * _scsih_sas_device_status_change_event - handle device status change
  5325. * @ioc: per adapter object
  5326. * @fw_event: The fw_event_work object
  5327. * Context: user.
  5328. *
  5329. * Return nothing.
  5330. */
  5331. static void
  5332. _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
  5333. struct fw_event_work *fw_event)
  5334. {
  5335. struct MPT3SAS_TARGET *target_priv_data;
  5336. struct _sas_device *sas_device;
  5337. u64 sas_address;
  5338. unsigned long flags;
  5339. Mpi2EventDataSasDeviceStatusChange_t *event_data =
  5340. (Mpi2EventDataSasDeviceStatusChange_t *)
  5341. fw_event->event_data;
  5342. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5343. _scsih_sas_device_status_change_event_debug(ioc,
  5344. event_data);
  5345. /* In MPI Revision K (0xC), the internal device reset complete was
  5346. * implemented, so avoid setting tm_busy flag for older firmware.
  5347. */
  5348. if ((ioc->facts.HeaderVersion >> 8) < 0xC)
  5349. return;
  5350. if (event_data->ReasonCode !=
  5351. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  5352. event_data->ReasonCode !=
  5353. MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
  5354. return;
  5355. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5356. sas_address = le64_to_cpu(event_data->SASAddress);
  5357. sas_device = __mpt3sas_get_sdev_by_addr(ioc,
  5358. sas_address);
  5359. if (!sas_device || !sas_device->starget)
  5360. goto out;
  5361. target_priv_data = sas_device->starget->hostdata;
  5362. if (!target_priv_data)
  5363. goto out;
  5364. if (event_data->ReasonCode ==
  5365. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
  5366. target_priv_data->tm_busy = 1;
  5367. else
  5368. target_priv_data->tm_busy = 0;
  5369. out:
  5370. if (sas_device)
  5371. sas_device_put(sas_device);
  5372. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5373. }
  5374. /**
  5375. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
  5376. * event
  5377. * @ioc: per adapter object
  5378. * @event_data: event data payload
  5379. * Context: user.
  5380. *
  5381. * Return nothing.
  5382. */
  5383. static void
  5384. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  5385. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  5386. {
  5387. char *reason_str = NULL;
  5388. switch (event_data->ReasonCode) {
  5389. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  5390. reason_str = "enclosure add";
  5391. break;
  5392. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  5393. reason_str = "enclosure remove";
  5394. break;
  5395. default:
  5396. reason_str = "unknown reason";
  5397. break;
  5398. }
  5399. pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
  5400. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  5401. " number slots(%d)\n", ioc->name, reason_str,
  5402. le16_to_cpu(event_data->EnclosureHandle),
  5403. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  5404. le16_to_cpu(event_data->StartSlot));
  5405. }
  5406. /**
  5407. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  5408. * @ioc: per adapter object
  5409. * @fw_event: The fw_event_work object
  5410. * Context: user.
  5411. *
  5412. * Return nothing.
  5413. */
  5414. static void
  5415. _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
  5416. struct fw_event_work *fw_event)
  5417. {
  5418. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  5419. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  5420. (Mpi2EventDataSasEnclDevStatusChange_t *)
  5421. fw_event->event_data);
  5422. }
  5423. /**
  5424. * _scsih_sas_broadcast_primitive_event - handle broadcast events
  5425. * @ioc: per adapter object
  5426. * @fw_event: The fw_event_work object
  5427. * Context: user.
  5428. *
  5429. * Return nothing.
  5430. */
  5431. static void
  5432. _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
  5433. struct fw_event_work *fw_event)
  5434. {
  5435. struct scsi_cmnd *scmd;
  5436. struct scsi_device *sdev;
  5437. u16 smid, handle;
  5438. u32 lun;
  5439. struct MPT3SAS_DEVICE *sas_device_priv_data;
  5440. u32 termination_count;
  5441. u32 query_count;
  5442. Mpi2SCSITaskManagementReply_t *mpi_reply;
  5443. Mpi2EventDataSasBroadcastPrimitive_t *event_data =
  5444. (Mpi2EventDataSasBroadcastPrimitive_t *)
  5445. fw_event->event_data;
  5446. u16 ioc_status;
  5447. unsigned long flags;
  5448. int r;
  5449. u8 max_retries = 0;
  5450. u8 task_abort_retries;
  5451. mutex_lock(&ioc->tm_cmds.mutex);
  5452. pr_info(MPT3SAS_FMT
  5453. "%s: enter: phy number(%d), width(%d)\n",
  5454. ioc->name, __func__, event_data->PhyNum,
  5455. event_data->PortWidth);
  5456. _scsih_block_io_all_device(ioc);
  5457. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5458. mpi_reply = ioc->tm_cmds.reply;
  5459. broadcast_aen_retry:
  5460. /* sanity checks for retrying this loop */
  5461. if (max_retries++ == 5) {
  5462. dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
  5463. ioc->name, __func__));
  5464. goto out;
  5465. } else if (max_retries > 1)
  5466. dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
  5467. ioc->name, __func__, max_retries - 1));
  5468. termination_count = 0;
  5469. query_count = 0;
  5470. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  5471. if (ioc->shost_recovery)
  5472. goto out;
  5473. scmd = _scsih_scsi_lookup_get(ioc, smid);
  5474. if (!scmd)
  5475. continue;
  5476. sdev = scmd->device;
  5477. sas_device_priv_data = sdev->hostdata;
  5478. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  5479. continue;
  5480. /* skip hidden raid components */
  5481. if (sas_device_priv_data->sas_target->flags &
  5482. MPT_TARGET_FLAGS_RAID_COMPONENT)
  5483. continue;
  5484. /* skip volumes */
  5485. if (sas_device_priv_data->sas_target->flags &
  5486. MPT_TARGET_FLAGS_VOLUME)
  5487. continue;
  5488. handle = sas_device_priv_data->sas_target->handle;
  5489. lun = sas_device_priv_data->lun;
  5490. query_count++;
  5491. if (ioc->shost_recovery)
  5492. goto out;
  5493. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  5494. r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
  5495. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
  5496. if (r == FAILED) {
  5497. sdev_printk(KERN_WARNING, sdev,
  5498. "mpt3sas_scsih_issue_tm: FAILED when sending "
  5499. "QUERY_TASK: scmd(%p)\n", scmd);
  5500. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5501. goto broadcast_aen_retry;
  5502. }
  5503. ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
  5504. & MPI2_IOCSTATUS_MASK;
  5505. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5506. sdev_printk(KERN_WARNING, sdev,
  5507. "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
  5508. ioc_status, scmd);
  5509. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5510. goto broadcast_aen_retry;
  5511. }
  5512. /* see if IO is still owned by IOC and target */
  5513. if (mpi_reply->ResponseCode ==
  5514. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  5515. mpi_reply->ResponseCode ==
  5516. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
  5517. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5518. continue;
  5519. }
  5520. task_abort_retries = 0;
  5521. tm_retry:
  5522. if (task_abort_retries++ == 60) {
  5523. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5524. "%s: ABORT_TASK: giving up\n", ioc->name,
  5525. __func__));
  5526. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5527. goto broadcast_aen_retry;
  5528. }
  5529. if (ioc->shost_recovery)
  5530. goto out_no_lock;
  5531. r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
  5532. sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid,
  5533. 30);
  5534. if (r == FAILED) {
  5535. sdev_printk(KERN_WARNING, sdev,
  5536. "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
  5537. "scmd(%p)\n", scmd);
  5538. goto tm_retry;
  5539. }
  5540. if (task_abort_retries > 1)
  5541. sdev_printk(KERN_WARNING, sdev,
  5542. "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
  5543. " scmd(%p)\n",
  5544. task_abort_retries - 1, scmd);
  5545. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  5546. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  5547. }
  5548. if (ioc->broadcast_aen_pending) {
  5549. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5550. "%s: loop back due to pending AEN\n",
  5551. ioc->name, __func__));
  5552. ioc->broadcast_aen_pending = 0;
  5553. goto broadcast_aen_retry;
  5554. }
  5555. out:
  5556. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  5557. out_no_lock:
  5558. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5559. "%s - exit, query_count = %d termination_count = %d\n",
  5560. ioc->name, __func__, query_count, termination_count));
  5561. ioc->broadcast_aen_busy = 0;
  5562. if (!ioc->shost_recovery)
  5563. _scsih_ublock_io_all_device(ioc);
  5564. mutex_unlock(&ioc->tm_cmds.mutex);
  5565. }
  5566. /**
  5567. * _scsih_sas_discovery_event - handle discovery events
  5568. * @ioc: per adapter object
  5569. * @fw_event: The fw_event_work object
  5570. * Context: user.
  5571. *
  5572. * Return nothing.
  5573. */
  5574. static void
  5575. _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
  5576. struct fw_event_work *fw_event)
  5577. {
  5578. Mpi2EventDataSasDiscovery_t *event_data =
  5579. (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
  5580. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  5581. pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
  5582. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  5583. "start" : "stop");
  5584. if (event_data->DiscoveryStatus)
  5585. pr_info("discovery_status(0x%08x)",
  5586. le32_to_cpu(event_data->DiscoveryStatus));
  5587. pr_info("\n");
  5588. }
  5589. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  5590. !ioc->sas_hba.num_phys) {
  5591. if (disable_discovery > 0 && ioc->shost_recovery) {
  5592. /* Wait for the reset to complete */
  5593. while (ioc->shost_recovery)
  5594. ssleep(1);
  5595. }
  5596. _scsih_sas_host_add(ioc);
  5597. }
  5598. }
  5599. /**
  5600. * _scsih_ir_fastpath - turn on fastpath for IR physdisk
  5601. * @ioc: per adapter object
  5602. * @handle: device handle for physical disk
  5603. * @phys_disk_num: physical disk number
  5604. *
  5605. * Return 0 for success, else failure.
  5606. */
  5607. static int
  5608. _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
  5609. {
  5610. Mpi2RaidActionRequest_t *mpi_request;
  5611. Mpi2RaidActionReply_t *mpi_reply;
  5612. u16 smid;
  5613. u8 issue_reset = 0;
  5614. int rc = 0;
  5615. u16 ioc_status;
  5616. u32 log_info;
  5617. if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
  5618. return rc;
  5619. mutex_lock(&ioc->scsih_cmds.mutex);
  5620. if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
  5621. pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
  5622. ioc->name, __func__);
  5623. rc = -EAGAIN;
  5624. goto out;
  5625. }
  5626. ioc->scsih_cmds.status = MPT3_CMD_PENDING;
  5627. smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  5628. if (!smid) {
  5629. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  5630. ioc->name, __func__);
  5631. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  5632. rc = -EAGAIN;
  5633. goto out;
  5634. }
  5635. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  5636. ioc->scsih_cmds.smid = smid;
  5637. memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
  5638. mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
  5639. mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
  5640. mpi_request->PhysDiskNum = phys_disk_num;
  5641. dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
  5642. "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
  5643. handle, phys_disk_num));
  5644. init_completion(&ioc->scsih_cmds.done);
  5645. mpt3sas_base_put_smid_default(ioc, smid);
  5646. wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
  5647. if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
  5648. pr_err(MPT3SAS_FMT "%s: timeout\n",
  5649. ioc->name, __func__);
  5650. if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
  5651. issue_reset = 1;
  5652. rc = -EFAULT;
  5653. goto out;
  5654. }
  5655. if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
  5656. mpi_reply = ioc->scsih_cmds.reply;
  5657. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  5658. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  5659. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  5660. else
  5661. log_info = 0;
  5662. ioc_status &= MPI2_IOCSTATUS_MASK;
  5663. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5664. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5665. "IR RAID_ACTION: failed: ioc_status(0x%04x), "
  5666. "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
  5667. log_info));
  5668. rc = -EFAULT;
  5669. } else
  5670. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  5671. "IR RAID_ACTION: completed successfully\n",
  5672. ioc->name));
  5673. }
  5674. out:
  5675. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  5676. mutex_unlock(&ioc->scsih_cmds.mutex);
  5677. if (issue_reset)
  5678. mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  5679. return rc;
  5680. }
  5681. /**
  5682. * _scsih_reprobe_lun - reprobing lun
  5683. * @sdev: scsi device struct
  5684. * @no_uld_attach: sdev->no_uld_attach flag setting
  5685. *
  5686. **/
  5687. static void
  5688. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  5689. {
  5690. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  5691. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  5692. sdev->no_uld_attach ? "hidding" : "exposing");
  5693. WARN_ON(scsi_device_reprobe(sdev));
  5694. }
  5695. /**
  5696. * _scsih_sas_volume_add - add new volume
  5697. * @ioc: per adapter object
  5698. * @element: IR config element data
  5699. * Context: user.
  5700. *
  5701. * Return nothing.
  5702. */
  5703. static void
  5704. _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
  5705. Mpi2EventIrConfigElement_t *element)
  5706. {
  5707. struct _raid_device *raid_device;
  5708. unsigned long flags;
  5709. u64 wwid;
  5710. u16 handle = le16_to_cpu(element->VolDevHandle);
  5711. int rc;
  5712. mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
  5713. if (!wwid) {
  5714. pr_err(MPT3SAS_FMT
  5715. "failure at %s:%d/%s()!\n", ioc->name,
  5716. __FILE__, __LINE__, __func__);
  5717. return;
  5718. }
  5719. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5720. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  5721. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5722. if (raid_device)
  5723. return;
  5724. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  5725. if (!raid_device) {
  5726. pr_err(MPT3SAS_FMT
  5727. "failure at %s:%d/%s()!\n", ioc->name,
  5728. __FILE__, __LINE__, __func__);
  5729. return;
  5730. }
  5731. raid_device->id = ioc->sas_id++;
  5732. raid_device->channel = RAID_CHANNEL;
  5733. raid_device->handle = handle;
  5734. raid_device->wwid = wwid;
  5735. _scsih_raid_device_add(ioc, raid_device);
  5736. if (!ioc->wait_for_discovery_to_complete) {
  5737. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5738. raid_device->id, 0);
  5739. if (rc)
  5740. _scsih_raid_device_remove(ioc, raid_device);
  5741. } else {
  5742. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5743. _scsih_determine_boot_device(ioc, raid_device, 1);
  5744. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5745. }
  5746. }
  5747. /**
  5748. * _scsih_sas_volume_delete - delete volume
  5749. * @ioc: per adapter object
  5750. * @handle: volume device handle
  5751. * Context: user.
  5752. *
  5753. * Return nothing.
  5754. */
  5755. static void
  5756. _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
  5757. {
  5758. struct _raid_device *raid_device;
  5759. unsigned long flags;
  5760. struct MPT3SAS_TARGET *sas_target_priv_data;
  5761. struct scsi_target *starget = NULL;
  5762. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5763. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  5764. if (raid_device) {
  5765. if (raid_device->starget) {
  5766. starget = raid_device->starget;
  5767. sas_target_priv_data = starget->hostdata;
  5768. sas_target_priv_data->deleted = 1;
  5769. }
  5770. pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
  5771. ioc->name, raid_device->handle,
  5772. (unsigned long long) raid_device->wwid);
  5773. list_del(&raid_device->list);
  5774. kfree(raid_device);
  5775. }
  5776. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5777. if (starget)
  5778. scsi_remove_target(&starget->dev);
  5779. }
  5780. /**
  5781. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  5782. * @ioc: per adapter object
  5783. * @element: IR config element data
  5784. * Context: user.
  5785. *
  5786. * Return nothing.
  5787. */
  5788. static void
  5789. _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
  5790. Mpi2EventIrConfigElement_t *element)
  5791. {
  5792. struct _sas_device *sas_device;
  5793. struct scsi_target *starget = NULL;
  5794. struct MPT3SAS_TARGET *sas_target_priv_data;
  5795. unsigned long flags;
  5796. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5797. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5798. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  5799. if (sas_device) {
  5800. sas_device->volume_handle = 0;
  5801. sas_device->volume_wwid = 0;
  5802. clear_bit(handle, ioc->pd_handles);
  5803. if (sas_device->starget && sas_device->starget->hostdata) {
  5804. starget = sas_device->starget;
  5805. sas_target_priv_data = starget->hostdata;
  5806. sas_target_priv_data->flags &=
  5807. ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  5808. }
  5809. }
  5810. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5811. if (!sas_device)
  5812. return;
  5813. /* exposing raid component */
  5814. if (starget)
  5815. starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
  5816. sas_device_put(sas_device);
  5817. }
  5818. /**
  5819. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  5820. * @ioc: per adapter object
  5821. * @element: IR config element data
  5822. * Context: user.
  5823. *
  5824. * Return nothing.
  5825. */
  5826. static void
  5827. _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
  5828. Mpi2EventIrConfigElement_t *element)
  5829. {
  5830. struct _sas_device *sas_device;
  5831. struct scsi_target *starget = NULL;
  5832. struct MPT3SAS_TARGET *sas_target_priv_data;
  5833. unsigned long flags;
  5834. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5835. u16 volume_handle = 0;
  5836. u64 volume_wwid = 0;
  5837. mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
  5838. if (volume_handle)
  5839. mpt3sas_config_get_volume_wwid(ioc, volume_handle,
  5840. &volume_wwid);
  5841. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5842. sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
  5843. if (sas_device) {
  5844. set_bit(handle, ioc->pd_handles);
  5845. if (sas_device->starget && sas_device->starget->hostdata) {
  5846. starget = sas_device->starget;
  5847. sas_target_priv_data = starget->hostdata;
  5848. sas_target_priv_data->flags |=
  5849. MPT_TARGET_FLAGS_RAID_COMPONENT;
  5850. sas_device->volume_handle = volume_handle;
  5851. sas_device->volume_wwid = volume_wwid;
  5852. }
  5853. }
  5854. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5855. if (!sas_device)
  5856. return;
  5857. /* hiding raid component */
  5858. _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
  5859. if (starget)
  5860. starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
  5861. sas_device_put(sas_device);
  5862. }
  5863. /**
  5864. * _scsih_sas_pd_delete - delete pd component
  5865. * @ioc: per adapter object
  5866. * @element: IR config element data
  5867. * Context: user.
  5868. *
  5869. * Return nothing.
  5870. */
  5871. static void
  5872. _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
  5873. Mpi2EventIrConfigElement_t *element)
  5874. {
  5875. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5876. _scsih_device_remove_by_handle(ioc, handle);
  5877. }
  5878. /**
  5879. * _scsih_sas_pd_add - remove pd component
  5880. * @ioc: per adapter object
  5881. * @element: IR config element data
  5882. * Context: user.
  5883. *
  5884. * Return nothing.
  5885. */
  5886. static void
  5887. _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
  5888. Mpi2EventIrConfigElement_t *element)
  5889. {
  5890. struct _sas_device *sas_device;
  5891. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  5892. Mpi2ConfigReply_t mpi_reply;
  5893. Mpi2SasDevicePage0_t sas_device_pg0;
  5894. u32 ioc_status;
  5895. u64 sas_address;
  5896. u16 parent_handle;
  5897. set_bit(handle, ioc->pd_handles);
  5898. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  5899. if (sas_device) {
  5900. _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
  5901. sas_device_put(sas_device);
  5902. return;
  5903. }
  5904. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  5905. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  5906. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5907. ioc->name, __FILE__, __LINE__, __func__);
  5908. return;
  5909. }
  5910. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5911. MPI2_IOCSTATUS_MASK;
  5912. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  5913. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  5914. ioc->name, __FILE__, __LINE__, __func__);
  5915. return;
  5916. }
  5917. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  5918. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  5919. mpt3sas_transport_update_links(ioc, sas_address, handle,
  5920. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  5921. _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
  5922. _scsih_add_device(ioc, handle, 0, 1);
  5923. }
  5924. /**
  5925. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  5926. * @ioc: per adapter object
  5927. * @event_data: event data payload
  5928. * Context: user.
  5929. *
  5930. * Return nothing.
  5931. */
  5932. static void
  5933. _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
  5934. Mpi2EventDataIrConfigChangeList_t *event_data)
  5935. {
  5936. Mpi2EventIrConfigElement_t *element;
  5937. u8 element_type;
  5938. int i;
  5939. char *reason_str = NULL, *element_str = NULL;
  5940. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  5941. pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
  5942. ioc->name, (le32_to_cpu(event_data->Flags) &
  5943. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  5944. "foreign" : "native", event_data->NumElements);
  5945. for (i = 0; i < event_data->NumElements; i++, element++) {
  5946. switch (element->ReasonCode) {
  5947. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  5948. reason_str = "add";
  5949. break;
  5950. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  5951. reason_str = "remove";
  5952. break;
  5953. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  5954. reason_str = "no change";
  5955. break;
  5956. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  5957. reason_str = "hide";
  5958. break;
  5959. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  5960. reason_str = "unhide";
  5961. break;
  5962. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  5963. reason_str = "volume_created";
  5964. break;
  5965. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  5966. reason_str = "volume_deleted";
  5967. break;
  5968. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  5969. reason_str = "pd_created";
  5970. break;
  5971. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  5972. reason_str = "pd_deleted";
  5973. break;
  5974. default:
  5975. reason_str = "unknown reason";
  5976. break;
  5977. }
  5978. element_type = le16_to_cpu(element->ElementFlags) &
  5979. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  5980. switch (element_type) {
  5981. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  5982. element_str = "volume";
  5983. break;
  5984. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  5985. element_str = "phys disk";
  5986. break;
  5987. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  5988. element_str = "hot spare";
  5989. break;
  5990. default:
  5991. element_str = "unknown element";
  5992. break;
  5993. }
  5994. pr_info("\t(%s:%s), vol handle(0x%04x), " \
  5995. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  5996. reason_str, le16_to_cpu(element->VolDevHandle),
  5997. le16_to_cpu(element->PhysDiskDevHandle),
  5998. element->PhysDiskNum);
  5999. }
  6000. }
  6001. /**
  6002. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  6003. * @ioc: per adapter object
  6004. * @fw_event: The fw_event_work object
  6005. * Context: user.
  6006. *
  6007. * Return nothing.
  6008. */
  6009. static void
  6010. _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
  6011. struct fw_event_work *fw_event)
  6012. {
  6013. Mpi2EventIrConfigElement_t *element;
  6014. int i;
  6015. u8 foreign_config;
  6016. Mpi2EventDataIrConfigChangeList_t *event_data =
  6017. (Mpi2EventDataIrConfigChangeList_t *)
  6018. fw_event->event_data;
  6019. if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
  6020. (!ioc->hide_ir_msg))
  6021. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  6022. foreign_config = (le32_to_cpu(event_data->Flags) &
  6023. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
  6024. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  6025. if (ioc->shost_recovery &&
  6026. ioc->hba_mpi_version_belonged != MPI2_VERSION) {
  6027. for (i = 0; i < event_data->NumElements; i++, element++) {
  6028. if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
  6029. _scsih_ir_fastpath(ioc,
  6030. le16_to_cpu(element->PhysDiskDevHandle),
  6031. element->PhysDiskNum);
  6032. }
  6033. return;
  6034. }
  6035. for (i = 0; i < event_data->NumElements; i++, element++) {
  6036. switch (element->ReasonCode) {
  6037. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  6038. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  6039. if (!foreign_config)
  6040. _scsih_sas_volume_add(ioc, element);
  6041. break;
  6042. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  6043. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  6044. if (!foreign_config)
  6045. _scsih_sas_volume_delete(ioc,
  6046. le16_to_cpu(element->VolDevHandle));
  6047. break;
  6048. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  6049. if (!ioc->is_warpdrive)
  6050. _scsih_sas_pd_hide(ioc, element);
  6051. break;
  6052. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  6053. if (!ioc->is_warpdrive)
  6054. _scsih_sas_pd_expose(ioc, element);
  6055. break;
  6056. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  6057. if (!ioc->is_warpdrive)
  6058. _scsih_sas_pd_add(ioc, element);
  6059. break;
  6060. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  6061. if (!ioc->is_warpdrive)
  6062. _scsih_sas_pd_delete(ioc, element);
  6063. break;
  6064. }
  6065. }
  6066. }
  6067. /**
  6068. * _scsih_sas_ir_volume_event - IR volume event
  6069. * @ioc: per adapter object
  6070. * @fw_event: The fw_event_work object
  6071. * Context: user.
  6072. *
  6073. * Return nothing.
  6074. */
  6075. static void
  6076. _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
  6077. struct fw_event_work *fw_event)
  6078. {
  6079. u64 wwid;
  6080. unsigned long flags;
  6081. struct _raid_device *raid_device;
  6082. u16 handle;
  6083. u32 state;
  6084. int rc;
  6085. Mpi2EventDataIrVolume_t *event_data =
  6086. (Mpi2EventDataIrVolume_t *) fw_event->event_data;
  6087. if (ioc->shost_recovery)
  6088. return;
  6089. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  6090. return;
  6091. handle = le16_to_cpu(event_data->VolDevHandle);
  6092. state = le32_to_cpu(event_data->NewValue);
  6093. if (!ioc->hide_ir_msg)
  6094. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6095. "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
  6096. ioc->name, __func__, handle,
  6097. le32_to_cpu(event_data->PreviousValue), state));
  6098. switch (state) {
  6099. case MPI2_RAID_VOL_STATE_MISSING:
  6100. case MPI2_RAID_VOL_STATE_FAILED:
  6101. _scsih_sas_volume_delete(ioc, handle);
  6102. break;
  6103. case MPI2_RAID_VOL_STATE_ONLINE:
  6104. case MPI2_RAID_VOL_STATE_DEGRADED:
  6105. case MPI2_RAID_VOL_STATE_OPTIMAL:
  6106. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6107. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  6108. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6109. if (raid_device)
  6110. break;
  6111. mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
  6112. if (!wwid) {
  6113. pr_err(MPT3SAS_FMT
  6114. "failure at %s:%d/%s()!\n", ioc->name,
  6115. __FILE__, __LINE__, __func__);
  6116. break;
  6117. }
  6118. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  6119. if (!raid_device) {
  6120. pr_err(MPT3SAS_FMT
  6121. "failure at %s:%d/%s()!\n", ioc->name,
  6122. __FILE__, __LINE__, __func__);
  6123. break;
  6124. }
  6125. raid_device->id = ioc->sas_id++;
  6126. raid_device->channel = RAID_CHANNEL;
  6127. raid_device->handle = handle;
  6128. raid_device->wwid = wwid;
  6129. _scsih_raid_device_add(ioc, raid_device);
  6130. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  6131. raid_device->id, 0);
  6132. if (rc)
  6133. _scsih_raid_device_remove(ioc, raid_device);
  6134. break;
  6135. case MPI2_RAID_VOL_STATE_INITIALIZING:
  6136. default:
  6137. break;
  6138. }
  6139. }
  6140. /**
  6141. * _scsih_sas_ir_physical_disk_event - PD event
  6142. * @ioc: per adapter object
  6143. * @fw_event: The fw_event_work object
  6144. * Context: user.
  6145. *
  6146. * Return nothing.
  6147. */
  6148. static void
  6149. _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
  6150. struct fw_event_work *fw_event)
  6151. {
  6152. u16 handle, parent_handle;
  6153. u32 state;
  6154. struct _sas_device *sas_device;
  6155. Mpi2ConfigReply_t mpi_reply;
  6156. Mpi2SasDevicePage0_t sas_device_pg0;
  6157. u32 ioc_status;
  6158. Mpi2EventDataIrPhysicalDisk_t *event_data =
  6159. (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
  6160. u64 sas_address;
  6161. if (ioc->shost_recovery)
  6162. return;
  6163. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  6164. return;
  6165. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  6166. state = le32_to_cpu(event_data->NewValue);
  6167. if (!ioc->hide_ir_msg)
  6168. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  6169. "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
  6170. ioc->name, __func__, handle,
  6171. le32_to_cpu(event_data->PreviousValue), state));
  6172. switch (state) {
  6173. case MPI2_RAID_PD_STATE_ONLINE:
  6174. case MPI2_RAID_PD_STATE_DEGRADED:
  6175. case MPI2_RAID_PD_STATE_REBUILDING:
  6176. case MPI2_RAID_PD_STATE_OPTIMAL:
  6177. case MPI2_RAID_PD_STATE_HOT_SPARE:
  6178. if (!ioc->is_warpdrive)
  6179. set_bit(handle, ioc->pd_handles);
  6180. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  6181. if (sas_device) {
  6182. sas_device_put(sas_device);
  6183. return;
  6184. }
  6185. if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  6186. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  6187. handle))) {
  6188. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  6189. ioc->name, __FILE__, __LINE__, __func__);
  6190. return;
  6191. }
  6192. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6193. MPI2_IOCSTATUS_MASK;
  6194. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6195. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  6196. ioc->name, __FILE__, __LINE__, __func__);
  6197. return;
  6198. }
  6199. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  6200. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  6201. mpt3sas_transport_update_links(ioc, sas_address, handle,
  6202. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  6203. _scsih_add_device(ioc, handle, 0, 1);
  6204. break;
  6205. case MPI2_RAID_PD_STATE_OFFLINE:
  6206. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  6207. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  6208. default:
  6209. break;
  6210. }
  6211. }
  6212. /**
  6213. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  6214. * @ioc: per adapter object
  6215. * @event_data: event data payload
  6216. * Context: user.
  6217. *
  6218. * Return nothing.
  6219. */
  6220. static void
  6221. _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
  6222. Mpi2EventDataIrOperationStatus_t *event_data)
  6223. {
  6224. char *reason_str = NULL;
  6225. switch (event_data->RAIDOperation) {
  6226. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  6227. reason_str = "resync";
  6228. break;
  6229. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  6230. reason_str = "online capacity expansion";
  6231. break;
  6232. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  6233. reason_str = "consistency check";
  6234. break;
  6235. case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
  6236. reason_str = "background init";
  6237. break;
  6238. case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
  6239. reason_str = "make data consistent";
  6240. break;
  6241. }
  6242. if (!reason_str)
  6243. return;
  6244. pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
  6245. "\thandle(0x%04x), percent complete(%d)\n",
  6246. ioc->name, reason_str,
  6247. le16_to_cpu(event_data->VolDevHandle),
  6248. event_data->PercentComplete);
  6249. }
  6250. /**
  6251. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  6252. * @ioc: per adapter object
  6253. * @fw_event: The fw_event_work object
  6254. * Context: user.
  6255. *
  6256. * Return nothing.
  6257. */
  6258. static void
  6259. _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
  6260. struct fw_event_work *fw_event)
  6261. {
  6262. Mpi2EventDataIrOperationStatus_t *event_data =
  6263. (Mpi2EventDataIrOperationStatus_t *)
  6264. fw_event->event_data;
  6265. static struct _raid_device *raid_device;
  6266. unsigned long flags;
  6267. u16 handle;
  6268. if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
  6269. (!ioc->hide_ir_msg))
  6270. _scsih_sas_ir_operation_status_event_debug(ioc,
  6271. event_data);
  6272. /* code added for raid transport support */
  6273. if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
  6274. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6275. handle = le16_to_cpu(event_data->VolDevHandle);
  6276. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  6277. if (raid_device)
  6278. raid_device->percent_complete =
  6279. event_data->PercentComplete;
  6280. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6281. }
  6282. }
  6283. /**
  6284. * _scsih_prep_device_scan - initialize parameters prior to device scan
  6285. * @ioc: per adapter object
  6286. *
  6287. * Set the deleted flag prior to device scan. If the device is found during
  6288. * the scan, then we clear the deleted flag.
  6289. */
  6290. static void
  6291. _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
  6292. {
  6293. struct MPT3SAS_DEVICE *sas_device_priv_data;
  6294. struct scsi_device *sdev;
  6295. shost_for_each_device(sdev, ioc->shost) {
  6296. sas_device_priv_data = sdev->hostdata;
  6297. if (sas_device_priv_data && sas_device_priv_data->sas_target)
  6298. sas_device_priv_data->sas_target->deleted = 1;
  6299. }
  6300. }
  6301. /**
  6302. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  6303. * @ioc: per adapter object
  6304. * @sas_device_pg0: SAS Device page 0
  6305. *
  6306. * After host reset, find out whether devices are still responding.
  6307. * Used in _scsih_remove_unresponsive_sas_devices.
  6308. *
  6309. * Return nothing.
  6310. */
  6311. static void
  6312. _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
  6313. Mpi2SasDevicePage0_t *sas_device_pg0)
  6314. {
  6315. struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
  6316. struct scsi_target *starget;
  6317. struct _sas_device *sas_device;
  6318. unsigned long flags;
  6319. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6320. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  6321. if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
  6322. (sas_device->slot == sas_device_pg0->Slot)) {
  6323. sas_device->responding = 1;
  6324. starget = sas_device->starget;
  6325. if (starget && starget->hostdata) {
  6326. sas_target_priv_data = starget->hostdata;
  6327. sas_target_priv_data->tm_busy = 0;
  6328. sas_target_priv_data->deleted = 0;
  6329. } else
  6330. sas_target_priv_data = NULL;
  6331. if (starget) {
  6332. starget_printk(KERN_INFO, starget,
  6333. "handle(0x%04x), sas_addr(0x%016llx)\n",
  6334. sas_device_pg0->DevHandle,
  6335. (unsigned long long)
  6336. sas_device->sas_address);
  6337. if (sas_device->enclosure_handle != 0)
  6338. starget_printk(KERN_INFO, starget,
  6339. "enclosure logical id(0x%016llx),"
  6340. " slot(%d)\n",
  6341. (unsigned long long)
  6342. sas_device->enclosure_logical_id,
  6343. sas_device->slot);
  6344. }
  6345. if (sas_device_pg0->Flags &
  6346. MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
  6347. sas_device->enclosure_level =
  6348. le16_to_cpu(sas_device_pg0->EnclosureLevel);
  6349. memcpy(&sas_device->connector_name[0],
  6350. &sas_device_pg0->ConnectorName[0], 4);
  6351. } else {
  6352. sas_device->enclosure_level = 0;
  6353. sas_device->connector_name[0] = '\0';
  6354. }
  6355. if (sas_device->handle == sas_device_pg0->DevHandle)
  6356. goto out;
  6357. pr_info("\thandle changed from(0x%04x)!!!\n",
  6358. sas_device->handle);
  6359. sas_device->handle = sas_device_pg0->DevHandle;
  6360. if (sas_target_priv_data)
  6361. sas_target_priv_data->handle =
  6362. sas_device_pg0->DevHandle;
  6363. goto out;
  6364. }
  6365. }
  6366. out:
  6367. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6368. }
  6369. /**
  6370. * _scsih_search_responding_sas_devices -
  6371. * @ioc: per adapter object
  6372. *
  6373. * After host reset, find out whether devices are still responding.
  6374. * If not remove.
  6375. *
  6376. * Return nothing.
  6377. */
  6378. static void
  6379. _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
  6380. {
  6381. Mpi2SasDevicePage0_t sas_device_pg0;
  6382. Mpi2ConfigReply_t mpi_reply;
  6383. u16 ioc_status;
  6384. u16 handle;
  6385. u32 device_info;
  6386. pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
  6387. if (list_empty(&ioc->sas_device_list))
  6388. goto out;
  6389. handle = 0xFFFF;
  6390. while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  6391. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  6392. handle))) {
  6393. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6394. MPI2_IOCSTATUS_MASK;
  6395. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  6396. break;
  6397. handle = sas_device_pg0.DevHandle =
  6398. le16_to_cpu(sas_device_pg0.DevHandle);
  6399. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  6400. if (!(_scsih_is_end_device(device_info)))
  6401. continue;
  6402. sas_device_pg0.SASAddress =
  6403. le64_to_cpu(sas_device_pg0.SASAddress);
  6404. sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
  6405. _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
  6406. }
  6407. out:
  6408. pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
  6409. ioc->name);
  6410. }
  6411. /**
  6412. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  6413. * @ioc: per adapter object
  6414. * @wwid: world wide identifier for raid volume
  6415. * @handle: device handle
  6416. *
  6417. * After host reset, find out whether devices are still responding.
  6418. * Used in _scsih_remove_unresponsive_raid_devices.
  6419. *
  6420. * Return nothing.
  6421. */
  6422. static void
  6423. _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
  6424. u16 handle)
  6425. {
  6426. struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
  6427. struct scsi_target *starget;
  6428. struct _raid_device *raid_device;
  6429. unsigned long flags;
  6430. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6431. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  6432. if (raid_device->wwid == wwid && raid_device->starget) {
  6433. starget = raid_device->starget;
  6434. if (starget && starget->hostdata) {
  6435. sas_target_priv_data = starget->hostdata;
  6436. sas_target_priv_data->deleted = 0;
  6437. } else
  6438. sas_target_priv_data = NULL;
  6439. raid_device->responding = 1;
  6440. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6441. starget_printk(KERN_INFO, raid_device->starget,
  6442. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  6443. (unsigned long long)raid_device->wwid);
  6444. /*
  6445. * WARPDRIVE: The handles of the PDs might have changed
  6446. * across the host reset so re-initialize the
  6447. * required data for Direct IO
  6448. */
  6449. mpt3sas_init_warpdrive_properties(ioc, raid_device);
  6450. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6451. if (raid_device->handle == handle) {
  6452. spin_unlock_irqrestore(&ioc->raid_device_lock,
  6453. flags);
  6454. return;
  6455. }
  6456. pr_info("\thandle changed from(0x%04x)!!!\n",
  6457. raid_device->handle);
  6458. raid_device->handle = handle;
  6459. if (sas_target_priv_data)
  6460. sas_target_priv_data->handle = handle;
  6461. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6462. return;
  6463. }
  6464. }
  6465. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6466. }
  6467. /**
  6468. * _scsih_search_responding_raid_devices -
  6469. * @ioc: per adapter object
  6470. *
  6471. * After host reset, find out whether devices are still responding.
  6472. * If not remove.
  6473. *
  6474. * Return nothing.
  6475. */
  6476. static void
  6477. _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
  6478. {
  6479. Mpi2RaidVolPage1_t volume_pg1;
  6480. Mpi2RaidVolPage0_t volume_pg0;
  6481. Mpi2RaidPhysDiskPage0_t pd_pg0;
  6482. Mpi2ConfigReply_t mpi_reply;
  6483. u16 ioc_status;
  6484. u16 handle;
  6485. u8 phys_disk_num;
  6486. if (!ioc->ir_firmware)
  6487. return;
  6488. pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
  6489. ioc->name);
  6490. if (list_empty(&ioc->raid_device_list))
  6491. goto out;
  6492. handle = 0xFFFF;
  6493. while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  6494. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  6495. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6496. MPI2_IOCSTATUS_MASK;
  6497. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  6498. break;
  6499. handle = le16_to_cpu(volume_pg1.DevHandle);
  6500. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
  6501. &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  6502. sizeof(Mpi2RaidVolPage0_t)))
  6503. continue;
  6504. if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
  6505. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
  6506. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
  6507. _scsih_mark_responding_raid_device(ioc,
  6508. le64_to_cpu(volume_pg1.WWID), handle);
  6509. }
  6510. /* refresh the pd_handles */
  6511. if (!ioc->is_warpdrive) {
  6512. phys_disk_num = 0xFF;
  6513. memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
  6514. while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  6515. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
  6516. phys_disk_num))) {
  6517. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6518. MPI2_IOCSTATUS_MASK;
  6519. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  6520. break;
  6521. phys_disk_num = pd_pg0.PhysDiskNum;
  6522. handle = le16_to_cpu(pd_pg0.DevHandle);
  6523. set_bit(handle, ioc->pd_handles);
  6524. }
  6525. }
  6526. out:
  6527. pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
  6528. ioc->name);
  6529. }
  6530. /**
  6531. * _scsih_mark_responding_expander - mark a expander as responding
  6532. * @ioc: per adapter object
  6533. * @sas_address: sas address
  6534. * @handle:
  6535. *
  6536. * After host reset, find out whether devices are still responding.
  6537. * Used in _scsih_remove_unresponsive_expanders.
  6538. *
  6539. * Return nothing.
  6540. */
  6541. static void
  6542. _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
  6543. u16 handle)
  6544. {
  6545. struct _sas_node *sas_expander;
  6546. unsigned long flags;
  6547. int i;
  6548. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  6549. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  6550. if (sas_expander->sas_address != sas_address)
  6551. continue;
  6552. sas_expander->responding = 1;
  6553. if (sas_expander->handle == handle)
  6554. goto out;
  6555. pr_info("\texpander(0x%016llx): handle changed" \
  6556. " from(0x%04x) to (0x%04x)!!!\n",
  6557. (unsigned long long)sas_expander->sas_address,
  6558. sas_expander->handle, handle);
  6559. sas_expander->handle = handle;
  6560. for (i = 0 ; i < sas_expander->num_phys ; i++)
  6561. sas_expander->phy[i].handle = handle;
  6562. goto out;
  6563. }
  6564. out:
  6565. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  6566. }
  6567. /**
  6568. * _scsih_search_responding_expanders -
  6569. * @ioc: per adapter object
  6570. *
  6571. * After host reset, find out whether devices are still responding.
  6572. * If not remove.
  6573. *
  6574. * Return nothing.
  6575. */
  6576. static void
  6577. _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
  6578. {
  6579. Mpi2ExpanderPage0_t expander_pg0;
  6580. Mpi2ConfigReply_t mpi_reply;
  6581. u16 ioc_status;
  6582. u64 sas_address;
  6583. u16 handle;
  6584. pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
  6585. if (list_empty(&ioc->sas_expander_list))
  6586. goto out;
  6587. handle = 0xFFFF;
  6588. while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  6589. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  6590. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6591. MPI2_IOCSTATUS_MASK;
  6592. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  6593. break;
  6594. handle = le16_to_cpu(expander_pg0.DevHandle);
  6595. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  6596. pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
  6597. handle,
  6598. (unsigned long long)sas_address);
  6599. _scsih_mark_responding_expander(ioc, sas_address, handle);
  6600. }
  6601. out:
  6602. pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
  6603. }
  6604. /**
  6605. * _scsih_remove_unresponding_sas_devices - removing unresponding devices
  6606. * @ioc: per adapter object
  6607. *
  6608. * Return nothing.
  6609. */
  6610. static void
  6611. _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
  6612. {
  6613. struct _sas_device *sas_device, *sas_device_next;
  6614. struct _sas_node *sas_expander, *sas_expander_next;
  6615. struct _raid_device *raid_device, *raid_device_next;
  6616. struct list_head tmp_list;
  6617. unsigned long flags;
  6618. LIST_HEAD(head);
  6619. pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
  6620. ioc->name);
  6621. /* removing unresponding end devices */
  6622. pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
  6623. ioc->name);
  6624. /*
  6625. * Iterate, pulling off devices marked as non-responding. We become the
  6626. * owner for the reference the list had on any object we prune.
  6627. */
  6628. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  6629. list_for_each_entry_safe(sas_device, sas_device_next,
  6630. &ioc->sas_device_list, list) {
  6631. if (!sas_device->responding)
  6632. list_move_tail(&sas_device->list, &head);
  6633. else
  6634. sas_device->responding = 0;
  6635. }
  6636. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  6637. /*
  6638. * Now, uninitialize and remove the unresponding devices we pruned.
  6639. */
  6640. list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
  6641. _scsih_remove_device(ioc, sas_device);
  6642. list_del_init(&sas_device->list);
  6643. sas_device_put(sas_device);
  6644. }
  6645. /* removing unresponding volumes */
  6646. if (ioc->ir_firmware) {
  6647. pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
  6648. ioc->name);
  6649. list_for_each_entry_safe(raid_device, raid_device_next,
  6650. &ioc->raid_device_list, list) {
  6651. if (!raid_device->responding)
  6652. _scsih_sas_volume_delete(ioc,
  6653. raid_device->handle);
  6654. else
  6655. raid_device->responding = 0;
  6656. }
  6657. }
  6658. /* removing unresponding expanders */
  6659. pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
  6660. ioc->name);
  6661. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  6662. INIT_LIST_HEAD(&tmp_list);
  6663. list_for_each_entry_safe(sas_expander, sas_expander_next,
  6664. &ioc->sas_expander_list, list) {
  6665. if (!sas_expander->responding)
  6666. list_move_tail(&sas_expander->list, &tmp_list);
  6667. else
  6668. sas_expander->responding = 0;
  6669. }
  6670. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  6671. list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
  6672. list) {
  6673. list_del(&sas_expander->list);
  6674. _scsih_expander_node_remove(ioc, sas_expander);
  6675. }
  6676. pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
  6677. ioc->name);
  6678. /* unblock devices */
  6679. _scsih_ublock_io_all_device(ioc);
  6680. }
  6681. static void
  6682. _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
  6683. struct _sas_node *sas_expander, u16 handle)
  6684. {
  6685. Mpi2ExpanderPage1_t expander_pg1;
  6686. Mpi2ConfigReply_t mpi_reply;
  6687. int i;
  6688. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  6689. if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
  6690. &expander_pg1, i, handle))) {
  6691. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  6692. ioc->name, __FILE__, __LINE__, __func__);
  6693. return;
  6694. }
  6695. mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
  6696. le16_to_cpu(expander_pg1.AttachedDevHandle), i,
  6697. expander_pg1.NegotiatedLinkRate >> 4);
  6698. }
  6699. }
  6700. /**
  6701. * _scsih_scan_for_devices_after_reset - scan for devices after host reset
  6702. * @ioc: per adapter object
  6703. *
  6704. * Return nothing.
  6705. */
  6706. static void
  6707. _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
  6708. {
  6709. Mpi2ExpanderPage0_t expander_pg0;
  6710. Mpi2SasDevicePage0_t sas_device_pg0;
  6711. Mpi2RaidVolPage1_t volume_pg1;
  6712. Mpi2RaidVolPage0_t volume_pg0;
  6713. Mpi2RaidPhysDiskPage0_t pd_pg0;
  6714. Mpi2EventIrConfigElement_t element;
  6715. Mpi2ConfigReply_t mpi_reply;
  6716. u8 phys_disk_num;
  6717. u16 ioc_status;
  6718. u16 handle, parent_handle;
  6719. u64 sas_address;
  6720. struct _sas_device *sas_device;
  6721. struct _sas_node *expander_device;
  6722. static struct _raid_device *raid_device;
  6723. u8 retry_count;
  6724. unsigned long flags;
  6725. pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
  6726. _scsih_sas_host_refresh(ioc);
  6727. pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
  6728. /* expanders */
  6729. handle = 0xFFFF;
  6730. while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  6731. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  6732. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6733. MPI2_IOCSTATUS_MASK;
  6734. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6735. pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
  6736. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  6737. ioc->name, ioc_status,
  6738. le32_to_cpu(mpi_reply.IOCLogInfo));
  6739. break;
  6740. }
  6741. handle = le16_to_cpu(expander_pg0.DevHandle);
  6742. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  6743. expander_device = mpt3sas_scsih_expander_find_by_sas_address(
  6744. ioc, le64_to_cpu(expander_pg0.SASAddress));
  6745. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  6746. if (expander_device)
  6747. _scsih_refresh_expander_links(ioc, expander_device,
  6748. handle);
  6749. else {
  6750. pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
  6751. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  6752. handle, (unsigned long long)
  6753. le64_to_cpu(expander_pg0.SASAddress));
  6754. _scsih_expander_add(ioc, handle);
  6755. pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
  6756. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  6757. handle, (unsigned long long)
  6758. le64_to_cpu(expander_pg0.SASAddress));
  6759. }
  6760. }
  6761. pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
  6762. ioc->name);
  6763. if (!ioc->ir_firmware)
  6764. goto skip_to_sas;
  6765. pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
  6766. /* phys disk */
  6767. phys_disk_num = 0xFF;
  6768. while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  6769. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
  6770. phys_disk_num))) {
  6771. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6772. MPI2_IOCSTATUS_MASK;
  6773. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6774. pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
  6775. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  6776. ioc->name, ioc_status,
  6777. le32_to_cpu(mpi_reply.IOCLogInfo));
  6778. break;
  6779. }
  6780. phys_disk_num = pd_pg0.PhysDiskNum;
  6781. handle = le16_to_cpu(pd_pg0.DevHandle);
  6782. sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
  6783. if (sas_device) {
  6784. sas_device_put(sas_device);
  6785. continue;
  6786. }
  6787. if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  6788. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  6789. handle) != 0)
  6790. continue;
  6791. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6792. MPI2_IOCSTATUS_MASK;
  6793. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6794. pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
  6795. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  6796. ioc->name, ioc_status,
  6797. le32_to_cpu(mpi_reply.IOCLogInfo));
  6798. break;
  6799. }
  6800. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  6801. if (!_scsih_get_sas_address(ioc, parent_handle,
  6802. &sas_address)) {
  6803. pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
  6804. " handle (0x%04x), sas_addr(0x%016llx)\n",
  6805. ioc->name, handle, (unsigned long long)
  6806. le64_to_cpu(sas_device_pg0.SASAddress));
  6807. mpt3sas_transport_update_links(ioc, sas_address,
  6808. handle, sas_device_pg0.PhyNum,
  6809. MPI2_SAS_NEG_LINK_RATE_1_5);
  6810. set_bit(handle, ioc->pd_handles);
  6811. retry_count = 0;
  6812. /* This will retry adding the end device.
  6813. * _scsih_add_device() will decide on retries and
  6814. * return "1" when it should be retried
  6815. */
  6816. while (_scsih_add_device(ioc, handle, retry_count++,
  6817. 1)) {
  6818. ssleep(1);
  6819. }
  6820. pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
  6821. " handle (0x%04x), sas_addr(0x%016llx)\n",
  6822. ioc->name, handle, (unsigned long long)
  6823. le64_to_cpu(sas_device_pg0.SASAddress));
  6824. }
  6825. }
  6826. pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
  6827. ioc->name);
  6828. pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
  6829. /* volumes */
  6830. handle = 0xFFFF;
  6831. while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  6832. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  6833. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6834. MPI2_IOCSTATUS_MASK;
  6835. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6836. pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
  6837. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  6838. ioc->name, ioc_status,
  6839. le32_to_cpu(mpi_reply.IOCLogInfo));
  6840. break;
  6841. }
  6842. handle = le16_to_cpu(volume_pg1.DevHandle);
  6843. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  6844. raid_device = _scsih_raid_device_find_by_wwid(ioc,
  6845. le64_to_cpu(volume_pg1.WWID));
  6846. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  6847. if (raid_device)
  6848. continue;
  6849. if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
  6850. &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
  6851. sizeof(Mpi2RaidVolPage0_t)))
  6852. continue;
  6853. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6854. MPI2_IOCSTATUS_MASK;
  6855. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6856. pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
  6857. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  6858. ioc->name, ioc_status,
  6859. le32_to_cpu(mpi_reply.IOCLogInfo));
  6860. break;
  6861. }
  6862. if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
  6863. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
  6864. volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
  6865. memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
  6866. element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
  6867. element.VolDevHandle = volume_pg1.DevHandle;
  6868. pr_info(MPT3SAS_FMT
  6869. "\tBEFORE adding volume: handle (0x%04x)\n",
  6870. ioc->name, volume_pg1.DevHandle);
  6871. _scsih_sas_volume_add(ioc, &element);
  6872. pr_info(MPT3SAS_FMT
  6873. "\tAFTER adding volume: handle (0x%04x)\n",
  6874. ioc->name, volume_pg1.DevHandle);
  6875. }
  6876. }
  6877. pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
  6878. ioc->name);
  6879. skip_to_sas:
  6880. pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
  6881. ioc->name);
  6882. /* sas devices */
  6883. handle = 0xFFFF;
  6884. while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  6885. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  6886. handle))) {
  6887. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  6888. MPI2_IOCSTATUS_MASK;
  6889. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  6890. pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
  6891. " ioc_status(0x%04x), loginfo(0x%08x)\n",
  6892. ioc->name, ioc_status,
  6893. le32_to_cpu(mpi_reply.IOCLogInfo));
  6894. break;
  6895. }
  6896. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  6897. if (!(_scsih_is_end_device(
  6898. le32_to_cpu(sas_device_pg0.DeviceInfo))))
  6899. continue;
  6900. sas_device = mpt3sas_get_sdev_by_addr(ioc,
  6901. le64_to_cpu(sas_device_pg0.SASAddress));
  6902. if (sas_device) {
  6903. sas_device_put(sas_device);
  6904. continue;
  6905. }
  6906. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  6907. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
  6908. pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
  6909. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  6910. handle, (unsigned long long)
  6911. le64_to_cpu(sas_device_pg0.SASAddress));
  6912. mpt3sas_transport_update_links(ioc, sas_address, handle,
  6913. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  6914. retry_count = 0;
  6915. /* This will retry adding the end device.
  6916. * _scsih_add_device() will decide on retries and
  6917. * return "1" when it should be retried
  6918. */
  6919. while (_scsih_add_device(ioc, handle, retry_count++,
  6920. 0)) {
  6921. ssleep(1);
  6922. }
  6923. pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
  6924. "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  6925. handle, (unsigned long long)
  6926. le64_to_cpu(sas_device_pg0.SASAddress));
  6927. }
  6928. }
  6929. pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
  6930. ioc->name);
  6931. pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
  6932. }
  6933. /**
  6934. * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
  6935. * @ioc: per adapter object
  6936. * @reset_phase: phase
  6937. *
  6938. * The handler for doing any required cleanup or initialization.
  6939. *
  6940. * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
  6941. * MPT3_IOC_DONE_RESET
  6942. *
  6943. * Return nothing.
  6944. */
  6945. void
  6946. mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
  6947. {
  6948. switch (reset_phase) {
  6949. case MPT3_IOC_PRE_RESET:
  6950. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  6951. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  6952. break;
  6953. case MPT3_IOC_AFTER_RESET:
  6954. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  6955. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  6956. if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
  6957. ioc->scsih_cmds.status |= MPT3_CMD_RESET;
  6958. mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
  6959. complete(&ioc->scsih_cmds.done);
  6960. }
  6961. if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
  6962. ioc->tm_cmds.status |= MPT3_CMD_RESET;
  6963. mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  6964. complete(&ioc->tm_cmds.done);
  6965. }
  6966. _scsih_fw_event_cleanup_queue(ioc);
  6967. _scsih_flush_running_cmds(ioc);
  6968. break;
  6969. case MPT3_IOC_DONE_RESET:
  6970. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  6971. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  6972. if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
  6973. !ioc->sas_hba.num_phys)) {
  6974. _scsih_prep_device_scan(ioc);
  6975. _scsih_search_responding_sas_devices(ioc);
  6976. _scsih_search_responding_raid_devices(ioc);
  6977. _scsih_search_responding_expanders(ioc);
  6978. _scsih_error_recovery_delete_devices(ioc);
  6979. }
  6980. break;
  6981. }
  6982. }
  6983. /**
  6984. * _mpt3sas_fw_work - delayed task for processing firmware events
  6985. * @ioc: per adapter object
  6986. * @fw_event: The fw_event_work object
  6987. * Context: user.
  6988. *
  6989. * Return nothing.
  6990. */
  6991. static void
  6992. _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  6993. {
  6994. _scsih_fw_event_del_from_list(ioc, fw_event);
  6995. /* the queue is being flushed so ignore this event */
  6996. if (ioc->remove_host || ioc->pci_error_recovery) {
  6997. fw_event_work_put(fw_event);
  6998. return;
  6999. }
  7000. switch (fw_event->event) {
  7001. case MPT3SAS_PROCESS_TRIGGER_DIAG:
  7002. mpt3sas_process_trigger_data(ioc,
  7003. (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
  7004. fw_event->event_data);
  7005. break;
  7006. case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
  7007. while (scsi_host_in_recovery(ioc->shost) ||
  7008. ioc->shost_recovery) {
  7009. /*
  7010. * If we're unloading, bail. Otherwise, this can become
  7011. * an infinite loop.
  7012. */
  7013. if (ioc->remove_host)
  7014. goto out;
  7015. ssleep(1);
  7016. }
  7017. _scsih_remove_unresponding_sas_devices(ioc);
  7018. _scsih_scan_for_devices_after_reset(ioc);
  7019. break;
  7020. case MPT3SAS_PORT_ENABLE_COMPLETE:
  7021. ioc->start_scan = 0;
  7022. if (missing_delay[0] != -1 && missing_delay[1] != -1)
  7023. mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
  7024. missing_delay[1]);
  7025. dewtprintk(ioc, pr_info(MPT3SAS_FMT
  7026. "port enable: complete from worker thread\n",
  7027. ioc->name));
  7028. break;
  7029. case MPT3SAS_TURN_ON_PFA_LED:
  7030. _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
  7031. break;
  7032. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  7033. _scsih_sas_topology_change_event(ioc, fw_event);
  7034. break;
  7035. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  7036. _scsih_sas_device_status_change_event(ioc, fw_event);
  7037. break;
  7038. case MPI2_EVENT_SAS_DISCOVERY:
  7039. _scsih_sas_discovery_event(ioc, fw_event);
  7040. break;
  7041. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  7042. _scsih_sas_broadcast_primitive_event(ioc, fw_event);
  7043. break;
  7044. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  7045. _scsih_sas_enclosure_dev_status_change_event(ioc,
  7046. fw_event);
  7047. break;
  7048. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  7049. _scsih_sas_ir_config_change_event(ioc, fw_event);
  7050. break;
  7051. case MPI2_EVENT_IR_VOLUME:
  7052. _scsih_sas_ir_volume_event(ioc, fw_event);
  7053. break;
  7054. case MPI2_EVENT_IR_PHYSICAL_DISK:
  7055. _scsih_sas_ir_physical_disk_event(ioc, fw_event);
  7056. break;
  7057. case MPI2_EVENT_IR_OPERATION_STATUS:
  7058. _scsih_sas_ir_operation_status_event(ioc, fw_event);
  7059. break;
  7060. }
  7061. out:
  7062. fw_event_work_put(fw_event);
  7063. }
  7064. /**
  7065. * _firmware_event_work
  7066. * @ioc: per adapter object
  7067. * @work: The fw_event_work object
  7068. * Context: user.
  7069. *
  7070. * wrappers for the work thread handling firmware events
  7071. *
  7072. * Return nothing.
  7073. */
  7074. static void
  7075. _firmware_event_work(struct work_struct *work)
  7076. {
  7077. struct fw_event_work *fw_event = container_of(work,
  7078. struct fw_event_work, work);
  7079. _mpt3sas_fw_work(fw_event->ioc, fw_event);
  7080. }
  7081. /**
  7082. * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
  7083. * @ioc: per adapter object
  7084. * @msix_index: MSIX table index supplied by the OS
  7085. * @reply: reply message frame(lower 32bit addr)
  7086. * Context: interrupt.
  7087. *
  7088. * This function merely adds a new work task into ioc->firmware_event_thread.
  7089. * The tasks are worked from _firmware_event_work in user context.
  7090. *
  7091. * Return 1 meaning mf should be freed from _base_interrupt
  7092. * 0 means the mf is freed from this function.
  7093. */
  7094. u8
  7095. mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
  7096. u32 reply)
  7097. {
  7098. struct fw_event_work *fw_event;
  7099. Mpi2EventNotificationReply_t *mpi_reply;
  7100. u16 event;
  7101. u16 sz;
  7102. Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
  7103. /* events turned off due to host reset or driver unloading */
  7104. if (ioc->remove_host || ioc->pci_error_recovery)
  7105. return 1;
  7106. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  7107. if (unlikely(!mpi_reply)) {
  7108. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  7109. ioc->name, __FILE__, __LINE__, __func__);
  7110. return 1;
  7111. }
  7112. event = le16_to_cpu(mpi_reply->Event);
  7113. if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
  7114. mpt3sas_trigger_event(ioc, event, 0);
  7115. switch (event) {
  7116. /* handle these */
  7117. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  7118. {
  7119. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  7120. (Mpi2EventDataSasBroadcastPrimitive_t *)
  7121. mpi_reply->EventData;
  7122. if (baen_data->Primitive !=
  7123. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
  7124. return 1;
  7125. if (ioc->broadcast_aen_busy) {
  7126. ioc->broadcast_aen_pending++;
  7127. return 1;
  7128. } else
  7129. ioc->broadcast_aen_busy = 1;
  7130. break;
  7131. }
  7132. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  7133. _scsih_check_topo_delete_events(ioc,
  7134. (Mpi2EventDataSasTopologyChangeList_t *)
  7135. mpi_reply->EventData);
  7136. break;
  7137. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  7138. _scsih_check_ir_config_unhide_events(ioc,
  7139. (Mpi2EventDataIrConfigChangeList_t *)
  7140. mpi_reply->EventData);
  7141. break;
  7142. case MPI2_EVENT_IR_VOLUME:
  7143. _scsih_check_volume_delete_events(ioc,
  7144. (Mpi2EventDataIrVolume_t *)
  7145. mpi_reply->EventData);
  7146. break;
  7147. case MPI2_EVENT_LOG_ENTRY_ADDED:
  7148. {
  7149. Mpi2EventDataLogEntryAdded_t *log_entry;
  7150. u32 *log_code;
  7151. if (!ioc->is_warpdrive)
  7152. break;
  7153. log_entry = (Mpi2EventDataLogEntryAdded_t *)
  7154. mpi_reply->EventData;
  7155. log_code = (u32 *)log_entry->LogData;
  7156. if (le16_to_cpu(log_entry->LogEntryQualifier)
  7157. != MPT2_WARPDRIVE_LOGENTRY)
  7158. break;
  7159. switch (le32_to_cpu(*log_code)) {
  7160. case MPT2_WARPDRIVE_LC_SSDT:
  7161. pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
  7162. "IO Throttling has occurred in the WarpDrive "
  7163. "subsystem. Check WarpDrive documentation for "
  7164. "additional details.\n", ioc->name);
  7165. break;
  7166. case MPT2_WARPDRIVE_LC_SSDLW:
  7167. pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
  7168. "Program/Erase Cycles for the WarpDrive subsystem "
  7169. "in degraded range. Check WarpDrive documentation "
  7170. "for additional details.\n", ioc->name);
  7171. break;
  7172. case MPT2_WARPDRIVE_LC_SSDLF:
  7173. pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
  7174. "There are no Program/Erase Cycles for the "
  7175. "WarpDrive subsystem. The storage device will be "
  7176. "in read-only mode. Check WarpDrive documentation "
  7177. "for additional details.\n", ioc->name);
  7178. break;
  7179. case MPT2_WARPDRIVE_LC_BRMF:
  7180. pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
  7181. "The Backup Rail Monitor has failed on the "
  7182. "WarpDrive subsystem. Check WarpDrive "
  7183. "documentation for additional details.\n",
  7184. ioc->name);
  7185. break;
  7186. }
  7187. break;
  7188. }
  7189. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  7190. case MPI2_EVENT_IR_OPERATION_STATUS:
  7191. case MPI2_EVENT_SAS_DISCOVERY:
  7192. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  7193. case MPI2_EVENT_IR_PHYSICAL_DISK:
  7194. break;
  7195. case MPI2_EVENT_TEMP_THRESHOLD:
  7196. _scsih_temp_threshold_events(ioc,
  7197. (Mpi2EventDataTemperature_t *)
  7198. mpi_reply->EventData);
  7199. break;
  7200. case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
  7201. ActiveCableEventData =
  7202. (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
  7203. if (ActiveCableEventData->ReasonCode ==
  7204. MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER) {
  7205. pr_info(MPT3SAS_FMT "Currently an active cable with ReceptacleID %d",
  7206. ioc->name, ActiveCableEventData->ReceptacleID);
  7207. pr_info("cannot be powered and devices connected to this active cable");
  7208. pr_info("will not be seen. This active cable");
  7209. pr_info("requires %d mW of power",
  7210. ActiveCableEventData->ActiveCablePowerRequirement);
  7211. }
  7212. break;
  7213. default: /* ignore the rest */
  7214. return 1;
  7215. }
  7216. sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
  7217. fw_event = alloc_fw_event_work(sz);
  7218. if (!fw_event) {
  7219. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7220. ioc->name, __FILE__, __LINE__, __func__);
  7221. return 1;
  7222. }
  7223. memcpy(fw_event->event_data, mpi_reply->EventData, sz);
  7224. fw_event->ioc = ioc;
  7225. fw_event->VF_ID = mpi_reply->VF_ID;
  7226. fw_event->VP_ID = mpi_reply->VP_ID;
  7227. fw_event->event = event;
  7228. _scsih_fw_event_add(ioc, fw_event);
  7229. fw_event_work_put(fw_event);
  7230. return 1;
  7231. }
  7232. /**
  7233. * _scsih_expander_node_remove - removing expander device from list.
  7234. * @ioc: per adapter object
  7235. * @sas_expander: the sas_device object
  7236. * Context: Calling function should acquire ioc->sas_node_lock.
  7237. *
  7238. * Removing object and freeing associated memory from the
  7239. * ioc->sas_expander_list.
  7240. *
  7241. * Return nothing.
  7242. */
  7243. static void
  7244. _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
  7245. struct _sas_node *sas_expander)
  7246. {
  7247. struct _sas_port *mpt3sas_port, *next;
  7248. /* remove sibling ports attached to this expander */
  7249. list_for_each_entry_safe(mpt3sas_port, next,
  7250. &sas_expander->sas_port_list, port_list) {
  7251. if (ioc->shost_recovery)
  7252. return;
  7253. if (mpt3sas_port->remote_identify.device_type ==
  7254. SAS_END_DEVICE)
  7255. mpt3sas_device_remove_by_sas_address(ioc,
  7256. mpt3sas_port->remote_identify.sas_address);
  7257. else if (mpt3sas_port->remote_identify.device_type ==
  7258. SAS_EDGE_EXPANDER_DEVICE ||
  7259. mpt3sas_port->remote_identify.device_type ==
  7260. SAS_FANOUT_EXPANDER_DEVICE)
  7261. mpt3sas_expander_remove(ioc,
  7262. mpt3sas_port->remote_identify.sas_address);
  7263. }
  7264. mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
  7265. sas_expander->sas_address_parent);
  7266. pr_info(MPT3SAS_FMT
  7267. "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
  7268. ioc->name,
  7269. sas_expander->handle, (unsigned long long)
  7270. sas_expander->sas_address);
  7271. kfree(sas_expander->phy);
  7272. kfree(sas_expander);
  7273. }
  7274. /**
  7275. * _scsih_ir_shutdown - IR shutdown notification
  7276. * @ioc: per adapter object
  7277. *
  7278. * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
  7279. * the host system is shutting down.
  7280. *
  7281. * Return nothing.
  7282. */
  7283. static void
  7284. _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
  7285. {
  7286. Mpi2RaidActionRequest_t *mpi_request;
  7287. Mpi2RaidActionReply_t *mpi_reply;
  7288. u16 smid;
  7289. /* is IR firmware build loaded ? */
  7290. if (!ioc->ir_firmware)
  7291. return;
  7292. /* are there any volumes ? */
  7293. if (list_empty(&ioc->raid_device_list))
  7294. return;
  7295. mutex_lock(&ioc->scsih_cmds.mutex);
  7296. if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
  7297. pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
  7298. ioc->name, __func__);
  7299. goto out;
  7300. }
  7301. ioc->scsih_cmds.status = MPT3_CMD_PENDING;
  7302. smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  7303. if (!smid) {
  7304. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  7305. ioc->name, __func__);
  7306. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  7307. goto out;
  7308. }
  7309. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  7310. ioc->scsih_cmds.smid = smid;
  7311. memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
  7312. mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
  7313. mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
  7314. if (!ioc->hide_ir_msg)
  7315. pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
  7316. init_completion(&ioc->scsih_cmds.done);
  7317. mpt3sas_base_put_smid_default(ioc, smid);
  7318. wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
  7319. if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
  7320. pr_err(MPT3SAS_FMT "%s: timeout\n",
  7321. ioc->name, __func__);
  7322. goto out;
  7323. }
  7324. if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
  7325. mpi_reply = ioc->scsih_cmds.reply;
  7326. if (!ioc->hide_ir_msg)
  7327. pr_info(MPT3SAS_FMT "IR shutdown "
  7328. "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
  7329. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  7330. le32_to_cpu(mpi_reply->IOCLogInfo));
  7331. }
  7332. out:
  7333. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  7334. mutex_unlock(&ioc->scsih_cmds.mutex);
  7335. }
  7336. /**
  7337. * scsih_remove - detach and remove add host
  7338. * @pdev: PCI device struct
  7339. *
  7340. * Routine called when unloading the driver.
  7341. * Return nothing.
  7342. */
  7343. static void scsih_remove(struct pci_dev *pdev)
  7344. {
  7345. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7346. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  7347. struct _sas_port *mpt3sas_port, *next_port;
  7348. struct _raid_device *raid_device, *next;
  7349. struct MPT3SAS_TARGET *sas_target_priv_data;
  7350. struct workqueue_struct *wq;
  7351. unsigned long flags;
  7352. ioc->remove_host = 1;
  7353. _scsih_fw_event_cleanup_queue(ioc);
  7354. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  7355. wq = ioc->firmware_event_thread;
  7356. ioc->firmware_event_thread = NULL;
  7357. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  7358. if (wq)
  7359. destroy_workqueue(wq);
  7360. /* release all the volumes */
  7361. _scsih_ir_shutdown(ioc);
  7362. list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
  7363. list) {
  7364. if (raid_device->starget) {
  7365. sas_target_priv_data =
  7366. raid_device->starget->hostdata;
  7367. sas_target_priv_data->deleted = 1;
  7368. scsi_remove_target(&raid_device->starget->dev);
  7369. }
  7370. pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
  7371. ioc->name, raid_device->handle,
  7372. (unsigned long long) raid_device->wwid);
  7373. _scsih_raid_device_remove(ioc, raid_device);
  7374. }
  7375. /* free ports attached to the sas_host */
  7376. list_for_each_entry_safe(mpt3sas_port, next_port,
  7377. &ioc->sas_hba.sas_port_list, port_list) {
  7378. if (mpt3sas_port->remote_identify.device_type ==
  7379. SAS_END_DEVICE)
  7380. mpt3sas_device_remove_by_sas_address(ioc,
  7381. mpt3sas_port->remote_identify.sas_address);
  7382. else if (mpt3sas_port->remote_identify.device_type ==
  7383. SAS_EDGE_EXPANDER_DEVICE ||
  7384. mpt3sas_port->remote_identify.device_type ==
  7385. SAS_FANOUT_EXPANDER_DEVICE)
  7386. mpt3sas_expander_remove(ioc,
  7387. mpt3sas_port->remote_identify.sas_address);
  7388. }
  7389. /* free phys attached to the sas_host */
  7390. if (ioc->sas_hba.num_phys) {
  7391. kfree(ioc->sas_hba.phy);
  7392. ioc->sas_hba.phy = NULL;
  7393. ioc->sas_hba.num_phys = 0;
  7394. }
  7395. sas_remove_host(shost);
  7396. scsi_remove_host(shost);
  7397. mpt3sas_base_detach(ioc);
  7398. spin_lock(&gioc_lock);
  7399. list_del(&ioc->list);
  7400. spin_unlock(&gioc_lock);
  7401. scsi_host_put(shost);
  7402. }
  7403. /**
  7404. * scsih_shutdown - routine call during system shutdown
  7405. * @pdev: PCI device struct
  7406. *
  7407. * Return nothing.
  7408. */
  7409. static void
  7410. scsih_shutdown(struct pci_dev *pdev)
  7411. {
  7412. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  7413. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  7414. struct workqueue_struct *wq;
  7415. unsigned long flags;
  7416. ioc->remove_host = 1;
  7417. _scsih_fw_event_cleanup_queue(ioc);
  7418. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  7419. wq = ioc->firmware_event_thread;
  7420. ioc->firmware_event_thread = NULL;
  7421. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  7422. if (wq)
  7423. destroy_workqueue(wq);
  7424. _scsih_ir_shutdown(ioc);
  7425. mpt3sas_base_detach(ioc);
  7426. }
  7427. /**
  7428. * _scsih_probe_boot_devices - reports 1st device
  7429. * @ioc: per adapter object
  7430. *
  7431. * If specified in bios page 2, this routine reports the 1st
  7432. * device scsi-ml or sas transport for persistent boot device
  7433. * purposes. Please refer to function _scsih_determine_boot_device()
  7434. */
  7435. static void
  7436. _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
  7437. {
  7438. u8 is_raid;
  7439. void *device;
  7440. struct _sas_device *sas_device;
  7441. struct _raid_device *raid_device;
  7442. u16 handle;
  7443. u64 sas_address_parent;
  7444. u64 sas_address;
  7445. unsigned long flags;
  7446. int rc;
  7447. /* no Bios, return immediately */
  7448. if (!ioc->bios_pg3.BiosVersion)
  7449. return;
  7450. device = NULL;
  7451. is_raid = 0;
  7452. if (ioc->req_boot_device.device) {
  7453. device = ioc->req_boot_device.device;
  7454. is_raid = ioc->req_boot_device.is_raid;
  7455. } else if (ioc->req_alt_boot_device.device) {
  7456. device = ioc->req_alt_boot_device.device;
  7457. is_raid = ioc->req_alt_boot_device.is_raid;
  7458. } else if (ioc->current_boot_device.device) {
  7459. device = ioc->current_boot_device.device;
  7460. is_raid = ioc->current_boot_device.is_raid;
  7461. }
  7462. if (!device)
  7463. return;
  7464. if (is_raid) {
  7465. raid_device = device;
  7466. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  7467. raid_device->id, 0);
  7468. if (rc)
  7469. _scsih_raid_device_remove(ioc, raid_device);
  7470. } else {
  7471. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7472. sas_device = device;
  7473. handle = sas_device->handle;
  7474. sas_address_parent = sas_device->sas_address_parent;
  7475. sas_address = sas_device->sas_address;
  7476. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  7477. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7478. if (ioc->hide_drives)
  7479. return;
  7480. if (!mpt3sas_transport_port_add(ioc, handle,
  7481. sas_address_parent)) {
  7482. _scsih_sas_device_remove(ioc, sas_device);
  7483. } else if (!sas_device->starget) {
  7484. if (!ioc->is_driver_loading) {
  7485. mpt3sas_transport_port_remove(ioc,
  7486. sas_address,
  7487. sas_address_parent);
  7488. _scsih_sas_device_remove(ioc, sas_device);
  7489. }
  7490. }
  7491. }
  7492. }
  7493. /**
  7494. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  7495. * @ioc: per adapter object
  7496. *
  7497. * Called during initial loading of the driver.
  7498. */
  7499. static void
  7500. _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
  7501. {
  7502. struct _raid_device *raid_device, *raid_next;
  7503. int rc;
  7504. list_for_each_entry_safe(raid_device, raid_next,
  7505. &ioc->raid_device_list, list) {
  7506. if (raid_device->starget)
  7507. continue;
  7508. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  7509. raid_device->id, 0);
  7510. if (rc)
  7511. _scsih_raid_device_remove(ioc, raid_device);
  7512. }
  7513. }
  7514. static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
  7515. {
  7516. struct _sas_device *sas_device = NULL;
  7517. unsigned long flags;
  7518. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7519. if (!list_empty(&ioc->sas_device_init_list)) {
  7520. sas_device = list_first_entry(&ioc->sas_device_init_list,
  7521. struct _sas_device, list);
  7522. sas_device_get(sas_device);
  7523. }
  7524. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7525. return sas_device;
  7526. }
  7527. static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
  7528. struct _sas_device *sas_device)
  7529. {
  7530. unsigned long flags;
  7531. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  7532. /*
  7533. * Since we dropped the lock during the call to port_add(), we need to
  7534. * be careful here that somebody else didn't move or delete this item
  7535. * while we were busy with other things.
  7536. *
  7537. * If it was on the list, we need a put() for the reference the list
  7538. * had. Either way, we need a get() for the destination list.
  7539. */
  7540. if (!list_empty(&sas_device->list)) {
  7541. list_del_init(&sas_device->list);
  7542. sas_device_put(sas_device);
  7543. }
  7544. sas_device_get(sas_device);
  7545. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  7546. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  7547. }
  7548. /**
  7549. * _scsih_probe_sas - reporting sas devices to sas transport
  7550. * @ioc: per adapter object
  7551. *
  7552. * Called during initial loading of the driver.
  7553. */
  7554. static void
  7555. _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
  7556. {
  7557. struct _sas_device *sas_device;
  7558. if (ioc->hide_drives)
  7559. return;
  7560. while ((sas_device = get_next_sas_device(ioc))) {
  7561. if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
  7562. sas_device->sas_address_parent)) {
  7563. _scsih_sas_device_remove(ioc, sas_device);
  7564. sas_device_put(sas_device);
  7565. continue;
  7566. } else if (!sas_device->starget) {
  7567. /*
  7568. * When asyn scanning is enabled, its not possible to
  7569. * remove devices while scanning is turned on due to an
  7570. * oops in scsi_sysfs_add_sdev()->add_device()->
  7571. * sysfs_addrm_start()
  7572. */
  7573. if (!ioc->is_driver_loading) {
  7574. mpt3sas_transport_port_remove(ioc,
  7575. sas_device->sas_address,
  7576. sas_device->sas_address_parent);
  7577. _scsih_sas_device_remove(ioc, sas_device);
  7578. sas_device_put(sas_device);
  7579. continue;
  7580. }
  7581. }
  7582. sas_device_make_active(ioc, sas_device);
  7583. sas_device_put(sas_device);
  7584. }
  7585. }
  7586. /**
  7587. * _scsih_probe_devices - probing for devices
  7588. * @ioc: per adapter object
  7589. *
  7590. * Called during initial loading of the driver.
  7591. */
  7592. static void
  7593. _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
  7594. {
  7595. u16 volume_mapping_flags;
  7596. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  7597. return; /* return when IOC doesn't support initiator mode */
  7598. _scsih_probe_boot_devices(ioc);
  7599. if (ioc->ir_firmware) {
  7600. volume_mapping_flags =
  7601. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  7602. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  7603. if (volume_mapping_flags ==
  7604. MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
  7605. _scsih_probe_raid(ioc);
  7606. _scsih_probe_sas(ioc);
  7607. } else {
  7608. _scsih_probe_sas(ioc);
  7609. _scsih_probe_raid(ioc);
  7610. }
  7611. } else
  7612. _scsih_probe_sas(ioc);
  7613. }
  7614. /**
  7615. * scsih_scan_start - scsi lld callback for .scan_start
  7616. * @shost: SCSI host pointer
  7617. *
  7618. * The shost has the ability to discover targets on its own instead
  7619. * of scanning the entire bus. In our implemention, we will kick off
  7620. * firmware discovery.
  7621. */
  7622. static void
  7623. scsih_scan_start(struct Scsi_Host *shost)
  7624. {
  7625. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  7626. int rc;
  7627. if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
  7628. mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
  7629. if (disable_discovery > 0)
  7630. return;
  7631. ioc->start_scan = 1;
  7632. rc = mpt3sas_port_enable(ioc);
  7633. if (rc != 0)
  7634. pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
  7635. }
  7636. /**
  7637. * scsih_scan_finished - scsi lld callback for .scan_finished
  7638. * @shost: SCSI host pointer
  7639. * @time: elapsed time of the scan in jiffies
  7640. *
  7641. * This function will be called periodicallyn until it returns 1 with the
  7642. * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
  7643. * we wait for firmware discovery to complete, then return 1.
  7644. */
  7645. static int
  7646. scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
  7647. {
  7648. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  7649. if (disable_discovery > 0) {
  7650. ioc->is_driver_loading = 0;
  7651. ioc->wait_for_discovery_to_complete = 0;
  7652. return 1;
  7653. }
  7654. if (time >= (300 * HZ)) {
  7655. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  7656. pr_info(MPT3SAS_FMT
  7657. "port enable: FAILED with timeout (timeout=300s)\n",
  7658. ioc->name);
  7659. ioc->is_driver_loading = 0;
  7660. return 1;
  7661. }
  7662. if (ioc->start_scan)
  7663. return 0;
  7664. if (ioc->start_scan_failed) {
  7665. pr_info(MPT3SAS_FMT
  7666. "port enable: FAILED with (ioc_status=0x%08x)\n",
  7667. ioc->name, ioc->start_scan_failed);
  7668. ioc->is_driver_loading = 0;
  7669. ioc->wait_for_discovery_to_complete = 0;
  7670. ioc->remove_host = 1;
  7671. return 1;
  7672. }
  7673. pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
  7674. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  7675. if (ioc->wait_for_discovery_to_complete) {
  7676. ioc->wait_for_discovery_to_complete = 0;
  7677. _scsih_probe_devices(ioc);
  7678. }
  7679. mpt3sas_base_start_watchdog(ioc);
  7680. ioc->is_driver_loading = 0;
  7681. return 1;
  7682. }
  7683. /* shost template for SAS 2.0 HBA devices */
  7684. static struct scsi_host_template mpt2sas_driver_template = {
  7685. .module = THIS_MODULE,
  7686. .name = "Fusion MPT SAS Host",
  7687. .proc_name = MPT2SAS_DRIVER_NAME,
  7688. .queuecommand = scsih_qcmd,
  7689. .target_alloc = scsih_target_alloc,
  7690. .slave_alloc = scsih_slave_alloc,
  7691. .slave_configure = scsih_slave_configure,
  7692. .target_destroy = scsih_target_destroy,
  7693. .slave_destroy = scsih_slave_destroy,
  7694. .scan_finished = scsih_scan_finished,
  7695. .scan_start = scsih_scan_start,
  7696. .change_queue_depth = scsih_change_queue_depth,
  7697. .eh_abort_handler = scsih_abort,
  7698. .eh_device_reset_handler = scsih_dev_reset,
  7699. .eh_target_reset_handler = scsih_target_reset,
  7700. .eh_host_reset_handler = scsih_host_reset,
  7701. .bios_param = scsih_bios_param,
  7702. .can_queue = 1,
  7703. .this_id = -1,
  7704. .sg_tablesize = MPT2SAS_SG_DEPTH,
  7705. .max_sectors = 32767,
  7706. .cmd_per_lun = 7,
  7707. .use_clustering = ENABLE_CLUSTERING,
  7708. .shost_attrs = mpt3sas_host_attrs,
  7709. .sdev_attrs = mpt3sas_dev_attrs,
  7710. .track_queue_depth = 1,
  7711. };
  7712. /* raid transport support for SAS 2.0 HBA devices */
  7713. static struct raid_function_template mpt2sas_raid_functions = {
  7714. .cookie = &mpt2sas_driver_template,
  7715. .is_raid = scsih_is_raid,
  7716. .get_resync = scsih_get_resync,
  7717. .get_state = scsih_get_state,
  7718. };
  7719. /* shost template for SAS 3.0 HBA devices */
  7720. static struct scsi_host_template mpt3sas_driver_template = {
  7721. .module = THIS_MODULE,
  7722. .name = "Fusion MPT SAS Host",
  7723. .proc_name = MPT3SAS_DRIVER_NAME,
  7724. .queuecommand = scsih_qcmd,
  7725. .target_alloc = scsih_target_alloc,
  7726. .slave_alloc = scsih_slave_alloc,
  7727. .slave_configure = scsih_slave_configure,
  7728. .target_destroy = scsih_target_destroy,
  7729. .slave_destroy = scsih_slave_destroy,
  7730. .scan_finished = scsih_scan_finished,
  7731. .scan_start = scsih_scan_start,
  7732. .change_queue_depth = scsih_change_queue_depth,
  7733. .eh_abort_handler = scsih_abort,
  7734. .eh_device_reset_handler = scsih_dev_reset,
  7735. .eh_target_reset_handler = scsih_target_reset,
  7736. .eh_host_reset_handler = scsih_host_reset,
  7737. .bios_param = scsih_bios_param,
  7738. .can_queue = 1,
  7739. .this_id = -1,
  7740. .sg_tablesize = MPT3SAS_SG_DEPTH,
  7741. .max_sectors = 32767,
  7742. .cmd_per_lun = 7,
  7743. .use_clustering = ENABLE_CLUSTERING,
  7744. .shost_attrs = mpt3sas_host_attrs,
  7745. .sdev_attrs = mpt3sas_dev_attrs,
  7746. .track_queue_depth = 1,
  7747. };
  7748. /* raid transport support for SAS 3.0 HBA devices */
  7749. static struct raid_function_template mpt3sas_raid_functions = {
  7750. .cookie = &mpt3sas_driver_template,
  7751. .is_raid = scsih_is_raid,
  7752. .get_resync = scsih_get_resync,
  7753. .get_state = scsih_get_state,
  7754. };
  7755. /**
  7756. * _scsih_determine_hba_mpi_version - determine in which MPI version class
  7757. * this device belongs to.
  7758. * @pdev: PCI device struct
  7759. *
  7760. * return MPI2_VERSION for SAS 2.0 HBA devices,
  7761. * MPI25_VERSION for SAS 3.0 HBA devices, and
  7762. * MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
  7763. */
  7764. static u16
  7765. _scsih_determine_hba_mpi_version(struct pci_dev *pdev)
  7766. {
  7767. switch (pdev->device) {
  7768. case MPI2_MFGPAGE_DEVID_SSS6200:
  7769. case MPI2_MFGPAGE_DEVID_SAS2004:
  7770. case MPI2_MFGPAGE_DEVID_SAS2008:
  7771. case MPI2_MFGPAGE_DEVID_SAS2108_1:
  7772. case MPI2_MFGPAGE_DEVID_SAS2108_2:
  7773. case MPI2_MFGPAGE_DEVID_SAS2108_3:
  7774. case MPI2_MFGPAGE_DEVID_SAS2116_1:
  7775. case MPI2_MFGPAGE_DEVID_SAS2116_2:
  7776. case MPI2_MFGPAGE_DEVID_SAS2208_1:
  7777. case MPI2_MFGPAGE_DEVID_SAS2208_2:
  7778. case MPI2_MFGPAGE_DEVID_SAS2208_3:
  7779. case MPI2_MFGPAGE_DEVID_SAS2208_4:
  7780. case MPI2_MFGPAGE_DEVID_SAS2208_5:
  7781. case MPI2_MFGPAGE_DEVID_SAS2208_6:
  7782. case MPI2_MFGPAGE_DEVID_SAS2308_1:
  7783. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  7784. case MPI2_MFGPAGE_DEVID_SAS2308_3:
  7785. return MPI2_VERSION;
  7786. case MPI25_MFGPAGE_DEVID_SAS3004:
  7787. case MPI25_MFGPAGE_DEVID_SAS3008:
  7788. case MPI25_MFGPAGE_DEVID_SAS3108_1:
  7789. case MPI25_MFGPAGE_DEVID_SAS3108_2:
  7790. case MPI25_MFGPAGE_DEVID_SAS3108_5:
  7791. case MPI25_MFGPAGE_DEVID_SAS3108_6:
  7792. return MPI25_VERSION;
  7793. case MPI26_MFGPAGE_DEVID_SAS3216:
  7794. case MPI26_MFGPAGE_DEVID_SAS3224:
  7795. case MPI26_MFGPAGE_DEVID_SAS3316_1:
  7796. case MPI26_MFGPAGE_DEVID_SAS3316_2:
  7797. case MPI26_MFGPAGE_DEVID_SAS3316_3:
  7798. case MPI26_MFGPAGE_DEVID_SAS3316_4:
  7799. case MPI26_MFGPAGE_DEVID_SAS3324_1:
  7800. case MPI26_MFGPAGE_DEVID_SAS3324_2:
  7801. case MPI26_MFGPAGE_DEVID_SAS3324_3:
  7802. case MPI26_MFGPAGE_DEVID_SAS3324_4:
  7803. return MPI26_VERSION;
  7804. }
  7805. return 0;
  7806. }
  7807. /**
  7808. * _scsih_probe - attach and add scsi host
  7809. * @pdev: PCI device struct
  7810. * @id: pci device id
  7811. *
  7812. * Returns 0 success, anything else error.
  7813. */
  7814. static int
  7815. _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  7816. {
  7817. struct MPT3SAS_ADAPTER *ioc;
  7818. struct Scsi_Host *shost = NULL;
  7819. int rv;
  7820. u16 hba_mpi_version;
  7821. /* Determine in which MPI version class this pci device belongs */
  7822. hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
  7823. if (hba_mpi_version == 0)
  7824. return -ENODEV;
  7825. /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
  7826. * for other generation HBA's return with -ENODEV
  7827. */
  7828. if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
  7829. return -ENODEV;
  7830. /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
  7831. * for other generation HBA's return with -ENODEV
  7832. */
  7833. if ((hbas_to_enumerate == 2) && (!(hba_mpi_version == MPI25_VERSION
  7834. || hba_mpi_version == MPI26_VERSION)))
  7835. return -ENODEV;
  7836. switch (hba_mpi_version) {
  7837. case MPI2_VERSION:
  7838. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
  7839. PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
  7840. /* Use mpt2sas driver host template for SAS 2.0 HBA's */
  7841. shost = scsi_host_alloc(&mpt2sas_driver_template,
  7842. sizeof(struct MPT3SAS_ADAPTER));
  7843. if (!shost)
  7844. return -ENODEV;
  7845. ioc = shost_priv(shost);
  7846. memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
  7847. ioc->hba_mpi_version_belonged = hba_mpi_version;
  7848. ioc->id = mpt2_ids++;
  7849. sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
  7850. if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
  7851. ioc->is_warpdrive = 1;
  7852. ioc->hide_ir_msg = 1;
  7853. } else
  7854. ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
  7855. break;
  7856. case MPI25_VERSION:
  7857. case MPI26_VERSION:
  7858. /* Use mpt3sas driver host template for SAS 3.0 HBA's */
  7859. shost = scsi_host_alloc(&mpt3sas_driver_template,
  7860. sizeof(struct MPT3SAS_ADAPTER));
  7861. if (!shost)
  7862. return -ENODEV;
  7863. ioc = shost_priv(shost);
  7864. memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
  7865. ioc->hba_mpi_version_belonged = hba_mpi_version;
  7866. ioc->id = mpt3_ids++;
  7867. sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
  7868. if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
  7869. pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
  7870. (ioc->hba_mpi_version_belonged == MPI26_VERSION))
  7871. ioc->msix96_vector = 1;
  7872. break;
  7873. default:
  7874. return -ENODEV;
  7875. }
  7876. INIT_LIST_HEAD(&ioc->list);
  7877. spin_lock(&gioc_lock);
  7878. list_add_tail(&ioc->list, &mpt3sas_ioc_list);
  7879. spin_unlock(&gioc_lock);
  7880. ioc->shost = shost;
  7881. ioc->pdev = pdev;
  7882. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  7883. ioc->tm_cb_idx = tm_cb_idx;
  7884. ioc->ctl_cb_idx = ctl_cb_idx;
  7885. ioc->base_cb_idx = base_cb_idx;
  7886. ioc->port_enable_cb_idx = port_enable_cb_idx;
  7887. ioc->transport_cb_idx = transport_cb_idx;
  7888. ioc->scsih_cb_idx = scsih_cb_idx;
  7889. ioc->config_cb_idx = config_cb_idx;
  7890. ioc->tm_tr_cb_idx = tm_tr_cb_idx;
  7891. ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
  7892. ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
  7893. ioc->logging_level = logging_level;
  7894. ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
  7895. /* misc semaphores and spin locks */
  7896. mutex_init(&ioc->reset_in_progress_mutex);
  7897. /* initializing pci_access_mutex lock */
  7898. mutex_init(&ioc->pci_access_mutex);
  7899. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  7900. spin_lock_init(&ioc->scsi_lookup_lock);
  7901. spin_lock_init(&ioc->sas_device_lock);
  7902. spin_lock_init(&ioc->sas_node_lock);
  7903. spin_lock_init(&ioc->fw_event_lock);
  7904. spin_lock_init(&ioc->raid_device_lock);
  7905. spin_lock_init(&ioc->diag_trigger_lock);
  7906. INIT_LIST_HEAD(&ioc->sas_device_list);
  7907. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  7908. INIT_LIST_HEAD(&ioc->sas_expander_list);
  7909. INIT_LIST_HEAD(&ioc->fw_event_list);
  7910. INIT_LIST_HEAD(&ioc->raid_device_list);
  7911. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  7912. INIT_LIST_HEAD(&ioc->delayed_tr_list);
  7913. INIT_LIST_HEAD(&ioc->delayed_sc_list);
  7914. INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
  7915. INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
  7916. INIT_LIST_HEAD(&ioc->reply_queue_list);
  7917. sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
  7918. /* init shost parameters */
  7919. shost->max_cmd_len = 32;
  7920. shost->max_lun = max_lun;
  7921. shost->transportt = mpt3sas_transport_template;
  7922. shost->unique_id = ioc->id;
  7923. if (max_sectors != 0xFFFF) {
  7924. if (max_sectors < 64) {
  7925. shost->max_sectors = 64;
  7926. pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
  7927. "for max_sectors, range is 64 to 32767. Assigning "
  7928. "value of 64.\n", ioc->name, max_sectors);
  7929. } else if (max_sectors > 32767) {
  7930. shost->max_sectors = 32767;
  7931. pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
  7932. "for max_sectors, range is 64 to 32767. Assigning "
  7933. "default value of 32767.\n", ioc->name,
  7934. max_sectors);
  7935. } else {
  7936. shost->max_sectors = max_sectors & 0xFFFE;
  7937. pr_info(MPT3SAS_FMT
  7938. "The max_sectors value is set to %d\n",
  7939. ioc->name, shost->max_sectors);
  7940. }
  7941. }
  7942. /* register EEDP capabilities with SCSI layer */
  7943. if (prot_mask > 0)
  7944. scsi_host_set_prot(shost, prot_mask);
  7945. else
  7946. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  7947. | SHOST_DIF_TYPE2_PROTECTION
  7948. | SHOST_DIF_TYPE3_PROTECTION);
  7949. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  7950. /* event thread */
  7951. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  7952. "fw_event_%s%d", ioc->driver_name, ioc->id);
  7953. ioc->firmware_event_thread = alloc_ordered_workqueue(
  7954. ioc->firmware_event_name, WQ_MEM_RECLAIM);
  7955. if (!ioc->firmware_event_thread) {
  7956. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7957. ioc->name, __FILE__, __LINE__, __func__);
  7958. rv = -ENODEV;
  7959. goto out_thread_fail;
  7960. }
  7961. ioc->is_driver_loading = 1;
  7962. if ((mpt3sas_base_attach(ioc))) {
  7963. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7964. ioc->name, __FILE__, __LINE__, __func__);
  7965. rv = -ENODEV;
  7966. goto out_attach_fail;
  7967. }
  7968. if (ioc->is_warpdrive) {
  7969. if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
  7970. ioc->hide_drives = 0;
  7971. else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
  7972. ioc->hide_drives = 1;
  7973. else {
  7974. if (mpt3sas_get_num_volumes(ioc))
  7975. ioc->hide_drives = 1;
  7976. else
  7977. ioc->hide_drives = 0;
  7978. }
  7979. } else
  7980. ioc->hide_drives = 0;
  7981. rv = scsi_add_host(shost, &pdev->dev);
  7982. if (rv) {
  7983. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  7984. ioc->name, __FILE__, __LINE__, __func__);
  7985. goto out_add_shost_fail;
  7986. }
  7987. scsi_scan_host(shost);
  7988. return 0;
  7989. out_add_shost_fail:
  7990. mpt3sas_base_detach(ioc);
  7991. out_attach_fail:
  7992. destroy_workqueue(ioc->firmware_event_thread);
  7993. out_thread_fail:
  7994. spin_lock(&gioc_lock);
  7995. list_del(&ioc->list);
  7996. spin_unlock(&gioc_lock);
  7997. scsi_host_put(shost);
  7998. return rv;
  7999. }
  8000. #ifdef CONFIG_PM
  8001. /**
  8002. * scsih_suspend - power management suspend main entry point
  8003. * @pdev: PCI device struct
  8004. * @state: PM state change to (usually PCI_D3)
  8005. *
  8006. * Returns 0 success, anything else error.
  8007. */
  8008. static int
  8009. scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  8010. {
  8011. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8012. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8013. pci_power_t device_state;
  8014. mpt3sas_base_stop_watchdog(ioc);
  8015. flush_scheduled_work();
  8016. scsi_block_requests(shost);
  8017. device_state = pci_choose_state(pdev, state);
  8018. pr_info(MPT3SAS_FMT
  8019. "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
  8020. ioc->name, pdev, pci_name(pdev), device_state);
  8021. pci_save_state(pdev);
  8022. mpt3sas_base_free_resources(ioc);
  8023. pci_set_power_state(pdev, device_state);
  8024. return 0;
  8025. }
  8026. /**
  8027. * scsih_resume - power management resume main entry point
  8028. * @pdev: PCI device struct
  8029. *
  8030. * Returns 0 success, anything else error.
  8031. */
  8032. static int
  8033. scsih_resume(struct pci_dev *pdev)
  8034. {
  8035. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8036. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8037. pci_power_t device_state = pdev->current_state;
  8038. int r;
  8039. pr_info(MPT3SAS_FMT
  8040. "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
  8041. ioc->name, pdev, pci_name(pdev), device_state);
  8042. pci_set_power_state(pdev, PCI_D0);
  8043. pci_enable_wake(pdev, PCI_D0, 0);
  8044. pci_restore_state(pdev);
  8045. ioc->pdev = pdev;
  8046. r = mpt3sas_base_map_resources(ioc);
  8047. if (r)
  8048. return r;
  8049. mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
  8050. scsi_unblock_requests(shost);
  8051. mpt3sas_base_start_watchdog(ioc);
  8052. return 0;
  8053. }
  8054. #endif /* CONFIG_PM */
  8055. /**
  8056. * scsih_pci_error_detected - Called when a PCI error is detected.
  8057. * @pdev: PCI device struct
  8058. * @state: PCI channel state
  8059. *
  8060. * Description: Called when a PCI error is detected.
  8061. *
  8062. * Return value:
  8063. * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  8064. */
  8065. static pci_ers_result_t
  8066. scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  8067. {
  8068. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8069. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8070. pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
  8071. ioc->name, state);
  8072. switch (state) {
  8073. case pci_channel_io_normal:
  8074. return PCI_ERS_RESULT_CAN_RECOVER;
  8075. case pci_channel_io_frozen:
  8076. /* Fatal error, prepare for slot reset */
  8077. ioc->pci_error_recovery = 1;
  8078. scsi_block_requests(ioc->shost);
  8079. mpt3sas_base_stop_watchdog(ioc);
  8080. mpt3sas_base_free_resources(ioc);
  8081. return PCI_ERS_RESULT_NEED_RESET;
  8082. case pci_channel_io_perm_failure:
  8083. /* Permanent error, prepare for device removal */
  8084. ioc->pci_error_recovery = 1;
  8085. mpt3sas_base_stop_watchdog(ioc);
  8086. _scsih_flush_running_cmds(ioc);
  8087. return PCI_ERS_RESULT_DISCONNECT;
  8088. }
  8089. return PCI_ERS_RESULT_NEED_RESET;
  8090. }
  8091. /**
  8092. * scsih_pci_slot_reset - Called when PCI slot has been reset.
  8093. * @pdev: PCI device struct
  8094. *
  8095. * Description: This routine is called by the pci error recovery
  8096. * code after the PCI slot has been reset, just before we
  8097. * should resume normal operations.
  8098. */
  8099. static pci_ers_result_t
  8100. scsih_pci_slot_reset(struct pci_dev *pdev)
  8101. {
  8102. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8103. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8104. int rc;
  8105. pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
  8106. ioc->name);
  8107. ioc->pci_error_recovery = 0;
  8108. ioc->pdev = pdev;
  8109. pci_restore_state(pdev);
  8110. rc = mpt3sas_base_map_resources(ioc);
  8111. if (rc)
  8112. return PCI_ERS_RESULT_DISCONNECT;
  8113. rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  8114. pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
  8115. (rc == 0) ? "success" : "failed");
  8116. if (!rc)
  8117. return PCI_ERS_RESULT_RECOVERED;
  8118. else
  8119. return PCI_ERS_RESULT_DISCONNECT;
  8120. }
  8121. /**
  8122. * scsih_pci_resume() - resume normal ops after PCI reset
  8123. * @pdev: pointer to PCI device
  8124. *
  8125. * Called when the error recovery driver tells us that its
  8126. * OK to resume normal operation. Use completion to allow
  8127. * halted scsi ops to resume.
  8128. */
  8129. static void
  8130. scsih_pci_resume(struct pci_dev *pdev)
  8131. {
  8132. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8133. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8134. pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
  8135. pci_cleanup_aer_uncorrect_error_status(pdev);
  8136. mpt3sas_base_start_watchdog(ioc);
  8137. scsi_unblock_requests(ioc->shost);
  8138. }
  8139. /**
  8140. * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
  8141. * @pdev: pointer to PCI device
  8142. */
  8143. static pci_ers_result_t
  8144. scsih_pci_mmio_enabled(struct pci_dev *pdev)
  8145. {
  8146. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  8147. struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
  8148. pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
  8149. ioc->name);
  8150. /* TODO - dump whatever for debugging purposes */
  8151. /* This called only if scsih_pci_error_detected returns
  8152. * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
  8153. * works, no need to reset slot.
  8154. */
  8155. return PCI_ERS_RESULT_RECOVERED;
  8156. }
  8157. /*
  8158. * The pci device ids are defined in mpi/mpi2_cnfg.h.
  8159. */
  8160. static const struct pci_device_id mpt3sas_pci_table[] = {
  8161. /* Spitfire ~ 2004 */
  8162. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
  8163. PCI_ANY_ID, PCI_ANY_ID },
  8164. /* Falcon ~ 2008 */
  8165. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
  8166. PCI_ANY_ID, PCI_ANY_ID },
  8167. /* Liberator ~ 2108 */
  8168. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
  8169. PCI_ANY_ID, PCI_ANY_ID },
  8170. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
  8171. PCI_ANY_ID, PCI_ANY_ID },
  8172. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
  8173. PCI_ANY_ID, PCI_ANY_ID },
  8174. /* Meteor ~ 2116 */
  8175. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
  8176. PCI_ANY_ID, PCI_ANY_ID },
  8177. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
  8178. PCI_ANY_ID, PCI_ANY_ID },
  8179. /* Thunderbolt ~ 2208 */
  8180. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
  8181. PCI_ANY_ID, PCI_ANY_ID },
  8182. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
  8183. PCI_ANY_ID, PCI_ANY_ID },
  8184. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
  8185. PCI_ANY_ID, PCI_ANY_ID },
  8186. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
  8187. PCI_ANY_ID, PCI_ANY_ID },
  8188. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
  8189. PCI_ANY_ID, PCI_ANY_ID },
  8190. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
  8191. PCI_ANY_ID, PCI_ANY_ID },
  8192. /* Mustang ~ 2308 */
  8193. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
  8194. PCI_ANY_ID, PCI_ANY_ID },
  8195. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
  8196. PCI_ANY_ID, PCI_ANY_ID },
  8197. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
  8198. PCI_ANY_ID, PCI_ANY_ID },
  8199. /* SSS6200 */
  8200. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
  8201. PCI_ANY_ID, PCI_ANY_ID },
  8202. /* Fury ~ 3004 and 3008 */
  8203. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
  8204. PCI_ANY_ID, PCI_ANY_ID },
  8205. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
  8206. PCI_ANY_ID, PCI_ANY_ID },
  8207. /* Invader ~ 3108 */
  8208. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
  8209. PCI_ANY_ID, PCI_ANY_ID },
  8210. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
  8211. PCI_ANY_ID, PCI_ANY_ID },
  8212. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
  8213. PCI_ANY_ID, PCI_ANY_ID },
  8214. { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
  8215. PCI_ANY_ID, PCI_ANY_ID },
  8216. /* Cutlass ~ 3216 and 3224 */
  8217. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
  8218. PCI_ANY_ID, PCI_ANY_ID },
  8219. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
  8220. PCI_ANY_ID, PCI_ANY_ID },
  8221. /* Intruder ~ 3316 and 3324 */
  8222. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
  8223. PCI_ANY_ID, PCI_ANY_ID },
  8224. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
  8225. PCI_ANY_ID, PCI_ANY_ID },
  8226. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
  8227. PCI_ANY_ID, PCI_ANY_ID },
  8228. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
  8229. PCI_ANY_ID, PCI_ANY_ID },
  8230. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
  8231. PCI_ANY_ID, PCI_ANY_ID },
  8232. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
  8233. PCI_ANY_ID, PCI_ANY_ID },
  8234. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
  8235. PCI_ANY_ID, PCI_ANY_ID },
  8236. { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
  8237. PCI_ANY_ID, PCI_ANY_ID },
  8238. {0} /* Terminating entry */
  8239. };
  8240. MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
  8241. static struct pci_error_handlers _mpt3sas_err_handler = {
  8242. .error_detected = scsih_pci_error_detected,
  8243. .mmio_enabled = scsih_pci_mmio_enabled,
  8244. .slot_reset = scsih_pci_slot_reset,
  8245. .resume = scsih_pci_resume,
  8246. };
  8247. static struct pci_driver mpt3sas_driver = {
  8248. .name = MPT3SAS_DRIVER_NAME,
  8249. .id_table = mpt3sas_pci_table,
  8250. .probe = _scsih_probe,
  8251. .remove = scsih_remove,
  8252. .shutdown = scsih_shutdown,
  8253. .err_handler = &_mpt3sas_err_handler,
  8254. #ifdef CONFIG_PM
  8255. .suspend = scsih_suspend,
  8256. .resume = scsih_resume,
  8257. #endif
  8258. };
  8259. /**
  8260. * scsih_init - main entry point for this driver.
  8261. *
  8262. * Returns 0 success, anything else error.
  8263. */
  8264. static int
  8265. scsih_init(void)
  8266. {
  8267. mpt2_ids = 0;
  8268. mpt3_ids = 0;
  8269. mpt3sas_base_initialize_callback_handler();
  8270. /* queuecommand callback hander */
  8271. scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
  8272. /* task managment callback handler */
  8273. tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
  8274. /* base internal commands callback handler */
  8275. base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
  8276. port_enable_cb_idx = mpt3sas_base_register_callback_handler(
  8277. mpt3sas_port_enable_done);
  8278. /* transport internal commands callback handler */
  8279. transport_cb_idx = mpt3sas_base_register_callback_handler(
  8280. mpt3sas_transport_done);
  8281. /* scsih internal commands callback handler */
  8282. scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
  8283. /* configuration page API internal commands callback handler */
  8284. config_cb_idx = mpt3sas_base_register_callback_handler(
  8285. mpt3sas_config_done);
  8286. /* ctl module callback handler */
  8287. ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
  8288. tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
  8289. _scsih_tm_tr_complete);
  8290. tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
  8291. _scsih_tm_volume_tr_complete);
  8292. tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
  8293. _scsih_sas_control_complete);
  8294. return 0;
  8295. }
  8296. /**
  8297. * scsih_exit - exit point for this driver (when it is a module).
  8298. *
  8299. * Returns 0 success, anything else error.
  8300. */
  8301. static void
  8302. scsih_exit(void)
  8303. {
  8304. mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
  8305. mpt3sas_base_release_callback_handler(tm_cb_idx);
  8306. mpt3sas_base_release_callback_handler(base_cb_idx);
  8307. mpt3sas_base_release_callback_handler(port_enable_cb_idx);
  8308. mpt3sas_base_release_callback_handler(transport_cb_idx);
  8309. mpt3sas_base_release_callback_handler(scsih_cb_idx);
  8310. mpt3sas_base_release_callback_handler(config_cb_idx);
  8311. mpt3sas_base_release_callback_handler(ctl_cb_idx);
  8312. mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
  8313. mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
  8314. mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
  8315. /* raid transport support */
  8316. if (hbas_to_enumerate != 1)
  8317. raid_class_release(mpt3sas_raid_template);
  8318. if (hbas_to_enumerate != 2)
  8319. raid_class_release(mpt2sas_raid_template);
  8320. sas_release_transport(mpt3sas_transport_template);
  8321. }
  8322. /**
  8323. * _mpt3sas_init - main entry point for this driver.
  8324. *
  8325. * Returns 0 success, anything else error.
  8326. */
  8327. static int __init
  8328. _mpt3sas_init(void)
  8329. {
  8330. int error;
  8331. pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
  8332. MPT3SAS_DRIVER_VERSION);
  8333. mpt3sas_transport_template =
  8334. sas_attach_transport(&mpt3sas_transport_functions);
  8335. if (!mpt3sas_transport_template)
  8336. return -ENODEV;
  8337. /* No need attach mpt3sas raid functions template
  8338. * if hbas_to_enumarate value is one.
  8339. */
  8340. if (hbas_to_enumerate != 1) {
  8341. mpt3sas_raid_template =
  8342. raid_class_attach(&mpt3sas_raid_functions);
  8343. if (!mpt3sas_raid_template) {
  8344. sas_release_transport(mpt3sas_transport_template);
  8345. return -ENODEV;
  8346. }
  8347. }
  8348. /* No need to attach mpt2sas raid functions template
  8349. * if hbas_to_enumarate value is two
  8350. */
  8351. if (hbas_to_enumerate != 2) {
  8352. mpt2sas_raid_template =
  8353. raid_class_attach(&mpt2sas_raid_functions);
  8354. if (!mpt2sas_raid_template) {
  8355. sas_release_transport(mpt3sas_transport_template);
  8356. return -ENODEV;
  8357. }
  8358. }
  8359. error = scsih_init();
  8360. if (error) {
  8361. scsih_exit();
  8362. return error;
  8363. }
  8364. mpt3sas_ctl_init(hbas_to_enumerate);
  8365. error = pci_register_driver(&mpt3sas_driver);
  8366. if (error)
  8367. scsih_exit();
  8368. return error;
  8369. }
  8370. /**
  8371. * _mpt3sas_exit - exit point for this driver (when it is a module).
  8372. *
  8373. */
  8374. static void __exit
  8375. _mpt3sas_exit(void)
  8376. {
  8377. pr_info("mpt3sas version %s unloading\n",
  8378. MPT3SAS_DRIVER_VERSION);
  8379. pci_unregister_driver(&mpt3sas_driver);
  8380. mpt3sas_ctl_exit(hbas_to_enumerate);
  8381. scsih_exit();
  8382. }
  8383. module_init(_mpt3sas_init);
  8384. module_exit(_mpt3sas_exit);