mptsas.c 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445
  1. /*
  2. * linux/drivers/message/fusion/mptsas.c
  3. * For use with LSI PCI chip/adapter(s)
  4. * running LSI Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2008 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. */
  9. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  10. /*
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; version 2 of the License.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/slab.h>
  44. #include <linux/init.h>
  45. #include <linux/errno.h>
  46. #include <linux/jiffies.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_transport_sas.h>
  54. #include <scsi/scsi_transport.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include "mptbase.h"
  57. #include "mptscsih.h"
  58. #include "mptsas.h"
  59. #define my_NAME "Fusion MPT SAS Host driver"
  60. #define my_VERSION MPT_LINUX_VERSION_COMMON
  61. #define MYNAM "mptsas"
  62. /*
  63. * Reserved channel for integrated raid
  64. */
  65. #define MPTSAS_RAID_CHANNEL 1
  66. #define SAS_CONFIG_PAGE_TIMEOUT 30
  67. MODULE_AUTHOR(MODULEAUTHOR);
  68. MODULE_DESCRIPTION(my_NAME);
  69. MODULE_LICENSE("GPL");
  70. MODULE_VERSION(my_VERSION);
  71. static int mpt_pt_clear;
  72. module_param(mpt_pt_clear, int, 0);
  73. MODULE_PARM_DESC(mpt_pt_clear,
  74. " Clear persistency table: enable=1 "
  75. "(default=MPTSCSIH_PT_CLEAR=0)");
  76. /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
  77. #define MPTSAS_MAX_LUN (16895)
  78. static int max_lun = MPTSAS_MAX_LUN;
  79. module_param(max_lun, int, 0);
  80. MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
  81. static int mpt_loadtime_max_sectors = 8192;
  82. module_param(mpt_loadtime_max_sectors, int, 0);
  83. MODULE_PARM_DESC(mpt_loadtime_max_sectors,
  84. " Maximum sector define for Host Bus Adaptor.Range 64 to 8192 default=8192");
  85. static u8 mptsasDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
  86. static u8 mptsasTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
  87. static u8 mptsasInternalCtx = MPT_MAX_PROTOCOL_DRIVERS; /* Used only for internal commands */
  88. static u8 mptsasMgmtCtx = MPT_MAX_PROTOCOL_DRIVERS;
  89. static u8 mptsasDeviceResetCtx = MPT_MAX_PROTOCOL_DRIVERS;
  90. static void mptsas_firmware_event_work(struct work_struct *work);
  91. static void mptsas_send_sas_event(struct fw_event_work *fw_event);
  92. static void mptsas_send_raid_event(struct fw_event_work *fw_event);
  93. static void mptsas_send_ir2_event(struct fw_event_work *fw_event);
  94. static void mptsas_parse_device_info(struct sas_identify *identify,
  95. struct mptsas_devinfo *device_info);
  96. static inline void mptsas_set_rphy(MPT_ADAPTER *ioc,
  97. struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy);
  98. static struct mptsas_phyinfo *mptsas_find_phyinfo_by_sas_address
  99. (MPT_ADAPTER *ioc, u64 sas_address);
  100. static int mptsas_sas_device_pg0(MPT_ADAPTER *ioc,
  101. struct mptsas_devinfo *device_info, u32 form, u32 form_specific);
  102. static int mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc,
  103. struct mptsas_enclosure *enclosure, u32 form, u32 form_specific);
  104. static int mptsas_add_end_device(MPT_ADAPTER *ioc,
  105. struct mptsas_phyinfo *phy_info);
  106. static void mptsas_del_end_device(MPT_ADAPTER *ioc,
  107. struct mptsas_phyinfo *phy_info);
  108. static void mptsas_send_link_status_event(struct fw_event_work *fw_event);
  109. static struct mptsas_portinfo *mptsas_find_portinfo_by_sas_address
  110. (MPT_ADAPTER *ioc, u64 sas_address);
  111. static void mptsas_expander_delete(MPT_ADAPTER *ioc,
  112. struct mptsas_portinfo *port_info, u8 force);
  113. static void mptsas_send_expander_event(struct fw_event_work *fw_event);
  114. static void mptsas_not_responding_devices(MPT_ADAPTER *ioc);
  115. static void mptsas_scan_sas_topology(MPT_ADAPTER *ioc);
  116. static void mptsas_broadcast_primative_work(struct fw_event_work *fw_event);
  117. static void mptsas_handle_queue_full_event(struct fw_event_work *fw_event);
  118. static void mptsas_volume_delete(MPT_ADAPTER *ioc, u8 id);
  119. void mptsas_schedule_target_reset(void *ioc);
  120. static void mptsas_print_phy_data(MPT_ADAPTER *ioc,
  121. MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
  122. {
  123. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  124. "---- IO UNIT PAGE 0 ------------\n", ioc->name));
  125. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  126. ioc->name, le16_to_cpu(phy_data->AttachedDeviceHandle)));
  127. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Controller Handle=0x%X\n",
  128. ioc->name, le16_to_cpu(phy_data->ControllerDevHandle)));
  129. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port=0x%X\n",
  130. ioc->name, phy_data->Port));
  131. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port Flags=0x%X\n",
  132. ioc->name, phy_data->PortFlags));
  133. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Flags=0x%X\n",
  134. ioc->name, phy_data->PhyFlags));
  135. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  136. ioc->name, phy_data->NegotiatedLinkRate));
  137. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  138. "Controller PHY Device Info=0x%X\n", ioc->name,
  139. le32_to_cpu(phy_data->ControllerPhyDeviceInfo)));
  140. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "DiscoveryStatus=0x%X\n\n",
  141. ioc->name, le32_to_cpu(phy_data->DiscoveryStatus)));
  142. }
  143. static void mptsas_print_phy_pg0(MPT_ADAPTER *ioc, SasPhyPage0_t *pg0)
  144. {
  145. __le64 sas_address;
  146. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  147. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  148. "---- SAS PHY PAGE 0 ------------\n", ioc->name));
  149. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  150. "Attached Device Handle=0x%X\n", ioc->name,
  151. le16_to_cpu(pg0->AttachedDevHandle)));
  152. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  153. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  154. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  155. "Attached PHY Identifier=0x%X\n", ioc->name,
  156. pg0->AttachedPhyIdentifier));
  157. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Attached Device Info=0x%X\n",
  158. ioc->name, le32_to_cpu(pg0->AttachedDeviceInfo)));
  159. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  160. ioc->name, pg0->ProgrammedLinkRate));
  161. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Change Count=0x%X\n",
  162. ioc->name, pg0->ChangeCount));
  163. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Info=0x%X\n\n",
  164. ioc->name, le32_to_cpu(pg0->PhyInfo)));
  165. }
  166. static void mptsas_print_phy_pg1(MPT_ADAPTER *ioc, SasPhyPage1_t *pg1)
  167. {
  168. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  169. "---- SAS PHY PAGE 1 ------------\n", ioc->name));
  170. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Invalid Dword Count=0x%x\n",
  171. ioc->name, pg1->InvalidDwordCount));
  172. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  173. "Running Disparity Error Count=0x%x\n", ioc->name,
  174. pg1->RunningDisparityErrorCount));
  175. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  176. "Loss Dword Synch Count=0x%x\n", ioc->name,
  177. pg1->LossDwordSynchCount));
  178. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  179. "PHY Reset Problem Count=0x%x\n\n", ioc->name,
  180. pg1->PhyResetProblemCount));
  181. }
  182. static void mptsas_print_device_pg0(MPT_ADAPTER *ioc, SasDevicePage0_t *pg0)
  183. {
  184. __le64 sas_address;
  185. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  186. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  187. "---- SAS DEVICE PAGE 0 ---------\n", ioc->name));
  188. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  189. ioc->name, le16_to_cpu(pg0->DevHandle)));
  190. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Handle=0x%X\n",
  191. ioc->name, le16_to_cpu(pg0->ParentDevHandle)));
  192. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Enclosure Handle=0x%X\n",
  193. ioc->name, le16_to_cpu(pg0->EnclosureHandle)));
  194. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Slot=0x%X\n",
  195. ioc->name, le16_to_cpu(pg0->Slot)));
  196. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  197. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  198. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Target ID=0x%X\n",
  199. ioc->name, pg0->TargetID));
  200. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Bus=0x%X\n",
  201. ioc->name, pg0->Bus));
  202. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Phy Num=0x%X\n",
  203. ioc->name, pg0->PhyNum));
  204. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Access Status=0x%X\n",
  205. ioc->name, le16_to_cpu(pg0->AccessStatus)));
  206. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Device Info=0x%X\n",
  207. ioc->name, le32_to_cpu(pg0->DeviceInfo)));
  208. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Flags=0x%X\n",
  209. ioc->name, le16_to_cpu(pg0->Flags)));
  210. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n\n",
  211. ioc->name, pg0->PhysicalPort));
  212. }
  213. static void mptsas_print_expander_pg1(MPT_ADAPTER *ioc, SasExpanderPage1_t *pg1)
  214. {
  215. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  216. "---- SAS EXPANDER PAGE 1 ------------\n", ioc->name));
  217. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n",
  218. ioc->name, pg1->PhysicalPort));
  219. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Identifier=0x%X\n",
  220. ioc->name, pg1->PhyIdentifier));
  221. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  222. ioc->name, pg1->NegotiatedLinkRate));
  223. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  224. ioc->name, pg1->ProgrammedLinkRate));
  225. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hardware Link Rate=0x%X\n",
  226. ioc->name, pg1->HwLinkRate));
  227. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Owner Device Handle=0x%X\n",
  228. ioc->name, le16_to_cpu(pg1->OwnerDevHandle)));
  229. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  230. "Attached Device Handle=0x%X\n\n", ioc->name,
  231. le16_to_cpu(pg1->AttachedDevHandle)));
  232. }
  233. /* inhibit sas firmware event handling */
  234. static void
  235. mptsas_fw_event_off(MPT_ADAPTER *ioc)
  236. {
  237. unsigned long flags;
  238. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  239. ioc->fw_events_off = 1;
  240. ioc->sas_discovery_quiesce_io = 0;
  241. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  242. }
  243. /* enable sas firmware event handling */
  244. static void
  245. mptsas_fw_event_on(MPT_ADAPTER *ioc)
  246. {
  247. unsigned long flags;
  248. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  249. ioc->fw_events_off = 0;
  250. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  251. }
  252. /* queue a sas firmware event */
  253. static void
  254. mptsas_add_fw_event(MPT_ADAPTER *ioc, struct fw_event_work *fw_event,
  255. unsigned long delay)
  256. {
  257. unsigned long flags;
  258. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  259. list_add_tail(&fw_event->list, &ioc->fw_event_list);
  260. INIT_DELAYED_WORK(&fw_event->work, mptsas_firmware_event_work);
  261. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: add (fw_event=0x%p)"
  262. "on cpuid %d\n", ioc->name, __func__,
  263. fw_event, smp_processor_id()));
  264. queue_delayed_work_on(smp_processor_id(), ioc->fw_event_q,
  265. &fw_event->work, delay);
  266. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  267. }
  268. /* requeue a sas firmware event */
  269. static void
  270. mptsas_requeue_fw_event(MPT_ADAPTER *ioc, struct fw_event_work *fw_event,
  271. unsigned long delay)
  272. {
  273. unsigned long flags;
  274. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  275. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: reschedule task "
  276. "(fw_event=0x%p)on cpuid %d\n", ioc->name, __func__,
  277. fw_event, smp_processor_id()));
  278. fw_event->retries++;
  279. queue_delayed_work_on(smp_processor_id(), ioc->fw_event_q,
  280. &fw_event->work, msecs_to_jiffies(delay));
  281. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  282. }
  283. /* free memory associated to a sas firmware event */
  284. static void
  285. mptsas_free_fw_event(MPT_ADAPTER *ioc, struct fw_event_work *fw_event)
  286. {
  287. unsigned long flags;
  288. spin_lock_irqsave(&ioc->fw_event_lock, flags);
  289. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: kfree (fw_event=0x%p)\n",
  290. ioc->name, __func__, fw_event));
  291. list_del(&fw_event->list);
  292. kfree(fw_event);
  293. spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
  294. }
  295. /* walk the firmware event queue, and either stop or wait for
  296. * outstanding events to complete */
  297. static void
  298. mptsas_cleanup_fw_event_q(MPT_ADAPTER *ioc)
  299. {
  300. struct fw_event_work *fw_event, *next;
  301. struct mptsas_target_reset_event *target_reset_list, *n;
  302. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  303. /* flush the target_reset_list */
  304. if (!list_empty(&hd->target_reset_list)) {
  305. list_for_each_entry_safe(target_reset_list, n,
  306. &hd->target_reset_list, list) {
  307. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  308. "%s: removing target reset for id=%d\n",
  309. ioc->name, __func__,
  310. target_reset_list->sas_event_data.TargetID));
  311. list_del(&target_reset_list->list);
  312. kfree(target_reset_list);
  313. }
  314. }
  315. if (list_empty(&ioc->fw_event_list) ||
  316. !ioc->fw_event_q || in_interrupt())
  317. return;
  318. list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
  319. if (cancel_delayed_work(&fw_event->work))
  320. mptsas_free_fw_event(ioc, fw_event);
  321. }
  322. }
  323. static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
  324. {
  325. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  326. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  327. }
  328. static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
  329. {
  330. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  331. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  332. }
  333. /*
  334. * mptsas_find_portinfo_by_handle
  335. *
  336. * This function should be called with the sas_topology_mutex already held
  337. */
  338. static struct mptsas_portinfo *
  339. mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
  340. {
  341. struct mptsas_portinfo *port_info, *rc=NULL;
  342. int i;
  343. list_for_each_entry(port_info, &ioc->sas_topology, list)
  344. for (i = 0; i < port_info->num_phys; i++)
  345. if (port_info->phy_info[i].identify.handle == handle) {
  346. rc = port_info;
  347. goto out;
  348. }
  349. out:
  350. return rc;
  351. }
  352. /**
  353. * mptsas_find_portinfo_by_sas_address -
  354. * @ioc: Pointer to MPT_ADAPTER structure
  355. * @handle:
  356. *
  357. * This function should be called with the sas_topology_mutex already held
  358. *
  359. **/
  360. static struct mptsas_portinfo *
  361. mptsas_find_portinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  362. {
  363. struct mptsas_portinfo *port_info, *rc = NULL;
  364. int i;
  365. if (sas_address >= ioc->hba_port_sas_addr &&
  366. sas_address < (ioc->hba_port_sas_addr +
  367. ioc->hba_port_num_phy))
  368. return ioc->hba_port_info;
  369. mutex_lock(&ioc->sas_topology_mutex);
  370. list_for_each_entry(port_info, &ioc->sas_topology, list)
  371. for (i = 0; i < port_info->num_phys; i++)
  372. if (port_info->phy_info[i].identify.sas_address ==
  373. sas_address) {
  374. rc = port_info;
  375. goto out;
  376. }
  377. out:
  378. mutex_unlock(&ioc->sas_topology_mutex);
  379. return rc;
  380. }
  381. /*
  382. * Returns true if there is a scsi end device
  383. */
  384. static inline int
  385. mptsas_is_end_device(struct mptsas_devinfo * attached)
  386. {
  387. if ((attached->sas_address) &&
  388. (attached->device_info &
  389. MPI_SAS_DEVICE_INFO_END_DEVICE) &&
  390. ((attached->device_info &
  391. MPI_SAS_DEVICE_INFO_SSP_TARGET) |
  392. (attached->device_info &
  393. MPI_SAS_DEVICE_INFO_STP_TARGET) |
  394. (attached->device_info &
  395. MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
  396. return 1;
  397. else
  398. return 0;
  399. }
  400. /* no mutex */
  401. static void
  402. mptsas_port_delete(MPT_ADAPTER *ioc, struct mptsas_portinfo_details * port_details)
  403. {
  404. struct mptsas_portinfo *port_info;
  405. struct mptsas_phyinfo *phy_info;
  406. u8 i;
  407. if (!port_details)
  408. return;
  409. port_info = port_details->port_info;
  410. phy_info = port_info->phy_info;
  411. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: [%p]: num_phys=%02d "
  412. "bitmask=0x%016llX\n", ioc->name, __func__, port_details,
  413. port_details->num_phys, (unsigned long long)
  414. port_details->phy_bitmask));
  415. for (i = 0; i < port_info->num_phys; i++, phy_info++) {
  416. if(phy_info->port_details != port_details)
  417. continue;
  418. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  419. mptsas_set_rphy(ioc, phy_info, NULL);
  420. phy_info->port_details = NULL;
  421. }
  422. kfree(port_details);
  423. }
  424. static inline struct sas_rphy *
  425. mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
  426. {
  427. if (phy_info->port_details)
  428. return phy_info->port_details->rphy;
  429. else
  430. return NULL;
  431. }
  432. static inline void
  433. mptsas_set_rphy(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
  434. {
  435. if (phy_info->port_details) {
  436. phy_info->port_details->rphy = rphy;
  437. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_rphy_add: rphy=%p\n",
  438. ioc->name, rphy));
  439. }
  440. if (rphy) {
  441. dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
  442. &rphy->dev, MYIOC_s_FMT "add:", ioc->name));
  443. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rphy=%p release=%p\n",
  444. ioc->name, rphy, rphy->dev.release));
  445. }
  446. }
  447. static inline struct sas_port *
  448. mptsas_get_port(struct mptsas_phyinfo *phy_info)
  449. {
  450. if (phy_info->port_details)
  451. return phy_info->port_details->port;
  452. else
  453. return NULL;
  454. }
  455. static inline void
  456. mptsas_set_port(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_port *port)
  457. {
  458. if (phy_info->port_details)
  459. phy_info->port_details->port = port;
  460. if (port) {
  461. dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
  462. &port->dev, MYIOC_s_FMT "add:", ioc->name));
  463. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "port=%p release=%p\n",
  464. ioc->name, port, port->dev.release));
  465. }
  466. }
  467. static inline struct scsi_target *
  468. mptsas_get_starget(struct mptsas_phyinfo *phy_info)
  469. {
  470. if (phy_info->port_details)
  471. return phy_info->port_details->starget;
  472. else
  473. return NULL;
  474. }
  475. static inline void
  476. mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
  477. starget)
  478. {
  479. if (phy_info->port_details)
  480. phy_info->port_details->starget = starget;
  481. }
  482. /**
  483. * mptsas_add_device_component -
  484. * @ioc: Pointer to MPT_ADAPTER structure
  485. * @channel: fw mapped id's
  486. * @id:
  487. * @sas_address:
  488. * @device_info:
  489. *
  490. **/
  491. static void
  492. mptsas_add_device_component(MPT_ADAPTER *ioc, u8 channel, u8 id,
  493. u64 sas_address, u32 device_info, u16 slot, u64 enclosure_logical_id)
  494. {
  495. struct mptsas_device_info *sas_info, *next;
  496. struct scsi_device *sdev;
  497. struct scsi_target *starget;
  498. struct sas_rphy *rphy;
  499. /*
  500. * Delete all matching devices out of the list
  501. */
  502. mutex_lock(&ioc->sas_device_info_mutex);
  503. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  504. list) {
  505. if (!sas_info->is_logical_volume &&
  506. (sas_info->sas_address == sas_address ||
  507. (sas_info->fw.channel == channel &&
  508. sas_info->fw.id == id))) {
  509. list_del(&sas_info->list);
  510. kfree(sas_info);
  511. }
  512. }
  513. sas_info = kzalloc(sizeof(struct mptsas_device_info), GFP_KERNEL);
  514. if (!sas_info)
  515. goto out;
  516. /*
  517. * Set Firmware mapping
  518. */
  519. sas_info->fw.id = id;
  520. sas_info->fw.channel = channel;
  521. sas_info->sas_address = sas_address;
  522. sas_info->device_info = device_info;
  523. sas_info->slot = slot;
  524. sas_info->enclosure_logical_id = enclosure_logical_id;
  525. INIT_LIST_HEAD(&sas_info->list);
  526. list_add_tail(&sas_info->list, &ioc->sas_device_info_list);
  527. /*
  528. * Set OS mapping
  529. */
  530. shost_for_each_device(sdev, ioc->sh) {
  531. starget = scsi_target(sdev);
  532. rphy = dev_to_rphy(starget->dev.parent);
  533. if (rphy->identify.sas_address == sas_address) {
  534. sas_info->os.id = starget->id;
  535. sas_info->os.channel = starget->channel;
  536. }
  537. }
  538. out:
  539. mutex_unlock(&ioc->sas_device_info_mutex);
  540. return;
  541. }
  542. /**
  543. * mptsas_add_device_component_by_fw -
  544. * @ioc: Pointer to MPT_ADAPTER structure
  545. * @channel: fw mapped id's
  546. * @id:
  547. *
  548. **/
  549. static void
  550. mptsas_add_device_component_by_fw(MPT_ADAPTER *ioc, u8 channel, u8 id)
  551. {
  552. struct mptsas_devinfo sas_device;
  553. struct mptsas_enclosure enclosure_info;
  554. int rc;
  555. rc = mptsas_sas_device_pg0(ioc, &sas_device,
  556. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  557. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  558. (channel << 8) + id);
  559. if (rc)
  560. return;
  561. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  562. mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  563. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  564. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT),
  565. sas_device.handle_enclosure);
  566. mptsas_add_device_component(ioc, sas_device.channel,
  567. sas_device.id, sas_device.sas_address, sas_device.device_info,
  568. sas_device.slot, enclosure_info.enclosure_logical_id);
  569. }
  570. /**
  571. * mptsas_add_device_component_starget_ir - Handle Integrated RAID, adding each individual device to list
  572. * @ioc: Pointer to MPT_ADAPTER structure
  573. * @channel: fw mapped id's
  574. * @id:
  575. *
  576. **/
  577. static void
  578. mptsas_add_device_component_starget_ir(MPT_ADAPTER *ioc,
  579. struct scsi_target *starget)
  580. {
  581. CONFIGPARMS cfg;
  582. ConfigPageHeader_t hdr;
  583. dma_addr_t dma_handle;
  584. pRaidVolumePage0_t buffer = NULL;
  585. int i;
  586. RaidPhysDiskPage0_t phys_disk;
  587. struct mptsas_device_info *sas_info, *next;
  588. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  589. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  590. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  591. /* assumption that all volumes on channel = 0 */
  592. cfg.pageAddr = starget->id;
  593. cfg.cfghdr.hdr = &hdr;
  594. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  595. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  596. if (mpt_config(ioc, &cfg) != 0)
  597. goto out;
  598. if (!hdr.PageLength)
  599. goto out;
  600. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  601. &dma_handle);
  602. if (!buffer)
  603. goto out;
  604. cfg.physAddr = dma_handle;
  605. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  606. if (mpt_config(ioc, &cfg) != 0)
  607. goto out;
  608. if (!buffer->NumPhysDisks)
  609. goto out;
  610. /*
  611. * Adding entry for hidden components
  612. */
  613. for (i = 0; i < buffer->NumPhysDisks; i++) {
  614. if (mpt_raid_phys_disk_pg0(ioc,
  615. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  616. continue;
  617. mptsas_add_device_component_by_fw(ioc, phys_disk.PhysDiskBus,
  618. phys_disk.PhysDiskID);
  619. mutex_lock(&ioc->sas_device_info_mutex);
  620. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  621. list) {
  622. if (!sas_info->is_logical_volume &&
  623. (sas_info->fw.channel == phys_disk.PhysDiskBus &&
  624. sas_info->fw.id == phys_disk.PhysDiskID)) {
  625. sas_info->is_hidden_raid_component = 1;
  626. sas_info->volume_id = starget->id;
  627. }
  628. }
  629. mutex_unlock(&ioc->sas_device_info_mutex);
  630. }
  631. /*
  632. * Delete all matching devices out of the list
  633. */
  634. mutex_lock(&ioc->sas_device_info_mutex);
  635. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  636. list) {
  637. if (sas_info->is_logical_volume && sas_info->fw.id ==
  638. starget->id) {
  639. list_del(&sas_info->list);
  640. kfree(sas_info);
  641. }
  642. }
  643. sas_info = kzalloc(sizeof(struct mptsas_device_info), GFP_KERNEL);
  644. if (sas_info) {
  645. sas_info->fw.id = starget->id;
  646. sas_info->os.id = starget->id;
  647. sas_info->os.channel = starget->channel;
  648. sas_info->is_logical_volume = 1;
  649. INIT_LIST_HEAD(&sas_info->list);
  650. list_add_tail(&sas_info->list, &ioc->sas_device_info_list);
  651. }
  652. mutex_unlock(&ioc->sas_device_info_mutex);
  653. out:
  654. if (buffer)
  655. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  656. dma_handle);
  657. }
  658. /**
  659. * mptsas_add_device_component_starget -
  660. * @ioc: Pointer to MPT_ADAPTER structure
  661. * @starget:
  662. *
  663. **/
  664. static void
  665. mptsas_add_device_component_starget(MPT_ADAPTER *ioc,
  666. struct scsi_target *starget)
  667. {
  668. VirtTarget *vtarget;
  669. struct sas_rphy *rphy;
  670. struct mptsas_phyinfo *phy_info = NULL;
  671. struct mptsas_enclosure enclosure_info;
  672. rphy = dev_to_rphy(starget->dev.parent);
  673. vtarget = starget->hostdata;
  674. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  675. rphy->identify.sas_address);
  676. if (!phy_info)
  677. return;
  678. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  679. mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  680. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  681. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT),
  682. phy_info->attached.handle_enclosure);
  683. mptsas_add_device_component(ioc, phy_info->attached.channel,
  684. phy_info->attached.id, phy_info->attached.sas_address,
  685. phy_info->attached.device_info,
  686. phy_info->attached.slot, enclosure_info.enclosure_logical_id);
  687. }
  688. /**
  689. * mptsas_del_device_component_by_os - Once a device has been removed, we mark the entry in the list as being cached
  690. * @ioc: Pointer to MPT_ADAPTER structure
  691. * @channel: os mapped id's
  692. * @id:
  693. *
  694. **/
  695. static void
  696. mptsas_del_device_component_by_os(MPT_ADAPTER *ioc, u8 channel, u8 id)
  697. {
  698. struct mptsas_device_info *sas_info, *next;
  699. /*
  700. * Set is_cached flag
  701. */
  702. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  703. list) {
  704. if (sas_info->os.channel == channel && sas_info->os.id == id)
  705. sas_info->is_cached = 1;
  706. }
  707. }
  708. /**
  709. * mptsas_del_device_components - Cleaning the list
  710. * @ioc: Pointer to MPT_ADAPTER structure
  711. *
  712. **/
  713. static void
  714. mptsas_del_device_components(MPT_ADAPTER *ioc)
  715. {
  716. struct mptsas_device_info *sas_info, *next;
  717. mutex_lock(&ioc->sas_device_info_mutex);
  718. list_for_each_entry_safe(sas_info, next, &ioc->sas_device_info_list,
  719. list) {
  720. list_del(&sas_info->list);
  721. kfree(sas_info);
  722. }
  723. mutex_unlock(&ioc->sas_device_info_mutex);
  724. }
  725. /*
  726. * mptsas_setup_wide_ports
  727. *
  728. * Updates for new and existing narrow/wide port configuration
  729. * in the sas_topology
  730. */
  731. static void
  732. mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  733. {
  734. struct mptsas_portinfo_details * port_details;
  735. struct mptsas_phyinfo *phy_info, *phy_info_cmp;
  736. u64 sas_address;
  737. int i, j;
  738. mutex_lock(&ioc->sas_topology_mutex);
  739. phy_info = port_info->phy_info;
  740. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  741. if (phy_info->attached.handle)
  742. continue;
  743. port_details = phy_info->port_details;
  744. if (!port_details)
  745. continue;
  746. if (port_details->num_phys < 2)
  747. continue;
  748. /*
  749. * Removing a phy from a port, letting the last
  750. * phy be removed by firmware events.
  751. */
  752. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  753. "%s: [%p]: deleting phy = %d\n",
  754. ioc->name, __func__, port_details, i));
  755. port_details->num_phys--;
  756. port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
  757. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  758. if (phy_info->phy) {
  759. devtprintk(ioc, dev_printk(KERN_DEBUG,
  760. &phy_info->phy->dev, MYIOC_s_FMT
  761. "delete phy %d, phy-obj (0x%p)\n", ioc->name,
  762. phy_info->phy_id, phy_info->phy));
  763. sas_port_delete_phy(port_details->port, phy_info->phy);
  764. }
  765. phy_info->port_details = NULL;
  766. }
  767. /*
  768. * Populate and refresh the tree
  769. */
  770. phy_info = port_info->phy_info;
  771. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  772. sas_address = phy_info->attached.sas_address;
  773. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "phy_id=%d sas_address=0x%018llX\n",
  774. ioc->name, i, (unsigned long long)sas_address));
  775. if (!sas_address)
  776. continue;
  777. port_details = phy_info->port_details;
  778. /*
  779. * Forming a port
  780. */
  781. if (!port_details) {
  782. port_details = kzalloc(sizeof(struct
  783. mptsas_portinfo_details), GFP_KERNEL);
  784. if (!port_details)
  785. goto out;
  786. port_details->num_phys = 1;
  787. port_details->port_info = port_info;
  788. if (phy_info->phy_id < 64 )
  789. port_details->phy_bitmask |=
  790. (1 << phy_info->phy_id);
  791. phy_info->sas_port_add_phy=1;
  792. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tForming port\n\t\t"
  793. "phy_id=%d sas_address=0x%018llX\n",
  794. ioc->name, i, (unsigned long long)sas_address));
  795. phy_info->port_details = port_details;
  796. }
  797. if (i == port_info->num_phys - 1)
  798. continue;
  799. phy_info_cmp = &port_info->phy_info[i + 1];
  800. for (j = i + 1 ; j < port_info->num_phys ; j++,
  801. phy_info_cmp++) {
  802. if (!phy_info_cmp->attached.sas_address)
  803. continue;
  804. if (sas_address != phy_info_cmp->attached.sas_address)
  805. continue;
  806. if (phy_info_cmp->port_details == port_details )
  807. continue;
  808. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  809. "\t\tphy_id=%d sas_address=0x%018llX\n",
  810. ioc->name, j, (unsigned long long)
  811. phy_info_cmp->attached.sas_address));
  812. if (phy_info_cmp->port_details) {
  813. port_details->rphy =
  814. mptsas_get_rphy(phy_info_cmp);
  815. port_details->port =
  816. mptsas_get_port(phy_info_cmp);
  817. port_details->starget =
  818. mptsas_get_starget(phy_info_cmp);
  819. port_details->num_phys =
  820. phy_info_cmp->port_details->num_phys;
  821. if (!phy_info_cmp->port_details->num_phys)
  822. kfree(phy_info_cmp->port_details);
  823. } else
  824. phy_info_cmp->sas_port_add_phy=1;
  825. /*
  826. * Adding a phy to a port
  827. */
  828. phy_info_cmp->port_details = port_details;
  829. if (phy_info_cmp->phy_id < 64 )
  830. port_details->phy_bitmask |=
  831. (1 << phy_info_cmp->phy_id);
  832. port_details->num_phys++;
  833. }
  834. }
  835. out:
  836. for (i = 0; i < port_info->num_phys; i++) {
  837. port_details = port_info->phy_info[i].port_details;
  838. if (!port_details)
  839. continue;
  840. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  841. "%s: [%p]: phy_id=%02d num_phys=%02d "
  842. "bitmask=0x%016llX\n", ioc->name, __func__,
  843. port_details, i, port_details->num_phys,
  844. (unsigned long long)port_details->phy_bitmask));
  845. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tport = %p rphy=%p\n",
  846. ioc->name, port_details->port, port_details->rphy));
  847. }
  848. dsaswideprintk(ioc, printk("\n"));
  849. mutex_unlock(&ioc->sas_topology_mutex);
  850. }
  851. /**
  852. * csmisas_find_vtarget
  853. *
  854. * @ioc
  855. * @volume_id
  856. * @volume_bus
  857. *
  858. **/
  859. static VirtTarget *
  860. mptsas_find_vtarget(MPT_ADAPTER *ioc, u8 channel, u8 id)
  861. {
  862. struct scsi_device *sdev;
  863. VirtDevice *vdevice;
  864. VirtTarget *vtarget = NULL;
  865. shost_for_each_device(sdev, ioc->sh) {
  866. vdevice = sdev->hostdata;
  867. if ((vdevice == NULL) ||
  868. (vdevice->vtarget == NULL))
  869. continue;
  870. if ((vdevice->vtarget->tflags &
  871. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  872. vdevice->vtarget->raidVolume))
  873. continue;
  874. if (vdevice->vtarget->id == id &&
  875. vdevice->vtarget->channel == channel)
  876. vtarget = vdevice->vtarget;
  877. }
  878. return vtarget;
  879. }
  880. static void
  881. mptsas_queue_device_delete(MPT_ADAPTER *ioc,
  882. MpiEventDataSasDeviceStatusChange_t *sas_event_data)
  883. {
  884. struct fw_event_work *fw_event;
  885. fw_event = kzalloc(sizeof(*fw_event) +
  886. sizeof(MpiEventDataSasDeviceStatusChange_t),
  887. GFP_ATOMIC);
  888. if (!fw_event) {
  889. printk(MYIOC_s_WARN_FMT "%s: failed at (line=%d)\n",
  890. ioc->name, __func__, __LINE__);
  891. return;
  892. }
  893. memcpy(fw_event->event_data, sas_event_data,
  894. sizeof(MpiEventDataSasDeviceStatusChange_t));
  895. fw_event->event = MPI_EVENT_SAS_DEVICE_STATUS_CHANGE;
  896. fw_event->ioc = ioc;
  897. mptsas_add_fw_event(ioc, fw_event, msecs_to_jiffies(1));
  898. }
  899. static void
  900. mptsas_queue_rescan(MPT_ADAPTER *ioc)
  901. {
  902. struct fw_event_work *fw_event;
  903. fw_event = kzalloc(sizeof(*fw_event), GFP_ATOMIC);
  904. if (!fw_event) {
  905. printk(MYIOC_s_WARN_FMT "%s: failed at (line=%d)\n",
  906. ioc->name, __func__, __LINE__);
  907. return;
  908. }
  909. fw_event->event = -1;
  910. fw_event->ioc = ioc;
  911. mptsas_add_fw_event(ioc, fw_event, msecs_to_jiffies(1));
  912. }
  913. /**
  914. * mptsas_target_reset
  915. *
  916. * Issues TARGET_RESET to end device using handshaking method
  917. *
  918. * @ioc
  919. * @channel
  920. * @id
  921. *
  922. * Returns (1) success
  923. * (0) failure
  924. *
  925. **/
  926. static int
  927. mptsas_target_reset(MPT_ADAPTER *ioc, u8 channel, u8 id)
  928. {
  929. MPT_FRAME_HDR *mf;
  930. SCSITaskMgmt_t *pScsiTm;
  931. if (mpt_set_taskmgmt_in_progress_flag(ioc) != 0)
  932. return 0;
  933. mf = mpt_get_msg_frame(mptsasDeviceResetCtx, ioc);
  934. if (mf == NULL) {
  935. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT
  936. "%s, no msg frames @%d!!\n", ioc->name,
  937. __func__, __LINE__));
  938. goto out_fail;
  939. }
  940. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt request (mf=%p)\n",
  941. ioc->name, mf));
  942. /* Format the Request
  943. */
  944. pScsiTm = (SCSITaskMgmt_t *) mf;
  945. memset (pScsiTm, 0, sizeof(SCSITaskMgmt_t));
  946. pScsiTm->TargetID = id;
  947. pScsiTm->Bus = channel;
  948. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  949. pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  950. pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
  951. DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
  952. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  953. "TaskMgmt type=%d (sas device delete) fw_channel = %d fw_id = %d)\n",
  954. ioc->name, MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET, channel, id));
  955. mpt_put_msg_frame_hi_pri(mptsasDeviceResetCtx, ioc, mf);
  956. return 1;
  957. out_fail:
  958. mpt_clear_taskmgmt_in_progress_flag(ioc);
  959. return 0;
  960. }
  961. static void
  962. mptsas_block_io_sdev(struct scsi_device *sdev, void *data)
  963. {
  964. scsi_device_set_state(sdev, SDEV_BLOCK);
  965. }
  966. static void
  967. mptsas_block_io_starget(struct scsi_target *starget)
  968. {
  969. if (starget)
  970. starget_for_each_device(starget, NULL, mptsas_block_io_sdev);
  971. }
  972. /**
  973. * mptsas_target_reset_queue
  974. *
  975. * Receive request for TARGET_RESET after receiving an firmware
  976. * event NOT_RESPONDING_EVENT, then put command in link list
  977. * and queue if task_queue already in use.
  978. *
  979. * @ioc
  980. * @sas_event_data
  981. *
  982. **/
  983. static void
  984. mptsas_target_reset_queue(MPT_ADAPTER *ioc,
  985. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  986. {
  987. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  988. VirtTarget *vtarget = NULL;
  989. struct mptsas_target_reset_event *target_reset_list;
  990. u8 id, channel;
  991. id = sas_event_data->TargetID;
  992. channel = sas_event_data->Bus;
  993. vtarget = mptsas_find_vtarget(ioc, channel, id);
  994. if (vtarget) {
  995. mptsas_block_io_starget(vtarget->starget);
  996. vtarget->deleted = 1; /* block IO */
  997. }
  998. target_reset_list = kzalloc(sizeof(struct mptsas_target_reset_event),
  999. GFP_ATOMIC);
  1000. if (!target_reset_list) {
  1001. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT
  1002. "%s, failed to allocate mem @%d..!!\n",
  1003. ioc->name, __func__, __LINE__));
  1004. return;
  1005. }
  1006. memcpy(&target_reset_list->sas_event_data, sas_event_data,
  1007. sizeof(*sas_event_data));
  1008. list_add_tail(&target_reset_list->list, &hd->target_reset_list);
  1009. target_reset_list->time_count = jiffies;
  1010. if (mptsas_target_reset(ioc, channel, id)) {
  1011. target_reset_list->target_reset_issued = 1;
  1012. }
  1013. }
  1014. /**
  1015. * mptsas_schedule_target_reset- send pending target reset
  1016. * @iocp: per adapter object
  1017. *
  1018. * This function will delete scheduled target reset from the list and
  1019. * try to send next target reset. This will be called from completion
  1020. * context of any Task management command.
  1021. */
  1022. void
  1023. mptsas_schedule_target_reset(void *iocp)
  1024. {
  1025. MPT_ADAPTER *ioc = (MPT_ADAPTER *)(iocp);
  1026. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  1027. struct list_head *head = &hd->target_reset_list;
  1028. struct mptsas_target_reset_event *target_reset_list;
  1029. u8 id, channel;
  1030. /*
  1031. * issue target reset to next device in the queue
  1032. */
  1033. head = &hd->target_reset_list;
  1034. if (list_empty(head))
  1035. return;
  1036. target_reset_list = list_entry(head->next,
  1037. struct mptsas_target_reset_event, list);
  1038. id = target_reset_list->sas_event_data.TargetID;
  1039. channel = target_reset_list->sas_event_data.Bus;
  1040. target_reset_list->time_count = jiffies;
  1041. if (mptsas_target_reset(ioc, channel, id))
  1042. target_reset_list->target_reset_issued = 1;
  1043. return;
  1044. }
  1045. /**
  1046. * mptsas_taskmgmt_complete - complete SAS task management function
  1047. * @ioc: Pointer to MPT_ADAPTER structure
  1048. *
  1049. * Completion for TARGET_RESET after NOT_RESPONDING_EVENT, enable work
  1050. * queue to finish off removing device from upper layers. then send next
  1051. * TARGET_RESET in the queue.
  1052. **/
  1053. static int
  1054. mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  1055. {
  1056. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  1057. struct list_head *head = &hd->target_reset_list;
  1058. u8 id, channel;
  1059. struct mptsas_target_reset_event *target_reset_list;
  1060. SCSITaskMgmtReply_t *pScsiTmReply;
  1061. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt completed: "
  1062. "(mf = %p, mr = %p)\n", ioc->name, mf, mr));
  1063. pScsiTmReply = (SCSITaskMgmtReply_t *)mr;
  1064. if (!pScsiTmReply)
  1065. return 0;
  1066. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1067. "\tTaskMgmt completed: fw_channel = %d, fw_id = %d,\n"
  1068. "\ttask_type = 0x%02X, iocstatus = 0x%04X "
  1069. "loginfo = 0x%08X,\n\tresponse_code = 0x%02X, "
  1070. "term_cmnds = %d\n", ioc->name,
  1071. pScsiTmReply->Bus, pScsiTmReply->TargetID,
  1072. pScsiTmReply->TaskType,
  1073. le16_to_cpu(pScsiTmReply->IOCStatus),
  1074. le32_to_cpu(pScsiTmReply->IOCLogInfo),
  1075. pScsiTmReply->ResponseCode,
  1076. le32_to_cpu(pScsiTmReply->TerminationCount)));
  1077. if (pScsiTmReply->ResponseCode)
  1078. mptscsih_taskmgmt_response_code(ioc,
  1079. pScsiTmReply->ResponseCode);
  1080. if (pScsiTmReply->TaskType ==
  1081. MPI_SCSITASKMGMT_TASKTYPE_QUERY_TASK || pScsiTmReply->TaskType ==
  1082. MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET) {
  1083. ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
  1084. ioc->taskmgmt_cmds.status |= MPT_MGMT_STATUS_RF_VALID;
  1085. memcpy(ioc->taskmgmt_cmds.reply, mr,
  1086. min(MPT_DEFAULT_FRAME_SIZE, 4 * mr->u.reply.MsgLength));
  1087. if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_PENDING) {
  1088. ioc->taskmgmt_cmds.status &= ~MPT_MGMT_STATUS_PENDING;
  1089. complete(&ioc->taskmgmt_cmds.done);
  1090. return 1;
  1091. }
  1092. return 0;
  1093. }
  1094. mpt_clear_taskmgmt_in_progress_flag(ioc);
  1095. if (list_empty(head))
  1096. return 1;
  1097. target_reset_list = list_entry(head->next,
  1098. struct mptsas_target_reset_event, list);
  1099. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1100. "TaskMgmt: completed (%d seconds)\n",
  1101. ioc->name, jiffies_to_msecs(jiffies -
  1102. target_reset_list->time_count)/1000));
  1103. id = pScsiTmReply->TargetID;
  1104. channel = pScsiTmReply->Bus;
  1105. target_reset_list->time_count = jiffies;
  1106. /*
  1107. * retry target reset
  1108. */
  1109. if (!target_reset_list->target_reset_issued) {
  1110. if (mptsas_target_reset(ioc, channel, id))
  1111. target_reset_list->target_reset_issued = 1;
  1112. return 1;
  1113. }
  1114. /*
  1115. * enable work queue to remove device from upper layers
  1116. */
  1117. list_del(&target_reset_list->list);
  1118. if (!ioc->fw_events_off)
  1119. mptsas_queue_device_delete(ioc,
  1120. &target_reset_list->sas_event_data);
  1121. ioc->schedule_target_reset(ioc);
  1122. return 1;
  1123. }
  1124. /**
  1125. * mptscsih_ioc_reset
  1126. *
  1127. * @ioc
  1128. * @reset_phase
  1129. *
  1130. **/
  1131. static int
  1132. mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  1133. {
  1134. MPT_SCSI_HOST *hd;
  1135. int rc;
  1136. rc = mptscsih_ioc_reset(ioc, reset_phase);
  1137. if ((ioc->bus_type != SAS) || (!rc))
  1138. return rc;
  1139. hd = shost_priv(ioc->sh);
  1140. if (!hd->ioc)
  1141. goto out;
  1142. switch (reset_phase) {
  1143. case MPT_IOC_SETUP_RESET:
  1144. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1145. "%s: MPT_IOC_SETUP_RESET\n", ioc->name, __func__));
  1146. mptsas_fw_event_off(ioc);
  1147. break;
  1148. case MPT_IOC_PRE_RESET:
  1149. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1150. "%s: MPT_IOC_PRE_RESET\n", ioc->name, __func__));
  1151. break;
  1152. case MPT_IOC_POST_RESET:
  1153. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1154. "%s: MPT_IOC_POST_RESET\n", ioc->name, __func__));
  1155. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_PENDING) {
  1156. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_DID_IOCRESET;
  1157. complete(&ioc->sas_mgmt.done);
  1158. }
  1159. mptsas_cleanup_fw_event_q(ioc);
  1160. mptsas_queue_rescan(ioc);
  1161. break;
  1162. default:
  1163. break;
  1164. }
  1165. out:
  1166. return rc;
  1167. }
  1168. /**
  1169. * enum device_state -
  1170. * @DEVICE_RETRY: need to retry the TUR
  1171. * @DEVICE_ERROR: TUR return error, don't add device
  1172. * @DEVICE_READY: device can be added
  1173. *
  1174. */
  1175. enum device_state{
  1176. DEVICE_RETRY,
  1177. DEVICE_ERROR,
  1178. DEVICE_READY,
  1179. };
  1180. static int
  1181. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  1182. u32 form, u32 form_specific)
  1183. {
  1184. ConfigExtendedPageHeader_t hdr;
  1185. CONFIGPARMS cfg;
  1186. SasEnclosurePage0_t *buffer;
  1187. dma_addr_t dma_handle;
  1188. int error;
  1189. __le64 le_identifier;
  1190. memset(&hdr, 0, sizeof(hdr));
  1191. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  1192. hdr.PageNumber = 0;
  1193. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1194. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  1195. cfg.cfghdr.ehdr = &hdr;
  1196. cfg.physAddr = -1;
  1197. cfg.pageAddr = form + form_specific;
  1198. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1199. cfg.dir = 0; /* read */
  1200. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  1201. error = mpt_config(ioc, &cfg);
  1202. if (error)
  1203. goto out;
  1204. if (!hdr.ExtPageLength) {
  1205. error = -ENXIO;
  1206. goto out;
  1207. }
  1208. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1209. &dma_handle);
  1210. if (!buffer) {
  1211. error = -ENOMEM;
  1212. goto out;
  1213. }
  1214. cfg.physAddr = dma_handle;
  1215. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1216. error = mpt_config(ioc, &cfg);
  1217. if (error)
  1218. goto out_free_consistent;
  1219. /* save config data */
  1220. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  1221. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  1222. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  1223. enclosure->flags = le16_to_cpu(buffer->Flags);
  1224. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  1225. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  1226. enclosure->start_id = buffer->StartTargetID;
  1227. enclosure->start_channel = buffer->StartBus;
  1228. enclosure->sep_id = buffer->SEPTargetID;
  1229. enclosure->sep_channel = buffer->SEPBus;
  1230. out_free_consistent:
  1231. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1232. buffer, dma_handle);
  1233. out:
  1234. return error;
  1235. }
  1236. /**
  1237. * mptsas_add_end_device - report a new end device to sas transport layer
  1238. * @ioc: Pointer to MPT_ADAPTER structure
  1239. * @phy_info: describes attached device
  1240. *
  1241. * return (0) success (1) failure
  1242. *
  1243. **/
  1244. static int
  1245. mptsas_add_end_device(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info)
  1246. {
  1247. struct sas_rphy *rphy;
  1248. struct sas_port *port;
  1249. struct sas_identify identify;
  1250. char *ds = NULL;
  1251. u8 fw_id;
  1252. if (!phy_info) {
  1253. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1254. "%s: exit at line=%d\n", ioc->name,
  1255. __func__, __LINE__));
  1256. return 1;
  1257. }
  1258. fw_id = phy_info->attached.id;
  1259. if (mptsas_get_rphy(phy_info)) {
  1260. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1261. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1262. __func__, fw_id, __LINE__));
  1263. return 2;
  1264. }
  1265. port = mptsas_get_port(phy_info);
  1266. if (!port) {
  1267. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1268. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1269. __func__, fw_id, __LINE__));
  1270. return 3;
  1271. }
  1272. if (phy_info->attached.device_info &
  1273. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1274. ds = "ssp";
  1275. if (phy_info->attached.device_info &
  1276. MPI_SAS_DEVICE_INFO_STP_TARGET)
  1277. ds = "stp";
  1278. if (phy_info->attached.device_info &
  1279. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1280. ds = "sata";
  1281. printk(MYIOC_s_INFO_FMT "attaching %s device: fw_channel %d, fw_id %d,"
  1282. " phy %d, sas_addr 0x%llx\n", ioc->name, ds,
  1283. phy_info->attached.channel, phy_info->attached.id,
  1284. phy_info->attached.phy_id, (unsigned long long)
  1285. phy_info->attached.sas_address);
  1286. mptsas_parse_device_info(&identify, &phy_info->attached);
  1287. rphy = sas_end_device_alloc(port);
  1288. if (!rphy) {
  1289. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1290. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1291. __func__, fw_id, __LINE__));
  1292. return 5; /* non-fatal: an rphy can be added later */
  1293. }
  1294. rphy->identify = identify;
  1295. if (sas_rphy_add(rphy)) {
  1296. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1297. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1298. __func__, fw_id, __LINE__));
  1299. sas_rphy_free(rphy);
  1300. return 6;
  1301. }
  1302. mptsas_set_rphy(ioc, phy_info, rphy);
  1303. return 0;
  1304. }
  1305. /**
  1306. * mptsas_del_end_device - report a deleted end device to sas transport layer
  1307. * @ioc: Pointer to MPT_ADAPTER structure
  1308. * @phy_info: describes attached device
  1309. *
  1310. **/
  1311. static void
  1312. mptsas_del_end_device(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info)
  1313. {
  1314. struct sas_rphy *rphy;
  1315. struct sas_port *port;
  1316. struct mptsas_portinfo *port_info;
  1317. struct mptsas_phyinfo *phy_info_parent;
  1318. int i;
  1319. char *ds = NULL;
  1320. u8 fw_id;
  1321. u64 sas_address;
  1322. if (!phy_info)
  1323. return;
  1324. fw_id = phy_info->attached.id;
  1325. sas_address = phy_info->attached.sas_address;
  1326. if (!phy_info->port_details) {
  1327. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1328. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1329. __func__, fw_id, __LINE__));
  1330. return;
  1331. }
  1332. rphy = mptsas_get_rphy(phy_info);
  1333. if (!rphy) {
  1334. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1335. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1336. __func__, fw_id, __LINE__));
  1337. return;
  1338. }
  1339. if (phy_info->attached.device_info & MPI_SAS_DEVICE_INFO_SSP_INITIATOR
  1340. || phy_info->attached.device_info
  1341. & MPI_SAS_DEVICE_INFO_SMP_INITIATOR
  1342. || phy_info->attached.device_info
  1343. & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  1344. ds = "initiator";
  1345. if (phy_info->attached.device_info &
  1346. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1347. ds = "ssp";
  1348. if (phy_info->attached.device_info &
  1349. MPI_SAS_DEVICE_INFO_STP_TARGET)
  1350. ds = "stp";
  1351. if (phy_info->attached.device_info &
  1352. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1353. ds = "sata";
  1354. dev_printk(KERN_DEBUG, &rphy->dev, MYIOC_s_FMT
  1355. "removing %s device: fw_channel %d, fw_id %d, phy %d,"
  1356. "sas_addr 0x%llx\n", ioc->name, ds, phy_info->attached.channel,
  1357. phy_info->attached.id, phy_info->attached.phy_id,
  1358. (unsigned long long) sas_address);
  1359. port = mptsas_get_port(phy_info);
  1360. if (!port) {
  1361. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1362. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  1363. __func__, fw_id, __LINE__));
  1364. return;
  1365. }
  1366. port_info = phy_info->portinfo;
  1367. phy_info_parent = port_info->phy_info;
  1368. for (i = 0; i < port_info->num_phys; i++, phy_info_parent++) {
  1369. if (!phy_info_parent->phy)
  1370. continue;
  1371. if (phy_info_parent->attached.sas_address !=
  1372. sas_address)
  1373. continue;
  1374. dev_printk(KERN_DEBUG, &phy_info_parent->phy->dev,
  1375. MYIOC_s_FMT "delete phy %d, phy-obj (0x%p)\n",
  1376. ioc->name, phy_info_parent->phy_id,
  1377. phy_info_parent->phy);
  1378. sas_port_delete_phy(port, phy_info_parent->phy);
  1379. }
  1380. dev_printk(KERN_DEBUG, &port->dev, MYIOC_s_FMT
  1381. "delete port %d, sas_addr (0x%llx)\n", ioc->name,
  1382. port->port_identifier, (unsigned long long)sas_address);
  1383. sas_port_delete(port);
  1384. mptsas_set_port(ioc, phy_info, NULL);
  1385. mptsas_port_delete(ioc, phy_info->port_details);
  1386. }
  1387. static struct mptsas_phyinfo *
  1388. mptsas_refreshing_device_handles(MPT_ADAPTER *ioc,
  1389. struct mptsas_devinfo *sas_device)
  1390. {
  1391. struct mptsas_phyinfo *phy_info;
  1392. struct mptsas_portinfo *port_info;
  1393. int i;
  1394. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  1395. sas_device->sas_address);
  1396. if (!phy_info)
  1397. goto out;
  1398. port_info = phy_info->portinfo;
  1399. if (!port_info)
  1400. goto out;
  1401. mutex_lock(&ioc->sas_topology_mutex);
  1402. for (i = 0; i < port_info->num_phys; i++) {
  1403. if (port_info->phy_info[i].attached.sas_address !=
  1404. sas_device->sas_address)
  1405. continue;
  1406. port_info->phy_info[i].attached.channel = sas_device->channel;
  1407. port_info->phy_info[i].attached.id = sas_device->id;
  1408. port_info->phy_info[i].attached.sas_address =
  1409. sas_device->sas_address;
  1410. port_info->phy_info[i].attached.handle = sas_device->handle;
  1411. port_info->phy_info[i].attached.handle_parent =
  1412. sas_device->handle_parent;
  1413. port_info->phy_info[i].attached.handle_enclosure =
  1414. sas_device->handle_enclosure;
  1415. }
  1416. mutex_unlock(&ioc->sas_topology_mutex);
  1417. out:
  1418. return phy_info;
  1419. }
  1420. /**
  1421. * mptsas_firmware_event_work - work thread for processing fw events
  1422. * @work: work queue payload containing info describing the event
  1423. * Context: user
  1424. *
  1425. */
  1426. static void
  1427. mptsas_firmware_event_work(struct work_struct *work)
  1428. {
  1429. struct fw_event_work *fw_event =
  1430. container_of(work, struct fw_event_work, work.work);
  1431. MPT_ADAPTER *ioc = fw_event->ioc;
  1432. /* special rescan topology handling */
  1433. if (fw_event->event == -1) {
  1434. if (ioc->in_rescan) {
  1435. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1436. "%s: rescan ignored as it is in progress\n",
  1437. ioc->name, __func__));
  1438. return;
  1439. }
  1440. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: rescan after "
  1441. "reset\n", ioc->name, __func__));
  1442. ioc->in_rescan = 1;
  1443. mptsas_not_responding_devices(ioc);
  1444. mptsas_scan_sas_topology(ioc);
  1445. ioc->in_rescan = 0;
  1446. mptsas_free_fw_event(ioc, fw_event);
  1447. mptsas_fw_event_on(ioc);
  1448. return;
  1449. }
  1450. /* events handling turned off during host reset */
  1451. if (ioc->fw_events_off) {
  1452. mptsas_free_fw_event(ioc, fw_event);
  1453. return;
  1454. }
  1455. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: fw_event=(0x%p), "
  1456. "event = (0x%02x)\n", ioc->name, __func__, fw_event,
  1457. (fw_event->event & 0xFF)));
  1458. switch (fw_event->event) {
  1459. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  1460. mptsas_send_sas_event(fw_event);
  1461. break;
  1462. case MPI_EVENT_INTEGRATED_RAID:
  1463. mptsas_send_raid_event(fw_event);
  1464. break;
  1465. case MPI_EVENT_IR2:
  1466. mptsas_send_ir2_event(fw_event);
  1467. break;
  1468. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  1469. mptbase_sas_persist_operation(ioc,
  1470. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  1471. mptsas_free_fw_event(ioc, fw_event);
  1472. break;
  1473. case MPI_EVENT_SAS_BROADCAST_PRIMITIVE:
  1474. mptsas_broadcast_primative_work(fw_event);
  1475. break;
  1476. case MPI_EVENT_SAS_EXPANDER_STATUS_CHANGE:
  1477. mptsas_send_expander_event(fw_event);
  1478. break;
  1479. case MPI_EVENT_SAS_PHY_LINK_STATUS:
  1480. mptsas_send_link_status_event(fw_event);
  1481. break;
  1482. case MPI_EVENT_QUEUE_FULL:
  1483. mptsas_handle_queue_full_event(fw_event);
  1484. break;
  1485. }
  1486. }
  1487. static int
  1488. mptsas_slave_configure(struct scsi_device *sdev)
  1489. {
  1490. struct Scsi_Host *host = sdev->host;
  1491. MPT_SCSI_HOST *hd = shost_priv(host);
  1492. MPT_ADAPTER *ioc = hd->ioc;
  1493. VirtDevice *vdevice = sdev->hostdata;
  1494. if (vdevice->vtarget->deleted) {
  1495. sdev_printk(KERN_INFO, sdev, "clearing deleted flag\n");
  1496. vdevice->vtarget->deleted = 0;
  1497. }
  1498. /*
  1499. * RAID volumes placed beyond the last expected port.
  1500. * Ignore sending sas mode pages in that case..
  1501. */
  1502. if (sdev->channel == MPTSAS_RAID_CHANNEL) {
  1503. mptsas_add_device_component_starget_ir(ioc, scsi_target(sdev));
  1504. goto out;
  1505. }
  1506. sas_read_port_mode_page(sdev);
  1507. mptsas_add_device_component_starget(ioc, scsi_target(sdev));
  1508. out:
  1509. return mptscsih_slave_configure(sdev);
  1510. }
  1511. static int
  1512. mptsas_target_alloc(struct scsi_target *starget)
  1513. {
  1514. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  1515. MPT_SCSI_HOST *hd = shost_priv(host);
  1516. VirtTarget *vtarget;
  1517. u8 id, channel;
  1518. struct sas_rphy *rphy;
  1519. struct mptsas_portinfo *p;
  1520. int i;
  1521. MPT_ADAPTER *ioc = hd->ioc;
  1522. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  1523. if (!vtarget)
  1524. return -ENOMEM;
  1525. vtarget->starget = starget;
  1526. vtarget->ioc_id = ioc->id;
  1527. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  1528. id = starget->id;
  1529. channel = 0;
  1530. /*
  1531. * RAID volumes placed beyond the last expected port.
  1532. */
  1533. if (starget->channel == MPTSAS_RAID_CHANNEL) {
  1534. if (!ioc->raid_data.pIocPg2) {
  1535. kfree(vtarget);
  1536. return -ENXIO;
  1537. }
  1538. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  1539. if (id == ioc->raid_data.pIocPg2->
  1540. RaidVolume[i].VolumeID) {
  1541. channel = ioc->raid_data.pIocPg2->
  1542. RaidVolume[i].VolumeBus;
  1543. }
  1544. }
  1545. vtarget->raidVolume = 1;
  1546. goto out;
  1547. }
  1548. rphy = dev_to_rphy(starget->dev.parent);
  1549. mutex_lock(&ioc->sas_topology_mutex);
  1550. list_for_each_entry(p, &ioc->sas_topology, list) {
  1551. for (i = 0; i < p->num_phys; i++) {
  1552. if (p->phy_info[i].attached.sas_address !=
  1553. rphy->identify.sas_address)
  1554. continue;
  1555. id = p->phy_info[i].attached.id;
  1556. channel = p->phy_info[i].attached.channel;
  1557. mptsas_set_starget(&p->phy_info[i], starget);
  1558. /*
  1559. * Exposing hidden raid components
  1560. */
  1561. if (mptscsih_is_phys_disk(ioc, channel, id)) {
  1562. id = mptscsih_raid_id_to_num(ioc,
  1563. channel, id);
  1564. vtarget->tflags |=
  1565. MPT_TARGET_FLAGS_RAID_COMPONENT;
  1566. p->phy_info[i].attached.phys_disk_num = id;
  1567. }
  1568. mutex_unlock(&ioc->sas_topology_mutex);
  1569. goto out;
  1570. }
  1571. }
  1572. mutex_unlock(&ioc->sas_topology_mutex);
  1573. kfree(vtarget);
  1574. return -ENXIO;
  1575. out:
  1576. vtarget->id = id;
  1577. vtarget->channel = channel;
  1578. starget->hostdata = vtarget;
  1579. return 0;
  1580. }
  1581. static void
  1582. mptsas_target_destroy(struct scsi_target *starget)
  1583. {
  1584. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  1585. MPT_SCSI_HOST *hd = shost_priv(host);
  1586. struct sas_rphy *rphy;
  1587. struct mptsas_portinfo *p;
  1588. int i;
  1589. MPT_ADAPTER *ioc = hd->ioc;
  1590. VirtTarget *vtarget;
  1591. if (!starget->hostdata)
  1592. return;
  1593. vtarget = starget->hostdata;
  1594. mptsas_del_device_component_by_os(ioc, starget->channel,
  1595. starget->id);
  1596. if (starget->channel == MPTSAS_RAID_CHANNEL)
  1597. goto out;
  1598. rphy = dev_to_rphy(starget->dev.parent);
  1599. list_for_each_entry(p, &ioc->sas_topology, list) {
  1600. for (i = 0; i < p->num_phys; i++) {
  1601. if (p->phy_info[i].attached.sas_address !=
  1602. rphy->identify.sas_address)
  1603. continue;
  1604. starget_printk(KERN_INFO, starget, MYIOC_s_FMT
  1605. "delete device: fw_channel %d, fw_id %d, phy %d, "
  1606. "sas_addr 0x%llx\n", ioc->name,
  1607. p->phy_info[i].attached.channel,
  1608. p->phy_info[i].attached.id,
  1609. p->phy_info[i].attached.phy_id, (unsigned long long)
  1610. p->phy_info[i].attached.sas_address);
  1611. mptsas_set_starget(&p->phy_info[i], NULL);
  1612. }
  1613. }
  1614. out:
  1615. vtarget->starget = NULL;
  1616. kfree(starget->hostdata);
  1617. starget->hostdata = NULL;
  1618. }
  1619. static int
  1620. mptsas_slave_alloc(struct scsi_device *sdev)
  1621. {
  1622. struct Scsi_Host *host = sdev->host;
  1623. MPT_SCSI_HOST *hd = shost_priv(host);
  1624. struct sas_rphy *rphy;
  1625. struct mptsas_portinfo *p;
  1626. VirtDevice *vdevice;
  1627. struct scsi_target *starget;
  1628. int i;
  1629. MPT_ADAPTER *ioc = hd->ioc;
  1630. vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  1631. if (!vdevice) {
  1632. printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
  1633. ioc->name, sizeof(VirtDevice));
  1634. return -ENOMEM;
  1635. }
  1636. starget = scsi_target(sdev);
  1637. vdevice->vtarget = starget->hostdata;
  1638. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  1639. goto out;
  1640. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  1641. mutex_lock(&ioc->sas_topology_mutex);
  1642. list_for_each_entry(p, &ioc->sas_topology, list) {
  1643. for (i = 0; i < p->num_phys; i++) {
  1644. if (p->phy_info[i].attached.sas_address !=
  1645. rphy->identify.sas_address)
  1646. continue;
  1647. vdevice->lun = sdev->lun;
  1648. /*
  1649. * Exposing hidden raid components
  1650. */
  1651. if (mptscsih_is_phys_disk(ioc,
  1652. p->phy_info[i].attached.channel,
  1653. p->phy_info[i].attached.id))
  1654. sdev->no_uld_attach = 1;
  1655. mutex_unlock(&ioc->sas_topology_mutex);
  1656. goto out;
  1657. }
  1658. }
  1659. mutex_unlock(&ioc->sas_topology_mutex);
  1660. kfree(vdevice);
  1661. return -ENXIO;
  1662. out:
  1663. vdevice->vtarget->num_luns++;
  1664. sdev->hostdata = vdevice;
  1665. return 0;
  1666. }
  1667. static int
  1668. mptsas_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *SCpnt)
  1669. {
  1670. MPT_SCSI_HOST *hd;
  1671. MPT_ADAPTER *ioc;
  1672. VirtDevice *vdevice = SCpnt->device->hostdata;
  1673. if (!vdevice || !vdevice->vtarget || vdevice->vtarget->deleted) {
  1674. SCpnt->result = DID_NO_CONNECT << 16;
  1675. SCpnt->scsi_done(SCpnt);
  1676. return 0;
  1677. }
  1678. hd = shost_priv(shost);
  1679. ioc = hd->ioc;
  1680. if (ioc->sas_discovery_quiesce_io)
  1681. return SCSI_MLQUEUE_HOST_BUSY;
  1682. if (ioc->debug_level & MPT_DEBUG_SCSI)
  1683. scsi_print_command(SCpnt);
  1684. return mptscsih_qcmd(SCpnt);
  1685. }
  1686. /**
  1687. * mptsas_mptsas_eh_timed_out - resets the scsi_cmnd timeout
  1688. * if the device under question is currently in the
  1689. * device removal delay.
  1690. * @sc: scsi command that the midlayer is about to time out
  1691. *
  1692. **/
  1693. static enum blk_eh_timer_return mptsas_eh_timed_out(struct scsi_cmnd *sc)
  1694. {
  1695. MPT_SCSI_HOST *hd;
  1696. MPT_ADAPTER *ioc;
  1697. VirtDevice *vdevice;
  1698. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1699. hd = shost_priv(sc->device->host);
  1700. if (hd == NULL) {
  1701. printk(KERN_ERR MYNAM ": %s: Can't locate host! (sc=%p)\n",
  1702. __func__, sc);
  1703. goto done;
  1704. }
  1705. ioc = hd->ioc;
  1706. if (ioc->bus_type != SAS) {
  1707. printk(KERN_ERR MYNAM ": %s: Wrong bus type (sc=%p)\n",
  1708. __func__, sc);
  1709. goto done;
  1710. }
  1711. /* In case if IOC is in reset from internal context.
  1712. * Do not execute EEH for the same IOC. SML should to reset timer.
  1713. */
  1714. if (ioc->ioc_reset_in_progress) {
  1715. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT ": %s: ioc is in reset,"
  1716. "SML need to reset the timer (sc=%p)\n",
  1717. ioc->name, __func__, sc));
  1718. rc = BLK_EH_RESET_TIMER;
  1719. }
  1720. vdevice = sc->device->hostdata;
  1721. if (vdevice && vdevice->vtarget && (vdevice->vtarget->inDMD
  1722. || vdevice->vtarget->deleted)) {
  1723. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT ": %s: target removed "
  1724. "or in device removal delay (sc=%p)\n",
  1725. ioc->name, __func__, sc));
  1726. rc = BLK_EH_RESET_TIMER;
  1727. goto done;
  1728. }
  1729. done:
  1730. return rc;
  1731. }
  1732. static struct scsi_host_template mptsas_driver_template = {
  1733. .module = THIS_MODULE,
  1734. .proc_name = "mptsas",
  1735. .show_info = mptscsih_show_info,
  1736. .name = "MPT SAS Host",
  1737. .info = mptscsih_info,
  1738. .queuecommand = mptsas_qcmd,
  1739. .target_alloc = mptsas_target_alloc,
  1740. .slave_alloc = mptsas_slave_alloc,
  1741. .slave_configure = mptsas_slave_configure,
  1742. .target_destroy = mptsas_target_destroy,
  1743. .slave_destroy = mptscsih_slave_destroy,
  1744. .change_queue_depth = mptscsih_change_queue_depth,
  1745. .eh_abort_handler = mptscsih_abort,
  1746. .eh_device_reset_handler = mptscsih_dev_reset,
  1747. .eh_host_reset_handler = mptscsih_host_reset,
  1748. .bios_param = mptscsih_bios_param,
  1749. .can_queue = MPT_SAS_CAN_QUEUE,
  1750. .this_id = -1,
  1751. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  1752. .max_sectors = 8192,
  1753. .cmd_per_lun = 7,
  1754. .use_clustering = ENABLE_CLUSTERING,
  1755. .shost_attrs = mptscsih_host_attrs,
  1756. .no_write_same = 1,
  1757. };
  1758. static int mptsas_get_linkerrors(struct sas_phy *phy)
  1759. {
  1760. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  1761. ConfigExtendedPageHeader_t hdr;
  1762. CONFIGPARMS cfg;
  1763. SasPhyPage1_t *buffer;
  1764. dma_addr_t dma_handle;
  1765. int error;
  1766. /* FIXME: only have link errors on local phys */
  1767. if (!scsi_is_sas_phy_local(phy))
  1768. return -EINVAL;
  1769. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  1770. hdr.ExtPageLength = 0;
  1771. hdr.PageNumber = 1 /* page number 1*/;
  1772. hdr.Reserved1 = 0;
  1773. hdr.Reserved2 = 0;
  1774. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1775. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  1776. cfg.cfghdr.ehdr = &hdr;
  1777. cfg.physAddr = -1;
  1778. cfg.pageAddr = phy->identify.phy_identifier;
  1779. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1780. cfg.dir = 0; /* read */
  1781. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  1782. error = mpt_config(ioc, &cfg);
  1783. if (error)
  1784. return error;
  1785. if (!hdr.ExtPageLength)
  1786. return -ENXIO;
  1787. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1788. &dma_handle);
  1789. if (!buffer)
  1790. return -ENOMEM;
  1791. cfg.physAddr = dma_handle;
  1792. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1793. error = mpt_config(ioc, &cfg);
  1794. if (error)
  1795. goto out_free_consistent;
  1796. mptsas_print_phy_pg1(ioc, buffer);
  1797. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  1798. phy->running_disparity_error_count =
  1799. le32_to_cpu(buffer->RunningDisparityErrorCount);
  1800. phy->loss_of_dword_sync_count =
  1801. le32_to_cpu(buffer->LossDwordSynchCount);
  1802. phy->phy_reset_problem_count =
  1803. le32_to_cpu(buffer->PhyResetProblemCount);
  1804. out_free_consistent:
  1805. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1806. buffer, dma_handle);
  1807. return error;
  1808. }
  1809. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  1810. MPT_FRAME_HDR *reply)
  1811. {
  1812. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_COMMAND_GOOD;
  1813. if (reply != NULL) {
  1814. ioc->sas_mgmt.status |= MPT_MGMT_STATUS_RF_VALID;
  1815. memcpy(ioc->sas_mgmt.reply, reply,
  1816. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  1817. }
  1818. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_PENDING) {
  1819. ioc->sas_mgmt.status &= ~MPT_MGMT_STATUS_PENDING;
  1820. complete(&ioc->sas_mgmt.done);
  1821. return 1;
  1822. }
  1823. return 0;
  1824. }
  1825. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  1826. {
  1827. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  1828. SasIoUnitControlRequest_t *req;
  1829. SasIoUnitControlReply_t *reply;
  1830. MPT_FRAME_HDR *mf;
  1831. MPIHeader_t *hdr;
  1832. unsigned long timeleft;
  1833. int error = -ERESTARTSYS;
  1834. /* FIXME: fusion doesn't allow non-local phy reset */
  1835. if (!scsi_is_sas_phy_local(phy))
  1836. return -EINVAL;
  1837. /* not implemented for expanders */
  1838. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  1839. return -ENXIO;
  1840. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  1841. goto out;
  1842. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1843. if (!mf) {
  1844. error = -ENOMEM;
  1845. goto out_unlock;
  1846. }
  1847. hdr = (MPIHeader_t *) mf;
  1848. req = (SasIoUnitControlRequest_t *)mf;
  1849. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  1850. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  1851. req->MsgContext = hdr->MsgContext;
  1852. req->Operation = hard_reset ?
  1853. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  1854. req->PhyNum = phy->identify.phy_identifier;
  1855. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  1856. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1857. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  1858. 10 * HZ);
  1859. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  1860. error = -ETIME;
  1861. mpt_free_msg_frame(ioc, mf);
  1862. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  1863. goto out_unlock;
  1864. if (!timeleft)
  1865. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  1866. goto out_unlock;
  1867. }
  1868. /* a reply frame is expected */
  1869. if ((ioc->sas_mgmt.status &
  1870. MPT_MGMT_STATUS_RF_VALID) == 0) {
  1871. error = -ENXIO;
  1872. goto out_unlock;
  1873. }
  1874. /* process the completed Reply Message Frame */
  1875. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  1876. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  1877. printk(MYIOC_s_INFO_FMT "%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  1878. ioc->name, __func__, reply->IOCStatus, reply->IOCLogInfo);
  1879. error = -ENXIO;
  1880. goto out_unlock;
  1881. }
  1882. error = 0;
  1883. out_unlock:
  1884. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  1885. mutex_unlock(&ioc->sas_mgmt.mutex);
  1886. out:
  1887. return error;
  1888. }
  1889. static int
  1890. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1891. {
  1892. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1893. int i, error;
  1894. struct mptsas_portinfo *p;
  1895. struct mptsas_enclosure enclosure_info;
  1896. u64 enclosure_handle;
  1897. mutex_lock(&ioc->sas_topology_mutex);
  1898. list_for_each_entry(p, &ioc->sas_topology, list) {
  1899. for (i = 0; i < p->num_phys; i++) {
  1900. if (p->phy_info[i].attached.sas_address ==
  1901. rphy->identify.sas_address) {
  1902. enclosure_handle = p->phy_info[i].
  1903. attached.handle_enclosure;
  1904. goto found_info;
  1905. }
  1906. }
  1907. }
  1908. mutex_unlock(&ioc->sas_topology_mutex);
  1909. return -ENXIO;
  1910. found_info:
  1911. mutex_unlock(&ioc->sas_topology_mutex);
  1912. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  1913. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  1914. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  1915. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  1916. if (!error)
  1917. *identifier = enclosure_info.enclosure_logical_id;
  1918. return error;
  1919. }
  1920. static int
  1921. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  1922. {
  1923. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1924. struct mptsas_portinfo *p;
  1925. int i, rc;
  1926. mutex_lock(&ioc->sas_topology_mutex);
  1927. list_for_each_entry(p, &ioc->sas_topology, list) {
  1928. for (i = 0; i < p->num_phys; i++) {
  1929. if (p->phy_info[i].attached.sas_address ==
  1930. rphy->identify.sas_address) {
  1931. rc = p->phy_info[i].attached.slot;
  1932. goto out;
  1933. }
  1934. }
  1935. }
  1936. rc = -ENXIO;
  1937. out:
  1938. mutex_unlock(&ioc->sas_topology_mutex);
  1939. return rc;
  1940. }
  1941. static int mptsas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1942. struct request *req)
  1943. {
  1944. MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
  1945. MPT_FRAME_HDR *mf;
  1946. SmpPassthroughRequest_t *smpreq;
  1947. struct request *rsp = req->next_rq;
  1948. int ret;
  1949. int flagsLength;
  1950. unsigned long timeleft;
  1951. char *psge;
  1952. dma_addr_t dma_addr_in = 0;
  1953. dma_addr_t dma_addr_out = 0;
  1954. u64 sas_address = 0;
  1955. if (!rsp) {
  1956. printk(MYIOC_s_ERR_FMT "%s: the smp response space is missing\n",
  1957. ioc->name, __func__);
  1958. return -EINVAL;
  1959. }
  1960. /* do we need to support multiple segments? */
  1961. if (bio_multiple_segments(req->bio) ||
  1962. bio_multiple_segments(rsp->bio)) {
  1963. printk(MYIOC_s_ERR_FMT "%s: multiple segments req %u, rsp %u\n",
  1964. ioc->name, __func__, blk_rq_bytes(req), blk_rq_bytes(rsp));
  1965. return -EINVAL;
  1966. }
  1967. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  1968. if (ret)
  1969. goto out;
  1970. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1971. if (!mf) {
  1972. ret = -ENOMEM;
  1973. goto out_unlock;
  1974. }
  1975. smpreq = (SmpPassthroughRequest_t *)mf;
  1976. memset(smpreq, 0, sizeof(*smpreq));
  1977. smpreq->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1978. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  1979. if (rphy)
  1980. sas_address = rphy->identify.sas_address;
  1981. else {
  1982. struct mptsas_portinfo *port_info;
  1983. mutex_lock(&ioc->sas_topology_mutex);
  1984. port_info = ioc->hba_port_info;
  1985. if (port_info && port_info->phy_info)
  1986. sas_address =
  1987. port_info->phy_info[0].phy->identify.sas_address;
  1988. mutex_unlock(&ioc->sas_topology_mutex);
  1989. }
  1990. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  1991. psge = (char *)
  1992. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  1993. /* request */
  1994. flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  1995. MPI_SGE_FLAGS_END_OF_BUFFER |
  1996. MPI_SGE_FLAGS_DIRECTION)
  1997. << MPI_SGE_FLAGS_SHIFT;
  1998. flagsLength |= (blk_rq_bytes(req) - 4);
  1999. dma_addr_out = pci_map_single(ioc->pcidev, bio_data(req->bio),
  2000. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  2001. if (pci_dma_mapping_error(ioc->pcidev, dma_addr_out))
  2002. goto put_mf;
  2003. ioc->add_sge(psge, flagsLength, dma_addr_out);
  2004. psge += ioc->SGE_size;
  2005. /* response */
  2006. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2007. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2008. MPI_SGE_FLAGS_IOC_TO_HOST |
  2009. MPI_SGE_FLAGS_END_OF_BUFFER;
  2010. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2011. flagsLength |= blk_rq_bytes(rsp) + 4;
  2012. dma_addr_in = pci_map_single(ioc->pcidev, bio_data(rsp->bio),
  2013. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  2014. if (pci_dma_mapping_error(ioc->pcidev, dma_addr_in))
  2015. goto unmap;
  2016. ioc->add_sge(psge, flagsLength, dma_addr_in);
  2017. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  2018. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  2019. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  2020. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  2021. ret = -ETIME;
  2022. mpt_free_msg_frame(ioc, mf);
  2023. mf = NULL;
  2024. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  2025. goto unmap;
  2026. if (!timeleft)
  2027. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  2028. goto unmap;
  2029. }
  2030. mf = NULL;
  2031. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
  2032. SmpPassthroughReply_t *smprep;
  2033. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  2034. memcpy(req->sense, smprep, sizeof(*smprep));
  2035. req->sense_len = sizeof(*smprep);
  2036. req->resid_len = 0;
  2037. rsp->resid_len -= smprep->ResponseDataLength;
  2038. } else {
  2039. printk(MYIOC_s_ERR_FMT
  2040. "%s: smp passthru reply failed to be returned\n",
  2041. ioc->name, __func__);
  2042. ret = -ENXIO;
  2043. }
  2044. unmap:
  2045. if (dma_addr_out)
  2046. pci_unmap_single(ioc->pcidev, dma_addr_out, blk_rq_bytes(req),
  2047. PCI_DMA_BIDIRECTIONAL);
  2048. if (dma_addr_in)
  2049. pci_unmap_single(ioc->pcidev, dma_addr_in, blk_rq_bytes(rsp),
  2050. PCI_DMA_BIDIRECTIONAL);
  2051. put_mf:
  2052. if (mf)
  2053. mpt_free_msg_frame(ioc, mf);
  2054. out_unlock:
  2055. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  2056. mutex_unlock(&ioc->sas_mgmt.mutex);
  2057. out:
  2058. return ret;
  2059. }
  2060. static struct sas_function_template mptsas_transport_functions = {
  2061. .get_linkerrors = mptsas_get_linkerrors,
  2062. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  2063. .get_bay_identifier = mptsas_get_bay_identifier,
  2064. .phy_reset = mptsas_phy_reset,
  2065. .smp_handler = mptsas_smp_handler,
  2066. };
  2067. static struct scsi_transport_template *mptsas_transport_template;
  2068. static int
  2069. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  2070. {
  2071. ConfigExtendedPageHeader_t hdr;
  2072. CONFIGPARMS cfg;
  2073. SasIOUnitPage0_t *buffer;
  2074. dma_addr_t dma_handle;
  2075. int error, i;
  2076. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  2077. hdr.ExtPageLength = 0;
  2078. hdr.PageNumber = 0;
  2079. hdr.Reserved1 = 0;
  2080. hdr.Reserved2 = 0;
  2081. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2082. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  2083. cfg.cfghdr.ehdr = &hdr;
  2084. cfg.physAddr = -1;
  2085. cfg.pageAddr = 0;
  2086. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2087. cfg.dir = 0; /* read */
  2088. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2089. error = mpt_config(ioc, &cfg);
  2090. if (error)
  2091. goto out;
  2092. if (!hdr.ExtPageLength) {
  2093. error = -ENXIO;
  2094. goto out;
  2095. }
  2096. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2097. &dma_handle);
  2098. if (!buffer) {
  2099. error = -ENOMEM;
  2100. goto out;
  2101. }
  2102. cfg.physAddr = dma_handle;
  2103. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2104. error = mpt_config(ioc, &cfg);
  2105. if (error)
  2106. goto out_free_consistent;
  2107. port_info->num_phys = buffer->NumPhys;
  2108. port_info->phy_info = kcalloc(port_info->num_phys,
  2109. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  2110. if (!port_info->phy_info) {
  2111. error = -ENOMEM;
  2112. goto out_free_consistent;
  2113. }
  2114. ioc->nvdata_version_persistent =
  2115. le16_to_cpu(buffer->NvdataVersionPersistent);
  2116. ioc->nvdata_version_default =
  2117. le16_to_cpu(buffer->NvdataVersionDefault);
  2118. for (i = 0; i < port_info->num_phys; i++) {
  2119. mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
  2120. port_info->phy_info[i].phy_id = i;
  2121. port_info->phy_info[i].port_id =
  2122. buffer->PhyData[i].Port;
  2123. port_info->phy_info[i].negotiated_link_rate =
  2124. buffer->PhyData[i].NegotiatedLinkRate;
  2125. port_info->phy_info[i].portinfo = port_info;
  2126. port_info->phy_info[i].handle =
  2127. le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
  2128. }
  2129. out_free_consistent:
  2130. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2131. buffer, dma_handle);
  2132. out:
  2133. return error;
  2134. }
  2135. static int
  2136. mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
  2137. {
  2138. ConfigExtendedPageHeader_t hdr;
  2139. CONFIGPARMS cfg;
  2140. SasIOUnitPage1_t *buffer;
  2141. dma_addr_t dma_handle;
  2142. int error;
  2143. u8 device_missing_delay;
  2144. memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
  2145. memset(&cfg, 0, sizeof(CONFIGPARMS));
  2146. cfg.cfghdr.ehdr = &hdr;
  2147. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2148. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2149. cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2150. cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  2151. cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
  2152. cfg.cfghdr.ehdr->PageNumber = 1;
  2153. error = mpt_config(ioc, &cfg);
  2154. if (error)
  2155. goto out;
  2156. if (!hdr.ExtPageLength) {
  2157. error = -ENXIO;
  2158. goto out;
  2159. }
  2160. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2161. &dma_handle);
  2162. if (!buffer) {
  2163. error = -ENOMEM;
  2164. goto out;
  2165. }
  2166. cfg.physAddr = dma_handle;
  2167. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2168. error = mpt_config(ioc, &cfg);
  2169. if (error)
  2170. goto out_free_consistent;
  2171. ioc->io_missing_delay =
  2172. le16_to_cpu(buffer->IODeviceMissingDelay);
  2173. device_missing_delay = buffer->ReportDeviceMissingDelay;
  2174. ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
  2175. (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
  2176. device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2177. out_free_consistent:
  2178. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2179. buffer, dma_handle);
  2180. out:
  2181. return error;
  2182. }
  2183. static int
  2184. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  2185. u32 form, u32 form_specific)
  2186. {
  2187. ConfigExtendedPageHeader_t hdr;
  2188. CONFIGPARMS cfg;
  2189. SasPhyPage0_t *buffer;
  2190. dma_addr_t dma_handle;
  2191. int error;
  2192. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  2193. hdr.ExtPageLength = 0;
  2194. hdr.PageNumber = 0;
  2195. hdr.Reserved1 = 0;
  2196. hdr.Reserved2 = 0;
  2197. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2198. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  2199. cfg.cfghdr.ehdr = &hdr;
  2200. cfg.dir = 0; /* read */
  2201. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2202. /* Get Phy Pg 0 for each Phy. */
  2203. cfg.physAddr = -1;
  2204. cfg.pageAddr = form + form_specific;
  2205. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2206. error = mpt_config(ioc, &cfg);
  2207. if (error)
  2208. goto out;
  2209. if (!hdr.ExtPageLength) {
  2210. error = -ENXIO;
  2211. goto out;
  2212. }
  2213. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2214. &dma_handle);
  2215. if (!buffer) {
  2216. error = -ENOMEM;
  2217. goto out;
  2218. }
  2219. cfg.physAddr = dma_handle;
  2220. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2221. error = mpt_config(ioc, &cfg);
  2222. if (error)
  2223. goto out_free_consistent;
  2224. mptsas_print_phy_pg0(ioc, buffer);
  2225. phy_info->hw_link_rate = buffer->HwLinkRate;
  2226. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  2227. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  2228. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  2229. out_free_consistent:
  2230. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2231. buffer, dma_handle);
  2232. out:
  2233. return error;
  2234. }
  2235. static int
  2236. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  2237. u32 form, u32 form_specific)
  2238. {
  2239. ConfigExtendedPageHeader_t hdr;
  2240. CONFIGPARMS cfg;
  2241. SasDevicePage0_t *buffer;
  2242. dma_addr_t dma_handle;
  2243. __le64 sas_address;
  2244. int error=0;
  2245. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  2246. hdr.ExtPageLength = 0;
  2247. hdr.PageNumber = 0;
  2248. hdr.Reserved1 = 0;
  2249. hdr.Reserved2 = 0;
  2250. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2251. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  2252. cfg.cfghdr.ehdr = &hdr;
  2253. cfg.pageAddr = form + form_specific;
  2254. cfg.physAddr = -1;
  2255. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2256. cfg.dir = 0; /* read */
  2257. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2258. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  2259. error = mpt_config(ioc, &cfg);
  2260. if (error)
  2261. goto out;
  2262. if (!hdr.ExtPageLength) {
  2263. error = -ENXIO;
  2264. goto out;
  2265. }
  2266. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2267. &dma_handle);
  2268. if (!buffer) {
  2269. error = -ENOMEM;
  2270. goto out;
  2271. }
  2272. cfg.physAddr = dma_handle;
  2273. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2274. error = mpt_config(ioc, &cfg);
  2275. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2276. error = -ENODEV;
  2277. goto out_free_consistent;
  2278. }
  2279. if (error)
  2280. goto out_free_consistent;
  2281. mptsas_print_device_pg0(ioc, buffer);
  2282. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  2283. device_info->handle = le16_to_cpu(buffer->DevHandle);
  2284. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  2285. device_info->handle_enclosure =
  2286. le16_to_cpu(buffer->EnclosureHandle);
  2287. device_info->slot = le16_to_cpu(buffer->Slot);
  2288. device_info->phy_id = buffer->PhyNum;
  2289. device_info->port_id = buffer->PhysicalPort;
  2290. device_info->id = buffer->TargetID;
  2291. device_info->phys_disk_num = ~0;
  2292. device_info->channel = buffer->Bus;
  2293. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  2294. device_info->sas_address = le64_to_cpu(sas_address);
  2295. device_info->device_info =
  2296. le32_to_cpu(buffer->DeviceInfo);
  2297. device_info->flags = le16_to_cpu(buffer->Flags);
  2298. out_free_consistent:
  2299. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2300. buffer, dma_handle);
  2301. out:
  2302. return error;
  2303. }
  2304. static int
  2305. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  2306. u32 form, u32 form_specific)
  2307. {
  2308. ConfigExtendedPageHeader_t hdr;
  2309. CONFIGPARMS cfg;
  2310. SasExpanderPage0_t *buffer;
  2311. dma_addr_t dma_handle;
  2312. int i, error;
  2313. __le64 sas_address;
  2314. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  2315. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  2316. hdr.ExtPageLength = 0;
  2317. hdr.PageNumber = 0;
  2318. hdr.Reserved1 = 0;
  2319. hdr.Reserved2 = 0;
  2320. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2321. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  2322. cfg.cfghdr.ehdr = &hdr;
  2323. cfg.physAddr = -1;
  2324. cfg.pageAddr = form + form_specific;
  2325. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2326. cfg.dir = 0; /* read */
  2327. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2328. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  2329. error = mpt_config(ioc, &cfg);
  2330. if (error)
  2331. goto out;
  2332. if (!hdr.ExtPageLength) {
  2333. error = -ENXIO;
  2334. goto out;
  2335. }
  2336. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2337. &dma_handle);
  2338. if (!buffer) {
  2339. error = -ENOMEM;
  2340. goto out;
  2341. }
  2342. cfg.physAddr = dma_handle;
  2343. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2344. error = mpt_config(ioc, &cfg);
  2345. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2346. error = -ENODEV;
  2347. goto out_free_consistent;
  2348. }
  2349. if (error)
  2350. goto out_free_consistent;
  2351. /* save config data */
  2352. port_info->num_phys = (buffer->NumPhys) ? buffer->NumPhys : 1;
  2353. port_info->phy_info = kcalloc(port_info->num_phys,
  2354. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  2355. if (!port_info->phy_info) {
  2356. error = -ENOMEM;
  2357. goto out_free_consistent;
  2358. }
  2359. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  2360. for (i = 0; i < port_info->num_phys; i++) {
  2361. port_info->phy_info[i].portinfo = port_info;
  2362. port_info->phy_info[i].handle =
  2363. le16_to_cpu(buffer->DevHandle);
  2364. port_info->phy_info[i].identify.sas_address =
  2365. le64_to_cpu(sas_address);
  2366. port_info->phy_info[i].identify.handle_parent =
  2367. le16_to_cpu(buffer->ParentDevHandle);
  2368. }
  2369. out_free_consistent:
  2370. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2371. buffer, dma_handle);
  2372. out:
  2373. return error;
  2374. }
  2375. static int
  2376. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  2377. u32 form, u32 form_specific)
  2378. {
  2379. ConfigExtendedPageHeader_t hdr;
  2380. CONFIGPARMS cfg;
  2381. SasExpanderPage1_t *buffer;
  2382. dma_addr_t dma_handle;
  2383. int error=0;
  2384. hdr.PageVersion = MPI_SASEXPANDER1_PAGEVERSION;
  2385. hdr.ExtPageLength = 0;
  2386. hdr.PageNumber = 1;
  2387. hdr.Reserved1 = 0;
  2388. hdr.Reserved2 = 0;
  2389. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  2390. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  2391. cfg.cfghdr.ehdr = &hdr;
  2392. cfg.physAddr = -1;
  2393. cfg.pageAddr = form + form_specific;
  2394. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2395. cfg.dir = 0; /* read */
  2396. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  2397. error = mpt_config(ioc, &cfg);
  2398. if (error)
  2399. goto out;
  2400. if (!hdr.ExtPageLength) {
  2401. error = -ENXIO;
  2402. goto out;
  2403. }
  2404. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2405. &dma_handle);
  2406. if (!buffer) {
  2407. error = -ENOMEM;
  2408. goto out;
  2409. }
  2410. cfg.physAddr = dma_handle;
  2411. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2412. error = mpt_config(ioc, &cfg);
  2413. if (error == MPI_IOCSTATUS_CONFIG_INVALID_PAGE) {
  2414. error = -ENODEV;
  2415. goto out_free_consistent;
  2416. }
  2417. if (error)
  2418. goto out_free_consistent;
  2419. mptsas_print_expander_pg1(ioc, buffer);
  2420. /* save config data */
  2421. phy_info->phy_id = buffer->PhyIdentifier;
  2422. phy_info->port_id = buffer->PhysicalPort;
  2423. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  2424. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  2425. phy_info->hw_link_rate = buffer->HwLinkRate;
  2426. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  2427. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  2428. out_free_consistent:
  2429. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  2430. buffer, dma_handle);
  2431. out:
  2432. return error;
  2433. }
  2434. struct rep_manu_request{
  2435. u8 smp_frame_type;
  2436. u8 function;
  2437. u8 reserved;
  2438. u8 request_length;
  2439. };
  2440. struct rep_manu_reply{
  2441. u8 smp_frame_type; /* 0x41 */
  2442. u8 function; /* 0x01 */
  2443. u8 function_result;
  2444. u8 response_length;
  2445. u16 expander_change_count;
  2446. u8 reserved0[2];
  2447. u8 sas_format:1;
  2448. u8 reserved1:7;
  2449. u8 reserved2[3];
  2450. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  2451. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  2452. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  2453. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  2454. u16 component_id;
  2455. u8 component_revision_id;
  2456. u8 reserved3;
  2457. u8 vendor_specific[8];
  2458. };
  2459. /**
  2460. * mptsas_exp_repmanufacture_info -
  2461. * @ioc: per adapter object
  2462. * @sas_address: expander sas address
  2463. * @edev: the sas_expander_device object
  2464. *
  2465. * Fills in the sas_expander_device object when SMP port is created.
  2466. *
  2467. * Returns 0 for success, non-zero for failure.
  2468. */
  2469. static int
  2470. mptsas_exp_repmanufacture_info(MPT_ADAPTER *ioc,
  2471. u64 sas_address, struct sas_expander_device *edev)
  2472. {
  2473. MPT_FRAME_HDR *mf;
  2474. SmpPassthroughRequest_t *smpreq;
  2475. SmpPassthroughReply_t *smprep;
  2476. struct rep_manu_reply *manufacture_reply;
  2477. struct rep_manu_request *manufacture_request;
  2478. int ret;
  2479. int flagsLength;
  2480. unsigned long timeleft;
  2481. char *psge;
  2482. unsigned long flags;
  2483. void *data_out = NULL;
  2484. dma_addr_t data_out_dma = 0;
  2485. u32 sz;
  2486. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  2487. if (ioc->ioc_reset_in_progress) {
  2488. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  2489. printk(MYIOC_s_INFO_FMT "%s: host reset in progress!\n",
  2490. __func__, ioc->name);
  2491. return -EFAULT;
  2492. }
  2493. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  2494. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  2495. if (ret)
  2496. goto out;
  2497. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  2498. if (!mf) {
  2499. ret = -ENOMEM;
  2500. goto out_unlock;
  2501. }
  2502. smpreq = (SmpPassthroughRequest_t *)mf;
  2503. memset(smpreq, 0, sizeof(*smpreq));
  2504. sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
  2505. data_out = pci_alloc_consistent(ioc->pcidev, sz, &data_out_dma);
  2506. if (!data_out) {
  2507. printk(KERN_ERR "Memory allocation failure at %s:%d/%s()!\n",
  2508. __FILE__, __LINE__, __func__);
  2509. ret = -ENOMEM;
  2510. goto put_mf;
  2511. }
  2512. manufacture_request = data_out;
  2513. manufacture_request->smp_frame_type = 0x40;
  2514. manufacture_request->function = 1;
  2515. manufacture_request->reserved = 0;
  2516. manufacture_request->request_length = 0;
  2517. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  2518. smpreq->PhysicalPort = 0xFF;
  2519. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  2520. smpreq->RequestDataLength = sizeof(struct rep_manu_request);
  2521. psge = (char *)
  2522. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  2523. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2524. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2525. MPI_SGE_FLAGS_HOST_TO_IOC |
  2526. MPI_SGE_FLAGS_END_OF_BUFFER;
  2527. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2528. flagsLength |= sizeof(struct rep_manu_request);
  2529. ioc->add_sge(psge, flagsLength, data_out_dma);
  2530. psge += ioc->SGE_size;
  2531. flagsLength = MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  2532. MPI_SGE_FLAGS_SYSTEM_ADDRESS |
  2533. MPI_SGE_FLAGS_IOC_TO_HOST |
  2534. MPI_SGE_FLAGS_END_OF_BUFFER;
  2535. flagsLength = flagsLength << MPI_SGE_FLAGS_SHIFT;
  2536. flagsLength |= sizeof(struct rep_manu_reply);
  2537. ioc->add_sge(psge, flagsLength, data_out_dma +
  2538. sizeof(struct rep_manu_request));
  2539. INITIALIZE_MGMT_STATUS(ioc->sas_mgmt.status)
  2540. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  2541. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  2542. if (!(ioc->sas_mgmt.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  2543. ret = -ETIME;
  2544. mpt_free_msg_frame(ioc, mf);
  2545. mf = NULL;
  2546. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_DID_IOCRESET)
  2547. goto out_free;
  2548. if (!timeleft)
  2549. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  2550. goto out_free;
  2551. }
  2552. mf = NULL;
  2553. if (ioc->sas_mgmt.status & MPT_MGMT_STATUS_RF_VALID) {
  2554. u8 *tmp;
  2555. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  2556. if (le16_to_cpu(smprep->ResponseDataLength) !=
  2557. sizeof(struct rep_manu_reply))
  2558. goto out_free;
  2559. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  2560. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  2561. SAS_EXPANDER_VENDOR_ID_LEN);
  2562. strncpy(edev->product_id, manufacture_reply->product_id,
  2563. SAS_EXPANDER_PRODUCT_ID_LEN);
  2564. strncpy(edev->product_rev, manufacture_reply->product_rev,
  2565. SAS_EXPANDER_PRODUCT_REV_LEN);
  2566. edev->level = manufacture_reply->sas_format;
  2567. if (manufacture_reply->sas_format) {
  2568. strncpy(edev->component_vendor_id,
  2569. manufacture_reply->component_vendor_id,
  2570. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  2571. tmp = (u8 *)&manufacture_reply->component_id;
  2572. edev->component_id = tmp[0] << 8 | tmp[1];
  2573. edev->component_revision_id =
  2574. manufacture_reply->component_revision_id;
  2575. }
  2576. } else {
  2577. printk(MYIOC_s_ERR_FMT
  2578. "%s: smp passthru reply failed to be returned\n",
  2579. ioc->name, __func__);
  2580. ret = -ENXIO;
  2581. }
  2582. out_free:
  2583. if (data_out_dma)
  2584. pci_free_consistent(ioc->pcidev, sz, data_out, data_out_dma);
  2585. put_mf:
  2586. if (mf)
  2587. mpt_free_msg_frame(ioc, mf);
  2588. out_unlock:
  2589. CLEAR_MGMT_STATUS(ioc->sas_mgmt.status)
  2590. mutex_unlock(&ioc->sas_mgmt.mutex);
  2591. out:
  2592. return ret;
  2593. }
  2594. static void
  2595. mptsas_parse_device_info(struct sas_identify *identify,
  2596. struct mptsas_devinfo *device_info)
  2597. {
  2598. u16 protocols;
  2599. identify->sas_address = device_info->sas_address;
  2600. identify->phy_identifier = device_info->phy_id;
  2601. /*
  2602. * Fill in Phy Initiator Port Protocol.
  2603. * Bits 6:3, more than one bit can be set, fall through cases.
  2604. */
  2605. protocols = device_info->device_info & 0x78;
  2606. identify->initiator_port_protocols = 0;
  2607. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  2608. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  2609. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  2610. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  2611. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  2612. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  2613. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  2614. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  2615. /*
  2616. * Fill in Phy Target Port Protocol.
  2617. * Bits 10:7, more than one bit can be set, fall through cases.
  2618. */
  2619. protocols = device_info->device_info & 0x780;
  2620. identify->target_port_protocols = 0;
  2621. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2622. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  2623. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  2624. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  2625. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  2626. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  2627. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2628. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  2629. /*
  2630. * Fill in Attached device type.
  2631. */
  2632. switch (device_info->device_info &
  2633. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  2634. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  2635. identify->device_type = SAS_PHY_UNUSED;
  2636. break;
  2637. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  2638. identify->device_type = SAS_END_DEVICE;
  2639. break;
  2640. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  2641. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  2642. break;
  2643. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  2644. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  2645. break;
  2646. }
  2647. }
  2648. static int mptsas_probe_one_phy(struct device *dev,
  2649. struct mptsas_phyinfo *phy_info, int index, int local)
  2650. {
  2651. MPT_ADAPTER *ioc;
  2652. struct sas_phy *phy;
  2653. struct sas_port *port;
  2654. int error = 0;
  2655. VirtTarget *vtarget;
  2656. if (!dev) {
  2657. error = -ENODEV;
  2658. goto out;
  2659. }
  2660. if (!phy_info->phy) {
  2661. phy = sas_phy_alloc(dev, index);
  2662. if (!phy) {
  2663. error = -ENOMEM;
  2664. goto out;
  2665. }
  2666. } else
  2667. phy = phy_info->phy;
  2668. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  2669. /*
  2670. * Set Negotiated link rate.
  2671. */
  2672. switch (phy_info->negotiated_link_rate) {
  2673. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  2674. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  2675. break;
  2676. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  2677. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  2678. break;
  2679. case MPI_SAS_IOUNIT0_RATE_1_5:
  2680. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2681. break;
  2682. case MPI_SAS_IOUNIT0_RATE_3_0:
  2683. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2684. break;
  2685. case MPI_SAS_IOUNIT0_RATE_6_0:
  2686. phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
  2687. break;
  2688. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  2689. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  2690. default:
  2691. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  2692. break;
  2693. }
  2694. /*
  2695. * Set Max hardware link rate.
  2696. */
  2697. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  2698. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  2699. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  2700. break;
  2701. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  2702. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  2703. break;
  2704. default:
  2705. break;
  2706. }
  2707. /*
  2708. * Set Max programmed link rate.
  2709. */
  2710. switch (phy_info->programmed_link_rate &
  2711. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  2712. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  2713. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2714. break;
  2715. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  2716. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2717. break;
  2718. default:
  2719. break;
  2720. }
  2721. /*
  2722. * Set Min hardware link rate.
  2723. */
  2724. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  2725. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  2726. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  2727. break;
  2728. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  2729. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  2730. break;
  2731. default:
  2732. break;
  2733. }
  2734. /*
  2735. * Set Min programmed link rate.
  2736. */
  2737. switch (phy_info->programmed_link_rate &
  2738. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  2739. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  2740. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  2741. break;
  2742. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  2743. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  2744. break;
  2745. default:
  2746. break;
  2747. }
  2748. if (!phy_info->phy) {
  2749. error = sas_phy_add(phy);
  2750. if (error) {
  2751. sas_phy_free(phy);
  2752. goto out;
  2753. }
  2754. phy_info->phy = phy;
  2755. }
  2756. if (!phy_info->attached.handle ||
  2757. !phy_info->port_details)
  2758. goto out;
  2759. port = mptsas_get_port(phy_info);
  2760. ioc = phy_to_ioc(phy_info->phy);
  2761. if (phy_info->sas_port_add_phy) {
  2762. if (!port) {
  2763. port = sas_port_alloc_num(dev);
  2764. if (!port) {
  2765. error = -ENOMEM;
  2766. goto out;
  2767. }
  2768. error = sas_port_add(port);
  2769. if (error) {
  2770. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2771. "%s: exit at line=%d\n", ioc->name,
  2772. __func__, __LINE__));
  2773. goto out;
  2774. }
  2775. mptsas_set_port(ioc, phy_info, port);
  2776. devtprintk(ioc, dev_printk(KERN_DEBUG, &port->dev,
  2777. MYIOC_s_FMT "add port %d, sas_addr (0x%llx)\n",
  2778. ioc->name, port->port_identifier,
  2779. (unsigned long long)phy_info->
  2780. attached.sas_address));
  2781. }
  2782. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2783. "sas_port_add_phy: phy_id=%d\n",
  2784. ioc->name, phy_info->phy_id));
  2785. sas_port_add_phy(port, phy_info->phy);
  2786. phy_info->sas_port_add_phy = 0;
  2787. devtprintk(ioc, dev_printk(KERN_DEBUG, &phy_info->phy->dev,
  2788. MYIOC_s_FMT "add phy %d, phy-obj (0x%p)\n", ioc->name,
  2789. phy_info->phy_id, phy_info->phy));
  2790. }
  2791. if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
  2792. struct sas_rphy *rphy;
  2793. struct device *parent;
  2794. struct sas_identify identify;
  2795. parent = dev->parent->parent;
  2796. /*
  2797. * Let the hotplug_work thread handle processing
  2798. * the adding/removing of devices that occur
  2799. * after start of day.
  2800. */
  2801. if (mptsas_is_end_device(&phy_info->attached) &&
  2802. phy_info->attached.handle_parent) {
  2803. goto out;
  2804. }
  2805. mptsas_parse_device_info(&identify, &phy_info->attached);
  2806. if (scsi_is_host_device(parent)) {
  2807. struct mptsas_portinfo *port_info;
  2808. int i;
  2809. port_info = ioc->hba_port_info;
  2810. for (i = 0; i < port_info->num_phys; i++)
  2811. if (port_info->phy_info[i].identify.sas_address ==
  2812. identify.sas_address) {
  2813. sas_port_mark_backlink(port);
  2814. goto out;
  2815. }
  2816. } else if (scsi_is_sas_rphy(parent)) {
  2817. struct sas_rphy *parent_rphy = dev_to_rphy(parent);
  2818. if (identify.sas_address ==
  2819. parent_rphy->identify.sas_address) {
  2820. sas_port_mark_backlink(port);
  2821. goto out;
  2822. }
  2823. }
  2824. switch (identify.device_type) {
  2825. case SAS_END_DEVICE:
  2826. rphy = sas_end_device_alloc(port);
  2827. break;
  2828. case SAS_EDGE_EXPANDER_DEVICE:
  2829. case SAS_FANOUT_EXPANDER_DEVICE:
  2830. rphy = sas_expander_alloc(port, identify.device_type);
  2831. break;
  2832. default:
  2833. rphy = NULL;
  2834. break;
  2835. }
  2836. if (!rphy) {
  2837. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2838. "%s: exit at line=%d\n", ioc->name,
  2839. __func__, __LINE__));
  2840. goto out;
  2841. }
  2842. rphy->identify = identify;
  2843. error = sas_rphy_add(rphy);
  2844. if (error) {
  2845. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2846. "%s: exit at line=%d\n", ioc->name,
  2847. __func__, __LINE__));
  2848. sas_rphy_free(rphy);
  2849. goto out;
  2850. }
  2851. mptsas_set_rphy(ioc, phy_info, rphy);
  2852. if (identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
  2853. identify.device_type == SAS_FANOUT_EXPANDER_DEVICE)
  2854. mptsas_exp_repmanufacture_info(ioc,
  2855. identify.sas_address,
  2856. rphy_to_expander_device(rphy));
  2857. }
  2858. /* If the device exists,verify it wasn't previously flagged
  2859. as a missing device. If so, clear it */
  2860. vtarget = mptsas_find_vtarget(ioc,
  2861. phy_info->attached.channel,
  2862. phy_info->attached.id);
  2863. if (vtarget && vtarget->inDMD) {
  2864. printk(KERN_INFO "Device returned, unsetting inDMD\n");
  2865. vtarget->inDMD = 0;
  2866. }
  2867. out:
  2868. return error;
  2869. }
  2870. static int
  2871. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  2872. {
  2873. struct mptsas_portinfo *port_info, *hba;
  2874. int error = -ENOMEM, i;
  2875. hba = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  2876. if (! hba)
  2877. goto out;
  2878. error = mptsas_sas_io_unit_pg0(ioc, hba);
  2879. if (error)
  2880. goto out_free_port_info;
  2881. mptsas_sas_io_unit_pg1(ioc);
  2882. mutex_lock(&ioc->sas_topology_mutex);
  2883. port_info = ioc->hba_port_info;
  2884. if (!port_info) {
  2885. ioc->hba_port_info = port_info = hba;
  2886. ioc->hba_port_num_phy = port_info->num_phys;
  2887. list_add_tail(&port_info->list, &ioc->sas_topology);
  2888. } else {
  2889. for (i = 0; i < hba->num_phys; i++) {
  2890. port_info->phy_info[i].negotiated_link_rate =
  2891. hba->phy_info[i].negotiated_link_rate;
  2892. port_info->phy_info[i].handle =
  2893. hba->phy_info[i].handle;
  2894. port_info->phy_info[i].port_id =
  2895. hba->phy_info[i].port_id;
  2896. }
  2897. kfree(hba->phy_info);
  2898. kfree(hba);
  2899. hba = NULL;
  2900. }
  2901. mutex_unlock(&ioc->sas_topology_mutex);
  2902. #if defined(CPQ_CIM)
  2903. ioc->num_ports = port_info->num_phys;
  2904. #endif
  2905. for (i = 0; i < port_info->num_phys; i++) {
  2906. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  2907. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  2908. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  2909. port_info->phy_info[i].identify.handle =
  2910. port_info->phy_info[i].handle;
  2911. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  2912. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2913. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2914. port_info->phy_info[i].identify.handle);
  2915. if (!ioc->hba_port_sas_addr)
  2916. ioc->hba_port_sas_addr =
  2917. port_info->phy_info[i].identify.sas_address;
  2918. port_info->phy_info[i].identify.phy_id =
  2919. port_info->phy_info[i].phy_id = i;
  2920. if (port_info->phy_info[i].attached.handle)
  2921. mptsas_sas_device_pg0(ioc,
  2922. &port_info->phy_info[i].attached,
  2923. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2924. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2925. port_info->phy_info[i].attached.handle);
  2926. }
  2927. mptsas_setup_wide_ports(ioc, port_info);
  2928. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  2929. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  2930. &port_info->phy_info[i], ioc->sas_index, 1);
  2931. return 0;
  2932. out_free_port_info:
  2933. kfree(hba);
  2934. out:
  2935. return error;
  2936. }
  2937. static void
  2938. mptsas_expander_refresh(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  2939. {
  2940. struct mptsas_portinfo *parent;
  2941. struct device *parent_dev;
  2942. struct sas_rphy *rphy;
  2943. int i;
  2944. u64 sas_address; /* expander sas address */
  2945. u32 handle;
  2946. handle = port_info->phy_info[0].handle;
  2947. sas_address = port_info->phy_info[0].identify.sas_address;
  2948. for (i = 0; i < port_info->num_phys; i++) {
  2949. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  2950. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  2951. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + handle);
  2952. mptsas_sas_device_pg0(ioc,
  2953. &port_info->phy_info[i].identify,
  2954. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2955. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2956. port_info->phy_info[i].identify.handle);
  2957. port_info->phy_info[i].identify.phy_id =
  2958. port_info->phy_info[i].phy_id;
  2959. if (port_info->phy_info[i].attached.handle) {
  2960. mptsas_sas_device_pg0(ioc,
  2961. &port_info->phy_info[i].attached,
  2962. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  2963. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2964. port_info->phy_info[i].attached.handle);
  2965. port_info->phy_info[i].attached.phy_id =
  2966. port_info->phy_info[i].phy_id;
  2967. }
  2968. }
  2969. mutex_lock(&ioc->sas_topology_mutex);
  2970. parent = mptsas_find_portinfo_by_handle(ioc,
  2971. port_info->phy_info[0].identify.handle_parent);
  2972. if (!parent) {
  2973. mutex_unlock(&ioc->sas_topology_mutex);
  2974. return;
  2975. }
  2976. for (i = 0, parent_dev = NULL; i < parent->num_phys && !parent_dev;
  2977. i++) {
  2978. if (parent->phy_info[i].attached.sas_address == sas_address) {
  2979. rphy = mptsas_get_rphy(&parent->phy_info[i]);
  2980. parent_dev = &rphy->dev;
  2981. }
  2982. }
  2983. mutex_unlock(&ioc->sas_topology_mutex);
  2984. mptsas_setup_wide_ports(ioc, port_info);
  2985. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  2986. mptsas_probe_one_phy(parent_dev, &port_info->phy_info[i],
  2987. ioc->sas_index, 0);
  2988. }
  2989. static void
  2990. mptsas_expander_event_add(MPT_ADAPTER *ioc,
  2991. MpiEventDataSasExpanderStatusChange_t *expander_data)
  2992. {
  2993. struct mptsas_portinfo *port_info;
  2994. int i;
  2995. __le64 sas_address;
  2996. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  2997. if (!port_info)
  2998. BUG();
  2999. port_info->num_phys = (expander_data->NumPhys) ?
  3000. expander_data->NumPhys : 1;
  3001. port_info->phy_info = kcalloc(port_info->num_phys,
  3002. sizeof(struct mptsas_phyinfo), GFP_KERNEL);
  3003. if (!port_info->phy_info)
  3004. BUG();
  3005. memcpy(&sas_address, &expander_data->SASAddress, sizeof(__le64));
  3006. for (i = 0; i < port_info->num_phys; i++) {
  3007. port_info->phy_info[i].portinfo = port_info;
  3008. port_info->phy_info[i].handle =
  3009. le16_to_cpu(expander_data->DevHandle);
  3010. port_info->phy_info[i].identify.sas_address =
  3011. le64_to_cpu(sas_address);
  3012. port_info->phy_info[i].identify.handle_parent =
  3013. le16_to_cpu(expander_data->ParentDevHandle);
  3014. }
  3015. mutex_lock(&ioc->sas_topology_mutex);
  3016. list_add_tail(&port_info->list, &ioc->sas_topology);
  3017. mutex_unlock(&ioc->sas_topology_mutex);
  3018. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3019. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3020. (unsigned long long)sas_address);
  3021. mptsas_expander_refresh(ioc, port_info);
  3022. }
  3023. /**
  3024. * mptsas_delete_expander_siblings - remove siblings attached to expander
  3025. * @ioc: Pointer to MPT_ADAPTER structure
  3026. * @parent: the parent port_info object
  3027. * @expander: the expander port_info object
  3028. **/
  3029. static void
  3030. mptsas_delete_expander_siblings(MPT_ADAPTER *ioc, struct mptsas_portinfo
  3031. *parent, struct mptsas_portinfo *expander)
  3032. {
  3033. struct mptsas_phyinfo *phy_info;
  3034. struct mptsas_portinfo *port_info;
  3035. struct sas_rphy *rphy;
  3036. int i;
  3037. phy_info = expander->phy_info;
  3038. for (i = 0; i < expander->num_phys; i++, phy_info++) {
  3039. rphy = mptsas_get_rphy(phy_info);
  3040. if (!rphy)
  3041. continue;
  3042. if (rphy->identify.device_type == SAS_END_DEVICE)
  3043. mptsas_del_end_device(ioc, phy_info);
  3044. }
  3045. phy_info = expander->phy_info;
  3046. for (i = 0; i < expander->num_phys; i++, phy_info++) {
  3047. rphy = mptsas_get_rphy(phy_info);
  3048. if (!rphy)
  3049. continue;
  3050. if (rphy->identify.device_type ==
  3051. MPI_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  3052. rphy->identify.device_type ==
  3053. MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
  3054. port_info = mptsas_find_portinfo_by_sas_address(ioc,
  3055. rphy->identify.sas_address);
  3056. if (!port_info)
  3057. continue;
  3058. if (port_info == parent) /* backlink rphy */
  3059. continue;
  3060. /*
  3061. Delete this expander even if the expdevpage is exists
  3062. because the parent expander is already deleted
  3063. */
  3064. mptsas_expander_delete(ioc, port_info, 1);
  3065. }
  3066. }
  3067. }
  3068. /**
  3069. * mptsas_expander_delete - remove this expander
  3070. * @ioc: Pointer to MPT_ADAPTER structure
  3071. * @port_info: expander port_info struct
  3072. * @force: Flag to forcefully delete the expander
  3073. *
  3074. **/
  3075. static void mptsas_expander_delete(MPT_ADAPTER *ioc,
  3076. struct mptsas_portinfo *port_info, u8 force)
  3077. {
  3078. struct mptsas_portinfo *parent;
  3079. int i;
  3080. u64 expander_sas_address;
  3081. struct mptsas_phyinfo *phy_info;
  3082. struct mptsas_portinfo buffer;
  3083. struct mptsas_portinfo_details *port_details;
  3084. struct sas_port *port;
  3085. if (!port_info)
  3086. return;
  3087. /* see if expander is still there before deleting */
  3088. mptsas_sas_expander_pg0(ioc, &buffer,
  3089. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  3090. MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
  3091. port_info->phy_info[0].identify.handle);
  3092. if (buffer.num_phys) {
  3093. kfree(buffer.phy_info);
  3094. if (!force)
  3095. return;
  3096. }
  3097. /*
  3098. * Obtain the port_info instance to the parent port
  3099. */
  3100. port_details = NULL;
  3101. expander_sas_address =
  3102. port_info->phy_info[0].identify.sas_address;
  3103. parent = mptsas_find_portinfo_by_handle(ioc,
  3104. port_info->phy_info[0].identify.handle_parent);
  3105. mptsas_delete_expander_siblings(ioc, parent, port_info);
  3106. if (!parent)
  3107. goto out;
  3108. /*
  3109. * Delete rphys in the parent that point
  3110. * to this expander.
  3111. */
  3112. phy_info = parent->phy_info;
  3113. port = NULL;
  3114. for (i = 0; i < parent->num_phys; i++, phy_info++) {
  3115. if (!phy_info->phy)
  3116. continue;
  3117. if (phy_info->attached.sas_address !=
  3118. expander_sas_address)
  3119. continue;
  3120. if (!port) {
  3121. port = mptsas_get_port(phy_info);
  3122. port_details = phy_info->port_details;
  3123. }
  3124. dev_printk(KERN_DEBUG, &phy_info->phy->dev,
  3125. MYIOC_s_FMT "delete phy %d, phy-obj (0x%p)\n", ioc->name,
  3126. phy_info->phy_id, phy_info->phy);
  3127. sas_port_delete_phy(port, phy_info->phy);
  3128. }
  3129. if (port) {
  3130. dev_printk(KERN_DEBUG, &port->dev,
  3131. MYIOC_s_FMT "delete port %d, sas_addr (0x%llx)\n",
  3132. ioc->name, port->port_identifier,
  3133. (unsigned long long)expander_sas_address);
  3134. sas_port_delete(port);
  3135. mptsas_port_delete(ioc, port_details);
  3136. }
  3137. out:
  3138. printk(MYIOC_s_INFO_FMT "delete expander: num_phys %d, "
  3139. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3140. (unsigned long long)expander_sas_address);
  3141. /*
  3142. * free link
  3143. */
  3144. list_del(&port_info->list);
  3145. kfree(port_info->phy_info);
  3146. kfree(port_info);
  3147. }
  3148. /**
  3149. * mptsas_send_expander_event - expanders events
  3150. * @ioc: Pointer to MPT_ADAPTER structure
  3151. * @expander_data: event data
  3152. *
  3153. *
  3154. * This function handles adding, removing, and refreshing
  3155. * device handles within the expander objects.
  3156. */
  3157. static void
  3158. mptsas_send_expander_event(struct fw_event_work *fw_event)
  3159. {
  3160. MPT_ADAPTER *ioc;
  3161. MpiEventDataSasExpanderStatusChange_t *expander_data;
  3162. struct mptsas_portinfo *port_info;
  3163. __le64 sas_address;
  3164. int i;
  3165. ioc = fw_event->ioc;
  3166. expander_data = (MpiEventDataSasExpanderStatusChange_t *)
  3167. fw_event->event_data;
  3168. memcpy(&sas_address, &expander_data->SASAddress, sizeof(__le64));
  3169. sas_address = le64_to_cpu(sas_address);
  3170. port_info = mptsas_find_portinfo_by_sas_address(ioc, sas_address);
  3171. if (expander_data->ReasonCode == MPI_EVENT_SAS_EXP_RC_ADDED) {
  3172. if (port_info) {
  3173. for (i = 0; i < port_info->num_phys; i++) {
  3174. port_info->phy_info[i].portinfo = port_info;
  3175. port_info->phy_info[i].handle =
  3176. le16_to_cpu(expander_data->DevHandle);
  3177. port_info->phy_info[i].identify.sas_address =
  3178. le64_to_cpu(sas_address);
  3179. port_info->phy_info[i].identify.handle_parent =
  3180. le16_to_cpu(expander_data->ParentDevHandle);
  3181. }
  3182. mptsas_expander_refresh(ioc, port_info);
  3183. } else if (!port_info && expander_data->NumPhys)
  3184. mptsas_expander_event_add(ioc, expander_data);
  3185. } else if (expander_data->ReasonCode ==
  3186. MPI_EVENT_SAS_EXP_RC_NOT_RESPONDING)
  3187. mptsas_expander_delete(ioc, port_info, 0);
  3188. mptsas_free_fw_event(ioc, fw_event);
  3189. }
  3190. /**
  3191. * mptsas_expander_add -
  3192. * @ioc: Pointer to MPT_ADAPTER structure
  3193. * @handle:
  3194. *
  3195. */
  3196. static struct mptsas_portinfo *
  3197. mptsas_expander_add(MPT_ADAPTER *ioc, u16 handle)
  3198. {
  3199. struct mptsas_portinfo buffer, *port_info;
  3200. int i;
  3201. if ((mptsas_sas_expander_pg0(ioc, &buffer,
  3202. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  3203. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle)))
  3204. return NULL;
  3205. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_ATOMIC);
  3206. if (!port_info) {
  3207. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3208. "%s: exit at line=%d\n", ioc->name,
  3209. __func__, __LINE__));
  3210. return NULL;
  3211. }
  3212. port_info->num_phys = buffer.num_phys;
  3213. port_info->phy_info = buffer.phy_info;
  3214. for (i = 0; i < port_info->num_phys; i++)
  3215. port_info->phy_info[i].portinfo = port_info;
  3216. mutex_lock(&ioc->sas_topology_mutex);
  3217. list_add_tail(&port_info->list, &ioc->sas_topology);
  3218. mutex_unlock(&ioc->sas_topology_mutex);
  3219. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3220. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3221. (unsigned long long)buffer.phy_info[0].identify.sas_address);
  3222. mptsas_expander_refresh(ioc, port_info);
  3223. return port_info;
  3224. }
  3225. static void
  3226. mptsas_send_link_status_event(struct fw_event_work *fw_event)
  3227. {
  3228. MPT_ADAPTER *ioc;
  3229. MpiEventDataSasPhyLinkStatus_t *link_data;
  3230. struct mptsas_portinfo *port_info;
  3231. struct mptsas_phyinfo *phy_info = NULL;
  3232. __le64 sas_address;
  3233. u8 phy_num;
  3234. u8 link_rate;
  3235. ioc = fw_event->ioc;
  3236. link_data = (MpiEventDataSasPhyLinkStatus_t *)fw_event->event_data;
  3237. memcpy(&sas_address, &link_data->SASAddress, sizeof(__le64));
  3238. sas_address = le64_to_cpu(sas_address);
  3239. link_rate = link_data->LinkRates >> 4;
  3240. phy_num = link_data->PhyNum;
  3241. port_info = mptsas_find_portinfo_by_sas_address(ioc, sas_address);
  3242. if (port_info) {
  3243. phy_info = &port_info->phy_info[phy_num];
  3244. if (phy_info)
  3245. phy_info->negotiated_link_rate = link_rate;
  3246. }
  3247. if (link_rate == MPI_SAS_IOUNIT0_RATE_1_5 ||
  3248. link_rate == MPI_SAS_IOUNIT0_RATE_3_0 ||
  3249. link_rate == MPI_SAS_IOUNIT0_RATE_6_0) {
  3250. if (!port_info) {
  3251. if (ioc->old_sas_discovery_protocal) {
  3252. port_info = mptsas_expander_add(ioc,
  3253. le16_to_cpu(link_data->DevHandle));
  3254. if (port_info)
  3255. goto out;
  3256. }
  3257. goto out;
  3258. }
  3259. if (port_info == ioc->hba_port_info)
  3260. mptsas_probe_hba_phys(ioc);
  3261. else
  3262. mptsas_expander_refresh(ioc, port_info);
  3263. } else if (phy_info && phy_info->phy) {
  3264. if (link_rate == MPI_SAS_IOUNIT0_RATE_PHY_DISABLED)
  3265. phy_info->phy->negotiated_linkrate =
  3266. SAS_PHY_DISABLED;
  3267. else if (link_rate ==
  3268. MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION)
  3269. phy_info->phy->negotiated_linkrate =
  3270. SAS_LINK_RATE_FAILED;
  3271. else {
  3272. phy_info->phy->negotiated_linkrate =
  3273. SAS_LINK_RATE_UNKNOWN;
  3274. if (ioc->device_missing_delay &&
  3275. mptsas_is_end_device(&phy_info->attached)) {
  3276. struct scsi_device *sdev;
  3277. VirtDevice *vdevice;
  3278. u8 channel, id;
  3279. id = phy_info->attached.id;
  3280. channel = phy_info->attached.channel;
  3281. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3282. "Link down for fw_id %d:fw_channel %d\n",
  3283. ioc->name, phy_info->attached.id,
  3284. phy_info->attached.channel));
  3285. shost_for_each_device(sdev, ioc->sh) {
  3286. vdevice = sdev->hostdata;
  3287. if ((vdevice == NULL) ||
  3288. (vdevice->vtarget == NULL))
  3289. continue;
  3290. if ((vdevice->vtarget->tflags &
  3291. MPT_TARGET_FLAGS_RAID_COMPONENT ||
  3292. vdevice->vtarget->raidVolume))
  3293. continue;
  3294. if (vdevice->vtarget->id == id &&
  3295. vdevice->vtarget->channel ==
  3296. channel)
  3297. devtprintk(ioc,
  3298. printk(MYIOC_s_DEBUG_FMT
  3299. "SDEV OUTSTANDING CMDS"
  3300. "%d\n", ioc->name,
  3301. atomic_read(&sdev->device_busy)));
  3302. }
  3303. }
  3304. }
  3305. }
  3306. out:
  3307. mptsas_free_fw_event(ioc, fw_event);
  3308. }
  3309. static void
  3310. mptsas_not_responding_devices(MPT_ADAPTER *ioc)
  3311. {
  3312. struct mptsas_portinfo buffer, *port_info;
  3313. struct mptsas_device_info *sas_info;
  3314. struct mptsas_devinfo sas_device;
  3315. u32 handle;
  3316. VirtTarget *vtarget = NULL;
  3317. struct mptsas_phyinfo *phy_info;
  3318. u8 found_expander;
  3319. int retval, retry_count;
  3320. unsigned long flags;
  3321. mpt_findImVolumes(ioc);
  3322. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  3323. if (ioc->ioc_reset_in_progress) {
  3324. dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3325. "%s: exiting due to a parallel reset \n", ioc->name,
  3326. __func__));
  3327. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  3328. return;
  3329. }
  3330. spin_unlock_irqrestore(&ioc->taskmgmt_lock, flags);
  3331. /* devices, logical volumes */
  3332. mutex_lock(&ioc->sas_device_info_mutex);
  3333. redo_device_scan:
  3334. list_for_each_entry(sas_info, &ioc->sas_device_info_list, list) {
  3335. if (sas_info->is_cached)
  3336. continue;
  3337. if (!sas_info->is_logical_volume) {
  3338. sas_device.handle = 0;
  3339. retry_count = 0;
  3340. retry_page:
  3341. retval = mptsas_sas_device_pg0(ioc, &sas_device,
  3342. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID
  3343. << MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3344. (sas_info->fw.channel << 8) +
  3345. sas_info->fw.id);
  3346. if (sas_device.handle)
  3347. continue;
  3348. if (retval == -EBUSY) {
  3349. spin_lock_irqsave(&ioc->taskmgmt_lock, flags);
  3350. if (ioc->ioc_reset_in_progress) {
  3351. dfailprintk(ioc,
  3352. printk(MYIOC_s_DEBUG_FMT
  3353. "%s: exiting due to reset\n",
  3354. ioc->name, __func__));
  3355. spin_unlock_irqrestore
  3356. (&ioc->taskmgmt_lock, flags);
  3357. mutex_unlock(&ioc->
  3358. sas_device_info_mutex);
  3359. return;
  3360. }
  3361. spin_unlock_irqrestore(&ioc->taskmgmt_lock,
  3362. flags);
  3363. }
  3364. if (retval && (retval != -ENODEV)) {
  3365. if (retry_count < 10) {
  3366. retry_count++;
  3367. goto retry_page;
  3368. } else {
  3369. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3370. "%s: Config page retry exceeded retry "
  3371. "count deleting device 0x%llx\n",
  3372. ioc->name, __func__,
  3373. sas_info->sas_address));
  3374. }
  3375. }
  3376. /* delete device */
  3377. vtarget = mptsas_find_vtarget(ioc,
  3378. sas_info->fw.channel, sas_info->fw.id);
  3379. if (vtarget)
  3380. vtarget->deleted = 1;
  3381. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3382. sas_info->sas_address);
  3383. mptsas_del_end_device(ioc, phy_info);
  3384. goto redo_device_scan;
  3385. } else
  3386. mptsas_volume_delete(ioc, sas_info->fw.id);
  3387. }
  3388. mutex_unlock(&ioc->sas_device_info_mutex);
  3389. /* expanders */
  3390. mutex_lock(&ioc->sas_topology_mutex);
  3391. redo_expander_scan:
  3392. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3393. if (!(port_info->phy_info[0].identify.device_info &
  3394. MPI_SAS_DEVICE_INFO_SMP_TARGET))
  3395. continue;
  3396. found_expander = 0;
  3397. handle = 0xFFFF;
  3398. while (!mptsas_sas_expander_pg0(ioc, &buffer,
  3399. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  3400. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle) &&
  3401. !found_expander) {
  3402. handle = buffer.phy_info[0].handle;
  3403. if (buffer.phy_info[0].identify.sas_address ==
  3404. port_info->phy_info[0].identify.sas_address) {
  3405. found_expander = 1;
  3406. }
  3407. kfree(buffer.phy_info);
  3408. }
  3409. if (!found_expander) {
  3410. mptsas_expander_delete(ioc, port_info, 0);
  3411. goto redo_expander_scan;
  3412. }
  3413. }
  3414. mutex_unlock(&ioc->sas_topology_mutex);
  3415. }
  3416. /**
  3417. * mptsas_probe_expanders - adding expanders
  3418. * @ioc: Pointer to MPT_ADAPTER structure
  3419. *
  3420. **/
  3421. static void
  3422. mptsas_probe_expanders(MPT_ADAPTER *ioc)
  3423. {
  3424. struct mptsas_portinfo buffer, *port_info;
  3425. u32 handle;
  3426. int i;
  3427. handle = 0xFFFF;
  3428. while (!mptsas_sas_expander_pg0(ioc, &buffer,
  3429. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  3430. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), handle)) {
  3431. handle = buffer.phy_info[0].handle;
  3432. port_info = mptsas_find_portinfo_by_sas_address(ioc,
  3433. buffer.phy_info[0].identify.sas_address);
  3434. if (port_info) {
  3435. /* refreshing handles */
  3436. for (i = 0; i < buffer.num_phys; i++) {
  3437. port_info->phy_info[i].handle = handle;
  3438. port_info->phy_info[i].identify.handle_parent =
  3439. buffer.phy_info[0].identify.handle_parent;
  3440. }
  3441. mptsas_expander_refresh(ioc, port_info);
  3442. kfree(buffer.phy_info);
  3443. continue;
  3444. }
  3445. port_info = kzalloc(sizeof(struct mptsas_portinfo), GFP_KERNEL);
  3446. if (!port_info) {
  3447. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3448. "%s: exit at line=%d\n", ioc->name,
  3449. __func__, __LINE__));
  3450. return;
  3451. }
  3452. port_info->num_phys = buffer.num_phys;
  3453. port_info->phy_info = buffer.phy_info;
  3454. for (i = 0; i < port_info->num_phys; i++)
  3455. port_info->phy_info[i].portinfo = port_info;
  3456. mutex_lock(&ioc->sas_topology_mutex);
  3457. list_add_tail(&port_info->list, &ioc->sas_topology);
  3458. mutex_unlock(&ioc->sas_topology_mutex);
  3459. printk(MYIOC_s_INFO_FMT "add expander: num_phys %d, "
  3460. "sas_addr (0x%llx)\n", ioc->name, port_info->num_phys,
  3461. (unsigned long long)buffer.phy_info[0].identify.sas_address);
  3462. mptsas_expander_refresh(ioc, port_info);
  3463. }
  3464. }
  3465. static void
  3466. mptsas_probe_devices(MPT_ADAPTER *ioc)
  3467. {
  3468. u16 handle;
  3469. struct mptsas_devinfo sas_device;
  3470. struct mptsas_phyinfo *phy_info;
  3471. handle = 0xFFFF;
  3472. while (!(mptsas_sas_device_pg0(ioc, &sas_device,
  3473. MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  3474. handle = sas_device.handle;
  3475. if ((sas_device.device_info &
  3476. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  3477. MPI_SAS_DEVICE_INFO_STP_TARGET |
  3478. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) == 0)
  3479. continue;
  3480. /* If there is no FW B_T mapping for this device then continue
  3481. * */
  3482. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3483. || !(sas_device.flags &
  3484. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3485. continue;
  3486. phy_info = mptsas_refreshing_device_handles(ioc, &sas_device);
  3487. if (!phy_info)
  3488. continue;
  3489. if (mptsas_get_rphy(phy_info))
  3490. continue;
  3491. mptsas_add_end_device(ioc, phy_info);
  3492. }
  3493. }
  3494. /**
  3495. * mptsas_scan_sas_topology -
  3496. * @ioc: Pointer to MPT_ADAPTER structure
  3497. * @sas_address:
  3498. *
  3499. **/
  3500. static void
  3501. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  3502. {
  3503. struct scsi_device *sdev;
  3504. int i;
  3505. mptsas_probe_hba_phys(ioc);
  3506. mptsas_probe_expanders(ioc);
  3507. mptsas_probe_devices(ioc);
  3508. /*
  3509. Reporting RAID volumes.
  3510. */
  3511. if (!ioc->ir_firmware || !ioc->raid_data.pIocPg2 ||
  3512. !ioc->raid_data.pIocPg2->NumActiveVolumes)
  3513. return;
  3514. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  3515. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  3516. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  3517. if (sdev) {
  3518. scsi_device_put(sdev);
  3519. continue;
  3520. }
  3521. printk(MYIOC_s_INFO_FMT "attaching raid volume, channel %d, "
  3522. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3523. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID);
  3524. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  3525. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  3526. }
  3527. }
  3528. static void
  3529. mptsas_handle_queue_full_event(struct fw_event_work *fw_event)
  3530. {
  3531. MPT_ADAPTER *ioc;
  3532. EventDataQueueFull_t *qfull_data;
  3533. struct mptsas_device_info *sas_info;
  3534. struct scsi_device *sdev;
  3535. int depth;
  3536. int id = -1;
  3537. int channel = -1;
  3538. int fw_id, fw_channel;
  3539. u16 current_depth;
  3540. ioc = fw_event->ioc;
  3541. qfull_data = (EventDataQueueFull_t *)fw_event->event_data;
  3542. fw_id = qfull_data->TargetID;
  3543. fw_channel = qfull_data->Bus;
  3544. current_depth = le16_to_cpu(qfull_data->CurrentDepth);
  3545. /* if hidden raid component, look for the volume id */
  3546. mutex_lock(&ioc->sas_device_info_mutex);
  3547. if (mptscsih_is_phys_disk(ioc, fw_channel, fw_id)) {
  3548. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  3549. list) {
  3550. if (sas_info->is_cached ||
  3551. sas_info->is_logical_volume)
  3552. continue;
  3553. if (sas_info->is_hidden_raid_component &&
  3554. (sas_info->fw.channel == fw_channel &&
  3555. sas_info->fw.id == fw_id)) {
  3556. id = sas_info->volume_id;
  3557. channel = MPTSAS_RAID_CHANNEL;
  3558. goto out;
  3559. }
  3560. }
  3561. } else {
  3562. list_for_each_entry(sas_info, &ioc->sas_device_info_list,
  3563. list) {
  3564. if (sas_info->is_cached ||
  3565. sas_info->is_hidden_raid_component ||
  3566. sas_info->is_logical_volume)
  3567. continue;
  3568. if (sas_info->fw.channel == fw_channel &&
  3569. sas_info->fw.id == fw_id) {
  3570. id = sas_info->os.id;
  3571. channel = sas_info->os.channel;
  3572. goto out;
  3573. }
  3574. }
  3575. }
  3576. out:
  3577. mutex_unlock(&ioc->sas_device_info_mutex);
  3578. if (id != -1) {
  3579. shost_for_each_device(sdev, ioc->sh) {
  3580. if (sdev->id == id && sdev->channel == channel) {
  3581. if (current_depth > sdev->queue_depth) {
  3582. sdev_printk(KERN_INFO, sdev,
  3583. "strange observation, the queue "
  3584. "depth is (%d) meanwhile fw queue "
  3585. "depth (%d)\n", sdev->queue_depth,
  3586. current_depth);
  3587. continue;
  3588. }
  3589. depth = scsi_track_queue_full(sdev,
  3590. sdev->queue_depth - 1);
  3591. if (depth > 0)
  3592. sdev_printk(KERN_INFO, sdev,
  3593. "Queue depth reduced to (%d)\n",
  3594. depth);
  3595. else if (depth < 0)
  3596. sdev_printk(KERN_INFO, sdev,
  3597. "Tagged Command Queueing is being "
  3598. "disabled\n");
  3599. else if (depth == 0)
  3600. sdev_printk(KERN_DEBUG, sdev,
  3601. "Queue depth not changed yet\n");
  3602. }
  3603. }
  3604. }
  3605. mptsas_free_fw_event(ioc, fw_event);
  3606. }
  3607. static struct mptsas_phyinfo *
  3608. mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  3609. {
  3610. struct mptsas_portinfo *port_info;
  3611. struct mptsas_phyinfo *phy_info = NULL;
  3612. int i;
  3613. mutex_lock(&ioc->sas_topology_mutex);
  3614. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3615. for (i = 0; i < port_info->num_phys; i++) {
  3616. if (!mptsas_is_end_device(
  3617. &port_info->phy_info[i].attached))
  3618. continue;
  3619. if (port_info->phy_info[i].attached.sas_address
  3620. != sas_address)
  3621. continue;
  3622. phy_info = &port_info->phy_info[i];
  3623. break;
  3624. }
  3625. }
  3626. mutex_unlock(&ioc->sas_topology_mutex);
  3627. return phy_info;
  3628. }
  3629. /**
  3630. * mptsas_find_phyinfo_by_phys_disk_num -
  3631. * @ioc: Pointer to MPT_ADAPTER structure
  3632. * @phys_disk_num:
  3633. * @channel:
  3634. * @id:
  3635. *
  3636. **/
  3637. static struct mptsas_phyinfo *
  3638. mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 phys_disk_num,
  3639. u8 channel, u8 id)
  3640. {
  3641. struct mptsas_phyinfo *phy_info = NULL;
  3642. struct mptsas_portinfo *port_info;
  3643. RaidPhysDiskPage1_t *phys_disk = NULL;
  3644. int num_paths;
  3645. u64 sas_address = 0;
  3646. int i;
  3647. phy_info = NULL;
  3648. if (!ioc->raid_data.pIocPg3)
  3649. return NULL;
  3650. /* dual port support */
  3651. num_paths = mpt_raid_phys_disk_get_num_paths(ioc, phys_disk_num);
  3652. if (!num_paths)
  3653. goto out;
  3654. phys_disk = kzalloc(offsetof(RaidPhysDiskPage1_t, Path) +
  3655. (num_paths * sizeof(RAID_PHYS_DISK1_PATH)), GFP_KERNEL);
  3656. if (!phys_disk)
  3657. goto out;
  3658. mpt_raid_phys_disk_pg1(ioc, phys_disk_num, phys_disk);
  3659. for (i = 0; i < num_paths; i++) {
  3660. if ((phys_disk->Path[i].Flags & 1) != 0)
  3661. /* entry no longer valid */
  3662. continue;
  3663. if ((id == phys_disk->Path[i].PhysDiskID) &&
  3664. (channel == phys_disk->Path[i].PhysDiskBus)) {
  3665. memcpy(&sas_address, &phys_disk->Path[i].WWID,
  3666. sizeof(u64));
  3667. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3668. sas_address);
  3669. goto out;
  3670. }
  3671. }
  3672. out:
  3673. kfree(phys_disk);
  3674. if (phy_info)
  3675. return phy_info;
  3676. /*
  3677. * Extra code to handle RAID0 case, where the sas_address is not updated
  3678. * in phys_disk_page_1 when hotswapped
  3679. */
  3680. mutex_lock(&ioc->sas_topology_mutex);
  3681. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  3682. for (i = 0; i < port_info->num_phys && !phy_info; i++) {
  3683. if (!mptsas_is_end_device(
  3684. &port_info->phy_info[i].attached))
  3685. continue;
  3686. if (port_info->phy_info[i].attached.phys_disk_num == ~0)
  3687. continue;
  3688. if ((port_info->phy_info[i].attached.phys_disk_num ==
  3689. phys_disk_num) &&
  3690. (port_info->phy_info[i].attached.id == id) &&
  3691. (port_info->phy_info[i].attached.channel ==
  3692. channel))
  3693. phy_info = &port_info->phy_info[i];
  3694. }
  3695. }
  3696. mutex_unlock(&ioc->sas_topology_mutex);
  3697. return phy_info;
  3698. }
  3699. static void
  3700. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  3701. {
  3702. int rc;
  3703. sdev->no_uld_attach = data ? 1 : 0;
  3704. rc = scsi_device_reprobe(sdev);
  3705. }
  3706. static void
  3707. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  3708. {
  3709. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  3710. mptsas_reprobe_lun);
  3711. }
  3712. static void
  3713. mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
  3714. {
  3715. CONFIGPARMS cfg;
  3716. ConfigPageHeader_t hdr;
  3717. dma_addr_t dma_handle;
  3718. pRaidVolumePage0_t buffer = NULL;
  3719. RaidPhysDiskPage0_t phys_disk;
  3720. int i;
  3721. struct mptsas_phyinfo *phy_info;
  3722. struct mptsas_devinfo sas_device;
  3723. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  3724. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  3725. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  3726. cfg.pageAddr = (channel << 8) + id;
  3727. cfg.cfghdr.hdr = &hdr;
  3728. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  3729. cfg.timeout = SAS_CONFIG_PAGE_TIMEOUT;
  3730. if (mpt_config(ioc, &cfg) != 0)
  3731. goto out;
  3732. if (!hdr.PageLength)
  3733. goto out;
  3734. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  3735. &dma_handle);
  3736. if (!buffer)
  3737. goto out;
  3738. cfg.physAddr = dma_handle;
  3739. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  3740. if (mpt_config(ioc, &cfg) != 0)
  3741. goto out;
  3742. if (!(buffer->VolumeStatus.Flags &
  3743. MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
  3744. goto out;
  3745. if (!buffer->NumPhysDisks)
  3746. goto out;
  3747. for (i = 0; i < buffer->NumPhysDisks; i++) {
  3748. if (mpt_raid_phys_disk_pg0(ioc,
  3749. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  3750. continue;
  3751. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3752. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3753. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3754. (phys_disk.PhysDiskBus << 8) +
  3755. phys_disk.PhysDiskID))
  3756. continue;
  3757. /* If there is no FW B_T mapping for this device then continue
  3758. * */
  3759. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3760. || !(sas_device.flags &
  3761. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3762. continue;
  3763. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3764. sas_device.sas_address);
  3765. mptsas_add_end_device(ioc, phy_info);
  3766. }
  3767. out:
  3768. if (buffer)
  3769. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  3770. dma_handle);
  3771. }
  3772. /*
  3773. * Work queue thread to handle SAS hotplug events
  3774. */
  3775. static void
  3776. mptsas_hotplug_work(MPT_ADAPTER *ioc, struct fw_event_work *fw_event,
  3777. struct mptsas_hotplug_event *hot_plug_info)
  3778. {
  3779. struct mptsas_phyinfo *phy_info;
  3780. struct scsi_target * starget;
  3781. struct mptsas_devinfo sas_device;
  3782. VirtTarget *vtarget;
  3783. int i;
  3784. struct mptsas_portinfo *port_info;
  3785. switch (hot_plug_info->event_type) {
  3786. case MPTSAS_ADD_PHYSDISK:
  3787. if (!ioc->raid_data.pIocPg2)
  3788. break;
  3789. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  3790. if (ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID ==
  3791. hot_plug_info->id) {
  3792. printk(MYIOC_s_WARN_FMT "firmware bug: unable "
  3793. "to add hidden disk - target_id matchs "
  3794. "volume_id\n", ioc->name);
  3795. mptsas_free_fw_event(ioc, fw_event);
  3796. return;
  3797. }
  3798. }
  3799. mpt_findImVolumes(ioc);
  3800. case MPTSAS_ADD_DEVICE:
  3801. memset(&sas_device, 0, sizeof(struct mptsas_devinfo));
  3802. mptsas_sas_device_pg0(ioc, &sas_device,
  3803. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3804. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3805. (hot_plug_info->channel << 8) +
  3806. hot_plug_info->id);
  3807. /* If there is no FW B_T mapping for this device then break
  3808. * */
  3809. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3810. || !(sas_device.flags &
  3811. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3812. break;
  3813. if (!sas_device.handle)
  3814. return;
  3815. phy_info = mptsas_refreshing_device_handles(ioc, &sas_device);
  3816. /* Only For SATA Device ADD */
  3817. if (!phy_info && (sas_device.device_info &
  3818. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) {
  3819. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  3820. "%s %d SATA HOT PLUG: "
  3821. "parent handle of device %x\n", ioc->name,
  3822. __func__, __LINE__, sas_device.handle_parent));
  3823. port_info = mptsas_find_portinfo_by_handle(ioc,
  3824. sas_device.handle_parent);
  3825. if (port_info == ioc->hba_port_info)
  3826. mptsas_probe_hba_phys(ioc);
  3827. else if (port_info)
  3828. mptsas_expander_refresh(ioc, port_info);
  3829. else {
  3830. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3831. "%s %d port info is NULL\n",
  3832. ioc->name, __func__, __LINE__));
  3833. break;
  3834. }
  3835. phy_info = mptsas_refreshing_device_handles
  3836. (ioc, &sas_device);
  3837. }
  3838. if (!phy_info) {
  3839. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3840. "%s %d phy info is NULL\n",
  3841. ioc->name, __func__, __LINE__));
  3842. break;
  3843. }
  3844. if (mptsas_get_rphy(phy_info))
  3845. break;
  3846. mptsas_add_end_device(ioc, phy_info);
  3847. break;
  3848. case MPTSAS_DEL_DEVICE:
  3849. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3850. hot_plug_info->sas_address);
  3851. mptsas_del_end_device(ioc, phy_info);
  3852. break;
  3853. case MPTSAS_DEL_PHYSDISK:
  3854. mpt_findImVolumes(ioc);
  3855. phy_info = mptsas_find_phyinfo_by_phys_disk_num(
  3856. ioc, hot_plug_info->phys_disk_num,
  3857. hot_plug_info->channel,
  3858. hot_plug_info->id);
  3859. mptsas_del_end_device(ioc, phy_info);
  3860. break;
  3861. case MPTSAS_ADD_PHYSDISK_REPROBE:
  3862. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3863. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3864. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3865. (hot_plug_info->channel << 8) + hot_plug_info->id)) {
  3866. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3867. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3868. __func__, hot_plug_info->id, __LINE__));
  3869. break;
  3870. }
  3871. /* If there is no FW B_T mapping for this device then break
  3872. * */
  3873. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3874. || !(sas_device.flags &
  3875. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3876. break;
  3877. phy_info = mptsas_find_phyinfo_by_sas_address(
  3878. ioc, sas_device.sas_address);
  3879. if (!phy_info) {
  3880. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3881. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3882. __func__, hot_plug_info->id, __LINE__));
  3883. break;
  3884. }
  3885. starget = mptsas_get_starget(phy_info);
  3886. if (!starget) {
  3887. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3888. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3889. __func__, hot_plug_info->id, __LINE__));
  3890. break;
  3891. }
  3892. vtarget = starget->hostdata;
  3893. if (!vtarget) {
  3894. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3895. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3896. __func__, hot_plug_info->id, __LINE__));
  3897. break;
  3898. }
  3899. mpt_findImVolumes(ioc);
  3900. starget_printk(KERN_INFO, starget, MYIOC_s_FMT "RAID Hidding: "
  3901. "fw_channel=%d, fw_id=%d, physdsk %d, sas_addr 0x%llx\n",
  3902. ioc->name, hot_plug_info->channel, hot_plug_info->id,
  3903. hot_plug_info->phys_disk_num, (unsigned long long)
  3904. sas_device.sas_address);
  3905. vtarget->id = hot_plug_info->phys_disk_num;
  3906. vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
  3907. phy_info->attached.phys_disk_num = hot_plug_info->phys_disk_num;
  3908. mptsas_reprobe_target(starget, 1);
  3909. break;
  3910. case MPTSAS_DEL_PHYSDISK_REPROBE:
  3911. if (mptsas_sas_device_pg0(ioc, &sas_device,
  3912. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  3913. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  3914. (hot_plug_info->channel << 8) + hot_plug_info->id)) {
  3915. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3916. "%s: fw_id=%d exit at line=%d\n",
  3917. ioc->name, __func__,
  3918. hot_plug_info->id, __LINE__));
  3919. break;
  3920. }
  3921. /* If there is no FW B_T mapping for this device then break
  3922. * */
  3923. if (!(sas_device.flags & MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)
  3924. || !(sas_device.flags &
  3925. MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED))
  3926. break;
  3927. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  3928. sas_device.sas_address);
  3929. if (!phy_info) {
  3930. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3931. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3932. __func__, hot_plug_info->id, __LINE__));
  3933. break;
  3934. }
  3935. starget = mptsas_get_starget(phy_info);
  3936. if (!starget) {
  3937. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3938. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3939. __func__, hot_plug_info->id, __LINE__));
  3940. break;
  3941. }
  3942. vtarget = starget->hostdata;
  3943. if (!vtarget) {
  3944. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3945. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3946. __func__, hot_plug_info->id, __LINE__));
  3947. break;
  3948. }
  3949. if (!(vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)) {
  3950. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  3951. "%s: fw_id=%d exit at line=%d\n", ioc->name,
  3952. __func__, hot_plug_info->id, __LINE__));
  3953. break;
  3954. }
  3955. mpt_findImVolumes(ioc);
  3956. starget_printk(KERN_INFO, starget, MYIOC_s_FMT "RAID Exposing:"
  3957. " fw_channel=%d, fw_id=%d, physdsk %d, sas_addr 0x%llx\n",
  3958. ioc->name, hot_plug_info->channel, hot_plug_info->id,
  3959. hot_plug_info->phys_disk_num, (unsigned long long)
  3960. sas_device.sas_address);
  3961. vtarget->tflags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  3962. vtarget->id = hot_plug_info->id;
  3963. phy_info->attached.phys_disk_num = ~0;
  3964. mptsas_reprobe_target(starget, 0);
  3965. mptsas_add_device_component_by_fw(ioc,
  3966. hot_plug_info->channel, hot_plug_info->id);
  3967. break;
  3968. case MPTSAS_ADD_RAID:
  3969. mpt_findImVolumes(ioc);
  3970. printk(MYIOC_s_INFO_FMT "attaching raid volume, channel %d, "
  3971. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3972. hot_plug_info->id);
  3973. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  3974. hot_plug_info->id, 0);
  3975. break;
  3976. case MPTSAS_DEL_RAID:
  3977. mpt_findImVolumes(ioc);
  3978. printk(MYIOC_s_INFO_FMT "removing raid volume, channel %d, "
  3979. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL,
  3980. hot_plug_info->id);
  3981. scsi_remove_device(hot_plug_info->sdev);
  3982. scsi_device_put(hot_plug_info->sdev);
  3983. break;
  3984. case MPTSAS_ADD_INACTIVE_VOLUME:
  3985. mpt_findImVolumes(ioc);
  3986. mptsas_adding_inactive_raid_components(ioc,
  3987. hot_plug_info->channel, hot_plug_info->id);
  3988. break;
  3989. default:
  3990. break;
  3991. }
  3992. mptsas_free_fw_event(ioc, fw_event);
  3993. }
  3994. static void
  3995. mptsas_send_sas_event(struct fw_event_work *fw_event)
  3996. {
  3997. MPT_ADAPTER *ioc;
  3998. struct mptsas_hotplug_event hot_plug_info;
  3999. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
  4000. u32 device_info;
  4001. u64 sas_address;
  4002. ioc = fw_event->ioc;
  4003. sas_event_data = (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)
  4004. fw_event->event_data;
  4005. device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  4006. if ((device_info &
  4007. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  4008. MPI_SAS_DEVICE_INFO_STP_TARGET |
  4009. MPI_SAS_DEVICE_INFO_SATA_DEVICE)) == 0) {
  4010. mptsas_free_fw_event(ioc, fw_event);
  4011. return;
  4012. }
  4013. if (sas_event_data->ReasonCode ==
  4014. MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED) {
  4015. mptbase_sas_persist_operation(ioc,
  4016. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  4017. mptsas_free_fw_event(ioc, fw_event);
  4018. return;
  4019. }
  4020. switch (sas_event_data->ReasonCode) {
  4021. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  4022. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  4023. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4024. hot_plug_info.handle = le16_to_cpu(sas_event_data->DevHandle);
  4025. hot_plug_info.channel = sas_event_data->Bus;
  4026. hot_plug_info.id = sas_event_data->TargetID;
  4027. hot_plug_info.phy_id = sas_event_data->PhyNum;
  4028. memcpy(&sas_address, &sas_event_data->SASAddress,
  4029. sizeof(u64));
  4030. hot_plug_info.sas_address = le64_to_cpu(sas_address);
  4031. hot_plug_info.device_info = device_info;
  4032. if (sas_event_data->ReasonCode &
  4033. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  4034. hot_plug_info.event_type = MPTSAS_ADD_DEVICE;
  4035. else
  4036. hot_plug_info.event_type = MPTSAS_DEL_DEVICE;
  4037. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4038. break;
  4039. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  4040. mptbase_sas_persist_operation(ioc,
  4041. MPI_SAS_OP_CLEAR_NOT_PRESENT);
  4042. mptsas_free_fw_event(ioc, fw_event);
  4043. break;
  4044. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  4045. /* TODO */
  4046. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  4047. /* TODO */
  4048. default:
  4049. mptsas_free_fw_event(ioc, fw_event);
  4050. break;
  4051. }
  4052. }
  4053. static void
  4054. mptsas_send_raid_event(struct fw_event_work *fw_event)
  4055. {
  4056. MPT_ADAPTER *ioc;
  4057. EVENT_DATA_RAID *raid_event_data;
  4058. struct mptsas_hotplug_event hot_plug_info;
  4059. int status;
  4060. int state;
  4061. struct scsi_device *sdev = NULL;
  4062. VirtDevice *vdevice = NULL;
  4063. RaidPhysDiskPage0_t phys_disk;
  4064. ioc = fw_event->ioc;
  4065. raid_event_data = (EVENT_DATA_RAID *)fw_event->event_data;
  4066. status = le32_to_cpu(raid_event_data->SettingsStatus);
  4067. state = (status >> 8) & 0xff;
  4068. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4069. hot_plug_info.id = raid_event_data->VolumeID;
  4070. hot_plug_info.channel = raid_event_data->VolumeBus;
  4071. hot_plug_info.phys_disk_num = raid_event_data->PhysDiskNum;
  4072. if (raid_event_data->ReasonCode == MPI_EVENT_RAID_RC_VOLUME_DELETED ||
  4073. raid_event_data->ReasonCode == MPI_EVENT_RAID_RC_VOLUME_CREATED ||
  4074. raid_event_data->ReasonCode ==
  4075. MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED) {
  4076. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  4077. hot_plug_info.id, 0);
  4078. hot_plug_info.sdev = sdev;
  4079. if (sdev)
  4080. vdevice = sdev->hostdata;
  4081. }
  4082. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Entering %s: "
  4083. "ReasonCode=%02x\n", ioc->name, __func__,
  4084. raid_event_data->ReasonCode));
  4085. switch (raid_event_data->ReasonCode) {
  4086. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  4087. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK_REPROBE;
  4088. break;
  4089. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  4090. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK_REPROBE;
  4091. break;
  4092. case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
  4093. switch (state) {
  4094. case MPI_PD_STATE_ONLINE:
  4095. case MPI_PD_STATE_NOT_COMPATIBLE:
  4096. mpt_raid_phys_disk_pg0(ioc,
  4097. raid_event_data->PhysDiskNum, &phys_disk);
  4098. hot_plug_info.id = phys_disk.PhysDiskID;
  4099. hot_plug_info.channel = phys_disk.PhysDiskBus;
  4100. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK;
  4101. break;
  4102. case MPI_PD_STATE_FAILED:
  4103. case MPI_PD_STATE_MISSING:
  4104. case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
  4105. case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
  4106. case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
  4107. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK;
  4108. break;
  4109. default:
  4110. break;
  4111. }
  4112. break;
  4113. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  4114. if (!sdev)
  4115. break;
  4116. vdevice->vtarget->deleted = 1; /* block IO */
  4117. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4118. break;
  4119. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  4120. if (sdev) {
  4121. scsi_device_put(sdev);
  4122. break;
  4123. }
  4124. hot_plug_info.event_type = MPTSAS_ADD_RAID;
  4125. break;
  4126. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  4127. if (!(status & MPI_RAIDVOL0_STATUS_FLAG_ENABLED)) {
  4128. if (!sdev)
  4129. break;
  4130. vdevice->vtarget->deleted = 1; /* block IO */
  4131. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4132. break;
  4133. }
  4134. switch (state) {
  4135. case MPI_RAIDVOL0_STATUS_STATE_FAILED:
  4136. case MPI_RAIDVOL0_STATUS_STATE_MISSING:
  4137. if (!sdev)
  4138. break;
  4139. vdevice->vtarget->deleted = 1; /* block IO */
  4140. hot_plug_info.event_type = MPTSAS_DEL_RAID;
  4141. break;
  4142. case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
  4143. case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
  4144. if (sdev) {
  4145. scsi_device_put(sdev);
  4146. break;
  4147. }
  4148. hot_plug_info.event_type = MPTSAS_ADD_RAID;
  4149. break;
  4150. default:
  4151. break;
  4152. }
  4153. break;
  4154. default:
  4155. break;
  4156. }
  4157. if (hot_plug_info.event_type != MPTSAS_IGNORE_EVENT)
  4158. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4159. else
  4160. mptsas_free_fw_event(ioc, fw_event);
  4161. }
  4162. /**
  4163. * mptsas_issue_tm - send mptsas internal tm request
  4164. * @ioc: Pointer to MPT_ADAPTER structure
  4165. * @type: Task Management type
  4166. * @channel: channel number for task management
  4167. * @id: Logical Target ID for reset (if appropriate)
  4168. * @lun: Logical unit for reset (if appropriate)
  4169. * @task_context: Context for the task to be aborted
  4170. * @timeout: timeout for task management control
  4171. *
  4172. * return 0 on success and -1 on failure:
  4173. *
  4174. */
  4175. static int
  4176. mptsas_issue_tm(MPT_ADAPTER *ioc, u8 type, u8 channel, u8 id, u64 lun,
  4177. int task_context, ulong timeout, u8 *issue_reset)
  4178. {
  4179. MPT_FRAME_HDR *mf;
  4180. SCSITaskMgmt_t *pScsiTm;
  4181. int retval;
  4182. unsigned long timeleft;
  4183. *issue_reset = 0;
  4184. mf = mpt_get_msg_frame(mptsasDeviceResetCtx, ioc);
  4185. if (mf == NULL) {
  4186. retval = -1; /* return failure */
  4187. dtmprintk(ioc, printk(MYIOC_s_WARN_FMT "TaskMgmt request: no "
  4188. "msg frames!!\n", ioc->name));
  4189. goto out;
  4190. }
  4191. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT "TaskMgmt request: mr = %p, "
  4192. "task_type = 0x%02X,\n\t timeout = %ld, fw_channel = %d, "
  4193. "fw_id = %d, lun = %lld,\n\t task_context = 0x%x\n", ioc->name, mf,
  4194. type, timeout, channel, id, (unsigned long long)lun,
  4195. task_context));
  4196. pScsiTm = (SCSITaskMgmt_t *) mf;
  4197. memset(pScsiTm, 0, sizeof(SCSITaskMgmt_t));
  4198. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  4199. pScsiTm->TaskType = type;
  4200. pScsiTm->MsgFlags = 0;
  4201. pScsiTm->TargetID = id;
  4202. pScsiTm->Bus = channel;
  4203. pScsiTm->ChainOffset = 0;
  4204. pScsiTm->Reserved = 0;
  4205. pScsiTm->Reserved1 = 0;
  4206. pScsiTm->TaskMsgContext = task_context;
  4207. int_to_scsilun(lun, (struct scsi_lun *)pScsiTm->LUN);
  4208. INITIALIZE_MGMT_STATUS(ioc->taskmgmt_cmds.status)
  4209. CLEAR_MGMT_STATUS(ioc->internal_cmds.status)
  4210. retval = 0;
  4211. mpt_put_msg_frame_hi_pri(mptsasDeviceResetCtx, ioc, mf);
  4212. /* Now wait for the command to complete */
  4213. timeleft = wait_for_completion_timeout(&ioc->taskmgmt_cmds.done,
  4214. timeout*HZ);
  4215. if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
  4216. retval = -1; /* return failure */
  4217. dtmprintk(ioc, printk(MYIOC_s_ERR_FMT
  4218. "TaskMgmt request: TIMED OUT!(mr=%p)\n", ioc->name, mf));
  4219. mpt_free_msg_frame(ioc, mf);
  4220. if (ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_DID_IOCRESET)
  4221. goto out;
  4222. *issue_reset = 1;
  4223. goto out;
  4224. }
  4225. if (!(ioc->taskmgmt_cmds.status & MPT_MGMT_STATUS_RF_VALID)) {
  4226. retval = -1; /* return failure */
  4227. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4228. "TaskMgmt request: failed with no reply\n", ioc->name));
  4229. goto out;
  4230. }
  4231. out:
  4232. CLEAR_MGMT_STATUS(ioc->taskmgmt_cmds.status)
  4233. return retval;
  4234. }
  4235. /**
  4236. * mptsas_broadcast_primative_work - Handle broadcast primitives
  4237. * @work: work queue payload containing info describing the event
  4238. *
  4239. * this will be handled in workqueue context.
  4240. */
  4241. static void
  4242. mptsas_broadcast_primative_work(struct fw_event_work *fw_event)
  4243. {
  4244. MPT_ADAPTER *ioc = fw_event->ioc;
  4245. MPT_FRAME_HDR *mf;
  4246. VirtDevice *vdevice;
  4247. int ii;
  4248. struct scsi_cmnd *sc;
  4249. SCSITaskMgmtReply_t *pScsiTmReply;
  4250. u8 issue_reset;
  4251. int task_context;
  4252. u8 channel, id;
  4253. int lun;
  4254. u32 termination_count;
  4255. u32 query_count;
  4256. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4257. "%s - enter\n", ioc->name, __func__));
  4258. mutex_lock(&ioc->taskmgmt_cmds.mutex);
  4259. if (mpt_set_taskmgmt_in_progress_flag(ioc) != 0) {
  4260. mutex_unlock(&ioc->taskmgmt_cmds.mutex);
  4261. mptsas_requeue_fw_event(ioc, fw_event, 1000);
  4262. return;
  4263. }
  4264. issue_reset = 0;
  4265. termination_count = 0;
  4266. query_count = 0;
  4267. mpt_findImVolumes(ioc);
  4268. pScsiTmReply = (SCSITaskMgmtReply_t *) ioc->taskmgmt_cmds.reply;
  4269. for (ii = 0; ii < ioc->req_depth; ii++) {
  4270. if (ioc->fw_events_off)
  4271. goto out;
  4272. sc = mptscsih_get_scsi_lookup(ioc, ii);
  4273. if (!sc)
  4274. continue;
  4275. mf = MPT_INDEX_2_MFPTR(ioc, ii);
  4276. if (!mf)
  4277. continue;
  4278. task_context = mf->u.frame.hwhdr.msgctxu.MsgContext;
  4279. vdevice = sc->device->hostdata;
  4280. if (!vdevice || !vdevice->vtarget)
  4281. continue;
  4282. if (vdevice->vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT)
  4283. continue; /* skip hidden raid components */
  4284. if (vdevice->vtarget->raidVolume)
  4285. continue; /* skip hidden raid components */
  4286. channel = vdevice->vtarget->channel;
  4287. id = vdevice->vtarget->id;
  4288. lun = vdevice->lun;
  4289. if (mptsas_issue_tm(ioc, MPI_SCSITASKMGMT_TASKTYPE_QUERY_TASK,
  4290. channel, id, (u64)lun, task_context, 30, &issue_reset))
  4291. goto out;
  4292. query_count++;
  4293. termination_count +=
  4294. le32_to_cpu(pScsiTmReply->TerminationCount);
  4295. if ((pScsiTmReply->IOCStatus == MPI_IOCSTATUS_SUCCESS) &&
  4296. (pScsiTmReply->ResponseCode ==
  4297. MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
  4298. pScsiTmReply->ResponseCode ==
  4299. MPI_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
  4300. continue;
  4301. if (mptsas_issue_tm(ioc,
  4302. MPI_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET,
  4303. channel, id, (u64)lun, 0, 30, &issue_reset))
  4304. goto out;
  4305. termination_count +=
  4306. le32_to_cpu(pScsiTmReply->TerminationCount);
  4307. }
  4308. out:
  4309. dtmprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4310. "%s - exit, query_count = %d termination_count = %d\n",
  4311. ioc->name, __func__, query_count, termination_count));
  4312. ioc->broadcast_aen_busy = 0;
  4313. mpt_clear_taskmgmt_in_progress_flag(ioc);
  4314. mutex_unlock(&ioc->taskmgmt_cmds.mutex);
  4315. if (issue_reset) {
  4316. printk(MYIOC_s_WARN_FMT
  4317. "Issuing Reset from %s!! doorbell=0x%08x\n",
  4318. ioc->name, __func__, mpt_GetIocState(ioc, 0));
  4319. mpt_Soft_Hard_ResetHandler(ioc, CAN_SLEEP);
  4320. }
  4321. mptsas_free_fw_event(ioc, fw_event);
  4322. }
  4323. /*
  4324. * mptsas_send_ir2_event - handle exposing hidden disk when
  4325. * an inactive raid volume is added
  4326. *
  4327. * @ioc: Pointer to MPT_ADAPTER structure
  4328. * @ir2_data
  4329. *
  4330. */
  4331. static void
  4332. mptsas_send_ir2_event(struct fw_event_work *fw_event)
  4333. {
  4334. MPT_ADAPTER *ioc;
  4335. struct mptsas_hotplug_event hot_plug_info;
  4336. MPI_EVENT_DATA_IR2 *ir2_data;
  4337. u8 reasonCode;
  4338. RaidPhysDiskPage0_t phys_disk;
  4339. ioc = fw_event->ioc;
  4340. ir2_data = (MPI_EVENT_DATA_IR2 *)fw_event->event_data;
  4341. reasonCode = ir2_data->ReasonCode;
  4342. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Entering %s: "
  4343. "ReasonCode=%02x\n", ioc->name, __func__, reasonCode));
  4344. memset(&hot_plug_info, 0, sizeof(struct mptsas_hotplug_event));
  4345. hot_plug_info.id = ir2_data->TargetID;
  4346. hot_plug_info.channel = ir2_data->Bus;
  4347. switch (reasonCode) {
  4348. case MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED:
  4349. hot_plug_info.event_type = MPTSAS_ADD_INACTIVE_VOLUME;
  4350. break;
  4351. case MPI_EVENT_IR2_RC_DUAL_PORT_REMOVED:
  4352. hot_plug_info.phys_disk_num = ir2_data->PhysDiskNum;
  4353. hot_plug_info.event_type = MPTSAS_DEL_PHYSDISK;
  4354. break;
  4355. case MPI_EVENT_IR2_RC_DUAL_PORT_ADDED:
  4356. hot_plug_info.phys_disk_num = ir2_data->PhysDiskNum;
  4357. mpt_raid_phys_disk_pg0(ioc,
  4358. ir2_data->PhysDiskNum, &phys_disk);
  4359. hot_plug_info.id = phys_disk.PhysDiskID;
  4360. hot_plug_info.event_type = MPTSAS_ADD_PHYSDISK;
  4361. break;
  4362. default:
  4363. mptsas_free_fw_event(ioc, fw_event);
  4364. return;
  4365. }
  4366. mptsas_hotplug_work(ioc, fw_event, &hot_plug_info);
  4367. }
  4368. static int
  4369. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  4370. {
  4371. u32 event = le32_to_cpu(reply->Event);
  4372. int event_data_sz;
  4373. struct fw_event_work *fw_event;
  4374. unsigned long delay;
  4375. if (ioc->bus_type != SAS)
  4376. return 0;
  4377. /* events turned off due to host reset or driver unloading */
  4378. if (ioc->fw_events_off)
  4379. return 0;
  4380. delay = msecs_to_jiffies(1);
  4381. switch (event) {
  4382. case MPI_EVENT_SAS_BROADCAST_PRIMITIVE:
  4383. {
  4384. EVENT_DATA_SAS_BROADCAST_PRIMITIVE *broadcast_event_data =
  4385. (EVENT_DATA_SAS_BROADCAST_PRIMITIVE *)reply->Data;
  4386. if (broadcast_event_data->Primitive !=
  4387. MPI_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
  4388. return 0;
  4389. if (ioc->broadcast_aen_busy)
  4390. return 0;
  4391. ioc->broadcast_aen_busy = 1;
  4392. break;
  4393. }
  4394. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  4395. {
  4396. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data =
  4397. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data;
  4398. u16 ioc_stat;
  4399. ioc_stat = le16_to_cpu(reply->IOCStatus);
  4400. if (sas_event_data->ReasonCode ==
  4401. MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING) {
  4402. mptsas_target_reset_queue(ioc, sas_event_data);
  4403. return 0;
  4404. }
  4405. if (sas_event_data->ReasonCode ==
  4406. MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
  4407. ioc->device_missing_delay &&
  4408. (ioc_stat & MPI_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)) {
  4409. VirtTarget *vtarget = NULL;
  4410. u8 id, channel;
  4411. id = sas_event_data->TargetID;
  4412. channel = sas_event_data->Bus;
  4413. vtarget = mptsas_find_vtarget(ioc, channel, id);
  4414. if (vtarget) {
  4415. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4416. "LogInfo (0x%x) available for "
  4417. "INTERNAL_DEVICE_RESET"
  4418. "fw_id %d fw_channel %d\n", ioc->name,
  4419. le32_to_cpu(reply->IOCLogInfo),
  4420. id, channel));
  4421. if (vtarget->raidVolume) {
  4422. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4423. "Skipping Raid Volume for inDMD\n",
  4424. ioc->name));
  4425. } else {
  4426. devtprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4427. "Setting device flag inDMD\n",
  4428. ioc->name));
  4429. vtarget->inDMD = 1;
  4430. }
  4431. }
  4432. }
  4433. break;
  4434. }
  4435. case MPI_EVENT_SAS_EXPANDER_STATUS_CHANGE:
  4436. {
  4437. MpiEventDataSasExpanderStatusChange_t *expander_data =
  4438. (MpiEventDataSasExpanderStatusChange_t *)reply->Data;
  4439. if (ioc->old_sas_discovery_protocal)
  4440. return 0;
  4441. if (expander_data->ReasonCode ==
  4442. MPI_EVENT_SAS_EXP_RC_NOT_RESPONDING &&
  4443. ioc->device_missing_delay)
  4444. delay = HZ * ioc->device_missing_delay;
  4445. break;
  4446. }
  4447. case MPI_EVENT_SAS_DISCOVERY:
  4448. {
  4449. u32 discovery_status;
  4450. EventDataSasDiscovery_t *discovery_data =
  4451. (EventDataSasDiscovery_t *)reply->Data;
  4452. discovery_status = le32_to_cpu(discovery_data->DiscoveryStatus);
  4453. ioc->sas_discovery_quiesce_io = discovery_status ? 1 : 0;
  4454. if (ioc->old_sas_discovery_protocal && !discovery_status)
  4455. mptsas_queue_rescan(ioc);
  4456. return 0;
  4457. }
  4458. case MPI_EVENT_INTEGRATED_RAID:
  4459. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  4460. case MPI_EVENT_IR2:
  4461. case MPI_EVENT_SAS_PHY_LINK_STATUS:
  4462. case MPI_EVENT_QUEUE_FULL:
  4463. break;
  4464. default:
  4465. return 0;
  4466. }
  4467. event_data_sz = ((reply->MsgLength * 4) -
  4468. offsetof(EventNotificationReply_t, Data));
  4469. fw_event = kzalloc(sizeof(*fw_event) + event_data_sz, GFP_ATOMIC);
  4470. if (!fw_event) {
  4471. printk(MYIOC_s_WARN_FMT "%s: failed at (line=%d)\n", ioc->name,
  4472. __func__, __LINE__);
  4473. return 0;
  4474. }
  4475. memcpy(fw_event->event_data, reply->Data, event_data_sz);
  4476. fw_event->event = event;
  4477. fw_event->ioc = ioc;
  4478. mptsas_add_fw_event(ioc, fw_event, delay);
  4479. return 0;
  4480. }
  4481. /* Delete a volume when no longer listed in ioc pg2
  4482. */
  4483. static void mptsas_volume_delete(MPT_ADAPTER *ioc, u8 id)
  4484. {
  4485. struct scsi_device *sdev;
  4486. int i;
  4487. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL, id, 0);
  4488. if (!sdev)
  4489. return;
  4490. if (!ioc->raid_data.pIocPg2)
  4491. goto out;
  4492. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  4493. goto out;
  4494. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
  4495. if (ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID == id)
  4496. goto release_sdev;
  4497. out:
  4498. printk(MYIOC_s_INFO_FMT "removing raid volume, channel %d, "
  4499. "id %d\n", ioc->name, MPTSAS_RAID_CHANNEL, id);
  4500. scsi_remove_device(sdev);
  4501. release_sdev:
  4502. scsi_device_put(sdev);
  4503. }
  4504. static int
  4505. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  4506. {
  4507. struct Scsi_Host *sh;
  4508. MPT_SCSI_HOST *hd;
  4509. MPT_ADAPTER *ioc;
  4510. unsigned long flags;
  4511. int ii;
  4512. int numSGE = 0;
  4513. int scale;
  4514. int ioc_cap;
  4515. int error=0;
  4516. int r;
  4517. r = mpt_attach(pdev,id);
  4518. if (r)
  4519. return r;
  4520. ioc = pci_get_drvdata(pdev);
  4521. mptsas_fw_event_off(ioc);
  4522. ioc->DoneCtx = mptsasDoneCtx;
  4523. ioc->TaskCtx = mptsasTaskCtx;
  4524. ioc->InternalCtx = mptsasInternalCtx;
  4525. ioc->schedule_target_reset = &mptsas_schedule_target_reset;
  4526. ioc->schedule_dead_ioc_flush_running_cmds =
  4527. &mptscsih_flush_running_cmds;
  4528. /* Added sanity check on readiness of the MPT adapter.
  4529. */
  4530. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  4531. printk(MYIOC_s_WARN_FMT
  4532. "Skipping because it's not operational!\n",
  4533. ioc->name);
  4534. error = -ENODEV;
  4535. goto out_mptsas_probe;
  4536. }
  4537. if (!ioc->active) {
  4538. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  4539. ioc->name);
  4540. error = -ENODEV;
  4541. goto out_mptsas_probe;
  4542. }
  4543. /* Sanity check - ensure at least 1 port is INITIATOR capable
  4544. */
  4545. ioc_cap = 0;
  4546. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  4547. if (ioc->pfacts[ii].ProtocolFlags &
  4548. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  4549. ioc_cap++;
  4550. }
  4551. if (!ioc_cap) {
  4552. printk(MYIOC_s_WARN_FMT
  4553. "Skipping ioc=%p because SCSI Initiator mode "
  4554. "is NOT enabled!\n", ioc->name, ioc);
  4555. return 0;
  4556. }
  4557. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  4558. if (!sh) {
  4559. printk(MYIOC_s_WARN_FMT
  4560. "Unable to register controller with SCSI subsystem\n",
  4561. ioc->name);
  4562. error = -1;
  4563. goto out_mptsas_probe;
  4564. }
  4565. spin_lock_irqsave(&ioc->FreeQlock, flags);
  4566. /* Attach the SCSI Host to the IOC structure
  4567. */
  4568. ioc->sh = sh;
  4569. sh->io_port = 0;
  4570. sh->n_io_port = 0;
  4571. sh->irq = 0;
  4572. /* set 16 byte cdb's */
  4573. sh->max_cmd_len = 16;
  4574. sh->can_queue = min_t(int, ioc->req_depth - 10, sh->can_queue);
  4575. sh->max_id = -1;
  4576. sh->max_lun = max_lun;
  4577. sh->transportt = mptsas_transport_template;
  4578. /* Required entry.
  4579. */
  4580. sh->unique_id = ioc->id;
  4581. INIT_LIST_HEAD(&ioc->sas_topology);
  4582. mutex_init(&ioc->sas_topology_mutex);
  4583. mutex_init(&ioc->sas_discovery_mutex);
  4584. mutex_init(&ioc->sas_mgmt.mutex);
  4585. init_completion(&ioc->sas_mgmt.done);
  4586. /* Verify that we won't exceed the maximum
  4587. * number of chain buffers
  4588. * We can optimize: ZZ = req_sz/sizeof(SGE)
  4589. * For 32bit SGE's:
  4590. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  4591. * + (req_sz - 64)/sizeof(SGE)
  4592. * A slightly different algorithm is required for
  4593. * 64bit SGEs.
  4594. */
  4595. scale = ioc->req_sz/ioc->SGE_size;
  4596. if (ioc->sg_addr_size == sizeof(u64)) {
  4597. numSGE = (scale - 1) *
  4598. (ioc->facts.MaxChainDepth-1) + scale +
  4599. (ioc->req_sz - 60) / ioc->SGE_size;
  4600. } else {
  4601. numSGE = 1 + (scale - 1) *
  4602. (ioc->facts.MaxChainDepth-1) + scale +
  4603. (ioc->req_sz - 64) / ioc->SGE_size;
  4604. }
  4605. if (numSGE < sh->sg_tablesize) {
  4606. /* Reset this value */
  4607. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4608. "Resetting sg_tablesize to %d from %d\n",
  4609. ioc->name, numSGE, sh->sg_tablesize));
  4610. sh->sg_tablesize = numSGE;
  4611. }
  4612. if (mpt_loadtime_max_sectors) {
  4613. if (mpt_loadtime_max_sectors < 64 ||
  4614. mpt_loadtime_max_sectors > 8192) {
  4615. printk(MYIOC_s_INFO_FMT "Invalid value passed for"
  4616. "mpt_loadtime_max_sectors %d."
  4617. "Range from 64 to 8192\n", ioc->name,
  4618. mpt_loadtime_max_sectors);
  4619. }
  4620. mpt_loadtime_max_sectors &= 0xFFFFFFFE;
  4621. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  4622. "Resetting max sector to %d from %d\n",
  4623. ioc->name, mpt_loadtime_max_sectors, sh->max_sectors));
  4624. sh->max_sectors = mpt_loadtime_max_sectors;
  4625. }
  4626. hd = shost_priv(sh);
  4627. hd->ioc = ioc;
  4628. /* SCSI needs scsi_cmnd lookup table!
  4629. * (with size equal to req_depth*PtrSz!)
  4630. */
  4631. ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  4632. if (!ioc->ScsiLookup) {
  4633. error = -ENOMEM;
  4634. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  4635. goto out_mptsas_probe;
  4636. }
  4637. spin_lock_init(&ioc->scsi_lookup_lock);
  4638. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
  4639. ioc->name, ioc->ScsiLookup));
  4640. ioc->sas_data.ptClear = mpt_pt_clear;
  4641. hd->last_queue_full = 0;
  4642. INIT_LIST_HEAD(&hd->target_reset_list);
  4643. INIT_LIST_HEAD(&ioc->sas_device_info_list);
  4644. mutex_init(&ioc->sas_device_info_mutex);
  4645. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  4646. if (ioc->sas_data.ptClear==1) {
  4647. mptbase_sas_persist_operation(
  4648. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  4649. }
  4650. error = scsi_add_host(sh, &ioc->pcidev->dev);
  4651. if (error) {
  4652. dprintk(ioc, printk(MYIOC_s_ERR_FMT
  4653. "scsi_add_host failed\n", ioc->name));
  4654. goto out_mptsas_probe;
  4655. }
  4656. /* older firmware doesn't support expander events */
  4657. if ((ioc->facts.HeaderVersion >> 8) < 0xE)
  4658. ioc->old_sas_discovery_protocal = 1;
  4659. mptsas_scan_sas_topology(ioc);
  4660. mptsas_fw_event_on(ioc);
  4661. return 0;
  4662. out_mptsas_probe:
  4663. mptscsih_remove(pdev);
  4664. return error;
  4665. }
  4666. static void
  4667. mptsas_shutdown(struct pci_dev *pdev)
  4668. {
  4669. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  4670. mptsas_fw_event_off(ioc);
  4671. mptsas_cleanup_fw_event_q(ioc);
  4672. }
  4673. static void mptsas_remove(struct pci_dev *pdev)
  4674. {
  4675. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  4676. struct mptsas_portinfo *p, *n;
  4677. int i;
  4678. if (!ioc->sh) {
  4679. printk(MYIOC_s_INFO_FMT "IOC is in Target mode\n", ioc->name);
  4680. mpt_detach(pdev);
  4681. return;
  4682. }
  4683. mptsas_shutdown(pdev);
  4684. mptsas_del_device_components(ioc);
  4685. ioc->sas_discovery_ignore_events = 1;
  4686. sas_remove_host(ioc->sh);
  4687. mutex_lock(&ioc->sas_topology_mutex);
  4688. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  4689. list_del(&p->list);
  4690. for (i = 0 ; i < p->num_phys ; i++)
  4691. mptsas_port_delete(ioc, p->phy_info[i].port_details);
  4692. kfree(p->phy_info);
  4693. kfree(p);
  4694. }
  4695. mutex_unlock(&ioc->sas_topology_mutex);
  4696. ioc->hba_port_info = NULL;
  4697. mptscsih_remove(pdev);
  4698. }
  4699. static struct pci_device_id mptsas_pci_table[] = {
  4700. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
  4701. PCI_ANY_ID, PCI_ANY_ID },
  4702. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
  4703. PCI_ANY_ID, PCI_ANY_ID },
  4704. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
  4705. PCI_ANY_ID, PCI_ANY_ID },
  4706. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
  4707. PCI_ANY_ID, PCI_ANY_ID },
  4708. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
  4709. PCI_ANY_ID, PCI_ANY_ID },
  4710. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068_820XELP,
  4711. PCI_ANY_ID, PCI_ANY_ID },
  4712. {0} /* Terminating entry */
  4713. };
  4714. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  4715. static struct pci_driver mptsas_driver = {
  4716. .name = "mptsas",
  4717. .id_table = mptsas_pci_table,
  4718. .probe = mptsas_probe,
  4719. .remove = mptsas_remove,
  4720. .shutdown = mptsas_shutdown,
  4721. #ifdef CONFIG_PM
  4722. .suspend = mptscsih_suspend,
  4723. .resume = mptscsih_resume,
  4724. #endif
  4725. };
  4726. static int __init
  4727. mptsas_init(void)
  4728. {
  4729. int error;
  4730. show_mptmod_ver(my_NAME, my_VERSION);
  4731. mptsas_transport_template =
  4732. sas_attach_transport(&mptsas_transport_functions);
  4733. if (!mptsas_transport_template)
  4734. return -ENODEV;
  4735. mptsas_transport_template->eh_timed_out = mptsas_eh_timed_out;
  4736. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER,
  4737. "mptscsih_io_done");
  4738. mptsasTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSAS_DRIVER,
  4739. "mptscsih_taskmgmt_complete");
  4740. mptsasInternalCtx =
  4741. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER,
  4742. "mptscsih_scandv_complete");
  4743. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER,
  4744. "mptsas_mgmt_done");
  4745. mptsasDeviceResetCtx =
  4746. mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER,
  4747. "mptsas_taskmgmt_complete");
  4748. mpt_event_register(mptsasDoneCtx, mptsas_event_process);
  4749. mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
  4750. error = pci_register_driver(&mptsas_driver);
  4751. if (error)
  4752. sas_release_transport(mptsas_transport_template);
  4753. return error;
  4754. }
  4755. static void __exit
  4756. mptsas_exit(void)
  4757. {
  4758. pci_unregister_driver(&mptsas_driver);
  4759. sas_release_transport(mptsas_transport_template);
  4760. mpt_reset_deregister(mptsasDoneCtx);
  4761. mpt_event_deregister(mptsasDoneCtx);
  4762. mpt_deregister(mptsasMgmtCtx);
  4763. mpt_deregister(mptsasInternalCtx);
  4764. mpt_deregister(mptsasTaskCtx);
  4765. mpt_deregister(mptsasDoneCtx);
  4766. mpt_deregister(mptsasDeviceResetCtx);
  4767. }
  4768. module_init(mptsas_init);
  4769. module_exit(mptsas_exit);