mpt2sas_base.c 116 KB

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