mpt2sas_scsih.c 189 KB

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