cnic.c 125 KB

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