mpt2sas_scsih.c 196 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925
  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-2010 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/version.h>
  42. #include <linux/module.h>
  43. #include <linux/kernel.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/sched.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/delay.h>
  50. #include <linux/pci.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/aer.h>
  53. #include <linux/raid_class.h>
  54. #include <linux/slab.h>
  55. #include "mpt2sas_base.h"
  56. MODULE_AUTHOR(MPT2SAS_AUTHOR);
  57. MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
  58. MODULE_LICENSE("GPL");
  59. MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
  60. #define RAID_CHANNEL 1
  61. /* forward proto's */
  62. static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  63. struct _sas_node *sas_expander);
  64. static void _firmware_event_work(struct work_struct *work);
  65. /* global parameters */
  66. LIST_HEAD(mpt2sas_ioc_list);
  67. /* local parameters */
  68. static u8 scsi_io_cb_idx = -1;
  69. static u8 tm_cb_idx = -1;
  70. static u8 ctl_cb_idx = -1;
  71. static u8 base_cb_idx = -1;
  72. static u8 transport_cb_idx = -1;
  73. static u8 scsih_cb_idx = -1;
  74. static u8 config_cb_idx = -1;
  75. static int mpt_ids;
  76. static u8 tm_tr_cb_idx = -1 ;
  77. static u8 tm_sas_control_cb_idx = -1;
  78. /* command line options */
  79. static u32 logging_level;
  80. MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
  81. "(default=0)");
  82. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  83. #define MPT2SAS_MAX_LUN (16895)
  84. static int max_lun = MPT2SAS_MAX_LUN;
  85. module_param(max_lun, int, 0);
  86. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  87. /**
  88. * struct sense_info - common structure for obtaining sense keys
  89. * @skey: sense key
  90. * @asc: additional sense code
  91. * @ascq: additional sense code qualifier
  92. */
  93. struct sense_info {
  94. u8 skey;
  95. u8 asc;
  96. u8 ascq;
  97. };
  98. #define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
  99. /**
  100. * struct fw_event_work - firmware event struct
  101. * @list: link list framework
  102. * @work: work object (ioc->fault_reset_work_q)
  103. * @cancel_pending_work: flag set during reset handling
  104. * @ioc: per adapter object
  105. * @VF_ID: virtual function id
  106. * @VP_ID: virtual port id
  107. * @ignore: flag meaning this event has been marked to ignore
  108. * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
  109. * @event_data: reply event data payload follows
  110. *
  111. * This object stored on ioc->fw_event_list.
  112. */
  113. struct fw_event_work {
  114. struct list_head list;
  115. u8 cancel_pending_work;
  116. struct delayed_work delayed_work;
  117. struct MPT2SAS_ADAPTER *ioc;
  118. u8 VF_ID;
  119. u8 VP_ID;
  120. u8 ignore;
  121. u16 event;
  122. void *event_data;
  123. };
  124. /* raid transport support */
  125. static struct raid_template *mpt2sas_raid_template;
  126. /**
  127. * struct _scsi_io_transfer - scsi io transfer
  128. * @handle: sas device handle (assigned by firmware)
  129. * @is_raid: flag set for hidden raid components
  130. * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
  131. * @data_length: data transfer length
  132. * @data_dma: dma pointer to data
  133. * @sense: sense data
  134. * @lun: lun number
  135. * @cdb_length: cdb length
  136. * @cdb: cdb contents
  137. * @timeout: timeout for this command
  138. * @VF_ID: virtual function id
  139. * @VP_ID: virtual port id
  140. * @valid_reply: flag set for reply message
  141. * @sense_length: sense length
  142. * @ioc_status: ioc status
  143. * @scsi_state: scsi state
  144. * @scsi_status: scsi staus
  145. * @log_info: log information
  146. * @transfer_length: data length transfer when there is a reply message
  147. *
  148. * Used for sending internal scsi commands to devices within this module.
  149. * Refer to _scsi_send_scsi_io().
  150. */
  151. struct _scsi_io_transfer {
  152. u16 handle;
  153. u8 is_raid;
  154. enum dma_data_direction dir;
  155. u32 data_length;
  156. dma_addr_t data_dma;
  157. u8 sense[SCSI_SENSE_BUFFERSIZE];
  158. u32 lun;
  159. u8 cdb_length;
  160. u8 cdb[32];
  161. u8 timeout;
  162. u8 VF_ID;
  163. u8 VP_ID;
  164. u8 valid_reply;
  165. /* the following bits are only valid when 'valid_reply = 1' */
  166. u32 sense_length;
  167. u16 ioc_status;
  168. u8 scsi_state;
  169. u8 scsi_status;
  170. u32 log_info;
  171. u32 transfer_length;
  172. };
  173. /*
  174. * The pci device ids are defined in mpi/mpi2_cnfg.h.
  175. */
  176. static struct pci_device_id scsih_pci_table[] = {
  177. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
  178. PCI_ANY_ID, PCI_ANY_ID },
  179. /* Falcon ~ 2008*/
  180. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
  181. PCI_ANY_ID, PCI_ANY_ID },
  182. /* Liberator ~ 2108 */
  183. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
  184. PCI_ANY_ID, PCI_ANY_ID },
  185. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
  186. PCI_ANY_ID, PCI_ANY_ID },
  187. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
  188. PCI_ANY_ID, PCI_ANY_ID },
  189. /* Meteor ~ 2116 */
  190. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
  191. PCI_ANY_ID, PCI_ANY_ID },
  192. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
  193. PCI_ANY_ID, PCI_ANY_ID },
  194. /* Thunderbolt ~ 2208 */
  195. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
  196. PCI_ANY_ID, PCI_ANY_ID },
  197. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
  198. PCI_ANY_ID, PCI_ANY_ID },
  199. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
  200. PCI_ANY_ID, PCI_ANY_ID },
  201. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
  202. PCI_ANY_ID, PCI_ANY_ID },
  203. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
  204. PCI_ANY_ID, PCI_ANY_ID },
  205. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
  206. PCI_ANY_ID, PCI_ANY_ID },
  207. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7,
  208. PCI_ANY_ID, PCI_ANY_ID },
  209. { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8,
  210. PCI_ANY_ID, PCI_ANY_ID },
  211. {0} /* Terminating entry */
  212. };
  213. MODULE_DEVICE_TABLE(pci, scsih_pci_table);
  214. /**
  215. * _scsih_set_debug_level - global setting of ioc->logging_level.
  216. *
  217. * Note: The logging levels are defined in mpt2sas_debug.h.
  218. */
  219. static int
  220. _scsih_set_debug_level(const char *val, struct kernel_param *kp)
  221. {
  222. int ret = param_set_int(val, kp);
  223. struct MPT2SAS_ADAPTER *ioc;
  224. if (ret)
  225. return ret;
  226. printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
  227. list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
  228. ioc->logging_level = logging_level;
  229. return 0;
  230. }
  231. module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
  232. &logging_level, 0644);
  233. /**
  234. * _scsih_srch_boot_sas_address - search based on sas_address
  235. * @sas_address: sas address
  236. * @boot_device: boot device object from bios page 2
  237. *
  238. * Returns 1 when there's a match, 0 means no match.
  239. */
  240. static inline int
  241. _scsih_srch_boot_sas_address(u64 sas_address,
  242. Mpi2BootDeviceSasWwid_t *boot_device)
  243. {
  244. return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
  245. }
  246. /**
  247. * _scsih_srch_boot_device_name - search based on device name
  248. * @device_name: device name specified in INDENTIFY fram
  249. * @boot_device: boot device object from bios page 2
  250. *
  251. * Returns 1 when there's a match, 0 means no match.
  252. */
  253. static inline int
  254. _scsih_srch_boot_device_name(u64 device_name,
  255. Mpi2BootDeviceDeviceName_t *boot_device)
  256. {
  257. return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
  258. }
  259. /**
  260. * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
  261. * @enclosure_logical_id: enclosure logical id
  262. * @slot_number: slot number
  263. * @boot_device: boot device object from bios page 2
  264. *
  265. * Returns 1 when there's a match, 0 means no match.
  266. */
  267. static inline int
  268. _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
  269. Mpi2BootDeviceEnclosureSlot_t *boot_device)
  270. {
  271. return (enclosure_logical_id == le64_to_cpu(boot_device->
  272. EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
  273. SlotNumber)) ? 1 : 0;
  274. }
  275. /**
  276. * _scsih_is_boot_device - search for matching boot device.
  277. * @sas_address: sas address
  278. * @device_name: device name specified in INDENTIFY fram
  279. * @enclosure_logical_id: enclosure logical id
  280. * @slot_number: slot number
  281. * @form: specifies boot device form
  282. * @boot_device: boot device object from bios page 2
  283. *
  284. * Returns 1 when there's a match, 0 means no match.
  285. */
  286. static int
  287. _scsih_is_boot_device(u64 sas_address, u64 device_name,
  288. u64 enclosure_logical_id, u16 slot, u8 form,
  289. Mpi2BiosPage2BootDevice_t *boot_device)
  290. {
  291. int rc = 0;
  292. switch (form) {
  293. case MPI2_BIOSPAGE2_FORM_SAS_WWID:
  294. if (!sas_address)
  295. break;
  296. rc = _scsih_srch_boot_sas_address(
  297. sas_address, &boot_device->SasWwid);
  298. break;
  299. case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
  300. if (!enclosure_logical_id)
  301. break;
  302. rc = _scsih_srch_boot_encl_slot(
  303. enclosure_logical_id,
  304. slot, &boot_device->EnclosureSlot);
  305. break;
  306. case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
  307. if (!device_name)
  308. break;
  309. rc = _scsih_srch_boot_device_name(
  310. device_name, &boot_device->DeviceName);
  311. break;
  312. case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
  313. break;
  314. }
  315. return rc;
  316. }
  317. /**
  318. * _scsih_get_sas_address - set the sas_address for given device handle
  319. * @handle: device handle
  320. * @sas_address: sas address
  321. *
  322. * Returns 0 success, non-zero when failure
  323. */
  324. static int
  325. _scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  326. u64 *sas_address)
  327. {
  328. Mpi2SasDevicePage0_t sas_device_pg0;
  329. Mpi2ConfigReply_t mpi_reply;
  330. u32 ioc_status;
  331. if (handle <= ioc->sas_hba.num_phys) {
  332. *sas_address = ioc->sas_hba.sas_address;
  333. return 0;
  334. } else
  335. *sas_address = 0;
  336. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  337. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  338. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  339. ioc->name, __FILE__, __LINE__, __func__);
  340. return -ENXIO;
  341. }
  342. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  343. MPI2_IOCSTATUS_MASK;
  344. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  345. printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
  346. "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
  347. __FILE__, __LINE__, __func__);
  348. return -EIO;
  349. }
  350. *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  351. return 0;
  352. }
  353. /**
  354. * _scsih_determine_boot_device - determine boot device.
  355. * @ioc: per adapter object
  356. * @device: either sas_device or raid_device object
  357. * @is_raid: [flag] 1 = raid object, 0 = sas object
  358. *
  359. * Determines whether this device should be first reported device to
  360. * to scsi-ml or sas transport, this purpose is for persistant boot device.
  361. * There are primary, alternate, and current entries in bios page 2. The order
  362. * priority is primary, alternate, then current. This routine saves
  363. * the corresponding device object and is_raid flag in the ioc object.
  364. * The saved data to be used later in _scsih_probe_boot_devices().
  365. */
  366. static void
  367. _scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
  368. void *device, u8 is_raid)
  369. {
  370. struct _sas_device *sas_device;
  371. struct _raid_device *raid_device;
  372. u64 sas_address;
  373. u64 device_name;
  374. u64 enclosure_logical_id;
  375. u16 slot;
  376. /* only process this function when driver loads */
  377. if (!ioc->wait_for_port_enable_to_complete)
  378. return;
  379. if (!is_raid) {
  380. sas_device = device;
  381. sas_address = sas_device->sas_address;
  382. device_name = sas_device->device_name;
  383. enclosure_logical_id = sas_device->enclosure_logical_id;
  384. slot = sas_device->slot;
  385. } else {
  386. raid_device = device;
  387. sas_address = raid_device->wwid;
  388. device_name = 0;
  389. enclosure_logical_id = 0;
  390. slot = 0;
  391. }
  392. if (!ioc->req_boot_device.device) {
  393. if (_scsih_is_boot_device(sas_address, device_name,
  394. enclosure_logical_id, slot,
  395. (ioc->bios_pg2.ReqBootDeviceForm &
  396. MPI2_BIOSPAGE2_FORM_MASK),
  397. &ioc->bios_pg2.RequestedBootDevice)) {
  398. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  399. "%s: req_boot_device(0x%016llx)\n",
  400. ioc->name, __func__,
  401. (unsigned long long)sas_address));
  402. ioc->req_boot_device.device = device;
  403. ioc->req_boot_device.is_raid = is_raid;
  404. }
  405. }
  406. if (!ioc->req_alt_boot_device.device) {
  407. if (_scsih_is_boot_device(sas_address, device_name,
  408. enclosure_logical_id, slot,
  409. (ioc->bios_pg2.ReqAltBootDeviceForm &
  410. MPI2_BIOSPAGE2_FORM_MASK),
  411. &ioc->bios_pg2.RequestedAltBootDevice)) {
  412. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  413. "%s: req_alt_boot_device(0x%016llx)\n",
  414. ioc->name, __func__,
  415. (unsigned long long)sas_address));
  416. ioc->req_alt_boot_device.device = device;
  417. ioc->req_alt_boot_device.is_raid = is_raid;
  418. }
  419. }
  420. if (!ioc->current_boot_device.device) {
  421. if (_scsih_is_boot_device(sas_address, device_name,
  422. enclosure_logical_id, slot,
  423. (ioc->bios_pg2.CurrentBootDeviceForm &
  424. MPI2_BIOSPAGE2_FORM_MASK),
  425. &ioc->bios_pg2.CurrentBootDevice)) {
  426. dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  427. "%s: current_boot_device(0x%016llx)\n",
  428. ioc->name, __func__,
  429. (unsigned long long)sas_address));
  430. ioc->current_boot_device.device = device;
  431. ioc->current_boot_device.is_raid = is_raid;
  432. }
  433. }
  434. }
  435. /**
  436. * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
  437. * @ioc: per adapter object
  438. * @sas_address: sas address
  439. * Context: Calling function should acquire ioc->sas_device_lock
  440. *
  441. * This searches for sas_device based on sas_address, then return sas_device
  442. * object.
  443. */
  444. struct _sas_device *
  445. mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  446. u64 sas_address)
  447. {
  448. struct _sas_device *sas_device;
  449. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  450. if (sas_device->sas_address == sas_address)
  451. return sas_device;
  452. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  453. if (sas_device->sas_address == sas_address)
  454. return sas_device;
  455. return NULL;
  456. }
  457. /**
  458. * _scsih_sas_device_find_by_handle - sas device search
  459. * @ioc: per adapter object
  460. * @handle: sas device handle (assigned by firmware)
  461. * Context: Calling function should acquire ioc->sas_device_lock
  462. *
  463. * This searches for sas_device based on sas_address, then return sas_device
  464. * object.
  465. */
  466. static struct _sas_device *
  467. _scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  468. {
  469. struct _sas_device *sas_device;
  470. list_for_each_entry(sas_device, &ioc->sas_device_list, list)
  471. if (sas_device->handle == handle)
  472. return sas_device;
  473. list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
  474. if (sas_device->handle == handle)
  475. return sas_device;
  476. return NULL;
  477. }
  478. /**
  479. * _scsih_sas_device_remove - remove sas_device from list.
  480. * @ioc: per adapter object
  481. * @sas_device: the sas_device object
  482. * Context: This function will acquire ioc->sas_device_lock.
  483. *
  484. * Removing object and freeing associated memory from the ioc->sas_device_list.
  485. */
  486. static void
  487. _scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
  488. struct _sas_device *sas_device)
  489. {
  490. unsigned long flags;
  491. if (!sas_device)
  492. return;
  493. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  494. if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  495. sas_device->sas_address)) {
  496. list_del(&sas_device->list);
  497. kfree(sas_device);
  498. }
  499. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  500. }
  501. /**
  502. * _scsih_sas_device_add - insert sas_device to the list.
  503. * @ioc: per adapter object
  504. * @sas_device: the sas_device object
  505. * Context: This function will acquire ioc->sas_device_lock.
  506. *
  507. * Adding new object to the ioc->sas_device_list.
  508. */
  509. static void
  510. _scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
  511. struct _sas_device *sas_device)
  512. {
  513. unsigned long flags;
  514. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  515. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  516. sas_device->handle, (unsigned long long)sas_device->sas_address));
  517. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  518. list_add_tail(&sas_device->list, &ioc->sas_device_list);
  519. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  520. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  521. sas_device->sas_address_parent))
  522. _scsih_sas_device_remove(ioc, sas_device);
  523. }
  524. /**
  525. * _scsih_sas_device_init_add - insert sas_device to the list.
  526. * @ioc: per adapter object
  527. * @sas_device: the sas_device object
  528. * Context: This function will acquire ioc->sas_device_lock.
  529. *
  530. * Adding new object at driver load time to the ioc->sas_device_init_list.
  531. */
  532. static void
  533. _scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
  534. struct _sas_device *sas_device)
  535. {
  536. unsigned long flags;
  537. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  538. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  539. sas_device->handle, (unsigned long long)sas_device->sas_address));
  540. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  541. list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
  542. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  543. _scsih_determine_boot_device(ioc, sas_device, 0);
  544. }
  545. /**
  546. * _scsih_raid_device_find_by_id - raid device search
  547. * @ioc: per adapter object
  548. * @id: sas device target id
  549. * @channel: sas device channel
  550. * Context: Calling function should acquire ioc->raid_device_lock
  551. *
  552. * This searches for raid_device based on target id, then return raid_device
  553. * object.
  554. */
  555. static struct _raid_device *
  556. _scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
  557. {
  558. struct _raid_device *raid_device, *r;
  559. r = NULL;
  560. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  561. if (raid_device->id == id && raid_device->channel == channel) {
  562. r = raid_device;
  563. goto out;
  564. }
  565. }
  566. out:
  567. return r;
  568. }
  569. /**
  570. * _scsih_raid_device_find_by_handle - raid device search
  571. * @ioc: per adapter object
  572. * @handle: sas device handle (assigned by firmware)
  573. * Context: Calling function should acquire ioc->raid_device_lock
  574. *
  575. * This searches for raid_device based on handle, then return raid_device
  576. * object.
  577. */
  578. static struct _raid_device *
  579. _scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  580. {
  581. struct _raid_device *raid_device, *r;
  582. r = NULL;
  583. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  584. if (raid_device->handle != handle)
  585. continue;
  586. r = raid_device;
  587. goto out;
  588. }
  589. out:
  590. return r;
  591. }
  592. /**
  593. * _scsih_raid_device_find_by_wwid - raid device search
  594. * @ioc: per adapter object
  595. * @handle: sas device handle (assigned by firmware)
  596. * Context: Calling function should acquire ioc->raid_device_lock
  597. *
  598. * This searches for raid_device based on wwid, then return raid_device
  599. * object.
  600. */
  601. static struct _raid_device *
  602. _scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
  603. {
  604. struct _raid_device *raid_device, *r;
  605. r = NULL;
  606. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  607. if (raid_device->wwid != wwid)
  608. continue;
  609. r = raid_device;
  610. goto out;
  611. }
  612. out:
  613. return r;
  614. }
  615. /**
  616. * _scsih_raid_device_add - add raid_device object
  617. * @ioc: per adapter object
  618. * @raid_device: raid_device object
  619. *
  620. * This is added to the raid_device_list link list.
  621. */
  622. static void
  623. _scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
  624. struct _raid_device *raid_device)
  625. {
  626. unsigned long flags;
  627. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
  628. "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
  629. raid_device->handle, (unsigned long long)raid_device->wwid));
  630. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  631. list_add_tail(&raid_device->list, &ioc->raid_device_list);
  632. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  633. }
  634. /**
  635. * _scsih_raid_device_remove - delete raid_device object
  636. * @ioc: per adapter object
  637. * @raid_device: raid_device object
  638. *
  639. * This is removed from the raid_device_list link list.
  640. */
  641. static void
  642. _scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
  643. struct _raid_device *raid_device)
  644. {
  645. unsigned long flags;
  646. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  647. list_del(&raid_device->list);
  648. memset(raid_device, 0, sizeof(struct _raid_device));
  649. kfree(raid_device);
  650. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  651. }
  652. /**
  653. * mpt2sas_scsih_expander_find_by_handle - expander device search
  654. * @ioc: per adapter object
  655. * @handle: expander handle (assigned by firmware)
  656. * Context: Calling function should acquire ioc->sas_device_lock
  657. *
  658. * This searches for expander device based on handle, then returns the
  659. * sas_node object.
  660. */
  661. struct _sas_node *
  662. mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  663. {
  664. struct _sas_node *sas_expander, *r;
  665. r = NULL;
  666. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  667. if (sas_expander->handle != handle)
  668. continue;
  669. r = sas_expander;
  670. goto out;
  671. }
  672. out:
  673. return r;
  674. }
  675. /**
  676. * mpt2sas_scsih_expander_find_by_sas_address - expander device search
  677. * @ioc: per adapter object
  678. * @sas_address: sas address
  679. * Context: Calling function should acquire ioc->sas_node_lock.
  680. *
  681. * This searches for expander device based on sas_address, then returns the
  682. * sas_node object.
  683. */
  684. struct _sas_node *
  685. mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  686. u64 sas_address)
  687. {
  688. struct _sas_node *sas_expander, *r;
  689. r = NULL;
  690. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  691. if (sas_expander->sas_address != sas_address)
  692. continue;
  693. r = sas_expander;
  694. goto out;
  695. }
  696. out:
  697. return r;
  698. }
  699. /**
  700. * _scsih_expander_node_add - insert expander device to the list.
  701. * @ioc: per adapter object
  702. * @sas_expander: the sas_device object
  703. * Context: This function will acquire ioc->sas_node_lock.
  704. *
  705. * Adding new object to the ioc->sas_expander_list.
  706. *
  707. * Return nothing.
  708. */
  709. static void
  710. _scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
  711. struct _sas_node *sas_expander)
  712. {
  713. unsigned long flags;
  714. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  715. list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
  716. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  717. }
  718. /**
  719. * _scsih_is_end_device - determines if device is an end device
  720. * @device_info: bitfield providing information about the device.
  721. * Context: none
  722. *
  723. * Returns 1 if end device.
  724. */
  725. static int
  726. _scsih_is_end_device(u32 device_info)
  727. {
  728. if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
  729. ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
  730. (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
  731. (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
  732. return 1;
  733. else
  734. return 0;
  735. }
  736. /**
  737. * mptscsih_get_scsi_lookup - returns scmd entry
  738. * @ioc: per adapter object
  739. * @smid: system request message index
  740. *
  741. * Returns the smid stored scmd pointer.
  742. */
  743. static struct scsi_cmnd *
  744. _scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  745. {
  746. return ioc->scsi_lookup[smid - 1].scmd;
  747. }
  748. /**
  749. * _scsih_scsi_lookup_find_by_scmd - scmd lookup
  750. * @ioc: per adapter object
  751. * @smid: system request message index
  752. * @scmd: pointer to scsi command object
  753. * Context: This function will acquire ioc->scsi_lookup_lock.
  754. *
  755. * This will search for a scmd pointer in the scsi_lookup array,
  756. * returning the revelent smid. A returned value of zero means invalid.
  757. */
  758. static u16
  759. _scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
  760. *scmd)
  761. {
  762. u16 smid;
  763. unsigned long flags;
  764. int i;
  765. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  766. smid = 0;
  767. for (i = 0; i < ioc->scsiio_depth; i++) {
  768. if (ioc->scsi_lookup[i].scmd == scmd) {
  769. smid = ioc->scsi_lookup[i].smid;
  770. goto out;
  771. }
  772. }
  773. out:
  774. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  775. return smid;
  776. }
  777. /**
  778. * _scsih_scsi_lookup_find_by_target - search for matching channel:id
  779. * @ioc: per adapter object
  780. * @id: target id
  781. * @channel: channel
  782. * Context: This function will acquire ioc->scsi_lookup_lock.
  783. *
  784. * This will search for a matching channel:id in the scsi_lookup array,
  785. * returning 1 if found.
  786. */
  787. static u8
  788. _scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
  789. int channel)
  790. {
  791. u8 found;
  792. unsigned long flags;
  793. int i;
  794. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  795. found = 0;
  796. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  797. if (ioc->scsi_lookup[i].scmd &&
  798. (ioc->scsi_lookup[i].scmd->device->id == id &&
  799. ioc->scsi_lookup[i].scmd->device->channel == channel)) {
  800. found = 1;
  801. goto out;
  802. }
  803. }
  804. out:
  805. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  806. return found;
  807. }
  808. /**
  809. * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
  810. * @ioc: per adapter object
  811. * @id: target id
  812. * @lun: lun number
  813. * @channel: channel
  814. * Context: This function will acquire ioc->scsi_lookup_lock.
  815. *
  816. * This will search for a matching channel:id:lun in the scsi_lookup array,
  817. * returning 1 if found.
  818. */
  819. static u8
  820. _scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
  821. unsigned int lun, int channel)
  822. {
  823. u8 found;
  824. unsigned long flags;
  825. int i;
  826. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  827. found = 0;
  828. for (i = 0 ; i < ioc->scsiio_depth; i++) {
  829. if (ioc->scsi_lookup[i].scmd &&
  830. (ioc->scsi_lookup[i].scmd->device->id == id &&
  831. ioc->scsi_lookup[i].scmd->device->channel == channel &&
  832. ioc->scsi_lookup[i].scmd->device->lun == lun)) {
  833. found = 1;
  834. goto out;
  835. }
  836. }
  837. out:
  838. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  839. return found;
  840. }
  841. /**
  842. * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
  843. * @ioc: per adapter object
  844. * @smid: system request message index
  845. *
  846. * Returns phys pointer to chain buffer.
  847. */
  848. static dma_addr_t
  849. _scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  850. {
  851. return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
  852. ioc->chains_needed_per_io));
  853. }
  854. /**
  855. * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
  856. * @ioc: per adapter object
  857. * @smid: system request message index
  858. *
  859. * Returns virt pointer to chain buffer.
  860. */
  861. static void *
  862. _scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  863. {
  864. return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
  865. ioc->chains_needed_per_io)));
  866. }
  867. /**
  868. * _scsih_build_scatter_gather - main sg creation routine
  869. * @ioc: per adapter object
  870. * @scmd: scsi command
  871. * @smid: system request message index
  872. * Context: none.
  873. *
  874. * The main routine that builds scatter gather table from a given
  875. * scsi request sent via the .queuecommand main handler.
  876. *
  877. * Returns 0 success, anything else error
  878. */
  879. static int
  880. _scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
  881. struct scsi_cmnd *scmd, u16 smid)
  882. {
  883. Mpi2SCSIIORequest_t *mpi_request;
  884. dma_addr_t chain_dma;
  885. struct scatterlist *sg_scmd;
  886. void *sg_local, *chain;
  887. u32 chain_offset;
  888. u32 chain_length;
  889. u32 chain_flags;
  890. int sges_left;
  891. u32 sges_in_segment;
  892. u32 sgl_flags;
  893. u32 sgl_flags_last_element;
  894. u32 sgl_flags_end_buffer;
  895. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  896. /* init scatter gather flags */
  897. sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
  898. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  899. sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
  900. sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
  901. << MPI2_SGE_FLAGS_SHIFT;
  902. sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
  903. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
  904. << MPI2_SGE_FLAGS_SHIFT;
  905. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  906. sg_scmd = scsi_sglist(scmd);
  907. sges_left = scsi_dma_map(scmd);
  908. if (sges_left < 0) {
  909. sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
  910. " failed: request for %d bytes!\n", scsi_bufflen(scmd));
  911. return -ENOMEM;
  912. }
  913. sg_local = &mpi_request->SGL;
  914. sges_in_segment = ioc->max_sges_in_main_message;
  915. if (sges_left <= sges_in_segment)
  916. goto fill_in_last_segment;
  917. mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
  918. (sges_in_segment * ioc->sge_size))/4;
  919. /* fill in main message segment when there is a chain following */
  920. while (sges_in_segment) {
  921. if (sges_in_segment == 1)
  922. ioc->base_add_sg_single(sg_local,
  923. sgl_flags_last_element | sg_dma_len(sg_scmd),
  924. sg_dma_address(sg_scmd));
  925. else
  926. ioc->base_add_sg_single(sg_local, sgl_flags |
  927. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  928. sg_scmd = sg_next(sg_scmd);
  929. sg_local += ioc->sge_size;
  930. sges_left--;
  931. sges_in_segment--;
  932. }
  933. /* initializing the chain flags and pointers */
  934. chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
  935. chain = _scsih_get_chain_buffer(ioc, smid);
  936. chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
  937. do {
  938. sges_in_segment = (sges_left <=
  939. ioc->max_sges_in_chain_message) ? sges_left :
  940. ioc->max_sges_in_chain_message;
  941. chain_offset = (sges_left == sges_in_segment) ?
  942. 0 : (sges_in_segment * ioc->sge_size)/4;
  943. chain_length = sges_in_segment * ioc->sge_size;
  944. if (chain_offset) {
  945. chain_offset = chain_offset <<
  946. MPI2_SGE_CHAIN_OFFSET_SHIFT;
  947. chain_length += ioc->sge_size;
  948. }
  949. ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
  950. chain_length, chain_dma);
  951. sg_local = chain;
  952. if (!chain_offset)
  953. goto fill_in_last_segment;
  954. /* fill in chain segments */
  955. while (sges_in_segment) {
  956. if (sges_in_segment == 1)
  957. ioc->base_add_sg_single(sg_local,
  958. sgl_flags_last_element |
  959. sg_dma_len(sg_scmd),
  960. sg_dma_address(sg_scmd));
  961. else
  962. ioc->base_add_sg_single(sg_local, sgl_flags |
  963. sg_dma_len(sg_scmd),
  964. sg_dma_address(sg_scmd));
  965. sg_scmd = sg_next(sg_scmd);
  966. sg_local += ioc->sge_size;
  967. sges_left--;
  968. sges_in_segment--;
  969. }
  970. chain_dma += ioc->request_sz;
  971. chain += ioc->request_sz;
  972. } while (1);
  973. fill_in_last_segment:
  974. /* fill the last segment */
  975. while (sges_left) {
  976. if (sges_left == 1)
  977. ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
  978. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  979. else
  980. ioc->base_add_sg_single(sg_local, sgl_flags |
  981. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  982. sg_scmd = sg_next(sg_scmd);
  983. sg_local += ioc->sge_size;
  984. sges_left--;
  985. }
  986. return 0;
  987. }
  988. /**
  989. * _scsih_change_queue_depth - setting device queue depth
  990. * @sdev: scsi device struct
  991. * @qdepth: requested queue depth
  992. * @reason: calling context
  993. *
  994. * Returns queue depth.
  995. */
  996. static int
  997. _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  998. {
  999. struct Scsi_Host *shost = sdev->host;
  1000. int max_depth;
  1001. int tag_type;
  1002. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1003. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1004. struct MPT2SAS_TARGET *sas_target_priv_data;
  1005. struct _sas_device *sas_device;
  1006. unsigned long flags;
  1007. if (reason != SCSI_QDEPTH_DEFAULT)
  1008. return -EOPNOTSUPP;
  1009. max_depth = shost->can_queue;
  1010. /* limit max device queue for SATA to 32 */
  1011. sas_device_priv_data = sdev->hostdata;
  1012. if (!sas_device_priv_data)
  1013. goto not_sata;
  1014. sas_target_priv_data = sas_device_priv_data->sas_target;
  1015. if (!sas_target_priv_data)
  1016. goto not_sata;
  1017. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
  1018. goto not_sata;
  1019. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1020. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1021. sas_device_priv_data->sas_target->sas_address);
  1022. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1023. if (sas_device && sas_device->device_info &
  1024. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1025. max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
  1026. not_sata:
  1027. if (!sdev->tagged_supported)
  1028. max_depth = 1;
  1029. if (qdepth > max_depth)
  1030. qdepth = max_depth;
  1031. tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
  1032. scsi_adjust_queue_depth(sdev, tag_type, qdepth);
  1033. if (sdev->inquiry_len > 7)
  1034. sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
  1035. "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
  1036. sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
  1037. sdev->ordered_tags, sdev->scsi_level,
  1038. (sdev->inquiry[7] & 2) >> 1);
  1039. return sdev->queue_depth;
  1040. }
  1041. /**
  1042. * _scsih_change_queue_type - changing device queue tag type
  1043. * @sdev: scsi device struct
  1044. * @tag_type: requested tag type
  1045. *
  1046. * Returns queue tag type.
  1047. */
  1048. static int
  1049. _scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
  1050. {
  1051. if (sdev->tagged_supported) {
  1052. scsi_set_tag_type(sdev, tag_type);
  1053. if (tag_type)
  1054. scsi_activate_tcq(sdev, sdev->queue_depth);
  1055. else
  1056. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1057. } else
  1058. tag_type = 0;
  1059. return tag_type;
  1060. }
  1061. /**
  1062. * _scsih_target_alloc - target add routine
  1063. * @starget: scsi target struct
  1064. *
  1065. * Returns 0 if ok. Any other return is assumed to be an error and
  1066. * the device is ignored.
  1067. */
  1068. static int
  1069. _scsih_target_alloc(struct scsi_target *starget)
  1070. {
  1071. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1072. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1073. struct MPT2SAS_TARGET *sas_target_priv_data;
  1074. struct _sas_device *sas_device;
  1075. struct _raid_device *raid_device;
  1076. unsigned long flags;
  1077. struct sas_rphy *rphy;
  1078. sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
  1079. if (!sas_target_priv_data)
  1080. return -ENOMEM;
  1081. starget->hostdata = sas_target_priv_data;
  1082. sas_target_priv_data->starget = starget;
  1083. sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
  1084. /* RAID volumes */
  1085. if (starget->channel == RAID_CHANNEL) {
  1086. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1087. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1088. starget->channel);
  1089. if (raid_device) {
  1090. sas_target_priv_data->handle = raid_device->handle;
  1091. sas_target_priv_data->sas_address = raid_device->wwid;
  1092. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
  1093. raid_device->starget = starget;
  1094. }
  1095. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1096. return 0;
  1097. }
  1098. /* sas/sata devices */
  1099. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1100. rphy = dev_to_rphy(starget->dev.parent);
  1101. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1102. rphy->identify.sas_address);
  1103. if (sas_device) {
  1104. sas_target_priv_data->handle = sas_device->handle;
  1105. sas_target_priv_data->sas_address = sas_device->sas_address;
  1106. sas_device->starget = starget;
  1107. sas_device->id = starget->id;
  1108. sas_device->channel = starget->channel;
  1109. if (sas_device->hidden_raid_component)
  1110. sas_target_priv_data->flags |=
  1111. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1112. }
  1113. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1114. return 0;
  1115. }
  1116. /**
  1117. * _scsih_target_destroy - target destroy routine
  1118. * @starget: scsi target struct
  1119. *
  1120. * Returns nothing.
  1121. */
  1122. static void
  1123. _scsih_target_destroy(struct scsi_target *starget)
  1124. {
  1125. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1126. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1127. struct MPT2SAS_TARGET *sas_target_priv_data;
  1128. struct _sas_device *sas_device;
  1129. struct _raid_device *raid_device;
  1130. unsigned long flags;
  1131. struct sas_rphy *rphy;
  1132. sas_target_priv_data = starget->hostdata;
  1133. if (!sas_target_priv_data)
  1134. return;
  1135. if (starget->channel == RAID_CHANNEL) {
  1136. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1137. raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
  1138. starget->channel);
  1139. if (raid_device) {
  1140. raid_device->starget = NULL;
  1141. raid_device->sdev = NULL;
  1142. }
  1143. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1144. goto out;
  1145. }
  1146. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1147. rphy = dev_to_rphy(starget->dev.parent);
  1148. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1149. rphy->identify.sas_address);
  1150. if (sas_device && (sas_device->starget == starget) &&
  1151. (sas_device->id == starget->id) &&
  1152. (sas_device->channel == starget->channel))
  1153. sas_device->starget = NULL;
  1154. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1155. out:
  1156. kfree(sas_target_priv_data);
  1157. starget->hostdata = NULL;
  1158. }
  1159. /**
  1160. * _scsih_slave_alloc - device add routine
  1161. * @sdev: scsi device struct
  1162. *
  1163. * Returns 0 if ok. Any other return is assumed to be an error and
  1164. * the device is ignored.
  1165. */
  1166. static int
  1167. _scsih_slave_alloc(struct scsi_device *sdev)
  1168. {
  1169. struct Scsi_Host *shost;
  1170. struct MPT2SAS_ADAPTER *ioc;
  1171. struct MPT2SAS_TARGET *sas_target_priv_data;
  1172. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1173. struct scsi_target *starget;
  1174. struct _raid_device *raid_device;
  1175. unsigned long flags;
  1176. sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
  1177. if (!sas_device_priv_data)
  1178. return -ENOMEM;
  1179. sas_device_priv_data->lun = sdev->lun;
  1180. sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
  1181. starget = scsi_target(sdev);
  1182. sas_target_priv_data = starget->hostdata;
  1183. sas_target_priv_data->num_luns++;
  1184. sas_device_priv_data->sas_target = sas_target_priv_data;
  1185. sdev->hostdata = sas_device_priv_data;
  1186. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
  1187. sdev->no_uld_attach = 1;
  1188. shost = dev_to_shost(&starget->dev);
  1189. ioc = shost_priv(shost);
  1190. if (starget->channel == RAID_CHANNEL) {
  1191. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1192. raid_device = _scsih_raid_device_find_by_id(ioc,
  1193. starget->id, starget->channel);
  1194. if (raid_device)
  1195. raid_device->sdev = sdev; /* raid is single lun */
  1196. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1197. }
  1198. return 0;
  1199. }
  1200. /**
  1201. * _scsih_slave_destroy - device destroy routine
  1202. * @sdev: scsi device struct
  1203. *
  1204. * Returns nothing.
  1205. */
  1206. static void
  1207. _scsih_slave_destroy(struct scsi_device *sdev)
  1208. {
  1209. struct MPT2SAS_TARGET *sas_target_priv_data;
  1210. struct scsi_target *starget;
  1211. if (!sdev->hostdata)
  1212. return;
  1213. starget = scsi_target(sdev);
  1214. sas_target_priv_data = starget->hostdata;
  1215. sas_target_priv_data->num_luns--;
  1216. kfree(sdev->hostdata);
  1217. sdev->hostdata = NULL;
  1218. }
  1219. /**
  1220. * _scsih_display_sata_capabilities - sata capabilities
  1221. * @ioc: per adapter object
  1222. * @sas_device: the sas_device object
  1223. * @sdev: scsi device struct
  1224. */
  1225. static void
  1226. _scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1227. struct _sas_device *sas_device, struct scsi_device *sdev)
  1228. {
  1229. Mpi2ConfigReply_t mpi_reply;
  1230. Mpi2SasDevicePage0_t sas_device_pg0;
  1231. u32 ioc_status;
  1232. u16 flags;
  1233. u32 device_info;
  1234. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  1235. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
  1236. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1237. ioc->name, __FILE__, __LINE__, __func__);
  1238. return;
  1239. }
  1240. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1241. MPI2_IOCSTATUS_MASK;
  1242. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1243. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1244. ioc->name, __FILE__, __LINE__, __func__);
  1245. return;
  1246. }
  1247. flags = le16_to_cpu(sas_device_pg0.Flags);
  1248. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  1249. sdev_printk(KERN_INFO, sdev,
  1250. "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
  1251. "sw_preserve(%s)\n",
  1252. (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
  1253. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
  1254. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
  1255. "n",
  1256. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
  1257. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
  1258. (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
  1259. }
  1260. /**
  1261. * _scsih_is_raid - return boolean indicating device is raid volume
  1262. * @dev the device struct object
  1263. */
  1264. static int
  1265. _scsih_is_raid(struct device *dev)
  1266. {
  1267. struct scsi_device *sdev = to_scsi_device(dev);
  1268. return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
  1269. }
  1270. /**
  1271. * _scsih_get_resync - get raid volume resync percent complete
  1272. * @dev the device struct object
  1273. */
  1274. static void
  1275. _scsih_get_resync(struct device *dev)
  1276. {
  1277. struct scsi_device *sdev = to_scsi_device(dev);
  1278. struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1279. static struct _raid_device *raid_device;
  1280. unsigned long flags;
  1281. Mpi2RaidVolPage0_t vol_pg0;
  1282. Mpi2ConfigReply_t mpi_reply;
  1283. u32 volume_status_flags;
  1284. u8 percent_complete = 0;
  1285. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1286. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1287. sdev->channel);
  1288. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1289. if (!raid_device)
  1290. goto out;
  1291. if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1292. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
  1293. sizeof(Mpi2RaidVolPage0_t))) {
  1294. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1295. ioc->name, __FILE__, __LINE__, __func__);
  1296. goto out;
  1297. }
  1298. volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1299. if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
  1300. percent_complete = raid_device->percent_complete;
  1301. out:
  1302. raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
  1303. }
  1304. /**
  1305. * _scsih_get_state - get raid volume level
  1306. * @dev the device struct object
  1307. */
  1308. static void
  1309. _scsih_get_state(struct device *dev)
  1310. {
  1311. struct scsi_device *sdev = to_scsi_device(dev);
  1312. struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
  1313. static struct _raid_device *raid_device;
  1314. unsigned long flags;
  1315. Mpi2RaidVolPage0_t vol_pg0;
  1316. Mpi2ConfigReply_t mpi_reply;
  1317. u32 volstate;
  1318. enum raid_state state = RAID_STATE_UNKNOWN;
  1319. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1320. raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
  1321. sdev->channel);
  1322. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1323. if (!raid_device)
  1324. goto out;
  1325. if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
  1326. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
  1327. sizeof(Mpi2RaidVolPage0_t))) {
  1328. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1329. ioc->name, __FILE__, __LINE__, __func__);
  1330. goto out;
  1331. }
  1332. volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
  1333. if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
  1334. state = RAID_STATE_RESYNCING;
  1335. goto out;
  1336. }
  1337. switch (vol_pg0.VolumeState) {
  1338. case MPI2_RAID_VOL_STATE_OPTIMAL:
  1339. case MPI2_RAID_VOL_STATE_ONLINE:
  1340. state = RAID_STATE_ACTIVE;
  1341. break;
  1342. case MPI2_RAID_VOL_STATE_DEGRADED:
  1343. state = RAID_STATE_DEGRADED;
  1344. break;
  1345. case MPI2_RAID_VOL_STATE_FAILED:
  1346. case MPI2_RAID_VOL_STATE_MISSING:
  1347. state = RAID_STATE_OFFLINE;
  1348. break;
  1349. }
  1350. out:
  1351. raid_set_state(mpt2sas_raid_template, dev, state);
  1352. }
  1353. /**
  1354. * _scsih_set_level - set raid level
  1355. * @sdev: scsi device struct
  1356. * @raid_device: raid_device object
  1357. */
  1358. static void
  1359. _scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
  1360. {
  1361. enum raid_level level = RAID_LEVEL_UNKNOWN;
  1362. switch (raid_device->volume_type) {
  1363. case MPI2_RAID_VOL_TYPE_RAID0:
  1364. level = RAID_LEVEL_0;
  1365. break;
  1366. case MPI2_RAID_VOL_TYPE_RAID10:
  1367. level = RAID_LEVEL_10;
  1368. break;
  1369. case MPI2_RAID_VOL_TYPE_RAID1E:
  1370. level = RAID_LEVEL_1E;
  1371. break;
  1372. case MPI2_RAID_VOL_TYPE_RAID1:
  1373. level = RAID_LEVEL_1;
  1374. break;
  1375. }
  1376. raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
  1377. }
  1378. /**
  1379. * _scsih_get_volume_capabilities - volume capabilities
  1380. * @ioc: per adapter object
  1381. * @sas_device: the raid_device object
  1382. */
  1383. static void
  1384. _scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
  1385. struct _raid_device *raid_device)
  1386. {
  1387. Mpi2RaidVolPage0_t *vol_pg0;
  1388. Mpi2RaidPhysDiskPage0_t pd_pg0;
  1389. Mpi2SasDevicePage0_t sas_device_pg0;
  1390. Mpi2ConfigReply_t mpi_reply;
  1391. u16 sz;
  1392. u8 num_pds;
  1393. if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
  1394. &num_pds)) || !num_pds) {
  1395. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1396. ioc->name, __FILE__, __LINE__, __func__);
  1397. return;
  1398. }
  1399. raid_device->num_pds = num_pds;
  1400. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  1401. sizeof(Mpi2RaidVol0PhysDisk_t));
  1402. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  1403. if (!vol_pg0) {
  1404. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1405. ioc->name, __FILE__, __LINE__, __func__);
  1406. return;
  1407. }
  1408. if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  1409. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  1410. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1411. ioc->name, __FILE__, __LINE__, __func__);
  1412. kfree(vol_pg0);
  1413. return;
  1414. }
  1415. raid_device->volume_type = vol_pg0->VolumeType;
  1416. /* figure out what the underlying devices are by
  1417. * obtaining the device_info bits for the 1st device
  1418. */
  1419. if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  1420. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  1421. vol_pg0->PhysDisk[0].PhysDiskNum))) {
  1422. if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  1423. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  1424. le16_to_cpu(pd_pg0.DevHandle)))) {
  1425. raid_device->device_info =
  1426. le32_to_cpu(sas_device_pg0.DeviceInfo);
  1427. }
  1428. }
  1429. kfree(vol_pg0);
  1430. }
  1431. /**
  1432. * _scsih_enable_tlr - setting TLR flags
  1433. * @ioc: per adapter object
  1434. * @sdev: scsi device struct
  1435. *
  1436. * Enabling Transaction Layer Retries for tape devices when
  1437. * vpd page 0x90 is present
  1438. *
  1439. */
  1440. static void
  1441. _scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
  1442. {
  1443. /* only for TAPE */
  1444. if (sdev->type != TYPE_TAPE)
  1445. return;
  1446. if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
  1447. return;
  1448. sas_enable_tlr(sdev);
  1449. sdev_printk(KERN_INFO, sdev, "TLR %s\n",
  1450. sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
  1451. return;
  1452. }
  1453. /**
  1454. * _scsih_slave_configure - device configure routine.
  1455. * @sdev: scsi device struct
  1456. *
  1457. * Returns 0 if ok. Any other return is assumed to be an error and
  1458. * the device is ignored.
  1459. */
  1460. static int
  1461. _scsih_slave_configure(struct scsi_device *sdev)
  1462. {
  1463. struct Scsi_Host *shost = sdev->host;
  1464. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1465. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1466. struct MPT2SAS_TARGET *sas_target_priv_data;
  1467. struct _sas_device *sas_device;
  1468. struct _raid_device *raid_device;
  1469. unsigned long flags;
  1470. int qdepth;
  1471. u8 ssp_target = 0;
  1472. char *ds = "";
  1473. char *r_level = "";
  1474. qdepth = 1;
  1475. sas_device_priv_data = sdev->hostdata;
  1476. sas_device_priv_data->configured_lun = 1;
  1477. sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
  1478. sas_target_priv_data = sas_device_priv_data->sas_target;
  1479. /* raid volume handling */
  1480. if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
  1481. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  1482. raid_device = _scsih_raid_device_find_by_handle(ioc,
  1483. sas_target_priv_data->handle);
  1484. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  1485. if (!raid_device) {
  1486. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1487. ioc->name, __FILE__, __LINE__, __func__);
  1488. return 0;
  1489. }
  1490. _scsih_get_volume_capabilities(ioc, raid_device);
  1491. /* RAID Queue Depth Support
  1492. * IS volume = underlying qdepth of drive type, either
  1493. * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
  1494. * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
  1495. */
  1496. if (raid_device->device_info &
  1497. MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1498. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1499. ds = "SSP";
  1500. } else {
  1501. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1502. if (raid_device->device_info &
  1503. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1504. ds = "SATA";
  1505. else
  1506. ds = "STP";
  1507. }
  1508. switch (raid_device->volume_type) {
  1509. case MPI2_RAID_VOL_TYPE_RAID0:
  1510. r_level = "RAID0";
  1511. break;
  1512. case MPI2_RAID_VOL_TYPE_RAID1E:
  1513. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1514. if (ioc->manu_pg10.OEMIdentifier &&
  1515. (ioc->manu_pg10.GenericFlags0 &
  1516. MFG10_GF0_R10_DISPLAY) &&
  1517. !(raid_device->num_pds % 2))
  1518. r_level = "RAID10";
  1519. else
  1520. r_level = "RAID1E";
  1521. break;
  1522. case MPI2_RAID_VOL_TYPE_RAID1:
  1523. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1524. r_level = "RAID1";
  1525. break;
  1526. case MPI2_RAID_VOL_TYPE_RAID10:
  1527. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1528. r_level = "RAID10";
  1529. break;
  1530. case MPI2_RAID_VOL_TYPE_UNKNOWN:
  1531. default:
  1532. qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
  1533. r_level = "RAIDX";
  1534. break;
  1535. }
  1536. sdev_printk(KERN_INFO, sdev, "%s: "
  1537. "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
  1538. r_level, raid_device->handle,
  1539. (unsigned long long)raid_device->wwid,
  1540. raid_device->num_pds, ds);
  1541. _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
  1542. /* raid transport support */
  1543. _scsih_set_level(sdev, raid_device);
  1544. return 0;
  1545. }
  1546. /* non-raid handling */
  1547. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1548. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1549. sas_device_priv_data->sas_target->sas_address);
  1550. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1551. if (sas_device) {
  1552. if (sas_target_priv_data->flags &
  1553. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1554. mpt2sas_config_get_volume_handle(ioc,
  1555. sas_device->handle, &sas_device->volume_handle);
  1556. mpt2sas_config_get_volume_wwid(ioc,
  1557. sas_device->volume_handle,
  1558. &sas_device->volume_wwid);
  1559. }
  1560. if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
  1561. qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
  1562. ssp_target = 1;
  1563. ds = "SSP";
  1564. } else {
  1565. qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
  1566. if (sas_device->device_info &
  1567. MPI2_SAS_DEVICE_INFO_STP_TARGET)
  1568. ds = "STP";
  1569. else if (sas_device->device_info &
  1570. MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  1571. ds = "SATA";
  1572. }
  1573. sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
  1574. "sas_addr(0x%016llx), device_name(0x%016llx)\n",
  1575. ds, sas_device->handle,
  1576. (unsigned long long)sas_device->sas_address,
  1577. (unsigned long long)sas_device->device_name);
  1578. sdev_printk(KERN_INFO, sdev, "%s: "
  1579. "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
  1580. (unsigned long long) sas_device->enclosure_logical_id,
  1581. sas_device->slot);
  1582. if (!ssp_target)
  1583. _scsih_display_sata_capabilities(ioc, sas_device, sdev);
  1584. }
  1585. _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
  1586. if (ssp_target) {
  1587. sas_read_port_mode_page(sdev);
  1588. _scsih_enable_tlr(ioc, sdev);
  1589. }
  1590. return 0;
  1591. }
  1592. /**
  1593. * _scsih_bios_param - fetch head, sector, cylinder info for a disk
  1594. * @sdev: scsi device struct
  1595. * @bdev: pointer to block device context
  1596. * @capacity: device size (in 512 byte sectors)
  1597. * @params: three element array to place output:
  1598. * params[0] number of heads (max 255)
  1599. * params[1] number of sectors (max 63)
  1600. * params[2] number of cylinders
  1601. *
  1602. * Return nothing.
  1603. */
  1604. static int
  1605. _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1606. sector_t capacity, int params[])
  1607. {
  1608. int heads;
  1609. int sectors;
  1610. sector_t cylinders;
  1611. ulong dummy;
  1612. heads = 64;
  1613. sectors = 32;
  1614. dummy = heads * sectors;
  1615. cylinders = capacity;
  1616. sector_div(cylinders, dummy);
  1617. /*
  1618. * Handle extended translation size for logical drives
  1619. * > 1Gb
  1620. */
  1621. if ((ulong)capacity >= 0x200000) {
  1622. heads = 255;
  1623. sectors = 63;
  1624. dummy = heads * sectors;
  1625. cylinders = capacity;
  1626. sector_div(cylinders, dummy);
  1627. }
  1628. /* return result */
  1629. params[0] = heads;
  1630. params[1] = sectors;
  1631. params[2] = cylinders;
  1632. return 0;
  1633. }
  1634. /**
  1635. * _scsih_response_code - translation of device response code
  1636. * @ioc: per adapter object
  1637. * @response_code: response code returned by the device
  1638. *
  1639. * Return nothing.
  1640. */
  1641. static void
  1642. _scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
  1643. {
  1644. char *desc;
  1645. switch (response_code) {
  1646. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  1647. desc = "task management request completed";
  1648. break;
  1649. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  1650. desc = "invalid frame";
  1651. break;
  1652. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  1653. desc = "task management request not supported";
  1654. break;
  1655. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  1656. desc = "task management request failed";
  1657. break;
  1658. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  1659. desc = "task management request succeeded";
  1660. break;
  1661. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  1662. desc = "invalid lun";
  1663. break;
  1664. case 0xA:
  1665. desc = "overlapped tag attempted";
  1666. break;
  1667. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  1668. desc = "task queued, however not sent to target";
  1669. break;
  1670. default:
  1671. desc = "unknown";
  1672. break;
  1673. }
  1674. printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
  1675. ioc->name, response_code, desc);
  1676. }
  1677. /**
  1678. * _scsih_tm_done - tm completion routine
  1679. * @ioc: per adapter object
  1680. * @smid: system request message index
  1681. * @msix_index: MSIX table index supplied by the OS
  1682. * @reply: reply message frame(lower 32bit addr)
  1683. * Context: none.
  1684. *
  1685. * The callback handler when using scsih_issue_tm.
  1686. *
  1687. * Return 1 meaning mf should be freed from _base_interrupt
  1688. * 0 means the mf is freed from this function.
  1689. */
  1690. static u8
  1691. _scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  1692. {
  1693. MPI2DefaultReply_t *mpi_reply;
  1694. if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
  1695. return 1;
  1696. if (ioc->tm_cmds.smid != smid)
  1697. return 1;
  1698. ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
  1699. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  1700. if (mpi_reply) {
  1701. memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  1702. ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
  1703. }
  1704. ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
  1705. complete(&ioc->tm_cmds.done);
  1706. return 1;
  1707. }
  1708. /**
  1709. * mpt2sas_scsih_set_tm_flag - set per target tm_busy
  1710. * @ioc: per adapter object
  1711. * @handle: device handle
  1712. *
  1713. * During taskmangement request, we need to freeze the device queue.
  1714. */
  1715. void
  1716. mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1717. {
  1718. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1719. struct scsi_device *sdev;
  1720. u8 skip = 0;
  1721. shost_for_each_device(sdev, ioc->shost) {
  1722. if (skip)
  1723. continue;
  1724. sas_device_priv_data = sdev->hostdata;
  1725. if (!sas_device_priv_data)
  1726. continue;
  1727. if (sas_device_priv_data->sas_target->handle == handle) {
  1728. sas_device_priv_data->sas_target->tm_busy = 1;
  1729. skip = 1;
  1730. ioc->ignore_loginfos = 1;
  1731. }
  1732. }
  1733. }
  1734. /**
  1735. * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
  1736. * @ioc: per adapter object
  1737. * @handle: device handle
  1738. *
  1739. * During taskmangement request, we need to freeze the device queue.
  1740. */
  1741. void
  1742. mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  1743. {
  1744. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1745. struct scsi_device *sdev;
  1746. u8 skip = 0;
  1747. shost_for_each_device(sdev, ioc->shost) {
  1748. if (skip)
  1749. continue;
  1750. sas_device_priv_data = sdev->hostdata;
  1751. if (!sas_device_priv_data)
  1752. continue;
  1753. if (sas_device_priv_data->sas_target->handle == handle) {
  1754. sas_device_priv_data->sas_target->tm_busy = 0;
  1755. skip = 1;
  1756. ioc->ignore_loginfos = 0;
  1757. }
  1758. }
  1759. }
  1760. /**
  1761. * mpt2sas_scsih_issue_tm - main routine for sending tm requests
  1762. * @ioc: per adapter struct
  1763. * @device_handle: device handle
  1764. * @channel: the channel assigned by the OS
  1765. * @id: the id assigned by the OS
  1766. * @lun: lun number
  1767. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
  1768. * @smid_task: smid assigned to the task
  1769. * @timeout: timeout in seconds
  1770. * Context: user
  1771. *
  1772. * A generic API for sending task management requests to firmware.
  1773. *
  1774. * The callback index is set inside `ioc->tm_cb_idx`.
  1775. *
  1776. * Return SUCCESS or FAILED.
  1777. */
  1778. int
  1779. mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
  1780. uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
  1781. struct scsi_cmnd *scmd)
  1782. {
  1783. Mpi2SCSITaskManagementRequest_t *mpi_request;
  1784. Mpi2SCSITaskManagementReply_t *mpi_reply;
  1785. u16 smid = 0;
  1786. u32 ioc_state;
  1787. unsigned long timeleft;
  1788. struct scsi_cmnd *scmd_lookup;
  1789. int rc;
  1790. mutex_lock(&ioc->tm_cmds.mutex);
  1791. if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
  1792. printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
  1793. __func__, ioc->name);
  1794. rc = FAILED;
  1795. goto err_out;
  1796. }
  1797. if (ioc->shost_recovery || ioc->remove_host) {
  1798. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1799. __func__, ioc->name);
  1800. rc = FAILED;
  1801. goto err_out;
  1802. }
  1803. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  1804. if (ioc_state & MPI2_DOORBELL_USED) {
  1805. dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
  1806. "active!\n", ioc->name));
  1807. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1808. FORCE_BIG_HAMMER);
  1809. rc = SUCCESS;
  1810. goto err_out;
  1811. }
  1812. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  1813. mpt2sas_base_fault_info(ioc, ioc_state &
  1814. MPI2_DOORBELL_DATA_MASK);
  1815. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1816. FORCE_BIG_HAMMER);
  1817. rc = SUCCESS;
  1818. goto err_out;
  1819. }
  1820. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
  1821. if (!smid) {
  1822. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1823. ioc->name, __func__);
  1824. rc = FAILED;
  1825. goto err_out;
  1826. }
  1827. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
  1828. " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
  1829. smid_task));
  1830. ioc->tm_cmds.status = MPT2_CMD_PENDING;
  1831. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1832. ioc->tm_cmds.smid = smid;
  1833. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  1834. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  1835. mpi_request->DevHandle = cpu_to_le16(handle);
  1836. mpi_request->TaskType = type;
  1837. mpi_request->TaskMID = cpu_to_le16(smid_task);
  1838. int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
  1839. mpt2sas_scsih_set_tm_flag(ioc, handle);
  1840. init_completion(&ioc->tm_cmds.done);
  1841. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  1842. timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
  1843. if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
  1844. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  1845. ioc->name, __func__);
  1846. _debug_dump_mf(mpi_request,
  1847. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  1848. if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
  1849. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1850. FORCE_BIG_HAMMER);
  1851. rc = SUCCESS;
  1852. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1853. mpt2sas_scsih_clear_tm_flag(ioc, handle);
  1854. goto err_out;
  1855. }
  1856. }
  1857. if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
  1858. mpi_reply = ioc->tm_cmds.reply;
  1859. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
  1860. "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
  1861. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  1862. le32_to_cpu(mpi_reply->IOCLogInfo),
  1863. le32_to_cpu(mpi_reply->TerminationCount)));
  1864. if (ioc->logging_level & MPT_DEBUG_TM) {
  1865. _scsih_response_code(ioc, mpi_reply->ResponseCode);
  1866. if (mpi_reply->IOCStatus)
  1867. _debug_dump_mf(mpi_request,
  1868. sizeof(Mpi2SCSITaskManagementRequest_t)/4);
  1869. }
  1870. }
  1871. /* sanity check:
  1872. * Check to see the commands were terminated.
  1873. * This is only needed for eh callbacks, hence the scmd check.
  1874. */
  1875. rc = FAILED;
  1876. if (scmd == NULL)
  1877. goto bypass_sanity_checks;
  1878. switch (type) {
  1879. case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  1880. scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
  1881. if (scmd_lookup && (scmd_lookup->serial_number ==
  1882. scmd->serial_number))
  1883. rc = FAILED;
  1884. else
  1885. rc = SUCCESS;
  1886. break;
  1887. case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  1888. if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
  1889. rc = FAILED;
  1890. else
  1891. rc = SUCCESS;
  1892. break;
  1893. case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
  1894. if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
  1895. rc = FAILED;
  1896. else
  1897. rc = SUCCESS;
  1898. break;
  1899. }
  1900. bypass_sanity_checks:
  1901. mpt2sas_scsih_clear_tm_flag(ioc, handle);
  1902. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  1903. mutex_unlock(&ioc->tm_cmds.mutex);
  1904. return rc;
  1905. err_out:
  1906. mutex_unlock(&ioc->tm_cmds.mutex);
  1907. return rc;
  1908. }
  1909. /**
  1910. * _scsih_abort - eh threads main abort routine
  1911. * @sdev: scsi device struct
  1912. *
  1913. * Returns SUCCESS if command aborted else FAILED
  1914. */
  1915. static int
  1916. _scsih_abort(struct scsi_cmnd *scmd)
  1917. {
  1918. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1919. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1920. u16 smid;
  1921. u16 handle;
  1922. int r;
  1923. printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
  1924. ioc->name, scmd);
  1925. scsi_print_command(scmd);
  1926. sas_device_priv_data = scmd->device->hostdata;
  1927. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1928. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1929. ioc->name, scmd);
  1930. scmd->result = DID_NO_CONNECT << 16;
  1931. scmd->scsi_done(scmd);
  1932. r = SUCCESS;
  1933. goto out;
  1934. }
  1935. /* search for the command */
  1936. smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
  1937. if (!smid) {
  1938. scmd->result = DID_RESET << 16;
  1939. r = SUCCESS;
  1940. goto out;
  1941. }
  1942. /* for hidden raid components and volumes this is not supported */
  1943. if (sas_device_priv_data->sas_target->flags &
  1944. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  1945. sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
  1946. scmd->result = DID_RESET << 16;
  1947. r = FAILED;
  1948. goto out;
  1949. }
  1950. mpt2sas_halt_firmware(ioc);
  1951. handle = sas_device_priv_data->sas_target->handle;
  1952. r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
  1953. scmd->device->id, scmd->device->lun,
  1954. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
  1955. out:
  1956. printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
  1957. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  1958. return r;
  1959. }
  1960. /**
  1961. * _scsih_dev_reset - eh threads main device reset routine
  1962. * @sdev: scsi device struct
  1963. *
  1964. * Returns SUCCESS if command aborted else FAILED
  1965. */
  1966. static int
  1967. _scsih_dev_reset(struct scsi_cmnd *scmd)
  1968. {
  1969. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  1970. struct MPT2SAS_DEVICE *sas_device_priv_data;
  1971. struct _sas_device *sas_device;
  1972. unsigned long flags;
  1973. u16 handle;
  1974. int r;
  1975. printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
  1976. ioc->name, scmd);
  1977. scsi_print_command(scmd);
  1978. sas_device_priv_data = scmd->device->hostdata;
  1979. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  1980. printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
  1981. ioc->name, scmd);
  1982. scmd->result = DID_NO_CONNECT << 16;
  1983. scmd->scsi_done(scmd);
  1984. r = SUCCESS;
  1985. goto out;
  1986. }
  1987. /* for hidden raid components obtain the volume_handle */
  1988. handle = 0;
  1989. if (sas_device_priv_data->sas_target->flags &
  1990. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  1991. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1992. sas_device = _scsih_sas_device_find_by_handle(ioc,
  1993. sas_device_priv_data->sas_target->handle);
  1994. if (sas_device)
  1995. handle = sas_device->volume_handle;
  1996. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1997. } else
  1998. handle = sas_device_priv_data->sas_target->handle;
  1999. if (!handle) {
  2000. scmd->result = DID_RESET << 16;
  2001. r = FAILED;
  2002. goto out;
  2003. }
  2004. r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
  2005. scmd->device->id, scmd->device->lun,
  2006. MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
  2007. out:
  2008. printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
  2009. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2010. return r;
  2011. }
  2012. /**
  2013. * _scsih_target_reset - eh threads main target reset routine
  2014. * @sdev: scsi device struct
  2015. *
  2016. * Returns SUCCESS if command aborted else FAILED
  2017. */
  2018. static int
  2019. _scsih_target_reset(struct scsi_cmnd *scmd)
  2020. {
  2021. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2022. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2023. struct _sas_device *sas_device;
  2024. unsigned long flags;
  2025. u16 handle;
  2026. int r;
  2027. printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
  2028. ioc->name, scmd);
  2029. scsi_print_command(scmd);
  2030. sas_device_priv_data = scmd->device->hostdata;
  2031. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2032. printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
  2033. ioc->name, scmd);
  2034. scmd->result = DID_NO_CONNECT << 16;
  2035. scmd->scsi_done(scmd);
  2036. r = SUCCESS;
  2037. goto out;
  2038. }
  2039. /* for hidden raid components obtain the volume_handle */
  2040. handle = 0;
  2041. if (sas_device_priv_data->sas_target->flags &
  2042. MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2043. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2044. sas_device = _scsih_sas_device_find_by_handle(ioc,
  2045. sas_device_priv_data->sas_target->handle);
  2046. if (sas_device)
  2047. handle = sas_device->volume_handle;
  2048. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2049. } else
  2050. handle = sas_device_priv_data->sas_target->handle;
  2051. if (!handle) {
  2052. scmd->result = DID_RESET << 16;
  2053. r = FAILED;
  2054. goto out;
  2055. }
  2056. r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
  2057. scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
  2058. 30, scmd);
  2059. out:
  2060. printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
  2061. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2062. return r;
  2063. }
  2064. /**
  2065. * _scsih_host_reset - eh threads main host reset routine
  2066. * @sdev: scsi device struct
  2067. *
  2068. * Returns SUCCESS if command aborted else FAILED
  2069. */
  2070. static int
  2071. _scsih_host_reset(struct scsi_cmnd *scmd)
  2072. {
  2073. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2074. int r, retval;
  2075. printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
  2076. ioc->name, scmd);
  2077. scsi_print_command(scmd);
  2078. retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2079. FORCE_BIG_HAMMER);
  2080. r = (retval < 0) ? FAILED : SUCCESS;
  2081. printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
  2082. ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2083. return r;
  2084. }
  2085. /**
  2086. * _scsih_fw_event_add - insert and queue up fw_event
  2087. * @ioc: per adapter object
  2088. * @fw_event: object describing the event
  2089. * Context: This function will acquire ioc->fw_event_lock.
  2090. *
  2091. * This adds the firmware event object into link list, then queues it up to
  2092. * be processed from user context.
  2093. *
  2094. * Return nothing.
  2095. */
  2096. static void
  2097. _scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
  2098. {
  2099. unsigned long flags;
  2100. if (ioc->firmware_event_thread == NULL)
  2101. return;
  2102. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2103. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  2104. INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
  2105. queue_delayed_work(ioc->firmware_event_thread,
  2106. &fw_event->delayed_work, 0);
  2107. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2108. }
  2109. /**
  2110. * _scsih_fw_event_free - delete fw_event
  2111. * @ioc: per adapter object
  2112. * @fw_event: object describing the event
  2113. * Context: This function will acquire ioc->fw_event_lock.
  2114. *
  2115. * This removes firmware event object from link list, frees associated memory.
  2116. *
  2117. * Return nothing.
  2118. */
  2119. static void
  2120. _scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  2121. *fw_event)
  2122. {
  2123. unsigned long flags;
  2124. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2125. list_del(&fw_event->list);
  2126. kfree(fw_event->event_data);
  2127. kfree(fw_event);
  2128. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2129. }
  2130. /**
  2131. * _scsih_queue_rescan - queue a topology rescan from user context
  2132. * @ioc: per adapter object
  2133. *
  2134. * Return nothing.
  2135. */
  2136. static void
  2137. _scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
  2138. {
  2139. struct fw_event_work *fw_event;
  2140. if (ioc->wait_for_port_enable_to_complete)
  2141. return;
  2142. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  2143. if (!fw_event)
  2144. return;
  2145. fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
  2146. fw_event->ioc = ioc;
  2147. _scsih_fw_event_add(ioc, fw_event);
  2148. }
  2149. /**
  2150. * _scsih_fw_event_cleanup_queue - cleanup event queue
  2151. * @ioc: per adapter object
  2152. *
  2153. * Walk the firmware event queue, either killing timers, or waiting
  2154. * for outstanding events to complete
  2155. *
  2156. * Return nothing.
  2157. */
  2158. static void
  2159. _scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
  2160. {
  2161. struct fw_event_work *fw_event, *next;
  2162. if (list_empty(&ioc->fw_event_list) ||
  2163. !ioc->firmware_event_thread || in_interrupt())
  2164. return;
  2165. list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
  2166. if (cancel_delayed_work(&fw_event->delayed_work)) {
  2167. _scsih_fw_event_free(ioc, fw_event);
  2168. continue;
  2169. }
  2170. fw_event->cancel_pending_work = 1;
  2171. }
  2172. }
  2173. /**
  2174. * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
  2175. * @ioc: per adapter object
  2176. * @handle: device handle
  2177. *
  2178. * During device pull we need to appropiately set the sdev state.
  2179. */
  2180. static void
  2181. _scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2182. {
  2183. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2184. struct scsi_device *sdev;
  2185. shost_for_each_device(sdev, ioc->shost) {
  2186. sas_device_priv_data = sdev->hostdata;
  2187. if (!sas_device_priv_data)
  2188. continue;
  2189. if (!sas_device_priv_data->block)
  2190. continue;
  2191. if (sas_device_priv_data->sas_target->handle == handle) {
  2192. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2193. MPT2SAS_INFO_FMT "SDEV_RUNNING: "
  2194. "handle(0x%04x)\n", ioc->name, handle));
  2195. sas_device_priv_data->block = 0;
  2196. scsi_internal_device_unblock(sdev);
  2197. }
  2198. }
  2199. }
  2200. /**
  2201. * _scsih_block_io_device - set the device state to SDEV_BLOCK
  2202. * @ioc: per adapter object
  2203. * @handle: device handle
  2204. *
  2205. * During device pull we need to appropiately set the sdev state.
  2206. */
  2207. static void
  2208. _scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2209. {
  2210. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2211. struct scsi_device *sdev;
  2212. shost_for_each_device(sdev, ioc->shost) {
  2213. sas_device_priv_data = sdev->hostdata;
  2214. if (!sas_device_priv_data)
  2215. continue;
  2216. if (sas_device_priv_data->block)
  2217. continue;
  2218. if (sas_device_priv_data->sas_target->handle == handle) {
  2219. dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
  2220. MPT2SAS_INFO_FMT "SDEV_BLOCK: "
  2221. "handle(0x%04x)\n", ioc->name, handle));
  2222. sas_device_priv_data->block = 1;
  2223. scsi_internal_device_block(sdev);
  2224. }
  2225. }
  2226. }
  2227. /**
  2228. * _scsih_block_io_to_children_attached_to_ex
  2229. * @ioc: per adapter object
  2230. * @sas_expander: the sas_device object
  2231. *
  2232. * This routine set sdev state to SDEV_BLOCK for all devices
  2233. * attached to this expander. This function called when expander is
  2234. * pulled.
  2235. */
  2236. static void
  2237. _scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
  2238. struct _sas_node *sas_expander)
  2239. {
  2240. struct _sas_port *mpt2sas_port;
  2241. struct _sas_device *sas_device;
  2242. struct _sas_node *expander_sibling;
  2243. unsigned long flags;
  2244. if (!sas_expander)
  2245. return;
  2246. list_for_each_entry(mpt2sas_port,
  2247. &sas_expander->sas_port_list, port_list) {
  2248. if (mpt2sas_port->remote_identify.device_type ==
  2249. SAS_END_DEVICE) {
  2250. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2251. sas_device =
  2252. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  2253. mpt2sas_port->remote_identify.sas_address);
  2254. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2255. if (!sas_device)
  2256. continue;
  2257. _scsih_block_io_device(ioc, sas_device->handle);
  2258. }
  2259. }
  2260. list_for_each_entry(mpt2sas_port,
  2261. &sas_expander->sas_port_list, port_list) {
  2262. if (mpt2sas_port->remote_identify.device_type ==
  2263. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  2264. mpt2sas_port->remote_identify.device_type ==
  2265. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  2266. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2267. expander_sibling =
  2268. mpt2sas_scsih_expander_find_by_sas_address(
  2269. ioc, mpt2sas_port->remote_identify.sas_address);
  2270. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2271. _scsih_block_io_to_children_attached_to_ex(ioc,
  2272. expander_sibling);
  2273. }
  2274. }
  2275. }
  2276. /**
  2277. * _scsih_block_io_to_children_attached_directly
  2278. * @ioc: per adapter object
  2279. * @event_data: topology change event data
  2280. *
  2281. * This routine set sdev state to SDEV_BLOCK for all devices
  2282. * direct attached during device pull.
  2283. */
  2284. static void
  2285. _scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
  2286. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2287. {
  2288. int i;
  2289. u16 handle;
  2290. u16 reason_code;
  2291. u8 phy_number;
  2292. for (i = 0; i < event_data->NumEntries; i++) {
  2293. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2294. if (!handle)
  2295. continue;
  2296. phy_number = event_data->StartPhyNum + i;
  2297. reason_code = event_data->PHY[i].PhyStatus &
  2298. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2299. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
  2300. _scsih_block_io_device(ioc, handle);
  2301. }
  2302. }
  2303. /**
  2304. * _scsih_tm_tr_send - send task management request
  2305. * @ioc: per adapter object
  2306. * @handle: device handle
  2307. * Context: interrupt time.
  2308. *
  2309. * This code is to initiate the device removal handshake protocal
  2310. * with controller firmware. This function will issue target reset
  2311. * using high priority request queue. It will send a sas iounit
  2312. * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
  2313. *
  2314. * This is designed to send muliple task management request at the same
  2315. * time to the fifo. If the fifo is full, we will append the request,
  2316. * and process it in a future completion.
  2317. */
  2318. static void
  2319. _scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2320. {
  2321. Mpi2SCSITaskManagementRequest_t *mpi_request;
  2322. u16 smid;
  2323. struct _sas_device *sas_device;
  2324. unsigned long flags;
  2325. struct _tr_list *delayed_tr;
  2326. if (ioc->shost_recovery || ioc->remove_host) {
  2327. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
  2328. "progress!\n", __func__, ioc->name));
  2329. return;
  2330. }
  2331. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2332. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  2333. if (sas_device && sas_device->hidden_raid_component) {
  2334. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2335. return;
  2336. }
  2337. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2338. smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
  2339. if (!smid) {
  2340. delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
  2341. if (!delayed_tr)
  2342. return;
  2343. INIT_LIST_HEAD(&delayed_tr->list);
  2344. delayed_tr->handle = handle;
  2345. list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
  2346. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2347. "DELAYED:tr:handle(0x%04x), (open)\n",
  2348. ioc->name, handle));
  2349. return;
  2350. }
  2351. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
  2352. "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
  2353. ioc->tm_tr_cb_idx));
  2354. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2355. memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
  2356. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2357. mpi_request->DevHandle = cpu_to_le16(handle);
  2358. mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  2359. mpt2sas_base_put_smid_hi_priority(ioc, smid);
  2360. }
  2361. /**
  2362. * _scsih_sas_control_complete - completion routine
  2363. * @ioc: per adapter object
  2364. * @smid: system request message index
  2365. * @msix_index: MSIX table index supplied by the OS
  2366. * @reply: reply message frame(lower 32bit addr)
  2367. * Context: interrupt time.
  2368. *
  2369. * This is the sas iounit controll completion routine.
  2370. * This code is part of the code to initiate the device removal
  2371. * handshake protocal with controller firmware.
  2372. *
  2373. * Return 1 meaning mf should be freed from _base_interrupt
  2374. * 0 means the mf is freed from this function.
  2375. */
  2376. static u8
  2377. _scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  2378. u8 msix_index, u32 reply)
  2379. {
  2380. Mpi2SasIoUnitControlReply_t *mpi_reply =
  2381. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2382. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2383. "sc_complete:handle(0x%04x), (open) "
  2384. "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
  2385. ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
  2386. le16_to_cpu(mpi_reply->IOCStatus),
  2387. le32_to_cpu(mpi_reply->IOCLogInfo)));
  2388. return 1;
  2389. }
  2390. /**
  2391. * _scsih_tm_tr_complete -
  2392. * @ioc: per adapter object
  2393. * @smid: system request message index
  2394. * @msix_index: MSIX table index supplied by the OS
  2395. * @reply: reply message frame(lower 32bit addr)
  2396. * Context: interrupt time.
  2397. *
  2398. * This is the target reset completion routine.
  2399. * This code is part of the code to initiate the device removal
  2400. * handshake protocal with controller firmware.
  2401. * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
  2402. *
  2403. * Return 1 meaning mf should be freed from _base_interrupt
  2404. * 0 means the mf is freed from this function.
  2405. */
  2406. static u8
  2407. _scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  2408. u32 reply)
  2409. {
  2410. u16 handle;
  2411. Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
  2412. Mpi2SCSITaskManagementReply_t *mpi_reply =
  2413. mpt2sas_base_get_reply_virt_addr(ioc, reply);
  2414. Mpi2SasIoUnitControlRequest_t *mpi_request;
  2415. u16 smid_sas_ctrl;
  2416. struct _tr_list *delayed_tr;
  2417. if (ioc->shost_recovery || ioc->remove_host) {
  2418. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
  2419. "progress!\n", __func__, ioc->name));
  2420. return 1;
  2421. }
  2422. mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
  2423. handle = le16_to_cpu(mpi_request_tm->DevHandle);
  2424. if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
  2425. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
  2426. "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
  2427. le16_to_cpu(mpi_reply->DevHandle), smid));
  2428. return 0;
  2429. }
  2430. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  2431. "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
  2432. "loginfo(0x%08x), completed(%d)\n", ioc->name,
  2433. handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
  2434. le32_to_cpu(mpi_reply->IOCLogInfo),
  2435. le32_to_cpu(mpi_reply->TerminationCount)));
  2436. smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
  2437. if (!smid_sas_ctrl) {
  2438. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2439. ioc->name, __func__);
  2440. return 1;
  2441. }
  2442. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
  2443. "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
  2444. ioc->tm_sas_control_cb_idx));
  2445. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
  2446. memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  2447. mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  2448. mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  2449. mpi_request->DevHandle = mpi_request_tm->DevHandle;
  2450. mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
  2451. if (!list_empty(&ioc->delayed_tr_list)) {
  2452. delayed_tr = list_entry(ioc->delayed_tr_list.next,
  2453. struct _tr_list, list);
  2454. mpt2sas_base_free_smid(ioc, smid);
  2455. _scsih_tm_tr_send(ioc, delayed_tr->handle);
  2456. list_del(&delayed_tr->list);
  2457. kfree(delayed_tr);
  2458. return 0; /* tells base_interrupt not to free mf */
  2459. }
  2460. return 1;
  2461. }
  2462. /**
  2463. * _scsih_check_topo_delete_events - sanity check on topo events
  2464. * @ioc: per adapter object
  2465. * @event_data: the event data payload
  2466. *
  2467. * This routine added to better handle cable breaker.
  2468. *
  2469. * This handles the case where driver recieves multiple expander
  2470. * add and delete events in a single shot. When there is a delete event
  2471. * the routine will void any pending add events waiting in the event queue.
  2472. *
  2473. * Return nothing.
  2474. */
  2475. static void
  2476. _scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
  2477. Mpi2EventDataSasTopologyChangeList_t *event_data)
  2478. {
  2479. struct fw_event_work *fw_event;
  2480. Mpi2EventDataSasTopologyChangeList_t *local_event_data;
  2481. u16 expander_handle;
  2482. struct _sas_node *sas_expander;
  2483. unsigned long flags;
  2484. int i, reason_code;
  2485. u16 handle;
  2486. for (i = 0 ; i < event_data->NumEntries; i++) {
  2487. if (event_data->PHY[i].PhyStatus &
  2488. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
  2489. continue;
  2490. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  2491. if (!handle)
  2492. continue;
  2493. reason_code = event_data->PHY[i].PhyStatus &
  2494. MPI2_EVENT_SAS_TOPO_RC_MASK;
  2495. if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
  2496. _scsih_tm_tr_send(ioc, handle);
  2497. }
  2498. expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  2499. if (expander_handle < ioc->sas_hba.num_phys) {
  2500. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2501. return;
  2502. }
  2503. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
  2504. || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
  2505. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  2506. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  2507. expander_handle);
  2508. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  2509. _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
  2510. } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
  2511. _scsih_block_io_to_children_attached_directly(ioc, event_data);
  2512. if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
  2513. return;
  2514. /* mark ignore flag for pending events */
  2515. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  2516. list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
  2517. if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
  2518. fw_event->ignore)
  2519. continue;
  2520. local_event_data = fw_event->event_data;
  2521. if (local_event_data->ExpStatus ==
  2522. MPI2_EVENT_SAS_TOPO_ES_ADDED ||
  2523. local_event_data->ExpStatus ==
  2524. MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
  2525. if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
  2526. expander_handle) {
  2527. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  2528. "setting ignoring flag\n", ioc->name));
  2529. fw_event->ignore = 1;
  2530. }
  2531. }
  2532. }
  2533. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  2534. }
  2535. /**
  2536. * _scsih_flush_running_cmds - completing outstanding commands.
  2537. * @ioc: per adapter object
  2538. *
  2539. * The flushing out of all pending scmd commands following host reset,
  2540. * where all IO is dropped to the floor.
  2541. *
  2542. * Return nothing.
  2543. */
  2544. static void
  2545. _scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
  2546. {
  2547. struct scsi_cmnd *scmd;
  2548. u16 smid;
  2549. u16 count = 0;
  2550. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  2551. scmd = _scsih_scsi_lookup_get(ioc, smid);
  2552. if (!scmd)
  2553. continue;
  2554. count++;
  2555. mpt2sas_base_free_smid(ioc, smid);
  2556. scsi_dma_unmap(scmd);
  2557. scmd->result = DID_RESET << 16;
  2558. scmd->scsi_done(scmd);
  2559. }
  2560. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
  2561. ioc->name, count));
  2562. }
  2563. /**
  2564. * _scsih_setup_eedp - setup MPI request for EEDP transfer
  2565. * @scmd: pointer to scsi command object
  2566. * @mpi_request: pointer to the SCSI_IO reqest message frame
  2567. *
  2568. * Supporting protection 1 and 3.
  2569. *
  2570. * Returns nothing
  2571. */
  2572. static void
  2573. _scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
  2574. {
  2575. u16 eedp_flags;
  2576. unsigned char prot_op = scsi_get_prot_op(scmd);
  2577. unsigned char prot_type = scsi_get_prot_type(scmd);
  2578. if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
  2579. return;
  2580. if (prot_op == SCSI_PROT_READ_STRIP)
  2581. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
  2582. else if (prot_op == SCSI_PROT_WRITE_INSERT)
  2583. eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  2584. else
  2585. return;
  2586. switch (prot_type) {
  2587. case SCSI_PROT_DIF_TYPE1:
  2588. /*
  2589. * enable ref/guard checking
  2590. * auto increment ref tag
  2591. */
  2592. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  2593. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  2594. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2595. mpi_request->CDB.EEDP32.PrimaryReferenceTag =
  2596. cpu_to_be32(scsi_get_lba(scmd));
  2597. break;
  2598. case SCSI_PROT_DIF_TYPE2:
  2599. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  2600. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  2601. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2602. break;
  2603. case SCSI_PROT_DIF_TYPE3:
  2604. /*
  2605. * enable guard checking
  2606. */
  2607. eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  2608. break;
  2609. }
  2610. mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
  2611. mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
  2612. }
  2613. /**
  2614. * _scsih_eedp_error_handling - return sense code for EEDP errors
  2615. * @scmd: pointer to scsi command object
  2616. * @ioc_status: ioc status
  2617. *
  2618. * Returns nothing
  2619. */
  2620. static void
  2621. _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
  2622. {
  2623. u8 ascq;
  2624. u8 sk;
  2625. u8 host_byte;
  2626. switch (ioc_status) {
  2627. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2628. ascq = 0x01;
  2629. break;
  2630. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2631. ascq = 0x02;
  2632. break;
  2633. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2634. ascq = 0x03;
  2635. break;
  2636. default:
  2637. ascq = 0x00;
  2638. break;
  2639. }
  2640. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2641. sk = ILLEGAL_REQUEST;
  2642. host_byte = DID_ABORT;
  2643. } else {
  2644. sk = ABORTED_COMMAND;
  2645. host_byte = DID_OK;
  2646. }
  2647. scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
  2648. scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
  2649. SAM_STAT_CHECK_CONDITION;
  2650. }
  2651. /**
  2652. * _scsih_qcmd - main scsi request entry point
  2653. * @scmd: pointer to scsi command object
  2654. * @done: function pointer to be invoked on completion
  2655. *
  2656. * The callback index is set inside `ioc->scsi_io_cb_idx`.
  2657. *
  2658. * Returns 0 on success. If there's a failure, return either:
  2659. * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
  2660. * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
  2661. */
  2662. static int
  2663. _scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
  2664. {
  2665. struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
  2666. struct MPT2SAS_DEVICE *sas_device_priv_data;
  2667. struct MPT2SAS_TARGET *sas_target_priv_data;
  2668. Mpi2SCSIIORequest_t *mpi_request;
  2669. u32 mpi_control;
  2670. u16 smid;
  2671. scmd->scsi_done = done;
  2672. sas_device_priv_data = scmd->device->hostdata;
  2673. if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
  2674. scmd->result = DID_NO_CONNECT << 16;
  2675. scmd->scsi_done(scmd);
  2676. return 0;
  2677. }
  2678. sas_target_priv_data = sas_device_priv_data->sas_target;
  2679. /* invalid device handle */
  2680. if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
  2681. scmd->result = DID_NO_CONNECT << 16;
  2682. scmd->scsi_done(scmd);
  2683. return 0;
  2684. }
  2685. /* host recovery or link resets sent via IOCTLs */
  2686. if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
  2687. return SCSI_MLQUEUE_HOST_BUSY;
  2688. /* device busy with task managment */
  2689. else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
  2690. return SCSI_MLQUEUE_DEVICE_BUSY;
  2691. /* device has been deleted */
  2692. else if (sas_target_priv_data->deleted) {
  2693. scmd->result = DID_NO_CONNECT << 16;
  2694. scmd->scsi_done(scmd);
  2695. return 0;
  2696. }
  2697. if (scmd->sc_data_direction == DMA_FROM_DEVICE)
  2698. mpi_control = MPI2_SCSIIO_CONTROL_READ;
  2699. else if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2700. mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
  2701. else
  2702. mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
  2703. /* set tags */
  2704. if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
  2705. if (scmd->device->tagged_supported) {
  2706. if (scmd->device->ordered_tags)
  2707. mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
  2708. else
  2709. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2710. } else
  2711. /* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
  2712. /* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
  2713. */
  2714. mpi_control |= (0x500);
  2715. } else
  2716. mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
  2717. /* Make sure Device is not raid volume */
  2718. if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
  2719. sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
  2720. mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
  2721. smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
  2722. if (!smid) {
  2723. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2724. ioc->name, __func__);
  2725. goto out;
  2726. }
  2727. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2728. memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
  2729. _scsih_setup_eedp(scmd, mpi_request);
  2730. if (scmd->cmd_len == 32)
  2731. mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
  2732. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2733. if (sas_device_priv_data->sas_target->flags &
  2734. MPT_TARGET_FLAGS_RAID_COMPONENT)
  2735. mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
  2736. else
  2737. mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  2738. mpi_request->DevHandle =
  2739. cpu_to_le16(sas_device_priv_data->sas_target->handle);
  2740. mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  2741. mpi_request->Control = cpu_to_le32(mpi_control);
  2742. mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
  2743. mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
  2744. mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  2745. mpi_request->SenseBufferLowAddress =
  2746. mpt2sas_base_get_sense_buffer_dma(ioc, smid);
  2747. mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
  2748. mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
  2749. MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
  2750. mpi_request->VF_ID = 0; /* TODO */
  2751. mpi_request->VP_ID = 0;
  2752. int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
  2753. mpi_request->LUN);
  2754. memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
  2755. if (!mpi_request->DataLength) {
  2756. mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
  2757. } else {
  2758. if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
  2759. mpt2sas_base_free_smid(ioc, smid);
  2760. goto out;
  2761. }
  2762. }
  2763. if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
  2764. mpt2sas_base_put_smid_scsi_io(ioc, smid,
  2765. sas_device_priv_data->sas_target->handle);
  2766. else
  2767. mpt2sas_base_put_smid_default(ioc, smid);
  2768. return 0;
  2769. out:
  2770. return SCSI_MLQUEUE_HOST_BUSY;
  2771. }
  2772. /**
  2773. * _scsih_normalize_sense - normalize descriptor and fixed format sense data
  2774. * @sense_buffer: sense data returned by target
  2775. * @data: normalized skey/asc/ascq
  2776. *
  2777. * Return nothing.
  2778. */
  2779. static void
  2780. _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
  2781. {
  2782. if ((sense_buffer[0] & 0x7F) >= 0x72) {
  2783. /* descriptor format */
  2784. data->skey = sense_buffer[1] & 0x0F;
  2785. data->asc = sense_buffer[2];
  2786. data->ascq = sense_buffer[3];
  2787. } else {
  2788. /* fixed format */
  2789. data->skey = sense_buffer[2] & 0x0F;
  2790. data->asc = sense_buffer[12];
  2791. data->ascq = sense_buffer[13];
  2792. }
  2793. }
  2794. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  2795. /**
  2796. * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
  2797. * @ioc: per adapter object
  2798. * @scmd: pointer to scsi command object
  2799. * @mpi_reply: reply mf payload returned from firmware
  2800. *
  2801. * scsi_status - SCSI Status code returned from target device
  2802. * scsi_state - state info associated with SCSI_IO determined by ioc
  2803. * ioc_status - ioc supplied status info
  2804. *
  2805. * Return nothing.
  2806. */
  2807. static void
  2808. _scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  2809. Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
  2810. {
  2811. u32 response_info;
  2812. u8 *response_bytes;
  2813. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  2814. MPI2_IOCSTATUS_MASK;
  2815. u8 scsi_state = mpi_reply->SCSIState;
  2816. u8 scsi_status = mpi_reply->SCSIStatus;
  2817. char *desc_ioc_state = NULL;
  2818. char *desc_scsi_status = NULL;
  2819. char *desc_scsi_state = ioc->tmp_string;
  2820. u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  2821. if (log_info == 0x31170000)
  2822. return;
  2823. switch (ioc_status) {
  2824. case MPI2_IOCSTATUS_SUCCESS:
  2825. desc_ioc_state = "success";
  2826. break;
  2827. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  2828. desc_ioc_state = "invalid function";
  2829. break;
  2830. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  2831. desc_ioc_state = "scsi recovered error";
  2832. break;
  2833. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  2834. desc_ioc_state = "scsi invalid dev handle";
  2835. break;
  2836. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  2837. desc_ioc_state = "scsi device not there";
  2838. break;
  2839. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  2840. desc_ioc_state = "scsi data overrun";
  2841. break;
  2842. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  2843. desc_ioc_state = "scsi data underrun";
  2844. break;
  2845. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  2846. desc_ioc_state = "scsi io data error";
  2847. break;
  2848. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  2849. desc_ioc_state = "scsi protocol error";
  2850. break;
  2851. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  2852. desc_ioc_state = "scsi task terminated";
  2853. break;
  2854. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  2855. desc_ioc_state = "scsi residual mismatch";
  2856. break;
  2857. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  2858. desc_ioc_state = "scsi task mgmt failed";
  2859. break;
  2860. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  2861. desc_ioc_state = "scsi ioc terminated";
  2862. break;
  2863. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  2864. desc_ioc_state = "scsi ext terminated";
  2865. break;
  2866. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  2867. desc_ioc_state = "eedp guard error";
  2868. break;
  2869. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  2870. desc_ioc_state = "eedp ref tag error";
  2871. break;
  2872. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  2873. desc_ioc_state = "eedp app tag error";
  2874. break;
  2875. default:
  2876. desc_ioc_state = "unknown";
  2877. break;
  2878. }
  2879. switch (scsi_status) {
  2880. case MPI2_SCSI_STATUS_GOOD:
  2881. desc_scsi_status = "good";
  2882. break;
  2883. case MPI2_SCSI_STATUS_CHECK_CONDITION:
  2884. desc_scsi_status = "check condition";
  2885. break;
  2886. case MPI2_SCSI_STATUS_CONDITION_MET:
  2887. desc_scsi_status = "condition met";
  2888. break;
  2889. case MPI2_SCSI_STATUS_BUSY:
  2890. desc_scsi_status = "busy";
  2891. break;
  2892. case MPI2_SCSI_STATUS_INTERMEDIATE:
  2893. desc_scsi_status = "intermediate";
  2894. break;
  2895. case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
  2896. desc_scsi_status = "intermediate condmet";
  2897. break;
  2898. case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
  2899. desc_scsi_status = "reservation conflict";
  2900. break;
  2901. case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
  2902. desc_scsi_status = "command terminated";
  2903. break;
  2904. case MPI2_SCSI_STATUS_TASK_SET_FULL:
  2905. desc_scsi_status = "task set full";
  2906. break;
  2907. case MPI2_SCSI_STATUS_ACA_ACTIVE:
  2908. desc_scsi_status = "aca active";
  2909. break;
  2910. case MPI2_SCSI_STATUS_TASK_ABORTED:
  2911. desc_scsi_status = "task aborted";
  2912. break;
  2913. default:
  2914. desc_scsi_status = "unknown";
  2915. break;
  2916. }
  2917. desc_scsi_state[0] = '\0';
  2918. if (!scsi_state)
  2919. desc_scsi_state = " ";
  2920. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  2921. strcat(desc_scsi_state, "response info ");
  2922. if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  2923. strcat(desc_scsi_state, "state terminated ");
  2924. if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
  2925. strcat(desc_scsi_state, "no status ");
  2926. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
  2927. strcat(desc_scsi_state, "autosense failed ");
  2928. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
  2929. strcat(desc_scsi_state, "autosense valid ");
  2930. scsi_print_command(scmd);
  2931. printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
  2932. "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
  2933. le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
  2934. ioc_status, smid);
  2935. printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
  2936. "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
  2937. scsi_get_resid(scmd));
  2938. printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
  2939. "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
  2940. le32_to_cpu(mpi_reply->TransferCount), scmd->result);
  2941. printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
  2942. "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
  2943. scsi_status, desc_scsi_state, scsi_state);
  2944. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  2945. struct sense_info data;
  2946. _scsih_normalize_sense(scmd->sense_buffer, &data);
  2947. printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
  2948. "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
  2949. data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
  2950. }
  2951. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
  2952. response_info = le32_to_cpu(mpi_reply->ResponseInfo);
  2953. response_bytes = (u8 *)&response_info;
  2954. _scsih_response_code(ioc, response_bytes[0]);
  2955. }
  2956. }
  2957. #endif
  2958. /**
  2959. * _scsih_smart_predicted_fault - illuminate Fault LED
  2960. * @ioc: per adapter object
  2961. * @handle: device handle
  2962. *
  2963. * Return nothing.
  2964. */
  2965. static void
  2966. _scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  2967. {
  2968. Mpi2SepReply_t mpi_reply;
  2969. Mpi2SepRequest_t mpi_request;
  2970. struct scsi_target *starget;
  2971. struct MPT2SAS_TARGET *sas_target_priv_data;
  2972. Mpi2EventNotificationReply_t *event_reply;
  2973. Mpi2EventDataSasDeviceStatusChange_t *event_data;
  2974. struct _sas_device *sas_device;
  2975. ssize_t sz;
  2976. unsigned long flags;
  2977. /* only handle non-raid devices */
  2978. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  2979. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  2980. if (!sas_device) {
  2981. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2982. return;
  2983. }
  2984. starget = sas_device->starget;
  2985. sas_target_priv_data = starget->hostdata;
  2986. if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
  2987. ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
  2988. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2989. return;
  2990. }
  2991. starget_printk(KERN_WARNING, starget, "predicted fault\n");
  2992. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  2993. if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
  2994. memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
  2995. mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
  2996. mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
  2997. mpi_request.SlotStatus =
  2998. cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
  2999. mpi_request.DevHandle = cpu_to_le16(handle);
  3000. mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
  3001. if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
  3002. &mpi_request)) != 0) {
  3003. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3004. ioc->name, __FILE__, __LINE__, __func__);
  3005. return;
  3006. }
  3007. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
  3008. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
  3009. "enclosure_processor: ioc_status (0x%04x), "
  3010. "loginfo(0x%08x)\n", ioc->name,
  3011. le16_to_cpu(mpi_reply.IOCStatus),
  3012. le32_to_cpu(mpi_reply.IOCLogInfo)));
  3013. return;
  3014. }
  3015. }
  3016. /* insert into event log */
  3017. sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
  3018. sizeof(Mpi2EventDataSasDeviceStatusChange_t);
  3019. event_reply = kzalloc(sz, GFP_KERNEL);
  3020. if (!event_reply) {
  3021. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3022. ioc->name, __FILE__, __LINE__, __func__);
  3023. return;
  3024. }
  3025. event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  3026. event_reply->Event =
  3027. cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  3028. event_reply->MsgLength = sz/4;
  3029. event_reply->EventDataLength =
  3030. cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
  3031. event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
  3032. event_reply->EventData;
  3033. event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
  3034. event_data->ASC = 0x5D;
  3035. event_data->DevHandle = cpu_to_le16(handle);
  3036. event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
  3037. mpt2sas_ctl_add_to_event_log(ioc, event_reply);
  3038. kfree(event_reply);
  3039. }
  3040. /**
  3041. * _scsih_io_done - scsi request callback
  3042. * @ioc: per adapter object
  3043. * @smid: system request message index
  3044. * @msix_index: MSIX table index supplied by the OS
  3045. * @reply: reply message frame(lower 32bit addr)
  3046. *
  3047. * Callback handler when using _scsih_qcmd.
  3048. *
  3049. * Return 1 meaning mf should be freed from _base_interrupt
  3050. * 0 means the mf is freed from this function.
  3051. */
  3052. static u8
  3053. _scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  3054. {
  3055. Mpi2SCSIIORequest_t *mpi_request;
  3056. Mpi2SCSIIOReply_t *mpi_reply;
  3057. struct scsi_cmnd *scmd;
  3058. u16 ioc_status;
  3059. u32 xfer_cnt;
  3060. u8 scsi_state;
  3061. u8 scsi_status;
  3062. u32 log_info;
  3063. struct MPT2SAS_DEVICE *sas_device_priv_data;
  3064. u32 response_code = 0;
  3065. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  3066. scmd = _scsih_scsi_lookup_get(ioc, smid);
  3067. if (scmd == NULL)
  3068. return 1;
  3069. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3070. if (mpi_reply == NULL) {
  3071. scmd->result = DID_OK << 16;
  3072. goto out;
  3073. }
  3074. sas_device_priv_data = scmd->device->hostdata;
  3075. if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
  3076. sas_device_priv_data->sas_target->deleted) {
  3077. scmd->result = DID_NO_CONNECT << 16;
  3078. goto out;
  3079. }
  3080. /* turning off TLR */
  3081. scsi_state = mpi_reply->SCSIState;
  3082. if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
  3083. response_code =
  3084. le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
  3085. if (!sas_device_priv_data->tlr_snoop_check) {
  3086. sas_device_priv_data->tlr_snoop_check++;
  3087. if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
  3088. sas_is_tlr_enabled(scmd->device) &&
  3089. response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
  3090. sas_disable_tlr(scmd->device);
  3091. sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
  3092. }
  3093. }
  3094. xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
  3095. scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
  3096. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  3097. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  3098. log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
  3099. else
  3100. log_info = 0;
  3101. ioc_status &= MPI2_IOCSTATUS_MASK;
  3102. scsi_status = mpi_reply->SCSIStatus;
  3103. if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
  3104. (scsi_status == MPI2_SCSI_STATUS_BUSY ||
  3105. scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
  3106. scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
  3107. ioc_status = MPI2_IOCSTATUS_SUCCESS;
  3108. }
  3109. if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
  3110. struct sense_info data;
  3111. const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
  3112. smid);
  3113. u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
  3114. le32_to_cpu(mpi_reply->SenseCount));
  3115. memcpy(scmd->sense_buffer, sense_data, sz);
  3116. _scsih_normalize_sense(scmd->sense_buffer, &data);
  3117. /* failure prediction threshold exceeded */
  3118. if (data.asc == 0x5D)
  3119. _scsih_smart_predicted_fault(ioc,
  3120. le16_to_cpu(mpi_reply->DevHandle));
  3121. }
  3122. switch (ioc_status) {
  3123. case MPI2_IOCSTATUS_BUSY:
  3124. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  3125. scmd->result = SAM_STAT_BUSY;
  3126. break;
  3127. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  3128. scmd->result = DID_NO_CONNECT << 16;
  3129. break;
  3130. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  3131. if (sas_device_priv_data->block) {
  3132. scmd->result = DID_TRANSPORT_DISRUPTED << 16;
  3133. goto out;
  3134. }
  3135. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  3136. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  3137. scmd->result = DID_RESET << 16;
  3138. break;
  3139. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  3140. if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
  3141. scmd->result = DID_SOFT_ERROR << 16;
  3142. else
  3143. scmd->result = (DID_OK << 16) | scsi_status;
  3144. break;
  3145. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  3146. scmd->result = (DID_OK << 16) | scsi_status;
  3147. if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
  3148. break;
  3149. if (xfer_cnt < scmd->underflow) {
  3150. if (scsi_status == SAM_STAT_BUSY)
  3151. scmd->result = SAM_STAT_BUSY;
  3152. else
  3153. scmd->result = DID_SOFT_ERROR << 16;
  3154. } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  3155. MPI2_SCSI_STATE_NO_SCSI_STATUS))
  3156. scmd->result = DID_SOFT_ERROR << 16;
  3157. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  3158. scmd->result = DID_RESET << 16;
  3159. else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
  3160. mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
  3161. mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
  3162. scmd->result = (DRIVER_SENSE << 24) |
  3163. SAM_STAT_CHECK_CONDITION;
  3164. scmd->sense_buffer[0] = 0x70;
  3165. scmd->sense_buffer[2] = ILLEGAL_REQUEST;
  3166. scmd->sense_buffer[12] = 0x20;
  3167. scmd->sense_buffer[13] = 0;
  3168. }
  3169. break;
  3170. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  3171. scsi_set_resid(scmd, 0);
  3172. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  3173. case MPI2_IOCSTATUS_SUCCESS:
  3174. scmd->result = (DID_OK << 16) | scsi_status;
  3175. if (response_code ==
  3176. MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
  3177. (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
  3178. MPI2_SCSI_STATE_NO_SCSI_STATUS)))
  3179. scmd->result = DID_SOFT_ERROR << 16;
  3180. else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
  3181. scmd->result = DID_RESET << 16;
  3182. break;
  3183. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  3184. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  3185. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  3186. _scsih_eedp_error_handling(scmd, ioc_status);
  3187. break;
  3188. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  3189. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  3190. case MPI2_IOCSTATUS_INVALID_SGL:
  3191. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  3192. case MPI2_IOCSTATUS_INVALID_FIELD:
  3193. case MPI2_IOCSTATUS_INVALID_STATE:
  3194. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  3195. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  3196. default:
  3197. scmd->result = DID_SOFT_ERROR << 16;
  3198. break;
  3199. }
  3200. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3201. if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
  3202. _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
  3203. #endif
  3204. out:
  3205. scsi_dma_unmap(scmd);
  3206. scmd->scsi_done(scmd);
  3207. return 1;
  3208. }
  3209. /**
  3210. * _scsih_sas_host_refresh - refreshing sas host object contents
  3211. * @ioc: per adapter object
  3212. * Context: user
  3213. *
  3214. * During port enable, fw will send topology events for every device. Its
  3215. * possible that the handles may change from the previous setting, so this
  3216. * code keeping handles updating if changed.
  3217. *
  3218. * Return nothing.
  3219. */
  3220. static void
  3221. _scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
  3222. {
  3223. u16 sz;
  3224. u16 ioc_status;
  3225. int i;
  3226. Mpi2ConfigReply_t mpi_reply;
  3227. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  3228. u16 attached_handle;
  3229. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
  3230. "updating handles for sas_host(0x%016llx)\n",
  3231. ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
  3232. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
  3233. * sizeof(Mpi2SasIOUnit0PhyData_t));
  3234. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  3235. if (!sas_iounit_pg0) {
  3236. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3237. ioc->name, __FILE__, __LINE__, __func__);
  3238. return;
  3239. }
  3240. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  3241. sas_iounit_pg0, sz)) != 0)
  3242. goto out;
  3243. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  3244. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  3245. goto out;
  3246. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  3247. if (i == 0)
  3248. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  3249. PhyData[0].ControllerDevHandle);
  3250. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  3251. attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
  3252. AttachedDevHandle);
  3253. mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
  3254. attached_handle, i, sas_iounit_pg0->PhyData[i].
  3255. NegotiatedLinkRate >> 4);
  3256. }
  3257. out:
  3258. kfree(sas_iounit_pg0);
  3259. }
  3260. /**
  3261. * _scsih_sas_host_add - create sas host object
  3262. * @ioc: per adapter object
  3263. *
  3264. * Creating host side data object, stored in ioc->sas_hba
  3265. *
  3266. * Return nothing.
  3267. */
  3268. static void
  3269. _scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
  3270. {
  3271. int i;
  3272. Mpi2ConfigReply_t mpi_reply;
  3273. Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
  3274. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  3275. Mpi2SasPhyPage0_t phy_pg0;
  3276. Mpi2SasDevicePage0_t sas_device_pg0;
  3277. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3278. u16 ioc_status;
  3279. u16 sz;
  3280. u16 device_missing_delay;
  3281. mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
  3282. if (!ioc->sas_hba.num_phys) {
  3283. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3284. ioc->name, __FILE__, __LINE__, __func__);
  3285. return;
  3286. }
  3287. /* sas_iounit page 0 */
  3288. sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
  3289. sizeof(Mpi2SasIOUnit0PhyData_t));
  3290. sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
  3291. if (!sas_iounit_pg0) {
  3292. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3293. ioc->name, __FILE__, __LINE__, __func__);
  3294. return;
  3295. }
  3296. if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
  3297. sas_iounit_pg0, sz))) {
  3298. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3299. ioc->name, __FILE__, __LINE__, __func__);
  3300. goto out;
  3301. }
  3302. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3303. MPI2_IOCSTATUS_MASK;
  3304. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3305. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3306. ioc->name, __FILE__, __LINE__, __func__);
  3307. goto out;
  3308. }
  3309. /* sas_iounit page 1 */
  3310. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  3311. sizeof(Mpi2SasIOUnit1PhyData_t));
  3312. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  3313. if (!sas_iounit_pg1) {
  3314. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3315. ioc->name, __FILE__, __LINE__, __func__);
  3316. goto out;
  3317. }
  3318. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  3319. sas_iounit_pg1, sz))) {
  3320. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3321. ioc->name, __FILE__, __LINE__, __func__);
  3322. goto out;
  3323. }
  3324. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3325. MPI2_IOCSTATUS_MASK;
  3326. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3327. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3328. ioc->name, __FILE__, __LINE__, __func__);
  3329. goto out;
  3330. }
  3331. ioc->io_missing_delay =
  3332. le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
  3333. device_missing_delay =
  3334. le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
  3335. if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  3336. ioc->device_missing_delay = (device_missing_delay &
  3337. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  3338. else
  3339. ioc->device_missing_delay = device_missing_delay &
  3340. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  3341. ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
  3342. ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
  3343. sizeof(struct _sas_phy), GFP_KERNEL);
  3344. if (!ioc->sas_hba.phy) {
  3345. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3346. ioc->name, __FILE__, __LINE__, __func__);
  3347. goto out;
  3348. }
  3349. for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
  3350. if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  3351. i))) {
  3352. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3353. ioc->name, __FILE__, __LINE__, __func__);
  3354. goto out;
  3355. }
  3356. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3357. MPI2_IOCSTATUS_MASK;
  3358. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3359. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3360. ioc->name, __FILE__, __LINE__, __func__);
  3361. goto out;
  3362. }
  3363. if (i == 0)
  3364. ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
  3365. PhyData[0].ControllerDevHandle);
  3366. ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
  3367. ioc->sas_hba.phy[i].phy_id = i;
  3368. mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
  3369. phy_pg0, ioc->sas_hba.parent_dev);
  3370. }
  3371. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3372. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
  3373. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3374. ioc->name, __FILE__, __LINE__, __func__);
  3375. goto out;
  3376. }
  3377. ioc->sas_hba.enclosure_handle =
  3378. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3379. ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3380. printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
  3381. "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
  3382. (unsigned long long) ioc->sas_hba.sas_address,
  3383. ioc->sas_hba.num_phys) ;
  3384. if (ioc->sas_hba.enclosure_handle) {
  3385. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3386. &enclosure_pg0,
  3387. MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3388. ioc->sas_hba.enclosure_handle))) {
  3389. ioc->sas_hba.enclosure_logical_id =
  3390. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3391. }
  3392. }
  3393. out:
  3394. kfree(sas_iounit_pg1);
  3395. kfree(sas_iounit_pg0);
  3396. }
  3397. /**
  3398. * _scsih_expander_add - creating expander object
  3399. * @ioc: per adapter object
  3400. * @handle: expander handle
  3401. *
  3402. * Creating expander object, stored in ioc->sas_expander_list.
  3403. *
  3404. * Return 0 for success, else error.
  3405. */
  3406. static int
  3407. _scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3408. {
  3409. struct _sas_node *sas_expander;
  3410. Mpi2ConfigReply_t mpi_reply;
  3411. Mpi2ExpanderPage0_t expander_pg0;
  3412. Mpi2ExpanderPage1_t expander_pg1;
  3413. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3414. u32 ioc_status;
  3415. u16 parent_handle;
  3416. __le64 sas_address, sas_address_parent = 0;
  3417. int i;
  3418. unsigned long flags;
  3419. struct _sas_port *mpt2sas_port = NULL;
  3420. int rc = 0;
  3421. if (!handle)
  3422. return -1;
  3423. if (ioc->shost_recovery)
  3424. return -1;
  3425. if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  3426. MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
  3427. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3428. ioc->name, __FILE__, __LINE__, __func__);
  3429. return -1;
  3430. }
  3431. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3432. MPI2_IOCSTATUS_MASK;
  3433. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3434. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3435. ioc->name, __FILE__, __LINE__, __func__);
  3436. return -1;
  3437. }
  3438. /* handle out of order topology events */
  3439. parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
  3440. if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
  3441. != 0) {
  3442. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3443. ioc->name, __FILE__, __LINE__, __func__);
  3444. return -1;
  3445. }
  3446. if (sas_address_parent != ioc->sas_hba.sas_address) {
  3447. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3448. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3449. sas_address_parent);
  3450. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3451. if (!sas_expander) {
  3452. rc = _scsih_expander_add(ioc, parent_handle);
  3453. if (rc != 0)
  3454. return rc;
  3455. }
  3456. }
  3457. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3458. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  3459. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3460. sas_address);
  3461. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3462. if (sas_expander)
  3463. return 0;
  3464. sas_expander = kzalloc(sizeof(struct _sas_node),
  3465. GFP_KERNEL);
  3466. if (!sas_expander) {
  3467. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3468. ioc->name, __FILE__, __LINE__, __func__);
  3469. return -1;
  3470. }
  3471. sas_expander->handle = handle;
  3472. sas_expander->num_phys = expander_pg0.NumPhys;
  3473. sas_expander->sas_address_parent = sas_address_parent;
  3474. sas_expander->sas_address = sas_address;
  3475. printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
  3476. " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
  3477. handle, parent_handle, (unsigned long long)
  3478. sas_expander->sas_address, sas_expander->num_phys);
  3479. if (!sas_expander->num_phys)
  3480. goto out_fail;
  3481. sas_expander->phy = kcalloc(sas_expander->num_phys,
  3482. sizeof(struct _sas_phy), GFP_KERNEL);
  3483. if (!sas_expander->phy) {
  3484. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3485. ioc->name, __FILE__, __LINE__, __func__);
  3486. rc = -1;
  3487. goto out_fail;
  3488. }
  3489. INIT_LIST_HEAD(&sas_expander->sas_port_list);
  3490. mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
  3491. sas_address_parent);
  3492. if (!mpt2sas_port) {
  3493. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3494. ioc->name, __FILE__, __LINE__, __func__);
  3495. rc = -1;
  3496. goto out_fail;
  3497. }
  3498. sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
  3499. for (i = 0 ; i < sas_expander->num_phys ; i++) {
  3500. if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
  3501. &expander_pg1, i, handle))) {
  3502. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3503. ioc->name, __FILE__, __LINE__, __func__);
  3504. rc = -1;
  3505. goto out_fail;
  3506. }
  3507. sas_expander->phy[i].handle = handle;
  3508. sas_expander->phy[i].phy_id = i;
  3509. if ((mpt2sas_transport_add_expander_phy(ioc,
  3510. &sas_expander->phy[i], expander_pg1,
  3511. sas_expander->parent_dev))) {
  3512. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3513. ioc->name, __FILE__, __LINE__, __func__);
  3514. rc = -1;
  3515. goto out_fail;
  3516. }
  3517. }
  3518. if (sas_expander->enclosure_handle) {
  3519. if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
  3520. &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3521. sas_expander->enclosure_handle))) {
  3522. sas_expander->enclosure_logical_id =
  3523. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3524. }
  3525. }
  3526. _scsih_expander_node_add(ioc, sas_expander);
  3527. return 0;
  3528. out_fail:
  3529. if (mpt2sas_port)
  3530. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  3531. sas_address_parent);
  3532. kfree(sas_expander);
  3533. return rc;
  3534. }
  3535. /**
  3536. * _scsih_done - scsih callback handler.
  3537. * @ioc: per adapter object
  3538. * @smid: system request message index
  3539. * @msix_index: MSIX table index supplied by the OS
  3540. * @reply: reply message frame(lower 32bit addr)
  3541. *
  3542. * Callback handler when sending internal generated message frames.
  3543. * The callback index passed is `ioc->scsih_cb_idx`
  3544. *
  3545. * Return 1 meaning mf should be freed from _base_interrupt
  3546. * 0 means the mf is freed from this function.
  3547. */
  3548. static u8
  3549. _scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
  3550. {
  3551. MPI2DefaultReply_t *mpi_reply;
  3552. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  3553. if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
  3554. return 1;
  3555. if (ioc->scsih_cmds.smid != smid)
  3556. return 1;
  3557. ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
  3558. if (mpi_reply) {
  3559. memcpy(ioc->scsih_cmds.reply, mpi_reply,
  3560. mpi_reply->MsgLength*4);
  3561. ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
  3562. }
  3563. ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
  3564. complete(&ioc->scsih_cmds.done);
  3565. return 1;
  3566. }
  3567. /**
  3568. * _scsih_expander_remove - removing expander object
  3569. * @ioc: per adapter object
  3570. * @sas_address: expander sas_address
  3571. *
  3572. * Return nothing.
  3573. */
  3574. static void
  3575. _scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
  3576. {
  3577. struct _sas_node *sas_expander;
  3578. unsigned long flags;
  3579. if (ioc->shost_recovery)
  3580. return;
  3581. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  3582. sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
  3583. sas_address);
  3584. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  3585. _scsih_expander_node_remove(ioc, sas_expander);
  3586. }
  3587. /**
  3588. * _scsih_check_access_status - check access flags
  3589. * @ioc: per adapter object
  3590. * @sas_address: sas address
  3591. * @handle: sas device handle
  3592. * @access_flags: errors returned during discovery of the device
  3593. *
  3594. * Return 0 for success, else failure
  3595. */
  3596. static u8
  3597. _scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  3598. u16 handle, u8 access_status)
  3599. {
  3600. u8 rc = 1;
  3601. char *desc = NULL;
  3602. switch (access_status) {
  3603. case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
  3604. case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
  3605. rc = 0;
  3606. break;
  3607. case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
  3608. desc = "sata capability failed";
  3609. break;
  3610. case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
  3611. desc = "sata affiliation conflict";
  3612. break;
  3613. case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
  3614. desc = "route not addressable";
  3615. break;
  3616. case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
  3617. desc = "smp error not addressable";
  3618. break;
  3619. case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
  3620. desc = "device blocked";
  3621. break;
  3622. case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
  3623. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
  3624. case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
  3625. case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
  3626. case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
  3627. case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
  3628. case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
  3629. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
  3630. case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
  3631. case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
  3632. case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
  3633. case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
  3634. desc = "sata initialization failed";
  3635. break;
  3636. default:
  3637. desc = "unknown";
  3638. break;
  3639. }
  3640. if (!rc)
  3641. return 0;
  3642. printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
  3643. "handle(0x%04x)\n", ioc->name, desc,
  3644. (unsigned long long)sas_address, handle);
  3645. return rc;
  3646. }
  3647. static void
  3648. _scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  3649. {
  3650. Mpi2ConfigReply_t mpi_reply;
  3651. Mpi2SasDevicePage0_t sas_device_pg0;
  3652. struct _sas_device *sas_device;
  3653. u32 ioc_status;
  3654. unsigned long flags;
  3655. u64 sas_address;
  3656. struct scsi_target *starget;
  3657. struct MPT2SAS_TARGET *sas_target_priv_data;
  3658. u32 device_info;
  3659. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3660. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
  3661. return;
  3662. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  3663. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  3664. return;
  3665. /* check if this is end device */
  3666. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  3667. if (!(_scsih_is_end_device(device_info)))
  3668. return;
  3669. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3670. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3671. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  3672. sas_address);
  3673. if (!sas_device) {
  3674. printk(MPT2SAS_ERR_FMT "device is not present "
  3675. "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
  3676. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3677. return;
  3678. }
  3679. if (unlikely(sas_device->handle != handle)) {
  3680. starget = sas_device->starget;
  3681. sas_target_priv_data = starget->hostdata;
  3682. starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
  3683. " to (0x%04x)!!!\n", sas_device->handle, handle);
  3684. sas_target_priv_data->handle = handle;
  3685. sas_device->handle = handle;
  3686. }
  3687. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3688. /* check if device is present */
  3689. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  3690. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  3691. printk(MPT2SAS_ERR_FMT "device is not present "
  3692. "handle(0x%04x), flags!!!\n", ioc->name, handle);
  3693. return;
  3694. }
  3695. /* check if there were any issues with discovery */
  3696. if (_scsih_check_access_status(ioc, sas_address, handle,
  3697. sas_device_pg0.AccessStatus))
  3698. return;
  3699. _scsih_ublock_io_device(ioc, handle);
  3700. }
  3701. /**
  3702. * _scsih_add_device - creating sas device object
  3703. * @ioc: per adapter object
  3704. * @handle: sas device handle
  3705. * @phy_num: phy number end device attached to
  3706. * @is_pd: is this hidden raid component
  3707. *
  3708. * Creating end device object, stored in ioc->sas_device_list.
  3709. *
  3710. * Returns 0 for success, non-zero for failure.
  3711. */
  3712. static int
  3713. _scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
  3714. {
  3715. Mpi2ConfigReply_t mpi_reply;
  3716. Mpi2SasDevicePage0_t sas_device_pg0;
  3717. Mpi2SasEnclosurePage0_t enclosure_pg0;
  3718. struct _sas_device *sas_device;
  3719. u32 ioc_status;
  3720. __le64 sas_address;
  3721. u32 device_info;
  3722. unsigned long flags;
  3723. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  3724. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  3725. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3726. ioc->name, __FILE__, __LINE__, __func__);
  3727. return -1;
  3728. }
  3729. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  3730. MPI2_IOCSTATUS_MASK;
  3731. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3732. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3733. ioc->name, __FILE__, __LINE__, __func__);
  3734. return -1;
  3735. }
  3736. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  3737. /* check if device is present */
  3738. if (!(le16_to_cpu(sas_device_pg0.Flags) &
  3739. MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
  3740. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3741. ioc->name, __FILE__, __LINE__, __func__);
  3742. printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
  3743. ioc->name, le16_to_cpu(sas_device_pg0.Flags));
  3744. return -1;
  3745. }
  3746. /* check if there were any issues with discovery */
  3747. if (_scsih_check_access_status(ioc, sas_address, handle,
  3748. sas_device_pg0.AccessStatus))
  3749. return -1;
  3750. /* check if this is end device */
  3751. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  3752. if (!(_scsih_is_end_device(device_info))) {
  3753. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3754. ioc->name, __FILE__, __LINE__, __func__);
  3755. return -1;
  3756. }
  3757. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  3758. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  3759. sas_address);
  3760. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  3761. if (sas_device)
  3762. return 0;
  3763. sas_device = kzalloc(sizeof(struct _sas_device),
  3764. GFP_KERNEL);
  3765. if (!sas_device) {
  3766. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3767. ioc->name, __FILE__, __LINE__, __func__);
  3768. return -1;
  3769. }
  3770. sas_device->handle = handle;
  3771. if (_scsih_get_sas_address(ioc, le16_to_cpu
  3772. (sas_device_pg0.ParentDevHandle),
  3773. &sas_device->sas_address_parent) != 0)
  3774. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3775. ioc->name, __FILE__, __LINE__, __func__);
  3776. sas_device->enclosure_handle =
  3777. le16_to_cpu(sas_device_pg0.EnclosureHandle);
  3778. sas_device->slot =
  3779. le16_to_cpu(sas_device_pg0.Slot);
  3780. sas_device->device_info = device_info;
  3781. sas_device->sas_address = sas_address;
  3782. sas_device->hidden_raid_component = is_pd;
  3783. /* get enclosure_logical_id */
  3784. if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
  3785. ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
  3786. sas_device->enclosure_handle)))
  3787. sas_device->enclosure_logical_id =
  3788. le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
  3789. /* get device name */
  3790. sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
  3791. if (ioc->wait_for_port_enable_to_complete)
  3792. _scsih_sas_device_init_add(ioc, sas_device);
  3793. else
  3794. _scsih_sas_device_add(ioc, sas_device);
  3795. return 0;
  3796. }
  3797. /**
  3798. * _scsih_remove_pd_device - removing sas device pd object
  3799. * @ioc: per adapter object
  3800. * @sas_device_delete: the sas_device object
  3801. *
  3802. * For hidden raid components, we do driver-fw handshake from
  3803. * hotplug work threads.
  3804. * Return nothing.
  3805. */
  3806. static void
  3807. _scsih_remove_pd_device(struct MPT2SAS_ADAPTER *ioc, struct _sas_device
  3808. sas_device)
  3809. {
  3810. Mpi2SasIoUnitControlReply_t mpi_reply;
  3811. Mpi2SasIoUnitControlRequest_t mpi_request;
  3812. u16 vol_handle, handle;
  3813. handle = sas_device.handle;
  3814. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle(0x%04x),"
  3815. " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
  3816. (unsigned long long) sas_device.sas_address));
  3817. vol_handle = sas_device.volume_handle;
  3818. if (!vol_handle)
  3819. return;
  3820. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
  3821. "handle(0x%04x)\n", ioc->name, vol_handle));
  3822. mpt2sas_scsih_issue_tm(ioc, vol_handle, 0, 0, 0,
  3823. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30, NULL);
  3824. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
  3825. "done: handle(0x%04x)\n", ioc->name, vol_handle));
  3826. if (ioc->shost_recovery)
  3827. return;
  3828. /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
  3829. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
  3830. "(0x%04x)\n", ioc->name, handle));
  3831. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
  3832. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  3833. mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
  3834. mpi_request.DevHandle = cpu_to_le16(handle);
  3835. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
  3836. &mpi_request)) != 0)
  3837. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  3838. ioc->name, __FILE__, __LINE__, __func__);
  3839. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
  3840. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  3841. le16_to_cpu(mpi_reply.IOCStatus),
  3842. le32_to_cpu(mpi_reply.IOCLogInfo)));
  3843. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle(0x%04x),"
  3844. " sas_addr(0x%016llx)\n", ioc->name, __func__, handle,
  3845. (unsigned long long) sas_device.sas_address));
  3846. }
  3847. /**
  3848. * _scsih_remove_device - removing sas device object
  3849. * @ioc: per adapter object
  3850. * @sas_device_delete: the sas_device object
  3851. *
  3852. * Return nothing.
  3853. */
  3854. static void
  3855. _scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
  3856. struct _sas_device *sas_device)
  3857. {
  3858. struct _sas_device sas_device_backup;
  3859. struct MPT2SAS_TARGET *sas_target_priv_data;
  3860. if (!sas_device)
  3861. return;
  3862. memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
  3863. _scsih_sas_device_remove(ioc, sas_device);
  3864. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
  3865. "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  3866. sas_device_backup.handle, (unsigned long long)
  3867. sas_device_backup.sas_address));
  3868. if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
  3869. sas_target_priv_data = sas_device_backup.starget->hostdata;
  3870. sas_target_priv_data->deleted = 1;
  3871. }
  3872. if (sas_device_backup.hidden_raid_component)
  3873. _scsih_remove_pd_device(ioc, sas_device_backup);
  3874. _scsih_ublock_io_device(ioc, sas_device_backup.handle);
  3875. mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
  3876. sas_device_backup.sas_address_parent);
  3877. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
  3878. "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
  3879. (unsigned long long) sas_device_backup.sas_address);
  3880. dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
  3881. "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
  3882. sas_device_backup.handle, (unsigned long long)
  3883. sas_device_backup.sas_address));
  3884. }
  3885. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3886. /**
  3887. * _scsih_sas_topology_change_event_debug - debug for topology event
  3888. * @ioc: per adapter object
  3889. * @event_data: event data payload
  3890. * Context: user.
  3891. */
  3892. static void
  3893. _scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  3894. Mpi2EventDataSasTopologyChangeList_t *event_data)
  3895. {
  3896. int i;
  3897. u16 handle;
  3898. u16 reason_code;
  3899. u8 phy_number;
  3900. char *status_str = NULL;
  3901. u8 link_rate, prev_link_rate;
  3902. switch (event_data->ExpStatus) {
  3903. case MPI2_EVENT_SAS_TOPO_ES_ADDED:
  3904. status_str = "add";
  3905. break;
  3906. case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
  3907. status_str = "remove";
  3908. break;
  3909. case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
  3910. case 0:
  3911. status_str = "responding";
  3912. break;
  3913. case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
  3914. status_str = "remove delay";
  3915. break;
  3916. default:
  3917. status_str = "unknown status";
  3918. break;
  3919. }
  3920. printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
  3921. ioc->name, status_str);
  3922. printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
  3923. "start_phy(%02d), count(%d)\n",
  3924. le16_to_cpu(event_data->ExpanderDevHandle),
  3925. le16_to_cpu(event_data->EnclosureHandle),
  3926. event_data->StartPhyNum, event_data->NumEntries);
  3927. for (i = 0; i < event_data->NumEntries; i++) {
  3928. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  3929. if (!handle)
  3930. continue;
  3931. phy_number = event_data->StartPhyNum + i;
  3932. reason_code = event_data->PHY[i].PhyStatus &
  3933. MPI2_EVENT_SAS_TOPO_RC_MASK;
  3934. switch (reason_code) {
  3935. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  3936. status_str = "target add";
  3937. break;
  3938. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  3939. status_str = "target remove";
  3940. break;
  3941. case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
  3942. status_str = "delay target remove";
  3943. break;
  3944. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  3945. status_str = "link rate change";
  3946. break;
  3947. case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
  3948. status_str = "target responding";
  3949. break;
  3950. default:
  3951. status_str = "unknown";
  3952. break;
  3953. }
  3954. link_rate = event_data->PHY[i].LinkRate >> 4;
  3955. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  3956. printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x): %s:"
  3957. " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
  3958. handle, status_str, link_rate, prev_link_rate);
  3959. }
  3960. }
  3961. #endif
  3962. /**
  3963. * _scsih_sas_topology_change_event - handle topology changes
  3964. * @ioc: per adapter object
  3965. * @fw_event: The fw_event_work object
  3966. * Context: user.
  3967. *
  3968. */
  3969. static void
  3970. _scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
  3971. struct fw_event_work *fw_event)
  3972. {
  3973. int i;
  3974. u16 parent_handle, handle;
  3975. u16 reason_code;
  3976. u8 phy_number;
  3977. struct _sas_node *sas_expander;
  3978. struct _sas_device *sas_device;
  3979. u64 sas_address;
  3980. unsigned long flags;
  3981. u8 link_rate, prev_link_rate;
  3982. Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
  3983. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  3984. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  3985. _scsih_sas_topology_change_event_debug(ioc, event_data);
  3986. #endif
  3987. if (ioc->shost_recovery || ioc->remove_host)
  3988. return;
  3989. if (!ioc->sas_hba.num_phys)
  3990. _scsih_sas_host_add(ioc);
  3991. else
  3992. _scsih_sas_host_refresh(ioc);
  3993. if (fw_event->ignore) {
  3994. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
  3995. "event\n", ioc->name));
  3996. return;
  3997. }
  3998. parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
  3999. /* handle expander add */
  4000. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
  4001. if (_scsih_expander_add(ioc, parent_handle) != 0)
  4002. return;
  4003. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  4004. sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
  4005. parent_handle);
  4006. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  4007. if (sas_expander)
  4008. sas_address = sas_expander->sas_address;
  4009. else if (parent_handle < ioc->sas_hba.num_phys)
  4010. sas_address = ioc->sas_hba.sas_address;
  4011. else
  4012. return;
  4013. /* handle siblings events */
  4014. for (i = 0; i < event_data->NumEntries; i++) {
  4015. if (fw_event->ignore) {
  4016. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
  4017. "expander event\n", ioc->name));
  4018. return;
  4019. }
  4020. if (ioc->shost_recovery || ioc->remove_host)
  4021. return;
  4022. phy_number = event_data->StartPhyNum + i;
  4023. reason_code = event_data->PHY[i].PhyStatus &
  4024. MPI2_EVENT_SAS_TOPO_RC_MASK;
  4025. if ((event_data->PHY[i].PhyStatus &
  4026. MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
  4027. MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
  4028. continue;
  4029. handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
  4030. if (!handle)
  4031. continue;
  4032. link_rate = event_data->PHY[i].LinkRate >> 4;
  4033. prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
  4034. switch (reason_code) {
  4035. case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
  4036. if (link_rate == prev_link_rate)
  4037. break;
  4038. mpt2sas_transport_update_links(ioc, sas_address,
  4039. handle, phy_number, link_rate);
  4040. if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
  4041. break;
  4042. _scsih_check_device(ioc, handle);
  4043. break;
  4044. case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
  4045. mpt2sas_transport_update_links(ioc, sas_address,
  4046. handle, phy_number, link_rate);
  4047. _scsih_add_device(ioc, handle, phy_number, 0);
  4048. break;
  4049. case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
  4050. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4051. sas_device = _scsih_sas_device_find_by_handle(ioc,
  4052. handle);
  4053. if (!sas_device) {
  4054. spin_unlock_irqrestore(&ioc->sas_device_lock,
  4055. flags);
  4056. break;
  4057. }
  4058. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4059. _scsih_remove_device(ioc, sas_device);
  4060. break;
  4061. }
  4062. }
  4063. /* handle expander removal */
  4064. if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
  4065. sas_expander)
  4066. _scsih_expander_remove(ioc, sas_address);
  4067. }
  4068. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4069. /**
  4070. * _scsih_sas_device_status_change_event_debug - debug for device event
  4071. * @event_data: event data payload
  4072. * Context: user.
  4073. *
  4074. * Return nothing.
  4075. */
  4076. static void
  4077. _scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4078. Mpi2EventDataSasDeviceStatusChange_t *event_data)
  4079. {
  4080. char *reason_str = NULL;
  4081. switch (event_data->ReasonCode) {
  4082. case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  4083. reason_str = "smart data";
  4084. break;
  4085. case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  4086. reason_str = "unsupported device discovered";
  4087. break;
  4088. case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  4089. reason_str = "internal device reset";
  4090. break;
  4091. case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  4092. reason_str = "internal task abort";
  4093. break;
  4094. case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  4095. reason_str = "internal task abort set";
  4096. break;
  4097. case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  4098. reason_str = "internal clear task set";
  4099. break;
  4100. case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  4101. reason_str = "internal query task";
  4102. break;
  4103. case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
  4104. reason_str = "sata init failure";
  4105. break;
  4106. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
  4107. reason_str = "internal device reset complete";
  4108. break;
  4109. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
  4110. reason_str = "internal task abort complete";
  4111. break;
  4112. case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
  4113. reason_str = "internal async notification";
  4114. break;
  4115. case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
  4116. reason_str = "expander reduced functionality";
  4117. break;
  4118. case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
  4119. reason_str = "expander reduced functionality complete";
  4120. break;
  4121. default:
  4122. reason_str = "unknown reason";
  4123. break;
  4124. }
  4125. printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
  4126. "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
  4127. reason_str, le16_to_cpu(event_data->DevHandle),
  4128. (unsigned long long)le64_to_cpu(event_data->SASAddress));
  4129. if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
  4130. printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
  4131. event_data->ASC, event_data->ASCQ);
  4132. printk(KERN_INFO "\n");
  4133. }
  4134. #endif
  4135. /**
  4136. * _scsih_sas_device_status_change_event - handle device status change
  4137. * @ioc: per adapter object
  4138. * @fw_event: The fw_event_work object
  4139. * Context: user.
  4140. *
  4141. * Return nothing.
  4142. */
  4143. static void
  4144. _scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  4145. struct fw_event_work *fw_event)
  4146. {
  4147. struct MPT2SAS_TARGET *target_priv_data;
  4148. struct _sas_device *sas_device;
  4149. __le64 sas_address;
  4150. unsigned long flags;
  4151. Mpi2EventDataSasDeviceStatusChange_t *event_data =
  4152. fw_event->event_data;
  4153. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4154. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4155. _scsih_sas_device_status_change_event_debug(ioc,
  4156. event_data);
  4157. #endif
  4158. if (event_data->ReasonCode !=
  4159. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  4160. event_data->ReasonCode !=
  4161. MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
  4162. return;
  4163. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4164. sas_address = le64_to_cpu(event_data->SASAddress);
  4165. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  4166. sas_address);
  4167. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4168. if (!sas_device || !sas_device->starget)
  4169. return;
  4170. target_priv_data = sas_device->starget->hostdata;
  4171. if (!target_priv_data)
  4172. return;
  4173. if (event_data->ReasonCode ==
  4174. MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
  4175. target_priv_data->tm_busy = 1;
  4176. else
  4177. target_priv_data->tm_busy = 0;
  4178. }
  4179. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4180. /**
  4181. * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
  4182. * @ioc: per adapter object
  4183. * @event_data: event data payload
  4184. * Context: user.
  4185. *
  4186. * Return nothing.
  4187. */
  4188. static void
  4189. _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4190. Mpi2EventDataSasEnclDevStatusChange_t *event_data)
  4191. {
  4192. char *reason_str = NULL;
  4193. switch (event_data->ReasonCode) {
  4194. case MPI2_EVENT_SAS_ENCL_RC_ADDED:
  4195. reason_str = "enclosure add";
  4196. break;
  4197. case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
  4198. reason_str = "enclosure remove";
  4199. break;
  4200. default:
  4201. reason_str = "unknown reason";
  4202. break;
  4203. }
  4204. printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
  4205. "\thandle(0x%04x), enclosure logical id(0x%016llx)"
  4206. " number slots(%d)\n", ioc->name, reason_str,
  4207. le16_to_cpu(event_data->EnclosureHandle),
  4208. (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
  4209. le16_to_cpu(event_data->StartSlot));
  4210. }
  4211. #endif
  4212. /**
  4213. * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
  4214. * @ioc: per adapter object
  4215. * @fw_event: The fw_event_work object
  4216. * Context: user.
  4217. *
  4218. * Return nothing.
  4219. */
  4220. static void
  4221. _scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
  4222. struct fw_event_work *fw_event)
  4223. {
  4224. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4225. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4226. _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
  4227. fw_event->event_data);
  4228. #endif
  4229. }
  4230. /**
  4231. * _scsih_sas_broadcast_primative_event - handle broadcast events
  4232. * @ioc: per adapter object
  4233. * @fw_event: The fw_event_work object
  4234. * Context: user.
  4235. *
  4236. * Return nothing.
  4237. */
  4238. static void
  4239. _scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
  4240. struct fw_event_work *fw_event)
  4241. {
  4242. struct scsi_cmnd *scmd;
  4243. u16 smid, handle;
  4244. u32 lun;
  4245. struct MPT2SAS_DEVICE *sas_device_priv_data;
  4246. u32 termination_count;
  4247. u32 query_count;
  4248. Mpi2SCSITaskManagementReply_t *mpi_reply;
  4249. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4250. Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
  4251. #endif
  4252. u16 ioc_status;
  4253. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
  4254. "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
  4255. event_data->PortWidth));
  4256. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
  4257. __func__));
  4258. termination_count = 0;
  4259. query_count = 0;
  4260. mpi_reply = ioc->tm_cmds.reply;
  4261. for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
  4262. scmd = _scsih_scsi_lookup_get(ioc, smid);
  4263. if (!scmd)
  4264. continue;
  4265. sas_device_priv_data = scmd->device->hostdata;
  4266. if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
  4267. continue;
  4268. /* skip hidden raid components */
  4269. if (sas_device_priv_data->sas_target->flags &
  4270. MPT_TARGET_FLAGS_RAID_COMPONENT)
  4271. continue;
  4272. /* skip volumes */
  4273. if (sas_device_priv_data->sas_target->flags &
  4274. MPT_TARGET_FLAGS_VOLUME)
  4275. continue;
  4276. handle = sas_device_priv_data->sas_target->handle;
  4277. lun = sas_device_priv_data->lun;
  4278. query_count++;
  4279. mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
  4280. MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
  4281. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  4282. ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
  4283. & MPI2_IOCSTATUS_MASK;
  4284. if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
  4285. (mpi_reply->ResponseCode ==
  4286. MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  4287. mpi_reply->ResponseCode ==
  4288. MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  4289. continue;
  4290. mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
  4291. MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30, NULL);
  4292. termination_count += le32_to_cpu(mpi_reply->TerminationCount);
  4293. }
  4294. ioc->broadcast_aen_busy = 0;
  4295. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
  4296. "%s - exit, query_count = %d termination_count = %d\n",
  4297. ioc->name, __func__, query_count, termination_count));
  4298. }
  4299. /**
  4300. * _scsih_sas_discovery_event - handle discovery events
  4301. * @ioc: per adapter object
  4302. * @fw_event: The fw_event_work object
  4303. * Context: user.
  4304. *
  4305. * Return nothing.
  4306. */
  4307. static void
  4308. _scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
  4309. struct fw_event_work *fw_event)
  4310. {
  4311. Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
  4312. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4313. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
  4314. printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
  4315. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  4316. "start" : "stop");
  4317. if (event_data->DiscoveryStatus)
  4318. printk("discovery_status(0x%08x)",
  4319. le32_to_cpu(event_data->DiscoveryStatus));
  4320. printk("\n");
  4321. }
  4322. #endif
  4323. if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
  4324. !ioc->sas_hba.num_phys)
  4325. _scsih_sas_host_add(ioc);
  4326. }
  4327. /**
  4328. * _scsih_reprobe_lun - reprobing lun
  4329. * @sdev: scsi device struct
  4330. * @no_uld_attach: sdev->no_uld_attach flag setting
  4331. *
  4332. **/
  4333. static void
  4334. _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
  4335. {
  4336. int rc;
  4337. sdev->no_uld_attach = no_uld_attach ? 1 : 0;
  4338. sdev_printk(KERN_INFO, sdev, "%s raid component\n",
  4339. sdev->no_uld_attach ? "hidding" : "exposing");
  4340. rc = scsi_device_reprobe(sdev);
  4341. }
  4342. /**
  4343. * _scsih_reprobe_target - reprobing target
  4344. * @starget: scsi target struct
  4345. * @no_uld_attach: sdev->no_uld_attach flag setting
  4346. *
  4347. * Note: no_uld_attach flag determines whether the disk device is attached
  4348. * to block layer. A value of `1` means to not attach.
  4349. **/
  4350. static void
  4351. _scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
  4352. {
  4353. struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
  4354. if (no_uld_attach)
  4355. sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  4356. else
  4357. sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  4358. starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
  4359. _scsih_reprobe_lun);
  4360. }
  4361. /**
  4362. * _scsih_sas_volume_add - add new volume
  4363. * @ioc: per adapter object
  4364. * @element: IR config element data
  4365. * Context: user.
  4366. *
  4367. * Return nothing.
  4368. */
  4369. static void
  4370. _scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
  4371. Mpi2EventIrConfigElement_t *element)
  4372. {
  4373. struct _raid_device *raid_device;
  4374. unsigned long flags;
  4375. u64 wwid;
  4376. u16 handle = le16_to_cpu(element->VolDevHandle);
  4377. int rc;
  4378. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  4379. if (!wwid) {
  4380. printk(MPT2SAS_ERR_FMT
  4381. "failure at %s:%d/%s()!\n", ioc->name,
  4382. __FILE__, __LINE__, __func__);
  4383. return;
  4384. }
  4385. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4386. raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
  4387. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4388. if (raid_device)
  4389. return;
  4390. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  4391. if (!raid_device) {
  4392. printk(MPT2SAS_ERR_FMT
  4393. "failure at %s:%d/%s()!\n", ioc->name,
  4394. __FILE__, __LINE__, __func__);
  4395. return;
  4396. }
  4397. raid_device->id = ioc->sas_id++;
  4398. raid_device->channel = RAID_CHANNEL;
  4399. raid_device->handle = handle;
  4400. raid_device->wwid = wwid;
  4401. _scsih_raid_device_add(ioc, raid_device);
  4402. if (!ioc->wait_for_port_enable_to_complete) {
  4403. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4404. raid_device->id, 0);
  4405. if (rc)
  4406. _scsih_raid_device_remove(ioc, raid_device);
  4407. } else
  4408. _scsih_determine_boot_device(ioc, raid_device, 1);
  4409. }
  4410. /**
  4411. * _scsih_sas_volume_delete - delete volume
  4412. * @ioc: per adapter object
  4413. * @element: IR config element data
  4414. * Context: user.
  4415. *
  4416. * Return nothing.
  4417. */
  4418. static void
  4419. _scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
  4420. Mpi2EventIrConfigElement_t *element)
  4421. {
  4422. struct _raid_device *raid_device;
  4423. u16 handle = le16_to_cpu(element->VolDevHandle);
  4424. unsigned long flags;
  4425. struct MPT2SAS_TARGET *sas_target_priv_data;
  4426. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4427. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  4428. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4429. if (!raid_device)
  4430. return;
  4431. if (raid_device->starget) {
  4432. sas_target_priv_data = raid_device->starget->hostdata;
  4433. sas_target_priv_data->deleted = 1;
  4434. scsi_remove_target(&raid_device->starget->dev);
  4435. }
  4436. _scsih_raid_device_remove(ioc, raid_device);
  4437. }
  4438. /**
  4439. * _scsih_sas_pd_expose - expose pd component to /dev/sdX
  4440. * @ioc: per adapter object
  4441. * @element: IR config element data
  4442. * Context: user.
  4443. *
  4444. * Return nothing.
  4445. */
  4446. static void
  4447. _scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
  4448. Mpi2EventIrConfigElement_t *element)
  4449. {
  4450. struct _sas_device *sas_device;
  4451. unsigned long flags;
  4452. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4453. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4454. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4455. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4456. if (!sas_device)
  4457. return;
  4458. /* exposing raid component */
  4459. sas_device->volume_handle = 0;
  4460. sas_device->volume_wwid = 0;
  4461. sas_device->hidden_raid_component = 0;
  4462. _scsih_reprobe_target(sas_device->starget, 0);
  4463. }
  4464. /**
  4465. * _scsih_sas_pd_hide - hide pd component from /dev/sdX
  4466. * @ioc: per adapter object
  4467. * @element: IR config element data
  4468. * Context: user.
  4469. *
  4470. * Return nothing.
  4471. */
  4472. static void
  4473. _scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
  4474. Mpi2EventIrConfigElement_t *element)
  4475. {
  4476. struct _sas_device *sas_device;
  4477. unsigned long flags;
  4478. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4479. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4480. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4481. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4482. if (!sas_device)
  4483. return;
  4484. /* hiding raid component */
  4485. mpt2sas_config_get_volume_handle(ioc, handle,
  4486. &sas_device->volume_handle);
  4487. mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
  4488. &sas_device->volume_wwid);
  4489. sas_device->hidden_raid_component = 1;
  4490. _scsih_reprobe_target(sas_device->starget, 1);
  4491. }
  4492. /**
  4493. * _scsih_sas_pd_delete - delete pd component
  4494. * @ioc: per adapter object
  4495. * @element: IR config element data
  4496. * Context: user.
  4497. *
  4498. * Return nothing.
  4499. */
  4500. static void
  4501. _scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
  4502. Mpi2EventIrConfigElement_t *element)
  4503. {
  4504. struct _sas_device *sas_device;
  4505. unsigned long flags;
  4506. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4507. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4508. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4509. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4510. if (!sas_device)
  4511. return;
  4512. _scsih_remove_device(ioc, sas_device);
  4513. }
  4514. /**
  4515. * _scsih_sas_pd_add - remove pd component
  4516. * @ioc: per adapter object
  4517. * @element: IR config element data
  4518. * Context: user.
  4519. *
  4520. * Return nothing.
  4521. */
  4522. static void
  4523. _scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
  4524. Mpi2EventIrConfigElement_t *element)
  4525. {
  4526. struct _sas_device *sas_device;
  4527. unsigned long flags;
  4528. u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
  4529. Mpi2ConfigReply_t mpi_reply;
  4530. Mpi2SasDevicePage0_t sas_device_pg0;
  4531. u32 ioc_status;
  4532. u64 sas_address;
  4533. u16 parent_handle;
  4534. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4535. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4536. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4537. if (sas_device) {
  4538. sas_device->hidden_raid_component = 1;
  4539. return;
  4540. }
  4541. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  4542. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  4543. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4544. ioc->name, __FILE__, __LINE__, __func__);
  4545. return;
  4546. }
  4547. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4548. MPI2_IOCSTATUS_MASK;
  4549. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4550. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4551. ioc->name, __FILE__, __LINE__, __func__);
  4552. return;
  4553. }
  4554. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  4555. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  4556. mpt2sas_transport_update_links(ioc, sas_address, handle,
  4557. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  4558. _scsih_add_device(ioc, handle, 0, 1);
  4559. }
  4560. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4561. /**
  4562. * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
  4563. * @ioc: per adapter object
  4564. * @event_data: event data payload
  4565. * Context: user.
  4566. *
  4567. * Return nothing.
  4568. */
  4569. static void
  4570. _scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4571. Mpi2EventDataIrConfigChangeList_t *event_data)
  4572. {
  4573. Mpi2EventIrConfigElement_t *element;
  4574. u8 element_type;
  4575. int i;
  4576. char *reason_str = NULL, *element_str = NULL;
  4577. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4578. printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
  4579. ioc->name, (le32_to_cpu(event_data->Flags) &
  4580. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
  4581. "foreign" : "native", event_data->NumElements);
  4582. for (i = 0; i < event_data->NumElements; i++, element++) {
  4583. switch (element->ReasonCode) {
  4584. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4585. reason_str = "add";
  4586. break;
  4587. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4588. reason_str = "remove";
  4589. break;
  4590. case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
  4591. reason_str = "no change";
  4592. break;
  4593. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4594. reason_str = "hide";
  4595. break;
  4596. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4597. reason_str = "unhide";
  4598. break;
  4599. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4600. reason_str = "volume_created";
  4601. break;
  4602. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4603. reason_str = "volume_deleted";
  4604. break;
  4605. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4606. reason_str = "pd_created";
  4607. break;
  4608. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4609. reason_str = "pd_deleted";
  4610. break;
  4611. default:
  4612. reason_str = "unknown reason";
  4613. break;
  4614. }
  4615. element_type = le16_to_cpu(element->ElementFlags) &
  4616. MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
  4617. switch (element_type) {
  4618. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
  4619. element_str = "volume";
  4620. break;
  4621. case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
  4622. element_str = "phys disk";
  4623. break;
  4624. case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
  4625. element_str = "hot spare";
  4626. break;
  4627. default:
  4628. element_str = "unknown element";
  4629. break;
  4630. }
  4631. printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
  4632. "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
  4633. reason_str, le16_to_cpu(element->VolDevHandle),
  4634. le16_to_cpu(element->PhysDiskDevHandle),
  4635. element->PhysDiskNum);
  4636. }
  4637. }
  4638. #endif
  4639. /**
  4640. * _scsih_sas_ir_config_change_event - handle ir configuration change events
  4641. * @ioc: per adapter object
  4642. * @fw_event: The fw_event_work object
  4643. * Context: user.
  4644. *
  4645. * Return nothing.
  4646. */
  4647. static void
  4648. _scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
  4649. struct fw_event_work *fw_event)
  4650. {
  4651. Mpi2EventIrConfigElement_t *element;
  4652. int i;
  4653. u8 foreign_config;
  4654. Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
  4655. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4656. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4657. _scsih_sas_ir_config_change_event_debug(ioc, event_data);
  4658. #endif
  4659. foreign_config = (le32_to_cpu(event_data->Flags) &
  4660. MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
  4661. element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
  4662. for (i = 0; i < event_data->NumElements; i++, element++) {
  4663. switch (element->ReasonCode) {
  4664. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
  4665. case MPI2_EVENT_IR_CHANGE_RC_ADDED:
  4666. if (!foreign_config)
  4667. _scsih_sas_volume_add(ioc, element);
  4668. break;
  4669. case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
  4670. case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
  4671. if (!foreign_config)
  4672. _scsih_sas_volume_delete(ioc, element);
  4673. break;
  4674. case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
  4675. _scsih_sas_pd_hide(ioc, element);
  4676. break;
  4677. case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
  4678. _scsih_sas_pd_expose(ioc, element);
  4679. break;
  4680. case MPI2_EVENT_IR_CHANGE_RC_HIDE:
  4681. _scsih_sas_pd_add(ioc, element);
  4682. break;
  4683. case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
  4684. _scsih_sas_pd_delete(ioc, element);
  4685. break;
  4686. }
  4687. }
  4688. }
  4689. /**
  4690. * _scsih_sas_ir_volume_event - IR volume event
  4691. * @ioc: per adapter object
  4692. * @fw_event: The fw_event_work object
  4693. * Context: user.
  4694. *
  4695. * Return nothing.
  4696. */
  4697. static void
  4698. _scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
  4699. struct fw_event_work *fw_event)
  4700. {
  4701. u64 wwid;
  4702. unsigned long flags;
  4703. struct _raid_device *raid_device;
  4704. u16 handle;
  4705. u32 state;
  4706. int rc;
  4707. struct MPT2SAS_TARGET *sas_target_priv_data;
  4708. Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
  4709. if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
  4710. return;
  4711. handle = le16_to_cpu(event_data->VolDevHandle);
  4712. state = le32_to_cpu(event_data->NewValue);
  4713. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4714. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4715. le32_to_cpu(event_data->PreviousValue), state));
  4716. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4717. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  4718. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4719. switch (state) {
  4720. case MPI2_RAID_VOL_STATE_MISSING:
  4721. case MPI2_RAID_VOL_STATE_FAILED:
  4722. if (!raid_device)
  4723. break;
  4724. if (raid_device->starget) {
  4725. sas_target_priv_data = raid_device->starget->hostdata;
  4726. sas_target_priv_data->deleted = 1;
  4727. scsi_remove_target(&raid_device->starget->dev);
  4728. }
  4729. _scsih_raid_device_remove(ioc, raid_device);
  4730. break;
  4731. case MPI2_RAID_VOL_STATE_ONLINE:
  4732. case MPI2_RAID_VOL_STATE_DEGRADED:
  4733. case MPI2_RAID_VOL_STATE_OPTIMAL:
  4734. if (raid_device)
  4735. break;
  4736. mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
  4737. if (!wwid) {
  4738. printk(MPT2SAS_ERR_FMT
  4739. "failure at %s:%d/%s()!\n", ioc->name,
  4740. __FILE__, __LINE__, __func__);
  4741. break;
  4742. }
  4743. raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
  4744. if (!raid_device) {
  4745. printk(MPT2SAS_ERR_FMT
  4746. "failure at %s:%d/%s()!\n", ioc->name,
  4747. __FILE__, __LINE__, __func__);
  4748. break;
  4749. }
  4750. raid_device->id = ioc->sas_id++;
  4751. raid_device->channel = RAID_CHANNEL;
  4752. raid_device->handle = handle;
  4753. raid_device->wwid = wwid;
  4754. _scsih_raid_device_add(ioc, raid_device);
  4755. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  4756. raid_device->id, 0);
  4757. if (rc)
  4758. _scsih_raid_device_remove(ioc, raid_device);
  4759. break;
  4760. case MPI2_RAID_VOL_STATE_INITIALIZING:
  4761. default:
  4762. break;
  4763. }
  4764. }
  4765. /**
  4766. * _scsih_sas_ir_physical_disk_event - PD event
  4767. * @ioc: per adapter object
  4768. * @fw_event: The fw_event_work object
  4769. * Context: user.
  4770. *
  4771. * Return nothing.
  4772. */
  4773. static void
  4774. _scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
  4775. struct fw_event_work *fw_event)
  4776. {
  4777. u16 handle, parent_handle;
  4778. u32 state;
  4779. struct _sas_device *sas_device;
  4780. unsigned long flags;
  4781. Mpi2ConfigReply_t mpi_reply;
  4782. Mpi2SasDevicePage0_t sas_device_pg0;
  4783. u32 ioc_status;
  4784. Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
  4785. u64 sas_address;
  4786. if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
  4787. return;
  4788. handle = le16_to_cpu(event_data->PhysDiskDevHandle);
  4789. state = le32_to_cpu(event_data->NewValue);
  4790. dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
  4791. "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
  4792. le32_to_cpu(event_data->PreviousValue), state));
  4793. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4794. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4795. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4796. switch (state) {
  4797. case MPI2_RAID_PD_STATE_ONLINE:
  4798. case MPI2_RAID_PD_STATE_DEGRADED:
  4799. case MPI2_RAID_PD_STATE_REBUILDING:
  4800. case MPI2_RAID_PD_STATE_OPTIMAL:
  4801. if (sas_device) {
  4802. sas_device->hidden_raid_component = 1;
  4803. return;
  4804. }
  4805. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  4806. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
  4807. handle))) {
  4808. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4809. ioc->name, __FILE__, __LINE__, __func__);
  4810. return;
  4811. }
  4812. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  4813. MPI2_IOCSTATUS_MASK;
  4814. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  4815. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  4816. ioc->name, __FILE__, __LINE__, __func__);
  4817. return;
  4818. }
  4819. parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
  4820. if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
  4821. mpt2sas_transport_update_links(ioc, sas_address, handle,
  4822. sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
  4823. _scsih_add_device(ioc, handle, 0, 1);
  4824. break;
  4825. case MPI2_RAID_PD_STATE_OFFLINE:
  4826. case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
  4827. case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
  4828. case MPI2_RAID_PD_STATE_HOT_SPARE:
  4829. default:
  4830. break;
  4831. }
  4832. }
  4833. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4834. /**
  4835. * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
  4836. * @ioc: per adapter object
  4837. * @event_data: event data payload
  4838. * Context: user.
  4839. *
  4840. * Return nothing.
  4841. */
  4842. static void
  4843. _scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
  4844. Mpi2EventDataIrOperationStatus_t *event_data)
  4845. {
  4846. char *reason_str = NULL;
  4847. switch (event_data->RAIDOperation) {
  4848. case MPI2_EVENT_IR_RAIDOP_RESYNC:
  4849. reason_str = "resync";
  4850. break;
  4851. case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
  4852. reason_str = "online capacity expansion";
  4853. break;
  4854. case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
  4855. reason_str = "consistency check";
  4856. break;
  4857. case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
  4858. reason_str = "background init";
  4859. break;
  4860. case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
  4861. reason_str = "make data consistent";
  4862. break;
  4863. }
  4864. if (!reason_str)
  4865. return;
  4866. printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
  4867. "\thandle(0x%04x), percent complete(%d)\n",
  4868. ioc->name, reason_str,
  4869. le16_to_cpu(event_data->VolDevHandle),
  4870. event_data->PercentComplete);
  4871. }
  4872. #endif
  4873. /**
  4874. * _scsih_sas_ir_operation_status_event - handle RAID operation events
  4875. * @ioc: per adapter object
  4876. * @fw_event: The fw_event_work object
  4877. * Context: user.
  4878. *
  4879. * Return nothing.
  4880. */
  4881. static void
  4882. _scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
  4883. struct fw_event_work *fw_event)
  4884. {
  4885. Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
  4886. static struct _raid_device *raid_device;
  4887. unsigned long flags;
  4888. u16 handle;
  4889. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  4890. if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
  4891. _scsih_sas_ir_operation_status_event_debug(ioc,
  4892. event_data);
  4893. #endif
  4894. /* code added for raid transport support */
  4895. if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
  4896. handle = le16_to_cpu(event_data->VolDevHandle);
  4897. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4898. raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
  4899. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4900. if (!raid_device)
  4901. return;
  4902. if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
  4903. raid_device->percent_complete =
  4904. event_data->PercentComplete;
  4905. }
  4906. }
  4907. /**
  4908. * _scsih_task_set_full - handle task set full
  4909. * @ioc: per adapter object
  4910. * @fw_event: The fw_event_work object
  4911. * Context: user.
  4912. *
  4913. * Throttle back qdepth.
  4914. */
  4915. static void
  4916. _scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
  4917. *fw_event)
  4918. {
  4919. unsigned long flags;
  4920. struct _sas_device *sas_device;
  4921. static struct _raid_device *raid_device;
  4922. struct scsi_device *sdev;
  4923. int depth;
  4924. u16 current_depth;
  4925. u16 handle;
  4926. int id, channel;
  4927. u64 sas_address;
  4928. Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
  4929. current_depth = le16_to_cpu(event_data->CurrentDepth);
  4930. handle = le16_to_cpu(event_data->DevHandle);
  4931. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  4932. sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
  4933. if (!sas_device) {
  4934. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4935. return;
  4936. }
  4937. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  4938. id = sas_device->id;
  4939. channel = sas_device->channel;
  4940. sas_address = sas_device->sas_address;
  4941. /* if hidden raid component, then change to volume characteristics */
  4942. if (sas_device->hidden_raid_component && sas_device->volume_handle) {
  4943. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  4944. raid_device = _scsih_raid_device_find_by_handle(
  4945. ioc, sas_device->volume_handle);
  4946. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  4947. if (raid_device) {
  4948. id = raid_device->id;
  4949. channel = raid_device->channel;
  4950. handle = raid_device->handle;
  4951. sas_address = raid_device->wwid;
  4952. }
  4953. }
  4954. if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
  4955. starget_printk(KERN_DEBUG, sas_device->starget, "task set "
  4956. "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
  4957. handle, (unsigned long long)sas_address, current_depth);
  4958. shost_for_each_device(sdev, ioc->shost) {
  4959. if (sdev->id == id && sdev->channel == channel) {
  4960. if (current_depth > sdev->queue_depth) {
  4961. if (ioc->logging_level &
  4962. MPT_DEBUG_TASK_SET_FULL)
  4963. sdev_printk(KERN_INFO, sdev, "strange "
  4964. "observation, the queue depth is"
  4965. " (%d) meanwhile fw queue depth "
  4966. "is (%d)\n", sdev->queue_depth,
  4967. current_depth);
  4968. continue;
  4969. }
  4970. depth = scsi_track_queue_full(sdev,
  4971. current_depth - 1);
  4972. if (depth > 0)
  4973. sdev_printk(KERN_INFO, sdev, "Queue depth "
  4974. "reduced to (%d)\n", depth);
  4975. else if (depth < 0)
  4976. sdev_printk(KERN_INFO, sdev, "Tagged Command "
  4977. "Queueing is being disabled\n");
  4978. else if (depth == 0)
  4979. if (ioc->logging_level &
  4980. MPT_DEBUG_TASK_SET_FULL)
  4981. sdev_printk(KERN_INFO, sdev,
  4982. "Queue depth not changed yet\n");
  4983. }
  4984. }
  4985. }
  4986. /**
  4987. * _scsih_prep_device_scan - initialize parameters prior to device scan
  4988. * @ioc: per adapter object
  4989. *
  4990. * Set the deleted flag prior to device scan. If the device is found during
  4991. * the scan, then we clear the deleted flag.
  4992. */
  4993. static void
  4994. _scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
  4995. {
  4996. struct MPT2SAS_DEVICE *sas_device_priv_data;
  4997. struct scsi_device *sdev;
  4998. shost_for_each_device(sdev, ioc->shost) {
  4999. sas_device_priv_data = sdev->hostdata;
  5000. if (sas_device_priv_data && sas_device_priv_data->sas_target)
  5001. sas_device_priv_data->sas_target->deleted = 1;
  5002. }
  5003. }
  5004. /**
  5005. * _scsih_mark_responding_sas_device - mark a sas_devices as responding
  5006. * @ioc: per adapter object
  5007. * @sas_address: sas address
  5008. * @slot: enclosure slot id
  5009. * @handle: device handle
  5010. *
  5011. * After host reset, find out whether devices are still responding.
  5012. * Used in _scsi_remove_unresponsive_sas_devices.
  5013. *
  5014. * Return nothing.
  5015. */
  5016. static void
  5017. _scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  5018. u16 slot, u16 handle)
  5019. {
  5020. struct MPT2SAS_TARGET *sas_target_priv_data;
  5021. struct scsi_target *starget;
  5022. struct _sas_device *sas_device;
  5023. unsigned long flags;
  5024. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5025. list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
  5026. if (sas_device->sas_address == sas_address &&
  5027. sas_device->slot == slot && sas_device->starget) {
  5028. sas_device->responding = 1;
  5029. starget = sas_device->starget;
  5030. if (starget && starget->hostdata) {
  5031. sas_target_priv_data = starget->hostdata;
  5032. sas_target_priv_data->tm_busy = 0;
  5033. sas_target_priv_data->deleted = 0;
  5034. } else
  5035. sas_target_priv_data = NULL;
  5036. starget_printk(KERN_INFO, sas_device->starget,
  5037. "handle(0x%04x), sas_addr(0x%016llx), enclosure "
  5038. "logical id(0x%016llx), slot(%d)\n", handle,
  5039. (unsigned long long)sas_device->sas_address,
  5040. (unsigned long long)
  5041. sas_device->enclosure_logical_id,
  5042. sas_device->slot);
  5043. if (sas_device->handle == handle)
  5044. goto out;
  5045. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  5046. sas_device->handle);
  5047. sas_device->handle = handle;
  5048. if (sas_target_priv_data)
  5049. sas_target_priv_data->handle = handle;
  5050. goto out;
  5051. }
  5052. }
  5053. out:
  5054. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5055. }
  5056. /**
  5057. * _scsih_search_responding_sas_devices -
  5058. * @ioc: per adapter object
  5059. *
  5060. * After host reset, find out whether devices are still responding.
  5061. * If not remove.
  5062. *
  5063. * Return nothing.
  5064. */
  5065. static void
  5066. _scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  5067. {
  5068. Mpi2SasDevicePage0_t sas_device_pg0;
  5069. Mpi2ConfigReply_t mpi_reply;
  5070. u16 ioc_status;
  5071. __le64 sas_address;
  5072. u16 handle;
  5073. u32 device_info;
  5074. u16 slot;
  5075. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  5076. if (list_empty(&ioc->sas_device_list))
  5077. return;
  5078. handle = 0xFFFF;
  5079. while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
  5080. &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
  5081. handle))) {
  5082. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5083. MPI2_IOCSTATUS_MASK;
  5084. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  5085. break;
  5086. handle = le16_to_cpu(sas_device_pg0.DevHandle);
  5087. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  5088. if (!(_scsih_is_end_device(device_info)))
  5089. continue;
  5090. sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  5091. slot = le16_to_cpu(sas_device_pg0.Slot);
  5092. _scsih_mark_responding_sas_device(ioc, sas_address, slot,
  5093. handle);
  5094. }
  5095. }
  5096. /**
  5097. * _scsih_mark_responding_raid_device - mark a raid_device as responding
  5098. * @ioc: per adapter object
  5099. * @wwid: world wide identifier for raid volume
  5100. * @handle: device handle
  5101. *
  5102. * After host reset, find out whether devices are still responding.
  5103. * Used in _scsi_remove_unresponsive_raid_devices.
  5104. *
  5105. * Return nothing.
  5106. */
  5107. static void
  5108. _scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
  5109. u16 handle)
  5110. {
  5111. struct MPT2SAS_TARGET *sas_target_priv_data;
  5112. struct scsi_target *starget;
  5113. struct _raid_device *raid_device;
  5114. unsigned long flags;
  5115. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  5116. list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
  5117. if (raid_device->wwid == wwid && raid_device->starget) {
  5118. starget = raid_device->starget;
  5119. if (starget && starget->hostdata) {
  5120. sas_target_priv_data = starget->hostdata;
  5121. sas_target_priv_data->deleted = 0;
  5122. } else
  5123. sas_target_priv_data = NULL;
  5124. raid_device->responding = 1;
  5125. starget_printk(KERN_INFO, raid_device->starget,
  5126. "handle(0x%04x), wwid(0x%016llx)\n", handle,
  5127. (unsigned long long)raid_device->wwid);
  5128. if (raid_device->handle == handle)
  5129. goto out;
  5130. printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
  5131. raid_device->handle);
  5132. raid_device->handle = handle;
  5133. if (sas_target_priv_data)
  5134. sas_target_priv_data->handle = handle;
  5135. goto out;
  5136. }
  5137. }
  5138. out:
  5139. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  5140. }
  5141. /**
  5142. * _scsih_search_responding_raid_devices -
  5143. * @ioc: per adapter object
  5144. *
  5145. * After host reset, find out whether devices are still responding.
  5146. * If not remove.
  5147. *
  5148. * Return nothing.
  5149. */
  5150. static void
  5151. _scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
  5152. {
  5153. Mpi2RaidVolPage1_t volume_pg1;
  5154. Mpi2ConfigReply_t mpi_reply;
  5155. u16 ioc_status;
  5156. u16 handle;
  5157. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  5158. if (list_empty(&ioc->raid_device_list))
  5159. return;
  5160. handle = 0xFFFF;
  5161. while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  5162. &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  5163. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5164. MPI2_IOCSTATUS_MASK;
  5165. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  5166. break;
  5167. handle = le16_to_cpu(volume_pg1.DevHandle);
  5168. _scsih_mark_responding_raid_device(ioc,
  5169. le64_to_cpu(volume_pg1.WWID), handle);
  5170. }
  5171. }
  5172. /**
  5173. * _scsih_mark_responding_expander - mark a expander as responding
  5174. * @ioc: per adapter object
  5175. * @sas_address: sas address
  5176. * @handle:
  5177. *
  5178. * After host reset, find out whether devices are still responding.
  5179. * Used in _scsi_remove_unresponsive_expanders.
  5180. *
  5181. * Return nothing.
  5182. */
  5183. static void
  5184. _scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  5185. u16 handle)
  5186. {
  5187. struct _sas_node *sas_expander;
  5188. unsigned long flags;
  5189. int i;
  5190. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5191. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  5192. if (sas_expander->sas_address != sas_address)
  5193. continue;
  5194. sas_expander->responding = 1;
  5195. if (sas_expander->handle == handle)
  5196. goto out;
  5197. printk(KERN_INFO "\texpander(0x%016llx): handle changed"
  5198. " from(0x%04x) to (0x%04x)!!!\n",
  5199. (unsigned long long)sas_expander->sas_address,
  5200. sas_expander->handle, handle);
  5201. sas_expander->handle = handle;
  5202. for (i = 0 ; i < sas_expander->num_phys ; i++)
  5203. sas_expander->phy[i].handle = handle;
  5204. goto out;
  5205. }
  5206. out:
  5207. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5208. }
  5209. /**
  5210. * _scsih_search_responding_expanders -
  5211. * @ioc: per adapter object
  5212. *
  5213. * After host reset, find out whether devices are still responding.
  5214. * If not remove.
  5215. *
  5216. * Return nothing.
  5217. */
  5218. static void
  5219. _scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
  5220. {
  5221. Mpi2ExpanderPage0_t expander_pg0;
  5222. Mpi2ConfigReply_t mpi_reply;
  5223. u16 ioc_status;
  5224. __le64 sas_address;
  5225. u16 handle;
  5226. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
  5227. if (list_empty(&ioc->sas_expander_list))
  5228. return;
  5229. handle = 0xFFFF;
  5230. while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
  5231. MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
  5232. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  5233. MPI2_IOCSTATUS_MASK;
  5234. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  5235. break;
  5236. handle = le16_to_cpu(expander_pg0.DevHandle);
  5237. sas_address = le64_to_cpu(expander_pg0.SASAddress);
  5238. printk(KERN_INFO "\texpander present: handle(0x%04x), "
  5239. "sas_addr(0x%016llx)\n", handle,
  5240. (unsigned long long)sas_address);
  5241. _scsih_mark_responding_expander(ioc, sas_address, handle);
  5242. }
  5243. }
  5244. /**
  5245. * _scsih_remove_unresponding_sas_devices - removing unresponding devices
  5246. * @ioc: per adapter object
  5247. *
  5248. * Return nothing.
  5249. */
  5250. static void
  5251. _scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
  5252. {
  5253. struct _sas_device *sas_device, *sas_device_next;
  5254. struct _sas_node *sas_expander;
  5255. struct _raid_device *raid_device, *raid_device_next;
  5256. list_for_each_entry_safe(sas_device, sas_device_next,
  5257. &ioc->sas_device_list, list) {
  5258. if (sas_device->responding) {
  5259. sas_device->responding = 0;
  5260. continue;
  5261. }
  5262. if (sas_device->starget)
  5263. starget_printk(KERN_INFO, sas_device->starget,
  5264. "removing: handle(0x%04x), sas_addr(0x%016llx), "
  5265. "enclosure logical id(0x%016llx), slot(%d)\n",
  5266. sas_device->handle,
  5267. (unsigned long long)sas_device->sas_address,
  5268. (unsigned long long)
  5269. sas_device->enclosure_logical_id,
  5270. sas_device->slot);
  5271. _scsih_remove_device(ioc, sas_device);
  5272. }
  5273. list_for_each_entry_safe(raid_device, raid_device_next,
  5274. &ioc->raid_device_list, list) {
  5275. if (raid_device->responding) {
  5276. raid_device->responding = 0;
  5277. continue;
  5278. }
  5279. if (raid_device->starget) {
  5280. starget_printk(KERN_INFO, raid_device->starget,
  5281. "removing: handle(0x%04x), wwid(0x%016llx)\n",
  5282. raid_device->handle,
  5283. (unsigned long long)raid_device->wwid);
  5284. scsi_remove_target(&raid_device->starget->dev);
  5285. }
  5286. _scsih_raid_device_remove(ioc, raid_device);
  5287. }
  5288. retry_expander_search:
  5289. sas_expander = NULL;
  5290. list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
  5291. if (sas_expander->responding) {
  5292. sas_expander->responding = 0;
  5293. continue;
  5294. }
  5295. _scsih_expander_remove(ioc, sas_expander->sas_address);
  5296. goto retry_expander_search;
  5297. }
  5298. }
  5299. /**
  5300. * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
  5301. * @ioc: per adapter object
  5302. * @reset_phase: phase
  5303. *
  5304. * The handler for doing any required cleanup or initialization.
  5305. *
  5306. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  5307. * MPT2_IOC_DONE_RESET
  5308. *
  5309. * Return nothing.
  5310. */
  5311. void
  5312. mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  5313. {
  5314. switch (reset_phase) {
  5315. case MPT2_IOC_PRE_RESET:
  5316. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  5317. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  5318. break;
  5319. case MPT2_IOC_AFTER_RESET:
  5320. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  5321. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  5322. if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
  5323. ioc->scsih_cmds.status |= MPT2_CMD_RESET;
  5324. mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
  5325. complete(&ioc->scsih_cmds.done);
  5326. }
  5327. if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
  5328. ioc->tm_cmds.status |= MPT2_CMD_RESET;
  5329. mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
  5330. complete(&ioc->tm_cmds.done);
  5331. }
  5332. _scsih_fw_event_cleanup_queue(ioc);
  5333. _scsih_flush_running_cmds(ioc);
  5334. _scsih_queue_rescan(ioc);
  5335. break;
  5336. case MPT2_IOC_DONE_RESET:
  5337. dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
  5338. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  5339. _scsih_sas_host_refresh(ioc);
  5340. _scsih_prep_device_scan(ioc);
  5341. _scsih_search_responding_sas_devices(ioc);
  5342. _scsih_search_responding_raid_devices(ioc);
  5343. _scsih_search_responding_expanders(ioc);
  5344. break;
  5345. }
  5346. }
  5347. /**
  5348. * _firmware_event_work - delayed task for processing firmware events
  5349. * @ioc: per adapter object
  5350. * @work: equal to the fw_event_work object
  5351. * Context: user.
  5352. *
  5353. * Return nothing.
  5354. */
  5355. static void
  5356. _firmware_event_work(struct work_struct *work)
  5357. {
  5358. struct fw_event_work *fw_event = container_of(work,
  5359. struct fw_event_work, delayed_work.work);
  5360. unsigned long flags;
  5361. struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
  5362. /* the queue is being flushed so ignore this event */
  5363. if (ioc->remove_host || fw_event->cancel_pending_work) {
  5364. _scsih_fw_event_free(ioc, fw_event);
  5365. return;
  5366. }
  5367. if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
  5368. _scsih_fw_event_free(ioc, fw_event);
  5369. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  5370. if (ioc->shost_recovery) {
  5371. init_completion(&ioc->shost_recovery_done);
  5372. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
  5373. flags);
  5374. wait_for_completion(&ioc->shost_recovery_done);
  5375. } else
  5376. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
  5377. flags);
  5378. _scsih_remove_unresponding_sas_devices(ioc);
  5379. return;
  5380. }
  5381. switch (fw_event->event) {
  5382. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  5383. _scsih_sas_topology_change_event(ioc, fw_event);
  5384. break;
  5385. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  5386. _scsih_sas_device_status_change_event(ioc,
  5387. fw_event);
  5388. break;
  5389. case MPI2_EVENT_SAS_DISCOVERY:
  5390. _scsih_sas_discovery_event(ioc,
  5391. fw_event);
  5392. break;
  5393. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  5394. _scsih_sas_broadcast_primative_event(ioc,
  5395. fw_event);
  5396. break;
  5397. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  5398. _scsih_sas_enclosure_dev_status_change_event(ioc,
  5399. fw_event);
  5400. break;
  5401. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  5402. _scsih_sas_ir_config_change_event(ioc, fw_event);
  5403. break;
  5404. case MPI2_EVENT_IR_VOLUME:
  5405. _scsih_sas_ir_volume_event(ioc, fw_event);
  5406. break;
  5407. case MPI2_EVENT_IR_PHYSICAL_DISK:
  5408. _scsih_sas_ir_physical_disk_event(ioc, fw_event);
  5409. break;
  5410. case MPI2_EVENT_IR_OPERATION_STATUS:
  5411. _scsih_sas_ir_operation_status_event(ioc, fw_event);
  5412. break;
  5413. case MPI2_EVENT_TASK_SET_FULL:
  5414. _scsih_task_set_full(ioc, fw_event);
  5415. break;
  5416. }
  5417. _scsih_fw_event_free(ioc, fw_event);
  5418. }
  5419. /**
  5420. * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
  5421. * @ioc: per adapter object
  5422. * @msix_index: MSIX table index supplied by the OS
  5423. * @reply: reply message frame(lower 32bit addr)
  5424. * Context: interrupt.
  5425. *
  5426. * This function merely adds a new work task into ioc->firmware_event_thread.
  5427. * The tasks are worked from _firmware_event_work in user context.
  5428. *
  5429. * Return 1 meaning mf should be freed from _base_interrupt
  5430. * 0 means the mf is freed from this function.
  5431. */
  5432. u8
  5433. mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
  5434. u32 reply)
  5435. {
  5436. struct fw_event_work *fw_event;
  5437. Mpi2EventNotificationReply_t *mpi_reply;
  5438. u16 event;
  5439. u16 sz;
  5440. /* events turned off due to host reset or driver unloading */
  5441. if (ioc->remove_host)
  5442. return 1;
  5443. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  5444. event = le16_to_cpu(mpi_reply->Event);
  5445. switch (event) {
  5446. /* handle these */
  5447. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  5448. {
  5449. Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
  5450. (Mpi2EventDataSasBroadcastPrimitive_t *)
  5451. mpi_reply->EventData;
  5452. if (baen_data->Primitive !=
  5453. MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
  5454. ioc->broadcast_aen_busy)
  5455. return 1;
  5456. ioc->broadcast_aen_busy = 1;
  5457. break;
  5458. }
  5459. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  5460. _scsih_check_topo_delete_events(ioc,
  5461. (Mpi2EventDataSasTopologyChangeList_t *)
  5462. mpi_reply->EventData);
  5463. break;
  5464. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  5465. case MPI2_EVENT_IR_OPERATION_STATUS:
  5466. case MPI2_EVENT_SAS_DISCOVERY:
  5467. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  5468. case MPI2_EVENT_IR_VOLUME:
  5469. case MPI2_EVENT_IR_PHYSICAL_DISK:
  5470. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  5471. case MPI2_EVENT_TASK_SET_FULL:
  5472. break;
  5473. default: /* ignore the rest */
  5474. return 1;
  5475. }
  5476. fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
  5477. if (!fw_event) {
  5478. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5479. ioc->name, __FILE__, __LINE__, __func__);
  5480. return 1;
  5481. }
  5482. sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
  5483. fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
  5484. if (!fw_event->event_data) {
  5485. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5486. ioc->name, __FILE__, __LINE__, __func__);
  5487. kfree(fw_event);
  5488. return 1;
  5489. }
  5490. memcpy(fw_event->event_data, mpi_reply->EventData,
  5491. sz);
  5492. fw_event->ioc = ioc;
  5493. fw_event->VF_ID = mpi_reply->VF_ID;
  5494. fw_event->VP_ID = mpi_reply->VP_ID;
  5495. fw_event->event = event;
  5496. _scsih_fw_event_add(ioc, fw_event);
  5497. return 1;
  5498. }
  5499. /* shost template */
  5500. static struct scsi_host_template scsih_driver_template = {
  5501. .module = THIS_MODULE,
  5502. .name = "Fusion MPT SAS Host",
  5503. .proc_name = MPT2SAS_DRIVER_NAME,
  5504. .queuecommand = _scsih_qcmd,
  5505. .target_alloc = _scsih_target_alloc,
  5506. .slave_alloc = _scsih_slave_alloc,
  5507. .slave_configure = _scsih_slave_configure,
  5508. .target_destroy = _scsih_target_destroy,
  5509. .slave_destroy = _scsih_slave_destroy,
  5510. .change_queue_depth = _scsih_change_queue_depth,
  5511. .change_queue_type = _scsih_change_queue_type,
  5512. .eh_abort_handler = _scsih_abort,
  5513. .eh_device_reset_handler = _scsih_dev_reset,
  5514. .eh_target_reset_handler = _scsih_target_reset,
  5515. .eh_host_reset_handler = _scsih_host_reset,
  5516. .bios_param = _scsih_bios_param,
  5517. .can_queue = 1,
  5518. .this_id = -1,
  5519. .sg_tablesize = MPT2SAS_SG_DEPTH,
  5520. .max_sectors = 8192,
  5521. .cmd_per_lun = 7,
  5522. .use_clustering = ENABLE_CLUSTERING,
  5523. .shost_attrs = mpt2sas_host_attrs,
  5524. .sdev_attrs = mpt2sas_dev_attrs,
  5525. };
  5526. /**
  5527. * _scsih_expander_node_remove - removing expander device from list.
  5528. * @ioc: per adapter object
  5529. * @sas_expander: the sas_device object
  5530. * Context: Calling function should acquire ioc->sas_node_lock.
  5531. *
  5532. * Removing object and freeing associated memory from the
  5533. * ioc->sas_expander_list.
  5534. *
  5535. * Return nothing.
  5536. */
  5537. static void
  5538. _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
  5539. struct _sas_node *sas_expander)
  5540. {
  5541. struct _sas_port *mpt2sas_port;
  5542. struct _sas_device *sas_device;
  5543. struct _sas_node *expander_sibling;
  5544. unsigned long flags;
  5545. if (!sas_expander)
  5546. return;
  5547. /* remove sibling ports attached to this expander */
  5548. retry_device_search:
  5549. list_for_each_entry(mpt2sas_port,
  5550. &sas_expander->sas_port_list, port_list) {
  5551. if (mpt2sas_port->remote_identify.device_type ==
  5552. SAS_END_DEVICE) {
  5553. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5554. sas_device =
  5555. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  5556. mpt2sas_port->remote_identify.sas_address);
  5557. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5558. if (!sas_device)
  5559. continue;
  5560. _scsih_remove_device(ioc, sas_device);
  5561. if (ioc->shost_recovery)
  5562. return;
  5563. goto retry_device_search;
  5564. }
  5565. }
  5566. retry_expander_search:
  5567. list_for_each_entry(mpt2sas_port,
  5568. &sas_expander->sas_port_list, port_list) {
  5569. if (mpt2sas_port->remote_identify.device_type ==
  5570. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  5571. mpt2sas_port->remote_identify.device_type ==
  5572. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  5573. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  5574. expander_sibling =
  5575. mpt2sas_scsih_expander_find_by_sas_address(
  5576. ioc, mpt2sas_port->remote_identify.sas_address);
  5577. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  5578. if (!expander_sibling)
  5579. continue;
  5580. _scsih_expander_remove(ioc,
  5581. expander_sibling->sas_address);
  5582. if (ioc->shost_recovery)
  5583. return;
  5584. goto retry_expander_search;
  5585. }
  5586. }
  5587. mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
  5588. sas_expander->sas_address_parent);
  5589. printk(MPT2SAS_INFO_FMT "expander_remove: handle"
  5590. "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
  5591. sas_expander->handle, (unsigned long long)
  5592. sas_expander->sas_address);
  5593. list_del(&sas_expander->list);
  5594. kfree(sas_expander->phy);
  5595. kfree(sas_expander);
  5596. }
  5597. /**
  5598. * _scsih_ir_shutdown - IR shutdown notification
  5599. * @ioc: per adapter object
  5600. *
  5601. * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
  5602. * the host system is shutting down.
  5603. *
  5604. * Return nothing.
  5605. */
  5606. static void
  5607. _scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
  5608. {
  5609. Mpi2RaidActionRequest_t *mpi_request;
  5610. Mpi2RaidActionReply_t *mpi_reply;
  5611. u16 smid;
  5612. /* is IR firmware build loaded ? */
  5613. if (!ioc->ir_firmware)
  5614. return;
  5615. /* are there any volumes ? */
  5616. if (list_empty(&ioc->raid_device_list))
  5617. return;
  5618. mutex_lock(&ioc->scsih_cmds.mutex);
  5619. if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
  5620. printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
  5621. ioc->name, __func__);
  5622. goto out;
  5623. }
  5624. ioc->scsih_cmds.status = MPT2_CMD_PENDING;
  5625. smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
  5626. if (!smid) {
  5627. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  5628. ioc->name, __func__);
  5629. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  5630. goto out;
  5631. }
  5632. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  5633. ioc->scsih_cmds.smid = smid;
  5634. memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
  5635. mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
  5636. mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
  5637. printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
  5638. init_completion(&ioc->scsih_cmds.done);
  5639. mpt2sas_base_put_smid_default(ioc, smid);
  5640. wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
  5641. if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
  5642. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  5643. ioc->name, __func__);
  5644. goto out;
  5645. }
  5646. if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
  5647. mpi_reply = ioc->scsih_cmds.reply;
  5648. printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
  5649. "ioc_status(0x%04x), loginfo(0x%08x)\n",
  5650. ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
  5651. le32_to_cpu(mpi_reply->IOCLogInfo));
  5652. }
  5653. out:
  5654. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  5655. mutex_unlock(&ioc->scsih_cmds.mutex);
  5656. }
  5657. /**
  5658. * _scsih_shutdown - routine call during system shutdown
  5659. * @pdev: PCI device struct
  5660. *
  5661. * Return nothing.
  5662. */
  5663. static void
  5664. _scsih_shutdown(struct pci_dev *pdev)
  5665. {
  5666. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5667. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5668. struct workqueue_struct *wq;
  5669. unsigned long flags;
  5670. ioc->remove_host = 1;
  5671. _scsih_fw_event_cleanup_queue(ioc);
  5672. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  5673. wq = ioc->firmware_event_thread;
  5674. ioc->firmware_event_thread = NULL;
  5675. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  5676. if (wq)
  5677. destroy_workqueue(wq);
  5678. _scsih_ir_shutdown(ioc);
  5679. mpt2sas_base_detach(ioc);
  5680. }
  5681. /**
  5682. * _scsih_remove - detach and remove add host
  5683. * @pdev: PCI device struct
  5684. *
  5685. * Routine called when unloading the driver.
  5686. * Return nothing.
  5687. */
  5688. static void __devexit
  5689. _scsih_remove(struct pci_dev *pdev)
  5690. {
  5691. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5692. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5693. struct _sas_port *mpt2sas_port;
  5694. struct _sas_device *sas_device;
  5695. struct _sas_node *expander_sibling;
  5696. struct _raid_device *raid_device, *next;
  5697. struct MPT2SAS_TARGET *sas_target_priv_data;
  5698. struct workqueue_struct *wq;
  5699. unsigned long flags;
  5700. ioc->remove_host = 1;
  5701. _scsih_fw_event_cleanup_queue(ioc);
  5702. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  5703. wq = ioc->firmware_event_thread;
  5704. ioc->firmware_event_thread = NULL;
  5705. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  5706. if (wq)
  5707. destroy_workqueue(wq);
  5708. /* release all the volumes */
  5709. list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
  5710. list) {
  5711. if (raid_device->starget) {
  5712. sas_target_priv_data =
  5713. raid_device->starget->hostdata;
  5714. sas_target_priv_data->deleted = 1;
  5715. scsi_remove_target(&raid_device->starget->dev);
  5716. }
  5717. printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
  5718. "(0x%016llx)\n", ioc->name, raid_device->handle,
  5719. (unsigned long long) raid_device->wwid);
  5720. _scsih_raid_device_remove(ioc, raid_device);
  5721. }
  5722. /* free ports attached to the sas_host */
  5723. retry_again:
  5724. list_for_each_entry(mpt2sas_port,
  5725. &ioc->sas_hba.sas_port_list, port_list) {
  5726. if (mpt2sas_port->remote_identify.device_type ==
  5727. SAS_END_DEVICE) {
  5728. sas_device =
  5729. mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  5730. mpt2sas_port->remote_identify.sas_address);
  5731. if (sas_device) {
  5732. _scsih_remove_device(ioc, sas_device);
  5733. goto retry_again;
  5734. }
  5735. } else {
  5736. expander_sibling =
  5737. mpt2sas_scsih_expander_find_by_sas_address(ioc,
  5738. mpt2sas_port->remote_identify.sas_address);
  5739. if (expander_sibling) {
  5740. _scsih_expander_remove(ioc,
  5741. expander_sibling->sas_address);
  5742. goto retry_again;
  5743. }
  5744. }
  5745. }
  5746. /* free phys attached to the sas_host */
  5747. if (ioc->sas_hba.num_phys) {
  5748. kfree(ioc->sas_hba.phy);
  5749. ioc->sas_hba.phy = NULL;
  5750. ioc->sas_hba.num_phys = 0;
  5751. }
  5752. sas_remove_host(shost);
  5753. _scsih_shutdown(pdev);
  5754. list_del(&ioc->list);
  5755. scsi_remove_host(shost);
  5756. scsi_host_put(shost);
  5757. }
  5758. /**
  5759. * _scsih_probe_boot_devices - reports 1st device
  5760. * @ioc: per adapter object
  5761. *
  5762. * If specified in bios page 2, this routine reports the 1st
  5763. * device scsi-ml or sas transport for persistent boot device
  5764. * purposes. Please refer to function _scsih_determine_boot_device()
  5765. */
  5766. static void
  5767. _scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
  5768. {
  5769. u8 is_raid;
  5770. void *device;
  5771. struct _sas_device *sas_device;
  5772. struct _raid_device *raid_device;
  5773. u16 handle;
  5774. u64 sas_address_parent;
  5775. u64 sas_address;
  5776. unsigned long flags;
  5777. int rc;
  5778. device = NULL;
  5779. if (ioc->req_boot_device.device) {
  5780. device = ioc->req_boot_device.device;
  5781. is_raid = ioc->req_boot_device.is_raid;
  5782. } else if (ioc->req_alt_boot_device.device) {
  5783. device = ioc->req_alt_boot_device.device;
  5784. is_raid = ioc->req_alt_boot_device.is_raid;
  5785. } else if (ioc->current_boot_device.device) {
  5786. device = ioc->current_boot_device.device;
  5787. is_raid = ioc->current_boot_device.is_raid;
  5788. }
  5789. if (!device)
  5790. return;
  5791. if (is_raid) {
  5792. raid_device = device;
  5793. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5794. raid_device->id, 0);
  5795. if (rc)
  5796. _scsih_raid_device_remove(ioc, raid_device);
  5797. } else {
  5798. sas_device = device;
  5799. handle = sas_device->handle;
  5800. sas_address_parent = sas_device->sas_address_parent;
  5801. sas_address = sas_device->sas_address;
  5802. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5803. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  5804. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5805. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  5806. sas_device->sas_address_parent)) {
  5807. _scsih_sas_device_remove(ioc, sas_device);
  5808. } else if (!sas_device->starget) {
  5809. mpt2sas_transport_port_remove(ioc, sas_address,
  5810. sas_address_parent);
  5811. _scsih_sas_device_remove(ioc, sas_device);
  5812. }
  5813. }
  5814. }
  5815. /**
  5816. * _scsih_probe_raid - reporting raid volumes to scsi-ml
  5817. * @ioc: per adapter object
  5818. *
  5819. * Called during initial loading of the driver.
  5820. */
  5821. static void
  5822. _scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
  5823. {
  5824. struct _raid_device *raid_device, *raid_next;
  5825. int rc;
  5826. list_for_each_entry_safe(raid_device, raid_next,
  5827. &ioc->raid_device_list, list) {
  5828. if (raid_device->starget)
  5829. continue;
  5830. rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
  5831. raid_device->id, 0);
  5832. if (rc)
  5833. _scsih_raid_device_remove(ioc, raid_device);
  5834. }
  5835. }
  5836. /**
  5837. * _scsih_probe_sas - reporting sas devices to sas transport
  5838. * @ioc: per adapter object
  5839. *
  5840. * Called during initial loading of the driver.
  5841. */
  5842. static void
  5843. _scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
  5844. {
  5845. struct _sas_device *sas_device, *next;
  5846. unsigned long flags;
  5847. /* SAS Device List */
  5848. list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
  5849. list) {
  5850. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  5851. list_move_tail(&sas_device->list, &ioc->sas_device_list);
  5852. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  5853. if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
  5854. sas_device->sas_address_parent)) {
  5855. _scsih_sas_device_remove(ioc, sas_device);
  5856. } else if (!sas_device->starget) {
  5857. mpt2sas_transport_port_remove(ioc,
  5858. sas_device->sas_address,
  5859. sas_device->sas_address_parent);
  5860. _scsih_sas_device_remove(ioc, sas_device);
  5861. }
  5862. }
  5863. }
  5864. /**
  5865. * _scsih_probe_devices - probing for devices
  5866. * @ioc: per adapter object
  5867. *
  5868. * Called during initial loading of the driver.
  5869. */
  5870. static void
  5871. _scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
  5872. {
  5873. u16 volume_mapping_flags =
  5874. le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
  5875. MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
  5876. if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
  5877. return; /* return when IOC doesn't support initiator mode */
  5878. _scsih_probe_boot_devices(ioc);
  5879. if (ioc->ir_firmware) {
  5880. if ((volume_mapping_flags &
  5881. MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
  5882. _scsih_probe_sas(ioc);
  5883. _scsih_probe_raid(ioc);
  5884. } else {
  5885. _scsih_probe_raid(ioc);
  5886. _scsih_probe_sas(ioc);
  5887. }
  5888. } else
  5889. _scsih_probe_sas(ioc);
  5890. }
  5891. /**
  5892. * _scsih_probe - attach and add scsi host
  5893. * @pdev: PCI device struct
  5894. * @id: pci device id
  5895. *
  5896. * Returns 0 success, anything else error.
  5897. */
  5898. static int
  5899. _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  5900. {
  5901. struct MPT2SAS_ADAPTER *ioc;
  5902. struct Scsi_Host *shost;
  5903. shost = scsi_host_alloc(&scsih_driver_template,
  5904. sizeof(struct MPT2SAS_ADAPTER));
  5905. if (!shost)
  5906. return -ENODEV;
  5907. /* init local params */
  5908. ioc = shost_priv(shost);
  5909. memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
  5910. INIT_LIST_HEAD(&ioc->list);
  5911. list_add_tail(&ioc->list, &mpt2sas_ioc_list);
  5912. ioc->shost = shost;
  5913. ioc->id = mpt_ids++;
  5914. sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
  5915. ioc->pdev = pdev;
  5916. ioc->scsi_io_cb_idx = scsi_io_cb_idx;
  5917. ioc->tm_cb_idx = tm_cb_idx;
  5918. ioc->ctl_cb_idx = ctl_cb_idx;
  5919. ioc->base_cb_idx = base_cb_idx;
  5920. ioc->transport_cb_idx = transport_cb_idx;
  5921. ioc->scsih_cb_idx = scsih_cb_idx;
  5922. ioc->config_cb_idx = config_cb_idx;
  5923. ioc->tm_tr_cb_idx = tm_tr_cb_idx;
  5924. ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
  5925. ioc->logging_level = logging_level;
  5926. /* misc semaphores and spin locks */
  5927. mutex_init(&ioc->reset_in_progress_mutex);
  5928. spin_lock_init(&ioc->ioc_reset_in_progress_lock);
  5929. spin_lock_init(&ioc->scsi_lookup_lock);
  5930. spin_lock_init(&ioc->sas_device_lock);
  5931. spin_lock_init(&ioc->sas_node_lock);
  5932. spin_lock_init(&ioc->fw_event_lock);
  5933. spin_lock_init(&ioc->raid_device_lock);
  5934. INIT_LIST_HEAD(&ioc->sas_device_list);
  5935. INIT_LIST_HEAD(&ioc->sas_device_init_list);
  5936. INIT_LIST_HEAD(&ioc->sas_expander_list);
  5937. INIT_LIST_HEAD(&ioc->fw_event_list);
  5938. INIT_LIST_HEAD(&ioc->raid_device_list);
  5939. INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
  5940. INIT_LIST_HEAD(&ioc->delayed_tr_list);
  5941. /* init shost parameters */
  5942. shost->max_cmd_len = 32;
  5943. shost->max_lun = max_lun;
  5944. shost->transportt = mpt2sas_transport_template;
  5945. shost->unique_id = ioc->id;
  5946. if ((scsi_add_host(shost, &pdev->dev))) {
  5947. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5948. ioc->name, __FILE__, __LINE__, __func__);
  5949. list_del(&ioc->list);
  5950. goto out_add_shost_fail;
  5951. }
  5952. scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
  5953. | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
  5954. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  5955. /* event thread */
  5956. snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
  5957. "fw_event%d", ioc->id);
  5958. ioc->firmware_event_thread = create_singlethread_workqueue(
  5959. ioc->firmware_event_name);
  5960. if (!ioc->firmware_event_thread) {
  5961. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5962. ioc->name, __FILE__, __LINE__, __func__);
  5963. goto out_thread_fail;
  5964. }
  5965. ioc->wait_for_port_enable_to_complete = 1;
  5966. if ((mpt2sas_base_attach(ioc))) {
  5967. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  5968. ioc->name, __FILE__, __LINE__, __func__);
  5969. goto out_attach_fail;
  5970. }
  5971. ioc->wait_for_port_enable_to_complete = 0;
  5972. _scsih_probe_devices(ioc);
  5973. return 0;
  5974. out_attach_fail:
  5975. destroy_workqueue(ioc->firmware_event_thread);
  5976. out_thread_fail:
  5977. list_del(&ioc->list);
  5978. scsi_remove_host(shost);
  5979. out_add_shost_fail:
  5980. return -ENODEV;
  5981. }
  5982. #ifdef CONFIG_PM
  5983. /**
  5984. * _scsih_suspend - power management suspend main entry point
  5985. * @pdev: PCI device struct
  5986. * @state: PM state change to (usually PCI_D3)
  5987. *
  5988. * Returns 0 success, anything else error.
  5989. */
  5990. static int
  5991. _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
  5992. {
  5993. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  5994. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  5995. u32 device_state;
  5996. mpt2sas_base_stop_watchdog(ioc);
  5997. flush_scheduled_work();
  5998. scsi_block_requests(shost);
  5999. device_state = pci_choose_state(pdev, state);
  6000. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
  6001. "operating state [D%d]\n", ioc->name, pdev,
  6002. pci_name(pdev), device_state);
  6003. mpt2sas_base_free_resources(ioc);
  6004. pci_save_state(pdev);
  6005. pci_disable_device(pdev);
  6006. pci_set_power_state(pdev, device_state);
  6007. return 0;
  6008. }
  6009. /**
  6010. * _scsih_resume - power management resume main entry point
  6011. * @pdev: PCI device struct
  6012. *
  6013. * Returns 0 success, anything else error.
  6014. */
  6015. static int
  6016. _scsih_resume(struct pci_dev *pdev)
  6017. {
  6018. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6019. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6020. u32 device_state = pdev->current_state;
  6021. int r;
  6022. printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
  6023. "operating state [D%d]\n", ioc->name, pdev,
  6024. pci_name(pdev), device_state);
  6025. pci_set_power_state(pdev, PCI_D0);
  6026. pci_enable_wake(pdev, PCI_D0, 0);
  6027. pci_restore_state(pdev);
  6028. ioc->pdev = pdev;
  6029. r = mpt2sas_base_map_resources(ioc);
  6030. if (r)
  6031. return r;
  6032. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
  6033. scsi_unblock_requests(shost);
  6034. mpt2sas_base_start_watchdog(ioc);
  6035. return 0;
  6036. }
  6037. #endif /* CONFIG_PM */
  6038. /**
  6039. * _scsih_pci_error_detected - Called when a PCI error is detected.
  6040. * @pdev: PCI device struct
  6041. * @state: PCI channel state
  6042. *
  6043. * Description: Called when a PCI error is detected.
  6044. *
  6045. * Return value:
  6046. * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
  6047. */
  6048. static pci_ers_result_t
  6049. _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  6050. {
  6051. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6052. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6053. printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
  6054. ioc->name, state);
  6055. switch (state) {
  6056. case pci_channel_io_normal:
  6057. return PCI_ERS_RESULT_CAN_RECOVER;
  6058. case pci_channel_io_frozen:
  6059. scsi_block_requests(ioc->shost);
  6060. mpt2sas_base_stop_watchdog(ioc);
  6061. mpt2sas_base_free_resources(ioc);
  6062. return PCI_ERS_RESULT_NEED_RESET;
  6063. case pci_channel_io_perm_failure:
  6064. _scsih_remove(pdev);
  6065. return PCI_ERS_RESULT_DISCONNECT;
  6066. }
  6067. return PCI_ERS_RESULT_NEED_RESET;
  6068. }
  6069. /**
  6070. * _scsih_pci_slot_reset - Called when PCI slot has been reset.
  6071. * @pdev: PCI device struct
  6072. *
  6073. * Description: This routine is called by the pci error recovery
  6074. * code after the PCI slot has been reset, just before we
  6075. * should resume normal operations.
  6076. */
  6077. static pci_ers_result_t
  6078. _scsih_pci_slot_reset(struct pci_dev *pdev)
  6079. {
  6080. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6081. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6082. int rc;
  6083. printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
  6084. ioc->name);
  6085. ioc->pdev = pdev;
  6086. rc = mpt2sas_base_map_resources(ioc);
  6087. if (rc)
  6088. return PCI_ERS_RESULT_DISCONNECT;
  6089. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  6090. FORCE_BIG_HAMMER);
  6091. printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
  6092. (rc == 0) ? "success" : "failed");
  6093. if (!rc)
  6094. return PCI_ERS_RESULT_RECOVERED;
  6095. else
  6096. return PCI_ERS_RESULT_DISCONNECT;
  6097. }
  6098. /**
  6099. * _scsih_pci_resume() - resume normal ops after PCI reset
  6100. * @pdev: pointer to PCI device
  6101. *
  6102. * Called when the error recovery driver tells us that its
  6103. * OK to resume normal operation. Use completion to allow
  6104. * halted scsi ops to resume.
  6105. */
  6106. static void
  6107. _scsih_pci_resume(struct pci_dev *pdev)
  6108. {
  6109. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6110. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6111. printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
  6112. pci_cleanup_aer_uncorrect_error_status(pdev);
  6113. mpt2sas_base_start_watchdog(ioc);
  6114. scsi_unblock_requests(ioc->shost);
  6115. }
  6116. /**
  6117. * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
  6118. * @pdev: pointer to PCI device
  6119. */
  6120. static pci_ers_result_t
  6121. _scsih_pci_mmio_enabled(struct pci_dev *pdev)
  6122. {
  6123. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  6124. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  6125. printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
  6126. ioc->name);
  6127. /* TODO - dump whatever for debugging purposes */
  6128. /* Request a slot reset. */
  6129. return PCI_ERS_RESULT_NEED_RESET;
  6130. }
  6131. static struct pci_error_handlers _scsih_err_handler = {
  6132. .error_detected = _scsih_pci_error_detected,
  6133. .mmio_enabled = _scsih_pci_mmio_enabled,
  6134. .slot_reset = _scsih_pci_slot_reset,
  6135. .resume = _scsih_pci_resume,
  6136. };
  6137. static struct pci_driver scsih_driver = {
  6138. .name = MPT2SAS_DRIVER_NAME,
  6139. .id_table = scsih_pci_table,
  6140. .probe = _scsih_probe,
  6141. .remove = __devexit_p(_scsih_remove),
  6142. .shutdown = _scsih_shutdown,
  6143. .err_handler = &_scsih_err_handler,
  6144. #ifdef CONFIG_PM
  6145. .suspend = _scsih_suspend,
  6146. .resume = _scsih_resume,
  6147. #endif
  6148. };
  6149. /* raid transport support */
  6150. static struct raid_function_template mpt2sas_raid_functions = {
  6151. .cookie = &scsih_driver_template,
  6152. .is_raid = _scsih_is_raid,
  6153. .get_resync = _scsih_get_resync,
  6154. .get_state = _scsih_get_state,
  6155. };
  6156. /**
  6157. * _scsih_init - main entry point for this driver.
  6158. *
  6159. * Returns 0 success, anything else error.
  6160. */
  6161. static int __init
  6162. _scsih_init(void)
  6163. {
  6164. int error;
  6165. mpt_ids = 0;
  6166. printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
  6167. MPT2SAS_DRIVER_VERSION);
  6168. mpt2sas_transport_template =
  6169. sas_attach_transport(&mpt2sas_transport_functions);
  6170. if (!mpt2sas_transport_template)
  6171. return -ENODEV;
  6172. /* raid transport support */
  6173. mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
  6174. if (!mpt2sas_raid_template) {
  6175. sas_release_transport(mpt2sas_transport_template);
  6176. return -ENODEV;
  6177. }
  6178. mpt2sas_base_initialize_callback_handler();
  6179. /* queuecommand callback hander */
  6180. scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
  6181. /* task managment callback handler */
  6182. tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
  6183. /* base internal commands callback handler */
  6184. base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
  6185. /* transport internal commands callback handler */
  6186. transport_cb_idx = mpt2sas_base_register_callback_handler(
  6187. mpt2sas_transport_done);
  6188. /* scsih internal commands callback handler */
  6189. scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
  6190. /* configuration page API internal commands callback handler */
  6191. config_cb_idx = mpt2sas_base_register_callback_handler(
  6192. mpt2sas_config_done);
  6193. /* ctl module callback handler */
  6194. ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
  6195. tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
  6196. _scsih_tm_tr_complete);
  6197. tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
  6198. _scsih_sas_control_complete);
  6199. mpt2sas_ctl_init();
  6200. error = pci_register_driver(&scsih_driver);
  6201. if (error) {
  6202. /* raid transport support */
  6203. raid_class_release(mpt2sas_raid_template);
  6204. sas_release_transport(mpt2sas_transport_template);
  6205. }
  6206. return error;
  6207. }
  6208. /**
  6209. * _scsih_exit - exit point for this driver (when it is a module).
  6210. *
  6211. * Returns 0 success, anything else error.
  6212. */
  6213. static void __exit
  6214. _scsih_exit(void)
  6215. {
  6216. printk(KERN_INFO "mpt2sas version %s unloading\n",
  6217. MPT2SAS_DRIVER_VERSION);
  6218. pci_unregister_driver(&scsih_driver);
  6219. mpt2sas_ctl_exit();
  6220. mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
  6221. mpt2sas_base_release_callback_handler(tm_cb_idx);
  6222. mpt2sas_base_release_callback_handler(base_cb_idx);
  6223. mpt2sas_base_release_callback_handler(transport_cb_idx);
  6224. mpt2sas_base_release_callback_handler(scsih_cb_idx);
  6225. mpt2sas_base_release_callback_handler(config_cb_idx);
  6226. mpt2sas_base_release_callback_handler(ctl_cb_idx);
  6227. mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
  6228. mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
  6229. /* raid transport support */
  6230. raid_class_release(mpt2sas_raid_template);
  6231. sas_release_transport(mpt2sas_transport_template);
  6232. }
  6233. module_init(_scsih_init);
  6234. module_exit(_scsih_exit);