megaraid_sas.c 68 KB

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