mpt2sas_scsih.c 240 KB

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