mpt2sas_scsih.c 241 KB

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