cnic.c 148 KB

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