mpt2sas_base.c 107 KB

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