ena_netdev.c 84 KB

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