mpt2sas_scsih.c 191 KB

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