megaraid_sas_base.c 138 KB

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