megaraid_sas.c 84 KB

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