megaraid_sas.c 68 KB

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