megaraid_sas.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413
  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.17.1-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->pad_0 = 0;
  731. pthru->flags = flags;
  732. pthru->data_xfer_len = scsi_bufflen(scp);
  733. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  734. /*
  735. * If the command is for the tape device, set the
  736. * pthru timeout to the os layer timeout value.
  737. */
  738. if (scp->device->type == TYPE_TAPE) {
  739. if ((scp->request->timeout / HZ) > 0xFFFF)
  740. pthru->timeout = 0xFFFF;
  741. else
  742. pthru->timeout = scp->request->timeout / HZ;
  743. }
  744. /*
  745. * Construct SGL
  746. */
  747. if (instance->flag_ieee == 1) {
  748. pthru->flags |= MFI_FRAME_SGL64;
  749. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  750. &pthru->sgl);
  751. } else if (IS_DMA64) {
  752. pthru->flags |= MFI_FRAME_SGL64;
  753. pthru->sge_count = megasas_make_sgl64(instance, scp,
  754. &pthru->sgl);
  755. } else
  756. pthru->sge_count = megasas_make_sgl32(instance, scp,
  757. &pthru->sgl);
  758. if (pthru->sge_count > instance->max_num_sge) {
  759. printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
  760. pthru->sge_count);
  761. return 0;
  762. }
  763. /*
  764. * Sense info specific
  765. */
  766. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  767. pthru->sense_buf_phys_addr_hi = 0;
  768. pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  769. /*
  770. * Compute the total number of frames this command consumes. FW uses
  771. * this number to pull sufficient number of frames from host memory.
  772. */
  773. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  774. PTHRU_FRAME);
  775. return cmd->frame_count;
  776. }
  777. /**
  778. * megasas_build_ldio - Prepares IOs to logical devices
  779. * @instance: Adapter soft state
  780. * @scp: SCSI command
  781. * @cmd: Command to be prepared
  782. *
  783. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  784. */
  785. static int
  786. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  787. struct megasas_cmd *cmd)
  788. {
  789. u32 device_id;
  790. u8 sc = scp->cmnd[0];
  791. u16 flags = 0;
  792. struct megasas_io_frame *ldio;
  793. device_id = MEGASAS_DEV_INDEX(instance, scp);
  794. ldio = (struct megasas_io_frame *)cmd->frame;
  795. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  796. flags = MFI_FRAME_DIR_WRITE;
  797. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  798. flags = MFI_FRAME_DIR_READ;
  799. if (instance->flag_ieee == 1) {
  800. flags |= MFI_FRAME_IEEE;
  801. }
  802. /*
  803. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  804. */
  805. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  806. ldio->cmd_status = 0x0;
  807. ldio->scsi_status = 0x0;
  808. ldio->target_id = device_id;
  809. ldio->timeout = 0;
  810. ldio->reserved_0 = 0;
  811. ldio->pad_0 = 0;
  812. ldio->flags = flags;
  813. ldio->start_lba_hi = 0;
  814. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  815. /*
  816. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  817. */
  818. if (scp->cmd_len == 6) {
  819. ldio->lba_count = (u32) scp->cmnd[4];
  820. ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  821. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  822. ldio->start_lba_lo &= 0x1FFFFF;
  823. }
  824. /*
  825. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  826. */
  827. else if (scp->cmd_len == 10) {
  828. ldio->lba_count = (u32) scp->cmnd[8] |
  829. ((u32) scp->cmnd[7] << 8);
  830. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  831. ((u32) scp->cmnd[3] << 16) |
  832. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  833. }
  834. /*
  835. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  836. */
  837. else if (scp->cmd_len == 12) {
  838. ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
  839. ((u32) scp->cmnd[7] << 16) |
  840. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  841. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  842. ((u32) scp->cmnd[3] << 16) |
  843. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  844. }
  845. /*
  846. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  847. */
  848. else if (scp->cmd_len == 16) {
  849. ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
  850. ((u32) scp->cmnd[11] << 16) |
  851. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  852. ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  853. ((u32) scp->cmnd[7] << 16) |
  854. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  855. ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  856. ((u32) scp->cmnd[3] << 16) |
  857. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  858. }
  859. /*
  860. * Construct SGL
  861. */
  862. if (instance->flag_ieee) {
  863. ldio->flags |= MFI_FRAME_SGL64;
  864. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  865. &ldio->sgl);
  866. } else if (IS_DMA64) {
  867. ldio->flags |= MFI_FRAME_SGL64;
  868. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  869. } else
  870. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  871. if (ldio->sge_count > instance->max_num_sge) {
  872. printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
  873. ldio->sge_count);
  874. return 0;
  875. }
  876. /*
  877. * Sense info specific
  878. */
  879. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  880. ldio->sense_buf_phys_addr_hi = 0;
  881. ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  882. /*
  883. * Compute the total number of frames this command consumes. FW uses
  884. * this number to pull sufficient number of frames from host memory.
  885. */
  886. cmd->frame_count = megasas_get_frame_count(instance,
  887. ldio->sge_count, IO_FRAME);
  888. return cmd->frame_count;
  889. }
  890. /**
  891. * megasas_is_ldio - Checks if the cmd is for logical drive
  892. * @scmd: SCSI command
  893. *
  894. * Called by megasas_queue_command to find out if the command to be queued
  895. * is a logical drive command
  896. */
  897. static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  898. {
  899. if (!MEGASAS_IS_LOGICAL(cmd))
  900. return 0;
  901. switch (cmd->cmnd[0]) {
  902. case READ_10:
  903. case WRITE_10:
  904. case READ_12:
  905. case WRITE_12:
  906. case READ_6:
  907. case WRITE_6:
  908. case READ_16:
  909. case WRITE_16:
  910. return 1;
  911. default:
  912. return 0;
  913. }
  914. }
  915. /**
  916. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  917. * in FW
  918. * @instance: Adapter soft state
  919. */
  920. static inline void
  921. megasas_dump_pending_frames(struct megasas_instance *instance)
  922. {
  923. struct megasas_cmd *cmd;
  924. int i,n;
  925. union megasas_sgl *mfi_sgl;
  926. struct megasas_io_frame *ldio;
  927. struct megasas_pthru_frame *pthru;
  928. u32 sgcount;
  929. u32 max_cmd = instance->max_fw_cmds;
  930. printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  931. printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  932. if (IS_DMA64)
  933. printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  934. else
  935. printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  936. printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  937. for (i = 0; i < max_cmd; i++) {
  938. cmd = instance->cmd_list[i];
  939. if(!cmd->scmd)
  940. continue;
  941. printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  942. if (megasas_is_ldio(cmd->scmd)){
  943. ldio = (struct megasas_io_frame *)cmd->frame;
  944. mfi_sgl = &ldio->sgl;
  945. sgcount = ldio->sge_count;
  946. 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);
  947. }
  948. else {
  949. pthru = (struct megasas_pthru_frame *) cmd->frame;
  950. mfi_sgl = &pthru->sgl;
  951. sgcount = pthru->sge_count;
  952. 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);
  953. }
  954. if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
  955. for (n = 0; n < sgcount; n++){
  956. if (IS_DMA64)
  957. 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) ;
  958. else
  959. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
  960. }
  961. }
  962. printk(KERN_ERR "\n");
  963. } /*for max_cmd*/
  964. printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  965. for (i = 0; i < max_cmd; i++) {
  966. cmd = instance->cmd_list[i];
  967. if(cmd->sync_cmd == 1){
  968. printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  969. }
  970. }
  971. printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
  972. }
  973. /**
  974. * megasas_queue_command - Queue entry point
  975. * @scmd: SCSI command to be queued
  976. * @done: Callback entry point
  977. */
  978. static int
  979. megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  980. {
  981. u32 frame_count;
  982. struct megasas_cmd *cmd;
  983. struct megasas_instance *instance;
  984. instance = (struct megasas_instance *)
  985. scmd->device->host->hostdata;
  986. /* Don't process if we have already declared adapter dead */
  987. if (instance->hw_crit_error)
  988. return SCSI_MLQUEUE_HOST_BUSY;
  989. scmd->scsi_done = done;
  990. scmd->result = 0;
  991. if (MEGASAS_IS_LOGICAL(scmd) &&
  992. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  993. scmd->result = DID_BAD_TARGET << 16;
  994. goto out_done;
  995. }
  996. switch (scmd->cmnd[0]) {
  997. case SYNCHRONIZE_CACHE:
  998. /*
  999. * FW takes care of flush cache on its own
  1000. * No need to send it down
  1001. */
  1002. scmd->result = DID_OK << 16;
  1003. goto out_done;
  1004. default:
  1005. break;
  1006. }
  1007. cmd = megasas_get_cmd(instance);
  1008. if (!cmd)
  1009. return SCSI_MLQUEUE_HOST_BUSY;
  1010. /*
  1011. * Logical drive command
  1012. */
  1013. if (megasas_is_ldio(scmd))
  1014. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1015. else
  1016. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1017. if (!frame_count)
  1018. goto out_return_cmd;
  1019. cmd->scmd = scmd;
  1020. scmd->SCp.ptr = (char *)cmd;
  1021. /*
  1022. * Issue the command to the FW
  1023. */
  1024. atomic_inc(&instance->fw_outstanding);
  1025. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1026. cmd->frame_count-1, instance->reg_set);
  1027. /*
  1028. * Check if we have pend cmds to be completed
  1029. */
  1030. if (poll_mode_io && atomic_read(&instance->fw_outstanding))
  1031. tasklet_schedule(&instance->isr_tasklet);
  1032. return 0;
  1033. out_return_cmd:
  1034. megasas_return_cmd(instance, cmd);
  1035. out_done:
  1036. done(scmd);
  1037. return 0;
  1038. }
  1039. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1040. {
  1041. int i;
  1042. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1043. if ((megasas_mgmt_info.instance[i]) &&
  1044. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1045. return megasas_mgmt_info.instance[i];
  1046. }
  1047. return NULL;
  1048. }
  1049. static int megasas_slave_configure(struct scsi_device *sdev)
  1050. {
  1051. u16 pd_index = 0;
  1052. struct megasas_instance *instance ;
  1053. instance = megasas_lookup_instance(sdev->host->host_no);
  1054. /*
  1055. * Don't export physical disk devices to the disk driver.
  1056. *
  1057. * FIXME: Currently we don't export them to the midlayer at all.
  1058. * That will be fixed once LSI engineers have audited the
  1059. * firmware for possible issues.
  1060. */
  1061. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1062. sdev->type == TYPE_DISK) {
  1063. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1064. sdev->id;
  1065. if (instance->pd_list[pd_index].driveState ==
  1066. MR_PD_STATE_SYSTEM) {
  1067. blk_queue_rq_timeout(sdev->request_queue,
  1068. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1069. return 0;
  1070. }
  1071. return -ENXIO;
  1072. }
  1073. /*
  1074. * The RAID firmware may require extended timeouts.
  1075. */
  1076. blk_queue_rq_timeout(sdev->request_queue,
  1077. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1078. return 0;
  1079. }
  1080. static int megasas_slave_alloc(struct scsi_device *sdev)
  1081. {
  1082. u16 pd_index = 0;
  1083. struct megasas_instance *instance ;
  1084. instance = megasas_lookup_instance(sdev->host->host_no);
  1085. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1086. (sdev->type == TYPE_DISK)) {
  1087. /*
  1088. * Open the OS scan to the SYSTEM PD
  1089. */
  1090. pd_index =
  1091. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1092. sdev->id;
  1093. if ((instance->pd_list[pd_index].driveState ==
  1094. MR_PD_STATE_SYSTEM) &&
  1095. (instance->pd_list[pd_index].driveType ==
  1096. TYPE_DISK)) {
  1097. return 0;
  1098. }
  1099. return -ENXIO;
  1100. }
  1101. return 0;
  1102. }
  1103. /**
  1104. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1105. * @instance_addr: Address of adapter soft state
  1106. *
  1107. * Tasklet to complete cmds
  1108. */
  1109. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1110. {
  1111. u32 producer;
  1112. u32 consumer;
  1113. u32 context;
  1114. struct megasas_cmd *cmd;
  1115. struct megasas_instance *instance =
  1116. (struct megasas_instance *)instance_addr;
  1117. unsigned long flags;
  1118. /* If we have already declared adapter dead, donot complete cmds */
  1119. if (instance->hw_crit_error)
  1120. return;
  1121. spin_lock_irqsave(&instance->completion_lock, flags);
  1122. producer = *instance->producer;
  1123. consumer = *instance->consumer;
  1124. while (consumer != producer) {
  1125. context = instance->reply_queue[consumer];
  1126. cmd = instance->cmd_list[context];
  1127. megasas_complete_cmd(instance, cmd, DID_OK);
  1128. consumer++;
  1129. if (consumer == (instance->max_fw_cmds + 1)) {
  1130. consumer = 0;
  1131. }
  1132. }
  1133. *instance->consumer = producer;
  1134. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1135. /*
  1136. * Check if we can restore can_queue
  1137. */
  1138. if (instance->flag & MEGASAS_FW_BUSY
  1139. && time_after(jiffies, instance->last_time + 5 * HZ)
  1140. && atomic_read(&instance->fw_outstanding) < 17) {
  1141. spin_lock_irqsave(instance->host->host_lock, flags);
  1142. instance->flag &= ~MEGASAS_FW_BUSY;
  1143. if ((instance->pdev->device ==
  1144. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1145. (instance->pdev->device ==
  1146. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1147. instance->host->can_queue =
  1148. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1149. } else
  1150. instance->host->can_queue =
  1151. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1152. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1153. }
  1154. }
  1155. /**
  1156. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1157. * @instance: Adapter soft state
  1158. *
  1159. * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
  1160. * complete all its outstanding commands. Returns error if one or more IOs
  1161. * are pending after this time period. It also marks the controller dead.
  1162. */
  1163. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1164. {
  1165. int i;
  1166. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1167. for (i = 0; i < wait_time; i++) {
  1168. int outstanding = atomic_read(&instance->fw_outstanding);
  1169. if (!outstanding)
  1170. break;
  1171. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1172. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1173. "commands to complete\n",i,outstanding);
  1174. /*
  1175. * Call cmd completion routine. Cmd to be
  1176. * be completed directly without depending on isr.
  1177. */
  1178. megasas_complete_cmd_dpc((unsigned long)instance);
  1179. }
  1180. msleep(1000);
  1181. }
  1182. if (atomic_read(&instance->fw_outstanding)) {
  1183. /*
  1184. * Send signal to FW to stop processing any pending cmds.
  1185. * The controller will be taken offline by the OS now.
  1186. */
  1187. if ((instance->pdev->device ==
  1188. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1189. (instance->pdev->device ==
  1190. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1191. writel(MFI_STOP_ADP,
  1192. &instance->reg_set->reserved_0[0]);
  1193. } else {
  1194. writel(MFI_STOP_ADP,
  1195. &instance->reg_set->inbound_doorbell);
  1196. }
  1197. megasas_dump_pending_frames(instance);
  1198. instance->hw_crit_error = 1;
  1199. return FAILED;
  1200. }
  1201. return SUCCESS;
  1202. }
  1203. /**
  1204. * megasas_generic_reset - Generic reset routine
  1205. * @scmd: Mid-layer SCSI command
  1206. *
  1207. * This routine implements a generic reset handler for device, bus and host
  1208. * reset requests. Device, bus and host specific reset handlers can use this
  1209. * function after they do their specific tasks.
  1210. */
  1211. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1212. {
  1213. int ret_val;
  1214. struct megasas_instance *instance;
  1215. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1216. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
  1217. scmd->serial_number, scmd->cmnd[0], scmd->retries);
  1218. if (instance->hw_crit_error) {
  1219. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1220. "failures\n");
  1221. return FAILED;
  1222. }
  1223. ret_val = megasas_wait_for_outstanding(instance);
  1224. if (ret_val == SUCCESS)
  1225. printk(KERN_NOTICE "megasas: reset successful \n");
  1226. else
  1227. printk(KERN_ERR "megasas: failed to do reset\n");
  1228. return ret_val;
  1229. }
  1230. /**
  1231. * megasas_reset_timer - quiesce the adapter if required
  1232. * @scmd: scsi cmnd
  1233. *
  1234. * Sets the FW busy flag and reduces the host->can_queue if the
  1235. * cmd has not been completed within the timeout period.
  1236. */
  1237. static enum
  1238. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1239. {
  1240. struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
  1241. struct megasas_instance *instance;
  1242. unsigned long flags;
  1243. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1244. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1245. return BLK_EH_NOT_HANDLED;
  1246. }
  1247. instance = cmd->instance;
  1248. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1249. /* FW is busy, throttle IO */
  1250. spin_lock_irqsave(instance->host->host_lock, flags);
  1251. instance->host->can_queue = 16;
  1252. instance->last_time = jiffies;
  1253. instance->flag |= MEGASAS_FW_BUSY;
  1254. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1255. }
  1256. return BLK_EH_RESET_TIMER;
  1257. }
  1258. /**
  1259. * megasas_reset_device - Device reset handler entry point
  1260. */
  1261. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1262. {
  1263. int ret;
  1264. /*
  1265. * First wait for all commands to complete
  1266. */
  1267. ret = megasas_generic_reset(scmd);
  1268. return ret;
  1269. }
  1270. /**
  1271. * megasas_reset_bus_host - Bus & host reset handler entry point
  1272. */
  1273. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1274. {
  1275. int ret;
  1276. /*
  1277. * First wait for all commands to complete
  1278. */
  1279. ret = megasas_generic_reset(scmd);
  1280. return ret;
  1281. }
  1282. /**
  1283. * megasas_bios_param - Returns disk geometry for a disk
  1284. * @sdev: device handle
  1285. * @bdev: block device
  1286. * @capacity: drive capacity
  1287. * @geom: geometry parameters
  1288. */
  1289. static int
  1290. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1291. sector_t capacity, int geom[])
  1292. {
  1293. int heads;
  1294. int sectors;
  1295. sector_t cylinders;
  1296. unsigned long tmp;
  1297. /* Default heads (64) & sectors (32) */
  1298. heads = 64;
  1299. sectors = 32;
  1300. tmp = heads * sectors;
  1301. cylinders = capacity;
  1302. sector_div(cylinders, tmp);
  1303. /*
  1304. * Handle extended translation size for logical drives > 1Gb
  1305. */
  1306. if (capacity >= 0x200000) {
  1307. heads = 255;
  1308. sectors = 63;
  1309. tmp = heads*sectors;
  1310. cylinders = capacity;
  1311. sector_div(cylinders, tmp);
  1312. }
  1313. geom[0] = heads;
  1314. geom[1] = sectors;
  1315. geom[2] = cylinders;
  1316. return 0;
  1317. }
  1318. static void megasas_aen_polling(struct work_struct *work);
  1319. /**
  1320. * megasas_service_aen - Processes an event notification
  1321. * @instance: Adapter soft state
  1322. * @cmd: AEN command completed by the ISR
  1323. *
  1324. * For AEN, driver sends a command down to FW that is held by the FW till an
  1325. * event occurs. When an event of interest occurs, FW completes the command
  1326. * that it was previously holding.
  1327. *
  1328. * This routines sends SIGIO signal to processes that have registered with the
  1329. * driver for AEN.
  1330. */
  1331. static void
  1332. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1333. {
  1334. unsigned long flags;
  1335. /*
  1336. * Don't signal app if it is just an aborted previously registered aen
  1337. */
  1338. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1339. spin_lock_irqsave(&poll_aen_lock, flags);
  1340. megasas_poll_wait_aen = 1;
  1341. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1342. wake_up(&megasas_poll_wait);
  1343. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1344. }
  1345. else
  1346. cmd->abort_aen = 0;
  1347. instance->aen_cmd = NULL;
  1348. megasas_return_cmd(instance, cmd);
  1349. if (instance->unload == 0) {
  1350. struct megasas_aen_event *ev;
  1351. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1352. if (!ev) {
  1353. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1354. } else {
  1355. ev->instance = instance;
  1356. instance->ev = ev;
  1357. INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
  1358. schedule_delayed_work(
  1359. (struct delayed_work *)&ev->hotplug_work, 0);
  1360. }
  1361. }
  1362. }
  1363. /*
  1364. * Scsi host template for megaraid_sas driver
  1365. */
  1366. static struct scsi_host_template megasas_template = {
  1367. .module = THIS_MODULE,
  1368. .name = "LSI SAS based MegaRAID driver",
  1369. .proc_name = "megaraid_sas",
  1370. .slave_configure = megasas_slave_configure,
  1371. .slave_alloc = megasas_slave_alloc,
  1372. .queuecommand = megasas_queue_command,
  1373. .eh_device_reset_handler = megasas_reset_device,
  1374. .eh_bus_reset_handler = megasas_reset_bus_host,
  1375. .eh_host_reset_handler = megasas_reset_bus_host,
  1376. .eh_timed_out = megasas_reset_timer,
  1377. .bios_param = megasas_bios_param,
  1378. .use_clustering = ENABLE_CLUSTERING,
  1379. };
  1380. /**
  1381. * megasas_complete_int_cmd - Completes an internal command
  1382. * @instance: Adapter soft state
  1383. * @cmd: Command to be completed
  1384. *
  1385. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1386. * after it issues a command. This function wakes up that waiting routine by
  1387. * calling wake_up() on the wait queue.
  1388. */
  1389. static void
  1390. megasas_complete_int_cmd(struct megasas_instance *instance,
  1391. struct megasas_cmd *cmd)
  1392. {
  1393. cmd->cmd_status = cmd->frame->io.cmd_status;
  1394. if (cmd->cmd_status == ENODATA) {
  1395. cmd->cmd_status = 0;
  1396. }
  1397. wake_up(&instance->int_cmd_wait_q);
  1398. }
  1399. /**
  1400. * megasas_complete_abort - Completes aborting a command
  1401. * @instance: Adapter soft state
  1402. * @cmd: Cmd that was issued to abort another cmd
  1403. *
  1404. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1405. * after it issues an abort on a previously issued command. This function
  1406. * wakes up all functions waiting on the same wait queue.
  1407. */
  1408. static void
  1409. megasas_complete_abort(struct megasas_instance *instance,
  1410. struct megasas_cmd *cmd)
  1411. {
  1412. if (cmd->sync_cmd) {
  1413. cmd->sync_cmd = 0;
  1414. cmd->cmd_status = 0;
  1415. wake_up(&instance->abort_cmd_wait_q);
  1416. }
  1417. return;
  1418. }
  1419. /**
  1420. * megasas_complete_cmd - Completes a command
  1421. * @instance: Adapter soft state
  1422. * @cmd: Command to be completed
  1423. * @alt_status: If non-zero, use this value as status to
  1424. * SCSI mid-layer instead of the value returned
  1425. * by the FW. This should be used if caller wants
  1426. * an alternate status (as in the case of aborted
  1427. * commands)
  1428. */
  1429. static void
  1430. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1431. u8 alt_status)
  1432. {
  1433. int exception = 0;
  1434. struct megasas_header *hdr = &cmd->frame->hdr;
  1435. unsigned long flags;
  1436. if (cmd->scmd)
  1437. cmd->scmd->SCp.ptr = NULL;
  1438. switch (hdr->cmd) {
  1439. case MFI_CMD_PD_SCSI_IO:
  1440. case MFI_CMD_LD_SCSI_IO:
  1441. /*
  1442. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1443. * issued either through an IO path or an IOCTL path. If it
  1444. * was via IOCTL, we will send it to internal completion.
  1445. */
  1446. if (cmd->sync_cmd) {
  1447. cmd->sync_cmd = 0;
  1448. megasas_complete_int_cmd(instance, cmd);
  1449. break;
  1450. }
  1451. case MFI_CMD_LD_READ:
  1452. case MFI_CMD_LD_WRITE:
  1453. if (alt_status) {
  1454. cmd->scmd->result = alt_status << 16;
  1455. exception = 1;
  1456. }
  1457. if (exception) {
  1458. atomic_dec(&instance->fw_outstanding);
  1459. scsi_dma_unmap(cmd->scmd);
  1460. cmd->scmd->scsi_done(cmd->scmd);
  1461. megasas_return_cmd(instance, cmd);
  1462. break;
  1463. }
  1464. switch (hdr->cmd_status) {
  1465. case MFI_STAT_OK:
  1466. cmd->scmd->result = DID_OK << 16;
  1467. break;
  1468. case MFI_STAT_SCSI_IO_FAILED:
  1469. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1470. cmd->scmd->result =
  1471. (DID_ERROR << 16) | hdr->scsi_status;
  1472. break;
  1473. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1474. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1475. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1476. memset(cmd->scmd->sense_buffer, 0,
  1477. SCSI_SENSE_BUFFERSIZE);
  1478. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1479. hdr->sense_len);
  1480. cmd->scmd->result |= DRIVER_SENSE << 24;
  1481. }
  1482. break;
  1483. case MFI_STAT_LD_OFFLINE:
  1484. case MFI_STAT_DEVICE_NOT_FOUND:
  1485. cmd->scmd->result = DID_BAD_TARGET << 16;
  1486. break;
  1487. default:
  1488. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1489. hdr->cmd_status);
  1490. cmd->scmd->result = DID_ERROR << 16;
  1491. break;
  1492. }
  1493. atomic_dec(&instance->fw_outstanding);
  1494. scsi_dma_unmap(cmd->scmd);
  1495. cmd->scmd->scsi_done(cmd->scmd);
  1496. megasas_return_cmd(instance, cmd);
  1497. break;
  1498. case MFI_CMD_SMP:
  1499. case MFI_CMD_STP:
  1500. case MFI_CMD_DCMD:
  1501. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1502. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1503. spin_lock_irqsave(&poll_aen_lock, flags);
  1504. megasas_poll_wait_aen = 0;
  1505. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1506. }
  1507. /*
  1508. * See if got an event notification
  1509. */
  1510. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1511. megasas_service_aen(instance, cmd);
  1512. else
  1513. megasas_complete_int_cmd(instance, cmd);
  1514. break;
  1515. case MFI_CMD_ABORT:
  1516. /*
  1517. * Cmd issued to abort another cmd returned
  1518. */
  1519. megasas_complete_abort(instance, cmd);
  1520. break;
  1521. default:
  1522. printk("megasas: Unknown command completed! [0x%X]\n",
  1523. hdr->cmd);
  1524. break;
  1525. }
  1526. }
  1527. /**
  1528. * megasas_deplete_reply_queue - Processes all completed commands
  1529. * @instance: Adapter soft state
  1530. * @alt_status: Alternate status to be returned to
  1531. * SCSI mid-layer instead of the status
  1532. * returned by the FW
  1533. */
  1534. static int
  1535. megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
  1536. {
  1537. /*
  1538. * Check if it is our interrupt
  1539. * Clear the interrupt
  1540. */
  1541. if(instance->instancet->clear_intr(instance->reg_set))
  1542. return IRQ_NONE;
  1543. if (instance->hw_crit_error)
  1544. goto out_done;
  1545. /*
  1546. * Schedule the tasklet for cmd completion
  1547. */
  1548. tasklet_schedule(&instance->isr_tasklet);
  1549. out_done:
  1550. return IRQ_HANDLED;
  1551. }
  1552. /**
  1553. * megasas_isr - isr entry point
  1554. */
  1555. static irqreturn_t megasas_isr(int irq, void *devp)
  1556. {
  1557. return megasas_deplete_reply_queue((struct megasas_instance *)devp,
  1558. DID_OK);
  1559. }
  1560. /**
  1561. * megasas_transition_to_ready - Move the FW to READY state
  1562. * @instance: Adapter soft state
  1563. *
  1564. * During the initialization, FW passes can potentially be in any one of
  1565. * several possible states. If the FW in operational, waiting-for-handshake
  1566. * states, driver must take steps to bring it to ready state. Otherwise, it
  1567. * has to wait for the ready state.
  1568. */
  1569. static int
  1570. megasas_transition_to_ready(struct megasas_instance* instance)
  1571. {
  1572. int i;
  1573. u8 max_wait;
  1574. u32 fw_state;
  1575. u32 cur_state;
  1576. u32 abs_state, curr_abs_state;
  1577. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  1578. if (fw_state != MFI_STATE_READY)
  1579. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  1580. " state\n");
  1581. while (fw_state != MFI_STATE_READY) {
  1582. abs_state =
  1583. instance->instancet->read_fw_status_reg(instance->reg_set);
  1584. switch (fw_state) {
  1585. case MFI_STATE_FAULT:
  1586. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  1587. return -ENODEV;
  1588. case MFI_STATE_WAIT_HANDSHAKE:
  1589. /*
  1590. * Set the CLR bit in inbound doorbell
  1591. */
  1592. if ((instance->pdev->device ==
  1593. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1594. (instance->pdev->device ==
  1595. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1596. writel(
  1597. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1598. &instance->reg_set->reserved_0[0]);
  1599. } else {
  1600. writel(
  1601. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1602. &instance->reg_set->inbound_doorbell);
  1603. }
  1604. max_wait = MEGASAS_RESET_WAIT_TIME;
  1605. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  1606. break;
  1607. case MFI_STATE_BOOT_MESSAGE_PENDING:
  1608. if ((instance->pdev->device ==
  1609. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1610. (instance->pdev->device ==
  1611. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1612. writel(MFI_INIT_HOTPLUG,
  1613. &instance->reg_set->reserved_0[0]);
  1614. } else
  1615. writel(MFI_INIT_HOTPLUG,
  1616. &instance->reg_set->inbound_doorbell);
  1617. max_wait = MEGASAS_RESET_WAIT_TIME;
  1618. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  1619. break;
  1620. case MFI_STATE_OPERATIONAL:
  1621. /*
  1622. * Bring it to READY state; assuming max wait 10 secs
  1623. */
  1624. instance->instancet->disable_intr(instance->reg_set);
  1625. if ((instance->pdev->device ==
  1626. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1627. (instance->pdev->device ==
  1628. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1629. writel(MFI_RESET_FLAGS,
  1630. &instance->reg_set->reserved_0[0]);
  1631. } else
  1632. writel(MFI_RESET_FLAGS,
  1633. &instance->reg_set->inbound_doorbell);
  1634. max_wait = MEGASAS_RESET_WAIT_TIME;
  1635. cur_state = MFI_STATE_OPERATIONAL;
  1636. break;
  1637. case MFI_STATE_UNDEFINED:
  1638. /*
  1639. * This state should not last for more than 2 seconds
  1640. */
  1641. max_wait = MEGASAS_RESET_WAIT_TIME;
  1642. cur_state = MFI_STATE_UNDEFINED;
  1643. break;
  1644. case MFI_STATE_BB_INIT:
  1645. max_wait = MEGASAS_RESET_WAIT_TIME;
  1646. cur_state = MFI_STATE_BB_INIT;
  1647. break;
  1648. case MFI_STATE_FW_INIT:
  1649. max_wait = MEGASAS_RESET_WAIT_TIME;
  1650. cur_state = MFI_STATE_FW_INIT;
  1651. break;
  1652. case MFI_STATE_FW_INIT_2:
  1653. max_wait = MEGASAS_RESET_WAIT_TIME;
  1654. cur_state = MFI_STATE_FW_INIT_2;
  1655. break;
  1656. case MFI_STATE_DEVICE_SCAN:
  1657. max_wait = MEGASAS_RESET_WAIT_TIME;
  1658. cur_state = MFI_STATE_DEVICE_SCAN;
  1659. break;
  1660. case MFI_STATE_FLUSH_CACHE:
  1661. max_wait = MEGASAS_RESET_WAIT_TIME;
  1662. cur_state = MFI_STATE_FLUSH_CACHE;
  1663. break;
  1664. default:
  1665. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  1666. fw_state);
  1667. return -ENODEV;
  1668. }
  1669. /*
  1670. * The cur_state should not last for more than max_wait secs
  1671. */
  1672. for (i = 0; i < (max_wait * 1000); i++) {
  1673. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  1674. MFI_STATE_MASK ;
  1675. curr_abs_state =
  1676. instance->instancet->read_fw_status_reg(instance->reg_set);
  1677. if (abs_state == curr_abs_state) {
  1678. msleep(1);
  1679. } else
  1680. break;
  1681. }
  1682. /*
  1683. * Return error if fw_state hasn't changed after max_wait
  1684. */
  1685. if (curr_abs_state == abs_state) {
  1686. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  1687. "in %d secs\n", fw_state, max_wait);
  1688. return -ENODEV;
  1689. }
  1690. };
  1691. printk(KERN_INFO "megasas: FW now in Ready state\n");
  1692. return 0;
  1693. }
  1694. /**
  1695. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  1696. * @instance: Adapter soft state
  1697. */
  1698. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  1699. {
  1700. int i;
  1701. u32 max_cmd = instance->max_fw_cmds;
  1702. struct megasas_cmd *cmd;
  1703. if (!instance->frame_dma_pool)
  1704. return;
  1705. /*
  1706. * Return all frames to pool
  1707. */
  1708. for (i = 0; i < max_cmd; i++) {
  1709. cmd = instance->cmd_list[i];
  1710. if (cmd->frame)
  1711. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  1712. cmd->frame_phys_addr);
  1713. if (cmd->sense)
  1714. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  1715. cmd->sense_phys_addr);
  1716. }
  1717. /*
  1718. * Now destroy the pool itself
  1719. */
  1720. pci_pool_destroy(instance->frame_dma_pool);
  1721. pci_pool_destroy(instance->sense_dma_pool);
  1722. instance->frame_dma_pool = NULL;
  1723. instance->sense_dma_pool = NULL;
  1724. }
  1725. /**
  1726. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  1727. * @instance: Adapter soft state
  1728. *
  1729. * Each command packet has an embedded DMA memory buffer that is used for
  1730. * filling MFI frame and the SG list that immediately follows the frame. This
  1731. * function creates those DMA memory buffers for each command packet by using
  1732. * PCI pool facility.
  1733. */
  1734. static int megasas_create_frame_pool(struct megasas_instance *instance)
  1735. {
  1736. int i;
  1737. u32 max_cmd;
  1738. u32 sge_sz;
  1739. u32 sgl_sz;
  1740. u32 total_sz;
  1741. u32 frame_count;
  1742. struct megasas_cmd *cmd;
  1743. max_cmd = instance->max_fw_cmds;
  1744. /*
  1745. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  1746. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  1747. */
  1748. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1749. sizeof(struct megasas_sge32);
  1750. if (instance->flag_ieee) {
  1751. sge_sz = sizeof(struct megasas_sge_skinny);
  1752. }
  1753. /*
  1754. * Calculated the number of 64byte frames required for SGL
  1755. */
  1756. sgl_sz = sge_sz * instance->max_num_sge;
  1757. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  1758. /*
  1759. * We need one extra frame for the MFI command
  1760. */
  1761. frame_count++;
  1762. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  1763. /*
  1764. * Use DMA pool facility provided by PCI layer
  1765. */
  1766. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  1767. instance->pdev, total_sz, 64,
  1768. 0);
  1769. if (!instance->frame_dma_pool) {
  1770. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  1771. return -ENOMEM;
  1772. }
  1773. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  1774. instance->pdev, 128, 4, 0);
  1775. if (!instance->sense_dma_pool) {
  1776. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  1777. pci_pool_destroy(instance->frame_dma_pool);
  1778. instance->frame_dma_pool = NULL;
  1779. return -ENOMEM;
  1780. }
  1781. /*
  1782. * Allocate and attach a frame to each of the commands in cmd_list.
  1783. * By making cmd->index as the context instead of the &cmd, we can
  1784. * always use 32bit context regardless of the architecture
  1785. */
  1786. for (i = 0; i < max_cmd; i++) {
  1787. cmd = instance->cmd_list[i];
  1788. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  1789. GFP_KERNEL, &cmd->frame_phys_addr);
  1790. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  1791. GFP_KERNEL, &cmd->sense_phys_addr);
  1792. /*
  1793. * megasas_teardown_frame_pool() takes care of freeing
  1794. * whatever has been allocated
  1795. */
  1796. if (!cmd->frame || !cmd->sense) {
  1797. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  1798. megasas_teardown_frame_pool(instance);
  1799. return -ENOMEM;
  1800. }
  1801. cmd->frame->io.context = cmd->index;
  1802. cmd->frame->io.pad_0 = 0;
  1803. }
  1804. return 0;
  1805. }
  1806. /**
  1807. * megasas_free_cmds - Free all the cmds in the free cmd pool
  1808. * @instance: Adapter soft state
  1809. */
  1810. static void megasas_free_cmds(struct megasas_instance *instance)
  1811. {
  1812. int i;
  1813. /* First free the MFI frame pool */
  1814. megasas_teardown_frame_pool(instance);
  1815. /* Free all the commands in the cmd_list */
  1816. for (i = 0; i < instance->max_fw_cmds; i++)
  1817. kfree(instance->cmd_list[i]);
  1818. /* Free the cmd_list buffer itself */
  1819. kfree(instance->cmd_list);
  1820. instance->cmd_list = NULL;
  1821. INIT_LIST_HEAD(&instance->cmd_pool);
  1822. }
  1823. /**
  1824. * megasas_alloc_cmds - Allocates the command packets
  1825. * @instance: Adapter soft state
  1826. *
  1827. * Each command that is issued to the FW, whether IO commands from the OS or
  1828. * internal commands like IOCTLs, are wrapped in local data structure called
  1829. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  1830. * the FW.
  1831. *
  1832. * Each frame has a 32-bit field called context (tag). This context is used
  1833. * to get back the megasas_cmd from the frame when a frame gets completed in
  1834. * the ISR. Typically the address of the megasas_cmd itself would be used as
  1835. * the context. But we wanted to keep the differences between 32 and 64 bit
  1836. * systems to the mininum. We always use 32 bit integers for the context. In
  1837. * this driver, the 32 bit values are the indices into an array cmd_list.
  1838. * This array is used only to look up the megasas_cmd given the context. The
  1839. * free commands themselves are maintained in a linked list called cmd_pool.
  1840. */
  1841. static int megasas_alloc_cmds(struct megasas_instance *instance)
  1842. {
  1843. int i;
  1844. int j;
  1845. u32 max_cmd;
  1846. struct megasas_cmd *cmd;
  1847. max_cmd = instance->max_fw_cmds;
  1848. /*
  1849. * instance->cmd_list is an array of struct megasas_cmd pointers.
  1850. * Allocate the dynamic array first and then allocate individual
  1851. * commands.
  1852. */
  1853. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  1854. if (!instance->cmd_list) {
  1855. printk(KERN_DEBUG "megasas: out of memory\n");
  1856. return -ENOMEM;
  1857. }
  1858. for (i = 0; i < max_cmd; i++) {
  1859. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  1860. GFP_KERNEL);
  1861. if (!instance->cmd_list[i]) {
  1862. for (j = 0; j < i; j++)
  1863. kfree(instance->cmd_list[j]);
  1864. kfree(instance->cmd_list);
  1865. instance->cmd_list = NULL;
  1866. return -ENOMEM;
  1867. }
  1868. }
  1869. /*
  1870. * Add all the commands to command pool (instance->cmd_pool)
  1871. */
  1872. for (i = 0; i < max_cmd; i++) {
  1873. cmd = instance->cmd_list[i];
  1874. memset(cmd, 0, sizeof(struct megasas_cmd));
  1875. cmd->index = i;
  1876. cmd->instance = instance;
  1877. list_add_tail(&cmd->list, &instance->cmd_pool);
  1878. }
  1879. /*
  1880. * Create a frame pool and assign one frame to each cmd
  1881. */
  1882. if (megasas_create_frame_pool(instance)) {
  1883. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  1884. megasas_free_cmds(instance);
  1885. }
  1886. return 0;
  1887. }
  1888. /*
  1889. * megasas_get_pd_list_info - Returns FW's pd_list structure
  1890. * @instance: Adapter soft state
  1891. * @pd_list: pd_list structure
  1892. *
  1893. * Issues an internal command (DCMD) to get the FW's controller PD
  1894. * list structure. This information is mainly used to find out SYSTEM
  1895. * supported by the FW.
  1896. */
  1897. static int
  1898. megasas_get_pd_list(struct megasas_instance *instance)
  1899. {
  1900. int ret = 0, pd_index = 0;
  1901. struct megasas_cmd *cmd;
  1902. struct megasas_dcmd_frame *dcmd;
  1903. struct MR_PD_LIST *ci;
  1904. struct MR_PD_ADDRESS *pd_addr;
  1905. dma_addr_t ci_h = 0;
  1906. cmd = megasas_get_cmd(instance);
  1907. if (!cmd) {
  1908. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  1909. return -ENOMEM;
  1910. }
  1911. dcmd = &cmd->frame->dcmd;
  1912. ci = pci_alloc_consistent(instance->pdev,
  1913. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  1914. if (!ci) {
  1915. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  1916. megasas_return_cmd(instance, cmd);
  1917. return -ENOMEM;
  1918. }
  1919. memset(ci, 0, sizeof(*ci));
  1920. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1921. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  1922. dcmd->mbox.b[1] = 0;
  1923. dcmd->cmd = MFI_CMD_DCMD;
  1924. dcmd->cmd_status = 0xFF;
  1925. dcmd->sge_count = 1;
  1926. dcmd->flags = MFI_FRAME_DIR_READ;
  1927. dcmd->timeout = 0;
  1928. dcmd->pad_0 = 0;
  1929. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  1930. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  1931. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1932. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  1933. if (!megasas_issue_polled(instance, cmd)) {
  1934. ret = 0;
  1935. } else {
  1936. ret = -1;
  1937. }
  1938. /*
  1939. * the following function will get the instance PD LIST.
  1940. */
  1941. pd_addr = ci->addr;
  1942. if ( ret == 0 &&
  1943. (ci->count <
  1944. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  1945. memset(instance->pd_list, 0,
  1946. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  1947. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  1948. instance->pd_list[pd_addr->deviceId].tid =
  1949. pd_addr->deviceId;
  1950. instance->pd_list[pd_addr->deviceId].driveType =
  1951. pd_addr->scsiDevType;
  1952. instance->pd_list[pd_addr->deviceId].driveState =
  1953. MR_PD_STATE_SYSTEM;
  1954. pd_addr++;
  1955. }
  1956. }
  1957. pci_free_consistent(instance->pdev,
  1958. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  1959. ci, ci_h);
  1960. megasas_return_cmd(instance, cmd);
  1961. return ret;
  1962. }
  1963. /*
  1964. * megasas_get_ld_list_info - Returns FW's ld_list structure
  1965. * @instance: Adapter soft state
  1966. * @ld_list: ld_list structure
  1967. *
  1968. * Issues an internal command (DCMD) to get the FW's controller PD
  1969. * list structure. This information is mainly used to find out SYSTEM
  1970. * supported by the FW.
  1971. */
  1972. static int
  1973. megasas_get_ld_list(struct megasas_instance *instance)
  1974. {
  1975. int ret = 0, ld_index = 0, ids = 0;
  1976. struct megasas_cmd *cmd;
  1977. struct megasas_dcmd_frame *dcmd;
  1978. struct MR_LD_LIST *ci;
  1979. dma_addr_t ci_h = 0;
  1980. cmd = megasas_get_cmd(instance);
  1981. if (!cmd) {
  1982. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  1983. return -ENOMEM;
  1984. }
  1985. dcmd = &cmd->frame->dcmd;
  1986. ci = pci_alloc_consistent(instance->pdev,
  1987. sizeof(struct MR_LD_LIST),
  1988. &ci_h);
  1989. if (!ci) {
  1990. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  1991. megasas_return_cmd(instance, cmd);
  1992. return -ENOMEM;
  1993. }
  1994. memset(ci, 0, sizeof(*ci));
  1995. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1996. dcmd->cmd = MFI_CMD_DCMD;
  1997. dcmd->cmd_status = 0xFF;
  1998. dcmd->sge_count = 1;
  1999. dcmd->flags = MFI_FRAME_DIR_READ;
  2000. dcmd->timeout = 0;
  2001. dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
  2002. dcmd->opcode = MR_DCMD_LD_GET_LIST;
  2003. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2004. dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
  2005. dcmd->pad_0 = 0;
  2006. if (!megasas_issue_polled(instance, cmd)) {
  2007. ret = 0;
  2008. } else {
  2009. ret = -1;
  2010. }
  2011. /* the following function will get the instance PD LIST */
  2012. if ((ret == 0) && (ci->ldCount < MAX_LOGICAL_DRIVES)) {
  2013. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2014. for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
  2015. if (ci->ldList[ld_index].state != 0) {
  2016. ids = ci->ldList[ld_index].ref.targetId;
  2017. instance->ld_ids[ids] =
  2018. ci->ldList[ld_index].ref.targetId;
  2019. }
  2020. }
  2021. }
  2022. pci_free_consistent(instance->pdev,
  2023. sizeof(struct MR_LD_LIST),
  2024. ci,
  2025. ci_h);
  2026. megasas_return_cmd(instance, cmd);
  2027. return ret;
  2028. }
  2029. /**
  2030. * megasas_get_controller_info - Returns FW's controller structure
  2031. * @instance: Adapter soft state
  2032. * @ctrl_info: Controller information structure
  2033. *
  2034. * Issues an internal command (DCMD) to get the FW's controller structure.
  2035. * This information is mainly used to find out the maximum IO transfer per
  2036. * command supported by the FW.
  2037. */
  2038. static int
  2039. megasas_get_ctrl_info(struct megasas_instance *instance,
  2040. struct megasas_ctrl_info *ctrl_info)
  2041. {
  2042. int ret = 0;
  2043. struct megasas_cmd *cmd;
  2044. struct megasas_dcmd_frame *dcmd;
  2045. struct megasas_ctrl_info *ci;
  2046. dma_addr_t ci_h = 0;
  2047. cmd = megasas_get_cmd(instance);
  2048. if (!cmd) {
  2049. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2050. return -ENOMEM;
  2051. }
  2052. dcmd = &cmd->frame->dcmd;
  2053. ci = pci_alloc_consistent(instance->pdev,
  2054. sizeof(struct megasas_ctrl_info), &ci_h);
  2055. if (!ci) {
  2056. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2057. megasas_return_cmd(instance, cmd);
  2058. return -ENOMEM;
  2059. }
  2060. memset(ci, 0, sizeof(*ci));
  2061. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2062. dcmd->cmd = MFI_CMD_DCMD;
  2063. dcmd->cmd_status = 0xFF;
  2064. dcmd->sge_count = 1;
  2065. dcmd->flags = MFI_FRAME_DIR_READ;
  2066. dcmd->timeout = 0;
  2067. dcmd->pad_0 = 0;
  2068. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  2069. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  2070. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2071. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  2072. if (!megasas_issue_polled(instance, cmd)) {
  2073. ret = 0;
  2074. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2075. } else {
  2076. ret = -1;
  2077. }
  2078. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2079. ci, ci_h);
  2080. megasas_return_cmd(instance, cmd);
  2081. return ret;
  2082. }
  2083. /**
  2084. * megasas_issue_init_mfi - Initializes the FW
  2085. * @instance: Adapter soft state
  2086. *
  2087. * Issues the INIT MFI cmd
  2088. */
  2089. static int
  2090. megasas_issue_init_mfi(struct megasas_instance *instance)
  2091. {
  2092. u32 context;
  2093. struct megasas_cmd *cmd;
  2094. struct megasas_init_frame *init_frame;
  2095. struct megasas_init_queue_info *initq_info;
  2096. dma_addr_t init_frame_h;
  2097. dma_addr_t initq_info_h;
  2098. /*
  2099. * Prepare a init frame. Note the init frame points to queue info
  2100. * structure. Each frame has SGL allocated after first 64 bytes. For
  2101. * this frame - since we don't need any SGL - we use SGL's space as
  2102. * queue info structure
  2103. *
  2104. * We will not get a NULL command below. We just created the pool.
  2105. */
  2106. cmd = megasas_get_cmd(instance);
  2107. init_frame = (struct megasas_init_frame *)cmd->frame;
  2108. initq_info = (struct megasas_init_queue_info *)
  2109. ((unsigned long)init_frame + 64);
  2110. init_frame_h = cmd->frame_phys_addr;
  2111. initq_info_h = init_frame_h + 64;
  2112. context = init_frame->context;
  2113. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2114. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2115. init_frame->context = context;
  2116. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2117. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2118. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2119. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2120. init_frame->cmd = MFI_CMD_INIT;
  2121. init_frame->cmd_status = 0xFF;
  2122. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2123. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2124. /*
  2125. * disable the intr before firing the init frame to FW
  2126. */
  2127. instance->instancet->disable_intr(instance->reg_set);
  2128. /*
  2129. * Issue the init frame in polled mode
  2130. */
  2131. if (megasas_issue_polled(instance, cmd)) {
  2132. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2133. megasas_return_cmd(instance, cmd);
  2134. goto fail_fw_init;
  2135. }
  2136. megasas_return_cmd(instance, cmd);
  2137. return 0;
  2138. fail_fw_init:
  2139. return -EINVAL;
  2140. }
  2141. /**
  2142. * megasas_start_timer - Initializes a timer object
  2143. * @instance: Adapter soft state
  2144. * @timer: timer object to be initialized
  2145. * @fn: timer function
  2146. * @interval: time interval between timer function call
  2147. */
  2148. static inline void
  2149. megasas_start_timer(struct megasas_instance *instance,
  2150. struct timer_list *timer,
  2151. void *fn, unsigned long interval)
  2152. {
  2153. init_timer(timer);
  2154. timer->expires = jiffies + interval;
  2155. timer->data = (unsigned long)instance;
  2156. timer->function = fn;
  2157. add_timer(timer);
  2158. }
  2159. /**
  2160. * megasas_io_completion_timer - Timer fn
  2161. * @instance_addr: Address of adapter soft state
  2162. *
  2163. * Schedules tasklet for cmd completion
  2164. * if poll_mode_io is set
  2165. */
  2166. static void
  2167. megasas_io_completion_timer(unsigned long instance_addr)
  2168. {
  2169. struct megasas_instance *instance =
  2170. (struct megasas_instance *)instance_addr;
  2171. if (atomic_read(&instance->fw_outstanding))
  2172. tasklet_schedule(&instance->isr_tasklet);
  2173. /* Restart timer */
  2174. if (poll_mode_io)
  2175. mod_timer(&instance->io_completion_timer,
  2176. jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
  2177. }
  2178. /**
  2179. * megasas_init_mfi - Initializes the FW
  2180. * @instance: Adapter soft state
  2181. *
  2182. * This is the main function for initializing MFI firmware.
  2183. */
  2184. static int megasas_init_mfi(struct megasas_instance *instance)
  2185. {
  2186. u32 context_sz;
  2187. u32 reply_q_sz;
  2188. u32 max_sectors_1;
  2189. u32 max_sectors_2;
  2190. u32 tmp_sectors;
  2191. struct megasas_register_set __iomem *reg_set;
  2192. struct megasas_ctrl_info *ctrl_info;
  2193. /*
  2194. * Map the message registers
  2195. */
  2196. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  2197. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2198. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2199. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
  2200. instance->base_addr = pci_resource_start(instance->pdev, 1);
  2201. } else {
  2202. instance->base_addr = pci_resource_start(instance->pdev, 0);
  2203. }
  2204. if (pci_request_selected_regions(instance->pdev,
  2205. pci_select_bars(instance->pdev, IORESOURCE_MEM),
  2206. "megasas: LSI")) {
  2207. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2208. return -EBUSY;
  2209. }
  2210. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2211. if (!instance->reg_set) {
  2212. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2213. goto fail_ioremap;
  2214. }
  2215. reg_set = instance->reg_set;
  2216. switch(instance->pdev->device)
  2217. {
  2218. case PCI_DEVICE_ID_LSI_SAS1078R:
  2219. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2220. instance->instancet = &megasas_instance_template_ppc;
  2221. break;
  2222. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2223. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2224. instance->instancet = &megasas_instance_template_gen2;
  2225. break;
  2226. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2227. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2228. instance->instancet = &megasas_instance_template_skinny;
  2229. break;
  2230. case PCI_DEVICE_ID_LSI_SAS1064R:
  2231. case PCI_DEVICE_ID_DELL_PERC5:
  2232. default:
  2233. instance->instancet = &megasas_instance_template_xscale;
  2234. break;
  2235. }
  2236. /*
  2237. * We expect the FW state to be READY
  2238. */
  2239. if (megasas_transition_to_ready(instance))
  2240. goto fail_ready_state;
  2241. /*
  2242. * Get various operational parameters from status register
  2243. */
  2244. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2245. /*
  2246. * Reduce the max supported cmds by 1. This is to ensure that the
  2247. * reply_q_sz (1 more than the max cmd that driver may send)
  2248. * does not exceed max cmds that the FW can support
  2249. */
  2250. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2251. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2252. 0x10;
  2253. /*
  2254. * Create a pool of commands
  2255. */
  2256. if (megasas_alloc_cmds(instance))
  2257. goto fail_alloc_cmds;
  2258. /*
  2259. * Allocate memory for reply queue. Length of reply queue should
  2260. * be _one_ more than the maximum commands handled by the firmware.
  2261. *
  2262. * Note: When FW completes commands, it places corresponding contex
  2263. * values in this circular reply queue. This circular queue is a fairly
  2264. * typical producer-consumer queue. FW is the producer (of completed
  2265. * commands) and the driver is the consumer.
  2266. */
  2267. context_sz = sizeof(u32);
  2268. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2269. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2270. reply_q_sz,
  2271. &instance->reply_queue_h);
  2272. if (!instance->reply_queue) {
  2273. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2274. goto fail_reply_queue;
  2275. }
  2276. if (megasas_issue_init_mfi(instance))
  2277. goto fail_fw_init;
  2278. memset(instance->pd_list, 0 ,
  2279. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  2280. megasas_get_pd_list(instance);
  2281. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2282. megasas_get_ld_list(instance);
  2283. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  2284. /*
  2285. * Compute the max allowed sectors per IO: The controller info has two
  2286. * limits on max sectors. Driver should use the minimum of these two.
  2287. *
  2288. * 1 << stripe_sz_ops.min = max sectors per strip
  2289. *
  2290. * Note that older firmwares ( < FW ver 30) didn't report information
  2291. * to calculate max_sectors_1. So the number ended up as zero always.
  2292. */
  2293. tmp_sectors = 0;
  2294. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  2295. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  2296. ctrl_info->max_strips_per_io;
  2297. max_sectors_2 = ctrl_info->max_request_size;
  2298. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  2299. }
  2300. instance->max_sectors_per_req = instance->max_num_sge *
  2301. PAGE_SIZE / 512;
  2302. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  2303. instance->max_sectors_per_req = tmp_sectors;
  2304. kfree(ctrl_info);
  2305. /*
  2306. * Setup tasklet for cmd completion
  2307. */
  2308. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  2309. (unsigned long)instance);
  2310. /* Initialize the cmd completion timer */
  2311. if (poll_mode_io)
  2312. megasas_start_timer(instance, &instance->io_completion_timer,
  2313. megasas_io_completion_timer,
  2314. MEGASAS_COMPLETION_TIMER_INTERVAL);
  2315. return 0;
  2316. fail_fw_init:
  2317. pci_free_consistent(instance->pdev, reply_q_sz,
  2318. instance->reply_queue, instance->reply_queue_h);
  2319. fail_reply_queue:
  2320. megasas_free_cmds(instance);
  2321. fail_alloc_cmds:
  2322. fail_ready_state:
  2323. iounmap(instance->reg_set);
  2324. fail_ioremap:
  2325. pci_release_selected_regions(instance->pdev,
  2326. pci_select_bars(instance->pdev, IORESOURCE_MEM));
  2327. return -EINVAL;
  2328. }
  2329. /**
  2330. * megasas_release_mfi - Reverses the FW initialization
  2331. * @intance: Adapter soft state
  2332. */
  2333. static void megasas_release_mfi(struct megasas_instance *instance)
  2334. {
  2335. u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
  2336. pci_free_consistent(instance->pdev, reply_q_sz,
  2337. instance->reply_queue, instance->reply_queue_h);
  2338. megasas_free_cmds(instance);
  2339. iounmap(instance->reg_set);
  2340. pci_release_selected_regions(instance->pdev,
  2341. pci_select_bars(instance->pdev, IORESOURCE_MEM));
  2342. }
  2343. /**
  2344. * megasas_get_seq_num - Gets latest event sequence numbers
  2345. * @instance: Adapter soft state
  2346. * @eli: FW event log sequence numbers information
  2347. *
  2348. * FW maintains a log of all events in a non-volatile area. Upper layers would
  2349. * usually find out the latest sequence number of the events, the seq number at
  2350. * the boot etc. They would "read" all the events below the latest seq number
  2351. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  2352. * number), they would subsribe to AEN (asynchronous event notification) and
  2353. * wait for the events to happen.
  2354. */
  2355. static int
  2356. megasas_get_seq_num(struct megasas_instance *instance,
  2357. struct megasas_evt_log_info *eli)
  2358. {
  2359. struct megasas_cmd *cmd;
  2360. struct megasas_dcmd_frame *dcmd;
  2361. struct megasas_evt_log_info *el_info;
  2362. dma_addr_t el_info_h = 0;
  2363. cmd = megasas_get_cmd(instance);
  2364. if (!cmd) {
  2365. return -ENOMEM;
  2366. }
  2367. dcmd = &cmd->frame->dcmd;
  2368. el_info = pci_alloc_consistent(instance->pdev,
  2369. sizeof(struct megasas_evt_log_info),
  2370. &el_info_h);
  2371. if (!el_info) {
  2372. megasas_return_cmd(instance, cmd);
  2373. return -ENOMEM;
  2374. }
  2375. memset(el_info, 0, sizeof(*el_info));
  2376. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2377. dcmd->cmd = MFI_CMD_DCMD;
  2378. dcmd->cmd_status = 0x0;
  2379. dcmd->sge_count = 1;
  2380. dcmd->flags = MFI_FRAME_DIR_READ;
  2381. dcmd->timeout = 0;
  2382. dcmd->pad_0 = 0;
  2383. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  2384. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  2385. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  2386. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  2387. megasas_issue_blocked_cmd(instance, cmd);
  2388. /*
  2389. * Copy the data back into callers buffer
  2390. */
  2391. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  2392. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  2393. el_info, el_info_h);
  2394. megasas_return_cmd(instance, cmd);
  2395. return 0;
  2396. }
  2397. /**
  2398. * megasas_register_aen - Registers for asynchronous event notification
  2399. * @instance: Adapter soft state
  2400. * @seq_num: The starting sequence number
  2401. * @class_locale: Class of the event
  2402. *
  2403. * This function subscribes for AEN for events beyond the @seq_num. It requests
  2404. * to be notified if and only if the event is of type @class_locale
  2405. */
  2406. static int
  2407. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  2408. u32 class_locale_word)
  2409. {
  2410. int ret_val;
  2411. struct megasas_cmd *cmd;
  2412. struct megasas_dcmd_frame *dcmd;
  2413. union megasas_evt_class_locale curr_aen;
  2414. union megasas_evt_class_locale prev_aen;
  2415. /*
  2416. * If there an AEN pending already (aen_cmd), check if the
  2417. * class_locale of that pending AEN is inclusive of the new
  2418. * AEN request we currently have. If it is, then we don't have
  2419. * to do anything. In other words, whichever events the current
  2420. * AEN request is subscribing to, have already been subscribed
  2421. * to.
  2422. *
  2423. * If the old_cmd is _not_ inclusive, then we have to abort
  2424. * that command, form a class_locale that is superset of both
  2425. * old and current and re-issue to the FW
  2426. */
  2427. curr_aen.word = class_locale_word;
  2428. if (instance->aen_cmd) {
  2429. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  2430. /*
  2431. * A class whose enum value is smaller is inclusive of all
  2432. * higher values. If a PROGRESS (= -1) was previously
  2433. * registered, then a new registration requests for higher
  2434. * classes need not be sent to FW. They are automatically
  2435. * included.
  2436. *
  2437. * Locale numbers don't have such hierarchy. They are bitmap
  2438. * values
  2439. */
  2440. if ((prev_aen.members.class <= curr_aen.members.class) &&
  2441. !((prev_aen.members.locale & curr_aen.members.locale) ^
  2442. curr_aen.members.locale)) {
  2443. /*
  2444. * Previously issued event registration includes
  2445. * current request. Nothing to do.
  2446. */
  2447. return 0;
  2448. } else {
  2449. curr_aen.members.locale |= prev_aen.members.locale;
  2450. if (prev_aen.members.class < curr_aen.members.class)
  2451. curr_aen.members.class = prev_aen.members.class;
  2452. instance->aen_cmd->abort_aen = 1;
  2453. ret_val = megasas_issue_blocked_abort_cmd(instance,
  2454. instance->
  2455. aen_cmd);
  2456. if (ret_val) {
  2457. printk(KERN_DEBUG "megasas: Failed to abort "
  2458. "previous AEN command\n");
  2459. return ret_val;
  2460. }
  2461. }
  2462. }
  2463. cmd = megasas_get_cmd(instance);
  2464. if (!cmd)
  2465. return -ENOMEM;
  2466. dcmd = &cmd->frame->dcmd;
  2467. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  2468. /*
  2469. * Prepare DCMD for aen registration
  2470. */
  2471. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2472. dcmd->cmd = MFI_CMD_DCMD;
  2473. dcmd->cmd_status = 0x0;
  2474. dcmd->sge_count = 1;
  2475. dcmd->flags = MFI_FRAME_DIR_READ;
  2476. dcmd->timeout = 0;
  2477. dcmd->pad_0 = 0;
  2478. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  2479. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  2480. dcmd->mbox.w[0] = seq_num;
  2481. dcmd->mbox.w[1] = curr_aen.word;
  2482. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  2483. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  2484. if (instance->aen_cmd != NULL) {
  2485. megasas_return_cmd(instance, cmd);
  2486. return 0;
  2487. }
  2488. /*
  2489. * Store reference to the cmd used to register for AEN. When an
  2490. * application wants us to register for AEN, we have to abort this
  2491. * cmd and re-register with a new EVENT LOCALE supplied by that app
  2492. */
  2493. instance->aen_cmd = cmd;
  2494. /*
  2495. * Issue the aen registration frame
  2496. */
  2497. instance->instancet->fire_cmd(instance,
  2498. cmd->frame_phys_addr, 0, instance->reg_set);
  2499. return 0;
  2500. }
  2501. /**
  2502. * megasas_start_aen - Subscribes to AEN during driver load time
  2503. * @instance: Adapter soft state
  2504. */
  2505. static int megasas_start_aen(struct megasas_instance *instance)
  2506. {
  2507. struct megasas_evt_log_info eli;
  2508. union megasas_evt_class_locale class_locale;
  2509. /*
  2510. * Get the latest sequence number from FW
  2511. */
  2512. memset(&eli, 0, sizeof(eli));
  2513. if (megasas_get_seq_num(instance, &eli))
  2514. return -1;
  2515. /*
  2516. * Register AEN with FW for latest sequence number plus 1
  2517. */
  2518. class_locale.members.reserved = 0;
  2519. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2520. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2521. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  2522. class_locale.word);
  2523. }
  2524. /**
  2525. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  2526. * @instance: Adapter soft state
  2527. */
  2528. static int megasas_io_attach(struct megasas_instance *instance)
  2529. {
  2530. struct Scsi_Host *host = instance->host;
  2531. /*
  2532. * Export parameters required by SCSI mid-layer
  2533. */
  2534. host->irq = instance->pdev->irq;
  2535. host->unique_id = instance->unique_id;
  2536. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2537. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  2538. host->can_queue =
  2539. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  2540. } else
  2541. host->can_queue =
  2542. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  2543. host->this_id = instance->init_id;
  2544. host->sg_tablesize = instance->max_num_sge;
  2545. host->max_sectors = instance->max_sectors_per_req;
  2546. host->cmd_per_lun = 128;
  2547. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  2548. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  2549. host->max_lun = MEGASAS_MAX_LUN;
  2550. host->max_cmd_len = 16;
  2551. /*
  2552. * Notify the mid-layer about the new controller
  2553. */
  2554. if (scsi_add_host(host, &instance->pdev->dev)) {
  2555. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  2556. return -ENODEV;
  2557. }
  2558. /*
  2559. * Trigger SCSI to scan our drives
  2560. */
  2561. scsi_scan_host(host);
  2562. return 0;
  2563. }
  2564. static int
  2565. megasas_set_dma_mask(struct pci_dev *pdev)
  2566. {
  2567. /*
  2568. * All our contollers are capable of performing 64-bit DMA
  2569. */
  2570. if (IS_DMA64) {
  2571. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  2572. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  2573. goto fail_set_dma_mask;
  2574. }
  2575. } else {
  2576. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  2577. goto fail_set_dma_mask;
  2578. }
  2579. return 0;
  2580. fail_set_dma_mask:
  2581. return 1;
  2582. }
  2583. /**
  2584. * megasas_probe_one - PCI hotplug entry point
  2585. * @pdev: PCI device structure
  2586. * @id: PCI ids of supported hotplugged adapter
  2587. */
  2588. static int __devinit
  2589. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  2590. {
  2591. int rval;
  2592. struct Scsi_Host *host;
  2593. struct megasas_instance *instance;
  2594. /*
  2595. * Announce PCI information
  2596. */
  2597. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  2598. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  2599. pdev->subsystem_device);
  2600. printk("bus %d:slot %d:func %d\n",
  2601. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  2602. /*
  2603. * PCI prepping: enable device set bus mastering and dma mask
  2604. */
  2605. rval = pci_enable_device_mem(pdev);
  2606. if (rval) {
  2607. return rval;
  2608. }
  2609. pci_set_master(pdev);
  2610. if (megasas_set_dma_mask(pdev))
  2611. goto fail_set_dma_mask;
  2612. host = scsi_host_alloc(&megasas_template,
  2613. sizeof(struct megasas_instance));
  2614. if (!host) {
  2615. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  2616. goto fail_alloc_instance;
  2617. }
  2618. instance = (struct megasas_instance *)host->hostdata;
  2619. memset(instance, 0, sizeof(*instance));
  2620. instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
  2621. &instance->producer_h);
  2622. instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
  2623. &instance->consumer_h);
  2624. if (!instance->producer || !instance->consumer) {
  2625. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  2626. "producer, consumer\n");
  2627. goto fail_alloc_dma_buf;
  2628. }
  2629. *instance->producer = 0;
  2630. *instance->consumer = 0;
  2631. megasas_poll_wait_aen = 0;
  2632. instance->flag_ieee = 0;
  2633. instance->ev = NULL;
  2634. instance->evt_detail = pci_alloc_consistent(pdev,
  2635. sizeof(struct
  2636. megasas_evt_detail),
  2637. &instance->evt_detail_h);
  2638. if (!instance->evt_detail) {
  2639. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  2640. "event detail structure\n");
  2641. goto fail_alloc_dma_buf;
  2642. }
  2643. /*
  2644. * Initialize locks and queues
  2645. */
  2646. INIT_LIST_HEAD(&instance->cmd_pool);
  2647. atomic_set(&instance->fw_outstanding,0);
  2648. init_waitqueue_head(&instance->int_cmd_wait_q);
  2649. init_waitqueue_head(&instance->abort_cmd_wait_q);
  2650. spin_lock_init(&instance->cmd_pool_lock);
  2651. spin_lock_init(&instance->fire_lock);
  2652. spin_lock_init(&instance->completion_lock);
  2653. spin_lock_init(&poll_aen_lock);
  2654. mutex_init(&instance->aen_mutex);
  2655. /*
  2656. * Initialize PCI related and misc parameters
  2657. */
  2658. instance->pdev = pdev;
  2659. instance->host = host;
  2660. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  2661. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  2662. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2663. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  2664. instance->flag_ieee = 1;
  2665. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  2666. } else
  2667. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  2668. megasas_dbg_lvl = 0;
  2669. instance->flag = 0;
  2670. instance->unload = 1;
  2671. instance->last_time = 0;
  2672. /*
  2673. * Initialize MFI Firmware
  2674. */
  2675. if (megasas_init_mfi(instance))
  2676. goto fail_init_mfi;
  2677. /*
  2678. * Register IRQ
  2679. */
  2680. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
  2681. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  2682. goto fail_irq;
  2683. }
  2684. instance->instancet->enable_intr(instance->reg_set);
  2685. /*
  2686. * Store instance in PCI softstate
  2687. */
  2688. pci_set_drvdata(pdev, instance);
  2689. /*
  2690. * Add this controller to megasas_mgmt_info structure so that it
  2691. * can be exported to management applications
  2692. */
  2693. megasas_mgmt_info.count++;
  2694. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  2695. megasas_mgmt_info.max_index++;
  2696. /*
  2697. * Initiate AEN (Asynchronous Event Notification)
  2698. */
  2699. if (megasas_start_aen(instance)) {
  2700. printk(KERN_DEBUG "megasas: start aen failed\n");
  2701. goto fail_start_aen;
  2702. }
  2703. /*
  2704. * Register with SCSI mid-layer
  2705. */
  2706. if (megasas_io_attach(instance))
  2707. goto fail_io_attach;
  2708. instance->unload = 0;
  2709. return 0;
  2710. fail_start_aen:
  2711. fail_io_attach:
  2712. megasas_mgmt_info.count--;
  2713. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  2714. megasas_mgmt_info.max_index--;
  2715. pci_set_drvdata(pdev, NULL);
  2716. instance->instancet->disable_intr(instance->reg_set);
  2717. free_irq(instance->pdev->irq, instance);
  2718. megasas_release_mfi(instance);
  2719. fail_irq:
  2720. fail_init_mfi:
  2721. fail_alloc_dma_buf:
  2722. if (instance->evt_detail)
  2723. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2724. instance->evt_detail,
  2725. instance->evt_detail_h);
  2726. if (instance->producer)
  2727. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2728. instance->producer_h);
  2729. if (instance->consumer)
  2730. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2731. instance->consumer_h);
  2732. scsi_host_put(host);
  2733. fail_alloc_instance:
  2734. fail_set_dma_mask:
  2735. pci_disable_device(pdev);
  2736. return -ENODEV;
  2737. }
  2738. /**
  2739. * megasas_flush_cache - Requests FW to flush all its caches
  2740. * @instance: Adapter soft state
  2741. */
  2742. static void megasas_flush_cache(struct megasas_instance *instance)
  2743. {
  2744. struct megasas_cmd *cmd;
  2745. struct megasas_dcmd_frame *dcmd;
  2746. cmd = megasas_get_cmd(instance);
  2747. if (!cmd)
  2748. return;
  2749. dcmd = &cmd->frame->dcmd;
  2750. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2751. dcmd->cmd = MFI_CMD_DCMD;
  2752. dcmd->cmd_status = 0x0;
  2753. dcmd->sge_count = 0;
  2754. dcmd->flags = MFI_FRAME_DIR_NONE;
  2755. dcmd->timeout = 0;
  2756. dcmd->pad_0 = 0;
  2757. dcmd->data_xfer_len = 0;
  2758. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  2759. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  2760. megasas_issue_blocked_cmd(instance, cmd);
  2761. megasas_return_cmd(instance, cmd);
  2762. return;
  2763. }
  2764. /**
  2765. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  2766. * @instance: Adapter soft state
  2767. * @opcode: Shutdown/Hibernate
  2768. */
  2769. static void megasas_shutdown_controller(struct megasas_instance *instance,
  2770. u32 opcode)
  2771. {
  2772. struct megasas_cmd *cmd;
  2773. struct megasas_dcmd_frame *dcmd;
  2774. cmd = megasas_get_cmd(instance);
  2775. if (!cmd)
  2776. return;
  2777. if (instance->aen_cmd)
  2778. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  2779. dcmd = &cmd->frame->dcmd;
  2780. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2781. dcmd->cmd = MFI_CMD_DCMD;
  2782. dcmd->cmd_status = 0x0;
  2783. dcmd->sge_count = 0;
  2784. dcmd->flags = MFI_FRAME_DIR_NONE;
  2785. dcmd->timeout = 0;
  2786. dcmd->pad_0 = 0;
  2787. dcmd->data_xfer_len = 0;
  2788. dcmd->opcode = opcode;
  2789. megasas_issue_blocked_cmd(instance, cmd);
  2790. megasas_return_cmd(instance, cmd);
  2791. return;
  2792. }
  2793. #ifdef CONFIG_PM
  2794. /**
  2795. * megasas_suspend - driver suspend entry point
  2796. * @pdev: PCI device structure
  2797. * @state: PCI power state to suspend routine
  2798. */
  2799. static int
  2800. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  2801. {
  2802. struct Scsi_Host *host;
  2803. struct megasas_instance *instance;
  2804. instance = pci_get_drvdata(pdev);
  2805. host = instance->host;
  2806. instance->unload = 1;
  2807. if (poll_mode_io)
  2808. del_timer_sync(&instance->io_completion_timer);
  2809. megasas_flush_cache(instance);
  2810. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  2811. /* cancel the delayed work if this work still in queue */
  2812. if (instance->ev != NULL) {
  2813. struct megasas_aen_event *ev = instance->ev;
  2814. cancel_delayed_work(
  2815. (struct delayed_work *)&ev->hotplug_work);
  2816. flush_scheduled_work();
  2817. instance->ev = NULL;
  2818. }
  2819. tasklet_kill(&instance->isr_tasklet);
  2820. pci_set_drvdata(instance->pdev, instance);
  2821. instance->instancet->disable_intr(instance->reg_set);
  2822. free_irq(instance->pdev->irq, instance);
  2823. pci_save_state(pdev);
  2824. pci_disable_device(pdev);
  2825. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2826. return 0;
  2827. }
  2828. /**
  2829. * megasas_resume- driver resume entry point
  2830. * @pdev: PCI device structure
  2831. */
  2832. static int
  2833. megasas_resume(struct pci_dev *pdev)
  2834. {
  2835. int rval;
  2836. struct Scsi_Host *host;
  2837. struct megasas_instance *instance;
  2838. instance = pci_get_drvdata(pdev);
  2839. host = instance->host;
  2840. pci_set_power_state(pdev, PCI_D0);
  2841. pci_enable_wake(pdev, PCI_D0, 0);
  2842. pci_restore_state(pdev);
  2843. /*
  2844. * PCI prepping: enable device set bus mastering and dma mask
  2845. */
  2846. rval = pci_enable_device_mem(pdev);
  2847. if (rval) {
  2848. printk(KERN_ERR "megasas: Enable device failed\n");
  2849. return rval;
  2850. }
  2851. pci_set_master(pdev);
  2852. if (megasas_set_dma_mask(pdev))
  2853. goto fail_set_dma_mask;
  2854. /*
  2855. * Initialize MFI Firmware
  2856. */
  2857. *instance->producer = 0;
  2858. *instance->consumer = 0;
  2859. atomic_set(&instance->fw_outstanding, 0);
  2860. /*
  2861. * We expect the FW state to be READY
  2862. */
  2863. if (megasas_transition_to_ready(instance))
  2864. goto fail_ready_state;
  2865. if (megasas_issue_init_mfi(instance))
  2866. goto fail_init_mfi;
  2867. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  2868. (unsigned long)instance);
  2869. /*
  2870. * Register IRQ
  2871. */
  2872. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
  2873. "megasas", instance)) {
  2874. printk(KERN_ERR "megasas: Failed to register IRQ\n");
  2875. goto fail_irq;
  2876. }
  2877. instance->instancet->enable_intr(instance->reg_set);
  2878. /*
  2879. * Initiate AEN (Asynchronous Event Notification)
  2880. */
  2881. if (megasas_start_aen(instance))
  2882. printk(KERN_ERR "megasas: Start AEN failed\n");
  2883. /* Initialize the cmd completion timer */
  2884. if (poll_mode_io)
  2885. megasas_start_timer(instance, &instance->io_completion_timer,
  2886. megasas_io_completion_timer,
  2887. MEGASAS_COMPLETION_TIMER_INTERVAL);
  2888. instance->unload = 0;
  2889. return 0;
  2890. fail_irq:
  2891. fail_init_mfi:
  2892. if (instance->evt_detail)
  2893. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2894. instance->evt_detail,
  2895. instance->evt_detail_h);
  2896. if (instance->producer)
  2897. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2898. instance->producer_h);
  2899. if (instance->consumer)
  2900. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2901. instance->consumer_h);
  2902. scsi_host_put(host);
  2903. fail_set_dma_mask:
  2904. fail_ready_state:
  2905. pci_disable_device(pdev);
  2906. return -ENODEV;
  2907. }
  2908. #else
  2909. #define megasas_suspend NULL
  2910. #define megasas_resume NULL
  2911. #endif
  2912. /**
  2913. * megasas_detach_one - PCI hot"un"plug entry point
  2914. * @pdev: PCI device structure
  2915. */
  2916. static void __devexit megasas_detach_one(struct pci_dev *pdev)
  2917. {
  2918. int i;
  2919. struct Scsi_Host *host;
  2920. struct megasas_instance *instance;
  2921. instance = pci_get_drvdata(pdev);
  2922. instance->unload = 1;
  2923. host = instance->host;
  2924. if (poll_mode_io)
  2925. del_timer_sync(&instance->io_completion_timer);
  2926. scsi_remove_host(instance->host);
  2927. megasas_flush_cache(instance);
  2928. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  2929. /* cancel the delayed work if this work still in queue*/
  2930. if (instance->ev != NULL) {
  2931. struct megasas_aen_event *ev = instance->ev;
  2932. cancel_delayed_work(
  2933. (struct delayed_work *)&ev->hotplug_work);
  2934. flush_scheduled_work();
  2935. instance->ev = NULL;
  2936. }
  2937. tasklet_kill(&instance->isr_tasklet);
  2938. /*
  2939. * Take the instance off the instance array. Note that we will not
  2940. * decrement the max_index. We let this array be sparse array
  2941. */
  2942. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2943. if (megasas_mgmt_info.instance[i] == instance) {
  2944. megasas_mgmt_info.count--;
  2945. megasas_mgmt_info.instance[i] = NULL;
  2946. break;
  2947. }
  2948. }
  2949. pci_set_drvdata(instance->pdev, NULL);
  2950. instance->instancet->disable_intr(instance->reg_set);
  2951. free_irq(instance->pdev->irq, instance);
  2952. megasas_release_mfi(instance);
  2953. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2954. instance->evt_detail, instance->evt_detail_h);
  2955. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2956. instance->producer_h);
  2957. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2958. instance->consumer_h);
  2959. scsi_host_put(host);
  2960. pci_set_drvdata(pdev, NULL);
  2961. pci_disable_device(pdev);
  2962. return;
  2963. }
  2964. /**
  2965. * megasas_shutdown - Shutdown entry point
  2966. * @device: Generic device structure
  2967. */
  2968. static void megasas_shutdown(struct pci_dev *pdev)
  2969. {
  2970. struct megasas_instance *instance = pci_get_drvdata(pdev);
  2971. instance->unload = 1;
  2972. megasas_flush_cache(instance);
  2973. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  2974. }
  2975. /**
  2976. * megasas_mgmt_open - char node "open" entry point
  2977. */
  2978. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  2979. {
  2980. cycle_kernel_lock();
  2981. /*
  2982. * Allow only those users with admin rights
  2983. */
  2984. if (!capable(CAP_SYS_ADMIN))
  2985. return -EACCES;
  2986. return 0;
  2987. }
  2988. /**
  2989. * megasas_mgmt_fasync - Async notifier registration from applications
  2990. *
  2991. * This function adds the calling process to a driver global queue. When an
  2992. * event occurs, SIGIO will be sent to all processes in this queue.
  2993. */
  2994. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  2995. {
  2996. int rc;
  2997. mutex_lock(&megasas_async_queue_mutex);
  2998. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  2999. mutex_unlock(&megasas_async_queue_mutex);
  3000. if (rc >= 0) {
  3001. /* For sanity check when we get ioctl */
  3002. filep->private_data = filep;
  3003. return 0;
  3004. }
  3005. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  3006. return rc;
  3007. }
  3008. /**
  3009. * megasas_mgmt_poll - char node "poll" entry point
  3010. * */
  3011. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  3012. {
  3013. unsigned int mask;
  3014. unsigned long flags;
  3015. poll_wait(file, &megasas_poll_wait, wait);
  3016. spin_lock_irqsave(&poll_aen_lock, flags);
  3017. if (megasas_poll_wait_aen)
  3018. mask = (POLLIN | POLLRDNORM);
  3019. else
  3020. mask = 0;
  3021. spin_unlock_irqrestore(&poll_aen_lock, flags);
  3022. return mask;
  3023. }
  3024. /**
  3025. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  3026. * @instance: Adapter soft state
  3027. * @argp: User's ioctl packet
  3028. */
  3029. static int
  3030. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  3031. struct megasas_iocpacket __user * user_ioc,
  3032. struct megasas_iocpacket *ioc)
  3033. {
  3034. struct megasas_sge32 *kern_sge32;
  3035. struct megasas_cmd *cmd;
  3036. void *kbuff_arr[MAX_IOCTL_SGE];
  3037. dma_addr_t buf_handle = 0;
  3038. int error = 0, i;
  3039. void *sense = NULL;
  3040. dma_addr_t sense_handle;
  3041. unsigned long *sense_ptr;
  3042. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  3043. if (ioc->sge_count > MAX_IOCTL_SGE) {
  3044. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  3045. ioc->sge_count, MAX_IOCTL_SGE);
  3046. return -EINVAL;
  3047. }
  3048. cmd = megasas_get_cmd(instance);
  3049. if (!cmd) {
  3050. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  3051. return -ENOMEM;
  3052. }
  3053. /*
  3054. * User's IOCTL packet has 2 frames (maximum). Copy those two
  3055. * frames into our cmd's frames. cmd->frame's context will get
  3056. * overwritten when we copy from user's frames. So set that value
  3057. * alone separately
  3058. */
  3059. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  3060. cmd->frame->hdr.context = cmd->index;
  3061. cmd->frame->hdr.pad_0 = 0;
  3062. /*
  3063. * The management interface between applications and the fw uses
  3064. * MFI frames. E.g, RAID configuration changes, LD property changes
  3065. * etc are accomplishes through different kinds of MFI frames. The
  3066. * driver needs to care only about substituting user buffers with
  3067. * kernel buffers in SGLs. The location of SGL is embedded in the
  3068. * struct iocpacket itself.
  3069. */
  3070. kern_sge32 = (struct megasas_sge32 *)
  3071. ((unsigned long)cmd->frame + ioc->sgl_off);
  3072. /*
  3073. * For each user buffer, create a mirror buffer and copy in
  3074. */
  3075. for (i = 0; i < ioc->sge_count; i++) {
  3076. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  3077. ioc->sgl[i].iov_len,
  3078. &buf_handle, GFP_KERNEL);
  3079. if (!kbuff_arr[i]) {
  3080. printk(KERN_DEBUG "megasas: Failed to alloc "
  3081. "kernel SGL buffer for IOCTL \n");
  3082. error = -ENOMEM;
  3083. goto out;
  3084. }
  3085. /*
  3086. * We don't change the dma_coherent_mask, so
  3087. * pci_alloc_consistent only returns 32bit addresses
  3088. */
  3089. kern_sge32[i].phys_addr = (u32) buf_handle;
  3090. kern_sge32[i].length = ioc->sgl[i].iov_len;
  3091. /*
  3092. * We created a kernel buffer corresponding to the
  3093. * user buffer. Now copy in from the user buffer
  3094. */
  3095. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  3096. (u32) (ioc->sgl[i].iov_len))) {
  3097. error = -EFAULT;
  3098. goto out;
  3099. }
  3100. }
  3101. if (ioc->sense_len) {
  3102. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  3103. &sense_handle, GFP_KERNEL);
  3104. if (!sense) {
  3105. error = -ENOMEM;
  3106. goto out;
  3107. }
  3108. sense_ptr =
  3109. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  3110. *sense_ptr = sense_handle;
  3111. }
  3112. /*
  3113. * Set the sync_cmd flag so that the ISR knows not to complete this
  3114. * cmd to the SCSI mid-layer
  3115. */
  3116. cmd->sync_cmd = 1;
  3117. megasas_issue_blocked_cmd(instance, cmd);
  3118. cmd->sync_cmd = 0;
  3119. /*
  3120. * copy out the kernel buffers to user buffers
  3121. */
  3122. for (i = 0; i < ioc->sge_count; i++) {
  3123. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  3124. ioc->sgl[i].iov_len)) {
  3125. error = -EFAULT;
  3126. goto out;
  3127. }
  3128. }
  3129. /*
  3130. * copy out the sense
  3131. */
  3132. if (ioc->sense_len) {
  3133. /*
  3134. * sense_ptr points to the location that has the user
  3135. * sense buffer address
  3136. */
  3137. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  3138. ioc->sense_off);
  3139. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  3140. sense, ioc->sense_len)) {
  3141. printk(KERN_ERR "megasas: Failed to copy out to user "
  3142. "sense data\n");
  3143. error = -EFAULT;
  3144. goto out;
  3145. }
  3146. }
  3147. /*
  3148. * copy the status codes returned by the fw
  3149. */
  3150. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  3151. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  3152. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  3153. error = -EFAULT;
  3154. }
  3155. out:
  3156. if (sense) {
  3157. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  3158. sense, sense_handle);
  3159. }
  3160. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  3161. dma_free_coherent(&instance->pdev->dev,
  3162. kern_sge32[i].length,
  3163. kbuff_arr[i], kern_sge32[i].phys_addr);
  3164. }
  3165. megasas_return_cmd(instance, cmd);
  3166. return error;
  3167. }
  3168. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  3169. {
  3170. struct megasas_iocpacket __user *user_ioc =
  3171. (struct megasas_iocpacket __user *)arg;
  3172. struct megasas_iocpacket *ioc;
  3173. struct megasas_instance *instance;
  3174. int error;
  3175. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  3176. if (!ioc)
  3177. return -ENOMEM;
  3178. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  3179. error = -EFAULT;
  3180. goto out_kfree_ioc;
  3181. }
  3182. instance = megasas_lookup_instance(ioc->host_no);
  3183. if (!instance) {
  3184. error = -ENODEV;
  3185. goto out_kfree_ioc;
  3186. }
  3187. if (instance->hw_crit_error == 1) {
  3188. printk(KERN_DEBUG "Controller in Crit ERROR\n");
  3189. error = -ENODEV;
  3190. goto out_kfree_ioc;
  3191. }
  3192. if (instance->unload == 1) {
  3193. error = -ENODEV;
  3194. goto out_kfree_ioc;
  3195. }
  3196. /*
  3197. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  3198. */
  3199. if (down_interruptible(&instance->ioctl_sem)) {
  3200. error = -ERESTARTSYS;
  3201. goto out_kfree_ioc;
  3202. }
  3203. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  3204. up(&instance->ioctl_sem);
  3205. out_kfree_ioc:
  3206. kfree(ioc);
  3207. return error;
  3208. }
  3209. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  3210. {
  3211. struct megasas_instance *instance;
  3212. struct megasas_aen aen;
  3213. int error;
  3214. if (file->private_data != file) {
  3215. printk(KERN_DEBUG "megasas: fasync_helper was not "
  3216. "called first\n");
  3217. return -EINVAL;
  3218. }
  3219. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  3220. return -EFAULT;
  3221. instance = megasas_lookup_instance(aen.host_no);
  3222. if (!instance)
  3223. return -ENODEV;
  3224. if (instance->hw_crit_error == 1) {
  3225. error = -ENODEV;
  3226. }
  3227. if (instance->unload == 1) {
  3228. return -ENODEV;
  3229. }
  3230. mutex_lock(&instance->aen_mutex);
  3231. error = megasas_register_aen(instance, aen.seq_num,
  3232. aen.class_locale_word);
  3233. mutex_unlock(&instance->aen_mutex);
  3234. return error;
  3235. }
  3236. /**
  3237. * megasas_mgmt_ioctl - char node ioctl entry point
  3238. */
  3239. static long
  3240. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3241. {
  3242. switch (cmd) {
  3243. case MEGASAS_IOC_FIRMWARE:
  3244. return megasas_mgmt_ioctl_fw(file, arg);
  3245. case MEGASAS_IOC_GET_AEN:
  3246. return megasas_mgmt_ioctl_aen(file, arg);
  3247. }
  3248. return -ENOTTY;
  3249. }
  3250. #ifdef CONFIG_COMPAT
  3251. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  3252. {
  3253. struct compat_megasas_iocpacket __user *cioc =
  3254. (struct compat_megasas_iocpacket __user *)arg;
  3255. struct megasas_iocpacket __user *ioc =
  3256. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  3257. int i;
  3258. int error = 0;
  3259. compat_uptr_t ptr;
  3260. if (clear_user(ioc, sizeof(*ioc)))
  3261. return -EFAULT;
  3262. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  3263. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  3264. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  3265. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  3266. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  3267. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  3268. return -EFAULT;
  3269. /*
  3270. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  3271. * sense_len is not null, so prepare the 64bit value under
  3272. * the same condition.
  3273. */
  3274. if (ioc->sense_len) {
  3275. void __user **sense_ioc_ptr =
  3276. (void __user **)(ioc->frame.raw + ioc->sense_off);
  3277. compat_uptr_t *sense_cioc_ptr =
  3278. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  3279. if (get_user(ptr, sense_cioc_ptr) ||
  3280. put_user(compat_ptr(ptr), sense_ioc_ptr))
  3281. return -EFAULT;
  3282. }
  3283. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  3284. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  3285. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  3286. copy_in_user(&ioc->sgl[i].iov_len,
  3287. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  3288. return -EFAULT;
  3289. }
  3290. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  3291. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  3292. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  3293. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  3294. return -EFAULT;
  3295. }
  3296. return error;
  3297. }
  3298. static long
  3299. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  3300. unsigned long arg)
  3301. {
  3302. switch (cmd) {
  3303. case MEGASAS_IOC_FIRMWARE32:
  3304. return megasas_mgmt_compat_ioctl_fw(file, arg);
  3305. case MEGASAS_IOC_GET_AEN:
  3306. return megasas_mgmt_ioctl_aen(file, arg);
  3307. }
  3308. return -ENOTTY;
  3309. }
  3310. #endif
  3311. /*
  3312. * File operations structure for management interface
  3313. */
  3314. static const struct file_operations megasas_mgmt_fops = {
  3315. .owner = THIS_MODULE,
  3316. .open = megasas_mgmt_open,
  3317. .fasync = megasas_mgmt_fasync,
  3318. .unlocked_ioctl = megasas_mgmt_ioctl,
  3319. .poll = megasas_mgmt_poll,
  3320. #ifdef CONFIG_COMPAT
  3321. .compat_ioctl = megasas_mgmt_compat_ioctl,
  3322. #endif
  3323. };
  3324. /*
  3325. * PCI hotplug support registration structure
  3326. */
  3327. static struct pci_driver megasas_pci_driver = {
  3328. .name = "megaraid_sas",
  3329. .id_table = megasas_pci_table,
  3330. .probe = megasas_probe_one,
  3331. .remove = __devexit_p(megasas_detach_one),
  3332. .suspend = megasas_suspend,
  3333. .resume = megasas_resume,
  3334. .shutdown = megasas_shutdown,
  3335. };
  3336. /*
  3337. * Sysfs driver attributes
  3338. */
  3339. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  3340. {
  3341. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  3342. MEGASAS_VERSION);
  3343. }
  3344. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  3345. static ssize_t
  3346. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  3347. {
  3348. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  3349. MEGASAS_RELDATE);
  3350. }
  3351. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  3352. NULL);
  3353. static ssize_t
  3354. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  3355. {
  3356. return sprintf(buf, "%u\n", support_poll_for_event);
  3357. }
  3358. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  3359. megasas_sysfs_show_support_poll_for_event, NULL);
  3360. static ssize_t
  3361. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  3362. {
  3363. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  3364. }
  3365. static ssize_t
  3366. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  3367. {
  3368. int retval = count;
  3369. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  3370. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  3371. retval = -EINVAL;
  3372. }
  3373. return retval;
  3374. }
  3375. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  3376. megasas_sysfs_set_dbg_lvl);
  3377. static ssize_t
  3378. megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
  3379. {
  3380. return sprintf(buf, "%u\n", poll_mode_io);
  3381. }
  3382. static ssize_t
  3383. megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
  3384. const char *buf, size_t count)
  3385. {
  3386. int retval = count;
  3387. int tmp = poll_mode_io;
  3388. int i;
  3389. struct megasas_instance *instance;
  3390. if (sscanf(buf, "%u", &poll_mode_io) < 1) {
  3391. printk(KERN_ERR "megasas: could not set poll_mode_io\n");
  3392. retval = -EINVAL;
  3393. }
  3394. /*
  3395. * Check if poll_mode_io is already set or is same as previous value
  3396. */
  3397. if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
  3398. goto out;
  3399. if (poll_mode_io) {
  3400. /*
  3401. * Start timers for all adapters
  3402. */
  3403. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3404. instance = megasas_mgmt_info.instance[i];
  3405. if (instance) {
  3406. megasas_start_timer(instance,
  3407. &instance->io_completion_timer,
  3408. megasas_io_completion_timer,
  3409. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3410. }
  3411. }
  3412. } else {
  3413. /*
  3414. * Delete timers for all adapters
  3415. */
  3416. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3417. instance = megasas_mgmt_info.instance[i];
  3418. if (instance)
  3419. del_timer_sync(&instance->io_completion_timer);
  3420. }
  3421. }
  3422. out:
  3423. return retval;
  3424. }
  3425. static void
  3426. megasas_aen_polling(struct work_struct *work)
  3427. {
  3428. struct megasas_aen_event *ev =
  3429. container_of(work, struct megasas_aen_event, hotplug_work);
  3430. struct megasas_instance *instance = ev->instance;
  3431. union megasas_evt_class_locale class_locale;
  3432. struct Scsi_Host *host;
  3433. struct scsi_device *sdev1;
  3434. u16 pd_index = 0;
  3435. u16 ld_index = 0;
  3436. int i, j, doscan = 0;
  3437. u32 seq_num;
  3438. int error;
  3439. if (!instance) {
  3440. printk(KERN_ERR "invalid instance!\n");
  3441. kfree(ev);
  3442. return;
  3443. }
  3444. instance->ev = NULL;
  3445. host = instance->host;
  3446. if (instance->evt_detail) {
  3447. switch (instance->evt_detail->code) {
  3448. case MR_EVT_PD_INSERTED:
  3449. if (megasas_get_pd_list(instance) == 0) {
  3450. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  3451. for (j = 0;
  3452. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3453. j++) {
  3454. pd_index =
  3455. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3456. sdev1 =
  3457. scsi_device_lookup(host, i, j, 0);
  3458. if (instance->pd_list[pd_index].driveState
  3459. == MR_PD_STATE_SYSTEM) {
  3460. if (!sdev1) {
  3461. scsi_add_device(host, i, j, 0);
  3462. }
  3463. if (sdev1)
  3464. scsi_device_put(sdev1);
  3465. }
  3466. }
  3467. }
  3468. }
  3469. doscan = 0;
  3470. break;
  3471. case MR_EVT_PD_REMOVED:
  3472. if (megasas_get_pd_list(instance) == 0) {
  3473. megasas_get_pd_list(instance);
  3474. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  3475. for (j = 0;
  3476. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3477. j++) {
  3478. pd_index =
  3479. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3480. sdev1 =
  3481. scsi_device_lookup(host, i, j, 0);
  3482. if (instance->pd_list[pd_index].driveState
  3483. == MR_PD_STATE_SYSTEM) {
  3484. if (sdev1) {
  3485. scsi_device_put(sdev1);
  3486. }
  3487. } else {
  3488. if (sdev1) {
  3489. scsi_remove_device(sdev1);
  3490. scsi_device_put(sdev1);
  3491. }
  3492. }
  3493. }
  3494. }
  3495. }
  3496. doscan = 0;
  3497. break;
  3498. case MR_EVT_LD_OFFLINE:
  3499. case MR_EVT_LD_DELETED:
  3500. megasas_get_ld_list(instance);
  3501. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  3502. for (j = 0;
  3503. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3504. j++) {
  3505. ld_index =
  3506. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3507. sdev1 = scsi_device_lookup(host,
  3508. i + MEGASAS_MAX_LD_CHANNELS,
  3509. j,
  3510. 0);
  3511. if (instance->ld_ids[ld_index] != 0xff) {
  3512. if (sdev1) {
  3513. scsi_device_put(sdev1);
  3514. }
  3515. } else {
  3516. if (sdev1) {
  3517. scsi_remove_device(sdev1);
  3518. scsi_device_put(sdev1);
  3519. }
  3520. }
  3521. }
  3522. }
  3523. doscan = 0;
  3524. break;
  3525. case MR_EVT_LD_CREATED:
  3526. megasas_get_ld_list(instance);
  3527. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  3528. for (j = 0;
  3529. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3530. j++) {
  3531. ld_index =
  3532. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3533. sdev1 = scsi_device_lookup(host,
  3534. i+MEGASAS_MAX_LD_CHANNELS,
  3535. j, 0);
  3536. if (instance->ld_ids[ld_index] !=
  3537. 0xff) {
  3538. if (!sdev1) {
  3539. scsi_add_device(host,
  3540. i + 2,
  3541. j, 0);
  3542. }
  3543. }
  3544. if (sdev1) {
  3545. scsi_device_put(sdev1);
  3546. }
  3547. }
  3548. }
  3549. doscan = 0;
  3550. break;
  3551. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  3552. case MR_EVT_FOREIGN_CFG_IMPORTED:
  3553. doscan = 1;
  3554. break;
  3555. default:
  3556. doscan = 0;
  3557. break;
  3558. }
  3559. } else {
  3560. printk(KERN_ERR "invalid evt_detail!\n");
  3561. kfree(ev);
  3562. return;
  3563. }
  3564. if (doscan) {
  3565. printk(KERN_INFO "scanning ...\n");
  3566. megasas_get_pd_list(instance);
  3567. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  3568. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  3569. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  3570. sdev1 = scsi_device_lookup(host, i, j, 0);
  3571. if (instance->pd_list[pd_index].driveState ==
  3572. MR_PD_STATE_SYSTEM) {
  3573. if (!sdev1) {
  3574. scsi_add_device(host, i, j, 0);
  3575. }
  3576. if (sdev1)
  3577. scsi_device_put(sdev1);
  3578. } else {
  3579. if (sdev1) {
  3580. scsi_remove_device(sdev1);
  3581. scsi_device_put(sdev1);
  3582. }
  3583. }
  3584. }
  3585. }
  3586. megasas_get_ld_list(instance);
  3587. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  3588. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  3589. ld_index =
  3590. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3591. sdev1 = scsi_device_lookup(host,
  3592. i+MEGASAS_MAX_LD_CHANNELS, j, 0);
  3593. if (instance->ld_ids[ld_index] != 0xff) {
  3594. if (!sdev1) {
  3595. scsi_add_device(host,
  3596. i+2,
  3597. j, 0);
  3598. } else {
  3599. scsi_device_put(sdev1);
  3600. }
  3601. } else {
  3602. if (sdev1) {
  3603. scsi_remove_device(sdev1);
  3604. scsi_device_put(sdev1);
  3605. }
  3606. }
  3607. }
  3608. }
  3609. }
  3610. if ( instance->aen_cmd != NULL ) {
  3611. kfree(ev);
  3612. return ;
  3613. }
  3614. seq_num = instance->evt_detail->seq_num + 1;
  3615. /* Register AEN with FW for latest sequence number plus 1 */
  3616. class_locale.members.reserved = 0;
  3617. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3618. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3619. mutex_lock(&instance->aen_mutex);
  3620. error = megasas_register_aen(instance, seq_num,
  3621. class_locale.word);
  3622. mutex_unlock(&instance->aen_mutex);
  3623. if (error)
  3624. printk(KERN_ERR "register aen failed error %x\n", error);
  3625. kfree(ev);
  3626. }
  3627. static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
  3628. megasas_sysfs_show_poll_mode_io,
  3629. megasas_sysfs_set_poll_mode_io);
  3630. /**
  3631. * megasas_init - Driver load entry point
  3632. */
  3633. static int __init megasas_init(void)
  3634. {
  3635. int rval;
  3636. /*
  3637. * Announce driver version and other information
  3638. */
  3639. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  3640. MEGASAS_EXT_VERSION);
  3641. support_poll_for_event = 2;
  3642. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  3643. /*
  3644. * Register character device node
  3645. */
  3646. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  3647. if (rval < 0) {
  3648. printk(KERN_DEBUG "megasas: failed to open device node\n");
  3649. return rval;
  3650. }
  3651. megasas_mgmt_majorno = rval;
  3652. /*
  3653. * Register ourselves as PCI hotplug module
  3654. */
  3655. rval = pci_register_driver(&megasas_pci_driver);
  3656. if (rval) {
  3657. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  3658. goto err_pcidrv;
  3659. }
  3660. rval = driver_create_file(&megasas_pci_driver.driver,
  3661. &driver_attr_version);
  3662. if (rval)
  3663. goto err_dcf_attr_ver;
  3664. rval = driver_create_file(&megasas_pci_driver.driver,
  3665. &driver_attr_release_date);
  3666. if (rval)
  3667. goto err_dcf_rel_date;
  3668. rval = driver_create_file(&megasas_pci_driver.driver,
  3669. &driver_attr_support_poll_for_event);
  3670. if (rval)
  3671. goto err_dcf_support_poll_for_event;
  3672. rval = driver_create_file(&megasas_pci_driver.driver,
  3673. &driver_attr_dbg_lvl);
  3674. if (rval)
  3675. goto err_dcf_dbg_lvl;
  3676. rval = driver_create_file(&megasas_pci_driver.driver,
  3677. &driver_attr_poll_mode_io);
  3678. if (rval)
  3679. goto err_dcf_poll_mode_io;
  3680. return rval;
  3681. err_dcf_poll_mode_io:
  3682. driver_remove_file(&megasas_pci_driver.driver,
  3683. &driver_attr_dbg_lvl);
  3684. err_dcf_dbg_lvl:
  3685. driver_remove_file(&megasas_pci_driver.driver,
  3686. &driver_attr_support_poll_for_event);
  3687. err_dcf_support_poll_for_event:
  3688. driver_remove_file(&megasas_pci_driver.driver,
  3689. &driver_attr_release_date);
  3690. err_dcf_rel_date:
  3691. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  3692. err_dcf_attr_ver:
  3693. pci_unregister_driver(&megasas_pci_driver);
  3694. err_pcidrv:
  3695. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  3696. return rval;
  3697. }
  3698. /**
  3699. * megasas_exit - Driver unload entry point
  3700. */
  3701. static void __exit megasas_exit(void)
  3702. {
  3703. driver_remove_file(&megasas_pci_driver.driver,
  3704. &driver_attr_poll_mode_io);
  3705. driver_remove_file(&megasas_pci_driver.driver,
  3706. &driver_attr_dbg_lvl);
  3707. driver_remove_file(&megasas_pci_driver.driver,
  3708. &driver_attr_release_date);
  3709. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  3710. pci_unregister_driver(&megasas_pci_driver);
  3711. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  3712. }
  3713. module_init(megasas_init);
  3714. module_exit(megasas_exit);