megaraid_sas_base.c 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487
  1. /*
  2. * Linux MegaRAID driver for SAS based RAID controllers
  3. *
  4. * Copyright (c) 2009-2011 LSI Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * FILE: megaraid_sas_base.c
  21. * Version : v00.00.05.38-rc1
  22. *
  23. * Authors: LSI Corporation
  24. * Sreenivas Bagalkote
  25. * Sumant Patro
  26. * Bo Yang
  27. * Adam Radford <linuxraid@lsi.com>
  28. *
  29. * Send feedback to: <megaraidlinux@lsi.com>
  30. *
  31. * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
  32. * ATTN: Linuxraid
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/pci.h>
  37. #include <linux/list.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/module.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/delay.h>
  43. #include <linux/uio.h>
  44. #include <linux/slab.h>
  45. #include <asm/uaccess.h>
  46. #include <linux/fs.h>
  47. #include <linux/compat.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/mutex.h>
  50. #include <linux/poll.h>
  51. #include <scsi/scsi.h>
  52. #include <scsi/scsi_cmnd.h>
  53. #include <scsi/scsi_device.h>
  54. #include <scsi/scsi_host.h>
  55. #include "megaraid_sas_fusion.h"
  56. #include "megaraid_sas.h"
  57. /*
  58. * poll_mode_io:1- schedule complete completion from q cmd
  59. */
  60. static unsigned int poll_mode_io;
  61. module_param_named(poll_mode_io, poll_mode_io, int, 0);
  62. MODULE_PARM_DESC(poll_mode_io,
  63. "Complete cmds from IO path, (default=0)");
  64. /*
  65. * Number of sectors per IO command
  66. * Will be set in megasas_init_mfi if user does not provide
  67. */
  68. static unsigned int max_sectors;
  69. module_param_named(max_sectors, max_sectors, int, 0);
  70. MODULE_PARM_DESC(max_sectors,
  71. "Maximum number of sectors per IO command");
  72. static int msix_disable;
  73. module_param(msix_disable, int, S_IRUGO);
  74. MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  75. MODULE_LICENSE("GPL");
  76. MODULE_VERSION(MEGASAS_VERSION);
  77. MODULE_AUTHOR("megaraidlinux@lsi.com");
  78. MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
  79. int megasas_transition_to_ready(struct megasas_instance *instance);
  80. static int megasas_get_pd_list(struct megasas_instance *instance);
  81. static int megasas_issue_init_mfi(struct megasas_instance *instance);
  82. static int megasas_register_aen(struct megasas_instance *instance,
  83. u32 seq_num, u32 class_locale_word);
  84. /*
  85. * PCI ID table for all supported controllers
  86. */
  87. static struct pci_device_id megasas_pci_table[] = {
  88. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  89. /* xscale IOP */
  90. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  91. /* ppc IOP */
  92. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
  93. /* ppc IOP */
  94. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
  95. /* gen2*/
  96. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
  97. /* gen2*/
  98. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
  99. /* skinny*/
  100. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
  101. /* skinny*/
  102. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  103. /* xscale IOP, vega */
  104. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  105. /* xscale IOP */
  106. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
  107. /* Fusion */
  108. {}
  109. };
  110. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  111. static int megasas_mgmt_majorno;
  112. static struct megasas_mgmt_info megasas_mgmt_info;
  113. static struct fasync_struct *megasas_async_queue;
  114. static DEFINE_MUTEX(megasas_async_queue_mutex);
  115. static int megasas_poll_wait_aen;
  116. static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
  117. static u32 support_poll_for_event;
  118. u32 megasas_dbg_lvl;
  119. static u32 support_device_change;
  120. /* define lock for aen poll */
  121. spinlock_t poll_aen_lock;
  122. void
  123. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  124. u8 alt_status);
  125. static u32
  126. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
  127. static int
  128. megasas_adp_reset_gen2(struct megasas_instance *instance,
  129. struct megasas_register_set __iomem *reg_set);
  130. static irqreturn_t megasas_isr(int irq, void *devp);
  131. static u32
  132. megasas_init_adapter_mfi(struct megasas_instance *instance);
  133. u32
  134. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  135. struct scsi_cmnd *scmd);
  136. static void megasas_complete_cmd_dpc(unsigned long instance_addr);
  137. void
  138. megasas_release_fusion(struct megasas_instance *instance);
  139. int
  140. megasas_ioc_init_fusion(struct megasas_instance *instance);
  141. void
  142. megasas_free_cmds_fusion(struct megasas_instance *instance);
  143. u8
  144. megasas_get_map_info(struct megasas_instance *instance);
  145. int
  146. megasas_sync_map_info(struct megasas_instance *instance);
  147. int
  148. wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
  149. void megasas_reset_reply_desc(struct megasas_instance *instance);
  150. u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
  151. struct LD_LOAD_BALANCE_INFO *lbInfo);
  152. int megasas_reset_fusion(struct Scsi_Host *shost);
  153. void megasas_fusion_ocr_wq(struct work_struct *work);
  154. void
  155. megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  156. {
  157. instance->instancet->fire_cmd(instance,
  158. cmd->frame_phys_addr, 0, instance->reg_set);
  159. }
  160. /**
  161. * megasas_get_cmd - Get a command from the free pool
  162. * @instance: Adapter soft state
  163. *
  164. * Returns a free command from the pool
  165. */
  166. struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  167. *instance)
  168. {
  169. unsigned long flags;
  170. struct megasas_cmd *cmd = NULL;
  171. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  172. if (!list_empty(&instance->cmd_pool)) {
  173. cmd = list_entry((&instance->cmd_pool)->next,
  174. struct megasas_cmd, list);
  175. list_del_init(&cmd->list);
  176. } else {
  177. printk(KERN_ERR "megasas: Command pool empty!\n");
  178. }
  179. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  180. return cmd;
  181. }
  182. /**
  183. * megasas_return_cmd - Return a cmd to free command pool
  184. * @instance: Adapter soft state
  185. * @cmd: Command packet to be returned to free command pool
  186. */
  187. inline void
  188. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  189. {
  190. unsigned long flags;
  191. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  192. cmd->scmd = NULL;
  193. cmd->frame_count = 0;
  194. list_add_tail(&cmd->list, &instance->cmd_pool);
  195. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  196. }
  197. /**
  198. * The following functions are defined for xscale
  199. * (deviceid : 1064R, PERC5) controllers
  200. */
  201. /**
  202. * megasas_enable_intr_xscale - Enables interrupts
  203. * @regs: MFI register set
  204. */
  205. static inline void
  206. megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
  207. {
  208. writel(0, &(regs)->outbound_intr_mask);
  209. /* Dummy readl to force pci flush */
  210. readl(&regs->outbound_intr_mask);
  211. }
  212. /**
  213. * megasas_disable_intr_xscale -Disables interrupt
  214. * @regs: MFI register set
  215. */
  216. static inline void
  217. megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
  218. {
  219. u32 mask = 0x1f;
  220. writel(mask, &regs->outbound_intr_mask);
  221. /* Dummy readl to force pci flush */
  222. readl(&regs->outbound_intr_mask);
  223. }
  224. /**
  225. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  226. * @regs: MFI register set
  227. */
  228. static u32
  229. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  230. {
  231. return readl(&(regs)->outbound_msg_0);
  232. }
  233. /**
  234. * megasas_clear_interrupt_xscale - Check & clear interrupt
  235. * @regs: MFI register set
  236. */
  237. static int
  238. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  239. {
  240. u32 status;
  241. u32 mfiStatus = 0;
  242. /*
  243. * Check if it is our interrupt
  244. */
  245. status = readl(&regs->outbound_intr_status);
  246. if (status & MFI_OB_INTR_STATUS_MASK)
  247. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  248. if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
  249. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  250. /*
  251. * Clear the interrupt by writing back the same value
  252. */
  253. if (mfiStatus)
  254. writel(status, &regs->outbound_intr_status);
  255. /* Dummy readl to force pci flush */
  256. readl(&regs->outbound_intr_status);
  257. return mfiStatus;
  258. }
  259. /**
  260. * megasas_fire_cmd_xscale - Sends command to the FW
  261. * @frame_phys_addr : Physical address of cmd
  262. * @frame_count : Number of frames for the command
  263. * @regs : MFI register set
  264. */
  265. static inline void
  266. megasas_fire_cmd_xscale(struct megasas_instance *instance,
  267. dma_addr_t frame_phys_addr,
  268. u32 frame_count,
  269. struct megasas_register_set __iomem *regs)
  270. {
  271. unsigned long flags;
  272. spin_lock_irqsave(&instance->hba_lock, flags);
  273. writel((frame_phys_addr >> 3)|(frame_count),
  274. &(regs)->inbound_queue_port);
  275. spin_unlock_irqrestore(&instance->hba_lock, flags);
  276. }
  277. /**
  278. * megasas_adp_reset_xscale - For controller reset
  279. * @regs: MFI register set
  280. */
  281. static int
  282. megasas_adp_reset_xscale(struct megasas_instance *instance,
  283. struct megasas_register_set __iomem *regs)
  284. {
  285. u32 i;
  286. u32 pcidata;
  287. writel(MFI_ADP_RESET, &regs->inbound_doorbell);
  288. for (i = 0; i < 3; i++)
  289. msleep(1000); /* sleep for 3 secs */
  290. pcidata = 0;
  291. pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
  292. printk(KERN_NOTICE "pcidata = %x\n", pcidata);
  293. if (pcidata & 0x2) {
  294. printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
  295. pcidata &= ~0x2;
  296. pci_write_config_dword(instance->pdev,
  297. MFI_1068_PCSR_OFFSET, pcidata);
  298. for (i = 0; i < 2; i++)
  299. msleep(1000); /* need to wait 2 secs again */
  300. pcidata = 0;
  301. pci_read_config_dword(instance->pdev,
  302. MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
  303. printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
  304. if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
  305. printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
  306. pcidata = 0;
  307. pci_write_config_dword(instance->pdev,
  308. MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
  309. }
  310. }
  311. return 0;
  312. }
  313. /**
  314. * megasas_check_reset_xscale - For controller reset check
  315. * @regs: MFI register set
  316. */
  317. static int
  318. megasas_check_reset_xscale(struct megasas_instance *instance,
  319. struct megasas_register_set __iomem *regs)
  320. {
  321. u32 consumer;
  322. consumer = *instance->consumer;
  323. if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
  324. (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. static struct megasas_instance_template megasas_instance_template_xscale = {
  330. .fire_cmd = megasas_fire_cmd_xscale,
  331. .enable_intr = megasas_enable_intr_xscale,
  332. .disable_intr = megasas_disable_intr_xscale,
  333. .clear_intr = megasas_clear_intr_xscale,
  334. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  335. .adp_reset = megasas_adp_reset_xscale,
  336. .check_reset = megasas_check_reset_xscale,
  337. .service_isr = megasas_isr,
  338. .tasklet = megasas_complete_cmd_dpc,
  339. .init_adapter = megasas_init_adapter_mfi,
  340. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  341. .issue_dcmd = megasas_issue_dcmd,
  342. };
  343. /**
  344. * This is the end of set of functions & definitions specific
  345. * to xscale (deviceid : 1064R, PERC5) controllers
  346. */
  347. /**
  348. * The following functions are defined for ppc (deviceid : 0x60)
  349. * controllers
  350. */
  351. /**
  352. * megasas_enable_intr_ppc - Enables interrupts
  353. * @regs: MFI register set
  354. */
  355. static inline void
  356. megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
  357. {
  358. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  359. writel(~0x80000000, &(regs)->outbound_intr_mask);
  360. /* Dummy readl to force pci flush */
  361. readl(&regs->outbound_intr_mask);
  362. }
  363. /**
  364. * megasas_disable_intr_ppc - Disable interrupt
  365. * @regs: MFI register set
  366. */
  367. static inline void
  368. megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
  369. {
  370. u32 mask = 0xFFFFFFFF;
  371. writel(mask, &regs->outbound_intr_mask);
  372. /* Dummy readl to force pci flush */
  373. readl(&regs->outbound_intr_mask);
  374. }
  375. /**
  376. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  377. * @regs: MFI register set
  378. */
  379. static u32
  380. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  381. {
  382. return readl(&(regs)->outbound_scratch_pad);
  383. }
  384. /**
  385. * megasas_clear_interrupt_ppc - Check & clear interrupt
  386. * @regs: MFI register set
  387. */
  388. static int
  389. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  390. {
  391. u32 status, mfiStatus = 0;
  392. /*
  393. * Check if it is our interrupt
  394. */
  395. status = readl(&regs->outbound_intr_status);
  396. if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
  397. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  398. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
  399. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  400. /*
  401. * Clear the interrupt by writing back the same value
  402. */
  403. writel(status, &regs->outbound_doorbell_clear);
  404. /* Dummy readl to force pci flush */
  405. readl(&regs->outbound_doorbell_clear);
  406. return mfiStatus;
  407. }
  408. /**
  409. * megasas_fire_cmd_ppc - Sends command to the FW
  410. * @frame_phys_addr : Physical address of cmd
  411. * @frame_count : Number of frames for the command
  412. * @regs : MFI register set
  413. */
  414. static inline void
  415. megasas_fire_cmd_ppc(struct megasas_instance *instance,
  416. dma_addr_t frame_phys_addr,
  417. u32 frame_count,
  418. struct megasas_register_set __iomem *regs)
  419. {
  420. unsigned long flags;
  421. spin_lock_irqsave(&instance->hba_lock, flags);
  422. writel((frame_phys_addr | (frame_count<<1))|1,
  423. &(regs)->inbound_queue_port);
  424. spin_unlock_irqrestore(&instance->hba_lock, flags);
  425. }
  426. /**
  427. * megasas_check_reset_ppc - For controller reset check
  428. * @regs: MFI register set
  429. */
  430. static int
  431. megasas_check_reset_ppc(struct megasas_instance *instance,
  432. struct megasas_register_set __iomem *regs)
  433. {
  434. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
  435. return 1;
  436. return 0;
  437. }
  438. static struct megasas_instance_template megasas_instance_template_ppc = {
  439. .fire_cmd = megasas_fire_cmd_ppc,
  440. .enable_intr = megasas_enable_intr_ppc,
  441. .disable_intr = megasas_disable_intr_ppc,
  442. .clear_intr = megasas_clear_intr_ppc,
  443. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  444. .adp_reset = megasas_adp_reset_xscale,
  445. .check_reset = megasas_check_reset_ppc,
  446. .service_isr = megasas_isr,
  447. .tasklet = megasas_complete_cmd_dpc,
  448. .init_adapter = megasas_init_adapter_mfi,
  449. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  450. .issue_dcmd = megasas_issue_dcmd,
  451. };
  452. /**
  453. * megasas_enable_intr_skinny - Enables interrupts
  454. * @regs: MFI register set
  455. */
  456. static inline void
  457. megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
  458. {
  459. writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
  460. writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  461. /* Dummy readl to force pci flush */
  462. readl(&regs->outbound_intr_mask);
  463. }
  464. /**
  465. * megasas_disable_intr_skinny - Disables interrupt
  466. * @regs: MFI register set
  467. */
  468. static inline void
  469. megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
  470. {
  471. u32 mask = 0xFFFFFFFF;
  472. writel(mask, &regs->outbound_intr_mask);
  473. /* Dummy readl to force pci flush */
  474. readl(&regs->outbound_intr_mask);
  475. }
  476. /**
  477. * megasas_read_fw_status_reg_skinny - returns the current FW status value
  478. * @regs: MFI register set
  479. */
  480. static u32
  481. megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
  482. {
  483. return readl(&(regs)->outbound_scratch_pad);
  484. }
  485. /**
  486. * megasas_clear_interrupt_skinny - Check & clear interrupt
  487. * @regs: MFI register set
  488. */
  489. static int
  490. megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
  491. {
  492. u32 status;
  493. u32 mfiStatus = 0;
  494. /*
  495. * Check if it is our interrupt
  496. */
  497. status = readl(&regs->outbound_intr_status);
  498. if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
  499. return 0;
  500. }
  501. /*
  502. * Check if it is our interrupt
  503. */
  504. if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
  505. MFI_STATE_FAULT) {
  506. mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  507. } else
  508. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  509. /*
  510. * Clear the interrupt by writing back the same value
  511. */
  512. writel(status, &regs->outbound_intr_status);
  513. /*
  514. * dummy read to flush PCI
  515. */
  516. readl(&regs->outbound_intr_status);
  517. return mfiStatus;
  518. }
  519. /**
  520. * megasas_fire_cmd_skinny - Sends command to the FW
  521. * @frame_phys_addr : Physical address of cmd
  522. * @frame_count : Number of frames for the command
  523. * @regs : MFI register set
  524. */
  525. static inline void
  526. megasas_fire_cmd_skinny(struct megasas_instance *instance,
  527. dma_addr_t frame_phys_addr,
  528. u32 frame_count,
  529. struct megasas_register_set __iomem *regs)
  530. {
  531. unsigned long flags;
  532. spin_lock_irqsave(&instance->hba_lock, flags);
  533. writel(0, &(regs)->inbound_high_queue_port);
  534. writel((frame_phys_addr | (frame_count<<1))|1,
  535. &(regs)->inbound_low_queue_port);
  536. spin_unlock_irqrestore(&instance->hba_lock, flags);
  537. }
  538. /**
  539. * megasas_check_reset_skinny - For controller reset check
  540. * @regs: MFI register set
  541. */
  542. static int
  543. megasas_check_reset_skinny(struct megasas_instance *instance,
  544. struct megasas_register_set __iomem *regs)
  545. {
  546. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
  547. return 1;
  548. return 0;
  549. }
  550. static struct megasas_instance_template megasas_instance_template_skinny = {
  551. .fire_cmd = megasas_fire_cmd_skinny,
  552. .enable_intr = megasas_enable_intr_skinny,
  553. .disable_intr = megasas_disable_intr_skinny,
  554. .clear_intr = megasas_clear_intr_skinny,
  555. .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
  556. .adp_reset = megasas_adp_reset_gen2,
  557. .check_reset = megasas_check_reset_skinny,
  558. .service_isr = megasas_isr,
  559. .tasklet = megasas_complete_cmd_dpc,
  560. .init_adapter = megasas_init_adapter_mfi,
  561. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  562. .issue_dcmd = megasas_issue_dcmd,
  563. };
  564. /**
  565. * The following functions are defined for gen2 (deviceid : 0x78 0x79)
  566. * controllers
  567. */
  568. /**
  569. * megasas_enable_intr_gen2 - Enables interrupts
  570. * @regs: MFI register set
  571. */
  572. static inline void
  573. megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
  574. {
  575. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  576. /* write ~0x00000005 (4 & 1) to the intr mask*/
  577. writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  578. /* Dummy readl to force pci flush */
  579. readl(&regs->outbound_intr_mask);
  580. }
  581. /**
  582. * megasas_disable_intr_gen2 - Disables interrupt
  583. * @regs: MFI register set
  584. */
  585. static inline void
  586. megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
  587. {
  588. u32 mask = 0xFFFFFFFF;
  589. writel(mask, &regs->outbound_intr_mask);
  590. /* Dummy readl to force pci flush */
  591. readl(&regs->outbound_intr_mask);
  592. }
  593. /**
  594. * megasas_read_fw_status_reg_gen2 - returns the current FW status value
  595. * @regs: MFI register set
  596. */
  597. static u32
  598. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
  599. {
  600. return readl(&(regs)->outbound_scratch_pad);
  601. }
  602. /**
  603. * megasas_clear_interrupt_gen2 - Check & clear interrupt
  604. * @regs: MFI register set
  605. */
  606. static int
  607. megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
  608. {
  609. u32 status;
  610. u32 mfiStatus = 0;
  611. /*
  612. * Check if it is our interrupt
  613. */
  614. status = readl(&regs->outbound_intr_status);
  615. if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
  616. mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
  617. }
  618. if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
  619. mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
  620. }
  621. /*
  622. * Clear the interrupt by writing back the same value
  623. */
  624. if (mfiStatus)
  625. writel(status, &regs->outbound_doorbell_clear);
  626. /* Dummy readl to force pci flush */
  627. readl(&regs->outbound_intr_status);
  628. return mfiStatus;
  629. }
  630. /**
  631. * megasas_fire_cmd_gen2 - Sends command to the FW
  632. * @frame_phys_addr : Physical address of cmd
  633. * @frame_count : Number of frames for the command
  634. * @regs : MFI register set
  635. */
  636. static inline void
  637. megasas_fire_cmd_gen2(struct megasas_instance *instance,
  638. dma_addr_t frame_phys_addr,
  639. u32 frame_count,
  640. struct megasas_register_set __iomem *regs)
  641. {
  642. unsigned long flags;
  643. spin_lock_irqsave(&instance->hba_lock, flags);
  644. writel((frame_phys_addr | (frame_count<<1))|1,
  645. &(regs)->inbound_queue_port);
  646. spin_unlock_irqrestore(&instance->hba_lock, flags);
  647. }
  648. /**
  649. * megasas_adp_reset_gen2 - For controller reset
  650. * @regs: MFI register set
  651. */
  652. static int
  653. megasas_adp_reset_gen2(struct megasas_instance *instance,
  654. struct megasas_register_set __iomem *reg_set)
  655. {
  656. u32 retry = 0 ;
  657. u32 HostDiag;
  658. u32 *seq_offset = &reg_set->seq_offset;
  659. u32 *hostdiag_offset = &reg_set->host_diag;
  660. if (instance->instancet == &megasas_instance_template_skinny) {
  661. seq_offset = &reg_set->fusion_seq_offset;
  662. hostdiag_offset = &reg_set->fusion_host_diag;
  663. }
  664. writel(0, seq_offset);
  665. writel(4, seq_offset);
  666. writel(0xb, seq_offset);
  667. writel(2, seq_offset);
  668. writel(7, seq_offset);
  669. writel(0xd, seq_offset);
  670. msleep(1000);
  671. HostDiag = (u32)readl(hostdiag_offset);
  672. while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
  673. msleep(100);
  674. HostDiag = (u32)readl(hostdiag_offset);
  675. printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
  676. retry, HostDiag);
  677. if (retry++ >= 100)
  678. return 1;
  679. }
  680. printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
  681. writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
  682. ssleep(10);
  683. HostDiag = (u32)readl(hostdiag_offset);
  684. while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
  685. msleep(100);
  686. HostDiag = (u32)readl(hostdiag_offset);
  687. printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
  688. retry, HostDiag);
  689. if (retry++ >= 1000)
  690. return 1;
  691. }
  692. return 0;
  693. }
  694. /**
  695. * megasas_check_reset_gen2 - For controller reset check
  696. * @regs: MFI register set
  697. */
  698. static int
  699. megasas_check_reset_gen2(struct megasas_instance *instance,
  700. struct megasas_register_set __iomem *regs)
  701. {
  702. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  703. return 1;
  704. }
  705. return 0;
  706. }
  707. static struct megasas_instance_template megasas_instance_template_gen2 = {
  708. .fire_cmd = megasas_fire_cmd_gen2,
  709. .enable_intr = megasas_enable_intr_gen2,
  710. .disable_intr = megasas_disable_intr_gen2,
  711. .clear_intr = megasas_clear_intr_gen2,
  712. .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
  713. .adp_reset = megasas_adp_reset_gen2,
  714. .check_reset = megasas_check_reset_gen2,
  715. .service_isr = megasas_isr,
  716. .tasklet = megasas_complete_cmd_dpc,
  717. .init_adapter = megasas_init_adapter_mfi,
  718. .build_and_issue_cmd = megasas_build_and_issue_cmd,
  719. .issue_dcmd = megasas_issue_dcmd,
  720. };
  721. /**
  722. * This is the end of set of functions & definitions
  723. * specific to gen2 (deviceid : 0x78, 0x79) controllers
  724. */
  725. /*
  726. * Template added for TB (Fusion)
  727. */
  728. extern struct megasas_instance_template megasas_instance_template_fusion;
  729. /**
  730. * megasas_issue_polled - Issues a polling command
  731. * @instance: Adapter soft state
  732. * @cmd: Command packet to be issued
  733. *
  734. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  735. */
  736. int
  737. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  738. {
  739. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  740. frame_hdr->cmd_status = 0xFF;
  741. frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  742. /*
  743. * Issue the frame using inbound queue port
  744. */
  745. instance->instancet->issue_dcmd(instance, cmd);
  746. /*
  747. * Wait for cmd_status to change
  748. */
  749. return wait_and_poll(instance, cmd);
  750. }
  751. /**
  752. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  753. * @instance: Adapter soft state
  754. * @cmd: Command to be issued
  755. *
  756. * This function waits on an event for the command to be returned from ISR.
  757. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  758. * Used to issue ioctl commands.
  759. */
  760. static int
  761. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  762. struct megasas_cmd *cmd)
  763. {
  764. cmd->cmd_status = ENODATA;
  765. instance->instancet->issue_dcmd(instance, cmd);
  766. wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
  767. return 0;
  768. }
  769. /**
  770. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  771. * @instance: Adapter soft state
  772. * @cmd_to_abort: Previously issued cmd to be aborted
  773. *
  774. * MFI firmware can abort previously issued AEN command (automatic event
  775. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  776. * cmd and waits for return status.
  777. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  778. */
  779. static int
  780. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  781. struct megasas_cmd *cmd_to_abort)
  782. {
  783. struct megasas_cmd *cmd;
  784. struct megasas_abort_frame *abort_fr;
  785. cmd = megasas_get_cmd(instance);
  786. if (!cmd)
  787. return -1;
  788. abort_fr = &cmd->frame->abort;
  789. /*
  790. * Prepare and issue the abort frame
  791. */
  792. abort_fr->cmd = MFI_CMD_ABORT;
  793. abort_fr->cmd_status = 0xFF;
  794. abort_fr->flags = 0;
  795. abort_fr->abort_context = cmd_to_abort->index;
  796. abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
  797. abort_fr->abort_mfi_phys_addr_hi = 0;
  798. cmd->sync_cmd = 1;
  799. cmd->cmd_status = 0xFF;
  800. instance->instancet->issue_dcmd(instance, cmd);
  801. /*
  802. * Wait for this cmd to complete
  803. */
  804. wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
  805. cmd->sync_cmd = 0;
  806. megasas_return_cmd(instance, cmd);
  807. return 0;
  808. }
  809. /**
  810. * megasas_make_sgl32 - Prepares 32-bit SGL
  811. * @instance: Adapter soft state
  812. * @scp: SCSI command from the mid-layer
  813. * @mfi_sgl: SGL to be filled in
  814. *
  815. * If successful, this function returns the number of SG elements. Otherwise,
  816. * it returnes -1.
  817. */
  818. static int
  819. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  820. union megasas_sgl *mfi_sgl)
  821. {
  822. int i;
  823. int sge_count;
  824. struct scatterlist *os_sgl;
  825. sge_count = scsi_dma_map(scp);
  826. BUG_ON(sge_count < 0);
  827. if (sge_count) {
  828. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  829. mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
  830. mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
  831. }
  832. }
  833. return sge_count;
  834. }
  835. /**
  836. * megasas_make_sgl64 - Prepares 64-bit SGL
  837. * @instance: Adapter soft state
  838. * @scp: SCSI command from the mid-layer
  839. * @mfi_sgl: SGL to be filled in
  840. *
  841. * If successful, this function returns the number of SG elements. Otherwise,
  842. * it returnes -1.
  843. */
  844. static int
  845. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  846. union megasas_sgl *mfi_sgl)
  847. {
  848. int i;
  849. int sge_count;
  850. struct scatterlist *os_sgl;
  851. sge_count = scsi_dma_map(scp);
  852. BUG_ON(sge_count < 0);
  853. if (sge_count) {
  854. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  855. mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
  856. mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
  857. }
  858. }
  859. return sge_count;
  860. }
  861. /**
  862. * megasas_make_sgl_skinny - Prepares IEEE SGL
  863. * @instance: Adapter soft state
  864. * @scp: SCSI command from the mid-layer
  865. * @mfi_sgl: SGL to be filled in
  866. *
  867. * If successful, this function returns the number of SG elements. Otherwise,
  868. * it returnes -1.
  869. */
  870. static int
  871. megasas_make_sgl_skinny(struct megasas_instance *instance,
  872. struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
  873. {
  874. int i;
  875. int sge_count;
  876. struct scatterlist *os_sgl;
  877. sge_count = scsi_dma_map(scp);
  878. if (sge_count) {
  879. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  880. mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
  881. mfi_sgl->sge_skinny[i].phys_addr =
  882. sg_dma_address(os_sgl);
  883. mfi_sgl->sge_skinny[i].flag = 0;
  884. }
  885. }
  886. return sge_count;
  887. }
  888. /**
  889. * megasas_get_frame_count - Computes the number of frames
  890. * @frame_type : type of frame- io or pthru frame
  891. * @sge_count : number of sg elements
  892. *
  893. * Returns the number of frames required for numnber of sge's (sge_count)
  894. */
  895. static u32 megasas_get_frame_count(struct megasas_instance *instance,
  896. u8 sge_count, u8 frame_type)
  897. {
  898. int num_cnt;
  899. int sge_bytes;
  900. u32 sge_sz;
  901. u32 frame_count=0;
  902. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  903. sizeof(struct megasas_sge32);
  904. if (instance->flag_ieee) {
  905. sge_sz = sizeof(struct megasas_sge_skinny);
  906. }
  907. /*
  908. * Main frame can contain 2 SGEs for 64-bit SGLs and
  909. * 3 SGEs for 32-bit SGLs for ldio &
  910. * 1 SGEs for 64-bit SGLs and
  911. * 2 SGEs for 32-bit SGLs for pthru frame
  912. */
  913. if (unlikely(frame_type == PTHRU_FRAME)) {
  914. if (instance->flag_ieee == 1) {
  915. num_cnt = sge_count - 1;
  916. } else if (IS_DMA64)
  917. num_cnt = sge_count - 1;
  918. else
  919. num_cnt = sge_count - 2;
  920. } else {
  921. if (instance->flag_ieee == 1) {
  922. num_cnt = sge_count - 1;
  923. } else if (IS_DMA64)
  924. num_cnt = sge_count - 2;
  925. else
  926. num_cnt = sge_count - 3;
  927. }
  928. if(num_cnt>0){
  929. sge_bytes = sge_sz * num_cnt;
  930. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  931. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  932. }
  933. /* Main frame */
  934. frame_count +=1;
  935. if (frame_count > 7)
  936. frame_count = 8;
  937. return frame_count;
  938. }
  939. /**
  940. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  941. * @instance: Adapter soft state
  942. * @scp: SCSI command
  943. * @cmd: Command to be prepared in
  944. *
  945. * This function prepares CDB commands. These are typcially pass-through
  946. * commands to the devices.
  947. */
  948. static int
  949. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  950. struct megasas_cmd *cmd)
  951. {
  952. u32 is_logical;
  953. u32 device_id;
  954. u16 flags = 0;
  955. struct megasas_pthru_frame *pthru;
  956. is_logical = MEGASAS_IS_LOGICAL(scp);
  957. device_id = MEGASAS_DEV_INDEX(instance, scp);
  958. pthru = (struct megasas_pthru_frame *)cmd->frame;
  959. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  960. flags = MFI_FRAME_DIR_WRITE;
  961. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  962. flags = MFI_FRAME_DIR_READ;
  963. else if (scp->sc_data_direction == PCI_DMA_NONE)
  964. flags = MFI_FRAME_DIR_NONE;
  965. if (instance->flag_ieee == 1) {
  966. flags |= MFI_FRAME_IEEE;
  967. }
  968. /*
  969. * Prepare the DCDB frame
  970. */
  971. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  972. pthru->cmd_status = 0x0;
  973. pthru->scsi_status = 0x0;
  974. pthru->target_id = device_id;
  975. pthru->lun = scp->device->lun;
  976. pthru->cdb_len = scp->cmd_len;
  977. pthru->timeout = 0;
  978. pthru->pad_0 = 0;
  979. pthru->flags = flags;
  980. pthru->data_xfer_len = scsi_bufflen(scp);
  981. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  982. /*
  983. * If the command is for the tape device, set the
  984. * pthru timeout to the os layer timeout value.
  985. */
  986. if (scp->device->type == TYPE_TAPE) {
  987. if ((scp->request->timeout / HZ) > 0xFFFF)
  988. pthru->timeout = 0xFFFF;
  989. else
  990. pthru->timeout = scp->request->timeout / HZ;
  991. }
  992. /*
  993. * Construct SGL
  994. */
  995. if (instance->flag_ieee == 1) {
  996. pthru->flags |= MFI_FRAME_SGL64;
  997. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  998. &pthru->sgl);
  999. } else if (IS_DMA64) {
  1000. pthru->flags |= MFI_FRAME_SGL64;
  1001. pthru->sge_count = megasas_make_sgl64(instance, scp,
  1002. &pthru->sgl);
  1003. } else
  1004. pthru->sge_count = megasas_make_sgl32(instance, scp,
  1005. &pthru->sgl);
  1006. if (pthru->sge_count > instance->max_num_sge) {
  1007. printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
  1008. pthru->sge_count);
  1009. return 0;
  1010. }
  1011. /*
  1012. * Sense info specific
  1013. */
  1014. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  1015. pthru->sense_buf_phys_addr_hi = 0;
  1016. pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  1017. /*
  1018. * Compute the total number of frames this command consumes. FW uses
  1019. * this number to pull sufficient number of frames from host memory.
  1020. */
  1021. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  1022. PTHRU_FRAME);
  1023. return cmd->frame_count;
  1024. }
  1025. /**
  1026. * megasas_build_ldio - Prepares IOs to logical devices
  1027. * @instance: Adapter soft state
  1028. * @scp: SCSI command
  1029. * @cmd: Command to be prepared
  1030. *
  1031. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  1032. */
  1033. static int
  1034. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  1035. struct megasas_cmd *cmd)
  1036. {
  1037. u32 device_id;
  1038. u8 sc = scp->cmnd[0];
  1039. u16 flags = 0;
  1040. struct megasas_io_frame *ldio;
  1041. device_id = MEGASAS_DEV_INDEX(instance, scp);
  1042. ldio = (struct megasas_io_frame *)cmd->frame;
  1043. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  1044. flags = MFI_FRAME_DIR_WRITE;
  1045. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  1046. flags = MFI_FRAME_DIR_READ;
  1047. if (instance->flag_ieee == 1) {
  1048. flags |= MFI_FRAME_IEEE;
  1049. }
  1050. /*
  1051. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  1052. */
  1053. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  1054. ldio->cmd_status = 0x0;
  1055. ldio->scsi_status = 0x0;
  1056. ldio->target_id = device_id;
  1057. ldio->timeout = 0;
  1058. ldio->reserved_0 = 0;
  1059. ldio->pad_0 = 0;
  1060. ldio->flags = flags;
  1061. ldio->start_lba_hi = 0;
  1062. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  1063. /*
  1064. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1065. */
  1066. if (scp->cmd_len == 6) {
  1067. ldio->lba_count = (u32) scp->cmnd[4];
  1068. ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  1069. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  1070. ldio->start_lba_lo &= 0x1FFFFF;
  1071. }
  1072. /*
  1073. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1074. */
  1075. else if (scp->cmd_len == 10) {
  1076. ldio->lba_count = (u32) scp->cmnd[8] |
  1077. ((u32) scp->cmnd[7] << 8);
  1078. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1079. ((u32) scp->cmnd[3] << 16) |
  1080. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1081. }
  1082. /*
  1083. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1084. */
  1085. else if (scp->cmd_len == 12) {
  1086. ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
  1087. ((u32) scp->cmnd[7] << 16) |
  1088. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1089. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  1090. ((u32) scp->cmnd[3] << 16) |
  1091. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1092. }
  1093. /*
  1094. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  1095. */
  1096. else if (scp->cmd_len == 16) {
  1097. ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
  1098. ((u32) scp->cmnd[11] << 16) |
  1099. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  1100. ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  1101. ((u32) scp->cmnd[7] << 16) |
  1102. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  1103. ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  1104. ((u32) scp->cmnd[3] << 16) |
  1105. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  1106. }
  1107. /*
  1108. * Construct SGL
  1109. */
  1110. if (instance->flag_ieee) {
  1111. ldio->flags |= MFI_FRAME_SGL64;
  1112. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  1113. &ldio->sgl);
  1114. } else if (IS_DMA64) {
  1115. ldio->flags |= MFI_FRAME_SGL64;
  1116. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  1117. } else
  1118. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  1119. if (ldio->sge_count > instance->max_num_sge) {
  1120. printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
  1121. ldio->sge_count);
  1122. return 0;
  1123. }
  1124. /*
  1125. * Sense info specific
  1126. */
  1127. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  1128. ldio->sense_buf_phys_addr_hi = 0;
  1129. ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  1130. /*
  1131. * Compute the total number of frames this command consumes. FW uses
  1132. * this number to pull sufficient number of frames from host memory.
  1133. */
  1134. cmd->frame_count = megasas_get_frame_count(instance,
  1135. ldio->sge_count, IO_FRAME);
  1136. return cmd->frame_count;
  1137. }
  1138. /**
  1139. * megasas_is_ldio - Checks if the cmd is for logical drive
  1140. * @scmd: SCSI command
  1141. *
  1142. * Called by megasas_queue_command to find out if the command to be queued
  1143. * is a logical drive command
  1144. */
  1145. inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  1146. {
  1147. if (!MEGASAS_IS_LOGICAL(cmd))
  1148. return 0;
  1149. switch (cmd->cmnd[0]) {
  1150. case READ_10:
  1151. case WRITE_10:
  1152. case READ_12:
  1153. case WRITE_12:
  1154. case READ_6:
  1155. case WRITE_6:
  1156. case READ_16:
  1157. case WRITE_16:
  1158. return 1;
  1159. default:
  1160. return 0;
  1161. }
  1162. }
  1163. /**
  1164. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  1165. * in FW
  1166. * @instance: Adapter soft state
  1167. */
  1168. static inline void
  1169. megasas_dump_pending_frames(struct megasas_instance *instance)
  1170. {
  1171. struct megasas_cmd *cmd;
  1172. int i,n;
  1173. union megasas_sgl *mfi_sgl;
  1174. struct megasas_io_frame *ldio;
  1175. struct megasas_pthru_frame *pthru;
  1176. u32 sgcount;
  1177. u32 max_cmd = instance->max_fw_cmds;
  1178. printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  1179. printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  1180. if (IS_DMA64)
  1181. printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  1182. else
  1183. printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  1184. printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  1185. for (i = 0; i < max_cmd; i++) {
  1186. cmd = instance->cmd_list[i];
  1187. if(!cmd->scmd)
  1188. continue;
  1189. printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  1190. if (megasas_is_ldio(cmd->scmd)){
  1191. ldio = (struct megasas_io_frame *)cmd->frame;
  1192. mfi_sgl = &ldio->sgl;
  1193. sgcount = ldio->sge_count;
  1194. 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);
  1195. }
  1196. else {
  1197. pthru = (struct megasas_pthru_frame *) cmd->frame;
  1198. mfi_sgl = &pthru->sgl;
  1199. sgcount = pthru->sge_count;
  1200. 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);
  1201. }
  1202. if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
  1203. for (n = 0; n < sgcount; n++){
  1204. if (IS_DMA64)
  1205. 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) ;
  1206. else
  1207. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
  1208. }
  1209. }
  1210. printk(KERN_ERR "\n");
  1211. } /*for max_cmd*/
  1212. printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  1213. for (i = 0; i < max_cmd; i++) {
  1214. cmd = instance->cmd_list[i];
  1215. if(cmd->sync_cmd == 1){
  1216. printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  1217. }
  1218. }
  1219. printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
  1220. }
  1221. u32
  1222. megasas_build_and_issue_cmd(struct megasas_instance *instance,
  1223. struct scsi_cmnd *scmd)
  1224. {
  1225. struct megasas_cmd *cmd;
  1226. u32 frame_count;
  1227. cmd = megasas_get_cmd(instance);
  1228. if (!cmd)
  1229. return SCSI_MLQUEUE_HOST_BUSY;
  1230. /*
  1231. * Logical drive command
  1232. */
  1233. if (megasas_is_ldio(scmd))
  1234. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1235. else
  1236. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1237. if (!frame_count)
  1238. goto out_return_cmd;
  1239. cmd->scmd = scmd;
  1240. scmd->SCp.ptr = (char *)cmd;
  1241. /*
  1242. * Issue the command to the FW
  1243. */
  1244. atomic_inc(&instance->fw_outstanding);
  1245. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1246. cmd->frame_count-1, instance->reg_set);
  1247. /*
  1248. * Check if we have pend cmds to be completed
  1249. */
  1250. if (poll_mode_io && atomic_read(&instance->fw_outstanding))
  1251. tasklet_schedule(&instance->isr_tasklet);
  1252. return 0;
  1253. out_return_cmd:
  1254. megasas_return_cmd(instance, cmd);
  1255. return 1;
  1256. }
  1257. /**
  1258. * megasas_queue_command - Queue entry point
  1259. * @scmd: SCSI command to be queued
  1260. * @done: Callback entry point
  1261. */
  1262. static int
  1263. megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  1264. {
  1265. struct megasas_instance *instance;
  1266. unsigned long flags;
  1267. instance = (struct megasas_instance *)
  1268. scmd->device->host->hostdata;
  1269. if (instance->issuepend_done == 0)
  1270. return SCSI_MLQUEUE_HOST_BUSY;
  1271. spin_lock_irqsave(&instance->hba_lock, flags);
  1272. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1273. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1274. return SCSI_MLQUEUE_HOST_BUSY;
  1275. }
  1276. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1277. scmd->scsi_done = done;
  1278. scmd->result = 0;
  1279. if (MEGASAS_IS_LOGICAL(scmd) &&
  1280. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  1281. scmd->result = DID_BAD_TARGET << 16;
  1282. goto out_done;
  1283. }
  1284. switch (scmd->cmnd[0]) {
  1285. case SYNCHRONIZE_CACHE:
  1286. /*
  1287. * FW takes care of flush cache on its own
  1288. * No need to send it down
  1289. */
  1290. scmd->result = DID_OK << 16;
  1291. goto out_done;
  1292. default:
  1293. break;
  1294. }
  1295. if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
  1296. printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
  1297. return SCSI_MLQUEUE_HOST_BUSY;
  1298. }
  1299. return 0;
  1300. out_done:
  1301. done(scmd);
  1302. return 0;
  1303. }
  1304. static DEF_SCSI_QCMD(megasas_queue_command)
  1305. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1306. {
  1307. int i;
  1308. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1309. if ((megasas_mgmt_info.instance[i]) &&
  1310. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1311. return megasas_mgmt_info.instance[i];
  1312. }
  1313. return NULL;
  1314. }
  1315. static int megasas_slave_configure(struct scsi_device *sdev)
  1316. {
  1317. u16 pd_index = 0;
  1318. struct megasas_instance *instance ;
  1319. instance = megasas_lookup_instance(sdev->host->host_no);
  1320. /*
  1321. * Don't export physical disk devices to the disk driver.
  1322. *
  1323. * FIXME: Currently we don't export them to the midlayer at all.
  1324. * That will be fixed once LSI engineers have audited the
  1325. * firmware for possible issues.
  1326. */
  1327. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1328. sdev->type == TYPE_DISK) {
  1329. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1330. sdev->id;
  1331. if (instance->pd_list[pd_index].driveState ==
  1332. MR_PD_STATE_SYSTEM) {
  1333. blk_queue_rq_timeout(sdev->request_queue,
  1334. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1335. return 0;
  1336. }
  1337. return -ENXIO;
  1338. }
  1339. /*
  1340. * The RAID firmware may require extended timeouts.
  1341. */
  1342. blk_queue_rq_timeout(sdev->request_queue,
  1343. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1344. return 0;
  1345. }
  1346. static int megasas_slave_alloc(struct scsi_device *sdev)
  1347. {
  1348. u16 pd_index = 0;
  1349. struct megasas_instance *instance ;
  1350. instance = megasas_lookup_instance(sdev->host->host_no);
  1351. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1352. (sdev->type == TYPE_DISK)) {
  1353. /*
  1354. * Open the OS scan to the SYSTEM PD
  1355. */
  1356. pd_index =
  1357. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1358. sdev->id;
  1359. if ((instance->pd_list[pd_index].driveState ==
  1360. MR_PD_STATE_SYSTEM) &&
  1361. (instance->pd_list[pd_index].driveType ==
  1362. TYPE_DISK)) {
  1363. return 0;
  1364. }
  1365. return -ENXIO;
  1366. }
  1367. return 0;
  1368. }
  1369. void megaraid_sas_kill_hba(struct megasas_instance *instance)
  1370. {
  1371. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1372. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  1373. (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)) {
  1374. writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
  1375. } else {
  1376. writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
  1377. }
  1378. }
  1379. /**
  1380. * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
  1381. * restored to max value
  1382. * @instance: Adapter soft state
  1383. *
  1384. */
  1385. void
  1386. megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
  1387. {
  1388. unsigned long flags;
  1389. if (instance->flag & MEGASAS_FW_BUSY
  1390. && time_after(jiffies, instance->last_time + 5 * HZ)
  1391. && atomic_read(&instance->fw_outstanding) < 17) {
  1392. spin_lock_irqsave(instance->host->host_lock, flags);
  1393. instance->flag &= ~MEGASAS_FW_BUSY;
  1394. if ((instance->pdev->device ==
  1395. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1396. (instance->pdev->device ==
  1397. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1398. instance->host->can_queue =
  1399. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1400. } else
  1401. instance->host->can_queue =
  1402. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1403. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1404. }
  1405. }
  1406. /**
  1407. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1408. * @instance_addr: Address of adapter soft state
  1409. *
  1410. * Tasklet to complete cmds
  1411. */
  1412. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1413. {
  1414. u32 producer;
  1415. u32 consumer;
  1416. u32 context;
  1417. struct megasas_cmd *cmd;
  1418. struct megasas_instance *instance =
  1419. (struct megasas_instance *)instance_addr;
  1420. unsigned long flags;
  1421. /* If we have already declared adapter dead, donot complete cmds */
  1422. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
  1423. return;
  1424. spin_lock_irqsave(&instance->completion_lock, flags);
  1425. producer = *instance->producer;
  1426. consumer = *instance->consumer;
  1427. while (consumer != producer) {
  1428. context = instance->reply_queue[consumer];
  1429. if (context >= instance->max_fw_cmds) {
  1430. printk(KERN_ERR "Unexpected context value %x\n",
  1431. context);
  1432. BUG();
  1433. }
  1434. cmd = instance->cmd_list[context];
  1435. megasas_complete_cmd(instance, cmd, DID_OK);
  1436. consumer++;
  1437. if (consumer == (instance->max_fw_cmds + 1)) {
  1438. consumer = 0;
  1439. }
  1440. }
  1441. *instance->consumer = producer;
  1442. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1443. /*
  1444. * Check if we can restore can_queue
  1445. */
  1446. megasas_check_and_restore_queue_depth(instance);
  1447. }
  1448. static void
  1449. megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
  1450. static void
  1451. process_fw_state_change_wq(struct work_struct *work);
  1452. void megasas_do_ocr(struct megasas_instance *instance)
  1453. {
  1454. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  1455. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  1456. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  1457. *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
  1458. }
  1459. instance->instancet->disable_intr(instance->reg_set);
  1460. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  1461. instance->issuepend_done = 0;
  1462. atomic_set(&instance->fw_outstanding, 0);
  1463. megasas_internal_reset_defer_cmds(instance);
  1464. process_fw_state_change_wq(&instance->work_init);
  1465. }
  1466. /**
  1467. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1468. * @instance: Adapter soft state
  1469. *
  1470. * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
  1471. * complete all its outstanding commands. Returns error if one or more IOs
  1472. * are pending after this time period. It also marks the controller dead.
  1473. */
  1474. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1475. {
  1476. int i;
  1477. u32 reset_index;
  1478. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1479. u8 adprecovery;
  1480. unsigned long flags;
  1481. struct list_head clist_local;
  1482. struct megasas_cmd *reset_cmd;
  1483. u32 fw_state;
  1484. u8 kill_adapter_flag;
  1485. spin_lock_irqsave(&instance->hba_lock, flags);
  1486. adprecovery = instance->adprecovery;
  1487. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1488. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1489. INIT_LIST_HEAD(&clist_local);
  1490. spin_lock_irqsave(&instance->hba_lock, flags);
  1491. list_splice_init(&instance->internal_reset_pending_q,
  1492. &clist_local);
  1493. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1494. printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
  1495. for (i = 0; i < wait_time; i++) {
  1496. msleep(1000);
  1497. spin_lock_irqsave(&instance->hba_lock, flags);
  1498. adprecovery = instance->adprecovery;
  1499. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1500. if (adprecovery == MEGASAS_HBA_OPERATIONAL)
  1501. break;
  1502. }
  1503. if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
  1504. printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
  1505. spin_lock_irqsave(&instance->hba_lock, flags);
  1506. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1507. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1508. return FAILED;
  1509. }
  1510. reset_index = 0;
  1511. while (!list_empty(&clist_local)) {
  1512. reset_cmd = list_entry((&clist_local)->next,
  1513. struct megasas_cmd, list);
  1514. list_del_init(&reset_cmd->list);
  1515. if (reset_cmd->scmd) {
  1516. reset_cmd->scmd->result = DID_RESET << 16;
  1517. printk(KERN_NOTICE "%d:%p reset [%02x]\n",
  1518. reset_index, reset_cmd,
  1519. reset_cmd->scmd->cmnd[0]);
  1520. reset_cmd->scmd->scsi_done(reset_cmd->scmd);
  1521. megasas_return_cmd(instance, reset_cmd);
  1522. } else if (reset_cmd->sync_cmd) {
  1523. printk(KERN_NOTICE "megasas:%p synch cmds"
  1524. "reset queue\n",
  1525. reset_cmd);
  1526. reset_cmd->cmd_status = ENODATA;
  1527. instance->instancet->fire_cmd(instance,
  1528. reset_cmd->frame_phys_addr,
  1529. 0, instance->reg_set);
  1530. } else {
  1531. printk(KERN_NOTICE "megasas: %p unexpected"
  1532. "cmds lst\n",
  1533. reset_cmd);
  1534. }
  1535. reset_index++;
  1536. }
  1537. return SUCCESS;
  1538. }
  1539. for (i = 0; i < wait_time; i++) {
  1540. int outstanding = atomic_read(&instance->fw_outstanding);
  1541. if (!outstanding)
  1542. break;
  1543. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1544. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1545. "commands to complete\n",i,outstanding);
  1546. /*
  1547. * Call cmd completion routine. Cmd to be
  1548. * be completed directly without depending on isr.
  1549. */
  1550. megasas_complete_cmd_dpc((unsigned long)instance);
  1551. }
  1552. msleep(1000);
  1553. }
  1554. i = 0;
  1555. kill_adapter_flag = 0;
  1556. do {
  1557. fw_state = instance->instancet->read_fw_status_reg(
  1558. instance->reg_set) & MFI_STATE_MASK;
  1559. if ((fw_state == MFI_STATE_FAULT) &&
  1560. (instance->disableOnlineCtrlReset == 0)) {
  1561. if (i == 3) {
  1562. kill_adapter_flag = 2;
  1563. break;
  1564. }
  1565. megasas_do_ocr(instance);
  1566. kill_adapter_flag = 1;
  1567. /* wait for 1 secs to let FW finish the pending cmds */
  1568. msleep(1000);
  1569. }
  1570. i++;
  1571. } while (i <= 3);
  1572. if (atomic_read(&instance->fw_outstanding) &&
  1573. !kill_adapter_flag) {
  1574. if (instance->disableOnlineCtrlReset == 0) {
  1575. megasas_do_ocr(instance);
  1576. /* wait for 5 secs to let FW finish the pending cmds */
  1577. for (i = 0; i < wait_time; i++) {
  1578. int outstanding =
  1579. atomic_read(&instance->fw_outstanding);
  1580. if (!outstanding)
  1581. return SUCCESS;
  1582. msleep(1000);
  1583. }
  1584. }
  1585. }
  1586. if (atomic_read(&instance->fw_outstanding) ||
  1587. (kill_adapter_flag == 2)) {
  1588. printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
  1589. /*
  1590. * Send signal to FW to stop processing any pending cmds.
  1591. * The controller will be taken offline by the OS now.
  1592. */
  1593. if ((instance->pdev->device ==
  1594. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1595. (instance->pdev->device ==
  1596. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1597. writel(MFI_STOP_ADP,
  1598. &instance->reg_set->doorbell);
  1599. } else {
  1600. writel(MFI_STOP_ADP,
  1601. &instance->reg_set->inbound_doorbell);
  1602. }
  1603. megasas_dump_pending_frames(instance);
  1604. spin_lock_irqsave(&instance->hba_lock, flags);
  1605. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  1606. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1607. return FAILED;
  1608. }
  1609. printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
  1610. return SUCCESS;
  1611. }
  1612. /**
  1613. * megasas_generic_reset - Generic reset routine
  1614. * @scmd: Mid-layer SCSI command
  1615. *
  1616. * This routine implements a generic reset handler for device, bus and host
  1617. * reset requests. Device, bus and host specific reset handlers can use this
  1618. * function after they do their specific tasks.
  1619. */
  1620. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1621. {
  1622. int ret_val;
  1623. struct megasas_instance *instance;
  1624. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1625. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
  1626. scmd->cmnd[0], scmd->retries);
  1627. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  1628. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1629. "failures\n");
  1630. return FAILED;
  1631. }
  1632. ret_val = megasas_wait_for_outstanding(instance);
  1633. if (ret_val == SUCCESS)
  1634. printk(KERN_NOTICE "megasas: reset successful \n");
  1635. else
  1636. printk(KERN_ERR "megasas: failed to do reset\n");
  1637. return ret_val;
  1638. }
  1639. /**
  1640. * megasas_reset_timer - quiesce the adapter if required
  1641. * @scmd: scsi cmnd
  1642. *
  1643. * Sets the FW busy flag and reduces the host->can_queue if the
  1644. * cmd has not been completed within the timeout period.
  1645. */
  1646. static enum
  1647. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1648. {
  1649. struct megasas_instance *instance;
  1650. unsigned long flags;
  1651. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1652. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1653. return BLK_EH_NOT_HANDLED;
  1654. }
  1655. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1656. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1657. /* FW is busy, throttle IO */
  1658. spin_lock_irqsave(instance->host->host_lock, flags);
  1659. instance->host->can_queue = 16;
  1660. instance->last_time = jiffies;
  1661. instance->flag |= MEGASAS_FW_BUSY;
  1662. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1663. }
  1664. return BLK_EH_RESET_TIMER;
  1665. }
  1666. /**
  1667. * megasas_reset_device - Device reset handler entry point
  1668. */
  1669. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1670. {
  1671. int ret;
  1672. /*
  1673. * First wait for all commands to complete
  1674. */
  1675. ret = megasas_generic_reset(scmd);
  1676. return ret;
  1677. }
  1678. /**
  1679. * megasas_reset_bus_host - Bus & host reset handler entry point
  1680. */
  1681. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1682. {
  1683. int ret;
  1684. struct megasas_instance *instance;
  1685. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1686. /*
  1687. * First wait for all commands to complete
  1688. */
  1689. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  1690. ret = megasas_reset_fusion(scmd->device->host);
  1691. else
  1692. ret = megasas_generic_reset(scmd);
  1693. return ret;
  1694. }
  1695. /**
  1696. * megasas_bios_param - Returns disk geometry for a disk
  1697. * @sdev: device handle
  1698. * @bdev: block device
  1699. * @capacity: drive capacity
  1700. * @geom: geometry parameters
  1701. */
  1702. static int
  1703. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1704. sector_t capacity, int geom[])
  1705. {
  1706. int heads;
  1707. int sectors;
  1708. sector_t cylinders;
  1709. unsigned long tmp;
  1710. /* Default heads (64) & sectors (32) */
  1711. heads = 64;
  1712. sectors = 32;
  1713. tmp = heads * sectors;
  1714. cylinders = capacity;
  1715. sector_div(cylinders, tmp);
  1716. /*
  1717. * Handle extended translation size for logical drives > 1Gb
  1718. */
  1719. if (capacity >= 0x200000) {
  1720. heads = 255;
  1721. sectors = 63;
  1722. tmp = heads*sectors;
  1723. cylinders = capacity;
  1724. sector_div(cylinders, tmp);
  1725. }
  1726. geom[0] = heads;
  1727. geom[1] = sectors;
  1728. geom[2] = cylinders;
  1729. return 0;
  1730. }
  1731. static void megasas_aen_polling(struct work_struct *work);
  1732. /**
  1733. * megasas_service_aen - Processes an event notification
  1734. * @instance: Adapter soft state
  1735. * @cmd: AEN command completed by the ISR
  1736. *
  1737. * For AEN, driver sends a command down to FW that is held by the FW till an
  1738. * event occurs. When an event of interest occurs, FW completes the command
  1739. * that it was previously holding.
  1740. *
  1741. * This routines sends SIGIO signal to processes that have registered with the
  1742. * driver for AEN.
  1743. */
  1744. static void
  1745. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1746. {
  1747. unsigned long flags;
  1748. /*
  1749. * Don't signal app if it is just an aborted previously registered aen
  1750. */
  1751. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1752. spin_lock_irqsave(&poll_aen_lock, flags);
  1753. megasas_poll_wait_aen = 1;
  1754. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1755. wake_up(&megasas_poll_wait);
  1756. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1757. }
  1758. else
  1759. cmd->abort_aen = 0;
  1760. instance->aen_cmd = NULL;
  1761. megasas_return_cmd(instance, cmd);
  1762. if ((instance->unload == 0) &&
  1763. ((instance->issuepend_done == 1))) {
  1764. struct megasas_aen_event *ev;
  1765. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1766. if (!ev) {
  1767. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1768. } else {
  1769. ev->instance = instance;
  1770. instance->ev = ev;
  1771. INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
  1772. schedule_delayed_work(
  1773. (struct delayed_work *)&ev->hotplug_work, 0);
  1774. }
  1775. }
  1776. }
  1777. /*
  1778. * Scsi host template for megaraid_sas driver
  1779. */
  1780. static struct scsi_host_template megasas_template = {
  1781. .module = THIS_MODULE,
  1782. .name = "LSI SAS based MegaRAID driver",
  1783. .proc_name = "megaraid_sas",
  1784. .slave_configure = megasas_slave_configure,
  1785. .slave_alloc = megasas_slave_alloc,
  1786. .queuecommand = megasas_queue_command,
  1787. .eh_device_reset_handler = megasas_reset_device,
  1788. .eh_bus_reset_handler = megasas_reset_bus_host,
  1789. .eh_host_reset_handler = megasas_reset_bus_host,
  1790. .eh_timed_out = megasas_reset_timer,
  1791. .bios_param = megasas_bios_param,
  1792. .use_clustering = ENABLE_CLUSTERING,
  1793. };
  1794. /**
  1795. * megasas_complete_int_cmd - Completes an internal command
  1796. * @instance: Adapter soft state
  1797. * @cmd: Command to be completed
  1798. *
  1799. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1800. * after it issues a command. This function wakes up that waiting routine by
  1801. * calling wake_up() on the wait queue.
  1802. */
  1803. static void
  1804. megasas_complete_int_cmd(struct megasas_instance *instance,
  1805. struct megasas_cmd *cmd)
  1806. {
  1807. cmd->cmd_status = cmd->frame->io.cmd_status;
  1808. if (cmd->cmd_status == ENODATA) {
  1809. cmd->cmd_status = 0;
  1810. }
  1811. wake_up(&instance->int_cmd_wait_q);
  1812. }
  1813. /**
  1814. * megasas_complete_abort - Completes aborting a command
  1815. * @instance: Adapter soft state
  1816. * @cmd: Cmd that was issued to abort another cmd
  1817. *
  1818. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1819. * after it issues an abort on a previously issued command. This function
  1820. * wakes up all functions waiting on the same wait queue.
  1821. */
  1822. static void
  1823. megasas_complete_abort(struct megasas_instance *instance,
  1824. struct megasas_cmd *cmd)
  1825. {
  1826. if (cmd->sync_cmd) {
  1827. cmd->sync_cmd = 0;
  1828. cmd->cmd_status = 0;
  1829. wake_up(&instance->abort_cmd_wait_q);
  1830. }
  1831. return;
  1832. }
  1833. /**
  1834. * megasas_complete_cmd - Completes a command
  1835. * @instance: Adapter soft state
  1836. * @cmd: Command to be completed
  1837. * @alt_status: If non-zero, use this value as status to
  1838. * SCSI mid-layer instead of the value returned
  1839. * by the FW. This should be used if caller wants
  1840. * an alternate status (as in the case of aborted
  1841. * commands)
  1842. */
  1843. void
  1844. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1845. u8 alt_status)
  1846. {
  1847. int exception = 0;
  1848. struct megasas_header *hdr = &cmd->frame->hdr;
  1849. unsigned long flags;
  1850. struct fusion_context *fusion = instance->ctrl_context;
  1851. /* flag for the retry reset */
  1852. cmd->retry_for_fw_reset = 0;
  1853. if (cmd->scmd)
  1854. cmd->scmd->SCp.ptr = NULL;
  1855. switch (hdr->cmd) {
  1856. case MFI_CMD_PD_SCSI_IO:
  1857. case MFI_CMD_LD_SCSI_IO:
  1858. /*
  1859. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1860. * issued either through an IO path or an IOCTL path. If it
  1861. * was via IOCTL, we will send it to internal completion.
  1862. */
  1863. if (cmd->sync_cmd) {
  1864. cmd->sync_cmd = 0;
  1865. megasas_complete_int_cmd(instance, cmd);
  1866. break;
  1867. }
  1868. case MFI_CMD_LD_READ:
  1869. case MFI_CMD_LD_WRITE:
  1870. if (alt_status) {
  1871. cmd->scmd->result = alt_status << 16;
  1872. exception = 1;
  1873. }
  1874. if (exception) {
  1875. atomic_dec(&instance->fw_outstanding);
  1876. scsi_dma_unmap(cmd->scmd);
  1877. cmd->scmd->scsi_done(cmd->scmd);
  1878. megasas_return_cmd(instance, cmd);
  1879. break;
  1880. }
  1881. switch (hdr->cmd_status) {
  1882. case MFI_STAT_OK:
  1883. cmd->scmd->result = DID_OK << 16;
  1884. break;
  1885. case MFI_STAT_SCSI_IO_FAILED:
  1886. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1887. cmd->scmd->result =
  1888. (DID_ERROR << 16) | hdr->scsi_status;
  1889. break;
  1890. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1891. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1892. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1893. memset(cmd->scmd->sense_buffer, 0,
  1894. SCSI_SENSE_BUFFERSIZE);
  1895. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1896. hdr->sense_len);
  1897. cmd->scmd->result |= DRIVER_SENSE << 24;
  1898. }
  1899. break;
  1900. case MFI_STAT_LD_OFFLINE:
  1901. case MFI_STAT_DEVICE_NOT_FOUND:
  1902. cmd->scmd->result = DID_BAD_TARGET << 16;
  1903. break;
  1904. default:
  1905. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1906. hdr->cmd_status);
  1907. cmd->scmd->result = DID_ERROR << 16;
  1908. break;
  1909. }
  1910. atomic_dec(&instance->fw_outstanding);
  1911. scsi_dma_unmap(cmd->scmd);
  1912. cmd->scmd->scsi_done(cmd->scmd);
  1913. megasas_return_cmd(instance, cmd);
  1914. break;
  1915. case MFI_CMD_SMP:
  1916. case MFI_CMD_STP:
  1917. case MFI_CMD_DCMD:
  1918. /* Check for LD map update */
  1919. if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
  1920. (cmd->frame->dcmd.mbox.b[1] == 1)) {
  1921. spin_lock_irqsave(instance->host->host_lock, flags);
  1922. if (cmd->frame->hdr.cmd_status != 0) {
  1923. if (cmd->frame->hdr.cmd_status !=
  1924. MFI_STAT_NOT_FOUND)
  1925. printk(KERN_WARNING "megasas: map sync"
  1926. "failed, status = 0x%x.\n",
  1927. cmd->frame->hdr.cmd_status);
  1928. else {
  1929. megasas_return_cmd(instance, cmd);
  1930. spin_unlock_irqrestore(
  1931. instance->host->host_lock,
  1932. flags);
  1933. break;
  1934. }
  1935. } else
  1936. instance->map_id++;
  1937. megasas_return_cmd(instance, cmd);
  1938. if (MR_ValidateMapInfo(
  1939. fusion->ld_map[(instance->map_id & 1)],
  1940. fusion->load_balance_info))
  1941. fusion->fast_path_io = 1;
  1942. else
  1943. fusion->fast_path_io = 0;
  1944. megasas_sync_map_info(instance);
  1945. spin_unlock_irqrestore(instance->host->host_lock,
  1946. flags);
  1947. break;
  1948. }
  1949. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1950. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1951. spin_lock_irqsave(&poll_aen_lock, flags);
  1952. megasas_poll_wait_aen = 0;
  1953. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1954. }
  1955. /*
  1956. * See if got an event notification
  1957. */
  1958. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1959. megasas_service_aen(instance, cmd);
  1960. else
  1961. megasas_complete_int_cmd(instance, cmd);
  1962. break;
  1963. case MFI_CMD_ABORT:
  1964. /*
  1965. * Cmd issued to abort another cmd returned
  1966. */
  1967. megasas_complete_abort(instance, cmd);
  1968. break;
  1969. default:
  1970. printk("megasas: Unknown command completed! [0x%X]\n",
  1971. hdr->cmd);
  1972. break;
  1973. }
  1974. }
  1975. /**
  1976. * megasas_issue_pending_cmds_again - issue all pending cmds
  1977. * in FW again because of the fw reset
  1978. * @instance: Adapter soft state
  1979. */
  1980. static inline void
  1981. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  1982. {
  1983. struct megasas_cmd *cmd;
  1984. struct list_head clist_local;
  1985. union megasas_evt_class_locale class_locale;
  1986. unsigned long flags;
  1987. u32 seq_num;
  1988. INIT_LIST_HEAD(&clist_local);
  1989. spin_lock_irqsave(&instance->hba_lock, flags);
  1990. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  1991. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1992. while (!list_empty(&clist_local)) {
  1993. cmd = list_entry((&clist_local)->next,
  1994. struct megasas_cmd, list);
  1995. list_del_init(&cmd->list);
  1996. if (cmd->sync_cmd || cmd->scmd) {
  1997. printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
  1998. "detected to be pending while HBA reset.\n",
  1999. cmd, cmd->scmd, cmd->sync_cmd);
  2000. cmd->retry_for_fw_reset++;
  2001. if (cmd->retry_for_fw_reset == 3) {
  2002. printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
  2003. "was tried multiple times during reset."
  2004. "Shutting down the HBA\n",
  2005. cmd, cmd->scmd, cmd->sync_cmd);
  2006. megaraid_sas_kill_hba(instance);
  2007. instance->adprecovery =
  2008. MEGASAS_HW_CRITICAL_ERROR;
  2009. return;
  2010. }
  2011. }
  2012. if (cmd->sync_cmd == 1) {
  2013. if (cmd->scmd) {
  2014. printk(KERN_NOTICE "megaraid_sas: unexpected"
  2015. "cmd attached to internal command!\n");
  2016. }
  2017. printk(KERN_NOTICE "megasas: %p synchronous cmd"
  2018. "on the internal reset queue,"
  2019. "issue it again.\n", cmd);
  2020. cmd->cmd_status = ENODATA;
  2021. instance->instancet->fire_cmd(instance,
  2022. cmd->frame_phys_addr ,
  2023. 0, instance->reg_set);
  2024. } else if (cmd->scmd) {
  2025. printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
  2026. "detected on the internal queue, issue again.\n",
  2027. cmd, cmd->scmd->cmnd[0]);
  2028. atomic_inc(&instance->fw_outstanding);
  2029. instance->instancet->fire_cmd(instance,
  2030. cmd->frame_phys_addr,
  2031. cmd->frame_count-1, instance->reg_set);
  2032. } else {
  2033. printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
  2034. "internal reset defer list while re-issue!!\n",
  2035. cmd);
  2036. }
  2037. }
  2038. if (instance->aen_cmd) {
  2039. printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
  2040. megasas_return_cmd(instance, instance->aen_cmd);
  2041. instance->aen_cmd = NULL;
  2042. }
  2043. /*
  2044. * Initiate AEN (Asynchronous Event Notification)
  2045. */
  2046. seq_num = instance->last_seq_num;
  2047. class_locale.members.reserved = 0;
  2048. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2049. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2050. megasas_register_aen(instance, seq_num, class_locale.word);
  2051. }
  2052. /**
  2053. * Move the internal reset pending commands to a deferred queue.
  2054. *
  2055. * We move the commands pending at internal reset time to a
  2056. * pending queue. This queue would be flushed after successful
  2057. * completion of the internal reset sequence. if the internal reset
  2058. * did not complete in time, the kernel reset handler would flush
  2059. * these commands.
  2060. **/
  2061. static void
  2062. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2063. {
  2064. struct megasas_cmd *cmd;
  2065. int i;
  2066. u32 max_cmd = instance->max_fw_cmds;
  2067. u32 defer_index;
  2068. unsigned long flags;
  2069. defer_index = 0;
  2070. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  2071. for (i = 0; i < max_cmd; i++) {
  2072. cmd = instance->cmd_list[i];
  2073. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2074. printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
  2075. "on the defer queue as internal\n",
  2076. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2077. if (!list_empty(&cmd->list)) {
  2078. printk(KERN_NOTICE "megaraid_sas: ERROR while"
  2079. " moving this cmd:%p, %d %p, it was"
  2080. "discovered on some list?\n",
  2081. cmd, cmd->sync_cmd, cmd->scmd);
  2082. list_del_init(&cmd->list);
  2083. }
  2084. defer_index++;
  2085. list_add_tail(&cmd->list,
  2086. &instance->internal_reset_pending_q);
  2087. }
  2088. }
  2089. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  2090. }
  2091. static void
  2092. process_fw_state_change_wq(struct work_struct *work)
  2093. {
  2094. struct megasas_instance *instance =
  2095. container_of(work, struct megasas_instance, work_init);
  2096. u32 wait;
  2097. unsigned long flags;
  2098. if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
  2099. printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
  2100. instance->adprecovery);
  2101. return ;
  2102. }
  2103. if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
  2104. printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
  2105. "state, restarting it...\n");
  2106. instance->instancet->disable_intr(instance->reg_set);
  2107. atomic_set(&instance->fw_outstanding, 0);
  2108. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2109. instance->instancet->adp_reset(instance, instance->reg_set);
  2110. atomic_set(&instance->fw_reset_no_pci_access, 0 );
  2111. printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
  2112. "initiating next stage...\n");
  2113. printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
  2114. "state 2 starting...\n");
  2115. /*waitting for about 20 second before start the second init*/
  2116. for (wait = 0; wait < 30; wait++) {
  2117. msleep(1000);
  2118. }
  2119. if (megasas_transition_to_ready(instance)) {
  2120. printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
  2121. megaraid_sas_kill_hba(instance);
  2122. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2123. return ;
  2124. }
  2125. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2126. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2127. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2128. ) {
  2129. *instance->consumer = *instance->producer;
  2130. } else {
  2131. *instance->consumer = 0;
  2132. *instance->producer = 0;
  2133. }
  2134. megasas_issue_init_mfi(instance);
  2135. spin_lock_irqsave(&instance->hba_lock, flags);
  2136. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2137. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2138. instance->instancet->enable_intr(instance->reg_set);
  2139. megasas_issue_pending_cmds_again(instance);
  2140. instance->issuepend_done = 1;
  2141. }
  2142. return ;
  2143. }
  2144. /**
  2145. * megasas_deplete_reply_queue - Processes all completed commands
  2146. * @instance: Adapter soft state
  2147. * @alt_status: Alternate status to be returned to
  2148. * SCSI mid-layer instead of the status
  2149. * returned by the FW
  2150. * Note: this must be called with hba lock held
  2151. */
  2152. static int
  2153. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2154. u8 alt_status)
  2155. {
  2156. u32 mfiStatus;
  2157. u32 fw_state;
  2158. if ((mfiStatus = instance->instancet->check_reset(instance,
  2159. instance->reg_set)) == 1) {
  2160. return IRQ_HANDLED;
  2161. }
  2162. if ((mfiStatus = instance->instancet->clear_intr(
  2163. instance->reg_set)
  2164. ) == 0) {
  2165. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2166. if (!instance->msi_flag)
  2167. return IRQ_NONE;
  2168. }
  2169. instance->mfiStatus = mfiStatus;
  2170. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2171. fw_state = instance->instancet->read_fw_status_reg(
  2172. instance->reg_set) & MFI_STATE_MASK;
  2173. if (fw_state != MFI_STATE_FAULT) {
  2174. printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
  2175. fw_state);
  2176. }
  2177. if ((fw_state == MFI_STATE_FAULT) &&
  2178. (instance->disableOnlineCtrlReset == 0)) {
  2179. printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
  2180. if ((instance->pdev->device ==
  2181. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2182. (instance->pdev->device ==
  2183. PCI_DEVICE_ID_DELL_PERC5) ||
  2184. (instance->pdev->device ==
  2185. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2186. *instance->consumer =
  2187. MEGASAS_ADPRESET_INPROG_SIGN;
  2188. }
  2189. instance->instancet->disable_intr(instance->reg_set);
  2190. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2191. instance->issuepend_done = 0;
  2192. atomic_set(&instance->fw_outstanding, 0);
  2193. megasas_internal_reset_defer_cmds(instance);
  2194. printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
  2195. fw_state, instance->adprecovery);
  2196. schedule_work(&instance->work_init);
  2197. return IRQ_HANDLED;
  2198. } else {
  2199. printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
  2200. fw_state, instance->disableOnlineCtrlReset);
  2201. }
  2202. }
  2203. tasklet_schedule(&instance->isr_tasklet);
  2204. return IRQ_HANDLED;
  2205. }
  2206. /**
  2207. * megasas_isr - isr entry point
  2208. */
  2209. static irqreturn_t megasas_isr(int irq, void *devp)
  2210. {
  2211. struct megasas_instance *instance;
  2212. unsigned long flags;
  2213. irqreturn_t rc;
  2214. if (atomic_read(
  2215. &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
  2216. return IRQ_HANDLED;
  2217. instance = (struct megasas_instance *)devp;
  2218. spin_lock_irqsave(&instance->hba_lock, flags);
  2219. rc = megasas_deplete_reply_queue(instance, DID_OK);
  2220. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2221. return rc;
  2222. }
  2223. /**
  2224. * megasas_transition_to_ready - Move the FW to READY state
  2225. * @instance: Adapter soft state
  2226. *
  2227. * During the initialization, FW passes can potentially be in any one of
  2228. * several possible states. If the FW in operational, waiting-for-handshake
  2229. * states, driver must take steps to bring it to ready state. Otherwise, it
  2230. * has to wait for the ready state.
  2231. */
  2232. int
  2233. megasas_transition_to_ready(struct megasas_instance* instance)
  2234. {
  2235. int i;
  2236. u8 max_wait;
  2237. u32 fw_state;
  2238. u32 cur_state;
  2239. u32 abs_state, curr_abs_state;
  2240. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2241. if (fw_state != MFI_STATE_READY)
  2242. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  2243. " state\n");
  2244. while (fw_state != MFI_STATE_READY) {
  2245. abs_state =
  2246. instance->instancet->read_fw_status_reg(instance->reg_set);
  2247. switch (fw_state) {
  2248. case MFI_STATE_FAULT:
  2249. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  2250. max_wait = MEGASAS_RESET_WAIT_TIME;
  2251. cur_state = MFI_STATE_FAULT;
  2252. break;
  2253. case MFI_STATE_WAIT_HANDSHAKE:
  2254. /*
  2255. * Set the CLR bit in inbound doorbell
  2256. */
  2257. if ((instance->pdev->device ==
  2258. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2259. (instance->pdev->device ==
  2260. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2261. (instance->pdev->device ==
  2262. PCI_DEVICE_ID_LSI_FUSION)) {
  2263. writel(
  2264. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2265. &instance->reg_set->doorbell);
  2266. } else {
  2267. writel(
  2268. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2269. &instance->reg_set->inbound_doorbell);
  2270. }
  2271. max_wait = MEGASAS_RESET_WAIT_TIME;
  2272. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  2273. break;
  2274. case MFI_STATE_BOOT_MESSAGE_PENDING:
  2275. if ((instance->pdev->device ==
  2276. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2277. (instance->pdev->device ==
  2278. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2279. (instance->pdev->device ==
  2280. PCI_DEVICE_ID_LSI_FUSION)) {
  2281. writel(MFI_INIT_HOTPLUG,
  2282. &instance->reg_set->doorbell);
  2283. } else
  2284. writel(MFI_INIT_HOTPLUG,
  2285. &instance->reg_set->inbound_doorbell);
  2286. max_wait = MEGASAS_RESET_WAIT_TIME;
  2287. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  2288. break;
  2289. case MFI_STATE_OPERATIONAL:
  2290. /*
  2291. * Bring it to READY state; assuming max wait 10 secs
  2292. */
  2293. instance->instancet->disable_intr(instance->reg_set);
  2294. if ((instance->pdev->device ==
  2295. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2296. (instance->pdev->device ==
  2297. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2298. (instance->pdev->device
  2299. == PCI_DEVICE_ID_LSI_FUSION)) {
  2300. writel(MFI_RESET_FLAGS,
  2301. &instance->reg_set->doorbell);
  2302. if (instance->pdev->device ==
  2303. PCI_DEVICE_ID_LSI_FUSION) {
  2304. for (i = 0; i < (10 * 1000); i += 20) {
  2305. if (readl(
  2306. &instance->
  2307. reg_set->
  2308. doorbell) & 1)
  2309. msleep(20);
  2310. else
  2311. break;
  2312. }
  2313. }
  2314. } else
  2315. writel(MFI_RESET_FLAGS,
  2316. &instance->reg_set->inbound_doorbell);
  2317. max_wait = MEGASAS_RESET_WAIT_TIME;
  2318. cur_state = MFI_STATE_OPERATIONAL;
  2319. break;
  2320. case MFI_STATE_UNDEFINED:
  2321. /*
  2322. * This state should not last for more than 2 seconds
  2323. */
  2324. max_wait = MEGASAS_RESET_WAIT_TIME;
  2325. cur_state = MFI_STATE_UNDEFINED;
  2326. break;
  2327. case MFI_STATE_BB_INIT:
  2328. max_wait = MEGASAS_RESET_WAIT_TIME;
  2329. cur_state = MFI_STATE_BB_INIT;
  2330. break;
  2331. case MFI_STATE_FW_INIT:
  2332. max_wait = MEGASAS_RESET_WAIT_TIME;
  2333. cur_state = MFI_STATE_FW_INIT;
  2334. break;
  2335. case MFI_STATE_FW_INIT_2:
  2336. max_wait = MEGASAS_RESET_WAIT_TIME;
  2337. cur_state = MFI_STATE_FW_INIT_2;
  2338. break;
  2339. case MFI_STATE_DEVICE_SCAN:
  2340. max_wait = MEGASAS_RESET_WAIT_TIME;
  2341. cur_state = MFI_STATE_DEVICE_SCAN;
  2342. break;
  2343. case MFI_STATE_FLUSH_CACHE:
  2344. max_wait = MEGASAS_RESET_WAIT_TIME;
  2345. cur_state = MFI_STATE_FLUSH_CACHE;
  2346. break;
  2347. default:
  2348. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  2349. fw_state);
  2350. return -ENODEV;
  2351. }
  2352. /*
  2353. * The cur_state should not last for more than max_wait secs
  2354. */
  2355. for (i = 0; i < (max_wait * 1000); i++) {
  2356. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  2357. MFI_STATE_MASK ;
  2358. curr_abs_state =
  2359. instance->instancet->read_fw_status_reg(instance->reg_set);
  2360. if (abs_state == curr_abs_state) {
  2361. msleep(1);
  2362. } else
  2363. break;
  2364. }
  2365. /*
  2366. * Return error if fw_state hasn't changed after max_wait
  2367. */
  2368. if (curr_abs_state == abs_state) {
  2369. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  2370. "in %d secs\n", fw_state, max_wait);
  2371. return -ENODEV;
  2372. }
  2373. }
  2374. printk(KERN_INFO "megasas: FW now in Ready state\n");
  2375. return 0;
  2376. }
  2377. /**
  2378. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  2379. * @instance: Adapter soft state
  2380. */
  2381. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  2382. {
  2383. int i;
  2384. u32 max_cmd = instance->max_mfi_cmds;
  2385. struct megasas_cmd *cmd;
  2386. if (!instance->frame_dma_pool)
  2387. return;
  2388. /*
  2389. * Return all frames to pool
  2390. */
  2391. for (i = 0; i < max_cmd; i++) {
  2392. cmd = instance->cmd_list[i];
  2393. if (cmd->frame)
  2394. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  2395. cmd->frame_phys_addr);
  2396. if (cmd->sense)
  2397. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  2398. cmd->sense_phys_addr);
  2399. }
  2400. /*
  2401. * Now destroy the pool itself
  2402. */
  2403. pci_pool_destroy(instance->frame_dma_pool);
  2404. pci_pool_destroy(instance->sense_dma_pool);
  2405. instance->frame_dma_pool = NULL;
  2406. instance->sense_dma_pool = NULL;
  2407. }
  2408. /**
  2409. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  2410. * @instance: Adapter soft state
  2411. *
  2412. * Each command packet has an embedded DMA memory buffer that is used for
  2413. * filling MFI frame and the SG list that immediately follows the frame. This
  2414. * function creates those DMA memory buffers for each command packet by using
  2415. * PCI pool facility.
  2416. */
  2417. static int megasas_create_frame_pool(struct megasas_instance *instance)
  2418. {
  2419. int i;
  2420. u32 max_cmd;
  2421. u32 sge_sz;
  2422. u32 sgl_sz;
  2423. u32 total_sz;
  2424. u32 frame_count;
  2425. struct megasas_cmd *cmd;
  2426. max_cmd = instance->max_mfi_cmds;
  2427. /*
  2428. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  2429. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  2430. */
  2431. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  2432. sizeof(struct megasas_sge32);
  2433. if (instance->flag_ieee) {
  2434. sge_sz = sizeof(struct megasas_sge_skinny);
  2435. }
  2436. /*
  2437. * Calculated the number of 64byte frames required for SGL
  2438. */
  2439. sgl_sz = sge_sz * instance->max_num_sge;
  2440. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  2441. frame_count = 15;
  2442. /*
  2443. * We need one extra frame for the MFI command
  2444. */
  2445. frame_count++;
  2446. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  2447. /*
  2448. * Use DMA pool facility provided by PCI layer
  2449. */
  2450. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  2451. instance->pdev, total_sz, 64,
  2452. 0);
  2453. if (!instance->frame_dma_pool) {
  2454. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  2455. return -ENOMEM;
  2456. }
  2457. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  2458. instance->pdev, 128, 4, 0);
  2459. if (!instance->sense_dma_pool) {
  2460. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  2461. pci_pool_destroy(instance->frame_dma_pool);
  2462. instance->frame_dma_pool = NULL;
  2463. return -ENOMEM;
  2464. }
  2465. /*
  2466. * Allocate and attach a frame to each of the commands in cmd_list.
  2467. * By making cmd->index as the context instead of the &cmd, we can
  2468. * always use 32bit context regardless of the architecture
  2469. */
  2470. for (i = 0; i < max_cmd; i++) {
  2471. cmd = instance->cmd_list[i];
  2472. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  2473. GFP_KERNEL, &cmd->frame_phys_addr);
  2474. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  2475. GFP_KERNEL, &cmd->sense_phys_addr);
  2476. /*
  2477. * megasas_teardown_frame_pool() takes care of freeing
  2478. * whatever has been allocated
  2479. */
  2480. if (!cmd->frame || !cmd->sense) {
  2481. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  2482. megasas_teardown_frame_pool(instance);
  2483. return -ENOMEM;
  2484. }
  2485. memset(cmd->frame, 0, total_sz);
  2486. cmd->frame->io.context = cmd->index;
  2487. cmd->frame->io.pad_0 = 0;
  2488. }
  2489. return 0;
  2490. }
  2491. /**
  2492. * megasas_free_cmds - Free all the cmds in the free cmd pool
  2493. * @instance: Adapter soft state
  2494. */
  2495. void megasas_free_cmds(struct megasas_instance *instance)
  2496. {
  2497. int i;
  2498. /* First free the MFI frame pool */
  2499. megasas_teardown_frame_pool(instance);
  2500. /* Free all the commands in the cmd_list */
  2501. for (i = 0; i < instance->max_mfi_cmds; i++)
  2502. kfree(instance->cmd_list[i]);
  2503. /* Free the cmd_list buffer itself */
  2504. kfree(instance->cmd_list);
  2505. instance->cmd_list = NULL;
  2506. INIT_LIST_HEAD(&instance->cmd_pool);
  2507. }
  2508. /**
  2509. * megasas_alloc_cmds - Allocates the command packets
  2510. * @instance: Adapter soft state
  2511. *
  2512. * Each command that is issued to the FW, whether IO commands from the OS or
  2513. * internal commands like IOCTLs, are wrapped in local data structure called
  2514. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  2515. * the FW.
  2516. *
  2517. * Each frame has a 32-bit field called context (tag). This context is used
  2518. * to get back the megasas_cmd from the frame when a frame gets completed in
  2519. * the ISR. Typically the address of the megasas_cmd itself would be used as
  2520. * the context. But we wanted to keep the differences between 32 and 64 bit
  2521. * systems to the mininum. We always use 32 bit integers for the context. In
  2522. * this driver, the 32 bit values are the indices into an array cmd_list.
  2523. * This array is used only to look up the megasas_cmd given the context. The
  2524. * free commands themselves are maintained in a linked list called cmd_pool.
  2525. */
  2526. int megasas_alloc_cmds(struct megasas_instance *instance)
  2527. {
  2528. int i;
  2529. int j;
  2530. u32 max_cmd;
  2531. struct megasas_cmd *cmd;
  2532. max_cmd = instance->max_mfi_cmds;
  2533. /*
  2534. * instance->cmd_list is an array of struct megasas_cmd pointers.
  2535. * Allocate the dynamic array first and then allocate individual
  2536. * commands.
  2537. */
  2538. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  2539. if (!instance->cmd_list) {
  2540. printk(KERN_DEBUG "megasas: out of memory\n");
  2541. return -ENOMEM;
  2542. }
  2543. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  2544. for (i = 0; i < max_cmd; i++) {
  2545. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  2546. GFP_KERNEL);
  2547. if (!instance->cmd_list[i]) {
  2548. for (j = 0; j < i; j++)
  2549. kfree(instance->cmd_list[j]);
  2550. kfree(instance->cmd_list);
  2551. instance->cmd_list = NULL;
  2552. return -ENOMEM;
  2553. }
  2554. }
  2555. /*
  2556. * Add all the commands to command pool (instance->cmd_pool)
  2557. */
  2558. for (i = 0; i < max_cmd; i++) {
  2559. cmd = instance->cmd_list[i];
  2560. memset(cmd, 0, sizeof(struct megasas_cmd));
  2561. cmd->index = i;
  2562. cmd->scmd = NULL;
  2563. cmd->instance = instance;
  2564. list_add_tail(&cmd->list, &instance->cmd_pool);
  2565. }
  2566. /*
  2567. * Create a frame pool and assign one frame to each cmd
  2568. */
  2569. if (megasas_create_frame_pool(instance)) {
  2570. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  2571. megasas_free_cmds(instance);
  2572. }
  2573. return 0;
  2574. }
  2575. /*
  2576. * megasas_get_pd_list_info - Returns FW's pd_list structure
  2577. * @instance: Adapter soft state
  2578. * @pd_list: pd_list structure
  2579. *
  2580. * Issues an internal command (DCMD) to get the FW's controller PD
  2581. * list structure. This information is mainly used to find out SYSTEM
  2582. * supported by the FW.
  2583. */
  2584. static int
  2585. megasas_get_pd_list(struct megasas_instance *instance)
  2586. {
  2587. int ret = 0, pd_index = 0;
  2588. struct megasas_cmd *cmd;
  2589. struct megasas_dcmd_frame *dcmd;
  2590. struct MR_PD_LIST *ci;
  2591. struct MR_PD_ADDRESS *pd_addr;
  2592. dma_addr_t ci_h = 0;
  2593. cmd = megasas_get_cmd(instance);
  2594. if (!cmd) {
  2595. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  2596. return -ENOMEM;
  2597. }
  2598. dcmd = &cmd->frame->dcmd;
  2599. ci = pci_alloc_consistent(instance->pdev,
  2600. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  2601. if (!ci) {
  2602. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  2603. megasas_return_cmd(instance, cmd);
  2604. return -ENOMEM;
  2605. }
  2606. memset(ci, 0, sizeof(*ci));
  2607. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2608. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  2609. dcmd->mbox.b[1] = 0;
  2610. dcmd->cmd = MFI_CMD_DCMD;
  2611. dcmd->cmd_status = 0xFF;
  2612. dcmd->sge_count = 1;
  2613. dcmd->flags = MFI_FRAME_DIR_READ;
  2614. dcmd->timeout = 0;
  2615. dcmd->pad_0 = 0;
  2616. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2617. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  2618. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2619. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2620. if (!megasas_issue_polled(instance, cmd)) {
  2621. ret = 0;
  2622. } else {
  2623. ret = -1;
  2624. }
  2625. /*
  2626. * the following function will get the instance PD LIST.
  2627. */
  2628. pd_addr = ci->addr;
  2629. if ( ret == 0 &&
  2630. (ci->count <
  2631. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  2632. memset(instance->pd_list, 0,
  2633. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  2634. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  2635. instance->pd_list[pd_addr->deviceId].tid =
  2636. pd_addr->deviceId;
  2637. instance->pd_list[pd_addr->deviceId].driveType =
  2638. pd_addr->scsiDevType;
  2639. instance->pd_list[pd_addr->deviceId].driveState =
  2640. MR_PD_STATE_SYSTEM;
  2641. pd_addr++;
  2642. }
  2643. }
  2644. pci_free_consistent(instance->pdev,
  2645. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  2646. ci, ci_h);
  2647. megasas_return_cmd(instance, cmd);
  2648. return ret;
  2649. }
  2650. /*
  2651. * megasas_get_ld_list_info - Returns FW's ld_list structure
  2652. * @instance: Adapter soft state
  2653. * @ld_list: ld_list structure
  2654. *
  2655. * Issues an internal command (DCMD) to get the FW's controller PD
  2656. * list structure. This information is mainly used to find out SYSTEM
  2657. * supported by the FW.
  2658. */
  2659. static int
  2660. megasas_get_ld_list(struct megasas_instance *instance)
  2661. {
  2662. int ret = 0, ld_index = 0, ids = 0;
  2663. struct megasas_cmd *cmd;
  2664. struct megasas_dcmd_frame *dcmd;
  2665. struct MR_LD_LIST *ci;
  2666. dma_addr_t ci_h = 0;
  2667. cmd = megasas_get_cmd(instance);
  2668. if (!cmd) {
  2669. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  2670. return -ENOMEM;
  2671. }
  2672. dcmd = &cmd->frame->dcmd;
  2673. ci = pci_alloc_consistent(instance->pdev,
  2674. sizeof(struct MR_LD_LIST),
  2675. &ci_h);
  2676. if (!ci) {
  2677. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  2678. megasas_return_cmd(instance, cmd);
  2679. return -ENOMEM;
  2680. }
  2681. memset(ci, 0, sizeof(*ci));
  2682. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2683. dcmd->cmd = MFI_CMD_DCMD;
  2684. dcmd->cmd_status = 0xFF;
  2685. dcmd->sge_count = 1;
  2686. dcmd->flags = MFI_FRAME_DIR_READ;
  2687. dcmd->timeout = 0;
  2688. dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
  2689. dcmd->opcode = MR_DCMD_LD_GET_LIST;
  2690. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2691. dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
  2692. dcmd->pad_0 = 0;
  2693. if (!megasas_issue_polled(instance, cmd)) {
  2694. ret = 0;
  2695. } else {
  2696. ret = -1;
  2697. }
  2698. /* the following function will get the instance PD LIST */
  2699. if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
  2700. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2701. for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
  2702. if (ci->ldList[ld_index].state != 0) {
  2703. ids = ci->ldList[ld_index].ref.targetId;
  2704. instance->ld_ids[ids] =
  2705. ci->ldList[ld_index].ref.targetId;
  2706. }
  2707. }
  2708. }
  2709. pci_free_consistent(instance->pdev,
  2710. sizeof(struct MR_LD_LIST),
  2711. ci,
  2712. ci_h);
  2713. megasas_return_cmd(instance, cmd);
  2714. return ret;
  2715. }
  2716. /**
  2717. * megasas_get_controller_info - Returns FW's controller structure
  2718. * @instance: Adapter soft state
  2719. * @ctrl_info: Controller information structure
  2720. *
  2721. * Issues an internal command (DCMD) to get the FW's controller structure.
  2722. * This information is mainly used to find out the maximum IO transfer per
  2723. * command supported by the FW.
  2724. */
  2725. static int
  2726. megasas_get_ctrl_info(struct megasas_instance *instance,
  2727. struct megasas_ctrl_info *ctrl_info)
  2728. {
  2729. int ret = 0;
  2730. struct megasas_cmd *cmd;
  2731. struct megasas_dcmd_frame *dcmd;
  2732. struct megasas_ctrl_info *ci;
  2733. dma_addr_t ci_h = 0;
  2734. cmd = megasas_get_cmd(instance);
  2735. if (!cmd) {
  2736. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2737. return -ENOMEM;
  2738. }
  2739. dcmd = &cmd->frame->dcmd;
  2740. ci = pci_alloc_consistent(instance->pdev,
  2741. sizeof(struct megasas_ctrl_info), &ci_h);
  2742. if (!ci) {
  2743. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2744. megasas_return_cmd(instance, cmd);
  2745. return -ENOMEM;
  2746. }
  2747. memset(ci, 0, sizeof(*ci));
  2748. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2749. dcmd->cmd = MFI_CMD_DCMD;
  2750. dcmd->cmd_status = 0xFF;
  2751. dcmd->sge_count = 1;
  2752. dcmd->flags = MFI_FRAME_DIR_READ;
  2753. dcmd->timeout = 0;
  2754. dcmd->pad_0 = 0;
  2755. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  2756. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  2757. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2758. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  2759. if (!megasas_issue_polled(instance, cmd)) {
  2760. ret = 0;
  2761. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2762. } else {
  2763. ret = -1;
  2764. }
  2765. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2766. ci, ci_h);
  2767. megasas_return_cmd(instance, cmd);
  2768. return ret;
  2769. }
  2770. /**
  2771. * megasas_issue_init_mfi - Initializes the FW
  2772. * @instance: Adapter soft state
  2773. *
  2774. * Issues the INIT MFI cmd
  2775. */
  2776. static int
  2777. megasas_issue_init_mfi(struct megasas_instance *instance)
  2778. {
  2779. u32 context;
  2780. struct megasas_cmd *cmd;
  2781. struct megasas_init_frame *init_frame;
  2782. struct megasas_init_queue_info *initq_info;
  2783. dma_addr_t init_frame_h;
  2784. dma_addr_t initq_info_h;
  2785. /*
  2786. * Prepare a init frame. Note the init frame points to queue info
  2787. * structure. Each frame has SGL allocated after first 64 bytes. For
  2788. * this frame - since we don't need any SGL - we use SGL's space as
  2789. * queue info structure
  2790. *
  2791. * We will not get a NULL command below. We just created the pool.
  2792. */
  2793. cmd = megasas_get_cmd(instance);
  2794. init_frame = (struct megasas_init_frame *)cmd->frame;
  2795. initq_info = (struct megasas_init_queue_info *)
  2796. ((unsigned long)init_frame + 64);
  2797. init_frame_h = cmd->frame_phys_addr;
  2798. initq_info_h = init_frame_h + 64;
  2799. context = init_frame->context;
  2800. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2801. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2802. init_frame->context = context;
  2803. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2804. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2805. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2806. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2807. init_frame->cmd = MFI_CMD_INIT;
  2808. init_frame->cmd_status = 0xFF;
  2809. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2810. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2811. /*
  2812. * disable the intr before firing the init frame to FW
  2813. */
  2814. instance->instancet->disable_intr(instance->reg_set);
  2815. /*
  2816. * Issue the init frame in polled mode
  2817. */
  2818. if (megasas_issue_polled(instance, cmd)) {
  2819. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2820. megasas_return_cmd(instance, cmd);
  2821. goto fail_fw_init;
  2822. }
  2823. megasas_return_cmd(instance, cmd);
  2824. return 0;
  2825. fail_fw_init:
  2826. return -EINVAL;
  2827. }
  2828. /**
  2829. * megasas_start_timer - Initializes a timer object
  2830. * @instance: Adapter soft state
  2831. * @timer: timer object to be initialized
  2832. * @fn: timer function
  2833. * @interval: time interval between timer function call
  2834. */
  2835. static inline void
  2836. megasas_start_timer(struct megasas_instance *instance,
  2837. struct timer_list *timer,
  2838. void *fn, unsigned long interval)
  2839. {
  2840. init_timer(timer);
  2841. timer->expires = jiffies + interval;
  2842. timer->data = (unsigned long)instance;
  2843. timer->function = fn;
  2844. add_timer(timer);
  2845. }
  2846. /**
  2847. * megasas_io_completion_timer - Timer fn
  2848. * @instance_addr: Address of adapter soft state
  2849. *
  2850. * Schedules tasklet for cmd completion
  2851. * if poll_mode_io is set
  2852. */
  2853. static void
  2854. megasas_io_completion_timer(unsigned long instance_addr)
  2855. {
  2856. struct megasas_instance *instance =
  2857. (struct megasas_instance *)instance_addr;
  2858. if (atomic_read(&instance->fw_outstanding))
  2859. tasklet_schedule(&instance->isr_tasklet);
  2860. /* Restart timer */
  2861. if (poll_mode_io)
  2862. mod_timer(&instance->io_completion_timer,
  2863. jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
  2864. }
  2865. static u32
  2866. megasas_init_adapter_mfi(struct megasas_instance *instance)
  2867. {
  2868. struct megasas_register_set __iomem *reg_set;
  2869. u32 context_sz;
  2870. u32 reply_q_sz;
  2871. reg_set = instance->reg_set;
  2872. /*
  2873. * Get various operational parameters from status register
  2874. */
  2875. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2876. /*
  2877. * Reduce the max supported cmds by 1. This is to ensure that the
  2878. * reply_q_sz (1 more than the max cmd that driver may send)
  2879. * does not exceed max cmds that the FW can support
  2880. */
  2881. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2882. instance->max_mfi_cmds = instance->max_fw_cmds;
  2883. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2884. 0x10;
  2885. /*
  2886. * Create a pool of commands
  2887. */
  2888. if (megasas_alloc_cmds(instance))
  2889. goto fail_alloc_cmds;
  2890. /*
  2891. * Allocate memory for reply queue. Length of reply queue should
  2892. * be _one_ more than the maximum commands handled by the firmware.
  2893. *
  2894. * Note: When FW completes commands, it places corresponding contex
  2895. * values in this circular reply queue. This circular queue is a fairly
  2896. * typical producer-consumer queue. FW is the producer (of completed
  2897. * commands) and the driver is the consumer.
  2898. */
  2899. context_sz = sizeof(u32);
  2900. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2901. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2902. reply_q_sz,
  2903. &instance->reply_queue_h);
  2904. if (!instance->reply_queue) {
  2905. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2906. goto fail_reply_queue;
  2907. }
  2908. if (megasas_issue_init_mfi(instance))
  2909. goto fail_fw_init;
  2910. instance->fw_support_ieee = 0;
  2911. instance->fw_support_ieee =
  2912. (instance->instancet->read_fw_status_reg(reg_set) &
  2913. 0x04000000);
  2914. printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
  2915. instance->fw_support_ieee);
  2916. if (instance->fw_support_ieee)
  2917. instance->flag_ieee = 1;
  2918. return 0;
  2919. fail_fw_init:
  2920. pci_free_consistent(instance->pdev, reply_q_sz,
  2921. instance->reply_queue, instance->reply_queue_h);
  2922. fail_reply_queue:
  2923. megasas_free_cmds(instance);
  2924. fail_alloc_cmds:
  2925. return 1;
  2926. }
  2927. /**
  2928. * megasas_init_fw - Initializes the FW
  2929. * @instance: Adapter soft state
  2930. *
  2931. * This is the main function for initializing firmware
  2932. */
  2933. static int megasas_init_fw(struct megasas_instance *instance)
  2934. {
  2935. u32 max_sectors_1;
  2936. u32 max_sectors_2;
  2937. u32 tmp_sectors, msix_enable;
  2938. struct megasas_register_set __iomem *reg_set;
  2939. struct megasas_ctrl_info *ctrl_info;
  2940. unsigned long bar_list;
  2941. /* Find first memory bar */
  2942. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  2943. instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
  2944. instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
  2945. if (pci_request_selected_regions(instance->pdev, instance->bar,
  2946. "megasas: LSI")) {
  2947. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2948. return -EBUSY;
  2949. }
  2950. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2951. if (!instance->reg_set) {
  2952. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2953. goto fail_ioremap;
  2954. }
  2955. reg_set = instance->reg_set;
  2956. switch (instance->pdev->device) {
  2957. case PCI_DEVICE_ID_LSI_FUSION:
  2958. instance->instancet = &megasas_instance_template_fusion;
  2959. break;
  2960. case PCI_DEVICE_ID_LSI_SAS1078R:
  2961. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2962. instance->instancet = &megasas_instance_template_ppc;
  2963. break;
  2964. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2965. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2966. instance->instancet = &megasas_instance_template_gen2;
  2967. break;
  2968. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2969. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2970. instance->instancet = &megasas_instance_template_skinny;
  2971. break;
  2972. case PCI_DEVICE_ID_LSI_SAS1064R:
  2973. case PCI_DEVICE_ID_DELL_PERC5:
  2974. default:
  2975. instance->instancet = &megasas_instance_template_xscale;
  2976. break;
  2977. }
  2978. /*
  2979. * We expect the FW state to be READY
  2980. */
  2981. if (megasas_transition_to_ready(instance))
  2982. goto fail_ready_state;
  2983. /* Check if MSI-X is supported while in ready state */
  2984. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  2985. 0x4000000) >> 0x1a;
  2986. if (msix_enable && !msix_disable &&
  2987. !pci_enable_msix(instance->pdev, &instance->msixentry, 1))
  2988. instance->msi_flag = 1;
  2989. /* Get operational params, sge flags, send init cmd to controller */
  2990. if (instance->instancet->init_adapter(instance))
  2991. goto fail_init_adapter;
  2992. printk(KERN_ERR "megasas: INIT adapter done\n");
  2993. /** for passthrough
  2994. * the following function will get the PD LIST.
  2995. */
  2996. memset(instance->pd_list, 0 ,
  2997. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  2998. megasas_get_pd_list(instance);
  2999. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3000. megasas_get_ld_list(instance);
  3001. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  3002. /*
  3003. * Compute the max allowed sectors per IO: The controller info has two
  3004. * limits on max sectors. Driver should use the minimum of these two.
  3005. *
  3006. * 1 << stripe_sz_ops.min = max sectors per strip
  3007. *
  3008. * Note that older firmwares ( < FW ver 30) didn't report information
  3009. * to calculate max_sectors_1. So the number ended up as zero always.
  3010. */
  3011. tmp_sectors = 0;
  3012. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  3013. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  3014. ctrl_info->max_strips_per_io;
  3015. max_sectors_2 = ctrl_info->max_request_size;
  3016. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  3017. instance->disableOnlineCtrlReset =
  3018. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3019. }
  3020. instance->max_sectors_per_req = instance->max_num_sge *
  3021. PAGE_SIZE / 512;
  3022. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  3023. instance->max_sectors_per_req = tmp_sectors;
  3024. kfree(ctrl_info);
  3025. /*
  3026. * Setup tasklet for cmd completion
  3027. */
  3028. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3029. (unsigned long)instance);
  3030. /* Initialize the cmd completion timer */
  3031. if (poll_mode_io)
  3032. megasas_start_timer(instance, &instance->io_completion_timer,
  3033. megasas_io_completion_timer,
  3034. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3035. return 0;
  3036. fail_init_adapter:
  3037. fail_ready_state:
  3038. iounmap(instance->reg_set);
  3039. fail_ioremap:
  3040. pci_release_selected_regions(instance->pdev, instance->bar);
  3041. return -EINVAL;
  3042. }
  3043. /**
  3044. * megasas_release_mfi - Reverses the FW initialization
  3045. * @intance: Adapter soft state
  3046. */
  3047. static void megasas_release_mfi(struct megasas_instance *instance)
  3048. {
  3049. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  3050. if (instance->reply_queue)
  3051. pci_free_consistent(instance->pdev, reply_q_sz,
  3052. instance->reply_queue, instance->reply_queue_h);
  3053. megasas_free_cmds(instance);
  3054. iounmap(instance->reg_set);
  3055. pci_release_selected_regions(instance->pdev, instance->bar);
  3056. }
  3057. /**
  3058. * megasas_get_seq_num - Gets latest event sequence numbers
  3059. * @instance: Adapter soft state
  3060. * @eli: FW event log sequence numbers information
  3061. *
  3062. * FW maintains a log of all events in a non-volatile area. Upper layers would
  3063. * usually find out the latest sequence number of the events, the seq number at
  3064. * the boot etc. They would "read" all the events below the latest seq number
  3065. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  3066. * number), they would subsribe to AEN (asynchronous event notification) and
  3067. * wait for the events to happen.
  3068. */
  3069. static int
  3070. megasas_get_seq_num(struct megasas_instance *instance,
  3071. struct megasas_evt_log_info *eli)
  3072. {
  3073. struct megasas_cmd *cmd;
  3074. struct megasas_dcmd_frame *dcmd;
  3075. struct megasas_evt_log_info *el_info;
  3076. dma_addr_t el_info_h = 0;
  3077. cmd = megasas_get_cmd(instance);
  3078. if (!cmd) {
  3079. return -ENOMEM;
  3080. }
  3081. dcmd = &cmd->frame->dcmd;
  3082. el_info = pci_alloc_consistent(instance->pdev,
  3083. sizeof(struct megasas_evt_log_info),
  3084. &el_info_h);
  3085. if (!el_info) {
  3086. megasas_return_cmd(instance, cmd);
  3087. return -ENOMEM;
  3088. }
  3089. memset(el_info, 0, sizeof(*el_info));
  3090. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3091. dcmd->cmd = MFI_CMD_DCMD;
  3092. dcmd->cmd_status = 0x0;
  3093. dcmd->sge_count = 1;
  3094. dcmd->flags = MFI_FRAME_DIR_READ;
  3095. dcmd->timeout = 0;
  3096. dcmd->pad_0 = 0;
  3097. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  3098. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  3099. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  3100. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  3101. megasas_issue_blocked_cmd(instance, cmd);
  3102. /*
  3103. * Copy the data back into callers buffer
  3104. */
  3105. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  3106. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  3107. el_info, el_info_h);
  3108. megasas_return_cmd(instance, cmd);
  3109. return 0;
  3110. }
  3111. /**
  3112. * megasas_register_aen - Registers for asynchronous event notification
  3113. * @instance: Adapter soft state
  3114. * @seq_num: The starting sequence number
  3115. * @class_locale: Class of the event
  3116. *
  3117. * This function subscribes for AEN for events beyond the @seq_num. It requests
  3118. * to be notified if and only if the event is of type @class_locale
  3119. */
  3120. static int
  3121. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  3122. u32 class_locale_word)
  3123. {
  3124. int ret_val;
  3125. struct megasas_cmd *cmd;
  3126. struct megasas_dcmd_frame *dcmd;
  3127. union megasas_evt_class_locale curr_aen;
  3128. union megasas_evt_class_locale prev_aen;
  3129. /*
  3130. * If there an AEN pending already (aen_cmd), check if the
  3131. * class_locale of that pending AEN is inclusive of the new
  3132. * AEN request we currently have. If it is, then we don't have
  3133. * to do anything. In other words, whichever events the current
  3134. * AEN request is subscribing to, have already been subscribed
  3135. * to.
  3136. *
  3137. * If the old_cmd is _not_ inclusive, then we have to abort
  3138. * that command, form a class_locale that is superset of both
  3139. * old and current and re-issue to the FW
  3140. */
  3141. curr_aen.word = class_locale_word;
  3142. if (instance->aen_cmd) {
  3143. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  3144. /*
  3145. * A class whose enum value is smaller is inclusive of all
  3146. * higher values. If a PROGRESS (= -1) was previously
  3147. * registered, then a new registration requests for higher
  3148. * classes need not be sent to FW. They are automatically
  3149. * included.
  3150. *
  3151. * Locale numbers don't have such hierarchy. They are bitmap
  3152. * values
  3153. */
  3154. if ((prev_aen.members.class <= curr_aen.members.class) &&
  3155. !((prev_aen.members.locale & curr_aen.members.locale) ^
  3156. curr_aen.members.locale)) {
  3157. /*
  3158. * Previously issued event registration includes
  3159. * current request. Nothing to do.
  3160. */
  3161. return 0;
  3162. } else {
  3163. curr_aen.members.locale |= prev_aen.members.locale;
  3164. if (prev_aen.members.class < curr_aen.members.class)
  3165. curr_aen.members.class = prev_aen.members.class;
  3166. instance->aen_cmd->abort_aen = 1;
  3167. ret_val = megasas_issue_blocked_abort_cmd(instance,
  3168. instance->
  3169. aen_cmd);
  3170. if (ret_val) {
  3171. printk(KERN_DEBUG "megasas: Failed to abort "
  3172. "previous AEN command\n");
  3173. return ret_val;
  3174. }
  3175. }
  3176. }
  3177. cmd = megasas_get_cmd(instance);
  3178. if (!cmd)
  3179. return -ENOMEM;
  3180. dcmd = &cmd->frame->dcmd;
  3181. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  3182. /*
  3183. * Prepare DCMD for aen registration
  3184. */
  3185. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3186. dcmd->cmd = MFI_CMD_DCMD;
  3187. dcmd->cmd_status = 0x0;
  3188. dcmd->sge_count = 1;
  3189. dcmd->flags = MFI_FRAME_DIR_READ;
  3190. dcmd->timeout = 0;
  3191. dcmd->pad_0 = 0;
  3192. instance->last_seq_num = seq_num;
  3193. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  3194. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  3195. dcmd->mbox.w[0] = seq_num;
  3196. dcmd->mbox.w[1] = curr_aen.word;
  3197. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  3198. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  3199. if (instance->aen_cmd != NULL) {
  3200. megasas_return_cmd(instance, cmd);
  3201. return 0;
  3202. }
  3203. /*
  3204. * Store reference to the cmd used to register for AEN. When an
  3205. * application wants us to register for AEN, we have to abort this
  3206. * cmd and re-register with a new EVENT LOCALE supplied by that app
  3207. */
  3208. instance->aen_cmd = cmd;
  3209. /*
  3210. * Issue the aen registration frame
  3211. */
  3212. instance->instancet->issue_dcmd(instance, cmd);
  3213. return 0;
  3214. }
  3215. /**
  3216. * megasas_start_aen - Subscribes to AEN during driver load time
  3217. * @instance: Adapter soft state
  3218. */
  3219. static int megasas_start_aen(struct megasas_instance *instance)
  3220. {
  3221. struct megasas_evt_log_info eli;
  3222. union megasas_evt_class_locale class_locale;
  3223. /*
  3224. * Get the latest sequence number from FW
  3225. */
  3226. memset(&eli, 0, sizeof(eli));
  3227. if (megasas_get_seq_num(instance, &eli))
  3228. return -1;
  3229. /*
  3230. * Register AEN with FW for latest sequence number plus 1
  3231. */
  3232. class_locale.members.reserved = 0;
  3233. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3234. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3235. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  3236. class_locale.word);
  3237. }
  3238. /**
  3239. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  3240. * @instance: Adapter soft state
  3241. */
  3242. static int megasas_io_attach(struct megasas_instance *instance)
  3243. {
  3244. struct Scsi_Host *host = instance->host;
  3245. /*
  3246. * Export parameters required by SCSI mid-layer
  3247. */
  3248. host->irq = instance->pdev->irq;
  3249. host->unique_id = instance->unique_id;
  3250. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3251. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3252. host->can_queue =
  3253. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  3254. } else
  3255. host->can_queue =
  3256. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  3257. host->this_id = instance->init_id;
  3258. host->sg_tablesize = instance->max_num_sge;
  3259. if (instance->fw_support_ieee)
  3260. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  3261. /*
  3262. * Check if the module parameter value for max_sectors can be used
  3263. */
  3264. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  3265. instance->max_sectors_per_req = max_sectors;
  3266. else {
  3267. if (max_sectors) {
  3268. if (((instance->pdev->device ==
  3269. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  3270. (instance->pdev->device ==
  3271. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  3272. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  3273. instance->max_sectors_per_req = max_sectors;
  3274. } else {
  3275. printk(KERN_INFO "megasas: max_sectors should be > 0"
  3276. "and <= %d (or < 1MB for GEN2 controller)\n",
  3277. instance->max_sectors_per_req);
  3278. }
  3279. }
  3280. }
  3281. host->max_sectors = instance->max_sectors_per_req;
  3282. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  3283. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  3284. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  3285. host->max_lun = MEGASAS_MAX_LUN;
  3286. host->max_cmd_len = 16;
  3287. /* Fusion only supports host reset */
  3288. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
  3289. host->hostt->eh_device_reset_handler = NULL;
  3290. host->hostt->eh_bus_reset_handler = NULL;
  3291. }
  3292. /*
  3293. * Notify the mid-layer about the new controller
  3294. */
  3295. if (scsi_add_host(host, &instance->pdev->dev)) {
  3296. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  3297. return -ENODEV;
  3298. }
  3299. /*
  3300. * Trigger SCSI to scan our drives
  3301. */
  3302. scsi_scan_host(host);
  3303. return 0;
  3304. }
  3305. static int
  3306. megasas_set_dma_mask(struct pci_dev *pdev)
  3307. {
  3308. /*
  3309. * All our contollers are capable of performing 64-bit DMA
  3310. */
  3311. if (IS_DMA64) {
  3312. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  3313. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3314. goto fail_set_dma_mask;
  3315. }
  3316. } else {
  3317. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3318. goto fail_set_dma_mask;
  3319. }
  3320. return 0;
  3321. fail_set_dma_mask:
  3322. return 1;
  3323. }
  3324. /**
  3325. * megasas_probe_one - PCI hotplug entry point
  3326. * @pdev: PCI device structure
  3327. * @id: PCI ids of supported hotplugged adapter
  3328. */
  3329. static int __devinit
  3330. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3331. {
  3332. int rval, pos;
  3333. struct Scsi_Host *host;
  3334. struct megasas_instance *instance;
  3335. u16 control = 0;
  3336. /* Reset MSI-X in the kdump kernel */
  3337. if (reset_devices) {
  3338. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3339. if (pos) {
  3340. pci_read_config_word(pdev, msi_control_reg(pos),
  3341. &control);
  3342. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3343. dev_info(&pdev->dev, "resetting MSI-X\n");
  3344. pci_write_config_word(pdev,
  3345. msi_control_reg(pos),
  3346. control &
  3347. ~PCI_MSIX_FLAGS_ENABLE);
  3348. }
  3349. }
  3350. }
  3351. /*
  3352. * Announce PCI information
  3353. */
  3354. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  3355. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  3356. pdev->subsystem_device);
  3357. printk("bus %d:slot %d:func %d\n",
  3358. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  3359. /*
  3360. * PCI prepping: enable device set bus mastering and dma mask
  3361. */
  3362. rval = pci_enable_device_mem(pdev);
  3363. if (rval) {
  3364. return rval;
  3365. }
  3366. pci_set_master(pdev);
  3367. if (megasas_set_dma_mask(pdev))
  3368. goto fail_set_dma_mask;
  3369. host = scsi_host_alloc(&megasas_template,
  3370. sizeof(struct megasas_instance));
  3371. if (!host) {
  3372. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  3373. goto fail_alloc_instance;
  3374. }
  3375. instance = (struct megasas_instance *)host->hostdata;
  3376. memset(instance, 0, sizeof(*instance));
  3377. atomic_set( &instance->fw_reset_no_pci_access, 0 );
  3378. instance->pdev = pdev;
  3379. switch (instance->pdev->device) {
  3380. case PCI_DEVICE_ID_LSI_FUSION:
  3381. {
  3382. struct fusion_context *fusion;
  3383. instance->ctrl_context =
  3384. kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
  3385. if (!instance->ctrl_context) {
  3386. printk(KERN_DEBUG "megasas: Failed to allocate "
  3387. "memory for Fusion context info\n");
  3388. goto fail_alloc_dma_buf;
  3389. }
  3390. fusion = instance->ctrl_context;
  3391. INIT_LIST_HEAD(&fusion->cmd_pool);
  3392. spin_lock_init(&fusion->cmd_pool_lock);
  3393. }
  3394. break;
  3395. default: /* For all other supported controllers */
  3396. instance->producer =
  3397. pci_alloc_consistent(pdev, sizeof(u32),
  3398. &instance->producer_h);
  3399. instance->consumer =
  3400. pci_alloc_consistent(pdev, sizeof(u32),
  3401. &instance->consumer_h);
  3402. if (!instance->producer || !instance->consumer) {
  3403. printk(KERN_DEBUG "megasas: Failed to allocate"
  3404. "memory for producer, consumer\n");
  3405. goto fail_alloc_dma_buf;
  3406. }
  3407. *instance->producer = 0;
  3408. *instance->consumer = 0;
  3409. break;
  3410. }
  3411. megasas_poll_wait_aen = 0;
  3412. instance->flag_ieee = 0;
  3413. instance->ev = NULL;
  3414. instance->issuepend_done = 1;
  3415. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  3416. megasas_poll_wait_aen = 0;
  3417. instance->evt_detail = pci_alloc_consistent(pdev,
  3418. sizeof(struct
  3419. megasas_evt_detail),
  3420. &instance->evt_detail_h);
  3421. if (!instance->evt_detail) {
  3422. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  3423. "event detail structure\n");
  3424. goto fail_alloc_dma_buf;
  3425. }
  3426. /*
  3427. * Initialize locks and queues
  3428. */
  3429. INIT_LIST_HEAD(&instance->cmd_pool);
  3430. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  3431. atomic_set(&instance->fw_outstanding,0);
  3432. init_waitqueue_head(&instance->int_cmd_wait_q);
  3433. init_waitqueue_head(&instance->abort_cmd_wait_q);
  3434. spin_lock_init(&instance->cmd_pool_lock);
  3435. spin_lock_init(&instance->hba_lock);
  3436. spin_lock_init(&instance->completion_lock);
  3437. spin_lock_init(&poll_aen_lock);
  3438. mutex_init(&instance->aen_mutex);
  3439. mutex_init(&instance->reset_mutex);
  3440. /*
  3441. * Initialize PCI related and misc parameters
  3442. */
  3443. instance->host = host;
  3444. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  3445. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  3446. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3447. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3448. instance->flag_ieee = 1;
  3449. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  3450. } else
  3451. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  3452. megasas_dbg_lvl = 0;
  3453. instance->flag = 0;
  3454. instance->unload = 1;
  3455. instance->last_time = 0;
  3456. instance->disableOnlineCtrlReset = 1;
  3457. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  3458. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  3459. else
  3460. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  3461. /*
  3462. * Initialize MFI Firmware
  3463. */
  3464. if (megasas_init_fw(instance))
  3465. goto fail_init_mfi;
  3466. /*
  3467. * Register IRQ
  3468. */
  3469. if (request_irq(instance->msi_flag ? instance->msixentry.vector :
  3470. pdev->irq, instance->instancet->service_isr,
  3471. IRQF_SHARED, "megasas", instance)) {
  3472. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3473. goto fail_irq;
  3474. }
  3475. instance->instancet->enable_intr(instance->reg_set);
  3476. /*
  3477. * Store instance in PCI softstate
  3478. */
  3479. pci_set_drvdata(pdev, instance);
  3480. /*
  3481. * Add this controller to megasas_mgmt_info structure so that it
  3482. * can be exported to management applications
  3483. */
  3484. megasas_mgmt_info.count++;
  3485. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  3486. megasas_mgmt_info.max_index++;
  3487. /*
  3488. * Register with SCSI mid-layer
  3489. */
  3490. if (megasas_io_attach(instance))
  3491. goto fail_io_attach;
  3492. instance->unload = 0;
  3493. /*
  3494. * Initiate AEN (Asynchronous Event Notification)
  3495. */
  3496. if (megasas_start_aen(instance)) {
  3497. printk(KERN_DEBUG "megasas: start aen failed\n");
  3498. goto fail_start_aen;
  3499. }
  3500. return 0;
  3501. fail_start_aen:
  3502. fail_io_attach:
  3503. megasas_mgmt_info.count--;
  3504. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  3505. megasas_mgmt_info.max_index--;
  3506. pci_set_drvdata(pdev, NULL);
  3507. instance->instancet->disable_intr(instance->reg_set);
  3508. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3509. instance->pdev->irq, instance);
  3510. fail_irq:
  3511. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  3512. megasas_release_fusion(instance);
  3513. else
  3514. megasas_release_mfi(instance);
  3515. fail_init_mfi:
  3516. if (instance->msi_flag)
  3517. pci_disable_msix(instance->pdev);
  3518. fail_alloc_dma_buf:
  3519. if (instance->evt_detail)
  3520. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3521. instance->evt_detail,
  3522. instance->evt_detail_h);
  3523. if (instance->producer)
  3524. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3525. instance->producer_h);
  3526. if (instance->consumer)
  3527. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3528. instance->consumer_h);
  3529. scsi_host_put(host);
  3530. fail_alloc_instance:
  3531. fail_set_dma_mask:
  3532. pci_disable_device(pdev);
  3533. return -ENODEV;
  3534. }
  3535. /**
  3536. * megasas_flush_cache - Requests FW to flush all its caches
  3537. * @instance: Adapter soft state
  3538. */
  3539. static void megasas_flush_cache(struct megasas_instance *instance)
  3540. {
  3541. struct megasas_cmd *cmd;
  3542. struct megasas_dcmd_frame *dcmd;
  3543. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3544. return;
  3545. cmd = megasas_get_cmd(instance);
  3546. if (!cmd)
  3547. return;
  3548. dcmd = &cmd->frame->dcmd;
  3549. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3550. dcmd->cmd = MFI_CMD_DCMD;
  3551. dcmd->cmd_status = 0x0;
  3552. dcmd->sge_count = 0;
  3553. dcmd->flags = MFI_FRAME_DIR_NONE;
  3554. dcmd->timeout = 0;
  3555. dcmd->pad_0 = 0;
  3556. dcmd->data_xfer_len = 0;
  3557. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  3558. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  3559. megasas_issue_blocked_cmd(instance, cmd);
  3560. megasas_return_cmd(instance, cmd);
  3561. return;
  3562. }
  3563. /**
  3564. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  3565. * @instance: Adapter soft state
  3566. * @opcode: Shutdown/Hibernate
  3567. */
  3568. static void megasas_shutdown_controller(struct megasas_instance *instance,
  3569. u32 opcode)
  3570. {
  3571. struct megasas_cmd *cmd;
  3572. struct megasas_dcmd_frame *dcmd;
  3573. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3574. return;
  3575. cmd = megasas_get_cmd(instance);
  3576. if (!cmd)
  3577. return;
  3578. if (instance->aen_cmd)
  3579. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  3580. if (instance->map_update_cmd)
  3581. megasas_issue_blocked_abort_cmd(instance,
  3582. instance->map_update_cmd);
  3583. dcmd = &cmd->frame->dcmd;
  3584. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3585. dcmd->cmd = MFI_CMD_DCMD;
  3586. dcmd->cmd_status = 0x0;
  3587. dcmd->sge_count = 0;
  3588. dcmd->flags = MFI_FRAME_DIR_NONE;
  3589. dcmd->timeout = 0;
  3590. dcmd->pad_0 = 0;
  3591. dcmd->data_xfer_len = 0;
  3592. dcmd->opcode = opcode;
  3593. megasas_issue_blocked_cmd(instance, cmd);
  3594. megasas_return_cmd(instance, cmd);
  3595. return;
  3596. }
  3597. #ifdef CONFIG_PM
  3598. /**
  3599. * megasas_suspend - driver suspend entry point
  3600. * @pdev: PCI device structure
  3601. * @state: PCI power state to suspend routine
  3602. */
  3603. static int
  3604. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  3605. {
  3606. struct Scsi_Host *host;
  3607. struct megasas_instance *instance;
  3608. instance = pci_get_drvdata(pdev);
  3609. host = instance->host;
  3610. instance->unload = 1;
  3611. if (poll_mode_io)
  3612. del_timer_sync(&instance->io_completion_timer);
  3613. megasas_flush_cache(instance);
  3614. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  3615. /* cancel the delayed work if this work still in queue */
  3616. if (instance->ev != NULL) {
  3617. struct megasas_aen_event *ev = instance->ev;
  3618. cancel_delayed_work_sync(
  3619. (struct delayed_work *)&ev->hotplug_work);
  3620. instance->ev = NULL;
  3621. }
  3622. tasklet_kill(&instance->isr_tasklet);
  3623. pci_set_drvdata(instance->pdev, instance);
  3624. instance->instancet->disable_intr(instance->reg_set);
  3625. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3626. instance->pdev->irq, instance);
  3627. if (instance->msi_flag)
  3628. pci_disable_msix(instance->pdev);
  3629. pci_save_state(pdev);
  3630. pci_disable_device(pdev);
  3631. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3632. return 0;
  3633. }
  3634. /**
  3635. * megasas_resume- driver resume entry point
  3636. * @pdev: PCI device structure
  3637. */
  3638. static int
  3639. megasas_resume(struct pci_dev *pdev)
  3640. {
  3641. int rval;
  3642. struct Scsi_Host *host;
  3643. struct megasas_instance *instance;
  3644. instance = pci_get_drvdata(pdev);
  3645. host = instance->host;
  3646. pci_set_power_state(pdev, PCI_D0);
  3647. pci_enable_wake(pdev, PCI_D0, 0);
  3648. pci_restore_state(pdev);
  3649. /*
  3650. * PCI prepping: enable device set bus mastering and dma mask
  3651. */
  3652. rval = pci_enable_device_mem(pdev);
  3653. if (rval) {
  3654. printk(KERN_ERR "megasas: Enable device failed\n");
  3655. return rval;
  3656. }
  3657. pci_set_master(pdev);
  3658. if (megasas_set_dma_mask(pdev))
  3659. goto fail_set_dma_mask;
  3660. /*
  3661. * Initialize MFI Firmware
  3662. */
  3663. atomic_set(&instance->fw_outstanding, 0);
  3664. /*
  3665. * We expect the FW state to be READY
  3666. */
  3667. if (megasas_transition_to_ready(instance))
  3668. goto fail_ready_state;
  3669. /* Now re-enable MSI-X */
  3670. if (instance->msi_flag)
  3671. pci_enable_msix(instance->pdev, &instance->msixentry, 1);
  3672. switch (instance->pdev->device) {
  3673. case PCI_DEVICE_ID_LSI_FUSION:
  3674. {
  3675. megasas_reset_reply_desc(instance);
  3676. if (megasas_ioc_init_fusion(instance)) {
  3677. megasas_free_cmds(instance);
  3678. megasas_free_cmds_fusion(instance);
  3679. goto fail_init_mfi;
  3680. }
  3681. if (!megasas_get_map_info(instance))
  3682. megasas_sync_map_info(instance);
  3683. }
  3684. break;
  3685. default:
  3686. *instance->producer = 0;
  3687. *instance->consumer = 0;
  3688. if (megasas_issue_init_mfi(instance))
  3689. goto fail_init_mfi;
  3690. break;
  3691. }
  3692. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3693. (unsigned long)instance);
  3694. /*
  3695. * Register IRQ
  3696. */
  3697. if (request_irq(instance->msi_flag ? instance->msixentry.vector :
  3698. pdev->irq, instance->instancet->service_isr,
  3699. IRQF_SHARED, "megasas", instance)) {
  3700. printk(KERN_ERR "megasas: Failed to register IRQ\n");
  3701. goto fail_irq;
  3702. }
  3703. instance->instancet->enable_intr(instance->reg_set);
  3704. /* Initialize the cmd completion timer */
  3705. if (poll_mode_io)
  3706. megasas_start_timer(instance, &instance->io_completion_timer,
  3707. megasas_io_completion_timer,
  3708. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3709. instance->unload = 0;
  3710. /*
  3711. * Initiate AEN (Asynchronous Event Notification)
  3712. */
  3713. if (megasas_start_aen(instance))
  3714. printk(KERN_ERR "megasas: Start AEN failed\n");
  3715. return 0;
  3716. fail_irq:
  3717. fail_init_mfi:
  3718. if (instance->evt_detail)
  3719. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3720. instance->evt_detail,
  3721. instance->evt_detail_h);
  3722. if (instance->producer)
  3723. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3724. instance->producer_h);
  3725. if (instance->consumer)
  3726. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3727. instance->consumer_h);
  3728. scsi_host_put(host);
  3729. fail_set_dma_mask:
  3730. fail_ready_state:
  3731. pci_disable_device(pdev);
  3732. return -ENODEV;
  3733. }
  3734. #else
  3735. #define megasas_suspend NULL
  3736. #define megasas_resume NULL
  3737. #endif
  3738. /**
  3739. * megasas_detach_one - PCI hot"un"plug entry point
  3740. * @pdev: PCI device structure
  3741. */
  3742. static void __devexit megasas_detach_one(struct pci_dev *pdev)
  3743. {
  3744. int i;
  3745. struct Scsi_Host *host;
  3746. struct megasas_instance *instance;
  3747. struct fusion_context *fusion;
  3748. instance = pci_get_drvdata(pdev);
  3749. instance->unload = 1;
  3750. host = instance->host;
  3751. fusion = instance->ctrl_context;
  3752. if (poll_mode_io)
  3753. del_timer_sync(&instance->io_completion_timer);
  3754. scsi_remove_host(instance->host);
  3755. megasas_flush_cache(instance);
  3756. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3757. /* cancel the delayed work if this work still in queue*/
  3758. if (instance->ev != NULL) {
  3759. struct megasas_aen_event *ev = instance->ev;
  3760. cancel_delayed_work_sync(
  3761. (struct delayed_work *)&ev->hotplug_work);
  3762. instance->ev = NULL;
  3763. }
  3764. tasklet_kill(&instance->isr_tasklet);
  3765. /*
  3766. * Take the instance off the instance array. Note that we will not
  3767. * decrement the max_index. We let this array be sparse array
  3768. */
  3769. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3770. if (megasas_mgmt_info.instance[i] == instance) {
  3771. megasas_mgmt_info.count--;
  3772. megasas_mgmt_info.instance[i] = NULL;
  3773. break;
  3774. }
  3775. }
  3776. pci_set_drvdata(instance->pdev, NULL);
  3777. instance->instancet->disable_intr(instance->reg_set);
  3778. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3779. instance->pdev->irq, instance);
  3780. if (instance->msi_flag)
  3781. pci_disable_msix(instance->pdev);
  3782. switch (instance->pdev->device) {
  3783. case PCI_DEVICE_ID_LSI_FUSION:
  3784. megasas_release_fusion(instance);
  3785. for (i = 0; i < 2 ; i++)
  3786. if (fusion->ld_map[i])
  3787. dma_free_coherent(&instance->pdev->dev,
  3788. fusion->map_sz,
  3789. fusion->ld_map[i],
  3790. fusion->
  3791. ld_map_phys[i]);
  3792. kfree(instance->ctrl_context);
  3793. break;
  3794. default:
  3795. megasas_release_mfi(instance);
  3796. pci_free_consistent(pdev,
  3797. sizeof(struct megasas_evt_detail),
  3798. instance->evt_detail,
  3799. instance->evt_detail_h);
  3800. pci_free_consistent(pdev, sizeof(u32),
  3801. instance->producer,
  3802. instance->producer_h);
  3803. pci_free_consistent(pdev, sizeof(u32),
  3804. instance->consumer,
  3805. instance->consumer_h);
  3806. break;
  3807. }
  3808. scsi_host_put(host);
  3809. pci_set_drvdata(pdev, NULL);
  3810. pci_disable_device(pdev);
  3811. return;
  3812. }
  3813. /**
  3814. * megasas_shutdown - Shutdown entry point
  3815. * @device: Generic device structure
  3816. */
  3817. static void megasas_shutdown(struct pci_dev *pdev)
  3818. {
  3819. struct megasas_instance *instance = pci_get_drvdata(pdev);
  3820. instance->unload = 1;
  3821. megasas_flush_cache(instance);
  3822. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3823. instance->instancet->disable_intr(instance->reg_set);
  3824. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3825. instance->pdev->irq, instance);
  3826. if (instance->msi_flag)
  3827. pci_disable_msix(instance->pdev);
  3828. }
  3829. /**
  3830. * megasas_mgmt_open - char node "open" entry point
  3831. */
  3832. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  3833. {
  3834. /*
  3835. * Allow only those users with admin rights
  3836. */
  3837. if (!capable(CAP_SYS_ADMIN))
  3838. return -EACCES;
  3839. return 0;
  3840. }
  3841. /**
  3842. * megasas_mgmt_fasync - Async notifier registration from applications
  3843. *
  3844. * This function adds the calling process to a driver global queue. When an
  3845. * event occurs, SIGIO will be sent to all processes in this queue.
  3846. */
  3847. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  3848. {
  3849. int rc;
  3850. mutex_lock(&megasas_async_queue_mutex);
  3851. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  3852. mutex_unlock(&megasas_async_queue_mutex);
  3853. if (rc >= 0) {
  3854. /* For sanity check when we get ioctl */
  3855. filep->private_data = filep;
  3856. return 0;
  3857. }
  3858. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  3859. return rc;
  3860. }
  3861. /**
  3862. * megasas_mgmt_poll - char node "poll" entry point
  3863. * */
  3864. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  3865. {
  3866. unsigned int mask;
  3867. unsigned long flags;
  3868. poll_wait(file, &megasas_poll_wait, wait);
  3869. spin_lock_irqsave(&poll_aen_lock, flags);
  3870. if (megasas_poll_wait_aen)
  3871. mask = (POLLIN | POLLRDNORM);
  3872. else
  3873. mask = 0;
  3874. spin_unlock_irqrestore(&poll_aen_lock, flags);
  3875. return mask;
  3876. }
  3877. /**
  3878. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  3879. * @instance: Adapter soft state
  3880. * @argp: User's ioctl packet
  3881. */
  3882. static int
  3883. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  3884. struct megasas_iocpacket __user * user_ioc,
  3885. struct megasas_iocpacket *ioc)
  3886. {
  3887. struct megasas_sge32 *kern_sge32;
  3888. struct megasas_cmd *cmd;
  3889. void *kbuff_arr[MAX_IOCTL_SGE];
  3890. dma_addr_t buf_handle = 0;
  3891. int error = 0, i;
  3892. void *sense = NULL;
  3893. dma_addr_t sense_handle;
  3894. unsigned long *sense_ptr;
  3895. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  3896. if (ioc->sge_count > MAX_IOCTL_SGE) {
  3897. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  3898. ioc->sge_count, MAX_IOCTL_SGE);
  3899. return -EINVAL;
  3900. }
  3901. cmd = megasas_get_cmd(instance);
  3902. if (!cmd) {
  3903. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  3904. return -ENOMEM;
  3905. }
  3906. /*
  3907. * User's IOCTL packet has 2 frames (maximum). Copy those two
  3908. * frames into our cmd's frames. cmd->frame's context will get
  3909. * overwritten when we copy from user's frames. So set that value
  3910. * alone separately
  3911. */
  3912. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  3913. cmd->frame->hdr.context = cmd->index;
  3914. cmd->frame->hdr.pad_0 = 0;
  3915. /*
  3916. * The management interface between applications and the fw uses
  3917. * MFI frames. E.g, RAID configuration changes, LD property changes
  3918. * etc are accomplishes through different kinds of MFI frames. The
  3919. * driver needs to care only about substituting user buffers with
  3920. * kernel buffers in SGLs. The location of SGL is embedded in the
  3921. * struct iocpacket itself.
  3922. */
  3923. kern_sge32 = (struct megasas_sge32 *)
  3924. ((unsigned long)cmd->frame + ioc->sgl_off);
  3925. /*
  3926. * For each user buffer, create a mirror buffer and copy in
  3927. */
  3928. for (i = 0; i < ioc->sge_count; i++) {
  3929. if (!ioc->sgl[i].iov_len)
  3930. continue;
  3931. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  3932. ioc->sgl[i].iov_len,
  3933. &buf_handle, GFP_KERNEL);
  3934. if (!kbuff_arr[i]) {
  3935. printk(KERN_DEBUG "megasas: Failed to alloc "
  3936. "kernel SGL buffer for IOCTL \n");
  3937. error = -ENOMEM;
  3938. goto out;
  3939. }
  3940. /*
  3941. * We don't change the dma_coherent_mask, so
  3942. * pci_alloc_consistent only returns 32bit addresses
  3943. */
  3944. kern_sge32[i].phys_addr = (u32) buf_handle;
  3945. kern_sge32[i].length = ioc->sgl[i].iov_len;
  3946. /*
  3947. * We created a kernel buffer corresponding to the
  3948. * user buffer. Now copy in from the user buffer
  3949. */
  3950. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  3951. (u32) (ioc->sgl[i].iov_len))) {
  3952. error = -EFAULT;
  3953. goto out;
  3954. }
  3955. }
  3956. if (ioc->sense_len) {
  3957. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  3958. &sense_handle, GFP_KERNEL);
  3959. if (!sense) {
  3960. error = -ENOMEM;
  3961. goto out;
  3962. }
  3963. sense_ptr =
  3964. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  3965. *sense_ptr = sense_handle;
  3966. }
  3967. /*
  3968. * Set the sync_cmd flag so that the ISR knows not to complete this
  3969. * cmd to the SCSI mid-layer
  3970. */
  3971. cmd->sync_cmd = 1;
  3972. megasas_issue_blocked_cmd(instance, cmd);
  3973. cmd->sync_cmd = 0;
  3974. /*
  3975. * copy out the kernel buffers to user buffers
  3976. */
  3977. for (i = 0; i < ioc->sge_count; i++) {
  3978. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  3979. ioc->sgl[i].iov_len)) {
  3980. error = -EFAULT;
  3981. goto out;
  3982. }
  3983. }
  3984. /*
  3985. * copy out the sense
  3986. */
  3987. if (ioc->sense_len) {
  3988. /*
  3989. * sense_ptr points to the location that has the user
  3990. * sense buffer address
  3991. */
  3992. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  3993. ioc->sense_off);
  3994. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  3995. sense, ioc->sense_len)) {
  3996. printk(KERN_ERR "megasas: Failed to copy out to user "
  3997. "sense data\n");
  3998. error = -EFAULT;
  3999. goto out;
  4000. }
  4001. }
  4002. /*
  4003. * copy the status codes returned by the fw
  4004. */
  4005. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  4006. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  4007. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  4008. error = -EFAULT;
  4009. }
  4010. out:
  4011. if (sense) {
  4012. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  4013. sense, sense_handle);
  4014. }
  4015. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  4016. dma_free_coherent(&instance->pdev->dev,
  4017. kern_sge32[i].length,
  4018. kbuff_arr[i], kern_sge32[i].phys_addr);
  4019. }
  4020. megasas_return_cmd(instance, cmd);
  4021. return error;
  4022. }
  4023. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  4024. {
  4025. struct megasas_iocpacket __user *user_ioc =
  4026. (struct megasas_iocpacket __user *)arg;
  4027. struct megasas_iocpacket *ioc;
  4028. struct megasas_instance *instance;
  4029. int error;
  4030. int i;
  4031. unsigned long flags;
  4032. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4033. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  4034. if (!ioc)
  4035. return -ENOMEM;
  4036. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  4037. error = -EFAULT;
  4038. goto out_kfree_ioc;
  4039. }
  4040. instance = megasas_lookup_instance(ioc->host_no);
  4041. if (!instance) {
  4042. error = -ENODEV;
  4043. goto out_kfree_ioc;
  4044. }
  4045. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4046. printk(KERN_ERR "Controller in crit error\n");
  4047. error = -ENODEV;
  4048. goto out_kfree_ioc;
  4049. }
  4050. if (instance->unload == 1) {
  4051. error = -ENODEV;
  4052. goto out_kfree_ioc;
  4053. }
  4054. /*
  4055. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  4056. */
  4057. if (down_interruptible(&instance->ioctl_sem)) {
  4058. error = -ERESTARTSYS;
  4059. goto out_kfree_ioc;
  4060. }
  4061. for (i = 0; i < wait_time; i++) {
  4062. spin_lock_irqsave(&instance->hba_lock, flags);
  4063. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4064. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4065. break;
  4066. }
  4067. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4068. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4069. printk(KERN_NOTICE "megasas: waiting"
  4070. "for controller reset to finish\n");
  4071. }
  4072. msleep(1000);
  4073. }
  4074. spin_lock_irqsave(&instance->hba_lock, flags);
  4075. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4076. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4077. printk(KERN_ERR "megaraid_sas: timed out while"
  4078. "waiting for HBA to recover\n");
  4079. error = -ENODEV;
  4080. goto out_kfree_ioc;
  4081. }
  4082. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4083. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  4084. up(&instance->ioctl_sem);
  4085. out_kfree_ioc:
  4086. kfree(ioc);
  4087. return error;
  4088. }
  4089. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  4090. {
  4091. struct megasas_instance *instance;
  4092. struct megasas_aen aen;
  4093. int error;
  4094. int i;
  4095. unsigned long flags;
  4096. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4097. if (file->private_data != file) {
  4098. printk(KERN_DEBUG "megasas: fasync_helper was not "
  4099. "called first\n");
  4100. return -EINVAL;
  4101. }
  4102. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  4103. return -EFAULT;
  4104. instance = megasas_lookup_instance(aen.host_no);
  4105. if (!instance)
  4106. return -ENODEV;
  4107. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4108. return -ENODEV;
  4109. }
  4110. if (instance->unload == 1) {
  4111. return -ENODEV;
  4112. }
  4113. for (i = 0; i < wait_time; i++) {
  4114. spin_lock_irqsave(&instance->hba_lock, flags);
  4115. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4116. spin_unlock_irqrestore(&instance->hba_lock,
  4117. flags);
  4118. break;
  4119. }
  4120. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4121. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4122. printk(KERN_NOTICE "megasas: waiting for"
  4123. "controller reset to finish\n");
  4124. }
  4125. msleep(1000);
  4126. }
  4127. spin_lock_irqsave(&instance->hba_lock, flags);
  4128. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4129. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4130. printk(KERN_ERR "megaraid_sas: timed out while waiting"
  4131. "for HBA to recover.\n");
  4132. return -ENODEV;
  4133. }
  4134. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4135. mutex_lock(&instance->aen_mutex);
  4136. error = megasas_register_aen(instance, aen.seq_num,
  4137. aen.class_locale_word);
  4138. mutex_unlock(&instance->aen_mutex);
  4139. return error;
  4140. }
  4141. /**
  4142. * megasas_mgmt_ioctl - char node ioctl entry point
  4143. */
  4144. static long
  4145. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4146. {
  4147. switch (cmd) {
  4148. case MEGASAS_IOC_FIRMWARE:
  4149. return megasas_mgmt_ioctl_fw(file, arg);
  4150. case MEGASAS_IOC_GET_AEN:
  4151. return megasas_mgmt_ioctl_aen(file, arg);
  4152. }
  4153. return -ENOTTY;
  4154. }
  4155. #ifdef CONFIG_COMPAT
  4156. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  4157. {
  4158. struct compat_megasas_iocpacket __user *cioc =
  4159. (struct compat_megasas_iocpacket __user *)arg;
  4160. struct megasas_iocpacket __user *ioc =
  4161. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  4162. int i;
  4163. int error = 0;
  4164. compat_uptr_t ptr;
  4165. if (clear_user(ioc, sizeof(*ioc)))
  4166. return -EFAULT;
  4167. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  4168. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  4169. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  4170. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  4171. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  4172. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  4173. return -EFAULT;
  4174. /*
  4175. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  4176. * sense_len is not null, so prepare the 64bit value under
  4177. * the same condition.
  4178. */
  4179. if (ioc->sense_len) {
  4180. void __user **sense_ioc_ptr =
  4181. (void __user **)(ioc->frame.raw + ioc->sense_off);
  4182. compat_uptr_t *sense_cioc_ptr =
  4183. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  4184. if (get_user(ptr, sense_cioc_ptr) ||
  4185. put_user(compat_ptr(ptr), sense_ioc_ptr))
  4186. return -EFAULT;
  4187. }
  4188. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  4189. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  4190. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  4191. copy_in_user(&ioc->sgl[i].iov_len,
  4192. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  4193. return -EFAULT;
  4194. }
  4195. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  4196. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  4197. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  4198. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  4199. return -EFAULT;
  4200. }
  4201. return error;
  4202. }
  4203. static long
  4204. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  4205. unsigned long arg)
  4206. {
  4207. switch (cmd) {
  4208. case MEGASAS_IOC_FIRMWARE32:
  4209. return megasas_mgmt_compat_ioctl_fw(file, arg);
  4210. case MEGASAS_IOC_GET_AEN:
  4211. return megasas_mgmt_ioctl_aen(file, arg);
  4212. }
  4213. return -ENOTTY;
  4214. }
  4215. #endif
  4216. /*
  4217. * File operations structure for management interface
  4218. */
  4219. static const struct file_operations megasas_mgmt_fops = {
  4220. .owner = THIS_MODULE,
  4221. .open = megasas_mgmt_open,
  4222. .fasync = megasas_mgmt_fasync,
  4223. .unlocked_ioctl = megasas_mgmt_ioctl,
  4224. .poll = megasas_mgmt_poll,
  4225. #ifdef CONFIG_COMPAT
  4226. .compat_ioctl = megasas_mgmt_compat_ioctl,
  4227. #endif
  4228. .llseek = noop_llseek,
  4229. };
  4230. /*
  4231. * PCI hotplug support registration structure
  4232. */
  4233. static struct pci_driver megasas_pci_driver = {
  4234. .name = "megaraid_sas",
  4235. .id_table = megasas_pci_table,
  4236. .probe = megasas_probe_one,
  4237. .remove = __devexit_p(megasas_detach_one),
  4238. .suspend = megasas_suspend,
  4239. .resume = megasas_resume,
  4240. .shutdown = megasas_shutdown,
  4241. };
  4242. /*
  4243. * Sysfs driver attributes
  4244. */
  4245. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  4246. {
  4247. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  4248. MEGASAS_VERSION);
  4249. }
  4250. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  4251. static ssize_t
  4252. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  4253. {
  4254. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  4255. MEGASAS_RELDATE);
  4256. }
  4257. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  4258. NULL);
  4259. static ssize_t
  4260. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  4261. {
  4262. return sprintf(buf, "%u\n", support_poll_for_event);
  4263. }
  4264. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  4265. megasas_sysfs_show_support_poll_for_event, NULL);
  4266. static ssize_t
  4267. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  4268. {
  4269. return sprintf(buf, "%u\n", support_device_change);
  4270. }
  4271. static DRIVER_ATTR(support_device_change, S_IRUGO,
  4272. megasas_sysfs_show_support_device_change, NULL);
  4273. static ssize_t
  4274. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  4275. {
  4276. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  4277. }
  4278. static ssize_t
  4279. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  4280. {
  4281. int retval = count;
  4282. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  4283. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  4284. retval = -EINVAL;
  4285. }
  4286. return retval;
  4287. }
  4288. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  4289. megasas_sysfs_set_dbg_lvl);
  4290. static ssize_t
  4291. megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
  4292. {
  4293. return sprintf(buf, "%u\n", poll_mode_io);
  4294. }
  4295. static ssize_t
  4296. megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
  4297. const char *buf, size_t count)
  4298. {
  4299. int retval = count;
  4300. int tmp = poll_mode_io;
  4301. int i;
  4302. struct megasas_instance *instance;
  4303. if (sscanf(buf, "%u", &poll_mode_io) < 1) {
  4304. printk(KERN_ERR "megasas: could not set poll_mode_io\n");
  4305. retval = -EINVAL;
  4306. }
  4307. /*
  4308. * Check if poll_mode_io is already set or is same as previous value
  4309. */
  4310. if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
  4311. goto out;
  4312. if (poll_mode_io) {
  4313. /*
  4314. * Start timers for all adapters
  4315. */
  4316. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  4317. instance = megasas_mgmt_info.instance[i];
  4318. if (instance) {
  4319. megasas_start_timer(instance,
  4320. &instance->io_completion_timer,
  4321. megasas_io_completion_timer,
  4322. MEGASAS_COMPLETION_TIMER_INTERVAL);
  4323. }
  4324. }
  4325. } else {
  4326. /*
  4327. * Delete timers for all adapters
  4328. */
  4329. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  4330. instance = megasas_mgmt_info.instance[i];
  4331. if (instance)
  4332. del_timer_sync(&instance->io_completion_timer);
  4333. }
  4334. }
  4335. out:
  4336. return retval;
  4337. }
  4338. static void
  4339. megasas_aen_polling(struct work_struct *work)
  4340. {
  4341. struct megasas_aen_event *ev =
  4342. container_of(work, struct megasas_aen_event, hotplug_work);
  4343. struct megasas_instance *instance = ev->instance;
  4344. union megasas_evt_class_locale class_locale;
  4345. struct Scsi_Host *host;
  4346. struct scsi_device *sdev1;
  4347. u16 pd_index = 0;
  4348. u16 ld_index = 0;
  4349. int i, j, doscan = 0;
  4350. u32 seq_num;
  4351. int error;
  4352. if (!instance) {
  4353. printk(KERN_ERR "invalid instance!\n");
  4354. kfree(ev);
  4355. return;
  4356. }
  4357. instance->ev = NULL;
  4358. host = instance->host;
  4359. if (instance->evt_detail) {
  4360. switch (instance->evt_detail->code) {
  4361. case MR_EVT_PD_INSERTED:
  4362. if (megasas_get_pd_list(instance) == 0) {
  4363. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4364. for (j = 0;
  4365. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4366. j++) {
  4367. pd_index =
  4368. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4369. sdev1 =
  4370. scsi_device_lookup(host, i, j, 0);
  4371. if (instance->pd_list[pd_index].driveState
  4372. == MR_PD_STATE_SYSTEM) {
  4373. if (!sdev1) {
  4374. scsi_add_device(host, i, j, 0);
  4375. }
  4376. if (sdev1)
  4377. scsi_device_put(sdev1);
  4378. }
  4379. }
  4380. }
  4381. }
  4382. doscan = 0;
  4383. break;
  4384. case MR_EVT_PD_REMOVED:
  4385. if (megasas_get_pd_list(instance) == 0) {
  4386. megasas_get_pd_list(instance);
  4387. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4388. for (j = 0;
  4389. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4390. j++) {
  4391. pd_index =
  4392. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4393. sdev1 =
  4394. scsi_device_lookup(host, i, j, 0);
  4395. if (instance->pd_list[pd_index].driveState
  4396. == MR_PD_STATE_SYSTEM) {
  4397. if (sdev1) {
  4398. scsi_device_put(sdev1);
  4399. }
  4400. } else {
  4401. if (sdev1) {
  4402. scsi_remove_device(sdev1);
  4403. scsi_device_put(sdev1);
  4404. }
  4405. }
  4406. }
  4407. }
  4408. }
  4409. doscan = 0;
  4410. break;
  4411. case MR_EVT_LD_OFFLINE:
  4412. case MR_EVT_CFG_CLEARED:
  4413. case MR_EVT_LD_DELETED:
  4414. megasas_get_ld_list(instance);
  4415. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4416. for (j = 0;
  4417. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4418. j++) {
  4419. ld_index =
  4420. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4421. sdev1 = scsi_device_lookup(host,
  4422. i + MEGASAS_MAX_LD_CHANNELS,
  4423. j,
  4424. 0);
  4425. if (instance->ld_ids[ld_index] != 0xff) {
  4426. if (sdev1) {
  4427. scsi_device_put(sdev1);
  4428. }
  4429. } else {
  4430. if (sdev1) {
  4431. scsi_remove_device(sdev1);
  4432. scsi_device_put(sdev1);
  4433. }
  4434. }
  4435. }
  4436. }
  4437. doscan = 0;
  4438. break;
  4439. case MR_EVT_LD_CREATED:
  4440. megasas_get_ld_list(instance);
  4441. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4442. for (j = 0;
  4443. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4444. j++) {
  4445. ld_index =
  4446. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4447. sdev1 = scsi_device_lookup(host,
  4448. i+MEGASAS_MAX_LD_CHANNELS,
  4449. j, 0);
  4450. if (instance->ld_ids[ld_index] !=
  4451. 0xff) {
  4452. if (!sdev1) {
  4453. scsi_add_device(host,
  4454. i + 2,
  4455. j, 0);
  4456. }
  4457. }
  4458. if (sdev1) {
  4459. scsi_device_put(sdev1);
  4460. }
  4461. }
  4462. }
  4463. doscan = 0;
  4464. break;
  4465. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  4466. case MR_EVT_FOREIGN_CFG_IMPORTED:
  4467. case MR_EVT_LD_STATE_CHANGE:
  4468. doscan = 1;
  4469. break;
  4470. default:
  4471. doscan = 0;
  4472. break;
  4473. }
  4474. } else {
  4475. printk(KERN_ERR "invalid evt_detail!\n");
  4476. kfree(ev);
  4477. return;
  4478. }
  4479. if (doscan) {
  4480. printk(KERN_INFO "scanning ...\n");
  4481. megasas_get_pd_list(instance);
  4482. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4483. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4484. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  4485. sdev1 = scsi_device_lookup(host, i, j, 0);
  4486. if (instance->pd_list[pd_index].driveState ==
  4487. MR_PD_STATE_SYSTEM) {
  4488. if (!sdev1) {
  4489. scsi_add_device(host, i, j, 0);
  4490. }
  4491. if (sdev1)
  4492. scsi_device_put(sdev1);
  4493. } else {
  4494. if (sdev1) {
  4495. scsi_remove_device(sdev1);
  4496. scsi_device_put(sdev1);
  4497. }
  4498. }
  4499. }
  4500. }
  4501. megasas_get_ld_list(instance);
  4502. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4503. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4504. ld_index =
  4505. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4506. sdev1 = scsi_device_lookup(host,
  4507. i+MEGASAS_MAX_LD_CHANNELS, j, 0);
  4508. if (instance->ld_ids[ld_index] != 0xff) {
  4509. if (!sdev1) {
  4510. scsi_add_device(host,
  4511. i+2,
  4512. j, 0);
  4513. } else {
  4514. scsi_device_put(sdev1);
  4515. }
  4516. } else {
  4517. if (sdev1) {
  4518. scsi_remove_device(sdev1);
  4519. scsi_device_put(sdev1);
  4520. }
  4521. }
  4522. }
  4523. }
  4524. }
  4525. if ( instance->aen_cmd != NULL ) {
  4526. kfree(ev);
  4527. return ;
  4528. }
  4529. seq_num = instance->evt_detail->seq_num + 1;
  4530. /* Register AEN with FW for latest sequence number plus 1 */
  4531. class_locale.members.reserved = 0;
  4532. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4533. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4534. mutex_lock(&instance->aen_mutex);
  4535. error = megasas_register_aen(instance, seq_num,
  4536. class_locale.word);
  4537. mutex_unlock(&instance->aen_mutex);
  4538. if (error)
  4539. printk(KERN_ERR "register aen failed error %x\n", error);
  4540. kfree(ev);
  4541. }
  4542. static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
  4543. megasas_sysfs_show_poll_mode_io,
  4544. megasas_sysfs_set_poll_mode_io);
  4545. /**
  4546. * megasas_init - Driver load entry point
  4547. */
  4548. static int __init megasas_init(void)
  4549. {
  4550. int rval;
  4551. /*
  4552. * Announce driver version and other information
  4553. */
  4554. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  4555. MEGASAS_EXT_VERSION);
  4556. support_poll_for_event = 2;
  4557. support_device_change = 1;
  4558. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  4559. /*
  4560. * Register character device node
  4561. */
  4562. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  4563. if (rval < 0) {
  4564. printk(KERN_DEBUG "megasas: failed to open device node\n");
  4565. return rval;
  4566. }
  4567. megasas_mgmt_majorno = rval;
  4568. /*
  4569. * Register ourselves as PCI hotplug module
  4570. */
  4571. rval = pci_register_driver(&megasas_pci_driver);
  4572. if (rval) {
  4573. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  4574. goto err_pcidrv;
  4575. }
  4576. rval = driver_create_file(&megasas_pci_driver.driver,
  4577. &driver_attr_version);
  4578. if (rval)
  4579. goto err_dcf_attr_ver;
  4580. rval = driver_create_file(&megasas_pci_driver.driver,
  4581. &driver_attr_release_date);
  4582. if (rval)
  4583. goto err_dcf_rel_date;
  4584. rval = driver_create_file(&megasas_pci_driver.driver,
  4585. &driver_attr_support_poll_for_event);
  4586. if (rval)
  4587. goto err_dcf_support_poll_for_event;
  4588. rval = driver_create_file(&megasas_pci_driver.driver,
  4589. &driver_attr_dbg_lvl);
  4590. if (rval)
  4591. goto err_dcf_dbg_lvl;
  4592. rval = driver_create_file(&megasas_pci_driver.driver,
  4593. &driver_attr_poll_mode_io);
  4594. if (rval)
  4595. goto err_dcf_poll_mode_io;
  4596. rval = driver_create_file(&megasas_pci_driver.driver,
  4597. &driver_attr_support_device_change);
  4598. if (rval)
  4599. goto err_dcf_support_device_change;
  4600. return rval;
  4601. err_dcf_support_device_change:
  4602. driver_remove_file(&megasas_pci_driver.driver,
  4603. &driver_attr_poll_mode_io);
  4604. err_dcf_poll_mode_io:
  4605. driver_remove_file(&megasas_pci_driver.driver,
  4606. &driver_attr_dbg_lvl);
  4607. err_dcf_dbg_lvl:
  4608. driver_remove_file(&megasas_pci_driver.driver,
  4609. &driver_attr_support_poll_for_event);
  4610. err_dcf_support_poll_for_event:
  4611. driver_remove_file(&megasas_pci_driver.driver,
  4612. &driver_attr_release_date);
  4613. err_dcf_rel_date:
  4614. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4615. err_dcf_attr_ver:
  4616. pci_unregister_driver(&megasas_pci_driver);
  4617. err_pcidrv:
  4618. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4619. return rval;
  4620. }
  4621. /**
  4622. * megasas_exit - Driver unload entry point
  4623. */
  4624. static void __exit megasas_exit(void)
  4625. {
  4626. driver_remove_file(&megasas_pci_driver.driver,
  4627. &driver_attr_poll_mode_io);
  4628. driver_remove_file(&megasas_pci_driver.driver,
  4629. &driver_attr_dbg_lvl);
  4630. driver_remove_file(&megasas_pci_driver.driver,
  4631. &driver_attr_support_poll_for_event);
  4632. driver_remove_file(&megasas_pci_driver.driver,
  4633. &driver_attr_support_device_change);
  4634. driver_remove_file(&megasas_pci_driver.driver,
  4635. &driver_attr_release_date);
  4636. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4637. pci_unregister_driver(&megasas_pci_driver);
  4638. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4639. }
  4640. module_init(megasas_init);
  4641. module_exit(megasas_exit);