megaraid_sas_fusion.c 68 KB

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