mptsas.c 83 KB

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