mpt2sas_scsih.c 208 KB

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