megaraid_sas.c 70 KB

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