mptsas.c 124 KB

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