cnic.c 143 KB

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