be_main.c 167 KB

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