mpt2sas_scsih.c 226 KB

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