mpt2sas_scsih.c 245 KB

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