mpt2sas_scsih.c 222 KB

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