mpt2sas_base.c 106 KB

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