megaraid_sas.c 85 KB

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