megaraid_sas.c 70 KB

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