cnic.c 147 KB

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