cnic.c 119 KB

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