mptsas.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  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 void mptsas_hotplug_work(struct work_struct *work);
  83. static void mptsas_print_phy_data(MPT_ADAPTER *ioc,
  84. MPI_SAS_IO_UNIT0_PHY_DATA *phy_data)
  85. {
  86. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  87. "---- IO UNIT PAGE 0 ------------\n", ioc->name));
  88. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  89. ioc->name, le16_to_cpu(phy_data->AttachedDeviceHandle)));
  90. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Controller Handle=0x%X\n",
  91. ioc->name, le16_to_cpu(phy_data->ControllerDevHandle)));
  92. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port=0x%X\n",
  93. ioc->name, phy_data->Port));
  94. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Port Flags=0x%X\n",
  95. ioc->name, phy_data->PortFlags));
  96. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Flags=0x%X\n",
  97. ioc->name, phy_data->PhyFlags));
  98. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  99. ioc->name, phy_data->NegotiatedLinkRate));
  100. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  101. "Controller PHY Device Info=0x%X\n", ioc->name,
  102. le32_to_cpu(phy_data->ControllerPhyDeviceInfo)));
  103. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "DiscoveryStatus=0x%X\n\n",
  104. ioc->name, le32_to_cpu(phy_data->DiscoveryStatus)));
  105. }
  106. static void mptsas_print_phy_pg0(MPT_ADAPTER *ioc, SasPhyPage0_t *pg0)
  107. {
  108. __le64 sas_address;
  109. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  110. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  111. "---- SAS PHY PAGE 0 ------------\n", ioc->name));
  112. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  113. "Attached Device Handle=0x%X\n", ioc->name,
  114. le16_to_cpu(pg0->AttachedDevHandle)));
  115. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  116. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  117. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  118. "Attached PHY Identifier=0x%X\n", ioc->name,
  119. pg0->AttachedPhyIdentifier));
  120. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Attached Device Info=0x%X\n",
  121. ioc->name, le32_to_cpu(pg0->AttachedDeviceInfo)));
  122. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  123. ioc->name, pg0->ProgrammedLinkRate));
  124. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Change Count=0x%X\n",
  125. ioc->name, pg0->ChangeCount));
  126. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Info=0x%X\n\n",
  127. ioc->name, le32_to_cpu(pg0->PhyInfo)));
  128. }
  129. static void mptsas_print_phy_pg1(MPT_ADAPTER *ioc, SasPhyPage1_t *pg1)
  130. {
  131. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  132. "---- SAS PHY PAGE 1 ------------\n", ioc->name));
  133. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Invalid Dword Count=0x%x\n",
  134. ioc->name, pg1->InvalidDwordCount));
  135. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  136. "Running Disparity Error Count=0x%x\n", ioc->name,
  137. pg1->RunningDisparityErrorCount));
  138. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  139. "Loss Dword Synch Count=0x%x\n", ioc->name,
  140. pg1->LossDwordSynchCount));
  141. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  142. "PHY Reset Problem Count=0x%x\n\n", ioc->name,
  143. pg1->PhyResetProblemCount));
  144. }
  145. static void mptsas_print_device_pg0(MPT_ADAPTER *ioc, SasDevicePage0_t *pg0)
  146. {
  147. __le64 sas_address;
  148. memcpy(&sas_address, &pg0->SASAddress, sizeof(__le64));
  149. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  150. "---- SAS DEVICE PAGE 0 ---------\n", ioc->name));
  151. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Handle=0x%X\n",
  152. ioc->name, le16_to_cpu(pg0->DevHandle)));
  153. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Handle=0x%X\n",
  154. ioc->name, le16_to_cpu(pg0->ParentDevHandle)));
  155. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Enclosure Handle=0x%X\n",
  156. ioc->name, le16_to_cpu(pg0->EnclosureHandle)));
  157. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Slot=0x%X\n",
  158. ioc->name, le16_to_cpu(pg0->Slot)));
  159. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "SAS Address=0x%llX\n",
  160. ioc->name, (unsigned long long)le64_to_cpu(sas_address)));
  161. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Target ID=0x%X\n",
  162. ioc->name, pg0->TargetID));
  163. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Bus=0x%X\n",
  164. ioc->name, pg0->Bus));
  165. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Parent Phy Num=0x%X\n",
  166. ioc->name, pg0->PhyNum));
  167. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Access Status=0x%X\n",
  168. ioc->name, le16_to_cpu(pg0->AccessStatus)));
  169. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Device Info=0x%X\n",
  170. ioc->name, le32_to_cpu(pg0->DeviceInfo)));
  171. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Flags=0x%X\n",
  172. ioc->name, le16_to_cpu(pg0->Flags)));
  173. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n\n",
  174. ioc->name, pg0->PhysicalPort));
  175. }
  176. static void mptsas_print_expander_pg1(MPT_ADAPTER *ioc, SasExpanderPage1_t *pg1)
  177. {
  178. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  179. "---- SAS EXPANDER PAGE 1 ------------\n", ioc->name));
  180. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Physical Port=0x%X\n",
  181. ioc->name, pg1->PhysicalPort));
  182. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "PHY Identifier=0x%X\n",
  183. ioc->name, pg1->PhyIdentifier));
  184. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Negotiated Link Rate=0x%X\n",
  185. ioc->name, pg1->NegotiatedLinkRate));
  186. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Programmed Link Rate=0x%X\n",
  187. ioc->name, pg1->ProgrammedLinkRate));
  188. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Hardware Link Rate=0x%X\n",
  189. ioc->name, pg1->HwLinkRate));
  190. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Owner Device Handle=0x%X\n",
  191. ioc->name, le16_to_cpu(pg1->OwnerDevHandle)));
  192. dsasprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  193. "Attached Device Handle=0x%X\n\n", ioc->name,
  194. le16_to_cpu(pg1->AttachedDevHandle)));
  195. }
  196. static inline MPT_ADAPTER *phy_to_ioc(struct sas_phy *phy)
  197. {
  198. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  199. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  200. }
  201. static inline MPT_ADAPTER *rphy_to_ioc(struct sas_rphy *rphy)
  202. {
  203. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  204. return ((MPT_SCSI_HOST *)shost->hostdata)->ioc;
  205. }
  206. static struct mptsas_portinfo *
  207. mptsas_get_hba_portinfo(MPT_ADAPTER *ioc)
  208. {
  209. struct list_head *head = &ioc->sas_topology;
  210. struct mptsas_portinfo *pi = NULL;
  211. /* always the first entry on sas_topology list */
  212. if (!list_empty(head))
  213. pi = list_entry(head->next, struct mptsas_portinfo, list);
  214. return pi;
  215. }
  216. /*
  217. * mptsas_find_portinfo_by_handle
  218. *
  219. * This function should be called with the sas_topology_mutex already held
  220. */
  221. static struct mptsas_portinfo *
  222. mptsas_find_portinfo_by_handle(MPT_ADAPTER *ioc, u16 handle)
  223. {
  224. struct mptsas_portinfo *port_info, *rc=NULL;
  225. int i;
  226. list_for_each_entry(port_info, &ioc->sas_topology, list)
  227. for (i = 0; i < port_info->num_phys; i++)
  228. if (port_info->phy_info[i].identify.handle == handle) {
  229. rc = port_info;
  230. goto out;
  231. }
  232. out:
  233. return rc;
  234. }
  235. /*
  236. * Returns true if there is a scsi end device
  237. */
  238. static inline int
  239. mptsas_is_end_device(struct mptsas_devinfo * attached)
  240. {
  241. if ((attached->sas_address) &&
  242. (attached->device_info &
  243. MPI_SAS_DEVICE_INFO_END_DEVICE) &&
  244. ((attached->device_info &
  245. MPI_SAS_DEVICE_INFO_SSP_TARGET) |
  246. (attached->device_info &
  247. MPI_SAS_DEVICE_INFO_STP_TARGET) |
  248. (attached->device_info &
  249. MPI_SAS_DEVICE_INFO_SATA_DEVICE)))
  250. return 1;
  251. else
  252. return 0;
  253. }
  254. /* no mutex */
  255. static void
  256. mptsas_port_delete(MPT_ADAPTER *ioc, struct mptsas_portinfo_details * port_details)
  257. {
  258. struct mptsas_portinfo *port_info;
  259. struct mptsas_phyinfo *phy_info;
  260. u8 i;
  261. if (!port_details)
  262. return;
  263. port_info = port_details->port_info;
  264. phy_info = port_info->phy_info;
  265. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: [%p]: num_phys=%02d "
  266. "bitmask=0x%016llX\n", ioc->name, __func__, port_details,
  267. port_details->num_phys, (unsigned long long)
  268. port_details->phy_bitmask));
  269. for (i = 0; i < port_info->num_phys; i++, phy_info++) {
  270. if(phy_info->port_details != port_details)
  271. continue;
  272. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  273. phy_info->port_details = NULL;
  274. }
  275. kfree(port_details);
  276. }
  277. static inline struct sas_rphy *
  278. mptsas_get_rphy(struct mptsas_phyinfo *phy_info)
  279. {
  280. if (phy_info->port_details)
  281. return phy_info->port_details->rphy;
  282. else
  283. return NULL;
  284. }
  285. static inline void
  286. mptsas_set_rphy(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_rphy *rphy)
  287. {
  288. if (phy_info->port_details) {
  289. phy_info->port_details->rphy = rphy;
  290. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_rphy_add: rphy=%p\n",
  291. ioc->name, rphy));
  292. }
  293. if (rphy) {
  294. dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
  295. &rphy->dev, MYIOC_s_FMT "add:", ioc->name));
  296. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "rphy=%p release=%p\n",
  297. ioc->name, rphy, rphy->dev.release));
  298. }
  299. }
  300. static inline struct sas_port *
  301. mptsas_get_port(struct mptsas_phyinfo *phy_info)
  302. {
  303. if (phy_info->port_details)
  304. return phy_info->port_details->port;
  305. else
  306. return NULL;
  307. }
  308. static inline void
  309. mptsas_set_port(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info, struct sas_port *port)
  310. {
  311. if (phy_info->port_details)
  312. phy_info->port_details->port = port;
  313. if (port) {
  314. dsaswideprintk(ioc, dev_printk(KERN_DEBUG,
  315. &port->dev, MYIOC_s_FMT "add:", ioc->name));
  316. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "port=%p release=%p\n",
  317. ioc->name, port, port->dev.release));
  318. }
  319. }
  320. static inline struct scsi_target *
  321. mptsas_get_starget(struct mptsas_phyinfo *phy_info)
  322. {
  323. if (phy_info->port_details)
  324. return phy_info->port_details->starget;
  325. else
  326. return NULL;
  327. }
  328. static inline void
  329. mptsas_set_starget(struct mptsas_phyinfo *phy_info, struct scsi_target *
  330. starget)
  331. {
  332. if (phy_info->port_details)
  333. phy_info->port_details->starget = starget;
  334. }
  335. /*
  336. * mptsas_setup_wide_ports
  337. *
  338. * Updates for new and existing narrow/wide port configuration
  339. * in the sas_topology
  340. */
  341. static void
  342. mptsas_setup_wide_ports(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  343. {
  344. struct mptsas_portinfo_details * port_details;
  345. struct mptsas_phyinfo *phy_info, *phy_info_cmp;
  346. u64 sas_address;
  347. int i, j;
  348. mutex_lock(&ioc->sas_topology_mutex);
  349. phy_info = port_info->phy_info;
  350. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  351. if (phy_info->attached.handle)
  352. continue;
  353. port_details = phy_info->port_details;
  354. if (!port_details)
  355. continue;
  356. if (port_details->num_phys < 2)
  357. continue;
  358. /*
  359. * Removing a phy from a port, letting the last
  360. * phy be removed by firmware events.
  361. */
  362. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  363. "%s: [%p]: deleting phy = %d\n",
  364. ioc->name, __func__, port_details, i));
  365. port_details->num_phys--;
  366. port_details->phy_bitmask &= ~ (1 << phy_info->phy_id);
  367. memset(&phy_info->attached, 0, sizeof(struct mptsas_devinfo));
  368. sas_port_delete_phy(port_details->port, phy_info->phy);
  369. phy_info->port_details = NULL;
  370. }
  371. /*
  372. * Populate and refresh the tree
  373. */
  374. phy_info = port_info->phy_info;
  375. for (i = 0 ; i < port_info->num_phys ; i++, phy_info++) {
  376. sas_address = phy_info->attached.sas_address;
  377. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "phy_id=%d sas_address=0x%018llX\n",
  378. ioc->name, i, (unsigned long long)sas_address));
  379. if (!sas_address)
  380. continue;
  381. port_details = phy_info->port_details;
  382. /*
  383. * Forming a port
  384. */
  385. if (!port_details) {
  386. port_details = kzalloc(sizeof(*port_details),
  387. GFP_KERNEL);
  388. if (!port_details)
  389. goto out;
  390. port_details->num_phys = 1;
  391. port_details->port_info = port_info;
  392. if (phy_info->phy_id < 64 )
  393. port_details->phy_bitmask |=
  394. (1 << phy_info->phy_id);
  395. phy_info->sas_port_add_phy=1;
  396. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tForming port\n\t\t"
  397. "phy_id=%d sas_address=0x%018llX\n",
  398. ioc->name, i, (unsigned long long)sas_address));
  399. phy_info->port_details = port_details;
  400. }
  401. if (i == port_info->num_phys - 1)
  402. continue;
  403. phy_info_cmp = &port_info->phy_info[i + 1];
  404. for (j = i + 1 ; j < port_info->num_phys ; j++,
  405. phy_info_cmp++) {
  406. if (!phy_info_cmp->attached.sas_address)
  407. continue;
  408. if (sas_address != phy_info_cmp->attached.sas_address)
  409. continue;
  410. if (phy_info_cmp->port_details == port_details )
  411. continue;
  412. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  413. "\t\tphy_id=%d sas_address=0x%018llX\n",
  414. ioc->name, j, (unsigned long long)
  415. phy_info_cmp->attached.sas_address));
  416. if (phy_info_cmp->port_details) {
  417. port_details->rphy =
  418. mptsas_get_rphy(phy_info_cmp);
  419. port_details->port =
  420. mptsas_get_port(phy_info_cmp);
  421. port_details->starget =
  422. mptsas_get_starget(phy_info_cmp);
  423. port_details->num_phys =
  424. phy_info_cmp->port_details->num_phys;
  425. if (!phy_info_cmp->port_details->num_phys)
  426. kfree(phy_info_cmp->port_details);
  427. } else
  428. phy_info_cmp->sas_port_add_phy=1;
  429. /*
  430. * Adding a phy to a port
  431. */
  432. phy_info_cmp->port_details = port_details;
  433. if (phy_info_cmp->phy_id < 64 )
  434. port_details->phy_bitmask |=
  435. (1 << phy_info_cmp->phy_id);
  436. port_details->num_phys++;
  437. }
  438. }
  439. out:
  440. for (i = 0; i < port_info->num_phys; i++) {
  441. port_details = port_info->phy_info[i].port_details;
  442. if (!port_details)
  443. continue;
  444. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  445. "%s: [%p]: phy_id=%02d num_phys=%02d "
  446. "bitmask=0x%016llX\n", ioc->name, __func__,
  447. port_details, i, port_details->num_phys,
  448. (unsigned long long)port_details->phy_bitmask));
  449. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\t\tport = %p rphy=%p\n",
  450. ioc->name, port_details->port, port_details->rphy));
  451. }
  452. dsaswideprintk(ioc, printk("\n"));
  453. mutex_unlock(&ioc->sas_topology_mutex);
  454. }
  455. /**
  456. * csmisas_find_vtarget
  457. *
  458. * @ioc
  459. * @volume_id
  460. * @volume_bus
  461. *
  462. **/
  463. static VirtTarget *
  464. mptsas_find_vtarget(MPT_ADAPTER *ioc, u8 channel, u8 id)
  465. {
  466. struct scsi_device *sdev;
  467. VirtDevice *vdevice;
  468. VirtTarget *vtarget = NULL;
  469. shost_for_each_device(sdev, ioc->sh) {
  470. if ((vdevice = sdev->hostdata) == NULL)
  471. continue;
  472. if (vdevice->vtarget->id == id &&
  473. vdevice->vtarget->channel == channel)
  474. vtarget = vdevice->vtarget;
  475. }
  476. return vtarget;
  477. }
  478. /**
  479. * mptsas_target_reset
  480. *
  481. * Issues TARGET_RESET to end device using handshaking method
  482. *
  483. * @ioc
  484. * @channel
  485. * @id
  486. *
  487. * Returns (1) success
  488. * (0) failure
  489. *
  490. **/
  491. static int
  492. mptsas_target_reset(MPT_ADAPTER *ioc, u8 channel, u8 id)
  493. {
  494. MPT_FRAME_HDR *mf;
  495. SCSITaskMgmt_t *pScsiTm;
  496. if ((mf = mpt_get_msg_frame(ioc->TaskCtx, ioc)) == NULL) {
  497. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, no msg frames @%d!!\n",
  498. ioc->name,__func__, __LINE__));
  499. return 0;
  500. }
  501. /* Format the Request
  502. */
  503. pScsiTm = (SCSITaskMgmt_t *) mf;
  504. memset (pScsiTm, 0, sizeof(SCSITaskMgmt_t));
  505. pScsiTm->TargetID = id;
  506. pScsiTm->Bus = channel;
  507. pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
  508. pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
  509. pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
  510. DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
  511. mpt_put_msg_frame_hi_pri(ioc->TaskCtx, ioc, mf);
  512. return 1;
  513. }
  514. /**
  515. * mptsas_target_reset_queue
  516. *
  517. * Receive request for TARGET_RESET after recieving an firmware
  518. * event NOT_RESPONDING_EVENT, then put command in link list
  519. * and queue if task_queue already in use.
  520. *
  521. * @ioc
  522. * @sas_event_data
  523. *
  524. **/
  525. static void
  526. mptsas_target_reset_queue(MPT_ADAPTER *ioc,
  527. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  528. {
  529. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  530. VirtTarget *vtarget = NULL;
  531. struct mptsas_target_reset_event *target_reset_list;
  532. u8 id, channel;
  533. id = sas_event_data->TargetID;
  534. channel = sas_event_data->Bus;
  535. if (!(vtarget = mptsas_find_vtarget(ioc, channel, id)))
  536. return;
  537. vtarget->deleted = 1; /* block IO */
  538. target_reset_list = kzalloc(sizeof(*target_reset_list),
  539. GFP_ATOMIC);
  540. if (!target_reset_list) {
  541. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
  542. ioc->name,__func__, __LINE__));
  543. return;
  544. }
  545. memcpy(&target_reset_list->sas_event_data, sas_event_data,
  546. sizeof(*sas_event_data));
  547. list_add_tail(&target_reset_list->list, &hd->target_reset_list);
  548. if (hd->resetPending)
  549. return;
  550. if (mptsas_target_reset(ioc, channel, id)) {
  551. target_reset_list->target_reset_issued = 1;
  552. hd->resetPending = 1;
  553. }
  554. }
  555. /**
  556. * mptsas_dev_reset_complete
  557. *
  558. * Completion for TARGET_RESET after NOT_RESPONDING_EVENT,
  559. * enable work queue to finish off removing device from upper layers.
  560. * then send next TARGET_RESET in the queue.
  561. *
  562. * @ioc
  563. *
  564. **/
  565. static void
  566. mptsas_dev_reset_complete(MPT_ADAPTER *ioc)
  567. {
  568. MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
  569. struct list_head *head = &hd->target_reset_list;
  570. struct mptsas_target_reset_event *target_reset_list;
  571. struct mptsas_hotplug_event *ev;
  572. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data;
  573. u8 id, channel;
  574. __le64 sas_address;
  575. if (list_empty(head))
  576. return;
  577. target_reset_list = list_entry(head->next, struct mptsas_target_reset_event, list);
  578. sas_event_data = &target_reset_list->sas_event_data;
  579. id = sas_event_data->TargetID;
  580. channel = sas_event_data->Bus;
  581. hd->resetPending = 0;
  582. /*
  583. * retry target reset
  584. */
  585. if (!target_reset_list->target_reset_issued) {
  586. if (mptsas_target_reset(ioc, channel, id)) {
  587. target_reset_list->target_reset_issued = 1;
  588. hd->resetPending = 1;
  589. }
  590. return;
  591. }
  592. /*
  593. * enable work queue to remove device from upper layers
  594. */
  595. list_del(&target_reset_list->list);
  596. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  597. if (!ev) {
  598. dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, failed to allocate mem @%d..!!\n",
  599. ioc->name,__func__, __LINE__));
  600. return;
  601. }
  602. INIT_WORK(&ev->work, mptsas_hotplug_work);
  603. ev->ioc = ioc;
  604. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  605. ev->parent_handle =
  606. le16_to_cpu(sas_event_data->ParentDevHandle);
  607. ev->channel = channel;
  608. ev->id =id;
  609. ev->phy_id = sas_event_data->PhyNum;
  610. memcpy(&sas_address, &sas_event_data->SASAddress,
  611. sizeof(__le64));
  612. ev->sas_address = le64_to_cpu(sas_address);
  613. ev->device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  614. ev->event_type = MPTSAS_DEL_DEVICE;
  615. schedule_work(&ev->work);
  616. kfree(target_reset_list);
  617. /*
  618. * issue target reset to next device in the queue
  619. */
  620. head = &hd->target_reset_list;
  621. if (list_empty(head))
  622. return;
  623. target_reset_list = list_entry(head->next, struct mptsas_target_reset_event,
  624. list);
  625. sas_event_data = &target_reset_list->sas_event_data;
  626. id = sas_event_data->TargetID;
  627. channel = sas_event_data->Bus;
  628. if (mptsas_target_reset(ioc, channel, id)) {
  629. target_reset_list->target_reset_issued = 1;
  630. hd->resetPending = 1;
  631. }
  632. }
  633. /**
  634. * mptsas_taskmgmt_complete
  635. *
  636. * @ioc
  637. * @mf
  638. * @mr
  639. *
  640. **/
  641. static int
  642. mptsas_taskmgmt_complete(MPT_ADAPTER *ioc, MPT_FRAME_HDR *mf, MPT_FRAME_HDR *mr)
  643. {
  644. mptsas_dev_reset_complete(ioc);
  645. return mptscsih_taskmgmt_complete(ioc, mf, mr);
  646. }
  647. /**
  648. * mptscsih_ioc_reset
  649. *
  650. * @ioc
  651. * @reset_phase
  652. *
  653. **/
  654. static int
  655. mptsas_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  656. {
  657. MPT_SCSI_HOST *hd;
  658. struct mptsas_target_reset_event *target_reset_list, *n;
  659. int rc;
  660. rc = mptscsih_ioc_reset(ioc, reset_phase);
  661. if (ioc->bus_type != SAS)
  662. goto out;
  663. if (reset_phase != MPT_IOC_POST_RESET)
  664. goto out;
  665. if (!ioc->sh || !ioc->sh->hostdata)
  666. goto out;
  667. hd = shost_priv(ioc->sh);
  668. if (!hd->ioc)
  669. goto out;
  670. if (list_empty(&hd->target_reset_list))
  671. goto out;
  672. /* flush the target_reset_list */
  673. list_for_each_entry_safe(target_reset_list, n,
  674. &hd->target_reset_list, list) {
  675. list_del(&target_reset_list->list);
  676. kfree(target_reset_list);
  677. }
  678. out:
  679. return rc;
  680. }
  681. static int
  682. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  683. u32 form, u32 form_specific)
  684. {
  685. ConfigExtendedPageHeader_t hdr;
  686. CONFIGPARMS cfg;
  687. SasEnclosurePage0_t *buffer;
  688. dma_addr_t dma_handle;
  689. int error;
  690. __le64 le_identifier;
  691. memset(&hdr, 0, sizeof(hdr));
  692. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  693. hdr.PageNumber = 0;
  694. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  695. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  696. cfg.cfghdr.ehdr = &hdr;
  697. cfg.physAddr = -1;
  698. cfg.pageAddr = form + form_specific;
  699. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  700. cfg.dir = 0; /* read */
  701. cfg.timeout = 10;
  702. error = mpt_config(ioc, &cfg);
  703. if (error)
  704. goto out;
  705. if (!hdr.ExtPageLength) {
  706. error = -ENXIO;
  707. goto out;
  708. }
  709. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  710. &dma_handle);
  711. if (!buffer) {
  712. error = -ENOMEM;
  713. goto out;
  714. }
  715. cfg.physAddr = dma_handle;
  716. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  717. error = mpt_config(ioc, &cfg);
  718. if (error)
  719. goto out_free_consistent;
  720. /* save config data */
  721. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  722. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  723. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  724. enclosure->flags = le16_to_cpu(buffer->Flags);
  725. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  726. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  727. enclosure->start_id = buffer->StartTargetID;
  728. enclosure->start_channel = buffer->StartBus;
  729. enclosure->sep_id = buffer->SEPTargetID;
  730. enclosure->sep_channel = buffer->SEPBus;
  731. out_free_consistent:
  732. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  733. buffer, dma_handle);
  734. out:
  735. return error;
  736. }
  737. static int
  738. mptsas_slave_configure(struct scsi_device *sdev)
  739. {
  740. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  741. goto out;
  742. sas_read_port_mode_page(sdev);
  743. out:
  744. return mptscsih_slave_configure(sdev);
  745. }
  746. static int
  747. mptsas_target_alloc(struct scsi_target *starget)
  748. {
  749. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  750. MPT_SCSI_HOST *hd = shost_priv(host);
  751. VirtTarget *vtarget;
  752. u8 id, channel;
  753. struct sas_rphy *rphy;
  754. struct mptsas_portinfo *p;
  755. int i;
  756. MPT_ADAPTER *ioc = hd->ioc;
  757. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  758. if (!vtarget)
  759. return -ENOMEM;
  760. vtarget->starget = starget;
  761. vtarget->ioc_id = ioc->id;
  762. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  763. id = starget->id;
  764. channel = 0;
  765. /*
  766. * RAID volumes placed beyond the last expected port.
  767. */
  768. if (starget->channel == MPTSAS_RAID_CHANNEL) {
  769. for (i=0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
  770. if (id == ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID)
  771. channel = ioc->raid_data.pIocPg2->RaidVolume[i].VolumeBus;
  772. goto out;
  773. }
  774. rphy = dev_to_rphy(starget->dev.parent);
  775. mutex_lock(&ioc->sas_topology_mutex);
  776. list_for_each_entry(p, &ioc->sas_topology, list) {
  777. for (i = 0; i < p->num_phys; i++) {
  778. if (p->phy_info[i].attached.sas_address !=
  779. rphy->identify.sas_address)
  780. continue;
  781. id = p->phy_info[i].attached.id;
  782. channel = p->phy_info[i].attached.channel;
  783. mptsas_set_starget(&p->phy_info[i], starget);
  784. /*
  785. * Exposing hidden raid components
  786. */
  787. if (mptscsih_is_phys_disk(ioc, channel, id)) {
  788. id = mptscsih_raid_id_to_num(ioc,
  789. channel, id);
  790. vtarget->tflags |=
  791. MPT_TARGET_FLAGS_RAID_COMPONENT;
  792. p->phy_info[i].attached.phys_disk_num = id;
  793. }
  794. mutex_unlock(&ioc->sas_topology_mutex);
  795. goto out;
  796. }
  797. }
  798. mutex_unlock(&ioc->sas_topology_mutex);
  799. kfree(vtarget);
  800. return -ENXIO;
  801. out:
  802. vtarget->id = id;
  803. vtarget->channel = channel;
  804. starget->hostdata = vtarget;
  805. return 0;
  806. }
  807. static void
  808. mptsas_target_destroy(struct scsi_target *starget)
  809. {
  810. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  811. MPT_SCSI_HOST *hd = shost_priv(host);
  812. struct sas_rphy *rphy;
  813. struct mptsas_portinfo *p;
  814. int i;
  815. MPT_ADAPTER *ioc = hd->ioc;
  816. if (!starget->hostdata)
  817. return;
  818. if (starget->channel == MPTSAS_RAID_CHANNEL)
  819. goto out;
  820. rphy = dev_to_rphy(starget->dev.parent);
  821. list_for_each_entry(p, &ioc->sas_topology, list) {
  822. for (i = 0; i < p->num_phys; i++) {
  823. if (p->phy_info[i].attached.sas_address !=
  824. rphy->identify.sas_address)
  825. continue;
  826. mptsas_set_starget(&p->phy_info[i], NULL);
  827. goto out;
  828. }
  829. }
  830. out:
  831. kfree(starget->hostdata);
  832. starget->hostdata = NULL;
  833. }
  834. static int
  835. mptsas_slave_alloc(struct scsi_device *sdev)
  836. {
  837. struct Scsi_Host *host = sdev->host;
  838. MPT_SCSI_HOST *hd = shost_priv(host);
  839. struct sas_rphy *rphy;
  840. struct mptsas_portinfo *p;
  841. VirtDevice *vdevice;
  842. struct scsi_target *starget;
  843. int i;
  844. MPT_ADAPTER *ioc = hd->ioc;
  845. vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  846. if (!vdevice) {
  847. printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
  848. ioc->name, sizeof(VirtDevice));
  849. return -ENOMEM;
  850. }
  851. starget = scsi_target(sdev);
  852. vdevice->vtarget = starget->hostdata;
  853. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  854. goto out;
  855. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  856. mutex_lock(&ioc->sas_topology_mutex);
  857. list_for_each_entry(p, &ioc->sas_topology, list) {
  858. for (i = 0; i < p->num_phys; i++) {
  859. if (p->phy_info[i].attached.sas_address !=
  860. rphy->identify.sas_address)
  861. continue;
  862. vdevice->lun = sdev->lun;
  863. /*
  864. * Exposing hidden raid components
  865. */
  866. if (mptscsih_is_phys_disk(ioc,
  867. p->phy_info[i].attached.channel,
  868. p->phy_info[i].attached.id))
  869. sdev->no_uld_attach = 1;
  870. mutex_unlock(&ioc->sas_topology_mutex);
  871. goto out;
  872. }
  873. }
  874. mutex_unlock(&ioc->sas_topology_mutex);
  875. kfree(vdevice);
  876. return -ENXIO;
  877. out:
  878. vdevice->vtarget->num_luns++;
  879. sdev->hostdata = vdevice;
  880. return 0;
  881. }
  882. static int
  883. mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  884. {
  885. VirtDevice *vdevice = SCpnt->device->hostdata;
  886. if (!vdevice || !vdevice->vtarget || vdevice->vtarget->deleted) {
  887. SCpnt->result = DID_NO_CONNECT << 16;
  888. done(SCpnt);
  889. return 0;
  890. }
  891. // scsi_print_command(SCpnt);
  892. return mptscsih_qcmd(SCpnt,done);
  893. }
  894. static struct scsi_host_template mptsas_driver_template = {
  895. .module = THIS_MODULE,
  896. .proc_name = "mptsas",
  897. .proc_info = mptscsih_proc_info,
  898. .name = "MPT SPI Host",
  899. .info = mptscsih_info,
  900. .queuecommand = mptsas_qcmd,
  901. .target_alloc = mptsas_target_alloc,
  902. .slave_alloc = mptsas_slave_alloc,
  903. .slave_configure = mptsas_slave_configure,
  904. .target_destroy = mptsas_target_destroy,
  905. .slave_destroy = mptscsih_slave_destroy,
  906. .change_queue_depth = mptscsih_change_queue_depth,
  907. .eh_abort_handler = mptscsih_abort,
  908. .eh_device_reset_handler = mptscsih_dev_reset,
  909. .eh_bus_reset_handler = mptscsih_bus_reset,
  910. .eh_host_reset_handler = mptscsih_host_reset,
  911. .bios_param = mptscsih_bios_param,
  912. .can_queue = MPT_FC_CAN_QUEUE,
  913. .this_id = -1,
  914. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  915. .max_sectors = 8192,
  916. .cmd_per_lun = 7,
  917. .use_clustering = ENABLE_CLUSTERING,
  918. .shost_attrs = mptscsih_host_attrs,
  919. };
  920. static int mptsas_get_linkerrors(struct sas_phy *phy)
  921. {
  922. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  923. ConfigExtendedPageHeader_t hdr;
  924. CONFIGPARMS cfg;
  925. SasPhyPage1_t *buffer;
  926. dma_addr_t dma_handle;
  927. int error;
  928. /* FIXME: only have link errors on local phys */
  929. if (!scsi_is_sas_phy_local(phy))
  930. return -EINVAL;
  931. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  932. hdr.ExtPageLength = 0;
  933. hdr.PageNumber = 1 /* page number 1*/;
  934. hdr.Reserved1 = 0;
  935. hdr.Reserved2 = 0;
  936. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  937. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  938. cfg.cfghdr.ehdr = &hdr;
  939. cfg.physAddr = -1;
  940. cfg.pageAddr = phy->identify.phy_identifier;
  941. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  942. cfg.dir = 0; /* read */
  943. cfg.timeout = 10;
  944. error = mpt_config(ioc, &cfg);
  945. if (error)
  946. return error;
  947. if (!hdr.ExtPageLength)
  948. return -ENXIO;
  949. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  950. &dma_handle);
  951. if (!buffer)
  952. return -ENOMEM;
  953. cfg.physAddr = dma_handle;
  954. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  955. error = mpt_config(ioc, &cfg);
  956. if (error)
  957. goto out_free_consistent;
  958. mptsas_print_phy_pg1(ioc, buffer);
  959. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  960. phy->running_disparity_error_count =
  961. le32_to_cpu(buffer->RunningDisparityErrorCount);
  962. phy->loss_of_dword_sync_count =
  963. le32_to_cpu(buffer->LossDwordSynchCount);
  964. phy->phy_reset_problem_count =
  965. le32_to_cpu(buffer->PhyResetProblemCount);
  966. out_free_consistent:
  967. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  968. buffer, dma_handle);
  969. return error;
  970. }
  971. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  972. MPT_FRAME_HDR *reply)
  973. {
  974. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
  975. if (reply != NULL) {
  976. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
  977. memcpy(ioc->sas_mgmt.reply, reply,
  978. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  979. }
  980. complete(&ioc->sas_mgmt.done);
  981. return 1;
  982. }
  983. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  984. {
  985. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  986. SasIoUnitControlRequest_t *req;
  987. SasIoUnitControlReply_t *reply;
  988. MPT_FRAME_HDR *mf;
  989. MPIHeader_t *hdr;
  990. unsigned long timeleft;
  991. int error = -ERESTARTSYS;
  992. /* FIXME: fusion doesn't allow non-local phy reset */
  993. if (!scsi_is_sas_phy_local(phy))
  994. return -EINVAL;
  995. /* not implemented for expanders */
  996. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  997. return -ENXIO;
  998. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  999. goto out;
  1000. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1001. if (!mf) {
  1002. error = -ENOMEM;
  1003. goto out_unlock;
  1004. }
  1005. hdr = (MPIHeader_t *) mf;
  1006. req = (SasIoUnitControlRequest_t *)mf;
  1007. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  1008. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  1009. req->MsgContext = hdr->MsgContext;
  1010. req->Operation = hard_reset ?
  1011. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  1012. req->PhyNum = phy->identify.phy_identifier;
  1013. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1014. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  1015. 10 * HZ);
  1016. if (!timeleft) {
  1017. /* On timeout reset the board */
  1018. mpt_free_msg_frame(ioc, mf);
  1019. mpt_HardResetHandler(ioc, CAN_SLEEP);
  1020. error = -ETIMEDOUT;
  1021. goto out_unlock;
  1022. }
  1023. /* a reply frame is expected */
  1024. if ((ioc->sas_mgmt.status &
  1025. MPT_IOCTL_STATUS_RF_VALID) == 0) {
  1026. error = -ENXIO;
  1027. goto out_unlock;
  1028. }
  1029. /* process the completed Reply Message Frame */
  1030. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  1031. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  1032. printk(MYIOC_s_INFO_FMT "%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  1033. ioc->name, __func__, reply->IOCStatus, reply->IOCLogInfo);
  1034. error = -ENXIO;
  1035. goto out_unlock;
  1036. }
  1037. error = 0;
  1038. out_unlock:
  1039. mutex_unlock(&ioc->sas_mgmt.mutex);
  1040. out:
  1041. return error;
  1042. }
  1043. static int
  1044. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1045. {
  1046. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1047. int i, error;
  1048. struct mptsas_portinfo *p;
  1049. struct mptsas_enclosure enclosure_info;
  1050. u64 enclosure_handle;
  1051. mutex_lock(&ioc->sas_topology_mutex);
  1052. list_for_each_entry(p, &ioc->sas_topology, list) {
  1053. for (i = 0; i < p->num_phys; i++) {
  1054. if (p->phy_info[i].attached.sas_address ==
  1055. rphy->identify.sas_address) {
  1056. enclosure_handle = p->phy_info[i].
  1057. attached.handle_enclosure;
  1058. goto found_info;
  1059. }
  1060. }
  1061. }
  1062. mutex_unlock(&ioc->sas_topology_mutex);
  1063. return -ENXIO;
  1064. found_info:
  1065. mutex_unlock(&ioc->sas_topology_mutex);
  1066. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  1067. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  1068. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  1069. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  1070. if (!error)
  1071. *identifier = enclosure_info.enclosure_logical_id;
  1072. return error;
  1073. }
  1074. static int
  1075. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  1076. {
  1077. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1078. struct mptsas_portinfo *p;
  1079. int i, rc;
  1080. mutex_lock(&ioc->sas_topology_mutex);
  1081. list_for_each_entry(p, &ioc->sas_topology, list) {
  1082. for (i = 0; i < p->num_phys; i++) {
  1083. if (p->phy_info[i].attached.sas_address ==
  1084. rphy->identify.sas_address) {
  1085. rc = p->phy_info[i].attached.slot;
  1086. goto out;
  1087. }
  1088. }
  1089. }
  1090. rc = -ENXIO;
  1091. out:
  1092. mutex_unlock(&ioc->sas_topology_mutex);
  1093. return rc;
  1094. }
  1095. static int mptsas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1096. struct request *req)
  1097. {
  1098. MPT_ADAPTER *ioc = ((MPT_SCSI_HOST *) shost->hostdata)->ioc;
  1099. MPT_FRAME_HDR *mf;
  1100. SmpPassthroughRequest_t *smpreq;
  1101. struct request *rsp = req->next_rq;
  1102. int ret;
  1103. int flagsLength;
  1104. unsigned long timeleft;
  1105. char *psge;
  1106. dma_addr_t dma_addr_in = 0;
  1107. dma_addr_t dma_addr_out = 0;
  1108. u64 sas_address = 0;
  1109. if (!rsp) {
  1110. printk(MYIOC_s_ERR_FMT "%s: the smp response space is missing\n",
  1111. ioc->name, __func__);
  1112. return -EINVAL;
  1113. }
  1114. /* do we need to support multiple segments? */
  1115. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1116. printk(MYIOC_s_ERR_FMT "%s: multiple segments req %u %u, rsp %u %u\n",
  1117. ioc->name, __func__, req->bio->bi_vcnt, req->data_len,
  1118. rsp->bio->bi_vcnt, rsp->data_len);
  1119. return -EINVAL;
  1120. }
  1121. ret = mutex_lock_interruptible(&ioc->sas_mgmt.mutex);
  1122. if (ret)
  1123. goto out;
  1124. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1125. if (!mf) {
  1126. ret = -ENOMEM;
  1127. goto out_unlock;
  1128. }
  1129. smpreq = (SmpPassthroughRequest_t *)mf;
  1130. memset(smpreq, 0, sizeof(*smpreq));
  1131. smpreq->RequestDataLength = cpu_to_le16(req->data_len - 4);
  1132. smpreq->Function = MPI_FUNCTION_SMP_PASSTHROUGH;
  1133. if (rphy)
  1134. sas_address = rphy->identify.sas_address;
  1135. else {
  1136. struct mptsas_portinfo *port_info;
  1137. mutex_lock(&ioc->sas_topology_mutex);
  1138. port_info = mptsas_get_hba_portinfo(ioc);
  1139. if (port_info && port_info->phy_info)
  1140. sas_address =
  1141. port_info->phy_info[0].phy->identify.sas_address;
  1142. mutex_unlock(&ioc->sas_topology_mutex);
  1143. }
  1144. *((u64 *)&smpreq->SASAddress) = cpu_to_le64(sas_address);
  1145. psge = (char *)
  1146. (((int *) mf) + (offsetof(SmpPassthroughRequest_t, SGL) / 4));
  1147. /* request */
  1148. flagsLength = (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
  1149. MPI_SGE_FLAGS_END_OF_BUFFER |
  1150. MPI_SGE_FLAGS_DIRECTION |
  1151. mpt_addr_size()) << MPI_SGE_FLAGS_SHIFT;
  1152. flagsLength |= (req->data_len - 4);
  1153. dma_addr_out = pci_map_single(ioc->pcidev, bio_data(req->bio),
  1154. req->data_len, PCI_DMA_BIDIRECTIONAL);
  1155. if (!dma_addr_out)
  1156. goto put_mf;
  1157. mpt_add_sge(psge, flagsLength, dma_addr_out);
  1158. psge += (sizeof(u32) + sizeof(dma_addr_t));
  1159. /* response */
  1160. flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
  1161. flagsLength |= rsp->data_len + 4;
  1162. dma_addr_in = pci_map_single(ioc->pcidev, bio_data(rsp->bio),
  1163. rsp->data_len, PCI_DMA_BIDIRECTIONAL);
  1164. if (!dma_addr_in)
  1165. goto unmap;
  1166. mpt_add_sge(psge, flagsLength, dma_addr_in);
  1167. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1168. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done, 10 * HZ);
  1169. if (!timeleft) {
  1170. printk(MYIOC_s_ERR_FMT "%s: smp timeout!\n", ioc->name, __func__);
  1171. /* On timeout reset the board */
  1172. mpt_HardResetHandler(ioc, CAN_SLEEP);
  1173. ret = -ETIMEDOUT;
  1174. goto unmap;
  1175. }
  1176. mf = NULL;
  1177. if (ioc->sas_mgmt.status & MPT_IOCTL_STATUS_RF_VALID) {
  1178. SmpPassthroughReply_t *smprep;
  1179. smprep = (SmpPassthroughReply_t *)ioc->sas_mgmt.reply;
  1180. memcpy(req->sense, smprep, sizeof(*smprep));
  1181. req->sense_len = sizeof(*smprep);
  1182. req->data_len = 0;
  1183. rsp->data_len -= smprep->ResponseDataLength;
  1184. } else {
  1185. printk(MYIOC_s_ERR_FMT "%s: smp passthru reply failed to be returned\n",
  1186. ioc->name, __func__);
  1187. ret = -ENXIO;
  1188. }
  1189. unmap:
  1190. if (dma_addr_out)
  1191. pci_unmap_single(ioc->pcidev, dma_addr_out, req->data_len,
  1192. PCI_DMA_BIDIRECTIONAL);
  1193. if (dma_addr_in)
  1194. pci_unmap_single(ioc->pcidev, dma_addr_in, rsp->data_len,
  1195. PCI_DMA_BIDIRECTIONAL);
  1196. put_mf:
  1197. if (mf)
  1198. mpt_free_msg_frame(ioc, mf);
  1199. out_unlock:
  1200. mutex_unlock(&ioc->sas_mgmt.mutex);
  1201. out:
  1202. return ret;
  1203. }
  1204. static struct sas_function_template mptsas_transport_functions = {
  1205. .get_linkerrors = mptsas_get_linkerrors,
  1206. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  1207. .get_bay_identifier = mptsas_get_bay_identifier,
  1208. .phy_reset = mptsas_phy_reset,
  1209. .smp_handler = mptsas_smp_handler,
  1210. };
  1211. static struct scsi_transport_template *mptsas_transport_template;
  1212. static int
  1213. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  1214. {
  1215. ConfigExtendedPageHeader_t hdr;
  1216. CONFIGPARMS cfg;
  1217. SasIOUnitPage0_t *buffer;
  1218. dma_addr_t dma_handle;
  1219. int error, i;
  1220. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  1221. hdr.ExtPageLength = 0;
  1222. hdr.PageNumber = 0;
  1223. hdr.Reserved1 = 0;
  1224. hdr.Reserved2 = 0;
  1225. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1226. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  1227. cfg.cfghdr.ehdr = &hdr;
  1228. cfg.physAddr = -1;
  1229. cfg.pageAddr = 0;
  1230. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1231. cfg.dir = 0; /* read */
  1232. cfg.timeout = 10;
  1233. error = mpt_config(ioc, &cfg);
  1234. if (error)
  1235. goto out;
  1236. if (!hdr.ExtPageLength) {
  1237. error = -ENXIO;
  1238. goto out;
  1239. }
  1240. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1241. &dma_handle);
  1242. if (!buffer) {
  1243. error = -ENOMEM;
  1244. goto out;
  1245. }
  1246. cfg.physAddr = dma_handle;
  1247. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1248. error = mpt_config(ioc, &cfg);
  1249. if (error)
  1250. goto out_free_consistent;
  1251. port_info->num_phys = buffer->NumPhys;
  1252. port_info->phy_info = kcalloc(port_info->num_phys,
  1253. sizeof(*port_info->phy_info),GFP_KERNEL);
  1254. if (!port_info->phy_info) {
  1255. error = -ENOMEM;
  1256. goto out_free_consistent;
  1257. }
  1258. ioc->nvdata_version_persistent =
  1259. le16_to_cpu(buffer->NvdataVersionPersistent);
  1260. ioc->nvdata_version_default =
  1261. le16_to_cpu(buffer->NvdataVersionDefault);
  1262. for (i = 0; i < port_info->num_phys; i++) {
  1263. mptsas_print_phy_data(ioc, &buffer->PhyData[i]);
  1264. port_info->phy_info[i].phy_id = i;
  1265. port_info->phy_info[i].port_id =
  1266. buffer->PhyData[i].Port;
  1267. port_info->phy_info[i].negotiated_link_rate =
  1268. buffer->PhyData[i].NegotiatedLinkRate;
  1269. port_info->phy_info[i].portinfo = port_info;
  1270. port_info->phy_info[i].handle =
  1271. le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
  1272. }
  1273. out_free_consistent:
  1274. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1275. buffer, dma_handle);
  1276. out:
  1277. return error;
  1278. }
  1279. static int
  1280. mptsas_sas_io_unit_pg1(MPT_ADAPTER *ioc)
  1281. {
  1282. ConfigExtendedPageHeader_t hdr;
  1283. CONFIGPARMS cfg;
  1284. SasIOUnitPage1_t *buffer;
  1285. dma_addr_t dma_handle;
  1286. int error;
  1287. u16 device_missing_delay;
  1288. memset(&hdr, 0, sizeof(ConfigExtendedPageHeader_t));
  1289. memset(&cfg, 0, sizeof(CONFIGPARMS));
  1290. cfg.cfghdr.ehdr = &hdr;
  1291. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1292. cfg.timeout = 10;
  1293. cfg.cfghdr.ehdr->PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1294. cfg.cfghdr.ehdr->ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  1295. cfg.cfghdr.ehdr->PageVersion = MPI_SASIOUNITPAGE1_PAGEVERSION;
  1296. cfg.cfghdr.ehdr->PageNumber = 1;
  1297. error = mpt_config(ioc, &cfg);
  1298. if (error)
  1299. goto out;
  1300. if (!hdr.ExtPageLength) {
  1301. error = -ENXIO;
  1302. goto out;
  1303. }
  1304. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1305. &dma_handle);
  1306. if (!buffer) {
  1307. error = -ENOMEM;
  1308. goto out;
  1309. }
  1310. cfg.physAddr = dma_handle;
  1311. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1312. error = mpt_config(ioc, &cfg);
  1313. if (error)
  1314. goto out_free_consistent;
  1315. ioc->io_missing_delay =
  1316. le16_to_cpu(buffer->IODeviceMissingDelay);
  1317. device_missing_delay = le16_to_cpu(buffer->ReportDeviceMissingDelay);
  1318. ioc->device_missing_delay = (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_UNIT_16) ?
  1319. (device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16 :
  1320. device_missing_delay & MPI_SAS_IOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  1321. out_free_consistent:
  1322. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1323. buffer, dma_handle);
  1324. out:
  1325. return error;
  1326. }
  1327. static int
  1328. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  1329. u32 form, u32 form_specific)
  1330. {
  1331. ConfigExtendedPageHeader_t hdr;
  1332. CONFIGPARMS cfg;
  1333. SasPhyPage0_t *buffer;
  1334. dma_addr_t dma_handle;
  1335. int error;
  1336. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  1337. hdr.ExtPageLength = 0;
  1338. hdr.PageNumber = 0;
  1339. hdr.Reserved1 = 0;
  1340. hdr.Reserved2 = 0;
  1341. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1342. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  1343. cfg.cfghdr.ehdr = &hdr;
  1344. cfg.dir = 0; /* read */
  1345. cfg.timeout = 10;
  1346. /* Get Phy Pg 0 for each Phy. */
  1347. cfg.physAddr = -1;
  1348. cfg.pageAddr = form + form_specific;
  1349. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1350. error = mpt_config(ioc, &cfg);
  1351. if (error)
  1352. goto out;
  1353. if (!hdr.ExtPageLength) {
  1354. error = -ENXIO;
  1355. goto out;
  1356. }
  1357. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1358. &dma_handle);
  1359. if (!buffer) {
  1360. error = -ENOMEM;
  1361. goto out;
  1362. }
  1363. cfg.physAddr = dma_handle;
  1364. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1365. error = mpt_config(ioc, &cfg);
  1366. if (error)
  1367. goto out_free_consistent;
  1368. mptsas_print_phy_pg0(ioc, buffer);
  1369. phy_info->hw_link_rate = buffer->HwLinkRate;
  1370. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  1371. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  1372. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  1373. out_free_consistent:
  1374. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1375. buffer, dma_handle);
  1376. out:
  1377. return error;
  1378. }
  1379. static int
  1380. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  1381. u32 form, u32 form_specific)
  1382. {
  1383. ConfigExtendedPageHeader_t hdr;
  1384. CONFIGPARMS cfg;
  1385. SasDevicePage0_t *buffer;
  1386. dma_addr_t dma_handle;
  1387. __le64 sas_address;
  1388. int error=0;
  1389. if (ioc->sas_discovery_runtime &&
  1390. mptsas_is_end_device(device_info))
  1391. goto out;
  1392. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  1393. hdr.ExtPageLength = 0;
  1394. hdr.PageNumber = 0;
  1395. hdr.Reserved1 = 0;
  1396. hdr.Reserved2 = 0;
  1397. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1398. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  1399. cfg.cfghdr.ehdr = &hdr;
  1400. cfg.pageAddr = form + form_specific;
  1401. cfg.physAddr = -1;
  1402. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1403. cfg.dir = 0; /* read */
  1404. cfg.timeout = 10;
  1405. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  1406. error = mpt_config(ioc, &cfg);
  1407. if (error)
  1408. goto out;
  1409. if (!hdr.ExtPageLength) {
  1410. error = -ENXIO;
  1411. goto out;
  1412. }
  1413. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1414. &dma_handle);
  1415. if (!buffer) {
  1416. error = -ENOMEM;
  1417. goto out;
  1418. }
  1419. cfg.physAddr = dma_handle;
  1420. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1421. error = mpt_config(ioc, &cfg);
  1422. if (error)
  1423. goto out_free_consistent;
  1424. mptsas_print_device_pg0(ioc, buffer);
  1425. device_info->handle = le16_to_cpu(buffer->DevHandle);
  1426. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  1427. device_info->handle_enclosure =
  1428. le16_to_cpu(buffer->EnclosureHandle);
  1429. device_info->slot = le16_to_cpu(buffer->Slot);
  1430. device_info->phy_id = buffer->PhyNum;
  1431. device_info->port_id = buffer->PhysicalPort;
  1432. device_info->id = buffer->TargetID;
  1433. device_info->phys_disk_num = ~0;
  1434. device_info->channel = buffer->Bus;
  1435. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  1436. device_info->sas_address = le64_to_cpu(sas_address);
  1437. device_info->device_info =
  1438. le32_to_cpu(buffer->DeviceInfo);
  1439. out_free_consistent:
  1440. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1441. buffer, dma_handle);
  1442. out:
  1443. return error;
  1444. }
  1445. static int
  1446. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  1447. u32 form, u32 form_specific)
  1448. {
  1449. ConfigExtendedPageHeader_t hdr;
  1450. CONFIGPARMS cfg;
  1451. SasExpanderPage0_t *buffer;
  1452. dma_addr_t dma_handle;
  1453. int i, error;
  1454. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  1455. hdr.ExtPageLength = 0;
  1456. hdr.PageNumber = 0;
  1457. hdr.Reserved1 = 0;
  1458. hdr.Reserved2 = 0;
  1459. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1460. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  1461. cfg.cfghdr.ehdr = &hdr;
  1462. cfg.physAddr = -1;
  1463. cfg.pageAddr = form + form_specific;
  1464. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1465. cfg.dir = 0; /* read */
  1466. cfg.timeout = 10;
  1467. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  1468. error = mpt_config(ioc, &cfg);
  1469. if (error)
  1470. goto out;
  1471. if (!hdr.ExtPageLength) {
  1472. error = -ENXIO;
  1473. goto out;
  1474. }
  1475. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1476. &dma_handle);
  1477. if (!buffer) {
  1478. error = -ENOMEM;
  1479. goto out;
  1480. }
  1481. cfg.physAddr = dma_handle;
  1482. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1483. error = mpt_config(ioc, &cfg);
  1484. if (error)
  1485. goto out_free_consistent;
  1486. if (!buffer->NumPhys) {
  1487. error = -ENODEV;
  1488. goto out_free_consistent;
  1489. }
  1490. /* save config data */
  1491. port_info->num_phys = buffer->NumPhys;
  1492. port_info->phy_info = kcalloc(port_info->num_phys,
  1493. sizeof(*port_info->phy_info),GFP_KERNEL);
  1494. if (!port_info->phy_info) {
  1495. error = -ENOMEM;
  1496. goto out_free_consistent;
  1497. }
  1498. for (i = 0; i < port_info->num_phys; i++) {
  1499. port_info->phy_info[i].portinfo = port_info;
  1500. port_info->phy_info[i].handle =
  1501. le16_to_cpu(buffer->DevHandle);
  1502. }
  1503. out_free_consistent:
  1504. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1505. buffer, dma_handle);
  1506. out:
  1507. return error;
  1508. }
  1509. static int
  1510. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  1511. u32 form, u32 form_specific)
  1512. {
  1513. ConfigExtendedPageHeader_t hdr;
  1514. CONFIGPARMS cfg;
  1515. SasExpanderPage1_t *buffer;
  1516. dma_addr_t dma_handle;
  1517. int error=0;
  1518. if (ioc->sas_discovery_runtime &&
  1519. mptsas_is_end_device(&phy_info->attached))
  1520. goto out;
  1521. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  1522. hdr.ExtPageLength = 0;
  1523. hdr.PageNumber = 1;
  1524. hdr.Reserved1 = 0;
  1525. hdr.Reserved2 = 0;
  1526. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1527. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  1528. cfg.cfghdr.ehdr = &hdr;
  1529. cfg.physAddr = -1;
  1530. cfg.pageAddr = form + form_specific;
  1531. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1532. cfg.dir = 0; /* read */
  1533. cfg.timeout = 10;
  1534. error = mpt_config(ioc, &cfg);
  1535. if (error)
  1536. goto out;
  1537. if (!hdr.ExtPageLength) {
  1538. error = -ENXIO;
  1539. goto out;
  1540. }
  1541. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1542. &dma_handle);
  1543. if (!buffer) {
  1544. error = -ENOMEM;
  1545. goto out;
  1546. }
  1547. cfg.physAddr = dma_handle;
  1548. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1549. error = mpt_config(ioc, &cfg);
  1550. if (error)
  1551. goto out_free_consistent;
  1552. mptsas_print_expander_pg1(ioc, buffer);
  1553. /* save config data */
  1554. phy_info->phy_id = buffer->PhyIdentifier;
  1555. phy_info->port_id = buffer->PhysicalPort;
  1556. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  1557. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  1558. phy_info->hw_link_rate = buffer->HwLinkRate;
  1559. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  1560. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  1561. out_free_consistent:
  1562. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1563. buffer, dma_handle);
  1564. out:
  1565. return error;
  1566. }
  1567. static void
  1568. mptsas_parse_device_info(struct sas_identify *identify,
  1569. struct mptsas_devinfo *device_info)
  1570. {
  1571. u16 protocols;
  1572. identify->sas_address = device_info->sas_address;
  1573. identify->phy_identifier = device_info->phy_id;
  1574. /*
  1575. * Fill in Phy Initiator Port Protocol.
  1576. * Bits 6:3, more than one bit can be set, fall through cases.
  1577. */
  1578. protocols = device_info->device_info & 0x78;
  1579. identify->initiator_port_protocols = 0;
  1580. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  1581. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  1582. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  1583. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  1584. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  1585. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  1586. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  1587. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  1588. /*
  1589. * Fill in Phy Target Port Protocol.
  1590. * Bits 10:7, more than one bit can be set, fall through cases.
  1591. */
  1592. protocols = device_info->device_info & 0x780;
  1593. identify->target_port_protocols = 0;
  1594. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1595. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  1596. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1597. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  1598. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  1599. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  1600. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1601. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  1602. /*
  1603. * Fill in Attached device type.
  1604. */
  1605. switch (device_info->device_info &
  1606. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  1607. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  1608. identify->device_type = SAS_PHY_UNUSED;
  1609. break;
  1610. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  1611. identify->device_type = SAS_END_DEVICE;
  1612. break;
  1613. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  1614. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  1615. break;
  1616. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  1617. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  1618. break;
  1619. }
  1620. }
  1621. static int mptsas_probe_one_phy(struct device *dev,
  1622. struct mptsas_phyinfo *phy_info, int index, int local)
  1623. {
  1624. MPT_ADAPTER *ioc;
  1625. struct sas_phy *phy;
  1626. struct sas_port *port;
  1627. int error = 0;
  1628. if (!dev) {
  1629. error = -ENODEV;
  1630. goto out;
  1631. }
  1632. if (!phy_info->phy) {
  1633. phy = sas_phy_alloc(dev, index);
  1634. if (!phy) {
  1635. error = -ENOMEM;
  1636. goto out;
  1637. }
  1638. } else
  1639. phy = phy_info->phy;
  1640. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  1641. /*
  1642. * Set Negotiated link rate.
  1643. */
  1644. switch (phy_info->negotiated_link_rate) {
  1645. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  1646. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  1647. break;
  1648. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  1649. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  1650. break;
  1651. case MPI_SAS_IOUNIT0_RATE_1_5:
  1652. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1653. break;
  1654. case MPI_SAS_IOUNIT0_RATE_3_0:
  1655. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1656. break;
  1657. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  1658. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  1659. default:
  1660. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  1661. break;
  1662. }
  1663. /*
  1664. * Set Max hardware link rate.
  1665. */
  1666. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1667. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  1668. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1669. break;
  1670. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1671. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1672. break;
  1673. default:
  1674. break;
  1675. }
  1676. /*
  1677. * Set Max programmed link rate.
  1678. */
  1679. switch (phy_info->programmed_link_rate &
  1680. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1681. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  1682. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1683. break;
  1684. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1685. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1686. break;
  1687. default:
  1688. break;
  1689. }
  1690. /*
  1691. * Set Min hardware link rate.
  1692. */
  1693. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  1694. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  1695. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1696. break;
  1697. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1698. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1699. break;
  1700. default:
  1701. break;
  1702. }
  1703. /*
  1704. * Set Min programmed link rate.
  1705. */
  1706. switch (phy_info->programmed_link_rate &
  1707. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  1708. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  1709. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1710. break;
  1711. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1712. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1713. break;
  1714. default:
  1715. break;
  1716. }
  1717. if (!phy_info->phy) {
  1718. error = sas_phy_add(phy);
  1719. if (error) {
  1720. sas_phy_free(phy);
  1721. goto out;
  1722. }
  1723. phy_info->phy = phy;
  1724. }
  1725. if (!phy_info->attached.handle ||
  1726. !phy_info->port_details)
  1727. goto out;
  1728. port = mptsas_get_port(phy_info);
  1729. ioc = phy_to_ioc(phy_info->phy);
  1730. if (phy_info->sas_port_add_phy) {
  1731. if (!port) {
  1732. port = sas_port_alloc_num(dev);
  1733. if (!port) {
  1734. error = -ENOMEM;
  1735. goto out;
  1736. }
  1737. error = sas_port_add(port);
  1738. if (error) {
  1739. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1740. "%s: exit at line=%d\n", ioc->name,
  1741. __func__, __LINE__));
  1742. goto out;
  1743. }
  1744. mptsas_set_port(ioc, phy_info, port);
  1745. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  1746. "sas_port_alloc: port=%p dev=%p port_id=%d\n",
  1747. ioc->name, port, dev, port->port_identifier));
  1748. }
  1749. dsaswideprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sas_port_add_phy: phy_id=%d\n",
  1750. ioc->name, phy_info->phy_id));
  1751. sas_port_add_phy(port, phy_info->phy);
  1752. phy_info->sas_port_add_phy = 0;
  1753. }
  1754. if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
  1755. struct sas_rphy *rphy;
  1756. struct device *parent;
  1757. struct sas_identify identify;
  1758. parent = dev->parent->parent;
  1759. /*
  1760. * Let the hotplug_work thread handle processing
  1761. * the adding/removing of devices that occur
  1762. * after start of day.
  1763. */
  1764. if (ioc->sas_discovery_runtime &&
  1765. mptsas_is_end_device(&phy_info->attached))
  1766. goto out;
  1767. mptsas_parse_device_info(&identify, &phy_info->attached);
  1768. if (scsi_is_host_device(parent)) {
  1769. struct mptsas_portinfo *port_info;
  1770. int i;
  1771. mutex_lock(&ioc->sas_topology_mutex);
  1772. port_info = mptsas_get_hba_portinfo(ioc);
  1773. mutex_unlock(&ioc->sas_topology_mutex);
  1774. for (i = 0; i < port_info->num_phys; i++)
  1775. if (port_info->phy_info[i].identify.sas_address ==
  1776. identify.sas_address) {
  1777. sas_port_mark_backlink(port);
  1778. goto out;
  1779. }
  1780. } else if (scsi_is_sas_rphy(parent)) {
  1781. struct sas_rphy *parent_rphy = dev_to_rphy(parent);
  1782. if (identify.sas_address ==
  1783. parent_rphy->identify.sas_address) {
  1784. sas_port_mark_backlink(port);
  1785. goto out;
  1786. }
  1787. }
  1788. switch (identify.device_type) {
  1789. case SAS_END_DEVICE:
  1790. rphy = sas_end_device_alloc(port);
  1791. break;
  1792. case SAS_EDGE_EXPANDER_DEVICE:
  1793. case SAS_FANOUT_EXPANDER_DEVICE:
  1794. rphy = sas_expander_alloc(port, identify.device_type);
  1795. break;
  1796. default:
  1797. rphy = NULL;
  1798. break;
  1799. }
  1800. if (!rphy) {
  1801. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1802. "%s: exit at line=%d\n", ioc->name,
  1803. __func__, __LINE__));
  1804. goto out;
  1805. }
  1806. rphy->identify = identify;
  1807. error = sas_rphy_add(rphy);
  1808. if (error) {
  1809. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  1810. "%s: exit at line=%d\n", ioc->name,
  1811. __func__, __LINE__));
  1812. sas_rphy_free(rphy);
  1813. goto out;
  1814. }
  1815. mptsas_set_rphy(ioc, phy_info, rphy);
  1816. }
  1817. out:
  1818. return error;
  1819. }
  1820. static int
  1821. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  1822. {
  1823. struct mptsas_portinfo *port_info, *hba;
  1824. int error = -ENOMEM, i;
  1825. hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1826. if (! hba)
  1827. goto out;
  1828. error = mptsas_sas_io_unit_pg0(ioc, hba);
  1829. if (error)
  1830. goto out_free_port_info;
  1831. mptsas_sas_io_unit_pg1(ioc);
  1832. mutex_lock(&ioc->sas_topology_mutex);
  1833. port_info = mptsas_get_hba_portinfo(ioc);
  1834. if (!port_info) {
  1835. port_info = hba;
  1836. list_add_tail(&port_info->list, &ioc->sas_topology);
  1837. } else {
  1838. for (i = 0; i < hba->num_phys; i++) {
  1839. port_info->phy_info[i].negotiated_link_rate =
  1840. hba->phy_info[i].negotiated_link_rate;
  1841. port_info->phy_info[i].handle =
  1842. hba->phy_info[i].handle;
  1843. port_info->phy_info[i].port_id =
  1844. hba->phy_info[i].port_id;
  1845. }
  1846. kfree(hba->phy_info);
  1847. kfree(hba);
  1848. hba = NULL;
  1849. }
  1850. mutex_unlock(&ioc->sas_topology_mutex);
  1851. for (i = 0; i < port_info->num_phys; i++) {
  1852. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  1853. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  1854. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  1855. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  1856. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1857. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1858. port_info->phy_info[i].handle);
  1859. port_info->phy_info[i].identify.phy_id =
  1860. port_info->phy_info[i].phy_id = i;
  1861. if (port_info->phy_info[i].attached.handle)
  1862. mptsas_sas_device_pg0(ioc,
  1863. &port_info->phy_info[i].attached,
  1864. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1865. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1866. port_info->phy_info[i].attached.handle);
  1867. }
  1868. mptsas_setup_wide_ports(ioc, port_info);
  1869. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  1870. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  1871. &port_info->phy_info[i], ioc->sas_index, 1);
  1872. return 0;
  1873. out_free_port_info:
  1874. kfree(hba);
  1875. out:
  1876. return error;
  1877. }
  1878. static int
  1879. mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
  1880. {
  1881. struct mptsas_portinfo *port_info, *p, *ex;
  1882. struct device *parent;
  1883. struct sas_rphy *rphy;
  1884. int error = -ENOMEM, i, j;
  1885. ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1886. if (!ex)
  1887. goto out;
  1888. error = mptsas_sas_expander_pg0(ioc, ex,
  1889. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  1890. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
  1891. if (error)
  1892. goto out_free_port_info;
  1893. *handle = ex->phy_info[0].handle;
  1894. mutex_lock(&ioc->sas_topology_mutex);
  1895. port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
  1896. if (!port_info) {
  1897. port_info = ex;
  1898. list_add_tail(&port_info->list, &ioc->sas_topology);
  1899. } else {
  1900. for (i = 0; i < ex->num_phys; i++) {
  1901. port_info->phy_info[i].handle =
  1902. ex->phy_info[i].handle;
  1903. port_info->phy_info[i].port_id =
  1904. ex->phy_info[i].port_id;
  1905. }
  1906. kfree(ex->phy_info);
  1907. kfree(ex);
  1908. ex = NULL;
  1909. }
  1910. mutex_unlock(&ioc->sas_topology_mutex);
  1911. for (i = 0; i < port_info->num_phys; i++) {
  1912. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  1913. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  1914. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
  1915. if (port_info->phy_info[i].identify.handle) {
  1916. mptsas_sas_device_pg0(ioc,
  1917. &port_info->phy_info[i].identify,
  1918. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1919. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1920. port_info->phy_info[i].identify.handle);
  1921. port_info->phy_info[i].identify.phy_id =
  1922. port_info->phy_info[i].phy_id;
  1923. }
  1924. if (port_info->phy_info[i].attached.handle) {
  1925. mptsas_sas_device_pg0(ioc,
  1926. &port_info->phy_info[i].attached,
  1927. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1928. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1929. port_info->phy_info[i].attached.handle);
  1930. port_info->phy_info[i].attached.phy_id =
  1931. port_info->phy_info[i].phy_id;
  1932. }
  1933. }
  1934. parent = &ioc->sh->shost_gendev;
  1935. for (i = 0; i < port_info->num_phys; i++) {
  1936. mutex_lock(&ioc->sas_topology_mutex);
  1937. list_for_each_entry(p, &ioc->sas_topology, list) {
  1938. for (j = 0; j < p->num_phys; j++) {
  1939. if (port_info->phy_info[i].identify.handle !=
  1940. p->phy_info[j].attached.handle)
  1941. continue;
  1942. rphy = mptsas_get_rphy(&p->phy_info[j]);
  1943. parent = &rphy->dev;
  1944. }
  1945. }
  1946. mutex_unlock(&ioc->sas_topology_mutex);
  1947. }
  1948. mptsas_setup_wide_ports(ioc, port_info);
  1949. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  1950. mptsas_probe_one_phy(parent, &port_info->phy_info[i],
  1951. ioc->sas_index, 0);
  1952. return 0;
  1953. out_free_port_info:
  1954. if (ex) {
  1955. kfree(ex->phy_info);
  1956. kfree(ex);
  1957. }
  1958. out:
  1959. return error;
  1960. }
  1961. /*
  1962. * mptsas_delete_expander_phys
  1963. *
  1964. *
  1965. * This will traverse topology, and remove expanders
  1966. * that are no longer present
  1967. */
  1968. static void
  1969. mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
  1970. {
  1971. struct mptsas_portinfo buffer;
  1972. struct mptsas_portinfo *port_info, *n, *parent;
  1973. struct mptsas_phyinfo *phy_info;
  1974. struct sas_port * port;
  1975. int i;
  1976. u64 expander_sas_address;
  1977. mutex_lock(&ioc->sas_topology_mutex);
  1978. list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
  1979. if (port_info->phy_info &&
  1980. (!(port_info->phy_info[0].identify.device_info &
  1981. MPI_SAS_DEVICE_INFO_SMP_TARGET)))
  1982. continue;
  1983. if (mptsas_sas_expander_pg0(ioc, &buffer,
  1984. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  1985. MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
  1986. port_info->phy_info[0].handle)) {
  1987. /*
  1988. * Obtain the port_info instance to the parent port
  1989. */
  1990. parent = mptsas_find_portinfo_by_handle(ioc,
  1991. port_info->phy_info[0].identify.handle_parent);
  1992. if (!parent)
  1993. goto next_port;
  1994. expander_sas_address =
  1995. port_info->phy_info[0].identify.sas_address;
  1996. /*
  1997. * Delete rphys in the parent that point
  1998. * to this expander. The transport layer will
  1999. * cleanup all the children.
  2000. */
  2001. phy_info = parent->phy_info;
  2002. for (i = 0; i < parent->num_phys; i++, phy_info++) {
  2003. port = mptsas_get_port(phy_info);
  2004. if (!port)
  2005. continue;
  2006. if (phy_info->attached.sas_address !=
  2007. expander_sas_address)
  2008. continue;
  2009. dsaswideprintk(ioc,
  2010. dev_printk(KERN_DEBUG, &port->dev,
  2011. MYIOC_s_FMT "delete port (%d)\n", ioc->name,
  2012. port->port_identifier));
  2013. sas_port_delete(port);
  2014. mptsas_port_delete(ioc, phy_info->port_details);
  2015. }
  2016. next_port:
  2017. phy_info = port_info->phy_info;
  2018. for (i = 0; i < port_info->num_phys; i++, phy_info++)
  2019. mptsas_port_delete(ioc, phy_info->port_details);
  2020. list_del(&port_info->list);
  2021. kfree(port_info->phy_info);
  2022. kfree(port_info);
  2023. }
  2024. /*
  2025. * Free this memory allocated from inside
  2026. * mptsas_sas_expander_pg0
  2027. */
  2028. kfree(buffer.phy_info);
  2029. }
  2030. mutex_unlock(&ioc->sas_topology_mutex);
  2031. }
  2032. /*
  2033. * Start of day discovery
  2034. */
  2035. static void
  2036. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  2037. {
  2038. u32 handle = 0xFFFF;
  2039. int i;
  2040. mutex_lock(&ioc->sas_discovery_mutex);
  2041. mptsas_probe_hba_phys(ioc);
  2042. while (!mptsas_probe_expander_phys(ioc, &handle))
  2043. ;
  2044. /*
  2045. Reporting RAID volumes.
  2046. */
  2047. if (!ioc->ir_firmware)
  2048. goto out;
  2049. if (!ioc->raid_data.pIocPg2)
  2050. goto out;
  2051. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  2052. goto out;
  2053. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  2054. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  2055. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  2056. }
  2057. out:
  2058. mutex_unlock(&ioc->sas_discovery_mutex);
  2059. }
  2060. /*
  2061. * Work queue thread to handle Runtime discovery
  2062. * Mere purpose is the hot add/delete of expanders
  2063. *(Mutex UNLOCKED)
  2064. */
  2065. static void
  2066. __mptsas_discovery_work(MPT_ADAPTER *ioc)
  2067. {
  2068. u32 handle = 0xFFFF;
  2069. ioc->sas_discovery_runtime=1;
  2070. mptsas_delete_expander_phys(ioc);
  2071. mptsas_probe_hba_phys(ioc);
  2072. while (!mptsas_probe_expander_phys(ioc, &handle))
  2073. ;
  2074. ioc->sas_discovery_runtime=0;
  2075. }
  2076. /*
  2077. * Work queue thread to handle Runtime discovery
  2078. * Mere purpose is the hot add/delete of expanders
  2079. *(Mutex LOCKED)
  2080. */
  2081. static void
  2082. mptsas_discovery_work(struct work_struct *work)
  2083. {
  2084. struct mptsas_discovery_event *ev =
  2085. container_of(work, struct mptsas_discovery_event, work);
  2086. MPT_ADAPTER *ioc = ev->ioc;
  2087. mutex_lock(&ioc->sas_discovery_mutex);
  2088. __mptsas_discovery_work(ioc);
  2089. mutex_unlock(&ioc->sas_discovery_mutex);
  2090. kfree(ev);
  2091. }
  2092. static struct mptsas_phyinfo *
  2093. mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  2094. {
  2095. struct mptsas_portinfo *port_info;
  2096. struct mptsas_phyinfo *phy_info = NULL;
  2097. int i;
  2098. mutex_lock(&ioc->sas_topology_mutex);
  2099. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2100. for (i = 0; i < port_info->num_phys; i++) {
  2101. if (!mptsas_is_end_device(
  2102. &port_info->phy_info[i].attached))
  2103. continue;
  2104. if (port_info->phy_info[i].attached.sas_address
  2105. != sas_address)
  2106. continue;
  2107. phy_info = &port_info->phy_info[i];
  2108. break;
  2109. }
  2110. }
  2111. mutex_unlock(&ioc->sas_topology_mutex);
  2112. return phy_info;
  2113. }
  2114. static struct mptsas_phyinfo *
  2115. mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2116. {
  2117. struct mptsas_portinfo *port_info;
  2118. struct mptsas_phyinfo *phy_info = NULL;
  2119. int i;
  2120. mutex_lock(&ioc->sas_topology_mutex);
  2121. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2122. for (i = 0; i < port_info->num_phys; i++) {
  2123. if (!mptsas_is_end_device(
  2124. &port_info->phy_info[i].attached))
  2125. continue;
  2126. if (port_info->phy_info[i].attached.id != id)
  2127. continue;
  2128. if (port_info->phy_info[i].attached.channel != channel)
  2129. continue;
  2130. phy_info = &port_info->phy_info[i];
  2131. break;
  2132. }
  2133. }
  2134. mutex_unlock(&ioc->sas_topology_mutex);
  2135. return phy_info;
  2136. }
  2137. static struct mptsas_phyinfo *
  2138. mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2139. {
  2140. struct mptsas_portinfo *port_info;
  2141. struct mptsas_phyinfo *phy_info = NULL;
  2142. int i;
  2143. mutex_lock(&ioc->sas_topology_mutex);
  2144. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2145. for (i = 0; i < port_info->num_phys; i++) {
  2146. if (!mptsas_is_end_device(
  2147. &port_info->phy_info[i].attached))
  2148. continue;
  2149. if (port_info->phy_info[i].attached.phys_disk_num == ~0)
  2150. continue;
  2151. if (port_info->phy_info[i].attached.phys_disk_num != id)
  2152. continue;
  2153. if (port_info->phy_info[i].attached.channel != channel)
  2154. continue;
  2155. phy_info = &port_info->phy_info[i];
  2156. break;
  2157. }
  2158. }
  2159. mutex_unlock(&ioc->sas_topology_mutex);
  2160. return phy_info;
  2161. }
  2162. /*
  2163. * Work queue thread to clear the persitency table
  2164. */
  2165. static void
  2166. mptsas_persist_clear_table(struct work_struct *work)
  2167. {
  2168. MPT_ADAPTER *ioc = container_of(work, MPT_ADAPTER, sas_persist_task);
  2169. mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
  2170. }
  2171. static void
  2172. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  2173. {
  2174. int rc;
  2175. sdev->no_uld_attach = data ? 1 : 0;
  2176. rc = scsi_device_reprobe(sdev);
  2177. }
  2178. static void
  2179. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  2180. {
  2181. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  2182. mptsas_reprobe_lun);
  2183. }
  2184. static void
  2185. mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2186. {
  2187. CONFIGPARMS cfg;
  2188. ConfigPageHeader_t hdr;
  2189. dma_addr_t dma_handle;
  2190. pRaidVolumePage0_t buffer = NULL;
  2191. RaidPhysDiskPage0_t phys_disk;
  2192. int i;
  2193. struct mptsas_hotplug_event *ev;
  2194. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  2195. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  2196. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  2197. cfg.pageAddr = (channel << 8) + id;
  2198. cfg.cfghdr.hdr = &hdr;
  2199. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2200. if (mpt_config(ioc, &cfg) != 0)
  2201. goto out;
  2202. if (!hdr.PageLength)
  2203. goto out;
  2204. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  2205. &dma_handle);
  2206. if (!buffer)
  2207. goto out;
  2208. cfg.physAddr = dma_handle;
  2209. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2210. if (mpt_config(ioc, &cfg) != 0)
  2211. goto out;
  2212. if (!(buffer->VolumeStatus.Flags &
  2213. MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
  2214. goto out;
  2215. if (!buffer->NumPhysDisks)
  2216. goto out;
  2217. for (i = 0; i < buffer->NumPhysDisks; i++) {
  2218. if (mpt_raid_phys_disk_pg0(ioc,
  2219. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  2220. continue;
  2221. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2222. if (!ev) {
  2223. printk(MYIOC_s_WARN_FMT "mptsas: lost hotplug event\n", ioc->name);
  2224. goto out;
  2225. }
  2226. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2227. ev->ioc = ioc;
  2228. ev->id = phys_disk.PhysDiskID;
  2229. ev->channel = phys_disk.PhysDiskBus;
  2230. ev->phys_disk_num_valid = 1;
  2231. ev->phys_disk_num = phys_disk.PhysDiskNum;
  2232. ev->event_type = MPTSAS_ADD_DEVICE;
  2233. schedule_work(&ev->work);
  2234. }
  2235. out:
  2236. if (buffer)
  2237. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  2238. dma_handle);
  2239. }
  2240. /*
  2241. * Work queue thread to handle SAS hotplug events
  2242. */
  2243. static void
  2244. mptsas_hotplug_work(struct work_struct *work)
  2245. {
  2246. struct mptsas_hotplug_event *ev =
  2247. container_of(work, struct mptsas_hotplug_event, work);
  2248. MPT_ADAPTER *ioc = ev->ioc;
  2249. struct mptsas_phyinfo *phy_info;
  2250. struct sas_rphy *rphy;
  2251. struct sas_port *port;
  2252. struct scsi_device *sdev;
  2253. struct scsi_target * starget;
  2254. struct sas_identify identify;
  2255. char *ds = NULL;
  2256. struct mptsas_devinfo sas_device;
  2257. VirtTarget *vtarget;
  2258. VirtDevice *vdevice;
  2259. mutex_lock(&ioc->sas_discovery_mutex);
  2260. switch (ev->event_type) {
  2261. case MPTSAS_DEL_DEVICE:
  2262. phy_info = NULL;
  2263. if (ev->phys_disk_num_valid) {
  2264. if (ev->hidden_raid_component){
  2265. if (mptsas_sas_device_pg0(ioc, &sas_device,
  2266. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  2267. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2268. (ev->channel << 8) + ev->id)) {
  2269. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2270. "%s: exit at line=%d\n", ioc->name,
  2271. __func__, __LINE__));
  2272. break;
  2273. }
  2274. phy_info = mptsas_find_phyinfo_by_sas_address(
  2275. ioc, sas_device.sas_address);
  2276. }else
  2277. phy_info = mptsas_find_phyinfo_by_phys_disk_num(
  2278. ioc, ev->channel, ev->phys_disk_num);
  2279. }
  2280. if (!phy_info)
  2281. phy_info = mptsas_find_phyinfo_by_target(ioc,
  2282. ev->channel, ev->id);
  2283. /*
  2284. * Sanity checks, for non-existing phys and remote rphys.
  2285. */
  2286. if (!phy_info){
  2287. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2288. "%s: exit at line=%d\n", ioc->name,
  2289. __func__, __LINE__));
  2290. break;
  2291. }
  2292. if (!phy_info->port_details) {
  2293. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2294. "%s: exit at line=%d\n", ioc->name,
  2295. __func__, __LINE__));
  2296. break;
  2297. }
  2298. rphy = mptsas_get_rphy(phy_info);
  2299. if (!rphy) {
  2300. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2301. "%s: exit at line=%d\n", ioc->name,
  2302. __func__, __LINE__));
  2303. break;
  2304. }
  2305. port = mptsas_get_port(phy_info);
  2306. if (!port) {
  2307. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2308. "%s: exit at line=%d\n", ioc->name,
  2309. __func__, __LINE__));
  2310. break;
  2311. }
  2312. starget = mptsas_get_starget(phy_info);
  2313. if (starget) {
  2314. vtarget = starget->hostdata;
  2315. if (!vtarget) {
  2316. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2317. "%s: exit at line=%d\n", ioc->name,
  2318. __func__, __LINE__));
  2319. break;
  2320. }
  2321. /*
  2322. * Handling RAID components
  2323. */
  2324. if (ev->phys_disk_num_valid &&
  2325. ev->hidden_raid_component) {
  2326. printk(MYIOC_s_INFO_FMT
  2327. "RAID Hidding: channel=%d, id=%d, "
  2328. "physdsk %d \n", ioc->name, ev->channel,
  2329. ev->id, ev->phys_disk_num);
  2330. vtarget->id = ev->phys_disk_num;
  2331. vtarget->tflags |=
  2332. MPT_TARGET_FLAGS_RAID_COMPONENT;
  2333. mptsas_reprobe_target(starget, 1);
  2334. phy_info->attached.phys_disk_num =
  2335. ev->phys_disk_num;
  2336. break;
  2337. }
  2338. }
  2339. if (phy_info->attached.device_info &
  2340. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2341. ds = "ssp";
  2342. if (phy_info->attached.device_info &
  2343. MPI_SAS_DEVICE_INFO_STP_TARGET)
  2344. ds = "stp";
  2345. if (phy_info->attached.device_info &
  2346. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2347. ds = "sata";
  2348. printk(MYIOC_s_INFO_FMT
  2349. "removing %s device, channel %d, id %d, phy %d\n",
  2350. ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
  2351. dev_printk(KERN_DEBUG, &port->dev, MYIOC_s_FMT
  2352. "delete port (%d)\n", ioc->name, port->port_identifier);
  2353. sas_port_delete(port);
  2354. mptsas_port_delete(ioc, phy_info->port_details);
  2355. break;
  2356. case MPTSAS_ADD_DEVICE:
  2357. if (ev->phys_disk_num_valid)
  2358. mpt_findImVolumes(ioc);
  2359. /*
  2360. * Refresh sas device pg0 data
  2361. */
  2362. if (mptsas_sas_device_pg0(ioc, &sas_device,
  2363. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  2364. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2365. (ev->channel << 8) + ev->id)) {
  2366. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2367. "%s: exit at line=%d\n", ioc->name,
  2368. __func__, __LINE__));
  2369. break;
  2370. }
  2371. __mptsas_discovery_work(ioc);
  2372. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  2373. sas_device.sas_address);
  2374. if (!phy_info || !phy_info->port_details) {
  2375. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2376. "%s: exit at line=%d\n", ioc->name,
  2377. __func__, __LINE__));
  2378. break;
  2379. }
  2380. starget = mptsas_get_starget(phy_info);
  2381. if (starget && (!ev->hidden_raid_component)){
  2382. vtarget = starget->hostdata;
  2383. if (!vtarget) {
  2384. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2385. "%s: exit at line=%d\n", ioc->name,
  2386. __func__, __LINE__));
  2387. break;
  2388. }
  2389. /*
  2390. * Handling RAID components
  2391. */
  2392. if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2393. printk(MYIOC_s_INFO_FMT
  2394. "RAID Exposing: channel=%d, id=%d, "
  2395. "physdsk %d \n", ioc->name, ev->channel,
  2396. ev->id, ev->phys_disk_num);
  2397. vtarget->tflags &=
  2398. ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  2399. vtarget->id = ev->id;
  2400. mptsas_reprobe_target(starget, 0);
  2401. phy_info->attached.phys_disk_num = ~0;
  2402. }
  2403. break;
  2404. }
  2405. if (mptsas_get_rphy(phy_info)) {
  2406. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2407. "%s: exit at line=%d\n", ioc->name,
  2408. __func__, __LINE__));
  2409. if (ev->channel) printk("%d\n", __LINE__);
  2410. break;
  2411. }
  2412. port = mptsas_get_port(phy_info);
  2413. if (!port) {
  2414. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2415. "%s: exit at line=%d\n", ioc->name,
  2416. __func__, __LINE__));
  2417. break;
  2418. }
  2419. memcpy(&phy_info->attached, &sas_device,
  2420. sizeof(struct mptsas_devinfo));
  2421. if (phy_info->attached.device_info &
  2422. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2423. ds = "ssp";
  2424. if (phy_info->attached.device_info &
  2425. MPI_SAS_DEVICE_INFO_STP_TARGET)
  2426. ds = "stp";
  2427. if (phy_info->attached.device_info &
  2428. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2429. ds = "sata";
  2430. printk(MYIOC_s_INFO_FMT
  2431. "attaching %s device, channel %d, id %d, phy %d\n",
  2432. ioc->name, ds, ev->channel, ev->id, ev->phy_id);
  2433. mptsas_parse_device_info(&identify, &phy_info->attached);
  2434. rphy = sas_end_device_alloc(port);
  2435. if (!rphy) {
  2436. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2437. "%s: exit at line=%d\n", ioc->name,
  2438. __func__, __LINE__));
  2439. break; /* non-fatal: an rphy can be added later */
  2440. }
  2441. rphy->identify = identify;
  2442. if (sas_rphy_add(rphy)) {
  2443. dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
  2444. "%s: exit at line=%d\n", ioc->name,
  2445. __func__, __LINE__));
  2446. sas_rphy_free(rphy);
  2447. break;
  2448. }
  2449. mptsas_set_rphy(ioc, phy_info, rphy);
  2450. break;
  2451. case MPTSAS_ADD_RAID:
  2452. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  2453. ev->id, 0);
  2454. if (sdev) {
  2455. scsi_device_put(sdev);
  2456. break;
  2457. }
  2458. printk(MYIOC_s_INFO_FMT
  2459. "attaching raid volume, channel %d, id %d\n",
  2460. ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
  2461. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
  2462. mpt_findImVolumes(ioc);
  2463. break;
  2464. case MPTSAS_DEL_RAID:
  2465. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  2466. ev->id, 0);
  2467. if (!sdev)
  2468. break;
  2469. printk(MYIOC_s_INFO_FMT
  2470. "removing raid volume, channel %d, id %d\n",
  2471. ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
  2472. vdevice = sdev->hostdata;
  2473. scsi_remove_device(sdev);
  2474. scsi_device_put(sdev);
  2475. mpt_findImVolumes(ioc);
  2476. break;
  2477. case MPTSAS_ADD_INACTIVE_VOLUME:
  2478. mptsas_adding_inactive_raid_components(ioc,
  2479. ev->channel, ev->id);
  2480. break;
  2481. case MPTSAS_IGNORE_EVENT:
  2482. default:
  2483. break;
  2484. }
  2485. mutex_unlock(&ioc->sas_discovery_mutex);
  2486. kfree(ev);
  2487. }
  2488. static void
  2489. mptsas_send_sas_event(MPT_ADAPTER *ioc,
  2490. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  2491. {
  2492. struct mptsas_hotplug_event *ev;
  2493. u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  2494. __le64 sas_address;
  2495. if ((device_info &
  2496. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  2497. MPI_SAS_DEVICE_INFO_STP_TARGET |
  2498. MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
  2499. return;
  2500. switch (sas_event_data->ReasonCode) {
  2501. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  2502. mptsas_target_reset_queue(ioc, sas_event_data);
  2503. break;
  2504. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  2505. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2506. if (!ev) {
  2507. printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
  2508. break;
  2509. }
  2510. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2511. ev->ioc = ioc;
  2512. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  2513. ev->parent_handle =
  2514. le16_to_cpu(sas_event_data->ParentDevHandle);
  2515. ev->channel = sas_event_data->Bus;
  2516. ev->id = sas_event_data->TargetID;
  2517. ev->phy_id = sas_event_data->PhyNum;
  2518. memcpy(&sas_address, &sas_event_data->SASAddress,
  2519. sizeof(__le64));
  2520. ev->sas_address = le64_to_cpu(sas_address);
  2521. ev->device_info = device_info;
  2522. if (sas_event_data->ReasonCode &
  2523. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  2524. ev->event_type = MPTSAS_ADD_DEVICE;
  2525. else
  2526. ev->event_type = MPTSAS_DEL_DEVICE;
  2527. schedule_work(&ev->work);
  2528. break;
  2529. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  2530. /*
  2531. * Persistent table is full.
  2532. */
  2533. INIT_WORK(&ioc->sas_persist_task,
  2534. mptsas_persist_clear_table);
  2535. schedule_work(&ioc->sas_persist_task);
  2536. break;
  2537. /*
  2538. * TODO, handle other events
  2539. */
  2540. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  2541. case MPI_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  2542. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  2543. case MPI_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  2544. case MPI_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  2545. case MPI_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  2546. case MPI_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  2547. default:
  2548. break;
  2549. }
  2550. }
  2551. static void
  2552. mptsas_send_raid_event(MPT_ADAPTER *ioc,
  2553. EVENT_DATA_RAID *raid_event_data)
  2554. {
  2555. struct mptsas_hotplug_event *ev;
  2556. int status = le32_to_cpu(raid_event_data->SettingsStatus);
  2557. int state = (status >> 8) & 0xff;
  2558. if (ioc->bus_type != SAS)
  2559. return;
  2560. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2561. if (!ev) {
  2562. printk(MYIOC_s_WARN_FMT "lost hotplug event\n", ioc->name);
  2563. return;
  2564. }
  2565. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2566. ev->ioc = ioc;
  2567. ev->id = raid_event_data->VolumeID;
  2568. ev->channel = raid_event_data->VolumeBus;
  2569. ev->event_type = MPTSAS_IGNORE_EVENT;
  2570. switch (raid_event_data->ReasonCode) {
  2571. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  2572. ev->phys_disk_num_valid = 1;
  2573. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2574. ev->event_type = MPTSAS_ADD_DEVICE;
  2575. break;
  2576. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  2577. ev->phys_disk_num_valid = 1;
  2578. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2579. ev->hidden_raid_component = 1;
  2580. ev->event_type = MPTSAS_DEL_DEVICE;
  2581. break;
  2582. case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
  2583. switch (state) {
  2584. case MPI_PD_STATE_ONLINE:
  2585. case MPI_PD_STATE_NOT_COMPATIBLE:
  2586. ev->phys_disk_num_valid = 1;
  2587. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2588. ev->hidden_raid_component = 1;
  2589. ev->event_type = MPTSAS_ADD_DEVICE;
  2590. break;
  2591. case MPI_PD_STATE_MISSING:
  2592. case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
  2593. case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
  2594. case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
  2595. ev->phys_disk_num_valid = 1;
  2596. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2597. ev->event_type = MPTSAS_DEL_DEVICE;
  2598. break;
  2599. default:
  2600. break;
  2601. }
  2602. break;
  2603. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  2604. ev->event_type = MPTSAS_DEL_RAID;
  2605. break;
  2606. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  2607. ev->event_type = MPTSAS_ADD_RAID;
  2608. break;
  2609. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  2610. switch (state) {
  2611. case MPI_RAIDVOL0_STATUS_STATE_FAILED:
  2612. case MPI_RAIDVOL0_STATUS_STATE_MISSING:
  2613. ev->event_type = MPTSAS_DEL_RAID;
  2614. break;
  2615. case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
  2616. case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
  2617. ev->event_type = MPTSAS_ADD_RAID;
  2618. break;
  2619. default:
  2620. break;
  2621. }
  2622. break;
  2623. default:
  2624. break;
  2625. }
  2626. schedule_work(&ev->work);
  2627. }
  2628. static void
  2629. mptsas_send_discovery_event(MPT_ADAPTER *ioc,
  2630. EVENT_DATA_SAS_DISCOVERY *discovery_data)
  2631. {
  2632. struct mptsas_discovery_event *ev;
  2633. /*
  2634. * DiscoveryStatus
  2635. *
  2636. * This flag will be non-zero when firmware
  2637. * kicks off discovery, and return to zero
  2638. * once its completed.
  2639. */
  2640. if (discovery_data->DiscoveryStatus)
  2641. return;
  2642. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2643. if (!ev)
  2644. return;
  2645. INIT_WORK(&ev->work, mptsas_discovery_work);
  2646. ev->ioc = ioc;
  2647. schedule_work(&ev->work);
  2648. };
  2649. /*
  2650. * mptsas_send_ir2_event - handle exposing hidden disk when
  2651. * an inactive raid volume is added
  2652. *
  2653. * @ioc: Pointer to MPT_ADAPTER structure
  2654. * @ir2_data
  2655. *
  2656. */
  2657. static void
  2658. mptsas_send_ir2_event(MPT_ADAPTER *ioc, PTR_MPI_EVENT_DATA_IR2 ir2_data)
  2659. {
  2660. struct mptsas_hotplug_event *ev;
  2661. if (ir2_data->ReasonCode !=
  2662. MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED)
  2663. return;
  2664. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2665. if (!ev)
  2666. return;
  2667. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2668. ev->ioc = ioc;
  2669. ev->id = ir2_data->TargetID;
  2670. ev->channel = ir2_data->Bus;
  2671. ev->event_type = MPTSAS_ADD_INACTIVE_VOLUME;
  2672. schedule_work(&ev->work);
  2673. };
  2674. static int
  2675. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  2676. {
  2677. int rc=1;
  2678. u8 event = le32_to_cpu(reply->Event) & 0xFF;
  2679. if (!ioc->sh)
  2680. goto out;
  2681. /*
  2682. * sas_discovery_ignore_events
  2683. *
  2684. * This flag is to prevent anymore processing of
  2685. * sas events once mptsas_remove function is called.
  2686. */
  2687. if (ioc->sas_discovery_ignore_events) {
  2688. rc = mptscsih_event_process(ioc, reply);
  2689. goto out;
  2690. }
  2691. switch (event) {
  2692. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  2693. mptsas_send_sas_event(ioc,
  2694. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
  2695. break;
  2696. case MPI_EVENT_INTEGRATED_RAID:
  2697. mptsas_send_raid_event(ioc,
  2698. (EVENT_DATA_RAID *)reply->Data);
  2699. break;
  2700. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  2701. INIT_WORK(&ioc->sas_persist_task,
  2702. mptsas_persist_clear_table);
  2703. schedule_work(&ioc->sas_persist_task);
  2704. break;
  2705. case MPI_EVENT_SAS_DISCOVERY:
  2706. mptsas_send_discovery_event(ioc,
  2707. (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
  2708. break;
  2709. case MPI_EVENT_IR2:
  2710. mptsas_send_ir2_event(ioc,
  2711. (PTR_MPI_EVENT_DATA_IR2)reply->Data);
  2712. break;
  2713. default:
  2714. rc = mptscsih_event_process(ioc, reply);
  2715. break;
  2716. }
  2717. out:
  2718. return rc;
  2719. }
  2720. static int
  2721. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2722. {
  2723. struct Scsi_Host *sh;
  2724. MPT_SCSI_HOST *hd;
  2725. MPT_ADAPTER *ioc;
  2726. unsigned long flags;
  2727. int ii;
  2728. int numSGE = 0;
  2729. int scale;
  2730. int ioc_cap;
  2731. int error=0;
  2732. int r;
  2733. r = mpt_attach(pdev,id);
  2734. if (r)
  2735. return r;
  2736. ioc = pci_get_drvdata(pdev);
  2737. ioc->DoneCtx = mptsasDoneCtx;
  2738. ioc->TaskCtx = mptsasTaskCtx;
  2739. ioc->InternalCtx = mptsasInternalCtx;
  2740. /* Added sanity check on readiness of the MPT adapter.
  2741. */
  2742. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  2743. printk(MYIOC_s_WARN_FMT
  2744. "Skipping because it's not operational!\n",
  2745. ioc->name);
  2746. error = -ENODEV;
  2747. goto out_mptsas_probe;
  2748. }
  2749. if (!ioc->active) {
  2750. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  2751. ioc->name);
  2752. error = -ENODEV;
  2753. goto out_mptsas_probe;
  2754. }
  2755. /* Sanity check - ensure at least 1 port is INITIATOR capable
  2756. */
  2757. ioc_cap = 0;
  2758. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  2759. if (ioc->pfacts[ii].ProtocolFlags &
  2760. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  2761. ioc_cap++;
  2762. }
  2763. if (!ioc_cap) {
  2764. printk(MYIOC_s_WARN_FMT
  2765. "Skipping ioc=%p because SCSI Initiator mode "
  2766. "is NOT enabled!\n", ioc->name, ioc);
  2767. return 0;
  2768. }
  2769. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  2770. if (!sh) {
  2771. printk(MYIOC_s_WARN_FMT
  2772. "Unable to register controller with SCSI subsystem\n",
  2773. ioc->name);
  2774. error = -1;
  2775. goto out_mptsas_probe;
  2776. }
  2777. spin_lock_irqsave(&ioc->FreeQlock, flags);
  2778. /* Attach the SCSI Host to the IOC structure
  2779. */
  2780. ioc->sh = sh;
  2781. sh->io_port = 0;
  2782. sh->n_io_port = 0;
  2783. sh->irq = 0;
  2784. /* set 16 byte cdb's */
  2785. sh->max_cmd_len = 16;
  2786. sh->max_id = ioc->pfacts[0].PortSCSIID;
  2787. sh->max_lun = max_lun;
  2788. sh->transportt = mptsas_transport_template;
  2789. /* Required entry.
  2790. */
  2791. sh->unique_id = ioc->id;
  2792. INIT_LIST_HEAD(&ioc->sas_topology);
  2793. mutex_init(&ioc->sas_topology_mutex);
  2794. mutex_init(&ioc->sas_discovery_mutex);
  2795. mutex_init(&ioc->sas_mgmt.mutex);
  2796. init_completion(&ioc->sas_mgmt.done);
  2797. /* Verify that we won't exceed the maximum
  2798. * number of chain buffers
  2799. * We can optimize: ZZ = req_sz/sizeof(SGE)
  2800. * For 32bit SGE's:
  2801. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  2802. * + (req_sz - 64)/sizeof(SGE)
  2803. * A slightly different algorithm is required for
  2804. * 64bit SGEs.
  2805. */
  2806. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  2807. if (sizeof(dma_addr_t) == sizeof(u64)) {
  2808. numSGE = (scale - 1) *
  2809. (ioc->facts.MaxChainDepth-1) + scale +
  2810. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  2811. sizeof(u32));
  2812. } else {
  2813. numSGE = 1 + (scale - 1) *
  2814. (ioc->facts.MaxChainDepth-1) + scale +
  2815. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  2816. sizeof(u32));
  2817. }
  2818. if (numSGE < sh->sg_tablesize) {
  2819. /* Reset this value */
  2820. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
  2821. "Resetting sg_tablesize to %d from %d\n",
  2822. ioc->name, numSGE, sh->sg_tablesize));
  2823. sh->sg_tablesize = numSGE;
  2824. }
  2825. hd = shost_priv(sh);
  2826. hd->ioc = ioc;
  2827. /* SCSI needs scsi_cmnd lookup table!
  2828. * (with size equal to req_depth*PtrSz!)
  2829. */
  2830. ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  2831. if (!ioc->ScsiLookup) {
  2832. error = -ENOMEM;
  2833. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2834. goto out_mptsas_probe;
  2835. }
  2836. spin_lock_init(&ioc->scsi_lookup_lock);
  2837. dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
  2838. ioc->name, ioc->ScsiLookup));
  2839. /* Clear the TM flags
  2840. */
  2841. hd->tmPending = 0;
  2842. hd->tmState = TM_STATE_NONE;
  2843. hd->resetPending = 0;
  2844. hd->abortSCpnt = NULL;
  2845. /* Clear the pointer used to store
  2846. * single-threaded commands, i.e., those
  2847. * issued during a bus scan, dv and
  2848. * configuration pages.
  2849. */
  2850. hd->cmdPtr = NULL;
  2851. /* Initialize this SCSI Hosts' timers
  2852. * To use, set the timer expires field
  2853. * and add_timer
  2854. */
  2855. init_timer(&hd->timer);
  2856. hd->timer.data = (unsigned long) hd;
  2857. hd->timer.function = mptscsih_timer_expired;
  2858. ioc->sas_data.ptClear = mpt_pt_clear;
  2859. init_waitqueue_head(&hd->scandv_waitq);
  2860. hd->scandv_wait_done = 0;
  2861. hd->last_queue_full = 0;
  2862. INIT_LIST_HEAD(&hd->target_reset_list);
  2863. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2864. if (ioc->sas_data.ptClear==1) {
  2865. mptbase_sas_persist_operation(
  2866. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  2867. }
  2868. error = scsi_add_host(sh, &ioc->pcidev->dev);
  2869. if (error) {
  2870. dprintk(ioc, printk(MYIOC_s_ERR_FMT
  2871. "scsi_add_host failed\n", ioc->name));
  2872. goto out_mptsas_probe;
  2873. }
  2874. mptsas_scan_sas_topology(ioc);
  2875. return 0;
  2876. out_mptsas_probe:
  2877. mptscsih_remove(pdev);
  2878. return error;
  2879. }
  2880. static void __devexit mptsas_remove(struct pci_dev *pdev)
  2881. {
  2882. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  2883. struct mptsas_portinfo *p, *n;
  2884. int i;
  2885. ioc->sas_discovery_ignore_events = 1;
  2886. sas_remove_host(ioc->sh);
  2887. mutex_lock(&ioc->sas_topology_mutex);
  2888. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  2889. list_del(&p->list);
  2890. for (i = 0 ; i < p->num_phys ; i++)
  2891. mptsas_port_delete(ioc, p->phy_info[i].port_details);
  2892. kfree(p->phy_info);
  2893. kfree(p);
  2894. }
  2895. mutex_unlock(&ioc->sas_topology_mutex);
  2896. mptscsih_remove(pdev);
  2897. }
  2898. static struct pci_device_id mptsas_pci_table[] = {
  2899. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
  2900. PCI_ANY_ID, PCI_ANY_ID },
  2901. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
  2902. PCI_ANY_ID, PCI_ANY_ID },
  2903. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
  2904. PCI_ANY_ID, PCI_ANY_ID },
  2905. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
  2906. PCI_ANY_ID, PCI_ANY_ID },
  2907. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
  2908. PCI_ANY_ID, PCI_ANY_ID },
  2909. {0} /* Terminating entry */
  2910. };
  2911. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  2912. static struct pci_driver mptsas_driver = {
  2913. .name = "mptsas",
  2914. .id_table = mptsas_pci_table,
  2915. .probe = mptsas_probe,
  2916. .remove = __devexit_p(mptsas_remove),
  2917. .shutdown = mptscsih_shutdown,
  2918. #ifdef CONFIG_PM
  2919. .suspend = mptscsih_suspend,
  2920. .resume = mptscsih_resume,
  2921. #endif
  2922. };
  2923. static int __init
  2924. mptsas_init(void)
  2925. {
  2926. int error;
  2927. show_mptmod_ver(my_NAME, my_VERSION);
  2928. mptsas_transport_template =
  2929. sas_attach_transport(&mptsas_transport_functions);
  2930. if (!mptsas_transport_template)
  2931. return -ENODEV;
  2932. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
  2933. mptsasTaskCtx = mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER);
  2934. mptsasInternalCtx =
  2935. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
  2936. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
  2937. mpt_event_register(mptsasDoneCtx, mptsas_event_process);
  2938. mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset);
  2939. error = pci_register_driver(&mptsas_driver);
  2940. if (error)
  2941. sas_release_transport(mptsas_transport_template);
  2942. return error;
  2943. }
  2944. static void __exit
  2945. mptsas_exit(void)
  2946. {
  2947. pci_unregister_driver(&mptsas_driver);
  2948. sas_release_transport(mptsas_transport_template);
  2949. mpt_reset_deregister(mptsasDoneCtx);
  2950. mpt_event_deregister(mptsasDoneCtx);
  2951. mpt_deregister(mptsasMgmtCtx);
  2952. mpt_deregister(mptsasInternalCtx);
  2953. mpt_deregister(mptsasTaskCtx);
  2954. mpt_deregister(mptsasDoneCtx);
  2955. }
  2956. module_init(mptsas_init);
  2957. module_exit(mptsas_exit);