megaraid_sas_base.c 145 KB

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