mptsas.c 134 KB

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