ionic_lif.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
  3. #include <linux/ethtool.h>
  4. #include <linux/printk.h>
  5. #include <linux/dynamic_debug.h>
  6. #include <linux/netdevice.h>
  7. #include <linux/etherdevice.h>
  8. #include <linux/if_vlan.h>
  9. #include <linux/rtnetlink.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/pci.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/crash_dump.h>
  14. #include <linux/vmalloc.h>
  15. #include <net/page_pool/helpers.h>
  16. #include "ionic.h"
  17. #include "ionic_bus.h"
  18. #include "ionic_dev.h"
  19. #include "ionic_lif.h"
  20. #include "ionic_aux.h"
  21. #include "ionic_txrx.h"
  22. #include "ionic_ethtool.h"
  23. #include "ionic_debugfs.h"
  24. /* queuetype support level */
  25. static const u8 ionic_qtype_versions[IONIC_QTYPE_MAX] = {
  26. [IONIC_QTYPE_ADMINQ] = 0, /* 0 = Base version with CQ support */
  27. [IONIC_QTYPE_NOTIFYQ] = 0, /* 0 = Base version */
  28. [IONIC_QTYPE_RXQ] = 2, /* 0 = Base version with CQ+SG support
  29. * 2 = ... with CMB rings
  30. */
  31. [IONIC_QTYPE_TXQ] = 3, /* 0 = Base version with CQ+SG support
  32. * 1 = ... with Tx SG version 1
  33. * 3 = ... with CMB rings
  34. */
  35. };
  36. static void ionic_link_status_check(struct ionic_lif *lif);
  37. static void ionic_lif_handle_fw_down(struct ionic_lif *lif);
  38. static void ionic_lif_handle_fw_up(struct ionic_lif *lif);
  39. static void ionic_lif_set_netdev_info(struct ionic_lif *lif);
  40. static void ionic_txrx_deinit(struct ionic_lif *lif);
  41. static int ionic_txrx_init(struct ionic_lif *lif);
  42. static int ionic_start_queues(struct ionic_lif *lif);
  43. static void ionic_stop_queues(struct ionic_lif *lif);
  44. static void ionic_lif_queue_identify(struct ionic_lif *lif);
  45. static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif);
  46. static void ionic_unregister_rxq_info(struct ionic_queue *q);
  47. static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id);
  48. static void ionic_dim_work(struct work_struct *work)
  49. {
  50. struct dim *dim = container_of(work, struct dim, work);
  51. struct dim_cq_moder cur_moder;
  52. struct ionic_intr_info *intr;
  53. struct ionic_qcq *qcq;
  54. struct ionic_lif *lif;
  55. struct ionic_queue *q;
  56. u32 new_coal;
  57. qcq = container_of(dim, struct ionic_qcq, dim);
  58. q = &qcq->q;
  59. if (q->type == IONIC_QTYPE_RXQ)
  60. cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
  61. else
  62. cur_moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
  63. lif = q->lif;
  64. new_coal = ionic_coal_usec_to_hw(lif->ionic, cur_moder.usec);
  65. new_coal = new_coal ? new_coal : 1;
  66. intr = &qcq->intr;
  67. if (intr->dim_coal_hw != new_coal) {
  68. intr->dim_coal_hw = new_coal;
  69. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  70. intr->index, intr->dim_coal_hw);
  71. }
  72. dim->state = DIM_START_MEASURE;
  73. }
  74. static void ionic_lif_deferred_work(struct work_struct *work)
  75. {
  76. struct ionic_lif *lif = container_of(work, struct ionic_lif, deferred.work);
  77. struct ionic_deferred *def = &lif->deferred;
  78. struct ionic_deferred_work *w = NULL;
  79. do {
  80. spin_lock_bh(&def->lock);
  81. if (!list_empty(&def->list)) {
  82. w = list_first_entry(&def->list,
  83. struct ionic_deferred_work, list);
  84. list_del(&w->list);
  85. }
  86. spin_unlock_bh(&def->lock);
  87. if (!w)
  88. break;
  89. switch (w->type) {
  90. case IONIC_DW_TYPE_RX_MODE:
  91. ionic_lif_rx_mode(lif);
  92. break;
  93. case IONIC_DW_TYPE_LINK_STATUS:
  94. ionic_link_status_check(lif);
  95. break;
  96. case IONIC_DW_TYPE_LIF_RESET:
  97. if (w->fw_status) {
  98. ionic_lif_handle_fw_up(lif);
  99. } else {
  100. ionic_lif_handle_fw_down(lif);
  101. /* Fire off another watchdog to see
  102. * if the FW is already back rather than
  103. * waiting another whole cycle
  104. */
  105. mod_timer(&lif->ionic->watchdog_timer, jiffies + 1);
  106. }
  107. break;
  108. default:
  109. break;
  110. }
  111. kfree(w);
  112. w = NULL;
  113. } while (true);
  114. }
  115. void ionic_lif_deferred_enqueue(struct ionic_lif *lif,
  116. struct ionic_deferred_work *work)
  117. {
  118. spin_lock_bh(&lif->deferred.lock);
  119. list_add_tail(&work->list, &lif->deferred.list);
  120. spin_unlock_bh(&lif->deferred.lock);
  121. queue_work(lif->ionic->wq, &lif->deferred.work);
  122. }
  123. static void ionic_link_status_check(struct ionic_lif *lif)
  124. {
  125. struct net_device *netdev = lif->netdev;
  126. u16 link_status;
  127. bool link_up;
  128. if (!test_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
  129. return;
  130. /* Don't put carrier back up if we're in a broken state */
  131. if (test_bit(IONIC_LIF_F_BROKEN, lif->state)) {
  132. clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
  133. return;
  134. }
  135. link_status = le16_to_cpu(lif->info->status.link_status);
  136. link_up = link_status == IONIC_PORT_OPER_STATUS_UP;
  137. if (link_up) {
  138. int err = 0;
  139. if (netdev->flags & IFF_UP && netif_running(netdev)) {
  140. mutex_lock(&lif->queue_lock);
  141. err = ionic_start_queues(lif);
  142. if (err && err != -EBUSY) {
  143. netdev_err(netdev,
  144. "Failed to start queues: %d\n", err);
  145. set_bit(IONIC_LIF_F_BROKEN, lif->state);
  146. netif_carrier_off(lif->netdev);
  147. }
  148. mutex_unlock(&lif->queue_lock);
  149. }
  150. if (!err && !netif_carrier_ok(netdev)) {
  151. ionic_port_identify(lif->ionic);
  152. netdev_info(netdev, "Link up - %d Gbps\n",
  153. le32_to_cpu(lif->info->status.link_speed) / 1000);
  154. netif_carrier_on(netdev);
  155. }
  156. } else {
  157. if (netif_carrier_ok(netdev)) {
  158. lif->link_down_count++;
  159. netdev_info(netdev, "Link down\n");
  160. netif_carrier_off(netdev);
  161. }
  162. if (netdev->flags & IFF_UP && netif_running(netdev)) {
  163. mutex_lock(&lif->queue_lock);
  164. ionic_stop_queues(lif);
  165. mutex_unlock(&lif->queue_lock);
  166. }
  167. }
  168. clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
  169. }
  170. void ionic_link_status_check_request(struct ionic_lif *lif, bool can_sleep)
  171. {
  172. struct ionic_deferred_work *work;
  173. /* we only need one request outstanding at a time */
  174. if (test_and_set_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state))
  175. return;
  176. if (!can_sleep) {
  177. work = kzalloc_obj(*work, GFP_ATOMIC);
  178. if (!work) {
  179. clear_bit(IONIC_LIF_F_LINK_CHECK_REQUESTED, lif->state);
  180. return;
  181. }
  182. work->type = IONIC_DW_TYPE_LINK_STATUS;
  183. ionic_lif_deferred_enqueue(lif, work);
  184. } else {
  185. ionic_link_status_check(lif);
  186. }
  187. }
  188. static irqreturn_t ionic_isr(int irq, void *data)
  189. {
  190. struct napi_struct *napi = data;
  191. napi_schedule_irqoff(napi);
  192. return IRQ_HANDLED;
  193. }
  194. static int ionic_request_irq(struct ionic_lif *lif, struct ionic_qcq *qcq)
  195. {
  196. struct ionic_intr_info *intr = &qcq->intr;
  197. struct device *dev = lif->ionic->dev;
  198. struct ionic_queue *q = &qcq->q;
  199. const char *name;
  200. if (lif->registered)
  201. name = netdev_name(lif->netdev);
  202. else
  203. name = dev_name(dev);
  204. snprintf(intr->name, sizeof(intr->name),
  205. "%.5s-%.16s-%.8s", IONIC_DRV_NAME, name, q->name);
  206. return devm_request_irq(dev, intr->vector, ionic_isr,
  207. 0, intr->name, &qcq->napi);
  208. }
  209. int ionic_intr_alloc(struct ionic_lif *lif, struct ionic_intr_info *intr)
  210. {
  211. struct ionic *ionic = lif->ionic;
  212. int index, err;
  213. index = find_first_zero_bit(ionic->intrs, ionic->nintrs);
  214. if (index == ionic->nintrs)
  215. return -ENOSPC;
  216. set_bit(index, ionic->intrs);
  217. ionic_intr_init(&ionic->idev, intr, index);
  218. err = ionic_bus_get_irq(ionic, intr->index);
  219. if (err < 0) {
  220. clear_bit(index, ionic->intrs);
  221. return err;
  222. }
  223. intr->vector = err;
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_NS(ionic_intr_alloc, "NET_IONIC");
  227. void ionic_intr_free(struct ionic_lif *lif, int index)
  228. {
  229. if (index != IONIC_INTR_INDEX_NOT_ASSIGNED && index < lif->ionic->nintrs)
  230. clear_bit(index, lif->ionic->intrs);
  231. }
  232. EXPORT_SYMBOL_NS(ionic_intr_free, "NET_IONIC");
  233. static void ionic_irq_aff_notify(struct irq_affinity_notify *notify,
  234. const cpumask_t *mask)
  235. {
  236. struct ionic_intr_info *intr = container_of(notify, struct ionic_intr_info, aff_notify);
  237. cpumask_copy(*intr->affinity_mask, mask);
  238. }
  239. static void ionic_irq_aff_release(struct kref __always_unused *ref)
  240. {
  241. }
  242. static int ionic_qcq_enable(struct ionic_qcq *qcq)
  243. {
  244. struct ionic_queue *q = &qcq->q;
  245. struct ionic_lif *lif = q->lif;
  246. struct ionic_dev *idev;
  247. struct device *dev;
  248. struct ionic_admin_ctx ctx = {
  249. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  250. .cmd.q_control = {
  251. .opcode = IONIC_CMD_Q_CONTROL,
  252. .lif_index = cpu_to_le16(lif->index),
  253. .type = q->type,
  254. .index = cpu_to_le32(q->index),
  255. .oper = IONIC_Q_ENABLE,
  256. },
  257. };
  258. int ret;
  259. idev = &lif->ionic->idev;
  260. dev = lif->ionic->dev;
  261. dev_dbg(dev, "q_enable.index %d q_enable.qtype %d\n",
  262. ctx.cmd.q_control.index, ctx.cmd.q_control.type);
  263. if (qcq->flags & IONIC_QCQ_F_INTR)
  264. ionic_intr_clean(idev->intr_ctrl, qcq->intr.index);
  265. ret = ionic_adminq_post_wait(lif, &ctx);
  266. if (ret)
  267. return ret;
  268. if (qcq->flags & IONIC_QCQ_F_INTR) {
  269. napi_enable(&qcq->napi);
  270. irq_set_affinity_notifier(qcq->intr.vector,
  271. &qcq->intr.aff_notify);
  272. irq_set_affinity_hint(qcq->intr.vector,
  273. *qcq->intr.affinity_mask);
  274. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  275. IONIC_INTR_MASK_CLEAR);
  276. }
  277. return 0;
  278. }
  279. static int ionic_qcq_disable(struct ionic_lif *lif, struct ionic_qcq *qcq, int fw_err)
  280. {
  281. struct ionic_queue *q;
  282. struct ionic_admin_ctx ctx = {
  283. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  284. .cmd.q_control = {
  285. .opcode = IONIC_CMD_Q_CONTROL,
  286. .oper = IONIC_Q_DISABLE,
  287. },
  288. };
  289. if (!qcq) {
  290. netdev_err(lif->netdev, "%s: bad qcq\n", __func__);
  291. return -ENXIO;
  292. }
  293. q = &qcq->q;
  294. if (qcq->flags & IONIC_QCQ_F_INTR) {
  295. struct ionic_dev *idev = &lif->ionic->idev;
  296. if (lif->doorbell_wa)
  297. cancel_work_sync(&qcq->doorbell_napi_work);
  298. cancel_work_sync(&qcq->dim.work);
  299. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  300. IONIC_INTR_MASK_SET);
  301. synchronize_irq(qcq->intr.vector);
  302. irq_set_affinity_notifier(qcq->intr.vector, NULL);
  303. irq_set_affinity_hint(qcq->intr.vector, NULL);
  304. napi_disable(&qcq->napi);
  305. }
  306. /* If there was a previous fw communcation error, don't bother with
  307. * sending the adminq command and just return the same error value.
  308. */
  309. if (fw_err == -ETIMEDOUT || fw_err == -ENXIO)
  310. return fw_err;
  311. ctx.cmd.q_control.lif_index = cpu_to_le16(lif->index);
  312. ctx.cmd.q_control.type = q->type;
  313. ctx.cmd.q_control.index = cpu_to_le32(q->index);
  314. dev_dbg(lif->ionic->dev, "q_disable.index %d q_disable.qtype %d\n",
  315. ctx.cmd.q_control.index, ctx.cmd.q_control.type);
  316. return ionic_adminq_post_wait(lif, &ctx);
  317. }
  318. static void ionic_lif_qcq_deinit(struct ionic_lif *lif, struct ionic_qcq *qcq)
  319. {
  320. struct ionic_dev *idev = &lif->ionic->idev;
  321. if (!qcq)
  322. return;
  323. if (!(qcq->flags & IONIC_QCQ_F_INITED))
  324. return;
  325. ionic_unregister_rxq_info(&qcq->q);
  326. if (qcq->flags & IONIC_QCQ_F_INTR) {
  327. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  328. IONIC_INTR_MASK_SET);
  329. netif_napi_del(&qcq->napi);
  330. }
  331. qcq->flags &= ~IONIC_QCQ_F_INITED;
  332. }
  333. static void ionic_qcq_intr_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
  334. {
  335. if (!(qcq->flags & IONIC_QCQ_F_INTR) || qcq->intr.vector == 0)
  336. return;
  337. irq_set_affinity_hint(qcq->intr.vector, NULL);
  338. devm_free_irq(lif->ionic->dev, qcq->intr.vector, &qcq->napi);
  339. qcq->intr.vector = 0;
  340. ionic_intr_free(lif, qcq->intr.index);
  341. qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
  342. }
  343. static void ionic_qcq_free(struct ionic_lif *lif, struct ionic_qcq *qcq)
  344. {
  345. struct device *dev = lif->ionic->dev;
  346. if (!qcq)
  347. return;
  348. ionic_debugfs_del_qcq(qcq);
  349. if (qcq->q_base) {
  350. dma_free_coherent(dev, qcq->q_size, qcq->q_base, qcq->q_base_pa);
  351. qcq->q_base = NULL;
  352. qcq->q_base_pa = 0;
  353. }
  354. if (qcq->cmb_q_base) {
  355. iounmap(qcq->cmb_q_base);
  356. ionic_put_cmb(lif, qcq->cmb_pgid, qcq->cmb_order);
  357. qcq->cmb_pgid = 0;
  358. qcq->cmb_order = 0;
  359. qcq->cmb_q_base = NULL;
  360. qcq->cmb_q_base_pa = 0;
  361. }
  362. if (qcq->cq_base) {
  363. dma_free_coherent(dev, qcq->cq_size, qcq->cq_base, qcq->cq_base_pa);
  364. qcq->cq_base = NULL;
  365. qcq->cq_base_pa = 0;
  366. }
  367. if (qcq->sg_base) {
  368. dma_free_coherent(dev, qcq->sg_size, qcq->sg_base, qcq->sg_base_pa);
  369. qcq->sg_base = NULL;
  370. qcq->sg_base_pa = 0;
  371. }
  372. page_pool_destroy(qcq->q.page_pool);
  373. qcq->q.page_pool = NULL;
  374. ionic_qcq_intr_free(lif, qcq);
  375. vfree(qcq->q.info);
  376. qcq->q.info = NULL;
  377. }
  378. void ionic_qcqs_free(struct ionic_lif *lif)
  379. {
  380. struct device *dev = lif->ionic->dev;
  381. struct ionic_qcq *adminqcq;
  382. unsigned long irqflags;
  383. if (lif->notifyqcq) {
  384. ionic_qcq_free(lif, lif->notifyqcq);
  385. devm_kfree(dev, lif->notifyqcq);
  386. lif->notifyqcq = NULL;
  387. }
  388. if (lif->adminqcq) {
  389. spin_lock_irqsave(&lif->adminq_lock, irqflags);
  390. adminqcq = READ_ONCE(lif->adminqcq);
  391. lif->adminqcq = NULL;
  392. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  393. if (adminqcq) {
  394. ionic_qcq_free(lif, adminqcq);
  395. devm_kfree(dev, adminqcq);
  396. }
  397. }
  398. if (lif->rxqcqs) {
  399. devm_kfree(dev, lif->rxqstats);
  400. lif->rxqstats = NULL;
  401. devm_kfree(dev, lif->rxqcqs);
  402. lif->rxqcqs = NULL;
  403. }
  404. if (lif->txqcqs) {
  405. devm_kfree(dev, lif->txqstats);
  406. lif->txqstats = NULL;
  407. devm_kfree(dev, lif->txqcqs);
  408. lif->txqcqs = NULL;
  409. }
  410. }
  411. static void ionic_link_qcq_interrupts(struct ionic_qcq *src_qcq,
  412. struct ionic_qcq *n_qcq)
  413. {
  414. n_qcq->intr.vector = src_qcq->intr.vector;
  415. n_qcq->intr.index = src_qcq->intr.index;
  416. }
  417. static int ionic_alloc_qcq_interrupt(struct ionic_lif *lif, struct ionic_qcq *qcq)
  418. {
  419. cpumask_var_t *affinity_mask;
  420. int err;
  421. if (!(qcq->flags & IONIC_QCQ_F_INTR)) {
  422. qcq->intr.index = IONIC_INTR_INDEX_NOT_ASSIGNED;
  423. return 0;
  424. }
  425. err = ionic_intr_alloc(lif, &qcq->intr);
  426. if (err) {
  427. netdev_warn(lif->netdev, "no intr for %s: %d\n",
  428. qcq->q.name, err);
  429. goto err_out;
  430. }
  431. ionic_intr_mask_assert(lif->ionic->idev.intr_ctrl, qcq->intr.index,
  432. IONIC_INTR_MASK_SET);
  433. err = ionic_request_irq(lif, qcq);
  434. if (err) {
  435. netdev_warn(lif->netdev, "irq request failed %d\n", err);
  436. goto err_out_free_intr;
  437. }
  438. /* try to get the irq on the local numa node first */
  439. affinity_mask = &lif->ionic->affinity_masks[qcq->intr.index];
  440. if (cpumask_empty(*affinity_mask)) {
  441. unsigned int cpu;
  442. cpu = cpumask_local_spread(qcq->intr.index,
  443. dev_to_node(lif->ionic->dev));
  444. if (cpu != -1)
  445. cpumask_set_cpu(cpu, *affinity_mask);
  446. }
  447. qcq->intr.affinity_mask = affinity_mask;
  448. qcq->intr.aff_notify.notify = ionic_irq_aff_notify;
  449. qcq->intr.aff_notify.release = ionic_irq_aff_release;
  450. netdev_dbg(lif->netdev, "%s: Interrupt index %d\n", qcq->q.name, qcq->intr.index);
  451. return 0;
  452. err_out_free_intr:
  453. ionic_intr_free(lif, qcq->intr.index);
  454. err_out:
  455. return err;
  456. }
  457. static int ionic_qcq_alloc(struct ionic_lif *lif, unsigned int type,
  458. unsigned int index,
  459. const char *name, unsigned int flags,
  460. unsigned int num_descs, unsigned int desc_size,
  461. unsigned int cq_desc_size,
  462. unsigned int sg_desc_size,
  463. unsigned int desc_info_size,
  464. unsigned int pid, struct bpf_prog *xdp_prog,
  465. struct ionic_qcq **qcq)
  466. {
  467. struct ionic_dev *idev = &lif->ionic->idev;
  468. struct device *dev = lif->ionic->dev;
  469. struct ionic_qcq *new;
  470. int err;
  471. *qcq = NULL;
  472. new = devm_kzalloc(dev, sizeof(*new), GFP_KERNEL);
  473. if (!new) {
  474. netdev_err(lif->netdev, "Cannot allocate queue structure\n");
  475. err = -ENOMEM;
  476. goto err_out;
  477. }
  478. new->q.dev = dev;
  479. new->flags = flags;
  480. new->q.info = vcalloc(num_descs, desc_info_size);
  481. if (!new->q.info) {
  482. netdev_err(lif->netdev, "Cannot allocate queue info\n");
  483. err = -ENOMEM;
  484. goto err_out_free_qcq;
  485. }
  486. if (type == IONIC_QTYPE_RXQ) {
  487. struct page_pool_params pp_params = {
  488. .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
  489. .order = 0,
  490. .pool_size = num_descs,
  491. .nid = NUMA_NO_NODE,
  492. .dev = lif->ionic->dev,
  493. .napi = &new->napi,
  494. .dma_dir = DMA_FROM_DEVICE,
  495. .max_len = PAGE_SIZE,
  496. .netdev = lif->netdev,
  497. };
  498. if (xdp_prog)
  499. pp_params.dma_dir = DMA_BIDIRECTIONAL;
  500. new->q.page_pool = page_pool_create(&pp_params);
  501. if (IS_ERR(new->q.page_pool)) {
  502. netdev_err(lif->netdev, "Cannot create page_pool\n");
  503. err = PTR_ERR(new->q.page_pool);
  504. new->q.page_pool = NULL;
  505. goto err_out_free_q_info;
  506. }
  507. }
  508. new->q.type = type;
  509. new->q.max_sg_elems = lif->qtype_info[type].max_sg_elems;
  510. err = ionic_q_init(lif, idev, &new->q, index, name, num_descs,
  511. desc_size, sg_desc_size, pid);
  512. if (err) {
  513. netdev_err(lif->netdev, "Cannot initialize queue\n");
  514. goto err_out_free_page_pool;
  515. }
  516. err = ionic_alloc_qcq_interrupt(lif, new);
  517. if (err)
  518. goto err_out_free_page_pool;
  519. err = ionic_cq_init(lif, &new->cq, &new->intr, num_descs, cq_desc_size);
  520. if (err) {
  521. netdev_err(lif->netdev, "Cannot initialize completion queue\n");
  522. goto err_out_free_irq;
  523. }
  524. if (flags & IONIC_QCQ_F_NOTIFYQ) {
  525. int q_size;
  526. /* q & cq need to be contiguous in NotifyQ, so alloc it all in q
  527. * and don't alloc qc. We leave new->qc_size and new->qc_base
  528. * as 0 to be sure we don't try to free it later.
  529. */
  530. q_size = ALIGN(num_descs * desc_size, PAGE_SIZE);
  531. new->q_size = PAGE_SIZE + q_size +
  532. ALIGN(num_descs * cq_desc_size, PAGE_SIZE);
  533. new->q_base = dma_alloc_coherent(dev, new->q_size,
  534. &new->q_base_pa, GFP_KERNEL);
  535. if (!new->q_base) {
  536. netdev_err(lif->netdev, "Cannot allocate qcq DMA memory\n");
  537. err = -ENOMEM;
  538. goto err_out_free_irq;
  539. }
  540. new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
  541. new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
  542. /* Base the NotifyQ cq.base off of the ALIGNed q.base */
  543. new->cq.base = PTR_ALIGN(new->q.base + q_size, PAGE_SIZE);
  544. new->cq.base_pa = ALIGN(new->q_base_pa + q_size, PAGE_SIZE);
  545. new->cq.bound_q = &new->q;
  546. } else {
  547. /* regular DMA q descriptors */
  548. new->q_size = PAGE_SIZE + (num_descs * desc_size);
  549. new->q_base = dma_alloc_coherent(dev, new->q_size, &new->q_base_pa,
  550. GFP_KERNEL);
  551. if (!new->q_base) {
  552. netdev_err(lif->netdev, "Cannot allocate queue DMA memory\n");
  553. err = -ENOMEM;
  554. goto err_out_free_irq;
  555. }
  556. new->q.base = PTR_ALIGN(new->q_base, PAGE_SIZE);
  557. new->q.base_pa = ALIGN(new->q_base_pa, PAGE_SIZE);
  558. if (flags & IONIC_QCQ_F_CMB_RINGS) {
  559. /* on-chip CMB q descriptors */
  560. new->cmb_q_size = num_descs * desc_size;
  561. new->cmb_order = order_base_2(new->cmb_q_size / PAGE_SIZE);
  562. err = ionic_get_cmb(lif, &new->cmb_pgid, &new->cmb_q_base_pa,
  563. new->cmb_order, 0, NULL);
  564. if (err) {
  565. netdev_err(lif->netdev,
  566. "Cannot allocate queue order %d from cmb: err %d\n",
  567. new->cmb_order, err);
  568. goto err_out_free_q;
  569. }
  570. new->cmb_q_base = ioremap_wc(new->cmb_q_base_pa, new->cmb_q_size);
  571. if (!new->cmb_q_base) {
  572. netdev_err(lif->netdev, "Cannot map queue from cmb\n");
  573. ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
  574. err = -ENOMEM;
  575. goto err_out_free_q;
  576. }
  577. new->cmb_q_base_pa -= idev->phy_cmb_pages;
  578. new->q.cmb_base = new->cmb_q_base;
  579. new->q.cmb_base_pa = new->cmb_q_base_pa;
  580. }
  581. /* cq DMA descriptors */
  582. new->cq_size = PAGE_SIZE + (num_descs * cq_desc_size);
  583. new->cq_base = dma_alloc_coherent(dev, new->cq_size, &new->cq_base_pa,
  584. GFP_KERNEL);
  585. if (!new->cq_base) {
  586. netdev_err(lif->netdev, "Cannot allocate cq DMA memory\n");
  587. err = -ENOMEM;
  588. goto err_out_free_q;
  589. }
  590. new->cq.base = PTR_ALIGN(new->cq_base, PAGE_SIZE);
  591. new->cq.base_pa = ALIGN(new->cq_base_pa, PAGE_SIZE);
  592. new->cq.bound_q = &new->q;
  593. }
  594. if (flags & IONIC_QCQ_F_SG) {
  595. new->sg_size = PAGE_SIZE + (num_descs * sg_desc_size);
  596. new->sg_base = dma_alloc_coherent(dev, new->sg_size, &new->sg_base_pa,
  597. GFP_KERNEL);
  598. if (!new->sg_base) {
  599. netdev_err(lif->netdev, "Cannot allocate sg DMA memory\n");
  600. err = -ENOMEM;
  601. goto err_out_free_cq;
  602. }
  603. new->q.sg_base = PTR_ALIGN(new->sg_base, PAGE_SIZE);
  604. new->q.sg_base_pa = ALIGN(new->sg_base_pa, PAGE_SIZE);
  605. }
  606. INIT_WORK(&new->dim.work, ionic_dim_work);
  607. new->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_CQE;
  608. if (lif->doorbell_wa)
  609. INIT_WORK(&new->doorbell_napi_work, ionic_doorbell_napi_work);
  610. *qcq = new;
  611. return 0;
  612. err_out_free_cq:
  613. dma_free_coherent(dev, new->cq_size, new->cq_base, new->cq_base_pa);
  614. err_out_free_q:
  615. if (new->cmb_q_base) {
  616. iounmap(new->cmb_q_base);
  617. ionic_put_cmb(lif, new->cmb_pgid, new->cmb_order);
  618. }
  619. dma_free_coherent(dev, new->q_size, new->q_base, new->q_base_pa);
  620. err_out_free_irq:
  621. if (flags & IONIC_QCQ_F_INTR) {
  622. devm_free_irq(dev, new->intr.vector, &new->napi);
  623. ionic_intr_free(lif, new->intr.index);
  624. }
  625. err_out_free_page_pool:
  626. page_pool_destroy(new->q.page_pool);
  627. err_out_free_q_info:
  628. vfree(new->q.info);
  629. err_out_free_qcq:
  630. devm_kfree(dev, new);
  631. err_out:
  632. dev_err(dev, "qcq alloc of %s%d failed %d\n", name, index, err);
  633. return err;
  634. }
  635. static int ionic_qcqs_alloc(struct ionic_lif *lif)
  636. {
  637. struct device *dev = lif->ionic->dev;
  638. unsigned int flags;
  639. int err;
  640. flags = IONIC_QCQ_F_INTR;
  641. err = ionic_qcq_alloc(lif, IONIC_QTYPE_ADMINQ, 0, "admin", flags,
  642. IONIC_ADMINQ_LENGTH,
  643. sizeof(struct ionic_admin_cmd),
  644. sizeof(struct ionic_admin_comp),
  645. 0,
  646. sizeof(struct ionic_admin_desc_info),
  647. lif->kern_pid, NULL, &lif->adminqcq);
  648. if (err)
  649. return err;
  650. ionic_debugfs_add_qcq(lif, lif->adminqcq);
  651. if (lif->ionic->nnqs_per_lif) {
  652. flags = IONIC_QCQ_F_NOTIFYQ;
  653. err = ionic_qcq_alloc(lif, IONIC_QTYPE_NOTIFYQ, 0, "notifyq",
  654. flags, IONIC_NOTIFYQ_LENGTH,
  655. sizeof(struct ionic_notifyq_cmd),
  656. sizeof(union ionic_notifyq_comp),
  657. 0,
  658. sizeof(struct ionic_admin_desc_info),
  659. lif->kern_pid, NULL, &lif->notifyqcq);
  660. if (err)
  661. goto err_out;
  662. ionic_debugfs_add_qcq(lif, lif->notifyqcq);
  663. /* Let the notifyq ride on the adminq interrupt */
  664. ionic_link_qcq_interrupts(lif->adminqcq, lif->notifyqcq);
  665. }
  666. err = -ENOMEM;
  667. lif->txqcqs = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif,
  668. sizeof(*lif->txqcqs), GFP_KERNEL);
  669. if (!lif->txqcqs)
  670. goto err_out;
  671. lif->rxqcqs = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif,
  672. sizeof(*lif->rxqcqs), GFP_KERNEL);
  673. if (!lif->rxqcqs)
  674. goto err_out;
  675. lif->txqstats = devm_kcalloc(dev, lif->ionic->ntxqs_per_lif + 1,
  676. sizeof(*lif->txqstats), GFP_KERNEL);
  677. if (!lif->txqstats)
  678. goto err_out;
  679. lif->rxqstats = devm_kcalloc(dev, lif->ionic->nrxqs_per_lif + 1,
  680. sizeof(*lif->rxqstats), GFP_KERNEL);
  681. if (!lif->rxqstats)
  682. goto err_out;
  683. return 0;
  684. err_out:
  685. ionic_qcqs_free(lif);
  686. return err;
  687. }
  688. static void ionic_qcq_sanitize(struct ionic_qcq *qcq)
  689. {
  690. qcq->q.tail_idx = 0;
  691. qcq->q.head_idx = 0;
  692. qcq->cq.tail_idx = 0;
  693. qcq->cq.done_color = 1;
  694. memset(qcq->q_base, 0, qcq->q_size);
  695. if (qcq->cmb_q_base)
  696. memset_io(qcq->cmb_q_base, 0, qcq->cmb_q_size);
  697. memset(qcq->cq_base, 0, qcq->cq_size);
  698. memset(qcq->sg_base, 0, qcq->sg_size);
  699. }
  700. static int ionic_lif_txq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
  701. {
  702. struct device *dev = lif->ionic->dev;
  703. struct ionic_queue *q = &qcq->q;
  704. struct ionic_cq *cq = &qcq->cq;
  705. struct ionic_admin_ctx ctx = {
  706. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  707. .cmd.q_init = {
  708. .opcode = IONIC_CMD_Q_INIT,
  709. .lif_index = cpu_to_le16(lif->index),
  710. .type = q->type,
  711. .ver = lif->qtype_info[q->type].version,
  712. .index = cpu_to_le32(q->index),
  713. .flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
  714. IONIC_QINIT_F_SG),
  715. .intr_index = cpu_to_le16(qcq->intr.index),
  716. .pid = cpu_to_le16(q->pid),
  717. .ring_size = ilog2(q->num_descs),
  718. .ring_base = cpu_to_le64(q->base_pa),
  719. .cq_ring_base = cpu_to_le64(cq->base_pa),
  720. .sg_ring_base = cpu_to_le64(q->sg_base_pa),
  721. .features = cpu_to_le64(q->features),
  722. },
  723. };
  724. int err;
  725. if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
  726. ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
  727. ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
  728. }
  729. dev_dbg(dev, "txq_init.pid %d\n", ctx.cmd.q_init.pid);
  730. dev_dbg(dev, "txq_init.index %d\n", ctx.cmd.q_init.index);
  731. dev_dbg(dev, "txq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
  732. dev_dbg(dev, "txq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
  733. dev_dbg(dev, "txq_init.cq_ring_base 0x%llx\n", ctx.cmd.q_init.cq_ring_base);
  734. dev_dbg(dev, "txq_init.sg_ring_base 0x%llx\n", ctx.cmd.q_init.sg_ring_base);
  735. dev_dbg(dev, "txq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
  736. dev_dbg(dev, "txq_init.ver %d\n", ctx.cmd.q_init.ver);
  737. dev_dbg(dev, "txq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
  738. ionic_qcq_sanitize(qcq);
  739. err = ionic_adminq_post_wait(lif, &ctx);
  740. if (err)
  741. return err;
  742. q->hw_type = ctx.comp.q_init.hw_type;
  743. q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
  744. q->dbval = IONIC_DBELL_QID(q->hw_index);
  745. dev_dbg(dev, "txq->hw_type %d\n", q->hw_type);
  746. dev_dbg(dev, "txq->hw_index %d\n", q->hw_index);
  747. q->dbell_deadline = IONIC_TX_DOORBELL_DEADLINE;
  748. q->dbell_jiffies = jiffies;
  749. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  750. netif_napi_add(lif->netdev, &qcq->napi, ionic_tx_napi);
  751. qcq->flags |= IONIC_QCQ_F_INITED;
  752. return 0;
  753. }
  754. static int ionic_lif_rxq_init(struct ionic_lif *lif, struct ionic_qcq *qcq)
  755. {
  756. struct device *dev = lif->ionic->dev;
  757. struct ionic_queue *q = &qcq->q;
  758. struct ionic_cq *cq = &qcq->cq;
  759. struct ionic_admin_ctx ctx = {
  760. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  761. .cmd.q_init = {
  762. .opcode = IONIC_CMD_Q_INIT,
  763. .lif_index = cpu_to_le16(lif->index),
  764. .type = q->type,
  765. .ver = lif->qtype_info[q->type].version,
  766. .index = cpu_to_le32(q->index),
  767. .flags = cpu_to_le16(IONIC_QINIT_F_IRQ),
  768. .intr_index = cpu_to_le16(cq->bound_intr->index),
  769. .pid = cpu_to_le16(q->pid),
  770. .ring_size = ilog2(q->num_descs),
  771. .ring_base = cpu_to_le64(q->base_pa),
  772. .cq_ring_base = cpu_to_le64(cq->base_pa),
  773. .sg_ring_base = cpu_to_le64(q->sg_base_pa),
  774. .features = cpu_to_le64(q->features),
  775. },
  776. };
  777. int err;
  778. q->partner = &lif->txqcqs[q->index]->q;
  779. q->partner->partner = q;
  780. if (!lif->xdp_prog ||
  781. (lif->xdp_prog->aux && lif->xdp_prog->aux->xdp_has_frags))
  782. ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_SG);
  783. if (qcq->flags & IONIC_QCQ_F_CMB_RINGS) {
  784. ctx.cmd.q_init.flags |= cpu_to_le16(IONIC_QINIT_F_CMB);
  785. ctx.cmd.q_init.ring_base = cpu_to_le64(qcq->cmb_q_base_pa);
  786. }
  787. dev_dbg(dev, "rxq_init.pid %d\n", ctx.cmd.q_init.pid);
  788. dev_dbg(dev, "rxq_init.index %d\n", ctx.cmd.q_init.index);
  789. dev_dbg(dev, "rxq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
  790. dev_dbg(dev, "rxq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
  791. dev_dbg(dev, "rxq_init.flags 0x%x\n", ctx.cmd.q_init.flags);
  792. dev_dbg(dev, "rxq_init.ver %d\n", ctx.cmd.q_init.ver);
  793. dev_dbg(dev, "rxq_init.intr_index %d\n", ctx.cmd.q_init.intr_index);
  794. ionic_qcq_sanitize(qcq);
  795. err = ionic_adminq_post_wait(lif, &ctx);
  796. if (err)
  797. return err;
  798. q->hw_type = ctx.comp.q_init.hw_type;
  799. q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
  800. q->dbval = IONIC_DBELL_QID(q->hw_index);
  801. dev_dbg(dev, "rxq->hw_type %d\n", q->hw_type);
  802. dev_dbg(dev, "rxq->hw_index %d\n", q->hw_index);
  803. q->dbell_deadline = IONIC_RX_MIN_DOORBELL_DEADLINE;
  804. q->dbell_jiffies = jiffies;
  805. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  806. netif_napi_add(lif->netdev, &qcq->napi, ionic_rx_napi);
  807. else
  808. netif_napi_add(lif->netdev, &qcq->napi, ionic_txrx_napi);
  809. err = ionic_register_rxq_info(q, qcq->napi.napi_id);
  810. if (err) {
  811. netif_napi_del(&qcq->napi);
  812. return err;
  813. }
  814. qcq->flags |= IONIC_QCQ_F_INITED;
  815. return 0;
  816. }
  817. int ionic_lif_create_hwstamp_txq(struct ionic_lif *lif)
  818. {
  819. unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
  820. unsigned int txq_i, flags;
  821. struct ionic_qcq *txq;
  822. u64 features;
  823. int err;
  824. if (lif->hwstamp_txq)
  825. return 0;
  826. features = IONIC_Q_F_2X_CQ_DESC | IONIC_TXQ_F_HWSTAMP;
  827. num_desc = IONIC_MIN_TXRX_DESC;
  828. desc_sz = sizeof(struct ionic_txq_desc);
  829. comp_sz = 2 * sizeof(struct ionic_txq_comp);
  830. if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
  831. lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz == sizeof(struct ionic_txq_sg_desc_v1))
  832. sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
  833. else
  834. sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
  835. txq_i = lif->ionic->ntxqs_per_lif;
  836. flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
  837. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, txq_i, "hwstamp_tx", flags,
  838. num_desc, desc_sz, comp_sz, sg_desc_sz,
  839. sizeof(struct ionic_tx_desc_info),
  840. lif->kern_pid, NULL, &txq);
  841. if (err)
  842. goto err_qcq_alloc;
  843. txq->q.features = features;
  844. ionic_link_qcq_interrupts(lif->adminqcq, txq);
  845. ionic_debugfs_add_qcq(lif, txq);
  846. lif->hwstamp_txq = txq;
  847. if (netif_running(lif->netdev)) {
  848. err = ionic_lif_txq_init(lif, txq);
  849. if (err)
  850. goto err_qcq_init;
  851. if (test_bit(IONIC_LIF_F_UP, lif->state)) {
  852. err = ionic_qcq_enable(txq);
  853. if (err)
  854. goto err_qcq_enable;
  855. }
  856. }
  857. return 0;
  858. err_qcq_enable:
  859. ionic_lif_qcq_deinit(lif, txq);
  860. err_qcq_init:
  861. lif->hwstamp_txq = NULL;
  862. ionic_debugfs_del_qcq(txq);
  863. ionic_qcq_free(lif, txq);
  864. devm_kfree(lif->ionic->dev, txq);
  865. err_qcq_alloc:
  866. return err;
  867. }
  868. int ionic_lif_create_hwstamp_rxq(struct ionic_lif *lif)
  869. {
  870. unsigned int num_desc, desc_sz, comp_sz, sg_desc_sz;
  871. unsigned int rxq_i, flags;
  872. struct ionic_qcq *rxq;
  873. u64 features;
  874. int err;
  875. if (lif->hwstamp_rxq)
  876. return 0;
  877. features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
  878. num_desc = IONIC_MIN_TXRX_DESC;
  879. desc_sz = sizeof(struct ionic_rxq_desc);
  880. comp_sz = 2 * sizeof(struct ionic_rxq_comp);
  881. sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
  882. rxq_i = lif->ionic->nrxqs_per_lif;
  883. flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
  884. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, rxq_i, "hwstamp_rx", flags,
  885. num_desc, desc_sz, comp_sz, sg_desc_sz,
  886. sizeof(struct ionic_rx_desc_info),
  887. lif->kern_pid, NULL, &rxq);
  888. if (err)
  889. goto err_qcq_alloc;
  890. rxq->q.features = features;
  891. ionic_link_qcq_interrupts(lif->adminqcq, rxq);
  892. ionic_debugfs_add_qcq(lif, rxq);
  893. lif->hwstamp_rxq = rxq;
  894. if (netif_running(lif->netdev)) {
  895. err = ionic_lif_rxq_init(lif, rxq);
  896. if (err)
  897. goto err_qcq_init;
  898. if (test_bit(IONIC_LIF_F_UP, lif->state)) {
  899. ionic_rx_fill(&rxq->q, NULL);
  900. err = ionic_qcq_enable(rxq);
  901. if (err)
  902. goto err_qcq_enable;
  903. }
  904. }
  905. return 0;
  906. err_qcq_enable:
  907. ionic_lif_qcq_deinit(lif, rxq);
  908. err_qcq_init:
  909. lif->hwstamp_rxq = NULL;
  910. ionic_debugfs_del_qcq(rxq);
  911. ionic_qcq_free(lif, rxq);
  912. devm_kfree(lif->ionic->dev, rxq);
  913. err_qcq_alloc:
  914. return err;
  915. }
  916. int ionic_lif_config_hwstamp_rxq_all(struct ionic_lif *lif, bool rx_all)
  917. {
  918. struct ionic_queue_params qparam;
  919. ionic_init_queue_params(lif, &qparam);
  920. if (rx_all)
  921. qparam.rxq_features = IONIC_Q_F_2X_CQ_DESC | IONIC_RXQ_F_HWSTAMP;
  922. else
  923. qparam.rxq_features = 0;
  924. /* if we're not running, just set the values and return */
  925. if (!netif_running(lif->netdev)) {
  926. lif->rxq_features = qparam.rxq_features;
  927. return 0;
  928. }
  929. return ionic_reconfigure_queues(lif, &qparam);
  930. }
  931. int ionic_lif_set_hwstamp_txmode(struct ionic_lif *lif, u16 txstamp_mode)
  932. {
  933. struct ionic_admin_ctx ctx = {
  934. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  935. .cmd.lif_setattr = {
  936. .opcode = IONIC_CMD_LIF_SETATTR,
  937. .index = cpu_to_le16(lif->index),
  938. .attr = IONIC_LIF_ATTR_TXSTAMP,
  939. .txstamp_mode = cpu_to_le16(txstamp_mode),
  940. },
  941. };
  942. return ionic_adminq_post_wait(lif, &ctx);
  943. }
  944. static void ionic_lif_del_hwstamp_rxfilt(struct ionic_lif *lif)
  945. {
  946. struct ionic_admin_ctx ctx = {
  947. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  948. .cmd.rx_filter_del = {
  949. .opcode = IONIC_CMD_RX_FILTER_DEL,
  950. .lif_index = cpu_to_le16(lif->index),
  951. },
  952. };
  953. struct ionic_rx_filter *f;
  954. u32 filter_id;
  955. int err;
  956. spin_lock_bh(&lif->rx_filters.lock);
  957. f = ionic_rx_filter_rxsteer(lif);
  958. if (!f) {
  959. spin_unlock_bh(&lif->rx_filters.lock);
  960. return;
  961. }
  962. filter_id = f->filter_id;
  963. ionic_rx_filter_free(lif, f);
  964. spin_unlock_bh(&lif->rx_filters.lock);
  965. netdev_dbg(lif->netdev, "rx_filter del RXSTEER (id %d)\n", filter_id);
  966. ctx.cmd.rx_filter_del.filter_id = cpu_to_le32(filter_id);
  967. err = ionic_adminq_post_wait(lif, &ctx);
  968. if (err && err != -EEXIST)
  969. netdev_dbg(lif->netdev, "failed to delete rx_filter RXSTEER (id %d)\n", filter_id);
  970. }
  971. static int ionic_lif_add_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
  972. {
  973. struct ionic_admin_ctx ctx = {
  974. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  975. .cmd.rx_filter_add = {
  976. .opcode = IONIC_CMD_RX_FILTER_ADD,
  977. .lif_index = cpu_to_le16(lif->index),
  978. .match = cpu_to_le16(IONIC_RX_FILTER_STEER_PKTCLASS),
  979. .pkt_class = cpu_to_le64(pkt_class),
  980. },
  981. };
  982. u8 qtype;
  983. u32 qid;
  984. int err;
  985. if (!lif->hwstamp_rxq)
  986. return -EINVAL;
  987. qtype = lif->hwstamp_rxq->q.type;
  988. ctx.cmd.rx_filter_add.qtype = qtype;
  989. qid = lif->hwstamp_rxq->q.index;
  990. ctx.cmd.rx_filter_add.qid = cpu_to_le32(qid);
  991. netdev_dbg(lif->netdev, "rx_filter add RXSTEER\n");
  992. err = ionic_adminq_post_wait(lif, &ctx);
  993. if (err && err != -EEXIST)
  994. return err;
  995. spin_lock_bh(&lif->rx_filters.lock);
  996. err = ionic_rx_filter_save(lif, 0, qid, 0, &ctx, IONIC_FILTER_STATE_SYNCED);
  997. spin_unlock_bh(&lif->rx_filters.lock);
  998. return err;
  999. }
  1000. int ionic_lif_set_hwstamp_rxfilt(struct ionic_lif *lif, u64 pkt_class)
  1001. {
  1002. ionic_lif_del_hwstamp_rxfilt(lif);
  1003. if (!pkt_class)
  1004. return 0;
  1005. return ionic_lif_add_hwstamp_rxfilt(lif, pkt_class);
  1006. }
  1007. static int ionic_adminq_napi(struct napi_struct *napi, int budget)
  1008. {
  1009. struct ionic_intr_info *intr = napi_to_cq(napi)->bound_intr;
  1010. struct ionic_lif *lif = napi_to_cq(napi)->lif;
  1011. struct ionic_dev *idev = &lif->ionic->idev;
  1012. unsigned long irqflags;
  1013. unsigned int flags = 0;
  1014. int rx_work = 0;
  1015. int tx_work = 0;
  1016. int n_work = 0;
  1017. int a_work = 0;
  1018. int work_done;
  1019. int credits;
  1020. if (lif->notifyqcq && lif->notifyqcq->flags & IONIC_QCQ_F_INITED)
  1021. n_work = ionic_cq_service(&lif->notifyqcq->cq, budget,
  1022. ionic_notifyq_service, NULL, NULL);
  1023. spin_lock_irqsave(&lif->adminq_lock, irqflags);
  1024. if (lif->adminqcq && lif->adminqcq->flags & IONIC_QCQ_F_INITED)
  1025. a_work = ionic_cq_service(&lif->adminqcq->cq, budget,
  1026. ionic_adminq_service, NULL, NULL);
  1027. spin_unlock_irqrestore(&lif->adminq_lock, irqflags);
  1028. if (lif->hwstamp_rxq)
  1029. rx_work = ionic_cq_service(&lif->hwstamp_rxq->cq, budget,
  1030. ionic_rx_service, NULL, NULL);
  1031. if (lif->hwstamp_txq)
  1032. tx_work = ionic_tx_cq_service(&lif->hwstamp_txq->cq, budget, !!budget);
  1033. work_done = max(max(n_work, a_work), max(rx_work, tx_work));
  1034. if (work_done < budget && napi_complete_done(napi, work_done)) {
  1035. flags |= IONIC_INTR_CRED_UNMASK;
  1036. intr->rearm_count++;
  1037. }
  1038. if (work_done || flags) {
  1039. flags |= IONIC_INTR_CRED_RESET_COALESCE;
  1040. credits = n_work + a_work + rx_work + tx_work;
  1041. ionic_intr_credits(idev->intr_ctrl, intr->index, credits, flags);
  1042. }
  1043. if (lif->doorbell_wa) {
  1044. if (!a_work)
  1045. ionic_adminq_poke_doorbell(&lif->adminqcq->q);
  1046. if (lif->hwstamp_rxq && !rx_work)
  1047. ionic_rxq_poke_doorbell(&lif->hwstamp_rxq->q);
  1048. if (lif->hwstamp_txq && !tx_work)
  1049. ionic_txq_poke_doorbell(&lif->hwstamp_txq->q);
  1050. }
  1051. return work_done;
  1052. }
  1053. void ionic_get_stats64(struct net_device *netdev,
  1054. struct rtnl_link_stats64 *ns)
  1055. {
  1056. struct ionic_lif *lif = netdev_priv(netdev);
  1057. struct ionic_lif_stats *ls;
  1058. memset(ns, 0, sizeof(*ns));
  1059. ls = &lif->info->stats;
  1060. ns->rx_packets = le64_to_cpu(ls->rx_ucast_packets) +
  1061. le64_to_cpu(ls->rx_mcast_packets) +
  1062. le64_to_cpu(ls->rx_bcast_packets);
  1063. ns->tx_packets = le64_to_cpu(ls->tx_ucast_packets) +
  1064. le64_to_cpu(ls->tx_mcast_packets) +
  1065. le64_to_cpu(ls->tx_bcast_packets);
  1066. ns->rx_bytes = le64_to_cpu(ls->rx_ucast_bytes) +
  1067. le64_to_cpu(ls->rx_mcast_bytes) +
  1068. le64_to_cpu(ls->rx_bcast_bytes);
  1069. ns->tx_bytes = le64_to_cpu(ls->tx_ucast_bytes) +
  1070. le64_to_cpu(ls->tx_mcast_bytes) +
  1071. le64_to_cpu(ls->tx_bcast_bytes);
  1072. ns->rx_dropped = le64_to_cpu(ls->rx_ucast_drop_packets) +
  1073. le64_to_cpu(ls->rx_mcast_drop_packets) +
  1074. le64_to_cpu(ls->rx_bcast_drop_packets);
  1075. ns->tx_dropped = le64_to_cpu(ls->tx_ucast_drop_packets) +
  1076. le64_to_cpu(ls->tx_mcast_drop_packets) +
  1077. le64_to_cpu(ls->tx_bcast_drop_packets);
  1078. ns->multicast = le64_to_cpu(ls->rx_mcast_packets);
  1079. ns->rx_over_errors = le64_to_cpu(ls->rx_queue_empty);
  1080. ns->rx_missed_errors = le64_to_cpu(ls->rx_dma_error) +
  1081. le64_to_cpu(ls->rx_queue_disabled) +
  1082. le64_to_cpu(ls->rx_desc_fetch_error) +
  1083. le64_to_cpu(ls->rx_desc_data_error);
  1084. ns->tx_aborted_errors = le64_to_cpu(ls->tx_dma_error) +
  1085. le64_to_cpu(ls->tx_queue_disabled) +
  1086. le64_to_cpu(ls->tx_desc_fetch_error) +
  1087. le64_to_cpu(ls->tx_desc_data_error);
  1088. ns->rx_errors = ns->rx_over_errors +
  1089. ns->rx_missed_errors;
  1090. ns->tx_errors = ns->tx_aborted_errors;
  1091. }
  1092. static int ionic_addr_add(struct net_device *netdev, const u8 *addr)
  1093. {
  1094. return ionic_lif_list_addr(netdev_priv(netdev), addr, ADD_ADDR);
  1095. }
  1096. static int ionic_addr_del(struct net_device *netdev, const u8 *addr)
  1097. {
  1098. /* Don't delete our own address from the uc list */
  1099. if (ether_addr_equal(addr, netdev->dev_addr))
  1100. return 0;
  1101. return ionic_lif_list_addr(netdev_priv(netdev), addr, DEL_ADDR);
  1102. }
  1103. void ionic_lif_rx_mode(struct ionic_lif *lif)
  1104. {
  1105. struct net_device *netdev = lif->netdev;
  1106. unsigned int nfilters;
  1107. unsigned int nd_flags;
  1108. char buf[128];
  1109. u16 rx_mode;
  1110. int i;
  1111. #define REMAIN(__x) (sizeof(buf) - (__x))
  1112. mutex_lock(&lif->config_lock);
  1113. /* grab the flags once for local use */
  1114. nd_flags = netdev->flags;
  1115. rx_mode = IONIC_RX_MODE_F_UNICAST;
  1116. rx_mode |= (nd_flags & IFF_MULTICAST) ? IONIC_RX_MODE_F_MULTICAST : 0;
  1117. rx_mode |= (nd_flags & IFF_BROADCAST) ? IONIC_RX_MODE_F_BROADCAST : 0;
  1118. rx_mode |= (nd_flags & IFF_PROMISC) ? IONIC_RX_MODE_F_PROMISC : 0;
  1119. rx_mode |= (nd_flags & IFF_ALLMULTI) ? IONIC_RX_MODE_F_ALLMULTI : 0;
  1120. /* sync the filters */
  1121. ionic_rx_filter_sync(lif);
  1122. /* check for overflow state
  1123. * if so, we track that we overflowed and enable NIC PROMISC
  1124. * else if the overflow is set and not needed
  1125. * we remove our overflow flag and check the netdev flags
  1126. * to see if we can disable NIC PROMISC
  1127. */
  1128. nfilters = le32_to_cpu(lif->identity->eth.max_ucast_filters);
  1129. if (((lif->nucast + lif->nmcast) >= nfilters) ||
  1130. (lif->max_vlans && lif->nvlans >= lif->max_vlans)) {
  1131. rx_mode |= IONIC_RX_MODE_F_PROMISC;
  1132. rx_mode |= IONIC_RX_MODE_F_ALLMULTI;
  1133. } else {
  1134. if (!(nd_flags & IFF_PROMISC))
  1135. rx_mode &= ~IONIC_RX_MODE_F_PROMISC;
  1136. if (!(nd_flags & IFF_ALLMULTI))
  1137. rx_mode &= ~IONIC_RX_MODE_F_ALLMULTI;
  1138. }
  1139. i = scnprintf(buf, sizeof(buf), "rx_mode 0x%04x -> 0x%04x:",
  1140. lif->rx_mode, rx_mode);
  1141. if (rx_mode & IONIC_RX_MODE_F_UNICAST)
  1142. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_UNICAST");
  1143. if (rx_mode & IONIC_RX_MODE_F_MULTICAST)
  1144. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_MULTICAST");
  1145. if (rx_mode & IONIC_RX_MODE_F_BROADCAST)
  1146. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_BROADCAST");
  1147. if (rx_mode & IONIC_RX_MODE_F_PROMISC)
  1148. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_PROMISC");
  1149. if (rx_mode & IONIC_RX_MODE_F_ALLMULTI)
  1150. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_ALLMULTI");
  1151. if (rx_mode & IONIC_RX_MODE_F_RDMA_SNIFFER)
  1152. i += scnprintf(&buf[i], REMAIN(i), " RX_MODE_F_RDMA_SNIFFER");
  1153. netdev_dbg(netdev, "lif%d %s\n", lif->index, buf);
  1154. if (lif->rx_mode != rx_mode) {
  1155. struct ionic_admin_ctx ctx = {
  1156. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1157. .cmd.rx_mode_set = {
  1158. .opcode = IONIC_CMD_RX_MODE_SET,
  1159. .lif_index = cpu_to_le16(lif->index),
  1160. },
  1161. };
  1162. int err;
  1163. ctx.cmd.rx_mode_set.rx_mode = cpu_to_le16(rx_mode);
  1164. err = ionic_adminq_post_wait(lif, &ctx);
  1165. if (err)
  1166. netdev_warn(netdev, "set rx_mode 0x%04x failed: %d\n",
  1167. rx_mode, err);
  1168. else
  1169. lif->rx_mode = rx_mode;
  1170. }
  1171. mutex_unlock(&lif->config_lock);
  1172. }
  1173. static void ionic_ndo_set_rx_mode(struct net_device *netdev)
  1174. {
  1175. struct ionic_lif *lif = netdev_priv(netdev);
  1176. struct ionic_deferred_work *work;
  1177. /* Sync the kernel filter list with the driver filter list */
  1178. __dev_uc_sync(netdev, ionic_addr_add, ionic_addr_del);
  1179. __dev_mc_sync(netdev, ionic_addr_add, ionic_addr_del);
  1180. /* Shove off the rest of the rxmode work to the work task
  1181. * which will include syncing the filters to the firmware.
  1182. */
  1183. work = kzalloc_obj(*work, GFP_ATOMIC);
  1184. if (!work) {
  1185. netdev_err(lif->netdev, "rxmode change dropped\n");
  1186. return;
  1187. }
  1188. work->type = IONIC_DW_TYPE_RX_MODE;
  1189. netdev_dbg(lif->netdev, "deferred: rx_mode\n");
  1190. ionic_lif_deferred_enqueue(lif, work);
  1191. }
  1192. static __le64 ionic_netdev_features_to_nic(netdev_features_t features)
  1193. {
  1194. u64 wanted = 0;
  1195. if (features & NETIF_F_HW_VLAN_CTAG_TX)
  1196. wanted |= IONIC_ETH_HW_VLAN_TX_TAG;
  1197. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1198. wanted |= IONIC_ETH_HW_VLAN_RX_STRIP;
  1199. if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
  1200. wanted |= IONIC_ETH_HW_VLAN_RX_FILTER;
  1201. if (features & NETIF_F_RXHASH)
  1202. wanted |= IONIC_ETH_HW_RX_HASH;
  1203. if (features & NETIF_F_RXCSUM)
  1204. wanted |= IONIC_ETH_HW_RX_CSUM;
  1205. if (features & NETIF_F_SG)
  1206. wanted |= IONIC_ETH_HW_TX_SG;
  1207. if (features & NETIF_F_HW_CSUM)
  1208. wanted |= IONIC_ETH_HW_TX_CSUM;
  1209. if (features & NETIF_F_TSO)
  1210. wanted |= IONIC_ETH_HW_TSO;
  1211. if (features & NETIF_F_TSO6)
  1212. wanted |= IONIC_ETH_HW_TSO_IPV6;
  1213. if (features & NETIF_F_TSO_ECN)
  1214. wanted |= IONIC_ETH_HW_TSO_ECN;
  1215. if (features & NETIF_F_GSO_GRE)
  1216. wanted |= IONIC_ETH_HW_TSO_GRE;
  1217. if (features & NETIF_F_GSO_GRE_CSUM)
  1218. wanted |= IONIC_ETH_HW_TSO_GRE_CSUM;
  1219. if (features & NETIF_F_GSO_IPXIP4)
  1220. wanted |= IONIC_ETH_HW_TSO_IPXIP4;
  1221. if (features & NETIF_F_GSO_IPXIP6)
  1222. wanted |= IONIC_ETH_HW_TSO_IPXIP6;
  1223. if (features & NETIF_F_GSO_UDP_TUNNEL)
  1224. wanted |= IONIC_ETH_HW_TSO_UDP;
  1225. if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM)
  1226. wanted |= IONIC_ETH_HW_TSO_UDP_CSUM;
  1227. return cpu_to_le64(wanted);
  1228. }
  1229. static int ionic_set_nic_features(struct ionic_lif *lif,
  1230. netdev_features_t features)
  1231. {
  1232. struct device *dev = lif->ionic->dev;
  1233. struct ionic_admin_ctx ctx = {
  1234. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1235. .cmd.lif_setattr = {
  1236. .opcode = IONIC_CMD_LIF_SETATTR,
  1237. .index = cpu_to_le16(lif->index),
  1238. .attr = IONIC_LIF_ATTR_FEATURES,
  1239. },
  1240. };
  1241. u64 vlan_flags = IONIC_ETH_HW_VLAN_TX_TAG |
  1242. IONIC_ETH_HW_VLAN_RX_STRIP |
  1243. IONIC_ETH_HW_VLAN_RX_FILTER;
  1244. u64 old_hw_features;
  1245. int err;
  1246. ctx.cmd.lif_setattr.features = ionic_netdev_features_to_nic(features);
  1247. if (lif->phc)
  1248. ctx.cmd.lif_setattr.features |= cpu_to_le64(IONIC_ETH_HW_TIMESTAMP);
  1249. err = ionic_adminq_post_wait(lif, &ctx);
  1250. if (err)
  1251. return err;
  1252. old_hw_features = lif->hw_features;
  1253. lif->hw_features = le64_to_cpu(ctx.cmd.lif_setattr.features &
  1254. ctx.comp.lif_setattr.features);
  1255. if ((old_hw_features ^ lif->hw_features) & IONIC_ETH_HW_RX_HASH)
  1256. ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
  1257. if ((vlan_flags & le64_to_cpu(ctx.cmd.lif_setattr.features)) &&
  1258. !(vlan_flags & le64_to_cpu(ctx.comp.lif_setattr.features)))
  1259. dev_info_once(lif->ionic->dev, "NIC is not supporting vlan offload, likely in SmartNIC mode\n");
  1260. if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
  1261. dev_dbg(dev, "feature ETH_HW_VLAN_TX_TAG\n");
  1262. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
  1263. dev_dbg(dev, "feature ETH_HW_VLAN_RX_STRIP\n");
  1264. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
  1265. dev_dbg(dev, "feature ETH_HW_VLAN_RX_FILTER\n");
  1266. if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
  1267. dev_dbg(dev, "feature ETH_HW_RX_HASH\n");
  1268. if (lif->hw_features & IONIC_ETH_HW_TX_SG)
  1269. dev_dbg(dev, "feature ETH_HW_TX_SG\n");
  1270. if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
  1271. dev_dbg(dev, "feature ETH_HW_TX_CSUM\n");
  1272. if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
  1273. dev_dbg(dev, "feature ETH_HW_RX_CSUM\n");
  1274. if (lif->hw_features & IONIC_ETH_HW_TSO)
  1275. dev_dbg(dev, "feature ETH_HW_TSO\n");
  1276. if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
  1277. dev_dbg(dev, "feature ETH_HW_TSO_IPV6\n");
  1278. if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
  1279. dev_dbg(dev, "feature ETH_HW_TSO_ECN\n");
  1280. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
  1281. dev_dbg(dev, "feature ETH_HW_TSO_GRE\n");
  1282. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
  1283. dev_dbg(dev, "feature ETH_HW_TSO_GRE_CSUM\n");
  1284. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
  1285. dev_dbg(dev, "feature ETH_HW_TSO_IPXIP4\n");
  1286. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
  1287. dev_dbg(dev, "feature ETH_HW_TSO_IPXIP6\n");
  1288. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
  1289. dev_dbg(dev, "feature ETH_HW_TSO_UDP\n");
  1290. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
  1291. dev_dbg(dev, "feature ETH_HW_TSO_UDP_CSUM\n");
  1292. if (lif->hw_features & IONIC_ETH_HW_TIMESTAMP)
  1293. dev_dbg(dev, "feature ETH_HW_TIMESTAMP\n");
  1294. return 0;
  1295. }
  1296. static int ionic_init_nic_features(struct ionic_lif *lif)
  1297. {
  1298. struct net_device *netdev = lif->netdev;
  1299. netdev_features_t features;
  1300. int err;
  1301. /* set up what we expect to support by default */
  1302. features = NETIF_F_HW_VLAN_CTAG_TX |
  1303. NETIF_F_HW_VLAN_CTAG_RX |
  1304. NETIF_F_HW_VLAN_CTAG_FILTER |
  1305. NETIF_F_SG |
  1306. NETIF_F_HW_CSUM |
  1307. NETIF_F_RXCSUM |
  1308. NETIF_F_TSO |
  1309. NETIF_F_TSO6 |
  1310. NETIF_F_TSO_ECN |
  1311. NETIF_F_GSO_GRE |
  1312. NETIF_F_GSO_GRE_CSUM |
  1313. NETIF_F_GSO_IPXIP4 |
  1314. NETIF_F_GSO_IPXIP6 |
  1315. NETIF_F_GSO_UDP_TUNNEL |
  1316. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  1317. if (lif->nxqs > 1)
  1318. features |= NETIF_F_RXHASH;
  1319. err = ionic_set_nic_features(lif, features);
  1320. if (err)
  1321. return err;
  1322. /* tell the netdev what we actually can support */
  1323. netdev->features |= NETIF_F_HIGHDMA;
  1324. if (lif->hw_features & IONIC_ETH_HW_VLAN_TX_TAG)
  1325. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
  1326. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_STRIP)
  1327. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
  1328. if (lif->hw_features & IONIC_ETH_HW_VLAN_RX_FILTER)
  1329. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1330. if (lif->hw_features & IONIC_ETH_HW_RX_HASH)
  1331. netdev->hw_features |= NETIF_F_RXHASH;
  1332. if (lif->hw_features & IONIC_ETH_HW_TX_SG)
  1333. netdev->hw_features |= NETIF_F_SG;
  1334. if (lif->hw_features & IONIC_ETH_HW_TX_CSUM)
  1335. netdev->hw_enc_features |= NETIF_F_HW_CSUM;
  1336. if (lif->hw_features & IONIC_ETH_HW_RX_CSUM)
  1337. netdev->hw_enc_features |= NETIF_F_RXCSUM;
  1338. if (lif->hw_features & IONIC_ETH_HW_TSO)
  1339. netdev->hw_enc_features |= NETIF_F_TSO;
  1340. if (lif->hw_features & IONIC_ETH_HW_TSO_IPV6)
  1341. netdev->hw_enc_features |= NETIF_F_TSO6;
  1342. if (lif->hw_features & IONIC_ETH_HW_TSO_ECN)
  1343. netdev->hw_enc_features |= NETIF_F_TSO_ECN;
  1344. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE)
  1345. netdev->hw_enc_features |= NETIF_F_GSO_GRE;
  1346. if (lif->hw_features & IONIC_ETH_HW_TSO_GRE_CSUM)
  1347. netdev->hw_enc_features |= NETIF_F_GSO_GRE_CSUM;
  1348. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP4)
  1349. netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4;
  1350. if (lif->hw_features & IONIC_ETH_HW_TSO_IPXIP6)
  1351. netdev->hw_enc_features |= NETIF_F_GSO_IPXIP6;
  1352. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP)
  1353. netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
  1354. if (lif->hw_features & IONIC_ETH_HW_TSO_UDP_CSUM)
  1355. netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  1356. netdev->hw_features |= netdev->hw_enc_features;
  1357. netdev->features |= netdev->hw_features;
  1358. netdev->vlan_features |= netdev->features & ~NETIF_F_VLAN_FEATURES;
  1359. netdev->priv_flags |= IFF_UNICAST_FLT |
  1360. IFF_LIVE_ADDR_CHANGE;
  1361. netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
  1362. NETDEV_XDP_ACT_REDIRECT |
  1363. NETDEV_XDP_ACT_RX_SG |
  1364. NETDEV_XDP_ACT_NDO_XMIT |
  1365. NETDEV_XDP_ACT_NDO_XMIT_SG;
  1366. return 0;
  1367. }
  1368. static int ionic_set_features(struct net_device *netdev,
  1369. netdev_features_t features)
  1370. {
  1371. struct ionic_lif *lif = netdev_priv(netdev);
  1372. int err;
  1373. netdev_dbg(netdev, "%s: lif->features=0x%08llx new_features=0x%08llx\n",
  1374. __func__, (u64)lif->netdev->features, (u64)features);
  1375. err = ionic_set_nic_features(lif, features);
  1376. return err;
  1377. }
  1378. static int ionic_set_attr_mac(struct ionic_lif *lif, u8 *mac)
  1379. {
  1380. struct ionic_admin_ctx ctx = {
  1381. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1382. .cmd.lif_setattr = {
  1383. .opcode = IONIC_CMD_LIF_SETATTR,
  1384. .index = cpu_to_le16(lif->index),
  1385. .attr = IONIC_LIF_ATTR_MAC,
  1386. },
  1387. };
  1388. ether_addr_copy(ctx.cmd.lif_setattr.mac, mac);
  1389. return ionic_adminq_post_wait(lif, &ctx);
  1390. }
  1391. static int ionic_get_attr_mac(struct ionic_lif *lif, u8 *mac_addr)
  1392. {
  1393. struct ionic_admin_ctx ctx = {
  1394. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1395. .cmd.lif_getattr = {
  1396. .opcode = IONIC_CMD_LIF_GETATTR,
  1397. .index = cpu_to_le16(lif->index),
  1398. .attr = IONIC_LIF_ATTR_MAC,
  1399. },
  1400. };
  1401. int err;
  1402. err = ionic_adminq_post_wait(lif, &ctx);
  1403. if (err)
  1404. return err;
  1405. ether_addr_copy(mac_addr, ctx.comp.lif_getattr.mac);
  1406. return 0;
  1407. }
  1408. static int ionic_program_mac(struct ionic_lif *lif, u8 *mac)
  1409. {
  1410. u8 get_mac[ETH_ALEN];
  1411. int err;
  1412. err = ionic_set_attr_mac(lif, mac);
  1413. if (err)
  1414. return err;
  1415. err = ionic_get_attr_mac(lif, get_mac);
  1416. if (err)
  1417. return err;
  1418. /* To deal with older firmware that silently ignores the set attr mac:
  1419. * doesn't actually change the mac and doesn't return an error, so we
  1420. * do the get attr to verify whether or not the set actually happened
  1421. */
  1422. if (!ether_addr_equal(get_mac, mac))
  1423. return 1;
  1424. return 0;
  1425. }
  1426. static int ionic_set_mac_address(struct net_device *netdev, void *sa)
  1427. {
  1428. struct ionic_lif *lif = netdev_priv(netdev);
  1429. struct sockaddr *addr = sa;
  1430. u8 *mac;
  1431. int err;
  1432. mac = (u8 *)addr->sa_data;
  1433. if (ether_addr_equal(netdev->dev_addr, mac))
  1434. return 0;
  1435. /* Only program macs for virtual functions to avoid losing the permanent
  1436. * Mac across warm reset/reboot.
  1437. */
  1438. if (lif->ionic->pdev->is_virtfn) {
  1439. err = ionic_program_mac(lif, mac);
  1440. if (err < 0)
  1441. return err;
  1442. if (err > 0)
  1443. netdev_dbg(netdev, "%s: SET and GET ATTR Mac are not equal-due to old FW running\n",
  1444. __func__);
  1445. }
  1446. err = eth_prepare_mac_addr_change(netdev, addr);
  1447. if (err)
  1448. return err;
  1449. if (!is_zero_ether_addr(netdev->dev_addr)) {
  1450. netdev_info(netdev, "deleting mac addr %pM\n",
  1451. netdev->dev_addr);
  1452. ionic_lif_addr_del(netdev_priv(netdev), netdev->dev_addr);
  1453. }
  1454. eth_commit_mac_addr_change(netdev, addr);
  1455. netdev_info(netdev, "updating mac addr %pM\n", mac);
  1456. return ionic_lif_addr_add(netdev_priv(netdev), mac);
  1457. }
  1458. void ionic_stop_queues_reconfig(struct ionic_lif *lif)
  1459. {
  1460. /* Stop and clean the queues before reconfiguration */
  1461. netif_device_detach(lif->netdev);
  1462. ionic_stop_queues(lif);
  1463. ionic_txrx_deinit(lif);
  1464. }
  1465. static int ionic_start_queues_reconfig(struct ionic_lif *lif)
  1466. {
  1467. int err;
  1468. /* Re-init the queues after reconfiguration */
  1469. /* The only way txrx_init can fail here is if communication
  1470. * with FW is suddenly broken. There's not much we can do
  1471. * at this point - error messages have already been printed,
  1472. * so we can continue on and the user can eventually do a
  1473. * DOWN and UP to try to reset and clear the issue.
  1474. */
  1475. err = ionic_txrx_init(lif);
  1476. ionic_link_status_check_request(lif, CAN_NOT_SLEEP);
  1477. netif_device_attach(lif->netdev);
  1478. return err;
  1479. }
  1480. static bool ionic_xdp_is_valid_mtu(struct ionic_lif *lif, u32 mtu,
  1481. struct bpf_prog *xdp_prog)
  1482. {
  1483. if (!xdp_prog)
  1484. return true;
  1485. if (mtu <= IONIC_XDP_MAX_LINEAR_MTU)
  1486. return true;
  1487. if (xdp_prog->aux && xdp_prog->aux->xdp_has_frags)
  1488. return true;
  1489. return false;
  1490. }
  1491. static int ionic_change_mtu(struct net_device *netdev, int new_mtu)
  1492. {
  1493. struct ionic_lif *lif = netdev_priv(netdev);
  1494. struct ionic_admin_ctx ctx = {
  1495. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1496. .cmd.lif_setattr = {
  1497. .opcode = IONIC_CMD_LIF_SETATTR,
  1498. .index = cpu_to_le16(lif->index),
  1499. .attr = IONIC_LIF_ATTR_MTU,
  1500. .mtu = cpu_to_le32(new_mtu),
  1501. },
  1502. };
  1503. struct bpf_prog *xdp_prog;
  1504. int err;
  1505. xdp_prog = READ_ONCE(lif->xdp_prog);
  1506. if (!ionic_xdp_is_valid_mtu(lif, new_mtu, xdp_prog))
  1507. return -EINVAL;
  1508. err = ionic_adminq_post_wait(lif, &ctx);
  1509. if (err)
  1510. return err;
  1511. /* if we're not running, nothing more to do */
  1512. if (!netif_running(netdev)) {
  1513. WRITE_ONCE(netdev->mtu, new_mtu);
  1514. return 0;
  1515. }
  1516. mutex_lock(&lif->queue_lock);
  1517. ionic_stop_queues_reconfig(lif);
  1518. WRITE_ONCE(netdev->mtu, new_mtu);
  1519. err = ionic_start_queues_reconfig(lif);
  1520. mutex_unlock(&lif->queue_lock);
  1521. return err;
  1522. }
  1523. static void ionic_tx_timeout_work(struct work_struct *ws)
  1524. {
  1525. struct ionic_lif *lif = container_of(ws, struct ionic_lif, tx_timeout_work);
  1526. int err;
  1527. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  1528. return;
  1529. /* if we were stopped before this scheduled job was launched,
  1530. * don't bother the queues as they are already stopped.
  1531. */
  1532. if (!netif_running(lif->netdev))
  1533. return;
  1534. mutex_lock(&lif->queue_lock);
  1535. ionic_stop_queues_reconfig(lif);
  1536. err = ionic_start_queues_reconfig(lif);
  1537. mutex_unlock(&lif->queue_lock);
  1538. if (err)
  1539. dev_err(lif->ionic->dev, "%s: Restarting queues failed\n", __func__);
  1540. }
  1541. static void ionic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  1542. {
  1543. struct ionic_lif *lif = netdev_priv(netdev);
  1544. netdev_info(lif->netdev, "Tx Timeout triggered - txq %d\n", txqueue);
  1545. schedule_work(&lif->tx_timeout_work);
  1546. }
  1547. static int ionic_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
  1548. u16 vid)
  1549. {
  1550. struct ionic_lif *lif = netdev_priv(netdev);
  1551. int err;
  1552. err = ionic_lif_vlan_add(lif, vid);
  1553. if (err)
  1554. return err;
  1555. ionic_lif_rx_mode(lif);
  1556. return 0;
  1557. }
  1558. static int ionic_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
  1559. u16 vid)
  1560. {
  1561. struct ionic_lif *lif = netdev_priv(netdev);
  1562. int err;
  1563. err = ionic_lif_vlan_del(lif, vid);
  1564. if (err)
  1565. return err;
  1566. ionic_lif_rx_mode(lif);
  1567. return 0;
  1568. }
  1569. int ionic_lif_rss_config(struct ionic_lif *lif, const u16 types,
  1570. const u8 *key, const u32 *indir)
  1571. {
  1572. struct ionic_admin_ctx ctx = {
  1573. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1574. .cmd.lif_setattr = {
  1575. .opcode = IONIC_CMD_LIF_SETATTR,
  1576. .attr = IONIC_LIF_ATTR_RSS,
  1577. .rss.addr = cpu_to_le64(lif->rss_ind_tbl_pa),
  1578. },
  1579. };
  1580. unsigned int i, tbl_sz;
  1581. if (lif->hw_features & IONIC_ETH_HW_RX_HASH) {
  1582. lif->rss_types = types;
  1583. ctx.cmd.lif_setattr.rss.types = cpu_to_le16(types);
  1584. }
  1585. if (key)
  1586. memcpy(lif->rss_hash_key, key, IONIC_RSS_HASH_KEY_SIZE);
  1587. if (indir) {
  1588. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  1589. for (i = 0; i < tbl_sz; i++)
  1590. lif->rss_ind_tbl[i] = indir[i];
  1591. }
  1592. memcpy(ctx.cmd.lif_setattr.rss.key, lif->rss_hash_key,
  1593. IONIC_RSS_HASH_KEY_SIZE);
  1594. return ionic_adminq_post_wait(lif, &ctx);
  1595. }
  1596. static int ionic_lif_rss_init(struct ionic_lif *lif)
  1597. {
  1598. unsigned int tbl_sz;
  1599. unsigned int i;
  1600. lif->rss_types = IONIC_RSS_TYPE_IPV4 |
  1601. IONIC_RSS_TYPE_IPV4_TCP |
  1602. IONIC_RSS_TYPE_IPV4_UDP |
  1603. IONIC_RSS_TYPE_IPV6 |
  1604. IONIC_RSS_TYPE_IPV6_TCP |
  1605. IONIC_RSS_TYPE_IPV6_UDP;
  1606. /* Fill indirection table with 'default' values */
  1607. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  1608. for (i = 0; i < tbl_sz; i++)
  1609. lif->rss_ind_tbl[i] = ethtool_rxfh_indir_default(i, lif->nxqs);
  1610. return ionic_lif_rss_config(lif, lif->rss_types, NULL, NULL);
  1611. }
  1612. static void ionic_lif_rss_deinit(struct ionic_lif *lif)
  1613. {
  1614. int tbl_sz;
  1615. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  1616. memset(lif->rss_ind_tbl, 0, tbl_sz);
  1617. memset(lif->rss_hash_key, 0, IONIC_RSS_HASH_KEY_SIZE);
  1618. ionic_lif_rss_config(lif, 0x0, NULL, NULL);
  1619. }
  1620. static void ionic_lif_quiesce(struct ionic_lif *lif)
  1621. {
  1622. struct ionic_admin_ctx ctx = {
  1623. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  1624. .cmd.lif_setattr = {
  1625. .opcode = IONIC_CMD_LIF_SETATTR,
  1626. .index = cpu_to_le16(lif->index),
  1627. .attr = IONIC_LIF_ATTR_STATE,
  1628. .state = IONIC_LIF_QUIESCE,
  1629. },
  1630. };
  1631. int err;
  1632. err = ionic_adminq_post_wait(lif, &ctx);
  1633. if (err)
  1634. netdev_dbg(lif->netdev, "lif quiesce failed %d\n", err);
  1635. }
  1636. static void ionic_txrx_disable(struct ionic_lif *lif)
  1637. {
  1638. unsigned int i;
  1639. int err = 0;
  1640. if (lif->txqcqs) {
  1641. for (i = 0; i < lif->nxqs; i++)
  1642. err = ionic_qcq_disable(lif, lif->txqcqs[i], err);
  1643. }
  1644. if (lif->hwstamp_txq)
  1645. err = ionic_qcq_disable(lif, lif->hwstamp_txq, err);
  1646. if (lif->rxqcqs) {
  1647. for (i = 0; i < lif->nxqs; i++)
  1648. err = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
  1649. }
  1650. if (lif->hwstamp_rxq)
  1651. err = ionic_qcq_disable(lif, lif->hwstamp_rxq, err);
  1652. ionic_lif_quiesce(lif);
  1653. }
  1654. static void ionic_txrx_deinit(struct ionic_lif *lif)
  1655. {
  1656. unsigned int i;
  1657. if (lif->txqcqs) {
  1658. for (i = 0; i < lif->nxqs && lif->txqcqs[i]; i++) {
  1659. ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
  1660. ionic_tx_flush(&lif->txqcqs[i]->cq);
  1661. ionic_tx_empty(&lif->txqcqs[i]->q);
  1662. }
  1663. }
  1664. if (lif->rxqcqs) {
  1665. for (i = 0; i < lif->nxqs && lif->rxqcqs[i]; i++) {
  1666. ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
  1667. ionic_rx_empty(&lif->rxqcqs[i]->q);
  1668. }
  1669. }
  1670. lif->rx_mode = 0;
  1671. if (lif->hwstamp_txq) {
  1672. ionic_lif_qcq_deinit(lif, lif->hwstamp_txq);
  1673. ionic_tx_flush(&lif->hwstamp_txq->cq);
  1674. ionic_tx_empty(&lif->hwstamp_txq->q);
  1675. }
  1676. if (lif->hwstamp_rxq) {
  1677. ionic_lif_qcq_deinit(lif, lif->hwstamp_rxq);
  1678. ionic_rx_empty(&lif->hwstamp_rxq->q);
  1679. }
  1680. }
  1681. void ionic_txrx_free(struct ionic_lif *lif)
  1682. {
  1683. unsigned int i;
  1684. if (lif->txqcqs) {
  1685. for (i = 0; i < lif->ionic->ntxqs_per_lif && lif->txqcqs[i]; i++) {
  1686. ionic_qcq_free(lif, lif->txqcqs[i]);
  1687. devm_kfree(lif->ionic->dev, lif->txqcqs[i]);
  1688. lif->txqcqs[i] = NULL;
  1689. }
  1690. }
  1691. if (lif->rxqcqs) {
  1692. for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
  1693. ionic_qcq_free(lif, lif->rxqcqs[i]);
  1694. devm_kfree(lif->ionic->dev, lif->rxqcqs[i]);
  1695. lif->rxqcqs[i] = NULL;
  1696. }
  1697. }
  1698. if (lif->hwstamp_txq) {
  1699. ionic_qcq_free(lif, lif->hwstamp_txq);
  1700. devm_kfree(lif->ionic->dev, lif->hwstamp_txq);
  1701. lif->hwstamp_txq = NULL;
  1702. }
  1703. if (lif->hwstamp_rxq) {
  1704. ionic_qcq_free(lif, lif->hwstamp_rxq);
  1705. devm_kfree(lif->ionic->dev, lif->hwstamp_rxq);
  1706. lif->hwstamp_rxq = NULL;
  1707. }
  1708. }
  1709. static int ionic_txrx_alloc(struct ionic_lif *lif)
  1710. {
  1711. unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
  1712. unsigned int flags, i;
  1713. int err = 0;
  1714. num_desc = lif->ntxq_descs;
  1715. desc_sz = sizeof(struct ionic_txq_desc);
  1716. comp_sz = sizeof(struct ionic_txq_comp);
  1717. if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
  1718. lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
  1719. sizeof(struct ionic_txq_sg_desc_v1))
  1720. sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
  1721. else
  1722. sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
  1723. flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
  1724. if (test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state))
  1725. flags |= IONIC_QCQ_F_CMB_RINGS;
  1726. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  1727. flags |= IONIC_QCQ_F_INTR;
  1728. for (i = 0; i < lif->nxqs; i++) {
  1729. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
  1730. num_desc, desc_sz, comp_sz, sg_desc_sz,
  1731. sizeof(struct ionic_tx_desc_info),
  1732. lif->kern_pid, NULL, &lif->txqcqs[i]);
  1733. if (err)
  1734. goto err_out;
  1735. if (flags & IONIC_QCQ_F_INTR) {
  1736. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  1737. lif->txqcqs[i]->intr.index,
  1738. lif->tx_coalesce_hw);
  1739. if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
  1740. lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
  1741. }
  1742. ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
  1743. }
  1744. flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG | IONIC_QCQ_F_INTR;
  1745. if (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state))
  1746. flags |= IONIC_QCQ_F_CMB_RINGS;
  1747. num_desc = lif->nrxq_descs;
  1748. desc_sz = sizeof(struct ionic_rxq_desc);
  1749. comp_sz = sizeof(struct ionic_rxq_comp);
  1750. sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
  1751. if (lif->rxq_features & IONIC_Q_F_2X_CQ_DESC)
  1752. comp_sz *= 2;
  1753. for (i = 0; i < lif->nxqs; i++) {
  1754. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
  1755. num_desc, desc_sz, comp_sz, sg_desc_sz,
  1756. sizeof(struct ionic_rx_desc_info),
  1757. lif->kern_pid, lif->xdp_prog,
  1758. &lif->rxqcqs[i]);
  1759. if (err)
  1760. goto err_out;
  1761. lif->rxqcqs[i]->q.features = lif->rxq_features;
  1762. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  1763. lif->rxqcqs[i]->intr.index,
  1764. lif->rx_coalesce_hw);
  1765. if (test_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state))
  1766. lif->rxqcqs[i]->intr.dim_coal_hw = lif->rx_coalesce_hw;
  1767. if (!test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state))
  1768. ionic_link_qcq_interrupts(lif->rxqcqs[i],
  1769. lif->txqcqs[i]);
  1770. ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
  1771. }
  1772. return 0;
  1773. err_out:
  1774. ionic_txrx_free(lif);
  1775. return err;
  1776. }
  1777. static int ionic_txrx_init(struct ionic_lif *lif)
  1778. {
  1779. unsigned int i;
  1780. int err;
  1781. for (i = 0; i < lif->nxqs; i++) {
  1782. err = ionic_lif_txq_init(lif, lif->txqcqs[i]);
  1783. if (err)
  1784. goto err_out;
  1785. err = ionic_lif_rxq_init(lif, lif->rxqcqs[i]);
  1786. if (err) {
  1787. ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
  1788. goto err_out;
  1789. }
  1790. }
  1791. if (lif->netdev->features & NETIF_F_RXHASH)
  1792. ionic_lif_rss_init(lif);
  1793. ionic_lif_rx_mode(lif);
  1794. return 0;
  1795. err_out:
  1796. while (i--) {
  1797. ionic_lif_qcq_deinit(lif, lif->txqcqs[i]);
  1798. ionic_lif_qcq_deinit(lif, lif->rxqcqs[i]);
  1799. }
  1800. return err;
  1801. }
  1802. static int ionic_txrx_enable(struct ionic_lif *lif)
  1803. {
  1804. int derr = 0;
  1805. int i, err;
  1806. ionic_xdp_rxqs_prog_update(lif);
  1807. for (i = 0; i < lif->nxqs; i++) {
  1808. if (!(lif->rxqcqs[i] && lif->txqcqs[i])) {
  1809. dev_err(lif->ionic->dev, "%s: bad qcq %d\n", __func__, i);
  1810. err = -ENXIO;
  1811. goto err_out;
  1812. }
  1813. ionic_rx_fill(&lif->rxqcqs[i]->q,
  1814. READ_ONCE(lif->rxqcqs[i]->q.xdp_prog));
  1815. err = ionic_qcq_enable(lif->rxqcqs[i]);
  1816. if (err)
  1817. goto err_out;
  1818. err = ionic_qcq_enable(lif->txqcqs[i]);
  1819. if (err) {
  1820. derr = ionic_qcq_disable(lif, lif->rxqcqs[i], err);
  1821. goto err_out;
  1822. }
  1823. }
  1824. if (lif->hwstamp_rxq) {
  1825. ionic_rx_fill(&lif->hwstamp_rxq->q, NULL);
  1826. err = ionic_qcq_enable(lif->hwstamp_rxq);
  1827. if (err)
  1828. goto err_out_hwstamp_rx;
  1829. }
  1830. if (lif->hwstamp_txq) {
  1831. err = ionic_qcq_enable(lif->hwstamp_txq);
  1832. if (err)
  1833. goto err_out_hwstamp_tx;
  1834. }
  1835. return 0;
  1836. err_out_hwstamp_tx:
  1837. if (lif->hwstamp_rxq)
  1838. derr = ionic_qcq_disable(lif, lif->hwstamp_rxq, derr);
  1839. err_out_hwstamp_rx:
  1840. i = lif->nxqs;
  1841. err_out:
  1842. while (i--) {
  1843. derr = ionic_qcq_disable(lif, lif->txqcqs[i], derr);
  1844. derr = ionic_qcq_disable(lif, lif->rxqcqs[i], derr);
  1845. }
  1846. ionic_xdp_rxqs_prog_update(lif);
  1847. return err;
  1848. }
  1849. static int ionic_start_queues(struct ionic_lif *lif)
  1850. {
  1851. int err;
  1852. if (test_bit(IONIC_LIF_F_BROKEN, lif->state))
  1853. return -EIO;
  1854. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  1855. return -EBUSY;
  1856. if (test_and_set_bit(IONIC_LIF_F_UP, lif->state))
  1857. return 0;
  1858. err = ionic_txrx_enable(lif);
  1859. if (err) {
  1860. clear_bit(IONIC_LIF_F_UP, lif->state);
  1861. return err;
  1862. }
  1863. netif_tx_wake_all_queues(lif->netdev);
  1864. return 0;
  1865. }
  1866. static int ionic_open(struct net_device *netdev)
  1867. {
  1868. struct ionic_lif *lif = netdev_priv(netdev);
  1869. int err;
  1870. /* If recovering from a broken state, clear the bit and we'll try again */
  1871. if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
  1872. netdev_info(netdev, "clearing broken state\n");
  1873. mutex_lock(&lif->queue_lock);
  1874. err = ionic_txrx_alloc(lif);
  1875. if (err)
  1876. goto err_unlock;
  1877. err = ionic_txrx_init(lif);
  1878. if (err)
  1879. goto err_txrx_free;
  1880. err = netif_set_real_num_tx_queues(netdev, lif->nxqs);
  1881. if (err)
  1882. goto err_txrx_deinit;
  1883. err = netif_set_real_num_rx_queues(netdev, lif->nxqs);
  1884. if (err)
  1885. goto err_txrx_deinit;
  1886. /* don't start the queues until we have link */
  1887. if (netif_carrier_ok(netdev)) {
  1888. err = ionic_start_queues(lif);
  1889. if (err)
  1890. goto err_txrx_deinit;
  1891. }
  1892. /* If hardware timestamping is enabled, but the queues were freed by
  1893. * ionic_stop, those need to be reallocated and initialized, too.
  1894. */
  1895. ionic_lif_hwstamp_recreate_queues(lif);
  1896. mutex_unlock(&lif->queue_lock);
  1897. return 0;
  1898. err_txrx_deinit:
  1899. ionic_txrx_deinit(lif);
  1900. err_txrx_free:
  1901. ionic_txrx_free(lif);
  1902. err_unlock:
  1903. mutex_unlock(&lif->queue_lock);
  1904. return err;
  1905. }
  1906. static void ionic_stop_queues(struct ionic_lif *lif)
  1907. {
  1908. if (!test_and_clear_bit(IONIC_LIF_F_UP, lif->state))
  1909. return;
  1910. netif_tx_disable(lif->netdev);
  1911. ionic_txrx_disable(lif);
  1912. }
  1913. static int ionic_stop(struct net_device *netdev)
  1914. {
  1915. struct ionic_lif *lif = netdev_priv(netdev);
  1916. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  1917. return 0;
  1918. mutex_lock(&lif->queue_lock);
  1919. ionic_stop_queues(lif);
  1920. ionic_txrx_deinit(lif);
  1921. ionic_txrx_free(lif);
  1922. mutex_unlock(&lif->queue_lock);
  1923. return 0;
  1924. }
  1925. static int ionic_get_vf_config(struct net_device *netdev,
  1926. int vf, struct ifla_vf_info *ivf)
  1927. {
  1928. struct ionic_lif *lif = netdev_priv(netdev);
  1929. struct ionic *ionic = lif->ionic;
  1930. int ret = 0;
  1931. if (!netif_device_present(netdev))
  1932. return -EBUSY;
  1933. down_read(&ionic->vf_op_lock);
  1934. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  1935. ret = -EINVAL;
  1936. } else {
  1937. struct ionic_vf *vfdata = &ionic->vfs[vf];
  1938. ivf->vf = vf;
  1939. ivf->qos = 0;
  1940. ivf->vlan = le16_to_cpu(vfdata->vlanid);
  1941. ivf->spoofchk = vfdata->spoofchk;
  1942. ivf->linkstate = vfdata->linkstate;
  1943. ivf->max_tx_rate = le32_to_cpu(vfdata->maxrate);
  1944. ivf->trusted = vfdata->trusted;
  1945. ether_addr_copy(ivf->mac, vfdata->macaddr);
  1946. }
  1947. up_read(&ionic->vf_op_lock);
  1948. return ret;
  1949. }
  1950. static int ionic_get_vf_stats(struct net_device *netdev, int vf,
  1951. struct ifla_vf_stats *vf_stats)
  1952. {
  1953. struct ionic_lif *lif = netdev_priv(netdev);
  1954. struct ionic *ionic = lif->ionic;
  1955. struct ionic_lif_stats *vs;
  1956. int ret = 0;
  1957. if (!netif_device_present(netdev))
  1958. return -EBUSY;
  1959. down_read(&ionic->vf_op_lock);
  1960. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  1961. ret = -EINVAL;
  1962. } else {
  1963. memset(vf_stats, 0, sizeof(*vf_stats));
  1964. vs = &ionic->vfs[vf].stats;
  1965. vf_stats->rx_packets = le64_to_cpu(vs->rx_ucast_packets);
  1966. vf_stats->tx_packets = le64_to_cpu(vs->tx_ucast_packets);
  1967. vf_stats->rx_bytes = le64_to_cpu(vs->rx_ucast_bytes);
  1968. vf_stats->tx_bytes = le64_to_cpu(vs->tx_ucast_bytes);
  1969. vf_stats->broadcast = le64_to_cpu(vs->rx_bcast_packets);
  1970. vf_stats->multicast = le64_to_cpu(vs->rx_mcast_packets);
  1971. vf_stats->rx_dropped = le64_to_cpu(vs->rx_ucast_drop_packets) +
  1972. le64_to_cpu(vs->rx_mcast_drop_packets) +
  1973. le64_to_cpu(vs->rx_bcast_drop_packets);
  1974. vf_stats->tx_dropped = le64_to_cpu(vs->tx_ucast_drop_packets) +
  1975. le64_to_cpu(vs->tx_mcast_drop_packets) +
  1976. le64_to_cpu(vs->tx_bcast_drop_packets);
  1977. }
  1978. up_read(&ionic->vf_op_lock);
  1979. return ret;
  1980. }
  1981. static int ionic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  1982. {
  1983. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_MAC };
  1984. struct ionic_lif *lif = netdev_priv(netdev);
  1985. struct ionic *ionic = lif->ionic;
  1986. int ret;
  1987. if (!(is_zero_ether_addr(mac) || is_valid_ether_addr(mac)))
  1988. return -EINVAL;
  1989. if (!netif_device_present(netdev))
  1990. return -EBUSY;
  1991. down_write(&ionic->vf_op_lock);
  1992. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  1993. ret = -EINVAL;
  1994. } else {
  1995. ether_addr_copy(vfc.macaddr, mac);
  1996. dev_dbg(ionic->dev, "%s: vf %d macaddr %pM\n",
  1997. __func__, vf, vfc.macaddr);
  1998. ret = ionic_set_vf_config(ionic, vf, &vfc);
  1999. if (!ret)
  2000. ether_addr_copy(ionic->vfs[vf].macaddr, mac);
  2001. }
  2002. up_write(&ionic->vf_op_lock);
  2003. return ret;
  2004. }
  2005. static int ionic_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan,
  2006. u8 qos, __be16 proto)
  2007. {
  2008. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_VLAN };
  2009. struct ionic_lif *lif = netdev_priv(netdev);
  2010. struct ionic *ionic = lif->ionic;
  2011. int ret;
  2012. /* until someday when we support qos */
  2013. if (qos)
  2014. return -EINVAL;
  2015. if (vlan > 4095)
  2016. return -EINVAL;
  2017. if (proto != htons(ETH_P_8021Q))
  2018. return -EPROTONOSUPPORT;
  2019. if (!netif_device_present(netdev))
  2020. return -EBUSY;
  2021. down_write(&ionic->vf_op_lock);
  2022. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2023. ret = -EINVAL;
  2024. } else {
  2025. vfc.vlanid = cpu_to_le16(vlan);
  2026. dev_dbg(ionic->dev, "%s: vf %d vlan %d\n",
  2027. __func__, vf, le16_to_cpu(vfc.vlanid));
  2028. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2029. if (!ret)
  2030. ionic->vfs[vf].vlanid = cpu_to_le16(vlan);
  2031. }
  2032. up_write(&ionic->vf_op_lock);
  2033. return ret;
  2034. }
  2035. static int ionic_set_vf_rate(struct net_device *netdev, int vf,
  2036. int tx_min, int tx_max)
  2037. {
  2038. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_RATE };
  2039. struct ionic_lif *lif = netdev_priv(netdev);
  2040. struct ionic *ionic = lif->ionic;
  2041. int ret;
  2042. /* setting the min just seems silly */
  2043. if (tx_min)
  2044. return -EINVAL;
  2045. if (!netif_device_present(netdev))
  2046. return -EBUSY;
  2047. down_write(&ionic->vf_op_lock);
  2048. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2049. ret = -EINVAL;
  2050. } else {
  2051. vfc.maxrate = cpu_to_le32(tx_max);
  2052. dev_dbg(ionic->dev, "%s: vf %d maxrate %d\n",
  2053. __func__, vf, le32_to_cpu(vfc.maxrate));
  2054. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2055. if (!ret)
  2056. ionic->vfs[vf].maxrate = cpu_to_le32(tx_max);
  2057. }
  2058. up_write(&ionic->vf_op_lock);
  2059. return ret;
  2060. }
  2061. static int ionic_set_vf_spoofchk(struct net_device *netdev, int vf, bool set)
  2062. {
  2063. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_SPOOFCHK };
  2064. struct ionic_lif *lif = netdev_priv(netdev);
  2065. struct ionic *ionic = lif->ionic;
  2066. int ret;
  2067. if (!netif_device_present(netdev))
  2068. return -EBUSY;
  2069. down_write(&ionic->vf_op_lock);
  2070. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2071. ret = -EINVAL;
  2072. } else {
  2073. vfc.spoofchk = set;
  2074. dev_dbg(ionic->dev, "%s: vf %d spoof %d\n",
  2075. __func__, vf, vfc.spoofchk);
  2076. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2077. if (!ret)
  2078. ionic->vfs[vf].spoofchk = set;
  2079. }
  2080. up_write(&ionic->vf_op_lock);
  2081. return ret;
  2082. }
  2083. static int ionic_set_vf_trust(struct net_device *netdev, int vf, bool set)
  2084. {
  2085. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_TRUST };
  2086. struct ionic_lif *lif = netdev_priv(netdev);
  2087. struct ionic *ionic = lif->ionic;
  2088. int ret;
  2089. if (!netif_device_present(netdev))
  2090. return -EBUSY;
  2091. down_write(&ionic->vf_op_lock);
  2092. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2093. ret = -EINVAL;
  2094. } else {
  2095. vfc.trust = set;
  2096. dev_dbg(ionic->dev, "%s: vf %d trust %d\n",
  2097. __func__, vf, vfc.trust);
  2098. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2099. if (!ret)
  2100. ionic->vfs[vf].trusted = set;
  2101. }
  2102. up_write(&ionic->vf_op_lock);
  2103. return ret;
  2104. }
  2105. static int ionic_set_vf_link_state(struct net_device *netdev, int vf, int set)
  2106. {
  2107. struct ionic_vf_setattr_cmd vfc = { .attr = IONIC_VF_ATTR_LINKSTATE };
  2108. struct ionic_lif *lif = netdev_priv(netdev);
  2109. struct ionic *ionic = lif->ionic;
  2110. u8 vfls;
  2111. int ret;
  2112. switch (set) {
  2113. case IFLA_VF_LINK_STATE_ENABLE:
  2114. vfls = IONIC_VF_LINK_STATUS_UP;
  2115. break;
  2116. case IFLA_VF_LINK_STATE_DISABLE:
  2117. vfls = IONIC_VF_LINK_STATUS_DOWN;
  2118. break;
  2119. case IFLA_VF_LINK_STATE_AUTO:
  2120. vfls = IONIC_VF_LINK_STATUS_AUTO;
  2121. break;
  2122. default:
  2123. return -EINVAL;
  2124. }
  2125. if (!netif_device_present(netdev))
  2126. return -EBUSY;
  2127. down_write(&ionic->vf_op_lock);
  2128. if (vf >= pci_num_vf(ionic->pdev) || !ionic->vfs) {
  2129. ret = -EINVAL;
  2130. } else {
  2131. vfc.linkstate = vfls;
  2132. dev_dbg(ionic->dev, "%s: vf %d linkstate %d\n",
  2133. __func__, vf, vfc.linkstate);
  2134. ret = ionic_set_vf_config(ionic, vf, &vfc);
  2135. if (!ret)
  2136. ionic->vfs[vf].linkstate = set;
  2137. }
  2138. up_write(&ionic->vf_op_lock);
  2139. return ret;
  2140. }
  2141. static void ionic_vf_attr_replay(struct ionic_lif *lif)
  2142. {
  2143. struct ionic_vf_setattr_cmd vfc = { };
  2144. struct ionic *ionic = lif->ionic;
  2145. struct ionic_vf *v;
  2146. int i;
  2147. if (!ionic->vfs)
  2148. return;
  2149. down_read(&ionic->vf_op_lock);
  2150. for (i = 0; i < ionic->num_vfs; i++) {
  2151. v = &ionic->vfs[i];
  2152. if (v->stats_pa) {
  2153. vfc.attr = IONIC_VF_ATTR_STATSADDR;
  2154. vfc.stats_pa = cpu_to_le64(v->stats_pa);
  2155. ionic_set_vf_config(ionic, i, &vfc);
  2156. vfc.stats_pa = 0;
  2157. }
  2158. if (!is_zero_ether_addr(v->macaddr)) {
  2159. vfc.attr = IONIC_VF_ATTR_MAC;
  2160. ether_addr_copy(vfc.macaddr, v->macaddr);
  2161. ionic_set_vf_config(ionic, i, &vfc);
  2162. eth_zero_addr(vfc.macaddr);
  2163. }
  2164. if (v->vlanid) {
  2165. vfc.attr = IONIC_VF_ATTR_VLAN;
  2166. vfc.vlanid = v->vlanid;
  2167. ionic_set_vf_config(ionic, i, &vfc);
  2168. vfc.vlanid = 0;
  2169. }
  2170. if (v->maxrate) {
  2171. vfc.attr = IONIC_VF_ATTR_RATE;
  2172. vfc.maxrate = v->maxrate;
  2173. ionic_set_vf_config(ionic, i, &vfc);
  2174. vfc.maxrate = 0;
  2175. }
  2176. if (v->spoofchk) {
  2177. vfc.attr = IONIC_VF_ATTR_SPOOFCHK;
  2178. vfc.spoofchk = v->spoofchk;
  2179. ionic_set_vf_config(ionic, i, &vfc);
  2180. vfc.spoofchk = 0;
  2181. }
  2182. if (v->trusted) {
  2183. vfc.attr = IONIC_VF_ATTR_TRUST;
  2184. vfc.trust = v->trusted;
  2185. ionic_set_vf_config(ionic, i, &vfc);
  2186. vfc.trust = 0;
  2187. }
  2188. if (v->linkstate) {
  2189. vfc.attr = IONIC_VF_ATTR_LINKSTATE;
  2190. vfc.linkstate = v->linkstate;
  2191. ionic_set_vf_config(ionic, i, &vfc);
  2192. vfc.linkstate = 0;
  2193. }
  2194. }
  2195. up_read(&ionic->vf_op_lock);
  2196. ionic_vf_start(ionic);
  2197. }
  2198. static void ionic_unregister_rxq_info(struct ionic_queue *q)
  2199. {
  2200. struct xdp_rxq_info *xi;
  2201. if (!q->xdp_rxq_info)
  2202. return;
  2203. xi = q->xdp_rxq_info;
  2204. q->xdp_rxq_info = NULL;
  2205. xdp_rxq_info_unreg(xi);
  2206. kfree(xi);
  2207. }
  2208. static int ionic_register_rxq_info(struct ionic_queue *q, unsigned int napi_id)
  2209. {
  2210. struct xdp_rxq_info *rxq_info;
  2211. int err;
  2212. rxq_info = kzalloc_obj(*rxq_info);
  2213. if (!rxq_info)
  2214. return -ENOMEM;
  2215. err = xdp_rxq_info_reg(rxq_info, q->lif->netdev, q->index, napi_id);
  2216. if (err) {
  2217. netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg failed, err %d\n",
  2218. q->index, err);
  2219. goto err_out;
  2220. }
  2221. err = xdp_rxq_info_reg_mem_model(rxq_info, MEM_TYPE_PAGE_POOL, q->page_pool);
  2222. if (err) {
  2223. netdev_err(q->lif->netdev, "q%d xdp_rxq_info_reg_mem_model failed, err %d\n",
  2224. q->index, err);
  2225. xdp_rxq_info_unreg(rxq_info);
  2226. goto err_out;
  2227. }
  2228. q->xdp_rxq_info = rxq_info;
  2229. return 0;
  2230. err_out:
  2231. kfree(rxq_info);
  2232. return err;
  2233. }
  2234. static void ionic_xdp_rxqs_prog_update(struct ionic_lif *lif)
  2235. {
  2236. struct bpf_prog *xdp_prog;
  2237. unsigned int i;
  2238. if (!lif->rxqcqs)
  2239. return;
  2240. xdp_prog = READ_ONCE(lif->xdp_prog);
  2241. for (i = 0; i < lif->ionic->nrxqs_per_lif && lif->rxqcqs[i]; i++) {
  2242. struct ionic_queue *q = &lif->rxqcqs[i]->q;
  2243. WRITE_ONCE(q->xdp_prog, xdp_prog);
  2244. }
  2245. }
  2246. static int ionic_xdp_config(struct net_device *netdev, struct netdev_bpf *bpf)
  2247. {
  2248. struct ionic_lif *lif = netdev_priv(netdev);
  2249. struct bpf_prog *old_prog;
  2250. u32 maxfs;
  2251. if (test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state)) {
  2252. #define XDP_ERR_SPLIT "XDP not available with split Tx/Rx interrupts"
  2253. NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_SPLIT);
  2254. netdev_info(lif->netdev, XDP_ERR_SPLIT);
  2255. return -EOPNOTSUPP;
  2256. }
  2257. if (!ionic_xdp_is_valid_mtu(lif, netdev->mtu, bpf->prog)) {
  2258. #define XDP_ERR_MTU "MTU is too large for XDP without frags support"
  2259. NL_SET_ERR_MSG_MOD(bpf->extack, XDP_ERR_MTU);
  2260. netdev_info(lif->netdev, XDP_ERR_MTU);
  2261. return -EINVAL;
  2262. }
  2263. maxfs = __le32_to_cpu(lif->identity->eth.max_frame_size) - VLAN_ETH_HLEN;
  2264. if (bpf->prog && !(bpf->prog->aux && bpf->prog->aux->xdp_has_frags))
  2265. maxfs = min_t(u32, maxfs, IONIC_XDP_MAX_LINEAR_MTU);
  2266. netdev->max_mtu = maxfs;
  2267. if (!netif_running(netdev)) {
  2268. old_prog = xchg(&lif->xdp_prog, bpf->prog);
  2269. } else if (lif->xdp_prog && bpf->prog) {
  2270. old_prog = xchg(&lif->xdp_prog, bpf->prog);
  2271. ionic_xdp_rxqs_prog_update(lif);
  2272. } else {
  2273. struct ionic_queue_params qparams;
  2274. ionic_init_queue_params(lif, &qparams);
  2275. qparams.xdp_prog = bpf->prog;
  2276. mutex_lock(&lif->queue_lock);
  2277. ionic_reconfigure_queues(lif, &qparams);
  2278. old_prog = xchg(&lif->xdp_prog, bpf->prog);
  2279. mutex_unlock(&lif->queue_lock);
  2280. }
  2281. if (old_prog)
  2282. bpf_prog_put(old_prog);
  2283. return 0;
  2284. }
  2285. static int ionic_xdp(struct net_device *netdev, struct netdev_bpf *bpf)
  2286. {
  2287. switch (bpf->command) {
  2288. case XDP_SETUP_PROG:
  2289. return ionic_xdp_config(netdev, bpf);
  2290. default:
  2291. return -EINVAL;
  2292. }
  2293. }
  2294. static const struct net_device_ops ionic_netdev_ops = {
  2295. .ndo_open = ionic_open,
  2296. .ndo_stop = ionic_stop,
  2297. .ndo_start_xmit = ionic_start_xmit,
  2298. .ndo_bpf = ionic_xdp,
  2299. .ndo_xdp_xmit = ionic_xdp_xmit,
  2300. .ndo_get_stats64 = ionic_get_stats64,
  2301. .ndo_set_rx_mode = ionic_ndo_set_rx_mode,
  2302. .ndo_set_features = ionic_set_features,
  2303. .ndo_set_mac_address = ionic_set_mac_address,
  2304. .ndo_validate_addr = eth_validate_addr,
  2305. .ndo_tx_timeout = ionic_tx_timeout,
  2306. .ndo_change_mtu = ionic_change_mtu,
  2307. .ndo_vlan_rx_add_vid = ionic_vlan_rx_add_vid,
  2308. .ndo_vlan_rx_kill_vid = ionic_vlan_rx_kill_vid,
  2309. .ndo_set_vf_vlan = ionic_set_vf_vlan,
  2310. .ndo_set_vf_trust = ionic_set_vf_trust,
  2311. .ndo_set_vf_mac = ionic_set_vf_mac,
  2312. .ndo_set_vf_rate = ionic_set_vf_rate,
  2313. .ndo_set_vf_spoofchk = ionic_set_vf_spoofchk,
  2314. .ndo_get_vf_config = ionic_get_vf_config,
  2315. .ndo_set_vf_link_state = ionic_set_vf_link_state,
  2316. .ndo_get_vf_stats = ionic_get_vf_stats,
  2317. .ndo_hwtstamp_get = ionic_hwstamp_get,
  2318. .ndo_hwtstamp_set = ionic_hwstamp_set,
  2319. };
  2320. static int ionic_cmb_reconfig(struct ionic_lif *lif,
  2321. struct ionic_queue_params *qparam)
  2322. {
  2323. struct ionic_queue_params start_qparams;
  2324. int err = 0;
  2325. /* When changing CMB queue parameters, we're using limited
  2326. * on-device memory and don't have extra memory to use for
  2327. * duplicate allocations, so we free it all first then
  2328. * re-allocate with the new parameters.
  2329. */
  2330. /* Checkpoint for possible unwind */
  2331. ionic_init_queue_params(lif, &start_qparams);
  2332. /* Stop and free the queues */
  2333. ionic_stop_queues_reconfig(lif);
  2334. ionic_txrx_free(lif);
  2335. /* Set up new qparams */
  2336. ionic_set_queue_params(lif, qparam);
  2337. if (netif_running(lif->netdev)) {
  2338. /* Alloc and start the new configuration */
  2339. err = ionic_txrx_alloc(lif);
  2340. if (err) {
  2341. dev_warn(lif->ionic->dev,
  2342. "CMB reconfig failed, restoring values: %d\n", err);
  2343. /* Back out the changes */
  2344. ionic_set_queue_params(lif, &start_qparams);
  2345. err = ionic_txrx_alloc(lif);
  2346. if (err) {
  2347. dev_err(lif->ionic->dev,
  2348. "CMB restore failed: %d\n", err);
  2349. goto err_out;
  2350. }
  2351. }
  2352. err = ionic_start_queues_reconfig(lif);
  2353. if (err) {
  2354. dev_err(lif->ionic->dev,
  2355. "CMB reconfig failed: %d\n", err);
  2356. goto err_out;
  2357. }
  2358. }
  2359. err_out:
  2360. /* This was detached in ionic_stop_queues_reconfig() */
  2361. netif_device_attach(lif->netdev);
  2362. return err;
  2363. }
  2364. static void ionic_swap_queues(struct ionic_qcq *a, struct ionic_qcq *b)
  2365. {
  2366. /* only swapping the queues and napi, not flags or other stuff */
  2367. swap(a->napi, b->napi);
  2368. if (a->q.type == IONIC_QTYPE_RXQ) {
  2369. swap(a->q.page_pool, b->q.page_pool);
  2370. a->q.page_pool->p.napi = &a->napi;
  2371. if (b->q.page_pool) /* is NULL when increasing queue count */
  2372. b->q.page_pool->p.napi = &b->napi;
  2373. }
  2374. swap(a->q.features, b->q.features);
  2375. swap(a->q.num_descs, b->q.num_descs);
  2376. swap(a->q.desc_size, b->q.desc_size);
  2377. swap(a->q.base, b->q.base);
  2378. swap(a->q.base_pa, b->q.base_pa);
  2379. swap(a->q.info, b->q.info);
  2380. swap(a->q.xdp_prog, b->q.xdp_prog);
  2381. swap(a->q.xdp_rxq_info, b->q.xdp_rxq_info);
  2382. swap(a->q.partner, b->q.partner);
  2383. swap(a->q_base, b->q_base);
  2384. swap(a->q_base_pa, b->q_base_pa);
  2385. swap(a->q_size, b->q_size);
  2386. swap(a->q.sg_desc_size, b->q.sg_desc_size);
  2387. swap(a->q.sg_base, b->q.sg_base);
  2388. swap(a->q.sg_base_pa, b->q.sg_base_pa);
  2389. swap(a->sg_base, b->sg_base);
  2390. swap(a->sg_base_pa, b->sg_base_pa);
  2391. swap(a->sg_size, b->sg_size);
  2392. swap(a->cq.num_descs, b->cq.num_descs);
  2393. swap(a->cq.desc_size, b->cq.desc_size);
  2394. swap(a->cq.base, b->cq.base);
  2395. swap(a->cq.base_pa, b->cq.base_pa);
  2396. swap(a->cq_base, b->cq_base);
  2397. swap(a->cq_base_pa, b->cq_base_pa);
  2398. swap(a->cq_size, b->cq_size);
  2399. ionic_debugfs_del_qcq(a);
  2400. ionic_debugfs_add_qcq(a->q.lif, a);
  2401. }
  2402. int ionic_reconfigure_queues(struct ionic_lif *lif,
  2403. struct ionic_queue_params *qparam)
  2404. {
  2405. unsigned int comp_sz, desc_sz, num_desc, sg_desc_sz;
  2406. struct ionic_qcq **tx_qcqs = NULL;
  2407. struct ionic_qcq **rx_qcqs = NULL;
  2408. unsigned int flags, i;
  2409. int err = 0;
  2410. /* Are we changing q params while CMB is on */
  2411. if ((test_bit(IONIC_LIF_F_CMB_TX_RINGS, lif->state) && qparam->cmb_tx) ||
  2412. (test_bit(IONIC_LIF_F_CMB_RX_RINGS, lif->state) && qparam->cmb_rx))
  2413. return ionic_cmb_reconfig(lif, qparam);
  2414. /* allocate temporary qcq arrays to hold new queue structs */
  2415. if (qparam->nxqs != lif->nxqs || qparam->ntxq_descs != lif->ntxq_descs) {
  2416. tx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->ntxqs_per_lif,
  2417. sizeof(struct ionic_qcq *), GFP_KERNEL);
  2418. if (!tx_qcqs) {
  2419. err = -ENOMEM;
  2420. goto err_out;
  2421. }
  2422. }
  2423. if (qparam->nxqs != lif->nxqs ||
  2424. qparam->nrxq_descs != lif->nrxq_descs ||
  2425. qparam->rxq_features != lif->rxq_features ||
  2426. qparam->xdp_prog != lif->xdp_prog) {
  2427. rx_qcqs = devm_kcalloc(lif->ionic->dev, lif->ionic->nrxqs_per_lif,
  2428. sizeof(struct ionic_qcq *), GFP_KERNEL);
  2429. if (!rx_qcqs) {
  2430. err = -ENOMEM;
  2431. goto err_out;
  2432. }
  2433. }
  2434. /* allocate new desc_info and rings, but leave the interrupt setup
  2435. * until later so as to not mess with the still-running queues
  2436. */
  2437. if (tx_qcqs) {
  2438. num_desc = qparam->ntxq_descs;
  2439. desc_sz = sizeof(struct ionic_txq_desc);
  2440. comp_sz = sizeof(struct ionic_txq_comp);
  2441. if (lif->qtype_info[IONIC_QTYPE_TXQ].version >= 1 &&
  2442. lif->qtype_info[IONIC_QTYPE_TXQ].sg_desc_sz ==
  2443. sizeof(struct ionic_txq_sg_desc_v1))
  2444. sg_desc_sz = sizeof(struct ionic_txq_sg_desc_v1);
  2445. else
  2446. sg_desc_sz = sizeof(struct ionic_txq_sg_desc);
  2447. for (i = 0; i < qparam->nxqs; i++) {
  2448. /* If missing, short placeholder qcq needed for swap */
  2449. if (!lif->txqcqs[i]) {
  2450. flags = IONIC_QCQ_F_TX_STATS | IONIC_QCQ_F_SG;
  2451. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
  2452. 4, desc_sz, comp_sz, sg_desc_sz,
  2453. sizeof(struct ionic_tx_desc_info),
  2454. lif->kern_pid, NULL, &lif->txqcqs[i]);
  2455. if (err)
  2456. goto err_out;
  2457. }
  2458. flags = lif->txqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
  2459. err = ionic_qcq_alloc(lif, IONIC_QTYPE_TXQ, i, "tx", flags,
  2460. num_desc, desc_sz, comp_sz, sg_desc_sz,
  2461. sizeof(struct ionic_tx_desc_info),
  2462. lif->kern_pid, NULL, &tx_qcqs[i]);
  2463. if (err)
  2464. goto err_out;
  2465. }
  2466. }
  2467. if (rx_qcqs) {
  2468. num_desc = qparam->nrxq_descs;
  2469. desc_sz = sizeof(struct ionic_rxq_desc);
  2470. comp_sz = sizeof(struct ionic_rxq_comp);
  2471. sg_desc_sz = sizeof(struct ionic_rxq_sg_desc);
  2472. if (qparam->rxq_features & IONIC_Q_F_2X_CQ_DESC)
  2473. comp_sz *= 2;
  2474. for (i = 0; i < qparam->nxqs; i++) {
  2475. /* If missing, short placeholder qcq needed for swap */
  2476. if (!lif->rxqcqs[i]) {
  2477. flags = IONIC_QCQ_F_RX_STATS | IONIC_QCQ_F_SG;
  2478. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
  2479. 4, desc_sz, comp_sz, sg_desc_sz,
  2480. sizeof(struct ionic_rx_desc_info),
  2481. lif->kern_pid, NULL, &lif->rxqcqs[i]);
  2482. if (err)
  2483. goto err_out;
  2484. }
  2485. flags = lif->rxqcqs[i]->flags & ~IONIC_QCQ_F_INTR;
  2486. err = ionic_qcq_alloc(lif, IONIC_QTYPE_RXQ, i, "rx", flags,
  2487. num_desc, desc_sz, comp_sz, sg_desc_sz,
  2488. sizeof(struct ionic_rx_desc_info),
  2489. lif->kern_pid, qparam->xdp_prog, &rx_qcqs[i]);
  2490. if (err)
  2491. goto err_out;
  2492. rx_qcqs[i]->q.features = qparam->rxq_features;
  2493. rx_qcqs[i]->q.xdp_prog = qparam->xdp_prog;
  2494. }
  2495. }
  2496. /* stop and clean the queues */
  2497. ionic_stop_queues_reconfig(lif);
  2498. if (qparam->nxqs != lif->nxqs) {
  2499. err = netif_set_real_num_tx_queues(lif->netdev, qparam->nxqs);
  2500. if (err)
  2501. goto err_out_reinit_unlock;
  2502. err = netif_set_real_num_rx_queues(lif->netdev, qparam->nxqs);
  2503. if (err) {
  2504. netif_set_real_num_tx_queues(lif->netdev, lif->nxqs);
  2505. goto err_out_reinit_unlock;
  2506. }
  2507. }
  2508. /* swap new desc_info and rings, keeping existing interrupt config */
  2509. if (tx_qcqs) {
  2510. lif->ntxq_descs = qparam->ntxq_descs;
  2511. for (i = 0; i < qparam->nxqs; i++)
  2512. ionic_swap_queues(lif->txqcqs[i], tx_qcqs[i]);
  2513. }
  2514. if (rx_qcqs) {
  2515. lif->nrxq_descs = qparam->nrxq_descs;
  2516. for (i = 0; i < qparam->nxqs; i++)
  2517. ionic_swap_queues(lif->rxqcqs[i], rx_qcqs[i]);
  2518. }
  2519. /* if we need to change the interrupt layout, this is the time */
  2520. if (qparam->intr_split != test_bit(IONIC_LIF_F_SPLIT_INTR, lif->state) ||
  2521. qparam->nxqs != lif->nxqs) {
  2522. if (qparam->intr_split) {
  2523. set_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
  2524. } else {
  2525. clear_bit(IONIC_LIF_F_SPLIT_INTR, lif->state);
  2526. lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
  2527. lif->tx_coalesce_hw = lif->rx_coalesce_hw;
  2528. }
  2529. /* Clear existing interrupt assignments. We check for NULL here
  2530. * because we're checking the whole array for potential qcqs, not
  2531. * just those qcqs that have just been set up.
  2532. */
  2533. for (i = 0; i < lif->ionic->ntxqs_per_lif; i++) {
  2534. if (lif->txqcqs[i])
  2535. ionic_qcq_intr_free(lif, lif->txqcqs[i]);
  2536. if (lif->rxqcqs[i])
  2537. ionic_qcq_intr_free(lif, lif->rxqcqs[i]);
  2538. }
  2539. /* re-assign the interrupts */
  2540. for (i = 0; i < qparam->nxqs; i++) {
  2541. lif->rxqcqs[i]->flags |= IONIC_QCQ_F_INTR;
  2542. err = ionic_alloc_qcq_interrupt(lif, lif->rxqcqs[i]);
  2543. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  2544. lif->rxqcqs[i]->intr.index,
  2545. lif->rx_coalesce_hw);
  2546. if (qparam->intr_split) {
  2547. lif->txqcqs[i]->flags |= IONIC_QCQ_F_INTR;
  2548. err = ionic_alloc_qcq_interrupt(lif, lif->txqcqs[i]);
  2549. ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
  2550. lif->txqcqs[i]->intr.index,
  2551. lif->tx_coalesce_hw);
  2552. if (test_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state))
  2553. lif->txqcqs[i]->intr.dim_coal_hw = lif->tx_coalesce_hw;
  2554. } else {
  2555. lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2556. ionic_link_qcq_interrupts(lif->rxqcqs[i], lif->txqcqs[i]);
  2557. }
  2558. }
  2559. }
  2560. /* now we can rework the debugfs mappings */
  2561. if (tx_qcqs) {
  2562. for (i = 0; i < qparam->nxqs; i++) {
  2563. ionic_debugfs_del_qcq(lif->txqcqs[i]);
  2564. ionic_debugfs_add_qcq(lif, lif->txqcqs[i]);
  2565. }
  2566. }
  2567. if (rx_qcqs) {
  2568. for (i = 0; i < qparam->nxqs; i++) {
  2569. ionic_debugfs_del_qcq(lif->rxqcqs[i]);
  2570. ionic_debugfs_add_qcq(lif, lif->rxqcqs[i]);
  2571. }
  2572. }
  2573. swap(lif->nxqs, qparam->nxqs);
  2574. swap(lif->rxq_features, qparam->rxq_features);
  2575. err_out_reinit_unlock:
  2576. /* re-init the queues, but don't lose an error code */
  2577. if (err)
  2578. ionic_start_queues_reconfig(lif);
  2579. else
  2580. err = ionic_start_queues_reconfig(lif);
  2581. err_out:
  2582. /* free old allocs without cleaning intr */
  2583. for (i = 0; i < qparam->nxqs; i++) {
  2584. if (tx_qcqs && tx_qcqs[i]) {
  2585. tx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2586. ionic_qcq_free(lif, tx_qcqs[i]);
  2587. devm_kfree(lif->ionic->dev, tx_qcqs[i]);
  2588. tx_qcqs[i] = NULL;
  2589. }
  2590. if (rx_qcqs && rx_qcqs[i]) {
  2591. rx_qcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2592. ionic_qcq_free(lif, rx_qcqs[i]);
  2593. devm_kfree(lif->ionic->dev, rx_qcqs[i]);
  2594. rx_qcqs[i] = NULL;
  2595. }
  2596. }
  2597. /* free q array */
  2598. if (rx_qcqs) {
  2599. devm_kfree(lif->ionic->dev, rx_qcqs);
  2600. rx_qcqs = NULL;
  2601. }
  2602. if (tx_qcqs) {
  2603. devm_kfree(lif->ionic->dev, tx_qcqs);
  2604. tx_qcqs = NULL;
  2605. }
  2606. /* clean the unused dma and info allocations when new set is smaller
  2607. * than the full array, but leave the qcq shells in place
  2608. */
  2609. for (i = lif->nxqs; i < lif->ionic->ntxqs_per_lif; i++) {
  2610. if (lif->txqcqs && lif->txqcqs[i]) {
  2611. lif->txqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2612. ionic_qcq_free(lif, lif->txqcqs[i]);
  2613. }
  2614. if (lif->rxqcqs && lif->rxqcqs[i]) {
  2615. lif->rxqcqs[i]->flags &= ~IONIC_QCQ_F_INTR;
  2616. ionic_qcq_free(lif, lif->rxqcqs[i]);
  2617. }
  2618. }
  2619. if (err)
  2620. netdev_info(lif->netdev, "%s: failed %d\n", __func__, err);
  2621. return err;
  2622. }
  2623. static int ionic_affinity_masks_alloc(struct ionic *ionic)
  2624. {
  2625. cpumask_var_t *affinity_masks;
  2626. int nintrs = ionic->nintrs;
  2627. int i;
  2628. affinity_masks = kzalloc_objs(cpumask_var_t, nintrs);
  2629. if (!affinity_masks)
  2630. return -ENOMEM;
  2631. for (i = 0; i < nintrs; i++) {
  2632. if (!zalloc_cpumask_var_node(&affinity_masks[i], GFP_KERNEL,
  2633. dev_to_node(ionic->dev)))
  2634. goto err_out;
  2635. }
  2636. ionic->affinity_masks = affinity_masks;
  2637. return 0;
  2638. err_out:
  2639. for (--i; i >= 0; i--)
  2640. free_cpumask_var(affinity_masks[i]);
  2641. kfree(affinity_masks);
  2642. return -ENOMEM;
  2643. }
  2644. static void ionic_affinity_masks_free(struct ionic *ionic)
  2645. {
  2646. int i;
  2647. for (i = 0; i < ionic->nintrs; i++)
  2648. free_cpumask_var(ionic->affinity_masks[i]);
  2649. kfree(ionic->affinity_masks);
  2650. ionic->affinity_masks = NULL;
  2651. }
  2652. int ionic_lif_alloc(struct ionic *ionic)
  2653. {
  2654. struct device *dev = ionic->dev;
  2655. union ionic_lif_identity *lid;
  2656. struct net_device *netdev;
  2657. struct ionic_lif *lif;
  2658. int tbl_sz;
  2659. int err;
  2660. lid = kzalloc_obj(*lid);
  2661. if (!lid)
  2662. return -ENOMEM;
  2663. netdev = alloc_etherdev_mqs(sizeof(*lif),
  2664. ionic->ntxqs_per_lif, ionic->ntxqs_per_lif);
  2665. if (!netdev) {
  2666. dev_err(dev, "Cannot allocate netdev, aborting\n");
  2667. err = -ENOMEM;
  2668. goto err_out_free_lid;
  2669. }
  2670. SET_NETDEV_DEV(netdev, dev);
  2671. lif = netdev_priv(netdev);
  2672. lif->netdev = netdev;
  2673. ionic->lif = lif;
  2674. lif->ionic = ionic;
  2675. netdev->netdev_ops = &ionic_netdev_ops;
  2676. ionic_ethtool_set_ops(netdev);
  2677. netdev->watchdog_timeo = 5 * HZ;
  2678. netif_carrier_off(netdev);
  2679. lif->identity = lid;
  2680. lif->lif_type = IONIC_LIF_TYPE_CLASSIC;
  2681. err = ionic_lif_identify(ionic, lif->lif_type, lif->identity);
  2682. if (err) {
  2683. dev_err(ionic->dev, "Cannot identify type %d: %d\n",
  2684. lif->lif_type, err);
  2685. goto err_out_free_netdev;
  2686. }
  2687. lif->netdev->min_mtu = max_t(unsigned int, ETH_MIN_MTU,
  2688. le32_to_cpu(lif->identity->eth.min_frame_size));
  2689. lif->netdev->max_mtu =
  2690. le32_to_cpu(lif->identity->eth.max_frame_size) - VLAN_ETH_HLEN;
  2691. lif->neqs = ionic->neqs_per_lif;
  2692. lif->nxqs = ionic->ntxqs_per_lif;
  2693. lif->index = 0;
  2694. if (is_kdump_kernel()) {
  2695. lif->ntxq_descs = IONIC_MIN_TXRX_DESC;
  2696. lif->nrxq_descs = IONIC_MIN_TXRX_DESC;
  2697. } else {
  2698. lif->ntxq_descs = IONIC_DEF_TXRX_DESC;
  2699. lif->nrxq_descs = IONIC_DEF_TXRX_DESC;
  2700. }
  2701. /* Convert the default coalesce value to actual hw resolution */
  2702. lif->rx_coalesce_usecs = IONIC_ITR_COAL_USEC_DEFAULT;
  2703. lif->rx_coalesce_hw = ionic_coal_usec_to_hw(lif->ionic,
  2704. lif->rx_coalesce_usecs);
  2705. lif->tx_coalesce_usecs = lif->rx_coalesce_usecs;
  2706. lif->tx_coalesce_hw = lif->rx_coalesce_hw;
  2707. set_bit(IONIC_LIF_F_RX_DIM_INTR, lif->state);
  2708. set_bit(IONIC_LIF_F_TX_DIM_INTR, lif->state);
  2709. snprintf(lif->name, sizeof(lif->name), "lif%u", lif->index);
  2710. mutex_init(&lif->queue_lock);
  2711. mutex_init(&lif->config_lock);
  2712. mutex_init(&lif->adev_lock);
  2713. spin_lock_init(&lif->adminq_lock);
  2714. spin_lock_init(&lif->deferred.lock);
  2715. INIT_LIST_HEAD(&lif->deferred.list);
  2716. INIT_WORK(&lif->deferred.work, ionic_lif_deferred_work);
  2717. /* allocate lif info */
  2718. lif->info_sz = ALIGN(sizeof(*lif->info), PAGE_SIZE);
  2719. lif->info = dma_alloc_coherent(dev, lif->info_sz,
  2720. &lif->info_pa, GFP_KERNEL);
  2721. if (!lif->info) {
  2722. dev_err(dev, "Failed to allocate lif info, aborting\n");
  2723. err = -ENOMEM;
  2724. goto err_out_free_mutex;
  2725. }
  2726. ionic_debugfs_add_lif(lif);
  2727. err = ionic_affinity_masks_alloc(ionic);
  2728. if (err)
  2729. goto err_out_free_lif_info;
  2730. /* allocate control queues and txrx queue arrays */
  2731. ionic_lif_queue_identify(lif);
  2732. err = ionic_qcqs_alloc(lif);
  2733. if (err)
  2734. goto err_out_free_affinity_masks;
  2735. /* allocate rss indirection table */
  2736. tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
  2737. lif->rss_ind_tbl_sz = sizeof(*lif->rss_ind_tbl) * tbl_sz;
  2738. lif->rss_ind_tbl = dma_alloc_coherent(dev, lif->rss_ind_tbl_sz,
  2739. &lif->rss_ind_tbl_pa,
  2740. GFP_KERNEL);
  2741. if (!lif->rss_ind_tbl) {
  2742. err = -ENOMEM;
  2743. dev_err(dev, "Failed to allocate rss indirection table, aborting\n");
  2744. goto err_out_free_qcqs;
  2745. }
  2746. netdev_rss_key_fill(lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
  2747. ionic_lif_alloc_phc(lif);
  2748. return 0;
  2749. err_out_free_qcqs:
  2750. ionic_qcqs_free(lif);
  2751. err_out_free_affinity_masks:
  2752. ionic_affinity_masks_free(lif->ionic);
  2753. err_out_free_lif_info:
  2754. dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
  2755. lif->info = NULL;
  2756. lif->info_pa = 0;
  2757. err_out_free_mutex:
  2758. mutex_destroy(&lif->adev_lock);
  2759. mutex_destroy(&lif->config_lock);
  2760. mutex_destroy(&lif->queue_lock);
  2761. err_out_free_netdev:
  2762. free_netdev(lif->netdev);
  2763. lif = NULL;
  2764. err_out_free_lid:
  2765. kfree(lid);
  2766. return err;
  2767. }
  2768. static void ionic_lif_reset(struct ionic_lif *lif)
  2769. {
  2770. struct ionic_dev *idev = &lif->ionic->idev;
  2771. if (!ionic_is_fw_running(idev))
  2772. return;
  2773. mutex_lock(&lif->ionic->dev_cmd_lock);
  2774. ionic_dev_cmd_lif_reset(idev, lif->index);
  2775. ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
  2776. mutex_unlock(&lif->ionic->dev_cmd_lock);
  2777. }
  2778. static void ionic_lif_handle_fw_down(struct ionic_lif *lif)
  2779. {
  2780. struct ionic *ionic = lif->ionic;
  2781. if (test_and_set_bit(IONIC_LIF_F_FW_RESET, lif->state))
  2782. return;
  2783. dev_info(ionic->dev, "FW Down: Stopping LIFs\n");
  2784. netif_device_detach(lif->netdev);
  2785. ionic_auxbus_unregister(ionic->lif);
  2786. mutex_lock(&lif->queue_lock);
  2787. if (test_bit(IONIC_LIF_F_UP, lif->state)) {
  2788. dev_info(ionic->dev, "Surprise FW stop, stopping queues\n");
  2789. ionic_stop_queues(lif);
  2790. }
  2791. if (netif_running(lif->netdev)) {
  2792. ionic_txrx_deinit(lif);
  2793. ionic_txrx_free(lif);
  2794. }
  2795. ionic_lif_deinit(lif);
  2796. ionic_reset(ionic);
  2797. ionic_qcqs_free(lif);
  2798. mutex_unlock(&lif->queue_lock);
  2799. clear_bit(IONIC_LIF_F_FW_STOPPING, lif->state);
  2800. dev_info(ionic->dev, "FW Down: LIFs stopped\n");
  2801. }
  2802. int ionic_restart_lif(struct ionic_lif *lif)
  2803. {
  2804. struct ionic *ionic = lif->ionic;
  2805. int err;
  2806. mutex_lock(&lif->queue_lock);
  2807. if (test_and_clear_bit(IONIC_LIF_F_BROKEN, lif->state))
  2808. dev_info(ionic->dev, "FW Up: clearing broken state\n");
  2809. err = ionic_qcqs_alloc(lif);
  2810. if (err)
  2811. goto err_unlock;
  2812. err = ionic_lif_init(lif);
  2813. if (err)
  2814. goto err_qcqs_free;
  2815. ionic_vf_attr_replay(lif);
  2816. if (lif->registered)
  2817. ionic_lif_set_netdev_info(lif);
  2818. ionic_rx_filter_replay(lif);
  2819. if (netif_running(lif->netdev)) {
  2820. err = ionic_txrx_alloc(lif);
  2821. if (err)
  2822. goto err_lifs_deinit;
  2823. err = ionic_txrx_init(lif);
  2824. if (err)
  2825. goto err_txrx_free;
  2826. }
  2827. mutex_unlock(&lif->queue_lock);
  2828. clear_bit(IONIC_LIF_F_FW_RESET, lif->state);
  2829. ionic_link_status_check_request(lif, CAN_SLEEP);
  2830. netif_device_attach(lif->netdev);
  2831. ionic_queue_doorbell_check(ionic, IONIC_NAPI_DEADLINE);
  2832. ionic_auxbus_register(ionic->lif);
  2833. return 0;
  2834. err_txrx_free:
  2835. ionic_txrx_free(lif);
  2836. err_lifs_deinit:
  2837. ionic_lif_deinit(lif);
  2838. err_qcqs_free:
  2839. ionic_qcqs_free(lif);
  2840. err_unlock:
  2841. mutex_unlock(&lif->queue_lock);
  2842. return err;
  2843. }
  2844. static void ionic_lif_handle_fw_up(struct ionic_lif *lif)
  2845. {
  2846. struct ionic *ionic = lif->ionic;
  2847. int err;
  2848. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  2849. return;
  2850. dev_info(ionic->dev, "FW Up: restarting LIFs\n");
  2851. /* This is a little different from what happens at
  2852. * probe time because the LIF already exists so we
  2853. * just need to reanimate it.
  2854. */
  2855. ionic_init_devinfo(ionic);
  2856. ionic_reset(ionic);
  2857. err = ionic_identify(ionic);
  2858. if (err)
  2859. goto err_out;
  2860. err = ionic_port_identify(ionic);
  2861. if (err)
  2862. goto err_out;
  2863. err = ionic_port_init(ionic);
  2864. if (err)
  2865. goto err_out;
  2866. err = ionic_restart_lif(lif);
  2867. if (err)
  2868. goto err_out;
  2869. dev_info(ionic->dev, "FW Up: LIFs restarted\n");
  2870. /* restore the hardware timestamping queues */
  2871. ionic_lif_hwstamp_replay(lif);
  2872. return;
  2873. err_out:
  2874. dev_err(ionic->dev, "FW Up: LIFs restart failed - err %d\n", err);
  2875. }
  2876. void ionic_lif_free(struct ionic_lif *lif)
  2877. {
  2878. struct device *dev = lif->ionic->dev;
  2879. ionic_lif_free_phc(lif);
  2880. /* free rss indirection table */
  2881. dma_free_coherent(dev, lif->rss_ind_tbl_sz, lif->rss_ind_tbl,
  2882. lif->rss_ind_tbl_pa);
  2883. lif->rss_ind_tbl = NULL;
  2884. lif->rss_ind_tbl_pa = 0;
  2885. /* free queues */
  2886. ionic_qcqs_free(lif);
  2887. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  2888. ionic_lif_reset(lif);
  2889. ionic_affinity_masks_free(lif->ionic);
  2890. /* free lif info */
  2891. kfree(lif->identity);
  2892. dma_free_coherent(dev, lif->info_sz, lif->info, lif->info_pa);
  2893. lif->info = NULL;
  2894. lif->info_pa = 0;
  2895. mutex_destroy(&lif->config_lock);
  2896. mutex_destroy(&lif->queue_lock);
  2897. mutex_destroy(&lif->adev_lock);
  2898. /* free netdev & lif */
  2899. ionic_debugfs_del_lif(lif);
  2900. free_netdev(lif->netdev);
  2901. }
  2902. void ionic_lif_deinit(struct ionic_lif *lif)
  2903. {
  2904. if (!test_and_clear_bit(IONIC_LIF_F_INITED, lif->state))
  2905. return;
  2906. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
  2907. cancel_work_sync(&lif->deferred.work);
  2908. cancel_work_sync(&lif->tx_timeout_work);
  2909. ionic_rx_filters_deinit(lif);
  2910. if (lif->netdev->features & NETIF_F_RXHASH)
  2911. ionic_lif_rss_deinit(lif);
  2912. }
  2913. napi_disable(&lif->adminqcq->napi);
  2914. ionic_lif_qcq_deinit(lif, lif->notifyqcq);
  2915. ionic_lif_qcq_deinit(lif, lif->adminqcq);
  2916. ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
  2917. lif->kern_dbpage = NULL;
  2918. ionic_lif_reset(lif);
  2919. }
  2920. static int ionic_lif_adminq_init(struct ionic_lif *lif)
  2921. {
  2922. struct device *dev = lif->ionic->dev;
  2923. struct ionic_q_init_comp comp;
  2924. struct ionic_dev *idev;
  2925. struct ionic_qcq *qcq;
  2926. struct ionic_queue *q;
  2927. int err;
  2928. idev = &lif->ionic->idev;
  2929. qcq = lif->adminqcq;
  2930. q = &qcq->q;
  2931. mutex_lock(&lif->ionic->dev_cmd_lock);
  2932. ionic_dev_cmd_adminq_init(idev, qcq, lif->index, qcq->intr.index);
  2933. err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
  2934. ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
  2935. mutex_unlock(&lif->ionic->dev_cmd_lock);
  2936. if (err) {
  2937. netdev_err(lif->netdev, "adminq init failed %d\n", err);
  2938. return err;
  2939. }
  2940. q->hw_type = comp.hw_type;
  2941. q->hw_index = le32_to_cpu(comp.hw_index);
  2942. q->dbval = IONIC_DBELL_QID(q->hw_index);
  2943. dev_dbg(dev, "adminq->hw_type %d\n", q->hw_type);
  2944. dev_dbg(dev, "adminq->hw_index %d\n", q->hw_index);
  2945. q->dbell_deadline = IONIC_ADMIN_DOORBELL_DEADLINE;
  2946. q->dbell_jiffies = jiffies;
  2947. netif_napi_add(lif->netdev, &qcq->napi, ionic_adminq_napi);
  2948. napi_enable(&qcq->napi);
  2949. if (qcq->flags & IONIC_QCQ_F_INTR) {
  2950. irq_set_affinity_hint(qcq->intr.vector,
  2951. *qcq->intr.affinity_mask);
  2952. ionic_intr_mask(idev->intr_ctrl, qcq->intr.index,
  2953. IONIC_INTR_MASK_CLEAR);
  2954. }
  2955. qcq->flags |= IONIC_QCQ_F_INITED;
  2956. return 0;
  2957. }
  2958. static int ionic_lif_notifyq_init(struct ionic_lif *lif)
  2959. {
  2960. struct ionic_qcq *qcq = lif->notifyqcq;
  2961. struct device *dev = lif->ionic->dev;
  2962. struct ionic_queue *q = &qcq->q;
  2963. int err;
  2964. struct ionic_admin_ctx ctx = {
  2965. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  2966. .cmd.q_init = {
  2967. .opcode = IONIC_CMD_Q_INIT,
  2968. .lif_index = cpu_to_le16(lif->index),
  2969. .type = q->type,
  2970. .ver = lif->qtype_info[q->type].version,
  2971. .index = cpu_to_le32(q->index),
  2972. .flags = cpu_to_le16(IONIC_QINIT_F_IRQ |
  2973. IONIC_QINIT_F_ENA),
  2974. .intr_index = cpu_to_le16(lif->adminqcq->intr.index),
  2975. .pid = cpu_to_le16(q->pid),
  2976. .ring_size = ilog2(q->num_descs),
  2977. .ring_base = cpu_to_le64(q->base_pa),
  2978. }
  2979. };
  2980. dev_dbg(dev, "notifyq_init.pid %d\n", ctx.cmd.q_init.pid);
  2981. dev_dbg(dev, "notifyq_init.index %d\n", ctx.cmd.q_init.index);
  2982. dev_dbg(dev, "notifyq_init.ring_base 0x%llx\n", ctx.cmd.q_init.ring_base);
  2983. dev_dbg(dev, "notifyq_init.ring_size %d\n", ctx.cmd.q_init.ring_size);
  2984. err = ionic_adminq_post_wait(lif, &ctx);
  2985. if (err)
  2986. return err;
  2987. lif->last_eid = 0;
  2988. q->hw_type = ctx.comp.q_init.hw_type;
  2989. q->hw_index = le32_to_cpu(ctx.comp.q_init.hw_index);
  2990. q->dbval = IONIC_DBELL_QID(q->hw_index);
  2991. dev_dbg(dev, "notifyq->hw_type %d\n", q->hw_type);
  2992. dev_dbg(dev, "notifyq->hw_index %d\n", q->hw_index);
  2993. /* preset the callback info */
  2994. q->admin_info[0].ctx = lif;
  2995. qcq->flags |= IONIC_QCQ_F_INITED;
  2996. return 0;
  2997. }
  2998. static int ionic_station_set(struct ionic_lif *lif)
  2999. {
  3000. struct net_device *netdev = lif->netdev;
  3001. struct ionic_admin_ctx ctx = {
  3002. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  3003. .cmd.lif_getattr = {
  3004. .opcode = IONIC_CMD_LIF_GETATTR,
  3005. .index = cpu_to_le16(lif->index),
  3006. .attr = IONIC_LIF_ATTR_MAC,
  3007. },
  3008. };
  3009. u8 mac_address[ETH_ALEN];
  3010. struct sockaddr addr;
  3011. int err;
  3012. err = ionic_adminq_post_wait(lif, &ctx);
  3013. if (err)
  3014. return err;
  3015. netdev_dbg(lif->netdev, "found initial MAC addr %pM\n",
  3016. ctx.comp.lif_getattr.mac);
  3017. ether_addr_copy(mac_address, ctx.comp.lif_getattr.mac);
  3018. if (is_zero_ether_addr(mac_address)) {
  3019. eth_hw_addr_random(netdev);
  3020. netdev_dbg(netdev, "Random Mac generated: %pM\n", netdev->dev_addr);
  3021. ether_addr_copy(mac_address, netdev->dev_addr);
  3022. err = ionic_program_mac(lif, mac_address);
  3023. if (err < 0)
  3024. return err;
  3025. if (err > 0) {
  3026. netdev_dbg(netdev, "%s:SET/GET ATTR Mac are not same-due to old FW running\n",
  3027. __func__);
  3028. return 0;
  3029. }
  3030. }
  3031. if (!is_zero_ether_addr(netdev->dev_addr)) {
  3032. /* If the netdev mac is non-zero and doesn't match the default
  3033. * device address, it was set by something earlier and we're
  3034. * likely here again after a fw-upgrade reset. We need to be
  3035. * sure the netdev mac is in our filter list.
  3036. */
  3037. if (!ether_addr_equal(mac_address, netdev->dev_addr))
  3038. ionic_lif_addr_add(lif, netdev->dev_addr);
  3039. } else {
  3040. /* Update the netdev mac with the device's mac */
  3041. ether_addr_copy(addr.sa_data, mac_address);
  3042. addr.sa_family = AF_INET;
  3043. err = eth_prepare_mac_addr_change(netdev, &addr);
  3044. if (err) {
  3045. netdev_warn(lif->netdev, "ignoring bad MAC addr from NIC %pM - err %d\n",
  3046. addr.sa_data, err);
  3047. return 0;
  3048. }
  3049. eth_commit_mac_addr_change(netdev, &addr);
  3050. }
  3051. netdev_dbg(lif->netdev, "adding station MAC addr %pM\n",
  3052. netdev->dev_addr);
  3053. ionic_lif_addr_add(lif, netdev->dev_addr);
  3054. return 0;
  3055. }
  3056. int ionic_lif_init(struct ionic_lif *lif)
  3057. {
  3058. struct ionic_dev *idev = &lif->ionic->idev;
  3059. struct device *dev = lif->ionic->dev;
  3060. struct ionic_lif_init_comp comp;
  3061. int dbpage_num;
  3062. int err;
  3063. mutex_lock(&lif->ionic->dev_cmd_lock);
  3064. ionic_dev_cmd_lif_init(idev, lif->index, lif->info_pa);
  3065. err = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
  3066. ionic_dev_cmd_comp(idev, (union ionic_dev_cmd_comp *)&comp);
  3067. mutex_unlock(&lif->ionic->dev_cmd_lock);
  3068. if (err)
  3069. return err;
  3070. lif->hw_index = le16_to_cpu(comp.hw_index);
  3071. /* now that we have the hw_index we can figure out our doorbell page */
  3072. lif->dbid_count = le32_to_cpu(lif->ionic->ident.dev.ndbpgs_per_lif);
  3073. if (!lif->dbid_count) {
  3074. dev_err(dev, "No doorbell pages, aborting\n");
  3075. return -EINVAL;
  3076. }
  3077. lif->kern_pid = 0;
  3078. dbpage_num = ionic_db_page_num(lif, lif->kern_pid);
  3079. lif->kern_dbpage = ionic_bus_map_dbpage(lif->ionic, dbpage_num);
  3080. if (!lif->kern_dbpage) {
  3081. dev_err(dev, "Cannot map dbpage, aborting\n");
  3082. return -ENOMEM;
  3083. }
  3084. err = ionic_lif_adminq_init(lif);
  3085. if (err)
  3086. goto err_out_adminq_deinit;
  3087. if (lif->ionic->nnqs_per_lif) {
  3088. err = ionic_lif_notifyq_init(lif);
  3089. if (err)
  3090. goto err_out_notifyq_deinit;
  3091. }
  3092. if (test_bit(IONIC_LIF_F_FW_RESET, lif->state))
  3093. err = ionic_set_nic_features(lif, lif->netdev->features);
  3094. else
  3095. err = ionic_init_nic_features(lif);
  3096. if (err)
  3097. goto err_out_notifyq_deinit;
  3098. if (!test_bit(IONIC_LIF_F_FW_RESET, lif->state)) {
  3099. err = ionic_rx_filters_init(lif);
  3100. if (err)
  3101. goto err_out_notifyq_deinit;
  3102. }
  3103. err = ionic_station_set(lif);
  3104. if (err)
  3105. goto err_out_notifyq_deinit;
  3106. lif->rx_copybreak = IONIC_RX_COPYBREAK_DEFAULT;
  3107. lif->doorbell_wa = ionic_doorbell_wa(lif->ionic);
  3108. set_bit(IONIC_LIF_F_INITED, lif->state);
  3109. INIT_WORK(&lif->tx_timeout_work, ionic_tx_timeout_work);
  3110. return 0;
  3111. err_out_notifyq_deinit:
  3112. napi_disable(&lif->adminqcq->napi);
  3113. ionic_lif_qcq_deinit(lif, lif->notifyqcq);
  3114. err_out_adminq_deinit:
  3115. ionic_lif_qcq_deinit(lif, lif->adminqcq);
  3116. ionic_lif_reset(lif);
  3117. ionic_bus_unmap_dbpage(lif->ionic, lif->kern_dbpage);
  3118. lif->kern_dbpage = NULL;
  3119. return err;
  3120. }
  3121. static void ionic_lif_set_netdev_info(struct ionic_lif *lif)
  3122. {
  3123. struct ionic_admin_ctx ctx = {
  3124. .work = COMPLETION_INITIALIZER_ONSTACK(ctx.work),
  3125. .cmd.lif_setattr = {
  3126. .opcode = IONIC_CMD_LIF_SETATTR,
  3127. .index = cpu_to_le16(lif->index),
  3128. .attr = IONIC_LIF_ATTR_NAME,
  3129. },
  3130. };
  3131. strscpy(ctx.cmd.lif_setattr.name, netdev_name(lif->netdev),
  3132. sizeof(ctx.cmd.lif_setattr.name));
  3133. ionic_adminq_post_wait(lif, &ctx);
  3134. }
  3135. static struct ionic_lif *ionic_netdev_lif(struct net_device *netdev)
  3136. {
  3137. if (!netdev || netdev->netdev_ops->ndo_start_xmit != ionic_start_xmit)
  3138. return NULL;
  3139. return netdev_priv(netdev);
  3140. }
  3141. static int ionic_lif_notify(struct notifier_block *nb,
  3142. unsigned long event, void *info)
  3143. {
  3144. struct net_device *ndev = netdev_notifier_info_to_dev(info);
  3145. struct ionic *ionic = container_of(nb, struct ionic, nb);
  3146. struct ionic_lif *lif = ionic_netdev_lif(ndev);
  3147. if (!lif || lif->ionic != ionic)
  3148. return NOTIFY_DONE;
  3149. switch (event) {
  3150. case NETDEV_CHANGENAME:
  3151. ionic_lif_set_netdev_info(lif);
  3152. break;
  3153. }
  3154. return NOTIFY_DONE;
  3155. }
  3156. int ionic_lif_register(struct ionic_lif *lif)
  3157. {
  3158. int err;
  3159. ionic_lif_register_phc(lif);
  3160. lif->ionic->nb.notifier_call = ionic_lif_notify;
  3161. err = register_netdevice_notifier(&lif->ionic->nb);
  3162. if (err)
  3163. lif->ionic->nb.notifier_call = NULL;
  3164. /* only register LIF0 for now */
  3165. err = register_netdev(lif->netdev);
  3166. if (err) {
  3167. dev_err(lif->ionic->dev, "Cannot register net device: %d, aborting\n", err);
  3168. ionic_lif_unregister(lif);
  3169. return err;
  3170. }
  3171. ionic_link_status_check_request(lif, CAN_SLEEP);
  3172. lif->registered = true;
  3173. ionic_lif_set_netdev_info(lif);
  3174. return 0;
  3175. }
  3176. void ionic_lif_unregister(struct ionic_lif *lif)
  3177. {
  3178. if (lif->ionic->nb.notifier_call) {
  3179. unregister_netdevice_notifier(&lif->ionic->nb);
  3180. lif->ionic->nb.notifier_call = NULL;
  3181. }
  3182. if (lif->netdev->reg_state == NETREG_REGISTERED)
  3183. unregister_netdev(lif->netdev);
  3184. ionic_lif_unregister_phc(lif);
  3185. lif->registered = false;
  3186. }
  3187. static void ionic_lif_queue_identify(struct ionic_lif *lif)
  3188. {
  3189. union ionic_q_identity __iomem *q_ident;
  3190. struct ionic *ionic = lif->ionic;
  3191. struct ionic_dev *idev;
  3192. u16 max_frags;
  3193. int qtype;
  3194. int err;
  3195. idev = &lif->ionic->idev;
  3196. q_ident = (union ionic_q_identity __iomem *)&idev->dev_cmd_regs->data;
  3197. for (qtype = 0; qtype < ARRAY_SIZE(ionic_qtype_versions); qtype++) {
  3198. struct ionic_qtype_info *qti = &lif->qtype_info[qtype];
  3199. /* filter out the ones we know about */
  3200. switch (qtype) {
  3201. case IONIC_QTYPE_ADMINQ:
  3202. case IONIC_QTYPE_NOTIFYQ:
  3203. case IONIC_QTYPE_RXQ:
  3204. case IONIC_QTYPE_TXQ:
  3205. break;
  3206. default:
  3207. continue;
  3208. }
  3209. memset(qti, 0, sizeof(*qti));
  3210. mutex_lock(&ionic->dev_cmd_lock);
  3211. ionic_dev_cmd_queue_identify(idev, lif->lif_type, qtype,
  3212. ionic_qtype_versions[qtype]);
  3213. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  3214. if (!err) {
  3215. qti->version = readb(&q_ident->version);
  3216. qti->supported = readb(&q_ident->supported);
  3217. qti->features = readq(&q_ident->features);
  3218. qti->desc_sz = readw(&q_ident->desc_sz);
  3219. qti->comp_sz = readw(&q_ident->comp_sz);
  3220. qti->sg_desc_sz = readw(&q_ident->sg_desc_sz);
  3221. qti->max_sg_elems = readw(&q_ident->max_sg_elems);
  3222. qti->sg_desc_stride = readw(&q_ident->sg_desc_stride);
  3223. }
  3224. mutex_unlock(&ionic->dev_cmd_lock);
  3225. if (err == -EINVAL) {
  3226. dev_err(ionic->dev, "qtype %d not supported\n", qtype);
  3227. continue;
  3228. } else if (err == -EIO) {
  3229. dev_err(ionic->dev, "q_ident failed, not supported on older FW\n");
  3230. return;
  3231. } else if (err) {
  3232. dev_err(ionic->dev, "q_ident failed, qtype %d: %d\n",
  3233. qtype, err);
  3234. return;
  3235. }
  3236. dev_dbg(ionic->dev, " qtype[%d].version = %d\n",
  3237. qtype, qti->version);
  3238. dev_dbg(ionic->dev, " qtype[%d].supported = 0x%02x\n",
  3239. qtype, qti->supported);
  3240. dev_dbg(ionic->dev, " qtype[%d].features = 0x%04llx\n",
  3241. qtype, qti->features);
  3242. dev_dbg(ionic->dev, " qtype[%d].desc_sz = %d\n",
  3243. qtype, qti->desc_sz);
  3244. dev_dbg(ionic->dev, " qtype[%d].comp_sz = %d\n",
  3245. qtype, qti->comp_sz);
  3246. dev_dbg(ionic->dev, " qtype[%d].sg_desc_sz = %d\n",
  3247. qtype, qti->sg_desc_sz);
  3248. dev_dbg(ionic->dev, " qtype[%d].max_sg_elems = %d\n",
  3249. qtype, qti->max_sg_elems);
  3250. dev_dbg(ionic->dev, " qtype[%d].sg_desc_stride = %d\n",
  3251. qtype, qti->sg_desc_stride);
  3252. if (qtype == IONIC_QTYPE_TXQ)
  3253. max_frags = IONIC_TX_MAX_FRAGS;
  3254. else if (qtype == IONIC_QTYPE_RXQ)
  3255. max_frags = IONIC_RX_MAX_FRAGS;
  3256. else
  3257. max_frags = 1;
  3258. qti->max_sg_elems = min_t(u16, max_frags - 1, MAX_SKB_FRAGS);
  3259. dev_dbg(ionic->dev, "qtype %d max_sg_elems %d\n",
  3260. qtype, qti->max_sg_elems);
  3261. }
  3262. }
  3263. int ionic_lif_identify(struct ionic *ionic, u8 lif_type,
  3264. union ionic_lif_identity *lid)
  3265. {
  3266. struct ionic_dev *idev = &ionic->idev;
  3267. size_t sz;
  3268. int err;
  3269. sz = min(sizeof(*lid), sizeof(idev->dev_cmd_regs->data));
  3270. mutex_lock(&ionic->dev_cmd_lock);
  3271. ionic_dev_cmd_lif_identify(idev, lif_type, IONIC_IDENTITY_VERSION_1);
  3272. err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
  3273. memcpy_fromio(lid, &idev->dev_cmd_regs->data, sz);
  3274. mutex_unlock(&ionic->dev_cmd_lock);
  3275. if (err)
  3276. return (err);
  3277. dev_dbg(ionic->dev, "capabilities 0x%llx\n",
  3278. le64_to_cpu(lid->capabilities));
  3279. dev_dbg(ionic->dev, "eth.max_ucast_filters %d\n",
  3280. le32_to_cpu(lid->eth.max_ucast_filters));
  3281. dev_dbg(ionic->dev, "eth.max_mcast_filters %d\n",
  3282. le32_to_cpu(lid->eth.max_mcast_filters));
  3283. dev_dbg(ionic->dev, "eth.features 0x%llx\n",
  3284. le64_to_cpu(lid->eth.config.features));
  3285. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_ADMINQ] %d\n",
  3286. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_ADMINQ]));
  3287. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_NOTIFYQ] %d\n",
  3288. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_NOTIFYQ]));
  3289. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_RXQ] %d\n",
  3290. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_RXQ]));
  3291. dev_dbg(ionic->dev, "eth.queue_count[IONIC_QTYPE_TXQ] %d\n",
  3292. le32_to_cpu(lid->eth.config.queue_count[IONIC_QTYPE_TXQ]));
  3293. dev_dbg(ionic->dev, "eth.config.name %s\n", lid->eth.config.name);
  3294. dev_dbg(ionic->dev, "eth.config.mac %pM\n", lid->eth.config.mac);
  3295. dev_dbg(ionic->dev, "eth.config.mtu %d\n",
  3296. le32_to_cpu(lid->eth.config.mtu));
  3297. return 0;
  3298. }
  3299. int ionic_lif_size(struct ionic *ionic)
  3300. {
  3301. struct ionic_identity *ident = &ionic->ident;
  3302. unsigned int nintrs, dev_nintrs;
  3303. union ionic_lif_config *lc;
  3304. unsigned int ntxqs_per_lif;
  3305. unsigned int nrxqs_per_lif;
  3306. unsigned int neqs_per_lif;
  3307. unsigned int nnqs_per_lif;
  3308. unsigned int nxqs, neqs;
  3309. unsigned int min_intrs;
  3310. int err;
  3311. /* retrieve basic values from FW */
  3312. lc = &ident->lif.eth.config;
  3313. dev_nintrs = le32_to_cpu(ident->dev.nintrs);
  3314. neqs_per_lif = le32_to_cpu(ident->lif.rdma.eq_qtype.qid_count);
  3315. nnqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_NOTIFYQ]);
  3316. ntxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_TXQ]);
  3317. nrxqs_per_lif = le32_to_cpu(lc->queue_count[IONIC_QTYPE_RXQ]);
  3318. /* limit values to play nice with kdump */
  3319. if (is_kdump_kernel()) {
  3320. dev_nintrs = 2;
  3321. neqs_per_lif = 0;
  3322. nnqs_per_lif = 0;
  3323. ntxqs_per_lif = 1;
  3324. nrxqs_per_lif = 1;
  3325. }
  3326. /* reserve last queue id for hardware timestamping */
  3327. if (lc->features & cpu_to_le64(IONIC_ETH_HW_TIMESTAMP)) {
  3328. if (ntxqs_per_lif <= 1 || nrxqs_per_lif <= 1) {
  3329. lc->features &= cpu_to_le64(~IONIC_ETH_HW_TIMESTAMP);
  3330. } else {
  3331. ntxqs_per_lif -= 1;
  3332. nrxqs_per_lif -= 1;
  3333. }
  3334. }
  3335. nxqs = min(ntxqs_per_lif, nrxqs_per_lif);
  3336. nxqs = min(nxqs, num_online_cpus());
  3337. neqs = min(neqs_per_lif, num_online_cpus());
  3338. try_again:
  3339. /* interrupt usage:
  3340. * 1 for master lif adminq/notifyq
  3341. * 1 for each CPU for master lif TxRx queue pairs
  3342. * whatever's left is for RDMA queues
  3343. */
  3344. nintrs = 1 + nxqs + neqs;
  3345. min_intrs = 2; /* adminq + 1 TxRx queue pair */
  3346. if (nintrs > dev_nintrs)
  3347. goto try_fewer;
  3348. err = ionic_bus_alloc_irq_vectors(ionic, nintrs);
  3349. if (err < 0 && err != -ENOSPC) {
  3350. dev_err(ionic->dev, "Can't get intrs from OS: %d\n", err);
  3351. return err;
  3352. }
  3353. if (err == -ENOSPC)
  3354. goto try_fewer;
  3355. if (err != nintrs) {
  3356. ionic_bus_free_irq_vectors(ionic);
  3357. goto try_fewer;
  3358. }
  3359. ionic->nnqs_per_lif = nnqs_per_lif;
  3360. ionic->neqs_per_lif = neqs;
  3361. ionic->ntxqs_per_lif = nxqs;
  3362. ionic->nrxqs_per_lif = nxqs;
  3363. ionic->nintrs = nintrs;
  3364. ionic_debugfs_add_sizes(ionic);
  3365. return 0;
  3366. try_fewer:
  3367. if (nnqs_per_lif > 1) {
  3368. nnqs_per_lif >>= 1;
  3369. goto try_again;
  3370. }
  3371. if (neqs > 1) {
  3372. neqs >>= 1;
  3373. goto try_again;
  3374. }
  3375. if (nxqs > 1) {
  3376. nxqs >>= 1;
  3377. goto try_again;
  3378. }
  3379. dev_err(ionic->dev, "Can't get minimum %d intrs from OS\n", min_intrs);
  3380. return -ENOSPC;
  3381. }