megaraid_sas.c 77 KB

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