mptsas.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282
  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/jiffies.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/delay.h> /* for mdelay */
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_transport_sas.h>
  54. #include <scsi/scsi_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;
  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 (!ioc->sh || !ioc->sh->hostdata)
  735. goto out;
  736. hd = (MPT_SCSI_HOST *)ioc->sh->hostdata;
  737. if (!hd->ioc)
  738. goto out;
  739. if (list_empty(&hd->target_reset_list))
  740. goto out;
  741. /* flush the target_reset_list */
  742. list_for_each_entry_safe(target_reset_list, n,
  743. &hd->target_reset_list, list) {
  744. list_del(&target_reset_list->list);
  745. kfree(target_reset_list);
  746. }
  747. out:
  748. return rc;
  749. }
  750. static int
  751. mptsas_sas_enclosure_pg0(MPT_ADAPTER *ioc, struct mptsas_enclosure *enclosure,
  752. u32 form, u32 form_specific)
  753. {
  754. ConfigExtendedPageHeader_t hdr;
  755. CONFIGPARMS cfg;
  756. SasEnclosurePage0_t *buffer;
  757. dma_addr_t dma_handle;
  758. int error;
  759. __le64 le_identifier;
  760. memset(&hdr, 0, sizeof(hdr));
  761. hdr.PageVersion = MPI_SASENCLOSURE0_PAGEVERSION;
  762. hdr.PageNumber = 0;
  763. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  764. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_ENCLOSURE;
  765. cfg.cfghdr.ehdr = &hdr;
  766. cfg.physAddr = -1;
  767. cfg.pageAddr = form + form_specific;
  768. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  769. cfg.dir = 0; /* read */
  770. cfg.timeout = 10;
  771. error = mpt_config(ioc, &cfg);
  772. if (error)
  773. goto out;
  774. if (!hdr.ExtPageLength) {
  775. error = -ENXIO;
  776. goto out;
  777. }
  778. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  779. &dma_handle);
  780. if (!buffer) {
  781. error = -ENOMEM;
  782. goto out;
  783. }
  784. cfg.physAddr = dma_handle;
  785. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  786. error = mpt_config(ioc, &cfg);
  787. if (error)
  788. goto out_free_consistent;
  789. /* save config data */
  790. memcpy(&le_identifier, &buffer->EnclosureLogicalID, sizeof(__le64));
  791. enclosure->enclosure_logical_id = le64_to_cpu(le_identifier);
  792. enclosure->enclosure_handle = le16_to_cpu(buffer->EnclosureHandle);
  793. enclosure->flags = le16_to_cpu(buffer->Flags);
  794. enclosure->num_slot = le16_to_cpu(buffer->NumSlots);
  795. enclosure->start_slot = le16_to_cpu(buffer->StartSlot);
  796. enclosure->start_id = buffer->StartTargetID;
  797. enclosure->start_channel = buffer->StartBus;
  798. enclosure->sep_id = buffer->SEPTargetID;
  799. enclosure->sep_channel = buffer->SEPBus;
  800. out_free_consistent:
  801. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  802. buffer, dma_handle);
  803. out:
  804. return error;
  805. }
  806. static int
  807. mptsas_slave_configure(struct scsi_device *sdev)
  808. {
  809. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  810. goto out;
  811. sas_read_port_mode_page(sdev);
  812. out:
  813. return mptscsih_slave_configure(sdev);
  814. }
  815. static int
  816. mptsas_target_alloc(struct scsi_target *starget)
  817. {
  818. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  819. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  820. VirtTarget *vtarget;
  821. u8 id, channel;
  822. struct sas_rphy *rphy;
  823. struct mptsas_portinfo *p;
  824. int i;
  825. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  826. if (!vtarget)
  827. return -ENOMEM;
  828. vtarget->starget = starget;
  829. vtarget->ioc_id = hd->ioc->id;
  830. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
  831. id = starget->id;
  832. channel = 0;
  833. /*
  834. * RAID volumes placed beyond the last expected port.
  835. */
  836. if (starget->channel == MPTSAS_RAID_CHANNEL) {
  837. for (i=0; i < hd->ioc->raid_data.pIocPg2->NumActiveVolumes; i++)
  838. if (id == hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID)
  839. channel = hd->ioc->raid_data.pIocPg2->RaidVolume[i].VolumeBus;
  840. goto out;
  841. }
  842. rphy = dev_to_rphy(starget->dev.parent);
  843. mutex_lock(&hd->ioc->sas_topology_mutex);
  844. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  845. for (i = 0; i < p->num_phys; i++) {
  846. if (p->phy_info[i].attached.sas_address !=
  847. rphy->identify.sas_address)
  848. continue;
  849. id = p->phy_info[i].attached.id;
  850. channel = p->phy_info[i].attached.channel;
  851. mptsas_set_starget(&p->phy_info[i], starget);
  852. /*
  853. * Exposing hidden raid components
  854. */
  855. if (mptscsih_is_phys_disk(hd->ioc, channel, id)) {
  856. id = mptscsih_raid_id_to_num(hd->ioc,
  857. channel, id);
  858. vtarget->tflags |=
  859. MPT_TARGET_FLAGS_RAID_COMPONENT;
  860. p->phy_info[i].attached.phys_disk_num = id;
  861. }
  862. mutex_unlock(&hd->ioc->sas_topology_mutex);
  863. goto out;
  864. }
  865. }
  866. mutex_unlock(&hd->ioc->sas_topology_mutex);
  867. kfree(vtarget);
  868. return -ENXIO;
  869. out:
  870. vtarget->id = id;
  871. vtarget->channel = channel;
  872. starget->hostdata = vtarget;
  873. return 0;
  874. }
  875. static void
  876. mptsas_target_destroy(struct scsi_target *starget)
  877. {
  878. struct Scsi_Host *host = dev_to_shost(&starget->dev);
  879. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  880. struct sas_rphy *rphy;
  881. struct mptsas_portinfo *p;
  882. int i;
  883. if (!starget->hostdata)
  884. return;
  885. if (starget->channel == MPTSAS_RAID_CHANNEL)
  886. goto out;
  887. rphy = dev_to_rphy(starget->dev.parent);
  888. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  889. for (i = 0; i < p->num_phys; i++) {
  890. if (p->phy_info[i].attached.sas_address !=
  891. rphy->identify.sas_address)
  892. continue;
  893. mptsas_set_starget(&p->phy_info[i], NULL);
  894. goto out;
  895. }
  896. }
  897. out:
  898. kfree(starget->hostdata);
  899. starget->hostdata = NULL;
  900. }
  901. static int
  902. mptsas_slave_alloc(struct scsi_device *sdev)
  903. {
  904. struct Scsi_Host *host = sdev->host;
  905. MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)host->hostdata;
  906. struct sas_rphy *rphy;
  907. struct mptsas_portinfo *p;
  908. VirtDevice *vdev;
  909. struct scsi_target *starget;
  910. int i;
  911. vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  912. if (!vdev) {
  913. printk(MYIOC_s_ERR_FMT "slave_alloc kzalloc(%zd) FAILED!\n",
  914. hd->ioc->name, sizeof(VirtDevice));
  915. return -ENOMEM;
  916. }
  917. starget = scsi_target(sdev);
  918. vdev->vtarget = starget->hostdata;
  919. if (sdev->channel == MPTSAS_RAID_CHANNEL)
  920. goto out;
  921. rphy = dev_to_rphy(sdev->sdev_target->dev.parent);
  922. mutex_lock(&hd->ioc->sas_topology_mutex);
  923. list_for_each_entry(p, &hd->ioc->sas_topology, list) {
  924. for (i = 0; i < p->num_phys; i++) {
  925. if (p->phy_info[i].attached.sas_address !=
  926. rphy->identify.sas_address)
  927. continue;
  928. vdev->lun = sdev->lun;
  929. /*
  930. * Exposing hidden raid components
  931. */
  932. if (mptscsih_is_phys_disk(hd->ioc,
  933. p->phy_info[i].attached.channel,
  934. p->phy_info[i].attached.id))
  935. sdev->no_uld_attach = 1;
  936. mutex_unlock(&hd->ioc->sas_topology_mutex);
  937. goto out;
  938. }
  939. }
  940. mutex_unlock(&hd->ioc->sas_topology_mutex);
  941. kfree(vdev);
  942. return -ENXIO;
  943. out:
  944. vdev->vtarget->num_luns++;
  945. sdev->hostdata = vdev;
  946. return 0;
  947. }
  948. static int
  949. mptsas_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  950. {
  951. VirtDevice *vdev = SCpnt->device->hostdata;
  952. if (!vdev || !vdev->vtarget || vdev->vtarget->deleted) {
  953. SCpnt->result = DID_NO_CONNECT << 16;
  954. done(SCpnt);
  955. return 0;
  956. }
  957. // scsi_print_command(SCpnt);
  958. return mptscsih_qcmd(SCpnt,done);
  959. }
  960. static struct scsi_host_template mptsas_driver_template = {
  961. .module = THIS_MODULE,
  962. .proc_name = "mptsas",
  963. .proc_info = mptscsih_proc_info,
  964. .name = "MPT SPI Host",
  965. .info = mptscsih_info,
  966. .queuecommand = mptsas_qcmd,
  967. .target_alloc = mptsas_target_alloc,
  968. .slave_alloc = mptsas_slave_alloc,
  969. .slave_configure = mptsas_slave_configure,
  970. .target_destroy = mptsas_target_destroy,
  971. .slave_destroy = mptscsih_slave_destroy,
  972. .change_queue_depth = mptscsih_change_queue_depth,
  973. .eh_abort_handler = mptscsih_abort,
  974. .eh_device_reset_handler = mptscsih_dev_reset,
  975. .eh_bus_reset_handler = mptscsih_bus_reset,
  976. .eh_host_reset_handler = mptscsih_host_reset,
  977. .bios_param = mptscsih_bios_param,
  978. .can_queue = MPT_FC_CAN_QUEUE,
  979. .this_id = -1,
  980. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  981. .max_sectors = 8192,
  982. .cmd_per_lun = 7,
  983. .use_clustering = ENABLE_CLUSTERING,
  984. };
  985. static int mptsas_get_linkerrors(struct sas_phy *phy)
  986. {
  987. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  988. ConfigExtendedPageHeader_t hdr;
  989. CONFIGPARMS cfg;
  990. SasPhyPage1_t *buffer;
  991. dma_addr_t dma_handle;
  992. int error;
  993. /* FIXME: only have link errors on local phys */
  994. if (!scsi_is_sas_phy_local(phy))
  995. return -EINVAL;
  996. hdr.PageVersion = MPI_SASPHY1_PAGEVERSION;
  997. hdr.ExtPageLength = 0;
  998. hdr.PageNumber = 1 /* page number 1*/;
  999. hdr.Reserved1 = 0;
  1000. hdr.Reserved2 = 0;
  1001. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1002. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  1003. cfg.cfghdr.ehdr = &hdr;
  1004. cfg.physAddr = -1;
  1005. cfg.pageAddr = phy->identify.phy_identifier;
  1006. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1007. cfg.dir = 0; /* read */
  1008. cfg.timeout = 10;
  1009. error = mpt_config(ioc, &cfg);
  1010. if (error)
  1011. return error;
  1012. if (!hdr.ExtPageLength)
  1013. return -ENXIO;
  1014. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1015. &dma_handle);
  1016. if (!buffer)
  1017. return -ENOMEM;
  1018. cfg.physAddr = dma_handle;
  1019. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1020. error = mpt_config(ioc, &cfg);
  1021. if (error)
  1022. goto out_free_consistent;
  1023. mptsas_print_phy_pg1(buffer);
  1024. phy->invalid_dword_count = le32_to_cpu(buffer->InvalidDwordCount);
  1025. phy->running_disparity_error_count =
  1026. le32_to_cpu(buffer->RunningDisparityErrorCount);
  1027. phy->loss_of_dword_sync_count =
  1028. le32_to_cpu(buffer->LossDwordSynchCount);
  1029. phy->phy_reset_problem_count =
  1030. le32_to_cpu(buffer->PhyResetProblemCount);
  1031. out_free_consistent:
  1032. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1033. buffer, dma_handle);
  1034. return error;
  1035. }
  1036. static int mptsas_mgmt_done(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req,
  1037. MPT_FRAME_HDR *reply)
  1038. {
  1039. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_COMMAND_GOOD;
  1040. if (reply != NULL) {
  1041. ioc->sas_mgmt.status |= MPT_SAS_MGMT_STATUS_RF_VALID;
  1042. memcpy(ioc->sas_mgmt.reply, reply,
  1043. min(ioc->reply_sz, 4 * reply->u.reply.MsgLength));
  1044. }
  1045. complete(&ioc->sas_mgmt.done);
  1046. return 1;
  1047. }
  1048. static int mptsas_phy_reset(struct sas_phy *phy, int hard_reset)
  1049. {
  1050. MPT_ADAPTER *ioc = phy_to_ioc(phy);
  1051. SasIoUnitControlRequest_t *req;
  1052. SasIoUnitControlReply_t *reply;
  1053. MPT_FRAME_HDR *mf;
  1054. MPIHeader_t *hdr;
  1055. unsigned long timeleft;
  1056. int error = -ERESTARTSYS;
  1057. /* FIXME: fusion doesn't allow non-local phy reset */
  1058. if (!scsi_is_sas_phy_local(phy))
  1059. return -EINVAL;
  1060. /* not implemented for expanders */
  1061. if (phy->identify.target_port_protocols & SAS_PROTOCOL_SMP)
  1062. return -ENXIO;
  1063. if (mutex_lock_interruptible(&ioc->sas_mgmt.mutex))
  1064. goto out;
  1065. mf = mpt_get_msg_frame(mptsasMgmtCtx, ioc);
  1066. if (!mf) {
  1067. error = -ENOMEM;
  1068. goto out_unlock;
  1069. }
  1070. hdr = (MPIHeader_t *) mf;
  1071. req = (SasIoUnitControlRequest_t *)mf;
  1072. memset(req, 0, sizeof(SasIoUnitControlRequest_t));
  1073. req->Function = MPI_FUNCTION_SAS_IO_UNIT_CONTROL;
  1074. req->MsgContext = hdr->MsgContext;
  1075. req->Operation = hard_reset ?
  1076. MPI_SAS_OP_PHY_HARD_RESET : MPI_SAS_OP_PHY_LINK_RESET;
  1077. req->PhyNum = phy->identify.phy_identifier;
  1078. mpt_put_msg_frame(mptsasMgmtCtx, ioc, mf);
  1079. timeleft = wait_for_completion_timeout(&ioc->sas_mgmt.done,
  1080. 10 * HZ);
  1081. if (!timeleft) {
  1082. /* On timeout reset the board */
  1083. mpt_free_msg_frame(ioc, mf);
  1084. mpt_HardResetHandler(ioc, CAN_SLEEP);
  1085. error = -ETIMEDOUT;
  1086. goto out_unlock;
  1087. }
  1088. /* a reply frame is expected */
  1089. if ((ioc->sas_mgmt.status &
  1090. MPT_IOCTL_STATUS_RF_VALID) == 0) {
  1091. error = -ENXIO;
  1092. goto out_unlock;
  1093. }
  1094. /* process the completed Reply Message Frame */
  1095. reply = (SasIoUnitControlReply_t *)ioc->sas_mgmt.reply;
  1096. if (reply->IOCStatus != MPI_IOCSTATUS_SUCCESS) {
  1097. printk("%s: IOCStatus=0x%X IOCLogInfo=0x%X\n",
  1098. __FUNCTION__,
  1099. reply->IOCStatus,
  1100. reply->IOCLogInfo);
  1101. error = -ENXIO;
  1102. goto out_unlock;
  1103. }
  1104. error = 0;
  1105. out_unlock:
  1106. mutex_unlock(&ioc->sas_mgmt.mutex);
  1107. out:
  1108. return error;
  1109. }
  1110. static int
  1111. mptsas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1112. {
  1113. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1114. int i, error;
  1115. struct mptsas_portinfo *p;
  1116. struct mptsas_enclosure enclosure_info;
  1117. u64 enclosure_handle;
  1118. mutex_lock(&ioc->sas_topology_mutex);
  1119. list_for_each_entry(p, &ioc->sas_topology, list) {
  1120. for (i = 0; i < p->num_phys; i++) {
  1121. if (p->phy_info[i].attached.sas_address ==
  1122. rphy->identify.sas_address) {
  1123. enclosure_handle = p->phy_info[i].
  1124. attached.handle_enclosure;
  1125. goto found_info;
  1126. }
  1127. }
  1128. }
  1129. mutex_unlock(&ioc->sas_topology_mutex);
  1130. return -ENXIO;
  1131. found_info:
  1132. mutex_unlock(&ioc->sas_topology_mutex);
  1133. memset(&enclosure_info, 0, sizeof(struct mptsas_enclosure));
  1134. error = mptsas_sas_enclosure_pg0(ioc, &enclosure_info,
  1135. (MPI_SAS_ENCLOS_PGAD_FORM_HANDLE <<
  1136. MPI_SAS_ENCLOS_PGAD_FORM_SHIFT), enclosure_handle);
  1137. if (!error)
  1138. *identifier = enclosure_info.enclosure_logical_id;
  1139. return error;
  1140. }
  1141. static int
  1142. mptsas_get_bay_identifier(struct sas_rphy *rphy)
  1143. {
  1144. MPT_ADAPTER *ioc = rphy_to_ioc(rphy);
  1145. struct mptsas_portinfo *p;
  1146. int i, rc;
  1147. mutex_lock(&ioc->sas_topology_mutex);
  1148. list_for_each_entry(p, &ioc->sas_topology, list) {
  1149. for (i = 0; i < p->num_phys; i++) {
  1150. if (p->phy_info[i].attached.sas_address ==
  1151. rphy->identify.sas_address) {
  1152. rc = p->phy_info[i].attached.slot;
  1153. goto out;
  1154. }
  1155. }
  1156. }
  1157. rc = -ENXIO;
  1158. out:
  1159. mutex_unlock(&ioc->sas_topology_mutex);
  1160. return rc;
  1161. }
  1162. static struct sas_function_template mptsas_transport_functions = {
  1163. .get_linkerrors = mptsas_get_linkerrors,
  1164. .get_enclosure_identifier = mptsas_get_enclosure_identifier,
  1165. .get_bay_identifier = mptsas_get_bay_identifier,
  1166. .phy_reset = mptsas_phy_reset,
  1167. };
  1168. static struct scsi_transport_template *mptsas_transport_template;
  1169. static int
  1170. mptsas_sas_io_unit_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info)
  1171. {
  1172. ConfigExtendedPageHeader_t hdr;
  1173. CONFIGPARMS cfg;
  1174. SasIOUnitPage0_t *buffer;
  1175. dma_addr_t dma_handle;
  1176. int error, i;
  1177. hdr.PageVersion = MPI_SASIOUNITPAGE0_PAGEVERSION;
  1178. hdr.ExtPageLength = 0;
  1179. hdr.PageNumber = 0;
  1180. hdr.Reserved1 = 0;
  1181. hdr.Reserved2 = 0;
  1182. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1183. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT;
  1184. cfg.cfghdr.ehdr = &hdr;
  1185. cfg.physAddr = -1;
  1186. cfg.pageAddr = 0;
  1187. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1188. cfg.dir = 0; /* read */
  1189. cfg.timeout = 10;
  1190. error = mpt_config(ioc, &cfg);
  1191. if (error)
  1192. goto out;
  1193. if (!hdr.ExtPageLength) {
  1194. error = -ENXIO;
  1195. goto out;
  1196. }
  1197. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1198. &dma_handle);
  1199. if (!buffer) {
  1200. error = -ENOMEM;
  1201. goto out;
  1202. }
  1203. cfg.physAddr = dma_handle;
  1204. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1205. error = mpt_config(ioc, &cfg);
  1206. if (error)
  1207. goto out_free_consistent;
  1208. port_info->num_phys = buffer->NumPhys;
  1209. port_info->phy_info = kcalloc(port_info->num_phys,
  1210. sizeof(*port_info->phy_info),GFP_KERNEL);
  1211. if (!port_info->phy_info) {
  1212. error = -ENOMEM;
  1213. goto out_free_consistent;
  1214. }
  1215. for (i = 0; i < port_info->num_phys; i++) {
  1216. mptsas_print_phy_data(&buffer->PhyData[i]);
  1217. port_info->phy_info[i].phy_id = i;
  1218. port_info->phy_info[i].port_id =
  1219. buffer->PhyData[i].Port;
  1220. port_info->phy_info[i].negotiated_link_rate =
  1221. buffer->PhyData[i].NegotiatedLinkRate;
  1222. port_info->phy_info[i].portinfo = port_info;
  1223. port_info->phy_info[i].handle =
  1224. le16_to_cpu(buffer->PhyData[i].ControllerDevHandle);
  1225. }
  1226. out_free_consistent:
  1227. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1228. buffer, dma_handle);
  1229. out:
  1230. return error;
  1231. }
  1232. static int
  1233. mptsas_sas_phy_pg0(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  1234. u32 form, u32 form_specific)
  1235. {
  1236. ConfigExtendedPageHeader_t hdr;
  1237. CONFIGPARMS cfg;
  1238. SasPhyPage0_t *buffer;
  1239. dma_addr_t dma_handle;
  1240. int error;
  1241. hdr.PageVersion = MPI_SASPHY0_PAGEVERSION;
  1242. hdr.ExtPageLength = 0;
  1243. hdr.PageNumber = 0;
  1244. hdr.Reserved1 = 0;
  1245. hdr.Reserved2 = 0;
  1246. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1247. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_PHY;
  1248. cfg.cfghdr.ehdr = &hdr;
  1249. cfg.dir = 0; /* read */
  1250. cfg.timeout = 10;
  1251. /* Get Phy Pg 0 for each Phy. */
  1252. cfg.physAddr = -1;
  1253. cfg.pageAddr = form + form_specific;
  1254. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1255. error = mpt_config(ioc, &cfg);
  1256. if (error)
  1257. goto out;
  1258. if (!hdr.ExtPageLength) {
  1259. error = -ENXIO;
  1260. goto out;
  1261. }
  1262. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1263. &dma_handle);
  1264. if (!buffer) {
  1265. error = -ENOMEM;
  1266. goto out;
  1267. }
  1268. cfg.physAddr = dma_handle;
  1269. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1270. error = mpt_config(ioc, &cfg);
  1271. if (error)
  1272. goto out_free_consistent;
  1273. mptsas_print_phy_pg0(buffer);
  1274. phy_info->hw_link_rate = buffer->HwLinkRate;
  1275. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  1276. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  1277. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  1278. out_free_consistent:
  1279. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1280. buffer, dma_handle);
  1281. out:
  1282. return error;
  1283. }
  1284. static int
  1285. mptsas_sas_device_pg0(MPT_ADAPTER *ioc, struct mptsas_devinfo *device_info,
  1286. u32 form, u32 form_specific)
  1287. {
  1288. ConfigExtendedPageHeader_t hdr;
  1289. CONFIGPARMS cfg;
  1290. SasDevicePage0_t *buffer;
  1291. dma_addr_t dma_handle;
  1292. __le64 sas_address;
  1293. int error=0;
  1294. if (ioc->sas_discovery_runtime &&
  1295. mptsas_is_end_device(device_info))
  1296. goto out;
  1297. hdr.PageVersion = MPI_SASDEVICE0_PAGEVERSION;
  1298. hdr.ExtPageLength = 0;
  1299. hdr.PageNumber = 0;
  1300. hdr.Reserved1 = 0;
  1301. hdr.Reserved2 = 0;
  1302. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1303. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE;
  1304. cfg.cfghdr.ehdr = &hdr;
  1305. cfg.pageAddr = form + form_specific;
  1306. cfg.physAddr = -1;
  1307. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1308. cfg.dir = 0; /* read */
  1309. cfg.timeout = 10;
  1310. memset(device_info, 0, sizeof(struct mptsas_devinfo));
  1311. error = mpt_config(ioc, &cfg);
  1312. if (error)
  1313. goto out;
  1314. if (!hdr.ExtPageLength) {
  1315. error = -ENXIO;
  1316. goto out;
  1317. }
  1318. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1319. &dma_handle);
  1320. if (!buffer) {
  1321. error = -ENOMEM;
  1322. goto out;
  1323. }
  1324. cfg.physAddr = dma_handle;
  1325. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1326. error = mpt_config(ioc, &cfg);
  1327. if (error)
  1328. goto out_free_consistent;
  1329. mptsas_print_device_pg0(buffer);
  1330. device_info->handle = le16_to_cpu(buffer->DevHandle);
  1331. device_info->handle_parent = le16_to_cpu(buffer->ParentDevHandle);
  1332. device_info->handle_enclosure =
  1333. le16_to_cpu(buffer->EnclosureHandle);
  1334. device_info->slot = le16_to_cpu(buffer->Slot);
  1335. device_info->phy_id = buffer->PhyNum;
  1336. device_info->port_id = buffer->PhysicalPort;
  1337. device_info->id = buffer->TargetID;
  1338. device_info->phys_disk_num = ~0;
  1339. device_info->channel = buffer->Bus;
  1340. memcpy(&sas_address, &buffer->SASAddress, sizeof(__le64));
  1341. device_info->sas_address = le64_to_cpu(sas_address);
  1342. device_info->device_info =
  1343. le32_to_cpu(buffer->DeviceInfo);
  1344. out_free_consistent:
  1345. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1346. buffer, dma_handle);
  1347. out:
  1348. return error;
  1349. }
  1350. static int
  1351. mptsas_sas_expander_pg0(MPT_ADAPTER *ioc, struct mptsas_portinfo *port_info,
  1352. u32 form, u32 form_specific)
  1353. {
  1354. ConfigExtendedPageHeader_t hdr;
  1355. CONFIGPARMS cfg;
  1356. SasExpanderPage0_t *buffer;
  1357. dma_addr_t dma_handle;
  1358. int i, error;
  1359. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  1360. hdr.ExtPageLength = 0;
  1361. hdr.PageNumber = 0;
  1362. hdr.Reserved1 = 0;
  1363. hdr.Reserved2 = 0;
  1364. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1365. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  1366. cfg.cfghdr.ehdr = &hdr;
  1367. cfg.physAddr = -1;
  1368. cfg.pageAddr = form + form_specific;
  1369. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1370. cfg.dir = 0; /* read */
  1371. cfg.timeout = 10;
  1372. memset(port_info, 0, sizeof(struct mptsas_portinfo));
  1373. error = mpt_config(ioc, &cfg);
  1374. if (error)
  1375. goto out;
  1376. if (!hdr.ExtPageLength) {
  1377. error = -ENXIO;
  1378. goto out;
  1379. }
  1380. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1381. &dma_handle);
  1382. if (!buffer) {
  1383. error = -ENOMEM;
  1384. goto out;
  1385. }
  1386. cfg.physAddr = dma_handle;
  1387. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1388. error = mpt_config(ioc, &cfg);
  1389. if (error)
  1390. goto out_free_consistent;
  1391. /* save config data */
  1392. port_info->num_phys = buffer->NumPhys;
  1393. port_info->phy_info = kcalloc(port_info->num_phys,
  1394. sizeof(*port_info->phy_info),GFP_KERNEL);
  1395. if (!port_info->phy_info) {
  1396. error = -ENOMEM;
  1397. goto out_free_consistent;
  1398. }
  1399. for (i = 0; i < port_info->num_phys; i++) {
  1400. port_info->phy_info[i].portinfo = port_info;
  1401. port_info->phy_info[i].handle =
  1402. le16_to_cpu(buffer->DevHandle);
  1403. }
  1404. out_free_consistent:
  1405. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1406. buffer, dma_handle);
  1407. out:
  1408. return error;
  1409. }
  1410. static int
  1411. mptsas_sas_expander_pg1(MPT_ADAPTER *ioc, struct mptsas_phyinfo *phy_info,
  1412. u32 form, u32 form_specific)
  1413. {
  1414. ConfigExtendedPageHeader_t hdr;
  1415. CONFIGPARMS cfg;
  1416. SasExpanderPage1_t *buffer;
  1417. dma_addr_t dma_handle;
  1418. int error=0;
  1419. if (ioc->sas_discovery_runtime &&
  1420. mptsas_is_end_device(&phy_info->attached))
  1421. goto out;
  1422. hdr.PageVersion = MPI_SASEXPANDER0_PAGEVERSION;
  1423. hdr.ExtPageLength = 0;
  1424. hdr.PageNumber = 1;
  1425. hdr.Reserved1 = 0;
  1426. hdr.Reserved2 = 0;
  1427. hdr.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
  1428. hdr.ExtPageType = MPI_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
  1429. cfg.cfghdr.ehdr = &hdr;
  1430. cfg.physAddr = -1;
  1431. cfg.pageAddr = form + form_specific;
  1432. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  1433. cfg.dir = 0; /* read */
  1434. cfg.timeout = 10;
  1435. error = mpt_config(ioc, &cfg);
  1436. if (error)
  1437. goto out;
  1438. if (!hdr.ExtPageLength) {
  1439. error = -ENXIO;
  1440. goto out;
  1441. }
  1442. buffer = pci_alloc_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1443. &dma_handle);
  1444. if (!buffer) {
  1445. error = -ENOMEM;
  1446. goto out;
  1447. }
  1448. cfg.physAddr = dma_handle;
  1449. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  1450. error = mpt_config(ioc, &cfg);
  1451. if (error)
  1452. goto out_free_consistent;
  1453. mptsas_print_expander_pg1(buffer);
  1454. /* save config data */
  1455. phy_info->phy_id = buffer->PhyIdentifier;
  1456. phy_info->port_id = buffer->PhysicalPort;
  1457. phy_info->negotiated_link_rate = buffer->NegotiatedLinkRate;
  1458. phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
  1459. phy_info->hw_link_rate = buffer->HwLinkRate;
  1460. phy_info->identify.handle = le16_to_cpu(buffer->OwnerDevHandle);
  1461. phy_info->attached.handle = le16_to_cpu(buffer->AttachedDevHandle);
  1462. out_free_consistent:
  1463. pci_free_consistent(ioc->pcidev, hdr.ExtPageLength * 4,
  1464. buffer, dma_handle);
  1465. out:
  1466. return error;
  1467. }
  1468. static void
  1469. mptsas_parse_device_info(struct sas_identify *identify,
  1470. struct mptsas_devinfo *device_info)
  1471. {
  1472. u16 protocols;
  1473. identify->sas_address = device_info->sas_address;
  1474. identify->phy_identifier = device_info->phy_id;
  1475. /*
  1476. * Fill in Phy Initiator Port Protocol.
  1477. * Bits 6:3, more than one bit can be set, fall through cases.
  1478. */
  1479. protocols = device_info->device_info & 0x78;
  1480. identify->initiator_port_protocols = 0;
  1481. if (protocols & MPI_SAS_DEVICE_INFO_SSP_INITIATOR)
  1482. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  1483. if (protocols & MPI_SAS_DEVICE_INFO_STP_INITIATOR)
  1484. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  1485. if (protocols & MPI_SAS_DEVICE_INFO_SMP_INITIATOR)
  1486. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  1487. if (protocols & MPI_SAS_DEVICE_INFO_SATA_HOST)
  1488. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  1489. /*
  1490. * Fill in Phy Target Port Protocol.
  1491. * Bits 10:7, more than one bit can be set, fall through cases.
  1492. */
  1493. protocols = device_info->device_info & 0x780;
  1494. identify->target_port_protocols = 0;
  1495. if (protocols & MPI_SAS_DEVICE_INFO_SSP_TARGET)
  1496. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  1497. if (protocols & MPI_SAS_DEVICE_INFO_STP_TARGET)
  1498. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  1499. if (protocols & MPI_SAS_DEVICE_INFO_SMP_TARGET)
  1500. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  1501. if (protocols & MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  1502. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  1503. /*
  1504. * Fill in Attached device type.
  1505. */
  1506. switch (device_info->device_info &
  1507. MPI_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  1508. case MPI_SAS_DEVICE_INFO_NO_DEVICE:
  1509. identify->device_type = SAS_PHY_UNUSED;
  1510. break;
  1511. case MPI_SAS_DEVICE_INFO_END_DEVICE:
  1512. identify->device_type = SAS_END_DEVICE;
  1513. break;
  1514. case MPI_SAS_DEVICE_INFO_EDGE_EXPANDER:
  1515. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  1516. break;
  1517. case MPI_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  1518. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  1519. break;
  1520. }
  1521. }
  1522. static int mptsas_probe_one_phy(struct device *dev,
  1523. struct mptsas_phyinfo *phy_info, int index, int local)
  1524. {
  1525. MPT_ADAPTER *ioc;
  1526. struct sas_phy *phy;
  1527. struct sas_port *port;
  1528. int error = 0;
  1529. if (!dev) {
  1530. error = -ENODEV;
  1531. goto out;
  1532. }
  1533. if (!phy_info->phy) {
  1534. phy = sas_phy_alloc(dev, index);
  1535. if (!phy) {
  1536. error = -ENOMEM;
  1537. goto out;
  1538. }
  1539. } else
  1540. phy = phy_info->phy;
  1541. mptsas_parse_device_info(&phy->identify, &phy_info->identify);
  1542. /*
  1543. * Set Negotiated link rate.
  1544. */
  1545. switch (phy_info->negotiated_link_rate) {
  1546. case MPI_SAS_IOUNIT0_RATE_PHY_DISABLED:
  1547. phy->negotiated_linkrate = SAS_PHY_DISABLED;
  1548. break;
  1549. case MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION:
  1550. phy->negotiated_linkrate = SAS_LINK_RATE_FAILED;
  1551. break;
  1552. case MPI_SAS_IOUNIT0_RATE_1_5:
  1553. phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1554. break;
  1555. case MPI_SAS_IOUNIT0_RATE_3_0:
  1556. phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1557. break;
  1558. case MPI_SAS_IOUNIT0_RATE_SATA_OOB_COMPLETE:
  1559. case MPI_SAS_IOUNIT0_RATE_UNKNOWN:
  1560. default:
  1561. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  1562. break;
  1563. }
  1564. /*
  1565. * Set Max hardware link rate.
  1566. */
  1567. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1568. case MPI_SAS_PHY0_HWRATE_MAX_RATE_1_5:
  1569. phy->maximum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1570. break;
  1571. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1572. phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1573. break;
  1574. default:
  1575. break;
  1576. }
  1577. /*
  1578. * Set Max programmed link rate.
  1579. */
  1580. switch (phy_info->programmed_link_rate &
  1581. MPI_SAS_PHY0_PRATE_MAX_RATE_MASK) {
  1582. case MPI_SAS_PHY0_PRATE_MAX_RATE_1_5:
  1583. phy->maximum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1584. break;
  1585. case MPI_SAS_PHY0_PRATE_MAX_RATE_3_0:
  1586. phy->maximum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1587. break;
  1588. default:
  1589. break;
  1590. }
  1591. /*
  1592. * Set Min hardware link rate.
  1593. */
  1594. switch (phy_info->hw_link_rate & MPI_SAS_PHY0_HWRATE_MIN_RATE_MASK) {
  1595. case MPI_SAS_PHY0_HWRATE_MIN_RATE_1_5:
  1596. phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
  1597. break;
  1598. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1599. phy->minimum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
  1600. break;
  1601. default:
  1602. break;
  1603. }
  1604. /*
  1605. * Set Min programmed link rate.
  1606. */
  1607. switch (phy_info->programmed_link_rate &
  1608. MPI_SAS_PHY0_PRATE_MIN_RATE_MASK) {
  1609. case MPI_SAS_PHY0_PRATE_MIN_RATE_1_5:
  1610. phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
  1611. break;
  1612. case MPI_SAS_PHY0_PRATE_MIN_RATE_3_0:
  1613. phy->minimum_linkrate = SAS_LINK_RATE_3_0_GBPS;
  1614. break;
  1615. default:
  1616. break;
  1617. }
  1618. if (!phy_info->phy) {
  1619. error = sas_phy_add(phy);
  1620. if (error) {
  1621. sas_phy_free(phy);
  1622. goto out;
  1623. }
  1624. phy_info->phy = phy;
  1625. }
  1626. if (!phy_info->attached.handle ||
  1627. !phy_info->port_details)
  1628. goto out;
  1629. port = mptsas_get_port(phy_info);
  1630. ioc = phy_to_ioc(phy_info->phy);
  1631. if (phy_info->sas_port_add_phy) {
  1632. if (!port) {
  1633. port = sas_port_alloc_num(dev);
  1634. if (!port) {
  1635. error = -ENOMEM;
  1636. goto out;
  1637. }
  1638. error = sas_port_add(port);
  1639. if (error) {
  1640. dfailprintk((MYIOC_s_ERR_FMT
  1641. "%s: exit at line=%d\n", ioc->name,
  1642. __FUNCTION__, __LINE__));
  1643. goto out;
  1644. }
  1645. mptsas_set_port(phy_info, port);
  1646. dsaswideprintk((KERN_DEBUG
  1647. "sas_port_alloc: port=%p dev=%p port_id=%d\n",
  1648. port, dev, port->port_identifier));
  1649. }
  1650. dsaswideprintk((KERN_DEBUG "sas_port_add_phy: phy_id=%d\n",
  1651. phy_info->phy_id));
  1652. sas_port_add_phy(port, phy_info->phy);
  1653. phy_info->sas_port_add_phy = 0;
  1654. }
  1655. if (!mptsas_get_rphy(phy_info) && port && !port->rphy) {
  1656. struct sas_rphy *rphy;
  1657. struct device *parent;
  1658. struct sas_identify identify;
  1659. parent = dev->parent->parent;
  1660. /*
  1661. * Let the hotplug_work thread handle processing
  1662. * the adding/removing of devices that occur
  1663. * after start of day.
  1664. */
  1665. if (ioc->sas_discovery_runtime &&
  1666. mptsas_is_end_device(&phy_info->attached))
  1667. goto out;
  1668. mptsas_parse_device_info(&identify, &phy_info->attached);
  1669. if (scsi_is_host_device(parent)) {
  1670. struct mptsas_portinfo *port_info;
  1671. int i;
  1672. mutex_lock(&ioc->sas_topology_mutex);
  1673. port_info = mptsas_find_portinfo_by_handle(ioc,
  1674. ioc->handle);
  1675. mutex_unlock(&ioc->sas_topology_mutex);
  1676. for (i = 0; i < port_info->num_phys; i++)
  1677. if (port_info->phy_info[i].identify.sas_address ==
  1678. identify.sas_address) {
  1679. sas_port_mark_backlink(port);
  1680. goto out;
  1681. }
  1682. } else if (scsi_is_sas_rphy(parent)) {
  1683. struct sas_rphy *parent_rphy = dev_to_rphy(parent);
  1684. if (identify.sas_address ==
  1685. parent_rphy->identify.sas_address) {
  1686. sas_port_mark_backlink(port);
  1687. goto out;
  1688. }
  1689. }
  1690. switch (identify.device_type) {
  1691. case SAS_END_DEVICE:
  1692. rphy = sas_end_device_alloc(port);
  1693. break;
  1694. case SAS_EDGE_EXPANDER_DEVICE:
  1695. case SAS_FANOUT_EXPANDER_DEVICE:
  1696. rphy = sas_expander_alloc(port, identify.device_type);
  1697. break;
  1698. default:
  1699. rphy = NULL;
  1700. break;
  1701. }
  1702. if (!rphy) {
  1703. dfailprintk((MYIOC_s_ERR_FMT
  1704. "%s: exit at line=%d\n", ioc->name,
  1705. __FUNCTION__, __LINE__));
  1706. goto out;
  1707. }
  1708. rphy->identify = identify;
  1709. error = sas_rphy_add(rphy);
  1710. if (error) {
  1711. dfailprintk((MYIOC_s_ERR_FMT
  1712. "%s: exit at line=%d\n", ioc->name,
  1713. __FUNCTION__, __LINE__));
  1714. sas_rphy_free(rphy);
  1715. goto out;
  1716. }
  1717. mptsas_set_rphy(phy_info, rphy);
  1718. }
  1719. out:
  1720. return error;
  1721. }
  1722. static int
  1723. mptsas_probe_hba_phys(MPT_ADAPTER *ioc)
  1724. {
  1725. struct mptsas_portinfo *port_info, *hba;
  1726. int error = -ENOMEM, i;
  1727. hba = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1728. if (! hba)
  1729. goto out;
  1730. error = mptsas_sas_io_unit_pg0(ioc, hba);
  1731. if (error)
  1732. goto out_free_port_info;
  1733. mutex_lock(&ioc->sas_topology_mutex);
  1734. ioc->handle = hba->phy_info[0].handle;
  1735. port_info = mptsas_find_portinfo_by_handle(ioc, ioc->handle);
  1736. if (!port_info) {
  1737. port_info = hba;
  1738. list_add_tail(&port_info->list, &ioc->sas_topology);
  1739. } else {
  1740. for (i = 0; i < hba->num_phys; i++) {
  1741. port_info->phy_info[i].negotiated_link_rate =
  1742. hba->phy_info[i].negotiated_link_rate;
  1743. port_info->phy_info[i].handle =
  1744. hba->phy_info[i].handle;
  1745. port_info->phy_info[i].port_id =
  1746. hba->phy_info[i].port_id;
  1747. }
  1748. kfree(hba->phy_info);
  1749. kfree(hba);
  1750. hba = NULL;
  1751. }
  1752. mutex_unlock(&ioc->sas_topology_mutex);
  1753. for (i = 0; i < port_info->num_phys; i++) {
  1754. mptsas_sas_phy_pg0(ioc, &port_info->phy_info[i],
  1755. (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
  1756. MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
  1757. mptsas_sas_device_pg0(ioc, &port_info->phy_info[i].identify,
  1758. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1759. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1760. port_info->phy_info[i].handle);
  1761. port_info->phy_info[i].identify.phy_id =
  1762. port_info->phy_info[i].phy_id = i;
  1763. if (port_info->phy_info[i].attached.handle)
  1764. mptsas_sas_device_pg0(ioc,
  1765. &port_info->phy_info[i].attached,
  1766. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1767. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1768. port_info->phy_info[i].attached.handle);
  1769. }
  1770. mptsas_setup_wide_ports(ioc, port_info);
  1771. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  1772. mptsas_probe_one_phy(&ioc->sh->shost_gendev,
  1773. &port_info->phy_info[i], ioc->sas_index, 1);
  1774. return 0;
  1775. out_free_port_info:
  1776. kfree(hba);
  1777. out:
  1778. return error;
  1779. }
  1780. static int
  1781. mptsas_probe_expander_phys(MPT_ADAPTER *ioc, u32 *handle)
  1782. {
  1783. struct mptsas_portinfo *port_info, *p, *ex;
  1784. struct device *parent;
  1785. struct sas_rphy *rphy;
  1786. int error = -ENOMEM, i, j;
  1787. ex = kzalloc(sizeof(*port_info), GFP_KERNEL);
  1788. if (!ex)
  1789. goto out;
  1790. error = mptsas_sas_expander_pg0(ioc, ex,
  1791. (MPI_SAS_EXPAND_PGAD_FORM_GET_NEXT_HANDLE <<
  1792. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), *handle);
  1793. if (error)
  1794. goto out_free_port_info;
  1795. *handle = ex->phy_info[0].handle;
  1796. mutex_lock(&ioc->sas_topology_mutex);
  1797. port_info = mptsas_find_portinfo_by_handle(ioc, *handle);
  1798. if (!port_info) {
  1799. port_info = ex;
  1800. list_add_tail(&port_info->list, &ioc->sas_topology);
  1801. } else {
  1802. for (i = 0; i < ex->num_phys; i++) {
  1803. port_info->phy_info[i].handle =
  1804. ex->phy_info[i].handle;
  1805. port_info->phy_info[i].port_id =
  1806. ex->phy_info[i].port_id;
  1807. }
  1808. kfree(ex->phy_info);
  1809. kfree(ex);
  1810. ex = NULL;
  1811. }
  1812. mutex_unlock(&ioc->sas_topology_mutex);
  1813. for (i = 0; i < port_info->num_phys; i++) {
  1814. mptsas_sas_expander_pg1(ioc, &port_info->phy_info[i],
  1815. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE_PHY_NUM <<
  1816. MPI_SAS_EXPAND_PGAD_FORM_SHIFT), (i << 16) + *handle);
  1817. if (port_info->phy_info[i].identify.handle) {
  1818. mptsas_sas_device_pg0(ioc,
  1819. &port_info->phy_info[i].identify,
  1820. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1821. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1822. port_info->phy_info[i].identify.handle);
  1823. port_info->phy_info[i].identify.phy_id =
  1824. port_info->phy_info[i].phy_id;
  1825. }
  1826. if (port_info->phy_info[i].attached.handle) {
  1827. mptsas_sas_device_pg0(ioc,
  1828. &port_info->phy_info[i].attached,
  1829. (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
  1830. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  1831. port_info->phy_info[i].attached.handle);
  1832. port_info->phy_info[i].attached.phy_id =
  1833. port_info->phy_info[i].phy_id;
  1834. }
  1835. }
  1836. parent = &ioc->sh->shost_gendev;
  1837. for (i = 0; i < port_info->num_phys; i++) {
  1838. mutex_lock(&ioc->sas_topology_mutex);
  1839. list_for_each_entry(p, &ioc->sas_topology, list) {
  1840. for (j = 0; j < p->num_phys; j++) {
  1841. if (port_info->phy_info[i].identify.handle !=
  1842. p->phy_info[j].attached.handle)
  1843. continue;
  1844. rphy = mptsas_get_rphy(&p->phy_info[j]);
  1845. parent = &rphy->dev;
  1846. }
  1847. }
  1848. mutex_unlock(&ioc->sas_topology_mutex);
  1849. }
  1850. mptsas_setup_wide_ports(ioc, port_info);
  1851. for (i = 0; i < port_info->num_phys; i++, ioc->sas_index++)
  1852. mptsas_probe_one_phy(parent, &port_info->phy_info[i],
  1853. ioc->sas_index, 0);
  1854. return 0;
  1855. out_free_port_info:
  1856. if (ex) {
  1857. kfree(ex->phy_info);
  1858. kfree(ex);
  1859. }
  1860. out:
  1861. return error;
  1862. }
  1863. /*
  1864. * mptsas_delete_expander_phys
  1865. *
  1866. *
  1867. * This will traverse topology, and remove expanders
  1868. * that are no longer present
  1869. */
  1870. static void
  1871. mptsas_delete_expander_phys(MPT_ADAPTER *ioc)
  1872. {
  1873. struct mptsas_portinfo buffer;
  1874. struct mptsas_portinfo *port_info, *n, *parent;
  1875. struct mptsas_phyinfo *phy_info;
  1876. struct sas_port * port;
  1877. int i;
  1878. u64 expander_sas_address;
  1879. mutex_lock(&ioc->sas_topology_mutex);
  1880. list_for_each_entry_safe(port_info, n, &ioc->sas_topology, list) {
  1881. if (port_info->phy_info &&
  1882. (!(port_info->phy_info[0].identify.device_info &
  1883. MPI_SAS_DEVICE_INFO_SMP_TARGET)))
  1884. continue;
  1885. if (mptsas_sas_expander_pg0(ioc, &buffer,
  1886. (MPI_SAS_EXPAND_PGAD_FORM_HANDLE <<
  1887. MPI_SAS_EXPAND_PGAD_FORM_SHIFT),
  1888. port_info->phy_info[0].handle)) {
  1889. /*
  1890. * Obtain the port_info instance to the parent port
  1891. */
  1892. parent = mptsas_find_portinfo_by_handle(ioc,
  1893. port_info->phy_info[0].identify.handle_parent);
  1894. if (!parent)
  1895. goto next_port;
  1896. expander_sas_address =
  1897. port_info->phy_info[0].identify.sas_address;
  1898. /*
  1899. * Delete rphys in the parent that point
  1900. * to this expander. The transport layer will
  1901. * cleanup all the children.
  1902. */
  1903. phy_info = parent->phy_info;
  1904. for (i = 0; i < parent->num_phys; i++, phy_info++) {
  1905. port = mptsas_get_port(phy_info);
  1906. if (!port)
  1907. continue;
  1908. if (phy_info->attached.sas_address !=
  1909. expander_sas_address)
  1910. continue;
  1911. #ifdef MPT_DEBUG_SAS_WIDE
  1912. dev_printk(KERN_DEBUG, &port->dev,
  1913. "delete port (%d)\n", port->port_identifier);
  1914. #endif
  1915. sas_port_delete(port);
  1916. mptsas_port_delete(phy_info->port_details);
  1917. }
  1918. next_port:
  1919. phy_info = port_info->phy_info;
  1920. for (i = 0; i < port_info->num_phys; i++, phy_info++)
  1921. mptsas_port_delete(phy_info->port_details);
  1922. list_del(&port_info->list);
  1923. kfree(port_info->phy_info);
  1924. kfree(port_info);
  1925. }
  1926. /*
  1927. * Free this memory allocated from inside
  1928. * mptsas_sas_expander_pg0
  1929. */
  1930. kfree(buffer.phy_info);
  1931. }
  1932. mutex_unlock(&ioc->sas_topology_mutex);
  1933. }
  1934. /*
  1935. * Start of day discovery
  1936. */
  1937. static void
  1938. mptsas_scan_sas_topology(MPT_ADAPTER *ioc)
  1939. {
  1940. u32 handle = 0xFFFF;
  1941. int i;
  1942. mutex_lock(&ioc->sas_discovery_mutex);
  1943. mptsas_probe_hba_phys(ioc);
  1944. while (!mptsas_probe_expander_phys(ioc, &handle))
  1945. ;
  1946. /*
  1947. Reporting RAID volumes.
  1948. */
  1949. if (!ioc->ir_firmware)
  1950. goto out;
  1951. if (!ioc->raid_data.pIocPg2)
  1952. goto out;
  1953. if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
  1954. goto out;
  1955. for (i = 0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
  1956. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL,
  1957. ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID, 0);
  1958. }
  1959. out:
  1960. mutex_unlock(&ioc->sas_discovery_mutex);
  1961. }
  1962. /*
  1963. * Work queue thread to handle Runtime discovery
  1964. * Mere purpose is the hot add/delete of expanders
  1965. *(Mutex UNLOCKED)
  1966. */
  1967. static void
  1968. __mptsas_discovery_work(MPT_ADAPTER *ioc)
  1969. {
  1970. u32 handle = 0xFFFF;
  1971. ioc->sas_discovery_runtime=1;
  1972. mptsas_delete_expander_phys(ioc);
  1973. mptsas_probe_hba_phys(ioc);
  1974. while (!mptsas_probe_expander_phys(ioc, &handle))
  1975. ;
  1976. ioc->sas_discovery_runtime=0;
  1977. }
  1978. /*
  1979. * Work queue thread to handle Runtime discovery
  1980. * Mere purpose is the hot add/delete of expanders
  1981. *(Mutex LOCKED)
  1982. */
  1983. static void
  1984. mptsas_discovery_work(struct work_struct *work)
  1985. {
  1986. struct mptsas_discovery_event *ev =
  1987. container_of(work, struct mptsas_discovery_event, work);
  1988. MPT_ADAPTER *ioc = ev->ioc;
  1989. mutex_lock(&ioc->sas_discovery_mutex);
  1990. __mptsas_discovery_work(ioc);
  1991. mutex_unlock(&ioc->sas_discovery_mutex);
  1992. kfree(ev);
  1993. }
  1994. static struct mptsas_phyinfo *
  1995. mptsas_find_phyinfo_by_sas_address(MPT_ADAPTER *ioc, u64 sas_address)
  1996. {
  1997. struct mptsas_portinfo *port_info;
  1998. struct mptsas_phyinfo *phy_info = NULL;
  1999. int i;
  2000. mutex_lock(&ioc->sas_topology_mutex);
  2001. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2002. for (i = 0; i < port_info->num_phys; i++) {
  2003. if (!mptsas_is_end_device(
  2004. &port_info->phy_info[i].attached))
  2005. continue;
  2006. if (port_info->phy_info[i].attached.sas_address
  2007. != sas_address)
  2008. continue;
  2009. phy_info = &port_info->phy_info[i];
  2010. break;
  2011. }
  2012. }
  2013. mutex_unlock(&ioc->sas_topology_mutex);
  2014. return phy_info;
  2015. }
  2016. static struct mptsas_phyinfo *
  2017. mptsas_find_phyinfo_by_target(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2018. {
  2019. struct mptsas_portinfo *port_info;
  2020. struct mptsas_phyinfo *phy_info = NULL;
  2021. int i;
  2022. mutex_lock(&ioc->sas_topology_mutex);
  2023. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2024. for (i = 0; i < port_info->num_phys; i++) {
  2025. if (!mptsas_is_end_device(
  2026. &port_info->phy_info[i].attached))
  2027. continue;
  2028. if (port_info->phy_info[i].attached.id != id)
  2029. continue;
  2030. if (port_info->phy_info[i].attached.channel != channel)
  2031. continue;
  2032. phy_info = &port_info->phy_info[i];
  2033. break;
  2034. }
  2035. }
  2036. mutex_unlock(&ioc->sas_topology_mutex);
  2037. return phy_info;
  2038. }
  2039. static struct mptsas_phyinfo *
  2040. mptsas_find_phyinfo_by_phys_disk_num(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2041. {
  2042. struct mptsas_portinfo *port_info;
  2043. struct mptsas_phyinfo *phy_info = NULL;
  2044. int i;
  2045. mutex_lock(&ioc->sas_topology_mutex);
  2046. list_for_each_entry(port_info, &ioc->sas_topology, list) {
  2047. for (i = 0; i < port_info->num_phys; i++) {
  2048. if (!mptsas_is_end_device(
  2049. &port_info->phy_info[i].attached))
  2050. continue;
  2051. if (port_info->phy_info[i].attached.phys_disk_num == ~0)
  2052. continue;
  2053. if (port_info->phy_info[i].attached.phys_disk_num != id)
  2054. continue;
  2055. if (port_info->phy_info[i].attached.channel != channel)
  2056. continue;
  2057. phy_info = &port_info->phy_info[i];
  2058. break;
  2059. }
  2060. }
  2061. mutex_unlock(&ioc->sas_topology_mutex);
  2062. return phy_info;
  2063. }
  2064. /*
  2065. * Work queue thread to clear the persitency table
  2066. */
  2067. static void
  2068. mptsas_persist_clear_table(struct work_struct *work)
  2069. {
  2070. MPT_ADAPTER *ioc = container_of(work, MPT_ADAPTER, sas_persist_task);
  2071. mptbase_sas_persist_operation(ioc, MPI_SAS_OP_CLEAR_NOT_PRESENT);
  2072. }
  2073. static void
  2074. mptsas_reprobe_lun(struct scsi_device *sdev, void *data)
  2075. {
  2076. int rc;
  2077. sdev->no_uld_attach = data ? 1 : 0;
  2078. rc = scsi_device_reprobe(sdev);
  2079. }
  2080. static void
  2081. mptsas_reprobe_target(struct scsi_target *starget, int uld_attach)
  2082. {
  2083. starget_for_each_device(starget, uld_attach ? (void *)1 : NULL,
  2084. mptsas_reprobe_lun);
  2085. }
  2086. static void
  2087. mptsas_adding_inactive_raid_components(MPT_ADAPTER *ioc, u8 channel, u8 id)
  2088. {
  2089. CONFIGPARMS cfg;
  2090. ConfigPageHeader_t hdr;
  2091. dma_addr_t dma_handle;
  2092. pRaidVolumePage0_t buffer = NULL;
  2093. RaidPhysDiskPage0_t phys_disk;
  2094. int i;
  2095. struct mptsas_hotplug_event *ev;
  2096. memset(&cfg, 0 , sizeof(CONFIGPARMS));
  2097. memset(&hdr, 0 , sizeof(ConfigPageHeader_t));
  2098. hdr.PageType = MPI_CONFIG_PAGETYPE_RAID_VOLUME;
  2099. cfg.pageAddr = (channel << 8) + id;
  2100. cfg.cfghdr.hdr = &hdr;
  2101. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  2102. if (mpt_config(ioc, &cfg) != 0)
  2103. goto out;
  2104. if (!hdr.PageLength)
  2105. goto out;
  2106. buffer = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4,
  2107. &dma_handle);
  2108. if (!buffer)
  2109. goto out;
  2110. cfg.physAddr = dma_handle;
  2111. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  2112. if (mpt_config(ioc, &cfg) != 0)
  2113. goto out;
  2114. if (!(buffer->VolumeStatus.Flags &
  2115. MPI_RAIDVOL0_STATUS_FLAG_VOLUME_INACTIVE))
  2116. goto out;
  2117. if (!buffer->NumPhysDisks)
  2118. goto out;
  2119. for (i = 0; i < buffer->NumPhysDisks; i++) {
  2120. if (mpt_raid_phys_disk_pg0(ioc,
  2121. buffer->PhysDisk[i].PhysDiskNum, &phys_disk) != 0)
  2122. continue;
  2123. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2124. if (!ev) {
  2125. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  2126. goto out;
  2127. }
  2128. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2129. ev->ioc = ioc;
  2130. ev->id = phys_disk.PhysDiskID;
  2131. ev->channel = phys_disk.PhysDiskBus;
  2132. ev->phys_disk_num_valid = 1;
  2133. ev->phys_disk_num = phys_disk.PhysDiskNum;
  2134. ev->event_type = MPTSAS_ADD_DEVICE;
  2135. schedule_work(&ev->work);
  2136. }
  2137. out:
  2138. if (buffer)
  2139. pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, buffer,
  2140. dma_handle);
  2141. }
  2142. /*
  2143. * Work queue thread to handle SAS hotplug events
  2144. */
  2145. static void
  2146. mptsas_hotplug_work(struct work_struct *work)
  2147. {
  2148. struct mptsas_hotplug_event *ev =
  2149. container_of(work, struct mptsas_hotplug_event, work);
  2150. MPT_ADAPTER *ioc = ev->ioc;
  2151. struct mptsas_phyinfo *phy_info;
  2152. struct sas_rphy *rphy;
  2153. struct sas_port *port;
  2154. struct scsi_device *sdev;
  2155. struct scsi_target * starget;
  2156. struct sas_identify identify;
  2157. char *ds = NULL;
  2158. struct mptsas_devinfo sas_device;
  2159. VirtTarget *vtarget;
  2160. VirtDevice *vdevice;
  2161. mutex_lock(&ioc->sas_discovery_mutex);
  2162. switch (ev->event_type) {
  2163. case MPTSAS_DEL_DEVICE:
  2164. phy_info = NULL;
  2165. if (ev->phys_disk_num_valid) {
  2166. if (ev->hidden_raid_component){
  2167. if (mptsas_sas_device_pg0(ioc, &sas_device,
  2168. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  2169. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2170. (ev->channel << 8) + ev->id)) {
  2171. dfailprintk((MYIOC_s_ERR_FMT
  2172. "%s: exit at line=%d\n", ioc->name,
  2173. __FUNCTION__, __LINE__));
  2174. break;
  2175. }
  2176. phy_info = mptsas_find_phyinfo_by_sas_address(
  2177. ioc, sas_device.sas_address);
  2178. }else
  2179. phy_info = mptsas_find_phyinfo_by_phys_disk_num(
  2180. ioc, ev->channel, ev->phys_disk_num);
  2181. }
  2182. if (!phy_info)
  2183. phy_info = mptsas_find_phyinfo_by_target(ioc,
  2184. ev->channel, ev->id);
  2185. /*
  2186. * Sanity checks, for non-existing phys and remote rphys.
  2187. */
  2188. if (!phy_info){
  2189. dfailprintk((MYIOC_s_ERR_FMT
  2190. "%s: exit at line=%d\n", ioc->name,
  2191. __FUNCTION__, __LINE__));
  2192. break;
  2193. }
  2194. if (!phy_info->port_details) {
  2195. dfailprintk((MYIOC_s_ERR_FMT
  2196. "%s: exit at line=%d\n", ioc->name,
  2197. __FUNCTION__, __LINE__));
  2198. break;
  2199. }
  2200. rphy = mptsas_get_rphy(phy_info);
  2201. if (!rphy) {
  2202. dfailprintk((MYIOC_s_ERR_FMT
  2203. "%s: exit at line=%d\n", ioc->name,
  2204. __FUNCTION__, __LINE__));
  2205. break;
  2206. }
  2207. port = mptsas_get_port(phy_info);
  2208. if (!port) {
  2209. dfailprintk((MYIOC_s_ERR_FMT
  2210. "%s: exit at line=%d\n", ioc->name,
  2211. __FUNCTION__, __LINE__));
  2212. break;
  2213. }
  2214. starget = mptsas_get_starget(phy_info);
  2215. if (starget) {
  2216. vtarget = starget->hostdata;
  2217. if (!vtarget) {
  2218. dfailprintk((MYIOC_s_ERR_FMT
  2219. "%s: exit at line=%d\n", ioc->name,
  2220. __FUNCTION__, __LINE__));
  2221. break;
  2222. }
  2223. /*
  2224. * Handling RAID components
  2225. */
  2226. if (ev->phys_disk_num_valid &&
  2227. ev->hidden_raid_component) {
  2228. printk(MYIOC_s_INFO_FMT
  2229. "RAID Hidding: channel=%d, id=%d, "
  2230. "physdsk %d \n", ioc->name, ev->channel,
  2231. ev->id, ev->phys_disk_num);
  2232. vtarget->id = ev->phys_disk_num;
  2233. vtarget->tflags |=
  2234. MPT_TARGET_FLAGS_RAID_COMPONENT;
  2235. mptsas_reprobe_target(starget, 1);
  2236. phy_info->attached.phys_disk_num =
  2237. ev->phys_disk_num;
  2238. break;
  2239. }
  2240. }
  2241. if (phy_info->attached.device_info &
  2242. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2243. ds = "ssp";
  2244. if (phy_info->attached.device_info &
  2245. MPI_SAS_DEVICE_INFO_STP_TARGET)
  2246. ds = "stp";
  2247. if (phy_info->attached.device_info &
  2248. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2249. ds = "sata";
  2250. printk(MYIOC_s_INFO_FMT
  2251. "removing %s device, channel %d, id %d, phy %d\n",
  2252. ioc->name, ds, ev->channel, ev->id, phy_info->phy_id);
  2253. #ifdef MPT_DEBUG_SAS_WIDE
  2254. dev_printk(KERN_DEBUG, &port->dev,
  2255. "delete port (%d)\n", port->port_identifier);
  2256. #endif
  2257. sas_port_delete(port);
  2258. mptsas_port_delete(phy_info->port_details);
  2259. break;
  2260. case MPTSAS_ADD_DEVICE:
  2261. if (ev->phys_disk_num_valid)
  2262. mpt_findImVolumes(ioc);
  2263. /*
  2264. * Refresh sas device pg0 data
  2265. */
  2266. if (mptsas_sas_device_pg0(ioc, &sas_device,
  2267. (MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID <<
  2268. MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
  2269. (ev->channel << 8) + ev->id)) {
  2270. dfailprintk((MYIOC_s_ERR_FMT
  2271. "%s: exit at line=%d\n", ioc->name,
  2272. __FUNCTION__, __LINE__));
  2273. break;
  2274. }
  2275. __mptsas_discovery_work(ioc);
  2276. phy_info = mptsas_find_phyinfo_by_sas_address(ioc,
  2277. sas_device.sas_address);
  2278. if (!phy_info || !phy_info->port_details) {
  2279. dfailprintk((MYIOC_s_ERR_FMT
  2280. "%s: exit at line=%d\n", ioc->name,
  2281. __FUNCTION__, __LINE__));
  2282. break;
  2283. }
  2284. starget = mptsas_get_starget(phy_info);
  2285. if (starget && (!ev->hidden_raid_component)){
  2286. vtarget = starget->hostdata;
  2287. if (!vtarget) {
  2288. dfailprintk((MYIOC_s_ERR_FMT
  2289. "%s: exit at line=%d\n", ioc->name,
  2290. __FUNCTION__, __LINE__));
  2291. break;
  2292. }
  2293. /*
  2294. * Handling RAID components
  2295. */
  2296. if (vtarget->tflags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
  2297. printk(MYIOC_s_INFO_FMT
  2298. "RAID Exposing: channel=%d, id=%d, "
  2299. "physdsk %d \n", ioc->name, ev->channel,
  2300. ev->id, ev->phys_disk_num);
  2301. vtarget->tflags &=
  2302. ~MPT_TARGET_FLAGS_RAID_COMPONENT;
  2303. vtarget->id = ev->id;
  2304. mptsas_reprobe_target(starget, 0);
  2305. phy_info->attached.phys_disk_num = ~0;
  2306. }
  2307. break;
  2308. }
  2309. if (mptsas_get_rphy(phy_info)) {
  2310. dfailprintk((MYIOC_s_ERR_FMT
  2311. "%s: exit at line=%d\n", ioc->name,
  2312. __FUNCTION__, __LINE__));
  2313. if (ev->channel) printk("%d\n", __LINE__);
  2314. break;
  2315. }
  2316. port = mptsas_get_port(phy_info);
  2317. if (!port) {
  2318. dfailprintk((MYIOC_s_ERR_FMT
  2319. "%s: exit at line=%d\n", ioc->name,
  2320. __FUNCTION__, __LINE__));
  2321. break;
  2322. }
  2323. memcpy(&phy_info->attached, &sas_device,
  2324. sizeof(struct mptsas_devinfo));
  2325. if (phy_info->attached.device_info &
  2326. MPI_SAS_DEVICE_INFO_SSP_TARGET)
  2327. ds = "ssp";
  2328. if (phy_info->attached.device_info &
  2329. MPI_SAS_DEVICE_INFO_STP_TARGET)
  2330. ds = "stp";
  2331. if (phy_info->attached.device_info &
  2332. MPI_SAS_DEVICE_INFO_SATA_DEVICE)
  2333. ds = "sata";
  2334. printk(MYIOC_s_INFO_FMT
  2335. "attaching %s device, channel %d, id %d, phy %d\n",
  2336. ioc->name, ds, ev->channel, ev->id, ev->phy_id);
  2337. mptsas_parse_device_info(&identify, &phy_info->attached);
  2338. rphy = sas_end_device_alloc(port);
  2339. if (!rphy) {
  2340. dfailprintk((MYIOC_s_ERR_FMT
  2341. "%s: exit at line=%d\n", ioc->name,
  2342. __FUNCTION__, __LINE__));
  2343. break; /* non-fatal: an rphy can be added later */
  2344. }
  2345. rphy->identify = identify;
  2346. if (sas_rphy_add(rphy)) {
  2347. dfailprintk((MYIOC_s_ERR_FMT
  2348. "%s: exit at line=%d\n", ioc->name,
  2349. __FUNCTION__, __LINE__));
  2350. sas_rphy_free(rphy);
  2351. break;
  2352. }
  2353. mptsas_set_rphy(phy_info, rphy);
  2354. break;
  2355. case MPTSAS_ADD_RAID:
  2356. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  2357. ev->id, 0);
  2358. if (sdev) {
  2359. scsi_device_put(sdev);
  2360. break;
  2361. }
  2362. printk(MYIOC_s_INFO_FMT
  2363. "attaching raid volume, channel %d, id %d\n",
  2364. ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
  2365. scsi_add_device(ioc->sh, MPTSAS_RAID_CHANNEL, ev->id, 0);
  2366. mpt_findImVolumes(ioc);
  2367. break;
  2368. case MPTSAS_DEL_RAID:
  2369. sdev = scsi_device_lookup(ioc->sh, MPTSAS_RAID_CHANNEL,
  2370. ev->id, 0);
  2371. if (!sdev)
  2372. break;
  2373. printk(MYIOC_s_INFO_FMT
  2374. "removing raid volume, channel %d, id %d\n",
  2375. ioc->name, MPTSAS_RAID_CHANNEL, ev->id);
  2376. vdevice = sdev->hostdata;
  2377. scsi_remove_device(sdev);
  2378. scsi_device_put(sdev);
  2379. mpt_findImVolumes(ioc);
  2380. break;
  2381. case MPTSAS_ADD_INACTIVE_VOLUME:
  2382. mptsas_adding_inactive_raid_components(ioc,
  2383. ev->channel, ev->id);
  2384. break;
  2385. case MPTSAS_IGNORE_EVENT:
  2386. default:
  2387. break;
  2388. }
  2389. mutex_unlock(&ioc->sas_discovery_mutex);
  2390. kfree(ev);
  2391. }
  2392. static void
  2393. mptsas_send_sas_event(MPT_ADAPTER *ioc,
  2394. EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *sas_event_data)
  2395. {
  2396. struct mptsas_hotplug_event *ev;
  2397. u32 device_info = le32_to_cpu(sas_event_data->DeviceInfo);
  2398. __le64 sas_address;
  2399. if ((device_info &
  2400. (MPI_SAS_DEVICE_INFO_SSP_TARGET |
  2401. MPI_SAS_DEVICE_INFO_STP_TARGET |
  2402. MPI_SAS_DEVICE_INFO_SATA_DEVICE )) == 0)
  2403. return;
  2404. switch (sas_event_data->ReasonCode) {
  2405. case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
  2406. mptsas_target_reset_queue(ioc, sas_event_data);
  2407. break;
  2408. case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
  2409. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2410. if (!ev) {
  2411. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  2412. break;
  2413. }
  2414. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2415. ev->ioc = ioc;
  2416. ev->handle = le16_to_cpu(sas_event_data->DevHandle);
  2417. ev->parent_handle =
  2418. le16_to_cpu(sas_event_data->ParentDevHandle);
  2419. ev->channel = sas_event_data->Bus;
  2420. ev->id = sas_event_data->TargetID;
  2421. ev->phy_id = sas_event_data->PhyNum;
  2422. memcpy(&sas_address, &sas_event_data->SASAddress,
  2423. sizeof(__le64));
  2424. ev->sas_address = le64_to_cpu(sas_address);
  2425. ev->device_info = device_info;
  2426. if (sas_event_data->ReasonCode &
  2427. MPI_EVENT_SAS_DEV_STAT_RC_ADDED)
  2428. ev->event_type = MPTSAS_ADD_DEVICE;
  2429. else
  2430. ev->event_type = MPTSAS_DEL_DEVICE;
  2431. schedule_work(&ev->work);
  2432. break;
  2433. case MPI_EVENT_SAS_DEV_STAT_RC_NO_PERSIST_ADDED:
  2434. /*
  2435. * Persistent table is full.
  2436. */
  2437. INIT_WORK(&ioc->sas_persist_task,
  2438. mptsas_persist_clear_table);
  2439. schedule_work(&ioc->sas_persist_task);
  2440. break;
  2441. /*
  2442. * TODO, handle other events
  2443. */
  2444. case MPI_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
  2445. case MPI_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
  2446. case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
  2447. case MPI_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
  2448. case MPI_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
  2449. case MPI_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
  2450. case MPI_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
  2451. default:
  2452. break;
  2453. }
  2454. }
  2455. static void
  2456. mptsas_send_raid_event(MPT_ADAPTER *ioc,
  2457. EVENT_DATA_RAID *raid_event_data)
  2458. {
  2459. struct mptsas_hotplug_event *ev;
  2460. int status = le32_to_cpu(raid_event_data->SettingsStatus);
  2461. int state = (status >> 8) & 0xff;
  2462. if (ioc->bus_type != SAS)
  2463. return;
  2464. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2465. if (!ev) {
  2466. printk(KERN_WARNING "mptsas: lost hotplug event\n");
  2467. return;
  2468. }
  2469. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2470. ev->ioc = ioc;
  2471. ev->id = raid_event_data->VolumeID;
  2472. ev->channel = raid_event_data->VolumeBus;
  2473. ev->event_type = MPTSAS_IGNORE_EVENT;
  2474. switch (raid_event_data->ReasonCode) {
  2475. case MPI_EVENT_RAID_RC_PHYSDISK_DELETED:
  2476. ev->phys_disk_num_valid = 1;
  2477. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2478. ev->event_type = MPTSAS_ADD_DEVICE;
  2479. break;
  2480. case MPI_EVENT_RAID_RC_PHYSDISK_CREATED:
  2481. ev->phys_disk_num_valid = 1;
  2482. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2483. ev->hidden_raid_component = 1;
  2484. ev->event_type = MPTSAS_DEL_DEVICE;
  2485. break;
  2486. case MPI_EVENT_RAID_RC_PHYSDISK_STATUS_CHANGED:
  2487. switch (state) {
  2488. case MPI_PD_STATE_ONLINE:
  2489. case MPI_PD_STATE_NOT_COMPATIBLE:
  2490. ev->phys_disk_num_valid = 1;
  2491. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2492. ev->hidden_raid_component = 1;
  2493. ev->event_type = MPTSAS_ADD_DEVICE;
  2494. break;
  2495. case MPI_PD_STATE_MISSING:
  2496. case MPI_PD_STATE_OFFLINE_AT_HOST_REQUEST:
  2497. case MPI_PD_STATE_FAILED_AT_HOST_REQUEST:
  2498. case MPI_PD_STATE_OFFLINE_FOR_ANOTHER_REASON:
  2499. ev->phys_disk_num_valid = 1;
  2500. ev->phys_disk_num = raid_event_data->PhysDiskNum;
  2501. ev->event_type = MPTSAS_DEL_DEVICE;
  2502. break;
  2503. default:
  2504. break;
  2505. }
  2506. break;
  2507. case MPI_EVENT_RAID_RC_VOLUME_DELETED:
  2508. ev->event_type = MPTSAS_DEL_RAID;
  2509. break;
  2510. case MPI_EVENT_RAID_RC_VOLUME_CREATED:
  2511. ev->event_type = MPTSAS_ADD_RAID;
  2512. break;
  2513. case MPI_EVENT_RAID_RC_VOLUME_STATUS_CHANGED:
  2514. switch (state) {
  2515. case MPI_RAIDVOL0_STATUS_STATE_FAILED:
  2516. case MPI_RAIDVOL0_STATUS_STATE_MISSING:
  2517. ev->event_type = MPTSAS_DEL_RAID;
  2518. break;
  2519. case MPI_RAIDVOL0_STATUS_STATE_OPTIMAL:
  2520. case MPI_RAIDVOL0_STATUS_STATE_DEGRADED:
  2521. ev->event_type = MPTSAS_ADD_RAID;
  2522. break;
  2523. default:
  2524. break;
  2525. }
  2526. break;
  2527. default:
  2528. break;
  2529. }
  2530. schedule_work(&ev->work);
  2531. }
  2532. static void
  2533. mptsas_send_discovery_event(MPT_ADAPTER *ioc,
  2534. EVENT_DATA_SAS_DISCOVERY *discovery_data)
  2535. {
  2536. struct mptsas_discovery_event *ev;
  2537. /*
  2538. * DiscoveryStatus
  2539. *
  2540. * This flag will be non-zero when firmware
  2541. * kicks off discovery, and return to zero
  2542. * once its completed.
  2543. */
  2544. if (discovery_data->DiscoveryStatus)
  2545. return;
  2546. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2547. if (!ev)
  2548. return;
  2549. INIT_WORK(&ev->work, mptsas_discovery_work);
  2550. ev->ioc = ioc;
  2551. schedule_work(&ev->work);
  2552. };
  2553. /*
  2554. * mptsas_send_ir2_event - handle exposing hidden disk when
  2555. * an inactive raid volume is added
  2556. *
  2557. * @ioc: Pointer to MPT_ADAPTER structure
  2558. * @ir2_data
  2559. *
  2560. */
  2561. static void
  2562. mptsas_send_ir2_event(MPT_ADAPTER *ioc, PTR_MPI_EVENT_DATA_IR2 ir2_data)
  2563. {
  2564. struct mptsas_hotplug_event *ev;
  2565. if (ir2_data->ReasonCode !=
  2566. MPI_EVENT_IR2_RC_FOREIGN_CFG_DETECTED)
  2567. return;
  2568. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  2569. if (!ev)
  2570. return;
  2571. INIT_WORK(&ev->work, mptsas_hotplug_work);
  2572. ev->ioc = ioc;
  2573. ev->id = ir2_data->TargetID;
  2574. ev->channel = ir2_data->Bus;
  2575. ev->event_type = MPTSAS_ADD_INACTIVE_VOLUME;
  2576. schedule_work(&ev->work);
  2577. };
  2578. static int
  2579. mptsas_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *reply)
  2580. {
  2581. int rc=1;
  2582. u8 event = le32_to_cpu(reply->Event) & 0xFF;
  2583. if (!ioc->sh)
  2584. goto out;
  2585. /*
  2586. * sas_discovery_ignore_events
  2587. *
  2588. * This flag is to prevent anymore processing of
  2589. * sas events once mptsas_remove function is called.
  2590. */
  2591. if (ioc->sas_discovery_ignore_events) {
  2592. rc = mptscsih_event_process(ioc, reply);
  2593. goto out;
  2594. }
  2595. switch (event) {
  2596. case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
  2597. mptsas_send_sas_event(ioc,
  2598. (EVENT_DATA_SAS_DEVICE_STATUS_CHANGE *)reply->Data);
  2599. break;
  2600. case MPI_EVENT_INTEGRATED_RAID:
  2601. mptsas_send_raid_event(ioc,
  2602. (EVENT_DATA_RAID *)reply->Data);
  2603. break;
  2604. case MPI_EVENT_PERSISTENT_TABLE_FULL:
  2605. INIT_WORK(&ioc->sas_persist_task,
  2606. mptsas_persist_clear_table);
  2607. schedule_work(&ioc->sas_persist_task);
  2608. break;
  2609. case MPI_EVENT_SAS_DISCOVERY:
  2610. mptsas_send_discovery_event(ioc,
  2611. (EVENT_DATA_SAS_DISCOVERY *)reply->Data);
  2612. break;
  2613. case MPI_EVENT_IR2:
  2614. mptsas_send_ir2_event(ioc,
  2615. (PTR_MPI_EVENT_DATA_IR2)reply->Data);
  2616. break;
  2617. default:
  2618. rc = mptscsih_event_process(ioc, reply);
  2619. break;
  2620. }
  2621. out:
  2622. return rc;
  2623. }
  2624. static int
  2625. mptsas_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2626. {
  2627. struct Scsi_Host *sh;
  2628. MPT_SCSI_HOST *hd;
  2629. MPT_ADAPTER *ioc;
  2630. unsigned long flags;
  2631. int ii;
  2632. int numSGE = 0;
  2633. int scale;
  2634. int ioc_cap;
  2635. int error=0;
  2636. int r;
  2637. r = mpt_attach(pdev,id);
  2638. if (r)
  2639. return r;
  2640. ioc = pci_get_drvdata(pdev);
  2641. ioc->DoneCtx = mptsasDoneCtx;
  2642. ioc->TaskCtx = mptsasTaskCtx;
  2643. ioc->InternalCtx = mptsasInternalCtx;
  2644. /* Added sanity check on readiness of the MPT adapter.
  2645. */
  2646. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  2647. printk(MYIOC_s_WARN_FMT
  2648. "Skipping because it's not operational!\n",
  2649. ioc->name);
  2650. error = -ENODEV;
  2651. goto out_mptsas_probe;
  2652. }
  2653. if (!ioc->active) {
  2654. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  2655. ioc->name);
  2656. error = -ENODEV;
  2657. goto out_mptsas_probe;
  2658. }
  2659. /* Sanity check - ensure at least 1 port is INITIATOR capable
  2660. */
  2661. ioc_cap = 0;
  2662. for (ii = 0; ii < ioc->facts.NumberOfPorts; ii++) {
  2663. if (ioc->pfacts[ii].ProtocolFlags &
  2664. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  2665. ioc_cap++;
  2666. }
  2667. if (!ioc_cap) {
  2668. printk(MYIOC_s_WARN_FMT
  2669. "Skipping ioc=%p because SCSI Initiator mode "
  2670. "is NOT enabled!\n", ioc->name, ioc);
  2671. return 0;
  2672. }
  2673. sh = scsi_host_alloc(&mptsas_driver_template, sizeof(MPT_SCSI_HOST));
  2674. if (!sh) {
  2675. printk(MYIOC_s_WARN_FMT
  2676. "Unable to register controller with SCSI subsystem\n",
  2677. ioc->name);
  2678. error = -1;
  2679. goto out_mptsas_probe;
  2680. }
  2681. spin_lock_irqsave(&ioc->FreeQlock, flags);
  2682. /* Attach the SCSI Host to the IOC structure
  2683. */
  2684. ioc->sh = sh;
  2685. sh->io_port = 0;
  2686. sh->n_io_port = 0;
  2687. sh->irq = 0;
  2688. /* set 16 byte cdb's */
  2689. sh->max_cmd_len = 16;
  2690. sh->max_id = ioc->pfacts[0].PortSCSIID;
  2691. sh->max_lun = max_lun;
  2692. sh->transportt = mptsas_transport_template;
  2693. sh->this_id = ioc->pfacts[0].PortSCSIID;
  2694. /* Required entry.
  2695. */
  2696. sh->unique_id = ioc->id;
  2697. INIT_LIST_HEAD(&ioc->sas_topology);
  2698. mutex_init(&ioc->sas_topology_mutex);
  2699. mutex_init(&ioc->sas_discovery_mutex);
  2700. mutex_init(&ioc->sas_mgmt.mutex);
  2701. init_completion(&ioc->sas_mgmt.done);
  2702. /* Verify that we won't exceed the maximum
  2703. * number of chain buffers
  2704. * We can optimize: ZZ = req_sz/sizeof(SGE)
  2705. * For 32bit SGE's:
  2706. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  2707. * + (req_sz - 64)/sizeof(SGE)
  2708. * A slightly different algorithm is required for
  2709. * 64bit SGEs.
  2710. */
  2711. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  2712. if (sizeof(dma_addr_t) == sizeof(u64)) {
  2713. numSGE = (scale - 1) *
  2714. (ioc->facts.MaxChainDepth-1) + scale +
  2715. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  2716. sizeof(u32));
  2717. } else {
  2718. numSGE = 1 + (scale - 1) *
  2719. (ioc->facts.MaxChainDepth-1) + scale +
  2720. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  2721. sizeof(u32));
  2722. }
  2723. if (numSGE < sh->sg_tablesize) {
  2724. /* Reset this value */
  2725. dprintk((MYIOC_s_INFO_FMT
  2726. "Resetting sg_tablesize to %d from %d\n",
  2727. ioc->name, numSGE, sh->sg_tablesize));
  2728. sh->sg_tablesize = numSGE;
  2729. }
  2730. hd = (MPT_SCSI_HOST *) sh->hostdata;
  2731. hd->ioc = ioc;
  2732. /* SCSI needs scsi_cmnd lookup table!
  2733. * (with size equal to req_depth*PtrSz!)
  2734. */
  2735. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  2736. if (!hd->ScsiLookup) {
  2737. error = -ENOMEM;
  2738. goto out_mptsas_probe;
  2739. }
  2740. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  2741. ioc->name, hd->ScsiLookup));
  2742. /* Clear the TM flags
  2743. */
  2744. hd->tmPending = 0;
  2745. hd->tmState = TM_STATE_NONE;
  2746. hd->resetPending = 0;
  2747. hd->abortSCpnt = NULL;
  2748. /* Clear the pointer used to store
  2749. * single-threaded commands, i.e., those
  2750. * issued during a bus scan, dv and
  2751. * configuration pages.
  2752. */
  2753. hd->cmdPtr = NULL;
  2754. /* Initialize this SCSI Hosts' timers
  2755. * To use, set the timer expires field
  2756. * and add_timer
  2757. */
  2758. init_timer(&hd->timer);
  2759. hd->timer.data = (unsigned long) hd;
  2760. hd->timer.function = mptscsih_timer_expired;
  2761. ioc->sas_data.ptClear = mpt_pt_clear;
  2762. init_waitqueue_head(&hd->scandv_waitq);
  2763. hd->scandv_wait_done = 0;
  2764. hd->last_queue_full = 0;
  2765. INIT_LIST_HEAD(&hd->target_reset_list);
  2766. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  2767. if (ioc->sas_data.ptClear==1) {
  2768. mptbase_sas_persist_operation(
  2769. ioc, MPI_SAS_OP_CLEAR_ALL_PERSISTENT);
  2770. }
  2771. error = scsi_add_host(sh, &ioc->pcidev->dev);
  2772. if (error) {
  2773. dprintk((KERN_ERR MYNAM
  2774. "scsi_add_host failed\n"));
  2775. goto out_mptsas_probe;
  2776. }
  2777. mptsas_scan_sas_topology(ioc);
  2778. return 0;
  2779. out_mptsas_probe:
  2780. mptscsih_remove(pdev);
  2781. return error;
  2782. }
  2783. static void __devexit mptsas_remove(struct pci_dev *pdev)
  2784. {
  2785. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  2786. struct mptsas_portinfo *p, *n;
  2787. int i;
  2788. ioc->sas_discovery_ignore_events = 1;
  2789. sas_remove_host(ioc->sh);
  2790. mutex_lock(&ioc->sas_topology_mutex);
  2791. list_for_each_entry_safe(p, n, &ioc->sas_topology, list) {
  2792. list_del(&p->list);
  2793. for (i = 0 ; i < p->num_phys ; i++)
  2794. mptsas_port_delete(p->phy_info[i].port_details);
  2795. kfree(p->phy_info);
  2796. kfree(p);
  2797. }
  2798. mutex_unlock(&ioc->sas_topology_mutex);
  2799. mptscsih_remove(pdev);
  2800. }
  2801. static struct pci_device_id mptsas_pci_table[] = {
  2802. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064,
  2803. PCI_ANY_ID, PCI_ANY_ID },
  2804. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068,
  2805. PCI_ANY_ID, PCI_ANY_ID },
  2806. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1064E,
  2807. PCI_ANY_ID, PCI_ANY_ID },
  2808. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1068E,
  2809. PCI_ANY_ID, PCI_ANY_ID },
  2810. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_SAS1078,
  2811. PCI_ANY_ID, PCI_ANY_ID },
  2812. {0} /* Terminating entry */
  2813. };
  2814. MODULE_DEVICE_TABLE(pci, mptsas_pci_table);
  2815. static struct pci_driver mptsas_driver = {
  2816. .name = "mptsas",
  2817. .id_table = mptsas_pci_table,
  2818. .probe = mptsas_probe,
  2819. .remove = __devexit_p(mptsas_remove),
  2820. .shutdown = mptscsih_shutdown,
  2821. #ifdef CONFIG_PM
  2822. .suspend = mptscsih_suspend,
  2823. .resume = mptscsih_resume,
  2824. #endif
  2825. };
  2826. static int __init
  2827. mptsas_init(void)
  2828. {
  2829. show_mptmod_ver(my_NAME, my_VERSION);
  2830. mptsas_transport_template =
  2831. sas_attach_transport(&mptsas_transport_functions);
  2832. if (!mptsas_transport_template)
  2833. return -ENODEV;
  2834. mptsasDoneCtx = mpt_register(mptscsih_io_done, MPTSAS_DRIVER);
  2835. mptsasTaskCtx = mpt_register(mptsas_taskmgmt_complete, MPTSAS_DRIVER);
  2836. mptsasInternalCtx =
  2837. mpt_register(mptscsih_scandv_complete, MPTSAS_DRIVER);
  2838. mptsasMgmtCtx = mpt_register(mptsas_mgmt_done, MPTSAS_DRIVER);
  2839. if (mpt_event_register(mptsasDoneCtx, mptsas_event_process) == 0) {
  2840. devtverboseprintk((KERN_INFO MYNAM
  2841. ": Registered for IOC event notifications\n"));
  2842. }
  2843. if (mpt_reset_register(mptsasDoneCtx, mptsas_ioc_reset) == 0) {
  2844. dprintk((KERN_INFO MYNAM
  2845. ": Registered for IOC reset notifications\n"));
  2846. }
  2847. return pci_register_driver(&mptsas_driver);
  2848. }
  2849. static void __exit
  2850. mptsas_exit(void)
  2851. {
  2852. pci_unregister_driver(&mptsas_driver);
  2853. sas_release_transport(mptsas_transport_template);
  2854. mpt_reset_deregister(mptsasDoneCtx);
  2855. mpt_event_deregister(mptsasDoneCtx);
  2856. mpt_deregister(mptsasMgmtCtx);
  2857. mpt_deregister(mptsasInternalCtx);
  2858. mpt_deregister(mptsasTaskCtx);
  2859. mpt_deregister(mptsasDoneCtx);
  2860. }
  2861. module_init(mptsas_init);
  2862. module_exit(mptsas_exit);