ipa_dp.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/device.h>
  7. #include <linux/dmapool.h>
  8. #include <linux/list.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/msm_gsi.h>
  11. #include "ipa_i.h"
  12. #include "ipa_trace.h"
  13. #include "ipahal/ipahal.h"
  14. #include "ipahal/ipahal_fltrt.h"
  15. #define IPA_WAN_AGGR_PKT_CNT 5
  16. #define IPA_WAN_NAPI_MAX_FRAMES (NAPI_WEIGHT / IPA_WAN_AGGR_PKT_CNT)
  17. #define IPA_LAST_DESC_CNT 0xFFFF
  18. #define POLLING_INACTIVITY_RX 40
  19. #define POLLING_MIN_SLEEP_RX 1010
  20. #define POLLING_MAX_SLEEP_RX 1050
  21. #define POLLING_INACTIVITY_TX 40
  22. #define POLLING_MIN_SLEEP_TX 400
  23. #define POLLING_MAX_SLEEP_TX 500
  24. #define SUSPEND_MIN_SLEEP_RX 1000
  25. #define SUSPEND_MAX_SLEEP_RX 1005
  26. /* 8K less 1 nominal MTU (1500 bytes) rounded to units of KB */
  27. #define IPA_MTU 1500
  28. #define IPA_GENERIC_AGGR_BYTE_LIMIT 6
  29. #define IPA_GENERIC_AGGR_TIME_LIMIT 500 /* 0.5msec */
  30. #define IPA_GENERIC_AGGR_PKT_LIMIT 0
  31. #define IPA_GSB_AGGR_BYTE_LIMIT 14
  32. #define IPA_GSB_RX_BUFF_BASE_SZ 16384
  33. #define IPA_GENERIC_RX_BUFF_BASE_SZ 8192
  34. #define IPA_REAL_GENERIC_RX_BUFF_SZ(X) (SKB_DATA_ALIGN(\
  35. (X) + NET_SKB_PAD) +\
  36. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  37. #define IPA_GENERIC_RX_BUFF_SZ(X) ((X) -\
  38. (IPA_REAL_GENERIC_RX_BUFF_SZ(X) - (X)))
  39. #define IPA_GENERIC_RX_BUFF_LIMIT (\
  40. IPA_REAL_GENERIC_RX_BUFF_SZ(\
  41. IPA_GENERIC_RX_BUFF_BASE_SZ) -\
  42. IPA_GENERIC_RX_BUFF_BASE_SZ)
  43. /* less 1 nominal MTU (1500 bytes) rounded to units of KB */
  44. #define IPA_ADJUST_AGGR_BYTE_LIMIT(X) (((X) - IPA_MTU)/1000)
  45. #define IPA_RX_BUFF_CLIENT_HEADROOM 256
  46. #define IPA_WLAN_RX_POOL_SZ 100
  47. #define IPA_WLAN_RX_POOL_SZ_LOW_WM 5
  48. #define IPA_WLAN_RX_BUFF_SZ 2048
  49. #define IPA_WLAN_COMM_RX_POOL_LOW 100
  50. #define IPA_WLAN_COMM_RX_POOL_HIGH 900
  51. #define IPA_ODU_RX_BUFF_SZ 2048
  52. #define IPA_ODU_RX_POOL_SZ 64
  53. #define IPA_ODL_RX_BUFF_SZ (16 * 1024)
  54. #define IPA_GSI_MAX_CH_LOW_WEIGHT 15
  55. #define IPA_GSI_EVT_RING_INT_MODT (16) /* 0.5ms under 32KHz clock */
  56. #define IPA_GSI_EVT_RING_INT_MODC (20)
  57. #define IPA_GSI_CH_20_WA_NUM_CH_TO_ALLOC 10
  58. /* The below virtual channel cannot be used by any entity */
  59. #define IPA_GSI_CH_20_WA_VIRT_CHAN 29
  60. #define IPA_DEFAULT_SYS_YELLOW_WM 32
  61. #define IPA_REPL_XFER_THRESH 20
  62. #define IPA_REPL_XFER_MAX 36
  63. #define IPA_TX_SEND_COMPL_NOP_DELAY_NS (2 * 1000 * 1000)
  64. #define IPA_APPS_BW_FOR_PM 700
  65. #define IPA_SEND_MAX_DESC (20)
  66. #define IPA_EOT_THRESH 32
  67. #define IPA_QMAP_ID_BYTE 0
  68. static struct sk_buff *ipa3_get_skb_ipa_rx(unsigned int len, gfp_t flags);
  69. static void ipa3_replenish_wlan_rx_cache(struct ipa3_sys_context *sys);
  70. static void ipa3_replenish_rx_cache(struct ipa3_sys_context *sys);
  71. static void ipa3_replenish_rx_work_func(struct work_struct *work);
  72. static void ipa3_fast_replenish_rx_cache(struct ipa3_sys_context *sys);
  73. static void ipa3_wq_handle_rx(struct work_struct *work);
  74. static void ipa3_wq_rx_common(struct ipa3_sys_context *sys,
  75. struct gsi_chan_xfer_notify *notify);
  76. static void ipa3_wq_rx_napi_chain(struct ipa3_sys_context *sys,
  77. struct gsi_chan_xfer_notify *notify, uint32_t num);
  78. static void ipa3_wlan_wq_rx_common(struct ipa3_sys_context *sys,
  79. struct gsi_chan_xfer_notify *notify);
  80. static int ipa3_assign_policy(struct ipa_sys_connect_params *in,
  81. struct ipa3_sys_context *sys);
  82. static void ipa3_cleanup_rx(struct ipa3_sys_context *sys);
  83. static void ipa3_wq_rx_avail(struct work_struct *work);
  84. static void ipa3_alloc_wlan_rx_common_cache(u32 size);
  85. static void ipa3_cleanup_wlan_rx_common_cache(void);
  86. static void ipa3_wq_repl_rx(struct work_struct *work);
  87. static void ipa3_dma_memcpy_notify(struct ipa3_sys_context *sys);
  88. static int ipa_gsi_setup_coal_def_channel(struct ipa_sys_connect_params *in,
  89. struct ipa3_ep_context *ep, struct ipa3_ep_context *coal_ep);
  90. static int ipa_gsi_setup_channel(struct ipa_sys_connect_params *in,
  91. struct ipa3_ep_context *ep);
  92. static int ipa_gsi_setup_event_ring(struct ipa3_ep_context *ep,
  93. u32 ring_size, gfp_t mem_flag);
  94. static int ipa_gsi_setup_transfer_ring(struct ipa3_ep_context *ep,
  95. u32 ring_size, struct ipa3_sys_context *user_data, gfp_t mem_flag);
  96. static int ipa3_teardown_coal_def_pipe(u32 clnt_hdl);
  97. static int ipa_populate_tag_field(struct ipa3_desc *desc,
  98. struct ipa3_tx_pkt_wrapper *tx_pkt,
  99. struct ipahal_imm_cmd_pyld **tag_pyld_ret);
  100. static int ipa_poll_gsi_pkt(struct ipa3_sys_context *sys,
  101. struct gsi_chan_xfer_notify *notify);
  102. static int ipa_poll_gsi_n_pkt(struct ipa3_sys_context *sys,
  103. struct gsi_chan_xfer_notify *notify, int expected_num,
  104. int *actual_num);
  105. static unsigned long tag_to_pointer_wa(uint64_t tag);
  106. static uint64_t pointer_to_tag_wa(struct ipa3_tx_pkt_wrapper *tx_pkt);
  107. static u32 ipa_adjust_ra_buff_base_sz(u32 aggr_byte_limit);
  108. static void ipa3_wq_write_done_common(struct ipa3_sys_context *sys,
  109. struct ipa3_tx_pkt_wrapper *tx_pkt)
  110. {
  111. struct ipa3_tx_pkt_wrapper *next_pkt;
  112. int i, cnt;
  113. if (unlikely(tx_pkt == NULL)) {
  114. IPAERR("tx_pkt is NULL\n");
  115. return;
  116. }
  117. cnt = tx_pkt->cnt;
  118. IPADBG_LOW("cnt: %d\n", cnt);
  119. for (i = 0; i < cnt; i++) {
  120. spin_lock_bh(&sys->spinlock);
  121. if (unlikely(list_empty(&sys->head_desc_list))) {
  122. spin_unlock_bh(&sys->spinlock);
  123. return;
  124. }
  125. next_pkt = list_next_entry(tx_pkt, link);
  126. list_del(&tx_pkt->link);
  127. sys->len--;
  128. spin_unlock_bh(&sys->spinlock);
  129. if (!tx_pkt->no_unmap_dma) {
  130. if (tx_pkt->type != IPA_DATA_DESC_SKB_PAGED) {
  131. dma_unmap_single(ipa3_ctx->pdev,
  132. tx_pkt->mem.phys_base,
  133. tx_pkt->mem.size,
  134. DMA_TO_DEVICE);
  135. } else {
  136. dma_unmap_page(ipa3_ctx->pdev,
  137. tx_pkt->mem.phys_base,
  138. tx_pkt->mem.size,
  139. DMA_TO_DEVICE);
  140. }
  141. }
  142. if (tx_pkt->callback)
  143. tx_pkt->callback(tx_pkt->user1, tx_pkt->user2);
  144. kmem_cache_free(ipa3_ctx->tx_pkt_wrapper_cache, tx_pkt);
  145. tx_pkt = next_pkt;
  146. }
  147. }
  148. static void ipa3_wq_write_done_status(int src_pipe,
  149. struct ipa3_tx_pkt_wrapper *tx_pkt)
  150. {
  151. struct ipa3_sys_context *sys;
  152. WARN_ON(src_pipe >= ipa3_ctx->ipa_num_pipes);
  153. if (!ipa3_ctx->ep[src_pipe].status.status_en)
  154. return;
  155. sys = ipa3_ctx->ep[src_pipe].sys;
  156. if (!sys)
  157. return;
  158. ipa3_wq_write_done_common(sys, tx_pkt);
  159. }
  160. /**
  161. * ipa_write_done() - this function will be (eventually) called when a Tx
  162. * operation is complete
  163. * @data: user pointer point to the ipa3_sys_context
  164. *
  165. * Will be called in deferred context.
  166. * - invoke the callback supplied by the client who sent this command
  167. * - iterate over all packets and validate that
  168. * the order for sent packet is the same as expected
  169. * - delete all the tx packet descriptors from the system
  170. * pipe context (not needed anymore)
  171. */
  172. static void ipa3_tasklet_write_done(unsigned long data)
  173. {
  174. struct ipa3_sys_context *sys;
  175. struct ipa3_tx_pkt_wrapper *this_pkt;
  176. bool xmit_done = false;
  177. sys = (struct ipa3_sys_context *)data;
  178. spin_lock_bh(&sys->spinlock);
  179. while (atomic_add_unless(&sys->xmit_eot_cnt, -1, 0)) {
  180. while (!list_empty(&sys->head_desc_list)) {
  181. this_pkt = list_first_entry(&sys->head_desc_list,
  182. struct ipa3_tx_pkt_wrapper, link);
  183. xmit_done = this_pkt->xmit_done;
  184. spin_unlock_bh(&sys->spinlock);
  185. ipa3_wq_write_done_common(sys, this_pkt);
  186. spin_lock_bh(&sys->spinlock);
  187. if (xmit_done)
  188. break;
  189. }
  190. }
  191. spin_unlock_bh(&sys->spinlock);
  192. }
  193. static void ipa3_send_nop_desc(struct work_struct *work)
  194. {
  195. struct ipa3_sys_context *sys = container_of(work,
  196. struct ipa3_sys_context, work);
  197. struct gsi_xfer_elem nop_xfer;
  198. struct ipa3_tx_pkt_wrapper *tx_pkt;
  199. IPADBG_LOW("gsi send NOP for ch: %lu\n", sys->ep->gsi_chan_hdl);
  200. tx_pkt = kmem_cache_zalloc(ipa3_ctx->tx_pkt_wrapper_cache, GFP_KERNEL);
  201. if (!tx_pkt) {
  202. queue_work(sys->wq, &sys->work);
  203. return;
  204. }
  205. INIT_LIST_HEAD(&tx_pkt->link);
  206. tx_pkt->cnt = 1;
  207. tx_pkt->no_unmap_dma = true;
  208. tx_pkt->sys = sys;
  209. spin_lock_bh(&sys->spinlock);
  210. if (unlikely(!sys->nop_pending)) {
  211. spin_unlock_bh(&sys->spinlock);
  212. kmem_cache_free(ipa3_ctx->tx_pkt_wrapper_cache, tx_pkt);
  213. return;
  214. }
  215. list_add_tail(&tx_pkt->link, &sys->head_desc_list);
  216. sys->nop_pending = false;
  217. memset(&nop_xfer, 0, sizeof(nop_xfer));
  218. nop_xfer.type = GSI_XFER_ELEM_NOP;
  219. nop_xfer.flags = GSI_XFER_FLAG_EOT;
  220. nop_xfer.xfer_user_data = tx_pkt;
  221. if (gsi_queue_xfer(sys->ep->gsi_chan_hdl, 1, &nop_xfer, true)) {
  222. spin_unlock_bh(&sys->spinlock);
  223. IPAERR("gsi_queue_xfer for ch:%lu failed\n",
  224. sys->ep->gsi_chan_hdl);
  225. queue_work(sys->wq, &sys->work);
  226. return;
  227. }
  228. spin_unlock_bh(&sys->spinlock);
  229. /* make sure TAG process is sent before clocks are gated */
  230. ipa3_ctx->tag_process_before_gating = true;
  231. }
  232. /**
  233. * ipa3_send() - Send multiple descriptors in one HW transaction
  234. * @sys: system pipe context
  235. * @num_desc: number of packets
  236. * @desc: packets to send (may be immediate command or data)
  237. * @in_atomic: whether caller is in atomic context
  238. *
  239. * This function is used for GPI connection.
  240. * - ipa3_tx_pkt_wrapper will be used for each ipa
  241. * descriptor (allocated from wrappers cache)
  242. * - The wrapper struct will be configured for each ipa-desc payload and will
  243. * contain information which will be later used by the user callbacks
  244. * - Each packet (command or data) that will be sent will also be saved in
  245. * ipa3_sys_context for later check that all data was sent
  246. *
  247. * Return codes: 0: success, -EFAULT: failure
  248. */
  249. int ipa3_send(struct ipa3_sys_context *sys,
  250. u32 num_desc,
  251. struct ipa3_desc *desc,
  252. bool in_atomic)
  253. {
  254. struct ipa3_tx_pkt_wrapper *tx_pkt, *tx_pkt_first = NULL;
  255. struct ipahal_imm_cmd_pyld *tag_pyld_ret = NULL;
  256. struct ipa3_tx_pkt_wrapper *next_pkt;
  257. struct gsi_xfer_elem gsi_xfer[IPA_SEND_MAX_DESC];
  258. int i = 0;
  259. int j;
  260. int result;
  261. u32 mem_flag = GFP_ATOMIC;
  262. const struct ipa_gsi_ep_config *gsi_ep_cfg;
  263. bool send_nop = false;
  264. unsigned int max_desc;
  265. if (unlikely(!in_atomic))
  266. mem_flag = GFP_KERNEL;
  267. gsi_ep_cfg = ipa3_get_gsi_ep_info(sys->ep->client);
  268. if (unlikely(!gsi_ep_cfg)) {
  269. IPAERR("failed to get gsi EP config for client=%d\n",
  270. sys->ep->client);
  271. return -EFAULT;
  272. }
  273. if (unlikely(num_desc > IPA_SEND_MAX_DESC)) {
  274. IPAERR("max descriptors reached need=%d max=%d\n",
  275. num_desc, IPA_SEND_MAX_DESC);
  276. WARN_ON(1);
  277. return -EPERM;
  278. }
  279. max_desc = gsi_ep_cfg->ipa_if_tlv;
  280. if (gsi_ep_cfg->prefetch_mode == GSI_SMART_PRE_FETCH ||
  281. gsi_ep_cfg->prefetch_mode == GSI_FREE_PRE_FETCH)
  282. max_desc -= gsi_ep_cfg->prefetch_threshold;
  283. if (unlikely(num_desc > max_desc)) {
  284. IPAERR("Too many chained descriptors need=%d max=%d\n",
  285. num_desc, max_desc);
  286. WARN_ON(1);
  287. return -EPERM;
  288. }
  289. /* initialize only the xfers we use */
  290. memset(gsi_xfer, 0, sizeof(gsi_xfer[0]) * num_desc);
  291. spin_lock_bh(&sys->spinlock);
  292. for (i = 0; i < num_desc; i++) {
  293. tx_pkt = kmem_cache_zalloc(ipa3_ctx->tx_pkt_wrapper_cache,
  294. GFP_ATOMIC);
  295. if (!tx_pkt) {
  296. IPAERR("failed to alloc tx wrapper\n");
  297. result = -ENOMEM;
  298. goto failure;
  299. }
  300. INIT_LIST_HEAD(&tx_pkt->link);
  301. if (i == 0) {
  302. tx_pkt_first = tx_pkt;
  303. tx_pkt->cnt = num_desc;
  304. }
  305. /* populate tag field */
  306. if (desc[i].is_tag_status) {
  307. if (ipa_populate_tag_field(&desc[i], tx_pkt,
  308. &tag_pyld_ret)) {
  309. IPAERR("Failed to populate tag field\n");
  310. result = -EFAULT;
  311. goto failure_dma_map;
  312. }
  313. }
  314. tx_pkt->type = desc[i].type;
  315. if (desc[i].type != IPA_DATA_DESC_SKB_PAGED) {
  316. tx_pkt->mem.base = desc[i].pyld;
  317. tx_pkt->mem.size = desc[i].len;
  318. if (!desc[i].dma_address_valid) {
  319. tx_pkt->mem.phys_base =
  320. dma_map_single(ipa3_ctx->pdev,
  321. tx_pkt->mem.base,
  322. tx_pkt->mem.size,
  323. DMA_TO_DEVICE);
  324. } else {
  325. tx_pkt->mem.phys_base =
  326. desc[i].dma_address;
  327. tx_pkt->no_unmap_dma = true;
  328. }
  329. } else {
  330. tx_pkt->mem.base = desc[i].frag;
  331. tx_pkt->mem.size = desc[i].len;
  332. if (!desc[i].dma_address_valid) {
  333. tx_pkt->mem.phys_base =
  334. skb_frag_dma_map(ipa3_ctx->pdev,
  335. desc[i].frag,
  336. 0, tx_pkt->mem.size,
  337. DMA_TO_DEVICE);
  338. } else {
  339. tx_pkt->mem.phys_base =
  340. desc[i].dma_address;
  341. tx_pkt->no_unmap_dma = true;
  342. }
  343. }
  344. if (dma_mapping_error(ipa3_ctx->pdev, tx_pkt->mem.phys_base)) {
  345. IPAERR("failed to do dma map.\n");
  346. result = -EFAULT;
  347. goto failure_dma_map;
  348. }
  349. tx_pkt->sys = sys;
  350. tx_pkt->callback = desc[i].callback;
  351. tx_pkt->user1 = desc[i].user1;
  352. tx_pkt->user2 = desc[i].user2;
  353. tx_pkt->xmit_done = false;
  354. list_add_tail(&tx_pkt->link, &sys->head_desc_list);
  355. gsi_xfer[i].addr = tx_pkt->mem.phys_base;
  356. /*
  357. * Special treatment for immediate commands, where
  358. * the structure of the descriptor is different
  359. */
  360. if (desc[i].type == IPA_IMM_CMD_DESC) {
  361. gsi_xfer[i].len = desc[i].opcode;
  362. gsi_xfer[i].type =
  363. GSI_XFER_ELEM_IMME_CMD;
  364. } else {
  365. gsi_xfer[i].len = desc[i].len;
  366. gsi_xfer[i].type =
  367. GSI_XFER_ELEM_DATA;
  368. }
  369. if (i == (num_desc - 1)) {
  370. if (!sys->use_comm_evt_ring ||
  371. (sys->pkt_sent % IPA_EOT_THRESH == 0)) {
  372. gsi_xfer[i].flags |=
  373. GSI_XFER_FLAG_EOT;
  374. gsi_xfer[i].flags |=
  375. GSI_XFER_FLAG_BEI;
  376. } else {
  377. send_nop = true;
  378. }
  379. gsi_xfer[i].xfer_user_data =
  380. tx_pkt_first;
  381. } else {
  382. gsi_xfer[i].flags |=
  383. GSI_XFER_FLAG_CHAIN;
  384. }
  385. }
  386. IPADBG_LOW("ch:%lu queue xfer\n", sys->ep->gsi_chan_hdl);
  387. result = gsi_queue_xfer(sys->ep->gsi_chan_hdl, num_desc,
  388. gsi_xfer, true);
  389. if (result != GSI_STATUS_SUCCESS) {
  390. IPAERR_RL("GSI xfer failed.\n");
  391. result = -EFAULT;
  392. goto failure;
  393. }
  394. if (send_nop && !sys->nop_pending)
  395. sys->nop_pending = true;
  396. else
  397. send_nop = false;
  398. sys->pkt_sent++;
  399. spin_unlock_bh(&sys->spinlock);
  400. /* set the timer for sending the NOP descriptor */
  401. if (send_nop) {
  402. ktime_t time = ktime_set(0, IPA_TX_SEND_COMPL_NOP_DELAY_NS);
  403. IPADBG_LOW("scheduling timer for ch %lu\n",
  404. sys->ep->gsi_chan_hdl);
  405. hrtimer_start(&sys->db_timer, time, HRTIMER_MODE_REL);
  406. }
  407. /* make sure TAG process is sent before clocks are gated */
  408. ipa3_ctx->tag_process_before_gating = true;
  409. return 0;
  410. failure_dma_map:
  411. kmem_cache_free(ipa3_ctx->tx_pkt_wrapper_cache, tx_pkt);
  412. failure:
  413. ipahal_destroy_imm_cmd(tag_pyld_ret);
  414. tx_pkt = tx_pkt_first;
  415. for (j = 0; j < i; j++) {
  416. next_pkt = list_next_entry(tx_pkt, link);
  417. list_del(&tx_pkt->link);
  418. if (!tx_pkt->no_unmap_dma) {
  419. if (desc[j].type != IPA_DATA_DESC_SKB_PAGED) {
  420. dma_unmap_single(ipa3_ctx->pdev,
  421. tx_pkt->mem.phys_base,
  422. tx_pkt->mem.size, DMA_TO_DEVICE);
  423. } else {
  424. dma_unmap_page(ipa3_ctx->pdev,
  425. tx_pkt->mem.phys_base,
  426. tx_pkt->mem.size,
  427. DMA_TO_DEVICE);
  428. }
  429. }
  430. kmem_cache_free(ipa3_ctx->tx_pkt_wrapper_cache, tx_pkt);
  431. tx_pkt = next_pkt;
  432. }
  433. spin_unlock_bh(&sys->spinlock);
  434. return result;
  435. }
  436. /**
  437. * ipa3_send_one() - Send a single descriptor
  438. * @sys: system pipe context
  439. * @desc: descriptor to send
  440. * @in_atomic: whether caller is in atomic context
  441. *
  442. * - Allocate tx_packet wrapper
  443. * - transfer data to the IPA
  444. * - after the transfer was done the SPS will
  445. * notify the sending user via ipa_sps_irq_comp_tx()
  446. *
  447. * Return codes: 0: success, -EFAULT: failure
  448. */
  449. int ipa3_send_one(struct ipa3_sys_context *sys, struct ipa3_desc *desc,
  450. bool in_atomic)
  451. {
  452. return ipa3_send(sys, 1, desc, in_atomic);
  453. }
  454. /**
  455. * ipa3_transport_irq_cmd_ack - callback function which will be called by
  456. * the transport driver after an immediate command is complete.
  457. * @user1: pointer to the descriptor of the transfer
  458. * @user2:
  459. *
  460. * Complete the immediate commands completion object, this will release the
  461. * thread which waits on this completion object (ipa3_send_cmd())
  462. */
  463. static void ipa3_transport_irq_cmd_ack(void *user1, int user2)
  464. {
  465. struct ipa3_desc *desc = (struct ipa3_desc *)user1;
  466. if (WARN(!desc, "desc is NULL"))
  467. return;
  468. IPADBG_LOW("got ack for cmd=%d\n", desc->opcode);
  469. complete(&desc->xfer_done);
  470. }
  471. /**
  472. * ipa3_transport_irq_cmd_ack_free - callback function which will be
  473. * called by the transport driver after an immediate command is complete.
  474. * This function will also free the completion object once it is done.
  475. * @tag_comp: pointer to the completion object
  476. * @ignored: parameter not used
  477. *
  478. * Complete the immediate commands completion object, this will release the
  479. * thread which waits on this completion object (ipa3_send_cmd())
  480. */
  481. static void ipa3_transport_irq_cmd_ack_free(void *tag_comp, int ignored)
  482. {
  483. struct ipa3_tag_completion *comp = tag_comp;
  484. if (!comp) {
  485. IPAERR("comp is NULL\n");
  486. return;
  487. }
  488. complete(&comp->comp);
  489. if (atomic_dec_return(&comp->cnt) == 0)
  490. kfree(comp);
  491. }
  492. /**
  493. * ipa3_send_cmd - send immediate commands
  494. * @num_desc: number of descriptors within the desc struct
  495. * @descr: descriptor structure
  496. *
  497. * Function will block till command gets ACK from IPA HW, caller needs
  498. * to free any resources it allocated after function returns
  499. * The callback in ipa3_desc should not be set by the caller
  500. * for this function.
  501. */
  502. int ipa3_send_cmd(u16 num_desc, struct ipa3_desc *descr)
  503. {
  504. struct ipa3_desc *desc;
  505. int i, result = 0;
  506. struct ipa3_sys_context *sys;
  507. int ep_idx;
  508. for (i = 0; i < num_desc; i++)
  509. IPADBG("sending imm cmd %d\n", descr[i].opcode);
  510. ep_idx = ipa3_get_ep_mapping(IPA_CLIENT_APPS_CMD_PROD);
  511. if (-1 == ep_idx) {
  512. IPAERR("Client %u is not mapped\n",
  513. IPA_CLIENT_APPS_CMD_PROD);
  514. return -EFAULT;
  515. }
  516. sys = ipa3_ctx->ep[ep_idx].sys;
  517. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  518. if (num_desc == 1) {
  519. init_completion(&descr->xfer_done);
  520. if (descr->callback || descr->user1)
  521. WARN_ON(1);
  522. descr->callback = ipa3_transport_irq_cmd_ack;
  523. descr->user1 = descr;
  524. if (ipa3_send_one(sys, descr, true)) {
  525. IPAERR("fail to send immediate command\n");
  526. result = -EFAULT;
  527. goto bail;
  528. }
  529. wait_for_completion(&descr->xfer_done);
  530. } else {
  531. desc = &descr[num_desc - 1];
  532. init_completion(&desc->xfer_done);
  533. if (desc->callback || desc->user1)
  534. WARN_ON(1);
  535. desc->callback = ipa3_transport_irq_cmd_ack;
  536. desc->user1 = desc;
  537. if (ipa3_send(sys, num_desc, descr, true)) {
  538. IPAERR("fail to send multiple immediate command set\n");
  539. result = -EFAULT;
  540. goto bail;
  541. }
  542. wait_for_completion(&desc->xfer_done);
  543. }
  544. bail:
  545. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  546. return result;
  547. }
  548. /**
  549. * ipa3_send_cmd_timeout - send immediate commands with limited time
  550. * waiting for ACK from IPA HW
  551. * @num_desc: number of descriptors within the desc struct
  552. * @descr: descriptor structure
  553. * @timeout: millisecond to wait till get ACK from IPA HW
  554. *
  555. * Function will block till command gets ACK from IPA HW or timeout.
  556. * Caller needs to free any resources it allocated after function returns
  557. * The callback in ipa3_desc should not be set by the caller
  558. * for this function.
  559. */
  560. int ipa3_send_cmd_timeout(u16 num_desc, struct ipa3_desc *descr, u32 timeout)
  561. {
  562. struct ipa3_desc *desc;
  563. int i, result = 0;
  564. struct ipa3_sys_context *sys;
  565. int ep_idx;
  566. int completed;
  567. struct ipa3_tag_completion *comp;
  568. for (i = 0; i < num_desc; i++)
  569. IPADBG("sending imm cmd %d\n", descr[i].opcode);
  570. ep_idx = ipa3_get_ep_mapping(IPA_CLIENT_APPS_CMD_PROD);
  571. if (-1 == ep_idx) {
  572. IPAERR("Client %u is not mapped\n",
  573. IPA_CLIENT_APPS_CMD_PROD);
  574. return -EFAULT;
  575. }
  576. comp = kzalloc(sizeof(*comp), GFP_ATOMIC);
  577. if (!comp)
  578. return -ENOMEM;
  579. init_completion(&comp->comp);
  580. /* completion needs to be released from both here and in ack callback */
  581. atomic_set(&comp->cnt, 2);
  582. sys = ipa3_ctx->ep[ep_idx].sys;
  583. if (num_desc == 1) {
  584. if (descr->callback || descr->user1)
  585. WARN_ON(1);
  586. descr->callback = ipa3_transport_irq_cmd_ack_free;
  587. descr->user1 = comp;
  588. if (ipa3_send_one(sys, descr, true)) {
  589. IPAERR("fail to send immediate command\n");
  590. kfree(comp);
  591. result = -EFAULT;
  592. goto bail;
  593. }
  594. } else {
  595. desc = &descr[num_desc - 1];
  596. if (desc->callback || desc->user1)
  597. WARN_ON(1);
  598. desc->callback = ipa3_transport_irq_cmd_ack_free;
  599. desc->user1 = comp;
  600. if (ipa3_send(sys, num_desc, descr, true)) {
  601. IPAERR("fail to send multiple immediate command set\n");
  602. kfree(comp);
  603. result = -EFAULT;
  604. goto bail;
  605. }
  606. }
  607. completed = wait_for_completion_timeout(
  608. &comp->comp, msecs_to_jiffies(timeout));
  609. if (!completed)
  610. IPADBG("timeout waiting for imm-cmd ACK\n");
  611. if (atomic_dec_return(&comp->cnt) == 0)
  612. kfree(comp);
  613. bail:
  614. return result;
  615. }
  616. /**
  617. * ipa3_handle_rx_core() - The core functionality of packet reception. This
  618. * function is read from multiple code paths.
  619. *
  620. * All the packets on the Rx data path are received on the IPA_A5_LAN_WAN_IN
  621. * endpoint. The function runs as long as there are packets in the pipe.
  622. * For each packet:
  623. * - Disconnect the packet from the system pipe linked list
  624. * - Unmap the packets skb, make it non DMAable
  625. * - Free the packet from the cache
  626. * - Prepare a proper skb
  627. * - Call the endpoints notify function, passing the skb in the parameters
  628. * - Replenish the rx cache
  629. */
  630. static int ipa3_handle_rx_core(struct ipa3_sys_context *sys, bool process_all,
  631. bool in_poll_state)
  632. {
  633. int ret;
  634. int cnt = 0;
  635. struct gsi_chan_xfer_notify notify = { 0 };
  636. while ((in_poll_state ? atomic_read(&sys->curr_polling_state) :
  637. !atomic_read(&sys->curr_polling_state))) {
  638. if (cnt && !process_all)
  639. break;
  640. ret = ipa_poll_gsi_pkt(sys, &notify);
  641. if (ret)
  642. break;
  643. if (IPA_CLIENT_IS_MEMCPY_DMA_CONS(sys->ep->client))
  644. ipa3_dma_memcpy_notify(sys);
  645. else if (IPA_CLIENT_IS_WLAN_CONS(sys->ep->client))
  646. ipa3_wlan_wq_rx_common(sys, &notify);
  647. else
  648. ipa3_wq_rx_common(sys, &notify);
  649. ++cnt;
  650. }
  651. return cnt;
  652. }
  653. /**
  654. * ipa3_rx_switch_to_intr_mode() - Operate the Rx data path in interrupt mode
  655. */
  656. static int ipa3_rx_switch_to_intr_mode(struct ipa3_sys_context *sys)
  657. {
  658. int ret;
  659. atomic_set(&sys->curr_polling_state, 0);
  660. ipa3_dec_release_wakelock();
  661. ret = gsi_config_channel_mode(sys->ep->gsi_chan_hdl,
  662. GSI_CHAN_MODE_CALLBACK);
  663. if ((ret != GSI_STATUS_SUCCESS) &&
  664. !atomic_read(&sys->curr_polling_state)) {
  665. if (ret == -GSI_STATUS_PENDING_IRQ) {
  666. ipa3_inc_acquire_wakelock();
  667. atomic_set(&sys->curr_polling_state, 1);
  668. } else {
  669. IPAERR("Failed to switch to intr mode.\n");
  670. }
  671. }
  672. return ret;
  673. }
  674. /**
  675. * ipa3_handle_rx() - handle packet reception. This function is executed in the
  676. * context of a work queue.
  677. * @work: work struct needed by the work queue
  678. *
  679. * ipa3_handle_rx_core() is run in polling mode. After all packets has been
  680. * received, the driver switches back to interrupt mode.
  681. */
  682. static void ipa3_handle_rx(struct ipa3_sys_context *sys)
  683. {
  684. int inactive_cycles;
  685. int cnt;
  686. int ret;
  687. ipa_pm_activate_sync(sys->pm_hdl);
  688. start_poll:
  689. inactive_cycles = 0;
  690. do {
  691. cnt = ipa3_handle_rx_core(sys, true, true);
  692. if (cnt == 0)
  693. inactive_cycles++;
  694. else
  695. inactive_cycles = 0;
  696. trace_idle_sleep_enter3(sys->ep->client);
  697. usleep_range(POLLING_MIN_SLEEP_RX, POLLING_MAX_SLEEP_RX);
  698. trace_idle_sleep_exit3(sys->ep->client);
  699. /*
  700. * if pipe is out of buffers there is no point polling for
  701. * completed descs; release the worker so delayed work can
  702. * run in a timely manner
  703. */
  704. if (sys->len == 0)
  705. break;
  706. } while (inactive_cycles <= POLLING_INACTIVITY_RX);
  707. trace_poll_to_intr3(sys->ep->client);
  708. ret = ipa3_rx_switch_to_intr_mode(sys);
  709. if (ret == -GSI_STATUS_PENDING_IRQ)
  710. goto start_poll;
  711. ipa_pm_deferred_deactivate(sys->pm_hdl);
  712. }
  713. static void ipa3_switch_to_intr_rx_work_func(struct work_struct *work)
  714. {
  715. struct delayed_work *dwork;
  716. struct ipa3_sys_context *sys;
  717. dwork = container_of(work, struct delayed_work, work);
  718. sys = container_of(dwork, struct ipa3_sys_context, switch_to_intr_work);
  719. if (sys->napi_obj) {
  720. /* interrupt mode is done in ipa3_rx_poll context */
  721. ipa_assert();
  722. } else
  723. ipa3_handle_rx(sys);
  724. }
  725. enum hrtimer_restart ipa3_ring_doorbell_timer_fn(struct hrtimer *param)
  726. {
  727. struct ipa3_sys_context *sys = container_of(param,
  728. struct ipa3_sys_context, db_timer);
  729. queue_work(sys->wq, &sys->work);
  730. return HRTIMER_NORESTART;
  731. }
  732. static void ipa_pm_sys_pipe_cb(void *p, enum ipa_pm_cb_event event)
  733. {
  734. struct ipa3_sys_context *sys = (struct ipa3_sys_context *)p;
  735. switch (event) {
  736. case IPA_PM_CLIENT_ACTIVATED:
  737. /*
  738. * this event is ignored as the sync version of activation
  739. * will be used.
  740. */
  741. break;
  742. case IPA_PM_REQUEST_WAKEUP:
  743. /*
  744. * pipe will be unsuspended as part of
  745. * enabling IPA clocks
  746. */
  747. IPADBG("calling wakeup for client %d\n", sys->ep->client);
  748. if (sys->ep->client == IPA_CLIENT_APPS_WAN_CONS) {
  749. IPA_ACTIVE_CLIENTS_INC_SPECIAL("PIPE_SUSPEND_WAN");
  750. usleep_range(SUSPEND_MIN_SLEEP_RX,
  751. SUSPEND_MAX_SLEEP_RX);
  752. IPA_ACTIVE_CLIENTS_DEC_SPECIAL("PIPE_SUSPEND_WAN");
  753. } else if (sys->ep->client == IPA_CLIENT_APPS_LAN_CONS) {
  754. IPA_ACTIVE_CLIENTS_INC_SPECIAL("PIPE_SUSPEND_LAN");
  755. usleep_range(SUSPEND_MIN_SLEEP_RX,
  756. SUSPEND_MAX_SLEEP_RX);
  757. IPA_ACTIVE_CLIENTS_DEC_SPECIAL("PIPE_SUSPEND_LAN");
  758. } else if (sys->ep->client == IPA_CLIENT_ODL_DPL_CONS) {
  759. IPA_ACTIVE_CLIENTS_INC_SPECIAL("PIPE_SUSPEND_ODL");
  760. usleep_range(SUSPEND_MIN_SLEEP_RX,
  761. SUSPEND_MAX_SLEEP_RX);
  762. IPA_ACTIVE_CLIENTS_DEC_SPECIAL("PIPE_SUSPEND_ODL");
  763. } else
  764. IPAERR("Unexpected event %d\n for client %d\n",
  765. event, sys->ep->client);
  766. break;
  767. default:
  768. IPAERR("Unexpected event %d\n for client %d\n",
  769. event, sys->ep->client);
  770. WARN_ON(1);
  771. return;
  772. }
  773. }
  774. /**
  775. * ipa3_setup_sys_pipe() - Setup an IPA GPI pipe and perform
  776. * IPA EP configuration
  777. * @sys_in: [in] input needed to setup the pipe and configure EP
  778. * @clnt_hdl: [out] client handle
  779. *
  780. * - configure the end-point registers with the supplied
  781. * parameters from the user.
  782. * - Creates a GPI connection with IPA.
  783. * - allocate descriptor FIFO
  784. *
  785. * Returns: 0 on success, negative on failure
  786. */
  787. int ipa3_setup_sys_pipe(struct ipa_sys_connect_params *sys_in, u32 *clnt_hdl)
  788. {
  789. struct ipa3_ep_context *ep;
  790. int i, ipa_ep_idx, wan_handle;
  791. int result = -EINVAL;
  792. struct ipahal_reg_coal_qmap_cfg qmap_cfg;
  793. struct ipahal_reg_coal_evict_lru evict_lru;
  794. char buff[IPA_RESOURCE_NAME_MAX];
  795. struct ipa_ep_cfg ep_cfg_copy;
  796. if (sys_in == NULL || clnt_hdl == NULL) {
  797. IPAERR("NULL args\n");
  798. goto fail_gen;
  799. }
  800. if (sys_in->client >= IPA_CLIENT_MAX || sys_in->desc_fifo_sz == 0) {
  801. IPAERR("bad parm client:%d fifo_sz:%d\n",
  802. sys_in->client, sys_in->desc_fifo_sz);
  803. goto fail_gen;
  804. }
  805. ipa_ep_idx = ipa3_get_ep_mapping(sys_in->client);
  806. if (ipa_ep_idx == IPA_EP_NOT_ALLOCATED) {
  807. IPAERR("Invalid client.\n");
  808. goto fail_gen;
  809. }
  810. ep = &ipa3_ctx->ep[ipa_ep_idx];
  811. if (ep->valid == 1) {
  812. IPAERR("EP %d already allocated.\n", ipa_ep_idx);
  813. goto fail_gen;
  814. }
  815. /* save the input config parameters */
  816. if (sys_in->client == IPA_CLIENT_APPS_WAN_COAL_CONS)
  817. ep_cfg_copy = sys_in->ipa_ep_cfg;
  818. IPA_ACTIVE_CLIENTS_INC_EP(sys_in->client);
  819. memset(ep, 0, offsetof(struct ipa3_ep_context, sys));
  820. if (!ep->sys) {
  821. struct ipa_pm_register_params pm_reg;
  822. memset(&pm_reg, 0, sizeof(pm_reg));
  823. ep->sys = kzalloc(sizeof(struct ipa3_sys_context), GFP_KERNEL);
  824. if (!ep->sys) {
  825. IPAERR("failed to sys ctx for client %d\n",
  826. sys_in->client);
  827. result = -ENOMEM;
  828. goto fail_and_disable_clocks;
  829. }
  830. ep->sys->ep = ep;
  831. snprintf(buff, IPA_RESOURCE_NAME_MAX, "ipawq%d",
  832. sys_in->client);
  833. ep->sys->wq = alloc_workqueue(buff,
  834. WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_SYSFS, 1);
  835. if (!ep->sys->wq) {
  836. IPAERR("failed to create wq for client %d\n",
  837. sys_in->client);
  838. result = -EFAULT;
  839. goto fail_wq;
  840. }
  841. snprintf(buff, IPA_RESOURCE_NAME_MAX, "iparepwq%d",
  842. sys_in->client);
  843. ep->sys->repl_wq = alloc_workqueue(buff,
  844. WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_SYSFS, 1);
  845. if (!ep->sys->repl_wq) {
  846. IPAERR("failed to create rep wq for client %d\n",
  847. sys_in->client);
  848. result = -EFAULT;
  849. goto fail_wq2;
  850. }
  851. INIT_LIST_HEAD(&ep->sys->head_desc_list);
  852. INIT_LIST_HEAD(&ep->sys->rcycl_list);
  853. spin_lock_init(&ep->sys->spinlock);
  854. hrtimer_init(&ep->sys->db_timer, CLOCK_MONOTONIC,
  855. HRTIMER_MODE_REL);
  856. ep->sys->db_timer.function = ipa3_ring_doorbell_timer_fn;
  857. /* create IPA PM resources for handling polling mode */
  858. if (IPA_CLIENT_IS_CONS(sys_in->client)) {
  859. pm_reg.name = ipa_clients_strings[sys_in->client];
  860. pm_reg.callback = ipa_pm_sys_pipe_cb;
  861. pm_reg.user_data = ep->sys;
  862. pm_reg.group = IPA_PM_GROUP_APPS;
  863. result = ipa_pm_register(&pm_reg, &ep->sys->pm_hdl);
  864. if (result) {
  865. IPAERR("failed to create IPA PM client %d\n",
  866. result);
  867. goto fail_pm;
  868. }
  869. if (IPA_CLIENT_IS_APPS_CONS(sys_in->client)) {
  870. result = ipa_pm_associate_ipa_cons_to_client(
  871. ep->sys->pm_hdl, sys_in->client);
  872. if (result) {
  873. IPAERR("failed to associate\n");
  874. goto fail_gen2;
  875. }
  876. }
  877. result = ipa_pm_set_throughput(ep->sys->pm_hdl,
  878. IPA_APPS_BW_FOR_PM);
  879. if (result) {
  880. IPAERR("failed to set profile IPA PM client\n");
  881. goto fail_gen2;
  882. }
  883. }
  884. } else {
  885. memset(ep->sys, 0, offsetof(struct ipa3_sys_context, ep));
  886. }
  887. atomic_set(&ep->sys->xmit_eot_cnt, 0);
  888. tasklet_init(&ep->sys->tasklet, ipa3_tasklet_write_done,
  889. (unsigned long) ep->sys);
  890. ep->skip_ep_cfg = sys_in->skip_ep_cfg;
  891. if (ipa3_assign_policy(sys_in, ep->sys)) {
  892. IPAERR("failed to sys ctx for client %d\n", sys_in->client);
  893. result = -ENOMEM;
  894. goto fail_gen2;
  895. }
  896. ep->valid = 1;
  897. ep->client = sys_in->client;
  898. ep->client_notify = sys_in->notify;
  899. ep->sys->napi_obj = sys_in->napi_obj;
  900. ep->priv = sys_in->priv;
  901. ep->keep_ipa_awake = sys_in->keep_ipa_awake;
  902. atomic_set(&ep->avail_fifo_desc,
  903. ((sys_in->desc_fifo_sz / IPA_FIFO_ELEMENT_SIZE) - 1));
  904. if (ep->status.status_en && IPA_CLIENT_IS_CONS(ep->client) &&
  905. ep->sys->status_stat == NULL) {
  906. ep->sys->status_stat =
  907. kzalloc(sizeof(struct ipa3_status_stats), GFP_KERNEL);
  908. if (!ep->sys->status_stat)
  909. goto fail_gen2;
  910. }
  911. if (!ep->skip_ep_cfg) {
  912. if (ipa3_cfg_ep(ipa_ep_idx, &sys_in->ipa_ep_cfg)) {
  913. IPAERR("fail to configure EP.\n");
  914. goto fail_gen2;
  915. }
  916. if (ipa3_cfg_ep_status(ipa_ep_idx, &ep->status)) {
  917. IPAERR("fail to configure status of EP.\n");
  918. goto fail_gen2;
  919. }
  920. IPADBG("ep %d configuration successful\n", ipa_ep_idx);
  921. } else {
  922. IPADBG("skipping ep %d configuration\n", ipa_ep_idx);
  923. }
  924. result = ipa_gsi_setup_channel(sys_in, ep);
  925. if (result) {
  926. IPAERR("Failed to setup GSI channel\n");
  927. goto fail_gen2;
  928. }
  929. *clnt_hdl = ipa_ep_idx;
  930. if (ep->sys->repl_hdlr == ipa3_fast_replenish_rx_cache) {
  931. ep->sys->repl = kzalloc(sizeof(*ep->sys->repl), GFP_KERNEL);
  932. if (!ep->sys->repl) {
  933. IPAERR("failed to alloc repl for client %d\n",
  934. sys_in->client);
  935. result = -ENOMEM;
  936. goto fail_gen2;
  937. }
  938. atomic_set(&ep->sys->repl->pending, 0);
  939. ep->sys->repl->capacity = ep->sys->rx_pool_sz + 1;
  940. ep->sys->repl->cache = kcalloc(ep->sys->repl->capacity,
  941. sizeof(void *), GFP_KERNEL);
  942. if (!ep->sys->repl->cache) {
  943. IPAERR("ep=%d fail to alloc repl cache\n", ipa_ep_idx);
  944. ep->sys->repl_hdlr = ipa3_replenish_rx_cache;
  945. ep->sys->repl->capacity = 0;
  946. } else {
  947. atomic_set(&ep->sys->repl->head_idx, 0);
  948. atomic_set(&ep->sys->repl->tail_idx, 0);
  949. ipa3_wq_repl_rx(&ep->sys->repl_work);
  950. }
  951. }
  952. if (IPA_CLIENT_IS_CONS(sys_in->client)) {
  953. ipa3_replenish_rx_cache(ep->sys);
  954. for (i = 0; i < GSI_VEID_MAX; i++)
  955. INIT_LIST_HEAD(&ep->sys->pending_pkts[i]);
  956. }
  957. if (IPA_CLIENT_IS_WLAN_CONS(sys_in->client)) {
  958. ipa3_alloc_wlan_rx_common_cache(IPA_WLAN_COMM_RX_POOL_LOW);
  959. atomic_inc(&ipa3_ctx->wc_memb.active_clnt_cnt);
  960. }
  961. ipa3_ctx->skip_ep_cfg_shadow[ipa_ep_idx] = ep->skip_ep_cfg;
  962. if (!ep->skip_ep_cfg && IPA_CLIENT_IS_PROD(sys_in->client)) {
  963. if (ipa3_ctx->modem_cfg_emb_pipe_flt &&
  964. sys_in->client == IPA_CLIENT_APPS_WAN_PROD)
  965. IPADBG("modem cfg emb pipe flt\n");
  966. else
  967. ipa3_install_dflt_flt_rules(ipa_ep_idx);
  968. }
  969. result = ipa3_enable_data_path(ipa_ep_idx);
  970. if (result) {
  971. IPAERR("enable data path failed res=%d ep=%d.\n", result,
  972. ipa_ep_idx);
  973. goto fail_repl;
  974. }
  975. result = gsi_start_channel(ep->gsi_chan_hdl);
  976. if (result != GSI_STATUS_SUCCESS) {
  977. IPAERR("gsi_start_channel failed res=%d ep=%d.\n", result,
  978. ipa_ep_idx);
  979. goto fail_gen3;
  980. }
  981. if (!ep->keep_ipa_awake)
  982. IPA_ACTIVE_CLIENTS_DEC_EP(sys_in->client);
  983. IPADBG("client %d (ep: %d) connected sys=%pK\n", sys_in->client,
  984. ipa_ep_idx, ep->sys);
  985. /* configure the registers and setup the default pipe */
  986. if (sys_in->client == IPA_CLIENT_APPS_WAN_COAL_CONS) {
  987. evict_lru.coal_vp_lru_thrshld = 0;
  988. evict_lru.coal_eviction_en = true;
  989. ipahal_write_reg_fields(IPA_COAL_EVICT_LRU, &evict_lru);
  990. qmap_cfg.mux_id_byte_sel = IPA_QMAP_ID_BYTE;
  991. ipahal_write_reg_fields(IPA_COAL_QMAP_CFG, &qmap_cfg);
  992. sys_in->client = IPA_CLIENT_APPS_WAN_CONS;
  993. sys_in->ipa_ep_cfg = ep_cfg_copy;
  994. result = ipa3_setup_sys_pipe(sys_in, &wan_handle);
  995. if (result) {
  996. IPAERR("failed to setup default coalescing pipe\n");
  997. goto fail_repl;
  998. }
  999. }
  1000. return 0;
  1001. fail_gen3:
  1002. ipa3_disable_data_path(ipa_ep_idx);
  1003. fail_repl:
  1004. ep->sys->repl_hdlr = ipa3_replenish_rx_cache;
  1005. ep->sys->repl->capacity = 0;
  1006. kfree(ep->sys->repl);
  1007. fail_gen2:
  1008. ipa_pm_deregister(ep->sys->pm_hdl);
  1009. fail_pm:
  1010. destroy_workqueue(ep->sys->repl_wq);
  1011. fail_wq2:
  1012. destroy_workqueue(ep->sys->wq);
  1013. fail_wq:
  1014. kfree(ep->sys);
  1015. memset(&ipa3_ctx->ep[ipa_ep_idx], 0, sizeof(struct ipa3_ep_context));
  1016. fail_and_disable_clocks:
  1017. IPA_ACTIVE_CLIENTS_DEC_EP(sys_in->client);
  1018. fail_gen:
  1019. return result;
  1020. }
  1021. /**
  1022. * ipa3_teardown_sys_pipe() - Teardown the GPI pipe and cleanup IPA EP
  1023. * @clnt_hdl: [in] the handle obtained from ipa3_setup_sys_pipe
  1024. *
  1025. * Returns: 0 on success, negative on failure
  1026. */
  1027. int ipa3_teardown_sys_pipe(u32 clnt_hdl)
  1028. {
  1029. struct ipa3_ep_context *ep;
  1030. int empty;
  1031. int result;
  1032. int i;
  1033. if (clnt_hdl >= ipa3_ctx->ipa_num_pipes ||
  1034. ipa3_ctx->ep[clnt_hdl].valid == 0) {
  1035. IPAERR("bad parm.\n");
  1036. return -EINVAL;
  1037. }
  1038. ep = &ipa3_ctx->ep[clnt_hdl];
  1039. if (!ep->keep_ipa_awake)
  1040. IPA_ACTIVE_CLIENTS_INC_EP(ipa3_get_client_mapping(clnt_hdl));
  1041. ipa3_disable_data_path(clnt_hdl);
  1042. if (IPA_CLIENT_IS_PROD(ep->client)) {
  1043. do {
  1044. spin_lock_bh(&ep->sys->spinlock);
  1045. empty = list_empty(&ep->sys->head_desc_list);
  1046. spin_unlock_bh(&ep->sys->spinlock);
  1047. if (!empty)
  1048. usleep_range(95, 105);
  1049. else
  1050. break;
  1051. } while (1);
  1052. }
  1053. /* channel stop might fail on timeout if IPA is busy */
  1054. for (i = 0; i < IPA_GSI_CHANNEL_STOP_MAX_RETRY; i++) {
  1055. result = ipa3_stop_gsi_channel(clnt_hdl);
  1056. if (result == GSI_STATUS_SUCCESS)
  1057. break;
  1058. if (result != -GSI_STATUS_AGAIN &&
  1059. result != -GSI_STATUS_TIMED_OUT)
  1060. break;
  1061. }
  1062. if (result != GSI_STATUS_SUCCESS) {
  1063. IPAERR("GSI stop chan err: %d.\n", result);
  1064. ipa_assert();
  1065. return result;
  1066. }
  1067. if (ep->sys->napi_obj) {
  1068. do {
  1069. usleep_range(95, 105);
  1070. } while (atomic_read(&ep->sys->curr_polling_state));
  1071. }
  1072. if (IPA_CLIENT_IS_CONS(ep->client))
  1073. cancel_delayed_work_sync(&ep->sys->replenish_rx_work);
  1074. flush_workqueue(ep->sys->wq);
  1075. /* tear down the default pipe before we reset the channel*/
  1076. if (ep->client == IPA_CLIENT_APPS_WAN_COAL_CONS) {
  1077. i = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_CONS);
  1078. if (i == IPA_EP_NOT_ALLOCATED) {
  1079. IPAERR("failed to get idx");
  1080. return i;
  1081. }
  1082. result = ipa3_teardown_coal_def_pipe(i);
  1083. if (result) {
  1084. IPAERR("failed to teardown default coal pipe\n");
  1085. return result;
  1086. }
  1087. }
  1088. result = ipa3_reset_gsi_channel(clnt_hdl);
  1089. if (result != GSI_STATUS_SUCCESS) {
  1090. IPAERR("Failed to reset chan: %d.\n", result);
  1091. ipa_assert();
  1092. return result;
  1093. }
  1094. dma_free_coherent(ipa3_ctx->pdev,
  1095. ep->gsi_mem_info.chan_ring_len,
  1096. ep->gsi_mem_info.chan_ring_base_vaddr,
  1097. ep->gsi_mem_info.chan_ring_base_addr);
  1098. result = gsi_dealloc_channel(ep->gsi_chan_hdl);
  1099. if (result != GSI_STATUS_SUCCESS) {
  1100. IPAERR("Failed to dealloc chan: %d.\n", result);
  1101. ipa_assert();
  1102. return result;
  1103. }
  1104. /* free event ring only when it is present */
  1105. if (ep->sys->use_comm_evt_ring) {
  1106. ipa3_ctx->gsi_evt_comm_ring_rem +=
  1107. ep->gsi_mem_info.chan_ring_len;
  1108. } else if (ep->gsi_evt_ring_hdl != ~0) {
  1109. result = gsi_reset_evt_ring(ep->gsi_evt_ring_hdl);
  1110. if (WARN(result != GSI_STATUS_SUCCESS, "reset evt %d", result))
  1111. return result;
  1112. dma_free_coherent(ipa3_ctx->pdev,
  1113. ep->gsi_mem_info.evt_ring_len,
  1114. ep->gsi_mem_info.evt_ring_base_vaddr,
  1115. ep->gsi_mem_info.evt_ring_base_addr);
  1116. result = gsi_dealloc_evt_ring(ep->gsi_evt_ring_hdl);
  1117. if (WARN(result != GSI_STATUS_SUCCESS, "deall evt %d", result))
  1118. return result;
  1119. }
  1120. if (ep->sys->repl_wq)
  1121. flush_workqueue(ep->sys->repl_wq);
  1122. if (IPA_CLIENT_IS_CONS(ep->client))
  1123. ipa3_cleanup_rx(ep->sys);
  1124. if (!ep->skip_ep_cfg && IPA_CLIENT_IS_PROD(ep->client)) {
  1125. if (ipa3_ctx->modem_cfg_emb_pipe_flt &&
  1126. ep->client == IPA_CLIENT_APPS_WAN_PROD)
  1127. IPADBG("modem cfg emb pipe flt\n");
  1128. else
  1129. ipa3_delete_dflt_flt_rules(clnt_hdl);
  1130. }
  1131. if (IPA_CLIENT_IS_WLAN_CONS(ep->client))
  1132. atomic_dec(&ipa3_ctx->wc_memb.active_clnt_cnt);
  1133. memset(&ep->wstats, 0, sizeof(struct ipa3_wlan_stats));
  1134. if (!atomic_read(&ipa3_ctx->wc_memb.active_clnt_cnt))
  1135. ipa3_cleanup_wlan_rx_common_cache();
  1136. ep->valid = 0;
  1137. IPA_ACTIVE_CLIENTS_DEC_EP(ipa3_get_client_mapping(clnt_hdl));
  1138. IPADBG("client (ep: %d) disconnected\n", clnt_hdl);
  1139. return 0;
  1140. }
  1141. /**
  1142. * ipa3_teardown_coal_def_pipe() - Teardown the APPS_WAN_COAL_CONS
  1143. * default GPI pipe and cleanup IPA EP
  1144. * called after the coalesced pipe is destroyed.
  1145. * @clnt_hdl: [in] the handle obtained from ipa3_setup_sys_pipe
  1146. *
  1147. * Returns: 0 on success, negative on failure
  1148. */
  1149. static int ipa3_teardown_coal_def_pipe(u32 clnt_hdl)
  1150. {
  1151. struct ipa3_ep_context *ep;
  1152. int result;
  1153. int i;
  1154. ep = &ipa3_ctx->ep[clnt_hdl];
  1155. ipa3_disable_data_path(clnt_hdl);
  1156. /* channel stop might fail on timeout if IPA is busy */
  1157. for (i = 0; i < IPA_GSI_CHANNEL_STOP_MAX_RETRY; i++) {
  1158. result = ipa3_stop_gsi_channel(clnt_hdl);
  1159. if (result == GSI_STATUS_SUCCESS)
  1160. break;
  1161. if (result != -GSI_STATUS_AGAIN &&
  1162. result != -GSI_STATUS_TIMED_OUT)
  1163. break;
  1164. }
  1165. if (result != GSI_STATUS_SUCCESS) {
  1166. IPAERR("GSI stop chan err: %d.\n", result);
  1167. ipa_assert();
  1168. return result;
  1169. }
  1170. result = ipa3_reset_gsi_channel(clnt_hdl);
  1171. if (result != GSI_STATUS_SUCCESS) {
  1172. IPAERR("Failed to reset chan: %d.\n", result);
  1173. ipa_assert();
  1174. return result;
  1175. }
  1176. dma_free_coherent(ipa3_ctx->pdev,
  1177. ep->gsi_mem_info.chan_ring_len,
  1178. ep->gsi_mem_info.chan_ring_base_vaddr,
  1179. ep->gsi_mem_info.chan_ring_base_addr);
  1180. result = gsi_dealloc_channel(ep->gsi_chan_hdl);
  1181. if (result != GSI_STATUS_SUCCESS) {
  1182. IPAERR("Failed to dealloc chan: %d.\n", result);
  1183. ipa_assert();
  1184. return result;
  1185. }
  1186. if (IPA_CLIENT_IS_CONS(ep->client))
  1187. cancel_delayed_work_sync(&ep->sys->replenish_rx_work);
  1188. flush_workqueue(ep->sys->wq);
  1189. if (ep->sys->repl_wq)
  1190. flush_workqueue(ep->sys->repl_wq);
  1191. if (IPA_CLIENT_IS_CONS(ep->client))
  1192. ipa3_cleanup_rx(ep->sys);
  1193. ep->valid = 0;
  1194. IPADBG("client (ep: %d) disconnected\n", clnt_hdl);
  1195. return 0;
  1196. }
  1197. /**
  1198. * ipa3_tx_comp_usr_notify_release() - Callback function which will call the
  1199. * user supplied callback function to release the skb, or release it on
  1200. * its own if no callback function was supplied.
  1201. * @user1
  1202. * @user2
  1203. *
  1204. * This notified callback is for the destination client.
  1205. */
  1206. static void ipa3_tx_comp_usr_notify_release(void *user1, int user2)
  1207. {
  1208. struct sk_buff *skb = (struct sk_buff *)user1;
  1209. int ep_idx = user2;
  1210. IPADBG_LOW("skb=%pK ep=%d\n", skb, ep_idx);
  1211. IPA_STATS_INC_CNT(ipa3_ctx->stats.tx_pkts_compl);
  1212. if (ipa3_ctx->ep[ep_idx].client_notify)
  1213. ipa3_ctx->ep[ep_idx].client_notify(ipa3_ctx->ep[ep_idx].priv,
  1214. IPA_WRITE_DONE, (unsigned long)skb);
  1215. else
  1216. dev_kfree_skb_any(skb);
  1217. }
  1218. void ipa3_tx_cmd_comp(void *user1, int user2)
  1219. {
  1220. ipahal_destroy_imm_cmd(user1);
  1221. }
  1222. /**
  1223. * ipa3_tx_dp() - Data-path tx handler
  1224. * @dst: [in] which IPA destination to route tx packets to
  1225. * @skb: [in] the packet to send
  1226. * @metadata: [in] TX packet meta-data
  1227. *
  1228. * Data-path tx handler, this is used for both SW data-path which by-passes most
  1229. * IPA HW blocks AND the regular HW data-path for WLAN AMPDU traffic only. If
  1230. * dst is a "valid" CONS type, then SW data-path is used. If dst is the
  1231. * WLAN_AMPDU PROD type, then HW data-path for WLAN AMPDU is used. Anything else
  1232. * is an error. For errors, client needs to free the skb as needed. For success,
  1233. * IPA driver will later invoke client callback if one was supplied. That
  1234. * callback should free the skb. If no callback supplied, IPA driver will free
  1235. * the skb internally
  1236. *
  1237. * The function will use two descriptors for this send command
  1238. * (for A5_WLAN_AMPDU_PROD only one desciprtor will be sent),
  1239. * the first descriptor will be used to inform the IPA hardware that
  1240. * apps need to push data into the IPA (IP_PACKET_INIT immediate command).
  1241. * Once this send was done from transport point-of-view the IPA driver will
  1242. * get notified by the supplied callback.
  1243. *
  1244. * Returns: 0 on success, negative on failure
  1245. */
  1246. int ipa3_tx_dp(enum ipa_client_type dst, struct sk_buff *skb,
  1247. struct ipa_tx_meta *meta)
  1248. {
  1249. struct ipa3_desc *desc;
  1250. struct ipa3_desc _desc[3];
  1251. int dst_ep_idx;
  1252. struct ipahal_imm_cmd_pyld *cmd_pyld = NULL;
  1253. struct ipa3_sys_context *sys;
  1254. int src_ep_idx;
  1255. int num_frags, f;
  1256. const struct ipa_gsi_ep_config *gsi_ep;
  1257. int data_idx;
  1258. unsigned int max_desc;
  1259. if (unlikely(!ipa3_ctx)) {
  1260. IPAERR("IPA3 driver was not initialized\n");
  1261. return -EINVAL;
  1262. }
  1263. if (skb->len == 0) {
  1264. IPAERR("packet size is 0\n");
  1265. return -EINVAL;
  1266. }
  1267. /*
  1268. * USB_CONS: PKT_INIT ep_idx = dst pipe
  1269. * Q6_CONS: PKT_INIT ep_idx = sender pipe
  1270. * A5_LAN_WAN_PROD: HW path ep_idx = sender pipe
  1271. *
  1272. * LAN TX: all PKT_INIT
  1273. * WAN TX: PKT_INIT (cmd) + HW (data)
  1274. *
  1275. */
  1276. if (IPA_CLIENT_IS_CONS(dst)) {
  1277. src_ep_idx = ipa3_get_ep_mapping(IPA_CLIENT_APPS_LAN_PROD);
  1278. if (-1 == src_ep_idx) {
  1279. IPAERR("Client %u is not mapped\n",
  1280. IPA_CLIENT_APPS_LAN_PROD);
  1281. goto fail_gen;
  1282. }
  1283. dst_ep_idx = ipa3_get_ep_mapping(dst);
  1284. } else {
  1285. src_ep_idx = ipa3_get_ep_mapping(dst);
  1286. if (-1 == src_ep_idx) {
  1287. IPAERR("Client %u is not mapped\n", dst);
  1288. goto fail_gen;
  1289. }
  1290. if (meta && meta->pkt_init_dst_ep_valid)
  1291. dst_ep_idx = meta->pkt_init_dst_ep;
  1292. else
  1293. dst_ep_idx = -1;
  1294. }
  1295. sys = ipa3_ctx->ep[src_ep_idx].sys;
  1296. if (!sys || !sys->ep->valid) {
  1297. IPAERR_RL("pipe %d not valid\n", src_ep_idx);
  1298. goto fail_pipe_not_valid;
  1299. }
  1300. num_frags = skb_shinfo(skb)->nr_frags;
  1301. /*
  1302. * make sure TLV FIFO supports the needed frags.
  1303. * 2 descriptors are needed for IP_PACKET_INIT and TAG_STATUS.
  1304. * 1 descriptor needed for the linear portion of skb.
  1305. */
  1306. gsi_ep = ipa3_get_gsi_ep_info(ipa3_ctx->ep[src_ep_idx].client);
  1307. if (unlikely(gsi_ep == NULL)) {
  1308. IPAERR("failed to get EP %d GSI info\n", src_ep_idx);
  1309. goto fail_gen;
  1310. }
  1311. max_desc = gsi_ep->ipa_if_tlv;
  1312. if (gsi_ep->prefetch_mode == GSI_SMART_PRE_FETCH ||
  1313. gsi_ep->prefetch_mode == GSI_FREE_PRE_FETCH)
  1314. max_desc -= gsi_ep->prefetch_threshold;
  1315. if (num_frags + 3 > max_desc) {
  1316. if (skb_linearize(skb)) {
  1317. IPAERR("Failed to linear skb with %d frags\n",
  1318. num_frags);
  1319. goto fail_gen;
  1320. }
  1321. num_frags = 0;
  1322. }
  1323. if (num_frags) {
  1324. /* 1 desc for tag to resolve status out-of-order issue;
  1325. * 1 desc is needed for the linear portion of skb;
  1326. * 1 desc may be needed for the PACKET_INIT;
  1327. * 1 desc for each frag
  1328. */
  1329. desc = kzalloc(sizeof(*desc) * (num_frags + 3), GFP_ATOMIC);
  1330. if (!desc) {
  1331. IPAERR("failed to alloc desc array\n");
  1332. goto fail_gen;
  1333. }
  1334. } else {
  1335. memset(_desc, 0, 3 * sizeof(struct ipa3_desc));
  1336. desc = &_desc[0];
  1337. }
  1338. if (dst_ep_idx != -1) {
  1339. int skb_idx;
  1340. /* SW data path */
  1341. data_idx = 0;
  1342. if (sys->policy == IPA_POLICY_NOINTR_MODE) {
  1343. /*
  1344. * For non-interrupt mode channel (where there is no
  1345. * event ring) TAG STATUS are used for completion
  1346. * notification. IPA will generate a status packet with
  1347. * tag info as a result of the TAG STATUS command.
  1348. */
  1349. desc[data_idx].is_tag_status = true;
  1350. data_idx++;
  1351. }
  1352. desc[data_idx].opcode = ipa3_ctx->pkt_init_imm_opcode;
  1353. desc[data_idx].dma_address_valid = true;
  1354. desc[data_idx].dma_address = ipa3_ctx->pkt_init_imm[dst_ep_idx];
  1355. desc[data_idx].type = IPA_IMM_CMD_DESC;
  1356. desc[data_idx].callback = NULL;
  1357. data_idx++;
  1358. desc[data_idx].pyld = skb->data;
  1359. desc[data_idx].len = skb_headlen(skb);
  1360. desc[data_idx].type = IPA_DATA_DESC_SKB;
  1361. desc[data_idx].callback = ipa3_tx_comp_usr_notify_release;
  1362. desc[data_idx].user1 = skb;
  1363. desc[data_idx].user2 = (meta && meta->pkt_init_dst_ep_valid &&
  1364. meta->pkt_init_dst_ep_remote) ?
  1365. src_ep_idx :
  1366. dst_ep_idx;
  1367. if (meta && meta->dma_address_valid) {
  1368. desc[data_idx].dma_address_valid = true;
  1369. desc[data_idx].dma_address = meta->dma_address;
  1370. }
  1371. skb_idx = data_idx;
  1372. data_idx++;
  1373. for (f = 0; f < num_frags; f++) {
  1374. desc[data_idx + f].frag = &skb_shinfo(skb)->frags[f];
  1375. desc[data_idx + f].type = IPA_DATA_DESC_SKB_PAGED;
  1376. desc[data_idx + f].len =
  1377. skb_frag_size(desc[data_idx + f].frag);
  1378. }
  1379. /* don't free skb till frag mappings are released */
  1380. if (num_frags) {
  1381. desc[data_idx + f - 1].callback =
  1382. desc[skb_idx].callback;
  1383. desc[data_idx + f - 1].user1 = desc[skb_idx].user1;
  1384. desc[data_idx + f - 1].user2 = desc[skb_idx].user2;
  1385. desc[skb_idx].callback = NULL;
  1386. }
  1387. if (ipa3_send(sys, num_frags + data_idx, desc, true)) {
  1388. IPAERR_RL("fail to send skb %pK num_frags %u SWP\n",
  1389. skb, num_frags);
  1390. goto fail_send;
  1391. }
  1392. IPA_STATS_INC_CNT(ipa3_ctx->stats.tx_sw_pkts);
  1393. } else {
  1394. /* HW data path */
  1395. data_idx = 0;
  1396. if (sys->policy == IPA_POLICY_NOINTR_MODE) {
  1397. /*
  1398. * For non-interrupt mode channel (where there is no
  1399. * event ring) TAG STATUS are used for completion
  1400. * notification. IPA will generate a status packet with
  1401. * tag info as a result of the TAG STATUS command.
  1402. */
  1403. desc[data_idx].is_tag_status = true;
  1404. data_idx++;
  1405. }
  1406. desc[data_idx].pyld = skb->data;
  1407. desc[data_idx].len = skb_headlen(skb);
  1408. desc[data_idx].type = IPA_DATA_DESC_SKB;
  1409. desc[data_idx].callback = ipa3_tx_comp_usr_notify_release;
  1410. desc[data_idx].user1 = skb;
  1411. desc[data_idx].user2 = src_ep_idx;
  1412. if (meta && meta->dma_address_valid) {
  1413. desc[data_idx].dma_address_valid = true;
  1414. desc[data_idx].dma_address = meta->dma_address;
  1415. }
  1416. if (num_frags == 0) {
  1417. if (ipa3_send(sys, data_idx + 1, desc, true)) {
  1418. IPAERR("fail to send skb %pK HWP\n", skb);
  1419. goto fail_mem;
  1420. }
  1421. } else {
  1422. for (f = 0; f < num_frags; f++) {
  1423. desc[data_idx+f+1].frag =
  1424. &skb_shinfo(skb)->frags[f];
  1425. desc[data_idx+f+1].type =
  1426. IPA_DATA_DESC_SKB_PAGED;
  1427. desc[data_idx+f+1].len =
  1428. skb_frag_size(desc[data_idx+f+1].frag);
  1429. }
  1430. /* don't free skb till frag mappings are released */
  1431. desc[data_idx+f].callback = desc[data_idx].callback;
  1432. desc[data_idx+f].user1 = desc[data_idx].user1;
  1433. desc[data_idx+f].user2 = desc[data_idx].user2;
  1434. desc[data_idx].callback = NULL;
  1435. if (ipa3_send(sys, num_frags + data_idx + 1,
  1436. desc, true)) {
  1437. IPAERR("fail to send skb %pK num_frags %u\n",
  1438. skb, num_frags);
  1439. goto fail_mem;
  1440. }
  1441. }
  1442. IPA_STATS_INC_CNT(ipa3_ctx->stats.tx_hw_pkts);
  1443. }
  1444. if (num_frags) {
  1445. kfree(desc);
  1446. IPA_STATS_INC_CNT(ipa3_ctx->stats.tx_non_linear);
  1447. }
  1448. return 0;
  1449. fail_send:
  1450. ipahal_destroy_imm_cmd(cmd_pyld);
  1451. fail_mem:
  1452. if (num_frags)
  1453. kfree(desc);
  1454. fail_gen:
  1455. return -EFAULT;
  1456. fail_pipe_not_valid:
  1457. return -EPIPE;
  1458. }
  1459. static void ipa3_wq_handle_rx(struct work_struct *work)
  1460. {
  1461. struct ipa3_sys_context *sys;
  1462. sys = container_of(work, struct ipa3_sys_context, work);
  1463. if (sys->napi_obj) {
  1464. ipa_pm_activate_sync(sys->pm_hdl);
  1465. napi_schedule(sys->napi_obj);
  1466. } else
  1467. ipa3_handle_rx(sys);
  1468. }
  1469. static void ipa3_wq_repl_rx(struct work_struct *work)
  1470. {
  1471. struct ipa3_sys_context *sys;
  1472. void *ptr;
  1473. struct ipa3_rx_pkt_wrapper *rx_pkt;
  1474. gfp_t flag = GFP_KERNEL;
  1475. u32 next;
  1476. u32 curr;
  1477. sys = container_of(work, struct ipa3_sys_context, repl_work);
  1478. atomic_set(&sys->repl->pending, 0);
  1479. curr = atomic_read(&sys->repl->tail_idx);
  1480. begin:
  1481. while (1) {
  1482. next = (curr + 1) % sys->repl->capacity;
  1483. if (next == atomic_read(&sys->repl->head_idx))
  1484. goto fail_kmem_cache_alloc;
  1485. rx_pkt = kmem_cache_zalloc(ipa3_ctx->rx_pkt_wrapper_cache,
  1486. flag);
  1487. if (!rx_pkt)
  1488. goto fail_kmem_cache_alloc;
  1489. INIT_WORK(&rx_pkt->work, ipa3_wq_rx_avail);
  1490. rx_pkt->sys = sys;
  1491. rx_pkt->data.skb = sys->get_skb(sys->rx_buff_sz, flag);
  1492. if (rx_pkt->data.skb == NULL)
  1493. goto fail_skb_alloc;
  1494. ptr = skb_put(rx_pkt->data.skb, sys->rx_buff_sz);
  1495. rx_pkt->data.dma_addr = dma_map_single(ipa3_ctx->pdev, ptr,
  1496. sys->rx_buff_sz,
  1497. DMA_FROM_DEVICE);
  1498. if (dma_mapping_error(ipa3_ctx->pdev, rx_pkt->data.dma_addr)) {
  1499. pr_err_ratelimited("%s dma map fail %pK for %pK sys=%pK\n",
  1500. __func__, (void *)rx_pkt->data.dma_addr,
  1501. ptr, sys);
  1502. goto fail_dma_mapping;
  1503. }
  1504. sys->repl->cache[curr] = rx_pkt;
  1505. curr = next;
  1506. /* ensure write is done before setting tail index */
  1507. mb();
  1508. atomic_set(&sys->repl->tail_idx, next);
  1509. }
  1510. return;
  1511. fail_dma_mapping:
  1512. sys->free_skb(rx_pkt->data.skb);
  1513. fail_skb_alloc:
  1514. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rx_pkt);
  1515. fail_kmem_cache_alloc:
  1516. if (atomic_read(&sys->repl->tail_idx) ==
  1517. atomic_read(&sys->repl->head_idx)) {
  1518. if (sys->ep->client == IPA_CLIENT_APPS_WAN_CONS ||
  1519. sys->ep->client == IPA_CLIENT_APPS_WAN_COAL_CONS)
  1520. IPA_STATS_INC_CNT(ipa3_ctx->stats.wan_repl_rx_empty);
  1521. else if (sys->ep->client == IPA_CLIENT_APPS_LAN_CONS)
  1522. IPA_STATS_INC_CNT(ipa3_ctx->stats.lan_repl_rx_empty);
  1523. pr_err_ratelimited("%s sys=%pK repl ring empty\n",
  1524. __func__, sys);
  1525. goto begin;
  1526. }
  1527. }
  1528. static void ipa3_replenish_wlan_rx_cache(struct ipa3_sys_context *sys)
  1529. {
  1530. struct ipa3_rx_pkt_wrapper *rx_pkt = NULL;
  1531. struct ipa3_rx_pkt_wrapper *tmp;
  1532. int ret;
  1533. struct gsi_xfer_elem gsi_xfer_elem_one;
  1534. u32 rx_len_cached = 0;
  1535. IPADBG_LOW("\n");
  1536. spin_lock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  1537. rx_len_cached = sys->len;
  1538. if (rx_len_cached < sys->rx_pool_sz) {
  1539. list_for_each_entry_safe(rx_pkt, tmp,
  1540. &ipa3_ctx->wc_memb.wlan_comm_desc_list, link) {
  1541. list_del(&rx_pkt->link);
  1542. if (ipa3_ctx->wc_memb.wlan_comm_free_cnt > 0)
  1543. ipa3_ctx->wc_memb.wlan_comm_free_cnt--;
  1544. rx_pkt->len = 0;
  1545. rx_pkt->sys = sys;
  1546. memset(&gsi_xfer_elem_one, 0,
  1547. sizeof(gsi_xfer_elem_one));
  1548. gsi_xfer_elem_one.addr = rx_pkt->data.dma_addr;
  1549. gsi_xfer_elem_one.len = IPA_WLAN_RX_BUFF_SZ;
  1550. gsi_xfer_elem_one.flags |= GSI_XFER_FLAG_EOT;
  1551. gsi_xfer_elem_one.flags |= GSI_XFER_FLAG_EOB;
  1552. gsi_xfer_elem_one.type = GSI_XFER_ELEM_DATA;
  1553. gsi_xfer_elem_one.xfer_user_data = rx_pkt;
  1554. ret = gsi_queue_xfer(sys->ep->gsi_chan_hdl, 1,
  1555. &gsi_xfer_elem_one, true);
  1556. if (ret) {
  1557. IPAERR("failed to provide buffer: %d\n", ret);
  1558. goto fail_provide_rx_buffer;
  1559. }
  1560. rx_len_cached = ++sys->len;
  1561. if (rx_len_cached >= sys->rx_pool_sz) {
  1562. spin_unlock_bh(
  1563. &ipa3_ctx->wc_memb.wlan_spinlock);
  1564. return;
  1565. }
  1566. }
  1567. }
  1568. spin_unlock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  1569. if (rx_len_cached < sys->rx_pool_sz &&
  1570. ipa3_ctx->wc_memb.wlan_comm_total_cnt <
  1571. IPA_WLAN_COMM_RX_POOL_HIGH) {
  1572. ipa3_replenish_rx_cache(sys);
  1573. ipa3_ctx->wc_memb.wlan_comm_total_cnt +=
  1574. (sys->rx_pool_sz - rx_len_cached);
  1575. }
  1576. return;
  1577. fail_provide_rx_buffer:
  1578. list_del(&rx_pkt->link);
  1579. spin_unlock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  1580. }
  1581. static void ipa3_cleanup_wlan_rx_common_cache(void)
  1582. {
  1583. struct ipa3_rx_pkt_wrapper *rx_pkt;
  1584. struct ipa3_rx_pkt_wrapper *tmp;
  1585. spin_lock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  1586. list_for_each_entry_safe(rx_pkt, tmp,
  1587. &ipa3_ctx->wc_memb.wlan_comm_desc_list, link) {
  1588. list_del(&rx_pkt->link);
  1589. dma_unmap_single(ipa3_ctx->pdev, rx_pkt->data.dma_addr,
  1590. IPA_WLAN_RX_BUFF_SZ, DMA_FROM_DEVICE);
  1591. dev_kfree_skb_any(rx_pkt->data.skb);
  1592. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rx_pkt);
  1593. ipa3_ctx->wc_memb.wlan_comm_free_cnt--;
  1594. ipa3_ctx->wc_memb.wlan_comm_total_cnt--;
  1595. }
  1596. ipa3_ctx->wc_memb.total_tx_pkts_freed = 0;
  1597. if (ipa3_ctx->wc_memb.wlan_comm_free_cnt != 0)
  1598. IPAERR("wlan comm buff free cnt: %d\n",
  1599. ipa3_ctx->wc_memb.wlan_comm_free_cnt);
  1600. if (ipa3_ctx->wc_memb.wlan_comm_total_cnt != 0)
  1601. IPAERR("wlan comm buff total cnt: %d\n",
  1602. ipa3_ctx->wc_memb.wlan_comm_total_cnt);
  1603. spin_unlock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  1604. }
  1605. static void ipa3_alloc_wlan_rx_common_cache(u32 size)
  1606. {
  1607. void *ptr;
  1608. struct ipa3_rx_pkt_wrapper *rx_pkt;
  1609. int rx_len_cached = 0;
  1610. gfp_t flag = GFP_NOWAIT | __GFP_NOWARN;
  1611. rx_len_cached = ipa3_ctx->wc_memb.wlan_comm_total_cnt;
  1612. while (rx_len_cached < size) {
  1613. rx_pkt = kmem_cache_zalloc(ipa3_ctx->rx_pkt_wrapper_cache,
  1614. flag);
  1615. if (!rx_pkt)
  1616. goto fail_kmem_cache_alloc;
  1617. INIT_LIST_HEAD(&rx_pkt->link);
  1618. INIT_WORK(&rx_pkt->work, ipa3_wq_rx_avail);
  1619. rx_pkt->data.skb =
  1620. ipa3_get_skb_ipa_rx(IPA_WLAN_RX_BUFF_SZ,
  1621. flag);
  1622. if (rx_pkt->data.skb == NULL) {
  1623. IPAERR("failed to alloc skb\n");
  1624. goto fail_skb_alloc;
  1625. }
  1626. ptr = skb_put(rx_pkt->data.skb, IPA_WLAN_RX_BUFF_SZ);
  1627. rx_pkt->data.dma_addr = dma_map_single(ipa3_ctx->pdev, ptr,
  1628. IPA_WLAN_RX_BUFF_SZ, DMA_FROM_DEVICE);
  1629. if (dma_mapping_error(ipa3_ctx->pdev, rx_pkt->data.dma_addr)) {
  1630. IPAERR("dma_map_single failure %pK for %pK\n",
  1631. (void *)rx_pkt->data.dma_addr, ptr);
  1632. goto fail_dma_mapping;
  1633. }
  1634. spin_lock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  1635. list_add_tail(&rx_pkt->link,
  1636. &ipa3_ctx->wc_memb.wlan_comm_desc_list);
  1637. rx_len_cached = ++ipa3_ctx->wc_memb.wlan_comm_total_cnt;
  1638. ipa3_ctx->wc_memb.wlan_comm_free_cnt++;
  1639. spin_unlock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  1640. }
  1641. return;
  1642. fail_dma_mapping:
  1643. dev_kfree_skb_any(rx_pkt->data.skb);
  1644. fail_skb_alloc:
  1645. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rx_pkt);
  1646. fail_kmem_cache_alloc:
  1647. return;
  1648. }
  1649. /**
  1650. * ipa3_replenish_rx_cache() - Replenish the Rx packets cache.
  1651. *
  1652. * The function allocates buffers in the rx_pkt_wrapper_cache cache until there
  1653. * are IPA_RX_POOL_CEIL buffers in the cache.
  1654. * - Allocate a buffer in the cache
  1655. * - Initialized the packets link
  1656. * - Initialize the packets work struct
  1657. * - Allocate the packets socket buffer (skb)
  1658. * - Fill the packets skb with data
  1659. * - Make the packet DMAable
  1660. * - Add the packet to the system pipe linked list
  1661. */
  1662. static void ipa3_replenish_rx_cache(struct ipa3_sys_context *sys)
  1663. {
  1664. void *ptr;
  1665. struct ipa3_rx_pkt_wrapper *rx_pkt;
  1666. int ret;
  1667. int idx = 0;
  1668. int rx_len_cached = 0;
  1669. struct gsi_xfer_elem gsi_xfer_elem_array[IPA_REPL_XFER_MAX];
  1670. gfp_t flag = GFP_NOWAIT | __GFP_NOWARN;
  1671. rx_len_cached = sys->len;
  1672. /* start replenish only when buffers go lower than the threshold */
  1673. if (sys->rx_pool_sz - sys->len < IPA_REPL_XFER_THRESH)
  1674. return;
  1675. while (rx_len_cached < sys->rx_pool_sz) {
  1676. rx_pkt = kmem_cache_zalloc(ipa3_ctx->rx_pkt_wrapper_cache,
  1677. flag);
  1678. if (!rx_pkt)
  1679. goto fail_kmem_cache_alloc;
  1680. INIT_WORK(&rx_pkt->work, ipa3_wq_rx_avail);
  1681. rx_pkt->sys = sys;
  1682. rx_pkt->data.skb = sys->get_skb(sys->rx_buff_sz, flag);
  1683. if (rx_pkt->data.skb == NULL) {
  1684. IPAERR("failed to alloc skb\n");
  1685. goto fail_skb_alloc;
  1686. }
  1687. ptr = skb_put(rx_pkt->data.skb, sys->rx_buff_sz);
  1688. rx_pkt->data.dma_addr = dma_map_single(ipa3_ctx->pdev, ptr,
  1689. sys->rx_buff_sz,
  1690. DMA_FROM_DEVICE);
  1691. if (dma_mapping_error(ipa3_ctx->pdev, rx_pkt->data.dma_addr)) {
  1692. IPAERR("dma_map_single failure %pK for %pK\n",
  1693. (void *)rx_pkt->data.dma_addr, ptr);
  1694. goto fail_dma_mapping;
  1695. }
  1696. gsi_xfer_elem_array[idx].addr = rx_pkt->data.dma_addr;
  1697. gsi_xfer_elem_array[idx].len = sys->rx_buff_sz;
  1698. gsi_xfer_elem_array[idx].flags = GSI_XFER_FLAG_EOT;
  1699. gsi_xfer_elem_array[idx].flags |= GSI_XFER_FLAG_EOB;
  1700. gsi_xfer_elem_array[idx].flags |= GSI_XFER_FLAG_BEI;
  1701. gsi_xfer_elem_array[idx].type = GSI_XFER_ELEM_DATA;
  1702. gsi_xfer_elem_array[idx].xfer_user_data = rx_pkt;
  1703. idx++;
  1704. rx_len_cached++;
  1705. /*
  1706. * gsi_xfer_elem_buffer has a size of IPA_REPL_XFER_MAX.
  1707. * If this size is reached we need to queue the xfers.
  1708. */
  1709. if (idx == IPA_REPL_XFER_MAX) {
  1710. ret = gsi_queue_xfer(sys->ep->gsi_chan_hdl, idx,
  1711. gsi_xfer_elem_array, false);
  1712. if (ret != GSI_STATUS_SUCCESS) {
  1713. /* we don't expect this will happen */
  1714. IPAERR("failed to provide buffer: %d\n", ret);
  1715. WARN_ON(1);
  1716. break;
  1717. }
  1718. idx = 0;
  1719. }
  1720. }
  1721. goto done;
  1722. fail_dma_mapping:
  1723. sys->free_skb(rx_pkt->data.skb);
  1724. fail_skb_alloc:
  1725. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rx_pkt);
  1726. fail_kmem_cache_alloc:
  1727. if (rx_len_cached == 0)
  1728. queue_delayed_work(sys->wq, &sys->replenish_rx_work,
  1729. msecs_to_jiffies(1));
  1730. done:
  1731. /* only ring doorbell once here */
  1732. ret = gsi_queue_xfer(sys->ep->gsi_chan_hdl, idx,
  1733. gsi_xfer_elem_array, true);
  1734. if (ret == GSI_STATUS_SUCCESS) {
  1735. sys->len = rx_len_cached;
  1736. } else {
  1737. /* we don't expect this will happen */
  1738. IPAERR("failed to provide buffer: %d\n", ret);
  1739. WARN_ON(1);
  1740. }
  1741. }
  1742. static void ipa3_replenish_rx_cache_recycle(struct ipa3_sys_context *sys)
  1743. {
  1744. void *ptr;
  1745. struct ipa3_rx_pkt_wrapper *rx_pkt;
  1746. int ret;
  1747. int idx = 0;
  1748. int rx_len_cached = 0;
  1749. struct gsi_xfer_elem gsi_xfer_elem_array[IPA_REPL_XFER_MAX];
  1750. gfp_t flag = GFP_NOWAIT | __GFP_NOWARN;
  1751. /* start replenish only when buffers go lower than the threshold */
  1752. if (sys->rx_pool_sz - sys->len < IPA_REPL_XFER_THRESH)
  1753. return;
  1754. rx_len_cached = sys->len;
  1755. while (rx_len_cached < sys->rx_pool_sz) {
  1756. if (list_empty(&sys->rcycl_list)) {
  1757. rx_pkt = kmem_cache_zalloc(
  1758. ipa3_ctx->rx_pkt_wrapper_cache, flag);
  1759. if (!rx_pkt)
  1760. goto fail_kmem_cache_alloc;
  1761. INIT_WORK(&rx_pkt->work, ipa3_wq_rx_avail);
  1762. rx_pkt->sys = sys;
  1763. rx_pkt->data.skb = sys->get_skb(sys->rx_buff_sz, flag);
  1764. if (rx_pkt->data.skb == NULL) {
  1765. IPAERR("failed to alloc skb\n");
  1766. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache,
  1767. rx_pkt);
  1768. goto fail_kmem_cache_alloc;
  1769. }
  1770. ptr = skb_put(rx_pkt->data.skb, sys->rx_buff_sz);
  1771. rx_pkt->data.dma_addr = dma_map_single(ipa3_ctx->pdev,
  1772. ptr, sys->rx_buff_sz, DMA_FROM_DEVICE);
  1773. if (dma_mapping_error(ipa3_ctx->pdev,
  1774. rx_pkt->data.dma_addr)) {
  1775. IPAERR("dma_map_single failure %pK for %pK\n",
  1776. (void *)rx_pkt->data.dma_addr, ptr);
  1777. goto fail_dma_mapping;
  1778. }
  1779. } else {
  1780. spin_lock_bh(&sys->spinlock);
  1781. rx_pkt = list_first_entry(&sys->rcycl_list,
  1782. struct ipa3_rx_pkt_wrapper, link);
  1783. list_del(&rx_pkt->link);
  1784. spin_unlock_bh(&sys->spinlock);
  1785. ptr = skb_put(rx_pkt->data.skb, sys->rx_buff_sz);
  1786. rx_pkt->data.dma_addr = dma_map_single(ipa3_ctx->pdev,
  1787. ptr, sys->rx_buff_sz, DMA_FROM_DEVICE);
  1788. if (dma_mapping_error(ipa3_ctx->pdev,
  1789. rx_pkt->data.dma_addr)) {
  1790. IPAERR("dma_map_single failure %pK for %pK\n",
  1791. (void *)rx_pkt->data.dma_addr, ptr);
  1792. goto fail_dma_mapping;
  1793. }
  1794. }
  1795. gsi_xfer_elem_array[idx].addr = rx_pkt->data.dma_addr;
  1796. gsi_xfer_elem_array[idx].len = sys->rx_buff_sz;
  1797. gsi_xfer_elem_array[idx].flags = GSI_XFER_FLAG_EOT;
  1798. gsi_xfer_elem_array[idx].flags |= GSI_XFER_FLAG_EOB;
  1799. gsi_xfer_elem_array[idx].flags |= GSI_XFER_FLAG_BEI;
  1800. gsi_xfer_elem_array[idx].type = GSI_XFER_ELEM_DATA;
  1801. gsi_xfer_elem_array[idx].xfer_user_data = rx_pkt;
  1802. idx++;
  1803. rx_len_cached++;
  1804. /*
  1805. * gsi_xfer_elem_buffer has a size of IPA_REPL_XFER_MAX.
  1806. * If this size is reached we need to queue the xfers.
  1807. */
  1808. if (idx == IPA_REPL_XFER_MAX) {
  1809. ret = gsi_queue_xfer(sys->ep->gsi_chan_hdl, idx,
  1810. gsi_xfer_elem_array, false);
  1811. if (ret != GSI_STATUS_SUCCESS) {
  1812. /* we don't expect this will happen */
  1813. IPAERR("failed to provide buffer: %d\n", ret);
  1814. WARN_ON(1);
  1815. break;
  1816. }
  1817. idx = 0;
  1818. }
  1819. }
  1820. goto done;
  1821. fail_dma_mapping:
  1822. spin_lock_bh(&sys->spinlock);
  1823. list_add_tail(&rx_pkt->link, &sys->rcycl_list);
  1824. INIT_LIST_HEAD(&rx_pkt->link);
  1825. spin_unlock_bh(&sys->spinlock);
  1826. fail_kmem_cache_alloc:
  1827. if (rx_len_cached == 0)
  1828. queue_delayed_work(sys->wq, &sys->replenish_rx_work,
  1829. msecs_to_jiffies(1));
  1830. done:
  1831. /* only ring doorbell once here */
  1832. ret = gsi_queue_xfer(sys->ep->gsi_chan_hdl, idx,
  1833. gsi_xfer_elem_array, true);
  1834. if (ret == GSI_STATUS_SUCCESS) {
  1835. sys->len = rx_len_cached;
  1836. } else {
  1837. /* we don't expect this will happen */
  1838. IPAERR("failed to provide buffer: %d\n", ret);
  1839. WARN_ON(1);
  1840. }
  1841. }
  1842. static inline void __trigger_repl_work(struct ipa3_sys_context *sys)
  1843. {
  1844. int tail, head, avail;
  1845. if (atomic_read(&sys->repl->pending))
  1846. return;
  1847. tail = atomic_read(&sys->repl->tail_idx);
  1848. head = atomic_read(&sys->repl->head_idx);
  1849. avail = (tail - head) % sys->repl->capacity;
  1850. if (avail < sys->repl->capacity / 4) {
  1851. atomic_set(&sys->repl->pending, 1);
  1852. queue_work(sys->repl_wq, &sys->repl_work);
  1853. }
  1854. }
  1855. static void ipa3_fast_replenish_rx_cache(struct ipa3_sys_context *sys)
  1856. {
  1857. struct ipa3_rx_pkt_wrapper *rx_pkt;
  1858. int ret;
  1859. int rx_len_cached = 0;
  1860. struct gsi_xfer_elem gsi_xfer_elem_array[IPA_REPL_XFER_MAX];
  1861. u32 curr;
  1862. int idx = 0;
  1863. /* start replenish only when buffers go lower than the threshold */
  1864. if (sys->rx_pool_sz - sys->len < IPA_REPL_XFER_THRESH)
  1865. return;
  1866. spin_lock_bh(&sys->spinlock);
  1867. rx_len_cached = sys->len;
  1868. curr = atomic_read(&sys->repl->head_idx);
  1869. while (rx_len_cached < sys->rx_pool_sz) {
  1870. if (curr == atomic_read(&sys->repl->tail_idx))
  1871. break;
  1872. rx_pkt = sys->repl->cache[curr];
  1873. gsi_xfer_elem_array[idx].addr = rx_pkt->data.dma_addr;
  1874. gsi_xfer_elem_array[idx].len = sys->rx_buff_sz;
  1875. gsi_xfer_elem_array[idx].flags = GSI_XFER_FLAG_EOT;
  1876. gsi_xfer_elem_array[idx].flags |= GSI_XFER_FLAG_EOB;
  1877. gsi_xfer_elem_array[idx].flags |= GSI_XFER_FLAG_BEI;
  1878. gsi_xfer_elem_array[idx].type = GSI_XFER_ELEM_DATA;
  1879. gsi_xfer_elem_array[idx].xfer_user_data = rx_pkt;
  1880. rx_len_cached++;
  1881. curr = (++curr == sys->repl->capacity) ? 0 : curr;
  1882. idx++;
  1883. /*
  1884. * gsi_xfer_elem_buffer has a size of IPA_REPL_XFER_THRESH.
  1885. * If this size is reached we need to queue the xfers.
  1886. */
  1887. if (idx == IPA_REPL_XFER_MAX) {
  1888. ret = gsi_queue_xfer(sys->ep->gsi_chan_hdl, idx,
  1889. gsi_xfer_elem_array, false);
  1890. if (ret != GSI_STATUS_SUCCESS) {
  1891. /* we don't expect this will happen */
  1892. IPAERR("failed to provide buffer: %d\n", ret);
  1893. WARN_ON(1);
  1894. break;
  1895. }
  1896. idx = 0;
  1897. }
  1898. }
  1899. /* only ring doorbell once here */
  1900. ret = gsi_queue_xfer(sys->ep->gsi_chan_hdl, idx,
  1901. gsi_xfer_elem_array, true);
  1902. if (ret == GSI_STATUS_SUCCESS) {
  1903. /* ensure write is done before setting head index */
  1904. mb();
  1905. atomic_set(&sys->repl->head_idx, curr);
  1906. sys->len = rx_len_cached;
  1907. } else {
  1908. /* we don't expect this will happen */
  1909. IPAERR("failed to provide buffer: %d\n", ret);
  1910. WARN_ON(1);
  1911. }
  1912. spin_unlock_bh(&sys->spinlock);
  1913. __trigger_repl_work(sys);
  1914. if (rx_len_cached <= IPA_DEFAULT_SYS_YELLOW_WM) {
  1915. if (sys->ep->client == IPA_CLIENT_APPS_WAN_CONS ||
  1916. sys->ep->client == IPA_CLIENT_APPS_WAN_COAL_CONS)
  1917. IPA_STATS_INC_CNT(ipa3_ctx->stats.wan_rx_empty);
  1918. else if (sys->ep->client == IPA_CLIENT_APPS_LAN_CONS)
  1919. IPA_STATS_INC_CNT(ipa3_ctx->stats.lan_rx_empty);
  1920. else
  1921. WARN_ON(1);
  1922. queue_delayed_work(sys->wq, &sys->replenish_rx_work,
  1923. msecs_to_jiffies(1));
  1924. }
  1925. }
  1926. static void ipa3_replenish_rx_work_func(struct work_struct *work)
  1927. {
  1928. struct delayed_work *dwork;
  1929. struct ipa3_sys_context *sys;
  1930. dwork = container_of(work, struct delayed_work, work);
  1931. sys = container_of(dwork, struct ipa3_sys_context, replenish_rx_work);
  1932. IPA_ACTIVE_CLIENTS_INC_SIMPLE();
  1933. sys->repl_hdlr(sys);
  1934. IPA_ACTIVE_CLIENTS_DEC_SIMPLE();
  1935. }
  1936. /**
  1937. * free_rx_pkt() - function to free the skb and rx_pkt_wrapper
  1938. *
  1939. * @chan_user_data: ipa_sys_context used for skb size and skb_free func
  1940. * @xfer_uder_data: rx_pkt wrapper to be freed
  1941. *
  1942. */
  1943. static void free_rx_pkt(void *chan_user_data, void *xfer_user_data)
  1944. {
  1945. struct ipa3_rx_pkt_wrapper *rx_pkt = (struct ipa3_rx_pkt_wrapper *)
  1946. xfer_user_data;
  1947. struct ipa3_sys_context *sys = (struct ipa3_sys_context *)
  1948. chan_user_data;
  1949. dma_unmap_single(ipa3_ctx->pdev, rx_pkt->data.dma_addr,
  1950. sys->rx_buff_sz, DMA_FROM_DEVICE);
  1951. sys->free_skb(rx_pkt->data.skb);
  1952. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rx_pkt);
  1953. }
  1954. /**
  1955. * ipa3_cleanup_rx() - release RX queue resources
  1956. *
  1957. */
  1958. static void ipa3_cleanup_rx(struct ipa3_sys_context *sys)
  1959. {
  1960. struct ipa3_rx_pkt_wrapper *rx_pkt;
  1961. struct ipa3_rx_pkt_wrapper *r;
  1962. u32 head;
  1963. u32 tail;
  1964. /*
  1965. * buffers not consumed by gsi are cleaned up using cleanup callback
  1966. * provided to gsi
  1967. */
  1968. spin_lock_bh(&sys->spinlock);
  1969. list_for_each_entry_safe(rx_pkt, r,
  1970. &sys->rcycl_list, link) {
  1971. list_del(&rx_pkt->link);
  1972. dma_unmap_single(ipa3_ctx->pdev, rx_pkt->data.dma_addr,
  1973. sys->rx_buff_sz, DMA_FROM_DEVICE);
  1974. sys->free_skb(rx_pkt->data.skb);
  1975. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rx_pkt);
  1976. }
  1977. spin_unlock_bh(&sys->spinlock);
  1978. if (sys->repl) {
  1979. head = atomic_read(&sys->repl->head_idx);
  1980. tail = atomic_read(&sys->repl->tail_idx);
  1981. while (head != tail) {
  1982. rx_pkt = sys->repl->cache[head];
  1983. dma_unmap_single(ipa3_ctx->pdev, rx_pkt->data.dma_addr,
  1984. sys->rx_buff_sz, DMA_FROM_DEVICE);
  1985. sys->free_skb(rx_pkt->data.skb);
  1986. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rx_pkt);
  1987. head = (head + 1) % sys->repl->capacity;
  1988. }
  1989. kfree(sys->repl->cache);
  1990. kfree(sys->repl);
  1991. }
  1992. }
  1993. static struct sk_buff *ipa3_skb_copy_for_client(struct sk_buff *skb, int len)
  1994. {
  1995. struct sk_buff *skb2 = NULL;
  1996. skb2 = __dev_alloc_skb(len + IPA_RX_BUFF_CLIENT_HEADROOM, GFP_KERNEL);
  1997. if (likely(skb2)) {
  1998. /* Set the data pointer */
  1999. skb_reserve(skb2, IPA_RX_BUFF_CLIENT_HEADROOM);
  2000. memcpy(skb2->data, skb->data, len);
  2001. skb2->len = len;
  2002. skb_set_tail_pointer(skb2, len);
  2003. }
  2004. return skb2;
  2005. }
  2006. static int ipa3_lan_rx_pyld_hdlr(struct sk_buff *skb,
  2007. struct ipa3_sys_context *sys)
  2008. {
  2009. int rc = 0;
  2010. struct ipahal_pkt_status status;
  2011. u32 pkt_status_sz;
  2012. struct sk_buff *skb2;
  2013. int pad_len_byte;
  2014. int len;
  2015. unsigned char *buf;
  2016. int src_pipe;
  2017. unsigned int used = *(unsigned int *)skb->cb;
  2018. unsigned int used_align = ALIGN(used, 32);
  2019. unsigned long unused = IPA_GENERIC_RX_BUFF_BASE_SZ - used;
  2020. struct ipa3_tx_pkt_wrapper *tx_pkt = NULL;
  2021. unsigned long ptr;
  2022. IPA_DUMP_BUFF(skb->data, 0, skb->len);
  2023. if (skb->len == 0) {
  2024. IPAERR("ZLT packet arrived to AP\n");
  2025. goto out;
  2026. }
  2027. if (sys->len_partial) {
  2028. IPADBG_LOW("len_partial %d\n", sys->len_partial);
  2029. buf = skb_push(skb, sys->len_partial);
  2030. memcpy(buf, sys->prev_skb->data, sys->len_partial);
  2031. sys->len_partial = 0;
  2032. sys->free_skb(sys->prev_skb);
  2033. sys->prev_skb = NULL;
  2034. goto begin;
  2035. }
  2036. /* this pipe has TX comp (status only) + mux-ed LAN RX data
  2037. * (status+data)
  2038. */
  2039. if (sys->len_rem) {
  2040. IPADBG_LOW("rem %d skb %d pad %d\n", sys->len_rem, skb->len,
  2041. sys->len_pad);
  2042. if (sys->len_rem <= skb->len) {
  2043. if (sys->prev_skb) {
  2044. skb2 = skb_copy_expand(sys->prev_skb, 0,
  2045. sys->len_rem, GFP_KERNEL);
  2046. if (likely(skb2)) {
  2047. memcpy(skb_put(skb2, sys->len_rem),
  2048. skb->data, sys->len_rem);
  2049. skb_trim(skb2,
  2050. skb2->len - sys->len_pad);
  2051. skb2->truesize = skb2->len +
  2052. sizeof(struct sk_buff);
  2053. if (sys->drop_packet)
  2054. dev_kfree_skb_any(skb2);
  2055. else
  2056. sys->ep->client_notify(
  2057. sys->ep->priv,
  2058. IPA_RECEIVE,
  2059. (unsigned long)(skb2));
  2060. } else {
  2061. IPAERR("copy expand failed\n");
  2062. }
  2063. dev_kfree_skb_any(sys->prev_skb);
  2064. }
  2065. skb_pull(skb, sys->len_rem);
  2066. sys->prev_skb = NULL;
  2067. sys->len_rem = 0;
  2068. sys->len_pad = 0;
  2069. } else {
  2070. if (sys->prev_skb) {
  2071. skb2 = skb_copy_expand(sys->prev_skb, 0,
  2072. skb->len, GFP_KERNEL);
  2073. if (likely(skb2)) {
  2074. memcpy(skb_put(skb2, skb->len),
  2075. skb->data, skb->len);
  2076. } else {
  2077. IPAERR("copy expand failed\n");
  2078. }
  2079. dev_kfree_skb_any(sys->prev_skb);
  2080. sys->prev_skb = skb2;
  2081. }
  2082. sys->len_rem -= skb->len;
  2083. goto out;
  2084. }
  2085. }
  2086. begin:
  2087. pkt_status_sz = ipahal_pkt_status_get_size();
  2088. while (skb->len) {
  2089. sys->drop_packet = false;
  2090. IPADBG_LOW("LEN_REM %d\n", skb->len);
  2091. if (skb->len < pkt_status_sz) {
  2092. WARN_ON(sys->prev_skb != NULL);
  2093. IPADBG_LOW("status straddles buffer\n");
  2094. sys->prev_skb = skb_copy(skb, GFP_KERNEL);
  2095. sys->len_partial = skb->len;
  2096. goto out;
  2097. }
  2098. ipahal_pkt_status_parse(skb->data, &status);
  2099. IPADBG_LOW("STATUS opcode=%d src=%d dst=%d len=%d\n",
  2100. status.status_opcode, status.endp_src_idx,
  2101. status.endp_dest_idx, status.pkt_len);
  2102. if (sys->status_stat) {
  2103. sys->status_stat->status[sys->status_stat->curr] =
  2104. status;
  2105. sys->status_stat->curr++;
  2106. if (sys->status_stat->curr == IPA_MAX_STATUS_STAT_NUM)
  2107. sys->status_stat->curr = 0;
  2108. }
  2109. if ((status.status_opcode !=
  2110. IPAHAL_PKT_STATUS_OPCODE_DROPPED_PACKET) &&
  2111. (status.status_opcode !=
  2112. IPAHAL_PKT_STATUS_OPCODE_PACKET) &&
  2113. (status.status_opcode !=
  2114. IPAHAL_PKT_STATUS_OPCODE_SUSPENDED_PACKET) &&
  2115. (status.status_opcode !=
  2116. IPAHAL_PKT_STATUS_OPCODE_PACKET_2ND_PASS)) {
  2117. IPAERR("unsupported opcode(%d)\n",
  2118. status.status_opcode);
  2119. skb_pull(skb, pkt_status_sz);
  2120. continue;
  2121. }
  2122. IPA_STATS_EXCP_CNT(status.exception,
  2123. ipa3_ctx->stats.rx_excp_pkts);
  2124. if (status.endp_dest_idx >= ipa3_ctx->ipa_num_pipes ||
  2125. status.endp_src_idx >= ipa3_ctx->ipa_num_pipes) {
  2126. IPAERR("status fields invalid\n");
  2127. IPAERR("STATUS opcode=%d src=%d dst=%d len=%d\n",
  2128. status.status_opcode, status.endp_src_idx,
  2129. status.endp_dest_idx, status.pkt_len);
  2130. WARN_ON(1);
  2131. /* HW gave an unexpected status */
  2132. ipa_assert();
  2133. }
  2134. if (IPAHAL_PKT_STATUS_MASK_FLAG_VAL(
  2135. IPAHAL_PKT_STATUS_MASK_TAG_VALID_SHFT, &status)) {
  2136. struct ipa3_tag_completion *comp;
  2137. IPADBG_LOW("TAG packet arrived\n");
  2138. if (status.tag_info == IPA_COOKIE) {
  2139. skb_pull(skb, pkt_status_sz);
  2140. if (skb->len < sizeof(comp)) {
  2141. IPAERR("TAG arrived without packet\n");
  2142. goto out;
  2143. }
  2144. memcpy(&comp, skb->data, sizeof(comp));
  2145. skb_pull(skb, sizeof(comp));
  2146. complete(&comp->comp);
  2147. if (atomic_dec_return(&comp->cnt) == 0)
  2148. kfree(comp);
  2149. continue;
  2150. } else {
  2151. ptr = tag_to_pointer_wa(status.tag_info);
  2152. tx_pkt = (struct ipa3_tx_pkt_wrapper *)ptr;
  2153. IPADBG_LOW("tx_pkt recv = %pK\n", tx_pkt);
  2154. }
  2155. }
  2156. if (status.pkt_len == 0) {
  2157. IPADBG_LOW("Skip aggr close status\n");
  2158. skb_pull(skb, pkt_status_sz);
  2159. IPA_STATS_INC_CNT(ipa3_ctx->stats.aggr_close);
  2160. IPA_STATS_DEC_CNT(ipa3_ctx->stats.rx_excp_pkts
  2161. [IPAHAL_PKT_STATUS_EXCEPTION_NONE]);
  2162. continue;
  2163. }
  2164. if (status.endp_dest_idx == (sys->ep - ipa3_ctx->ep)) {
  2165. /* RX data */
  2166. src_pipe = status.endp_src_idx;
  2167. /*
  2168. * A packet which is received back to the AP after
  2169. * there was no route match.
  2170. */
  2171. if (status.exception ==
  2172. IPAHAL_PKT_STATUS_EXCEPTION_NONE &&
  2173. ipahal_is_rule_miss_id(status.rt_rule_id))
  2174. sys->drop_packet = true;
  2175. if (skb->len == pkt_status_sz &&
  2176. status.exception ==
  2177. IPAHAL_PKT_STATUS_EXCEPTION_NONE) {
  2178. WARN_ON(sys->prev_skb != NULL);
  2179. IPADBG_LOW("Ins header in next buffer\n");
  2180. sys->prev_skb = skb_copy(skb, GFP_KERNEL);
  2181. sys->len_partial = skb->len;
  2182. goto out;
  2183. }
  2184. pad_len_byte = ((status.pkt_len + 3) & ~3) -
  2185. status.pkt_len;
  2186. len = status.pkt_len + pad_len_byte;
  2187. IPADBG_LOW("pad %d pkt_len %d len %d\n", pad_len_byte,
  2188. status.pkt_len, len);
  2189. if (status.exception ==
  2190. IPAHAL_PKT_STATUS_EXCEPTION_DEAGGR) {
  2191. IPADBG_LOW(
  2192. "Dropping packet on DeAggr Exception\n");
  2193. sys->drop_packet = true;
  2194. }
  2195. skb2 = ipa3_skb_copy_for_client(skb,
  2196. min(status.pkt_len + pkt_status_sz, skb->len));
  2197. if (likely(skb2)) {
  2198. if (skb->len < len + pkt_status_sz) {
  2199. IPADBG_LOW("SPL skb len %d len %d\n",
  2200. skb->len, len);
  2201. sys->prev_skb = skb2;
  2202. sys->len_rem = len - skb->len +
  2203. pkt_status_sz;
  2204. sys->len_pad = pad_len_byte;
  2205. skb_pull(skb, skb->len);
  2206. } else {
  2207. skb_trim(skb2, status.pkt_len +
  2208. pkt_status_sz);
  2209. IPADBG_LOW("rx avail for %d\n",
  2210. status.endp_dest_idx);
  2211. if (sys->drop_packet) {
  2212. dev_kfree_skb_any(skb2);
  2213. } else if (status.pkt_len >
  2214. IPA_GENERIC_AGGR_BYTE_LIMIT *
  2215. 1024) {
  2216. IPAERR("packet size invalid\n");
  2217. IPAERR("STATUS opcode=%d\n",
  2218. status.status_opcode);
  2219. IPAERR("src=%d dst=%d len=%d\n",
  2220. status.endp_src_idx,
  2221. status.endp_dest_idx,
  2222. status.pkt_len);
  2223. /* Unexpected HW status */
  2224. ipa_assert();
  2225. } else {
  2226. skb2->truesize = skb2->len +
  2227. sizeof(struct sk_buff) +
  2228. (ALIGN(len +
  2229. pkt_status_sz, 32) *
  2230. unused / used_align);
  2231. sys->ep->client_notify(
  2232. sys->ep->priv,
  2233. IPA_RECEIVE,
  2234. (unsigned long)(skb2));
  2235. }
  2236. skb_pull(skb, len + pkt_status_sz);
  2237. }
  2238. } else {
  2239. IPAERR("fail to alloc skb\n");
  2240. if (skb->len < len) {
  2241. sys->prev_skb = NULL;
  2242. sys->len_rem = len - skb->len +
  2243. pkt_status_sz;
  2244. sys->len_pad = pad_len_byte;
  2245. skb_pull(skb, skb->len);
  2246. } else {
  2247. skb_pull(skb, len + pkt_status_sz);
  2248. }
  2249. }
  2250. /* TX comp */
  2251. ipa3_wq_write_done_status(src_pipe, tx_pkt);
  2252. IPADBG_LOW("tx comp imp for %d\n", src_pipe);
  2253. } else {
  2254. /* TX comp */
  2255. ipa3_wq_write_done_status(status.endp_src_idx, tx_pkt);
  2256. IPADBG_LOW("tx comp exp for %d\n",
  2257. status.endp_src_idx);
  2258. skb_pull(skb, pkt_status_sz);
  2259. IPA_STATS_INC_CNT(ipa3_ctx->stats.stat_compl);
  2260. IPA_STATS_DEC_CNT(ipa3_ctx->stats.rx_excp_pkts
  2261. [IPAHAL_PKT_STATUS_EXCEPTION_NONE]);
  2262. }
  2263. tx_pkt = NULL;
  2264. }
  2265. out:
  2266. ipa3_skb_recycle(skb);
  2267. return rc;
  2268. }
  2269. static struct sk_buff *ipa3_join_prev_skb(struct sk_buff *prev_skb,
  2270. struct sk_buff *skb, unsigned int len)
  2271. {
  2272. struct sk_buff *skb2;
  2273. skb2 = skb_copy_expand(prev_skb, 0,
  2274. len, GFP_KERNEL);
  2275. if (likely(skb2)) {
  2276. memcpy(skb_put(skb2, len),
  2277. skb->data, len);
  2278. } else {
  2279. IPAERR("copy expand failed\n");
  2280. skb2 = NULL;
  2281. }
  2282. dev_kfree_skb_any(prev_skb);
  2283. return skb2;
  2284. }
  2285. static void ipa3_wan_rx_handle_splt_pyld(struct sk_buff *skb,
  2286. struct ipa3_sys_context *sys)
  2287. {
  2288. struct sk_buff *skb2;
  2289. IPADBG_LOW("rem %d skb %d\n", sys->len_rem, skb->len);
  2290. if (sys->len_rem <= skb->len) {
  2291. if (sys->prev_skb) {
  2292. skb2 = ipa3_join_prev_skb(sys->prev_skb, skb,
  2293. sys->len_rem);
  2294. if (likely(skb2)) {
  2295. IPADBG_LOW(
  2296. "removing Status element from skb and sending to WAN client");
  2297. skb_pull(skb2, ipahal_pkt_status_get_size());
  2298. skb2->truesize = skb2->len +
  2299. sizeof(struct sk_buff);
  2300. sys->ep->client_notify(sys->ep->priv,
  2301. IPA_RECEIVE,
  2302. (unsigned long)(skb2));
  2303. }
  2304. }
  2305. skb_pull(skb, sys->len_rem);
  2306. sys->prev_skb = NULL;
  2307. sys->len_rem = 0;
  2308. } else {
  2309. if (sys->prev_skb) {
  2310. skb2 = ipa3_join_prev_skb(sys->prev_skb, skb,
  2311. skb->len);
  2312. sys->prev_skb = skb2;
  2313. }
  2314. sys->len_rem -= skb->len;
  2315. skb_pull(skb, skb->len);
  2316. }
  2317. }
  2318. static int ipa3_wan_rx_pyld_hdlr(struct sk_buff *skb,
  2319. struct ipa3_sys_context *sys)
  2320. {
  2321. struct ipahal_pkt_status status;
  2322. unsigned char *skb_data;
  2323. u32 pkt_status_sz;
  2324. struct sk_buff *skb2;
  2325. u16 pkt_len_with_pad;
  2326. u32 qmap_hdr;
  2327. int checksum_trailer_exists;
  2328. int frame_len;
  2329. int ep_idx;
  2330. unsigned int used = *(unsigned int *)skb->cb;
  2331. unsigned int used_align = ALIGN(used, 32);
  2332. unsigned long unused = IPA_GENERIC_RX_BUFF_BASE_SZ - used;
  2333. IPA_DUMP_BUFF(skb->data, 0, skb->len);
  2334. if (skb->len == 0) {
  2335. IPAERR("ZLT\n");
  2336. goto bail;
  2337. }
  2338. if (ipa3_ctx->ipa_client_apps_wan_cons_agg_gro) {
  2339. sys->ep->client_notify(sys->ep->priv,
  2340. IPA_RECEIVE, (unsigned long)(skb));
  2341. return 0;
  2342. }
  2343. if (sys->repl_hdlr == ipa3_replenish_rx_cache_recycle) {
  2344. IPAERR("Recycle should enable only with GRO Aggr\n");
  2345. ipa_assert();
  2346. }
  2347. /*
  2348. * payload splits across 2 buff or more,
  2349. * take the start of the payload from prev_skb
  2350. */
  2351. if (sys->len_rem)
  2352. ipa3_wan_rx_handle_splt_pyld(skb, sys);
  2353. pkt_status_sz = ipahal_pkt_status_get_size();
  2354. while (skb->len) {
  2355. IPADBG_LOW("LEN_REM %d\n", skb->len);
  2356. if (skb->len < pkt_status_sz) {
  2357. IPAERR("status straddles buffer\n");
  2358. WARN_ON(1);
  2359. goto bail;
  2360. }
  2361. ipahal_pkt_status_parse(skb->data, &status);
  2362. skb_data = skb->data;
  2363. IPADBG_LOW("STATUS opcode=%d src=%d dst=%d len=%d\n",
  2364. status.status_opcode, status.endp_src_idx,
  2365. status.endp_dest_idx, status.pkt_len);
  2366. if (sys->status_stat) {
  2367. sys->status_stat->status[sys->status_stat->curr] =
  2368. status;
  2369. sys->status_stat->curr++;
  2370. if (sys->status_stat->curr == IPA_MAX_STATUS_STAT_NUM)
  2371. sys->status_stat->curr = 0;
  2372. }
  2373. if ((status.status_opcode !=
  2374. IPAHAL_PKT_STATUS_OPCODE_DROPPED_PACKET) &&
  2375. (status.status_opcode !=
  2376. IPAHAL_PKT_STATUS_OPCODE_PACKET) &&
  2377. (status.status_opcode !=
  2378. IPAHAL_PKT_STATUS_OPCODE_PACKET_2ND_PASS)) {
  2379. IPAERR("unsupported opcode(%d)\n",
  2380. status.status_opcode);
  2381. skb_pull(skb, pkt_status_sz);
  2382. continue;
  2383. }
  2384. IPA_STATS_INC_CNT(ipa3_ctx->stats.rx_pkts);
  2385. if (status.endp_dest_idx >= ipa3_ctx->ipa_num_pipes ||
  2386. status.endp_src_idx >= ipa3_ctx->ipa_num_pipes) {
  2387. IPAERR("status fields invalid\n");
  2388. WARN_ON(1);
  2389. goto bail;
  2390. }
  2391. if (status.pkt_len == 0) {
  2392. IPADBG_LOW("Skip aggr close status\n");
  2393. skb_pull(skb, pkt_status_sz);
  2394. IPA_STATS_DEC_CNT(ipa3_ctx->stats.rx_pkts);
  2395. IPA_STATS_INC_CNT(ipa3_ctx->stats.wan_aggr_close);
  2396. continue;
  2397. }
  2398. ep_idx = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_CONS);
  2399. if (status.endp_dest_idx != ep_idx) {
  2400. IPAERR("expected endp_dest_idx %d received %d\n",
  2401. ep_idx, status.endp_dest_idx);
  2402. WARN_ON(1);
  2403. goto bail;
  2404. }
  2405. /* RX data */
  2406. if (skb->len == pkt_status_sz) {
  2407. IPAERR("Ins header in next buffer\n");
  2408. WARN_ON(1);
  2409. goto bail;
  2410. }
  2411. qmap_hdr = *(u32 *)(skb_data + pkt_status_sz);
  2412. /*
  2413. * Take the pkt_len_with_pad from the last 2 bytes of the QMAP
  2414. * header
  2415. */
  2416. /*QMAP is BE: convert the pkt_len field from BE to LE*/
  2417. pkt_len_with_pad = ntohs((qmap_hdr>>16) & 0xffff);
  2418. IPADBG_LOW("pkt_len with pad %d\n", pkt_len_with_pad);
  2419. /*get the CHECKSUM_PROCESS bit*/
  2420. checksum_trailer_exists = IPAHAL_PKT_STATUS_MASK_FLAG_VAL(
  2421. IPAHAL_PKT_STATUS_MASK_CKSUM_PROCESS_SHFT, &status);
  2422. IPADBG_LOW("checksum_trailer_exists %d\n",
  2423. checksum_trailer_exists);
  2424. frame_len = pkt_status_sz + IPA_QMAP_HEADER_LENGTH +
  2425. pkt_len_with_pad;
  2426. if (checksum_trailer_exists)
  2427. frame_len += IPA_DL_CHECKSUM_LENGTH;
  2428. IPADBG_LOW("frame_len %d\n", frame_len);
  2429. skb2 = skb_clone(skb, GFP_KERNEL);
  2430. if (likely(skb2)) {
  2431. /*
  2432. * the len of actual data is smaller than expected
  2433. * payload split across 2 buff
  2434. */
  2435. if (skb->len < frame_len) {
  2436. IPADBG_LOW("SPL skb len %d len %d\n",
  2437. skb->len, frame_len);
  2438. sys->prev_skb = skb2;
  2439. sys->len_rem = frame_len - skb->len;
  2440. skb_pull(skb, skb->len);
  2441. } else {
  2442. skb_trim(skb2, frame_len);
  2443. IPADBG_LOW("rx avail for %d\n",
  2444. status.endp_dest_idx);
  2445. IPADBG_LOW(
  2446. "removing Status element from skb and sending to WAN client");
  2447. skb_pull(skb2, pkt_status_sz);
  2448. skb2->truesize = skb2->len +
  2449. sizeof(struct sk_buff) +
  2450. (ALIGN(frame_len, 32) *
  2451. unused / used_align);
  2452. sys->ep->client_notify(sys->ep->priv,
  2453. IPA_RECEIVE, (unsigned long)(skb2));
  2454. skb_pull(skb, frame_len);
  2455. }
  2456. } else {
  2457. IPAERR("fail to clone\n");
  2458. if (skb->len < frame_len) {
  2459. sys->prev_skb = NULL;
  2460. sys->len_rem = frame_len - skb->len;
  2461. skb_pull(skb, skb->len);
  2462. } else {
  2463. skb_pull(skb, frame_len);
  2464. }
  2465. }
  2466. }
  2467. bail:
  2468. sys->free_skb(skb);
  2469. return 0;
  2470. }
  2471. static struct sk_buff *ipa3_get_skb_ipa_rx(unsigned int len, gfp_t flags)
  2472. {
  2473. return __dev_alloc_skb(len, flags);
  2474. }
  2475. static void ipa3_free_skb_rx(struct sk_buff *skb)
  2476. {
  2477. dev_kfree_skb_any(skb);
  2478. }
  2479. void ipa3_lan_rx_cb(void *priv, enum ipa_dp_evt_type evt, unsigned long data)
  2480. {
  2481. struct sk_buff *rx_skb = (struct sk_buff *)data;
  2482. struct ipahal_pkt_status status;
  2483. struct ipa3_ep_context *ep;
  2484. unsigned int src_pipe;
  2485. u32 metadata;
  2486. u8 ucp;
  2487. ipahal_pkt_status_parse(rx_skb->data, &status);
  2488. src_pipe = status.endp_src_idx;
  2489. metadata = status.metadata;
  2490. ucp = status.ucp;
  2491. ep = &ipa3_ctx->ep[src_pipe];
  2492. if (unlikely(src_pipe >= ipa3_ctx->ipa_num_pipes ||
  2493. !ep->valid ||
  2494. !ep->client_notify)) {
  2495. IPAERR_RL("drop pipe=%d ep_valid=%d client_notify=%pK\n",
  2496. src_pipe, ep->valid, ep->client_notify);
  2497. dev_kfree_skb_any(rx_skb);
  2498. return;
  2499. }
  2500. if (status.exception == IPAHAL_PKT_STATUS_EXCEPTION_NONE)
  2501. skb_pull(rx_skb, ipahal_pkt_status_get_size() +
  2502. IPA_LAN_RX_HEADER_LENGTH);
  2503. else
  2504. skb_pull(rx_skb, ipahal_pkt_status_get_size());
  2505. /* Metadata Info
  2506. * ------------------------------------------
  2507. * | 3 | 2 | 1 | 0 |
  2508. * | fw_desc | vdev_id | qmap mux id | Resv |
  2509. * ------------------------------------------
  2510. */
  2511. *(u16 *)rx_skb->cb = ((metadata >> 16) & 0xFFFF);
  2512. *(u8 *)(rx_skb->cb + 4) = ucp;
  2513. IPADBG_LOW("meta_data: 0x%x cb: 0x%x\n",
  2514. metadata, *(u32 *)rx_skb->cb);
  2515. IPADBG_LOW("ucp: %d\n", *(u8 *)(rx_skb->cb + 4));
  2516. ep->client_notify(ep->priv, IPA_RECEIVE, (unsigned long)(rx_skb));
  2517. }
  2518. static void ipa3_recycle_rx_wrapper(struct ipa3_rx_pkt_wrapper *rx_pkt)
  2519. {
  2520. rx_pkt->data.dma_addr = 0;
  2521. /* skb recycle was moved to pyld_hdlr */
  2522. INIT_LIST_HEAD(&rx_pkt->link);
  2523. spin_lock_bh(&rx_pkt->sys->spinlock);
  2524. list_add_tail(&rx_pkt->link, &rx_pkt->sys->rcycl_list);
  2525. spin_unlock_bh(&rx_pkt->sys->spinlock);
  2526. }
  2527. /**
  2528. * handle_skb_completion()- Handle event completion EOB or EOT and prep the skb
  2529. *
  2530. * if eob: Set skb values, put rx_pkt at the end of the list and return NULL
  2531. *
  2532. * if eot: Set skb values, put skb at the end of the list. Then update the
  2533. * length and chain the skbs together while also freeing and unmapping the
  2534. * corresponding rx pkt. Once finished return the head_skb to be sent up the
  2535. * network stack.
  2536. */
  2537. static struct sk_buff *handle_skb_completion(struct gsi_chan_xfer_notify
  2538. *notify, bool update_truesize)
  2539. {
  2540. struct ipa3_rx_pkt_wrapper *rx_pkt, *tmp;
  2541. struct sk_buff *rx_skb, *next_skb = NULL;
  2542. struct list_head *head;
  2543. struct ipa3_sys_context *sys;
  2544. sys = (struct ipa3_sys_context *) notify->chan_user_data;
  2545. rx_pkt = (struct ipa3_rx_pkt_wrapper *) notify->xfer_user_data;
  2546. spin_lock_bh(&rx_pkt->sys->spinlock);
  2547. rx_pkt->sys->len--;
  2548. spin_unlock_bh(&rx_pkt->sys->spinlock);
  2549. if (notify->bytes_xfered)
  2550. rx_pkt->len = notify->bytes_xfered;
  2551. rx_skb = rx_pkt->data.skb;
  2552. skb_set_tail_pointer(rx_skb, rx_pkt->len);
  2553. rx_skb->len = rx_pkt->len;
  2554. if (update_truesize) {
  2555. *(unsigned int *)rx_skb->cb = rx_skb->len;
  2556. rx_skb->truesize = rx_pkt->len + sizeof(struct sk_buff);
  2557. }
  2558. if (notify->veid >= GSI_VEID_MAX) {
  2559. WARN_ON(1);
  2560. return NULL;
  2561. }
  2562. /*Assesrt when WAN consumer channel receive EOB event*/
  2563. if (notify->evt_id == GSI_CHAN_EVT_EOB &&
  2564. sys->ep->client == IPA_CLIENT_APPS_WAN_CONS) {
  2565. IPAERR("EOB event received on WAN consumer channel\n");
  2566. ipa_assert();
  2567. }
  2568. head = &rx_pkt->sys->pending_pkts[notify->veid];
  2569. INIT_LIST_HEAD(&rx_pkt->link);
  2570. list_add_tail(&rx_pkt->link, head);
  2571. /* Check added for handling LAN consumer packet without EOT flag */
  2572. if (notify->evt_id == GSI_CHAN_EVT_EOT ||
  2573. sys->ep->client == IPA_CLIENT_APPS_LAN_CONS) {
  2574. /* go over the list backward to save computations on updating length */
  2575. list_for_each_entry_safe_reverse(rx_pkt, tmp, head, link) {
  2576. rx_skb = rx_pkt->data.skb;
  2577. list_del(&rx_pkt->link);
  2578. dma_unmap_single(ipa3_ctx->pdev, rx_pkt->data.dma_addr,
  2579. sys->rx_buff_sz, DMA_FROM_DEVICE);
  2580. sys->free_rx_wrapper(rx_pkt);
  2581. if (next_skb) {
  2582. skb_shinfo(rx_skb)->frag_list = next_skb;
  2583. rx_skb->len += next_skb->len;
  2584. rx_skb->data_len += next_skb->len;
  2585. }
  2586. next_skb = rx_skb;
  2587. }
  2588. } else {
  2589. return NULL;
  2590. }
  2591. return rx_skb;
  2592. }
  2593. static void ipa3_wq_rx_common(struct ipa3_sys_context *sys,
  2594. struct gsi_chan_xfer_notify *notify)
  2595. {
  2596. struct sk_buff *rx_skb;
  2597. struct ipa3_sys_context *coal_sys;
  2598. int ipa_ep_idx;
  2599. if (!notify) {
  2600. IPAERR_RL("gsi_chan_xfer_notify is null\n");
  2601. return;
  2602. }
  2603. rx_skb = handle_skb_completion(notify, true);
  2604. if (rx_skb) {
  2605. sys->pyld_hdlr(rx_skb, sys);
  2606. /* For coalescing, we have 2 transfer rings to replenish */
  2607. if (sys->ep->client == IPA_CLIENT_APPS_WAN_COAL_CONS) {
  2608. ipa_ep_idx = ipa3_get_ep_mapping(
  2609. IPA_CLIENT_APPS_WAN_CONS);
  2610. if (ipa_ep_idx == IPA_EP_NOT_ALLOCATED) {
  2611. IPAERR("Invalid client.\n");
  2612. return;
  2613. }
  2614. coal_sys = ipa3_ctx->ep[ipa_ep_idx].sys;
  2615. coal_sys->repl_hdlr(coal_sys);
  2616. }
  2617. sys->repl_hdlr(sys);
  2618. }
  2619. }
  2620. static void ipa3_wq_rx_napi_chain(struct ipa3_sys_context *sys,
  2621. struct gsi_chan_xfer_notify *notify, uint32_t num)
  2622. {
  2623. struct ipa3_sys_context *wan_def_sys;
  2624. int i, ipa_ep_idx;
  2625. struct sk_buff *rx_skb, *first_skb = NULL, *prev_skb = NULL;
  2626. /* non-coalescing case (SKB chaining enabled) */
  2627. if (sys->ep->client != IPA_CLIENT_APPS_WAN_COAL_CONS) {
  2628. for (i = 0; i < num; i++) {
  2629. rx_skb = handle_skb_completion(&notify[i], false);
  2630. /* this is always true for EOTs */
  2631. if (rx_skb) {
  2632. if (!first_skb)
  2633. first_skb = rx_skb;
  2634. if (prev_skb)
  2635. skb_shinfo(prev_skb)->frag_list =
  2636. rx_skb;
  2637. prev_skb = rx_skb;
  2638. }
  2639. }
  2640. if (prev_skb) {
  2641. skb_shinfo(prev_skb)->frag_list = NULL;
  2642. sys->pyld_hdlr(first_skb, sys);
  2643. }
  2644. /* TODO: add chaining for coal case */
  2645. } else {
  2646. for (i = 0; i < num; i++) {
  2647. rx_skb = handle_skb_completion(&notify[i], false);
  2648. if (rx_skb) {
  2649. sys->pyld_hdlr(rx_skb, sys);
  2650. /*
  2651. * For coalescing, we have 2 transfer rings to replenish
  2652. */
  2653. ipa_ep_idx = ipa3_get_ep_mapping(
  2654. IPA_CLIENT_APPS_WAN_CONS);
  2655. if (ipa_ep_idx == IPA_EP_NOT_ALLOCATED) {
  2656. IPAERR("Invalid client.\n");
  2657. return;
  2658. }
  2659. wan_def_sys = ipa3_ctx->ep[ipa_ep_idx].sys;
  2660. wan_def_sys->repl_hdlr(wan_def_sys);
  2661. }
  2662. }
  2663. }
  2664. }
  2665. static void ipa3_wlan_wq_rx_common(struct ipa3_sys_context *sys,
  2666. struct gsi_chan_xfer_notify *notify)
  2667. {
  2668. struct ipa3_rx_pkt_wrapper *rx_pkt_expected;
  2669. struct sk_buff *rx_skb;
  2670. rx_pkt_expected = (struct ipa3_rx_pkt_wrapper *) notify->xfer_user_data;
  2671. sys->len--;
  2672. if (notify->bytes_xfered)
  2673. rx_pkt_expected->len = notify->bytes_xfered;
  2674. rx_skb = rx_pkt_expected->data.skb;
  2675. skb_set_tail_pointer(rx_skb, rx_pkt_expected->len);
  2676. rx_skb->len = rx_pkt_expected->len;
  2677. rx_skb->truesize = rx_pkt_expected->len + sizeof(struct sk_buff);
  2678. sys->ep->wstats.tx_pkts_rcvd++;
  2679. if (sys->len <= IPA_WLAN_RX_POOL_SZ_LOW_WM) {
  2680. ipa3_free_skb(&rx_pkt_expected->data);
  2681. sys->ep->wstats.tx_pkts_dropped++;
  2682. } else {
  2683. sys->ep->wstats.tx_pkts_sent++;
  2684. sys->ep->client_notify(sys->ep->priv, IPA_RECEIVE,
  2685. (unsigned long)(&rx_pkt_expected->data));
  2686. }
  2687. ipa3_replenish_wlan_rx_cache(sys);
  2688. }
  2689. static void ipa3_dma_memcpy_notify(struct ipa3_sys_context *sys)
  2690. {
  2691. IPADBG_LOW("ENTER.\n");
  2692. if (unlikely(list_empty(&sys->head_desc_list))) {
  2693. IPAERR("descriptor list is empty!\n");
  2694. WARN_ON(1);
  2695. return;
  2696. }
  2697. sys->ep->client_notify(sys->ep->priv, IPA_RECEIVE, 0);
  2698. IPADBG_LOW("EXIT\n");
  2699. }
  2700. static void ipa3_wq_rx_avail(struct work_struct *work)
  2701. {
  2702. struct ipa3_rx_pkt_wrapper *rx_pkt;
  2703. struct ipa3_sys_context *sys;
  2704. rx_pkt = container_of(work, struct ipa3_rx_pkt_wrapper, work);
  2705. WARN(unlikely(rx_pkt == NULL), "rx pkt is null");
  2706. sys = rx_pkt->sys;
  2707. ipa3_wq_rx_common(sys, 0);
  2708. }
  2709. static int ipa3_odu_rx_pyld_hdlr(struct sk_buff *rx_skb,
  2710. struct ipa3_sys_context *sys)
  2711. {
  2712. if (sys->ep->client_notify) {
  2713. sys->ep->client_notify(sys->ep->priv, IPA_RECEIVE,
  2714. (unsigned long)(rx_skb));
  2715. } else {
  2716. dev_kfree_skb_any(rx_skb);
  2717. WARN(1, "client notify is null");
  2718. }
  2719. return 0;
  2720. }
  2721. static int ipa3_odl_dpl_rx_pyld_hdlr(struct sk_buff *rx_skb,
  2722. struct ipa3_sys_context *sys)
  2723. {
  2724. if (WARN(!sys->ep->client_notify, "sys->ep->client_notify is NULL\n"))
  2725. dev_kfree_skb_any(rx_skb);
  2726. else
  2727. sys->ep->client_notify(sys->ep->priv, IPA_RECEIVE,
  2728. (unsigned long)(rx_skb));
  2729. return 0;
  2730. }
  2731. static void ipa3_free_rx_wrapper(struct ipa3_rx_pkt_wrapper *rk_pkt)
  2732. {
  2733. kmem_cache_free(ipa3_ctx->rx_pkt_wrapper_cache, rk_pkt);
  2734. }
  2735. static void ipa3_set_aggr_limit(struct ipa_sys_connect_params *in,
  2736. struct ipa3_sys_context *sys)
  2737. {
  2738. u32 *aggr_byte_limit = &in->ipa_ep_cfg.aggr.aggr_byte_limit;
  2739. u32 adjusted_sz = ipa_adjust_ra_buff_base_sz(*aggr_byte_limit);
  2740. IPADBG("get close-by %u\n", adjusted_sz);
  2741. IPADBG("set rx_buff_sz %lu\n", (unsigned long)
  2742. IPA_GENERIC_RX_BUFF_SZ(adjusted_sz));
  2743. /* disable ipa_status */
  2744. sys->ep->status.status_en = false;
  2745. sys->rx_buff_sz = IPA_GENERIC_RX_BUFF_SZ(adjusted_sz);
  2746. if (in->client == IPA_CLIENT_APPS_WAN_COAL_CONS)
  2747. in->ipa_ep_cfg.aggr.aggr_hard_byte_limit_en = 1;
  2748. *aggr_byte_limit = sys->rx_buff_sz < *aggr_byte_limit ?
  2749. IPA_ADJUST_AGGR_BYTE_LIMIT(sys->rx_buff_sz) :
  2750. IPA_ADJUST_AGGR_BYTE_LIMIT(*aggr_byte_limit);
  2751. IPADBG("set aggr_limit %lu\n", (unsigned long) *aggr_byte_limit);
  2752. }
  2753. static int ipa3_assign_policy(struct ipa_sys_connect_params *in,
  2754. struct ipa3_sys_context *sys)
  2755. {
  2756. bool apps_wan_cons_agg_gro_flag;
  2757. unsigned long aggr_byte_limit;
  2758. if (in->client == IPA_CLIENT_APPS_CMD_PROD) {
  2759. sys->policy = IPA_POLICY_INTR_MODE;
  2760. sys->use_comm_evt_ring = false;
  2761. return 0;
  2762. }
  2763. if (in->client == IPA_CLIENT_APPS_WAN_PROD) {
  2764. sys->policy = IPA_POLICY_INTR_MODE;
  2765. sys->use_comm_evt_ring = true;
  2766. INIT_WORK(&sys->work, ipa3_send_nop_desc);
  2767. /*
  2768. * enable source notification status for exception packets
  2769. * (i.e. QMAP commands) to be routed to modem.
  2770. */
  2771. sys->ep->status.status_en = true;
  2772. sys->ep->status.status_ep =
  2773. ipa3_get_ep_mapping(IPA_CLIENT_Q6_WAN_CONS);
  2774. return 0;
  2775. }
  2776. if (IPA_CLIENT_IS_MEMCPY_DMA_PROD(in->client)) {
  2777. sys->policy = IPA_POLICY_NOINTR_MODE;
  2778. return 0;
  2779. }
  2780. apps_wan_cons_agg_gro_flag =
  2781. ipa3_ctx->ipa_client_apps_wan_cons_agg_gro;
  2782. aggr_byte_limit = in->ipa_ep_cfg.aggr.aggr_byte_limit;
  2783. if (IPA_CLIENT_IS_PROD(in->client)) {
  2784. if (sys->ep->skip_ep_cfg) {
  2785. sys->policy = IPA_POLICY_INTR_POLL_MODE;
  2786. sys->use_comm_evt_ring = true;
  2787. atomic_set(&sys->curr_polling_state, 0);
  2788. } else {
  2789. sys->policy = IPA_POLICY_INTR_MODE;
  2790. sys->use_comm_evt_ring = true;
  2791. INIT_WORK(&sys->work, ipa3_send_nop_desc);
  2792. }
  2793. } else {
  2794. if (in->client == IPA_CLIENT_APPS_LAN_CONS ||
  2795. in->client == IPA_CLIENT_APPS_WAN_CONS ||
  2796. in->client == IPA_CLIENT_APPS_WAN_COAL_CONS) {
  2797. sys->ep->status.status_en = true;
  2798. sys->policy = IPA_POLICY_INTR_POLL_MODE;
  2799. INIT_WORK(&sys->work, ipa3_wq_handle_rx);
  2800. INIT_DELAYED_WORK(&sys->switch_to_intr_work,
  2801. ipa3_switch_to_intr_rx_work_func);
  2802. INIT_DELAYED_WORK(&sys->replenish_rx_work,
  2803. ipa3_replenish_rx_work_func);
  2804. INIT_WORK(&sys->repl_work, ipa3_wq_repl_rx);
  2805. atomic_set(&sys->curr_polling_state, 0);
  2806. sys->rx_buff_sz = IPA_GENERIC_RX_BUFF_SZ(
  2807. IPA_GENERIC_RX_BUFF_BASE_SZ);
  2808. sys->get_skb = ipa3_get_skb_ipa_rx;
  2809. sys->free_skb = ipa3_free_skb_rx;
  2810. in->ipa_ep_cfg.aggr.aggr_en = IPA_ENABLE_AGGR;
  2811. if (in->client == IPA_CLIENT_APPS_WAN_COAL_CONS)
  2812. in->ipa_ep_cfg.aggr.aggr = IPA_COALESCE;
  2813. else
  2814. in->ipa_ep_cfg.aggr.aggr = IPA_GENERIC;
  2815. in->ipa_ep_cfg.aggr.aggr_time_limit =
  2816. IPA_GENERIC_AGGR_TIME_LIMIT;
  2817. if (in->client == IPA_CLIENT_APPS_LAN_CONS) {
  2818. sys->pyld_hdlr = ipa3_lan_rx_pyld_hdlr;
  2819. sys->repl_hdlr =
  2820. ipa3_replenish_rx_cache_recycle;
  2821. sys->free_rx_wrapper =
  2822. ipa3_recycle_rx_wrapper;
  2823. sys->rx_pool_sz =
  2824. ipa3_ctx->lan_rx_ring_size;
  2825. in->ipa_ep_cfg.aggr.aggr_byte_limit =
  2826. IPA_GENERIC_AGGR_BYTE_LIMIT;
  2827. in->ipa_ep_cfg.aggr.aggr_pkt_limit =
  2828. IPA_GENERIC_AGGR_PKT_LIMIT;
  2829. } else if (in->client == IPA_CLIENT_APPS_WAN_CONS ||
  2830. in->client == IPA_CLIENT_APPS_WAN_COAL_CONS) {
  2831. sys->pyld_hdlr = ipa3_wan_rx_pyld_hdlr;
  2832. sys->free_rx_wrapper = ipa3_free_rx_wrapper;
  2833. sys->rx_pool_sz = ipa3_ctx->wan_rx_ring_size;
  2834. if (nr_cpu_ids > 1) {
  2835. sys->repl_hdlr =
  2836. ipa3_fast_replenish_rx_cache;
  2837. } else {
  2838. sys->repl_hdlr =
  2839. ipa3_replenish_rx_cache;
  2840. }
  2841. if (in->napi_obj && in->recycle_enabled)
  2842. sys->repl_hdlr =
  2843. ipa3_replenish_rx_cache_recycle;
  2844. in->ipa_ep_cfg.aggr.aggr_sw_eof_active
  2845. = true;
  2846. if (apps_wan_cons_agg_gro_flag) {
  2847. ipa3_set_aggr_limit(in, sys);
  2848. } else {
  2849. in->ipa_ep_cfg.aggr.aggr_byte_limit =
  2850. IPA_GENERIC_AGGR_BYTE_LIMIT;
  2851. in->ipa_ep_cfg.aggr.aggr_pkt_limit =
  2852. IPA_GENERIC_AGGR_PKT_LIMIT;
  2853. }
  2854. }
  2855. } else if (IPA_CLIENT_IS_WLAN_CONS(in->client)) {
  2856. IPADBG("assigning policy to client:%d",
  2857. in->client);
  2858. sys->policy = IPA_POLICY_INTR_POLL_MODE;
  2859. INIT_WORK(&sys->work, ipa3_wq_handle_rx);
  2860. INIT_DELAYED_WORK(&sys->switch_to_intr_work,
  2861. ipa3_switch_to_intr_rx_work_func);
  2862. INIT_DELAYED_WORK(&sys->replenish_rx_work,
  2863. ipa3_replenish_rx_work_func);
  2864. atomic_set(&sys->curr_polling_state, 0);
  2865. sys->rx_buff_sz = IPA_WLAN_RX_BUFF_SZ;
  2866. sys->rx_pool_sz = in->desc_fifo_sz /
  2867. IPA_FIFO_ELEMENT_SIZE - 1;
  2868. if (sys->rx_pool_sz > IPA_WLAN_RX_POOL_SZ)
  2869. sys->rx_pool_sz = IPA_WLAN_RX_POOL_SZ;
  2870. sys->pyld_hdlr = NULL;
  2871. sys->repl_hdlr = ipa3_replenish_wlan_rx_cache;
  2872. sys->get_skb = ipa3_get_skb_ipa_rx;
  2873. sys->free_skb = ipa3_free_skb_rx;
  2874. sys->free_rx_wrapper = ipa3_free_rx_wrapper;
  2875. in->ipa_ep_cfg.aggr.aggr_en = IPA_BYPASS_AGGR;
  2876. } else if (IPA_CLIENT_IS_ODU_CONS(in->client)) {
  2877. IPADBG("assigning policy to client:%d",
  2878. in->client);
  2879. sys->policy = IPA_POLICY_INTR_POLL_MODE;
  2880. INIT_WORK(&sys->work, ipa3_wq_handle_rx);
  2881. INIT_DELAYED_WORK(&sys->switch_to_intr_work,
  2882. ipa3_switch_to_intr_rx_work_func);
  2883. INIT_DELAYED_WORK(&sys->replenish_rx_work,
  2884. ipa3_replenish_rx_work_func);
  2885. atomic_set(&sys->curr_polling_state, 0);
  2886. sys->rx_pool_sz = in->desc_fifo_sz /
  2887. IPA_FIFO_ELEMENT_SIZE - 1;
  2888. if (sys->rx_pool_sz > IPA_ODU_RX_POOL_SZ)
  2889. sys->rx_pool_sz = IPA_ODU_RX_POOL_SZ;
  2890. sys->pyld_hdlr = ipa3_odu_rx_pyld_hdlr;
  2891. sys->get_skb = ipa3_get_skb_ipa_rx;
  2892. sys->free_skb = ipa3_free_skb_rx;
  2893. /* recycle skb for GSB use case */
  2894. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_0) {
  2895. sys->free_rx_wrapper =
  2896. ipa3_free_rx_wrapper;
  2897. sys->repl_hdlr =
  2898. ipa3_replenish_rx_cache;
  2899. /* Overwrite buffer size & aggr limit for GSB */
  2900. sys->rx_buff_sz = IPA_GENERIC_RX_BUFF_SZ(
  2901. IPA_GSB_RX_BUFF_BASE_SZ);
  2902. in->ipa_ep_cfg.aggr.aggr_byte_limit =
  2903. IPA_GSB_AGGR_BYTE_LIMIT;
  2904. } else {
  2905. sys->free_rx_wrapper =
  2906. ipa3_free_rx_wrapper;
  2907. sys->repl_hdlr = ipa3_replenish_rx_cache;
  2908. sys->rx_buff_sz = IPA_ODU_RX_BUFF_SZ;
  2909. }
  2910. } else if (in->client ==
  2911. IPA_CLIENT_MEMCPY_DMA_ASYNC_CONS) {
  2912. IPADBG("assigning policy to client:%d",
  2913. in->client);
  2914. sys->policy = IPA_POLICY_INTR_POLL_MODE;
  2915. INIT_WORK(&sys->work, ipa3_wq_handle_rx);
  2916. INIT_DELAYED_WORK(&sys->switch_to_intr_work,
  2917. ipa3_switch_to_intr_rx_work_func);
  2918. } else if (in->client ==
  2919. IPA_CLIENT_MEMCPY_DMA_SYNC_CONS) {
  2920. IPADBG("assigning policy to client:%d",
  2921. in->client);
  2922. sys->policy = IPA_POLICY_NOINTR_MODE;
  2923. } else if (in->client == IPA_CLIENT_ODL_DPL_CONS) {
  2924. IPADBG("assigning policy to ODL client:%d\n",
  2925. in->client);
  2926. sys->ep->status.status_en = true;
  2927. sys->policy = IPA_POLICY_INTR_POLL_MODE;
  2928. INIT_WORK(&sys->work, ipa3_wq_handle_rx);
  2929. INIT_DELAYED_WORK(&sys->switch_to_intr_work,
  2930. ipa3_switch_to_intr_rx_work_func);
  2931. INIT_DELAYED_WORK(&sys->replenish_rx_work,
  2932. ipa3_replenish_rx_work_func);
  2933. atomic_set(&sys->curr_polling_state, 0);
  2934. sys->rx_buff_sz =
  2935. IPA_GENERIC_RX_BUFF_SZ(IPA_ODL_RX_BUFF_SZ);
  2936. sys->pyld_hdlr = ipa3_odl_dpl_rx_pyld_hdlr;
  2937. sys->get_skb = ipa3_get_skb_ipa_rx;
  2938. sys->free_skb = ipa3_free_skb_rx;
  2939. sys->free_rx_wrapper = ipa3_recycle_rx_wrapper;
  2940. sys->repl_hdlr = ipa3_replenish_rx_cache_recycle;
  2941. sys->rx_pool_sz = in->desc_fifo_sz /
  2942. IPA_FIFO_ELEMENT_SIZE - 1;
  2943. } else {
  2944. WARN(1, "Need to install a RX pipe hdlr\n");
  2945. return -EINVAL;
  2946. }
  2947. }
  2948. return 0;
  2949. }
  2950. /**
  2951. * ipa3_tx_client_rx_notify_release() - Callback function
  2952. * which will call the user supplied callback function to
  2953. * release the skb, or release it on its own if no callback
  2954. * function was supplied
  2955. *
  2956. * @user1: [in] - Data Descriptor
  2957. * @user2: [in] - endpoint idx
  2958. *
  2959. * This notified callback is for the destination client
  2960. * This function is supplied in ipa3_tx_dp_mul
  2961. */
  2962. static void ipa3_tx_client_rx_notify_release(void *user1, int user2)
  2963. {
  2964. struct ipa_tx_data_desc *dd = (struct ipa_tx_data_desc *)user1;
  2965. int ep_idx = user2;
  2966. IPADBG_LOW("Received data desc anchor:%pK\n", dd);
  2967. atomic_inc(&ipa3_ctx->ep[ep_idx].avail_fifo_desc);
  2968. ipa3_ctx->ep[ep_idx].wstats.rx_pkts_status_rcvd++;
  2969. /* wlan host driver waits till tx complete before unload */
  2970. IPADBG_LOW("ep=%d fifo_desc_free_count=%d\n",
  2971. ep_idx, atomic_read(&ipa3_ctx->ep[ep_idx].avail_fifo_desc));
  2972. IPADBG_LOW("calling client notify callback with priv:%pK\n",
  2973. ipa3_ctx->ep[ep_idx].priv);
  2974. if (ipa3_ctx->ep[ep_idx].client_notify) {
  2975. ipa3_ctx->ep[ep_idx].client_notify(ipa3_ctx->ep[ep_idx].priv,
  2976. IPA_WRITE_DONE, (unsigned long)user1);
  2977. ipa3_ctx->ep[ep_idx].wstats.rx_hd_reply++;
  2978. }
  2979. }
  2980. /**
  2981. * ipa3_tx_client_rx_pkt_status() - Callback function
  2982. * which will call the user supplied callback function to
  2983. * increase the available fifo descriptor
  2984. *
  2985. * @user1: [in] - Data Descriptor
  2986. * @user2: [in] - endpoint idx
  2987. *
  2988. * This notified callback is for the destination client
  2989. * This function is supplied in ipa3_tx_dp_mul
  2990. */
  2991. static void ipa3_tx_client_rx_pkt_status(void *user1, int user2)
  2992. {
  2993. int ep_idx = user2;
  2994. atomic_inc(&ipa3_ctx->ep[ep_idx].avail_fifo_desc);
  2995. ipa3_ctx->ep[ep_idx].wstats.rx_pkts_status_rcvd++;
  2996. }
  2997. /**
  2998. * ipa3_tx_dp_mul() - Data-path tx handler for multiple packets
  2999. * @src: [in] - Client that is sending data
  3000. * @ipa_tx_data_desc: [in] data descriptors from wlan
  3001. *
  3002. * this is used for to transfer data descriptors that received
  3003. * from WLAN1_PROD pipe to IPA HW
  3004. *
  3005. * The function will send data descriptors from WLAN1_PROD (one
  3006. * at a time). Will set EOT flag for last descriptor Once this send was done
  3007. * from transport point-of-view the IPA driver will get notified by the
  3008. * supplied callback - ipa_gsi_irq_tx_notify_cb()
  3009. *
  3010. * ipa_gsi_irq_tx_notify_cb will call to the user supplied callback
  3011. *
  3012. * Returns: 0 on success, negative on failure
  3013. */
  3014. int ipa3_tx_dp_mul(enum ipa_client_type src,
  3015. struct ipa_tx_data_desc *data_desc)
  3016. {
  3017. /* The second byte in wlan header holds qmap id */
  3018. #define IPA_WLAN_HDR_QMAP_ID_OFFSET 1
  3019. struct ipa_tx_data_desc *entry;
  3020. struct ipa3_sys_context *sys;
  3021. struct ipa3_desc desc[2];
  3022. u32 num_desc, cnt;
  3023. int ep_idx;
  3024. IPADBG_LOW("Received data desc anchor:%pK\n", data_desc);
  3025. spin_lock_bh(&ipa3_ctx->wc_memb.ipa_tx_mul_spinlock);
  3026. ep_idx = ipa3_get_ep_mapping(src);
  3027. if (unlikely(ep_idx == -1)) {
  3028. IPAERR("dest EP does not exist.\n");
  3029. goto fail_send;
  3030. }
  3031. IPADBG_LOW("ep idx:%d\n", ep_idx);
  3032. sys = ipa3_ctx->ep[ep_idx].sys;
  3033. if (unlikely(ipa3_ctx->ep[ep_idx].valid == 0)) {
  3034. IPAERR("dest EP not valid.\n");
  3035. goto fail_send;
  3036. }
  3037. sys->ep->wstats.rx_hd_rcvd++;
  3038. /* Calculate the number of descriptors */
  3039. num_desc = 0;
  3040. list_for_each_entry(entry, &data_desc->link, link) {
  3041. num_desc++;
  3042. }
  3043. IPADBG_LOW("Number of Data Descriptors:%d", num_desc);
  3044. if (atomic_read(&sys->ep->avail_fifo_desc) < num_desc) {
  3045. IPAERR("Insufficient data descriptors available\n");
  3046. goto fail_send;
  3047. }
  3048. /* Assign callback only for last data descriptor */
  3049. cnt = 0;
  3050. list_for_each_entry(entry, &data_desc->link, link) {
  3051. memset(desc, 0, 2 * sizeof(struct ipa3_desc));
  3052. IPADBG_LOW("Parsing data desc :%d\n", cnt);
  3053. cnt++;
  3054. ((u8 *)entry->pyld_buffer)[IPA_WLAN_HDR_QMAP_ID_OFFSET] =
  3055. (u8)sys->ep->cfg.meta.qmap_id;
  3056. /* the tag field will be populated in ipa3_send() function */
  3057. desc[0].is_tag_status = true;
  3058. desc[1].pyld = entry->pyld_buffer;
  3059. desc[1].len = entry->pyld_len;
  3060. desc[1].type = IPA_DATA_DESC_SKB;
  3061. desc[1].user1 = data_desc;
  3062. desc[1].user2 = ep_idx;
  3063. IPADBG_LOW("priv:%pK pyld_buf:0x%pK pyld_len:%d\n",
  3064. entry->priv, desc[1].pyld, desc[1].len);
  3065. /* In case of last descriptor populate callback */
  3066. if (cnt == num_desc) {
  3067. IPADBG_LOW("data desc:%pK\n", data_desc);
  3068. desc[1].callback = ipa3_tx_client_rx_notify_release;
  3069. } else {
  3070. desc[1].callback = ipa3_tx_client_rx_pkt_status;
  3071. }
  3072. IPADBG_LOW("calling ipa3_send()\n");
  3073. if (ipa3_send(sys, 2, desc, true)) {
  3074. IPAERR("fail to send skb\n");
  3075. sys->ep->wstats.rx_pkt_leak += (cnt-1);
  3076. sys->ep->wstats.rx_dp_fail++;
  3077. goto fail_send;
  3078. }
  3079. if (atomic_read(&sys->ep->avail_fifo_desc) >= 0)
  3080. atomic_dec(&sys->ep->avail_fifo_desc);
  3081. sys->ep->wstats.rx_pkts_rcvd++;
  3082. IPADBG_LOW("ep=%d fifo desc=%d\n",
  3083. ep_idx, atomic_read(&sys->ep->avail_fifo_desc));
  3084. }
  3085. sys->ep->wstats.rx_hd_processed++;
  3086. spin_unlock_bh(&ipa3_ctx->wc_memb.ipa_tx_mul_spinlock);
  3087. return 0;
  3088. fail_send:
  3089. spin_unlock_bh(&ipa3_ctx->wc_memb.ipa_tx_mul_spinlock);
  3090. return -EFAULT;
  3091. }
  3092. void ipa3_free_skb(struct ipa_rx_data *data)
  3093. {
  3094. struct ipa3_rx_pkt_wrapper *rx_pkt;
  3095. spin_lock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  3096. ipa3_ctx->wc_memb.total_tx_pkts_freed++;
  3097. rx_pkt = container_of(data, struct ipa3_rx_pkt_wrapper, data);
  3098. ipa3_skb_recycle(rx_pkt->data.skb);
  3099. (void)skb_put(rx_pkt->data.skb, IPA_WLAN_RX_BUFF_SZ);
  3100. list_add_tail(&rx_pkt->link,
  3101. &ipa3_ctx->wc_memb.wlan_comm_desc_list);
  3102. ipa3_ctx->wc_memb.wlan_comm_free_cnt++;
  3103. spin_unlock_bh(&ipa3_ctx->wc_memb.wlan_spinlock);
  3104. }
  3105. /* Functions added to support kernel tests */
  3106. int ipa3_sys_setup(struct ipa_sys_connect_params *sys_in,
  3107. unsigned long *ipa_transport_hdl,
  3108. u32 *ipa_pipe_num, u32 *clnt_hdl, bool en_status)
  3109. {
  3110. struct ipa3_ep_context *ep;
  3111. int ipa_ep_idx;
  3112. int result = -EINVAL;
  3113. if (sys_in == NULL || clnt_hdl == NULL) {
  3114. IPAERR("NULL args\n");
  3115. goto fail_gen;
  3116. }
  3117. if (ipa_transport_hdl == NULL || ipa_pipe_num == NULL) {
  3118. IPAERR("NULL args\n");
  3119. goto fail_gen;
  3120. }
  3121. if (sys_in->client >= IPA_CLIENT_MAX) {
  3122. IPAERR("bad parm client:%d\n", sys_in->client);
  3123. goto fail_gen;
  3124. }
  3125. ipa_ep_idx = ipa3_get_ep_mapping(sys_in->client);
  3126. if (ipa_ep_idx == -1) {
  3127. IPAERR("Invalid client :%d\n", sys_in->client);
  3128. goto fail_gen;
  3129. }
  3130. ep = &ipa3_ctx->ep[ipa_ep_idx];
  3131. IPA_ACTIVE_CLIENTS_INC_EP(sys_in->client);
  3132. if (ep->valid == 1) {
  3133. if (sys_in->client != IPA_CLIENT_APPS_WAN_PROD) {
  3134. IPAERR("EP %d already allocated\n", ipa_ep_idx);
  3135. goto fail_and_disable_clocks;
  3136. } else {
  3137. if (ipa3_cfg_ep_hdr(ipa_ep_idx,
  3138. &sys_in->ipa_ep_cfg.hdr)) {
  3139. IPAERR("fail to configure hdr prop of EP %d\n",
  3140. ipa_ep_idx);
  3141. result = -EFAULT;
  3142. goto fail_and_disable_clocks;
  3143. }
  3144. if (ipa3_cfg_ep_hdr_ext(ipa_ep_idx,
  3145. &sys_in->ipa_ep_cfg.hdr_ext)) {
  3146. IPAERR("fail config hdr_ext prop of EP %d\n",
  3147. ipa_ep_idx);
  3148. result = -EFAULT;
  3149. goto fail_and_disable_clocks;
  3150. }
  3151. if (ipa3_cfg_ep_cfg(ipa_ep_idx,
  3152. &sys_in->ipa_ep_cfg.cfg)) {
  3153. IPAERR("fail to configure cfg prop of EP %d\n",
  3154. ipa_ep_idx);
  3155. result = -EFAULT;
  3156. goto fail_and_disable_clocks;
  3157. }
  3158. IPAERR("client %d (ep: %d) overlay ok sys=%pK\n",
  3159. sys_in->client, ipa_ep_idx, ep->sys);
  3160. ep->client_notify = sys_in->notify;
  3161. ep->priv = sys_in->priv;
  3162. *clnt_hdl = ipa_ep_idx;
  3163. if (!ep->keep_ipa_awake)
  3164. IPA_ACTIVE_CLIENTS_DEC_EP(sys_in->client);
  3165. return 0;
  3166. }
  3167. }
  3168. memset(ep, 0, offsetof(struct ipa3_ep_context, sys));
  3169. ep->valid = 1;
  3170. ep->client = sys_in->client;
  3171. ep->client_notify = sys_in->notify;
  3172. ep->priv = sys_in->priv;
  3173. ep->keep_ipa_awake = true;
  3174. if (en_status) {
  3175. ep->status.status_en = true;
  3176. ep->status.status_ep = ipa_ep_idx;
  3177. }
  3178. result = ipa3_enable_data_path(ipa_ep_idx);
  3179. if (result) {
  3180. IPAERR("enable data path failed res=%d clnt=%d.\n",
  3181. result, ipa_ep_idx);
  3182. goto fail_gen2;
  3183. }
  3184. if (!ep->skip_ep_cfg) {
  3185. if (ipa3_cfg_ep(ipa_ep_idx, &sys_in->ipa_ep_cfg)) {
  3186. IPAERR("fail to configure EP.\n");
  3187. goto fail_gen2;
  3188. }
  3189. if (ipa3_cfg_ep_status(ipa_ep_idx, &ep->status)) {
  3190. IPAERR("fail to configure status of EP.\n");
  3191. goto fail_gen2;
  3192. }
  3193. IPADBG("ep configuration successful\n");
  3194. } else {
  3195. IPADBG("skipping ep configuration\n");
  3196. }
  3197. *clnt_hdl = ipa_ep_idx;
  3198. *ipa_pipe_num = ipa_ep_idx;
  3199. *ipa_transport_hdl = ipa3_ctx->gsi_dev_hdl;
  3200. if (!ep->keep_ipa_awake)
  3201. IPA_ACTIVE_CLIENTS_DEC_EP(sys_in->client);
  3202. ipa3_ctx->skip_ep_cfg_shadow[ipa_ep_idx] = ep->skip_ep_cfg;
  3203. IPADBG("client %d (ep: %d) connected sys=%pK\n", sys_in->client,
  3204. ipa_ep_idx, ep->sys);
  3205. return 0;
  3206. fail_gen2:
  3207. fail_and_disable_clocks:
  3208. IPA_ACTIVE_CLIENTS_DEC_EP(sys_in->client);
  3209. fail_gen:
  3210. return result;
  3211. }
  3212. int ipa3_sys_teardown(u32 clnt_hdl)
  3213. {
  3214. struct ipa3_ep_context *ep;
  3215. if (clnt_hdl >= ipa3_ctx->ipa_num_pipes ||
  3216. ipa3_ctx->ep[clnt_hdl].valid == 0) {
  3217. IPAERR("bad parm(Either endpoint or client hdl invalid)\n");
  3218. return -EINVAL;
  3219. }
  3220. ep = &ipa3_ctx->ep[clnt_hdl];
  3221. if (!ep->keep_ipa_awake)
  3222. IPA_ACTIVE_CLIENTS_INC_EP(ipa3_get_client_mapping(clnt_hdl));
  3223. ipa3_disable_data_path(clnt_hdl);
  3224. ep->valid = 0;
  3225. IPA_ACTIVE_CLIENTS_DEC_EP(ipa3_get_client_mapping(clnt_hdl));
  3226. IPADBG("client (ep: %d) disconnected\n", clnt_hdl);
  3227. return 0;
  3228. }
  3229. int ipa3_sys_update_gsi_hdls(u32 clnt_hdl, unsigned long gsi_ch_hdl,
  3230. unsigned long gsi_ev_hdl)
  3231. {
  3232. struct ipa3_ep_context *ep;
  3233. if (clnt_hdl >= ipa3_ctx->ipa_num_pipes ||
  3234. ipa3_ctx->ep[clnt_hdl].valid == 0) {
  3235. IPAERR("bad parm(Either endpoint or client hdl invalid)\n");
  3236. return -EINVAL;
  3237. }
  3238. ep = &ipa3_ctx->ep[clnt_hdl];
  3239. ep->gsi_chan_hdl = gsi_ch_hdl;
  3240. ep->gsi_evt_ring_hdl = gsi_ev_hdl;
  3241. return 0;
  3242. }
  3243. static void ipa_gsi_evt_ring_err_cb(struct gsi_evt_err_notify *notify)
  3244. {
  3245. switch (notify->evt_id) {
  3246. case GSI_EVT_OUT_OF_BUFFERS_ERR:
  3247. IPAERR("Got GSI_EVT_OUT_OF_BUFFERS_ERR\n");
  3248. break;
  3249. case GSI_EVT_OUT_OF_RESOURCES_ERR:
  3250. IPAERR("Got GSI_EVT_OUT_OF_RESOURCES_ERR\n");
  3251. break;
  3252. case GSI_EVT_UNSUPPORTED_INTER_EE_OP_ERR:
  3253. IPAERR("Got GSI_EVT_UNSUPPORTED_INTER_EE_OP_ERR\n");
  3254. break;
  3255. case GSI_EVT_EVT_RING_EMPTY_ERR:
  3256. IPAERR("Got GSI_EVT_EVT_RING_EMPTY_ERR\n");
  3257. break;
  3258. default:
  3259. IPAERR("Unexpected err evt: %d\n", notify->evt_id);
  3260. }
  3261. }
  3262. static void ipa_gsi_chan_err_cb(struct gsi_chan_err_notify *notify)
  3263. {
  3264. switch (notify->evt_id) {
  3265. case GSI_CHAN_INVALID_TRE_ERR:
  3266. IPAERR("Got GSI_CHAN_INVALID_TRE_ERR\n");
  3267. break;
  3268. case GSI_CHAN_NON_ALLOCATED_EVT_ACCESS_ERR:
  3269. IPAERR("Got GSI_CHAN_NON_ALLOCATED_EVT_ACCESS_ERR\n");
  3270. break;
  3271. case GSI_CHAN_OUT_OF_BUFFERS_ERR:
  3272. IPAERR("Got GSI_CHAN_OUT_OF_BUFFERS_ERR\n");
  3273. break;
  3274. case GSI_CHAN_OUT_OF_RESOURCES_ERR:
  3275. IPAERR("Got GSI_CHAN_OUT_OF_RESOURCES_ERR\n");
  3276. break;
  3277. case GSI_CHAN_UNSUPPORTED_INTER_EE_OP_ERR:
  3278. IPAERR("Got GSI_CHAN_UNSUPPORTED_INTER_EE_OP_ERR\n");
  3279. break;
  3280. case GSI_CHAN_HWO_1_ERR:
  3281. IPAERR("Got GSI_CHAN_HWO_1_ERR\n");
  3282. break;
  3283. default:
  3284. IPAERR("Unexpected err evt: %d\n", notify->evt_id);
  3285. }
  3286. }
  3287. static void ipa_gsi_irq_tx_notify_cb(struct gsi_chan_xfer_notify *notify)
  3288. {
  3289. struct ipa3_tx_pkt_wrapper *tx_pkt;
  3290. IPADBG_LOW("event %d notified\n", notify->evt_id);
  3291. switch (notify->evt_id) {
  3292. case GSI_CHAN_EVT_EOT:
  3293. atomic_set(&ipa3_ctx->transport_pm.eot_activity, 1);
  3294. tx_pkt = notify->xfer_user_data;
  3295. tx_pkt->xmit_done = true;
  3296. atomic_inc(&tx_pkt->sys->xmit_eot_cnt);
  3297. tasklet_schedule(&tx_pkt->sys->tasklet);
  3298. break;
  3299. default:
  3300. IPAERR("received unexpected event id %d\n", notify->evt_id);
  3301. }
  3302. }
  3303. void __ipa_gsi_irq_rx_scedule_poll(struct ipa3_sys_context *sys)
  3304. {
  3305. bool clk_off;
  3306. atomic_set(&sys->curr_polling_state, 1);
  3307. ipa3_inc_acquire_wakelock();
  3308. /*
  3309. * pm deactivate is done in wq context
  3310. * or after NAPI poll
  3311. */
  3312. clk_off = ipa_pm_activate(sys->pm_hdl);
  3313. if (!clk_off && sys->napi_obj) {
  3314. napi_schedule(sys->napi_obj);
  3315. return;
  3316. }
  3317. queue_work(sys->wq, &sys->work);
  3318. return;
  3319. }
  3320. static void ipa_gsi_irq_rx_notify_cb(struct gsi_chan_xfer_notify *notify)
  3321. {
  3322. struct ipa3_sys_context *sys;
  3323. if (!notify) {
  3324. IPAERR("gsi notify is NULL.\n");
  3325. return;
  3326. }
  3327. IPADBG_LOW("event %d notified\n", notify->evt_id);
  3328. sys = (struct ipa3_sys_context *)notify->chan_user_data;
  3329. sys->ep->xfer_notify_valid = true;
  3330. sys->ep->xfer_notify = *notify;
  3331. switch (notify->evt_id) {
  3332. case GSI_CHAN_EVT_EOT:
  3333. case GSI_CHAN_EVT_EOB:
  3334. atomic_set(&ipa3_ctx->transport_pm.eot_activity, 1);
  3335. if (!atomic_read(&sys->curr_polling_state)) {
  3336. /* put the gsi channel into polling mode */
  3337. gsi_config_channel_mode(sys->ep->gsi_chan_hdl,
  3338. GSI_CHAN_MODE_POLL);
  3339. __ipa_gsi_irq_rx_scedule_poll(sys);
  3340. }
  3341. break;
  3342. default:
  3343. IPAERR("received unexpected event id %d\n", notify->evt_id);
  3344. }
  3345. }
  3346. static void ipa_dma_gsi_irq_rx_notify_cb(struct gsi_chan_xfer_notify *notify)
  3347. {
  3348. struct ipa3_sys_context *sys;
  3349. if (!notify) {
  3350. IPAERR("gsi notify is NULL.\n");
  3351. return;
  3352. }
  3353. IPADBG_LOW("event %d notified\n", notify->evt_id);
  3354. sys = (struct ipa3_sys_context *)notify->chan_user_data;
  3355. if (sys->ep->client == IPA_CLIENT_MEMCPY_DMA_SYNC_CONS) {
  3356. IPAERR("IRQ_RX Callback was called for DMA_SYNC_CONS.\n");
  3357. return;
  3358. }
  3359. sys->ep->xfer_notify_valid = true;
  3360. sys->ep->xfer_notify = *notify;
  3361. switch (notify->evt_id) {
  3362. case GSI_CHAN_EVT_EOT:
  3363. if (!atomic_read(&sys->curr_polling_state)) {
  3364. /* put the gsi channel into polling mode */
  3365. gsi_config_channel_mode(sys->ep->gsi_chan_hdl,
  3366. GSI_CHAN_MODE_POLL);
  3367. ipa3_inc_acquire_wakelock();
  3368. atomic_set(&sys->curr_polling_state, 1);
  3369. queue_work(sys->wq, &sys->work);
  3370. }
  3371. break;
  3372. default:
  3373. IPAERR("received unexpected event id %d\n", notify->evt_id);
  3374. }
  3375. }
  3376. int ipa3_alloc_common_event_ring(void)
  3377. {
  3378. struct gsi_evt_ring_props gsi_evt_ring_props;
  3379. dma_addr_t evt_dma_addr;
  3380. int result;
  3381. memset(&gsi_evt_ring_props, 0, sizeof(gsi_evt_ring_props));
  3382. gsi_evt_ring_props.intf = GSI_EVT_CHTYPE_GPI_EV;
  3383. gsi_evt_ring_props.intr = GSI_INTR_IRQ;
  3384. gsi_evt_ring_props.re_size = GSI_EVT_RING_RE_SIZE_16B;
  3385. gsi_evt_ring_props.ring_len = IPA_COMMON_EVENT_RING_SIZE;
  3386. gsi_evt_ring_props.ring_base_vaddr =
  3387. dma_alloc_coherent(ipa3_ctx->pdev,
  3388. gsi_evt_ring_props.ring_len, &evt_dma_addr, GFP_KERNEL);
  3389. if (!gsi_evt_ring_props.ring_base_vaddr) {
  3390. IPAERR("fail to dma alloc %u bytes\n",
  3391. gsi_evt_ring_props.ring_len);
  3392. return -ENOMEM;
  3393. }
  3394. gsi_evt_ring_props.ring_base_addr = evt_dma_addr;
  3395. gsi_evt_ring_props.int_modt = 0;
  3396. gsi_evt_ring_props.int_modc = 1; /* moderation comes from channel*/
  3397. gsi_evt_ring_props.rp_update_addr = 0;
  3398. gsi_evt_ring_props.exclusive = false;
  3399. gsi_evt_ring_props.err_cb = ipa_gsi_evt_ring_err_cb;
  3400. gsi_evt_ring_props.user_data = NULL;
  3401. result = gsi_alloc_evt_ring(&gsi_evt_ring_props,
  3402. ipa3_ctx->gsi_dev_hdl, &ipa3_ctx->gsi_evt_comm_hdl);
  3403. if (result) {
  3404. IPAERR("gsi_alloc_evt_ring failed %d\n", result);
  3405. return result;
  3406. }
  3407. ipa3_ctx->gsi_evt_comm_ring_rem = IPA_COMMON_EVENT_RING_SIZE;
  3408. return 0;
  3409. }
  3410. static int ipa_gsi_setup_channel(struct ipa_sys_connect_params *in,
  3411. struct ipa3_ep_context *ep)
  3412. {
  3413. u32 ring_size;
  3414. int result;
  3415. gfp_t mem_flag = GFP_KERNEL;
  3416. u32 coale_ep_idx;
  3417. if (in->client == IPA_CLIENT_APPS_WAN_CONS ||
  3418. in->client == IPA_CLIENT_APPS_WAN_COAL_CONS ||
  3419. in->client == IPA_CLIENT_APPS_WAN_PROD)
  3420. mem_flag = GFP_ATOMIC;
  3421. if (!ep) {
  3422. IPAERR("EP context is empty\n");
  3423. return -EINVAL;
  3424. }
  3425. coale_ep_idx = ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS);
  3426. /*
  3427. * GSI ring length is calculated based on the desc_fifo_sz
  3428. * which was meant to define the BAM desc fifo. GSI descriptors
  3429. * are 16B as opposed to 8B for BAM.
  3430. */
  3431. ring_size = 2 * in->desc_fifo_sz;
  3432. ep->gsi_evt_ring_hdl = ~0;
  3433. if (ep->sys->use_comm_evt_ring) {
  3434. if (ipa3_ctx->gsi_evt_comm_ring_rem < ring_size) {
  3435. IPAERR("not enough space in common event ring\n");
  3436. IPAERR("available: %d needed: %d\n",
  3437. ipa3_ctx->gsi_evt_comm_ring_rem,
  3438. ring_size);
  3439. WARN_ON(1);
  3440. return -EFAULT;
  3441. }
  3442. ipa3_ctx->gsi_evt_comm_ring_rem -= (ring_size);
  3443. ep->gsi_evt_ring_hdl = ipa3_ctx->gsi_evt_comm_hdl;
  3444. } else if (in->client == IPA_CLIENT_APPS_WAN_CONS &&
  3445. coale_ep_idx != IPA_EP_NOT_ALLOCATED &&
  3446. ipa3_ctx->ep[coale_ep_idx].valid == 1) {
  3447. IPADBG("Wan consumer pipe configured\n");
  3448. result = ipa_gsi_setup_coal_def_channel(in, ep,
  3449. &ipa3_ctx->ep[coale_ep_idx]);
  3450. if (result) {
  3451. IPAERR("Failed to setup default coal GSI channel\n");
  3452. goto fail_setup_event_ring;
  3453. }
  3454. return result;
  3455. } else if (ep->sys->policy != IPA_POLICY_NOINTR_MODE ||
  3456. IPA_CLIENT_IS_CONS(ep->client)) {
  3457. result = ipa_gsi_setup_event_ring(ep, ring_size, mem_flag);
  3458. if (result)
  3459. goto fail_setup_event_ring;
  3460. }
  3461. result = ipa_gsi_setup_transfer_ring(ep, ring_size,
  3462. ep->sys, mem_flag);
  3463. if (result)
  3464. goto fail_setup_transfer_ring;
  3465. if (ep->client == IPA_CLIENT_MEMCPY_DMA_SYNC_CONS)
  3466. gsi_config_channel_mode(ep->gsi_chan_hdl,
  3467. GSI_CHAN_MODE_POLL);
  3468. return 0;
  3469. fail_setup_transfer_ring:
  3470. if (ep->gsi_mem_info.evt_ring_base_vaddr)
  3471. dma_free_coherent(ipa3_ctx->pdev, ep->gsi_mem_info.evt_ring_len,
  3472. ep->gsi_mem_info.evt_ring_base_vaddr,
  3473. ep->gsi_mem_info.evt_ring_base_addr);
  3474. fail_setup_event_ring:
  3475. IPAERR("Return with err: %d\n", result);
  3476. return result;
  3477. }
  3478. static int ipa_gsi_setup_event_ring(struct ipa3_ep_context *ep,
  3479. u32 ring_size, gfp_t mem_flag)
  3480. {
  3481. struct gsi_evt_ring_props gsi_evt_ring_props;
  3482. dma_addr_t evt_dma_addr;
  3483. int result;
  3484. evt_dma_addr = 0;
  3485. memset(&gsi_evt_ring_props, 0, sizeof(gsi_evt_ring_props));
  3486. gsi_evt_ring_props.intf = GSI_EVT_CHTYPE_GPI_EV;
  3487. gsi_evt_ring_props.intr = GSI_INTR_IRQ;
  3488. gsi_evt_ring_props.re_size = GSI_EVT_RING_RE_SIZE_16B;
  3489. gsi_evt_ring_props.ring_len = ring_size;
  3490. gsi_evt_ring_props.ring_base_vaddr =
  3491. dma_alloc_coherent(ipa3_ctx->pdev, gsi_evt_ring_props.ring_len,
  3492. &evt_dma_addr, mem_flag);
  3493. if (!gsi_evt_ring_props.ring_base_vaddr) {
  3494. IPAERR("fail to dma alloc %u bytes\n",
  3495. gsi_evt_ring_props.ring_len);
  3496. return -ENOMEM;
  3497. }
  3498. gsi_evt_ring_props.ring_base_addr = evt_dma_addr;
  3499. /* copy mem info */
  3500. ep->gsi_mem_info.evt_ring_len = gsi_evt_ring_props.ring_len;
  3501. ep->gsi_mem_info.evt_ring_base_addr =
  3502. gsi_evt_ring_props.ring_base_addr;
  3503. ep->gsi_mem_info.evt_ring_base_vaddr =
  3504. gsi_evt_ring_props.ring_base_vaddr;
  3505. if (ep->sys->napi_obj) {
  3506. gsi_evt_ring_props.int_modt = IPA_GSI_EVT_RING_INT_MODT;
  3507. gsi_evt_ring_props.int_modc = IPA_GSI_EVT_RING_INT_MODC;
  3508. } else {
  3509. gsi_evt_ring_props.int_modt = IPA_GSI_EVT_RING_INT_MODT;
  3510. gsi_evt_ring_props.int_modc = 1;
  3511. }
  3512. IPADBG("client=%d moderation threshold cycles=%u cnt=%u\n",
  3513. ep->client,
  3514. gsi_evt_ring_props.int_modt,
  3515. gsi_evt_ring_props.int_modc);
  3516. gsi_evt_ring_props.rp_update_addr = 0;
  3517. gsi_evt_ring_props.exclusive = true;
  3518. gsi_evt_ring_props.err_cb = ipa_gsi_evt_ring_err_cb;
  3519. gsi_evt_ring_props.user_data = NULL;
  3520. result = gsi_alloc_evt_ring(&gsi_evt_ring_props,
  3521. ipa3_ctx->gsi_dev_hdl, &ep->gsi_evt_ring_hdl);
  3522. if (result != GSI_STATUS_SUCCESS)
  3523. goto fail_alloc_evt_ring;
  3524. return 0;
  3525. fail_alloc_evt_ring:
  3526. if (ep->gsi_mem_info.evt_ring_base_vaddr)
  3527. dma_free_coherent(ipa3_ctx->pdev, ep->gsi_mem_info.evt_ring_len,
  3528. ep->gsi_mem_info.evt_ring_base_vaddr,
  3529. ep->gsi_mem_info.evt_ring_base_addr);
  3530. IPAERR("Return with err: %d\n", result);
  3531. return result;
  3532. }
  3533. static int ipa_gsi_setup_transfer_ring(struct ipa3_ep_context *ep,
  3534. u32 ring_size, struct ipa3_sys_context *user_data, gfp_t mem_flag)
  3535. {
  3536. dma_addr_t dma_addr;
  3537. union __packed gsi_channel_scratch ch_scratch;
  3538. struct gsi_chan_props gsi_channel_props;
  3539. const struct ipa_gsi_ep_config *gsi_ep_info;
  3540. int result;
  3541. memset(&gsi_channel_props, 0, sizeof(gsi_channel_props));
  3542. if (ep->client == IPA_CLIENT_APPS_WAN_COAL_CONS)
  3543. gsi_channel_props.prot = GSI_CHAN_PROT_GCI;
  3544. else
  3545. gsi_channel_props.prot = GSI_CHAN_PROT_GPI;
  3546. if (IPA_CLIENT_IS_PROD(ep->client)) {
  3547. gsi_channel_props.dir = GSI_CHAN_DIR_TO_GSI;
  3548. } else {
  3549. gsi_channel_props.dir = GSI_CHAN_DIR_FROM_GSI;
  3550. gsi_channel_props.max_re_expected = ep->sys->rx_pool_sz;
  3551. }
  3552. gsi_ep_info = ipa3_get_gsi_ep_info(ep->client);
  3553. if (!gsi_ep_info) {
  3554. IPAERR("Failed getting GSI EP info for client=%d\n",
  3555. ep->client);
  3556. result = -EINVAL;
  3557. goto fail_get_gsi_ep_info;
  3558. } else {
  3559. gsi_channel_props.ch_id = gsi_ep_info->ipa_gsi_chan_num;
  3560. }
  3561. gsi_channel_props.evt_ring_hdl = ep->gsi_evt_ring_hdl;
  3562. gsi_channel_props.re_size = GSI_CHAN_RE_SIZE_16B;
  3563. gsi_channel_props.ring_len = ring_size;
  3564. gsi_channel_props.ring_base_vaddr =
  3565. dma_alloc_coherent(ipa3_ctx->pdev, gsi_channel_props.ring_len,
  3566. &dma_addr, mem_flag);
  3567. if (!gsi_channel_props.ring_base_vaddr) {
  3568. IPAERR("fail to dma alloc %u bytes\n",
  3569. gsi_channel_props.ring_len);
  3570. result = -ENOMEM;
  3571. goto fail_alloc_channel_ring;
  3572. }
  3573. gsi_channel_props.ring_base_addr = dma_addr;
  3574. /* copy mem info */
  3575. ep->gsi_mem_info.chan_ring_len = gsi_channel_props.ring_len;
  3576. ep->gsi_mem_info.chan_ring_base_addr =
  3577. gsi_channel_props.ring_base_addr;
  3578. ep->gsi_mem_info.chan_ring_base_vaddr =
  3579. gsi_channel_props.ring_base_vaddr;
  3580. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_0)
  3581. gsi_channel_props.use_db_eng = GSI_CHAN_DIRECT_MODE;
  3582. else
  3583. gsi_channel_props.use_db_eng = GSI_CHAN_DB_MODE;
  3584. gsi_channel_props.max_prefetch = GSI_ONE_PREFETCH_SEG;
  3585. if (ep->client == IPA_CLIENT_APPS_CMD_PROD)
  3586. gsi_channel_props.low_weight = IPA_GSI_MAX_CH_LOW_WEIGHT;
  3587. else
  3588. gsi_channel_props.low_weight = 1;
  3589. gsi_channel_props.prefetch_mode = gsi_ep_info->prefetch_mode;
  3590. gsi_channel_props.empty_lvl_threshold = gsi_ep_info->prefetch_threshold;
  3591. gsi_channel_props.chan_user_data = user_data;
  3592. gsi_channel_props.err_cb = ipa_gsi_chan_err_cb;
  3593. if (IPA_CLIENT_IS_PROD(ep->client))
  3594. gsi_channel_props.xfer_cb = ipa_gsi_irq_tx_notify_cb;
  3595. else
  3596. gsi_channel_props.xfer_cb = ipa_gsi_irq_rx_notify_cb;
  3597. if (IPA_CLIENT_IS_MEMCPY_DMA_CONS(ep->client))
  3598. gsi_channel_props.xfer_cb = ipa_dma_gsi_irq_rx_notify_cb;
  3599. if (IPA_CLIENT_IS_CONS(ep->client))
  3600. gsi_channel_props.cleanup_cb = free_rx_pkt;
  3601. result = gsi_alloc_channel(&gsi_channel_props, ipa3_ctx->gsi_dev_hdl,
  3602. &ep->gsi_chan_hdl);
  3603. if (result != GSI_STATUS_SUCCESS) {
  3604. IPAERR("Failed to alloc GSI chan.\n");
  3605. goto fail_alloc_channel;
  3606. }
  3607. memset(&ch_scratch, 0, sizeof(ch_scratch));
  3608. /*
  3609. * Update scratch for MCS smart prefetch:
  3610. * Starting IPA4.5, smart prefetch implemented by H/W.
  3611. * At IPA 4.0/4.1/4.2, we do not use MCS smart prefetch
  3612. * so keep the fields zero.
  3613. */
  3614. if (ipa3_ctx->ipa_hw_type < IPA_HW_v4_0) {
  3615. ch_scratch.gpi.max_outstanding_tre =
  3616. gsi_ep_info->ipa_if_tlv * GSI_CHAN_RE_SIZE_16B;
  3617. ch_scratch.gpi.outstanding_threshold =
  3618. 2 * GSI_CHAN_RE_SIZE_16B;
  3619. }
  3620. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_5)
  3621. ch_scratch.gpi.dl_nlo_channel = 0;
  3622. result = gsi_write_channel_scratch(ep->gsi_chan_hdl, ch_scratch);
  3623. if (result != GSI_STATUS_SUCCESS) {
  3624. IPAERR("failed to write scratch %d\n", result);
  3625. goto fail_write_channel_scratch;
  3626. }
  3627. return 0;
  3628. fail_write_channel_scratch:
  3629. if (gsi_dealloc_channel(ep->gsi_chan_hdl)
  3630. != GSI_STATUS_SUCCESS) {
  3631. IPAERR("Failed to dealloc GSI chan.\n");
  3632. WARN_ON(1);
  3633. }
  3634. fail_alloc_channel:
  3635. dma_free_coherent(ipa3_ctx->pdev, ep->gsi_mem_info.chan_ring_len,
  3636. ep->gsi_mem_info.chan_ring_base_vaddr,
  3637. ep->gsi_mem_info.chan_ring_base_addr);
  3638. fail_alloc_channel_ring:
  3639. fail_get_gsi_ep_info:
  3640. if (ep->gsi_evt_ring_hdl != ~0) {
  3641. gsi_dealloc_evt_ring(ep->gsi_evt_ring_hdl);
  3642. ep->gsi_evt_ring_hdl = ~0;
  3643. }
  3644. return result;
  3645. }
  3646. static int ipa_gsi_setup_coal_def_channel(struct ipa_sys_connect_params *in,
  3647. struct ipa3_ep_context *ep, struct ipa3_ep_context *coal_ep)
  3648. {
  3649. u32 ring_size;
  3650. int result;
  3651. ring_size = 2 * in->desc_fifo_sz;
  3652. /* copy event ring handle */
  3653. ep->gsi_evt_ring_hdl = coal_ep->gsi_evt_ring_hdl;
  3654. result = ipa_gsi_setup_transfer_ring(ep, ring_size,
  3655. coal_ep->sys, GFP_ATOMIC);
  3656. if (result) {
  3657. if (ep->gsi_mem_info.evt_ring_base_vaddr)
  3658. dma_free_coherent(ipa3_ctx->pdev,
  3659. ep->gsi_mem_info.chan_ring_len,
  3660. ep->gsi_mem_info.chan_ring_base_vaddr,
  3661. ep->gsi_mem_info.chan_ring_base_addr);
  3662. IPAERR("Destroying WAN_COAL_CONS evt_ring");
  3663. if (ep->gsi_evt_ring_hdl != ~0) {
  3664. gsi_dealloc_evt_ring(ep->gsi_evt_ring_hdl);
  3665. ep->gsi_evt_ring_hdl = ~0;
  3666. }
  3667. IPAERR("Return with err: %d\n", result);
  3668. return result;
  3669. }
  3670. return 0;
  3671. }
  3672. static int ipa_populate_tag_field(struct ipa3_desc *desc,
  3673. struct ipa3_tx_pkt_wrapper *tx_pkt,
  3674. struct ipahal_imm_cmd_pyld **tag_pyld_ret)
  3675. {
  3676. struct ipahal_imm_cmd_pyld *tag_pyld;
  3677. struct ipahal_imm_cmd_ip_packet_tag_status tag_cmd = {0};
  3678. /* populate tag field only if it is NULL */
  3679. if (desc->pyld == NULL) {
  3680. tag_cmd.tag = pointer_to_tag_wa(tx_pkt);
  3681. tag_pyld = ipahal_construct_imm_cmd(
  3682. IPA_IMM_CMD_IP_PACKET_TAG_STATUS, &tag_cmd, true);
  3683. if (unlikely(!tag_pyld)) {
  3684. IPAERR("Failed to construct ip_packet_tag_status\n");
  3685. return -EFAULT;
  3686. }
  3687. /*
  3688. * This is for 32-bit pointer, will need special
  3689. * handling if 64-bit pointer is used
  3690. */
  3691. IPADBG_LOW("tx_pkt sent in tag: 0x%pK\n", tx_pkt);
  3692. desc->pyld = tag_pyld->data;
  3693. desc->opcode = tag_pyld->opcode;
  3694. desc->len = tag_pyld->len;
  3695. desc->user1 = tag_pyld;
  3696. desc->type = IPA_IMM_CMD_DESC;
  3697. desc->callback = ipa3_tag_destroy_imm;
  3698. *tag_pyld_ret = tag_pyld;
  3699. }
  3700. return 0;
  3701. }
  3702. static int ipa_poll_gsi_pkt(struct ipa3_sys_context *sys,
  3703. struct gsi_chan_xfer_notify *notify)
  3704. {
  3705. int unused_var;
  3706. return ipa_poll_gsi_n_pkt(sys, notify, 1, &unused_var);
  3707. }
  3708. static int ipa_poll_gsi_n_pkt(struct ipa3_sys_context *sys,
  3709. struct gsi_chan_xfer_notify *notify,
  3710. int expected_num, int *actual_num)
  3711. {
  3712. int ret;
  3713. int idx = 0;
  3714. int poll_num = 0;
  3715. if (!actual_num || expected_num <= 0 ||
  3716. expected_num > IPA_WAN_NAPI_MAX_FRAMES) {
  3717. IPAERR("bad params actual_num=%pK expected_num=%d\n",
  3718. actual_num, expected_num);
  3719. return GSI_STATUS_INVALID_PARAMS;
  3720. }
  3721. if (sys->ep->xfer_notify_valid) {
  3722. *notify = sys->ep->xfer_notify;
  3723. sys->ep->xfer_notify_valid = false;
  3724. idx++;
  3725. }
  3726. if (expected_num == idx) {
  3727. *actual_num = idx;
  3728. return GSI_STATUS_SUCCESS;
  3729. }
  3730. ret = gsi_poll_n_channel(sys->ep->gsi_chan_hdl,
  3731. &notify[idx], expected_num - idx, &poll_num);
  3732. if (ret == GSI_STATUS_POLL_EMPTY) {
  3733. if (idx) {
  3734. *actual_num = idx;
  3735. return GSI_STATUS_SUCCESS;
  3736. }
  3737. *actual_num = 0;
  3738. return ret;
  3739. } else if (ret != GSI_STATUS_SUCCESS) {
  3740. if (idx) {
  3741. *actual_num = idx;
  3742. return GSI_STATUS_SUCCESS;
  3743. }
  3744. *actual_num = 0;
  3745. IPAERR("Poll channel err: %d\n", ret);
  3746. return ret;
  3747. }
  3748. *actual_num = idx + poll_num;
  3749. return ret;
  3750. }
  3751. /**
  3752. * ipa3_rx_poll() - Poll the rx packets from IPA HW. This
  3753. * function is exectued in the softirq context
  3754. *
  3755. * if input budget is zero, the driver switches back to
  3756. * interrupt mode.
  3757. *
  3758. * return number of polled packets, on error 0(zero)
  3759. */
  3760. int ipa3_rx_poll(u32 clnt_hdl, int weight)
  3761. {
  3762. struct ipa3_ep_context *ep;
  3763. int ret;
  3764. int cnt = 0;
  3765. int num = 0;
  3766. int remain_aggr_weight;
  3767. struct ipa_active_client_logging_info log;
  3768. struct gsi_chan_xfer_notify notify[IPA_WAN_NAPI_MAX_FRAMES];
  3769. IPA_ACTIVE_CLIENTS_PREP_SPECIAL(log, "NAPI");
  3770. if (clnt_hdl >= ipa3_ctx->ipa_num_pipes ||
  3771. ipa3_ctx->ep[clnt_hdl].valid == 0) {
  3772. IPAERR("bad parm 0x%x\n", clnt_hdl);
  3773. return cnt;
  3774. }
  3775. remain_aggr_weight = weight / IPA_WAN_AGGR_PKT_CNT;
  3776. if (remain_aggr_weight > IPA_WAN_NAPI_MAX_FRAMES) {
  3777. IPAERR("NAPI weight is higher than expected\n");
  3778. IPAERR("expected %d got %d\n",
  3779. IPA_WAN_NAPI_MAX_FRAMES, remain_aggr_weight);
  3780. return -EINVAL;
  3781. }
  3782. ep = &ipa3_ctx->ep[clnt_hdl];
  3783. start_poll:
  3784. while (remain_aggr_weight > 0 &&
  3785. atomic_read(&ep->sys->curr_polling_state)) {
  3786. atomic_set(&ipa3_ctx->transport_pm.eot_activity, 1);
  3787. if (ipa3_ctx->enable_napi_chain) {
  3788. ret = ipa_poll_gsi_n_pkt(ep->sys, notify,
  3789. remain_aggr_weight, &num);
  3790. } else {
  3791. ret = ipa_poll_gsi_n_pkt(ep->sys, notify,
  3792. 1, &num);
  3793. }
  3794. if (ret)
  3795. break;
  3796. trace_ipa3_rx_poll_num(num);
  3797. ipa3_wq_rx_napi_chain(ep->sys, notify, num);
  3798. remain_aggr_weight -= num;
  3799. trace_ipa3_rx_poll_cnt(ep->sys->len);
  3800. if (ep->sys->len == 0) {
  3801. if (remain_aggr_weight == 0)
  3802. cnt--;
  3803. break;
  3804. }
  3805. }
  3806. cnt += weight - remain_aggr_weight * IPA_WAN_AGGR_PKT_CNT;
  3807. /* call repl_hdlr before napi_reschedule / napi_complete */
  3808. if (cnt)
  3809. ep->sys->repl_hdlr(ep->sys);
  3810. if (cnt < weight) {
  3811. napi_complete(ep->sys->napi_obj);
  3812. ret = ipa3_rx_switch_to_intr_mode(ep->sys);
  3813. if (ret == -GSI_STATUS_PENDING_IRQ &&
  3814. napi_reschedule(ep->sys->napi_obj))
  3815. goto start_poll;
  3816. ipa_pm_deferred_deactivate(ep->sys->pm_hdl);
  3817. }
  3818. return cnt;
  3819. }
  3820. static unsigned long tag_to_pointer_wa(uint64_t tag)
  3821. {
  3822. return 0xFFFF000000000000 | (unsigned long) tag;
  3823. }
  3824. static uint64_t pointer_to_tag_wa(struct ipa3_tx_pkt_wrapper *tx_pkt)
  3825. {
  3826. u16 temp;
  3827. /* Add the check but it might have throughput issue */
  3828. if (BITS_PER_LONG == 64) {
  3829. temp = (u16) (~((unsigned long) tx_pkt &
  3830. 0xFFFF000000000000) >> 48);
  3831. if (temp) {
  3832. IPAERR("The 16 prefix is not all 1s (%pK)\n",
  3833. tx_pkt);
  3834. /*
  3835. * We need all addresses starting at 0xFFFF to
  3836. * pass it to HW.
  3837. */
  3838. ipa_assert();
  3839. }
  3840. }
  3841. return (unsigned long)tx_pkt & 0x0000FFFFFFFFFFFF;
  3842. }
  3843. /**
  3844. * ipa_gsi_ch20_wa() - software workaround for IPA GSI channel 20
  3845. *
  3846. * A hardware limitation requires to avoid using GSI physical channel 20.
  3847. * This function allocates GSI physical channel 20 and holds it to prevent
  3848. * others to use it.
  3849. *
  3850. * Return codes: 0 on success, negative on failure
  3851. */
  3852. int ipa_gsi_ch20_wa(void)
  3853. {
  3854. struct gsi_chan_props gsi_channel_props;
  3855. dma_addr_t dma_addr;
  3856. int result;
  3857. int i;
  3858. unsigned long chan_hdl[IPA_GSI_CH_20_WA_NUM_CH_TO_ALLOC];
  3859. unsigned long chan_hdl_to_keep;
  3860. memset(&gsi_channel_props, 0, sizeof(gsi_channel_props));
  3861. gsi_channel_props.prot = GSI_CHAN_PROT_GPI;
  3862. gsi_channel_props.dir = GSI_CHAN_DIR_TO_GSI;
  3863. gsi_channel_props.evt_ring_hdl = ~0;
  3864. gsi_channel_props.re_size = GSI_CHAN_RE_SIZE_16B;
  3865. gsi_channel_props.ring_len = 4 * gsi_channel_props.re_size;
  3866. gsi_channel_props.ring_base_vaddr =
  3867. dma_alloc_coherent(ipa3_ctx->pdev, gsi_channel_props.ring_len,
  3868. &dma_addr, 0);
  3869. gsi_channel_props.ring_base_addr = dma_addr;
  3870. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_0)
  3871. gsi_channel_props.use_db_eng = GSI_CHAN_DIRECT_MODE;
  3872. else
  3873. gsi_channel_props.use_db_eng = GSI_CHAN_DB_MODE;
  3874. gsi_channel_props.max_prefetch = GSI_ONE_PREFETCH_SEG;
  3875. gsi_channel_props.low_weight = 1;
  3876. gsi_channel_props.err_cb = ipa_gsi_chan_err_cb;
  3877. gsi_channel_props.xfer_cb = ipa_gsi_irq_tx_notify_cb;
  3878. /* first allocate channels up to channel 20 */
  3879. for (i = 0; i < IPA_GSI_CH_20_WA_NUM_CH_TO_ALLOC; i++) {
  3880. gsi_channel_props.ch_id = i;
  3881. result = gsi_alloc_channel(&gsi_channel_props,
  3882. ipa3_ctx->gsi_dev_hdl,
  3883. &chan_hdl[i]);
  3884. if (result != GSI_STATUS_SUCCESS) {
  3885. IPAERR("failed to alloc channel %d err %d\n",
  3886. i, result);
  3887. return result;
  3888. }
  3889. }
  3890. /* allocate channel 20 */
  3891. gsi_channel_props.ch_id = IPA_GSI_CH_20_WA_VIRT_CHAN;
  3892. result = gsi_alloc_channel(&gsi_channel_props, ipa3_ctx->gsi_dev_hdl,
  3893. &chan_hdl_to_keep);
  3894. if (result != GSI_STATUS_SUCCESS) {
  3895. IPAERR("failed to alloc channel %d err %d\n",
  3896. i, result);
  3897. return result;
  3898. }
  3899. /* release all other channels */
  3900. for (i = 0; i < IPA_GSI_CH_20_WA_NUM_CH_TO_ALLOC; i++) {
  3901. result = gsi_dealloc_channel(chan_hdl[i]);
  3902. if (result != GSI_STATUS_SUCCESS) {
  3903. IPAERR("failed to dealloc channel %d err %d\n",
  3904. i, result);
  3905. return result;
  3906. }
  3907. }
  3908. /* DMA memory shall not be freed as it is used by channel 20 */
  3909. return 0;
  3910. }
  3911. /**
  3912. * ipa_adjust_ra_buff_base_sz()
  3913. *
  3914. * Return value: the largest power of two which is smaller
  3915. * than the input value
  3916. */
  3917. static u32 ipa_adjust_ra_buff_base_sz(u32 aggr_byte_limit)
  3918. {
  3919. aggr_byte_limit += IPA_MTU;
  3920. aggr_byte_limit += IPA_GENERIC_RX_BUFF_LIMIT;
  3921. aggr_byte_limit--;
  3922. aggr_byte_limit |= aggr_byte_limit >> 1;
  3923. aggr_byte_limit |= aggr_byte_limit >> 2;
  3924. aggr_byte_limit |= aggr_byte_limit >> 4;
  3925. aggr_byte_limit |= aggr_byte_limit >> 8;
  3926. aggr_byte_limit |= aggr_byte_limit >> 16;
  3927. aggr_byte_limit++;
  3928. return aggr_byte_limit >> 1;
  3929. }