megaraid_sas_fusion.c 68 KB

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