mpt2sas_base.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192
  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/mpt2sas/mpt2_base.c
  6. * Copyright (C) 2007-2010 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/version.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/slab.h>
  48. #include <linux/types.h>
  49. #include <linux/pci.h>
  50. #include <linux/kdev_t.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/delay.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/dma-mapping.h>
  55. #include <linux/sort.h>
  56. #include <linux/io.h>
  57. #include <linux/time.h>
  58. #include <linux/aer.h>
  59. #include "mpt2sas_base.h"
  60. static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
  61. #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
  62. static int max_queue_depth = -1;
  63. module_param(max_queue_depth, int, 0);
  64. MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
  65. static int max_sgl_entries = -1;
  66. module_param(max_sgl_entries, int, 0);
  67. MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
  68. static int msix_disable = -1;
  69. module_param(msix_disable, int, 0);
  70. MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
  71. static int missing_delay[2] = {-1, -1};
  72. module_param_array(missing_delay, int, NULL, 0);
  73. MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
  74. /* diag_buffer_enable is bitwise
  75. * bit 0 set = TRACE
  76. * bit 1 set = SNAPSHOT
  77. * bit 2 set = EXTENDED
  78. *
  79. * Either bit can be set, or both
  80. */
  81. static int diag_buffer_enable;
  82. module_param(diag_buffer_enable, int, 0);
  83. MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
  84. "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
  85. static int mpt2sas_fwfault_debug;
  86. MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
  87. "and halt firmware - (default=0)");
  88. static int disable_discovery = -1;
  89. module_param(disable_discovery, int, 0);
  90. MODULE_PARM_DESC(disable_discovery, " disable discovery ");
  91. /**
  92. * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
  93. *
  94. */
  95. static int
  96. _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
  97. {
  98. int ret = param_set_int(val, kp);
  99. struct MPT2SAS_ADAPTER *ioc;
  100. if (ret)
  101. return ret;
  102. printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
  103. list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
  104. ioc->fwfault_debug = mpt2sas_fwfault_debug;
  105. return 0;
  106. }
  107. module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
  108. param_get_int, &mpt2sas_fwfault_debug, 0644);
  109. /**
  110. * _base_fault_reset_work - workq handling ioc fault conditions
  111. * @work: input argument, used to derive ioc
  112. * Context: sleep.
  113. *
  114. * Return nothing.
  115. */
  116. static void
  117. _base_fault_reset_work(struct work_struct *work)
  118. {
  119. struct MPT2SAS_ADAPTER *ioc =
  120. container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
  121. unsigned long flags;
  122. u32 doorbell;
  123. int rc;
  124. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  125. if (ioc->shost_recovery)
  126. goto rearm_timer;
  127. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  128. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  129. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  130. rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  131. FORCE_BIG_HAMMER);
  132. printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
  133. __func__, (rc == 0) ? "success" : "failed");
  134. doorbell = mpt2sas_base_get_iocstate(ioc, 0);
  135. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  136. mpt2sas_base_fault_info(ioc, doorbell &
  137. MPI2_DOORBELL_DATA_MASK);
  138. }
  139. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  140. rearm_timer:
  141. if (ioc->fault_reset_work_q)
  142. queue_delayed_work(ioc->fault_reset_work_q,
  143. &ioc->fault_reset_work,
  144. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  145. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  146. }
  147. /**
  148. * mpt2sas_base_start_watchdog - start the fault_reset_work_q
  149. * @ioc: per adapter object
  150. * Context: sleep.
  151. *
  152. * Return nothing.
  153. */
  154. void
  155. mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
  156. {
  157. unsigned long flags;
  158. if (ioc->fault_reset_work_q)
  159. return;
  160. /* initialize fault polling */
  161. INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
  162. snprintf(ioc->fault_reset_work_q_name,
  163. sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
  164. ioc->fault_reset_work_q =
  165. create_singlethread_workqueue(ioc->fault_reset_work_q_name);
  166. if (!ioc->fault_reset_work_q) {
  167. printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
  168. ioc->name, __func__, __LINE__);
  169. return;
  170. }
  171. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  172. if (ioc->fault_reset_work_q)
  173. queue_delayed_work(ioc->fault_reset_work_q,
  174. &ioc->fault_reset_work,
  175. msecs_to_jiffies(FAULT_POLLING_INTERVAL));
  176. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  177. }
  178. /**
  179. * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
  180. * @ioc: per adapter object
  181. * Context: sleep.
  182. *
  183. * Return nothing.
  184. */
  185. void
  186. mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
  187. {
  188. unsigned long flags;
  189. struct workqueue_struct *wq;
  190. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  191. wq = ioc->fault_reset_work_q;
  192. ioc->fault_reset_work_q = NULL;
  193. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  194. if (wq) {
  195. if (!cancel_delayed_work(&ioc->fault_reset_work))
  196. flush_workqueue(wq);
  197. destroy_workqueue(wq);
  198. }
  199. }
  200. /**
  201. * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
  202. * @ioc: per adapter object
  203. * @fault_code: fault code
  204. *
  205. * Return nothing.
  206. */
  207. void
  208. mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
  209. {
  210. printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
  211. ioc->name, fault_code);
  212. }
  213. /**
  214. * mpt2sas_halt_firmware - halt's mpt controller firmware
  215. * @ioc: per adapter object
  216. *
  217. * For debugging timeout related issues. Writing 0xCOFFEE00
  218. * to the doorbell register will halt controller firmware. With
  219. * the purpose to stop both driver and firmware, the enduser can
  220. * obtain a ring buffer from controller UART.
  221. */
  222. void
  223. mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
  224. {
  225. u32 doorbell;
  226. if (!ioc->fwfault_debug)
  227. return;
  228. dump_stack();
  229. doorbell = readl(&ioc->chip->Doorbell);
  230. if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
  231. mpt2sas_base_fault_info(ioc , doorbell);
  232. else {
  233. writel(0xC0FFEE00, &ioc->chip->Doorbell);
  234. printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
  235. "timeout\n", ioc->name);
  236. }
  237. panic("panic in %s\n", __func__);
  238. }
  239. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  240. /**
  241. * _base_sas_ioc_info - verbose translation of the ioc status
  242. * @ioc: per adapter object
  243. * @mpi_reply: reply mf payload returned from firmware
  244. * @request_hdr: request mf
  245. *
  246. * Return nothing.
  247. */
  248. static void
  249. _base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
  250. MPI2RequestHeader_t *request_hdr)
  251. {
  252. u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
  253. MPI2_IOCSTATUS_MASK;
  254. char *desc = NULL;
  255. u16 frame_sz;
  256. char *func_str = NULL;
  257. /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
  258. if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
  259. request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
  260. request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  261. return;
  262. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  263. return;
  264. switch (ioc_status) {
  265. /****************************************************************************
  266. * Common IOCStatus values for all replies
  267. ****************************************************************************/
  268. case MPI2_IOCSTATUS_INVALID_FUNCTION:
  269. desc = "invalid function";
  270. break;
  271. case MPI2_IOCSTATUS_BUSY:
  272. desc = "busy";
  273. break;
  274. case MPI2_IOCSTATUS_INVALID_SGL:
  275. desc = "invalid sgl";
  276. break;
  277. case MPI2_IOCSTATUS_INTERNAL_ERROR:
  278. desc = "internal error";
  279. break;
  280. case MPI2_IOCSTATUS_INVALID_VPID:
  281. desc = "invalid vpid";
  282. break;
  283. case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  284. desc = "insufficient resources";
  285. break;
  286. case MPI2_IOCSTATUS_INVALID_FIELD:
  287. desc = "invalid field";
  288. break;
  289. case MPI2_IOCSTATUS_INVALID_STATE:
  290. desc = "invalid state";
  291. break;
  292. case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  293. desc = "op state not supported";
  294. break;
  295. /****************************************************************************
  296. * Config IOCStatus values
  297. ****************************************************************************/
  298. case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  299. desc = "config invalid action";
  300. break;
  301. case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  302. desc = "config invalid type";
  303. break;
  304. case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  305. desc = "config invalid page";
  306. break;
  307. case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  308. desc = "config invalid data";
  309. break;
  310. case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  311. desc = "config no defaults";
  312. break;
  313. case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  314. desc = "config cant commit";
  315. break;
  316. /****************************************************************************
  317. * SCSI IO Reply
  318. ****************************************************************************/
  319. case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
  320. case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
  321. case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
  322. case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
  323. case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
  324. case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
  325. case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
  326. case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
  327. case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
  328. case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
  329. case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
  330. case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  331. break;
  332. /****************************************************************************
  333. * For use by SCSI Initiator and SCSI Target end-to-end data protection
  334. ****************************************************************************/
  335. case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  336. desc = "eedp guard error";
  337. break;
  338. case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  339. desc = "eedp ref tag error";
  340. break;
  341. case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  342. desc = "eedp app tag error";
  343. break;
  344. /****************************************************************************
  345. * SCSI Target values
  346. ****************************************************************************/
  347. case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  348. desc = "target invalid io index";
  349. break;
  350. case MPI2_IOCSTATUS_TARGET_ABORTED:
  351. desc = "target aborted";
  352. break;
  353. case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  354. desc = "target no conn retryable";
  355. break;
  356. case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  357. desc = "target no connection";
  358. break;
  359. case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  360. desc = "target xfer count mismatch";
  361. break;
  362. case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  363. desc = "target data offset error";
  364. break;
  365. case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  366. desc = "target too much write data";
  367. break;
  368. case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  369. desc = "target iu too short";
  370. break;
  371. case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  372. desc = "target ack nak timeout";
  373. break;
  374. case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  375. desc = "target nak received";
  376. break;
  377. /****************************************************************************
  378. * Serial Attached SCSI values
  379. ****************************************************************************/
  380. case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  381. desc = "smp request failed";
  382. break;
  383. case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  384. desc = "smp data overrun";
  385. break;
  386. /****************************************************************************
  387. * Diagnostic Buffer Post / Diagnostic Release values
  388. ****************************************************************************/
  389. case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  390. desc = "diagnostic released";
  391. break;
  392. default:
  393. break;
  394. }
  395. if (!desc)
  396. return;
  397. switch (request_hdr->Function) {
  398. case MPI2_FUNCTION_CONFIG:
  399. frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  400. func_str = "config_page";
  401. break;
  402. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  403. frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  404. func_str = "task_mgmt";
  405. break;
  406. case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  407. frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  408. func_str = "sas_iounit_ctl";
  409. break;
  410. case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  411. frame_sz = sizeof(Mpi2SepRequest_t);
  412. func_str = "enclosure";
  413. break;
  414. case MPI2_FUNCTION_IOC_INIT:
  415. frame_sz = sizeof(Mpi2IOCInitRequest_t);
  416. func_str = "ioc_init";
  417. break;
  418. case MPI2_FUNCTION_PORT_ENABLE:
  419. frame_sz = sizeof(Mpi2PortEnableRequest_t);
  420. func_str = "port_enable";
  421. break;
  422. case MPI2_FUNCTION_SMP_PASSTHROUGH:
  423. frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  424. func_str = "smp_passthru";
  425. break;
  426. default:
  427. frame_sz = 32;
  428. func_str = "unknown";
  429. break;
  430. }
  431. printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
  432. " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
  433. _debug_dump_mf(request_hdr, frame_sz/4);
  434. }
  435. /**
  436. * _base_display_event_data - verbose translation of firmware asyn events
  437. * @ioc: per adapter object
  438. * @mpi_reply: reply mf payload returned from firmware
  439. *
  440. * Return nothing.
  441. */
  442. static void
  443. _base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
  444. Mpi2EventNotificationReply_t *mpi_reply)
  445. {
  446. char *desc = NULL;
  447. u16 event;
  448. if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  449. return;
  450. event = le16_to_cpu(mpi_reply->Event);
  451. switch (event) {
  452. case MPI2_EVENT_LOG_DATA:
  453. desc = "Log Data";
  454. break;
  455. case MPI2_EVENT_STATE_CHANGE:
  456. desc = "Status Change";
  457. break;
  458. case MPI2_EVENT_HARD_RESET_RECEIVED:
  459. desc = "Hard Reset Received";
  460. break;
  461. case MPI2_EVENT_EVENT_CHANGE:
  462. desc = "Event Change";
  463. break;
  464. case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  465. desc = "Device Status Change";
  466. break;
  467. case MPI2_EVENT_IR_OPERATION_STATUS:
  468. if (!ioc->hide_ir_msg)
  469. desc = "IR Operation Status";
  470. break;
  471. case MPI2_EVENT_SAS_DISCOVERY:
  472. {
  473. Mpi2EventDataSasDiscovery_t *event_data =
  474. (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  475. printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
  476. (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
  477. "start" : "stop");
  478. if (event_data->DiscoveryStatus)
  479. printk("discovery_status(0x%08x)",
  480. le32_to_cpu(event_data->DiscoveryStatus));
  481. printk("\n");
  482. return;
  483. }
  484. case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  485. desc = "SAS Broadcast Primitive";
  486. break;
  487. case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  488. desc = "SAS Init Device Status Change";
  489. break;
  490. case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  491. desc = "SAS Init Table Overflow";
  492. break;
  493. case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  494. desc = "SAS Topology Change List";
  495. break;
  496. case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  497. desc = "SAS Enclosure Device Status Change";
  498. break;
  499. case MPI2_EVENT_IR_VOLUME:
  500. if (!ioc->hide_ir_msg)
  501. desc = "IR Volume";
  502. break;
  503. case MPI2_EVENT_IR_PHYSICAL_DISK:
  504. if (!ioc->hide_ir_msg)
  505. desc = "IR Physical Disk";
  506. break;
  507. case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  508. if (!ioc->hide_ir_msg)
  509. desc = "IR Configuration Change List";
  510. break;
  511. case MPI2_EVENT_LOG_ENTRY_ADDED:
  512. if (!ioc->hide_ir_msg)
  513. desc = "Log Entry Added";
  514. break;
  515. }
  516. if (!desc)
  517. return;
  518. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
  519. }
  520. #endif
  521. /**
  522. * _base_sas_log_info - verbose translation of firmware log info
  523. * @ioc: per adapter object
  524. * @log_info: log info
  525. *
  526. * Return nothing.
  527. */
  528. static void
  529. _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
  530. {
  531. union loginfo_type {
  532. u32 loginfo;
  533. struct {
  534. u32 subcode:16;
  535. u32 code:8;
  536. u32 originator:4;
  537. u32 bus_type:4;
  538. } dw;
  539. };
  540. union loginfo_type sas_loginfo;
  541. char *originator_str = NULL;
  542. sas_loginfo.loginfo = log_info;
  543. if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  544. return;
  545. /* each nexus loss loginfo */
  546. if (log_info == 0x31170000)
  547. return;
  548. /* eat the loginfos associated with task aborts */
  549. if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
  550. 0x31140000 || log_info == 0x31130000))
  551. return;
  552. switch (sas_loginfo.dw.originator) {
  553. case 0:
  554. originator_str = "IOP";
  555. break;
  556. case 1:
  557. originator_str = "PL";
  558. break;
  559. case 2:
  560. if (!ioc->hide_ir_msg)
  561. originator_str = "IR";
  562. else
  563. originator_str = "WarpDrive";
  564. break;
  565. }
  566. printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
  567. "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
  568. originator_str, sas_loginfo.dw.code,
  569. sas_loginfo.dw.subcode);
  570. }
  571. /**
  572. * _base_display_reply_info -
  573. * @ioc: per adapter object
  574. * @smid: system request message index
  575. * @msix_index: MSIX table index supplied by the OS
  576. * @reply: reply message frame(lower 32bit addr)
  577. *
  578. * Return nothing.
  579. */
  580. static void
  581. _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  582. u32 reply)
  583. {
  584. MPI2DefaultReply_t *mpi_reply;
  585. u16 ioc_status;
  586. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  587. ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
  588. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  589. if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
  590. (ioc->logging_level & MPT_DEBUG_REPLY)) {
  591. _base_sas_ioc_info(ioc , mpi_reply,
  592. mpt2sas_base_get_msg_frame(ioc, smid));
  593. }
  594. #endif
  595. if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
  596. _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
  597. }
  598. /**
  599. * mpt2sas_base_done - base internal command completion routine
  600. * @ioc: per adapter object
  601. * @smid: system request message index
  602. * @msix_index: MSIX table index supplied by the OS
  603. * @reply: reply message frame(lower 32bit addr)
  604. *
  605. * Return 1 meaning mf should be freed from _base_interrupt
  606. * 0 means the mf is freed from this function.
  607. */
  608. u8
  609. mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  610. u32 reply)
  611. {
  612. MPI2DefaultReply_t *mpi_reply;
  613. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  614. if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  615. return 1;
  616. if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
  617. return 1;
  618. ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
  619. if (mpi_reply) {
  620. ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
  621. memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
  622. }
  623. ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
  624. complete(&ioc->base_cmds.done);
  625. return 1;
  626. }
  627. /**
  628. * _base_async_event - main callback handler for firmware asyn events
  629. * @ioc: per adapter object
  630. * @msix_index: MSIX table index supplied by the OS
  631. * @reply: reply message frame(lower 32bit addr)
  632. *
  633. * Return 1 meaning mf should be freed from _base_interrupt
  634. * 0 means the mf is freed from this function.
  635. */
  636. static u8
  637. _base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
  638. {
  639. Mpi2EventNotificationReply_t *mpi_reply;
  640. Mpi2EventAckRequest_t *ack_request;
  641. u16 smid;
  642. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  643. if (!mpi_reply)
  644. return 1;
  645. if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  646. return 1;
  647. #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  648. _base_display_event_data(ioc, mpi_reply);
  649. #endif
  650. if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  651. goto out;
  652. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  653. if (!smid) {
  654. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  655. ioc->name, __func__);
  656. goto out;
  657. }
  658. ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
  659. memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
  660. ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
  661. ack_request->Event = mpi_reply->Event;
  662. ack_request->EventContext = mpi_reply->EventContext;
  663. ack_request->VF_ID = 0; /* TODO */
  664. ack_request->VP_ID = 0;
  665. mpt2sas_base_put_smid_default(ioc, smid);
  666. out:
  667. /* scsih callback handler */
  668. mpt2sas_scsih_event_callback(ioc, msix_index, reply);
  669. /* ctl callback handler */
  670. mpt2sas_ctl_event_callback(ioc, msix_index, reply);
  671. return 1;
  672. }
  673. /**
  674. * _base_get_cb_idx - obtain the callback index
  675. * @ioc: per adapter object
  676. * @smid: system request message index
  677. *
  678. * Return callback index.
  679. */
  680. static u8
  681. _base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  682. {
  683. int i;
  684. u8 cb_idx;
  685. if (smid < ioc->hi_priority_smid) {
  686. i = smid - 1;
  687. cb_idx = ioc->scsi_lookup[i].cb_idx;
  688. } else if (smid < ioc->internal_smid) {
  689. i = smid - ioc->hi_priority_smid;
  690. cb_idx = ioc->hpr_lookup[i].cb_idx;
  691. } else if (smid <= ioc->hba_queue_depth) {
  692. i = smid - ioc->internal_smid;
  693. cb_idx = ioc->internal_lookup[i].cb_idx;
  694. } else
  695. cb_idx = 0xFF;
  696. return cb_idx;
  697. }
  698. /**
  699. * _base_mask_interrupts - disable interrupts
  700. * @ioc: per adapter object
  701. *
  702. * Disabling ResetIRQ, Reply and Doorbell Interrupts
  703. *
  704. * Return nothing.
  705. */
  706. static void
  707. _base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
  708. {
  709. u32 him_register;
  710. ioc->mask_interrupts = 1;
  711. him_register = readl(&ioc->chip->HostInterruptMask);
  712. him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
  713. writel(him_register, &ioc->chip->HostInterruptMask);
  714. readl(&ioc->chip->HostInterruptMask);
  715. }
  716. /**
  717. * _base_unmask_interrupts - enable interrupts
  718. * @ioc: per adapter object
  719. *
  720. * Enabling only Reply Interrupts
  721. *
  722. * Return nothing.
  723. */
  724. static void
  725. _base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
  726. {
  727. u32 him_register;
  728. him_register = readl(&ioc->chip->HostInterruptMask);
  729. him_register &= ~MPI2_HIM_RIM;
  730. writel(him_register, &ioc->chip->HostInterruptMask);
  731. ioc->mask_interrupts = 0;
  732. }
  733. union reply_descriptor {
  734. u64 word;
  735. struct {
  736. u32 low;
  737. u32 high;
  738. } u;
  739. };
  740. /**
  741. * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
  742. * @irq: irq number (not used)
  743. * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
  744. * @r: pt_regs pointer (not used)
  745. *
  746. * Return IRQ_HANDLE if processed, else IRQ_NONE.
  747. */
  748. static irqreturn_t
  749. _base_interrupt(int irq, void *bus_id)
  750. {
  751. union reply_descriptor rd;
  752. u32 completed_cmds;
  753. u8 request_desript_type;
  754. u16 smid;
  755. u8 cb_idx;
  756. u32 reply;
  757. u8 msix_index;
  758. struct MPT2SAS_ADAPTER *ioc = bus_id;
  759. Mpi2ReplyDescriptorsUnion_t *rpf;
  760. u8 rc;
  761. if (ioc->mask_interrupts)
  762. return IRQ_NONE;
  763. rpf = &ioc->reply_post_free[ioc->reply_post_host_index];
  764. request_desript_type = rpf->Default.ReplyFlags
  765. & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  766. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  767. return IRQ_NONE;
  768. completed_cmds = 0;
  769. cb_idx = 0xFF;
  770. do {
  771. rd.word = le64_to_cpu(rpf->Words);
  772. if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
  773. goto out;
  774. reply = 0;
  775. cb_idx = 0xFF;
  776. smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  777. msix_index = rpf->Default.MSIxIndex;
  778. if (request_desript_type ==
  779. MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
  780. reply = le32_to_cpu
  781. (rpf->AddressReply.ReplyFrameAddress);
  782. if (reply > ioc->reply_dma_max_address ||
  783. reply < ioc->reply_dma_min_address)
  784. reply = 0;
  785. } else if (request_desript_type ==
  786. MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
  787. goto next;
  788. else if (request_desript_type ==
  789. MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
  790. goto next;
  791. if (smid)
  792. cb_idx = _base_get_cb_idx(ioc, smid);
  793. if (smid && cb_idx != 0xFF) {
  794. rc = mpt_callbacks[cb_idx](ioc, smid, msix_index,
  795. reply);
  796. if (reply)
  797. _base_display_reply_info(ioc, smid, msix_index,
  798. reply);
  799. if (rc)
  800. mpt2sas_base_free_smid(ioc, smid);
  801. }
  802. if (!smid)
  803. _base_async_event(ioc, msix_index, reply);
  804. /* reply free queue handling */
  805. if (reply) {
  806. ioc->reply_free_host_index =
  807. (ioc->reply_free_host_index ==
  808. (ioc->reply_free_queue_depth - 1)) ?
  809. 0 : ioc->reply_free_host_index + 1;
  810. ioc->reply_free[ioc->reply_free_host_index] =
  811. cpu_to_le32(reply);
  812. wmb();
  813. writel(ioc->reply_free_host_index,
  814. &ioc->chip->ReplyFreeHostIndex);
  815. }
  816. next:
  817. rpf->Words = cpu_to_le64(ULLONG_MAX);
  818. ioc->reply_post_host_index = (ioc->reply_post_host_index ==
  819. (ioc->reply_post_queue_depth - 1)) ? 0 :
  820. ioc->reply_post_host_index + 1;
  821. request_desript_type =
  822. ioc->reply_post_free[ioc->reply_post_host_index].Default.
  823. ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  824. completed_cmds++;
  825. if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  826. goto out;
  827. if (!ioc->reply_post_host_index)
  828. rpf = ioc->reply_post_free;
  829. else
  830. rpf++;
  831. } while (1);
  832. out:
  833. if (!completed_cmds)
  834. return IRQ_NONE;
  835. wmb();
  836. writel(ioc->reply_post_host_index, &ioc->chip->ReplyPostHostIndex);
  837. return IRQ_HANDLED;
  838. }
  839. /**
  840. * mpt2sas_base_release_callback_handler - clear interrupt callback handler
  841. * @cb_idx: callback index
  842. *
  843. * Return nothing.
  844. */
  845. void
  846. mpt2sas_base_release_callback_handler(u8 cb_idx)
  847. {
  848. mpt_callbacks[cb_idx] = NULL;
  849. }
  850. /**
  851. * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
  852. * @cb_func: callback function
  853. *
  854. * Returns cb_func.
  855. */
  856. u8
  857. mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
  858. {
  859. u8 cb_idx;
  860. for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  861. if (mpt_callbacks[cb_idx] == NULL)
  862. break;
  863. mpt_callbacks[cb_idx] = cb_func;
  864. return cb_idx;
  865. }
  866. /**
  867. * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
  868. *
  869. * Return nothing.
  870. */
  871. void
  872. mpt2sas_base_initialize_callback_handler(void)
  873. {
  874. u8 cb_idx;
  875. for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  876. mpt2sas_base_release_callback_handler(cb_idx);
  877. }
  878. /**
  879. * mpt2sas_base_build_zero_len_sge - build zero length sg entry
  880. * @ioc: per adapter object
  881. * @paddr: virtual address for SGE
  882. *
  883. * Create a zero length scatter gather entry to insure the IOCs hardware has
  884. * something to use if the target device goes brain dead and tries
  885. * to send data even when none is asked for.
  886. *
  887. * Return nothing.
  888. */
  889. void
  890. mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
  891. {
  892. u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
  893. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
  894. MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
  895. MPI2_SGE_FLAGS_SHIFT);
  896. ioc->base_add_sg_single(paddr, flags_length, -1);
  897. }
  898. /**
  899. * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
  900. * @paddr: virtual address for SGE
  901. * @flags_length: SGE flags and data transfer length
  902. * @dma_addr: Physical address
  903. *
  904. * Return nothing.
  905. */
  906. static void
  907. _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  908. {
  909. Mpi2SGESimple32_t *sgel = paddr;
  910. flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
  911. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  912. sgel->FlagsLength = cpu_to_le32(flags_length);
  913. sgel->Address = cpu_to_le32(dma_addr);
  914. }
  915. /**
  916. * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
  917. * @paddr: virtual address for SGE
  918. * @flags_length: SGE flags and data transfer length
  919. * @dma_addr: Physical address
  920. *
  921. * Return nothing.
  922. */
  923. static void
  924. _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
  925. {
  926. Mpi2SGESimple64_t *sgel = paddr;
  927. flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
  928. MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
  929. sgel->FlagsLength = cpu_to_le32(flags_length);
  930. sgel->Address = cpu_to_le64(dma_addr);
  931. }
  932. #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
  933. /**
  934. * _base_config_dma_addressing - set dma addressing
  935. * @ioc: per adapter object
  936. * @pdev: PCI device struct
  937. *
  938. * Returns 0 for success, non-zero for failure.
  939. */
  940. static int
  941. _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
  942. {
  943. struct sysinfo s;
  944. char *desc = NULL;
  945. if (sizeof(dma_addr_t) > 4) {
  946. const uint64_t required_mask =
  947. dma_get_required_mask(&pdev->dev);
  948. if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
  949. DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
  950. DMA_BIT_MASK(64))) {
  951. ioc->base_add_sg_single = &_base_add_sg_single_64;
  952. ioc->sge_size = sizeof(Mpi2SGESimple64_t);
  953. desc = "64";
  954. goto out;
  955. }
  956. }
  957. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
  958. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
  959. ioc->base_add_sg_single = &_base_add_sg_single_32;
  960. ioc->sge_size = sizeof(Mpi2SGESimple32_t);
  961. desc = "32";
  962. } else
  963. return -ENODEV;
  964. out:
  965. si_meminfo(&s);
  966. printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
  967. "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
  968. return 0;
  969. }
  970. /**
  971. * _base_save_msix_table - backup msix vector table
  972. * @ioc: per adapter object
  973. *
  974. * This address an errata where diag reset clears out the table
  975. */
  976. static void
  977. _base_save_msix_table(struct MPT2SAS_ADAPTER *ioc)
  978. {
  979. int i;
  980. if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
  981. return;
  982. for (i = 0; i < ioc->msix_vector_count; i++)
  983. ioc->msix_table_backup[i] = ioc->msix_table[i];
  984. }
  985. /**
  986. * _base_restore_msix_table - this restores the msix vector table
  987. * @ioc: per adapter object
  988. *
  989. */
  990. static void
  991. _base_restore_msix_table(struct MPT2SAS_ADAPTER *ioc)
  992. {
  993. int i;
  994. if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
  995. return;
  996. for (i = 0; i < ioc->msix_vector_count; i++)
  997. ioc->msix_table[i] = ioc->msix_table_backup[i];
  998. }
  999. /**
  1000. * _base_check_enable_msix - checks MSIX capabable.
  1001. * @ioc: per adapter object
  1002. *
  1003. * Check to see if card is capable of MSIX, and set number
  1004. * of available msix vectors
  1005. */
  1006. static int
  1007. _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  1008. {
  1009. int base;
  1010. u16 message_control;
  1011. u32 msix_table_offset;
  1012. base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
  1013. if (!base) {
  1014. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
  1015. "supported\n", ioc->name));
  1016. return -EINVAL;
  1017. }
  1018. /* get msix vector count */
  1019. pci_read_config_word(ioc->pdev, base + 2, &message_control);
  1020. ioc->msix_vector_count = (message_control & 0x3FF) + 1;
  1021. /* get msix table */
  1022. pci_read_config_dword(ioc->pdev, base + 4, &msix_table_offset);
  1023. msix_table_offset &= 0xFFFFFFF8;
  1024. ioc->msix_table = (u32 *)((void *)ioc->chip + msix_table_offset);
  1025. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
  1026. "vector_count(%d), table_offset(0x%08x), table(%p)\n", ioc->name,
  1027. ioc->msix_vector_count, msix_table_offset, ioc->msix_table));
  1028. return 0;
  1029. }
  1030. /**
  1031. * _base_disable_msix - disables msix
  1032. * @ioc: per adapter object
  1033. *
  1034. */
  1035. static void
  1036. _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
  1037. {
  1038. if (ioc->msix_enable) {
  1039. pci_disable_msix(ioc->pdev);
  1040. kfree(ioc->msix_table_backup);
  1041. ioc->msix_table_backup = NULL;
  1042. ioc->msix_enable = 0;
  1043. }
  1044. }
  1045. /**
  1046. * _base_enable_msix - enables msix, failback to io_apic
  1047. * @ioc: per adapter object
  1048. *
  1049. */
  1050. static int
  1051. _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
  1052. {
  1053. struct msix_entry entries;
  1054. int r;
  1055. u8 try_msix = 0;
  1056. if (msix_disable == -1 || msix_disable == 0)
  1057. try_msix = 1;
  1058. if (!try_msix)
  1059. goto try_ioapic;
  1060. if (_base_check_enable_msix(ioc) != 0)
  1061. goto try_ioapic;
  1062. ioc->msix_table_backup = kcalloc(ioc->msix_vector_count,
  1063. sizeof(u32), GFP_KERNEL);
  1064. if (!ioc->msix_table_backup) {
  1065. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
  1066. "msix_table_backup failed!!!\n", ioc->name));
  1067. goto try_ioapic;
  1068. }
  1069. memset(&entries, 0, sizeof(struct msix_entry));
  1070. r = pci_enable_msix(ioc->pdev, &entries, 1);
  1071. if (r) {
  1072. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
  1073. "failed (r=%d) !!!\n", ioc->name, r));
  1074. goto try_ioapic;
  1075. }
  1076. r = request_irq(entries.vector, _base_interrupt, IRQF_SHARED,
  1077. ioc->name, ioc);
  1078. if (r) {
  1079. dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "unable to allocate "
  1080. "interrupt %d !!!\n", ioc->name, entries.vector));
  1081. pci_disable_msix(ioc->pdev);
  1082. goto try_ioapic;
  1083. }
  1084. ioc->pci_irq = entries.vector;
  1085. ioc->msix_enable = 1;
  1086. return 0;
  1087. /* failback to io_apic interrupt routing */
  1088. try_ioapic:
  1089. r = request_irq(ioc->pdev->irq, _base_interrupt, IRQF_SHARED,
  1090. ioc->name, ioc);
  1091. if (r) {
  1092. printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
  1093. ioc->name, ioc->pdev->irq);
  1094. r = -EBUSY;
  1095. goto out_fail;
  1096. }
  1097. ioc->pci_irq = ioc->pdev->irq;
  1098. return 0;
  1099. out_fail:
  1100. return r;
  1101. }
  1102. /**
  1103. * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
  1104. * @ioc: per adapter object
  1105. *
  1106. * Returns 0 for success, non-zero for failure.
  1107. */
  1108. int
  1109. mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
  1110. {
  1111. struct pci_dev *pdev = ioc->pdev;
  1112. u32 memap_sz;
  1113. u32 pio_sz;
  1114. int i, r = 0;
  1115. u64 pio_chip = 0;
  1116. u64 chip_phys = 0;
  1117. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
  1118. ioc->name, __func__));
  1119. ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1120. if (pci_enable_device_mem(pdev)) {
  1121. printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
  1122. "failed\n", ioc->name);
  1123. return -ENODEV;
  1124. }
  1125. if (pci_request_selected_regions(pdev, ioc->bars,
  1126. MPT2SAS_DRIVER_NAME)) {
  1127. printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
  1128. "failed\n", ioc->name);
  1129. r = -ENODEV;
  1130. goto out_fail;
  1131. }
  1132. /* AER (Advanced Error Reporting) hooks */
  1133. pci_enable_pcie_error_reporting(pdev);
  1134. pci_set_master(pdev);
  1135. if (_base_config_dma_addressing(ioc, pdev) != 0) {
  1136. printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
  1137. ioc->name, pci_name(pdev));
  1138. r = -ENODEV;
  1139. goto out_fail;
  1140. }
  1141. for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
  1142. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  1143. if (pio_sz)
  1144. continue;
  1145. pio_chip = (u64)pci_resource_start(pdev, i);
  1146. pio_sz = pci_resource_len(pdev, i);
  1147. } else {
  1148. if (memap_sz)
  1149. continue;
  1150. /* verify memory resource is valid before using */
  1151. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  1152. ioc->chip_phys = pci_resource_start(pdev, i);
  1153. chip_phys = (u64)ioc->chip_phys;
  1154. memap_sz = pci_resource_len(pdev, i);
  1155. ioc->chip = ioremap(ioc->chip_phys, memap_sz);
  1156. if (ioc->chip == NULL) {
  1157. printk(MPT2SAS_ERR_FMT "unable to map "
  1158. "adapter memory!\n", ioc->name);
  1159. r = -EINVAL;
  1160. goto out_fail;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. _base_mask_interrupts(ioc);
  1166. r = _base_enable_msix(ioc);
  1167. if (r)
  1168. goto out_fail;
  1169. printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
  1170. ioc->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
  1171. "IO-APIC enabled"), ioc->pci_irq);
  1172. printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
  1173. ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
  1174. printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
  1175. ioc->name, (unsigned long long)pio_chip, pio_sz);
  1176. /* Save PCI configuration state for recovery from PCI AER/EEH errors */
  1177. pci_save_state(pdev);
  1178. return 0;
  1179. out_fail:
  1180. if (ioc->chip_phys)
  1181. iounmap(ioc->chip);
  1182. ioc->chip_phys = 0;
  1183. ioc->pci_irq = -1;
  1184. pci_release_selected_regions(ioc->pdev, ioc->bars);
  1185. pci_disable_pcie_error_reporting(pdev);
  1186. pci_disable_device(pdev);
  1187. return r;
  1188. }
  1189. /**
  1190. * mpt2sas_base_get_msg_frame - obtain request mf pointer
  1191. * @ioc: per adapter object
  1192. * @smid: system request message index(smid zero is invalid)
  1193. *
  1194. * Returns virt pointer to message frame.
  1195. */
  1196. void *
  1197. mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1198. {
  1199. return (void *)(ioc->request + (smid * ioc->request_sz));
  1200. }
  1201. /**
  1202. * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
  1203. * @ioc: per adapter object
  1204. * @smid: system request message index
  1205. *
  1206. * Returns virt pointer to sense buffer.
  1207. */
  1208. void *
  1209. mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1210. {
  1211. return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1212. }
  1213. /**
  1214. * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
  1215. * @ioc: per adapter object
  1216. * @smid: system request message index
  1217. *
  1218. * Returns phys pointer to the low 32bit address of the sense buffer.
  1219. */
  1220. __le32
  1221. mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1222. {
  1223. return cpu_to_le32(ioc->sense_dma +
  1224. ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
  1225. }
  1226. /**
  1227. * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
  1228. * @ioc: per adapter object
  1229. * @phys_addr: lower 32 physical addr of the reply
  1230. *
  1231. * Converts 32bit lower physical addr into a virt address.
  1232. */
  1233. void *
  1234. mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
  1235. {
  1236. if (!phys_addr)
  1237. return NULL;
  1238. return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
  1239. }
  1240. /**
  1241. * mpt2sas_base_get_smid - obtain a free smid from internal queue
  1242. * @ioc: per adapter object
  1243. * @cb_idx: callback index
  1244. *
  1245. * Returns smid (zero is invalid)
  1246. */
  1247. u16
  1248. mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
  1249. {
  1250. unsigned long flags;
  1251. struct request_tracker *request;
  1252. u16 smid;
  1253. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1254. if (list_empty(&ioc->internal_free_list)) {
  1255. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1256. printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
  1257. ioc->name, __func__);
  1258. return 0;
  1259. }
  1260. request = list_entry(ioc->internal_free_list.next,
  1261. struct request_tracker, tracker_list);
  1262. request->cb_idx = cb_idx;
  1263. smid = request->smid;
  1264. list_del(&request->tracker_list);
  1265. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1266. return smid;
  1267. }
  1268. /**
  1269. * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
  1270. * @ioc: per adapter object
  1271. * @cb_idx: callback index
  1272. * @scmd: pointer to scsi command object
  1273. *
  1274. * Returns smid (zero is invalid)
  1275. */
  1276. u16
  1277. mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
  1278. struct scsi_cmnd *scmd)
  1279. {
  1280. unsigned long flags;
  1281. struct scsiio_tracker *request;
  1282. u16 smid;
  1283. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1284. if (list_empty(&ioc->free_list)) {
  1285. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1286. printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
  1287. ioc->name, __func__);
  1288. return 0;
  1289. }
  1290. request = list_entry(ioc->free_list.next,
  1291. struct scsiio_tracker, tracker_list);
  1292. request->scmd = scmd;
  1293. request->cb_idx = cb_idx;
  1294. smid = request->smid;
  1295. list_del(&request->tracker_list);
  1296. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1297. return smid;
  1298. }
  1299. /**
  1300. * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
  1301. * @ioc: per adapter object
  1302. * @cb_idx: callback index
  1303. *
  1304. * Returns smid (zero is invalid)
  1305. */
  1306. u16
  1307. mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
  1308. {
  1309. unsigned long flags;
  1310. struct request_tracker *request;
  1311. u16 smid;
  1312. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1313. if (list_empty(&ioc->hpr_free_list)) {
  1314. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1315. return 0;
  1316. }
  1317. request = list_entry(ioc->hpr_free_list.next,
  1318. struct request_tracker, tracker_list);
  1319. request->cb_idx = cb_idx;
  1320. smid = request->smid;
  1321. list_del(&request->tracker_list);
  1322. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1323. return smid;
  1324. }
  1325. /**
  1326. * mpt2sas_base_free_smid - put smid back on free_list
  1327. * @ioc: per adapter object
  1328. * @smid: system request message index
  1329. *
  1330. * Return nothing.
  1331. */
  1332. void
  1333. mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1334. {
  1335. unsigned long flags;
  1336. int i;
  1337. struct chain_tracker *chain_req, *next;
  1338. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  1339. if (smid < ioc->hi_priority_smid) {
  1340. /* scsiio queue */
  1341. i = smid - 1;
  1342. if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
  1343. list_for_each_entry_safe(chain_req, next,
  1344. &ioc->scsi_lookup[i].chain_list, tracker_list) {
  1345. list_del_init(&chain_req->tracker_list);
  1346. list_add_tail(&chain_req->tracker_list,
  1347. &ioc->free_chain_list);
  1348. }
  1349. }
  1350. ioc->scsi_lookup[i].cb_idx = 0xFF;
  1351. ioc->scsi_lookup[i].scmd = NULL;
  1352. ioc->scsi_lookup[i].direct_io = 0;
  1353. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  1354. &ioc->free_list);
  1355. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1356. /*
  1357. * See _wait_for_commands_to_complete() call with regards
  1358. * to this code.
  1359. */
  1360. if (ioc->shost_recovery && ioc->pending_io_count) {
  1361. if (ioc->pending_io_count == 1)
  1362. wake_up(&ioc->reset_wq);
  1363. ioc->pending_io_count--;
  1364. }
  1365. return;
  1366. } else if (smid < ioc->internal_smid) {
  1367. /* hi-priority */
  1368. i = smid - ioc->hi_priority_smid;
  1369. ioc->hpr_lookup[i].cb_idx = 0xFF;
  1370. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  1371. &ioc->hpr_free_list);
  1372. } else if (smid <= ioc->hba_queue_depth) {
  1373. /* internal queue */
  1374. i = smid - ioc->internal_smid;
  1375. ioc->internal_lookup[i].cb_idx = 0xFF;
  1376. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  1377. &ioc->internal_free_list);
  1378. }
  1379. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  1380. }
  1381. /**
  1382. * _base_writeq - 64 bit write to MMIO
  1383. * @ioc: per adapter object
  1384. * @b: data payload
  1385. * @addr: address in MMIO space
  1386. * @writeq_lock: spin lock
  1387. *
  1388. * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
  1389. * care of 32 bit environment where its not quarenteed to send the entire word
  1390. * in one transfer.
  1391. */
  1392. #ifndef writeq
  1393. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1394. spinlock_t *writeq_lock)
  1395. {
  1396. unsigned long flags;
  1397. __u64 data_out = cpu_to_le64(b);
  1398. spin_lock_irqsave(writeq_lock, flags);
  1399. writel((u32)(data_out), addr);
  1400. writel((u32)(data_out >> 32), (addr + 4));
  1401. spin_unlock_irqrestore(writeq_lock, flags);
  1402. }
  1403. #else
  1404. static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
  1405. spinlock_t *writeq_lock)
  1406. {
  1407. writeq(cpu_to_le64(b), addr);
  1408. }
  1409. #endif
  1410. /**
  1411. * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
  1412. * @ioc: per adapter object
  1413. * @smid: system request message index
  1414. * @handle: device handle
  1415. *
  1416. * Return nothing.
  1417. */
  1418. void
  1419. mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
  1420. {
  1421. Mpi2RequestDescriptorUnion_t descriptor;
  1422. u64 *request = (u64 *)&descriptor;
  1423. descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
  1424. descriptor.SCSIIO.MSIxIndex = 0; /* TODO */
  1425. descriptor.SCSIIO.SMID = cpu_to_le16(smid);
  1426. descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
  1427. descriptor.SCSIIO.LMID = 0;
  1428. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1429. &ioc->scsi_lookup_lock);
  1430. }
  1431. /**
  1432. * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
  1433. * @ioc: per adapter object
  1434. * @smid: system request message index
  1435. *
  1436. * Return nothing.
  1437. */
  1438. void
  1439. mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1440. {
  1441. Mpi2RequestDescriptorUnion_t descriptor;
  1442. u64 *request = (u64 *)&descriptor;
  1443. descriptor.HighPriority.RequestFlags =
  1444. MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
  1445. descriptor.HighPriority.MSIxIndex = 0; /* TODO */
  1446. descriptor.HighPriority.SMID = cpu_to_le16(smid);
  1447. descriptor.HighPriority.LMID = 0;
  1448. descriptor.HighPriority.Reserved1 = 0;
  1449. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1450. &ioc->scsi_lookup_lock);
  1451. }
  1452. /**
  1453. * mpt2sas_base_put_smid_default - Default, primarily used for config pages
  1454. * @ioc: per adapter object
  1455. * @smid: system request message index
  1456. *
  1457. * Return nothing.
  1458. */
  1459. void
  1460. mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
  1461. {
  1462. Mpi2RequestDescriptorUnion_t descriptor;
  1463. u64 *request = (u64 *)&descriptor;
  1464. descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
  1465. descriptor.Default.MSIxIndex = 0; /* TODO */
  1466. descriptor.Default.SMID = cpu_to_le16(smid);
  1467. descriptor.Default.LMID = 0;
  1468. descriptor.Default.DescriptorTypeDependent = 0;
  1469. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1470. &ioc->scsi_lookup_lock);
  1471. }
  1472. /**
  1473. * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
  1474. * @ioc: per adapter object
  1475. * @smid: system request message index
  1476. * @io_index: value used to track the IO
  1477. *
  1478. * Return nothing.
  1479. */
  1480. void
  1481. mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
  1482. u16 io_index)
  1483. {
  1484. Mpi2RequestDescriptorUnion_t descriptor;
  1485. u64 *request = (u64 *)&descriptor;
  1486. descriptor.SCSITarget.RequestFlags =
  1487. MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
  1488. descriptor.SCSITarget.MSIxIndex = 0; /* TODO */
  1489. descriptor.SCSITarget.SMID = cpu_to_le16(smid);
  1490. descriptor.SCSITarget.LMID = 0;
  1491. descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
  1492. _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
  1493. &ioc->scsi_lookup_lock);
  1494. }
  1495. /**
  1496. * _base_display_dell_branding - Disply branding string
  1497. * @ioc: per adapter object
  1498. *
  1499. * Return nothing.
  1500. */
  1501. static void
  1502. _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
  1503. {
  1504. char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
  1505. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
  1506. return;
  1507. memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
  1508. switch (ioc->pdev->subsystem_device) {
  1509. case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
  1510. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
  1511. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1512. break;
  1513. case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
  1514. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
  1515. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1516. break;
  1517. case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
  1518. strncpy(dell_branding,
  1519. MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
  1520. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1521. break;
  1522. case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
  1523. strncpy(dell_branding,
  1524. MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
  1525. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1526. break;
  1527. case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
  1528. strncpy(dell_branding,
  1529. MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
  1530. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1531. break;
  1532. case MPT2SAS_DELL_PERC_H200_SSDID:
  1533. strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
  1534. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1535. break;
  1536. case MPT2SAS_DELL_6GBPS_SAS_SSDID:
  1537. strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
  1538. MPT2SAS_DELL_BRANDING_SIZE - 1);
  1539. break;
  1540. default:
  1541. sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
  1542. break;
  1543. }
  1544. printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
  1545. " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
  1546. ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
  1547. ioc->pdev->subsystem_device);
  1548. }
  1549. /**
  1550. * _base_display_intel_branding - Display branding string
  1551. * @ioc: per adapter object
  1552. *
  1553. * Return nothing.
  1554. */
  1555. static void
  1556. _base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
  1557. {
  1558. if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  1559. return;
  1560. switch (ioc->pdev->device) {
  1561. case MPI2_MFGPAGE_DEVID_SAS2008:
  1562. switch (ioc->pdev->subsystem_device) {
  1563. case MPT2SAS_INTEL_RMS2LL080_SSDID:
  1564. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1565. MPT2SAS_INTEL_RMS2LL080_BRANDING);
  1566. break;
  1567. case MPT2SAS_INTEL_RMS2LL040_SSDID:
  1568. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1569. MPT2SAS_INTEL_RMS2LL040_BRANDING);
  1570. break;
  1571. default:
  1572. break;
  1573. }
  1574. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  1575. switch (ioc->pdev->subsystem_device) {
  1576. case MPT2SAS_INTEL_RS25GB008_SSDID:
  1577. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1578. MPT2SAS_INTEL_RS25GB008_BRANDING);
  1579. break;
  1580. default:
  1581. break;
  1582. }
  1583. default:
  1584. break;
  1585. }
  1586. }
  1587. /**
  1588. * _base_display_hp_branding - Display branding string
  1589. * @ioc: per adapter object
  1590. *
  1591. * Return nothing.
  1592. */
  1593. static void
  1594. _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
  1595. {
  1596. if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
  1597. return;
  1598. switch (ioc->pdev->device) {
  1599. case MPI2_MFGPAGE_DEVID_SAS2004:
  1600. switch (ioc->pdev->subsystem_device) {
  1601. case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
  1602. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1603. MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
  1604. break;
  1605. default:
  1606. break;
  1607. }
  1608. case MPI2_MFGPAGE_DEVID_SAS2308_2:
  1609. switch (ioc->pdev->subsystem_device) {
  1610. case MPT2SAS_HP_2_4_INTERNAL_SSDID:
  1611. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1612. MPT2SAS_HP_2_4_INTERNAL_BRANDING);
  1613. break;
  1614. case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
  1615. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1616. MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
  1617. break;
  1618. case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
  1619. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1620. MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
  1621. break;
  1622. case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
  1623. printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1624. MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
  1625. break;
  1626. default:
  1627. break;
  1628. }
  1629. default:
  1630. break;
  1631. }
  1632. }
  1633. /**
  1634. * _base_display_ioc_capabilities - Disply IOC's capabilities.
  1635. * @ioc: per adapter object
  1636. *
  1637. * Return nothing.
  1638. */
  1639. static void
  1640. _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
  1641. {
  1642. int i = 0;
  1643. char desc[16];
  1644. u8 revision;
  1645. u32 iounit_pg1_flags;
  1646. u32 bios_version;
  1647. bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
  1648. pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
  1649. strncpy(desc, ioc->manu_pg0.ChipName, 16);
  1650. printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
  1651. "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
  1652. ioc->name, desc,
  1653. (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
  1654. (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
  1655. (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
  1656. ioc->facts.FWVersion.Word & 0x000000FF,
  1657. revision,
  1658. (bios_version & 0xFF000000) >> 24,
  1659. (bios_version & 0x00FF0000) >> 16,
  1660. (bios_version & 0x0000FF00) >> 8,
  1661. bios_version & 0x000000FF);
  1662. _base_display_dell_branding(ioc);
  1663. _base_display_intel_branding(ioc);
  1664. _base_display_hp_branding(ioc);
  1665. printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
  1666. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
  1667. printk("Initiator");
  1668. i++;
  1669. }
  1670. if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
  1671. printk("%sTarget", i ? "," : "");
  1672. i++;
  1673. }
  1674. i = 0;
  1675. printk("), ");
  1676. printk("Capabilities=(");
  1677. if (!ioc->hide_ir_msg) {
  1678. if (ioc->facts.IOCCapabilities &
  1679. MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
  1680. printk("Raid");
  1681. i++;
  1682. }
  1683. }
  1684. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
  1685. printk("%sTLR", i ? "," : "");
  1686. i++;
  1687. }
  1688. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
  1689. printk("%sMulticast", i ? "," : "");
  1690. i++;
  1691. }
  1692. if (ioc->facts.IOCCapabilities &
  1693. MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
  1694. printk("%sBIDI Target", i ? "," : "");
  1695. i++;
  1696. }
  1697. if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
  1698. printk("%sEEDP", i ? "," : "");
  1699. i++;
  1700. }
  1701. if (ioc->facts.IOCCapabilities &
  1702. MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
  1703. printk("%sSnapshot Buffer", i ? "," : "");
  1704. i++;
  1705. }
  1706. if (ioc->facts.IOCCapabilities &
  1707. MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
  1708. printk("%sDiag Trace Buffer", i ? "," : "");
  1709. i++;
  1710. }
  1711. if (ioc->facts.IOCCapabilities &
  1712. MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
  1713. printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
  1714. i++;
  1715. }
  1716. if (ioc->facts.IOCCapabilities &
  1717. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
  1718. printk("%sTask Set Full", i ? "," : "");
  1719. i++;
  1720. }
  1721. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  1722. if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
  1723. printk("%sNCQ", i ? "," : "");
  1724. i++;
  1725. }
  1726. printk(")\n");
  1727. }
  1728. /**
  1729. * _base_update_missing_delay - change the missing delay timers
  1730. * @ioc: per adapter object
  1731. * @device_missing_delay: amount of time till device is reported missing
  1732. * @io_missing_delay: interval IO is returned when there is a missing device
  1733. *
  1734. * Return nothing.
  1735. *
  1736. * Passed on the command line, this function will modify the device missing
  1737. * delay, as well as the io missing delay. This should be called at driver
  1738. * load time.
  1739. */
  1740. static void
  1741. _base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
  1742. u16 device_missing_delay, u8 io_missing_delay)
  1743. {
  1744. u16 dmd, dmd_new, dmd_orignal;
  1745. u8 io_missing_delay_original;
  1746. u16 sz;
  1747. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1748. Mpi2ConfigReply_t mpi_reply;
  1749. u8 num_phys = 0;
  1750. u16 ioc_status;
  1751. mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
  1752. if (!num_phys)
  1753. return;
  1754. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
  1755. sizeof(Mpi2SasIOUnit1PhyData_t));
  1756. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1757. if (!sas_iounit_pg1) {
  1758. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1759. ioc->name, __FILE__, __LINE__, __func__);
  1760. goto out;
  1761. }
  1762. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1763. sas_iounit_pg1, sz))) {
  1764. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1765. ioc->name, __FILE__, __LINE__, __func__);
  1766. goto out;
  1767. }
  1768. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1769. MPI2_IOCSTATUS_MASK;
  1770. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1771. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1772. ioc->name, __FILE__, __LINE__, __func__);
  1773. goto out;
  1774. }
  1775. /* device missing delay */
  1776. dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
  1777. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  1778. dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  1779. else
  1780. dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  1781. dmd_orignal = dmd;
  1782. if (device_missing_delay > 0x7F) {
  1783. dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
  1784. device_missing_delay;
  1785. dmd = dmd / 16;
  1786. dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
  1787. } else
  1788. dmd = device_missing_delay;
  1789. sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
  1790. /* io missing delay */
  1791. io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
  1792. sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
  1793. if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1794. sz)) {
  1795. if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
  1796. dmd_new = (dmd &
  1797. MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
  1798. else
  1799. dmd_new =
  1800. dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
  1801. printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
  1802. "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
  1803. printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
  1804. "new(%d)\n", ioc->name, io_missing_delay_original,
  1805. io_missing_delay);
  1806. ioc->device_missing_delay = dmd_new;
  1807. ioc->io_missing_delay = io_missing_delay;
  1808. }
  1809. out:
  1810. kfree(sas_iounit_pg1);
  1811. }
  1812. /**
  1813. * _base_static_config_pages - static start of day config pages
  1814. * @ioc: per adapter object
  1815. *
  1816. * Return nothing.
  1817. */
  1818. static void
  1819. _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
  1820. {
  1821. Mpi2ConfigReply_t mpi_reply;
  1822. u32 iounit_pg1_flags;
  1823. mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
  1824. if (ioc->ir_firmware)
  1825. mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
  1826. &ioc->manu_pg10);
  1827. mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
  1828. mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
  1829. mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
  1830. mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
  1831. mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  1832. _base_display_ioc_capabilities(ioc);
  1833. /*
  1834. * Enable task_set_full handling in iounit_pg1 when the
  1835. * facts capabilities indicate that its supported.
  1836. */
  1837. iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
  1838. if ((ioc->facts.IOCCapabilities &
  1839. MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
  1840. iounit_pg1_flags &=
  1841. ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  1842. else
  1843. iounit_pg1_flags |=
  1844. MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
  1845. ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
  1846. mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
  1847. }
  1848. /**
  1849. * _base_release_memory_pools - release memory
  1850. * @ioc: per adapter object
  1851. *
  1852. * Free memory allocated from _base_allocate_memory_pools.
  1853. *
  1854. * Return nothing.
  1855. */
  1856. static void
  1857. _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
  1858. {
  1859. int i;
  1860. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1861. __func__));
  1862. if (ioc->request) {
  1863. pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
  1864. ioc->request, ioc->request_dma);
  1865. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
  1866. ": free\n", ioc->name, ioc->request));
  1867. ioc->request = NULL;
  1868. }
  1869. if (ioc->sense) {
  1870. pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
  1871. if (ioc->sense_dma_pool)
  1872. pci_pool_destroy(ioc->sense_dma_pool);
  1873. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
  1874. ": free\n", ioc->name, ioc->sense));
  1875. ioc->sense = NULL;
  1876. }
  1877. if (ioc->reply) {
  1878. pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
  1879. if (ioc->reply_dma_pool)
  1880. pci_pool_destroy(ioc->reply_dma_pool);
  1881. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
  1882. ": free\n", ioc->name, ioc->reply));
  1883. ioc->reply = NULL;
  1884. }
  1885. if (ioc->reply_free) {
  1886. pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
  1887. ioc->reply_free_dma);
  1888. if (ioc->reply_free_dma_pool)
  1889. pci_pool_destroy(ioc->reply_free_dma_pool);
  1890. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
  1891. "(0x%p): free\n", ioc->name, ioc->reply_free));
  1892. ioc->reply_free = NULL;
  1893. }
  1894. if (ioc->reply_post_free) {
  1895. pci_pool_free(ioc->reply_post_free_dma_pool,
  1896. ioc->reply_post_free, ioc->reply_post_free_dma);
  1897. if (ioc->reply_post_free_dma_pool)
  1898. pci_pool_destroy(ioc->reply_post_free_dma_pool);
  1899. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  1900. "reply_post_free_pool(0x%p): free\n", ioc->name,
  1901. ioc->reply_post_free));
  1902. ioc->reply_post_free = NULL;
  1903. }
  1904. if (ioc->config_page) {
  1905. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
  1906. "config_page(0x%p): free\n", ioc->name,
  1907. ioc->config_page));
  1908. pci_free_consistent(ioc->pdev, ioc->config_page_sz,
  1909. ioc->config_page, ioc->config_page_dma);
  1910. }
  1911. if (ioc->scsi_lookup) {
  1912. free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
  1913. ioc->scsi_lookup = NULL;
  1914. }
  1915. kfree(ioc->hpr_lookup);
  1916. kfree(ioc->internal_lookup);
  1917. if (ioc->chain_lookup) {
  1918. for (i = 0; i < ioc->chain_depth; i++) {
  1919. if (ioc->chain_lookup[i].chain_buffer)
  1920. pci_pool_free(ioc->chain_dma_pool,
  1921. ioc->chain_lookup[i].chain_buffer,
  1922. ioc->chain_lookup[i].chain_buffer_dma);
  1923. }
  1924. if (ioc->chain_dma_pool)
  1925. pci_pool_destroy(ioc->chain_dma_pool);
  1926. }
  1927. if (ioc->chain_lookup) {
  1928. free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
  1929. ioc->chain_lookup = NULL;
  1930. }
  1931. }
  1932. /**
  1933. * _base_allocate_memory_pools - allocate start of day memory pools
  1934. * @ioc: per adapter object
  1935. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  1936. *
  1937. * Returns 0 success, anything else error
  1938. */
  1939. static int
  1940. _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  1941. {
  1942. struct mpt2sas_facts *facts;
  1943. u32 queue_size, queue_diff;
  1944. u16 max_sge_elements;
  1945. u16 num_of_reply_frames;
  1946. u16 chains_needed_per_io;
  1947. u32 sz, total_sz;
  1948. u32 retry_sz;
  1949. u16 max_request_credit;
  1950. int i;
  1951. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  1952. __func__));
  1953. retry_sz = 0;
  1954. facts = &ioc->facts;
  1955. /* command line tunables for max sgl entries */
  1956. if (max_sgl_entries != -1) {
  1957. ioc->shost->sg_tablesize = (max_sgl_entries <
  1958. MPT2SAS_SG_DEPTH) ? max_sgl_entries :
  1959. MPT2SAS_SG_DEPTH;
  1960. } else {
  1961. ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
  1962. }
  1963. /* command line tunables for max controller queue depth */
  1964. if (max_queue_depth != -1)
  1965. max_request_credit = (max_queue_depth < facts->RequestCredit)
  1966. ? max_queue_depth : facts->RequestCredit;
  1967. else
  1968. max_request_credit = facts->RequestCredit;
  1969. ioc->hba_queue_depth = max_request_credit;
  1970. ioc->hi_priority_depth = facts->HighPriorityCredit;
  1971. ioc->internal_depth = ioc->hi_priority_depth + 5;
  1972. /* request frame size */
  1973. ioc->request_sz = facts->IOCRequestFrameSize * 4;
  1974. /* reply frame size */
  1975. ioc->reply_sz = facts->ReplyFrameSize * 4;
  1976. retry_allocation:
  1977. total_sz = 0;
  1978. /* calculate number of sg elements left over in the 1st frame */
  1979. max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
  1980. sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
  1981. ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
  1982. /* now do the same for a chain buffer */
  1983. max_sge_elements = ioc->request_sz - ioc->sge_size;
  1984. ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
  1985. ioc->chain_offset_value_for_main_message =
  1986. ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
  1987. (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
  1988. /*
  1989. * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
  1990. */
  1991. chains_needed_per_io = ((ioc->shost->sg_tablesize -
  1992. ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
  1993. + 1;
  1994. if (chains_needed_per_io > facts->MaxChainDepth) {
  1995. chains_needed_per_io = facts->MaxChainDepth;
  1996. ioc->shost->sg_tablesize = min_t(u16,
  1997. ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
  1998. * chains_needed_per_io), ioc->shost->sg_tablesize);
  1999. }
  2000. ioc->chains_needed_per_io = chains_needed_per_io;
  2001. /* reply free queue sizing - taking into account for events */
  2002. num_of_reply_frames = ioc->hba_queue_depth + 32;
  2003. /* number of replies frames can't be a multiple of 16 */
  2004. /* decrease number of reply frames by 1 */
  2005. if (!(num_of_reply_frames % 16))
  2006. num_of_reply_frames--;
  2007. /* calculate number of reply free queue entries
  2008. * (must be multiple of 16)
  2009. */
  2010. /* (we know reply_free_queue_depth is not a multiple of 16) */
  2011. queue_size = num_of_reply_frames;
  2012. queue_size += 16 - (queue_size % 16);
  2013. ioc->reply_free_queue_depth = queue_size;
  2014. /* reply descriptor post queue sizing */
  2015. /* this size should be the number of request frames + number of reply
  2016. * frames
  2017. */
  2018. queue_size = ioc->hba_queue_depth + num_of_reply_frames + 1;
  2019. /* round up to 16 byte boundary */
  2020. if (queue_size % 16)
  2021. queue_size += 16 - (queue_size % 16);
  2022. /* check against IOC maximum reply post queue depth */
  2023. if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
  2024. queue_diff = queue_size -
  2025. facts->MaxReplyDescriptorPostQueueDepth;
  2026. /* round queue_diff up to multiple of 16 */
  2027. if (queue_diff % 16)
  2028. queue_diff += 16 - (queue_diff % 16);
  2029. /* adjust hba_queue_depth, reply_free_queue_depth,
  2030. * and queue_size
  2031. */
  2032. ioc->hba_queue_depth -= (queue_diff / 2);
  2033. ioc->reply_free_queue_depth -= (queue_diff / 2);
  2034. queue_size = facts->MaxReplyDescriptorPostQueueDepth;
  2035. }
  2036. ioc->reply_post_queue_depth = queue_size;
  2037. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
  2038. "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
  2039. "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
  2040. ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
  2041. ioc->chains_needed_per_io));
  2042. ioc->scsiio_depth = ioc->hba_queue_depth -
  2043. ioc->hi_priority_depth - ioc->internal_depth;
  2044. /* set the scsi host can_queue depth
  2045. * with some internal commands that could be outstanding
  2046. */
  2047. ioc->shost->can_queue = ioc->scsiio_depth - (2);
  2048. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
  2049. "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
  2050. /* contiguous pool for request and chains, 16 byte align, one extra "
  2051. * "frame for smid=0
  2052. */
  2053. ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
  2054. sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
  2055. /* hi-priority queue */
  2056. sz += (ioc->hi_priority_depth * ioc->request_sz);
  2057. /* internal queue */
  2058. sz += (ioc->internal_depth * ioc->request_sz);
  2059. ioc->request_dma_sz = sz;
  2060. ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
  2061. if (!ioc->request) {
  2062. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  2063. "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2064. "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
  2065. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2066. if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
  2067. goto out;
  2068. retry_sz += 64;
  2069. ioc->hba_queue_depth = max_request_credit - retry_sz;
  2070. goto retry_allocation;
  2071. }
  2072. if (retry_sz)
  2073. printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
  2074. "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
  2075. "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
  2076. ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
  2077. /* hi-priority queue */
  2078. ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
  2079. ioc->request_sz);
  2080. ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
  2081. ioc->request_sz);
  2082. /* internal queue */
  2083. ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
  2084. ioc->request_sz);
  2085. ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
  2086. ioc->request_sz);
  2087. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
  2088. "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
  2089. ioc->request, ioc->hba_queue_depth, ioc->request_sz,
  2090. (ioc->hba_queue_depth * ioc->request_sz)/1024));
  2091. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
  2092. ioc->name, (unsigned long long) ioc->request_dma));
  2093. total_sz += sz;
  2094. sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
  2095. ioc->scsi_lookup_pages = get_order(sz);
  2096. ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
  2097. GFP_KERNEL, ioc->scsi_lookup_pages);
  2098. if (!ioc->scsi_lookup) {
  2099. printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
  2100. "sz(%d)\n", ioc->name, (int)sz);
  2101. goto out;
  2102. }
  2103. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
  2104. "depth(%d)\n", ioc->name, ioc->request,
  2105. ioc->scsiio_depth));
  2106. /* loop till the allocation succeeds */
  2107. do {
  2108. sz = ioc->chain_depth * sizeof(struct chain_tracker);
  2109. ioc->chain_pages = get_order(sz);
  2110. ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
  2111. GFP_KERNEL, ioc->chain_pages);
  2112. if (ioc->chain_lookup == NULL)
  2113. ioc->chain_depth -= 100;
  2114. } while (ioc->chain_lookup == NULL);
  2115. ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
  2116. ioc->request_sz, 16, 0);
  2117. if (!ioc->chain_dma_pool) {
  2118. printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
  2119. "failed\n", ioc->name);
  2120. goto out;
  2121. }
  2122. for (i = 0; i < ioc->chain_depth; i++) {
  2123. ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
  2124. ioc->chain_dma_pool , GFP_KERNEL,
  2125. &ioc->chain_lookup[i].chain_buffer_dma);
  2126. if (!ioc->chain_lookup[i].chain_buffer) {
  2127. ioc->chain_depth = i;
  2128. goto chain_done;
  2129. }
  2130. total_sz += ioc->request_sz;
  2131. }
  2132. chain_done:
  2133. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
  2134. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
  2135. ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
  2136. ioc->request_sz))/1024));
  2137. /* initialize hi-priority queue smid's */
  2138. ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
  2139. sizeof(struct request_tracker), GFP_KERNEL);
  2140. if (!ioc->hpr_lookup) {
  2141. printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
  2142. ioc->name);
  2143. goto out;
  2144. }
  2145. ioc->hi_priority_smid = ioc->scsiio_depth + 1;
  2146. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
  2147. "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
  2148. ioc->hi_priority_depth, ioc->hi_priority_smid));
  2149. /* initialize internal queue smid's */
  2150. ioc->internal_lookup = kcalloc(ioc->internal_depth,
  2151. sizeof(struct request_tracker), GFP_KERNEL);
  2152. if (!ioc->internal_lookup) {
  2153. printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
  2154. ioc->name);
  2155. goto out;
  2156. }
  2157. ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
  2158. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
  2159. "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
  2160. ioc->internal_depth, ioc->internal_smid));
  2161. /* sense buffers, 4 byte align */
  2162. sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
  2163. ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
  2164. 0);
  2165. if (!ioc->sense_dma_pool) {
  2166. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
  2167. ioc->name);
  2168. goto out;
  2169. }
  2170. ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
  2171. &ioc->sense_dma);
  2172. if (!ioc->sense) {
  2173. printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
  2174. ioc->name);
  2175. goto out;
  2176. }
  2177. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
  2178. "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
  2179. "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
  2180. SCSI_SENSE_BUFFERSIZE, sz/1024));
  2181. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
  2182. ioc->name, (unsigned long long)ioc->sense_dma));
  2183. total_sz += sz;
  2184. /* reply pool, 4 byte align */
  2185. sz = ioc->reply_free_queue_depth * ioc->reply_sz;
  2186. ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
  2187. 0);
  2188. if (!ioc->reply_dma_pool) {
  2189. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
  2190. ioc->name);
  2191. goto out;
  2192. }
  2193. ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
  2194. &ioc->reply_dma);
  2195. if (!ioc->reply) {
  2196. printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
  2197. ioc->name);
  2198. goto out;
  2199. }
  2200. ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
  2201. ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
  2202. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
  2203. "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
  2204. ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
  2205. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
  2206. ioc->name, (unsigned long long)ioc->reply_dma));
  2207. total_sz += sz;
  2208. /* reply free queue, 16 byte align */
  2209. sz = ioc->reply_free_queue_depth * 4;
  2210. ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
  2211. ioc->pdev, sz, 16, 0);
  2212. if (!ioc->reply_free_dma_pool) {
  2213. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
  2214. "failed\n", ioc->name);
  2215. goto out;
  2216. }
  2217. ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
  2218. &ioc->reply_free_dma);
  2219. if (!ioc->reply_free) {
  2220. printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
  2221. "failed\n", ioc->name);
  2222. goto out;
  2223. }
  2224. memset(ioc->reply_free, 0, sz);
  2225. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
  2226. "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
  2227. ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
  2228. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
  2229. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
  2230. total_sz += sz;
  2231. /* reply post queue, 16 byte align */
  2232. sz = ioc->reply_post_queue_depth * sizeof(Mpi2DefaultReplyDescriptor_t);
  2233. ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
  2234. ioc->pdev, sz, 16, 0);
  2235. if (!ioc->reply_post_free_dma_pool) {
  2236. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
  2237. "failed\n", ioc->name);
  2238. goto out;
  2239. }
  2240. ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
  2241. GFP_KERNEL, &ioc->reply_post_free_dma);
  2242. if (!ioc->reply_post_free) {
  2243. printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
  2244. "failed\n", ioc->name);
  2245. goto out;
  2246. }
  2247. memset(ioc->reply_post_free, 0, sz);
  2248. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
  2249. "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
  2250. ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
  2251. sz/1024));
  2252. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
  2253. "(0x%llx)\n", ioc->name, (unsigned long long)
  2254. ioc->reply_post_free_dma));
  2255. total_sz += sz;
  2256. ioc->config_page_sz = 512;
  2257. ioc->config_page = pci_alloc_consistent(ioc->pdev,
  2258. ioc->config_page_sz, &ioc->config_page_dma);
  2259. if (!ioc->config_page) {
  2260. printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
  2261. "failed\n", ioc->name);
  2262. goto out;
  2263. }
  2264. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
  2265. "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
  2266. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
  2267. "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
  2268. total_sz += ioc->config_page_sz;
  2269. printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
  2270. ioc->name, total_sz/1024);
  2271. printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
  2272. "Max Controller Queue Depth(%d)\n",
  2273. ioc->name, ioc->shost->can_queue, facts->RequestCredit);
  2274. printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
  2275. ioc->name, ioc->shost->sg_tablesize);
  2276. return 0;
  2277. out:
  2278. return -ENOMEM;
  2279. }
  2280. /**
  2281. * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
  2282. * @ioc: Pointer to MPT_ADAPTER structure
  2283. * @cooked: Request raw or cooked IOC state
  2284. *
  2285. * Returns all IOC Doorbell register bits if cooked==0, else just the
  2286. * Doorbell bits in MPI_IOC_STATE_MASK.
  2287. */
  2288. u32
  2289. mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
  2290. {
  2291. u32 s, sc;
  2292. s = readl(&ioc->chip->Doorbell);
  2293. sc = s & MPI2_IOC_STATE_MASK;
  2294. return cooked ? sc : s;
  2295. }
  2296. /**
  2297. * _base_wait_on_iocstate - waiting on a particular ioc state
  2298. * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
  2299. * @timeout: timeout in second
  2300. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2301. *
  2302. * Returns 0 for success, non-zero for failure.
  2303. */
  2304. static int
  2305. _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
  2306. int sleep_flag)
  2307. {
  2308. u32 count, cntdn;
  2309. u32 current_state;
  2310. count = 0;
  2311. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2312. do {
  2313. current_state = mpt2sas_base_get_iocstate(ioc, 1);
  2314. if (current_state == ioc_state)
  2315. return 0;
  2316. if (count && current_state == MPI2_IOC_STATE_FAULT)
  2317. break;
  2318. if (sleep_flag == CAN_SLEEP)
  2319. msleep(1);
  2320. else
  2321. udelay(500);
  2322. count++;
  2323. } while (--cntdn);
  2324. return current_state;
  2325. }
  2326. /**
  2327. * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
  2328. * a write to the doorbell)
  2329. * @ioc: per adapter object
  2330. * @timeout: timeout in second
  2331. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2332. *
  2333. * Returns 0 for success, non-zero for failure.
  2334. *
  2335. * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
  2336. */
  2337. static int
  2338. _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2339. int sleep_flag)
  2340. {
  2341. u32 cntdn, count;
  2342. u32 int_status;
  2343. count = 0;
  2344. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2345. do {
  2346. int_status = readl(&ioc->chip->HostInterruptStatus);
  2347. if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2348. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2349. "successful count(%d), timeout(%d)\n", ioc->name,
  2350. __func__, count, timeout));
  2351. return 0;
  2352. }
  2353. if (sleep_flag == CAN_SLEEP)
  2354. msleep(1);
  2355. else
  2356. udelay(500);
  2357. count++;
  2358. } while (--cntdn);
  2359. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2360. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  2361. return -EFAULT;
  2362. }
  2363. /**
  2364. * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
  2365. * @ioc: per adapter object
  2366. * @timeout: timeout in second
  2367. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2368. *
  2369. * Returns 0 for success, non-zero for failure.
  2370. *
  2371. * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
  2372. * doorbell.
  2373. */
  2374. static int
  2375. _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2376. int sleep_flag)
  2377. {
  2378. u32 cntdn, count;
  2379. u32 int_status;
  2380. u32 doorbell;
  2381. count = 0;
  2382. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2383. do {
  2384. int_status = readl(&ioc->chip->HostInterruptStatus);
  2385. if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
  2386. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2387. "successful count(%d), timeout(%d)\n", ioc->name,
  2388. __func__, count, timeout));
  2389. return 0;
  2390. } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
  2391. doorbell = readl(&ioc->chip->Doorbell);
  2392. if ((doorbell & MPI2_IOC_STATE_MASK) ==
  2393. MPI2_IOC_STATE_FAULT) {
  2394. mpt2sas_base_fault_info(ioc , doorbell);
  2395. return -EFAULT;
  2396. }
  2397. } else if (int_status == 0xFFFFFFFF)
  2398. goto out;
  2399. if (sleep_flag == CAN_SLEEP)
  2400. msleep(1);
  2401. else
  2402. udelay(500);
  2403. count++;
  2404. } while (--cntdn);
  2405. out:
  2406. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2407. "int_status(%x)!\n", ioc->name, __func__, count, int_status);
  2408. return -EFAULT;
  2409. }
  2410. /**
  2411. * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
  2412. * @ioc: per adapter object
  2413. * @timeout: timeout in second
  2414. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2415. *
  2416. * Returns 0 for success, non-zero for failure.
  2417. *
  2418. */
  2419. static int
  2420. _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
  2421. int sleep_flag)
  2422. {
  2423. u32 cntdn, count;
  2424. u32 doorbell_reg;
  2425. count = 0;
  2426. cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
  2427. do {
  2428. doorbell_reg = readl(&ioc->chip->Doorbell);
  2429. if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
  2430. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  2431. "successful count(%d), timeout(%d)\n", ioc->name,
  2432. __func__, count, timeout));
  2433. return 0;
  2434. }
  2435. if (sleep_flag == CAN_SLEEP)
  2436. msleep(1);
  2437. else
  2438. udelay(500);
  2439. count++;
  2440. } while (--cntdn);
  2441. printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
  2442. "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
  2443. return -EFAULT;
  2444. }
  2445. /**
  2446. * _base_send_ioc_reset - send doorbell reset
  2447. * @ioc: per adapter object
  2448. * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
  2449. * @timeout: timeout in second
  2450. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2451. *
  2452. * Returns 0 for success, non-zero for failure.
  2453. */
  2454. static int
  2455. _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
  2456. int sleep_flag)
  2457. {
  2458. u32 ioc_state;
  2459. int r = 0;
  2460. if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
  2461. printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
  2462. ioc->name, __func__);
  2463. return -EFAULT;
  2464. }
  2465. if (!(ioc->facts.IOCCapabilities &
  2466. MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
  2467. return -EFAULT;
  2468. printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
  2469. writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
  2470. &ioc->chip->Doorbell);
  2471. if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
  2472. r = -EFAULT;
  2473. goto out;
  2474. }
  2475. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
  2476. timeout, sleep_flag);
  2477. if (ioc_state) {
  2478. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  2479. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  2480. r = -EFAULT;
  2481. goto out;
  2482. }
  2483. out:
  2484. printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
  2485. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  2486. return r;
  2487. }
  2488. /**
  2489. * _base_handshake_req_reply_wait - send request thru doorbell interface
  2490. * @ioc: per adapter object
  2491. * @request_bytes: request length
  2492. * @request: pointer having request payload
  2493. * @reply_bytes: reply length
  2494. * @reply: pointer to reply payload
  2495. * @timeout: timeout in second
  2496. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2497. *
  2498. * Returns 0 for success, non-zero for failure.
  2499. */
  2500. static int
  2501. _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
  2502. u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
  2503. {
  2504. MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
  2505. int i;
  2506. u8 failed;
  2507. u16 dummy;
  2508. __le32 *mfp;
  2509. /* make sure doorbell is not in use */
  2510. if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
  2511. printk(MPT2SAS_ERR_FMT "doorbell is in use "
  2512. " (line=%d)\n", ioc->name, __LINE__);
  2513. return -EFAULT;
  2514. }
  2515. /* clear pending doorbell interrupts from previous state changes */
  2516. if (readl(&ioc->chip->HostInterruptStatus) &
  2517. MPI2_HIS_IOC2SYS_DB_STATUS)
  2518. writel(0, &ioc->chip->HostInterruptStatus);
  2519. /* send message to ioc */
  2520. writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
  2521. ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
  2522. &ioc->chip->Doorbell);
  2523. if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
  2524. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2525. "int failed (line=%d)\n", ioc->name, __LINE__);
  2526. return -EFAULT;
  2527. }
  2528. writel(0, &ioc->chip->HostInterruptStatus);
  2529. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
  2530. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2531. "ack failed (line=%d)\n", ioc->name, __LINE__);
  2532. return -EFAULT;
  2533. }
  2534. /* send message 32-bits at a time */
  2535. for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
  2536. writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
  2537. if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
  2538. failed = 1;
  2539. }
  2540. if (failed) {
  2541. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2542. "sending request failed (line=%d)\n", ioc->name, __LINE__);
  2543. return -EFAULT;
  2544. }
  2545. /* now wait for the reply */
  2546. if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
  2547. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2548. "int failed (line=%d)\n", ioc->name, __LINE__);
  2549. return -EFAULT;
  2550. }
  2551. /* read the first two 16-bits, it gives the total length of the reply */
  2552. reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2553. & MPI2_DOORBELL_DATA_MASK);
  2554. writel(0, &ioc->chip->HostInterruptStatus);
  2555. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2556. printk(MPT2SAS_ERR_FMT "doorbell handshake "
  2557. "int failed (line=%d)\n", ioc->name, __LINE__);
  2558. return -EFAULT;
  2559. }
  2560. reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2561. & MPI2_DOORBELL_DATA_MASK);
  2562. writel(0, &ioc->chip->HostInterruptStatus);
  2563. for (i = 2; i < default_reply->MsgLength * 2; i++) {
  2564. if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
  2565. printk(MPT2SAS_ERR_FMT "doorbell "
  2566. "handshake int failed (line=%d)\n", ioc->name,
  2567. __LINE__);
  2568. return -EFAULT;
  2569. }
  2570. if (i >= reply_bytes/2) /* overflow case */
  2571. dummy = readl(&ioc->chip->Doorbell);
  2572. else
  2573. reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
  2574. & MPI2_DOORBELL_DATA_MASK);
  2575. writel(0, &ioc->chip->HostInterruptStatus);
  2576. }
  2577. _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
  2578. if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
  2579. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
  2580. " (line=%d)\n", ioc->name, __LINE__));
  2581. }
  2582. writel(0, &ioc->chip->HostInterruptStatus);
  2583. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2584. mfp = (__le32 *)reply;
  2585. printk(KERN_INFO "\toffset:data\n");
  2586. for (i = 0; i < reply_bytes/4; i++)
  2587. printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
  2588. le32_to_cpu(mfp[i]));
  2589. }
  2590. return 0;
  2591. }
  2592. /**
  2593. * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
  2594. * @ioc: per adapter object
  2595. * @mpi_reply: the reply payload from FW
  2596. * @mpi_request: the request payload sent to FW
  2597. *
  2598. * The SAS IO Unit Control Request message allows the host to perform low-level
  2599. * operations, such as resets on the PHYs of the IO Unit, also allows the host
  2600. * to obtain the IOC assigned device handles for a device if it has other
  2601. * identifying information about the device, in addition allows the host to
  2602. * remove IOC resources associated with the device.
  2603. *
  2604. * Returns 0 for success, non-zero for failure.
  2605. */
  2606. int
  2607. mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
  2608. Mpi2SasIoUnitControlReply_t *mpi_reply,
  2609. Mpi2SasIoUnitControlRequest_t *mpi_request)
  2610. {
  2611. u16 smid;
  2612. u32 ioc_state;
  2613. unsigned long timeleft;
  2614. u8 issue_reset;
  2615. int rc;
  2616. void *request;
  2617. u16 wait_state_count;
  2618. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2619. __func__));
  2620. mutex_lock(&ioc->base_cmds.mutex);
  2621. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2622. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2623. ioc->name, __func__);
  2624. rc = -EAGAIN;
  2625. goto out;
  2626. }
  2627. wait_state_count = 0;
  2628. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2629. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2630. if (wait_state_count++ == 10) {
  2631. printk(MPT2SAS_ERR_FMT
  2632. "%s: failed due to ioc not operational\n",
  2633. ioc->name, __func__);
  2634. rc = -EFAULT;
  2635. goto out;
  2636. }
  2637. ssleep(1);
  2638. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2639. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2640. "operational state(count=%d)\n", ioc->name,
  2641. __func__, wait_state_count);
  2642. }
  2643. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2644. if (!smid) {
  2645. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2646. ioc->name, __func__);
  2647. rc = -EAGAIN;
  2648. goto out;
  2649. }
  2650. rc = 0;
  2651. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2652. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2653. ioc->base_cmds.smid = smid;
  2654. memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
  2655. if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2656. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
  2657. ioc->ioc_link_reset_in_progress = 1;
  2658. mpt2sas_base_put_smid_default(ioc, smid);
  2659. init_completion(&ioc->base_cmds.done);
  2660. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2661. msecs_to_jiffies(10000));
  2662. if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
  2663. mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
  2664. ioc->ioc_link_reset_in_progress)
  2665. ioc->ioc_link_reset_in_progress = 0;
  2666. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2667. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2668. ioc->name, __func__);
  2669. _debug_dump_mf(mpi_request,
  2670. sizeof(Mpi2SasIoUnitControlRequest_t)/4);
  2671. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2672. issue_reset = 1;
  2673. goto issue_host_reset;
  2674. }
  2675. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2676. memcpy(mpi_reply, ioc->base_cmds.reply,
  2677. sizeof(Mpi2SasIoUnitControlReply_t));
  2678. else
  2679. memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  2680. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2681. goto out;
  2682. issue_host_reset:
  2683. if (issue_reset)
  2684. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2685. FORCE_BIG_HAMMER);
  2686. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2687. rc = -EFAULT;
  2688. out:
  2689. mutex_unlock(&ioc->base_cmds.mutex);
  2690. return rc;
  2691. }
  2692. /**
  2693. * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
  2694. * @ioc: per adapter object
  2695. * @mpi_reply: the reply payload from FW
  2696. * @mpi_request: the request payload sent to FW
  2697. *
  2698. * The SCSI Enclosure Processor request message causes the IOC to
  2699. * communicate with SES devices to control LED status signals.
  2700. *
  2701. * Returns 0 for success, non-zero for failure.
  2702. */
  2703. int
  2704. mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
  2705. Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
  2706. {
  2707. u16 smid;
  2708. u32 ioc_state;
  2709. unsigned long timeleft;
  2710. u8 issue_reset;
  2711. int rc;
  2712. void *request;
  2713. u16 wait_state_count;
  2714. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2715. __func__));
  2716. mutex_lock(&ioc->base_cmds.mutex);
  2717. if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
  2718. printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
  2719. ioc->name, __func__);
  2720. rc = -EAGAIN;
  2721. goto out;
  2722. }
  2723. wait_state_count = 0;
  2724. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2725. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  2726. if (wait_state_count++ == 10) {
  2727. printk(MPT2SAS_ERR_FMT
  2728. "%s: failed due to ioc not operational\n",
  2729. ioc->name, __func__);
  2730. rc = -EFAULT;
  2731. goto out;
  2732. }
  2733. ssleep(1);
  2734. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  2735. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  2736. "operational state(count=%d)\n", ioc->name,
  2737. __func__, wait_state_count);
  2738. }
  2739. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2740. if (!smid) {
  2741. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2742. ioc->name, __func__);
  2743. rc = -EAGAIN;
  2744. goto out;
  2745. }
  2746. rc = 0;
  2747. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2748. request = mpt2sas_base_get_msg_frame(ioc, smid);
  2749. ioc->base_cmds.smid = smid;
  2750. memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
  2751. mpt2sas_base_put_smid_default(ioc, smid);
  2752. init_completion(&ioc->base_cmds.done);
  2753. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2754. msecs_to_jiffies(10000));
  2755. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2756. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2757. ioc->name, __func__);
  2758. _debug_dump_mf(mpi_request,
  2759. sizeof(Mpi2SepRequest_t)/4);
  2760. if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
  2761. issue_reset = 1;
  2762. goto issue_host_reset;
  2763. }
  2764. if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
  2765. memcpy(mpi_reply, ioc->base_cmds.reply,
  2766. sizeof(Mpi2SepReply_t));
  2767. else
  2768. memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
  2769. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2770. goto out;
  2771. issue_host_reset:
  2772. if (issue_reset)
  2773. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  2774. FORCE_BIG_HAMMER);
  2775. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  2776. rc = -EFAULT;
  2777. out:
  2778. mutex_unlock(&ioc->base_cmds.mutex);
  2779. return rc;
  2780. }
  2781. /**
  2782. * _base_get_port_facts - obtain port facts reply and save in ioc
  2783. * @ioc: per adapter object
  2784. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2785. *
  2786. * Returns 0 for success, non-zero for failure.
  2787. */
  2788. static int
  2789. _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
  2790. {
  2791. Mpi2PortFactsRequest_t mpi_request;
  2792. Mpi2PortFactsReply_t mpi_reply;
  2793. struct mpt2sas_port_facts *pfacts;
  2794. int mpi_reply_sz, mpi_request_sz, r;
  2795. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2796. __func__));
  2797. mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
  2798. mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
  2799. memset(&mpi_request, 0, mpi_request_sz);
  2800. mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
  2801. mpi_request.PortNumber = port;
  2802. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  2803. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  2804. if (r != 0) {
  2805. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  2806. ioc->name, __func__, r);
  2807. return r;
  2808. }
  2809. pfacts = &ioc->pfacts[port];
  2810. memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
  2811. pfacts->PortNumber = mpi_reply.PortNumber;
  2812. pfacts->VP_ID = mpi_reply.VP_ID;
  2813. pfacts->VF_ID = mpi_reply.VF_ID;
  2814. pfacts->MaxPostedCmdBuffers =
  2815. le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
  2816. return 0;
  2817. }
  2818. /**
  2819. * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
  2820. * @ioc: per adapter object
  2821. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2822. *
  2823. * Returns 0 for success, non-zero for failure.
  2824. */
  2825. static int
  2826. _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2827. {
  2828. Mpi2IOCFactsRequest_t mpi_request;
  2829. Mpi2IOCFactsReply_t mpi_reply;
  2830. struct mpt2sas_facts *facts;
  2831. int mpi_reply_sz, mpi_request_sz, r;
  2832. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2833. __func__));
  2834. mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
  2835. mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
  2836. memset(&mpi_request, 0, mpi_request_sz);
  2837. mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
  2838. r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
  2839. (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
  2840. if (r != 0) {
  2841. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  2842. ioc->name, __func__, r);
  2843. return r;
  2844. }
  2845. facts = &ioc->facts;
  2846. memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
  2847. facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
  2848. facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
  2849. facts->VP_ID = mpi_reply.VP_ID;
  2850. facts->VF_ID = mpi_reply.VF_ID;
  2851. facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
  2852. facts->MaxChainDepth = mpi_reply.MaxChainDepth;
  2853. facts->WhoInit = mpi_reply.WhoInit;
  2854. facts->NumberOfPorts = mpi_reply.NumberOfPorts;
  2855. facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
  2856. facts->MaxReplyDescriptorPostQueueDepth =
  2857. le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
  2858. facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
  2859. facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
  2860. if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
  2861. ioc->ir_firmware = 1;
  2862. facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
  2863. facts->IOCRequestFrameSize =
  2864. le16_to_cpu(mpi_reply.IOCRequestFrameSize);
  2865. facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
  2866. facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
  2867. ioc->shost->max_id = -1;
  2868. facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
  2869. facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
  2870. facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
  2871. facts->HighPriorityCredit =
  2872. le16_to_cpu(mpi_reply.HighPriorityCredit);
  2873. facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
  2874. facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
  2875. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
  2876. "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
  2877. facts->MaxChainDepth));
  2878. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
  2879. "reply frame size(%d)\n", ioc->name,
  2880. facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
  2881. return 0;
  2882. }
  2883. /**
  2884. * _base_send_ioc_init - send ioc_init to firmware
  2885. * @ioc: per adapter object
  2886. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2887. *
  2888. * Returns 0 for success, non-zero for failure.
  2889. */
  2890. static int
  2891. _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2892. {
  2893. Mpi2IOCInitRequest_t mpi_request;
  2894. Mpi2IOCInitReply_t mpi_reply;
  2895. int r;
  2896. struct timeval current_time;
  2897. u16 ioc_status;
  2898. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  2899. __func__));
  2900. memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
  2901. mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
  2902. mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  2903. mpi_request.VF_ID = 0; /* TODO */
  2904. mpi_request.VP_ID = 0;
  2905. mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
  2906. mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  2907. mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
  2908. mpi_request.ReplyDescriptorPostQueueDepth =
  2909. cpu_to_le16(ioc->reply_post_queue_depth);
  2910. mpi_request.ReplyFreeQueueDepth =
  2911. cpu_to_le16(ioc->reply_free_queue_depth);
  2912. mpi_request.SenseBufferAddressHigh =
  2913. cpu_to_le32((u64)ioc->sense_dma >> 32);
  2914. mpi_request.SystemReplyAddressHigh =
  2915. cpu_to_le32((u64)ioc->reply_dma >> 32);
  2916. mpi_request.SystemRequestFrameBaseAddress =
  2917. cpu_to_le64((u64)ioc->request_dma);
  2918. mpi_request.ReplyFreeQueueAddress =
  2919. cpu_to_le64((u64)ioc->reply_free_dma);
  2920. mpi_request.ReplyDescriptorPostQueueAddress =
  2921. cpu_to_le64((u64)ioc->reply_post_free_dma);
  2922. /* This time stamp specifies number of milliseconds
  2923. * since epoch ~ midnight January 1, 1970.
  2924. */
  2925. do_gettimeofday(&current_time);
  2926. mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
  2927. (current_time.tv_usec / 1000));
  2928. if (ioc->logging_level & MPT_DEBUG_INIT) {
  2929. __le32 *mfp;
  2930. int i;
  2931. mfp = (__le32 *)&mpi_request;
  2932. printk(KERN_INFO "\toffset:data\n");
  2933. for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
  2934. printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
  2935. le32_to_cpu(mfp[i]));
  2936. }
  2937. r = _base_handshake_req_reply_wait(ioc,
  2938. sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
  2939. sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
  2940. sleep_flag);
  2941. if (r != 0) {
  2942. printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
  2943. ioc->name, __func__, r);
  2944. return r;
  2945. }
  2946. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
  2947. if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
  2948. mpi_reply.IOCLogInfo) {
  2949. printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
  2950. r = -EIO;
  2951. }
  2952. return 0;
  2953. }
  2954. /**
  2955. * _base_send_port_enable - send port_enable(discovery stuff) to firmware
  2956. * @ioc: per adapter object
  2957. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  2958. *
  2959. * Returns 0 for success, non-zero for failure.
  2960. */
  2961. static int
  2962. _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  2963. {
  2964. Mpi2PortEnableRequest_t *mpi_request;
  2965. u32 ioc_state;
  2966. unsigned long timeleft;
  2967. int r = 0;
  2968. u16 smid;
  2969. printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
  2970. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  2971. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  2972. ioc->name, __func__);
  2973. return -EAGAIN;
  2974. }
  2975. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  2976. if (!smid) {
  2977. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  2978. ioc->name, __func__);
  2979. return -EAGAIN;
  2980. }
  2981. ioc->base_cmds.status = MPT2_CMD_PENDING;
  2982. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  2983. ioc->base_cmds.smid = smid;
  2984. memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
  2985. mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
  2986. mpi_request->VF_ID = 0; /* TODO */
  2987. mpi_request->VP_ID = 0;
  2988. mpt2sas_base_put_smid_default(ioc, smid);
  2989. init_completion(&ioc->base_cmds.done);
  2990. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
  2991. 300*HZ);
  2992. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  2993. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  2994. ioc->name, __func__);
  2995. _debug_dump_mf(mpi_request,
  2996. sizeof(Mpi2PortEnableRequest_t)/4);
  2997. if (ioc->base_cmds.status & MPT2_CMD_RESET)
  2998. r = -EFAULT;
  2999. else
  3000. r = -ETIME;
  3001. goto out;
  3002. } else
  3003. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
  3004. ioc->name, __func__));
  3005. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
  3006. 60, sleep_flag);
  3007. if (ioc_state) {
  3008. printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
  3009. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  3010. r = -EFAULT;
  3011. }
  3012. out:
  3013. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  3014. printk(MPT2SAS_INFO_FMT "port enable: %s\n",
  3015. ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
  3016. return r;
  3017. }
  3018. /**
  3019. * _base_unmask_events - turn on notification for this event
  3020. * @ioc: per adapter object
  3021. * @event: firmware event
  3022. *
  3023. * The mask is stored in ioc->event_masks.
  3024. */
  3025. static void
  3026. _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
  3027. {
  3028. u32 desired_event;
  3029. if (event >= 128)
  3030. return;
  3031. desired_event = (1 << (event % 32));
  3032. if (event < 32)
  3033. ioc->event_masks[0] &= ~desired_event;
  3034. else if (event < 64)
  3035. ioc->event_masks[1] &= ~desired_event;
  3036. else if (event < 96)
  3037. ioc->event_masks[2] &= ~desired_event;
  3038. else if (event < 128)
  3039. ioc->event_masks[3] &= ~desired_event;
  3040. }
  3041. /**
  3042. * _base_event_notification - send event notification
  3043. * @ioc: per adapter object
  3044. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3045. *
  3046. * Returns 0 for success, non-zero for failure.
  3047. */
  3048. static int
  3049. _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3050. {
  3051. Mpi2EventNotificationRequest_t *mpi_request;
  3052. unsigned long timeleft;
  3053. u16 smid;
  3054. int r = 0;
  3055. int i;
  3056. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3057. __func__));
  3058. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  3059. printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
  3060. ioc->name, __func__);
  3061. return -EAGAIN;
  3062. }
  3063. smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
  3064. if (!smid) {
  3065. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  3066. ioc->name, __func__);
  3067. return -EAGAIN;
  3068. }
  3069. ioc->base_cmds.status = MPT2_CMD_PENDING;
  3070. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  3071. ioc->base_cmds.smid = smid;
  3072. memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
  3073. mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
  3074. mpi_request->VF_ID = 0; /* TODO */
  3075. mpi_request->VP_ID = 0;
  3076. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3077. mpi_request->EventMasks[i] =
  3078. cpu_to_le32(ioc->event_masks[i]);
  3079. mpt2sas_base_put_smid_default(ioc, smid);
  3080. init_completion(&ioc->base_cmds.done);
  3081. timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
  3082. if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
  3083. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  3084. ioc->name, __func__);
  3085. _debug_dump_mf(mpi_request,
  3086. sizeof(Mpi2EventNotificationRequest_t)/4);
  3087. if (ioc->base_cmds.status & MPT2_CMD_RESET)
  3088. r = -EFAULT;
  3089. else
  3090. r = -ETIME;
  3091. } else
  3092. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
  3093. ioc->name, __func__));
  3094. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  3095. return r;
  3096. }
  3097. /**
  3098. * mpt2sas_base_validate_event_type - validating event types
  3099. * @ioc: per adapter object
  3100. * @event: firmware event
  3101. *
  3102. * This will turn on firmware event notification when application
  3103. * ask for that event. We don't mask events that are already enabled.
  3104. */
  3105. void
  3106. mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
  3107. {
  3108. int i, j;
  3109. u32 event_mask, desired_event;
  3110. u8 send_update_to_fw;
  3111. for (i = 0, send_update_to_fw = 0; i <
  3112. MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
  3113. event_mask = ~event_type[i];
  3114. desired_event = 1;
  3115. for (j = 0; j < 32; j++) {
  3116. if (!(event_mask & desired_event) &&
  3117. (ioc->event_masks[i] & desired_event)) {
  3118. ioc->event_masks[i] &= ~desired_event;
  3119. send_update_to_fw = 1;
  3120. }
  3121. desired_event = (desired_event << 1);
  3122. }
  3123. }
  3124. if (!send_update_to_fw)
  3125. return;
  3126. mutex_lock(&ioc->base_cmds.mutex);
  3127. _base_event_notification(ioc, CAN_SLEEP);
  3128. mutex_unlock(&ioc->base_cmds.mutex);
  3129. }
  3130. /**
  3131. * _base_diag_reset - the "big hammer" start of day reset
  3132. * @ioc: per adapter object
  3133. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3134. *
  3135. * Returns 0 for success, non-zero for failure.
  3136. */
  3137. static int
  3138. _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3139. {
  3140. u32 host_diagnostic;
  3141. u32 ioc_state;
  3142. u32 count;
  3143. u32 hcb_size;
  3144. printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
  3145. _base_save_msix_table(ioc);
  3146. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
  3147. ioc->name));
  3148. count = 0;
  3149. do {
  3150. /* Write magic sequence to WriteSequence register
  3151. * Loop until in diagnostic mode
  3152. */
  3153. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
  3154. "sequence\n", ioc->name));
  3155. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3156. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
  3157. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
  3158. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
  3159. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3160. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3161. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
  3162. /* wait 100 msec */
  3163. if (sleep_flag == CAN_SLEEP)
  3164. msleep(100);
  3165. else
  3166. mdelay(100);
  3167. if (count++ > 20)
  3168. goto out;
  3169. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3170. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
  3171. "sequence: count(%d), host_diagnostic(0x%08x)\n",
  3172. ioc->name, count, host_diagnostic));
  3173. } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
  3174. hcb_size = readl(&ioc->chip->HCBSize);
  3175. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
  3176. ioc->name));
  3177. writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
  3178. &ioc->chip->HostDiagnostic);
  3179. /* don't access any registers for 50 milliseconds */
  3180. msleep(50);
  3181. /* 300 second max wait */
  3182. for (count = 0; count < 3000000 ; count++) {
  3183. host_diagnostic = readl(&ioc->chip->HostDiagnostic);
  3184. if (host_diagnostic == 0xFFFFFFFF)
  3185. goto out;
  3186. if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
  3187. break;
  3188. /* wait 100 msec */
  3189. if (sleep_flag == CAN_SLEEP)
  3190. msleep(1);
  3191. else
  3192. mdelay(1);
  3193. }
  3194. if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
  3195. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
  3196. "assuming the HCB Address points to good F/W\n",
  3197. ioc->name));
  3198. host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
  3199. host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
  3200. writel(host_diagnostic, &ioc->chip->HostDiagnostic);
  3201. drsprintk(ioc, printk(MPT2SAS_INFO_FMT
  3202. "re-enable the HCDW\n", ioc->name));
  3203. writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
  3204. &ioc->chip->HCBSize);
  3205. }
  3206. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
  3207. ioc->name));
  3208. writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
  3209. &ioc->chip->HostDiagnostic);
  3210. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
  3211. "diagnostic register\n", ioc->name));
  3212. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
  3213. drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
  3214. "READY state\n", ioc->name));
  3215. ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
  3216. sleep_flag);
  3217. if (ioc_state) {
  3218. printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
  3219. " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
  3220. goto out;
  3221. }
  3222. _base_restore_msix_table(ioc);
  3223. printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
  3224. return 0;
  3225. out:
  3226. printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
  3227. return -EFAULT;
  3228. }
  3229. /**
  3230. * _base_make_ioc_ready - put controller in READY state
  3231. * @ioc: per adapter object
  3232. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3233. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  3234. *
  3235. * Returns 0 for success, non-zero for failure.
  3236. */
  3237. static int
  3238. _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  3239. enum reset_type type)
  3240. {
  3241. u32 ioc_state;
  3242. int rc;
  3243. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3244. __func__));
  3245. if (ioc->pci_error_recovery)
  3246. return 0;
  3247. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  3248. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
  3249. ioc->name, __func__, ioc_state));
  3250. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
  3251. return 0;
  3252. if (ioc_state & MPI2_DOORBELL_USED) {
  3253. dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
  3254. "active!\n", ioc->name));
  3255. goto issue_diag_reset;
  3256. }
  3257. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  3258. mpt2sas_base_fault_info(ioc, ioc_state &
  3259. MPI2_DOORBELL_DATA_MASK);
  3260. goto issue_diag_reset;
  3261. }
  3262. if (type == FORCE_BIG_HAMMER)
  3263. goto issue_diag_reset;
  3264. if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
  3265. if (!(_base_send_ioc_reset(ioc,
  3266. MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
  3267. ioc->ioc_reset_count++;
  3268. return 0;
  3269. }
  3270. issue_diag_reset:
  3271. rc = _base_diag_reset(ioc, CAN_SLEEP);
  3272. ioc->ioc_reset_count++;
  3273. return rc;
  3274. }
  3275. /**
  3276. * _base_make_ioc_operational - put controller in OPERATIONAL state
  3277. * @ioc: per adapter object
  3278. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3279. *
  3280. * Returns 0 for success, non-zero for failure.
  3281. */
  3282. static int
  3283. _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3284. {
  3285. int r, i;
  3286. unsigned long flags;
  3287. u32 reply_address;
  3288. u16 smid;
  3289. struct _tr_list *delayed_tr, *delayed_tr_next;
  3290. u8 hide_flag;
  3291. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3292. __func__));
  3293. /* clean the delayed target reset list */
  3294. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3295. &ioc->delayed_tr_list, list) {
  3296. list_del(&delayed_tr->list);
  3297. kfree(delayed_tr);
  3298. }
  3299. list_for_each_entry_safe(delayed_tr, delayed_tr_next,
  3300. &ioc->delayed_tr_volume_list, list) {
  3301. list_del(&delayed_tr->list);
  3302. kfree(delayed_tr);
  3303. }
  3304. /* initialize the scsi lookup free list */
  3305. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3306. INIT_LIST_HEAD(&ioc->free_list);
  3307. smid = 1;
  3308. for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
  3309. INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
  3310. ioc->scsi_lookup[i].cb_idx = 0xFF;
  3311. ioc->scsi_lookup[i].smid = smid;
  3312. ioc->scsi_lookup[i].scmd = NULL;
  3313. ioc->scsi_lookup[i].direct_io = 0;
  3314. list_add_tail(&ioc->scsi_lookup[i].tracker_list,
  3315. &ioc->free_list);
  3316. }
  3317. /* hi-priority queue */
  3318. INIT_LIST_HEAD(&ioc->hpr_free_list);
  3319. smid = ioc->hi_priority_smid;
  3320. for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
  3321. ioc->hpr_lookup[i].cb_idx = 0xFF;
  3322. ioc->hpr_lookup[i].smid = smid;
  3323. list_add_tail(&ioc->hpr_lookup[i].tracker_list,
  3324. &ioc->hpr_free_list);
  3325. }
  3326. /* internal queue */
  3327. INIT_LIST_HEAD(&ioc->internal_free_list);
  3328. smid = ioc->internal_smid;
  3329. for (i = 0; i < ioc->internal_depth; i++, smid++) {
  3330. ioc->internal_lookup[i].cb_idx = 0xFF;
  3331. ioc->internal_lookup[i].smid = smid;
  3332. list_add_tail(&ioc->internal_lookup[i].tracker_list,
  3333. &ioc->internal_free_list);
  3334. }
  3335. /* chain pool */
  3336. INIT_LIST_HEAD(&ioc->free_chain_list);
  3337. for (i = 0; i < ioc->chain_depth; i++)
  3338. list_add_tail(&ioc->chain_lookup[i].tracker_list,
  3339. &ioc->free_chain_list);
  3340. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3341. /* initialize Reply Free Queue */
  3342. for (i = 0, reply_address = (u32)ioc->reply_dma ;
  3343. i < ioc->reply_free_queue_depth ; i++, reply_address +=
  3344. ioc->reply_sz)
  3345. ioc->reply_free[i] = cpu_to_le32(reply_address);
  3346. /* initialize Reply Post Free Queue */
  3347. for (i = 0; i < ioc->reply_post_queue_depth; i++)
  3348. ioc->reply_post_free[i].Words = cpu_to_le64(ULLONG_MAX);
  3349. r = _base_send_ioc_init(ioc, sleep_flag);
  3350. if (r)
  3351. return r;
  3352. /* initialize the index's */
  3353. ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
  3354. ioc->reply_post_host_index = 0;
  3355. writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
  3356. writel(0, &ioc->chip->ReplyPostHostIndex);
  3357. _base_unmask_interrupts(ioc);
  3358. r = _base_event_notification(ioc, sleep_flag);
  3359. if (r)
  3360. return r;
  3361. if (sleep_flag == CAN_SLEEP)
  3362. _base_static_config_pages(ioc);
  3363. if (ioc->wait_for_port_enable_to_complete && ioc->is_warpdrive) {
  3364. if (ioc->manu_pg10.OEMIdentifier == 0x80) {
  3365. hide_flag = (u8) (ioc->manu_pg10.OEMSpecificFlags0 &
  3366. MFG_PAGE10_HIDE_SSDS_MASK);
  3367. if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
  3368. ioc->mfg_pg10_hide_flag = hide_flag;
  3369. }
  3370. }
  3371. if (ioc->wait_for_port_enable_to_complete) {
  3372. if (diag_buffer_enable != 0)
  3373. mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
  3374. if (disable_discovery > 0)
  3375. return r;
  3376. }
  3377. r = _base_send_port_enable(ioc, sleep_flag);
  3378. if (r)
  3379. return r;
  3380. return r;
  3381. }
  3382. /**
  3383. * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
  3384. * @ioc: per adapter object
  3385. *
  3386. * Return nothing.
  3387. */
  3388. void
  3389. mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
  3390. {
  3391. struct pci_dev *pdev = ioc->pdev;
  3392. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3393. __func__));
  3394. _base_mask_interrupts(ioc);
  3395. ioc->shost_recovery = 1;
  3396. _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3397. ioc->shost_recovery = 0;
  3398. if (ioc->pci_irq) {
  3399. synchronize_irq(pdev->irq);
  3400. free_irq(ioc->pci_irq, ioc);
  3401. }
  3402. _base_disable_msix(ioc);
  3403. if (ioc->chip_phys)
  3404. iounmap(ioc->chip);
  3405. ioc->pci_irq = -1;
  3406. ioc->chip_phys = 0;
  3407. pci_release_selected_regions(ioc->pdev, ioc->bars);
  3408. pci_disable_pcie_error_reporting(pdev);
  3409. pci_disable_device(pdev);
  3410. return;
  3411. }
  3412. /**
  3413. * mpt2sas_base_attach - attach controller instance
  3414. * @ioc: per adapter object
  3415. *
  3416. * Returns 0 for success, non-zero for failure.
  3417. */
  3418. int
  3419. mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
  3420. {
  3421. int r, i;
  3422. dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3423. __func__));
  3424. r = mpt2sas_base_map_resources(ioc);
  3425. if (r)
  3426. return r;
  3427. pci_set_drvdata(ioc->pdev, ioc->shost);
  3428. r = _base_get_ioc_facts(ioc, CAN_SLEEP);
  3429. if (r)
  3430. goto out_free_resources;
  3431. r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
  3432. if (r)
  3433. goto out_free_resources;
  3434. ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
  3435. sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
  3436. if (!ioc->pfacts) {
  3437. r = -ENOMEM;
  3438. goto out_free_resources;
  3439. }
  3440. for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
  3441. r = _base_get_port_facts(ioc, i, CAN_SLEEP);
  3442. if (r)
  3443. goto out_free_resources;
  3444. }
  3445. r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
  3446. if (r)
  3447. goto out_free_resources;
  3448. init_waitqueue_head(&ioc->reset_wq);
  3449. /* allocate memory pd handle bitmask list */
  3450. ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
  3451. if (ioc->facts.MaxDevHandle % 8)
  3452. ioc->pd_handles_sz++;
  3453. ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
  3454. GFP_KERNEL);
  3455. if (!ioc->pd_handles) {
  3456. r = -ENOMEM;
  3457. goto out_free_resources;
  3458. }
  3459. ioc->fwfault_debug = mpt2sas_fwfault_debug;
  3460. /* base internal command bits */
  3461. mutex_init(&ioc->base_cmds.mutex);
  3462. ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3463. ioc->base_cmds.status = MPT2_CMD_NOT_USED;
  3464. /* transport internal command bits */
  3465. ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3466. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  3467. mutex_init(&ioc->transport_cmds.mutex);
  3468. /* scsih internal command bits */
  3469. ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3470. ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
  3471. mutex_init(&ioc->scsih_cmds.mutex);
  3472. /* task management internal command bits */
  3473. ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3474. ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
  3475. mutex_init(&ioc->tm_cmds.mutex);
  3476. /* config page internal command bits */
  3477. ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3478. ioc->config_cmds.status = MPT2_CMD_NOT_USED;
  3479. mutex_init(&ioc->config_cmds.mutex);
  3480. /* ctl module internal command bits */
  3481. ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
  3482. ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  3483. ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
  3484. mutex_init(&ioc->ctl_cmds.mutex);
  3485. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  3486. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  3487. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
  3488. !ioc->ctl_cmds.sense) {
  3489. r = -ENOMEM;
  3490. goto out_free_resources;
  3491. }
  3492. if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
  3493. !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
  3494. !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
  3495. r = -ENOMEM;
  3496. goto out_free_resources;
  3497. }
  3498. init_completion(&ioc->shost_recovery_done);
  3499. for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
  3500. ioc->event_masks[i] = -1;
  3501. /* here we enable the events we care about */
  3502. _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
  3503. _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
  3504. _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
  3505. _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
  3506. _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
  3507. _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
  3508. _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
  3509. _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
  3510. _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
  3511. _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
  3512. r = _base_make_ioc_operational(ioc, CAN_SLEEP);
  3513. if (r)
  3514. goto out_free_resources;
  3515. if (missing_delay[0] != -1 && missing_delay[1] != -1)
  3516. _base_update_missing_delay(ioc, missing_delay[0],
  3517. missing_delay[1]);
  3518. mpt2sas_base_start_watchdog(ioc);
  3519. return 0;
  3520. out_free_resources:
  3521. ioc->remove_host = 1;
  3522. mpt2sas_base_free_resources(ioc);
  3523. _base_release_memory_pools(ioc);
  3524. pci_set_drvdata(ioc->pdev, NULL);
  3525. kfree(ioc->pd_handles);
  3526. kfree(ioc->tm_cmds.reply);
  3527. kfree(ioc->transport_cmds.reply);
  3528. kfree(ioc->scsih_cmds.reply);
  3529. kfree(ioc->config_cmds.reply);
  3530. kfree(ioc->base_cmds.reply);
  3531. kfree(ioc->ctl_cmds.reply);
  3532. kfree(ioc->ctl_cmds.sense);
  3533. kfree(ioc->pfacts);
  3534. ioc->ctl_cmds.reply = NULL;
  3535. ioc->base_cmds.reply = NULL;
  3536. ioc->tm_cmds.reply = NULL;
  3537. ioc->scsih_cmds.reply = NULL;
  3538. ioc->transport_cmds.reply = NULL;
  3539. ioc->config_cmds.reply = NULL;
  3540. ioc->pfacts = NULL;
  3541. return r;
  3542. }
  3543. /**
  3544. * mpt2sas_base_detach - remove controller instance
  3545. * @ioc: per adapter object
  3546. *
  3547. * Return nothing.
  3548. */
  3549. void
  3550. mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
  3551. {
  3552. dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
  3553. __func__));
  3554. mpt2sas_base_stop_watchdog(ioc);
  3555. mpt2sas_base_free_resources(ioc);
  3556. _base_release_memory_pools(ioc);
  3557. pci_set_drvdata(ioc->pdev, NULL);
  3558. kfree(ioc->pd_handles);
  3559. kfree(ioc->pfacts);
  3560. kfree(ioc->ctl_cmds.reply);
  3561. kfree(ioc->ctl_cmds.sense);
  3562. kfree(ioc->base_cmds.reply);
  3563. kfree(ioc->tm_cmds.reply);
  3564. kfree(ioc->transport_cmds.reply);
  3565. kfree(ioc->scsih_cmds.reply);
  3566. kfree(ioc->config_cmds.reply);
  3567. }
  3568. /**
  3569. * _base_reset_handler - reset callback handler (for base)
  3570. * @ioc: per adapter object
  3571. * @reset_phase: phase
  3572. *
  3573. * The handler for doing any required cleanup or initialization.
  3574. *
  3575. * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
  3576. * MPT2_IOC_DONE_RESET
  3577. *
  3578. * Return nothing.
  3579. */
  3580. static void
  3581. _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
  3582. {
  3583. mpt2sas_scsih_reset_handler(ioc, reset_phase);
  3584. mpt2sas_ctl_reset_handler(ioc, reset_phase);
  3585. switch (reset_phase) {
  3586. case MPT2_IOC_PRE_RESET:
  3587. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  3588. "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
  3589. break;
  3590. case MPT2_IOC_AFTER_RESET:
  3591. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  3592. "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
  3593. if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
  3594. ioc->transport_cmds.status |= MPT2_CMD_RESET;
  3595. mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
  3596. complete(&ioc->transport_cmds.done);
  3597. }
  3598. if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
  3599. ioc->base_cmds.status |= MPT2_CMD_RESET;
  3600. mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
  3601. complete(&ioc->base_cmds.done);
  3602. }
  3603. if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
  3604. ioc->config_cmds.status |= MPT2_CMD_RESET;
  3605. mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
  3606. ioc->config_cmds.smid = USHRT_MAX;
  3607. complete(&ioc->config_cmds.done);
  3608. }
  3609. break;
  3610. case MPT2_IOC_DONE_RESET:
  3611. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
  3612. "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
  3613. break;
  3614. }
  3615. }
  3616. /**
  3617. * _wait_for_commands_to_complete - reset controller
  3618. * @ioc: Pointer to MPT_ADAPTER structure
  3619. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3620. *
  3621. * This function waiting(3s) for all pending commands to complete
  3622. * prior to putting controller in reset.
  3623. */
  3624. static void
  3625. _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
  3626. {
  3627. u32 ioc_state;
  3628. unsigned long flags;
  3629. u16 i;
  3630. ioc->pending_io_count = 0;
  3631. if (sleep_flag != CAN_SLEEP)
  3632. return;
  3633. ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
  3634. if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
  3635. return;
  3636. /* pending command count */
  3637. spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
  3638. for (i = 0; i < ioc->scsiio_depth; i++)
  3639. if (ioc->scsi_lookup[i].cb_idx != 0xFF)
  3640. ioc->pending_io_count++;
  3641. spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
  3642. if (!ioc->pending_io_count)
  3643. return;
  3644. /* wait for pending commands to complete */
  3645. wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
  3646. }
  3647. /**
  3648. * mpt2sas_base_hard_reset_handler - reset controller
  3649. * @ioc: Pointer to MPT_ADAPTER structure
  3650. * @sleep_flag: CAN_SLEEP or NO_SLEEP
  3651. * @type: FORCE_BIG_HAMMER or SOFT_RESET
  3652. *
  3653. * Returns 0 for success, non-zero for failure.
  3654. */
  3655. int
  3656. mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
  3657. enum reset_type type)
  3658. {
  3659. int r;
  3660. unsigned long flags;
  3661. u8 pe_complete = ioc->wait_for_port_enable_to_complete;
  3662. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
  3663. __func__));
  3664. if (ioc->pci_error_recovery) {
  3665. printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
  3666. ioc->name, __func__);
  3667. r = 0;
  3668. goto out;
  3669. }
  3670. if (mpt2sas_fwfault_debug)
  3671. mpt2sas_halt_firmware(ioc);
  3672. /* TODO - What we really should be doing is pulling
  3673. * out all the code associated with NO_SLEEP; its never used.
  3674. * That is legacy code from mpt fusion driver, ported over.
  3675. * I will leave this BUG_ON here for now till its been resolved.
  3676. */
  3677. BUG_ON(sleep_flag == NO_SLEEP);
  3678. /* wait for an active reset in progress to complete */
  3679. if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
  3680. do {
  3681. ssleep(1);
  3682. } while (ioc->shost_recovery == 1);
  3683. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
  3684. __func__));
  3685. return ioc->ioc_reset_in_progress_status;
  3686. }
  3687. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  3688. ioc->shost_recovery = 1;
  3689. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  3690. _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
  3691. _wait_for_commands_to_complete(ioc, sleep_flag);
  3692. _base_mask_interrupts(ioc);
  3693. r = _base_make_ioc_ready(ioc, sleep_flag, type);
  3694. if (r)
  3695. goto out;
  3696. _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
  3697. /* If this hard reset is called while port enable is active, then
  3698. * there is no reason to call make_ioc_operational
  3699. */
  3700. if (pe_complete) {
  3701. r = -EFAULT;
  3702. goto out;
  3703. }
  3704. r = _base_make_ioc_operational(ioc, sleep_flag);
  3705. if (!r)
  3706. _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
  3707. out:
  3708. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
  3709. ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
  3710. spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
  3711. ioc->ioc_reset_in_progress_status = r;
  3712. ioc->shost_recovery = 0;
  3713. complete(&ioc->shost_recovery_done);
  3714. spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
  3715. mutex_unlock(&ioc->reset_in_progress_mutex);
  3716. dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
  3717. __func__));
  3718. return r;
  3719. }