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.34-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_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
  1650. struct megasas_instance *instance;
  1651. unsigned long flags;
  1652. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1653. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1654. return BLK_EH_NOT_HANDLED;
  1655. }
  1656. instance = cmd->instance;
  1657. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1658. /* FW is busy, throttle IO */
  1659. spin_lock_irqsave(instance->host->host_lock, flags);
  1660. instance->host->can_queue = 16;
  1661. instance->last_time = jiffies;
  1662. instance->flag |= MEGASAS_FW_BUSY;
  1663. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1664. }
  1665. return BLK_EH_RESET_TIMER;
  1666. }
  1667. /**
  1668. * megasas_reset_device - Device reset handler entry point
  1669. */
  1670. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1671. {
  1672. int ret;
  1673. /*
  1674. * First wait for all commands to complete
  1675. */
  1676. ret = megasas_generic_reset(scmd);
  1677. return ret;
  1678. }
  1679. /**
  1680. * megasas_reset_bus_host - Bus & host reset handler entry point
  1681. */
  1682. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1683. {
  1684. int ret;
  1685. struct megasas_instance *instance;
  1686. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1687. /*
  1688. * First wait for all commands to complete
  1689. */
  1690. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  1691. ret = megasas_reset_fusion(scmd->device->host);
  1692. else
  1693. ret = megasas_generic_reset(scmd);
  1694. return ret;
  1695. }
  1696. /**
  1697. * megasas_bios_param - Returns disk geometry for a disk
  1698. * @sdev: device handle
  1699. * @bdev: block device
  1700. * @capacity: drive capacity
  1701. * @geom: geometry parameters
  1702. */
  1703. static int
  1704. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1705. sector_t capacity, int geom[])
  1706. {
  1707. int heads;
  1708. int sectors;
  1709. sector_t cylinders;
  1710. unsigned long tmp;
  1711. /* Default heads (64) & sectors (32) */
  1712. heads = 64;
  1713. sectors = 32;
  1714. tmp = heads * sectors;
  1715. cylinders = capacity;
  1716. sector_div(cylinders, tmp);
  1717. /*
  1718. * Handle extended translation size for logical drives > 1Gb
  1719. */
  1720. if (capacity >= 0x200000) {
  1721. heads = 255;
  1722. sectors = 63;
  1723. tmp = heads*sectors;
  1724. cylinders = capacity;
  1725. sector_div(cylinders, tmp);
  1726. }
  1727. geom[0] = heads;
  1728. geom[1] = sectors;
  1729. geom[2] = cylinders;
  1730. return 0;
  1731. }
  1732. static void megasas_aen_polling(struct work_struct *work);
  1733. /**
  1734. * megasas_service_aen - Processes an event notification
  1735. * @instance: Adapter soft state
  1736. * @cmd: AEN command completed by the ISR
  1737. *
  1738. * For AEN, driver sends a command down to FW that is held by the FW till an
  1739. * event occurs. When an event of interest occurs, FW completes the command
  1740. * that it was previously holding.
  1741. *
  1742. * This routines sends SIGIO signal to processes that have registered with the
  1743. * driver for AEN.
  1744. */
  1745. static void
  1746. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1747. {
  1748. unsigned long flags;
  1749. /*
  1750. * Don't signal app if it is just an aborted previously registered aen
  1751. */
  1752. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1753. spin_lock_irqsave(&poll_aen_lock, flags);
  1754. megasas_poll_wait_aen = 1;
  1755. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1756. wake_up(&megasas_poll_wait);
  1757. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1758. }
  1759. else
  1760. cmd->abort_aen = 0;
  1761. instance->aen_cmd = NULL;
  1762. megasas_return_cmd(instance, cmd);
  1763. if ((instance->unload == 0) &&
  1764. ((instance->issuepend_done == 1))) {
  1765. struct megasas_aen_event *ev;
  1766. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1767. if (!ev) {
  1768. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1769. } else {
  1770. ev->instance = instance;
  1771. instance->ev = ev;
  1772. INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
  1773. schedule_delayed_work(
  1774. (struct delayed_work *)&ev->hotplug_work, 0);
  1775. }
  1776. }
  1777. }
  1778. /*
  1779. * Scsi host template for megaraid_sas driver
  1780. */
  1781. static struct scsi_host_template megasas_template = {
  1782. .module = THIS_MODULE,
  1783. .name = "LSI SAS based MegaRAID driver",
  1784. .proc_name = "megaraid_sas",
  1785. .slave_configure = megasas_slave_configure,
  1786. .slave_alloc = megasas_slave_alloc,
  1787. .queuecommand = megasas_queue_command,
  1788. .eh_device_reset_handler = megasas_reset_device,
  1789. .eh_bus_reset_handler = megasas_reset_bus_host,
  1790. .eh_host_reset_handler = megasas_reset_bus_host,
  1791. .eh_timed_out = megasas_reset_timer,
  1792. .bios_param = megasas_bios_param,
  1793. .use_clustering = ENABLE_CLUSTERING,
  1794. };
  1795. /**
  1796. * megasas_complete_int_cmd - Completes an internal command
  1797. * @instance: Adapter soft state
  1798. * @cmd: Command to be completed
  1799. *
  1800. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1801. * after it issues a command. This function wakes up that waiting routine by
  1802. * calling wake_up() on the wait queue.
  1803. */
  1804. static void
  1805. megasas_complete_int_cmd(struct megasas_instance *instance,
  1806. struct megasas_cmd *cmd)
  1807. {
  1808. cmd->cmd_status = cmd->frame->io.cmd_status;
  1809. if (cmd->cmd_status == ENODATA) {
  1810. cmd->cmd_status = 0;
  1811. }
  1812. wake_up(&instance->int_cmd_wait_q);
  1813. }
  1814. /**
  1815. * megasas_complete_abort - Completes aborting a command
  1816. * @instance: Adapter soft state
  1817. * @cmd: Cmd that was issued to abort another cmd
  1818. *
  1819. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1820. * after it issues an abort on a previously issued command. This function
  1821. * wakes up all functions waiting on the same wait queue.
  1822. */
  1823. static void
  1824. megasas_complete_abort(struct megasas_instance *instance,
  1825. struct megasas_cmd *cmd)
  1826. {
  1827. if (cmd->sync_cmd) {
  1828. cmd->sync_cmd = 0;
  1829. cmd->cmd_status = 0;
  1830. wake_up(&instance->abort_cmd_wait_q);
  1831. }
  1832. return;
  1833. }
  1834. /**
  1835. * megasas_complete_cmd - Completes a command
  1836. * @instance: Adapter soft state
  1837. * @cmd: Command to be completed
  1838. * @alt_status: If non-zero, use this value as status to
  1839. * SCSI mid-layer instead of the value returned
  1840. * by the FW. This should be used if caller wants
  1841. * an alternate status (as in the case of aborted
  1842. * commands)
  1843. */
  1844. void
  1845. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1846. u8 alt_status)
  1847. {
  1848. int exception = 0;
  1849. struct megasas_header *hdr = &cmd->frame->hdr;
  1850. unsigned long flags;
  1851. struct fusion_context *fusion = instance->ctrl_context;
  1852. /* flag for the retry reset */
  1853. cmd->retry_for_fw_reset = 0;
  1854. if (cmd->scmd)
  1855. cmd->scmd->SCp.ptr = NULL;
  1856. switch (hdr->cmd) {
  1857. case MFI_CMD_PD_SCSI_IO:
  1858. case MFI_CMD_LD_SCSI_IO:
  1859. /*
  1860. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1861. * issued either through an IO path or an IOCTL path. If it
  1862. * was via IOCTL, we will send it to internal completion.
  1863. */
  1864. if (cmd->sync_cmd) {
  1865. cmd->sync_cmd = 0;
  1866. megasas_complete_int_cmd(instance, cmd);
  1867. break;
  1868. }
  1869. case MFI_CMD_LD_READ:
  1870. case MFI_CMD_LD_WRITE:
  1871. if (alt_status) {
  1872. cmd->scmd->result = alt_status << 16;
  1873. exception = 1;
  1874. }
  1875. if (exception) {
  1876. atomic_dec(&instance->fw_outstanding);
  1877. scsi_dma_unmap(cmd->scmd);
  1878. cmd->scmd->scsi_done(cmd->scmd);
  1879. megasas_return_cmd(instance, cmd);
  1880. break;
  1881. }
  1882. switch (hdr->cmd_status) {
  1883. case MFI_STAT_OK:
  1884. cmd->scmd->result = DID_OK << 16;
  1885. break;
  1886. case MFI_STAT_SCSI_IO_FAILED:
  1887. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1888. cmd->scmd->result =
  1889. (DID_ERROR << 16) | hdr->scsi_status;
  1890. break;
  1891. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1892. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1893. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1894. memset(cmd->scmd->sense_buffer, 0,
  1895. SCSI_SENSE_BUFFERSIZE);
  1896. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1897. hdr->sense_len);
  1898. cmd->scmd->result |= DRIVER_SENSE << 24;
  1899. }
  1900. break;
  1901. case MFI_STAT_LD_OFFLINE:
  1902. case MFI_STAT_DEVICE_NOT_FOUND:
  1903. cmd->scmd->result = DID_BAD_TARGET << 16;
  1904. break;
  1905. default:
  1906. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1907. hdr->cmd_status);
  1908. cmd->scmd->result = DID_ERROR << 16;
  1909. break;
  1910. }
  1911. atomic_dec(&instance->fw_outstanding);
  1912. scsi_dma_unmap(cmd->scmd);
  1913. cmd->scmd->scsi_done(cmd->scmd);
  1914. megasas_return_cmd(instance, cmd);
  1915. break;
  1916. case MFI_CMD_SMP:
  1917. case MFI_CMD_STP:
  1918. case MFI_CMD_DCMD:
  1919. /* Check for LD map update */
  1920. if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
  1921. (cmd->frame->dcmd.mbox.b[1] == 1)) {
  1922. spin_lock_irqsave(instance->host->host_lock, flags);
  1923. if (cmd->frame->hdr.cmd_status != 0) {
  1924. if (cmd->frame->hdr.cmd_status !=
  1925. MFI_STAT_NOT_FOUND)
  1926. printk(KERN_WARNING "megasas: map sync"
  1927. "failed, status = 0x%x.\n",
  1928. cmd->frame->hdr.cmd_status);
  1929. else {
  1930. megasas_return_cmd(instance, cmd);
  1931. spin_unlock_irqrestore(
  1932. instance->host->host_lock,
  1933. flags);
  1934. break;
  1935. }
  1936. } else
  1937. instance->map_id++;
  1938. megasas_return_cmd(instance, cmd);
  1939. if (MR_ValidateMapInfo(
  1940. fusion->ld_map[(instance->map_id & 1)],
  1941. fusion->load_balance_info))
  1942. fusion->fast_path_io = 1;
  1943. else
  1944. fusion->fast_path_io = 0;
  1945. megasas_sync_map_info(instance);
  1946. spin_unlock_irqrestore(instance->host->host_lock,
  1947. flags);
  1948. break;
  1949. }
  1950. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1951. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1952. spin_lock_irqsave(&poll_aen_lock, flags);
  1953. megasas_poll_wait_aen = 0;
  1954. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1955. }
  1956. /*
  1957. * See if got an event notification
  1958. */
  1959. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1960. megasas_service_aen(instance, cmd);
  1961. else
  1962. megasas_complete_int_cmd(instance, cmd);
  1963. break;
  1964. case MFI_CMD_ABORT:
  1965. /*
  1966. * Cmd issued to abort another cmd returned
  1967. */
  1968. megasas_complete_abort(instance, cmd);
  1969. break;
  1970. default:
  1971. printk("megasas: Unknown command completed! [0x%X]\n",
  1972. hdr->cmd);
  1973. break;
  1974. }
  1975. }
  1976. /**
  1977. * megasas_issue_pending_cmds_again - issue all pending cmds
  1978. * in FW again because of the fw reset
  1979. * @instance: Adapter soft state
  1980. */
  1981. static inline void
  1982. megasas_issue_pending_cmds_again(struct megasas_instance *instance)
  1983. {
  1984. struct megasas_cmd *cmd;
  1985. struct list_head clist_local;
  1986. union megasas_evt_class_locale class_locale;
  1987. unsigned long flags;
  1988. u32 seq_num;
  1989. INIT_LIST_HEAD(&clist_local);
  1990. spin_lock_irqsave(&instance->hba_lock, flags);
  1991. list_splice_init(&instance->internal_reset_pending_q, &clist_local);
  1992. spin_unlock_irqrestore(&instance->hba_lock, flags);
  1993. while (!list_empty(&clist_local)) {
  1994. cmd = list_entry((&clist_local)->next,
  1995. struct megasas_cmd, list);
  1996. list_del_init(&cmd->list);
  1997. if (cmd->sync_cmd || cmd->scmd) {
  1998. printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
  1999. "detected to be pending while HBA reset.\n",
  2000. cmd, cmd->scmd, cmd->sync_cmd);
  2001. cmd->retry_for_fw_reset++;
  2002. if (cmd->retry_for_fw_reset == 3) {
  2003. printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
  2004. "was tried multiple times during reset."
  2005. "Shutting down the HBA\n",
  2006. cmd, cmd->scmd, cmd->sync_cmd);
  2007. megaraid_sas_kill_hba(instance);
  2008. instance->adprecovery =
  2009. MEGASAS_HW_CRITICAL_ERROR;
  2010. return;
  2011. }
  2012. }
  2013. if (cmd->sync_cmd == 1) {
  2014. if (cmd->scmd) {
  2015. printk(KERN_NOTICE "megaraid_sas: unexpected"
  2016. "cmd attached to internal command!\n");
  2017. }
  2018. printk(KERN_NOTICE "megasas: %p synchronous cmd"
  2019. "on the internal reset queue,"
  2020. "issue it again.\n", cmd);
  2021. cmd->cmd_status = ENODATA;
  2022. instance->instancet->fire_cmd(instance,
  2023. cmd->frame_phys_addr ,
  2024. 0, instance->reg_set);
  2025. } else if (cmd->scmd) {
  2026. printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
  2027. "detected on the internal queue, issue again.\n",
  2028. cmd, cmd->scmd->cmnd[0]);
  2029. atomic_inc(&instance->fw_outstanding);
  2030. instance->instancet->fire_cmd(instance,
  2031. cmd->frame_phys_addr,
  2032. cmd->frame_count-1, instance->reg_set);
  2033. } else {
  2034. printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
  2035. "internal reset defer list while re-issue!!\n",
  2036. cmd);
  2037. }
  2038. }
  2039. if (instance->aen_cmd) {
  2040. printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
  2041. megasas_return_cmd(instance, instance->aen_cmd);
  2042. instance->aen_cmd = NULL;
  2043. }
  2044. /*
  2045. * Initiate AEN (Asynchronous Event Notification)
  2046. */
  2047. seq_num = instance->last_seq_num;
  2048. class_locale.members.reserved = 0;
  2049. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2050. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2051. megasas_register_aen(instance, seq_num, class_locale.word);
  2052. }
  2053. /**
  2054. * Move the internal reset pending commands to a deferred queue.
  2055. *
  2056. * We move the commands pending at internal reset time to a
  2057. * pending queue. This queue would be flushed after successful
  2058. * completion of the internal reset sequence. if the internal reset
  2059. * did not complete in time, the kernel reset handler would flush
  2060. * these commands.
  2061. **/
  2062. static void
  2063. megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
  2064. {
  2065. struct megasas_cmd *cmd;
  2066. int i;
  2067. u32 max_cmd = instance->max_fw_cmds;
  2068. u32 defer_index;
  2069. unsigned long flags;
  2070. defer_index = 0;
  2071. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  2072. for (i = 0; i < max_cmd; i++) {
  2073. cmd = instance->cmd_list[i];
  2074. if (cmd->sync_cmd == 1 || cmd->scmd) {
  2075. printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
  2076. "on the defer queue as internal\n",
  2077. defer_index, cmd, cmd->sync_cmd, cmd->scmd);
  2078. if (!list_empty(&cmd->list)) {
  2079. printk(KERN_NOTICE "megaraid_sas: ERROR while"
  2080. " moving this cmd:%p, %d %p, it was"
  2081. "discovered on some list?\n",
  2082. cmd, cmd->sync_cmd, cmd->scmd);
  2083. list_del_init(&cmd->list);
  2084. }
  2085. defer_index++;
  2086. list_add_tail(&cmd->list,
  2087. &instance->internal_reset_pending_q);
  2088. }
  2089. }
  2090. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  2091. }
  2092. static void
  2093. process_fw_state_change_wq(struct work_struct *work)
  2094. {
  2095. struct megasas_instance *instance =
  2096. container_of(work, struct megasas_instance, work_init);
  2097. u32 wait;
  2098. unsigned long flags;
  2099. if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
  2100. printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
  2101. instance->adprecovery);
  2102. return ;
  2103. }
  2104. if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
  2105. printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
  2106. "state, restarting it...\n");
  2107. instance->instancet->disable_intr(instance->reg_set);
  2108. atomic_set(&instance->fw_outstanding, 0);
  2109. atomic_set(&instance->fw_reset_no_pci_access, 1);
  2110. instance->instancet->adp_reset(instance, instance->reg_set);
  2111. atomic_set(&instance->fw_reset_no_pci_access, 0 );
  2112. printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
  2113. "initiating next stage...\n");
  2114. printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
  2115. "state 2 starting...\n");
  2116. /*waitting for about 20 second before start the second init*/
  2117. for (wait = 0; wait < 30; wait++) {
  2118. msleep(1000);
  2119. }
  2120. if (megasas_transition_to_ready(instance)) {
  2121. printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
  2122. megaraid_sas_kill_hba(instance);
  2123. instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
  2124. return ;
  2125. }
  2126. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
  2127. (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
  2128. (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
  2129. ) {
  2130. *instance->consumer = *instance->producer;
  2131. } else {
  2132. *instance->consumer = 0;
  2133. *instance->producer = 0;
  2134. }
  2135. megasas_issue_init_mfi(instance);
  2136. spin_lock_irqsave(&instance->hba_lock, flags);
  2137. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  2138. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2139. instance->instancet->enable_intr(instance->reg_set);
  2140. megasas_issue_pending_cmds_again(instance);
  2141. instance->issuepend_done = 1;
  2142. }
  2143. return ;
  2144. }
  2145. /**
  2146. * megasas_deplete_reply_queue - Processes all completed commands
  2147. * @instance: Adapter soft state
  2148. * @alt_status: Alternate status to be returned to
  2149. * SCSI mid-layer instead of the status
  2150. * returned by the FW
  2151. * Note: this must be called with hba lock held
  2152. */
  2153. static int
  2154. megasas_deplete_reply_queue(struct megasas_instance *instance,
  2155. u8 alt_status)
  2156. {
  2157. u32 mfiStatus;
  2158. u32 fw_state;
  2159. if ((mfiStatus = instance->instancet->check_reset(instance,
  2160. instance->reg_set)) == 1) {
  2161. return IRQ_HANDLED;
  2162. }
  2163. if ((mfiStatus = instance->instancet->clear_intr(
  2164. instance->reg_set)
  2165. ) == 0) {
  2166. /* Hardware may not set outbound_intr_status in MSI-X mode */
  2167. if (!instance->msi_flag)
  2168. return IRQ_NONE;
  2169. }
  2170. instance->mfiStatus = mfiStatus;
  2171. if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
  2172. fw_state = instance->instancet->read_fw_status_reg(
  2173. instance->reg_set) & MFI_STATE_MASK;
  2174. if (fw_state != MFI_STATE_FAULT) {
  2175. printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
  2176. fw_state);
  2177. }
  2178. if ((fw_state == MFI_STATE_FAULT) &&
  2179. (instance->disableOnlineCtrlReset == 0)) {
  2180. printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
  2181. if ((instance->pdev->device ==
  2182. PCI_DEVICE_ID_LSI_SAS1064R) ||
  2183. (instance->pdev->device ==
  2184. PCI_DEVICE_ID_DELL_PERC5) ||
  2185. (instance->pdev->device ==
  2186. PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
  2187. *instance->consumer =
  2188. MEGASAS_ADPRESET_INPROG_SIGN;
  2189. }
  2190. instance->instancet->disable_intr(instance->reg_set);
  2191. instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
  2192. instance->issuepend_done = 0;
  2193. atomic_set(&instance->fw_outstanding, 0);
  2194. megasas_internal_reset_defer_cmds(instance);
  2195. printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
  2196. fw_state, instance->adprecovery);
  2197. schedule_work(&instance->work_init);
  2198. return IRQ_HANDLED;
  2199. } else {
  2200. printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
  2201. fw_state, instance->disableOnlineCtrlReset);
  2202. }
  2203. }
  2204. tasklet_schedule(&instance->isr_tasklet);
  2205. return IRQ_HANDLED;
  2206. }
  2207. /**
  2208. * megasas_isr - isr entry point
  2209. */
  2210. static irqreturn_t megasas_isr(int irq, void *devp)
  2211. {
  2212. struct megasas_instance *instance;
  2213. unsigned long flags;
  2214. irqreturn_t rc;
  2215. if (atomic_read(
  2216. &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
  2217. return IRQ_HANDLED;
  2218. instance = (struct megasas_instance *)devp;
  2219. spin_lock_irqsave(&instance->hba_lock, flags);
  2220. rc = megasas_deplete_reply_queue(instance, DID_OK);
  2221. spin_unlock_irqrestore(&instance->hba_lock, flags);
  2222. return rc;
  2223. }
  2224. /**
  2225. * megasas_transition_to_ready - Move the FW to READY state
  2226. * @instance: Adapter soft state
  2227. *
  2228. * During the initialization, FW passes can potentially be in any one of
  2229. * several possible states. If the FW in operational, waiting-for-handshake
  2230. * states, driver must take steps to bring it to ready state. Otherwise, it
  2231. * has to wait for the ready state.
  2232. */
  2233. int
  2234. megasas_transition_to_ready(struct megasas_instance* instance)
  2235. {
  2236. int i;
  2237. u8 max_wait;
  2238. u32 fw_state;
  2239. u32 cur_state;
  2240. u32 abs_state, curr_abs_state;
  2241. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  2242. if (fw_state != MFI_STATE_READY)
  2243. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  2244. " state\n");
  2245. while (fw_state != MFI_STATE_READY) {
  2246. abs_state =
  2247. instance->instancet->read_fw_status_reg(instance->reg_set);
  2248. switch (fw_state) {
  2249. case MFI_STATE_FAULT:
  2250. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  2251. max_wait = MEGASAS_RESET_WAIT_TIME;
  2252. cur_state = MFI_STATE_FAULT;
  2253. break;
  2254. case MFI_STATE_WAIT_HANDSHAKE:
  2255. /*
  2256. * Set the CLR bit in inbound doorbell
  2257. */
  2258. if ((instance->pdev->device ==
  2259. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2260. (instance->pdev->device ==
  2261. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2262. (instance->pdev->device ==
  2263. PCI_DEVICE_ID_LSI_FUSION)) {
  2264. writel(
  2265. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2266. &instance->reg_set->doorbell);
  2267. } else {
  2268. writel(
  2269. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  2270. &instance->reg_set->inbound_doorbell);
  2271. }
  2272. max_wait = MEGASAS_RESET_WAIT_TIME;
  2273. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  2274. break;
  2275. case MFI_STATE_BOOT_MESSAGE_PENDING:
  2276. if ((instance->pdev->device ==
  2277. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2278. (instance->pdev->device ==
  2279. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2280. (instance->pdev->device ==
  2281. PCI_DEVICE_ID_LSI_FUSION)) {
  2282. writel(MFI_INIT_HOTPLUG,
  2283. &instance->reg_set->doorbell);
  2284. } else
  2285. writel(MFI_INIT_HOTPLUG,
  2286. &instance->reg_set->inbound_doorbell);
  2287. max_wait = MEGASAS_RESET_WAIT_TIME;
  2288. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  2289. break;
  2290. case MFI_STATE_OPERATIONAL:
  2291. /*
  2292. * Bring it to READY state; assuming max wait 10 secs
  2293. */
  2294. instance->instancet->disable_intr(instance->reg_set);
  2295. if ((instance->pdev->device ==
  2296. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2297. (instance->pdev->device ==
  2298. PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2299. (instance->pdev->device
  2300. == PCI_DEVICE_ID_LSI_FUSION)) {
  2301. writel(MFI_RESET_FLAGS,
  2302. &instance->reg_set->doorbell);
  2303. if (instance->pdev->device ==
  2304. PCI_DEVICE_ID_LSI_FUSION) {
  2305. for (i = 0; i < (10 * 1000); i += 20) {
  2306. if (readl(
  2307. &instance->
  2308. reg_set->
  2309. doorbell) & 1)
  2310. msleep(20);
  2311. else
  2312. break;
  2313. }
  2314. }
  2315. } else
  2316. writel(MFI_RESET_FLAGS,
  2317. &instance->reg_set->inbound_doorbell);
  2318. max_wait = MEGASAS_RESET_WAIT_TIME;
  2319. cur_state = MFI_STATE_OPERATIONAL;
  2320. break;
  2321. case MFI_STATE_UNDEFINED:
  2322. /*
  2323. * This state should not last for more than 2 seconds
  2324. */
  2325. max_wait = MEGASAS_RESET_WAIT_TIME;
  2326. cur_state = MFI_STATE_UNDEFINED;
  2327. break;
  2328. case MFI_STATE_BB_INIT:
  2329. max_wait = MEGASAS_RESET_WAIT_TIME;
  2330. cur_state = MFI_STATE_BB_INIT;
  2331. break;
  2332. case MFI_STATE_FW_INIT:
  2333. max_wait = MEGASAS_RESET_WAIT_TIME;
  2334. cur_state = MFI_STATE_FW_INIT;
  2335. break;
  2336. case MFI_STATE_FW_INIT_2:
  2337. max_wait = MEGASAS_RESET_WAIT_TIME;
  2338. cur_state = MFI_STATE_FW_INIT_2;
  2339. break;
  2340. case MFI_STATE_DEVICE_SCAN:
  2341. max_wait = MEGASAS_RESET_WAIT_TIME;
  2342. cur_state = MFI_STATE_DEVICE_SCAN;
  2343. break;
  2344. case MFI_STATE_FLUSH_CACHE:
  2345. max_wait = MEGASAS_RESET_WAIT_TIME;
  2346. cur_state = MFI_STATE_FLUSH_CACHE;
  2347. break;
  2348. default:
  2349. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  2350. fw_state);
  2351. return -ENODEV;
  2352. }
  2353. /*
  2354. * The cur_state should not last for more than max_wait secs
  2355. */
  2356. for (i = 0; i < (max_wait * 1000); i++) {
  2357. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  2358. MFI_STATE_MASK ;
  2359. curr_abs_state =
  2360. instance->instancet->read_fw_status_reg(instance->reg_set);
  2361. if (abs_state == curr_abs_state) {
  2362. msleep(1);
  2363. } else
  2364. break;
  2365. }
  2366. /*
  2367. * Return error if fw_state hasn't changed after max_wait
  2368. */
  2369. if (curr_abs_state == abs_state) {
  2370. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  2371. "in %d secs\n", fw_state, max_wait);
  2372. return -ENODEV;
  2373. }
  2374. }
  2375. printk(KERN_INFO "megasas: FW now in Ready state\n");
  2376. return 0;
  2377. }
  2378. /**
  2379. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  2380. * @instance: Adapter soft state
  2381. */
  2382. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  2383. {
  2384. int i;
  2385. u32 max_cmd = instance->max_mfi_cmds;
  2386. struct megasas_cmd *cmd;
  2387. if (!instance->frame_dma_pool)
  2388. return;
  2389. /*
  2390. * Return all frames to pool
  2391. */
  2392. for (i = 0; i < max_cmd; i++) {
  2393. cmd = instance->cmd_list[i];
  2394. if (cmd->frame)
  2395. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  2396. cmd->frame_phys_addr);
  2397. if (cmd->sense)
  2398. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  2399. cmd->sense_phys_addr);
  2400. }
  2401. /*
  2402. * Now destroy the pool itself
  2403. */
  2404. pci_pool_destroy(instance->frame_dma_pool);
  2405. pci_pool_destroy(instance->sense_dma_pool);
  2406. instance->frame_dma_pool = NULL;
  2407. instance->sense_dma_pool = NULL;
  2408. }
  2409. /**
  2410. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  2411. * @instance: Adapter soft state
  2412. *
  2413. * Each command packet has an embedded DMA memory buffer that is used for
  2414. * filling MFI frame and the SG list that immediately follows the frame. This
  2415. * function creates those DMA memory buffers for each command packet by using
  2416. * PCI pool facility.
  2417. */
  2418. static int megasas_create_frame_pool(struct megasas_instance *instance)
  2419. {
  2420. int i;
  2421. u32 max_cmd;
  2422. u32 sge_sz;
  2423. u32 sgl_sz;
  2424. u32 total_sz;
  2425. u32 frame_count;
  2426. struct megasas_cmd *cmd;
  2427. max_cmd = instance->max_mfi_cmds;
  2428. /*
  2429. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  2430. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  2431. */
  2432. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  2433. sizeof(struct megasas_sge32);
  2434. if (instance->flag_ieee) {
  2435. sge_sz = sizeof(struct megasas_sge_skinny);
  2436. }
  2437. /*
  2438. * Calculated the number of 64byte frames required for SGL
  2439. */
  2440. sgl_sz = sge_sz * instance->max_num_sge;
  2441. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  2442. frame_count = 15;
  2443. /*
  2444. * We need one extra frame for the MFI command
  2445. */
  2446. frame_count++;
  2447. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  2448. /*
  2449. * Use DMA pool facility provided by PCI layer
  2450. */
  2451. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  2452. instance->pdev, total_sz, 64,
  2453. 0);
  2454. if (!instance->frame_dma_pool) {
  2455. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  2456. return -ENOMEM;
  2457. }
  2458. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  2459. instance->pdev, 128, 4, 0);
  2460. if (!instance->sense_dma_pool) {
  2461. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  2462. pci_pool_destroy(instance->frame_dma_pool);
  2463. instance->frame_dma_pool = NULL;
  2464. return -ENOMEM;
  2465. }
  2466. /*
  2467. * Allocate and attach a frame to each of the commands in cmd_list.
  2468. * By making cmd->index as the context instead of the &cmd, we can
  2469. * always use 32bit context regardless of the architecture
  2470. */
  2471. for (i = 0; i < max_cmd; i++) {
  2472. cmd = instance->cmd_list[i];
  2473. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  2474. GFP_KERNEL, &cmd->frame_phys_addr);
  2475. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  2476. GFP_KERNEL, &cmd->sense_phys_addr);
  2477. /*
  2478. * megasas_teardown_frame_pool() takes care of freeing
  2479. * whatever has been allocated
  2480. */
  2481. if (!cmd->frame || !cmd->sense) {
  2482. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  2483. megasas_teardown_frame_pool(instance);
  2484. return -ENOMEM;
  2485. }
  2486. memset(cmd->frame, 0, total_sz);
  2487. cmd->frame->io.context = cmd->index;
  2488. cmd->frame->io.pad_0 = 0;
  2489. }
  2490. return 0;
  2491. }
  2492. /**
  2493. * megasas_free_cmds - Free all the cmds in the free cmd pool
  2494. * @instance: Adapter soft state
  2495. */
  2496. void megasas_free_cmds(struct megasas_instance *instance)
  2497. {
  2498. int i;
  2499. /* First free the MFI frame pool */
  2500. megasas_teardown_frame_pool(instance);
  2501. /* Free all the commands in the cmd_list */
  2502. for (i = 0; i < instance->max_mfi_cmds; i++)
  2503. kfree(instance->cmd_list[i]);
  2504. /* Free the cmd_list buffer itself */
  2505. kfree(instance->cmd_list);
  2506. instance->cmd_list = NULL;
  2507. INIT_LIST_HEAD(&instance->cmd_pool);
  2508. }
  2509. /**
  2510. * megasas_alloc_cmds - Allocates the command packets
  2511. * @instance: Adapter soft state
  2512. *
  2513. * Each command that is issued to the FW, whether IO commands from the OS or
  2514. * internal commands like IOCTLs, are wrapped in local data structure called
  2515. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  2516. * the FW.
  2517. *
  2518. * Each frame has a 32-bit field called context (tag). This context is used
  2519. * to get back the megasas_cmd from the frame when a frame gets completed in
  2520. * the ISR. Typically the address of the megasas_cmd itself would be used as
  2521. * the context. But we wanted to keep the differences between 32 and 64 bit
  2522. * systems to the mininum. We always use 32 bit integers for the context. In
  2523. * this driver, the 32 bit values are the indices into an array cmd_list.
  2524. * This array is used only to look up the megasas_cmd given the context. The
  2525. * free commands themselves are maintained in a linked list called cmd_pool.
  2526. */
  2527. int megasas_alloc_cmds(struct megasas_instance *instance)
  2528. {
  2529. int i;
  2530. int j;
  2531. u32 max_cmd;
  2532. struct megasas_cmd *cmd;
  2533. max_cmd = instance->max_mfi_cmds;
  2534. /*
  2535. * instance->cmd_list is an array of struct megasas_cmd pointers.
  2536. * Allocate the dynamic array first and then allocate individual
  2537. * commands.
  2538. */
  2539. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  2540. if (!instance->cmd_list) {
  2541. printk(KERN_DEBUG "megasas: out of memory\n");
  2542. return -ENOMEM;
  2543. }
  2544. memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
  2545. for (i = 0; i < max_cmd; i++) {
  2546. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  2547. GFP_KERNEL);
  2548. if (!instance->cmd_list[i]) {
  2549. for (j = 0; j < i; j++)
  2550. kfree(instance->cmd_list[j]);
  2551. kfree(instance->cmd_list);
  2552. instance->cmd_list = NULL;
  2553. return -ENOMEM;
  2554. }
  2555. }
  2556. /*
  2557. * Add all the commands to command pool (instance->cmd_pool)
  2558. */
  2559. for (i = 0; i < max_cmd; i++) {
  2560. cmd = instance->cmd_list[i];
  2561. memset(cmd, 0, sizeof(struct megasas_cmd));
  2562. cmd->index = i;
  2563. cmd->scmd = NULL;
  2564. cmd->instance = instance;
  2565. list_add_tail(&cmd->list, &instance->cmd_pool);
  2566. }
  2567. /*
  2568. * Create a frame pool and assign one frame to each cmd
  2569. */
  2570. if (megasas_create_frame_pool(instance)) {
  2571. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  2572. megasas_free_cmds(instance);
  2573. }
  2574. return 0;
  2575. }
  2576. /*
  2577. * megasas_get_pd_list_info - Returns FW's pd_list structure
  2578. * @instance: Adapter soft state
  2579. * @pd_list: pd_list structure
  2580. *
  2581. * Issues an internal command (DCMD) to get the FW's controller PD
  2582. * list structure. This information is mainly used to find out SYSTEM
  2583. * supported by the FW.
  2584. */
  2585. static int
  2586. megasas_get_pd_list(struct megasas_instance *instance)
  2587. {
  2588. int ret = 0, pd_index = 0;
  2589. struct megasas_cmd *cmd;
  2590. struct megasas_dcmd_frame *dcmd;
  2591. struct MR_PD_LIST *ci;
  2592. struct MR_PD_ADDRESS *pd_addr;
  2593. dma_addr_t ci_h = 0;
  2594. cmd = megasas_get_cmd(instance);
  2595. if (!cmd) {
  2596. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  2597. return -ENOMEM;
  2598. }
  2599. dcmd = &cmd->frame->dcmd;
  2600. ci = pci_alloc_consistent(instance->pdev,
  2601. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  2602. if (!ci) {
  2603. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  2604. megasas_return_cmd(instance, cmd);
  2605. return -ENOMEM;
  2606. }
  2607. memset(ci, 0, sizeof(*ci));
  2608. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2609. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  2610. dcmd->mbox.b[1] = 0;
  2611. dcmd->cmd = MFI_CMD_DCMD;
  2612. dcmd->cmd_status = 0xFF;
  2613. dcmd->sge_count = 1;
  2614. dcmd->flags = MFI_FRAME_DIR_READ;
  2615. dcmd->timeout = 0;
  2616. dcmd->pad_0 = 0;
  2617. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2618. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  2619. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2620. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  2621. if (!megasas_issue_polled(instance, cmd)) {
  2622. ret = 0;
  2623. } else {
  2624. ret = -1;
  2625. }
  2626. /*
  2627. * the following function will get the instance PD LIST.
  2628. */
  2629. pd_addr = ci->addr;
  2630. if ( ret == 0 &&
  2631. (ci->count <
  2632. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  2633. memset(instance->pd_list, 0,
  2634. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  2635. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  2636. instance->pd_list[pd_addr->deviceId].tid =
  2637. pd_addr->deviceId;
  2638. instance->pd_list[pd_addr->deviceId].driveType =
  2639. pd_addr->scsiDevType;
  2640. instance->pd_list[pd_addr->deviceId].driveState =
  2641. MR_PD_STATE_SYSTEM;
  2642. pd_addr++;
  2643. }
  2644. }
  2645. pci_free_consistent(instance->pdev,
  2646. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  2647. ci, ci_h);
  2648. megasas_return_cmd(instance, cmd);
  2649. return ret;
  2650. }
  2651. /*
  2652. * megasas_get_ld_list_info - Returns FW's ld_list structure
  2653. * @instance: Adapter soft state
  2654. * @ld_list: ld_list structure
  2655. *
  2656. * Issues an internal command (DCMD) to get the FW's controller PD
  2657. * list structure. This information is mainly used to find out SYSTEM
  2658. * supported by the FW.
  2659. */
  2660. static int
  2661. megasas_get_ld_list(struct megasas_instance *instance)
  2662. {
  2663. int ret = 0, ld_index = 0, ids = 0;
  2664. struct megasas_cmd *cmd;
  2665. struct megasas_dcmd_frame *dcmd;
  2666. struct MR_LD_LIST *ci;
  2667. dma_addr_t ci_h = 0;
  2668. cmd = megasas_get_cmd(instance);
  2669. if (!cmd) {
  2670. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  2671. return -ENOMEM;
  2672. }
  2673. dcmd = &cmd->frame->dcmd;
  2674. ci = pci_alloc_consistent(instance->pdev,
  2675. sizeof(struct MR_LD_LIST),
  2676. &ci_h);
  2677. if (!ci) {
  2678. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  2679. megasas_return_cmd(instance, cmd);
  2680. return -ENOMEM;
  2681. }
  2682. memset(ci, 0, sizeof(*ci));
  2683. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2684. dcmd->cmd = MFI_CMD_DCMD;
  2685. dcmd->cmd_status = 0xFF;
  2686. dcmd->sge_count = 1;
  2687. dcmd->flags = MFI_FRAME_DIR_READ;
  2688. dcmd->timeout = 0;
  2689. dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
  2690. dcmd->opcode = MR_DCMD_LD_GET_LIST;
  2691. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2692. dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
  2693. dcmd->pad_0 = 0;
  2694. if (!megasas_issue_polled(instance, cmd)) {
  2695. ret = 0;
  2696. } else {
  2697. ret = -1;
  2698. }
  2699. /* the following function will get the instance PD LIST */
  2700. if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
  2701. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2702. for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
  2703. if (ci->ldList[ld_index].state != 0) {
  2704. ids = ci->ldList[ld_index].ref.targetId;
  2705. instance->ld_ids[ids] =
  2706. ci->ldList[ld_index].ref.targetId;
  2707. }
  2708. }
  2709. }
  2710. pci_free_consistent(instance->pdev,
  2711. sizeof(struct MR_LD_LIST),
  2712. ci,
  2713. ci_h);
  2714. megasas_return_cmd(instance, cmd);
  2715. return ret;
  2716. }
  2717. /**
  2718. * megasas_get_controller_info - Returns FW's controller structure
  2719. * @instance: Adapter soft state
  2720. * @ctrl_info: Controller information structure
  2721. *
  2722. * Issues an internal command (DCMD) to get the FW's controller structure.
  2723. * This information is mainly used to find out the maximum IO transfer per
  2724. * command supported by the FW.
  2725. */
  2726. static int
  2727. megasas_get_ctrl_info(struct megasas_instance *instance,
  2728. struct megasas_ctrl_info *ctrl_info)
  2729. {
  2730. int ret = 0;
  2731. struct megasas_cmd *cmd;
  2732. struct megasas_dcmd_frame *dcmd;
  2733. struct megasas_ctrl_info *ci;
  2734. dma_addr_t ci_h = 0;
  2735. cmd = megasas_get_cmd(instance);
  2736. if (!cmd) {
  2737. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2738. return -ENOMEM;
  2739. }
  2740. dcmd = &cmd->frame->dcmd;
  2741. ci = pci_alloc_consistent(instance->pdev,
  2742. sizeof(struct megasas_ctrl_info), &ci_h);
  2743. if (!ci) {
  2744. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2745. megasas_return_cmd(instance, cmd);
  2746. return -ENOMEM;
  2747. }
  2748. memset(ci, 0, sizeof(*ci));
  2749. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2750. dcmd->cmd = MFI_CMD_DCMD;
  2751. dcmd->cmd_status = 0xFF;
  2752. dcmd->sge_count = 1;
  2753. dcmd->flags = MFI_FRAME_DIR_READ;
  2754. dcmd->timeout = 0;
  2755. dcmd->pad_0 = 0;
  2756. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  2757. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  2758. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2759. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  2760. if (!megasas_issue_polled(instance, cmd)) {
  2761. ret = 0;
  2762. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2763. } else {
  2764. ret = -1;
  2765. }
  2766. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2767. ci, ci_h);
  2768. megasas_return_cmd(instance, cmd);
  2769. return ret;
  2770. }
  2771. /**
  2772. * megasas_issue_init_mfi - Initializes the FW
  2773. * @instance: Adapter soft state
  2774. *
  2775. * Issues the INIT MFI cmd
  2776. */
  2777. static int
  2778. megasas_issue_init_mfi(struct megasas_instance *instance)
  2779. {
  2780. u32 context;
  2781. struct megasas_cmd *cmd;
  2782. struct megasas_init_frame *init_frame;
  2783. struct megasas_init_queue_info *initq_info;
  2784. dma_addr_t init_frame_h;
  2785. dma_addr_t initq_info_h;
  2786. /*
  2787. * Prepare a init frame. Note the init frame points to queue info
  2788. * structure. Each frame has SGL allocated after first 64 bytes. For
  2789. * this frame - since we don't need any SGL - we use SGL's space as
  2790. * queue info structure
  2791. *
  2792. * We will not get a NULL command below. We just created the pool.
  2793. */
  2794. cmd = megasas_get_cmd(instance);
  2795. init_frame = (struct megasas_init_frame *)cmd->frame;
  2796. initq_info = (struct megasas_init_queue_info *)
  2797. ((unsigned long)init_frame + 64);
  2798. init_frame_h = cmd->frame_phys_addr;
  2799. initq_info_h = init_frame_h + 64;
  2800. context = init_frame->context;
  2801. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2802. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2803. init_frame->context = context;
  2804. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2805. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2806. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2807. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2808. init_frame->cmd = MFI_CMD_INIT;
  2809. init_frame->cmd_status = 0xFF;
  2810. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2811. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2812. /*
  2813. * disable the intr before firing the init frame to FW
  2814. */
  2815. instance->instancet->disable_intr(instance->reg_set);
  2816. /*
  2817. * Issue the init frame in polled mode
  2818. */
  2819. if (megasas_issue_polled(instance, cmd)) {
  2820. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2821. megasas_return_cmd(instance, cmd);
  2822. goto fail_fw_init;
  2823. }
  2824. megasas_return_cmd(instance, cmd);
  2825. return 0;
  2826. fail_fw_init:
  2827. return -EINVAL;
  2828. }
  2829. /**
  2830. * megasas_start_timer - Initializes a timer object
  2831. * @instance: Adapter soft state
  2832. * @timer: timer object to be initialized
  2833. * @fn: timer function
  2834. * @interval: time interval between timer function call
  2835. */
  2836. static inline void
  2837. megasas_start_timer(struct megasas_instance *instance,
  2838. struct timer_list *timer,
  2839. void *fn, unsigned long interval)
  2840. {
  2841. init_timer(timer);
  2842. timer->expires = jiffies + interval;
  2843. timer->data = (unsigned long)instance;
  2844. timer->function = fn;
  2845. add_timer(timer);
  2846. }
  2847. /**
  2848. * megasas_io_completion_timer - Timer fn
  2849. * @instance_addr: Address of adapter soft state
  2850. *
  2851. * Schedules tasklet for cmd completion
  2852. * if poll_mode_io is set
  2853. */
  2854. static void
  2855. megasas_io_completion_timer(unsigned long instance_addr)
  2856. {
  2857. struct megasas_instance *instance =
  2858. (struct megasas_instance *)instance_addr;
  2859. if (atomic_read(&instance->fw_outstanding))
  2860. tasklet_schedule(&instance->isr_tasklet);
  2861. /* Restart timer */
  2862. if (poll_mode_io)
  2863. mod_timer(&instance->io_completion_timer,
  2864. jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
  2865. }
  2866. static u32
  2867. megasas_init_adapter_mfi(struct megasas_instance *instance)
  2868. {
  2869. struct megasas_register_set __iomem *reg_set;
  2870. u32 context_sz;
  2871. u32 reply_q_sz;
  2872. reg_set = instance->reg_set;
  2873. /*
  2874. * Get various operational parameters from status register
  2875. */
  2876. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2877. /*
  2878. * Reduce the max supported cmds by 1. This is to ensure that the
  2879. * reply_q_sz (1 more than the max cmd that driver may send)
  2880. * does not exceed max cmds that the FW can support
  2881. */
  2882. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2883. instance->max_mfi_cmds = instance->max_fw_cmds;
  2884. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2885. 0x10;
  2886. /*
  2887. * Create a pool of commands
  2888. */
  2889. if (megasas_alloc_cmds(instance))
  2890. goto fail_alloc_cmds;
  2891. /*
  2892. * Allocate memory for reply queue. Length of reply queue should
  2893. * be _one_ more than the maximum commands handled by the firmware.
  2894. *
  2895. * Note: When FW completes commands, it places corresponding contex
  2896. * values in this circular reply queue. This circular queue is a fairly
  2897. * typical producer-consumer queue. FW is the producer (of completed
  2898. * commands) and the driver is the consumer.
  2899. */
  2900. context_sz = sizeof(u32);
  2901. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2902. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2903. reply_q_sz,
  2904. &instance->reply_queue_h);
  2905. if (!instance->reply_queue) {
  2906. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2907. goto fail_reply_queue;
  2908. }
  2909. if (megasas_issue_init_mfi(instance))
  2910. goto fail_fw_init;
  2911. instance->fw_support_ieee = 0;
  2912. instance->fw_support_ieee =
  2913. (instance->instancet->read_fw_status_reg(reg_set) &
  2914. 0x04000000);
  2915. printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
  2916. instance->fw_support_ieee);
  2917. if (instance->fw_support_ieee)
  2918. instance->flag_ieee = 1;
  2919. return 0;
  2920. fail_fw_init:
  2921. pci_free_consistent(instance->pdev, reply_q_sz,
  2922. instance->reply_queue, instance->reply_queue_h);
  2923. fail_reply_queue:
  2924. megasas_free_cmds(instance);
  2925. fail_alloc_cmds:
  2926. return 1;
  2927. }
  2928. /**
  2929. * megasas_init_fw - Initializes the FW
  2930. * @instance: Adapter soft state
  2931. *
  2932. * This is the main function for initializing firmware
  2933. */
  2934. static int megasas_init_fw(struct megasas_instance *instance)
  2935. {
  2936. u32 max_sectors_1;
  2937. u32 max_sectors_2;
  2938. u32 tmp_sectors, msix_enable;
  2939. struct megasas_register_set __iomem *reg_set;
  2940. struct megasas_ctrl_info *ctrl_info;
  2941. unsigned long bar_list;
  2942. /* Find first memory bar */
  2943. bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
  2944. instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
  2945. instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
  2946. if (pci_request_selected_regions(instance->pdev, instance->bar,
  2947. "megasas: LSI")) {
  2948. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2949. return -EBUSY;
  2950. }
  2951. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2952. if (!instance->reg_set) {
  2953. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2954. goto fail_ioremap;
  2955. }
  2956. reg_set = instance->reg_set;
  2957. switch (instance->pdev->device) {
  2958. case PCI_DEVICE_ID_LSI_FUSION:
  2959. instance->instancet = &megasas_instance_template_fusion;
  2960. break;
  2961. case PCI_DEVICE_ID_LSI_SAS1078R:
  2962. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2963. instance->instancet = &megasas_instance_template_ppc;
  2964. break;
  2965. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2966. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2967. instance->instancet = &megasas_instance_template_gen2;
  2968. break;
  2969. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2970. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2971. instance->instancet = &megasas_instance_template_skinny;
  2972. break;
  2973. case PCI_DEVICE_ID_LSI_SAS1064R:
  2974. case PCI_DEVICE_ID_DELL_PERC5:
  2975. default:
  2976. instance->instancet = &megasas_instance_template_xscale;
  2977. break;
  2978. }
  2979. /*
  2980. * We expect the FW state to be READY
  2981. */
  2982. if (megasas_transition_to_ready(instance))
  2983. goto fail_ready_state;
  2984. /* Check if MSI-X is supported while in ready state */
  2985. msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
  2986. 0x4000000) >> 0x1a;
  2987. if (msix_enable && !msix_disable &&
  2988. !pci_enable_msix(instance->pdev, &instance->msixentry, 1))
  2989. instance->msi_flag = 1;
  2990. /* Get operational params, sge flags, send init cmd to controller */
  2991. if (instance->instancet->init_adapter(instance))
  2992. goto fail_init_adapter;
  2993. printk(KERN_ERR "megasas: INIT adapter done\n");
  2994. /** for passthrough
  2995. * the following function will get the PD LIST.
  2996. */
  2997. memset(instance->pd_list, 0 ,
  2998. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  2999. megasas_get_pd_list(instance);
  3000. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  3001. megasas_get_ld_list(instance);
  3002. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  3003. /*
  3004. * Compute the max allowed sectors per IO: The controller info has two
  3005. * limits on max sectors. Driver should use the minimum of these two.
  3006. *
  3007. * 1 << stripe_sz_ops.min = max sectors per strip
  3008. *
  3009. * Note that older firmwares ( < FW ver 30) didn't report information
  3010. * to calculate max_sectors_1. So the number ended up as zero always.
  3011. */
  3012. tmp_sectors = 0;
  3013. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  3014. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  3015. ctrl_info->max_strips_per_io;
  3016. max_sectors_2 = ctrl_info->max_request_size;
  3017. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  3018. instance->disableOnlineCtrlReset =
  3019. ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
  3020. }
  3021. instance->max_sectors_per_req = instance->max_num_sge *
  3022. PAGE_SIZE / 512;
  3023. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  3024. instance->max_sectors_per_req = tmp_sectors;
  3025. kfree(ctrl_info);
  3026. /*
  3027. * Setup tasklet for cmd completion
  3028. */
  3029. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3030. (unsigned long)instance);
  3031. /* Initialize the cmd completion timer */
  3032. if (poll_mode_io)
  3033. megasas_start_timer(instance, &instance->io_completion_timer,
  3034. megasas_io_completion_timer,
  3035. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3036. return 0;
  3037. fail_init_adapter:
  3038. fail_ready_state:
  3039. iounmap(instance->reg_set);
  3040. fail_ioremap:
  3041. pci_release_selected_regions(instance->pdev, instance->bar);
  3042. return -EINVAL;
  3043. }
  3044. /**
  3045. * megasas_release_mfi - Reverses the FW initialization
  3046. * @intance: Adapter soft state
  3047. */
  3048. static void megasas_release_mfi(struct megasas_instance *instance)
  3049. {
  3050. u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
  3051. if (instance->reply_queue)
  3052. pci_free_consistent(instance->pdev, reply_q_sz,
  3053. instance->reply_queue, instance->reply_queue_h);
  3054. megasas_free_cmds(instance);
  3055. iounmap(instance->reg_set);
  3056. pci_release_selected_regions(instance->pdev, instance->bar);
  3057. }
  3058. /**
  3059. * megasas_get_seq_num - Gets latest event sequence numbers
  3060. * @instance: Adapter soft state
  3061. * @eli: FW event log sequence numbers information
  3062. *
  3063. * FW maintains a log of all events in a non-volatile area. Upper layers would
  3064. * usually find out the latest sequence number of the events, the seq number at
  3065. * the boot etc. They would "read" all the events below the latest seq number
  3066. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  3067. * number), they would subsribe to AEN (asynchronous event notification) and
  3068. * wait for the events to happen.
  3069. */
  3070. static int
  3071. megasas_get_seq_num(struct megasas_instance *instance,
  3072. struct megasas_evt_log_info *eli)
  3073. {
  3074. struct megasas_cmd *cmd;
  3075. struct megasas_dcmd_frame *dcmd;
  3076. struct megasas_evt_log_info *el_info;
  3077. dma_addr_t el_info_h = 0;
  3078. cmd = megasas_get_cmd(instance);
  3079. if (!cmd) {
  3080. return -ENOMEM;
  3081. }
  3082. dcmd = &cmd->frame->dcmd;
  3083. el_info = pci_alloc_consistent(instance->pdev,
  3084. sizeof(struct megasas_evt_log_info),
  3085. &el_info_h);
  3086. if (!el_info) {
  3087. megasas_return_cmd(instance, cmd);
  3088. return -ENOMEM;
  3089. }
  3090. memset(el_info, 0, sizeof(*el_info));
  3091. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3092. dcmd->cmd = MFI_CMD_DCMD;
  3093. dcmd->cmd_status = 0x0;
  3094. dcmd->sge_count = 1;
  3095. dcmd->flags = MFI_FRAME_DIR_READ;
  3096. dcmd->timeout = 0;
  3097. dcmd->pad_0 = 0;
  3098. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  3099. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  3100. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  3101. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  3102. megasas_issue_blocked_cmd(instance, cmd);
  3103. /*
  3104. * Copy the data back into callers buffer
  3105. */
  3106. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  3107. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  3108. el_info, el_info_h);
  3109. megasas_return_cmd(instance, cmd);
  3110. return 0;
  3111. }
  3112. /**
  3113. * megasas_register_aen - Registers for asynchronous event notification
  3114. * @instance: Adapter soft state
  3115. * @seq_num: The starting sequence number
  3116. * @class_locale: Class of the event
  3117. *
  3118. * This function subscribes for AEN for events beyond the @seq_num. It requests
  3119. * to be notified if and only if the event is of type @class_locale
  3120. */
  3121. static int
  3122. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  3123. u32 class_locale_word)
  3124. {
  3125. int ret_val;
  3126. struct megasas_cmd *cmd;
  3127. struct megasas_dcmd_frame *dcmd;
  3128. union megasas_evt_class_locale curr_aen;
  3129. union megasas_evt_class_locale prev_aen;
  3130. /*
  3131. * If there an AEN pending already (aen_cmd), check if the
  3132. * class_locale of that pending AEN is inclusive of the new
  3133. * AEN request we currently have. If it is, then we don't have
  3134. * to do anything. In other words, whichever events the current
  3135. * AEN request is subscribing to, have already been subscribed
  3136. * to.
  3137. *
  3138. * If the old_cmd is _not_ inclusive, then we have to abort
  3139. * that command, form a class_locale that is superset of both
  3140. * old and current and re-issue to the FW
  3141. */
  3142. curr_aen.word = class_locale_word;
  3143. if (instance->aen_cmd) {
  3144. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  3145. /*
  3146. * A class whose enum value is smaller is inclusive of all
  3147. * higher values. If a PROGRESS (= -1) was previously
  3148. * registered, then a new registration requests for higher
  3149. * classes need not be sent to FW. They are automatically
  3150. * included.
  3151. *
  3152. * Locale numbers don't have such hierarchy. They are bitmap
  3153. * values
  3154. */
  3155. if ((prev_aen.members.class <= curr_aen.members.class) &&
  3156. !((prev_aen.members.locale & curr_aen.members.locale) ^
  3157. curr_aen.members.locale)) {
  3158. /*
  3159. * Previously issued event registration includes
  3160. * current request. Nothing to do.
  3161. */
  3162. return 0;
  3163. } else {
  3164. curr_aen.members.locale |= prev_aen.members.locale;
  3165. if (prev_aen.members.class < curr_aen.members.class)
  3166. curr_aen.members.class = prev_aen.members.class;
  3167. instance->aen_cmd->abort_aen = 1;
  3168. ret_val = megasas_issue_blocked_abort_cmd(instance,
  3169. instance->
  3170. aen_cmd);
  3171. if (ret_val) {
  3172. printk(KERN_DEBUG "megasas: Failed to abort "
  3173. "previous AEN command\n");
  3174. return ret_val;
  3175. }
  3176. }
  3177. }
  3178. cmd = megasas_get_cmd(instance);
  3179. if (!cmd)
  3180. return -ENOMEM;
  3181. dcmd = &cmd->frame->dcmd;
  3182. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  3183. /*
  3184. * Prepare DCMD for aen registration
  3185. */
  3186. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3187. dcmd->cmd = MFI_CMD_DCMD;
  3188. dcmd->cmd_status = 0x0;
  3189. dcmd->sge_count = 1;
  3190. dcmd->flags = MFI_FRAME_DIR_READ;
  3191. dcmd->timeout = 0;
  3192. dcmd->pad_0 = 0;
  3193. instance->last_seq_num = seq_num;
  3194. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  3195. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  3196. dcmd->mbox.w[0] = seq_num;
  3197. dcmd->mbox.w[1] = curr_aen.word;
  3198. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  3199. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  3200. if (instance->aen_cmd != NULL) {
  3201. megasas_return_cmd(instance, cmd);
  3202. return 0;
  3203. }
  3204. /*
  3205. * Store reference to the cmd used to register for AEN. When an
  3206. * application wants us to register for AEN, we have to abort this
  3207. * cmd and re-register with a new EVENT LOCALE supplied by that app
  3208. */
  3209. instance->aen_cmd = cmd;
  3210. /*
  3211. * Issue the aen registration frame
  3212. */
  3213. instance->instancet->issue_dcmd(instance, cmd);
  3214. return 0;
  3215. }
  3216. /**
  3217. * megasas_start_aen - Subscribes to AEN during driver load time
  3218. * @instance: Adapter soft state
  3219. */
  3220. static int megasas_start_aen(struct megasas_instance *instance)
  3221. {
  3222. struct megasas_evt_log_info eli;
  3223. union megasas_evt_class_locale class_locale;
  3224. /*
  3225. * Get the latest sequence number from FW
  3226. */
  3227. memset(&eli, 0, sizeof(eli));
  3228. if (megasas_get_seq_num(instance, &eli))
  3229. return -1;
  3230. /*
  3231. * Register AEN with FW for latest sequence number plus 1
  3232. */
  3233. class_locale.members.reserved = 0;
  3234. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3235. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3236. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  3237. class_locale.word);
  3238. }
  3239. /**
  3240. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  3241. * @instance: Adapter soft state
  3242. */
  3243. static int megasas_io_attach(struct megasas_instance *instance)
  3244. {
  3245. struct Scsi_Host *host = instance->host;
  3246. /*
  3247. * Export parameters required by SCSI mid-layer
  3248. */
  3249. host->irq = instance->pdev->irq;
  3250. host->unique_id = instance->unique_id;
  3251. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3252. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3253. host->can_queue =
  3254. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  3255. } else
  3256. host->can_queue =
  3257. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  3258. host->this_id = instance->init_id;
  3259. host->sg_tablesize = instance->max_num_sge;
  3260. if (instance->fw_support_ieee)
  3261. instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
  3262. /*
  3263. * Check if the module parameter value for max_sectors can be used
  3264. */
  3265. if (max_sectors && max_sectors < instance->max_sectors_per_req)
  3266. instance->max_sectors_per_req = max_sectors;
  3267. else {
  3268. if (max_sectors) {
  3269. if (((instance->pdev->device ==
  3270. PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  3271. (instance->pdev->device ==
  3272. PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
  3273. (max_sectors <= MEGASAS_MAX_SECTORS)) {
  3274. instance->max_sectors_per_req = max_sectors;
  3275. } else {
  3276. printk(KERN_INFO "megasas: max_sectors should be > 0"
  3277. "and <= %d (or < 1MB for GEN2 controller)\n",
  3278. instance->max_sectors_per_req);
  3279. }
  3280. }
  3281. }
  3282. host->max_sectors = instance->max_sectors_per_req;
  3283. host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
  3284. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  3285. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  3286. host->max_lun = MEGASAS_MAX_LUN;
  3287. host->max_cmd_len = 16;
  3288. /* Fusion only supports host reset */
  3289. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
  3290. host->hostt->eh_device_reset_handler = NULL;
  3291. host->hostt->eh_bus_reset_handler = NULL;
  3292. }
  3293. /*
  3294. * Notify the mid-layer about the new controller
  3295. */
  3296. if (scsi_add_host(host, &instance->pdev->dev)) {
  3297. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  3298. return -ENODEV;
  3299. }
  3300. /*
  3301. * Trigger SCSI to scan our drives
  3302. */
  3303. scsi_scan_host(host);
  3304. return 0;
  3305. }
  3306. static int
  3307. megasas_set_dma_mask(struct pci_dev *pdev)
  3308. {
  3309. /*
  3310. * All our contollers are capable of performing 64-bit DMA
  3311. */
  3312. if (IS_DMA64) {
  3313. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  3314. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3315. goto fail_set_dma_mask;
  3316. }
  3317. } else {
  3318. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  3319. goto fail_set_dma_mask;
  3320. }
  3321. return 0;
  3322. fail_set_dma_mask:
  3323. return 1;
  3324. }
  3325. /**
  3326. * megasas_probe_one - PCI hotplug entry point
  3327. * @pdev: PCI device structure
  3328. * @id: PCI ids of supported hotplugged adapter
  3329. */
  3330. static int __devinit
  3331. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3332. {
  3333. int rval, pos;
  3334. struct Scsi_Host *host;
  3335. struct megasas_instance *instance;
  3336. u16 control = 0;
  3337. /* Reset MSI-X in the kdump kernel */
  3338. if (reset_devices) {
  3339. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3340. if (pos) {
  3341. pci_read_config_word(pdev, msi_control_reg(pos),
  3342. &control);
  3343. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3344. dev_info(&pdev->dev, "resetting MSI-X\n");
  3345. pci_write_config_word(pdev,
  3346. msi_control_reg(pos),
  3347. control &
  3348. ~PCI_MSIX_FLAGS_ENABLE);
  3349. }
  3350. }
  3351. }
  3352. /*
  3353. * Announce PCI information
  3354. */
  3355. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  3356. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  3357. pdev->subsystem_device);
  3358. printk("bus %d:slot %d:func %d\n",
  3359. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  3360. /*
  3361. * PCI prepping: enable device set bus mastering and dma mask
  3362. */
  3363. rval = pci_enable_device_mem(pdev);
  3364. if (rval) {
  3365. return rval;
  3366. }
  3367. pci_set_master(pdev);
  3368. if (megasas_set_dma_mask(pdev))
  3369. goto fail_set_dma_mask;
  3370. host = scsi_host_alloc(&megasas_template,
  3371. sizeof(struct megasas_instance));
  3372. if (!host) {
  3373. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  3374. goto fail_alloc_instance;
  3375. }
  3376. instance = (struct megasas_instance *)host->hostdata;
  3377. memset(instance, 0, sizeof(*instance));
  3378. atomic_set( &instance->fw_reset_no_pci_access, 0 );
  3379. instance->pdev = pdev;
  3380. switch (instance->pdev->device) {
  3381. case PCI_DEVICE_ID_LSI_FUSION:
  3382. {
  3383. struct fusion_context *fusion;
  3384. instance->ctrl_context =
  3385. kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
  3386. if (!instance->ctrl_context) {
  3387. printk(KERN_DEBUG "megasas: Failed to allocate "
  3388. "memory for Fusion context info\n");
  3389. goto fail_alloc_dma_buf;
  3390. }
  3391. fusion = instance->ctrl_context;
  3392. INIT_LIST_HEAD(&fusion->cmd_pool);
  3393. spin_lock_init(&fusion->cmd_pool_lock);
  3394. }
  3395. break;
  3396. default: /* For all other supported controllers */
  3397. instance->producer =
  3398. pci_alloc_consistent(pdev, sizeof(u32),
  3399. &instance->producer_h);
  3400. instance->consumer =
  3401. pci_alloc_consistent(pdev, sizeof(u32),
  3402. &instance->consumer_h);
  3403. if (!instance->producer || !instance->consumer) {
  3404. printk(KERN_DEBUG "megasas: Failed to allocate"
  3405. "memory for producer, consumer\n");
  3406. goto fail_alloc_dma_buf;
  3407. }
  3408. *instance->producer = 0;
  3409. *instance->consumer = 0;
  3410. break;
  3411. }
  3412. megasas_poll_wait_aen = 0;
  3413. instance->flag_ieee = 0;
  3414. instance->ev = NULL;
  3415. instance->issuepend_done = 1;
  3416. instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
  3417. megasas_poll_wait_aen = 0;
  3418. instance->evt_detail = pci_alloc_consistent(pdev,
  3419. sizeof(struct
  3420. megasas_evt_detail),
  3421. &instance->evt_detail_h);
  3422. if (!instance->evt_detail) {
  3423. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  3424. "event detail structure\n");
  3425. goto fail_alloc_dma_buf;
  3426. }
  3427. /*
  3428. * Initialize locks and queues
  3429. */
  3430. INIT_LIST_HEAD(&instance->cmd_pool);
  3431. INIT_LIST_HEAD(&instance->internal_reset_pending_q);
  3432. atomic_set(&instance->fw_outstanding,0);
  3433. init_waitqueue_head(&instance->int_cmd_wait_q);
  3434. init_waitqueue_head(&instance->abort_cmd_wait_q);
  3435. spin_lock_init(&instance->cmd_pool_lock);
  3436. spin_lock_init(&instance->hba_lock);
  3437. spin_lock_init(&instance->completion_lock);
  3438. spin_lock_init(&poll_aen_lock);
  3439. mutex_init(&instance->aen_mutex);
  3440. mutex_init(&instance->reset_mutex);
  3441. /*
  3442. * Initialize PCI related and misc parameters
  3443. */
  3444. instance->host = host;
  3445. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  3446. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  3447. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  3448. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  3449. instance->flag_ieee = 1;
  3450. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  3451. } else
  3452. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  3453. megasas_dbg_lvl = 0;
  3454. instance->flag = 0;
  3455. instance->unload = 1;
  3456. instance->last_time = 0;
  3457. instance->disableOnlineCtrlReset = 1;
  3458. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  3459. INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
  3460. else
  3461. INIT_WORK(&instance->work_init, process_fw_state_change_wq);
  3462. /*
  3463. * Initialize MFI Firmware
  3464. */
  3465. if (megasas_init_fw(instance))
  3466. goto fail_init_mfi;
  3467. /*
  3468. * Register IRQ
  3469. */
  3470. if (request_irq(instance->msi_flag ? instance->msixentry.vector :
  3471. pdev->irq, instance->instancet->service_isr,
  3472. IRQF_SHARED, "megasas", instance)) {
  3473. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  3474. goto fail_irq;
  3475. }
  3476. instance->instancet->enable_intr(instance->reg_set);
  3477. /*
  3478. * Store instance in PCI softstate
  3479. */
  3480. pci_set_drvdata(pdev, instance);
  3481. /*
  3482. * Add this controller to megasas_mgmt_info structure so that it
  3483. * can be exported to management applications
  3484. */
  3485. megasas_mgmt_info.count++;
  3486. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  3487. megasas_mgmt_info.max_index++;
  3488. /*
  3489. * Register with SCSI mid-layer
  3490. */
  3491. if (megasas_io_attach(instance))
  3492. goto fail_io_attach;
  3493. instance->unload = 0;
  3494. /*
  3495. * Initiate AEN (Asynchronous Event Notification)
  3496. */
  3497. if (megasas_start_aen(instance)) {
  3498. printk(KERN_DEBUG "megasas: start aen failed\n");
  3499. goto fail_start_aen;
  3500. }
  3501. return 0;
  3502. fail_start_aen:
  3503. fail_io_attach:
  3504. megasas_mgmt_info.count--;
  3505. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  3506. megasas_mgmt_info.max_index--;
  3507. pci_set_drvdata(pdev, NULL);
  3508. instance->instancet->disable_intr(instance->reg_set);
  3509. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3510. instance->pdev->irq, instance);
  3511. fail_irq:
  3512. if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
  3513. megasas_release_fusion(instance);
  3514. else
  3515. megasas_release_mfi(instance);
  3516. fail_init_mfi:
  3517. if (instance->msi_flag)
  3518. pci_disable_msix(instance->pdev);
  3519. fail_alloc_dma_buf:
  3520. if (instance->evt_detail)
  3521. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3522. instance->evt_detail,
  3523. instance->evt_detail_h);
  3524. if (instance->producer)
  3525. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3526. instance->producer_h);
  3527. if (instance->consumer)
  3528. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3529. instance->consumer_h);
  3530. scsi_host_put(host);
  3531. fail_alloc_instance:
  3532. fail_set_dma_mask:
  3533. pci_disable_device(pdev);
  3534. return -ENODEV;
  3535. }
  3536. /**
  3537. * megasas_flush_cache - Requests FW to flush all its caches
  3538. * @instance: Adapter soft state
  3539. */
  3540. static void megasas_flush_cache(struct megasas_instance *instance)
  3541. {
  3542. struct megasas_cmd *cmd;
  3543. struct megasas_dcmd_frame *dcmd;
  3544. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3545. return;
  3546. cmd = megasas_get_cmd(instance);
  3547. if (!cmd)
  3548. return;
  3549. dcmd = &cmd->frame->dcmd;
  3550. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3551. dcmd->cmd = MFI_CMD_DCMD;
  3552. dcmd->cmd_status = 0x0;
  3553. dcmd->sge_count = 0;
  3554. dcmd->flags = MFI_FRAME_DIR_NONE;
  3555. dcmd->timeout = 0;
  3556. dcmd->pad_0 = 0;
  3557. dcmd->data_xfer_len = 0;
  3558. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  3559. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  3560. megasas_issue_blocked_cmd(instance, cmd);
  3561. megasas_return_cmd(instance, cmd);
  3562. return;
  3563. }
  3564. /**
  3565. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  3566. * @instance: Adapter soft state
  3567. * @opcode: Shutdown/Hibernate
  3568. */
  3569. static void megasas_shutdown_controller(struct megasas_instance *instance,
  3570. u32 opcode)
  3571. {
  3572. struct megasas_cmd *cmd;
  3573. struct megasas_dcmd_frame *dcmd;
  3574. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
  3575. return;
  3576. cmd = megasas_get_cmd(instance);
  3577. if (!cmd)
  3578. return;
  3579. if (instance->aen_cmd)
  3580. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  3581. if (instance->map_update_cmd)
  3582. megasas_issue_blocked_abort_cmd(instance,
  3583. instance->map_update_cmd);
  3584. dcmd = &cmd->frame->dcmd;
  3585. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  3586. dcmd->cmd = MFI_CMD_DCMD;
  3587. dcmd->cmd_status = 0x0;
  3588. dcmd->sge_count = 0;
  3589. dcmd->flags = MFI_FRAME_DIR_NONE;
  3590. dcmd->timeout = 0;
  3591. dcmd->pad_0 = 0;
  3592. dcmd->data_xfer_len = 0;
  3593. dcmd->opcode = opcode;
  3594. megasas_issue_blocked_cmd(instance, cmd);
  3595. megasas_return_cmd(instance, cmd);
  3596. return;
  3597. }
  3598. #ifdef CONFIG_PM
  3599. /**
  3600. * megasas_suspend - driver suspend entry point
  3601. * @pdev: PCI device structure
  3602. * @state: PCI power state to suspend routine
  3603. */
  3604. static int
  3605. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  3606. {
  3607. struct Scsi_Host *host;
  3608. struct megasas_instance *instance;
  3609. instance = pci_get_drvdata(pdev);
  3610. host = instance->host;
  3611. instance->unload = 1;
  3612. if (poll_mode_io)
  3613. del_timer_sync(&instance->io_completion_timer);
  3614. megasas_flush_cache(instance);
  3615. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  3616. /* cancel the delayed work if this work still in queue */
  3617. if (instance->ev != NULL) {
  3618. struct megasas_aen_event *ev = instance->ev;
  3619. cancel_delayed_work_sync(
  3620. (struct delayed_work *)&ev->hotplug_work);
  3621. instance->ev = NULL;
  3622. }
  3623. tasklet_kill(&instance->isr_tasklet);
  3624. pci_set_drvdata(instance->pdev, instance);
  3625. instance->instancet->disable_intr(instance->reg_set);
  3626. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3627. instance->pdev->irq, instance);
  3628. if (instance->msi_flag)
  3629. pci_disable_msix(instance->pdev);
  3630. pci_save_state(pdev);
  3631. pci_disable_device(pdev);
  3632. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3633. return 0;
  3634. }
  3635. /**
  3636. * megasas_resume- driver resume entry point
  3637. * @pdev: PCI device structure
  3638. */
  3639. static int
  3640. megasas_resume(struct pci_dev *pdev)
  3641. {
  3642. int rval;
  3643. struct Scsi_Host *host;
  3644. struct megasas_instance *instance;
  3645. instance = pci_get_drvdata(pdev);
  3646. host = instance->host;
  3647. pci_set_power_state(pdev, PCI_D0);
  3648. pci_enable_wake(pdev, PCI_D0, 0);
  3649. pci_restore_state(pdev);
  3650. /*
  3651. * PCI prepping: enable device set bus mastering and dma mask
  3652. */
  3653. rval = pci_enable_device_mem(pdev);
  3654. if (rval) {
  3655. printk(KERN_ERR "megasas: Enable device failed\n");
  3656. return rval;
  3657. }
  3658. pci_set_master(pdev);
  3659. if (megasas_set_dma_mask(pdev))
  3660. goto fail_set_dma_mask;
  3661. /*
  3662. * Initialize MFI Firmware
  3663. */
  3664. atomic_set(&instance->fw_outstanding, 0);
  3665. /*
  3666. * We expect the FW state to be READY
  3667. */
  3668. if (megasas_transition_to_ready(instance))
  3669. goto fail_ready_state;
  3670. /* Now re-enable MSI-X */
  3671. if (instance->msi_flag)
  3672. pci_enable_msix(instance->pdev, &instance->msixentry, 1);
  3673. switch (instance->pdev->device) {
  3674. case PCI_DEVICE_ID_LSI_FUSION:
  3675. {
  3676. megasas_reset_reply_desc(instance);
  3677. if (megasas_ioc_init_fusion(instance)) {
  3678. megasas_free_cmds(instance);
  3679. megasas_free_cmds_fusion(instance);
  3680. goto fail_init_mfi;
  3681. }
  3682. if (!megasas_get_map_info(instance))
  3683. megasas_sync_map_info(instance);
  3684. }
  3685. break;
  3686. default:
  3687. *instance->producer = 0;
  3688. *instance->consumer = 0;
  3689. if (megasas_issue_init_mfi(instance))
  3690. goto fail_init_mfi;
  3691. break;
  3692. }
  3693. tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
  3694. (unsigned long)instance);
  3695. /*
  3696. * Register IRQ
  3697. */
  3698. if (request_irq(instance->msi_flag ? instance->msixentry.vector :
  3699. pdev->irq, instance->instancet->service_isr,
  3700. IRQF_SHARED, "megasas", instance)) {
  3701. printk(KERN_ERR "megasas: Failed to register IRQ\n");
  3702. goto fail_irq;
  3703. }
  3704. instance->instancet->enable_intr(instance->reg_set);
  3705. /* Initialize the cmd completion timer */
  3706. if (poll_mode_io)
  3707. megasas_start_timer(instance, &instance->io_completion_timer,
  3708. megasas_io_completion_timer,
  3709. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3710. instance->unload = 0;
  3711. /*
  3712. * Initiate AEN (Asynchronous Event Notification)
  3713. */
  3714. if (megasas_start_aen(instance))
  3715. printk(KERN_ERR "megasas: Start AEN failed\n");
  3716. return 0;
  3717. fail_irq:
  3718. fail_init_mfi:
  3719. if (instance->evt_detail)
  3720. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  3721. instance->evt_detail,
  3722. instance->evt_detail_h);
  3723. if (instance->producer)
  3724. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  3725. instance->producer_h);
  3726. if (instance->consumer)
  3727. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  3728. instance->consumer_h);
  3729. scsi_host_put(host);
  3730. fail_set_dma_mask:
  3731. fail_ready_state:
  3732. pci_disable_device(pdev);
  3733. return -ENODEV;
  3734. }
  3735. #else
  3736. #define megasas_suspend NULL
  3737. #define megasas_resume NULL
  3738. #endif
  3739. /**
  3740. * megasas_detach_one - PCI hot"un"plug entry point
  3741. * @pdev: PCI device structure
  3742. */
  3743. static void __devexit megasas_detach_one(struct pci_dev *pdev)
  3744. {
  3745. int i;
  3746. struct Scsi_Host *host;
  3747. struct megasas_instance *instance;
  3748. struct fusion_context *fusion;
  3749. instance = pci_get_drvdata(pdev);
  3750. instance->unload = 1;
  3751. host = instance->host;
  3752. fusion = instance->ctrl_context;
  3753. if (poll_mode_io)
  3754. del_timer_sync(&instance->io_completion_timer);
  3755. scsi_remove_host(instance->host);
  3756. megasas_flush_cache(instance);
  3757. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3758. /* cancel the delayed work if this work still in queue*/
  3759. if (instance->ev != NULL) {
  3760. struct megasas_aen_event *ev = instance->ev;
  3761. cancel_delayed_work_sync(
  3762. (struct delayed_work *)&ev->hotplug_work);
  3763. instance->ev = NULL;
  3764. }
  3765. tasklet_kill(&instance->isr_tasklet);
  3766. /*
  3767. * Take the instance off the instance array. Note that we will not
  3768. * decrement the max_index. We let this array be sparse array
  3769. */
  3770. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3771. if (megasas_mgmt_info.instance[i] == instance) {
  3772. megasas_mgmt_info.count--;
  3773. megasas_mgmt_info.instance[i] = NULL;
  3774. break;
  3775. }
  3776. }
  3777. pci_set_drvdata(instance->pdev, NULL);
  3778. instance->instancet->disable_intr(instance->reg_set);
  3779. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3780. instance->pdev->irq, instance);
  3781. if (instance->msi_flag)
  3782. pci_disable_msix(instance->pdev);
  3783. switch (instance->pdev->device) {
  3784. case PCI_DEVICE_ID_LSI_FUSION:
  3785. megasas_release_fusion(instance);
  3786. for (i = 0; i < 2 ; i++)
  3787. if (fusion->ld_map[i])
  3788. dma_free_coherent(&instance->pdev->dev,
  3789. fusion->map_sz,
  3790. fusion->ld_map[i],
  3791. fusion->
  3792. ld_map_phys[i]);
  3793. kfree(instance->ctrl_context);
  3794. break;
  3795. default:
  3796. megasas_release_mfi(instance);
  3797. pci_free_consistent(pdev,
  3798. sizeof(struct megasas_evt_detail),
  3799. instance->evt_detail,
  3800. instance->evt_detail_h);
  3801. pci_free_consistent(pdev, sizeof(u32),
  3802. instance->producer,
  3803. instance->producer_h);
  3804. pci_free_consistent(pdev, sizeof(u32),
  3805. instance->consumer,
  3806. instance->consumer_h);
  3807. break;
  3808. }
  3809. scsi_host_put(host);
  3810. pci_set_drvdata(pdev, NULL);
  3811. pci_disable_device(pdev);
  3812. return;
  3813. }
  3814. /**
  3815. * megasas_shutdown - Shutdown entry point
  3816. * @device: Generic device structure
  3817. */
  3818. static void megasas_shutdown(struct pci_dev *pdev)
  3819. {
  3820. struct megasas_instance *instance = pci_get_drvdata(pdev);
  3821. instance->unload = 1;
  3822. megasas_flush_cache(instance);
  3823. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  3824. instance->instancet->disable_intr(instance->reg_set);
  3825. free_irq(instance->msi_flag ? instance->msixentry.vector :
  3826. instance->pdev->irq, instance);
  3827. if (instance->msi_flag)
  3828. pci_disable_msix(instance->pdev);
  3829. }
  3830. /**
  3831. * megasas_mgmt_open - char node "open" entry point
  3832. */
  3833. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  3834. {
  3835. /*
  3836. * Allow only those users with admin rights
  3837. */
  3838. if (!capable(CAP_SYS_ADMIN))
  3839. return -EACCES;
  3840. return 0;
  3841. }
  3842. /**
  3843. * megasas_mgmt_fasync - Async notifier registration from applications
  3844. *
  3845. * This function adds the calling process to a driver global queue. When an
  3846. * event occurs, SIGIO will be sent to all processes in this queue.
  3847. */
  3848. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  3849. {
  3850. int rc;
  3851. mutex_lock(&megasas_async_queue_mutex);
  3852. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  3853. mutex_unlock(&megasas_async_queue_mutex);
  3854. if (rc >= 0) {
  3855. /* For sanity check when we get ioctl */
  3856. filep->private_data = filep;
  3857. return 0;
  3858. }
  3859. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  3860. return rc;
  3861. }
  3862. /**
  3863. * megasas_mgmt_poll - char node "poll" entry point
  3864. * */
  3865. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  3866. {
  3867. unsigned int mask;
  3868. unsigned long flags;
  3869. poll_wait(file, &megasas_poll_wait, wait);
  3870. spin_lock_irqsave(&poll_aen_lock, flags);
  3871. if (megasas_poll_wait_aen)
  3872. mask = (POLLIN | POLLRDNORM);
  3873. else
  3874. mask = 0;
  3875. spin_unlock_irqrestore(&poll_aen_lock, flags);
  3876. return mask;
  3877. }
  3878. /**
  3879. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  3880. * @instance: Adapter soft state
  3881. * @argp: User's ioctl packet
  3882. */
  3883. static int
  3884. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  3885. struct megasas_iocpacket __user * user_ioc,
  3886. struct megasas_iocpacket *ioc)
  3887. {
  3888. struct megasas_sge32 *kern_sge32;
  3889. struct megasas_cmd *cmd;
  3890. void *kbuff_arr[MAX_IOCTL_SGE];
  3891. dma_addr_t buf_handle = 0;
  3892. int error = 0, i;
  3893. void *sense = NULL;
  3894. dma_addr_t sense_handle;
  3895. unsigned long *sense_ptr;
  3896. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  3897. if (ioc->sge_count > MAX_IOCTL_SGE) {
  3898. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  3899. ioc->sge_count, MAX_IOCTL_SGE);
  3900. return -EINVAL;
  3901. }
  3902. cmd = megasas_get_cmd(instance);
  3903. if (!cmd) {
  3904. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  3905. return -ENOMEM;
  3906. }
  3907. /*
  3908. * User's IOCTL packet has 2 frames (maximum). Copy those two
  3909. * frames into our cmd's frames. cmd->frame's context will get
  3910. * overwritten when we copy from user's frames. So set that value
  3911. * alone separately
  3912. */
  3913. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  3914. cmd->frame->hdr.context = cmd->index;
  3915. cmd->frame->hdr.pad_0 = 0;
  3916. /*
  3917. * The management interface between applications and the fw uses
  3918. * MFI frames. E.g, RAID configuration changes, LD property changes
  3919. * etc are accomplishes through different kinds of MFI frames. The
  3920. * driver needs to care only about substituting user buffers with
  3921. * kernel buffers in SGLs. The location of SGL is embedded in the
  3922. * struct iocpacket itself.
  3923. */
  3924. kern_sge32 = (struct megasas_sge32 *)
  3925. ((unsigned long)cmd->frame + ioc->sgl_off);
  3926. /*
  3927. * For each user buffer, create a mirror buffer and copy in
  3928. */
  3929. for (i = 0; i < ioc->sge_count; i++) {
  3930. if (!ioc->sgl[i].iov_len)
  3931. continue;
  3932. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  3933. ioc->sgl[i].iov_len,
  3934. &buf_handle, GFP_KERNEL);
  3935. if (!kbuff_arr[i]) {
  3936. printk(KERN_DEBUG "megasas: Failed to alloc "
  3937. "kernel SGL buffer for IOCTL \n");
  3938. error = -ENOMEM;
  3939. goto out;
  3940. }
  3941. /*
  3942. * We don't change the dma_coherent_mask, so
  3943. * pci_alloc_consistent only returns 32bit addresses
  3944. */
  3945. kern_sge32[i].phys_addr = (u32) buf_handle;
  3946. kern_sge32[i].length = ioc->sgl[i].iov_len;
  3947. /*
  3948. * We created a kernel buffer corresponding to the
  3949. * user buffer. Now copy in from the user buffer
  3950. */
  3951. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  3952. (u32) (ioc->sgl[i].iov_len))) {
  3953. error = -EFAULT;
  3954. goto out;
  3955. }
  3956. }
  3957. if (ioc->sense_len) {
  3958. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  3959. &sense_handle, GFP_KERNEL);
  3960. if (!sense) {
  3961. error = -ENOMEM;
  3962. goto out;
  3963. }
  3964. sense_ptr =
  3965. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  3966. *sense_ptr = sense_handle;
  3967. }
  3968. /*
  3969. * Set the sync_cmd flag so that the ISR knows not to complete this
  3970. * cmd to the SCSI mid-layer
  3971. */
  3972. cmd->sync_cmd = 1;
  3973. megasas_issue_blocked_cmd(instance, cmd);
  3974. cmd->sync_cmd = 0;
  3975. /*
  3976. * copy out the kernel buffers to user buffers
  3977. */
  3978. for (i = 0; i < ioc->sge_count; i++) {
  3979. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  3980. ioc->sgl[i].iov_len)) {
  3981. error = -EFAULT;
  3982. goto out;
  3983. }
  3984. }
  3985. /*
  3986. * copy out the sense
  3987. */
  3988. if (ioc->sense_len) {
  3989. /*
  3990. * sense_ptr points to the location that has the user
  3991. * sense buffer address
  3992. */
  3993. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  3994. ioc->sense_off);
  3995. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  3996. sense, ioc->sense_len)) {
  3997. printk(KERN_ERR "megasas: Failed to copy out to user "
  3998. "sense data\n");
  3999. error = -EFAULT;
  4000. goto out;
  4001. }
  4002. }
  4003. /*
  4004. * copy the status codes returned by the fw
  4005. */
  4006. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  4007. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  4008. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  4009. error = -EFAULT;
  4010. }
  4011. out:
  4012. if (sense) {
  4013. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  4014. sense, sense_handle);
  4015. }
  4016. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  4017. dma_free_coherent(&instance->pdev->dev,
  4018. kern_sge32[i].length,
  4019. kbuff_arr[i], kern_sge32[i].phys_addr);
  4020. }
  4021. megasas_return_cmd(instance, cmd);
  4022. return error;
  4023. }
  4024. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  4025. {
  4026. struct megasas_iocpacket __user *user_ioc =
  4027. (struct megasas_iocpacket __user *)arg;
  4028. struct megasas_iocpacket *ioc;
  4029. struct megasas_instance *instance;
  4030. int error;
  4031. int i;
  4032. unsigned long flags;
  4033. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4034. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  4035. if (!ioc)
  4036. return -ENOMEM;
  4037. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  4038. error = -EFAULT;
  4039. goto out_kfree_ioc;
  4040. }
  4041. instance = megasas_lookup_instance(ioc->host_no);
  4042. if (!instance) {
  4043. error = -ENODEV;
  4044. goto out_kfree_ioc;
  4045. }
  4046. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4047. printk(KERN_ERR "Controller in crit error\n");
  4048. error = -ENODEV;
  4049. goto out_kfree_ioc;
  4050. }
  4051. if (instance->unload == 1) {
  4052. error = -ENODEV;
  4053. goto out_kfree_ioc;
  4054. }
  4055. /*
  4056. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  4057. */
  4058. if (down_interruptible(&instance->ioctl_sem)) {
  4059. error = -ERESTARTSYS;
  4060. goto out_kfree_ioc;
  4061. }
  4062. for (i = 0; i < wait_time; i++) {
  4063. spin_lock_irqsave(&instance->hba_lock, flags);
  4064. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4065. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4066. break;
  4067. }
  4068. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4069. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4070. printk(KERN_NOTICE "megasas: waiting"
  4071. "for controller reset to finish\n");
  4072. }
  4073. msleep(1000);
  4074. }
  4075. spin_lock_irqsave(&instance->hba_lock, flags);
  4076. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4077. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4078. printk(KERN_ERR "megaraid_sas: timed out while"
  4079. "waiting for HBA to recover\n");
  4080. error = -ENODEV;
  4081. goto out_kfree_ioc;
  4082. }
  4083. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4084. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  4085. up(&instance->ioctl_sem);
  4086. out_kfree_ioc:
  4087. kfree(ioc);
  4088. return error;
  4089. }
  4090. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  4091. {
  4092. struct megasas_instance *instance;
  4093. struct megasas_aen aen;
  4094. int error;
  4095. int i;
  4096. unsigned long flags;
  4097. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  4098. if (file->private_data != file) {
  4099. printk(KERN_DEBUG "megasas: fasync_helper was not "
  4100. "called first\n");
  4101. return -EINVAL;
  4102. }
  4103. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  4104. return -EFAULT;
  4105. instance = megasas_lookup_instance(aen.host_no);
  4106. if (!instance)
  4107. return -ENODEV;
  4108. if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
  4109. return -ENODEV;
  4110. }
  4111. if (instance->unload == 1) {
  4112. return -ENODEV;
  4113. }
  4114. for (i = 0; i < wait_time; i++) {
  4115. spin_lock_irqsave(&instance->hba_lock, flags);
  4116. if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
  4117. spin_unlock_irqrestore(&instance->hba_lock,
  4118. flags);
  4119. break;
  4120. }
  4121. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4122. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  4123. printk(KERN_NOTICE "megasas: waiting for"
  4124. "controller reset to finish\n");
  4125. }
  4126. msleep(1000);
  4127. }
  4128. spin_lock_irqsave(&instance->hba_lock, flags);
  4129. if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
  4130. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4131. printk(KERN_ERR "megaraid_sas: timed out while waiting"
  4132. "for HBA to recover.\n");
  4133. return -ENODEV;
  4134. }
  4135. spin_unlock_irqrestore(&instance->hba_lock, flags);
  4136. mutex_lock(&instance->aen_mutex);
  4137. error = megasas_register_aen(instance, aen.seq_num,
  4138. aen.class_locale_word);
  4139. mutex_unlock(&instance->aen_mutex);
  4140. return error;
  4141. }
  4142. /**
  4143. * megasas_mgmt_ioctl - char node ioctl entry point
  4144. */
  4145. static long
  4146. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4147. {
  4148. switch (cmd) {
  4149. case MEGASAS_IOC_FIRMWARE:
  4150. return megasas_mgmt_ioctl_fw(file, arg);
  4151. case MEGASAS_IOC_GET_AEN:
  4152. return megasas_mgmt_ioctl_aen(file, arg);
  4153. }
  4154. return -ENOTTY;
  4155. }
  4156. #ifdef CONFIG_COMPAT
  4157. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  4158. {
  4159. struct compat_megasas_iocpacket __user *cioc =
  4160. (struct compat_megasas_iocpacket __user *)arg;
  4161. struct megasas_iocpacket __user *ioc =
  4162. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  4163. int i;
  4164. int error = 0;
  4165. compat_uptr_t ptr;
  4166. if (clear_user(ioc, sizeof(*ioc)))
  4167. return -EFAULT;
  4168. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  4169. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  4170. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  4171. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  4172. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  4173. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  4174. return -EFAULT;
  4175. /*
  4176. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  4177. * sense_len is not null, so prepare the 64bit value under
  4178. * the same condition.
  4179. */
  4180. if (ioc->sense_len) {
  4181. void __user **sense_ioc_ptr =
  4182. (void __user **)(ioc->frame.raw + ioc->sense_off);
  4183. compat_uptr_t *sense_cioc_ptr =
  4184. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  4185. if (get_user(ptr, sense_cioc_ptr) ||
  4186. put_user(compat_ptr(ptr), sense_ioc_ptr))
  4187. return -EFAULT;
  4188. }
  4189. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  4190. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  4191. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  4192. copy_in_user(&ioc->sgl[i].iov_len,
  4193. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  4194. return -EFAULT;
  4195. }
  4196. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  4197. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  4198. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  4199. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  4200. return -EFAULT;
  4201. }
  4202. return error;
  4203. }
  4204. static long
  4205. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  4206. unsigned long arg)
  4207. {
  4208. switch (cmd) {
  4209. case MEGASAS_IOC_FIRMWARE32:
  4210. return megasas_mgmt_compat_ioctl_fw(file, arg);
  4211. case MEGASAS_IOC_GET_AEN:
  4212. return megasas_mgmt_ioctl_aen(file, arg);
  4213. }
  4214. return -ENOTTY;
  4215. }
  4216. #endif
  4217. /*
  4218. * File operations structure for management interface
  4219. */
  4220. static const struct file_operations megasas_mgmt_fops = {
  4221. .owner = THIS_MODULE,
  4222. .open = megasas_mgmt_open,
  4223. .fasync = megasas_mgmt_fasync,
  4224. .unlocked_ioctl = megasas_mgmt_ioctl,
  4225. .poll = megasas_mgmt_poll,
  4226. #ifdef CONFIG_COMPAT
  4227. .compat_ioctl = megasas_mgmt_compat_ioctl,
  4228. #endif
  4229. .llseek = noop_llseek,
  4230. };
  4231. /*
  4232. * PCI hotplug support registration structure
  4233. */
  4234. static struct pci_driver megasas_pci_driver = {
  4235. .name = "megaraid_sas",
  4236. .id_table = megasas_pci_table,
  4237. .probe = megasas_probe_one,
  4238. .remove = __devexit_p(megasas_detach_one),
  4239. .suspend = megasas_suspend,
  4240. .resume = megasas_resume,
  4241. .shutdown = megasas_shutdown,
  4242. };
  4243. /*
  4244. * Sysfs driver attributes
  4245. */
  4246. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  4247. {
  4248. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  4249. MEGASAS_VERSION);
  4250. }
  4251. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  4252. static ssize_t
  4253. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  4254. {
  4255. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  4256. MEGASAS_RELDATE);
  4257. }
  4258. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  4259. NULL);
  4260. static ssize_t
  4261. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  4262. {
  4263. return sprintf(buf, "%u\n", support_poll_for_event);
  4264. }
  4265. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  4266. megasas_sysfs_show_support_poll_for_event, NULL);
  4267. static ssize_t
  4268. megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
  4269. {
  4270. return sprintf(buf, "%u\n", support_device_change);
  4271. }
  4272. static DRIVER_ATTR(support_device_change, S_IRUGO,
  4273. megasas_sysfs_show_support_device_change, NULL);
  4274. static ssize_t
  4275. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  4276. {
  4277. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  4278. }
  4279. static ssize_t
  4280. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  4281. {
  4282. int retval = count;
  4283. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  4284. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  4285. retval = -EINVAL;
  4286. }
  4287. return retval;
  4288. }
  4289. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  4290. megasas_sysfs_set_dbg_lvl);
  4291. static ssize_t
  4292. megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
  4293. {
  4294. return sprintf(buf, "%u\n", poll_mode_io);
  4295. }
  4296. static ssize_t
  4297. megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
  4298. const char *buf, size_t count)
  4299. {
  4300. int retval = count;
  4301. int tmp = poll_mode_io;
  4302. int i;
  4303. struct megasas_instance *instance;
  4304. if (sscanf(buf, "%u", &poll_mode_io) < 1) {
  4305. printk(KERN_ERR "megasas: could not set poll_mode_io\n");
  4306. retval = -EINVAL;
  4307. }
  4308. /*
  4309. * Check if poll_mode_io is already set or is same as previous value
  4310. */
  4311. if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
  4312. goto out;
  4313. if (poll_mode_io) {
  4314. /*
  4315. * Start timers for all adapters
  4316. */
  4317. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  4318. instance = megasas_mgmt_info.instance[i];
  4319. if (instance) {
  4320. megasas_start_timer(instance,
  4321. &instance->io_completion_timer,
  4322. megasas_io_completion_timer,
  4323. MEGASAS_COMPLETION_TIMER_INTERVAL);
  4324. }
  4325. }
  4326. } else {
  4327. /*
  4328. * Delete timers for all adapters
  4329. */
  4330. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  4331. instance = megasas_mgmt_info.instance[i];
  4332. if (instance)
  4333. del_timer_sync(&instance->io_completion_timer);
  4334. }
  4335. }
  4336. out:
  4337. return retval;
  4338. }
  4339. static void
  4340. megasas_aen_polling(struct work_struct *work)
  4341. {
  4342. struct megasas_aen_event *ev =
  4343. container_of(work, struct megasas_aen_event, hotplug_work);
  4344. struct megasas_instance *instance = ev->instance;
  4345. union megasas_evt_class_locale class_locale;
  4346. struct Scsi_Host *host;
  4347. struct scsi_device *sdev1;
  4348. u16 pd_index = 0;
  4349. u16 ld_index = 0;
  4350. int i, j, doscan = 0;
  4351. u32 seq_num;
  4352. int error;
  4353. if (!instance) {
  4354. printk(KERN_ERR "invalid instance!\n");
  4355. kfree(ev);
  4356. return;
  4357. }
  4358. instance->ev = NULL;
  4359. host = instance->host;
  4360. if (instance->evt_detail) {
  4361. switch (instance->evt_detail->code) {
  4362. case MR_EVT_PD_INSERTED:
  4363. if (megasas_get_pd_list(instance) == 0) {
  4364. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4365. for (j = 0;
  4366. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4367. j++) {
  4368. pd_index =
  4369. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4370. sdev1 =
  4371. scsi_device_lookup(host, i, j, 0);
  4372. if (instance->pd_list[pd_index].driveState
  4373. == MR_PD_STATE_SYSTEM) {
  4374. if (!sdev1) {
  4375. scsi_add_device(host, i, j, 0);
  4376. }
  4377. if (sdev1)
  4378. scsi_device_put(sdev1);
  4379. }
  4380. }
  4381. }
  4382. }
  4383. doscan = 0;
  4384. break;
  4385. case MR_EVT_PD_REMOVED:
  4386. if (megasas_get_pd_list(instance) == 0) {
  4387. megasas_get_pd_list(instance);
  4388. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4389. for (j = 0;
  4390. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4391. j++) {
  4392. pd_index =
  4393. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4394. sdev1 =
  4395. scsi_device_lookup(host, i, j, 0);
  4396. if (instance->pd_list[pd_index].driveState
  4397. == MR_PD_STATE_SYSTEM) {
  4398. if (sdev1) {
  4399. scsi_device_put(sdev1);
  4400. }
  4401. } else {
  4402. if (sdev1) {
  4403. scsi_remove_device(sdev1);
  4404. scsi_device_put(sdev1);
  4405. }
  4406. }
  4407. }
  4408. }
  4409. }
  4410. doscan = 0;
  4411. break;
  4412. case MR_EVT_LD_OFFLINE:
  4413. case MR_EVT_CFG_CLEARED:
  4414. case MR_EVT_LD_DELETED:
  4415. megasas_get_ld_list(instance);
  4416. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4417. for (j = 0;
  4418. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4419. j++) {
  4420. ld_index =
  4421. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4422. sdev1 = scsi_device_lookup(host,
  4423. i + MEGASAS_MAX_LD_CHANNELS,
  4424. j,
  4425. 0);
  4426. if (instance->ld_ids[ld_index] != 0xff) {
  4427. if (sdev1) {
  4428. scsi_device_put(sdev1);
  4429. }
  4430. } else {
  4431. if (sdev1) {
  4432. scsi_remove_device(sdev1);
  4433. scsi_device_put(sdev1);
  4434. }
  4435. }
  4436. }
  4437. }
  4438. doscan = 0;
  4439. break;
  4440. case MR_EVT_LD_CREATED:
  4441. megasas_get_ld_list(instance);
  4442. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4443. for (j = 0;
  4444. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  4445. j++) {
  4446. ld_index =
  4447. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4448. sdev1 = scsi_device_lookup(host,
  4449. i+MEGASAS_MAX_LD_CHANNELS,
  4450. j, 0);
  4451. if (instance->ld_ids[ld_index] !=
  4452. 0xff) {
  4453. if (!sdev1) {
  4454. scsi_add_device(host,
  4455. i + 2,
  4456. j, 0);
  4457. }
  4458. }
  4459. if (sdev1) {
  4460. scsi_device_put(sdev1);
  4461. }
  4462. }
  4463. }
  4464. doscan = 0;
  4465. break;
  4466. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  4467. case MR_EVT_FOREIGN_CFG_IMPORTED:
  4468. case MR_EVT_LD_STATE_CHANGE:
  4469. doscan = 1;
  4470. break;
  4471. default:
  4472. doscan = 0;
  4473. break;
  4474. }
  4475. } else {
  4476. printk(KERN_ERR "invalid evt_detail!\n");
  4477. kfree(ev);
  4478. return;
  4479. }
  4480. if (doscan) {
  4481. printk(KERN_INFO "scanning ...\n");
  4482. megasas_get_pd_list(instance);
  4483. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  4484. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4485. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  4486. sdev1 = scsi_device_lookup(host, i, j, 0);
  4487. if (instance->pd_list[pd_index].driveState ==
  4488. MR_PD_STATE_SYSTEM) {
  4489. if (!sdev1) {
  4490. scsi_add_device(host, i, j, 0);
  4491. }
  4492. if (sdev1)
  4493. scsi_device_put(sdev1);
  4494. } else {
  4495. if (sdev1) {
  4496. scsi_remove_device(sdev1);
  4497. scsi_device_put(sdev1);
  4498. }
  4499. }
  4500. }
  4501. }
  4502. megasas_get_ld_list(instance);
  4503. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  4504. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  4505. ld_index =
  4506. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  4507. sdev1 = scsi_device_lookup(host,
  4508. i+MEGASAS_MAX_LD_CHANNELS, j, 0);
  4509. if (instance->ld_ids[ld_index] != 0xff) {
  4510. if (!sdev1) {
  4511. scsi_add_device(host,
  4512. i+2,
  4513. j, 0);
  4514. } else {
  4515. scsi_device_put(sdev1);
  4516. }
  4517. } else {
  4518. if (sdev1) {
  4519. scsi_remove_device(sdev1);
  4520. scsi_device_put(sdev1);
  4521. }
  4522. }
  4523. }
  4524. }
  4525. }
  4526. if ( instance->aen_cmd != NULL ) {
  4527. kfree(ev);
  4528. return ;
  4529. }
  4530. seq_num = instance->evt_detail->seq_num + 1;
  4531. /* Register AEN with FW for latest sequence number plus 1 */
  4532. class_locale.members.reserved = 0;
  4533. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  4534. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  4535. mutex_lock(&instance->aen_mutex);
  4536. error = megasas_register_aen(instance, seq_num,
  4537. class_locale.word);
  4538. mutex_unlock(&instance->aen_mutex);
  4539. if (error)
  4540. printk(KERN_ERR "register aen failed error %x\n", error);
  4541. kfree(ev);
  4542. }
  4543. static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
  4544. megasas_sysfs_show_poll_mode_io,
  4545. megasas_sysfs_set_poll_mode_io);
  4546. /**
  4547. * megasas_init - Driver load entry point
  4548. */
  4549. static int __init megasas_init(void)
  4550. {
  4551. int rval;
  4552. /*
  4553. * Announce driver version and other information
  4554. */
  4555. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  4556. MEGASAS_EXT_VERSION);
  4557. support_poll_for_event = 2;
  4558. support_device_change = 1;
  4559. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  4560. /*
  4561. * Register character device node
  4562. */
  4563. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  4564. if (rval < 0) {
  4565. printk(KERN_DEBUG "megasas: failed to open device node\n");
  4566. return rval;
  4567. }
  4568. megasas_mgmt_majorno = rval;
  4569. /*
  4570. * Register ourselves as PCI hotplug module
  4571. */
  4572. rval = pci_register_driver(&megasas_pci_driver);
  4573. if (rval) {
  4574. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  4575. goto err_pcidrv;
  4576. }
  4577. rval = driver_create_file(&megasas_pci_driver.driver,
  4578. &driver_attr_version);
  4579. if (rval)
  4580. goto err_dcf_attr_ver;
  4581. rval = driver_create_file(&megasas_pci_driver.driver,
  4582. &driver_attr_release_date);
  4583. if (rval)
  4584. goto err_dcf_rel_date;
  4585. rval = driver_create_file(&megasas_pci_driver.driver,
  4586. &driver_attr_support_poll_for_event);
  4587. if (rval)
  4588. goto err_dcf_support_poll_for_event;
  4589. rval = driver_create_file(&megasas_pci_driver.driver,
  4590. &driver_attr_dbg_lvl);
  4591. if (rval)
  4592. goto err_dcf_dbg_lvl;
  4593. rval = driver_create_file(&megasas_pci_driver.driver,
  4594. &driver_attr_poll_mode_io);
  4595. if (rval)
  4596. goto err_dcf_poll_mode_io;
  4597. rval = driver_create_file(&megasas_pci_driver.driver,
  4598. &driver_attr_support_device_change);
  4599. if (rval)
  4600. goto err_dcf_support_device_change;
  4601. return rval;
  4602. err_dcf_support_device_change:
  4603. driver_remove_file(&megasas_pci_driver.driver,
  4604. &driver_attr_poll_mode_io);
  4605. err_dcf_poll_mode_io:
  4606. driver_remove_file(&megasas_pci_driver.driver,
  4607. &driver_attr_dbg_lvl);
  4608. err_dcf_dbg_lvl:
  4609. driver_remove_file(&megasas_pci_driver.driver,
  4610. &driver_attr_support_poll_for_event);
  4611. err_dcf_support_poll_for_event:
  4612. driver_remove_file(&megasas_pci_driver.driver,
  4613. &driver_attr_release_date);
  4614. err_dcf_rel_date:
  4615. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4616. err_dcf_attr_ver:
  4617. pci_unregister_driver(&megasas_pci_driver);
  4618. err_pcidrv:
  4619. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4620. return rval;
  4621. }
  4622. /**
  4623. * megasas_exit - Driver unload entry point
  4624. */
  4625. static void __exit megasas_exit(void)
  4626. {
  4627. driver_remove_file(&megasas_pci_driver.driver,
  4628. &driver_attr_poll_mode_io);
  4629. driver_remove_file(&megasas_pci_driver.driver,
  4630. &driver_attr_dbg_lvl);
  4631. driver_remove_file(&megasas_pci_driver.driver,
  4632. &driver_attr_support_poll_for_event);
  4633. driver_remove_file(&megasas_pci_driver.driver,
  4634. &driver_attr_support_device_change);
  4635. driver_remove_file(&megasas_pci_driver.driver,
  4636. &driver_attr_release_date);
  4637. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  4638. pci_unregister_driver(&megasas_pci_driver);
  4639. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  4640. }
  4641. module_init(megasas_init);
  4642. module_exit(megasas_exit);