megaraid_sas_fusion.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2013 LSI Corporation
  5. * Copyright (c) 2013-2014 Avago Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * FILE: megaraid_sas_fusion.c
  21. *
  22. * Authors: Avago Technologies
  23. * Sumant Patro
  24. * Adam Radford
  25. * Kashyap Desai <kashyap.desai@avagotech.com>
  26. * Sumit Saxena <sumit.saxena@avagotech.com>
  27. *
  28. * Send feedback to: megaraidlinux.pdl@avagotech.com
  29. *
  30. * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
  31. * San Jose, California 95131
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/list.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/uio.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/fs.h>
  45. #include <linux/compat.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/mutex.h>
  48. #include <linux/poll.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_dbg.h>
  54. #include <linux/dmi.h>
  55. #include "megaraid_sas_fusion.h"
  56. #include "megaraid_sas.h"
  57. extern void megasas_free_cmds(struct megasas_instance *instance);
  58. extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  59. *instance);
  60. extern void
  61. megasas_complete_cmd(struct megasas_instance *instance,
  62. struct megasas_cmd *cmd, u8 alt_status);
  63. int
  64. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  65. int seconds);
  66. void
  67. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
  68. int megasas_alloc_cmds(struct megasas_instance *instance);
  69. int
  70. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
  71. int
  72. megasas_issue_polled(struct megasas_instance *instance,
  73. struct megasas_cmd *cmd);
  74. void
  75. megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
  76. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  77. void megaraid_sas_kill_hba(struct megasas_instance *instance);
  78. extern u32 megasas_dbg_lvl;
  79. void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
  80. int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
  81. int initial);
  82. void megasas_start_timer(struct megasas_instance *instance,
  83. struct timer_list *timer,
  84. void *fn, unsigned long interval);
  85. extern struct megasas_mgmt_info megasas_mgmt_info;
  86. extern unsigned int resetwaittime;
  87. extern unsigned int dual_qdepth_disable;
  88. static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
  89. static void megasas_free_reply_fusion(struct megasas_instance *instance);
  90. /**
  91. * megasas_enable_intr_fusion - Enables interrupts
  92. * @regs: MFI register set
  93. */
  94. void
  95. megasas_enable_intr_fusion(struct megasas_instance *instance)
  96. {
  97. struct megasas_register_set __iomem *regs;
  98. regs = instance->reg_set;
  99. instance->mask_interrupts = 0;
  100. /* For Thunderbolt/Invader also clear intr on enable */
  101. writel(~0, &regs->outbound_intr_status);
  102. readl(&regs->outbound_intr_status);
  103. writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  104. /* Dummy readl to force pci flush */
  105. readl(&regs->outbound_intr_mask);
  106. }
  107. /**
  108. * megasas_disable_intr_fusion - Disables interrupt
  109. * @regs: MFI register set
  110. */
  111. void
  112. megasas_disable_intr_fusion(struct megasas_instance *instance)
  113. {
  114. u32 mask = 0xFFFFFFFF;
  115. u32 status;
  116. struct megasas_register_set __iomem *regs;
  117. regs = instance->reg_set;
  118. instance->mask_interrupts = 1;
  119. writel(mask, &regs->outbound_intr_mask);
  120. /* Dummy readl to force pci flush */
  121. status = readl(&regs->outbound_intr_mask);
  122. }
  123. int
  124. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
  125. {
  126. u32 status;
  127. /*
  128. * Check if it is our interrupt
  129. */
  130. status = readl(&regs->outbound_intr_status);
  131. if (status & 1) {
  132. writel(status, &regs->outbound_intr_status);
  133. readl(&regs->outbound_intr_status);
  134. return 1;
  135. }
  136. if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
  137. return 0;
  138. return 1;
  139. }
  140. /**
  141. * megasas_get_cmd_fusion - Get a command from the free pool
  142. * @instance: Adapter soft state
  143. *
  144. * Returns a blk_tag indexed mpt frame
  145. */
  146. inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
  147. *instance, u32 blk_tag)
  148. {
  149. struct fusion_context *fusion;
  150. fusion = instance->ctrl_context;
  151. return fusion->cmd_list[blk_tag];
  152. }
  153. /**
  154. * megasas_return_cmd_fusion - Return a cmd to free command pool
  155. * @instance: Adapter soft state
  156. * @cmd: Command packet to be returned to free command pool
  157. */
  158. inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
  159. struct megasas_cmd_fusion *cmd)
  160. {
  161. cmd->scmd = NULL;
  162. memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
  163. }
  164. /**
  165. * megasas_fire_cmd_fusion - Sends command to the FW
  166. */
  167. static void
  168. megasas_fire_cmd_fusion(struct megasas_instance *instance,
  169. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
  170. {
  171. #if defined(writeq) && defined(CONFIG_64BIT)
  172. u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
  173. le32_to_cpu(req_desc->u.low));
  174. writeq(req_data, &instance->reg_set->inbound_low_queue_port);
  175. #else
  176. unsigned long flags;
  177. spin_lock_irqsave(&instance->hba_lock, flags);
  178. writel(le32_to_cpu(req_desc->u.low),
  179. &instance->reg_set->inbound_low_queue_port);
  180. writel(le32_to_cpu(req_desc->u.high),
  181. &instance->reg_set->inbound_high_queue_port);
  182. mmiowb();
  183. spin_unlock_irqrestore(&instance->hba_lock, flags);
  184. #endif
  185. }
  186. /**
  187. * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
  188. * @instance: Adapter soft state
  189. * fw_boot_context: Whether this function called during probe or after OCR
  190. *
  191. * This function is only for fusion controllers.
  192. * Update host can queue, if firmware downgrade max supported firmware commands.
  193. * Firmware upgrade case will be skiped because underlying firmware has
  194. * more resource than exposed to the OS.
  195. *
  196. */
  197. static void
  198. megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
  199. {
  200. u16 cur_max_fw_cmds = 0;
  201. u16 ldio_threshold = 0;
  202. struct megasas_register_set __iomem *reg_set;
  203. reg_set = instance->reg_set;
  204. cur_max_fw_cmds = readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
  205. if (dual_qdepth_disable || !cur_max_fw_cmds)
  206. cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  207. else
  208. ldio_threshold =
  209. (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
  210. dev_info(&instance->pdev->dev,
  211. "Current firmware maximum commands: %d\t LDIO threshold: %d\n",
  212. cur_max_fw_cmds, ldio_threshold);
  213. if (fw_boot_context == OCR_CONTEXT) {
  214. cur_max_fw_cmds = cur_max_fw_cmds - 1;
  215. if (cur_max_fw_cmds <= instance->max_fw_cmds) {
  216. instance->cur_can_queue =
  217. cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
  218. MEGASAS_FUSION_IOCTL_CMDS);
  219. instance->host->can_queue = instance->cur_can_queue;
  220. instance->ldio_threshold = ldio_threshold;
  221. }
  222. } else {
  223. instance->max_fw_cmds = cur_max_fw_cmds;
  224. instance->ldio_threshold = ldio_threshold;
  225. if (!instance->is_rdpq)
  226. instance->max_fw_cmds = min_t(u16, instance->max_fw_cmds, 1024);
  227. if (reset_devices)
  228. instance->max_fw_cmds = min(instance->max_fw_cmds,
  229. (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
  230. /*
  231. * Reduce the max supported cmds by 1. This is to ensure that the
  232. * reply_q_sz (1 more than the max cmd that driver may send)
  233. * does not exceed max cmds that the FW can support
  234. */
  235. instance->max_fw_cmds = instance->max_fw_cmds-1;
  236. instance->max_scsi_cmds = instance->max_fw_cmds -
  237. (MEGASAS_FUSION_INTERNAL_CMDS +
  238. MEGASAS_FUSION_IOCTL_CMDS);
  239. instance->cur_can_queue = instance->max_scsi_cmds;
  240. }
  241. }
  242. /**
  243. * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
  244. * @instance: Adapter soft state
  245. */
  246. void
  247. megasas_free_cmds_fusion(struct megasas_instance *instance)
  248. {
  249. int i;
  250. struct fusion_context *fusion = instance->ctrl_context;
  251. struct megasas_cmd_fusion *cmd;
  252. /* SG, Sense */
  253. for (i = 0; i < instance->max_fw_cmds; i++) {
  254. cmd = fusion->cmd_list[i];
  255. if (cmd) {
  256. if (cmd->sg_frame)
  257. pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
  258. cmd->sg_frame_phys_addr);
  259. if (cmd->sense)
  260. pci_pool_free(fusion->sense_dma_pool, cmd->sense,
  261. cmd->sense_phys_addr);
  262. }
  263. }
  264. if (fusion->sg_dma_pool) {
  265. pci_pool_destroy(fusion->sg_dma_pool);
  266. fusion->sg_dma_pool = NULL;
  267. }
  268. if (fusion->sense_dma_pool) {
  269. pci_pool_destroy(fusion->sense_dma_pool);
  270. fusion->sense_dma_pool = NULL;
  271. }
  272. /* Reply Frame, Desc*/
  273. if (instance->is_rdpq)
  274. megasas_free_rdpq_fusion(instance);
  275. else
  276. megasas_free_reply_fusion(instance);
  277. /* Request Frame, Desc*/
  278. if (fusion->req_frames_desc)
  279. dma_free_coherent(&instance->pdev->dev,
  280. fusion->request_alloc_sz, fusion->req_frames_desc,
  281. fusion->req_frames_desc_phys);
  282. if (fusion->io_request_frames)
  283. pci_pool_free(fusion->io_request_frames_pool,
  284. fusion->io_request_frames,
  285. fusion->io_request_frames_phys);
  286. if (fusion->io_request_frames_pool) {
  287. pci_pool_destroy(fusion->io_request_frames_pool);
  288. fusion->io_request_frames_pool = NULL;
  289. }
  290. /* cmd_list */
  291. for (i = 0; i < instance->max_fw_cmds; i++)
  292. kfree(fusion->cmd_list[i]);
  293. kfree(fusion->cmd_list);
  294. }
  295. /**
  296. * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
  297. * @instance: Adapter soft state
  298. *
  299. */
  300. static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
  301. {
  302. int i;
  303. u32 max_cmd;
  304. struct fusion_context *fusion;
  305. struct megasas_cmd_fusion *cmd;
  306. fusion = instance->ctrl_context;
  307. max_cmd = instance->max_fw_cmds;
  308. fusion->sg_dma_pool =
  309. pci_pool_create("mr_sg", instance->pdev,
  310. instance->max_chain_frame_sz, 4, 0);
  311. /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
  312. fusion->sense_dma_pool =
  313. pci_pool_create("mr_sense", instance->pdev,
  314. SCSI_SENSE_BUFFERSIZE, 64, 0);
  315. if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
  316. dev_err(&instance->pdev->dev,
  317. "Failed from %s %d\n", __func__, __LINE__);
  318. return -ENOMEM;
  319. }
  320. /*
  321. * Allocate and attach a frame to each of the commands in cmd_list
  322. */
  323. for (i = 0; i < max_cmd; i++) {
  324. cmd = fusion->cmd_list[i];
  325. cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
  326. GFP_KERNEL, &cmd->sg_frame_phys_addr);
  327. cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
  328. GFP_KERNEL, &cmd->sense_phys_addr);
  329. if (!cmd->sg_frame || !cmd->sense) {
  330. dev_err(&instance->pdev->dev,
  331. "Failed from %s %d\n", __func__, __LINE__);
  332. return -ENOMEM;
  333. }
  334. }
  335. return 0;
  336. }
  337. int
  338. megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
  339. {
  340. u32 max_cmd, i;
  341. struct fusion_context *fusion;
  342. fusion = instance->ctrl_context;
  343. max_cmd = instance->max_fw_cmds;
  344. /*
  345. * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
  346. * Allocate the dynamic array first and then allocate individual
  347. * commands.
  348. */
  349. fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *) * max_cmd,
  350. GFP_KERNEL);
  351. if (!fusion->cmd_list) {
  352. dev_err(&instance->pdev->dev,
  353. "Failed from %s %d\n", __func__, __LINE__);
  354. return -ENOMEM;
  355. }
  356. for (i = 0; i < max_cmd; i++) {
  357. fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
  358. GFP_KERNEL);
  359. if (!fusion->cmd_list[i]) {
  360. dev_err(&instance->pdev->dev,
  361. "Failed from %s %d\n", __func__, __LINE__);
  362. return -ENOMEM;
  363. }
  364. }
  365. return 0;
  366. }
  367. int
  368. megasas_alloc_request_fusion(struct megasas_instance *instance)
  369. {
  370. struct fusion_context *fusion;
  371. fusion = instance->ctrl_context;
  372. fusion->req_frames_desc =
  373. dma_alloc_coherent(&instance->pdev->dev,
  374. fusion->request_alloc_sz,
  375. &fusion->req_frames_desc_phys, GFP_KERNEL);
  376. if (!fusion->req_frames_desc) {
  377. dev_err(&instance->pdev->dev,
  378. "Failed from %s %d\n", __func__, __LINE__);
  379. return -ENOMEM;
  380. }
  381. fusion->io_request_frames_pool =
  382. pci_pool_create("mr_ioreq", instance->pdev,
  383. fusion->io_frames_alloc_sz, 16, 0);
  384. if (!fusion->io_request_frames_pool) {
  385. dev_err(&instance->pdev->dev,
  386. "Failed from %s %d\n", __func__, __LINE__);
  387. return -ENOMEM;
  388. }
  389. fusion->io_request_frames =
  390. pci_pool_alloc(fusion->io_request_frames_pool,
  391. GFP_KERNEL, &fusion->io_request_frames_phys);
  392. if (!fusion->io_request_frames) {
  393. dev_err(&instance->pdev->dev,
  394. "Failed from %s %d\n", __func__, __LINE__);
  395. return -ENOMEM;
  396. }
  397. return 0;
  398. }
  399. int
  400. megasas_alloc_reply_fusion(struct megasas_instance *instance)
  401. {
  402. int i, count;
  403. struct fusion_context *fusion;
  404. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  405. fusion = instance->ctrl_context;
  406. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  407. fusion->reply_frames_desc_pool =
  408. pci_pool_create("mr_reply", instance->pdev,
  409. fusion->reply_alloc_sz * count, 16, 0);
  410. if (!fusion->reply_frames_desc_pool) {
  411. dev_err(&instance->pdev->dev,
  412. "Failed from %s %d\n", __func__, __LINE__);
  413. return -ENOMEM;
  414. }
  415. fusion->reply_frames_desc[0] =
  416. pci_pool_alloc(fusion->reply_frames_desc_pool,
  417. GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
  418. if (!fusion->reply_frames_desc[0]) {
  419. dev_err(&instance->pdev->dev,
  420. "Failed from %s %d\n", __func__, __LINE__);
  421. return -ENOMEM;
  422. }
  423. reply_desc = fusion->reply_frames_desc[0];
  424. for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
  425. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  426. /* This is not a rdpq mode, but driver still populate
  427. * reply_frame_desc array to use same msix index in ISR path.
  428. */
  429. for (i = 0; i < (count - 1); i++)
  430. fusion->reply_frames_desc[i + 1] =
  431. fusion->reply_frames_desc[i] +
  432. (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
  433. return 0;
  434. }
  435. int
  436. megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
  437. {
  438. int i, j, count;
  439. struct fusion_context *fusion;
  440. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  441. fusion = instance->ctrl_context;
  442. fusion->rdpq_virt = pci_alloc_consistent(instance->pdev,
  443. sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
  444. &fusion->rdpq_phys);
  445. if (!fusion->rdpq_virt) {
  446. dev_err(&instance->pdev->dev,
  447. "Failed from %s %d\n", __func__, __LINE__);
  448. return -ENOMEM;
  449. }
  450. memset(fusion->rdpq_virt, 0,
  451. sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION);
  452. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  453. fusion->reply_frames_desc_pool = pci_pool_create("mr_rdpq",
  454. instance->pdev, fusion->reply_alloc_sz, 16, 0);
  455. if (!fusion->reply_frames_desc_pool) {
  456. dev_err(&instance->pdev->dev,
  457. "Failed from %s %d\n", __func__, __LINE__);
  458. return -ENOMEM;
  459. }
  460. for (i = 0; i < count; i++) {
  461. fusion->reply_frames_desc[i] =
  462. pci_pool_alloc(fusion->reply_frames_desc_pool,
  463. GFP_KERNEL, &fusion->reply_frames_desc_phys[i]);
  464. if (!fusion->reply_frames_desc[i]) {
  465. dev_err(&instance->pdev->dev,
  466. "Failed from %s %d\n", __func__, __LINE__);
  467. return -ENOMEM;
  468. }
  469. fusion->rdpq_virt[i].RDPQBaseAddress =
  470. fusion->reply_frames_desc_phys[i];
  471. reply_desc = fusion->reply_frames_desc[i];
  472. for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
  473. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  474. }
  475. return 0;
  476. }
  477. static void
  478. megasas_free_rdpq_fusion(struct megasas_instance *instance) {
  479. int i;
  480. struct fusion_context *fusion;
  481. fusion = instance->ctrl_context;
  482. for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++) {
  483. if (fusion->reply_frames_desc[i])
  484. pci_pool_free(fusion->reply_frames_desc_pool,
  485. fusion->reply_frames_desc[i],
  486. fusion->reply_frames_desc_phys[i]);
  487. }
  488. if (fusion->reply_frames_desc_pool)
  489. pci_pool_destroy(fusion->reply_frames_desc_pool);
  490. if (fusion->rdpq_virt)
  491. pci_free_consistent(instance->pdev,
  492. sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
  493. fusion->rdpq_virt, fusion->rdpq_phys);
  494. }
  495. static void
  496. megasas_free_reply_fusion(struct megasas_instance *instance) {
  497. struct fusion_context *fusion;
  498. fusion = instance->ctrl_context;
  499. if (fusion->reply_frames_desc[0])
  500. pci_pool_free(fusion->reply_frames_desc_pool,
  501. fusion->reply_frames_desc[0],
  502. fusion->reply_frames_desc_phys[0]);
  503. if (fusion->reply_frames_desc_pool)
  504. pci_pool_destroy(fusion->reply_frames_desc_pool);
  505. }
  506. /**
  507. * megasas_alloc_cmds_fusion - Allocates the command packets
  508. * @instance: Adapter soft state
  509. *
  510. *
  511. * Each frame has a 32-bit field called context. This context is used to get
  512. * back the megasas_cmd_fusion from the frame when a frame gets completed
  513. * In this driver, the 32 bit values are the indices into an array cmd_list.
  514. * This array is used only to look up the megasas_cmd_fusion given the context.
  515. * The free commands themselves are maintained in a linked list called cmd_pool.
  516. *
  517. * cmds are formed in the io_request and sg_frame members of the
  518. * megasas_cmd_fusion. The context field is used to get a request descriptor
  519. * and is used as SMID of the cmd.
  520. * SMID value range is from 1 to max_fw_cmds.
  521. */
  522. int
  523. megasas_alloc_cmds_fusion(struct megasas_instance *instance)
  524. {
  525. int i;
  526. struct fusion_context *fusion;
  527. struct megasas_cmd_fusion *cmd;
  528. u32 offset;
  529. dma_addr_t io_req_base_phys;
  530. u8 *io_req_base;
  531. fusion = instance->ctrl_context;
  532. if (megasas_alloc_cmdlist_fusion(instance))
  533. goto fail_exit;
  534. if (megasas_alloc_request_fusion(instance))
  535. goto fail_exit;
  536. if (instance->is_rdpq) {
  537. if (megasas_alloc_rdpq_fusion(instance))
  538. goto fail_exit;
  539. } else
  540. if (megasas_alloc_reply_fusion(instance))
  541. goto fail_exit;
  542. /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
  543. io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  544. io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  545. /*
  546. * Add all the commands to command pool (fusion->cmd_pool)
  547. */
  548. /* SMID 0 is reserved. Set SMID/index from 1 */
  549. for (i = 0; i < instance->max_fw_cmds; i++) {
  550. cmd = fusion->cmd_list[i];
  551. offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
  552. memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
  553. cmd->index = i + 1;
  554. cmd->scmd = NULL;
  555. cmd->sync_cmd_idx = (i >= instance->max_scsi_cmds) ?
  556. (i - instance->max_scsi_cmds) :
  557. (u32)ULONG_MAX; /* Set to Invalid */
  558. cmd->instance = instance;
  559. cmd->io_request =
  560. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  561. (io_req_base + offset);
  562. memset(cmd->io_request, 0,
  563. sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
  564. cmd->io_request_phys_addr = io_req_base_phys + offset;
  565. }
  566. if (megasas_create_sg_sense_fusion(instance))
  567. goto fail_exit;
  568. return 0;
  569. fail_exit:
  570. megasas_free_cmds_fusion(instance);
  571. return -ENOMEM;
  572. }
  573. /**
  574. * wait_and_poll - Issues a polling command
  575. * @instance: Adapter soft state
  576. * @cmd: Command packet to be issued
  577. *
  578. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  579. */
  580. int
  581. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  582. int seconds)
  583. {
  584. int i;
  585. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  586. struct fusion_context *fusion;
  587. u32 msecs = seconds * 1000;
  588. fusion = instance->ctrl_context;
  589. /*
  590. * Wait for cmd_status to change
  591. */
  592. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
  593. rmb();
  594. msleep(20);
  595. }
  596. if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
  597. return DCMD_TIMEOUT;
  598. else if (frame_hdr->cmd_status == MFI_STAT_OK)
  599. return DCMD_SUCCESS;
  600. else
  601. return DCMD_FAILED;
  602. }
  603. /**
  604. * megasas_ioc_init_fusion - Initializes the FW
  605. * @instance: Adapter soft state
  606. *
  607. * Issues the IOC Init cmd
  608. */
  609. int
  610. megasas_ioc_init_fusion(struct megasas_instance *instance)
  611. {
  612. struct megasas_init_frame *init_frame;
  613. struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
  614. dma_addr_t ioc_init_handle;
  615. struct megasas_cmd *cmd;
  616. u8 ret, cur_rdpq_mode;
  617. struct fusion_context *fusion;
  618. union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
  619. int i;
  620. struct megasas_header *frame_hdr;
  621. const char *sys_info;
  622. MFI_CAPABILITIES *drv_ops;
  623. u32 scratch_pad_2;
  624. fusion = instance->ctrl_context;
  625. cmd = megasas_get_cmd(instance);
  626. if (!cmd) {
  627. dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
  628. ret = 1;
  629. goto fail_get_cmd;
  630. }
  631. scratch_pad_2 = readl
  632. (&instance->reg_set->outbound_scratch_pad_2);
  633. cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
  634. if (instance->is_rdpq && !cur_rdpq_mode) {
  635. dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
  636. " from RDPQ mode to non RDPQ mode\n");
  637. ret = 1;
  638. goto fail_fw_init;
  639. }
  640. IOCInitMessage =
  641. dma_alloc_coherent(&instance->pdev->dev,
  642. sizeof(struct MPI2_IOC_INIT_REQUEST),
  643. &ioc_init_handle, GFP_KERNEL);
  644. if (!IOCInitMessage) {
  645. dev_err(&instance->pdev->dev, "Could not allocate memory for "
  646. "IOCInitMessage\n");
  647. ret = 1;
  648. goto fail_fw_init;
  649. }
  650. memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
  651. IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
  652. IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  653. IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
  654. IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  655. IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
  656. IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
  657. IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
  658. cpu_to_le64(fusion->rdpq_phys) :
  659. cpu_to_le64(fusion->reply_frames_desc_phys[0]);
  660. IOCInitMessage->MsgFlags = instance->is_rdpq ?
  661. MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
  662. IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
  663. IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
  664. init_frame = (struct megasas_init_frame *)cmd->frame;
  665. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  666. frame_hdr = &cmd->frame->hdr;
  667. frame_hdr->cmd_status = 0xFF;
  668. frame_hdr->flags = cpu_to_le16(
  669. le16_to_cpu(frame_hdr->flags) |
  670. MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  671. init_frame->cmd = MFI_CMD_INIT;
  672. init_frame->cmd_status = 0xFF;
  673. drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
  674. /* driver support Extended MSIX */
  675. if (fusion->adapter_type == INVADER_SERIES)
  676. drv_ops->mfi_capabilities.support_additional_msix = 1;
  677. /* driver supports HA / Remote LUN over Fast Path interface */
  678. drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
  679. drv_ops->mfi_capabilities.support_max_255lds = 1;
  680. drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
  681. drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
  682. if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
  683. drv_ops->mfi_capabilities.support_ext_io_size = 1;
  684. drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
  685. if (!dual_qdepth_disable)
  686. drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
  687. drv_ops->mfi_capabilities.support_qd_throttling = 1;
  688. /* Convert capability to LE32 */
  689. cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
  690. sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
  691. if (instance->system_info_buf && sys_info) {
  692. memcpy(instance->system_info_buf->systemId, sys_info,
  693. strlen(sys_info) > 64 ? 64 : strlen(sys_info));
  694. instance->system_info_buf->systemIdLength =
  695. strlen(sys_info) > 64 ? 64 : strlen(sys_info);
  696. init_frame->system_info_lo = instance->system_info_h;
  697. init_frame->system_info_hi = 0;
  698. }
  699. init_frame->queue_info_new_phys_addr_hi =
  700. cpu_to_le32(upper_32_bits(ioc_init_handle));
  701. init_frame->queue_info_new_phys_addr_lo =
  702. cpu_to_le32(lower_32_bits(ioc_init_handle));
  703. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
  704. req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
  705. req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
  706. req_desc.MFAIo.RequestFlags =
  707. (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
  708. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  709. /*
  710. * disable the intr before firing the init frame
  711. */
  712. instance->instancet->disable_intr(instance);
  713. for (i = 0; i < (10 * 1000); i += 20) {
  714. if (readl(&instance->reg_set->doorbell) & 1)
  715. msleep(20);
  716. else
  717. break;
  718. }
  719. megasas_fire_cmd_fusion(instance, &req_desc);
  720. wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
  721. frame_hdr = &cmd->frame->hdr;
  722. if (frame_hdr->cmd_status != 0) {
  723. ret = 1;
  724. goto fail_fw_init;
  725. }
  726. dev_info(&instance->pdev->dev, "Init cmd success\n");
  727. ret = 0;
  728. fail_fw_init:
  729. megasas_return_cmd(instance, cmd);
  730. if (IOCInitMessage)
  731. dma_free_coherent(&instance->pdev->dev,
  732. sizeof(struct MPI2_IOC_INIT_REQUEST),
  733. IOCInitMessage, ioc_init_handle);
  734. fail_get_cmd:
  735. return ret;
  736. }
  737. /**
  738. * megasas_sync_pd_seq_num - JBOD SEQ MAP
  739. * @instance: Adapter soft state
  740. * @pend: set to 1, if it is pended jbod map.
  741. *
  742. * Issue Jbod map to the firmware. If it is pended command,
  743. * issue command and return. If it is first instance of jbod map
  744. * issue and receive command.
  745. */
  746. int
  747. megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
  748. int ret = 0;
  749. u32 pd_seq_map_sz;
  750. struct megasas_cmd *cmd;
  751. struct megasas_dcmd_frame *dcmd;
  752. struct fusion_context *fusion = instance->ctrl_context;
  753. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  754. dma_addr_t pd_seq_h;
  755. pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
  756. pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
  757. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  758. (sizeof(struct MR_PD_CFG_SEQ) *
  759. (MAX_PHYSICAL_DEVICES - 1));
  760. cmd = megasas_get_cmd(instance);
  761. if (!cmd) {
  762. dev_err(&instance->pdev->dev,
  763. "Could not get mfi cmd. Fail from %s %d\n",
  764. __func__, __LINE__);
  765. return -ENOMEM;
  766. }
  767. dcmd = &cmd->frame->dcmd;
  768. memset(pd_sync, 0, pd_seq_map_sz);
  769. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  770. dcmd->cmd = MFI_CMD_DCMD;
  771. dcmd->cmd_status = 0xFF;
  772. dcmd->sge_count = 1;
  773. dcmd->timeout = 0;
  774. dcmd->pad_0 = 0;
  775. dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
  776. dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
  777. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
  778. dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
  779. if (pend) {
  780. dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  781. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
  782. instance->jbod_seq_cmd = cmd;
  783. instance->instancet->issue_dcmd(instance, cmd);
  784. return 0;
  785. }
  786. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  787. /* Below code is only for non pended DCMD */
  788. if (instance->ctrl_context && !instance->mask_interrupts)
  789. ret = megasas_issue_blocked_cmd(instance, cmd,
  790. MFI_IO_TIMEOUT_SECS);
  791. else
  792. ret = megasas_issue_polled(instance, cmd);
  793. if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
  794. dev_warn(&instance->pdev->dev,
  795. "driver supports max %d JBOD, but FW reports %d\n",
  796. MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
  797. ret = -EINVAL;
  798. }
  799. if (ret == DCMD_TIMEOUT && instance->ctrl_context)
  800. megaraid_sas_kill_hba(instance);
  801. if (ret == DCMD_SUCCESS)
  802. instance->pd_seq_map_id++;
  803. megasas_return_cmd(instance, cmd);
  804. return ret;
  805. }
  806. /*
  807. * megasas_get_ld_map_info - Returns FW's ld_map structure
  808. * @instance: Adapter soft state
  809. * @pend: Pend the command or not
  810. * Issues an internal command (DCMD) to get the FW's controller PD
  811. * list structure. This information is mainly used to find out SYSTEM
  812. * supported by the FW.
  813. * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
  814. * dcmd.mbox.b[0] - number of LDs being sync'd
  815. * dcmd.mbox.b[1] - 0 - complete command immediately.
  816. * - 1 - pend till config change
  817. * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
  818. * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
  819. * uses extended struct MR_FW_RAID_MAP_EXT
  820. */
  821. static int
  822. megasas_get_ld_map_info(struct megasas_instance *instance)
  823. {
  824. int ret = 0;
  825. struct megasas_cmd *cmd;
  826. struct megasas_dcmd_frame *dcmd;
  827. void *ci;
  828. dma_addr_t ci_h = 0;
  829. u32 size_map_info;
  830. struct fusion_context *fusion;
  831. cmd = megasas_get_cmd(instance);
  832. if (!cmd) {
  833. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
  834. return -ENOMEM;
  835. }
  836. fusion = instance->ctrl_context;
  837. if (!fusion) {
  838. megasas_return_cmd(instance, cmd);
  839. return -ENXIO;
  840. }
  841. dcmd = &cmd->frame->dcmd;
  842. size_map_info = fusion->current_map_sz;
  843. ci = (void *) fusion->ld_map[(instance->map_id & 1)];
  844. ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
  845. if (!ci) {
  846. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
  847. megasas_return_cmd(instance, cmd);
  848. return -ENOMEM;
  849. }
  850. memset(ci, 0, fusion->max_map_sz);
  851. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  852. #if VD_EXT_DEBUG
  853. dev_dbg(&instance->pdev->dev,
  854. "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
  855. __func__, cpu_to_le32(size_map_info));
  856. #endif
  857. dcmd->cmd = MFI_CMD_DCMD;
  858. dcmd->cmd_status = 0xFF;
  859. dcmd->sge_count = 1;
  860. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  861. dcmd->timeout = 0;
  862. dcmd->pad_0 = 0;
  863. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  864. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  865. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  866. dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
  867. if (instance->ctrl_context && !instance->mask_interrupts)
  868. ret = megasas_issue_blocked_cmd(instance, cmd,
  869. MFI_IO_TIMEOUT_SECS);
  870. else
  871. ret = megasas_issue_polled(instance, cmd);
  872. if (ret == DCMD_TIMEOUT && instance->ctrl_context)
  873. megaraid_sas_kill_hba(instance);
  874. megasas_return_cmd(instance, cmd);
  875. return ret;
  876. }
  877. u8
  878. megasas_get_map_info(struct megasas_instance *instance)
  879. {
  880. struct fusion_context *fusion = instance->ctrl_context;
  881. fusion->fast_path_io = 0;
  882. if (!megasas_get_ld_map_info(instance)) {
  883. if (MR_ValidateMapInfo(instance)) {
  884. fusion->fast_path_io = 1;
  885. return 0;
  886. }
  887. }
  888. return 1;
  889. }
  890. /*
  891. * megasas_sync_map_info - Returns FW's ld_map structure
  892. * @instance: Adapter soft state
  893. *
  894. * Issues an internal command (DCMD) to get the FW's controller PD
  895. * list structure. This information is mainly used to find out SYSTEM
  896. * supported by the FW.
  897. */
  898. int
  899. megasas_sync_map_info(struct megasas_instance *instance)
  900. {
  901. int ret = 0, i;
  902. struct megasas_cmd *cmd;
  903. struct megasas_dcmd_frame *dcmd;
  904. u32 size_sync_info, num_lds;
  905. struct fusion_context *fusion;
  906. struct MR_LD_TARGET_SYNC *ci = NULL;
  907. struct MR_DRV_RAID_MAP_ALL *map;
  908. struct MR_LD_RAID *raid;
  909. struct MR_LD_TARGET_SYNC *ld_sync;
  910. dma_addr_t ci_h = 0;
  911. u32 size_map_info;
  912. cmd = megasas_get_cmd(instance);
  913. if (!cmd) {
  914. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
  915. return -ENOMEM;
  916. }
  917. fusion = instance->ctrl_context;
  918. if (!fusion) {
  919. megasas_return_cmd(instance, cmd);
  920. return 1;
  921. }
  922. map = fusion->ld_drv_map[instance->map_id & 1];
  923. num_lds = le16_to_cpu(map->raidMap.ldCount);
  924. dcmd = &cmd->frame->dcmd;
  925. size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
  926. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  927. ci = (struct MR_LD_TARGET_SYNC *)
  928. fusion->ld_map[(instance->map_id - 1) & 1];
  929. memset(ci, 0, fusion->max_map_sz);
  930. ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
  931. ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
  932. for (i = 0; i < num_lds; i++, ld_sync++) {
  933. raid = MR_LdRaidGet(i, map);
  934. ld_sync->targetId = MR_GetLDTgtId(i, map);
  935. ld_sync->seqNum = raid->seqNum;
  936. }
  937. size_map_info = fusion->current_map_sz;
  938. dcmd->cmd = MFI_CMD_DCMD;
  939. dcmd->cmd_status = 0xFF;
  940. dcmd->sge_count = 1;
  941. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
  942. dcmd->timeout = 0;
  943. dcmd->pad_0 = 0;
  944. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  945. dcmd->mbox.b[0] = num_lds;
  946. dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  947. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  948. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  949. dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
  950. instance->map_update_cmd = cmd;
  951. instance->instancet->issue_dcmd(instance, cmd);
  952. return ret;
  953. }
  954. /*
  955. * meagasas_display_intel_branding - Display branding string
  956. * @instance: per adapter object
  957. *
  958. * Return nothing.
  959. */
  960. static void
  961. megasas_display_intel_branding(struct megasas_instance *instance)
  962. {
  963. if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  964. return;
  965. switch (instance->pdev->device) {
  966. case PCI_DEVICE_ID_LSI_INVADER:
  967. switch (instance->pdev->subsystem_device) {
  968. case MEGARAID_INTEL_RS3DC080_SSDID:
  969. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  970. instance->host->host_no,
  971. MEGARAID_INTEL_RS3DC080_BRANDING);
  972. break;
  973. case MEGARAID_INTEL_RS3DC040_SSDID:
  974. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  975. instance->host->host_no,
  976. MEGARAID_INTEL_RS3DC040_BRANDING);
  977. break;
  978. case MEGARAID_INTEL_RS3SC008_SSDID:
  979. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  980. instance->host->host_no,
  981. MEGARAID_INTEL_RS3SC008_BRANDING);
  982. break;
  983. case MEGARAID_INTEL_RS3MC044_SSDID:
  984. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  985. instance->host->host_no,
  986. MEGARAID_INTEL_RS3MC044_BRANDING);
  987. break;
  988. default:
  989. break;
  990. }
  991. break;
  992. case PCI_DEVICE_ID_LSI_FURY:
  993. switch (instance->pdev->subsystem_device) {
  994. case MEGARAID_INTEL_RS3WC080_SSDID:
  995. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  996. instance->host->host_no,
  997. MEGARAID_INTEL_RS3WC080_BRANDING);
  998. break;
  999. case MEGARAID_INTEL_RS3WC040_SSDID:
  1000. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1001. instance->host->host_no,
  1002. MEGARAID_INTEL_RS3WC040_BRANDING);
  1003. break;
  1004. default:
  1005. break;
  1006. }
  1007. break;
  1008. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  1009. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  1010. switch (instance->pdev->subsystem_device) {
  1011. case MEGARAID_INTEL_RMS3BC160_SSDID:
  1012. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1013. instance->host->host_no,
  1014. MEGARAID_INTEL_RMS3BC160_BRANDING);
  1015. break;
  1016. default:
  1017. break;
  1018. }
  1019. break;
  1020. default:
  1021. break;
  1022. }
  1023. }
  1024. /**
  1025. * megasas_init_adapter_fusion - Initializes the FW
  1026. * @instance: Adapter soft state
  1027. *
  1028. * This is the main function for initializing firmware.
  1029. */
  1030. u32
  1031. megasas_init_adapter_fusion(struct megasas_instance *instance)
  1032. {
  1033. struct megasas_register_set __iomem *reg_set;
  1034. struct fusion_context *fusion;
  1035. u32 max_cmd, scratch_pad_2;
  1036. int i = 0, count;
  1037. fusion = instance->ctrl_context;
  1038. reg_set = instance->reg_set;
  1039. megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
  1040. /*
  1041. * Reduce the max supported cmds by 1. This is to ensure that the
  1042. * reply_q_sz (1 more than the max cmd that driver may send)
  1043. * does not exceed max cmds that the FW can support
  1044. */
  1045. instance->max_fw_cmds = instance->max_fw_cmds-1;
  1046. /*
  1047. * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
  1048. */
  1049. instance->max_mfi_cmds =
  1050. MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
  1051. max_cmd = instance->max_fw_cmds;
  1052. fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
  1053. fusion->request_alloc_sz =
  1054. sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
  1055. fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
  1056. *(fusion->reply_q_depth);
  1057. fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
  1058. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
  1059. (max_cmd + 1)); /* Extra 1 for SMID 0 */
  1060. scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
  1061. /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
  1062. * Firmware support extended IO chain frame which is 4 times more than
  1063. * legacy Firmware.
  1064. * Legacy Firmware - Frame size is (8 * 128) = 1K
  1065. * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
  1066. */
  1067. if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
  1068. instance->max_chain_frame_sz =
  1069. ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
  1070. MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
  1071. else
  1072. instance->max_chain_frame_sz =
  1073. ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
  1074. MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
  1075. if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
  1076. dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
  1077. instance->max_chain_frame_sz,
  1078. MEGASAS_CHAIN_FRAME_SZ_MIN);
  1079. instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
  1080. }
  1081. fusion->max_sge_in_main_msg =
  1082. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
  1083. - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
  1084. fusion->max_sge_in_chain =
  1085. instance->max_chain_frame_sz
  1086. / sizeof(union MPI2_SGE_IO_UNION);
  1087. instance->max_num_sge =
  1088. rounddown_pow_of_two(fusion->max_sge_in_main_msg
  1089. + fusion->max_sge_in_chain - 2);
  1090. /* Used for pass thru MFI frame (DCMD) */
  1091. fusion->chain_offset_mfi_pthru =
  1092. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
  1093. fusion->chain_offset_io_request =
  1094. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
  1095. sizeof(union MPI2_SGE_IO_UNION))/16;
  1096. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  1097. for (i = 0 ; i < count; i++)
  1098. fusion->last_reply_idx[i] = 0;
  1099. /*
  1100. * For fusion adapters, 3 commands for IOCTL and 5 commands
  1101. * for driver's internal DCMDs.
  1102. */
  1103. instance->max_scsi_cmds = instance->max_fw_cmds -
  1104. (MEGASAS_FUSION_INTERNAL_CMDS +
  1105. MEGASAS_FUSION_IOCTL_CMDS);
  1106. sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
  1107. /*
  1108. * Allocate memory for descriptors
  1109. * Create a pool of commands
  1110. */
  1111. if (megasas_alloc_cmds(instance))
  1112. goto fail_alloc_mfi_cmds;
  1113. if (megasas_alloc_cmds_fusion(instance))
  1114. goto fail_alloc_cmds;
  1115. if (megasas_ioc_init_fusion(instance))
  1116. goto fail_ioc_init;
  1117. megasas_display_intel_branding(instance);
  1118. if (megasas_get_ctrl_info(instance)) {
  1119. dev_err(&instance->pdev->dev,
  1120. "Could not get controller info. Fail from %s %d\n",
  1121. __func__, __LINE__);
  1122. goto fail_ioc_init;
  1123. }
  1124. instance->flag_ieee = 1;
  1125. fusion->fast_path_io = 0;
  1126. fusion->drv_map_pages = get_order(fusion->drv_map_sz);
  1127. for (i = 0; i < 2; i++) {
  1128. fusion->ld_map[i] = NULL;
  1129. fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
  1130. fusion->drv_map_pages);
  1131. if (!fusion->ld_drv_map[i]) {
  1132. dev_err(&instance->pdev->dev, "Could not allocate "
  1133. "memory for local map info for %d pages\n",
  1134. fusion->drv_map_pages);
  1135. if (i == 1)
  1136. free_pages((ulong)fusion->ld_drv_map[0],
  1137. fusion->drv_map_pages);
  1138. goto fail_ioc_init;
  1139. }
  1140. memset(fusion->ld_drv_map[i], 0,
  1141. ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
  1142. }
  1143. for (i = 0; i < 2; i++) {
  1144. fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
  1145. fusion->max_map_sz,
  1146. &fusion->ld_map_phys[i],
  1147. GFP_KERNEL);
  1148. if (!fusion->ld_map[i]) {
  1149. dev_err(&instance->pdev->dev, "Could not allocate memory "
  1150. "for map info\n");
  1151. goto fail_map_info;
  1152. }
  1153. }
  1154. if (!megasas_get_map_info(instance))
  1155. megasas_sync_map_info(instance);
  1156. return 0;
  1157. fail_map_info:
  1158. if (i == 1)
  1159. dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
  1160. fusion->ld_map[0], fusion->ld_map_phys[0]);
  1161. fail_ioc_init:
  1162. megasas_free_cmds_fusion(instance);
  1163. fail_alloc_cmds:
  1164. megasas_free_cmds(instance);
  1165. fail_alloc_mfi_cmds:
  1166. return 1;
  1167. }
  1168. /**
  1169. * map_cmd_status - Maps FW cmd status to OS cmd status
  1170. * @cmd : Pointer to cmd
  1171. * @status : status of cmd returned by FW
  1172. * @ext_status : ext status of cmd returned by FW
  1173. */
  1174. void
  1175. map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
  1176. {
  1177. switch (status) {
  1178. case MFI_STAT_OK:
  1179. cmd->scmd->result = DID_OK << 16;
  1180. break;
  1181. case MFI_STAT_SCSI_IO_FAILED:
  1182. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1183. cmd->scmd->result = (DID_ERROR << 16) | ext_status;
  1184. break;
  1185. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1186. cmd->scmd->result = (DID_OK << 16) | ext_status;
  1187. if (ext_status == SAM_STAT_CHECK_CONDITION) {
  1188. memset(cmd->scmd->sense_buffer, 0,
  1189. SCSI_SENSE_BUFFERSIZE);
  1190. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1191. SCSI_SENSE_BUFFERSIZE);
  1192. cmd->scmd->result |= DRIVER_SENSE << 24;
  1193. }
  1194. break;
  1195. case MFI_STAT_LD_OFFLINE:
  1196. case MFI_STAT_DEVICE_NOT_FOUND:
  1197. cmd->scmd->result = DID_BAD_TARGET << 16;
  1198. break;
  1199. case MFI_STAT_CONFIG_SEQ_MISMATCH:
  1200. cmd->scmd->result = DID_IMM_RETRY << 16;
  1201. break;
  1202. default:
  1203. dev_printk(KERN_DEBUG, &cmd->instance->pdev->dev, "FW status %#x\n", status);
  1204. cmd->scmd->result = DID_ERROR << 16;
  1205. break;
  1206. }
  1207. }
  1208. /**
  1209. * megasas_make_sgl_fusion - Prepares 32-bit SGL
  1210. * @instance: Adapter soft state
  1211. * @scp: SCSI command from the mid-layer
  1212. * @sgl_ptr: SGL to be filled in
  1213. * @cmd: cmd we are working on
  1214. *
  1215. * If successful, this function returns the number of SG elements.
  1216. */
  1217. static int
  1218. megasas_make_sgl_fusion(struct megasas_instance *instance,
  1219. struct scsi_cmnd *scp,
  1220. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
  1221. struct megasas_cmd_fusion *cmd)
  1222. {
  1223. int i, sg_processed, sge_count;
  1224. struct scatterlist *os_sgl;
  1225. struct fusion_context *fusion;
  1226. fusion = instance->ctrl_context;
  1227. if (fusion->adapter_type == INVADER_SERIES) {
  1228. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
  1229. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  1230. sgl_ptr_end->Flags = 0;
  1231. }
  1232. sge_count = scsi_dma_map(scp);
  1233. BUG_ON(sge_count < 0);
  1234. if (sge_count > instance->max_num_sge || !sge_count)
  1235. return sge_count;
  1236. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1237. sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
  1238. sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
  1239. sgl_ptr->Flags = 0;
  1240. if (fusion->adapter_type == INVADER_SERIES)
  1241. if (i == sge_count - 1)
  1242. sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
  1243. sgl_ptr++;
  1244. sg_processed = i + 1;
  1245. if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
  1246. (sge_count > fusion->max_sge_in_main_msg)) {
  1247. struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
  1248. if (fusion->adapter_type == INVADER_SERIES) {
  1249. if ((le16_to_cpu(cmd->io_request->IoFlags) &
  1250. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
  1251. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
  1252. cmd->io_request->ChainOffset =
  1253. fusion->
  1254. chain_offset_io_request;
  1255. else
  1256. cmd->io_request->ChainOffset = 0;
  1257. } else
  1258. cmd->io_request->ChainOffset =
  1259. fusion->chain_offset_io_request;
  1260. sg_chain = sgl_ptr;
  1261. /* Prepare chain element */
  1262. sg_chain->NextChainOffset = 0;
  1263. if (fusion->adapter_type == INVADER_SERIES)
  1264. sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
  1265. else
  1266. sg_chain->Flags =
  1267. (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1268. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
  1269. sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
  1270. sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
  1271. sgl_ptr =
  1272. (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
  1273. memset(sgl_ptr, 0, instance->max_chain_frame_sz);
  1274. }
  1275. }
  1276. return sge_count;
  1277. }
  1278. /**
  1279. * megasas_set_pd_lba - Sets PD LBA
  1280. * @cdb: CDB
  1281. * @cdb_len: cdb length
  1282. * @start_blk: Start block of IO
  1283. *
  1284. * Used to set the PD LBA in CDB for FP IOs
  1285. */
  1286. void
  1287. megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
  1288. struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
  1289. struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
  1290. {
  1291. struct MR_LD_RAID *raid;
  1292. u32 ld;
  1293. u64 start_blk = io_info->pdBlock;
  1294. u8 *cdb = io_request->CDB.CDB32;
  1295. u32 num_blocks = io_info->numBlocks;
  1296. u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
  1297. /* Check if T10 PI (DIF) is enabled for this LD */
  1298. ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
  1299. raid = MR_LdRaidGet(ld, local_map_ptr);
  1300. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
  1301. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1302. cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
  1303. cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
  1304. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1305. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
  1306. else
  1307. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
  1308. cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
  1309. /* LBA */
  1310. cdb[12] = (u8)((start_blk >> 56) & 0xff);
  1311. cdb[13] = (u8)((start_blk >> 48) & 0xff);
  1312. cdb[14] = (u8)((start_blk >> 40) & 0xff);
  1313. cdb[15] = (u8)((start_blk >> 32) & 0xff);
  1314. cdb[16] = (u8)((start_blk >> 24) & 0xff);
  1315. cdb[17] = (u8)((start_blk >> 16) & 0xff);
  1316. cdb[18] = (u8)((start_blk >> 8) & 0xff);
  1317. cdb[19] = (u8)(start_blk & 0xff);
  1318. /* Logical block reference tag */
  1319. io_request->CDB.EEDP32.PrimaryReferenceTag =
  1320. cpu_to_be32(ref_tag);
  1321. io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
  1322. io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
  1323. /* Transfer length */
  1324. cdb[28] = (u8)((num_blocks >> 24) & 0xff);
  1325. cdb[29] = (u8)((num_blocks >> 16) & 0xff);
  1326. cdb[30] = (u8)((num_blocks >> 8) & 0xff);
  1327. cdb[31] = (u8)(num_blocks & 0xff);
  1328. /* set SCSI IO EEDPFlags */
  1329. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
  1330. io_request->EEDPFlags = cpu_to_le16(
  1331. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1332. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  1333. MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
  1334. MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
  1335. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
  1336. } else {
  1337. io_request->EEDPFlags = cpu_to_le16(
  1338. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1339. MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
  1340. }
  1341. io_request->Control |= cpu_to_le32((0x4 << 26));
  1342. io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
  1343. } else {
  1344. /* Some drives don't support 16/12 byte CDB's, convert to 10 */
  1345. if (((cdb_len == 12) || (cdb_len == 16)) &&
  1346. (start_blk <= 0xffffffff)) {
  1347. if (cdb_len == 16) {
  1348. opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
  1349. flagvals = cdb[1];
  1350. groupnum = cdb[14];
  1351. control = cdb[15];
  1352. } else {
  1353. opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
  1354. flagvals = cdb[1];
  1355. groupnum = cdb[10];
  1356. control = cdb[11];
  1357. }
  1358. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1359. cdb[0] = opcode;
  1360. cdb[1] = flagvals;
  1361. cdb[6] = groupnum;
  1362. cdb[9] = control;
  1363. /* Transfer length */
  1364. cdb[8] = (u8)(num_blocks & 0xff);
  1365. cdb[7] = (u8)((num_blocks >> 8) & 0xff);
  1366. io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
  1367. cdb_len = 10;
  1368. } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
  1369. /* Convert to 16 byte CDB for large LBA's */
  1370. switch (cdb_len) {
  1371. case 6:
  1372. opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
  1373. control = cdb[5];
  1374. break;
  1375. case 10:
  1376. opcode =
  1377. cdb[0] == READ_10 ? READ_16 : WRITE_16;
  1378. flagvals = cdb[1];
  1379. groupnum = cdb[6];
  1380. control = cdb[9];
  1381. break;
  1382. case 12:
  1383. opcode =
  1384. cdb[0] == READ_12 ? READ_16 : WRITE_16;
  1385. flagvals = cdb[1];
  1386. groupnum = cdb[10];
  1387. control = cdb[11];
  1388. break;
  1389. }
  1390. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1391. cdb[0] = opcode;
  1392. cdb[1] = flagvals;
  1393. cdb[14] = groupnum;
  1394. cdb[15] = control;
  1395. /* Transfer length */
  1396. cdb[13] = (u8)(num_blocks & 0xff);
  1397. cdb[12] = (u8)((num_blocks >> 8) & 0xff);
  1398. cdb[11] = (u8)((num_blocks >> 16) & 0xff);
  1399. cdb[10] = (u8)((num_blocks >> 24) & 0xff);
  1400. io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
  1401. cdb_len = 16;
  1402. }
  1403. /* Normal case, just load LBA here */
  1404. switch (cdb_len) {
  1405. case 6:
  1406. {
  1407. u8 val = cdb[1] & 0xE0;
  1408. cdb[3] = (u8)(start_blk & 0xff);
  1409. cdb[2] = (u8)((start_blk >> 8) & 0xff);
  1410. cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
  1411. break;
  1412. }
  1413. case 10:
  1414. cdb[5] = (u8)(start_blk & 0xff);
  1415. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1416. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1417. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1418. break;
  1419. case 12:
  1420. cdb[5] = (u8)(start_blk & 0xff);
  1421. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1422. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1423. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1424. break;
  1425. case 16:
  1426. cdb[9] = (u8)(start_blk & 0xff);
  1427. cdb[8] = (u8)((start_blk >> 8) & 0xff);
  1428. cdb[7] = (u8)((start_blk >> 16) & 0xff);
  1429. cdb[6] = (u8)((start_blk >> 24) & 0xff);
  1430. cdb[5] = (u8)((start_blk >> 32) & 0xff);
  1431. cdb[4] = (u8)((start_blk >> 40) & 0xff);
  1432. cdb[3] = (u8)((start_blk >> 48) & 0xff);
  1433. cdb[2] = (u8)((start_blk >> 56) & 0xff);
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. /**
  1439. * megasas_build_ldio_fusion - Prepares IOs to devices
  1440. * @instance: Adapter soft state
  1441. * @scp: SCSI command
  1442. * @cmd: Command to be prepared
  1443. *
  1444. * Prepares the io_request and chain elements (sg_frame) for IO
  1445. * The IO can be for PD (Fast Path) or LD
  1446. */
  1447. void
  1448. megasas_build_ldio_fusion(struct megasas_instance *instance,
  1449. struct scsi_cmnd *scp,
  1450. struct megasas_cmd_fusion *cmd)
  1451. {
  1452. u8 fp_possible;
  1453. u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
  1454. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1455. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1456. struct IO_REQUEST_INFO io_info;
  1457. struct fusion_context *fusion;
  1458. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1459. u8 *raidLUN;
  1460. device_id = MEGASAS_DEV_INDEX(scp);
  1461. fusion = instance->ctrl_context;
  1462. io_request = cmd->io_request;
  1463. io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
  1464. io_request->RaidContext.status = 0;
  1465. io_request->RaidContext.exStatus = 0;
  1466. req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
  1467. start_lba_lo = 0;
  1468. start_lba_hi = 0;
  1469. fp_possible = 0;
  1470. /*
  1471. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1472. */
  1473. if (scp->cmd_len == 6) {
  1474. datalength = (u32) scp->cmnd[4];
  1475. start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  1476. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  1477. start_lba_lo &= 0x1FFFFF;
  1478. }
  1479. /*
  1480. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1481. */
  1482. else if (scp->cmd_len == 10) {
  1483. datalength = (u32) scp->cmnd[8] |
  1484. ((u32) scp->cmnd[7] << 8);
  1485. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1486. ((u32) scp->cmnd[3] << 16) |
  1487. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1488. }
  1489. /*
  1490. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1491. */
  1492. else if (scp->cmd_len == 12) {
  1493. datalength = ((u32) scp->cmnd[6] << 24) |
  1494. ((u32) scp->cmnd[7] << 16) |
  1495. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1496. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1497. ((u32) scp->cmnd[3] << 16) |
  1498. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1499. }
  1500. /*
  1501. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1502. */
  1503. else if (scp->cmd_len == 16) {
  1504. datalength = ((u32) scp->cmnd[10] << 24) |
  1505. ((u32) scp->cmnd[11] << 16) |
  1506. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  1507. start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  1508. ((u32) scp->cmnd[7] << 16) |
  1509. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1510. start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  1511. ((u32) scp->cmnd[3] << 16) |
  1512. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1513. }
  1514. memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
  1515. io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
  1516. io_info.numBlocks = datalength;
  1517. io_info.ldTgtId = device_id;
  1518. io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
  1519. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1520. io_info.isRead = 1;
  1521. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1522. if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
  1523. instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
  1524. io_request->RaidContext.regLockFlags = 0;
  1525. fp_possible = 0;
  1526. } else {
  1527. if (MR_BuildRaidContext(instance, &io_info,
  1528. &io_request->RaidContext,
  1529. local_map_ptr, &raidLUN))
  1530. fp_possible = io_info.fpOkForIo;
  1531. }
  1532. /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
  1533. id by default, not CPU group id, otherwise all MSI-X queues won't
  1534. be utilized */
  1535. cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
  1536. raw_smp_processor_id() % instance->msix_vectors : 0;
  1537. if (fp_possible) {
  1538. megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
  1539. local_map_ptr, start_lba_lo);
  1540. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1541. cmd->request_desc->SCSIIO.RequestFlags =
  1542. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
  1543. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1544. if (fusion->adapter_type == INVADER_SERIES) {
  1545. if (io_request->RaidContext.regLockFlags ==
  1546. REGION_TYPE_UNUSED)
  1547. cmd->request_desc->SCSIIO.RequestFlags =
  1548. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1549. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1550. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1551. io_request->RaidContext.nseg = 0x1;
  1552. io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  1553. io_request->RaidContext.regLockFlags |=
  1554. (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
  1555. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1556. }
  1557. if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
  1558. (io_info.isRead)) {
  1559. io_info.devHandle =
  1560. get_updated_dev_handle(instance,
  1561. &fusion->load_balance_info[device_id],
  1562. &io_info);
  1563. scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
  1564. cmd->pd_r1_lb = io_info.pd_after_lb;
  1565. } else
  1566. scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
  1567. if ((raidLUN[0] == 1) &&
  1568. (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
  1569. instance->dev_handle = !(instance->dev_handle);
  1570. io_info.devHandle =
  1571. local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
  1572. }
  1573. cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
  1574. io_request->DevHandle = io_info.devHandle;
  1575. /* populate the LUN field */
  1576. memcpy(io_request->LUN, raidLUN, 8);
  1577. } else {
  1578. io_request->RaidContext.timeoutValue =
  1579. cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
  1580. cmd->request_desc->SCSIIO.RequestFlags =
  1581. (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
  1582. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1583. if (fusion->adapter_type == INVADER_SERIES) {
  1584. if (io_info.do_fp_rlbypass ||
  1585. (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED))
  1586. cmd->request_desc->SCSIIO.RequestFlags =
  1587. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1588. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1589. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1590. io_request->RaidContext.regLockFlags |=
  1591. (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
  1592. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1593. io_request->RaidContext.nseg = 0x1;
  1594. }
  1595. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1596. io_request->DevHandle = cpu_to_le16(device_id);
  1597. } /* Not FP */
  1598. }
  1599. /**
  1600. * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
  1601. * @instance: Adapter soft state
  1602. * @scp: SCSI command
  1603. * @cmd: Command to be prepared
  1604. *
  1605. * Prepares the io_request frame for non-rw io cmds for vd.
  1606. */
  1607. static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
  1608. struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
  1609. {
  1610. u32 device_id;
  1611. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1612. u16 pd_index = 0;
  1613. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1614. struct fusion_context *fusion = instance->ctrl_context;
  1615. u8 span, physArm;
  1616. __le16 devHandle;
  1617. u32 ld, arRef, pd;
  1618. struct MR_LD_RAID *raid;
  1619. struct RAID_CONTEXT *pRAID_Context;
  1620. u8 fp_possible = 1;
  1621. io_request = cmd->io_request;
  1622. device_id = MEGASAS_DEV_INDEX(scmd);
  1623. pd_index = MEGASAS_PD_INDEX(scmd);
  1624. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1625. io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  1626. /* get RAID_Context pointer */
  1627. pRAID_Context = &io_request->RaidContext;
  1628. /* Check with FW team */
  1629. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1630. pRAID_Context->regLockRowLBA = 0;
  1631. pRAID_Context->regLockLength = 0;
  1632. if (fusion->fast_path_io && (
  1633. device_id < instance->fw_supported_vd_count)) {
  1634. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  1635. if (ld >= instance->fw_supported_vd_count)
  1636. fp_possible = 0;
  1637. raid = MR_LdRaidGet(ld, local_map_ptr);
  1638. if (!(raid->capability.fpNonRWCapable))
  1639. fp_possible = 0;
  1640. } else
  1641. fp_possible = 0;
  1642. if (!fp_possible) {
  1643. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1644. io_request->DevHandle = cpu_to_le16(device_id);
  1645. io_request->LUN[1] = scmd->device->lun;
  1646. pRAID_Context->timeoutValue =
  1647. cpu_to_le16 (scmd->request->timeout / HZ);
  1648. cmd->request_desc->SCSIIO.RequestFlags =
  1649. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1650. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1651. } else {
  1652. /* set RAID context values */
  1653. pRAID_Context->configSeqNum = raid->seqNum;
  1654. pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
  1655. pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
  1656. /* get the DevHandle for the PD (since this is
  1657. fpNonRWCapable, this is a single disk RAID0) */
  1658. span = physArm = 0;
  1659. arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
  1660. pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
  1661. devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
  1662. /* build request descriptor */
  1663. cmd->request_desc->SCSIIO.RequestFlags =
  1664. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
  1665. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1666. cmd->request_desc->SCSIIO.DevHandle = devHandle;
  1667. /* populate the LUN field */
  1668. memcpy(io_request->LUN, raid->LUN, 8);
  1669. /* build the raidScsiIO structure */
  1670. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1671. io_request->DevHandle = devHandle;
  1672. }
  1673. }
  1674. /**
  1675. * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
  1676. * @instance: Adapter soft state
  1677. * @scp: SCSI command
  1678. * @cmd: Command to be prepared
  1679. * @fp_possible: parameter to detect fast path or firmware path io.
  1680. *
  1681. * Prepares the io_request frame for rw/non-rw io cmds for syspds
  1682. */
  1683. static void
  1684. megasas_build_syspd_fusion(struct megasas_instance *instance,
  1685. struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
  1686. bool fp_possible)
  1687. {
  1688. u32 device_id;
  1689. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1690. u16 pd_index = 0;
  1691. u16 os_timeout_value;
  1692. u16 timeout_limit;
  1693. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1694. struct RAID_CONTEXT *pRAID_Context;
  1695. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  1696. struct fusion_context *fusion = instance->ctrl_context;
  1697. pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
  1698. device_id = MEGASAS_DEV_INDEX(scmd);
  1699. pd_index = MEGASAS_PD_INDEX(scmd);
  1700. os_timeout_value = scmd->request->timeout / HZ;
  1701. io_request = cmd->io_request;
  1702. /* get RAID_Context pointer */
  1703. pRAID_Context = &io_request->RaidContext;
  1704. pRAID_Context->regLockFlags = 0;
  1705. pRAID_Context->regLockRowLBA = 0;
  1706. pRAID_Context->regLockLength = 0;
  1707. io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  1708. io_request->LUN[1] = scmd->device->lun;
  1709. pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
  1710. << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
  1711. /* If FW supports PD sequence number */
  1712. if (instance->use_seqnum_jbod_fp &&
  1713. instance->pd_list[pd_index].driveType == TYPE_DISK) {
  1714. /* TgtId must be incremented by 255 as jbod seq number is index
  1715. * below raid map
  1716. */
  1717. pRAID_Context->VirtualDiskTgtId =
  1718. cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
  1719. pRAID_Context->configSeqNum = pd_sync->seq[pd_index].seqNum;
  1720. io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
  1721. pRAID_Context->regLockFlags |=
  1722. (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
  1723. pRAID_Context->Type = MPI2_TYPE_CUDA;
  1724. pRAID_Context->nseg = 0x1;
  1725. } else if (fusion->fast_path_io) {
  1726. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1727. pRAID_Context->configSeqNum = 0;
  1728. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1729. io_request->DevHandle =
  1730. local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
  1731. } else {
  1732. /* Want to send all IO via FW path */
  1733. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1734. pRAID_Context->configSeqNum = 0;
  1735. io_request->DevHandle = cpu_to_le16(0xFFFF);
  1736. }
  1737. cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
  1738. cmd->request_desc->SCSIIO.MSIxIndex =
  1739. instance->msix_vectors ?
  1740. (raw_smp_processor_id() % instance->msix_vectors) : 0;
  1741. if (!fp_possible) {
  1742. /* system pd firmware path */
  1743. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1744. cmd->request_desc->SCSIIO.RequestFlags =
  1745. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1746. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1747. pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
  1748. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1749. } else {
  1750. if (os_timeout_value)
  1751. os_timeout_value++;
  1752. /* system pd Fast Path */
  1753. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1754. timeout_limit = (scmd->device->type == TYPE_DISK) ?
  1755. 255 : 0xFFFF;
  1756. pRAID_Context->timeoutValue =
  1757. cpu_to_le16((os_timeout_value > timeout_limit) ?
  1758. timeout_limit : os_timeout_value);
  1759. if (fusion->adapter_type == INVADER_SERIES)
  1760. io_request->IoFlags |=
  1761. cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  1762. cmd->request_desc->SCSIIO.RequestFlags =
  1763. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
  1764. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1765. }
  1766. }
  1767. /**
  1768. * megasas_build_io_fusion - Prepares IOs to devices
  1769. * @instance: Adapter soft state
  1770. * @scp: SCSI command
  1771. * @cmd: Command to be prepared
  1772. *
  1773. * Invokes helper functions to prepare request frames
  1774. * and sets flags appropriate for IO/Non-IO cmd
  1775. */
  1776. int
  1777. megasas_build_io_fusion(struct megasas_instance *instance,
  1778. struct scsi_cmnd *scp,
  1779. struct megasas_cmd_fusion *cmd)
  1780. {
  1781. u16 sge_count;
  1782. u8 cmd_type;
  1783. struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
  1784. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1785. mr_device_priv_data = scp->device->hostdata;
  1786. /* Zero out some fields so they don't get reused */
  1787. memset(io_request->LUN, 0x0, 8);
  1788. io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
  1789. io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
  1790. io_request->EEDPFlags = 0;
  1791. io_request->Control = 0;
  1792. io_request->EEDPBlockSize = 0;
  1793. io_request->ChainOffset = 0;
  1794. io_request->RaidContext.RAIDFlags = 0;
  1795. io_request->RaidContext.Type = 0;
  1796. io_request->RaidContext.nseg = 0;
  1797. memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
  1798. /*
  1799. * Just the CDB length,rest of the Flags are zero
  1800. * This will be modified for FP in build_ldio_fusion
  1801. */
  1802. io_request->IoFlags = cpu_to_le16(scp->cmd_len);
  1803. switch (cmd_type = megasas_cmd_type(scp)) {
  1804. case READ_WRITE_LDIO:
  1805. megasas_build_ldio_fusion(instance, scp, cmd);
  1806. break;
  1807. case NON_READ_WRITE_LDIO:
  1808. megasas_build_ld_nonrw_fusion(instance, scp, cmd);
  1809. break;
  1810. case READ_WRITE_SYSPDIO:
  1811. megasas_build_syspd_fusion(instance, scp, cmd, true);
  1812. break;
  1813. case NON_READ_WRITE_SYSPDIO:
  1814. if (instance->secure_jbod_support ||
  1815. mr_device_priv_data->is_tm_capable)
  1816. megasas_build_syspd_fusion(instance, scp, cmd, false);
  1817. else
  1818. megasas_build_syspd_fusion(instance, scp, cmd, true);
  1819. break;
  1820. default:
  1821. break;
  1822. }
  1823. /*
  1824. * Construct SGL
  1825. */
  1826. sge_count =
  1827. megasas_make_sgl_fusion(instance, scp,
  1828. (struct MPI25_IEEE_SGE_CHAIN64 *)
  1829. &io_request->SGL, cmd);
  1830. if (sge_count > instance->max_num_sge) {
  1831. dev_err(&instance->pdev->dev, "Error. sge_count (0x%x) exceeds "
  1832. "max (0x%x) allowed\n", sge_count,
  1833. instance->max_num_sge);
  1834. return 1;
  1835. }
  1836. /* numSGE store lower 8 bit of sge_count.
  1837. * numSGEExt store higher 8 bit of sge_count
  1838. */
  1839. io_request->RaidContext.numSGE = sge_count;
  1840. io_request->RaidContext.numSGEExt = (u8)(sge_count >> 8);
  1841. io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
  1842. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1843. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
  1844. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1845. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
  1846. io_request->SGLOffset0 =
  1847. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
  1848. io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
  1849. io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  1850. cmd->scmd = scp;
  1851. scp->SCp.ptr = (char *)cmd;
  1852. return 0;
  1853. }
  1854. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  1855. megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
  1856. {
  1857. u8 *p;
  1858. struct fusion_context *fusion;
  1859. if (index >= instance->max_fw_cmds) {
  1860. dev_err(&instance->pdev->dev, "Invalid SMID (0x%x)request for "
  1861. "descriptor for scsi%d\n", index,
  1862. instance->host->host_no);
  1863. return NULL;
  1864. }
  1865. fusion = instance->ctrl_context;
  1866. p = fusion->req_frames_desc
  1867. +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
  1868. return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
  1869. }
  1870. /**
  1871. * megasas_build_and_issue_cmd_fusion -Main routine for building and
  1872. * issuing non IOCTL cmd
  1873. * @instance: Adapter soft state
  1874. * @scmd: pointer to scsi cmd from OS
  1875. */
  1876. static u32
  1877. megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
  1878. struct scsi_cmnd *scmd)
  1879. {
  1880. struct megasas_cmd_fusion *cmd;
  1881. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1882. u32 index;
  1883. struct fusion_context *fusion;
  1884. fusion = instance->ctrl_context;
  1885. if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
  1886. instance->ldio_threshold &&
  1887. (atomic_inc_return(&instance->ldio_outstanding) >
  1888. instance->ldio_threshold)) {
  1889. atomic_dec(&instance->ldio_outstanding);
  1890. return SCSI_MLQUEUE_DEVICE_BUSY;
  1891. }
  1892. cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
  1893. index = cmd->index;
  1894. req_desc = megasas_get_request_descriptor(instance, index-1);
  1895. if (!req_desc)
  1896. return SCSI_MLQUEUE_HOST_BUSY;
  1897. req_desc->Words = 0;
  1898. cmd->request_desc = req_desc;
  1899. if (megasas_build_io_fusion(instance, scmd, cmd)) {
  1900. megasas_return_cmd_fusion(instance, cmd);
  1901. dev_err(&instance->pdev->dev, "Error building command\n");
  1902. cmd->request_desc = NULL;
  1903. return SCSI_MLQUEUE_HOST_BUSY;
  1904. }
  1905. req_desc = cmd->request_desc;
  1906. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  1907. if (cmd->io_request->ChainOffset != 0 &&
  1908. cmd->io_request->ChainOffset != 0xF)
  1909. dev_err(&instance->pdev->dev, "The chain offset value is not "
  1910. "correct : %x\n", cmd->io_request->ChainOffset);
  1911. /*
  1912. * Issue the command to the FW
  1913. */
  1914. atomic_inc(&instance->fw_outstanding);
  1915. megasas_fire_cmd_fusion(instance, req_desc);
  1916. return 0;
  1917. }
  1918. /**
  1919. * complete_cmd_fusion - Completes command
  1920. * @instance: Adapter soft state
  1921. * Completes all commands that is in reply descriptor queue
  1922. */
  1923. int
  1924. complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
  1925. {
  1926. union MPI2_REPLY_DESCRIPTORS_UNION *desc;
  1927. struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
  1928. struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
  1929. struct fusion_context *fusion;
  1930. struct megasas_cmd *cmd_mfi;
  1931. struct megasas_cmd_fusion *cmd_fusion;
  1932. u16 smid, num_completed;
  1933. u8 reply_descript_type;
  1934. u32 status, extStatus, device_id;
  1935. union desc_value d_val;
  1936. struct LD_LOAD_BALANCE_INFO *lbinfo;
  1937. int threshold_reply_count = 0;
  1938. struct scsi_cmnd *scmd_local = NULL;
  1939. struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
  1940. struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
  1941. fusion = instance->ctrl_context;
  1942. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  1943. return IRQ_HANDLED;
  1944. desc = fusion->reply_frames_desc[MSIxIndex] +
  1945. fusion->last_reply_idx[MSIxIndex];
  1946. reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  1947. d_val.word = desc->Words;
  1948. reply_descript_type = reply_desc->ReplyFlags &
  1949. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1950. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1951. return IRQ_NONE;
  1952. num_completed = 0;
  1953. while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
  1954. d_val.u.high != cpu_to_le32(UINT_MAX)) {
  1955. smid = le16_to_cpu(reply_desc->SMID);
  1956. cmd_fusion = fusion->cmd_list[smid - 1];
  1957. scsi_io_req =
  1958. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  1959. cmd_fusion->io_request;
  1960. if (cmd_fusion->scmd)
  1961. cmd_fusion->scmd->SCp.ptr = NULL;
  1962. scmd_local = cmd_fusion->scmd;
  1963. status = scsi_io_req->RaidContext.status;
  1964. extStatus = scsi_io_req->RaidContext.exStatus;
  1965. switch (scsi_io_req->Function) {
  1966. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  1967. mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
  1968. cmd_fusion->io_request;
  1969. mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
  1970. &mr_tm_req->TmRequest;
  1971. dev_dbg(&instance->pdev->dev, "TM completion:"
  1972. "type: 0x%x TaskMID: 0x%x\n",
  1973. mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
  1974. complete(&cmd_fusion->done);
  1975. break;
  1976. case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
  1977. /* Update load balancing info */
  1978. device_id = MEGASAS_DEV_INDEX(scmd_local);
  1979. lbinfo = &fusion->load_balance_info[device_id];
  1980. if (cmd_fusion->scmd->SCp.Status &
  1981. MEGASAS_LOAD_BALANCE_FLAG) {
  1982. atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
  1983. cmd_fusion->scmd->SCp.Status &=
  1984. ~MEGASAS_LOAD_BALANCE_FLAG;
  1985. }
  1986. if (reply_descript_type ==
  1987. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
  1988. if (megasas_dbg_lvl == 5)
  1989. dev_err(&instance->pdev->dev, "\nFAST Path "
  1990. "IO Success\n");
  1991. }
  1992. /* Fall thru and complete IO */
  1993. case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
  1994. /* Map the FW Cmd Status */
  1995. map_cmd_status(cmd_fusion, status, extStatus);
  1996. scsi_io_req->RaidContext.status = 0;
  1997. scsi_io_req->RaidContext.exStatus = 0;
  1998. if (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
  1999. atomic_dec(&instance->ldio_outstanding);
  2000. megasas_return_cmd_fusion(instance, cmd_fusion);
  2001. scsi_dma_unmap(scmd_local);
  2002. scmd_local->scsi_done(scmd_local);
  2003. atomic_dec(&instance->fw_outstanding);
  2004. break;
  2005. case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
  2006. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  2007. /* Poll mode. Dummy free.
  2008. * In case of Interrupt mode, caller has reverse check.
  2009. */
  2010. if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
  2011. cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
  2012. megasas_return_cmd(instance, cmd_mfi);
  2013. } else
  2014. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  2015. break;
  2016. }
  2017. fusion->last_reply_idx[MSIxIndex]++;
  2018. if (fusion->last_reply_idx[MSIxIndex] >=
  2019. fusion->reply_q_depth)
  2020. fusion->last_reply_idx[MSIxIndex] = 0;
  2021. desc->Words = cpu_to_le64(ULLONG_MAX);
  2022. num_completed++;
  2023. threshold_reply_count++;
  2024. /* Get the next reply descriptor */
  2025. if (!fusion->last_reply_idx[MSIxIndex])
  2026. desc = fusion->reply_frames_desc[MSIxIndex];
  2027. else
  2028. desc++;
  2029. reply_desc =
  2030. (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  2031. d_val.word = desc->Words;
  2032. reply_descript_type = reply_desc->ReplyFlags &
  2033. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  2034. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  2035. break;
  2036. /*
  2037. * Write to reply post host index register after completing threshold
  2038. * number of reply counts and still there are more replies in reply queue
  2039. * pending to be completed
  2040. */
  2041. if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
  2042. if (fusion->adapter_type == INVADER_SERIES)
  2043. writel(((MSIxIndex & 0x7) << 24) |
  2044. fusion->last_reply_idx[MSIxIndex],
  2045. instance->reply_post_host_index_addr[MSIxIndex/8]);
  2046. else
  2047. writel((MSIxIndex << 24) |
  2048. fusion->last_reply_idx[MSIxIndex],
  2049. instance->reply_post_host_index_addr[0]);
  2050. threshold_reply_count = 0;
  2051. }
  2052. }
  2053. if (!num_completed)
  2054. return IRQ_NONE;
  2055. wmb();
  2056. if (fusion->adapter_type == INVADER_SERIES)
  2057. writel(((MSIxIndex & 0x7) << 24) |
  2058. fusion->last_reply_idx[MSIxIndex],
  2059. instance->reply_post_host_index_addr[MSIxIndex/8]);
  2060. else
  2061. writel((MSIxIndex << 24) |
  2062. fusion->last_reply_idx[MSIxIndex],
  2063. instance->reply_post_host_index_addr[0]);
  2064. megasas_check_and_restore_queue_depth(instance);
  2065. return IRQ_HANDLED;
  2066. }
  2067. /**
  2068. * megasas_complete_cmd_dpc_fusion - Completes command
  2069. * @instance: Adapter soft state
  2070. *
  2071. * Tasklet to complete cmds
  2072. */
  2073. void
  2074. megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
  2075. {
  2076. struct megasas_instance *instance =
  2077. (struct megasas_instance *)instance_addr;
  2078. unsigned long flags;
  2079. u32 count, MSIxIndex;
  2080. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  2081. /* If we have already declared adapter dead, donot complete cmds */
  2082. spin_lock_irqsave(&instance->hba_lock, flags);
  2083. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2084. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2085. return;
  2086. }
  2087. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2088. for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
  2089. complete_cmd_fusion(instance, MSIxIndex);
  2090. }
  2091. /**
  2092. * megasas_isr_fusion - isr entry point
  2093. */
  2094. irqreturn_t megasas_isr_fusion(int irq, void *devp)
  2095. {
  2096. struct megasas_irq_context *irq_context = devp;
  2097. struct megasas_instance *instance = irq_context->instance;
  2098. u32 mfiStatus, fw_state, dma_state;
  2099. if (instance->mask_interrupts)
  2100. return IRQ_NONE;
  2101. if (!instance->msix_vectors) {
  2102. mfiStatus = instance->instancet->clear_intr(instance->reg_set);
  2103. if (!mfiStatus)
  2104. return IRQ_NONE;
  2105. }
  2106. /* If we are resetting, bail */
  2107. if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
  2108. instance->instancet->clear_intr(instance->reg_set);
  2109. return IRQ_HANDLED;
  2110. }
  2111. if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
  2112. instance->instancet->clear_intr(instance->reg_set);
  2113. /* If we didn't complete any commands, check for FW fault */
  2114. fw_state = instance->instancet->read_fw_status_reg(
  2115. instance->reg_set) & MFI_STATE_MASK;
  2116. dma_state = instance->instancet->read_fw_status_reg
  2117. (instance->reg_set) & MFI_STATE_DMADONE;
  2118. if (instance->crash_dump_drv_support &&
  2119. instance->crash_dump_app_support) {
  2120. /* Start collecting crash, if DMA bit is done */
  2121. if ((fw_state == MFI_STATE_FAULT) && dma_state)
  2122. schedule_work(&instance->crash_init);
  2123. else if (fw_state == MFI_STATE_FAULT)
  2124. schedule_work(&instance->work_init);
  2125. } else if (fw_state == MFI_STATE_FAULT) {
  2126. dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
  2127. "for scsi%d\n", instance->host->host_no);
  2128. schedule_work(&instance->work_init);
  2129. }
  2130. }
  2131. return IRQ_HANDLED;
  2132. }
  2133. /**
  2134. * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
  2135. * @instance: Adapter soft state
  2136. * mfi_cmd: megasas_cmd pointer
  2137. *
  2138. */
  2139. u8
  2140. build_mpt_mfi_pass_thru(struct megasas_instance *instance,
  2141. struct megasas_cmd *mfi_cmd)
  2142. {
  2143. struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
  2144. struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
  2145. struct megasas_cmd_fusion *cmd;
  2146. struct fusion_context *fusion;
  2147. struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
  2148. fusion = instance->ctrl_context;
  2149. cmd = megasas_get_cmd_fusion(instance,
  2150. instance->max_scsi_cmds + mfi_cmd->index);
  2151. /* Save the smid. To be used for returning the cmd */
  2152. mfi_cmd->context.smid = cmd->index;
  2153. /*
  2154. * For cmds where the flag is set, store the flag and check
  2155. * on completion. For cmds with this flag, don't call
  2156. * megasas_complete_cmd
  2157. */
  2158. if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
  2159. mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
  2160. io_req = cmd->io_request;
  2161. if (fusion->adapter_type == INVADER_SERIES) {
  2162. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
  2163. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
  2164. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  2165. sgl_ptr_end->Flags = 0;
  2166. }
  2167. mpi25_ieee_chain =
  2168. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
  2169. io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
  2170. io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
  2171. SGL) / 4;
  2172. io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
  2173. mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
  2174. mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  2175. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
  2176. mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
  2177. return 0;
  2178. }
  2179. /**
  2180. * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
  2181. * @instance: Adapter soft state
  2182. * @cmd: mfi cmd to build
  2183. *
  2184. */
  2185. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  2186. build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  2187. {
  2188. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2189. u16 index;
  2190. if (build_mpt_mfi_pass_thru(instance, cmd)) {
  2191. dev_err(&instance->pdev->dev, "Couldn't build MFI pass thru cmd\n");
  2192. return NULL;
  2193. }
  2194. index = cmd->context.smid;
  2195. req_desc = megasas_get_request_descriptor(instance, index - 1);
  2196. if (!req_desc)
  2197. return NULL;
  2198. req_desc->Words = 0;
  2199. req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  2200. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2201. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  2202. return req_desc;
  2203. }
  2204. /**
  2205. * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
  2206. * @instance: Adapter soft state
  2207. * @cmd: mfi cmd pointer
  2208. *
  2209. */
  2210. int
  2211. megasas_issue_dcmd_fusion(struct megasas_instance *instance,
  2212. struct megasas_cmd *cmd)
  2213. {
  2214. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2215. req_desc = build_mpt_cmd(instance, cmd);
  2216. if (!req_desc) {
  2217. dev_info(&instance->pdev->dev, "Failed from %s %d\n",
  2218. __func__, __LINE__);
  2219. return DCMD_NOT_FIRED;
  2220. }
  2221. megasas_fire_cmd_fusion(instance, req_desc);
  2222. return DCMD_SUCCESS;
  2223. }
  2224. /**
  2225. * megasas_release_fusion - Reverses the FW initialization
  2226. * @instance: Adapter soft state
  2227. */
  2228. void
  2229. megasas_release_fusion(struct megasas_instance *instance)
  2230. {
  2231. megasas_free_cmds(instance);
  2232. megasas_free_cmds_fusion(instance);
  2233. iounmap(instance->reg_set);
  2234. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  2235. }
  2236. /**
  2237. * megasas_read_fw_status_reg_fusion - returns the current FW status value
  2238. * @regs: MFI register set
  2239. */
  2240. static u32
  2241. megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
  2242. {
  2243. return readl(&(regs)->outbound_scratch_pad);
  2244. }
  2245. /**
  2246. * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  2247. * @instance: Controller's soft instance
  2248. * return: Number of allocated host crash buffers
  2249. */
  2250. static void
  2251. megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
  2252. {
  2253. unsigned int i;
  2254. instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
  2255. for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
  2256. instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
  2257. instance->crash_buf_pages);
  2258. if (!instance->crash_buf[i]) {
  2259. dev_info(&instance->pdev->dev, "Firmware crash dump "
  2260. "memory allocation failed at index %d\n", i);
  2261. break;
  2262. }
  2263. memset(instance->crash_buf[i], 0,
  2264. ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
  2265. }
  2266. instance->drv_buf_alloc = i;
  2267. }
  2268. /**
  2269. * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  2270. * @instance: Controller's soft instance
  2271. */
  2272. void
  2273. megasas_free_host_crash_buffer(struct megasas_instance *instance)
  2274. {
  2275. unsigned int i
  2276. ;
  2277. for (i = 0; i < instance->drv_buf_alloc; i++) {
  2278. if (instance->crash_buf[i])
  2279. free_pages((ulong)instance->crash_buf[i],
  2280. instance->crash_buf_pages);
  2281. }
  2282. instance->drv_buf_index = 0;
  2283. instance->drv_buf_alloc = 0;
  2284. instance->fw_crash_state = UNAVAILABLE;
  2285. instance->fw_crash_buffer_size = 0;
  2286. }
  2287. /**
  2288. * megasas_adp_reset_fusion - For controller reset
  2289. * @regs: MFI register set
  2290. */
  2291. static int
  2292. megasas_adp_reset_fusion(struct megasas_instance *instance,
  2293. struct megasas_register_set __iomem *regs)
  2294. {
  2295. u32 host_diag, abs_state, retry;
  2296. /* Now try to reset the chip */
  2297. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2298. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2299. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2300. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2301. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2302. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2303. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2304. /* Check that the diag write enable (DRWE) bit is on */
  2305. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2306. retry = 0;
  2307. while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
  2308. msleep(100);
  2309. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2310. if (retry++ == 100) {
  2311. dev_warn(&instance->pdev->dev,
  2312. "Host diag unlock failed from %s %d\n",
  2313. __func__, __LINE__);
  2314. break;
  2315. }
  2316. }
  2317. if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
  2318. return -1;
  2319. /* Send chip reset command */
  2320. writel(host_diag | HOST_DIAG_RESET_ADAPTER,
  2321. &instance->reg_set->fusion_host_diag);
  2322. msleep(3000);
  2323. /* Make sure reset adapter bit is cleared */
  2324. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2325. retry = 0;
  2326. while (host_diag & HOST_DIAG_RESET_ADAPTER) {
  2327. msleep(100);
  2328. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2329. if (retry++ == 1000) {
  2330. dev_warn(&instance->pdev->dev,
  2331. "Diag reset adapter never cleared %s %d\n",
  2332. __func__, __LINE__);
  2333. break;
  2334. }
  2335. }
  2336. if (host_diag & HOST_DIAG_RESET_ADAPTER)
  2337. return -1;
  2338. abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
  2339. & MFI_STATE_MASK;
  2340. retry = 0;
  2341. while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
  2342. msleep(100);
  2343. abs_state = instance->instancet->
  2344. read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2345. }
  2346. if (abs_state <= MFI_STATE_FW_INIT) {
  2347. dev_warn(&instance->pdev->dev,
  2348. "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
  2349. abs_state, __func__, __LINE__);
  2350. return -1;
  2351. }
  2352. return 0;
  2353. }
  2354. /**
  2355. * megasas_check_reset_fusion - For controller reset check
  2356. * @regs: MFI register set
  2357. */
  2358. static int
  2359. megasas_check_reset_fusion(struct megasas_instance *instance,
  2360. struct megasas_register_set __iomem *regs)
  2361. {
  2362. return 0;
  2363. }
  2364. /* This function waits for outstanding commands on fusion to complete */
  2365. int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
  2366. int reason, int *convert)
  2367. {
  2368. int i, outstanding, retval = 0, hb_seconds_missed = 0;
  2369. u32 fw_state;
  2370. for (i = 0; i < resetwaittime; i++) {
  2371. /* Check if firmware is in fault state */
  2372. fw_state = instance->instancet->read_fw_status_reg(
  2373. instance->reg_set) & MFI_STATE_MASK;
  2374. if (fw_state == MFI_STATE_FAULT) {
  2375. dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
  2376. " will reset adapter scsi%d.\n",
  2377. instance->host->host_no);
  2378. megasas_complete_cmd_dpc_fusion((unsigned long)instance);
  2379. retval = 1;
  2380. goto out;
  2381. }
  2382. if (reason == MFI_IO_TIMEOUT_OCR) {
  2383. dev_info(&instance->pdev->dev,
  2384. "MFI IO is timed out, initiating OCR\n");
  2385. megasas_complete_cmd_dpc_fusion((unsigned long)instance);
  2386. retval = 1;
  2387. goto out;
  2388. }
  2389. /* If SR-IOV VF mode & heartbeat timeout, don't wait */
  2390. if (instance->requestorId && !reason) {
  2391. retval = 1;
  2392. goto out;
  2393. }
  2394. /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
  2395. if (instance->requestorId && reason) {
  2396. if (instance->hb_host_mem->HB.fwCounter !=
  2397. instance->hb_host_mem->HB.driverCounter) {
  2398. instance->hb_host_mem->HB.driverCounter =
  2399. instance->hb_host_mem->HB.fwCounter;
  2400. hb_seconds_missed = 0;
  2401. } else {
  2402. hb_seconds_missed++;
  2403. if (hb_seconds_missed ==
  2404. (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
  2405. dev_warn(&instance->pdev->dev, "SR-IOV:"
  2406. " Heartbeat never completed "
  2407. " while polling during I/O "
  2408. " timeout handling for "
  2409. "scsi%d.\n",
  2410. instance->host->host_no);
  2411. *convert = 1;
  2412. retval = 1;
  2413. goto out;
  2414. }
  2415. }
  2416. }
  2417. outstanding = atomic_read(&instance->fw_outstanding);
  2418. if (!outstanding)
  2419. goto out;
  2420. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  2421. dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
  2422. "commands to complete for scsi%d\n", i,
  2423. outstanding, instance->host->host_no);
  2424. megasas_complete_cmd_dpc_fusion(
  2425. (unsigned long)instance);
  2426. }
  2427. msleep(1000);
  2428. }
  2429. if (atomic_read(&instance->fw_outstanding)) {
  2430. dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
  2431. "will reset adapter scsi%d.\n",
  2432. instance->host->host_no);
  2433. *convert = 1;
  2434. retval = 1;
  2435. }
  2436. out:
  2437. return retval;
  2438. }
  2439. void megasas_reset_reply_desc(struct megasas_instance *instance)
  2440. {
  2441. int i, j, count;
  2442. struct fusion_context *fusion;
  2443. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  2444. fusion = instance->ctrl_context;
  2445. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  2446. for (i = 0 ; i < count ; i++) {
  2447. fusion->last_reply_idx[i] = 0;
  2448. reply_desc = fusion->reply_frames_desc[i];
  2449. for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
  2450. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  2451. }
  2452. }
  2453. /*
  2454. * megasas_refire_mgmt_cmd : Re-fire management commands
  2455. * @instance: Controller's soft instance
  2456. */
  2457. void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
  2458. {
  2459. int j;
  2460. struct megasas_cmd_fusion *cmd_fusion;
  2461. struct fusion_context *fusion;
  2462. struct megasas_cmd *cmd_mfi;
  2463. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2464. u16 smid;
  2465. bool refire_cmd = 0;
  2466. fusion = instance->ctrl_context;
  2467. /* Re-fire management commands.
  2468. * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
  2469. */
  2470. for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
  2471. cmd_fusion = fusion->cmd_list[j];
  2472. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  2473. smid = le16_to_cpu(cmd_mfi->context.smid);
  2474. if (!smid)
  2475. continue;
  2476. req_desc = megasas_get_request_descriptor
  2477. (instance, smid - 1);
  2478. refire_cmd = req_desc && ((cmd_mfi->frame->dcmd.opcode !=
  2479. cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
  2480. (cmd_mfi->frame->dcmd.opcode !=
  2481. cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO)))
  2482. && !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
  2483. if (refire_cmd)
  2484. megasas_fire_cmd_fusion(instance, req_desc);
  2485. else
  2486. megasas_return_cmd(instance, cmd_mfi);
  2487. }
  2488. }
  2489. /*
  2490. * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
  2491. * @instance: per adapter struct
  2492. * @channel: the channel assigned by the OS
  2493. * @id: the id assigned by the OS
  2494. *
  2495. * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
  2496. */
  2497. static int megasas_track_scsiio(struct megasas_instance *instance,
  2498. int id, int channel)
  2499. {
  2500. int i, found = 0;
  2501. struct megasas_cmd_fusion *cmd_fusion;
  2502. struct fusion_context *fusion;
  2503. fusion = instance->ctrl_context;
  2504. for (i = 0 ; i < instance->max_scsi_cmds; i++) {
  2505. cmd_fusion = fusion->cmd_list[i];
  2506. if (cmd_fusion->scmd &&
  2507. (cmd_fusion->scmd->device->id == id &&
  2508. cmd_fusion->scmd->device->channel == channel)) {
  2509. dev_info(&instance->pdev->dev,
  2510. "SCSI commands pending to target"
  2511. "channel %d id %d \tSMID: 0x%x\n",
  2512. channel, id, cmd_fusion->index);
  2513. scsi_print_command(cmd_fusion->scmd);
  2514. found = 1;
  2515. break;
  2516. }
  2517. }
  2518. return found ? FAILED : SUCCESS;
  2519. }
  2520. /**
  2521. * megasas_tm_response_code - translation of device response code
  2522. * @ioc: per adapter object
  2523. * @mpi_reply: MPI reply returned by firmware
  2524. *
  2525. * Return nothing.
  2526. */
  2527. static void
  2528. megasas_tm_response_code(struct megasas_instance *instance,
  2529. struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
  2530. {
  2531. char *desc;
  2532. switch (mpi_reply->ResponseCode) {
  2533. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  2534. desc = "task management request completed";
  2535. break;
  2536. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  2537. desc = "invalid frame";
  2538. break;
  2539. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  2540. desc = "task management request not supported";
  2541. break;
  2542. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  2543. desc = "task management request failed";
  2544. break;
  2545. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  2546. desc = "task management request succeeded";
  2547. break;
  2548. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  2549. desc = "invalid lun";
  2550. break;
  2551. case 0xA:
  2552. desc = "overlapped tag attempted";
  2553. break;
  2554. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  2555. desc = "task queued, however not sent to target";
  2556. break;
  2557. default:
  2558. desc = "unknown";
  2559. break;
  2560. }
  2561. dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
  2562. mpi_reply->ResponseCode, desc);
  2563. dev_dbg(&instance->pdev->dev,
  2564. "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
  2565. " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
  2566. mpi_reply->TerminationCount, mpi_reply->DevHandle,
  2567. mpi_reply->Function, mpi_reply->TaskType,
  2568. mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
  2569. }
  2570. /**
  2571. * megasas_issue_tm - main routine for sending tm requests
  2572. * @instance: per adapter struct
  2573. * @device_handle: device handle
  2574. * @channel: the channel assigned by the OS
  2575. * @id: the id assigned by the OS
  2576. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
  2577. * @smid_task: smid assigned to the task
  2578. * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
  2579. * Context: user
  2580. *
  2581. * MegaRaid use MPT interface for Task Magement request.
  2582. * A generic API for sending task management requests to firmware.
  2583. *
  2584. * Return SUCCESS or FAILED.
  2585. */
  2586. static int
  2587. megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
  2588. uint channel, uint id, u16 smid_task, u8 type)
  2589. {
  2590. struct MR_TASK_MANAGE_REQUEST *mr_request;
  2591. struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
  2592. unsigned long timeleft;
  2593. struct megasas_cmd_fusion *cmd_fusion;
  2594. struct megasas_cmd *cmd_mfi;
  2595. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2596. struct fusion_context *fusion;
  2597. struct megasas_cmd_fusion *scsi_lookup;
  2598. int rc;
  2599. struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
  2600. fusion = instance->ctrl_context;
  2601. cmd_mfi = megasas_get_cmd(instance);
  2602. if (!cmd_mfi) {
  2603. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  2604. __func__, __LINE__);
  2605. return -ENOMEM;
  2606. }
  2607. cmd_fusion = megasas_get_cmd_fusion(instance,
  2608. instance->max_scsi_cmds + cmd_mfi->index);
  2609. /* Save the smid. To be used for returning the cmd */
  2610. cmd_mfi->context.smid = cmd_fusion->index;
  2611. req_desc = megasas_get_request_descriptor(instance,
  2612. (cmd_fusion->index - 1));
  2613. if (!req_desc) {
  2614. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  2615. __func__, __LINE__);
  2616. megasas_return_cmd(instance, cmd_mfi);
  2617. return -ENOMEM;
  2618. }
  2619. cmd_fusion->request_desc = req_desc;
  2620. req_desc->Words = 0;
  2621. scsi_lookup = fusion->cmd_list[smid_task - 1];
  2622. mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
  2623. memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
  2624. mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
  2625. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2626. mpi_request->DevHandle = cpu_to_le16(device_handle);
  2627. mpi_request->TaskType = type;
  2628. mpi_request->TaskMID = cpu_to_le16(smid_task);
  2629. mpi_request->LUN[1] = 0;
  2630. req_desc = cmd_fusion->request_desc;
  2631. req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
  2632. req_desc->HighPriority.RequestFlags =
  2633. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
  2634. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2635. req_desc->HighPriority.MSIxIndex = 0;
  2636. req_desc->HighPriority.LMID = 0;
  2637. req_desc->HighPriority.Reserved1 = 0;
  2638. if (channel < MEGASAS_MAX_PD_CHANNELS)
  2639. mr_request->tmReqFlags.isTMForPD = 1;
  2640. else
  2641. mr_request->tmReqFlags.isTMForLD = 1;
  2642. init_completion(&cmd_fusion->done);
  2643. megasas_fire_cmd_fusion(instance, req_desc);
  2644. timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);
  2645. if (!timeleft) {
  2646. dev_err(&instance->pdev->dev,
  2647. "task mgmt type 0x%x timed out\n", type);
  2648. cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
  2649. mutex_unlock(&instance->reset_mutex);
  2650. rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
  2651. mutex_lock(&instance->reset_mutex);
  2652. return rc;
  2653. }
  2654. mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
  2655. megasas_tm_response_code(instance, mpi_reply);
  2656. megasas_return_cmd(instance, cmd_mfi);
  2657. rc = SUCCESS;
  2658. switch (type) {
  2659. case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  2660. if (scsi_lookup->scmd == NULL)
  2661. break;
  2662. else {
  2663. instance->instancet->disable_intr(instance);
  2664. msleep(1000);
  2665. megasas_complete_cmd_dpc_fusion
  2666. ((unsigned long)instance);
  2667. instance->instancet->enable_intr(instance);
  2668. if (scsi_lookup->scmd == NULL)
  2669. break;
  2670. }
  2671. rc = FAILED;
  2672. break;
  2673. case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  2674. if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
  2675. break;
  2676. instance->instancet->disable_intr(instance);
  2677. msleep(1000);
  2678. megasas_complete_cmd_dpc_fusion
  2679. ((unsigned long)instance);
  2680. rc = megasas_track_scsiio(instance, id, channel);
  2681. instance->instancet->enable_intr(instance);
  2682. break;
  2683. case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
  2684. case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
  2685. break;
  2686. default:
  2687. rc = FAILED;
  2688. break;
  2689. }
  2690. return rc;
  2691. }
  2692. /*
  2693. * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
  2694. * @instance: per adapter struct
  2695. *
  2696. * Return Non Zero index, if SMID found in outstanding commands
  2697. */
  2698. static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
  2699. {
  2700. int i, ret = 0;
  2701. struct megasas_instance *instance;
  2702. struct megasas_cmd_fusion *cmd_fusion;
  2703. struct fusion_context *fusion;
  2704. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2705. fusion = instance->ctrl_context;
  2706. for (i = 0; i < instance->max_scsi_cmds; i++) {
  2707. cmd_fusion = fusion->cmd_list[i];
  2708. if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
  2709. scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
  2710. " SMID: %d\n", cmd_fusion->index);
  2711. ret = cmd_fusion->index;
  2712. break;
  2713. }
  2714. }
  2715. return ret;
  2716. }
  2717. /*
  2718. * megasas_get_tm_devhandle - Get devhandle for TM request
  2719. * @sdev- OS provided scsi device
  2720. *
  2721. * Returns- devhandle/targetID of SCSI device
  2722. */
  2723. static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
  2724. {
  2725. u16 pd_index = 0;
  2726. u32 device_id;
  2727. struct megasas_instance *instance;
  2728. struct fusion_context *fusion;
  2729. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  2730. u16 devhandle = (u16)ULONG_MAX;
  2731. instance = (struct megasas_instance *)sdev->host->hostdata;
  2732. fusion = instance->ctrl_context;
  2733. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
  2734. if (instance->use_seqnum_jbod_fp) {
  2735. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  2736. sdev->id;
  2737. pd_sync = (void *)fusion->pd_seq_sync
  2738. [(instance->pd_seq_map_id - 1) & 1];
  2739. devhandle = pd_sync->seq[pd_index].devHandle;
  2740. } else
  2741. sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
  2742. " without JBOD MAP support from %s %d\n", __func__, __LINE__);
  2743. } else {
  2744. device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
  2745. + sdev->id;
  2746. devhandle = device_id;
  2747. }
  2748. return devhandle;
  2749. }
  2750. /*
  2751. * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
  2752. * @scmd : pointer to scsi command object
  2753. *
  2754. * Return SUCCESS, if command aborted else FAILED
  2755. */
  2756. int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
  2757. {
  2758. struct megasas_instance *instance;
  2759. u16 smid, devhandle;
  2760. struct fusion_context *fusion;
  2761. int ret;
  2762. struct MR_PRIV_DEVICE *mr_device_priv_data;
  2763. mr_device_priv_data = scmd->device->hostdata;
  2764. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2765. fusion = instance->ctrl_context;
  2766. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2767. dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
  2768. "SCSI host:%d\n", instance->host->host_no);
  2769. ret = FAILED;
  2770. return ret;
  2771. }
  2772. if (!mr_device_priv_data) {
  2773. sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
  2774. "scmd(%p)\n", scmd);
  2775. scmd->result = DID_NO_CONNECT << 16;
  2776. ret = SUCCESS;
  2777. goto out;
  2778. }
  2779. if (!mr_device_priv_data->is_tm_capable) {
  2780. ret = FAILED;
  2781. goto out;
  2782. }
  2783. mutex_lock(&instance->reset_mutex);
  2784. smid = megasas_fusion_smid_lookup(scmd);
  2785. if (!smid) {
  2786. ret = SUCCESS;
  2787. scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
  2788. " issued is not found in oustanding commands\n");
  2789. mutex_unlock(&instance->reset_mutex);
  2790. goto out;
  2791. }
  2792. devhandle = megasas_get_tm_devhandle(scmd->device);
  2793. if (devhandle == (u16)ULONG_MAX) {
  2794. ret = SUCCESS;
  2795. sdev_printk(KERN_INFO, scmd->device,
  2796. "task abort issued for invalid devhandle\n");
  2797. mutex_unlock(&instance->reset_mutex);
  2798. goto out;
  2799. }
  2800. sdev_printk(KERN_INFO, scmd->device,
  2801. "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
  2802. scmd, devhandle);
  2803. mr_device_priv_data->tm_busy = 1;
  2804. ret = megasas_issue_tm(instance, devhandle,
  2805. scmd->device->channel, scmd->device->id, smid,
  2806. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
  2807. mr_device_priv_data->tm_busy = 0;
  2808. mutex_unlock(&instance->reset_mutex);
  2809. out:
  2810. sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
  2811. ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2812. return ret;
  2813. }
  2814. /*
  2815. * megasas_reset_target_fusion : target reset function for fusion adapters
  2816. * scmd: SCSI command pointer
  2817. *
  2818. * Returns SUCCESS if all commands associated with target aborted else FAILED
  2819. */
  2820. int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
  2821. {
  2822. struct megasas_instance *instance;
  2823. int ret = FAILED;
  2824. u16 devhandle;
  2825. struct fusion_context *fusion;
  2826. struct MR_PRIV_DEVICE *mr_device_priv_data;
  2827. mr_device_priv_data = scmd->device->hostdata;
  2828. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2829. fusion = instance->ctrl_context;
  2830. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2831. dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
  2832. "SCSI host:%d\n", instance->host->host_no);
  2833. ret = FAILED;
  2834. return ret;
  2835. }
  2836. if (!mr_device_priv_data) {
  2837. sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
  2838. "scmd(%p)\n", scmd);
  2839. scmd->result = DID_NO_CONNECT << 16;
  2840. ret = SUCCESS;
  2841. goto out;
  2842. }
  2843. if (!mr_device_priv_data->is_tm_capable) {
  2844. ret = FAILED;
  2845. goto out;
  2846. }
  2847. mutex_lock(&instance->reset_mutex);
  2848. devhandle = megasas_get_tm_devhandle(scmd->device);
  2849. if (devhandle == (u16)ULONG_MAX) {
  2850. ret = SUCCESS;
  2851. sdev_printk(KERN_INFO, scmd->device,
  2852. "target reset issued for invalid devhandle\n");
  2853. mutex_unlock(&instance->reset_mutex);
  2854. goto out;
  2855. }
  2856. sdev_printk(KERN_INFO, scmd->device,
  2857. "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
  2858. scmd, devhandle);
  2859. mr_device_priv_data->tm_busy = 1;
  2860. ret = megasas_issue_tm(instance, devhandle,
  2861. scmd->device->channel, scmd->device->id, 0,
  2862. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
  2863. mr_device_priv_data->tm_busy = 0;
  2864. mutex_unlock(&instance->reset_mutex);
  2865. out:
  2866. scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
  2867. (ret == SUCCESS) ? "SUCCESS" : "FAILED");
  2868. return ret;
  2869. }
  2870. /*SRIOV get other instance in cluster if any*/
  2871. struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
  2872. {
  2873. int i;
  2874. for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
  2875. if (megasas_mgmt_info.instance[i] &&
  2876. (megasas_mgmt_info.instance[i] != instance) &&
  2877. megasas_mgmt_info.instance[i]->requestorId &&
  2878. megasas_mgmt_info.instance[i]->peerIsPresent &&
  2879. (memcmp((megasas_mgmt_info.instance[i]->clusterId),
  2880. instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
  2881. return megasas_mgmt_info.instance[i];
  2882. }
  2883. return NULL;
  2884. }
  2885. /* Check for a second path that is currently UP */
  2886. int megasas_check_mpio_paths(struct megasas_instance *instance,
  2887. struct scsi_cmnd *scmd)
  2888. {
  2889. struct megasas_instance *peer_instance = NULL;
  2890. int retval = (DID_RESET << 16);
  2891. if (instance->peerIsPresent) {
  2892. peer_instance = megasas_get_peer_instance(instance);
  2893. if ((peer_instance) &&
  2894. (atomic_read(&peer_instance->adprecovery) ==
  2895. MEGASAS_HBA_OPERATIONAL))
  2896. retval = (DID_NO_CONNECT << 16);
  2897. }
  2898. return retval;
  2899. }
  2900. /* Core fusion reset function */
  2901. int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
  2902. {
  2903. int retval = SUCCESS, i, convert = 0;
  2904. struct megasas_instance *instance;
  2905. struct megasas_cmd_fusion *cmd_fusion;
  2906. struct fusion_context *fusion;
  2907. u32 abs_state, status_reg, reset_adapter;
  2908. u32 io_timeout_in_crash_mode = 0;
  2909. struct scsi_cmnd *scmd_local = NULL;
  2910. struct scsi_device *sdev;
  2911. instance = (struct megasas_instance *)shost->hostdata;
  2912. fusion = instance->ctrl_context;
  2913. mutex_lock(&instance->reset_mutex);
  2914. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2915. dev_warn(&instance->pdev->dev, "Hardware critical error, "
  2916. "returning FAILED for scsi%d.\n",
  2917. instance->host->host_no);
  2918. mutex_unlock(&instance->reset_mutex);
  2919. return FAILED;
  2920. }
  2921. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  2922. abs_state = status_reg & MFI_STATE_MASK;
  2923. /* IO timeout detected, forcibly put FW in FAULT state */
  2924. if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
  2925. instance->crash_dump_app_support && reason) {
  2926. dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
  2927. "forcibly FAULT Firmware\n");
  2928. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  2929. status_reg = readl(&instance->reg_set->doorbell);
  2930. writel(status_reg | MFI_STATE_FORCE_OCR,
  2931. &instance->reg_set->doorbell);
  2932. readl(&instance->reg_set->doorbell);
  2933. mutex_unlock(&instance->reset_mutex);
  2934. do {
  2935. ssleep(3);
  2936. io_timeout_in_crash_mode++;
  2937. dev_dbg(&instance->pdev->dev, "waiting for [%d] "
  2938. "seconds for crash dump collection and OCR "
  2939. "to be done\n", (io_timeout_in_crash_mode * 3));
  2940. } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
  2941. (io_timeout_in_crash_mode < 80));
  2942. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
  2943. dev_info(&instance->pdev->dev, "OCR done for IO "
  2944. "timeout case\n");
  2945. retval = SUCCESS;
  2946. } else {
  2947. dev_info(&instance->pdev->dev, "Controller is not "
  2948. "operational after 240 seconds wait for IO "
  2949. "timeout case in FW crash dump mode\n do "
  2950. "OCR/kill adapter\n");
  2951. retval = megasas_reset_fusion(shost, 0);
  2952. }
  2953. return retval;
  2954. }
  2955. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  2956. del_timer_sync(&instance->sriov_heartbeat_timer);
  2957. set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  2958. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
  2959. instance->instancet->disable_intr(instance);
  2960. msleep(1000);
  2961. /* First try waiting for commands to complete */
  2962. if (megasas_wait_for_outstanding_fusion(instance, reason,
  2963. &convert)) {
  2964. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  2965. dev_warn(&instance->pdev->dev, "resetting fusion "
  2966. "adapter scsi%d.\n", instance->host->host_no);
  2967. if (convert)
  2968. reason = 0;
  2969. /* Now return commands back to the OS */
  2970. for (i = 0 ; i < instance->max_scsi_cmds; i++) {
  2971. cmd_fusion = fusion->cmd_list[i];
  2972. scmd_local = cmd_fusion->scmd;
  2973. if (cmd_fusion->scmd) {
  2974. scmd_local->result =
  2975. megasas_check_mpio_paths(instance,
  2976. scmd_local);
  2977. if (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
  2978. atomic_dec(&instance->ldio_outstanding);
  2979. megasas_return_cmd_fusion(instance, cmd_fusion);
  2980. scsi_dma_unmap(scmd_local);
  2981. scmd_local->scsi_done(scmd_local);
  2982. atomic_dec(&instance->fw_outstanding);
  2983. }
  2984. }
  2985. status_reg = instance->instancet->read_fw_status_reg(
  2986. instance->reg_set);
  2987. abs_state = status_reg & MFI_STATE_MASK;
  2988. reset_adapter = status_reg & MFI_RESET_ADAPTER;
  2989. if (instance->disableOnlineCtrlReset ||
  2990. (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
  2991. /* Reset not supported, kill adapter */
  2992. dev_warn(&instance->pdev->dev, "Reset not supported"
  2993. ", killing adapter scsi%d.\n",
  2994. instance->host->host_no);
  2995. megaraid_sas_kill_hba(instance);
  2996. instance->skip_heartbeat_timer_del = 1;
  2997. retval = FAILED;
  2998. goto out;
  2999. }
  3000. /* Let SR-IOV VF & PF sync up if there was a HB failure */
  3001. if (instance->requestorId && !reason) {
  3002. msleep(MEGASAS_OCR_SETTLE_TIME_VF);
  3003. goto transition_to_ready;
  3004. }
  3005. /* Now try to reset the chip */
  3006. for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
  3007. if (instance->instancet->adp_reset
  3008. (instance, instance->reg_set))
  3009. continue;
  3010. transition_to_ready:
  3011. /* Wait for FW to become ready */
  3012. if (megasas_transition_to_ready(instance, 1)) {
  3013. dev_warn(&instance->pdev->dev,
  3014. "Failed to transition controller to ready for "
  3015. "scsi%d.\n", instance->host->host_no);
  3016. if (instance->requestorId && !reason)
  3017. goto fail_kill_adapter;
  3018. else
  3019. continue;
  3020. }
  3021. megasas_reset_reply_desc(instance);
  3022. megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
  3023. if (megasas_ioc_init_fusion(instance)) {
  3024. dev_warn(&instance->pdev->dev,
  3025. "megasas_ioc_init_fusion() failed! for "
  3026. "scsi%d\n", instance->host->host_no);
  3027. if (instance->requestorId && !reason)
  3028. goto fail_kill_adapter;
  3029. else
  3030. continue;
  3031. }
  3032. megasas_refire_mgmt_cmd(instance);
  3033. if (megasas_get_ctrl_info(instance)) {
  3034. dev_info(&instance->pdev->dev,
  3035. "Failed from %s %d\n",
  3036. __func__, __LINE__);
  3037. megaraid_sas_kill_hba(instance);
  3038. retval = FAILED;
  3039. }
  3040. /* Reset load balance info */
  3041. memset(fusion->load_balance_info, 0,
  3042. sizeof(struct LD_LOAD_BALANCE_INFO)
  3043. *MAX_LOGICAL_DRIVES_EXT);
  3044. if (!megasas_get_map_info(instance))
  3045. megasas_sync_map_info(instance);
  3046. megasas_setup_jbod_map(instance);
  3047. shost_for_each_device(sdev, shost)
  3048. megasas_update_sdev_properties(sdev);
  3049. clear_bit(MEGASAS_FUSION_IN_RESET,
  3050. &instance->reset_flags);
  3051. instance->instancet->enable_intr(instance);
  3052. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  3053. /* Restart SR-IOV heartbeat */
  3054. if (instance->requestorId) {
  3055. if (!megasas_sriov_start_heartbeat(instance, 0))
  3056. megasas_start_timer(instance,
  3057. &instance->sriov_heartbeat_timer,
  3058. megasas_sriov_heartbeat_handler,
  3059. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  3060. else
  3061. instance->skip_heartbeat_timer_del = 1;
  3062. }
  3063. /* Adapter reset completed successfully */
  3064. dev_warn(&instance->pdev->dev, "Reset "
  3065. "successful for scsi%d.\n",
  3066. instance->host->host_no);
  3067. if (instance->crash_dump_drv_support &&
  3068. instance->crash_dump_app_support)
  3069. megasas_set_crash_dump_params(instance,
  3070. MR_CRASH_BUF_TURN_ON);
  3071. else
  3072. megasas_set_crash_dump_params(instance,
  3073. MR_CRASH_BUF_TURN_OFF);
  3074. retval = SUCCESS;
  3075. goto out;
  3076. }
  3077. fail_kill_adapter:
  3078. /* Reset failed, kill the adapter */
  3079. dev_warn(&instance->pdev->dev, "Reset failed, killing "
  3080. "adapter scsi%d.\n", instance->host->host_no);
  3081. megaraid_sas_kill_hba(instance);
  3082. instance->skip_heartbeat_timer_del = 1;
  3083. retval = FAILED;
  3084. } else {
  3085. /* For VF: Restart HB timer if we didn't OCR */
  3086. if (instance->requestorId) {
  3087. megasas_start_timer(instance,
  3088. &instance->sriov_heartbeat_timer,
  3089. megasas_sriov_heartbeat_handler,
  3090. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  3091. }
  3092. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  3093. instance->instancet->enable_intr(instance);
  3094. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  3095. }
  3096. out:
  3097. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  3098. mutex_unlock(&instance->reset_mutex);
  3099. return retval;
  3100. }
  3101. /* Fusion Crash dump collection work queue */
  3102. void megasas_fusion_crash_dump_wq(struct work_struct *work)
  3103. {
  3104. struct megasas_instance *instance =
  3105. container_of(work, struct megasas_instance, crash_init);
  3106. u32 status_reg;
  3107. u8 partial_copy = 0;
  3108. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  3109. /*
  3110. * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
  3111. * to host crash buffers
  3112. */
  3113. if (instance->drv_buf_index == 0) {
  3114. /* Buffer is already allocated for old Crash dump.
  3115. * Do OCR and do not wait for crash dump collection
  3116. */
  3117. if (instance->drv_buf_alloc) {
  3118. dev_info(&instance->pdev->dev, "earlier crash dump is "
  3119. "not yet copied by application, ignoring this "
  3120. "crash dump and initiating OCR\n");
  3121. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  3122. writel(status_reg,
  3123. &instance->reg_set->outbound_scratch_pad);
  3124. readl(&instance->reg_set->outbound_scratch_pad);
  3125. return;
  3126. }
  3127. megasas_alloc_host_crash_buffer(instance);
  3128. dev_info(&instance->pdev->dev, "Number of host crash buffers "
  3129. "allocated: %d\n", instance->drv_buf_alloc);
  3130. }
  3131. /*
  3132. * Driver has allocated max buffers, which can be allocated
  3133. * and FW has more crash dump data, then driver will
  3134. * ignore the data.
  3135. */
  3136. if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
  3137. dev_info(&instance->pdev->dev, "Driver is done copying "
  3138. "the buffer: %d\n", instance->drv_buf_alloc);
  3139. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  3140. partial_copy = 1;
  3141. } else {
  3142. memcpy(instance->crash_buf[instance->drv_buf_index],
  3143. instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
  3144. instance->drv_buf_index++;
  3145. status_reg &= ~MFI_STATE_DMADONE;
  3146. }
  3147. if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
  3148. dev_info(&instance->pdev->dev, "Crash Dump is available,number "
  3149. "of copied buffers: %d\n", instance->drv_buf_index);
  3150. instance->fw_crash_buffer_size = instance->drv_buf_index;
  3151. instance->fw_crash_state = AVAILABLE;
  3152. instance->drv_buf_index = 0;
  3153. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  3154. readl(&instance->reg_set->outbound_scratch_pad);
  3155. if (!partial_copy)
  3156. megasas_reset_fusion(instance->host, 0);
  3157. } else {
  3158. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  3159. readl(&instance->reg_set->outbound_scratch_pad);
  3160. }
  3161. }
  3162. /* Fusion OCR work queue */
  3163. void megasas_fusion_ocr_wq(struct work_struct *work)
  3164. {
  3165. struct megasas_instance *instance =
  3166. container_of(work, struct megasas_instance, work_init);
  3167. megasas_reset_fusion(instance->host, 0);
  3168. }
  3169. struct megasas_instance_template megasas_instance_template_fusion = {
  3170. .enable_intr = megasas_enable_intr_fusion,
  3171. .disable_intr = megasas_disable_intr_fusion,
  3172. .clear_intr = megasas_clear_intr_fusion,
  3173. .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
  3174. .adp_reset = megasas_adp_reset_fusion,
  3175. .check_reset = megasas_check_reset_fusion,
  3176. .service_isr = megasas_isr_fusion,
  3177. .tasklet = megasas_complete_cmd_dpc_fusion,
  3178. .init_adapter = megasas_init_adapter_fusion,
  3179. .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
  3180. .issue_dcmd = megasas_issue_dcmd_fusion,
  3181. };