megaraid_sas.c 88 KB

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