mpt3sas_base.c 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837
  1. /*
  2. * This is the Fusion MPT base driver providing common API layer interface
  3. * for access to MPT (Message Passing Technology) firmware.
  4. *
  5. * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
  6. * Copyright (C) 2012 LSI Corporation
  7. * (mailto:DL-MPTFusionLinux@lsi.com)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  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. *
  19. * NO WARRANTY
  20. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. * solely responsible for determining the appropriateness of using and
  25. * distributing the Program and assumes all risks associated with its
  26. * exercise of rights under this Agreement, including but not limited to
  27. * the risks and costs of program errors, damage to or loss of data,
  28. * programs or equipment, and unavailability or interruption of operations.
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  40. * USA.
  41. */
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/init.h>
  46. #include <linux/slab.h>
  47. #include <linux/types.h>
  48. #include <linux/pci.h>
  49. #include <linux/kdev_t.h>
  50. #include <linux/blkdev.h>
  51. #include <linux/delay.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/dma-mapping.h>
  54. #include <linux/io.h>
  55. #include <linux/time.h>
  56. #include <linux/kthread.h>
  57. #include <linux/aer.h>
  58. #include "mpt3sas_base.h"
  59. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  60. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  61. /* maximum controller queue depth */
  62. #define MAX_HBA_QUEUE_DEPTH 30000
  63. #define MAX_CHAIN_DEPTH 100000
  64. static int max_queue_depth = -1;
  65. module_param(max_queue_depth, int, 0);
  66. MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
  67. static int max_sgl_entries = -1;
  68. module_param(max_sgl_entries, int, 0);
  69. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  70. static int msix_disable = -1;
  71. module_param(msix_disable, int, 0);
  72. MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
  73. static int mpt3sas_fwfault_debug;
  74. MODULE_PARM_DESC(mpt3sas_fwfault_debug,
  75. " enable detection of firmware fault and halt firmware - (default=0)");
  76. /**
  77. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  78. *
  79. */
  80. static int
  81. _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
  82. {
  83. int ret = param_set_int(val, kp);
  84. struct MPT3SAS_ADAPTER *ioc;
  85. if (ret)
  86. return ret;
  87. pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
  88. list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  89. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  90. return 0;
  91. }
  92. module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
  93. param_get_int, &mpt3sas_fwfault_debug, 0644);
  94. /**
  95. * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
  96. * @arg: input argument, used to derive ioc
  97. *
  98. * Return 0 if controller is removed from pci subsystem.
  99. * Return -1 for other case.
  100. */
  101. static int mpt3sas_remove_dead_ioc_func(void *arg)
  102. {
  103. struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
  104. struct pci_dev *pdev;
  105. if ((ioc == NULL))
  106. return -1;
  107. pdev = ioc->pdev;
  108. if ((pdev == NULL))
  109. return -1;
  110. pci_stop_and_remove_bus_device(pdev);
  111. return 0;
  112. }
  113. /**
  114. * _base_fault_reset_work - workq handling ioc fault conditions
  115. * @work: input argument, used to derive ioc
  116. * Context: sleep.
  117. *
  118. * Return nothing.
  119. */
  120. static void
  121. _base_fault_reset_work(struct work_struct *work)
  122. {
  123. struct MPT3SAS_ADAPTER *ioc =
  124. container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
  125. unsigned long flags;
  126. u32 doorbell;
  127. int rc;
  128. struct task_struct *p;
  129. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  130. if (ioc->shost_recovery)
  131. goto rearm_timer;
  132. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  133. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  134. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  135. pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
  136. ioc->name);
  137. /*
  138. * Call _scsih_flush_pending_cmds callback so that we flush all
  139. * pending commands back to OS. This call is required to aovid
  140. * deadlock at block layer. Dead IOC will fail to do diag reset,
  141. * and this call is safe since dead ioc will never return any
  142. * command back from HW.
  143. */
  144. ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  145. /*
  146. * Set remove_host flag early since kernel thread will
  147. * take some time to execute.
  148. */
  149. ioc->remove_host = 1;
  150. /*Remove the Dead Host */
  151. p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
  152. "mpt3sas_dead_ioc_%d", ioc->id);
  153. if (IS_ERR(p))
  154. pr_err(MPT3SAS_FMT
  155. "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
  156. ioc->name, __func__);
  157. else
  158. pr_err(MPT3SAS_FMT
  159. "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
  160. ioc->name, __func__);
  161. return; /* don't rearm timer */
  162. }
  163. if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
  164. rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  165. FORCE_BIG_HAMMER);
  166. pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
  167. __func__, (rc == 0) ? "success" : "failed");
  168. doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  169. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  170. mpt3sas_base_fault_info(ioc, doorbell &
  171. MPI2_DOORBELL_DATA_MASK);
  172. if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
  173. MPI2_IOC_STATE_OPERATIONAL)
  174. return; /* don't rearm timer */
  175. }
  176. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  177. rearm_timer:
  178. if (ioc->fault_reset_work_q)
  179. queue_delayed_work(ioc->fault_reset_work_q,
  180. &ioc->fault_reset_work,
  181. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  182. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  183. }
  184. /**
  185. * mpt3sas_base_start_watchdog - start the fault_reset_work_q
  186. * @ioc: per adapter object
  187. * Context: sleep.
  188. *
  189. * Return nothing.
  190. */
  191. void
  192. mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
  193. {
  194. unsigned long flags;
  195. if (ioc->fault_reset_work_q)
  196. return;
  197. /* initialize fault polling */
  198. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  199. snprintf(ioc->fault_reset_work_q_name,
  200. sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
  201. ioc->fault_reset_work_q =
  202. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  203. if (!ioc->fault_reset_work_q) {
  204. pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
  205. ioc->name, __func__, __LINE__);
  206. return;
  207. }
  208. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  209. if (ioc->fault_reset_work_q)
  210. queue_delayed_work(ioc->fault_reset_work_q,
  211. &ioc->fault_reset_work,
  212. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  213. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  214. }
  215. /**
  216. * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
  217. * @ioc: per adapter object
  218. * Context: sleep.
  219. *
  220. * Return nothing.
  221. */
  222. void
  223. mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
  224. {
  225. unsigned long flags;
  226. struct workqueue_struct *wq;
  227. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  228. wq = ioc->fault_reset_work_q;
  229. ioc->fault_reset_work_q = NULL;
  230. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  231. if (wq) {
  232. if (!cancel_delayed_work(&ioc->fault_reset_work))
  233. flush_workqueue(wq);
  234. destroy_workqueue(wq);
  235. }
  236. }
  237. /**
  238. * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
  239. * @ioc: per adapter object
  240. * @fault_code: fault code
  241. *
  242. * Return nothing.
  243. */
  244. void
  245. mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
  246. {
  247. pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
  248. ioc->name, fault_code);
  249. }
  250. /**
  251. * mpt3sas_halt_firmware - halt's mpt controller firmware
  252. * @ioc: per adapter object
  253. *
  254. * For debugging timeout related issues. Writing 0xCOFFEE00
  255. * to the doorbell register will halt controller firmware. With
  256. * the purpose to stop both driver and firmware, the enduser can
  257. * obtain a ring buffer from controller UART.
  258. */
  259. void
  260. mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
  261. {
  262. u32 doorbell;
  263. if (!ioc->fwfault_debug)
  264. return;
  265. dump_stack();
  266. doorbell = readl(&ioc->chip->Doorbell);
  267. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  268. mpt3sas_base_fault_info(ioc , doorbell);
  269. else {
  270. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  271. pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
  272. ioc->name);
  273. }
  274. if (ioc->fwfault_debug == 2)
  275. for (;;)
  276. ;
  277. else
  278. panic("panic in %s\n", __func__);
  279. }
  280. #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
  281. /**
  282. * _base_sas_ioc_info - verbose translation of the ioc status
  283. * @ioc: per adapter object
  284. * @mpi_reply: reply mf payload returned from firmware
  285. * @request_hdr: request mf
  286. *
  287. * Return nothing.
  288. */
  289. static void
  290. _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  291. MPI2RequestHeader_t *request_hdr)
  292. {
  293. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  294. MPI2_IOCSTATUS_MASK;
  295. char *desc = NULL;
  296. u16 frame_sz;
  297. char *func_str = NULL;
  298. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  299. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  300. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  301. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  302. return;
  303. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  304. return;
  305. switch (ioc_status) {
  306. /****************************************************************************
  307. * Common IOCStatus values for all replies
  308. ****************************************************************************/
  309. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  310. desc = "invalid function";
  311. break;
  312. case MPI2_IOCSTATUS_BUSY:
  313. desc = "busy";
  314. break;
  315. case MPI2_IOCSTATUS_INVALID_SGL:
  316. desc = "invalid sgl";
  317. break;
  318. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  319. desc = "internal error";
  320. break;
  321. case MPI2_IOCSTATUS_INVALID_VPID:
  322. desc = "invalid vpid";
  323. break;
  324. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  325. desc = "insufficient resources";
  326. break;
  327. case MPI2_IOCSTATUS_INVALID_FIELD:
  328. desc = "invalid field";
  329. break;
  330. case MPI2_IOCSTATUS_INVALID_STATE:
  331. desc = "invalid state";
  332. break;
  333. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  334. desc = "op state not supported";
  335. break;
  336. /****************************************************************************
  337. * Config IOCStatus values
  338. ****************************************************************************/
  339. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  340. desc = "config invalid action";
  341. break;
  342. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  343. desc = "config invalid type";
  344. break;
  345. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  346. desc = "config invalid page";
  347. break;
  348. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  349. desc = "config invalid data";
  350. break;
  351. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  352. desc = "config no defaults";
  353. break;
  354. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  355. desc = "config cant commit";
  356. break;
  357. /****************************************************************************
  358. * SCSI IO Reply
  359. ****************************************************************************/
  360. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  361. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  362. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  363. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  364. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  365. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  366. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  367. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  368. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  369. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  370. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  371. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  372. break;
  373. /****************************************************************************
  374. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  375. ****************************************************************************/
  376. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  377. desc = "eedp guard error";
  378. break;
  379. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  380. desc = "eedp ref tag error";
  381. break;
  382. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  383. desc = "eedp app tag error";
  384. break;
  385. /****************************************************************************
  386. * SCSI Target values
  387. ****************************************************************************/
  388. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  389. desc = "target invalid io index";
  390. break;
  391. case MPI2_IOCSTATUS_TARGET_ABORTED:
  392. desc = "target aborted";
  393. break;
  394. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  395. desc = "target no conn retryable";
  396. break;
  397. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  398. desc = "target no connection";
  399. break;
  400. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  401. desc = "target xfer count mismatch";
  402. break;
  403. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  404. desc = "target data offset error";
  405. break;
  406. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  407. desc = "target too much write data";
  408. break;
  409. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  410. desc = "target iu too short";
  411. break;
  412. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  413. desc = "target ack nak timeout";
  414. break;
  415. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  416. desc = "target nak received";
  417. break;
  418. /****************************************************************************
  419. * Serial Attached SCSI values
  420. ****************************************************************************/
  421. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  422. desc = "smp request failed";
  423. break;
  424. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  425. desc = "smp data overrun";
  426. break;
  427. /****************************************************************************
  428. * Diagnostic Buffer Post / Diagnostic Release values
  429. ****************************************************************************/
  430. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  431. desc = "diagnostic released";
  432. break;
  433. default:
  434. break;
  435. }
  436. if (!desc)
  437. return;
  438. switch (request_hdr->Function) {
  439. case MPI2_FUNCTION_CONFIG:
  440. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  441. func_str = "config_page";
  442. break;
  443. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  444. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  445. func_str = "task_mgmt";
  446. break;
  447. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  448. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  449. func_str = "sas_iounit_ctl";
  450. break;
  451. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  452. frame_sz = sizeof(Mpi2SepRequest_t);
  453. func_str = "enclosure";
  454. break;
  455. case MPI2_FUNCTION_IOC_INIT:
  456. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  457. func_str = "ioc_init";
  458. break;
  459. case MPI2_FUNCTION_PORT_ENABLE:
  460. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  461. func_str = "port_enable";
  462. break;
  463. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  464. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  465. func_str = "smp_passthru";
  466. break;
  467. default:
  468. frame_sz = 32;
  469. func_str = "unknown";
  470. break;
  471. }
  472. pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
  473. ioc->name, desc, ioc_status, request_hdr, func_str);
  474. _debug_dump_mf(request_hdr, frame_sz/4);
  475. }
  476. /**
  477. * _base_display_event_data - verbose translation of firmware asyn events
  478. * @ioc: per adapter object
  479. * @mpi_reply: reply mf payload returned from firmware
  480. *
  481. * Return nothing.
  482. */
  483. static void
  484. _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
  485. Mpi2EventNotificationReply_t *mpi_reply)
  486. {
  487. char *desc = NULL;
  488. u16 event;
  489. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  490. return;
  491. event = le16_to_cpu(mpi_reply->Event);
  492. switch (event) {
  493. case MPI2_EVENT_LOG_DATA:
  494. desc = "Log Data";
  495. break;
  496. case MPI2_EVENT_STATE_CHANGE:
  497. desc = "Status Change";
  498. break;
  499. case MPI2_EVENT_HARD_RESET_RECEIVED:
  500. desc = "Hard Reset Received";
  501. break;
  502. case MPI2_EVENT_EVENT_CHANGE:
  503. desc = "Event Change";
  504. break;
  505. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  506. desc = "Device Status Change";
  507. break;
  508. case MPI2_EVENT_IR_OPERATION_STATUS:
  509. desc = "IR Operation Status";
  510. break;
  511. case MPI2_EVENT_SAS_DISCOVERY:
  512. {
  513. Mpi2EventDataSasDiscovery_t *event_data =
  514. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  515. pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
  516. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  517. "start" : "stop");
  518. if (event_data->DiscoveryStatus)
  519. pr_info("discovery_status(0x%08x)",
  520. le32_to_cpu(event_data->DiscoveryStatus));
  521. pr_info("\n");
  522. return;
  523. }
  524. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  525. desc = "SAS Broadcast Primitive";
  526. break;
  527. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  528. desc = "SAS Init Device Status Change";
  529. break;
  530. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  531. desc = "SAS Init Table Overflow";
  532. break;
  533. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  534. desc = "SAS Topology Change List";
  535. break;
  536. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  537. desc = "SAS Enclosure Device Status Change";
  538. break;
  539. case MPI2_EVENT_IR_VOLUME:
  540. desc = "IR Volume";
  541. break;
  542. case MPI2_EVENT_IR_PHYSICAL_DISK:
  543. desc = "IR Physical Disk";
  544. break;
  545. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  546. desc = "IR Configuration Change List";
  547. break;
  548. case MPI2_EVENT_LOG_ENTRY_ADDED:
  549. desc = "Log Entry Added";
  550. break;
  551. }
  552. if (!desc)
  553. return;
  554. pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
  555. }
  556. #endif
  557. /**
  558. * _base_sas_log_info - verbose translation of firmware log info
  559. * @ioc: per adapter object
  560. * @log_info: log info
  561. *
  562. * Return nothing.
  563. */
  564. static void
  565. _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
  566. {
  567. union loginfo_type {
  568. u32 loginfo;
  569. struct {
  570. u32 subcode:16;
  571. u32 code:8;
  572. u32 originator:4;
  573. u32 bus_type:4;
  574. } dw;
  575. };
  576. union loginfo_type sas_loginfo;
  577. char *originator_str = NULL;
  578. sas_loginfo.loginfo = log_info;
  579. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  580. return;
  581. /* each nexus loss loginfo */
  582. if (log_info == 0x31170000)
  583. return;
  584. /* eat the loginfos associated with task aborts */
  585. if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
  586. 0x31140000 || log_info == 0x31130000))
  587. return;
  588. switch (sas_loginfo.dw.originator) {
  589. case 0:
  590. originator_str = "IOP";
  591. break;
  592. case 1:
  593. originator_str = "PL";
  594. break;
  595. case 2:
  596. originator_str = "IR";
  597. break;
  598. }
  599. pr_warn(MPT3SAS_FMT
  600. "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
  601. ioc->name, log_info,
  602. originator_str, sas_loginfo.dw.code,
  603. sas_loginfo.dw.subcode);
  604. }
  605. /**
  606. * _base_display_reply_info -
  607. * @ioc: per adapter object
  608. * @smid: system request message index
  609. * @msix_index: MSIX table index supplied by the OS
  610. * @reply: reply message frame(lower 32bit addr)
  611. *
  612. * Return nothing.
  613. */
  614. static void
  615. _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  616. u32 reply)
  617. {
  618. MPI2DefaultReply_t *mpi_reply;
  619. u16 ioc_status;
  620. u32 loginfo = 0;
  621. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  622. if (unlikely(!mpi_reply)) {
  623. pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
  624. ioc->name, __FILE__, __LINE__, __func__);
  625. return;
  626. }
  627. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  628. #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
  629. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  630. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  631. _base_sas_ioc_info(ioc , mpi_reply,
  632. mpt3sas_base_get_msg_frame(ioc, smid));
  633. }
  634. #endif
  635. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
  636. loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
  637. _base_sas_log_info(ioc, loginfo);
  638. }
  639. if (ioc_status || loginfo) {
  640. ioc_status &= MPI2_IOCSTATUS_MASK;
  641. mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
  642. }
  643. }
  644. /**
  645. * mpt3sas_base_done - base internal command completion routine
  646. * @ioc: per adapter object
  647. * @smid: system request message index
  648. * @msix_index: MSIX table index supplied by the OS
  649. * @reply: reply message frame(lower 32bit addr)
  650. *
  651. * Return 1 meaning mf should be freed from _base_interrupt
  652. * 0 means the mf is freed from this function.
  653. */
  654. u8
  655. mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  656. u32 reply)
  657. {
  658. MPI2DefaultReply_t *mpi_reply;
  659. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  660. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  661. return 1;
  662. if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
  663. return 1;
  664. ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
  665. if (mpi_reply) {
  666. ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
  667. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  668. }
  669. ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
  670. complete(&ioc->base_cmds.done);
  671. return 1;
  672. }
  673. /**
  674. * _base_async_event - main callback handler for firmware asyn events
  675. * @ioc: per adapter object
  676. * @msix_index: MSIX table index supplied by the OS
  677. * @reply: reply message frame(lower 32bit addr)
  678. *
  679. * Return 1 meaning mf should be freed from _base_interrupt
  680. * 0 means the mf is freed from this function.
  681. */
  682. static u8
  683. _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  684. {
  685. Mpi2EventNotificationReply_t *mpi_reply;
  686. Mpi2EventAckRequest_t *ack_request;
  687. u16 smid;
  688. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  689. if (!mpi_reply)
  690. return 1;
  691. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  692. return 1;
  693. #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
  694. _base_display_event_data(ioc, mpi_reply);
  695. #endif
  696. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  697. goto out;
  698. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  699. if (!smid) {
  700. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  701. ioc->name, __func__);
  702. goto out;
  703. }
  704. ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
  705. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  706. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  707. ack_request->Event = mpi_reply->Event;
  708. ack_request->EventContext = mpi_reply->EventContext;
  709. ack_request->VF_ID = 0; /* TODO */
  710. ack_request->VP_ID = 0;
  711. mpt3sas_base_put_smid_default(ioc, smid);
  712. out:
  713. /* scsih callback handler */
  714. mpt3sas_scsih_event_callback(ioc, msix_index, reply);
  715. /* ctl callback handler */
  716. mpt3sas_ctl_event_callback(ioc, msix_index, reply);
  717. return 1;
  718. }
  719. /**
  720. * _base_get_cb_idx - obtain the callback index
  721. * @ioc: per adapter object
  722. * @smid: system request message index
  723. *
  724. * Return callback index.
  725. */
  726. static u8
  727. _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  728. {
  729. int i;
  730. u8 cb_idx;
  731. if (smid < ioc->hi_priority_smid) {
  732. i = smid - 1;
  733. cb_idx = ioc->scsi_lookup[i].cb_idx;
  734. } else if (smid < ioc->internal_smid) {
  735. i = smid - ioc->hi_priority_smid;
  736. cb_idx = ioc->hpr_lookup[i].cb_idx;
  737. } else if (smid <= ioc->hba_queue_depth) {
  738. i = smid - ioc->internal_smid;
  739. cb_idx = ioc->internal_lookup[i].cb_idx;
  740. } else
  741. cb_idx = 0xFF;
  742. return cb_idx;
  743. }
  744. /**
  745. * _base_mask_interrupts - disable interrupts
  746. * @ioc: per adapter object
  747. *
  748. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  749. *
  750. * Return nothing.
  751. */
  752. static void
  753. _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  754. {
  755. u32 him_register;
  756. ioc->mask_interrupts = 1;
  757. him_register = readl(&ioc->chip->HostInterruptMask);
  758. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  759. writel(him_register, &ioc->chip->HostInterruptMask);
  760. readl(&ioc->chip->HostInterruptMask);
  761. }
  762. /**
  763. * _base_unmask_interrupts - enable interrupts
  764. * @ioc: per adapter object
  765. *
  766. * Enabling only Reply Interrupts
  767. *
  768. * Return nothing.
  769. */
  770. static void
  771. _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
  772. {
  773. u32 him_register;
  774. him_register = readl(&ioc->chip->HostInterruptMask);
  775. him_register &= ~MPI2_HIM_RIM;
  776. writel(him_register, &ioc->chip->HostInterruptMask);
  777. ioc->mask_interrupts = 0;
  778. }
  779. union reply_descriptor {
  780. u64 word;
  781. struct {
  782. u32 low;
  783. u32 high;
  784. } u;
  785. };
  786. /**
  787. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  788. * @irq: irq number (not used)
  789. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  790. * @r: pt_regs pointer (not used)
  791. *
  792. * Return IRQ_HANDLE if processed, else IRQ_NONE.
  793. */
  794. static irqreturn_t
  795. _base_interrupt(int irq, void *bus_id)
  796. {
  797. struct adapter_reply_queue *reply_q = bus_id;
  798. union reply_descriptor rd;
  799. u32 completed_cmds;
  800. u8 request_desript_type;
  801. u16 smid;
  802. u8 cb_idx;
  803. u32 reply;
  804. u8 msix_index = reply_q->msix_index;
  805. struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
  806. Mpi2ReplyDescriptorsUnion_t *rpf;
  807. u8 rc;
  808. if (ioc->mask_interrupts)
  809. return IRQ_NONE;
  810. if (!atomic_add_unless(&reply_q->busy, 1, 1))
  811. return IRQ_NONE;
  812. rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
  813. request_desript_type = rpf->Default.ReplyFlags
  814. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  815. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
  816. atomic_dec(&reply_q->busy);
  817. return IRQ_NONE;
  818. }
  819. completed_cmds = 0;
  820. cb_idx = 0xFF;
  821. do {
  822. rd.word = le64_to_cpu(rpf->Words);
  823. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  824. goto out;
  825. reply = 0;
  826. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  827. if (request_desript_type ==
  828. MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
  829. request_desript_type ==
  830. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
  831. cb_idx = _base_get_cb_idx(ioc, smid);
  832. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  833. (likely(mpt_callbacks[cb_idx] != NULL))) {
  834. rc = mpt_callbacks[cb_idx](ioc, smid,
  835. msix_index, 0);
  836. if (rc)
  837. mpt3sas_base_free_smid(ioc, smid);
  838. }
  839. } else if (request_desript_type ==
  840. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  841. reply = le32_to_cpu(
  842. rpf->AddressReply.ReplyFrameAddress);
  843. if (reply > ioc->reply_dma_max_address ||
  844. reply < ioc->reply_dma_min_address)
  845. reply = 0;
  846. if (smid) {
  847. cb_idx = _base_get_cb_idx(ioc, smid);
  848. if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
  849. (likely(mpt_callbacks[cb_idx] != NULL))) {
  850. rc = mpt_callbacks[cb_idx](ioc, smid,
  851. msix_index, reply);
  852. if (reply)
  853. _base_display_reply_info(ioc,
  854. smid, msix_index, reply);
  855. if (rc)
  856. mpt3sas_base_free_smid(ioc,
  857. smid);
  858. }
  859. } else {
  860. _base_async_event(ioc, msix_index, reply);
  861. }
  862. /* reply free queue handling */
  863. if (reply) {
  864. ioc->reply_free_host_index =
  865. (ioc->reply_free_host_index ==
  866. (ioc->reply_free_queue_depth - 1)) ?
  867. 0 : ioc->reply_free_host_index + 1;
  868. ioc->reply_free[ioc->reply_free_host_index] =
  869. cpu_to_le32(reply);
  870. wmb();
  871. writel(ioc->reply_free_host_index,
  872. &ioc->chip->ReplyFreeHostIndex);
  873. }
  874. }
  875. rpf->Words = cpu_to_le64(ULLONG_MAX);
  876. reply_q->reply_post_host_index =
  877. (reply_q->reply_post_host_index ==
  878. (ioc->reply_post_queue_depth - 1)) ? 0 :
  879. reply_q->reply_post_host_index + 1;
  880. request_desript_type =
  881. reply_q->reply_post_free[reply_q->reply_post_host_index].
  882. Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  883. completed_cmds++;
  884. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  885. goto out;
  886. if (!reply_q->reply_post_host_index)
  887. rpf = reply_q->reply_post_free;
  888. else
  889. rpf++;
  890. } while (1);
  891. out:
  892. if (!completed_cmds) {
  893. atomic_dec(&reply_q->busy);
  894. return IRQ_NONE;
  895. }
  896. wmb();
  897. writel(reply_q->reply_post_host_index | (msix_index <<
  898. MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
  899. atomic_dec(&reply_q->busy);
  900. return IRQ_HANDLED;
  901. }
  902. /**
  903. * _base_is_controller_msix_enabled - is controller support muli-reply queues
  904. * @ioc: per adapter object
  905. *
  906. */
  907. static inline int
  908. _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
  909. {
  910. return (ioc->facts.IOCCapabilities &
  911. MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
  912. }
  913. /**
  914. * mpt3sas_base_flush_reply_queues - flushing the MSIX reply queues
  915. * @ioc: per adapter object
  916. * Context: ISR conext
  917. *
  918. * Called when a Task Management request has completed. We want
  919. * to flush the other reply queues so all the outstanding IO has been
  920. * completed back to OS before we process the TM completetion.
  921. *
  922. * Return nothing.
  923. */
  924. void
  925. mpt3sas_base_flush_reply_queues(struct MPT3SAS_ADAPTER *ioc)
  926. {
  927. struct adapter_reply_queue *reply_q;
  928. /* If MSIX capability is turned off
  929. * then multi-queues are not enabled
  930. */
  931. if (!_base_is_controller_msix_enabled(ioc))
  932. return;
  933. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  934. if (ioc->shost_recovery)
  935. return;
  936. /* TMs are on msix_index == 0 */
  937. if (reply_q->msix_index == 0)
  938. continue;
  939. _base_interrupt(reply_q->vector, (void *)reply_q);
  940. }
  941. }
  942. /**
  943. * mpt3sas_base_release_callback_handler - clear interrupt callback handler
  944. * @cb_idx: callback index
  945. *
  946. * Return nothing.
  947. */
  948. void
  949. mpt3sas_base_release_callback_handler(u8 cb_idx)
  950. {
  951. mpt_callbacks[cb_idx] = NULL;
  952. }
  953. /**
  954. * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
  955. * @cb_func: callback function
  956. *
  957. * Returns cb_func.
  958. */
  959. u8
  960. mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  961. {
  962. u8 cb_idx;
  963. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  964. if (mpt_callbacks[cb_idx] == NULL)
  965. break;
  966. mpt_callbacks[cb_idx] = cb_func;
  967. return cb_idx;
  968. }
  969. /**
  970. * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
  971. *
  972. * Return nothing.
  973. */
  974. void
  975. mpt3sas_base_initialize_callback_handler(void)
  976. {
  977. u8 cb_idx;
  978. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  979. mpt3sas_base_release_callback_handler(cb_idx);
  980. }
  981. /**
  982. * _base_build_zero_len_sge - build zero length sg entry
  983. * @ioc: per adapter object
  984. * @paddr: virtual address for SGE
  985. *
  986. * Create a zero length scatter gather entry to insure the IOCs hardware has
  987. * something to use if the target device goes brain dead and tries
  988. * to send data even when none is asked for.
  989. *
  990. * Return nothing.
  991. */
  992. static void
  993. _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  994. {
  995. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  996. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  997. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  998. MPI2_SGE_FLAGS_SHIFT);
  999. ioc->base_add_sg_single(paddr, flags_length, -1);
  1000. }
  1001. /**
  1002. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  1003. * @paddr: virtual address for SGE
  1004. * @flags_length: SGE flags and data transfer length
  1005. * @dma_addr: Physical address
  1006. *
  1007. * Return nothing.
  1008. */
  1009. static void
  1010. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1011. {
  1012. Mpi2SGESimple32_t *sgel = paddr;
  1013. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  1014. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1015. sgel->FlagsLength = cpu_to_le32(flags_length);
  1016. sgel->Address = cpu_to_le32(dma_addr);
  1017. }
  1018. /**
  1019. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  1020. * @paddr: virtual address for SGE
  1021. * @flags_length: SGE flags and data transfer length
  1022. * @dma_addr: Physical address
  1023. *
  1024. * Return nothing.
  1025. */
  1026. static void
  1027. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  1028. {
  1029. Mpi2SGESimple64_t *sgel = paddr;
  1030. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  1031. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  1032. sgel->FlagsLength = cpu_to_le32(flags_length);
  1033. sgel->Address = cpu_to_le64(dma_addr);
  1034. }
  1035. /**
  1036. * _base_get_chain_buffer_tracker - obtain chain tracker
  1037. * @ioc: per adapter object
  1038. * @smid: smid associated to an IO request
  1039. *
  1040. * Returns chain tracker(from ioc->free_chain_list)
  1041. */
  1042. static struct chain_tracker *
  1043. _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1044. {
  1045. struct chain_tracker *chain_req;
  1046. unsigned long flags;
  1047. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1048. if (list_empty(&ioc->free_chain_list)) {
  1049. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1050. dfailprintk(ioc, pr_warn(MPT3SAS_FMT
  1051. "chain buffers not available\n", ioc->name));
  1052. return NULL;
  1053. }
  1054. chain_req = list_entry(ioc->free_chain_list.next,
  1055. struct chain_tracker, tracker_list);
  1056. list_del_init(&chain_req->tracker_list);
  1057. list_add_tail(&chain_req->tracker_list,
  1058. &ioc->scsi_lookup[smid - 1].chain_list);
  1059. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1060. return chain_req;
  1061. }
  1062. /**
  1063. * _base_build_sg - build generic sg
  1064. * @ioc: per adapter object
  1065. * @psge: virtual address for SGE
  1066. * @data_out_dma: physical address for WRITES
  1067. * @data_out_sz: data xfer size for WRITES
  1068. * @data_in_dma: physical address for READS
  1069. * @data_in_sz: data xfer size for READS
  1070. *
  1071. * Return nothing.
  1072. */
  1073. static void
  1074. _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1075. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1076. size_t data_in_sz)
  1077. {
  1078. u32 sgl_flags;
  1079. if (!data_out_sz && !data_in_sz) {
  1080. _base_build_zero_len_sge(ioc, psge);
  1081. return;
  1082. }
  1083. if (data_out_sz && data_in_sz) {
  1084. /* WRITE sgel first */
  1085. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1086. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1087. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1088. ioc->base_add_sg_single(psge, sgl_flags |
  1089. data_out_sz, data_out_dma);
  1090. /* incr sgel */
  1091. psge += ioc->sge_size;
  1092. /* READ sgel last */
  1093. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1094. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1095. MPI2_SGE_FLAGS_END_OF_LIST);
  1096. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1097. ioc->base_add_sg_single(psge, sgl_flags |
  1098. data_in_sz, data_in_dma);
  1099. } else if (data_out_sz) /* WRITE */ {
  1100. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1101. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1102. MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1103. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1104. ioc->base_add_sg_single(psge, sgl_flags |
  1105. data_out_sz, data_out_dma);
  1106. } else if (data_in_sz) /* READ */ {
  1107. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1108. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1109. MPI2_SGE_FLAGS_END_OF_LIST);
  1110. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1111. ioc->base_add_sg_single(psge, sgl_flags |
  1112. data_in_sz, data_in_dma);
  1113. }
  1114. }
  1115. /* IEEE format sgls */
  1116. /**
  1117. * _base_add_sg_single_ieee - add sg element for IEEE format
  1118. * @paddr: virtual address for SGE
  1119. * @flags: SGE flags
  1120. * @chain_offset: number of 128 byte elements from start of segment
  1121. * @length: data transfer length
  1122. * @dma_addr: Physical address
  1123. *
  1124. * Return nothing.
  1125. */
  1126. static void
  1127. _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
  1128. dma_addr_t dma_addr)
  1129. {
  1130. Mpi25IeeeSgeChain64_t *sgel = paddr;
  1131. sgel->Flags = flags;
  1132. sgel->NextChainOffset = chain_offset;
  1133. sgel->Length = cpu_to_le32(length);
  1134. sgel->Address = cpu_to_le64(dma_addr);
  1135. }
  1136. /**
  1137. * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
  1138. * @ioc: per adapter object
  1139. * @paddr: virtual address for SGE
  1140. *
  1141. * Create a zero length scatter gather entry to insure the IOCs hardware has
  1142. * something to use if the target device goes brain dead and tries
  1143. * to send data even when none is asked for.
  1144. *
  1145. * Return nothing.
  1146. */
  1147. static void
  1148. _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
  1149. {
  1150. u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1151. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
  1152. MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
  1153. _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
  1154. }
  1155. /**
  1156. * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
  1157. * @ioc: per adapter object
  1158. * @scmd: scsi command
  1159. * @smid: system request message index
  1160. * Context: none.
  1161. *
  1162. * The main routine that builds scatter gather table from a given
  1163. * scsi request sent via the .queuecommand main handler.
  1164. *
  1165. * Returns 0 success, anything else error
  1166. */
  1167. static int
  1168. _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
  1169. struct scsi_cmnd *scmd, u16 smid)
  1170. {
  1171. Mpi2SCSIIORequest_t *mpi_request;
  1172. dma_addr_t chain_dma;
  1173. struct scatterlist *sg_scmd;
  1174. void *sg_local, *chain;
  1175. u32 chain_offset;
  1176. u32 chain_length;
  1177. int sges_left;
  1178. u32 sges_in_segment;
  1179. u8 simple_sgl_flags;
  1180. u8 simple_sgl_flags_last;
  1181. u8 chain_sgl_flags;
  1182. struct chain_tracker *chain_req;
  1183. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  1184. /* init scatter gather flags */
  1185. simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1186. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1187. simple_sgl_flags_last = simple_sgl_flags |
  1188. MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  1189. chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1190. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1191. sg_scmd = scsi_sglist(scmd);
  1192. sges_left = scsi_dma_map(scmd);
  1193. if (!sges_left) {
  1194. sdev_printk(KERN_ERR, scmd->device,
  1195. "pci_map_sg failed: request for %d bytes!\n",
  1196. scsi_bufflen(scmd));
  1197. return -ENOMEM;
  1198. }
  1199. sg_local = &mpi_request->SGL;
  1200. sges_in_segment = (ioc->request_sz -
  1201. offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
  1202. if (sges_left <= sges_in_segment)
  1203. goto fill_in_last_segment;
  1204. mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
  1205. (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
  1206. /* fill in main message segment when there is a chain following */
  1207. while (sges_in_segment > 1) {
  1208. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1209. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1210. sg_scmd = sg_next(sg_scmd);
  1211. sg_local += ioc->sge_size_ieee;
  1212. sges_left--;
  1213. sges_in_segment--;
  1214. }
  1215. /* initializing the pointers */
  1216. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1217. if (!chain_req)
  1218. return -1;
  1219. chain = chain_req->chain_buffer;
  1220. chain_dma = chain_req->chain_buffer_dma;
  1221. do {
  1222. sges_in_segment = (sges_left <=
  1223. ioc->max_sges_in_chain_message) ? sges_left :
  1224. ioc->max_sges_in_chain_message;
  1225. chain_offset = (sges_left == sges_in_segment) ?
  1226. 0 : sges_in_segment;
  1227. chain_length = sges_in_segment * ioc->sge_size_ieee;
  1228. if (chain_offset)
  1229. chain_length += ioc->sge_size_ieee;
  1230. _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
  1231. chain_offset, chain_length, chain_dma);
  1232. sg_local = chain;
  1233. if (!chain_offset)
  1234. goto fill_in_last_segment;
  1235. /* fill in chain segments */
  1236. while (sges_in_segment) {
  1237. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1238. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1239. sg_scmd = sg_next(sg_scmd);
  1240. sg_local += ioc->sge_size_ieee;
  1241. sges_left--;
  1242. sges_in_segment--;
  1243. }
  1244. chain_req = _base_get_chain_buffer_tracker(ioc, smid);
  1245. if (!chain_req)
  1246. return -1;
  1247. chain = chain_req->chain_buffer;
  1248. chain_dma = chain_req->chain_buffer_dma;
  1249. } while (1);
  1250. fill_in_last_segment:
  1251. /* fill the last segment */
  1252. while (sges_left) {
  1253. if (sges_left == 1)
  1254. _base_add_sg_single_ieee(sg_local,
  1255. simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
  1256. sg_dma_address(sg_scmd));
  1257. else
  1258. _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
  1259. sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
  1260. sg_scmd = sg_next(sg_scmd);
  1261. sg_local += ioc->sge_size_ieee;
  1262. sges_left--;
  1263. }
  1264. return 0;
  1265. }
  1266. /**
  1267. * _base_build_sg_ieee - build generic sg for IEEE format
  1268. * @ioc: per adapter object
  1269. * @psge: virtual address for SGE
  1270. * @data_out_dma: physical address for WRITES
  1271. * @data_out_sz: data xfer size for WRITES
  1272. * @data_in_dma: physical address for READS
  1273. * @data_in_sz: data xfer size for READS
  1274. *
  1275. * Return nothing.
  1276. */
  1277. static void
  1278. _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
  1279. dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
  1280. size_t data_in_sz)
  1281. {
  1282. u8 sgl_flags;
  1283. if (!data_out_sz && !data_in_sz) {
  1284. _base_build_zero_len_sge_ieee(ioc, psge);
  1285. return;
  1286. }
  1287. if (data_out_sz && data_in_sz) {
  1288. /* WRITE sgel first */
  1289. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1290. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1291. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  1292. data_out_dma);
  1293. /* incr sgel */
  1294. psge += ioc->sge_size_ieee;
  1295. /* READ sgel last */
  1296. sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
  1297. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  1298. data_in_dma);
  1299. } else if (data_out_sz) /* WRITE */ {
  1300. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1301. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  1302. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1303. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
  1304. data_out_dma);
  1305. } else if (data_in_sz) /* READ */ {
  1306. sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
  1307. MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
  1308. MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
  1309. _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
  1310. data_in_dma);
  1311. }
  1312. }
  1313. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  1314. /**
  1315. * _base_config_dma_addressing - set dma addressing
  1316. * @ioc: per adapter object
  1317. * @pdev: PCI device struct
  1318. *
  1319. * Returns 0 for success, non-zero for failure.
  1320. */
  1321. static int
  1322. _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
  1323. {
  1324. struct sysinfo s;
  1325. char *desc = NULL;
  1326. if (sizeof(dma_addr_t) > 4) {
  1327. const uint64_t required_mask =
  1328. dma_get_required_mask(&pdev->dev);
  1329. if ((required_mask > DMA_BIT_MASK(32)) &&
  1330. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
  1331. !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
  1332. ioc->base_add_sg_single = &_base_add_sg_single_64;
  1333. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  1334. desc = "64";
  1335. goto out;
  1336. }
  1337. }
  1338. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  1339. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1340. ioc->base_add_sg_single = &_base_add_sg_single_32;
  1341. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  1342. desc = "32";
  1343. } else
  1344. return -ENODEV;
  1345. out:
  1346. si_meminfo(&s);
  1347. pr_info(MPT3SAS_FMT
  1348. "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
  1349. ioc->name, desc, convert_to_kb(s.totalram));
  1350. return 0;
  1351. }
  1352. /**
  1353. * _base_check_enable_msix - checks MSIX capabable.
  1354. * @ioc: per adapter object
  1355. *
  1356. * Check to see if card is capable of MSIX, and set number
  1357. * of available msix vectors
  1358. */
  1359. static int
  1360. _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  1361. {
  1362. int base;
  1363. u16 message_control;
  1364. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  1365. if (!base) {
  1366. dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
  1367. ioc->name));
  1368. return -EINVAL;
  1369. }
  1370. /* get msix vector count */
  1371. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  1372. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  1373. if (ioc->msix_vector_count > 8)
  1374. ioc->msix_vector_count = 8;
  1375. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  1376. "msix is supported, vector_count(%d)\n",
  1377. ioc->name, ioc->msix_vector_count));
  1378. return 0;
  1379. }
  1380. /**
  1381. * _base_free_irq - free irq
  1382. * @ioc: per adapter object
  1383. *
  1384. * Freeing respective reply_queue from the list.
  1385. */
  1386. static void
  1387. _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
  1388. {
  1389. struct adapter_reply_queue *reply_q, *next;
  1390. if (list_empty(&ioc->reply_queue_list))
  1391. return;
  1392. list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  1393. list_del(&reply_q->list);
  1394. synchronize_irq(reply_q->vector);
  1395. free_irq(reply_q->vector, reply_q);
  1396. kfree(reply_q);
  1397. }
  1398. }
  1399. /**
  1400. * _base_request_irq - request irq
  1401. * @ioc: per adapter object
  1402. * @index: msix index into vector table
  1403. * @vector: irq vector
  1404. *
  1405. * Inserting respective reply_queue into the list.
  1406. */
  1407. static int
  1408. _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
  1409. {
  1410. struct adapter_reply_queue *reply_q;
  1411. int r;
  1412. reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
  1413. if (!reply_q) {
  1414. pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
  1415. ioc->name, (int)sizeof(struct adapter_reply_queue));
  1416. return -ENOMEM;
  1417. }
  1418. reply_q->ioc = ioc;
  1419. reply_q->msix_index = index;
  1420. reply_q->vector = vector;
  1421. atomic_set(&reply_q->busy, 0);
  1422. if (ioc->msix_enable)
  1423. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
  1424. MPT3SAS_DRIVER_NAME, ioc->id, index);
  1425. else
  1426. snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
  1427. MPT3SAS_DRIVER_NAME, ioc->id);
  1428. r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
  1429. reply_q);
  1430. if (r) {
  1431. pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
  1432. reply_q->name, vector);
  1433. kfree(reply_q);
  1434. return -EBUSY;
  1435. }
  1436. INIT_LIST_HEAD(&reply_q->list);
  1437. list_add_tail(&reply_q->list, &ioc->reply_queue_list);
  1438. return 0;
  1439. }
  1440. /**
  1441. * _base_assign_reply_queues - assigning msix index for each cpu
  1442. * @ioc: per adapter object
  1443. *
  1444. * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
  1445. *
  1446. * It would nice if we could call irq_set_affinity, however it is not
  1447. * an exported symbol
  1448. */
  1449. static void
  1450. _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
  1451. {
  1452. struct adapter_reply_queue *reply_q;
  1453. int cpu_id;
  1454. int cpu_grouping, loop, grouping, grouping_mod;
  1455. int reply_queue;
  1456. if (!_base_is_controller_msix_enabled(ioc))
  1457. return;
  1458. memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
  1459. /* NUMA Hardware bug workaround - drop to less reply queues */
  1460. if (ioc->reply_queue_count > ioc->facts.MaxMSIxVectors) {
  1461. ioc->reply_queue_count = ioc->facts.MaxMSIxVectors;
  1462. reply_queue = 0;
  1463. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  1464. reply_q->msix_index = reply_queue;
  1465. if (++reply_queue == ioc->reply_queue_count)
  1466. reply_queue = 0;
  1467. }
  1468. }
  1469. /* when there are more cpus than available msix vectors,
  1470. * then group cpus togeather on same irq
  1471. */
  1472. if (ioc->cpu_count > ioc->msix_vector_count) {
  1473. grouping = ioc->cpu_count / ioc->msix_vector_count;
  1474. grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
  1475. if (grouping < 2 || (grouping == 2 && !grouping_mod))
  1476. cpu_grouping = 2;
  1477. else if (grouping < 4 || (grouping == 4 && !grouping_mod))
  1478. cpu_grouping = 4;
  1479. else if (grouping < 8 || (grouping == 8 && !grouping_mod))
  1480. cpu_grouping = 8;
  1481. else
  1482. cpu_grouping = 16;
  1483. } else
  1484. cpu_grouping = 0;
  1485. loop = 0;
  1486. reply_q = list_entry(ioc->reply_queue_list.next,
  1487. struct adapter_reply_queue, list);
  1488. for_each_online_cpu(cpu_id) {
  1489. if (!cpu_grouping) {
  1490. ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
  1491. reply_q = list_entry(reply_q->list.next,
  1492. struct adapter_reply_queue, list);
  1493. } else {
  1494. if (loop < cpu_grouping) {
  1495. ioc->cpu_msix_table[cpu_id] =
  1496. reply_q->msix_index;
  1497. loop++;
  1498. } else {
  1499. reply_q = list_entry(reply_q->list.next,
  1500. struct adapter_reply_queue, list);
  1501. ioc->cpu_msix_table[cpu_id] =
  1502. reply_q->msix_index;
  1503. loop = 1;
  1504. }
  1505. }
  1506. }
  1507. }
  1508. /**
  1509. * _base_disable_msix - disables msix
  1510. * @ioc: per adapter object
  1511. *
  1512. */
  1513. static void
  1514. _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
  1515. {
  1516. if (!ioc->msix_enable)
  1517. return;
  1518. pci_disable_msix(ioc->pdev);
  1519. ioc->msix_enable = 0;
  1520. }
  1521. /**
  1522. * _base_enable_msix - enables msix, failback to io_apic
  1523. * @ioc: per adapter object
  1524. *
  1525. */
  1526. static int
  1527. _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
  1528. {
  1529. struct msix_entry *entries, *a;
  1530. int r;
  1531. int i;
  1532. u8 try_msix = 0;
  1533. INIT_LIST_HEAD(&ioc->reply_queue_list);
  1534. if (msix_disable == -1 || msix_disable == 0)
  1535. try_msix = 1;
  1536. if (!try_msix)
  1537. goto try_ioapic;
  1538. if (_base_check_enable_msix(ioc) != 0)
  1539. goto try_ioapic;
  1540. ioc->reply_queue_count = min_t(int, ioc->cpu_count,
  1541. ioc->msix_vector_count);
  1542. entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
  1543. GFP_KERNEL);
  1544. if (!entries) {
  1545. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  1546. "kcalloc failed @ at %s:%d/%s() !!!\n",
  1547. ioc->name, __FILE__, __LINE__, __func__));
  1548. goto try_ioapic;
  1549. }
  1550. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
  1551. a->entry = i;
  1552. r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
  1553. if (r) {
  1554. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  1555. "pci_enable_msix failed (r=%d) !!!\n",
  1556. ioc->name, r));
  1557. kfree(entries);
  1558. goto try_ioapic;
  1559. }
  1560. ioc->msix_enable = 1;
  1561. for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
  1562. r = _base_request_irq(ioc, i, a->vector);
  1563. if (r) {
  1564. _base_free_irq(ioc);
  1565. _base_disable_msix(ioc);
  1566. kfree(entries);
  1567. goto try_ioapic;
  1568. }
  1569. }
  1570. kfree(entries);
  1571. return 0;
  1572. /* failback to io_apic interrupt routing */
  1573. try_ioapic:
  1574. r = _base_request_irq(ioc, 0, ioc->pdev->irq);
  1575. return r;
  1576. }
  1577. /**
  1578. * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
  1579. * @ioc: per adapter object
  1580. *
  1581. * Returns 0 for success, non-zero for failure.
  1582. */
  1583. int
  1584. mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
  1585. {
  1586. struct pci_dev *pdev = ioc->pdev;
  1587. u32 memap_sz;
  1588. u32 pio_sz;
  1589. int i, r = 0;
  1590. u64 pio_chip = 0;
  1591. u64 chip_phys = 0;
  1592. struct adapter_reply_queue *reply_q;
  1593. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
  1594. ioc->name, __func__));
  1595. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1596. if (pci_enable_device_mem(pdev)) {
  1597. pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
  1598. ioc->name);
  1599. return -ENODEV;
  1600. }
  1601. if (pci_request_selected_regions(pdev, ioc->bars,
  1602. MPT3SAS_DRIVER_NAME)) {
  1603. pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
  1604. ioc->name);
  1605. r = -ENODEV;
  1606. goto out_fail;
  1607. }
  1608. /* AER (Advanced Error Reporting) hooks */
  1609. pci_enable_pcie_error_reporting(pdev);
  1610. pci_set_master(pdev);
  1611. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  1612. pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
  1613. ioc->name, pci_name(pdev));
  1614. r = -ENODEV;
  1615. goto out_fail;
  1616. }
  1617. for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
  1618. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  1619. if (pio_sz)
  1620. continue;
  1621. pio_chip = (u64)pci_resource_start(pdev, i);
  1622. pio_sz = pci_resource_len(pdev, i);
  1623. } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  1624. if (memap_sz)
  1625. continue;
  1626. ioc->chip_phys = pci_resource_start(pdev, i);
  1627. chip_phys = (u64)ioc->chip_phys;
  1628. memap_sz = pci_resource_len(pdev, i);
  1629. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  1630. if (ioc->chip == NULL) {
  1631. pr_err(MPT3SAS_FMT "unable to map adapter memory!\n",
  1632. ioc->name);
  1633. r = -EINVAL;
  1634. goto out_fail;
  1635. }
  1636. }
  1637. }
  1638. _base_mask_interrupts(ioc);
  1639. r = _base_enable_msix(ioc);
  1640. if (r)
  1641. goto out_fail;
  1642. list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
  1643. pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
  1644. reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  1645. "IO-APIC enabled"), reply_q->vector);
  1646. pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
  1647. ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
  1648. pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
  1649. ioc->name, (unsigned long long)pio_chip, pio_sz);
  1650. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  1651. pci_save_state(pdev);
  1652. return 0;
  1653. out_fail:
  1654. if (ioc->chip_phys)
  1655. iounmap(ioc->chip);
  1656. ioc->chip_phys = 0;
  1657. pci_release_selected_regions(ioc->pdev, ioc->bars);
  1658. pci_disable_pcie_error_reporting(pdev);
  1659. pci_disable_device(pdev);
  1660. return r;
  1661. }
  1662. /**
  1663. * mpt3sas_base_get_msg_frame - obtain request mf pointer
  1664. * @ioc: per adapter object
  1665. * @smid: system request message index(smid zero is invalid)
  1666. *
  1667. * Returns virt pointer to message frame.
  1668. */
  1669. void *
  1670. mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1671. {
  1672. return (void *)(ioc->request + (smid * ioc->request_sz));
  1673. }
  1674. /**
  1675. * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
  1676. * @ioc: per adapter object
  1677. * @smid: system request message index
  1678. *
  1679. * Returns virt pointer to sense buffer.
  1680. */
  1681. void *
  1682. mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1683. {
  1684. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1685. }
  1686. /**
  1687. * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
  1688. * @ioc: per adapter object
  1689. * @smid: system request message index
  1690. *
  1691. * Returns phys pointer to the low 32bit address of the sense buffer.
  1692. */
  1693. __le32
  1694. mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1695. {
  1696. return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
  1697. SCSI_SENSE_BUFFERSIZE));
  1698. }
  1699. /**
  1700. * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
  1701. * @ioc: per adapter object
  1702. * @phys_addr: lower 32 physical addr of the reply
  1703. *
  1704. * Converts 32bit lower physical addr into a virt address.
  1705. */
  1706. void *
  1707. mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
  1708. {
  1709. if (!phys_addr)
  1710. return NULL;
  1711. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  1712. }
  1713. /**
  1714. * mpt3sas_base_get_smid - obtain a free smid from internal queue
  1715. * @ioc: per adapter object
  1716. * @cb_idx: callback index
  1717. *
  1718. * Returns smid (zero is invalid)
  1719. */
  1720. u16
  1721. mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  1722. {
  1723. unsigned long flags;
  1724. struct request_tracker *request;
  1725. u16 smid;
  1726. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1727. if (list_empty(&ioc->internal_free_list)) {
  1728. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1729. pr_err(MPT3SAS_FMT "%s: smid not available\n",
  1730. ioc->name, __func__);
  1731. return 0;
  1732. }
  1733. request = list_entry(ioc->internal_free_list.next,
  1734. struct request_tracker, tracker_list);
  1735. request->cb_idx = cb_idx;
  1736. smid = request->smid;
  1737. list_del(&request->tracker_list);
  1738. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1739. return smid;
  1740. }
  1741. /**
  1742. * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  1743. * @ioc: per adapter object
  1744. * @cb_idx: callback index
  1745. * @scmd: pointer to scsi command object
  1746. *
  1747. * Returns smid (zero is invalid)
  1748. */
  1749. u16
  1750. mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
  1751. struct scsi_cmnd *scmd)
  1752. {
  1753. unsigned long flags;
  1754. struct scsiio_tracker *request;
  1755. u16 smid;
  1756. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1757. if (list_empty(&ioc->free_list)) {
  1758. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1759. pr_err(MPT3SAS_FMT "%s: smid not available\n",
  1760. ioc->name, __func__);
  1761. return 0;
  1762. }
  1763. request = list_entry(ioc->free_list.next,
  1764. struct scsiio_tracker, tracker_list);
  1765. request->scmd = scmd;
  1766. request->cb_idx = cb_idx;
  1767. smid = request->smid;
  1768. list_del(&request->tracker_list);
  1769. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1770. return smid;
  1771. }
  1772. /**
  1773. * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  1774. * @ioc: per adapter object
  1775. * @cb_idx: callback index
  1776. *
  1777. * Returns smid (zero is invalid)
  1778. */
  1779. u16
  1780. mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
  1781. {
  1782. unsigned long flags;
  1783. struct request_tracker *request;
  1784. u16 smid;
  1785. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1786. if (list_empty(&ioc->hpr_free_list)) {
  1787. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1788. return 0;
  1789. }
  1790. request = list_entry(ioc->hpr_free_list.next,
  1791. struct request_tracker, tracker_list);
  1792. request->cb_idx = cb_idx;
  1793. smid = request->smid;
  1794. list_del(&request->tracker_list);
  1795. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1796. return smid;
  1797. }
  1798. /**
  1799. * mpt3sas_base_free_smid - put smid back on free_list
  1800. * @ioc: per adapter object
  1801. * @smid: system request message index
  1802. *
  1803. * Return nothing.
  1804. */
  1805. void
  1806. mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1807. {
  1808. unsigned long flags;
  1809. int i;
  1810. struct chain_tracker *chain_req, *next;
  1811. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1812. if (smid < ioc->hi_priority_smid) {
  1813. /* scsiio queue */
  1814. i = smid - 1;
  1815. if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
  1816. list_for_each_entry_safe(chain_req, next,
  1817. &ioc->scsi_lookup[i].chain_list, tracker_list) {
  1818. list_del_init(&chain_req->tracker_list);
  1819. list_add(&chain_req->tracker_list,
  1820. &ioc->free_chain_list);
  1821. }
  1822. }
  1823. ioc->scsi_lookup[i].cb_idx = 0xFF;
  1824. ioc->scsi_lookup[i].scmd = NULL;
  1825. list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
  1826. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1827. /*
  1828. * See _wait_for_commands_to_complete() call with regards
  1829. * to this code.
  1830. */
  1831. if (ioc->shost_recovery && ioc->pending_io_count) {
  1832. if (ioc->pending_io_count == 1)
  1833. wake_up(&ioc->reset_wq);
  1834. ioc->pending_io_count--;
  1835. }
  1836. return;
  1837. } else if (smid < ioc->internal_smid) {
  1838. /* hi-priority */
  1839. i = smid - ioc->hi_priority_smid;
  1840. ioc->hpr_lookup[i].cb_idx = 0xFF;
  1841. list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
  1842. } else if (smid <= ioc->hba_queue_depth) {
  1843. /* internal queue */
  1844. i = smid - ioc->internal_smid;
  1845. ioc->internal_lookup[i].cb_idx = 0xFF;
  1846. list_add(&ioc->internal_lookup[i].tracker_list,
  1847. &ioc->internal_free_list);
  1848. }
  1849. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1850. }
  1851. /**
  1852. * _base_writeq - 64 bit write to MMIO
  1853. * @ioc: per adapter object
  1854. * @b: data payload
  1855. * @addr: address in MMIO space
  1856. * @writeq_lock: spin lock
  1857. *
  1858. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  1859. * care of 32 bit environment where its not quarenteed to send the entire word
  1860. * in one transfer.
  1861. */
  1862. #if defined(writeq) && defined(CONFIG_64BIT)
  1863. static inline void
  1864. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  1865. {
  1866. writeq(cpu_to_le64(b), addr);
  1867. }
  1868. #else
  1869. static inline void
  1870. _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
  1871. {
  1872. unsigned long flags;
  1873. __u64 data_out = cpu_to_le64(b);
  1874. spin_lock_irqsave(writeq_lock, flags);
  1875. writel((u32)(data_out), addr);
  1876. writel((u32)(data_out >> 32), (addr + 4));
  1877. spin_unlock_irqrestore(writeq_lock, flags);
  1878. }
  1879. #endif
  1880. static inline u8
  1881. _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
  1882. {
  1883. return ioc->cpu_msix_table[raw_smp_processor_id()];
  1884. }
  1885. /**
  1886. * mpt3sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
  1887. * @ioc: per adapter object
  1888. * @smid: system request message index
  1889. * @handle: device handle
  1890. *
  1891. * Return nothing.
  1892. */
  1893. void
  1894. mpt3sas_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
  1895. {
  1896. Mpi2RequestDescriptorUnion_t descriptor;
  1897. u64 *request = (u64 *)&descriptor;
  1898. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  1899. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  1900. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1901. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1902. descriptor.SCSIIO.LMID = 0;
  1903. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1904. &ioc->scsi_lookup_lock);
  1905. }
  1906. /**
  1907. * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
  1908. * @ioc: per adapter object
  1909. * @smid: system request message index
  1910. * @handle: device handle
  1911. *
  1912. * Return nothing.
  1913. */
  1914. void
  1915. mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  1916. u16 handle)
  1917. {
  1918. Mpi2RequestDescriptorUnion_t descriptor;
  1919. u64 *request = (u64 *)&descriptor;
  1920. descriptor.SCSIIO.RequestFlags =
  1921. MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
  1922. descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
  1923. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1924. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1925. descriptor.SCSIIO.LMID = 0;
  1926. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1927. &ioc->scsi_lookup_lock);
  1928. }
  1929. /**
  1930. * mpt3sas_base_put_smid_hi_priority - send Task Managment request to firmware
  1931. * @ioc: per adapter object
  1932. * @smid: system request message index
  1933. *
  1934. * Return nothing.
  1935. */
  1936. void
  1937. mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1938. {
  1939. Mpi2RequestDescriptorUnion_t descriptor;
  1940. u64 *request = (u64 *)&descriptor;
  1941. descriptor.HighPriority.RequestFlags =
  1942. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  1943. descriptor.HighPriority.MSIxIndex = 0;
  1944. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  1945. descriptor.HighPriority.LMID = 0;
  1946. descriptor.HighPriority.Reserved1 = 0;
  1947. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1948. &ioc->scsi_lookup_lock);
  1949. }
  1950. /**
  1951. * mpt3sas_base_put_smid_default - Default, primarily used for config pages
  1952. * @ioc: per adapter object
  1953. * @smid: system request message index
  1954. *
  1955. * Return nothing.
  1956. */
  1957. void
  1958. mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  1959. {
  1960. Mpi2RequestDescriptorUnion_t descriptor;
  1961. u64 *request = (u64 *)&descriptor;
  1962. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  1963. descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
  1964. descriptor.Default.SMID = cpu_to_le16(smid);
  1965. descriptor.Default.LMID = 0;
  1966. descriptor.Default.DescriptorTypeDependent = 0;
  1967. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1968. &ioc->scsi_lookup_lock);
  1969. }
  1970. /**
  1971. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  1972. * @ioc: per adapter object
  1973. *
  1974. * Return nothing.
  1975. */
  1976. static void
  1977. _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
  1978. {
  1979. int i = 0;
  1980. char desc[16];
  1981. u32 iounit_pg1_flags;
  1982. u32 bios_version;
  1983. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  1984. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  1985. pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
  1986. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  1987. ioc->name, desc,
  1988. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  1989. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  1990. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  1991. ioc->facts.FWVersion.Word & 0x000000FF,
  1992. ioc->pdev->revision,
  1993. (bios_version & 0xFF000000) >> 24,
  1994. (bios_version & 0x00FF0000) >> 16,
  1995. (bios_version & 0x0000FF00) >> 8,
  1996. bios_version & 0x000000FF);
  1997. pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
  1998. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  1999. pr_info("Initiator");
  2000. i++;
  2001. }
  2002. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  2003. pr_info("%sTarget", i ? "," : "");
  2004. i++;
  2005. }
  2006. i = 0;
  2007. pr_info("), ");
  2008. pr_info("Capabilities=(");
  2009. if (ioc->facts.IOCCapabilities &
  2010. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  2011. pr_info("Raid");
  2012. i++;
  2013. }
  2014. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  2015. pr_info("%sTLR", i ? "," : "");
  2016. i++;
  2017. }
  2018. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  2019. pr_info("%sMulticast", i ? "," : "");
  2020. i++;
  2021. }
  2022. if (ioc->facts.IOCCapabilities &
  2023. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  2024. pr_info("%sBIDI Target", i ? "," : "");
  2025. i++;
  2026. }
  2027. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  2028. pr_info("%sEEDP", i ? "," : "");
  2029. i++;
  2030. }
  2031. if (ioc->facts.IOCCapabilities &
  2032. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  2033. pr_info("%sSnapshot Buffer", i ? "," : "");
  2034. i++;
  2035. }
  2036. if (ioc->facts.IOCCapabilities &
  2037. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  2038. pr_info("%sDiag Trace Buffer", i ? "," : "");
  2039. i++;
  2040. }
  2041. if (ioc->facts.IOCCapabilities &
  2042. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  2043. pr_info("%sDiag Extended Buffer", i ? "," : "");
  2044. i++;
  2045. }
  2046. if (ioc->facts.IOCCapabilities &
  2047. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  2048. pr_info("%sTask Set Full", i ? "," : "");
  2049. i++;
  2050. }
  2051. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2052. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  2053. pr_info("%sNCQ", i ? "," : "");
  2054. i++;
  2055. }
  2056. pr_info(")\n");
  2057. }
  2058. /**
  2059. * mpt3sas_base_update_missing_delay - change the missing delay timers
  2060. * @ioc: per adapter object
  2061. * @device_missing_delay: amount of time till device is reported missing
  2062. * @io_missing_delay: interval IO is returned when there is a missing device
  2063. *
  2064. * Return nothing.
  2065. *
  2066. * Passed on the command line, this function will modify the device missing
  2067. * delay, as well as the io missing delay. This should be called at driver
  2068. * load time.
  2069. */
  2070. void
  2071. mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
  2072. u16 device_missing_delay, u8 io_missing_delay)
  2073. {
  2074. u16 dmd, dmd_new, dmd_orignal;
  2075. u8 io_missing_delay_original;
  2076. u16 sz;
  2077. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  2078. Mpi2ConfigReply_t mpi_reply;
  2079. u8 num_phys = 0;
  2080. u16 ioc_status;
  2081. mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
  2082. if (!num_phys)
  2083. return;
  2084. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  2085. sizeof(Mpi2SasIOUnit1PhyData_t));
  2086. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  2087. if (!sas_iounit_pg1) {
  2088. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2089. ioc->name, __FILE__, __LINE__, __func__);
  2090. goto out;
  2091. }
  2092. if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  2093. sas_iounit_pg1, sz))) {
  2094. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2095. ioc->name, __FILE__, __LINE__, __func__);
  2096. goto out;
  2097. }
  2098. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  2099. MPI2_IOCSTATUS_MASK;
  2100. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  2101. pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
  2102. ioc->name, __FILE__, __LINE__, __func__);
  2103. goto out;
  2104. }
  2105. /* device missing delay */
  2106. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  2107. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2108. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2109. else
  2110. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2111. dmd_orignal = dmd;
  2112. if (device_missing_delay > 0x7F) {
  2113. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  2114. device_missing_delay;
  2115. dmd = dmd / 16;
  2116. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  2117. } else
  2118. dmd = device_missing_delay;
  2119. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  2120. /* io missing delay */
  2121. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  2122. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  2123. if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  2124. sz)) {
  2125. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  2126. dmd_new = (dmd &
  2127. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  2128. else
  2129. dmd_new =
  2130. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  2131. pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
  2132. ioc->name, dmd_orignal, dmd_new);
  2133. pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
  2134. ioc->name, io_missing_delay_original,
  2135. io_missing_delay);
  2136. ioc->device_missing_delay = dmd_new;
  2137. ioc->io_missing_delay = io_missing_delay;
  2138. }
  2139. out:
  2140. kfree(sas_iounit_pg1);
  2141. }
  2142. /**
  2143. * _base_static_config_pages - static start of day config pages
  2144. * @ioc: per adapter object
  2145. *
  2146. * Return nothing.
  2147. */
  2148. static void
  2149. _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
  2150. {
  2151. Mpi2ConfigReply_t mpi_reply;
  2152. u32 iounit_pg1_flags;
  2153. mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  2154. if (ioc->ir_firmware)
  2155. mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  2156. &ioc->manu_pg10);
  2157. /*
  2158. * Ensure correct T10 PI operation if vendor left EEDPTagMode
  2159. * flag unset in NVDATA.
  2160. */
  2161. mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
  2162. if (ioc->manu_pg11.EEDPTagMode == 0) {
  2163. pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
  2164. ioc->name);
  2165. ioc->manu_pg11.EEDPTagMode &= ~0x3;
  2166. ioc->manu_pg11.EEDPTagMode |= 0x1;
  2167. mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
  2168. &ioc->manu_pg11);
  2169. }
  2170. mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  2171. mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  2172. mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  2173. mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  2174. mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2175. _base_display_ioc_capabilities(ioc);
  2176. /*
  2177. * Enable task_set_full handling in iounit_pg1 when the
  2178. * facts capabilities indicate that its supported.
  2179. */
  2180. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  2181. if ((ioc->facts.IOCCapabilities &
  2182. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  2183. iounit_pg1_flags &=
  2184. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2185. else
  2186. iounit_pg1_flags |=
  2187. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  2188. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  2189. mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  2190. }
  2191. /**
  2192. * _base_release_memory_pools - release memory
  2193. * @ioc: per adapter object
  2194. *
  2195. * Free memory allocated from _base_allocate_memory_pools.
  2196. *
  2197. * Return nothing.
  2198. */
  2199. static void
  2200. _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
  2201. {
  2202. int i;
  2203. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2204. __func__));
  2205. if (ioc->request) {
  2206. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  2207. ioc->request, ioc->request_dma);
  2208. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2209. "request_pool(0x%p): free\n",
  2210. ioc->name, ioc->request));
  2211. ioc->request = NULL;
  2212. }
  2213. if (ioc->sense) {
  2214. pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  2215. if (ioc->sense_dma_pool)
  2216. pci_pool_destroy(ioc->sense_dma_pool);
  2217. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2218. "sense_pool(0x%p): free\n",
  2219. ioc->name, ioc->sense));
  2220. ioc->sense = NULL;
  2221. }
  2222. if (ioc->reply) {
  2223. pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  2224. if (ioc->reply_dma_pool)
  2225. pci_pool_destroy(ioc->reply_dma_pool);
  2226. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2227. "reply_pool(0x%p): free\n",
  2228. ioc->name, ioc->reply));
  2229. ioc->reply = NULL;
  2230. }
  2231. if (ioc->reply_free) {
  2232. pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  2233. ioc->reply_free_dma);
  2234. if (ioc->reply_free_dma_pool)
  2235. pci_pool_destroy(ioc->reply_free_dma_pool);
  2236. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2237. "reply_free_pool(0x%p): free\n",
  2238. ioc->name, ioc->reply_free));
  2239. ioc->reply_free = NULL;
  2240. }
  2241. if (ioc->reply_post_free) {
  2242. pci_pool_free(ioc->reply_post_free_dma_pool,
  2243. ioc->reply_post_free, ioc->reply_post_free_dma);
  2244. if (ioc->reply_post_free_dma_pool)
  2245. pci_pool_destroy(ioc->reply_post_free_dma_pool);
  2246. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2247. "reply_post_free_pool(0x%p): free\n", ioc->name,
  2248. ioc->reply_post_free));
  2249. ioc->reply_post_free = NULL;
  2250. }
  2251. if (ioc->config_page) {
  2252. dexitprintk(ioc, pr_info(MPT3SAS_FMT
  2253. "config_page(0x%p): free\n", ioc->name,
  2254. ioc->config_page));
  2255. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  2256. ioc->config_page, ioc->config_page_dma);
  2257. }
  2258. if (ioc->scsi_lookup) {
  2259. free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
  2260. ioc->scsi_lookup = NULL;
  2261. }
  2262. kfree(ioc->hpr_lookup);
  2263. kfree(ioc->internal_lookup);
  2264. if (ioc->chain_lookup) {
  2265. for (i = 0; i < ioc->chain_depth; i++) {
  2266. if (ioc->chain_lookup[i].chain_buffer)
  2267. pci_pool_free(ioc->chain_dma_pool,
  2268. ioc->chain_lookup[i].chain_buffer,
  2269. ioc->chain_lookup[i].chain_buffer_dma);
  2270. }
  2271. if (ioc->chain_dma_pool)
  2272. pci_pool_destroy(ioc->chain_dma_pool);
  2273. free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
  2274. ioc->chain_lookup = NULL;
  2275. }
  2276. }
  2277. /**
  2278. * _base_allocate_memory_pools - allocate start of day memory pools
  2279. * @ioc: per adapter object
  2280. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2281. *
  2282. * Returns 0 success, anything else error
  2283. */
  2284. static int
  2285. _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  2286. {
  2287. struct mpt3sas_facts *facts;
  2288. u16 max_sge_elements;
  2289. u16 chains_needed_per_io;
  2290. u32 sz, total_sz, reply_post_free_sz;
  2291. u32 retry_sz;
  2292. u16 max_request_credit;
  2293. unsigned short sg_tablesize;
  2294. u16 sge_size;
  2295. int i;
  2296. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2297. __func__));
  2298. retry_sz = 0;
  2299. facts = &ioc->facts;
  2300. /* command line tunables for max sgl entries */
  2301. if (max_sgl_entries != -1)
  2302. sg_tablesize = max_sgl_entries;
  2303. else
  2304. sg_tablesize = MPT3SAS_SG_DEPTH;
  2305. if (sg_tablesize < MPT3SAS_MIN_PHYS_SEGMENTS)
  2306. sg_tablesize = MPT3SAS_MIN_PHYS_SEGMENTS;
  2307. else if (sg_tablesize > MPT3SAS_MAX_PHYS_SEGMENTS)
  2308. sg_tablesize = MPT3SAS_MAX_PHYS_SEGMENTS;
  2309. ioc->shost->sg_tablesize = sg_tablesize;
  2310. ioc->hi_priority_depth = facts->HighPriorityCredit;
  2311. ioc->internal_depth = ioc->hi_priority_depth + (5);
  2312. /* command line tunables for max controller queue depth */
  2313. if (max_queue_depth != -1 && max_queue_depth != 0) {
  2314. max_request_credit = min_t(u16, max_queue_depth +
  2315. ioc->hi_priority_depth + ioc->internal_depth,
  2316. facts->RequestCredit);
  2317. if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
  2318. max_request_credit = MAX_HBA_QUEUE_DEPTH;
  2319. } else
  2320. max_request_credit = min_t(u16, facts->RequestCredit,
  2321. MAX_HBA_QUEUE_DEPTH);
  2322. ioc->hba_queue_depth = max_request_credit;
  2323. /* request frame size */
  2324. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  2325. /* reply frame size */
  2326. ioc->reply_sz = facts->ReplyFrameSize * 4;
  2327. /* calculate the max scatter element size */
  2328. sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
  2329. retry_allocation:
  2330. total_sz = 0;
  2331. /* calculate number of sg elements left over in the 1st frame */
  2332. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  2333. sizeof(Mpi2SGEIOUnion_t)) + sge_size);
  2334. ioc->max_sges_in_main_message = max_sge_elements/sge_size;
  2335. /* now do the same for a chain buffer */
  2336. max_sge_elements = ioc->request_sz - sge_size;
  2337. ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
  2338. /*
  2339. * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  2340. */
  2341. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  2342. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  2343. + 1;
  2344. if (chains_needed_per_io > facts->MaxChainDepth) {
  2345. chains_needed_per_io = facts->MaxChainDepth;
  2346. ioc->shost->sg_tablesize = min_t(u16,
  2347. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  2348. * chains_needed_per_io), ioc->shost->sg_tablesize);
  2349. }
  2350. ioc->chains_needed_per_io = chains_needed_per_io;
  2351. /* reply free queue sizing - taking into account for 64 FW events */
  2352. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  2353. /* calculate reply descriptor post queue depth */
  2354. ioc->reply_post_queue_depth = ioc->hba_queue_depth +
  2355. ioc->reply_free_queue_depth + 1 ;
  2356. /* align the reply post queue on the next 16 count boundary */
  2357. if (ioc->reply_post_queue_depth % 16)
  2358. ioc->reply_post_queue_depth += 16 -
  2359. (ioc->reply_post_queue_depth % 16);
  2360. if (ioc->reply_post_queue_depth >
  2361. facts->MaxReplyDescriptorPostQueueDepth) {
  2362. ioc->reply_post_queue_depth =
  2363. facts->MaxReplyDescriptorPostQueueDepth -
  2364. (facts->MaxReplyDescriptorPostQueueDepth % 16);
  2365. ioc->hba_queue_depth =
  2366. ((ioc->reply_post_queue_depth - 64) / 2) - 1;
  2367. ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
  2368. }
  2369. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
  2370. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  2371. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  2372. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  2373. ioc->chains_needed_per_io));
  2374. ioc->scsiio_depth = ioc->hba_queue_depth -
  2375. ioc->hi_priority_depth - ioc->internal_depth;
  2376. /* set the scsi host can_queue depth
  2377. * with some internal commands that could be outstanding
  2378. */
  2379. ioc->shost->can_queue = ioc->scsiio_depth;
  2380. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2381. "scsi host: can_queue depth (%d)\n",
  2382. ioc->name, ioc->shost->can_queue));
  2383. /* contiguous pool for request and chains, 16 byte align, one extra "
  2384. * "frame for smid=0
  2385. */
  2386. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  2387. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  2388. /* hi-priority queue */
  2389. sz += (ioc->hi_priority_depth * ioc->request_sz);
  2390. /* internal queue */
  2391. sz += (ioc->internal_depth * ioc->request_sz);
  2392. ioc->request_dma_sz = sz;
  2393. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  2394. if (!ioc->request) {
  2395. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  2396. "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2397. "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
  2398. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2399. if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
  2400. goto out;
  2401. retry_sz += 64;
  2402. ioc->hba_queue_depth = max_request_credit - retry_sz;
  2403. goto retry_allocation;
  2404. }
  2405. if (retry_sz)
  2406. pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
  2407. "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2408. "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
  2409. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2410. /* hi-priority queue */
  2411. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  2412. ioc->request_sz);
  2413. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  2414. ioc->request_sz);
  2415. /* internal queue */
  2416. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  2417. ioc->request_sz);
  2418. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  2419. ioc->request_sz);
  2420. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2421. "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  2422. ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
  2423. (ioc->hba_queue_depth * ioc->request_sz)/1024));
  2424. dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
  2425. ioc->name, (unsigned long long) ioc->request_dma));
  2426. total_sz += sz;
  2427. sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
  2428. ioc->scsi_lookup_pages = get_order(sz);
  2429. ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
  2430. GFP_KERNEL, ioc->scsi_lookup_pages);
  2431. if (!ioc->scsi_lookup) {
  2432. pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
  2433. ioc->name, (int)sz);
  2434. goto out;
  2435. }
  2436. dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
  2437. ioc->name, ioc->request, ioc->scsiio_depth));
  2438. ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
  2439. sz = ioc->chain_depth * sizeof(struct chain_tracker);
  2440. ioc->chain_pages = get_order(sz);
  2441. ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
  2442. GFP_KERNEL, ioc->chain_pages);
  2443. if (!ioc->chain_lookup) {
  2444. pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
  2445. ioc->name);
  2446. goto out;
  2447. }
  2448. ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
  2449. ioc->request_sz, 16, 0);
  2450. if (!ioc->chain_dma_pool) {
  2451. pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
  2452. ioc->name);
  2453. goto out;
  2454. }
  2455. for (i = 0; i < ioc->chain_depth; i++) {
  2456. ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
  2457. ioc->chain_dma_pool , GFP_KERNEL,
  2458. &ioc->chain_lookup[i].chain_buffer_dma);
  2459. if (!ioc->chain_lookup[i].chain_buffer) {
  2460. ioc->chain_depth = i;
  2461. goto chain_done;
  2462. }
  2463. total_sz += ioc->request_sz;
  2464. }
  2465. chain_done:
  2466. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2467. "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
  2468. ioc->name, ioc->chain_depth, ioc->request_sz,
  2469. ((ioc->chain_depth * ioc->request_sz))/1024));
  2470. /* initialize hi-priority queue smid's */
  2471. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  2472. sizeof(struct request_tracker), GFP_KERNEL);
  2473. if (!ioc->hpr_lookup) {
  2474. pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
  2475. ioc->name);
  2476. goto out;
  2477. }
  2478. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  2479. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2480. "hi_priority(0x%p): depth(%d), start smid(%d)\n",
  2481. ioc->name, ioc->hi_priority,
  2482. ioc->hi_priority_depth, ioc->hi_priority_smid));
  2483. /* initialize internal queue smid's */
  2484. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  2485. sizeof(struct request_tracker), GFP_KERNEL);
  2486. if (!ioc->internal_lookup) {
  2487. pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
  2488. ioc->name);
  2489. goto out;
  2490. }
  2491. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  2492. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2493. "internal(0x%p): depth(%d), start smid(%d)\n",
  2494. ioc->name, ioc->internal,
  2495. ioc->internal_depth, ioc->internal_smid));
  2496. /* sense buffers, 4 byte align */
  2497. sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  2498. ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
  2499. 0);
  2500. if (!ioc->sense_dma_pool) {
  2501. pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
  2502. ioc->name);
  2503. goto out;
  2504. }
  2505. ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
  2506. &ioc->sense_dma);
  2507. if (!ioc->sense) {
  2508. pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
  2509. ioc->name);
  2510. goto out;
  2511. }
  2512. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2513. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  2514. "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
  2515. SCSI_SENSE_BUFFERSIZE, sz/1024));
  2516. dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
  2517. ioc->name, (unsigned long long)ioc->sense_dma));
  2518. total_sz += sz;
  2519. /* reply pool, 4 byte align */
  2520. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  2521. ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
  2522. 0);
  2523. if (!ioc->reply_dma_pool) {
  2524. pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
  2525. ioc->name);
  2526. goto out;
  2527. }
  2528. ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
  2529. &ioc->reply_dma);
  2530. if (!ioc->reply) {
  2531. pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
  2532. ioc->name);
  2533. goto out;
  2534. }
  2535. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  2536. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  2537. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2538. "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
  2539. ioc->name, ioc->reply,
  2540. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  2541. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
  2542. ioc->name, (unsigned long long)ioc->reply_dma));
  2543. total_sz += sz;
  2544. /* reply free queue, 16 byte align */
  2545. sz = ioc->reply_free_queue_depth * 4;
  2546. ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
  2547. ioc->pdev, sz, 16, 0);
  2548. if (!ioc->reply_free_dma_pool) {
  2549. pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
  2550. ioc->name);
  2551. goto out;
  2552. }
  2553. ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
  2554. &ioc->reply_free_dma);
  2555. if (!ioc->reply_free) {
  2556. pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
  2557. ioc->name);
  2558. goto out;
  2559. }
  2560. memset(ioc->reply_free, 0, sz);
  2561. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
  2562. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  2563. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  2564. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2565. "reply_free_dma (0x%llx)\n",
  2566. ioc->name, (unsigned long long)ioc->reply_free_dma));
  2567. total_sz += sz;
  2568. /* reply post queue, 16 byte align */
  2569. reply_post_free_sz = ioc->reply_post_queue_depth *
  2570. sizeof(Mpi2DefaultReplyDescriptor_t);
  2571. if (_base_is_controller_msix_enabled(ioc))
  2572. sz = reply_post_free_sz * ioc->reply_queue_count;
  2573. else
  2574. sz = reply_post_free_sz;
  2575. ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
  2576. ioc->pdev, sz, 16, 0);
  2577. if (!ioc->reply_post_free_dma_pool) {
  2578. pr_err(MPT3SAS_FMT
  2579. "reply_post_free pool: pci_pool_create failed\n",
  2580. ioc->name);
  2581. goto out;
  2582. }
  2583. ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
  2584. GFP_KERNEL, &ioc->reply_post_free_dma);
  2585. if (!ioc->reply_post_free) {
  2586. pr_err(MPT3SAS_FMT
  2587. "reply_post_free pool: pci_pool_alloc failed\n",
  2588. ioc->name);
  2589. goto out;
  2590. }
  2591. memset(ioc->reply_post_free, 0, sz);
  2592. dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply post free pool" \
  2593. "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
  2594. ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
  2595. sz/1024));
  2596. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2597. "reply_post_free_dma = (0x%llx)\n",
  2598. ioc->name, (unsigned long long)
  2599. ioc->reply_post_free_dma));
  2600. total_sz += sz;
  2601. ioc->config_page_sz = 512;
  2602. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  2603. ioc->config_page_sz, &ioc->config_page_dma);
  2604. if (!ioc->config_page) {
  2605. pr_err(MPT3SAS_FMT
  2606. "config page: pci_pool_alloc failed\n",
  2607. ioc->name);
  2608. goto out;
  2609. }
  2610. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  2611. "config page(0x%p): size(%d)\n",
  2612. ioc->name, ioc->config_page, ioc->config_page_sz));
  2613. dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
  2614. ioc->name, (unsigned long long)ioc->config_page_dma));
  2615. total_sz += ioc->config_page_sz;
  2616. pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
  2617. ioc->name, total_sz/1024);
  2618. pr_info(MPT3SAS_FMT
  2619. "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
  2620. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  2621. pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
  2622. ioc->name, ioc->shost->sg_tablesize);
  2623. return 0;
  2624. out:
  2625. return -ENOMEM;
  2626. }
  2627. /**
  2628. * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
  2629. * @ioc: Pointer to MPT_ADAPTER structure
  2630. * @cooked: Request raw or cooked IOC state
  2631. *
  2632. * Returns all IOC Doorbell register bits if cooked==0, else just the
  2633. * Doorbell bits in MPI_IOC_STATE_MASK.
  2634. */
  2635. u32
  2636. mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
  2637. {
  2638. u32 s, sc;
  2639. s = readl(&ioc->chip->Doorbell);
  2640. sc = s & MPI2_IOC_STATE_MASK;
  2641. return cooked ? sc : s;
  2642. }
  2643. /**
  2644. * _base_wait_on_iocstate - waiting on a particular ioc state
  2645. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  2646. * @timeout: timeout in second
  2647. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2648. *
  2649. * Returns 0 for success, non-zero for failure.
  2650. */
  2651. static int
  2652. _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
  2653. int sleep_flag)
  2654. {
  2655. u32 count, cntdn;
  2656. u32 current_state;
  2657. count = 0;
  2658. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2659. do {
  2660. current_state = mpt3sas_base_get_iocstate(ioc, 1);
  2661. if (current_state == ioc_state)
  2662. return 0;
  2663. if (count && current_state == MPI2_IOC_STATE_FAULT)
  2664. break;
  2665. if (sleep_flag == CAN_SLEEP)
  2666. usleep_range(1000, 1500);
  2667. else
  2668. udelay(500);
  2669. count++;
  2670. } while (--cntdn);
  2671. return current_state;
  2672. }
  2673. /**
  2674. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  2675. * a write to the doorbell)
  2676. * @ioc: per adapter object
  2677. * @timeout: timeout in second
  2678. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2679. *
  2680. * Returns 0 for success, non-zero for failure.
  2681. *
  2682. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  2683. */
  2684. static int
  2685. _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout,
  2686. int sleep_flag)
  2687. {
  2688. u32 cntdn, count;
  2689. u32 int_status;
  2690. count = 0;
  2691. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2692. do {
  2693. int_status = readl(&ioc->chip->HostInterruptStatus);
  2694. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2695. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2696. "%s: successful count(%d), timeout(%d)\n",
  2697. ioc->name, __func__, count, timeout));
  2698. return 0;
  2699. }
  2700. if (sleep_flag == CAN_SLEEP)
  2701. usleep_range(1000, 1500);
  2702. else
  2703. udelay(500);
  2704. count++;
  2705. } while (--cntdn);
  2706. pr_err(MPT3SAS_FMT
  2707. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  2708. ioc->name, __func__, count, int_status);
  2709. return -EFAULT;
  2710. }
  2711. /**
  2712. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  2713. * @ioc: per adapter object
  2714. * @timeout: timeout in second
  2715. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2716. *
  2717. * Returns 0 for success, non-zero for failure.
  2718. *
  2719. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  2720. * doorbell.
  2721. */
  2722. static int
  2723. _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout,
  2724. int sleep_flag)
  2725. {
  2726. u32 cntdn, count;
  2727. u32 int_status;
  2728. u32 doorbell;
  2729. count = 0;
  2730. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2731. do {
  2732. int_status = readl(&ioc->chip->HostInterruptStatus);
  2733. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  2734. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2735. "%s: successful count(%d), timeout(%d)\n",
  2736. ioc->name, __func__, count, timeout));
  2737. return 0;
  2738. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2739. doorbell = readl(&ioc->chip->Doorbell);
  2740. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  2741. MPI2_IOC_STATE_FAULT) {
  2742. mpt3sas_base_fault_info(ioc , doorbell);
  2743. return -EFAULT;
  2744. }
  2745. } else if (int_status == 0xFFFFFFFF)
  2746. goto out;
  2747. if (sleep_flag == CAN_SLEEP)
  2748. usleep_range(1000, 1500);
  2749. else
  2750. udelay(500);
  2751. count++;
  2752. } while (--cntdn);
  2753. out:
  2754. pr_err(MPT3SAS_FMT
  2755. "%s: failed due to timeout count(%d), int_status(%x)!\n",
  2756. ioc->name, __func__, count, int_status);
  2757. return -EFAULT;
  2758. }
  2759. /**
  2760. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  2761. * @ioc: per adapter object
  2762. * @timeout: timeout in second
  2763. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2764. *
  2765. * Returns 0 for success, non-zero for failure.
  2766. *
  2767. */
  2768. static int
  2769. _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout,
  2770. int sleep_flag)
  2771. {
  2772. u32 cntdn, count;
  2773. u32 doorbell_reg;
  2774. count = 0;
  2775. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2776. do {
  2777. doorbell_reg = readl(&ioc->chip->Doorbell);
  2778. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  2779. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2780. "%s: successful count(%d), timeout(%d)\n",
  2781. ioc->name, __func__, count, timeout));
  2782. return 0;
  2783. }
  2784. if (sleep_flag == CAN_SLEEP)
  2785. usleep_range(1000, 1500);
  2786. else
  2787. udelay(500);
  2788. count++;
  2789. } while (--cntdn);
  2790. pr_err(MPT3SAS_FMT
  2791. "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
  2792. ioc->name, __func__, count, doorbell_reg);
  2793. return -EFAULT;
  2794. }
  2795. /**
  2796. * _base_send_ioc_reset - send doorbell reset
  2797. * @ioc: per adapter object
  2798. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  2799. * @timeout: timeout in second
  2800. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2801. *
  2802. * Returns 0 for success, non-zero for failure.
  2803. */
  2804. static int
  2805. _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout,
  2806. int sleep_flag)
  2807. {
  2808. u32 ioc_state;
  2809. int r = 0;
  2810. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  2811. pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
  2812. ioc->name, __func__);
  2813. return -EFAULT;
  2814. }
  2815. if (!(ioc->facts.IOCCapabilities &
  2816. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  2817. return -EFAULT;
  2818. pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
  2819. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  2820. &ioc->chip->Doorbell);
  2821. if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
  2822. r = -EFAULT;
  2823. goto out;
  2824. }
  2825. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
  2826. timeout, sleep_flag);
  2827. if (ioc_state) {
  2828. pr_err(MPT3SAS_FMT
  2829. "%s: failed going to ready state (ioc_state=0x%x)\n",
  2830. ioc->name, __func__, ioc_state);
  2831. r = -EFAULT;
  2832. goto out;
  2833. }
  2834. out:
  2835. pr_info(MPT3SAS_FMT "message unit reset: %s\n",
  2836. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  2837. return r;
  2838. }
  2839. /**
  2840. * _base_handshake_req_reply_wait - send request thru doorbell interface
  2841. * @ioc: per adapter object
  2842. * @request_bytes: request length
  2843. * @request: pointer having request payload
  2844. * @reply_bytes: reply length
  2845. * @reply: pointer to reply payload
  2846. * @timeout: timeout in second
  2847. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2848. *
  2849. * Returns 0 for success, non-zero for failure.
  2850. */
  2851. static int
  2852. _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
  2853. u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
  2854. {
  2855. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  2856. int i;
  2857. u8 failed;
  2858. u16 dummy;
  2859. __le32 *mfp;
  2860. /* make sure doorbell is not in use */
  2861. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  2862. pr_err(MPT3SAS_FMT
  2863. "doorbell is in use (line=%d)\n",
  2864. ioc->name, __LINE__);
  2865. return -EFAULT;
  2866. }
  2867. /* clear pending doorbell interrupts from previous state changes */
  2868. if (readl(&ioc->chip->HostInterruptStatus) &
  2869. MPI2_HIS_IOC2SYS_DB_STATUS)
  2870. writel(0, &ioc->chip->HostInterruptStatus);
  2871. /* send message to ioc */
  2872. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  2873. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  2874. &ioc->chip->Doorbell);
  2875. if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
  2876. pr_err(MPT3SAS_FMT
  2877. "doorbell handshake int failed (line=%d)\n",
  2878. ioc->name, __LINE__);
  2879. return -EFAULT;
  2880. }
  2881. writel(0, &ioc->chip->HostInterruptStatus);
  2882. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
  2883. pr_err(MPT3SAS_FMT
  2884. "doorbell handshake ack failed (line=%d)\n",
  2885. ioc->name, __LINE__);
  2886. return -EFAULT;
  2887. }
  2888. /* send message 32-bits at a time */
  2889. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  2890. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  2891. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
  2892. failed = 1;
  2893. }
  2894. if (failed) {
  2895. pr_err(MPT3SAS_FMT
  2896. "doorbell handshake sending request failed (line=%d)\n",
  2897. ioc->name, __LINE__);
  2898. return -EFAULT;
  2899. }
  2900. /* now wait for the reply */
  2901. if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
  2902. pr_err(MPT3SAS_FMT
  2903. "doorbell handshake int failed (line=%d)\n",
  2904. ioc->name, __LINE__);
  2905. return -EFAULT;
  2906. }
  2907. /* read the first two 16-bits, it gives the total length of the reply */
  2908. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2909. & MPI2_DOORBELL_DATA_MASK);
  2910. writel(0, &ioc->chip->HostInterruptStatus);
  2911. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2912. pr_err(MPT3SAS_FMT
  2913. "doorbell handshake int failed (line=%d)\n",
  2914. ioc->name, __LINE__);
  2915. return -EFAULT;
  2916. }
  2917. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2918. & MPI2_DOORBELL_DATA_MASK);
  2919. writel(0, &ioc->chip->HostInterruptStatus);
  2920. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  2921. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2922. pr_err(MPT3SAS_FMT
  2923. "doorbell handshake int failed (line=%d)\n",
  2924. ioc->name, __LINE__);
  2925. return -EFAULT;
  2926. }
  2927. if (i >= reply_bytes/2) /* overflow case */
  2928. dummy = readl(&ioc->chip->Doorbell);
  2929. else
  2930. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2931. & MPI2_DOORBELL_DATA_MASK);
  2932. writel(0, &ioc->chip->HostInterruptStatus);
  2933. }
  2934. _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
  2935. if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
  2936. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  2937. "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
  2938. }
  2939. writel(0, &ioc->chip->HostInterruptStatus);
  2940. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2941. mfp = (__le32 *)reply;
  2942. pr_info("\toffset:data\n");
  2943. for (i = 0; i < reply_bytes/4; i++)
  2944. pr_info("\t[0x%02x]:%08x\n", i*4,
  2945. le32_to_cpu(mfp[i]));
  2946. }
  2947. return 0;
  2948. }
  2949. /**
  2950. * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
  2951. * @ioc: per adapter object
  2952. * @mpi_reply: the reply payload from FW
  2953. * @mpi_request: the request payload sent to FW
  2954. *
  2955. * The SAS IO Unit Control Request message allows the host to perform low-level
  2956. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  2957. * to obtain the IOC assigned device handles for a device if it has other
  2958. * identifying information about the device, in addition allows the host to
  2959. * remove IOC resources associated with the device.
  2960. *
  2961. * Returns 0 for success, non-zero for failure.
  2962. */
  2963. int
  2964. mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
  2965. Mpi2SasIoUnitControlReply_t *mpi_reply,
  2966. Mpi2SasIoUnitControlRequest_t *mpi_request)
  2967. {
  2968. u16 smid;
  2969. u32 ioc_state;
  2970. unsigned long timeleft;
  2971. u8 issue_reset;
  2972. int rc;
  2973. void *request;
  2974. u16 wait_state_count;
  2975. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  2976. __func__));
  2977. mutex_lock(&ioc->base_cmds.mutex);
  2978. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  2979. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  2980. ioc->name, __func__);
  2981. rc = -EAGAIN;
  2982. goto out;
  2983. }
  2984. wait_state_count = 0;
  2985. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  2986. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2987. if (wait_state_count++ == 10) {
  2988. pr_err(MPT3SAS_FMT
  2989. "%s: failed due to ioc not operational\n",
  2990. ioc->name, __func__);
  2991. rc = -EFAULT;
  2992. goto out;
  2993. }
  2994. ssleep(1);
  2995. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  2996. pr_info(MPT3SAS_FMT
  2997. "%s: waiting for operational state(count=%d)\n",
  2998. ioc->name, __func__, wait_state_count);
  2999. }
  3000. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3001. if (!smid) {
  3002. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3003. ioc->name, __func__);
  3004. rc = -EAGAIN;
  3005. goto out;
  3006. }
  3007. rc = 0;
  3008. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3009. request = mpt3sas_base_get_msg_frame(ioc, smid);
  3010. ioc->base_cmds.smid = smid;
  3011. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  3012. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  3013. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  3014. ioc->ioc_link_reset_in_progress = 1;
  3015. init_completion(&ioc->base_cmds.done);
  3016. mpt3sas_base_put_smid_default(ioc, smid);
  3017. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  3018. msecs_to_jiffies(10000));
  3019. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  3020. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  3021. ioc->ioc_link_reset_in_progress)
  3022. ioc->ioc_link_reset_in_progress = 0;
  3023. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3024. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3025. ioc->name, __func__);
  3026. _debug_dump_mf(mpi_request,
  3027. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  3028. if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
  3029. issue_reset = 1;
  3030. goto issue_host_reset;
  3031. }
  3032. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  3033. memcpy(mpi_reply, ioc->base_cmds.reply,
  3034. sizeof(Mpi2SasIoUnitControlReply_t));
  3035. else
  3036. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  3037. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3038. goto out;
  3039. issue_host_reset:
  3040. if (issue_reset)
  3041. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  3042. FORCE_BIG_HAMMER);
  3043. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3044. rc = -EFAULT;
  3045. out:
  3046. mutex_unlock(&ioc->base_cmds.mutex);
  3047. return rc;
  3048. }
  3049. /**
  3050. * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
  3051. * @ioc: per adapter object
  3052. * @mpi_reply: the reply payload from FW
  3053. * @mpi_request: the request payload sent to FW
  3054. *
  3055. * The SCSI Enclosure Processor request message causes the IOC to
  3056. * communicate with SES devices to control LED status signals.
  3057. *
  3058. * Returns 0 for success, non-zero for failure.
  3059. */
  3060. int
  3061. mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
  3062. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  3063. {
  3064. u16 smid;
  3065. u32 ioc_state;
  3066. unsigned long timeleft;
  3067. u8 issue_reset;
  3068. int rc;
  3069. void *request;
  3070. u16 wait_state_count;
  3071. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3072. __func__));
  3073. mutex_lock(&ioc->base_cmds.mutex);
  3074. if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
  3075. pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
  3076. ioc->name, __func__);
  3077. rc = -EAGAIN;
  3078. goto out;
  3079. }
  3080. wait_state_count = 0;
  3081. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3082. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  3083. if (wait_state_count++ == 10) {
  3084. pr_err(MPT3SAS_FMT
  3085. "%s: failed due to ioc not operational\n",
  3086. ioc->name, __func__);
  3087. rc = -EFAULT;
  3088. goto out;
  3089. }
  3090. ssleep(1);
  3091. ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
  3092. pr_info(MPT3SAS_FMT
  3093. "%s: waiting for operational state(count=%d)\n",
  3094. ioc->name,
  3095. __func__, wait_state_count);
  3096. }
  3097. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3098. if (!smid) {
  3099. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3100. ioc->name, __func__);
  3101. rc = -EAGAIN;
  3102. goto out;
  3103. }
  3104. rc = 0;
  3105. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3106. request = mpt3sas_base_get_msg_frame(ioc, smid);
  3107. ioc->base_cmds.smid = smid;
  3108. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  3109. init_completion(&ioc->base_cmds.done);
  3110. mpt3sas_base_put_smid_default(ioc, smid);
  3111. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  3112. msecs_to_jiffies(10000));
  3113. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3114. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3115. ioc->name, __func__);
  3116. _debug_dump_mf(mpi_request,
  3117. sizeof(Mpi2SepRequest_t)/4);
  3118. if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
  3119. issue_reset = 1;
  3120. goto issue_host_reset;
  3121. }
  3122. if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
  3123. memcpy(mpi_reply, ioc->base_cmds.reply,
  3124. sizeof(Mpi2SepReply_t));
  3125. else
  3126. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  3127. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3128. goto out;
  3129. issue_host_reset:
  3130. if (issue_reset)
  3131. mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  3132. FORCE_BIG_HAMMER);
  3133. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3134. rc = -EFAULT;
  3135. out:
  3136. mutex_unlock(&ioc->base_cmds.mutex);
  3137. return rc;
  3138. }
  3139. /**
  3140. * _base_get_port_facts - obtain port facts reply and save in ioc
  3141. * @ioc: per adapter object
  3142. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3143. *
  3144. * Returns 0 for success, non-zero for failure.
  3145. */
  3146. static int
  3147. _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port, int sleep_flag)
  3148. {
  3149. Mpi2PortFactsRequest_t mpi_request;
  3150. Mpi2PortFactsReply_t mpi_reply;
  3151. struct mpt3sas_port_facts *pfacts;
  3152. int mpi_reply_sz, mpi_request_sz, r;
  3153. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3154. __func__));
  3155. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  3156. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  3157. memset(&mpi_request, 0, mpi_request_sz);
  3158. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  3159. mpi_request.PortNumber = port;
  3160. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3161. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3162. if (r != 0) {
  3163. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3164. ioc->name, __func__, r);
  3165. return r;
  3166. }
  3167. pfacts = &ioc->pfacts[port];
  3168. memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
  3169. pfacts->PortNumber = mpi_reply.PortNumber;
  3170. pfacts->VP_ID = mpi_reply.VP_ID;
  3171. pfacts->VF_ID = mpi_reply.VF_ID;
  3172. pfacts->MaxPostedCmdBuffers =
  3173. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  3174. return 0;
  3175. }
  3176. /**
  3177. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  3178. * @ioc: per adapter object
  3179. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3180. *
  3181. * Returns 0 for success, non-zero for failure.
  3182. */
  3183. static int
  3184. _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3185. {
  3186. Mpi2IOCFactsRequest_t mpi_request;
  3187. Mpi2IOCFactsReply_t mpi_reply;
  3188. struct mpt3sas_facts *facts;
  3189. int mpi_reply_sz, mpi_request_sz, r;
  3190. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3191. __func__));
  3192. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  3193. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  3194. memset(&mpi_request, 0, mpi_request_sz);
  3195. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  3196. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  3197. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  3198. if (r != 0) {
  3199. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3200. ioc->name, __func__, r);
  3201. return r;
  3202. }
  3203. facts = &ioc->facts;
  3204. memset(facts, 0, sizeof(struct mpt3sas_facts));
  3205. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  3206. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  3207. facts->VP_ID = mpi_reply.VP_ID;
  3208. facts->VF_ID = mpi_reply.VF_ID;
  3209. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  3210. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  3211. facts->WhoInit = mpi_reply.WhoInit;
  3212. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  3213. facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
  3214. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  3215. facts->MaxReplyDescriptorPostQueueDepth =
  3216. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  3217. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  3218. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  3219. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  3220. ioc->ir_firmware = 1;
  3221. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  3222. facts->IOCRequestFrameSize =
  3223. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  3224. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  3225. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  3226. ioc->shost->max_id = -1;
  3227. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  3228. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  3229. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  3230. facts->HighPriorityCredit =
  3231. le16_to_cpu(mpi_reply.HighPriorityCredit);
  3232. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  3233. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  3234. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3235. "hba queue depth(%d), max chains per io(%d)\n",
  3236. ioc->name, facts->RequestCredit,
  3237. facts->MaxChainDepth));
  3238. dinitprintk(ioc, pr_info(MPT3SAS_FMT
  3239. "request frame size(%d), reply frame size(%d)\n", ioc->name,
  3240. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  3241. return 0;
  3242. }
  3243. /**
  3244. * _base_send_ioc_init - send ioc_init to firmware
  3245. * @ioc: per adapter object
  3246. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3247. *
  3248. * Returns 0 for success, non-zero for failure.
  3249. */
  3250. static int
  3251. _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3252. {
  3253. Mpi2IOCInitRequest_t mpi_request;
  3254. Mpi2IOCInitReply_t mpi_reply;
  3255. int r;
  3256. struct timeval current_time;
  3257. u16 ioc_status;
  3258. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3259. __func__));
  3260. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  3261. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  3262. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  3263. mpi_request.VF_ID = 0; /* TODO */
  3264. mpi_request.VP_ID = 0;
  3265. mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
  3266. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  3267. if (_base_is_controller_msix_enabled(ioc))
  3268. mpi_request.HostMSIxVectors = ioc->reply_queue_count;
  3269. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  3270. mpi_request.ReplyDescriptorPostQueueDepth =
  3271. cpu_to_le16(ioc->reply_post_queue_depth);
  3272. mpi_request.ReplyFreeQueueDepth =
  3273. cpu_to_le16(ioc->reply_free_queue_depth);
  3274. mpi_request.SenseBufferAddressHigh =
  3275. cpu_to_le32((u64)ioc->sense_dma >> 32);
  3276. mpi_request.SystemReplyAddressHigh =
  3277. cpu_to_le32((u64)ioc->reply_dma >> 32);
  3278. mpi_request.SystemRequestFrameBaseAddress =
  3279. cpu_to_le64((u64)ioc->request_dma);
  3280. mpi_request.ReplyFreeQueueAddress =
  3281. cpu_to_le64((u64)ioc->reply_free_dma);
  3282. mpi_request.ReplyDescriptorPostQueueAddress =
  3283. cpu_to_le64((u64)ioc->reply_post_free_dma);
  3284. /* This time stamp specifies number of milliseconds
  3285. * since epoch ~ midnight January 1, 1970.
  3286. */
  3287. do_gettimeofday(&current_time);
  3288. mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
  3289. (current_time.tv_usec / 1000));
  3290. if (ioc->logging_level & MPT_DEBUG_INIT) {
  3291. __le32 *mfp;
  3292. int i;
  3293. mfp = (__le32 *)&mpi_request;
  3294. pr_info("\toffset:data\n");
  3295. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  3296. pr_info("\t[0x%02x]:%08x\n", i*4,
  3297. le32_to_cpu(mfp[i]));
  3298. }
  3299. r = _base_handshake_req_reply_wait(ioc,
  3300. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  3301. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
  3302. sleep_flag);
  3303. if (r != 0) {
  3304. pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
  3305. ioc->name, __func__, r);
  3306. return r;
  3307. }
  3308. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  3309. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  3310. mpi_reply.IOCLogInfo) {
  3311. pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
  3312. r = -EIO;
  3313. }
  3314. return 0;
  3315. }
  3316. /**
  3317. * mpt3sas_port_enable_done - command completion routine for port enable
  3318. * @ioc: per adapter object
  3319. * @smid: system request message index
  3320. * @msix_index: MSIX table index supplied by the OS
  3321. * @reply: reply message frame(lower 32bit addr)
  3322. *
  3323. * Return 1 meaning mf should be freed from _base_interrupt
  3324. * 0 means the mf is freed from this function.
  3325. */
  3326. u8
  3327. mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  3328. u32 reply)
  3329. {
  3330. MPI2DefaultReply_t *mpi_reply;
  3331. u16 ioc_status;
  3332. if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
  3333. return 1;
  3334. mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
  3335. if (!mpi_reply)
  3336. return 1;
  3337. if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
  3338. return 1;
  3339. ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
  3340. ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
  3341. ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
  3342. memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  3343. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3344. if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
  3345. ioc->port_enable_failed = 1;
  3346. if (ioc->is_driver_loading) {
  3347. if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
  3348. mpt3sas_port_enable_complete(ioc);
  3349. return 1;
  3350. } else {
  3351. ioc->start_scan_failed = ioc_status;
  3352. ioc->start_scan = 0;
  3353. return 1;
  3354. }
  3355. }
  3356. complete(&ioc->port_enable_cmds.done);
  3357. return 1;
  3358. }
  3359. /**
  3360. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  3361. * @ioc: per adapter object
  3362. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3363. *
  3364. * Returns 0 for success, non-zero for failure.
  3365. */
  3366. static int
  3367. _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3368. {
  3369. Mpi2PortEnableRequest_t *mpi_request;
  3370. Mpi2PortEnableReply_t *mpi_reply;
  3371. unsigned long timeleft;
  3372. int r = 0;
  3373. u16 smid;
  3374. u16 ioc_status;
  3375. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  3376. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  3377. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  3378. ioc->name, __func__);
  3379. return -EAGAIN;
  3380. }
  3381. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3382. if (!smid) {
  3383. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3384. ioc->name, __func__);
  3385. return -EAGAIN;
  3386. }
  3387. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  3388. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3389. ioc->port_enable_cmds.smid = smid;
  3390. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3391. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3392. init_completion(&ioc->port_enable_cmds.done);
  3393. mpt3sas_base_put_smid_default(ioc, smid);
  3394. timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
  3395. 300*HZ);
  3396. if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
  3397. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3398. ioc->name, __func__);
  3399. _debug_dump_mf(mpi_request,
  3400. sizeof(Mpi2PortEnableRequest_t)/4);
  3401. if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
  3402. r = -EFAULT;
  3403. else
  3404. r = -ETIME;
  3405. goto out;
  3406. }
  3407. mpi_reply = ioc->port_enable_cmds.reply;
  3408. ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
  3409. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  3410. pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
  3411. ioc->name, __func__, ioc_status);
  3412. r = -EFAULT;
  3413. goto out;
  3414. }
  3415. out:
  3416. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  3417. pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
  3418. "SUCCESS" : "FAILED"));
  3419. return r;
  3420. }
  3421. /**
  3422. * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
  3423. * @ioc: per adapter object
  3424. *
  3425. * Returns 0 for success, non-zero for failure.
  3426. */
  3427. int
  3428. mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
  3429. {
  3430. Mpi2PortEnableRequest_t *mpi_request;
  3431. u16 smid;
  3432. pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
  3433. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  3434. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  3435. ioc->name, __func__);
  3436. return -EAGAIN;
  3437. }
  3438. smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
  3439. if (!smid) {
  3440. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3441. ioc->name, __func__);
  3442. return -EAGAIN;
  3443. }
  3444. ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
  3445. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3446. ioc->port_enable_cmds.smid = smid;
  3447. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  3448. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  3449. mpt3sas_base_put_smid_default(ioc, smid);
  3450. return 0;
  3451. }
  3452. /**
  3453. * _base_determine_wait_on_discovery - desposition
  3454. * @ioc: per adapter object
  3455. *
  3456. * Decide whether to wait on discovery to complete. Used to either
  3457. * locate boot device, or report volumes ahead of physical devices.
  3458. *
  3459. * Returns 1 for wait, 0 for don't wait
  3460. */
  3461. static int
  3462. _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
  3463. {
  3464. /* We wait for discovery to complete if IR firmware is loaded.
  3465. * The sas topology events arrive before PD events, so we need time to
  3466. * turn on the bit in ioc->pd_handles to indicate PD
  3467. * Also, it maybe required to report Volumes ahead of physical
  3468. * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
  3469. */
  3470. if (ioc->ir_firmware)
  3471. return 1;
  3472. /* if no Bios, then we don't need to wait */
  3473. if (!ioc->bios_pg3.BiosVersion)
  3474. return 0;
  3475. /* Bios is present, then we drop down here.
  3476. *
  3477. * If there any entries in the Bios Page 2, then we wait
  3478. * for discovery to complete.
  3479. */
  3480. /* Current Boot Device */
  3481. if ((ioc->bios_pg2.CurrentBootDeviceForm &
  3482. MPI2_BIOSPAGE2_FORM_MASK) ==
  3483. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3484. /* Request Boot Device */
  3485. (ioc->bios_pg2.ReqBootDeviceForm &
  3486. MPI2_BIOSPAGE2_FORM_MASK) ==
  3487. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
  3488. /* Alternate Request Boot Device */
  3489. (ioc->bios_pg2.ReqAltBootDeviceForm &
  3490. MPI2_BIOSPAGE2_FORM_MASK) ==
  3491. MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
  3492. return 0;
  3493. return 1;
  3494. }
  3495. /**
  3496. * _base_unmask_events - turn on notification for this event
  3497. * @ioc: per adapter object
  3498. * @event: firmware event
  3499. *
  3500. * The mask is stored in ioc->event_masks.
  3501. */
  3502. static void
  3503. _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
  3504. {
  3505. u32 desired_event;
  3506. if (event >= 128)
  3507. return;
  3508. desired_event = (1 << (event % 32));
  3509. if (event < 32)
  3510. ioc->event_masks[0] &= ~desired_event;
  3511. else if (event < 64)
  3512. ioc->event_masks[1] &= ~desired_event;
  3513. else if (event < 96)
  3514. ioc->event_masks[2] &= ~desired_event;
  3515. else if (event < 128)
  3516. ioc->event_masks[3] &= ~desired_event;
  3517. }
  3518. /**
  3519. * _base_event_notification - send event notification
  3520. * @ioc: per adapter object
  3521. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3522. *
  3523. * Returns 0 for success, non-zero for failure.
  3524. */
  3525. static int
  3526. _base_event_notification(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3527. {
  3528. Mpi2EventNotificationRequest_t *mpi_request;
  3529. unsigned long timeleft;
  3530. u16 smid;
  3531. int r = 0;
  3532. int i;
  3533. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3534. __func__));
  3535. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  3536. pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
  3537. ioc->name, __func__);
  3538. return -EAGAIN;
  3539. }
  3540. smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
  3541. if (!smid) {
  3542. pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
  3543. ioc->name, __func__);
  3544. return -EAGAIN;
  3545. }
  3546. ioc->base_cmds.status = MPT3_CMD_PENDING;
  3547. mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
  3548. ioc->base_cmds.smid = smid;
  3549. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  3550. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  3551. mpi_request->VF_ID = 0; /* TODO */
  3552. mpi_request->VP_ID = 0;
  3553. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3554. mpi_request->EventMasks[i] =
  3555. cpu_to_le32(ioc->event_masks[i]);
  3556. init_completion(&ioc->base_cmds.done);
  3557. mpt3sas_base_put_smid_default(ioc, smid);
  3558. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  3559. if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
  3560. pr_err(MPT3SAS_FMT "%s: timeout\n",
  3561. ioc->name, __func__);
  3562. _debug_dump_mf(mpi_request,
  3563. sizeof(Mpi2EventNotificationRequest_t)/4);
  3564. if (ioc->base_cmds.status & MPT3_CMD_RESET)
  3565. r = -EFAULT;
  3566. else
  3567. r = -ETIME;
  3568. } else
  3569. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
  3570. ioc->name, __func__));
  3571. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  3572. return r;
  3573. }
  3574. /**
  3575. * mpt3sas_base_validate_event_type - validating event types
  3576. * @ioc: per adapter object
  3577. * @event: firmware event
  3578. *
  3579. * This will turn on firmware event notification when application
  3580. * ask for that event. We don't mask events that are already enabled.
  3581. */
  3582. void
  3583. mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
  3584. {
  3585. int i, j;
  3586. u32 event_mask, desired_event;
  3587. u8 send_update_to_fw;
  3588. for (i = 0, send_update_to_fw = 0; i <
  3589. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  3590. event_mask = ~event_type[i];
  3591. desired_event = 1;
  3592. for (j = 0; j < 32; j++) {
  3593. if (!(event_mask & desired_event) &&
  3594. (ioc->event_masks[i] & desired_event)) {
  3595. ioc->event_masks[i] &= ~desired_event;
  3596. send_update_to_fw = 1;
  3597. }
  3598. desired_event = (desired_event << 1);
  3599. }
  3600. }
  3601. if (!send_update_to_fw)
  3602. return;
  3603. mutex_lock(&ioc->base_cmds.mutex);
  3604. _base_event_notification(ioc, CAN_SLEEP);
  3605. mutex_unlock(&ioc->base_cmds.mutex);
  3606. }
  3607. /**
  3608. * _base_diag_reset - the "big hammer" start of day reset
  3609. * @ioc: per adapter object
  3610. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3611. *
  3612. * Returns 0 for success, non-zero for failure.
  3613. */
  3614. static int
  3615. _base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3616. {
  3617. u32 host_diagnostic;
  3618. u32 ioc_state;
  3619. u32 count;
  3620. u32 hcb_size;
  3621. pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
  3622. drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
  3623. ioc->name));
  3624. count = 0;
  3625. do {
  3626. /* Write magic sequence to WriteSequence register
  3627. * Loop until in diagnostic mode
  3628. */
  3629. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3630. "write magic sequence\n", ioc->name));
  3631. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3632. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  3633. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  3634. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  3635. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3636. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3637. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3638. /* wait 100 msec */
  3639. if (sleep_flag == CAN_SLEEP)
  3640. msleep(100);
  3641. else
  3642. mdelay(100);
  3643. if (count++ > 20)
  3644. goto out;
  3645. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3646. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3647. "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
  3648. ioc->name, count, host_diagnostic));
  3649. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  3650. hcb_size = readl(&ioc->chip->HCBSize);
  3651. drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
  3652. ioc->name));
  3653. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  3654. &ioc->chip->HostDiagnostic);
  3655. /* don't access any registers for 50 milliseconds */
  3656. msleep(50);
  3657. /* 300 second max wait */
  3658. for (count = 0; count < 3000000 ; count++) {
  3659. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3660. if (host_diagnostic == 0xFFFFFFFF)
  3661. goto out;
  3662. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  3663. break;
  3664. /* wait 1 msec */
  3665. if (sleep_flag == CAN_SLEEP)
  3666. usleep_range(1000, 1500);
  3667. else
  3668. mdelay(1);
  3669. }
  3670. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  3671. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3672. "restart the adapter assuming the HCB Address points to good F/W\n",
  3673. ioc->name));
  3674. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  3675. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  3676. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  3677. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3678. "re-enable the HCDW\n", ioc->name));
  3679. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  3680. &ioc->chip->HCBSize);
  3681. }
  3682. drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
  3683. ioc->name));
  3684. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  3685. &ioc->chip->HostDiagnostic);
  3686. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3687. "disable writes to the diagnostic register\n", ioc->name));
  3688. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3689. drsprintk(ioc, pr_info(MPT3SAS_FMT
  3690. "Wait for FW to go to the READY state\n", ioc->name));
  3691. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
  3692. sleep_flag);
  3693. if (ioc_state) {
  3694. pr_err(MPT3SAS_FMT
  3695. "%s: failed going to ready state (ioc_state=0x%x)\n",
  3696. ioc->name, __func__, ioc_state);
  3697. goto out;
  3698. }
  3699. pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
  3700. return 0;
  3701. out:
  3702. pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
  3703. return -EFAULT;
  3704. }
  3705. /**
  3706. * _base_make_ioc_ready - put controller in READY state
  3707. * @ioc: per adapter object
  3708. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3709. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  3710. *
  3711. * Returns 0 for success, non-zero for failure.
  3712. */
  3713. static int
  3714. _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
  3715. enum reset_type type)
  3716. {
  3717. u32 ioc_state;
  3718. int rc;
  3719. int count;
  3720. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3721. __func__));
  3722. if (ioc->pci_error_recovery)
  3723. return 0;
  3724. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  3725. dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
  3726. ioc->name, __func__, ioc_state));
  3727. /* if in RESET state, it should move to READY state shortly */
  3728. count = 0;
  3729. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
  3730. while ((ioc_state & MPI2_IOC_STATE_MASK) !=
  3731. MPI2_IOC_STATE_READY) {
  3732. if (count++ == 10) {
  3733. pr_err(MPT3SAS_FMT
  3734. "%s: failed going to ready state (ioc_state=0x%x)\n",
  3735. ioc->name, __func__, ioc_state);
  3736. return -EFAULT;
  3737. }
  3738. if (sleep_flag == CAN_SLEEP)
  3739. ssleep(1);
  3740. else
  3741. mdelay(1000);
  3742. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  3743. }
  3744. }
  3745. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  3746. return 0;
  3747. if (ioc_state & MPI2_DOORBELL_USED) {
  3748. dhsprintk(ioc, pr_info(MPT3SAS_FMT
  3749. "unexpected doorbell active!\n",
  3750. ioc->name));
  3751. goto issue_diag_reset;
  3752. }
  3753. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  3754. mpt3sas_base_fault_info(ioc, ioc_state &
  3755. MPI2_DOORBELL_DATA_MASK);
  3756. goto issue_diag_reset;
  3757. }
  3758. if (type == FORCE_BIG_HAMMER)
  3759. goto issue_diag_reset;
  3760. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  3761. if (!(_base_send_ioc_reset(ioc,
  3762. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
  3763. return 0;
  3764. }
  3765. issue_diag_reset:
  3766. rc = _base_diag_reset(ioc, CAN_SLEEP);
  3767. return rc;
  3768. }
  3769. /**
  3770. * _base_make_ioc_operational - put controller in OPERATIONAL state
  3771. * @ioc: per adapter object
  3772. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3773. *
  3774. * Returns 0 for success, non-zero for failure.
  3775. */
  3776. static int
  3777. _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  3778. {
  3779. int r, i;
  3780. unsigned long flags;
  3781. u32 reply_address;
  3782. u16 smid;
  3783. struct _tr_list *delayed_tr, *delayed_tr_next;
  3784. struct adapter_reply_queue *reply_q;
  3785. long reply_post_free;
  3786. u32 reply_post_free_sz;
  3787. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3788. __func__));
  3789. /* clean the delayed target reset list */
  3790. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3791. &ioc->delayed_tr_list, list) {
  3792. list_del(&delayed_tr->list);
  3793. kfree(delayed_tr);
  3794. }
  3795. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3796. &ioc->delayed_tr_volume_list, list) {
  3797. list_del(&delayed_tr->list);
  3798. kfree(delayed_tr);
  3799. }
  3800. /* initialize the scsi lookup free list */
  3801. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3802. INIT_LIST_HEAD(&ioc->free_list);
  3803. smid = 1;
  3804. for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
  3805. INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
  3806. ioc->scsi_lookup[i].cb_idx = 0xFF;
  3807. ioc->scsi_lookup[i].smid = smid;
  3808. ioc->scsi_lookup[i].scmd = NULL;
  3809. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  3810. &ioc->free_list);
  3811. }
  3812. /* hi-priority queue */
  3813. INIT_LIST_HEAD(&ioc->hpr_free_list);
  3814. smid = ioc->hi_priority_smid;
  3815. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  3816. ioc->hpr_lookup[i].cb_idx = 0xFF;
  3817. ioc->hpr_lookup[i].smid = smid;
  3818. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  3819. &ioc->hpr_free_list);
  3820. }
  3821. /* internal queue */
  3822. INIT_LIST_HEAD(&ioc->internal_free_list);
  3823. smid = ioc->internal_smid;
  3824. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  3825. ioc->internal_lookup[i].cb_idx = 0xFF;
  3826. ioc->internal_lookup[i].smid = smid;
  3827. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  3828. &ioc->internal_free_list);
  3829. }
  3830. /* chain pool */
  3831. INIT_LIST_HEAD(&ioc->free_chain_list);
  3832. for (i = 0; i < ioc->chain_depth; i++)
  3833. list_add_tail(&ioc->chain_lookup[i].tracker_list,
  3834. &ioc->free_chain_list);
  3835. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3836. /* initialize Reply Free Queue */
  3837. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  3838. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  3839. ioc->reply_sz)
  3840. ioc->reply_free[i] = cpu_to_le32(reply_address);
  3841. /* initialize reply queues */
  3842. if (ioc->is_driver_loading)
  3843. _base_assign_reply_queues(ioc);
  3844. /* initialize Reply Post Free Queue */
  3845. reply_post_free = (long)ioc->reply_post_free;
  3846. reply_post_free_sz = ioc->reply_post_queue_depth *
  3847. sizeof(Mpi2DefaultReplyDescriptor_t);
  3848. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3849. reply_q->reply_post_host_index = 0;
  3850. reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
  3851. reply_post_free;
  3852. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  3853. reply_q->reply_post_free[i].Words =
  3854. cpu_to_le64(ULLONG_MAX);
  3855. if (!_base_is_controller_msix_enabled(ioc))
  3856. goto skip_init_reply_post_free_queue;
  3857. reply_post_free += reply_post_free_sz;
  3858. }
  3859. skip_init_reply_post_free_queue:
  3860. r = _base_send_ioc_init(ioc, sleep_flag);
  3861. if (r)
  3862. return r;
  3863. /* initialize reply free host index */
  3864. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  3865. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  3866. /* initialize reply post host index */
  3867. list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  3868. writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
  3869. &ioc->chip->ReplyPostHostIndex);
  3870. if (!_base_is_controller_msix_enabled(ioc))
  3871. goto skip_init_reply_post_host_index;
  3872. }
  3873. skip_init_reply_post_host_index:
  3874. _base_unmask_interrupts(ioc);
  3875. r = _base_event_notification(ioc, sleep_flag);
  3876. if (r)
  3877. return r;
  3878. if (sleep_flag == CAN_SLEEP)
  3879. _base_static_config_pages(ioc);
  3880. if (ioc->is_driver_loading) {
  3881. ioc->wait_for_discovery_to_complete =
  3882. _base_determine_wait_on_discovery(ioc);
  3883. return r; /* scan_start and scan_finished support */
  3884. }
  3885. r = _base_send_port_enable(ioc, sleep_flag);
  3886. if (r)
  3887. return r;
  3888. return r;
  3889. }
  3890. /**
  3891. * mpt3sas_base_free_resources - free resources controller resources
  3892. * @ioc: per adapter object
  3893. *
  3894. * Return nothing.
  3895. */
  3896. void
  3897. mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
  3898. {
  3899. struct pci_dev *pdev = ioc->pdev;
  3900. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3901. __func__));
  3902. _base_mask_interrupts(ioc);
  3903. ioc->shost_recovery = 1;
  3904. _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3905. ioc->shost_recovery = 0;
  3906. _base_free_irq(ioc);
  3907. _base_disable_msix(ioc);
  3908. if (ioc->chip_phys)
  3909. iounmap(ioc->chip);
  3910. ioc->chip_phys = 0;
  3911. pci_release_selected_regions(ioc->pdev, ioc->bars);
  3912. pci_disable_pcie_error_reporting(pdev);
  3913. pci_disable_device(pdev);
  3914. return;
  3915. }
  3916. /**
  3917. * mpt3sas_base_attach - attach controller instance
  3918. * @ioc: per adapter object
  3919. *
  3920. * Returns 0 for success, non-zero for failure.
  3921. */
  3922. int
  3923. mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
  3924. {
  3925. int r, i;
  3926. int cpu_id, last_cpu_id = 0;
  3927. dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  3928. __func__));
  3929. /* setup cpu_msix_table */
  3930. ioc->cpu_count = num_online_cpus();
  3931. for_each_online_cpu(cpu_id)
  3932. last_cpu_id = cpu_id;
  3933. ioc->cpu_msix_table_sz = last_cpu_id + 1;
  3934. ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
  3935. ioc->reply_queue_count = 1;
  3936. if (!ioc->cpu_msix_table) {
  3937. dfailprintk(ioc, pr_info(MPT3SAS_FMT
  3938. "allocation for cpu_msix_table failed!!!\n",
  3939. ioc->name));
  3940. r = -ENOMEM;
  3941. goto out_free_resources;
  3942. }
  3943. r = mpt3sas_base_map_resources(ioc);
  3944. if (r)
  3945. goto out_free_resources;
  3946. pci_set_drvdata(ioc->pdev, ioc->shost);
  3947. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  3948. if (r)
  3949. goto out_free_resources;
  3950. /*
  3951. * In SAS3.0,
  3952. * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
  3953. * Target Status - all require the IEEE formated scatter gather
  3954. * elements.
  3955. */
  3956. ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
  3957. ioc->build_sg = &_base_build_sg_ieee;
  3958. ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
  3959. ioc->mpi25 = 1;
  3960. ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
  3961. /*
  3962. * These function pointers for other requests that don't
  3963. * the require IEEE scatter gather elements.
  3964. *
  3965. * For example Configuration Pages and SAS IOUNIT Control don't.
  3966. */
  3967. ioc->build_sg_mpi = &_base_build_sg;
  3968. ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
  3969. r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3970. if (r)
  3971. goto out_free_resources;
  3972. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  3973. sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
  3974. if (!ioc->pfacts) {
  3975. r = -ENOMEM;
  3976. goto out_free_resources;
  3977. }
  3978. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  3979. r = _base_get_port_facts(ioc, i, CAN_SLEEP);
  3980. if (r)
  3981. goto out_free_resources;
  3982. }
  3983. r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
  3984. if (r)
  3985. goto out_free_resources;
  3986. init_waitqueue_head(&ioc->reset_wq);
  3987. /* allocate memory pd handle bitmask list */
  3988. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  3989. if (ioc->facts.MaxDevHandle % 8)
  3990. ioc->pd_handles_sz++;
  3991. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  3992. GFP_KERNEL);
  3993. if (!ioc->pd_handles) {
  3994. r = -ENOMEM;
  3995. goto out_free_resources;
  3996. }
  3997. ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
  3998. GFP_KERNEL);
  3999. if (!ioc->blocking_handles) {
  4000. r = -ENOMEM;
  4001. goto out_free_resources;
  4002. }
  4003. ioc->fwfault_debug = mpt3sas_fwfault_debug;
  4004. /* base internal command bits */
  4005. mutex_init(&ioc->base_cmds.mutex);
  4006. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4007. ioc->base_cmds.status = MPT3_CMD_NOT_USED;
  4008. /* port_enable command bits */
  4009. ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4010. ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
  4011. /* transport internal command bits */
  4012. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4013. ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
  4014. mutex_init(&ioc->transport_cmds.mutex);
  4015. /* scsih internal command bits */
  4016. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4017. ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  4018. mutex_init(&ioc->scsih_cmds.mutex);
  4019. /* task management internal command bits */
  4020. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4021. ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
  4022. mutex_init(&ioc->tm_cmds.mutex);
  4023. /* config page internal command bits */
  4024. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4025. ioc->config_cmds.status = MPT3_CMD_NOT_USED;
  4026. mutex_init(&ioc->config_cmds.mutex);
  4027. /* ctl module internal command bits */
  4028. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  4029. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  4030. ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
  4031. mutex_init(&ioc->ctl_cmds.mutex);
  4032. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  4033. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  4034. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
  4035. !ioc->ctl_cmds.sense) {
  4036. r = -ENOMEM;
  4037. goto out_free_resources;
  4038. }
  4039. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  4040. ioc->event_masks[i] = -1;
  4041. /* here we enable the events we care about */
  4042. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  4043. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  4044. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  4045. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  4046. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  4047. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  4048. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  4049. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  4050. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  4051. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  4052. r = _base_make_ioc_operational(ioc, CAN_SLEEP);
  4053. if (r)
  4054. goto out_free_resources;
  4055. return 0;
  4056. out_free_resources:
  4057. ioc->remove_host = 1;
  4058. mpt3sas_base_free_resources(ioc);
  4059. _base_release_memory_pools(ioc);
  4060. pci_set_drvdata(ioc->pdev, NULL);
  4061. kfree(ioc->cpu_msix_table);
  4062. kfree(ioc->pd_handles);
  4063. kfree(ioc->blocking_handles);
  4064. kfree(ioc->tm_cmds.reply);
  4065. kfree(ioc->transport_cmds.reply);
  4066. kfree(ioc->scsih_cmds.reply);
  4067. kfree(ioc->config_cmds.reply);
  4068. kfree(ioc->base_cmds.reply);
  4069. kfree(ioc->port_enable_cmds.reply);
  4070. kfree(ioc->ctl_cmds.reply);
  4071. kfree(ioc->ctl_cmds.sense);
  4072. kfree(ioc->pfacts);
  4073. ioc->ctl_cmds.reply = NULL;
  4074. ioc->base_cmds.reply = NULL;
  4075. ioc->tm_cmds.reply = NULL;
  4076. ioc->scsih_cmds.reply = NULL;
  4077. ioc->transport_cmds.reply = NULL;
  4078. ioc->config_cmds.reply = NULL;
  4079. ioc->pfacts = NULL;
  4080. return r;
  4081. }
  4082. /**
  4083. * mpt3sas_base_detach - remove controller instance
  4084. * @ioc: per adapter object
  4085. *
  4086. * Return nothing.
  4087. */
  4088. void
  4089. mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
  4090. {
  4091. dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
  4092. __func__));
  4093. mpt3sas_base_stop_watchdog(ioc);
  4094. mpt3sas_base_free_resources(ioc);
  4095. _base_release_memory_pools(ioc);
  4096. pci_set_drvdata(ioc->pdev, NULL);
  4097. kfree(ioc->cpu_msix_table);
  4098. kfree(ioc->pd_handles);
  4099. kfree(ioc->blocking_handles);
  4100. kfree(ioc->pfacts);
  4101. kfree(ioc->ctl_cmds.reply);
  4102. kfree(ioc->ctl_cmds.sense);
  4103. kfree(ioc->base_cmds.reply);
  4104. kfree(ioc->port_enable_cmds.reply);
  4105. kfree(ioc->tm_cmds.reply);
  4106. kfree(ioc->transport_cmds.reply);
  4107. kfree(ioc->scsih_cmds.reply);
  4108. kfree(ioc->config_cmds.reply);
  4109. }
  4110. /**
  4111. * _base_reset_handler - reset callback handler (for base)
  4112. * @ioc: per adapter object
  4113. * @reset_phase: phase
  4114. *
  4115. * The handler for doing any required cleanup or initialization.
  4116. *
  4117. * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
  4118. * MPT3_IOC_DONE_RESET
  4119. *
  4120. * Return nothing.
  4121. */
  4122. static void
  4123. _base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
  4124. {
  4125. mpt3sas_scsih_reset_handler(ioc, reset_phase);
  4126. mpt3sas_ctl_reset_handler(ioc, reset_phase);
  4127. switch (reset_phase) {
  4128. case MPT3_IOC_PRE_RESET:
  4129. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4130. "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
  4131. break;
  4132. case MPT3_IOC_AFTER_RESET:
  4133. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4134. "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
  4135. if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
  4136. ioc->transport_cmds.status |= MPT3_CMD_RESET;
  4137. mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  4138. complete(&ioc->transport_cmds.done);
  4139. }
  4140. if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
  4141. ioc->base_cmds.status |= MPT3_CMD_RESET;
  4142. mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
  4143. complete(&ioc->base_cmds.done);
  4144. }
  4145. if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
  4146. ioc->port_enable_failed = 1;
  4147. ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
  4148. mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
  4149. if (ioc->is_driver_loading) {
  4150. ioc->start_scan_failed =
  4151. MPI2_IOCSTATUS_INTERNAL_ERROR;
  4152. ioc->start_scan = 0;
  4153. ioc->port_enable_cmds.status =
  4154. MPT3_CMD_NOT_USED;
  4155. } else
  4156. complete(&ioc->port_enable_cmds.done);
  4157. }
  4158. if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
  4159. ioc->config_cmds.status |= MPT3_CMD_RESET;
  4160. mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
  4161. ioc->config_cmds.smid = USHRT_MAX;
  4162. complete(&ioc->config_cmds.done);
  4163. }
  4164. break;
  4165. case MPT3_IOC_DONE_RESET:
  4166. dtmprintk(ioc, pr_info(MPT3SAS_FMT
  4167. "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
  4168. break;
  4169. }
  4170. }
  4171. /**
  4172. * _wait_for_commands_to_complete - reset controller
  4173. * @ioc: Pointer to MPT_ADAPTER structure
  4174. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4175. *
  4176. * This function waiting(3s) for all pending commands to complete
  4177. * prior to putting controller in reset.
  4178. */
  4179. static void
  4180. _wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
  4181. {
  4182. u32 ioc_state;
  4183. unsigned long flags;
  4184. u16 i;
  4185. ioc->pending_io_count = 0;
  4186. if (sleep_flag != CAN_SLEEP)
  4187. return;
  4188. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  4189. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  4190. return;
  4191. /* pending command count */
  4192. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  4193. for (i = 0; i < ioc->scsiio_depth; i++)
  4194. if (ioc->scsi_lookup[i].cb_idx != 0xFF)
  4195. ioc->pending_io_count++;
  4196. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  4197. if (!ioc->pending_io_count)
  4198. return;
  4199. /* wait for pending commands to complete */
  4200. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  4201. }
  4202. /**
  4203. * mpt3sas_base_hard_reset_handler - reset controller
  4204. * @ioc: Pointer to MPT_ADAPTER structure
  4205. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  4206. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  4207. *
  4208. * Returns 0 for success, non-zero for failure.
  4209. */
  4210. int
  4211. mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
  4212. enum reset_type type)
  4213. {
  4214. int r;
  4215. unsigned long flags;
  4216. u32 ioc_state;
  4217. u8 is_fault = 0, is_trigger = 0;
  4218. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
  4219. __func__));
  4220. if (ioc->pci_error_recovery) {
  4221. pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
  4222. ioc->name, __func__);
  4223. r = 0;
  4224. goto out_unlocked;
  4225. }
  4226. if (mpt3sas_fwfault_debug)
  4227. mpt3sas_halt_firmware(ioc);
  4228. /* TODO - What we really should be doing is pulling
  4229. * out all the code associated with NO_SLEEP; its never used.
  4230. * That is legacy code from mpt fusion driver, ported over.
  4231. * I will leave this BUG_ON here for now till its been resolved.
  4232. */
  4233. BUG_ON(sleep_flag == NO_SLEEP);
  4234. /* wait for an active reset in progress to complete */
  4235. if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
  4236. do {
  4237. ssleep(1);
  4238. } while (ioc->shost_recovery == 1);
  4239. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
  4240. __func__));
  4241. return ioc->ioc_reset_in_progress_status;
  4242. }
  4243. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4244. ioc->shost_recovery = 1;
  4245. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4246. if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  4247. MPT3_DIAG_BUFFER_IS_REGISTERED) &&
  4248. (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
  4249. MPT3_DIAG_BUFFER_IS_RELEASED))) {
  4250. is_trigger = 1;
  4251. ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
  4252. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  4253. is_fault = 1;
  4254. }
  4255. _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
  4256. _wait_for_commands_to_complete(ioc, sleep_flag);
  4257. _base_mask_interrupts(ioc);
  4258. r = _base_make_ioc_ready(ioc, sleep_flag, type);
  4259. if (r)
  4260. goto out;
  4261. _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
  4262. /* If this hard reset is called while port enable is active, then
  4263. * there is no reason to call make_ioc_operational
  4264. */
  4265. if (ioc->is_driver_loading && ioc->port_enable_failed) {
  4266. ioc->remove_host = 1;
  4267. r = -EFAULT;
  4268. goto out;
  4269. }
  4270. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  4271. if (r)
  4272. goto out;
  4273. r = _base_make_ioc_operational(ioc, sleep_flag);
  4274. if (!r)
  4275. _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
  4276. out:
  4277. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
  4278. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  4279. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  4280. ioc->ioc_reset_in_progress_status = r;
  4281. ioc->shost_recovery = 0;
  4282. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  4283. ioc->ioc_reset_count++;
  4284. mutex_unlock(&ioc->reset_in_progress_mutex);
  4285. out_unlocked:
  4286. if ((r == 0) && is_trigger) {
  4287. if (is_fault)
  4288. mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
  4289. else
  4290. mpt3sas_trigger_master(ioc,
  4291. MASTER_TRIGGER_ADAPTER_RESET);
  4292. }
  4293. dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
  4294. __func__));
  4295. return r;
  4296. }