megaraid_sas.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115
  1. /*
  2. *
  3. * Linux MegaRAID driver for SAS based RAID controllers
  4. *
  5. * Copyright (c) 2003-2005 LSI Logic Corporation.
  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
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * FILE : megaraid_sas.c
  13. * Version : v00.00.03.05
  14. *
  15. * Authors:
  16. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
  17. * Sumant Patro <Sumant.Patro@lsi.com>
  18. *
  19. * List of supported controllers
  20. *
  21. * OEM Product Name VID DID SSVID SSID
  22. * --- ------------ --- --- ---- ----
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/types.h>
  26. #include <linux/pci.h>
  27. #include <linux/list.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/module.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/delay.h>
  33. #include <linux/uio.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/fs.h>
  36. #include <linux/compat.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/mutex.h>
  39. #include <scsi/scsi.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include <scsi/scsi_device.h>
  42. #include <scsi/scsi_host.h>
  43. #include "megaraid_sas.h"
  44. MODULE_LICENSE("GPL");
  45. MODULE_VERSION(MEGASAS_VERSION);
  46. MODULE_AUTHOR("megaraidlinux@lsi.com");
  47. MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
  48. /*
  49. * PCI ID table for all supported controllers
  50. */
  51. static struct pci_device_id megasas_pci_table[] = {
  52. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  53. /* xscale IOP */
  54. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  55. /* ppc IOP */
  56. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  57. /* xscale IOP, vega */
  58. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  59. /* xscale IOP */
  60. {}
  61. };
  62. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  63. static int megasas_mgmt_majorno;
  64. static struct megasas_mgmt_info megasas_mgmt_info;
  65. static struct fasync_struct *megasas_async_queue;
  66. static DEFINE_MUTEX(megasas_async_queue_mutex);
  67. static u32 megasas_dbg_lvl;
  68. /**
  69. * megasas_get_cmd - Get a command from the free pool
  70. * @instance: Adapter soft state
  71. *
  72. * Returns a free command from the pool
  73. */
  74. static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  75. *instance)
  76. {
  77. unsigned long flags;
  78. struct megasas_cmd *cmd = NULL;
  79. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  80. if (!list_empty(&instance->cmd_pool)) {
  81. cmd = list_entry((&instance->cmd_pool)->next,
  82. struct megasas_cmd, list);
  83. list_del_init(&cmd->list);
  84. } else {
  85. printk(KERN_ERR "megasas: Command pool empty!\n");
  86. }
  87. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  88. return cmd;
  89. }
  90. /**
  91. * megasas_return_cmd - Return a cmd to free command pool
  92. * @instance: Adapter soft state
  93. * @cmd: Command packet to be returned to free command pool
  94. */
  95. static inline void
  96. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  97. {
  98. unsigned long flags;
  99. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  100. cmd->scmd = NULL;
  101. list_add_tail(&cmd->list, &instance->cmd_pool);
  102. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  103. }
  104. /**
  105. * The following functions are defined for xscale
  106. * (deviceid : 1064R, PERC5) controllers
  107. */
  108. /**
  109. * megasas_enable_intr_xscale - Enables interrupts
  110. * @regs: MFI register set
  111. */
  112. static inline void
  113. megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
  114. {
  115. writel(1, &(regs)->outbound_intr_mask);
  116. /* Dummy readl to force pci flush */
  117. readl(&regs->outbound_intr_mask);
  118. }
  119. /**
  120. * megasas_disable_intr_xscale -Disables interrupt
  121. * @regs: MFI register set
  122. */
  123. static inline void
  124. megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
  125. {
  126. u32 mask = 0x1f;
  127. writel(mask, &regs->outbound_intr_mask);
  128. /* Dummy readl to force pci flush */
  129. readl(&regs->outbound_intr_mask);
  130. }
  131. /**
  132. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  133. * @regs: MFI register set
  134. */
  135. static u32
  136. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  137. {
  138. return readl(&(regs)->outbound_msg_0);
  139. }
  140. /**
  141. * megasas_clear_interrupt_xscale - Check & clear interrupt
  142. * @regs: MFI register set
  143. */
  144. static int
  145. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  146. {
  147. u32 status;
  148. /*
  149. * Check if it is our interrupt
  150. */
  151. status = readl(&regs->outbound_intr_status);
  152. if (!(status & MFI_OB_INTR_STATUS_MASK)) {
  153. return 1;
  154. }
  155. /*
  156. * Clear the interrupt by writing back the same value
  157. */
  158. writel(status, &regs->outbound_intr_status);
  159. return 0;
  160. }
  161. /**
  162. * megasas_fire_cmd_xscale - Sends command to the FW
  163. * @frame_phys_addr : Physical address of cmd
  164. * @frame_count : Number of frames for the command
  165. * @regs : MFI register set
  166. */
  167. static inline void
  168. megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
  169. {
  170. writel((frame_phys_addr >> 3)|(frame_count),
  171. &(regs)->inbound_queue_port);
  172. }
  173. static struct megasas_instance_template megasas_instance_template_xscale = {
  174. .fire_cmd = megasas_fire_cmd_xscale,
  175. .enable_intr = megasas_enable_intr_xscale,
  176. .disable_intr = megasas_disable_intr_xscale,
  177. .clear_intr = megasas_clear_intr_xscale,
  178. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  179. };
  180. /**
  181. * This is the end of set of functions & definitions specific
  182. * to xscale (deviceid : 1064R, PERC5) controllers
  183. */
  184. /**
  185. * The following functions are defined for ppc (deviceid : 0x60)
  186. * controllers
  187. */
  188. /**
  189. * megasas_enable_intr_ppc - Enables interrupts
  190. * @regs: MFI register set
  191. */
  192. static inline void
  193. megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
  194. {
  195. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  196. writel(~0x80000004, &(regs)->outbound_intr_mask);
  197. /* Dummy readl to force pci flush */
  198. readl(&regs->outbound_intr_mask);
  199. }
  200. /**
  201. * megasas_disable_intr_ppc - Disable interrupt
  202. * @regs: MFI register set
  203. */
  204. static inline void
  205. megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
  206. {
  207. u32 mask = 0xFFFFFFFF;
  208. writel(mask, &regs->outbound_intr_mask);
  209. /* Dummy readl to force pci flush */
  210. readl(&regs->outbound_intr_mask);
  211. }
  212. /**
  213. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  214. * @regs: MFI register set
  215. */
  216. static u32
  217. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  218. {
  219. return readl(&(regs)->outbound_scratch_pad);
  220. }
  221. /**
  222. * megasas_clear_interrupt_ppc - Check & clear interrupt
  223. * @regs: MFI register set
  224. */
  225. static int
  226. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  227. {
  228. u32 status;
  229. /*
  230. * Check if it is our interrupt
  231. */
  232. status = readl(&regs->outbound_intr_status);
  233. if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
  234. return 1;
  235. }
  236. /*
  237. * Clear the interrupt by writing back the same value
  238. */
  239. writel(status, &regs->outbound_doorbell_clear);
  240. return 0;
  241. }
  242. /**
  243. * megasas_fire_cmd_ppc - Sends command to the FW
  244. * @frame_phys_addr : Physical address of cmd
  245. * @frame_count : Number of frames for the command
  246. * @regs : MFI register set
  247. */
  248. static inline void
  249. megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
  250. {
  251. writel((frame_phys_addr | (frame_count<<1))|1,
  252. &(regs)->inbound_queue_port);
  253. }
  254. static struct megasas_instance_template megasas_instance_template_ppc = {
  255. .fire_cmd = megasas_fire_cmd_ppc,
  256. .enable_intr = megasas_enable_intr_ppc,
  257. .disable_intr = megasas_disable_intr_ppc,
  258. .clear_intr = megasas_clear_intr_ppc,
  259. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  260. };
  261. /**
  262. * This is the end of set of functions & definitions
  263. * specific to ppc (deviceid : 0x60) controllers
  264. */
  265. /**
  266. * megasas_issue_polled - Issues a polling command
  267. * @instance: Adapter soft state
  268. * @cmd: Command packet to be issued
  269. *
  270. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  271. */
  272. static int
  273. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  274. {
  275. int i;
  276. u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
  277. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  278. frame_hdr->cmd_status = 0xFF;
  279. frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  280. /*
  281. * Issue the frame using inbound queue port
  282. */
  283. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  284. /*
  285. * Wait for cmd_status to change
  286. */
  287. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
  288. rmb();
  289. msleep(1);
  290. }
  291. if (frame_hdr->cmd_status == 0xff)
  292. return -ETIME;
  293. return 0;
  294. }
  295. /**
  296. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  297. * @instance: Adapter soft state
  298. * @cmd: Command to be issued
  299. *
  300. * This function waits on an event for the command to be returned from ISR.
  301. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  302. * Used to issue ioctl commands.
  303. */
  304. static int
  305. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  306. struct megasas_cmd *cmd)
  307. {
  308. cmd->cmd_status = ENODATA;
  309. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  310. wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
  311. MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
  312. return 0;
  313. }
  314. /**
  315. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  316. * @instance: Adapter soft state
  317. * @cmd_to_abort: Previously issued cmd to be aborted
  318. *
  319. * MFI firmware can abort previously issued AEN comamnd (automatic event
  320. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  321. * cmd and waits for return status.
  322. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  323. */
  324. static int
  325. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  326. struct megasas_cmd *cmd_to_abort)
  327. {
  328. struct megasas_cmd *cmd;
  329. struct megasas_abort_frame *abort_fr;
  330. cmd = megasas_get_cmd(instance);
  331. if (!cmd)
  332. return -1;
  333. abort_fr = &cmd->frame->abort;
  334. /*
  335. * Prepare and issue the abort frame
  336. */
  337. abort_fr->cmd = MFI_CMD_ABORT;
  338. abort_fr->cmd_status = 0xFF;
  339. abort_fr->flags = 0;
  340. abort_fr->abort_context = cmd_to_abort->index;
  341. abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
  342. abort_fr->abort_mfi_phys_addr_hi = 0;
  343. cmd->sync_cmd = 1;
  344. cmd->cmd_status = 0xFF;
  345. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  346. /*
  347. * Wait for this cmd to complete
  348. */
  349. wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
  350. MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
  351. megasas_return_cmd(instance, cmd);
  352. return 0;
  353. }
  354. /**
  355. * megasas_make_sgl32 - Prepares 32-bit SGL
  356. * @instance: Adapter soft state
  357. * @scp: SCSI command from the mid-layer
  358. * @mfi_sgl: SGL to be filled in
  359. *
  360. * If successful, this function returns the number of SG elements. Otherwise,
  361. * it returnes -1.
  362. */
  363. static int
  364. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  365. union megasas_sgl *mfi_sgl)
  366. {
  367. int i;
  368. int sge_count;
  369. struct scatterlist *os_sgl;
  370. /*
  371. * Return 0 if there is no data transfer
  372. */
  373. if (!scp->request_buffer || !scp->request_bufflen)
  374. return 0;
  375. if (!scp->use_sg) {
  376. mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
  377. scp->
  378. request_buffer,
  379. scp->
  380. request_bufflen,
  381. scp->
  382. sc_data_direction);
  383. mfi_sgl->sge32[0].length = scp->request_bufflen;
  384. return 1;
  385. }
  386. os_sgl = (struct scatterlist *)scp->request_buffer;
  387. sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
  388. scp->sc_data_direction);
  389. for (i = 0; i < sge_count; i++, os_sgl++) {
  390. mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
  391. mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
  392. }
  393. return sge_count;
  394. }
  395. /**
  396. * megasas_make_sgl64 - Prepares 64-bit SGL
  397. * @instance: Adapter soft state
  398. * @scp: SCSI command from the mid-layer
  399. * @mfi_sgl: SGL to be filled in
  400. *
  401. * If successful, this function returns the number of SG elements. Otherwise,
  402. * it returnes -1.
  403. */
  404. static int
  405. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  406. union megasas_sgl *mfi_sgl)
  407. {
  408. int i;
  409. int sge_count;
  410. struct scatterlist *os_sgl;
  411. /*
  412. * Return 0 if there is no data transfer
  413. */
  414. if (!scp->request_buffer || !scp->request_bufflen)
  415. return 0;
  416. if (!scp->use_sg) {
  417. mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
  418. scp->
  419. request_buffer,
  420. scp->
  421. request_bufflen,
  422. scp->
  423. sc_data_direction);
  424. mfi_sgl->sge64[0].length = scp->request_bufflen;
  425. return 1;
  426. }
  427. os_sgl = (struct scatterlist *)scp->request_buffer;
  428. sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
  429. scp->sc_data_direction);
  430. for (i = 0; i < sge_count; i++, os_sgl++) {
  431. mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
  432. mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
  433. }
  434. return sge_count;
  435. }
  436. /**
  437. * megasas_get_frame_count - Computes the number of frames
  438. * @sge_count : number of sg elements
  439. *
  440. * Returns the number of frames required for numnber of sge's (sge_count)
  441. */
  442. static u32 megasas_get_frame_count(u8 sge_count)
  443. {
  444. int num_cnt;
  445. int sge_bytes;
  446. u32 sge_sz;
  447. u32 frame_count=0;
  448. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  449. sizeof(struct megasas_sge32);
  450. /*
  451. * Main frame can contain 2 SGEs for 64-bit SGLs and
  452. * 3 SGEs for 32-bit SGLs
  453. */
  454. if (IS_DMA64)
  455. num_cnt = sge_count - 2;
  456. else
  457. num_cnt = sge_count - 3;
  458. if(num_cnt>0){
  459. sge_bytes = sge_sz * num_cnt;
  460. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  461. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  462. }
  463. /* Main frame */
  464. frame_count +=1;
  465. if (frame_count > 7)
  466. frame_count = 8;
  467. return frame_count;
  468. }
  469. /**
  470. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  471. * @instance: Adapter soft state
  472. * @scp: SCSI command
  473. * @cmd: Command to be prepared in
  474. *
  475. * This function prepares CDB commands. These are typcially pass-through
  476. * commands to the devices.
  477. */
  478. static int
  479. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  480. struct megasas_cmd *cmd)
  481. {
  482. u32 is_logical;
  483. u32 device_id;
  484. u16 flags = 0;
  485. struct megasas_pthru_frame *pthru;
  486. is_logical = MEGASAS_IS_LOGICAL(scp);
  487. device_id = MEGASAS_DEV_INDEX(instance, scp);
  488. pthru = (struct megasas_pthru_frame *)cmd->frame;
  489. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  490. flags = MFI_FRAME_DIR_WRITE;
  491. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  492. flags = MFI_FRAME_DIR_READ;
  493. else if (scp->sc_data_direction == PCI_DMA_NONE)
  494. flags = MFI_FRAME_DIR_NONE;
  495. /*
  496. * Prepare the DCDB frame
  497. */
  498. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  499. pthru->cmd_status = 0x0;
  500. pthru->scsi_status = 0x0;
  501. pthru->target_id = device_id;
  502. pthru->lun = scp->device->lun;
  503. pthru->cdb_len = scp->cmd_len;
  504. pthru->timeout = 0;
  505. pthru->flags = flags;
  506. pthru->data_xfer_len = scp->request_bufflen;
  507. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  508. /*
  509. * Construct SGL
  510. */
  511. if (IS_DMA64) {
  512. pthru->flags |= MFI_FRAME_SGL64;
  513. pthru->sge_count = megasas_make_sgl64(instance, scp,
  514. &pthru->sgl);
  515. } else
  516. pthru->sge_count = megasas_make_sgl32(instance, scp,
  517. &pthru->sgl);
  518. /*
  519. * Sense info specific
  520. */
  521. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  522. pthru->sense_buf_phys_addr_hi = 0;
  523. pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  524. /*
  525. * Compute the total number of frames this command consumes. FW uses
  526. * this number to pull sufficient number of frames from host memory.
  527. */
  528. cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
  529. return cmd->frame_count;
  530. }
  531. /**
  532. * megasas_build_ldio - Prepares IOs to logical devices
  533. * @instance: Adapter soft state
  534. * @scp: SCSI command
  535. * @cmd: Command to to be prepared
  536. *
  537. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  538. */
  539. static int
  540. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  541. struct megasas_cmd *cmd)
  542. {
  543. u32 device_id;
  544. u8 sc = scp->cmnd[0];
  545. u16 flags = 0;
  546. struct megasas_io_frame *ldio;
  547. device_id = MEGASAS_DEV_INDEX(instance, scp);
  548. ldio = (struct megasas_io_frame *)cmd->frame;
  549. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  550. flags = MFI_FRAME_DIR_WRITE;
  551. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  552. flags = MFI_FRAME_DIR_READ;
  553. /*
  554. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  555. */
  556. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  557. ldio->cmd_status = 0x0;
  558. ldio->scsi_status = 0x0;
  559. ldio->target_id = device_id;
  560. ldio->timeout = 0;
  561. ldio->reserved_0 = 0;
  562. ldio->pad_0 = 0;
  563. ldio->flags = flags;
  564. ldio->start_lba_hi = 0;
  565. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  566. /*
  567. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  568. */
  569. if (scp->cmd_len == 6) {
  570. ldio->lba_count = (u32) scp->cmnd[4];
  571. ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  572. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  573. ldio->start_lba_lo &= 0x1FFFFF;
  574. }
  575. /*
  576. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  577. */
  578. else if (scp->cmd_len == 10) {
  579. ldio->lba_count = (u32) scp->cmnd[8] |
  580. ((u32) scp->cmnd[7] << 8);
  581. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  582. ((u32) scp->cmnd[3] << 16) |
  583. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  584. }
  585. /*
  586. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  587. */
  588. else if (scp->cmd_len == 12) {
  589. ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
  590. ((u32) scp->cmnd[7] << 16) |
  591. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  592. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  593. ((u32) scp->cmnd[3] << 16) |
  594. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  595. }
  596. /*
  597. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  598. */
  599. else if (scp->cmd_len == 16) {
  600. ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
  601. ((u32) scp->cmnd[11] << 16) |
  602. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  603. ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  604. ((u32) scp->cmnd[7] << 16) |
  605. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  606. ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  607. ((u32) scp->cmnd[3] << 16) |
  608. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  609. }
  610. /*
  611. * Construct SGL
  612. */
  613. if (IS_DMA64) {
  614. ldio->flags |= MFI_FRAME_SGL64;
  615. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  616. } else
  617. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  618. /*
  619. * Sense info specific
  620. */
  621. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  622. ldio->sense_buf_phys_addr_hi = 0;
  623. ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  624. /*
  625. * Compute the total number of frames this command consumes. FW uses
  626. * this number to pull sufficient number of frames from host memory.
  627. */
  628. cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
  629. return cmd->frame_count;
  630. }
  631. /**
  632. * megasas_is_ldio - Checks if the cmd is for logical drive
  633. * @scmd: SCSI command
  634. *
  635. * Called by megasas_queue_command to find out if the command to be queued
  636. * is a logical drive command
  637. */
  638. static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  639. {
  640. if (!MEGASAS_IS_LOGICAL(cmd))
  641. return 0;
  642. switch (cmd->cmnd[0]) {
  643. case READ_10:
  644. case WRITE_10:
  645. case READ_12:
  646. case WRITE_12:
  647. case READ_6:
  648. case WRITE_6:
  649. case READ_16:
  650. case WRITE_16:
  651. return 1;
  652. default:
  653. return 0;
  654. }
  655. }
  656. /**
  657. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  658. * in FW
  659. * @instance: Adapter soft state
  660. */
  661. static inline void
  662. megasas_dump_pending_frames(struct megasas_instance *instance)
  663. {
  664. struct megasas_cmd *cmd;
  665. int i,n;
  666. union megasas_sgl *mfi_sgl;
  667. struct megasas_io_frame *ldio;
  668. struct megasas_pthru_frame *pthru;
  669. u32 sgcount;
  670. u32 max_cmd = instance->max_fw_cmds;
  671. printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  672. printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  673. if (IS_DMA64)
  674. printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  675. else
  676. printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  677. printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  678. for (i = 0; i < max_cmd; i++) {
  679. cmd = instance->cmd_list[i];
  680. if(!cmd->scmd)
  681. continue;
  682. printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  683. if (megasas_is_ldio(cmd->scmd)){
  684. ldio = (struct megasas_io_frame *)cmd->frame;
  685. mfi_sgl = &ldio->sgl;
  686. sgcount = ldio->sge_count;
  687. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
  688. }
  689. else {
  690. pthru = (struct megasas_pthru_frame *) cmd->frame;
  691. mfi_sgl = &pthru->sgl;
  692. sgcount = pthru->sge_count;
  693. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
  694. }
  695. if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
  696. for (n = 0; n < sgcount; n++){
  697. if (IS_DMA64)
  698. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
  699. else
  700. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
  701. }
  702. }
  703. printk(KERN_ERR "\n");
  704. } /*for max_cmd*/
  705. printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  706. for (i = 0; i < max_cmd; i++) {
  707. cmd = instance->cmd_list[i];
  708. if(cmd->sync_cmd == 1){
  709. printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  710. }
  711. }
  712. printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
  713. }
  714. /**
  715. * megasas_queue_command - Queue entry point
  716. * @scmd: SCSI command to be queued
  717. * @done: Callback entry point
  718. */
  719. static int
  720. megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  721. {
  722. u32 frame_count;
  723. struct megasas_cmd *cmd;
  724. struct megasas_instance *instance;
  725. instance = (struct megasas_instance *)
  726. scmd->device->host->hostdata;
  727. /* Don't process if we have already declared adapter dead */
  728. if (instance->hw_crit_error)
  729. return SCSI_MLQUEUE_HOST_BUSY;
  730. scmd->scsi_done = done;
  731. scmd->result = 0;
  732. if (MEGASAS_IS_LOGICAL(scmd) &&
  733. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  734. scmd->result = DID_BAD_TARGET << 16;
  735. goto out_done;
  736. }
  737. cmd = megasas_get_cmd(instance);
  738. if (!cmd)
  739. return SCSI_MLQUEUE_HOST_BUSY;
  740. /*
  741. * Logical drive command
  742. */
  743. if (megasas_is_ldio(scmd))
  744. frame_count = megasas_build_ldio(instance, scmd, cmd);
  745. else
  746. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  747. if (!frame_count)
  748. goto out_return_cmd;
  749. cmd->scmd = scmd;
  750. /*
  751. * Issue the command to the FW
  752. */
  753. atomic_inc(&instance->fw_outstanding);
  754. instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
  755. return 0;
  756. out_return_cmd:
  757. megasas_return_cmd(instance, cmd);
  758. out_done:
  759. done(scmd);
  760. return 0;
  761. }
  762. static int megasas_slave_configure(struct scsi_device *sdev)
  763. {
  764. /*
  765. * Don't export physical disk devices to the disk driver.
  766. *
  767. * FIXME: Currently we don't export them to the midlayer at all.
  768. * That will be fixed once LSI engineers have audited the
  769. * firmware for possible issues.
  770. */
  771. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
  772. return -ENXIO;
  773. /*
  774. * The RAID firmware may require extended timeouts.
  775. */
  776. if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
  777. sdev->timeout = 90 * HZ;
  778. return 0;
  779. }
  780. /**
  781. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  782. * @instance: Adapter soft state
  783. *
  784. * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
  785. * complete all its outstanding commands. Returns error if one or more IOs
  786. * are pending after this time period. It also marks the controller dead.
  787. */
  788. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  789. {
  790. int i;
  791. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  792. for (i = 0; i < wait_time; i++) {
  793. int outstanding = atomic_read(&instance->fw_outstanding);
  794. if (!outstanding)
  795. break;
  796. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  797. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  798. "commands to complete\n",i,outstanding);
  799. }
  800. msleep(1000);
  801. }
  802. if (atomic_read(&instance->fw_outstanding)) {
  803. /*
  804. * Send signal to FW to stop processing any pending cmds.
  805. * The controller will be taken offline by the OS now.
  806. */
  807. writel(MFI_STOP_ADP,
  808. &instance->reg_set->inbound_doorbell);
  809. megasas_dump_pending_frames(instance);
  810. instance->hw_crit_error = 1;
  811. return FAILED;
  812. }
  813. return SUCCESS;
  814. }
  815. /**
  816. * megasas_generic_reset - Generic reset routine
  817. * @scmd: Mid-layer SCSI command
  818. *
  819. * This routine implements a generic reset handler for device, bus and host
  820. * reset requests. Device, bus and host specific reset handlers can use this
  821. * function after they do their specific tasks.
  822. */
  823. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  824. {
  825. int ret_val;
  826. struct megasas_instance *instance;
  827. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  828. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
  829. scmd->serial_number, scmd->cmnd[0]);
  830. if (instance->hw_crit_error) {
  831. printk(KERN_ERR "megasas: cannot recover from previous reset "
  832. "failures\n");
  833. return FAILED;
  834. }
  835. ret_val = megasas_wait_for_outstanding(instance);
  836. if (ret_val == SUCCESS)
  837. printk(KERN_NOTICE "megasas: reset successful \n");
  838. else
  839. printk(KERN_ERR "megasas: failed to do reset\n");
  840. return ret_val;
  841. }
  842. /**
  843. * megasas_reset_device - Device reset handler entry point
  844. */
  845. static int megasas_reset_device(struct scsi_cmnd *scmd)
  846. {
  847. int ret;
  848. /*
  849. * First wait for all commands to complete
  850. */
  851. ret = megasas_generic_reset(scmd);
  852. return ret;
  853. }
  854. /**
  855. * megasas_reset_bus_host - Bus & host reset handler entry point
  856. */
  857. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  858. {
  859. int ret;
  860. /*
  861. * First wait for all commands to complete
  862. */
  863. ret = megasas_generic_reset(scmd);
  864. return ret;
  865. }
  866. /**
  867. * megasas_bios_param - Returns disk geometry for a disk
  868. * @sdev: device handle
  869. * @bdev: block device
  870. * @capacity: drive capacity
  871. * @geom: geometry parameters
  872. */
  873. static int
  874. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  875. sector_t capacity, int geom[])
  876. {
  877. int heads;
  878. int sectors;
  879. sector_t cylinders;
  880. unsigned long tmp;
  881. /* Default heads (64) & sectors (32) */
  882. heads = 64;
  883. sectors = 32;
  884. tmp = heads * sectors;
  885. cylinders = capacity;
  886. sector_div(cylinders, tmp);
  887. /*
  888. * Handle extended translation size for logical drives > 1Gb
  889. */
  890. if (capacity >= 0x200000) {
  891. heads = 255;
  892. sectors = 63;
  893. tmp = heads*sectors;
  894. cylinders = capacity;
  895. sector_div(cylinders, tmp);
  896. }
  897. geom[0] = heads;
  898. geom[1] = sectors;
  899. geom[2] = cylinders;
  900. return 0;
  901. }
  902. /**
  903. * megasas_service_aen - Processes an event notification
  904. * @instance: Adapter soft state
  905. * @cmd: AEN command completed by the ISR
  906. *
  907. * For AEN, driver sends a command down to FW that is held by the FW till an
  908. * event occurs. When an event of interest occurs, FW completes the command
  909. * that it was previously holding.
  910. *
  911. * This routines sends SIGIO signal to processes that have registered with the
  912. * driver for AEN.
  913. */
  914. static void
  915. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  916. {
  917. /*
  918. * Don't signal app if it is just an aborted previously registered aen
  919. */
  920. if (!cmd->abort_aen)
  921. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  922. else
  923. cmd->abort_aen = 0;
  924. instance->aen_cmd = NULL;
  925. megasas_return_cmd(instance, cmd);
  926. }
  927. /*
  928. * Scsi host template for megaraid_sas driver
  929. */
  930. static struct scsi_host_template megasas_template = {
  931. .module = THIS_MODULE,
  932. .name = "LSI Logic SAS based MegaRAID driver",
  933. .proc_name = "megaraid_sas",
  934. .slave_configure = megasas_slave_configure,
  935. .queuecommand = megasas_queue_command,
  936. .eh_device_reset_handler = megasas_reset_device,
  937. .eh_bus_reset_handler = megasas_reset_bus_host,
  938. .eh_host_reset_handler = megasas_reset_bus_host,
  939. .bios_param = megasas_bios_param,
  940. .use_clustering = ENABLE_CLUSTERING,
  941. };
  942. /**
  943. * megasas_complete_int_cmd - Completes an internal command
  944. * @instance: Adapter soft state
  945. * @cmd: Command to be completed
  946. *
  947. * The megasas_issue_blocked_cmd() function waits for a command to complete
  948. * after it issues a command. This function wakes up that waiting routine by
  949. * calling wake_up() on the wait queue.
  950. */
  951. static void
  952. megasas_complete_int_cmd(struct megasas_instance *instance,
  953. struct megasas_cmd *cmd)
  954. {
  955. cmd->cmd_status = cmd->frame->io.cmd_status;
  956. if (cmd->cmd_status == ENODATA) {
  957. cmd->cmd_status = 0;
  958. }
  959. wake_up(&instance->int_cmd_wait_q);
  960. }
  961. /**
  962. * megasas_complete_abort - Completes aborting a command
  963. * @instance: Adapter soft state
  964. * @cmd: Cmd that was issued to abort another cmd
  965. *
  966. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  967. * after it issues an abort on a previously issued command. This function
  968. * wakes up all functions waiting on the same wait queue.
  969. */
  970. static void
  971. megasas_complete_abort(struct megasas_instance *instance,
  972. struct megasas_cmd *cmd)
  973. {
  974. if (cmd->sync_cmd) {
  975. cmd->sync_cmd = 0;
  976. cmd->cmd_status = 0;
  977. wake_up(&instance->abort_cmd_wait_q);
  978. }
  979. return;
  980. }
  981. /**
  982. * megasas_unmap_sgbuf - Unmap SG buffers
  983. * @instance: Adapter soft state
  984. * @cmd: Completed command
  985. */
  986. static void
  987. megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
  988. {
  989. dma_addr_t buf_h;
  990. u8 opcode;
  991. if (cmd->scmd->use_sg) {
  992. pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
  993. cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
  994. return;
  995. }
  996. if (!cmd->scmd->request_bufflen)
  997. return;
  998. opcode = cmd->frame->hdr.cmd;
  999. if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
  1000. if (IS_DMA64)
  1001. buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
  1002. else
  1003. buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
  1004. } else {
  1005. if (IS_DMA64)
  1006. buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
  1007. else
  1008. buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
  1009. }
  1010. pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
  1011. cmd->scmd->sc_data_direction);
  1012. return;
  1013. }
  1014. /**
  1015. * megasas_complete_cmd - Completes a command
  1016. * @instance: Adapter soft state
  1017. * @cmd: Command to be completed
  1018. * @alt_status: If non-zero, use this value as status to
  1019. * SCSI mid-layer instead of the value returned
  1020. * by the FW. This should be used if caller wants
  1021. * an alternate status (as in the case of aborted
  1022. * commands)
  1023. */
  1024. static void
  1025. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1026. u8 alt_status)
  1027. {
  1028. int exception = 0;
  1029. struct megasas_header *hdr = &cmd->frame->hdr;
  1030. if (cmd->scmd) {
  1031. cmd->scmd->SCp.ptr = (char *)0;
  1032. }
  1033. switch (hdr->cmd) {
  1034. case MFI_CMD_PD_SCSI_IO:
  1035. case MFI_CMD_LD_SCSI_IO:
  1036. /*
  1037. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1038. * issued either through an IO path or an IOCTL path. If it
  1039. * was via IOCTL, we will send it to internal completion.
  1040. */
  1041. if (cmd->sync_cmd) {
  1042. cmd->sync_cmd = 0;
  1043. megasas_complete_int_cmd(instance, cmd);
  1044. break;
  1045. }
  1046. case MFI_CMD_LD_READ:
  1047. case MFI_CMD_LD_WRITE:
  1048. if (alt_status) {
  1049. cmd->scmd->result = alt_status << 16;
  1050. exception = 1;
  1051. }
  1052. if (exception) {
  1053. atomic_dec(&instance->fw_outstanding);
  1054. megasas_unmap_sgbuf(instance, cmd);
  1055. cmd->scmd->scsi_done(cmd->scmd);
  1056. megasas_return_cmd(instance, cmd);
  1057. break;
  1058. }
  1059. switch (hdr->cmd_status) {
  1060. case MFI_STAT_OK:
  1061. cmd->scmd->result = DID_OK << 16;
  1062. break;
  1063. case MFI_STAT_SCSI_IO_FAILED:
  1064. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1065. cmd->scmd->result =
  1066. (DID_ERROR << 16) | hdr->scsi_status;
  1067. break;
  1068. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1069. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1070. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1071. memset(cmd->scmd->sense_buffer, 0,
  1072. SCSI_SENSE_BUFFERSIZE);
  1073. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1074. hdr->sense_len);
  1075. cmd->scmd->result |= DRIVER_SENSE << 24;
  1076. }
  1077. break;
  1078. case MFI_STAT_LD_OFFLINE:
  1079. case MFI_STAT_DEVICE_NOT_FOUND:
  1080. cmd->scmd->result = DID_BAD_TARGET << 16;
  1081. break;
  1082. default:
  1083. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1084. hdr->cmd_status);
  1085. cmd->scmd->result = DID_ERROR << 16;
  1086. break;
  1087. }
  1088. atomic_dec(&instance->fw_outstanding);
  1089. megasas_unmap_sgbuf(instance, cmd);
  1090. cmd->scmd->scsi_done(cmd->scmd);
  1091. megasas_return_cmd(instance, cmd);
  1092. break;
  1093. case MFI_CMD_SMP:
  1094. case MFI_CMD_STP:
  1095. case MFI_CMD_DCMD:
  1096. /*
  1097. * See if got an event notification
  1098. */
  1099. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1100. megasas_service_aen(instance, cmd);
  1101. else
  1102. megasas_complete_int_cmd(instance, cmd);
  1103. break;
  1104. case MFI_CMD_ABORT:
  1105. /*
  1106. * Cmd issued to abort another cmd returned
  1107. */
  1108. megasas_complete_abort(instance, cmd);
  1109. break;
  1110. default:
  1111. printk("megasas: Unknown command completed! [0x%X]\n",
  1112. hdr->cmd);
  1113. break;
  1114. }
  1115. }
  1116. /**
  1117. * megasas_deplete_reply_queue - Processes all completed commands
  1118. * @instance: Adapter soft state
  1119. * @alt_status: Alternate status to be returned to
  1120. * SCSI mid-layer instead of the status
  1121. * returned by the FW
  1122. */
  1123. static int
  1124. megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
  1125. {
  1126. /*
  1127. * Check if it is our interrupt
  1128. * Clear the interrupt
  1129. */
  1130. if(instance->instancet->clear_intr(instance->reg_set))
  1131. return IRQ_NONE;
  1132. if (instance->hw_crit_error)
  1133. goto out_done;
  1134. /*
  1135. * Schedule the tasklet for cmd completion
  1136. */
  1137. tasklet_schedule(&instance->isr_tasklet);
  1138. out_done:
  1139. return IRQ_HANDLED;
  1140. }
  1141. /**
  1142. * megasas_isr - isr entry point
  1143. */
  1144. static irqreturn_t megasas_isr(int irq, void *devp)
  1145. {
  1146. return megasas_deplete_reply_queue((struct megasas_instance *)devp,
  1147. DID_OK);
  1148. }
  1149. /**
  1150. * megasas_transition_to_ready - Move the FW to READY state
  1151. * @instance: Adapter soft state
  1152. *
  1153. * During the initialization, FW passes can potentially be in any one of
  1154. * several possible states. If the FW in operational, waiting-for-handshake
  1155. * states, driver must take steps to bring it to ready state. Otherwise, it
  1156. * has to wait for the ready state.
  1157. */
  1158. static int
  1159. megasas_transition_to_ready(struct megasas_instance* instance)
  1160. {
  1161. int i;
  1162. u8 max_wait;
  1163. u32 fw_state;
  1164. u32 cur_state;
  1165. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  1166. if (fw_state != MFI_STATE_READY)
  1167. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  1168. " state\n");
  1169. while (fw_state != MFI_STATE_READY) {
  1170. switch (fw_state) {
  1171. case MFI_STATE_FAULT:
  1172. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  1173. return -ENODEV;
  1174. case MFI_STATE_WAIT_HANDSHAKE:
  1175. /*
  1176. * Set the CLR bit in inbound doorbell
  1177. */
  1178. writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1179. &instance->reg_set->inbound_doorbell);
  1180. max_wait = 2;
  1181. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  1182. break;
  1183. case MFI_STATE_BOOT_MESSAGE_PENDING:
  1184. writel(MFI_INIT_HOTPLUG,
  1185. &instance->reg_set->inbound_doorbell);
  1186. max_wait = 10;
  1187. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  1188. break;
  1189. case MFI_STATE_OPERATIONAL:
  1190. /*
  1191. * Bring it to READY state; assuming max wait 10 secs
  1192. */
  1193. instance->instancet->disable_intr(instance->reg_set);
  1194. writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
  1195. max_wait = 10;
  1196. cur_state = MFI_STATE_OPERATIONAL;
  1197. break;
  1198. case MFI_STATE_UNDEFINED:
  1199. /*
  1200. * This state should not last for more than 2 seconds
  1201. */
  1202. max_wait = 2;
  1203. cur_state = MFI_STATE_UNDEFINED;
  1204. break;
  1205. case MFI_STATE_BB_INIT:
  1206. max_wait = 2;
  1207. cur_state = MFI_STATE_BB_INIT;
  1208. break;
  1209. case MFI_STATE_FW_INIT:
  1210. max_wait = 20;
  1211. cur_state = MFI_STATE_FW_INIT;
  1212. break;
  1213. case MFI_STATE_FW_INIT_2:
  1214. max_wait = 20;
  1215. cur_state = MFI_STATE_FW_INIT_2;
  1216. break;
  1217. case MFI_STATE_DEVICE_SCAN:
  1218. max_wait = 20;
  1219. cur_state = MFI_STATE_DEVICE_SCAN;
  1220. break;
  1221. case MFI_STATE_FLUSH_CACHE:
  1222. max_wait = 20;
  1223. cur_state = MFI_STATE_FLUSH_CACHE;
  1224. break;
  1225. default:
  1226. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  1227. fw_state);
  1228. return -ENODEV;
  1229. }
  1230. /*
  1231. * The cur_state should not last for more than max_wait secs
  1232. */
  1233. for (i = 0; i < (max_wait * 1000); i++) {
  1234. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  1235. MFI_STATE_MASK ;
  1236. if (fw_state == cur_state) {
  1237. msleep(1);
  1238. } else
  1239. break;
  1240. }
  1241. /*
  1242. * Return error if fw_state hasn't changed after max_wait
  1243. */
  1244. if (fw_state == cur_state) {
  1245. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  1246. "in %d secs\n", fw_state, max_wait);
  1247. return -ENODEV;
  1248. }
  1249. };
  1250. printk(KERN_INFO "megasas: FW now in Ready state\n");
  1251. return 0;
  1252. }
  1253. /**
  1254. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  1255. * @instance: Adapter soft state
  1256. */
  1257. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  1258. {
  1259. int i;
  1260. u32 max_cmd = instance->max_fw_cmds;
  1261. struct megasas_cmd *cmd;
  1262. if (!instance->frame_dma_pool)
  1263. return;
  1264. /*
  1265. * Return all frames to pool
  1266. */
  1267. for (i = 0; i < max_cmd; i++) {
  1268. cmd = instance->cmd_list[i];
  1269. if (cmd->frame)
  1270. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  1271. cmd->frame_phys_addr);
  1272. if (cmd->sense)
  1273. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  1274. cmd->sense_phys_addr);
  1275. }
  1276. /*
  1277. * Now destroy the pool itself
  1278. */
  1279. pci_pool_destroy(instance->frame_dma_pool);
  1280. pci_pool_destroy(instance->sense_dma_pool);
  1281. instance->frame_dma_pool = NULL;
  1282. instance->sense_dma_pool = NULL;
  1283. }
  1284. /**
  1285. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  1286. * @instance: Adapter soft state
  1287. *
  1288. * Each command packet has an embedded DMA memory buffer that is used for
  1289. * filling MFI frame and the SG list that immediately follows the frame. This
  1290. * function creates those DMA memory buffers for each command packet by using
  1291. * PCI pool facility.
  1292. */
  1293. static int megasas_create_frame_pool(struct megasas_instance *instance)
  1294. {
  1295. int i;
  1296. u32 max_cmd;
  1297. u32 sge_sz;
  1298. u32 sgl_sz;
  1299. u32 total_sz;
  1300. u32 frame_count;
  1301. struct megasas_cmd *cmd;
  1302. max_cmd = instance->max_fw_cmds;
  1303. /*
  1304. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  1305. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  1306. */
  1307. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1308. sizeof(struct megasas_sge32);
  1309. /*
  1310. * Calculated the number of 64byte frames required for SGL
  1311. */
  1312. sgl_sz = sge_sz * instance->max_num_sge;
  1313. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  1314. /*
  1315. * We need one extra frame for the MFI command
  1316. */
  1317. frame_count++;
  1318. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  1319. /*
  1320. * Use DMA pool facility provided by PCI layer
  1321. */
  1322. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  1323. instance->pdev, total_sz, 64,
  1324. 0);
  1325. if (!instance->frame_dma_pool) {
  1326. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  1327. return -ENOMEM;
  1328. }
  1329. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  1330. instance->pdev, 128, 4, 0);
  1331. if (!instance->sense_dma_pool) {
  1332. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  1333. pci_pool_destroy(instance->frame_dma_pool);
  1334. instance->frame_dma_pool = NULL;
  1335. return -ENOMEM;
  1336. }
  1337. /*
  1338. * Allocate and attach a frame to each of the commands in cmd_list.
  1339. * By making cmd->index as the context instead of the &cmd, we can
  1340. * always use 32bit context regardless of the architecture
  1341. */
  1342. for (i = 0; i < max_cmd; i++) {
  1343. cmd = instance->cmd_list[i];
  1344. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  1345. GFP_KERNEL, &cmd->frame_phys_addr);
  1346. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  1347. GFP_KERNEL, &cmd->sense_phys_addr);
  1348. /*
  1349. * megasas_teardown_frame_pool() takes care of freeing
  1350. * whatever has been allocated
  1351. */
  1352. if (!cmd->frame || !cmd->sense) {
  1353. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  1354. megasas_teardown_frame_pool(instance);
  1355. return -ENOMEM;
  1356. }
  1357. cmd->frame->io.context = cmd->index;
  1358. }
  1359. return 0;
  1360. }
  1361. /**
  1362. * megasas_free_cmds - Free all the cmds in the free cmd pool
  1363. * @instance: Adapter soft state
  1364. */
  1365. static void megasas_free_cmds(struct megasas_instance *instance)
  1366. {
  1367. int i;
  1368. /* First free the MFI frame pool */
  1369. megasas_teardown_frame_pool(instance);
  1370. /* Free all the commands in the cmd_list */
  1371. for (i = 0; i < instance->max_fw_cmds; i++)
  1372. kfree(instance->cmd_list[i]);
  1373. /* Free the cmd_list buffer itself */
  1374. kfree(instance->cmd_list);
  1375. instance->cmd_list = NULL;
  1376. INIT_LIST_HEAD(&instance->cmd_pool);
  1377. }
  1378. /**
  1379. * megasas_alloc_cmds - Allocates the command packets
  1380. * @instance: Adapter soft state
  1381. *
  1382. * Each command that is issued to the FW, whether IO commands from the OS or
  1383. * internal commands like IOCTLs, are wrapped in local data structure called
  1384. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  1385. * the FW.
  1386. *
  1387. * Each frame has a 32-bit field called context (tag). This context is used
  1388. * to get back the megasas_cmd from the frame when a frame gets completed in
  1389. * the ISR. Typically the address of the megasas_cmd itself would be used as
  1390. * the context. But we wanted to keep the differences between 32 and 64 bit
  1391. * systems to the mininum. We always use 32 bit integers for the context. In
  1392. * this driver, the 32 bit values are the indices into an array cmd_list.
  1393. * This array is used only to look up the megasas_cmd given the context. The
  1394. * free commands themselves are maintained in a linked list called cmd_pool.
  1395. */
  1396. static int megasas_alloc_cmds(struct megasas_instance *instance)
  1397. {
  1398. int i;
  1399. int j;
  1400. u32 max_cmd;
  1401. struct megasas_cmd *cmd;
  1402. max_cmd = instance->max_fw_cmds;
  1403. /*
  1404. * instance->cmd_list is an array of struct megasas_cmd pointers.
  1405. * Allocate the dynamic array first and then allocate individual
  1406. * commands.
  1407. */
  1408. instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
  1409. GFP_KERNEL);
  1410. if (!instance->cmd_list) {
  1411. printk(KERN_DEBUG "megasas: out of memory\n");
  1412. return -ENOMEM;
  1413. }
  1414. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
  1415. for (i = 0; i < max_cmd; i++) {
  1416. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  1417. GFP_KERNEL);
  1418. if (!instance->cmd_list[i]) {
  1419. for (j = 0; j < i; j++)
  1420. kfree(instance->cmd_list[j]);
  1421. kfree(instance->cmd_list);
  1422. instance->cmd_list = NULL;
  1423. return -ENOMEM;
  1424. }
  1425. }
  1426. /*
  1427. * Add all the commands to command pool (instance->cmd_pool)
  1428. */
  1429. for (i = 0; i < max_cmd; i++) {
  1430. cmd = instance->cmd_list[i];
  1431. memset(cmd, 0, sizeof(struct megasas_cmd));
  1432. cmd->index = i;
  1433. cmd->instance = instance;
  1434. list_add_tail(&cmd->list, &instance->cmd_pool);
  1435. }
  1436. /*
  1437. * Create a frame pool and assign one frame to each cmd
  1438. */
  1439. if (megasas_create_frame_pool(instance)) {
  1440. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  1441. megasas_free_cmds(instance);
  1442. }
  1443. return 0;
  1444. }
  1445. /**
  1446. * megasas_get_controller_info - Returns FW's controller structure
  1447. * @instance: Adapter soft state
  1448. * @ctrl_info: Controller information structure
  1449. *
  1450. * Issues an internal command (DCMD) to get the FW's controller structure.
  1451. * This information is mainly used to find out the maximum IO transfer per
  1452. * command supported by the FW.
  1453. */
  1454. static int
  1455. megasas_get_ctrl_info(struct megasas_instance *instance,
  1456. struct megasas_ctrl_info *ctrl_info)
  1457. {
  1458. int ret = 0;
  1459. struct megasas_cmd *cmd;
  1460. struct megasas_dcmd_frame *dcmd;
  1461. struct megasas_ctrl_info *ci;
  1462. dma_addr_t ci_h = 0;
  1463. cmd = megasas_get_cmd(instance);
  1464. if (!cmd) {
  1465. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  1466. return -ENOMEM;
  1467. }
  1468. dcmd = &cmd->frame->dcmd;
  1469. ci = pci_alloc_consistent(instance->pdev,
  1470. sizeof(struct megasas_ctrl_info), &ci_h);
  1471. if (!ci) {
  1472. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  1473. megasas_return_cmd(instance, cmd);
  1474. return -ENOMEM;
  1475. }
  1476. memset(ci, 0, sizeof(*ci));
  1477. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1478. dcmd->cmd = MFI_CMD_DCMD;
  1479. dcmd->cmd_status = 0xFF;
  1480. dcmd->sge_count = 1;
  1481. dcmd->flags = MFI_FRAME_DIR_READ;
  1482. dcmd->timeout = 0;
  1483. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  1484. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  1485. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1486. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  1487. if (!megasas_issue_polled(instance, cmd)) {
  1488. ret = 0;
  1489. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  1490. } else {
  1491. ret = -1;
  1492. }
  1493. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  1494. ci, ci_h);
  1495. megasas_return_cmd(instance, cmd);
  1496. return ret;
  1497. }
  1498. /**
  1499. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1500. * @instance_addr: Address of adapter soft state
  1501. *
  1502. * Tasklet to complete cmds
  1503. */
  1504. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1505. {
  1506. u32 producer;
  1507. u32 consumer;
  1508. u32 context;
  1509. struct megasas_cmd *cmd;
  1510. struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
  1511. /* If we have already declared adapter dead, donot complete cmds */
  1512. if (instance->hw_crit_error)
  1513. return;
  1514. producer = *instance->producer;
  1515. consumer = *instance->consumer;
  1516. while (consumer != producer) {
  1517. context = instance->reply_queue[consumer];
  1518. cmd = instance->cmd_list[context];
  1519. megasas_complete_cmd(instance, cmd, DID_OK);
  1520. consumer++;
  1521. if (consumer == (instance->max_fw_cmds + 1)) {
  1522. consumer = 0;
  1523. }
  1524. }
  1525. *instance->consumer = producer;
  1526. }
  1527. /**
  1528. * megasas_init_mfi - Initializes the FW
  1529. * @instance: Adapter soft state
  1530. *
  1531. * This is the main function for initializing MFI firmware.
  1532. */
  1533. static int megasas_init_mfi(struct megasas_instance *instance)
  1534. {
  1535. u32 context_sz;
  1536. u32 reply_q_sz;
  1537. u32 max_sectors_1;
  1538. u32 max_sectors_2;
  1539. struct megasas_register_set __iomem *reg_set;
  1540. struct megasas_cmd *cmd;
  1541. struct megasas_ctrl_info *ctrl_info;
  1542. struct megasas_init_frame *init_frame;
  1543. struct megasas_init_queue_info *initq_info;
  1544. dma_addr_t init_frame_h;
  1545. dma_addr_t initq_info_h;
  1546. /*
  1547. * Map the message registers
  1548. */
  1549. instance->base_addr = pci_resource_start(instance->pdev, 0);
  1550. if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
  1551. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  1552. return -EBUSY;
  1553. }
  1554. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  1555. if (!instance->reg_set) {
  1556. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  1557. goto fail_ioremap;
  1558. }
  1559. reg_set = instance->reg_set;
  1560. switch(instance->pdev->device)
  1561. {
  1562. case PCI_DEVICE_ID_LSI_SAS1078R:
  1563. instance->instancet = &megasas_instance_template_ppc;
  1564. break;
  1565. case PCI_DEVICE_ID_LSI_SAS1064R:
  1566. case PCI_DEVICE_ID_DELL_PERC5:
  1567. default:
  1568. instance->instancet = &megasas_instance_template_xscale;
  1569. break;
  1570. }
  1571. /*
  1572. * We expect the FW state to be READY
  1573. */
  1574. if (megasas_transition_to_ready(instance))
  1575. goto fail_ready_state;
  1576. /*
  1577. * Get various operational parameters from status register
  1578. */
  1579. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  1580. /*
  1581. * Reduce the max supported cmds by 1. This is to ensure that the
  1582. * reply_q_sz (1 more than the max cmd that driver may send)
  1583. * does not exceed max cmds that the FW can support
  1584. */
  1585. instance->max_fw_cmds = instance->max_fw_cmds-1;
  1586. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  1587. 0x10;
  1588. /*
  1589. * Create a pool of commands
  1590. */
  1591. if (megasas_alloc_cmds(instance))
  1592. goto fail_alloc_cmds;
  1593. /*
  1594. * Allocate memory for reply queue. Length of reply queue should
  1595. * be _one_ more than the maximum commands handled by the firmware.
  1596. *
  1597. * Note: When FW completes commands, it places corresponding contex
  1598. * values in this circular reply queue. This circular queue is a fairly
  1599. * typical producer-consumer queue. FW is the producer (of completed
  1600. * commands) and the driver is the consumer.
  1601. */
  1602. context_sz = sizeof(u32);
  1603. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  1604. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  1605. reply_q_sz,
  1606. &instance->reply_queue_h);
  1607. if (!instance->reply_queue) {
  1608. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  1609. goto fail_reply_queue;
  1610. }
  1611. /*
  1612. * Prepare a init frame. Note the init frame points to queue info
  1613. * structure. Each frame has SGL allocated after first 64 bytes. For
  1614. * this frame - since we don't need any SGL - we use SGL's space as
  1615. * queue info structure
  1616. *
  1617. * We will not get a NULL command below. We just created the pool.
  1618. */
  1619. cmd = megasas_get_cmd(instance);
  1620. init_frame = (struct megasas_init_frame *)cmd->frame;
  1621. initq_info = (struct megasas_init_queue_info *)
  1622. ((unsigned long)init_frame + 64);
  1623. init_frame_h = cmd->frame_phys_addr;
  1624. initq_info_h = init_frame_h + 64;
  1625. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  1626. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  1627. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  1628. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  1629. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  1630. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  1631. init_frame->cmd = MFI_CMD_INIT;
  1632. init_frame->cmd_status = 0xFF;
  1633. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  1634. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  1635. /*
  1636. * disable the intr before firing the init frame to FW
  1637. */
  1638. instance->instancet->disable_intr(instance->reg_set);
  1639. /*
  1640. * Issue the init frame in polled mode
  1641. */
  1642. if (megasas_issue_polled(instance, cmd)) {
  1643. printk(KERN_DEBUG "megasas: Failed to init firmware\n");
  1644. goto fail_fw_init;
  1645. }
  1646. megasas_return_cmd(instance, cmd);
  1647. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  1648. /*
  1649. * Compute the max allowed sectors per IO: The controller info has two
  1650. * limits on max sectors. Driver should use the minimum of these two.
  1651. *
  1652. * 1 << stripe_sz_ops.min = max sectors per strip
  1653. *
  1654. * Note that older firmwares ( < FW ver 30) didn't report information
  1655. * to calculate max_sectors_1. So the number ended up as zero always.
  1656. */
  1657. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  1658. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  1659. ctrl_info->max_strips_per_io;
  1660. max_sectors_2 = ctrl_info->max_request_size;
  1661. instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
  1662. ? max_sectors_1 : max_sectors_2;
  1663. } else
  1664. instance->max_sectors_per_req = instance->max_num_sge *
  1665. PAGE_SIZE / 512;
  1666. kfree(ctrl_info);
  1667. /*
  1668. * Setup tasklet for cmd completion
  1669. */
  1670. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  1671. (unsigned long)instance);
  1672. return 0;
  1673. fail_fw_init:
  1674. megasas_return_cmd(instance, cmd);
  1675. pci_free_consistent(instance->pdev, reply_q_sz,
  1676. instance->reply_queue, instance->reply_queue_h);
  1677. fail_reply_queue:
  1678. megasas_free_cmds(instance);
  1679. fail_alloc_cmds:
  1680. fail_ready_state:
  1681. iounmap(instance->reg_set);
  1682. fail_ioremap:
  1683. pci_release_regions(instance->pdev);
  1684. return -EINVAL;
  1685. }
  1686. /**
  1687. * megasas_release_mfi - Reverses the FW initialization
  1688. * @intance: Adapter soft state
  1689. */
  1690. static void megasas_release_mfi(struct megasas_instance *instance)
  1691. {
  1692. u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
  1693. pci_free_consistent(instance->pdev, reply_q_sz,
  1694. instance->reply_queue, instance->reply_queue_h);
  1695. megasas_free_cmds(instance);
  1696. iounmap(instance->reg_set);
  1697. pci_release_regions(instance->pdev);
  1698. }
  1699. /**
  1700. * megasas_get_seq_num - Gets latest event sequence numbers
  1701. * @instance: Adapter soft state
  1702. * @eli: FW event log sequence numbers information
  1703. *
  1704. * FW maintains a log of all events in a non-volatile area. Upper layers would
  1705. * usually find out the latest sequence number of the events, the seq number at
  1706. * the boot etc. They would "read" all the events below the latest seq number
  1707. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  1708. * number), they would subsribe to AEN (asynchronous event notification) and
  1709. * wait for the events to happen.
  1710. */
  1711. static int
  1712. megasas_get_seq_num(struct megasas_instance *instance,
  1713. struct megasas_evt_log_info *eli)
  1714. {
  1715. struct megasas_cmd *cmd;
  1716. struct megasas_dcmd_frame *dcmd;
  1717. struct megasas_evt_log_info *el_info;
  1718. dma_addr_t el_info_h = 0;
  1719. cmd = megasas_get_cmd(instance);
  1720. if (!cmd) {
  1721. return -ENOMEM;
  1722. }
  1723. dcmd = &cmd->frame->dcmd;
  1724. el_info = pci_alloc_consistent(instance->pdev,
  1725. sizeof(struct megasas_evt_log_info),
  1726. &el_info_h);
  1727. if (!el_info) {
  1728. megasas_return_cmd(instance, cmd);
  1729. return -ENOMEM;
  1730. }
  1731. memset(el_info, 0, sizeof(*el_info));
  1732. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1733. dcmd->cmd = MFI_CMD_DCMD;
  1734. dcmd->cmd_status = 0x0;
  1735. dcmd->sge_count = 1;
  1736. dcmd->flags = MFI_FRAME_DIR_READ;
  1737. dcmd->timeout = 0;
  1738. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  1739. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  1740. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  1741. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  1742. megasas_issue_blocked_cmd(instance, cmd);
  1743. /*
  1744. * Copy the data back into callers buffer
  1745. */
  1746. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  1747. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  1748. el_info, el_info_h);
  1749. megasas_return_cmd(instance, cmd);
  1750. return 0;
  1751. }
  1752. /**
  1753. * megasas_register_aen - Registers for asynchronous event notification
  1754. * @instance: Adapter soft state
  1755. * @seq_num: The starting sequence number
  1756. * @class_locale: Class of the event
  1757. *
  1758. * This function subscribes for AEN for events beyond the @seq_num. It requests
  1759. * to be notified if and only if the event is of type @class_locale
  1760. */
  1761. static int
  1762. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  1763. u32 class_locale_word)
  1764. {
  1765. int ret_val;
  1766. struct megasas_cmd *cmd;
  1767. struct megasas_dcmd_frame *dcmd;
  1768. union megasas_evt_class_locale curr_aen;
  1769. union megasas_evt_class_locale prev_aen;
  1770. /*
  1771. * If there an AEN pending already (aen_cmd), check if the
  1772. * class_locale of that pending AEN is inclusive of the new
  1773. * AEN request we currently have. If it is, then we don't have
  1774. * to do anything. In other words, whichever events the current
  1775. * AEN request is subscribing to, have already been subscribed
  1776. * to.
  1777. *
  1778. * If the old_cmd is _not_ inclusive, then we have to abort
  1779. * that command, form a class_locale that is superset of both
  1780. * old and current and re-issue to the FW
  1781. */
  1782. curr_aen.word = class_locale_word;
  1783. if (instance->aen_cmd) {
  1784. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  1785. /*
  1786. * A class whose enum value is smaller is inclusive of all
  1787. * higher values. If a PROGRESS (= -1) was previously
  1788. * registered, then a new registration requests for higher
  1789. * classes need not be sent to FW. They are automatically
  1790. * included.
  1791. *
  1792. * Locale numbers don't have such hierarchy. They are bitmap
  1793. * values
  1794. */
  1795. if ((prev_aen.members.class <= curr_aen.members.class) &&
  1796. !((prev_aen.members.locale & curr_aen.members.locale) ^
  1797. curr_aen.members.locale)) {
  1798. /*
  1799. * Previously issued event registration includes
  1800. * current request. Nothing to do.
  1801. */
  1802. return 0;
  1803. } else {
  1804. curr_aen.members.locale |= prev_aen.members.locale;
  1805. if (prev_aen.members.class < curr_aen.members.class)
  1806. curr_aen.members.class = prev_aen.members.class;
  1807. instance->aen_cmd->abort_aen = 1;
  1808. ret_val = megasas_issue_blocked_abort_cmd(instance,
  1809. instance->
  1810. aen_cmd);
  1811. if (ret_val) {
  1812. printk(KERN_DEBUG "megasas: Failed to abort "
  1813. "previous AEN command\n");
  1814. return ret_val;
  1815. }
  1816. }
  1817. }
  1818. cmd = megasas_get_cmd(instance);
  1819. if (!cmd)
  1820. return -ENOMEM;
  1821. dcmd = &cmd->frame->dcmd;
  1822. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  1823. /*
  1824. * Prepare DCMD for aen registration
  1825. */
  1826. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1827. dcmd->cmd = MFI_CMD_DCMD;
  1828. dcmd->cmd_status = 0x0;
  1829. dcmd->sge_count = 1;
  1830. dcmd->flags = MFI_FRAME_DIR_READ;
  1831. dcmd->timeout = 0;
  1832. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  1833. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  1834. dcmd->mbox.w[0] = seq_num;
  1835. dcmd->mbox.w[1] = curr_aen.word;
  1836. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  1837. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  1838. /*
  1839. * Store reference to the cmd used to register for AEN. When an
  1840. * application wants us to register for AEN, we have to abort this
  1841. * cmd and re-register with a new EVENT LOCALE supplied by that app
  1842. */
  1843. instance->aen_cmd = cmd;
  1844. /*
  1845. * Issue the aen registration frame
  1846. */
  1847. instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
  1848. return 0;
  1849. }
  1850. /**
  1851. * megasas_start_aen - Subscribes to AEN during driver load time
  1852. * @instance: Adapter soft state
  1853. */
  1854. static int megasas_start_aen(struct megasas_instance *instance)
  1855. {
  1856. struct megasas_evt_log_info eli;
  1857. union megasas_evt_class_locale class_locale;
  1858. /*
  1859. * Get the latest sequence number from FW
  1860. */
  1861. memset(&eli, 0, sizeof(eli));
  1862. if (megasas_get_seq_num(instance, &eli))
  1863. return -1;
  1864. /*
  1865. * Register AEN with FW for latest sequence number plus 1
  1866. */
  1867. class_locale.members.reserved = 0;
  1868. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  1869. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  1870. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  1871. class_locale.word);
  1872. }
  1873. /**
  1874. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  1875. * @instance: Adapter soft state
  1876. */
  1877. static int megasas_io_attach(struct megasas_instance *instance)
  1878. {
  1879. struct Scsi_Host *host = instance->host;
  1880. /*
  1881. * Export parameters required by SCSI mid-layer
  1882. */
  1883. host->irq = instance->pdev->irq;
  1884. host->unique_id = instance->unique_id;
  1885. host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1886. host->this_id = instance->init_id;
  1887. host->sg_tablesize = instance->max_num_sge;
  1888. host->max_sectors = instance->max_sectors_per_req;
  1889. host->cmd_per_lun = 128;
  1890. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  1891. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  1892. host->max_lun = MEGASAS_MAX_LUN;
  1893. host->max_cmd_len = 16;
  1894. /*
  1895. * Notify the mid-layer about the new controller
  1896. */
  1897. if (scsi_add_host(host, &instance->pdev->dev)) {
  1898. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  1899. return -ENODEV;
  1900. }
  1901. /*
  1902. * Trigger SCSI to scan our drives
  1903. */
  1904. scsi_scan_host(host);
  1905. return 0;
  1906. }
  1907. /**
  1908. * megasas_probe_one - PCI hotplug entry point
  1909. * @pdev: PCI device structure
  1910. * @id: PCI ids of supported hotplugged adapter
  1911. */
  1912. static int __devinit
  1913. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1914. {
  1915. int rval;
  1916. struct Scsi_Host *host;
  1917. struct megasas_instance *instance;
  1918. /*
  1919. * Announce PCI information
  1920. */
  1921. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  1922. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1923. pdev->subsystem_device);
  1924. printk("bus %d:slot %d:func %d\n",
  1925. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1926. /*
  1927. * PCI prepping: enable device set bus mastering and dma mask
  1928. */
  1929. rval = pci_enable_device(pdev);
  1930. if (rval) {
  1931. return rval;
  1932. }
  1933. pci_set_master(pdev);
  1934. /*
  1935. * All our contollers are capable of performing 64-bit DMA
  1936. */
  1937. if (IS_DMA64) {
  1938. if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
  1939. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
  1940. goto fail_set_dma_mask;
  1941. }
  1942. } else {
  1943. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
  1944. goto fail_set_dma_mask;
  1945. }
  1946. host = scsi_host_alloc(&megasas_template,
  1947. sizeof(struct megasas_instance));
  1948. if (!host) {
  1949. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  1950. goto fail_alloc_instance;
  1951. }
  1952. instance = (struct megasas_instance *)host->hostdata;
  1953. memset(instance, 0, sizeof(*instance));
  1954. instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
  1955. &instance->producer_h);
  1956. instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
  1957. &instance->consumer_h);
  1958. if (!instance->producer || !instance->consumer) {
  1959. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  1960. "producer, consumer\n");
  1961. goto fail_alloc_dma_buf;
  1962. }
  1963. *instance->producer = 0;
  1964. *instance->consumer = 0;
  1965. instance->evt_detail = pci_alloc_consistent(pdev,
  1966. sizeof(struct
  1967. megasas_evt_detail),
  1968. &instance->evt_detail_h);
  1969. if (!instance->evt_detail) {
  1970. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  1971. "event detail structure\n");
  1972. goto fail_alloc_dma_buf;
  1973. }
  1974. /*
  1975. * Initialize locks and queues
  1976. */
  1977. INIT_LIST_HEAD(&instance->cmd_pool);
  1978. atomic_set(&instance->fw_outstanding,0);
  1979. init_waitqueue_head(&instance->int_cmd_wait_q);
  1980. init_waitqueue_head(&instance->abort_cmd_wait_q);
  1981. spin_lock_init(&instance->cmd_pool_lock);
  1982. sema_init(&instance->aen_mutex, 1);
  1983. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  1984. /*
  1985. * Initialize PCI related and misc parameters
  1986. */
  1987. instance->pdev = pdev;
  1988. instance->host = host;
  1989. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  1990. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  1991. megasas_dbg_lvl = 0;
  1992. /*
  1993. * Initialize MFI Firmware
  1994. */
  1995. if (megasas_init_mfi(instance))
  1996. goto fail_init_mfi;
  1997. /*
  1998. * Register IRQ
  1999. */
  2000. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
  2001. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  2002. goto fail_irq;
  2003. }
  2004. instance->instancet->enable_intr(instance->reg_set);
  2005. /*
  2006. * Store instance in PCI softstate
  2007. */
  2008. pci_set_drvdata(pdev, instance);
  2009. /*
  2010. * Add this controller to megasas_mgmt_info structure so that it
  2011. * can be exported to management applications
  2012. */
  2013. megasas_mgmt_info.count++;
  2014. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  2015. megasas_mgmt_info.max_index++;
  2016. /*
  2017. * Initiate AEN (Asynchronous Event Notification)
  2018. */
  2019. if (megasas_start_aen(instance)) {
  2020. printk(KERN_DEBUG "megasas: start aen failed\n");
  2021. goto fail_start_aen;
  2022. }
  2023. /*
  2024. * Register with SCSI mid-layer
  2025. */
  2026. if (megasas_io_attach(instance))
  2027. goto fail_io_attach;
  2028. return 0;
  2029. fail_start_aen:
  2030. fail_io_attach:
  2031. megasas_mgmt_info.count--;
  2032. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  2033. megasas_mgmt_info.max_index--;
  2034. pci_set_drvdata(pdev, NULL);
  2035. instance->instancet->disable_intr(instance->reg_set);
  2036. free_irq(instance->pdev->irq, instance);
  2037. megasas_release_mfi(instance);
  2038. fail_irq:
  2039. fail_init_mfi:
  2040. fail_alloc_dma_buf:
  2041. if (instance->evt_detail)
  2042. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2043. instance->evt_detail,
  2044. instance->evt_detail_h);
  2045. if (instance->producer)
  2046. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2047. instance->producer_h);
  2048. if (instance->consumer)
  2049. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2050. instance->consumer_h);
  2051. scsi_host_put(host);
  2052. fail_alloc_instance:
  2053. fail_set_dma_mask:
  2054. pci_disable_device(pdev);
  2055. return -ENODEV;
  2056. }
  2057. /**
  2058. * megasas_flush_cache - Requests FW to flush all its caches
  2059. * @instance: Adapter soft state
  2060. */
  2061. static void megasas_flush_cache(struct megasas_instance *instance)
  2062. {
  2063. struct megasas_cmd *cmd;
  2064. struct megasas_dcmd_frame *dcmd;
  2065. cmd = megasas_get_cmd(instance);
  2066. if (!cmd)
  2067. return;
  2068. dcmd = &cmd->frame->dcmd;
  2069. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2070. dcmd->cmd = MFI_CMD_DCMD;
  2071. dcmd->cmd_status = 0x0;
  2072. dcmd->sge_count = 0;
  2073. dcmd->flags = MFI_FRAME_DIR_NONE;
  2074. dcmd->timeout = 0;
  2075. dcmd->data_xfer_len = 0;
  2076. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  2077. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  2078. megasas_issue_blocked_cmd(instance, cmd);
  2079. megasas_return_cmd(instance, cmd);
  2080. return;
  2081. }
  2082. /**
  2083. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  2084. * @instance: Adapter soft state
  2085. */
  2086. static void megasas_shutdown_controller(struct megasas_instance *instance)
  2087. {
  2088. struct megasas_cmd *cmd;
  2089. struct megasas_dcmd_frame *dcmd;
  2090. cmd = megasas_get_cmd(instance);
  2091. if (!cmd)
  2092. return;
  2093. if (instance->aen_cmd)
  2094. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  2095. dcmd = &cmd->frame->dcmd;
  2096. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2097. dcmd->cmd = MFI_CMD_DCMD;
  2098. dcmd->cmd_status = 0x0;
  2099. dcmd->sge_count = 0;
  2100. dcmd->flags = MFI_FRAME_DIR_NONE;
  2101. dcmd->timeout = 0;
  2102. dcmd->data_xfer_len = 0;
  2103. dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
  2104. megasas_issue_blocked_cmd(instance, cmd);
  2105. megasas_return_cmd(instance, cmd);
  2106. return;
  2107. }
  2108. /**
  2109. * megasas_detach_one - PCI hot"un"plug entry point
  2110. * @pdev: PCI device structure
  2111. */
  2112. static void megasas_detach_one(struct pci_dev *pdev)
  2113. {
  2114. int i;
  2115. struct Scsi_Host *host;
  2116. struct megasas_instance *instance;
  2117. instance = pci_get_drvdata(pdev);
  2118. host = instance->host;
  2119. scsi_remove_host(instance->host);
  2120. megasas_flush_cache(instance);
  2121. megasas_shutdown_controller(instance);
  2122. tasklet_kill(&instance->isr_tasklet);
  2123. /*
  2124. * Take the instance off the instance array. Note that we will not
  2125. * decrement the max_index. We let this array be sparse array
  2126. */
  2127. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2128. if (megasas_mgmt_info.instance[i] == instance) {
  2129. megasas_mgmt_info.count--;
  2130. megasas_mgmt_info.instance[i] = NULL;
  2131. break;
  2132. }
  2133. }
  2134. pci_set_drvdata(instance->pdev, NULL);
  2135. instance->instancet->disable_intr(instance->reg_set);
  2136. free_irq(instance->pdev->irq, instance);
  2137. megasas_release_mfi(instance);
  2138. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2139. instance->evt_detail, instance->evt_detail_h);
  2140. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2141. instance->producer_h);
  2142. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2143. instance->consumer_h);
  2144. scsi_host_put(host);
  2145. pci_set_drvdata(pdev, NULL);
  2146. pci_disable_device(pdev);
  2147. return;
  2148. }
  2149. /**
  2150. * megasas_shutdown - Shutdown entry point
  2151. * @device: Generic device structure
  2152. */
  2153. static void megasas_shutdown(struct pci_dev *pdev)
  2154. {
  2155. struct megasas_instance *instance = pci_get_drvdata(pdev);
  2156. megasas_flush_cache(instance);
  2157. }
  2158. /**
  2159. * megasas_mgmt_open - char node "open" entry point
  2160. */
  2161. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  2162. {
  2163. /*
  2164. * Allow only those users with admin rights
  2165. */
  2166. if (!capable(CAP_SYS_ADMIN))
  2167. return -EACCES;
  2168. return 0;
  2169. }
  2170. /**
  2171. * megasas_mgmt_release - char node "release" entry point
  2172. */
  2173. static int megasas_mgmt_release(struct inode *inode, struct file *filep)
  2174. {
  2175. filep->private_data = NULL;
  2176. fasync_helper(-1, filep, 0, &megasas_async_queue);
  2177. return 0;
  2178. }
  2179. /**
  2180. * megasas_mgmt_fasync - Async notifier registration from applications
  2181. *
  2182. * This function adds the calling process to a driver global queue. When an
  2183. * event occurs, SIGIO will be sent to all processes in this queue.
  2184. */
  2185. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  2186. {
  2187. int rc;
  2188. mutex_lock(&megasas_async_queue_mutex);
  2189. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  2190. mutex_unlock(&megasas_async_queue_mutex);
  2191. if (rc >= 0) {
  2192. /* For sanity check when we get ioctl */
  2193. filep->private_data = filep;
  2194. return 0;
  2195. }
  2196. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  2197. return rc;
  2198. }
  2199. /**
  2200. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  2201. * @instance: Adapter soft state
  2202. * @argp: User's ioctl packet
  2203. */
  2204. static int
  2205. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  2206. struct megasas_iocpacket __user * user_ioc,
  2207. struct megasas_iocpacket *ioc)
  2208. {
  2209. struct megasas_sge32 *kern_sge32;
  2210. struct megasas_cmd *cmd;
  2211. void *kbuff_arr[MAX_IOCTL_SGE];
  2212. dma_addr_t buf_handle = 0;
  2213. int error = 0, i;
  2214. void *sense = NULL;
  2215. dma_addr_t sense_handle;
  2216. u32 *sense_ptr;
  2217. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  2218. if (ioc->sge_count > MAX_IOCTL_SGE) {
  2219. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  2220. ioc->sge_count, MAX_IOCTL_SGE);
  2221. return -EINVAL;
  2222. }
  2223. cmd = megasas_get_cmd(instance);
  2224. if (!cmd) {
  2225. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  2226. return -ENOMEM;
  2227. }
  2228. /*
  2229. * User's IOCTL packet has 2 frames (maximum). Copy those two
  2230. * frames into our cmd's frames. cmd->frame's context will get
  2231. * overwritten when we copy from user's frames. So set that value
  2232. * alone separately
  2233. */
  2234. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  2235. cmd->frame->hdr.context = cmd->index;
  2236. /*
  2237. * The management interface between applications and the fw uses
  2238. * MFI frames. E.g, RAID configuration changes, LD property changes
  2239. * etc are accomplishes through different kinds of MFI frames. The
  2240. * driver needs to care only about substituting user buffers with
  2241. * kernel buffers in SGLs. The location of SGL is embedded in the
  2242. * struct iocpacket itself.
  2243. */
  2244. kern_sge32 = (struct megasas_sge32 *)
  2245. ((unsigned long)cmd->frame + ioc->sgl_off);
  2246. /*
  2247. * For each user buffer, create a mirror buffer and copy in
  2248. */
  2249. for (i = 0; i < ioc->sge_count; i++) {
  2250. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  2251. ioc->sgl[i].iov_len,
  2252. &buf_handle, GFP_KERNEL);
  2253. if (!kbuff_arr[i]) {
  2254. printk(KERN_DEBUG "megasas: Failed to alloc "
  2255. "kernel SGL buffer for IOCTL \n");
  2256. error = -ENOMEM;
  2257. goto out;
  2258. }
  2259. /*
  2260. * We don't change the dma_coherent_mask, so
  2261. * pci_alloc_consistent only returns 32bit addresses
  2262. */
  2263. kern_sge32[i].phys_addr = (u32) buf_handle;
  2264. kern_sge32[i].length = ioc->sgl[i].iov_len;
  2265. /*
  2266. * We created a kernel buffer corresponding to the
  2267. * user buffer. Now copy in from the user buffer
  2268. */
  2269. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  2270. (u32) (ioc->sgl[i].iov_len))) {
  2271. error = -EFAULT;
  2272. goto out;
  2273. }
  2274. }
  2275. if (ioc->sense_len) {
  2276. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  2277. &sense_handle, GFP_KERNEL);
  2278. if (!sense) {
  2279. error = -ENOMEM;
  2280. goto out;
  2281. }
  2282. sense_ptr =
  2283. (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
  2284. *sense_ptr = sense_handle;
  2285. }
  2286. /*
  2287. * Set the sync_cmd flag so that the ISR knows not to complete this
  2288. * cmd to the SCSI mid-layer
  2289. */
  2290. cmd->sync_cmd = 1;
  2291. megasas_issue_blocked_cmd(instance, cmd);
  2292. cmd->sync_cmd = 0;
  2293. /*
  2294. * copy out the kernel buffers to user buffers
  2295. */
  2296. for (i = 0; i < ioc->sge_count; i++) {
  2297. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  2298. ioc->sgl[i].iov_len)) {
  2299. error = -EFAULT;
  2300. goto out;
  2301. }
  2302. }
  2303. /*
  2304. * copy out the sense
  2305. */
  2306. if (ioc->sense_len) {
  2307. /*
  2308. * sense_ptr points to the location that has the user
  2309. * sense buffer address
  2310. */
  2311. sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
  2312. ioc->sense_off);
  2313. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  2314. sense, ioc->sense_len)) {
  2315. error = -EFAULT;
  2316. goto out;
  2317. }
  2318. }
  2319. /*
  2320. * copy the status codes returned by the fw
  2321. */
  2322. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  2323. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  2324. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  2325. error = -EFAULT;
  2326. }
  2327. out:
  2328. if (sense) {
  2329. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  2330. sense, sense_handle);
  2331. }
  2332. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  2333. dma_free_coherent(&instance->pdev->dev,
  2334. kern_sge32[i].length,
  2335. kbuff_arr[i], kern_sge32[i].phys_addr);
  2336. }
  2337. megasas_return_cmd(instance, cmd);
  2338. return error;
  2339. }
  2340. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  2341. {
  2342. int i;
  2343. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2344. if ((megasas_mgmt_info.instance[i]) &&
  2345. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  2346. return megasas_mgmt_info.instance[i];
  2347. }
  2348. return NULL;
  2349. }
  2350. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  2351. {
  2352. struct megasas_iocpacket __user *user_ioc =
  2353. (struct megasas_iocpacket __user *)arg;
  2354. struct megasas_iocpacket *ioc;
  2355. struct megasas_instance *instance;
  2356. int error;
  2357. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  2358. if (!ioc)
  2359. return -ENOMEM;
  2360. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  2361. error = -EFAULT;
  2362. goto out_kfree_ioc;
  2363. }
  2364. instance = megasas_lookup_instance(ioc->host_no);
  2365. if (!instance) {
  2366. error = -ENODEV;
  2367. goto out_kfree_ioc;
  2368. }
  2369. /*
  2370. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  2371. */
  2372. if (down_interruptible(&instance->ioctl_sem)) {
  2373. error = -ERESTARTSYS;
  2374. goto out_kfree_ioc;
  2375. }
  2376. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  2377. up(&instance->ioctl_sem);
  2378. out_kfree_ioc:
  2379. kfree(ioc);
  2380. return error;
  2381. }
  2382. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  2383. {
  2384. struct megasas_instance *instance;
  2385. struct megasas_aen aen;
  2386. int error;
  2387. if (file->private_data != file) {
  2388. printk(KERN_DEBUG "megasas: fasync_helper was not "
  2389. "called first\n");
  2390. return -EINVAL;
  2391. }
  2392. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  2393. return -EFAULT;
  2394. instance = megasas_lookup_instance(aen.host_no);
  2395. if (!instance)
  2396. return -ENODEV;
  2397. down(&instance->aen_mutex);
  2398. error = megasas_register_aen(instance, aen.seq_num,
  2399. aen.class_locale_word);
  2400. up(&instance->aen_mutex);
  2401. return error;
  2402. }
  2403. /**
  2404. * megasas_mgmt_ioctl - char node ioctl entry point
  2405. */
  2406. static long
  2407. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2408. {
  2409. switch (cmd) {
  2410. case MEGASAS_IOC_FIRMWARE:
  2411. return megasas_mgmt_ioctl_fw(file, arg);
  2412. case MEGASAS_IOC_GET_AEN:
  2413. return megasas_mgmt_ioctl_aen(file, arg);
  2414. }
  2415. return -ENOTTY;
  2416. }
  2417. #ifdef CONFIG_COMPAT
  2418. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  2419. {
  2420. struct compat_megasas_iocpacket __user *cioc =
  2421. (struct compat_megasas_iocpacket __user *)arg;
  2422. struct megasas_iocpacket __user *ioc =
  2423. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  2424. int i;
  2425. int error = 0;
  2426. if (clear_user(ioc, sizeof(*ioc)))
  2427. return -EFAULT;
  2428. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  2429. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  2430. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  2431. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  2432. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  2433. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  2434. return -EFAULT;
  2435. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  2436. compat_uptr_t ptr;
  2437. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  2438. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  2439. copy_in_user(&ioc->sgl[i].iov_len,
  2440. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  2441. return -EFAULT;
  2442. }
  2443. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  2444. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  2445. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  2446. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  2447. return -EFAULT;
  2448. }
  2449. return error;
  2450. }
  2451. static long
  2452. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  2453. unsigned long arg)
  2454. {
  2455. switch (cmd) {
  2456. case MEGASAS_IOC_FIRMWARE32:
  2457. return megasas_mgmt_compat_ioctl_fw(file, arg);
  2458. case MEGASAS_IOC_GET_AEN:
  2459. return megasas_mgmt_ioctl_aen(file, arg);
  2460. }
  2461. return -ENOTTY;
  2462. }
  2463. #endif
  2464. /*
  2465. * File operations structure for management interface
  2466. */
  2467. static const struct file_operations megasas_mgmt_fops = {
  2468. .owner = THIS_MODULE,
  2469. .open = megasas_mgmt_open,
  2470. .release = megasas_mgmt_release,
  2471. .fasync = megasas_mgmt_fasync,
  2472. .unlocked_ioctl = megasas_mgmt_ioctl,
  2473. #ifdef CONFIG_COMPAT
  2474. .compat_ioctl = megasas_mgmt_compat_ioctl,
  2475. #endif
  2476. };
  2477. /*
  2478. * PCI hotplug support registration structure
  2479. */
  2480. static struct pci_driver megasas_pci_driver = {
  2481. .name = "megaraid_sas",
  2482. .id_table = megasas_pci_table,
  2483. .probe = megasas_probe_one,
  2484. .remove = __devexit_p(megasas_detach_one),
  2485. .shutdown = megasas_shutdown,
  2486. };
  2487. /*
  2488. * Sysfs driver attributes
  2489. */
  2490. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  2491. {
  2492. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  2493. MEGASAS_VERSION);
  2494. }
  2495. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  2496. static ssize_t
  2497. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  2498. {
  2499. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  2500. MEGASAS_RELDATE);
  2501. }
  2502. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  2503. NULL);
  2504. static ssize_t
  2505. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  2506. {
  2507. return sprintf(buf,"%u",megasas_dbg_lvl);
  2508. }
  2509. static ssize_t
  2510. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  2511. {
  2512. int retval = count;
  2513. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  2514. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  2515. retval = -EINVAL;
  2516. }
  2517. return retval;
  2518. }
  2519. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
  2520. megasas_sysfs_set_dbg_lvl);
  2521. /**
  2522. * megasas_init - Driver load entry point
  2523. */
  2524. static int __init megasas_init(void)
  2525. {
  2526. int rval;
  2527. /*
  2528. * Announce driver version and other information
  2529. */
  2530. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  2531. MEGASAS_EXT_VERSION);
  2532. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  2533. /*
  2534. * Register character device node
  2535. */
  2536. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  2537. if (rval < 0) {
  2538. printk(KERN_DEBUG "megasas: failed to open device node\n");
  2539. return rval;
  2540. }
  2541. megasas_mgmt_majorno = rval;
  2542. /*
  2543. * Register ourselves as PCI hotplug module
  2544. */
  2545. rval = pci_register_driver(&megasas_pci_driver);
  2546. if (rval) {
  2547. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  2548. goto err_pcidrv;
  2549. }
  2550. rval = driver_create_file(&megasas_pci_driver.driver,
  2551. &driver_attr_version);
  2552. if (rval)
  2553. goto err_dcf_attr_ver;
  2554. rval = driver_create_file(&megasas_pci_driver.driver,
  2555. &driver_attr_release_date);
  2556. if (rval)
  2557. goto err_dcf_rel_date;
  2558. rval = driver_create_file(&megasas_pci_driver.driver,
  2559. &driver_attr_dbg_lvl);
  2560. if (rval)
  2561. goto err_dcf_dbg_lvl;
  2562. return rval;
  2563. err_dcf_dbg_lvl:
  2564. driver_remove_file(&megasas_pci_driver.driver,
  2565. &driver_attr_release_date);
  2566. err_dcf_rel_date:
  2567. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  2568. err_dcf_attr_ver:
  2569. pci_unregister_driver(&megasas_pci_driver);
  2570. err_pcidrv:
  2571. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  2572. return rval;
  2573. }
  2574. /**
  2575. * megasas_exit - Driver unload entry point
  2576. */
  2577. static void __exit megasas_exit(void)
  2578. {
  2579. driver_remove_file(&megasas_pci_driver.driver,
  2580. &driver_attr_dbg_lvl);
  2581. driver_remove_file(&megasas_pci_driver.driver,
  2582. &driver_attr_release_date);
  2583. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  2584. pci_unregister_driver(&megasas_pci_driver);
  2585. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  2586. }
  2587. module_init(megasas_init);
  2588. module_exit(megasas_exit);