megaraid_sas.c 70 KB

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