be_main.c 164 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880
  1. /*
  2. * Copyright 2017 Broadcom. All Rights Reserved.
  3. * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation. The full GNU General
  8. * Public License is included in this distribution in the file called COPYING.
  9. *
  10. * Contact Information:
  11. * linux-drivers@broadcom.com
  12. *
  13. */
  14. #include <linux/reboot.h>
  15. #include <linux/delay.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/pci.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <linux/semaphore.h>
  23. #include <linux/iscsi_boot_sysfs.h>
  24. #include <linux/module.h>
  25. #include <linux/bsg-lib.h>
  26. #include <linux/irq_poll.h>
  27. #include <scsi/libiscsi.h>
  28. #include <scsi/scsi_bsg_iscsi.h>
  29. #include <scsi/scsi_netlink.h>
  30. #include <scsi/scsi_transport_iscsi.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include "be_main.h"
  37. #include "be_iscsi.h"
  38. #include "be_mgmt.h"
  39. #include "be_cmds.h"
  40. static unsigned int be_iopoll_budget = 10;
  41. static unsigned int be_max_phys_size = 64;
  42. static unsigned int enable_msix = 1;
  43. MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
  44. MODULE_VERSION(BUILD_STR);
  45. MODULE_AUTHOR("Emulex Corporation");
  46. MODULE_LICENSE("GPL");
  47. module_param(be_iopoll_budget, int, 0);
  48. module_param(enable_msix, int, 0);
  49. module_param(be_max_phys_size, uint, S_IRUGO);
  50. MODULE_PARM_DESC(be_max_phys_size,
  51. "Maximum Size (In Kilobytes) of physically contiguous "
  52. "memory that can be allocated. Range is 16 - 128");
  53. #define beiscsi_disp_param(_name)\
  54. static ssize_t \
  55. beiscsi_##_name##_disp(struct device *dev,\
  56. struct device_attribute *attrib, char *buf) \
  57. { \
  58. struct Scsi_Host *shost = class_to_shost(dev);\
  59. struct beiscsi_hba *phba = iscsi_host_priv(shost); \
  60. return snprintf(buf, PAGE_SIZE, "%d\n",\
  61. phba->attr_##_name);\
  62. }
  63. #define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
  64. static int \
  65. beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
  66. {\
  67. if (val >= _minval && val <= _maxval) {\
  68. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
  69. "BA_%d : beiscsi_"#_name" updated "\
  70. "from 0x%x ==> 0x%x\n",\
  71. phba->attr_##_name, val); \
  72. phba->attr_##_name = val;\
  73. return 0;\
  74. } \
  75. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
  76. "BA_%d beiscsi_"#_name" attribute "\
  77. "cannot be updated to 0x%x, "\
  78. "range allowed is ["#_minval" - "#_maxval"]\n", val);\
  79. return -EINVAL;\
  80. }
  81. #define beiscsi_store_param(_name) \
  82. static ssize_t \
  83. beiscsi_##_name##_store(struct device *dev,\
  84. struct device_attribute *attr, const char *buf,\
  85. size_t count) \
  86. { \
  87. struct Scsi_Host *shost = class_to_shost(dev);\
  88. struct beiscsi_hba *phba = iscsi_host_priv(shost);\
  89. uint32_t param_val = 0;\
  90. if (!isdigit(buf[0]))\
  91. return -EINVAL;\
  92. if (sscanf(buf, "%i", &param_val) != 1)\
  93. return -EINVAL;\
  94. if (beiscsi_##_name##_change(phba, param_val) == 0) \
  95. return strlen(buf);\
  96. else \
  97. return -EINVAL;\
  98. }
  99. #define beiscsi_init_param(_name, _minval, _maxval, _defval) \
  100. static int \
  101. beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
  102. { \
  103. if (val >= _minval && val <= _maxval) {\
  104. phba->attr_##_name = val;\
  105. return 0;\
  106. } \
  107. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
  108. "BA_%d beiscsi_"#_name" attribute " \
  109. "cannot be updated to 0x%x, "\
  110. "range allowed is ["#_minval" - "#_maxval"]\n", val);\
  111. phba->attr_##_name = _defval;\
  112. return -EINVAL;\
  113. }
  114. #define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
  115. static uint beiscsi_##_name = _defval;\
  116. module_param(beiscsi_##_name, uint, S_IRUGO);\
  117. MODULE_PARM_DESC(beiscsi_##_name, _descp);\
  118. beiscsi_disp_param(_name)\
  119. beiscsi_change_param(_name, _minval, _maxval, _defval)\
  120. beiscsi_store_param(_name)\
  121. beiscsi_init_param(_name, _minval, _maxval, _defval)\
  122. DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
  123. beiscsi_##_name##_disp, beiscsi_##_name##_store)
  124. /*
  125. * When new log level added update the
  126. * the MAX allowed value for log_enable
  127. */
  128. BEISCSI_RW_ATTR(log_enable, 0x00,
  129. 0xFF, 0x00, "Enable logging Bit Mask\n"
  130. "\t\t\t\tInitialization Events : 0x01\n"
  131. "\t\t\t\tMailbox Events : 0x02\n"
  132. "\t\t\t\tMiscellaneous Events : 0x04\n"
  133. "\t\t\t\tError Handling : 0x08\n"
  134. "\t\t\t\tIO Path Events : 0x10\n"
  135. "\t\t\t\tConfiguration Path : 0x20\n"
  136. "\t\t\t\tiSCSI Protocol : 0x40\n");
  137. DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
  138. DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
  139. DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
  140. DEVICE_ATTR(beiscsi_phys_port, S_IRUGO, beiscsi_phys_port_disp, NULL);
  141. DEVICE_ATTR(beiscsi_active_session_count, S_IRUGO,
  142. beiscsi_active_session_disp, NULL);
  143. DEVICE_ATTR(beiscsi_free_session_count, S_IRUGO,
  144. beiscsi_free_session_disp, NULL);
  145. struct device_attribute *beiscsi_attrs[] = {
  146. &dev_attr_beiscsi_log_enable,
  147. &dev_attr_beiscsi_drvr_ver,
  148. &dev_attr_beiscsi_adapter_family,
  149. &dev_attr_beiscsi_fw_ver,
  150. &dev_attr_beiscsi_active_session_count,
  151. &dev_attr_beiscsi_free_session_count,
  152. &dev_attr_beiscsi_phys_port,
  153. NULL,
  154. };
  155. static char const *cqe_desc[] = {
  156. "RESERVED_DESC",
  157. "SOL_CMD_COMPLETE",
  158. "SOL_CMD_KILLED_DATA_DIGEST_ERR",
  159. "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
  160. "CXN_KILLED_BURST_LEN_MISMATCH",
  161. "CXN_KILLED_AHS_RCVD",
  162. "CXN_KILLED_HDR_DIGEST_ERR",
  163. "CXN_KILLED_UNKNOWN_HDR",
  164. "CXN_KILLED_STALE_ITT_TTT_RCVD",
  165. "CXN_KILLED_INVALID_ITT_TTT_RCVD",
  166. "CXN_KILLED_RST_RCVD",
  167. "CXN_KILLED_TIMED_OUT",
  168. "CXN_KILLED_RST_SENT",
  169. "CXN_KILLED_FIN_RCVD",
  170. "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
  171. "CXN_KILLED_BAD_WRB_INDEX_ERROR",
  172. "CXN_KILLED_OVER_RUN_RESIDUAL",
  173. "CXN_KILLED_UNDER_RUN_RESIDUAL",
  174. "CMD_KILLED_INVALID_STATSN_RCVD",
  175. "CMD_KILLED_INVALID_R2T_RCVD",
  176. "CMD_CXN_KILLED_LUN_INVALID",
  177. "CMD_CXN_KILLED_ICD_INVALID",
  178. "CMD_CXN_KILLED_ITT_INVALID",
  179. "CMD_CXN_KILLED_SEQ_OUTOFORDER",
  180. "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
  181. "CXN_INVALIDATE_NOTIFY",
  182. "CXN_INVALIDATE_INDEX_NOTIFY",
  183. "CMD_INVALIDATED_NOTIFY",
  184. "UNSOL_HDR_NOTIFY",
  185. "UNSOL_DATA_NOTIFY",
  186. "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
  187. "DRIVERMSG_NOTIFY",
  188. "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
  189. "SOL_CMD_KILLED_DIF_ERR",
  190. "CXN_KILLED_SYN_RCVD",
  191. "CXN_KILLED_IMM_DATA_RCVD"
  192. };
  193. static int beiscsi_slave_configure(struct scsi_device *sdev)
  194. {
  195. blk_queue_max_segment_size(sdev->request_queue, 65536);
  196. return 0;
  197. }
  198. static int beiscsi_eh_abort(struct scsi_cmnd *sc)
  199. {
  200. struct iscsi_task *abrt_task = (struct iscsi_task *)sc->SCp.ptr;
  201. struct iscsi_cls_session *cls_session;
  202. struct beiscsi_io_task *abrt_io_task;
  203. struct beiscsi_conn *beiscsi_conn;
  204. struct iscsi_session *session;
  205. struct invldt_cmd_tbl inv_tbl;
  206. struct beiscsi_hba *phba;
  207. struct iscsi_conn *conn;
  208. int rc;
  209. cls_session = starget_to_session(scsi_target(sc->device));
  210. session = cls_session->dd_data;
  211. /* check if we raced, task just got cleaned up under us */
  212. spin_lock_bh(&session->back_lock);
  213. if (!abrt_task || !abrt_task->sc) {
  214. spin_unlock_bh(&session->back_lock);
  215. return SUCCESS;
  216. }
  217. /* get a task ref till FW processes the req for the ICD used */
  218. __iscsi_get_task(abrt_task);
  219. abrt_io_task = abrt_task->dd_data;
  220. conn = abrt_task->conn;
  221. beiscsi_conn = conn->dd_data;
  222. phba = beiscsi_conn->phba;
  223. /* mark WRB invalid which have been not processed by FW yet */
  224. if (is_chip_be2_be3r(phba)) {
  225. AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
  226. abrt_io_task->pwrb_handle->pwrb, 1);
  227. } else {
  228. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
  229. abrt_io_task->pwrb_handle->pwrb, 1);
  230. }
  231. inv_tbl.cid = beiscsi_conn->beiscsi_conn_cid;
  232. inv_tbl.icd = abrt_io_task->psgl_handle->sgl_index;
  233. spin_unlock_bh(&session->back_lock);
  234. rc = beiscsi_mgmt_invalidate_icds(phba, &inv_tbl, 1);
  235. iscsi_put_task(abrt_task);
  236. if (rc) {
  237. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  238. "BM_%d : sc %p invalidation failed %d\n",
  239. sc, rc);
  240. return FAILED;
  241. }
  242. return iscsi_eh_abort(sc);
  243. }
  244. static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
  245. {
  246. struct beiscsi_invldt_cmd_tbl {
  247. struct invldt_cmd_tbl tbl[BE_INVLDT_CMD_TBL_SZ];
  248. struct iscsi_task *task[BE_INVLDT_CMD_TBL_SZ];
  249. } *inv_tbl;
  250. struct iscsi_cls_session *cls_session;
  251. struct beiscsi_conn *beiscsi_conn;
  252. struct beiscsi_io_task *io_task;
  253. struct iscsi_session *session;
  254. struct beiscsi_hba *phba;
  255. struct iscsi_conn *conn;
  256. struct iscsi_task *task;
  257. unsigned int i, nents;
  258. int rc, more = 0;
  259. cls_session = starget_to_session(scsi_target(sc->device));
  260. session = cls_session->dd_data;
  261. spin_lock_bh(&session->frwd_lock);
  262. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
  263. spin_unlock_bh(&session->frwd_lock);
  264. return FAILED;
  265. }
  266. conn = session->leadconn;
  267. beiscsi_conn = conn->dd_data;
  268. phba = beiscsi_conn->phba;
  269. inv_tbl = kzalloc(sizeof(*inv_tbl), GFP_ATOMIC);
  270. if (!inv_tbl) {
  271. spin_unlock_bh(&session->frwd_lock);
  272. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  273. "BM_%d : invldt_cmd_tbl alloc failed\n");
  274. return FAILED;
  275. }
  276. nents = 0;
  277. /* take back_lock to prevent task from getting cleaned up under us */
  278. spin_lock(&session->back_lock);
  279. for (i = 0; i < conn->session->cmds_max; i++) {
  280. task = conn->session->cmds[i];
  281. if (!task->sc)
  282. continue;
  283. if (sc->device->lun != task->sc->device->lun)
  284. continue;
  285. /**
  286. * Can't fit in more cmds? Normally this won't happen b'coz
  287. * BEISCSI_CMD_PER_LUN is same as BE_INVLDT_CMD_TBL_SZ.
  288. */
  289. if (nents == BE_INVLDT_CMD_TBL_SZ) {
  290. more = 1;
  291. break;
  292. }
  293. /* get a task ref till FW processes the req for the ICD used */
  294. __iscsi_get_task(task);
  295. io_task = task->dd_data;
  296. /* mark WRB invalid which have been not processed by FW yet */
  297. if (is_chip_be2_be3r(phba)) {
  298. AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
  299. io_task->pwrb_handle->pwrb, 1);
  300. } else {
  301. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
  302. io_task->pwrb_handle->pwrb, 1);
  303. }
  304. inv_tbl->tbl[nents].cid = beiscsi_conn->beiscsi_conn_cid;
  305. inv_tbl->tbl[nents].icd = io_task->psgl_handle->sgl_index;
  306. inv_tbl->task[nents] = task;
  307. nents++;
  308. }
  309. spin_unlock(&session->back_lock);
  310. spin_unlock_bh(&session->frwd_lock);
  311. rc = SUCCESS;
  312. if (!nents)
  313. goto end_reset;
  314. if (more) {
  315. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
  316. "BM_%d : number of cmds exceeds size of invalidation table\n");
  317. rc = FAILED;
  318. goto end_reset;
  319. }
  320. if (beiscsi_mgmt_invalidate_icds(phba, &inv_tbl->tbl[0], nents)) {
  321. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
  322. "BM_%d : cid %u scmds invalidation failed\n",
  323. beiscsi_conn->beiscsi_conn_cid);
  324. rc = FAILED;
  325. }
  326. end_reset:
  327. for (i = 0; i < nents; i++)
  328. iscsi_put_task(inv_tbl->task[i]);
  329. kfree(inv_tbl);
  330. if (rc == SUCCESS)
  331. rc = iscsi_eh_device_reset(sc);
  332. return rc;
  333. }
  334. /*------------------- PCI Driver operations and data ----------------- */
  335. static const struct pci_device_id beiscsi_pci_id_table[] = {
  336. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
  337. { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
  338. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
  339. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
  340. { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
  341. { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
  342. { 0 }
  343. };
  344. MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
  345. static struct scsi_host_template beiscsi_sht = {
  346. .module = THIS_MODULE,
  347. .name = "Emulex 10Gbe open-iscsi Initiator Driver",
  348. .proc_name = DRV_NAME,
  349. .queuecommand = iscsi_queuecommand,
  350. .change_queue_depth = scsi_change_queue_depth,
  351. .slave_configure = beiscsi_slave_configure,
  352. .target_alloc = iscsi_target_alloc,
  353. .eh_timed_out = iscsi_eh_cmd_timed_out,
  354. .eh_abort_handler = beiscsi_eh_abort,
  355. .eh_device_reset_handler = beiscsi_eh_device_reset,
  356. .eh_target_reset_handler = iscsi_eh_session_reset,
  357. .shost_attrs = beiscsi_attrs,
  358. .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
  359. .can_queue = BE2_IO_DEPTH,
  360. .this_id = -1,
  361. .max_sectors = BEISCSI_MAX_SECTORS,
  362. .cmd_per_lun = BEISCSI_CMD_PER_LUN,
  363. .use_clustering = ENABLE_CLUSTERING,
  364. .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
  365. .track_queue_depth = 1,
  366. };
  367. static struct scsi_transport_template *beiscsi_scsi_transport;
  368. static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
  369. {
  370. struct beiscsi_hba *phba;
  371. struct Scsi_Host *shost;
  372. shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
  373. if (!shost) {
  374. dev_err(&pcidev->dev,
  375. "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
  376. return NULL;
  377. }
  378. shost->max_id = BE2_MAX_SESSIONS;
  379. shost->max_channel = 0;
  380. shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
  381. shost->max_lun = BEISCSI_NUM_MAX_LUN;
  382. shost->transportt = beiscsi_scsi_transport;
  383. phba = iscsi_host_priv(shost);
  384. memset(phba, 0, sizeof(*phba));
  385. phba->shost = shost;
  386. phba->pcidev = pci_dev_get(pcidev);
  387. pci_set_drvdata(pcidev, phba);
  388. phba->interface_handle = 0xFFFFFFFF;
  389. return phba;
  390. }
  391. static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
  392. {
  393. if (phba->csr_va) {
  394. iounmap(phba->csr_va);
  395. phba->csr_va = NULL;
  396. }
  397. if (phba->db_va) {
  398. iounmap(phba->db_va);
  399. phba->db_va = NULL;
  400. }
  401. if (phba->pci_va) {
  402. iounmap(phba->pci_va);
  403. phba->pci_va = NULL;
  404. }
  405. }
  406. static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
  407. struct pci_dev *pcidev)
  408. {
  409. u8 __iomem *addr;
  410. int pcicfg_reg;
  411. addr = ioremap_nocache(pci_resource_start(pcidev, 2),
  412. pci_resource_len(pcidev, 2));
  413. if (addr == NULL)
  414. return -ENOMEM;
  415. phba->ctrl.csr = addr;
  416. phba->csr_va = addr;
  417. addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
  418. if (addr == NULL)
  419. goto pci_map_err;
  420. phba->ctrl.db = addr;
  421. phba->db_va = addr;
  422. if (phba->generation == BE_GEN2)
  423. pcicfg_reg = 1;
  424. else
  425. pcicfg_reg = 0;
  426. addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
  427. pci_resource_len(pcidev, pcicfg_reg));
  428. if (addr == NULL)
  429. goto pci_map_err;
  430. phba->ctrl.pcicfg = addr;
  431. phba->pci_va = addr;
  432. return 0;
  433. pci_map_err:
  434. beiscsi_unmap_pci_function(phba);
  435. return -ENOMEM;
  436. }
  437. static int beiscsi_enable_pci(struct pci_dev *pcidev)
  438. {
  439. int ret;
  440. ret = pci_enable_device(pcidev);
  441. if (ret) {
  442. dev_err(&pcidev->dev,
  443. "beiscsi_enable_pci - enable device failed\n");
  444. return ret;
  445. }
  446. ret = pci_request_regions(pcidev, DRV_NAME);
  447. if (ret) {
  448. dev_err(&pcidev->dev,
  449. "beiscsi_enable_pci - request region failed\n");
  450. goto pci_dev_disable;
  451. }
  452. pci_set_master(pcidev);
  453. ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
  454. if (ret) {
  455. ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
  456. if (ret) {
  457. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  458. goto pci_region_release;
  459. } else {
  460. ret = pci_set_consistent_dma_mask(pcidev,
  461. DMA_BIT_MASK(32));
  462. }
  463. } else {
  464. ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
  465. if (ret) {
  466. dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
  467. goto pci_region_release;
  468. }
  469. }
  470. return 0;
  471. pci_region_release:
  472. pci_release_regions(pcidev);
  473. pci_dev_disable:
  474. pci_disable_device(pcidev);
  475. return ret;
  476. }
  477. static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
  478. {
  479. struct be_ctrl_info *ctrl = &phba->ctrl;
  480. struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
  481. struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
  482. int status = 0;
  483. ctrl->pdev = pdev;
  484. status = beiscsi_map_pci_bars(phba, pdev);
  485. if (status)
  486. return status;
  487. mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
  488. mbox_mem_alloc->va = pci_alloc_consistent(pdev,
  489. mbox_mem_alloc->size,
  490. &mbox_mem_alloc->dma);
  491. if (!mbox_mem_alloc->va) {
  492. beiscsi_unmap_pci_function(phba);
  493. return -ENOMEM;
  494. }
  495. mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
  496. mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
  497. mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
  498. memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
  499. mutex_init(&ctrl->mbox_lock);
  500. spin_lock_init(&phba->ctrl.mcc_lock);
  501. return status;
  502. }
  503. /**
  504. * beiscsi_get_params()- Set the config paramters
  505. * @phba: ptr device priv structure
  506. **/
  507. static void beiscsi_get_params(struct beiscsi_hba *phba)
  508. {
  509. uint32_t total_cid_count = 0;
  510. uint32_t total_icd_count = 0;
  511. uint8_t ulp_num = 0;
  512. total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
  513. BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
  514. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  515. uint32_t align_mask = 0;
  516. uint32_t icd_post_per_page = 0;
  517. uint32_t icd_count_unavailable = 0;
  518. uint32_t icd_start = 0, icd_count = 0;
  519. uint32_t icd_start_align = 0, icd_count_align = 0;
  520. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  521. icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
  522. icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
  523. /* Get ICD count that can be posted on each page */
  524. icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
  525. sizeof(struct iscsi_sge)));
  526. align_mask = (icd_post_per_page - 1);
  527. /* Check if icd_start is aligned ICD per page posting */
  528. if (icd_start % icd_post_per_page) {
  529. icd_start_align = ((icd_start +
  530. icd_post_per_page) &
  531. ~(align_mask));
  532. phba->fw_config.
  533. iscsi_icd_start[ulp_num] =
  534. icd_start_align;
  535. }
  536. icd_count_align = (icd_count & ~align_mask);
  537. /* ICD discarded in the process of alignment */
  538. if (icd_start_align)
  539. icd_count_unavailable = ((icd_start_align -
  540. icd_start) +
  541. (icd_count -
  542. icd_count_align));
  543. /* Updated ICD count available */
  544. phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
  545. icd_count_unavailable);
  546. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  547. "BM_%d : Aligned ICD values\n"
  548. "\t ICD Start : %d\n"
  549. "\t ICD Count : %d\n"
  550. "\t ICD Discarded : %d\n",
  551. phba->fw_config.
  552. iscsi_icd_start[ulp_num],
  553. phba->fw_config.
  554. iscsi_icd_count[ulp_num],
  555. icd_count_unavailable);
  556. break;
  557. }
  558. }
  559. total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
  560. phba->params.ios_per_ctrl = (total_icd_count -
  561. (total_cid_count +
  562. BE2_TMFS + BE2_NOPOUT_REQ));
  563. phba->params.cxns_per_ctrl = total_cid_count;
  564. phba->params.icds_per_ctrl = total_icd_count;
  565. phba->params.num_sge_per_io = BE2_SGE;
  566. phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
  567. phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
  568. phba->params.num_eq_entries = 1024;
  569. phba->params.num_cq_entries = 1024;
  570. phba->params.wrbs_per_cxn = 256;
  571. }
  572. static void hwi_ring_eq_db(struct beiscsi_hba *phba,
  573. unsigned int id, unsigned int clr_interrupt,
  574. unsigned int num_processed,
  575. unsigned char rearm, unsigned char event)
  576. {
  577. u32 val = 0;
  578. if (rearm)
  579. val |= 1 << DB_EQ_REARM_SHIFT;
  580. if (clr_interrupt)
  581. val |= 1 << DB_EQ_CLR_SHIFT;
  582. if (event)
  583. val |= 1 << DB_EQ_EVNT_SHIFT;
  584. val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
  585. /* Setting lower order EQ_ID Bits */
  586. val |= (id & DB_EQ_RING_ID_LOW_MASK);
  587. /* Setting Higher order EQ_ID Bits */
  588. val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
  589. DB_EQ_RING_ID_HIGH_MASK)
  590. << DB_EQ_HIGH_SET_SHIFT);
  591. iowrite32(val, phba->db_va + DB_EQ_OFFSET);
  592. }
  593. /**
  594. * be_isr_mcc - The isr routine of the driver.
  595. * @irq: Not used
  596. * @dev_id: Pointer to host adapter structure
  597. */
  598. static irqreturn_t be_isr_mcc(int irq, void *dev_id)
  599. {
  600. struct beiscsi_hba *phba;
  601. struct be_eq_entry *eqe;
  602. struct be_queue_info *eq;
  603. struct be_queue_info *mcc;
  604. unsigned int mcc_events;
  605. struct be_eq_obj *pbe_eq;
  606. pbe_eq = dev_id;
  607. eq = &pbe_eq->q;
  608. phba = pbe_eq->phba;
  609. mcc = &phba->ctrl.mcc_obj.cq;
  610. eqe = queue_tail_node(eq);
  611. mcc_events = 0;
  612. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  613. & EQE_VALID_MASK) {
  614. if (((eqe->dw[offsetof(struct amap_eq_entry,
  615. resource_id) / 32] &
  616. EQE_RESID_MASK) >> 16) == mcc->id) {
  617. mcc_events++;
  618. }
  619. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  620. queue_tail_inc(eq);
  621. eqe = queue_tail_node(eq);
  622. }
  623. if (mcc_events) {
  624. queue_work(phba->wq, &pbe_eq->mcc_work);
  625. hwi_ring_eq_db(phba, eq->id, 1, mcc_events, 1, 1);
  626. }
  627. return IRQ_HANDLED;
  628. }
  629. /**
  630. * be_isr_msix - The isr routine of the driver.
  631. * @irq: Not used
  632. * @dev_id: Pointer to host adapter structure
  633. */
  634. static irqreturn_t be_isr_msix(int irq, void *dev_id)
  635. {
  636. struct beiscsi_hba *phba;
  637. struct be_queue_info *eq;
  638. struct be_eq_obj *pbe_eq;
  639. pbe_eq = dev_id;
  640. eq = &pbe_eq->q;
  641. phba = pbe_eq->phba;
  642. /* disable interrupt till iopoll completes */
  643. hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
  644. irq_poll_sched(&pbe_eq->iopoll);
  645. return IRQ_HANDLED;
  646. }
  647. /**
  648. * be_isr - The isr routine of the driver.
  649. * @irq: Not used
  650. * @dev_id: Pointer to host adapter structure
  651. */
  652. static irqreturn_t be_isr(int irq, void *dev_id)
  653. {
  654. struct beiscsi_hba *phba;
  655. struct hwi_controller *phwi_ctrlr;
  656. struct hwi_context_memory *phwi_context;
  657. struct be_eq_entry *eqe;
  658. struct be_queue_info *eq;
  659. struct be_queue_info *mcc;
  660. unsigned int mcc_events, io_events;
  661. struct be_ctrl_info *ctrl;
  662. struct be_eq_obj *pbe_eq;
  663. int isr, rearm;
  664. phba = dev_id;
  665. ctrl = &phba->ctrl;
  666. isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
  667. (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
  668. if (!isr)
  669. return IRQ_NONE;
  670. phwi_ctrlr = phba->phwi_ctrlr;
  671. phwi_context = phwi_ctrlr->phwi_ctxt;
  672. pbe_eq = &phwi_context->be_eq[0];
  673. eq = &phwi_context->be_eq[0].q;
  674. mcc = &phba->ctrl.mcc_obj.cq;
  675. eqe = queue_tail_node(eq);
  676. io_events = 0;
  677. mcc_events = 0;
  678. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  679. & EQE_VALID_MASK) {
  680. if (((eqe->dw[offsetof(struct amap_eq_entry,
  681. resource_id) / 32] & EQE_RESID_MASK) >> 16) == mcc->id)
  682. mcc_events++;
  683. else
  684. io_events++;
  685. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  686. queue_tail_inc(eq);
  687. eqe = queue_tail_node(eq);
  688. }
  689. if (!io_events && !mcc_events)
  690. return IRQ_NONE;
  691. /* no need to rearm if interrupt is only for IOs */
  692. rearm = 0;
  693. if (mcc_events) {
  694. queue_work(phba->wq, &pbe_eq->mcc_work);
  695. /* rearm for MCCQ */
  696. rearm = 1;
  697. }
  698. if (io_events)
  699. irq_poll_sched(&pbe_eq->iopoll);
  700. hwi_ring_eq_db(phba, eq->id, 0, (io_events + mcc_events), rearm, 1);
  701. return IRQ_HANDLED;
  702. }
  703. static void beiscsi_free_irqs(struct beiscsi_hba *phba)
  704. {
  705. struct hwi_context_memory *phwi_context;
  706. int i;
  707. if (!phba->pcidev->msix_enabled) {
  708. if (phba->pcidev->irq)
  709. free_irq(phba->pcidev->irq, phba);
  710. return;
  711. }
  712. phwi_context = phba->phwi_ctrlr->phwi_ctxt;
  713. for (i = 0; i <= phba->num_cpus; i++) {
  714. free_irq(pci_irq_vector(phba->pcidev, i),
  715. &phwi_context->be_eq[i]);
  716. kfree(phba->msi_name[i]);
  717. }
  718. }
  719. static int beiscsi_init_irqs(struct beiscsi_hba *phba)
  720. {
  721. struct pci_dev *pcidev = phba->pcidev;
  722. struct hwi_controller *phwi_ctrlr;
  723. struct hwi_context_memory *phwi_context;
  724. int ret, i, j;
  725. phwi_ctrlr = phba->phwi_ctrlr;
  726. phwi_context = phwi_ctrlr->phwi_ctxt;
  727. if (pcidev->msix_enabled) {
  728. for (i = 0; i < phba->num_cpus; i++) {
  729. phba->msi_name[i] = kasprintf(GFP_KERNEL,
  730. "beiscsi_%02x_%02x",
  731. phba->shost->host_no, i);
  732. if (!phba->msi_name[i]) {
  733. ret = -ENOMEM;
  734. goto free_msix_irqs;
  735. }
  736. ret = request_irq(pci_irq_vector(pcidev, i),
  737. be_isr_msix, 0, phba->msi_name[i],
  738. &phwi_context->be_eq[i]);
  739. if (ret) {
  740. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  741. "BM_%d : beiscsi_init_irqs-Failed to"
  742. "register msix for i = %d\n",
  743. i);
  744. kfree(phba->msi_name[i]);
  745. goto free_msix_irqs;
  746. }
  747. }
  748. phba->msi_name[i] = kasprintf(GFP_KERNEL, "beiscsi_mcc_%02x",
  749. phba->shost->host_no);
  750. if (!phba->msi_name[i]) {
  751. ret = -ENOMEM;
  752. goto free_msix_irqs;
  753. }
  754. ret = request_irq(pci_irq_vector(pcidev, i), be_isr_mcc, 0,
  755. phba->msi_name[i], &phwi_context->be_eq[i]);
  756. if (ret) {
  757. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
  758. "BM_%d : beiscsi_init_irqs-"
  759. "Failed to register beiscsi_msix_mcc\n");
  760. kfree(phba->msi_name[i]);
  761. goto free_msix_irqs;
  762. }
  763. } else {
  764. ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
  765. "beiscsi", phba);
  766. if (ret) {
  767. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  768. "BM_%d : beiscsi_init_irqs-"
  769. "Failed to register irq\\n");
  770. return ret;
  771. }
  772. }
  773. return 0;
  774. free_msix_irqs:
  775. for (j = i - 1; j >= 0; j--) {
  776. free_irq(pci_irq_vector(pcidev, i), &phwi_context->be_eq[j]);
  777. kfree(phba->msi_name[j]);
  778. }
  779. return ret;
  780. }
  781. void hwi_ring_cq_db(struct beiscsi_hba *phba,
  782. unsigned int id, unsigned int num_processed,
  783. unsigned char rearm)
  784. {
  785. u32 val = 0;
  786. if (rearm)
  787. val |= 1 << DB_CQ_REARM_SHIFT;
  788. val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
  789. /* Setting lower order CQ_ID Bits */
  790. val |= (id & DB_CQ_RING_ID_LOW_MASK);
  791. /* Setting Higher order CQ_ID Bits */
  792. val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
  793. DB_CQ_RING_ID_HIGH_MASK)
  794. << DB_CQ_HIGH_SET_SHIFT);
  795. iowrite32(val, phba->db_va + DB_CQ_OFFSET);
  796. }
  797. static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
  798. {
  799. struct sgl_handle *psgl_handle;
  800. unsigned long flags;
  801. spin_lock_irqsave(&phba->io_sgl_lock, flags);
  802. if (phba->io_sgl_hndl_avbl) {
  803. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  804. "BM_%d : In alloc_io_sgl_handle,"
  805. " io_sgl_alloc_index=%d\n",
  806. phba->io_sgl_alloc_index);
  807. psgl_handle = phba->io_sgl_hndl_base[phba->
  808. io_sgl_alloc_index];
  809. phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
  810. phba->io_sgl_hndl_avbl--;
  811. if (phba->io_sgl_alloc_index == (phba->params.
  812. ios_per_ctrl - 1))
  813. phba->io_sgl_alloc_index = 0;
  814. else
  815. phba->io_sgl_alloc_index++;
  816. } else
  817. psgl_handle = NULL;
  818. spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
  819. return psgl_handle;
  820. }
  821. static void
  822. free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  823. {
  824. unsigned long flags;
  825. spin_lock_irqsave(&phba->io_sgl_lock, flags);
  826. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  827. "BM_%d : In free_,io_sgl_free_index=%d\n",
  828. phba->io_sgl_free_index);
  829. if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
  830. /*
  831. * this can happen if clean_task is called on a task that
  832. * failed in xmit_task or alloc_pdu.
  833. */
  834. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
  835. "BM_%d : Double Free in IO SGL io_sgl_free_index=%d, value there=%p\n",
  836. phba->io_sgl_free_index,
  837. phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
  838. spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
  839. return;
  840. }
  841. phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
  842. phba->io_sgl_hndl_avbl++;
  843. if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
  844. phba->io_sgl_free_index = 0;
  845. else
  846. phba->io_sgl_free_index++;
  847. spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
  848. }
  849. static inline struct wrb_handle *
  850. beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
  851. unsigned int wrbs_per_cxn)
  852. {
  853. struct wrb_handle *pwrb_handle;
  854. unsigned long flags;
  855. spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
  856. if (!pwrb_context->wrb_handles_available) {
  857. spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
  858. return NULL;
  859. }
  860. pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
  861. pwrb_context->wrb_handles_available--;
  862. if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
  863. pwrb_context->alloc_index = 0;
  864. else
  865. pwrb_context->alloc_index++;
  866. spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
  867. if (pwrb_handle)
  868. memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
  869. return pwrb_handle;
  870. }
  871. /**
  872. * alloc_wrb_handle - To allocate a wrb handle
  873. * @phba: The hba pointer
  874. * @cid: The cid to use for allocation
  875. * @pwrb_context: ptr to ptr to wrb context
  876. *
  877. * This happens under session_lock until submission to chip
  878. */
  879. struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
  880. struct hwi_wrb_context **pcontext)
  881. {
  882. struct hwi_wrb_context *pwrb_context;
  883. struct hwi_controller *phwi_ctrlr;
  884. uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
  885. phwi_ctrlr = phba->phwi_ctrlr;
  886. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  887. /* return the context address */
  888. *pcontext = pwrb_context;
  889. return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
  890. }
  891. static inline void
  892. beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
  893. struct wrb_handle *pwrb_handle,
  894. unsigned int wrbs_per_cxn)
  895. {
  896. unsigned long flags;
  897. spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
  898. pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
  899. pwrb_context->wrb_handles_available++;
  900. if (pwrb_context->free_index == (wrbs_per_cxn - 1))
  901. pwrb_context->free_index = 0;
  902. else
  903. pwrb_context->free_index++;
  904. pwrb_handle->pio_handle = NULL;
  905. spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
  906. }
  907. /**
  908. * free_wrb_handle - To free the wrb handle back to pool
  909. * @phba: The hba pointer
  910. * @pwrb_context: The context to free from
  911. * @pwrb_handle: The wrb_handle to free
  912. *
  913. * This happens under session_lock until submission to chip
  914. */
  915. static void
  916. free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
  917. struct wrb_handle *pwrb_handle)
  918. {
  919. beiscsi_put_wrb_handle(pwrb_context,
  920. pwrb_handle,
  921. phba->params.wrbs_per_cxn);
  922. beiscsi_log(phba, KERN_INFO,
  923. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  924. "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
  925. "wrb_handles_available=%d\n",
  926. pwrb_handle, pwrb_context->free_index,
  927. pwrb_context->wrb_handles_available);
  928. }
  929. static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
  930. {
  931. struct sgl_handle *psgl_handle;
  932. unsigned long flags;
  933. spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
  934. if (phba->eh_sgl_hndl_avbl) {
  935. psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
  936. phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
  937. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  938. "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
  939. phba->eh_sgl_alloc_index,
  940. phba->eh_sgl_alloc_index);
  941. phba->eh_sgl_hndl_avbl--;
  942. if (phba->eh_sgl_alloc_index ==
  943. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
  944. 1))
  945. phba->eh_sgl_alloc_index = 0;
  946. else
  947. phba->eh_sgl_alloc_index++;
  948. } else
  949. psgl_handle = NULL;
  950. spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
  951. return psgl_handle;
  952. }
  953. void
  954. free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
  955. {
  956. unsigned long flags;
  957. spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
  958. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  959. "BM_%d : In free_mgmt_sgl_handle,"
  960. "eh_sgl_free_index=%d\n",
  961. phba->eh_sgl_free_index);
  962. if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
  963. /*
  964. * this can happen if clean_task is called on a task that
  965. * failed in xmit_task or alloc_pdu.
  966. */
  967. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
  968. "BM_%d : Double Free in eh SGL ,"
  969. "eh_sgl_free_index=%d\n",
  970. phba->eh_sgl_free_index);
  971. spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
  972. return;
  973. }
  974. phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
  975. phba->eh_sgl_hndl_avbl++;
  976. if (phba->eh_sgl_free_index ==
  977. (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
  978. phba->eh_sgl_free_index = 0;
  979. else
  980. phba->eh_sgl_free_index++;
  981. spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
  982. }
  983. static void
  984. be_complete_io(struct beiscsi_conn *beiscsi_conn,
  985. struct iscsi_task *task,
  986. struct common_sol_cqe *csol_cqe)
  987. {
  988. struct beiscsi_io_task *io_task = task->dd_data;
  989. struct be_status_bhs *sts_bhs =
  990. (struct be_status_bhs *)io_task->cmd_bhs;
  991. struct iscsi_conn *conn = beiscsi_conn->conn;
  992. unsigned char *sense;
  993. u32 resid = 0, exp_cmdsn, max_cmdsn;
  994. u8 rsp, status, flags;
  995. exp_cmdsn = csol_cqe->exp_cmdsn;
  996. max_cmdsn = (csol_cqe->exp_cmdsn +
  997. csol_cqe->cmd_wnd - 1);
  998. rsp = csol_cqe->i_resp;
  999. status = csol_cqe->i_sts;
  1000. flags = csol_cqe->i_flags;
  1001. resid = csol_cqe->res_cnt;
  1002. if (!task->sc) {
  1003. if (io_task->scsi_cmnd) {
  1004. scsi_dma_unmap(io_task->scsi_cmnd);
  1005. io_task->scsi_cmnd = NULL;
  1006. }
  1007. return;
  1008. }
  1009. task->sc->result = (DID_OK << 16) | status;
  1010. if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
  1011. task->sc->result = DID_ERROR << 16;
  1012. goto unmap;
  1013. }
  1014. /* bidi not initially supported */
  1015. if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
  1016. if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
  1017. task->sc->result = DID_ERROR << 16;
  1018. if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  1019. scsi_set_resid(task->sc, resid);
  1020. if (!status && (scsi_bufflen(task->sc) - resid <
  1021. task->sc->underflow))
  1022. task->sc->result = DID_ERROR << 16;
  1023. }
  1024. }
  1025. if (status == SAM_STAT_CHECK_CONDITION) {
  1026. u16 sense_len;
  1027. unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
  1028. sense = sts_bhs->sense_info + sizeof(unsigned short);
  1029. sense_len = be16_to_cpu(*slen);
  1030. memcpy(task->sc->sense_buffer, sense,
  1031. min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
  1032. }
  1033. if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
  1034. conn->rxdata_octets += resid;
  1035. unmap:
  1036. if (io_task->scsi_cmnd) {
  1037. scsi_dma_unmap(io_task->scsi_cmnd);
  1038. io_task->scsi_cmnd = NULL;
  1039. }
  1040. iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
  1041. }
  1042. static void
  1043. be_complete_logout(struct beiscsi_conn *beiscsi_conn,
  1044. struct iscsi_task *task,
  1045. struct common_sol_cqe *csol_cqe)
  1046. {
  1047. struct iscsi_logout_rsp *hdr;
  1048. struct beiscsi_io_task *io_task = task->dd_data;
  1049. struct iscsi_conn *conn = beiscsi_conn->conn;
  1050. hdr = (struct iscsi_logout_rsp *)task->hdr;
  1051. hdr->opcode = ISCSI_OP_LOGOUT_RSP;
  1052. hdr->t2wait = 5;
  1053. hdr->t2retain = 0;
  1054. hdr->flags = csol_cqe->i_flags;
  1055. hdr->response = csol_cqe->i_resp;
  1056. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1057. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1058. csol_cqe->cmd_wnd - 1);
  1059. hdr->dlength[0] = 0;
  1060. hdr->dlength[1] = 0;
  1061. hdr->dlength[2] = 0;
  1062. hdr->hlength = 0;
  1063. hdr->itt = io_task->libiscsi_itt;
  1064. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1065. }
  1066. static void
  1067. be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
  1068. struct iscsi_task *task,
  1069. struct common_sol_cqe *csol_cqe)
  1070. {
  1071. struct iscsi_tm_rsp *hdr;
  1072. struct iscsi_conn *conn = beiscsi_conn->conn;
  1073. struct beiscsi_io_task *io_task = task->dd_data;
  1074. hdr = (struct iscsi_tm_rsp *)task->hdr;
  1075. hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
  1076. hdr->flags = csol_cqe->i_flags;
  1077. hdr->response = csol_cqe->i_resp;
  1078. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1079. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1080. csol_cqe->cmd_wnd - 1);
  1081. hdr->itt = io_task->libiscsi_itt;
  1082. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1083. }
  1084. static void
  1085. hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
  1086. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1087. {
  1088. struct hwi_wrb_context *pwrb_context;
  1089. uint16_t wrb_index, cid, cri_index;
  1090. struct hwi_controller *phwi_ctrlr;
  1091. struct wrb_handle *pwrb_handle;
  1092. struct iscsi_session *session;
  1093. struct iscsi_task *task;
  1094. phwi_ctrlr = phba->phwi_ctrlr;
  1095. if (is_chip_be2_be3r(phba)) {
  1096. wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
  1097. wrb_idx, psol);
  1098. cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
  1099. cid, psol);
  1100. } else {
  1101. wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
  1102. wrb_idx, psol);
  1103. cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
  1104. cid, psol);
  1105. }
  1106. cri_index = BE_GET_CRI_FROM_CID(cid);
  1107. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  1108. pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
  1109. session = beiscsi_conn->conn->session;
  1110. spin_lock_bh(&session->back_lock);
  1111. task = pwrb_handle->pio_handle;
  1112. if (task)
  1113. __iscsi_put_task(task);
  1114. spin_unlock_bh(&session->back_lock);
  1115. }
  1116. static void
  1117. be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
  1118. struct iscsi_task *task,
  1119. struct common_sol_cqe *csol_cqe)
  1120. {
  1121. struct iscsi_nopin *hdr;
  1122. struct iscsi_conn *conn = beiscsi_conn->conn;
  1123. struct beiscsi_io_task *io_task = task->dd_data;
  1124. hdr = (struct iscsi_nopin *)task->hdr;
  1125. hdr->flags = csol_cqe->i_flags;
  1126. hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
  1127. hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
  1128. csol_cqe->cmd_wnd - 1);
  1129. hdr->opcode = ISCSI_OP_NOOP_IN;
  1130. hdr->itt = io_task->libiscsi_itt;
  1131. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
  1132. }
  1133. static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
  1134. struct sol_cqe *psol,
  1135. struct common_sol_cqe *csol_cqe)
  1136. {
  1137. if (is_chip_be2_be3r(phba)) {
  1138. csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
  1139. i_exp_cmd_sn, psol);
  1140. csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
  1141. i_res_cnt, psol);
  1142. csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
  1143. i_cmd_wnd, psol);
  1144. csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
  1145. wrb_index, psol);
  1146. csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
  1147. cid, psol);
  1148. csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
  1149. hw_sts, psol);
  1150. csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
  1151. i_resp, psol);
  1152. csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
  1153. i_sts, psol);
  1154. csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
  1155. i_flags, psol);
  1156. } else {
  1157. csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1158. i_exp_cmd_sn, psol);
  1159. csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1160. i_res_cnt, psol);
  1161. csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1162. wrb_index, psol);
  1163. csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1164. cid, psol);
  1165. csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1166. hw_sts, psol);
  1167. csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1168. i_cmd_wnd, psol);
  1169. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1170. cmd_cmpl, psol))
  1171. csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1172. i_sts, psol);
  1173. else
  1174. csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1175. i_sts, psol);
  1176. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1177. u, psol))
  1178. csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
  1179. if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1180. o, psol))
  1181. csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
  1182. }
  1183. }
  1184. static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
  1185. struct beiscsi_hba *phba, struct sol_cqe *psol)
  1186. {
  1187. struct iscsi_conn *conn = beiscsi_conn->conn;
  1188. struct iscsi_session *session = conn->session;
  1189. struct common_sol_cqe csol_cqe = {0};
  1190. struct hwi_wrb_context *pwrb_context;
  1191. struct hwi_controller *phwi_ctrlr;
  1192. struct wrb_handle *pwrb_handle;
  1193. struct iscsi_task *task;
  1194. uint16_t cri_index = 0;
  1195. uint8_t type;
  1196. phwi_ctrlr = phba->phwi_ctrlr;
  1197. /* Copy the elements to a common structure */
  1198. adapter_get_sol_cqe(phba, psol, &csol_cqe);
  1199. cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
  1200. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  1201. pwrb_handle = pwrb_context->pwrb_handle_basestd[
  1202. csol_cqe.wrb_index];
  1203. spin_lock_bh(&session->back_lock);
  1204. task = pwrb_handle->pio_handle;
  1205. if (!task) {
  1206. spin_unlock_bh(&session->back_lock);
  1207. return;
  1208. }
  1209. type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
  1210. switch (type) {
  1211. case HWH_TYPE_IO:
  1212. case HWH_TYPE_IO_RD:
  1213. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
  1214. ISCSI_OP_NOOP_OUT)
  1215. be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
  1216. else
  1217. be_complete_io(beiscsi_conn, task, &csol_cqe);
  1218. break;
  1219. case HWH_TYPE_LOGOUT:
  1220. if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  1221. be_complete_logout(beiscsi_conn, task, &csol_cqe);
  1222. else
  1223. be_complete_tmf(beiscsi_conn, task, &csol_cqe);
  1224. break;
  1225. case HWH_TYPE_LOGIN:
  1226. beiscsi_log(phba, KERN_ERR,
  1227. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1228. "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
  1229. " hwi_complete_cmd- Solicited path\n");
  1230. break;
  1231. case HWH_TYPE_NOP:
  1232. be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
  1233. break;
  1234. default:
  1235. beiscsi_log(phba, KERN_WARNING,
  1236. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1237. "BM_%d : In hwi_complete_cmd, unknown type = %d"
  1238. "wrb_index 0x%x CID 0x%x\n", type,
  1239. csol_cqe.wrb_index,
  1240. csol_cqe.cid);
  1241. break;
  1242. }
  1243. spin_unlock_bh(&session->back_lock);
  1244. }
  1245. /**
  1246. * ASYNC PDUs include
  1247. * a. Unsolicited NOP-In (target initiated NOP-In)
  1248. * b. ASYNC Messages
  1249. * c. Reject PDU
  1250. * d. Login response
  1251. * These headers arrive unprocessed by the EP firmware.
  1252. * iSCSI layer processes them.
  1253. */
  1254. static unsigned int
  1255. beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
  1256. struct pdu_base *phdr, void *pdata, unsigned int dlen)
  1257. {
  1258. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1259. struct iscsi_conn *conn = beiscsi_conn->conn;
  1260. struct beiscsi_io_task *io_task;
  1261. struct iscsi_hdr *login_hdr;
  1262. struct iscsi_task *task;
  1263. u8 code;
  1264. code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
  1265. switch (code) {
  1266. case ISCSI_OP_NOOP_IN:
  1267. pdata = NULL;
  1268. dlen = 0;
  1269. break;
  1270. case ISCSI_OP_ASYNC_EVENT:
  1271. break;
  1272. case ISCSI_OP_REJECT:
  1273. WARN_ON(!pdata);
  1274. WARN_ON(!(dlen == 48));
  1275. beiscsi_log(phba, KERN_ERR,
  1276. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1277. "BM_%d : In ISCSI_OP_REJECT\n");
  1278. break;
  1279. case ISCSI_OP_LOGIN_RSP:
  1280. case ISCSI_OP_TEXT_RSP:
  1281. task = conn->login_task;
  1282. io_task = task->dd_data;
  1283. login_hdr = (struct iscsi_hdr *)phdr;
  1284. login_hdr->itt = io_task->libiscsi_itt;
  1285. break;
  1286. default:
  1287. beiscsi_log(phba, KERN_WARNING,
  1288. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1289. "BM_%d : unrecognized async PDU opcode 0x%x\n",
  1290. code);
  1291. return 1;
  1292. }
  1293. __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
  1294. return 0;
  1295. }
  1296. static inline void
  1297. beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
  1298. struct hd_async_handle *pasync_handle)
  1299. {
  1300. pasync_handle->is_final = 0;
  1301. pasync_handle->buffer_len = 0;
  1302. pasync_handle->in_use = 0;
  1303. list_del_init(&pasync_handle->link);
  1304. }
  1305. static void
  1306. beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
  1307. struct hd_async_context *pasync_ctx,
  1308. u16 cri)
  1309. {
  1310. struct hd_async_handle *pasync_handle, *tmp_handle;
  1311. struct list_head *plist;
  1312. plist = &pasync_ctx->async_entry[cri].wq.list;
  1313. list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link)
  1314. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1315. INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
  1316. pasync_ctx->async_entry[cri].wq.hdr_len = 0;
  1317. pasync_ctx->async_entry[cri].wq.bytes_received = 0;
  1318. pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
  1319. }
  1320. static struct hd_async_handle *
  1321. beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
  1322. struct hd_async_context *pasync_ctx,
  1323. struct i_t_dpdu_cqe *pdpdu_cqe,
  1324. u8 *header)
  1325. {
  1326. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1327. struct hd_async_handle *pasync_handle;
  1328. struct be_bus_address phys_addr;
  1329. u16 cid, code, ci, cri;
  1330. u8 final, error = 0;
  1331. u32 dpl;
  1332. cid = beiscsi_conn->beiscsi_conn_cid;
  1333. cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
  1334. /**
  1335. * This function is invoked to get the right async_handle structure
  1336. * from a given DEF PDU CQ entry.
  1337. *
  1338. * - index in CQ entry gives the vertical index
  1339. * - address in CQ entry is the offset where the DMA last ended
  1340. * - final - no more notifications for this PDU
  1341. */
  1342. if (is_chip_be2_be3r(phba)) {
  1343. dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1344. dpl, pdpdu_cqe);
  1345. ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1346. index, pdpdu_cqe);
  1347. final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1348. final, pdpdu_cqe);
  1349. } else {
  1350. dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1351. dpl, pdpdu_cqe);
  1352. ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1353. index, pdpdu_cqe);
  1354. final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1355. final, pdpdu_cqe);
  1356. }
  1357. /**
  1358. * DB addr Hi/Lo is same for BE and SKH.
  1359. * Subtract the dataplacementlength to get to the base.
  1360. */
  1361. phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1362. db_addr_lo, pdpdu_cqe);
  1363. phys_addr.u.a32.address_lo -= dpl;
  1364. phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1365. db_addr_hi, pdpdu_cqe);
  1366. code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
  1367. switch (code) {
  1368. case UNSOL_HDR_NOTIFY:
  1369. pasync_handle = pasync_ctx->async_entry[ci].header;
  1370. *header = 1;
  1371. break;
  1372. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1373. error = 1;
  1374. case UNSOL_DATA_NOTIFY:
  1375. pasync_handle = pasync_ctx->async_entry[ci].data;
  1376. break;
  1377. /* called only for above codes */
  1378. default:
  1379. return NULL;
  1380. }
  1381. if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
  1382. pasync_handle->index != ci) {
  1383. /* driver bug - if ci does not match async handle index */
  1384. error = 1;
  1385. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1386. "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
  1387. cid, pasync_handle->is_header ? 'H' : 'D',
  1388. pasync_handle->pa.u.a64.address,
  1389. pasync_handle->index,
  1390. phys_addr.u.a64.address, ci);
  1391. /* FW has stale address - attempt continuing by dropping */
  1392. }
  1393. /**
  1394. * DEF PDU header and data buffers with errors should be simply
  1395. * dropped as there are no consumers for it.
  1396. */
  1397. if (error) {
  1398. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1399. return NULL;
  1400. }
  1401. if (pasync_handle->in_use || !list_empty(&pasync_handle->link)) {
  1402. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1403. "BM_%d : cid %d async PDU handle in use - code %d ci %d addr %llx\n",
  1404. cid, code, ci, phys_addr.u.a64.address);
  1405. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1406. }
  1407. list_del_init(&pasync_handle->link);
  1408. /**
  1409. * Each CID is associated with unique CRI.
  1410. * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
  1411. **/
  1412. pasync_handle->cri = cri;
  1413. pasync_handle->is_final = final;
  1414. pasync_handle->buffer_len = dpl;
  1415. pasync_handle->in_use = 1;
  1416. return pasync_handle;
  1417. }
  1418. static unsigned int
  1419. beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
  1420. struct hd_async_context *pasync_ctx,
  1421. u16 cri)
  1422. {
  1423. struct iscsi_session *session = beiscsi_conn->conn->session;
  1424. struct hd_async_handle *pasync_handle, *plast_handle;
  1425. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1426. void *phdr = NULL, *pdata = NULL;
  1427. u32 dlen = 0, status = 0;
  1428. struct list_head *plist;
  1429. plist = &pasync_ctx->async_entry[cri].wq.list;
  1430. plast_handle = NULL;
  1431. list_for_each_entry(pasync_handle, plist, link) {
  1432. plast_handle = pasync_handle;
  1433. /* get the header, the first entry */
  1434. if (!phdr) {
  1435. phdr = pasync_handle->pbuffer;
  1436. continue;
  1437. }
  1438. /* use first buffer to collect all the data */
  1439. if (!pdata) {
  1440. pdata = pasync_handle->pbuffer;
  1441. dlen = pasync_handle->buffer_len;
  1442. continue;
  1443. }
  1444. if (!pasync_handle->buffer_len ||
  1445. (dlen + pasync_handle->buffer_len) >
  1446. pasync_ctx->async_data.buffer_size)
  1447. break;
  1448. memcpy(pdata + dlen, pasync_handle->pbuffer,
  1449. pasync_handle->buffer_len);
  1450. dlen += pasync_handle->buffer_len;
  1451. }
  1452. if (!plast_handle->is_final) {
  1453. /* last handle should have final PDU notification from FW */
  1454. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1455. "BM_%d : cid %u %p fwd async PDU opcode %x with last handle missing - HL%u:DN%u:DR%u\n",
  1456. beiscsi_conn->beiscsi_conn_cid, plast_handle,
  1457. AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr),
  1458. pasync_ctx->async_entry[cri].wq.hdr_len,
  1459. pasync_ctx->async_entry[cri].wq.bytes_needed,
  1460. pasync_ctx->async_entry[cri].wq.bytes_received);
  1461. }
  1462. spin_lock_bh(&session->back_lock);
  1463. status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
  1464. spin_unlock_bh(&session->back_lock);
  1465. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1466. return status;
  1467. }
  1468. static unsigned int
  1469. beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
  1470. struct hd_async_context *pasync_ctx,
  1471. struct hd_async_handle *pasync_handle)
  1472. {
  1473. unsigned int bytes_needed = 0, status = 0;
  1474. u16 cri = pasync_handle->cri;
  1475. struct cri_wait_queue *wq;
  1476. struct beiscsi_hba *phba;
  1477. struct pdu_base *ppdu;
  1478. char *err = "";
  1479. phba = beiscsi_conn->phba;
  1480. wq = &pasync_ctx->async_entry[cri].wq;
  1481. if (pasync_handle->is_header) {
  1482. /* check if PDU hdr is rcv'd when old hdr not completed */
  1483. if (wq->hdr_len) {
  1484. err = "incomplete";
  1485. goto drop_pdu;
  1486. }
  1487. ppdu = pasync_handle->pbuffer;
  1488. bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
  1489. data_len_hi, ppdu);
  1490. bytes_needed <<= 16;
  1491. bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
  1492. data_len_lo, ppdu));
  1493. wq->hdr_len = pasync_handle->buffer_len;
  1494. wq->bytes_received = 0;
  1495. wq->bytes_needed = bytes_needed;
  1496. list_add_tail(&pasync_handle->link, &wq->list);
  1497. if (!bytes_needed)
  1498. status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
  1499. pasync_ctx, cri);
  1500. } else {
  1501. /* check if data received has header and is needed */
  1502. if (!wq->hdr_len || !wq->bytes_needed) {
  1503. err = "header less";
  1504. goto drop_pdu;
  1505. }
  1506. wq->bytes_received += pasync_handle->buffer_len;
  1507. /* Something got overwritten? Better catch it here. */
  1508. if (wq->bytes_received > wq->bytes_needed) {
  1509. err = "overflow";
  1510. goto drop_pdu;
  1511. }
  1512. list_add_tail(&pasync_handle->link, &wq->list);
  1513. if (wq->bytes_received == wq->bytes_needed)
  1514. status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
  1515. pasync_ctx, cri);
  1516. }
  1517. return status;
  1518. drop_pdu:
  1519. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
  1520. "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
  1521. beiscsi_conn->beiscsi_conn_cid, err,
  1522. pasync_handle->is_header ? 'H' : 'D',
  1523. wq->hdr_len, wq->bytes_needed,
  1524. pasync_handle->buffer_len);
  1525. /* discard this handle */
  1526. beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
  1527. /* free all the other handles in cri_wait_queue */
  1528. beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
  1529. /* try continuing */
  1530. return status;
  1531. }
  1532. static void
  1533. beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
  1534. u8 header, u8 ulp_num, u16 nbuf)
  1535. {
  1536. struct hd_async_handle *pasync_handle;
  1537. struct hd_async_context *pasync_ctx;
  1538. struct hwi_controller *phwi_ctrlr;
  1539. struct phys_addr *pasync_sge;
  1540. u32 ring_id, doorbell = 0;
  1541. u32 doorbell_offset;
  1542. u16 prod, pi;
  1543. phwi_ctrlr = phba->phwi_ctrlr;
  1544. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
  1545. if (header) {
  1546. pasync_sge = pasync_ctx->async_header.ring_base;
  1547. pi = pasync_ctx->async_header.pi;
  1548. ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
  1549. doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
  1550. doorbell_offset;
  1551. } else {
  1552. pasync_sge = pasync_ctx->async_data.ring_base;
  1553. pi = pasync_ctx->async_data.pi;
  1554. ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
  1555. doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
  1556. doorbell_offset;
  1557. }
  1558. for (prod = 0; prod < nbuf; prod++) {
  1559. if (header)
  1560. pasync_handle = pasync_ctx->async_entry[pi].header;
  1561. else
  1562. pasync_handle = pasync_ctx->async_entry[pi].data;
  1563. WARN_ON(pasync_handle->is_header != header);
  1564. WARN_ON(pasync_handle->index != pi);
  1565. /* setup the ring only once */
  1566. if (nbuf == pasync_ctx->num_entries) {
  1567. /* note hi is lo */
  1568. pasync_sge[pi].hi = pasync_handle->pa.u.a32.address_lo;
  1569. pasync_sge[pi].lo = pasync_handle->pa.u.a32.address_hi;
  1570. }
  1571. if (++pi == pasync_ctx->num_entries)
  1572. pi = 0;
  1573. }
  1574. if (header)
  1575. pasync_ctx->async_header.pi = pi;
  1576. else
  1577. pasync_ctx->async_data.pi = pi;
  1578. doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
  1579. doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
  1580. doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
  1581. doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
  1582. iowrite32(doorbell, phba->db_va + doorbell_offset);
  1583. }
  1584. static void
  1585. beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
  1586. struct i_t_dpdu_cqe *pdpdu_cqe)
  1587. {
  1588. struct beiscsi_hba *phba = beiscsi_conn->phba;
  1589. struct hd_async_handle *pasync_handle = NULL;
  1590. struct hd_async_context *pasync_ctx;
  1591. struct hwi_controller *phwi_ctrlr;
  1592. u8 ulp_num, consumed, header = 0;
  1593. u16 cid_cri;
  1594. phwi_ctrlr = phba->phwi_ctrlr;
  1595. cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
  1596. ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
  1597. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
  1598. pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
  1599. pdpdu_cqe, &header);
  1600. if (is_chip_be2_be3r(phba))
  1601. consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
  1602. num_cons, pdpdu_cqe);
  1603. else
  1604. consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
  1605. num_cons, pdpdu_cqe);
  1606. if (pasync_handle)
  1607. beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
  1608. /* num_cons indicates number of 8 RQEs consumed */
  1609. if (consumed)
  1610. beiscsi_hdq_post_handles(phba, header, ulp_num, 8 * consumed);
  1611. }
  1612. void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
  1613. {
  1614. struct be_queue_info *mcc_cq;
  1615. struct be_mcc_compl *mcc_compl;
  1616. unsigned int num_processed = 0;
  1617. mcc_cq = &phba->ctrl.mcc_obj.cq;
  1618. mcc_compl = queue_tail_node(mcc_cq);
  1619. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1620. while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
  1621. if (beiscsi_hba_in_error(phba))
  1622. return;
  1623. if (num_processed >= 32) {
  1624. hwi_ring_cq_db(phba, mcc_cq->id,
  1625. num_processed, 0);
  1626. num_processed = 0;
  1627. }
  1628. if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
  1629. beiscsi_process_async_event(phba, mcc_compl);
  1630. } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
  1631. beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
  1632. }
  1633. mcc_compl->flags = 0;
  1634. queue_tail_inc(mcc_cq);
  1635. mcc_compl = queue_tail_node(mcc_cq);
  1636. mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
  1637. num_processed++;
  1638. }
  1639. if (num_processed > 0)
  1640. hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
  1641. }
  1642. static void beiscsi_mcc_work(struct work_struct *work)
  1643. {
  1644. struct be_eq_obj *pbe_eq;
  1645. struct beiscsi_hba *phba;
  1646. pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
  1647. phba = pbe_eq->phba;
  1648. beiscsi_process_mcc_cq(phba);
  1649. /* rearm EQ for further interrupts */
  1650. if (!beiscsi_hba_in_error(phba))
  1651. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1652. }
  1653. /**
  1654. * beiscsi_process_cq()- Process the Completion Queue
  1655. * @pbe_eq: Event Q on which the Completion has come
  1656. * @budget: Max number of events to processed
  1657. *
  1658. * return
  1659. * Number of Completion Entries processed.
  1660. **/
  1661. unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
  1662. {
  1663. struct be_queue_info *cq;
  1664. struct sol_cqe *sol;
  1665. struct dmsg_cqe *dmsg;
  1666. unsigned int total = 0;
  1667. unsigned int num_processed = 0;
  1668. unsigned short code = 0, cid = 0;
  1669. uint16_t cri_index = 0;
  1670. struct beiscsi_conn *beiscsi_conn;
  1671. struct beiscsi_endpoint *beiscsi_ep;
  1672. struct iscsi_endpoint *ep;
  1673. struct beiscsi_hba *phba;
  1674. cq = pbe_eq->cq;
  1675. sol = queue_tail_node(cq);
  1676. phba = pbe_eq->phba;
  1677. while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
  1678. CQE_VALID_MASK) {
  1679. if (beiscsi_hba_in_error(phba))
  1680. return 0;
  1681. be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
  1682. code = (sol->dw[offsetof(struct amap_sol_cqe, code) / 32] &
  1683. CQE_CODE_MASK);
  1684. /* Get the CID */
  1685. if (is_chip_be2_be3r(phba)) {
  1686. cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
  1687. } else {
  1688. if ((code == DRIVERMSG_NOTIFY) ||
  1689. (code == UNSOL_HDR_NOTIFY) ||
  1690. (code == UNSOL_DATA_NOTIFY))
  1691. cid = AMAP_GET_BITS(
  1692. struct amap_i_t_dpdu_cqe_v2,
  1693. cid, sol);
  1694. else
  1695. cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
  1696. cid, sol);
  1697. }
  1698. cri_index = BE_GET_CRI_FROM_CID(cid);
  1699. ep = phba->ep_array[cri_index];
  1700. if (ep == NULL) {
  1701. /* connection has already been freed
  1702. * just move on to next one
  1703. */
  1704. beiscsi_log(phba, KERN_WARNING,
  1705. BEISCSI_LOG_INIT,
  1706. "BM_%d : proc cqe of disconn ep: cid %d\n",
  1707. cid);
  1708. goto proc_next_cqe;
  1709. }
  1710. beiscsi_ep = ep->dd_data;
  1711. beiscsi_conn = beiscsi_ep->conn;
  1712. /* replenish cq */
  1713. if (num_processed == 32) {
  1714. hwi_ring_cq_db(phba, cq->id, 32, 0);
  1715. num_processed = 0;
  1716. }
  1717. total++;
  1718. switch (code) {
  1719. case SOL_CMD_COMPLETE:
  1720. hwi_complete_cmd(beiscsi_conn, phba, sol);
  1721. break;
  1722. case DRIVERMSG_NOTIFY:
  1723. beiscsi_log(phba, KERN_INFO,
  1724. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1725. "BM_%d : Received %s[%d] on CID : %d\n",
  1726. cqe_desc[code], code, cid);
  1727. dmsg = (struct dmsg_cqe *)sol;
  1728. hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
  1729. break;
  1730. case UNSOL_HDR_NOTIFY:
  1731. beiscsi_log(phba, KERN_INFO,
  1732. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1733. "BM_%d : Received %s[%d] on CID : %d\n",
  1734. cqe_desc[code], code, cid);
  1735. spin_lock_bh(&phba->async_pdu_lock);
  1736. beiscsi_hdq_process_compl(beiscsi_conn,
  1737. (struct i_t_dpdu_cqe *)sol);
  1738. spin_unlock_bh(&phba->async_pdu_lock);
  1739. break;
  1740. case UNSOL_DATA_NOTIFY:
  1741. beiscsi_log(phba, KERN_INFO,
  1742. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1743. "BM_%d : Received %s[%d] on CID : %d\n",
  1744. cqe_desc[code], code, cid);
  1745. spin_lock_bh(&phba->async_pdu_lock);
  1746. beiscsi_hdq_process_compl(beiscsi_conn,
  1747. (struct i_t_dpdu_cqe *)sol);
  1748. spin_unlock_bh(&phba->async_pdu_lock);
  1749. break;
  1750. case CXN_INVALIDATE_INDEX_NOTIFY:
  1751. case CMD_INVALIDATED_NOTIFY:
  1752. case CXN_INVALIDATE_NOTIFY:
  1753. beiscsi_log(phba, KERN_ERR,
  1754. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1755. "BM_%d : Ignoring %s[%d] on CID : %d\n",
  1756. cqe_desc[code], code, cid);
  1757. break;
  1758. case CXN_KILLED_HDR_DIGEST_ERR:
  1759. case SOL_CMD_KILLED_DATA_DIGEST_ERR:
  1760. beiscsi_log(phba, KERN_ERR,
  1761. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1762. "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
  1763. cqe_desc[code], code, cid);
  1764. break;
  1765. case CMD_KILLED_INVALID_STATSN_RCVD:
  1766. case CMD_KILLED_INVALID_R2T_RCVD:
  1767. case CMD_CXN_KILLED_LUN_INVALID:
  1768. case CMD_CXN_KILLED_ICD_INVALID:
  1769. case CMD_CXN_KILLED_ITT_INVALID:
  1770. case CMD_CXN_KILLED_SEQ_OUTOFORDER:
  1771. case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
  1772. beiscsi_log(phba, KERN_ERR,
  1773. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1774. "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
  1775. cqe_desc[code], code, cid);
  1776. break;
  1777. case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
  1778. beiscsi_log(phba, KERN_ERR,
  1779. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1780. "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
  1781. cqe_desc[code], code, cid);
  1782. spin_lock_bh(&phba->async_pdu_lock);
  1783. /* driver consumes the entry and drops the contents */
  1784. beiscsi_hdq_process_compl(beiscsi_conn,
  1785. (struct i_t_dpdu_cqe *)sol);
  1786. spin_unlock_bh(&phba->async_pdu_lock);
  1787. break;
  1788. case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
  1789. case CXN_KILLED_BURST_LEN_MISMATCH:
  1790. case CXN_KILLED_AHS_RCVD:
  1791. case CXN_KILLED_UNKNOWN_HDR:
  1792. case CXN_KILLED_STALE_ITT_TTT_RCVD:
  1793. case CXN_KILLED_INVALID_ITT_TTT_RCVD:
  1794. case CXN_KILLED_TIMED_OUT:
  1795. case CXN_KILLED_FIN_RCVD:
  1796. case CXN_KILLED_RST_SENT:
  1797. case CXN_KILLED_RST_RCVD:
  1798. case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
  1799. case CXN_KILLED_BAD_WRB_INDEX_ERROR:
  1800. case CXN_KILLED_OVER_RUN_RESIDUAL:
  1801. case CXN_KILLED_UNDER_RUN_RESIDUAL:
  1802. case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
  1803. beiscsi_log(phba, KERN_ERR,
  1804. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1805. "BM_%d : Event %s[%d] received on CID : %d\n",
  1806. cqe_desc[code], code, cid);
  1807. if (beiscsi_conn)
  1808. iscsi_conn_failure(beiscsi_conn->conn,
  1809. ISCSI_ERR_CONN_FAILED);
  1810. break;
  1811. default:
  1812. beiscsi_log(phba, KERN_ERR,
  1813. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  1814. "BM_%d : Invalid CQE Event Received Code : %d"
  1815. "CID 0x%x...\n",
  1816. code, cid);
  1817. break;
  1818. }
  1819. proc_next_cqe:
  1820. AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
  1821. queue_tail_inc(cq);
  1822. sol = queue_tail_node(cq);
  1823. num_processed++;
  1824. if (total == budget)
  1825. break;
  1826. }
  1827. hwi_ring_cq_db(phba, cq->id, num_processed, 1);
  1828. return total;
  1829. }
  1830. static int be_iopoll(struct irq_poll *iop, int budget)
  1831. {
  1832. unsigned int ret, io_events;
  1833. struct beiscsi_hba *phba;
  1834. struct be_eq_obj *pbe_eq;
  1835. struct be_eq_entry *eqe = NULL;
  1836. struct be_queue_info *eq;
  1837. pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
  1838. phba = pbe_eq->phba;
  1839. if (beiscsi_hba_in_error(phba)) {
  1840. irq_poll_complete(iop);
  1841. return 0;
  1842. }
  1843. io_events = 0;
  1844. eq = &pbe_eq->q;
  1845. eqe = queue_tail_node(eq);
  1846. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
  1847. EQE_VALID_MASK) {
  1848. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  1849. queue_tail_inc(eq);
  1850. eqe = queue_tail_node(eq);
  1851. io_events++;
  1852. }
  1853. hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
  1854. ret = beiscsi_process_cq(pbe_eq, budget);
  1855. pbe_eq->cq_count += ret;
  1856. if (ret < budget) {
  1857. irq_poll_complete(iop);
  1858. beiscsi_log(phba, KERN_INFO,
  1859. BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
  1860. "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
  1861. pbe_eq->q.id, ret);
  1862. if (!beiscsi_hba_in_error(phba))
  1863. hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
  1864. }
  1865. return ret;
  1866. }
  1867. static void
  1868. hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1869. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1870. {
  1871. struct iscsi_sge *psgl;
  1872. unsigned int sg_len, index;
  1873. unsigned int sge_len = 0;
  1874. unsigned long long addr;
  1875. struct scatterlist *l_sg;
  1876. unsigned int offset;
  1877. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
  1878. io_task->bhs_pa.u.a32.address_lo);
  1879. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
  1880. io_task->bhs_pa.u.a32.address_hi);
  1881. l_sg = sg;
  1882. for (index = 0; (index < num_sg) && (index < 2); index++,
  1883. sg = sg_next(sg)) {
  1884. if (index == 0) {
  1885. sg_len = sg_dma_len(sg);
  1886. addr = (u64) sg_dma_address(sg);
  1887. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1888. sge0_addr_lo, pwrb,
  1889. lower_32_bits(addr));
  1890. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1891. sge0_addr_hi, pwrb,
  1892. upper_32_bits(addr));
  1893. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1894. sge0_len, pwrb,
  1895. sg_len);
  1896. sge_len = sg_len;
  1897. } else {
  1898. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
  1899. pwrb, sge_len);
  1900. sg_len = sg_dma_len(sg);
  1901. addr = (u64) sg_dma_address(sg);
  1902. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1903. sge1_addr_lo, pwrb,
  1904. lower_32_bits(addr));
  1905. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1906. sge1_addr_hi, pwrb,
  1907. upper_32_bits(addr));
  1908. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  1909. sge1_len, pwrb,
  1910. sg_len);
  1911. }
  1912. }
  1913. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1914. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1915. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1916. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1917. io_task->bhs_pa.u.a32.address_hi);
  1918. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1919. io_task->bhs_pa.u.a32.address_lo);
  1920. if (num_sg == 1) {
  1921. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1922. 1);
  1923. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1924. 0);
  1925. } else if (num_sg == 2) {
  1926. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1927. 0);
  1928. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1929. 1);
  1930. } else {
  1931. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
  1932. 0);
  1933. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
  1934. 0);
  1935. }
  1936. sg = l_sg;
  1937. psgl++;
  1938. psgl++;
  1939. offset = 0;
  1940. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  1941. sg_len = sg_dma_len(sg);
  1942. addr = (u64) sg_dma_address(sg);
  1943. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  1944. lower_32_bits(addr));
  1945. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1946. upper_32_bits(addr));
  1947. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  1948. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  1949. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  1950. offset += sg_len;
  1951. }
  1952. psgl--;
  1953. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  1954. }
  1955. static void
  1956. hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
  1957. unsigned int num_sg, struct beiscsi_io_task *io_task)
  1958. {
  1959. struct iscsi_sge *psgl;
  1960. unsigned int sg_len, index;
  1961. unsigned int sge_len = 0;
  1962. unsigned long long addr;
  1963. struct scatterlist *l_sg;
  1964. unsigned int offset;
  1965. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  1966. io_task->bhs_pa.u.a32.address_lo);
  1967. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  1968. io_task->bhs_pa.u.a32.address_hi);
  1969. l_sg = sg;
  1970. for (index = 0; (index < num_sg) && (index < 2); index++,
  1971. sg = sg_next(sg)) {
  1972. if (index == 0) {
  1973. sg_len = sg_dma_len(sg);
  1974. addr = (u64) sg_dma_address(sg);
  1975. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  1976. ((u32)(addr & 0xFFFFFFFF)));
  1977. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  1978. ((u32)(addr >> 32)));
  1979. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  1980. sg_len);
  1981. sge_len = sg_len;
  1982. } else {
  1983. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
  1984. pwrb, sge_len);
  1985. sg_len = sg_dma_len(sg);
  1986. addr = (u64) sg_dma_address(sg);
  1987. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
  1988. ((u32)(addr & 0xFFFFFFFF)));
  1989. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
  1990. ((u32)(addr >> 32)));
  1991. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
  1992. sg_len);
  1993. }
  1994. }
  1995. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  1996. memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
  1997. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
  1998. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  1999. io_task->bhs_pa.u.a32.address_hi);
  2000. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2001. io_task->bhs_pa.u.a32.address_lo);
  2002. if (num_sg == 1) {
  2003. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2004. 1);
  2005. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2006. 0);
  2007. } else if (num_sg == 2) {
  2008. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2009. 0);
  2010. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2011. 1);
  2012. } else {
  2013. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
  2014. 0);
  2015. AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
  2016. 0);
  2017. }
  2018. sg = l_sg;
  2019. psgl++;
  2020. psgl++;
  2021. offset = 0;
  2022. for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
  2023. sg_len = sg_dma_len(sg);
  2024. addr = (u64) sg_dma_address(sg);
  2025. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2026. (addr & 0xFFFFFFFF));
  2027. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2028. (addr >> 32));
  2029. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
  2030. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
  2031. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2032. offset += sg_len;
  2033. }
  2034. psgl--;
  2035. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2036. }
  2037. /**
  2038. * hwi_write_buffer()- Populate the WRB with task info
  2039. * @pwrb: ptr to the WRB entry
  2040. * @task: iscsi task which is to be executed
  2041. **/
  2042. static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
  2043. {
  2044. struct iscsi_sge *psgl;
  2045. struct beiscsi_io_task *io_task = task->dd_data;
  2046. struct beiscsi_conn *beiscsi_conn = io_task->conn;
  2047. struct beiscsi_hba *phba = beiscsi_conn->phba;
  2048. uint8_t dsp_value = 0;
  2049. io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
  2050. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
  2051. io_task->bhs_pa.u.a32.address_lo);
  2052. AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
  2053. io_task->bhs_pa.u.a32.address_hi);
  2054. if (task->data) {
  2055. /* Check for the data_count */
  2056. dsp_value = (task->data_count) ? 1 : 0;
  2057. if (is_chip_be2_be3r(phba))
  2058. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
  2059. pwrb, dsp_value);
  2060. else
  2061. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
  2062. pwrb, dsp_value);
  2063. /* Map addr only if there is data_count */
  2064. if (dsp_value) {
  2065. io_task->mtask_addr = pci_map_single(phba->pcidev,
  2066. task->data,
  2067. task->data_count,
  2068. PCI_DMA_TODEVICE);
  2069. if (pci_dma_mapping_error(phba->pcidev,
  2070. io_task->mtask_addr))
  2071. return -ENOMEM;
  2072. io_task->mtask_data_count = task->data_count;
  2073. } else
  2074. io_task->mtask_addr = 0;
  2075. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
  2076. lower_32_bits(io_task->mtask_addr));
  2077. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
  2078. upper_32_bits(io_task->mtask_addr));
  2079. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
  2080. task->data_count);
  2081. AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
  2082. } else {
  2083. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  2084. io_task->mtask_addr = 0;
  2085. }
  2086. psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
  2087. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
  2088. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2089. io_task->bhs_pa.u.a32.address_hi);
  2090. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2091. io_task->bhs_pa.u.a32.address_lo);
  2092. if (task->data) {
  2093. psgl++;
  2094. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
  2095. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
  2096. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
  2097. AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
  2098. AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
  2099. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
  2100. psgl++;
  2101. if (task->data) {
  2102. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
  2103. lower_32_bits(io_task->mtask_addr));
  2104. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
  2105. upper_32_bits(io_task->mtask_addr));
  2106. }
  2107. AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
  2108. }
  2109. AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
  2110. return 0;
  2111. }
  2112. /**
  2113. * beiscsi_find_mem_req()- Find mem needed
  2114. * @phba: ptr to HBA struct
  2115. **/
  2116. static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
  2117. {
  2118. uint8_t mem_descr_index, ulp_num;
  2119. unsigned int num_async_pdu_buf_pages;
  2120. unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
  2121. unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
  2122. phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
  2123. phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
  2124. BE_ISCSI_PDU_HEADER_SIZE;
  2125. phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
  2126. sizeof(struct hwi_context_memory);
  2127. phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
  2128. * (phba->params.wrbs_per_cxn)
  2129. * phba->params.cxns_per_ctrl;
  2130. wrb_sz_per_cxn = sizeof(struct wrb_handle) *
  2131. (phba->params.wrbs_per_cxn);
  2132. phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
  2133. phba->params.cxns_per_ctrl);
  2134. phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
  2135. phba->params.icds_per_ctrl;
  2136. phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
  2137. phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
  2138. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2139. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2140. num_async_pdu_buf_sgl_pages =
  2141. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2142. phba, ulp_num) *
  2143. sizeof(struct phys_addr));
  2144. num_async_pdu_buf_pages =
  2145. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2146. phba, ulp_num) *
  2147. phba->params.defpdu_hdr_sz);
  2148. num_async_pdu_data_pages =
  2149. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2150. phba, ulp_num) *
  2151. phba->params.defpdu_data_sz);
  2152. num_async_pdu_data_sgl_pages =
  2153. PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
  2154. phba, ulp_num) *
  2155. sizeof(struct phys_addr));
  2156. mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
  2157. (ulp_num * MEM_DESCR_OFFSET));
  2158. phba->mem_req[mem_descr_index] =
  2159. BEISCSI_GET_CID_COUNT(phba, ulp_num) *
  2160. BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
  2161. mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
  2162. (ulp_num * MEM_DESCR_OFFSET));
  2163. phba->mem_req[mem_descr_index] =
  2164. num_async_pdu_buf_pages *
  2165. PAGE_SIZE;
  2166. mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
  2167. (ulp_num * MEM_DESCR_OFFSET));
  2168. phba->mem_req[mem_descr_index] =
  2169. num_async_pdu_data_pages *
  2170. PAGE_SIZE;
  2171. mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2172. (ulp_num * MEM_DESCR_OFFSET));
  2173. phba->mem_req[mem_descr_index] =
  2174. num_async_pdu_buf_sgl_pages *
  2175. PAGE_SIZE;
  2176. mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2177. (ulp_num * MEM_DESCR_OFFSET));
  2178. phba->mem_req[mem_descr_index] =
  2179. num_async_pdu_data_sgl_pages *
  2180. PAGE_SIZE;
  2181. mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
  2182. (ulp_num * MEM_DESCR_OFFSET));
  2183. phba->mem_req[mem_descr_index] =
  2184. BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
  2185. sizeof(struct hd_async_handle);
  2186. mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
  2187. (ulp_num * MEM_DESCR_OFFSET));
  2188. phba->mem_req[mem_descr_index] =
  2189. BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
  2190. sizeof(struct hd_async_handle);
  2191. mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
  2192. (ulp_num * MEM_DESCR_OFFSET));
  2193. phba->mem_req[mem_descr_index] =
  2194. sizeof(struct hd_async_context) +
  2195. (BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
  2196. sizeof(struct hd_async_entry));
  2197. }
  2198. }
  2199. }
  2200. static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
  2201. {
  2202. dma_addr_t bus_add;
  2203. struct hwi_controller *phwi_ctrlr;
  2204. struct be_mem_descriptor *mem_descr;
  2205. struct mem_array *mem_arr, *mem_arr_orig;
  2206. unsigned int i, j, alloc_size, curr_alloc_size;
  2207. phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
  2208. if (!phba->phwi_ctrlr)
  2209. return -ENOMEM;
  2210. /* Allocate memory for wrb_context */
  2211. phwi_ctrlr = phba->phwi_ctrlr;
  2212. phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
  2213. phba->params.cxns_per_ctrl,
  2214. GFP_KERNEL);
  2215. if (!phwi_ctrlr->wrb_context) {
  2216. kfree(phba->phwi_ctrlr);
  2217. return -ENOMEM;
  2218. }
  2219. phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
  2220. GFP_KERNEL);
  2221. if (!phba->init_mem) {
  2222. kfree(phwi_ctrlr->wrb_context);
  2223. kfree(phba->phwi_ctrlr);
  2224. return -ENOMEM;
  2225. }
  2226. mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
  2227. GFP_KERNEL);
  2228. if (!mem_arr_orig) {
  2229. kfree(phba->init_mem);
  2230. kfree(phwi_ctrlr->wrb_context);
  2231. kfree(phba->phwi_ctrlr);
  2232. return -ENOMEM;
  2233. }
  2234. mem_descr = phba->init_mem;
  2235. for (i = 0; i < SE_MEM_MAX; i++) {
  2236. if (!phba->mem_req[i]) {
  2237. mem_descr->mem_array = NULL;
  2238. mem_descr++;
  2239. continue;
  2240. }
  2241. j = 0;
  2242. mem_arr = mem_arr_orig;
  2243. alloc_size = phba->mem_req[i];
  2244. memset(mem_arr, 0, sizeof(struct mem_array) *
  2245. BEISCSI_MAX_FRAGS_INIT);
  2246. curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
  2247. do {
  2248. mem_arr->virtual_address = pci_alloc_consistent(
  2249. phba->pcidev,
  2250. curr_alloc_size,
  2251. &bus_add);
  2252. if (!mem_arr->virtual_address) {
  2253. if (curr_alloc_size <= BE_MIN_MEM_SIZE)
  2254. goto free_mem;
  2255. if (curr_alloc_size -
  2256. rounddown_pow_of_two(curr_alloc_size))
  2257. curr_alloc_size = rounddown_pow_of_two
  2258. (curr_alloc_size);
  2259. else
  2260. curr_alloc_size = curr_alloc_size / 2;
  2261. } else {
  2262. mem_arr->bus_address.u.
  2263. a64.address = (__u64) bus_add;
  2264. mem_arr->size = curr_alloc_size;
  2265. alloc_size -= curr_alloc_size;
  2266. curr_alloc_size = min(be_max_phys_size *
  2267. 1024, alloc_size);
  2268. j++;
  2269. mem_arr++;
  2270. }
  2271. } while (alloc_size);
  2272. mem_descr->num_elements = j;
  2273. mem_descr->size_in_bytes = phba->mem_req[i];
  2274. mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
  2275. GFP_KERNEL);
  2276. if (!mem_descr->mem_array)
  2277. goto free_mem;
  2278. memcpy(mem_descr->mem_array, mem_arr_orig,
  2279. sizeof(struct mem_array) * j);
  2280. mem_descr++;
  2281. }
  2282. kfree(mem_arr_orig);
  2283. return 0;
  2284. free_mem:
  2285. mem_descr->num_elements = j;
  2286. while ((i) || (j)) {
  2287. for (j = mem_descr->num_elements; j > 0; j--) {
  2288. pci_free_consistent(phba->pcidev,
  2289. mem_descr->mem_array[j - 1].size,
  2290. mem_descr->mem_array[j - 1].
  2291. virtual_address,
  2292. (unsigned long)mem_descr->
  2293. mem_array[j - 1].
  2294. bus_address.u.a64.address);
  2295. }
  2296. if (i) {
  2297. i--;
  2298. kfree(mem_descr->mem_array);
  2299. mem_descr--;
  2300. }
  2301. }
  2302. kfree(mem_arr_orig);
  2303. kfree(phba->init_mem);
  2304. kfree(phba->phwi_ctrlr->wrb_context);
  2305. kfree(phba->phwi_ctrlr);
  2306. return -ENOMEM;
  2307. }
  2308. static int beiscsi_get_memory(struct beiscsi_hba *phba)
  2309. {
  2310. beiscsi_find_mem_req(phba);
  2311. return beiscsi_alloc_mem(phba);
  2312. }
  2313. static void iscsi_init_global_templates(struct beiscsi_hba *phba)
  2314. {
  2315. struct pdu_data_out *pdata_out;
  2316. struct pdu_nop_out *pnop_out;
  2317. struct be_mem_descriptor *mem_descr;
  2318. mem_descr = phba->init_mem;
  2319. mem_descr += ISCSI_MEM_GLOBAL_HEADER;
  2320. pdata_out =
  2321. (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
  2322. memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2323. AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
  2324. IIOC_SCSI_DATA);
  2325. pnop_out =
  2326. (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
  2327. virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
  2328. memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
  2329. AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
  2330. AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
  2331. AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
  2332. }
  2333. static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
  2334. {
  2335. struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
  2336. struct hwi_context_memory *phwi_ctxt;
  2337. struct wrb_handle *pwrb_handle = NULL;
  2338. struct hwi_controller *phwi_ctrlr;
  2339. struct hwi_wrb_context *pwrb_context;
  2340. struct iscsi_wrb *pwrb = NULL;
  2341. unsigned int num_cxn_wrbh = 0;
  2342. unsigned int num_cxn_wrb = 0, j, idx = 0, index;
  2343. mem_descr_wrbh = phba->init_mem;
  2344. mem_descr_wrbh += HWI_MEM_WRBH;
  2345. mem_descr_wrb = phba->init_mem;
  2346. mem_descr_wrb += HWI_MEM_WRB;
  2347. phwi_ctrlr = phba->phwi_ctrlr;
  2348. /* Allocate memory for WRBQ */
  2349. phwi_ctxt = phwi_ctrlr->phwi_ctxt;
  2350. phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
  2351. phba->params.cxns_per_ctrl,
  2352. GFP_KERNEL);
  2353. if (!phwi_ctxt->be_wrbq) {
  2354. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2355. "BM_%d : WRBQ Mem Alloc Failed\n");
  2356. return -ENOMEM;
  2357. }
  2358. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  2359. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2360. pwrb_context->pwrb_handle_base =
  2361. kzalloc(sizeof(struct wrb_handle *) *
  2362. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2363. if (!pwrb_context->pwrb_handle_base) {
  2364. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2365. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2366. goto init_wrb_hndl_failed;
  2367. }
  2368. pwrb_context->pwrb_handle_basestd =
  2369. kzalloc(sizeof(struct wrb_handle *) *
  2370. phba->params.wrbs_per_cxn, GFP_KERNEL);
  2371. if (!pwrb_context->pwrb_handle_basestd) {
  2372. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2373. "BM_%d : Mem Alloc Failed. Failing to load\n");
  2374. goto init_wrb_hndl_failed;
  2375. }
  2376. if (!num_cxn_wrbh) {
  2377. pwrb_handle =
  2378. mem_descr_wrbh->mem_array[idx].virtual_address;
  2379. num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
  2380. ((sizeof(struct wrb_handle)) *
  2381. phba->params.wrbs_per_cxn));
  2382. idx++;
  2383. }
  2384. pwrb_context->alloc_index = 0;
  2385. pwrb_context->wrb_handles_available = 0;
  2386. pwrb_context->free_index = 0;
  2387. if (num_cxn_wrbh) {
  2388. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2389. pwrb_context->pwrb_handle_base[j] = pwrb_handle;
  2390. pwrb_context->pwrb_handle_basestd[j] =
  2391. pwrb_handle;
  2392. pwrb_context->wrb_handles_available++;
  2393. pwrb_handle->wrb_index = j;
  2394. pwrb_handle++;
  2395. }
  2396. num_cxn_wrbh--;
  2397. }
  2398. spin_lock_init(&pwrb_context->wrb_lock);
  2399. }
  2400. idx = 0;
  2401. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  2402. pwrb_context = &phwi_ctrlr->wrb_context[index];
  2403. if (!num_cxn_wrb) {
  2404. pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
  2405. num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
  2406. ((sizeof(struct iscsi_wrb) *
  2407. phba->params.wrbs_per_cxn));
  2408. idx++;
  2409. }
  2410. if (num_cxn_wrb) {
  2411. for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
  2412. pwrb_handle = pwrb_context->pwrb_handle_base[j];
  2413. pwrb_handle->pwrb = pwrb;
  2414. pwrb++;
  2415. }
  2416. num_cxn_wrb--;
  2417. }
  2418. }
  2419. return 0;
  2420. init_wrb_hndl_failed:
  2421. for (j = index; j > 0; j--) {
  2422. pwrb_context = &phwi_ctrlr->wrb_context[j];
  2423. kfree(pwrb_context->pwrb_handle_base);
  2424. kfree(pwrb_context->pwrb_handle_basestd);
  2425. }
  2426. return -ENOMEM;
  2427. }
  2428. static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
  2429. {
  2430. uint8_t ulp_num;
  2431. struct hwi_controller *phwi_ctrlr;
  2432. struct hba_parameters *p = &phba->params;
  2433. struct hd_async_context *pasync_ctx;
  2434. struct hd_async_handle *pasync_header_h, *pasync_data_h;
  2435. unsigned int index, idx, num_per_mem, num_async_data;
  2436. struct be_mem_descriptor *mem_descr;
  2437. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2438. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2439. /* get async_ctx for each ULP */
  2440. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2441. mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
  2442. (ulp_num * MEM_DESCR_OFFSET));
  2443. phwi_ctrlr = phba->phwi_ctrlr;
  2444. phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
  2445. (struct hd_async_context *)
  2446. mem_descr->mem_array[0].virtual_address;
  2447. pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
  2448. memset(pasync_ctx, 0, sizeof(*pasync_ctx));
  2449. pasync_ctx->async_entry =
  2450. (struct hd_async_entry *)
  2451. ((long unsigned int)pasync_ctx +
  2452. sizeof(struct hd_async_context));
  2453. pasync_ctx->num_entries = BEISCSI_ASYNC_HDQ_SIZE(phba,
  2454. ulp_num);
  2455. /* setup header buffers */
  2456. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2457. mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
  2458. (ulp_num * MEM_DESCR_OFFSET);
  2459. if (mem_descr->mem_array[0].virtual_address) {
  2460. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2461. "BM_%d : hwi_init_async_pdu_ctx"
  2462. " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
  2463. ulp_num,
  2464. mem_descr->mem_array[0].
  2465. virtual_address);
  2466. } else
  2467. beiscsi_log(phba, KERN_WARNING,
  2468. BEISCSI_LOG_INIT,
  2469. "BM_%d : No Virtual address for ULP : %d\n",
  2470. ulp_num);
  2471. pasync_ctx->async_header.pi = 0;
  2472. pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
  2473. pasync_ctx->async_header.va_base =
  2474. mem_descr->mem_array[0].virtual_address;
  2475. pasync_ctx->async_header.pa_base.u.a64.address =
  2476. mem_descr->mem_array[0].
  2477. bus_address.u.a64.address;
  2478. /* setup header buffer sgls */
  2479. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2480. mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2481. (ulp_num * MEM_DESCR_OFFSET);
  2482. if (mem_descr->mem_array[0].virtual_address) {
  2483. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2484. "BM_%d : hwi_init_async_pdu_ctx"
  2485. " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
  2486. ulp_num,
  2487. mem_descr->mem_array[0].
  2488. virtual_address);
  2489. } else
  2490. beiscsi_log(phba, KERN_WARNING,
  2491. BEISCSI_LOG_INIT,
  2492. "BM_%d : No Virtual address for ULP : %d\n",
  2493. ulp_num);
  2494. pasync_ctx->async_header.ring_base =
  2495. mem_descr->mem_array[0].virtual_address;
  2496. /* setup header buffer handles */
  2497. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2498. mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
  2499. (ulp_num * MEM_DESCR_OFFSET);
  2500. if (mem_descr->mem_array[0].virtual_address) {
  2501. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2502. "BM_%d : hwi_init_async_pdu_ctx"
  2503. " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
  2504. ulp_num,
  2505. mem_descr->mem_array[0].
  2506. virtual_address);
  2507. } else
  2508. beiscsi_log(phba, KERN_WARNING,
  2509. BEISCSI_LOG_INIT,
  2510. "BM_%d : No Virtual address for ULP : %d\n",
  2511. ulp_num);
  2512. pasync_ctx->async_header.handle_base =
  2513. mem_descr->mem_array[0].virtual_address;
  2514. /* setup data buffer sgls */
  2515. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2516. mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2517. (ulp_num * MEM_DESCR_OFFSET);
  2518. if (mem_descr->mem_array[0].virtual_address) {
  2519. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2520. "BM_%d : hwi_init_async_pdu_ctx"
  2521. " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
  2522. ulp_num,
  2523. mem_descr->mem_array[0].
  2524. virtual_address);
  2525. } else
  2526. beiscsi_log(phba, KERN_WARNING,
  2527. BEISCSI_LOG_INIT,
  2528. "BM_%d : No Virtual address for ULP : %d\n",
  2529. ulp_num);
  2530. pasync_ctx->async_data.ring_base =
  2531. mem_descr->mem_array[0].virtual_address;
  2532. /* setup data buffer handles */
  2533. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2534. mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
  2535. (ulp_num * MEM_DESCR_OFFSET);
  2536. if (!mem_descr->mem_array[0].virtual_address)
  2537. beiscsi_log(phba, KERN_WARNING,
  2538. BEISCSI_LOG_INIT,
  2539. "BM_%d : No Virtual address for ULP : %d\n",
  2540. ulp_num);
  2541. pasync_ctx->async_data.handle_base =
  2542. mem_descr->mem_array[0].virtual_address;
  2543. pasync_header_h =
  2544. (struct hd_async_handle *)
  2545. pasync_ctx->async_header.handle_base;
  2546. pasync_data_h =
  2547. (struct hd_async_handle *)
  2548. pasync_ctx->async_data.handle_base;
  2549. /* setup data buffers */
  2550. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2551. mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
  2552. (ulp_num * MEM_DESCR_OFFSET);
  2553. if (mem_descr->mem_array[0].virtual_address) {
  2554. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2555. "BM_%d : hwi_init_async_pdu_ctx"
  2556. " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
  2557. ulp_num,
  2558. mem_descr->mem_array[0].
  2559. virtual_address);
  2560. } else
  2561. beiscsi_log(phba, KERN_WARNING,
  2562. BEISCSI_LOG_INIT,
  2563. "BM_%d : No Virtual address for ULP : %d\n",
  2564. ulp_num);
  2565. idx = 0;
  2566. pasync_ctx->async_data.pi = 0;
  2567. pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
  2568. pasync_ctx->async_data.va_base =
  2569. mem_descr->mem_array[idx].virtual_address;
  2570. pasync_ctx->async_data.pa_base.u.a64.address =
  2571. mem_descr->mem_array[idx].
  2572. bus_address.u.a64.address;
  2573. num_async_data = ((mem_descr->mem_array[idx].size) /
  2574. phba->params.defpdu_data_sz);
  2575. num_per_mem = 0;
  2576. for (index = 0; index < BEISCSI_ASYNC_HDQ_SIZE
  2577. (phba, ulp_num); index++) {
  2578. pasync_header_h->cri = -1;
  2579. pasync_header_h->is_header = 1;
  2580. pasync_header_h->index = index;
  2581. INIT_LIST_HEAD(&pasync_header_h->link);
  2582. pasync_header_h->pbuffer =
  2583. (void *)((unsigned long)
  2584. (pasync_ctx->
  2585. async_header.va_base) +
  2586. (p->defpdu_hdr_sz * index));
  2587. pasync_header_h->pa.u.a64.address =
  2588. pasync_ctx->async_header.pa_base.u.a64.
  2589. address + (p->defpdu_hdr_sz * index);
  2590. pasync_ctx->async_entry[index].header =
  2591. pasync_header_h;
  2592. pasync_header_h++;
  2593. INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
  2594. wq.list);
  2595. pasync_data_h->cri = -1;
  2596. pasync_data_h->is_header = 0;
  2597. pasync_data_h->index = index;
  2598. INIT_LIST_HEAD(&pasync_data_h->link);
  2599. if (!num_async_data) {
  2600. num_per_mem = 0;
  2601. idx++;
  2602. pasync_ctx->async_data.va_base =
  2603. mem_descr->mem_array[idx].
  2604. virtual_address;
  2605. pasync_ctx->async_data.pa_base.u.
  2606. a64.address =
  2607. mem_descr->mem_array[idx].
  2608. bus_address.u.a64.address;
  2609. num_async_data =
  2610. ((mem_descr->mem_array[idx].
  2611. size) /
  2612. phba->params.defpdu_data_sz);
  2613. }
  2614. pasync_data_h->pbuffer =
  2615. (void *)((unsigned long)
  2616. (pasync_ctx->async_data.va_base) +
  2617. (p->defpdu_data_sz * num_per_mem));
  2618. pasync_data_h->pa.u.a64.address =
  2619. pasync_ctx->async_data.pa_base.u.a64.
  2620. address + (p->defpdu_data_sz *
  2621. num_per_mem);
  2622. num_per_mem++;
  2623. num_async_data--;
  2624. pasync_ctx->async_entry[index].data =
  2625. pasync_data_h;
  2626. pasync_data_h++;
  2627. }
  2628. }
  2629. }
  2630. return 0;
  2631. }
  2632. static int
  2633. be_sgl_create_contiguous(void *virtual_address,
  2634. u64 physical_address, u32 length,
  2635. struct be_dma_mem *sgl)
  2636. {
  2637. WARN_ON(!virtual_address);
  2638. WARN_ON(!physical_address);
  2639. WARN_ON(!length);
  2640. WARN_ON(!sgl);
  2641. sgl->va = virtual_address;
  2642. sgl->dma = (unsigned long)physical_address;
  2643. sgl->size = length;
  2644. return 0;
  2645. }
  2646. static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
  2647. {
  2648. memset(sgl, 0, sizeof(*sgl));
  2649. }
  2650. static void
  2651. hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
  2652. struct mem_array *pmem, struct be_dma_mem *sgl)
  2653. {
  2654. if (sgl->va)
  2655. be_sgl_destroy_contiguous(sgl);
  2656. be_sgl_create_contiguous(pmem->virtual_address,
  2657. pmem->bus_address.u.a64.address,
  2658. pmem->size, sgl);
  2659. }
  2660. static void
  2661. hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
  2662. struct mem_array *pmem, struct be_dma_mem *sgl)
  2663. {
  2664. if (sgl->va)
  2665. be_sgl_destroy_contiguous(sgl);
  2666. be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
  2667. pmem->bus_address.u.a64.address,
  2668. pmem->size, sgl);
  2669. }
  2670. static int be_fill_queue(struct be_queue_info *q,
  2671. u16 len, u16 entry_size, void *vaddress)
  2672. {
  2673. struct be_dma_mem *mem = &q->dma_mem;
  2674. memset(q, 0, sizeof(*q));
  2675. q->len = len;
  2676. q->entry_size = entry_size;
  2677. mem->size = len * entry_size;
  2678. mem->va = vaddress;
  2679. if (!mem->va)
  2680. return -ENOMEM;
  2681. memset(mem->va, 0, mem->size);
  2682. return 0;
  2683. }
  2684. static int beiscsi_create_eqs(struct beiscsi_hba *phba,
  2685. struct hwi_context_memory *phwi_context)
  2686. {
  2687. int ret = -ENOMEM, eq_for_mcc;
  2688. unsigned int i, num_eq_pages;
  2689. struct be_queue_info *eq;
  2690. struct be_dma_mem *mem;
  2691. void *eq_vaddress;
  2692. dma_addr_t paddr;
  2693. num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
  2694. sizeof(struct be_eq_entry));
  2695. if (phba->pcidev->msix_enabled)
  2696. eq_for_mcc = 1;
  2697. else
  2698. eq_for_mcc = 0;
  2699. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2700. eq = &phwi_context->be_eq[i].q;
  2701. mem = &eq->dma_mem;
  2702. phwi_context->be_eq[i].phba = phba;
  2703. eq_vaddress = pci_alloc_consistent(phba->pcidev,
  2704. num_eq_pages * PAGE_SIZE,
  2705. &paddr);
  2706. if (!eq_vaddress) {
  2707. ret = -ENOMEM;
  2708. goto create_eq_error;
  2709. }
  2710. mem->va = eq_vaddress;
  2711. ret = be_fill_queue(eq, phba->params.num_eq_entries,
  2712. sizeof(struct be_eq_entry), eq_vaddress);
  2713. if (ret) {
  2714. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2715. "BM_%d : be_fill_queue Failed for EQ\n");
  2716. goto create_eq_error;
  2717. }
  2718. mem->dma = paddr;
  2719. ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
  2720. BEISCSI_EQ_DELAY_DEF);
  2721. if (ret) {
  2722. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2723. "BM_%d : beiscsi_cmd_eq_create"
  2724. "Failed for EQ\n");
  2725. goto create_eq_error;
  2726. }
  2727. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2728. "BM_%d : eqid = %d\n",
  2729. phwi_context->be_eq[i].q.id);
  2730. }
  2731. return 0;
  2732. create_eq_error:
  2733. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  2734. eq = &phwi_context->be_eq[i].q;
  2735. mem = &eq->dma_mem;
  2736. if (mem->va)
  2737. pci_free_consistent(phba->pcidev, num_eq_pages
  2738. * PAGE_SIZE,
  2739. mem->va, mem->dma);
  2740. }
  2741. return ret;
  2742. }
  2743. static int beiscsi_create_cqs(struct beiscsi_hba *phba,
  2744. struct hwi_context_memory *phwi_context)
  2745. {
  2746. unsigned int i, num_cq_pages;
  2747. struct be_queue_info *cq, *eq;
  2748. struct be_dma_mem *mem;
  2749. struct be_eq_obj *pbe_eq;
  2750. void *cq_vaddress;
  2751. int ret = -ENOMEM;
  2752. dma_addr_t paddr;
  2753. num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
  2754. sizeof(struct sol_cqe));
  2755. for (i = 0; i < phba->num_cpus; i++) {
  2756. cq = &phwi_context->be_cq[i];
  2757. eq = &phwi_context->be_eq[i].q;
  2758. pbe_eq = &phwi_context->be_eq[i];
  2759. pbe_eq->cq = cq;
  2760. pbe_eq->phba = phba;
  2761. mem = &cq->dma_mem;
  2762. cq_vaddress = pci_alloc_consistent(phba->pcidev,
  2763. num_cq_pages * PAGE_SIZE,
  2764. &paddr);
  2765. if (!cq_vaddress) {
  2766. ret = -ENOMEM;
  2767. goto create_cq_error;
  2768. }
  2769. ret = be_fill_queue(cq, phba->params.num_cq_entries,
  2770. sizeof(struct sol_cqe), cq_vaddress);
  2771. if (ret) {
  2772. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2773. "BM_%d : be_fill_queue Failed "
  2774. "for ISCSI CQ\n");
  2775. goto create_cq_error;
  2776. }
  2777. mem->dma = paddr;
  2778. ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
  2779. false, 0);
  2780. if (ret) {
  2781. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2782. "BM_%d : beiscsi_cmd_eq_create"
  2783. "Failed for ISCSI CQ\n");
  2784. goto create_cq_error;
  2785. }
  2786. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2787. "BM_%d : iscsi cq_id is %d for eq_id %d\n"
  2788. "iSCSI CQ CREATED\n", cq->id, eq->id);
  2789. }
  2790. return 0;
  2791. create_cq_error:
  2792. for (i = 0; i < phba->num_cpus; i++) {
  2793. cq = &phwi_context->be_cq[i];
  2794. mem = &cq->dma_mem;
  2795. if (mem->va)
  2796. pci_free_consistent(phba->pcidev, num_cq_pages
  2797. * PAGE_SIZE,
  2798. mem->va, mem->dma);
  2799. }
  2800. return ret;
  2801. }
  2802. static int
  2803. beiscsi_create_def_hdr(struct beiscsi_hba *phba,
  2804. struct hwi_context_memory *phwi_context,
  2805. struct hwi_controller *phwi_ctrlr,
  2806. unsigned int def_pdu_ring_sz, uint8_t ulp_num)
  2807. {
  2808. unsigned int idx;
  2809. int ret;
  2810. struct be_queue_info *dq, *cq;
  2811. struct be_dma_mem *mem;
  2812. struct be_mem_descriptor *mem_descr;
  2813. void *dq_vaddress;
  2814. idx = 0;
  2815. dq = &phwi_context->be_def_hdrq[ulp_num];
  2816. cq = &phwi_context->be_cq[0];
  2817. mem = &dq->dma_mem;
  2818. mem_descr = phba->init_mem;
  2819. mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
  2820. (ulp_num * MEM_DESCR_OFFSET);
  2821. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2822. ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
  2823. sizeof(struct phys_addr),
  2824. sizeof(struct phys_addr), dq_vaddress);
  2825. if (ret) {
  2826. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2827. "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
  2828. ulp_num);
  2829. return ret;
  2830. }
  2831. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2832. bus_address.u.a64.address;
  2833. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
  2834. def_pdu_ring_sz,
  2835. phba->params.defpdu_hdr_sz,
  2836. BEISCSI_DEFQ_HDR, ulp_num);
  2837. if (ret) {
  2838. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2839. "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
  2840. ulp_num);
  2841. return ret;
  2842. }
  2843. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2844. "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
  2845. ulp_num,
  2846. phwi_context->be_def_hdrq[ulp_num].id);
  2847. return 0;
  2848. }
  2849. static int
  2850. beiscsi_create_def_data(struct beiscsi_hba *phba,
  2851. struct hwi_context_memory *phwi_context,
  2852. struct hwi_controller *phwi_ctrlr,
  2853. unsigned int def_pdu_ring_sz, uint8_t ulp_num)
  2854. {
  2855. unsigned int idx;
  2856. int ret;
  2857. struct be_queue_info *dataq, *cq;
  2858. struct be_dma_mem *mem;
  2859. struct be_mem_descriptor *mem_descr;
  2860. void *dq_vaddress;
  2861. idx = 0;
  2862. dataq = &phwi_context->be_def_dataq[ulp_num];
  2863. cq = &phwi_context->be_cq[0];
  2864. mem = &dataq->dma_mem;
  2865. mem_descr = phba->init_mem;
  2866. mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
  2867. (ulp_num * MEM_DESCR_OFFSET);
  2868. dq_vaddress = mem_descr->mem_array[idx].virtual_address;
  2869. ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
  2870. sizeof(struct phys_addr),
  2871. sizeof(struct phys_addr), dq_vaddress);
  2872. if (ret) {
  2873. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2874. "BM_%d : be_fill_queue Failed for DEF PDU "
  2875. "DATA on ULP : %d\n",
  2876. ulp_num);
  2877. return ret;
  2878. }
  2879. mem->dma = (unsigned long)mem_descr->mem_array[idx].
  2880. bus_address.u.a64.address;
  2881. ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
  2882. def_pdu_ring_sz,
  2883. phba->params.defpdu_data_sz,
  2884. BEISCSI_DEFQ_DATA, ulp_num);
  2885. if (ret) {
  2886. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2887. "BM_%d be_cmd_create_default_pdu_queue"
  2888. " Failed for DEF PDU DATA on ULP : %d\n",
  2889. ulp_num);
  2890. return ret;
  2891. }
  2892. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2893. "BM_%d : iscsi def data id on ULP : %d is %d\n",
  2894. ulp_num,
  2895. phwi_context->be_def_dataq[ulp_num].id);
  2896. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2897. "BM_%d : DEFAULT PDU DATA RING CREATED"
  2898. "on ULP : %d\n", ulp_num);
  2899. return 0;
  2900. }
  2901. static int
  2902. beiscsi_post_template_hdr(struct beiscsi_hba *phba)
  2903. {
  2904. struct be_mem_descriptor *mem_descr;
  2905. struct mem_array *pm_arr;
  2906. struct be_dma_mem sgl;
  2907. int status, ulp_num;
  2908. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  2909. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  2910. mem_descr = (struct be_mem_descriptor *)phba->init_mem;
  2911. mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
  2912. (ulp_num * MEM_DESCR_OFFSET);
  2913. pm_arr = mem_descr->mem_array;
  2914. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2915. status = be_cmd_iscsi_post_template_hdr(
  2916. &phba->ctrl, &sgl);
  2917. if (status != 0) {
  2918. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2919. "BM_%d : Post Template HDR Failed for"
  2920. "ULP_%d\n", ulp_num);
  2921. return status;
  2922. }
  2923. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2924. "BM_%d : Template HDR Pages Posted for"
  2925. "ULP_%d\n", ulp_num);
  2926. }
  2927. }
  2928. return 0;
  2929. }
  2930. static int
  2931. beiscsi_post_pages(struct beiscsi_hba *phba)
  2932. {
  2933. struct be_mem_descriptor *mem_descr;
  2934. struct mem_array *pm_arr;
  2935. unsigned int page_offset, i;
  2936. struct be_dma_mem sgl;
  2937. int status, ulp_num = 0;
  2938. mem_descr = phba->init_mem;
  2939. mem_descr += HWI_MEM_SGE;
  2940. pm_arr = mem_descr->mem_array;
  2941. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  2942. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  2943. break;
  2944. page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
  2945. phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
  2946. for (i = 0; i < mem_descr->num_elements; i++) {
  2947. hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
  2948. status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
  2949. page_offset,
  2950. (pm_arr->size / PAGE_SIZE));
  2951. page_offset += pm_arr->size / PAGE_SIZE;
  2952. if (status != 0) {
  2953. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  2954. "BM_%d : post sgl failed.\n");
  2955. return status;
  2956. }
  2957. pm_arr++;
  2958. }
  2959. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  2960. "BM_%d : POSTED PAGES\n");
  2961. return 0;
  2962. }
  2963. static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
  2964. {
  2965. struct be_dma_mem *mem = &q->dma_mem;
  2966. if (mem->va) {
  2967. pci_free_consistent(phba->pcidev, mem->size,
  2968. mem->va, mem->dma);
  2969. mem->va = NULL;
  2970. }
  2971. }
  2972. static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
  2973. u16 len, u16 entry_size)
  2974. {
  2975. struct be_dma_mem *mem = &q->dma_mem;
  2976. memset(q, 0, sizeof(*q));
  2977. q->len = len;
  2978. q->entry_size = entry_size;
  2979. mem->size = len * entry_size;
  2980. mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
  2981. if (!mem->va)
  2982. return -ENOMEM;
  2983. return 0;
  2984. }
  2985. static int
  2986. beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
  2987. struct hwi_context_memory *phwi_context,
  2988. struct hwi_controller *phwi_ctrlr)
  2989. {
  2990. unsigned int num_wrb_rings;
  2991. u64 pa_addr_lo;
  2992. unsigned int idx, num, i, ulp_num;
  2993. struct mem_array *pwrb_arr;
  2994. void *wrb_vaddr;
  2995. struct be_dma_mem sgl;
  2996. struct be_mem_descriptor *mem_descr;
  2997. struct hwi_wrb_context *pwrb_context;
  2998. int status;
  2999. uint8_t ulp_count = 0, ulp_base_num = 0;
  3000. uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
  3001. idx = 0;
  3002. mem_descr = phba->init_mem;
  3003. mem_descr += HWI_MEM_WRB;
  3004. pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
  3005. GFP_KERNEL);
  3006. if (!pwrb_arr) {
  3007. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3008. "BM_%d : Memory alloc failed in create wrb ring.\n");
  3009. return -ENOMEM;
  3010. }
  3011. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  3012. pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
  3013. num_wrb_rings = mem_descr->mem_array[idx].size /
  3014. (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
  3015. for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
  3016. if (num_wrb_rings) {
  3017. pwrb_arr[num].virtual_address = wrb_vaddr;
  3018. pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
  3019. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  3020. sizeof(struct iscsi_wrb);
  3021. wrb_vaddr += pwrb_arr[num].size;
  3022. pa_addr_lo += pwrb_arr[num].size;
  3023. num_wrb_rings--;
  3024. } else {
  3025. idx++;
  3026. wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
  3027. pa_addr_lo = mem_descr->mem_array[idx].\
  3028. bus_address.u.a64.address;
  3029. num_wrb_rings = mem_descr->mem_array[idx].size /
  3030. (phba->params.wrbs_per_cxn *
  3031. sizeof(struct iscsi_wrb));
  3032. pwrb_arr[num].virtual_address = wrb_vaddr;
  3033. pwrb_arr[num].bus_address.u.a64.address\
  3034. = pa_addr_lo;
  3035. pwrb_arr[num].size = phba->params.wrbs_per_cxn *
  3036. sizeof(struct iscsi_wrb);
  3037. wrb_vaddr += pwrb_arr[num].size;
  3038. pa_addr_lo += pwrb_arr[num].size;
  3039. num_wrb_rings--;
  3040. }
  3041. }
  3042. /* Get the ULP Count */
  3043. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3044. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3045. ulp_count++;
  3046. ulp_base_num = ulp_num;
  3047. cid_count_ulp[ulp_num] =
  3048. BEISCSI_GET_CID_COUNT(phba, ulp_num);
  3049. }
  3050. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3051. if (ulp_count > 1) {
  3052. ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
  3053. if (!cid_count_ulp[ulp_base_num])
  3054. ulp_base_num = (ulp_base_num + 1) %
  3055. BEISCSI_ULP_COUNT;
  3056. cid_count_ulp[ulp_base_num]--;
  3057. }
  3058. hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
  3059. status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
  3060. &phwi_context->be_wrbq[i],
  3061. &phwi_ctrlr->wrb_context[i],
  3062. ulp_base_num);
  3063. if (status != 0) {
  3064. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3065. "BM_%d : wrbq create failed.");
  3066. kfree(pwrb_arr);
  3067. return status;
  3068. }
  3069. pwrb_context = &phwi_ctrlr->wrb_context[i];
  3070. BE_SET_CID_TO_CRI(i, pwrb_context->cid);
  3071. }
  3072. kfree(pwrb_arr);
  3073. return 0;
  3074. }
  3075. static void free_wrb_handles(struct beiscsi_hba *phba)
  3076. {
  3077. unsigned int index;
  3078. struct hwi_controller *phwi_ctrlr;
  3079. struct hwi_wrb_context *pwrb_context;
  3080. phwi_ctrlr = phba->phwi_ctrlr;
  3081. for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
  3082. pwrb_context = &phwi_ctrlr->wrb_context[index];
  3083. kfree(pwrb_context->pwrb_handle_base);
  3084. kfree(pwrb_context->pwrb_handle_basestd);
  3085. }
  3086. }
  3087. static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
  3088. {
  3089. struct be_ctrl_info *ctrl = &phba->ctrl;
  3090. struct be_dma_mem *ptag_mem;
  3091. struct be_queue_info *q;
  3092. int i, tag;
  3093. q = &phba->ctrl.mcc_obj.q;
  3094. for (i = 0; i < MAX_MCC_CMD; i++) {
  3095. tag = i + 1;
  3096. if (!test_bit(MCC_TAG_STATE_RUNNING,
  3097. &ctrl->ptag_state[tag].tag_state))
  3098. continue;
  3099. if (test_bit(MCC_TAG_STATE_TIMEOUT,
  3100. &ctrl->ptag_state[tag].tag_state)) {
  3101. ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
  3102. if (ptag_mem->size) {
  3103. pci_free_consistent(ctrl->pdev,
  3104. ptag_mem->size,
  3105. ptag_mem->va,
  3106. ptag_mem->dma);
  3107. ptag_mem->size = 0;
  3108. }
  3109. continue;
  3110. }
  3111. /**
  3112. * If MCC is still active and waiting then wake up the process.
  3113. * We are here only because port is going offline. The process
  3114. * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
  3115. * returned for the operation and allocated memory cleaned up.
  3116. */
  3117. if (waitqueue_active(&ctrl->mcc_wait[tag])) {
  3118. ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
  3119. ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
  3120. wake_up_interruptible(&ctrl->mcc_wait[tag]);
  3121. /*
  3122. * Control tag info gets reinitialized in enable
  3123. * so wait for the process to clear running state.
  3124. */
  3125. while (test_bit(MCC_TAG_STATE_RUNNING,
  3126. &ctrl->ptag_state[tag].tag_state))
  3127. schedule_timeout_uninterruptible(HZ);
  3128. }
  3129. /**
  3130. * For MCC with tag_states MCC_TAG_STATE_ASYNC and
  3131. * MCC_TAG_STATE_IGNORE nothing needs to done.
  3132. */
  3133. }
  3134. if (q->created) {
  3135. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
  3136. be_queue_free(phba, q);
  3137. }
  3138. q = &phba->ctrl.mcc_obj.cq;
  3139. if (q->created) {
  3140. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  3141. be_queue_free(phba, q);
  3142. }
  3143. }
  3144. static int be_mcc_queues_create(struct beiscsi_hba *phba,
  3145. struct hwi_context_memory *phwi_context)
  3146. {
  3147. struct be_queue_info *q, *cq;
  3148. struct be_ctrl_info *ctrl = &phba->ctrl;
  3149. /* Alloc MCC compl queue */
  3150. cq = &phba->ctrl.mcc_obj.cq;
  3151. if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
  3152. sizeof(struct be_mcc_compl)))
  3153. goto err;
  3154. /* Ask BE to create MCC compl queue; */
  3155. if (phba->pcidev->msix_enabled) {
  3156. if (beiscsi_cmd_cq_create(ctrl, cq,
  3157. &phwi_context->be_eq[phba->num_cpus].q,
  3158. false, true, 0))
  3159. goto mcc_cq_free;
  3160. } else {
  3161. if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
  3162. false, true, 0))
  3163. goto mcc_cq_free;
  3164. }
  3165. /* Alloc MCC queue */
  3166. q = &phba->ctrl.mcc_obj.q;
  3167. if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
  3168. goto mcc_cq_destroy;
  3169. /* Ask BE to create MCC queue */
  3170. if (beiscsi_cmd_mccq_create(phba, q, cq))
  3171. goto mcc_q_free;
  3172. return 0;
  3173. mcc_q_free:
  3174. be_queue_free(phba, q);
  3175. mcc_cq_destroy:
  3176. beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
  3177. mcc_cq_free:
  3178. be_queue_free(phba, cq);
  3179. err:
  3180. return -ENOMEM;
  3181. }
  3182. static void be2iscsi_enable_msix(struct beiscsi_hba *phba)
  3183. {
  3184. int nvec = 1;
  3185. switch (phba->generation) {
  3186. case BE_GEN2:
  3187. case BE_GEN3:
  3188. nvec = BEISCSI_MAX_NUM_CPUS + 1;
  3189. break;
  3190. case BE_GEN4:
  3191. nvec = phba->fw_config.eqid_count;
  3192. break;
  3193. default:
  3194. nvec = 2;
  3195. break;
  3196. }
  3197. /* if eqid_count == 1 fall back to INTX */
  3198. if (enable_msix && nvec > 1) {
  3199. const struct irq_affinity desc = { .post_vectors = 1 };
  3200. if (pci_alloc_irq_vectors_affinity(phba->pcidev, 2, nvec,
  3201. PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, &desc) < 0) {
  3202. phba->num_cpus = nvec - 1;
  3203. return;
  3204. }
  3205. }
  3206. phba->num_cpus = 1;
  3207. }
  3208. static void hwi_purge_eq(struct beiscsi_hba *phba)
  3209. {
  3210. struct hwi_controller *phwi_ctrlr;
  3211. struct hwi_context_memory *phwi_context;
  3212. struct be_queue_info *eq;
  3213. struct be_eq_entry *eqe = NULL;
  3214. int i, eq_msix;
  3215. unsigned int num_processed;
  3216. if (beiscsi_hba_in_error(phba))
  3217. return;
  3218. phwi_ctrlr = phba->phwi_ctrlr;
  3219. phwi_context = phwi_ctrlr->phwi_ctxt;
  3220. if (phba->pcidev->msix_enabled)
  3221. eq_msix = 1;
  3222. else
  3223. eq_msix = 0;
  3224. for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
  3225. eq = &phwi_context->be_eq[i].q;
  3226. eqe = queue_tail_node(eq);
  3227. num_processed = 0;
  3228. while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
  3229. & EQE_VALID_MASK) {
  3230. AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
  3231. queue_tail_inc(eq);
  3232. eqe = queue_tail_node(eq);
  3233. num_processed++;
  3234. }
  3235. if (num_processed)
  3236. hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
  3237. }
  3238. }
  3239. static void hwi_cleanup_port(struct beiscsi_hba *phba)
  3240. {
  3241. struct be_queue_info *q;
  3242. struct be_ctrl_info *ctrl = &phba->ctrl;
  3243. struct hwi_controller *phwi_ctrlr;
  3244. struct hwi_context_memory *phwi_context;
  3245. int i, eq_for_mcc, ulp_num;
  3246. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3247. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  3248. beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
  3249. /**
  3250. * Purge all EQ entries that may have been left out. This is to
  3251. * workaround a problem we've seen occasionally where driver gets an
  3252. * interrupt with EQ entry bit set after stopping the controller.
  3253. */
  3254. hwi_purge_eq(phba);
  3255. phwi_ctrlr = phba->phwi_ctrlr;
  3256. phwi_context = phwi_ctrlr->phwi_ctxt;
  3257. be_cmd_iscsi_remove_template_hdr(ctrl);
  3258. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3259. q = &phwi_context->be_wrbq[i];
  3260. if (q->created)
  3261. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
  3262. }
  3263. kfree(phwi_context->be_wrbq);
  3264. free_wrb_handles(phba);
  3265. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3266. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3267. q = &phwi_context->be_def_hdrq[ulp_num];
  3268. if (q->created)
  3269. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  3270. q = &phwi_context->be_def_dataq[ulp_num];
  3271. if (q->created)
  3272. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
  3273. }
  3274. }
  3275. beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
  3276. for (i = 0; i < (phba->num_cpus); i++) {
  3277. q = &phwi_context->be_cq[i];
  3278. if (q->created) {
  3279. be_queue_free(phba, q);
  3280. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
  3281. }
  3282. }
  3283. be_mcc_queues_destroy(phba);
  3284. if (phba->pcidev->msix_enabled)
  3285. eq_for_mcc = 1;
  3286. else
  3287. eq_for_mcc = 0;
  3288. for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
  3289. q = &phwi_context->be_eq[i].q;
  3290. if (q->created) {
  3291. be_queue_free(phba, q);
  3292. beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
  3293. }
  3294. }
  3295. /* this ensures complete FW cleanup */
  3296. beiscsi_cmd_function_reset(phba);
  3297. /* last communication, indicate driver is unloading */
  3298. beiscsi_cmd_special_wrb(&phba->ctrl, 0);
  3299. }
  3300. static int hwi_init_port(struct beiscsi_hba *phba)
  3301. {
  3302. struct hwi_controller *phwi_ctrlr;
  3303. struct hwi_context_memory *phwi_context;
  3304. unsigned int def_pdu_ring_sz;
  3305. struct be_ctrl_info *ctrl = &phba->ctrl;
  3306. int status, ulp_num;
  3307. u16 nbufs;
  3308. phwi_ctrlr = phba->phwi_ctrlr;
  3309. phwi_context = phwi_ctrlr->phwi_ctxt;
  3310. /* set port optic state to unknown */
  3311. phba->optic_state = 0xff;
  3312. status = beiscsi_create_eqs(phba, phwi_context);
  3313. if (status != 0) {
  3314. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3315. "BM_%d : EQ not created\n");
  3316. goto error;
  3317. }
  3318. status = be_mcc_queues_create(phba, phwi_context);
  3319. if (status != 0)
  3320. goto error;
  3321. status = beiscsi_check_supported_fw(ctrl, phba);
  3322. if (status != 0) {
  3323. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3324. "BM_%d : Unsupported fw version\n");
  3325. goto error;
  3326. }
  3327. status = beiscsi_create_cqs(phba, phwi_context);
  3328. if (status != 0) {
  3329. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3330. "BM_%d : CQ not created\n");
  3331. goto error;
  3332. }
  3333. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3334. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3335. nbufs = phwi_context->pasync_ctx[ulp_num]->num_entries;
  3336. def_pdu_ring_sz = nbufs * sizeof(struct phys_addr);
  3337. status = beiscsi_create_def_hdr(phba, phwi_context,
  3338. phwi_ctrlr,
  3339. def_pdu_ring_sz,
  3340. ulp_num);
  3341. if (status != 0) {
  3342. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3343. "BM_%d : Default Header not created for ULP : %d\n",
  3344. ulp_num);
  3345. goto error;
  3346. }
  3347. status = beiscsi_create_def_data(phba, phwi_context,
  3348. phwi_ctrlr,
  3349. def_pdu_ring_sz,
  3350. ulp_num);
  3351. if (status != 0) {
  3352. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3353. "BM_%d : Default Data not created for ULP : %d\n",
  3354. ulp_num);
  3355. goto error;
  3356. }
  3357. /**
  3358. * Now that the default PDU rings have been created,
  3359. * let EP know about it.
  3360. */
  3361. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
  3362. ulp_num, nbufs);
  3363. beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
  3364. ulp_num, nbufs);
  3365. }
  3366. }
  3367. status = beiscsi_post_pages(phba);
  3368. if (status != 0) {
  3369. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3370. "BM_%d : Post SGL Pages Failed\n");
  3371. goto error;
  3372. }
  3373. status = beiscsi_post_template_hdr(phba);
  3374. if (status != 0) {
  3375. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3376. "BM_%d : Template HDR Posting for CXN Failed\n");
  3377. }
  3378. status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
  3379. if (status != 0) {
  3380. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3381. "BM_%d : WRB Rings not created\n");
  3382. goto error;
  3383. }
  3384. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3385. uint16_t async_arr_idx = 0;
  3386. if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
  3387. uint16_t cri = 0;
  3388. struct hd_async_context *pasync_ctx;
  3389. pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
  3390. phwi_ctrlr, ulp_num);
  3391. for (cri = 0; cri <
  3392. phba->params.cxns_per_ctrl; cri++) {
  3393. if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
  3394. (phwi_ctrlr, cri))
  3395. pasync_ctx->cid_to_async_cri_map[
  3396. phwi_ctrlr->wrb_context[cri].cid] =
  3397. async_arr_idx++;
  3398. }
  3399. }
  3400. }
  3401. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3402. "BM_%d : hwi_init_port success\n");
  3403. return 0;
  3404. error:
  3405. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3406. "BM_%d : hwi_init_port failed");
  3407. hwi_cleanup_port(phba);
  3408. return status;
  3409. }
  3410. static int hwi_init_controller(struct beiscsi_hba *phba)
  3411. {
  3412. struct hwi_controller *phwi_ctrlr;
  3413. phwi_ctrlr = phba->phwi_ctrlr;
  3414. if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
  3415. phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
  3416. init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
  3417. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3418. "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
  3419. phwi_ctrlr->phwi_ctxt);
  3420. } else {
  3421. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3422. "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
  3423. "than one element.Failing to load\n");
  3424. return -ENOMEM;
  3425. }
  3426. iscsi_init_global_templates(phba);
  3427. if (beiscsi_init_wrb_handle(phba))
  3428. return -ENOMEM;
  3429. if (hwi_init_async_pdu_ctx(phba)) {
  3430. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3431. "BM_%d : hwi_init_async_pdu_ctx failed\n");
  3432. return -ENOMEM;
  3433. }
  3434. if (hwi_init_port(phba) != 0) {
  3435. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3436. "BM_%d : hwi_init_controller failed\n");
  3437. return -ENOMEM;
  3438. }
  3439. return 0;
  3440. }
  3441. static void beiscsi_free_mem(struct beiscsi_hba *phba)
  3442. {
  3443. struct be_mem_descriptor *mem_descr;
  3444. int i, j;
  3445. mem_descr = phba->init_mem;
  3446. i = 0;
  3447. j = 0;
  3448. for (i = 0; i < SE_MEM_MAX; i++) {
  3449. for (j = mem_descr->num_elements; j > 0; j--) {
  3450. pci_free_consistent(phba->pcidev,
  3451. mem_descr->mem_array[j - 1].size,
  3452. mem_descr->mem_array[j - 1].virtual_address,
  3453. (unsigned long)mem_descr->mem_array[j - 1].
  3454. bus_address.u.a64.address);
  3455. }
  3456. kfree(mem_descr->mem_array);
  3457. mem_descr++;
  3458. }
  3459. kfree(phba->init_mem);
  3460. kfree(phba->phwi_ctrlr->wrb_context);
  3461. kfree(phba->phwi_ctrlr);
  3462. }
  3463. static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
  3464. {
  3465. struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
  3466. struct sgl_handle *psgl_handle;
  3467. struct iscsi_sge *pfrag;
  3468. unsigned int arr_index, i, idx;
  3469. unsigned int ulp_icd_start, ulp_num = 0;
  3470. phba->io_sgl_hndl_avbl = 0;
  3471. phba->eh_sgl_hndl_avbl = 0;
  3472. mem_descr_sglh = phba->init_mem;
  3473. mem_descr_sglh += HWI_MEM_SGLH;
  3474. if (1 == mem_descr_sglh->num_elements) {
  3475. phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  3476. phba->params.ios_per_ctrl,
  3477. GFP_KERNEL);
  3478. if (!phba->io_sgl_hndl_base) {
  3479. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3480. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3481. return -ENOMEM;
  3482. }
  3483. phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
  3484. (phba->params.icds_per_ctrl -
  3485. phba->params.ios_per_ctrl),
  3486. GFP_KERNEL);
  3487. if (!phba->eh_sgl_hndl_base) {
  3488. kfree(phba->io_sgl_hndl_base);
  3489. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3490. "BM_%d : Mem Alloc Failed. Failing to load\n");
  3491. return -ENOMEM;
  3492. }
  3493. } else {
  3494. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3495. "BM_%d : HWI_MEM_SGLH is more than one element."
  3496. "Failing to load\n");
  3497. return -ENOMEM;
  3498. }
  3499. arr_index = 0;
  3500. idx = 0;
  3501. while (idx < mem_descr_sglh->num_elements) {
  3502. psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
  3503. for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
  3504. sizeof(struct sgl_handle)); i++) {
  3505. if (arr_index < phba->params.ios_per_ctrl) {
  3506. phba->io_sgl_hndl_base[arr_index] = psgl_handle;
  3507. phba->io_sgl_hndl_avbl++;
  3508. arr_index++;
  3509. } else {
  3510. phba->eh_sgl_hndl_base[arr_index -
  3511. phba->params.ios_per_ctrl] =
  3512. psgl_handle;
  3513. arr_index++;
  3514. phba->eh_sgl_hndl_avbl++;
  3515. }
  3516. psgl_handle++;
  3517. }
  3518. idx++;
  3519. }
  3520. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3521. "BM_%d : phba->io_sgl_hndl_avbl=%d"
  3522. "phba->eh_sgl_hndl_avbl=%d\n",
  3523. phba->io_sgl_hndl_avbl,
  3524. phba->eh_sgl_hndl_avbl);
  3525. mem_descr_sg = phba->init_mem;
  3526. mem_descr_sg += HWI_MEM_SGE;
  3527. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3528. "\n BM_%d : mem_descr_sg->num_elements=%d\n",
  3529. mem_descr_sg->num_elements);
  3530. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
  3531. if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
  3532. break;
  3533. ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
  3534. arr_index = 0;
  3535. idx = 0;
  3536. while (idx < mem_descr_sg->num_elements) {
  3537. pfrag = mem_descr_sg->mem_array[idx].virtual_address;
  3538. for (i = 0;
  3539. i < (mem_descr_sg->mem_array[idx].size) /
  3540. (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
  3541. i++) {
  3542. if (arr_index < phba->params.ios_per_ctrl)
  3543. psgl_handle = phba->io_sgl_hndl_base[arr_index];
  3544. else
  3545. psgl_handle = phba->eh_sgl_hndl_base[arr_index -
  3546. phba->params.ios_per_ctrl];
  3547. psgl_handle->pfrag = pfrag;
  3548. AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
  3549. AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
  3550. pfrag += phba->params.num_sge_per_io;
  3551. psgl_handle->sgl_index = ulp_icd_start + arr_index++;
  3552. }
  3553. idx++;
  3554. }
  3555. phba->io_sgl_free_index = 0;
  3556. phba->io_sgl_alloc_index = 0;
  3557. phba->eh_sgl_free_index = 0;
  3558. phba->eh_sgl_alloc_index = 0;
  3559. return 0;
  3560. }
  3561. static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
  3562. {
  3563. int ret;
  3564. uint16_t i, ulp_num;
  3565. struct ulp_cid_info *ptr_cid_info = NULL;
  3566. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3567. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3568. ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
  3569. GFP_KERNEL);
  3570. if (!ptr_cid_info) {
  3571. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3572. "BM_%d : Failed to allocate memory"
  3573. "for ULP_CID_INFO for ULP : %d\n",
  3574. ulp_num);
  3575. ret = -ENOMEM;
  3576. goto free_memory;
  3577. }
  3578. /* Allocate memory for CID array */
  3579. ptr_cid_info->cid_array =
  3580. kcalloc(BEISCSI_GET_CID_COUNT(phba, ulp_num),
  3581. sizeof(*ptr_cid_info->cid_array),
  3582. GFP_KERNEL);
  3583. if (!ptr_cid_info->cid_array) {
  3584. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3585. "BM_%d : Failed to allocate memory"
  3586. "for CID_ARRAY for ULP : %d\n",
  3587. ulp_num);
  3588. kfree(ptr_cid_info);
  3589. ptr_cid_info = NULL;
  3590. ret = -ENOMEM;
  3591. goto free_memory;
  3592. }
  3593. ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
  3594. phba, ulp_num);
  3595. /* Save the cid_info_array ptr */
  3596. phba->cid_array_info[ulp_num] = ptr_cid_info;
  3597. }
  3598. }
  3599. phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
  3600. phba->params.cxns_per_ctrl, GFP_KERNEL);
  3601. if (!phba->ep_array) {
  3602. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3603. "BM_%d : Failed to allocate memory in "
  3604. "hba_setup_cid_tbls\n");
  3605. ret = -ENOMEM;
  3606. goto free_memory;
  3607. }
  3608. phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
  3609. phba->params.cxns_per_ctrl, GFP_KERNEL);
  3610. if (!phba->conn_table) {
  3611. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3612. "BM_%d : Failed to allocate memory in"
  3613. "hba_setup_cid_tbls\n");
  3614. kfree(phba->ep_array);
  3615. phba->ep_array = NULL;
  3616. ret = -ENOMEM;
  3617. goto free_memory;
  3618. }
  3619. for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
  3620. ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
  3621. ptr_cid_info = phba->cid_array_info[ulp_num];
  3622. ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
  3623. phba->phwi_ctrlr->wrb_context[i].cid;
  3624. }
  3625. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3626. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3627. ptr_cid_info = phba->cid_array_info[ulp_num];
  3628. ptr_cid_info->cid_alloc = 0;
  3629. ptr_cid_info->cid_free = 0;
  3630. }
  3631. }
  3632. return 0;
  3633. free_memory:
  3634. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3635. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3636. ptr_cid_info = phba->cid_array_info[ulp_num];
  3637. if (ptr_cid_info) {
  3638. kfree(ptr_cid_info->cid_array);
  3639. kfree(ptr_cid_info);
  3640. phba->cid_array_info[ulp_num] = NULL;
  3641. }
  3642. }
  3643. }
  3644. return ret;
  3645. }
  3646. static void hwi_enable_intr(struct beiscsi_hba *phba)
  3647. {
  3648. struct be_ctrl_info *ctrl = &phba->ctrl;
  3649. struct hwi_controller *phwi_ctrlr;
  3650. struct hwi_context_memory *phwi_context;
  3651. struct be_queue_info *eq;
  3652. u8 __iomem *addr;
  3653. u32 reg, i;
  3654. u32 enabled;
  3655. phwi_ctrlr = phba->phwi_ctrlr;
  3656. phwi_context = phwi_ctrlr->phwi_ctxt;
  3657. addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
  3658. PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
  3659. reg = ioread32(addr);
  3660. enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3661. if (!enabled) {
  3662. reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3663. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3664. "BM_%d : reg =x%08x addr=%p\n", reg, addr);
  3665. iowrite32(reg, addr);
  3666. }
  3667. if (!phba->pcidev->msix_enabled) {
  3668. eq = &phwi_context->be_eq[0].q;
  3669. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3670. "BM_%d : eq->id=%d\n", eq->id);
  3671. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3672. } else {
  3673. for (i = 0; i <= phba->num_cpus; i++) {
  3674. eq = &phwi_context->be_eq[i].q;
  3675. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  3676. "BM_%d : eq->id=%d\n", eq->id);
  3677. hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
  3678. }
  3679. }
  3680. }
  3681. static void hwi_disable_intr(struct beiscsi_hba *phba)
  3682. {
  3683. struct be_ctrl_info *ctrl = &phba->ctrl;
  3684. u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
  3685. u32 reg = ioread32(addr);
  3686. u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3687. if (enabled) {
  3688. reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
  3689. iowrite32(reg, addr);
  3690. } else
  3691. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  3692. "BM_%d : In hwi_disable_intr, Already Disabled\n");
  3693. }
  3694. static int beiscsi_init_port(struct beiscsi_hba *phba)
  3695. {
  3696. int ret;
  3697. ret = hwi_init_controller(phba);
  3698. if (ret < 0) {
  3699. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3700. "BM_%d : init controller failed\n");
  3701. return ret;
  3702. }
  3703. ret = beiscsi_init_sgl_handle(phba);
  3704. if (ret < 0) {
  3705. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3706. "BM_%d : init sgl handles failed\n");
  3707. goto cleanup_port;
  3708. }
  3709. ret = hba_setup_cid_tbls(phba);
  3710. if (ret < 0) {
  3711. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  3712. "BM_%d : setup CID table failed\n");
  3713. kfree(phba->io_sgl_hndl_base);
  3714. kfree(phba->eh_sgl_hndl_base);
  3715. goto cleanup_port;
  3716. }
  3717. return ret;
  3718. cleanup_port:
  3719. hwi_cleanup_port(phba);
  3720. return ret;
  3721. }
  3722. static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
  3723. {
  3724. struct ulp_cid_info *ptr_cid_info = NULL;
  3725. int ulp_num;
  3726. kfree(phba->io_sgl_hndl_base);
  3727. kfree(phba->eh_sgl_hndl_base);
  3728. kfree(phba->ep_array);
  3729. kfree(phba->conn_table);
  3730. for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
  3731. if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
  3732. ptr_cid_info = phba->cid_array_info[ulp_num];
  3733. if (ptr_cid_info) {
  3734. kfree(ptr_cid_info->cid_array);
  3735. kfree(ptr_cid_info);
  3736. phba->cid_array_info[ulp_num] = NULL;
  3737. }
  3738. }
  3739. }
  3740. }
  3741. /**
  3742. * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
  3743. * @beiscsi_conn: ptr to the conn to be cleaned up
  3744. * @task: ptr to iscsi_task resource to be freed.
  3745. *
  3746. * Free driver mgmt resources binded to CXN.
  3747. **/
  3748. void
  3749. beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
  3750. struct iscsi_task *task)
  3751. {
  3752. struct beiscsi_io_task *io_task;
  3753. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3754. struct hwi_wrb_context *pwrb_context;
  3755. struct hwi_controller *phwi_ctrlr;
  3756. uint16_t cri_index = BE_GET_CRI_FROM_CID(
  3757. beiscsi_conn->beiscsi_conn_cid);
  3758. phwi_ctrlr = phba->phwi_ctrlr;
  3759. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  3760. io_task = task->dd_data;
  3761. if (io_task->pwrb_handle) {
  3762. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  3763. io_task->pwrb_handle = NULL;
  3764. }
  3765. if (io_task->psgl_handle) {
  3766. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  3767. io_task->psgl_handle = NULL;
  3768. }
  3769. if (io_task->mtask_addr) {
  3770. pci_unmap_single(phba->pcidev,
  3771. io_task->mtask_addr,
  3772. io_task->mtask_data_count,
  3773. PCI_DMA_TODEVICE);
  3774. io_task->mtask_addr = 0;
  3775. }
  3776. }
  3777. /**
  3778. * beiscsi_cleanup_task()- Free driver resources of the task
  3779. * @task: ptr to the iscsi task
  3780. *
  3781. **/
  3782. static void beiscsi_cleanup_task(struct iscsi_task *task)
  3783. {
  3784. struct beiscsi_io_task *io_task = task->dd_data;
  3785. struct iscsi_conn *conn = task->conn;
  3786. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3787. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3788. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3789. struct hwi_wrb_context *pwrb_context;
  3790. struct hwi_controller *phwi_ctrlr;
  3791. uint16_t cri_index = BE_GET_CRI_FROM_CID(
  3792. beiscsi_conn->beiscsi_conn_cid);
  3793. phwi_ctrlr = phba->phwi_ctrlr;
  3794. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  3795. if (io_task->cmd_bhs) {
  3796. dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  3797. io_task->bhs_pa.u.a64.address);
  3798. io_task->cmd_bhs = NULL;
  3799. task->hdr = NULL;
  3800. }
  3801. if (task->sc) {
  3802. if (io_task->pwrb_handle) {
  3803. free_wrb_handle(phba, pwrb_context,
  3804. io_task->pwrb_handle);
  3805. io_task->pwrb_handle = NULL;
  3806. }
  3807. if (io_task->psgl_handle) {
  3808. free_io_sgl_handle(phba, io_task->psgl_handle);
  3809. io_task->psgl_handle = NULL;
  3810. }
  3811. if (io_task->scsi_cmnd) {
  3812. if (io_task->num_sg)
  3813. scsi_dma_unmap(io_task->scsi_cmnd);
  3814. io_task->scsi_cmnd = NULL;
  3815. }
  3816. } else {
  3817. if (!beiscsi_conn->login_in_progress)
  3818. beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
  3819. }
  3820. }
  3821. void
  3822. beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
  3823. struct beiscsi_offload_params *params)
  3824. {
  3825. struct wrb_handle *pwrb_handle;
  3826. struct hwi_wrb_context *pwrb_context = NULL;
  3827. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3828. struct iscsi_task *task = beiscsi_conn->task;
  3829. struct iscsi_session *session = task->conn->session;
  3830. u32 doorbell = 0;
  3831. /*
  3832. * We can always use 0 here because it is reserved by libiscsi for
  3833. * login/startup related tasks.
  3834. */
  3835. beiscsi_conn->login_in_progress = 0;
  3836. spin_lock_bh(&session->back_lock);
  3837. beiscsi_cleanup_task(task);
  3838. spin_unlock_bh(&session->back_lock);
  3839. pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
  3840. &pwrb_context);
  3841. /* Check for the adapter family */
  3842. if (is_chip_be2_be3r(phba))
  3843. beiscsi_offload_cxn_v0(params, pwrb_handle,
  3844. phba->init_mem,
  3845. pwrb_context);
  3846. else
  3847. beiscsi_offload_cxn_v2(params, pwrb_handle,
  3848. pwrb_context);
  3849. be_dws_le_to_cpu(pwrb_handle->pwrb,
  3850. sizeof(struct iscsi_target_context_update_wrb));
  3851. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  3852. doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
  3853. << DB_DEF_PDU_WRB_INDEX_SHIFT;
  3854. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  3855. iowrite32(doorbell, phba->db_va +
  3856. beiscsi_conn->doorbell_offset);
  3857. /*
  3858. * There is no completion for CONTEXT_UPDATE. The completion of next
  3859. * WRB posted guarantees FW's processing and DMA'ing of it.
  3860. * Use beiscsi_put_wrb_handle to put it back in the pool which makes
  3861. * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
  3862. */
  3863. beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
  3864. phba->params.wrbs_per_cxn);
  3865. beiscsi_log(phba, KERN_INFO,
  3866. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3867. "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
  3868. pwrb_handle, pwrb_context->free_index,
  3869. pwrb_context->wrb_handles_available);
  3870. }
  3871. static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
  3872. int *index, int *age)
  3873. {
  3874. *index = (int)itt;
  3875. if (age)
  3876. *age = conn->session->age;
  3877. }
  3878. /**
  3879. * beiscsi_alloc_pdu - allocates pdu and related resources
  3880. * @task: libiscsi task
  3881. * @opcode: opcode of pdu for task
  3882. *
  3883. * This is called with the session lock held. It will allocate
  3884. * the wrb and sgl if needed for the command. And it will prep
  3885. * the pdu's itt. beiscsi_parse_pdu will later translate
  3886. * the pdu itt to the libiscsi task itt.
  3887. */
  3888. static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  3889. {
  3890. struct beiscsi_io_task *io_task = task->dd_data;
  3891. struct iscsi_conn *conn = task->conn;
  3892. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  3893. struct beiscsi_hba *phba = beiscsi_conn->phba;
  3894. struct hwi_wrb_context *pwrb_context;
  3895. struct hwi_controller *phwi_ctrlr;
  3896. itt_t itt;
  3897. uint16_t cri_index = 0;
  3898. struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
  3899. dma_addr_t paddr;
  3900. io_task->cmd_bhs = dma_pool_alloc(beiscsi_sess->bhs_pool,
  3901. GFP_ATOMIC, &paddr);
  3902. if (!io_task->cmd_bhs)
  3903. return -ENOMEM;
  3904. io_task->bhs_pa.u.a64.address = paddr;
  3905. io_task->libiscsi_itt = (itt_t)task->itt;
  3906. io_task->conn = beiscsi_conn;
  3907. task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
  3908. task->hdr_max = sizeof(struct be_cmd_bhs);
  3909. io_task->psgl_handle = NULL;
  3910. io_task->pwrb_handle = NULL;
  3911. if (task->sc) {
  3912. io_task->psgl_handle = alloc_io_sgl_handle(phba);
  3913. if (!io_task->psgl_handle) {
  3914. beiscsi_log(phba, KERN_ERR,
  3915. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3916. "BM_%d : Alloc of IO_SGL_ICD Failed"
  3917. "for the CID : %d\n",
  3918. beiscsi_conn->beiscsi_conn_cid);
  3919. goto free_hndls;
  3920. }
  3921. io_task->pwrb_handle = alloc_wrb_handle(phba,
  3922. beiscsi_conn->beiscsi_conn_cid,
  3923. &io_task->pwrb_context);
  3924. if (!io_task->pwrb_handle) {
  3925. beiscsi_log(phba, KERN_ERR,
  3926. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3927. "BM_%d : Alloc of WRB_HANDLE Failed"
  3928. "for the CID : %d\n",
  3929. beiscsi_conn->beiscsi_conn_cid);
  3930. goto free_io_hndls;
  3931. }
  3932. } else {
  3933. io_task->scsi_cmnd = NULL;
  3934. if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
  3935. beiscsi_conn->task = task;
  3936. if (!beiscsi_conn->login_in_progress) {
  3937. io_task->psgl_handle = (struct sgl_handle *)
  3938. alloc_mgmt_sgl_handle(phba);
  3939. if (!io_task->psgl_handle) {
  3940. beiscsi_log(phba, KERN_ERR,
  3941. BEISCSI_LOG_IO |
  3942. BEISCSI_LOG_CONFIG,
  3943. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  3944. "for the CID : %d\n",
  3945. beiscsi_conn->
  3946. beiscsi_conn_cid);
  3947. goto free_hndls;
  3948. }
  3949. beiscsi_conn->login_in_progress = 1;
  3950. beiscsi_conn->plogin_sgl_handle =
  3951. io_task->psgl_handle;
  3952. io_task->pwrb_handle =
  3953. alloc_wrb_handle(phba,
  3954. beiscsi_conn->beiscsi_conn_cid,
  3955. &io_task->pwrb_context);
  3956. if (!io_task->pwrb_handle) {
  3957. beiscsi_log(phba, KERN_ERR,
  3958. BEISCSI_LOG_IO |
  3959. BEISCSI_LOG_CONFIG,
  3960. "BM_%d : Alloc of WRB_HANDLE Failed"
  3961. "for the CID : %d\n",
  3962. beiscsi_conn->
  3963. beiscsi_conn_cid);
  3964. goto free_mgmt_hndls;
  3965. }
  3966. beiscsi_conn->plogin_wrb_handle =
  3967. io_task->pwrb_handle;
  3968. } else {
  3969. io_task->psgl_handle =
  3970. beiscsi_conn->plogin_sgl_handle;
  3971. io_task->pwrb_handle =
  3972. beiscsi_conn->plogin_wrb_handle;
  3973. }
  3974. } else {
  3975. io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
  3976. if (!io_task->psgl_handle) {
  3977. beiscsi_log(phba, KERN_ERR,
  3978. BEISCSI_LOG_IO |
  3979. BEISCSI_LOG_CONFIG,
  3980. "BM_%d : Alloc of MGMT_SGL_ICD Failed"
  3981. "for the CID : %d\n",
  3982. beiscsi_conn->
  3983. beiscsi_conn_cid);
  3984. goto free_hndls;
  3985. }
  3986. io_task->pwrb_handle =
  3987. alloc_wrb_handle(phba,
  3988. beiscsi_conn->beiscsi_conn_cid,
  3989. &io_task->pwrb_context);
  3990. if (!io_task->pwrb_handle) {
  3991. beiscsi_log(phba, KERN_ERR,
  3992. BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
  3993. "BM_%d : Alloc of WRB_HANDLE Failed"
  3994. "for the CID : %d\n",
  3995. beiscsi_conn->beiscsi_conn_cid);
  3996. goto free_mgmt_hndls;
  3997. }
  3998. }
  3999. }
  4000. itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
  4001. wrb_index << 16) | (unsigned int)
  4002. (io_task->psgl_handle->sgl_index));
  4003. io_task->pwrb_handle->pio_handle = task;
  4004. io_task->cmd_bhs->iscsi_hdr.itt = itt;
  4005. return 0;
  4006. free_io_hndls:
  4007. free_io_sgl_handle(phba, io_task->psgl_handle);
  4008. goto free_hndls;
  4009. free_mgmt_hndls:
  4010. free_mgmt_sgl_handle(phba, io_task->psgl_handle);
  4011. io_task->psgl_handle = NULL;
  4012. free_hndls:
  4013. phwi_ctrlr = phba->phwi_ctrlr;
  4014. cri_index = BE_GET_CRI_FROM_CID(
  4015. beiscsi_conn->beiscsi_conn_cid);
  4016. pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
  4017. if (io_task->pwrb_handle)
  4018. free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
  4019. io_task->pwrb_handle = NULL;
  4020. dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
  4021. io_task->bhs_pa.u.a64.address);
  4022. io_task->cmd_bhs = NULL;
  4023. return -ENOMEM;
  4024. }
  4025. static int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
  4026. unsigned int num_sg, unsigned int xferlen,
  4027. unsigned int writedir)
  4028. {
  4029. struct beiscsi_io_task *io_task = task->dd_data;
  4030. struct iscsi_conn *conn = task->conn;
  4031. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4032. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4033. struct iscsi_wrb *pwrb = NULL;
  4034. unsigned int doorbell = 0;
  4035. pwrb = io_task->pwrb_handle->pwrb;
  4036. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  4037. if (writedir) {
  4038. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
  4039. INI_WR_CMD);
  4040. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
  4041. } else {
  4042. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
  4043. INI_RD_CMD);
  4044. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
  4045. }
  4046. io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
  4047. type, pwrb);
  4048. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
  4049. cpu_to_be16(*(unsigned short *)
  4050. &io_task->cmd_bhs->iscsi_hdr.lun));
  4051. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
  4052. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
  4053. io_task->pwrb_handle->wrb_index);
  4054. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
  4055. be32_to_cpu(task->cmdsn));
  4056. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
  4057. io_task->psgl_handle->sgl_index);
  4058. hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
  4059. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
  4060. io_task->pwrb_handle->wrb_index);
  4061. if (io_task->pwrb_context->plast_wrb)
  4062. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
  4063. io_task->pwrb_context->plast_wrb,
  4064. io_task->pwrb_handle->wrb_index);
  4065. io_task->pwrb_context->plast_wrb = pwrb;
  4066. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  4067. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  4068. doorbell |= (io_task->pwrb_handle->wrb_index &
  4069. DB_DEF_PDU_WRB_INDEX_MASK) <<
  4070. DB_DEF_PDU_WRB_INDEX_SHIFT;
  4071. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4072. iowrite32(doorbell, phba->db_va +
  4073. beiscsi_conn->doorbell_offset);
  4074. return 0;
  4075. }
  4076. static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
  4077. unsigned int num_sg, unsigned int xferlen,
  4078. unsigned int writedir)
  4079. {
  4080. struct beiscsi_io_task *io_task = task->dd_data;
  4081. struct iscsi_conn *conn = task->conn;
  4082. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4083. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4084. struct iscsi_wrb *pwrb = NULL;
  4085. unsigned int doorbell = 0;
  4086. pwrb = io_task->pwrb_handle->pwrb;
  4087. io_task->bhs_len = sizeof(struct be_cmd_bhs);
  4088. if (writedir) {
  4089. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  4090. INI_WR_CMD);
  4091. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
  4092. } else {
  4093. AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
  4094. INI_RD_CMD);
  4095. AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
  4096. }
  4097. io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
  4098. type, pwrb);
  4099. AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
  4100. cpu_to_be16(*(unsigned short *)
  4101. &io_task->cmd_bhs->iscsi_hdr.lun));
  4102. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
  4103. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  4104. io_task->pwrb_handle->wrb_index);
  4105. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  4106. be32_to_cpu(task->cmdsn));
  4107. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  4108. io_task->psgl_handle->sgl_index);
  4109. hwi_write_sgl(pwrb, sg, num_sg, io_task);
  4110. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  4111. io_task->pwrb_handle->wrb_index);
  4112. if (io_task->pwrb_context->plast_wrb)
  4113. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
  4114. io_task->pwrb_context->plast_wrb,
  4115. io_task->pwrb_handle->wrb_index);
  4116. io_task->pwrb_context->plast_wrb = pwrb;
  4117. be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
  4118. doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
  4119. doorbell |= (io_task->pwrb_handle->wrb_index &
  4120. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  4121. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4122. iowrite32(doorbell, phba->db_va +
  4123. beiscsi_conn->doorbell_offset);
  4124. return 0;
  4125. }
  4126. static int beiscsi_mtask(struct iscsi_task *task)
  4127. {
  4128. struct beiscsi_io_task *io_task = task->dd_data;
  4129. struct iscsi_conn *conn = task->conn;
  4130. struct beiscsi_conn *beiscsi_conn = conn->dd_data;
  4131. struct beiscsi_hba *phba = beiscsi_conn->phba;
  4132. struct iscsi_wrb *pwrb = NULL;
  4133. unsigned int doorbell = 0;
  4134. unsigned int cid;
  4135. unsigned int pwrb_typeoffset = 0;
  4136. int ret = 0;
  4137. cid = beiscsi_conn->beiscsi_conn_cid;
  4138. pwrb = io_task->pwrb_handle->pwrb;
  4139. if (is_chip_be2_be3r(phba)) {
  4140. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
  4141. be32_to_cpu(task->cmdsn));
  4142. AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
  4143. io_task->pwrb_handle->wrb_index);
  4144. AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
  4145. io_task->psgl_handle->sgl_index);
  4146. AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
  4147. task->data_count);
  4148. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
  4149. io_task->pwrb_handle->wrb_index);
  4150. if (io_task->pwrb_context->plast_wrb)
  4151. AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
  4152. io_task->pwrb_context->plast_wrb,
  4153. io_task->pwrb_handle->wrb_index);
  4154. io_task->pwrb_context->plast_wrb = pwrb;
  4155. pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
  4156. } else {
  4157. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
  4158. be32_to_cpu(task->cmdsn));
  4159. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
  4160. io_task->pwrb_handle->wrb_index);
  4161. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
  4162. io_task->psgl_handle->sgl_index);
  4163. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
  4164. task->data_count);
  4165. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
  4166. io_task->pwrb_handle->wrb_index);
  4167. if (io_task->pwrb_context->plast_wrb)
  4168. AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
  4169. io_task->pwrb_context->plast_wrb,
  4170. io_task->pwrb_handle->wrb_index);
  4171. io_task->pwrb_context->plast_wrb = pwrb;
  4172. pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
  4173. }
  4174. switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
  4175. case ISCSI_OP_LOGIN:
  4176. AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
  4177. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4178. ret = hwi_write_buffer(pwrb, task);
  4179. break;
  4180. case ISCSI_OP_NOOP_OUT:
  4181. if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
  4182. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4183. if (is_chip_be2_be3r(phba))
  4184. AMAP_SET_BITS(struct amap_iscsi_wrb,
  4185. dmsg, pwrb, 1);
  4186. else
  4187. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  4188. dmsg, pwrb, 1);
  4189. } else {
  4190. ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
  4191. if (is_chip_be2_be3r(phba))
  4192. AMAP_SET_BITS(struct amap_iscsi_wrb,
  4193. dmsg, pwrb, 0);
  4194. else
  4195. AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
  4196. dmsg, pwrb, 0);
  4197. }
  4198. ret = hwi_write_buffer(pwrb, task);
  4199. break;
  4200. case ISCSI_OP_TEXT:
  4201. ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
  4202. ret = hwi_write_buffer(pwrb, task);
  4203. break;
  4204. case ISCSI_OP_SCSI_TMFUNC:
  4205. ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
  4206. ret = hwi_write_buffer(pwrb, task);
  4207. break;
  4208. case ISCSI_OP_LOGOUT:
  4209. ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
  4210. ret = hwi_write_buffer(pwrb, task);
  4211. break;
  4212. default:
  4213. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4214. "BM_%d : opcode =%d Not supported\n",
  4215. task->hdr->opcode & ISCSI_OPCODE_MASK);
  4216. return -EINVAL;
  4217. }
  4218. if (ret)
  4219. return ret;
  4220. /* Set the task type */
  4221. io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
  4222. AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
  4223. AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
  4224. doorbell |= cid & DB_WRB_POST_CID_MASK;
  4225. doorbell |= (io_task->pwrb_handle->wrb_index &
  4226. DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
  4227. doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
  4228. iowrite32(doorbell, phba->db_va +
  4229. beiscsi_conn->doorbell_offset);
  4230. return 0;
  4231. }
  4232. static int beiscsi_task_xmit(struct iscsi_task *task)
  4233. {
  4234. struct beiscsi_io_task *io_task = task->dd_data;
  4235. struct scsi_cmnd *sc = task->sc;
  4236. struct beiscsi_hba *phba;
  4237. struct scatterlist *sg;
  4238. int num_sg;
  4239. unsigned int writedir = 0, xferlen = 0;
  4240. phba = io_task->conn->phba;
  4241. /**
  4242. * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
  4243. * operational if FW still gets heartbeat from EP FW. Is management
  4244. * path really needed to continue further?
  4245. */
  4246. if (!beiscsi_hba_is_online(phba))
  4247. return -EIO;
  4248. if (!io_task->conn->login_in_progress)
  4249. task->hdr->exp_statsn = 0;
  4250. if (!sc)
  4251. return beiscsi_mtask(task);
  4252. io_task->scsi_cmnd = sc;
  4253. io_task->num_sg = 0;
  4254. num_sg = scsi_dma_map(sc);
  4255. if (num_sg < 0) {
  4256. beiscsi_log(phba, KERN_ERR,
  4257. BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
  4258. "BM_%d : scsi_dma_map Failed "
  4259. "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
  4260. be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
  4261. io_task->libiscsi_itt, scsi_bufflen(sc));
  4262. return num_sg;
  4263. }
  4264. /**
  4265. * For scsi cmd task, check num_sg before unmapping in cleanup_task.
  4266. * For management task, cleanup_task checks mtask_addr before unmapping.
  4267. */
  4268. io_task->num_sg = num_sg;
  4269. xferlen = scsi_bufflen(sc);
  4270. sg = scsi_sglist(sc);
  4271. if (sc->sc_data_direction == DMA_TO_DEVICE)
  4272. writedir = 1;
  4273. else
  4274. writedir = 0;
  4275. return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
  4276. }
  4277. /**
  4278. * beiscsi_bsg_request - handle bsg request from ISCSI transport
  4279. * @job: job to handle
  4280. */
  4281. static int beiscsi_bsg_request(struct bsg_job *job)
  4282. {
  4283. struct Scsi_Host *shost;
  4284. struct beiscsi_hba *phba;
  4285. struct iscsi_bsg_request *bsg_req = job->request;
  4286. int rc = -EINVAL;
  4287. unsigned int tag;
  4288. struct be_dma_mem nonemb_cmd;
  4289. struct be_cmd_resp_hdr *resp;
  4290. struct iscsi_bsg_reply *bsg_reply = job->reply;
  4291. unsigned short status, extd_status;
  4292. shost = iscsi_job_to_shost(job);
  4293. phba = iscsi_host_priv(shost);
  4294. if (!beiscsi_hba_is_online(phba)) {
  4295. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
  4296. "BM_%d : HBA in error 0x%lx\n", phba->state);
  4297. return -ENXIO;
  4298. }
  4299. switch (bsg_req->msgcode) {
  4300. case ISCSI_BSG_HST_VENDOR:
  4301. nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
  4302. job->request_payload.payload_len,
  4303. &nonemb_cmd.dma);
  4304. if (nonemb_cmd.va == NULL) {
  4305. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4306. "BM_%d : Failed to allocate memory for "
  4307. "beiscsi_bsg_request\n");
  4308. return -ENOMEM;
  4309. }
  4310. tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
  4311. &nonemb_cmd);
  4312. if (!tag) {
  4313. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4314. "BM_%d : MBX Tag Allocation Failed\n");
  4315. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4316. nonemb_cmd.va, nonemb_cmd.dma);
  4317. return -EAGAIN;
  4318. }
  4319. rc = wait_event_interruptible_timeout(
  4320. phba->ctrl.mcc_wait[tag],
  4321. phba->ctrl.mcc_tag_status[tag],
  4322. msecs_to_jiffies(
  4323. BEISCSI_HOST_MBX_TIMEOUT));
  4324. if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
  4325. clear_bit(MCC_TAG_STATE_RUNNING,
  4326. &phba->ctrl.ptag_state[tag].tag_state);
  4327. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4328. nonemb_cmd.va, nonemb_cmd.dma);
  4329. return -EIO;
  4330. }
  4331. extd_status = (phba->ctrl.mcc_tag_status[tag] &
  4332. CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
  4333. status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
  4334. free_mcc_wrb(&phba->ctrl, tag);
  4335. resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
  4336. sg_copy_from_buffer(job->reply_payload.sg_list,
  4337. job->reply_payload.sg_cnt,
  4338. nonemb_cmd.va, (resp->response_length
  4339. + sizeof(*resp)));
  4340. bsg_reply->reply_payload_rcv_len = resp->response_length;
  4341. bsg_reply->result = status;
  4342. bsg_job_done(job, bsg_reply->result,
  4343. bsg_reply->reply_payload_rcv_len);
  4344. pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
  4345. nonemb_cmd.va, nonemb_cmd.dma);
  4346. if (status || extd_status) {
  4347. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4348. "BM_%d : MBX Cmd Failed"
  4349. " status = %d extd_status = %d\n",
  4350. status, extd_status);
  4351. return -EIO;
  4352. } else {
  4353. rc = 0;
  4354. }
  4355. break;
  4356. default:
  4357. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
  4358. "BM_%d : Unsupported bsg command: 0x%x\n",
  4359. bsg_req->msgcode);
  4360. break;
  4361. }
  4362. return rc;
  4363. }
  4364. static void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
  4365. {
  4366. /* Set the logging parameter */
  4367. beiscsi_log_enable_init(phba, beiscsi_log_enable);
  4368. }
  4369. void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
  4370. {
  4371. if (phba->boot_struct.boot_kset)
  4372. return;
  4373. /* skip if boot work is already in progress */
  4374. if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
  4375. return;
  4376. phba->boot_struct.retry = 3;
  4377. phba->boot_struct.tag = 0;
  4378. phba->boot_struct.s_handle = s_handle;
  4379. phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
  4380. schedule_work(&phba->boot_work);
  4381. }
  4382. /**
  4383. * Boot flag info for iscsi-utilities
  4384. * Bit 0 Block valid flag
  4385. * Bit 1 Firmware booting selected
  4386. */
  4387. #define BEISCSI_SYSFS_ISCSI_BOOT_FLAGS 3
  4388. static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
  4389. {
  4390. struct beiscsi_hba *phba = data;
  4391. struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
  4392. struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
  4393. char *str = buf;
  4394. int rc = -EPERM;
  4395. switch (type) {
  4396. case ISCSI_BOOT_TGT_NAME:
  4397. rc = sprintf(buf, "%.*s\n",
  4398. (int)strlen(boot_sess->target_name),
  4399. (char *)&boot_sess->target_name);
  4400. break;
  4401. case ISCSI_BOOT_TGT_IP_ADDR:
  4402. if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
  4403. rc = sprintf(buf, "%pI4\n",
  4404. (char *)&boot_conn->dest_ipaddr.addr);
  4405. else
  4406. rc = sprintf(str, "%pI6\n",
  4407. (char *)&boot_conn->dest_ipaddr.addr);
  4408. break;
  4409. case ISCSI_BOOT_TGT_PORT:
  4410. rc = sprintf(str, "%d\n", boot_conn->dest_port);
  4411. break;
  4412. case ISCSI_BOOT_TGT_CHAP_NAME:
  4413. rc = sprintf(str, "%.*s\n",
  4414. boot_conn->negotiated_login_options.auth_data.chap.
  4415. target_chap_name_length,
  4416. (char *)&boot_conn->negotiated_login_options.
  4417. auth_data.chap.target_chap_name);
  4418. break;
  4419. case ISCSI_BOOT_TGT_CHAP_SECRET:
  4420. rc = sprintf(str, "%.*s\n",
  4421. boot_conn->negotiated_login_options.auth_data.chap.
  4422. target_secret_length,
  4423. (char *)&boot_conn->negotiated_login_options.
  4424. auth_data.chap.target_secret);
  4425. break;
  4426. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  4427. rc = sprintf(str, "%.*s\n",
  4428. boot_conn->negotiated_login_options.auth_data.chap.
  4429. intr_chap_name_length,
  4430. (char *)&boot_conn->negotiated_login_options.
  4431. auth_data.chap.intr_chap_name);
  4432. break;
  4433. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  4434. rc = sprintf(str, "%.*s\n",
  4435. boot_conn->negotiated_login_options.auth_data.chap.
  4436. intr_secret_length,
  4437. (char *)&boot_conn->negotiated_login_options.
  4438. auth_data.chap.intr_secret);
  4439. break;
  4440. case ISCSI_BOOT_TGT_FLAGS:
  4441. rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
  4442. break;
  4443. case ISCSI_BOOT_TGT_NIC_ASSOC:
  4444. rc = sprintf(str, "0\n");
  4445. break;
  4446. }
  4447. return rc;
  4448. }
  4449. static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
  4450. {
  4451. struct beiscsi_hba *phba = data;
  4452. char *str = buf;
  4453. int rc = -EPERM;
  4454. switch (type) {
  4455. case ISCSI_BOOT_INI_INITIATOR_NAME:
  4456. rc = sprintf(str, "%s\n",
  4457. phba->boot_struct.boot_sess.initiator_iscsiname);
  4458. break;
  4459. }
  4460. return rc;
  4461. }
  4462. static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
  4463. {
  4464. struct beiscsi_hba *phba = data;
  4465. char *str = buf;
  4466. int rc = -EPERM;
  4467. switch (type) {
  4468. case ISCSI_BOOT_ETH_FLAGS:
  4469. rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
  4470. break;
  4471. case ISCSI_BOOT_ETH_INDEX:
  4472. rc = sprintf(str, "0\n");
  4473. break;
  4474. case ISCSI_BOOT_ETH_MAC:
  4475. rc = beiscsi_get_macaddr(str, phba);
  4476. break;
  4477. }
  4478. return rc;
  4479. }
  4480. static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
  4481. {
  4482. umode_t rc = 0;
  4483. switch (type) {
  4484. case ISCSI_BOOT_TGT_NAME:
  4485. case ISCSI_BOOT_TGT_IP_ADDR:
  4486. case ISCSI_BOOT_TGT_PORT:
  4487. case ISCSI_BOOT_TGT_CHAP_NAME:
  4488. case ISCSI_BOOT_TGT_CHAP_SECRET:
  4489. case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  4490. case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  4491. case ISCSI_BOOT_TGT_NIC_ASSOC:
  4492. case ISCSI_BOOT_TGT_FLAGS:
  4493. rc = S_IRUGO;
  4494. break;
  4495. }
  4496. return rc;
  4497. }
  4498. static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
  4499. {
  4500. umode_t rc = 0;
  4501. switch (type) {
  4502. case ISCSI_BOOT_INI_INITIATOR_NAME:
  4503. rc = S_IRUGO;
  4504. break;
  4505. }
  4506. return rc;
  4507. }
  4508. static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
  4509. {
  4510. umode_t rc = 0;
  4511. switch (type) {
  4512. case ISCSI_BOOT_ETH_FLAGS:
  4513. case ISCSI_BOOT_ETH_MAC:
  4514. case ISCSI_BOOT_ETH_INDEX:
  4515. rc = S_IRUGO;
  4516. break;
  4517. }
  4518. return rc;
  4519. }
  4520. static void beiscsi_boot_kobj_release(void *data)
  4521. {
  4522. struct beiscsi_hba *phba = data;
  4523. scsi_host_put(phba->shost);
  4524. }
  4525. static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
  4526. {
  4527. struct boot_struct *bs = &phba->boot_struct;
  4528. struct iscsi_boot_kobj *boot_kobj;
  4529. if (bs->boot_kset) {
  4530. __beiscsi_log(phba, KERN_ERR,
  4531. "BM_%d: boot_kset already created\n");
  4532. return 0;
  4533. }
  4534. bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
  4535. if (!bs->boot_kset) {
  4536. __beiscsi_log(phba, KERN_ERR,
  4537. "BM_%d: boot_kset alloc failed\n");
  4538. return -ENOMEM;
  4539. }
  4540. /* get shost ref because the show function will refer phba */
  4541. if (!scsi_host_get(phba->shost))
  4542. goto free_kset;
  4543. boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
  4544. beiscsi_show_boot_tgt_info,
  4545. beiscsi_tgt_get_attr_visibility,
  4546. beiscsi_boot_kobj_release);
  4547. if (!boot_kobj)
  4548. goto put_shost;
  4549. if (!scsi_host_get(phba->shost))
  4550. goto free_kset;
  4551. boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
  4552. beiscsi_show_boot_ini_info,
  4553. beiscsi_ini_get_attr_visibility,
  4554. beiscsi_boot_kobj_release);
  4555. if (!boot_kobj)
  4556. goto put_shost;
  4557. if (!scsi_host_get(phba->shost))
  4558. goto free_kset;
  4559. boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
  4560. beiscsi_show_boot_eth_info,
  4561. beiscsi_eth_get_attr_visibility,
  4562. beiscsi_boot_kobj_release);
  4563. if (!boot_kobj)
  4564. goto put_shost;
  4565. return 0;
  4566. put_shost:
  4567. scsi_host_put(phba->shost);
  4568. free_kset:
  4569. iscsi_boot_destroy_kset(bs->boot_kset);
  4570. bs->boot_kset = NULL;
  4571. return -ENOMEM;
  4572. }
  4573. static void beiscsi_boot_work(struct work_struct *work)
  4574. {
  4575. struct beiscsi_hba *phba =
  4576. container_of(work, struct beiscsi_hba, boot_work);
  4577. struct boot_struct *bs = &phba->boot_struct;
  4578. unsigned int tag = 0;
  4579. if (!beiscsi_hba_is_online(phba))
  4580. return;
  4581. beiscsi_log(phba, KERN_INFO,
  4582. BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
  4583. "BM_%d : %s action %d\n",
  4584. __func__, phba->boot_struct.action);
  4585. switch (phba->boot_struct.action) {
  4586. case BEISCSI_BOOT_REOPEN_SESS:
  4587. tag = beiscsi_boot_reopen_sess(phba);
  4588. break;
  4589. case BEISCSI_BOOT_GET_SHANDLE:
  4590. tag = __beiscsi_boot_get_shandle(phba, 1);
  4591. break;
  4592. case BEISCSI_BOOT_GET_SINFO:
  4593. tag = beiscsi_boot_get_sinfo(phba);
  4594. break;
  4595. case BEISCSI_BOOT_LOGOUT_SESS:
  4596. tag = beiscsi_boot_logout_sess(phba);
  4597. break;
  4598. case BEISCSI_BOOT_CREATE_KSET:
  4599. beiscsi_boot_create_kset(phba);
  4600. /**
  4601. * updated boot_kset is made visible to all before
  4602. * ending the boot work.
  4603. */
  4604. mb();
  4605. clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
  4606. return;
  4607. }
  4608. if (!tag) {
  4609. if (bs->retry--)
  4610. schedule_work(&phba->boot_work);
  4611. else
  4612. clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
  4613. }
  4614. }
  4615. static void beiscsi_eqd_update_work(struct work_struct *work)
  4616. {
  4617. struct hwi_context_memory *phwi_context;
  4618. struct be_set_eqd set_eqd[MAX_CPUS];
  4619. struct hwi_controller *phwi_ctrlr;
  4620. struct be_eq_obj *pbe_eq;
  4621. struct beiscsi_hba *phba;
  4622. unsigned int pps, delta;
  4623. struct be_aic_obj *aic;
  4624. int eqd, i, num = 0;
  4625. unsigned long now;
  4626. phba = container_of(work, struct beiscsi_hba, eqd_update.work);
  4627. if (!beiscsi_hba_is_online(phba))
  4628. return;
  4629. phwi_ctrlr = phba->phwi_ctrlr;
  4630. phwi_context = phwi_ctrlr->phwi_ctxt;
  4631. for (i = 0; i <= phba->num_cpus; i++) {
  4632. aic = &phba->aic_obj[i];
  4633. pbe_eq = &phwi_context->be_eq[i];
  4634. now = jiffies;
  4635. if (!aic->jiffies || time_before(now, aic->jiffies) ||
  4636. pbe_eq->cq_count < aic->eq_prev) {
  4637. aic->jiffies = now;
  4638. aic->eq_prev = pbe_eq->cq_count;
  4639. continue;
  4640. }
  4641. delta = jiffies_to_msecs(now - aic->jiffies);
  4642. pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
  4643. eqd = (pps / 1500) << 2;
  4644. if (eqd < 8)
  4645. eqd = 0;
  4646. eqd = min_t(u32, eqd, BEISCSI_EQ_DELAY_MAX);
  4647. eqd = max_t(u32, eqd, BEISCSI_EQ_DELAY_MIN);
  4648. aic->jiffies = now;
  4649. aic->eq_prev = pbe_eq->cq_count;
  4650. if (eqd != aic->prev_eqd) {
  4651. set_eqd[num].delay_multiplier = (eqd * 65)/100;
  4652. set_eqd[num].eq_id = pbe_eq->q.id;
  4653. aic->prev_eqd = eqd;
  4654. num++;
  4655. }
  4656. }
  4657. if (num)
  4658. /* completion of this is ignored */
  4659. beiscsi_modify_eq_delay(phba, set_eqd, num);
  4660. schedule_delayed_work(&phba->eqd_update,
  4661. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  4662. }
  4663. static void beiscsi_hw_tpe_check(struct timer_list *t)
  4664. {
  4665. struct beiscsi_hba *phba = from_timer(phba, t, hw_check);
  4666. u32 wait;
  4667. /* if not TPE, do nothing */
  4668. if (!beiscsi_detect_tpe(phba))
  4669. return;
  4670. /* wait default 4000ms before recovering */
  4671. wait = 4000;
  4672. if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
  4673. wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
  4674. queue_delayed_work(phba->wq, &phba->recover_port,
  4675. msecs_to_jiffies(wait));
  4676. }
  4677. static void beiscsi_hw_health_check(struct timer_list *t)
  4678. {
  4679. struct beiscsi_hba *phba = from_timer(phba, t, hw_check);
  4680. beiscsi_detect_ue(phba);
  4681. if (beiscsi_detect_ue(phba)) {
  4682. __beiscsi_log(phba, KERN_ERR,
  4683. "BM_%d : port in error: %lx\n", phba->state);
  4684. /* sessions are no longer valid, so first fail the sessions */
  4685. queue_work(phba->wq, &phba->sess_work);
  4686. /* detect UER supported */
  4687. if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
  4688. return;
  4689. /* modify this timer to check TPE */
  4690. phba->hw_check.function = beiscsi_hw_tpe_check;
  4691. }
  4692. mod_timer(&phba->hw_check,
  4693. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  4694. }
  4695. /*
  4696. * beiscsi_enable_port()- Enables the disabled port.
  4697. * Only port resources freed in disable function are reallocated.
  4698. * This is called in HBA error handling path.
  4699. *
  4700. * @phba: Instance of driver private structure
  4701. *
  4702. **/
  4703. static int beiscsi_enable_port(struct beiscsi_hba *phba)
  4704. {
  4705. struct hwi_context_memory *phwi_context;
  4706. struct hwi_controller *phwi_ctrlr;
  4707. struct be_eq_obj *pbe_eq;
  4708. int ret, i;
  4709. if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
  4710. __beiscsi_log(phba, KERN_ERR,
  4711. "BM_%d : %s : port is online %lx\n",
  4712. __func__, phba->state);
  4713. return 0;
  4714. }
  4715. ret = beiscsi_init_sliport(phba);
  4716. if (ret)
  4717. return ret;
  4718. be2iscsi_enable_msix(phba);
  4719. beiscsi_get_params(phba);
  4720. beiscsi_set_host_data(phba);
  4721. /* Re-enable UER. If different TPE occurs then it is recoverable. */
  4722. beiscsi_set_uer_feature(phba);
  4723. phba->shost->max_id = phba->params.cxns_per_ctrl;
  4724. phba->shost->can_queue = phba->params.ios_per_ctrl;
  4725. ret = beiscsi_init_port(phba);
  4726. if (ret < 0) {
  4727. __beiscsi_log(phba, KERN_ERR,
  4728. "BM_%d : init port failed\n");
  4729. goto disable_msix;
  4730. }
  4731. for (i = 0; i < MAX_MCC_CMD; i++) {
  4732. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  4733. phba->ctrl.mcc_tag[i] = i + 1;
  4734. phba->ctrl.mcc_tag_status[i + 1] = 0;
  4735. phba->ctrl.mcc_tag_available++;
  4736. }
  4737. phwi_ctrlr = phba->phwi_ctrlr;
  4738. phwi_context = phwi_ctrlr->phwi_ctxt;
  4739. for (i = 0; i < phba->num_cpus; i++) {
  4740. pbe_eq = &phwi_context->be_eq[i];
  4741. irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
  4742. }
  4743. i = (phba->pcidev->msix_enabled) ? i : 0;
  4744. /* Work item for MCC handling */
  4745. pbe_eq = &phwi_context->be_eq[i];
  4746. INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
  4747. ret = beiscsi_init_irqs(phba);
  4748. if (ret < 0) {
  4749. __beiscsi_log(phba, KERN_ERR,
  4750. "BM_%d : setup IRQs failed %d\n", ret);
  4751. goto cleanup_port;
  4752. }
  4753. hwi_enable_intr(phba);
  4754. /* port operational: clear all error bits */
  4755. set_bit(BEISCSI_HBA_ONLINE, &phba->state);
  4756. __beiscsi_log(phba, KERN_INFO,
  4757. "BM_%d : port online: 0x%lx\n", phba->state);
  4758. /* start hw_check timer and eqd_update work */
  4759. schedule_delayed_work(&phba->eqd_update,
  4760. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  4761. /**
  4762. * Timer function gets modified for TPE detection.
  4763. * Always reinit to do health check first.
  4764. */
  4765. phba->hw_check.function = beiscsi_hw_health_check;
  4766. mod_timer(&phba->hw_check,
  4767. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  4768. return 0;
  4769. cleanup_port:
  4770. for (i = 0; i < phba->num_cpus; i++) {
  4771. pbe_eq = &phwi_context->be_eq[i];
  4772. irq_poll_disable(&pbe_eq->iopoll);
  4773. }
  4774. hwi_cleanup_port(phba);
  4775. disable_msix:
  4776. pci_free_irq_vectors(phba->pcidev);
  4777. return ret;
  4778. }
  4779. /*
  4780. * beiscsi_disable_port()- Disable port and cleanup driver resources.
  4781. * This is called in HBA error handling and driver removal.
  4782. * @phba: Instance Priv structure
  4783. * @unload: indicate driver is unloading
  4784. *
  4785. * Free the OS and HW resources held by the driver
  4786. **/
  4787. static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
  4788. {
  4789. struct hwi_context_memory *phwi_context;
  4790. struct hwi_controller *phwi_ctrlr;
  4791. struct be_eq_obj *pbe_eq;
  4792. unsigned int i;
  4793. if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
  4794. return;
  4795. phwi_ctrlr = phba->phwi_ctrlr;
  4796. phwi_context = phwi_ctrlr->phwi_ctxt;
  4797. hwi_disable_intr(phba);
  4798. beiscsi_free_irqs(phba);
  4799. pci_free_irq_vectors(phba->pcidev);
  4800. for (i = 0; i < phba->num_cpus; i++) {
  4801. pbe_eq = &phwi_context->be_eq[i];
  4802. irq_poll_disable(&pbe_eq->iopoll);
  4803. }
  4804. cancel_delayed_work_sync(&phba->eqd_update);
  4805. cancel_work_sync(&phba->boot_work);
  4806. /* WQ might be running cancel queued mcc_work if we are not exiting */
  4807. if (!unload && beiscsi_hba_in_error(phba)) {
  4808. pbe_eq = &phwi_context->be_eq[i];
  4809. cancel_work_sync(&pbe_eq->mcc_work);
  4810. }
  4811. hwi_cleanup_port(phba);
  4812. beiscsi_cleanup_port(phba);
  4813. }
  4814. static void beiscsi_sess_work(struct work_struct *work)
  4815. {
  4816. struct beiscsi_hba *phba;
  4817. phba = container_of(work, struct beiscsi_hba, sess_work);
  4818. /*
  4819. * This work gets scheduled only in case of HBA error.
  4820. * Old sessions are gone so need to be re-established.
  4821. * iscsi_session_failure needs process context hence this work.
  4822. */
  4823. iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
  4824. }
  4825. static void beiscsi_recover_port(struct work_struct *work)
  4826. {
  4827. struct beiscsi_hba *phba;
  4828. phba = container_of(work, struct beiscsi_hba, recover_port.work);
  4829. beiscsi_disable_port(phba, 0);
  4830. beiscsi_enable_port(phba);
  4831. }
  4832. static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
  4833. pci_channel_state_t state)
  4834. {
  4835. struct beiscsi_hba *phba = NULL;
  4836. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4837. set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
  4838. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4839. "BM_%d : EEH error detected\n");
  4840. /* first stop UE detection when PCI error detected */
  4841. del_timer_sync(&phba->hw_check);
  4842. cancel_delayed_work_sync(&phba->recover_port);
  4843. /* sessions are no longer valid, so first fail the sessions */
  4844. iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
  4845. beiscsi_disable_port(phba, 0);
  4846. if (state == pci_channel_io_perm_failure) {
  4847. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4848. "BM_%d : EEH : State PERM Failure");
  4849. return PCI_ERS_RESULT_DISCONNECT;
  4850. }
  4851. pci_disable_device(pdev);
  4852. /* The error could cause the FW to trigger a flash debug dump.
  4853. * Resetting the card while flash dump is in progress
  4854. * can cause it not to recover; wait for it to finish.
  4855. * Wait only for first function as it is needed only once per
  4856. * adapter.
  4857. **/
  4858. if (pdev->devfn == 0)
  4859. ssleep(30);
  4860. return PCI_ERS_RESULT_NEED_RESET;
  4861. }
  4862. static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
  4863. {
  4864. struct beiscsi_hba *phba = NULL;
  4865. int status = 0;
  4866. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4867. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4868. "BM_%d : EEH Reset\n");
  4869. status = pci_enable_device(pdev);
  4870. if (status)
  4871. return PCI_ERS_RESULT_DISCONNECT;
  4872. pci_set_master(pdev);
  4873. pci_set_power_state(pdev, PCI_D0);
  4874. pci_restore_state(pdev);
  4875. status = beiscsi_check_fw_rdy(phba);
  4876. if (status) {
  4877. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4878. "BM_%d : EEH Reset Completed\n");
  4879. } else {
  4880. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4881. "BM_%d : EEH Reset Completion Failure\n");
  4882. return PCI_ERS_RESULT_DISCONNECT;
  4883. }
  4884. pci_cleanup_aer_uncorrect_error_status(pdev);
  4885. return PCI_ERS_RESULT_RECOVERED;
  4886. }
  4887. static void beiscsi_eeh_resume(struct pci_dev *pdev)
  4888. {
  4889. struct beiscsi_hba *phba;
  4890. int ret;
  4891. phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
  4892. pci_save_state(pdev);
  4893. ret = beiscsi_enable_port(phba);
  4894. if (ret)
  4895. __beiscsi_log(phba, KERN_ERR,
  4896. "BM_%d : AER EEH resume failed\n");
  4897. }
  4898. static int beiscsi_dev_probe(struct pci_dev *pcidev,
  4899. const struct pci_device_id *id)
  4900. {
  4901. struct hwi_context_memory *phwi_context;
  4902. struct hwi_controller *phwi_ctrlr;
  4903. struct beiscsi_hba *phba = NULL;
  4904. struct be_eq_obj *pbe_eq;
  4905. unsigned int s_handle;
  4906. char wq_name[20];
  4907. int ret, i;
  4908. ret = beiscsi_enable_pci(pcidev);
  4909. if (ret < 0) {
  4910. dev_err(&pcidev->dev,
  4911. "beiscsi_dev_probe - Failed to enable pci device\n");
  4912. return ret;
  4913. }
  4914. phba = beiscsi_hba_alloc(pcidev);
  4915. if (!phba) {
  4916. dev_err(&pcidev->dev,
  4917. "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
  4918. ret = -ENOMEM;
  4919. goto disable_pci;
  4920. }
  4921. /* Enable EEH reporting */
  4922. ret = pci_enable_pcie_error_reporting(pcidev);
  4923. if (ret)
  4924. beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
  4925. "BM_%d : PCIe Error Reporting "
  4926. "Enabling Failed\n");
  4927. pci_save_state(pcidev);
  4928. /* Initialize Driver configuration Paramters */
  4929. beiscsi_hba_attrs_init(phba);
  4930. phba->mac_addr_set = false;
  4931. switch (pcidev->device) {
  4932. case BE_DEVICE_ID1:
  4933. case OC_DEVICE_ID1:
  4934. case OC_DEVICE_ID2:
  4935. phba->generation = BE_GEN2;
  4936. phba->iotask_fn = beiscsi_iotask;
  4937. dev_warn(&pcidev->dev,
  4938. "Obsolete/Unsupported BE2 Adapter Family\n");
  4939. break;
  4940. case BE_DEVICE_ID2:
  4941. case OC_DEVICE_ID3:
  4942. phba->generation = BE_GEN3;
  4943. phba->iotask_fn = beiscsi_iotask;
  4944. break;
  4945. case OC_SKH_ID1:
  4946. phba->generation = BE_GEN4;
  4947. phba->iotask_fn = beiscsi_iotask_v2;
  4948. break;
  4949. default:
  4950. phba->generation = 0;
  4951. }
  4952. ret = be_ctrl_init(phba, pcidev);
  4953. if (ret) {
  4954. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4955. "BM_%d : be_ctrl_init failed\n");
  4956. goto free_hba;
  4957. }
  4958. ret = beiscsi_init_sliport(phba);
  4959. if (ret)
  4960. goto free_hba;
  4961. spin_lock_init(&phba->io_sgl_lock);
  4962. spin_lock_init(&phba->mgmt_sgl_lock);
  4963. spin_lock_init(&phba->async_pdu_lock);
  4964. ret = beiscsi_get_fw_config(&phba->ctrl, phba);
  4965. if (ret != 0) {
  4966. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4967. "BM_%d : Error getting fw config\n");
  4968. goto free_port;
  4969. }
  4970. beiscsi_get_port_name(&phba->ctrl, phba);
  4971. beiscsi_get_params(phba);
  4972. beiscsi_set_host_data(phba);
  4973. beiscsi_set_uer_feature(phba);
  4974. be2iscsi_enable_msix(phba);
  4975. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  4976. "BM_%d : num_cpus = %d\n",
  4977. phba->num_cpus);
  4978. phba->shost->max_id = phba->params.cxns_per_ctrl;
  4979. phba->shost->can_queue = phba->params.ios_per_ctrl;
  4980. ret = beiscsi_get_memory(phba);
  4981. if (ret < 0) {
  4982. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4983. "BM_%d : alloc host mem failed\n");
  4984. goto free_port;
  4985. }
  4986. ret = beiscsi_init_port(phba);
  4987. if (ret < 0) {
  4988. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  4989. "BM_%d : init port failed\n");
  4990. beiscsi_free_mem(phba);
  4991. goto free_port;
  4992. }
  4993. for (i = 0; i < MAX_MCC_CMD; i++) {
  4994. init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
  4995. phba->ctrl.mcc_tag[i] = i + 1;
  4996. phba->ctrl.mcc_tag_status[i + 1] = 0;
  4997. phba->ctrl.mcc_tag_available++;
  4998. memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
  4999. sizeof(struct be_dma_mem));
  5000. }
  5001. phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
  5002. snprintf(wq_name, sizeof(wq_name), "beiscsi_%02x_wq",
  5003. phba->shost->host_no);
  5004. phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, wq_name);
  5005. if (!phba->wq) {
  5006. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5007. "BM_%d : beiscsi_dev_probe-"
  5008. "Failed to allocate work queue\n");
  5009. ret = -ENOMEM;
  5010. goto free_twq;
  5011. }
  5012. INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
  5013. phwi_ctrlr = phba->phwi_ctrlr;
  5014. phwi_context = phwi_ctrlr->phwi_ctxt;
  5015. for (i = 0; i < phba->num_cpus; i++) {
  5016. pbe_eq = &phwi_context->be_eq[i];
  5017. irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
  5018. }
  5019. i = (phba->pcidev->msix_enabled) ? i : 0;
  5020. /* Work item for MCC handling */
  5021. pbe_eq = &phwi_context->be_eq[i];
  5022. INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
  5023. ret = beiscsi_init_irqs(phba);
  5024. if (ret < 0) {
  5025. beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
  5026. "BM_%d : beiscsi_dev_probe-"
  5027. "Failed to beiscsi_init_irqs\n");
  5028. goto disable_iopoll;
  5029. }
  5030. hwi_enable_intr(phba);
  5031. ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
  5032. if (ret)
  5033. goto free_irqs;
  5034. /* set online bit after port is operational */
  5035. set_bit(BEISCSI_HBA_ONLINE, &phba->state);
  5036. __beiscsi_log(phba, KERN_INFO,
  5037. "BM_%d : port online: 0x%lx\n", phba->state);
  5038. INIT_WORK(&phba->boot_work, beiscsi_boot_work);
  5039. ret = beiscsi_boot_get_shandle(phba, &s_handle);
  5040. if (ret > 0) {
  5041. beiscsi_start_boot_work(phba, s_handle);
  5042. /**
  5043. * Set this bit after starting the work to let
  5044. * probe handle it first.
  5045. * ASYNC event can too schedule this work.
  5046. */
  5047. set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
  5048. }
  5049. beiscsi_iface_create_default(phba);
  5050. schedule_delayed_work(&phba->eqd_update,
  5051. msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
  5052. INIT_WORK(&phba->sess_work, beiscsi_sess_work);
  5053. INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
  5054. /**
  5055. * Start UE detection here. UE before this will cause stall in probe
  5056. * and eventually fail the probe.
  5057. */
  5058. timer_setup(&phba->hw_check, beiscsi_hw_health_check, 0);
  5059. mod_timer(&phba->hw_check,
  5060. jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
  5061. beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
  5062. "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
  5063. return 0;
  5064. free_irqs:
  5065. hwi_disable_intr(phba);
  5066. beiscsi_free_irqs(phba);
  5067. disable_iopoll:
  5068. for (i = 0; i < phba->num_cpus; i++) {
  5069. pbe_eq = &phwi_context->be_eq[i];
  5070. irq_poll_disable(&pbe_eq->iopoll);
  5071. }
  5072. destroy_workqueue(phba->wq);
  5073. free_twq:
  5074. hwi_cleanup_port(phba);
  5075. beiscsi_cleanup_port(phba);
  5076. beiscsi_free_mem(phba);
  5077. free_port:
  5078. pci_free_consistent(phba->pcidev,
  5079. phba->ctrl.mbox_mem_alloced.size,
  5080. phba->ctrl.mbox_mem_alloced.va,
  5081. phba->ctrl.mbox_mem_alloced.dma);
  5082. beiscsi_unmap_pci_function(phba);
  5083. free_hba:
  5084. pci_disable_msix(phba->pcidev);
  5085. pci_dev_put(phba->pcidev);
  5086. iscsi_host_free(phba->shost);
  5087. pci_set_drvdata(pcidev, NULL);
  5088. disable_pci:
  5089. pci_release_regions(pcidev);
  5090. pci_disable_device(pcidev);
  5091. return ret;
  5092. }
  5093. static void beiscsi_remove(struct pci_dev *pcidev)
  5094. {
  5095. struct beiscsi_hba *phba = NULL;
  5096. phba = pci_get_drvdata(pcidev);
  5097. if (!phba) {
  5098. dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
  5099. return;
  5100. }
  5101. /* first stop UE detection before unloading */
  5102. del_timer_sync(&phba->hw_check);
  5103. cancel_delayed_work_sync(&phba->recover_port);
  5104. cancel_work_sync(&phba->sess_work);
  5105. beiscsi_iface_destroy_default(phba);
  5106. iscsi_host_remove(phba->shost);
  5107. beiscsi_disable_port(phba, 1);
  5108. /* after cancelling boot_work */
  5109. iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
  5110. /* free all resources */
  5111. destroy_workqueue(phba->wq);
  5112. beiscsi_free_mem(phba);
  5113. /* ctrl uninit */
  5114. beiscsi_unmap_pci_function(phba);
  5115. pci_free_consistent(phba->pcidev,
  5116. phba->ctrl.mbox_mem_alloced.size,
  5117. phba->ctrl.mbox_mem_alloced.va,
  5118. phba->ctrl.mbox_mem_alloced.dma);
  5119. pci_dev_put(phba->pcidev);
  5120. iscsi_host_free(phba->shost);
  5121. pci_disable_pcie_error_reporting(pcidev);
  5122. pci_set_drvdata(pcidev, NULL);
  5123. pci_release_regions(pcidev);
  5124. pci_disable_device(pcidev);
  5125. }
  5126. static struct pci_error_handlers beiscsi_eeh_handlers = {
  5127. .error_detected = beiscsi_eeh_err_detected,
  5128. .slot_reset = beiscsi_eeh_reset,
  5129. .resume = beiscsi_eeh_resume,
  5130. };
  5131. struct iscsi_transport beiscsi_iscsi_transport = {
  5132. .owner = THIS_MODULE,
  5133. .name = DRV_NAME,
  5134. .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
  5135. CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
  5136. .create_session = beiscsi_session_create,
  5137. .destroy_session = beiscsi_session_destroy,
  5138. .create_conn = beiscsi_conn_create,
  5139. .bind_conn = beiscsi_conn_bind,
  5140. .destroy_conn = iscsi_conn_teardown,
  5141. .attr_is_visible = beiscsi_attr_is_visible,
  5142. .set_iface_param = beiscsi_iface_set_param,
  5143. .get_iface_param = beiscsi_iface_get_param,
  5144. .set_param = beiscsi_set_param,
  5145. .get_conn_param = iscsi_conn_get_param,
  5146. .get_session_param = iscsi_session_get_param,
  5147. .get_host_param = beiscsi_get_host_param,
  5148. .start_conn = beiscsi_conn_start,
  5149. .stop_conn = iscsi_conn_stop,
  5150. .send_pdu = iscsi_conn_send_pdu,
  5151. .xmit_task = beiscsi_task_xmit,
  5152. .cleanup_task = beiscsi_cleanup_task,
  5153. .alloc_pdu = beiscsi_alloc_pdu,
  5154. .parse_pdu_itt = beiscsi_parse_pdu,
  5155. .get_stats = beiscsi_conn_get_stats,
  5156. .get_ep_param = beiscsi_ep_get_param,
  5157. .ep_connect = beiscsi_ep_connect,
  5158. .ep_poll = beiscsi_ep_poll,
  5159. .ep_disconnect = beiscsi_ep_disconnect,
  5160. .session_recovery_timedout = iscsi_session_recovery_timedout,
  5161. .bsg_request = beiscsi_bsg_request,
  5162. };
  5163. static struct pci_driver beiscsi_pci_driver = {
  5164. .name = DRV_NAME,
  5165. .probe = beiscsi_dev_probe,
  5166. .remove = beiscsi_remove,
  5167. .id_table = beiscsi_pci_id_table,
  5168. .err_handler = &beiscsi_eeh_handlers
  5169. };
  5170. static int __init beiscsi_module_init(void)
  5171. {
  5172. int ret;
  5173. beiscsi_scsi_transport =
  5174. iscsi_register_transport(&beiscsi_iscsi_transport);
  5175. if (!beiscsi_scsi_transport) {
  5176. printk(KERN_ERR
  5177. "beiscsi_module_init - Unable to register beiscsi transport.\n");
  5178. return -ENOMEM;
  5179. }
  5180. printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
  5181. &beiscsi_iscsi_transport);
  5182. ret = pci_register_driver(&beiscsi_pci_driver);
  5183. if (ret) {
  5184. printk(KERN_ERR
  5185. "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
  5186. goto unregister_iscsi_transport;
  5187. }
  5188. return 0;
  5189. unregister_iscsi_transport:
  5190. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  5191. return ret;
  5192. }
  5193. static void __exit beiscsi_module_exit(void)
  5194. {
  5195. pci_unregister_driver(&beiscsi_pci_driver);
  5196. iscsi_unregister_transport(&beiscsi_iscsi_transport);
  5197. }
  5198. module_init(beiscsi_module_init);
  5199. module_exit(beiscsi_module_exit);