megaraid_sas_fusion.c 65 KB

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