mpt2sas_scsih.c 247 KB

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