lio_main.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more details.
  17. ***********************************************************************/
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pci.h>
  21. #include <linux/firmware.h>
  22. #include <net/vxlan.h>
  23. #include <linux/kthread.h>
  24. #include "liquidio_common.h"
  25. #include "octeon_droq.h"
  26. #include "octeon_iq.h"
  27. #include "response_manager.h"
  28. #include "octeon_device.h"
  29. #include "octeon_nic.h"
  30. #include "octeon_main.h"
  31. #include "octeon_network.h"
  32. #include "cn66xx_regs.h"
  33. #include "cn66xx_device.h"
  34. #include "cn68xx_device.h"
  35. #include "cn23xx_pf_device.h"
  36. #include "liquidio_image.h"
  37. MODULE_AUTHOR("Cavium Networks, <support@cavium.com>");
  38. MODULE_DESCRIPTION("Cavium LiquidIO Intelligent Server Adapter Driver");
  39. MODULE_LICENSE("GPL");
  40. MODULE_VERSION(LIQUIDIO_VERSION);
  41. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210SV_NAME LIO_FW_NAME_SUFFIX);
  42. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_210NV_NAME LIO_FW_NAME_SUFFIX);
  43. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_410NV_NAME LIO_FW_NAME_SUFFIX);
  44. MODULE_FIRMWARE(LIO_FW_DIR LIO_FW_BASE_NAME LIO_23XX_NAME LIO_FW_NAME_SUFFIX);
  45. static int ddr_timeout = 10000;
  46. module_param(ddr_timeout, int, 0644);
  47. MODULE_PARM_DESC(ddr_timeout,
  48. "Number of milliseconds to wait for DDR initialization. 0 waits for ddr_timeout to be set to non-zero value before starting to check");
  49. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  50. static int debug = -1;
  51. module_param(debug, int, 0644);
  52. MODULE_PARM_DESC(debug, "NETIF_MSG debug bits");
  53. static char fw_type[LIO_MAX_FW_TYPE_LEN];
  54. module_param_string(fw_type, fw_type, sizeof(fw_type), 0000);
  55. MODULE_PARM_DESC(fw_type, "Type of firmware to be loaded. Default \"nic\"");
  56. static int ptp_enable = 1;
  57. /* Polling interval for determining when NIC application is alive */
  58. #define LIQUIDIO_STARTER_POLL_INTERVAL_MS 100
  59. /* runtime link query interval */
  60. #define LIQUIDIO_LINK_QUERY_INTERVAL_MS 1000
  61. struct liquidio_if_cfg_context {
  62. int octeon_id;
  63. wait_queue_head_t wc;
  64. int cond;
  65. };
  66. struct liquidio_if_cfg_resp {
  67. u64 rh;
  68. struct liquidio_if_cfg_info cfg_info;
  69. u64 status;
  70. };
  71. struct liquidio_rx_ctl_context {
  72. int octeon_id;
  73. wait_queue_head_t wc;
  74. int cond;
  75. };
  76. struct oct_link_status_resp {
  77. u64 rh;
  78. struct oct_link_info link_info;
  79. u64 status;
  80. };
  81. struct oct_timestamp_resp {
  82. u64 rh;
  83. u64 timestamp;
  84. u64 status;
  85. };
  86. #define OCT_TIMESTAMP_RESP_SIZE (sizeof(struct oct_timestamp_resp))
  87. union tx_info {
  88. u64 u64;
  89. struct {
  90. #ifdef __BIG_ENDIAN_BITFIELD
  91. u16 gso_size;
  92. u16 gso_segs;
  93. u32 reserved;
  94. #else
  95. u32 reserved;
  96. u16 gso_segs;
  97. u16 gso_size;
  98. #endif
  99. } s;
  100. };
  101. /** Octeon device properties to be used by the NIC module.
  102. * Each octeon device in the system will be represented
  103. * by this structure in the NIC module.
  104. */
  105. #define OCTNIC_MAX_SG (MAX_SKB_FRAGS)
  106. #define OCTNIC_GSO_MAX_HEADER_SIZE 128
  107. #define OCTNIC_GSO_MAX_SIZE \
  108. (CN23XX_DEFAULT_INPUT_JABBER - OCTNIC_GSO_MAX_HEADER_SIZE)
  109. /** Structure of a node in list of gather components maintained by
  110. * NIC driver for each network device.
  111. */
  112. struct octnic_gather {
  113. /** List manipulation. Next and prev pointers. */
  114. struct list_head list;
  115. /** Size of the gather component at sg in bytes. */
  116. int sg_size;
  117. /** Number of bytes that sg was adjusted to make it 8B-aligned. */
  118. int adjust;
  119. /** Gather component that can accommodate max sized fragment list
  120. * received from the IP layer.
  121. */
  122. struct octeon_sg_entry *sg;
  123. dma_addr_t sg_dma_ptr;
  124. };
  125. struct handshake {
  126. struct completion init;
  127. struct completion started;
  128. struct pci_dev *pci_dev;
  129. int init_ok;
  130. int started_ok;
  131. };
  132. struct octeon_device_priv {
  133. /** Tasklet structures for this device. */
  134. struct tasklet_struct droq_tasklet;
  135. unsigned long napi_mask;
  136. };
  137. #ifdef CONFIG_PCI_IOV
  138. static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs);
  139. #endif
  140. static int octeon_device_init(struct octeon_device *);
  141. static int liquidio_stop(struct net_device *netdev);
  142. static void liquidio_remove(struct pci_dev *pdev);
  143. static int liquidio_probe(struct pci_dev *pdev,
  144. const struct pci_device_id *ent);
  145. static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
  146. int linkstate);
  147. static struct handshake handshake[MAX_OCTEON_DEVICES];
  148. static struct completion first_stage;
  149. static void octeon_droq_bh(unsigned long pdev)
  150. {
  151. int q_no;
  152. int reschedule = 0;
  153. struct octeon_device *oct = (struct octeon_device *)pdev;
  154. struct octeon_device_priv *oct_priv =
  155. (struct octeon_device_priv *)oct->priv;
  156. for (q_no = 0; q_no < MAX_OCTEON_OUTPUT_QUEUES(oct); q_no++) {
  157. if (!(oct->io_qmask.oq & BIT_ULL(q_no)))
  158. continue;
  159. reschedule |= octeon_droq_process_packets(oct, oct->droq[q_no],
  160. MAX_PACKET_BUDGET);
  161. lio_enable_irq(oct->droq[q_no], NULL);
  162. if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
  163. /* set time and cnt interrupt thresholds for this DROQ
  164. * for NAPI
  165. */
  166. int adjusted_q_no = q_no + oct->sriov_info.pf_srn;
  167. octeon_write_csr64(
  168. oct, CN23XX_SLI_OQ_PKT_INT_LEVELS(adjusted_q_no),
  169. 0x5700000040ULL);
  170. octeon_write_csr64(
  171. oct, CN23XX_SLI_OQ_PKTS_SENT(adjusted_q_no), 0);
  172. }
  173. }
  174. if (reschedule)
  175. tasklet_schedule(&oct_priv->droq_tasklet);
  176. }
  177. static int lio_wait_for_oq_pkts(struct octeon_device *oct)
  178. {
  179. struct octeon_device_priv *oct_priv =
  180. (struct octeon_device_priv *)oct->priv;
  181. int retry = 100, pkt_cnt = 0, pending_pkts = 0;
  182. int i;
  183. do {
  184. pending_pkts = 0;
  185. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
  186. if (!(oct->io_qmask.oq & BIT_ULL(i)))
  187. continue;
  188. pkt_cnt += octeon_droq_check_hw_for_pkts(oct->droq[i]);
  189. }
  190. if (pkt_cnt > 0) {
  191. pending_pkts += pkt_cnt;
  192. tasklet_schedule(&oct_priv->droq_tasklet);
  193. }
  194. pkt_cnt = 0;
  195. schedule_timeout_uninterruptible(1);
  196. } while (retry-- && pending_pkts);
  197. return pkt_cnt;
  198. }
  199. /**
  200. * \brief Forces all IO queues off on a given device
  201. * @param oct Pointer to Octeon device
  202. */
  203. static void force_io_queues_off(struct octeon_device *oct)
  204. {
  205. if ((oct->chip_id == OCTEON_CN66XX) ||
  206. (oct->chip_id == OCTEON_CN68XX)) {
  207. /* Reset the Enable bits for Input Queues. */
  208. octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, 0);
  209. /* Reset the Enable bits for Output Queues. */
  210. octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, 0);
  211. }
  212. }
  213. /**
  214. * \brief wait for all pending requests to complete
  215. * @param oct Pointer to Octeon device
  216. *
  217. * Called during shutdown sequence
  218. */
  219. static int wait_for_pending_requests(struct octeon_device *oct)
  220. {
  221. int i, pcount = 0;
  222. for (i = 0; i < 100; i++) {
  223. pcount =
  224. atomic_read(&oct->response_list
  225. [OCTEON_ORDERED_SC_LIST].pending_req_count);
  226. if (pcount)
  227. schedule_timeout_uninterruptible(HZ / 10);
  228. else
  229. break;
  230. }
  231. if (pcount)
  232. return 1;
  233. return 0;
  234. }
  235. /**
  236. * \brief Cause device to go quiet so it can be safely removed/reset/etc
  237. * @param oct Pointer to Octeon device
  238. */
  239. static inline void pcierror_quiesce_device(struct octeon_device *oct)
  240. {
  241. int i;
  242. /* Disable the input and output queues now. No more packets will
  243. * arrive from Octeon, but we should wait for all packet processing
  244. * to finish.
  245. */
  246. force_io_queues_off(oct);
  247. /* To allow for in-flight requests */
  248. schedule_timeout_uninterruptible(100);
  249. if (wait_for_pending_requests(oct))
  250. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  251. /* Force all requests waiting to be fetched by OCTEON to complete. */
  252. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  253. struct octeon_instr_queue *iq;
  254. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  255. continue;
  256. iq = oct->instr_queue[i];
  257. if (atomic_read(&iq->instr_pending)) {
  258. spin_lock_bh(&iq->lock);
  259. iq->fill_cnt = 0;
  260. iq->octeon_read_index = iq->host_write_index;
  261. iq->stats.instr_processed +=
  262. atomic_read(&iq->instr_pending);
  263. lio_process_iq_request_list(oct, iq, 0);
  264. spin_unlock_bh(&iq->lock);
  265. }
  266. }
  267. /* Force all pending ordered list requests to time out. */
  268. lio_process_ordered_list(oct, 1);
  269. /* We do not need to wait for output queue packets to be processed. */
  270. }
  271. /**
  272. * \brief Cleanup PCI AER uncorrectable error status
  273. * @param dev Pointer to PCI device
  274. */
  275. static void cleanup_aer_uncorrect_error_status(struct pci_dev *dev)
  276. {
  277. int pos = 0x100;
  278. u32 status, mask;
  279. pr_info("%s :\n", __func__);
  280. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, &status);
  281. pci_read_config_dword(dev, pos + PCI_ERR_UNCOR_SEVER, &mask);
  282. if (dev->error_state == pci_channel_io_normal)
  283. status &= ~mask; /* Clear corresponding nonfatal bits */
  284. else
  285. status &= mask; /* Clear corresponding fatal bits */
  286. pci_write_config_dword(dev, pos + PCI_ERR_UNCOR_STATUS, status);
  287. }
  288. /**
  289. * \brief Stop all PCI IO to a given device
  290. * @param dev Pointer to Octeon device
  291. */
  292. static void stop_pci_io(struct octeon_device *oct)
  293. {
  294. /* No more instructions will be forwarded. */
  295. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  296. pci_disable_device(oct->pci_dev);
  297. /* Disable interrupts */
  298. oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
  299. pcierror_quiesce_device(oct);
  300. /* Release the interrupt line */
  301. free_irq(oct->pci_dev->irq, oct);
  302. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  303. pci_disable_msi(oct->pci_dev);
  304. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  305. lio_get_state_string(&oct->status));
  306. /* making it a common function for all OCTEON models */
  307. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  308. }
  309. /**
  310. * \brief called when PCI error is detected
  311. * @param pdev Pointer to PCI device
  312. * @param state The current pci connection state
  313. *
  314. * This function is called after a PCI bus error affecting
  315. * this device has been detected.
  316. */
  317. static pci_ers_result_t liquidio_pcie_error_detected(struct pci_dev *pdev,
  318. pci_channel_state_t state)
  319. {
  320. struct octeon_device *oct = pci_get_drvdata(pdev);
  321. /* Non-correctable Non-fatal errors */
  322. if (state == pci_channel_io_normal) {
  323. dev_err(&oct->pci_dev->dev, "Non-correctable non-fatal error reported:\n");
  324. cleanup_aer_uncorrect_error_status(oct->pci_dev);
  325. return PCI_ERS_RESULT_CAN_RECOVER;
  326. }
  327. /* Non-correctable Fatal errors */
  328. dev_err(&oct->pci_dev->dev, "Non-correctable FATAL reported by PCI AER driver\n");
  329. stop_pci_io(oct);
  330. /* Always return a DISCONNECT. There is no support for recovery but only
  331. * for a clean shutdown.
  332. */
  333. return PCI_ERS_RESULT_DISCONNECT;
  334. }
  335. /**
  336. * \brief mmio handler
  337. * @param pdev Pointer to PCI device
  338. */
  339. static pci_ers_result_t liquidio_pcie_mmio_enabled(
  340. struct pci_dev *pdev __attribute__((unused)))
  341. {
  342. /* We should never hit this since we never ask for a reset for a Fatal
  343. * Error. We always return DISCONNECT in io_error above.
  344. * But play safe and return RECOVERED for now.
  345. */
  346. return PCI_ERS_RESULT_RECOVERED;
  347. }
  348. /**
  349. * \brief called after the pci bus has been reset.
  350. * @param pdev Pointer to PCI device
  351. *
  352. * Restart the card from scratch, as if from a cold-boot. Implementation
  353. * resembles the first-half of the octeon_resume routine.
  354. */
  355. static pci_ers_result_t liquidio_pcie_slot_reset(
  356. struct pci_dev *pdev __attribute__((unused)))
  357. {
  358. /* We should never hit this since we never ask for a reset for a Fatal
  359. * Error. We always return DISCONNECT in io_error above.
  360. * But play safe and return RECOVERED for now.
  361. */
  362. return PCI_ERS_RESULT_RECOVERED;
  363. }
  364. /**
  365. * \brief called when traffic can start flowing again.
  366. * @param pdev Pointer to PCI device
  367. *
  368. * This callback is called when the error recovery driver tells us that
  369. * its OK to resume normal operation. Implementation resembles the
  370. * second-half of the octeon_resume routine.
  371. */
  372. static void liquidio_pcie_resume(struct pci_dev *pdev __attribute__((unused)))
  373. {
  374. /* Nothing to be done here. */
  375. }
  376. #ifdef CONFIG_PM
  377. /**
  378. * \brief called when suspending
  379. * @param pdev Pointer to PCI device
  380. * @param state state to suspend to
  381. */
  382. static int liquidio_suspend(struct pci_dev *pdev __attribute__((unused)),
  383. pm_message_t state __attribute__((unused)))
  384. {
  385. return 0;
  386. }
  387. /**
  388. * \brief called when resuming
  389. * @param pdev Pointer to PCI device
  390. */
  391. static int liquidio_resume(struct pci_dev *pdev __attribute__((unused)))
  392. {
  393. return 0;
  394. }
  395. #endif
  396. /* For PCI-E Advanced Error Recovery (AER) Interface */
  397. static const struct pci_error_handlers liquidio_err_handler = {
  398. .error_detected = liquidio_pcie_error_detected,
  399. .mmio_enabled = liquidio_pcie_mmio_enabled,
  400. .slot_reset = liquidio_pcie_slot_reset,
  401. .resume = liquidio_pcie_resume,
  402. };
  403. static const struct pci_device_id liquidio_pci_tbl[] = {
  404. { /* 68xx */
  405. PCI_VENDOR_ID_CAVIUM, 0x91, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  406. },
  407. { /* 66xx */
  408. PCI_VENDOR_ID_CAVIUM, 0x92, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  409. },
  410. { /* 23xx pf */
  411. PCI_VENDOR_ID_CAVIUM, 0x9702, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0
  412. },
  413. {
  414. 0, 0, 0, 0, 0, 0, 0
  415. }
  416. };
  417. MODULE_DEVICE_TABLE(pci, liquidio_pci_tbl);
  418. static struct pci_driver liquidio_pci_driver = {
  419. .name = "LiquidIO",
  420. .id_table = liquidio_pci_tbl,
  421. .probe = liquidio_probe,
  422. .remove = liquidio_remove,
  423. .err_handler = &liquidio_err_handler, /* For AER */
  424. #ifdef CONFIG_PM
  425. .suspend = liquidio_suspend,
  426. .resume = liquidio_resume,
  427. #endif
  428. #ifdef CONFIG_PCI_IOV
  429. .sriov_configure = liquidio_enable_sriov,
  430. #endif
  431. };
  432. /**
  433. * \brief register PCI driver
  434. */
  435. static int liquidio_init_pci(void)
  436. {
  437. return pci_register_driver(&liquidio_pci_driver);
  438. }
  439. /**
  440. * \brief unregister PCI driver
  441. */
  442. static void liquidio_deinit_pci(void)
  443. {
  444. pci_unregister_driver(&liquidio_pci_driver);
  445. }
  446. /**
  447. * \brief Stop Tx queues
  448. * @param netdev network device
  449. */
  450. static inline void txqs_stop(struct net_device *netdev)
  451. {
  452. if (netif_is_multiqueue(netdev)) {
  453. int i;
  454. for (i = 0; i < netdev->num_tx_queues; i++)
  455. netif_stop_subqueue(netdev, i);
  456. } else {
  457. netif_stop_queue(netdev);
  458. }
  459. }
  460. /**
  461. * \brief Start Tx queues
  462. * @param netdev network device
  463. */
  464. static inline void txqs_start(struct net_device *netdev)
  465. {
  466. if (netif_is_multiqueue(netdev)) {
  467. int i;
  468. for (i = 0; i < netdev->num_tx_queues; i++)
  469. netif_start_subqueue(netdev, i);
  470. } else {
  471. netif_start_queue(netdev);
  472. }
  473. }
  474. /**
  475. * \brief Wake Tx queues
  476. * @param netdev network device
  477. */
  478. static inline void txqs_wake(struct net_device *netdev)
  479. {
  480. struct lio *lio = GET_LIO(netdev);
  481. if (netif_is_multiqueue(netdev)) {
  482. int i;
  483. for (i = 0; i < netdev->num_tx_queues; i++) {
  484. int qno = lio->linfo.txpciq[i %
  485. (lio->linfo.num_txpciq)].s.q_no;
  486. if (__netif_subqueue_stopped(netdev, i)) {
  487. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, qno,
  488. tx_restart, 1);
  489. netif_wake_subqueue(netdev, i);
  490. }
  491. }
  492. } else {
  493. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
  494. tx_restart, 1);
  495. netif_wake_queue(netdev);
  496. }
  497. }
  498. /**
  499. * \brief Stop Tx queue
  500. * @param netdev network device
  501. */
  502. static void stop_txq(struct net_device *netdev)
  503. {
  504. txqs_stop(netdev);
  505. }
  506. /**
  507. * \brief Start Tx queue
  508. * @param netdev network device
  509. */
  510. static void start_txq(struct net_device *netdev)
  511. {
  512. struct lio *lio = GET_LIO(netdev);
  513. if (lio->linfo.link.s.link_up) {
  514. txqs_start(netdev);
  515. return;
  516. }
  517. }
  518. /**
  519. * \brief Wake a queue
  520. * @param netdev network device
  521. * @param q which queue to wake
  522. */
  523. static inline void wake_q(struct net_device *netdev, int q)
  524. {
  525. if (netif_is_multiqueue(netdev))
  526. netif_wake_subqueue(netdev, q);
  527. else
  528. netif_wake_queue(netdev);
  529. }
  530. /**
  531. * \brief Stop a queue
  532. * @param netdev network device
  533. * @param q which queue to stop
  534. */
  535. static inline void stop_q(struct net_device *netdev, int q)
  536. {
  537. if (netif_is_multiqueue(netdev))
  538. netif_stop_subqueue(netdev, q);
  539. else
  540. netif_stop_queue(netdev);
  541. }
  542. /**
  543. * \brief Check Tx queue status, and take appropriate action
  544. * @param lio per-network private data
  545. * @returns 0 if full, number of queues woken up otherwise
  546. */
  547. static inline int check_txq_status(struct lio *lio)
  548. {
  549. int ret_val = 0;
  550. if (netif_is_multiqueue(lio->netdev)) {
  551. int numqs = lio->netdev->num_tx_queues;
  552. int q, iq = 0;
  553. /* check each sub-queue state */
  554. for (q = 0; q < numqs; q++) {
  555. iq = lio->linfo.txpciq[q %
  556. (lio->linfo.num_txpciq)].s.q_no;
  557. if (octnet_iq_is_full(lio->oct_dev, iq))
  558. continue;
  559. if (__netif_subqueue_stopped(lio->netdev, q)) {
  560. wake_q(lio->netdev, q);
  561. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq,
  562. tx_restart, 1);
  563. ret_val++;
  564. }
  565. }
  566. } else {
  567. if (octnet_iq_is_full(lio->oct_dev, lio->txq))
  568. return 0;
  569. wake_q(lio->netdev, lio->txq);
  570. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, lio->txq,
  571. tx_restart, 1);
  572. ret_val = 1;
  573. }
  574. return ret_val;
  575. }
  576. /**
  577. * Remove the node at the head of the list. The list would be empty at
  578. * the end of this call if there are no more nodes in the list.
  579. */
  580. static inline struct list_head *list_delete_head(struct list_head *root)
  581. {
  582. struct list_head *node;
  583. if ((root->prev == root) && (root->next == root))
  584. node = NULL;
  585. else
  586. node = root->next;
  587. if (node)
  588. list_del(node);
  589. return node;
  590. }
  591. /**
  592. * \brief Delete gather lists
  593. * @param lio per-network private data
  594. */
  595. static void delete_glists(struct lio *lio)
  596. {
  597. struct octnic_gather *g;
  598. int i;
  599. kfree(lio->glist_lock);
  600. lio->glist_lock = NULL;
  601. if (!lio->glist)
  602. return;
  603. for (i = 0; i < lio->linfo.num_txpciq; i++) {
  604. do {
  605. g = (struct octnic_gather *)
  606. list_delete_head(&lio->glist[i]);
  607. if (g)
  608. kfree(g);
  609. } while (g);
  610. if (lio->glists_virt_base && lio->glists_virt_base[i] &&
  611. lio->glists_dma_base && lio->glists_dma_base[i]) {
  612. lio_dma_free(lio->oct_dev,
  613. lio->glist_entry_size * lio->tx_qsize,
  614. lio->glists_virt_base[i],
  615. lio->glists_dma_base[i]);
  616. }
  617. }
  618. kfree(lio->glists_virt_base);
  619. lio->glists_virt_base = NULL;
  620. kfree(lio->glists_dma_base);
  621. lio->glists_dma_base = NULL;
  622. kfree(lio->glist);
  623. lio->glist = NULL;
  624. }
  625. /**
  626. * \brief Setup gather lists
  627. * @param lio per-network private data
  628. */
  629. static int setup_glists(struct octeon_device *oct, struct lio *lio, int num_iqs)
  630. {
  631. int i, j;
  632. struct octnic_gather *g;
  633. lio->glist_lock = kcalloc(num_iqs, sizeof(*lio->glist_lock),
  634. GFP_KERNEL);
  635. if (!lio->glist_lock)
  636. return -ENOMEM;
  637. lio->glist = kcalloc(num_iqs, sizeof(*lio->glist),
  638. GFP_KERNEL);
  639. if (!lio->glist) {
  640. kfree(lio->glist_lock);
  641. lio->glist_lock = NULL;
  642. return -ENOMEM;
  643. }
  644. lio->glist_entry_size =
  645. ROUNDUP8((ROUNDUP4(OCTNIC_MAX_SG) >> 2) * OCT_SG_ENTRY_SIZE);
  646. /* allocate memory to store virtual and dma base address of
  647. * per glist consistent memory
  648. */
  649. lio->glists_virt_base = kcalloc(num_iqs, sizeof(*lio->glists_virt_base),
  650. GFP_KERNEL);
  651. lio->glists_dma_base = kcalloc(num_iqs, sizeof(*lio->glists_dma_base),
  652. GFP_KERNEL);
  653. if (!lio->glists_virt_base || !lio->glists_dma_base) {
  654. delete_glists(lio);
  655. return -ENOMEM;
  656. }
  657. for (i = 0; i < num_iqs; i++) {
  658. int numa_node = dev_to_node(&oct->pci_dev->dev);
  659. spin_lock_init(&lio->glist_lock[i]);
  660. INIT_LIST_HEAD(&lio->glist[i]);
  661. lio->glists_virt_base[i] =
  662. lio_dma_alloc(oct,
  663. lio->glist_entry_size * lio->tx_qsize,
  664. &lio->glists_dma_base[i]);
  665. if (!lio->glists_virt_base[i]) {
  666. delete_glists(lio);
  667. return -ENOMEM;
  668. }
  669. for (j = 0; j < lio->tx_qsize; j++) {
  670. g = kzalloc_node(sizeof(*g), GFP_KERNEL,
  671. numa_node);
  672. if (!g)
  673. g = kzalloc(sizeof(*g), GFP_KERNEL);
  674. if (!g)
  675. break;
  676. g->sg = lio->glists_virt_base[i] +
  677. (j * lio->glist_entry_size);
  678. g->sg_dma_ptr = lio->glists_dma_base[i] +
  679. (j * lio->glist_entry_size);
  680. list_add_tail(&g->list, &lio->glist[i]);
  681. }
  682. if (j != lio->tx_qsize) {
  683. delete_glists(lio);
  684. return -ENOMEM;
  685. }
  686. }
  687. return 0;
  688. }
  689. /**
  690. * \brief Print link information
  691. * @param netdev network device
  692. */
  693. static void print_link_info(struct net_device *netdev)
  694. {
  695. struct lio *lio = GET_LIO(netdev);
  696. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED) {
  697. struct oct_link_info *linfo = &lio->linfo;
  698. if (linfo->link.s.link_up) {
  699. netif_info(lio, link, lio->netdev, "%d Mbps %s Duplex UP\n",
  700. linfo->link.s.speed,
  701. (linfo->link.s.duplex) ? "Full" : "Half");
  702. } else {
  703. netif_info(lio, link, lio->netdev, "Link Down\n");
  704. }
  705. }
  706. }
  707. /**
  708. * \brief Routine to notify MTU change
  709. * @param work work_struct data structure
  710. */
  711. static void octnet_link_status_change(struct work_struct *work)
  712. {
  713. struct cavium_wk *wk = (struct cavium_wk *)work;
  714. struct lio *lio = (struct lio *)wk->ctxptr;
  715. rtnl_lock();
  716. call_netdevice_notifiers(NETDEV_CHANGEMTU, lio->netdev);
  717. rtnl_unlock();
  718. }
  719. /**
  720. * \brief Sets up the mtu status change work
  721. * @param netdev network device
  722. */
  723. static inline int setup_link_status_change_wq(struct net_device *netdev)
  724. {
  725. struct lio *lio = GET_LIO(netdev);
  726. struct octeon_device *oct = lio->oct_dev;
  727. lio->link_status_wq.wq = alloc_workqueue("link-status",
  728. WQ_MEM_RECLAIM, 0);
  729. if (!lio->link_status_wq.wq) {
  730. dev_err(&oct->pci_dev->dev, "unable to create cavium link status wq\n");
  731. return -1;
  732. }
  733. INIT_DELAYED_WORK(&lio->link_status_wq.wk.work,
  734. octnet_link_status_change);
  735. lio->link_status_wq.wk.ctxptr = lio;
  736. return 0;
  737. }
  738. static inline void cleanup_link_status_change_wq(struct net_device *netdev)
  739. {
  740. struct lio *lio = GET_LIO(netdev);
  741. if (lio->link_status_wq.wq) {
  742. cancel_delayed_work_sync(&lio->link_status_wq.wk.work);
  743. destroy_workqueue(lio->link_status_wq.wq);
  744. }
  745. }
  746. /**
  747. * \brief Update link status
  748. * @param netdev network device
  749. * @param ls link status structure
  750. *
  751. * Called on receipt of a link status response from the core application to
  752. * update each interface's link status.
  753. */
  754. static inline void update_link_status(struct net_device *netdev,
  755. union oct_link_status *ls)
  756. {
  757. struct lio *lio = GET_LIO(netdev);
  758. int changed = (lio->linfo.link.u64 != ls->u64);
  759. lio->linfo.link.u64 = ls->u64;
  760. if ((lio->intf_open) && (changed)) {
  761. print_link_info(netdev);
  762. lio->link_changes++;
  763. if (lio->linfo.link.s.link_up) {
  764. netif_carrier_on(netdev);
  765. txqs_wake(netdev);
  766. } else {
  767. netif_carrier_off(netdev);
  768. stop_txq(netdev);
  769. }
  770. }
  771. }
  772. /* Runs in interrupt context. */
  773. static void update_txq_status(struct octeon_device *oct, int iq_num)
  774. {
  775. struct net_device *netdev;
  776. struct lio *lio;
  777. struct octeon_instr_queue *iq = oct->instr_queue[iq_num];
  778. netdev = oct->props[iq->ifidx].netdev;
  779. /* This is needed because the first IQ does not have
  780. * a netdev associated with it.
  781. */
  782. if (!netdev)
  783. return;
  784. lio = GET_LIO(netdev);
  785. if (netif_is_multiqueue(netdev)) {
  786. if (__netif_subqueue_stopped(netdev, iq->q_index) &&
  787. lio->linfo.link.s.link_up &&
  788. (!octnet_iq_is_full(oct, iq_num))) {
  789. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq_num,
  790. tx_restart, 1);
  791. netif_wake_subqueue(netdev, iq->q_index);
  792. }
  793. } else if (netif_queue_stopped(netdev) &&
  794. lio->linfo.link.s.link_up &&
  795. (!octnet_iq_is_full(oct, lio->txq))) {
  796. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev,
  797. lio->txq, tx_restart, 1);
  798. netif_wake_queue(netdev);
  799. }
  800. }
  801. static
  802. int liquidio_schedule_msix_droq_pkt_handler(struct octeon_droq *droq, u64 ret)
  803. {
  804. struct octeon_device *oct = droq->oct_dev;
  805. struct octeon_device_priv *oct_priv =
  806. (struct octeon_device_priv *)oct->priv;
  807. if (droq->ops.poll_mode) {
  808. droq->ops.napi_fn(droq);
  809. } else {
  810. if (ret & MSIX_PO_INT) {
  811. tasklet_schedule(&oct_priv->droq_tasklet);
  812. return 1;
  813. }
  814. /* this will be flushed periodically by check iq db */
  815. if (ret & MSIX_PI_INT)
  816. return 0;
  817. }
  818. return 0;
  819. }
  820. /**
  821. * \brief Droq packet processor sceduler
  822. * @param oct octeon device
  823. */
  824. static void liquidio_schedule_droq_pkt_handlers(struct octeon_device *oct)
  825. {
  826. struct octeon_device_priv *oct_priv =
  827. (struct octeon_device_priv *)oct->priv;
  828. u64 oq_no;
  829. struct octeon_droq *droq;
  830. if (oct->int_status & OCT_DEV_INTR_PKT_DATA) {
  831. for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct);
  832. oq_no++) {
  833. if (!(oct->droq_intr & BIT_ULL(oq_no)))
  834. continue;
  835. droq = oct->droq[oq_no];
  836. if (droq->ops.poll_mode) {
  837. droq->ops.napi_fn(droq);
  838. oct_priv->napi_mask |= (1 << oq_no);
  839. } else {
  840. tasklet_schedule(&oct_priv->droq_tasklet);
  841. }
  842. }
  843. }
  844. }
  845. static irqreturn_t
  846. liquidio_msix_intr_handler(int irq __attribute__((unused)), void *dev)
  847. {
  848. u64 ret;
  849. struct octeon_ioq_vector *ioq_vector = (struct octeon_ioq_vector *)dev;
  850. struct octeon_device *oct = ioq_vector->oct_dev;
  851. struct octeon_droq *droq = oct->droq[ioq_vector->droq_index];
  852. ret = oct->fn_list.msix_interrupt_handler(ioq_vector);
  853. if ((ret & MSIX_PO_INT) || (ret & MSIX_PI_INT))
  854. liquidio_schedule_msix_droq_pkt_handler(droq, ret);
  855. return IRQ_HANDLED;
  856. }
  857. /**
  858. * \brief Interrupt handler for octeon
  859. * @param irq unused
  860. * @param dev octeon device
  861. */
  862. static
  863. irqreturn_t liquidio_legacy_intr_handler(int irq __attribute__((unused)),
  864. void *dev)
  865. {
  866. struct octeon_device *oct = (struct octeon_device *)dev;
  867. irqreturn_t ret;
  868. /* Disable our interrupts for the duration of ISR */
  869. oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
  870. ret = oct->fn_list.process_interrupt_regs(oct);
  871. if (ret == IRQ_HANDLED)
  872. liquidio_schedule_droq_pkt_handlers(oct);
  873. /* Re-enable our interrupts */
  874. if (!(atomic_read(&oct->status) == OCT_DEV_IN_RESET))
  875. oct->fn_list.enable_interrupt(oct, OCTEON_ALL_INTR);
  876. return ret;
  877. }
  878. /**
  879. * \brief Setup interrupt for octeon device
  880. * @param oct octeon device
  881. *
  882. * Enable interrupt in Octeon device as given in the PCI interrupt mask.
  883. */
  884. static int octeon_setup_interrupt(struct octeon_device *oct)
  885. {
  886. int irqret, err;
  887. struct msix_entry *msix_entries;
  888. int i;
  889. int num_ioq_vectors;
  890. int num_alloc_ioq_vectors;
  891. char *queue_irq_names = NULL;
  892. char *aux_irq_name = NULL;
  893. if (OCTEON_CN23XX_PF(oct) && oct->msix_on) {
  894. oct->num_msix_irqs = oct->sriov_info.num_pf_rings;
  895. /* one non ioq interrupt for handling sli_mac_pf_int_sum */
  896. oct->num_msix_irqs += 1;
  897. /* allocate storage for the names assigned to each irq */
  898. oct->irq_name_storage =
  899. kcalloc((MAX_IOQ_INTERRUPTS_PER_PF + 1), INTRNAMSIZ,
  900. GFP_KERNEL);
  901. if (!oct->irq_name_storage) {
  902. dev_err(&oct->pci_dev->dev, "Irq name storage alloc failed...\n");
  903. return -ENOMEM;
  904. }
  905. queue_irq_names = oct->irq_name_storage;
  906. aux_irq_name = &queue_irq_names
  907. [IRQ_NAME_OFF(MAX_IOQ_INTERRUPTS_PER_PF)];
  908. oct->msix_entries = kcalloc(
  909. oct->num_msix_irqs, sizeof(struct msix_entry), GFP_KERNEL);
  910. if (!oct->msix_entries) {
  911. dev_err(&oct->pci_dev->dev, "Memory Alloc failed...\n");
  912. kfree(oct->irq_name_storage);
  913. oct->irq_name_storage = NULL;
  914. return -ENOMEM;
  915. }
  916. msix_entries = (struct msix_entry *)oct->msix_entries;
  917. /*Assumption is that pf msix vectors start from pf srn to pf to
  918. * trs and not from 0. if not change this code
  919. */
  920. for (i = 0; i < oct->num_msix_irqs - 1; i++)
  921. msix_entries[i].entry = oct->sriov_info.pf_srn + i;
  922. msix_entries[oct->num_msix_irqs - 1].entry =
  923. oct->sriov_info.trs;
  924. num_alloc_ioq_vectors = pci_enable_msix_range(
  925. oct->pci_dev, msix_entries,
  926. oct->num_msix_irqs,
  927. oct->num_msix_irqs);
  928. if (num_alloc_ioq_vectors < 0) {
  929. dev_err(&oct->pci_dev->dev, "unable to Allocate MSI-X interrupts\n");
  930. kfree(oct->msix_entries);
  931. oct->msix_entries = NULL;
  932. kfree(oct->irq_name_storage);
  933. oct->irq_name_storage = NULL;
  934. return num_alloc_ioq_vectors;
  935. }
  936. dev_dbg(&oct->pci_dev->dev, "OCTEON: Enough MSI-X interrupts are allocated...\n");
  937. num_ioq_vectors = oct->num_msix_irqs;
  938. /** For PF, there is one non-ioq interrupt handler */
  939. num_ioq_vectors -= 1;
  940. snprintf(aux_irq_name, INTRNAMSIZ,
  941. "LiquidIO%u-pf%u-aux", oct->octeon_id, oct->pf_num);
  942. irqret = request_irq(msix_entries[num_ioq_vectors].vector,
  943. liquidio_legacy_intr_handler, 0,
  944. aux_irq_name, oct);
  945. if (irqret) {
  946. dev_err(&oct->pci_dev->dev,
  947. "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
  948. irqret);
  949. pci_disable_msix(oct->pci_dev);
  950. kfree(oct->msix_entries);
  951. oct->msix_entries = NULL;
  952. kfree(oct->irq_name_storage);
  953. oct->irq_name_storage = NULL;
  954. return irqret;
  955. }
  956. for (i = 0; i < num_ioq_vectors; i++) {
  957. snprintf(&queue_irq_names[IRQ_NAME_OFF(i)], INTRNAMSIZ,
  958. "LiquidIO%u-pf%u-rxtx-%u",
  959. oct->octeon_id, oct->pf_num, i);
  960. irqret = request_irq(msix_entries[i].vector,
  961. liquidio_msix_intr_handler, 0,
  962. &queue_irq_names[IRQ_NAME_OFF(i)],
  963. &oct->ioq_vector[i]);
  964. if (irqret) {
  965. dev_err(&oct->pci_dev->dev,
  966. "OCTEON: Request_irq failed for MSIX interrupt Error: %d\n",
  967. irqret);
  968. /** Freeing the non-ioq irq vector here . */
  969. free_irq(msix_entries[num_ioq_vectors].vector,
  970. oct);
  971. while (i) {
  972. i--;
  973. /** clearing affinity mask. */
  974. irq_set_affinity_hint(
  975. msix_entries[i].vector, NULL);
  976. free_irq(msix_entries[i].vector,
  977. &oct->ioq_vector[i]);
  978. }
  979. pci_disable_msix(oct->pci_dev);
  980. kfree(oct->msix_entries);
  981. oct->msix_entries = NULL;
  982. kfree(oct->irq_name_storage);
  983. oct->irq_name_storage = NULL;
  984. return irqret;
  985. }
  986. oct->ioq_vector[i].vector = msix_entries[i].vector;
  987. /* assign the cpu mask for this msix interrupt vector */
  988. irq_set_affinity_hint(
  989. msix_entries[i].vector,
  990. (&oct->ioq_vector[i].affinity_mask));
  991. }
  992. dev_dbg(&oct->pci_dev->dev, "OCTEON[%d]: MSI-X enabled\n",
  993. oct->octeon_id);
  994. } else {
  995. err = pci_enable_msi(oct->pci_dev);
  996. if (err)
  997. dev_warn(&oct->pci_dev->dev, "Reverting to legacy interrupts. Error: %d\n",
  998. err);
  999. else
  1000. oct->flags |= LIO_FLAG_MSI_ENABLED;
  1001. /* allocate storage for the names assigned to the irq */
  1002. oct->irq_name_storage = kcalloc(1, INTRNAMSIZ, GFP_KERNEL);
  1003. if (!oct->irq_name_storage)
  1004. return -ENOMEM;
  1005. queue_irq_names = oct->irq_name_storage;
  1006. snprintf(&queue_irq_names[IRQ_NAME_OFF(0)], INTRNAMSIZ,
  1007. "LiquidIO%u-pf%u-rxtx-%u",
  1008. oct->octeon_id, oct->pf_num, 0);
  1009. irqret = request_irq(oct->pci_dev->irq,
  1010. liquidio_legacy_intr_handler,
  1011. IRQF_SHARED,
  1012. &queue_irq_names[IRQ_NAME_OFF(0)], oct);
  1013. if (irqret) {
  1014. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  1015. pci_disable_msi(oct->pci_dev);
  1016. dev_err(&oct->pci_dev->dev, "Request IRQ failed with code: %d\n",
  1017. irqret);
  1018. kfree(oct->irq_name_storage);
  1019. oct->irq_name_storage = NULL;
  1020. return irqret;
  1021. }
  1022. }
  1023. return 0;
  1024. }
  1025. static struct octeon_device *get_other_octeon_device(struct octeon_device *oct)
  1026. {
  1027. struct octeon_device *other_oct;
  1028. other_oct = lio_get_device(oct->octeon_id + 1);
  1029. if (other_oct && other_oct->pci_dev) {
  1030. int oct_busnum, other_oct_busnum;
  1031. oct_busnum = oct->pci_dev->bus->number;
  1032. other_oct_busnum = other_oct->pci_dev->bus->number;
  1033. if (oct_busnum == other_oct_busnum) {
  1034. int oct_slot, other_oct_slot;
  1035. oct_slot = PCI_SLOT(oct->pci_dev->devfn);
  1036. other_oct_slot = PCI_SLOT(other_oct->pci_dev->devfn);
  1037. if (oct_slot == other_oct_slot)
  1038. return other_oct;
  1039. }
  1040. }
  1041. return NULL;
  1042. }
  1043. static void disable_all_vf_links(struct octeon_device *oct)
  1044. {
  1045. struct net_device *netdev;
  1046. int max_vfs, vf, i;
  1047. if (!oct)
  1048. return;
  1049. max_vfs = oct->sriov_info.max_vfs;
  1050. for (i = 0; i < oct->ifcount; i++) {
  1051. netdev = oct->props[i].netdev;
  1052. if (!netdev)
  1053. continue;
  1054. for (vf = 0; vf < max_vfs; vf++)
  1055. liquidio_set_vf_link_state(netdev, vf,
  1056. IFLA_VF_LINK_STATE_DISABLE);
  1057. }
  1058. }
  1059. static int liquidio_watchdog(void *param)
  1060. {
  1061. bool err_msg_was_printed[LIO_MAX_CORES];
  1062. u16 mask_of_crashed_or_stuck_cores = 0;
  1063. bool all_vf_links_are_disabled = false;
  1064. struct octeon_device *oct = param;
  1065. struct octeon_device *other_oct;
  1066. #ifdef CONFIG_MODULE_UNLOAD
  1067. long refcount, vfs_referencing_pf;
  1068. u64 vfs_mask1, vfs_mask2;
  1069. #endif
  1070. int core;
  1071. memset(err_msg_was_printed, 0, sizeof(err_msg_was_printed));
  1072. while (!kthread_should_stop()) {
  1073. /* sleep for a couple of seconds so that we don't hog the CPU */
  1074. set_current_state(TASK_INTERRUPTIBLE);
  1075. schedule_timeout(msecs_to_jiffies(2000));
  1076. mask_of_crashed_or_stuck_cores =
  1077. (u16)octeon_read_csr64(oct, CN23XX_SLI_SCRATCH2);
  1078. if (!mask_of_crashed_or_stuck_cores)
  1079. continue;
  1080. WRITE_ONCE(oct->cores_crashed, true);
  1081. other_oct = get_other_octeon_device(oct);
  1082. if (other_oct)
  1083. WRITE_ONCE(other_oct->cores_crashed, true);
  1084. for (core = 0; core < LIO_MAX_CORES; core++) {
  1085. bool core_crashed_or_got_stuck;
  1086. core_crashed_or_got_stuck =
  1087. (mask_of_crashed_or_stuck_cores
  1088. >> core) & 1;
  1089. if (core_crashed_or_got_stuck &&
  1090. !err_msg_was_printed[core]) {
  1091. dev_err(&oct->pci_dev->dev,
  1092. "ERROR: Octeon core %d crashed or got stuck! See oct-fwdump for details.\n",
  1093. core);
  1094. err_msg_was_printed[core] = true;
  1095. }
  1096. }
  1097. if (all_vf_links_are_disabled)
  1098. continue;
  1099. disable_all_vf_links(oct);
  1100. disable_all_vf_links(other_oct);
  1101. all_vf_links_are_disabled = true;
  1102. #ifdef CONFIG_MODULE_UNLOAD
  1103. vfs_mask1 = READ_ONCE(oct->sriov_info.vf_drv_loaded_mask);
  1104. vfs_mask2 = READ_ONCE(other_oct->sriov_info.vf_drv_loaded_mask);
  1105. vfs_referencing_pf = hweight64(vfs_mask1);
  1106. vfs_referencing_pf += hweight64(vfs_mask2);
  1107. refcount = module_refcount(THIS_MODULE);
  1108. if (refcount >= vfs_referencing_pf) {
  1109. while (vfs_referencing_pf) {
  1110. module_put(THIS_MODULE);
  1111. vfs_referencing_pf--;
  1112. }
  1113. }
  1114. #endif
  1115. }
  1116. return 0;
  1117. }
  1118. /**
  1119. * \brief PCI probe handler
  1120. * @param pdev PCI device structure
  1121. * @param ent unused
  1122. */
  1123. static int
  1124. liquidio_probe(struct pci_dev *pdev,
  1125. const struct pci_device_id *ent __attribute__((unused)))
  1126. {
  1127. struct octeon_device *oct_dev = NULL;
  1128. struct handshake *hs;
  1129. oct_dev = octeon_allocate_device(pdev->device,
  1130. sizeof(struct octeon_device_priv));
  1131. if (!oct_dev) {
  1132. dev_err(&pdev->dev, "Unable to allocate device\n");
  1133. return -ENOMEM;
  1134. }
  1135. if (pdev->device == OCTEON_CN23XX_PF_VID)
  1136. oct_dev->msix_on = LIO_FLAG_MSIX_ENABLED;
  1137. dev_info(&pdev->dev, "Initializing device %x:%x.\n",
  1138. (u32)pdev->vendor, (u32)pdev->device);
  1139. /* Assign octeon_device for this device to the private data area. */
  1140. pci_set_drvdata(pdev, oct_dev);
  1141. /* set linux specific device pointer */
  1142. oct_dev->pci_dev = (void *)pdev;
  1143. hs = &handshake[oct_dev->octeon_id];
  1144. init_completion(&hs->init);
  1145. init_completion(&hs->started);
  1146. hs->pci_dev = pdev;
  1147. if (oct_dev->octeon_id == 0)
  1148. /* first LiquidIO NIC is detected */
  1149. complete(&first_stage);
  1150. if (octeon_device_init(oct_dev)) {
  1151. complete(&hs->init);
  1152. liquidio_remove(pdev);
  1153. return -ENOMEM;
  1154. }
  1155. if (OCTEON_CN23XX_PF(oct_dev)) {
  1156. u64 scratch1;
  1157. u8 bus, device, function;
  1158. scratch1 = octeon_read_csr64(oct_dev, CN23XX_SLI_SCRATCH1);
  1159. if (!(scratch1 & 4ULL)) {
  1160. /* Bit 2 of SLI_SCRATCH_1 is a flag that indicates that
  1161. * the lio watchdog kernel thread is running for this
  1162. * NIC. Each NIC gets one watchdog kernel thread.
  1163. */
  1164. scratch1 |= 4ULL;
  1165. octeon_write_csr64(oct_dev, CN23XX_SLI_SCRATCH1,
  1166. scratch1);
  1167. bus = pdev->bus->number;
  1168. device = PCI_SLOT(pdev->devfn);
  1169. function = PCI_FUNC(pdev->devfn);
  1170. oct_dev->watchdog_task = kthread_create(
  1171. liquidio_watchdog, oct_dev,
  1172. "liowd/%02hhx:%02hhx.%hhx", bus, device, function);
  1173. if (!IS_ERR(oct_dev->watchdog_task)) {
  1174. wake_up_process(oct_dev->watchdog_task);
  1175. } else {
  1176. oct_dev->watchdog_task = NULL;
  1177. dev_err(&oct_dev->pci_dev->dev,
  1178. "failed to create kernel_thread\n");
  1179. liquidio_remove(pdev);
  1180. return -1;
  1181. }
  1182. }
  1183. }
  1184. oct_dev->rx_pause = 1;
  1185. oct_dev->tx_pause = 1;
  1186. dev_dbg(&oct_dev->pci_dev->dev, "Device is ready\n");
  1187. return 0;
  1188. }
  1189. static bool fw_type_is_none(void)
  1190. {
  1191. return strncmp(fw_type, LIO_FW_NAME_TYPE_NONE,
  1192. sizeof(LIO_FW_NAME_TYPE_NONE)) == 0;
  1193. }
  1194. /**
  1195. *\brief Destroy resources associated with octeon device
  1196. * @param pdev PCI device structure
  1197. * @param ent unused
  1198. */
  1199. static void octeon_destroy_resources(struct octeon_device *oct)
  1200. {
  1201. int i;
  1202. struct msix_entry *msix_entries;
  1203. struct octeon_device_priv *oct_priv =
  1204. (struct octeon_device_priv *)oct->priv;
  1205. struct handshake *hs;
  1206. switch (atomic_read(&oct->status)) {
  1207. case OCT_DEV_RUNNING:
  1208. case OCT_DEV_CORE_OK:
  1209. /* No more instructions will be forwarded. */
  1210. atomic_set(&oct->status, OCT_DEV_IN_RESET);
  1211. oct->app_mode = CVM_DRV_INVALID_APP;
  1212. dev_dbg(&oct->pci_dev->dev, "Device state is now %s\n",
  1213. lio_get_state_string(&oct->status));
  1214. schedule_timeout_uninterruptible(HZ / 10);
  1215. /* fallthrough */
  1216. case OCT_DEV_HOST_OK:
  1217. /* fallthrough */
  1218. case OCT_DEV_CONSOLE_INIT_DONE:
  1219. /* Remove any consoles */
  1220. octeon_remove_consoles(oct);
  1221. /* fallthrough */
  1222. case OCT_DEV_IO_QUEUES_DONE:
  1223. if (wait_for_pending_requests(oct))
  1224. dev_err(&oct->pci_dev->dev, "There were pending requests\n");
  1225. if (lio_wait_for_instr_fetch(oct))
  1226. dev_err(&oct->pci_dev->dev, "IQ had pending instructions\n");
  1227. /* Disable the input and output queues now. No more packets will
  1228. * arrive from Octeon, but we should wait for all packet
  1229. * processing to finish.
  1230. */
  1231. oct->fn_list.disable_io_queues(oct);
  1232. if (lio_wait_for_oq_pkts(oct))
  1233. dev_err(&oct->pci_dev->dev, "OQ had pending packets\n");
  1234. /* fallthrough */
  1235. case OCT_DEV_INTR_SET_DONE:
  1236. /* Disable interrupts */
  1237. oct->fn_list.disable_interrupt(oct, OCTEON_ALL_INTR);
  1238. if (oct->msix_on) {
  1239. msix_entries = (struct msix_entry *)oct->msix_entries;
  1240. for (i = 0; i < oct->num_msix_irqs - 1; i++) {
  1241. /* clear the affinity_cpumask */
  1242. irq_set_affinity_hint(msix_entries[i].vector,
  1243. NULL);
  1244. free_irq(msix_entries[i].vector,
  1245. &oct->ioq_vector[i]);
  1246. }
  1247. /* non-iov vector's argument is oct struct */
  1248. free_irq(msix_entries[i].vector, oct);
  1249. pci_disable_msix(oct->pci_dev);
  1250. kfree(oct->msix_entries);
  1251. oct->msix_entries = NULL;
  1252. } else {
  1253. /* Release the interrupt line */
  1254. free_irq(oct->pci_dev->irq, oct);
  1255. if (oct->flags & LIO_FLAG_MSI_ENABLED)
  1256. pci_disable_msi(oct->pci_dev);
  1257. }
  1258. kfree(oct->irq_name_storage);
  1259. oct->irq_name_storage = NULL;
  1260. /* fallthrough */
  1261. case OCT_DEV_MSIX_ALLOC_VECTOR_DONE:
  1262. if (OCTEON_CN23XX_PF(oct))
  1263. octeon_free_ioq_vector(oct);
  1264. /* fallthrough */
  1265. case OCT_DEV_MBOX_SETUP_DONE:
  1266. if (OCTEON_CN23XX_PF(oct))
  1267. oct->fn_list.free_mbox(oct);
  1268. /* fallthrough */
  1269. case OCT_DEV_IN_RESET:
  1270. case OCT_DEV_DROQ_INIT_DONE:
  1271. /* Wait for any pending operations */
  1272. mdelay(100);
  1273. for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
  1274. if (!(oct->io_qmask.oq & BIT_ULL(i)))
  1275. continue;
  1276. octeon_delete_droq(oct, i);
  1277. }
  1278. /* Force any pending handshakes to complete */
  1279. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  1280. hs = &handshake[i];
  1281. if (hs->pci_dev) {
  1282. handshake[oct->octeon_id].init_ok = 0;
  1283. complete(&handshake[oct->octeon_id].init);
  1284. handshake[oct->octeon_id].started_ok = 0;
  1285. complete(&handshake[oct->octeon_id].started);
  1286. }
  1287. }
  1288. /* fallthrough */
  1289. case OCT_DEV_RESP_LIST_INIT_DONE:
  1290. octeon_delete_response_list(oct);
  1291. /* fallthrough */
  1292. case OCT_DEV_INSTR_QUEUE_INIT_DONE:
  1293. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  1294. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  1295. continue;
  1296. octeon_delete_instr_queue(oct, i);
  1297. }
  1298. #ifdef CONFIG_PCI_IOV
  1299. if (oct->sriov_info.sriov_enabled)
  1300. pci_disable_sriov(oct->pci_dev);
  1301. #endif
  1302. /* fallthrough */
  1303. case OCT_DEV_SC_BUFF_POOL_INIT_DONE:
  1304. octeon_free_sc_buffer_pool(oct);
  1305. /* fallthrough */
  1306. case OCT_DEV_DISPATCH_INIT_DONE:
  1307. octeon_delete_dispatch_list(oct);
  1308. cancel_delayed_work_sync(&oct->nic_poll_work.work);
  1309. /* fallthrough */
  1310. case OCT_DEV_PCI_MAP_DONE:
  1311. if (!fw_type_is_none()) {
  1312. /* Soft reset the octeon device before exiting */
  1313. if (!OCTEON_CN23XX_PF(oct) ||
  1314. (OCTEON_CN23XX_PF(oct) && !oct->octeon_id))
  1315. oct->fn_list.soft_reset(oct);
  1316. }
  1317. octeon_unmap_pci_barx(oct, 0);
  1318. octeon_unmap_pci_barx(oct, 1);
  1319. /* fallthrough */
  1320. case OCT_DEV_PCI_ENABLE_DONE:
  1321. pci_clear_master(oct->pci_dev);
  1322. /* Disable the device, releasing the PCI INT */
  1323. pci_disable_device(oct->pci_dev);
  1324. /* fallthrough */
  1325. case OCT_DEV_BEGIN_STATE:
  1326. /* Nothing to be done here either */
  1327. break;
  1328. } /* end switch (oct->status) */
  1329. tasklet_kill(&oct_priv->droq_tasklet);
  1330. }
  1331. /**
  1332. * \brief Callback for rx ctrl
  1333. * @param status status of request
  1334. * @param buf pointer to resp structure
  1335. */
  1336. static void rx_ctl_callback(struct octeon_device *oct,
  1337. u32 status,
  1338. void *buf)
  1339. {
  1340. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  1341. struct liquidio_rx_ctl_context *ctx;
  1342. ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
  1343. oct = lio_get_device(ctx->octeon_id);
  1344. if (status)
  1345. dev_err(&oct->pci_dev->dev, "rx ctl instruction failed. Status: %llx\n",
  1346. CVM_CAST64(status));
  1347. WRITE_ONCE(ctx->cond, 1);
  1348. /* This barrier is required to be sure that the response has been
  1349. * written fully before waking up the handler
  1350. */
  1351. wmb();
  1352. wake_up_interruptible(&ctx->wc);
  1353. }
  1354. /**
  1355. * \brief Send Rx control command
  1356. * @param lio per-network private data
  1357. * @param start_stop whether to start or stop
  1358. */
  1359. static void send_rx_ctrl_cmd(struct lio *lio, int start_stop)
  1360. {
  1361. struct octeon_soft_command *sc;
  1362. struct liquidio_rx_ctl_context *ctx;
  1363. union octnet_cmd *ncmd;
  1364. int ctx_size = sizeof(struct liquidio_rx_ctl_context);
  1365. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1366. int retval;
  1367. if (oct->props[lio->ifidx].rx_on == start_stop)
  1368. return;
  1369. sc = (struct octeon_soft_command *)
  1370. octeon_alloc_soft_command(oct, OCTNET_CMD_SIZE,
  1371. 16, ctx_size);
  1372. ncmd = (union octnet_cmd *)sc->virtdptr;
  1373. ctx = (struct liquidio_rx_ctl_context *)sc->ctxptr;
  1374. WRITE_ONCE(ctx->cond, 0);
  1375. ctx->octeon_id = lio_get_device_id(oct);
  1376. init_waitqueue_head(&ctx->wc);
  1377. ncmd->u64 = 0;
  1378. ncmd->s.cmd = OCTNET_CMD_RX_CTL;
  1379. ncmd->s.param1 = start_stop;
  1380. octeon_swap_8B_data((u64 *)ncmd, (OCTNET_CMD_SIZE >> 3));
  1381. sc->iq_no = lio->linfo.txpciq[0].s.q_no;
  1382. octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
  1383. OPCODE_NIC_CMD, 0, 0, 0);
  1384. sc->callback = rx_ctl_callback;
  1385. sc->callback_arg = sc;
  1386. sc->wait_time = 5000;
  1387. retval = octeon_send_soft_command(oct, sc);
  1388. if (retval == IQ_SEND_FAILED) {
  1389. netif_info(lio, rx_err, lio->netdev, "Failed to send RX Control message\n");
  1390. } else {
  1391. /* Sleep on a wait queue till the cond flag indicates that the
  1392. * response arrived or timed-out.
  1393. */
  1394. if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR)
  1395. return;
  1396. oct->props[lio->ifidx].rx_on = start_stop;
  1397. }
  1398. octeon_free_soft_command(oct, sc);
  1399. }
  1400. /**
  1401. * \brief Destroy NIC device interface
  1402. * @param oct octeon device
  1403. * @param ifidx which interface to destroy
  1404. *
  1405. * Cleanup associated with each interface for an Octeon device when NIC
  1406. * module is being unloaded or if initialization fails during load.
  1407. */
  1408. static void liquidio_destroy_nic_device(struct octeon_device *oct, int ifidx)
  1409. {
  1410. struct net_device *netdev = oct->props[ifidx].netdev;
  1411. struct lio *lio;
  1412. struct napi_struct *napi, *n;
  1413. if (!netdev) {
  1414. dev_err(&oct->pci_dev->dev, "%s No netdevice ptr for index %d\n",
  1415. __func__, ifidx);
  1416. return;
  1417. }
  1418. lio = GET_LIO(netdev);
  1419. dev_dbg(&oct->pci_dev->dev, "NIC device cleanup\n");
  1420. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING)
  1421. liquidio_stop(netdev);
  1422. if (fw_type_is_none()) {
  1423. struct octnic_ctrl_pkt nctrl;
  1424. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  1425. nctrl.ncmd.s.cmd = OCTNET_CMD_RESET_PF;
  1426. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  1427. octnet_send_nic_ctrl_pkt(oct, &nctrl);
  1428. }
  1429. if (oct->props[lio->ifidx].napi_enabled == 1) {
  1430. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  1431. napi_disable(napi);
  1432. oct->props[lio->ifidx].napi_enabled = 0;
  1433. if (OCTEON_CN23XX_PF(oct))
  1434. oct->droq[0]->ops.poll_mode = 0;
  1435. }
  1436. if (atomic_read(&lio->ifstate) & LIO_IFSTATE_REGISTERED)
  1437. unregister_netdev(netdev);
  1438. cleanup_link_status_change_wq(netdev);
  1439. cleanup_rx_oom_poll_fn(netdev);
  1440. delete_glists(lio);
  1441. free_netdev(netdev);
  1442. oct->props[ifidx].gmxport = -1;
  1443. oct->props[ifidx].netdev = NULL;
  1444. }
  1445. /**
  1446. * \brief Stop complete NIC functionality
  1447. * @param oct octeon device
  1448. */
  1449. static int liquidio_stop_nic_module(struct octeon_device *oct)
  1450. {
  1451. int i, j;
  1452. struct lio *lio;
  1453. dev_dbg(&oct->pci_dev->dev, "Stopping network interfaces\n");
  1454. if (!oct->ifcount) {
  1455. dev_err(&oct->pci_dev->dev, "Init for Octeon was not completed\n");
  1456. return 1;
  1457. }
  1458. spin_lock_bh(&oct->cmd_resp_wqlock);
  1459. oct->cmd_resp_state = OCT_DRV_OFFLINE;
  1460. spin_unlock_bh(&oct->cmd_resp_wqlock);
  1461. for (i = 0; i < oct->ifcount; i++) {
  1462. lio = GET_LIO(oct->props[i].netdev);
  1463. for (j = 0; j < lio->linfo.num_rxpciq; j++)
  1464. octeon_unregister_droq_ops(oct,
  1465. lio->linfo.rxpciq[j].s.q_no);
  1466. }
  1467. for (i = 0; i < oct->ifcount; i++)
  1468. liquidio_destroy_nic_device(oct, i);
  1469. dev_dbg(&oct->pci_dev->dev, "Network interfaces stopped\n");
  1470. return 0;
  1471. }
  1472. /**
  1473. * \brief Cleans up resources at unload time
  1474. * @param pdev PCI device structure
  1475. */
  1476. static void liquidio_remove(struct pci_dev *pdev)
  1477. {
  1478. struct octeon_device *oct_dev = pci_get_drvdata(pdev);
  1479. dev_dbg(&oct_dev->pci_dev->dev, "Stopping device\n");
  1480. if (oct_dev->watchdog_task)
  1481. kthread_stop(oct_dev->watchdog_task);
  1482. if (oct_dev->app_mode && (oct_dev->app_mode == CVM_DRV_NIC_APP))
  1483. liquidio_stop_nic_module(oct_dev);
  1484. /* Reset the octeon device and cleanup all memory allocated for
  1485. * the octeon device by driver.
  1486. */
  1487. octeon_destroy_resources(oct_dev);
  1488. dev_info(&oct_dev->pci_dev->dev, "Device removed\n");
  1489. /* This octeon device has been removed. Update the global
  1490. * data structure to reflect this. Free the device structure.
  1491. */
  1492. octeon_free_device_mem(oct_dev);
  1493. }
  1494. /**
  1495. * \brief Identify the Octeon device and to map the BAR address space
  1496. * @param oct octeon device
  1497. */
  1498. static int octeon_chip_specific_setup(struct octeon_device *oct)
  1499. {
  1500. u32 dev_id, rev_id;
  1501. int ret = 1;
  1502. char *s;
  1503. pci_read_config_dword(oct->pci_dev, 0, &dev_id);
  1504. pci_read_config_dword(oct->pci_dev, 8, &rev_id);
  1505. oct->rev_id = rev_id & 0xff;
  1506. switch (dev_id) {
  1507. case OCTEON_CN68XX_PCIID:
  1508. oct->chip_id = OCTEON_CN68XX;
  1509. ret = lio_setup_cn68xx_octeon_device(oct);
  1510. s = "CN68XX";
  1511. break;
  1512. case OCTEON_CN66XX_PCIID:
  1513. oct->chip_id = OCTEON_CN66XX;
  1514. ret = lio_setup_cn66xx_octeon_device(oct);
  1515. s = "CN66XX";
  1516. break;
  1517. case OCTEON_CN23XX_PCIID_PF:
  1518. oct->chip_id = OCTEON_CN23XX_PF_VID;
  1519. ret = setup_cn23xx_octeon_pf_device(oct);
  1520. s = "CN23XX";
  1521. break;
  1522. default:
  1523. s = "?";
  1524. dev_err(&oct->pci_dev->dev, "Unknown device found (dev_id: %x)\n",
  1525. dev_id);
  1526. }
  1527. if (!ret)
  1528. dev_info(&oct->pci_dev->dev, "%s PASS%d.%d %s Version: %s\n", s,
  1529. OCTEON_MAJOR_REV(oct),
  1530. OCTEON_MINOR_REV(oct),
  1531. octeon_get_conf(oct)->card_name,
  1532. LIQUIDIO_VERSION);
  1533. return ret;
  1534. }
  1535. /**
  1536. * \brief PCI initialization for each Octeon device.
  1537. * @param oct octeon device
  1538. */
  1539. static int octeon_pci_os_setup(struct octeon_device *oct)
  1540. {
  1541. /* setup PCI stuff first */
  1542. if (pci_enable_device(oct->pci_dev)) {
  1543. dev_err(&oct->pci_dev->dev, "pci_enable_device failed\n");
  1544. return 1;
  1545. }
  1546. if (dma_set_mask_and_coherent(&oct->pci_dev->dev, DMA_BIT_MASK(64))) {
  1547. dev_err(&oct->pci_dev->dev, "Unexpected DMA device capability\n");
  1548. pci_disable_device(oct->pci_dev);
  1549. return 1;
  1550. }
  1551. /* Enable PCI DMA Master. */
  1552. pci_set_master(oct->pci_dev);
  1553. return 0;
  1554. }
  1555. static inline int skb_iq(struct lio *lio, struct sk_buff *skb)
  1556. {
  1557. int q = 0;
  1558. if (netif_is_multiqueue(lio->netdev))
  1559. q = skb->queue_mapping % lio->linfo.num_txpciq;
  1560. return q;
  1561. }
  1562. /**
  1563. * \brief Check Tx queue state for a given network buffer
  1564. * @param lio per-network private data
  1565. * @param skb network buffer
  1566. */
  1567. static inline int check_txq_state(struct lio *lio, struct sk_buff *skb)
  1568. {
  1569. int q = 0, iq = 0;
  1570. if (netif_is_multiqueue(lio->netdev)) {
  1571. q = skb->queue_mapping;
  1572. iq = lio->linfo.txpciq[(q % (lio->linfo.num_txpciq))].s.q_no;
  1573. } else {
  1574. iq = lio->txq;
  1575. q = iq;
  1576. }
  1577. if (octnet_iq_is_full(lio->oct_dev, iq))
  1578. return 0;
  1579. if (__netif_subqueue_stopped(lio->netdev, q)) {
  1580. INCR_INSTRQUEUE_PKT_COUNT(lio->oct_dev, iq, tx_restart, 1);
  1581. wake_q(lio->netdev, q);
  1582. }
  1583. return 1;
  1584. }
  1585. /**
  1586. * \brief Unmap and free network buffer
  1587. * @param buf buffer
  1588. */
  1589. static void free_netbuf(void *buf)
  1590. {
  1591. struct sk_buff *skb;
  1592. struct octnet_buf_free_info *finfo;
  1593. struct lio *lio;
  1594. finfo = (struct octnet_buf_free_info *)buf;
  1595. skb = finfo->skb;
  1596. lio = finfo->lio;
  1597. dma_unmap_single(&lio->oct_dev->pci_dev->dev, finfo->dptr, skb->len,
  1598. DMA_TO_DEVICE);
  1599. check_txq_state(lio, skb);
  1600. tx_buffer_free(skb);
  1601. }
  1602. /**
  1603. * \brief Unmap and free gather buffer
  1604. * @param buf buffer
  1605. */
  1606. static void free_netsgbuf(void *buf)
  1607. {
  1608. struct octnet_buf_free_info *finfo;
  1609. struct sk_buff *skb;
  1610. struct lio *lio;
  1611. struct octnic_gather *g;
  1612. int i, frags, iq;
  1613. finfo = (struct octnet_buf_free_info *)buf;
  1614. skb = finfo->skb;
  1615. lio = finfo->lio;
  1616. g = finfo->g;
  1617. frags = skb_shinfo(skb)->nr_frags;
  1618. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1619. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1620. DMA_TO_DEVICE);
  1621. i = 1;
  1622. while (frags--) {
  1623. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1624. pci_unmap_page((lio->oct_dev)->pci_dev,
  1625. g->sg[(i >> 2)].ptr[(i & 3)],
  1626. frag->size, DMA_TO_DEVICE);
  1627. i++;
  1628. }
  1629. iq = skb_iq(lio, skb);
  1630. spin_lock(&lio->glist_lock[iq]);
  1631. list_add_tail(&g->list, &lio->glist[iq]);
  1632. spin_unlock(&lio->glist_lock[iq]);
  1633. check_txq_state(lio, skb); /* mq support: sub-queue state check */
  1634. tx_buffer_free(skb);
  1635. }
  1636. /**
  1637. * \brief Unmap and free gather buffer with response
  1638. * @param buf buffer
  1639. */
  1640. static void free_netsgbuf_with_resp(void *buf)
  1641. {
  1642. struct octeon_soft_command *sc;
  1643. struct octnet_buf_free_info *finfo;
  1644. struct sk_buff *skb;
  1645. struct lio *lio;
  1646. struct octnic_gather *g;
  1647. int i, frags, iq;
  1648. sc = (struct octeon_soft_command *)buf;
  1649. skb = (struct sk_buff *)sc->callback_arg;
  1650. finfo = (struct octnet_buf_free_info *)&skb->cb;
  1651. lio = finfo->lio;
  1652. g = finfo->g;
  1653. frags = skb_shinfo(skb)->nr_frags;
  1654. dma_unmap_single(&lio->oct_dev->pci_dev->dev,
  1655. g->sg[0].ptr[0], (skb->len - skb->data_len),
  1656. DMA_TO_DEVICE);
  1657. i = 1;
  1658. while (frags--) {
  1659. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1660. pci_unmap_page((lio->oct_dev)->pci_dev,
  1661. g->sg[(i >> 2)].ptr[(i & 3)],
  1662. frag->size, DMA_TO_DEVICE);
  1663. i++;
  1664. }
  1665. iq = skb_iq(lio, skb);
  1666. spin_lock(&lio->glist_lock[iq]);
  1667. list_add_tail(&g->list, &lio->glist[iq]);
  1668. spin_unlock(&lio->glist_lock[iq]);
  1669. /* Don't free the skb yet */
  1670. check_txq_state(lio, skb);
  1671. }
  1672. /**
  1673. * \brief Adjust ptp frequency
  1674. * @param ptp PTP clock info
  1675. * @param ppb how much to adjust by, in parts-per-billion
  1676. */
  1677. static int liquidio_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  1678. {
  1679. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1680. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1681. u64 comp, delta;
  1682. unsigned long flags;
  1683. bool neg_adj = false;
  1684. if (ppb < 0) {
  1685. neg_adj = true;
  1686. ppb = -ppb;
  1687. }
  1688. /* The hardware adds the clock compensation value to the
  1689. * PTP clock on every coprocessor clock cycle, so we
  1690. * compute the delta in terms of coprocessor clocks.
  1691. */
  1692. delta = (u64)ppb << 32;
  1693. do_div(delta, oct->coproc_clock_rate);
  1694. spin_lock_irqsave(&lio->ptp_lock, flags);
  1695. comp = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_COMP);
  1696. if (neg_adj)
  1697. comp -= delta;
  1698. else
  1699. comp += delta;
  1700. lio_pci_writeq(oct, comp, CN6XXX_MIO_PTP_CLOCK_COMP);
  1701. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1702. return 0;
  1703. }
  1704. /**
  1705. * \brief Adjust ptp time
  1706. * @param ptp PTP clock info
  1707. * @param delta how much to adjust by, in nanosecs
  1708. */
  1709. static int liquidio_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  1710. {
  1711. unsigned long flags;
  1712. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1713. spin_lock_irqsave(&lio->ptp_lock, flags);
  1714. lio->ptp_adjust += delta;
  1715. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1716. return 0;
  1717. }
  1718. /**
  1719. * \brief Get hardware clock time, including any adjustment
  1720. * @param ptp PTP clock info
  1721. * @param ts timespec
  1722. */
  1723. static int liquidio_ptp_gettime(struct ptp_clock_info *ptp,
  1724. struct timespec64 *ts)
  1725. {
  1726. u64 ns;
  1727. unsigned long flags;
  1728. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1729. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1730. spin_lock_irqsave(&lio->ptp_lock, flags);
  1731. ns = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_HI);
  1732. ns += lio->ptp_adjust;
  1733. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1734. *ts = ns_to_timespec64(ns);
  1735. return 0;
  1736. }
  1737. /**
  1738. * \brief Set hardware clock time. Reset adjustment
  1739. * @param ptp PTP clock info
  1740. * @param ts timespec
  1741. */
  1742. static int liquidio_ptp_settime(struct ptp_clock_info *ptp,
  1743. const struct timespec64 *ts)
  1744. {
  1745. u64 ns;
  1746. unsigned long flags;
  1747. struct lio *lio = container_of(ptp, struct lio, ptp_info);
  1748. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1749. ns = timespec_to_ns(ts);
  1750. spin_lock_irqsave(&lio->ptp_lock, flags);
  1751. lio_pci_writeq(oct, ns, CN6XXX_MIO_PTP_CLOCK_HI);
  1752. lio->ptp_adjust = 0;
  1753. spin_unlock_irqrestore(&lio->ptp_lock, flags);
  1754. return 0;
  1755. }
  1756. /**
  1757. * \brief Check if PTP is enabled
  1758. * @param ptp PTP clock info
  1759. * @param rq request
  1760. * @param on is it on
  1761. */
  1762. static int
  1763. liquidio_ptp_enable(struct ptp_clock_info *ptp __attribute__((unused)),
  1764. struct ptp_clock_request *rq __attribute__((unused)),
  1765. int on __attribute__((unused)))
  1766. {
  1767. return -EOPNOTSUPP;
  1768. }
  1769. /**
  1770. * \brief Open PTP clock source
  1771. * @param netdev network device
  1772. */
  1773. static void oct_ptp_open(struct net_device *netdev)
  1774. {
  1775. struct lio *lio = GET_LIO(netdev);
  1776. struct octeon_device *oct = (struct octeon_device *)lio->oct_dev;
  1777. spin_lock_init(&lio->ptp_lock);
  1778. snprintf(lio->ptp_info.name, 16, "%s", netdev->name);
  1779. lio->ptp_info.owner = THIS_MODULE;
  1780. lio->ptp_info.max_adj = 250000000;
  1781. lio->ptp_info.n_alarm = 0;
  1782. lio->ptp_info.n_ext_ts = 0;
  1783. lio->ptp_info.n_per_out = 0;
  1784. lio->ptp_info.pps = 0;
  1785. lio->ptp_info.adjfreq = liquidio_ptp_adjfreq;
  1786. lio->ptp_info.adjtime = liquidio_ptp_adjtime;
  1787. lio->ptp_info.gettime64 = liquidio_ptp_gettime;
  1788. lio->ptp_info.settime64 = liquidio_ptp_settime;
  1789. lio->ptp_info.enable = liquidio_ptp_enable;
  1790. lio->ptp_adjust = 0;
  1791. lio->ptp_clock = ptp_clock_register(&lio->ptp_info,
  1792. &oct->pci_dev->dev);
  1793. if (IS_ERR(lio->ptp_clock))
  1794. lio->ptp_clock = NULL;
  1795. }
  1796. /**
  1797. * \brief Init PTP clock
  1798. * @param oct octeon device
  1799. */
  1800. static void liquidio_ptp_init(struct octeon_device *oct)
  1801. {
  1802. u64 clock_comp, cfg;
  1803. clock_comp = (u64)NSEC_PER_SEC << 32;
  1804. do_div(clock_comp, oct->coproc_clock_rate);
  1805. lio_pci_writeq(oct, clock_comp, CN6XXX_MIO_PTP_CLOCK_COMP);
  1806. /* Enable */
  1807. cfg = lio_pci_readq(oct, CN6XXX_MIO_PTP_CLOCK_CFG);
  1808. lio_pci_writeq(oct, cfg | 0x01, CN6XXX_MIO_PTP_CLOCK_CFG);
  1809. }
  1810. /**
  1811. * \brief Load firmware to device
  1812. * @param oct octeon device
  1813. *
  1814. * Maps device to firmware filename, requests firmware, and downloads it
  1815. */
  1816. static int load_firmware(struct octeon_device *oct)
  1817. {
  1818. int ret = 0;
  1819. const struct firmware *fw;
  1820. char fw_name[LIO_MAX_FW_FILENAME_LEN];
  1821. char *tmp_fw_type;
  1822. if (fw_type_is_none()) {
  1823. dev_info(&oct->pci_dev->dev, "Skipping firmware load\n");
  1824. return ret;
  1825. }
  1826. if (fw_type[0] == '\0')
  1827. tmp_fw_type = LIO_FW_NAME_TYPE_NIC;
  1828. else
  1829. tmp_fw_type = fw_type;
  1830. sprintf(fw_name, "%s%s%s_%s%s", LIO_FW_DIR, LIO_FW_BASE_NAME,
  1831. octeon_get_conf(oct)->card_name, tmp_fw_type,
  1832. LIO_FW_NAME_SUFFIX);
  1833. ret = request_firmware(&fw, fw_name, &oct->pci_dev->dev);
  1834. if (ret) {
  1835. dev_err(&oct->pci_dev->dev, "Request firmware failed. Could not find file %s.\n.",
  1836. fw_name);
  1837. release_firmware(fw);
  1838. return ret;
  1839. }
  1840. ret = octeon_download_firmware(oct, fw->data, fw->size);
  1841. release_firmware(fw);
  1842. return ret;
  1843. }
  1844. /**
  1845. * \brief Setup output queue
  1846. * @param oct octeon device
  1847. * @param q_no which queue
  1848. * @param num_descs how many descriptors
  1849. * @param desc_size size of each descriptor
  1850. * @param app_ctx application context
  1851. */
  1852. static int octeon_setup_droq(struct octeon_device *oct, int q_no, int num_descs,
  1853. int desc_size, void *app_ctx)
  1854. {
  1855. int ret_val = 0;
  1856. dev_dbg(&oct->pci_dev->dev, "Creating Droq: %d\n", q_no);
  1857. /* droq creation and local register settings. */
  1858. ret_val = octeon_create_droq(oct, q_no, num_descs, desc_size, app_ctx);
  1859. if (ret_val < 0)
  1860. return ret_val;
  1861. if (ret_val == 1) {
  1862. dev_dbg(&oct->pci_dev->dev, "Using default droq %d\n", q_no);
  1863. return 0;
  1864. }
  1865. /* tasklet creation for the droq */
  1866. /* Enable the droq queues */
  1867. octeon_set_droq_pkt_op(oct, q_no, 1);
  1868. /* Send Credit for Octeon Output queues. Credits are always
  1869. * sent after the output queue is enabled.
  1870. */
  1871. writel(oct->droq[q_no]->max_count,
  1872. oct->droq[q_no]->pkts_credit_reg);
  1873. return ret_val;
  1874. }
  1875. /**
  1876. * \brief Callback for getting interface configuration
  1877. * @param status status of request
  1878. * @param buf pointer to resp structure
  1879. */
  1880. static void if_cfg_callback(struct octeon_device *oct,
  1881. u32 status __attribute__((unused)),
  1882. void *buf)
  1883. {
  1884. struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
  1885. struct liquidio_if_cfg_resp *resp;
  1886. struct liquidio_if_cfg_context *ctx;
  1887. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  1888. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  1889. oct = lio_get_device(ctx->octeon_id);
  1890. if (resp->status)
  1891. dev_err(&oct->pci_dev->dev, "nic if cfg instruction failed. Status: 0x%llx (0x%08x)\n",
  1892. CVM_CAST64(resp->status), status);
  1893. WRITE_ONCE(ctx->cond, 1);
  1894. snprintf(oct->fw_info.liquidio_firmware_version, 32, "%s",
  1895. resp->cfg_info.liquidio_firmware_version);
  1896. /* This barrier is required to be sure that the response has been
  1897. * written fully before waking up the handler
  1898. */
  1899. wmb();
  1900. wake_up_interruptible(&ctx->wc);
  1901. }
  1902. /** Routine to push packets arriving on Octeon interface upto network layer.
  1903. * @param oct_id - octeon device id.
  1904. * @param skbuff - skbuff struct to be passed to network layer.
  1905. * @param len - size of total data received.
  1906. * @param rh - Control header associated with the packet
  1907. * @param param - additional control data with the packet
  1908. * @param arg - farg registered in droq_ops
  1909. */
  1910. static void
  1911. liquidio_push_packet(u32 octeon_id __attribute__((unused)),
  1912. void *skbuff,
  1913. u32 len,
  1914. union octeon_rh *rh,
  1915. void *param,
  1916. void *arg)
  1917. {
  1918. struct napi_struct *napi = param;
  1919. struct sk_buff *skb = (struct sk_buff *)skbuff;
  1920. struct skb_shared_hwtstamps *shhwtstamps;
  1921. u64 ns;
  1922. u16 vtag = 0;
  1923. u32 r_dh_off;
  1924. struct net_device *netdev = (struct net_device *)arg;
  1925. struct octeon_droq *droq = container_of(param, struct octeon_droq,
  1926. napi);
  1927. if (netdev) {
  1928. int packet_was_received;
  1929. struct lio *lio = GET_LIO(netdev);
  1930. struct octeon_device *oct = lio->oct_dev;
  1931. /* Do not proceed if the interface is not in RUNNING state. */
  1932. if (!ifstate_check(lio, LIO_IFSTATE_RUNNING)) {
  1933. recv_buffer_free(skb);
  1934. droq->stats.rx_dropped++;
  1935. return;
  1936. }
  1937. skb->dev = netdev;
  1938. skb_record_rx_queue(skb, droq->q_no);
  1939. if (likely(len > MIN_SKB_SIZE)) {
  1940. struct octeon_skb_page_info *pg_info;
  1941. unsigned char *va;
  1942. pg_info = ((struct octeon_skb_page_info *)(skb->cb));
  1943. if (pg_info->page) {
  1944. /* For Paged allocation use the frags */
  1945. va = page_address(pg_info->page) +
  1946. pg_info->page_offset;
  1947. memcpy(skb->data, va, MIN_SKB_SIZE);
  1948. skb_put(skb, MIN_SKB_SIZE);
  1949. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
  1950. pg_info->page,
  1951. pg_info->page_offset +
  1952. MIN_SKB_SIZE,
  1953. len - MIN_SKB_SIZE,
  1954. LIO_RXBUFFER_SZ);
  1955. }
  1956. } else {
  1957. struct octeon_skb_page_info *pg_info =
  1958. ((struct octeon_skb_page_info *)(skb->cb));
  1959. skb_copy_to_linear_data(skb, page_address(pg_info->page)
  1960. + pg_info->page_offset, len);
  1961. skb_put(skb, len);
  1962. put_page(pg_info->page);
  1963. }
  1964. r_dh_off = (rh->r_dh.len - 1) * BYTES_PER_DHLEN_UNIT;
  1965. if (((oct->chip_id == OCTEON_CN66XX) ||
  1966. (oct->chip_id == OCTEON_CN68XX)) &&
  1967. ptp_enable) {
  1968. if (rh->r_dh.has_hwtstamp) {
  1969. /* timestamp is included from the hardware at
  1970. * the beginning of the packet.
  1971. */
  1972. if (ifstate_check
  1973. (lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED)) {
  1974. /* Nanoseconds are in the first 64-bits
  1975. * of the packet.
  1976. */
  1977. memcpy(&ns, (skb->data + r_dh_off),
  1978. sizeof(ns));
  1979. r_dh_off -= BYTES_PER_DHLEN_UNIT;
  1980. shhwtstamps = skb_hwtstamps(skb);
  1981. shhwtstamps->hwtstamp =
  1982. ns_to_ktime(ns +
  1983. lio->ptp_adjust);
  1984. }
  1985. }
  1986. }
  1987. if (rh->r_dh.has_hash) {
  1988. __be32 *hash_be = (__be32 *)(skb->data + r_dh_off);
  1989. u32 hash = be32_to_cpu(*hash_be);
  1990. skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
  1991. r_dh_off -= BYTES_PER_DHLEN_UNIT;
  1992. }
  1993. skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
  1994. skb->protocol = eth_type_trans(skb, skb->dev);
  1995. if ((netdev->features & NETIF_F_RXCSUM) &&
  1996. (((rh->r_dh.encap_on) &&
  1997. (rh->r_dh.csum_verified & CNNIC_TUN_CSUM_VERIFIED)) ||
  1998. (!(rh->r_dh.encap_on) &&
  1999. (rh->r_dh.csum_verified & CNNIC_CSUM_VERIFIED))))
  2000. /* checksum has already been verified */
  2001. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2002. else
  2003. skb->ip_summed = CHECKSUM_NONE;
  2004. /* Setting Encapsulation field on basis of status received
  2005. * from the firmware
  2006. */
  2007. if (rh->r_dh.encap_on) {
  2008. skb->encapsulation = 1;
  2009. skb->csum_level = 1;
  2010. droq->stats.rx_vxlan++;
  2011. }
  2012. /* inbound VLAN tag */
  2013. if ((netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  2014. (rh->r_dh.vlan != 0)) {
  2015. u16 vid = rh->r_dh.vlan;
  2016. u16 priority = rh->r_dh.priority;
  2017. vtag = priority << 13 | vid;
  2018. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
  2019. }
  2020. packet_was_received = napi_gro_receive(napi, skb) != GRO_DROP;
  2021. if (packet_was_received) {
  2022. droq->stats.rx_bytes_received += len;
  2023. droq->stats.rx_pkts_received++;
  2024. } else {
  2025. droq->stats.rx_dropped++;
  2026. netif_info(lio, rx_err, lio->netdev,
  2027. "droq:%d error rx_dropped:%llu\n",
  2028. droq->q_no, droq->stats.rx_dropped);
  2029. }
  2030. } else {
  2031. recv_buffer_free(skb);
  2032. }
  2033. }
  2034. /**
  2035. * \brief wrapper for calling napi_schedule
  2036. * @param param parameters to pass to napi_schedule
  2037. *
  2038. * Used when scheduling on different CPUs
  2039. */
  2040. static void napi_schedule_wrapper(void *param)
  2041. {
  2042. struct napi_struct *napi = param;
  2043. napi_schedule(napi);
  2044. }
  2045. /**
  2046. * \brief callback when receive interrupt occurs and we are in NAPI mode
  2047. * @param arg pointer to octeon output queue
  2048. */
  2049. static void liquidio_napi_drv_callback(void *arg)
  2050. {
  2051. struct octeon_device *oct;
  2052. struct octeon_droq *droq = arg;
  2053. int this_cpu = smp_processor_id();
  2054. oct = droq->oct_dev;
  2055. if (OCTEON_CN23XX_PF(oct) || droq->cpu_id == this_cpu) {
  2056. napi_schedule_irqoff(&droq->napi);
  2057. } else {
  2058. struct call_single_data *csd = &droq->csd;
  2059. csd->func = napi_schedule_wrapper;
  2060. csd->info = &droq->napi;
  2061. csd->flags = 0;
  2062. smp_call_function_single_async(droq->cpu_id, csd);
  2063. }
  2064. }
  2065. /**
  2066. * \brief Entry point for NAPI polling
  2067. * @param napi NAPI structure
  2068. * @param budget maximum number of items to process
  2069. */
  2070. static int liquidio_napi_poll(struct napi_struct *napi, int budget)
  2071. {
  2072. struct octeon_droq *droq;
  2073. int work_done;
  2074. int tx_done = 0, iq_no;
  2075. struct octeon_instr_queue *iq;
  2076. struct octeon_device *oct;
  2077. droq = container_of(napi, struct octeon_droq, napi);
  2078. oct = droq->oct_dev;
  2079. iq_no = droq->q_no;
  2080. /* Handle Droq descriptors */
  2081. work_done = octeon_process_droq_poll_cmd(oct, droq->q_no,
  2082. POLL_EVENT_PROCESS_PKTS,
  2083. budget);
  2084. /* Flush the instruction queue */
  2085. iq = oct->instr_queue[iq_no];
  2086. if (iq) {
  2087. if (atomic_read(&iq->instr_pending))
  2088. /* Process iq buffers with in the budget limits */
  2089. tx_done = octeon_flush_iq(oct, iq, budget);
  2090. else
  2091. tx_done = 1;
  2092. /* Update iq read-index rather than waiting for next interrupt.
  2093. * Return back if tx_done is false.
  2094. */
  2095. update_txq_status(oct, iq_no);
  2096. } else {
  2097. dev_err(&oct->pci_dev->dev, "%s: iq (%d) num invalid\n",
  2098. __func__, iq_no);
  2099. }
  2100. /* force enable interrupt if reg cnts are high to avoid wraparound */
  2101. if ((work_done < budget && tx_done) ||
  2102. (iq && iq->pkt_in_done >= MAX_REG_CNT) ||
  2103. (droq->pkt_count >= MAX_REG_CNT)) {
  2104. tx_done = 1;
  2105. napi_complete_done(napi, work_done);
  2106. octeon_process_droq_poll_cmd(droq->oct_dev, droq->q_no,
  2107. POLL_EVENT_ENABLE_INTR, 0);
  2108. return 0;
  2109. }
  2110. return (!tx_done) ? (budget) : (work_done);
  2111. }
  2112. /**
  2113. * \brief Setup input and output queues
  2114. * @param octeon_dev octeon device
  2115. * @param ifidx Interface Index
  2116. *
  2117. * Note: Queues are with respect to the octeon device. Thus
  2118. * an input queue is for egress packets, and output queues
  2119. * are for ingress packets.
  2120. */
  2121. static inline int setup_io_queues(struct octeon_device *octeon_dev,
  2122. int ifidx)
  2123. {
  2124. struct octeon_droq_ops droq_ops;
  2125. struct net_device *netdev;
  2126. static int cpu_id;
  2127. static int cpu_id_modulus;
  2128. struct octeon_droq *droq;
  2129. struct napi_struct *napi;
  2130. int q, q_no, retval = 0;
  2131. struct lio *lio;
  2132. int num_tx_descs;
  2133. netdev = octeon_dev->props[ifidx].netdev;
  2134. lio = GET_LIO(netdev);
  2135. memset(&droq_ops, 0, sizeof(struct octeon_droq_ops));
  2136. droq_ops.fptr = liquidio_push_packet;
  2137. droq_ops.farg = (void *)netdev;
  2138. droq_ops.poll_mode = 1;
  2139. droq_ops.napi_fn = liquidio_napi_drv_callback;
  2140. cpu_id = 0;
  2141. cpu_id_modulus = num_present_cpus();
  2142. /* set up DROQs. */
  2143. for (q = 0; q < lio->linfo.num_rxpciq; q++) {
  2144. q_no = lio->linfo.rxpciq[q].s.q_no;
  2145. dev_dbg(&octeon_dev->pci_dev->dev,
  2146. "setup_io_queues index:%d linfo.rxpciq.s.q_no:%d\n",
  2147. q, q_no);
  2148. retval = octeon_setup_droq(octeon_dev, q_no,
  2149. CFG_GET_NUM_RX_DESCS_NIC_IF
  2150. (octeon_get_conf(octeon_dev),
  2151. lio->ifidx),
  2152. CFG_GET_NUM_RX_BUF_SIZE_NIC_IF
  2153. (octeon_get_conf(octeon_dev),
  2154. lio->ifidx), NULL);
  2155. if (retval) {
  2156. dev_err(&octeon_dev->pci_dev->dev,
  2157. "%s : Runtime DROQ(RxQ) creation failed.\n",
  2158. __func__);
  2159. return 1;
  2160. }
  2161. droq = octeon_dev->droq[q_no];
  2162. napi = &droq->napi;
  2163. dev_dbg(&octeon_dev->pci_dev->dev, "netif_napi_add netdev:%llx oct:%llx pf_num:%d\n",
  2164. (u64)netdev, (u64)octeon_dev, octeon_dev->pf_num);
  2165. netif_napi_add(netdev, napi, liquidio_napi_poll, 64);
  2166. /* designate a CPU for this droq */
  2167. droq->cpu_id = cpu_id;
  2168. cpu_id++;
  2169. if (cpu_id >= cpu_id_modulus)
  2170. cpu_id = 0;
  2171. octeon_register_droq_ops(octeon_dev, q_no, &droq_ops);
  2172. }
  2173. if (OCTEON_CN23XX_PF(octeon_dev)) {
  2174. /* 23XX PF can receive control messages (via the first PF-owned
  2175. * droq) from the firmware even if the ethX interface is down,
  2176. * so that's why poll_mode must be off for the first droq.
  2177. */
  2178. octeon_dev->droq[0]->ops.poll_mode = 0;
  2179. }
  2180. /* set up IQs. */
  2181. for (q = 0; q < lio->linfo.num_txpciq; q++) {
  2182. num_tx_descs = CFG_GET_NUM_TX_DESCS_NIC_IF(octeon_get_conf
  2183. (octeon_dev),
  2184. lio->ifidx);
  2185. retval = octeon_setup_iq(octeon_dev, ifidx, q,
  2186. lio->linfo.txpciq[q], num_tx_descs,
  2187. netdev_get_tx_queue(netdev, q));
  2188. if (retval) {
  2189. dev_err(&octeon_dev->pci_dev->dev,
  2190. " %s : Runtime IQ(TxQ) creation failed.\n",
  2191. __func__);
  2192. return 1;
  2193. }
  2194. if (octeon_dev->ioq_vector) {
  2195. struct octeon_ioq_vector *ioq_vector;
  2196. ioq_vector = &octeon_dev->ioq_vector[q];
  2197. netif_set_xps_queue(netdev,
  2198. &ioq_vector->affinity_mask,
  2199. ioq_vector->iq_index);
  2200. }
  2201. }
  2202. return 0;
  2203. }
  2204. /**
  2205. * \brief Poll routine for checking transmit queue status
  2206. * @param work work_struct data structure
  2207. */
  2208. static void octnet_poll_check_txq_status(struct work_struct *work)
  2209. {
  2210. struct cavium_wk *wk = (struct cavium_wk *)work;
  2211. struct lio *lio = (struct lio *)wk->ctxptr;
  2212. if (!ifstate_check(lio, LIO_IFSTATE_RUNNING))
  2213. return;
  2214. check_txq_status(lio);
  2215. queue_delayed_work(lio->txq_status_wq.wq,
  2216. &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
  2217. }
  2218. /**
  2219. * \brief Sets up the txq poll check
  2220. * @param netdev network device
  2221. */
  2222. static inline int setup_tx_poll_fn(struct net_device *netdev)
  2223. {
  2224. struct lio *lio = GET_LIO(netdev);
  2225. struct octeon_device *oct = lio->oct_dev;
  2226. lio->txq_status_wq.wq = alloc_workqueue("txq-status",
  2227. WQ_MEM_RECLAIM, 0);
  2228. if (!lio->txq_status_wq.wq) {
  2229. dev_err(&oct->pci_dev->dev, "unable to create cavium txq status wq\n");
  2230. return -1;
  2231. }
  2232. INIT_DELAYED_WORK(&lio->txq_status_wq.wk.work,
  2233. octnet_poll_check_txq_status);
  2234. lio->txq_status_wq.wk.ctxptr = lio;
  2235. queue_delayed_work(lio->txq_status_wq.wq,
  2236. &lio->txq_status_wq.wk.work, msecs_to_jiffies(1));
  2237. return 0;
  2238. }
  2239. static inline void cleanup_tx_poll_fn(struct net_device *netdev)
  2240. {
  2241. struct lio *lio = GET_LIO(netdev);
  2242. if (lio->txq_status_wq.wq) {
  2243. cancel_delayed_work_sync(&lio->txq_status_wq.wk.work);
  2244. destroy_workqueue(lio->txq_status_wq.wq);
  2245. }
  2246. }
  2247. /**
  2248. * \brief Net device open for LiquidIO
  2249. * @param netdev network device
  2250. */
  2251. static int liquidio_open(struct net_device *netdev)
  2252. {
  2253. struct lio *lio = GET_LIO(netdev);
  2254. struct octeon_device *oct = lio->oct_dev;
  2255. struct napi_struct *napi, *n;
  2256. if (oct->props[lio->ifidx].napi_enabled == 0) {
  2257. list_for_each_entry_safe(napi, n, &netdev->napi_list, dev_list)
  2258. napi_enable(napi);
  2259. oct->props[lio->ifidx].napi_enabled = 1;
  2260. if (OCTEON_CN23XX_PF(oct))
  2261. oct->droq[0]->ops.poll_mode = 1;
  2262. }
  2263. if ((oct->chip_id == OCTEON_CN66XX || oct->chip_id == OCTEON_CN68XX) &&
  2264. ptp_enable)
  2265. oct_ptp_open(netdev);
  2266. ifstate_set(lio, LIO_IFSTATE_RUNNING);
  2267. /* Ready for link status updates */
  2268. lio->intf_open = 1;
  2269. netif_info(lio, ifup, lio->netdev, "Interface Open, ready for traffic\n");
  2270. if (OCTEON_CN23XX_PF(oct)) {
  2271. if (!oct->msix_on)
  2272. if (setup_tx_poll_fn(netdev))
  2273. return -1;
  2274. } else {
  2275. if (setup_tx_poll_fn(netdev))
  2276. return -1;
  2277. }
  2278. start_txq(netdev);
  2279. /* tell Octeon to start forwarding packets to host */
  2280. send_rx_ctrl_cmd(lio, 1);
  2281. dev_info(&oct->pci_dev->dev, "%s interface is opened\n",
  2282. netdev->name);
  2283. return 0;
  2284. }
  2285. /**
  2286. * \brief Net device stop for LiquidIO
  2287. * @param netdev network device
  2288. */
  2289. static int liquidio_stop(struct net_device *netdev)
  2290. {
  2291. struct lio *lio = GET_LIO(netdev);
  2292. struct octeon_device *oct = lio->oct_dev;
  2293. ifstate_reset(lio, LIO_IFSTATE_RUNNING);
  2294. netif_tx_disable(netdev);
  2295. /* Inform that netif carrier is down */
  2296. netif_carrier_off(netdev);
  2297. lio->intf_open = 0;
  2298. lio->linfo.link.s.link_up = 0;
  2299. lio->link_changes++;
  2300. /* Tell Octeon that nic interface is down. */
  2301. send_rx_ctrl_cmd(lio, 0);
  2302. if (OCTEON_CN23XX_PF(oct)) {
  2303. if (!oct->msix_on)
  2304. cleanup_tx_poll_fn(netdev);
  2305. } else {
  2306. cleanup_tx_poll_fn(netdev);
  2307. }
  2308. if (lio->ptp_clock) {
  2309. ptp_clock_unregister(lio->ptp_clock);
  2310. lio->ptp_clock = NULL;
  2311. }
  2312. dev_info(&oct->pci_dev->dev, "%s interface is stopped\n", netdev->name);
  2313. return 0;
  2314. }
  2315. /**
  2316. * \brief Converts a mask based on net device flags
  2317. * @param netdev network device
  2318. *
  2319. * This routine generates a octnet_ifflags mask from the net device flags
  2320. * received from the OS.
  2321. */
  2322. static inline enum octnet_ifflags get_new_flags(struct net_device *netdev)
  2323. {
  2324. enum octnet_ifflags f = OCTNET_IFFLAG_UNICAST;
  2325. if (netdev->flags & IFF_PROMISC)
  2326. f |= OCTNET_IFFLAG_PROMISC;
  2327. if (netdev->flags & IFF_ALLMULTI)
  2328. f |= OCTNET_IFFLAG_ALLMULTI;
  2329. if (netdev->flags & IFF_MULTICAST) {
  2330. f |= OCTNET_IFFLAG_MULTICAST;
  2331. /* Accept all multicast addresses if there are more than we
  2332. * can handle
  2333. */
  2334. if (netdev_mc_count(netdev) > MAX_OCTEON_MULTICAST_ADDR)
  2335. f |= OCTNET_IFFLAG_ALLMULTI;
  2336. }
  2337. if (netdev->flags & IFF_BROADCAST)
  2338. f |= OCTNET_IFFLAG_BROADCAST;
  2339. return f;
  2340. }
  2341. /**
  2342. * \brief Net device set_multicast_list
  2343. * @param netdev network device
  2344. */
  2345. static void liquidio_set_mcast_list(struct net_device *netdev)
  2346. {
  2347. struct lio *lio = GET_LIO(netdev);
  2348. struct octeon_device *oct = lio->oct_dev;
  2349. struct octnic_ctrl_pkt nctrl;
  2350. struct netdev_hw_addr *ha;
  2351. u64 *mc;
  2352. int ret;
  2353. int mc_count = min(netdev_mc_count(netdev), MAX_OCTEON_MULTICAST_ADDR);
  2354. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2355. /* Create a ctrl pkt command to be sent to core app. */
  2356. nctrl.ncmd.u64 = 0;
  2357. nctrl.ncmd.s.cmd = OCTNET_CMD_SET_MULTI_LIST;
  2358. nctrl.ncmd.s.param1 = get_new_flags(netdev);
  2359. nctrl.ncmd.s.param2 = mc_count;
  2360. nctrl.ncmd.s.more = mc_count;
  2361. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2362. nctrl.netpndev = (u64)netdev;
  2363. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2364. /* copy all the addresses into the udd */
  2365. mc = &nctrl.udd[0];
  2366. netdev_for_each_mc_addr(ha, netdev) {
  2367. *mc = 0;
  2368. memcpy(((u8 *)mc) + 2, ha->addr, ETH_ALEN);
  2369. /* no need to swap bytes */
  2370. if (++mc > &nctrl.udd[mc_count])
  2371. break;
  2372. }
  2373. /* Apparently, any activity in this call from the kernel has to
  2374. * be atomic. So we won't wait for response.
  2375. */
  2376. nctrl.wait_time = 0;
  2377. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2378. if (ret < 0) {
  2379. dev_err(&oct->pci_dev->dev, "DEVFLAGS change failed in core (ret: 0x%x)\n",
  2380. ret);
  2381. }
  2382. }
  2383. /**
  2384. * \brief Net device set_mac_address
  2385. * @param netdev network device
  2386. */
  2387. static int liquidio_set_mac(struct net_device *netdev, void *p)
  2388. {
  2389. int ret = 0;
  2390. struct lio *lio = GET_LIO(netdev);
  2391. struct octeon_device *oct = lio->oct_dev;
  2392. struct sockaddr *addr = (struct sockaddr *)p;
  2393. struct octnic_ctrl_pkt nctrl;
  2394. if (!is_valid_ether_addr(addr->sa_data))
  2395. return -EADDRNOTAVAIL;
  2396. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2397. nctrl.ncmd.u64 = 0;
  2398. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
  2399. nctrl.ncmd.s.param1 = 0;
  2400. nctrl.ncmd.s.more = 1;
  2401. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2402. nctrl.netpndev = (u64)netdev;
  2403. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2404. nctrl.wait_time = 100;
  2405. nctrl.udd[0] = 0;
  2406. /* The MAC Address is presented in network byte order. */
  2407. memcpy((u8 *)&nctrl.udd[0] + 2, addr->sa_data, ETH_ALEN);
  2408. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2409. if (ret < 0) {
  2410. dev_err(&oct->pci_dev->dev, "MAC Address change failed\n");
  2411. return -ENOMEM;
  2412. }
  2413. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  2414. memcpy(((u8 *)&lio->linfo.hw_addr) + 2, addr->sa_data, ETH_ALEN);
  2415. return 0;
  2416. }
  2417. /**
  2418. * \brief Net device get_stats
  2419. * @param netdev network device
  2420. */
  2421. static struct net_device_stats *liquidio_get_stats(struct net_device *netdev)
  2422. {
  2423. struct lio *lio = GET_LIO(netdev);
  2424. struct net_device_stats *stats = &netdev->stats;
  2425. struct octeon_device *oct;
  2426. u64 pkts = 0, drop = 0, bytes = 0;
  2427. struct oct_droq_stats *oq_stats;
  2428. struct oct_iq_stats *iq_stats;
  2429. int i, iq_no, oq_no;
  2430. oct = lio->oct_dev;
  2431. for (i = 0; i < lio->linfo.num_txpciq; i++) {
  2432. iq_no = lio->linfo.txpciq[i].s.q_no;
  2433. iq_stats = &oct->instr_queue[iq_no]->stats;
  2434. pkts += iq_stats->tx_done;
  2435. drop += iq_stats->tx_dropped;
  2436. bytes += iq_stats->tx_tot_bytes;
  2437. }
  2438. stats->tx_packets = pkts;
  2439. stats->tx_bytes = bytes;
  2440. stats->tx_dropped = drop;
  2441. pkts = 0;
  2442. drop = 0;
  2443. bytes = 0;
  2444. for (i = 0; i < lio->linfo.num_rxpciq; i++) {
  2445. oq_no = lio->linfo.rxpciq[i].s.q_no;
  2446. oq_stats = &oct->droq[oq_no]->stats;
  2447. pkts += oq_stats->rx_pkts_received;
  2448. drop += (oq_stats->rx_dropped +
  2449. oq_stats->dropped_nodispatch +
  2450. oq_stats->dropped_toomany +
  2451. oq_stats->dropped_nomem);
  2452. bytes += oq_stats->rx_bytes_received;
  2453. }
  2454. stats->rx_bytes = bytes;
  2455. stats->rx_packets = pkts;
  2456. stats->rx_dropped = drop;
  2457. return stats;
  2458. }
  2459. /**
  2460. * \brief Net device change_mtu
  2461. * @param netdev network device
  2462. */
  2463. static int liquidio_change_mtu(struct net_device *netdev, int new_mtu)
  2464. {
  2465. struct lio *lio = GET_LIO(netdev);
  2466. struct octeon_device *oct = lio->oct_dev;
  2467. struct octnic_ctrl_pkt nctrl;
  2468. int ret = 0;
  2469. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2470. nctrl.ncmd.u64 = 0;
  2471. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MTU;
  2472. nctrl.ncmd.s.param1 = new_mtu;
  2473. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2474. nctrl.wait_time = 100;
  2475. nctrl.netpndev = (u64)netdev;
  2476. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2477. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2478. if (ret < 0) {
  2479. dev_err(&oct->pci_dev->dev, "Failed to set MTU\n");
  2480. return -1;
  2481. }
  2482. lio->mtu = new_mtu;
  2483. return 0;
  2484. }
  2485. /**
  2486. * \brief Handler for SIOCSHWTSTAMP ioctl
  2487. * @param netdev network device
  2488. * @param ifr interface request
  2489. * @param cmd command
  2490. */
  2491. static int hwtstamp_ioctl(struct net_device *netdev, struct ifreq *ifr)
  2492. {
  2493. struct hwtstamp_config conf;
  2494. struct lio *lio = GET_LIO(netdev);
  2495. if (copy_from_user(&conf, ifr->ifr_data, sizeof(conf)))
  2496. return -EFAULT;
  2497. if (conf.flags)
  2498. return -EINVAL;
  2499. switch (conf.tx_type) {
  2500. case HWTSTAMP_TX_ON:
  2501. case HWTSTAMP_TX_OFF:
  2502. break;
  2503. default:
  2504. return -ERANGE;
  2505. }
  2506. switch (conf.rx_filter) {
  2507. case HWTSTAMP_FILTER_NONE:
  2508. break;
  2509. case HWTSTAMP_FILTER_ALL:
  2510. case HWTSTAMP_FILTER_SOME:
  2511. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  2512. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  2513. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  2514. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  2515. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  2516. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  2517. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  2518. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  2519. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  2520. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  2521. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  2522. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  2523. conf.rx_filter = HWTSTAMP_FILTER_ALL;
  2524. break;
  2525. default:
  2526. return -ERANGE;
  2527. }
  2528. if (conf.rx_filter == HWTSTAMP_FILTER_ALL)
  2529. ifstate_set(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  2530. else
  2531. ifstate_reset(lio, LIO_IFSTATE_RX_TIMESTAMP_ENABLED);
  2532. return copy_to_user(ifr->ifr_data, &conf, sizeof(conf)) ? -EFAULT : 0;
  2533. }
  2534. /**
  2535. * \brief ioctl handler
  2536. * @param netdev network device
  2537. * @param ifr interface request
  2538. * @param cmd command
  2539. */
  2540. static int liquidio_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2541. {
  2542. struct lio *lio = GET_LIO(netdev);
  2543. switch (cmd) {
  2544. case SIOCSHWTSTAMP:
  2545. if ((lio->oct_dev->chip_id == OCTEON_CN66XX ||
  2546. lio->oct_dev->chip_id == OCTEON_CN68XX) && ptp_enable)
  2547. return hwtstamp_ioctl(netdev, ifr);
  2548. default:
  2549. return -EOPNOTSUPP;
  2550. }
  2551. }
  2552. /**
  2553. * \brief handle a Tx timestamp response
  2554. * @param status response status
  2555. * @param buf pointer to skb
  2556. */
  2557. static void handle_timestamp(struct octeon_device *oct,
  2558. u32 status,
  2559. void *buf)
  2560. {
  2561. struct octnet_buf_free_info *finfo;
  2562. struct octeon_soft_command *sc;
  2563. struct oct_timestamp_resp *resp;
  2564. struct lio *lio;
  2565. struct sk_buff *skb = (struct sk_buff *)buf;
  2566. finfo = (struct octnet_buf_free_info *)skb->cb;
  2567. lio = finfo->lio;
  2568. sc = finfo->sc;
  2569. oct = lio->oct_dev;
  2570. resp = (struct oct_timestamp_resp *)sc->virtrptr;
  2571. if (status != OCTEON_REQUEST_DONE) {
  2572. dev_err(&oct->pci_dev->dev, "Tx timestamp instruction failed. Status: %llx\n",
  2573. CVM_CAST64(status));
  2574. resp->timestamp = 0;
  2575. }
  2576. octeon_swap_8B_data(&resp->timestamp, 1);
  2577. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) != 0)) {
  2578. struct skb_shared_hwtstamps ts;
  2579. u64 ns = resp->timestamp;
  2580. netif_info(lio, tx_done, lio->netdev,
  2581. "Got resulting SKBTX_HW_TSTAMP skb=%p ns=%016llu\n",
  2582. skb, (unsigned long long)ns);
  2583. ts.hwtstamp = ns_to_ktime(ns + lio->ptp_adjust);
  2584. skb_tstamp_tx(skb, &ts);
  2585. }
  2586. octeon_free_soft_command(oct, sc);
  2587. tx_buffer_free(skb);
  2588. }
  2589. /* \brief Send a data packet that will be timestamped
  2590. * @param oct octeon device
  2591. * @param ndata pointer to network data
  2592. * @param finfo pointer to private network data
  2593. */
  2594. static inline int send_nic_timestamp_pkt(struct octeon_device *oct,
  2595. struct octnic_data_pkt *ndata,
  2596. struct octnet_buf_free_info *finfo)
  2597. {
  2598. int retval;
  2599. struct octeon_soft_command *sc;
  2600. struct lio *lio;
  2601. int ring_doorbell;
  2602. u32 len;
  2603. lio = finfo->lio;
  2604. sc = octeon_alloc_soft_command_resp(oct, &ndata->cmd,
  2605. sizeof(struct oct_timestamp_resp));
  2606. finfo->sc = sc;
  2607. if (!sc) {
  2608. dev_err(&oct->pci_dev->dev, "No memory for timestamped data packet\n");
  2609. return IQ_SEND_FAILED;
  2610. }
  2611. if (ndata->reqtype == REQTYPE_NORESP_NET)
  2612. ndata->reqtype = REQTYPE_RESP_NET;
  2613. else if (ndata->reqtype == REQTYPE_NORESP_NET_SG)
  2614. ndata->reqtype = REQTYPE_RESP_NET_SG;
  2615. sc->callback = handle_timestamp;
  2616. sc->callback_arg = finfo->skb;
  2617. sc->iq_no = ndata->q_no;
  2618. if (OCTEON_CN23XX_PF(oct))
  2619. len = (u32)((struct octeon_instr_ih3 *)
  2620. (&sc->cmd.cmd3.ih3))->dlengsz;
  2621. else
  2622. len = (u32)((struct octeon_instr_ih2 *)
  2623. (&sc->cmd.cmd2.ih2))->dlengsz;
  2624. ring_doorbell = 1;
  2625. retval = octeon_send_command(oct, sc->iq_no, ring_doorbell, &sc->cmd,
  2626. sc, len, ndata->reqtype);
  2627. if (retval == IQ_SEND_FAILED) {
  2628. dev_err(&oct->pci_dev->dev, "timestamp data packet failed status: %x\n",
  2629. retval);
  2630. octeon_free_soft_command(oct, sc);
  2631. } else {
  2632. netif_info(lio, tx_queued, lio->netdev, "Queued timestamp packet\n");
  2633. }
  2634. return retval;
  2635. }
  2636. /** \brief Transmit networks packets to the Octeon interface
  2637. * @param skbuff skbuff struct to be passed to network layer.
  2638. * @param netdev pointer to network device
  2639. * @returns whether the packet was transmitted to the device okay or not
  2640. * (NETDEV_TX_OK or NETDEV_TX_BUSY)
  2641. */
  2642. static int liquidio_xmit(struct sk_buff *skb, struct net_device *netdev)
  2643. {
  2644. struct lio *lio;
  2645. struct octnet_buf_free_info *finfo;
  2646. union octnic_cmd_setup cmdsetup;
  2647. struct octnic_data_pkt ndata;
  2648. struct octeon_device *oct;
  2649. struct oct_iq_stats *stats;
  2650. struct octeon_instr_irh *irh;
  2651. union tx_info *tx_info;
  2652. int status = 0;
  2653. int q_idx = 0, iq_no = 0;
  2654. int j;
  2655. u64 dptr = 0;
  2656. u32 tag = 0;
  2657. lio = GET_LIO(netdev);
  2658. oct = lio->oct_dev;
  2659. if (netif_is_multiqueue(netdev)) {
  2660. q_idx = skb->queue_mapping;
  2661. q_idx = (q_idx % (lio->linfo.num_txpciq));
  2662. tag = q_idx;
  2663. iq_no = lio->linfo.txpciq[q_idx].s.q_no;
  2664. } else {
  2665. iq_no = lio->txq;
  2666. }
  2667. stats = &oct->instr_queue[iq_no]->stats;
  2668. /* Check for all conditions in which the current packet cannot be
  2669. * transmitted.
  2670. */
  2671. if (!(atomic_read(&lio->ifstate) & LIO_IFSTATE_RUNNING) ||
  2672. (!lio->linfo.link.s.link_up) ||
  2673. (skb->len <= 0)) {
  2674. netif_info(lio, tx_err, lio->netdev,
  2675. "Transmit failed link_status : %d\n",
  2676. lio->linfo.link.s.link_up);
  2677. goto lio_xmit_failed;
  2678. }
  2679. /* Use space in skb->cb to store info used to unmap and
  2680. * free the buffers.
  2681. */
  2682. finfo = (struct octnet_buf_free_info *)skb->cb;
  2683. finfo->lio = lio;
  2684. finfo->skb = skb;
  2685. finfo->sc = NULL;
  2686. /* Prepare the attributes for the data to be passed to OSI. */
  2687. memset(&ndata, 0, sizeof(struct octnic_data_pkt));
  2688. ndata.buf = (void *)finfo;
  2689. ndata.q_no = iq_no;
  2690. if (netif_is_multiqueue(netdev)) {
  2691. if (octnet_iq_is_full(oct, ndata.q_no)) {
  2692. /* defer sending if queue is full */
  2693. netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
  2694. ndata.q_no);
  2695. stats->tx_iq_busy++;
  2696. return NETDEV_TX_BUSY;
  2697. }
  2698. } else {
  2699. if (octnet_iq_is_full(oct, lio->txq)) {
  2700. /* defer sending if queue is full */
  2701. stats->tx_iq_busy++;
  2702. netif_info(lio, tx_err, lio->netdev, "Transmit failed iq:%d full\n",
  2703. lio->txq);
  2704. return NETDEV_TX_BUSY;
  2705. }
  2706. }
  2707. /* pr_info(" XMIT - valid Qs: %d, 1st Q no: %d, cpu: %d, q_no:%d\n",
  2708. * lio->linfo.num_txpciq, lio->txq, cpu, ndata.q_no);
  2709. */
  2710. ndata.datasize = skb->len;
  2711. cmdsetup.u64 = 0;
  2712. cmdsetup.s.iq_no = iq_no;
  2713. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2714. if (skb->encapsulation) {
  2715. cmdsetup.s.tnl_csum = 1;
  2716. stats->tx_vxlan++;
  2717. } else {
  2718. cmdsetup.s.transport_csum = 1;
  2719. }
  2720. }
  2721. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  2722. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2723. cmdsetup.s.timestamp = 1;
  2724. }
  2725. if (skb_shinfo(skb)->nr_frags == 0) {
  2726. cmdsetup.s.u.datasize = skb->len;
  2727. octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
  2728. /* Offload checksum calculation for TCP/UDP packets */
  2729. dptr = dma_map_single(&oct->pci_dev->dev,
  2730. skb->data,
  2731. skb->len,
  2732. DMA_TO_DEVICE);
  2733. if (dma_mapping_error(&oct->pci_dev->dev, dptr)) {
  2734. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 1\n",
  2735. __func__);
  2736. return NETDEV_TX_BUSY;
  2737. }
  2738. if (OCTEON_CN23XX_PF(oct))
  2739. ndata.cmd.cmd3.dptr = dptr;
  2740. else
  2741. ndata.cmd.cmd2.dptr = dptr;
  2742. finfo->dptr = dptr;
  2743. ndata.reqtype = REQTYPE_NORESP_NET;
  2744. } else {
  2745. int i, frags;
  2746. struct skb_frag_struct *frag;
  2747. struct octnic_gather *g;
  2748. spin_lock(&lio->glist_lock[q_idx]);
  2749. g = (struct octnic_gather *)
  2750. list_delete_head(&lio->glist[q_idx]);
  2751. spin_unlock(&lio->glist_lock[q_idx]);
  2752. if (!g) {
  2753. netif_info(lio, tx_err, lio->netdev,
  2754. "Transmit scatter gather: glist null!\n");
  2755. goto lio_xmit_failed;
  2756. }
  2757. cmdsetup.s.gather = 1;
  2758. cmdsetup.s.u.gatherptrs = (skb_shinfo(skb)->nr_frags + 1);
  2759. octnet_prepare_pci_cmd(oct, &ndata.cmd, &cmdsetup, tag);
  2760. memset(g->sg, 0, g->sg_size);
  2761. g->sg[0].ptr[0] = dma_map_single(&oct->pci_dev->dev,
  2762. skb->data,
  2763. (skb->len - skb->data_len),
  2764. DMA_TO_DEVICE);
  2765. if (dma_mapping_error(&oct->pci_dev->dev, g->sg[0].ptr[0])) {
  2766. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 2\n",
  2767. __func__);
  2768. return NETDEV_TX_BUSY;
  2769. }
  2770. add_sg_size(&g->sg[0], (skb->len - skb->data_len), 0);
  2771. frags = skb_shinfo(skb)->nr_frags;
  2772. i = 1;
  2773. while (frags--) {
  2774. frag = &skb_shinfo(skb)->frags[i - 1];
  2775. g->sg[(i >> 2)].ptr[(i & 3)] =
  2776. dma_map_page(&oct->pci_dev->dev,
  2777. frag->page.p,
  2778. frag->page_offset,
  2779. frag->size,
  2780. DMA_TO_DEVICE);
  2781. if (dma_mapping_error(&oct->pci_dev->dev,
  2782. g->sg[i >> 2].ptr[i & 3])) {
  2783. dma_unmap_single(&oct->pci_dev->dev,
  2784. g->sg[0].ptr[0],
  2785. skb->len - skb->data_len,
  2786. DMA_TO_DEVICE);
  2787. for (j = 1; j < i; j++) {
  2788. frag = &skb_shinfo(skb)->frags[j - 1];
  2789. dma_unmap_page(&oct->pci_dev->dev,
  2790. g->sg[j >> 2].ptr[j & 3],
  2791. frag->size,
  2792. DMA_TO_DEVICE);
  2793. }
  2794. dev_err(&oct->pci_dev->dev, "%s DMA mapping error 3\n",
  2795. __func__);
  2796. return NETDEV_TX_BUSY;
  2797. }
  2798. add_sg_size(&g->sg[(i >> 2)], frag->size, (i & 3));
  2799. i++;
  2800. }
  2801. dptr = g->sg_dma_ptr;
  2802. if (OCTEON_CN23XX_PF(oct))
  2803. ndata.cmd.cmd3.dptr = dptr;
  2804. else
  2805. ndata.cmd.cmd2.dptr = dptr;
  2806. finfo->dptr = dptr;
  2807. finfo->g = g;
  2808. ndata.reqtype = REQTYPE_NORESP_NET_SG;
  2809. }
  2810. if (OCTEON_CN23XX_PF(oct)) {
  2811. irh = (struct octeon_instr_irh *)&ndata.cmd.cmd3.irh;
  2812. tx_info = (union tx_info *)&ndata.cmd.cmd3.ossp[0];
  2813. } else {
  2814. irh = (struct octeon_instr_irh *)&ndata.cmd.cmd2.irh;
  2815. tx_info = (union tx_info *)&ndata.cmd.cmd2.ossp[0];
  2816. }
  2817. if (skb_shinfo(skb)->gso_size) {
  2818. tx_info->s.gso_size = skb_shinfo(skb)->gso_size;
  2819. tx_info->s.gso_segs = skb_shinfo(skb)->gso_segs;
  2820. stats->tx_gso++;
  2821. }
  2822. /* HW insert VLAN tag */
  2823. if (skb_vlan_tag_present(skb)) {
  2824. irh->priority = skb_vlan_tag_get(skb) >> 13;
  2825. irh->vlan = skb_vlan_tag_get(skb) & 0xfff;
  2826. }
  2827. if (unlikely(cmdsetup.s.timestamp))
  2828. status = send_nic_timestamp_pkt(oct, &ndata, finfo);
  2829. else
  2830. status = octnet_send_nic_data_pkt(oct, &ndata);
  2831. if (status == IQ_SEND_FAILED)
  2832. goto lio_xmit_failed;
  2833. netif_info(lio, tx_queued, lio->netdev, "Transmit queued successfully\n");
  2834. if (status == IQ_SEND_STOP)
  2835. stop_q(lio->netdev, q_idx);
  2836. netif_trans_update(netdev);
  2837. if (tx_info->s.gso_segs)
  2838. stats->tx_done += tx_info->s.gso_segs;
  2839. else
  2840. stats->tx_done++;
  2841. stats->tx_tot_bytes += ndata.datasize;
  2842. return NETDEV_TX_OK;
  2843. lio_xmit_failed:
  2844. stats->tx_dropped++;
  2845. netif_info(lio, tx_err, lio->netdev, "IQ%d Transmit dropped:%llu\n",
  2846. iq_no, stats->tx_dropped);
  2847. if (dptr)
  2848. dma_unmap_single(&oct->pci_dev->dev, dptr,
  2849. ndata.datasize, DMA_TO_DEVICE);
  2850. tx_buffer_free(skb);
  2851. return NETDEV_TX_OK;
  2852. }
  2853. /** \brief Network device Tx timeout
  2854. * @param netdev pointer to network device
  2855. */
  2856. static void liquidio_tx_timeout(struct net_device *netdev)
  2857. {
  2858. struct lio *lio;
  2859. lio = GET_LIO(netdev);
  2860. netif_info(lio, tx_err, lio->netdev,
  2861. "Transmit timeout tx_dropped:%ld, waking up queues now!!\n",
  2862. netdev->stats.tx_dropped);
  2863. netif_trans_update(netdev);
  2864. txqs_wake(netdev);
  2865. }
  2866. static int liquidio_vlan_rx_add_vid(struct net_device *netdev,
  2867. __be16 proto __attribute__((unused)),
  2868. u16 vid)
  2869. {
  2870. struct lio *lio = GET_LIO(netdev);
  2871. struct octeon_device *oct = lio->oct_dev;
  2872. struct octnic_ctrl_pkt nctrl;
  2873. int ret = 0;
  2874. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2875. nctrl.ncmd.u64 = 0;
  2876. nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
  2877. nctrl.ncmd.s.param1 = vid;
  2878. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2879. nctrl.wait_time = 100;
  2880. nctrl.netpndev = (u64)netdev;
  2881. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2882. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2883. if (ret < 0) {
  2884. dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
  2885. ret);
  2886. }
  2887. return ret;
  2888. }
  2889. static int liquidio_vlan_rx_kill_vid(struct net_device *netdev,
  2890. __be16 proto __attribute__((unused)),
  2891. u16 vid)
  2892. {
  2893. struct lio *lio = GET_LIO(netdev);
  2894. struct octeon_device *oct = lio->oct_dev;
  2895. struct octnic_ctrl_pkt nctrl;
  2896. int ret = 0;
  2897. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2898. nctrl.ncmd.u64 = 0;
  2899. nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
  2900. nctrl.ncmd.s.param1 = vid;
  2901. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2902. nctrl.wait_time = 100;
  2903. nctrl.netpndev = (u64)netdev;
  2904. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2905. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2906. if (ret < 0) {
  2907. dev_err(&oct->pci_dev->dev, "Add VLAN filter failed in core (ret: 0x%x)\n",
  2908. ret);
  2909. }
  2910. return ret;
  2911. }
  2912. /** Sending command to enable/disable RX checksum offload
  2913. * @param netdev pointer to network device
  2914. * @param command OCTNET_CMD_TNL_RX_CSUM_CTL
  2915. * @param rx_cmd_bit OCTNET_CMD_RXCSUM_ENABLE/
  2916. * OCTNET_CMD_RXCSUM_DISABLE
  2917. * @returns SUCCESS or FAILURE
  2918. */
  2919. static int liquidio_set_rxcsum_command(struct net_device *netdev, int command,
  2920. u8 rx_cmd)
  2921. {
  2922. struct lio *lio = GET_LIO(netdev);
  2923. struct octeon_device *oct = lio->oct_dev;
  2924. struct octnic_ctrl_pkt nctrl;
  2925. int ret = 0;
  2926. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2927. nctrl.ncmd.u64 = 0;
  2928. nctrl.ncmd.s.cmd = command;
  2929. nctrl.ncmd.s.param1 = rx_cmd;
  2930. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2931. nctrl.wait_time = 100;
  2932. nctrl.netpndev = (u64)netdev;
  2933. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2934. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2935. if (ret < 0) {
  2936. dev_err(&oct->pci_dev->dev,
  2937. "DEVFLAGS RXCSUM change failed in core(ret:0x%x)\n",
  2938. ret);
  2939. }
  2940. return ret;
  2941. }
  2942. /** Sending command to add/delete VxLAN UDP port to firmware
  2943. * @param netdev pointer to network device
  2944. * @param command OCTNET_CMD_VXLAN_PORT_CONFIG
  2945. * @param vxlan_port VxLAN port to be added or deleted
  2946. * @param vxlan_cmd_bit OCTNET_CMD_VXLAN_PORT_ADD,
  2947. * OCTNET_CMD_VXLAN_PORT_DEL
  2948. * @returns SUCCESS or FAILURE
  2949. */
  2950. static int liquidio_vxlan_port_command(struct net_device *netdev, int command,
  2951. u16 vxlan_port, u8 vxlan_cmd_bit)
  2952. {
  2953. struct lio *lio = GET_LIO(netdev);
  2954. struct octeon_device *oct = lio->oct_dev;
  2955. struct octnic_ctrl_pkt nctrl;
  2956. int ret = 0;
  2957. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  2958. nctrl.ncmd.u64 = 0;
  2959. nctrl.ncmd.s.cmd = command;
  2960. nctrl.ncmd.s.more = vxlan_cmd_bit;
  2961. nctrl.ncmd.s.param1 = vxlan_port;
  2962. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  2963. nctrl.wait_time = 100;
  2964. nctrl.netpndev = (u64)netdev;
  2965. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  2966. ret = octnet_send_nic_ctrl_pkt(lio->oct_dev, &nctrl);
  2967. if (ret < 0) {
  2968. dev_err(&oct->pci_dev->dev,
  2969. "VxLAN port add/delete failed in core (ret:0x%x)\n",
  2970. ret);
  2971. }
  2972. return ret;
  2973. }
  2974. /** \brief Net device fix features
  2975. * @param netdev pointer to network device
  2976. * @param request features requested
  2977. * @returns updated features list
  2978. */
  2979. static netdev_features_t liquidio_fix_features(struct net_device *netdev,
  2980. netdev_features_t request)
  2981. {
  2982. struct lio *lio = netdev_priv(netdev);
  2983. if ((request & NETIF_F_RXCSUM) &&
  2984. !(lio->dev_capability & NETIF_F_RXCSUM))
  2985. request &= ~NETIF_F_RXCSUM;
  2986. if ((request & NETIF_F_HW_CSUM) &&
  2987. !(lio->dev_capability & NETIF_F_HW_CSUM))
  2988. request &= ~NETIF_F_HW_CSUM;
  2989. if ((request & NETIF_F_TSO) && !(lio->dev_capability & NETIF_F_TSO))
  2990. request &= ~NETIF_F_TSO;
  2991. if ((request & NETIF_F_TSO6) && !(lio->dev_capability & NETIF_F_TSO6))
  2992. request &= ~NETIF_F_TSO6;
  2993. if ((request & NETIF_F_LRO) && !(lio->dev_capability & NETIF_F_LRO))
  2994. request &= ~NETIF_F_LRO;
  2995. /*Disable LRO if RXCSUM is off */
  2996. if (!(request & NETIF_F_RXCSUM) && (netdev->features & NETIF_F_LRO) &&
  2997. (lio->dev_capability & NETIF_F_LRO))
  2998. request &= ~NETIF_F_LRO;
  2999. return request;
  3000. }
  3001. /** \brief Net device set features
  3002. * @param netdev pointer to network device
  3003. * @param features features to enable/disable
  3004. */
  3005. static int liquidio_set_features(struct net_device *netdev,
  3006. netdev_features_t features)
  3007. {
  3008. struct lio *lio = netdev_priv(netdev);
  3009. if (!((netdev->features ^ features) & NETIF_F_LRO))
  3010. return 0;
  3011. if ((features & NETIF_F_LRO) && (lio->dev_capability & NETIF_F_LRO))
  3012. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
  3013. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  3014. else if (!(features & NETIF_F_LRO) &&
  3015. (lio->dev_capability & NETIF_F_LRO))
  3016. liquidio_set_feature(netdev, OCTNET_CMD_LRO_DISABLE,
  3017. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  3018. /* Sending command to firmware to enable/disable RX checksum
  3019. * offload settings using ethtool
  3020. */
  3021. if (!(netdev->features & NETIF_F_RXCSUM) &&
  3022. (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
  3023. (features & NETIF_F_RXCSUM))
  3024. liquidio_set_rxcsum_command(netdev,
  3025. OCTNET_CMD_TNL_RX_CSUM_CTL,
  3026. OCTNET_CMD_RXCSUM_ENABLE);
  3027. else if ((netdev->features & NETIF_F_RXCSUM) &&
  3028. (lio->enc_dev_capability & NETIF_F_RXCSUM) &&
  3029. !(features & NETIF_F_RXCSUM))
  3030. liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
  3031. OCTNET_CMD_RXCSUM_DISABLE);
  3032. return 0;
  3033. }
  3034. static void liquidio_add_vxlan_port(struct net_device *netdev,
  3035. struct udp_tunnel_info *ti)
  3036. {
  3037. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  3038. return;
  3039. liquidio_vxlan_port_command(netdev,
  3040. OCTNET_CMD_VXLAN_PORT_CONFIG,
  3041. htons(ti->port),
  3042. OCTNET_CMD_VXLAN_PORT_ADD);
  3043. }
  3044. static void liquidio_del_vxlan_port(struct net_device *netdev,
  3045. struct udp_tunnel_info *ti)
  3046. {
  3047. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  3048. return;
  3049. liquidio_vxlan_port_command(netdev,
  3050. OCTNET_CMD_VXLAN_PORT_CONFIG,
  3051. htons(ti->port),
  3052. OCTNET_CMD_VXLAN_PORT_DEL);
  3053. }
  3054. static int __liquidio_set_vf_mac(struct net_device *netdev, int vfidx,
  3055. u8 *mac, bool is_admin_assigned)
  3056. {
  3057. struct lio *lio = GET_LIO(netdev);
  3058. struct octeon_device *oct = lio->oct_dev;
  3059. struct octnic_ctrl_pkt nctrl;
  3060. if (!is_valid_ether_addr(mac))
  3061. return -EINVAL;
  3062. if (vfidx < 0 || vfidx >= oct->sriov_info.max_vfs)
  3063. return -EINVAL;
  3064. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  3065. nctrl.ncmd.u64 = 0;
  3066. nctrl.ncmd.s.cmd = OCTNET_CMD_CHANGE_MACADDR;
  3067. /* vfidx is 0 based, but vf_num (param1) is 1 based */
  3068. nctrl.ncmd.s.param1 = vfidx + 1;
  3069. nctrl.ncmd.s.param2 = (is_admin_assigned ? 1 : 0);
  3070. nctrl.ncmd.s.more = 1;
  3071. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  3072. nctrl.netpndev = (u64)netdev;
  3073. nctrl.cb_fn = liquidio_link_ctrl_cmd_completion;
  3074. nctrl.wait_time = LIO_CMD_WAIT_TM;
  3075. nctrl.udd[0] = 0;
  3076. /* The MAC Address is presented in network byte order. */
  3077. ether_addr_copy((u8 *)&nctrl.udd[0] + 2, mac);
  3078. oct->sriov_info.vf_macaddr[vfidx] = nctrl.udd[0];
  3079. octnet_send_nic_ctrl_pkt(oct, &nctrl);
  3080. return 0;
  3081. }
  3082. static int liquidio_set_vf_mac(struct net_device *netdev, int vfidx, u8 *mac)
  3083. {
  3084. struct lio *lio = GET_LIO(netdev);
  3085. struct octeon_device *oct = lio->oct_dev;
  3086. int retval;
  3087. retval = __liquidio_set_vf_mac(netdev, vfidx, mac, true);
  3088. if (!retval)
  3089. cn23xx_tell_vf_its_macaddr_changed(oct, vfidx, mac);
  3090. return retval;
  3091. }
  3092. static int liquidio_set_vf_vlan(struct net_device *netdev, int vfidx,
  3093. u16 vlan, u8 qos, __be16 vlan_proto)
  3094. {
  3095. struct lio *lio = GET_LIO(netdev);
  3096. struct octeon_device *oct = lio->oct_dev;
  3097. struct octnic_ctrl_pkt nctrl;
  3098. u16 vlantci;
  3099. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
  3100. return -EINVAL;
  3101. if (vlan_proto != htons(ETH_P_8021Q))
  3102. return -EPROTONOSUPPORT;
  3103. if (vlan >= VLAN_N_VID || qos > 7)
  3104. return -EINVAL;
  3105. if (vlan)
  3106. vlantci = vlan | (u16)qos << VLAN_PRIO_SHIFT;
  3107. else
  3108. vlantci = 0;
  3109. if (oct->sriov_info.vf_vlantci[vfidx] == vlantci)
  3110. return 0;
  3111. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  3112. if (vlan)
  3113. nctrl.ncmd.s.cmd = OCTNET_CMD_ADD_VLAN_FILTER;
  3114. else
  3115. nctrl.ncmd.s.cmd = OCTNET_CMD_DEL_VLAN_FILTER;
  3116. nctrl.ncmd.s.param1 = vlantci;
  3117. nctrl.ncmd.s.param2 =
  3118. vfidx + 1; /* vfidx is 0 based, but vf_num (param2) is 1 based */
  3119. nctrl.ncmd.s.more = 0;
  3120. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  3121. nctrl.cb_fn = 0;
  3122. nctrl.wait_time = LIO_CMD_WAIT_TM;
  3123. octnet_send_nic_ctrl_pkt(oct, &nctrl);
  3124. oct->sriov_info.vf_vlantci[vfidx] = vlantci;
  3125. return 0;
  3126. }
  3127. static int liquidio_get_vf_config(struct net_device *netdev, int vfidx,
  3128. struct ifla_vf_info *ivi)
  3129. {
  3130. struct lio *lio = GET_LIO(netdev);
  3131. struct octeon_device *oct = lio->oct_dev;
  3132. u8 *macaddr;
  3133. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
  3134. return -EINVAL;
  3135. ivi->vf = vfidx;
  3136. macaddr = 2 + (u8 *)&oct->sriov_info.vf_macaddr[vfidx];
  3137. ether_addr_copy(&ivi->mac[0], macaddr);
  3138. ivi->vlan = oct->sriov_info.vf_vlantci[vfidx] & VLAN_VID_MASK;
  3139. ivi->qos = oct->sriov_info.vf_vlantci[vfidx] >> VLAN_PRIO_SHIFT;
  3140. ivi->linkstate = oct->sriov_info.vf_linkstate[vfidx];
  3141. return 0;
  3142. }
  3143. static int liquidio_set_vf_link_state(struct net_device *netdev, int vfidx,
  3144. int linkstate)
  3145. {
  3146. struct lio *lio = GET_LIO(netdev);
  3147. struct octeon_device *oct = lio->oct_dev;
  3148. struct octnic_ctrl_pkt nctrl;
  3149. if (vfidx < 0 || vfidx >= oct->sriov_info.num_vfs_alloced)
  3150. return -EINVAL;
  3151. if (oct->sriov_info.vf_linkstate[vfidx] == linkstate)
  3152. return 0;
  3153. memset(&nctrl, 0, sizeof(struct octnic_ctrl_pkt));
  3154. nctrl.ncmd.s.cmd = OCTNET_CMD_SET_VF_LINKSTATE;
  3155. nctrl.ncmd.s.param1 =
  3156. vfidx + 1; /* vfidx is 0 based, but vf_num (param1) is 1 based */
  3157. nctrl.ncmd.s.param2 = linkstate;
  3158. nctrl.ncmd.s.more = 0;
  3159. nctrl.iq_no = lio->linfo.txpciq[0].s.q_no;
  3160. nctrl.cb_fn = 0;
  3161. nctrl.wait_time = LIO_CMD_WAIT_TM;
  3162. octnet_send_nic_ctrl_pkt(oct, &nctrl);
  3163. oct->sriov_info.vf_linkstate[vfidx] = linkstate;
  3164. return 0;
  3165. }
  3166. static const struct net_device_ops lionetdevops = {
  3167. .ndo_open = liquidio_open,
  3168. .ndo_stop = liquidio_stop,
  3169. .ndo_start_xmit = liquidio_xmit,
  3170. .ndo_get_stats = liquidio_get_stats,
  3171. .ndo_set_mac_address = liquidio_set_mac,
  3172. .ndo_set_rx_mode = liquidio_set_mcast_list,
  3173. .ndo_tx_timeout = liquidio_tx_timeout,
  3174. .ndo_vlan_rx_add_vid = liquidio_vlan_rx_add_vid,
  3175. .ndo_vlan_rx_kill_vid = liquidio_vlan_rx_kill_vid,
  3176. .ndo_change_mtu = liquidio_change_mtu,
  3177. .ndo_do_ioctl = liquidio_ioctl,
  3178. .ndo_fix_features = liquidio_fix_features,
  3179. .ndo_set_features = liquidio_set_features,
  3180. .ndo_udp_tunnel_add = liquidio_add_vxlan_port,
  3181. .ndo_udp_tunnel_del = liquidio_del_vxlan_port,
  3182. .ndo_set_vf_mac = liquidio_set_vf_mac,
  3183. .ndo_set_vf_vlan = liquidio_set_vf_vlan,
  3184. .ndo_get_vf_config = liquidio_get_vf_config,
  3185. .ndo_set_vf_link_state = liquidio_set_vf_link_state,
  3186. };
  3187. /** \brief Entry point for the liquidio module
  3188. */
  3189. static int __init liquidio_init(void)
  3190. {
  3191. int i;
  3192. struct handshake *hs;
  3193. init_completion(&first_stage);
  3194. octeon_init_device_list(OCTEON_CONFIG_TYPE_DEFAULT);
  3195. if (liquidio_init_pci())
  3196. return -EINVAL;
  3197. wait_for_completion_timeout(&first_stage, msecs_to_jiffies(1000));
  3198. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  3199. hs = &handshake[i];
  3200. if (hs->pci_dev) {
  3201. wait_for_completion(&hs->init);
  3202. if (!hs->init_ok) {
  3203. /* init handshake failed */
  3204. dev_err(&hs->pci_dev->dev,
  3205. "Failed to init device\n");
  3206. liquidio_deinit_pci();
  3207. return -EIO;
  3208. }
  3209. }
  3210. }
  3211. for (i = 0; i < MAX_OCTEON_DEVICES; i++) {
  3212. hs = &handshake[i];
  3213. if (hs->pci_dev) {
  3214. wait_for_completion_timeout(&hs->started,
  3215. msecs_to_jiffies(30000));
  3216. if (!hs->started_ok) {
  3217. /* starter handshake failed */
  3218. dev_err(&hs->pci_dev->dev,
  3219. "Firmware failed to start\n");
  3220. liquidio_deinit_pci();
  3221. return -EIO;
  3222. }
  3223. }
  3224. }
  3225. return 0;
  3226. }
  3227. static int lio_nic_info(struct octeon_recv_info *recv_info, void *buf)
  3228. {
  3229. struct octeon_device *oct = (struct octeon_device *)buf;
  3230. struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
  3231. int gmxport = 0;
  3232. union oct_link_status *ls;
  3233. int i;
  3234. if (recv_pkt->buffer_size[0] != sizeof(*ls)) {
  3235. dev_err(&oct->pci_dev->dev, "Malformed NIC_INFO, len=%d, ifidx=%d\n",
  3236. recv_pkt->buffer_size[0],
  3237. recv_pkt->rh.r_nic_info.gmxport);
  3238. goto nic_info_err;
  3239. }
  3240. gmxport = recv_pkt->rh.r_nic_info.gmxport;
  3241. ls = (union oct_link_status *)get_rbd(recv_pkt->buffer_ptr[0]);
  3242. octeon_swap_8B_data((u64 *)ls, (sizeof(union oct_link_status)) >> 3);
  3243. for (i = 0; i < oct->ifcount; i++) {
  3244. if (oct->props[i].gmxport == gmxport) {
  3245. update_link_status(oct->props[i].netdev, ls);
  3246. break;
  3247. }
  3248. }
  3249. nic_info_err:
  3250. for (i = 0; i < recv_pkt->buffer_count; i++)
  3251. recv_buffer_free(recv_pkt->buffer_ptr[i]);
  3252. octeon_free_recv_info(recv_info);
  3253. return 0;
  3254. }
  3255. /**
  3256. * \brief Setup network interfaces
  3257. * @param octeon_dev octeon device
  3258. *
  3259. * Called during init time for each device. It assumes the NIC
  3260. * is already up and running. The link information for each
  3261. * interface is passed in link_info.
  3262. */
  3263. static int setup_nic_devices(struct octeon_device *octeon_dev)
  3264. {
  3265. struct lio *lio = NULL;
  3266. struct net_device *netdev;
  3267. u8 mac[6], i, j;
  3268. struct octeon_soft_command *sc;
  3269. struct liquidio_if_cfg_context *ctx;
  3270. struct liquidio_if_cfg_resp *resp;
  3271. struct octdev_props *props;
  3272. int retval, num_iqueues, num_oqueues;
  3273. union oct_nic_if_cfg if_cfg;
  3274. unsigned int base_queue;
  3275. unsigned int gmx_port_id;
  3276. u32 resp_size, ctx_size, data_size;
  3277. u32 ifidx_or_pfnum;
  3278. struct lio_version *vdata;
  3279. /* This is to handle link status changes */
  3280. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  3281. OPCODE_NIC_INFO,
  3282. lio_nic_info, octeon_dev);
  3283. /* REQTYPE_RESP_NET and REQTYPE_SOFT_COMMAND do not have free functions.
  3284. * They are handled directly.
  3285. */
  3286. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET,
  3287. free_netbuf);
  3288. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_NORESP_NET_SG,
  3289. free_netsgbuf);
  3290. octeon_register_reqtype_free_fn(octeon_dev, REQTYPE_RESP_NET_SG,
  3291. free_netsgbuf_with_resp);
  3292. for (i = 0; i < octeon_dev->ifcount; i++) {
  3293. resp_size = sizeof(struct liquidio_if_cfg_resp);
  3294. ctx_size = sizeof(struct liquidio_if_cfg_context);
  3295. data_size = sizeof(struct lio_version);
  3296. sc = (struct octeon_soft_command *)
  3297. octeon_alloc_soft_command(octeon_dev, data_size,
  3298. resp_size, ctx_size);
  3299. resp = (struct liquidio_if_cfg_resp *)sc->virtrptr;
  3300. ctx = (struct liquidio_if_cfg_context *)sc->ctxptr;
  3301. vdata = (struct lio_version *)sc->virtdptr;
  3302. *((u64 *)vdata) = 0;
  3303. vdata->major = cpu_to_be16(LIQUIDIO_BASE_MAJOR_VERSION);
  3304. vdata->minor = cpu_to_be16(LIQUIDIO_BASE_MINOR_VERSION);
  3305. vdata->micro = cpu_to_be16(LIQUIDIO_BASE_MICRO_VERSION);
  3306. if (OCTEON_CN23XX_PF(octeon_dev)) {
  3307. num_iqueues = octeon_dev->sriov_info.num_pf_rings;
  3308. num_oqueues = octeon_dev->sriov_info.num_pf_rings;
  3309. base_queue = octeon_dev->sriov_info.pf_srn;
  3310. gmx_port_id = octeon_dev->pf_num;
  3311. ifidx_or_pfnum = octeon_dev->pf_num;
  3312. } else {
  3313. num_iqueues = CFG_GET_NUM_TXQS_NIC_IF(
  3314. octeon_get_conf(octeon_dev), i);
  3315. num_oqueues = CFG_GET_NUM_RXQS_NIC_IF(
  3316. octeon_get_conf(octeon_dev), i);
  3317. base_queue = CFG_GET_BASE_QUE_NIC_IF(
  3318. octeon_get_conf(octeon_dev), i);
  3319. gmx_port_id = CFG_GET_GMXID_NIC_IF(
  3320. octeon_get_conf(octeon_dev), i);
  3321. ifidx_or_pfnum = i;
  3322. }
  3323. dev_dbg(&octeon_dev->pci_dev->dev,
  3324. "requesting config for interface %d, iqs %d, oqs %d\n",
  3325. ifidx_or_pfnum, num_iqueues, num_oqueues);
  3326. WRITE_ONCE(ctx->cond, 0);
  3327. ctx->octeon_id = lio_get_device_id(octeon_dev);
  3328. init_waitqueue_head(&ctx->wc);
  3329. if_cfg.u64 = 0;
  3330. if_cfg.s.num_iqueues = num_iqueues;
  3331. if_cfg.s.num_oqueues = num_oqueues;
  3332. if_cfg.s.base_queue = base_queue;
  3333. if_cfg.s.gmx_port_id = gmx_port_id;
  3334. sc->iq_no = 0;
  3335. octeon_prepare_soft_command(octeon_dev, sc, OPCODE_NIC,
  3336. OPCODE_NIC_IF_CFG, 0,
  3337. if_cfg.u64, 0);
  3338. sc->callback = if_cfg_callback;
  3339. sc->callback_arg = sc;
  3340. sc->wait_time = 3000;
  3341. retval = octeon_send_soft_command(octeon_dev, sc);
  3342. if (retval == IQ_SEND_FAILED) {
  3343. dev_err(&octeon_dev->pci_dev->dev,
  3344. "iq/oq config failed status: %x\n",
  3345. retval);
  3346. /* Soft instr is freed by driver in case of failure. */
  3347. goto setup_nic_dev_fail;
  3348. }
  3349. /* Sleep on a wait queue till the cond flag indicates that the
  3350. * response arrived or timed-out.
  3351. */
  3352. if (sleep_cond(&ctx->wc, &ctx->cond) == -EINTR) {
  3353. dev_err(&octeon_dev->pci_dev->dev, "Wait interrupted\n");
  3354. goto setup_nic_wait_intr;
  3355. }
  3356. retval = resp->status;
  3357. if (retval) {
  3358. dev_err(&octeon_dev->pci_dev->dev, "iq/oq config failed\n");
  3359. goto setup_nic_dev_fail;
  3360. }
  3361. octeon_swap_8B_data((u64 *)(&resp->cfg_info),
  3362. (sizeof(struct liquidio_if_cfg_info)) >> 3);
  3363. num_iqueues = hweight64(resp->cfg_info.iqmask);
  3364. num_oqueues = hweight64(resp->cfg_info.oqmask);
  3365. if (!(num_iqueues) || !(num_oqueues)) {
  3366. dev_err(&octeon_dev->pci_dev->dev,
  3367. "Got bad iqueues (%016llx) or oqueues (%016llx) from firmware.\n",
  3368. resp->cfg_info.iqmask,
  3369. resp->cfg_info.oqmask);
  3370. goto setup_nic_dev_fail;
  3371. }
  3372. dev_dbg(&octeon_dev->pci_dev->dev,
  3373. "interface %d, iqmask %016llx, oqmask %016llx, numiqueues %d, numoqueues %d\n",
  3374. i, resp->cfg_info.iqmask, resp->cfg_info.oqmask,
  3375. num_iqueues, num_oqueues);
  3376. netdev = alloc_etherdev_mq(LIO_SIZE, num_iqueues);
  3377. if (!netdev) {
  3378. dev_err(&octeon_dev->pci_dev->dev, "Device allocation failed\n");
  3379. goto setup_nic_dev_fail;
  3380. }
  3381. SET_NETDEV_DEV(netdev, &octeon_dev->pci_dev->dev);
  3382. /* Associate the routines that will handle different
  3383. * netdev tasks.
  3384. */
  3385. netdev->netdev_ops = &lionetdevops;
  3386. lio = GET_LIO(netdev);
  3387. memset(lio, 0, sizeof(struct lio));
  3388. lio->ifidx = ifidx_or_pfnum;
  3389. props = &octeon_dev->props[i];
  3390. props->gmxport = resp->cfg_info.linfo.gmxport;
  3391. props->netdev = netdev;
  3392. lio->linfo.num_rxpciq = num_oqueues;
  3393. lio->linfo.num_txpciq = num_iqueues;
  3394. for (j = 0; j < num_oqueues; j++) {
  3395. lio->linfo.rxpciq[j].u64 =
  3396. resp->cfg_info.linfo.rxpciq[j].u64;
  3397. }
  3398. for (j = 0; j < num_iqueues; j++) {
  3399. lio->linfo.txpciq[j].u64 =
  3400. resp->cfg_info.linfo.txpciq[j].u64;
  3401. }
  3402. lio->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
  3403. lio->linfo.gmxport = resp->cfg_info.linfo.gmxport;
  3404. lio->linfo.link.u64 = resp->cfg_info.linfo.link.u64;
  3405. lio->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  3406. if (OCTEON_CN23XX_PF(octeon_dev) ||
  3407. OCTEON_CN6XXX(octeon_dev)) {
  3408. lio->dev_capability = NETIF_F_HIGHDMA
  3409. | NETIF_F_IP_CSUM
  3410. | NETIF_F_IPV6_CSUM
  3411. | NETIF_F_SG | NETIF_F_RXCSUM
  3412. | NETIF_F_GRO
  3413. | NETIF_F_TSO | NETIF_F_TSO6
  3414. | NETIF_F_LRO;
  3415. }
  3416. netif_set_gso_max_size(netdev, OCTNIC_GSO_MAX_SIZE);
  3417. /* Copy of transmit encapsulation capabilities:
  3418. * TSO, TSO6, Checksums for this device
  3419. */
  3420. lio->enc_dev_capability = NETIF_F_IP_CSUM
  3421. | NETIF_F_IPV6_CSUM
  3422. | NETIF_F_GSO_UDP_TUNNEL
  3423. | NETIF_F_HW_CSUM | NETIF_F_SG
  3424. | NETIF_F_RXCSUM
  3425. | NETIF_F_TSO | NETIF_F_TSO6
  3426. | NETIF_F_LRO;
  3427. netdev->hw_enc_features = (lio->enc_dev_capability &
  3428. ~NETIF_F_LRO);
  3429. lio->dev_capability |= NETIF_F_GSO_UDP_TUNNEL;
  3430. netdev->vlan_features = lio->dev_capability;
  3431. /* Add any unchangeable hw features */
  3432. lio->dev_capability |= NETIF_F_HW_VLAN_CTAG_FILTER |
  3433. NETIF_F_HW_VLAN_CTAG_RX |
  3434. NETIF_F_HW_VLAN_CTAG_TX;
  3435. netdev->features = (lio->dev_capability & ~NETIF_F_LRO);
  3436. netdev->hw_features = lio->dev_capability;
  3437. /*HW_VLAN_RX and HW_VLAN_FILTER is always on*/
  3438. netdev->hw_features = netdev->hw_features &
  3439. ~NETIF_F_HW_VLAN_CTAG_RX;
  3440. /* MTU range: 68 - 16000 */
  3441. netdev->min_mtu = LIO_MIN_MTU_SIZE;
  3442. netdev->max_mtu = LIO_MAX_MTU_SIZE;
  3443. /* Point to the properties for octeon device to which this
  3444. * interface belongs.
  3445. */
  3446. lio->oct_dev = octeon_dev;
  3447. lio->octprops = props;
  3448. lio->netdev = netdev;
  3449. dev_dbg(&octeon_dev->pci_dev->dev,
  3450. "if%d gmx: %d hw_addr: 0x%llx\n", i,
  3451. lio->linfo.gmxport, CVM_CAST64(lio->linfo.hw_addr));
  3452. for (j = 0; j < octeon_dev->sriov_info.max_vfs; j++) {
  3453. u8 vfmac[ETH_ALEN];
  3454. random_ether_addr(&vfmac[0]);
  3455. if (__liquidio_set_vf_mac(netdev, j,
  3456. &vfmac[0], false)) {
  3457. dev_err(&octeon_dev->pci_dev->dev,
  3458. "Error setting VF%d MAC address\n",
  3459. j);
  3460. goto setup_nic_dev_fail;
  3461. }
  3462. }
  3463. /* 64-bit swap required on LE machines */
  3464. octeon_swap_8B_data(&lio->linfo.hw_addr, 1);
  3465. for (j = 0; j < 6; j++)
  3466. mac[j] = *((u8 *)(((u8 *)&lio->linfo.hw_addr) + 2 + j));
  3467. /* Copy MAC Address to OS network device structure */
  3468. ether_addr_copy(netdev->dev_addr, mac);
  3469. /* By default all interfaces on a single Octeon uses the same
  3470. * tx and rx queues
  3471. */
  3472. lio->txq = lio->linfo.txpciq[0].s.q_no;
  3473. lio->rxq = lio->linfo.rxpciq[0].s.q_no;
  3474. if (setup_io_queues(octeon_dev, i)) {
  3475. dev_err(&octeon_dev->pci_dev->dev, "I/O queues creation failed\n");
  3476. goto setup_nic_dev_fail;
  3477. }
  3478. ifstate_set(lio, LIO_IFSTATE_DROQ_OPS);
  3479. lio->tx_qsize = octeon_get_tx_qsize(octeon_dev, lio->txq);
  3480. lio->rx_qsize = octeon_get_rx_qsize(octeon_dev, lio->rxq);
  3481. if (setup_glists(octeon_dev, lio, num_iqueues)) {
  3482. dev_err(&octeon_dev->pci_dev->dev,
  3483. "Gather list allocation failed\n");
  3484. goto setup_nic_dev_fail;
  3485. }
  3486. /* Register ethtool support */
  3487. liquidio_set_ethtool_ops(netdev);
  3488. if (lio->oct_dev->chip_id == OCTEON_CN23XX_PF_VID)
  3489. octeon_dev->priv_flags = OCT_PRIV_FLAG_DEFAULT;
  3490. else
  3491. octeon_dev->priv_flags = 0x0;
  3492. if (netdev->features & NETIF_F_LRO)
  3493. liquidio_set_feature(netdev, OCTNET_CMD_LRO_ENABLE,
  3494. OCTNIC_LROIPV4 | OCTNIC_LROIPV6);
  3495. liquidio_set_feature(netdev, OCTNET_CMD_ENABLE_VLAN_FILTER, 0);
  3496. if ((debug != -1) && (debug & NETIF_MSG_HW))
  3497. liquidio_set_feature(netdev,
  3498. OCTNET_CMD_VERBOSE_ENABLE, 0);
  3499. if (setup_link_status_change_wq(netdev))
  3500. goto setup_nic_dev_fail;
  3501. if (setup_rx_oom_poll_fn(netdev))
  3502. goto setup_nic_dev_fail;
  3503. /* Register the network device with the OS */
  3504. if (register_netdev(netdev)) {
  3505. dev_err(&octeon_dev->pci_dev->dev, "Device registration failed\n");
  3506. goto setup_nic_dev_fail;
  3507. }
  3508. dev_dbg(&octeon_dev->pci_dev->dev,
  3509. "Setup NIC ifidx:%d mac:%02x%02x%02x%02x%02x%02x\n",
  3510. i, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  3511. netif_carrier_off(netdev);
  3512. lio->link_changes++;
  3513. ifstate_set(lio, LIO_IFSTATE_REGISTERED);
  3514. /* Sending command to firmware to enable Rx checksum offload
  3515. * by default at the time of setup of Liquidio driver for
  3516. * this device
  3517. */
  3518. liquidio_set_rxcsum_command(netdev, OCTNET_CMD_TNL_RX_CSUM_CTL,
  3519. OCTNET_CMD_RXCSUM_ENABLE);
  3520. liquidio_set_feature(netdev, OCTNET_CMD_TNL_TX_CSUM_CTL,
  3521. OCTNET_CMD_TXCSUM_ENABLE);
  3522. dev_dbg(&octeon_dev->pci_dev->dev,
  3523. "NIC ifidx:%d Setup successful\n", i);
  3524. octeon_free_soft_command(octeon_dev, sc);
  3525. }
  3526. return 0;
  3527. setup_nic_dev_fail:
  3528. octeon_free_soft_command(octeon_dev, sc);
  3529. setup_nic_wait_intr:
  3530. while (i--) {
  3531. dev_err(&octeon_dev->pci_dev->dev,
  3532. "NIC ifidx:%d Setup failed\n", i);
  3533. liquidio_destroy_nic_device(octeon_dev, i);
  3534. }
  3535. return -ENODEV;
  3536. }
  3537. #ifdef CONFIG_PCI_IOV
  3538. static int octeon_enable_sriov(struct octeon_device *oct)
  3539. {
  3540. unsigned int num_vfs_alloced = oct->sriov_info.num_vfs_alloced;
  3541. struct pci_dev *vfdev;
  3542. int err;
  3543. u32 u;
  3544. if (OCTEON_CN23XX_PF(oct) && num_vfs_alloced) {
  3545. err = pci_enable_sriov(oct->pci_dev,
  3546. oct->sriov_info.num_vfs_alloced);
  3547. if (err) {
  3548. dev_err(&oct->pci_dev->dev,
  3549. "OCTEON: Failed to enable PCI sriov: %d\n",
  3550. err);
  3551. oct->sriov_info.num_vfs_alloced = 0;
  3552. return err;
  3553. }
  3554. oct->sriov_info.sriov_enabled = 1;
  3555. /* init lookup table that maps DPI ring number to VF pci_dev
  3556. * struct pointer
  3557. */
  3558. u = 0;
  3559. vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
  3560. OCTEON_CN23XX_VF_VID, NULL);
  3561. while (vfdev) {
  3562. if (vfdev->is_virtfn &&
  3563. (vfdev->physfn == oct->pci_dev)) {
  3564. oct->sriov_info.dpiring_to_vfpcidev_lut[u] =
  3565. vfdev;
  3566. u += oct->sriov_info.rings_per_vf;
  3567. }
  3568. vfdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
  3569. OCTEON_CN23XX_VF_VID, vfdev);
  3570. }
  3571. }
  3572. return num_vfs_alloced;
  3573. }
  3574. static int lio_pci_sriov_disable(struct octeon_device *oct)
  3575. {
  3576. int u;
  3577. if (pci_vfs_assigned(oct->pci_dev)) {
  3578. dev_err(&oct->pci_dev->dev, "VFs are still assigned to VMs.\n");
  3579. return -EPERM;
  3580. }
  3581. pci_disable_sriov(oct->pci_dev);
  3582. u = 0;
  3583. while (u < MAX_POSSIBLE_VFS) {
  3584. oct->sriov_info.dpiring_to_vfpcidev_lut[u] = NULL;
  3585. u += oct->sriov_info.rings_per_vf;
  3586. }
  3587. oct->sriov_info.num_vfs_alloced = 0;
  3588. dev_info(&oct->pci_dev->dev, "oct->pf_num:%d disabled VFs\n",
  3589. oct->pf_num);
  3590. return 0;
  3591. }
  3592. static int liquidio_enable_sriov(struct pci_dev *dev, int num_vfs)
  3593. {
  3594. struct octeon_device *oct = pci_get_drvdata(dev);
  3595. int ret = 0;
  3596. if ((num_vfs == oct->sriov_info.num_vfs_alloced) &&
  3597. (oct->sriov_info.sriov_enabled)) {
  3598. dev_info(&oct->pci_dev->dev, "oct->pf_num:%d already enabled num_vfs:%d\n",
  3599. oct->pf_num, num_vfs);
  3600. return 0;
  3601. }
  3602. if (!num_vfs) {
  3603. ret = lio_pci_sriov_disable(oct);
  3604. } else if (num_vfs > oct->sriov_info.max_vfs) {
  3605. dev_err(&oct->pci_dev->dev,
  3606. "OCTEON: Max allowed VFs:%d user requested:%d",
  3607. oct->sriov_info.max_vfs, num_vfs);
  3608. ret = -EPERM;
  3609. } else {
  3610. oct->sriov_info.num_vfs_alloced = num_vfs;
  3611. ret = octeon_enable_sriov(oct);
  3612. dev_info(&oct->pci_dev->dev, "oct->pf_num:%d num_vfs:%d\n",
  3613. oct->pf_num, num_vfs);
  3614. }
  3615. return ret;
  3616. }
  3617. #endif
  3618. /**
  3619. * \brief initialize the NIC
  3620. * @param oct octeon device
  3621. *
  3622. * This initialization routine is called once the Octeon device application is
  3623. * up and running
  3624. */
  3625. static int liquidio_init_nic_module(struct octeon_device *oct)
  3626. {
  3627. int i, retval = 0;
  3628. int num_nic_ports = CFG_GET_NUM_NIC_PORTS(octeon_get_conf(oct));
  3629. dev_dbg(&oct->pci_dev->dev, "Initializing network interfaces\n");
  3630. /* only default iq and oq were initialized
  3631. * initialize the rest as well
  3632. */
  3633. /* run port_config command for each port */
  3634. oct->ifcount = num_nic_ports;
  3635. memset(oct->props, 0, sizeof(struct octdev_props) * num_nic_ports);
  3636. for (i = 0; i < MAX_OCTEON_LINKS; i++)
  3637. oct->props[i].gmxport = -1;
  3638. retval = setup_nic_devices(oct);
  3639. if (retval) {
  3640. dev_err(&oct->pci_dev->dev, "Setup NIC devices failed\n");
  3641. goto octnet_init_failure;
  3642. }
  3643. liquidio_ptp_init(oct);
  3644. dev_dbg(&oct->pci_dev->dev, "Network interfaces ready\n");
  3645. return retval;
  3646. octnet_init_failure:
  3647. oct->ifcount = 0;
  3648. return retval;
  3649. }
  3650. /**
  3651. * \brief starter callback that invokes the remaining initialization work after
  3652. * the NIC is up and running.
  3653. * @param octptr work struct work_struct
  3654. */
  3655. static void nic_starter(struct work_struct *work)
  3656. {
  3657. struct octeon_device *oct;
  3658. struct cavium_wk *wk = (struct cavium_wk *)work;
  3659. oct = (struct octeon_device *)wk->ctxptr;
  3660. if (atomic_read(&oct->status) == OCT_DEV_RUNNING)
  3661. return;
  3662. /* If the status of the device is CORE_OK, the core
  3663. * application has reported its application type. Call
  3664. * any registered handlers now and move to the RUNNING
  3665. * state.
  3666. */
  3667. if (atomic_read(&oct->status) != OCT_DEV_CORE_OK) {
  3668. schedule_delayed_work(&oct->nic_poll_work.work,
  3669. LIQUIDIO_STARTER_POLL_INTERVAL_MS);
  3670. return;
  3671. }
  3672. atomic_set(&oct->status, OCT_DEV_RUNNING);
  3673. if (oct->app_mode && oct->app_mode == CVM_DRV_NIC_APP) {
  3674. dev_dbg(&oct->pci_dev->dev, "Starting NIC module\n");
  3675. if (liquidio_init_nic_module(oct))
  3676. dev_err(&oct->pci_dev->dev, "NIC initialization failed\n");
  3677. else
  3678. handshake[oct->octeon_id].started_ok = 1;
  3679. } else {
  3680. dev_err(&oct->pci_dev->dev,
  3681. "Unexpected application running on NIC (%d). Check firmware.\n",
  3682. oct->app_mode);
  3683. }
  3684. complete(&handshake[oct->octeon_id].started);
  3685. }
  3686. static int
  3687. octeon_recv_vf_drv_notice(struct octeon_recv_info *recv_info, void *buf)
  3688. {
  3689. struct octeon_device *oct = (struct octeon_device *)buf;
  3690. struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
  3691. int i, notice, vf_idx;
  3692. bool cores_crashed;
  3693. u64 *data, vf_num;
  3694. notice = recv_pkt->rh.r.ossp;
  3695. data = (u64 *)get_rbd(recv_pkt->buffer_ptr[0]);
  3696. /* the first 64-bit word of data is the vf_num */
  3697. vf_num = data[0];
  3698. octeon_swap_8B_data(&vf_num, 1);
  3699. vf_idx = (int)vf_num - 1;
  3700. cores_crashed = READ_ONCE(oct->cores_crashed);
  3701. if (notice == VF_DRV_LOADED) {
  3702. if (!(oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx))) {
  3703. oct->sriov_info.vf_drv_loaded_mask |= BIT_ULL(vf_idx);
  3704. dev_info(&oct->pci_dev->dev,
  3705. "driver for VF%d was loaded\n", vf_idx);
  3706. if (!cores_crashed)
  3707. try_module_get(THIS_MODULE);
  3708. }
  3709. } else if (notice == VF_DRV_REMOVED) {
  3710. if (oct->sriov_info.vf_drv_loaded_mask & BIT_ULL(vf_idx)) {
  3711. oct->sriov_info.vf_drv_loaded_mask &= ~BIT_ULL(vf_idx);
  3712. dev_info(&oct->pci_dev->dev,
  3713. "driver for VF%d was removed\n", vf_idx);
  3714. if (!cores_crashed)
  3715. module_put(THIS_MODULE);
  3716. }
  3717. } else if (notice == VF_DRV_MACADDR_CHANGED) {
  3718. u8 *b = (u8 *)&data[1];
  3719. oct->sriov_info.vf_macaddr[vf_idx] = data[1];
  3720. dev_info(&oct->pci_dev->dev,
  3721. "VF driver changed VF%d's MAC address to %pM\n",
  3722. vf_idx, b + 2);
  3723. }
  3724. for (i = 0; i < recv_pkt->buffer_count; i++)
  3725. recv_buffer_free(recv_pkt->buffer_ptr[i]);
  3726. octeon_free_recv_info(recv_info);
  3727. return 0;
  3728. }
  3729. /**
  3730. * \brief Device initialization for each Octeon device that is probed
  3731. * @param octeon_dev octeon device
  3732. */
  3733. static int octeon_device_init(struct octeon_device *octeon_dev)
  3734. {
  3735. int j, ret;
  3736. int fw_loaded = 0;
  3737. char bootcmd[] = "\n";
  3738. struct octeon_device_priv *oct_priv =
  3739. (struct octeon_device_priv *)octeon_dev->priv;
  3740. atomic_set(&octeon_dev->status, OCT_DEV_BEGIN_STATE);
  3741. /* Enable access to the octeon device and make its DMA capability
  3742. * known to the OS.
  3743. */
  3744. if (octeon_pci_os_setup(octeon_dev))
  3745. return 1;
  3746. atomic_set(&octeon_dev->status, OCT_DEV_PCI_ENABLE_DONE);
  3747. /* Identify the Octeon type and map the BAR address space. */
  3748. if (octeon_chip_specific_setup(octeon_dev)) {
  3749. dev_err(&octeon_dev->pci_dev->dev, "Chip specific setup failed\n");
  3750. return 1;
  3751. }
  3752. atomic_set(&octeon_dev->status, OCT_DEV_PCI_MAP_DONE);
  3753. octeon_dev->app_mode = CVM_DRV_INVALID_APP;
  3754. if (OCTEON_CN23XX_PF(octeon_dev)) {
  3755. if (!cn23xx_fw_loaded(octeon_dev)) {
  3756. fw_loaded = 0;
  3757. if (!fw_type_is_none()) {
  3758. /* Do a soft reset of the Octeon device. */
  3759. if (octeon_dev->fn_list.soft_reset(octeon_dev))
  3760. return 1;
  3761. /* things might have changed */
  3762. if (!cn23xx_fw_loaded(octeon_dev))
  3763. fw_loaded = 0;
  3764. else
  3765. fw_loaded = 1;
  3766. }
  3767. } else {
  3768. fw_loaded = 1;
  3769. }
  3770. } else if (octeon_dev->fn_list.soft_reset(octeon_dev)) {
  3771. return 1;
  3772. }
  3773. /* Initialize the dispatch mechanism used to push packets arriving on
  3774. * Octeon Output queues.
  3775. */
  3776. if (octeon_init_dispatch_list(octeon_dev))
  3777. return 1;
  3778. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  3779. OPCODE_NIC_CORE_DRV_ACTIVE,
  3780. octeon_core_drv_init,
  3781. octeon_dev);
  3782. octeon_register_dispatch_fn(octeon_dev, OPCODE_NIC,
  3783. OPCODE_NIC_VF_DRV_NOTICE,
  3784. octeon_recv_vf_drv_notice, octeon_dev);
  3785. INIT_DELAYED_WORK(&octeon_dev->nic_poll_work.work, nic_starter);
  3786. octeon_dev->nic_poll_work.ctxptr = (void *)octeon_dev;
  3787. schedule_delayed_work(&octeon_dev->nic_poll_work.work,
  3788. LIQUIDIO_STARTER_POLL_INTERVAL_MS);
  3789. atomic_set(&octeon_dev->status, OCT_DEV_DISPATCH_INIT_DONE);
  3790. if (octeon_set_io_queues_off(octeon_dev)) {
  3791. dev_err(&octeon_dev->pci_dev->dev, "setting io queues off failed\n");
  3792. return 1;
  3793. }
  3794. if (OCTEON_CN23XX_PF(octeon_dev)) {
  3795. ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
  3796. if (ret) {
  3797. dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Failed to configure device registers\n");
  3798. return ret;
  3799. }
  3800. }
  3801. /* Initialize soft command buffer pool
  3802. */
  3803. if (octeon_setup_sc_buffer_pool(octeon_dev)) {
  3804. dev_err(&octeon_dev->pci_dev->dev, "sc buffer pool allocation failed\n");
  3805. return 1;
  3806. }
  3807. atomic_set(&octeon_dev->status, OCT_DEV_SC_BUFF_POOL_INIT_DONE);
  3808. /* Setup the data structures that manage this Octeon's Input queues. */
  3809. if (octeon_setup_instr_queues(octeon_dev)) {
  3810. dev_err(&octeon_dev->pci_dev->dev,
  3811. "instruction queue initialization failed\n");
  3812. return 1;
  3813. }
  3814. atomic_set(&octeon_dev->status, OCT_DEV_INSTR_QUEUE_INIT_DONE);
  3815. /* Initialize lists to manage the requests of different types that
  3816. * arrive from user & kernel applications for this octeon device.
  3817. */
  3818. if (octeon_setup_response_list(octeon_dev)) {
  3819. dev_err(&octeon_dev->pci_dev->dev, "Response list allocation failed\n");
  3820. return 1;
  3821. }
  3822. atomic_set(&octeon_dev->status, OCT_DEV_RESP_LIST_INIT_DONE);
  3823. if (octeon_setup_output_queues(octeon_dev)) {
  3824. dev_err(&octeon_dev->pci_dev->dev, "Output queue initialization failed\n");
  3825. return 1;
  3826. }
  3827. atomic_set(&octeon_dev->status, OCT_DEV_DROQ_INIT_DONE);
  3828. if (OCTEON_CN23XX_PF(octeon_dev)) {
  3829. if (octeon_dev->fn_list.setup_mbox(octeon_dev)) {
  3830. dev_err(&octeon_dev->pci_dev->dev, "OCTEON: Mailbox setup failed\n");
  3831. return 1;
  3832. }
  3833. atomic_set(&octeon_dev->status, OCT_DEV_MBOX_SETUP_DONE);
  3834. if (octeon_allocate_ioq_vector(octeon_dev)) {
  3835. dev_err(&octeon_dev->pci_dev->dev, "OCTEON: ioq vector allocation failed\n");
  3836. return 1;
  3837. }
  3838. atomic_set(&octeon_dev->status, OCT_DEV_MSIX_ALLOC_VECTOR_DONE);
  3839. } else {
  3840. /* The input and output queue registers were setup earlier (the
  3841. * queues were not enabled). Any additional registers
  3842. * that need to be programmed should be done now.
  3843. */
  3844. ret = octeon_dev->fn_list.setup_device_regs(octeon_dev);
  3845. if (ret) {
  3846. dev_err(&octeon_dev->pci_dev->dev,
  3847. "Failed to configure device registers\n");
  3848. return ret;
  3849. }
  3850. }
  3851. /* Initialize the tasklet that handles output queue packet processing.*/
  3852. dev_dbg(&octeon_dev->pci_dev->dev, "Initializing droq tasklet\n");
  3853. tasklet_init(&oct_priv->droq_tasklet, octeon_droq_bh,
  3854. (unsigned long)octeon_dev);
  3855. /* Setup the interrupt handler and record the INT SUM register address
  3856. */
  3857. if (octeon_setup_interrupt(octeon_dev))
  3858. return 1;
  3859. /* Enable Octeon device interrupts */
  3860. octeon_dev->fn_list.enable_interrupt(octeon_dev, OCTEON_ALL_INTR);
  3861. atomic_set(&octeon_dev->status, OCT_DEV_INTR_SET_DONE);
  3862. /* Enable the input and output queues for this Octeon device */
  3863. ret = octeon_dev->fn_list.enable_io_queues(octeon_dev);
  3864. if (ret) {
  3865. dev_err(&octeon_dev->pci_dev->dev, "Failed to enable input/output queues");
  3866. return ret;
  3867. }
  3868. atomic_set(&octeon_dev->status, OCT_DEV_IO_QUEUES_DONE);
  3869. if ((!OCTEON_CN23XX_PF(octeon_dev)) || !fw_loaded) {
  3870. dev_dbg(&octeon_dev->pci_dev->dev, "Waiting for DDR initialization...\n");
  3871. if (!ddr_timeout) {
  3872. dev_info(&octeon_dev->pci_dev->dev,
  3873. "WAITING. Set ddr_timeout to non-zero value to proceed with initialization.\n");
  3874. }
  3875. schedule_timeout_uninterruptible(HZ * LIO_RESET_SECS);
  3876. /* Wait for the octeon to initialize DDR after the soft-reset.*/
  3877. while (!ddr_timeout) {
  3878. set_current_state(TASK_INTERRUPTIBLE);
  3879. if (schedule_timeout(HZ / 10)) {
  3880. /* user probably pressed Control-C */
  3881. return 1;
  3882. }
  3883. }
  3884. ret = octeon_wait_for_ddr_init(octeon_dev, &ddr_timeout);
  3885. if (ret) {
  3886. dev_err(&octeon_dev->pci_dev->dev,
  3887. "DDR not initialized. Please confirm that board is configured to boot from Flash, ret: %d\n",
  3888. ret);
  3889. return 1;
  3890. }
  3891. if (octeon_wait_for_bootloader(octeon_dev, 1000)) {
  3892. dev_err(&octeon_dev->pci_dev->dev, "Board not responding\n");
  3893. return 1;
  3894. }
  3895. /* Divert uboot to take commands from host instead. */
  3896. ret = octeon_console_send_cmd(octeon_dev, bootcmd, 50);
  3897. dev_dbg(&octeon_dev->pci_dev->dev, "Initializing consoles\n");
  3898. ret = octeon_init_consoles(octeon_dev);
  3899. if (ret) {
  3900. dev_err(&octeon_dev->pci_dev->dev, "Could not access board consoles\n");
  3901. return 1;
  3902. }
  3903. ret = octeon_add_console(octeon_dev, 0);
  3904. if (ret) {
  3905. dev_err(&octeon_dev->pci_dev->dev, "Could not access board console\n");
  3906. return 1;
  3907. }
  3908. atomic_set(&octeon_dev->status, OCT_DEV_CONSOLE_INIT_DONE);
  3909. dev_dbg(&octeon_dev->pci_dev->dev, "Loading firmware\n");
  3910. ret = load_firmware(octeon_dev);
  3911. if (ret) {
  3912. dev_err(&octeon_dev->pci_dev->dev, "Could not load firmware to board\n");
  3913. return 1;
  3914. }
  3915. /* set bit 1 of SLI_SCRATCH_1 to indicate that firmware is
  3916. * loaded
  3917. */
  3918. if (OCTEON_CN23XX_PF(octeon_dev))
  3919. octeon_write_csr64(octeon_dev, CN23XX_SLI_SCRATCH1,
  3920. 2ULL);
  3921. }
  3922. handshake[octeon_dev->octeon_id].init_ok = 1;
  3923. complete(&handshake[octeon_dev->octeon_id].init);
  3924. atomic_set(&octeon_dev->status, OCT_DEV_HOST_OK);
  3925. /* Send Credit for Octeon Output queues. Credits are always sent after
  3926. * the output queue is enabled.
  3927. */
  3928. for (j = 0; j < octeon_dev->num_oqs; j++)
  3929. writel(octeon_dev->droq[j]->max_count,
  3930. octeon_dev->droq[j]->pkts_credit_reg);
  3931. /* Packets can start arriving on the output queues from this point. */
  3932. return 0;
  3933. }
  3934. /**
  3935. * \brief Exits the module
  3936. */
  3937. static void __exit liquidio_exit(void)
  3938. {
  3939. liquidio_deinit_pci();
  3940. pr_info("LiquidIO network module is now unloaded\n");
  3941. }
  3942. module_init(liquidio_init);
  3943. module_exit(liquidio_exit);