hal_halmac.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2015 - 2018 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. #define _HAL_HALMAC_C_
  16. #include <drv_types.h> /* PADAPTER, struct dvobj_priv, SDIO_ERR_VAL8 and etc. */
  17. #include <hal_data.h> /* efuse, PHAL_DATA_TYPE and etc. */
  18. #include "hal_halmac.h" /* dvobj_to_halmac() and ect. */
  19. /*
  20. * HALMAC take return value 0 for fail and 1 for success to replace
  21. * _FALSE/_TRUE after V1_04_09
  22. */
  23. #define RTW_HALMAC_FAIL 0
  24. #define RTW_HALMAC_SUCCESS 1
  25. #define DEFAULT_INDICATOR_TIMELMT 1000 /* ms */
  26. #define MSG_PREFIX "[HALMAC]"
  27. #define RTW_HALMAC_DLFW_MEM_NO_STOP_TX
  28. /*
  29. * Driver API for HALMAC operations
  30. */
  31. #ifdef CONFIG_SDIO_HCI
  32. #include <rtw_sdio.h>
  33. static u8 _halmac_mac_reg_page0_chk(const char *func, struct dvobj_priv *dvobj, u32 offset)
  34. {
  35. #if defined(CONFIG_IO_CHECK_IN_ANA_LOW_CLK) && defined(CONFIG_LPS_LCLK)
  36. struct pwrctrl_priv *pwrpriv = &dvobj->pwrctl_priv;
  37. u32 mac_reg_offset = 0;
  38. if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)
  39. return _TRUE;
  40. if (pwrpriv->lps_level == LPS_NORMAL)
  41. return _TRUE;
  42. if (pwrpriv->rpwm >= PS_STATE_S2)
  43. return _TRUE;
  44. if (offset & (WLAN_IOREG_DEVICE_ID << 13)) { /*WLAN_IOREG_OFFSET*/
  45. mac_reg_offset = offset & HALMAC_WLAN_MAC_REG_MSK;
  46. if (mac_reg_offset < 0x100) {
  47. RTW_ERR(FUNC_ADPT_FMT
  48. "access MAC REG -0x%04x in PS-mode:0x%02x (rpwm:0x%02x, lps_level:0x%02x)\n",
  49. FUNC_ADPT_ARG(dvobj_get_primary_adapter(dvobj)), mac_reg_offset,
  50. pwrpriv->pwr_mode, pwrpriv->rpwm, pwrpriv->lps_level);
  51. rtw_warn_on(1);
  52. return _FALSE;
  53. }
  54. }
  55. #endif
  56. return _TRUE;
  57. }
  58. static u8 _halmac_sdio_cmd52_read(void *p, u32 offset)
  59. {
  60. struct dvobj_priv *d;
  61. u8 val;
  62. u8 ret;
  63. d = (struct dvobj_priv *)p;
  64. _halmac_mac_reg_page0_chk(__func__, d, offset);
  65. ret = rtw_sdio_read_cmd52(d, offset, &val, 1);
  66. if (_FAIL == ret) {
  67. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  68. return SDIO_ERR_VAL8;
  69. }
  70. return val;
  71. }
  72. static void _halmac_sdio_cmd52_write(void *p, u32 offset, u8 val)
  73. {
  74. struct dvobj_priv *d;
  75. u8 ret;
  76. d = (struct dvobj_priv *)p;
  77. _halmac_mac_reg_page0_chk(__func__, d, offset);
  78. ret = rtw_sdio_write_cmd52(d, offset, &val, 1);
  79. if (_FAIL == ret)
  80. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  81. }
  82. static u8 _halmac_sdio_reg_read_8(void *p, u32 offset)
  83. {
  84. struct dvobj_priv *d;
  85. u8 *pbuf;
  86. u8 val;
  87. u8 ret;
  88. d = (struct dvobj_priv *)p;
  89. val = SDIO_ERR_VAL8;
  90. _halmac_mac_reg_page0_chk(__func__, d, offset);
  91. pbuf = rtw_zmalloc(1);
  92. if (!pbuf)
  93. return val;
  94. ret = rtw_sdio_read_cmd53(d, offset, pbuf, 1);
  95. if (ret == _FAIL) {
  96. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  97. goto exit;
  98. }
  99. val = *pbuf;
  100. exit:
  101. rtw_mfree(pbuf, 1);
  102. return val;
  103. }
  104. static u16 _halmac_sdio_reg_read_16(void *p, u32 offset)
  105. {
  106. struct dvobj_priv *d;
  107. u8 *pbuf;
  108. u16 val;
  109. u8 ret;
  110. d = (struct dvobj_priv *)p;
  111. val = SDIO_ERR_VAL16;
  112. _halmac_mac_reg_page0_chk(__func__, d, offset);
  113. pbuf = rtw_zmalloc(2);
  114. if (!pbuf)
  115. return val;
  116. ret = rtw_sdio_read_cmd53(d, offset, pbuf, 2);
  117. if (ret == _FAIL) {
  118. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  119. goto exit;
  120. }
  121. val = le16_to_cpu(*(u16 *)pbuf);
  122. exit:
  123. rtw_mfree(pbuf, 2);
  124. return val;
  125. }
  126. static u32 _halmac_sdio_reg_read_32(void *p, u32 offset)
  127. {
  128. struct dvobj_priv *d;
  129. u8 *pbuf;
  130. u32 val;
  131. u8 ret;
  132. d = (struct dvobj_priv *)p;
  133. val = SDIO_ERR_VAL32;
  134. _halmac_mac_reg_page0_chk(__func__, d, offset);
  135. pbuf = rtw_zmalloc(4);
  136. if (!pbuf)
  137. return val;
  138. ret = rtw_sdio_read_cmd53(d, offset, pbuf, 4);
  139. if (ret == _FAIL) {
  140. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  141. goto exit;
  142. }
  143. val = le32_to_cpu(*(u32 *)pbuf);
  144. exit:
  145. rtw_mfree(pbuf, 4);
  146. return val;
  147. }
  148. static u8 _halmac_sdio_reg_read_n(void *p, u32 offset, u32 size, u8 *data)
  149. {
  150. struct dvobj_priv *d = (struct dvobj_priv *)p;
  151. u8 *pbuf;
  152. u8 ret;
  153. u8 rst = RTW_HALMAC_FAIL;
  154. u32 sdio_read_size;
  155. sdio_read_size = RND4(size);
  156. sdio_read_size = rtw_sdio_cmd53_align_size(d, sdio_read_size);
  157. pbuf = rtw_zmalloc(sdio_read_size);
  158. if ((!pbuf) || (!data))
  159. return rst;
  160. ret = rtw_sdio_read_cmd53(d, offset, pbuf, sdio_read_size);
  161. if (ret == _FAIL) {
  162. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  163. goto exit;
  164. }
  165. _rtw_memcpy(data, pbuf, size);
  166. rst = RTW_HALMAC_SUCCESS;
  167. exit:
  168. rtw_mfree(pbuf, sdio_read_size);
  169. return rst;
  170. }
  171. static void _halmac_sdio_reg_write_8(void *p, u32 offset, u8 val)
  172. {
  173. struct dvobj_priv *d;
  174. u8 *pbuf;
  175. u8 ret;
  176. d = (struct dvobj_priv *)p;
  177. _halmac_mac_reg_page0_chk(__func__, d, offset);
  178. pbuf = rtw_zmalloc(1);
  179. if (!pbuf)
  180. return;
  181. _rtw_memcpy(pbuf, &val, 1);
  182. ret = rtw_sdio_write_cmd53(d, offset, pbuf, 1);
  183. if (ret == _FAIL)
  184. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  185. rtw_mfree(pbuf, 1);
  186. }
  187. static void _halmac_sdio_reg_write_16(void *p, u32 offset, u16 val)
  188. {
  189. struct dvobj_priv *d;
  190. u8 *pbuf;
  191. u8 ret;
  192. d = (struct dvobj_priv *)p;
  193. _halmac_mac_reg_page0_chk(__func__, d, offset);
  194. val = cpu_to_le16(val);
  195. pbuf = rtw_zmalloc(2);
  196. if (!pbuf)
  197. return;
  198. _rtw_memcpy(pbuf, &val, 2);
  199. ret = rtw_sdio_write_cmd53(d, offset, pbuf, 2);
  200. if (ret == _FAIL)
  201. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  202. rtw_mfree(pbuf, 2);
  203. }
  204. static void _halmac_sdio_reg_write_32(void *p, u32 offset, u32 val)
  205. {
  206. struct dvobj_priv *d;
  207. u8 *pbuf;
  208. u8 ret;
  209. d = (struct dvobj_priv *)p;
  210. _halmac_mac_reg_page0_chk(__func__, d, offset);
  211. val = cpu_to_le32(val);
  212. pbuf = rtw_zmalloc(4);
  213. if (!pbuf)
  214. return;
  215. _rtw_memcpy(pbuf, &val, 4);
  216. ret = rtw_sdio_write_cmd53(d, offset, pbuf, 4);
  217. if (ret == _FAIL)
  218. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  219. rtw_mfree(pbuf, 4);
  220. }
  221. static u8 _halmac_sdio_read_cia(void *p, u32 offset)
  222. {
  223. struct dvobj_priv *d;
  224. u8 data = 0;
  225. u8 ret;
  226. d = (struct dvobj_priv *)p;
  227. ret = rtw_sdio_f0_read(d, offset, &data, 1);
  228. if (ret == _FAIL)
  229. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  230. return data;
  231. }
  232. #else /* !CONFIG_SDIO_HCI */
  233. static u8 _halmac_reg_read_8(void *p, u32 offset)
  234. {
  235. struct dvobj_priv *d;
  236. PADAPTER adapter;
  237. d = (struct dvobj_priv *)p;
  238. adapter = dvobj_get_primary_adapter(d);
  239. return rtw_read8(adapter, offset);
  240. }
  241. static u16 _halmac_reg_read_16(void *p, u32 offset)
  242. {
  243. struct dvobj_priv *d;
  244. PADAPTER adapter;
  245. d = (struct dvobj_priv *)p;
  246. adapter = dvobj_get_primary_adapter(d);
  247. return rtw_read16(adapter, offset);
  248. }
  249. static u32 _halmac_reg_read_32(void *p, u32 offset)
  250. {
  251. struct dvobj_priv *d;
  252. PADAPTER adapter;
  253. d = (struct dvobj_priv *)p;
  254. adapter = dvobj_get_primary_adapter(d);
  255. return rtw_read32(adapter, offset);
  256. }
  257. static void _halmac_reg_write_8(void *p, u32 offset, u8 val)
  258. {
  259. struct dvobj_priv *d;
  260. PADAPTER adapter;
  261. int err;
  262. d = (struct dvobj_priv *)p;
  263. adapter = dvobj_get_primary_adapter(d);
  264. err = rtw_write8(adapter, offset, val);
  265. if (err == _FAIL)
  266. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  267. }
  268. static void _halmac_reg_write_16(void *p, u32 offset, u16 val)
  269. {
  270. struct dvobj_priv *d;
  271. PADAPTER adapter;
  272. int err;
  273. d = (struct dvobj_priv *)p;
  274. adapter = dvobj_get_primary_adapter(d);
  275. err = rtw_write16(adapter, offset, val);
  276. if (err == _FAIL)
  277. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  278. }
  279. static void _halmac_reg_write_32(void *p, u32 offset, u32 val)
  280. {
  281. struct dvobj_priv *d;
  282. PADAPTER adapter;
  283. int err;
  284. d = (struct dvobj_priv *)p;
  285. adapter = dvobj_get_primary_adapter(d);
  286. err = rtw_write32(adapter, offset, val);
  287. if (err == _FAIL)
  288. RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);
  289. }
  290. #endif /* !CONFIG_SDIO_HCI */
  291. static u8 _halmac_mfree(void *p, void *buffer, u32 size)
  292. {
  293. rtw_mfree(buffer, size);
  294. return RTW_HALMAC_SUCCESS;
  295. }
  296. static void *_halmac_malloc(void *p, u32 size)
  297. {
  298. return rtw_zmalloc(size);
  299. }
  300. static u8 _halmac_memcpy(void *p, void *dest, void *src, u32 size)
  301. {
  302. _rtw_memcpy(dest, src, size);
  303. return RTW_HALMAC_SUCCESS;
  304. }
  305. static u8 _halmac_memset(void *p, void *addr, u8 value, u32 size)
  306. {
  307. _rtw_memset(addr, value, size);
  308. return RTW_HALMAC_SUCCESS;
  309. }
  310. static void _halmac_udelay(void *p, u32 us)
  311. {
  312. /* Most hardware polling wait time < 50us) */
  313. if (us <= 50)
  314. rtw_udelay_os(us);
  315. else if (us <= 1000)
  316. rtw_usleep_os(us);
  317. else
  318. rtw_msleep_os(RTW_DIV_ROUND_UP(us, 1000));
  319. }
  320. static u8 _halmac_mutex_init(void *p, HALMAC_MUTEX *pMutex)
  321. {
  322. _rtw_mutex_init(pMutex);
  323. return RTW_HALMAC_SUCCESS;
  324. }
  325. static u8 _halmac_mutex_deinit(void *p, HALMAC_MUTEX *pMutex)
  326. {
  327. _rtw_mutex_free(pMutex);
  328. return RTW_HALMAC_SUCCESS;
  329. }
  330. static u8 _halmac_mutex_lock(void *p, HALMAC_MUTEX *pMutex)
  331. {
  332. int err;
  333. err = _enter_critical_mutex(pMutex, NULL);
  334. if (err)
  335. return RTW_HALMAC_FAIL;
  336. return RTW_HALMAC_SUCCESS;
  337. }
  338. static u8 _halmac_mutex_unlock(void *p, HALMAC_MUTEX *pMutex)
  339. {
  340. _exit_critical_mutex(pMutex, NULL);
  341. return RTW_HALMAC_SUCCESS;
  342. }
  343. static u8 _halmac_msg_print(void *p, u32 msg_type, u8 msg_level, s8 *fmt, ...)
  344. {
  345. #define MSG_LEN 100
  346. va_list args;
  347. u8 str[MSG_LEN] = {0};
  348. int err;
  349. u8 ret = RTW_HALMAC_SUCCESS;
  350. str[0] = '\n';
  351. va_start(args, fmt);
  352. err = vsnprintf(str, MSG_LEN, fmt, args);
  353. va_end(args);
  354. /* An output error is encountered */
  355. if (err < 0)
  356. return RTW_HALMAC_FAIL;
  357. /* Output may be truncated due to size limit */
  358. if ((err == (MSG_LEN - 1)) && (str[MSG_LEN - 2] != '\n'))
  359. ret = RTW_HALMAC_FAIL;
  360. if (msg_level == HALMAC_DBG_ALWAYS)
  361. RTW_PRINT(MSG_PREFIX "%s", str);
  362. else if (msg_level <= HALMAC_DBG_ERR)
  363. RTW_ERR(MSG_PREFIX "%s", str);
  364. else if (msg_level <= HALMAC_DBG_WARN)
  365. RTW_WARN(MSG_PREFIX "%s", str);
  366. else
  367. RTW_DBG(MSG_PREFIX "%s", str);
  368. return ret;
  369. }
  370. static u8 _halmac_buff_print(void *p, u32 msg_type, u8 msg_level, s8 *buf, u32 size)
  371. {
  372. if (msg_level <= HALMAC_DBG_WARN)
  373. RTW_INFO_DUMP(MSG_PREFIX, buf, size);
  374. else
  375. RTW_DBG_DUMP(MSG_PREFIX, buf, size);
  376. return RTW_HALMAC_SUCCESS;
  377. }
  378. const char *const RTW_HALMAC_FEATURE_NAME[] = {
  379. "HALMAC_FEATURE_CFG_PARA",
  380. "HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE",
  381. "HALMAC_FEATURE_DUMP_LOGICAL_EFUSE",
  382. "HALMAC_FEATURE_UPDATE_PACKET",
  383. "HALMAC_FEATURE_UPDATE_DATAPACK",
  384. "HALMAC_FEATURE_RUN_DATAPACK",
  385. "HALMAC_FEATURE_CHANNEL_SWITCH",
  386. "HALMAC_FEATURE_IQK",
  387. "HALMAC_FEATURE_POWER_TRACKING",
  388. "HALMAC_FEATURE_PSD",
  389. "HALMAC_FEATURE_FW_SNDING",
  390. "HALMAC_FEATURE_ALL"
  391. };
  392. static inline u8 is_valid_id_status(enum halmac_feature_id id, enum halmac_cmd_process_status status)
  393. {
  394. switch (id) {
  395. case HALMAC_FEATURE_CFG_PARA:
  396. RTW_DBG("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  397. break;
  398. case HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE:
  399. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  400. if (HALMAC_CMD_PROCESS_DONE != status)
  401. RTW_INFO("%s: id(%d) unspecified status(%d)!\n",
  402. __FUNCTION__, id, status);
  403. break;
  404. case HALMAC_FEATURE_DUMP_LOGICAL_EFUSE:
  405. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  406. if (HALMAC_CMD_PROCESS_DONE != status)
  407. RTW_INFO("%s: id(%d) unspecified status(%d)!\n",
  408. __FUNCTION__, id, status);
  409. break;
  410. case HALMAC_FEATURE_UPDATE_PACKET:
  411. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  412. break;
  413. case HALMAC_FEATURE_UPDATE_DATAPACK:
  414. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  415. break;
  416. case HALMAC_FEATURE_RUN_DATAPACK:
  417. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  418. break;
  419. case HALMAC_FEATURE_CHANNEL_SWITCH:
  420. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  421. break;
  422. case HALMAC_FEATURE_IQK:
  423. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  424. break;
  425. case HALMAC_FEATURE_POWER_TRACKING:
  426. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  427. break;
  428. case HALMAC_FEATURE_PSD:
  429. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  430. break;
  431. case HALMAC_FEATURE_FW_SNDING:
  432. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  433. break;
  434. case HALMAC_FEATURE_ALL:
  435. RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);
  436. break;
  437. default:
  438. RTW_ERR("%s: unknown feature id(%d)\n", __FUNCTION__, id);
  439. return _FALSE;
  440. }
  441. return _TRUE;
  442. }
  443. static int init_halmac_event_with_waittime(struct dvobj_priv *d, enum halmac_feature_id id, u8 *buf, u32 size, u32 time)
  444. {
  445. struct submit_ctx *sctx;
  446. if (!d->hmpriv.indicator[id].sctx) {
  447. sctx = (struct submit_ctx *)rtw_zmalloc(sizeof(*sctx));
  448. if (!sctx)
  449. return -1;
  450. } else {
  451. RTW_WARN("%s: id(%d) sctx is not NULL!!\n", __FUNCTION__, id);
  452. sctx = d->hmpriv.indicator[id].sctx;
  453. d->hmpriv.indicator[id].sctx = NULL;
  454. }
  455. rtw_sctx_init(sctx, time);
  456. d->hmpriv.indicator[id].buffer = buf;
  457. d->hmpriv.indicator[id].buf_size = size;
  458. d->hmpriv.indicator[id].ret_size = 0;
  459. d->hmpriv.indicator[id].status = 0;
  460. /* fill sctx at least to sure other variables are all ready! */
  461. d->hmpriv.indicator[id].sctx = sctx;
  462. return 0;
  463. }
  464. static inline int init_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id, u8 *buf, u32 size)
  465. {
  466. return init_halmac_event_with_waittime(d, id, buf, size, DEFAULT_INDICATOR_TIMELMT);
  467. }
  468. static void free_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id)
  469. {
  470. struct submit_ctx *sctx;
  471. if (!d->hmpriv.indicator[id].sctx)
  472. return;
  473. sctx = d->hmpriv.indicator[id].sctx;
  474. d->hmpriv.indicator[id].sctx = NULL;
  475. rtw_mfree((u8 *)sctx, sizeof(*sctx));
  476. }
  477. static int wait_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id)
  478. {
  479. struct halmac_adapter *mac;
  480. struct halmac_api *api;
  481. struct submit_ctx *sctx;
  482. int ret;
  483. sctx = d->hmpriv.indicator[id].sctx;
  484. if (!sctx)
  485. return -1;
  486. ret = rtw_sctx_wait(sctx, RTW_HALMAC_FEATURE_NAME[id]);
  487. free_halmac_event(d, id);
  488. if (_SUCCESS == ret)
  489. return 0;
  490. /* timeout! We have to reset halmac state */
  491. RTW_ERR("%s: Wait id(%d, %s) TIMEOUT! Reset HALMAC state!\n",
  492. __FUNCTION__, id, RTW_HALMAC_FEATURE_NAME[id]);
  493. mac = dvobj_to_halmac(d);
  494. api = HALMAC_GET_API(mac);
  495. api->halmac_reset_feature(mac, id);
  496. return -1;
  497. }
  498. /*
  499. * Return:
  500. * Always return RTW_HALMAC_SUCCESS, HALMAC don't care the return value.
  501. */
  502. static u8 _halmac_event_indication(void *p, enum halmac_feature_id feature_id, enum halmac_cmd_process_status process_status, u8 *buf, u32 size)
  503. {
  504. struct dvobj_priv *d;
  505. PADAPTER adapter;
  506. PHAL_DATA_TYPE hal;
  507. struct halmac_indicator *tbl, *indicator;
  508. struct submit_ctx *sctx;
  509. u32 cpsz;
  510. u8 ret;
  511. d = (struct dvobj_priv *)p;
  512. adapter = dvobj_get_primary_adapter(d);
  513. hal = GET_HAL_DATA(adapter);
  514. tbl = d->hmpriv.indicator;
  515. /* Filter(Skip) middle status indication */
  516. ret = is_valid_id_status(feature_id, process_status);
  517. if (_FALSE == ret)
  518. goto exit;
  519. indicator = &tbl[feature_id];
  520. indicator->status = process_status;
  521. indicator->ret_size = size;
  522. if (!indicator->sctx) {
  523. RTW_WARN("%s: No feature id(%d, %s) waiting!!\n", __FUNCTION__, feature_id, RTW_HALMAC_FEATURE_NAME[feature_id]);
  524. goto exit;
  525. }
  526. sctx = indicator->sctx;
  527. if (HALMAC_CMD_PROCESS_ERROR == process_status) {
  528. RTW_ERR("%s: Something wrong id(%d, %s)!!\n", __FUNCTION__, feature_id, RTW_HALMAC_FEATURE_NAME[feature_id]);
  529. rtw_sctx_done_err(&sctx, RTW_SCTX_DONE_UNKNOWN);
  530. goto exit;
  531. }
  532. if (size > indicator->buf_size) {
  533. RTW_WARN("%s: id(%d, %s) buffer is not enough(%d<%d), data will be truncated!\n",
  534. __FUNCTION__, feature_id, RTW_HALMAC_FEATURE_NAME[feature_id], indicator->buf_size, size);
  535. cpsz = indicator->buf_size;
  536. } else {
  537. cpsz = size;
  538. }
  539. if (cpsz && indicator->buffer)
  540. _rtw_memcpy(indicator->buffer, buf, cpsz);
  541. rtw_sctx_done(&sctx);
  542. exit:
  543. return RTW_HALMAC_SUCCESS;
  544. }
  545. struct halmac_platform_api rtw_halmac_platform_api = {
  546. /* R/W register */
  547. #ifdef CONFIG_SDIO_HCI
  548. .SDIO_CMD52_READ = _halmac_sdio_cmd52_read,
  549. .SDIO_CMD53_READ_8 = _halmac_sdio_reg_read_8,
  550. .SDIO_CMD53_READ_16 = _halmac_sdio_reg_read_16,
  551. .SDIO_CMD53_READ_32 = _halmac_sdio_reg_read_32,
  552. .SDIO_CMD53_READ_N = _halmac_sdio_reg_read_n,
  553. .SDIO_CMD52_WRITE = _halmac_sdio_cmd52_write,
  554. .SDIO_CMD53_WRITE_8 = _halmac_sdio_reg_write_8,
  555. .SDIO_CMD53_WRITE_16 = _halmac_sdio_reg_write_16,
  556. .SDIO_CMD53_WRITE_32 = _halmac_sdio_reg_write_32,
  557. .SDIO_CMD52_CIA_READ = _halmac_sdio_read_cia,
  558. #endif /* CONFIG_SDIO_HCI */
  559. #if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI)
  560. .REG_READ_8 = _halmac_reg_read_8,
  561. .REG_READ_16 = _halmac_reg_read_16,
  562. .REG_READ_32 = _halmac_reg_read_32,
  563. .REG_WRITE_8 = _halmac_reg_write_8,
  564. .REG_WRITE_16 = _halmac_reg_write_16,
  565. .REG_WRITE_32 = _halmac_reg_write_32,
  566. #endif /* CONFIG_USB_HCI || CONFIG_PCI_HCI */
  567. /* Write data */
  568. #if 0
  569. /* impletement in HAL-IC level */
  570. .SEND_RSVD_PAGE = sdio_write_data_rsvd_page,
  571. .SEND_H2C_PKT = sdio_write_data_h2c,
  572. #endif
  573. /* Memory allocate */
  574. .RTL_FREE = _halmac_mfree,
  575. .RTL_MALLOC = _halmac_malloc,
  576. .RTL_MEMCPY = _halmac_memcpy,
  577. .RTL_MEMSET = _halmac_memset,
  578. /* Sleep */
  579. .RTL_DELAY_US = _halmac_udelay,
  580. /* Process Synchronization */
  581. .MUTEX_INIT = _halmac_mutex_init,
  582. .MUTEX_DEINIT = _halmac_mutex_deinit,
  583. .MUTEX_LOCK = _halmac_mutex_lock,
  584. .MUTEX_UNLOCK = _halmac_mutex_unlock,
  585. .MSG_PRINT = _halmac_msg_print,
  586. .BUFF_PRINT = _halmac_buff_print,
  587. .EVENT_INDICATION = _halmac_event_indication,
  588. };
  589. u8 rtw_halmac_read8(struct intf_hdl *pintfhdl, u32 addr)
  590. {
  591. struct halmac_adapter *mac;
  592. struct halmac_api *api;
  593. /* WARNING: pintf_dev should not be null! */
  594. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  595. api = HALMAC_GET_API(mac);
  596. return api->halmac_reg_read_8(mac, addr);
  597. }
  598. u16 rtw_halmac_read16(struct intf_hdl *pintfhdl, u32 addr)
  599. {
  600. struct halmac_adapter *mac;
  601. struct halmac_api *api;
  602. /* WARNING: pintf_dev should not be null! */
  603. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  604. api = HALMAC_GET_API(mac);
  605. return api->halmac_reg_read_16(mac, addr);
  606. }
  607. u32 rtw_halmac_read32(struct intf_hdl *pintfhdl, u32 addr)
  608. {
  609. struct halmac_adapter *mac;
  610. struct halmac_api *api;
  611. /* WARNING: pintf_dev should not be null! */
  612. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  613. api = HALMAC_GET_API(mac);
  614. return api->halmac_reg_read_32(mac, addr);
  615. }
  616. static void _read_register(struct dvobj_priv *d, u32 addr, u32 cnt, u8 *buf)
  617. {
  618. #if 1
  619. struct _ADAPTER *a;
  620. u32 i, n;
  621. u16 val16;
  622. u32 val32;
  623. a = dvobj_get_primary_adapter(d);
  624. i = addr & 0x3;
  625. /* Handle address not start from 4 bytes alignment case */
  626. if (i) {
  627. val32 = cpu_to_le32(rtw_read32(a, addr & ~0x3));
  628. n = 4 - i;
  629. _rtw_memcpy(buf, ((u8 *)&val32) + i, n);
  630. i = n;
  631. cnt -= n;
  632. }
  633. while (cnt) {
  634. if (cnt >= 4)
  635. n = 4;
  636. else if (cnt >= 2)
  637. n = 2;
  638. else
  639. n = 1;
  640. cnt -= n;
  641. switch (n) {
  642. case 1:
  643. buf[i] = rtw_read8(a, addr+i);
  644. i++;
  645. break;
  646. case 2:
  647. val16 = cpu_to_le16(rtw_read16(a, addr+i));
  648. _rtw_memcpy(&buf[i], &val16, 2);
  649. i += 2;
  650. break;
  651. case 4:
  652. val32 = cpu_to_le32(rtw_read32(a, addr+i));
  653. _rtw_memcpy(&buf[i], &val32, 4);
  654. i += 4;
  655. break;
  656. }
  657. }
  658. #else
  659. struct _ADAPTER *a;
  660. u32 i;
  661. a = dvobj_get_primary_adapter(d);
  662. for (i = 0; i < cnt; i++)
  663. buf[i] = rtw_read8(a, addr + i);
  664. #endif
  665. }
  666. #ifdef CONFIG_SDIO_HCI
  667. static int _sdio_read_local(struct dvobj_priv *d, u32 addr, u32 cnt, u8 *buf)
  668. {
  669. struct halmac_adapter *mac;
  670. struct halmac_api *api;
  671. enum halmac_ret_status status;
  672. if (buf == NULL)
  673. return -1;
  674. mac = dvobj_to_halmac(d);
  675. api = HALMAC_GET_API(mac);
  676. status = api->halmac_reg_sdio_cmd53_read_n(mac, addr, cnt, buf);
  677. if (status != HALMAC_RET_SUCCESS) {
  678. RTW_ERR("%s: addr=0x%08x cnt=%d err=%d\n",
  679. __FUNCTION__, addr, cnt, status);
  680. return -1;
  681. }
  682. return 0;
  683. }
  684. #endif /* CONFIG_SDIO_HCI */
  685. void rtw_halmac_read_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem)
  686. {
  687. struct dvobj_priv *d;
  688. if (pmem == NULL) {
  689. RTW_ERR("pmem is NULL\n");
  690. return;
  691. }
  692. d = pintfhdl->pintf_dev;
  693. #ifdef CONFIG_SDIO_HCI
  694. if (addr & 0xFFFF0000) {
  695. int err = 0;
  696. err = _sdio_read_local(d, addr, cnt, pmem);
  697. if (!err)
  698. return;
  699. }
  700. #endif /* CONFIG_SDIO_HCI */
  701. _read_register(d, addr, cnt, pmem);
  702. }
  703. #ifdef CONFIG_SDIO_INDIRECT_ACCESS
  704. u8 rtw_halmac_iread8(struct intf_hdl *pintfhdl, u32 addr)
  705. {
  706. struct halmac_adapter *mac;
  707. struct halmac_api *api;
  708. /* WARNING: pintf_dev should not be null! */
  709. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  710. api = HALMAC_GET_API(mac);
  711. /*return api->halmac_reg_read_indirect_8(mac, addr);*/
  712. return api->halmac_reg_read_8(mac, addr);
  713. }
  714. u16 rtw_halmac_iread16(struct intf_hdl *pintfhdl, u32 addr)
  715. {
  716. struct halmac_adapter *mac;
  717. struct halmac_api *api;
  718. u16 val16 = 0;
  719. /* WARNING: pintf_dev should not be null! */
  720. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  721. api = HALMAC_GET_API(mac);
  722. /*return api->halmac_reg_read_indirect_16(mac, addr);*/
  723. return api->halmac_reg_read_16(mac, addr);
  724. }
  725. u32 rtw_halmac_iread32(struct intf_hdl *pintfhdl, u32 addr)
  726. {
  727. struct halmac_adapter *mac;
  728. struct halmac_api *api;
  729. /* WARNING: pintf_dev should not be null! */
  730. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  731. api = HALMAC_GET_API(mac);
  732. return api->halmac_reg_read_indirect_32(mac, addr);
  733. }
  734. #endif /* CONFIG_SDIO_INDIRECT_ACCESS */
  735. int rtw_halmac_write8(struct intf_hdl *pintfhdl, u32 addr, u8 value)
  736. {
  737. struct halmac_adapter *mac;
  738. struct halmac_api *api;
  739. enum halmac_ret_status status;
  740. /* WARNING: pintf_dev should not be null! */
  741. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  742. api = HALMAC_GET_API(mac);
  743. status = api->halmac_reg_write_8(mac, addr, value);
  744. if (status == HALMAC_RET_SUCCESS)
  745. return 0;
  746. return -1;
  747. }
  748. int rtw_halmac_write16(struct intf_hdl *pintfhdl, u32 addr, u16 value)
  749. {
  750. struct halmac_adapter *mac;
  751. struct halmac_api *api;
  752. enum halmac_ret_status status;
  753. /* WARNING: pintf_dev should not be null! */
  754. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  755. api = HALMAC_GET_API(mac);
  756. status = api->halmac_reg_write_16(mac, addr, value);
  757. if (status == HALMAC_RET_SUCCESS)
  758. return 0;
  759. return -1;
  760. }
  761. int rtw_halmac_write32(struct intf_hdl *pintfhdl, u32 addr, u32 value)
  762. {
  763. struct halmac_adapter *mac;
  764. struct halmac_api *api;
  765. enum halmac_ret_status status;
  766. /* WARNING: pintf_dev should not be null! */
  767. mac = dvobj_to_halmac(pintfhdl->pintf_dev);
  768. api = HALMAC_GET_API(mac);
  769. status = api->halmac_reg_write_32(mac, addr, value);
  770. if (status == HALMAC_RET_SUCCESS)
  771. return 0;
  772. return -1;
  773. }
  774. static int init_write_rsvd_page_size(struct dvobj_priv *d)
  775. {
  776. struct halmac_adapter *mac;
  777. struct halmac_api *api;
  778. u32 size = 0;
  779. struct halmac_ofld_func_info ofld_info;
  780. enum halmac_ret_status status;
  781. int err = 0;
  782. #ifdef CONFIG_USB_HCI
  783. /* for USB do not exceed MAX_CMDBUF_SZ */
  784. size = 0x1000;
  785. #elif defined(CONFIG_PCI_HCI)
  786. size = MAX_CMDBUF_SZ - TXDESC_OFFSET;
  787. #elif defined(CONFIG_SDIO_HCI)
  788. size = 0x7000; /* 28KB */
  789. #endif
  790. /* If size==0, use HALMAC default setting and don't call any function */
  791. if (!size)
  792. return 0;
  793. err = rtw_halmac_set_max_dl_fw_size(d, size);
  794. if (err) {
  795. RTW_ERR("%s: Fail to set max download fw size!\n", __FUNCTION__);
  796. return -1;
  797. }
  798. mac = dvobj_to_halmac(d);
  799. api = HALMAC_GET_API(mac);
  800. _rtw_memset(&ofld_info, 0, sizeof(ofld_info));
  801. ofld_info.halmac_malloc_max_sz = 0xFFFFFFFF;
  802. ofld_info.rsvd_pg_drv_buf_max_sz = size;
  803. status = api->halmac_ofld_func_cfg(mac, &ofld_info);
  804. if (status != HALMAC_RET_SUCCESS) {
  805. RTW_ERR("%s: Fail to config offload parameters!\n", __FUNCTION__);
  806. return -1;
  807. }
  808. return 0;
  809. }
  810. static int init_priv(struct halmacpriv *priv)
  811. {
  812. struct halmac_indicator *indicator;
  813. u32 count, size;
  814. if (priv->indicator)
  815. RTW_WARN("%s: HALMAC private data is not CLEAR!\n", __FUNCTION__);
  816. count = HALMAC_FEATURE_ALL + 1;
  817. size = sizeof(*indicator) * count;
  818. indicator = (struct halmac_indicator *)rtw_zmalloc(size);
  819. if (!indicator)
  820. return -1;
  821. priv->indicator = indicator;
  822. return 0;
  823. }
  824. static void deinit_priv(struct halmacpriv *priv)
  825. {
  826. struct halmac_indicator *indicator;
  827. indicator = priv->indicator;
  828. priv->indicator = NULL;
  829. if (indicator) {
  830. u32 count, size;
  831. count = HALMAC_FEATURE_ALL + 1;
  832. #ifdef CONFIG_RTW_DEBUG
  833. {
  834. struct submit_ctx *sctx;
  835. u32 i;
  836. for (i = 0; i < count; i++) {
  837. if (!indicator[i].sctx)
  838. continue;
  839. RTW_WARN("%s: %s id(%d) sctx still exist!!\n",
  840. __FUNCTION__, RTW_HALMAC_FEATURE_NAME[i], i);
  841. sctx = indicator[i].sctx;
  842. indicator[i].sctx = NULL;
  843. rtw_mfree((u8 *)sctx, sizeof(*sctx));
  844. }
  845. }
  846. #endif /* !CONFIG_RTW_DEBUG */
  847. size = sizeof(*indicator) * count;
  848. rtw_mfree((u8 *)indicator, size);
  849. }
  850. }
  851. #ifdef CONFIG_SDIO_HCI
  852. static enum halmac_sdio_spec_ver _sdio_ver_drv2halmac(struct dvobj_priv *d)
  853. {
  854. bool v3;
  855. enum halmac_sdio_spec_ver ver;
  856. v3 = rtw_is_sdio30(dvobj_get_primary_adapter(d));
  857. if (v3)
  858. ver = HALMAC_SDIO_SPEC_VER_3_00;
  859. else
  860. ver = HALMAC_SDIO_SPEC_VER_2_00;
  861. return ver;
  862. }
  863. #endif /* CONFIG_SDIO_HCI */
  864. void rtw_halmac_get_version(char *str, u32 len)
  865. {
  866. enum halmac_ret_status status;
  867. struct halmac_ver ver;
  868. status = halmac_get_version(&ver);
  869. if (status != HALMAC_RET_SUCCESS)
  870. return;
  871. rtw_sprintf(str, len, "V%d_%02d_%02d",
  872. ver.major_ver, ver.prototype_ver, ver.minor_ver);
  873. }
  874. int rtw_halmac_init_adapter(struct dvobj_priv *d, struct halmac_platform_api *pf_api)
  875. {
  876. struct halmac_adapter *halmac;
  877. struct halmac_api *api;
  878. enum halmac_interface intf;
  879. enum halmac_ret_status status;
  880. int err = 0;
  881. #ifdef CONFIG_SDIO_HCI
  882. struct halmac_sdio_hw_info info;
  883. #endif /* CONFIG_SDIO_HCI */
  884. halmac = dvobj_to_halmac(d);
  885. if (halmac) {
  886. RTW_WARN("%s: initialize already completed!\n", __FUNCTION__);
  887. goto error;
  888. }
  889. err = init_priv(&d->hmpriv);
  890. if (err)
  891. goto error;
  892. #ifdef CONFIG_SDIO_HCI
  893. intf = HALMAC_INTERFACE_SDIO;
  894. #elif defined(CONFIG_USB_HCI)
  895. intf = HALMAC_INTERFACE_USB;
  896. #elif defined(CONFIG_PCI_HCI)
  897. intf = HALMAC_INTERFACE_PCIE;
  898. #else
  899. #warning "INTERFACE(CONFIG_XXX_HCI) not be defined!!"
  900. intf = HALMAC_INTERFACE_UNDEFINE;
  901. #endif
  902. status = halmac_init_adapter(d, pf_api, intf, &halmac, &api);
  903. if (HALMAC_RET_SUCCESS != status) {
  904. RTW_ERR("%s: halmac_init_adapter fail!(status=%d)\n", __FUNCTION__, status);
  905. err = -1;
  906. if (halmac)
  907. goto deinit;
  908. goto free;
  909. }
  910. dvobj_set_halmac(d, halmac);
  911. status = api->halmac_interface_integration_tuning(halmac);
  912. if (status != HALMAC_RET_SUCCESS) {
  913. RTW_ERR("%s: halmac_interface_integration_tuning fail!(status=%d)\n", __FUNCTION__, status);
  914. err = -1;
  915. goto deinit;
  916. }
  917. status = api->halmac_phy_cfg(halmac, HALMAC_INTF_PHY_PLATFORM_ALL);
  918. if (status != HALMAC_RET_SUCCESS) {
  919. RTW_ERR("%s: halmac_phy_cfg fail!(status=%d)\n", __FUNCTION__, status);
  920. err = -1;
  921. goto deinit;
  922. }
  923. init_write_rsvd_page_size(d);
  924. #ifdef CONFIG_SDIO_HCI
  925. _rtw_memset(&info, 0, sizeof(info));
  926. info.spec_ver = _sdio_ver_drv2halmac(d);
  927. /* Convert clock speed unit to MHz from Hz */
  928. info.clock_speed = RTW_DIV_ROUND_UP(rtw_sdio_get_clock(d), 1000000);
  929. info.block_size = rtw_sdio_get_block_size(d);
  930. RTW_DBG("%s: SDIO ver=%u clock=%uMHz blk_size=%u bytes\n",
  931. __FUNCTION__, info.spec_ver+2, info.clock_speed,
  932. info.block_size);
  933. status = api->halmac_sdio_hw_info(halmac, &info);
  934. if (status != HALMAC_RET_SUCCESS) {
  935. RTW_ERR("%s: halmac_sdio_hw_info fail!(status=%d)\n",
  936. __FUNCTION__, status);
  937. err = -1;
  938. goto deinit;
  939. }
  940. #endif /* CONFIG_SDIO_HCI */
  941. return 0;
  942. deinit:
  943. status = halmac_deinit_adapter(halmac);
  944. dvobj_set_halmac(d, NULL);
  945. if (status != HALMAC_RET_SUCCESS)
  946. RTW_ERR("%s: halmac_deinit_adapter fail!(status=%d)\n",
  947. __FUNCTION__, status);
  948. free:
  949. deinit_priv(&d->hmpriv);
  950. error:
  951. return err;
  952. }
  953. int rtw_halmac_deinit_adapter(struct dvobj_priv *d)
  954. {
  955. struct halmac_adapter *halmac;
  956. enum halmac_ret_status status;
  957. int err = 0;
  958. halmac = dvobj_to_halmac(d);
  959. if (halmac) {
  960. status = halmac_deinit_adapter(halmac);
  961. dvobj_set_halmac(d, NULL);
  962. if (status != HALMAC_RET_SUCCESS)
  963. err = -1;
  964. }
  965. deinit_priv(&d->hmpriv);
  966. return err;
  967. }
  968. static inline enum halmac_portid _hw_port_drv2halmac(enum _hw_port hwport)
  969. {
  970. enum halmac_portid port = HALMAC_PORTID_NUM;
  971. switch (hwport) {
  972. case HW_PORT0:
  973. port = HALMAC_PORTID0;
  974. break;
  975. case HW_PORT1:
  976. port = HALMAC_PORTID1;
  977. break;
  978. case HW_PORT2:
  979. port = HALMAC_PORTID2;
  980. break;
  981. case HW_PORT3:
  982. port = HALMAC_PORTID3;
  983. break;
  984. case HW_PORT4:
  985. port = HALMAC_PORTID4;
  986. break;
  987. default:
  988. break;
  989. }
  990. return port;
  991. }
  992. static enum halmac_network_type_select _network_type_drv2halmac(u8 type)
  993. {
  994. enum halmac_network_type_select network = HALMAC_NETWORK_UNDEFINE;
  995. switch (type) {
  996. case _HW_STATE_NOLINK_:
  997. case _HW_STATE_MONITOR_:
  998. network = HALMAC_NETWORK_NO_LINK;
  999. break;
  1000. case _HW_STATE_ADHOC_:
  1001. network = HALMAC_NETWORK_ADHOC;
  1002. break;
  1003. case _HW_STATE_STATION_:
  1004. network = HALMAC_NETWORK_INFRASTRUCTURE;
  1005. break;
  1006. case _HW_STATE_AP_:
  1007. network = HALMAC_NETWORK_AP;
  1008. break;
  1009. }
  1010. return network;
  1011. }
  1012. static u8 _network_type_halmac2drv(enum halmac_network_type_select network)
  1013. {
  1014. u8 type = _HW_STATE_NOLINK_;
  1015. switch (network) {
  1016. case HALMAC_NETWORK_NO_LINK:
  1017. case HALMAC_NETWORK_UNDEFINE:
  1018. type = _HW_STATE_NOLINK_;
  1019. break;
  1020. case HALMAC_NETWORK_ADHOC:
  1021. type = _HW_STATE_ADHOC_;
  1022. break;
  1023. case HALMAC_NETWORK_INFRASTRUCTURE:
  1024. type = _HW_STATE_STATION_;
  1025. break;
  1026. case HALMAC_NETWORK_AP:
  1027. type = _HW_STATE_AP_;
  1028. break;
  1029. }
  1030. return type;
  1031. }
  1032. static void _beacon_ctrl_halmac2drv(struct halmac_bcn_ctrl *ctrl,
  1033. struct rtw_halmac_bcn_ctrl *drv_ctrl)
  1034. {
  1035. drv_ctrl->rx_bssid_fit = ctrl->dis_rx_bssid_fit ? 0 : 1;
  1036. drv_ctrl->txbcn_rpt = ctrl->en_txbcn_rpt ? 1 : 0;
  1037. drv_ctrl->tsf_update = ctrl->dis_tsf_udt ? 0 : 1;
  1038. drv_ctrl->enable_bcn = ctrl->en_bcn ? 1 : 0;
  1039. drv_ctrl->rxbcn_rpt = ctrl->en_rxbcn_rpt ? 1 : 0;
  1040. drv_ctrl->p2p_ctwin = ctrl->en_p2p_ctwin ? 1 : 0;
  1041. drv_ctrl->p2p_bcn_area = ctrl->en_p2p_bcn_area ? 1 : 0;
  1042. }
  1043. static void _beacon_ctrl_drv2halmac(struct rtw_halmac_bcn_ctrl *drv_ctrl,
  1044. struct halmac_bcn_ctrl *ctrl)
  1045. {
  1046. ctrl->dis_rx_bssid_fit = drv_ctrl->rx_bssid_fit ? 0 : 1;
  1047. ctrl->en_txbcn_rpt = drv_ctrl->txbcn_rpt ? 1 : 0;
  1048. ctrl->dis_tsf_udt = drv_ctrl->tsf_update ? 0 : 1;
  1049. ctrl->en_bcn = drv_ctrl->enable_bcn ? 1 : 0;
  1050. ctrl->en_rxbcn_rpt = drv_ctrl->rxbcn_rpt ? 1 : 0;
  1051. ctrl->en_p2p_ctwin = drv_ctrl->p2p_ctwin ? 1 : 0;
  1052. ctrl->en_p2p_bcn_area = drv_ctrl->p2p_bcn_area ? 1 : 0;
  1053. }
  1054. int rtw_halmac_get_hw_value(struct dvobj_priv *d, enum halmac_hw_id hw_id, void *pvalue)
  1055. {
  1056. struct halmac_adapter *mac;
  1057. struct halmac_api *api;
  1058. enum halmac_ret_status status;
  1059. mac = dvobj_to_halmac(d);
  1060. api = HALMAC_GET_API(mac);
  1061. status = api->halmac_get_hw_value(mac, hw_id, pvalue);
  1062. if (HALMAC_RET_SUCCESS != status)
  1063. return -1;
  1064. return 0;
  1065. }
  1066. /**
  1067. * rtw_halmac_get_tx_fifo_size() - TX FIFO size
  1068. * @d: struct dvobj_priv*
  1069. * @size: TX FIFO size, unit is byte.
  1070. *
  1071. * Get TX FIFO size(byte) from HALMAC.
  1072. *
  1073. * Rteurn 0 for OK, otherwise fail.
  1074. */
  1075. int rtw_halmac_get_tx_fifo_size(struct dvobj_priv *d, u32 *size)
  1076. {
  1077. struct halmac_adapter *halmac;
  1078. struct halmac_api *api;
  1079. enum halmac_ret_status status;
  1080. u32 val = 0;
  1081. halmac = dvobj_to_halmac(d);
  1082. api = HALMAC_GET_API(halmac);
  1083. status = api->halmac_get_hw_value(halmac, HALMAC_HW_TXFIFO_SIZE, &val);
  1084. if (status != HALMAC_RET_SUCCESS)
  1085. return -1;
  1086. *size = val;
  1087. return 0;
  1088. }
  1089. /**
  1090. * rtw_halmac_get_rx_fifo_size() - RX FIFO size
  1091. * @d: struct dvobj_priv*
  1092. * @size: RX FIFO size, unit is byte
  1093. *
  1094. * Get RX FIFO size(byte) from HALMAC.
  1095. *
  1096. * Rteurn 0 for OK, otherwise fail.
  1097. */
  1098. int rtw_halmac_get_rx_fifo_size(struct dvobj_priv *d, u32 *size)
  1099. {
  1100. struct halmac_adapter *halmac;
  1101. struct halmac_api *api;
  1102. enum halmac_ret_status status;
  1103. u32 val = 0;
  1104. halmac = dvobj_to_halmac(d);
  1105. api = HALMAC_GET_API(halmac);
  1106. status = api->halmac_get_hw_value(halmac, HALMAC_HW_RXFIFO_SIZE, &val);
  1107. if (status != HALMAC_RET_SUCCESS)
  1108. return -1;
  1109. *size = val;
  1110. return 0;
  1111. }
  1112. /**
  1113. * rtw_halmac_get_rsvd_drv_pg_bndy() - Reserve page boundary of driver
  1114. * @d: struct dvobj_priv*
  1115. * @size: Page size, unit is byte
  1116. *
  1117. * Get reserve page boundary of driver from HALMAC.
  1118. *
  1119. * Rteurn 0 for OK, otherwise fail.
  1120. */
  1121. int rtw_halmac_get_rsvd_drv_pg_bndy(struct dvobj_priv *d, u16 *bndy)
  1122. {
  1123. struct halmac_adapter *halmac;
  1124. struct halmac_api *api;
  1125. enum halmac_ret_status status;
  1126. u16 val = 0;
  1127. halmac = dvobj_to_halmac(d);
  1128. api = HALMAC_GET_API(halmac);
  1129. status = api->halmac_get_hw_value(halmac, HALMAC_HW_RSVD_PG_BNDY, &val);
  1130. if (status != HALMAC_RET_SUCCESS)
  1131. return -1;
  1132. *bndy = val;
  1133. return 0;
  1134. }
  1135. /**
  1136. * rtw_halmac_get_page_size() - Page size
  1137. * @d: struct dvobj_priv*
  1138. * @size: Page size, unit is byte
  1139. *
  1140. * Get TX/RX page size(byte) from HALMAC.
  1141. *
  1142. * Rteurn 0 for OK, otherwise fail.
  1143. */
  1144. int rtw_halmac_get_page_size(struct dvobj_priv *d, u32 *size)
  1145. {
  1146. struct halmac_adapter *halmac;
  1147. struct halmac_api *api;
  1148. enum halmac_ret_status status;
  1149. u32 val = 0;
  1150. halmac = dvobj_to_halmac(d);
  1151. api = HALMAC_GET_API(halmac);
  1152. status = api->halmac_get_hw_value(halmac, HALMAC_HW_PAGE_SIZE, &val);
  1153. if (status != HALMAC_RET_SUCCESS)
  1154. return -1;
  1155. *size = val;
  1156. return 0;
  1157. }
  1158. /**
  1159. * rtw_halmac_get_tx_agg_align_size() - TX aggregation align size
  1160. * @d: struct dvobj_priv*
  1161. * @size: TX aggregation align size, unit is byte
  1162. *
  1163. * Get TX aggregation align size(byte) from HALMAC.
  1164. *
  1165. * Rteurn 0 for OK, otherwise fail.
  1166. */
  1167. int rtw_halmac_get_tx_agg_align_size(struct dvobj_priv *d, u16 *size)
  1168. {
  1169. struct halmac_adapter *halmac;
  1170. struct halmac_api *api;
  1171. enum halmac_ret_status status;
  1172. u16 val = 0;
  1173. halmac = dvobj_to_halmac(d);
  1174. api = HALMAC_GET_API(halmac);
  1175. status = api->halmac_get_hw_value(halmac, HALMAC_HW_TX_AGG_ALIGN_SIZE, &val);
  1176. if (status != HALMAC_RET_SUCCESS)
  1177. return -1;
  1178. *size = val;
  1179. return 0;
  1180. }
  1181. /**
  1182. * rtw_halmac_get_rx_agg_align_size() - RX aggregation align size
  1183. * @d: struct dvobj_priv*
  1184. * @size: RX aggregation align size, unit is byte
  1185. *
  1186. * Get RX aggregation align size(byte) from HALMAC.
  1187. *
  1188. * Rteurn 0 for OK, otherwise fail.
  1189. */
  1190. int rtw_halmac_get_rx_agg_align_size(struct dvobj_priv *d, u8 *size)
  1191. {
  1192. struct halmac_adapter *halmac;
  1193. struct halmac_api *api;
  1194. enum halmac_ret_status status;
  1195. u8 val = 0;
  1196. halmac = dvobj_to_halmac(d);
  1197. api = HALMAC_GET_API(halmac);
  1198. status = api->halmac_get_hw_value(halmac, HALMAC_HW_RX_AGG_ALIGN_SIZE, &val);
  1199. if (status != HALMAC_RET_SUCCESS)
  1200. return -1;
  1201. *size = val;
  1202. return 0;
  1203. }
  1204. /*
  1205. * Description:
  1206. * Get RX driver info size. RX driver info is a small memory space between
  1207. * scriptor and RX payload.
  1208. *
  1209. * +-------------------------+
  1210. * | RX descriptor |
  1211. * | usually 24 bytes |
  1212. * +-------------------------+
  1213. * | RX driver info |
  1214. * | depends on driver cfg |
  1215. * +-------------------------+
  1216. * | RX paylad |
  1217. * | |
  1218. * +-------------------------+
  1219. *
  1220. * Parameter:
  1221. * d pointer to struct dvobj_priv of driver
  1222. * sz rx driver info size in bytes.
  1223. *
  1224. * Rteurn:
  1225. * 0 Success
  1226. * other Fail
  1227. */
  1228. int rtw_halmac_get_rx_drv_info_sz(struct dvobj_priv *d, u8 *sz)
  1229. {
  1230. enum halmac_ret_status status;
  1231. struct halmac_adapter *halmac = dvobj_to_halmac(d);
  1232. struct halmac_api *api = HALMAC_GET_API(halmac);
  1233. u8 dw = 0;
  1234. status = api->halmac_get_hw_value(halmac, HALMAC_HW_DRV_INFO_SIZE, &dw);
  1235. if (status != HALMAC_RET_SUCCESS)
  1236. return -1;
  1237. *sz = dw * 8;
  1238. return 0;
  1239. }
  1240. /**
  1241. * rtw_halmac_get_tx_desc_size() - TX descriptor size
  1242. * @d: struct dvobj_priv*
  1243. * @size: TX descriptor size, unit is byte.
  1244. *
  1245. * Get TX descriptor size(byte) from HALMAC.
  1246. *
  1247. * Rteurn 0 for OK, otherwise fail.
  1248. */
  1249. int rtw_halmac_get_tx_desc_size(struct dvobj_priv *d, u32 *size)
  1250. {
  1251. struct halmac_adapter *halmac;
  1252. struct halmac_api *api;
  1253. enum halmac_ret_status status;
  1254. u32 val = 0;
  1255. halmac = dvobj_to_halmac(d);
  1256. api = HALMAC_GET_API(halmac);
  1257. status = api->halmac_get_hw_value(halmac, HALMAC_HW_TX_DESC_SIZE, &val);
  1258. if (status != HALMAC_RET_SUCCESS)
  1259. return -1;
  1260. *size = val;
  1261. return 0;
  1262. }
  1263. /**
  1264. * rtw_halmac_get_rx_desc_size() - RX descriptor size
  1265. * @d: struct dvobj_priv*
  1266. * @size: RX descriptor size, unit is byte.
  1267. *
  1268. * Get RX descriptor size(byte) from HALMAC.
  1269. *
  1270. * Rteurn 0 for OK, otherwise fail.
  1271. */
  1272. int rtw_halmac_get_rx_desc_size(struct dvobj_priv *d, u32 *size)
  1273. {
  1274. struct halmac_adapter *halmac;
  1275. struct halmac_api *api;
  1276. enum halmac_ret_status status;
  1277. u32 val = 0;
  1278. halmac = dvobj_to_halmac(d);
  1279. api = HALMAC_GET_API(halmac);
  1280. status = api->halmac_get_hw_value(halmac, HALMAC_HW_RX_DESC_SIZE, &val);
  1281. if (status != HALMAC_RET_SUCCESS)
  1282. return -1;
  1283. *size = val;
  1284. return 0;
  1285. }
  1286. /**
  1287. * rtw_halmac_get_fw_max_size() - Firmware MAX size
  1288. * @d: struct dvobj_priv*
  1289. * @size: MAX Firmware size, unit is byte.
  1290. *
  1291. * Get Firmware MAX size(byte) from HALMAC.
  1292. *
  1293. * Rteurn 0 for OK, otherwise fail.
  1294. */
  1295. static int rtw_halmac_get_fw_max_size(struct dvobj_priv *d, u32 *size)
  1296. {
  1297. struct halmac_adapter *halmac;
  1298. struct halmac_api *api;
  1299. enum halmac_ret_status status;
  1300. u32 val = 0;
  1301. halmac = dvobj_to_halmac(d);
  1302. api = HALMAC_GET_API(halmac);
  1303. status = api->halmac_get_hw_value(halmac, HALMAC_HW_FW_MAX_SIZE, &val);
  1304. if (status != HALMAC_RET_SUCCESS)
  1305. return -1;
  1306. *size = val;
  1307. return 0;
  1308. }
  1309. /**
  1310. * rtw_halmac_get_ori_h2c_size() - Original H2C MAX size
  1311. * @d: struct dvobj_priv*
  1312. * @size: H2C MAX size, unit is byte.
  1313. *
  1314. * Get original H2C MAX size(byte) from HALMAC.
  1315. *
  1316. * Rteurn 0 for OK, otherwise fail.
  1317. */
  1318. int rtw_halmac_get_ori_h2c_size(struct dvobj_priv *d, u32 *size)
  1319. {
  1320. struct halmac_adapter *halmac;
  1321. struct halmac_api *api;
  1322. enum halmac_ret_status status;
  1323. u32 val = 0;
  1324. halmac = dvobj_to_halmac(d);
  1325. api = HALMAC_GET_API(halmac);
  1326. status = api->halmac_get_hw_value(halmac, HALMAC_HW_ORI_H2C_SIZE, &val);
  1327. if (status != HALMAC_RET_SUCCESS)
  1328. return -1;
  1329. *size = val;
  1330. return 0;
  1331. }
  1332. int rtw_halmac_get_oqt_size(struct dvobj_priv *d, u8 *size)
  1333. {
  1334. enum halmac_ret_status status;
  1335. struct halmac_adapter *halmac;
  1336. struct halmac_api *api;
  1337. u8 val;
  1338. if (!size)
  1339. return -1;
  1340. halmac = dvobj_to_halmac(d);
  1341. api = HALMAC_GET_API(halmac);
  1342. status = api->halmac_get_hw_value(halmac, HALMAC_HW_AC_OQT_SIZE, &val);
  1343. if (status != HALMAC_RET_SUCCESS)
  1344. return -1;
  1345. *size = val;
  1346. return 0;
  1347. }
  1348. int rtw_halmac_get_ac_queue_number(struct dvobj_priv *d, u8 *num)
  1349. {
  1350. enum halmac_ret_status status;
  1351. struct halmac_adapter *halmac;
  1352. struct halmac_api *api;
  1353. u8 val;
  1354. if (!num)
  1355. return -1;
  1356. halmac = dvobj_to_halmac(d);
  1357. api = HALMAC_GET_API(halmac);
  1358. status = api->halmac_get_hw_value(halmac, HALMAC_HW_AC_QUEUE_NUM, &val);
  1359. if (status != HALMAC_RET_SUCCESS)
  1360. return -1;
  1361. *num = val;
  1362. return 0;
  1363. }
  1364. /**
  1365. * rtw_halmac_get_mac_address() - Get MAC address of specific port
  1366. * @d: struct dvobj_priv*
  1367. * @hwport: port
  1368. * @addr: buffer for storing MAC address
  1369. *
  1370. * Get MAC address of specific port from HALMAC.
  1371. *
  1372. * Rteurn 0 for OK, otherwise fail.
  1373. */
  1374. int rtw_halmac_get_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)
  1375. {
  1376. struct halmac_adapter *halmac;
  1377. struct halmac_api *api;
  1378. enum halmac_portid port;
  1379. union halmac_wlan_addr hwa;
  1380. enum halmac_ret_status status;
  1381. int err = -1;
  1382. if (!addr)
  1383. goto out;
  1384. halmac = dvobj_to_halmac(d);
  1385. api = HALMAC_GET_API(halmac);
  1386. port = _hw_port_drv2halmac(hwport);
  1387. _rtw_memset(&hwa, 0, sizeof(hwa));
  1388. status = api->halmac_get_mac_addr(halmac, port, &hwa);
  1389. if (status != HALMAC_RET_SUCCESS)
  1390. goto out;
  1391. _rtw_memcpy(addr, hwa.addr, 6);
  1392. err = 0;
  1393. out:
  1394. return err;
  1395. }
  1396. /**
  1397. * rtw_halmac_get_network_type() - Get network type of specific port
  1398. * @d: struct dvobj_priv*
  1399. * @hwport: port
  1400. * @type: buffer to put network type (_HW_STATE_*)
  1401. *
  1402. * Get network type of specific port from HALMAC.
  1403. *
  1404. * Rteurn 0 for OK, otherwise fail.
  1405. */
  1406. int rtw_halmac_get_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 *type)
  1407. {
  1408. #if 0
  1409. struct halmac_adapter *halmac;
  1410. struct halmac_api *api;
  1411. enum halmac_portid port;
  1412. enum halmac_network_type_select network;
  1413. enum halmac_ret_status status;
  1414. int err = -1;
  1415. halmac = dvobj_to_halmac(d);
  1416. api = HALMAC_GET_API(halmac);
  1417. port = _hw_port_drv2halmac(hwport);
  1418. network = HALMAC_NETWORK_UNDEFINE;
  1419. status = api->halmac_get_net_type(halmac, port, &network);
  1420. if (status != HALMAC_RET_SUCCESS)
  1421. goto out;
  1422. *type = _network_type_halmac2drv(network);
  1423. err = 0;
  1424. out:
  1425. return err;
  1426. #else
  1427. struct _ADAPTER *a;
  1428. enum halmac_portid port;
  1429. enum halmac_network_type_select network;
  1430. u32 val;
  1431. int err = -1;
  1432. a = dvobj_get_primary_adapter(d);
  1433. port = _hw_port_drv2halmac(hwport);
  1434. network = HALMAC_NETWORK_UNDEFINE;
  1435. switch (port) {
  1436. case HALMAC_PORTID0:
  1437. val = rtw_read32(a, REG_CR);
  1438. network = BIT_GET_NETYPE0(val);
  1439. break;
  1440. case HALMAC_PORTID1:
  1441. val = rtw_read32(a, REG_CR);
  1442. network = BIT_GET_NETYPE1(val);
  1443. break;
  1444. case HALMAC_PORTID2:
  1445. val = rtw_read32(a, REG_CR_EXT);
  1446. network = BIT_GET_NETYPE2(val);
  1447. break;
  1448. case HALMAC_PORTID3:
  1449. val = rtw_read32(a, REG_CR_EXT);
  1450. network = BIT_GET_NETYPE3(val);
  1451. break;
  1452. case HALMAC_PORTID4:
  1453. val = rtw_read32(a, REG_CR_EXT);
  1454. network = BIT_GET_NETYPE4(val);
  1455. break;
  1456. default:
  1457. goto out;
  1458. }
  1459. *type = _network_type_halmac2drv(network);
  1460. err = 0;
  1461. out:
  1462. return err;
  1463. #endif
  1464. }
  1465. /**
  1466. * rtw_halmac_get_bcn_ctrl() - Get beacon control setting of specific port
  1467. * @d: struct dvobj_priv*
  1468. * @hwport: port
  1469. * @bcn_ctrl: setting of beacon control
  1470. *
  1471. * Get beacon control setting of specific port from HALMAC.
  1472. *
  1473. * Rteurn 0 for OK, otherwise fail.
  1474. */
  1475. int rtw_halmac_get_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport,
  1476. struct rtw_halmac_bcn_ctrl *bcn_ctrl)
  1477. {
  1478. struct halmac_adapter *halmac;
  1479. struct halmac_api *api;
  1480. enum halmac_portid port;
  1481. struct halmac_bcn_ctrl ctrl;
  1482. enum halmac_ret_status status;
  1483. int err = -1;
  1484. halmac = dvobj_to_halmac(d);
  1485. api = HALMAC_GET_API(halmac);
  1486. port = _hw_port_drv2halmac(hwport);
  1487. _rtw_memset(&ctrl, 0, sizeof(ctrl));
  1488. status = api->halmac_rw_bcn_ctrl(halmac, port, 0, &ctrl);
  1489. if (status != HALMAC_RET_SUCCESS)
  1490. goto out;
  1491. _beacon_ctrl_halmac2drv(&ctrl, bcn_ctrl);
  1492. err = 0;
  1493. out:
  1494. return err;
  1495. }
  1496. /*
  1497. * Note:
  1498. * When this function return, the register REG_RCR may be changed.
  1499. */
  1500. int rtw_halmac_config_rx_info(struct dvobj_priv *d, enum halmac_drv_info info)
  1501. {
  1502. struct halmac_adapter *halmac;
  1503. struct halmac_api *api;
  1504. enum halmac_ret_status status;
  1505. int err = -1;
  1506. halmac = dvobj_to_halmac(d);
  1507. api = HALMAC_GET_API(halmac);
  1508. status = api->halmac_cfg_drv_info(halmac, info);
  1509. if (status != HALMAC_RET_SUCCESS)
  1510. goto out;
  1511. err = 0;
  1512. out:
  1513. return err;
  1514. }
  1515. /**
  1516. * rtw_halmac_set_max_dl_fw_size() - Set the MAX download firmware size
  1517. * @d: struct dvobj_priv*
  1518. * @size: the max download firmware size in one I/O
  1519. *
  1520. * Set the max download firmware size in one I/O.
  1521. * Please also consider the max size of the callback function "SEND_RSVD_PAGE"
  1522. * could accept, because download firmware would call "SEND_RSVD_PAGE" to send
  1523. * firmware to IC.
  1524. *
  1525. * If the value of "size" is not even, it would be rounded down to nearest
  1526. * even, and 0 and 1 are both invalid value.
  1527. *
  1528. * Return 0 for setting OK, otherwise fail.
  1529. */
  1530. int rtw_halmac_set_max_dl_fw_size(struct dvobj_priv *d, u32 size)
  1531. {
  1532. struct halmac_adapter *mac;
  1533. struct halmac_api *api;
  1534. enum halmac_ret_status status;
  1535. if (!size || (size == 1))
  1536. return -1;
  1537. mac = dvobj_to_halmac(d);
  1538. if (!mac) {
  1539. RTW_ERR("%s: HALMAC is not ready!!\n", __FUNCTION__);
  1540. return -1;
  1541. }
  1542. api = HALMAC_GET_API(mac);
  1543. size &= ~1; /* round down to even */
  1544. status = api->halmac_cfg_max_dl_size(mac, size);
  1545. if (status != HALMAC_RET_SUCCESS) {
  1546. RTW_WARN("%s: Fail to cfg_max_dl_size(%d), err=%d!!\n",
  1547. __FUNCTION__, size, status);
  1548. return -1;
  1549. }
  1550. return 0;
  1551. }
  1552. /**
  1553. * rtw_halmac_set_mac_address() - Set mac address of specific port
  1554. * @d: struct dvobj_priv*
  1555. * @hwport: port
  1556. * @addr: mac address
  1557. *
  1558. * Set self mac address of specific port to HALMAC.
  1559. *
  1560. * Rteurn 0 for OK, otherwise fail.
  1561. */
  1562. int rtw_halmac_set_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)
  1563. {
  1564. struct halmac_adapter *halmac;
  1565. struct halmac_api *api;
  1566. enum halmac_portid port;
  1567. union halmac_wlan_addr hwa;
  1568. enum halmac_ret_status status;
  1569. int err = -1;
  1570. halmac = dvobj_to_halmac(d);
  1571. api = HALMAC_GET_API(halmac);
  1572. port = _hw_port_drv2halmac(hwport);
  1573. _rtw_memset(&hwa, 0, sizeof(hwa));
  1574. _rtw_memcpy(hwa.addr, addr, 6);
  1575. status = api->halmac_cfg_mac_addr(halmac, port, &hwa);
  1576. if (status != HALMAC_RET_SUCCESS)
  1577. goto out;
  1578. err = 0;
  1579. out:
  1580. return err;
  1581. }
  1582. /**
  1583. * rtw_halmac_set_bssid() - Set BSSID of specific port
  1584. * @d: struct dvobj_priv*
  1585. * @hwport: port
  1586. * @addr: BSSID, mac address of AP
  1587. *
  1588. * Set BSSID of specific port to HALMAC.
  1589. *
  1590. * Rteurn 0 for OK, otherwise fail.
  1591. */
  1592. int rtw_halmac_set_bssid(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)
  1593. {
  1594. struct halmac_adapter *halmac;
  1595. struct halmac_api *api;
  1596. enum halmac_portid port;
  1597. union halmac_wlan_addr hwa;
  1598. enum halmac_ret_status status;
  1599. int err = -1;
  1600. halmac = dvobj_to_halmac(d);
  1601. api = HALMAC_GET_API(halmac);
  1602. port = _hw_port_drv2halmac(hwport);
  1603. _rtw_memset(&hwa, 0, sizeof(hwa));
  1604. _rtw_memcpy(hwa.addr, addr, 6);
  1605. status = api->halmac_cfg_bssid(halmac, port, &hwa);
  1606. if (status != HALMAC_RET_SUCCESS)
  1607. goto out;
  1608. err = 0;
  1609. out:
  1610. return err;
  1611. }
  1612. /**
  1613. * rtw_halmac_set_tx_address() - Set transmitter address of specific port
  1614. * @d: struct dvobj_priv*
  1615. * @hwport: port
  1616. * @addr: transmitter address
  1617. *
  1618. * Set transmitter address of specific port to HALMAC.
  1619. *
  1620. * Rteurn 0 for OK, otherwise fail.
  1621. */
  1622. int rtw_halmac_set_tx_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)
  1623. {
  1624. struct halmac_adapter *halmac;
  1625. struct halmac_api *api;
  1626. enum halmac_portid port;
  1627. union halmac_wlan_addr hwa;
  1628. enum halmac_ret_status status;
  1629. int err = -1;
  1630. halmac = dvobj_to_halmac(d);
  1631. api = HALMAC_GET_API(halmac);
  1632. port = _hw_port_drv2halmac(hwport);
  1633. _rtw_memset(&hwa, 0, sizeof(hwa));
  1634. _rtw_memcpy(hwa.addr, addr, 6);
  1635. status = api->halmac_cfg_transmitter_addr(halmac, port, &hwa);
  1636. if (status != HALMAC_RET_SUCCESS)
  1637. goto out;
  1638. err = 0;
  1639. out:
  1640. return err;
  1641. }
  1642. /**
  1643. * rtw_halmac_set_network_type() - Set network type of specific port
  1644. * @d: struct dvobj_priv*
  1645. * @hwport: port
  1646. * @type: network type (_HW_STATE_*)
  1647. *
  1648. * Set network type of specific port to HALMAC.
  1649. *
  1650. * Rteurn 0 for OK, otherwise fail.
  1651. */
  1652. int rtw_halmac_set_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 type)
  1653. {
  1654. struct halmac_adapter *halmac;
  1655. struct halmac_api *api;
  1656. enum halmac_portid port;
  1657. enum halmac_network_type_select network;
  1658. enum halmac_ret_status status;
  1659. int err = -1;
  1660. halmac = dvobj_to_halmac(d);
  1661. api = HALMAC_GET_API(halmac);
  1662. port = _hw_port_drv2halmac(hwport);
  1663. network = _network_type_drv2halmac(type);
  1664. status = api->halmac_cfg_net_type(halmac, port, network);
  1665. if (status != HALMAC_RET_SUCCESS)
  1666. goto out;
  1667. err = 0;
  1668. out:
  1669. return err;
  1670. }
  1671. /**
  1672. * rtw_halmac_reset_tsf() - Reset TSF timer of specific port
  1673. * @d: struct dvobj_priv*
  1674. * @hwport: port
  1675. *
  1676. * Notice HALMAC to reset timing synchronization function(TSF) timer of
  1677. * specific port.
  1678. *
  1679. * Rteurn 0 for OK, otherwise fail.
  1680. */
  1681. int rtw_halmac_reset_tsf(struct dvobj_priv *d, enum _hw_port hwport)
  1682. {
  1683. struct halmac_adapter *halmac;
  1684. struct halmac_api *api;
  1685. enum halmac_portid port;
  1686. enum halmac_ret_status status;
  1687. int err = -1;
  1688. halmac = dvobj_to_halmac(d);
  1689. api = HALMAC_GET_API(halmac);
  1690. port = _hw_port_drv2halmac(hwport);
  1691. status = api->halmac_cfg_tsf_rst(halmac, port);
  1692. if (status != HALMAC_RET_SUCCESS)
  1693. goto out;
  1694. err = 0;
  1695. out:
  1696. return err;
  1697. }
  1698. /**
  1699. * rtw_halmac_set_bcn_interval() - Set beacon interval of each port
  1700. * @d: struct dvobj_priv*
  1701. * @hwport: port
  1702. * @space: beacon interval, unit is ms
  1703. *
  1704. * Set beacon interval of specific port to HALMAC.
  1705. *
  1706. * Rteurn 0 for OK, otherwise fail.
  1707. */
  1708. int rtw_halmac_set_bcn_interval(struct dvobj_priv *d, enum _hw_port hwport,
  1709. u32 interval)
  1710. {
  1711. struct halmac_adapter *halmac;
  1712. struct halmac_api *api;
  1713. enum halmac_portid port;
  1714. enum halmac_ret_status status;
  1715. int err = -1;
  1716. halmac = dvobj_to_halmac(d);
  1717. api = HALMAC_GET_API(halmac);
  1718. port = _hw_port_drv2halmac(hwport);
  1719. status = api->halmac_cfg_bcn_space(halmac, port, interval);
  1720. if (status != HALMAC_RET_SUCCESS)
  1721. goto out;
  1722. err = 0;
  1723. out:
  1724. return err;
  1725. }
  1726. /**
  1727. * rtw_halmac_set_bcn_ctrl() - Set beacon control setting of each port
  1728. * @d: struct dvobj_priv*
  1729. * @hwport: port
  1730. * @bcn_ctrl: setting of beacon control
  1731. *
  1732. * Set beacon control setting of specific port to HALMAC.
  1733. *
  1734. * Rteurn 0 for OK, otherwise fail.
  1735. */
  1736. int rtw_halmac_set_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport,
  1737. struct rtw_halmac_bcn_ctrl *bcn_ctrl)
  1738. {
  1739. struct halmac_adapter *halmac;
  1740. struct halmac_api *api;
  1741. enum halmac_portid port;
  1742. struct halmac_bcn_ctrl ctrl;
  1743. enum halmac_ret_status status;
  1744. int err = -1;
  1745. halmac = dvobj_to_halmac(d);
  1746. api = HALMAC_GET_API(halmac);
  1747. port = _hw_port_drv2halmac(hwport);
  1748. _rtw_memset(&ctrl, 0, sizeof(ctrl));
  1749. _beacon_ctrl_drv2halmac(bcn_ctrl, &ctrl);
  1750. status = api->halmac_rw_bcn_ctrl(halmac, port, 1, &ctrl);
  1751. if (status != HALMAC_RET_SUCCESS)
  1752. goto out;
  1753. err = 0;
  1754. out:
  1755. return err;
  1756. }
  1757. /**
  1758. * rtw_halmac_set_aid() - Set association identifier(AID) of specific port
  1759. * @d: struct dvobj_priv*
  1760. * @hwport: port
  1761. * @aid: Association identifier
  1762. *
  1763. * Set association identifier(AID) of specific port to HALMAC.
  1764. *
  1765. * Rteurn 0 for OK, otherwise fail.
  1766. */
  1767. int rtw_halmac_set_aid(struct dvobj_priv *d, enum _hw_port hwport, u16 aid)
  1768. {
  1769. struct halmac_adapter *halmac;
  1770. struct halmac_api *api;
  1771. enum halmac_portid port;
  1772. enum halmac_ret_status status;
  1773. int err = -1;
  1774. halmac = dvobj_to_halmac(d);
  1775. api = HALMAC_GET_API(halmac);
  1776. port = _hw_port_drv2halmac(hwport);
  1777. #if 0
  1778. status = api->halmac_cfg_aid(halmac, port, aid);
  1779. if (status != HALMAC_RET_SUCCESS)
  1780. goto out;
  1781. #else
  1782. {
  1783. struct _ADAPTER *a;
  1784. u32 addr;
  1785. u16 val;
  1786. a = dvobj_get_primary_adapter(d);
  1787. switch (port) {
  1788. case 0:
  1789. addr = REG_BCN_PSR_RPT;
  1790. val = rtw_read16(a, addr);
  1791. val = BIT_SET_PS_AID_0(val, aid);
  1792. rtw_write16(a, addr, val);
  1793. break;
  1794. case 1:
  1795. addr = REG_BCN_PSR_RPT1;
  1796. val = rtw_read16(a, addr);
  1797. val = BIT_SET_PS_AID_1(val, aid);
  1798. rtw_write16(a, addr, val);
  1799. break;
  1800. case 2:
  1801. addr = REG_BCN_PSR_RPT2;
  1802. val = rtw_read16(a, addr);
  1803. val = BIT_SET_PS_AID_2(val, aid);
  1804. rtw_write16(a, addr, val);
  1805. break;
  1806. case 3:
  1807. addr = REG_BCN_PSR_RPT3;
  1808. val = rtw_read16(a, addr);
  1809. val = BIT_SET_PS_AID_3(val, aid);
  1810. rtw_write16(a, addr, val);
  1811. break;
  1812. case 4:
  1813. addr = REG_BCN_PSR_RPT4;
  1814. val = rtw_read16(a, addr);
  1815. val = BIT_SET_PS_AID_4(val, aid);
  1816. rtw_write16(a, addr, val);
  1817. break;
  1818. default:
  1819. goto out;
  1820. }
  1821. }
  1822. #endif
  1823. err = 0;
  1824. out:
  1825. return err;
  1826. }
  1827. int rtw_halmac_set_bandwidth(struct dvobj_priv *d, u8 channel, u8 pri_ch_idx, u8 bw)
  1828. {
  1829. struct halmac_adapter *mac;
  1830. struct halmac_api *api;
  1831. enum halmac_ret_status status;
  1832. mac = dvobj_to_halmac(d);
  1833. api = HALMAC_GET_API(mac);
  1834. status = api->halmac_cfg_ch_bw(mac, channel, pri_ch_idx, bw);
  1835. if (HALMAC_RET_SUCCESS != status)
  1836. return -1;
  1837. return 0;
  1838. }
  1839. /**
  1840. * rtw_halmac_set_edca() - config edca parameter
  1841. * @d: struct dvobj_priv*
  1842. * @queue: XMIT_[VO/VI/BE/BK]_QUEUE
  1843. * @aifs: Arbitration inter-frame space(AIFS)
  1844. * @cw: Contention window(CW)
  1845. * @txop: MAX Transmit Opportunity(TXOP)
  1846. *
  1847. * Return: 0 if process OK, otherwise -1.
  1848. */
  1849. int rtw_halmac_set_edca(struct dvobj_priv *d, u8 queue, u8 aifs, u8 cw, u16 txop)
  1850. {
  1851. struct halmac_adapter *mac;
  1852. struct halmac_api *api;
  1853. enum halmac_acq_id ac;
  1854. struct halmac_edca_para edca;
  1855. enum halmac_ret_status status;
  1856. mac = dvobj_to_halmac(d);
  1857. api = HALMAC_GET_API(mac);
  1858. switch (queue) {
  1859. case XMIT_VO_QUEUE:
  1860. ac = HALMAC_ACQ_ID_VO;
  1861. break;
  1862. case XMIT_VI_QUEUE:
  1863. ac = HALMAC_ACQ_ID_VI;
  1864. break;
  1865. case XMIT_BE_QUEUE:
  1866. ac = HALMAC_ACQ_ID_BE;
  1867. break;
  1868. case XMIT_BK_QUEUE:
  1869. ac = HALMAC_ACQ_ID_BK;
  1870. break;
  1871. default:
  1872. return -1;
  1873. }
  1874. edca.aifs = aifs;
  1875. edca.cw = cw;
  1876. edca.txop_limit = txop;
  1877. status = api->halmac_cfg_edca_para(mac, ac, &edca);
  1878. if (status != HALMAC_RET_SUCCESS)
  1879. return -1;
  1880. return 0;
  1881. }
  1882. /**
  1883. * rtw_halmac_set_rts_full_bw() - Send RTS to all covered channels
  1884. * @d: struct dvobj_priv*
  1885. * @enable: _TRUE(enable), _FALSE(disable)
  1886. *
  1887. * Hradware will duplicate RTS packet to all channels which are covered in used
  1888. * bandwidth.
  1889. *
  1890. * Return 0 if process OK, otherwise -1.
  1891. */
  1892. int rtw_halmac_set_rts_full_bw(struct dvobj_priv *d, u8 enable)
  1893. {
  1894. struct halmac_adapter *mac;
  1895. struct halmac_api *api;
  1896. enum halmac_ret_status status;
  1897. u8 full;
  1898. mac = dvobj_to_halmac(d);
  1899. api = HALMAC_GET_API(mac);
  1900. full = (enable == _TRUE) ? 1 : 0;
  1901. status = api->halmac_set_hw_value(mac, HALMAC_HW_RTS_FULL_BW, &full);
  1902. if (HALMAC_RET_SUCCESS != status)
  1903. return -1;
  1904. return 0;
  1905. }
  1906. #ifdef RTW_HALMAC_DBG_POWER_SWITCH
  1907. static void _dump_mac_reg(struct dvobj_priv *d, u32 start, u32 end)
  1908. {
  1909. struct _ADAPTER *adapter;
  1910. int i, j = 1;
  1911. adapter = dvobj_get_primary_adapter(d);
  1912. for (i = start; i < end; i += 4) {
  1913. if (j % 4 == 1)
  1914. RTW_PRINT("0x%04x", i);
  1915. _RTW_PRINT(" 0x%08x ", rtw_read32(adapter, i));
  1916. if ((j++) % 4 == 0)
  1917. _RTW_PRINT("\n");
  1918. }
  1919. }
  1920. void dump_dbg_val(struct _ADAPTER *a, u32 reg)
  1921. {
  1922. u32 v32;
  1923. rtw_write8(a, 0x3A, reg);
  1924. v32 = rtw_read32(a, 0xC0);
  1925. RTW_PRINT("0x3A = %02x, 0xC0 = 0x%08x\n",reg, v32);
  1926. }
  1927. #ifdef CONFIG_PCI_HCI
  1928. static void _dump_pcie_cfg_space(struct dvobj_priv *d)
  1929. {
  1930. struct _ADAPTER *padapter = dvobj_get_primary_adapter(d);
  1931. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  1932. struct pci_dev *pdev = pdvobjpriv->ppcidev;
  1933. struct pci_dev *bridge_pdev = pdev->bus->self;
  1934. u32 tmp[4] = { 0 };
  1935. u32 i, j;
  1936. RTW_PRINT("\n***** PCI Device Configuration Space *****\n\n");
  1937. for(i = 0; i < 0x100; i += 0x10)
  1938. {
  1939. for (j = 0 ; j < 4 ; j++)
  1940. pci_read_config_dword(pdev, i + j * 4, tmp+j);
  1941. RTW_PRINT("%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1942. i, tmp[0] & 0xFF, (tmp[0] >> 8) & 0xFF, (tmp[0] >> 16) & 0xFF, (tmp[0] >> 24) & 0xFF,
  1943. tmp[1] & 0xFF, (tmp[1] >> 8) & 0xFF, (tmp[1] >> 16) & 0xFF, (tmp[1] >> 24) & 0xFF,
  1944. tmp[2] & 0xFF, (tmp[2] >> 8) & 0xFF, (tmp[2] >> 16) & 0xFF, (tmp[2] >> 24) & 0xFF,
  1945. tmp[3] & 0xFF, (tmp[3] >> 8) & 0xFF, (tmp[3] >> 16) & 0xFF, (tmp[3] >> 24) & 0xFF);
  1946. }
  1947. RTW_PRINT("\n***** PCI Host Device Configuration Space*****\n\n");
  1948. for(i = 0; i < 0x100; i += 0x10)
  1949. {
  1950. for (j = 0 ; j < 4 ; j++)
  1951. pci_read_config_dword(bridge_pdev, i + j * 4, tmp+j);
  1952. RTW_PRINT("%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1953. i, tmp[0] & 0xFF, (tmp[0] >> 8) & 0xFF, (tmp[0] >> 16) & 0xFF, (tmp[0] >> 24) & 0xFF,
  1954. tmp[1] & 0xFF, (tmp[1] >> 8) & 0xFF, (tmp[1] >> 16) & 0xFF, (tmp[1] >> 24) & 0xFF,
  1955. tmp[2] & 0xFF, (tmp[2] >> 8) & 0xFF, (tmp[2] >> 16) & 0xFF, (tmp[2] >> 24) & 0xFF,
  1956. tmp[3] & 0xFF, (tmp[3] >> 8) & 0xFF, (tmp[3] >> 16) & 0xFF, (tmp[3] >> 24) & 0xFF);
  1957. }
  1958. }
  1959. #endif
  1960. static void _dump_mac_reg_for_power_switch(struct dvobj_priv *d,
  1961. const char* caller, char* desc)
  1962. {
  1963. struct _ADAPTER *a;
  1964. u8 v8;
  1965. RTW_PRINT("%s: %s\n", caller, desc);
  1966. RTW_PRINT("======= MAC REG =======\n");
  1967. /* page 0/1 */
  1968. _dump_mac_reg(d, 0x0, 0x200);
  1969. _dump_mac_reg(d, 0x300, 0x400); /* also dump page 3 */
  1970. /* dump debug register */
  1971. a = dvobj_get_primary_adapter(d);
  1972. #ifdef CONFIG_PCI_HCI
  1973. _dump_pcie_cfg_space(d);
  1974. v8 = rtw_read8(a, 0xF6) | 0x01;
  1975. rtw_write8(a, 0xF6, v8);
  1976. RTW_PRINT("0xF6 = %02x\n", v8);
  1977. dump_dbg_val(a, 0x63);
  1978. dump_dbg_val(a, 0x64);
  1979. dump_dbg_val(a, 0x68);
  1980. dump_dbg_val(a, 0x69);
  1981. dump_dbg_val(a, 0x6a);
  1982. dump_dbg_val(a, 0x6b);
  1983. dump_dbg_val(a, 0x71);
  1984. dump_dbg_val(a, 0x72);
  1985. #endif
  1986. }
  1987. static enum halmac_ret_status _power_switch(struct halmac_adapter *halmac,
  1988. struct halmac_api *api,
  1989. enum halmac_mac_power pwr)
  1990. {
  1991. enum halmac_ret_status status;
  1992. char desc[80] = {0};
  1993. rtw_sprintf(desc, 80, "before calling power %s",
  1994. (pwr==HALMAC_MAC_POWER_ON)?"on":"off");
  1995. _dump_mac_reg_for_power_switch((struct dvobj_priv *)halmac->drv_adapter,
  1996. __FUNCTION__, desc);
  1997. status = api->halmac_mac_power_switch(halmac, pwr);
  1998. RTW_PRINT("%s: status=%d\n", __FUNCTION__, status);
  1999. rtw_sprintf(desc, 80, "after calling power %s",
  2000. (pwr==HALMAC_MAC_POWER_ON)?"on":"off");
  2001. _dump_mac_reg_for_power_switch((struct dvobj_priv *)halmac->drv_adapter,
  2002. __FUNCTION__, desc);
  2003. return status;
  2004. }
  2005. #else /* !RTW_HALMAC_DBG_POWER_SWITCH */
  2006. #define _power_switch(mac, api, pwr) (api)->halmac_mac_power_switch(mac, pwr)
  2007. #endif /* !RTW_HALMAC_DBG_POWER_SWITCH */
  2008. /*
  2009. * Description:
  2010. * Power on device hardware.
  2011. * [Notice!] If device's power state is on before,
  2012. * it would be power off first and turn on power again.
  2013. *
  2014. * Return:
  2015. * 0 power on success
  2016. * -1 power on fail
  2017. * -2 power state unchange
  2018. */
  2019. int rtw_halmac_poweron(struct dvobj_priv *d)
  2020. {
  2021. struct halmac_adapter *halmac;
  2022. struct halmac_api *api;
  2023. enum halmac_ret_status status;
  2024. int err = -1;
  2025. #if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B)
  2026. struct _ADAPTER *a;
  2027. u8 v8;
  2028. u32 addr;
  2029. a = dvobj_get_primary_adapter(d);
  2030. #endif
  2031. halmac = dvobj_to_halmac(d);
  2032. if (!halmac)
  2033. goto out;
  2034. api = HALMAC_GET_API(halmac);
  2035. status = api->halmac_pre_init_system_cfg(halmac);
  2036. if (status != HALMAC_RET_SUCCESS)
  2037. goto out;
  2038. #ifdef CONFIG_SDIO_HCI
  2039. status = api->halmac_sdio_cmd53_4byte(halmac, HALMAC_SDIO_CMD53_4BYTE_MODE_RW);
  2040. if (status != HALMAC_RET_SUCCESS)
  2041. goto out;
  2042. #endif /* CONFIG_SDIO_HCI */
  2043. #if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B)
  2044. addr = 0x3F3;
  2045. v8 = rtw_read8(a, addr);
  2046. RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);
  2047. /* are we in pcie debug mode? */
  2048. if (!(v8 & BIT(2))) {
  2049. RTW_PRINT("%s: Enable pcie debug mode\n", __FUNCTION__);
  2050. v8 |= BIT(2);
  2051. v8 = rtw_write8(a, addr, v8);
  2052. }
  2053. #endif
  2054. status = _power_switch(halmac, api, HALMAC_MAC_POWER_ON);
  2055. if (HALMAC_RET_PWR_UNCHANGE == status) {
  2056. #if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B)
  2057. addr = 0x3F3;
  2058. v8 = rtw_read8(a, addr);
  2059. RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);
  2060. /* are we in pcie debug mode? */
  2061. if (!(v8 & BIT(2))) {
  2062. RTW_PRINT("%s: Enable pcie debug mode\n", __FUNCTION__);
  2063. v8 |= BIT(2);
  2064. v8 = rtw_write8(a, addr, v8);
  2065. } else if (v8 & BIT(0)) {
  2066. /* DMA stuck */
  2067. addr = 0x1350;
  2068. v8 = rtw_read8(a, addr);
  2069. RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);
  2070. RTW_PRINT("%s: recover DMA stuck\n", __FUNCTION__);
  2071. v8 |= BIT(6);
  2072. v8 = rtw_write8(a, addr, v8);
  2073. RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);
  2074. }
  2075. #endif
  2076. /*
  2077. * Work around for warm reboot but device not power off,
  2078. * but it would also fall into this case when auto power on is enabled.
  2079. */
  2080. _power_switch(halmac, api, HALMAC_MAC_POWER_OFF);
  2081. status = _power_switch(halmac, api, HALMAC_MAC_POWER_ON);
  2082. RTW_WARN("%s: Power state abnormal, try to recover...%s\n",
  2083. __FUNCTION__, (HALMAC_RET_SUCCESS == status)?"OK":"FAIL!");
  2084. }
  2085. if (HALMAC_RET_SUCCESS != status) {
  2086. if (HALMAC_RET_PWR_UNCHANGE == status)
  2087. err = -2;
  2088. goto out;
  2089. }
  2090. status = api->halmac_init_system_cfg(halmac);
  2091. if (status != HALMAC_RET_SUCCESS)
  2092. goto out;
  2093. err = 0;
  2094. out:
  2095. return err;
  2096. }
  2097. /*
  2098. * Description:
  2099. * Power off device hardware.
  2100. *
  2101. * Return:
  2102. * 0 Power off success
  2103. * -1 Power off fail
  2104. */
  2105. int rtw_halmac_poweroff(struct dvobj_priv *d)
  2106. {
  2107. struct halmac_adapter *halmac;
  2108. struct halmac_api *api;
  2109. enum halmac_ret_status status;
  2110. int err = -1;
  2111. halmac = dvobj_to_halmac(d);
  2112. if (!halmac)
  2113. goto out;
  2114. api = HALMAC_GET_API(halmac);
  2115. status = _power_switch(halmac, api, HALMAC_MAC_POWER_OFF);
  2116. if ((HALMAC_RET_SUCCESS != status)
  2117. && (HALMAC_RET_PWR_UNCHANGE != status))
  2118. goto out;
  2119. err = 0;
  2120. out:
  2121. return err;
  2122. }
  2123. #ifdef CONFIG_SUPPORT_TRX_SHARED
  2124. static inline enum halmac_rx_fifo_expanding_mode _trx_share_mode_drv2halmac(u8 trx_share_mode)
  2125. {
  2126. if (0 == trx_share_mode)
  2127. return HALMAC_RX_FIFO_EXPANDING_MODE_DISABLE;
  2128. else if (1 == trx_share_mode)
  2129. return HALMAC_RX_FIFO_EXPANDING_MODE_1_BLOCK;
  2130. else if (2 == trx_share_mode)
  2131. return HALMAC_RX_FIFO_EXPANDING_MODE_2_BLOCK;
  2132. else if (3 == trx_share_mode)
  2133. return HALMAC_RX_FIFO_EXPANDING_MODE_3_BLOCK;
  2134. else
  2135. return HALMAC_RX_FIFO_EXPANDING_MODE_DISABLE;
  2136. }
  2137. static enum halmac_rx_fifo_expanding_mode _rtw_get_trx_share_mode(struct _ADAPTER *adapter)
  2138. {
  2139. struct registry_priv *registry_par = &adapter->registrypriv;
  2140. return _trx_share_mode_drv2halmac(registry_par->trx_share_mode);
  2141. }
  2142. void dump_trx_share_mode(void *sel, struct _ADAPTER *adapter)
  2143. {
  2144. struct registry_priv *registry_par = &adapter->registrypriv;
  2145. u8 mode = _trx_share_mode_drv2halmac(registry_par->trx_share_mode);
  2146. if (HALMAC_RX_FIFO_EXPANDING_MODE_1_BLOCK == mode)
  2147. RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_1");
  2148. else if (HALMAC_RX_FIFO_EXPANDING_MODE_2_BLOCK == mode)
  2149. RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_2");
  2150. else if (HALMAC_RX_FIFO_EXPANDING_MODE_3_BLOCK == mode)
  2151. RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_3");
  2152. else
  2153. RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "DISABLE");
  2154. }
  2155. #endif
  2156. static enum halmac_drv_rsvd_pg_num _rsvd_page_num_drv2halmac(u8 num)
  2157. {
  2158. if (num <= 8)
  2159. return HALMAC_RSVD_PG_NUM8;
  2160. if (num <= 16)
  2161. return HALMAC_RSVD_PG_NUM16;
  2162. if (num <= 24)
  2163. return HALMAC_RSVD_PG_NUM24;
  2164. if (num <= 32)
  2165. return HALMAC_RSVD_PG_NUM32;
  2166. if (num <= 64)
  2167. return HALMAC_RSVD_PG_NUM64;
  2168. if (num > 128)
  2169. RTW_WARN("%s: Fail to allocate RSVD page(%d)!!"
  2170. " The MAX RSVD page number is 128...\n",
  2171. __FUNCTION__, num);
  2172. return HALMAC_RSVD_PG_NUM128;
  2173. }
  2174. static u8 _rsvd_page_num_halmac2drv(enum halmac_drv_rsvd_pg_num rsvd_page_number)
  2175. {
  2176. u8 num = 0;
  2177. switch (rsvd_page_number) {
  2178. case HALMAC_RSVD_PG_NUM8:
  2179. num = 8;
  2180. break;
  2181. case HALMAC_RSVD_PG_NUM16:
  2182. num = 16;
  2183. break;
  2184. case HALMAC_RSVD_PG_NUM24:
  2185. num = 24;
  2186. break;
  2187. case HALMAC_RSVD_PG_NUM32:
  2188. num = 32;
  2189. break;
  2190. case HALMAC_RSVD_PG_NUM64:
  2191. num = 64;
  2192. break;
  2193. case HALMAC_RSVD_PG_NUM128:
  2194. num = 128;
  2195. break;
  2196. }
  2197. return num;
  2198. }
  2199. static enum halmac_trx_mode _choose_trx_mode(struct dvobj_priv *d)
  2200. {
  2201. PADAPTER p;
  2202. p = dvobj_get_primary_adapter(d);
  2203. if (p->registrypriv.wifi_spec)
  2204. return HALMAC_TRX_MODE_WMM;
  2205. #ifdef CONFIG_SUPPORT_TRX_SHARED
  2206. if (_rtw_get_trx_share_mode(p))
  2207. return HALMAC_TRX_MODE_TRXSHARE;
  2208. #endif
  2209. return HALMAC_TRX_MODE_NORMAL;
  2210. }
  2211. static inline enum halmac_rf_type _rf_type_drv2halmac(enum rf_type rf_drv)
  2212. {
  2213. enum halmac_rf_type rf_mac;
  2214. switch (rf_drv) {
  2215. case RF_1T1R:
  2216. rf_mac = HALMAC_RF_1T1R;
  2217. break;
  2218. case RF_1T2R:
  2219. rf_mac = HALMAC_RF_1T2R;
  2220. break;
  2221. case RF_2T2R:
  2222. rf_mac = HALMAC_RF_2T2R;
  2223. break;
  2224. case RF_2T3R:
  2225. rf_mac = HALMAC_RF_2T3R;
  2226. break;
  2227. case RF_2T4R:
  2228. rf_mac = HALMAC_RF_2T4R;
  2229. break;
  2230. case RF_3T3R:
  2231. rf_mac = HALMAC_RF_3T3R;
  2232. break;
  2233. case RF_3T4R:
  2234. rf_mac = HALMAC_RF_3T4R;
  2235. break;
  2236. case RF_4T4R:
  2237. rf_mac = HALMAC_RF_4T4R;
  2238. break;
  2239. default:
  2240. rf_mac = HALMAC_RF_MAX_TYPE;
  2241. RTW_ERR("%s: Invalid RF type(0x%x)!\n", __FUNCTION__, rf_drv);
  2242. break;
  2243. }
  2244. return rf_mac;
  2245. }
  2246. static inline enum rf_type _rf_type_halmac2drv(enum halmac_rf_type rf_mac)
  2247. {
  2248. enum rf_type rf_drv;
  2249. switch (rf_mac) {
  2250. case HALMAC_RF_1T2R:
  2251. rf_drv = RF_1T2R;
  2252. break;
  2253. case HALMAC_RF_2T4R:
  2254. rf_drv = RF_2T4R;
  2255. break;
  2256. case HALMAC_RF_2T2R:
  2257. case HALMAC_RF_2T2R_GREEN:
  2258. rf_drv = RF_2T2R;
  2259. break;
  2260. case HALMAC_RF_2T3R:
  2261. rf_drv = RF_2T3R;
  2262. break;
  2263. case HALMAC_RF_1T1R:
  2264. rf_drv = RF_1T1R;
  2265. break;
  2266. case HALMAC_RF_3T3R:
  2267. rf_drv = RF_3T3R;
  2268. break;
  2269. case HALMAC_RF_3T4R:
  2270. rf_drv = RF_3T4R;
  2271. break;
  2272. case HALMAC_RF_4T4R:
  2273. rf_drv = RF_4T4R;
  2274. break;
  2275. default:
  2276. rf_drv = RF_TYPE_MAX;
  2277. RTW_ERR("%s: Invalid RF type(0x%x)!\n", __FUNCTION__, rf_mac);
  2278. break;
  2279. }
  2280. return rf_drv;
  2281. }
  2282. static enum odm_cut_version _cut_version_drv2phydm(
  2283. enum tag_HAL_Cut_Version_Definition cut_drv)
  2284. {
  2285. enum odm_cut_version cut_phydm = ODM_CUT_A;
  2286. u32 diff;
  2287. if (cut_drv > K_CUT_VERSION)
  2288. RTW_WARN("%s: unknown cut_ver=%d !!\n", __FUNCTION__, cut_drv);
  2289. diff = cut_drv - A_CUT_VERSION;
  2290. cut_phydm += diff;
  2291. return cut_phydm;
  2292. }
  2293. static int _send_general_info_by_reg(struct dvobj_priv *d,
  2294. struct halmac_general_info *info)
  2295. {
  2296. struct _ADAPTER *a;
  2297. struct hal_com_data *hal;
  2298. enum tag_HAL_Cut_Version_Definition cut_drv;
  2299. enum rf_type rftype;
  2300. enum odm_cut_version cut_phydm;
  2301. u8 h2c[RTW_HALMAC_H2C_MAX_SIZE] = {0};
  2302. a = dvobj_get_primary_adapter(d);
  2303. hal = GET_HAL_DATA(a);
  2304. rftype = _rf_type_halmac2drv(info->rf_type);
  2305. cut_drv = GET_CVID_CUT_VERSION(hal->version_id);
  2306. cut_phydm = _cut_version_drv2phydm(cut_drv);
  2307. #define CLASS_GENERAL_INFO_REG 0x02
  2308. #define CMD_ID_GENERAL_INFO_REG 0x0C
  2309. #define GENERAL_INFO_REG_SET_CMD_ID(buf, v) SET_BITS_TO_LE_4BYTE(buf, 0, 5, v)
  2310. #define GENERAL_INFO_REG_SET_CLASS(buf, v) SET_BITS_TO_LE_4BYTE(buf, 5, 3, v)
  2311. #define GENERAL_INFO_REG_SET_RFE_TYPE(buf, v) SET_BITS_TO_LE_4BYTE(buf, 8, 8, v)
  2312. #define GENERAL_INFO_REG_SET_RF_TYPE(buf, v) SET_BITS_TO_LE_4BYTE(buf, 16, 8, v)
  2313. #define GENERAL_INFO_REG_SET_CUT_VERSION(buf, v) SET_BITS_TO_LE_4BYTE(buf, 24, 8, v)
  2314. #define GENERAL_INFO_REG_SET_RX_ANT_STATUS(buf, v) SET_BITS_TO_LE_1BYTE(buf+4, 0, 4, v)
  2315. #define GENERAL_INFO_REG_SET_TX_ANT_STATUS(buf, v) SET_BITS_TO_LE_1BYTE(buf+4, 4, 4, v)
  2316. GENERAL_INFO_REG_SET_CMD_ID(h2c, CMD_ID_GENERAL_INFO_REG);
  2317. GENERAL_INFO_REG_SET_CLASS(h2c, CLASS_GENERAL_INFO_REG);
  2318. GENERAL_INFO_REG_SET_RFE_TYPE(h2c, info->rfe_type);
  2319. GENERAL_INFO_REG_SET_RF_TYPE(h2c, rftype);
  2320. GENERAL_INFO_REG_SET_CUT_VERSION(h2c, cut_phydm);
  2321. GENERAL_INFO_REG_SET_RX_ANT_STATUS(h2c, info->rx_ant_status);
  2322. GENERAL_INFO_REG_SET_TX_ANT_STATUS(h2c, info->tx_ant_status);
  2323. return rtw_halmac_send_h2c(d, h2c);
  2324. }
  2325. static int _send_general_info(struct dvobj_priv *d)
  2326. {
  2327. struct _ADAPTER *adapter;
  2328. struct hal_com_data *hal;
  2329. struct halmac_adapter *halmac;
  2330. struct halmac_api *api;
  2331. struct halmac_general_info info;
  2332. enum halmac_ret_status status;
  2333. enum rf_type rf = RF_1T1R;
  2334. enum bb_path txpath = BB_PATH_A;
  2335. enum bb_path rxpath = BB_PATH_A;
  2336. int err;
  2337. adapter = dvobj_get_primary_adapter(d);
  2338. hal = GET_HAL_DATA(adapter);
  2339. halmac = dvobj_to_halmac(d);
  2340. if (!halmac)
  2341. return -1;
  2342. api = HALMAC_GET_API(halmac);
  2343. _rtw_memset(&info, 0, sizeof(info));
  2344. info.rfe_type = (u8)hal->rfe_type;
  2345. rtw_hal_get_rf_path(d, &rf, &txpath, &rxpath);
  2346. info.rf_type = _rf_type_drv2halmac(rf);
  2347. info.tx_ant_status = (u8)txpath;
  2348. info.rx_ant_status = (u8)rxpath;
  2349. status = api->halmac_send_general_info(halmac, &info);
  2350. switch (status) {
  2351. case HALMAC_RET_SUCCESS:
  2352. break;
  2353. case HALMAC_RET_NO_DLFW:
  2354. RTW_WARN("%s: halmac_send_general_info() fail because fw not dl!\n",
  2355. __FUNCTION__);
  2356. __attribute__ ((fallthrough));
  2357. default:
  2358. return -1;
  2359. }
  2360. err = _send_general_info_by_reg(d, &info);
  2361. if (err) {
  2362. RTW_ERR("%s: Fail to send general info by register!\n",
  2363. __FUNCTION__);
  2364. return -1;
  2365. }
  2366. return 0;
  2367. }
  2368. static int _cfg_drv_rsvd_pg_num(struct dvobj_priv *d)
  2369. {
  2370. struct _ADAPTER *a;
  2371. struct hal_com_data *hal;
  2372. struct halmac_adapter *halmac;
  2373. struct halmac_api *api;
  2374. enum halmac_drv_rsvd_pg_num rsvd_page_number;
  2375. enum halmac_ret_status status;
  2376. u8 drv_rsvd_num;
  2377. a = dvobj_get_primary_adapter(d);
  2378. hal = GET_HAL_DATA(a);
  2379. halmac = dvobj_to_halmac(d);
  2380. api = HALMAC_GET_API(halmac);
  2381. drv_rsvd_num = rtw_hal_get_rsvd_page_num(a);
  2382. rsvd_page_number = _rsvd_page_num_drv2halmac(drv_rsvd_num);
  2383. status = api->halmac_cfg_drv_rsvd_pg_num(halmac, rsvd_page_number);
  2384. if (status != HALMAC_RET_SUCCESS)
  2385. return -1;
  2386. hal->drv_rsvd_page_number = _rsvd_page_num_halmac2drv(rsvd_page_number);
  2387. if (drv_rsvd_num != hal->drv_rsvd_page_number)
  2388. RTW_INFO("%s: request %d pages, but allocate %d pages\n",
  2389. __FUNCTION__, drv_rsvd_num, hal->drv_rsvd_page_number);
  2390. return 0;
  2391. }
  2392. static void _debug_dlfw_fail(struct dvobj_priv *d)
  2393. {
  2394. struct _ADAPTER *a;
  2395. u32 addr;
  2396. u32 v32, i, n;
  2397. u8 data[0x100] = {0};
  2398. a = dvobj_get_primary_adapter(d);
  2399. /* read 0x80[15:0], 0x10F8[31:0] once */
  2400. addr = 0x80;
  2401. v32 = rtw_read16(a, addr);
  2402. RTW_PRINT("%s: 0x%X = 0x%04x\n", __FUNCTION__, addr, v32);
  2403. addr = 0x10F8;
  2404. v32 = rtw_read32(a, addr);
  2405. RTW_PRINT("%s: 0x%X = 0x%08x\n", __FUNCTION__, addr, v32);
  2406. /* read 0x10FC[31:0], 5 times */
  2407. addr = 0x10FC;
  2408. n = 5;
  2409. for (i = 0; i < n; i++) {
  2410. v32 = rtw_read32(a, addr);
  2411. RTW_PRINT("%s: 0x%X = 0x%08x (%u/%u)\n",
  2412. __FUNCTION__, addr, v32, i, n);
  2413. }
  2414. /*
  2415. * write 0x3A[7:0]=0x28 and 0xF6[7:0]=0x01
  2416. * and then read 0xC0[31:0] 5 times
  2417. */
  2418. addr = 0x3A;
  2419. v32 = 0x28;
  2420. rtw_write8(a, addr, (u8)v32);
  2421. v32 = rtw_read8(a, addr);
  2422. RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v32);
  2423. addr = 0xF6;
  2424. v32 = 0x1;
  2425. rtw_write8(a, addr, (u8)v32);
  2426. v32 = rtw_read8(a, addr);
  2427. RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v32);
  2428. addr = 0xC0;
  2429. n = 5;
  2430. for (i = 0; i < n; i++) {
  2431. v32 = rtw_read32(a, addr);
  2432. RTW_PRINT("%s: 0x%X = 0x%08x (%u/%u)\n",
  2433. __FUNCTION__, addr, v32, i, n);
  2434. }
  2435. /* 0x00~0xFF, 0x1000~0x10FF */
  2436. addr = 0;
  2437. n = 0x100;
  2438. for (i = 0; i < n; i+=4)
  2439. *(u32*)&data[i] = cpu_to_le32(rtw_read32(a, addr+i));
  2440. for (i = 0; i < n; i++) {
  2441. if (i % 16 == 0)
  2442. RTW_PRINT("0x%04x\t", addr+i);
  2443. _RTW_PRINT("0x%02x", data[i]);
  2444. if (i % 16 == 15)
  2445. _RTW_PRINT("\n");
  2446. else
  2447. _RTW_PRINT(" ");
  2448. }
  2449. addr = 0x1000;
  2450. n = 0x100;
  2451. for (i = 0; i < n; i+=4)
  2452. *(u32*)&data[i] = cpu_to_le32(rtw_read32(a, addr+i));
  2453. for (i = 0; i < n; i++) {
  2454. if (i % 16 == 0)
  2455. RTW_PRINT("0x%04x\t", addr+i);
  2456. _RTW_PRINT("0x%02x", data[i]);
  2457. if (i % 16 == 15)
  2458. _RTW_PRINT("\n");
  2459. else
  2460. _RTW_PRINT(" ");
  2461. }
  2462. /* read 0x80 after 10 secs */
  2463. rtw_msleep_os(10000);
  2464. addr = 0x80;
  2465. v32 = rtw_read16(a, addr);
  2466. RTW_PRINT("%s: 0x%X = 0x%04x (after 10 secs)\n",
  2467. __FUNCTION__, addr, v32);
  2468. }
  2469. static enum halmac_ret_status _enter_cpu_sleep_mode(struct dvobj_priv *d)
  2470. {
  2471. struct hal_com_data *hal;
  2472. struct halmac_adapter *mac;
  2473. struct halmac_api *api;
  2474. hal = GET_HAL_DATA(dvobj_get_primary_adapter(d));
  2475. mac = dvobj_to_halmac(d);
  2476. api = HALMAC_GET_API(mac);
  2477. #ifdef CONFIG_RTL8822B
  2478. /* Support after firmware version 21 */
  2479. if (hal->firmware_version < 21)
  2480. return HALMAC_RET_NOT_SUPPORT;
  2481. #elif defined(CONFIG_RTL8821C)
  2482. /* Support after firmware version 13.6 or 16 */
  2483. if (hal->firmware_version == 13) {
  2484. if (hal->firmware_sub_version < 6)
  2485. return HALMAC_RET_NOT_SUPPORT;
  2486. } else if (hal->firmware_version < 16) {
  2487. return HALMAC_RET_NOT_SUPPORT;
  2488. }
  2489. #endif
  2490. return api->halmac_enter_cpu_sleep_mode(mac);
  2491. }
  2492. /*
  2493. * _cpu_sleep() - Let IC CPU enter sleep mode
  2494. * @d: struct dvobj_priv*
  2495. * @timeout: time limit of wait, unit is ms
  2496. * 0 for no limit
  2497. *
  2498. * Rteurn 0 for CPU in sleep mode, otherwise fail to enter sleep mode.
  2499. * Error codes definition are as follow:
  2500. * -1 HALMAC enter sleep return fail
  2501. * -2 HALMAC get CPU mode return fail
  2502. * -110 timeout
  2503. */
  2504. static int _cpu_sleep(struct dvobj_priv *d, u32 timeout)
  2505. {
  2506. struct halmac_adapter *mac;
  2507. struct halmac_api *api;
  2508. enum halmac_ret_status status;
  2509. enum halmac_wlcpu_mode mode = HALMAC_WLCPU_UNDEFINE;
  2510. systime start_t;
  2511. s32 period = 0;
  2512. u32 cnt = 0;
  2513. int err = 0;
  2514. mac = dvobj_to_halmac(d);
  2515. api = HALMAC_GET_API(mac);
  2516. start_t = rtw_get_current_time();
  2517. status = _enter_cpu_sleep_mode(d);
  2518. if (status != HALMAC_RET_SUCCESS) {
  2519. if (status != HALMAC_RET_NOT_SUPPORT)
  2520. err = -1;
  2521. goto exit;
  2522. }
  2523. do {
  2524. cnt++;
  2525. mode = HALMAC_WLCPU_UNDEFINE;
  2526. status = api->halmac_get_cpu_mode(mac, &mode);
  2527. period = rtw_get_passing_time_ms(start_t);
  2528. if (status != HALMAC_RET_SUCCESS) {
  2529. err = -2;
  2530. break;
  2531. }
  2532. if (mode == HALMAC_WLCPU_SLEEP)
  2533. break;
  2534. if (period > timeout) {
  2535. err = -110;
  2536. break;
  2537. }
  2538. rtw_msleep_os(1);
  2539. } while (1);
  2540. exit:
  2541. if (err)
  2542. RTW_ERR("%s: Fail to enter sleep mode! (%d, %d)\n",
  2543. __FUNCTION__, status, mode);
  2544. RTW_INFO("%s: Cost %dms to polling %u times. (err=%d)\n",
  2545. __FUNCTION__, period, cnt, err);
  2546. return err;
  2547. }
  2548. static void _init_trx_cfg_drv(struct dvobj_priv *d)
  2549. {
  2550. #ifdef CONFIG_PCI_HCI
  2551. rtw_hal_irp_reset(dvobj_get_primary_adapter(d));
  2552. #endif
  2553. }
  2554. /*
  2555. * Description:
  2556. * Downlaod Firmware Flow
  2557. *
  2558. * Parameters:
  2559. * d pointer of struct dvobj_priv
  2560. * fw firmware array
  2561. * fwsize firmware size
  2562. * re_dl re-download firmware or not
  2563. * 0: run in init hal flow, not re-download
  2564. * 1: it is a stand alone operation, not in init hal flow
  2565. *
  2566. * Return:
  2567. * 0 Success
  2568. * others Fail
  2569. */
  2570. static int download_fw(struct dvobj_priv *d, u8 *fw, u32 fwsize, u8 re_dl)
  2571. {
  2572. PHAL_DATA_TYPE hal;
  2573. struct halmac_adapter *mac;
  2574. struct halmac_api *api;
  2575. struct halmac_fw_version fw_vesion;
  2576. enum halmac_ret_status status;
  2577. int err = 0;
  2578. hal = GET_HAL_DATA(dvobj_get_primary_adapter(d));
  2579. mac = dvobj_to_halmac(d);
  2580. api = HALMAC_GET_API(mac);
  2581. if ((!fw) || (!fwsize))
  2582. return -1;
  2583. /* 1. Driver Stop Tx */
  2584. /* ToDo */
  2585. /* 2. Driver Check Tx FIFO is empty */
  2586. err = rtw_halmac_txfifo_wait_empty(d, 2000); /* wait 2s */
  2587. if (err) {
  2588. err = -1;
  2589. goto resume_tx;
  2590. }
  2591. /* 3. Config MAX download size */
  2592. /*
  2593. * Already done in rtw_halmac_init_adapter() or
  2594. * somewhere calling rtw_halmac_set_max_dl_fw_size().
  2595. */
  2596. if (re_dl) {
  2597. /* 4. Enter IC CPU sleep mode */
  2598. err = _cpu_sleep(d, 2000);
  2599. if (err) {
  2600. RTW_ERR("%s: IC CPU fail to enter sleep mode!(%d)\n",
  2601. __FUNCTION__, err);
  2602. /* skip this error */
  2603. err = 0;
  2604. }
  2605. }
  2606. /* 5. Download Firmware */
  2607. status = api->halmac_download_firmware(mac, fw, fwsize);
  2608. if (status != HALMAC_RET_SUCCESS) {
  2609. RTW_ERR("%s: download firmware FAIL! status=0x%02x\n",
  2610. __FUNCTION__, status);
  2611. _debug_dlfw_fail(d);
  2612. err = -1;
  2613. goto resume_tx;
  2614. }
  2615. /* 5.1. (Driver) Reset driver variables if needed */
  2616. hal->LastHMEBoxNum = 0;
  2617. /* 5.2. (Driver) Get FW version */
  2618. status = api->halmac_get_fw_version(mac, &fw_vesion);
  2619. if (status == HALMAC_RET_SUCCESS) {
  2620. hal->firmware_version = fw_vesion.version;
  2621. hal->firmware_sub_version = fw_vesion.sub_version;
  2622. hal->firmware_size = fwsize;
  2623. }
  2624. resume_tx:
  2625. /* 6. Driver resume TX if needed */
  2626. /* ToDo */
  2627. if (err)
  2628. goto exit;
  2629. if (re_dl) {
  2630. enum halmac_trx_mode mode;
  2631. /* 7. Change reserved page size */
  2632. err = _cfg_drv_rsvd_pg_num(d);
  2633. if (err)
  2634. return -1;
  2635. /* 8. Init TRX Configuration */
  2636. mode = _choose_trx_mode(d);
  2637. status = api->halmac_init_trx_cfg(mac, mode);
  2638. if (HALMAC_RET_SUCCESS != status)
  2639. return -1;
  2640. _init_trx_cfg_drv(d);
  2641. /* 9. Config RX Aggregation */
  2642. err = rtw_halmac_rx_agg_switch(d, _TRUE);
  2643. if (err)
  2644. return -1;
  2645. /* 10. Send General Info */
  2646. err = _send_general_info(d);
  2647. if (err)
  2648. return -1;
  2649. }
  2650. exit:
  2651. return err;
  2652. }
  2653. static int init_mac_flow(struct dvobj_priv *d)
  2654. {
  2655. PADAPTER p;
  2656. struct hal_com_data *hal;
  2657. struct halmac_adapter *halmac;
  2658. struct halmac_api *api;
  2659. enum halmac_drv_rsvd_pg_num rsvd_page_number;
  2660. union halmac_wlan_addr hwa;
  2661. enum halmac_trx_mode trx_mode;
  2662. enum halmac_ret_status status;
  2663. u8 drv_rsvd_num;
  2664. u8 nettype;
  2665. int err, err_ret = -1;
  2666. p = dvobj_get_primary_adapter(d);
  2667. hal = GET_HAL_DATA(p);
  2668. halmac = dvobj_to_halmac(d);
  2669. api = HALMAC_GET_API(halmac);
  2670. #ifdef CONFIG_SUPPORT_TRX_SHARED
  2671. status = api->halmac_cfg_rxff_expand_mode(halmac,
  2672. _rtw_get_trx_share_mode(p));
  2673. if (status != HALMAC_RET_SUCCESS)
  2674. goto out;
  2675. #endif
  2676. #if 0 /* It is not necessary to call this in normal driver */
  2677. status = api->halmac_cfg_la_mode(halmac, HALMAC_LA_MODE_DISABLE);
  2678. if (status != HALMAC_RET_SUCCESS)
  2679. goto out;
  2680. #endif
  2681. err = _cfg_drv_rsvd_pg_num(d);
  2682. if (err)
  2683. goto out;
  2684. #ifdef CONFIG_USB_HCI
  2685. status = api->halmac_set_bulkout_num(halmac, d->RtNumOutPipes);
  2686. if (status != HALMAC_RET_SUCCESS)
  2687. goto out;
  2688. #endif /* CONFIG_USB_HCI */
  2689. trx_mode = _choose_trx_mode(d);
  2690. status = api->halmac_init_mac_cfg(halmac, trx_mode);
  2691. if (status != HALMAC_RET_SUCCESS)
  2692. goto out;
  2693. _init_trx_cfg_drv(d);
  2694. err = rtw_halmac_rx_agg_switch(d, _TRUE);
  2695. if (err)
  2696. goto out;
  2697. nettype = dvobj_to_regsty(d)->wireless_mode;
  2698. if (is_supported_vht(nettype) == _TRUE)
  2699. status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_AC);
  2700. else if (is_supported_ht(nettype) == _TRUE)
  2701. status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_N);
  2702. else if (IsSupportedTxOFDM(nettype) == _TRUE)
  2703. status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_G);
  2704. else
  2705. status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_B);
  2706. if (status != HALMAC_RET_SUCCESS)
  2707. goto out;
  2708. err_ret = 0;
  2709. out:
  2710. return err_ret;
  2711. }
  2712. static int _drv_enable_trx(struct dvobj_priv *d)
  2713. {
  2714. struct _ADAPTER *adapter;
  2715. u32 status;
  2716. adapter = dvobj_get_primary_adapter(d);
  2717. if (adapter->bup == _FALSE) {
  2718. #ifdef CONFIG_NEW_NETDEV_HDL
  2719. status = rtw_mi_start_drv_threads(adapter);
  2720. #else
  2721. status = rtw_start_drv_threads(adapter);
  2722. #endif
  2723. if (status == _FAIL) {
  2724. RTW_ERR("%s: Start threads Failed!\n", __FUNCTION__);
  2725. return -1;
  2726. }
  2727. }
  2728. rtw_intf_start(adapter);
  2729. return 0;
  2730. }
  2731. /*
  2732. * Notices:
  2733. * Make sure
  2734. * 1. rtw_hal_get_hwreg(HW_VAR_RF_TYPE)
  2735. * 2. HAL_DATA_TYPE.rfe_type
  2736. * already ready for use before calling this function.
  2737. */
  2738. static int _halmac_init_hal(struct dvobj_priv *d, u8 *fw, u32 fwsize)
  2739. {
  2740. PADAPTER adapter;
  2741. struct halmac_adapter *halmac;
  2742. struct halmac_api *api;
  2743. enum halmac_ret_status status;
  2744. u32 ok;
  2745. u8 fw_ok = _FALSE;
  2746. int err, err_ret = -1;
  2747. adapter = dvobj_get_primary_adapter(d);
  2748. halmac = dvobj_to_halmac(d);
  2749. if (!halmac)
  2750. goto out;
  2751. api = HALMAC_GET_API(halmac);
  2752. /* StatePowerOff */
  2753. /* SKIP: halmac_init_adapter (Already done before) */
  2754. /* halmac_pre_Init_system_cfg */
  2755. /* halmac_mac_power_switch(on) */
  2756. /* halmac_Init_system_cfg */
  2757. ok = rtw_hal_power_on(adapter);
  2758. if (_FAIL == ok)
  2759. goto out;
  2760. /* StatePowerOn */
  2761. /* DownloadFW */
  2762. if (fw && fwsize) {
  2763. err = download_fw(d, fw, fwsize, 0);
  2764. if (err)
  2765. goto out;
  2766. fw_ok = _TRUE;
  2767. }
  2768. /* InitMACFlow */
  2769. err = init_mac_flow(d);
  2770. if (err)
  2771. goto out;
  2772. /* Driver insert flow: Enable TR/RX */
  2773. err = _drv_enable_trx(d);
  2774. if (err)
  2775. goto out;
  2776. /* halmac_send_general_info */
  2777. if (_TRUE == fw_ok) {
  2778. err = _send_general_info(d);
  2779. if (err)
  2780. goto out;
  2781. }
  2782. /* Init Phy parameter-MAC */
  2783. ok = rtw_hal_init_mac_register(adapter);
  2784. if (_FALSE == ok)
  2785. goto out;
  2786. /* StateMacInitialized */
  2787. /* halmac_cfg_drv_info */
  2788. err = rtw_halmac_config_rx_info(d, HALMAC_DRV_INFO_PHY_STATUS);
  2789. if (err)
  2790. goto out;
  2791. /* halmac_set_hw_value(HALMAC_HW_EN_BB_RF) */
  2792. /* Init BB, RF */
  2793. ok = rtw_hal_init_phy(adapter);
  2794. if (_FALSE == ok)
  2795. goto out;
  2796. status = api->halmac_init_interface_cfg(halmac);
  2797. if (status != HALMAC_RET_SUCCESS)
  2798. goto out;
  2799. /* SKIP: halmac_verify_platform_api */
  2800. /* SKIP: halmac_h2c_lb */
  2801. /* StateRxIdle */
  2802. err_ret = 0;
  2803. out:
  2804. return err_ret;
  2805. }
  2806. int rtw_halmac_init_hal(struct dvobj_priv *d)
  2807. {
  2808. return _halmac_init_hal(d, NULL, 0);
  2809. }
  2810. /*
  2811. * Notices:
  2812. * Make sure
  2813. * 1. rtw_hal_get_hwreg(HW_VAR_RF_TYPE)
  2814. * 2. HAL_DATA_TYPE.rfe_type
  2815. * already ready for use before calling this function.
  2816. */
  2817. int rtw_halmac_init_hal_fw(struct dvobj_priv *d, u8 *fw, u32 fwsize)
  2818. {
  2819. return _halmac_init_hal(d, fw, fwsize);
  2820. }
  2821. /*
  2822. * Notices:
  2823. * Make sure
  2824. * 1. rtw_hal_get_hwreg(HW_VAR_RF_TYPE)
  2825. * 2. HAL_DATA_TYPE.rfe_type
  2826. * already ready for use before calling this function.
  2827. */
  2828. int rtw_halmac_init_hal_fw_file(struct dvobj_priv *d, u8 *fwpath)
  2829. {
  2830. u8 *fw = NULL;
  2831. u32 fwmaxsize = 0, size = 0;
  2832. int err = 0;
  2833. err = rtw_halmac_get_fw_max_size(d, &fwmaxsize);
  2834. if (err) {
  2835. RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err);
  2836. return -1;
  2837. }
  2838. fw = rtw_zmalloc(fwmaxsize);
  2839. if (!fw)
  2840. return -1;
  2841. size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize);
  2842. if (!size) {
  2843. err = -1;
  2844. goto exit;
  2845. }
  2846. err = _halmac_init_hal(d, fw, size);
  2847. exit:
  2848. rtw_mfree(fw, fwmaxsize);
  2849. /*fw = NULL;*/
  2850. return err;
  2851. }
  2852. int rtw_halmac_deinit_hal(struct dvobj_priv *d)
  2853. {
  2854. PADAPTER adapter;
  2855. struct halmac_adapter *halmac;
  2856. struct halmac_api *api;
  2857. enum halmac_ret_status status;
  2858. int err = -1;
  2859. adapter = dvobj_get_primary_adapter(d);
  2860. halmac = dvobj_to_halmac(d);
  2861. if (!halmac)
  2862. goto out;
  2863. api = HALMAC_GET_API(halmac);
  2864. status = api->halmac_deinit_interface_cfg(halmac);
  2865. if (status != HALMAC_RET_SUCCESS)
  2866. goto out;
  2867. rtw_hal_power_off(adapter);
  2868. err = 0;
  2869. out:
  2870. return err;
  2871. }
  2872. int rtw_halmac_self_verify(struct dvobj_priv *d)
  2873. {
  2874. struct halmac_adapter *mac;
  2875. struct halmac_api *api;
  2876. enum halmac_ret_status status;
  2877. int err = -1;
  2878. mac = dvobj_to_halmac(d);
  2879. api = HALMAC_GET_API(mac);
  2880. status = api->halmac_verify_platform_api(mac);
  2881. if (status != HALMAC_RET_SUCCESS)
  2882. goto out;
  2883. status = api->halmac_h2c_lb(mac);
  2884. if (status != HALMAC_RET_SUCCESS)
  2885. goto out;
  2886. err = 0;
  2887. out:
  2888. return err;
  2889. }
  2890. static u8 rtw_halmac_txfifo_is_empty(struct dvobj_priv *d)
  2891. {
  2892. struct halmac_adapter *mac;
  2893. struct halmac_api *api;
  2894. enum halmac_ret_status status;
  2895. u32 chk_num = 10;
  2896. u8 rst = _FALSE;
  2897. mac = dvobj_to_halmac(d);
  2898. api = HALMAC_GET_API(mac);
  2899. status = api->halmac_txfifo_is_empty(mac, chk_num);
  2900. if (status == HALMAC_RET_SUCCESS)
  2901. rst = _TRUE;
  2902. return rst;
  2903. }
  2904. /**
  2905. * rtw_halmac_txfifo_wait_empty() - Wait TX FIFO to be emtpy
  2906. * @d: struct dvobj_priv*
  2907. * @timeout: time limit of wait, unit is ms
  2908. * 0 for no limit
  2909. *
  2910. * Wait TX FIFO to be emtpy.
  2911. *
  2912. * Rteurn 0 for TX FIFO is empty, otherwise not empty.
  2913. */
  2914. int rtw_halmac_txfifo_wait_empty(struct dvobj_priv *d, u32 timeout)
  2915. {
  2916. struct _ADAPTER *a;
  2917. u8 empty = _FALSE;
  2918. u32 cnt = 0;
  2919. systime start_time = 0;
  2920. u32 pass_time; /* ms */
  2921. a = dvobj_get_primary_adapter(d);
  2922. start_time = rtw_get_current_time();
  2923. do {
  2924. cnt++;
  2925. empty = rtw_halmac_txfifo_is_empty(d);
  2926. if (empty == _TRUE)
  2927. break;
  2928. if (timeout) {
  2929. pass_time = rtw_get_passing_time_ms(start_time);
  2930. if (pass_time > timeout)
  2931. break;
  2932. }
  2933. if (RTW_CANNOT_IO(a)) {
  2934. RTW_WARN("%s: Interrupted by I/O forbiden!\n", __FUNCTION__);
  2935. break;
  2936. }
  2937. rtw_msleep_os(2);
  2938. } while (1);
  2939. if (empty == _FALSE) {
  2940. #ifdef CONFIG_RTW_DEBUG
  2941. u16 dbg_reg[] = {0x210, 0x230, 0x234, 0x238, 0x23C, 0x240,
  2942. 0x41A, 0x10FC, 0x10F8, 0x11F4, 0x11F8};
  2943. u8 i;
  2944. u32 val;
  2945. if (!RTW_CANNOT_IO(a)) {
  2946. for (i = 0; i < ARRAY_SIZE(dbg_reg); i++) {
  2947. val = rtw_read32(a, dbg_reg[i]);
  2948. RTW_ERR("REG_%X:0x%08x\n", dbg_reg[i], val);
  2949. }
  2950. }
  2951. #endif /* CONFIG_RTW_DEBUG */
  2952. RTW_ERR("%s: Fail to wait txfifo empty!(cnt=%d)\n",
  2953. __FUNCTION__, cnt);
  2954. return -1;
  2955. }
  2956. return 0;
  2957. }
  2958. static enum halmac_dlfw_mem _fw_mem_drv2halmac(enum fw_mem mem, u8 tx_stop)
  2959. {
  2960. enum halmac_dlfw_mem mem_halmac = HALMAC_DLFW_MEM_UNDEFINE;
  2961. switch (mem) {
  2962. case FW_EMEM:
  2963. if (tx_stop == _FALSE)
  2964. mem_halmac = HALMAC_DLFW_MEM_EMEM_RSVD_PG;
  2965. else
  2966. mem_halmac = HALMAC_DLFW_MEM_EMEM;
  2967. break;
  2968. case FW_IMEM:
  2969. case FW_DMEM:
  2970. mem_halmac = HALMAC_DLFW_MEM_UNDEFINE;
  2971. break;
  2972. }
  2973. return mem_halmac;
  2974. }
  2975. int rtw_halmac_dlfw_mem(struct dvobj_priv *d, u8 *fw, u32 fwsize, enum fw_mem mem)
  2976. {
  2977. struct halmac_adapter *mac;
  2978. struct halmac_api *api;
  2979. enum halmac_ret_status status;
  2980. enum halmac_dlfw_mem dlfw_mem;
  2981. u8 tx_stop = _FALSE;
  2982. u32 chk_timeout = 2000; /* unit: ms */
  2983. int err = 0;
  2984. mac = dvobj_to_halmac(d);
  2985. api = HALMAC_GET_API(mac);
  2986. if ((!fw) || (!fwsize))
  2987. return -1;
  2988. #ifndef RTW_HALMAC_DLFW_MEM_NO_STOP_TX
  2989. /* 1. Driver Stop Tx */
  2990. /* ToDo */
  2991. /* 2. Driver Check Tx FIFO is empty */
  2992. err = rtw_halmac_txfifo_wait_empty(d, chk_timeout);
  2993. if (err)
  2994. tx_stop = _FALSE;
  2995. else
  2996. tx_stop = _TRUE;
  2997. #endif /* !RTW_HALMAC_DLFW_MEM_NO_STOP_TX */
  2998. /* 3. Download Firmware MEM */
  2999. dlfw_mem = _fw_mem_drv2halmac(mem, tx_stop);
  3000. if (dlfw_mem == HALMAC_DLFW_MEM_UNDEFINE) {
  3001. err = -1;
  3002. goto resume_tx;
  3003. }
  3004. status = api->halmac_free_download_firmware(mac, dlfw_mem, fw, fwsize);
  3005. if (status != HALMAC_RET_SUCCESS) {
  3006. RTW_ERR("%s: halmac_free_download_firmware fail(err=0x%x)\n",
  3007. __FUNCTION__, status);
  3008. err = -1;
  3009. goto resume_tx;
  3010. }
  3011. resume_tx:
  3012. #ifndef RTW_HALMAC_DLFW_MEM_NO_STOP_TX
  3013. /* 4. Driver resume TX if needed */
  3014. /* ToDo */
  3015. #endif /* !RTW_HALMAC_DLFW_MEM_NO_STOP_TX */
  3016. return err;
  3017. }
  3018. int rtw_halmac_dlfw_mem_from_file(struct dvobj_priv *d, u8 *fwpath, enum fw_mem mem)
  3019. {
  3020. u8 *fw = NULL;
  3021. u32 fwmaxsize = 0, size = 0;
  3022. int err = 0;
  3023. err = rtw_halmac_get_fw_max_size(d, &fwmaxsize);
  3024. if (err) {
  3025. RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err);
  3026. return -1;
  3027. }
  3028. fw = rtw_zmalloc(fwmaxsize);
  3029. if (!fw)
  3030. return -1;
  3031. size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize);
  3032. if (size)
  3033. err = rtw_halmac_dlfw_mem(d, fw, size, mem);
  3034. else
  3035. err = -1;
  3036. rtw_mfree(fw, fwmaxsize);
  3037. /*fw = NULL;*/
  3038. return err;
  3039. }
  3040. /*
  3041. * Return:
  3042. * 0 Success
  3043. * -22 Invalid arguemnt
  3044. */
  3045. int rtw_halmac_dlfw(struct dvobj_priv *d, u8 *fw, u32 fwsize)
  3046. {
  3047. PADAPTER adapter;
  3048. enum halmac_ret_status status;
  3049. u32 ok;
  3050. int err, err_ret = -1;
  3051. if (!fw || !fwsize)
  3052. return -22;
  3053. adapter = dvobj_get_primary_adapter(d);
  3054. /* re-download firmware */
  3055. if (rtw_is_hw_init_completed(adapter))
  3056. return download_fw(d, fw, fwsize, 1);
  3057. /* Download firmware before hal init */
  3058. /* Power on, download firmware and init mac */
  3059. ok = rtw_hal_power_on(adapter);
  3060. if (_FAIL == ok)
  3061. goto out;
  3062. err = download_fw(d, fw, fwsize, 0);
  3063. if (err) {
  3064. err_ret = err;
  3065. goto out;
  3066. }
  3067. err = init_mac_flow(d);
  3068. if (err)
  3069. goto out;
  3070. err = _send_general_info(d);
  3071. if (err)
  3072. goto out;
  3073. err_ret = 0;
  3074. out:
  3075. return err_ret;
  3076. }
  3077. int rtw_halmac_dlfw_from_file(struct dvobj_priv *d, u8 *fwpath)
  3078. {
  3079. u8 *fw = NULL;
  3080. u32 fwmaxsize = 0, size = 0;
  3081. int err = 0;
  3082. err = rtw_halmac_get_fw_max_size(d, &fwmaxsize);
  3083. if (err) {
  3084. RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err);
  3085. return -1;
  3086. }
  3087. fw = rtw_zmalloc(fwmaxsize);
  3088. if (!fw)
  3089. return -1;
  3090. size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize);
  3091. if (size)
  3092. err = rtw_halmac_dlfw(d, fw, size);
  3093. else
  3094. err = -1;
  3095. rtw_mfree(fw, fwmaxsize);
  3096. /*fw = NULL;*/
  3097. return err;
  3098. }
  3099. /*
  3100. * Description:
  3101. * Power on/off BB/RF domain.
  3102. *
  3103. * Parameters:
  3104. * enable _TRUE/_FALSE for power on/off
  3105. *
  3106. * Return:
  3107. * 0 Success
  3108. * others Fail
  3109. */
  3110. int rtw_halmac_phy_power_switch(struct dvobj_priv *d, u8 enable)
  3111. {
  3112. PADAPTER adapter;
  3113. struct halmac_adapter *halmac;
  3114. struct halmac_api *api;
  3115. enum halmac_ret_status status;
  3116. u8 on;
  3117. adapter = dvobj_get_primary_adapter(d);
  3118. halmac = dvobj_to_halmac(d);
  3119. if (!halmac)
  3120. return -1;
  3121. api = HALMAC_GET_API(halmac);
  3122. on = (enable == _TRUE) ? 1 : 0;
  3123. status = api->halmac_set_hw_value(halmac, HALMAC_HW_EN_BB_RF, &on);
  3124. if (status != HALMAC_RET_SUCCESS)
  3125. return -1;
  3126. return 0;
  3127. }
  3128. static u8 _is_fw_read_cmd_down(PADAPTER adapter, u8 msgbox_num)
  3129. {
  3130. u8 read_down = _FALSE;
  3131. int retry_cnts = 100;
  3132. u8 valid;
  3133. do {
  3134. valid = rtw_read8(adapter, REG_HMETFR) & BIT(msgbox_num);
  3135. if (0 == valid)
  3136. read_down = _TRUE;
  3137. else
  3138. rtw_msleep_os(1);
  3139. } while ((!read_down) && (retry_cnts--));
  3140. if (_FALSE == read_down)
  3141. RTW_WARN("%s, reg_1cc(%x), msg_box(%d)...\n", __func__, rtw_read8(adapter, REG_HMETFR), msgbox_num);
  3142. return read_down;
  3143. }
  3144. /**
  3145. * rtw_halmac_send_h2c() - Send H2C to firmware
  3146. * @d: struct dvobj_priv*
  3147. * @h2c: H2C data buffer, suppose to be 8 bytes
  3148. *
  3149. * Send H2C to firmware by message box register(0x1D0~0x1D3 & 0x1F0~0x1F3).
  3150. *
  3151. * Assume firmware be ready to accept H2C here, please check
  3152. * (hal->bFWReady == _TRUE) before call this function or make sure firmware is
  3153. * ready.
  3154. *
  3155. * Return: 0 if process OK, otherwise fail to send this H2C.
  3156. */
  3157. int rtw_halmac_send_h2c(struct dvobj_priv *d, u8 *h2c)
  3158. {
  3159. PADAPTER adapter = dvobj_get_primary_adapter(d);
  3160. PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter);
  3161. u8 h2c_box_num = 0;
  3162. u32 msgbox_addr = 0;
  3163. u32 msgbox_ex_addr = 0;
  3164. u32 h2c_cmd = 0;
  3165. u32 h2c_cmd_ex = 0;
  3166. int err = -1;
  3167. if (!h2c) {
  3168. RTW_WARN("%s: pbuf is NULL\n", __FUNCTION__);
  3169. return err;
  3170. }
  3171. if (rtw_is_surprise_removed(adapter)) {
  3172. RTW_WARN("%s: surprise removed\n", __FUNCTION__);
  3173. return err;
  3174. }
  3175. _enter_critical_mutex(&d->h2c_fwcmd_mutex, NULL);
  3176. /* pay attention to if race condition happened in H2C cmd setting */
  3177. h2c_box_num = hal->LastHMEBoxNum;
  3178. if (!_is_fw_read_cmd_down(adapter, h2c_box_num)) {
  3179. RTW_WARN(" fw read cmd failed...\n");
  3180. #ifdef DBG_CONFIG_ERROR_DETECT
  3181. hal->srestpriv.self_dect_fw = _TRUE;
  3182. hal->srestpriv.self_dect_fw_cnt++;
  3183. #endif /* DBG_CONFIG_ERROR_DETECT */
  3184. goto exit;
  3185. }
  3186. /* Write Ext command (byte 4~7) */
  3187. msgbox_ex_addr = REG_HMEBOX_E0 + (h2c_box_num * EX_MESSAGE_BOX_SIZE);
  3188. _rtw_memcpy((u8 *)(&h2c_cmd_ex), h2c + 4, EX_MESSAGE_BOX_SIZE);
  3189. h2c_cmd_ex = le32_to_cpu(h2c_cmd_ex);
  3190. rtw_write32(adapter, msgbox_ex_addr, h2c_cmd_ex);
  3191. /* Write command (byte 0~3) */
  3192. msgbox_addr = REG_HMEBOX0 + (h2c_box_num * MESSAGE_BOX_SIZE);
  3193. _rtw_memcpy((u8 *)(&h2c_cmd), h2c, 4);
  3194. h2c_cmd = le32_to_cpu(h2c_cmd);
  3195. rtw_write32(adapter, msgbox_addr, h2c_cmd);
  3196. /* update last msg box number */
  3197. hal->LastHMEBoxNum = (h2c_box_num + 1) % MAX_H2C_BOX_NUMS;
  3198. err = 0;
  3199. #ifdef DBG_H2C_CONTENT
  3200. RTW_INFO_DUMP("[H2C] - ", h2c, RTW_HALMAC_H2C_MAX_SIZE);
  3201. #endif
  3202. exit:
  3203. _exit_critical_mutex(&d->h2c_fwcmd_mutex, NULL);
  3204. return err;
  3205. }
  3206. /**
  3207. * rtw_halmac_c2h_handle() - Handle C2H for HALMAC
  3208. * @d: struct dvobj_priv*
  3209. * @c2h: Full C2H packet, including RX description and payload
  3210. * @size: Size(byte) of c2h
  3211. *
  3212. * Send C2H packet to HALMAC to process C2H packets, and the expected C2H ID is
  3213. * 0xFF. This function won't have any I/O, so caller doesn't have to call it in
  3214. * I/O safe place(ex. command thread).
  3215. *
  3216. * Please sure doesn't call this function in the same thread as someone is
  3217. * waiting HALMAC C2H ack, otherwise there is a deadlock happen.
  3218. *
  3219. * Return: 0 if process OK, otherwise no action for this C2H.
  3220. */
  3221. int rtw_halmac_c2h_handle(struct dvobj_priv *d, u8 *c2h, u32 size)
  3222. {
  3223. struct halmac_adapter *mac;
  3224. struct halmac_api *api;
  3225. enum halmac_ret_status status;
  3226. mac = dvobj_to_halmac(d);
  3227. api = HALMAC_GET_API(mac);
  3228. status = api->halmac_get_c2h_info(mac, c2h, size);
  3229. if (HALMAC_RET_SUCCESS != status)
  3230. return -1;
  3231. return 0;
  3232. }
  3233. int rtw_halmac_get_available_efuse_size(struct dvobj_priv *d, u32 *size)
  3234. {
  3235. struct halmac_adapter *mac;
  3236. struct halmac_api *api;
  3237. enum halmac_ret_status status;
  3238. u32 val;
  3239. mac = dvobj_to_halmac(d);
  3240. api = HALMAC_GET_API(mac);
  3241. status = api->halmac_get_efuse_available_size(mac, &val);
  3242. if (HALMAC_RET_SUCCESS != status)
  3243. return -1;
  3244. *size = val;
  3245. return 0;
  3246. }
  3247. int rtw_halmac_get_physical_efuse_size(struct dvobj_priv *d, u32 *size)
  3248. {
  3249. struct halmac_adapter *mac;
  3250. struct halmac_api *api;
  3251. enum halmac_ret_status status;
  3252. u32 val;
  3253. mac = dvobj_to_halmac(d);
  3254. api = HALMAC_GET_API(mac);
  3255. status = api->halmac_get_efuse_size(mac, &val);
  3256. if (HALMAC_RET_SUCCESS != status)
  3257. return -1;
  3258. *size = val;
  3259. return 0;
  3260. }
  3261. int rtw_halmac_read_physical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size)
  3262. {
  3263. struct halmac_adapter *mac;
  3264. struct halmac_api *api;
  3265. enum halmac_ret_status status;
  3266. enum halmac_feature_id id;
  3267. int ret;
  3268. mac = dvobj_to_halmac(d);
  3269. api = HALMAC_GET_API(mac);
  3270. id = HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE;
  3271. ret = init_halmac_event(d, id, map, size);
  3272. if (ret)
  3273. return -1;
  3274. status = api->halmac_dump_efuse_map(mac, HALMAC_EFUSE_R_DRV);
  3275. if (HALMAC_RET_SUCCESS != status) {
  3276. free_halmac_event(d, id);
  3277. return -1;
  3278. }
  3279. ret = wait_halmac_event(d, id);
  3280. if (ret)
  3281. return -1;
  3282. return 0;
  3283. }
  3284. int rtw_halmac_read_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)
  3285. {
  3286. struct halmac_adapter *mac;
  3287. struct halmac_api *api;
  3288. enum halmac_ret_status status;
  3289. u8 v;
  3290. u32 i;
  3291. u8 *efuse = NULL;
  3292. u32 size = 0;
  3293. int err = 0;
  3294. mac = dvobj_to_halmac(d);
  3295. api = HALMAC_GET_API(mac);
  3296. if (api->halmac_read_efuse) {
  3297. for (i = 0; i < cnt; i++) {
  3298. status = api->halmac_read_efuse(mac, offset + i, &v);
  3299. if (HALMAC_RET_SUCCESS != status)
  3300. return -1;
  3301. data[i] = v;
  3302. }
  3303. } else {
  3304. err = rtw_halmac_get_physical_efuse_size(d, &size);
  3305. if (err)
  3306. return -1;
  3307. efuse = rtw_zmalloc(size);
  3308. if (!efuse)
  3309. return -1;
  3310. err = rtw_halmac_read_physical_efuse_map(d, efuse, size);
  3311. if (err)
  3312. err = -1;
  3313. else
  3314. _rtw_memcpy(data, efuse + offset, cnt);
  3315. rtw_mfree(efuse, size);
  3316. }
  3317. return err;
  3318. }
  3319. int rtw_halmac_write_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)
  3320. {
  3321. struct halmac_adapter *mac;
  3322. struct halmac_api *api;
  3323. enum halmac_ret_status status;
  3324. u32 i;
  3325. mac = dvobj_to_halmac(d);
  3326. api = HALMAC_GET_API(mac);
  3327. if (api->halmac_write_efuse == NULL)
  3328. return -1;
  3329. for (i = 0; i < cnt; i++) {
  3330. status = api->halmac_write_efuse(mac, offset + i, data[i]);
  3331. if (HALMAC_RET_SUCCESS != status)
  3332. return -1;
  3333. }
  3334. return 0;
  3335. }
  3336. int rtw_halmac_get_logical_efuse_size(struct dvobj_priv *d, u32 *size)
  3337. {
  3338. struct halmac_adapter *mac;
  3339. struct halmac_api *api;
  3340. enum halmac_ret_status status;
  3341. u32 val;
  3342. mac = dvobj_to_halmac(d);
  3343. api = HALMAC_GET_API(mac);
  3344. status = api->halmac_get_logical_efuse_size(mac, &val);
  3345. if (HALMAC_RET_SUCCESS != status)
  3346. return -1;
  3347. *size = val;
  3348. return 0;
  3349. }
  3350. int rtw_halmac_read_logical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size, u8 *maskmap, u32 masksize)
  3351. {
  3352. struct halmac_adapter *mac;
  3353. struct halmac_api *api;
  3354. enum halmac_ret_status status;
  3355. enum halmac_feature_id id;
  3356. int ret;
  3357. mac = dvobj_to_halmac(d);
  3358. api = HALMAC_GET_API(mac);
  3359. id = HALMAC_FEATURE_DUMP_LOGICAL_EFUSE;
  3360. ret = init_halmac_event(d, id, map, size);
  3361. if (ret)
  3362. return -1;
  3363. status = api->halmac_dump_logical_efuse_map(mac, HALMAC_EFUSE_R_DRV);
  3364. if (HALMAC_RET_SUCCESS != status) {
  3365. free_halmac_event(d, id);
  3366. return -1;
  3367. }
  3368. ret = wait_halmac_event(d, id);
  3369. if (ret)
  3370. return -1;
  3371. if (maskmap && masksize) {
  3372. struct halmac_pg_efuse_info pginfo;
  3373. pginfo.efuse_map = map;
  3374. pginfo.efuse_map_size = size;
  3375. pginfo.efuse_mask = maskmap;
  3376. pginfo.efuse_mask_size = masksize;
  3377. status = api->halmac_mask_logical_efuse(mac, &pginfo);
  3378. if (status != HALMAC_RET_SUCCESS)
  3379. RTW_WARN("%s: mask logical efuse FAIL!\n", __FUNCTION__);
  3380. }
  3381. return 0;
  3382. }
  3383. int rtw_halmac_write_logical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size, u8 *maskmap, u32 masksize)
  3384. {
  3385. struct halmac_adapter *mac;
  3386. struct halmac_api *api;
  3387. struct halmac_pg_efuse_info pginfo;
  3388. enum halmac_ret_status status;
  3389. mac = dvobj_to_halmac(d);
  3390. api = HALMAC_GET_API(mac);
  3391. pginfo.efuse_map = map;
  3392. pginfo.efuse_map_size = size;
  3393. pginfo.efuse_mask = maskmap;
  3394. pginfo.efuse_mask_size = masksize;
  3395. status = api->halmac_pg_efuse_by_map(mac, &pginfo, HALMAC_EFUSE_R_AUTO);
  3396. if (HALMAC_RET_SUCCESS != status)
  3397. return -1;
  3398. return 0;
  3399. }
  3400. int rtw_halmac_read_logical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)
  3401. {
  3402. struct halmac_adapter *mac;
  3403. struct halmac_api *api;
  3404. enum halmac_ret_status status;
  3405. u8 v;
  3406. u32 i;
  3407. mac = dvobj_to_halmac(d);
  3408. api = HALMAC_GET_API(mac);
  3409. for (i = 0; i < cnt; i++) {
  3410. status = api->halmac_read_logical_efuse(mac, offset + i, &v);
  3411. if (HALMAC_RET_SUCCESS != status)
  3412. return -1;
  3413. data[i] = v;
  3414. }
  3415. return 0;
  3416. }
  3417. int rtw_halmac_write_logical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)
  3418. {
  3419. struct halmac_adapter *mac;
  3420. struct halmac_api *api;
  3421. enum halmac_ret_status status;
  3422. u32 i;
  3423. mac = dvobj_to_halmac(d);
  3424. api = HALMAC_GET_API(mac);
  3425. for (i = 0; i < cnt; i++) {
  3426. status = api->halmac_write_logical_efuse(mac, offset + i, data[i]);
  3427. if (HALMAC_RET_SUCCESS != status)
  3428. return -1;
  3429. }
  3430. return 0;
  3431. }
  3432. int rtw_halmac_write_bt_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)
  3433. {
  3434. struct halmac_adapter *mac;
  3435. struct halmac_api *api;
  3436. enum halmac_ret_status status;
  3437. u32 i;
  3438. u8 bank = 1;
  3439. mac = dvobj_to_halmac(d);
  3440. api = HALMAC_GET_API(mac);
  3441. for (i = 0; i < cnt; i++) {
  3442. status = api->halmac_write_efuse_bt(mac, offset + i, data[i], bank);
  3443. if (HALMAC_RET_SUCCESS != status) {
  3444. printk("%s: halmac_write_efuse_bt status = %d\n", __FUNCTION__, status);
  3445. return -1;
  3446. }
  3447. }
  3448. printk("%s: halmac_write_efuse_bt status = HALMAC_RET_SUCCESS %d\n", __FUNCTION__, status);
  3449. return 0;
  3450. }
  3451. int rtw_halmac_read_bt_physical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size)
  3452. {
  3453. struct halmac_adapter *mac;
  3454. struct halmac_api *api;
  3455. enum halmac_ret_status status;
  3456. int bank = 1;
  3457. mac = dvobj_to_halmac(d);
  3458. api = HALMAC_GET_API(mac);
  3459. status = api->halmac_dump_efuse_map_bt(mac, bank, size, map);
  3460. if (HALMAC_RET_SUCCESS != status) {
  3461. printk("%s: halmac_dump_efuse_map_bt fail!\n", __FUNCTION__);
  3462. return -1;
  3463. }
  3464. printk("%s: OK!\n", __FUNCTION__);
  3465. return 0;
  3466. }
  3467. static enum hal_fifo_sel _fifo_sel_drv2halmac(u8 fifo_sel)
  3468. {
  3469. switch (fifo_sel) {
  3470. case 0:
  3471. return HAL_FIFO_SEL_TX;
  3472. case 1:
  3473. return HAL_FIFO_SEL_RX;
  3474. case 2:
  3475. return HAL_FIFO_SEL_RSVD_PAGE;
  3476. case 3:
  3477. return HAL_FIFO_SEL_REPORT;
  3478. case 4:
  3479. return HAL_FIFO_SEL_LLT;
  3480. case 5:
  3481. return HAL_FIFO_SEL_RXBUF_FW;
  3482. }
  3483. return HAL_FIFO_SEL_RSVD_PAGE;
  3484. }
  3485. /*#define CONFIG_HALMAC_FIFO_DUMP*/
  3486. int rtw_halmac_dump_fifo(struct dvobj_priv *d, u8 fifo_sel, u32 addr, u32 size, u8 *buffer)
  3487. {
  3488. struct halmac_adapter *mac;
  3489. struct halmac_api *api;
  3490. enum hal_fifo_sel halmac_fifo_sel;
  3491. enum halmac_ret_status status;
  3492. u8 *pfifo_map = NULL;
  3493. u32 fifo_size = 0;
  3494. s8 ret = 0;/* 0:success, -1:error */
  3495. u8 mem_created = _FALSE;
  3496. mac = dvobj_to_halmac(d);
  3497. api = HALMAC_GET_API(mac);
  3498. if ((size != 0) && (buffer == NULL))
  3499. return -1;
  3500. halmac_fifo_sel = _fifo_sel_drv2halmac(fifo_sel);
  3501. if ((size) && (buffer)) {
  3502. pfifo_map = buffer;
  3503. fifo_size = size;
  3504. } else {
  3505. fifo_size = api->halmac_get_fifo_size(mac, halmac_fifo_sel);
  3506. if (fifo_size)
  3507. pfifo_map = rtw_zvmalloc(fifo_size);
  3508. if (pfifo_map == NULL)
  3509. return -1;
  3510. mem_created = _TRUE;
  3511. }
  3512. status = api->halmac_dump_fifo(mac, halmac_fifo_sel, addr, fifo_size, pfifo_map);
  3513. if (HALMAC_RET_SUCCESS != status) {
  3514. ret = -1;
  3515. goto _exit;
  3516. }
  3517. #ifdef CONFIG_HALMAC_FIFO_DUMP
  3518. {
  3519. static const char * const fifo_sel_str[] = {
  3520. "TX", "RX", "RSVD_PAGE", "REPORT", "LLT", "RXBUF_FW"
  3521. };
  3522. RTW_INFO("%s FIFO DUMP [start_addr:0x%04x , size:%d]\n", fifo_sel_str[halmac_fifo_sel], addr, fifo_size);
  3523. RTW_INFO_DUMP("\n", pfifo_map, fifo_size);
  3524. RTW_INFO(" ==================================================\n");
  3525. }
  3526. #endif /* CONFIG_HALMAC_FIFO_DUMP */
  3527. _exit:
  3528. if ((mem_created == _TRUE) && pfifo_map)
  3529. rtw_vmfree(pfifo_map, fifo_size);
  3530. return ret;
  3531. }
  3532. /*
  3533. * rtw_halmac_rx_agg_switch() - Switch RX aggregation function and setting
  3534. * @d struct dvobj_priv *
  3535. * @enable _FALSE/_TRUE for disable/enable RX aggregation function
  3536. *
  3537. * This function could help to on/off bus RX aggregation function, and is only
  3538. * useful for SDIO and USB interface. Although only "enable" flag is brough in,
  3539. * some setting would be taken from other places, and they are from:
  3540. * [DMA aggregation]
  3541. * struct hal_com_data.rxagg_dma_size
  3542. * struct hal_com_data.rxagg_dma_timeout
  3543. * [USB aggregation] (only use for USB interface)
  3544. * struct hal_com_data.rxagg_usb_size
  3545. * struct hal_com_data.rxagg_usb_timeout
  3546. * If above values of size and timeout are both 0 means driver would not
  3547. * control the threshold setting and leave it to HALMAC handle.
  3548. *
  3549. * From HALMAC V1_04_04, driver force the size threshold be hard limit, and the
  3550. * rx size can not exceed the setting.
  3551. *
  3552. * Return 0 for success, otherwise fail.
  3553. */
  3554. int rtw_halmac_rx_agg_switch(struct dvobj_priv *d, u8 enable)
  3555. {
  3556. struct _ADAPTER *adapter;
  3557. struct hal_com_data *hal;
  3558. struct halmac_adapter *halmac;
  3559. struct halmac_api *api;
  3560. struct halmac_rxagg_cfg rxaggcfg;
  3561. enum halmac_ret_status status;
  3562. adapter = dvobj_get_primary_adapter(d);
  3563. hal = GET_HAL_DATA(adapter);
  3564. halmac = dvobj_to_halmac(d);
  3565. api = HALMAC_GET_API(halmac);
  3566. _rtw_memset((void *)&rxaggcfg, 0, sizeof(rxaggcfg));
  3567. rxaggcfg.mode = HALMAC_RX_AGG_MODE_NONE;
  3568. /*
  3569. * Always enable size limit to avoid rx size exceed
  3570. * driver defined size.
  3571. */
  3572. rxaggcfg.threshold.size_limit_en = 1;
  3573. #ifdef RTW_RX_AGGREGATION
  3574. if (_TRUE == enable) {
  3575. #ifdef CONFIG_SDIO_HCI
  3576. rxaggcfg.mode = HALMAC_RX_AGG_MODE_DMA;
  3577. rxaggcfg.threshold.drv_define = 0;
  3578. if (hal->rxagg_dma_size || hal->rxagg_dma_timeout) {
  3579. rxaggcfg.threshold.drv_define = 1;
  3580. rxaggcfg.threshold.timeout = hal->rxagg_dma_timeout;
  3581. rxaggcfg.threshold.size = hal->rxagg_dma_size;
  3582. RTW_INFO("%s: RX aggregation threshold: "
  3583. "timeout=%u size=%u\n",
  3584. __FUNCTION__,
  3585. hal->rxagg_dma_timeout,
  3586. hal->rxagg_dma_size);
  3587. }
  3588. #elif defined(CONFIG_USB_HCI)
  3589. switch (hal->rxagg_mode) {
  3590. case RX_AGG_DISABLE:
  3591. rxaggcfg.mode = HALMAC_RX_AGG_MODE_NONE;
  3592. break;
  3593. case RX_AGG_DMA:
  3594. rxaggcfg.mode = HALMAC_RX_AGG_MODE_DMA;
  3595. if (hal->rxagg_dma_size || hal->rxagg_dma_timeout) {
  3596. rxaggcfg.threshold.drv_define = 1;
  3597. rxaggcfg.threshold.timeout = hal->rxagg_dma_timeout;
  3598. rxaggcfg.threshold.size = hal->rxagg_dma_size;
  3599. }
  3600. break;
  3601. case RX_AGG_USB:
  3602. case RX_AGG_MIX:
  3603. rxaggcfg.mode = HALMAC_RX_AGG_MODE_USB;
  3604. if (hal->rxagg_usb_size || hal->rxagg_usb_timeout) {
  3605. rxaggcfg.threshold.drv_define = 1;
  3606. rxaggcfg.threshold.timeout = hal->rxagg_usb_timeout;
  3607. rxaggcfg.threshold.size = hal->rxagg_usb_size;
  3608. }
  3609. break;
  3610. }
  3611. #endif /* CONFIG_USB_HCI */
  3612. }
  3613. #endif /* RTW_RX_AGGREGATION */
  3614. status = api->halmac_cfg_rx_aggregation(halmac, &rxaggcfg);
  3615. if (status != HALMAC_RET_SUCCESS)
  3616. return -1;
  3617. return 0;
  3618. }
  3619. int rtw_halmac_download_rsvd_page(struct dvobj_priv *dvobj, u8 pg_offset, u8 *pbuf, u32 size)
  3620. {
  3621. enum halmac_ret_status status = HALMAC_RET_SUCCESS;
  3622. struct halmac_adapter *halmac = dvobj_to_halmac(dvobj);
  3623. struct halmac_api *api = HALMAC_GET_API(halmac);
  3624. status = api->halmac_dl_drv_rsvd_page(halmac, pg_offset, pbuf, size);
  3625. if (status != HALMAC_RET_SUCCESS)
  3626. return -1;
  3627. return 0;
  3628. }
  3629. /*
  3630. * Description
  3631. * Fill following spec info from HALMAC API:
  3632. * sec_cam_ent_num
  3633. *
  3634. * Return
  3635. * 0 Success
  3636. * others Fail
  3637. */
  3638. int rtw_halmac_fill_hal_spec(struct dvobj_priv *dvobj, struct hal_spec_t *spec)
  3639. {
  3640. enum halmac_ret_status status;
  3641. struct halmac_adapter *halmac;
  3642. struct halmac_api *api;
  3643. u8 cam = 0; /* Security Cam Entry Number */
  3644. halmac = dvobj_to_halmac(dvobj);
  3645. api = HALMAC_GET_API(halmac);
  3646. /* Prepare data from HALMAC */
  3647. status = api->halmac_get_hw_value(halmac, HALMAC_HW_CAM_ENTRY_NUM, &cam);
  3648. if (status != HALMAC_RET_SUCCESS)
  3649. return -1;
  3650. /* Fill data to hal_spec_t */
  3651. spec->sec_cam_ent_num = cam;
  3652. return 0;
  3653. }
  3654. int rtw_halmac_p2pps(struct dvobj_priv *dvobj, struct hal_p2p_ps_para *pp2p_ps_para)
  3655. {
  3656. enum halmac_ret_status status = HALMAC_RET_SUCCESS;
  3657. struct halmac_adapter *halmac = dvobj_to_halmac(dvobj);
  3658. struct halmac_api *api = HALMAC_GET_API(halmac);
  3659. struct halmac_p2pps halmac_p2p_ps;
  3660. (&halmac_p2p_ps)->offload_en = pp2p_ps_para->offload_en;
  3661. (&halmac_p2p_ps)->role = pp2p_ps_para->role;
  3662. (&halmac_p2p_ps)->ctwindow_en = pp2p_ps_para->ctwindow_en;
  3663. (&halmac_p2p_ps)->noa_en = pp2p_ps_para->noa_en;
  3664. (&halmac_p2p_ps)->noa_sel = pp2p_ps_para->noa_sel;
  3665. (&halmac_p2p_ps)->all_sta_sleep = pp2p_ps_para->all_sta_sleep;
  3666. (&halmac_p2p_ps)->discovery = pp2p_ps_para->discovery;
  3667. (&halmac_p2p_ps)->p2p_port_id = _hw_port_drv2halmac(pp2p_ps_para->p2p_port_id);
  3668. (&halmac_p2p_ps)->p2p_group = pp2p_ps_para->p2p_group;
  3669. (&halmac_p2p_ps)->p2p_macid = pp2p_ps_para->p2p_macid;
  3670. (&halmac_p2p_ps)->ctwindow_length = pp2p_ps_para->ctwindow_length;
  3671. (&halmac_p2p_ps)->noa_duration_para = pp2p_ps_para->noa_duration_para;
  3672. (&halmac_p2p_ps)->noa_interval_para = pp2p_ps_para->noa_interval_para;
  3673. (&halmac_p2p_ps)->noa_start_time_para = pp2p_ps_para->noa_start_time_para;
  3674. (&halmac_p2p_ps)->noa_count_para = pp2p_ps_para->noa_count_para;
  3675. status = api->halmac_p2pps(halmac, (&halmac_p2p_ps));
  3676. if (status != HALMAC_RET_SUCCESS)
  3677. return -1;
  3678. return 0;
  3679. }
  3680. /**
  3681. * rtw_halmac_iqk() - Run IQ Calibration
  3682. * @d: struct dvobj_priv*
  3683. * @clear: IQK parameters
  3684. * @segment: IQK parameters
  3685. *
  3686. * Process IQ Calibration(IQK).
  3687. *
  3688. * Rteurn: 0 for OK, otherwise fail.
  3689. */
  3690. int rtw_halmac_iqk(struct dvobj_priv *d, u8 clear, u8 segment)
  3691. {
  3692. struct halmac_adapter *mac;
  3693. struct halmac_api *api;
  3694. enum halmac_ret_status status;
  3695. enum halmac_feature_id id;
  3696. struct halmac_iqk_para para;
  3697. int ret;
  3698. u8 retry = 3;
  3699. u8 delay = 1; /* ms */
  3700. mac = dvobj_to_halmac(d);
  3701. api = HALMAC_GET_API(mac);
  3702. id = HALMAC_FEATURE_IQK;
  3703. ret = init_halmac_event(d, id, NULL, 0);
  3704. if (ret)
  3705. return -1;
  3706. para.clear = clear;
  3707. para.segment_iqk = segment;
  3708. do {
  3709. status = api->halmac_start_iqk(mac, &para);
  3710. if (status != HALMAC_RET_BUSY_STATE)
  3711. break;
  3712. RTW_WARN("%s: Fail to start IQK, status is BUSY! retry=%d\n", __FUNCTION__, retry);
  3713. if (!retry)
  3714. break;
  3715. retry--;
  3716. rtw_msleep_os(delay);
  3717. } while (1);
  3718. if (status != HALMAC_RET_SUCCESS) {
  3719. free_halmac_event(d, id);
  3720. return -1;
  3721. }
  3722. ret = wait_halmac_event(d, id);
  3723. if (ret)
  3724. return -1;
  3725. return 0;
  3726. }
  3727. static inline u32 _phy_parameter_val_drv2halmac(u32 val, u8 msk_en, u32 msk)
  3728. {
  3729. if (!msk_en)
  3730. return val;
  3731. return (val << bitshift(msk));
  3732. }
  3733. static int _phy_parameter_drv2halmac(struct rtw_phy_parameter *para, struct halmac_phy_parameter_info *info)
  3734. {
  3735. if (!para || !info)
  3736. return -1;
  3737. _rtw_memset(info, 0, sizeof(*info));
  3738. switch (para->cmd) {
  3739. case 0:
  3740. /* MAC register */
  3741. switch (para->data.mac.size) {
  3742. case 1:
  3743. info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W8;
  3744. break;
  3745. case 2:
  3746. info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W16;
  3747. break;
  3748. default:
  3749. info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W32;
  3750. break;
  3751. }
  3752. info->content.MAC_REG_W.value = _phy_parameter_val_drv2halmac(
  3753. para->data.mac.value,
  3754. para->data.mac.msk_en,
  3755. para->data.mac.msk);
  3756. info->content.MAC_REG_W.msk = para->data.mac.msk;
  3757. info->content.MAC_REG_W.offset = para->data.mac.offset;
  3758. info->content.MAC_REG_W.msk_en = para->data.mac.msk_en;
  3759. break;
  3760. case 1:
  3761. /* BB register */
  3762. switch (para->data.bb.size) {
  3763. case 1:
  3764. info->cmd_id = HALMAC_PARAMETER_CMD_BB_W8;
  3765. break;
  3766. case 2:
  3767. info->cmd_id = HALMAC_PARAMETER_CMD_BB_W16;
  3768. break;
  3769. default:
  3770. info->cmd_id = HALMAC_PARAMETER_CMD_BB_W32;
  3771. break;
  3772. }
  3773. info->content.BB_REG_W.value = _phy_parameter_val_drv2halmac(
  3774. para->data.bb.value,
  3775. para->data.bb.msk_en,
  3776. para->data.bb.msk);
  3777. info->content.BB_REG_W.msk = para->data.bb.msk;
  3778. info->content.BB_REG_W.offset = para->data.bb.offset;
  3779. info->content.BB_REG_W.msk_en = para->data.bb.msk_en;
  3780. break;
  3781. case 2:
  3782. /* RF register */
  3783. info->cmd_id = HALMAC_PARAMETER_CMD_RF_W;
  3784. info->content.RF_REG_W.value = _phy_parameter_val_drv2halmac(
  3785. para->data.rf.value,
  3786. para->data.rf.msk_en,
  3787. para->data.rf.msk);
  3788. info->content.RF_REG_W.msk = para->data.rf.msk;
  3789. info->content.RF_REG_W.offset = para->data.rf.offset;
  3790. info->content.RF_REG_W.msk_en = para->data.rf.msk_en;
  3791. info->content.RF_REG_W.rf_path = para->data.rf.path;
  3792. break;
  3793. case 3:
  3794. /* Delay register */
  3795. if (para->data.delay.unit == 0)
  3796. info->cmd_id = HALMAC_PARAMETER_CMD_DELAY_US;
  3797. else
  3798. info->cmd_id = HALMAC_PARAMETER_CMD_DELAY_MS;
  3799. info->content.DELAY_TIME.delay_time = para->data.delay.value;
  3800. break;
  3801. case 0xFF:
  3802. /* Latest(End) command */
  3803. info->cmd_id = HALMAC_PARAMETER_CMD_END;
  3804. break;
  3805. default:
  3806. return -1;
  3807. }
  3808. return 0;
  3809. }
  3810. /**
  3811. * rtw_halmac_cfg_phy_para() - Register(Phy parameter) configuration
  3812. * @d: struct dvobj_priv*
  3813. * @para: phy parameter
  3814. *
  3815. * Configure registers by firmware using H2C/C2H mechanism.
  3816. * The latest command should be para->cmd==0xFF(End command) to finish all
  3817. * processes.
  3818. *
  3819. * Return: 0 for OK, otherwise fail.
  3820. */
  3821. int rtw_halmac_cfg_phy_para(struct dvobj_priv *d, struct rtw_phy_parameter *para)
  3822. {
  3823. struct halmac_adapter *mac;
  3824. struct halmac_api *api;
  3825. enum halmac_ret_status status;
  3826. enum halmac_feature_id id;
  3827. struct halmac_phy_parameter_info info;
  3828. u8 full_fifo;
  3829. int err, ret;
  3830. mac = dvobj_to_halmac(d);
  3831. api = HALMAC_GET_API(mac);
  3832. id = HALMAC_FEATURE_CFG_PARA;
  3833. full_fifo = 1; /* ToDo: How to deciede? */
  3834. ret = 0;
  3835. err = _phy_parameter_drv2halmac(para, &info);
  3836. if (err)
  3837. return -1;
  3838. err = init_halmac_event(d, id, NULL, 0);
  3839. if (err)
  3840. return -1;
  3841. status = api->halmac_cfg_parameter(mac, &info, full_fifo);
  3842. if (info.cmd_id == HALMAC_PARAMETER_CMD_END) {
  3843. if (status == HALMAC_RET_SUCCESS) {
  3844. err = wait_halmac_event(d, id);
  3845. if (err)
  3846. ret = -1;
  3847. } else {
  3848. free_halmac_event(d, id);
  3849. ret = -1;
  3850. RTW_ERR("%s: Fail to send END of cfg parameter, status is 0x%x!\n", __FUNCTION__, status);
  3851. }
  3852. } else {
  3853. if (status == HALMAC_RET_PARA_SENDING) {
  3854. err = wait_halmac_event(d, id);
  3855. if (err)
  3856. ret = -1;
  3857. } else {
  3858. free_halmac_event(d, id);
  3859. if (status != HALMAC_RET_SUCCESS) {
  3860. ret = -1;
  3861. RTW_ERR("%s: Fail to cfg parameter, status is 0x%x!\n", __FUNCTION__, status);
  3862. }
  3863. }
  3864. }
  3865. return ret;
  3866. }
  3867. static enum halmac_wlled_mode _led_mode_drv2halmac(u8 drv_mode)
  3868. {
  3869. enum halmac_wlled_mode halmac_mode;
  3870. switch (drv_mode) {
  3871. case 1:
  3872. halmac_mode = HALMAC_WLLED_MODE_TX;
  3873. break;
  3874. case 2:
  3875. halmac_mode = HALMAC_WLLED_MODE_RX;
  3876. break;
  3877. case 3:
  3878. halmac_mode = HALMAC_WLLED_MODE_SW_CTRL;
  3879. break;
  3880. case 0:
  3881. default:
  3882. halmac_mode = HALMAC_WLLED_MODE_TRX;
  3883. break;
  3884. }
  3885. return halmac_mode;
  3886. }
  3887. /**
  3888. * rtw_halmac_led_cfg() - Configure Hardware LED Mode
  3889. * @d: struct dvobj_priv*
  3890. * @enable: enable or disable LED function
  3891. * 0: disable
  3892. * 1: enable
  3893. * @mode: WLan LED mode (valid when enable==1)
  3894. * 0: Blink when TX(transmit packet) and RX(receive packet)
  3895. * 1: Blink when TX only
  3896. * 2: Blink when RX only
  3897. * 3: Software control
  3898. *
  3899. * Configure hardware WLan LED mode.
  3900. * If want to change LED mode after enabled, need to disable LED first and
  3901. * enable again to set new mode.
  3902. *
  3903. * Rteurn 0 for OK, otherwise fail.
  3904. */
  3905. int rtw_halmac_led_cfg(struct dvobj_priv *d, u8 enable, u8 mode)
  3906. {
  3907. struct halmac_adapter *halmac;
  3908. struct halmac_api *api;
  3909. enum halmac_wlled_mode led_mode;
  3910. enum halmac_ret_status status;
  3911. halmac = dvobj_to_halmac(d);
  3912. api = HALMAC_GET_API(halmac);
  3913. if (enable) {
  3914. status = api->halmac_pinmux_set_func(halmac,
  3915. HALMAC_GPIO_FUNC_WL_LED);
  3916. if (status != HALMAC_RET_SUCCESS) {
  3917. RTW_ERR("%s: pinmux set fail!(0x%x)\n",
  3918. __FUNCTION__, status);
  3919. return -1;
  3920. }
  3921. led_mode = _led_mode_drv2halmac(mode);
  3922. status = api->halmac_pinmux_wl_led_mode(halmac, led_mode);
  3923. if (status != HALMAC_RET_SUCCESS) {
  3924. RTW_ERR("%s: mode set fail!(0x%x)\n",
  3925. __FUNCTION__, status);
  3926. return -1;
  3927. }
  3928. } else {
  3929. /* Change LED to software control and turn off */
  3930. api->halmac_pinmux_wl_led_mode(halmac,
  3931. HALMAC_WLLED_MODE_SW_CTRL);
  3932. api->halmac_pinmux_wl_led_sw_ctrl(halmac, 0);
  3933. status = api->halmac_pinmux_free_func(halmac,
  3934. HALMAC_GPIO_FUNC_WL_LED);
  3935. if (status != HALMAC_RET_SUCCESS) {
  3936. RTW_ERR("%s: pinmux free fail!(0x%x)\n",
  3937. __FUNCTION__, status);
  3938. return -1;
  3939. }
  3940. }
  3941. return 0;
  3942. }
  3943. /**
  3944. * rtw_halmac_led_switch() - Turn Hardware LED on/off
  3945. * @d: struct dvobj_priv*
  3946. * @on: LED light or not
  3947. * 0: Off
  3948. * 1: On(Light)
  3949. *
  3950. * Turn Hardware WLan LED On/Off.
  3951. * Before use this function, user should call rtw_halmac_led_ctrl() to switch
  3952. * mode to "software control(3)" first, otherwise control would fail.
  3953. * The interval between on and off must be longer than 1 ms, or the LED would
  3954. * keep light or dark only.
  3955. * Ex. Turn off LED at first, turn on after 0.5ms and turn off again after
  3956. * 0.5ms. The LED during this flow will only keep dark, and miss the turn on
  3957. * operation between two turn off operations.
  3958. */
  3959. void rtw_halmac_led_switch(struct dvobj_priv *d, u8 on)
  3960. {
  3961. struct halmac_adapter *halmac;
  3962. struct halmac_api *api;
  3963. halmac = dvobj_to_halmac(d);
  3964. api = HALMAC_GET_API(halmac);
  3965. api->halmac_pinmux_wl_led_sw_ctrl(halmac, on);
  3966. }
  3967. #ifdef CONFIG_SDIO_HCI
  3968. /*
  3969. * Description:
  3970. * Update queue allocated page number to driver
  3971. *
  3972. * Parameter:
  3973. * d pointer to struct dvobj_priv of driver
  3974. *
  3975. * Rteurn:
  3976. * 0 Success, "page" is valid.
  3977. * others Fail, "page" is invalid.
  3978. */
  3979. int rtw_halmac_query_tx_page_num(struct dvobj_priv *d)
  3980. {
  3981. PADAPTER adapter;
  3982. struct halmacpriv *hmpriv;
  3983. struct halmac_adapter *halmac;
  3984. struct halmac_api *api;
  3985. struct halmac_rqpn_map rqpn;
  3986. enum halmac_dma_mapping dmaqueue;
  3987. struct halmac_txff_allocation fifosize;
  3988. enum halmac_ret_status status;
  3989. u8 i;
  3990. adapter = dvobj_get_primary_adapter(d);
  3991. hmpriv = &d->hmpriv;
  3992. halmac = dvobj_to_halmac(d);
  3993. api = HALMAC_GET_API(halmac);
  3994. _rtw_memset((void *)&rqpn, 0, sizeof(rqpn));
  3995. _rtw_memset((void *)&fifosize, 0, sizeof(fifosize));
  3996. status = api->halmac_get_hw_value(halmac, HALMAC_HW_RQPN_MAPPING, &rqpn);
  3997. if (status != HALMAC_RET_SUCCESS)
  3998. return -1;
  3999. status = api->halmac_get_hw_value(halmac, HALMAC_HW_TXFF_ALLOCATION, &fifosize);
  4000. if (status != HALMAC_RET_SUCCESS)
  4001. return -1;
  4002. for (i = 0; i < HW_QUEUE_ENTRY; i++) {
  4003. hmpriv->txpage[i] = 0;
  4004. /* Driver index mapping to HALMAC DMA queue */
  4005. dmaqueue = HALMAC_DMA_MAPPING_UNDEFINE;
  4006. switch (i) {
  4007. case VO_QUEUE_INX:
  4008. dmaqueue = rqpn.dma_map_vo;
  4009. break;
  4010. case VI_QUEUE_INX:
  4011. dmaqueue = rqpn.dma_map_vi;
  4012. break;
  4013. case BE_QUEUE_INX:
  4014. dmaqueue = rqpn.dma_map_be;
  4015. break;
  4016. case BK_QUEUE_INX:
  4017. dmaqueue = rqpn.dma_map_bk;
  4018. break;
  4019. case MGT_QUEUE_INX:
  4020. dmaqueue = rqpn.dma_map_mg;
  4021. break;
  4022. case HIGH_QUEUE_INX:
  4023. dmaqueue = rqpn.dma_map_hi;
  4024. break;
  4025. case BCN_QUEUE_INX:
  4026. case TXCMD_QUEUE_INX:
  4027. /* Unlimited */
  4028. hmpriv->txpage[i] = 0xFFFF;
  4029. continue;
  4030. }
  4031. switch (dmaqueue) {
  4032. case HALMAC_DMA_MAPPING_EXTRA:
  4033. hmpriv->txpage[i] = fifosize.extra_queue_pg_num;
  4034. break;
  4035. case HALMAC_DMA_MAPPING_LOW:
  4036. hmpriv->txpage[i] = fifosize.low_queue_pg_num;
  4037. break;
  4038. case HALMAC_DMA_MAPPING_NORMAL:
  4039. hmpriv->txpage[i] = fifosize.normal_queue_pg_num;
  4040. break;
  4041. case HALMAC_DMA_MAPPING_HIGH:
  4042. hmpriv->txpage[i] = fifosize.high_queue_pg_num;
  4043. break;
  4044. case HALMAC_DMA_MAPPING_UNDEFINE:
  4045. break;
  4046. }
  4047. hmpriv->txpage[i] += fifosize.pub_queue_pg_num;
  4048. }
  4049. return 0;
  4050. }
  4051. /*
  4052. * Description:
  4053. * Get specific queue allocated page number
  4054. *
  4055. * Parameter:
  4056. * d pointer to struct dvobj_priv of driver
  4057. * queue target queue to query, VO/VI/BE/BK/.../TXCMD_QUEUE_INX
  4058. * page return allocated page number
  4059. *
  4060. * Rteurn:
  4061. * 0 Success, "page" is valid.
  4062. * others Fail, "page" is invalid.
  4063. */
  4064. int rtw_halmac_get_tx_queue_page_num(struct dvobj_priv *d, u8 queue, u32 *page)
  4065. {
  4066. *page = 0;
  4067. if (queue < HW_QUEUE_ENTRY)
  4068. *page = d->hmpriv.txpage[queue];
  4069. return 0;
  4070. }
  4071. /*
  4072. * Return:
  4073. * address for SDIO command
  4074. */
  4075. u32 rtw_halmac_sdio_get_tx_addr(struct dvobj_priv *d, u8 *desc, u32 size)
  4076. {
  4077. struct halmac_adapter *mac;
  4078. struct halmac_api *api;
  4079. enum halmac_ret_status status;
  4080. u32 addr;
  4081. mac = dvobj_to_halmac(d);
  4082. api = HALMAC_GET_API(mac);
  4083. status = api->halmac_get_sdio_tx_addr(mac, desc, size, &addr);
  4084. if (HALMAC_RET_SUCCESS != status)
  4085. return 0;
  4086. return addr;
  4087. }
  4088. int rtw_halmac_sdio_tx_allowed(struct dvobj_priv *d, u8 *buf, u32 size)
  4089. {
  4090. struct halmac_adapter *mac;
  4091. struct halmac_api *api;
  4092. enum halmac_ret_status status;
  4093. mac = dvobj_to_halmac(d);
  4094. api = HALMAC_GET_API(mac);
  4095. status = api->halmac_tx_allowed_sdio(mac, buf, size);
  4096. if (HALMAC_RET_SUCCESS != status)
  4097. return -1;
  4098. return 0;
  4099. }
  4100. u32 rtw_halmac_sdio_get_rx_addr(struct dvobj_priv *d, u8 *seq)
  4101. {
  4102. u8 id;
  4103. #define RTW_SDIO_ADDR_RX_RX0FF_PRFIX 0x0E000
  4104. #define RTW_SDIO_ADDR_RX_RX0FF_GEN(a) (RTW_SDIO_ADDR_RX_RX0FF_PRFIX|(a&0x3))
  4105. id = *seq;
  4106. (*seq)++;
  4107. return RTW_SDIO_ADDR_RX_RX0FF_GEN(id);
  4108. }
  4109. #endif /* CONFIG_SDIO_HCI */
  4110. #ifdef CONFIG_USB_HCI
  4111. u8 rtw_halmac_usb_get_bulkout_id(struct dvobj_priv *d, u8 *buf, u32 size)
  4112. {
  4113. struct halmac_adapter *mac;
  4114. struct halmac_api *api;
  4115. enum halmac_ret_status status;
  4116. u8 bulkout_id;
  4117. mac = dvobj_to_halmac(d);
  4118. api = HALMAC_GET_API(mac);
  4119. status = api->halmac_get_usb_bulkout_id(mac, buf, size, &bulkout_id);
  4120. if (HALMAC_RET_SUCCESS != status)
  4121. return 0;
  4122. return bulkout_id;
  4123. }
  4124. /**
  4125. * rtw_halmac_usb_get_txagg_desc_num() - MAX descriptor number in one bulk for TX
  4126. * @d: struct dvobj_priv*
  4127. * @size: TX FIFO size, unit is byte.
  4128. *
  4129. * Get MAX descriptor number in one bulk out from HALMAC.
  4130. *
  4131. * Rteurn 0 for OK, otherwise fail.
  4132. */
  4133. int rtw_halmac_usb_get_txagg_desc_num(struct dvobj_priv *d, u8 *num)
  4134. {
  4135. struct halmac_adapter *halmac;
  4136. struct halmac_api *api;
  4137. enum halmac_ret_status status;
  4138. u8 val = 0;
  4139. halmac = dvobj_to_halmac(d);
  4140. api = HALMAC_GET_API(halmac);
  4141. status = api->halmac_get_hw_value(halmac, HALMAC_HW_USB_TXAGG_DESC_NUM, &val);
  4142. if (status != HALMAC_RET_SUCCESS)
  4143. return -1;
  4144. *num = val;
  4145. return 0;
  4146. }
  4147. static inline enum halmac_usb_mode _usb_mode_drv2halmac(enum RTW_USB_SPEED usb_mode)
  4148. {
  4149. enum halmac_usb_mode halmac_usb_mode = HALMAC_USB_MODE_U2;
  4150. switch (usb_mode) {
  4151. case RTW_USB_SPEED_2:
  4152. halmac_usb_mode = HALMAC_USB_MODE_U2;
  4153. break;
  4154. case RTW_USB_SPEED_3:
  4155. halmac_usb_mode = HALMAC_USB_MODE_U3;
  4156. break;
  4157. default:
  4158. halmac_usb_mode = HALMAC_USB_MODE_U2;
  4159. break;
  4160. }
  4161. return halmac_usb_mode;
  4162. }
  4163. u8 rtw_halmac_switch_usb_mode(struct dvobj_priv *d, enum RTW_USB_SPEED usb_mode)
  4164. {
  4165. PADAPTER adapter;
  4166. struct halmac_adapter *mac;
  4167. struct halmac_api *api;
  4168. enum halmac_ret_status status;
  4169. enum halmac_usb_mode halmac_usb_mode;
  4170. adapter = dvobj_get_primary_adapter(d);
  4171. mac = dvobj_to_halmac(d);
  4172. api = HALMAC_GET_API(mac);
  4173. halmac_usb_mode = _usb_mode_drv2halmac(usb_mode);
  4174. status = api->halmac_set_hw_value(mac, HALMAC_HW_USB_MODE, (void *)&halmac_usb_mode);
  4175. if (HALMAC_RET_SUCCESS != status)
  4176. return _FAIL;
  4177. return _SUCCESS;
  4178. }
  4179. #endif /* CONFIG_USB_HCI */
  4180. #ifdef CONFIG_BEAMFORMING
  4181. #ifdef RTW_BEAMFORMING_VERSION_2
  4182. int rtw_halmac_bf_add_mu_bfer(struct dvobj_priv *d, u16 paid, u16 csi_para,
  4183. u16 my_aid, enum halmac_csi_seg_len sel, u8 *addr)
  4184. {
  4185. struct halmac_adapter *mac;
  4186. struct halmac_api *api;
  4187. enum halmac_ret_status status;
  4188. struct halmac_mu_bfer_init_para param;
  4189. mac = dvobj_to_halmac(d);
  4190. api = HALMAC_GET_API(mac);
  4191. _rtw_memset(&param, 0, sizeof(param));
  4192. param.paid = paid;
  4193. param.csi_para = csi_para;
  4194. param.my_aid = my_aid;
  4195. param.csi_length_sel = sel;
  4196. _rtw_memcpy(param.bfer_address.addr, addr, 6);
  4197. status = api->halmac_mu_bfer_entry_init(mac, &param);
  4198. if (status != HALMAC_RET_SUCCESS)
  4199. return -1;
  4200. return 0;
  4201. }
  4202. int rtw_halmac_bf_del_mu_bfer(struct dvobj_priv *d)
  4203. {
  4204. struct halmac_adapter *mac;
  4205. struct halmac_api *api;
  4206. enum halmac_ret_status status;
  4207. mac = dvobj_to_halmac(d);
  4208. api = HALMAC_GET_API(mac);
  4209. status = api->halmac_mu_bfer_entry_del(mac);
  4210. if (status != HALMAC_RET_SUCCESS)
  4211. return -1;
  4212. return 0;
  4213. }
  4214. int rtw_halmac_bf_cfg_sounding(struct dvobj_priv *d,
  4215. enum halmac_snd_role role, enum halmac_data_rate rate)
  4216. {
  4217. struct halmac_adapter *mac;
  4218. struct halmac_api *api;
  4219. enum halmac_ret_status status;
  4220. mac = dvobj_to_halmac(d);
  4221. api = HALMAC_GET_API(mac);
  4222. status = api->halmac_cfg_sounding(mac, role, rate);
  4223. if (status != HALMAC_RET_SUCCESS)
  4224. return -1;
  4225. return 0;
  4226. }
  4227. int rtw_halmac_bf_del_sounding(struct dvobj_priv *d,
  4228. enum halmac_snd_role role)
  4229. {
  4230. struct halmac_adapter *mac;
  4231. struct halmac_api *api;
  4232. enum halmac_ret_status status;
  4233. mac = dvobj_to_halmac(d);
  4234. api = HALMAC_GET_API(mac);
  4235. status = api->halmac_del_sounding(mac, role);
  4236. if (status != HALMAC_RET_SUCCESS)
  4237. return -1;
  4238. return 0;
  4239. }
  4240. int rtw_halmac_bf_cfg_csi_rate(struct dvobj_priv *d,
  4241. u8 rssi, u8 current_rate, u8 fixrate_en,
  4242. u8 *new_rate)
  4243. {
  4244. struct halmac_adapter *mac;
  4245. struct halmac_api *api;
  4246. enum halmac_ret_status status;
  4247. mac = dvobj_to_halmac(d);
  4248. api = HALMAC_GET_API(mac);
  4249. status = api->halmac_cfg_csi_rate(mac,
  4250. rssi, current_rate, fixrate_en, new_rate);
  4251. if (status != HALMAC_RET_SUCCESS)
  4252. return -1;
  4253. return 0;
  4254. }
  4255. int rtw_halmac_bf_cfg_mu_mimo(struct dvobj_priv *d, enum halmac_snd_role role,
  4256. u8 *sounding_sts, u16 grouping_bitmap, u8 mu_tx_en,
  4257. u32 *given_gid_tab, u32 *given_user_pos)
  4258. {
  4259. struct halmac_adapter *mac;
  4260. struct halmac_api *api;
  4261. enum halmac_ret_status status;
  4262. struct halmac_cfg_mumimo_para param;
  4263. mac = dvobj_to_halmac(d);
  4264. api = HALMAC_GET_API(mac);
  4265. _rtw_memset(&param, 0, sizeof(param));
  4266. param.role = role;
  4267. param.grouping_bitmap = grouping_bitmap;
  4268. param.mu_tx_en = mu_tx_en;
  4269. if (sounding_sts)
  4270. _rtw_memcpy(param.sounding_sts, sounding_sts, 6);
  4271. if (given_gid_tab)
  4272. _rtw_memcpy(param.given_gid_tab, given_gid_tab, 8);
  4273. if (given_user_pos)
  4274. _rtw_memcpy(param.given_user_pos, given_user_pos, 16);
  4275. status = api->halmac_cfg_mumimo(mac, &param);
  4276. if (status != HALMAC_RET_SUCCESS)
  4277. return -1;
  4278. return 0;
  4279. }
  4280. #endif /* RTW_BEAMFORMING_VERSION_2 */
  4281. #endif /* CONFIG_BEAMFORMING */