ena_netdev.c 92 KB

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