mpt2sas_scsih.c 207 KB

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