mpt2sas_scsih.c 245 KB

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