cnic.c 138 KB

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