megaraid_sas_fusion.c 70 KB

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