mpt2sas_scsih.c 183 KB

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