megaraid_sas_fusion.c 67 KB

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