ena_netdev.c 83 KB

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