mpt2sas_scsih.c 245 KB

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