megaraid_sas_base.c 141 KB

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