mpt2sas_scsih.c 208 KB

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