cnic.c 143 KB

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