mpt2sas_scsih.c 183 KB

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