ena_netdev.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597
  1. /*
  2. * Copyright 2015 Amazon.com, Inc. or its affiliates.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #ifdef CONFIG_RFS_ACCEL
  34. #include <linux/cpu_rmap.h>
  35. #endif /* CONFIG_RFS_ACCEL */
  36. #include <linux/ethtool.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/numa.h>
  42. #include <linux/pci.h>
  43. #include <linux/utsname.h>
  44. #include <linux/version.h>
  45. #include <linux/vmalloc.h>
  46. #include <net/ip.h>
  47. #include "ena_netdev.h"
  48. #include "ena_pci_id_tbl.h"
  49. static char version[] = DEVICE_NAME " v" DRV_MODULE_VERSION "\n";
  50. MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
  51. MODULE_DESCRIPTION(DEVICE_NAME);
  52. MODULE_LICENSE("GPL");
  53. MODULE_VERSION(DRV_MODULE_VERSION);
  54. /* Time in jiffies before concluding the transmitter is hung. */
  55. #define TX_TIMEOUT (5 * HZ)
  56. #define ENA_NAPI_BUDGET 64
  57. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | \
  58. NETIF_MSG_TX_DONE | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
  59. static int debug = -1;
  60. module_param(debug, int, 0);
  61. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  62. static struct ena_aenq_handlers aenq_handlers;
  63. static struct workqueue_struct *ena_wq;
  64. MODULE_DEVICE_TABLE(pci, ena_pci_tbl);
  65. static int ena_rss_init_default(struct ena_adapter *adapter);
  66. static void check_for_admin_com_state(struct ena_adapter *adapter);
  67. static void ena_destroy_device(struct ena_adapter *adapter, bool graceful);
  68. static int ena_restore_device(struct ena_adapter *adapter);
  69. static void ena_tx_timeout(struct net_device *dev)
  70. {
  71. struct ena_adapter *adapter = netdev_priv(dev);
  72. /* Change the state of the device to trigger reset
  73. * Check that we are not in the middle or a trigger already
  74. */
  75. if (test_and_set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
  76. return;
  77. adapter->reset_reason = ENA_REGS_RESET_OS_NETDEV_WD;
  78. u64_stats_update_begin(&adapter->syncp);
  79. adapter->dev_stats.tx_timeout++;
  80. u64_stats_update_end(&adapter->syncp);
  81. netif_err(adapter, tx_err, dev, "Transmit time out\n");
  82. }
  83. static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu)
  84. {
  85. int i;
  86. for (i = 0; i < adapter->num_queues; i++)
  87. adapter->rx_ring[i].mtu = mtu;
  88. }
  89. static int ena_change_mtu(struct net_device *dev, int new_mtu)
  90. {
  91. struct ena_adapter *adapter = netdev_priv(dev);
  92. int ret;
  93. ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
  94. if (!ret) {
  95. netif_dbg(adapter, drv, dev, "set MTU to %d\n", new_mtu);
  96. update_rx_ring_mtu(adapter, new_mtu);
  97. dev->mtu = new_mtu;
  98. } else {
  99. netif_err(adapter, drv, dev, "Failed to set MTU to %d\n",
  100. new_mtu);
  101. }
  102. return ret;
  103. }
  104. static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter)
  105. {
  106. #ifdef CONFIG_RFS_ACCEL
  107. u32 i;
  108. int rc;
  109. adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_queues);
  110. if (!adapter->netdev->rx_cpu_rmap)
  111. return -ENOMEM;
  112. for (i = 0; i < adapter->num_queues; i++) {
  113. int irq_idx = ENA_IO_IRQ_IDX(i);
  114. rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap,
  115. pci_irq_vector(adapter->pdev, irq_idx));
  116. if (rc) {
  117. free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
  118. adapter->netdev->rx_cpu_rmap = NULL;
  119. return rc;
  120. }
  121. }
  122. #endif /* CONFIG_RFS_ACCEL */
  123. return 0;
  124. }
  125. static void ena_init_io_rings_common(struct ena_adapter *adapter,
  126. struct ena_ring *ring, u16 qid)
  127. {
  128. ring->qid = qid;
  129. ring->pdev = adapter->pdev;
  130. ring->dev = &adapter->pdev->dev;
  131. ring->netdev = adapter->netdev;
  132. ring->napi = &adapter->ena_napi[qid].napi;
  133. ring->adapter = adapter;
  134. ring->ena_dev = adapter->ena_dev;
  135. ring->per_napi_packets = 0;
  136. ring->per_napi_bytes = 0;
  137. ring->cpu = 0;
  138. ring->first_interrupt = false;
  139. ring->no_interrupt_event_cnt = 0;
  140. u64_stats_init(&ring->syncp);
  141. }
  142. static void ena_init_io_rings(struct ena_adapter *adapter)
  143. {
  144. struct ena_com_dev *ena_dev;
  145. struct ena_ring *txr, *rxr;
  146. int i;
  147. ena_dev = adapter->ena_dev;
  148. for (i = 0; i < adapter->num_queues; i++) {
  149. txr = &adapter->tx_ring[i];
  150. rxr = &adapter->rx_ring[i];
  151. /* TX/RX common ring state */
  152. ena_init_io_rings_common(adapter, txr, i);
  153. ena_init_io_rings_common(adapter, rxr, i);
  154. /* TX specific ring state */
  155. txr->ring_size = adapter->tx_ring_size;
  156. txr->tx_max_header_size = ena_dev->tx_max_header_size;
  157. txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
  158. txr->sgl_size = adapter->max_tx_sgl_size;
  159. txr->smoothed_interval =
  160. ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
  161. /* RX specific ring state */
  162. rxr->ring_size = adapter->rx_ring_size;
  163. rxr->rx_copybreak = adapter->rx_copybreak;
  164. rxr->sgl_size = adapter->max_rx_sgl_size;
  165. rxr->smoothed_interval =
  166. ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
  167. rxr->empty_rx_queue = 0;
  168. }
  169. }
  170. /* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors)
  171. * @adapter: network interface device structure
  172. * @qid: queue index
  173. *
  174. * Return 0 on success, negative on failure
  175. */
  176. static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
  177. {
  178. struct ena_ring *tx_ring = &adapter->tx_ring[qid];
  179. struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
  180. int size, i, node;
  181. if (tx_ring->tx_buffer_info) {
  182. netif_err(adapter, ifup,
  183. adapter->netdev, "tx_buffer_info info is not NULL");
  184. return -EEXIST;
  185. }
  186. size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
  187. node = cpu_to_node(ena_irq->cpu);
  188. tx_ring->tx_buffer_info = vzalloc_node(size, node);
  189. if (!tx_ring->tx_buffer_info) {
  190. tx_ring->tx_buffer_info = vzalloc(size);
  191. if (!tx_ring->tx_buffer_info)
  192. return -ENOMEM;
  193. }
  194. size = sizeof(u16) * tx_ring->ring_size;
  195. tx_ring->free_tx_ids = vzalloc_node(size, node);
  196. if (!tx_ring->free_tx_ids) {
  197. tx_ring->free_tx_ids = vzalloc(size);
  198. if (!tx_ring->free_tx_ids) {
  199. vfree(tx_ring->tx_buffer_info);
  200. return -ENOMEM;
  201. }
  202. }
  203. /* Req id ring for TX out of order completions */
  204. for (i = 0; i < tx_ring->ring_size; i++)
  205. tx_ring->free_tx_ids[i] = i;
  206. /* Reset tx statistics */
  207. memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats));
  208. tx_ring->next_to_use = 0;
  209. tx_ring->next_to_clean = 0;
  210. tx_ring->cpu = ena_irq->cpu;
  211. return 0;
  212. }
  213. /* ena_free_tx_resources - Free I/O Tx Resources per Queue
  214. * @adapter: network interface device structure
  215. * @qid: queue index
  216. *
  217. * Free all transmit software resources
  218. */
  219. static void ena_free_tx_resources(struct ena_adapter *adapter, int qid)
  220. {
  221. struct ena_ring *tx_ring = &adapter->tx_ring[qid];
  222. vfree(tx_ring->tx_buffer_info);
  223. tx_ring->tx_buffer_info = NULL;
  224. vfree(tx_ring->free_tx_ids);
  225. tx_ring->free_tx_ids = NULL;
  226. }
  227. /* ena_setup_all_tx_resources - allocate I/O Tx queues resources for All queues
  228. * @adapter: private structure
  229. *
  230. * Return 0 on success, negative on failure
  231. */
  232. static int ena_setup_all_tx_resources(struct ena_adapter *adapter)
  233. {
  234. int i, rc = 0;
  235. for (i = 0; i < adapter->num_queues; i++) {
  236. rc = ena_setup_tx_resources(adapter, i);
  237. if (rc)
  238. goto err_setup_tx;
  239. }
  240. return 0;
  241. err_setup_tx:
  242. netif_err(adapter, ifup, adapter->netdev,
  243. "Tx queue %d: allocation failed\n", i);
  244. /* rewind the index freeing the rings as we go */
  245. while (i--)
  246. ena_free_tx_resources(adapter, i);
  247. return rc;
  248. }
  249. /* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues
  250. * @adapter: board private structure
  251. *
  252. * Free all transmit software resources
  253. */
  254. static void ena_free_all_io_tx_resources(struct ena_adapter *adapter)
  255. {
  256. int i;
  257. for (i = 0; i < adapter->num_queues; i++)
  258. ena_free_tx_resources(adapter, i);
  259. }
  260. static inline int validate_rx_req_id(struct ena_ring *rx_ring, u16 req_id)
  261. {
  262. if (likely(req_id < rx_ring->ring_size))
  263. return 0;
  264. netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
  265. "Invalid rx req_id: %hu\n", req_id);
  266. u64_stats_update_begin(&rx_ring->syncp);
  267. rx_ring->rx_stats.bad_req_id++;
  268. u64_stats_update_end(&rx_ring->syncp);
  269. /* Trigger device reset */
  270. rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
  271. set_bit(ENA_FLAG_TRIGGER_RESET, &rx_ring->adapter->flags);
  272. return -EFAULT;
  273. }
  274. /* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors)
  275. * @adapter: network interface device structure
  276. * @qid: queue index
  277. *
  278. * Returns 0 on success, negative on failure
  279. */
  280. static int ena_setup_rx_resources(struct ena_adapter *adapter,
  281. u32 qid)
  282. {
  283. struct ena_ring *rx_ring = &adapter->rx_ring[qid];
  284. struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
  285. int size, node, i;
  286. if (rx_ring->rx_buffer_info) {
  287. netif_err(adapter, ifup, adapter->netdev,
  288. "rx_buffer_info is not NULL");
  289. return -EEXIST;
  290. }
  291. /* alloc extra element so in rx path
  292. * we can always prefetch rx_info + 1
  293. */
  294. size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1);
  295. node = cpu_to_node(ena_irq->cpu);
  296. rx_ring->rx_buffer_info = vzalloc_node(size, node);
  297. if (!rx_ring->rx_buffer_info) {
  298. rx_ring->rx_buffer_info = vzalloc(size);
  299. if (!rx_ring->rx_buffer_info)
  300. return -ENOMEM;
  301. }
  302. size = sizeof(u16) * rx_ring->ring_size;
  303. rx_ring->free_rx_ids = vzalloc_node(size, node);
  304. if (!rx_ring->free_rx_ids) {
  305. rx_ring->free_rx_ids = vzalloc(size);
  306. if (!rx_ring->free_rx_ids) {
  307. vfree(rx_ring->rx_buffer_info);
  308. return -ENOMEM;
  309. }
  310. }
  311. /* Req id ring for receiving RX pkts out of order */
  312. for (i = 0; i < rx_ring->ring_size; i++)
  313. rx_ring->free_rx_ids[i] = i;
  314. /* Reset rx statistics */
  315. memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats));
  316. rx_ring->next_to_clean = 0;
  317. rx_ring->next_to_use = 0;
  318. rx_ring->cpu = ena_irq->cpu;
  319. return 0;
  320. }
  321. /* ena_free_rx_resources - Free I/O Rx Resources
  322. * @adapter: network interface device structure
  323. * @qid: queue index
  324. *
  325. * Free all receive software resources
  326. */
  327. static void ena_free_rx_resources(struct ena_adapter *adapter,
  328. u32 qid)
  329. {
  330. struct ena_ring *rx_ring = &adapter->rx_ring[qid];
  331. vfree(rx_ring->rx_buffer_info);
  332. rx_ring->rx_buffer_info = NULL;
  333. vfree(rx_ring->free_rx_ids);
  334. rx_ring->free_rx_ids = NULL;
  335. }
  336. /* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues
  337. * @adapter: board private structure
  338. *
  339. * Return 0 on success, negative on failure
  340. */
  341. static int ena_setup_all_rx_resources(struct ena_adapter *adapter)
  342. {
  343. int i, rc = 0;
  344. for (i = 0; i < adapter->num_queues; i++) {
  345. rc = ena_setup_rx_resources(adapter, i);
  346. if (rc)
  347. goto err_setup_rx;
  348. }
  349. return 0;
  350. err_setup_rx:
  351. netif_err(adapter, ifup, adapter->netdev,
  352. "Rx queue %d: allocation failed\n", i);
  353. /* rewind the index freeing the rings as we go */
  354. while (i--)
  355. ena_free_rx_resources(adapter, i);
  356. return rc;
  357. }
  358. /* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues
  359. * @adapter: board private structure
  360. *
  361. * Free all receive software resources
  362. */
  363. static void ena_free_all_io_rx_resources(struct ena_adapter *adapter)
  364. {
  365. int i;
  366. for (i = 0; i < adapter->num_queues; i++)
  367. ena_free_rx_resources(adapter, i);
  368. }
  369. static inline int ena_alloc_rx_page(struct ena_ring *rx_ring,
  370. struct ena_rx_buffer *rx_info, gfp_t gfp)
  371. {
  372. struct ena_com_buf *ena_buf;
  373. struct page *page;
  374. dma_addr_t dma;
  375. /* if previous allocated page is not used */
  376. if (unlikely(rx_info->page))
  377. return 0;
  378. page = alloc_page(gfp);
  379. if (unlikely(!page)) {
  380. u64_stats_update_begin(&rx_ring->syncp);
  381. rx_ring->rx_stats.page_alloc_fail++;
  382. u64_stats_update_end(&rx_ring->syncp);
  383. return -ENOMEM;
  384. }
  385. dma = dma_map_page(rx_ring->dev, page, 0, ENA_PAGE_SIZE,
  386. DMA_FROM_DEVICE);
  387. if (unlikely(dma_mapping_error(rx_ring->dev, dma))) {
  388. u64_stats_update_begin(&rx_ring->syncp);
  389. rx_ring->rx_stats.dma_mapping_err++;
  390. u64_stats_update_end(&rx_ring->syncp);
  391. __free_page(page);
  392. return -EIO;
  393. }
  394. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  395. "alloc page %p, rx_info %p\n", page, rx_info);
  396. rx_info->page = page;
  397. rx_info->page_offset = 0;
  398. ena_buf = &rx_info->ena_buf;
  399. ena_buf->paddr = dma;
  400. ena_buf->len = ENA_PAGE_SIZE;
  401. return 0;
  402. }
  403. static void ena_free_rx_page(struct ena_ring *rx_ring,
  404. struct ena_rx_buffer *rx_info)
  405. {
  406. struct page *page = rx_info->page;
  407. struct ena_com_buf *ena_buf = &rx_info->ena_buf;
  408. if (unlikely(!page)) {
  409. netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
  410. "Trying to free unallocated buffer\n");
  411. return;
  412. }
  413. dma_unmap_page(rx_ring->dev, ena_buf->paddr, ENA_PAGE_SIZE,
  414. DMA_FROM_DEVICE);
  415. __free_page(page);
  416. rx_info->page = NULL;
  417. }
  418. static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num)
  419. {
  420. u16 next_to_use, req_id;
  421. u32 i;
  422. int rc;
  423. next_to_use = rx_ring->next_to_use;
  424. for (i = 0; i < num; i++) {
  425. struct ena_rx_buffer *rx_info;
  426. req_id = rx_ring->free_rx_ids[next_to_use];
  427. rc = validate_rx_req_id(rx_ring, req_id);
  428. if (unlikely(rc < 0))
  429. break;
  430. rx_info = &rx_ring->rx_buffer_info[req_id];
  431. rc = ena_alloc_rx_page(rx_ring, rx_info,
  432. GFP_ATOMIC | __GFP_COMP);
  433. if (unlikely(rc < 0)) {
  434. netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
  435. "failed to alloc buffer for rx queue %d\n",
  436. rx_ring->qid);
  437. break;
  438. }
  439. rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
  440. &rx_info->ena_buf,
  441. req_id);
  442. if (unlikely(rc)) {
  443. netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
  444. "failed to add buffer for rx queue %d\n",
  445. rx_ring->qid);
  446. break;
  447. }
  448. next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
  449. rx_ring->ring_size);
  450. }
  451. if (unlikely(i < num)) {
  452. u64_stats_update_begin(&rx_ring->syncp);
  453. rx_ring->rx_stats.refil_partial++;
  454. u64_stats_update_end(&rx_ring->syncp);
  455. netdev_warn(rx_ring->netdev,
  456. "refilled rx qid %d with only %d buffers (from %d)\n",
  457. rx_ring->qid, i, num);
  458. }
  459. /* ena_com_write_sq_doorbell issues a wmb() */
  460. if (likely(i))
  461. ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
  462. rx_ring->next_to_use = next_to_use;
  463. return i;
  464. }
  465. static void ena_free_rx_bufs(struct ena_adapter *adapter,
  466. u32 qid)
  467. {
  468. struct ena_ring *rx_ring = &adapter->rx_ring[qid];
  469. u32 i;
  470. for (i = 0; i < rx_ring->ring_size; i++) {
  471. struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
  472. if (rx_info->page)
  473. ena_free_rx_page(rx_ring, rx_info);
  474. }
  475. }
  476. /* ena_refill_all_rx_bufs - allocate all queues Rx buffers
  477. * @adapter: board private structure
  478. *
  479. */
  480. static void ena_refill_all_rx_bufs(struct ena_adapter *adapter)
  481. {
  482. struct ena_ring *rx_ring;
  483. int i, rc, bufs_num;
  484. for (i = 0; i < adapter->num_queues; i++) {
  485. rx_ring = &adapter->rx_ring[i];
  486. bufs_num = rx_ring->ring_size - 1;
  487. rc = ena_refill_rx_bufs(rx_ring, bufs_num);
  488. if (unlikely(rc != bufs_num))
  489. netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
  490. "refilling Queue %d failed. allocated %d buffers from: %d\n",
  491. i, rc, bufs_num);
  492. }
  493. }
  494. static void ena_free_all_rx_bufs(struct ena_adapter *adapter)
  495. {
  496. int i;
  497. for (i = 0; i < adapter->num_queues; i++)
  498. ena_free_rx_bufs(adapter, i);
  499. }
  500. /* ena_free_tx_bufs - Free Tx Buffers per Queue
  501. * @tx_ring: TX ring for which buffers be freed
  502. */
  503. static void ena_free_tx_bufs(struct ena_ring *tx_ring)
  504. {
  505. bool print_once = true;
  506. u32 i;
  507. for (i = 0; i < tx_ring->ring_size; i++) {
  508. struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
  509. struct ena_com_buf *ena_buf;
  510. int nr_frags;
  511. int j;
  512. if (!tx_info->skb)
  513. continue;
  514. if (print_once) {
  515. netdev_notice(tx_ring->netdev,
  516. "free uncompleted tx skb qid %d idx 0x%x\n",
  517. tx_ring->qid, i);
  518. print_once = false;
  519. } else {
  520. netdev_dbg(tx_ring->netdev,
  521. "free uncompleted tx skb qid %d idx 0x%x\n",
  522. tx_ring->qid, i);
  523. }
  524. ena_buf = tx_info->bufs;
  525. dma_unmap_single(tx_ring->dev,
  526. ena_buf->paddr,
  527. ena_buf->len,
  528. DMA_TO_DEVICE);
  529. /* unmap remaining mapped pages */
  530. nr_frags = tx_info->num_of_bufs - 1;
  531. for (j = 0; j < nr_frags; j++) {
  532. ena_buf++;
  533. dma_unmap_page(tx_ring->dev,
  534. ena_buf->paddr,
  535. ena_buf->len,
  536. DMA_TO_DEVICE);
  537. }
  538. dev_kfree_skb_any(tx_info->skb);
  539. }
  540. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  541. tx_ring->qid));
  542. }
  543. static void ena_free_all_tx_bufs(struct ena_adapter *adapter)
  544. {
  545. struct ena_ring *tx_ring;
  546. int i;
  547. for (i = 0; i < adapter->num_queues; i++) {
  548. tx_ring = &adapter->tx_ring[i];
  549. ena_free_tx_bufs(tx_ring);
  550. }
  551. }
  552. static void ena_destroy_all_tx_queues(struct ena_adapter *adapter)
  553. {
  554. u16 ena_qid;
  555. int i;
  556. for (i = 0; i < adapter->num_queues; i++) {
  557. ena_qid = ENA_IO_TXQ_IDX(i);
  558. ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
  559. }
  560. }
  561. static void ena_destroy_all_rx_queues(struct ena_adapter *adapter)
  562. {
  563. u16 ena_qid;
  564. int i;
  565. for (i = 0; i < adapter->num_queues; i++) {
  566. ena_qid = ENA_IO_RXQ_IDX(i);
  567. ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
  568. }
  569. }
  570. static void ena_destroy_all_io_queues(struct ena_adapter *adapter)
  571. {
  572. ena_destroy_all_tx_queues(adapter);
  573. ena_destroy_all_rx_queues(adapter);
  574. }
  575. static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
  576. {
  577. struct ena_tx_buffer *tx_info = NULL;
  578. if (likely(req_id < tx_ring->ring_size)) {
  579. tx_info = &tx_ring->tx_buffer_info[req_id];
  580. if (likely(tx_info->skb))
  581. return 0;
  582. }
  583. if (tx_info)
  584. netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
  585. "tx_info doesn't have valid skb\n");
  586. else
  587. netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
  588. "Invalid req_id: %hu\n", req_id);
  589. u64_stats_update_begin(&tx_ring->syncp);
  590. tx_ring->tx_stats.bad_req_id++;
  591. u64_stats_update_end(&tx_ring->syncp);
  592. /* Trigger device reset */
  593. tx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID;
  594. set_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags);
  595. return -EFAULT;
  596. }
  597. static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget)
  598. {
  599. struct netdev_queue *txq;
  600. bool above_thresh;
  601. u32 tx_bytes = 0;
  602. u32 total_done = 0;
  603. u16 next_to_clean;
  604. u16 req_id;
  605. int tx_pkts = 0;
  606. int rc;
  607. next_to_clean = tx_ring->next_to_clean;
  608. txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid);
  609. while (tx_pkts < budget) {
  610. struct ena_tx_buffer *tx_info;
  611. struct sk_buff *skb;
  612. struct ena_com_buf *ena_buf;
  613. int i, nr_frags;
  614. rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
  615. &req_id);
  616. if (rc)
  617. break;
  618. rc = validate_tx_req_id(tx_ring, req_id);
  619. if (rc)
  620. break;
  621. tx_info = &tx_ring->tx_buffer_info[req_id];
  622. skb = tx_info->skb;
  623. /* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */
  624. prefetch(&skb->end);
  625. tx_info->skb = NULL;
  626. tx_info->last_jiffies = 0;
  627. if (likely(tx_info->num_of_bufs != 0)) {
  628. ena_buf = tx_info->bufs;
  629. dma_unmap_single(tx_ring->dev,
  630. dma_unmap_addr(ena_buf, paddr),
  631. dma_unmap_len(ena_buf, len),
  632. DMA_TO_DEVICE);
  633. /* unmap remaining mapped pages */
  634. nr_frags = tx_info->num_of_bufs - 1;
  635. for (i = 0; i < nr_frags; i++) {
  636. ena_buf++;
  637. dma_unmap_page(tx_ring->dev,
  638. dma_unmap_addr(ena_buf, paddr),
  639. dma_unmap_len(ena_buf, len),
  640. DMA_TO_DEVICE);
  641. }
  642. }
  643. netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
  644. "tx_poll: q %d skb %p completed\n", tx_ring->qid,
  645. skb);
  646. tx_bytes += skb->len;
  647. dev_kfree_skb(skb);
  648. tx_pkts++;
  649. total_done += tx_info->tx_descs;
  650. tx_ring->free_tx_ids[next_to_clean] = req_id;
  651. next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
  652. tx_ring->ring_size);
  653. }
  654. tx_ring->next_to_clean = next_to_clean;
  655. ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
  656. ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
  657. netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
  658. netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
  659. "tx_poll: q %d done. total pkts: %d\n",
  660. tx_ring->qid, tx_pkts);
  661. /* need to make the rings circular update visible to
  662. * ena_start_xmit() before checking for netif_queue_stopped().
  663. */
  664. smp_mb();
  665. above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
  666. ENA_TX_WAKEUP_THRESH;
  667. if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) {
  668. __netif_tx_lock(txq, smp_processor_id());
  669. above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
  670. ENA_TX_WAKEUP_THRESH;
  671. if (netif_tx_queue_stopped(txq) && above_thresh) {
  672. netif_tx_wake_queue(txq);
  673. u64_stats_update_begin(&tx_ring->syncp);
  674. tx_ring->tx_stats.queue_wakeup++;
  675. u64_stats_update_end(&tx_ring->syncp);
  676. }
  677. __netif_tx_unlock(txq);
  678. }
  679. tx_ring->per_napi_bytes += tx_bytes;
  680. tx_ring->per_napi_packets += tx_pkts;
  681. return tx_pkts;
  682. }
  683. static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, bool frags)
  684. {
  685. struct sk_buff *skb;
  686. if (frags)
  687. skb = napi_get_frags(rx_ring->napi);
  688. else
  689. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  690. rx_ring->rx_copybreak);
  691. if (unlikely(!skb)) {
  692. u64_stats_update_begin(&rx_ring->syncp);
  693. rx_ring->rx_stats.skb_alloc_fail++;
  694. u64_stats_update_end(&rx_ring->syncp);
  695. netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
  696. "Failed to allocate skb. frags: %d\n", frags);
  697. return NULL;
  698. }
  699. return skb;
  700. }
  701. static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring,
  702. struct ena_com_rx_buf_info *ena_bufs,
  703. u32 descs,
  704. u16 *next_to_clean)
  705. {
  706. struct sk_buff *skb;
  707. struct ena_rx_buffer *rx_info;
  708. u16 len, req_id, buf = 0;
  709. void *va;
  710. len = ena_bufs[buf].len;
  711. req_id = ena_bufs[buf].req_id;
  712. rx_info = &rx_ring->rx_buffer_info[req_id];
  713. if (unlikely(!rx_info->page)) {
  714. netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
  715. "Page is NULL\n");
  716. return NULL;
  717. }
  718. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  719. "rx_info %p page %p\n",
  720. rx_info, rx_info->page);
  721. /* save virt address of first buffer */
  722. va = page_address(rx_info->page) + rx_info->page_offset;
  723. prefetch(va + NET_IP_ALIGN);
  724. if (len <= rx_ring->rx_copybreak) {
  725. skb = ena_alloc_skb(rx_ring, false);
  726. if (unlikely(!skb))
  727. return NULL;
  728. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  729. "rx allocated small packet. len %d. data_len %d\n",
  730. skb->len, skb->data_len);
  731. /* sync this buffer for CPU use */
  732. dma_sync_single_for_cpu(rx_ring->dev,
  733. dma_unmap_addr(&rx_info->ena_buf, paddr),
  734. len,
  735. DMA_FROM_DEVICE);
  736. skb_copy_to_linear_data(skb, va, len);
  737. dma_sync_single_for_device(rx_ring->dev,
  738. dma_unmap_addr(&rx_info->ena_buf, paddr),
  739. len,
  740. DMA_FROM_DEVICE);
  741. skb_put(skb, len);
  742. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  743. rx_ring->free_rx_ids[*next_to_clean] = req_id;
  744. *next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs,
  745. rx_ring->ring_size);
  746. return skb;
  747. }
  748. skb = ena_alloc_skb(rx_ring, true);
  749. if (unlikely(!skb))
  750. return NULL;
  751. do {
  752. dma_unmap_page(rx_ring->dev,
  753. dma_unmap_addr(&rx_info->ena_buf, paddr),
  754. ENA_PAGE_SIZE, DMA_FROM_DEVICE);
  755. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page,
  756. rx_info->page_offset, len, ENA_PAGE_SIZE);
  757. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  758. "rx skb updated. len %d. data_len %d\n",
  759. skb->len, skb->data_len);
  760. rx_info->page = NULL;
  761. rx_ring->free_rx_ids[*next_to_clean] = req_id;
  762. *next_to_clean =
  763. ENA_RX_RING_IDX_NEXT(*next_to_clean,
  764. rx_ring->ring_size);
  765. if (likely(--descs == 0))
  766. break;
  767. buf++;
  768. len = ena_bufs[buf].len;
  769. req_id = ena_bufs[buf].req_id;
  770. rx_info = &rx_ring->rx_buffer_info[req_id];
  771. } while (1);
  772. return skb;
  773. }
  774. /* ena_rx_checksum - indicate in skb if hw indicated a good cksum
  775. * @adapter: structure containing adapter specific data
  776. * @ena_rx_ctx: received packet context/metadata
  777. * @skb: skb currently being received and modified
  778. */
  779. static inline void ena_rx_checksum(struct ena_ring *rx_ring,
  780. struct ena_com_rx_ctx *ena_rx_ctx,
  781. struct sk_buff *skb)
  782. {
  783. /* Rx csum disabled */
  784. if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) {
  785. skb->ip_summed = CHECKSUM_NONE;
  786. return;
  787. }
  788. /* For fragmented packets the checksum isn't valid */
  789. if (ena_rx_ctx->frag) {
  790. skb->ip_summed = CHECKSUM_NONE;
  791. return;
  792. }
  793. /* if IP and error */
  794. if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
  795. (ena_rx_ctx->l3_csum_err))) {
  796. /* ipv4 checksum error */
  797. skb->ip_summed = CHECKSUM_NONE;
  798. u64_stats_update_begin(&rx_ring->syncp);
  799. rx_ring->rx_stats.bad_csum++;
  800. u64_stats_update_end(&rx_ring->syncp);
  801. netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
  802. "RX IPv4 header checksum error\n");
  803. return;
  804. }
  805. /* if TCP/UDP */
  806. if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
  807. (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) {
  808. if (unlikely(ena_rx_ctx->l4_csum_err)) {
  809. /* TCP/UDP checksum error */
  810. u64_stats_update_begin(&rx_ring->syncp);
  811. rx_ring->rx_stats.bad_csum++;
  812. u64_stats_update_end(&rx_ring->syncp);
  813. netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
  814. "RX L4 checksum error\n");
  815. skb->ip_summed = CHECKSUM_NONE;
  816. return;
  817. }
  818. skb->ip_summed = CHECKSUM_UNNECESSARY;
  819. }
  820. }
  821. static void ena_set_rx_hash(struct ena_ring *rx_ring,
  822. struct ena_com_rx_ctx *ena_rx_ctx,
  823. struct sk_buff *skb)
  824. {
  825. enum pkt_hash_types hash_type;
  826. if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) {
  827. if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
  828. (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)))
  829. hash_type = PKT_HASH_TYPE_L4;
  830. else
  831. hash_type = PKT_HASH_TYPE_NONE;
  832. /* Override hash type if the packet is fragmented */
  833. if (ena_rx_ctx->frag)
  834. hash_type = PKT_HASH_TYPE_NONE;
  835. skb_set_hash(skb, ena_rx_ctx->hash, hash_type);
  836. }
  837. }
  838. /* ena_clean_rx_irq - Cleanup RX irq
  839. * @rx_ring: RX ring to clean
  840. * @napi: napi handler
  841. * @budget: how many packets driver is allowed to clean
  842. *
  843. * Returns the number of cleaned buffers.
  844. */
  845. static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi,
  846. u32 budget)
  847. {
  848. u16 next_to_clean = rx_ring->next_to_clean;
  849. u32 res_budget, work_done;
  850. struct ena_com_rx_ctx ena_rx_ctx;
  851. struct ena_adapter *adapter;
  852. struct sk_buff *skb;
  853. int refill_required;
  854. int refill_threshold;
  855. int rc = 0;
  856. int total_len = 0;
  857. int rx_copybreak_pkt = 0;
  858. int i;
  859. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  860. "%s qid %d\n", __func__, rx_ring->qid);
  861. res_budget = budget;
  862. do {
  863. ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
  864. ena_rx_ctx.max_bufs = rx_ring->sgl_size;
  865. ena_rx_ctx.descs = 0;
  866. rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
  867. rx_ring->ena_com_io_sq,
  868. &ena_rx_ctx);
  869. if (unlikely(rc))
  870. goto error;
  871. if (unlikely(ena_rx_ctx.descs == 0))
  872. break;
  873. netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
  874. "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n",
  875. rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
  876. ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
  877. /* allocate skb and fill it */
  878. skb = ena_rx_skb(rx_ring, rx_ring->ena_bufs, ena_rx_ctx.descs,
  879. &next_to_clean);
  880. /* exit if we failed to retrieve a buffer */
  881. if (unlikely(!skb)) {
  882. for (i = 0; i < ena_rx_ctx.descs; i++) {
  883. rx_ring->free_tx_ids[next_to_clean] =
  884. rx_ring->ena_bufs[i].req_id;
  885. next_to_clean =
  886. ENA_RX_RING_IDX_NEXT(next_to_clean,
  887. rx_ring->ring_size);
  888. }
  889. break;
  890. }
  891. ena_rx_checksum(rx_ring, &ena_rx_ctx, skb);
  892. ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb);
  893. skb_record_rx_queue(skb, rx_ring->qid);
  894. if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak) {
  895. total_len += rx_ring->ena_bufs[0].len;
  896. rx_copybreak_pkt++;
  897. napi_gro_receive(napi, skb);
  898. } else {
  899. total_len += skb->len;
  900. napi_gro_frags(napi);
  901. }
  902. res_budget--;
  903. } while (likely(res_budget));
  904. work_done = budget - res_budget;
  905. rx_ring->per_napi_bytes += total_len;
  906. rx_ring->per_napi_packets += work_done;
  907. u64_stats_update_begin(&rx_ring->syncp);
  908. rx_ring->rx_stats.bytes += total_len;
  909. rx_ring->rx_stats.cnt += work_done;
  910. rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt;
  911. u64_stats_update_end(&rx_ring->syncp);
  912. rx_ring->next_to_clean = next_to_clean;
  913. refill_required = ena_com_sq_empty_space(rx_ring->ena_com_io_sq);
  914. refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
  915. /* Optimization, try to batch new rx buffers */
  916. if (refill_required > refill_threshold) {
  917. ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
  918. ena_refill_rx_bufs(rx_ring, refill_required);
  919. }
  920. return work_done;
  921. error:
  922. adapter = netdev_priv(rx_ring->netdev);
  923. u64_stats_update_begin(&rx_ring->syncp);
  924. rx_ring->rx_stats.bad_desc_num++;
  925. u64_stats_update_end(&rx_ring->syncp);
  926. /* Too many desc from the device. Trigger reset */
  927. adapter->reset_reason = ENA_REGS_RESET_TOO_MANY_RX_DESCS;
  928. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  929. return 0;
  930. }
  931. inline void ena_adjust_intr_moderation(struct ena_ring *rx_ring,
  932. struct ena_ring *tx_ring)
  933. {
  934. /* We apply adaptive moderation on Rx path only.
  935. * Tx uses static interrupt moderation.
  936. */
  937. ena_com_calculate_interrupt_delay(rx_ring->ena_dev,
  938. rx_ring->per_napi_packets,
  939. rx_ring->per_napi_bytes,
  940. &rx_ring->smoothed_interval,
  941. &rx_ring->moder_tbl_idx);
  942. /* Reset per napi packets/bytes */
  943. tx_ring->per_napi_packets = 0;
  944. tx_ring->per_napi_bytes = 0;
  945. rx_ring->per_napi_packets = 0;
  946. rx_ring->per_napi_bytes = 0;
  947. }
  948. static inline void ena_unmask_interrupt(struct ena_ring *tx_ring,
  949. struct ena_ring *rx_ring)
  950. {
  951. struct ena_eth_io_intr_reg intr_reg;
  952. /* Update intr register: rx intr delay,
  953. * tx intr delay and interrupt unmask
  954. */
  955. ena_com_update_intr_reg(&intr_reg,
  956. rx_ring->smoothed_interval,
  957. tx_ring->smoothed_interval,
  958. true);
  959. /* It is a shared MSI-X.
  960. * Tx and Rx CQ have pointer to it.
  961. * So we use one of them to reach the intr reg
  962. */
  963. ena_com_unmask_intr(rx_ring->ena_com_io_cq, &intr_reg);
  964. }
  965. static inline void ena_update_ring_numa_node(struct ena_ring *tx_ring,
  966. struct ena_ring *rx_ring)
  967. {
  968. int cpu = get_cpu();
  969. int numa_node;
  970. /* Check only one ring since the 2 rings are running on the same cpu */
  971. if (likely(tx_ring->cpu == cpu))
  972. goto out;
  973. numa_node = cpu_to_node(cpu);
  974. put_cpu();
  975. if (numa_node != NUMA_NO_NODE) {
  976. ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node);
  977. ena_com_update_numa_node(rx_ring->ena_com_io_cq, numa_node);
  978. }
  979. tx_ring->cpu = cpu;
  980. rx_ring->cpu = cpu;
  981. return;
  982. out:
  983. put_cpu();
  984. }
  985. static int ena_io_poll(struct napi_struct *napi, int budget)
  986. {
  987. struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
  988. struct ena_ring *tx_ring, *rx_ring;
  989. int tx_work_done;
  990. int rx_work_done = 0;
  991. int tx_budget;
  992. int napi_comp_call = 0;
  993. int ret;
  994. tx_ring = ena_napi->tx_ring;
  995. rx_ring = ena_napi->rx_ring;
  996. tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER;
  997. if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
  998. test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags)) {
  999. napi_complete_done(napi, 0);
  1000. return 0;
  1001. }
  1002. tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget);
  1003. /* On netpoll the budget is zero and the handler should only clean the
  1004. * tx completions.
  1005. */
  1006. if (likely(budget))
  1007. rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget);
  1008. /* If the device is about to reset or down, avoid unmask
  1009. * the interrupt and return 0 so NAPI won't reschedule
  1010. */
  1011. if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
  1012. test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags))) {
  1013. napi_complete_done(napi, 0);
  1014. ret = 0;
  1015. } else if ((budget > rx_work_done) && (tx_budget > tx_work_done)) {
  1016. napi_comp_call = 1;
  1017. /* Update numa and unmask the interrupt only when schedule
  1018. * from the interrupt context (vs from sk_busy_loop)
  1019. */
  1020. if (napi_complete_done(napi, rx_work_done)) {
  1021. /* Tx and Rx share the same interrupt vector */
  1022. if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev))
  1023. ena_adjust_intr_moderation(rx_ring, tx_ring);
  1024. ena_unmask_interrupt(tx_ring, rx_ring);
  1025. }
  1026. ena_update_ring_numa_node(tx_ring, rx_ring);
  1027. ret = rx_work_done;
  1028. } else {
  1029. ret = budget;
  1030. }
  1031. u64_stats_update_begin(&tx_ring->syncp);
  1032. tx_ring->tx_stats.napi_comp += napi_comp_call;
  1033. tx_ring->tx_stats.tx_poll++;
  1034. u64_stats_update_end(&tx_ring->syncp);
  1035. return ret;
  1036. }
  1037. static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data)
  1038. {
  1039. struct ena_adapter *adapter = (struct ena_adapter *)data;
  1040. ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
  1041. /* Don't call the aenq handler before probe is done */
  1042. if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)))
  1043. ena_com_aenq_intr_handler(adapter->ena_dev, data);
  1044. return IRQ_HANDLED;
  1045. }
  1046. /* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx
  1047. * @irq: interrupt number
  1048. * @data: pointer to a network interface private napi device structure
  1049. */
  1050. static irqreturn_t ena_intr_msix_io(int irq, void *data)
  1051. {
  1052. struct ena_napi *ena_napi = data;
  1053. ena_napi->tx_ring->first_interrupt = true;
  1054. ena_napi->rx_ring->first_interrupt = true;
  1055. napi_schedule_irqoff(&ena_napi->napi);
  1056. return IRQ_HANDLED;
  1057. }
  1058. /* Reserve a single MSI-X vector for management (admin + aenq).
  1059. * plus reserve one vector for each potential io queue.
  1060. * the number of potential io queues is the minimum of what the device
  1061. * supports and the number of vCPUs.
  1062. */
  1063. static int ena_enable_msix(struct ena_adapter *adapter, int num_queues)
  1064. {
  1065. int msix_vecs, irq_cnt;
  1066. if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
  1067. netif_err(adapter, probe, adapter->netdev,
  1068. "Error, MSI-X is already enabled\n");
  1069. return -EPERM;
  1070. }
  1071. /* Reserved the max msix vectors we might need */
  1072. msix_vecs = ENA_MAX_MSIX_VEC(num_queues);
  1073. netif_dbg(adapter, probe, adapter->netdev,
  1074. "trying to enable MSI-X, vectors %d\n", msix_vecs);
  1075. irq_cnt = pci_alloc_irq_vectors(adapter->pdev, ENA_MIN_MSIX_VEC,
  1076. msix_vecs, PCI_IRQ_MSIX);
  1077. if (irq_cnt < 0) {
  1078. netif_err(adapter, probe, adapter->netdev,
  1079. "Failed to enable MSI-X. irq_cnt %d\n", irq_cnt);
  1080. return -ENOSPC;
  1081. }
  1082. if (irq_cnt != msix_vecs) {
  1083. netif_notice(adapter, probe, adapter->netdev,
  1084. "enable only %d MSI-X (out of %d), reduce the number of queues\n",
  1085. irq_cnt, msix_vecs);
  1086. adapter->num_queues = irq_cnt - ENA_ADMIN_MSIX_VEC;
  1087. }
  1088. if (ena_init_rx_cpu_rmap(adapter))
  1089. netif_warn(adapter, probe, adapter->netdev,
  1090. "Failed to map IRQs to CPUs\n");
  1091. adapter->msix_vecs = irq_cnt;
  1092. set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags);
  1093. return 0;
  1094. }
  1095. static void ena_setup_mgmnt_intr(struct ena_adapter *adapter)
  1096. {
  1097. u32 cpu;
  1098. snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
  1099. ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
  1100. pci_name(adapter->pdev));
  1101. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler =
  1102. ena_intr_msix_mgmnt;
  1103. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
  1104. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
  1105. pci_irq_vector(adapter->pdev, ENA_MGMNT_IRQ_IDX);
  1106. cpu = cpumask_first(cpu_online_mask);
  1107. adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu;
  1108. cpumask_set_cpu(cpu,
  1109. &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask);
  1110. }
  1111. static void ena_setup_io_intr(struct ena_adapter *adapter)
  1112. {
  1113. struct net_device *netdev;
  1114. int irq_idx, i, cpu;
  1115. netdev = adapter->netdev;
  1116. for (i = 0; i < adapter->num_queues; i++) {
  1117. irq_idx = ENA_IO_IRQ_IDX(i);
  1118. cpu = i % num_online_cpus();
  1119. snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
  1120. "%s-Tx-Rx-%d", netdev->name, i);
  1121. adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io;
  1122. adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i];
  1123. adapter->irq_tbl[irq_idx].vector =
  1124. pci_irq_vector(adapter->pdev, irq_idx);
  1125. adapter->irq_tbl[irq_idx].cpu = cpu;
  1126. cpumask_set_cpu(cpu,
  1127. &adapter->irq_tbl[irq_idx].affinity_hint_mask);
  1128. }
  1129. }
  1130. static int ena_request_mgmnt_irq(struct ena_adapter *adapter)
  1131. {
  1132. unsigned long flags = 0;
  1133. struct ena_irq *irq;
  1134. int rc;
  1135. irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
  1136. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  1137. irq->data);
  1138. if (rc) {
  1139. netif_err(adapter, probe, adapter->netdev,
  1140. "failed to request admin irq\n");
  1141. return rc;
  1142. }
  1143. netif_dbg(adapter, probe, adapter->netdev,
  1144. "set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n",
  1145. irq->affinity_hint_mask.bits[0], irq->vector);
  1146. irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
  1147. return rc;
  1148. }
  1149. static int ena_request_io_irq(struct ena_adapter *adapter)
  1150. {
  1151. unsigned long flags = 0;
  1152. struct ena_irq *irq;
  1153. int rc = 0, i, k;
  1154. if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
  1155. netif_err(adapter, ifup, adapter->netdev,
  1156. "Failed to request I/O IRQ: MSI-X is not enabled\n");
  1157. return -EINVAL;
  1158. }
  1159. for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
  1160. irq = &adapter->irq_tbl[i];
  1161. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  1162. irq->data);
  1163. if (rc) {
  1164. netif_err(adapter, ifup, adapter->netdev,
  1165. "Failed to request I/O IRQ. index %d rc %d\n",
  1166. i, rc);
  1167. goto err;
  1168. }
  1169. netif_dbg(adapter, ifup, adapter->netdev,
  1170. "set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n",
  1171. i, irq->affinity_hint_mask.bits[0], irq->vector);
  1172. irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
  1173. }
  1174. return rc;
  1175. err:
  1176. for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) {
  1177. irq = &adapter->irq_tbl[k];
  1178. free_irq(irq->vector, irq->data);
  1179. }
  1180. return rc;
  1181. }
  1182. static void ena_free_mgmnt_irq(struct ena_adapter *adapter)
  1183. {
  1184. struct ena_irq *irq;
  1185. irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
  1186. synchronize_irq(irq->vector);
  1187. irq_set_affinity_hint(irq->vector, NULL);
  1188. free_irq(irq->vector, irq->data);
  1189. }
  1190. static void ena_free_io_irq(struct ena_adapter *adapter)
  1191. {
  1192. struct ena_irq *irq;
  1193. int i;
  1194. #ifdef CONFIG_RFS_ACCEL
  1195. if (adapter->msix_vecs >= 1) {
  1196. free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
  1197. adapter->netdev->rx_cpu_rmap = NULL;
  1198. }
  1199. #endif /* CONFIG_RFS_ACCEL */
  1200. for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
  1201. irq = &adapter->irq_tbl[i];
  1202. irq_set_affinity_hint(irq->vector, NULL);
  1203. free_irq(irq->vector, irq->data);
  1204. }
  1205. }
  1206. static void ena_disable_msix(struct ena_adapter *adapter)
  1207. {
  1208. if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags))
  1209. pci_free_irq_vectors(adapter->pdev);
  1210. }
  1211. static void ena_disable_io_intr_sync(struct ena_adapter *adapter)
  1212. {
  1213. int i;
  1214. if (!netif_running(adapter->netdev))
  1215. return;
  1216. for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++)
  1217. synchronize_irq(adapter->irq_tbl[i].vector);
  1218. }
  1219. static void ena_del_napi(struct ena_adapter *adapter)
  1220. {
  1221. int i;
  1222. for (i = 0; i < adapter->num_queues; i++)
  1223. netif_napi_del(&adapter->ena_napi[i].napi);
  1224. }
  1225. static void ena_init_napi(struct ena_adapter *adapter)
  1226. {
  1227. struct ena_napi *napi;
  1228. int i;
  1229. for (i = 0; i < adapter->num_queues; i++) {
  1230. napi = &adapter->ena_napi[i];
  1231. netif_napi_add(adapter->netdev,
  1232. &adapter->ena_napi[i].napi,
  1233. ena_io_poll,
  1234. ENA_NAPI_BUDGET);
  1235. napi->rx_ring = &adapter->rx_ring[i];
  1236. napi->tx_ring = &adapter->tx_ring[i];
  1237. napi->qid = i;
  1238. }
  1239. }
  1240. static void ena_napi_disable_all(struct ena_adapter *adapter)
  1241. {
  1242. int i;
  1243. for (i = 0; i < adapter->num_queues; i++)
  1244. napi_disable(&adapter->ena_napi[i].napi);
  1245. }
  1246. static void ena_napi_enable_all(struct ena_adapter *adapter)
  1247. {
  1248. int i;
  1249. for (i = 0; i < adapter->num_queues; i++)
  1250. napi_enable(&adapter->ena_napi[i].napi);
  1251. }
  1252. static void ena_restore_ethtool_params(struct ena_adapter *adapter)
  1253. {
  1254. adapter->tx_usecs = 0;
  1255. adapter->rx_usecs = 0;
  1256. adapter->tx_frames = 1;
  1257. adapter->rx_frames = 1;
  1258. }
  1259. /* Configure the Rx forwarding */
  1260. static int ena_rss_configure(struct ena_adapter *adapter)
  1261. {
  1262. struct ena_com_dev *ena_dev = adapter->ena_dev;
  1263. int rc;
  1264. /* In case the RSS table wasn't initialized by probe */
  1265. if (!ena_dev->rss.tbl_log_size) {
  1266. rc = ena_rss_init_default(adapter);
  1267. if (rc && (rc != -EOPNOTSUPP)) {
  1268. netif_err(adapter, ifup, adapter->netdev,
  1269. "Failed to init RSS rc: %d\n", rc);
  1270. return rc;
  1271. }
  1272. }
  1273. /* Set indirect table */
  1274. rc = ena_com_indirect_table_set(ena_dev);
  1275. if (unlikely(rc && rc != -EOPNOTSUPP))
  1276. return rc;
  1277. /* Configure hash function (if supported) */
  1278. rc = ena_com_set_hash_function(ena_dev);
  1279. if (unlikely(rc && (rc != -EOPNOTSUPP)))
  1280. return rc;
  1281. /* Configure hash inputs (if supported) */
  1282. rc = ena_com_set_hash_ctrl(ena_dev);
  1283. if (unlikely(rc && (rc != -EOPNOTSUPP)))
  1284. return rc;
  1285. return 0;
  1286. }
  1287. static int ena_up_complete(struct ena_adapter *adapter)
  1288. {
  1289. int rc;
  1290. rc = ena_rss_configure(adapter);
  1291. if (rc)
  1292. return rc;
  1293. ena_change_mtu(adapter->netdev, adapter->netdev->mtu);
  1294. ena_refill_all_rx_bufs(adapter);
  1295. /* enable transmits */
  1296. netif_tx_start_all_queues(adapter->netdev);
  1297. ena_restore_ethtool_params(adapter);
  1298. ena_napi_enable_all(adapter);
  1299. return 0;
  1300. }
  1301. static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid)
  1302. {
  1303. struct ena_com_create_io_ctx ctx = { 0 };
  1304. struct ena_com_dev *ena_dev;
  1305. struct ena_ring *tx_ring;
  1306. u32 msix_vector;
  1307. u16 ena_qid;
  1308. int rc;
  1309. ena_dev = adapter->ena_dev;
  1310. tx_ring = &adapter->tx_ring[qid];
  1311. msix_vector = ENA_IO_IRQ_IDX(qid);
  1312. ena_qid = ENA_IO_TXQ_IDX(qid);
  1313. ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
  1314. ctx.qid = ena_qid;
  1315. ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
  1316. ctx.msix_vector = msix_vector;
  1317. ctx.queue_size = adapter->tx_ring_size;
  1318. ctx.numa_node = cpu_to_node(tx_ring->cpu);
  1319. rc = ena_com_create_io_queue(ena_dev, &ctx);
  1320. if (rc) {
  1321. netif_err(adapter, ifup, adapter->netdev,
  1322. "Failed to create I/O TX queue num %d rc: %d\n",
  1323. qid, rc);
  1324. return rc;
  1325. }
  1326. rc = ena_com_get_io_handlers(ena_dev, ena_qid,
  1327. &tx_ring->ena_com_io_sq,
  1328. &tx_ring->ena_com_io_cq);
  1329. if (rc) {
  1330. netif_err(adapter, ifup, adapter->netdev,
  1331. "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
  1332. qid, rc);
  1333. ena_com_destroy_io_queue(ena_dev, ena_qid);
  1334. return rc;
  1335. }
  1336. ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node);
  1337. return rc;
  1338. }
  1339. static int ena_create_all_io_tx_queues(struct ena_adapter *adapter)
  1340. {
  1341. struct ena_com_dev *ena_dev = adapter->ena_dev;
  1342. int rc, i;
  1343. for (i = 0; i < adapter->num_queues; i++) {
  1344. rc = ena_create_io_tx_queue(adapter, i);
  1345. if (rc)
  1346. goto create_err;
  1347. }
  1348. return 0;
  1349. create_err:
  1350. while (i--)
  1351. ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
  1352. return rc;
  1353. }
  1354. static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid)
  1355. {
  1356. struct ena_com_dev *ena_dev;
  1357. struct ena_com_create_io_ctx ctx = { 0 };
  1358. struct ena_ring *rx_ring;
  1359. u32 msix_vector;
  1360. u16 ena_qid;
  1361. int rc;
  1362. ena_dev = adapter->ena_dev;
  1363. rx_ring = &adapter->rx_ring[qid];
  1364. msix_vector = ENA_IO_IRQ_IDX(qid);
  1365. ena_qid = ENA_IO_RXQ_IDX(qid);
  1366. ctx.qid = ena_qid;
  1367. ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
  1368. ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
  1369. ctx.msix_vector = msix_vector;
  1370. ctx.queue_size = adapter->rx_ring_size;
  1371. ctx.numa_node = cpu_to_node(rx_ring->cpu);
  1372. rc = ena_com_create_io_queue(ena_dev, &ctx);
  1373. if (rc) {
  1374. netif_err(adapter, ifup, adapter->netdev,
  1375. "Failed to create I/O RX queue num %d rc: %d\n",
  1376. qid, rc);
  1377. return rc;
  1378. }
  1379. rc = ena_com_get_io_handlers(ena_dev, ena_qid,
  1380. &rx_ring->ena_com_io_sq,
  1381. &rx_ring->ena_com_io_cq);
  1382. if (rc) {
  1383. netif_err(adapter, ifup, adapter->netdev,
  1384. "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
  1385. qid, rc);
  1386. ena_com_destroy_io_queue(ena_dev, ena_qid);
  1387. return rc;
  1388. }
  1389. ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node);
  1390. return rc;
  1391. }
  1392. static int ena_create_all_io_rx_queues(struct ena_adapter *adapter)
  1393. {
  1394. struct ena_com_dev *ena_dev = adapter->ena_dev;
  1395. int rc, i;
  1396. for (i = 0; i < adapter->num_queues; i++) {
  1397. rc = ena_create_io_rx_queue(adapter, i);
  1398. if (rc)
  1399. goto create_err;
  1400. }
  1401. return 0;
  1402. create_err:
  1403. while (i--)
  1404. ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
  1405. return rc;
  1406. }
  1407. static int ena_up(struct ena_adapter *adapter)
  1408. {
  1409. int rc, i;
  1410. netdev_dbg(adapter->netdev, "%s\n", __func__);
  1411. ena_setup_io_intr(adapter);
  1412. /* napi poll functions should be initialized before running
  1413. * request_irq(), to handle a rare condition where there is a pending
  1414. * interrupt, causing the ISR to fire immediately while the poll
  1415. * function wasn't set yet, causing a null dereference
  1416. */
  1417. ena_init_napi(adapter);
  1418. rc = ena_request_io_irq(adapter);
  1419. if (rc)
  1420. goto err_req_irq;
  1421. /* allocate transmit descriptors */
  1422. rc = ena_setup_all_tx_resources(adapter);
  1423. if (rc)
  1424. goto err_setup_tx;
  1425. /* allocate receive descriptors */
  1426. rc = ena_setup_all_rx_resources(adapter);
  1427. if (rc)
  1428. goto err_setup_rx;
  1429. /* Create TX queues */
  1430. rc = ena_create_all_io_tx_queues(adapter);
  1431. if (rc)
  1432. goto err_create_tx_queues;
  1433. /* Create RX queues */
  1434. rc = ena_create_all_io_rx_queues(adapter);
  1435. if (rc)
  1436. goto err_create_rx_queues;
  1437. rc = ena_up_complete(adapter);
  1438. if (rc)
  1439. goto err_up;
  1440. if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
  1441. netif_carrier_on(adapter->netdev);
  1442. u64_stats_update_begin(&adapter->syncp);
  1443. adapter->dev_stats.interface_up++;
  1444. u64_stats_update_end(&adapter->syncp);
  1445. set_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  1446. /* Enable completion queues interrupt */
  1447. for (i = 0; i < adapter->num_queues; i++)
  1448. ena_unmask_interrupt(&adapter->tx_ring[i],
  1449. &adapter->rx_ring[i]);
  1450. /* schedule napi in case we had pending packets
  1451. * from the last time we disable napi
  1452. */
  1453. for (i = 0; i < adapter->num_queues; i++)
  1454. napi_schedule(&adapter->ena_napi[i].napi);
  1455. return rc;
  1456. err_up:
  1457. ena_destroy_all_rx_queues(adapter);
  1458. err_create_rx_queues:
  1459. ena_destroy_all_tx_queues(adapter);
  1460. err_create_tx_queues:
  1461. ena_free_all_io_rx_resources(adapter);
  1462. err_setup_rx:
  1463. ena_free_all_io_tx_resources(adapter);
  1464. err_setup_tx:
  1465. ena_free_io_irq(adapter);
  1466. err_req_irq:
  1467. ena_del_napi(adapter);
  1468. return rc;
  1469. }
  1470. static void ena_down(struct ena_adapter *adapter)
  1471. {
  1472. netif_info(adapter, ifdown, adapter->netdev, "%s\n", __func__);
  1473. clear_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  1474. u64_stats_update_begin(&adapter->syncp);
  1475. adapter->dev_stats.interface_down++;
  1476. u64_stats_update_end(&adapter->syncp);
  1477. netif_carrier_off(adapter->netdev);
  1478. netif_tx_disable(adapter->netdev);
  1479. /* After this point the napi handler won't enable the tx queue */
  1480. ena_napi_disable_all(adapter);
  1481. /* After destroy the queue there won't be any new interrupts */
  1482. if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) {
  1483. int rc;
  1484. rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
  1485. if (rc)
  1486. dev_err(&adapter->pdev->dev, "Device reset failed\n");
  1487. }
  1488. ena_destroy_all_io_queues(adapter);
  1489. ena_disable_io_intr_sync(adapter);
  1490. ena_free_io_irq(adapter);
  1491. ena_del_napi(adapter);
  1492. ena_free_all_tx_bufs(adapter);
  1493. ena_free_all_rx_bufs(adapter);
  1494. ena_free_all_io_tx_resources(adapter);
  1495. ena_free_all_io_rx_resources(adapter);
  1496. }
  1497. /* ena_open - Called when a network interface is made active
  1498. * @netdev: network interface device structure
  1499. *
  1500. * Returns 0 on success, negative value on failure
  1501. *
  1502. * The open entry point is called when a network interface is made
  1503. * active by the system (IFF_UP). At this point all resources needed
  1504. * for transmit and receive operations are allocated, the interrupt
  1505. * handler is registered with the OS, the watchdog timer is started,
  1506. * and the stack is notified that the interface is ready.
  1507. */
  1508. static int ena_open(struct net_device *netdev)
  1509. {
  1510. struct ena_adapter *adapter = netdev_priv(netdev);
  1511. int rc;
  1512. /* Notify the stack of the actual queue counts. */
  1513. rc = netif_set_real_num_tx_queues(netdev, adapter->num_queues);
  1514. if (rc) {
  1515. netif_err(adapter, ifup, netdev, "Can't set num tx queues\n");
  1516. return rc;
  1517. }
  1518. rc = netif_set_real_num_rx_queues(netdev, adapter->num_queues);
  1519. if (rc) {
  1520. netif_err(adapter, ifup, netdev, "Can't set num rx queues\n");
  1521. return rc;
  1522. }
  1523. rc = ena_up(adapter);
  1524. if (rc)
  1525. return rc;
  1526. return rc;
  1527. }
  1528. /* ena_close - Disables a network interface
  1529. * @netdev: network interface device structure
  1530. *
  1531. * Returns 0, this is not allowed to fail
  1532. *
  1533. * The close entry point is called when an interface is de-activated
  1534. * by the OS. The hardware is still under the drivers control, but
  1535. * needs to be disabled. A global MAC reset is issued to stop the
  1536. * hardware, and all transmit and receive resources are freed.
  1537. */
  1538. static int ena_close(struct net_device *netdev)
  1539. {
  1540. struct ena_adapter *adapter = netdev_priv(netdev);
  1541. netif_dbg(adapter, ifdown, netdev, "%s\n", __func__);
  1542. if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  1543. ena_down(adapter);
  1544. /* Check for device status and issue reset if needed*/
  1545. check_for_admin_com_state(adapter);
  1546. if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  1547. netif_err(adapter, ifdown, adapter->netdev,
  1548. "Destroy failure, restarting device\n");
  1549. ena_dump_stats_to_dmesg(adapter);
  1550. /* rtnl lock already obtained in dev_ioctl() layer */
  1551. ena_destroy_device(adapter, false);
  1552. ena_restore_device(adapter);
  1553. }
  1554. return 0;
  1555. }
  1556. static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct sk_buff *skb)
  1557. {
  1558. u32 mss = skb_shinfo(skb)->gso_size;
  1559. struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
  1560. u8 l4_protocol = 0;
  1561. if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) {
  1562. ena_tx_ctx->l4_csum_enable = 1;
  1563. if (mss) {
  1564. ena_tx_ctx->tso_enable = 1;
  1565. ena_meta->l4_hdr_len = tcp_hdr(skb)->doff;
  1566. ena_tx_ctx->l4_csum_partial = 0;
  1567. } else {
  1568. ena_tx_ctx->tso_enable = 0;
  1569. ena_meta->l4_hdr_len = 0;
  1570. ena_tx_ctx->l4_csum_partial = 1;
  1571. }
  1572. switch (ip_hdr(skb)->version) {
  1573. case IPVERSION:
  1574. ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
  1575. if (ip_hdr(skb)->frag_off & htons(IP_DF))
  1576. ena_tx_ctx->df = 1;
  1577. if (mss)
  1578. ena_tx_ctx->l3_csum_enable = 1;
  1579. l4_protocol = ip_hdr(skb)->protocol;
  1580. break;
  1581. case 6:
  1582. ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
  1583. l4_protocol = ipv6_hdr(skb)->nexthdr;
  1584. break;
  1585. default:
  1586. break;
  1587. }
  1588. if (l4_protocol == IPPROTO_TCP)
  1589. ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
  1590. else
  1591. ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
  1592. ena_meta->mss = mss;
  1593. ena_meta->l3_hdr_len = skb_network_header_len(skb);
  1594. ena_meta->l3_hdr_offset = skb_network_offset(skb);
  1595. ena_tx_ctx->meta_valid = 1;
  1596. } else {
  1597. ena_tx_ctx->meta_valid = 0;
  1598. }
  1599. }
  1600. static int ena_check_and_linearize_skb(struct ena_ring *tx_ring,
  1601. struct sk_buff *skb)
  1602. {
  1603. int num_frags, header_len, rc;
  1604. num_frags = skb_shinfo(skb)->nr_frags;
  1605. header_len = skb_headlen(skb);
  1606. if (num_frags < tx_ring->sgl_size)
  1607. return 0;
  1608. if ((num_frags == tx_ring->sgl_size) &&
  1609. (header_len < tx_ring->tx_max_header_size))
  1610. return 0;
  1611. u64_stats_update_begin(&tx_ring->syncp);
  1612. tx_ring->tx_stats.linearize++;
  1613. u64_stats_update_end(&tx_ring->syncp);
  1614. rc = skb_linearize(skb);
  1615. if (unlikely(rc)) {
  1616. u64_stats_update_begin(&tx_ring->syncp);
  1617. tx_ring->tx_stats.linearize_failed++;
  1618. u64_stats_update_end(&tx_ring->syncp);
  1619. }
  1620. return rc;
  1621. }
  1622. /* Called with netif_tx_lock. */
  1623. static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1624. {
  1625. struct ena_adapter *adapter = netdev_priv(dev);
  1626. struct ena_tx_buffer *tx_info;
  1627. struct ena_com_tx_ctx ena_tx_ctx;
  1628. struct ena_ring *tx_ring;
  1629. struct netdev_queue *txq;
  1630. struct ena_com_buf *ena_buf;
  1631. void *push_hdr;
  1632. u32 len, last_frag;
  1633. u16 next_to_use;
  1634. u16 req_id;
  1635. u16 push_len;
  1636. u16 header_len;
  1637. dma_addr_t dma;
  1638. int qid, rc, nb_hw_desc;
  1639. int i = -1;
  1640. netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb);
  1641. /* Determine which tx ring we will be placed on */
  1642. qid = skb_get_queue_mapping(skb);
  1643. tx_ring = &adapter->tx_ring[qid];
  1644. txq = netdev_get_tx_queue(dev, qid);
  1645. rc = ena_check_and_linearize_skb(tx_ring, skb);
  1646. if (unlikely(rc))
  1647. goto error_drop_packet;
  1648. skb_tx_timestamp(skb);
  1649. len = skb_headlen(skb);
  1650. next_to_use = tx_ring->next_to_use;
  1651. req_id = tx_ring->free_tx_ids[next_to_use];
  1652. tx_info = &tx_ring->tx_buffer_info[req_id];
  1653. tx_info->num_of_bufs = 0;
  1654. WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id);
  1655. ena_buf = tx_info->bufs;
  1656. tx_info->skb = skb;
  1657. if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
  1658. /* prepared the push buffer */
  1659. push_len = min_t(u32, len, tx_ring->tx_max_header_size);
  1660. header_len = push_len;
  1661. push_hdr = skb->data;
  1662. } else {
  1663. push_len = 0;
  1664. header_len = min_t(u32, len, tx_ring->tx_max_header_size);
  1665. push_hdr = NULL;
  1666. }
  1667. netif_dbg(adapter, tx_queued, dev,
  1668. "skb: %p header_buf->vaddr: %p push_len: %d\n", skb,
  1669. push_hdr, push_len);
  1670. if (len > push_len) {
  1671. dma = dma_map_single(tx_ring->dev, skb->data + push_len,
  1672. len - push_len, DMA_TO_DEVICE);
  1673. if (dma_mapping_error(tx_ring->dev, dma))
  1674. goto error_report_dma_error;
  1675. ena_buf->paddr = dma;
  1676. ena_buf->len = len - push_len;
  1677. ena_buf++;
  1678. tx_info->num_of_bufs++;
  1679. }
  1680. last_frag = skb_shinfo(skb)->nr_frags;
  1681. for (i = 0; i < last_frag; i++) {
  1682. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1683. len = skb_frag_size(frag);
  1684. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len,
  1685. DMA_TO_DEVICE);
  1686. if (dma_mapping_error(tx_ring->dev, dma))
  1687. goto error_report_dma_error;
  1688. ena_buf->paddr = dma;
  1689. ena_buf->len = len;
  1690. ena_buf++;
  1691. }
  1692. tx_info->num_of_bufs += last_frag;
  1693. memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
  1694. ena_tx_ctx.ena_bufs = tx_info->bufs;
  1695. ena_tx_ctx.push_header = push_hdr;
  1696. ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
  1697. ena_tx_ctx.req_id = req_id;
  1698. ena_tx_ctx.header_len = header_len;
  1699. /* set flags and meta data */
  1700. ena_tx_csum(&ena_tx_ctx, skb);
  1701. /* prepare the packet's descriptors to dma engine */
  1702. rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq, &ena_tx_ctx,
  1703. &nb_hw_desc);
  1704. if (unlikely(rc)) {
  1705. netif_err(adapter, tx_queued, dev,
  1706. "failed to prepare tx bufs\n");
  1707. u64_stats_update_begin(&tx_ring->syncp);
  1708. tx_ring->tx_stats.queue_stop++;
  1709. tx_ring->tx_stats.prepare_ctx_err++;
  1710. u64_stats_update_end(&tx_ring->syncp);
  1711. netif_tx_stop_queue(txq);
  1712. goto error_unmap_dma;
  1713. }
  1714. netdev_tx_sent_queue(txq, skb->len);
  1715. u64_stats_update_begin(&tx_ring->syncp);
  1716. tx_ring->tx_stats.cnt++;
  1717. tx_ring->tx_stats.bytes += skb->len;
  1718. u64_stats_update_end(&tx_ring->syncp);
  1719. tx_info->tx_descs = nb_hw_desc;
  1720. tx_info->last_jiffies = jiffies;
  1721. tx_info->print_once = 0;
  1722. tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
  1723. tx_ring->ring_size);
  1724. /* stop the queue when no more space available, the packet can have up
  1725. * to sgl_size + 2. one for the meta descriptor and one for header
  1726. * (if the header is larger than tx_max_header_size).
  1727. */
  1728. if (unlikely(ena_com_sq_empty_space(tx_ring->ena_com_io_sq) <
  1729. (tx_ring->sgl_size + 2))) {
  1730. netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n",
  1731. __func__, qid);
  1732. netif_tx_stop_queue(txq);
  1733. u64_stats_update_begin(&tx_ring->syncp);
  1734. tx_ring->tx_stats.queue_stop++;
  1735. u64_stats_update_end(&tx_ring->syncp);
  1736. /* There is a rare condition where this function decide to
  1737. * stop the queue but meanwhile clean_tx_irq updates
  1738. * next_to_completion and terminates.
  1739. * The queue will remain stopped forever.
  1740. * To solve this issue add a mb() to make sure that
  1741. * netif_tx_stop_queue() write is vissible before checking if
  1742. * there is additional space in the queue.
  1743. */
  1744. smp_mb();
  1745. if (ena_com_sq_empty_space(tx_ring->ena_com_io_sq)
  1746. > ENA_TX_WAKEUP_THRESH) {
  1747. netif_tx_wake_queue(txq);
  1748. u64_stats_update_begin(&tx_ring->syncp);
  1749. tx_ring->tx_stats.queue_wakeup++;
  1750. u64_stats_update_end(&tx_ring->syncp);
  1751. }
  1752. }
  1753. if (netif_xmit_stopped(txq) || !skb->xmit_more) {
  1754. /* trigger the dma engine. ena_com_write_sq_doorbell()
  1755. * has a mb
  1756. */
  1757. ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
  1758. u64_stats_update_begin(&tx_ring->syncp);
  1759. tx_ring->tx_stats.doorbells++;
  1760. u64_stats_update_end(&tx_ring->syncp);
  1761. }
  1762. return NETDEV_TX_OK;
  1763. error_report_dma_error:
  1764. u64_stats_update_begin(&tx_ring->syncp);
  1765. tx_ring->tx_stats.dma_mapping_err++;
  1766. u64_stats_update_end(&tx_ring->syncp);
  1767. netdev_warn(adapter->netdev, "failed to map skb\n");
  1768. tx_info->skb = NULL;
  1769. error_unmap_dma:
  1770. if (i >= 0) {
  1771. /* save value of frag that failed */
  1772. last_frag = i;
  1773. /* start back at beginning and unmap skb */
  1774. tx_info->skb = NULL;
  1775. ena_buf = tx_info->bufs;
  1776. dma_unmap_single(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
  1777. dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
  1778. /* unmap remaining mapped pages */
  1779. for (i = 0; i < last_frag; i++) {
  1780. ena_buf++;
  1781. dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
  1782. dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
  1783. }
  1784. }
  1785. error_drop_packet:
  1786. dev_kfree_skb(skb);
  1787. return NETDEV_TX_OK;
  1788. }
  1789. static u16 ena_select_queue(struct net_device *dev, struct sk_buff *skb,
  1790. struct net_device *sb_dev,
  1791. select_queue_fallback_t fallback)
  1792. {
  1793. u16 qid;
  1794. /* we suspect that this is good for in--kernel network services that
  1795. * want to loop incoming skb rx to tx in normal user generated traffic,
  1796. * most probably we will not get to this
  1797. */
  1798. if (skb_rx_queue_recorded(skb))
  1799. qid = skb_get_rx_queue(skb);
  1800. else
  1801. qid = fallback(dev, skb, NULL);
  1802. return qid;
  1803. }
  1804. static void ena_config_host_info(struct ena_com_dev *ena_dev)
  1805. {
  1806. struct ena_admin_host_info *host_info;
  1807. int rc;
  1808. /* Allocate only the host info */
  1809. rc = ena_com_allocate_host_info(ena_dev);
  1810. if (rc) {
  1811. pr_err("Cannot allocate host info\n");
  1812. return;
  1813. }
  1814. host_info = ena_dev->host_attr.host_info;
  1815. host_info->os_type = ENA_ADMIN_OS_LINUX;
  1816. host_info->kernel_ver = LINUX_VERSION_CODE;
  1817. strlcpy(host_info->kernel_ver_str, utsname()->version,
  1818. sizeof(host_info->kernel_ver_str) - 1);
  1819. host_info->os_dist = 0;
  1820. strncpy(host_info->os_dist_str, utsname()->release,
  1821. sizeof(host_info->os_dist_str) - 1);
  1822. host_info->driver_version =
  1823. (DRV_MODULE_VER_MAJOR) |
  1824. (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
  1825. (DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
  1826. rc = ena_com_set_host_attributes(ena_dev);
  1827. if (rc) {
  1828. if (rc == -EOPNOTSUPP)
  1829. pr_warn("Cannot set host attributes\n");
  1830. else
  1831. pr_err("Cannot set host attributes\n");
  1832. goto err;
  1833. }
  1834. return;
  1835. err:
  1836. ena_com_delete_host_info(ena_dev);
  1837. }
  1838. static void ena_config_debug_area(struct ena_adapter *adapter)
  1839. {
  1840. u32 debug_area_size;
  1841. int rc, ss_count;
  1842. ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS);
  1843. if (ss_count <= 0) {
  1844. netif_err(adapter, drv, adapter->netdev,
  1845. "SS count is negative\n");
  1846. return;
  1847. }
  1848. /* allocate 32 bytes for each string and 64bit for the value */
  1849. debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
  1850. rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size);
  1851. if (rc) {
  1852. pr_err("Cannot allocate debug area\n");
  1853. return;
  1854. }
  1855. rc = ena_com_set_host_attributes(adapter->ena_dev);
  1856. if (rc) {
  1857. if (rc == -EOPNOTSUPP)
  1858. netif_warn(adapter, drv, adapter->netdev,
  1859. "Cannot set host attributes\n");
  1860. else
  1861. netif_err(adapter, drv, adapter->netdev,
  1862. "Cannot set host attributes\n");
  1863. goto err;
  1864. }
  1865. return;
  1866. err:
  1867. ena_com_delete_debug_area(adapter->ena_dev);
  1868. }
  1869. static void ena_get_stats64(struct net_device *netdev,
  1870. struct rtnl_link_stats64 *stats)
  1871. {
  1872. struct ena_adapter *adapter = netdev_priv(netdev);
  1873. struct ena_ring *rx_ring, *tx_ring;
  1874. unsigned int start;
  1875. u64 rx_drops;
  1876. int i;
  1877. if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  1878. return;
  1879. for (i = 0; i < adapter->num_queues; i++) {
  1880. u64 bytes, packets;
  1881. tx_ring = &adapter->tx_ring[i];
  1882. do {
  1883. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  1884. packets = tx_ring->tx_stats.cnt;
  1885. bytes = tx_ring->tx_stats.bytes;
  1886. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  1887. stats->tx_packets += packets;
  1888. stats->tx_bytes += bytes;
  1889. rx_ring = &adapter->rx_ring[i];
  1890. do {
  1891. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  1892. packets = rx_ring->rx_stats.cnt;
  1893. bytes = rx_ring->rx_stats.bytes;
  1894. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  1895. stats->rx_packets += packets;
  1896. stats->rx_bytes += bytes;
  1897. }
  1898. do {
  1899. start = u64_stats_fetch_begin_irq(&adapter->syncp);
  1900. rx_drops = adapter->dev_stats.rx_drops;
  1901. } while (u64_stats_fetch_retry_irq(&adapter->syncp, start));
  1902. stats->rx_dropped = rx_drops;
  1903. stats->multicast = 0;
  1904. stats->collisions = 0;
  1905. stats->rx_length_errors = 0;
  1906. stats->rx_crc_errors = 0;
  1907. stats->rx_frame_errors = 0;
  1908. stats->rx_fifo_errors = 0;
  1909. stats->rx_missed_errors = 0;
  1910. stats->tx_window_errors = 0;
  1911. stats->rx_errors = 0;
  1912. stats->tx_errors = 0;
  1913. }
  1914. static const struct net_device_ops ena_netdev_ops = {
  1915. .ndo_open = ena_open,
  1916. .ndo_stop = ena_close,
  1917. .ndo_start_xmit = ena_start_xmit,
  1918. .ndo_select_queue = ena_select_queue,
  1919. .ndo_get_stats64 = ena_get_stats64,
  1920. .ndo_tx_timeout = ena_tx_timeout,
  1921. .ndo_change_mtu = ena_change_mtu,
  1922. .ndo_set_mac_address = NULL,
  1923. .ndo_validate_addr = eth_validate_addr,
  1924. };
  1925. static int ena_device_validate_params(struct ena_adapter *adapter,
  1926. struct ena_com_dev_get_features_ctx *get_feat_ctx)
  1927. {
  1928. struct net_device *netdev = adapter->netdev;
  1929. int rc;
  1930. rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr,
  1931. adapter->mac_addr);
  1932. if (!rc) {
  1933. netif_err(adapter, drv, netdev,
  1934. "Error, mac address are different\n");
  1935. return -EINVAL;
  1936. }
  1937. if ((get_feat_ctx->max_queues.max_cq_num < adapter->num_queues) ||
  1938. (get_feat_ctx->max_queues.max_sq_num < adapter->num_queues)) {
  1939. netif_err(adapter, drv, netdev,
  1940. "Error, device doesn't support enough queues\n");
  1941. return -EINVAL;
  1942. }
  1943. if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) {
  1944. netif_err(adapter, drv, netdev,
  1945. "Error, device max mtu is smaller than netdev MTU\n");
  1946. return -EINVAL;
  1947. }
  1948. return 0;
  1949. }
  1950. static int ena_device_init(struct ena_com_dev *ena_dev, struct pci_dev *pdev,
  1951. struct ena_com_dev_get_features_ctx *get_feat_ctx,
  1952. bool *wd_state)
  1953. {
  1954. struct device *dev = &pdev->dev;
  1955. bool readless_supported;
  1956. u32 aenq_groups;
  1957. int dma_width;
  1958. int rc;
  1959. rc = ena_com_mmio_reg_read_request_init(ena_dev);
  1960. if (rc) {
  1961. dev_err(dev, "failed to init mmio read less\n");
  1962. return rc;
  1963. }
  1964. /* The PCIe configuration space revision id indicate if mmio reg
  1965. * read is disabled
  1966. */
  1967. readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ);
  1968. ena_com_set_mmio_read_mode(ena_dev, readless_supported);
  1969. rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
  1970. if (rc) {
  1971. dev_err(dev, "Can not reset device\n");
  1972. goto err_mmio_read_less;
  1973. }
  1974. rc = ena_com_validate_version(ena_dev);
  1975. if (rc) {
  1976. dev_err(dev, "device version is too low\n");
  1977. goto err_mmio_read_less;
  1978. }
  1979. dma_width = ena_com_get_dma_width(ena_dev);
  1980. if (dma_width < 0) {
  1981. dev_err(dev, "Invalid dma width value %d", dma_width);
  1982. rc = dma_width;
  1983. goto err_mmio_read_less;
  1984. }
  1985. rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_width));
  1986. if (rc) {
  1987. dev_err(dev, "dma_set_mask_and_coherent failed %d\n", rc);
  1988. goto err_mmio_read_less;
  1989. }
  1990. /* ENA admin level init */
  1991. rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
  1992. if (rc) {
  1993. dev_err(dev,
  1994. "Can not initialize ena admin queue with device\n");
  1995. goto err_mmio_read_less;
  1996. }
  1997. /* To enable the msix interrupts the driver needs to know the number
  1998. * of queues. So the driver uses polling mode to retrieve this
  1999. * information
  2000. */
  2001. ena_com_set_admin_polling_mode(ena_dev, true);
  2002. ena_config_host_info(ena_dev);
  2003. /* Get Device Attributes*/
  2004. rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
  2005. if (rc) {
  2006. dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc);
  2007. goto err_admin_init;
  2008. }
  2009. /* Try to turn all the available aenq groups */
  2010. aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
  2011. BIT(ENA_ADMIN_FATAL_ERROR) |
  2012. BIT(ENA_ADMIN_WARNING) |
  2013. BIT(ENA_ADMIN_NOTIFICATION) |
  2014. BIT(ENA_ADMIN_KEEP_ALIVE);
  2015. aenq_groups &= get_feat_ctx->aenq.supported_groups;
  2016. rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
  2017. if (rc) {
  2018. dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc);
  2019. goto err_admin_init;
  2020. }
  2021. *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
  2022. return 0;
  2023. err_admin_init:
  2024. ena_com_delete_host_info(ena_dev);
  2025. ena_com_admin_destroy(ena_dev);
  2026. err_mmio_read_less:
  2027. ena_com_mmio_reg_read_request_destroy(ena_dev);
  2028. return rc;
  2029. }
  2030. static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
  2031. int io_vectors)
  2032. {
  2033. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2034. struct device *dev = &adapter->pdev->dev;
  2035. int rc;
  2036. rc = ena_enable_msix(adapter, io_vectors);
  2037. if (rc) {
  2038. dev_err(dev, "Can not reserve msix vectors\n");
  2039. return rc;
  2040. }
  2041. ena_setup_mgmnt_intr(adapter);
  2042. rc = ena_request_mgmnt_irq(adapter);
  2043. if (rc) {
  2044. dev_err(dev, "Can not setup management interrupts\n");
  2045. goto err_disable_msix;
  2046. }
  2047. ena_com_set_admin_polling_mode(ena_dev, false);
  2048. ena_com_admin_aenq_enable(ena_dev);
  2049. return 0;
  2050. err_disable_msix:
  2051. ena_disable_msix(adapter);
  2052. return rc;
  2053. }
  2054. static void ena_destroy_device(struct ena_adapter *adapter, bool graceful)
  2055. {
  2056. struct net_device *netdev = adapter->netdev;
  2057. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2058. bool dev_up;
  2059. if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
  2060. return;
  2061. netif_carrier_off(netdev);
  2062. del_timer_sync(&adapter->timer_service);
  2063. dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
  2064. adapter->dev_up_before_reset = dev_up;
  2065. if (!graceful)
  2066. ena_com_set_admin_running_state(ena_dev, false);
  2067. if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  2068. ena_down(adapter);
  2069. /* Before releasing the ENA resources, a device reset is required.
  2070. * (to prevent the device from accessing them).
  2071. * In case the reset flag is set and the device is up, ena_down()
  2072. * already perform the reset, so it can be skipped.
  2073. */
  2074. if (!(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags) && dev_up))
  2075. ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
  2076. ena_free_mgmnt_irq(adapter);
  2077. ena_disable_msix(adapter);
  2078. ena_com_abort_admin_commands(ena_dev);
  2079. ena_com_wait_for_abort_completion(ena_dev);
  2080. ena_com_admin_destroy(ena_dev);
  2081. ena_com_mmio_reg_read_request_destroy(ena_dev);
  2082. adapter->reset_reason = ENA_REGS_RESET_NORMAL;
  2083. clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2084. clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  2085. }
  2086. static int ena_restore_device(struct ena_adapter *adapter)
  2087. {
  2088. struct ena_com_dev_get_features_ctx get_feat_ctx;
  2089. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2090. struct pci_dev *pdev = adapter->pdev;
  2091. bool wd_state;
  2092. int rc;
  2093. set_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
  2094. rc = ena_device_init(ena_dev, adapter->pdev, &get_feat_ctx, &wd_state);
  2095. if (rc) {
  2096. dev_err(&pdev->dev, "Can not initialize device\n");
  2097. goto err;
  2098. }
  2099. adapter->wd_state = wd_state;
  2100. rc = ena_device_validate_params(adapter, &get_feat_ctx);
  2101. if (rc) {
  2102. dev_err(&pdev->dev, "Validation of device parameters failed\n");
  2103. goto err_device_destroy;
  2104. }
  2105. rc = ena_enable_msix_and_set_admin_interrupts(adapter,
  2106. adapter->num_queues);
  2107. if (rc) {
  2108. dev_err(&pdev->dev, "Enable MSI-X failed\n");
  2109. goto err_device_destroy;
  2110. }
  2111. /* If the interface was up before the reset bring it up */
  2112. if (adapter->dev_up_before_reset) {
  2113. rc = ena_up(adapter);
  2114. if (rc) {
  2115. dev_err(&pdev->dev, "Failed to create I/O queues\n");
  2116. goto err_disable_msix;
  2117. }
  2118. }
  2119. set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  2120. clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
  2121. if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
  2122. netif_carrier_on(adapter->netdev);
  2123. mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
  2124. dev_err(&pdev->dev, "Device reset completed successfully\n");
  2125. return rc;
  2126. err_disable_msix:
  2127. ena_free_mgmnt_irq(adapter);
  2128. ena_disable_msix(adapter);
  2129. err_device_destroy:
  2130. ena_com_abort_admin_commands(ena_dev);
  2131. ena_com_wait_for_abort_completion(ena_dev);
  2132. ena_com_admin_destroy(ena_dev);
  2133. ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
  2134. ena_com_mmio_reg_read_request_destroy(ena_dev);
  2135. err:
  2136. clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  2137. clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
  2138. dev_err(&pdev->dev,
  2139. "Reset attempt failed. Can not reset the device\n");
  2140. return rc;
  2141. }
  2142. static void ena_fw_reset_device(struct work_struct *work)
  2143. {
  2144. struct ena_adapter *adapter =
  2145. container_of(work, struct ena_adapter, reset_task);
  2146. rtnl_lock();
  2147. if (likely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  2148. ena_destroy_device(adapter, false);
  2149. ena_restore_device(adapter);
  2150. }
  2151. rtnl_unlock();
  2152. }
  2153. static int check_for_rx_interrupt_queue(struct ena_adapter *adapter,
  2154. struct ena_ring *rx_ring)
  2155. {
  2156. if (likely(rx_ring->first_interrupt))
  2157. return 0;
  2158. if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
  2159. return 0;
  2160. rx_ring->no_interrupt_event_cnt++;
  2161. if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
  2162. netif_err(adapter, rx_err, adapter->netdev,
  2163. "Potential MSIX issue on Rx side Queue = %d. Reset the device\n",
  2164. rx_ring->qid);
  2165. adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
  2166. smp_mb__before_atomic();
  2167. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2168. return -EIO;
  2169. }
  2170. return 0;
  2171. }
  2172. static int check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
  2173. struct ena_ring *tx_ring)
  2174. {
  2175. struct ena_tx_buffer *tx_buf;
  2176. unsigned long last_jiffies;
  2177. u32 missed_tx = 0;
  2178. int i, rc = 0;
  2179. for (i = 0; i < tx_ring->ring_size; i++) {
  2180. tx_buf = &tx_ring->tx_buffer_info[i];
  2181. last_jiffies = tx_buf->last_jiffies;
  2182. if (last_jiffies == 0)
  2183. /* no pending Tx at this location */
  2184. continue;
  2185. if (unlikely(!tx_ring->first_interrupt && time_is_before_jiffies(last_jiffies +
  2186. 2 * adapter->missing_tx_completion_to))) {
  2187. /* If after graceful period interrupt is still not
  2188. * received, we schedule a reset
  2189. */
  2190. netif_err(adapter, tx_err, adapter->netdev,
  2191. "Potential MSIX issue on Tx side Queue = %d. Reset the device\n",
  2192. tx_ring->qid);
  2193. adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
  2194. smp_mb__before_atomic();
  2195. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2196. return -EIO;
  2197. }
  2198. if (unlikely(time_is_before_jiffies(last_jiffies +
  2199. adapter->missing_tx_completion_to))) {
  2200. if (!tx_buf->print_once)
  2201. netif_notice(adapter, tx_err, adapter->netdev,
  2202. "Found a Tx that wasn't completed on time, qid %d, index %d.\n",
  2203. tx_ring->qid, i);
  2204. tx_buf->print_once = 1;
  2205. missed_tx++;
  2206. }
  2207. }
  2208. if (unlikely(missed_tx > adapter->missing_tx_completion_threshold)) {
  2209. netif_err(adapter, tx_err, adapter->netdev,
  2210. "The number of lost tx completions is above the threshold (%d > %d). Reset the device\n",
  2211. missed_tx,
  2212. adapter->missing_tx_completion_threshold);
  2213. adapter->reset_reason =
  2214. ENA_REGS_RESET_MISS_TX_CMPL;
  2215. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2216. rc = -EIO;
  2217. }
  2218. u64_stats_update_begin(&tx_ring->syncp);
  2219. tx_ring->tx_stats.missed_tx += missed_tx;
  2220. u64_stats_update_end(&tx_ring->syncp);
  2221. return rc;
  2222. }
  2223. static void check_for_missing_completions(struct ena_adapter *adapter)
  2224. {
  2225. struct ena_ring *tx_ring;
  2226. struct ena_ring *rx_ring;
  2227. int i, budget, rc;
  2228. /* Make sure the driver doesn't turn the device in other process */
  2229. smp_rmb();
  2230. if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  2231. return;
  2232. if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
  2233. return;
  2234. if (adapter->missing_tx_completion_to == ENA_HW_HINTS_NO_TIMEOUT)
  2235. return;
  2236. budget = ENA_MONITORED_TX_QUEUES;
  2237. for (i = adapter->last_monitored_tx_qid; i < adapter->num_queues; i++) {
  2238. tx_ring = &adapter->tx_ring[i];
  2239. rx_ring = &adapter->rx_ring[i];
  2240. rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
  2241. if (unlikely(rc))
  2242. return;
  2243. rc = check_for_rx_interrupt_queue(adapter, rx_ring);
  2244. if (unlikely(rc))
  2245. return;
  2246. budget--;
  2247. if (!budget)
  2248. break;
  2249. }
  2250. adapter->last_monitored_tx_qid = i % adapter->num_queues;
  2251. }
  2252. /* trigger napi schedule after 2 consecutive detections */
  2253. #define EMPTY_RX_REFILL 2
  2254. /* For the rare case where the device runs out of Rx descriptors and the
  2255. * napi handler failed to refill new Rx descriptors (due to a lack of memory
  2256. * for example).
  2257. * This case will lead to a deadlock:
  2258. * The device won't send interrupts since all the new Rx packets will be dropped
  2259. * The napi handler won't allocate new Rx descriptors so the device will be
  2260. * able to send new packets.
  2261. *
  2262. * This scenario can happen when the kernel's vm.min_free_kbytes is too small.
  2263. * It is recommended to have at least 512MB, with a minimum of 128MB for
  2264. * constrained environment).
  2265. *
  2266. * When such a situation is detected - Reschedule napi
  2267. */
  2268. static void check_for_empty_rx_ring(struct ena_adapter *adapter)
  2269. {
  2270. struct ena_ring *rx_ring;
  2271. int i, refill_required;
  2272. if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
  2273. return;
  2274. if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
  2275. return;
  2276. for (i = 0; i < adapter->num_queues; i++) {
  2277. rx_ring = &adapter->rx_ring[i];
  2278. refill_required =
  2279. ena_com_sq_empty_space(rx_ring->ena_com_io_sq);
  2280. if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
  2281. rx_ring->empty_rx_queue++;
  2282. if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) {
  2283. u64_stats_update_begin(&rx_ring->syncp);
  2284. rx_ring->rx_stats.empty_rx_ring++;
  2285. u64_stats_update_end(&rx_ring->syncp);
  2286. netif_err(adapter, drv, adapter->netdev,
  2287. "trigger refill for ring %d\n", i);
  2288. napi_schedule(rx_ring->napi);
  2289. rx_ring->empty_rx_queue = 0;
  2290. }
  2291. } else {
  2292. rx_ring->empty_rx_queue = 0;
  2293. }
  2294. }
  2295. }
  2296. /* Check for keep alive expiration */
  2297. static void check_for_missing_keep_alive(struct ena_adapter *adapter)
  2298. {
  2299. unsigned long keep_alive_expired;
  2300. if (!adapter->wd_state)
  2301. return;
  2302. if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
  2303. return;
  2304. keep_alive_expired = adapter->last_keep_alive_jiffies +
  2305. adapter->keep_alive_timeout;
  2306. if (unlikely(time_is_before_jiffies(keep_alive_expired))) {
  2307. netif_err(adapter, drv, adapter->netdev,
  2308. "Keep alive watchdog timeout.\n");
  2309. u64_stats_update_begin(&adapter->syncp);
  2310. adapter->dev_stats.wd_expired++;
  2311. u64_stats_update_end(&adapter->syncp);
  2312. adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
  2313. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2314. }
  2315. }
  2316. static void check_for_admin_com_state(struct ena_adapter *adapter)
  2317. {
  2318. if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
  2319. netif_err(adapter, drv, adapter->netdev,
  2320. "ENA admin queue is not in running state!\n");
  2321. u64_stats_update_begin(&adapter->syncp);
  2322. adapter->dev_stats.admin_q_pause++;
  2323. u64_stats_update_end(&adapter->syncp);
  2324. adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
  2325. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2326. }
  2327. }
  2328. static void ena_update_hints(struct ena_adapter *adapter,
  2329. struct ena_admin_ena_hw_hints *hints)
  2330. {
  2331. struct net_device *netdev = adapter->netdev;
  2332. if (hints->admin_completion_tx_timeout)
  2333. adapter->ena_dev->admin_queue.completion_timeout =
  2334. hints->admin_completion_tx_timeout * 1000;
  2335. if (hints->mmio_read_timeout)
  2336. /* convert to usec */
  2337. adapter->ena_dev->mmio_read.reg_read_to =
  2338. hints->mmio_read_timeout * 1000;
  2339. if (hints->missed_tx_completion_count_threshold_to_reset)
  2340. adapter->missing_tx_completion_threshold =
  2341. hints->missed_tx_completion_count_threshold_to_reset;
  2342. if (hints->missing_tx_completion_timeout) {
  2343. if (hints->missing_tx_completion_timeout == ENA_HW_HINTS_NO_TIMEOUT)
  2344. adapter->missing_tx_completion_to = ENA_HW_HINTS_NO_TIMEOUT;
  2345. else
  2346. adapter->missing_tx_completion_to =
  2347. msecs_to_jiffies(hints->missing_tx_completion_timeout);
  2348. }
  2349. if (hints->netdev_wd_timeout)
  2350. netdev->watchdog_timeo = msecs_to_jiffies(hints->netdev_wd_timeout);
  2351. if (hints->driver_watchdog_timeout) {
  2352. if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
  2353. adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
  2354. else
  2355. adapter->keep_alive_timeout =
  2356. msecs_to_jiffies(hints->driver_watchdog_timeout);
  2357. }
  2358. }
  2359. static void ena_update_host_info(struct ena_admin_host_info *host_info,
  2360. struct net_device *netdev)
  2361. {
  2362. host_info->supported_network_features[0] =
  2363. netdev->features & GENMASK_ULL(31, 0);
  2364. host_info->supported_network_features[1] =
  2365. (netdev->features & GENMASK_ULL(63, 32)) >> 32;
  2366. }
  2367. static void ena_timer_service(struct timer_list *t)
  2368. {
  2369. struct ena_adapter *adapter = from_timer(adapter, t, timer_service);
  2370. u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr;
  2371. struct ena_admin_host_info *host_info =
  2372. adapter->ena_dev->host_attr.host_info;
  2373. check_for_missing_keep_alive(adapter);
  2374. check_for_admin_com_state(adapter);
  2375. check_for_missing_completions(adapter);
  2376. check_for_empty_rx_ring(adapter);
  2377. if (debug_area)
  2378. ena_dump_stats_to_buf(adapter, debug_area);
  2379. if (host_info)
  2380. ena_update_host_info(host_info, adapter->netdev);
  2381. if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  2382. netif_err(adapter, drv, adapter->netdev,
  2383. "Trigger reset is on\n");
  2384. ena_dump_stats_to_dmesg(adapter);
  2385. queue_work(ena_wq, &adapter->reset_task);
  2386. return;
  2387. }
  2388. /* Reset the timer */
  2389. mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
  2390. }
  2391. static int ena_calc_io_queue_num(struct pci_dev *pdev,
  2392. struct ena_com_dev *ena_dev,
  2393. struct ena_com_dev_get_features_ctx *get_feat_ctx)
  2394. {
  2395. int io_sq_num, io_queue_num;
  2396. /* In case of LLQ use the llq number in the get feature cmd */
  2397. if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
  2398. io_sq_num = get_feat_ctx->max_queues.max_llq_num;
  2399. if (io_sq_num == 0) {
  2400. dev_err(&pdev->dev,
  2401. "Trying to use LLQ but llq_num is 0. Fall back into regular queues\n");
  2402. ena_dev->tx_mem_queue_type =
  2403. ENA_ADMIN_PLACEMENT_POLICY_HOST;
  2404. io_sq_num = get_feat_ctx->max_queues.max_sq_num;
  2405. }
  2406. } else {
  2407. io_sq_num = get_feat_ctx->max_queues.max_sq_num;
  2408. }
  2409. io_queue_num = min_t(int, num_online_cpus(), ENA_MAX_NUM_IO_QUEUES);
  2410. io_queue_num = min_t(int, io_queue_num, io_sq_num);
  2411. io_queue_num = min_t(int, io_queue_num,
  2412. get_feat_ctx->max_queues.max_cq_num);
  2413. /* 1 IRQ for for mgmnt and 1 IRQs for each IO direction */
  2414. io_queue_num = min_t(int, io_queue_num, pci_msix_vec_count(pdev) - 1);
  2415. if (unlikely(!io_queue_num)) {
  2416. dev_err(&pdev->dev, "The device doesn't have io queues\n");
  2417. return -EFAULT;
  2418. }
  2419. return io_queue_num;
  2420. }
  2421. static void ena_set_push_mode(struct pci_dev *pdev, struct ena_com_dev *ena_dev,
  2422. struct ena_com_dev_get_features_ctx *get_feat_ctx)
  2423. {
  2424. bool has_mem_bar;
  2425. has_mem_bar = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(ENA_MEM_BAR);
  2426. /* Enable push mode if device supports LLQ */
  2427. if (has_mem_bar && (get_feat_ctx->max_queues.max_llq_num > 0))
  2428. ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_DEV;
  2429. else
  2430. ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
  2431. }
  2432. static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat,
  2433. struct net_device *netdev)
  2434. {
  2435. netdev_features_t dev_features = 0;
  2436. /* Set offload features */
  2437. if (feat->offload.tx &
  2438. ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
  2439. dev_features |= NETIF_F_IP_CSUM;
  2440. if (feat->offload.tx &
  2441. ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)
  2442. dev_features |= NETIF_F_IPV6_CSUM;
  2443. if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
  2444. dev_features |= NETIF_F_TSO;
  2445. if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
  2446. dev_features |= NETIF_F_TSO6;
  2447. if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK)
  2448. dev_features |= NETIF_F_TSO_ECN;
  2449. if (feat->offload.rx_supported &
  2450. ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
  2451. dev_features |= NETIF_F_RXCSUM;
  2452. if (feat->offload.rx_supported &
  2453. ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
  2454. dev_features |= NETIF_F_RXCSUM;
  2455. netdev->features =
  2456. dev_features |
  2457. NETIF_F_SG |
  2458. NETIF_F_RXHASH |
  2459. NETIF_F_HIGHDMA;
  2460. netdev->hw_features |= netdev->features;
  2461. netdev->vlan_features |= netdev->features;
  2462. }
  2463. static void ena_set_conf_feat_params(struct ena_adapter *adapter,
  2464. struct ena_com_dev_get_features_ctx *feat)
  2465. {
  2466. struct net_device *netdev = adapter->netdev;
  2467. /* Copy mac address */
  2468. if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) {
  2469. eth_hw_addr_random(netdev);
  2470. ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
  2471. } else {
  2472. ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr);
  2473. ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
  2474. }
  2475. /* Set offload features */
  2476. ena_set_dev_offloads(feat, netdev);
  2477. adapter->max_mtu = feat->dev_attr.max_mtu;
  2478. netdev->max_mtu = adapter->max_mtu;
  2479. netdev->min_mtu = ENA_MIN_MTU;
  2480. }
  2481. static int ena_rss_init_default(struct ena_adapter *adapter)
  2482. {
  2483. struct ena_com_dev *ena_dev = adapter->ena_dev;
  2484. struct device *dev = &adapter->pdev->dev;
  2485. int rc, i;
  2486. u32 val;
  2487. rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
  2488. if (unlikely(rc)) {
  2489. dev_err(dev, "Cannot init indirect table\n");
  2490. goto err_rss_init;
  2491. }
  2492. for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
  2493. val = ethtool_rxfh_indir_default(i, adapter->num_queues);
  2494. rc = ena_com_indirect_table_fill_entry(ena_dev, i,
  2495. ENA_IO_RXQ_IDX(val));
  2496. if (unlikely(rc && (rc != -EOPNOTSUPP))) {
  2497. dev_err(dev, "Cannot fill indirect table\n");
  2498. goto err_fill_indir;
  2499. }
  2500. }
  2501. rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
  2502. ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
  2503. if (unlikely(rc && (rc != -EOPNOTSUPP))) {
  2504. dev_err(dev, "Cannot fill hash function\n");
  2505. goto err_fill_indir;
  2506. }
  2507. rc = ena_com_set_default_hash_ctrl(ena_dev);
  2508. if (unlikely(rc && (rc != -EOPNOTSUPP))) {
  2509. dev_err(dev, "Cannot fill hash control\n");
  2510. goto err_fill_indir;
  2511. }
  2512. return 0;
  2513. err_fill_indir:
  2514. ena_com_rss_destroy(ena_dev);
  2515. err_rss_init:
  2516. return rc;
  2517. }
  2518. static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
  2519. {
  2520. int release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
  2521. pci_release_selected_regions(pdev, release_bars);
  2522. }
  2523. static int ena_calc_queue_size(struct pci_dev *pdev,
  2524. struct ena_com_dev *ena_dev,
  2525. u16 *max_tx_sgl_size,
  2526. u16 *max_rx_sgl_size,
  2527. struct ena_com_dev_get_features_ctx *get_feat_ctx)
  2528. {
  2529. u32 queue_size = ENA_DEFAULT_RING_SIZE;
  2530. queue_size = min_t(u32, queue_size,
  2531. get_feat_ctx->max_queues.max_cq_depth);
  2532. queue_size = min_t(u32, queue_size,
  2533. get_feat_ctx->max_queues.max_sq_depth);
  2534. if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
  2535. queue_size = min_t(u32, queue_size,
  2536. get_feat_ctx->max_queues.max_llq_depth);
  2537. queue_size = rounddown_pow_of_two(queue_size);
  2538. if (unlikely(!queue_size)) {
  2539. dev_err(&pdev->dev, "Invalid queue size\n");
  2540. return -EFAULT;
  2541. }
  2542. *max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
  2543. get_feat_ctx->max_queues.max_packet_tx_descs);
  2544. *max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
  2545. get_feat_ctx->max_queues.max_packet_rx_descs);
  2546. return queue_size;
  2547. }
  2548. /* ena_probe - Device Initialization Routine
  2549. * @pdev: PCI device information struct
  2550. * @ent: entry in ena_pci_tbl
  2551. *
  2552. * Returns 0 on success, negative on failure
  2553. *
  2554. * ena_probe initializes an adapter identified by a pci_dev structure.
  2555. * The OS initialization, configuring of the adapter private structure,
  2556. * and a hardware reset occur.
  2557. */
  2558. static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2559. {
  2560. struct ena_com_dev_get_features_ctx get_feat_ctx;
  2561. static int version_printed;
  2562. struct net_device *netdev;
  2563. struct ena_adapter *adapter;
  2564. struct ena_com_dev *ena_dev = NULL;
  2565. static int adapters_found;
  2566. int io_queue_num, bars, rc;
  2567. int queue_size;
  2568. u16 tx_sgl_size = 0;
  2569. u16 rx_sgl_size = 0;
  2570. bool wd_state;
  2571. dev_dbg(&pdev->dev, "%s\n", __func__);
  2572. if (version_printed++ == 0)
  2573. dev_info(&pdev->dev, "%s", version);
  2574. rc = pci_enable_device_mem(pdev);
  2575. if (rc) {
  2576. dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
  2577. return rc;
  2578. }
  2579. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(ENA_MAX_PHYS_ADDR_SIZE_BITS));
  2580. if (rc) {
  2581. dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", rc);
  2582. goto err_disable_device;
  2583. }
  2584. pci_set_master(pdev);
  2585. ena_dev = vzalloc(sizeof(*ena_dev));
  2586. if (!ena_dev) {
  2587. rc = -ENOMEM;
  2588. goto err_disable_device;
  2589. }
  2590. bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
  2591. rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
  2592. if (rc) {
  2593. dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
  2594. rc);
  2595. goto err_free_ena_dev;
  2596. }
  2597. ena_dev->reg_bar = devm_ioremap(&pdev->dev,
  2598. pci_resource_start(pdev, ENA_REG_BAR),
  2599. pci_resource_len(pdev, ENA_REG_BAR));
  2600. if (!ena_dev->reg_bar) {
  2601. dev_err(&pdev->dev, "failed to remap regs bar\n");
  2602. rc = -EFAULT;
  2603. goto err_free_region;
  2604. }
  2605. ena_dev->dmadev = &pdev->dev;
  2606. rc = ena_device_init(ena_dev, pdev, &get_feat_ctx, &wd_state);
  2607. if (rc) {
  2608. dev_err(&pdev->dev, "ena device init failed\n");
  2609. if (rc == -ETIME)
  2610. rc = -EPROBE_DEFER;
  2611. goto err_free_region;
  2612. }
  2613. ena_set_push_mode(pdev, ena_dev, &get_feat_ctx);
  2614. if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
  2615. ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev,
  2616. pci_resource_start(pdev, ENA_MEM_BAR),
  2617. pci_resource_len(pdev, ENA_MEM_BAR));
  2618. if (!ena_dev->mem_bar) {
  2619. rc = -EFAULT;
  2620. goto err_device_destroy;
  2621. }
  2622. }
  2623. /* initial Tx interrupt delay, Assumes 1 usec granularity.
  2624. * Updated during device initialization with the real granularity
  2625. */
  2626. ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS;
  2627. io_queue_num = ena_calc_io_queue_num(pdev, ena_dev, &get_feat_ctx);
  2628. queue_size = ena_calc_queue_size(pdev, ena_dev, &tx_sgl_size,
  2629. &rx_sgl_size, &get_feat_ctx);
  2630. if ((queue_size <= 0) || (io_queue_num <= 0)) {
  2631. rc = -EFAULT;
  2632. goto err_device_destroy;
  2633. }
  2634. dev_info(&pdev->dev, "creating %d io queues. queue size: %d\n",
  2635. io_queue_num, queue_size);
  2636. /* dev zeroed in init_etherdev */
  2637. netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), io_queue_num);
  2638. if (!netdev) {
  2639. dev_err(&pdev->dev, "alloc_etherdev_mq failed\n");
  2640. rc = -ENOMEM;
  2641. goto err_device_destroy;
  2642. }
  2643. SET_NETDEV_DEV(netdev, &pdev->dev);
  2644. adapter = netdev_priv(netdev);
  2645. pci_set_drvdata(pdev, adapter);
  2646. adapter->ena_dev = ena_dev;
  2647. adapter->netdev = netdev;
  2648. adapter->pdev = pdev;
  2649. ena_set_conf_feat_params(adapter, &get_feat_ctx);
  2650. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2651. adapter->reset_reason = ENA_REGS_RESET_NORMAL;
  2652. adapter->tx_ring_size = queue_size;
  2653. adapter->rx_ring_size = queue_size;
  2654. adapter->max_tx_sgl_size = tx_sgl_size;
  2655. adapter->max_rx_sgl_size = rx_sgl_size;
  2656. adapter->num_queues = io_queue_num;
  2657. adapter->last_monitored_tx_qid = 0;
  2658. adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK;
  2659. adapter->wd_state = wd_state;
  2660. snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found);
  2661. rc = ena_com_init_interrupt_moderation(adapter->ena_dev);
  2662. if (rc) {
  2663. dev_err(&pdev->dev,
  2664. "Failed to query interrupt moderation feature\n");
  2665. goto err_netdev_destroy;
  2666. }
  2667. ena_init_io_rings(adapter);
  2668. netdev->netdev_ops = &ena_netdev_ops;
  2669. netdev->watchdog_timeo = TX_TIMEOUT;
  2670. ena_set_ethtool_ops(netdev);
  2671. netdev->priv_flags |= IFF_UNICAST_FLT;
  2672. u64_stats_init(&adapter->syncp);
  2673. rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
  2674. if (rc) {
  2675. dev_err(&pdev->dev,
  2676. "Failed to enable and set the admin interrupts\n");
  2677. goto err_worker_destroy;
  2678. }
  2679. rc = ena_rss_init_default(adapter);
  2680. if (rc && (rc != -EOPNOTSUPP)) {
  2681. dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc);
  2682. goto err_free_msix;
  2683. }
  2684. ena_config_debug_area(adapter);
  2685. memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len);
  2686. netif_carrier_off(netdev);
  2687. rc = register_netdev(netdev);
  2688. if (rc) {
  2689. dev_err(&pdev->dev, "Cannot register net device\n");
  2690. goto err_rss;
  2691. }
  2692. INIT_WORK(&adapter->reset_task, ena_fw_reset_device);
  2693. adapter->last_keep_alive_jiffies = jiffies;
  2694. adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
  2695. adapter->missing_tx_completion_to = TX_TIMEOUT;
  2696. adapter->missing_tx_completion_threshold = MAX_NUM_OF_TIMEOUTED_PACKETS;
  2697. ena_update_hints(adapter, &get_feat_ctx.hw_hints);
  2698. timer_setup(&adapter->timer_service, ena_timer_service, 0);
  2699. mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
  2700. dev_info(&pdev->dev, "%s found at mem %lx, mac addr %pM Queues %d\n",
  2701. DEVICE_NAME, (long)pci_resource_start(pdev, 0),
  2702. netdev->dev_addr, io_queue_num);
  2703. set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
  2704. adapters_found++;
  2705. return 0;
  2706. err_rss:
  2707. ena_com_delete_debug_area(ena_dev);
  2708. ena_com_rss_destroy(ena_dev);
  2709. err_free_msix:
  2710. ena_com_dev_reset(ena_dev, ENA_REGS_RESET_INIT_ERR);
  2711. ena_free_mgmnt_irq(adapter);
  2712. ena_disable_msix(adapter);
  2713. err_worker_destroy:
  2714. ena_com_destroy_interrupt_moderation(ena_dev);
  2715. del_timer(&adapter->timer_service);
  2716. err_netdev_destroy:
  2717. free_netdev(netdev);
  2718. err_device_destroy:
  2719. ena_com_delete_host_info(ena_dev);
  2720. ena_com_admin_destroy(ena_dev);
  2721. err_free_region:
  2722. ena_release_bars(ena_dev, pdev);
  2723. err_free_ena_dev:
  2724. vfree(ena_dev);
  2725. err_disable_device:
  2726. pci_disable_device(pdev);
  2727. return rc;
  2728. }
  2729. /*****************************************************************************/
  2730. /* ena_remove - Device Removal Routine
  2731. * @pdev: PCI device information struct
  2732. *
  2733. * ena_remove is called by the PCI subsystem to alert the driver
  2734. * that it should release a PCI device.
  2735. */
  2736. static void ena_remove(struct pci_dev *pdev)
  2737. {
  2738. struct ena_adapter *adapter = pci_get_drvdata(pdev);
  2739. struct ena_com_dev *ena_dev;
  2740. struct net_device *netdev;
  2741. ena_dev = adapter->ena_dev;
  2742. netdev = adapter->netdev;
  2743. #ifdef CONFIG_RFS_ACCEL
  2744. if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) {
  2745. free_irq_cpu_rmap(netdev->rx_cpu_rmap);
  2746. netdev->rx_cpu_rmap = NULL;
  2747. }
  2748. #endif /* CONFIG_RFS_ACCEL */
  2749. /* Make sure timer and reset routine won't be called after
  2750. * freeing device resources.
  2751. */
  2752. del_timer_sync(&adapter->timer_service);
  2753. cancel_work_sync(&adapter->reset_task);
  2754. unregister_netdev(netdev);
  2755. /* If the device is running then we want to make sure the device will be
  2756. * reset to make sure no more events will be issued by the device.
  2757. */
  2758. if (test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
  2759. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2760. rtnl_lock();
  2761. ena_destroy_device(adapter, true);
  2762. rtnl_unlock();
  2763. free_netdev(netdev);
  2764. ena_com_rss_destroy(ena_dev);
  2765. ena_com_delete_debug_area(ena_dev);
  2766. ena_com_delete_host_info(ena_dev);
  2767. ena_release_bars(ena_dev, pdev);
  2768. pci_disable_device(pdev);
  2769. ena_com_destroy_interrupt_moderation(ena_dev);
  2770. vfree(ena_dev);
  2771. }
  2772. #ifdef CONFIG_PM
  2773. /* ena_suspend - PM suspend callback
  2774. * @pdev: PCI device information struct
  2775. * @state:power state
  2776. */
  2777. static int ena_suspend(struct pci_dev *pdev, pm_message_t state)
  2778. {
  2779. struct ena_adapter *adapter = pci_get_drvdata(pdev);
  2780. u64_stats_update_begin(&adapter->syncp);
  2781. adapter->dev_stats.suspend++;
  2782. u64_stats_update_end(&adapter->syncp);
  2783. rtnl_lock();
  2784. if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
  2785. dev_err(&pdev->dev,
  2786. "ignoring device reset request as the device is being suspended\n");
  2787. clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  2788. }
  2789. ena_destroy_device(adapter, true);
  2790. rtnl_unlock();
  2791. return 0;
  2792. }
  2793. /* ena_resume - PM resume callback
  2794. * @pdev: PCI device information struct
  2795. *
  2796. */
  2797. static int ena_resume(struct pci_dev *pdev)
  2798. {
  2799. struct ena_adapter *adapter = pci_get_drvdata(pdev);
  2800. int rc;
  2801. u64_stats_update_begin(&adapter->syncp);
  2802. adapter->dev_stats.resume++;
  2803. u64_stats_update_end(&adapter->syncp);
  2804. rtnl_lock();
  2805. rc = ena_restore_device(adapter);
  2806. rtnl_unlock();
  2807. return rc;
  2808. }
  2809. #endif
  2810. static struct pci_driver ena_pci_driver = {
  2811. .name = DRV_MODULE_NAME,
  2812. .id_table = ena_pci_tbl,
  2813. .probe = ena_probe,
  2814. .remove = ena_remove,
  2815. #ifdef CONFIG_PM
  2816. .suspend = ena_suspend,
  2817. .resume = ena_resume,
  2818. #endif
  2819. .sriov_configure = pci_sriov_configure_simple,
  2820. };
  2821. static int __init ena_init(void)
  2822. {
  2823. pr_info("%s", version);
  2824. ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME);
  2825. if (!ena_wq) {
  2826. pr_err("Failed to create workqueue\n");
  2827. return -ENOMEM;
  2828. }
  2829. return pci_register_driver(&ena_pci_driver);
  2830. }
  2831. static void __exit ena_cleanup(void)
  2832. {
  2833. pci_unregister_driver(&ena_pci_driver);
  2834. if (ena_wq) {
  2835. destroy_workqueue(ena_wq);
  2836. ena_wq = NULL;
  2837. }
  2838. }
  2839. /******************************************************************************
  2840. ******************************** AENQ Handlers *******************************
  2841. *****************************************************************************/
  2842. /* ena_update_on_link_change:
  2843. * Notify the network interface about the change in link status
  2844. */
  2845. static void ena_update_on_link_change(void *adapter_data,
  2846. struct ena_admin_aenq_entry *aenq_e)
  2847. {
  2848. struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
  2849. struct ena_admin_aenq_link_change_desc *aenq_desc =
  2850. (struct ena_admin_aenq_link_change_desc *)aenq_e;
  2851. int status = aenq_desc->flags &
  2852. ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
  2853. if (status) {
  2854. netdev_dbg(adapter->netdev, "%s\n", __func__);
  2855. set_bit(ENA_FLAG_LINK_UP, &adapter->flags);
  2856. if (!test_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags))
  2857. netif_carrier_on(adapter->netdev);
  2858. } else {
  2859. clear_bit(ENA_FLAG_LINK_UP, &adapter->flags);
  2860. netif_carrier_off(adapter->netdev);
  2861. }
  2862. }
  2863. static void ena_keep_alive_wd(void *adapter_data,
  2864. struct ena_admin_aenq_entry *aenq_e)
  2865. {
  2866. struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
  2867. struct ena_admin_aenq_keep_alive_desc *desc;
  2868. u64 rx_drops;
  2869. desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
  2870. adapter->last_keep_alive_jiffies = jiffies;
  2871. rx_drops = ((u64)desc->rx_drops_high << 32) | desc->rx_drops_low;
  2872. u64_stats_update_begin(&adapter->syncp);
  2873. /* These stats are accumulated by the device, so the counters indicate
  2874. * all drops since last reset.
  2875. */
  2876. adapter->dev_stats.rx_drops = rx_drops;
  2877. u64_stats_update_end(&adapter->syncp);
  2878. }
  2879. static void ena_notification(void *adapter_data,
  2880. struct ena_admin_aenq_entry *aenq_e)
  2881. {
  2882. struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
  2883. struct ena_admin_ena_hw_hints *hints;
  2884. WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
  2885. "Invalid group(%x) expected %x\n",
  2886. aenq_e->aenq_common_desc.group,
  2887. ENA_ADMIN_NOTIFICATION);
  2888. switch (aenq_e->aenq_common_desc.syndrom) {
  2889. case ENA_ADMIN_UPDATE_HINTS:
  2890. hints = (struct ena_admin_ena_hw_hints *)
  2891. (&aenq_e->inline_data_w4);
  2892. ena_update_hints(adapter, hints);
  2893. break;
  2894. default:
  2895. netif_err(adapter, drv, adapter->netdev,
  2896. "Invalid aenq notification link state %d\n",
  2897. aenq_e->aenq_common_desc.syndrom);
  2898. }
  2899. }
  2900. /* This handler will called for unknown event group or unimplemented handlers*/
  2901. static void unimplemented_aenq_handler(void *data,
  2902. struct ena_admin_aenq_entry *aenq_e)
  2903. {
  2904. struct ena_adapter *adapter = (struct ena_adapter *)data;
  2905. netif_err(adapter, drv, adapter->netdev,
  2906. "Unknown event was received or event with unimplemented handler\n");
  2907. }
  2908. static struct ena_aenq_handlers aenq_handlers = {
  2909. .handlers = {
  2910. [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
  2911. [ENA_ADMIN_NOTIFICATION] = ena_notification,
  2912. [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
  2913. },
  2914. .unimplemented_handler = unimplemented_aenq_handler
  2915. };
  2916. module_init(ena_init);
  2917. module_exit(ena_cleanup);