mpt2sas_scsih.c 189 KB

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