mpt2sas_scsih.c 245 KB

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