cnic.c 118 KB

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