megaraid_sas.c 88 KB

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