mpt2sas_scsih.c 246 KB

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