mpt2sas_scsih.c 245 KB

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