mptsas.c 105 KB

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