megaraid_sas.c 103 KB

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