megaraid_sas_base.c 133 KB

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