mpt2sas_scsih.c 245 KB

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