megaraid_sas.c 130 KB

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