megaraid_sas.c 85 KB

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