mpt2sas_scsih.c 241 KB

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