mpt2sas_base.c 107 KB

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