mpt2sas_scsih.c 205 KB

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