mpt2sas_base.c 108 KB

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