megaraid_sas_fusion.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2013 LSI Corporation
  5. * Copyright (c) 2013-2014 Avago Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * FILE: megaraid_sas_fusion.c
  21. *
  22. * Authors: Avago Technologies
  23. * Sumant Patro
  24. * Adam Radford
  25. * Kashyap Desai <[email protected]>
  26. * Sumit Saxena <[email protected]>
  27. *
  28. * Send feedback to: [email protected]
  29. *
  30. * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
  31. * San Jose, California 95131
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/list.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/module.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/delay.h>
  42. #include <linux/uio.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/fs.h>
  45. #include <linux/compat.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/mutex.h>
  48. #include <linux/poll.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_cmnd.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_host.h>
  53. #include <scsi/scsi_dbg.h>
  54. #include <linux/dmi.h>
  55. #include "megaraid_sas_fusion.h"
  56. #include "megaraid_sas.h"
  57. extern void megasas_free_cmds(struct megasas_instance *instance);
  58. extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  59. *instance);
  60. extern void
  61. megasas_complete_cmd(struct megasas_instance *instance,
  62. struct megasas_cmd *cmd, u8 alt_status);
  63. int
  64. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  65. int seconds);
  66. void
  67. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
  68. int megasas_alloc_cmds(struct megasas_instance *instance);
  69. int
  70. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
  71. int
  72. megasas_issue_polled(struct megasas_instance *instance,
  73. struct megasas_cmd *cmd);
  74. void
  75. megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
  76. int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
  77. void megaraid_sas_kill_hba(struct megasas_instance *instance);
  78. extern u32 megasas_dbg_lvl;
  79. void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
  80. int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
  81. int initial);
  82. void megasas_start_timer(struct megasas_instance *instance,
  83. struct timer_list *timer,
  84. void *fn, unsigned long interval);
  85. extern struct megasas_mgmt_info megasas_mgmt_info;
  86. extern unsigned int resetwaittime;
  87. extern unsigned int dual_qdepth_disable;
  88. static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
  89. static void megasas_free_reply_fusion(struct megasas_instance *instance);
  90. /**
  91. * megasas_enable_intr_fusion - Enables interrupts
  92. * @regs: MFI register set
  93. */
  94. void
  95. megasas_enable_intr_fusion(struct megasas_instance *instance)
  96. {
  97. struct megasas_register_set __iomem *regs;
  98. regs = instance->reg_set;
  99. instance->mask_interrupts = 0;
  100. /* For Thunderbolt/Invader also clear intr on enable */
  101. writel(~0, &regs->outbound_intr_status);
  102. readl(&regs->outbound_intr_status);
  103. writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  104. /* Dummy readl to force pci flush */
  105. readl(&regs->outbound_intr_mask);
  106. }
  107. /**
  108. * megasas_disable_intr_fusion - Disables interrupt
  109. * @regs: MFI register set
  110. */
  111. void
  112. megasas_disable_intr_fusion(struct megasas_instance *instance)
  113. {
  114. u32 mask = 0xFFFFFFFF;
  115. u32 status;
  116. struct megasas_register_set __iomem *regs;
  117. regs = instance->reg_set;
  118. instance->mask_interrupts = 1;
  119. writel(mask, &regs->outbound_intr_mask);
  120. /* Dummy readl to force pci flush */
  121. status = readl(&regs->outbound_intr_mask);
  122. }
  123. int
  124. megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
  125. {
  126. u32 status;
  127. /*
  128. * Check if it is our interrupt
  129. */
  130. status = readl(&regs->outbound_intr_status);
  131. if (status & 1) {
  132. writel(status, &regs->outbound_intr_status);
  133. readl(&regs->outbound_intr_status);
  134. return 1;
  135. }
  136. if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
  137. return 0;
  138. return 1;
  139. }
  140. /**
  141. * megasas_get_cmd_fusion - Get a command from the free pool
  142. * @instance: Adapter soft state
  143. *
  144. * Returns a blk_tag indexed mpt frame
  145. */
  146. inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
  147. *instance, u32 blk_tag)
  148. {
  149. struct fusion_context *fusion;
  150. fusion = instance->ctrl_context;
  151. return fusion->cmd_list[blk_tag];
  152. }
  153. /**
  154. * megasas_return_cmd_fusion - Return a cmd to free command pool
  155. * @instance: Adapter soft state
  156. * @cmd: Command packet to be returned to free command pool
  157. */
  158. inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
  159. struct megasas_cmd_fusion *cmd)
  160. {
  161. cmd->scmd = NULL;
  162. memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
  163. }
  164. /**
  165. * megasas_fire_cmd_fusion - Sends command to the FW
  166. */
  167. static void
  168. megasas_fire_cmd_fusion(struct megasas_instance *instance,
  169. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
  170. {
  171. #if defined(writeq) && defined(CONFIG_64BIT)
  172. u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
  173. le32_to_cpu(req_desc->u.low));
  174. writeq(req_data, &instance->reg_set->inbound_low_queue_port);
  175. #else
  176. unsigned long flags;
  177. spin_lock_irqsave(&instance->hba_lock, flags);
  178. writel(le32_to_cpu(req_desc->u.low),
  179. &instance->reg_set->inbound_low_queue_port);
  180. writel(le32_to_cpu(req_desc->u.high),
  181. &instance->reg_set->inbound_high_queue_port);
  182. mmiowb();
  183. spin_unlock_irqrestore(&instance->hba_lock, flags);
  184. #endif
  185. }
  186. /**
  187. * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
  188. * @instance: Adapter soft state
  189. * fw_boot_context: Whether this function called during probe or after OCR
  190. *
  191. * This function is only for fusion controllers.
  192. * Update host can queue, if firmware downgrade max supported firmware commands.
  193. * Firmware upgrade case will be skiped because underlying firmware has
  194. * more resource than exposed to the OS.
  195. *
  196. */
  197. static void
  198. megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
  199. {
  200. u16 cur_max_fw_cmds = 0;
  201. u16 ldio_threshold = 0;
  202. struct megasas_register_set __iomem *reg_set;
  203. reg_set = instance->reg_set;
  204. cur_max_fw_cmds = readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
  205. if (dual_qdepth_disable || !cur_max_fw_cmds)
  206. cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  207. else
  208. ldio_threshold =
  209. (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
  210. dev_info(&instance->pdev->dev,
  211. "Current firmware maximum commands: %d\t LDIO threshold: %d\n",
  212. cur_max_fw_cmds, ldio_threshold);
  213. if (fw_boot_context == OCR_CONTEXT) {
  214. cur_max_fw_cmds = cur_max_fw_cmds - 1;
  215. if (cur_max_fw_cmds <= instance->max_fw_cmds) {
  216. instance->cur_can_queue =
  217. cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
  218. MEGASAS_FUSION_IOCTL_CMDS);
  219. instance->host->can_queue = instance->cur_can_queue;
  220. instance->ldio_threshold = ldio_threshold;
  221. }
  222. } else {
  223. instance->max_fw_cmds = cur_max_fw_cmds;
  224. instance->ldio_threshold = ldio_threshold;
  225. if (!instance->is_rdpq)
  226. instance->max_fw_cmds = min_t(u16, instance->max_fw_cmds, 1024);
  227. if (reset_devices)
  228. instance->max_fw_cmds = min(instance->max_fw_cmds,
  229. (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
  230. /*
  231. * Reduce the max supported cmds by 1. This is to ensure that the
  232. * reply_q_sz (1 more than the max cmd that driver may send)
  233. * does not exceed max cmds that the FW can support
  234. */
  235. instance->max_fw_cmds = instance->max_fw_cmds-1;
  236. instance->max_scsi_cmds = instance->max_fw_cmds -
  237. (MEGASAS_FUSION_INTERNAL_CMDS +
  238. MEGASAS_FUSION_IOCTL_CMDS);
  239. instance->cur_can_queue = instance->max_scsi_cmds;
  240. }
  241. }
  242. /**
  243. * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
  244. * @instance: Adapter soft state
  245. */
  246. void
  247. megasas_free_cmds_fusion(struct megasas_instance *instance)
  248. {
  249. int i;
  250. struct fusion_context *fusion = instance->ctrl_context;
  251. struct megasas_cmd_fusion *cmd;
  252. /* SG, Sense */
  253. for (i = 0; i < instance->max_fw_cmds; i++) {
  254. cmd = fusion->cmd_list[i];
  255. if (cmd) {
  256. if (cmd->sg_frame)
  257. pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
  258. cmd->sg_frame_phys_addr);
  259. if (cmd->sense)
  260. pci_pool_free(fusion->sense_dma_pool, cmd->sense,
  261. cmd->sense_phys_addr);
  262. }
  263. }
  264. if (fusion->sg_dma_pool) {
  265. pci_pool_destroy(fusion->sg_dma_pool);
  266. fusion->sg_dma_pool = NULL;
  267. }
  268. if (fusion->sense_dma_pool) {
  269. pci_pool_destroy(fusion->sense_dma_pool);
  270. fusion->sense_dma_pool = NULL;
  271. }
  272. /* Reply Frame, Desc*/
  273. if (instance->is_rdpq)
  274. megasas_free_rdpq_fusion(instance);
  275. else
  276. megasas_free_reply_fusion(instance);
  277. /* Request Frame, Desc*/
  278. if (fusion->req_frames_desc)
  279. dma_free_coherent(&instance->pdev->dev,
  280. fusion->request_alloc_sz, fusion->req_frames_desc,
  281. fusion->req_frames_desc_phys);
  282. if (fusion->io_request_frames)
  283. pci_pool_free(fusion->io_request_frames_pool,
  284. fusion->io_request_frames,
  285. fusion->io_request_frames_phys);
  286. if (fusion->io_request_frames_pool) {
  287. pci_pool_destroy(fusion->io_request_frames_pool);
  288. fusion->io_request_frames_pool = NULL;
  289. }
  290. /* cmd_list */
  291. for (i = 0; i < instance->max_fw_cmds; i++)
  292. kfree(fusion->cmd_list[i]);
  293. kfree(fusion->cmd_list);
  294. }
  295. /**
  296. * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
  297. * @instance: Adapter soft state
  298. *
  299. */
  300. static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
  301. {
  302. int i;
  303. u32 max_cmd;
  304. struct fusion_context *fusion;
  305. struct megasas_cmd_fusion *cmd;
  306. fusion = instance->ctrl_context;
  307. max_cmd = instance->max_fw_cmds;
  308. fusion->sg_dma_pool =
  309. pci_pool_create("mr_sg", instance->pdev,
  310. instance->max_chain_frame_sz, 4, 0);
  311. /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
  312. fusion->sense_dma_pool =
  313. pci_pool_create("mr_sense", instance->pdev,
  314. SCSI_SENSE_BUFFERSIZE, 64, 0);
  315. if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
  316. dev_err(&instance->pdev->dev,
  317. "Failed from %s %d\n", __func__, __LINE__);
  318. return -ENOMEM;
  319. }
  320. /*
  321. * Allocate and attach a frame to each of the commands in cmd_list
  322. */
  323. for (i = 0; i < max_cmd; i++) {
  324. cmd = fusion->cmd_list[i];
  325. cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
  326. GFP_KERNEL, &cmd->sg_frame_phys_addr);
  327. cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
  328. GFP_KERNEL, &cmd->sense_phys_addr);
  329. if (!cmd->sg_frame || !cmd->sense) {
  330. dev_err(&instance->pdev->dev,
  331. "Failed from %s %d\n", __func__, __LINE__);
  332. return -ENOMEM;
  333. }
  334. }
  335. return 0;
  336. }
  337. int
  338. megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
  339. {
  340. u32 max_cmd, i;
  341. struct fusion_context *fusion;
  342. fusion = instance->ctrl_context;
  343. max_cmd = instance->max_fw_cmds;
  344. /*
  345. * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
  346. * Allocate the dynamic array first and then allocate individual
  347. * commands.
  348. */
  349. fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *) * max_cmd,
  350. GFP_KERNEL);
  351. if (!fusion->cmd_list) {
  352. dev_err(&instance->pdev->dev,
  353. "Failed from %s %d\n", __func__, __LINE__);
  354. return -ENOMEM;
  355. }
  356. for (i = 0; i < max_cmd; i++) {
  357. fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
  358. GFP_KERNEL);
  359. if (!fusion->cmd_list[i]) {
  360. dev_err(&instance->pdev->dev,
  361. "Failed from %s %d\n", __func__, __LINE__);
  362. return -ENOMEM;
  363. }
  364. }
  365. return 0;
  366. }
  367. int
  368. megasas_alloc_request_fusion(struct megasas_instance *instance)
  369. {
  370. struct fusion_context *fusion;
  371. fusion = instance->ctrl_context;
  372. fusion->req_frames_desc =
  373. dma_alloc_coherent(&instance->pdev->dev,
  374. fusion->request_alloc_sz,
  375. &fusion->req_frames_desc_phys, GFP_KERNEL);
  376. if (!fusion->req_frames_desc) {
  377. dev_err(&instance->pdev->dev,
  378. "Failed from %s %d\n", __func__, __LINE__);
  379. return -ENOMEM;
  380. }
  381. fusion->io_request_frames_pool =
  382. pci_pool_create("mr_ioreq", instance->pdev,
  383. fusion->io_frames_alloc_sz, 16, 0);
  384. if (!fusion->io_request_frames_pool) {
  385. dev_err(&instance->pdev->dev,
  386. "Failed from %s %d\n", __func__, __LINE__);
  387. return -ENOMEM;
  388. }
  389. fusion->io_request_frames =
  390. pci_pool_alloc(fusion->io_request_frames_pool,
  391. GFP_KERNEL, &fusion->io_request_frames_phys);
  392. if (!fusion->io_request_frames) {
  393. dev_err(&instance->pdev->dev,
  394. "Failed from %s %d\n", __func__, __LINE__);
  395. return -ENOMEM;
  396. }
  397. return 0;
  398. }
  399. int
  400. megasas_alloc_reply_fusion(struct megasas_instance *instance)
  401. {
  402. int i, count;
  403. struct fusion_context *fusion;
  404. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  405. fusion = instance->ctrl_context;
  406. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  407. fusion->reply_frames_desc_pool =
  408. pci_pool_create("mr_reply", instance->pdev,
  409. fusion->reply_alloc_sz * count, 16, 0);
  410. if (!fusion->reply_frames_desc_pool) {
  411. dev_err(&instance->pdev->dev,
  412. "Failed from %s %d\n", __func__, __LINE__);
  413. return -ENOMEM;
  414. }
  415. fusion->reply_frames_desc[0] =
  416. pci_pool_alloc(fusion->reply_frames_desc_pool,
  417. GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
  418. if (!fusion->reply_frames_desc[0]) {
  419. dev_err(&instance->pdev->dev,
  420. "Failed from %s %d\n", __func__, __LINE__);
  421. return -ENOMEM;
  422. }
  423. reply_desc = fusion->reply_frames_desc[0];
  424. for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
  425. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  426. /* This is not a rdpq mode, but driver still populate
  427. * reply_frame_desc array to use same msix index in ISR path.
  428. */
  429. for (i = 0; i < (count - 1); i++)
  430. fusion->reply_frames_desc[i + 1] =
  431. fusion->reply_frames_desc[i] +
  432. (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
  433. return 0;
  434. }
  435. int
  436. megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
  437. {
  438. int i, j, count;
  439. struct fusion_context *fusion;
  440. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  441. fusion = instance->ctrl_context;
  442. fusion->rdpq_virt = pci_alloc_consistent(instance->pdev,
  443. sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
  444. &fusion->rdpq_phys);
  445. if (!fusion->rdpq_virt) {
  446. dev_err(&instance->pdev->dev,
  447. "Failed from %s %d\n", __func__, __LINE__);
  448. return -ENOMEM;
  449. }
  450. memset(fusion->rdpq_virt, 0,
  451. sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION);
  452. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  453. fusion->reply_frames_desc_pool = pci_pool_create("mr_rdpq",
  454. instance->pdev, fusion->reply_alloc_sz, 16, 0);
  455. if (!fusion->reply_frames_desc_pool) {
  456. dev_err(&instance->pdev->dev,
  457. "Failed from %s %d\n", __func__, __LINE__);
  458. return -ENOMEM;
  459. }
  460. for (i = 0; i < count; i++) {
  461. fusion->reply_frames_desc[i] =
  462. pci_pool_alloc(fusion->reply_frames_desc_pool,
  463. GFP_KERNEL, &fusion->reply_frames_desc_phys[i]);
  464. if (!fusion->reply_frames_desc[i]) {
  465. dev_err(&instance->pdev->dev,
  466. "Failed from %s %d\n", __func__, __LINE__);
  467. return -ENOMEM;
  468. }
  469. fusion->rdpq_virt[i].RDPQBaseAddress =
  470. fusion->reply_frames_desc_phys[i];
  471. reply_desc = fusion->reply_frames_desc[i];
  472. for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
  473. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  474. }
  475. return 0;
  476. }
  477. static void
  478. megasas_free_rdpq_fusion(struct megasas_instance *instance) {
  479. int i;
  480. struct fusion_context *fusion;
  481. fusion = instance->ctrl_context;
  482. for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++) {
  483. if (fusion->reply_frames_desc[i])
  484. pci_pool_free(fusion->reply_frames_desc_pool,
  485. fusion->reply_frames_desc[i],
  486. fusion->reply_frames_desc_phys[i]);
  487. }
  488. if (fusion->reply_frames_desc_pool)
  489. pci_pool_destroy(fusion->reply_frames_desc_pool);
  490. if (fusion->rdpq_virt)
  491. pci_free_consistent(instance->pdev,
  492. sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
  493. fusion->rdpq_virt, fusion->rdpq_phys);
  494. }
  495. static void
  496. megasas_free_reply_fusion(struct megasas_instance *instance) {
  497. struct fusion_context *fusion;
  498. fusion = instance->ctrl_context;
  499. if (fusion->reply_frames_desc[0])
  500. pci_pool_free(fusion->reply_frames_desc_pool,
  501. fusion->reply_frames_desc[0],
  502. fusion->reply_frames_desc_phys[0]);
  503. if (fusion->reply_frames_desc_pool)
  504. pci_pool_destroy(fusion->reply_frames_desc_pool);
  505. }
  506. /**
  507. * megasas_alloc_cmds_fusion - Allocates the command packets
  508. * @instance: Adapter soft state
  509. *
  510. *
  511. * Each frame has a 32-bit field called context. This context is used to get
  512. * back the megasas_cmd_fusion from the frame when a frame gets completed
  513. * In this driver, the 32 bit values are the indices into an array cmd_list.
  514. * This array is used only to look up the megasas_cmd_fusion given the context.
  515. * The free commands themselves are maintained in a linked list called cmd_pool.
  516. *
  517. * cmds are formed in the io_request and sg_frame members of the
  518. * megasas_cmd_fusion. The context field is used to get a request descriptor
  519. * and is used as SMID of the cmd.
  520. * SMID value range is from 1 to max_fw_cmds.
  521. */
  522. int
  523. megasas_alloc_cmds_fusion(struct megasas_instance *instance)
  524. {
  525. int i;
  526. struct fusion_context *fusion;
  527. struct megasas_cmd_fusion *cmd;
  528. u32 offset;
  529. dma_addr_t io_req_base_phys;
  530. u8 *io_req_base;
  531. fusion = instance->ctrl_context;
  532. if (megasas_alloc_cmdlist_fusion(instance))
  533. goto fail_exit;
  534. if (megasas_alloc_request_fusion(instance))
  535. goto fail_exit;
  536. if (instance->is_rdpq) {
  537. if (megasas_alloc_rdpq_fusion(instance))
  538. goto fail_exit;
  539. } else
  540. if (megasas_alloc_reply_fusion(instance))
  541. goto fail_exit;
  542. /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
  543. io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  544. io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
  545. /*
  546. * Add all the commands to command pool (fusion->cmd_pool)
  547. */
  548. /* SMID 0 is reserved. Set SMID/index from 1 */
  549. for (i = 0; i < instance->max_fw_cmds; i++) {
  550. cmd = fusion->cmd_list[i];
  551. offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
  552. memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
  553. cmd->index = i + 1;
  554. cmd->scmd = NULL;
  555. cmd->sync_cmd_idx = (i >= instance->max_scsi_cmds) ?
  556. (i - instance->max_scsi_cmds) :
  557. (u32)ULONG_MAX; /* Set to Invalid */
  558. cmd->instance = instance;
  559. cmd->io_request =
  560. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  561. (io_req_base + offset);
  562. memset(cmd->io_request, 0,
  563. sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
  564. cmd->io_request_phys_addr = io_req_base_phys + offset;
  565. }
  566. if (megasas_create_sg_sense_fusion(instance))
  567. goto fail_exit;
  568. return 0;
  569. fail_exit:
  570. megasas_free_cmds_fusion(instance);
  571. return -ENOMEM;
  572. }
  573. /**
  574. * wait_and_poll - Issues a polling command
  575. * @instance: Adapter soft state
  576. * @cmd: Command packet to be issued
  577. *
  578. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  579. */
  580. int
  581. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
  582. int seconds)
  583. {
  584. int i;
  585. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  586. struct fusion_context *fusion;
  587. u32 msecs = seconds * 1000;
  588. fusion = instance->ctrl_context;
  589. /*
  590. * Wait for cmd_status to change
  591. */
  592. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
  593. rmb();
  594. msleep(20);
  595. }
  596. if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
  597. return DCMD_TIMEOUT;
  598. else if (frame_hdr->cmd_status == MFI_STAT_OK)
  599. return DCMD_SUCCESS;
  600. else
  601. return DCMD_FAILED;
  602. }
  603. /**
  604. * megasas_ioc_init_fusion - Initializes the FW
  605. * @instance: Adapter soft state
  606. *
  607. * Issues the IOC Init cmd
  608. */
  609. int
  610. megasas_ioc_init_fusion(struct megasas_instance *instance)
  611. {
  612. struct megasas_init_frame *init_frame;
  613. struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
  614. dma_addr_t ioc_init_handle;
  615. struct megasas_cmd *cmd;
  616. u8 ret, cur_rdpq_mode;
  617. struct fusion_context *fusion;
  618. union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
  619. int i;
  620. struct megasas_header *frame_hdr;
  621. const char *sys_info;
  622. MFI_CAPABILITIES *drv_ops;
  623. u32 scratch_pad_2;
  624. fusion = instance->ctrl_context;
  625. cmd = megasas_get_cmd(instance);
  626. if (!cmd) {
  627. dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
  628. ret = 1;
  629. goto fail_get_cmd;
  630. }
  631. scratch_pad_2 = readl
  632. (&instance->reg_set->outbound_scratch_pad_2);
  633. cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
  634. if (instance->is_rdpq && !cur_rdpq_mode) {
  635. dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
  636. " from RDPQ mode to non RDPQ mode\n");
  637. ret = 1;
  638. goto fail_fw_init;
  639. }
  640. IOCInitMessage =
  641. dma_alloc_coherent(&instance->pdev->dev,
  642. sizeof(struct MPI2_IOC_INIT_REQUEST),
  643. &ioc_init_handle, GFP_KERNEL);
  644. if (!IOCInitMessage) {
  645. dev_err(&instance->pdev->dev, "Could not allocate memory for "
  646. "IOCInitMessage\n");
  647. ret = 1;
  648. goto fail_fw_init;
  649. }
  650. memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
  651. IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
  652. IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
  653. IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
  654. IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
  655. IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
  656. IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
  657. IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
  658. cpu_to_le64(fusion->rdpq_phys) :
  659. cpu_to_le64(fusion->reply_frames_desc_phys[0]);
  660. IOCInitMessage->MsgFlags = instance->is_rdpq ?
  661. MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
  662. IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
  663. IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
  664. init_frame = (struct megasas_init_frame *)cmd->frame;
  665. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  666. frame_hdr = &cmd->frame->hdr;
  667. frame_hdr->cmd_status = 0xFF;
  668. frame_hdr->flags = cpu_to_le16(
  669. le16_to_cpu(frame_hdr->flags) |
  670. MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
  671. init_frame->cmd = MFI_CMD_INIT;
  672. init_frame->cmd_status = 0xFF;
  673. drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
  674. /* driver support Extended MSIX */
  675. if (fusion->adapter_type == INVADER_SERIES)
  676. drv_ops->mfi_capabilities.support_additional_msix = 1;
  677. /* driver supports HA / Remote LUN over Fast Path interface */
  678. drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
  679. drv_ops->mfi_capabilities.support_max_255lds = 1;
  680. drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
  681. drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
  682. if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
  683. drv_ops->mfi_capabilities.support_ext_io_size = 1;
  684. drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
  685. if (!dual_qdepth_disable)
  686. drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
  687. drv_ops->mfi_capabilities.support_qd_throttling = 1;
  688. /* Convert capability to LE32 */
  689. cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
  690. sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
  691. if (instance->system_info_buf && sys_info) {
  692. memcpy(instance->system_info_buf->systemId, sys_info,
  693. strlen(sys_info) > 64 ? 64 : strlen(sys_info));
  694. instance->system_info_buf->systemIdLength =
  695. strlen(sys_info) > 64 ? 64 : strlen(sys_info);
  696. init_frame->system_info_lo = instance->system_info_h;
  697. init_frame->system_info_hi = 0;
  698. }
  699. init_frame->queue_info_new_phys_addr_hi =
  700. cpu_to_le32(upper_32_bits(ioc_init_handle));
  701. init_frame->queue_info_new_phys_addr_lo =
  702. cpu_to_le32(lower_32_bits(ioc_init_handle));
  703. init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
  704. req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
  705. req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
  706. req_desc.MFAIo.RequestFlags =
  707. (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
  708. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  709. /*
  710. * disable the intr before firing the init frame
  711. */
  712. instance->instancet->disable_intr(instance);
  713. for (i = 0; i < (10 * 1000); i += 20) {
  714. if (readl(&instance->reg_set->doorbell) & 1)
  715. msleep(20);
  716. else
  717. break;
  718. }
  719. megasas_fire_cmd_fusion(instance, &req_desc);
  720. wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
  721. frame_hdr = &cmd->frame->hdr;
  722. if (frame_hdr->cmd_status != 0) {
  723. ret = 1;
  724. goto fail_fw_init;
  725. }
  726. dev_info(&instance->pdev->dev, "Init cmd success\n");
  727. ret = 0;
  728. fail_fw_init:
  729. megasas_return_cmd(instance, cmd);
  730. if (IOCInitMessage)
  731. dma_free_coherent(&instance->pdev->dev,
  732. sizeof(struct MPI2_IOC_INIT_REQUEST),
  733. IOCInitMessage, ioc_init_handle);
  734. fail_get_cmd:
  735. return ret;
  736. }
  737. /**
  738. * megasas_sync_pd_seq_num - JBOD SEQ MAP
  739. * @instance: Adapter soft state
  740. * @pend: set to 1, if it is pended jbod map.
  741. *
  742. * Issue Jbod map to the firmware. If it is pended command,
  743. * issue command and return. If it is first instance of jbod map
  744. * issue and receive command.
  745. */
  746. int
  747. megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
  748. int ret = 0;
  749. u32 pd_seq_map_sz;
  750. struct megasas_cmd *cmd;
  751. struct megasas_dcmd_frame *dcmd;
  752. struct fusion_context *fusion = instance->ctrl_context;
  753. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  754. dma_addr_t pd_seq_h;
  755. pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
  756. pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
  757. pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
  758. (sizeof(struct MR_PD_CFG_SEQ) *
  759. (MAX_PHYSICAL_DEVICES - 1));
  760. cmd = megasas_get_cmd(instance);
  761. if (!cmd) {
  762. dev_err(&instance->pdev->dev,
  763. "Could not get mfi cmd. Fail from %s %d\n",
  764. __func__, __LINE__);
  765. return -ENOMEM;
  766. }
  767. dcmd = &cmd->frame->dcmd;
  768. memset(pd_sync, 0, pd_seq_map_sz);
  769. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  770. dcmd->cmd = MFI_CMD_DCMD;
  771. dcmd->cmd_status = 0xFF;
  772. dcmd->sge_count = 1;
  773. dcmd->timeout = 0;
  774. dcmd->pad_0 = 0;
  775. dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
  776. dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
  777. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
  778. dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
  779. if (pend) {
  780. dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  781. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
  782. instance->jbod_seq_cmd = cmd;
  783. instance->instancet->issue_dcmd(instance, cmd);
  784. return 0;
  785. }
  786. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  787. /* Below code is only for non pended DCMD */
  788. if (instance->ctrl_context && !instance->mask_interrupts)
  789. ret = megasas_issue_blocked_cmd(instance, cmd,
  790. MFI_IO_TIMEOUT_SECS);
  791. else
  792. ret = megasas_issue_polled(instance, cmd);
  793. if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
  794. dev_warn(&instance->pdev->dev,
  795. "driver supports max %d JBOD, but FW reports %d\n",
  796. MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
  797. ret = -EINVAL;
  798. }
  799. if (ret == DCMD_TIMEOUT && instance->ctrl_context)
  800. megaraid_sas_kill_hba(instance);
  801. if (ret == DCMD_SUCCESS)
  802. instance->pd_seq_map_id++;
  803. megasas_return_cmd(instance, cmd);
  804. return ret;
  805. }
  806. /*
  807. * megasas_get_ld_map_info - Returns FW's ld_map structure
  808. * @instance: Adapter soft state
  809. * @pend: Pend the command or not
  810. * Issues an internal command (DCMD) to get the FW's controller PD
  811. * list structure. This information is mainly used to find out SYSTEM
  812. * supported by the FW.
  813. * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
  814. * dcmd.mbox.b[0] - number of LDs being sync'd
  815. * dcmd.mbox.b[1] - 0 - complete command immediately.
  816. * - 1 - pend till config change
  817. * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
  818. * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
  819. * uses extended struct MR_FW_RAID_MAP_EXT
  820. */
  821. static int
  822. megasas_get_ld_map_info(struct megasas_instance *instance)
  823. {
  824. int ret = 0;
  825. struct megasas_cmd *cmd;
  826. struct megasas_dcmd_frame *dcmd;
  827. void *ci;
  828. dma_addr_t ci_h = 0;
  829. u32 size_map_info;
  830. struct fusion_context *fusion;
  831. cmd = megasas_get_cmd(instance);
  832. if (!cmd) {
  833. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
  834. return -ENOMEM;
  835. }
  836. fusion = instance->ctrl_context;
  837. if (!fusion) {
  838. megasas_return_cmd(instance, cmd);
  839. return -ENXIO;
  840. }
  841. dcmd = &cmd->frame->dcmd;
  842. size_map_info = fusion->current_map_sz;
  843. ci = (void *) fusion->ld_map[(instance->map_id & 1)];
  844. ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
  845. if (!ci) {
  846. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
  847. megasas_return_cmd(instance, cmd);
  848. return -ENOMEM;
  849. }
  850. memset(ci, 0, fusion->max_map_sz);
  851. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  852. #if VD_EXT_DEBUG
  853. dev_dbg(&instance->pdev->dev,
  854. "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
  855. __func__, cpu_to_le32(size_map_info));
  856. #endif
  857. dcmd->cmd = MFI_CMD_DCMD;
  858. dcmd->cmd_status = 0xFF;
  859. dcmd->sge_count = 1;
  860. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
  861. dcmd->timeout = 0;
  862. dcmd->pad_0 = 0;
  863. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  864. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  865. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  866. dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
  867. if (instance->ctrl_context && !instance->mask_interrupts)
  868. ret = megasas_issue_blocked_cmd(instance, cmd,
  869. MFI_IO_TIMEOUT_SECS);
  870. else
  871. ret = megasas_issue_polled(instance, cmd);
  872. if (ret == DCMD_TIMEOUT && instance->ctrl_context)
  873. megaraid_sas_kill_hba(instance);
  874. megasas_return_cmd(instance, cmd);
  875. return ret;
  876. }
  877. u8
  878. megasas_get_map_info(struct megasas_instance *instance)
  879. {
  880. struct fusion_context *fusion = instance->ctrl_context;
  881. fusion->fast_path_io = 0;
  882. if (!megasas_get_ld_map_info(instance)) {
  883. if (MR_ValidateMapInfo(instance)) {
  884. fusion->fast_path_io = 1;
  885. return 0;
  886. }
  887. }
  888. return 1;
  889. }
  890. /*
  891. * megasas_sync_map_info - Returns FW's ld_map structure
  892. * @instance: Adapter soft state
  893. *
  894. * Issues an internal command (DCMD) to get the FW's controller PD
  895. * list structure. This information is mainly used to find out SYSTEM
  896. * supported by the FW.
  897. */
  898. int
  899. megasas_sync_map_info(struct megasas_instance *instance)
  900. {
  901. int ret = 0, i;
  902. struct megasas_cmd *cmd;
  903. struct megasas_dcmd_frame *dcmd;
  904. u32 size_sync_info, num_lds;
  905. struct fusion_context *fusion;
  906. struct MR_LD_TARGET_SYNC *ci = NULL;
  907. struct MR_DRV_RAID_MAP_ALL *map;
  908. struct MR_LD_RAID *raid;
  909. struct MR_LD_TARGET_SYNC *ld_sync;
  910. dma_addr_t ci_h = 0;
  911. u32 size_map_info;
  912. cmd = megasas_get_cmd(instance);
  913. if (!cmd) {
  914. dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
  915. return -ENOMEM;
  916. }
  917. fusion = instance->ctrl_context;
  918. if (!fusion) {
  919. megasas_return_cmd(instance, cmd);
  920. return 1;
  921. }
  922. map = fusion->ld_drv_map[instance->map_id & 1];
  923. num_lds = le16_to_cpu(map->raidMap.ldCount);
  924. dcmd = &cmd->frame->dcmd;
  925. size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
  926. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  927. ci = (struct MR_LD_TARGET_SYNC *)
  928. fusion->ld_map[(instance->map_id - 1) & 1];
  929. memset(ci, 0, fusion->max_map_sz);
  930. ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
  931. ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
  932. for (i = 0; i < num_lds; i++, ld_sync++) {
  933. raid = MR_LdRaidGet(i, map);
  934. ld_sync->targetId = MR_GetLDTgtId(i, map);
  935. ld_sync->seqNum = raid->seqNum;
  936. }
  937. size_map_info = fusion->current_map_sz;
  938. dcmd->cmd = MFI_CMD_DCMD;
  939. dcmd->cmd_status = 0xFF;
  940. dcmd->sge_count = 1;
  941. dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
  942. dcmd->timeout = 0;
  943. dcmd->pad_0 = 0;
  944. dcmd->data_xfer_len = cpu_to_le32(size_map_info);
  945. dcmd->mbox.b[0] = num_lds;
  946. dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
  947. dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
  948. dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
  949. dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
  950. instance->map_update_cmd = cmd;
  951. instance->instancet->issue_dcmd(instance, cmd);
  952. return ret;
  953. }
  954. /*
  955. * meagasas_display_intel_branding - Display branding string
  956. * @instance: per adapter object
  957. *
  958. * Return nothing.
  959. */
  960. static void
  961. megasas_display_intel_branding(struct megasas_instance *instance)
  962. {
  963. if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
  964. return;
  965. switch (instance->pdev->device) {
  966. case PCI_DEVICE_ID_LSI_INVADER:
  967. switch (instance->pdev->subsystem_device) {
  968. case MEGARAID_INTEL_RS3DC080_SSDID:
  969. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  970. instance->host->host_no,
  971. MEGARAID_INTEL_RS3DC080_BRANDING);
  972. break;
  973. case MEGARAID_INTEL_RS3DC040_SSDID:
  974. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  975. instance->host->host_no,
  976. MEGARAID_INTEL_RS3DC040_BRANDING);
  977. break;
  978. case MEGARAID_INTEL_RS3SC008_SSDID:
  979. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  980. instance->host->host_no,
  981. MEGARAID_INTEL_RS3SC008_BRANDING);
  982. break;
  983. case MEGARAID_INTEL_RS3MC044_SSDID:
  984. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  985. instance->host->host_no,
  986. MEGARAID_INTEL_RS3MC044_BRANDING);
  987. break;
  988. default:
  989. break;
  990. }
  991. break;
  992. case PCI_DEVICE_ID_LSI_FURY:
  993. switch (instance->pdev->subsystem_device) {
  994. case MEGARAID_INTEL_RS3WC080_SSDID:
  995. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  996. instance->host->host_no,
  997. MEGARAID_INTEL_RS3WC080_BRANDING);
  998. break;
  999. case MEGARAID_INTEL_RS3WC040_SSDID:
  1000. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1001. instance->host->host_no,
  1002. MEGARAID_INTEL_RS3WC040_BRANDING);
  1003. break;
  1004. default:
  1005. break;
  1006. }
  1007. break;
  1008. case PCI_DEVICE_ID_LSI_CUTLASS_52:
  1009. case PCI_DEVICE_ID_LSI_CUTLASS_53:
  1010. switch (instance->pdev->subsystem_device) {
  1011. case MEGARAID_INTEL_RMS3BC160_SSDID:
  1012. dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
  1013. instance->host->host_no,
  1014. MEGARAID_INTEL_RMS3BC160_BRANDING);
  1015. break;
  1016. default:
  1017. break;
  1018. }
  1019. break;
  1020. default:
  1021. break;
  1022. }
  1023. }
  1024. /**
  1025. * megasas_init_adapter_fusion - Initializes the FW
  1026. * @instance: Adapter soft state
  1027. *
  1028. * This is the main function for initializing firmware.
  1029. */
  1030. u32
  1031. megasas_init_adapter_fusion(struct megasas_instance *instance)
  1032. {
  1033. struct megasas_register_set __iomem *reg_set;
  1034. struct fusion_context *fusion;
  1035. u32 max_cmd, scratch_pad_2;
  1036. int i = 0, count;
  1037. fusion = instance->ctrl_context;
  1038. reg_set = instance->reg_set;
  1039. megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
  1040. /*
  1041. * Reduce the max supported cmds by 1. This is to ensure that the
  1042. * reply_q_sz (1 more than the max cmd that driver may send)
  1043. * does not exceed max cmds that the FW can support
  1044. */
  1045. instance->max_fw_cmds = instance->max_fw_cmds-1;
  1046. /*
  1047. * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
  1048. */
  1049. instance->max_mfi_cmds =
  1050. MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
  1051. max_cmd = instance->max_fw_cmds;
  1052. fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
  1053. fusion->request_alloc_sz =
  1054. sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
  1055. fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
  1056. *(fusion->reply_q_depth);
  1057. fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
  1058. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
  1059. (max_cmd + 1)); /* Extra 1 for SMID 0 */
  1060. scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
  1061. /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
  1062. * Firmware support extended IO chain frame which is 4 times more than
  1063. * legacy Firmware.
  1064. * Legacy Firmware - Frame size is (8 * 128) = 1K
  1065. * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
  1066. */
  1067. if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
  1068. instance->max_chain_frame_sz =
  1069. ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
  1070. MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
  1071. else
  1072. instance->max_chain_frame_sz =
  1073. ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
  1074. MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
  1075. if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
  1076. dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
  1077. instance->max_chain_frame_sz,
  1078. MEGASAS_CHAIN_FRAME_SZ_MIN);
  1079. instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
  1080. }
  1081. fusion->max_sge_in_main_msg =
  1082. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
  1083. - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
  1084. fusion->max_sge_in_chain =
  1085. instance->max_chain_frame_sz
  1086. / sizeof(union MPI2_SGE_IO_UNION);
  1087. instance->max_num_sge =
  1088. rounddown_pow_of_two(fusion->max_sge_in_main_msg
  1089. + fusion->max_sge_in_chain - 2);
  1090. /* Used for pass thru MFI frame (DCMD) */
  1091. fusion->chain_offset_mfi_pthru =
  1092. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
  1093. fusion->chain_offset_io_request =
  1094. (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
  1095. sizeof(union MPI2_SGE_IO_UNION))/16;
  1096. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  1097. for (i = 0 ; i < count; i++)
  1098. fusion->last_reply_idx[i] = 0;
  1099. /*
  1100. * For fusion adapters, 3 commands for IOCTL and 5 commands
  1101. * for driver's internal DCMDs.
  1102. */
  1103. instance->max_scsi_cmds = instance->max_fw_cmds -
  1104. (MEGASAS_FUSION_INTERNAL_CMDS +
  1105. MEGASAS_FUSION_IOCTL_CMDS);
  1106. sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
  1107. /*
  1108. * Allocate memory for descriptors
  1109. * Create a pool of commands
  1110. */
  1111. if (megasas_alloc_cmds(instance))
  1112. goto fail_alloc_mfi_cmds;
  1113. if (megasas_alloc_cmds_fusion(instance))
  1114. goto fail_alloc_cmds;
  1115. if (megasas_ioc_init_fusion(instance))
  1116. goto fail_ioc_init;
  1117. megasas_display_intel_branding(instance);
  1118. if (megasas_get_ctrl_info(instance)) {
  1119. dev_err(&instance->pdev->dev,
  1120. "Could not get controller info. Fail from %s %d\n",
  1121. __func__, __LINE__);
  1122. goto fail_ioc_init;
  1123. }
  1124. instance->flag_ieee = 1;
  1125. fusion->fast_path_io = 0;
  1126. fusion->drv_map_pages = get_order(fusion->drv_map_sz);
  1127. for (i = 0; i < 2; i++) {
  1128. fusion->ld_map[i] = NULL;
  1129. fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
  1130. fusion->drv_map_pages);
  1131. if (!fusion->ld_drv_map[i]) {
  1132. dev_err(&instance->pdev->dev, "Could not allocate "
  1133. "memory for local map info for %d pages\n",
  1134. fusion->drv_map_pages);
  1135. if (i == 1)
  1136. free_pages((ulong)fusion->ld_drv_map[0],
  1137. fusion->drv_map_pages);
  1138. goto fail_ioc_init;
  1139. }
  1140. memset(fusion->ld_drv_map[i], 0,
  1141. ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
  1142. }
  1143. for (i = 0; i < 2; i++) {
  1144. fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
  1145. fusion->max_map_sz,
  1146. &fusion->ld_map_phys[i],
  1147. GFP_KERNEL);
  1148. if (!fusion->ld_map[i]) {
  1149. dev_err(&instance->pdev->dev, "Could not allocate memory "
  1150. "for map info\n");
  1151. goto fail_map_info;
  1152. }
  1153. }
  1154. if (!megasas_get_map_info(instance))
  1155. megasas_sync_map_info(instance);
  1156. return 0;
  1157. fail_map_info:
  1158. if (i == 1)
  1159. dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
  1160. fusion->ld_map[0], fusion->ld_map_phys[0]);
  1161. fail_ioc_init:
  1162. megasas_free_cmds_fusion(instance);
  1163. fail_alloc_cmds:
  1164. megasas_free_cmds(instance);
  1165. fail_alloc_mfi_cmds:
  1166. return 1;
  1167. }
  1168. /**
  1169. * map_cmd_status - Maps FW cmd status to OS cmd status
  1170. * @cmd : Pointer to cmd
  1171. * @status : status of cmd returned by FW
  1172. * @ext_status : ext status of cmd returned by FW
  1173. */
  1174. void
  1175. map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
  1176. {
  1177. switch (status) {
  1178. case MFI_STAT_OK:
  1179. cmd->scmd->result = DID_OK << 16;
  1180. break;
  1181. case MFI_STAT_SCSI_IO_FAILED:
  1182. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1183. cmd->scmd->result = (DID_ERROR << 16) | ext_status;
  1184. break;
  1185. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1186. cmd->scmd->result = (DID_OK << 16) | ext_status;
  1187. if (ext_status == SAM_STAT_CHECK_CONDITION) {
  1188. memset(cmd->scmd->sense_buffer, 0,
  1189. SCSI_SENSE_BUFFERSIZE);
  1190. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1191. SCSI_SENSE_BUFFERSIZE);
  1192. cmd->scmd->result |= DRIVER_SENSE << 24;
  1193. }
  1194. break;
  1195. case MFI_STAT_LD_OFFLINE:
  1196. case MFI_STAT_DEVICE_NOT_FOUND:
  1197. cmd->scmd->result = DID_BAD_TARGET << 16;
  1198. break;
  1199. case MFI_STAT_CONFIG_SEQ_MISMATCH:
  1200. cmd->scmd->result = DID_IMM_RETRY << 16;
  1201. break;
  1202. default:
  1203. dev_printk(KERN_DEBUG, &cmd->instance->pdev->dev, "FW status %#x\n", status);
  1204. cmd->scmd->result = DID_ERROR << 16;
  1205. break;
  1206. }
  1207. }
  1208. /**
  1209. * megasas_make_sgl_fusion - Prepares 32-bit SGL
  1210. * @instance: Adapter soft state
  1211. * @scp: SCSI command from the mid-layer
  1212. * @sgl_ptr: SGL to be filled in
  1213. * @cmd: cmd we are working on
  1214. *
  1215. * If successful, this function returns the number of SG elements.
  1216. */
  1217. static int
  1218. megasas_make_sgl_fusion(struct megasas_instance *instance,
  1219. struct scsi_cmnd *scp,
  1220. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
  1221. struct megasas_cmd_fusion *cmd)
  1222. {
  1223. int i, sg_processed, sge_count;
  1224. struct scatterlist *os_sgl;
  1225. struct fusion_context *fusion;
  1226. fusion = instance->ctrl_context;
  1227. if (fusion->adapter_type == INVADER_SERIES) {
  1228. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
  1229. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  1230. sgl_ptr_end->Flags = 0;
  1231. }
  1232. sge_count = scsi_dma_map(scp);
  1233. BUG_ON(sge_count < 0);
  1234. if (sge_count > instance->max_num_sge || !sge_count)
  1235. return sge_count;
  1236. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  1237. sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
  1238. sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
  1239. sgl_ptr->Flags = 0;
  1240. if (fusion->adapter_type == INVADER_SERIES)
  1241. if (i == sge_count - 1)
  1242. sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
  1243. sgl_ptr++;
  1244. sg_processed = i + 1;
  1245. if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
  1246. (sge_count > fusion->max_sge_in_main_msg)) {
  1247. struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
  1248. if (fusion->adapter_type == INVADER_SERIES) {
  1249. if ((le16_to_cpu(cmd->io_request->IoFlags) &
  1250. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
  1251. MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
  1252. cmd->io_request->ChainOffset =
  1253. fusion->
  1254. chain_offset_io_request;
  1255. else
  1256. cmd->io_request->ChainOffset = 0;
  1257. } else
  1258. cmd->io_request->ChainOffset =
  1259. fusion->chain_offset_io_request;
  1260. sg_chain = sgl_ptr;
  1261. /* Prepare chain element */
  1262. sg_chain->NextChainOffset = 0;
  1263. if (fusion->adapter_type == INVADER_SERIES)
  1264. sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
  1265. else
  1266. sg_chain->Flags =
  1267. (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  1268. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
  1269. sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
  1270. sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
  1271. sgl_ptr =
  1272. (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
  1273. memset(sgl_ptr, 0, instance->max_chain_frame_sz);
  1274. }
  1275. }
  1276. return sge_count;
  1277. }
  1278. /**
  1279. * megasas_set_pd_lba - Sets PD LBA
  1280. * @cdb: CDB
  1281. * @cdb_len: cdb length
  1282. * @start_blk: Start block of IO
  1283. *
  1284. * Used to set the PD LBA in CDB for FP IOs
  1285. */
  1286. void
  1287. megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
  1288. struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
  1289. struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
  1290. {
  1291. struct MR_LD_RAID *raid;
  1292. u32 ld;
  1293. u64 start_blk = io_info->pdBlock;
  1294. u8 *cdb = io_request->CDB.CDB32;
  1295. u32 num_blocks = io_info->numBlocks;
  1296. u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
  1297. /* Check if T10 PI (DIF) is enabled for this LD */
  1298. ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
  1299. raid = MR_LdRaidGet(ld, local_map_ptr);
  1300. if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
  1301. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1302. cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
  1303. cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
  1304. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1305. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
  1306. else
  1307. cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
  1308. cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
  1309. /* LBA */
  1310. cdb[12] = (u8)((start_blk >> 56) & 0xff);
  1311. cdb[13] = (u8)((start_blk >> 48) & 0xff);
  1312. cdb[14] = (u8)((start_blk >> 40) & 0xff);
  1313. cdb[15] = (u8)((start_blk >> 32) & 0xff);
  1314. cdb[16] = (u8)((start_blk >> 24) & 0xff);
  1315. cdb[17] = (u8)((start_blk >> 16) & 0xff);
  1316. cdb[18] = (u8)((start_blk >> 8) & 0xff);
  1317. cdb[19] = (u8)(start_blk & 0xff);
  1318. /* Logical block reference tag */
  1319. io_request->CDB.EEDP32.PrimaryReferenceTag =
  1320. cpu_to_be32(ref_tag);
  1321. io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
  1322. io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
  1323. /* Transfer length */
  1324. cdb[28] = (u8)((num_blocks >> 24) & 0xff);
  1325. cdb[29] = (u8)((num_blocks >> 16) & 0xff);
  1326. cdb[30] = (u8)((num_blocks >> 8) & 0xff);
  1327. cdb[31] = (u8)(num_blocks & 0xff);
  1328. /* set SCSI IO EEDPFlags */
  1329. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
  1330. io_request->EEDPFlags = cpu_to_le16(
  1331. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1332. MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
  1333. MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
  1334. MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
  1335. MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
  1336. } else {
  1337. io_request->EEDPFlags = cpu_to_le16(
  1338. MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
  1339. MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
  1340. }
  1341. io_request->Control |= cpu_to_le32((0x4 << 26));
  1342. io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
  1343. } else {
  1344. /* Some drives don't support 16/12 byte CDB's, convert to 10 */
  1345. if (((cdb_len == 12) || (cdb_len == 16)) &&
  1346. (start_blk <= 0xffffffff)) {
  1347. if (cdb_len == 16) {
  1348. opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
  1349. flagvals = cdb[1];
  1350. groupnum = cdb[14];
  1351. control = cdb[15];
  1352. } else {
  1353. opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
  1354. flagvals = cdb[1];
  1355. groupnum = cdb[10];
  1356. control = cdb[11];
  1357. }
  1358. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1359. cdb[0] = opcode;
  1360. cdb[1] = flagvals;
  1361. cdb[6] = groupnum;
  1362. cdb[9] = control;
  1363. /* Transfer length */
  1364. cdb[8] = (u8)(num_blocks & 0xff);
  1365. cdb[7] = (u8)((num_blocks >> 8) & 0xff);
  1366. io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
  1367. cdb_len = 10;
  1368. } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
  1369. /* Convert to 16 byte CDB for large LBA's */
  1370. switch (cdb_len) {
  1371. case 6:
  1372. opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
  1373. control = cdb[5];
  1374. break;
  1375. case 10:
  1376. opcode =
  1377. cdb[0] == READ_10 ? READ_16 : WRITE_16;
  1378. flagvals = cdb[1];
  1379. groupnum = cdb[6];
  1380. control = cdb[9];
  1381. break;
  1382. case 12:
  1383. opcode =
  1384. cdb[0] == READ_12 ? READ_16 : WRITE_16;
  1385. flagvals = cdb[1];
  1386. groupnum = cdb[10];
  1387. control = cdb[11];
  1388. break;
  1389. }
  1390. memset(cdb, 0, sizeof(io_request->CDB.CDB32));
  1391. cdb[0] = opcode;
  1392. cdb[1] = flagvals;
  1393. cdb[14] = groupnum;
  1394. cdb[15] = control;
  1395. /* Transfer length */
  1396. cdb[13] = (u8)(num_blocks & 0xff);
  1397. cdb[12] = (u8)((num_blocks >> 8) & 0xff);
  1398. cdb[11] = (u8)((num_blocks >> 16) & 0xff);
  1399. cdb[10] = (u8)((num_blocks >> 24) & 0xff);
  1400. io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
  1401. cdb_len = 16;
  1402. }
  1403. /* Normal case, just load LBA here */
  1404. switch (cdb_len) {
  1405. case 6:
  1406. {
  1407. u8 val = cdb[1] & 0xE0;
  1408. cdb[3] = (u8)(start_blk & 0xff);
  1409. cdb[2] = (u8)((start_blk >> 8) & 0xff);
  1410. cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
  1411. break;
  1412. }
  1413. case 10:
  1414. cdb[5] = (u8)(start_blk & 0xff);
  1415. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1416. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1417. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1418. break;
  1419. case 12:
  1420. cdb[5] = (u8)(start_blk & 0xff);
  1421. cdb[4] = (u8)((start_blk >> 8) & 0xff);
  1422. cdb[3] = (u8)((start_blk >> 16) & 0xff);
  1423. cdb[2] = (u8)((start_blk >> 24) & 0xff);
  1424. break;
  1425. case 16:
  1426. cdb[9] = (u8)(start_blk & 0xff);
  1427. cdb[8] = (u8)((start_blk >> 8) & 0xff);
  1428. cdb[7] = (u8)((start_blk >> 16) & 0xff);
  1429. cdb[6] = (u8)((start_blk >> 24) & 0xff);
  1430. cdb[5] = (u8)((start_blk >> 32) & 0xff);
  1431. cdb[4] = (u8)((start_blk >> 40) & 0xff);
  1432. cdb[3] = (u8)((start_blk >> 48) & 0xff);
  1433. cdb[2] = (u8)((start_blk >> 56) & 0xff);
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. /**
  1439. * megasas_build_ldio_fusion - Prepares IOs to devices
  1440. * @instance: Adapter soft state
  1441. * @scp: SCSI command
  1442. * @cmd: Command to be prepared
  1443. *
  1444. * Prepares the io_request and chain elements (sg_frame) for IO
  1445. * The IO can be for PD (Fast Path) or LD
  1446. */
  1447. void
  1448. megasas_build_ldio_fusion(struct megasas_instance *instance,
  1449. struct scsi_cmnd *scp,
  1450. struct megasas_cmd_fusion *cmd)
  1451. {
  1452. u8 fp_possible;
  1453. u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
  1454. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1455. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1456. struct IO_REQUEST_INFO io_info;
  1457. struct fusion_context *fusion;
  1458. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1459. u8 *raidLUN;
  1460. device_id = MEGASAS_DEV_INDEX(scp);
  1461. fusion = instance->ctrl_context;
  1462. io_request = cmd->io_request;
  1463. io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
  1464. io_request->RaidContext.status = 0;
  1465. io_request->RaidContext.exStatus = 0;
  1466. req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
  1467. start_lba_lo = 0;
  1468. start_lba_hi = 0;
  1469. fp_possible = 0;
  1470. /*
  1471. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1472. */
  1473. if (scp->cmd_len == 6) {
  1474. datalength = (u32) scp->cmnd[4];
  1475. start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  1476. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  1477. start_lba_lo &= 0x1FFFFF;
  1478. }
  1479. /*
  1480. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1481. */
  1482. else if (scp->cmd_len == 10) {
  1483. datalength = (u32) scp->cmnd[8] |
  1484. ((u32) scp->cmnd[7] << 8);
  1485. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1486. ((u32) scp->cmnd[3] << 16) |
  1487. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1488. }
  1489. /*
  1490. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1491. */
  1492. else if (scp->cmd_len == 12) {
  1493. datalength = ((u32) scp->cmnd[6] << 24) |
  1494. ((u32) scp->cmnd[7] << 16) |
  1495. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1496. start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1497. ((u32) scp->cmnd[3] << 16) |
  1498. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1499. }
  1500. /*
  1501. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1502. */
  1503. else if (scp->cmd_len == 16) {
  1504. datalength = ((u32) scp->cmnd[10] << 24) |
  1505. ((u32) scp->cmnd[11] << 16) |
  1506. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  1507. start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  1508. ((u32) scp->cmnd[7] << 16) |
  1509. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1510. start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  1511. ((u32) scp->cmnd[3] << 16) |
  1512. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1513. }
  1514. memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
  1515. io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
  1516. io_info.numBlocks = datalength;
  1517. io_info.ldTgtId = device_id;
  1518. io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
  1519. if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1520. io_info.isRead = 1;
  1521. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1522. if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
  1523. instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
  1524. io_request->RaidContext.regLockFlags = 0;
  1525. fp_possible = 0;
  1526. } else {
  1527. if (MR_BuildRaidContext(instance, &io_info,
  1528. &io_request->RaidContext,
  1529. local_map_ptr, &raidLUN))
  1530. fp_possible = io_info.fpOkForIo;
  1531. }
  1532. /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
  1533. id by default, not CPU group id, otherwise all MSI-X queues won't
  1534. be utilized */
  1535. cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
  1536. raw_smp_processor_id() % instance->msix_vectors : 0;
  1537. if (fp_possible) {
  1538. megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
  1539. local_map_ptr, start_lba_lo);
  1540. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1541. cmd->request_desc->SCSIIO.RequestFlags =
  1542. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
  1543. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1544. if (fusion->adapter_type == INVADER_SERIES) {
  1545. if (io_request->RaidContext.regLockFlags ==
  1546. REGION_TYPE_UNUSED)
  1547. cmd->request_desc->SCSIIO.RequestFlags =
  1548. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1549. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1550. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1551. io_request->RaidContext.nseg = 0x1;
  1552. io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  1553. io_request->RaidContext.regLockFlags |=
  1554. (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
  1555. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1556. }
  1557. if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
  1558. (io_info.isRead)) {
  1559. io_info.devHandle =
  1560. get_updated_dev_handle(instance,
  1561. &fusion->load_balance_info[device_id],
  1562. &io_info);
  1563. scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
  1564. cmd->pd_r1_lb = io_info.pd_after_lb;
  1565. } else
  1566. scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
  1567. if ((raidLUN[0] == 1) &&
  1568. (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
  1569. instance->dev_handle = !(instance->dev_handle);
  1570. io_info.devHandle =
  1571. local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
  1572. }
  1573. cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
  1574. io_request->DevHandle = io_info.devHandle;
  1575. /* populate the LUN field */
  1576. memcpy(io_request->LUN, raidLUN, 8);
  1577. } else {
  1578. io_request->RaidContext.timeoutValue =
  1579. cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
  1580. cmd->request_desc->SCSIIO.RequestFlags =
  1581. (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
  1582. << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1583. if (fusion->adapter_type == INVADER_SERIES) {
  1584. if (io_info.do_fp_rlbypass ||
  1585. (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED))
  1586. cmd->request_desc->SCSIIO.RequestFlags =
  1587. (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
  1588. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1589. io_request->RaidContext.Type = MPI2_TYPE_CUDA;
  1590. io_request->RaidContext.regLockFlags |=
  1591. (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
  1592. MR_RL_FLAGS_SEQ_NUM_ENABLE);
  1593. io_request->RaidContext.nseg = 0x1;
  1594. }
  1595. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1596. io_request->DevHandle = cpu_to_le16(device_id);
  1597. } /* Not FP */
  1598. }
  1599. /**
  1600. * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
  1601. * @instance: Adapter soft state
  1602. * @scp: SCSI command
  1603. * @cmd: Command to be prepared
  1604. *
  1605. * Prepares the io_request frame for non-rw io cmds for vd.
  1606. */
  1607. static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
  1608. struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
  1609. {
  1610. u32 device_id;
  1611. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1612. u16 pd_index = 0;
  1613. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1614. struct fusion_context *fusion = instance->ctrl_context;
  1615. u8 span, physArm;
  1616. __le16 devHandle;
  1617. u32 ld, arRef, pd;
  1618. struct MR_LD_RAID *raid;
  1619. struct RAID_CONTEXT *pRAID_Context;
  1620. u8 fp_possible = 1;
  1621. io_request = cmd->io_request;
  1622. device_id = MEGASAS_DEV_INDEX(scmd);
  1623. pd_index = MEGASAS_PD_INDEX(scmd);
  1624. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1625. io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  1626. /* get RAID_Context pointer */
  1627. pRAID_Context = &io_request->RaidContext;
  1628. /* Check with FW team */
  1629. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1630. pRAID_Context->regLockRowLBA = 0;
  1631. pRAID_Context->regLockLength = 0;
  1632. if (fusion->fast_path_io && (
  1633. device_id < instance->fw_supported_vd_count)) {
  1634. ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
  1635. if (ld >= instance->fw_supported_vd_count - 1)
  1636. fp_possible = 0;
  1637. raid = MR_LdRaidGet(ld, local_map_ptr);
  1638. if (!(raid->capability.fpNonRWCapable))
  1639. fp_possible = 0;
  1640. } else
  1641. fp_possible = 0;
  1642. if (!fp_possible) {
  1643. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1644. io_request->DevHandle = cpu_to_le16(device_id);
  1645. io_request->LUN[1] = scmd->device->lun;
  1646. pRAID_Context->timeoutValue =
  1647. cpu_to_le16 (scmd->request->timeout / HZ);
  1648. cmd->request_desc->SCSIIO.RequestFlags =
  1649. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1650. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1651. } else {
  1652. /* set RAID context values */
  1653. pRAID_Context->configSeqNum = raid->seqNum;
  1654. pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
  1655. pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
  1656. /* get the DevHandle for the PD (since this is
  1657. fpNonRWCapable, this is a single disk RAID0) */
  1658. span = physArm = 0;
  1659. arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
  1660. pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
  1661. devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
  1662. /* build request descriptor */
  1663. cmd->request_desc->SCSIIO.RequestFlags =
  1664. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
  1665. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1666. cmd->request_desc->SCSIIO.DevHandle = devHandle;
  1667. /* populate the LUN field */
  1668. memcpy(io_request->LUN, raid->LUN, 8);
  1669. /* build the raidScsiIO structure */
  1670. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1671. io_request->DevHandle = devHandle;
  1672. }
  1673. }
  1674. /**
  1675. * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
  1676. * @instance: Adapter soft state
  1677. * @scp: SCSI command
  1678. * @cmd: Command to be prepared
  1679. * @fp_possible: parameter to detect fast path or firmware path io.
  1680. *
  1681. * Prepares the io_request frame for rw/non-rw io cmds for syspds
  1682. */
  1683. static void
  1684. megasas_build_syspd_fusion(struct megasas_instance *instance,
  1685. struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
  1686. bool fp_possible)
  1687. {
  1688. u32 device_id;
  1689. struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
  1690. u16 pd_index = 0;
  1691. u16 os_timeout_value;
  1692. u16 timeout_limit;
  1693. struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
  1694. struct RAID_CONTEXT *pRAID_Context;
  1695. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  1696. struct fusion_context *fusion = instance->ctrl_context;
  1697. pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
  1698. device_id = MEGASAS_DEV_INDEX(scmd);
  1699. pd_index = MEGASAS_PD_INDEX(scmd);
  1700. os_timeout_value = scmd->request->timeout / HZ;
  1701. io_request = cmd->io_request;
  1702. /* get RAID_Context pointer */
  1703. pRAID_Context = &io_request->RaidContext;
  1704. pRAID_Context->regLockFlags = 0;
  1705. pRAID_Context->regLockRowLBA = 0;
  1706. pRAID_Context->regLockLength = 0;
  1707. io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
  1708. io_request->LUN[1] = scmd->device->lun;
  1709. pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
  1710. << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
  1711. /* If FW supports PD sequence number */
  1712. if (instance->use_seqnum_jbod_fp &&
  1713. instance->pd_list[pd_index].driveType == TYPE_DISK) {
  1714. /* TgtId must be incremented by 255 as jbod seq number is index
  1715. * below raid map
  1716. */
  1717. pRAID_Context->VirtualDiskTgtId =
  1718. cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
  1719. pRAID_Context->configSeqNum = pd_sync->seq[pd_index].seqNum;
  1720. io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
  1721. pRAID_Context->regLockFlags |=
  1722. (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
  1723. pRAID_Context->Type = MPI2_TYPE_CUDA;
  1724. pRAID_Context->nseg = 0x1;
  1725. } else if (fusion->fast_path_io) {
  1726. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1727. pRAID_Context->configSeqNum = 0;
  1728. local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
  1729. io_request->DevHandle =
  1730. local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
  1731. } else {
  1732. /* Want to send all IO via FW path */
  1733. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1734. pRAID_Context->configSeqNum = 0;
  1735. io_request->DevHandle = cpu_to_le16(0xFFFF);
  1736. }
  1737. cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
  1738. cmd->request_desc->SCSIIO.MSIxIndex =
  1739. instance->msix_vectors ?
  1740. (raw_smp_processor_id() % instance->msix_vectors) : 0;
  1741. if (!fp_possible) {
  1742. /* system pd firmware path */
  1743. io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
  1744. cmd->request_desc->SCSIIO.RequestFlags =
  1745. (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  1746. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1747. pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
  1748. pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
  1749. } else {
  1750. if (os_timeout_value)
  1751. os_timeout_value++;
  1752. /* system pd Fast Path */
  1753. io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
  1754. timeout_limit = (scmd->device->type == TYPE_DISK) ?
  1755. 255 : 0xFFFF;
  1756. pRAID_Context->timeoutValue =
  1757. cpu_to_le16((os_timeout_value > timeout_limit) ?
  1758. timeout_limit : os_timeout_value);
  1759. if (fusion->adapter_type == INVADER_SERIES)
  1760. io_request->IoFlags |=
  1761. cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
  1762. cmd->request_desc->SCSIIO.RequestFlags =
  1763. (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
  1764. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  1765. }
  1766. }
  1767. /**
  1768. * megasas_build_io_fusion - Prepares IOs to devices
  1769. * @instance: Adapter soft state
  1770. * @scp: SCSI command
  1771. * @cmd: Command to be prepared
  1772. *
  1773. * Invokes helper functions to prepare request frames
  1774. * and sets flags appropriate for IO/Non-IO cmd
  1775. */
  1776. int
  1777. megasas_build_io_fusion(struct megasas_instance *instance,
  1778. struct scsi_cmnd *scp,
  1779. struct megasas_cmd_fusion *cmd)
  1780. {
  1781. u16 sge_count;
  1782. u8 cmd_type;
  1783. struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
  1784. struct MR_PRIV_DEVICE *mr_device_priv_data;
  1785. mr_device_priv_data = scp->device->hostdata;
  1786. /* Zero out some fields so they don't get reused */
  1787. memset(io_request->LUN, 0x0, 8);
  1788. io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
  1789. io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
  1790. io_request->EEDPFlags = 0;
  1791. io_request->Control = 0;
  1792. io_request->EEDPBlockSize = 0;
  1793. io_request->ChainOffset = 0;
  1794. io_request->RaidContext.RAIDFlags = 0;
  1795. io_request->RaidContext.Type = 0;
  1796. io_request->RaidContext.nseg = 0;
  1797. memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
  1798. /*
  1799. * Just the CDB length,rest of the Flags are zero
  1800. * This will be modified for FP in build_ldio_fusion
  1801. */
  1802. io_request->IoFlags = cpu_to_le16(scp->cmd_len);
  1803. switch (cmd_type = megasas_cmd_type(scp)) {
  1804. case READ_WRITE_LDIO:
  1805. megasas_build_ldio_fusion(instance, scp, cmd);
  1806. break;
  1807. case NON_READ_WRITE_LDIO:
  1808. megasas_build_ld_nonrw_fusion(instance, scp, cmd);
  1809. break;
  1810. case READ_WRITE_SYSPDIO:
  1811. megasas_build_syspd_fusion(instance, scp, cmd, true);
  1812. break;
  1813. case NON_READ_WRITE_SYSPDIO:
  1814. if (instance->secure_jbod_support ||
  1815. mr_device_priv_data->is_tm_capable)
  1816. megasas_build_syspd_fusion(instance, scp, cmd, false);
  1817. else
  1818. megasas_build_syspd_fusion(instance, scp, cmd, true);
  1819. break;
  1820. default:
  1821. break;
  1822. }
  1823. /*
  1824. * Construct SGL
  1825. */
  1826. sge_count =
  1827. megasas_make_sgl_fusion(instance, scp,
  1828. (struct MPI25_IEEE_SGE_CHAIN64 *)
  1829. &io_request->SGL, cmd);
  1830. if (sge_count > instance->max_num_sge) {
  1831. dev_err(&instance->pdev->dev, "Error. sge_count (0x%x) exceeds "
  1832. "max (0x%x) allowed\n", sge_count,
  1833. instance->max_num_sge);
  1834. return 1;
  1835. }
  1836. /* numSGE store lower 8 bit of sge_count.
  1837. * numSGEExt store higher 8 bit of sge_count
  1838. */
  1839. io_request->RaidContext.numSGE = sge_count;
  1840. io_request->RaidContext.numSGEExt = (u8)(sge_count >> 8);
  1841. io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
  1842. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1843. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
  1844. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1845. io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
  1846. io_request->SGLOffset0 =
  1847. offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
  1848. io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
  1849. io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
  1850. cmd->scmd = scp;
  1851. scp->SCp.ptr = (char *)cmd;
  1852. return 0;
  1853. }
  1854. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  1855. megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
  1856. {
  1857. u8 *p;
  1858. struct fusion_context *fusion;
  1859. if (index >= instance->max_fw_cmds) {
  1860. dev_err(&instance->pdev->dev, "Invalid SMID (0x%x)request for "
  1861. "descriptor for scsi%d\n", index,
  1862. instance->host->host_no);
  1863. return NULL;
  1864. }
  1865. fusion = instance->ctrl_context;
  1866. p = fusion->req_frames_desc
  1867. +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
  1868. return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
  1869. }
  1870. /**
  1871. * megasas_build_and_issue_cmd_fusion -Main routine for building and
  1872. * issuing non IOCTL cmd
  1873. * @instance: Adapter soft state
  1874. * @scmd: pointer to scsi cmd from OS
  1875. */
  1876. static u32
  1877. megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
  1878. struct scsi_cmnd *scmd)
  1879. {
  1880. struct megasas_cmd_fusion *cmd;
  1881. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  1882. u32 index;
  1883. struct fusion_context *fusion;
  1884. fusion = instance->ctrl_context;
  1885. if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
  1886. instance->ldio_threshold &&
  1887. (atomic_inc_return(&instance->ldio_outstanding) >
  1888. instance->ldio_threshold)) {
  1889. atomic_dec(&instance->ldio_outstanding);
  1890. return SCSI_MLQUEUE_DEVICE_BUSY;
  1891. }
  1892. cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
  1893. index = cmd->index;
  1894. req_desc = megasas_get_request_descriptor(instance, index-1);
  1895. if (!req_desc)
  1896. return SCSI_MLQUEUE_HOST_BUSY;
  1897. req_desc->Words = 0;
  1898. cmd->request_desc = req_desc;
  1899. if (megasas_build_io_fusion(instance, scmd, cmd)) {
  1900. megasas_return_cmd_fusion(instance, cmd);
  1901. dev_err(&instance->pdev->dev, "Error building command\n");
  1902. cmd->request_desc = NULL;
  1903. return SCSI_MLQUEUE_HOST_BUSY;
  1904. }
  1905. req_desc = cmd->request_desc;
  1906. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  1907. if (cmd->io_request->ChainOffset != 0 &&
  1908. cmd->io_request->ChainOffset != 0xF)
  1909. dev_err(&instance->pdev->dev, "The chain offset value is not "
  1910. "correct : %x\n", cmd->io_request->ChainOffset);
  1911. /*
  1912. * Issue the command to the FW
  1913. */
  1914. atomic_inc(&instance->fw_outstanding);
  1915. megasas_fire_cmd_fusion(instance, req_desc);
  1916. return 0;
  1917. }
  1918. /**
  1919. * complete_cmd_fusion - Completes command
  1920. * @instance: Adapter soft state
  1921. * Completes all commands that is in reply descriptor queue
  1922. */
  1923. int
  1924. complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
  1925. {
  1926. union MPI2_REPLY_DESCRIPTORS_UNION *desc;
  1927. struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
  1928. struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
  1929. struct fusion_context *fusion;
  1930. struct megasas_cmd *cmd_mfi;
  1931. struct megasas_cmd_fusion *cmd_fusion;
  1932. u16 smid, num_completed;
  1933. u8 reply_descript_type;
  1934. u32 status, extStatus, device_id;
  1935. union desc_value d_val;
  1936. struct LD_LOAD_BALANCE_INFO *lbinfo;
  1937. int threshold_reply_count = 0;
  1938. struct scsi_cmnd *scmd_local = NULL;
  1939. struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
  1940. struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
  1941. fusion = instance->ctrl_context;
  1942. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
  1943. return IRQ_HANDLED;
  1944. desc = fusion->reply_frames_desc[MSIxIndex] +
  1945. fusion->last_reply_idx[MSIxIndex];
  1946. reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  1947. d_val.word = desc->Words;
  1948. reply_descript_type = reply_desc->ReplyFlags &
  1949. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  1950. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  1951. return IRQ_NONE;
  1952. num_completed = 0;
  1953. while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
  1954. d_val.u.high != cpu_to_le32(UINT_MAX)) {
  1955. smid = le16_to_cpu(reply_desc->SMID);
  1956. cmd_fusion = fusion->cmd_list[smid - 1];
  1957. scsi_io_req =
  1958. (struct MPI2_RAID_SCSI_IO_REQUEST *)
  1959. cmd_fusion->io_request;
  1960. if (cmd_fusion->scmd)
  1961. cmd_fusion->scmd->SCp.ptr = NULL;
  1962. scmd_local = cmd_fusion->scmd;
  1963. status = scsi_io_req->RaidContext.status;
  1964. extStatus = scsi_io_req->RaidContext.exStatus;
  1965. switch (scsi_io_req->Function) {
  1966. case MPI2_FUNCTION_SCSI_TASK_MGMT:
  1967. mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
  1968. cmd_fusion->io_request;
  1969. mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
  1970. &mr_tm_req->TmRequest;
  1971. dev_dbg(&instance->pdev->dev, "TM completion:"
  1972. "type: 0x%x TaskMID: 0x%x\n",
  1973. mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
  1974. complete(&cmd_fusion->done);
  1975. break;
  1976. case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
  1977. /* Update load balancing info */
  1978. device_id = MEGASAS_DEV_INDEX(scmd_local);
  1979. lbinfo = &fusion->load_balance_info[device_id];
  1980. if (cmd_fusion->scmd->SCp.Status &
  1981. MEGASAS_LOAD_BALANCE_FLAG) {
  1982. atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
  1983. cmd_fusion->scmd->SCp.Status &=
  1984. ~MEGASAS_LOAD_BALANCE_FLAG;
  1985. }
  1986. if (reply_descript_type ==
  1987. MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
  1988. if (megasas_dbg_lvl == 5)
  1989. dev_err(&instance->pdev->dev, "\nFAST Path "
  1990. "IO Success\n");
  1991. }
  1992. /* Fall thru and complete IO */
  1993. case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
  1994. /* Map the FW Cmd Status */
  1995. map_cmd_status(cmd_fusion, status, extStatus);
  1996. scsi_io_req->RaidContext.status = 0;
  1997. scsi_io_req->RaidContext.exStatus = 0;
  1998. if (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
  1999. atomic_dec(&instance->ldio_outstanding);
  2000. megasas_return_cmd_fusion(instance, cmd_fusion);
  2001. scsi_dma_unmap(scmd_local);
  2002. scmd_local->scsi_done(scmd_local);
  2003. atomic_dec(&instance->fw_outstanding);
  2004. break;
  2005. case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
  2006. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  2007. /* Poll mode. Dummy free.
  2008. * In case of Interrupt mode, caller has reverse check.
  2009. */
  2010. if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
  2011. cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
  2012. megasas_return_cmd(instance, cmd_mfi);
  2013. } else
  2014. megasas_complete_cmd(instance, cmd_mfi, DID_OK);
  2015. break;
  2016. }
  2017. fusion->last_reply_idx[MSIxIndex]++;
  2018. if (fusion->last_reply_idx[MSIxIndex] >=
  2019. fusion->reply_q_depth)
  2020. fusion->last_reply_idx[MSIxIndex] = 0;
  2021. desc->Words = cpu_to_le64(ULLONG_MAX);
  2022. num_completed++;
  2023. threshold_reply_count++;
  2024. /* Get the next reply descriptor */
  2025. if (!fusion->last_reply_idx[MSIxIndex])
  2026. desc = fusion->reply_frames_desc[MSIxIndex];
  2027. else
  2028. desc++;
  2029. reply_desc =
  2030. (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
  2031. d_val.word = desc->Words;
  2032. reply_descript_type = reply_desc->ReplyFlags &
  2033. MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  2034. if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
  2035. break;
  2036. /*
  2037. * Write to reply post host index register after completing threshold
  2038. * number of reply counts and still there are more replies in reply queue
  2039. * pending to be completed
  2040. */
  2041. if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
  2042. if (fusion->adapter_type == INVADER_SERIES)
  2043. writel(((MSIxIndex & 0x7) << 24) |
  2044. fusion->last_reply_idx[MSIxIndex],
  2045. instance->reply_post_host_index_addr[MSIxIndex/8]);
  2046. else
  2047. writel((MSIxIndex << 24) |
  2048. fusion->last_reply_idx[MSIxIndex],
  2049. instance->reply_post_host_index_addr[0]);
  2050. threshold_reply_count = 0;
  2051. }
  2052. }
  2053. if (!num_completed)
  2054. return IRQ_NONE;
  2055. wmb();
  2056. if (fusion->adapter_type == INVADER_SERIES)
  2057. writel(((MSIxIndex & 0x7) << 24) |
  2058. fusion->last_reply_idx[MSIxIndex],
  2059. instance->reply_post_host_index_addr[MSIxIndex/8]);
  2060. else
  2061. writel((MSIxIndex << 24) |
  2062. fusion->last_reply_idx[MSIxIndex],
  2063. instance->reply_post_host_index_addr[0]);
  2064. megasas_check_and_restore_queue_depth(instance);
  2065. return IRQ_HANDLED;
  2066. }
  2067. /**
  2068. * megasas_complete_cmd_dpc_fusion - Completes command
  2069. * @instance: Adapter soft state
  2070. *
  2071. * Tasklet to complete cmds
  2072. */
  2073. void
  2074. megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
  2075. {
  2076. struct megasas_instance *instance =
  2077. (struct megasas_instance *)instance_addr;
  2078. unsigned long flags;
  2079. u32 count, MSIxIndex;
  2080. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  2081. /* If we have already declared adapter dead, donot complete cmds */
  2082. spin_lock_irqsave(&instance->hba_lock, flags);
  2083. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2084. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2085. return;
  2086. }
  2087. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2088. for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
  2089. complete_cmd_fusion(instance, MSIxIndex);
  2090. }
  2091. /**
  2092. * megasas_isr_fusion - isr entry point
  2093. */
  2094. irqreturn_t megasas_isr_fusion(int irq, void *devp)
  2095. {
  2096. struct megasas_irq_context *irq_context = devp;
  2097. struct megasas_instance *instance = irq_context->instance;
  2098. u32 mfiStatus, fw_state, dma_state;
  2099. if (instance->mask_interrupts)
  2100. return IRQ_NONE;
  2101. if (!instance->msix_vectors) {
  2102. mfiStatus = instance->instancet->clear_intr(instance->reg_set);
  2103. if (!mfiStatus)
  2104. return IRQ_NONE;
  2105. }
  2106. /* If we are resetting, bail */
  2107. if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
  2108. instance->instancet->clear_intr(instance->reg_set);
  2109. return IRQ_HANDLED;
  2110. }
  2111. if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
  2112. instance->instancet->clear_intr(instance->reg_set);
  2113. /* If we didn't complete any commands, check for FW fault */
  2114. fw_state = instance->instancet->read_fw_status_reg(
  2115. instance->reg_set) & MFI_STATE_MASK;
  2116. dma_state = instance->instancet->read_fw_status_reg
  2117. (instance->reg_set) & MFI_STATE_DMADONE;
  2118. if (instance->crash_dump_drv_support &&
  2119. instance->crash_dump_app_support) {
  2120. /* Start collecting crash, if DMA bit is done */
  2121. if ((fw_state == MFI_STATE_FAULT) && dma_state)
  2122. schedule_work(&instance->crash_init);
  2123. else if (fw_state == MFI_STATE_FAULT)
  2124. schedule_work(&instance->work_init);
  2125. } else if (fw_state == MFI_STATE_FAULT) {
  2126. dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
  2127. "for scsi%d\n", instance->host->host_no);
  2128. schedule_work(&instance->work_init);
  2129. }
  2130. }
  2131. return IRQ_HANDLED;
  2132. }
  2133. /**
  2134. * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
  2135. * @instance: Adapter soft state
  2136. * mfi_cmd: megasas_cmd pointer
  2137. *
  2138. */
  2139. u8
  2140. build_mpt_mfi_pass_thru(struct megasas_instance *instance,
  2141. struct megasas_cmd *mfi_cmd)
  2142. {
  2143. struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
  2144. struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
  2145. struct megasas_cmd_fusion *cmd;
  2146. struct fusion_context *fusion;
  2147. struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
  2148. fusion = instance->ctrl_context;
  2149. cmd = megasas_get_cmd_fusion(instance,
  2150. instance->max_scsi_cmds + mfi_cmd->index);
  2151. /* Save the smid. To be used for returning the cmd */
  2152. mfi_cmd->context.smid = cmd->index;
  2153. /*
  2154. * For cmds where the flag is set, store the flag and check
  2155. * on completion. For cmds with this flag, don't call
  2156. * megasas_complete_cmd
  2157. */
  2158. if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
  2159. mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
  2160. io_req = cmd->io_request;
  2161. if (fusion->adapter_type == INVADER_SERIES) {
  2162. struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
  2163. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
  2164. sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
  2165. sgl_ptr_end->Flags = 0;
  2166. }
  2167. mpi25_ieee_chain =
  2168. (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
  2169. io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
  2170. io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
  2171. SGL) / 4;
  2172. io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
  2173. mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
  2174. mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
  2175. MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
  2176. mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
  2177. return 0;
  2178. }
  2179. /**
  2180. * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
  2181. * @instance: Adapter soft state
  2182. * @cmd: mfi cmd to build
  2183. *
  2184. */
  2185. union MEGASAS_REQUEST_DESCRIPTOR_UNION *
  2186. build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  2187. {
  2188. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2189. u16 index;
  2190. if (build_mpt_mfi_pass_thru(instance, cmd)) {
  2191. dev_err(&instance->pdev->dev, "Couldn't build MFI pass thru cmd\n");
  2192. return NULL;
  2193. }
  2194. index = cmd->context.smid;
  2195. req_desc = megasas_get_request_descriptor(instance, index - 1);
  2196. if (!req_desc)
  2197. return NULL;
  2198. req_desc->Words = 0;
  2199. req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
  2200. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2201. req_desc->SCSIIO.SMID = cpu_to_le16(index);
  2202. return req_desc;
  2203. }
  2204. /**
  2205. * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
  2206. * @instance: Adapter soft state
  2207. * @cmd: mfi cmd pointer
  2208. *
  2209. */
  2210. int
  2211. megasas_issue_dcmd_fusion(struct megasas_instance *instance,
  2212. struct megasas_cmd *cmd)
  2213. {
  2214. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2215. req_desc = build_mpt_cmd(instance, cmd);
  2216. if (!req_desc) {
  2217. dev_info(&instance->pdev->dev, "Failed from %s %d\n",
  2218. __func__, __LINE__);
  2219. return DCMD_NOT_FIRED;
  2220. }
  2221. megasas_fire_cmd_fusion(instance, req_desc);
  2222. return DCMD_SUCCESS;
  2223. }
  2224. /**
  2225. * megasas_release_fusion - Reverses the FW initialization
  2226. * @instance: Adapter soft state
  2227. */
  2228. void
  2229. megasas_release_fusion(struct megasas_instance *instance)
  2230. {
  2231. megasas_free_cmds(instance);
  2232. megasas_free_cmds_fusion(instance);
  2233. iounmap(instance->reg_set);
  2234. pci_release_selected_regions(instance->pdev, 1<<instance->bar);
  2235. }
  2236. /**
  2237. * megasas_read_fw_status_reg_fusion - returns the current FW status value
  2238. * @regs: MFI register set
  2239. */
  2240. static u32
  2241. megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
  2242. {
  2243. return readl(&(regs)->outbound_scratch_pad);
  2244. }
  2245. /**
  2246. * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  2247. * @instance: Controller's soft instance
  2248. * return: Number of allocated host crash buffers
  2249. */
  2250. static void
  2251. megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
  2252. {
  2253. unsigned int i;
  2254. instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
  2255. for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
  2256. instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
  2257. instance->crash_buf_pages);
  2258. if (!instance->crash_buf[i]) {
  2259. dev_info(&instance->pdev->dev, "Firmware crash dump "
  2260. "memory allocation failed at index %d\n", i);
  2261. break;
  2262. }
  2263. memset(instance->crash_buf[i], 0,
  2264. ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
  2265. }
  2266. instance->drv_buf_alloc = i;
  2267. }
  2268. /**
  2269. * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
  2270. * @instance: Controller's soft instance
  2271. */
  2272. void
  2273. megasas_free_host_crash_buffer(struct megasas_instance *instance)
  2274. {
  2275. unsigned int i
  2276. ;
  2277. for (i = 0; i < instance->drv_buf_alloc; i++) {
  2278. if (instance->crash_buf[i])
  2279. free_pages((ulong)instance->crash_buf[i],
  2280. instance->crash_buf_pages);
  2281. }
  2282. instance->drv_buf_index = 0;
  2283. instance->drv_buf_alloc = 0;
  2284. instance->fw_crash_state = UNAVAILABLE;
  2285. instance->fw_crash_buffer_size = 0;
  2286. }
  2287. /**
  2288. * megasas_adp_reset_fusion - For controller reset
  2289. * @regs: MFI register set
  2290. */
  2291. static int
  2292. megasas_adp_reset_fusion(struct megasas_instance *instance,
  2293. struct megasas_register_set __iomem *regs)
  2294. {
  2295. u32 host_diag, abs_state, retry;
  2296. /* Now try to reset the chip */
  2297. writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2298. writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2299. writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2300. writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2301. writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2302. writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2303. writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
  2304. /* Check that the diag write enable (DRWE) bit is on */
  2305. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2306. retry = 0;
  2307. while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
  2308. msleep(100);
  2309. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2310. if (retry++ == 100) {
  2311. dev_warn(&instance->pdev->dev,
  2312. "Host diag unlock failed from %s %d\n",
  2313. __func__, __LINE__);
  2314. break;
  2315. }
  2316. }
  2317. if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
  2318. return -1;
  2319. /* Send chip reset command */
  2320. writel(host_diag | HOST_DIAG_RESET_ADAPTER,
  2321. &instance->reg_set->fusion_host_diag);
  2322. msleep(3000);
  2323. /* Make sure reset adapter bit is cleared */
  2324. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2325. retry = 0;
  2326. while (host_diag & HOST_DIAG_RESET_ADAPTER) {
  2327. msleep(100);
  2328. host_diag = readl(&instance->reg_set->fusion_host_diag);
  2329. if (retry++ == 1000) {
  2330. dev_warn(&instance->pdev->dev,
  2331. "Diag reset adapter never cleared %s %d\n",
  2332. __func__, __LINE__);
  2333. break;
  2334. }
  2335. }
  2336. if (host_diag & HOST_DIAG_RESET_ADAPTER)
  2337. return -1;
  2338. abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
  2339. & MFI_STATE_MASK;
  2340. retry = 0;
  2341. while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
  2342. msleep(100);
  2343. abs_state = instance->instancet->
  2344. read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2345. }
  2346. if (abs_state <= MFI_STATE_FW_INIT) {
  2347. dev_warn(&instance->pdev->dev,
  2348. "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
  2349. abs_state, __func__, __LINE__);
  2350. return -1;
  2351. }
  2352. return 0;
  2353. }
  2354. /**
  2355. * megasas_check_reset_fusion - For controller reset check
  2356. * @regs: MFI register set
  2357. */
  2358. static int
  2359. megasas_check_reset_fusion(struct megasas_instance *instance,
  2360. struct megasas_register_set __iomem *regs)
  2361. {
  2362. return 0;
  2363. }
  2364. /* This function waits for outstanding commands on fusion to complete */
  2365. int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
  2366. int reason, int *convert)
  2367. {
  2368. int i, outstanding, retval = 0, hb_seconds_missed = 0;
  2369. u32 fw_state;
  2370. for (i = 0; i < resetwaittime; i++) {
  2371. /* Check if firmware is in fault state */
  2372. fw_state = instance->instancet->read_fw_status_reg(
  2373. instance->reg_set) & MFI_STATE_MASK;
  2374. if (fw_state == MFI_STATE_FAULT) {
  2375. dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
  2376. " will reset adapter scsi%d.\n",
  2377. instance->host->host_no);
  2378. megasas_complete_cmd_dpc_fusion((unsigned long)instance);
  2379. retval = 1;
  2380. goto out;
  2381. }
  2382. if (reason == MFI_IO_TIMEOUT_OCR) {
  2383. dev_info(&instance->pdev->dev,
  2384. "MFI IO is timed out, initiating OCR\n");
  2385. megasas_complete_cmd_dpc_fusion((unsigned long)instance);
  2386. retval = 1;
  2387. goto out;
  2388. }
  2389. /* If SR-IOV VF mode & heartbeat timeout, don't wait */
  2390. if (instance->requestorId && !reason) {
  2391. retval = 1;
  2392. goto out;
  2393. }
  2394. /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
  2395. if (instance->requestorId && reason) {
  2396. if (instance->hb_host_mem->HB.fwCounter !=
  2397. instance->hb_host_mem->HB.driverCounter) {
  2398. instance->hb_host_mem->HB.driverCounter =
  2399. instance->hb_host_mem->HB.fwCounter;
  2400. hb_seconds_missed = 0;
  2401. } else {
  2402. hb_seconds_missed++;
  2403. if (hb_seconds_missed ==
  2404. (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
  2405. dev_warn(&instance->pdev->dev, "SR-IOV:"
  2406. " Heartbeat never completed "
  2407. " while polling during I/O "
  2408. " timeout handling for "
  2409. "scsi%d.\n",
  2410. instance->host->host_no);
  2411. *convert = 1;
  2412. retval = 1;
  2413. goto out;
  2414. }
  2415. }
  2416. }
  2417. outstanding = atomic_read(&instance->fw_outstanding);
  2418. if (!outstanding)
  2419. goto out;
  2420. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  2421. dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
  2422. "commands to complete for scsi%d\n", i,
  2423. outstanding, instance->host->host_no);
  2424. megasas_complete_cmd_dpc_fusion(
  2425. (unsigned long)instance);
  2426. }
  2427. msleep(1000);
  2428. }
  2429. if (atomic_read(&instance->fw_outstanding)) {
  2430. dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
  2431. "will reset adapter scsi%d.\n",
  2432. instance->host->host_no);
  2433. *convert = 1;
  2434. retval = 1;
  2435. }
  2436. out:
  2437. return retval;
  2438. }
  2439. void megasas_reset_reply_desc(struct megasas_instance *instance)
  2440. {
  2441. int i, j, count;
  2442. struct fusion_context *fusion;
  2443. union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
  2444. fusion = instance->ctrl_context;
  2445. count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
  2446. for (i = 0 ; i < count ; i++) {
  2447. fusion->last_reply_idx[i] = 0;
  2448. reply_desc = fusion->reply_frames_desc[i];
  2449. for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
  2450. reply_desc->Words = cpu_to_le64(ULLONG_MAX);
  2451. }
  2452. }
  2453. /*
  2454. * megasas_refire_mgmt_cmd : Re-fire management commands
  2455. * @instance: Controller's soft instance
  2456. */
  2457. void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
  2458. {
  2459. int j;
  2460. struct megasas_cmd_fusion *cmd_fusion;
  2461. struct fusion_context *fusion;
  2462. struct megasas_cmd *cmd_mfi;
  2463. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2464. u16 smid;
  2465. bool refire_cmd = 0;
  2466. fusion = instance->ctrl_context;
  2467. /* Re-fire management commands.
  2468. * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
  2469. */
  2470. for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
  2471. cmd_fusion = fusion->cmd_list[j];
  2472. cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
  2473. smid = le16_to_cpu(cmd_mfi->context.smid);
  2474. if (!smid)
  2475. continue;
  2476. req_desc = megasas_get_request_descriptor
  2477. (instance, smid - 1);
  2478. refire_cmd = req_desc && ((cmd_mfi->frame->dcmd.opcode !=
  2479. cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
  2480. (cmd_mfi->frame->dcmd.opcode !=
  2481. cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO)))
  2482. && !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
  2483. if (refire_cmd)
  2484. megasas_fire_cmd_fusion(instance, req_desc);
  2485. else
  2486. megasas_return_cmd(instance, cmd_mfi);
  2487. }
  2488. }
  2489. /*
  2490. * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
  2491. * @instance: per adapter struct
  2492. * @channel: the channel assigned by the OS
  2493. * @id: the id assigned by the OS
  2494. *
  2495. * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
  2496. */
  2497. static int megasas_track_scsiio(struct megasas_instance *instance,
  2498. int id, int channel)
  2499. {
  2500. int i, found = 0;
  2501. struct megasas_cmd_fusion *cmd_fusion;
  2502. struct fusion_context *fusion;
  2503. fusion = instance->ctrl_context;
  2504. for (i = 0 ; i < instance->max_scsi_cmds; i++) {
  2505. cmd_fusion = fusion->cmd_list[i];
  2506. if (cmd_fusion->scmd &&
  2507. (cmd_fusion->scmd->device->id == id &&
  2508. cmd_fusion->scmd->device->channel == channel)) {
  2509. dev_info(&instance->pdev->dev,
  2510. "SCSI commands pending to target"
  2511. "channel %d id %d \tSMID: 0x%x\n",
  2512. channel, id, cmd_fusion->index);
  2513. scsi_print_command(cmd_fusion->scmd);
  2514. found = 1;
  2515. break;
  2516. }
  2517. }
  2518. return found ? FAILED : SUCCESS;
  2519. }
  2520. /**
  2521. * megasas_tm_response_code - translation of device response code
  2522. * @ioc: per adapter object
  2523. * @mpi_reply: MPI reply returned by firmware
  2524. *
  2525. * Return nothing.
  2526. */
  2527. static void
  2528. megasas_tm_response_code(struct megasas_instance *instance,
  2529. struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
  2530. {
  2531. char *desc;
  2532. switch (mpi_reply->ResponseCode) {
  2533. case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
  2534. desc = "task management request completed";
  2535. break;
  2536. case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
  2537. desc = "invalid frame";
  2538. break;
  2539. case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
  2540. desc = "task management request not supported";
  2541. break;
  2542. case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
  2543. desc = "task management request failed";
  2544. break;
  2545. case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
  2546. desc = "task management request succeeded";
  2547. break;
  2548. case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
  2549. desc = "invalid lun";
  2550. break;
  2551. case 0xA:
  2552. desc = "overlapped tag attempted";
  2553. break;
  2554. case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
  2555. desc = "task queued, however not sent to target";
  2556. break;
  2557. default:
  2558. desc = "unknown";
  2559. break;
  2560. }
  2561. dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
  2562. mpi_reply->ResponseCode, desc);
  2563. dev_dbg(&instance->pdev->dev,
  2564. "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
  2565. " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
  2566. mpi_reply->TerminationCount, mpi_reply->DevHandle,
  2567. mpi_reply->Function, mpi_reply->TaskType,
  2568. mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
  2569. }
  2570. /**
  2571. * megasas_issue_tm - main routine for sending tm requests
  2572. * @instance: per adapter struct
  2573. * @device_handle: device handle
  2574. * @channel: the channel assigned by the OS
  2575. * @id: the id assigned by the OS
  2576. * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
  2577. * @smid_task: smid assigned to the task
  2578. * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
  2579. * Context: user
  2580. *
  2581. * MegaRaid use MPT interface for Task Magement request.
  2582. * A generic API for sending task management requests to firmware.
  2583. *
  2584. * Return SUCCESS or FAILED.
  2585. */
  2586. static int
  2587. megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
  2588. uint channel, uint id, u16 smid_task, u8 type)
  2589. {
  2590. struct MR_TASK_MANAGE_REQUEST *mr_request;
  2591. struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
  2592. unsigned long timeleft;
  2593. struct megasas_cmd_fusion *cmd_fusion;
  2594. struct megasas_cmd *cmd_mfi;
  2595. union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
  2596. struct fusion_context *fusion;
  2597. struct megasas_cmd_fusion *scsi_lookup;
  2598. int rc;
  2599. struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
  2600. fusion = instance->ctrl_context;
  2601. cmd_mfi = megasas_get_cmd(instance);
  2602. if (!cmd_mfi) {
  2603. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  2604. __func__, __LINE__);
  2605. return -ENOMEM;
  2606. }
  2607. cmd_fusion = megasas_get_cmd_fusion(instance,
  2608. instance->max_scsi_cmds + cmd_mfi->index);
  2609. /* Save the smid. To be used for returning the cmd */
  2610. cmd_mfi->context.smid = cmd_fusion->index;
  2611. req_desc = megasas_get_request_descriptor(instance,
  2612. (cmd_fusion->index - 1));
  2613. if (!req_desc) {
  2614. dev_err(&instance->pdev->dev, "Failed from %s %d\n",
  2615. __func__, __LINE__);
  2616. megasas_return_cmd(instance, cmd_mfi);
  2617. return -ENOMEM;
  2618. }
  2619. cmd_fusion->request_desc = req_desc;
  2620. req_desc->Words = 0;
  2621. scsi_lookup = fusion->cmd_list[smid_task - 1];
  2622. mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
  2623. memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
  2624. mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
  2625. mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
  2626. mpi_request->DevHandle = cpu_to_le16(device_handle);
  2627. mpi_request->TaskType = type;
  2628. mpi_request->TaskMID = cpu_to_le16(smid_task);
  2629. mpi_request->LUN[1] = 0;
  2630. req_desc = cmd_fusion->request_desc;
  2631. req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
  2632. req_desc->HighPriority.RequestFlags =
  2633. (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
  2634. MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
  2635. req_desc->HighPriority.MSIxIndex = 0;
  2636. req_desc->HighPriority.LMID = 0;
  2637. req_desc->HighPriority.Reserved1 = 0;
  2638. if (channel < MEGASAS_MAX_PD_CHANNELS)
  2639. mr_request->tmReqFlags.isTMForPD = 1;
  2640. else
  2641. mr_request->tmReqFlags.isTMForLD = 1;
  2642. init_completion(&cmd_fusion->done);
  2643. megasas_fire_cmd_fusion(instance, req_desc);
  2644. timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);
  2645. if (!timeleft) {
  2646. dev_err(&instance->pdev->dev,
  2647. "task mgmt type 0x%x timed out\n", type);
  2648. cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
  2649. mutex_unlock(&instance->reset_mutex);
  2650. rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
  2651. mutex_lock(&instance->reset_mutex);
  2652. return rc;
  2653. }
  2654. mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
  2655. megasas_tm_response_code(instance, mpi_reply);
  2656. megasas_return_cmd(instance, cmd_mfi);
  2657. rc = SUCCESS;
  2658. switch (type) {
  2659. case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
  2660. if (scsi_lookup->scmd == NULL)
  2661. break;
  2662. else {
  2663. instance->instancet->disable_intr(instance);
  2664. msleep(1000);
  2665. megasas_complete_cmd_dpc_fusion
  2666. ((unsigned long)instance);
  2667. instance->instancet->enable_intr(instance);
  2668. if (scsi_lookup->scmd == NULL)
  2669. break;
  2670. }
  2671. rc = FAILED;
  2672. break;
  2673. case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
  2674. if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
  2675. break;
  2676. instance->instancet->disable_intr(instance);
  2677. msleep(1000);
  2678. megasas_complete_cmd_dpc_fusion
  2679. ((unsigned long)instance);
  2680. rc = megasas_track_scsiio(instance, id, channel);
  2681. instance->instancet->enable_intr(instance);
  2682. break;
  2683. case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
  2684. case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
  2685. break;
  2686. default:
  2687. rc = FAILED;
  2688. break;
  2689. }
  2690. return rc;
  2691. }
  2692. /*
  2693. * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
  2694. * @instance: per adapter struct
  2695. *
  2696. * Return Non Zero index, if SMID found in outstanding commands
  2697. */
  2698. static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
  2699. {
  2700. int i, ret = 0;
  2701. struct megasas_instance *instance;
  2702. struct megasas_cmd_fusion *cmd_fusion;
  2703. struct fusion_context *fusion;
  2704. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2705. fusion = instance->ctrl_context;
  2706. for (i = 0; i < instance->max_scsi_cmds; i++) {
  2707. cmd_fusion = fusion->cmd_list[i];
  2708. if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
  2709. scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
  2710. " SMID: %d\n", cmd_fusion->index);
  2711. ret = cmd_fusion->index;
  2712. break;
  2713. }
  2714. }
  2715. return ret;
  2716. }
  2717. /*
  2718. * megasas_get_tm_devhandle - Get devhandle for TM request
  2719. * @sdev- OS provided scsi device
  2720. *
  2721. * Returns- devhandle/targetID of SCSI device
  2722. */
  2723. static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
  2724. {
  2725. u16 pd_index = 0;
  2726. u32 device_id;
  2727. struct megasas_instance *instance;
  2728. struct fusion_context *fusion;
  2729. struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
  2730. u16 devhandle = (u16)ULONG_MAX;
  2731. instance = (struct megasas_instance *)sdev->host->hostdata;
  2732. fusion = instance->ctrl_context;
  2733. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
  2734. if (instance->use_seqnum_jbod_fp) {
  2735. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  2736. sdev->id;
  2737. pd_sync = (void *)fusion->pd_seq_sync
  2738. [(instance->pd_seq_map_id - 1) & 1];
  2739. devhandle = pd_sync->seq[pd_index].devHandle;
  2740. } else
  2741. sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
  2742. " without JBOD MAP support from %s %d\n", __func__, __LINE__);
  2743. } else {
  2744. device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
  2745. + sdev->id;
  2746. devhandle = device_id;
  2747. }
  2748. return devhandle;
  2749. }
  2750. /*
  2751. * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
  2752. * @scmd : pointer to scsi command object
  2753. *
  2754. * Return SUCCESS, if command aborted else FAILED
  2755. */
  2756. int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
  2757. {
  2758. struct megasas_instance *instance;
  2759. u16 smid, devhandle;
  2760. struct fusion_context *fusion;
  2761. int ret;
  2762. struct MR_PRIV_DEVICE *mr_device_priv_data;
  2763. mr_device_priv_data = scmd->device->hostdata;
  2764. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2765. fusion = instance->ctrl_context;
  2766. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2767. dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
  2768. "SCSI host:%d\n", instance->host->host_no);
  2769. ret = FAILED;
  2770. return ret;
  2771. }
  2772. if (!mr_device_priv_data) {
  2773. sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
  2774. "scmd(%p)\n", scmd);
  2775. scmd->result = DID_NO_CONNECT << 16;
  2776. ret = SUCCESS;
  2777. goto out;
  2778. }
  2779. if (!mr_device_priv_data->is_tm_capable) {
  2780. ret = FAILED;
  2781. goto out;
  2782. }
  2783. mutex_lock(&instance->reset_mutex);
  2784. smid = megasas_fusion_smid_lookup(scmd);
  2785. if (!smid) {
  2786. ret = SUCCESS;
  2787. scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
  2788. " issued is not found in oustanding commands\n");
  2789. mutex_unlock(&instance->reset_mutex);
  2790. goto out;
  2791. }
  2792. devhandle = megasas_get_tm_devhandle(scmd->device);
  2793. if (devhandle == (u16)ULONG_MAX) {
  2794. ret = SUCCESS;
  2795. sdev_printk(KERN_INFO, scmd->device,
  2796. "task abort issued for invalid devhandle\n");
  2797. mutex_unlock(&instance->reset_mutex);
  2798. goto out;
  2799. }
  2800. sdev_printk(KERN_INFO, scmd->device,
  2801. "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
  2802. scmd, devhandle);
  2803. mr_device_priv_data->tm_busy = 1;
  2804. ret = megasas_issue_tm(instance, devhandle,
  2805. scmd->device->channel, scmd->device->id, smid,
  2806. MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
  2807. mr_device_priv_data->tm_busy = 0;
  2808. mutex_unlock(&instance->reset_mutex);
  2809. out:
  2810. sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
  2811. ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
  2812. return ret;
  2813. }
  2814. /*
  2815. * megasas_reset_target_fusion : target reset function for fusion adapters
  2816. * scmd: SCSI command pointer
  2817. *
  2818. * Returns SUCCESS if all commands associated with target aborted else FAILED
  2819. */
  2820. int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
  2821. {
  2822. struct megasas_instance *instance;
  2823. int ret = FAILED;
  2824. u16 devhandle;
  2825. struct fusion_context *fusion;
  2826. struct MR_PRIV_DEVICE *mr_device_priv_data;
  2827. mr_device_priv_data = scmd->device->hostdata;
  2828. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  2829. fusion = instance->ctrl_context;
  2830. if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
  2831. dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
  2832. "SCSI host:%d\n", instance->host->host_no);
  2833. ret = FAILED;
  2834. return ret;
  2835. }
  2836. if (!mr_device_priv_data) {
  2837. sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
  2838. "scmd(%p)\n", scmd);
  2839. scmd->result = DID_NO_CONNECT << 16;
  2840. ret = SUCCESS;
  2841. goto out;
  2842. }
  2843. if (!mr_device_priv_data->is_tm_capable) {
  2844. ret = FAILED;
  2845. goto out;
  2846. }
  2847. mutex_lock(&instance->reset_mutex);
  2848. devhandle = megasas_get_tm_devhandle(scmd->device);
  2849. if (devhandle == (u16)ULONG_MAX) {
  2850. ret = SUCCESS;
  2851. sdev_printk(KERN_INFO, scmd->device,
  2852. "target reset issued for invalid devhandle\n");
  2853. mutex_unlock(&instance->reset_mutex);
  2854. goto out;
  2855. }
  2856. sdev_printk(KERN_INFO, scmd->device,
  2857. "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
  2858. scmd, devhandle);
  2859. mr_device_priv_data->tm_busy = 1;
  2860. ret = megasas_issue_tm(instance, devhandle,
  2861. scmd->device->channel, scmd->device->id, 0,
  2862. MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
  2863. mr_device_priv_data->tm_busy = 0;
  2864. mutex_unlock(&instance->reset_mutex);
  2865. out:
  2866. scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
  2867. (ret == SUCCESS) ? "SUCCESS" : "FAILED");
  2868. return ret;
  2869. }
  2870. /*SRIOV get other instance in cluster if any*/
  2871. struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
  2872. {
  2873. int i;
  2874. for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
  2875. if (megasas_mgmt_info.instance[i] &&
  2876. (megasas_mgmt_info.instance[i] != instance) &&
  2877. megasas_mgmt_info.instance[i]->requestorId &&
  2878. megasas_mgmt_info.instance[i]->peerIsPresent &&
  2879. (memcmp((megasas_mgmt_info.instance[i]->clusterId),
  2880. instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
  2881. return megasas_mgmt_info.instance[i];
  2882. }
  2883. return NULL;
  2884. }
  2885. /* Check for a second path that is currently UP */
  2886. int megasas_check_mpio_paths(struct megasas_instance *instance,
  2887. struct scsi_cmnd *scmd)
  2888. {
  2889. struct megasas_instance *peer_instance = NULL;
  2890. int retval = (DID_RESET << 16);
  2891. if (instance->peerIsPresent) {
  2892. peer_instance = megasas_get_peer_instance(instance);
  2893. if ((peer_instance) &&
  2894. (atomic_read(&peer_instance->adprecovery) ==
  2895. MEGASAS_HBA_OPERATIONAL))
  2896. retval = (DID_NO_CONNECT << 16);
  2897. }
  2898. return retval;
  2899. }
  2900. /* Core fusion reset function */
  2901. int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
  2902. {
  2903. int retval = SUCCESS, i, convert = 0;
  2904. struct megasas_instance *instance;
  2905. struct megasas_cmd_fusion *cmd_fusion;
  2906. struct fusion_context *fusion;
  2907. u32 abs_state, status_reg, reset_adapter;
  2908. u32 io_timeout_in_crash_mode = 0;
  2909. struct scsi_cmnd *scmd_local = NULL;
  2910. struct scsi_device *sdev;
  2911. instance = (struct megasas_instance *)shost->hostdata;
  2912. fusion = instance->ctrl_context;
  2913. mutex_lock(&instance->reset_mutex);
  2914. if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
  2915. dev_warn(&instance->pdev->dev, "Hardware critical error, "
  2916. "returning FAILED for scsi%d.\n",
  2917. instance->host->host_no);
  2918. mutex_unlock(&instance->reset_mutex);
  2919. return FAILED;
  2920. }
  2921. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  2922. abs_state = status_reg & MFI_STATE_MASK;
  2923. /* IO timeout detected, forcibly put FW in FAULT state */
  2924. if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
  2925. instance->crash_dump_app_support && reason) {
  2926. dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
  2927. "forcibly FAULT Firmware\n");
  2928. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  2929. status_reg = readl(&instance->reg_set->doorbell);
  2930. writel(status_reg | MFI_STATE_FORCE_OCR,
  2931. &instance->reg_set->doorbell);
  2932. readl(&instance->reg_set->doorbell);
  2933. mutex_unlock(&instance->reset_mutex);
  2934. do {
  2935. ssleep(3);
  2936. io_timeout_in_crash_mode++;
  2937. dev_dbg(&instance->pdev->dev, "waiting for [%d] "
  2938. "seconds for crash dump collection and OCR "
  2939. "to be done\n", (io_timeout_in_crash_mode * 3));
  2940. } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
  2941. (io_timeout_in_crash_mode < 80));
  2942. if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
  2943. dev_info(&instance->pdev->dev, "OCR done for IO "
  2944. "timeout case\n");
  2945. retval = SUCCESS;
  2946. } else {
  2947. dev_info(&instance->pdev->dev, "Controller is not "
  2948. "operational after 240 seconds wait for IO "
  2949. "timeout case in FW crash dump mode\n do "
  2950. "OCR/kill adapter\n");
  2951. retval = megasas_reset_fusion(shost, 0);
  2952. }
  2953. return retval;
  2954. }
  2955. if (instance->requestorId && !instance->skip_heartbeat_timer_del)
  2956. del_timer_sync(&instance->sriov_heartbeat_timer);
  2957. set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  2958. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
  2959. instance->instancet->disable_intr(instance);
  2960. msleep(1000);
  2961. /* First try waiting for commands to complete */
  2962. if (megasas_wait_for_outstanding_fusion(instance, reason,
  2963. &convert)) {
  2964. atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
  2965. dev_warn(&instance->pdev->dev, "resetting fusion "
  2966. "adapter scsi%d.\n", instance->host->host_no);
  2967. if (convert)
  2968. reason = 0;
  2969. /* Now return commands back to the OS */
  2970. for (i = 0 ; i < instance->max_scsi_cmds; i++) {
  2971. cmd_fusion = fusion->cmd_list[i];
  2972. scmd_local = cmd_fusion->scmd;
  2973. if (cmd_fusion->scmd) {
  2974. scmd_local->result =
  2975. megasas_check_mpio_paths(instance,
  2976. scmd_local);
  2977. if (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
  2978. atomic_dec(&instance->ldio_outstanding);
  2979. megasas_return_cmd_fusion(instance, cmd_fusion);
  2980. scsi_dma_unmap(scmd_local);
  2981. scmd_local->scsi_done(scmd_local);
  2982. atomic_dec(&instance->fw_outstanding);
  2983. }
  2984. }
  2985. status_reg = instance->instancet->read_fw_status_reg(
  2986. instance->reg_set);
  2987. abs_state = status_reg & MFI_STATE_MASK;
  2988. reset_adapter = status_reg & MFI_RESET_ADAPTER;
  2989. if (instance->disableOnlineCtrlReset ||
  2990. (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
  2991. /* Reset not supported, kill adapter */
  2992. dev_warn(&instance->pdev->dev, "Reset not supported"
  2993. ", killing adapter scsi%d.\n",
  2994. instance->host->host_no);
  2995. megaraid_sas_kill_hba(instance);
  2996. instance->skip_heartbeat_timer_del = 1;
  2997. retval = FAILED;
  2998. goto out;
  2999. }
  3000. /* Let SR-IOV VF & PF sync up if there was a HB failure */
  3001. if (instance->requestorId && !reason) {
  3002. msleep(MEGASAS_OCR_SETTLE_TIME_VF);
  3003. goto transition_to_ready;
  3004. }
  3005. /* Now try to reset the chip */
  3006. for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
  3007. if (instance->instancet->adp_reset
  3008. (instance, instance->reg_set))
  3009. continue;
  3010. transition_to_ready:
  3011. /* Wait for FW to become ready */
  3012. if (megasas_transition_to_ready(instance, 1)) {
  3013. dev_warn(&instance->pdev->dev,
  3014. "Failed to transition controller to ready for "
  3015. "scsi%d.\n", instance->host->host_no);
  3016. if (instance->requestorId && !reason)
  3017. goto fail_kill_adapter;
  3018. else
  3019. continue;
  3020. }
  3021. megasas_reset_reply_desc(instance);
  3022. megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
  3023. if (megasas_ioc_init_fusion(instance)) {
  3024. dev_warn(&instance->pdev->dev,
  3025. "megasas_ioc_init_fusion() failed! for "
  3026. "scsi%d\n", instance->host->host_no);
  3027. if (instance->requestorId && !reason)
  3028. goto fail_kill_adapter;
  3029. else
  3030. continue;
  3031. }
  3032. megasas_refire_mgmt_cmd(instance);
  3033. if (megasas_get_ctrl_info(instance)) {
  3034. dev_info(&instance->pdev->dev,
  3035. "Failed from %s %d\n",
  3036. __func__, __LINE__);
  3037. megaraid_sas_kill_hba(instance);
  3038. retval = FAILED;
  3039. }
  3040. /* Reset load balance info */
  3041. memset(fusion->load_balance_info, 0,
  3042. sizeof(struct LD_LOAD_BALANCE_INFO)
  3043. *MAX_LOGICAL_DRIVES_EXT);
  3044. if (!megasas_get_map_info(instance))
  3045. megasas_sync_map_info(instance);
  3046. megasas_setup_jbod_map(instance);
  3047. shost_for_each_device(sdev, shost)
  3048. megasas_update_sdev_properties(sdev);
  3049. clear_bit(MEGASAS_FUSION_IN_RESET,
  3050. &instance->reset_flags);
  3051. instance->instancet->enable_intr(instance);
  3052. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  3053. /* Restart SR-IOV heartbeat */
  3054. if (instance->requestorId) {
  3055. if (!megasas_sriov_start_heartbeat(instance, 0))
  3056. megasas_start_timer(instance,
  3057. &instance->sriov_heartbeat_timer,
  3058. megasas_sriov_heartbeat_handler,
  3059. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  3060. else
  3061. instance->skip_heartbeat_timer_del = 1;
  3062. }
  3063. /* Adapter reset completed successfully */
  3064. dev_warn(&instance->pdev->dev, "Reset "
  3065. "successful for scsi%d.\n",
  3066. instance->host->host_no);
  3067. if (instance->crash_dump_drv_support &&
  3068. instance->crash_dump_app_support)
  3069. megasas_set_crash_dump_params(instance,
  3070. MR_CRASH_BUF_TURN_ON);
  3071. else
  3072. megasas_set_crash_dump_params(instance,
  3073. MR_CRASH_BUF_TURN_OFF);
  3074. retval = SUCCESS;
  3075. goto out;
  3076. }
  3077. fail_kill_adapter:
  3078. /* Reset failed, kill the adapter */
  3079. dev_warn(&instance->pdev->dev, "Reset failed, killing "
  3080. "adapter scsi%d.\n", instance->host->host_no);
  3081. megaraid_sas_kill_hba(instance);
  3082. instance->skip_heartbeat_timer_del = 1;
  3083. retval = FAILED;
  3084. } else {
  3085. /* For VF: Restart HB timer if we didn't OCR */
  3086. if (instance->requestorId) {
  3087. megasas_start_timer(instance,
  3088. &instance->sriov_heartbeat_timer,
  3089. megasas_sriov_heartbeat_handler,
  3090. MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
  3091. }
  3092. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  3093. instance->instancet->enable_intr(instance);
  3094. atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
  3095. }
  3096. out:
  3097. clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
  3098. mutex_unlock(&instance->reset_mutex);
  3099. return retval;
  3100. }
  3101. /* Fusion Crash dump collection work queue */
  3102. void megasas_fusion_crash_dump_wq(struct work_struct *work)
  3103. {
  3104. struct megasas_instance *instance =
  3105. container_of(work, struct megasas_instance, crash_init);
  3106. u32 status_reg;
  3107. u8 partial_copy = 0;
  3108. status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
  3109. /*
  3110. * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
  3111. * to host crash buffers
  3112. */
  3113. if (instance->drv_buf_index == 0) {
  3114. /* Buffer is already allocated for old Crash dump.
  3115. * Do OCR and do not wait for crash dump collection
  3116. */
  3117. if (instance->drv_buf_alloc) {
  3118. dev_info(&instance->pdev->dev, "earlier crash dump is "
  3119. "not yet copied by application, ignoring this "
  3120. "crash dump and initiating OCR\n");
  3121. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  3122. writel(status_reg,
  3123. &instance->reg_set->outbound_scratch_pad);
  3124. readl(&instance->reg_set->outbound_scratch_pad);
  3125. return;
  3126. }
  3127. megasas_alloc_host_crash_buffer(instance);
  3128. dev_info(&instance->pdev->dev, "Number of host crash buffers "
  3129. "allocated: %d\n", instance->drv_buf_alloc);
  3130. }
  3131. /*
  3132. * Driver has allocated max buffers, which can be allocated
  3133. * and FW has more crash dump data, then driver will
  3134. * ignore the data.
  3135. */
  3136. if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
  3137. dev_info(&instance->pdev->dev, "Driver is done copying "
  3138. "the buffer: %d\n", instance->drv_buf_alloc);
  3139. status_reg |= MFI_STATE_CRASH_DUMP_DONE;
  3140. partial_copy = 1;
  3141. } else {
  3142. memcpy(instance->crash_buf[instance->drv_buf_index],
  3143. instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
  3144. instance->drv_buf_index++;
  3145. status_reg &= ~MFI_STATE_DMADONE;
  3146. }
  3147. if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
  3148. dev_info(&instance->pdev->dev, "Crash Dump is available,number "
  3149. "of copied buffers: %d\n", instance->drv_buf_index);
  3150. instance->fw_crash_buffer_size = instance->drv_buf_index;
  3151. instance->fw_crash_state = AVAILABLE;
  3152. instance->drv_buf_index = 0;
  3153. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  3154. readl(&instance->reg_set->outbound_scratch_pad);
  3155. if (!partial_copy)
  3156. megasas_reset_fusion(instance->host, 0);
  3157. } else {
  3158. writel(status_reg, &instance->reg_set->outbound_scratch_pad);
  3159. readl(&instance->reg_set->outbound_scratch_pad);
  3160. }
  3161. }
  3162. /* Fusion OCR work queue */
  3163. void megasas_fusion_ocr_wq(struct work_struct *work)
  3164. {
  3165. struct megasas_instance *instance =
  3166. container_of(work, struct megasas_instance, work_init);
  3167. megasas_reset_fusion(instance->host, 0);
  3168. }
  3169. struct megasas_instance_template megasas_instance_template_fusion = {
  3170. .enable_intr = megasas_enable_intr_fusion,
  3171. .disable_intr = megasas_disable_intr_fusion,
  3172. .clear_intr = megasas_clear_intr_fusion,
  3173. .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
  3174. .adp_reset = megasas_adp_reset_fusion,
  3175. .check_reset = megasas_check_reset_fusion,
  3176. .service_isr = megasas_isr_fusion,
  3177. .tasklet = megasas_complete_cmd_dpc_fusion,
  3178. .init_adapter = megasas_init_adapter_fusion,
  3179. .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
  3180. .issue_dcmd = megasas_issue_dcmd_fusion,
  3181. };