megaraid_sas.c 88 KB

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