megaraid_sas_base.c 150 KB

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