mpt2sas_scsih.c 245 KB

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