megaraid_sas_fusion.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  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 "megaraid_sas_fusion.h"
  52. #include "megaraid_sas.h"
  53. extern void megasas_free_cmds(struct megasas_instance *instance);
  54. extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  55. *instance);
  56. extern void
  57. megasas_complete_cmd(struct megasas_instance *instance,
  58. struct megasas_cmd *cmd, u8 alt_status);
  59. int megasas_is_ldio(struct scsi_cmnd *cmd);
  60. int
  61. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
  62. void
  63. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
  64. int megasas_alloc_cmds(struct megasas_instance *instance);
  65. int
  66. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
  67. int
  68. megasas_issue_polled(struct megasas_instance *instance,
  69. struct megasas_cmd *cmd);
  70. u8
  71. MR_BuildRaidContext(struct megasas_instance *instance,
  72. struct IO_REQUEST_INFO *io_info,
  73. struct RAID_CONTEXT *pRAID_Context,
  74. struct MR_FW_RAID_MAP_ALL *map);
  75. u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
  76. struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
  77. u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
  78. void
  79. megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
  80. u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
  81. struct LD_LOAD_BALANCE_INFO *lbInfo);
  82. u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
  83. struct IO_REQUEST_INFO *in_info);
  84. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  85. void megaraid_sas_kill_hba(struct megasas_instance *instance);
  86. extern u32 megasas_dbg_lvl;
  87. extern int resetwaittime;
  88. /**
  89. * megasas_enable_intr_fusion - Enables interrupts
  90. * @regs: MFI register set
  91. */
  92. void
  93. megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
  94. {
  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_register_set __iomem *regs)
  108. {
  109. u32 mask = 0xFFFFFFFF;
  110. u32 status;
  111. writel(mask, &regs->outbound_intr_mask);
  112. /* Dummy readl to force pci flush */
  113. status = readl(&regs->outbound_intr_mask);
  114. }
  115. int
  116. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
  117. {
  118. u32 status;
  119. /*
  120. * Check if it is our interrupt
  121. */
  122. status = readl(&regs->outbound_intr_status);
  123. if (status & 1) {
  124. writel(status, &regs->outbound_intr_status);
  125. readl(&regs->outbound_intr_status);
  126. return 1;
  127. }
  128. if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
  129. return 0;
  130. return 1;
  131. }
  132. /**
  133. * megasas_get_cmd_fusion - Get a command from the free pool
  134. * @instance: Adapter soft state
  135. *
  136. * Returns a free command from the pool
  137. */
  138. struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
  139. *instance)
  140. {
  141. unsigned long flags;
  142. struct fusion_context *fusion =
  143. (struct fusion_context *)instance->ctrl_context;
  144. struct megasas_cmd_fusion *cmd = NULL;
  145. spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
  146. if (!list_empty(&fusion->cmd_pool)) {
  147. cmd = list_entry((&fusion->cmd_pool)->next,
  148. struct megasas_cmd_fusion, list);
  149. list_del_init(&cmd->list);
  150. } else {
  151. printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
  152. }
  153. spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
  154. return cmd;
  155. }
  156. /**
  157. * megasas_return_cmd_fusion - Return a cmd to free command pool
  158. * @instance: Adapter soft state
  159. * @cmd: Command packet to be returned to free command pool
  160. */
  161. static inline void
  162. megasas_return_cmd_fusion(struct megasas_instance *instance,
  163. struct megasas_cmd_fusion *cmd)
  164. {
  165. unsigned long flags;
  166. struct fusion_context *fusion =
  167. (struct fusion_context *)instance->ctrl_context;
  168. spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
  169. cmd->scmd = NULL;
  170. cmd->sync_cmd_idx = (u32)ULONG_MAX;
  171. list_add_tail(&cmd->list, &fusion->cmd_pool);
  172. spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
  173. }
  174. /**
  175. * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
  176. * @instance: Adapter soft state
  177. */
  178. static void megasas_teardown_frame_pool_fusion(
  179. struct megasas_instance *instance)
  180. {
  181. int i;
  182. struct fusion_context *fusion = instance->ctrl_context;
  183. u16 max_cmd = instance->max_fw_cmds;
  184. struct megasas_cmd_fusion *cmd;
  185. if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
  186. printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
  187. "sense pool : %p\n", fusion->sg_dma_pool,
  188. fusion->sense_dma_pool);
  189. return;
  190. }
  191. /*
  192. * Return all frames to pool
  193. */
  194. for (i = 0; i < max_cmd; i++) {
  195. cmd = fusion->cmd_list[i];
  196. if (cmd->sg_frame)
  197. pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
  198. cmd->sg_frame_phys_addr);
  199. if (cmd->sense)
  200. pci_pool_free(fusion->sense_dma_pool, cmd->sense,
  201. cmd->sense_phys_addr);
  202. }
  203. /*
  204. * Now destroy the pool itself
  205. */
  206. pci_pool_destroy(fusion->sg_dma_pool);
  207. pci_pool_destroy(fusion->sense_dma_pool);
  208. fusion->sg_dma_pool = NULL;
  209. fusion->sense_dma_pool = NULL;
  210. }
  211. /**
  212. * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
  213. * @instance: Adapter soft state
  214. */
  215. void
  216. megasas_free_cmds_fusion(struct megasas_instance *instance)
  217. {
  218. int i;
  219. struct fusion_context *fusion = instance->ctrl_context;
  220. u32 max_cmds, req_sz, reply_sz, io_frames_sz;
  221. req_sz = fusion->request_alloc_sz;
  222. reply_sz = fusion->reply_alloc_sz;
  223. io_frames_sz = fusion->io_frames_alloc_sz;
  224. max_cmds = instance->max_fw_cmds;
  225. /* Free descriptors and request Frames memory */
  226. if (fusion->req_frames_desc)
  227. dma_free_coherent(&instance->pdev->dev, req_sz,
  228. fusion->req_frames_desc,
  229. fusion->req_frames_desc_phys);
  230. if (fusion->reply_frames_desc) {
  231. pci_pool_free(fusion->reply_frames_desc_pool,
  232. fusion->reply_frames_desc,
  233. fusion->reply_frames_desc_phys);
  234. pci_pool_destroy(fusion->reply_frames_desc_pool);
  235. }
  236. if (fusion->io_request_frames) {
  237. pci_pool_free(fusion->io_request_frames_pool,
  238. fusion->io_request_frames,
  239. fusion->io_request_frames_phys);
  240. pci_pool_destroy(fusion->io_request_frames_pool);
  241. }
  242. /* Free the Fusion frame pool */
  243. megasas_teardown_frame_pool_fusion(instance);
  244. /* Free all the commands in the cmd_list */
  245. for (i = 0; i < max_cmds; i++)
  246. kfree(fusion->cmd_list[i]);
  247. /* Free the cmd_list buffer itself */
  248. kfree(fusion->cmd_list);
  249. fusion->cmd_list = NULL;
  250. INIT_LIST_HEAD(&fusion->cmd_pool);
  251. }
  252. /**
  253. * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
  254. * @instance: Adapter soft state
  255. *
  256. */
  257. static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
  258. {
  259. int i;
  260. u32 max_cmd;
  261. struct fusion_context *fusion;
  262. struct megasas_cmd_fusion *cmd;
  263. u32 total_sz_chain_frame;
  264. fusion = instance->ctrl_context;
  265. max_cmd = instance->max_fw_cmds;
  266. total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
  267. /*
  268. * Use DMA pool facility provided by PCI layer
  269. */
  270. fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
  271. instance->pdev,
  272. total_sz_chain_frame, 4,
  273. 0);
  274. if (!fusion->sg_dma_pool) {
  275. printk(KERN_DEBUG "megasas: failed to setup request pool "
  276. "fusion\n");
  277. return -ENOMEM;
  278. }
  279. fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
  280. instance->pdev,
  281. SCSI_SENSE_BUFFERSIZE, 64, 0);
  282. if (!fusion->sense_dma_pool) {
  283. printk(KERN_DEBUG "megasas: failed to setup sense pool "
  284. "fusion\n");
  285. pci_pool_destroy(fusion->sg_dma_pool);
  286. fusion->sg_dma_pool = NULL;
  287. return -ENOMEM;
  288. }
  289. /*
  290. * Allocate and attach a frame to each of the commands in cmd_list
  291. */
  292. for (i = 0; i < max_cmd; i++) {
  293. cmd = fusion->cmd_list[i];
  294. cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
  295. GFP_KERNEL,
  296. &cmd->sg_frame_phys_addr);
  297. cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
  298. GFP_KERNEL, &cmd->sense_phys_addr);
  299. /*
  300. * megasas_teardown_frame_pool_fusion() takes care of freeing
  301. * whatever has been allocated
  302. */
  303. if (!cmd->sg_frame || !cmd->sense) {
  304. printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
  305. megasas_teardown_frame_pool_fusion(instance);
  306. return -ENOMEM;
  307. }
  308. }
  309. return 0;
  310. }
  311. /**
  312. * megasas_alloc_cmds_fusion - Allocates the command packets
  313. * @instance: Adapter soft state
  314. *
  315. *
  316. * Each frame has a 32-bit field called context. This context is used to get
  317. * back the megasas_cmd_fusion from the frame when a frame gets completed
  318. * In this driver, the 32 bit values are the indices into an array cmd_list.
  319. * This array is used only to look up the megasas_cmd_fusion given the context.
  320. * The free commands themselves are maintained in a linked list called cmd_pool.
  321. *
  322. * cmds are formed in the io_request and sg_frame members of the
  323. * megasas_cmd_fusion. The context field is used to get a request descriptor
  324. * and is used as SMID of the cmd.
  325. * SMID value range is from 1 to max_fw_cmds.
  326. */
  327. int
  328. megasas_alloc_cmds_fusion(struct megasas_instance *instance)
  329. {
  330. int i, j, count;
  331. u32 max_cmd, io_frames_sz;
  332. struct fusion_context *fusion;
  333. struct megasas_cmd_fusion *cmd;
  334. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  335. u32 offset;
  336. dma_addr_t io_req_base_phys;
  337. u8 *io_req_base;
  338. fusion = instance->ctrl_context;
  339. max_cmd = instance->max_fw_cmds;
  340. fusion->req_frames_desc =
  341. dma_alloc_coherent(&instance->pdev->dev,
  342. fusion->request_alloc_sz,
  343. &fusion->req_frames_desc_phys, GFP_KERNEL);
  344. if (!fusion->req_frames_desc) {
  345. printk(KERN_ERR "megasas; Could not allocate memory for "
  346. "request_frames\n");
  347. goto fail_req_desc;
  348. }
  349. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  350. fusion->reply_frames_desc_pool =
  351. pci_pool_create("reply_frames pool", instance->pdev,
  352. fusion->reply_alloc_sz * count, 16, 0);
  353. if (!fusion->reply_frames_desc_pool) {
  354. printk(KERN_ERR "megasas; Could not allocate memory for "
  355. "reply_frame pool\n");
  356. goto fail_reply_desc;
  357. }
  358. fusion->reply_frames_desc =
  359. pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
  360. &fusion->reply_frames_desc_phys);
  361. if (!fusion->reply_frames_desc) {
  362. printk(KERN_ERR "megasas; Could not allocate memory for "
  363. "reply_frame pool\n");
  364. pci_pool_destroy(fusion->reply_frames_desc_pool);
  365. goto fail_reply_desc;
  366. }
  367. reply_desc = fusion->reply_frames_desc;
  368. for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
  369. reply_desc->Words = ULLONG_MAX;
  370. io_frames_sz = fusion->io_frames_alloc_sz;
  371. fusion->io_request_frames_pool =
  372. pci_pool_create("io_request_frames pool", instance->pdev,
  373. fusion->io_frames_alloc_sz, 16, 0);
  374. if (!fusion->io_request_frames_pool) {
  375. printk(KERN_ERR "megasas: Could not allocate memory for "
  376. "io_request_frame pool\n");
  377. goto fail_io_frames;
  378. }
  379. fusion->io_request_frames =
  380. pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
  381. &fusion->io_request_frames_phys);
  382. if (!fusion->io_request_frames) {
  383. printk(KERN_ERR "megasas: Could not allocate memory for "
  384. "io_request_frames frames\n");
  385. pci_pool_destroy(fusion->io_request_frames_pool);
  386. goto fail_io_frames;
  387. }
  388. /*
  389. * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
  390. * Allocate the dynamic array first and then allocate individual
  391. * commands.
  392. */
  393. fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
  394. * max_cmd, GFP_KERNEL);
  395. if (!fusion->cmd_list) {
  396. printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
  397. "memory for cmd_list_fusion\n");
  398. goto fail_cmd_list;
  399. }
  400. max_cmd = instance->max_fw_cmds;
  401. for (i = 0; i < max_cmd; i++) {
  402. fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
  403. GFP_KERNEL);
  404. if (!fusion->cmd_list[i]) {
  405. printk(KERN_ERR "Could not alloc cmd list fusion\n");
  406. for (j = 0; j < i; j++)
  407. kfree(fusion->cmd_list[j]);
  408. kfree(fusion->cmd_list);
  409. fusion->cmd_list = NULL;
  410. goto fail_cmd_list;
  411. }
  412. }
  413. /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
  414. io_req_base = fusion->io_request_frames +
  415. MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  416. io_req_base_phys = fusion->io_request_frames_phys +
  417. MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  418. /*
  419. * Add all the commands to command pool (fusion->cmd_pool)
  420. */
  421. /* SMID 0 is reserved. Set SMID/index from 1 */
  422. for (i = 0; i < max_cmd; i++) {
  423. cmd = fusion->cmd_list[i];
  424. offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
  425. memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
  426. cmd->index = i + 1;
  427. cmd->scmd = NULL;
  428. cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
  429. cmd->instance = instance;
  430. cmd->io_request =
  431. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  432. (io_req_base + offset);
  433. memset(cmd->io_request, 0,
  434. sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
  435. cmd->io_request_phys_addr = io_req_base_phys + offset;
  436. list_add_tail(&cmd->list, &fusion->cmd_pool);
  437. }
  438. /*
  439. * Create a frame pool and assign one frame to each cmd
  440. */
  441. if (megasas_create_frame_pool_fusion(instance)) {
  442. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  443. megasas_free_cmds_fusion(instance);
  444. goto fail_req_desc;
  445. }
  446. return 0;
  447. fail_cmd_list:
  448. pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
  449. fusion->io_request_frames_phys);
  450. pci_pool_destroy(fusion->io_request_frames_pool);
  451. fail_io_frames:
  452. dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
  453. fusion->reply_frames_desc,
  454. fusion->reply_frames_desc_phys);
  455. pci_pool_free(fusion->reply_frames_desc_pool,
  456. fusion->reply_frames_desc,
  457. fusion->reply_frames_desc_phys);
  458. pci_pool_destroy(fusion->reply_frames_desc_pool);
  459. fail_reply_desc:
  460. dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
  461. fusion->req_frames_desc,
  462. fusion->req_frames_desc_phys);
  463. fail_req_desc:
  464. return -ENOMEM;
  465. }
  466. /**
  467. * wait_and_poll - Issues a polling command
  468. * @instance: Adapter soft state
  469. * @cmd: Command packet to be issued
  470. *
  471. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  472. */
  473. int
  474. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
  475. {
  476. int i;
  477. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  478. u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
  479. /*
  480. * Wait for cmd_status to change
  481. */
  482. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
  483. rmb();
  484. msleep(20);
  485. }
  486. if (frame_hdr->cmd_status == 0xff)
  487. return -ETIME;
  488. return 0;
  489. }
  490. /**
  491. * megasas_ioc_init_fusion - Initializes the FW
  492. * @instance: Adapter soft state
  493. *
  494. * Issues the IOC Init cmd
  495. */
  496. int
  497. megasas_ioc_init_fusion(struct megasas_instance *instance)
  498. {
  499. struct megasas_init_frame *init_frame;
  500. struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
  501. dma_addr_t ioc_init_handle;
  502. struct megasas_cmd *cmd;
  503. u8 ret;
  504. struct fusion_context *fusion;
  505. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  506. int i;
  507. struct megasas_header *frame_hdr;
  508. fusion = instance->ctrl_context;
  509. cmd = megasas_get_cmd(instance);
  510. if (!cmd) {
  511. printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
  512. ret = 1;
  513. goto fail_get_cmd;
  514. }
  515. IOCInitMessage =
  516. dma_alloc_coherent(&instance->pdev->dev,
  517. sizeof(struct MPI2_IOC_INIT_REQUEST),
  518. &ioc_init_handle, GFP_KERNEL);
  519. if (!IOCInitMessage) {
  520. printk(KERN_ERR "Could not allocate memory for "
  521. "IOCInitMessage\n");
  522. ret = 1;
  523. goto fail_fw_init;
  524. }
  525. memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
  526. IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
  527. IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  528. IOCInitMessage->MsgVersion = MPI2_VERSION;
  529. IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
  530. IOCInitMessage->SystemRequestFrameSize =
  531. MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
  532. IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
  533. IOCInitMessage->ReplyDescriptorPostQueueAddress =
  534. fusion->reply_frames_desc_phys;
  535. IOCInitMessage->SystemRequestFrameBaseAddress =
  536. fusion->io_request_frames_phys;
  537. IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
  538. init_frame = (struct megasas_init_frame *)cmd->frame;
  539. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  540. frame_hdr = &cmd->frame->hdr;
  541. frame_hdr->cmd_status = 0xFF;
  542. frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  543. init_frame->cmd = MFI_CMD_INIT;
  544. init_frame->cmd_status = 0xFF;
  545. init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
  546. init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
  547. req_desc =
  548. (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
  549. req_desc->Words = cmd->frame_phys_addr;
  550. req_desc->MFAIo.RequestFlags =
  551. (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
  552. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  553. /*
  554. * disable the intr before firing the init frame
  555. */
  556. instance->instancet->disable_intr(instance->reg_set);
  557. for (i = 0; i < (10 * 1000); i += 20) {
  558. if (readl(&instance->reg_set->doorbell) & 1)
  559. msleep(20);
  560. else
  561. break;
  562. }
  563. instance->instancet->fire_cmd(instance, req_desc->u.low,
  564. req_desc->u.high, instance->reg_set);
  565. wait_and_poll(instance, cmd);
  566. frame_hdr = &cmd->frame->hdr;
  567. if (frame_hdr->cmd_status != 0) {
  568. ret = 1;
  569. goto fail_fw_init;
  570. }
  571. printk(KERN_ERR "megasas:IOC Init cmd success\n");
  572. ret = 0;
  573. fail_fw_init:
  574. megasas_return_cmd(instance, cmd);
  575. if (IOCInitMessage)
  576. dma_free_coherent(&instance->pdev->dev,
  577. sizeof(struct MPI2_IOC_INIT_REQUEST),
  578. IOCInitMessage, ioc_init_handle);
  579. fail_get_cmd:
  580. return ret;
  581. }
  582. /*
  583. * megasas_get_ld_map_info - Returns FW's ld_map structure
  584. * @instance: Adapter soft state
  585. * @pend: Pend the command or not
  586. * Issues an internal command (DCMD) to get the FW's controller PD
  587. * list structure. This information is mainly used to find out SYSTEM
  588. * supported by the FW.
  589. */
  590. static int
  591. megasas_get_ld_map_info(struct megasas_instance *instance)
  592. {
  593. int ret = 0;
  594. struct megasas_cmd *cmd;
  595. struct megasas_dcmd_frame *dcmd;
  596. struct MR_FW_RAID_MAP_ALL *ci;
  597. dma_addr_t ci_h = 0;
  598. u32 size_map_info;
  599. struct fusion_context *fusion;
  600. cmd = megasas_get_cmd(instance);
  601. if (!cmd) {
  602. printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
  603. return -ENOMEM;
  604. }
  605. fusion = instance->ctrl_context;
  606. if (!fusion) {
  607. megasas_return_cmd(instance, cmd);
  608. return 1;
  609. }
  610. dcmd = &cmd->frame->dcmd;
  611. size_map_info = sizeof(struct MR_FW_RAID_MAP) +
  612. (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
  613. ci = fusion->ld_map[(instance->map_id & 1)];
  614. ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
  615. if (!ci) {
  616. printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
  617. megasas_return_cmd(instance, cmd);
  618. return -ENOMEM;
  619. }
  620. memset(ci, 0, sizeof(*ci));
  621. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  622. dcmd->cmd = MFI_CMD_DCMD;
  623. dcmd->cmd_status = 0xFF;
  624. dcmd->sge_count = 1;
  625. dcmd->flags = MFI_FRAME_DIR_READ;
  626. dcmd->timeout = 0;
  627. dcmd->pad_0 = 0;
  628. dcmd->data_xfer_len = size_map_info;
  629. dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
  630. dcmd->sgl.sge32[0].phys_addr = ci_h;
  631. dcmd->sgl.sge32[0].length = size_map_info;
  632. if (!megasas_issue_polled(instance, cmd))
  633. ret = 0;
  634. else {
  635. printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
  636. ret = -1;
  637. }
  638. megasas_return_cmd(instance, cmd);
  639. return ret;
  640. }
  641. u8
  642. megasas_get_map_info(struct megasas_instance *instance)
  643. {
  644. struct fusion_context *fusion = instance->ctrl_context;
  645. fusion->fast_path_io = 0;
  646. if (!megasas_get_ld_map_info(instance)) {
  647. if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
  648. fusion->load_balance_info)) {
  649. fusion->fast_path_io = 1;
  650. return 0;
  651. }
  652. }
  653. return 1;
  654. }
  655. /*
  656. * megasas_sync_map_info - Returns FW's ld_map structure
  657. * @instance: Adapter soft state
  658. *
  659. * Issues an internal command (DCMD) to get the FW's controller PD
  660. * list structure. This information is mainly used to find out SYSTEM
  661. * supported by the FW.
  662. */
  663. int
  664. megasas_sync_map_info(struct megasas_instance *instance)
  665. {
  666. int ret = 0, i;
  667. struct megasas_cmd *cmd;
  668. struct megasas_dcmd_frame *dcmd;
  669. u32 size_sync_info, num_lds;
  670. struct fusion_context *fusion;
  671. struct MR_LD_TARGET_SYNC *ci = NULL;
  672. struct MR_FW_RAID_MAP_ALL *map;
  673. struct MR_LD_RAID *raid;
  674. struct MR_LD_TARGET_SYNC *ld_sync;
  675. dma_addr_t ci_h = 0;
  676. u32 size_map_info;
  677. cmd = megasas_get_cmd(instance);
  678. if (!cmd) {
  679. printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
  680. "info.\n");
  681. return -ENOMEM;
  682. }
  683. fusion = instance->ctrl_context;
  684. if (!fusion) {
  685. megasas_return_cmd(instance, cmd);
  686. return 1;
  687. }
  688. map = fusion->ld_map[instance->map_id & 1];
  689. num_lds = map->raidMap.ldCount;
  690. dcmd = &cmd->frame->dcmd;
  691. size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
  692. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  693. ci = (struct MR_LD_TARGET_SYNC *)
  694. fusion->ld_map[(instance->map_id - 1) & 1];
  695. memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
  696. ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
  697. ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
  698. for (i = 0; i < num_lds; i++, ld_sync++) {
  699. raid = MR_LdRaidGet(i, map);
  700. ld_sync->targetId = MR_GetLDTgtId(i, map);
  701. ld_sync->seqNum = raid->seqNum;
  702. }
  703. size_map_info = sizeof(struct MR_FW_RAID_MAP) +
  704. (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
  705. dcmd->cmd = MFI_CMD_DCMD;
  706. dcmd->cmd_status = 0xFF;
  707. dcmd->sge_count = 1;
  708. dcmd->flags = MFI_FRAME_DIR_WRITE;
  709. dcmd->timeout = 0;
  710. dcmd->pad_0 = 0;
  711. dcmd->data_xfer_len = size_map_info;
  712. dcmd->mbox.b[0] = num_lds;
  713. dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  714. dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
  715. dcmd->sgl.sge32[0].phys_addr = ci_h;
  716. dcmd->sgl.sge32[0].length = size_map_info;
  717. instance->map_update_cmd = cmd;
  718. instance->instancet->issue_dcmd(instance, cmd);
  719. return ret;
  720. }
  721. /**
  722. * megasas_init_adapter_fusion - Initializes the FW
  723. * @instance: Adapter soft state
  724. *
  725. * This is the main function for initializing firmware.
  726. */
  727. u32
  728. megasas_init_adapter_fusion(struct megasas_instance *instance)
  729. {
  730. struct megasas_register_set __iomem *reg_set;
  731. struct fusion_context *fusion;
  732. u32 max_cmd;
  733. int i = 0, count;
  734. fusion = instance->ctrl_context;
  735. reg_set = instance->reg_set;
  736. /*
  737. * Get various operational parameters from status register
  738. */
  739. instance->max_fw_cmds =
  740. instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  741. instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
  742. /*
  743. * Reduce the max supported cmds by 1. This is to ensure that the
  744. * reply_q_sz (1 more than the max cmd that driver may send)
  745. * does not exceed max cmds that the FW can support
  746. */
  747. instance->max_fw_cmds = instance->max_fw_cmds-1;
  748. /* Only internal cmds (DCMD) need to have MFI frames */
  749. instance->max_mfi_cmds = MEGASAS_INT_CMDS;
  750. max_cmd = instance->max_fw_cmds;
  751. fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
  752. fusion->request_alloc_sz =
  753. sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
  754. fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
  755. *(fusion->reply_q_depth);
  756. fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
  757. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
  758. (max_cmd + 1)); /* Extra 1 for SMID 0 */
  759. fusion->max_sge_in_main_msg =
  760. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
  761. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
  762. fusion->max_sge_in_chain =
  763. MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
  764. instance->max_num_sge = fusion->max_sge_in_main_msg +
  765. fusion->max_sge_in_chain - 2;
  766. /* Used for pass thru MFI frame (DCMD) */
  767. fusion->chain_offset_mfi_pthru =
  768. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
  769. fusion->chain_offset_io_request =
  770. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
  771. sizeof(union MPI2_SGE_IO_UNION))/16;
  772. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  773. for (i = 0 ; i < count; i++)
  774. fusion->last_reply_idx[i] = 0;
  775. /*
  776. * Allocate memory for descriptors
  777. * Create a pool of commands
  778. */
  779. if (megasas_alloc_cmds(instance))
  780. goto fail_alloc_mfi_cmds;
  781. if (megasas_alloc_cmds_fusion(instance))
  782. goto fail_alloc_cmds;
  783. if (megasas_ioc_init_fusion(instance))
  784. goto fail_ioc_init;
  785. instance->flag_ieee = 1;
  786. fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
  787. (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
  788. fusion->fast_path_io = 0;
  789. for (i = 0; i < 2; i++) {
  790. fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
  791. fusion->map_sz,
  792. &fusion->ld_map_phys[i],
  793. GFP_KERNEL);
  794. if (!fusion->ld_map[i]) {
  795. printk(KERN_ERR "megasas: Could not allocate memory "
  796. "for map info\n");
  797. goto fail_map_info;
  798. }
  799. }
  800. if (!megasas_get_map_info(instance))
  801. megasas_sync_map_info(instance);
  802. return 0;
  803. fail_map_info:
  804. if (i == 1)
  805. dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
  806. fusion->ld_map[0], fusion->ld_map_phys[0]);
  807. fail_ioc_init:
  808. megasas_free_cmds_fusion(instance);
  809. fail_alloc_cmds:
  810. megasas_free_cmds(instance);
  811. fail_alloc_mfi_cmds:
  812. return 1;
  813. }
  814. /**
  815. * megasas_fire_cmd_fusion - Sends command to the FW
  816. * @frame_phys_addr : Physical address of cmd
  817. * @frame_count : Number of frames for the command
  818. * @regs : MFI register set
  819. */
  820. void
  821. megasas_fire_cmd_fusion(struct megasas_instance *instance,
  822. dma_addr_t req_desc_lo,
  823. u32 req_desc_hi,
  824. struct megasas_register_set __iomem *regs)
  825. {
  826. unsigned long flags;
  827. spin_lock_irqsave(&instance->hba_lock, flags);
  828. writel(req_desc_lo,
  829. &(regs)->inbound_low_queue_port);
  830. writel(req_desc_hi, &(regs)->inbound_high_queue_port);
  831. spin_unlock_irqrestore(&instance->hba_lock, flags);
  832. }
  833. /**
  834. * map_cmd_status - Maps FW cmd status to OS cmd status
  835. * @cmd : Pointer to cmd
  836. * @status : status of cmd returned by FW
  837. * @ext_status : ext status of cmd returned by FW
  838. */
  839. void
  840. map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
  841. {
  842. switch (status) {
  843. case MFI_STAT_OK:
  844. cmd->scmd->result = DID_OK << 16;
  845. break;
  846. case MFI_STAT_SCSI_IO_FAILED:
  847. case MFI_STAT_LD_INIT_IN_PROGRESS:
  848. cmd->scmd->result = (DID_ERROR << 16) | ext_status;
  849. break;
  850. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  851. cmd->scmd->result = (DID_OK << 16) | ext_status;
  852. if (ext_status == SAM_STAT_CHECK_CONDITION) {
  853. memset(cmd->scmd->sense_buffer, 0,
  854. SCSI_SENSE_BUFFERSIZE);
  855. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  856. SCSI_SENSE_BUFFERSIZE);
  857. cmd->scmd->result |= DRIVER_SENSE << 24;
  858. }
  859. break;
  860. case MFI_STAT_LD_OFFLINE:
  861. case MFI_STAT_DEVICE_NOT_FOUND:
  862. cmd->scmd->result = DID_BAD_TARGET << 16;
  863. break;
  864. case MFI_STAT_CONFIG_SEQ_MISMATCH:
  865. cmd->scmd->result = DID_IMM_RETRY << 16;
  866. break;
  867. default:
  868. printk(KERN_DEBUG "megasas: FW status %#x\n", status);
  869. cmd->scmd->result = DID_ERROR << 16;
  870. break;
  871. }
  872. }
  873. /**
  874. * megasas_make_sgl_fusion - Prepares 32-bit SGL
  875. * @instance: Adapter soft state
  876. * @scp: SCSI command from the mid-layer
  877. * @sgl_ptr: SGL to be filled in
  878. * @cmd: cmd we are working on
  879. *
  880. * If successful, this function returns the number of SG elements.
  881. */
  882. static int
  883. megasas_make_sgl_fusion(struct megasas_instance *instance,
  884. struct scsi_cmnd *scp,
  885. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
  886. struct megasas_cmd_fusion *cmd)
  887. {
  888. int i, sg_processed, sge_count;
  889. struct scatterlist *os_sgl;
  890. struct fusion_context *fusion;
  891. fusion = instance->ctrl_context;
  892. if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
  893. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
  894. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  895. sgl_ptr_end->Flags = 0;
  896. }
  897. sge_count = scsi_dma_map(scp);
  898. BUG_ON(sge_count < 0);
  899. if (sge_count > instance->max_num_sge || !sge_count)
  900. return sge_count;
  901. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  902. sgl_ptr->Length = sg_dma_len(os_sgl);
  903. sgl_ptr->Address = sg_dma_address(os_sgl);
  904. sgl_ptr->Flags = 0;
  905. if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
  906. if (i == sge_count - 1)
  907. sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
  908. }
  909. sgl_ptr++;
  910. sg_processed = i + 1;
  911. if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
  912. (sge_count > fusion->max_sge_in_main_msg)) {
  913. struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
  914. if (instance->pdev->device ==
  915. PCI_DEVICE_ID_LSI_INVADER) {
  916. if ((cmd->io_request->IoFlags &
  917. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
  918. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
  919. cmd->io_request->ChainOffset =
  920. fusion->
  921. chain_offset_io_request;
  922. else
  923. cmd->io_request->ChainOffset = 0;
  924. } else
  925. cmd->io_request->ChainOffset =
  926. fusion->chain_offset_io_request;
  927. sg_chain = sgl_ptr;
  928. /* Prepare chain element */
  929. sg_chain->NextChainOffset = 0;
  930. if (instance->pdev->device ==
  931. PCI_DEVICE_ID_LSI_INVADER)
  932. sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
  933. else
  934. sg_chain->Flags =
  935. (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  936. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
  937. sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
  938. *(sge_count - sg_processed));
  939. sg_chain->Address = cmd->sg_frame_phys_addr;
  940. sgl_ptr =
  941. (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
  942. }
  943. }
  944. return sge_count;
  945. }
  946. /**
  947. * megasas_set_pd_lba - Sets PD LBA
  948. * @cdb: CDB
  949. * @cdb_len: cdb length
  950. * @start_blk: Start block of IO
  951. *
  952. * Used to set the PD LBA in CDB for FP IOs
  953. */
  954. void
  955. megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
  956. struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
  957. struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
  958. {
  959. struct MR_LD_RAID *raid;
  960. u32 ld;
  961. u64 start_blk = io_info->pdBlock;
  962. u8 *cdb = io_request->CDB.CDB32;
  963. u32 num_blocks = io_info->numBlocks;
  964. u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
  965. /* Check if T10 PI (DIF) is enabled for this LD */
  966. ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
  967. raid = MR_LdRaidGet(ld, local_map_ptr);
  968. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
  969. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  970. cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
  971. cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
  972. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  973. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
  974. else
  975. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
  976. cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
  977. /* LBA */
  978. cdb[12] = (u8)((start_blk >> 56) & 0xff);
  979. cdb[13] = (u8)((start_blk >> 48) & 0xff);
  980. cdb[14] = (u8)((start_blk >> 40) & 0xff);
  981. cdb[15] = (u8)((start_blk >> 32) & 0xff);
  982. cdb[16] = (u8)((start_blk >> 24) & 0xff);
  983. cdb[17] = (u8)((start_blk >> 16) & 0xff);
  984. cdb[18] = (u8)((start_blk >> 8) & 0xff);
  985. cdb[19] = (u8)(start_blk & 0xff);
  986. /* Logical block reference tag */
  987. io_request->CDB.EEDP32.PrimaryReferenceTag =
  988. cpu_to_be32(ref_tag);
  989. io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
  990. io_request->IoFlags = 32; /* Specify 32-byte cdb */
  991. /* Transfer length */
  992. cdb[28] = (u8)((num_blocks >> 24) & 0xff);
  993. cdb[29] = (u8)((num_blocks >> 16) & 0xff);
  994. cdb[30] = (u8)((num_blocks >> 8) & 0xff);
  995. cdb[31] = (u8)(num_blocks & 0xff);
  996. /* set SCSI IO EEDPFlags */
  997. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
  998. io_request->EEDPFlags =
  999. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1000. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  1001. MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
  1002. MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
  1003. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
  1004. } else {
  1005. io_request->EEDPFlags =
  1006. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1007. MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
  1008. }
  1009. io_request->Control |= (0x4 << 26);
  1010. io_request->EEDPBlockSize = scp->device->sector_size;
  1011. } else {
  1012. /* Some drives don't support 16/12 byte CDB's, convert to 10 */
  1013. if (((cdb_len == 12) || (cdb_len == 16)) &&
  1014. (start_blk <= 0xffffffff)) {
  1015. if (cdb_len == 16) {
  1016. opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
  1017. flagvals = cdb[1];
  1018. groupnum = cdb[14];
  1019. control = cdb[15];
  1020. } else {
  1021. opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
  1022. flagvals = cdb[1];
  1023. groupnum = cdb[10];
  1024. control = cdb[11];
  1025. }
  1026. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1027. cdb[0] = opcode;
  1028. cdb[1] = flagvals;
  1029. cdb[6] = groupnum;
  1030. cdb[9] = control;
  1031. /* Transfer length */
  1032. cdb[8] = (u8)(num_blocks & 0xff);
  1033. cdb[7] = (u8)((num_blocks >> 8) & 0xff);
  1034. io_request->IoFlags = 10; /* Specify 10-byte cdb */
  1035. cdb_len = 10;
  1036. } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
  1037. /* Convert to 16 byte CDB for large LBA's */
  1038. switch (cdb_len) {
  1039. case 6:
  1040. opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
  1041. control = cdb[5];
  1042. break;
  1043. case 10:
  1044. opcode =
  1045. cdb[0] == READ_10 ? READ_16 : WRITE_16;
  1046. flagvals = cdb[1];
  1047. groupnum = cdb[6];
  1048. control = cdb[9];
  1049. break;
  1050. case 12:
  1051. opcode =
  1052. cdb[0] == READ_12 ? READ_16 : WRITE_16;
  1053. flagvals = cdb[1];
  1054. groupnum = cdb[10];
  1055. control = cdb[11];
  1056. break;
  1057. }
  1058. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1059. cdb[0] = opcode;
  1060. cdb[1] = flagvals;
  1061. cdb[14] = groupnum;
  1062. cdb[15] = control;
  1063. /* Transfer length */
  1064. cdb[13] = (u8)(num_blocks & 0xff);
  1065. cdb[12] = (u8)((num_blocks >> 8) & 0xff);
  1066. cdb[11] = (u8)((num_blocks >> 16) & 0xff);
  1067. cdb[10] = (u8)((num_blocks >> 24) & 0xff);
  1068. io_request->IoFlags = 16; /* Specify 16-byte cdb */
  1069. cdb_len = 16;
  1070. }
  1071. /* Normal case, just load LBA here */
  1072. switch (cdb_len) {
  1073. case 6:
  1074. {
  1075. u8 val = cdb[1] & 0xE0;
  1076. cdb[3] = (u8)(start_blk & 0xff);
  1077. cdb[2] = (u8)((start_blk >> 8) & 0xff);
  1078. cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
  1079. break;
  1080. }
  1081. case 10:
  1082. cdb[5] = (u8)(start_blk & 0xff);
  1083. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1084. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1085. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1086. break;
  1087. case 12:
  1088. cdb[5] = (u8)(start_blk & 0xff);
  1089. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1090. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1091. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1092. break;
  1093. case 16:
  1094. cdb[9] = (u8)(start_blk & 0xff);
  1095. cdb[8] = (u8)((start_blk >> 8) & 0xff);
  1096. cdb[7] = (u8)((start_blk >> 16) & 0xff);
  1097. cdb[6] = (u8)((start_blk >> 24) & 0xff);
  1098. cdb[5] = (u8)((start_blk >> 32) & 0xff);
  1099. cdb[4] = (u8)((start_blk >> 40) & 0xff);
  1100. cdb[3] = (u8)((start_blk >> 48) & 0xff);
  1101. cdb[2] = (u8)((start_blk >> 56) & 0xff);
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. /**
  1107. * megasas_build_ldio_fusion - Prepares IOs to devices
  1108. * @instance: Adapter soft state
  1109. * @scp: SCSI command
  1110. * @cmd: Command to be prepared
  1111. *
  1112. * Prepares the io_request and chain elements (sg_frame) for IO
  1113. * The IO can be for PD (Fast Path) or LD
  1114. */
  1115. void
  1116. megasas_build_ldio_fusion(struct megasas_instance *instance,
  1117. struct scsi_cmnd *scp,
  1118. struct megasas_cmd_fusion *cmd)
  1119. {
  1120. u8 fp_possible;
  1121. u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
  1122. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1123. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1124. struct IO_REQUEST_INFO io_info;
  1125. struct fusion_context *fusion;
  1126. struct MR_FW_RAID_MAP_ALL *local_map_ptr;
  1127. device_id = MEGASAS_DEV_INDEX(instance, scp);
  1128. fusion = instance->ctrl_context;
  1129. io_request = cmd->io_request;
  1130. io_request->RaidContext.VirtualDiskTgtId = device_id;
  1131. io_request->RaidContext.status = 0;
  1132. io_request->RaidContext.exStatus = 0;
  1133. req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
  1134. start_lba_lo = 0;
  1135. start_lba_hi = 0;
  1136. fp_possible = 0;
  1137. /*
  1138. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1139. */
  1140. if (scp->cmd_len == 6) {
  1141. datalength = (u32) scp->cmnd[4];
  1142. start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  1143. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  1144. start_lba_lo &= 0x1FFFFF;
  1145. }
  1146. /*
  1147. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1148. */
  1149. else if (scp->cmd_len == 10) {
  1150. datalength = (u32) scp->cmnd[8] |
  1151. ((u32) scp->cmnd[7] << 8);
  1152. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1153. ((u32) scp->cmnd[3] << 16) |
  1154. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1155. }
  1156. /*
  1157. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1158. */
  1159. else if (scp->cmd_len == 12) {
  1160. datalength = ((u32) scp->cmnd[6] << 24) |
  1161. ((u32) scp->cmnd[7] << 16) |
  1162. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1163. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1164. ((u32) scp->cmnd[3] << 16) |
  1165. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1166. }
  1167. /*
  1168. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1169. */
  1170. else if (scp->cmd_len == 16) {
  1171. datalength = ((u32) scp->cmnd[10] << 24) |
  1172. ((u32) scp->cmnd[11] << 16) |
  1173. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  1174. start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  1175. ((u32) scp->cmnd[7] << 16) |
  1176. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1177. start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  1178. ((u32) scp->cmnd[3] << 16) |
  1179. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1180. }
  1181. memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
  1182. io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
  1183. io_info.numBlocks = datalength;
  1184. io_info.ldTgtId = device_id;
  1185. io_request->DataLength = scsi_bufflen(scp);
  1186. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1187. io_info.isRead = 1;
  1188. local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
  1189. if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
  1190. MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
  1191. io_request->RaidContext.regLockFlags = 0;
  1192. fp_possible = 0;
  1193. } else {
  1194. if (MR_BuildRaidContext(instance, &io_info,
  1195. &io_request->RaidContext,
  1196. local_map_ptr))
  1197. fp_possible = io_info.fpOkForIo;
  1198. }
  1199. /* Use smp_processor_id() for now until cmd->request->cpu is CPU
  1200. id by default, not CPU group id, otherwise all MSI-X queues won't
  1201. be utilized */
  1202. cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
  1203. smp_processor_id() % instance->msix_vectors : 0;
  1204. if (fp_possible) {
  1205. megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
  1206. local_map_ptr, start_lba_lo);
  1207. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1208. cmd->request_desc->SCSIIO.RequestFlags =
  1209. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
  1210. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1211. if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
  1212. if (io_request->RaidContext.regLockFlags ==
  1213. REGION_TYPE_UNUSED)
  1214. cmd->request_desc->SCSIIO.RequestFlags =
  1215. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1216. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1217. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1218. io_request->RaidContext.nseg = 0x1;
  1219. io_request->IoFlags |=
  1220. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
  1221. io_request->RaidContext.regLockFlags |=
  1222. (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
  1223. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1224. }
  1225. if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
  1226. (io_info.isRead)) {
  1227. io_info.devHandle =
  1228. get_updated_dev_handle(
  1229. &fusion->load_balance_info[device_id],
  1230. &io_info);
  1231. scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
  1232. } else
  1233. scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
  1234. cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
  1235. io_request->DevHandle = io_info.devHandle;
  1236. } else {
  1237. io_request->RaidContext.timeoutValue =
  1238. local_map_ptr->raidMap.fpPdIoTimeoutSec;
  1239. cmd->request_desc->SCSIIO.RequestFlags =
  1240. (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
  1241. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1242. if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
  1243. if (io_request->RaidContext.regLockFlags ==
  1244. REGION_TYPE_UNUSED)
  1245. cmd->request_desc->SCSIIO.RequestFlags =
  1246. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1247. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1248. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1249. io_request->RaidContext.regLockFlags |=
  1250. (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
  1251. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1252. io_request->RaidContext.nseg = 0x1;
  1253. }
  1254. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1255. io_request->DevHandle = device_id;
  1256. } /* Not FP */
  1257. }
  1258. /**
  1259. * megasas_build_dcdb_fusion - Prepares IOs to devices
  1260. * @instance: Adapter soft state
  1261. * @scp: SCSI command
  1262. * @cmd: Command to be prepared
  1263. *
  1264. * Prepares the io_request frame for non-io cmds
  1265. */
  1266. static void
  1267. megasas_build_dcdb_fusion(struct megasas_instance *instance,
  1268. struct scsi_cmnd *scmd,
  1269. struct megasas_cmd_fusion *cmd)
  1270. {
  1271. u32 device_id;
  1272. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1273. u16 pd_index = 0;
  1274. struct MR_FW_RAID_MAP_ALL *local_map_ptr;
  1275. struct fusion_context *fusion = instance->ctrl_context;
  1276. io_request = cmd->io_request;
  1277. device_id = MEGASAS_DEV_INDEX(instance, scmd);
  1278. pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
  1279. +scmd->device->id;
  1280. local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
  1281. /* Check if this is a system PD I/O */
  1282. if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
  1283. instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
  1284. io_request->Function = 0;
  1285. if (fusion->fast_path_io)
  1286. io_request->DevHandle =
  1287. local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
  1288. io_request->RaidContext.timeoutValue =
  1289. local_map_ptr->raidMap.fpPdIoTimeoutSec;
  1290. io_request->RaidContext.regLockFlags = 0;
  1291. io_request->RaidContext.regLockRowLBA = 0;
  1292. io_request->RaidContext.regLockLength = 0;
  1293. io_request->RaidContext.RAIDFlags =
  1294. MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
  1295. MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
  1296. cmd->request_desc->SCSIIO.RequestFlags =
  1297. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
  1298. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1299. cmd->request_desc->SCSIIO.DevHandle =
  1300. local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
  1301. } else {
  1302. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1303. io_request->DevHandle = device_id;
  1304. cmd->request_desc->SCSIIO.RequestFlags =
  1305. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1306. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1307. }
  1308. io_request->RaidContext.VirtualDiskTgtId = device_id;
  1309. io_request->LUN[1] = scmd->device->lun;
  1310. io_request->DataLength = scsi_bufflen(scmd);
  1311. }
  1312. /**
  1313. * megasas_build_io_fusion - Prepares IOs to devices
  1314. * @instance: Adapter soft state
  1315. * @scp: SCSI command
  1316. * @cmd: Command to be prepared
  1317. *
  1318. * Invokes helper functions to prepare request frames
  1319. * and sets flags appropriate for IO/Non-IO cmd
  1320. */
  1321. int
  1322. megasas_build_io_fusion(struct megasas_instance *instance,
  1323. struct scsi_cmnd *scp,
  1324. struct megasas_cmd_fusion *cmd)
  1325. {
  1326. u32 device_id, sge_count;
  1327. struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
  1328. device_id = MEGASAS_DEV_INDEX(instance, scp);
  1329. /* Zero out some fields so they don't get reused */
  1330. io_request->LUN[1] = 0;
  1331. io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
  1332. io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
  1333. io_request->EEDPFlags = 0;
  1334. io_request->Control = 0;
  1335. io_request->EEDPBlockSize = 0;
  1336. io_request->ChainOffset = 0;
  1337. io_request->RaidContext.RAIDFlags = 0;
  1338. io_request->RaidContext.Type = 0;
  1339. io_request->RaidContext.nseg = 0;
  1340. memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
  1341. /*
  1342. * Just the CDB length,rest of the Flags are zero
  1343. * This will be modified for FP in build_ldio_fusion
  1344. */
  1345. io_request->IoFlags = scp->cmd_len;
  1346. if (megasas_is_ldio(scp))
  1347. megasas_build_ldio_fusion(instance, scp, cmd);
  1348. else
  1349. megasas_build_dcdb_fusion(instance, scp, cmd);
  1350. /*
  1351. * Construct SGL
  1352. */
  1353. sge_count =
  1354. megasas_make_sgl_fusion(instance, scp,
  1355. (struct MPI25_IEEE_SGE_CHAIN64 *)
  1356. &io_request->SGL, cmd);
  1357. if (sge_count > instance->max_num_sge) {
  1358. printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
  1359. "max (0x%x) allowed\n", sge_count,
  1360. instance->max_num_sge);
  1361. return 1;
  1362. }
  1363. io_request->RaidContext.numSGE = sge_count;
  1364. io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
  1365. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1366. io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
  1367. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1368. io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
  1369. io_request->SGLOffset0 =
  1370. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
  1371. io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
  1372. io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  1373. cmd->scmd = scp;
  1374. scp->SCp.ptr = (char *)cmd;
  1375. return 0;
  1376. }
  1377. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  1378. megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
  1379. {
  1380. u8 *p;
  1381. struct fusion_context *fusion;
  1382. if (index >= instance->max_fw_cmds) {
  1383. printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
  1384. "descriptor\n", index);
  1385. return NULL;
  1386. }
  1387. fusion = instance->ctrl_context;
  1388. p = fusion->req_frames_desc
  1389. +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
  1390. return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
  1391. }
  1392. /**
  1393. * megasas_build_and_issue_cmd_fusion -Main routine for building and
  1394. * issuing non IOCTL cmd
  1395. * @instance: Adapter soft state
  1396. * @scmd: pointer to scsi cmd from OS
  1397. */
  1398. static u32
  1399. megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
  1400. struct scsi_cmnd *scmd)
  1401. {
  1402. struct megasas_cmd_fusion *cmd;
  1403. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1404. u32 index;
  1405. struct fusion_context *fusion;
  1406. fusion = instance->ctrl_context;
  1407. cmd = megasas_get_cmd_fusion(instance);
  1408. if (!cmd)
  1409. return SCSI_MLQUEUE_HOST_BUSY;
  1410. index = cmd->index;
  1411. req_desc = megasas_get_request_descriptor(instance, index-1);
  1412. if (!req_desc)
  1413. return 1;
  1414. req_desc->Words = 0;
  1415. cmd->request_desc = req_desc;
  1416. if (megasas_build_io_fusion(instance, scmd, cmd)) {
  1417. megasas_return_cmd_fusion(instance, cmd);
  1418. printk(KERN_ERR "megasas: Error building command.\n");
  1419. cmd->request_desc = NULL;
  1420. return 1;
  1421. }
  1422. req_desc = cmd->request_desc;
  1423. req_desc->SCSIIO.SMID = index;
  1424. if (cmd->io_request->ChainOffset != 0 &&
  1425. cmd->io_request->ChainOffset != 0xF)
  1426. printk(KERN_ERR "megasas: The chain offset value is not "
  1427. "correct : %x\n", cmd->io_request->ChainOffset);
  1428. /*
  1429. * Issue the command to the FW
  1430. */
  1431. atomic_inc(&instance->fw_outstanding);
  1432. instance->instancet->fire_cmd(instance,
  1433. req_desc->u.low, req_desc->u.high,
  1434. instance->reg_set);
  1435. return 0;
  1436. }
  1437. /**
  1438. * complete_cmd_fusion - Completes command
  1439. * @instance: Adapter soft state
  1440. * Completes all commands that is in reply descriptor queue
  1441. */
  1442. int
  1443. complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
  1444. {
  1445. union MPI2_REPLY_DESCRIPTORS_UNION *desc;
  1446. struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
  1447. struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
  1448. struct fusion_context *fusion;
  1449. struct megasas_cmd *cmd_mfi;
  1450. struct megasas_cmd_fusion *cmd_fusion;
  1451. u16 smid, num_completed;
  1452. u8 reply_descript_type, arm;
  1453. u32 status, extStatus, device_id;
  1454. union desc_value d_val;
  1455. struct LD_LOAD_BALANCE_INFO *lbinfo;
  1456. fusion = instance->ctrl_context;
  1457. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  1458. return IRQ_HANDLED;
  1459. desc = fusion->reply_frames_desc;
  1460. desc += ((MSIxIndex * fusion->reply_alloc_sz)/
  1461. sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
  1462. fusion->last_reply_idx[MSIxIndex];
  1463. reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  1464. d_val.word = desc->Words;
  1465. reply_descript_type = reply_desc->ReplyFlags &
  1466. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1467. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1468. return IRQ_NONE;
  1469. num_completed = 0;
  1470. while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
  1471. smid = reply_desc->SMID;
  1472. cmd_fusion = fusion->cmd_list[smid - 1];
  1473. scsi_io_req =
  1474. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  1475. cmd_fusion->io_request;
  1476. if (cmd_fusion->scmd)
  1477. cmd_fusion->scmd->SCp.ptr = NULL;
  1478. status = scsi_io_req->RaidContext.status;
  1479. extStatus = scsi_io_req->RaidContext.exStatus;
  1480. switch (scsi_io_req->Function) {
  1481. case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
  1482. /* Update load balancing info */
  1483. device_id = MEGASAS_DEV_INDEX(instance,
  1484. cmd_fusion->scmd);
  1485. lbinfo = &fusion->load_balance_info[device_id];
  1486. if (cmd_fusion->scmd->SCp.Status &
  1487. MEGASAS_LOAD_BALANCE_FLAG) {
  1488. arm = lbinfo->raid1DevHandle[0] ==
  1489. cmd_fusion->io_request->DevHandle ? 0 :
  1490. 1;
  1491. atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
  1492. cmd_fusion->scmd->SCp.Status &=
  1493. ~MEGASAS_LOAD_BALANCE_FLAG;
  1494. }
  1495. if (reply_descript_type ==
  1496. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
  1497. if (megasas_dbg_lvl == 5)
  1498. printk(KERN_ERR "\nmegasas: FAST Path "
  1499. "IO Success\n");
  1500. }
  1501. /* Fall thru and complete IO */
  1502. case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
  1503. /* Map the FW Cmd Status */
  1504. map_cmd_status(cmd_fusion, status, extStatus);
  1505. scsi_dma_unmap(cmd_fusion->scmd);
  1506. cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
  1507. scsi_io_req->RaidContext.status = 0;
  1508. scsi_io_req->RaidContext.exStatus = 0;
  1509. megasas_return_cmd_fusion(instance, cmd_fusion);
  1510. atomic_dec(&instance->fw_outstanding);
  1511. break;
  1512. case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
  1513. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  1514. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  1515. cmd_fusion->flags = 0;
  1516. megasas_return_cmd_fusion(instance, cmd_fusion);
  1517. break;
  1518. }
  1519. fusion->last_reply_idx[MSIxIndex]++;
  1520. if (fusion->last_reply_idx[MSIxIndex] >=
  1521. fusion->reply_q_depth)
  1522. fusion->last_reply_idx[MSIxIndex] = 0;
  1523. desc->Words = ULLONG_MAX;
  1524. num_completed++;
  1525. /* Get the next reply descriptor */
  1526. if (!fusion->last_reply_idx[MSIxIndex])
  1527. desc = fusion->reply_frames_desc +
  1528. ((MSIxIndex * fusion->reply_alloc_sz)/
  1529. sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
  1530. else
  1531. desc++;
  1532. reply_desc =
  1533. (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  1534. d_val.word = desc->Words;
  1535. reply_descript_type = reply_desc->ReplyFlags &
  1536. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1537. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1538. break;
  1539. }
  1540. if (!num_completed)
  1541. return IRQ_NONE;
  1542. wmb();
  1543. writel((MSIxIndex << 24) | fusion->last_reply_idx[MSIxIndex],
  1544. &instance->reg_set->reply_post_host_index);
  1545. megasas_check_and_restore_queue_depth(instance);
  1546. return IRQ_HANDLED;
  1547. }
  1548. /**
  1549. * megasas_complete_cmd_dpc_fusion - Completes command
  1550. * @instance: Adapter soft state
  1551. *
  1552. * Tasklet to complete cmds
  1553. */
  1554. void
  1555. megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
  1556. {
  1557. struct megasas_instance *instance =
  1558. (struct megasas_instance *)instance_addr;
  1559. unsigned long flags;
  1560. u32 count, MSIxIndex;
  1561. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  1562. /* If we have already declared adapter dead, donot complete cmds */
  1563. spin_lock_irqsave(&instance->hba_lock, flags);
  1564. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1565. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1566. return;
  1567. }
  1568. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1569. for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
  1570. complete_cmd_fusion(instance, MSIxIndex);
  1571. }
  1572. /**
  1573. * megasas_isr_fusion - isr entry point
  1574. */
  1575. irqreturn_t megasas_isr_fusion(int irq, void *devp)
  1576. {
  1577. struct megasas_irq_context *irq_context = devp;
  1578. struct megasas_instance *instance = irq_context->instance;
  1579. u32 mfiStatus, fw_state;
  1580. if (!instance->msix_vectors) {
  1581. mfiStatus = instance->instancet->clear_intr(instance->reg_set);
  1582. if (!mfiStatus)
  1583. return IRQ_NONE;
  1584. }
  1585. /* If we are resetting, bail */
  1586. if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
  1587. instance->instancet->clear_intr(instance->reg_set);
  1588. return IRQ_HANDLED;
  1589. }
  1590. if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
  1591. instance->instancet->clear_intr(instance->reg_set);
  1592. /* If we didn't complete any commands, check for FW fault */
  1593. fw_state = instance->instancet->read_fw_status_reg(
  1594. instance->reg_set) & MFI_STATE_MASK;
  1595. if (fw_state == MFI_STATE_FAULT)
  1596. schedule_work(&instance->work_init);
  1597. }
  1598. return IRQ_HANDLED;
  1599. }
  1600. /**
  1601. * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
  1602. * @instance: Adapter soft state
  1603. * mfi_cmd: megasas_cmd pointer
  1604. *
  1605. */
  1606. u8
  1607. build_mpt_mfi_pass_thru(struct megasas_instance *instance,
  1608. struct megasas_cmd *mfi_cmd)
  1609. {
  1610. struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
  1611. struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
  1612. struct megasas_cmd_fusion *cmd;
  1613. struct fusion_context *fusion;
  1614. struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
  1615. cmd = megasas_get_cmd_fusion(instance);
  1616. if (!cmd)
  1617. return 1;
  1618. /* Save the smid. To be used for returning the cmd */
  1619. mfi_cmd->context.smid = cmd->index;
  1620. cmd->sync_cmd_idx = mfi_cmd->index;
  1621. /*
  1622. * For cmds where the flag is set, store the flag and check
  1623. * on completion. For cmds with this flag, don't call
  1624. * megasas_complete_cmd
  1625. */
  1626. if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
  1627. cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  1628. fusion = instance->ctrl_context;
  1629. io_req = cmd->io_request;
  1630. if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
  1631. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
  1632. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
  1633. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  1634. sgl_ptr_end->Flags = 0;
  1635. }
  1636. mpi25_ieee_chain =
  1637. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
  1638. io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
  1639. io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
  1640. SGL) / 4;
  1641. io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
  1642. mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
  1643. mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1644. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
  1645. mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
  1646. return 0;
  1647. }
  1648. /**
  1649. * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
  1650. * @instance: Adapter soft state
  1651. * @cmd: mfi cmd to build
  1652. *
  1653. */
  1654. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  1655. build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1656. {
  1657. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1658. u16 index;
  1659. if (build_mpt_mfi_pass_thru(instance, cmd)) {
  1660. printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
  1661. return NULL;
  1662. }
  1663. index = cmd->context.smid;
  1664. req_desc = megasas_get_request_descriptor(instance, index - 1);
  1665. if (!req_desc)
  1666. return NULL;
  1667. req_desc->Words = 0;
  1668. req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1669. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1670. req_desc->SCSIIO.SMID = index;
  1671. return req_desc;
  1672. }
  1673. /**
  1674. * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
  1675. * @instance: Adapter soft state
  1676. * @cmd: mfi cmd pointer
  1677. *
  1678. */
  1679. void
  1680. megasas_issue_dcmd_fusion(struct megasas_instance *instance,
  1681. struct megasas_cmd *cmd)
  1682. {
  1683. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1684. req_desc = build_mpt_cmd(instance, cmd);
  1685. if (!req_desc) {
  1686. printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
  1687. return;
  1688. }
  1689. instance->instancet->fire_cmd(instance, req_desc->u.low,
  1690. req_desc->u.high, instance->reg_set);
  1691. }
  1692. /**
  1693. * megasas_release_fusion - Reverses the FW initialization
  1694. * @intance: Adapter soft state
  1695. */
  1696. void
  1697. megasas_release_fusion(struct megasas_instance *instance)
  1698. {
  1699. megasas_free_cmds(instance);
  1700. megasas_free_cmds_fusion(instance);
  1701. iounmap(instance->reg_set);
  1702. pci_release_selected_regions(instance->pdev, instance->bar);
  1703. }
  1704. /**
  1705. * megasas_read_fw_status_reg_fusion - returns the current FW status value
  1706. * @regs: MFI register set
  1707. */
  1708. static u32
  1709. megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
  1710. {
  1711. return readl(&(regs)->outbound_scratch_pad);
  1712. }
  1713. /**
  1714. * megasas_adp_reset_fusion - For controller reset
  1715. * @regs: MFI register set
  1716. */
  1717. static int
  1718. megasas_adp_reset_fusion(struct megasas_instance *instance,
  1719. struct megasas_register_set __iomem *regs)
  1720. {
  1721. return 0;
  1722. }
  1723. /**
  1724. * megasas_check_reset_fusion - For controller reset check
  1725. * @regs: MFI register set
  1726. */
  1727. static int
  1728. megasas_check_reset_fusion(struct megasas_instance *instance,
  1729. struct megasas_register_set __iomem *regs)
  1730. {
  1731. return 0;
  1732. }
  1733. /* This function waits for outstanding commands on fusion to complete */
  1734. int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
  1735. {
  1736. int i, outstanding, retval = 0;
  1737. u32 fw_state;
  1738. for (i = 0; i < resetwaittime; i++) {
  1739. /* Check if firmware is in fault state */
  1740. fw_state = instance->instancet->read_fw_status_reg(
  1741. instance->reg_set) & MFI_STATE_MASK;
  1742. if (fw_state == MFI_STATE_FAULT) {
  1743. printk(KERN_WARNING "megasas: Found FW in FAULT state,"
  1744. " will reset adapter.\n");
  1745. retval = 1;
  1746. goto out;
  1747. }
  1748. outstanding = atomic_read(&instance->fw_outstanding);
  1749. if (!outstanding)
  1750. goto out;
  1751. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1752. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1753. "commands to complete\n", i, outstanding);
  1754. megasas_complete_cmd_dpc_fusion(
  1755. (unsigned long)instance);
  1756. }
  1757. msleep(1000);
  1758. }
  1759. if (atomic_read(&instance->fw_outstanding)) {
  1760. printk("megaraid_sas: pending commands remain after waiting, "
  1761. "will reset adapter.\n");
  1762. retval = 1;
  1763. }
  1764. out:
  1765. return retval;
  1766. }
  1767. void megasas_reset_reply_desc(struct megasas_instance *instance)
  1768. {
  1769. int i, count;
  1770. struct fusion_context *fusion;
  1771. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  1772. fusion = instance->ctrl_context;
  1773. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  1774. for (i = 0 ; i < count ; i++)
  1775. fusion->last_reply_idx[i] = 0;
  1776. reply_desc = fusion->reply_frames_desc;
  1777. for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
  1778. reply_desc->Words = ULLONG_MAX;
  1779. }
  1780. /* Core fusion reset function */
  1781. int megasas_reset_fusion(struct Scsi_Host *shost)
  1782. {
  1783. int retval = SUCCESS, i, j, retry = 0;
  1784. struct megasas_instance *instance;
  1785. struct megasas_cmd_fusion *cmd_fusion;
  1786. struct fusion_context *fusion;
  1787. struct megasas_cmd *cmd_mfi;
  1788. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1789. u32 host_diag, abs_state, status_reg, reset_adapter;
  1790. instance = (struct megasas_instance *)shost->hostdata;
  1791. fusion = instance->ctrl_context;
  1792. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1793. printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
  1794. "returning FAILED.\n");
  1795. return FAILED;
  1796. }
  1797. mutex_lock(&instance->reset_mutex);
  1798. set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  1799. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  1800. instance->instancet->disable_intr(instance->reg_set);
  1801. msleep(1000);
  1802. /* First try waiting for commands to complete */
  1803. if (megasas_wait_for_outstanding_fusion(instance)) {
  1804. printk(KERN_WARNING "megaraid_sas: resetting fusion "
  1805. "adapter.\n");
  1806. /* Now return commands back to the OS */
  1807. for (i = 0 ; i < instance->max_fw_cmds; i++) {
  1808. cmd_fusion = fusion->cmd_list[i];
  1809. if (cmd_fusion->scmd) {
  1810. scsi_dma_unmap(cmd_fusion->scmd);
  1811. cmd_fusion->scmd->result = (DID_RESET << 16);
  1812. cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
  1813. megasas_return_cmd_fusion(instance, cmd_fusion);
  1814. atomic_dec(&instance->fw_outstanding);
  1815. }
  1816. }
  1817. status_reg = instance->instancet->read_fw_status_reg(
  1818. instance->reg_set);
  1819. abs_state = status_reg & MFI_STATE_MASK;
  1820. reset_adapter = status_reg & MFI_RESET_ADAPTER;
  1821. if (instance->disableOnlineCtrlReset ||
  1822. (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
  1823. /* Reset not supported, kill adapter */
  1824. printk(KERN_WARNING "megaraid_sas: Reset not supported"
  1825. ", killing adapter.\n");
  1826. megaraid_sas_kill_hba(instance);
  1827. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1828. retval = FAILED;
  1829. goto out;
  1830. }
  1831. /* Now try to reset the chip */
  1832. for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
  1833. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
  1834. &instance->reg_set->fusion_seq_offset);
  1835. writel(MPI2_WRSEQ_1ST_KEY_VALUE,
  1836. &instance->reg_set->fusion_seq_offset);
  1837. writel(MPI2_WRSEQ_2ND_KEY_VALUE,
  1838. &instance->reg_set->fusion_seq_offset);
  1839. writel(MPI2_WRSEQ_3RD_KEY_VALUE,
  1840. &instance->reg_set->fusion_seq_offset);
  1841. writel(MPI2_WRSEQ_4TH_KEY_VALUE,
  1842. &instance->reg_set->fusion_seq_offset);
  1843. writel(MPI2_WRSEQ_5TH_KEY_VALUE,
  1844. &instance->reg_set->fusion_seq_offset);
  1845. writel(MPI2_WRSEQ_6TH_KEY_VALUE,
  1846. &instance->reg_set->fusion_seq_offset);
  1847. /* Check that the diag write enable (DRWE) bit is on */
  1848. host_diag = readl(&instance->reg_set->fusion_host_diag);
  1849. retry = 0;
  1850. while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
  1851. msleep(100);
  1852. host_diag =
  1853. readl(&instance->reg_set->fusion_host_diag);
  1854. if (retry++ == 100) {
  1855. printk(KERN_WARNING "megaraid_sas: "
  1856. "Host diag unlock failed!\n");
  1857. break;
  1858. }
  1859. }
  1860. if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
  1861. continue;
  1862. /* Send chip reset command */
  1863. writel(host_diag | HOST_DIAG_RESET_ADAPTER,
  1864. &instance->reg_set->fusion_host_diag);
  1865. msleep(3000);
  1866. /* Make sure reset adapter bit is cleared */
  1867. host_diag = readl(&instance->reg_set->fusion_host_diag);
  1868. retry = 0;
  1869. while (host_diag & HOST_DIAG_RESET_ADAPTER) {
  1870. msleep(100);
  1871. host_diag =
  1872. readl(&instance->reg_set->fusion_host_diag);
  1873. if (retry++ == 1000) {
  1874. printk(KERN_WARNING "megaraid_sas: "
  1875. "Diag reset adapter never "
  1876. "cleared!\n");
  1877. break;
  1878. }
  1879. }
  1880. if (host_diag & HOST_DIAG_RESET_ADAPTER)
  1881. continue;
  1882. abs_state =
  1883. instance->instancet->read_fw_status_reg(
  1884. instance->reg_set) & MFI_STATE_MASK;
  1885. retry = 0;
  1886. while ((abs_state <= MFI_STATE_FW_INIT) &&
  1887. (retry++ < 1000)) {
  1888. msleep(100);
  1889. abs_state =
  1890. instance->instancet->read_fw_status_reg(
  1891. instance->reg_set) & MFI_STATE_MASK;
  1892. }
  1893. if (abs_state <= MFI_STATE_FW_INIT) {
  1894. printk(KERN_WARNING "megaraid_sas: firmware "
  1895. "state < MFI_STATE_FW_INIT, state = "
  1896. "0x%x\n", abs_state);
  1897. continue;
  1898. }
  1899. /* Wait for FW to become ready */
  1900. if (megasas_transition_to_ready(instance, 1)) {
  1901. printk(KERN_WARNING "megaraid_sas: Failed to "
  1902. "transition controller to ready.\n");
  1903. continue;
  1904. }
  1905. megasas_reset_reply_desc(instance);
  1906. if (megasas_ioc_init_fusion(instance)) {
  1907. printk(KERN_WARNING "megaraid_sas: "
  1908. "megasas_ioc_init_fusion() failed!\n");
  1909. continue;
  1910. }
  1911. clear_bit(MEGASAS_FUSION_IN_RESET,
  1912. &instance->reset_flags);
  1913. instance->instancet->enable_intr(instance->reg_set);
  1914. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  1915. /* Re-fire management commands */
  1916. for (j = 0 ; j < instance->max_fw_cmds; j++) {
  1917. cmd_fusion = fusion->cmd_list[j];
  1918. if (cmd_fusion->sync_cmd_idx !=
  1919. (u32)ULONG_MAX) {
  1920. cmd_mfi =
  1921. instance->
  1922. cmd_list[cmd_fusion->sync_cmd_idx];
  1923. if (cmd_mfi->frame->dcmd.opcode ==
  1924. MR_DCMD_LD_MAP_GET_INFO) {
  1925. megasas_return_cmd(instance,
  1926. cmd_mfi);
  1927. megasas_return_cmd_fusion(
  1928. instance, cmd_fusion);
  1929. } else {
  1930. req_desc =
  1931. megasas_get_request_descriptor(
  1932. instance,
  1933. cmd_mfi->context.smid
  1934. -1);
  1935. if (!req_desc)
  1936. printk(KERN_WARNING
  1937. "req_desc NULL"
  1938. "\n");
  1939. else {
  1940. instance->instancet->
  1941. fire_cmd(instance,
  1942. req_desc->
  1943. u.low,
  1944. req_desc->
  1945. u.high,
  1946. instance->
  1947. reg_set);
  1948. }
  1949. }
  1950. }
  1951. }
  1952. /* Reset load balance info */
  1953. memset(fusion->load_balance_info, 0,
  1954. sizeof(struct LD_LOAD_BALANCE_INFO)
  1955. *MAX_LOGICAL_DRIVES);
  1956. if (!megasas_get_map_info(instance))
  1957. megasas_sync_map_info(instance);
  1958. /* Adapter reset completed successfully */
  1959. printk(KERN_WARNING "megaraid_sas: Reset "
  1960. "successful.\n");
  1961. retval = SUCCESS;
  1962. goto out;
  1963. }
  1964. /* Reset failed, kill the adapter */
  1965. printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
  1966. "adapter.\n");
  1967. megaraid_sas_kill_hba(instance);
  1968. retval = FAILED;
  1969. } else {
  1970. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  1971. instance->instancet->enable_intr(instance->reg_set);
  1972. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  1973. }
  1974. out:
  1975. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  1976. mutex_unlock(&instance->reset_mutex);
  1977. return retval;
  1978. }
  1979. /* Fusion OCR work queue */
  1980. void megasas_fusion_ocr_wq(struct work_struct *work)
  1981. {
  1982. struct megasas_instance *instance =
  1983. container_of(work, struct megasas_instance, work_init);
  1984. megasas_reset_fusion(instance->host);
  1985. }
  1986. struct megasas_instance_template megasas_instance_template_fusion = {
  1987. .fire_cmd = megasas_fire_cmd_fusion,
  1988. .enable_intr = megasas_enable_intr_fusion,
  1989. .disable_intr = megasas_disable_intr_fusion,
  1990. .clear_intr = megasas_clear_intr_fusion,
  1991. .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
  1992. .adp_reset = megasas_adp_reset_fusion,
  1993. .check_reset = megasas_check_reset_fusion,
  1994. .service_isr = megasas_isr_fusion,
  1995. .tasklet = megasas_complete_cmd_dpc_fusion,
  1996. .init_adapter = megasas_init_adapter_fusion,
  1997. .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
  1998. .issue_dcmd = megasas_issue_dcmd_fusion,
  1999. };