mptsas.c 134 KB

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