ipa_dp.c 128 KB

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