mpt2sas_scsih.c 237 KB

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