mpt2sas_scsih.c 166 KB

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