mpt2sas_base.c 106 KB

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