megaraid_sas.c 70 KB

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