ipa_dp.c 127 KB

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