megaraid_sas.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800
  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/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,
  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 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. * 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 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 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 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 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 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. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
  636. scmd->serial_number, scmd->cmnd[0]);
  637. if (instance->hw_crit_error) {
  638. printk(KERN_ERR "megasas: cannot recover from previous reset "
  639. "failures\n");
  640. return FAILED;
  641. }
  642. ret_val = megasas_wait_for_outstanding(instance);
  643. if (ret_val == SUCCESS)
  644. printk(KERN_NOTICE "megasas: reset successful \n");
  645. else
  646. printk(KERN_ERR "megasas: failed to do reset\n");
  647. return ret_val;
  648. }
  649. static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  650. {
  651. unsigned long seconds;
  652. if (scmd->SCp.ptr) {
  653. seconds = (jiffies - scmd->SCp.sent_command) / HZ;
  654. if (seconds < 90) {
  655. return EH_RESET_TIMER;
  656. } else {
  657. return EH_NOT_HANDLED;
  658. }
  659. }
  660. return EH_HANDLED;
  661. }
  662. /**
  663. * megasas_reset_device - Device reset handler entry point
  664. */
  665. static int megasas_reset_device(struct scsi_cmnd *scmd)
  666. {
  667. int ret;
  668. /*
  669. * First wait for all commands to complete
  670. */
  671. ret = megasas_generic_reset(scmd);
  672. return ret;
  673. }
  674. /**
  675. * megasas_reset_bus_host - Bus & host reset handler entry point
  676. */
  677. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  678. {
  679. int ret;
  680. /*
  681. * Frist wait for all commands to complete
  682. */
  683. ret = megasas_generic_reset(scmd);
  684. return ret;
  685. }
  686. /**
  687. * megasas_service_aen - Processes an event notification
  688. * @instance: Adapter soft state
  689. * @cmd: AEN command completed by the ISR
  690. *
  691. * For AEN, driver sends a command down to FW that is held by the FW till an
  692. * event occurs. When an event of interest occurs, FW completes the command
  693. * that it was previously holding.
  694. *
  695. * This routines sends SIGIO signal to processes that have registered with the
  696. * driver for AEN.
  697. */
  698. static void
  699. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  700. {
  701. /*
  702. * Don't signal app if it is just an aborted previously registered aen
  703. */
  704. if (!cmd->abort_aen)
  705. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  706. else
  707. cmd->abort_aen = 0;
  708. instance->aen_cmd = NULL;
  709. megasas_return_cmd(instance, cmd);
  710. }
  711. /*
  712. * Scsi host template for megaraid_sas driver
  713. */
  714. static struct scsi_host_template megasas_template = {
  715. .module = THIS_MODULE,
  716. .name = "LSI Logic SAS based MegaRAID driver",
  717. .proc_name = "megaraid_sas",
  718. .queuecommand = megasas_queue_command,
  719. .eh_device_reset_handler = megasas_reset_device,
  720. .eh_bus_reset_handler = megasas_reset_bus_host,
  721. .eh_host_reset_handler = megasas_reset_bus_host,
  722. .eh_timed_out = megasas_reset_timer,
  723. .use_clustering = ENABLE_CLUSTERING,
  724. };
  725. /**
  726. * megasas_complete_int_cmd - Completes an internal command
  727. * @instance: Adapter soft state
  728. * @cmd: Command to be completed
  729. *
  730. * The megasas_issue_blocked_cmd() function waits for a command to complete
  731. * after it issues a command. This function wakes up that waiting routine by
  732. * calling wake_up() on the wait queue.
  733. */
  734. static void
  735. megasas_complete_int_cmd(struct megasas_instance *instance,
  736. struct megasas_cmd *cmd)
  737. {
  738. cmd->cmd_status = cmd->frame->io.cmd_status;
  739. if (cmd->cmd_status == ENODATA) {
  740. cmd->cmd_status = 0;
  741. }
  742. wake_up(&instance->int_cmd_wait_q);
  743. }
  744. /**
  745. * megasas_complete_abort - Completes aborting a command
  746. * @instance: Adapter soft state
  747. * @cmd: Cmd that was issued to abort another cmd
  748. *
  749. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  750. * after it issues an abort on a previously issued command. This function
  751. * wakes up all functions waiting on the same wait queue.
  752. */
  753. static void
  754. megasas_complete_abort(struct megasas_instance *instance,
  755. struct megasas_cmd *cmd)
  756. {
  757. if (cmd->sync_cmd) {
  758. cmd->sync_cmd = 0;
  759. cmd->cmd_status = 0;
  760. wake_up(&instance->abort_cmd_wait_q);
  761. }
  762. return;
  763. }
  764. /**
  765. * megasas_unmap_sgbuf - Unmap SG buffers
  766. * @instance: Adapter soft state
  767. * @cmd: Completed command
  768. */
  769. static void
  770. megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
  771. {
  772. dma_addr_t buf_h;
  773. u8 opcode;
  774. if (cmd->scmd->use_sg) {
  775. pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
  776. cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
  777. return;
  778. }
  779. if (!cmd->scmd->request_bufflen)
  780. return;
  781. opcode = cmd->frame->hdr.cmd;
  782. if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
  783. if (IS_DMA64)
  784. buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
  785. else
  786. buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
  787. } else {
  788. if (IS_DMA64)
  789. buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
  790. else
  791. buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
  792. }
  793. pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
  794. cmd->scmd->sc_data_direction);
  795. return;
  796. }
  797. /**
  798. * megasas_complete_cmd - Completes a command
  799. * @instance: Adapter soft state
  800. * @cmd: Command to be completed
  801. * @alt_status: If non-zero, use this value as status to
  802. * SCSI mid-layer instead of the value returned
  803. * by the FW. This should be used if caller wants
  804. * an alternate status (as in the case of aborted
  805. * commands)
  806. */
  807. static void
  808. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  809. u8 alt_status)
  810. {
  811. int exception = 0;
  812. struct megasas_header *hdr = &cmd->frame->hdr;
  813. unsigned long flags;
  814. if (cmd->scmd) {
  815. cmd->scmd->SCp.ptr = (char *)0;
  816. }
  817. switch (hdr->cmd) {
  818. case MFI_CMD_PD_SCSI_IO:
  819. case MFI_CMD_LD_SCSI_IO:
  820. /*
  821. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  822. * issued either through an IO path or an IOCTL path. If it
  823. * was via IOCTL, we will send it to internal completion.
  824. */
  825. if (cmd->sync_cmd) {
  826. cmd->sync_cmd = 0;
  827. megasas_complete_int_cmd(instance, cmd);
  828. break;
  829. }
  830. /*
  831. * Don't export physical disk devices to mid-layer.
  832. */
  833. if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
  834. (hdr->cmd_status == MFI_STAT_OK) &&
  835. (cmd->scmd->cmnd[0] == INQUIRY)) {
  836. if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
  837. TYPE_DISK) {
  838. cmd->scmd->result = DID_BAD_TARGET << 16;
  839. exception = 1;
  840. }
  841. }
  842. case MFI_CMD_LD_READ:
  843. case MFI_CMD_LD_WRITE:
  844. if (alt_status) {
  845. cmd->scmd->result = alt_status << 16;
  846. exception = 1;
  847. }
  848. if (exception) {
  849. spin_lock_irqsave(&instance->instance_lock, flags);
  850. instance->fw_outstanding--;
  851. spin_unlock_irqrestore(&instance->instance_lock, flags);
  852. megasas_unmap_sgbuf(instance, cmd);
  853. cmd->scmd->scsi_done(cmd->scmd);
  854. megasas_return_cmd(instance, cmd);
  855. break;
  856. }
  857. switch (hdr->cmd_status) {
  858. case MFI_STAT_OK:
  859. cmd->scmd->result = DID_OK << 16;
  860. break;
  861. case MFI_STAT_SCSI_IO_FAILED:
  862. case MFI_STAT_LD_INIT_IN_PROGRESS:
  863. cmd->scmd->result =
  864. (DID_ERROR << 16) | hdr->scsi_status;
  865. break;
  866. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  867. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  868. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  869. memset(cmd->scmd->sense_buffer, 0,
  870. SCSI_SENSE_BUFFERSIZE);
  871. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  872. hdr->sense_len);
  873. cmd->scmd->result |= DRIVER_SENSE << 24;
  874. }
  875. break;
  876. case MFI_STAT_LD_OFFLINE:
  877. case MFI_STAT_DEVICE_NOT_FOUND:
  878. cmd->scmd->result = DID_BAD_TARGET << 16;
  879. break;
  880. default:
  881. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  882. hdr->cmd_status);
  883. cmd->scmd->result = DID_ERROR << 16;
  884. break;
  885. }
  886. spin_lock_irqsave(&instance->instance_lock, flags);
  887. instance->fw_outstanding--;
  888. spin_unlock_irqrestore(&instance->instance_lock, flags);
  889. megasas_unmap_sgbuf(instance, cmd);
  890. cmd->scmd->scsi_done(cmd->scmd);
  891. megasas_return_cmd(instance, cmd);
  892. break;
  893. case MFI_CMD_SMP:
  894. case MFI_CMD_STP:
  895. case MFI_CMD_DCMD:
  896. /*
  897. * See if got an event notification
  898. */
  899. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  900. megasas_service_aen(instance, cmd);
  901. else
  902. megasas_complete_int_cmd(instance, cmd);
  903. break;
  904. case MFI_CMD_ABORT:
  905. /*
  906. * Cmd issued to abort another cmd returned
  907. */
  908. megasas_complete_abort(instance, cmd);
  909. break;
  910. default:
  911. printk("megasas: Unknown command completed! [0x%X]\n",
  912. hdr->cmd);
  913. break;
  914. }
  915. }
  916. /**
  917. * megasas_deplete_reply_queue - Processes all completed commands
  918. * @instance: Adapter soft state
  919. * @alt_status: Alternate status to be returned to
  920. * SCSI mid-layer instead of the status
  921. * returned by the FW
  922. */
  923. static int
  924. megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
  925. {
  926. u32 status;
  927. u32 producer;
  928. u32 consumer;
  929. u32 context;
  930. struct megasas_cmd *cmd;
  931. /*
  932. * Check if it is our interrupt
  933. */
  934. status = readl(&instance->reg_set->outbound_intr_status);
  935. if (!(status & MFI_OB_INTR_STATUS_MASK)) {
  936. return IRQ_NONE;
  937. }
  938. /*
  939. * Clear the interrupt by writing back the same value
  940. */
  941. writel(status, &instance->reg_set->outbound_intr_status);
  942. producer = *instance->producer;
  943. consumer = *instance->consumer;
  944. while (consumer != producer) {
  945. context = instance->reply_queue[consumer];
  946. cmd = instance->cmd_list[context];
  947. megasas_complete_cmd(instance, cmd, alt_status);
  948. consumer++;
  949. if (consumer == (instance->max_fw_cmds + 1)) {
  950. consumer = 0;
  951. }
  952. }
  953. *instance->consumer = producer;
  954. return IRQ_HANDLED;
  955. }
  956. /**
  957. * megasas_isr - isr entry point
  958. */
  959. static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
  960. {
  961. return megasas_deplete_reply_queue((struct megasas_instance *)devp,
  962. DID_OK);
  963. }
  964. /**
  965. * megasas_transition_to_ready - Move the FW to READY state
  966. * @reg_set: MFI register set
  967. *
  968. * During the initialization, FW passes can potentially be in any one of
  969. * several possible states. If the FW in operational, waiting-for-handshake
  970. * states, driver must take steps to bring it to ready state. Otherwise, it
  971. * has to wait for the ready state.
  972. */
  973. static int
  974. megasas_transition_to_ready(struct megasas_register_set __iomem * reg_set)
  975. {
  976. int i;
  977. u8 max_wait;
  978. u32 fw_state;
  979. u32 cur_state;
  980. fw_state = readl(&reg_set->outbound_msg_0) & MFI_STATE_MASK;
  981. while (fw_state != MFI_STATE_READY) {
  982. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  983. " state\n");
  984. switch (fw_state) {
  985. case MFI_STATE_FAULT:
  986. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  987. return -ENODEV;
  988. case MFI_STATE_WAIT_HANDSHAKE:
  989. /*
  990. * Set the CLR bit in inbound doorbell
  991. */
  992. writel(MFI_INIT_CLEAR_HANDSHAKE,
  993. &reg_set->inbound_doorbell);
  994. max_wait = 2;
  995. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  996. break;
  997. case MFI_STATE_OPERATIONAL:
  998. /*
  999. * Bring it to READY state; assuming max wait 2 secs
  1000. */
  1001. megasas_disable_intr(reg_set);
  1002. writel(MFI_INIT_READY, &reg_set->inbound_doorbell);
  1003. max_wait = 10;
  1004. cur_state = MFI_STATE_OPERATIONAL;
  1005. break;
  1006. case MFI_STATE_UNDEFINED:
  1007. /*
  1008. * This state should not last for more than 2 seconds
  1009. */
  1010. max_wait = 2;
  1011. cur_state = MFI_STATE_UNDEFINED;
  1012. break;
  1013. case MFI_STATE_BB_INIT:
  1014. max_wait = 2;
  1015. cur_state = MFI_STATE_BB_INIT;
  1016. break;
  1017. case MFI_STATE_FW_INIT:
  1018. max_wait = 20;
  1019. cur_state = MFI_STATE_FW_INIT;
  1020. break;
  1021. case MFI_STATE_FW_INIT_2:
  1022. max_wait = 20;
  1023. cur_state = MFI_STATE_FW_INIT_2;
  1024. break;
  1025. case MFI_STATE_DEVICE_SCAN:
  1026. max_wait = 20;
  1027. cur_state = MFI_STATE_DEVICE_SCAN;
  1028. break;
  1029. case MFI_STATE_FLUSH_CACHE:
  1030. max_wait = 20;
  1031. cur_state = MFI_STATE_FLUSH_CACHE;
  1032. break;
  1033. default:
  1034. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  1035. fw_state);
  1036. return -ENODEV;
  1037. }
  1038. /*
  1039. * The cur_state should not last for more than max_wait secs
  1040. */
  1041. for (i = 0; i < (max_wait * 1000); i++) {
  1042. fw_state = MFI_STATE_MASK &
  1043. readl(&reg_set->outbound_msg_0);
  1044. if (fw_state == cur_state) {
  1045. msleep(1);
  1046. } else
  1047. break;
  1048. }
  1049. /*
  1050. * Return error if fw_state hasn't changed after max_wait
  1051. */
  1052. if (fw_state == cur_state) {
  1053. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  1054. "in %d secs\n", fw_state, max_wait);
  1055. return -ENODEV;
  1056. }
  1057. };
  1058. return 0;
  1059. }
  1060. /**
  1061. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  1062. * @instance: Adapter soft state
  1063. */
  1064. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  1065. {
  1066. int i;
  1067. u32 max_cmd = instance->max_fw_cmds;
  1068. struct megasas_cmd *cmd;
  1069. if (!instance->frame_dma_pool)
  1070. return;
  1071. /*
  1072. * Return all frames to pool
  1073. */
  1074. for (i = 0; i < max_cmd; i++) {
  1075. cmd = instance->cmd_list[i];
  1076. if (cmd->frame)
  1077. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  1078. cmd->frame_phys_addr);
  1079. if (cmd->sense)
  1080. pci_pool_free(instance->sense_dma_pool, cmd->frame,
  1081. cmd->sense_phys_addr);
  1082. }
  1083. /*
  1084. * Now destroy the pool itself
  1085. */
  1086. pci_pool_destroy(instance->frame_dma_pool);
  1087. pci_pool_destroy(instance->sense_dma_pool);
  1088. instance->frame_dma_pool = NULL;
  1089. instance->sense_dma_pool = NULL;
  1090. }
  1091. /**
  1092. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  1093. * @instance: Adapter soft state
  1094. *
  1095. * Each command packet has an embedded DMA memory buffer that is used for
  1096. * filling MFI frame and the SG list that immediately follows the frame. This
  1097. * function creates those DMA memory buffers for each command packet by using
  1098. * PCI pool facility.
  1099. */
  1100. static int megasas_create_frame_pool(struct megasas_instance *instance)
  1101. {
  1102. int i;
  1103. u32 max_cmd;
  1104. u32 sge_sz;
  1105. u32 sgl_sz;
  1106. u32 total_sz;
  1107. u32 frame_count;
  1108. struct megasas_cmd *cmd;
  1109. max_cmd = instance->max_fw_cmds;
  1110. /*
  1111. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  1112. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  1113. */
  1114. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1115. sizeof(struct megasas_sge32);
  1116. /*
  1117. * Calculated the number of 64byte frames required for SGL
  1118. */
  1119. sgl_sz = sge_sz * instance->max_num_sge;
  1120. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  1121. /*
  1122. * We need one extra frame for the MFI command
  1123. */
  1124. frame_count++;
  1125. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  1126. /*
  1127. * Use DMA pool facility provided by PCI layer
  1128. */
  1129. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  1130. instance->pdev, total_sz, 64,
  1131. 0);
  1132. if (!instance->frame_dma_pool) {
  1133. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  1134. return -ENOMEM;
  1135. }
  1136. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  1137. instance->pdev, 128, 4, 0);
  1138. if (!instance->sense_dma_pool) {
  1139. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  1140. pci_pool_destroy(instance->frame_dma_pool);
  1141. instance->frame_dma_pool = NULL;
  1142. return -ENOMEM;
  1143. }
  1144. /*
  1145. * Allocate and attach a frame to each of the commands in cmd_list.
  1146. * By making cmd->index as the context instead of the &cmd, we can
  1147. * always use 32bit context regardless of the architecture
  1148. */
  1149. for (i = 0; i < max_cmd; i++) {
  1150. cmd = instance->cmd_list[i];
  1151. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  1152. GFP_KERNEL, &cmd->frame_phys_addr);
  1153. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  1154. GFP_KERNEL, &cmd->sense_phys_addr);
  1155. /*
  1156. * megasas_teardown_frame_pool() takes care of freeing
  1157. * whatever has been allocated
  1158. */
  1159. if (!cmd->frame || !cmd->sense) {
  1160. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  1161. megasas_teardown_frame_pool(instance);
  1162. return -ENOMEM;
  1163. }
  1164. cmd->frame->io.context = cmd->index;
  1165. }
  1166. return 0;
  1167. }
  1168. /**
  1169. * megasas_free_cmds - Free all the cmds in the free cmd pool
  1170. * @instance: Adapter soft state
  1171. */
  1172. static void megasas_free_cmds(struct megasas_instance *instance)
  1173. {
  1174. int i;
  1175. /* First free the MFI frame pool */
  1176. megasas_teardown_frame_pool(instance);
  1177. /* Free all the commands in the cmd_list */
  1178. for (i = 0; i < instance->max_fw_cmds; i++)
  1179. kfree(instance->cmd_list[i]);
  1180. /* Free the cmd_list buffer itself */
  1181. kfree(instance->cmd_list);
  1182. instance->cmd_list = NULL;
  1183. INIT_LIST_HEAD(&instance->cmd_pool);
  1184. }
  1185. /**
  1186. * megasas_alloc_cmds - Allocates the command packets
  1187. * @instance: Adapter soft state
  1188. *
  1189. * Each command that is issued to the FW, whether IO commands from the OS or
  1190. * internal commands like IOCTLs, are wrapped in local data structure called
  1191. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  1192. * the FW.
  1193. *
  1194. * Each frame has a 32-bit field called context (tag). This context is used
  1195. * to get back the megasas_cmd from the frame when a frame gets completed in
  1196. * the ISR. Typically the address of the megasas_cmd itself would be used as
  1197. * the context. But we wanted to keep the differences between 32 and 64 bit
  1198. * systems to the mininum. We always use 32 bit integers for the context. In
  1199. * this driver, the 32 bit values are the indices into an array cmd_list.
  1200. * This array is used only to look up the megasas_cmd given the context. The
  1201. * free commands themselves are maintained in a linked list called cmd_pool.
  1202. */
  1203. static int megasas_alloc_cmds(struct megasas_instance *instance)
  1204. {
  1205. int i;
  1206. int j;
  1207. u32 max_cmd;
  1208. struct megasas_cmd *cmd;
  1209. max_cmd = instance->max_fw_cmds;
  1210. /*
  1211. * instance->cmd_list is an array of struct megasas_cmd pointers.
  1212. * Allocate the dynamic array first and then allocate individual
  1213. * commands.
  1214. */
  1215. instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
  1216. GFP_KERNEL);
  1217. if (!instance->cmd_list) {
  1218. printk(KERN_DEBUG "megasas: out of memory\n");
  1219. return -ENOMEM;
  1220. }
  1221. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
  1222. for (i = 0; i < max_cmd; i++) {
  1223. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  1224. GFP_KERNEL);
  1225. if (!instance->cmd_list[i]) {
  1226. for (j = 0; j < i; j++)
  1227. kfree(instance->cmd_list[j]);
  1228. kfree(instance->cmd_list);
  1229. instance->cmd_list = NULL;
  1230. return -ENOMEM;
  1231. }
  1232. }
  1233. /*
  1234. * Add all the commands to command pool (instance->cmd_pool)
  1235. */
  1236. for (i = 0; i < max_cmd; i++) {
  1237. cmd = instance->cmd_list[i];
  1238. memset(cmd, 0, sizeof(struct megasas_cmd));
  1239. cmd->index = i;
  1240. cmd->instance = instance;
  1241. list_add_tail(&cmd->list, &instance->cmd_pool);
  1242. }
  1243. /*
  1244. * Create a frame pool and assign one frame to each cmd
  1245. */
  1246. if (megasas_create_frame_pool(instance)) {
  1247. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  1248. megasas_free_cmds(instance);
  1249. }
  1250. return 0;
  1251. }
  1252. /**
  1253. * megasas_get_controller_info - Returns FW's controller structure
  1254. * @instance: Adapter soft state
  1255. * @ctrl_info: Controller information structure
  1256. *
  1257. * Issues an internal command (DCMD) to get the FW's controller structure.
  1258. * This information is mainly used to find out the maximum IO transfer per
  1259. * command supported by the FW.
  1260. */
  1261. static int
  1262. megasas_get_ctrl_info(struct megasas_instance *instance,
  1263. struct megasas_ctrl_info *ctrl_info)
  1264. {
  1265. int ret = 0;
  1266. struct megasas_cmd *cmd;
  1267. struct megasas_dcmd_frame *dcmd;
  1268. struct megasas_ctrl_info *ci;
  1269. dma_addr_t ci_h = 0;
  1270. cmd = megasas_get_cmd(instance);
  1271. if (!cmd) {
  1272. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  1273. return -ENOMEM;
  1274. }
  1275. dcmd = &cmd->frame->dcmd;
  1276. ci = pci_alloc_consistent(instance->pdev,
  1277. sizeof(struct megasas_ctrl_info), &ci_h);
  1278. if (!ci) {
  1279. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  1280. megasas_return_cmd(instance, cmd);
  1281. return -ENOMEM;
  1282. }
  1283. memset(ci, 0, sizeof(*ci));
  1284. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1285. dcmd->cmd = MFI_CMD_DCMD;
  1286. dcmd->cmd_status = 0xFF;
  1287. dcmd->sge_count = 1;
  1288. dcmd->flags = MFI_FRAME_DIR_READ;
  1289. dcmd->timeout = 0;
  1290. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  1291. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  1292. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1293. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  1294. if (!megasas_issue_polled(instance, cmd)) {
  1295. ret = 0;
  1296. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  1297. } else {
  1298. ret = -1;
  1299. }
  1300. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  1301. ci, ci_h);
  1302. megasas_return_cmd(instance, cmd);
  1303. return ret;
  1304. }
  1305. /**
  1306. * megasas_init_mfi - Initializes the FW
  1307. * @instance: Adapter soft state
  1308. *
  1309. * This is the main function for initializing MFI firmware.
  1310. */
  1311. static int megasas_init_mfi(struct megasas_instance *instance)
  1312. {
  1313. u32 context_sz;
  1314. u32 reply_q_sz;
  1315. u32 max_sectors_1;
  1316. u32 max_sectors_2;
  1317. struct megasas_register_set __iomem *reg_set;
  1318. struct megasas_cmd *cmd;
  1319. struct megasas_ctrl_info *ctrl_info;
  1320. struct megasas_init_frame *init_frame;
  1321. struct megasas_init_queue_info *initq_info;
  1322. dma_addr_t init_frame_h;
  1323. dma_addr_t initq_info_h;
  1324. /*
  1325. * Map the message registers
  1326. */
  1327. instance->base_addr = pci_resource_start(instance->pdev, 0);
  1328. if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
  1329. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  1330. return -EBUSY;
  1331. }
  1332. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  1333. if (!instance->reg_set) {
  1334. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  1335. goto fail_ioremap;
  1336. }
  1337. reg_set = instance->reg_set;
  1338. /*
  1339. * We expect the FW state to be READY
  1340. */
  1341. if (megasas_transition_to_ready(instance->reg_set))
  1342. goto fail_ready_state;
  1343. /*
  1344. * Get various operational parameters from status register
  1345. */
  1346. instance->max_fw_cmds = readl(&reg_set->outbound_msg_0) & 0x00FFFF;
  1347. instance->max_num_sge = (readl(&reg_set->outbound_msg_0) & 0xFF0000) >>
  1348. 0x10;
  1349. /*
  1350. * Create a pool of commands
  1351. */
  1352. if (megasas_alloc_cmds(instance))
  1353. goto fail_alloc_cmds;
  1354. /*
  1355. * Allocate memory for reply queue. Length of reply queue should
  1356. * be _one_ more than the maximum commands handled by the firmware.
  1357. *
  1358. * Note: When FW completes commands, it places corresponding contex
  1359. * values in this circular reply queue. This circular queue is a fairly
  1360. * typical producer-consumer queue. FW is the producer (of completed
  1361. * commands) and the driver is the consumer.
  1362. */
  1363. context_sz = sizeof(u32);
  1364. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  1365. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  1366. reply_q_sz,
  1367. &instance->reply_queue_h);
  1368. if (!instance->reply_queue) {
  1369. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  1370. goto fail_reply_queue;
  1371. }
  1372. /*
  1373. * Prepare a init frame. Note the init frame points to queue info
  1374. * structure. Each frame has SGL allocated after first 64 bytes. For
  1375. * this frame - since we don't need any SGL - we use SGL's space as
  1376. * queue info structure
  1377. *
  1378. * We will not get a NULL command below. We just created the pool.
  1379. */
  1380. cmd = megasas_get_cmd(instance);
  1381. init_frame = (struct megasas_init_frame *)cmd->frame;
  1382. initq_info = (struct megasas_init_queue_info *)
  1383. ((unsigned long)init_frame + 64);
  1384. init_frame_h = cmd->frame_phys_addr;
  1385. initq_info_h = init_frame_h + 64;
  1386. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  1387. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  1388. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  1389. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  1390. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  1391. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  1392. init_frame->cmd = MFI_CMD_INIT;
  1393. init_frame->cmd_status = 0xFF;
  1394. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  1395. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  1396. /*
  1397. * Issue the init frame in polled mode
  1398. */
  1399. if (megasas_issue_polled(instance, cmd)) {
  1400. printk(KERN_DEBUG "megasas: Failed to init firmware\n");
  1401. goto fail_fw_init;
  1402. }
  1403. megasas_return_cmd(instance, cmd);
  1404. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  1405. /*
  1406. * Compute the max allowed sectors per IO: The controller info has two
  1407. * limits on max sectors. Driver should use the minimum of these two.
  1408. *
  1409. * 1 << stripe_sz_ops.min = max sectors per strip
  1410. *
  1411. * Note that older firmwares ( < FW ver 30) didn't report information
  1412. * to calculate max_sectors_1. So the number ended up as zero always.
  1413. */
  1414. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  1415. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  1416. ctrl_info->max_strips_per_io;
  1417. max_sectors_2 = ctrl_info->max_request_size;
  1418. instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
  1419. ? max_sectors_1 : max_sectors_2;
  1420. } else
  1421. instance->max_sectors_per_req = instance->max_num_sge *
  1422. PAGE_SIZE / 512;
  1423. kfree(ctrl_info);
  1424. return 0;
  1425. fail_fw_init:
  1426. megasas_return_cmd(instance, cmd);
  1427. pci_free_consistent(instance->pdev, reply_q_sz,
  1428. instance->reply_queue, instance->reply_queue_h);
  1429. fail_reply_queue:
  1430. megasas_free_cmds(instance);
  1431. fail_alloc_cmds:
  1432. fail_ready_state:
  1433. iounmap(instance->reg_set);
  1434. fail_ioremap:
  1435. pci_release_regions(instance->pdev);
  1436. return -EINVAL;
  1437. }
  1438. /**
  1439. * megasas_release_mfi - Reverses the FW initialization
  1440. * @intance: Adapter soft state
  1441. */
  1442. static void megasas_release_mfi(struct megasas_instance *instance)
  1443. {
  1444. u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
  1445. pci_free_consistent(instance->pdev, reply_q_sz,
  1446. instance->reply_queue, instance->reply_queue_h);
  1447. megasas_free_cmds(instance);
  1448. iounmap(instance->reg_set);
  1449. pci_release_regions(instance->pdev);
  1450. }
  1451. /**
  1452. * megasas_get_seq_num - Gets latest event sequence numbers
  1453. * @instance: Adapter soft state
  1454. * @eli: FW event log sequence numbers information
  1455. *
  1456. * FW maintains a log of all events in a non-volatile area. Upper layers would
  1457. * usually find out the latest sequence number of the events, the seq number at
  1458. * the boot etc. They would "read" all the events below the latest seq number
  1459. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  1460. * number), they would subsribe to AEN (asynchronous event notification) and
  1461. * wait for the events to happen.
  1462. */
  1463. static int
  1464. megasas_get_seq_num(struct megasas_instance *instance,
  1465. struct megasas_evt_log_info *eli)
  1466. {
  1467. struct megasas_cmd *cmd;
  1468. struct megasas_dcmd_frame *dcmd;
  1469. struct megasas_evt_log_info *el_info;
  1470. dma_addr_t el_info_h = 0;
  1471. cmd = megasas_get_cmd(instance);
  1472. if (!cmd) {
  1473. return -ENOMEM;
  1474. }
  1475. dcmd = &cmd->frame->dcmd;
  1476. el_info = pci_alloc_consistent(instance->pdev,
  1477. sizeof(struct megasas_evt_log_info),
  1478. &el_info_h);
  1479. if (!el_info) {
  1480. megasas_return_cmd(instance, cmd);
  1481. return -ENOMEM;
  1482. }
  1483. memset(el_info, 0, sizeof(*el_info));
  1484. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1485. dcmd->cmd = MFI_CMD_DCMD;
  1486. dcmd->cmd_status = 0x0;
  1487. dcmd->sge_count = 1;
  1488. dcmd->flags = MFI_FRAME_DIR_READ;
  1489. dcmd->timeout = 0;
  1490. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  1491. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  1492. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  1493. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  1494. megasas_issue_blocked_cmd(instance, cmd);
  1495. /*
  1496. * Copy the data back into callers buffer
  1497. */
  1498. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  1499. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  1500. el_info, el_info_h);
  1501. megasas_return_cmd(instance, cmd);
  1502. return 0;
  1503. }
  1504. /**
  1505. * megasas_register_aen - Registers for asynchronous event notification
  1506. * @instance: Adapter soft state
  1507. * @seq_num: The starting sequence number
  1508. * @class_locale: Class of the event
  1509. *
  1510. * This function subscribes for AEN for events beyond the @seq_num. It requests
  1511. * to be notified if and only if the event is of type @class_locale
  1512. */
  1513. static int
  1514. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  1515. u32 class_locale_word)
  1516. {
  1517. int ret_val;
  1518. struct megasas_cmd *cmd;
  1519. struct megasas_dcmd_frame *dcmd;
  1520. union megasas_evt_class_locale curr_aen;
  1521. union megasas_evt_class_locale prev_aen;
  1522. /*
  1523. * If there an AEN pending already (aen_cmd), check if the
  1524. * class_locale of that pending AEN is inclusive of the new
  1525. * AEN request we currently have. If it is, then we don't have
  1526. * to do anything. In other words, whichever events the current
  1527. * AEN request is subscribing to, have already been subscribed
  1528. * to.
  1529. *
  1530. * If the old_cmd is _not_ inclusive, then we have to abort
  1531. * that command, form a class_locale that is superset of both
  1532. * old and current and re-issue to the FW
  1533. */
  1534. curr_aen.word = class_locale_word;
  1535. if (instance->aen_cmd) {
  1536. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  1537. /*
  1538. * A class whose enum value is smaller is inclusive of all
  1539. * higher values. If a PROGRESS (= -1) was previously
  1540. * registered, then a new registration requests for higher
  1541. * classes need not be sent to FW. They are automatically
  1542. * included.
  1543. *
  1544. * Locale numbers don't have such hierarchy. They are bitmap
  1545. * values
  1546. */
  1547. if ((prev_aen.members.class <= curr_aen.members.class) &&
  1548. !((prev_aen.members.locale & curr_aen.members.locale) ^
  1549. curr_aen.members.locale)) {
  1550. /*
  1551. * Previously issued event registration includes
  1552. * current request. Nothing to do.
  1553. */
  1554. return 0;
  1555. } else {
  1556. curr_aen.members.locale |= prev_aen.members.locale;
  1557. if (prev_aen.members.class < curr_aen.members.class)
  1558. curr_aen.members.class = prev_aen.members.class;
  1559. instance->aen_cmd->abort_aen = 1;
  1560. ret_val = megasas_issue_blocked_abort_cmd(instance,
  1561. instance->
  1562. aen_cmd);
  1563. if (ret_val) {
  1564. printk(KERN_DEBUG "megasas: Failed to abort "
  1565. "previous AEN command\n");
  1566. return ret_val;
  1567. }
  1568. }
  1569. }
  1570. cmd = megasas_get_cmd(instance);
  1571. if (!cmd)
  1572. return -ENOMEM;
  1573. dcmd = &cmd->frame->dcmd;
  1574. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  1575. /*
  1576. * Prepare DCMD for aen registration
  1577. */
  1578. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1579. dcmd->cmd = MFI_CMD_DCMD;
  1580. dcmd->cmd_status = 0x0;
  1581. dcmd->sge_count = 1;
  1582. dcmd->flags = MFI_FRAME_DIR_READ;
  1583. dcmd->timeout = 0;
  1584. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  1585. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  1586. dcmd->mbox.w[0] = seq_num;
  1587. dcmd->mbox.w[1] = curr_aen.word;
  1588. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  1589. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  1590. /*
  1591. * Store reference to the cmd used to register for AEN. When an
  1592. * application wants us to register for AEN, we have to abort this
  1593. * cmd and re-register with a new EVENT LOCALE supplied by that app
  1594. */
  1595. instance->aen_cmd = cmd;
  1596. /*
  1597. * Issue the aen registration frame
  1598. */
  1599. writel(cmd->frame_phys_addr >> 3,
  1600. &instance->reg_set->inbound_queue_port);
  1601. return 0;
  1602. }
  1603. /**
  1604. * megasas_start_aen - Subscribes to AEN during driver load time
  1605. * @instance: Adapter soft state
  1606. */
  1607. static int megasas_start_aen(struct megasas_instance *instance)
  1608. {
  1609. struct megasas_evt_log_info eli;
  1610. union megasas_evt_class_locale class_locale;
  1611. /*
  1612. * Get the latest sequence number from FW
  1613. */
  1614. memset(&eli, 0, sizeof(eli));
  1615. if (megasas_get_seq_num(instance, &eli))
  1616. return -1;
  1617. /*
  1618. * Register AEN with FW for latest sequence number plus 1
  1619. */
  1620. class_locale.members.reserved = 0;
  1621. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  1622. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  1623. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  1624. class_locale.word);
  1625. }
  1626. /**
  1627. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  1628. * @instance: Adapter soft state
  1629. */
  1630. static int megasas_io_attach(struct megasas_instance *instance)
  1631. {
  1632. struct Scsi_Host *host = instance->host;
  1633. /*
  1634. * Export parameters required by SCSI mid-layer
  1635. */
  1636. host->irq = instance->pdev->irq;
  1637. host->unique_id = instance->unique_id;
  1638. host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1639. host->this_id = instance->init_id;
  1640. host->sg_tablesize = instance->max_num_sge;
  1641. host->max_sectors = instance->max_sectors_per_req;
  1642. host->cmd_per_lun = 128;
  1643. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  1644. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  1645. host->max_lun = MEGASAS_MAX_LUN;
  1646. /*
  1647. * Notify the mid-layer about the new controller
  1648. */
  1649. if (scsi_add_host(host, &instance->pdev->dev)) {
  1650. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  1651. return -ENODEV;
  1652. }
  1653. /*
  1654. * Trigger SCSI to scan our drives
  1655. */
  1656. scsi_scan_host(host);
  1657. return 0;
  1658. }
  1659. /**
  1660. * megasas_probe_one - PCI hotplug entry point
  1661. * @pdev: PCI device structure
  1662. * @id: PCI ids of supported hotplugged adapter
  1663. */
  1664. static int __devinit
  1665. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  1666. {
  1667. int rval;
  1668. struct Scsi_Host *host;
  1669. struct megasas_instance *instance;
  1670. /*
  1671. * Announce PCI information
  1672. */
  1673. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  1674. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1675. pdev->subsystem_device);
  1676. printk("bus %d:slot %d:func %d\n",
  1677. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  1678. /*
  1679. * PCI prepping: enable device set bus mastering and dma mask
  1680. */
  1681. rval = pci_enable_device(pdev);
  1682. if (rval) {
  1683. return rval;
  1684. }
  1685. pci_set_master(pdev);
  1686. /*
  1687. * All our contollers are capable of performing 64-bit DMA
  1688. */
  1689. if (IS_DMA64) {
  1690. if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
  1691. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
  1692. goto fail_set_dma_mask;
  1693. }
  1694. } else {
  1695. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
  1696. goto fail_set_dma_mask;
  1697. }
  1698. host = scsi_host_alloc(&megasas_template,
  1699. sizeof(struct megasas_instance));
  1700. if (!host) {
  1701. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  1702. goto fail_alloc_instance;
  1703. }
  1704. instance = (struct megasas_instance *)host->hostdata;
  1705. memset(instance, 0, sizeof(*instance));
  1706. instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
  1707. &instance->producer_h);
  1708. instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
  1709. &instance->consumer_h);
  1710. if (!instance->producer || !instance->consumer) {
  1711. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  1712. "producer, consumer\n");
  1713. goto fail_alloc_dma_buf;
  1714. }
  1715. *instance->producer = 0;
  1716. *instance->consumer = 0;
  1717. instance->evt_detail = pci_alloc_consistent(pdev,
  1718. sizeof(struct
  1719. megasas_evt_detail),
  1720. &instance->evt_detail_h);
  1721. if (!instance->evt_detail) {
  1722. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  1723. "event detail structure\n");
  1724. goto fail_alloc_dma_buf;
  1725. }
  1726. /*
  1727. * Initialize locks and queues
  1728. */
  1729. INIT_LIST_HEAD(&instance->cmd_pool);
  1730. init_waitqueue_head(&instance->int_cmd_wait_q);
  1731. init_waitqueue_head(&instance->abort_cmd_wait_q);
  1732. spin_lock_init(&instance->cmd_pool_lock);
  1733. spin_lock_init(&instance->instance_lock);
  1734. sema_init(&instance->aen_mutex, 1);
  1735. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  1736. /*
  1737. * Initialize PCI related and misc parameters
  1738. */
  1739. instance->pdev = pdev;
  1740. instance->host = host;
  1741. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  1742. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  1743. /*
  1744. * Initialize MFI Firmware
  1745. */
  1746. if (megasas_init_mfi(instance))
  1747. goto fail_init_mfi;
  1748. /*
  1749. * Register IRQ
  1750. */
  1751. if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
  1752. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  1753. goto fail_irq;
  1754. }
  1755. megasas_enable_intr(instance->reg_set);
  1756. /*
  1757. * Store instance in PCI softstate
  1758. */
  1759. pci_set_drvdata(pdev, instance);
  1760. /*
  1761. * Add this controller to megasas_mgmt_info structure so that it
  1762. * can be exported to management applications
  1763. */
  1764. megasas_mgmt_info.count++;
  1765. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  1766. megasas_mgmt_info.max_index++;
  1767. /*
  1768. * Initiate AEN (Asynchronous Event Notification)
  1769. */
  1770. if (megasas_start_aen(instance)) {
  1771. printk(KERN_DEBUG "megasas: start aen failed\n");
  1772. goto fail_start_aen;
  1773. }
  1774. /*
  1775. * Register with SCSI mid-layer
  1776. */
  1777. if (megasas_io_attach(instance))
  1778. goto fail_io_attach;
  1779. return 0;
  1780. fail_start_aen:
  1781. fail_io_attach:
  1782. megasas_mgmt_info.count--;
  1783. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  1784. megasas_mgmt_info.max_index--;
  1785. pci_set_drvdata(pdev, NULL);
  1786. megasas_disable_intr(instance->reg_set);
  1787. free_irq(instance->pdev->irq, instance);
  1788. megasas_release_mfi(instance);
  1789. fail_irq:
  1790. fail_init_mfi:
  1791. fail_alloc_dma_buf:
  1792. if (instance->evt_detail)
  1793. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  1794. instance->evt_detail,
  1795. instance->evt_detail_h);
  1796. if (instance->producer)
  1797. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  1798. instance->producer_h);
  1799. if (instance->consumer)
  1800. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  1801. instance->consumer_h);
  1802. scsi_host_put(host);
  1803. fail_alloc_instance:
  1804. fail_set_dma_mask:
  1805. pci_disable_device(pdev);
  1806. return -ENODEV;
  1807. }
  1808. /**
  1809. * megasas_flush_cache - Requests FW to flush all its caches
  1810. * @instance: Adapter soft state
  1811. */
  1812. static void megasas_flush_cache(struct megasas_instance *instance)
  1813. {
  1814. struct megasas_cmd *cmd;
  1815. struct megasas_dcmd_frame *dcmd;
  1816. cmd = megasas_get_cmd(instance);
  1817. if (!cmd)
  1818. return;
  1819. dcmd = &cmd->frame->dcmd;
  1820. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1821. dcmd->cmd = MFI_CMD_DCMD;
  1822. dcmd->cmd_status = 0x0;
  1823. dcmd->sge_count = 0;
  1824. dcmd->flags = MFI_FRAME_DIR_NONE;
  1825. dcmd->timeout = 0;
  1826. dcmd->data_xfer_len = 0;
  1827. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  1828. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  1829. megasas_issue_blocked_cmd(instance, cmd);
  1830. megasas_return_cmd(instance, cmd);
  1831. return;
  1832. }
  1833. /**
  1834. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  1835. * @instance: Adapter soft state
  1836. */
  1837. static void megasas_shutdown_controller(struct megasas_instance *instance)
  1838. {
  1839. struct megasas_cmd *cmd;
  1840. struct megasas_dcmd_frame *dcmd;
  1841. cmd = megasas_get_cmd(instance);
  1842. if (!cmd)
  1843. return;
  1844. if (instance->aen_cmd)
  1845. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  1846. dcmd = &cmd->frame->dcmd;
  1847. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1848. dcmd->cmd = MFI_CMD_DCMD;
  1849. dcmd->cmd_status = 0x0;
  1850. dcmd->sge_count = 0;
  1851. dcmd->flags = MFI_FRAME_DIR_NONE;
  1852. dcmd->timeout = 0;
  1853. dcmd->data_xfer_len = 0;
  1854. dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
  1855. megasas_issue_blocked_cmd(instance, cmd);
  1856. megasas_return_cmd(instance, cmd);
  1857. return;
  1858. }
  1859. /**
  1860. * megasas_detach_one - PCI hot"un"plug entry point
  1861. * @pdev: PCI device structure
  1862. */
  1863. static void megasas_detach_one(struct pci_dev *pdev)
  1864. {
  1865. int i;
  1866. struct Scsi_Host *host;
  1867. struct megasas_instance *instance;
  1868. instance = pci_get_drvdata(pdev);
  1869. host = instance->host;
  1870. scsi_remove_host(instance->host);
  1871. megasas_flush_cache(instance);
  1872. megasas_shutdown_controller(instance);
  1873. /*
  1874. * Take the instance off the instance array. Note that we will not
  1875. * decrement the max_index. We let this array be sparse array
  1876. */
  1877. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1878. if (megasas_mgmt_info.instance[i] == instance) {
  1879. megasas_mgmt_info.count--;
  1880. megasas_mgmt_info.instance[i] = NULL;
  1881. break;
  1882. }
  1883. }
  1884. pci_set_drvdata(instance->pdev, NULL);
  1885. megasas_disable_intr(instance->reg_set);
  1886. free_irq(instance->pdev->irq, instance);
  1887. megasas_release_mfi(instance);
  1888. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  1889. instance->evt_detail, instance->evt_detail_h);
  1890. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  1891. instance->producer_h);
  1892. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  1893. instance->consumer_h);
  1894. scsi_host_put(host);
  1895. pci_set_drvdata(pdev, NULL);
  1896. pci_disable_device(pdev);
  1897. return;
  1898. }
  1899. /**
  1900. * megasas_shutdown - Shutdown entry point
  1901. * @device: Generic device structure
  1902. */
  1903. static void megasas_shutdown(struct pci_dev *pdev)
  1904. {
  1905. struct megasas_instance *instance = pci_get_drvdata(pdev);
  1906. megasas_flush_cache(instance);
  1907. }
  1908. /**
  1909. * megasas_mgmt_open - char node "open" entry point
  1910. */
  1911. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  1912. {
  1913. /*
  1914. * Allow only those users with admin rights
  1915. */
  1916. if (!capable(CAP_SYS_ADMIN))
  1917. return -EACCES;
  1918. return 0;
  1919. }
  1920. /**
  1921. * megasas_mgmt_release - char node "release" entry point
  1922. */
  1923. static int megasas_mgmt_release(struct inode *inode, struct file *filep)
  1924. {
  1925. filep->private_data = NULL;
  1926. fasync_helper(-1, filep, 0, &megasas_async_queue);
  1927. return 0;
  1928. }
  1929. /**
  1930. * megasas_mgmt_fasync - Async notifier registration from applications
  1931. *
  1932. * This function adds the calling process to a driver global queue. When an
  1933. * event occurs, SIGIO will be sent to all processes in this queue.
  1934. */
  1935. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  1936. {
  1937. int rc;
  1938. mutex_lock(&megasas_async_queue_mutex);
  1939. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  1940. mutex_unlock(&megasas_async_queue_mutex);
  1941. if (rc >= 0) {
  1942. /* For sanity check when we get ioctl */
  1943. filep->private_data = filep;
  1944. return 0;
  1945. }
  1946. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  1947. return rc;
  1948. }
  1949. /**
  1950. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  1951. * @instance: Adapter soft state
  1952. * @argp: User's ioctl packet
  1953. */
  1954. static int
  1955. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  1956. struct megasas_iocpacket __user * user_ioc,
  1957. struct megasas_iocpacket *ioc)
  1958. {
  1959. struct megasas_sge32 *kern_sge32;
  1960. struct megasas_cmd *cmd;
  1961. void *kbuff_arr[MAX_IOCTL_SGE];
  1962. dma_addr_t buf_handle = 0;
  1963. int error = 0, i;
  1964. void *sense = NULL;
  1965. dma_addr_t sense_handle;
  1966. u32 *sense_ptr;
  1967. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  1968. if (ioc->sge_count > MAX_IOCTL_SGE) {
  1969. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  1970. ioc->sge_count, MAX_IOCTL_SGE);
  1971. return -EINVAL;
  1972. }
  1973. cmd = megasas_get_cmd(instance);
  1974. if (!cmd) {
  1975. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  1976. return -ENOMEM;
  1977. }
  1978. /*
  1979. * User's IOCTL packet has 2 frames (maximum). Copy those two
  1980. * frames into our cmd's frames. cmd->frame's context will get
  1981. * overwritten when we copy from user's frames. So set that value
  1982. * alone separately
  1983. */
  1984. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  1985. cmd->frame->hdr.context = cmd->index;
  1986. /*
  1987. * The management interface between applications and the fw uses
  1988. * MFI frames. E.g, RAID configuration changes, LD property changes
  1989. * etc are accomplishes through different kinds of MFI frames. The
  1990. * driver needs to care only about substituting user buffers with
  1991. * kernel buffers in SGLs. The location of SGL is embedded in the
  1992. * struct iocpacket itself.
  1993. */
  1994. kern_sge32 = (struct megasas_sge32 *)
  1995. ((unsigned long)cmd->frame + ioc->sgl_off);
  1996. /*
  1997. * For each user buffer, create a mirror buffer and copy in
  1998. */
  1999. for (i = 0; i < ioc->sge_count; i++) {
  2000. kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
  2001. ioc->sgl[i].iov_len,
  2002. &buf_handle);
  2003. if (!kbuff_arr[i]) {
  2004. printk(KERN_DEBUG "megasas: Failed to alloc "
  2005. "kernel SGL buffer for IOCTL \n");
  2006. error = -ENOMEM;
  2007. goto out;
  2008. }
  2009. /*
  2010. * We don't change the dma_coherent_mask, so
  2011. * pci_alloc_consistent only returns 32bit addresses
  2012. */
  2013. kern_sge32[i].phys_addr = (u32) buf_handle;
  2014. kern_sge32[i].length = ioc->sgl[i].iov_len;
  2015. /*
  2016. * We created a kernel buffer corresponding to the
  2017. * user buffer. Now copy in from the user buffer
  2018. */
  2019. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  2020. (u32) (ioc->sgl[i].iov_len))) {
  2021. error = -EFAULT;
  2022. goto out;
  2023. }
  2024. }
  2025. if (ioc->sense_len) {
  2026. sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
  2027. &sense_handle);
  2028. if (!sense) {
  2029. error = -ENOMEM;
  2030. goto out;
  2031. }
  2032. sense_ptr =
  2033. (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
  2034. *sense_ptr = sense_handle;
  2035. }
  2036. /*
  2037. * Set the sync_cmd flag so that the ISR knows not to complete this
  2038. * cmd to the SCSI mid-layer
  2039. */
  2040. cmd->sync_cmd = 1;
  2041. megasas_issue_blocked_cmd(instance, cmd);
  2042. cmd->sync_cmd = 0;
  2043. /*
  2044. * copy out the kernel buffers to user buffers
  2045. */
  2046. for (i = 0; i < ioc->sge_count; i++) {
  2047. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  2048. ioc->sgl[i].iov_len)) {
  2049. error = -EFAULT;
  2050. goto out;
  2051. }
  2052. }
  2053. /*
  2054. * copy out the sense
  2055. */
  2056. if (ioc->sense_len) {
  2057. /*
  2058. * sense_ptr points to the location that has the user
  2059. * sense buffer address
  2060. */
  2061. sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
  2062. ioc->sense_off);
  2063. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  2064. sense, ioc->sense_len)) {
  2065. error = -EFAULT;
  2066. goto out;
  2067. }
  2068. }
  2069. /*
  2070. * copy the status codes returned by the fw
  2071. */
  2072. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  2073. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  2074. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  2075. error = -EFAULT;
  2076. }
  2077. out:
  2078. if (sense) {
  2079. pci_free_consistent(instance->pdev, ioc->sense_len,
  2080. sense, sense_handle);
  2081. }
  2082. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  2083. pci_free_consistent(instance->pdev,
  2084. kern_sge32[i].length,
  2085. kbuff_arr[i], kern_sge32[i].phys_addr);
  2086. }
  2087. megasas_return_cmd(instance, cmd);
  2088. return error;
  2089. }
  2090. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  2091. {
  2092. int i;
  2093. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2094. if ((megasas_mgmt_info.instance[i]) &&
  2095. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  2096. return megasas_mgmt_info.instance[i];
  2097. }
  2098. return NULL;
  2099. }
  2100. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  2101. {
  2102. struct megasas_iocpacket __user *user_ioc =
  2103. (struct megasas_iocpacket __user *)arg;
  2104. struct megasas_iocpacket *ioc;
  2105. struct megasas_instance *instance;
  2106. int error;
  2107. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  2108. if (!ioc)
  2109. return -ENOMEM;
  2110. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  2111. error = -EFAULT;
  2112. goto out_kfree_ioc;
  2113. }
  2114. instance = megasas_lookup_instance(ioc->host_no);
  2115. if (!instance) {
  2116. error = -ENODEV;
  2117. goto out_kfree_ioc;
  2118. }
  2119. /*
  2120. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  2121. */
  2122. if (down_interruptible(&instance->ioctl_sem)) {
  2123. error = -ERESTARTSYS;
  2124. goto out_kfree_ioc;
  2125. }
  2126. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  2127. up(&instance->ioctl_sem);
  2128. out_kfree_ioc:
  2129. kfree(ioc);
  2130. return error;
  2131. }
  2132. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  2133. {
  2134. struct megasas_instance *instance;
  2135. struct megasas_aen aen;
  2136. int error;
  2137. if (file->private_data != file) {
  2138. printk(KERN_DEBUG "megasas: fasync_helper was not "
  2139. "called first\n");
  2140. return -EINVAL;
  2141. }
  2142. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  2143. return -EFAULT;
  2144. instance = megasas_lookup_instance(aen.host_no);
  2145. if (!instance)
  2146. return -ENODEV;
  2147. down(&instance->aen_mutex);
  2148. error = megasas_register_aen(instance, aen.seq_num,
  2149. aen.class_locale_word);
  2150. up(&instance->aen_mutex);
  2151. return error;
  2152. }
  2153. /**
  2154. * megasas_mgmt_ioctl - char node ioctl entry point
  2155. */
  2156. static long
  2157. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2158. {
  2159. switch (cmd) {
  2160. case MEGASAS_IOC_FIRMWARE:
  2161. return megasas_mgmt_ioctl_fw(file, arg);
  2162. case MEGASAS_IOC_GET_AEN:
  2163. return megasas_mgmt_ioctl_aen(file, arg);
  2164. }
  2165. return -ENOTTY;
  2166. }
  2167. #ifdef CONFIG_COMPAT
  2168. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  2169. {
  2170. struct compat_megasas_iocpacket __user *cioc =
  2171. (struct compat_megasas_iocpacket __user *)arg;
  2172. struct megasas_iocpacket __user *ioc =
  2173. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  2174. int i;
  2175. int error = 0;
  2176. clear_user(ioc, sizeof(*ioc));
  2177. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  2178. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  2179. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  2180. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  2181. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  2182. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  2183. return -EFAULT;
  2184. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  2185. compat_uptr_t ptr;
  2186. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  2187. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  2188. copy_in_user(&ioc->sgl[i].iov_len,
  2189. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  2190. return -EFAULT;
  2191. }
  2192. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  2193. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  2194. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  2195. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  2196. return -EFAULT;
  2197. }
  2198. return error;
  2199. }
  2200. static long
  2201. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  2202. unsigned long arg)
  2203. {
  2204. switch (cmd) {
  2205. case MEGASAS_IOC_FIRMWARE:{
  2206. return megasas_mgmt_compat_ioctl_fw(file, arg);
  2207. }
  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);