ena_netdev.c 85 KB

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