megaraid_sas_fusion.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2012 LSI Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * FILE: megaraid_sas_fusion.c
  21. *
  22. * Authors: LSI Corporation
  23. * Sumant Patro
  24. * Adam Radford <linuxraid@lsi.com>
  25. *
  26. * Send feedback to: <megaraidlinux@lsi.com>
  27. *
  28. * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
  29. * ATTN: Linuxraid
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/pci.h>
  34. #include <linux/list.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/module.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/delay.h>
  40. #include <linux/uio.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/fs.h>
  43. #include <linux/compat.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/mutex.h>
  46. #include <linux/poll.h>
  47. #include <scsi/scsi.h>
  48. #include <scsi/scsi_cmnd.h>
  49. #include <scsi/scsi_device.h>
  50. #include <scsi/scsi_host.h>
  51. #include <scsi/scsi_dbg.h>
  52. #include "megaraid_sas_fusion.h"
  53. #include "megaraid_sas.h"
  54. extern void megasas_free_cmds(struct megasas_instance *instance);
  55. extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  56. *instance);
  57. extern void
  58. megasas_complete_cmd(struct megasas_instance *instance,
  59. struct megasas_cmd *cmd, u8 alt_status);
  60. int megasas_is_ldio(struct scsi_cmnd *cmd);
  61. int
  62. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  63. int seconds);
  64. void
  65. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
  66. int megasas_alloc_cmds(struct megasas_instance *instance);
  67. int
  68. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
  69. int
  70. megasas_issue_polled(struct megasas_instance *instance,
  71. struct megasas_cmd *cmd);
  72. void
  73. megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
  74. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  75. void megaraid_sas_kill_hba(struct megasas_instance *instance);
  76. extern u32 megasas_dbg_lvl;
  77. void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
  78. int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
  79. int initial);
  80. void megasas_start_timer(struct megasas_instance *instance,
  81. struct timer_list *timer,
  82. void *fn, unsigned long interval);
  83. extern struct megasas_mgmt_info megasas_mgmt_info;
  84. extern int resetwaittime;
  85. /**
  86. * megasas_enable_intr_fusion - Enables interrupts
  87. * @regs: MFI register set
  88. */
  89. void
  90. megasas_enable_intr_fusion(struct megasas_instance *instance)
  91. {
  92. struct megasas_register_set __iomem *regs;
  93. regs = instance->reg_set;
  94. instance->mask_interrupts = 0;
  95. /* For Thunderbolt/Invader also clear intr on enable */
  96. writel(~0, &regs->outbound_intr_status);
  97. readl(&regs->outbound_intr_status);
  98. writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  99. /* Dummy readl to force pci flush */
  100. readl(&regs->outbound_intr_mask);
  101. }
  102. /**
  103. * megasas_disable_intr_fusion - Disables interrupt
  104. * @regs: MFI register set
  105. */
  106. void
  107. megasas_disable_intr_fusion(struct megasas_instance *instance)
  108. {
  109. u32 mask = 0xFFFFFFFF;
  110. u32 status;
  111. struct megasas_register_set __iomem *regs;
  112. regs = instance->reg_set;
  113. instance->mask_interrupts = 1;
  114. writel(mask, &regs->outbound_intr_mask);
  115. /* Dummy readl to force pci flush */
  116. status = readl(&regs->outbound_intr_mask);
  117. }
  118. int
  119. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
  120. {
  121. u32 status;
  122. /*
  123. * Check if it is our interrupt
  124. */
  125. status = readl(&regs->outbound_intr_status);
  126. if (status & 1) {
  127. writel(status, &regs->outbound_intr_status);
  128. readl(&regs->outbound_intr_status);
  129. return 1;
  130. }
  131. if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
  132. return 0;
  133. return 1;
  134. }
  135. /**
  136. * megasas_get_cmd_fusion - Get a command from the free pool
  137. * @instance: Adapter soft state
  138. *
  139. * Returns a free command from the pool
  140. */
  141. struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
  142. *instance)
  143. {
  144. unsigned long flags;
  145. struct fusion_context *fusion =
  146. (struct fusion_context *)instance->ctrl_context;
  147. struct megasas_cmd_fusion *cmd = NULL;
  148. spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
  149. if (!list_empty(&fusion->cmd_pool)) {
  150. cmd = list_entry((&fusion->cmd_pool)->next,
  151. struct megasas_cmd_fusion, list);
  152. list_del_init(&cmd->list);
  153. } else {
  154. printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
  155. }
  156. spin_unlock_irqrestore(&fusion->mpt_pool_lock, flags);
  157. return cmd;
  158. }
  159. /**
  160. * megasas_return_cmd_fusion - Return a cmd to free command pool
  161. * @instance: Adapter soft state
  162. * @cmd: Command packet to be returned to free command pool
  163. */
  164. inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
  165. struct megasas_cmd_fusion *cmd)
  166. {
  167. unsigned long flags;
  168. struct fusion_context *fusion =
  169. (struct fusion_context *)instance->ctrl_context;
  170. spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
  171. cmd->scmd = NULL;
  172. cmd->sync_cmd_idx = (u32)ULONG_MAX;
  173. list_add(&cmd->list, (&fusion->cmd_pool)->next);
  174. spin_unlock_irqrestore(&fusion->mpt_pool_lock, flags);
  175. }
  176. /**
  177. * megasas_return_mfi_mpt_pthr - Return a mfi and mpt to free command pool
  178. * @instance: Adapter soft state
  179. * @cmd_mfi: MFI Command packet to be returned to free command pool
  180. * @cmd_mpt: MPT Command packet to be returned to free command pool
  181. */
  182. inline void megasas_return_mfi_mpt_pthr(struct megasas_instance *instance,
  183. struct megasas_cmd *cmd_mfi,
  184. struct megasas_cmd_fusion *cmd_fusion)
  185. {
  186. unsigned long flags;
  187. /*
  188. * TO DO: optimize this code and use only one lock instead of two
  189. * locks being used currently- mpt_pool_lock is acquired
  190. * inside mfi_pool_lock
  191. */
  192. spin_lock_irqsave(&instance->mfi_pool_lock, flags);
  193. megasas_return_cmd_fusion(instance, cmd_fusion);
  194. if (atomic_read(&cmd_mfi->mfi_mpt_pthr) != MFI_MPT_ATTACHED)
  195. dev_err(&instance->pdev->dev, "Possible bug from %s %d\n",
  196. __func__, __LINE__);
  197. atomic_set(&cmd_mfi->mfi_mpt_pthr, MFI_MPT_DETACHED);
  198. __megasas_return_cmd(instance, cmd_mfi);
  199. spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
  200. }
  201. /**
  202. * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
  203. * @instance: Adapter soft state
  204. */
  205. static void megasas_teardown_frame_pool_fusion(
  206. struct megasas_instance *instance)
  207. {
  208. int i;
  209. struct fusion_context *fusion = instance->ctrl_context;
  210. u16 max_cmd = instance->max_fw_cmds;
  211. struct megasas_cmd_fusion *cmd;
  212. if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
  213. printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
  214. "sense pool : %p\n", fusion->sg_dma_pool,
  215. fusion->sense_dma_pool);
  216. return;
  217. }
  218. /*
  219. * Return all frames to pool
  220. */
  221. for (i = 0; i < max_cmd; i++) {
  222. cmd = fusion->cmd_list[i];
  223. if (cmd->sg_frame)
  224. pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
  225. cmd->sg_frame_phys_addr);
  226. if (cmd->sense)
  227. pci_pool_free(fusion->sense_dma_pool, cmd->sense,
  228. cmd->sense_phys_addr);
  229. }
  230. /*
  231. * Now destroy the pool itself
  232. */
  233. pci_pool_destroy(fusion->sg_dma_pool);
  234. pci_pool_destroy(fusion->sense_dma_pool);
  235. fusion->sg_dma_pool = NULL;
  236. fusion->sense_dma_pool = NULL;
  237. }
  238. /**
  239. * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
  240. * @instance: Adapter soft state
  241. */
  242. void
  243. megasas_free_cmds_fusion(struct megasas_instance *instance)
  244. {
  245. int i;
  246. struct fusion_context *fusion = instance->ctrl_context;
  247. u32 max_cmds, req_sz, reply_sz, io_frames_sz;
  248. req_sz = fusion->request_alloc_sz;
  249. reply_sz = fusion->reply_alloc_sz;
  250. io_frames_sz = fusion->io_frames_alloc_sz;
  251. max_cmds = instance->max_fw_cmds;
  252. /* Free descriptors and request Frames memory */
  253. if (fusion->req_frames_desc)
  254. dma_free_coherent(&instance->pdev->dev, req_sz,
  255. fusion->req_frames_desc,
  256. fusion->req_frames_desc_phys);
  257. if (fusion->reply_frames_desc) {
  258. pci_pool_free(fusion->reply_frames_desc_pool,
  259. fusion->reply_frames_desc,
  260. fusion->reply_frames_desc_phys);
  261. pci_pool_destroy(fusion->reply_frames_desc_pool);
  262. }
  263. if (fusion->io_request_frames) {
  264. pci_pool_free(fusion->io_request_frames_pool,
  265. fusion->io_request_frames,
  266. fusion->io_request_frames_phys);
  267. pci_pool_destroy(fusion->io_request_frames_pool);
  268. }
  269. /* Free the Fusion frame pool */
  270. megasas_teardown_frame_pool_fusion(instance);
  271. /* Free all the commands in the cmd_list */
  272. for (i = 0; i < max_cmds; i++)
  273. kfree(fusion->cmd_list[i]);
  274. /* Free the cmd_list buffer itself */
  275. kfree(fusion->cmd_list);
  276. fusion->cmd_list = NULL;
  277. INIT_LIST_HEAD(&fusion->cmd_pool);
  278. }
  279. /**
  280. * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
  281. * @instance: Adapter soft state
  282. *
  283. */
  284. static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
  285. {
  286. int i;
  287. u32 max_cmd;
  288. struct fusion_context *fusion;
  289. struct megasas_cmd_fusion *cmd;
  290. u32 total_sz_chain_frame;
  291. fusion = instance->ctrl_context;
  292. max_cmd = instance->max_fw_cmds;
  293. total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
  294. /*
  295. * Use DMA pool facility provided by PCI layer
  296. */
  297. fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
  298. instance->pdev,
  299. total_sz_chain_frame, 4,
  300. 0);
  301. if (!fusion->sg_dma_pool) {
  302. printk(KERN_DEBUG "megasas: failed to setup request pool "
  303. "fusion\n");
  304. return -ENOMEM;
  305. }
  306. fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
  307. instance->pdev,
  308. SCSI_SENSE_BUFFERSIZE, 64, 0);
  309. if (!fusion->sense_dma_pool) {
  310. printk(KERN_DEBUG "megasas: failed to setup sense pool "
  311. "fusion\n");
  312. pci_pool_destroy(fusion->sg_dma_pool);
  313. fusion->sg_dma_pool = NULL;
  314. return -ENOMEM;
  315. }
  316. /*
  317. * Allocate and attach a frame to each of the commands in cmd_list
  318. */
  319. for (i = 0; i < max_cmd; i++) {
  320. cmd = fusion->cmd_list[i];
  321. cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
  322. GFP_KERNEL,
  323. &cmd->sg_frame_phys_addr);
  324. cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
  325. GFP_KERNEL, &cmd->sense_phys_addr);
  326. /*
  327. * megasas_teardown_frame_pool_fusion() takes care of freeing
  328. * whatever has been allocated
  329. */
  330. if (!cmd->sg_frame || !cmd->sense) {
  331. printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
  332. megasas_teardown_frame_pool_fusion(instance);
  333. return -ENOMEM;
  334. }
  335. }
  336. return 0;
  337. }
  338. /**
  339. * megasas_alloc_cmds_fusion - Allocates the command packets
  340. * @instance: Adapter soft state
  341. *
  342. *
  343. * Each frame has a 32-bit field called context. This context is used to get
  344. * back the megasas_cmd_fusion from the frame when a frame gets completed
  345. * In this driver, the 32 bit values are the indices into an array cmd_list.
  346. * This array is used only to look up the megasas_cmd_fusion given the context.
  347. * The free commands themselves are maintained in a linked list called cmd_pool.
  348. *
  349. * cmds are formed in the io_request and sg_frame members of the
  350. * megasas_cmd_fusion. The context field is used to get a request descriptor
  351. * and is used as SMID of the cmd.
  352. * SMID value range is from 1 to max_fw_cmds.
  353. */
  354. int
  355. megasas_alloc_cmds_fusion(struct megasas_instance *instance)
  356. {
  357. int i, j, count;
  358. u32 max_cmd, io_frames_sz;
  359. struct fusion_context *fusion;
  360. struct megasas_cmd_fusion *cmd;
  361. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  362. u32 offset;
  363. dma_addr_t io_req_base_phys;
  364. u8 *io_req_base;
  365. fusion = instance->ctrl_context;
  366. max_cmd = instance->max_fw_cmds;
  367. fusion->req_frames_desc =
  368. dma_alloc_coherent(&instance->pdev->dev,
  369. fusion->request_alloc_sz,
  370. &fusion->req_frames_desc_phys, GFP_KERNEL);
  371. if (!fusion->req_frames_desc) {
  372. printk(KERN_ERR "megasas; Could not allocate memory for "
  373. "request_frames\n");
  374. goto fail_req_desc;
  375. }
  376. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  377. fusion->reply_frames_desc_pool =
  378. pci_pool_create("reply_frames pool", instance->pdev,
  379. fusion->reply_alloc_sz * count, 16, 0);
  380. if (!fusion->reply_frames_desc_pool) {
  381. printk(KERN_ERR "megasas; Could not allocate memory for "
  382. "reply_frame pool\n");
  383. goto fail_reply_desc;
  384. }
  385. fusion->reply_frames_desc =
  386. pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
  387. &fusion->reply_frames_desc_phys);
  388. if (!fusion->reply_frames_desc) {
  389. printk(KERN_ERR "megasas; Could not allocate memory for "
  390. "reply_frame pool\n");
  391. pci_pool_destroy(fusion->reply_frames_desc_pool);
  392. goto fail_reply_desc;
  393. }
  394. reply_desc = fusion->reply_frames_desc;
  395. for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
  396. reply_desc->Words = ULLONG_MAX;
  397. io_frames_sz = fusion->io_frames_alloc_sz;
  398. fusion->io_request_frames_pool =
  399. pci_pool_create("io_request_frames pool", instance->pdev,
  400. fusion->io_frames_alloc_sz, 16, 0);
  401. if (!fusion->io_request_frames_pool) {
  402. printk(KERN_ERR "megasas: Could not allocate memory for "
  403. "io_request_frame pool\n");
  404. goto fail_io_frames;
  405. }
  406. fusion->io_request_frames =
  407. pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
  408. &fusion->io_request_frames_phys);
  409. if (!fusion->io_request_frames) {
  410. printk(KERN_ERR "megasas: Could not allocate memory for "
  411. "io_request_frames frames\n");
  412. pci_pool_destroy(fusion->io_request_frames_pool);
  413. goto fail_io_frames;
  414. }
  415. /*
  416. * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
  417. * Allocate the dynamic array first and then allocate individual
  418. * commands.
  419. */
  420. fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
  421. * max_cmd, GFP_KERNEL);
  422. if (!fusion->cmd_list) {
  423. printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
  424. "memory for cmd_list_fusion\n");
  425. goto fail_cmd_list;
  426. }
  427. max_cmd = instance->max_fw_cmds;
  428. for (i = 0; i < max_cmd; i++) {
  429. fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
  430. GFP_KERNEL);
  431. if (!fusion->cmd_list[i]) {
  432. printk(KERN_ERR "Could not alloc cmd list fusion\n");
  433. for (j = 0; j < i; j++)
  434. kfree(fusion->cmd_list[j]);
  435. kfree(fusion->cmd_list);
  436. fusion->cmd_list = NULL;
  437. goto fail_cmd_list;
  438. }
  439. }
  440. /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
  441. io_req_base = fusion->io_request_frames +
  442. MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  443. io_req_base_phys = fusion->io_request_frames_phys +
  444. MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  445. /*
  446. * Add all the commands to command pool (fusion->cmd_pool)
  447. */
  448. /* SMID 0 is reserved. Set SMID/index from 1 */
  449. for (i = 0; i < max_cmd; i++) {
  450. cmd = fusion->cmd_list[i];
  451. offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
  452. memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
  453. cmd->index = i + 1;
  454. cmd->scmd = NULL;
  455. cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
  456. cmd->instance = instance;
  457. cmd->io_request =
  458. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  459. (io_req_base + offset);
  460. memset(cmd->io_request, 0,
  461. sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
  462. cmd->io_request_phys_addr = io_req_base_phys + offset;
  463. list_add_tail(&cmd->list, &fusion->cmd_pool);
  464. }
  465. /*
  466. * Create a frame pool and assign one frame to each cmd
  467. */
  468. if (megasas_create_frame_pool_fusion(instance)) {
  469. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  470. megasas_free_cmds_fusion(instance);
  471. goto fail_req_desc;
  472. }
  473. return 0;
  474. fail_cmd_list:
  475. pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
  476. fusion->io_request_frames_phys);
  477. pci_pool_destroy(fusion->io_request_frames_pool);
  478. fail_io_frames:
  479. dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
  480. fusion->reply_frames_desc,
  481. fusion->reply_frames_desc_phys);
  482. pci_pool_free(fusion->reply_frames_desc_pool,
  483. fusion->reply_frames_desc,
  484. fusion->reply_frames_desc_phys);
  485. pci_pool_destroy(fusion->reply_frames_desc_pool);
  486. fail_reply_desc:
  487. dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
  488. fusion->req_frames_desc,
  489. fusion->req_frames_desc_phys);
  490. fail_req_desc:
  491. return -ENOMEM;
  492. }
  493. /**
  494. * wait_and_poll - Issues a polling command
  495. * @instance: Adapter soft state
  496. * @cmd: Command packet to be issued
  497. *
  498. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  499. */
  500. int
  501. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  502. int seconds)
  503. {
  504. int i;
  505. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  506. struct fusion_context *fusion;
  507. u32 msecs = seconds * 1000;
  508. fusion = instance->ctrl_context;
  509. /*
  510. * Wait for cmd_status to change
  511. */
  512. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
  513. rmb();
  514. msleep(20);
  515. }
  516. if (frame_hdr->cmd_status == 0xff) {
  517. if (fusion)
  518. megasas_return_mfi_mpt_pthr(instance, cmd,
  519. cmd->mpt_pthr_cmd_blocked);
  520. return -ETIME;
  521. }
  522. return 0;
  523. }
  524. /**
  525. * megasas_ioc_init_fusion - Initializes the FW
  526. * @instance: Adapter soft state
  527. *
  528. * Issues the IOC Init cmd
  529. */
  530. int
  531. megasas_ioc_init_fusion(struct megasas_instance *instance)
  532. {
  533. struct megasas_init_frame *init_frame;
  534. struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
  535. dma_addr_t ioc_init_handle;
  536. struct megasas_cmd *cmd;
  537. u8 ret;
  538. struct fusion_context *fusion;
  539. union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
  540. int i;
  541. struct megasas_header *frame_hdr;
  542. fusion = instance->ctrl_context;
  543. cmd = megasas_get_cmd(instance);
  544. if (!cmd) {
  545. printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
  546. ret = 1;
  547. goto fail_get_cmd;
  548. }
  549. IOCInitMessage =
  550. dma_alloc_coherent(&instance->pdev->dev,
  551. sizeof(struct MPI2_IOC_INIT_REQUEST),
  552. &ioc_init_handle, GFP_KERNEL);
  553. if (!IOCInitMessage) {
  554. printk(KERN_ERR "Could not allocate memory for "
  555. "IOCInitMessage\n");
  556. ret = 1;
  557. goto fail_fw_init;
  558. }
  559. memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
  560. IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
  561. IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  562. IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
  563. IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  564. IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
  565. IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
  566. IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
  567. IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
  568. IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
  569. init_frame = (struct megasas_init_frame *)cmd->frame;
  570. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  571. frame_hdr = &cmd->frame->hdr;
  572. frame_hdr->cmd_status = 0xFF;
  573. frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  574. init_frame->cmd = MFI_CMD_INIT;
  575. init_frame->cmd_status = 0xFF;
  576. /* driver support Extended MSIX */
  577. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  578. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
  579. init_frame->driver_operations.
  580. mfi_capabilities.support_additional_msix = 1;
  581. /* driver supports HA / Remote LUN over Fast Path interface */
  582. init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
  583. = 1;
  584. init_frame->driver_operations.mfi_capabilities.support_max_255lds
  585. = 1;
  586. init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb
  587. = 1;
  588. /* Convert capability to LE32 */
  589. cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
  590. init_frame->queue_info_new_phys_addr_hi =
  591. cpu_to_le32(upper_32_bits(ioc_init_handle));
  592. init_frame->queue_info_new_phys_addr_lo =
  593. cpu_to_le32(lower_32_bits(ioc_init_handle));
  594. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
  595. req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
  596. req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
  597. req_desc.MFAIo.RequestFlags =
  598. (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
  599. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  600. /*
  601. * disable the intr before firing the init frame
  602. */
  603. instance->instancet->disable_intr(instance);
  604. for (i = 0; i < (10 * 1000); i += 20) {
  605. if (readl(&instance->reg_set->doorbell) & 1)
  606. msleep(20);
  607. else
  608. break;
  609. }
  610. instance->instancet->fire_cmd(instance, req_desc.u.low,
  611. req_desc.u.high, instance->reg_set);
  612. wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
  613. frame_hdr = &cmd->frame->hdr;
  614. if (frame_hdr->cmd_status != 0) {
  615. ret = 1;
  616. goto fail_fw_init;
  617. }
  618. printk(KERN_ERR "megasas:IOC Init cmd success\n");
  619. ret = 0;
  620. fail_fw_init:
  621. megasas_return_cmd(instance, cmd);
  622. if (IOCInitMessage)
  623. dma_free_coherent(&instance->pdev->dev,
  624. sizeof(struct MPI2_IOC_INIT_REQUEST),
  625. IOCInitMessage, ioc_init_handle);
  626. fail_get_cmd:
  627. return ret;
  628. }
  629. /*
  630. * megasas_get_ld_map_info - Returns FW's ld_map structure
  631. * @instance: Adapter soft state
  632. * @pend: Pend the command or not
  633. * Issues an internal command (DCMD) to get the FW's controller PD
  634. * list structure. This information is mainly used to find out SYSTEM
  635. * supported by the FW.
  636. * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
  637. * dcmd.mbox.b[0] - number of LDs being sync'd
  638. * dcmd.mbox.b[1] - 0 - complete command immediately.
  639. * - 1 - pend till config change
  640. * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
  641. * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
  642. * uses extended struct MR_FW_RAID_MAP_EXT
  643. */
  644. static int
  645. megasas_get_ld_map_info(struct megasas_instance *instance)
  646. {
  647. int ret = 0;
  648. struct megasas_cmd *cmd;
  649. struct megasas_dcmd_frame *dcmd;
  650. void *ci;
  651. dma_addr_t ci_h = 0;
  652. u32 size_map_info;
  653. struct fusion_context *fusion;
  654. cmd = megasas_get_cmd(instance);
  655. if (!cmd) {
  656. printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
  657. return -ENOMEM;
  658. }
  659. fusion = instance->ctrl_context;
  660. if (!fusion) {
  661. megasas_return_cmd(instance, cmd);
  662. return -ENXIO;
  663. }
  664. dcmd = &cmd->frame->dcmd;
  665. size_map_info = fusion->current_map_sz;
  666. ci = (void *) fusion->ld_map[(instance->map_id & 1)];
  667. ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
  668. if (!ci) {
  669. printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
  670. megasas_return_cmd(instance, cmd);
  671. return -ENOMEM;
  672. }
  673. memset(ci, 0, fusion->max_map_sz);
  674. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  675. #if VD_EXT_DEBUG
  676. dev_dbg(&instance->pdev->dev,
  677. "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
  678. __func__, cpu_to_le32(size_map_info));
  679. #endif
  680. dcmd->cmd = MFI_CMD_DCMD;
  681. dcmd->cmd_status = 0xFF;
  682. dcmd->sge_count = 1;
  683. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  684. dcmd->timeout = 0;
  685. dcmd->pad_0 = 0;
  686. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  687. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  688. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  689. dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
  690. if (instance->ctrl_context && !instance->mask_interrupts)
  691. ret = megasas_issue_blocked_cmd(instance, cmd,
  692. MEGASAS_BLOCKED_CMD_TIMEOUT);
  693. else
  694. ret = megasas_issue_polled(instance, cmd);
  695. if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
  696. megasas_return_mfi_mpt_pthr(instance, cmd,
  697. cmd->mpt_pthr_cmd_blocked);
  698. else
  699. megasas_return_cmd(instance, cmd);
  700. return ret;
  701. }
  702. u8
  703. megasas_get_map_info(struct megasas_instance *instance)
  704. {
  705. struct fusion_context *fusion = instance->ctrl_context;
  706. fusion->fast_path_io = 0;
  707. if (!megasas_get_ld_map_info(instance)) {
  708. if (MR_ValidateMapInfo(instance)) {
  709. fusion->fast_path_io = 1;
  710. return 0;
  711. }
  712. }
  713. return 1;
  714. }
  715. /*
  716. * megasas_sync_map_info - Returns FW's ld_map structure
  717. * @instance: Adapter soft state
  718. *
  719. * Issues an internal command (DCMD) to get the FW's controller PD
  720. * list structure. This information is mainly used to find out SYSTEM
  721. * supported by the FW.
  722. */
  723. int
  724. megasas_sync_map_info(struct megasas_instance *instance)
  725. {
  726. int ret = 0, i;
  727. struct megasas_cmd *cmd;
  728. struct megasas_dcmd_frame *dcmd;
  729. u32 size_sync_info, num_lds;
  730. struct fusion_context *fusion;
  731. struct MR_LD_TARGET_SYNC *ci = NULL;
  732. struct MR_DRV_RAID_MAP_ALL *map;
  733. struct MR_LD_RAID *raid;
  734. struct MR_LD_TARGET_SYNC *ld_sync;
  735. dma_addr_t ci_h = 0;
  736. u32 size_map_info;
  737. cmd = megasas_get_cmd(instance);
  738. if (!cmd) {
  739. printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
  740. "info.\n");
  741. return -ENOMEM;
  742. }
  743. fusion = instance->ctrl_context;
  744. if (!fusion) {
  745. megasas_return_cmd(instance, cmd);
  746. return 1;
  747. }
  748. map = fusion->ld_drv_map[instance->map_id & 1];
  749. num_lds = le16_to_cpu(map->raidMap.ldCount);
  750. dcmd = &cmd->frame->dcmd;
  751. size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
  752. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  753. ci = (struct MR_LD_TARGET_SYNC *)
  754. fusion->ld_map[(instance->map_id - 1) & 1];
  755. memset(ci, 0, fusion->max_map_sz);
  756. ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
  757. ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
  758. for (i = 0; i < num_lds; i++, ld_sync++) {
  759. raid = MR_LdRaidGet(i, map);
  760. ld_sync->targetId = MR_GetLDTgtId(i, map);
  761. ld_sync->seqNum = raid->seqNum;
  762. }
  763. size_map_info = fusion->current_map_sz;
  764. dcmd->cmd = MFI_CMD_DCMD;
  765. dcmd->cmd_status = 0xFF;
  766. dcmd->sge_count = 1;
  767. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
  768. dcmd->timeout = 0;
  769. dcmd->pad_0 = 0;
  770. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  771. dcmd->mbox.b[0] = num_lds;
  772. dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  773. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  774. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  775. dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
  776. instance->map_update_cmd = cmd;
  777. instance->instancet->issue_dcmd(instance, cmd);
  778. return ret;
  779. }
  780. /*
  781. * meagasas_display_intel_branding - Display branding string
  782. * @instance: per adapter object
  783. *
  784. * Return nothing.
  785. */
  786. static void
  787. megasas_display_intel_branding(struct megasas_instance *instance)
  788. {
  789. if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  790. return;
  791. switch (instance->pdev->device) {
  792. case PCI_DEVICE_ID_LSI_INVADER:
  793. switch (instance->pdev->subsystem_device) {
  794. case MEGARAID_INTEL_RS3DC080_SSDID:
  795. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  796. instance->host->host_no,
  797. MEGARAID_INTEL_RS3DC080_BRANDING);
  798. break;
  799. case MEGARAID_INTEL_RS3DC040_SSDID:
  800. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  801. instance->host->host_no,
  802. MEGARAID_INTEL_RS3DC040_BRANDING);
  803. break;
  804. case MEGARAID_INTEL_RS3SC008_SSDID:
  805. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  806. instance->host->host_no,
  807. MEGARAID_INTEL_RS3SC008_BRANDING);
  808. break;
  809. case MEGARAID_INTEL_RS3MC044_SSDID:
  810. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  811. instance->host->host_no,
  812. MEGARAID_INTEL_RS3MC044_BRANDING);
  813. break;
  814. default:
  815. break;
  816. }
  817. break;
  818. case PCI_DEVICE_ID_LSI_FURY:
  819. switch (instance->pdev->subsystem_device) {
  820. case MEGARAID_INTEL_RS3WC080_SSDID:
  821. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  822. instance->host->host_no,
  823. MEGARAID_INTEL_RS3WC080_BRANDING);
  824. break;
  825. case MEGARAID_INTEL_RS3WC040_SSDID:
  826. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  827. instance->host->host_no,
  828. MEGARAID_INTEL_RS3WC040_BRANDING);
  829. break;
  830. default:
  831. break;
  832. }
  833. break;
  834. default:
  835. break;
  836. }
  837. }
  838. /**
  839. * megasas_init_adapter_fusion - Initializes the FW
  840. * @instance: Adapter soft state
  841. *
  842. * This is the main function for initializing firmware.
  843. */
  844. u32
  845. megasas_init_adapter_fusion(struct megasas_instance *instance)
  846. {
  847. struct megasas_register_set __iomem *reg_set;
  848. struct fusion_context *fusion;
  849. u32 max_cmd;
  850. int i = 0, count;
  851. fusion = instance->ctrl_context;
  852. reg_set = instance->reg_set;
  853. /*
  854. * Get various operational parameters from status register
  855. */
  856. instance->max_fw_cmds =
  857. instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  858. instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
  859. /*
  860. * Reduce the max supported cmds by 1. This is to ensure that the
  861. * reply_q_sz (1 more than the max cmd that driver may send)
  862. * does not exceed max cmds that the FW can support
  863. */
  864. instance->max_fw_cmds = instance->max_fw_cmds-1;
  865. /* Only internal cmds (DCMD) need to have MFI frames */
  866. instance->max_mfi_cmds = MEGASAS_INT_CMDS;
  867. max_cmd = instance->max_fw_cmds;
  868. fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
  869. fusion->request_alloc_sz =
  870. sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
  871. fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
  872. *(fusion->reply_q_depth);
  873. fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
  874. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
  875. (max_cmd + 1)); /* Extra 1 for SMID 0 */
  876. fusion->max_sge_in_main_msg =
  877. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
  878. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
  879. fusion->max_sge_in_chain =
  880. MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
  881. instance->max_num_sge = rounddown_pow_of_two(
  882. fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
  883. /* Used for pass thru MFI frame (DCMD) */
  884. fusion->chain_offset_mfi_pthru =
  885. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
  886. fusion->chain_offset_io_request =
  887. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
  888. sizeof(union MPI2_SGE_IO_UNION))/16;
  889. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  890. for (i = 0 ; i < count; i++)
  891. fusion->last_reply_idx[i] = 0;
  892. /*
  893. * Allocate memory for descriptors
  894. * Create a pool of commands
  895. */
  896. if (megasas_alloc_cmds(instance))
  897. goto fail_alloc_mfi_cmds;
  898. if (megasas_alloc_cmds_fusion(instance))
  899. goto fail_alloc_cmds;
  900. if (megasas_ioc_init_fusion(instance))
  901. goto fail_ioc_init;
  902. megasas_display_intel_branding(instance);
  903. if (megasas_get_ctrl_info(instance, instance->ctrl_info)) {
  904. dev_err(&instance->pdev->dev,
  905. "Could not get controller info. Fail from %s %d\n",
  906. __func__, __LINE__);
  907. goto fail_ioc_init;
  908. }
  909. instance->supportmax256vd =
  910. instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
  911. /* Below is additional check to address future FW enhancement */
  912. if (instance->ctrl_info->max_lds > 64)
  913. instance->supportmax256vd = 1;
  914. instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
  915. * MEGASAS_MAX_DEV_PER_CHANNEL;
  916. instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
  917. * MEGASAS_MAX_DEV_PER_CHANNEL;
  918. if (instance->supportmax256vd) {
  919. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
  920. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  921. } else {
  922. instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
  923. instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
  924. }
  925. dev_info(&instance->pdev->dev, "Firmware supports %d VDs %d PDs\n"
  926. "Driver supports %d VDs %d PDs\n",
  927. instance->fw_supported_vd_count,
  928. instance->fw_supported_pd_count,
  929. instance->drv_supported_vd_count,
  930. instance->drv_supported_pd_count);
  931. instance->flag_ieee = 1;
  932. fusion->fast_path_io = 0;
  933. fusion->old_map_sz =
  934. sizeof(struct MR_FW_RAID_MAP) + (sizeof(struct MR_LD_SPAN_MAP) *
  935. (instance->fw_supported_vd_count - 1));
  936. fusion->new_map_sz =
  937. sizeof(struct MR_FW_RAID_MAP_EXT);
  938. fusion->drv_map_sz =
  939. sizeof(struct MR_DRV_RAID_MAP) + (sizeof(struct MR_LD_SPAN_MAP) *
  940. (instance->drv_supported_vd_count - 1));
  941. fusion->drv_map_pages = get_order(fusion->drv_map_sz);
  942. for (i = 0; i < 2; i++) {
  943. fusion->ld_map[i] = NULL;
  944. fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
  945. fusion->drv_map_pages);
  946. if (!fusion->ld_drv_map[i]) {
  947. dev_err(&instance->pdev->dev, "Could not allocate "
  948. "memory for local map info for %d pages\n",
  949. fusion->drv_map_pages);
  950. if (i == 1)
  951. free_pages((ulong)fusion->ld_drv_map[0],
  952. fusion->drv_map_pages);
  953. goto fail_ioc_init;
  954. }
  955. }
  956. fusion->max_map_sz = max(fusion->old_map_sz, fusion->new_map_sz);
  957. if (instance->supportmax256vd)
  958. fusion->current_map_sz = fusion->new_map_sz;
  959. else
  960. fusion->current_map_sz = fusion->old_map_sz;
  961. for (i = 0; i < 2; i++) {
  962. fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
  963. fusion->max_map_sz,
  964. &fusion->ld_map_phys[i],
  965. GFP_KERNEL);
  966. if (!fusion->ld_map[i]) {
  967. printk(KERN_ERR "megasas: Could not allocate memory "
  968. "for map info\n");
  969. goto fail_map_info;
  970. }
  971. }
  972. if (!megasas_get_map_info(instance))
  973. megasas_sync_map_info(instance);
  974. return 0;
  975. fail_map_info:
  976. if (i == 1)
  977. dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
  978. fusion->ld_map[0], fusion->ld_map_phys[0]);
  979. fail_ioc_init:
  980. megasas_free_cmds_fusion(instance);
  981. fail_alloc_cmds:
  982. megasas_free_cmds(instance);
  983. fail_alloc_mfi_cmds:
  984. return 1;
  985. }
  986. /**
  987. * megasas_fire_cmd_fusion - Sends command to the FW
  988. * @frame_phys_addr : Physical address of cmd
  989. * @frame_count : Number of frames for the command
  990. * @regs : MFI register set
  991. */
  992. void
  993. megasas_fire_cmd_fusion(struct megasas_instance *instance,
  994. dma_addr_t req_desc_lo,
  995. u32 req_desc_hi,
  996. struct megasas_register_set __iomem *regs)
  997. {
  998. #if defined(writeq) && defined(CONFIG_64BIT)
  999. u64 req_data = (((u64)le32_to_cpu(req_desc_hi) << 32) |
  1000. le32_to_cpu(req_desc_lo));
  1001. writeq(req_data, &(regs)->inbound_low_queue_port);
  1002. #else
  1003. unsigned long flags;
  1004. spin_lock_irqsave(&instance->hba_lock, flags);
  1005. writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
  1006. writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
  1007. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1008. #endif
  1009. }
  1010. /**
  1011. * map_cmd_status - Maps FW cmd status to OS cmd status
  1012. * @cmd : Pointer to cmd
  1013. * @status : status of cmd returned by FW
  1014. * @ext_status : ext status of cmd returned by FW
  1015. */
  1016. void
  1017. map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
  1018. {
  1019. switch (status) {
  1020. case MFI_STAT_OK:
  1021. cmd->scmd->result = DID_OK << 16;
  1022. break;
  1023. case MFI_STAT_SCSI_IO_FAILED:
  1024. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1025. cmd->scmd->result = (DID_ERROR << 16) | ext_status;
  1026. break;
  1027. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1028. cmd->scmd->result = (DID_OK << 16) | ext_status;
  1029. if (ext_status == SAM_STAT_CHECK_CONDITION) {
  1030. memset(cmd->scmd->sense_buffer, 0,
  1031. SCSI_SENSE_BUFFERSIZE);
  1032. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1033. SCSI_SENSE_BUFFERSIZE);
  1034. cmd->scmd->result |= DRIVER_SENSE << 24;
  1035. }
  1036. break;
  1037. case MFI_STAT_LD_OFFLINE:
  1038. case MFI_STAT_DEVICE_NOT_FOUND:
  1039. cmd->scmd->result = DID_BAD_TARGET << 16;
  1040. break;
  1041. case MFI_STAT_CONFIG_SEQ_MISMATCH:
  1042. cmd->scmd->result = DID_IMM_RETRY << 16;
  1043. break;
  1044. default:
  1045. printk(KERN_DEBUG "megasas: FW status %#x\n", status);
  1046. cmd->scmd->result = DID_ERROR << 16;
  1047. break;
  1048. }
  1049. }
  1050. /**
  1051. * megasas_make_sgl_fusion - Prepares 32-bit SGL
  1052. * @instance: Adapter soft state
  1053. * @scp: SCSI command from the mid-layer
  1054. * @sgl_ptr: SGL to be filled in
  1055. * @cmd: cmd we are working on
  1056. *
  1057. * If successful, this function returns the number of SG elements.
  1058. */
  1059. static int
  1060. megasas_make_sgl_fusion(struct megasas_instance *instance,
  1061. struct scsi_cmnd *scp,
  1062. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
  1063. struct megasas_cmd_fusion *cmd)
  1064. {
  1065. int i, sg_processed, sge_count;
  1066. struct scatterlist *os_sgl;
  1067. struct fusion_context *fusion;
  1068. fusion = instance->ctrl_context;
  1069. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1070. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  1071. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
  1072. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  1073. sgl_ptr_end->Flags = 0;
  1074. }
  1075. sge_count = scsi_dma_map(scp);
  1076. BUG_ON(sge_count < 0);
  1077. if (sge_count > instance->max_num_sge || !sge_count)
  1078. return sge_count;
  1079. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1080. sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
  1081. sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
  1082. sgl_ptr->Flags = 0;
  1083. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1084. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  1085. if (i == sge_count - 1)
  1086. sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
  1087. }
  1088. sgl_ptr++;
  1089. sg_processed = i + 1;
  1090. if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
  1091. (sge_count > fusion->max_sge_in_main_msg)) {
  1092. struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
  1093. if ((instance->pdev->device ==
  1094. PCI_DEVICE_ID_LSI_INVADER) ||
  1095. (instance->pdev->device ==
  1096. PCI_DEVICE_ID_LSI_FURY)) {
  1097. if ((le16_to_cpu(cmd->io_request->IoFlags) &
  1098. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
  1099. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
  1100. cmd->io_request->ChainOffset =
  1101. fusion->
  1102. chain_offset_io_request;
  1103. else
  1104. cmd->io_request->ChainOffset = 0;
  1105. } else
  1106. cmd->io_request->ChainOffset =
  1107. fusion->chain_offset_io_request;
  1108. sg_chain = sgl_ptr;
  1109. /* Prepare chain element */
  1110. sg_chain->NextChainOffset = 0;
  1111. if ((instance->pdev->device ==
  1112. PCI_DEVICE_ID_LSI_INVADER) ||
  1113. (instance->pdev->device ==
  1114. PCI_DEVICE_ID_LSI_FURY))
  1115. sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
  1116. else
  1117. sg_chain->Flags =
  1118. (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1119. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
  1120. sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
  1121. sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
  1122. sgl_ptr =
  1123. (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
  1124. }
  1125. }
  1126. return sge_count;
  1127. }
  1128. /**
  1129. * megasas_set_pd_lba - Sets PD LBA
  1130. * @cdb: CDB
  1131. * @cdb_len: cdb length
  1132. * @start_blk: Start block of IO
  1133. *
  1134. * Used to set the PD LBA in CDB for FP IOs
  1135. */
  1136. void
  1137. megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
  1138. struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
  1139. struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
  1140. {
  1141. struct MR_LD_RAID *raid;
  1142. u32 ld;
  1143. u64 start_blk = io_info->pdBlock;
  1144. u8 *cdb = io_request->CDB.CDB32;
  1145. u32 num_blocks = io_info->numBlocks;
  1146. u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
  1147. /* Check if T10 PI (DIF) is enabled for this LD */
  1148. ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
  1149. raid = MR_LdRaidGet(ld, local_map_ptr);
  1150. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
  1151. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1152. cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
  1153. cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
  1154. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1155. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
  1156. else
  1157. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
  1158. cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
  1159. /* LBA */
  1160. cdb[12] = (u8)((start_blk >> 56) & 0xff);
  1161. cdb[13] = (u8)((start_blk >> 48) & 0xff);
  1162. cdb[14] = (u8)((start_blk >> 40) & 0xff);
  1163. cdb[15] = (u8)((start_blk >> 32) & 0xff);
  1164. cdb[16] = (u8)((start_blk >> 24) & 0xff);
  1165. cdb[17] = (u8)((start_blk >> 16) & 0xff);
  1166. cdb[18] = (u8)((start_blk >> 8) & 0xff);
  1167. cdb[19] = (u8)(start_blk & 0xff);
  1168. /* Logical block reference tag */
  1169. io_request->CDB.EEDP32.PrimaryReferenceTag =
  1170. cpu_to_be32(ref_tag);
  1171. io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
  1172. io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
  1173. /* Transfer length */
  1174. cdb[28] = (u8)((num_blocks >> 24) & 0xff);
  1175. cdb[29] = (u8)((num_blocks >> 16) & 0xff);
  1176. cdb[30] = (u8)((num_blocks >> 8) & 0xff);
  1177. cdb[31] = (u8)(num_blocks & 0xff);
  1178. /* set SCSI IO EEDPFlags */
  1179. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
  1180. io_request->EEDPFlags = cpu_to_le16(
  1181. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1182. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  1183. MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
  1184. MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
  1185. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
  1186. } else {
  1187. io_request->EEDPFlags = cpu_to_le16(
  1188. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1189. MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
  1190. }
  1191. io_request->Control |= cpu_to_le32((0x4 << 26));
  1192. io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
  1193. } else {
  1194. /* Some drives don't support 16/12 byte CDB's, convert to 10 */
  1195. if (((cdb_len == 12) || (cdb_len == 16)) &&
  1196. (start_blk <= 0xffffffff)) {
  1197. if (cdb_len == 16) {
  1198. opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
  1199. flagvals = cdb[1];
  1200. groupnum = cdb[14];
  1201. control = cdb[15];
  1202. } else {
  1203. opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
  1204. flagvals = cdb[1];
  1205. groupnum = cdb[10];
  1206. control = cdb[11];
  1207. }
  1208. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1209. cdb[0] = opcode;
  1210. cdb[1] = flagvals;
  1211. cdb[6] = groupnum;
  1212. cdb[9] = control;
  1213. /* Transfer length */
  1214. cdb[8] = (u8)(num_blocks & 0xff);
  1215. cdb[7] = (u8)((num_blocks >> 8) & 0xff);
  1216. io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
  1217. cdb_len = 10;
  1218. } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
  1219. /* Convert to 16 byte CDB for large LBA's */
  1220. switch (cdb_len) {
  1221. case 6:
  1222. opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
  1223. control = cdb[5];
  1224. break;
  1225. case 10:
  1226. opcode =
  1227. cdb[0] == READ_10 ? READ_16 : WRITE_16;
  1228. flagvals = cdb[1];
  1229. groupnum = cdb[6];
  1230. control = cdb[9];
  1231. break;
  1232. case 12:
  1233. opcode =
  1234. cdb[0] == READ_12 ? READ_16 : WRITE_16;
  1235. flagvals = cdb[1];
  1236. groupnum = cdb[10];
  1237. control = cdb[11];
  1238. break;
  1239. }
  1240. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1241. cdb[0] = opcode;
  1242. cdb[1] = flagvals;
  1243. cdb[14] = groupnum;
  1244. cdb[15] = control;
  1245. /* Transfer length */
  1246. cdb[13] = (u8)(num_blocks & 0xff);
  1247. cdb[12] = (u8)((num_blocks >> 8) & 0xff);
  1248. cdb[11] = (u8)((num_blocks >> 16) & 0xff);
  1249. cdb[10] = (u8)((num_blocks >> 24) & 0xff);
  1250. io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
  1251. cdb_len = 16;
  1252. }
  1253. /* Normal case, just load LBA here */
  1254. switch (cdb_len) {
  1255. case 6:
  1256. {
  1257. u8 val = cdb[1] & 0xE0;
  1258. cdb[3] = (u8)(start_blk & 0xff);
  1259. cdb[2] = (u8)((start_blk >> 8) & 0xff);
  1260. cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
  1261. break;
  1262. }
  1263. case 10:
  1264. cdb[5] = (u8)(start_blk & 0xff);
  1265. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1266. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1267. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1268. break;
  1269. case 12:
  1270. cdb[5] = (u8)(start_blk & 0xff);
  1271. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1272. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1273. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1274. break;
  1275. case 16:
  1276. cdb[9] = (u8)(start_blk & 0xff);
  1277. cdb[8] = (u8)((start_blk >> 8) & 0xff);
  1278. cdb[7] = (u8)((start_blk >> 16) & 0xff);
  1279. cdb[6] = (u8)((start_blk >> 24) & 0xff);
  1280. cdb[5] = (u8)((start_blk >> 32) & 0xff);
  1281. cdb[4] = (u8)((start_blk >> 40) & 0xff);
  1282. cdb[3] = (u8)((start_blk >> 48) & 0xff);
  1283. cdb[2] = (u8)((start_blk >> 56) & 0xff);
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. /**
  1289. * megasas_build_ldio_fusion - Prepares IOs to devices
  1290. * @instance: Adapter soft state
  1291. * @scp: SCSI command
  1292. * @cmd: Command to be prepared
  1293. *
  1294. * Prepares the io_request and chain elements (sg_frame) for IO
  1295. * The IO can be for PD (Fast Path) or LD
  1296. */
  1297. void
  1298. megasas_build_ldio_fusion(struct megasas_instance *instance,
  1299. struct scsi_cmnd *scp,
  1300. struct megasas_cmd_fusion *cmd)
  1301. {
  1302. u8 fp_possible;
  1303. u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
  1304. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1305. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1306. struct IO_REQUEST_INFO io_info;
  1307. struct fusion_context *fusion;
  1308. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1309. u8 *raidLUN;
  1310. device_id = MEGASAS_DEV_INDEX(instance, scp);
  1311. fusion = instance->ctrl_context;
  1312. io_request = cmd->io_request;
  1313. io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
  1314. io_request->RaidContext.status = 0;
  1315. io_request->RaidContext.exStatus = 0;
  1316. req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
  1317. start_lba_lo = 0;
  1318. start_lba_hi = 0;
  1319. fp_possible = 0;
  1320. /*
  1321. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1322. */
  1323. if (scp->cmd_len == 6) {
  1324. datalength = (u32) scp->cmnd[4];
  1325. start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  1326. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  1327. start_lba_lo &= 0x1FFFFF;
  1328. }
  1329. /*
  1330. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1331. */
  1332. else if (scp->cmd_len == 10) {
  1333. datalength = (u32) scp->cmnd[8] |
  1334. ((u32) scp->cmnd[7] << 8);
  1335. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1336. ((u32) scp->cmnd[3] << 16) |
  1337. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1338. }
  1339. /*
  1340. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1341. */
  1342. else if (scp->cmd_len == 12) {
  1343. datalength = ((u32) scp->cmnd[6] << 24) |
  1344. ((u32) scp->cmnd[7] << 16) |
  1345. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1346. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1347. ((u32) scp->cmnd[3] << 16) |
  1348. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1349. }
  1350. /*
  1351. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1352. */
  1353. else if (scp->cmd_len == 16) {
  1354. datalength = ((u32) scp->cmnd[10] << 24) |
  1355. ((u32) scp->cmnd[11] << 16) |
  1356. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  1357. start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  1358. ((u32) scp->cmnd[7] << 16) |
  1359. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1360. start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  1361. ((u32) scp->cmnd[3] << 16) |
  1362. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1363. }
  1364. memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
  1365. io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
  1366. io_info.numBlocks = datalength;
  1367. io_info.ldTgtId = device_id;
  1368. io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
  1369. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1370. io_info.isRead = 1;
  1371. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1372. if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
  1373. instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
  1374. io_request->RaidContext.regLockFlags = 0;
  1375. fp_possible = 0;
  1376. } else {
  1377. if (MR_BuildRaidContext(instance, &io_info,
  1378. &io_request->RaidContext,
  1379. local_map_ptr, &raidLUN))
  1380. fp_possible = io_info.fpOkForIo;
  1381. }
  1382. /* Use smp_processor_id() for now until cmd->request->cpu is CPU
  1383. id by default, not CPU group id, otherwise all MSI-X queues won't
  1384. be utilized */
  1385. cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
  1386. smp_processor_id() % instance->msix_vectors : 0;
  1387. if (fp_possible) {
  1388. megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
  1389. local_map_ptr, start_lba_lo);
  1390. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1391. cmd->request_desc->SCSIIO.RequestFlags =
  1392. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
  1393. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1394. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1395. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  1396. if (io_request->RaidContext.regLockFlags ==
  1397. REGION_TYPE_UNUSED)
  1398. cmd->request_desc->SCSIIO.RequestFlags =
  1399. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1400. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1401. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1402. io_request->RaidContext.nseg = 0x1;
  1403. io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  1404. io_request->RaidContext.regLockFlags |=
  1405. (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
  1406. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1407. }
  1408. if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
  1409. (io_info.isRead)) {
  1410. io_info.devHandle =
  1411. get_updated_dev_handle(instance,
  1412. &fusion->load_balance_info[device_id],
  1413. &io_info);
  1414. scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
  1415. cmd->pd_r1_lb = io_info.pd_after_lb;
  1416. } else
  1417. scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
  1418. cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
  1419. io_request->DevHandle = io_info.devHandle;
  1420. /* populate the LUN field */
  1421. memcpy(io_request->LUN, raidLUN, 8);
  1422. } else {
  1423. io_request->RaidContext.timeoutValue =
  1424. cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
  1425. cmd->request_desc->SCSIIO.RequestFlags =
  1426. (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
  1427. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1428. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1429. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  1430. if (io_request->RaidContext.regLockFlags ==
  1431. REGION_TYPE_UNUSED)
  1432. cmd->request_desc->SCSIIO.RequestFlags =
  1433. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1434. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1435. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1436. io_request->RaidContext.regLockFlags |=
  1437. (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
  1438. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1439. io_request->RaidContext.nseg = 0x1;
  1440. }
  1441. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1442. io_request->DevHandle = cpu_to_le16(device_id);
  1443. } /* Not FP */
  1444. }
  1445. /**
  1446. * megasas_build_dcdb_fusion - Prepares IOs to devices
  1447. * @instance: Adapter soft state
  1448. * @scp: SCSI command
  1449. * @cmd: Command to be prepared
  1450. *
  1451. * Prepares the io_request frame for non-io cmds
  1452. */
  1453. static void
  1454. megasas_build_dcdb_fusion(struct megasas_instance *instance,
  1455. struct scsi_cmnd *scmd,
  1456. struct megasas_cmd_fusion *cmd)
  1457. {
  1458. u32 device_id;
  1459. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1460. u16 pd_index = 0;
  1461. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1462. struct fusion_context *fusion = instance->ctrl_context;
  1463. u8 span, physArm;
  1464. u16 devHandle;
  1465. u32 ld, arRef, pd;
  1466. struct MR_LD_RAID *raid;
  1467. struct RAID_CONTEXT *pRAID_Context;
  1468. io_request = cmd->io_request;
  1469. device_id = MEGASAS_DEV_INDEX(instance, scmd);
  1470. pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
  1471. +scmd->device->id;
  1472. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1473. io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  1474. /* Check if this is a system PD I/O */
  1475. if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
  1476. instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
  1477. io_request->Function = 0;
  1478. if (fusion->fast_path_io)
  1479. io_request->DevHandle =
  1480. local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
  1481. io_request->RaidContext.timeoutValue =
  1482. local_map_ptr->raidMap.fpPdIoTimeoutSec;
  1483. io_request->RaidContext.regLockFlags = 0;
  1484. io_request->RaidContext.regLockRowLBA = 0;
  1485. io_request->RaidContext.regLockLength = 0;
  1486. io_request->RaidContext.RAIDFlags =
  1487. MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
  1488. MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
  1489. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1490. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
  1491. io_request->IoFlags |= cpu_to_le16(
  1492. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  1493. cmd->request_desc->SCSIIO.RequestFlags =
  1494. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
  1495. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1496. cmd->request_desc->SCSIIO.DevHandle =
  1497. local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
  1498. cmd->request_desc->SCSIIO.MSIxIndex =
  1499. instance->msix_vectors ? smp_processor_id() % instance->msix_vectors : 0;
  1500. /*
  1501. * If the command is for the tape device, set the
  1502. * FP timeout to the os layer timeout value.
  1503. */
  1504. if (scmd->device->type == TYPE_TAPE) {
  1505. if ((scmd->request->timeout / HZ) > 0xFFFF)
  1506. io_request->RaidContext.timeoutValue =
  1507. 0xFFFF;
  1508. else
  1509. io_request->RaidContext.timeoutValue =
  1510. scmd->request->timeout / HZ;
  1511. }
  1512. } else {
  1513. if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
  1514. goto NonFastPath;
  1515. /*
  1516. * For older firmware, Driver should not access ldTgtIdToLd
  1517. * beyond index 127 and for Extended VD firmware, ldTgtIdToLd
  1518. * should not go beyond 255.
  1519. */
  1520. if ((!fusion->fast_path_io) ||
  1521. (device_id >= instance->fw_supported_vd_count))
  1522. goto NonFastPath;
  1523. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  1524. if (ld >= instance->fw_supported_vd_count)
  1525. goto NonFastPath;
  1526. raid = MR_LdRaidGet(ld, local_map_ptr);
  1527. /* check if this LD is FP capable */
  1528. if (!(raid->capability.fpNonRWCapable))
  1529. /* not FP capable, send as non-FP */
  1530. goto NonFastPath;
  1531. /* get RAID_Context pointer */
  1532. pRAID_Context = &io_request->RaidContext;
  1533. /* set RAID context values */
  1534. pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
  1535. pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
  1536. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1537. pRAID_Context->regLockRowLBA = 0;
  1538. pRAID_Context->regLockLength = 0;
  1539. pRAID_Context->configSeqNum = raid->seqNum;
  1540. /* get the DevHandle for the PD (since this is
  1541. fpNonRWCapable, this is a single disk RAID0) */
  1542. span = physArm = 0;
  1543. arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
  1544. pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
  1545. devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
  1546. /* build request descriptor */
  1547. cmd->request_desc->SCSIIO.RequestFlags =
  1548. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
  1549. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1550. cmd->request_desc->SCSIIO.DevHandle = devHandle;
  1551. /* populate the LUN field */
  1552. memcpy(io_request->LUN, raid->LUN, 8);
  1553. /* build the raidScsiIO structure */
  1554. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1555. io_request->DevHandle = devHandle;
  1556. return;
  1557. NonFastPath:
  1558. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1559. io_request->DevHandle = cpu_to_le16(device_id);
  1560. cmd->request_desc->SCSIIO.RequestFlags =
  1561. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1562. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1563. }
  1564. io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
  1565. int_to_scsilun(scmd->device->lun, (struct scsi_lun *)io_request->LUN);
  1566. }
  1567. /**
  1568. * megasas_build_io_fusion - Prepares IOs to devices
  1569. * @instance: Adapter soft state
  1570. * @scp: SCSI command
  1571. * @cmd: Command to be prepared
  1572. *
  1573. * Invokes helper functions to prepare request frames
  1574. * and sets flags appropriate for IO/Non-IO cmd
  1575. */
  1576. int
  1577. megasas_build_io_fusion(struct megasas_instance *instance,
  1578. struct scsi_cmnd *scp,
  1579. struct megasas_cmd_fusion *cmd)
  1580. {
  1581. u32 device_id, sge_count;
  1582. struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
  1583. device_id = MEGASAS_DEV_INDEX(instance, scp);
  1584. /* Zero out some fields so they don't get reused */
  1585. memset(io_request->LUN, 0x0, 8);
  1586. io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
  1587. io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
  1588. io_request->EEDPFlags = 0;
  1589. io_request->Control = 0;
  1590. io_request->EEDPBlockSize = 0;
  1591. io_request->ChainOffset = 0;
  1592. io_request->RaidContext.RAIDFlags = 0;
  1593. io_request->RaidContext.Type = 0;
  1594. io_request->RaidContext.nseg = 0;
  1595. memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
  1596. /*
  1597. * Just the CDB length,rest of the Flags are zero
  1598. * This will be modified for FP in build_ldio_fusion
  1599. */
  1600. io_request->IoFlags = cpu_to_le16(scp->cmd_len);
  1601. if (megasas_is_ldio(scp))
  1602. megasas_build_ldio_fusion(instance, scp, cmd);
  1603. else
  1604. megasas_build_dcdb_fusion(instance, scp, cmd);
  1605. /*
  1606. * Construct SGL
  1607. */
  1608. sge_count =
  1609. megasas_make_sgl_fusion(instance, scp,
  1610. (struct MPI25_IEEE_SGE_CHAIN64 *)
  1611. &io_request->SGL, cmd);
  1612. if (sge_count > instance->max_num_sge) {
  1613. printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
  1614. "max (0x%x) allowed\n", sge_count,
  1615. instance->max_num_sge);
  1616. return 1;
  1617. }
  1618. io_request->RaidContext.numSGE = sge_count;
  1619. io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
  1620. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1621. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
  1622. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1623. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
  1624. io_request->SGLOffset0 =
  1625. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
  1626. io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
  1627. io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  1628. cmd->scmd = scp;
  1629. scp->SCp.ptr = (char *)cmd;
  1630. return 0;
  1631. }
  1632. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  1633. megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
  1634. {
  1635. u8 *p;
  1636. struct fusion_context *fusion;
  1637. if (index >= instance->max_fw_cmds) {
  1638. printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
  1639. "descriptor for scsi%d\n", index,
  1640. instance->host->host_no);
  1641. return NULL;
  1642. }
  1643. fusion = instance->ctrl_context;
  1644. p = fusion->req_frames_desc
  1645. +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
  1646. return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
  1647. }
  1648. /**
  1649. * megasas_build_and_issue_cmd_fusion -Main routine for building and
  1650. * issuing non IOCTL cmd
  1651. * @instance: Adapter soft state
  1652. * @scmd: pointer to scsi cmd from OS
  1653. */
  1654. static u32
  1655. megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
  1656. struct scsi_cmnd *scmd)
  1657. {
  1658. struct megasas_cmd_fusion *cmd;
  1659. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1660. u32 index;
  1661. struct fusion_context *fusion;
  1662. fusion = instance->ctrl_context;
  1663. cmd = megasas_get_cmd_fusion(instance);
  1664. if (!cmd)
  1665. return SCSI_MLQUEUE_HOST_BUSY;
  1666. index = cmd->index;
  1667. req_desc = megasas_get_request_descriptor(instance, index-1);
  1668. if (!req_desc)
  1669. return 1;
  1670. req_desc->Words = 0;
  1671. cmd->request_desc = req_desc;
  1672. if (megasas_build_io_fusion(instance, scmd, cmd)) {
  1673. megasas_return_cmd_fusion(instance, cmd);
  1674. printk(KERN_ERR "megasas: Error building command.\n");
  1675. cmd->request_desc = NULL;
  1676. return 1;
  1677. }
  1678. req_desc = cmd->request_desc;
  1679. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  1680. if (cmd->io_request->ChainOffset != 0 &&
  1681. cmd->io_request->ChainOffset != 0xF)
  1682. printk(KERN_ERR "megasas: The chain offset value is not "
  1683. "correct : %x\n", cmd->io_request->ChainOffset);
  1684. /*
  1685. * Issue the command to the FW
  1686. */
  1687. atomic_inc(&instance->fw_outstanding);
  1688. instance->instancet->fire_cmd(instance,
  1689. req_desc->u.low, req_desc->u.high,
  1690. instance->reg_set);
  1691. return 0;
  1692. }
  1693. /**
  1694. * complete_cmd_fusion - Completes command
  1695. * @instance: Adapter soft state
  1696. * Completes all commands that is in reply descriptor queue
  1697. */
  1698. int
  1699. complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
  1700. {
  1701. union MPI2_REPLY_DESCRIPTORS_UNION *desc;
  1702. struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
  1703. struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
  1704. struct fusion_context *fusion;
  1705. struct megasas_cmd *cmd_mfi;
  1706. struct megasas_cmd_fusion *cmd_fusion;
  1707. u16 smid, num_completed;
  1708. u8 reply_descript_type;
  1709. u32 status, extStatus, device_id;
  1710. union desc_value d_val;
  1711. struct LD_LOAD_BALANCE_INFO *lbinfo;
  1712. int threshold_reply_count = 0;
  1713. fusion = instance->ctrl_context;
  1714. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  1715. return IRQ_HANDLED;
  1716. desc = fusion->reply_frames_desc;
  1717. desc += ((MSIxIndex * fusion->reply_alloc_sz)/
  1718. sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
  1719. fusion->last_reply_idx[MSIxIndex];
  1720. reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  1721. d_val.word = desc->Words;
  1722. reply_descript_type = reply_desc->ReplyFlags &
  1723. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1724. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1725. return IRQ_NONE;
  1726. num_completed = 0;
  1727. while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
  1728. smid = le16_to_cpu(reply_desc->SMID);
  1729. cmd_fusion = fusion->cmd_list[smid - 1];
  1730. scsi_io_req =
  1731. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  1732. cmd_fusion->io_request;
  1733. if (cmd_fusion->scmd)
  1734. cmd_fusion->scmd->SCp.ptr = NULL;
  1735. status = scsi_io_req->RaidContext.status;
  1736. extStatus = scsi_io_req->RaidContext.exStatus;
  1737. switch (scsi_io_req->Function) {
  1738. case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
  1739. /* Update load balancing info */
  1740. device_id = MEGASAS_DEV_INDEX(instance,
  1741. cmd_fusion->scmd);
  1742. lbinfo = &fusion->load_balance_info[device_id];
  1743. if (cmd_fusion->scmd->SCp.Status &
  1744. MEGASAS_LOAD_BALANCE_FLAG) {
  1745. atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
  1746. cmd_fusion->scmd->SCp.Status &=
  1747. ~MEGASAS_LOAD_BALANCE_FLAG;
  1748. }
  1749. if (reply_descript_type ==
  1750. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
  1751. if (megasas_dbg_lvl == 5)
  1752. printk(KERN_ERR "\nmegasas: FAST Path "
  1753. "IO Success\n");
  1754. }
  1755. /* Fall thru and complete IO */
  1756. case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
  1757. /* Map the FW Cmd Status */
  1758. map_cmd_status(cmd_fusion, status, extStatus);
  1759. scsi_dma_unmap(cmd_fusion->scmd);
  1760. cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
  1761. scsi_io_req->RaidContext.status = 0;
  1762. scsi_io_req->RaidContext.exStatus = 0;
  1763. megasas_return_cmd_fusion(instance, cmd_fusion);
  1764. atomic_dec(&instance->fw_outstanding);
  1765. break;
  1766. case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
  1767. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  1768. if (!cmd_mfi->mpt_pthr_cmd_blocked) {
  1769. if (megasas_dbg_lvl == 5)
  1770. dev_info(&instance->pdev->dev,
  1771. "freeing mfi/mpt pass-through "
  1772. "from %s %d\n",
  1773. __func__, __LINE__);
  1774. megasas_return_mfi_mpt_pthr(instance, cmd_mfi,
  1775. cmd_fusion);
  1776. }
  1777. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  1778. cmd_fusion->flags = 0;
  1779. break;
  1780. }
  1781. fusion->last_reply_idx[MSIxIndex]++;
  1782. if (fusion->last_reply_idx[MSIxIndex] >=
  1783. fusion->reply_q_depth)
  1784. fusion->last_reply_idx[MSIxIndex] = 0;
  1785. desc->Words = ULLONG_MAX;
  1786. num_completed++;
  1787. threshold_reply_count++;
  1788. /* Get the next reply descriptor */
  1789. if (!fusion->last_reply_idx[MSIxIndex])
  1790. desc = fusion->reply_frames_desc +
  1791. ((MSIxIndex * fusion->reply_alloc_sz)/
  1792. sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
  1793. else
  1794. desc++;
  1795. reply_desc =
  1796. (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  1797. d_val.word = desc->Words;
  1798. reply_descript_type = reply_desc->ReplyFlags &
  1799. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1800. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1801. break;
  1802. /*
  1803. * Write to reply post host index register after completing threshold
  1804. * number of reply counts and still there are more replies in reply queue
  1805. * pending to be completed
  1806. */
  1807. if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
  1808. if ((instance->pdev->device ==
  1809. PCI_DEVICE_ID_LSI_INVADER) ||
  1810. (instance->pdev->device ==
  1811. PCI_DEVICE_ID_LSI_FURY))
  1812. writel(((MSIxIndex & 0x7) << 24) |
  1813. fusion->last_reply_idx[MSIxIndex],
  1814. instance->reply_post_host_index_addr[MSIxIndex/8]);
  1815. else
  1816. writel((MSIxIndex << 24) |
  1817. fusion->last_reply_idx[MSIxIndex],
  1818. instance->reply_post_host_index_addr[0]);
  1819. threshold_reply_count = 0;
  1820. }
  1821. }
  1822. if (!num_completed)
  1823. return IRQ_NONE;
  1824. wmb();
  1825. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1826. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
  1827. writel(((MSIxIndex & 0x7) << 24) |
  1828. fusion->last_reply_idx[MSIxIndex],
  1829. instance->reply_post_host_index_addr[MSIxIndex/8]);
  1830. else
  1831. writel((MSIxIndex << 24) |
  1832. fusion->last_reply_idx[MSIxIndex],
  1833. instance->reply_post_host_index_addr[0]);
  1834. megasas_check_and_restore_queue_depth(instance);
  1835. return IRQ_HANDLED;
  1836. }
  1837. /**
  1838. * megasas_complete_cmd_dpc_fusion - Completes command
  1839. * @instance: Adapter soft state
  1840. *
  1841. * Tasklet to complete cmds
  1842. */
  1843. void
  1844. megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
  1845. {
  1846. struct megasas_instance *instance =
  1847. (struct megasas_instance *)instance_addr;
  1848. unsigned long flags;
  1849. u32 count, MSIxIndex;
  1850. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  1851. /* If we have already declared adapter dead, donot complete cmds */
  1852. spin_lock_irqsave(&instance->hba_lock, flags);
  1853. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1854. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1855. return;
  1856. }
  1857. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1858. for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
  1859. complete_cmd_fusion(instance, MSIxIndex);
  1860. }
  1861. /**
  1862. * megasas_isr_fusion - isr entry point
  1863. */
  1864. irqreturn_t megasas_isr_fusion(int irq, void *devp)
  1865. {
  1866. struct megasas_irq_context *irq_context = devp;
  1867. struct megasas_instance *instance = irq_context->instance;
  1868. u32 mfiStatus, fw_state, dma_state;
  1869. if (instance->mask_interrupts)
  1870. return IRQ_NONE;
  1871. if (!instance->msix_vectors) {
  1872. mfiStatus = instance->instancet->clear_intr(instance->reg_set);
  1873. if (!mfiStatus)
  1874. return IRQ_NONE;
  1875. }
  1876. /* If we are resetting, bail */
  1877. if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
  1878. instance->instancet->clear_intr(instance->reg_set);
  1879. return IRQ_HANDLED;
  1880. }
  1881. if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
  1882. instance->instancet->clear_intr(instance->reg_set);
  1883. /* If we didn't complete any commands, check for FW fault */
  1884. fw_state = instance->instancet->read_fw_status_reg(
  1885. instance->reg_set) & MFI_STATE_MASK;
  1886. dma_state = instance->instancet->read_fw_status_reg
  1887. (instance->reg_set) & MFI_STATE_DMADONE;
  1888. if (instance->crash_dump_drv_support &&
  1889. instance->crash_dump_app_support) {
  1890. /* Start collecting crash, if DMA bit is done */
  1891. if ((fw_state == MFI_STATE_FAULT) && dma_state)
  1892. schedule_work(&instance->crash_init);
  1893. else if (fw_state == MFI_STATE_FAULT)
  1894. schedule_work(&instance->work_init);
  1895. } else if (fw_state == MFI_STATE_FAULT) {
  1896. printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
  1897. "for scsi%d\n", instance->host->host_no);
  1898. schedule_work(&instance->work_init);
  1899. }
  1900. }
  1901. return IRQ_HANDLED;
  1902. }
  1903. /**
  1904. * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
  1905. * @instance: Adapter soft state
  1906. * mfi_cmd: megasas_cmd pointer
  1907. *
  1908. */
  1909. u8
  1910. build_mpt_mfi_pass_thru(struct megasas_instance *instance,
  1911. struct megasas_cmd *mfi_cmd)
  1912. {
  1913. struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
  1914. struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
  1915. struct megasas_cmd_fusion *cmd;
  1916. struct fusion_context *fusion;
  1917. struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
  1918. u32 opcode;
  1919. cmd = megasas_get_cmd_fusion(instance);
  1920. if (!cmd)
  1921. return 1;
  1922. /* Save the smid. To be used for returning the cmd */
  1923. mfi_cmd->context.smid = cmd->index;
  1924. cmd->sync_cmd_idx = mfi_cmd->index;
  1925. /* Set this only for Blocked commands */
  1926. opcode = le32_to_cpu(mfi_cmd->frame->dcmd.opcode);
  1927. if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
  1928. && (mfi_cmd->frame->dcmd.mbox.b[1] == 1))
  1929. mfi_cmd->is_wait_event = 1;
  1930. if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1931. mfi_cmd->is_wait_event = 1;
  1932. if (mfi_cmd->is_wait_event)
  1933. mfi_cmd->mpt_pthr_cmd_blocked = cmd;
  1934. /*
  1935. * For cmds where the flag is set, store the flag and check
  1936. * on completion. For cmds with this flag, don't call
  1937. * megasas_complete_cmd
  1938. */
  1939. if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
  1940. cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  1941. fusion = instance->ctrl_context;
  1942. io_req = cmd->io_request;
  1943. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
  1944. (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
  1945. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
  1946. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
  1947. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  1948. sgl_ptr_end->Flags = 0;
  1949. }
  1950. mpi25_ieee_chain =
  1951. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
  1952. io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
  1953. io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
  1954. SGL) / 4;
  1955. io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
  1956. mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
  1957. mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1958. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
  1959. mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
  1960. return 0;
  1961. }
  1962. /**
  1963. * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
  1964. * @instance: Adapter soft state
  1965. * @cmd: mfi cmd to build
  1966. *
  1967. */
  1968. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  1969. build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1970. {
  1971. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1972. u16 index;
  1973. if (build_mpt_mfi_pass_thru(instance, cmd)) {
  1974. printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
  1975. return NULL;
  1976. }
  1977. index = cmd->context.smid;
  1978. req_desc = megasas_get_request_descriptor(instance, index - 1);
  1979. if (!req_desc)
  1980. return NULL;
  1981. req_desc->Words = 0;
  1982. req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1983. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1984. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  1985. return req_desc;
  1986. }
  1987. /**
  1988. * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
  1989. * @instance: Adapter soft state
  1990. * @cmd: mfi cmd pointer
  1991. *
  1992. */
  1993. void
  1994. megasas_issue_dcmd_fusion(struct megasas_instance *instance,
  1995. struct megasas_cmd *cmd)
  1996. {
  1997. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1998. req_desc = build_mpt_cmd(instance, cmd);
  1999. if (!req_desc) {
  2000. printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
  2001. return;
  2002. }
  2003. atomic_set(&cmd->mfi_mpt_pthr, MFI_MPT_ATTACHED);
  2004. instance->instancet->fire_cmd(instance, req_desc->u.low,
  2005. req_desc->u.high, instance->reg_set);
  2006. }
  2007. /**
  2008. * megasas_release_fusion - Reverses the FW initialization
  2009. * @intance: Adapter soft state
  2010. */
  2011. void
  2012. megasas_release_fusion(struct megasas_instance *instance)
  2013. {
  2014. megasas_free_cmds(instance);
  2015. megasas_free_cmds_fusion(instance);
  2016. iounmap(instance->reg_set);
  2017. pci_release_selected_regions(instance->pdev, instance->bar);
  2018. }
  2019. /**
  2020. * megasas_read_fw_status_reg_fusion - returns the current FW status value
  2021. * @regs: MFI register set
  2022. */
  2023. static u32
  2024. megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
  2025. {
  2026. return readl(&(regs)->outbound_scratch_pad);
  2027. }
  2028. /**
  2029. * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  2030. * @instance: Controller's soft instance
  2031. * return: Number of allocated host crash buffers
  2032. */
  2033. static void
  2034. megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
  2035. {
  2036. unsigned int i;
  2037. instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
  2038. for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
  2039. instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
  2040. instance->crash_buf_pages);
  2041. if (!instance->crash_buf[i]) {
  2042. dev_info(&instance->pdev->dev, "Firmware crash dump "
  2043. "memory allocation failed at index %d\n", i);
  2044. break;
  2045. }
  2046. }
  2047. instance->drv_buf_alloc = i;
  2048. }
  2049. /**
  2050. * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  2051. * @instance: Controller's soft instance
  2052. */
  2053. void
  2054. megasas_free_host_crash_buffer(struct megasas_instance *instance)
  2055. {
  2056. unsigned int i
  2057. ;
  2058. for (i = 0; i < instance->drv_buf_alloc; i++) {
  2059. if (instance->crash_buf[i])
  2060. free_pages((ulong)instance->crash_buf[i],
  2061. instance->crash_buf_pages);
  2062. }
  2063. instance->drv_buf_index = 0;
  2064. instance->drv_buf_alloc = 0;
  2065. instance->fw_crash_state = UNAVAILABLE;
  2066. instance->fw_crash_buffer_size = 0;
  2067. }
  2068. /**
  2069. * megasas_adp_reset_fusion - For controller reset
  2070. * @regs: MFI register set
  2071. */
  2072. static int
  2073. megasas_adp_reset_fusion(struct megasas_instance *instance,
  2074. struct megasas_register_set __iomem *regs)
  2075. {
  2076. return 0;
  2077. }
  2078. /**
  2079. * megasas_check_reset_fusion - For controller reset check
  2080. * @regs: MFI register set
  2081. */
  2082. static int
  2083. megasas_check_reset_fusion(struct megasas_instance *instance,
  2084. struct megasas_register_set __iomem *regs)
  2085. {
  2086. return 0;
  2087. }
  2088. /* This function waits for outstanding commands on fusion to complete */
  2089. int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
  2090. int iotimeout, int *convert)
  2091. {
  2092. int i, outstanding, retval = 0, hb_seconds_missed = 0;
  2093. u32 fw_state;
  2094. for (i = 0; i < resetwaittime; i++) {
  2095. /* Check if firmware is in fault state */
  2096. fw_state = instance->instancet->read_fw_status_reg(
  2097. instance->reg_set) & MFI_STATE_MASK;
  2098. if (fw_state == MFI_STATE_FAULT) {
  2099. printk(KERN_WARNING "megasas: Found FW in FAULT state,"
  2100. " will reset adapter scsi%d.\n",
  2101. instance->host->host_no);
  2102. retval = 1;
  2103. goto out;
  2104. }
  2105. /* If SR-IOV VF mode & heartbeat timeout, don't wait */
  2106. if (instance->requestorId && !iotimeout) {
  2107. retval = 1;
  2108. goto out;
  2109. }
  2110. /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
  2111. if (instance->requestorId && iotimeout) {
  2112. if (instance->hb_host_mem->HB.fwCounter !=
  2113. instance->hb_host_mem->HB.driverCounter) {
  2114. instance->hb_host_mem->HB.driverCounter =
  2115. instance->hb_host_mem->HB.fwCounter;
  2116. hb_seconds_missed = 0;
  2117. } else {
  2118. hb_seconds_missed++;
  2119. if (hb_seconds_missed ==
  2120. (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
  2121. printk(KERN_WARNING "megasas: SR-IOV:"
  2122. " Heartbeat never completed "
  2123. " while polling during I/O "
  2124. " timeout handling for "
  2125. "scsi%d.\n",
  2126. instance->host->host_no);
  2127. *convert = 1;
  2128. retval = 1;
  2129. goto out;
  2130. }
  2131. }
  2132. }
  2133. outstanding = atomic_read(&instance->fw_outstanding);
  2134. if (!outstanding)
  2135. goto out;
  2136. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  2137. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  2138. "commands to complete for scsi%d\n", i,
  2139. outstanding, instance->host->host_no);
  2140. megasas_complete_cmd_dpc_fusion(
  2141. (unsigned long)instance);
  2142. }
  2143. msleep(1000);
  2144. }
  2145. if (atomic_read(&instance->fw_outstanding)) {
  2146. printk("megaraid_sas: pending commands remain after waiting, "
  2147. "will reset adapter scsi%d.\n",
  2148. instance->host->host_no);
  2149. retval = 1;
  2150. }
  2151. out:
  2152. return retval;
  2153. }
  2154. void megasas_reset_reply_desc(struct megasas_instance *instance)
  2155. {
  2156. int i, count;
  2157. struct fusion_context *fusion;
  2158. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  2159. fusion = instance->ctrl_context;
  2160. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  2161. for (i = 0 ; i < count ; i++)
  2162. fusion->last_reply_idx[i] = 0;
  2163. reply_desc = fusion->reply_frames_desc;
  2164. for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
  2165. reply_desc->Words = ULLONG_MAX;
  2166. }
  2167. /* Check for a second path that is currently UP */
  2168. int megasas_check_mpio_paths(struct megasas_instance *instance,
  2169. struct scsi_cmnd *scmd)
  2170. {
  2171. int i, j, retval = (DID_RESET << 16);
  2172. if (instance->mpio && instance->requestorId) {
  2173. for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
  2174. for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
  2175. if (megasas_mgmt_info.instance[i] &&
  2176. (megasas_mgmt_info.instance[i] != instance) &&
  2177. megasas_mgmt_info.instance[i]->mpio &&
  2178. megasas_mgmt_info.instance[i]->requestorId
  2179. &&
  2180. (megasas_mgmt_info.instance[i]->ld_ids[j]
  2181. == scmd->device->id)) {
  2182. retval = (DID_NO_CONNECT << 16);
  2183. goto out;
  2184. }
  2185. }
  2186. out:
  2187. return retval;
  2188. }
  2189. /* Core fusion reset function */
  2190. int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
  2191. {
  2192. int retval = SUCCESS, i, j, retry = 0, convert = 0;
  2193. struct megasas_instance *instance;
  2194. struct megasas_cmd_fusion *cmd_fusion;
  2195. struct fusion_context *fusion;
  2196. struct megasas_cmd *cmd_mfi;
  2197. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2198. u32 host_diag, abs_state, status_reg, reset_adapter;
  2199. u32 io_timeout_in_crash_mode = 0;
  2200. instance = (struct megasas_instance *)shost->hostdata;
  2201. fusion = instance->ctrl_context;
  2202. mutex_lock(&instance->reset_mutex);
  2203. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  2204. printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
  2205. "returning FAILED for scsi%d.\n",
  2206. instance->host->host_no);
  2207. mutex_unlock(&instance->reset_mutex);
  2208. return FAILED;
  2209. }
  2210. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  2211. abs_state = status_reg & MFI_STATE_MASK;
  2212. /* IO timeout detected, forcibly put FW in FAULT state */
  2213. if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
  2214. instance->crash_dump_app_support && iotimeout) {
  2215. dev_info(&instance->pdev->dev, "IO timeout is detected, "
  2216. "forcibly FAULT Firmware\n");
  2217. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2218. status_reg = readl(&instance->reg_set->doorbell);
  2219. writel(status_reg | MFI_STATE_FORCE_OCR,
  2220. &instance->reg_set->doorbell);
  2221. readl(&instance->reg_set->doorbell);
  2222. mutex_unlock(&instance->reset_mutex);
  2223. do {
  2224. ssleep(3);
  2225. io_timeout_in_crash_mode++;
  2226. dev_dbg(&instance->pdev->dev, "waiting for [%d] "
  2227. "seconds for crash dump collection and OCR "
  2228. "to be done\n", (io_timeout_in_crash_mode * 3));
  2229. } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
  2230. (io_timeout_in_crash_mode < 80));
  2231. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  2232. dev_info(&instance->pdev->dev, "OCR done for IO "
  2233. "timeout case\n");
  2234. retval = SUCCESS;
  2235. } else {
  2236. dev_info(&instance->pdev->dev, "Controller is not "
  2237. "operational after 240 seconds wait for IO "
  2238. "timeout case in FW crash dump mode\n do "
  2239. "OCR/kill adapter\n");
  2240. retval = megasas_reset_fusion(shost, 0);
  2241. }
  2242. return retval;
  2243. }
  2244. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  2245. del_timer_sync(&instance->sriov_heartbeat_timer);
  2246. set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  2247. instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
  2248. instance->instancet->disable_intr(instance);
  2249. msleep(1000);
  2250. /* First try waiting for commands to complete */
  2251. if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
  2252. &convert)) {
  2253. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2254. printk(KERN_WARNING "megaraid_sas: resetting fusion "
  2255. "adapter scsi%d.\n", instance->host->host_no);
  2256. if (convert)
  2257. iotimeout = 0;
  2258. /* Now return commands back to the OS */
  2259. for (i = 0 ; i < instance->max_fw_cmds; i++) {
  2260. cmd_fusion = fusion->cmd_list[i];
  2261. if (cmd_fusion->scmd) {
  2262. scsi_dma_unmap(cmd_fusion->scmd);
  2263. cmd_fusion->scmd->result =
  2264. megasas_check_mpio_paths(instance,
  2265. cmd_fusion->scmd);
  2266. cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
  2267. megasas_return_cmd_fusion(instance, cmd_fusion);
  2268. atomic_dec(&instance->fw_outstanding);
  2269. }
  2270. }
  2271. status_reg = instance->instancet->read_fw_status_reg(
  2272. instance->reg_set);
  2273. abs_state = status_reg & MFI_STATE_MASK;
  2274. reset_adapter = status_reg & MFI_RESET_ADAPTER;
  2275. if (instance->disableOnlineCtrlReset ||
  2276. (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
  2277. /* Reset not supported, kill adapter */
  2278. printk(KERN_WARNING "megaraid_sas: Reset not supported"
  2279. ", killing adapter scsi%d.\n",
  2280. instance->host->host_no);
  2281. megaraid_sas_kill_hba(instance);
  2282. instance->skip_heartbeat_timer_del = 1;
  2283. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2284. retval = FAILED;
  2285. goto out;
  2286. }
  2287. /* Let SR-IOV VF & PF sync up if there was a HB failure */
  2288. if (instance->requestorId && !iotimeout) {
  2289. msleep(MEGASAS_OCR_SETTLE_TIME_VF);
  2290. /* Look for a late HB update after VF settle time */
  2291. if (abs_state == MFI_STATE_OPERATIONAL &&
  2292. (instance->hb_host_mem->HB.fwCounter !=
  2293. instance->hb_host_mem->HB.driverCounter)) {
  2294. instance->hb_host_mem->HB.driverCounter =
  2295. instance->hb_host_mem->HB.fwCounter;
  2296. printk(KERN_WARNING "megasas: SR-IOV:"
  2297. "Late FW heartbeat update for "
  2298. "scsi%d.\n",
  2299. instance->host->host_no);
  2300. } else {
  2301. /* In VF mode, first poll for FW ready */
  2302. for (i = 0;
  2303. i < (MEGASAS_RESET_WAIT_TIME * 1000);
  2304. i += 20) {
  2305. status_reg =
  2306. instance->instancet->
  2307. read_fw_status_reg(
  2308. instance->reg_set);
  2309. abs_state = status_reg &
  2310. MFI_STATE_MASK;
  2311. if (abs_state == MFI_STATE_READY) {
  2312. printk(KERN_WARNING "megasas"
  2313. ": SR-IOV: FW was found"
  2314. "to be in ready state "
  2315. "for scsi%d.\n",
  2316. instance->host->host_no);
  2317. break;
  2318. }
  2319. msleep(20);
  2320. }
  2321. if (abs_state != MFI_STATE_READY) {
  2322. printk(KERN_WARNING "megasas: SR-IOV: "
  2323. "FW not in ready state after %d"
  2324. " seconds for scsi%d, status_reg = "
  2325. "0x%x.\n",
  2326. MEGASAS_RESET_WAIT_TIME,
  2327. instance->host->host_no,
  2328. status_reg);
  2329. megaraid_sas_kill_hba(instance);
  2330. instance->skip_heartbeat_timer_del = 1;
  2331. instance->adprecovery =
  2332. MEGASAS_HW_CRITICAL_ERROR;
  2333. retval = FAILED;
  2334. goto out;
  2335. }
  2336. }
  2337. }
  2338. /* Now try to reset the chip */
  2339. for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
  2340. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
  2341. &instance->reg_set->fusion_seq_offset);
  2342. writel(MPI2_WRSEQ_1ST_KEY_VALUE,
  2343. &instance->reg_set->fusion_seq_offset);
  2344. writel(MPI2_WRSEQ_2ND_KEY_VALUE,
  2345. &instance->reg_set->fusion_seq_offset);
  2346. writel(MPI2_WRSEQ_3RD_KEY_VALUE,
  2347. &instance->reg_set->fusion_seq_offset);
  2348. writel(MPI2_WRSEQ_4TH_KEY_VALUE,
  2349. &instance->reg_set->fusion_seq_offset);
  2350. writel(MPI2_WRSEQ_5TH_KEY_VALUE,
  2351. &instance->reg_set->fusion_seq_offset);
  2352. writel(MPI2_WRSEQ_6TH_KEY_VALUE,
  2353. &instance->reg_set->fusion_seq_offset);
  2354. /* Check that the diag write enable (DRWE) bit is on */
  2355. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2356. retry = 0;
  2357. while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
  2358. msleep(100);
  2359. host_diag =
  2360. readl(&instance->reg_set->fusion_host_diag);
  2361. if (retry++ == 100) {
  2362. printk(KERN_WARNING "megaraid_sas: "
  2363. "Host diag unlock failed! "
  2364. "for scsi%d\n",
  2365. instance->host->host_no);
  2366. break;
  2367. }
  2368. }
  2369. if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
  2370. continue;
  2371. /* Send chip reset command */
  2372. writel(host_diag | HOST_DIAG_RESET_ADAPTER,
  2373. &instance->reg_set->fusion_host_diag);
  2374. msleep(3000);
  2375. /* Make sure reset adapter bit is cleared */
  2376. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2377. retry = 0;
  2378. while (host_diag & HOST_DIAG_RESET_ADAPTER) {
  2379. msleep(100);
  2380. host_diag =
  2381. readl(&instance->reg_set->fusion_host_diag);
  2382. if (retry++ == 1000) {
  2383. printk(KERN_WARNING "megaraid_sas: "
  2384. "Diag reset adapter never "
  2385. "cleared for scsi%d!\n",
  2386. instance->host->host_no);
  2387. break;
  2388. }
  2389. }
  2390. if (host_diag & HOST_DIAG_RESET_ADAPTER)
  2391. continue;
  2392. abs_state =
  2393. instance->instancet->read_fw_status_reg(
  2394. instance->reg_set) & MFI_STATE_MASK;
  2395. retry = 0;
  2396. while ((abs_state <= MFI_STATE_FW_INIT) &&
  2397. (retry++ < 1000)) {
  2398. msleep(100);
  2399. abs_state =
  2400. instance->instancet->read_fw_status_reg(
  2401. instance->reg_set) & MFI_STATE_MASK;
  2402. }
  2403. if (abs_state <= MFI_STATE_FW_INIT) {
  2404. printk(KERN_WARNING "megaraid_sas: firmware "
  2405. "state < MFI_STATE_FW_INIT, state = "
  2406. "0x%x for scsi%d\n", abs_state,
  2407. instance->host->host_no);
  2408. continue;
  2409. }
  2410. /* Wait for FW to become ready */
  2411. if (megasas_transition_to_ready(instance, 1)) {
  2412. printk(KERN_WARNING "megaraid_sas: Failed to "
  2413. "transition controller to ready "
  2414. "for scsi%d.\n",
  2415. instance->host->host_no);
  2416. continue;
  2417. }
  2418. megasas_reset_reply_desc(instance);
  2419. if (megasas_ioc_init_fusion(instance)) {
  2420. printk(KERN_WARNING "megaraid_sas: "
  2421. "megasas_ioc_init_fusion() failed!"
  2422. " for scsi%d\n",
  2423. instance->host->host_no);
  2424. continue;
  2425. }
  2426. /* Re-fire management commands */
  2427. for (j = 0 ; j < instance->max_fw_cmds; j++) {
  2428. cmd_fusion = fusion->cmd_list[j];
  2429. if (cmd_fusion->sync_cmd_idx !=
  2430. (u32)ULONG_MAX) {
  2431. cmd_mfi =
  2432. instance->
  2433. cmd_list[cmd_fusion->sync_cmd_idx];
  2434. if (cmd_mfi->frame->dcmd.opcode ==
  2435. cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) {
  2436. megasas_return_mfi_mpt_pthr(instance, cmd_mfi, cmd_fusion);
  2437. } else {
  2438. req_desc =
  2439. megasas_get_request_descriptor(
  2440. instance,
  2441. cmd_mfi->context.smid
  2442. -1);
  2443. if (!req_desc) {
  2444. printk(KERN_WARNING
  2445. "req_desc NULL"
  2446. " for scsi%d\n",
  2447. instance->host->host_no);
  2448. /* Return leaked MPT
  2449. frame */
  2450. megasas_return_cmd_fusion(instance, cmd_fusion);
  2451. } else {
  2452. instance->instancet->
  2453. fire_cmd(instance,
  2454. req_desc->
  2455. u.low,
  2456. req_desc->
  2457. u.high,
  2458. instance->
  2459. reg_set);
  2460. }
  2461. }
  2462. }
  2463. }
  2464. clear_bit(MEGASAS_FUSION_IN_RESET,
  2465. &instance->reset_flags);
  2466. instance->instancet->enable_intr(instance);
  2467. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2468. /* Reset load balance info */
  2469. memset(fusion->load_balance_info, 0,
  2470. sizeof(struct LD_LOAD_BALANCE_INFO)
  2471. *MAX_LOGICAL_DRIVES_EXT);
  2472. if (!megasas_get_map_info(instance))
  2473. megasas_sync_map_info(instance);
  2474. /* Restart SR-IOV heartbeat */
  2475. if (instance->requestorId) {
  2476. if (!megasas_sriov_start_heartbeat(instance, 0))
  2477. megasas_start_timer(instance,
  2478. &instance->sriov_heartbeat_timer,
  2479. megasas_sriov_heartbeat_handler,
  2480. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  2481. else
  2482. instance->skip_heartbeat_timer_del = 1;
  2483. }
  2484. /* Adapter reset completed successfully */
  2485. printk(KERN_WARNING "megaraid_sas: Reset "
  2486. "successful for scsi%d.\n",
  2487. instance->host->host_no);
  2488. if (instance->crash_dump_drv_support) {
  2489. if (instance->crash_dump_app_support)
  2490. megasas_set_crash_dump_params(instance,
  2491. MR_CRASH_BUF_TURN_ON);
  2492. else
  2493. megasas_set_crash_dump_params(instance,
  2494. MR_CRASH_BUF_TURN_OFF);
  2495. }
  2496. retval = SUCCESS;
  2497. goto out;
  2498. }
  2499. /* Reset failed, kill the adapter */
  2500. printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
  2501. "adapter scsi%d.\n", instance->host->host_no);
  2502. megaraid_sas_kill_hba(instance);
  2503. instance->skip_heartbeat_timer_del = 1;
  2504. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2505. retval = FAILED;
  2506. } else {
  2507. /* For VF: Restart HB timer if we didn't OCR */
  2508. if (instance->requestorId) {
  2509. megasas_start_timer(instance,
  2510. &instance->sriov_heartbeat_timer,
  2511. megasas_sriov_heartbeat_handler,
  2512. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  2513. }
  2514. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  2515. instance->instancet->enable_intr(instance);
  2516. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2517. }
  2518. out:
  2519. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  2520. mutex_unlock(&instance->reset_mutex);
  2521. return retval;
  2522. }
  2523. /* Fusion Crash dump collection work queue */
  2524. void megasas_fusion_crash_dump_wq(struct work_struct *work)
  2525. {
  2526. struct megasas_instance *instance =
  2527. container_of(work, struct megasas_instance, crash_init);
  2528. u32 status_reg;
  2529. u8 partial_copy = 0;
  2530. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  2531. /*
  2532. * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
  2533. * to host crash buffers
  2534. */
  2535. if (instance->drv_buf_index == 0) {
  2536. /* Buffer is already allocated for old Crash dump.
  2537. * Do OCR and do not wait for crash dump collection
  2538. */
  2539. if (instance->drv_buf_alloc) {
  2540. dev_info(&instance->pdev->dev, "earlier crash dump is "
  2541. "not yet copied by application, ignoring this "
  2542. "crash dump and initiating OCR\n");
  2543. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  2544. writel(status_reg,
  2545. &instance->reg_set->outbound_scratch_pad);
  2546. readl(&instance->reg_set->outbound_scratch_pad);
  2547. return;
  2548. }
  2549. megasas_alloc_host_crash_buffer(instance);
  2550. dev_info(&instance->pdev->dev, "Number of host crash buffers "
  2551. "allocated: %d\n", instance->drv_buf_alloc);
  2552. }
  2553. /*
  2554. * Driver has allocated max buffers, which can be allocated
  2555. * and FW has more crash dump data, then driver will
  2556. * ignore the data.
  2557. */
  2558. if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
  2559. dev_info(&instance->pdev->dev, "Driver is done copying "
  2560. "the buffer: %d\n", instance->drv_buf_alloc);
  2561. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  2562. partial_copy = 1;
  2563. } else {
  2564. memcpy(instance->crash_buf[instance->drv_buf_index],
  2565. instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
  2566. instance->drv_buf_index++;
  2567. status_reg &= ~MFI_STATE_DMADONE;
  2568. }
  2569. if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
  2570. dev_info(&instance->pdev->dev, "Crash Dump is available,number "
  2571. "of copied buffers: %d\n", instance->drv_buf_index);
  2572. instance->fw_crash_buffer_size = instance->drv_buf_index;
  2573. instance->fw_crash_state = AVAILABLE;
  2574. instance->drv_buf_index = 0;
  2575. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  2576. readl(&instance->reg_set->outbound_scratch_pad);
  2577. if (!partial_copy)
  2578. megasas_reset_fusion(instance->host, 0);
  2579. } else {
  2580. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  2581. readl(&instance->reg_set->outbound_scratch_pad);
  2582. }
  2583. }
  2584. /* Fusion OCR work queue */
  2585. void megasas_fusion_ocr_wq(struct work_struct *work)
  2586. {
  2587. struct megasas_instance *instance =
  2588. container_of(work, struct megasas_instance, work_init);
  2589. megasas_reset_fusion(instance->host, 0);
  2590. }
  2591. struct megasas_instance_template megasas_instance_template_fusion = {
  2592. .fire_cmd = megasas_fire_cmd_fusion,
  2593. .enable_intr = megasas_enable_intr_fusion,
  2594. .disable_intr = megasas_disable_intr_fusion,
  2595. .clear_intr = megasas_clear_intr_fusion,
  2596. .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
  2597. .adp_reset = megasas_adp_reset_fusion,
  2598. .check_reset = megasas_check_reset_fusion,
  2599. .service_isr = megasas_isr_fusion,
  2600. .tasklet = megasas_complete_cmd_dpc_fusion,
  2601. .init_adapter = megasas_init_adapter_fusion,
  2602. .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
  2603. .issue_dcmd = megasas_issue_dcmd_fusion,
  2604. };