cnic.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760
  1. /* cnic.c: Broadcom CNIC core network driver.
  2. *
  3. * Copyright (c) 2006-2014 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. *
  9. * Original skeleton written by: John(Zongxi) Chen (zongxi@broadcom.com)
  10. * Modified and maintained by: Michael Chan <mchan@broadcom.com>
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/pci.h>
  19. #include <linux/init.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/uio_driver.h>
  22. #include <linux/in.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/delay.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/prefetch.h>
  28. #include <linux/random.h>
  29. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  30. #define BCM_VLAN 1
  31. #endif
  32. #include <net/ip.h>
  33. #include <net/tcp.h>
  34. #include <net/route.h>
  35. #include <net/ipv6.h>
  36. #include <net/ip6_route.h>
  37. #include <net/ip6_checksum.h>
  38. #include <scsi/iscsi_if.h>
  39. #define BCM_CNIC 1
  40. #include "cnic_if.h"
  41. #include "bnx2.h"
  42. #include "bnx2x/bnx2x.h"
  43. #include "bnx2x/bnx2x_reg.h"
  44. #include "bnx2x/bnx2x_fw_defs.h"
  45. #include "bnx2x/bnx2x_hsi.h"
  46. #include "../../../scsi/bnx2i/57xx_iscsi_constants.h"
  47. #include "../../../scsi/bnx2i/57xx_iscsi_hsi.h"
  48. #include "../../../scsi/bnx2fc/bnx2fc_constants.h"
  49. #include "cnic.h"
  50. #include "cnic_defs.h"
  51. #define CNIC_MODULE_NAME "cnic"
  52. static char version[] =
  53. "Broadcom NetXtreme II CNIC Driver " CNIC_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
  54. MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
  55. "Chen (zongxi@broadcom.com");
  56. MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
  57. MODULE_LICENSE("GPL");
  58. MODULE_VERSION(CNIC_MODULE_VERSION);
  59. /* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
  60. static LIST_HEAD(cnic_dev_list);
  61. static LIST_HEAD(cnic_udev_list);
  62. static DEFINE_RWLOCK(cnic_dev_lock);
  63. static DEFINE_MUTEX(cnic_lock);
  64. static struct cnic_ulp_ops __rcu *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
  65. /* helper function, assuming cnic_lock is held */
  66. static inline struct cnic_ulp_ops *cnic_ulp_tbl_prot(int type)
  67. {
  68. return rcu_dereference_protected(cnic_ulp_tbl[type],
  69. lockdep_is_held(&cnic_lock));
  70. }
  71. static int cnic_service_bnx2(void *, void *);
  72. static int cnic_service_bnx2x(void *, void *);
  73. static int cnic_ctl(void *, struct cnic_ctl_info *);
  74. static struct cnic_ops cnic_bnx2_ops = {
  75. .cnic_owner = THIS_MODULE,
  76. .cnic_handler = cnic_service_bnx2,
  77. .cnic_ctl = cnic_ctl,
  78. };
  79. static struct cnic_ops cnic_bnx2x_ops = {
  80. .cnic_owner = THIS_MODULE,
  81. .cnic_handler = cnic_service_bnx2x,
  82. .cnic_ctl = cnic_ctl,
  83. };
  84. static struct workqueue_struct *cnic_wq;
  85. static void cnic_shutdown_rings(struct cnic_dev *);
  86. static void cnic_init_rings(struct cnic_dev *);
  87. static int cnic_cm_set_pg(struct cnic_sock *);
  88. static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
  89. {
  90. struct cnic_uio_dev *udev = uinfo->priv;
  91. struct cnic_dev *dev;
  92. if (!capable(CAP_NET_ADMIN))
  93. return -EPERM;
  94. if (udev->uio_dev != -1)
  95. return -EBUSY;
  96. rtnl_lock();
  97. dev = udev->dev;
  98. if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
  99. rtnl_unlock();
  100. return -ENODEV;
  101. }
  102. udev->uio_dev = iminor(inode);
  103. cnic_shutdown_rings(dev);
  104. cnic_init_rings(dev);
  105. rtnl_unlock();
  106. return 0;
  107. }
  108. static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
  109. {
  110. struct cnic_uio_dev *udev = uinfo->priv;
  111. udev->uio_dev = -1;
  112. return 0;
  113. }
  114. static inline void cnic_hold(struct cnic_dev *dev)
  115. {
  116. atomic_inc(&dev->ref_count);
  117. }
  118. static inline void cnic_put(struct cnic_dev *dev)
  119. {
  120. atomic_dec(&dev->ref_count);
  121. }
  122. static inline void csk_hold(struct cnic_sock *csk)
  123. {
  124. atomic_inc(&csk->ref_count);
  125. }
  126. static inline void csk_put(struct cnic_sock *csk)
  127. {
  128. atomic_dec(&csk->ref_count);
  129. }
  130. static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
  131. {
  132. struct cnic_dev *cdev;
  133. read_lock(&cnic_dev_lock);
  134. list_for_each_entry(cdev, &cnic_dev_list, list) {
  135. if (netdev == cdev->netdev) {
  136. cnic_hold(cdev);
  137. read_unlock(&cnic_dev_lock);
  138. return cdev;
  139. }
  140. }
  141. read_unlock(&cnic_dev_lock);
  142. return NULL;
  143. }
  144. static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
  145. {
  146. atomic_inc(&ulp_ops->ref_count);
  147. }
  148. static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
  149. {
  150. atomic_dec(&ulp_ops->ref_count);
  151. }
  152. static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
  153. {
  154. struct cnic_local *cp = dev->cnic_priv;
  155. struct cnic_eth_dev *ethdev = cp->ethdev;
  156. struct drv_ctl_info info;
  157. struct drv_ctl_io *io = &info.data.io;
  158. info.cmd = DRV_CTL_CTX_WR_CMD;
  159. io->cid_addr = cid_addr;
  160. io->offset = off;
  161. io->data = val;
  162. ethdev->drv_ctl(dev->netdev, &info);
  163. }
  164. static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
  165. {
  166. struct cnic_local *cp = dev->cnic_priv;
  167. struct cnic_eth_dev *ethdev = cp->ethdev;
  168. struct drv_ctl_info info;
  169. struct drv_ctl_io *io = &info.data.io;
  170. info.cmd = DRV_CTL_CTXTBL_WR_CMD;
  171. io->offset = off;
  172. io->dma_addr = addr;
  173. ethdev->drv_ctl(dev->netdev, &info);
  174. }
  175. static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
  176. {
  177. struct cnic_local *cp = dev->cnic_priv;
  178. struct cnic_eth_dev *ethdev = cp->ethdev;
  179. struct drv_ctl_info info;
  180. struct drv_ctl_l2_ring *ring = &info.data.ring;
  181. if (start)
  182. info.cmd = DRV_CTL_START_L2_CMD;
  183. else
  184. info.cmd = DRV_CTL_STOP_L2_CMD;
  185. ring->cid = cid;
  186. ring->client_id = cl_id;
  187. ethdev->drv_ctl(dev->netdev, &info);
  188. }
  189. static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
  190. {
  191. struct cnic_local *cp = dev->cnic_priv;
  192. struct cnic_eth_dev *ethdev = cp->ethdev;
  193. struct drv_ctl_info info;
  194. struct drv_ctl_io *io = &info.data.io;
  195. info.cmd = DRV_CTL_IO_WR_CMD;
  196. io->offset = off;
  197. io->data = val;
  198. ethdev->drv_ctl(dev->netdev, &info);
  199. }
  200. static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
  201. {
  202. struct cnic_local *cp = dev->cnic_priv;
  203. struct cnic_eth_dev *ethdev = cp->ethdev;
  204. struct drv_ctl_info info;
  205. struct drv_ctl_io *io = &info.data.io;
  206. info.cmd = DRV_CTL_IO_RD_CMD;
  207. io->offset = off;
  208. ethdev->drv_ctl(dev->netdev, &info);
  209. return io->data;
  210. }
  211. static void cnic_ulp_ctl(struct cnic_dev *dev, int ulp_type, bool reg)
  212. {
  213. struct cnic_local *cp = dev->cnic_priv;
  214. struct cnic_eth_dev *ethdev = cp->ethdev;
  215. struct drv_ctl_info info;
  216. struct fcoe_capabilities *fcoe_cap =
  217. &info.data.register_data.fcoe_features;
  218. if (reg) {
  219. info.cmd = DRV_CTL_ULP_REGISTER_CMD;
  220. if (ulp_type == CNIC_ULP_FCOE && dev->fcoe_cap)
  221. memcpy(fcoe_cap, dev->fcoe_cap, sizeof(*fcoe_cap));
  222. } else {
  223. info.cmd = DRV_CTL_ULP_UNREGISTER_CMD;
  224. }
  225. info.data.ulp_type = ulp_type;
  226. ethdev->drv_ctl(dev->netdev, &info);
  227. }
  228. static int cnic_in_use(struct cnic_sock *csk)
  229. {
  230. return test_bit(SK_F_INUSE, &csk->flags);
  231. }
  232. static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
  233. {
  234. struct cnic_local *cp = dev->cnic_priv;
  235. struct cnic_eth_dev *ethdev = cp->ethdev;
  236. struct drv_ctl_info info;
  237. info.cmd = cmd;
  238. info.data.credit.credit_count = count;
  239. ethdev->drv_ctl(dev->netdev, &info);
  240. }
  241. static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
  242. {
  243. u32 i;
  244. if (!cp->ctx_tbl)
  245. return -EINVAL;
  246. for (i = 0; i < cp->max_cid_space; i++) {
  247. if (cp->ctx_tbl[i].cid == cid) {
  248. *l5_cid = i;
  249. return 0;
  250. }
  251. }
  252. return -EINVAL;
  253. }
  254. static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
  255. struct cnic_sock *csk)
  256. {
  257. struct iscsi_path path_req;
  258. char *buf = NULL;
  259. u16 len = 0;
  260. u32 msg_type = ISCSI_KEVENT_IF_DOWN;
  261. struct cnic_ulp_ops *ulp_ops;
  262. struct cnic_uio_dev *udev = cp->udev;
  263. int rc = 0, retry = 0;
  264. if (!udev || udev->uio_dev == -1)
  265. return -ENODEV;
  266. if (csk) {
  267. len = sizeof(path_req);
  268. buf = (char *) &path_req;
  269. memset(&path_req, 0, len);
  270. msg_type = ISCSI_KEVENT_PATH_REQ;
  271. path_req.handle = (u64) csk->l5_cid;
  272. if (test_bit(SK_F_IPV6, &csk->flags)) {
  273. memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
  274. sizeof(struct in6_addr));
  275. path_req.ip_addr_len = 16;
  276. } else {
  277. memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
  278. sizeof(struct in_addr));
  279. path_req.ip_addr_len = 4;
  280. }
  281. path_req.vlan_id = csk->vlan_id;
  282. path_req.pmtu = csk->mtu;
  283. }
  284. while (retry < 3) {
  285. rc = 0;
  286. rcu_read_lock();
  287. ulp_ops = rcu_dereference(cp->ulp_ops[CNIC_ULP_ISCSI]);
  288. if (ulp_ops)
  289. rc = ulp_ops->iscsi_nl_send_msg(
  290. cp->ulp_handle[CNIC_ULP_ISCSI],
  291. msg_type, buf, len);
  292. rcu_read_unlock();
  293. if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
  294. break;
  295. msleep(100);
  296. retry++;
  297. }
  298. return rc;
  299. }
  300. static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
  301. static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
  302. char *buf, u16 len)
  303. {
  304. int rc = -EINVAL;
  305. switch (msg_type) {
  306. case ISCSI_UEVENT_PATH_UPDATE: {
  307. struct cnic_local *cp;
  308. u32 l5_cid;
  309. struct cnic_sock *csk;
  310. struct iscsi_path *path_resp;
  311. if (len < sizeof(*path_resp))
  312. break;
  313. path_resp = (struct iscsi_path *) buf;
  314. cp = dev->cnic_priv;
  315. l5_cid = (u32) path_resp->handle;
  316. if (l5_cid >= MAX_CM_SK_TBL_SZ)
  317. break;
  318. rcu_read_lock();
  319. if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
  320. rc = -ENODEV;
  321. rcu_read_unlock();
  322. break;
  323. }
  324. csk = &cp->csk_tbl[l5_cid];
  325. csk_hold(csk);
  326. if (cnic_in_use(csk) &&
  327. test_bit(SK_F_CONNECT_START, &csk->flags)) {
  328. csk->vlan_id = path_resp->vlan_id;
  329. memcpy(csk->ha, path_resp->mac_addr, ETH_ALEN);
  330. if (test_bit(SK_F_IPV6, &csk->flags))
  331. memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
  332. sizeof(struct in6_addr));
  333. else
  334. memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
  335. sizeof(struct in_addr));
  336. if (is_valid_ether_addr(csk->ha)) {
  337. cnic_cm_set_pg(csk);
  338. } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
  339. !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
  340. cnic_cm_upcall(cp, csk,
  341. L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
  342. clear_bit(SK_F_CONNECT_START, &csk->flags);
  343. }
  344. }
  345. csk_put(csk);
  346. rcu_read_unlock();
  347. rc = 0;
  348. }
  349. }
  350. return rc;
  351. }
  352. static int cnic_offld_prep(struct cnic_sock *csk)
  353. {
  354. if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
  355. return 0;
  356. if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
  357. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  358. return 0;
  359. }
  360. return 1;
  361. }
  362. static int cnic_close_prep(struct cnic_sock *csk)
  363. {
  364. clear_bit(SK_F_CONNECT_START, &csk->flags);
  365. smp_mb__after_atomic();
  366. if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
  367. while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
  368. msleep(1);
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. static int cnic_abort_prep(struct cnic_sock *csk)
  374. {
  375. clear_bit(SK_F_CONNECT_START, &csk->flags);
  376. smp_mb__after_atomic();
  377. while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
  378. msleep(1);
  379. if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
  380. csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
  381. return 1;
  382. }
  383. return 0;
  384. }
  385. int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
  386. {
  387. struct cnic_dev *dev;
  388. if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
  389. pr_err("%s: Bad type %d\n", __func__, ulp_type);
  390. return -EINVAL;
  391. }
  392. mutex_lock(&cnic_lock);
  393. if (cnic_ulp_tbl_prot(ulp_type)) {
  394. pr_err("%s: Type %d has already been registered\n",
  395. __func__, ulp_type);
  396. mutex_unlock(&cnic_lock);
  397. return -EBUSY;
  398. }
  399. read_lock(&cnic_dev_lock);
  400. list_for_each_entry(dev, &cnic_dev_list, list) {
  401. struct cnic_local *cp = dev->cnic_priv;
  402. clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
  403. }
  404. read_unlock(&cnic_dev_lock);
  405. atomic_set(&ulp_ops->ref_count, 0);
  406. rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
  407. mutex_unlock(&cnic_lock);
  408. /* Prevent race conditions with netdev_event */
  409. rtnl_lock();
  410. list_for_each_entry(dev, &cnic_dev_list, list) {
  411. struct cnic_local *cp = dev->cnic_priv;
  412. if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
  413. ulp_ops->cnic_init(dev);
  414. }
  415. rtnl_unlock();
  416. return 0;
  417. }
  418. int cnic_unregister_driver(int ulp_type)
  419. {
  420. struct cnic_dev *dev;
  421. struct cnic_ulp_ops *ulp_ops;
  422. int i = 0;
  423. if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
  424. pr_err("%s: Bad type %d\n", __func__, ulp_type);
  425. return -EINVAL;
  426. }
  427. mutex_lock(&cnic_lock);
  428. ulp_ops = cnic_ulp_tbl_prot(ulp_type);
  429. if (!ulp_ops) {
  430. pr_err("%s: Type %d has not been registered\n",
  431. __func__, ulp_type);
  432. goto out_unlock;
  433. }
  434. read_lock(&cnic_dev_lock);
  435. list_for_each_entry(dev, &cnic_dev_list, list) {
  436. struct cnic_local *cp = dev->cnic_priv;
  437. if (rcu_dereference(cp->ulp_ops[ulp_type])) {
  438. pr_err("%s: Type %d still has devices registered\n",
  439. __func__, ulp_type);
  440. read_unlock(&cnic_dev_lock);
  441. goto out_unlock;
  442. }
  443. }
  444. read_unlock(&cnic_dev_lock);
  445. RCU_INIT_POINTER(cnic_ulp_tbl[ulp_type], NULL);
  446. mutex_unlock(&cnic_lock);
  447. synchronize_rcu();
  448. while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
  449. msleep(100);
  450. i++;
  451. }
  452. if (atomic_read(&ulp_ops->ref_count) != 0)
  453. pr_warn("%s: Failed waiting for ref count to go to zero\n",
  454. __func__);
  455. return 0;
  456. out_unlock:
  457. mutex_unlock(&cnic_lock);
  458. return -EINVAL;
  459. }
  460. static int cnic_start_hw(struct cnic_dev *);
  461. static void cnic_stop_hw(struct cnic_dev *);
  462. static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
  463. void *ulp_ctx)
  464. {
  465. struct cnic_local *cp = dev->cnic_priv;
  466. struct cnic_ulp_ops *ulp_ops;
  467. if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
  468. pr_err("%s: Bad type %d\n", __func__, ulp_type);
  469. return -EINVAL;
  470. }
  471. mutex_lock(&cnic_lock);
  472. if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
  473. pr_err("%s: Driver with type %d has not been registered\n",
  474. __func__, ulp_type);
  475. mutex_unlock(&cnic_lock);
  476. return -EAGAIN;
  477. }
  478. if (rcu_dereference(cp->ulp_ops[ulp_type])) {
  479. pr_err("%s: Type %d has already been registered to this device\n",
  480. __func__, ulp_type);
  481. mutex_unlock(&cnic_lock);
  482. return -EBUSY;
  483. }
  484. clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
  485. cp->ulp_handle[ulp_type] = ulp_ctx;
  486. ulp_ops = cnic_ulp_tbl_prot(ulp_type);
  487. rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
  488. cnic_hold(dev);
  489. if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
  490. if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
  491. ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
  492. mutex_unlock(&cnic_lock);
  493. cnic_ulp_ctl(dev, ulp_type, true);
  494. return 0;
  495. }
  496. EXPORT_SYMBOL(cnic_register_driver);
  497. static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
  498. {
  499. struct cnic_local *cp = dev->cnic_priv;
  500. int i = 0;
  501. if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
  502. pr_err("%s: Bad type %d\n", __func__, ulp_type);
  503. return -EINVAL;
  504. }
  505. if (ulp_type == CNIC_ULP_ISCSI)
  506. cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
  507. mutex_lock(&cnic_lock);
  508. if (rcu_dereference(cp->ulp_ops[ulp_type])) {
  509. RCU_INIT_POINTER(cp->ulp_ops[ulp_type], NULL);
  510. cnic_put(dev);
  511. } else {
  512. pr_err("%s: device not registered to this ulp type %d\n",
  513. __func__, ulp_type);
  514. mutex_unlock(&cnic_lock);
  515. return -EINVAL;
  516. }
  517. mutex_unlock(&cnic_lock);
  518. if (ulp_type == CNIC_ULP_FCOE)
  519. dev->fcoe_cap = NULL;
  520. synchronize_rcu();
  521. while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
  522. i < 20) {
  523. msleep(100);
  524. i++;
  525. }
  526. if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
  527. netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
  528. cnic_ulp_ctl(dev, ulp_type, false);
  529. return 0;
  530. }
  531. EXPORT_SYMBOL(cnic_unregister_driver);
  532. static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id,
  533. u32 next)
  534. {
  535. id_tbl->start = start_id;
  536. id_tbl->max = size;
  537. id_tbl->next = next;
  538. spin_lock_init(&id_tbl->lock);
  539. id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
  540. if (!id_tbl->table)
  541. return -ENOMEM;
  542. return 0;
  543. }
  544. static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
  545. {
  546. kfree(id_tbl->table);
  547. id_tbl->table = NULL;
  548. }
  549. static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
  550. {
  551. int ret = -1;
  552. id -= id_tbl->start;
  553. if (id >= id_tbl->max)
  554. return ret;
  555. spin_lock(&id_tbl->lock);
  556. if (!test_bit(id, id_tbl->table)) {
  557. set_bit(id, id_tbl->table);
  558. ret = 0;
  559. }
  560. spin_unlock(&id_tbl->lock);
  561. return ret;
  562. }
  563. /* Returns -1 if not successful */
  564. static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
  565. {
  566. u32 id;
  567. spin_lock(&id_tbl->lock);
  568. id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
  569. if (id >= id_tbl->max) {
  570. id = -1;
  571. if (id_tbl->next != 0) {
  572. id = find_first_zero_bit(id_tbl->table, id_tbl->next);
  573. if (id >= id_tbl->next)
  574. id = -1;
  575. }
  576. }
  577. if (id < id_tbl->max) {
  578. set_bit(id, id_tbl->table);
  579. id_tbl->next = (id + 1) & (id_tbl->max - 1);
  580. id += id_tbl->start;
  581. }
  582. spin_unlock(&id_tbl->lock);
  583. return id;
  584. }
  585. static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
  586. {
  587. if (id == -1)
  588. return;
  589. id -= id_tbl->start;
  590. if (id >= id_tbl->max)
  591. return;
  592. clear_bit(id, id_tbl->table);
  593. }
  594. static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
  595. {
  596. int i;
  597. if (!dma->pg_arr)
  598. return;
  599. for (i = 0; i < dma->num_pages; i++) {
  600. if (dma->pg_arr[i]) {
  601. dma_free_coherent(&dev->pcidev->dev, CNIC_PAGE_SIZE,
  602. dma->pg_arr[i], dma->pg_map_arr[i]);
  603. dma->pg_arr[i] = NULL;
  604. }
  605. }
  606. if (dma->pgtbl) {
  607. dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
  608. dma->pgtbl, dma->pgtbl_map);
  609. dma->pgtbl = NULL;
  610. }
  611. kfree(dma->pg_arr);
  612. dma->pg_arr = NULL;
  613. dma->num_pages = 0;
  614. }
  615. static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
  616. {
  617. int i;
  618. __le32 *page_table = (__le32 *) dma->pgtbl;
  619. for (i = 0; i < dma->num_pages; i++) {
  620. /* Each entry needs to be in big endian format. */
  621. *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
  622. page_table++;
  623. *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
  624. page_table++;
  625. }
  626. }
  627. static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
  628. {
  629. int i;
  630. __le32 *page_table = (__le32 *) dma->pgtbl;
  631. for (i = 0; i < dma->num_pages; i++) {
  632. /* Each entry needs to be in little endian format. */
  633. *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
  634. page_table++;
  635. *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
  636. page_table++;
  637. }
  638. }
  639. static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
  640. int pages, int use_pg_tbl)
  641. {
  642. int i, size;
  643. struct cnic_local *cp = dev->cnic_priv;
  644. size = pages * (sizeof(void *) + sizeof(dma_addr_t));
  645. dma->pg_arr = kzalloc(size, GFP_ATOMIC);
  646. if (dma->pg_arr == NULL)
  647. return -ENOMEM;
  648. dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
  649. dma->num_pages = pages;
  650. for (i = 0; i < pages; i++) {
  651. dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
  652. CNIC_PAGE_SIZE,
  653. &dma->pg_map_arr[i],
  654. GFP_ATOMIC);
  655. if (dma->pg_arr[i] == NULL)
  656. goto error;
  657. }
  658. if (!use_pg_tbl)
  659. return 0;
  660. dma->pgtbl_size = ((pages * 8) + CNIC_PAGE_SIZE - 1) &
  661. ~(CNIC_PAGE_SIZE - 1);
  662. dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
  663. &dma->pgtbl_map, GFP_ATOMIC);
  664. if (dma->pgtbl == NULL)
  665. goto error;
  666. cp->setup_pgtbl(dev, dma);
  667. return 0;
  668. error:
  669. cnic_free_dma(dev, dma);
  670. return -ENOMEM;
  671. }
  672. static void cnic_free_context(struct cnic_dev *dev)
  673. {
  674. struct cnic_local *cp = dev->cnic_priv;
  675. int i;
  676. for (i = 0; i < cp->ctx_blks; i++) {
  677. if (cp->ctx_arr[i].ctx) {
  678. dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
  679. cp->ctx_arr[i].ctx,
  680. cp->ctx_arr[i].mapping);
  681. cp->ctx_arr[i].ctx = NULL;
  682. }
  683. }
  684. }
  685. static void __cnic_free_uio_rings(struct cnic_uio_dev *udev)
  686. {
  687. if (udev->l2_buf) {
  688. dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
  689. udev->l2_buf, udev->l2_buf_map);
  690. udev->l2_buf = NULL;
  691. }
  692. if (udev->l2_ring) {
  693. dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
  694. udev->l2_ring, udev->l2_ring_map);
  695. udev->l2_ring = NULL;
  696. }
  697. }
  698. static void __cnic_free_uio(struct cnic_uio_dev *udev)
  699. {
  700. uio_unregister_device(&udev->cnic_uinfo);
  701. __cnic_free_uio_rings(udev);
  702. pci_dev_put(udev->pdev);
  703. kfree(udev);
  704. }
  705. static void cnic_free_uio(struct cnic_uio_dev *udev)
  706. {
  707. if (!udev)
  708. return;
  709. write_lock(&cnic_dev_lock);
  710. list_del_init(&udev->list);
  711. write_unlock(&cnic_dev_lock);
  712. __cnic_free_uio(udev);
  713. }
  714. static void cnic_free_resc(struct cnic_dev *dev)
  715. {
  716. struct cnic_local *cp = dev->cnic_priv;
  717. struct cnic_uio_dev *udev = cp->udev;
  718. if (udev) {
  719. udev->dev = NULL;
  720. cp->udev = NULL;
  721. if (udev->uio_dev == -1)
  722. __cnic_free_uio_rings(udev);
  723. }
  724. cnic_free_context(dev);
  725. kfree(cp->ctx_arr);
  726. cp->ctx_arr = NULL;
  727. cp->ctx_blks = 0;
  728. cnic_free_dma(dev, &cp->gbl_buf_info);
  729. cnic_free_dma(dev, &cp->kwq_info);
  730. cnic_free_dma(dev, &cp->kwq_16_data_info);
  731. cnic_free_dma(dev, &cp->kcq2.dma);
  732. cnic_free_dma(dev, &cp->kcq1.dma);
  733. kfree(cp->iscsi_tbl);
  734. cp->iscsi_tbl = NULL;
  735. kfree(cp->ctx_tbl);
  736. cp->ctx_tbl = NULL;
  737. cnic_free_id_tbl(&cp->fcoe_cid_tbl);
  738. cnic_free_id_tbl(&cp->cid_tbl);
  739. }
  740. static int cnic_alloc_context(struct cnic_dev *dev)
  741. {
  742. struct cnic_local *cp = dev->cnic_priv;
  743. if (BNX2_CHIP(cp) == BNX2_CHIP_5709) {
  744. int i, k, arr_size;
  745. cp->ctx_blk_size = CNIC_PAGE_SIZE;
  746. cp->cids_per_blk = CNIC_PAGE_SIZE / 128;
  747. arr_size = BNX2_MAX_CID / cp->cids_per_blk *
  748. sizeof(struct cnic_ctx);
  749. cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
  750. if (cp->ctx_arr == NULL)
  751. return -ENOMEM;
  752. k = 0;
  753. for (i = 0; i < 2; i++) {
  754. u32 j, reg, off, lo, hi;
  755. if (i == 0)
  756. off = BNX2_PG_CTX_MAP;
  757. else
  758. off = BNX2_ISCSI_CTX_MAP;
  759. reg = cnic_reg_rd_ind(dev, off);
  760. lo = reg >> 16;
  761. hi = reg & 0xffff;
  762. for (j = lo; j < hi; j += cp->cids_per_blk, k++)
  763. cp->ctx_arr[k].cid = j;
  764. }
  765. cp->ctx_blks = k;
  766. if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
  767. cp->ctx_blks = 0;
  768. return -ENOMEM;
  769. }
  770. for (i = 0; i < cp->ctx_blks; i++) {
  771. cp->ctx_arr[i].ctx =
  772. dma_alloc_coherent(&dev->pcidev->dev,
  773. CNIC_PAGE_SIZE,
  774. &cp->ctx_arr[i].mapping,
  775. GFP_KERNEL);
  776. if (cp->ctx_arr[i].ctx == NULL)
  777. return -ENOMEM;
  778. }
  779. }
  780. return 0;
  781. }
  782. static u16 cnic_bnx2_next_idx(u16 idx)
  783. {
  784. return idx + 1;
  785. }
  786. static u16 cnic_bnx2_hw_idx(u16 idx)
  787. {
  788. return idx;
  789. }
  790. static u16 cnic_bnx2x_next_idx(u16 idx)
  791. {
  792. idx++;
  793. if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
  794. idx++;
  795. return idx;
  796. }
  797. static u16 cnic_bnx2x_hw_idx(u16 idx)
  798. {
  799. if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
  800. idx++;
  801. return idx;
  802. }
  803. static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info,
  804. bool use_pg_tbl)
  805. {
  806. int err, i, use_page_tbl = 0;
  807. struct kcqe **kcq;
  808. if (use_pg_tbl)
  809. use_page_tbl = 1;
  810. err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, use_page_tbl);
  811. if (err)
  812. return err;
  813. kcq = (struct kcqe **) info->dma.pg_arr;
  814. info->kcq = kcq;
  815. info->next_idx = cnic_bnx2_next_idx;
  816. info->hw_idx = cnic_bnx2_hw_idx;
  817. if (use_pg_tbl)
  818. return 0;
  819. info->next_idx = cnic_bnx2x_next_idx;
  820. info->hw_idx = cnic_bnx2x_hw_idx;
  821. for (i = 0; i < KCQ_PAGE_CNT; i++) {
  822. struct bnx2x_bd_chain_next *next =
  823. (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
  824. int j = i + 1;
  825. if (j >= KCQ_PAGE_CNT)
  826. j = 0;
  827. next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
  828. next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
  829. }
  830. return 0;
  831. }
  832. static int __cnic_alloc_uio_rings(struct cnic_uio_dev *udev, int pages)
  833. {
  834. struct cnic_local *cp = udev->dev->cnic_priv;
  835. if (udev->l2_ring)
  836. return 0;
  837. udev->l2_ring_size = pages * CNIC_PAGE_SIZE;
  838. udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
  839. &udev->l2_ring_map,
  840. GFP_KERNEL | __GFP_COMP);
  841. if (!udev->l2_ring)
  842. return -ENOMEM;
  843. udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
  844. udev->l2_buf_size = CNIC_PAGE_ALIGN(udev->l2_buf_size);
  845. udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
  846. &udev->l2_buf_map,
  847. GFP_KERNEL | __GFP_COMP);
  848. if (!udev->l2_buf) {
  849. __cnic_free_uio_rings(udev);
  850. return -ENOMEM;
  851. }
  852. return 0;
  853. }
  854. static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
  855. {
  856. struct cnic_local *cp = dev->cnic_priv;
  857. struct cnic_uio_dev *udev;
  858. list_for_each_entry(udev, &cnic_udev_list, list) {
  859. if (udev->pdev == dev->pcidev) {
  860. udev->dev = dev;
  861. if (__cnic_alloc_uio_rings(udev, pages)) {
  862. udev->dev = NULL;
  863. return -ENOMEM;
  864. }
  865. cp->udev = udev;
  866. return 0;
  867. }
  868. }
  869. udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
  870. if (!udev)
  871. return -ENOMEM;
  872. udev->uio_dev = -1;
  873. udev->dev = dev;
  874. udev->pdev = dev->pcidev;
  875. if (__cnic_alloc_uio_rings(udev, pages))
  876. goto err_udev;
  877. list_add(&udev->list, &cnic_udev_list);
  878. pci_dev_get(udev->pdev);
  879. cp->udev = udev;
  880. return 0;
  881. err_udev:
  882. kfree(udev);
  883. return -ENOMEM;
  884. }
  885. static int cnic_init_uio(struct cnic_dev *dev)
  886. {
  887. struct cnic_local *cp = dev->cnic_priv;
  888. struct cnic_uio_dev *udev = cp->udev;
  889. struct uio_info *uinfo;
  890. int ret = 0;
  891. if (!udev)
  892. return -ENOMEM;
  893. uinfo = &udev->cnic_uinfo;
  894. uinfo->mem[0].addr = pci_resource_start(dev->pcidev, 0);
  895. uinfo->mem[0].internal_addr = dev->regview;
  896. uinfo->mem[0].memtype = UIO_MEM_PHYS;
  897. if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
  898. uinfo->mem[0].size = MB_GET_CID_ADDR(TX_TSS_CID +
  899. TX_MAX_TSS_RINGS + 1);
  900. uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
  901. CNIC_PAGE_MASK;
  902. if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
  903. uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
  904. else
  905. uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
  906. uinfo->name = "bnx2_cnic";
  907. } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  908. uinfo->mem[0].size = pci_resource_len(dev->pcidev, 0);
  909. uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
  910. CNIC_PAGE_MASK;
  911. uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
  912. uinfo->name = "bnx2x_cnic";
  913. }
  914. uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
  915. uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
  916. uinfo->mem[2].size = udev->l2_ring_size;
  917. uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
  918. uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
  919. uinfo->mem[3].size = udev->l2_buf_size;
  920. uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
  921. uinfo->version = CNIC_MODULE_VERSION;
  922. uinfo->irq = UIO_IRQ_CUSTOM;
  923. uinfo->open = cnic_uio_open;
  924. uinfo->release = cnic_uio_close;
  925. if (udev->uio_dev == -1) {
  926. if (!uinfo->priv) {
  927. uinfo->priv = udev;
  928. ret = uio_register_device(&udev->pdev->dev, uinfo);
  929. }
  930. } else {
  931. cnic_init_rings(dev);
  932. }
  933. return ret;
  934. }
  935. static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
  936. {
  937. struct cnic_local *cp = dev->cnic_priv;
  938. int ret;
  939. ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
  940. if (ret)
  941. goto error;
  942. cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
  943. ret = cnic_alloc_kcq(dev, &cp->kcq1, true);
  944. if (ret)
  945. goto error;
  946. ret = cnic_alloc_context(dev);
  947. if (ret)
  948. goto error;
  949. ret = cnic_alloc_uio_rings(dev, 2);
  950. if (ret)
  951. goto error;
  952. ret = cnic_init_uio(dev);
  953. if (ret)
  954. goto error;
  955. return 0;
  956. error:
  957. cnic_free_resc(dev);
  958. return ret;
  959. }
  960. static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
  961. {
  962. struct cnic_local *cp = dev->cnic_priv;
  963. struct bnx2x *bp = netdev_priv(dev->netdev);
  964. int ctx_blk_size = cp->ethdev->ctx_blk_size;
  965. int total_mem, blks, i;
  966. total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
  967. blks = total_mem / ctx_blk_size;
  968. if (total_mem % ctx_blk_size)
  969. blks++;
  970. if (blks > cp->ethdev->ctx_tbl_len)
  971. return -ENOMEM;
  972. cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
  973. if (cp->ctx_arr == NULL)
  974. return -ENOMEM;
  975. cp->ctx_blks = blks;
  976. cp->ctx_blk_size = ctx_blk_size;
  977. if (!CHIP_IS_E1(bp))
  978. cp->ctx_align = 0;
  979. else
  980. cp->ctx_align = ctx_blk_size;
  981. cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
  982. for (i = 0; i < blks; i++) {
  983. cp->ctx_arr[i].ctx =
  984. dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
  985. &cp->ctx_arr[i].mapping,
  986. GFP_KERNEL);
  987. if (cp->ctx_arr[i].ctx == NULL)
  988. return -ENOMEM;
  989. if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
  990. if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
  991. cnic_free_context(dev);
  992. cp->ctx_blk_size += cp->ctx_align;
  993. i = -1;
  994. continue;
  995. }
  996. }
  997. }
  998. return 0;
  999. }
  1000. static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
  1001. {
  1002. struct cnic_local *cp = dev->cnic_priv;
  1003. struct bnx2x *bp = netdev_priv(dev->netdev);
  1004. struct cnic_eth_dev *ethdev = cp->ethdev;
  1005. u32 start_cid = ethdev->starting_cid;
  1006. int i, j, n, ret, pages;
  1007. struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
  1008. cp->max_cid_space = MAX_ISCSI_TBL_SZ;
  1009. cp->iscsi_start_cid = start_cid;
  1010. cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
  1011. if (BNX2X_CHIP_IS_E2_PLUS(bp)) {
  1012. cp->max_cid_space += dev->max_fcoe_conn;
  1013. cp->fcoe_init_cid = ethdev->fcoe_init_cid;
  1014. if (!cp->fcoe_init_cid)
  1015. cp->fcoe_init_cid = 0x10;
  1016. }
  1017. cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
  1018. GFP_KERNEL);
  1019. if (!cp->iscsi_tbl)
  1020. goto error;
  1021. cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
  1022. cp->max_cid_space, GFP_KERNEL);
  1023. if (!cp->ctx_tbl)
  1024. goto error;
  1025. for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
  1026. cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
  1027. cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
  1028. }
  1029. for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
  1030. cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
  1031. pages = CNIC_PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
  1032. CNIC_PAGE_SIZE;
  1033. ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
  1034. if (ret)
  1035. return -ENOMEM;
  1036. n = CNIC_PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
  1037. for (i = 0, j = 0; i < cp->max_cid_space; i++) {
  1038. long off = CNIC_KWQ16_DATA_SIZE * (i % n);
  1039. cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
  1040. cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
  1041. off;
  1042. if ((i % n) == (n - 1))
  1043. j++;
  1044. }
  1045. ret = cnic_alloc_kcq(dev, &cp->kcq1, false);
  1046. if (ret)
  1047. goto error;
  1048. if (CNIC_SUPPORTS_FCOE(bp)) {
  1049. ret = cnic_alloc_kcq(dev, &cp->kcq2, true);
  1050. if (ret)
  1051. goto error;
  1052. }
  1053. pages = CNIC_PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / CNIC_PAGE_SIZE;
  1054. ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
  1055. if (ret)
  1056. goto error;
  1057. ret = cnic_alloc_bnx2x_context(dev);
  1058. if (ret)
  1059. goto error;
  1060. if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
  1061. return 0;
  1062. cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
  1063. cp->l2_rx_ring_size = 15;
  1064. ret = cnic_alloc_uio_rings(dev, 4);
  1065. if (ret)
  1066. goto error;
  1067. ret = cnic_init_uio(dev);
  1068. if (ret)
  1069. goto error;
  1070. return 0;
  1071. error:
  1072. cnic_free_resc(dev);
  1073. return -ENOMEM;
  1074. }
  1075. static inline u32 cnic_kwq_avail(struct cnic_local *cp)
  1076. {
  1077. return cp->max_kwq_idx -
  1078. ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
  1079. }
  1080. static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
  1081. u32 num_wqes)
  1082. {
  1083. struct cnic_local *cp = dev->cnic_priv;
  1084. struct kwqe *prod_qe;
  1085. u16 prod, sw_prod, i;
  1086. if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
  1087. return -EAGAIN; /* bnx2 is down */
  1088. spin_lock_bh(&cp->cnic_ulp_lock);
  1089. if (num_wqes > cnic_kwq_avail(cp) &&
  1090. !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
  1091. spin_unlock_bh(&cp->cnic_ulp_lock);
  1092. return -EAGAIN;
  1093. }
  1094. clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
  1095. prod = cp->kwq_prod_idx;
  1096. sw_prod = prod & MAX_KWQ_IDX;
  1097. for (i = 0; i < num_wqes; i++) {
  1098. prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
  1099. memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
  1100. prod++;
  1101. sw_prod = prod & MAX_KWQ_IDX;
  1102. }
  1103. cp->kwq_prod_idx = prod;
  1104. CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
  1105. spin_unlock_bh(&cp->cnic_ulp_lock);
  1106. return 0;
  1107. }
  1108. static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
  1109. union l5cm_specific_data *l5_data)
  1110. {
  1111. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  1112. dma_addr_t map;
  1113. map = ctx->kwqe_data_mapping;
  1114. l5_data->phy_address.lo = (u64) map & 0xffffffff;
  1115. l5_data->phy_address.hi = (u64) map >> 32;
  1116. return ctx->kwqe_data;
  1117. }
  1118. static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
  1119. u32 type, union l5cm_specific_data *l5_data)
  1120. {
  1121. struct cnic_local *cp = dev->cnic_priv;
  1122. struct bnx2x *bp = netdev_priv(dev->netdev);
  1123. struct l5cm_spe kwqe;
  1124. struct kwqe_16 *kwq[1];
  1125. u16 type_16;
  1126. int ret;
  1127. kwqe.hdr.conn_and_cmd_data =
  1128. cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
  1129. BNX2X_HW_CID(bp, cid)));
  1130. type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
  1131. type_16 |= (bp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
  1132. SPE_HDR_FUNCTION_ID;
  1133. kwqe.hdr.type = cpu_to_le16(type_16);
  1134. kwqe.hdr.reserved1 = 0;
  1135. kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
  1136. kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
  1137. kwq[0] = (struct kwqe_16 *) &kwqe;
  1138. spin_lock_bh(&cp->cnic_ulp_lock);
  1139. ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
  1140. spin_unlock_bh(&cp->cnic_ulp_lock);
  1141. if (ret == 1)
  1142. return 0;
  1143. return ret;
  1144. }
  1145. static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
  1146. struct kcqe *cqes[], u32 num_cqes)
  1147. {
  1148. struct cnic_local *cp = dev->cnic_priv;
  1149. struct cnic_ulp_ops *ulp_ops;
  1150. rcu_read_lock();
  1151. ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
  1152. if (likely(ulp_ops)) {
  1153. ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
  1154. cqes, num_cqes);
  1155. }
  1156. rcu_read_unlock();
  1157. }
  1158. static void cnic_bnx2x_set_tcp_options(struct cnic_dev *dev, int time_stamps,
  1159. int en_tcp_dack)
  1160. {
  1161. struct bnx2x *bp = netdev_priv(dev->netdev);
  1162. u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
  1163. u16 tstorm_flags = 0;
  1164. if (time_stamps) {
  1165. xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
  1166. tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
  1167. }
  1168. if (en_tcp_dack)
  1169. tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_DELAYED_ACK_EN;
  1170. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1171. XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(bp->pfid), xstorm_flags);
  1172. CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
  1173. TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(bp->pfid), tstorm_flags);
  1174. }
  1175. static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
  1176. {
  1177. struct cnic_local *cp = dev->cnic_priv;
  1178. struct bnx2x *bp = netdev_priv(dev->netdev);
  1179. struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
  1180. int hq_bds, pages;
  1181. u32 pfid = bp->pfid;
  1182. cp->num_iscsi_tasks = req1->num_tasks_per_conn;
  1183. cp->num_ccells = req1->num_ccells_per_conn;
  1184. cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
  1185. cp->num_iscsi_tasks;
  1186. cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
  1187. BNX2X_ISCSI_R2TQE_SIZE;
  1188. cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
  1189. pages = CNIC_PAGE_ALIGN(cp->hq_size) / CNIC_PAGE_SIZE;
  1190. hq_bds = pages * (CNIC_PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
  1191. cp->num_cqs = req1->num_cqs;
  1192. if (!dev->max_iscsi_conn)
  1193. return 0;
  1194. /* init Tstorm RAM */
  1195. CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
  1196. req1->rq_num_wqes);
  1197. CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
  1198. CNIC_PAGE_SIZE);
  1199. CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
  1200. TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), CNIC_PAGE_BITS);
  1201. CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
  1202. TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
  1203. req1->num_tasks_per_conn);
  1204. /* init Ustorm RAM */
  1205. CNIC_WR16(dev, BAR_USTRORM_INTMEM +
  1206. USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
  1207. req1->rq_buffer_size);
  1208. CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
  1209. CNIC_PAGE_SIZE);
  1210. CNIC_WR8(dev, BAR_USTRORM_INTMEM +
  1211. USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), CNIC_PAGE_BITS);
  1212. CNIC_WR16(dev, BAR_USTRORM_INTMEM +
  1213. USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
  1214. req1->num_tasks_per_conn);
  1215. CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
  1216. req1->rq_num_wqes);
  1217. CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
  1218. req1->cq_num_wqes);
  1219. CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
  1220. cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
  1221. /* init Xstorm RAM */
  1222. CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
  1223. CNIC_PAGE_SIZE);
  1224. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1225. XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), CNIC_PAGE_BITS);
  1226. CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
  1227. XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
  1228. req1->num_tasks_per_conn);
  1229. CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
  1230. hq_bds);
  1231. CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
  1232. req1->num_tasks_per_conn);
  1233. CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
  1234. cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
  1235. /* init Cstorm RAM */
  1236. CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
  1237. CNIC_PAGE_SIZE);
  1238. CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
  1239. CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), CNIC_PAGE_BITS);
  1240. CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
  1241. CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
  1242. req1->num_tasks_per_conn);
  1243. CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
  1244. req1->cq_num_wqes);
  1245. CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
  1246. hq_bds);
  1247. cnic_bnx2x_set_tcp_options(dev,
  1248. req1->flags & ISCSI_KWQE_INIT1_TIME_STAMPS_ENABLE,
  1249. req1->flags & ISCSI_KWQE_INIT1_DELAYED_ACK_ENABLE);
  1250. return 0;
  1251. }
  1252. static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
  1253. {
  1254. struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
  1255. struct bnx2x *bp = netdev_priv(dev->netdev);
  1256. u32 pfid = bp->pfid;
  1257. struct iscsi_kcqe kcqe;
  1258. struct kcqe *cqes[1];
  1259. memset(&kcqe, 0, sizeof(kcqe));
  1260. if (!dev->max_iscsi_conn) {
  1261. kcqe.completion_status =
  1262. ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
  1263. goto done;
  1264. }
  1265. CNIC_WR(dev, BAR_TSTRORM_INTMEM +
  1266. TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
  1267. CNIC_WR(dev, BAR_TSTRORM_INTMEM +
  1268. TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
  1269. req2->error_bit_map[1]);
  1270. CNIC_WR16(dev, BAR_USTRORM_INTMEM +
  1271. USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
  1272. CNIC_WR(dev, BAR_USTRORM_INTMEM +
  1273. USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
  1274. CNIC_WR(dev, BAR_USTRORM_INTMEM +
  1275. USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
  1276. req2->error_bit_map[1]);
  1277. CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
  1278. CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
  1279. kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
  1280. done:
  1281. kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
  1282. cqes[0] = (struct kcqe *) &kcqe;
  1283. cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
  1284. return 0;
  1285. }
  1286. static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
  1287. {
  1288. struct cnic_local *cp = dev->cnic_priv;
  1289. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  1290. if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
  1291. struct cnic_iscsi *iscsi = ctx->proto.iscsi;
  1292. cnic_free_dma(dev, &iscsi->hq_info);
  1293. cnic_free_dma(dev, &iscsi->r2tq_info);
  1294. cnic_free_dma(dev, &iscsi->task_array_info);
  1295. cnic_free_id(&cp->cid_tbl, ctx->cid);
  1296. } else {
  1297. cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
  1298. }
  1299. ctx->cid = 0;
  1300. }
  1301. static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
  1302. {
  1303. u32 cid;
  1304. int ret, pages;
  1305. struct cnic_local *cp = dev->cnic_priv;
  1306. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  1307. struct cnic_iscsi *iscsi = ctx->proto.iscsi;
  1308. if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
  1309. cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
  1310. if (cid == -1) {
  1311. ret = -ENOMEM;
  1312. goto error;
  1313. }
  1314. ctx->cid = cid;
  1315. return 0;
  1316. }
  1317. cid = cnic_alloc_new_id(&cp->cid_tbl);
  1318. if (cid == -1) {
  1319. ret = -ENOMEM;
  1320. goto error;
  1321. }
  1322. ctx->cid = cid;
  1323. pages = CNIC_PAGE_ALIGN(cp->task_array_size) / CNIC_PAGE_SIZE;
  1324. ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
  1325. if (ret)
  1326. goto error;
  1327. pages = CNIC_PAGE_ALIGN(cp->r2tq_size) / CNIC_PAGE_SIZE;
  1328. ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
  1329. if (ret)
  1330. goto error;
  1331. pages = CNIC_PAGE_ALIGN(cp->hq_size) / CNIC_PAGE_SIZE;
  1332. ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
  1333. if (ret)
  1334. goto error;
  1335. return 0;
  1336. error:
  1337. cnic_free_bnx2x_conn_resc(dev, l5_cid);
  1338. return ret;
  1339. }
  1340. static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
  1341. struct regpair *ctx_addr)
  1342. {
  1343. struct cnic_local *cp = dev->cnic_priv;
  1344. struct cnic_eth_dev *ethdev = cp->ethdev;
  1345. int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
  1346. int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
  1347. unsigned long align_off = 0;
  1348. dma_addr_t ctx_map;
  1349. void *ctx;
  1350. if (cp->ctx_align) {
  1351. unsigned long mask = cp->ctx_align - 1;
  1352. if (cp->ctx_arr[blk].mapping & mask)
  1353. align_off = cp->ctx_align -
  1354. (cp->ctx_arr[blk].mapping & mask);
  1355. }
  1356. ctx_map = cp->ctx_arr[blk].mapping + align_off +
  1357. (off * BNX2X_CONTEXT_MEM_SIZE);
  1358. ctx = cp->ctx_arr[blk].ctx + align_off +
  1359. (off * BNX2X_CONTEXT_MEM_SIZE);
  1360. if (init)
  1361. memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
  1362. ctx_addr->lo = ctx_map & 0xffffffff;
  1363. ctx_addr->hi = (u64) ctx_map >> 32;
  1364. return ctx;
  1365. }
  1366. static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
  1367. u32 num)
  1368. {
  1369. struct cnic_local *cp = dev->cnic_priv;
  1370. struct bnx2x *bp = netdev_priv(dev->netdev);
  1371. struct iscsi_kwqe_conn_offload1 *req1 =
  1372. (struct iscsi_kwqe_conn_offload1 *) wqes[0];
  1373. struct iscsi_kwqe_conn_offload2 *req2 =
  1374. (struct iscsi_kwqe_conn_offload2 *) wqes[1];
  1375. struct iscsi_kwqe_conn_offload3 *req3;
  1376. struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
  1377. struct cnic_iscsi *iscsi = ctx->proto.iscsi;
  1378. u32 cid = ctx->cid;
  1379. u32 hw_cid = BNX2X_HW_CID(bp, cid);
  1380. struct iscsi_context *ictx;
  1381. struct regpair context_addr;
  1382. int i, j, n = 2, n_max;
  1383. u8 port = BP_PORT(bp);
  1384. ctx->ctx_flags = 0;
  1385. if (!req2->num_additional_wqes)
  1386. return -EINVAL;
  1387. n_max = req2->num_additional_wqes + 2;
  1388. ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
  1389. if (ictx == NULL)
  1390. return -ENOMEM;
  1391. req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
  1392. ictx->xstorm_ag_context.hq_prod = 1;
  1393. ictx->xstorm_st_context.iscsi.first_burst_length =
  1394. ISCSI_DEF_FIRST_BURST_LEN;
  1395. ictx->xstorm_st_context.iscsi.max_send_pdu_length =
  1396. ISCSI_DEF_MAX_RECV_SEG_LEN;
  1397. ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
  1398. req1->sq_page_table_addr_lo;
  1399. ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
  1400. req1->sq_page_table_addr_hi;
  1401. ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
  1402. ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
  1403. ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
  1404. iscsi->hq_info.pgtbl_map & 0xffffffff;
  1405. ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
  1406. (u64) iscsi->hq_info.pgtbl_map >> 32;
  1407. ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
  1408. iscsi->hq_info.pgtbl[0];
  1409. ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
  1410. iscsi->hq_info.pgtbl[1];
  1411. ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
  1412. iscsi->r2tq_info.pgtbl_map & 0xffffffff;
  1413. ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
  1414. (u64) iscsi->r2tq_info.pgtbl_map >> 32;
  1415. ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
  1416. iscsi->r2tq_info.pgtbl[0];
  1417. ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
  1418. iscsi->r2tq_info.pgtbl[1];
  1419. ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
  1420. iscsi->task_array_info.pgtbl_map & 0xffffffff;
  1421. ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
  1422. (u64) iscsi->task_array_info.pgtbl_map >> 32;
  1423. ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
  1424. BNX2X_ISCSI_PBL_NOT_CACHED;
  1425. ictx->xstorm_st_context.iscsi.flags.flags |=
  1426. XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
  1427. ictx->xstorm_st_context.iscsi.flags.flags |=
  1428. XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
  1429. ictx->xstorm_st_context.common.ethernet.reserved_vlan_type =
  1430. ETH_P_8021Q;
  1431. if (BNX2X_CHIP_IS_E2_PLUS(bp) &&
  1432. bp->common.chip_port_mode == CHIP_2_PORT_MODE) {
  1433. port = 0;
  1434. }
  1435. ictx->xstorm_st_context.common.flags =
  1436. 1 << XSTORM_COMMON_CONTEXT_SECTION_PHYSQ_INITIALIZED_SHIFT;
  1437. ictx->xstorm_st_context.common.flags =
  1438. port << XSTORM_COMMON_CONTEXT_SECTION_PBF_PORT_SHIFT;
  1439. ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
  1440. /* TSTORM requires the base address of RQ DB & not PTE */
  1441. ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
  1442. req2->rq_page_table_addr_lo & CNIC_PAGE_MASK;
  1443. ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
  1444. req2->rq_page_table_addr_hi;
  1445. ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
  1446. ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
  1447. ictx->tstorm_st_context.tcp.flags2 |=
  1448. TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
  1449. ictx->tstorm_st_context.tcp.ooo_support_mode =
  1450. TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
  1451. ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
  1452. ictx->ustorm_st_context.ring.rq.pbl_base.lo =
  1453. req2->rq_page_table_addr_lo;
  1454. ictx->ustorm_st_context.ring.rq.pbl_base.hi =
  1455. req2->rq_page_table_addr_hi;
  1456. ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
  1457. ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
  1458. ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
  1459. iscsi->r2tq_info.pgtbl_map & 0xffffffff;
  1460. ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
  1461. (u64) iscsi->r2tq_info.pgtbl_map >> 32;
  1462. ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
  1463. iscsi->r2tq_info.pgtbl[0];
  1464. ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
  1465. iscsi->r2tq_info.pgtbl[1];
  1466. ictx->ustorm_st_context.ring.cq_pbl_base.lo =
  1467. req1->cq_page_table_addr_lo;
  1468. ictx->ustorm_st_context.ring.cq_pbl_base.hi =
  1469. req1->cq_page_table_addr_hi;
  1470. ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
  1471. ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
  1472. ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
  1473. ictx->ustorm_st_context.task_pbe_cache_index =
  1474. BNX2X_ISCSI_PBL_NOT_CACHED;
  1475. ictx->ustorm_st_context.task_pdu_cache_index =
  1476. BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
  1477. for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
  1478. if (j == 3) {
  1479. if (n >= n_max)
  1480. break;
  1481. req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
  1482. j = 0;
  1483. }
  1484. ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
  1485. ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
  1486. req3->qp_first_pte[j].hi;
  1487. ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
  1488. req3->qp_first_pte[j].lo;
  1489. }
  1490. ictx->ustorm_st_context.task_pbl_base.lo =
  1491. iscsi->task_array_info.pgtbl_map & 0xffffffff;
  1492. ictx->ustorm_st_context.task_pbl_base.hi =
  1493. (u64) iscsi->task_array_info.pgtbl_map >> 32;
  1494. ictx->ustorm_st_context.tce_phy_addr.lo =
  1495. iscsi->task_array_info.pgtbl[0];
  1496. ictx->ustorm_st_context.tce_phy_addr.hi =
  1497. iscsi->task_array_info.pgtbl[1];
  1498. ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
  1499. ictx->ustorm_st_context.num_cqs = cp->num_cqs;
  1500. ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
  1501. ictx->ustorm_st_context.negotiated_rx_and_flags |=
  1502. ISCSI_DEF_MAX_BURST_LEN;
  1503. ictx->ustorm_st_context.negotiated_rx |=
  1504. ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
  1505. USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
  1506. ictx->cstorm_st_context.hq_pbl_base.lo =
  1507. iscsi->hq_info.pgtbl_map & 0xffffffff;
  1508. ictx->cstorm_st_context.hq_pbl_base.hi =
  1509. (u64) iscsi->hq_info.pgtbl_map >> 32;
  1510. ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
  1511. ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
  1512. ictx->cstorm_st_context.task_pbl_base.lo =
  1513. iscsi->task_array_info.pgtbl_map & 0xffffffff;
  1514. ictx->cstorm_st_context.task_pbl_base.hi =
  1515. (u64) iscsi->task_array_info.pgtbl_map >> 32;
  1516. /* CSTORM and USTORM initialization is different, CSTORM requires
  1517. * CQ DB base & not PTE addr */
  1518. ictx->cstorm_st_context.cq_db_base.lo =
  1519. req1->cq_page_table_addr_lo & CNIC_PAGE_MASK;
  1520. ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
  1521. ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
  1522. ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
  1523. for (i = 0; i < cp->num_cqs; i++) {
  1524. ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
  1525. ISCSI_INITIAL_SN;
  1526. ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
  1527. ISCSI_INITIAL_SN;
  1528. }
  1529. ictx->xstorm_ag_context.cdu_reserved =
  1530. CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
  1531. ISCSI_CONNECTION_TYPE);
  1532. ictx->ustorm_ag_context.cdu_usage =
  1533. CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
  1534. ISCSI_CONNECTION_TYPE);
  1535. return 0;
  1536. }
  1537. static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
  1538. u32 num, int *work)
  1539. {
  1540. struct iscsi_kwqe_conn_offload1 *req1;
  1541. struct iscsi_kwqe_conn_offload2 *req2;
  1542. struct cnic_local *cp = dev->cnic_priv;
  1543. struct bnx2x *bp = netdev_priv(dev->netdev);
  1544. struct cnic_context *ctx;
  1545. struct iscsi_kcqe kcqe;
  1546. struct kcqe *cqes[1];
  1547. u32 l5_cid;
  1548. int ret = 0;
  1549. if (num < 2) {
  1550. *work = num;
  1551. return -EINVAL;
  1552. }
  1553. req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
  1554. req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
  1555. if ((num - 2) < req2->num_additional_wqes) {
  1556. *work = num;
  1557. return -EINVAL;
  1558. }
  1559. *work = 2 + req2->num_additional_wqes;
  1560. l5_cid = req1->iscsi_conn_id;
  1561. if (l5_cid >= MAX_ISCSI_TBL_SZ)
  1562. return -EINVAL;
  1563. memset(&kcqe, 0, sizeof(kcqe));
  1564. kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
  1565. kcqe.iscsi_conn_id = l5_cid;
  1566. kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
  1567. ctx = &cp->ctx_tbl[l5_cid];
  1568. if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
  1569. kcqe.completion_status =
  1570. ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
  1571. goto done;
  1572. }
  1573. if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
  1574. atomic_dec(&cp->iscsi_conn);
  1575. goto done;
  1576. }
  1577. ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
  1578. if (ret) {
  1579. atomic_dec(&cp->iscsi_conn);
  1580. ret = 0;
  1581. goto done;
  1582. }
  1583. ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
  1584. if (ret < 0) {
  1585. cnic_free_bnx2x_conn_resc(dev, l5_cid);
  1586. atomic_dec(&cp->iscsi_conn);
  1587. goto done;
  1588. }
  1589. kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
  1590. kcqe.iscsi_conn_context_id = BNX2X_HW_CID(bp, cp->ctx_tbl[l5_cid].cid);
  1591. done:
  1592. cqes[0] = (struct kcqe *) &kcqe;
  1593. cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
  1594. return 0;
  1595. }
  1596. static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
  1597. {
  1598. struct cnic_local *cp = dev->cnic_priv;
  1599. struct iscsi_kwqe_conn_update *req =
  1600. (struct iscsi_kwqe_conn_update *) kwqe;
  1601. void *data;
  1602. union l5cm_specific_data l5_data;
  1603. u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
  1604. int ret;
  1605. if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
  1606. return -EINVAL;
  1607. data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
  1608. if (!data)
  1609. return -ENOMEM;
  1610. memcpy(data, kwqe, sizeof(struct kwqe));
  1611. ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
  1612. req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
  1613. return ret;
  1614. }
  1615. static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
  1616. {
  1617. struct cnic_local *cp = dev->cnic_priv;
  1618. struct bnx2x *bp = netdev_priv(dev->netdev);
  1619. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  1620. union l5cm_specific_data l5_data;
  1621. int ret;
  1622. u32 hw_cid;
  1623. init_waitqueue_head(&ctx->waitq);
  1624. ctx->wait_cond = 0;
  1625. memset(&l5_data, 0, sizeof(l5_data));
  1626. hw_cid = BNX2X_HW_CID(bp, ctx->cid);
  1627. ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
  1628. hw_cid, NONE_CONNECTION_TYPE, &l5_data);
  1629. if (ret == 0) {
  1630. wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
  1631. if (unlikely(test_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags)))
  1632. return -EBUSY;
  1633. }
  1634. return 0;
  1635. }
  1636. static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
  1637. {
  1638. struct cnic_local *cp = dev->cnic_priv;
  1639. struct iscsi_kwqe_conn_destroy *req =
  1640. (struct iscsi_kwqe_conn_destroy *) kwqe;
  1641. u32 l5_cid = req->reserved0;
  1642. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  1643. int ret = 0;
  1644. struct iscsi_kcqe kcqe;
  1645. struct kcqe *cqes[1];
  1646. if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
  1647. goto skip_cfc_delete;
  1648. if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
  1649. unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
  1650. if (delta > (2 * HZ))
  1651. delta = 0;
  1652. set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
  1653. queue_delayed_work(cnic_wq, &cp->delete_task, delta);
  1654. goto destroy_reply;
  1655. }
  1656. ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
  1657. skip_cfc_delete:
  1658. cnic_free_bnx2x_conn_resc(dev, l5_cid);
  1659. if (!ret) {
  1660. atomic_dec(&cp->iscsi_conn);
  1661. clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
  1662. }
  1663. destroy_reply:
  1664. memset(&kcqe, 0, sizeof(kcqe));
  1665. kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
  1666. kcqe.iscsi_conn_id = l5_cid;
  1667. kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
  1668. kcqe.iscsi_conn_context_id = req->context_id;
  1669. cqes[0] = (struct kcqe *) &kcqe;
  1670. cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
  1671. return 0;
  1672. }
  1673. static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
  1674. struct l4_kwq_connect_req1 *kwqe1,
  1675. struct l4_kwq_connect_req3 *kwqe3,
  1676. struct l5cm_active_conn_buffer *conn_buf)
  1677. {
  1678. struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
  1679. struct l5cm_xstorm_conn_buffer *xstorm_buf =
  1680. &conn_buf->xstorm_conn_buffer;
  1681. struct l5cm_tstorm_conn_buffer *tstorm_buf =
  1682. &conn_buf->tstorm_conn_buffer;
  1683. struct regpair context_addr;
  1684. u32 cid = BNX2X_SW_CID(kwqe1->cid);
  1685. struct in6_addr src_ip, dst_ip;
  1686. int i;
  1687. u32 *addrp;
  1688. addrp = (u32 *) &conn_addr->local_ip_addr;
  1689. for (i = 0; i < 4; i++, addrp++)
  1690. src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
  1691. addrp = (u32 *) &conn_addr->remote_ip_addr;
  1692. for (i = 0; i < 4; i++, addrp++)
  1693. dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
  1694. cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
  1695. xstorm_buf->context_addr.hi = context_addr.hi;
  1696. xstorm_buf->context_addr.lo = context_addr.lo;
  1697. xstorm_buf->mss = 0xffff;
  1698. xstorm_buf->rcv_buf = kwqe3->rcv_buf;
  1699. if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
  1700. xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
  1701. xstorm_buf->pseudo_header_checksum =
  1702. swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
  1703. if (kwqe3->ka_timeout) {
  1704. tstorm_buf->ka_enable = 1;
  1705. tstorm_buf->ka_timeout = kwqe3->ka_timeout;
  1706. tstorm_buf->ka_interval = kwqe3->ka_interval;
  1707. tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
  1708. }
  1709. tstorm_buf->max_rt_time = 0xffffffff;
  1710. }
  1711. static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
  1712. {
  1713. struct bnx2x *bp = netdev_priv(dev->netdev);
  1714. u32 pfid = bp->pfid;
  1715. u8 *mac = dev->mac_addr;
  1716. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1717. XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
  1718. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1719. XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
  1720. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1721. XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
  1722. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1723. XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
  1724. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1725. XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
  1726. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  1727. XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
  1728. CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
  1729. TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
  1730. CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
  1731. TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
  1732. mac[4]);
  1733. CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
  1734. TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
  1735. CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
  1736. TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
  1737. mac[2]);
  1738. CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
  1739. TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[1]);
  1740. CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
  1741. TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
  1742. mac[0]);
  1743. }
  1744. static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
  1745. u32 num, int *work)
  1746. {
  1747. struct cnic_local *cp = dev->cnic_priv;
  1748. struct bnx2x *bp = netdev_priv(dev->netdev);
  1749. struct l4_kwq_connect_req1 *kwqe1 =
  1750. (struct l4_kwq_connect_req1 *) wqes[0];
  1751. struct l4_kwq_connect_req3 *kwqe3;
  1752. struct l5cm_active_conn_buffer *conn_buf;
  1753. struct l5cm_conn_addr_params *conn_addr;
  1754. union l5cm_specific_data l5_data;
  1755. u32 l5_cid = kwqe1->pg_cid;
  1756. struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
  1757. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  1758. int ret;
  1759. if (num < 2) {
  1760. *work = num;
  1761. return -EINVAL;
  1762. }
  1763. if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
  1764. *work = 3;
  1765. else
  1766. *work = 2;
  1767. if (num < *work) {
  1768. *work = num;
  1769. return -EINVAL;
  1770. }
  1771. if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
  1772. netdev_err(dev->netdev, "conn_buf size too big\n");
  1773. return -ENOMEM;
  1774. }
  1775. conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
  1776. if (!conn_buf)
  1777. return -ENOMEM;
  1778. memset(conn_buf, 0, sizeof(*conn_buf));
  1779. conn_addr = &conn_buf->conn_addr_buf;
  1780. conn_addr->remote_addr_0 = csk->ha[0];
  1781. conn_addr->remote_addr_1 = csk->ha[1];
  1782. conn_addr->remote_addr_2 = csk->ha[2];
  1783. conn_addr->remote_addr_3 = csk->ha[3];
  1784. conn_addr->remote_addr_4 = csk->ha[4];
  1785. conn_addr->remote_addr_5 = csk->ha[5];
  1786. if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
  1787. struct l4_kwq_connect_req2 *kwqe2 =
  1788. (struct l4_kwq_connect_req2 *) wqes[1];
  1789. conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
  1790. conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
  1791. conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
  1792. conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
  1793. conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
  1794. conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
  1795. conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
  1796. }
  1797. kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
  1798. conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
  1799. conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
  1800. conn_addr->local_tcp_port = kwqe1->src_port;
  1801. conn_addr->remote_tcp_port = kwqe1->dst_port;
  1802. conn_addr->pmtu = kwqe3->pmtu;
  1803. cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
  1804. CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
  1805. XSTORM_ISCSI_LOCAL_VLAN_OFFSET(bp->pfid), csk->vlan_id);
  1806. ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
  1807. kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
  1808. if (!ret)
  1809. set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
  1810. return ret;
  1811. }
  1812. static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
  1813. {
  1814. struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
  1815. union l5cm_specific_data l5_data;
  1816. int ret;
  1817. memset(&l5_data, 0, sizeof(l5_data));
  1818. ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
  1819. req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
  1820. return ret;
  1821. }
  1822. static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
  1823. {
  1824. struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
  1825. union l5cm_specific_data l5_data;
  1826. int ret;
  1827. memset(&l5_data, 0, sizeof(l5_data));
  1828. ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
  1829. req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
  1830. return ret;
  1831. }
  1832. static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
  1833. {
  1834. struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
  1835. struct l4_kcq kcqe;
  1836. struct kcqe *cqes[1];
  1837. memset(&kcqe, 0, sizeof(kcqe));
  1838. kcqe.pg_host_opaque = req->host_opaque;
  1839. kcqe.pg_cid = req->host_opaque;
  1840. kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
  1841. cqes[0] = (struct kcqe *) &kcqe;
  1842. cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
  1843. return 0;
  1844. }
  1845. static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
  1846. {
  1847. struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
  1848. struct l4_kcq kcqe;
  1849. struct kcqe *cqes[1];
  1850. memset(&kcqe, 0, sizeof(kcqe));
  1851. kcqe.pg_host_opaque = req->pg_host_opaque;
  1852. kcqe.pg_cid = req->pg_cid;
  1853. kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
  1854. cqes[0] = (struct kcqe *) &kcqe;
  1855. cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
  1856. return 0;
  1857. }
  1858. static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
  1859. {
  1860. struct fcoe_kwqe_stat *req;
  1861. struct fcoe_stat_ramrod_params *fcoe_stat;
  1862. union l5cm_specific_data l5_data;
  1863. struct cnic_local *cp = dev->cnic_priv;
  1864. struct bnx2x *bp = netdev_priv(dev->netdev);
  1865. int ret;
  1866. u32 cid;
  1867. req = (struct fcoe_kwqe_stat *) kwqe;
  1868. cid = BNX2X_HW_CID(bp, cp->fcoe_init_cid);
  1869. fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
  1870. if (!fcoe_stat)
  1871. return -ENOMEM;
  1872. memset(fcoe_stat, 0, sizeof(*fcoe_stat));
  1873. memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
  1874. ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT_FUNC, cid,
  1875. FCOE_CONNECTION_TYPE, &l5_data);
  1876. return ret;
  1877. }
  1878. static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
  1879. u32 num, int *work)
  1880. {
  1881. int ret;
  1882. struct cnic_local *cp = dev->cnic_priv;
  1883. struct bnx2x *bp = netdev_priv(dev->netdev);
  1884. u32 cid;
  1885. struct fcoe_init_ramrod_params *fcoe_init;
  1886. struct fcoe_kwqe_init1 *req1;
  1887. struct fcoe_kwqe_init2 *req2;
  1888. struct fcoe_kwqe_init3 *req3;
  1889. union l5cm_specific_data l5_data;
  1890. if (num < 3) {
  1891. *work = num;
  1892. return -EINVAL;
  1893. }
  1894. req1 = (struct fcoe_kwqe_init1 *) wqes[0];
  1895. req2 = (struct fcoe_kwqe_init2 *) wqes[1];
  1896. req3 = (struct fcoe_kwqe_init3 *) wqes[2];
  1897. if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
  1898. *work = 1;
  1899. return -EINVAL;
  1900. }
  1901. if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
  1902. *work = 2;
  1903. return -EINVAL;
  1904. }
  1905. if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
  1906. netdev_err(dev->netdev, "fcoe_init size too big\n");
  1907. return -ENOMEM;
  1908. }
  1909. fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
  1910. if (!fcoe_init)
  1911. return -ENOMEM;
  1912. memset(fcoe_init, 0, sizeof(*fcoe_init));
  1913. memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
  1914. memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
  1915. memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
  1916. fcoe_init->eq_pbl_base.lo = cp->kcq2.dma.pgtbl_map & 0xffffffff;
  1917. fcoe_init->eq_pbl_base.hi = (u64) cp->kcq2.dma.pgtbl_map >> 32;
  1918. fcoe_init->eq_pbl_size = cp->kcq2.dma.num_pages;
  1919. fcoe_init->sb_num = cp->status_blk_num;
  1920. fcoe_init->eq_prod = MAX_KCQ_IDX;
  1921. fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
  1922. cp->kcq2.sw_prod_idx = 0;
  1923. cid = BNX2X_HW_CID(bp, cp->fcoe_init_cid);
  1924. ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT_FUNC, cid,
  1925. FCOE_CONNECTION_TYPE, &l5_data);
  1926. *work = 3;
  1927. return ret;
  1928. }
  1929. static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
  1930. u32 num, int *work)
  1931. {
  1932. int ret = 0;
  1933. u32 cid = -1, l5_cid;
  1934. struct cnic_local *cp = dev->cnic_priv;
  1935. struct bnx2x *bp = netdev_priv(dev->netdev);
  1936. struct fcoe_kwqe_conn_offload1 *req1;
  1937. struct fcoe_kwqe_conn_offload2 *req2;
  1938. struct fcoe_kwqe_conn_offload3 *req3;
  1939. struct fcoe_kwqe_conn_offload4 *req4;
  1940. struct fcoe_conn_offload_ramrod_params *fcoe_offload;
  1941. struct cnic_context *ctx;
  1942. struct fcoe_context *fctx;
  1943. struct regpair ctx_addr;
  1944. union l5cm_specific_data l5_data;
  1945. struct fcoe_kcqe kcqe;
  1946. struct kcqe *cqes[1];
  1947. if (num < 4) {
  1948. *work = num;
  1949. return -EINVAL;
  1950. }
  1951. req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
  1952. req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
  1953. req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
  1954. req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
  1955. *work = 4;
  1956. l5_cid = req1->fcoe_conn_id;
  1957. if (l5_cid >= dev->max_fcoe_conn)
  1958. goto err_reply;
  1959. l5_cid += BNX2X_FCOE_L5_CID_BASE;
  1960. ctx = &cp->ctx_tbl[l5_cid];
  1961. if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
  1962. goto err_reply;
  1963. ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
  1964. if (ret) {
  1965. ret = 0;
  1966. goto err_reply;
  1967. }
  1968. cid = ctx->cid;
  1969. fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
  1970. if (fctx) {
  1971. u32 hw_cid = BNX2X_HW_CID(bp, cid);
  1972. u32 val;
  1973. val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
  1974. FCOE_CONNECTION_TYPE);
  1975. fctx->xstorm_ag_context.cdu_reserved = val;
  1976. val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
  1977. FCOE_CONNECTION_TYPE);
  1978. fctx->ustorm_ag_context.cdu_usage = val;
  1979. }
  1980. if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
  1981. netdev_err(dev->netdev, "fcoe_offload size too big\n");
  1982. goto err_reply;
  1983. }
  1984. fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
  1985. if (!fcoe_offload)
  1986. goto err_reply;
  1987. memset(fcoe_offload, 0, sizeof(*fcoe_offload));
  1988. memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
  1989. memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
  1990. memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
  1991. memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
  1992. cid = BNX2X_HW_CID(bp, cid);
  1993. ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
  1994. FCOE_CONNECTION_TYPE, &l5_data);
  1995. if (!ret)
  1996. set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
  1997. return ret;
  1998. err_reply:
  1999. if (cid != -1)
  2000. cnic_free_bnx2x_conn_resc(dev, l5_cid);
  2001. memset(&kcqe, 0, sizeof(kcqe));
  2002. kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
  2003. kcqe.fcoe_conn_id = req1->fcoe_conn_id;
  2004. kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
  2005. cqes[0] = (struct kcqe *) &kcqe;
  2006. cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
  2007. return ret;
  2008. }
  2009. static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
  2010. {
  2011. struct fcoe_kwqe_conn_enable_disable *req;
  2012. struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
  2013. union l5cm_specific_data l5_data;
  2014. int ret;
  2015. u32 cid, l5_cid;
  2016. struct cnic_local *cp = dev->cnic_priv;
  2017. req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
  2018. cid = req->context_id;
  2019. l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
  2020. if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
  2021. netdev_err(dev->netdev, "fcoe_enable size too big\n");
  2022. return -ENOMEM;
  2023. }
  2024. fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
  2025. if (!fcoe_enable)
  2026. return -ENOMEM;
  2027. memset(fcoe_enable, 0, sizeof(*fcoe_enable));
  2028. memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
  2029. ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
  2030. FCOE_CONNECTION_TYPE, &l5_data);
  2031. return ret;
  2032. }
  2033. static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
  2034. {
  2035. struct fcoe_kwqe_conn_enable_disable *req;
  2036. struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
  2037. union l5cm_specific_data l5_data;
  2038. int ret;
  2039. u32 cid, l5_cid;
  2040. struct cnic_local *cp = dev->cnic_priv;
  2041. req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
  2042. cid = req->context_id;
  2043. l5_cid = req->conn_id;
  2044. if (l5_cid >= dev->max_fcoe_conn)
  2045. return -EINVAL;
  2046. l5_cid += BNX2X_FCOE_L5_CID_BASE;
  2047. if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
  2048. netdev_err(dev->netdev, "fcoe_disable size too big\n");
  2049. return -ENOMEM;
  2050. }
  2051. fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
  2052. if (!fcoe_disable)
  2053. return -ENOMEM;
  2054. memset(fcoe_disable, 0, sizeof(*fcoe_disable));
  2055. memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
  2056. ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
  2057. FCOE_CONNECTION_TYPE, &l5_data);
  2058. return ret;
  2059. }
  2060. static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
  2061. {
  2062. struct fcoe_kwqe_conn_destroy *req;
  2063. union l5cm_specific_data l5_data;
  2064. int ret;
  2065. u32 cid, l5_cid;
  2066. struct cnic_local *cp = dev->cnic_priv;
  2067. struct cnic_context *ctx;
  2068. struct fcoe_kcqe kcqe;
  2069. struct kcqe *cqes[1];
  2070. req = (struct fcoe_kwqe_conn_destroy *) kwqe;
  2071. cid = req->context_id;
  2072. l5_cid = req->conn_id;
  2073. if (l5_cid >= dev->max_fcoe_conn)
  2074. return -EINVAL;
  2075. l5_cid += BNX2X_FCOE_L5_CID_BASE;
  2076. ctx = &cp->ctx_tbl[l5_cid];
  2077. init_waitqueue_head(&ctx->waitq);
  2078. ctx->wait_cond = 0;
  2079. memset(&kcqe, 0, sizeof(kcqe));
  2080. kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_ERROR;
  2081. memset(&l5_data, 0, sizeof(l5_data));
  2082. ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
  2083. FCOE_CONNECTION_TYPE, &l5_data);
  2084. if (ret == 0) {
  2085. wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
  2086. if (ctx->wait_cond)
  2087. kcqe.completion_status = 0;
  2088. }
  2089. set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
  2090. queue_delayed_work(cnic_wq, &cp->delete_task, msecs_to_jiffies(2000));
  2091. kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
  2092. kcqe.fcoe_conn_id = req->conn_id;
  2093. kcqe.fcoe_conn_context_id = cid;
  2094. cqes[0] = (struct kcqe *) &kcqe;
  2095. cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
  2096. return ret;
  2097. }
  2098. static void cnic_bnx2x_delete_wait(struct cnic_dev *dev, u32 start_cid)
  2099. {
  2100. struct cnic_local *cp = dev->cnic_priv;
  2101. u32 i;
  2102. for (i = start_cid; i < cp->max_cid_space; i++) {
  2103. struct cnic_context *ctx = &cp->ctx_tbl[i];
  2104. int j;
  2105. while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
  2106. msleep(10);
  2107. for (j = 0; j < 5; j++) {
  2108. if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
  2109. break;
  2110. msleep(20);
  2111. }
  2112. if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
  2113. netdev_warn(dev->netdev, "CID %x not deleted\n",
  2114. ctx->cid);
  2115. }
  2116. }
  2117. static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
  2118. {
  2119. struct fcoe_kwqe_destroy *req;
  2120. union l5cm_specific_data l5_data;
  2121. struct cnic_local *cp = dev->cnic_priv;
  2122. struct bnx2x *bp = netdev_priv(dev->netdev);
  2123. int ret;
  2124. u32 cid;
  2125. cnic_bnx2x_delete_wait(dev, MAX_ISCSI_TBL_SZ);
  2126. req = (struct fcoe_kwqe_destroy *) kwqe;
  2127. cid = BNX2X_HW_CID(bp, cp->fcoe_init_cid);
  2128. memset(&l5_data, 0, sizeof(l5_data));
  2129. ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY_FUNC, cid,
  2130. FCOE_CONNECTION_TYPE, &l5_data);
  2131. return ret;
  2132. }
  2133. static void cnic_bnx2x_kwqe_err(struct cnic_dev *dev, struct kwqe *kwqe)
  2134. {
  2135. struct cnic_local *cp = dev->cnic_priv;
  2136. struct kcqe kcqe;
  2137. struct kcqe *cqes[1];
  2138. u32 cid;
  2139. u32 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
  2140. u32 layer_code = kwqe->kwqe_op_flag & KWQE_LAYER_MASK;
  2141. u32 kcqe_op;
  2142. int ulp_type;
  2143. cid = kwqe->kwqe_info0;
  2144. memset(&kcqe, 0, sizeof(kcqe));
  2145. if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_FCOE) {
  2146. u32 l5_cid = 0;
  2147. ulp_type = CNIC_ULP_FCOE;
  2148. if (opcode == FCOE_KWQE_OPCODE_DISABLE_CONN) {
  2149. struct fcoe_kwqe_conn_enable_disable *req;
  2150. req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
  2151. kcqe_op = FCOE_KCQE_OPCODE_DISABLE_CONN;
  2152. cid = req->context_id;
  2153. l5_cid = req->conn_id;
  2154. } else if (opcode == FCOE_KWQE_OPCODE_DESTROY) {
  2155. kcqe_op = FCOE_KCQE_OPCODE_DESTROY_FUNC;
  2156. } else {
  2157. return;
  2158. }
  2159. kcqe.kcqe_op_flag = kcqe_op << KCQE_FLAGS_OPCODE_SHIFT;
  2160. kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_FCOE;
  2161. kcqe.kcqe_info1 = FCOE_KCQE_COMPLETION_STATUS_PARITY_ERROR;
  2162. kcqe.kcqe_info2 = cid;
  2163. kcqe.kcqe_info0 = l5_cid;
  2164. } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_ISCSI) {
  2165. ulp_type = CNIC_ULP_ISCSI;
  2166. if (opcode == ISCSI_KWQE_OPCODE_UPDATE_CONN)
  2167. cid = kwqe->kwqe_info1;
  2168. kcqe.kcqe_op_flag = (opcode + 0x10) << KCQE_FLAGS_OPCODE_SHIFT;
  2169. kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_ISCSI;
  2170. kcqe.kcqe_info1 = ISCSI_KCQE_COMPLETION_STATUS_PARITY_ERR;
  2171. kcqe.kcqe_info2 = cid;
  2172. cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &kcqe.kcqe_info0);
  2173. } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L4) {
  2174. struct l4_kcq *l4kcqe = (struct l4_kcq *) &kcqe;
  2175. ulp_type = CNIC_ULP_L4;
  2176. if (opcode == L4_KWQE_OPCODE_VALUE_CONNECT1)
  2177. kcqe_op = L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE;
  2178. else if (opcode == L4_KWQE_OPCODE_VALUE_RESET)
  2179. kcqe_op = L4_KCQE_OPCODE_VALUE_RESET_COMP;
  2180. else if (opcode == L4_KWQE_OPCODE_VALUE_CLOSE)
  2181. kcqe_op = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
  2182. else
  2183. return;
  2184. kcqe.kcqe_op_flag = (kcqe_op << KCQE_FLAGS_OPCODE_SHIFT) |
  2185. KCQE_FLAGS_LAYER_MASK_L4;
  2186. l4kcqe->status = L4_KCQE_COMPLETION_STATUS_PARITY_ERROR;
  2187. l4kcqe->cid = cid;
  2188. cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &l4kcqe->conn_id);
  2189. } else {
  2190. return;
  2191. }
  2192. cqes[0] = &kcqe;
  2193. cnic_reply_bnx2x_kcqes(dev, ulp_type, cqes, 1);
  2194. }
  2195. static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
  2196. struct kwqe *wqes[], u32 num_wqes)
  2197. {
  2198. int i, work, ret;
  2199. u32 opcode;
  2200. struct kwqe *kwqe;
  2201. if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
  2202. return -EAGAIN; /* bnx2 is down */
  2203. for (i = 0; i < num_wqes; ) {
  2204. kwqe = wqes[i];
  2205. opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
  2206. work = 1;
  2207. switch (opcode) {
  2208. case ISCSI_KWQE_OPCODE_INIT1:
  2209. ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
  2210. break;
  2211. case ISCSI_KWQE_OPCODE_INIT2:
  2212. ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
  2213. break;
  2214. case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
  2215. ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
  2216. num_wqes - i, &work);
  2217. break;
  2218. case ISCSI_KWQE_OPCODE_UPDATE_CONN:
  2219. ret = cnic_bnx2x_iscsi_update(dev, kwqe);
  2220. break;
  2221. case ISCSI_KWQE_OPCODE_DESTROY_CONN:
  2222. ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
  2223. break;
  2224. case L4_KWQE_OPCODE_VALUE_CONNECT1:
  2225. ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
  2226. &work);
  2227. break;
  2228. case L4_KWQE_OPCODE_VALUE_CLOSE:
  2229. ret = cnic_bnx2x_close(dev, kwqe);
  2230. break;
  2231. case L4_KWQE_OPCODE_VALUE_RESET:
  2232. ret = cnic_bnx2x_reset(dev, kwqe);
  2233. break;
  2234. case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
  2235. ret = cnic_bnx2x_offload_pg(dev, kwqe);
  2236. break;
  2237. case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
  2238. ret = cnic_bnx2x_update_pg(dev, kwqe);
  2239. break;
  2240. case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
  2241. ret = 0;
  2242. break;
  2243. default:
  2244. ret = 0;
  2245. netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
  2246. opcode);
  2247. break;
  2248. }
  2249. if (ret < 0) {
  2250. netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
  2251. opcode);
  2252. /* Possibly bnx2x parity error, send completion
  2253. * to ulp drivers with error code to speed up
  2254. * cleanup and reset recovery.
  2255. */
  2256. if (ret == -EIO || ret == -EAGAIN)
  2257. cnic_bnx2x_kwqe_err(dev, kwqe);
  2258. }
  2259. i += work;
  2260. }
  2261. return 0;
  2262. }
  2263. static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
  2264. struct kwqe *wqes[], u32 num_wqes)
  2265. {
  2266. struct bnx2x *bp = netdev_priv(dev->netdev);
  2267. int i, work, ret;
  2268. u32 opcode;
  2269. struct kwqe *kwqe;
  2270. if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
  2271. return -EAGAIN; /* bnx2 is down */
  2272. if (!BNX2X_CHIP_IS_E2_PLUS(bp))
  2273. return -EINVAL;
  2274. for (i = 0; i < num_wqes; ) {
  2275. kwqe = wqes[i];
  2276. opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
  2277. work = 1;
  2278. switch (opcode) {
  2279. case FCOE_KWQE_OPCODE_INIT1:
  2280. ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
  2281. num_wqes - i, &work);
  2282. break;
  2283. case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
  2284. ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
  2285. num_wqes - i, &work);
  2286. break;
  2287. case FCOE_KWQE_OPCODE_ENABLE_CONN:
  2288. ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
  2289. break;
  2290. case FCOE_KWQE_OPCODE_DISABLE_CONN:
  2291. ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
  2292. break;
  2293. case FCOE_KWQE_OPCODE_DESTROY_CONN:
  2294. ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
  2295. break;
  2296. case FCOE_KWQE_OPCODE_DESTROY:
  2297. ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
  2298. break;
  2299. case FCOE_KWQE_OPCODE_STAT:
  2300. ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
  2301. break;
  2302. default:
  2303. ret = 0;
  2304. netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
  2305. opcode);
  2306. break;
  2307. }
  2308. if (ret < 0) {
  2309. netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
  2310. opcode);
  2311. /* Possibly bnx2x parity error, send completion
  2312. * to ulp drivers with error code to speed up
  2313. * cleanup and reset recovery.
  2314. */
  2315. if (ret == -EIO || ret == -EAGAIN)
  2316. cnic_bnx2x_kwqe_err(dev, kwqe);
  2317. }
  2318. i += work;
  2319. }
  2320. return 0;
  2321. }
  2322. static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
  2323. u32 num_wqes)
  2324. {
  2325. int ret = -EINVAL;
  2326. u32 layer_code;
  2327. if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
  2328. return -EAGAIN; /* bnx2x is down */
  2329. if (!num_wqes)
  2330. return 0;
  2331. layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
  2332. switch (layer_code) {
  2333. case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
  2334. case KWQE_FLAGS_LAYER_MASK_L4:
  2335. case KWQE_FLAGS_LAYER_MASK_L2:
  2336. ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
  2337. break;
  2338. case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
  2339. ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
  2340. break;
  2341. }
  2342. return ret;
  2343. }
  2344. static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
  2345. {
  2346. if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
  2347. return KCQE_FLAGS_LAYER_MASK_L4;
  2348. return opflag & KCQE_FLAGS_LAYER_MASK;
  2349. }
  2350. static void service_kcqes(struct cnic_dev *dev, int num_cqes)
  2351. {
  2352. struct cnic_local *cp = dev->cnic_priv;
  2353. int i, j, comp = 0;
  2354. i = 0;
  2355. j = 1;
  2356. while (num_cqes) {
  2357. struct cnic_ulp_ops *ulp_ops;
  2358. int ulp_type;
  2359. u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
  2360. u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
  2361. if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
  2362. comp++;
  2363. while (j < num_cqes) {
  2364. u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
  2365. if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
  2366. break;
  2367. if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
  2368. comp++;
  2369. j++;
  2370. }
  2371. if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
  2372. ulp_type = CNIC_ULP_RDMA;
  2373. else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
  2374. ulp_type = CNIC_ULP_ISCSI;
  2375. else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
  2376. ulp_type = CNIC_ULP_FCOE;
  2377. else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
  2378. ulp_type = CNIC_ULP_L4;
  2379. else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
  2380. goto end;
  2381. else {
  2382. netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
  2383. kcqe_op_flag);
  2384. goto end;
  2385. }
  2386. rcu_read_lock();
  2387. ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
  2388. if (likely(ulp_ops)) {
  2389. ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
  2390. cp->completed_kcq + i, j);
  2391. }
  2392. rcu_read_unlock();
  2393. end:
  2394. num_cqes -= j;
  2395. i += j;
  2396. j = 1;
  2397. }
  2398. if (unlikely(comp))
  2399. cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
  2400. }
  2401. static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
  2402. {
  2403. struct cnic_local *cp = dev->cnic_priv;
  2404. u16 i, ri, hw_prod, last;
  2405. struct kcqe *kcqe;
  2406. int kcqe_cnt = 0, last_cnt = 0;
  2407. i = ri = last = info->sw_prod_idx;
  2408. ri &= MAX_KCQ_IDX;
  2409. hw_prod = *info->hw_prod_idx_ptr;
  2410. hw_prod = info->hw_idx(hw_prod);
  2411. while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
  2412. kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
  2413. cp->completed_kcq[kcqe_cnt++] = kcqe;
  2414. i = info->next_idx(i);
  2415. ri = i & MAX_KCQ_IDX;
  2416. if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
  2417. last_cnt = kcqe_cnt;
  2418. last = i;
  2419. }
  2420. }
  2421. info->sw_prod_idx = last;
  2422. return last_cnt;
  2423. }
  2424. static int cnic_l2_completion(struct cnic_local *cp)
  2425. {
  2426. u16 hw_cons, sw_cons;
  2427. struct cnic_uio_dev *udev = cp->udev;
  2428. union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
  2429. (udev->l2_ring + (2 * CNIC_PAGE_SIZE));
  2430. u32 cmd;
  2431. int comp = 0;
  2432. if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
  2433. return 0;
  2434. hw_cons = *cp->rx_cons_ptr;
  2435. if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
  2436. hw_cons++;
  2437. sw_cons = cp->rx_cons;
  2438. while (sw_cons != hw_cons) {
  2439. u8 cqe_fp_flags;
  2440. cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
  2441. cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
  2442. if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
  2443. cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
  2444. cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
  2445. if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
  2446. cmd == RAMROD_CMD_ID_ETH_HALT)
  2447. comp++;
  2448. }
  2449. sw_cons = BNX2X_NEXT_RCQE(sw_cons);
  2450. }
  2451. return comp;
  2452. }
  2453. static void cnic_chk_pkt_rings(struct cnic_local *cp)
  2454. {
  2455. u16 rx_cons, tx_cons;
  2456. int comp = 0;
  2457. if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
  2458. return;
  2459. rx_cons = *cp->rx_cons_ptr;
  2460. tx_cons = *cp->tx_cons_ptr;
  2461. if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
  2462. if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
  2463. comp = cnic_l2_completion(cp);
  2464. cp->tx_cons = tx_cons;
  2465. cp->rx_cons = rx_cons;
  2466. if (cp->udev)
  2467. uio_event_notify(&cp->udev->cnic_uinfo);
  2468. }
  2469. if (comp)
  2470. clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
  2471. }
  2472. static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
  2473. {
  2474. struct cnic_local *cp = dev->cnic_priv;
  2475. u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
  2476. int kcqe_cnt;
  2477. /* status block index must be read before reading other fields */
  2478. rmb();
  2479. cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
  2480. while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
  2481. service_kcqes(dev, kcqe_cnt);
  2482. /* Tell compiler that status_blk fields can change. */
  2483. barrier();
  2484. status_idx = (u16) *cp->kcq1.status_idx_ptr;
  2485. /* status block index must be read first */
  2486. rmb();
  2487. cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
  2488. }
  2489. CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
  2490. cnic_chk_pkt_rings(cp);
  2491. return status_idx;
  2492. }
  2493. static int cnic_service_bnx2(void *data, void *status_blk)
  2494. {
  2495. struct cnic_dev *dev = data;
  2496. if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
  2497. struct status_block *sblk = status_blk;
  2498. return sblk->status_idx;
  2499. }
  2500. return cnic_service_bnx2_queues(dev);
  2501. }
  2502. static void cnic_service_bnx2_msix(unsigned long data)
  2503. {
  2504. struct cnic_dev *dev = (struct cnic_dev *) data;
  2505. struct cnic_local *cp = dev->cnic_priv;
  2506. cp->last_status_idx = cnic_service_bnx2_queues(dev);
  2507. CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
  2508. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
  2509. }
  2510. static void cnic_doirq(struct cnic_dev *dev)
  2511. {
  2512. struct cnic_local *cp = dev->cnic_priv;
  2513. if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
  2514. u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
  2515. prefetch(cp->status_blk.gen);
  2516. prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
  2517. tasklet_schedule(&cp->cnic_irq_task);
  2518. }
  2519. }
  2520. static irqreturn_t cnic_irq(int irq, void *dev_instance)
  2521. {
  2522. struct cnic_dev *dev = dev_instance;
  2523. struct cnic_local *cp = dev->cnic_priv;
  2524. if (cp->ack_int)
  2525. cp->ack_int(dev);
  2526. cnic_doirq(dev);
  2527. return IRQ_HANDLED;
  2528. }
  2529. static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
  2530. u16 index, u8 op, u8 update)
  2531. {
  2532. struct bnx2x *bp = netdev_priv(dev->netdev);
  2533. u32 hc_addr = (HC_REG_COMMAND_REG + BP_PORT(bp) * 32 +
  2534. COMMAND_REG_INT_ACK);
  2535. struct igu_ack_register igu_ack;
  2536. igu_ack.status_block_index = index;
  2537. igu_ack.sb_id_and_flags =
  2538. ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
  2539. (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
  2540. (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
  2541. (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
  2542. CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
  2543. }
  2544. static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
  2545. u16 index, u8 op, u8 update)
  2546. {
  2547. struct igu_regular cmd_data;
  2548. u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
  2549. cmd_data.sb_id_and_flags =
  2550. (index << IGU_REGULAR_SB_INDEX_SHIFT) |
  2551. (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
  2552. (update << IGU_REGULAR_BUPDATE_SHIFT) |
  2553. (op << IGU_REGULAR_ENABLE_INT_SHIFT);
  2554. CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
  2555. }
  2556. static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
  2557. {
  2558. struct cnic_local *cp = dev->cnic_priv;
  2559. cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
  2560. IGU_INT_DISABLE, 0);
  2561. }
  2562. static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
  2563. {
  2564. struct cnic_local *cp = dev->cnic_priv;
  2565. cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
  2566. IGU_INT_DISABLE, 0);
  2567. }
  2568. static void cnic_arm_bnx2x_msix(struct cnic_dev *dev, u32 idx)
  2569. {
  2570. struct cnic_local *cp = dev->cnic_priv;
  2571. cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, idx,
  2572. IGU_INT_ENABLE, 1);
  2573. }
  2574. static void cnic_arm_bnx2x_e2_msix(struct cnic_dev *dev, u32 idx)
  2575. {
  2576. struct cnic_local *cp = dev->cnic_priv;
  2577. cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, idx,
  2578. IGU_INT_ENABLE, 1);
  2579. }
  2580. static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
  2581. {
  2582. u32 last_status = *info->status_idx_ptr;
  2583. int kcqe_cnt;
  2584. /* status block index must be read before reading the KCQ */
  2585. rmb();
  2586. while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
  2587. service_kcqes(dev, kcqe_cnt);
  2588. /* Tell compiler that sblk fields can change. */
  2589. barrier();
  2590. last_status = *info->status_idx_ptr;
  2591. /* status block index must be read before reading the KCQ */
  2592. rmb();
  2593. }
  2594. return last_status;
  2595. }
  2596. static void cnic_service_bnx2x_bh(unsigned long data)
  2597. {
  2598. struct cnic_dev *dev = (struct cnic_dev *) data;
  2599. struct cnic_local *cp = dev->cnic_priv;
  2600. struct bnx2x *bp = netdev_priv(dev->netdev);
  2601. u32 status_idx, new_status_idx;
  2602. if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
  2603. return;
  2604. while (1) {
  2605. status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
  2606. CNIC_WR16(dev, cp->kcq1.io_addr,
  2607. cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
  2608. if (!CNIC_SUPPORTS_FCOE(bp)) {
  2609. cp->arm_int(dev, status_idx);
  2610. break;
  2611. }
  2612. new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
  2613. if (new_status_idx != status_idx)
  2614. continue;
  2615. CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
  2616. MAX_KCQ_IDX);
  2617. cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
  2618. status_idx, IGU_INT_ENABLE, 1);
  2619. break;
  2620. }
  2621. }
  2622. static int cnic_service_bnx2x(void *data, void *status_blk)
  2623. {
  2624. struct cnic_dev *dev = data;
  2625. struct cnic_local *cp = dev->cnic_priv;
  2626. if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
  2627. cnic_doirq(dev);
  2628. cnic_chk_pkt_rings(cp);
  2629. return 0;
  2630. }
  2631. static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
  2632. {
  2633. struct cnic_ulp_ops *ulp_ops;
  2634. if (if_type == CNIC_ULP_ISCSI)
  2635. cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
  2636. mutex_lock(&cnic_lock);
  2637. ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
  2638. lockdep_is_held(&cnic_lock));
  2639. if (!ulp_ops) {
  2640. mutex_unlock(&cnic_lock);
  2641. return;
  2642. }
  2643. set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  2644. mutex_unlock(&cnic_lock);
  2645. if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
  2646. ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
  2647. clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  2648. }
  2649. static void cnic_ulp_stop(struct cnic_dev *dev)
  2650. {
  2651. struct cnic_local *cp = dev->cnic_priv;
  2652. int if_type;
  2653. for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
  2654. cnic_ulp_stop_one(cp, if_type);
  2655. }
  2656. static void cnic_ulp_start(struct cnic_dev *dev)
  2657. {
  2658. struct cnic_local *cp = dev->cnic_priv;
  2659. int if_type;
  2660. for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
  2661. struct cnic_ulp_ops *ulp_ops;
  2662. mutex_lock(&cnic_lock);
  2663. ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
  2664. lockdep_is_held(&cnic_lock));
  2665. if (!ulp_ops || !ulp_ops->cnic_start) {
  2666. mutex_unlock(&cnic_lock);
  2667. continue;
  2668. }
  2669. set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  2670. mutex_unlock(&cnic_lock);
  2671. if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
  2672. ulp_ops->cnic_start(cp->ulp_handle[if_type]);
  2673. clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  2674. }
  2675. }
  2676. static int cnic_copy_ulp_stats(struct cnic_dev *dev, int ulp_type)
  2677. {
  2678. struct cnic_local *cp = dev->cnic_priv;
  2679. struct cnic_ulp_ops *ulp_ops;
  2680. int rc;
  2681. mutex_lock(&cnic_lock);
  2682. ulp_ops = rcu_dereference_protected(cp->ulp_ops[ulp_type],
  2683. lockdep_is_held(&cnic_lock));
  2684. if (ulp_ops && ulp_ops->cnic_get_stats)
  2685. rc = ulp_ops->cnic_get_stats(cp->ulp_handle[ulp_type]);
  2686. else
  2687. rc = -ENODEV;
  2688. mutex_unlock(&cnic_lock);
  2689. return rc;
  2690. }
  2691. static int cnic_ctl(void *data, struct cnic_ctl_info *info)
  2692. {
  2693. struct cnic_dev *dev = data;
  2694. int ulp_type = CNIC_ULP_ISCSI;
  2695. switch (info->cmd) {
  2696. case CNIC_CTL_STOP_CMD:
  2697. cnic_hold(dev);
  2698. cnic_ulp_stop(dev);
  2699. cnic_stop_hw(dev);
  2700. cnic_put(dev);
  2701. break;
  2702. case CNIC_CTL_START_CMD:
  2703. cnic_hold(dev);
  2704. if (!cnic_start_hw(dev))
  2705. cnic_ulp_start(dev);
  2706. cnic_put(dev);
  2707. break;
  2708. case CNIC_CTL_STOP_ISCSI_CMD: {
  2709. struct cnic_local *cp = dev->cnic_priv;
  2710. set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
  2711. queue_delayed_work(cnic_wq, &cp->delete_task, 0);
  2712. break;
  2713. }
  2714. case CNIC_CTL_COMPLETION_CMD: {
  2715. struct cnic_ctl_completion *comp = &info->data.comp;
  2716. u32 cid = BNX2X_SW_CID(comp->cid);
  2717. u32 l5_cid;
  2718. struct cnic_local *cp = dev->cnic_priv;
  2719. if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
  2720. break;
  2721. if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
  2722. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  2723. if (unlikely(comp->error)) {
  2724. set_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags);
  2725. netdev_err(dev->netdev,
  2726. "CID %x CFC delete comp error %x\n",
  2727. cid, comp->error);
  2728. }
  2729. ctx->wait_cond = 1;
  2730. wake_up(&ctx->waitq);
  2731. }
  2732. break;
  2733. }
  2734. case CNIC_CTL_FCOE_STATS_GET_CMD:
  2735. ulp_type = CNIC_ULP_FCOE;
  2736. /* fall through */
  2737. case CNIC_CTL_ISCSI_STATS_GET_CMD:
  2738. cnic_hold(dev);
  2739. cnic_copy_ulp_stats(dev, ulp_type);
  2740. cnic_put(dev);
  2741. break;
  2742. default:
  2743. return -EINVAL;
  2744. }
  2745. return 0;
  2746. }
  2747. static void cnic_ulp_init(struct cnic_dev *dev)
  2748. {
  2749. int i;
  2750. struct cnic_local *cp = dev->cnic_priv;
  2751. for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
  2752. struct cnic_ulp_ops *ulp_ops;
  2753. mutex_lock(&cnic_lock);
  2754. ulp_ops = cnic_ulp_tbl_prot(i);
  2755. if (!ulp_ops || !ulp_ops->cnic_init) {
  2756. mutex_unlock(&cnic_lock);
  2757. continue;
  2758. }
  2759. ulp_get(ulp_ops);
  2760. mutex_unlock(&cnic_lock);
  2761. if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
  2762. ulp_ops->cnic_init(dev);
  2763. ulp_put(ulp_ops);
  2764. }
  2765. }
  2766. static void cnic_ulp_exit(struct cnic_dev *dev)
  2767. {
  2768. int i;
  2769. struct cnic_local *cp = dev->cnic_priv;
  2770. for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
  2771. struct cnic_ulp_ops *ulp_ops;
  2772. mutex_lock(&cnic_lock);
  2773. ulp_ops = cnic_ulp_tbl_prot(i);
  2774. if (!ulp_ops || !ulp_ops->cnic_exit) {
  2775. mutex_unlock(&cnic_lock);
  2776. continue;
  2777. }
  2778. ulp_get(ulp_ops);
  2779. mutex_unlock(&cnic_lock);
  2780. if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
  2781. ulp_ops->cnic_exit(dev);
  2782. ulp_put(ulp_ops);
  2783. }
  2784. }
  2785. static int cnic_cm_offload_pg(struct cnic_sock *csk)
  2786. {
  2787. struct cnic_dev *dev = csk->dev;
  2788. struct l4_kwq_offload_pg *l4kwqe;
  2789. struct kwqe *wqes[1];
  2790. l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
  2791. memset(l4kwqe, 0, sizeof(*l4kwqe));
  2792. wqes[0] = (struct kwqe *) l4kwqe;
  2793. l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
  2794. l4kwqe->flags =
  2795. L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
  2796. l4kwqe->l2hdr_nbytes = ETH_HLEN;
  2797. l4kwqe->da0 = csk->ha[0];
  2798. l4kwqe->da1 = csk->ha[1];
  2799. l4kwqe->da2 = csk->ha[2];
  2800. l4kwqe->da3 = csk->ha[3];
  2801. l4kwqe->da4 = csk->ha[4];
  2802. l4kwqe->da5 = csk->ha[5];
  2803. l4kwqe->sa0 = dev->mac_addr[0];
  2804. l4kwqe->sa1 = dev->mac_addr[1];
  2805. l4kwqe->sa2 = dev->mac_addr[2];
  2806. l4kwqe->sa3 = dev->mac_addr[3];
  2807. l4kwqe->sa4 = dev->mac_addr[4];
  2808. l4kwqe->sa5 = dev->mac_addr[5];
  2809. l4kwqe->etype = ETH_P_IP;
  2810. l4kwqe->ipid_start = DEF_IPID_START;
  2811. l4kwqe->host_opaque = csk->l5_cid;
  2812. if (csk->vlan_id) {
  2813. l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
  2814. l4kwqe->vlan_tag = csk->vlan_id;
  2815. l4kwqe->l2hdr_nbytes += 4;
  2816. }
  2817. return dev->submit_kwqes(dev, wqes, 1);
  2818. }
  2819. static int cnic_cm_update_pg(struct cnic_sock *csk)
  2820. {
  2821. struct cnic_dev *dev = csk->dev;
  2822. struct l4_kwq_update_pg *l4kwqe;
  2823. struct kwqe *wqes[1];
  2824. l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
  2825. memset(l4kwqe, 0, sizeof(*l4kwqe));
  2826. wqes[0] = (struct kwqe *) l4kwqe;
  2827. l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
  2828. l4kwqe->flags =
  2829. L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
  2830. l4kwqe->pg_cid = csk->pg_cid;
  2831. l4kwqe->da0 = csk->ha[0];
  2832. l4kwqe->da1 = csk->ha[1];
  2833. l4kwqe->da2 = csk->ha[2];
  2834. l4kwqe->da3 = csk->ha[3];
  2835. l4kwqe->da4 = csk->ha[4];
  2836. l4kwqe->da5 = csk->ha[5];
  2837. l4kwqe->pg_host_opaque = csk->l5_cid;
  2838. l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
  2839. return dev->submit_kwqes(dev, wqes, 1);
  2840. }
  2841. static int cnic_cm_upload_pg(struct cnic_sock *csk)
  2842. {
  2843. struct cnic_dev *dev = csk->dev;
  2844. struct l4_kwq_upload *l4kwqe;
  2845. struct kwqe *wqes[1];
  2846. l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
  2847. memset(l4kwqe, 0, sizeof(*l4kwqe));
  2848. wqes[0] = (struct kwqe *) l4kwqe;
  2849. l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
  2850. l4kwqe->flags =
  2851. L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
  2852. l4kwqe->cid = csk->pg_cid;
  2853. return dev->submit_kwqes(dev, wqes, 1);
  2854. }
  2855. static int cnic_cm_conn_req(struct cnic_sock *csk)
  2856. {
  2857. struct cnic_dev *dev = csk->dev;
  2858. struct l4_kwq_connect_req1 *l4kwqe1;
  2859. struct l4_kwq_connect_req2 *l4kwqe2;
  2860. struct l4_kwq_connect_req3 *l4kwqe3;
  2861. struct kwqe *wqes[3];
  2862. u8 tcp_flags = 0;
  2863. int num_wqes = 2;
  2864. l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
  2865. l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
  2866. l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
  2867. memset(l4kwqe1, 0, sizeof(*l4kwqe1));
  2868. memset(l4kwqe2, 0, sizeof(*l4kwqe2));
  2869. memset(l4kwqe3, 0, sizeof(*l4kwqe3));
  2870. l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
  2871. l4kwqe3->flags =
  2872. L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
  2873. l4kwqe3->ka_timeout = csk->ka_timeout;
  2874. l4kwqe3->ka_interval = csk->ka_interval;
  2875. l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
  2876. l4kwqe3->tos = csk->tos;
  2877. l4kwqe3->ttl = csk->ttl;
  2878. l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
  2879. l4kwqe3->pmtu = csk->mtu;
  2880. l4kwqe3->rcv_buf = csk->rcv_buf;
  2881. l4kwqe3->snd_buf = csk->snd_buf;
  2882. l4kwqe3->seed = csk->seed;
  2883. wqes[0] = (struct kwqe *) l4kwqe1;
  2884. if (test_bit(SK_F_IPV6, &csk->flags)) {
  2885. wqes[1] = (struct kwqe *) l4kwqe2;
  2886. wqes[2] = (struct kwqe *) l4kwqe3;
  2887. num_wqes = 3;
  2888. l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
  2889. l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
  2890. l4kwqe2->flags =
  2891. L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
  2892. L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
  2893. l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
  2894. l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
  2895. l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
  2896. l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
  2897. l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
  2898. l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
  2899. l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
  2900. sizeof(struct tcphdr);
  2901. } else {
  2902. wqes[1] = (struct kwqe *) l4kwqe3;
  2903. l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
  2904. sizeof(struct tcphdr);
  2905. }
  2906. l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
  2907. l4kwqe1->flags =
  2908. (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
  2909. L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
  2910. l4kwqe1->cid = csk->cid;
  2911. l4kwqe1->pg_cid = csk->pg_cid;
  2912. l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
  2913. l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
  2914. l4kwqe1->src_port = be16_to_cpu(csk->src_port);
  2915. l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
  2916. if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
  2917. tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
  2918. if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
  2919. tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
  2920. if (csk->tcp_flags & SK_TCP_NAGLE)
  2921. tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
  2922. if (csk->tcp_flags & SK_TCP_TIMESTAMP)
  2923. tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
  2924. if (csk->tcp_flags & SK_TCP_SACK)
  2925. tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
  2926. if (csk->tcp_flags & SK_TCP_SEG_SCALING)
  2927. tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
  2928. l4kwqe1->tcp_flags = tcp_flags;
  2929. return dev->submit_kwqes(dev, wqes, num_wqes);
  2930. }
  2931. static int cnic_cm_close_req(struct cnic_sock *csk)
  2932. {
  2933. struct cnic_dev *dev = csk->dev;
  2934. struct l4_kwq_close_req *l4kwqe;
  2935. struct kwqe *wqes[1];
  2936. l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
  2937. memset(l4kwqe, 0, sizeof(*l4kwqe));
  2938. wqes[0] = (struct kwqe *) l4kwqe;
  2939. l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
  2940. l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
  2941. l4kwqe->cid = csk->cid;
  2942. return dev->submit_kwqes(dev, wqes, 1);
  2943. }
  2944. static int cnic_cm_abort_req(struct cnic_sock *csk)
  2945. {
  2946. struct cnic_dev *dev = csk->dev;
  2947. struct l4_kwq_reset_req *l4kwqe;
  2948. struct kwqe *wqes[1];
  2949. l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
  2950. memset(l4kwqe, 0, sizeof(*l4kwqe));
  2951. wqes[0] = (struct kwqe *) l4kwqe;
  2952. l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
  2953. l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
  2954. l4kwqe->cid = csk->cid;
  2955. return dev->submit_kwqes(dev, wqes, 1);
  2956. }
  2957. static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
  2958. u32 l5_cid, struct cnic_sock **csk, void *context)
  2959. {
  2960. struct cnic_local *cp = dev->cnic_priv;
  2961. struct cnic_sock *csk1;
  2962. if (l5_cid >= MAX_CM_SK_TBL_SZ)
  2963. return -EINVAL;
  2964. if (cp->ctx_tbl) {
  2965. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  2966. if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
  2967. return -EAGAIN;
  2968. }
  2969. csk1 = &cp->csk_tbl[l5_cid];
  2970. if (atomic_read(&csk1->ref_count))
  2971. return -EAGAIN;
  2972. if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
  2973. return -EBUSY;
  2974. csk1->dev = dev;
  2975. csk1->cid = cid;
  2976. csk1->l5_cid = l5_cid;
  2977. csk1->ulp_type = ulp_type;
  2978. csk1->context = context;
  2979. csk1->ka_timeout = DEF_KA_TIMEOUT;
  2980. csk1->ka_interval = DEF_KA_INTERVAL;
  2981. csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
  2982. csk1->tos = DEF_TOS;
  2983. csk1->ttl = DEF_TTL;
  2984. csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
  2985. csk1->rcv_buf = DEF_RCV_BUF;
  2986. csk1->snd_buf = DEF_SND_BUF;
  2987. csk1->seed = DEF_SEED;
  2988. csk1->tcp_flags = 0;
  2989. *csk = csk1;
  2990. return 0;
  2991. }
  2992. static void cnic_cm_cleanup(struct cnic_sock *csk)
  2993. {
  2994. if (csk->src_port) {
  2995. struct cnic_dev *dev = csk->dev;
  2996. struct cnic_local *cp = dev->cnic_priv;
  2997. cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
  2998. csk->src_port = 0;
  2999. }
  3000. }
  3001. static void cnic_close_conn(struct cnic_sock *csk)
  3002. {
  3003. if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
  3004. cnic_cm_upload_pg(csk);
  3005. clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
  3006. }
  3007. cnic_cm_cleanup(csk);
  3008. }
  3009. static int cnic_cm_destroy(struct cnic_sock *csk)
  3010. {
  3011. if (!cnic_in_use(csk))
  3012. return -EINVAL;
  3013. csk_hold(csk);
  3014. clear_bit(SK_F_INUSE, &csk->flags);
  3015. smp_mb__after_atomic();
  3016. while (atomic_read(&csk->ref_count) != 1)
  3017. msleep(1);
  3018. cnic_cm_cleanup(csk);
  3019. csk->flags = 0;
  3020. csk_put(csk);
  3021. return 0;
  3022. }
  3023. static inline u16 cnic_get_vlan(struct net_device *dev,
  3024. struct net_device **vlan_dev)
  3025. {
  3026. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  3027. *vlan_dev = vlan_dev_real_dev(dev);
  3028. return vlan_dev_vlan_id(dev);
  3029. }
  3030. *vlan_dev = dev;
  3031. return 0;
  3032. }
  3033. static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
  3034. struct dst_entry **dst)
  3035. {
  3036. #if defined(CONFIG_INET)
  3037. struct rtable *rt;
  3038. rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
  3039. if (!IS_ERR(rt)) {
  3040. *dst = &rt->dst;
  3041. return 0;
  3042. }
  3043. return PTR_ERR(rt);
  3044. #else
  3045. return -ENETUNREACH;
  3046. #endif
  3047. }
  3048. static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
  3049. struct dst_entry **dst)
  3050. {
  3051. #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
  3052. struct flowi6 fl6;
  3053. memset(&fl6, 0, sizeof(fl6));
  3054. fl6.daddr = dst_addr->sin6_addr;
  3055. if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
  3056. fl6.flowi6_oif = dst_addr->sin6_scope_id;
  3057. *dst = ip6_route_output(&init_net, NULL, &fl6);
  3058. if ((*dst)->error) {
  3059. dst_release(*dst);
  3060. *dst = NULL;
  3061. return -ENETUNREACH;
  3062. } else
  3063. return 0;
  3064. #endif
  3065. return -ENETUNREACH;
  3066. }
  3067. static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
  3068. int ulp_type)
  3069. {
  3070. struct cnic_dev *dev = NULL;
  3071. struct dst_entry *dst;
  3072. struct net_device *netdev = NULL;
  3073. int err = -ENETUNREACH;
  3074. if (dst_addr->sin_family == AF_INET)
  3075. err = cnic_get_v4_route(dst_addr, &dst);
  3076. else if (dst_addr->sin_family == AF_INET6) {
  3077. struct sockaddr_in6 *dst_addr6 =
  3078. (struct sockaddr_in6 *) dst_addr;
  3079. err = cnic_get_v6_route(dst_addr6, &dst);
  3080. } else
  3081. return NULL;
  3082. if (err)
  3083. return NULL;
  3084. if (!dst->dev)
  3085. goto done;
  3086. cnic_get_vlan(dst->dev, &netdev);
  3087. dev = cnic_from_netdev(netdev);
  3088. done:
  3089. dst_release(dst);
  3090. if (dev)
  3091. cnic_put(dev);
  3092. return dev;
  3093. }
  3094. static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
  3095. {
  3096. struct cnic_dev *dev = csk->dev;
  3097. struct cnic_local *cp = dev->cnic_priv;
  3098. return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
  3099. }
  3100. static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
  3101. {
  3102. struct cnic_dev *dev = csk->dev;
  3103. struct cnic_local *cp = dev->cnic_priv;
  3104. int is_v6, rc = 0;
  3105. struct dst_entry *dst = NULL;
  3106. struct net_device *realdev;
  3107. __be16 local_port;
  3108. u32 port_id;
  3109. if (saddr->local.v6.sin6_family == AF_INET6 &&
  3110. saddr->remote.v6.sin6_family == AF_INET6)
  3111. is_v6 = 1;
  3112. else if (saddr->local.v4.sin_family == AF_INET &&
  3113. saddr->remote.v4.sin_family == AF_INET)
  3114. is_v6 = 0;
  3115. else
  3116. return -EINVAL;
  3117. clear_bit(SK_F_IPV6, &csk->flags);
  3118. if (is_v6) {
  3119. set_bit(SK_F_IPV6, &csk->flags);
  3120. cnic_get_v6_route(&saddr->remote.v6, &dst);
  3121. memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
  3122. sizeof(struct in6_addr));
  3123. csk->dst_port = saddr->remote.v6.sin6_port;
  3124. local_port = saddr->local.v6.sin6_port;
  3125. } else {
  3126. cnic_get_v4_route(&saddr->remote.v4, &dst);
  3127. csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
  3128. csk->dst_port = saddr->remote.v4.sin_port;
  3129. local_port = saddr->local.v4.sin_port;
  3130. }
  3131. csk->vlan_id = 0;
  3132. csk->mtu = dev->netdev->mtu;
  3133. if (dst && dst->dev) {
  3134. u16 vlan = cnic_get_vlan(dst->dev, &realdev);
  3135. if (realdev == dev->netdev) {
  3136. csk->vlan_id = vlan;
  3137. csk->mtu = dst_mtu(dst);
  3138. }
  3139. }
  3140. port_id = be16_to_cpu(local_port);
  3141. if (port_id >= CNIC_LOCAL_PORT_MIN &&
  3142. port_id < CNIC_LOCAL_PORT_MAX) {
  3143. if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
  3144. port_id = 0;
  3145. } else
  3146. port_id = 0;
  3147. if (!port_id) {
  3148. port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
  3149. if (port_id == -1) {
  3150. rc = -ENOMEM;
  3151. goto err_out;
  3152. }
  3153. local_port = cpu_to_be16(port_id);
  3154. }
  3155. csk->src_port = local_port;
  3156. err_out:
  3157. dst_release(dst);
  3158. return rc;
  3159. }
  3160. static void cnic_init_csk_state(struct cnic_sock *csk)
  3161. {
  3162. csk->state = 0;
  3163. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  3164. clear_bit(SK_F_CLOSING, &csk->flags);
  3165. }
  3166. static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
  3167. {
  3168. struct cnic_local *cp = csk->dev->cnic_priv;
  3169. int err = 0;
  3170. if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
  3171. return -EOPNOTSUPP;
  3172. if (!cnic_in_use(csk))
  3173. return -EINVAL;
  3174. if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
  3175. return -EINVAL;
  3176. cnic_init_csk_state(csk);
  3177. err = cnic_get_route(csk, saddr);
  3178. if (err)
  3179. goto err_out;
  3180. err = cnic_resolve_addr(csk, saddr);
  3181. if (!err)
  3182. return 0;
  3183. err_out:
  3184. clear_bit(SK_F_CONNECT_START, &csk->flags);
  3185. return err;
  3186. }
  3187. static int cnic_cm_abort(struct cnic_sock *csk)
  3188. {
  3189. struct cnic_local *cp = csk->dev->cnic_priv;
  3190. u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
  3191. if (!cnic_in_use(csk))
  3192. return -EINVAL;
  3193. if (cnic_abort_prep(csk))
  3194. return cnic_cm_abort_req(csk);
  3195. /* Getting here means that we haven't started connect, or
  3196. * connect was not successful, or it has been reset by the target.
  3197. */
  3198. cp->close_conn(csk, opcode);
  3199. if (csk->state != opcode) {
  3200. /* Wait for remote reset sequence to complete */
  3201. while (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
  3202. msleep(1);
  3203. return -EALREADY;
  3204. }
  3205. return 0;
  3206. }
  3207. static int cnic_cm_close(struct cnic_sock *csk)
  3208. {
  3209. if (!cnic_in_use(csk))
  3210. return -EINVAL;
  3211. if (cnic_close_prep(csk)) {
  3212. csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
  3213. return cnic_cm_close_req(csk);
  3214. } else {
  3215. /* Wait for remote reset sequence to complete */
  3216. while (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
  3217. msleep(1);
  3218. return -EALREADY;
  3219. }
  3220. return 0;
  3221. }
  3222. static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
  3223. u8 opcode)
  3224. {
  3225. struct cnic_ulp_ops *ulp_ops;
  3226. int ulp_type = csk->ulp_type;
  3227. rcu_read_lock();
  3228. ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
  3229. if (ulp_ops) {
  3230. if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
  3231. ulp_ops->cm_connect_complete(csk);
  3232. else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
  3233. ulp_ops->cm_close_complete(csk);
  3234. else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
  3235. ulp_ops->cm_remote_abort(csk);
  3236. else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
  3237. ulp_ops->cm_abort_complete(csk);
  3238. else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
  3239. ulp_ops->cm_remote_close(csk);
  3240. }
  3241. rcu_read_unlock();
  3242. }
  3243. static int cnic_cm_set_pg(struct cnic_sock *csk)
  3244. {
  3245. if (cnic_offld_prep(csk)) {
  3246. if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
  3247. cnic_cm_update_pg(csk);
  3248. else
  3249. cnic_cm_offload_pg(csk);
  3250. }
  3251. return 0;
  3252. }
  3253. static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
  3254. {
  3255. struct cnic_local *cp = dev->cnic_priv;
  3256. u32 l5_cid = kcqe->pg_host_opaque;
  3257. u8 opcode = kcqe->op_code;
  3258. struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
  3259. csk_hold(csk);
  3260. if (!cnic_in_use(csk))
  3261. goto done;
  3262. if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
  3263. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  3264. goto done;
  3265. }
  3266. /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
  3267. if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
  3268. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  3269. cnic_cm_upcall(cp, csk,
  3270. L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
  3271. goto done;
  3272. }
  3273. csk->pg_cid = kcqe->pg_cid;
  3274. set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
  3275. cnic_cm_conn_req(csk);
  3276. done:
  3277. csk_put(csk);
  3278. }
  3279. static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
  3280. {
  3281. struct cnic_local *cp = dev->cnic_priv;
  3282. struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
  3283. u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
  3284. struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
  3285. ctx->timestamp = jiffies;
  3286. ctx->wait_cond = 1;
  3287. wake_up(&ctx->waitq);
  3288. }
  3289. static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
  3290. {
  3291. struct cnic_local *cp = dev->cnic_priv;
  3292. struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
  3293. u8 opcode = l4kcqe->op_code;
  3294. u32 l5_cid;
  3295. struct cnic_sock *csk;
  3296. if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
  3297. cnic_process_fcoe_term_conn(dev, kcqe);
  3298. return;
  3299. }
  3300. if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
  3301. opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
  3302. cnic_cm_process_offld_pg(dev, l4kcqe);
  3303. return;
  3304. }
  3305. l5_cid = l4kcqe->conn_id;
  3306. if (opcode & 0x80)
  3307. l5_cid = l4kcqe->cid;
  3308. if (l5_cid >= MAX_CM_SK_TBL_SZ)
  3309. return;
  3310. csk = &cp->csk_tbl[l5_cid];
  3311. csk_hold(csk);
  3312. if (!cnic_in_use(csk)) {
  3313. csk_put(csk);
  3314. return;
  3315. }
  3316. switch (opcode) {
  3317. case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
  3318. if (l4kcqe->status != 0) {
  3319. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  3320. cnic_cm_upcall(cp, csk,
  3321. L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
  3322. }
  3323. break;
  3324. case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
  3325. if (l4kcqe->status == 0)
  3326. set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
  3327. else if (l4kcqe->status ==
  3328. L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
  3329. set_bit(SK_F_HW_ERR, &csk->flags);
  3330. smp_mb__before_atomic();
  3331. clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
  3332. cnic_cm_upcall(cp, csk, opcode);
  3333. break;
  3334. case L5CM_RAMROD_CMD_ID_CLOSE: {
  3335. struct iscsi_kcqe *l5kcqe = (struct iscsi_kcqe *) kcqe;
  3336. if (l4kcqe->status != 0 || l5kcqe->completion_status != 0) {
  3337. netdev_warn(dev->netdev, "RAMROD CLOSE compl with status 0x%x completion status 0x%x\n",
  3338. l4kcqe->status, l5kcqe->completion_status);
  3339. opcode = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
  3340. /* Fall through */
  3341. } else {
  3342. break;
  3343. }
  3344. }
  3345. case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
  3346. case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
  3347. case L4_KCQE_OPCODE_VALUE_RESET_COMP:
  3348. case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
  3349. case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
  3350. if (l4kcqe->status == L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
  3351. set_bit(SK_F_HW_ERR, &csk->flags);
  3352. cp->close_conn(csk, opcode);
  3353. break;
  3354. case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
  3355. /* after we already sent CLOSE_REQ */
  3356. if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
  3357. !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
  3358. csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
  3359. cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
  3360. else
  3361. cnic_cm_upcall(cp, csk, opcode);
  3362. break;
  3363. }
  3364. csk_put(csk);
  3365. }
  3366. static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
  3367. {
  3368. struct cnic_dev *dev = data;
  3369. int i;
  3370. for (i = 0; i < num; i++)
  3371. cnic_cm_process_kcqe(dev, kcqe[i]);
  3372. }
  3373. static struct cnic_ulp_ops cm_ulp_ops = {
  3374. .indicate_kcqes = cnic_cm_indicate_kcqe,
  3375. };
  3376. static void cnic_cm_free_mem(struct cnic_dev *dev)
  3377. {
  3378. struct cnic_local *cp = dev->cnic_priv;
  3379. kfree(cp->csk_tbl);
  3380. cp->csk_tbl = NULL;
  3381. cnic_free_id_tbl(&cp->csk_port_tbl);
  3382. }
  3383. static int cnic_cm_alloc_mem(struct cnic_dev *dev)
  3384. {
  3385. struct cnic_local *cp = dev->cnic_priv;
  3386. u32 port_id;
  3387. cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
  3388. GFP_KERNEL);
  3389. if (!cp->csk_tbl)
  3390. return -ENOMEM;
  3391. port_id = prandom_u32();
  3392. port_id %= CNIC_LOCAL_PORT_RANGE;
  3393. if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
  3394. CNIC_LOCAL_PORT_MIN, port_id)) {
  3395. cnic_cm_free_mem(dev);
  3396. return -ENOMEM;
  3397. }
  3398. return 0;
  3399. }
  3400. static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
  3401. {
  3402. if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
  3403. /* Unsolicited RESET_COMP or RESET_RECEIVED */
  3404. opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
  3405. csk->state = opcode;
  3406. }
  3407. /* 1. If event opcode matches the expected event in csk->state
  3408. * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
  3409. * event
  3410. * 3. If the expected event is 0, meaning the connection was never
  3411. * never established, we accept the opcode from cm_abort.
  3412. */
  3413. if (opcode == csk->state || csk->state == 0 ||
  3414. csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
  3415. csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
  3416. if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
  3417. if (csk->state == 0)
  3418. csk->state = opcode;
  3419. return 1;
  3420. }
  3421. }
  3422. return 0;
  3423. }
  3424. static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
  3425. {
  3426. struct cnic_dev *dev = csk->dev;
  3427. struct cnic_local *cp = dev->cnic_priv;
  3428. if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
  3429. cnic_cm_upcall(cp, csk, opcode);
  3430. return;
  3431. }
  3432. clear_bit(SK_F_CONNECT_START, &csk->flags);
  3433. cnic_close_conn(csk);
  3434. csk->state = opcode;
  3435. cnic_cm_upcall(cp, csk, opcode);
  3436. }
  3437. static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
  3438. {
  3439. }
  3440. static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
  3441. {
  3442. u32 seed;
  3443. seed = prandom_u32();
  3444. cnic_ctx_wr(dev, 45, 0, seed);
  3445. return 0;
  3446. }
  3447. static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
  3448. {
  3449. struct cnic_dev *dev = csk->dev;
  3450. struct cnic_local *cp = dev->cnic_priv;
  3451. struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
  3452. union l5cm_specific_data l5_data;
  3453. u32 cmd = 0;
  3454. int close_complete = 0;
  3455. switch (opcode) {
  3456. case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
  3457. case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
  3458. case L4_KCQE_OPCODE_VALUE_RESET_COMP:
  3459. if (cnic_ready_to_close(csk, opcode)) {
  3460. if (test_bit(SK_F_HW_ERR, &csk->flags))
  3461. close_complete = 1;
  3462. else if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
  3463. cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
  3464. else
  3465. close_complete = 1;
  3466. }
  3467. break;
  3468. case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
  3469. cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
  3470. break;
  3471. case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
  3472. close_complete = 1;
  3473. break;
  3474. }
  3475. if (cmd) {
  3476. memset(&l5_data, 0, sizeof(l5_data));
  3477. cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
  3478. &l5_data);
  3479. } else if (close_complete) {
  3480. ctx->timestamp = jiffies;
  3481. cnic_close_conn(csk);
  3482. cnic_cm_upcall(cp, csk, csk->state);
  3483. }
  3484. }
  3485. static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
  3486. {
  3487. struct cnic_local *cp = dev->cnic_priv;
  3488. if (!cp->ctx_tbl)
  3489. return;
  3490. if (!netif_running(dev->netdev))
  3491. return;
  3492. cnic_bnx2x_delete_wait(dev, 0);
  3493. cancel_delayed_work(&cp->delete_task);
  3494. flush_workqueue(cnic_wq);
  3495. if (atomic_read(&cp->iscsi_conn) != 0)
  3496. netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
  3497. atomic_read(&cp->iscsi_conn));
  3498. }
  3499. static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
  3500. {
  3501. struct bnx2x *bp = netdev_priv(dev->netdev);
  3502. u32 pfid = bp->pfid;
  3503. u32 port = BP_PORT(bp);
  3504. cnic_init_bnx2x_mac(dev);
  3505. cnic_bnx2x_set_tcp_options(dev, 0, 1);
  3506. CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
  3507. XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
  3508. CNIC_WR(dev, BAR_XSTRORM_INTMEM +
  3509. XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
  3510. CNIC_WR(dev, BAR_XSTRORM_INTMEM +
  3511. XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
  3512. DEF_MAX_DA_COUNT);
  3513. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  3514. XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
  3515. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  3516. XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
  3517. CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
  3518. XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
  3519. CNIC_WR(dev, BAR_XSTRORM_INTMEM +
  3520. XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
  3521. CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
  3522. DEF_MAX_CWND);
  3523. return 0;
  3524. }
  3525. static void cnic_delete_task(struct work_struct *work)
  3526. {
  3527. struct cnic_local *cp;
  3528. struct cnic_dev *dev;
  3529. u32 i;
  3530. int need_resched = 0;
  3531. cp = container_of(work, struct cnic_local, delete_task.work);
  3532. dev = cp->dev;
  3533. if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
  3534. struct drv_ctl_info info;
  3535. cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
  3536. info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
  3537. cp->ethdev->drv_ctl(dev->netdev, &info);
  3538. }
  3539. for (i = 0; i < cp->max_cid_space; i++) {
  3540. struct cnic_context *ctx = &cp->ctx_tbl[i];
  3541. int err;
  3542. if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
  3543. !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
  3544. continue;
  3545. if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
  3546. need_resched = 1;
  3547. continue;
  3548. }
  3549. if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
  3550. continue;
  3551. err = cnic_bnx2x_destroy_ramrod(dev, i);
  3552. cnic_free_bnx2x_conn_resc(dev, i);
  3553. if (!err) {
  3554. if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
  3555. atomic_dec(&cp->iscsi_conn);
  3556. clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
  3557. }
  3558. }
  3559. if (need_resched)
  3560. queue_delayed_work(cnic_wq, &cp->delete_task,
  3561. msecs_to_jiffies(10));
  3562. }
  3563. static int cnic_cm_open(struct cnic_dev *dev)
  3564. {
  3565. struct cnic_local *cp = dev->cnic_priv;
  3566. int err;
  3567. err = cnic_cm_alloc_mem(dev);
  3568. if (err)
  3569. return err;
  3570. err = cp->start_cm(dev);
  3571. if (err)
  3572. goto err_out;
  3573. INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
  3574. dev->cm_create = cnic_cm_create;
  3575. dev->cm_destroy = cnic_cm_destroy;
  3576. dev->cm_connect = cnic_cm_connect;
  3577. dev->cm_abort = cnic_cm_abort;
  3578. dev->cm_close = cnic_cm_close;
  3579. dev->cm_select_dev = cnic_cm_select_dev;
  3580. cp->ulp_handle[CNIC_ULP_L4] = dev;
  3581. rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
  3582. return 0;
  3583. err_out:
  3584. cnic_cm_free_mem(dev);
  3585. return err;
  3586. }
  3587. static int cnic_cm_shutdown(struct cnic_dev *dev)
  3588. {
  3589. struct cnic_local *cp = dev->cnic_priv;
  3590. int i;
  3591. if (!cp->csk_tbl)
  3592. return 0;
  3593. for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
  3594. struct cnic_sock *csk = &cp->csk_tbl[i];
  3595. clear_bit(SK_F_INUSE, &csk->flags);
  3596. cnic_cm_cleanup(csk);
  3597. }
  3598. cnic_cm_free_mem(dev);
  3599. return 0;
  3600. }
  3601. static void cnic_init_context(struct cnic_dev *dev, u32 cid)
  3602. {
  3603. u32 cid_addr;
  3604. int i;
  3605. cid_addr = GET_CID_ADDR(cid);
  3606. for (i = 0; i < CTX_SIZE; i += 4)
  3607. cnic_ctx_wr(dev, cid_addr, i, 0);
  3608. }
  3609. static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
  3610. {
  3611. struct cnic_local *cp = dev->cnic_priv;
  3612. int ret = 0, i;
  3613. u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
  3614. if (BNX2_CHIP(cp) != BNX2_CHIP_5709)
  3615. return 0;
  3616. for (i = 0; i < cp->ctx_blks; i++) {
  3617. int j;
  3618. u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
  3619. u32 val;
  3620. memset(cp->ctx_arr[i].ctx, 0, CNIC_PAGE_SIZE);
  3621. CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
  3622. (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
  3623. CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
  3624. (u64) cp->ctx_arr[i].mapping >> 32);
  3625. CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
  3626. BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
  3627. for (j = 0; j < 10; j++) {
  3628. val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
  3629. if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
  3630. break;
  3631. udelay(5);
  3632. }
  3633. if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
  3634. ret = -EBUSY;
  3635. break;
  3636. }
  3637. }
  3638. return ret;
  3639. }
  3640. static void cnic_free_irq(struct cnic_dev *dev)
  3641. {
  3642. struct cnic_local *cp = dev->cnic_priv;
  3643. struct cnic_eth_dev *ethdev = cp->ethdev;
  3644. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  3645. cp->disable_int_sync(dev);
  3646. tasklet_kill(&cp->cnic_irq_task);
  3647. free_irq(ethdev->irq_arr[0].vector, dev);
  3648. }
  3649. }
  3650. static int cnic_request_irq(struct cnic_dev *dev)
  3651. {
  3652. struct cnic_local *cp = dev->cnic_priv;
  3653. struct cnic_eth_dev *ethdev = cp->ethdev;
  3654. int err;
  3655. err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
  3656. if (err)
  3657. tasklet_disable(&cp->cnic_irq_task);
  3658. return err;
  3659. }
  3660. static int cnic_init_bnx2_irq(struct cnic_dev *dev)
  3661. {
  3662. struct cnic_local *cp = dev->cnic_priv;
  3663. struct cnic_eth_dev *ethdev = cp->ethdev;
  3664. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  3665. int err, i = 0;
  3666. int sblk_num = cp->status_blk_num;
  3667. u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
  3668. BNX2_HC_SB_CONFIG_1;
  3669. CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
  3670. CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
  3671. CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
  3672. CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
  3673. cp->last_status_idx = cp->status_blk.bnx2->status_idx;
  3674. tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
  3675. (unsigned long) dev);
  3676. err = cnic_request_irq(dev);
  3677. if (err)
  3678. return err;
  3679. while (cp->status_blk.bnx2->status_completion_producer_index &&
  3680. i < 10) {
  3681. CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
  3682. 1 << (11 + sblk_num));
  3683. udelay(10);
  3684. i++;
  3685. barrier();
  3686. }
  3687. if (cp->status_blk.bnx2->status_completion_producer_index) {
  3688. cnic_free_irq(dev);
  3689. goto failed;
  3690. }
  3691. } else {
  3692. struct status_block *sblk = cp->status_blk.gen;
  3693. u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
  3694. int i = 0;
  3695. while (sblk->status_completion_producer_index && i < 10) {
  3696. CNIC_WR(dev, BNX2_HC_COMMAND,
  3697. hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
  3698. udelay(10);
  3699. i++;
  3700. barrier();
  3701. }
  3702. if (sblk->status_completion_producer_index)
  3703. goto failed;
  3704. }
  3705. return 0;
  3706. failed:
  3707. netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
  3708. return -EBUSY;
  3709. }
  3710. static void cnic_enable_bnx2_int(struct cnic_dev *dev)
  3711. {
  3712. struct cnic_local *cp = dev->cnic_priv;
  3713. struct cnic_eth_dev *ethdev = cp->ethdev;
  3714. if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
  3715. return;
  3716. CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
  3717. BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
  3718. }
  3719. static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
  3720. {
  3721. struct cnic_local *cp = dev->cnic_priv;
  3722. struct cnic_eth_dev *ethdev = cp->ethdev;
  3723. if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
  3724. return;
  3725. CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
  3726. BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
  3727. CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
  3728. synchronize_irq(ethdev->irq_arr[0].vector);
  3729. }
  3730. static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
  3731. {
  3732. struct cnic_local *cp = dev->cnic_priv;
  3733. struct cnic_eth_dev *ethdev = cp->ethdev;
  3734. struct cnic_uio_dev *udev = cp->udev;
  3735. u32 cid_addr, tx_cid, sb_id;
  3736. u32 val, offset0, offset1, offset2, offset3;
  3737. int i;
  3738. struct bnx2_tx_bd *txbd;
  3739. dma_addr_t buf_map, ring_map = udev->l2_ring_map;
  3740. struct status_block *s_blk = cp->status_blk.gen;
  3741. sb_id = cp->status_blk_num;
  3742. tx_cid = 20;
  3743. cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
  3744. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  3745. struct status_block_msix *sblk = cp->status_blk.bnx2;
  3746. tx_cid = TX_TSS_CID + sb_id - 1;
  3747. CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
  3748. (TX_TSS_CID << 7));
  3749. cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
  3750. }
  3751. cp->tx_cons = *cp->tx_cons_ptr;
  3752. cid_addr = GET_CID_ADDR(tx_cid);
  3753. if (BNX2_CHIP(cp) == BNX2_CHIP_5709) {
  3754. u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
  3755. for (i = 0; i < PHY_CTX_SIZE; i += 4)
  3756. cnic_ctx_wr(dev, cid_addr2, i, 0);
  3757. offset0 = BNX2_L2CTX_TYPE_XI;
  3758. offset1 = BNX2_L2CTX_CMD_TYPE_XI;
  3759. offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
  3760. offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
  3761. } else {
  3762. cnic_init_context(dev, tx_cid);
  3763. cnic_init_context(dev, tx_cid + 1);
  3764. offset0 = BNX2_L2CTX_TYPE;
  3765. offset1 = BNX2_L2CTX_CMD_TYPE;
  3766. offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
  3767. offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
  3768. }
  3769. val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
  3770. cnic_ctx_wr(dev, cid_addr, offset0, val);
  3771. val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
  3772. cnic_ctx_wr(dev, cid_addr, offset1, val);
  3773. txbd = udev->l2_ring;
  3774. buf_map = udev->l2_buf_map;
  3775. for (i = 0; i < BNX2_MAX_TX_DESC_CNT; i++, txbd++) {
  3776. txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
  3777. txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
  3778. }
  3779. val = (u64) ring_map >> 32;
  3780. cnic_ctx_wr(dev, cid_addr, offset2, val);
  3781. txbd->tx_bd_haddr_hi = val;
  3782. val = (u64) ring_map & 0xffffffff;
  3783. cnic_ctx_wr(dev, cid_addr, offset3, val);
  3784. txbd->tx_bd_haddr_lo = val;
  3785. }
  3786. static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
  3787. {
  3788. struct cnic_local *cp = dev->cnic_priv;
  3789. struct cnic_eth_dev *ethdev = cp->ethdev;
  3790. struct cnic_uio_dev *udev = cp->udev;
  3791. u32 cid_addr, sb_id, val, coal_reg, coal_val;
  3792. int i;
  3793. struct bnx2_rx_bd *rxbd;
  3794. struct status_block *s_blk = cp->status_blk.gen;
  3795. dma_addr_t ring_map = udev->l2_ring_map;
  3796. sb_id = cp->status_blk_num;
  3797. cnic_init_context(dev, 2);
  3798. cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
  3799. coal_reg = BNX2_HC_COMMAND;
  3800. coal_val = CNIC_RD(dev, coal_reg);
  3801. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  3802. struct status_block_msix *sblk = cp->status_blk.bnx2;
  3803. cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
  3804. coal_reg = BNX2_HC_COALESCE_NOW;
  3805. coal_val = 1 << (11 + sb_id);
  3806. }
  3807. i = 0;
  3808. while (!(*cp->rx_cons_ptr != 0) && i < 10) {
  3809. CNIC_WR(dev, coal_reg, coal_val);
  3810. udelay(10);
  3811. i++;
  3812. barrier();
  3813. }
  3814. cp->rx_cons = *cp->rx_cons_ptr;
  3815. cid_addr = GET_CID_ADDR(2);
  3816. val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
  3817. BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
  3818. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
  3819. if (sb_id == 0)
  3820. val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
  3821. else
  3822. val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
  3823. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
  3824. rxbd = udev->l2_ring + CNIC_PAGE_SIZE;
  3825. for (i = 0; i < BNX2_MAX_RX_DESC_CNT; i++, rxbd++) {
  3826. dma_addr_t buf_map;
  3827. int n = (i % cp->l2_rx_ring_size) + 1;
  3828. buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
  3829. rxbd->rx_bd_len = cp->l2_single_buf_size;
  3830. rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
  3831. rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
  3832. rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
  3833. }
  3834. val = (u64) (ring_map + CNIC_PAGE_SIZE) >> 32;
  3835. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
  3836. rxbd->rx_bd_haddr_hi = val;
  3837. val = (u64) (ring_map + CNIC_PAGE_SIZE) & 0xffffffff;
  3838. cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
  3839. rxbd->rx_bd_haddr_lo = val;
  3840. val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
  3841. cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
  3842. }
  3843. static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
  3844. {
  3845. struct kwqe *wqes[1], l2kwqe;
  3846. memset(&l2kwqe, 0, sizeof(l2kwqe));
  3847. wqes[0] = &l2kwqe;
  3848. l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
  3849. (L2_KWQE_OPCODE_VALUE_FLUSH <<
  3850. KWQE_OPCODE_SHIFT) | 2;
  3851. dev->submit_kwqes(dev, wqes, 1);
  3852. }
  3853. static void cnic_set_bnx2_mac(struct cnic_dev *dev)
  3854. {
  3855. struct cnic_local *cp = dev->cnic_priv;
  3856. u32 val;
  3857. val = cp->func << 2;
  3858. cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
  3859. val = cnic_reg_rd_ind(dev, cp->shmem_base +
  3860. BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
  3861. dev->mac_addr[0] = (u8) (val >> 8);
  3862. dev->mac_addr[1] = (u8) val;
  3863. CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
  3864. val = cnic_reg_rd_ind(dev, cp->shmem_base +
  3865. BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
  3866. dev->mac_addr[2] = (u8) (val >> 24);
  3867. dev->mac_addr[3] = (u8) (val >> 16);
  3868. dev->mac_addr[4] = (u8) (val >> 8);
  3869. dev->mac_addr[5] = (u8) val;
  3870. CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
  3871. val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
  3872. if (BNX2_CHIP(cp) != BNX2_CHIP_5709)
  3873. val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
  3874. CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
  3875. CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
  3876. CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
  3877. }
  3878. static int cnic_start_bnx2_hw(struct cnic_dev *dev)
  3879. {
  3880. struct cnic_local *cp = dev->cnic_priv;
  3881. struct cnic_eth_dev *ethdev = cp->ethdev;
  3882. struct status_block *sblk = cp->status_blk.gen;
  3883. u32 val, kcq_cid_addr, kwq_cid_addr;
  3884. int err;
  3885. cnic_set_bnx2_mac(dev);
  3886. val = CNIC_RD(dev, BNX2_MQ_CONFIG);
  3887. val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
  3888. if (CNIC_PAGE_BITS > 12)
  3889. val |= (12 - 8) << 4;
  3890. else
  3891. val |= (CNIC_PAGE_BITS - 8) << 4;
  3892. CNIC_WR(dev, BNX2_MQ_CONFIG, val);
  3893. CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
  3894. CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
  3895. CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
  3896. err = cnic_setup_5709_context(dev, 1);
  3897. if (err)
  3898. return err;
  3899. cnic_init_context(dev, KWQ_CID);
  3900. cnic_init_context(dev, KCQ_CID);
  3901. kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
  3902. cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
  3903. cp->max_kwq_idx = MAX_KWQ_IDX;
  3904. cp->kwq_prod_idx = 0;
  3905. cp->kwq_con_idx = 0;
  3906. set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
  3907. if (BNX2_CHIP(cp) == BNX2_CHIP_5706 || BNX2_CHIP(cp) == BNX2_CHIP_5708)
  3908. cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
  3909. else
  3910. cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
  3911. /* Initialize the kernel work queue context. */
  3912. val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
  3913. (CNIC_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
  3914. cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
  3915. val = (CNIC_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
  3916. cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
  3917. val = ((CNIC_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
  3918. cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
  3919. val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
  3920. cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
  3921. val = (u32) cp->kwq_info.pgtbl_map;
  3922. cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
  3923. kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
  3924. cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
  3925. cp->kcq1.sw_prod_idx = 0;
  3926. cp->kcq1.hw_prod_idx_ptr =
  3927. &sblk->status_completion_producer_index;
  3928. cp->kcq1.status_idx_ptr = &sblk->status_idx;
  3929. /* Initialize the kernel complete queue context. */
  3930. val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
  3931. (CNIC_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
  3932. cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
  3933. val = (CNIC_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
  3934. cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
  3935. val = ((CNIC_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
  3936. cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
  3937. val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
  3938. cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
  3939. val = (u32) cp->kcq1.dma.pgtbl_map;
  3940. cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
  3941. cp->int_num = 0;
  3942. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
  3943. struct status_block_msix *msblk = cp->status_blk.bnx2;
  3944. u32 sb_id = cp->status_blk_num;
  3945. u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
  3946. cp->kcq1.hw_prod_idx_ptr =
  3947. &msblk->status_completion_producer_index;
  3948. cp->kcq1.status_idx_ptr = &msblk->status_idx;
  3949. cp->kwq_con_idx_ptr = &msblk->status_cmd_consumer_index;
  3950. cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
  3951. cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
  3952. cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
  3953. }
  3954. /* Enable Commnad Scheduler notification when we write to the
  3955. * host producer index of the kernel contexts. */
  3956. CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
  3957. /* Enable Command Scheduler notification when we write to either
  3958. * the Send Queue or Receive Queue producer indexes of the kernel
  3959. * bypass contexts. */
  3960. CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
  3961. CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
  3962. /* Notify COM when the driver post an application buffer. */
  3963. CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
  3964. /* Set the CP and COM doorbells. These two processors polls the
  3965. * doorbell for a non zero value before running. This must be done
  3966. * after setting up the kernel queue contexts. */
  3967. cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
  3968. cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
  3969. cnic_init_bnx2_tx_ring(dev);
  3970. cnic_init_bnx2_rx_ring(dev);
  3971. err = cnic_init_bnx2_irq(dev);
  3972. if (err) {
  3973. netdev_err(dev->netdev, "cnic_init_irq failed\n");
  3974. cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
  3975. cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
  3976. return err;
  3977. }
  3978. ethdev->drv_state |= CNIC_DRV_STATE_HANDLES_IRQ;
  3979. return 0;
  3980. }
  3981. static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
  3982. {
  3983. struct cnic_local *cp = dev->cnic_priv;
  3984. struct cnic_eth_dev *ethdev = cp->ethdev;
  3985. u32 start_offset = ethdev->ctx_tbl_offset;
  3986. int i;
  3987. for (i = 0; i < cp->ctx_blks; i++) {
  3988. struct cnic_ctx *ctx = &cp->ctx_arr[i];
  3989. dma_addr_t map = ctx->mapping;
  3990. if (cp->ctx_align) {
  3991. unsigned long mask = cp->ctx_align - 1;
  3992. map = (map + mask) & ~mask;
  3993. }
  3994. cnic_ctx_tbl_wr(dev, start_offset + i, map);
  3995. }
  3996. }
  3997. static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
  3998. {
  3999. struct cnic_local *cp = dev->cnic_priv;
  4000. struct cnic_eth_dev *ethdev = cp->ethdev;
  4001. int err = 0;
  4002. tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
  4003. (unsigned long) dev);
  4004. if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
  4005. err = cnic_request_irq(dev);
  4006. return err;
  4007. }
  4008. static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
  4009. u16 sb_id, u8 sb_index,
  4010. u8 disable)
  4011. {
  4012. struct bnx2x *bp = netdev_priv(dev->netdev);
  4013. u32 addr = BAR_CSTRORM_INTMEM +
  4014. CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
  4015. offsetof(struct hc_status_block_data_e1x, index_data) +
  4016. sizeof(struct hc_index_data)*sb_index +
  4017. offsetof(struct hc_index_data, flags);
  4018. u16 flags = CNIC_RD16(dev, addr);
  4019. /* clear and set */
  4020. flags &= ~HC_INDEX_DATA_HC_ENABLED;
  4021. flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
  4022. HC_INDEX_DATA_HC_ENABLED);
  4023. CNIC_WR16(dev, addr, flags);
  4024. }
  4025. static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
  4026. {
  4027. struct cnic_local *cp = dev->cnic_priv;
  4028. struct bnx2x *bp = netdev_priv(dev->netdev);
  4029. u8 sb_id = cp->status_blk_num;
  4030. CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
  4031. CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
  4032. offsetof(struct hc_status_block_data_e1x, index_data) +
  4033. sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
  4034. offsetof(struct hc_index_data, timeout), 64 / 4);
  4035. cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
  4036. }
  4037. static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
  4038. {
  4039. }
  4040. static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
  4041. struct client_init_ramrod_data *data)
  4042. {
  4043. struct cnic_local *cp = dev->cnic_priv;
  4044. struct bnx2x *bp = netdev_priv(dev->netdev);
  4045. struct cnic_uio_dev *udev = cp->udev;
  4046. union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
  4047. dma_addr_t buf_map, ring_map = udev->l2_ring_map;
  4048. struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
  4049. int i;
  4050. u32 cli = cp->ethdev->iscsi_l2_client_id;
  4051. u32 val;
  4052. memset(txbd, 0, CNIC_PAGE_SIZE);
  4053. buf_map = udev->l2_buf_map;
  4054. for (i = 0; i < BNX2_MAX_TX_DESC_CNT; i += 3, txbd += 3) {
  4055. struct eth_tx_start_bd *start_bd = &txbd->start_bd;
  4056. struct eth_tx_parse_bd_e1x *pbd_e1x =
  4057. &((txbd + 1)->parse_bd_e1x);
  4058. struct eth_tx_parse_bd_e2 *pbd_e2 = &((txbd + 1)->parse_bd_e2);
  4059. struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
  4060. start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
  4061. start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
  4062. reg_bd->addr_hi = start_bd->addr_hi;
  4063. reg_bd->addr_lo = start_bd->addr_lo + 0x10;
  4064. start_bd->nbytes = cpu_to_le16(0x10);
  4065. start_bd->nbd = cpu_to_le16(3);
  4066. start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
  4067. start_bd->general_data &= ~ETH_TX_START_BD_PARSE_NBDS;
  4068. start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
  4069. if (BNX2X_CHIP_IS_E2_PLUS(bp))
  4070. pbd_e2->parsing_data = (UNICAST_ADDRESS <<
  4071. ETH_TX_PARSE_BD_E2_ETH_ADDR_TYPE_SHIFT);
  4072. else
  4073. pbd_e1x->global_data = (UNICAST_ADDRESS <<
  4074. ETH_TX_PARSE_BD_E1X_ETH_ADDR_TYPE_SHIFT);
  4075. }
  4076. val = (u64) ring_map >> 32;
  4077. txbd->next_bd.addr_hi = cpu_to_le32(val);
  4078. data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
  4079. val = (u64) ring_map & 0xffffffff;
  4080. txbd->next_bd.addr_lo = cpu_to_le32(val);
  4081. data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
  4082. /* Other ramrod params */
  4083. data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
  4084. data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
  4085. /* reset xstorm per client statistics */
  4086. if (cli < MAX_STAT_COUNTER_ID) {
  4087. data->general.statistics_zero_flg = 1;
  4088. data->general.statistics_en_flg = 1;
  4089. data->general.statistics_counter_id = cli;
  4090. }
  4091. cp->tx_cons_ptr =
  4092. &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
  4093. }
  4094. static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
  4095. struct client_init_ramrod_data *data)
  4096. {
  4097. struct cnic_local *cp = dev->cnic_priv;
  4098. struct bnx2x *bp = netdev_priv(dev->netdev);
  4099. struct cnic_uio_dev *udev = cp->udev;
  4100. struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
  4101. CNIC_PAGE_SIZE);
  4102. struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
  4103. (udev->l2_ring + (2 * CNIC_PAGE_SIZE));
  4104. struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
  4105. int i;
  4106. u32 cli = cp->ethdev->iscsi_l2_client_id;
  4107. int cl_qzone_id = BNX2X_CL_QZONE_ID(bp, cli);
  4108. u32 val;
  4109. dma_addr_t ring_map = udev->l2_ring_map;
  4110. /* General data */
  4111. data->general.client_id = cli;
  4112. data->general.activate_flg = 1;
  4113. data->general.sp_client_id = cli;
  4114. data->general.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
  4115. data->general.func_id = bp->pfid;
  4116. for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
  4117. dma_addr_t buf_map;
  4118. int n = (i % cp->l2_rx_ring_size) + 1;
  4119. buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
  4120. rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
  4121. rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
  4122. }
  4123. val = (u64) (ring_map + CNIC_PAGE_SIZE) >> 32;
  4124. rxbd->addr_hi = cpu_to_le32(val);
  4125. data->rx.bd_page_base.hi = cpu_to_le32(val);
  4126. val = (u64) (ring_map + CNIC_PAGE_SIZE) & 0xffffffff;
  4127. rxbd->addr_lo = cpu_to_le32(val);
  4128. data->rx.bd_page_base.lo = cpu_to_le32(val);
  4129. rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
  4130. val = (u64) (ring_map + (2 * CNIC_PAGE_SIZE)) >> 32;
  4131. rxcqe->addr_hi = cpu_to_le32(val);
  4132. data->rx.cqe_page_base.hi = cpu_to_le32(val);
  4133. val = (u64) (ring_map + (2 * CNIC_PAGE_SIZE)) & 0xffffffff;
  4134. rxcqe->addr_lo = cpu_to_le32(val);
  4135. data->rx.cqe_page_base.lo = cpu_to_le32(val);
  4136. /* Other ramrod params */
  4137. data->rx.client_qzone_id = cl_qzone_id;
  4138. data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
  4139. data->rx.status_block_id = BNX2X_DEF_SB_ID;
  4140. data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
  4141. data->rx.max_bytes_on_bd = cpu_to_le16(cp->l2_single_buf_size);
  4142. data->rx.outer_vlan_removal_enable_flg = 1;
  4143. data->rx.silent_vlan_removal_flg = 1;
  4144. data->rx.silent_vlan_value = 0;
  4145. data->rx.silent_vlan_mask = 0xffff;
  4146. cp->rx_cons_ptr =
  4147. &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
  4148. cp->rx_cons = *cp->rx_cons_ptr;
  4149. }
  4150. static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
  4151. {
  4152. struct cnic_local *cp = dev->cnic_priv;
  4153. struct bnx2x *bp = netdev_priv(dev->netdev);
  4154. u32 pfid = bp->pfid;
  4155. cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
  4156. CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
  4157. cp->kcq1.sw_prod_idx = 0;
  4158. if (BNX2X_CHIP_IS_E2_PLUS(bp)) {
  4159. struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
  4160. cp->kcq1.hw_prod_idx_ptr =
  4161. &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
  4162. cp->kcq1.status_idx_ptr =
  4163. &sb->sb.running_index[SM_RX_ID];
  4164. } else {
  4165. struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
  4166. cp->kcq1.hw_prod_idx_ptr =
  4167. &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
  4168. cp->kcq1.status_idx_ptr =
  4169. &sb->sb.running_index[SM_RX_ID];
  4170. }
  4171. if (BNX2X_CHIP_IS_E2_PLUS(bp)) {
  4172. struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
  4173. cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
  4174. USTORM_FCOE_EQ_PROD_OFFSET(pfid);
  4175. cp->kcq2.sw_prod_idx = 0;
  4176. cp->kcq2.hw_prod_idx_ptr =
  4177. &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
  4178. cp->kcq2.status_idx_ptr =
  4179. &sb->sb.running_index[SM_RX_ID];
  4180. }
  4181. }
  4182. static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
  4183. {
  4184. struct cnic_local *cp = dev->cnic_priv;
  4185. struct bnx2x *bp = netdev_priv(dev->netdev);
  4186. struct cnic_eth_dev *ethdev = cp->ethdev;
  4187. int func, ret;
  4188. u32 pfid;
  4189. dev->stats_addr = ethdev->addr_drv_info_to_mcp;
  4190. cp->func = bp->pf_num;
  4191. func = CNIC_FUNC(cp);
  4192. pfid = bp->pfid;
  4193. ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
  4194. cp->iscsi_start_cid, 0);
  4195. if (ret)
  4196. return -ENOMEM;
  4197. if (BNX2X_CHIP_IS_E2_PLUS(bp)) {
  4198. ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl, dev->max_fcoe_conn,
  4199. cp->fcoe_start_cid, 0);
  4200. if (ret)
  4201. return -ENOMEM;
  4202. }
  4203. cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
  4204. cnic_init_bnx2x_kcq(dev);
  4205. /* Only 1 EQ */
  4206. CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
  4207. CNIC_WR(dev, BAR_CSTRORM_INTMEM +
  4208. CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
  4209. CNIC_WR(dev, BAR_CSTRORM_INTMEM +
  4210. CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
  4211. cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
  4212. CNIC_WR(dev, BAR_CSTRORM_INTMEM +
  4213. CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
  4214. (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
  4215. CNIC_WR(dev, BAR_CSTRORM_INTMEM +
  4216. CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
  4217. cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
  4218. CNIC_WR(dev, BAR_CSTRORM_INTMEM +
  4219. CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
  4220. (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
  4221. CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
  4222. CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
  4223. CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
  4224. CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
  4225. CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
  4226. CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
  4227. HC_INDEX_ISCSI_EQ_CONS);
  4228. CNIC_WR(dev, BAR_USTRORM_INTMEM +
  4229. USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
  4230. cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
  4231. CNIC_WR(dev, BAR_USTRORM_INTMEM +
  4232. USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
  4233. (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
  4234. CNIC_WR(dev, BAR_TSTRORM_INTMEM +
  4235. TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
  4236. cnic_setup_bnx2x_context(dev);
  4237. ret = cnic_init_bnx2x_irq(dev);
  4238. if (ret)
  4239. return ret;
  4240. ethdev->drv_state |= CNIC_DRV_STATE_HANDLES_IRQ;
  4241. return 0;
  4242. }
  4243. static void cnic_init_rings(struct cnic_dev *dev)
  4244. {
  4245. struct cnic_local *cp = dev->cnic_priv;
  4246. struct bnx2x *bp = netdev_priv(dev->netdev);
  4247. struct cnic_uio_dev *udev = cp->udev;
  4248. if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
  4249. return;
  4250. if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
  4251. cnic_init_bnx2_tx_ring(dev);
  4252. cnic_init_bnx2_rx_ring(dev);
  4253. set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
  4254. } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  4255. u32 cli = cp->ethdev->iscsi_l2_client_id;
  4256. u32 cid = cp->ethdev->iscsi_l2_cid;
  4257. u32 cl_qzone_id;
  4258. struct client_init_ramrod_data *data;
  4259. union l5cm_specific_data l5_data;
  4260. struct ustorm_eth_rx_producers rx_prods = {0};
  4261. u32 off, i, *cid_ptr;
  4262. rx_prods.bd_prod = 0;
  4263. rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
  4264. barrier();
  4265. cl_qzone_id = BNX2X_CL_QZONE_ID(bp, cli);
  4266. off = BAR_USTRORM_INTMEM +
  4267. (BNX2X_CHIP_IS_E2_PLUS(bp) ?
  4268. USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
  4269. USTORM_RX_PRODS_E1X_OFFSET(BP_PORT(bp), cli));
  4270. for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
  4271. CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
  4272. set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
  4273. data = udev->l2_buf;
  4274. cid_ptr = udev->l2_buf + 12;
  4275. memset(data, 0, sizeof(*data));
  4276. cnic_init_bnx2x_tx_ring(dev, data);
  4277. cnic_init_bnx2x_rx_ring(dev, data);
  4278. l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
  4279. l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
  4280. set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
  4281. cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
  4282. cid, ETH_CONNECTION_TYPE, &l5_data);
  4283. i = 0;
  4284. while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
  4285. ++i < 10)
  4286. msleep(1);
  4287. if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
  4288. netdev_err(dev->netdev,
  4289. "iSCSI CLIENT_SETUP did not complete\n");
  4290. cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
  4291. cnic_ring_ctl(dev, cid, cli, 1);
  4292. *cid_ptr = cid >> 4;
  4293. *(cid_ptr + 1) = cid * bp->db_size;
  4294. *(cid_ptr + 2) = UIO_USE_TX_DOORBELL;
  4295. }
  4296. }
  4297. static void cnic_shutdown_rings(struct cnic_dev *dev)
  4298. {
  4299. struct cnic_local *cp = dev->cnic_priv;
  4300. struct cnic_uio_dev *udev = cp->udev;
  4301. void *rx_ring;
  4302. if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
  4303. return;
  4304. if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
  4305. cnic_shutdown_bnx2_rx_ring(dev);
  4306. } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  4307. u32 cli = cp->ethdev->iscsi_l2_client_id;
  4308. u32 cid = cp->ethdev->iscsi_l2_cid;
  4309. union l5cm_specific_data l5_data;
  4310. int i;
  4311. cnic_ring_ctl(dev, cid, cli, 0);
  4312. set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
  4313. l5_data.phy_address.lo = cli;
  4314. l5_data.phy_address.hi = 0;
  4315. cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
  4316. cid, ETH_CONNECTION_TYPE, &l5_data);
  4317. i = 0;
  4318. while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
  4319. ++i < 10)
  4320. msleep(1);
  4321. if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
  4322. netdev_err(dev->netdev,
  4323. "iSCSI CLIENT_HALT did not complete\n");
  4324. cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
  4325. memset(&l5_data, 0, sizeof(l5_data));
  4326. cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
  4327. cid, NONE_CONNECTION_TYPE, &l5_data);
  4328. msleep(10);
  4329. }
  4330. clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
  4331. rx_ring = udev->l2_ring + CNIC_PAGE_SIZE;
  4332. memset(rx_ring, 0, CNIC_PAGE_SIZE);
  4333. }
  4334. static int cnic_register_netdev(struct cnic_dev *dev)
  4335. {
  4336. struct cnic_local *cp = dev->cnic_priv;
  4337. struct cnic_eth_dev *ethdev = cp->ethdev;
  4338. int err;
  4339. if (!ethdev)
  4340. return -ENODEV;
  4341. if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
  4342. return 0;
  4343. err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
  4344. if (err)
  4345. netdev_err(dev->netdev, "register_cnic failed\n");
  4346. /* Read iSCSI config again. On some bnx2x device, iSCSI config
  4347. * can change after firmware is downloaded.
  4348. */
  4349. dev->max_iscsi_conn = ethdev->max_iscsi_conn;
  4350. if (ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
  4351. dev->max_iscsi_conn = 0;
  4352. return err;
  4353. }
  4354. static void cnic_unregister_netdev(struct cnic_dev *dev)
  4355. {
  4356. struct cnic_local *cp = dev->cnic_priv;
  4357. struct cnic_eth_dev *ethdev = cp->ethdev;
  4358. if (!ethdev)
  4359. return;
  4360. ethdev->drv_unregister_cnic(dev->netdev);
  4361. }
  4362. static int cnic_start_hw(struct cnic_dev *dev)
  4363. {
  4364. struct cnic_local *cp = dev->cnic_priv;
  4365. struct cnic_eth_dev *ethdev = cp->ethdev;
  4366. int err;
  4367. if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
  4368. return -EALREADY;
  4369. dev->regview = ethdev->io_base;
  4370. pci_dev_get(dev->pcidev);
  4371. cp->func = PCI_FUNC(dev->pcidev->devfn);
  4372. cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
  4373. cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
  4374. err = cp->alloc_resc(dev);
  4375. if (err) {
  4376. netdev_err(dev->netdev, "allocate resource failure\n");
  4377. goto err1;
  4378. }
  4379. err = cp->start_hw(dev);
  4380. if (err)
  4381. goto err1;
  4382. err = cnic_cm_open(dev);
  4383. if (err)
  4384. goto err1;
  4385. set_bit(CNIC_F_CNIC_UP, &dev->flags);
  4386. cp->enable_int(dev);
  4387. return 0;
  4388. err1:
  4389. cp->free_resc(dev);
  4390. pci_dev_put(dev->pcidev);
  4391. return err;
  4392. }
  4393. static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
  4394. {
  4395. cnic_disable_bnx2_int_sync(dev);
  4396. cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
  4397. cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
  4398. cnic_init_context(dev, KWQ_CID);
  4399. cnic_init_context(dev, KCQ_CID);
  4400. cnic_setup_5709_context(dev, 0);
  4401. cnic_free_irq(dev);
  4402. cnic_free_resc(dev);
  4403. }
  4404. static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
  4405. {
  4406. struct cnic_local *cp = dev->cnic_priv;
  4407. struct bnx2x *bp = netdev_priv(dev->netdev);
  4408. u32 hc_index = HC_INDEX_ISCSI_EQ_CONS;
  4409. u32 sb_id = cp->status_blk_num;
  4410. u32 idx_off, syn_off;
  4411. cnic_free_irq(dev);
  4412. if (BNX2X_CHIP_IS_E2_PLUS(bp)) {
  4413. idx_off = offsetof(struct hc_status_block_e2, index_values) +
  4414. (hc_index * sizeof(u16));
  4415. syn_off = CSTORM_HC_SYNC_LINE_INDEX_E2_OFFSET(hc_index, sb_id);
  4416. } else {
  4417. idx_off = offsetof(struct hc_status_block_e1x, index_values) +
  4418. (hc_index * sizeof(u16));
  4419. syn_off = CSTORM_HC_SYNC_LINE_INDEX_E1X_OFFSET(hc_index, sb_id);
  4420. }
  4421. CNIC_WR16(dev, BAR_CSTRORM_INTMEM + syn_off, 0);
  4422. CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_STATUS_BLOCK_OFFSET(sb_id) +
  4423. idx_off, 0);
  4424. *cp->kcq1.hw_prod_idx_ptr = 0;
  4425. CNIC_WR(dev, BAR_CSTRORM_INTMEM +
  4426. CSTORM_ISCSI_EQ_CONS_OFFSET(bp->pfid, 0), 0);
  4427. CNIC_WR16(dev, cp->kcq1.io_addr, 0);
  4428. cnic_free_resc(dev);
  4429. }
  4430. static void cnic_stop_hw(struct cnic_dev *dev)
  4431. {
  4432. if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
  4433. struct cnic_local *cp = dev->cnic_priv;
  4434. int i = 0;
  4435. /* Need to wait for the ring shutdown event to complete
  4436. * before clearing the CNIC_UP flag.
  4437. */
  4438. while (cp->udev && cp->udev->uio_dev != -1 && i < 15) {
  4439. msleep(100);
  4440. i++;
  4441. }
  4442. cnic_shutdown_rings(dev);
  4443. cp->stop_cm(dev);
  4444. cp->ethdev->drv_state &= ~CNIC_DRV_STATE_HANDLES_IRQ;
  4445. clear_bit(CNIC_F_CNIC_UP, &dev->flags);
  4446. RCU_INIT_POINTER(cp->ulp_ops[CNIC_ULP_L4], NULL);
  4447. synchronize_rcu();
  4448. cnic_cm_shutdown(dev);
  4449. cp->stop_hw(dev);
  4450. pci_dev_put(dev->pcidev);
  4451. }
  4452. }
  4453. static void cnic_free_dev(struct cnic_dev *dev)
  4454. {
  4455. int i = 0;
  4456. while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
  4457. msleep(100);
  4458. i++;
  4459. }
  4460. if (atomic_read(&dev->ref_count) != 0)
  4461. netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
  4462. netdev_info(dev->netdev, "Removed CNIC device\n");
  4463. dev_put(dev->netdev);
  4464. kfree(dev);
  4465. }
  4466. static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
  4467. struct pci_dev *pdev)
  4468. {
  4469. struct cnic_dev *cdev;
  4470. struct cnic_local *cp;
  4471. int alloc_size;
  4472. alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
  4473. cdev = kzalloc(alloc_size, GFP_KERNEL);
  4474. if (cdev == NULL)
  4475. return NULL;
  4476. cdev->netdev = dev;
  4477. cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
  4478. cdev->register_device = cnic_register_device;
  4479. cdev->unregister_device = cnic_unregister_device;
  4480. cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
  4481. cp = cdev->cnic_priv;
  4482. cp->dev = cdev;
  4483. cp->l2_single_buf_size = 0x400;
  4484. cp->l2_rx_ring_size = 3;
  4485. spin_lock_init(&cp->cnic_ulp_lock);
  4486. netdev_info(dev, "Added CNIC device\n");
  4487. return cdev;
  4488. }
  4489. static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
  4490. {
  4491. struct pci_dev *pdev;
  4492. struct cnic_dev *cdev;
  4493. struct cnic_local *cp;
  4494. struct bnx2 *bp = netdev_priv(dev);
  4495. struct cnic_eth_dev *ethdev = NULL;
  4496. if (bp->cnic_probe)
  4497. ethdev = (bp->cnic_probe)(dev);
  4498. if (!ethdev)
  4499. return NULL;
  4500. pdev = ethdev->pdev;
  4501. if (!pdev)
  4502. return NULL;
  4503. dev_hold(dev);
  4504. pci_dev_get(pdev);
  4505. if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
  4506. pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
  4507. (pdev->revision < 0x10)) {
  4508. pci_dev_put(pdev);
  4509. goto cnic_err;
  4510. }
  4511. pci_dev_put(pdev);
  4512. cdev = cnic_alloc_dev(dev, pdev);
  4513. if (cdev == NULL)
  4514. goto cnic_err;
  4515. set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
  4516. cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
  4517. cp = cdev->cnic_priv;
  4518. cp->ethdev = ethdev;
  4519. cdev->pcidev = pdev;
  4520. cp->chip_id = ethdev->chip_id;
  4521. cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
  4522. cp->cnic_ops = &cnic_bnx2_ops;
  4523. cp->start_hw = cnic_start_bnx2_hw;
  4524. cp->stop_hw = cnic_stop_bnx2_hw;
  4525. cp->setup_pgtbl = cnic_setup_page_tbl;
  4526. cp->alloc_resc = cnic_alloc_bnx2_resc;
  4527. cp->free_resc = cnic_free_resc;
  4528. cp->start_cm = cnic_cm_init_bnx2_hw;
  4529. cp->stop_cm = cnic_cm_stop_bnx2_hw;
  4530. cp->enable_int = cnic_enable_bnx2_int;
  4531. cp->disable_int_sync = cnic_disable_bnx2_int_sync;
  4532. cp->close_conn = cnic_close_bnx2_conn;
  4533. return cdev;
  4534. cnic_err:
  4535. dev_put(dev);
  4536. return NULL;
  4537. }
  4538. static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
  4539. {
  4540. struct pci_dev *pdev;
  4541. struct cnic_dev *cdev;
  4542. struct cnic_local *cp;
  4543. struct bnx2x *bp = netdev_priv(dev);
  4544. struct cnic_eth_dev *ethdev = NULL;
  4545. if (bp->cnic_probe)
  4546. ethdev = bp->cnic_probe(dev);
  4547. if (!ethdev)
  4548. return NULL;
  4549. pdev = ethdev->pdev;
  4550. if (!pdev)
  4551. return NULL;
  4552. dev_hold(dev);
  4553. cdev = cnic_alloc_dev(dev, pdev);
  4554. if (cdev == NULL) {
  4555. dev_put(dev);
  4556. return NULL;
  4557. }
  4558. set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
  4559. cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
  4560. cp = cdev->cnic_priv;
  4561. cp->ethdev = ethdev;
  4562. cdev->pcidev = pdev;
  4563. cp->chip_id = ethdev->chip_id;
  4564. cdev->stats_addr = ethdev->addr_drv_info_to_mcp;
  4565. if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
  4566. cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
  4567. if (CNIC_SUPPORTS_FCOE(bp)) {
  4568. cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
  4569. cdev->max_fcoe_exchanges = ethdev->max_fcoe_exchanges;
  4570. }
  4571. if (cdev->max_fcoe_conn > BNX2X_FCOE_NUM_CONNECTIONS)
  4572. cdev->max_fcoe_conn = BNX2X_FCOE_NUM_CONNECTIONS;
  4573. memcpy(cdev->mac_addr, ethdev->iscsi_mac, ETH_ALEN);
  4574. cp->cnic_ops = &cnic_bnx2x_ops;
  4575. cp->start_hw = cnic_start_bnx2x_hw;
  4576. cp->stop_hw = cnic_stop_bnx2x_hw;
  4577. cp->setup_pgtbl = cnic_setup_page_tbl_le;
  4578. cp->alloc_resc = cnic_alloc_bnx2x_resc;
  4579. cp->free_resc = cnic_free_resc;
  4580. cp->start_cm = cnic_cm_init_bnx2x_hw;
  4581. cp->stop_cm = cnic_cm_stop_bnx2x_hw;
  4582. cp->enable_int = cnic_enable_bnx2x_int;
  4583. cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
  4584. if (BNX2X_CHIP_IS_E2_PLUS(bp)) {
  4585. cp->ack_int = cnic_ack_bnx2x_e2_msix;
  4586. cp->arm_int = cnic_arm_bnx2x_e2_msix;
  4587. } else {
  4588. cp->ack_int = cnic_ack_bnx2x_msix;
  4589. cp->arm_int = cnic_arm_bnx2x_msix;
  4590. }
  4591. cp->close_conn = cnic_close_bnx2x_conn;
  4592. return cdev;
  4593. }
  4594. static struct cnic_dev *is_cnic_dev(struct net_device *dev)
  4595. {
  4596. struct ethtool_drvinfo drvinfo;
  4597. struct cnic_dev *cdev = NULL;
  4598. if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
  4599. memset(&drvinfo, 0, sizeof(drvinfo));
  4600. dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
  4601. if (!strcmp(drvinfo.driver, "bnx2"))
  4602. cdev = init_bnx2_cnic(dev);
  4603. if (!strcmp(drvinfo.driver, "bnx2x"))
  4604. cdev = init_bnx2x_cnic(dev);
  4605. if (cdev) {
  4606. write_lock(&cnic_dev_lock);
  4607. list_add(&cdev->list, &cnic_dev_list);
  4608. write_unlock(&cnic_dev_lock);
  4609. }
  4610. }
  4611. return cdev;
  4612. }
  4613. static void cnic_rcv_netevent(struct cnic_local *cp, unsigned long event,
  4614. u16 vlan_id)
  4615. {
  4616. int if_type;
  4617. for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
  4618. struct cnic_ulp_ops *ulp_ops;
  4619. void *ctx;
  4620. mutex_lock(&cnic_lock);
  4621. ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
  4622. lockdep_is_held(&cnic_lock));
  4623. if (!ulp_ops || !ulp_ops->indicate_netevent) {
  4624. mutex_unlock(&cnic_lock);
  4625. continue;
  4626. }
  4627. ctx = cp->ulp_handle[if_type];
  4628. set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  4629. mutex_unlock(&cnic_lock);
  4630. ulp_ops->indicate_netevent(ctx, event, vlan_id);
  4631. clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
  4632. }
  4633. }
  4634. /* netdev event handler */
  4635. static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
  4636. void *ptr)
  4637. {
  4638. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  4639. struct cnic_dev *dev;
  4640. int new_dev = 0;
  4641. dev = cnic_from_netdev(netdev);
  4642. if (!dev && event == NETDEV_REGISTER) {
  4643. /* Check for the hot-plug device */
  4644. dev = is_cnic_dev(netdev);
  4645. if (dev) {
  4646. new_dev = 1;
  4647. cnic_hold(dev);
  4648. }
  4649. }
  4650. if (dev) {
  4651. struct cnic_local *cp = dev->cnic_priv;
  4652. if (new_dev)
  4653. cnic_ulp_init(dev);
  4654. else if (event == NETDEV_UNREGISTER)
  4655. cnic_ulp_exit(dev);
  4656. if (event == NETDEV_UP) {
  4657. if (cnic_register_netdev(dev) != 0) {
  4658. cnic_put(dev);
  4659. goto done;
  4660. }
  4661. if (!cnic_start_hw(dev))
  4662. cnic_ulp_start(dev);
  4663. }
  4664. cnic_rcv_netevent(cp, event, 0);
  4665. if (event == NETDEV_GOING_DOWN) {
  4666. cnic_ulp_stop(dev);
  4667. cnic_stop_hw(dev);
  4668. cnic_unregister_netdev(dev);
  4669. } else if (event == NETDEV_UNREGISTER) {
  4670. write_lock(&cnic_dev_lock);
  4671. list_del_init(&dev->list);
  4672. write_unlock(&cnic_dev_lock);
  4673. cnic_put(dev);
  4674. cnic_free_dev(dev);
  4675. goto done;
  4676. }
  4677. cnic_put(dev);
  4678. } else {
  4679. struct net_device *realdev;
  4680. u16 vid;
  4681. vid = cnic_get_vlan(netdev, &realdev);
  4682. if (realdev) {
  4683. dev = cnic_from_netdev(realdev);
  4684. if (dev) {
  4685. vid |= VLAN_TAG_PRESENT;
  4686. cnic_rcv_netevent(dev->cnic_priv, event, vid);
  4687. cnic_put(dev);
  4688. }
  4689. }
  4690. }
  4691. done:
  4692. return NOTIFY_DONE;
  4693. }
  4694. static struct notifier_block cnic_netdev_notifier = {
  4695. .notifier_call = cnic_netdev_event
  4696. };
  4697. static void cnic_release(void)
  4698. {
  4699. struct cnic_uio_dev *udev;
  4700. while (!list_empty(&cnic_udev_list)) {
  4701. udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
  4702. list);
  4703. cnic_free_uio(udev);
  4704. }
  4705. }
  4706. static int __init cnic_init(void)
  4707. {
  4708. int rc = 0;
  4709. pr_info("%s", version);
  4710. rc = register_netdevice_notifier(&cnic_netdev_notifier);
  4711. if (rc) {
  4712. cnic_release();
  4713. return rc;
  4714. }
  4715. cnic_wq = create_singlethread_workqueue("cnic_wq");
  4716. if (!cnic_wq) {
  4717. cnic_release();
  4718. unregister_netdevice_notifier(&cnic_netdev_notifier);
  4719. return -ENOMEM;
  4720. }
  4721. return 0;
  4722. }
  4723. static void __exit cnic_exit(void)
  4724. {
  4725. unregister_netdevice_notifier(&cnic_netdev_notifier);
  4726. cnic_release();
  4727. destroy_workqueue(cnic_wq);
  4728. }
  4729. module_init(cnic_init);
  4730. module_exit(cnic_exit);