ef10.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2012-2013 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #include "net_driver.h"
  10. #include "ef10_regs.h"
  11. #include "io.h"
  12. #include "mcdi.h"
  13. #include "mcdi_pcol.h"
  14. #include "nic.h"
  15. #include "workarounds.h"
  16. #include <linux/in.h>
  17. #include <linux/jhash.h>
  18. #include <linux/wait.h>
  19. #include <linux/workqueue.h>
  20. /* Hardware control for EF10 architecture including 'Huntington'. */
  21. #define EFX_EF10_DRVGEN_EV 7
  22. enum {
  23. EFX_EF10_TEST = 1,
  24. EFX_EF10_REFILL,
  25. };
  26. /* The reserved RSS context value */
  27. #define EFX_EF10_RSS_CONTEXT_INVALID 0xffffffff
  28. /* The filter table(s) are managed by firmware and we have write-only
  29. * access. When removing filters we must identify them to the
  30. * firmware by a 64-bit handle, but this is too wide for Linux kernel
  31. * interfaces (32-bit for RX NFC, 16-bit for RFS). Also, we need to
  32. * be able to tell in advance whether a requested insertion will
  33. * replace an existing filter. Therefore we maintain a software hash
  34. * table, which should be at least as large as the hardware hash
  35. * table.
  36. *
  37. * Huntington has a single 8K filter table shared between all filter
  38. * types and both ports.
  39. */
  40. #define HUNT_FILTER_TBL_ROWS 8192
  41. struct efx_ef10_filter_table {
  42. /* The RX match field masks supported by this fw & hw, in order of priority */
  43. enum efx_filter_match_flags rx_match_flags[
  44. MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_MAXNUM];
  45. unsigned int rx_match_count;
  46. struct {
  47. unsigned long spec; /* pointer to spec plus flag bits */
  48. /* BUSY flag indicates that an update is in progress. STACK_OLD is
  49. * used to mark and sweep stack-owned MAC filters.
  50. */
  51. #define EFX_EF10_FILTER_FLAG_BUSY 1UL
  52. #define EFX_EF10_FILTER_FLAG_STACK_OLD 2UL
  53. #define EFX_EF10_FILTER_FLAGS 3UL
  54. u64 handle; /* firmware handle */
  55. } *entry;
  56. wait_queue_head_t waitq;
  57. /* Shadow of net_device address lists, guarded by mac_lock */
  58. #define EFX_EF10_FILTER_STACK_UC_MAX 32
  59. #define EFX_EF10_FILTER_STACK_MC_MAX 256
  60. struct {
  61. u8 addr[ETH_ALEN];
  62. u16 id;
  63. } stack_uc_list[EFX_EF10_FILTER_STACK_UC_MAX],
  64. stack_mc_list[EFX_EF10_FILTER_STACK_MC_MAX];
  65. int stack_uc_count; /* negative for PROMISC */
  66. int stack_mc_count; /* negative for PROMISC/ALLMULTI */
  67. };
  68. /* An arbitrary search limit for the software hash table */
  69. #define EFX_EF10_FILTER_SEARCH_LIMIT 200
  70. static void efx_ef10_rx_push_indir_table(struct efx_nic *efx);
  71. static void efx_ef10_rx_free_indir_table(struct efx_nic *efx);
  72. static void efx_ef10_filter_table_remove(struct efx_nic *efx);
  73. static int efx_ef10_get_warm_boot_count(struct efx_nic *efx)
  74. {
  75. efx_dword_t reg;
  76. efx_readd(efx, &reg, ER_DZ_BIU_MC_SFT_STATUS);
  77. return EFX_DWORD_FIELD(reg, EFX_WORD_1) == 0xb007 ?
  78. EFX_DWORD_FIELD(reg, EFX_WORD_0) : -EIO;
  79. }
  80. static unsigned int efx_ef10_mem_map_size(struct efx_nic *efx)
  81. {
  82. return resource_size(&efx->pci_dev->resource[EFX_MEM_BAR]);
  83. }
  84. static int efx_ef10_init_datapath_caps(struct efx_nic *efx)
  85. {
  86. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CAPABILITIES_OUT_LEN);
  87. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  88. size_t outlen;
  89. int rc;
  90. BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
  91. rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
  92. outbuf, sizeof(outbuf), &outlen);
  93. if (rc)
  94. return rc;
  95. if (outlen < sizeof(outbuf)) {
  96. netif_err(efx, drv, efx->net_dev,
  97. "unable to read datapath firmware capabilities\n");
  98. return -EIO;
  99. }
  100. nic_data->datapath_caps =
  101. MCDI_DWORD(outbuf, GET_CAPABILITIES_OUT_FLAGS1);
  102. if (!(nic_data->datapath_caps &
  103. (1 << MC_CMD_GET_CAPABILITIES_OUT_TX_TSO_LBN))) {
  104. netif_err(efx, drv, efx->net_dev,
  105. "current firmware does not support TSO\n");
  106. return -ENODEV;
  107. }
  108. if (!(nic_data->datapath_caps &
  109. (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_PREFIX_LEN_14_LBN))) {
  110. netif_err(efx, probe, efx->net_dev,
  111. "current firmware does not support an RX prefix\n");
  112. return -ENODEV;
  113. }
  114. return 0;
  115. }
  116. static int efx_ef10_get_sysclk_freq(struct efx_nic *efx)
  117. {
  118. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CLOCK_OUT_LEN);
  119. int rc;
  120. rc = efx_mcdi_rpc(efx, MC_CMD_GET_CLOCK, NULL, 0,
  121. outbuf, sizeof(outbuf), NULL);
  122. if (rc)
  123. return rc;
  124. rc = MCDI_DWORD(outbuf, GET_CLOCK_OUT_SYS_FREQ);
  125. return rc > 0 ? rc : -ERANGE;
  126. }
  127. static int efx_ef10_get_mac_address(struct efx_nic *efx, u8 *mac_address)
  128. {
  129. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_MAC_ADDRESSES_OUT_LEN);
  130. size_t outlen;
  131. int rc;
  132. BUILD_BUG_ON(MC_CMD_GET_MAC_ADDRESSES_IN_LEN != 0);
  133. rc = efx_mcdi_rpc(efx, MC_CMD_GET_MAC_ADDRESSES, NULL, 0,
  134. outbuf, sizeof(outbuf), &outlen);
  135. if (rc)
  136. return rc;
  137. if (outlen < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN)
  138. return -EIO;
  139. memcpy(mac_address,
  140. MCDI_PTR(outbuf, GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE), ETH_ALEN);
  141. return 0;
  142. }
  143. static int efx_ef10_probe(struct efx_nic *efx)
  144. {
  145. struct efx_ef10_nic_data *nic_data;
  146. int i, rc;
  147. /* We can have one VI for each 8K region. However we need
  148. * multiple TX queues per channel.
  149. */
  150. efx->max_channels =
  151. min_t(unsigned int,
  152. EFX_MAX_CHANNELS,
  153. resource_size(&efx->pci_dev->resource[EFX_MEM_BAR]) /
  154. (EFX_VI_PAGE_SIZE * EFX_TXQ_TYPES));
  155. BUG_ON(efx->max_channels == 0);
  156. nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
  157. if (!nic_data)
  158. return -ENOMEM;
  159. efx->nic_data = nic_data;
  160. rc = efx_nic_alloc_buffer(efx, &nic_data->mcdi_buf,
  161. 8 + MCDI_CTL_SDU_LEN_MAX_V2, GFP_KERNEL);
  162. if (rc)
  163. goto fail1;
  164. /* Get the MC's warm boot count. In case it's rebooting right
  165. * now, be prepared to retry.
  166. */
  167. i = 0;
  168. for (;;) {
  169. rc = efx_ef10_get_warm_boot_count(efx);
  170. if (rc >= 0)
  171. break;
  172. if (++i == 5)
  173. goto fail2;
  174. ssleep(1);
  175. }
  176. nic_data->warm_boot_count = rc;
  177. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  178. /* In case we're recovering from a crash (kexec), we want to
  179. * cancel any outstanding request by the previous user of this
  180. * function. We send a special message using the least
  181. * significant bits of the 'high' (doorbell) register.
  182. */
  183. _efx_writed(efx, cpu_to_le32(1), ER_DZ_MC_DB_HWRD);
  184. rc = efx_mcdi_init(efx);
  185. if (rc)
  186. goto fail2;
  187. /* Reset (most) configuration for this function */
  188. rc = efx_mcdi_reset(efx, RESET_TYPE_ALL);
  189. if (rc)
  190. goto fail3;
  191. /* Enable event logging */
  192. rc = efx_mcdi_log_ctrl(efx, true, false, 0);
  193. if (rc)
  194. goto fail3;
  195. rc = efx_ef10_init_datapath_caps(efx);
  196. if (rc < 0)
  197. goto fail3;
  198. efx->rx_packet_len_offset =
  199. ES_DZ_RX_PREFIX_PKTLEN_OFST - ES_DZ_RX_PREFIX_SIZE;
  200. rc = efx_mcdi_port_get_number(efx);
  201. if (rc < 0)
  202. goto fail3;
  203. efx->port_num = rc;
  204. rc = efx_ef10_get_mac_address(efx, efx->net_dev->perm_addr);
  205. if (rc)
  206. goto fail3;
  207. rc = efx_ef10_get_sysclk_freq(efx);
  208. if (rc < 0)
  209. goto fail3;
  210. efx->timer_quantum_ns = 1536000 / rc; /* 1536 cycles */
  211. /* Check whether firmware supports bug 35388 workaround */
  212. rc = efx_mcdi_set_workaround(efx, MC_CMD_WORKAROUND_BUG35388, true);
  213. if (rc == 0)
  214. nic_data->workaround_35388 = true;
  215. else if (rc != -ENOSYS && rc != -ENOENT)
  216. goto fail3;
  217. netif_dbg(efx, probe, efx->net_dev,
  218. "workaround for bug 35388 is %sabled\n",
  219. nic_data->workaround_35388 ? "en" : "dis");
  220. rc = efx_mcdi_mon_probe(efx);
  221. if (rc)
  222. goto fail3;
  223. return 0;
  224. fail3:
  225. efx_mcdi_fini(efx);
  226. fail2:
  227. efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
  228. fail1:
  229. kfree(nic_data);
  230. efx->nic_data = NULL;
  231. return rc;
  232. }
  233. static int efx_ef10_free_vis(struct efx_nic *efx)
  234. {
  235. int rc = efx_mcdi_rpc(efx, MC_CMD_FREE_VIS, NULL, 0, NULL, 0, NULL);
  236. /* -EALREADY means nothing to free, so ignore */
  237. if (rc == -EALREADY)
  238. rc = 0;
  239. return rc;
  240. }
  241. #ifdef EFX_USE_PIO
  242. static void efx_ef10_free_piobufs(struct efx_nic *efx)
  243. {
  244. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  245. MCDI_DECLARE_BUF(inbuf, MC_CMD_FREE_PIOBUF_IN_LEN);
  246. unsigned int i;
  247. int rc;
  248. BUILD_BUG_ON(MC_CMD_FREE_PIOBUF_OUT_LEN != 0);
  249. for (i = 0; i < nic_data->n_piobufs; i++) {
  250. MCDI_SET_DWORD(inbuf, FREE_PIOBUF_IN_PIOBUF_HANDLE,
  251. nic_data->piobuf_handle[i]);
  252. rc = efx_mcdi_rpc(efx, MC_CMD_FREE_PIOBUF, inbuf, sizeof(inbuf),
  253. NULL, 0, NULL);
  254. WARN_ON(rc);
  255. }
  256. nic_data->n_piobufs = 0;
  257. }
  258. static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
  259. {
  260. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  261. MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_PIOBUF_OUT_LEN);
  262. unsigned int i;
  263. size_t outlen;
  264. int rc = 0;
  265. BUILD_BUG_ON(MC_CMD_ALLOC_PIOBUF_IN_LEN != 0);
  266. for (i = 0; i < n; i++) {
  267. rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_PIOBUF, NULL, 0,
  268. outbuf, sizeof(outbuf), &outlen);
  269. if (rc)
  270. break;
  271. if (outlen < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
  272. rc = -EIO;
  273. break;
  274. }
  275. nic_data->piobuf_handle[i] =
  276. MCDI_DWORD(outbuf, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
  277. netif_dbg(efx, probe, efx->net_dev,
  278. "allocated PIO buffer %u handle %x\n", i,
  279. nic_data->piobuf_handle[i]);
  280. }
  281. nic_data->n_piobufs = i;
  282. if (rc)
  283. efx_ef10_free_piobufs(efx);
  284. return rc;
  285. }
  286. static int efx_ef10_link_piobufs(struct efx_nic *efx)
  287. {
  288. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  289. MCDI_DECLARE_BUF(inbuf,
  290. max(MC_CMD_LINK_PIOBUF_IN_LEN,
  291. MC_CMD_UNLINK_PIOBUF_IN_LEN));
  292. struct efx_channel *channel;
  293. struct efx_tx_queue *tx_queue;
  294. unsigned int offset, index;
  295. int rc;
  296. BUILD_BUG_ON(MC_CMD_LINK_PIOBUF_OUT_LEN != 0);
  297. BUILD_BUG_ON(MC_CMD_UNLINK_PIOBUF_OUT_LEN != 0);
  298. /* Link a buffer to each VI in the write-combining mapping */
  299. for (index = 0; index < nic_data->n_piobufs; ++index) {
  300. MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_PIOBUF_HANDLE,
  301. nic_data->piobuf_handle[index]);
  302. MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_TXQ_INSTANCE,
  303. nic_data->pio_write_vi_base + index);
  304. rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
  305. inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
  306. NULL, 0, NULL);
  307. if (rc) {
  308. netif_err(efx, drv, efx->net_dev,
  309. "failed to link VI %u to PIO buffer %u (%d)\n",
  310. nic_data->pio_write_vi_base + index, index,
  311. rc);
  312. goto fail;
  313. }
  314. netif_dbg(efx, probe, efx->net_dev,
  315. "linked VI %u to PIO buffer %u\n",
  316. nic_data->pio_write_vi_base + index, index);
  317. }
  318. /* Link a buffer to each TX queue */
  319. efx_for_each_channel(channel, efx) {
  320. efx_for_each_channel_tx_queue(tx_queue, channel) {
  321. /* We assign the PIO buffers to queues in
  322. * reverse order to allow for the following
  323. * special case.
  324. */
  325. offset = ((efx->tx_channel_offset + efx->n_tx_channels -
  326. tx_queue->channel->channel - 1) *
  327. efx_piobuf_size);
  328. index = offset / ER_DZ_TX_PIOBUF_SIZE;
  329. offset = offset % ER_DZ_TX_PIOBUF_SIZE;
  330. /* When the host page size is 4K, the first
  331. * host page in the WC mapping may be within
  332. * the same VI page as the last TX queue. We
  333. * can only link one buffer to each VI.
  334. */
  335. if (tx_queue->queue == nic_data->pio_write_vi_base) {
  336. BUG_ON(index != 0);
  337. rc = 0;
  338. } else {
  339. MCDI_SET_DWORD(inbuf,
  340. LINK_PIOBUF_IN_PIOBUF_HANDLE,
  341. nic_data->piobuf_handle[index]);
  342. MCDI_SET_DWORD(inbuf,
  343. LINK_PIOBUF_IN_TXQ_INSTANCE,
  344. tx_queue->queue);
  345. rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
  346. inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
  347. NULL, 0, NULL);
  348. }
  349. if (rc) {
  350. /* This is non-fatal; the TX path just
  351. * won't use PIO for this queue
  352. */
  353. netif_err(efx, drv, efx->net_dev,
  354. "failed to link VI %u to PIO buffer %u (%d)\n",
  355. tx_queue->queue, index, rc);
  356. tx_queue->piobuf = NULL;
  357. } else {
  358. tx_queue->piobuf =
  359. nic_data->pio_write_base +
  360. index * EFX_VI_PAGE_SIZE + offset;
  361. tx_queue->piobuf_offset = offset;
  362. netif_dbg(efx, probe, efx->net_dev,
  363. "linked VI %u to PIO buffer %u offset %x addr %p\n",
  364. tx_queue->queue, index,
  365. tx_queue->piobuf_offset,
  366. tx_queue->piobuf);
  367. }
  368. }
  369. }
  370. return 0;
  371. fail:
  372. while (index--) {
  373. MCDI_SET_DWORD(inbuf, UNLINK_PIOBUF_IN_TXQ_INSTANCE,
  374. nic_data->pio_write_vi_base + index);
  375. efx_mcdi_rpc(efx, MC_CMD_UNLINK_PIOBUF,
  376. inbuf, MC_CMD_UNLINK_PIOBUF_IN_LEN,
  377. NULL, 0, NULL);
  378. }
  379. return rc;
  380. }
  381. #else /* !EFX_USE_PIO */
  382. static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
  383. {
  384. return n == 0 ? 0 : -ENOBUFS;
  385. }
  386. static int efx_ef10_link_piobufs(struct efx_nic *efx)
  387. {
  388. return 0;
  389. }
  390. static void efx_ef10_free_piobufs(struct efx_nic *efx)
  391. {
  392. }
  393. #endif /* EFX_USE_PIO */
  394. static void efx_ef10_remove(struct efx_nic *efx)
  395. {
  396. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  397. int rc;
  398. efx_mcdi_mon_remove(efx);
  399. /* This needs to be after efx_ptp_remove_channel() with no filters */
  400. efx_ef10_rx_free_indir_table(efx);
  401. if (nic_data->wc_membase)
  402. iounmap(nic_data->wc_membase);
  403. rc = efx_ef10_free_vis(efx);
  404. WARN_ON(rc != 0);
  405. if (!nic_data->must_restore_piobufs)
  406. efx_ef10_free_piobufs(efx);
  407. efx_mcdi_fini(efx);
  408. efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
  409. kfree(nic_data);
  410. }
  411. static int efx_ef10_alloc_vis(struct efx_nic *efx,
  412. unsigned int min_vis, unsigned int max_vis)
  413. {
  414. MCDI_DECLARE_BUF(inbuf, MC_CMD_ALLOC_VIS_IN_LEN);
  415. MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_VIS_OUT_LEN);
  416. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  417. size_t outlen;
  418. int rc;
  419. MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MIN_VI_COUNT, min_vis);
  420. MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MAX_VI_COUNT, max_vis);
  421. rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_VIS, inbuf, sizeof(inbuf),
  422. outbuf, sizeof(outbuf), &outlen);
  423. if (rc != 0)
  424. return rc;
  425. if (outlen < MC_CMD_ALLOC_VIS_OUT_LEN)
  426. return -EIO;
  427. netif_dbg(efx, drv, efx->net_dev, "base VI is A0x%03x\n",
  428. MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE));
  429. nic_data->vi_base = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE);
  430. nic_data->n_allocated_vis = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_COUNT);
  431. return 0;
  432. }
  433. /* Note that the failure path of this function does not free
  434. * resources, as this will be done by efx_ef10_remove().
  435. */
  436. static int efx_ef10_dimension_resources(struct efx_nic *efx)
  437. {
  438. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  439. unsigned int uc_mem_map_size, wc_mem_map_size;
  440. unsigned int min_vis, pio_write_vi_base, max_vis;
  441. void __iomem *membase;
  442. int rc;
  443. min_vis = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
  444. #ifdef EFX_USE_PIO
  445. /* Try to allocate PIO buffers if wanted and if the full
  446. * number of PIO buffers would be sufficient to allocate one
  447. * copy-buffer per TX channel. Failure is non-fatal, as there
  448. * are only a small number of PIO buffers shared between all
  449. * functions of the controller.
  450. */
  451. if (efx_piobuf_size != 0 &&
  452. ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size * EF10_TX_PIOBUF_COUNT >=
  453. efx->n_tx_channels) {
  454. unsigned int n_piobufs =
  455. DIV_ROUND_UP(efx->n_tx_channels,
  456. ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size);
  457. rc = efx_ef10_alloc_piobufs(efx, n_piobufs);
  458. if (rc)
  459. netif_err(efx, probe, efx->net_dev,
  460. "failed to allocate PIO buffers (%d)\n", rc);
  461. else
  462. netif_dbg(efx, probe, efx->net_dev,
  463. "allocated %u PIO buffers\n", n_piobufs);
  464. }
  465. #else
  466. nic_data->n_piobufs = 0;
  467. #endif
  468. /* PIO buffers should be mapped with write-combining enabled,
  469. * and we want to make single UC and WC mappings rather than
  470. * several of each (in fact that's the only option if host
  471. * page size is >4K). So we may allocate some extra VIs just
  472. * for writing PIO buffers through.
  473. */
  474. uc_mem_map_size = PAGE_ALIGN((min_vis - 1) * EFX_VI_PAGE_SIZE +
  475. ER_DZ_TX_PIOBUF);
  476. if (nic_data->n_piobufs) {
  477. pio_write_vi_base = uc_mem_map_size / EFX_VI_PAGE_SIZE;
  478. wc_mem_map_size = (PAGE_ALIGN((pio_write_vi_base +
  479. nic_data->n_piobufs) *
  480. EFX_VI_PAGE_SIZE) -
  481. uc_mem_map_size);
  482. max_vis = pio_write_vi_base + nic_data->n_piobufs;
  483. } else {
  484. pio_write_vi_base = 0;
  485. wc_mem_map_size = 0;
  486. max_vis = min_vis;
  487. }
  488. /* In case the last attached driver failed to free VIs, do it now */
  489. rc = efx_ef10_free_vis(efx);
  490. if (rc != 0)
  491. return rc;
  492. rc = efx_ef10_alloc_vis(efx, min_vis, max_vis);
  493. if (rc != 0)
  494. return rc;
  495. /* If we didn't get enough VIs to map all the PIO buffers, free the
  496. * PIO buffers
  497. */
  498. if (nic_data->n_piobufs &&
  499. nic_data->n_allocated_vis <
  500. pio_write_vi_base + nic_data->n_piobufs) {
  501. netif_dbg(efx, probe, efx->net_dev,
  502. "%u VIs are not sufficient to map %u PIO buffers\n",
  503. nic_data->n_allocated_vis, nic_data->n_piobufs);
  504. efx_ef10_free_piobufs(efx);
  505. }
  506. /* Shrink the original UC mapping of the memory BAR */
  507. membase = ioremap_nocache(efx->membase_phys, uc_mem_map_size);
  508. if (!membase) {
  509. netif_err(efx, probe, efx->net_dev,
  510. "could not shrink memory BAR to %x\n",
  511. uc_mem_map_size);
  512. return -ENOMEM;
  513. }
  514. iounmap(efx->membase);
  515. efx->membase = membase;
  516. /* Set up the WC mapping if needed */
  517. if (wc_mem_map_size) {
  518. nic_data->wc_membase = ioremap_wc(efx->membase_phys +
  519. uc_mem_map_size,
  520. wc_mem_map_size);
  521. if (!nic_data->wc_membase) {
  522. netif_err(efx, probe, efx->net_dev,
  523. "could not allocate WC mapping of size %x\n",
  524. wc_mem_map_size);
  525. return -ENOMEM;
  526. }
  527. nic_data->pio_write_vi_base = pio_write_vi_base;
  528. nic_data->pio_write_base =
  529. nic_data->wc_membase +
  530. (pio_write_vi_base * EFX_VI_PAGE_SIZE + ER_DZ_TX_PIOBUF -
  531. uc_mem_map_size);
  532. rc = efx_ef10_link_piobufs(efx);
  533. if (rc)
  534. efx_ef10_free_piobufs(efx);
  535. }
  536. netif_dbg(efx, probe, efx->net_dev,
  537. "memory BAR at %pa (virtual %p+%x UC, %p+%x WC)\n",
  538. &efx->membase_phys, efx->membase, uc_mem_map_size,
  539. nic_data->wc_membase, wc_mem_map_size);
  540. return 0;
  541. }
  542. static int efx_ef10_init_nic(struct efx_nic *efx)
  543. {
  544. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  545. int rc;
  546. if (nic_data->must_check_datapath_caps) {
  547. rc = efx_ef10_init_datapath_caps(efx);
  548. if (rc)
  549. return rc;
  550. nic_data->must_check_datapath_caps = false;
  551. }
  552. if (nic_data->must_realloc_vis) {
  553. /* We cannot let the number of VIs change now */
  554. rc = efx_ef10_alloc_vis(efx, nic_data->n_allocated_vis,
  555. nic_data->n_allocated_vis);
  556. if (rc)
  557. return rc;
  558. nic_data->must_realloc_vis = false;
  559. }
  560. if (nic_data->must_restore_piobufs && nic_data->n_piobufs) {
  561. rc = efx_ef10_alloc_piobufs(efx, nic_data->n_piobufs);
  562. if (rc == 0) {
  563. rc = efx_ef10_link_piobufs(efx);
  564. if (rc)
  565. efx_ef10_free_piobufs(efx);
  566. }
  567. /* Log an error on failure, but this is non-fatal */
  568. if (rc)
  569. netif_err(efx, drv, efx->net_dev,
  570. "failed to restore PIO buffers (%d)\n", rc);
  571. nic_data->must_restore_piobufs = false;
  572. }
  573. efx_ef10_rx_push_indir_table(efx);
  574. return 0;
  575. }
  576. static int efx_ef10_map_reset_flags(u32 *flags)
  577. {
  578. enum {
  579. EF10_RESET_PORT = ((ETH_RESET_MAC | ETH_RESET_PHY) <<
  580. ETH_RESET_SHARED_SHIFT),
  581. EF10_RESET_MC = ((ETH_RESET_DMA | ETH_RESET_FILTER |
  582. ETH_RESET_OFFLOAD | ETH_RESET_MAC |
  583. ETH_RESET_PHY | ETH_RESET_MGMT) <<
  584. ETH_RESET_SHARED_SHIFT)
  585. };
  586. /* We assume for now that our PCI function is permitted to
  587. * reset everything.
  588. */
  589. if ((*flags & EF10_RESET_MC) == EF10_RESET_MC) {
  590. *flags &= ~EF10_RESET_MC;
  591. return RESET_TYPE_WORLD;
  592. }
  593. if ((*flags & EF10_RESET_PORT) == EF10_RESET_PORT) {
  594. *flags &= ~EF10_RESET_PORT;
  595. return RESET_TYPE_ALL;
  596. }
  597. /* no invisible reset implemented */
  598. return -EINVAL;
  599. }
  600. #define EF10_DMA_STAT(ext_name, mcdi_name) \
  601. [EF10_STAT_ ## ext_name] = \
  602. { #ext_name, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
  603. #define EF10_DMA_INVIS_STAT(int_name, mcdi_name) \
  604. [EF10_STAT_ ## int_name] = \
  605. { NULL, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
  606. #define EF10_OTHER_STAT(ext_name) \
  607. [EF10_STAT_ ## ext_name] = { #ext_name, 0, 0 }
  608. static const struct efx_hw_stat_desc efx_ef10_stat_desc[EF10_STAT_COUNT] = {
  609. EF10_DMA_STAT(tx_bytes, TX_BYTES),
  610. EF10_DMA_STAT(tx_packets, TX_PKTS),
  611. EF10_DMA_STAT(tx_pause, TX_PAUSE_PKTS),
  612. EF10_DMA_STAT(tx_control, TX_CONTROL_PKTS),
  613. EF10_DMA_STAT(tx_unicast, TX_UNICAST_PKTS),
  614. EF10_DMA_STAT(tx_multicast, TX_MULTICAST_PKTS),
  615. EF10_DMA_STAT(tx_broadcast, TX_BROADCAST_PKTS),
  616. EF10_DMA_STAT(tx_lt64, TX_LT64_PKTS),
  617. EF10_DMA_STAT(tx_64, TX_64_PKTS),
  618. EF10_DMA_STAT(tx_65_to_127, TX_65_TO_127_PKTS),
  619. EF10_DMA_STAT(tx_128_to_255, TX_128_TO_255_PKTS),
  620. EF10_DMA_STAT(tx_256_to_511, TX_256_TO_511_PKTS),
  621. EF10_DMA_STAT(tx_512_to_1023, TX_512_TO_1023_PKTS),
  622. EF10_DMA_STAT(tx_1024_to_15xx, TX_1024_TO_15XX_PKTS),
  623. EF10_DMA_STAT(tx_15xx_to_jumbo, TX_15XX_TO_JUMBO_PKTS),
  624. EF10_DMA_STAT(rx_bytes, RX_BYTES),
  625. EF10_DMA_INVIS_STAT(rx_bytes_minus_good_bytes, RX_BAD_BYTES),
  626. EF10_OTHER_STAT(rx_good_bytes),
  627. EF10_OTHER_STAT(rx_bad_bytes),
  628. EF10_DMA_STAT(rx_packets, RX_PKTS),
  629. EF10_DMA_STAT(rx_good, RX_GOOD_PKTS),
  630. EF10_DMA_STAT(rx_bad, RX_BAD_FCS_PKTS),
  631. EF10_DMA_STAT(rx_pause, RX_PAUSE_PKTS),
  632. EF10_DMA_STAT(rx_control, RX_CONTROL_PKTS),
  633. EF10_DMA_STAT(rx_unicast, RX_UNICAST_PKTS),
  634. EF10_DMA_STAT(rx_multicast, RX_MULTICAST_PKTS),
  635. EF10_DMA_STAT(rx_broadcast, RX_BROADCAST_PKTS),
  636. EF10_DMA_STAT(rx_lt64, RX_UNDERSIZE_PKTS),
  637. EF10_DMA_STAT(rx_64, RX_64_PKTS),
  638. EF10_DMA_STAT(rx_65_to_127, RX_65_TO_127_PKTS),
  639. EF10_DMA_STAT(rx_128_to_255, RX_128_TO_255_PKTS),
  640. EF10_DMA_STAT(rx_256_to_511, RX_256_TO_511_PKTS),
  641. EF10_DMA_STAT(rx_512_to_1023, RX_512_TO_1023_PKTS),
  642. EF10_DMA_STAT(rx_1024_to_15xx, RX_1024_TO_15XX_PKTS),
  643. EF10_DMA_STAT(rx_15xx_to_jumbo, RX_15XX_TO_JUMBO_PKTS),
  644. EF10_DMA_STAT(rx_gtjumbo, RX_GTJUMBO_PKTS),
  645. EF10_DMA_STAT(rx_bad_gtjumbo, RX_JABBER_PKTS),
  646. EF10_DMA_STAT(rx_overflow, RX_OVERFLOW_PKTS),
  647. EF10_DMA_STAT(rx_align_error, RX_ALIGN_ERROR_PKTS),
  648. EF10_DMA_STAT(rx_length_error, RX_LENGTH_ERROR_PKTS),
  649. EF10_DMA_STAT(rx_nodesc_drops, RX_NODESC_DROPS),
  650. EF10_DMA_STAT(rx_pm_trunc_bb_overflow, PM_TRUNC_BB_OVERFLOW),
  651. EF10_DMA_STAT(rx_pm_discard_bb_overflow, PM_DISCARD_BB_OVERFLOW),
  652. EF10_DMA_STAT(rx_pm_trunc_vfifo_full, PM_TRUNC_VFIFO_FULL),
  653. EF10_DMA_STAT(rx_pm_discard_vfifo_full, PM_DISCARD_VFIFO_FULL),
  654. EF10_DMA_STAT(rx_pm_trunc_qbb, PM_TRUNC_QBB),
  655. EF10_DMA_STAT(rx_pm_discard_qbb, PM_DISCARD_QBB),
  656. EF10_DMA_STAT(rx_pm_discard_mapping, PM_DISCARD_MAPPING),
  657. EF10_DMA_STAT(rx_dp_q_disabled_packets, RXDP_Q_DISABLED_PKTS),
  658. EF10_DMA_STAT(rx_dp_di_dropped_packets, RXDP_DI_DROPPED_PKTS),
  659. EF10_DMA_STAT(rx_dp_streaming_packets, RXDP_STREAMING_PKTS),
  660. EF10_DMA_STAT(rx_dp_emerg_fetch, RXDP_EMERGENCY_FETCH_CONDITIONS),
  661. EF10_DMA_STAT(rx_dp_emerg_wait, RXDP_EMERGENCY_WAIT_CONDITIONS),
  662. };
  663. #define HUNT_COMMON_STAT_MASK ((1ULL << EF10_STAT_tx_bytes) | \
  664. (1ULL << EF10_STAT_tx_packets) | \
  665. (1ULL << EF10_STAT_tx_pause) | \
  666. (1ULL << EF10_STAT_tx_unicast) | \
  667. (1ULL << EF10_STAT_tx_multicast) | \
  668. (1ULL << EF10_STAT_tx_broadcast) | \
  669. (1ULL << EF10_STAT_rx_bytes) | \
  670. (1ULL << EF10_STAT_rx_bytes_minus_good_bytes) | \
  671. (1ULL << EF10_STAT_rx_good_bytes) | \
  672. (1ULL << EF10_STAT_rx_bad_bytes) | \
  673. (1ULL << EF10_STAT_rx_packets) | \
  674. (1ULL << EF10_STAT_rx_good) | \
  675. (1ULL << EF10_STAT_rx_bad) | \
  676. (1ULL << EF10_STAT_rx_pause) | \
  677. (1ULL << EF10_STAT_rx_control) | \
  678. (1ULL << EF10_STAT_rx_unicast) | \
  679. (1ULL << EF10_STAT_rx_multicast) | \
  680. (1ULL << EF10_STAT_rx_broadcast) | \
  681. (1ULL << EF10_STAT_rx_lt64) | \
  682. (1ULL << EF10_STAT_rx_64) | \
  683. (1ULL << EF10_STAT_rx_65_to_127) | \
  684. (1ULL << EF10_STAT_rx_128_to_255) | \
  685. (1ULL << EF10_STAT_rx_256_to_511) | \
  686. (1ULL << EF10_STAT_rx_512_to_1023) | \
  687. (1ULL << EF10_STAT_rx_1024_to_15xx) | \
  688. (1ULL << EF10_STAT_rx_15xx_to_jumbo) | \
  689. (1ULL << EF10_STAT_rx_gtjumbo) | \
  690. (1ULL << EF10_STAT_rx_bad_gtjumbo) | \
  691. (1ULL << EF10_STAT_rx_overflow) | \
  692. (1ULL << EF10_STAT_rx_nodesc_drops))
  693. /* These statistics are only provided by the 10G MAC. For a 10G/40G
  694. * switchable port we do not expose these because they might not
  695. * include all the packets they should.
  696. */
  697. #define HUNT_10G_ONLY_STAT_MASK ((1ULL << EF10_STAT_tx_control) | \
  698. (1ULL << EF10_STAT_tx_lt64) | \
  699. (1ULL << EF10_STAT_tx_64) | \
  700. (1ULL << EF10_STAT_tx_65_to_127) | \
  701. (1ULL << EF10_STAT_tx_128_to_255) | \
  702. (1ULL << EF10_STAT_tx_256_to_511) | \
  703. (1ULL << EF10_STAT_tx_512_to_1023) | \
  704. (1ULL << EF10_STAT_tx_1024_to_15xx) | \
  705. (1ULL << EF10_STAT_tx_15xx_to_jumbo))
  706. /* These statistics are only provided by the 40G MAC. For a 10G/40G
  707. * switchable port we do expose these because the errors will otherwise
  708. * be silent.
  709. */
  710. #define HUNT_40G_EXTRA_STAT_MASK ((1ULL << EF10_STAT_rx_align_error) | \
  711. (1ULL << EF10_STAT_rx_length_error))
  712. /* These statistics are only provided if the firmware supports the
  713. * capability PM_AND_RXDP_COUNTERS.
  714. */
  715. #define HUNT_PM_AND_RXDP_STAT_MASK ( \
  716. (1ULL << EF10_STAT_rx_pm_trunc_bb_overflow) | \
  717. (1ULL << EF10_STAT_rx_pm_discard_bb_overflow) | \
  718. (1ULL << EF10_STAT_rx_pm_trunc_vfifo_full) | \
  719. (1ULL << EF10_STAT_rx_pm_discard_vfifo_full) | \
  720. (1ULL << EF10_STAT_rx_pm_trunc_qbb) | \
  721. (1ULL << EF10_STAT_rx_pm_discard_qbb) | \
  722. (1ULL << EF10_STAT_rx_pm_discard_mapping) | \
  723. (1ULL << EF10_STAT_rx_dp_q_disabled_packets) | \
  724. (1ULL << EF10_STAT_rx_dp_di_dropped_packets) | \
  725. (1ULL << EF10_STAT_rx_dp_streaming_packets) | \
  726. (1ULL << EF10_STAT_rx_dp_emerg_fetch) | \
  727. (1ULL << EF10_STAT_rx_dp_emerg_wait))
  728. static u64 efx_ef10_raw_stat_mask(struct efx_nic *efx)
  729. {
  730. u64 raw_mask = HUNT_COMMON_STAT_MASK;
  731. u32 port_caps = efx_mcdi_phy_get_caps(efx);
  732. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  733. if (port_caps & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
  734. raw_mask |= HUNT_40G_EXTRA_STAT_MASK;
  735. else
  736. raw_mask |= HUNT_10G_ONLY_STAT_MASK;
  737. if (nic_data->datapath_caps &
  738. (1 << MC_CMD_GET_CAPABILITIES_OUT_PM_AND_RXDP_COUNTERS_LBN))
  739. raw_mask |= HUNT_PM_AND_RXDP_STAT_MASK;
  740. return raw_mask;
  741. }
  742. static void efx_ef10_get_stat_mask(struct efx_nic *efx, unsigned long *mask)
  743. {
  744. u64 raw_mask = efx_ef10_raw_stat_mask(efx);
  745. #if BITS_PER_LONG == 64
  746. mask[0] = raw_mask;
  747. #else
  748. mask[0] = raw_mask & 0xffffffff;
  749. mask[1] = raw_mask >> 32;
  750. #endif
  751. }
  752. static size_t efx_ef10_describe_stats(struct efx_nic *efx, u8 *names)
  753. {
  754. DECLARE_BITMAP(mask, EF10_STAT_COUNT);
  755. efx_ef10_get_stat_mask(efx, mask);
  756. return efx_nic_describe_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
  757. mask, names);
  758. }
  759. static int efx_ef10_try_update_nic_stats(struct efx_nic *efx)
  760. {
  761. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  762. DECLARE_BITMAP(mask, EF10_STAT_COUNT);
  763. __le64 generation_start, generation_end;
  764. u64 *stats = nic_data->stats;
  765. __le64 *dma_stats;
  766. efx_ef10_get_stat_mask(efx, mask);
  767. dma_stats = efx->stats_buffer.addr;
  768. nic_data = efx->nic_data;
  769. generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
  770. if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
  771. return 0;
  772. rmb();
  773. efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, mask,
  774. stats, efx->stats_buffer.addr, false);
  775. rmb();
  776. generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
  777. if (generation_end != generation_start)
  778. return -EAGAIN;
  779. /* Update derived statistics */
  780. stats[EF10_STAT_rx_good_bytes] =
  781. stats[EF10_STAT_rx_bytes] -
  782. stats[EF10_STAT_rx_bytes_minus_good_bytes];
  783. efx_update_diff_stat(&stats[EF10_STAT_rx_bad_bytes],
  784. stats[EF10_STAT_rx_bytes_minus_good_bytes]);
  785. return 0;
  786. }
  787. static size_t efx_ef10_update_stats(struct efx_nic *efx, u64 *full_stats,
  788. struct rtnl_link_stats64 *core_stats)
  789. {
  790. DECLARE_BITMAP(mask, EF10_STAT_COUNT);
  791. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  792. u64 *stats = nic_data->stats;
  793. size_t stats_count = 0, index;
  794. int retry;
  795. efx_ef10_get_stat_mask(efx, mask);
  796. /* If we're unlucky enough to read statistics during the DMA, wait
  797. * up to 10ms for it to finish (typically takes <500us)
  798. */
  799. for (retry = 0; retry < 100; ++retry) {
  800. if (efx_ef10_try_update_nic_stats(efx) == 0)
  801. break;
  802. udelay(100);
  803. }
  804. if (full_stats) {
  805. for_each_set_bit(index, mask, EF10_STAT_COUNT) {
  806. if (efx_ef10_stat_desc[index].name) {
  807. *full_stats++ = stats[index];
  808. ++stats_count;
  809. }
  810. }
  811. }
  812. if (core_stats) {
  813. core_stats->rx_packets = stats[EF10_STAT_rx_packets];
  814. core_stats->tx_packets = stats[EF10_STAT_tx_packets];
  815. core_stats->rx_bytes = stats[EF10_STAT_rx_bytes];
  816. core_stats->tx_bytes = stats[EF10_STAT_tx_bytes];
  817. core_stats->rx_dropped = stats[EF10_STAT_rx_nodesc_drops];
  818. core_stats->multicast = stats[EF10_STAT_rx_multicast];
  819. core_stats->rx_length_errors =
  820. stats[EF10_STAT_rx_gtjumbo] +
  821. stats[EF10_STAT_rx_length_error];
  822. core_stats->rx_crc_errors = stats[EF10_STAT_rx_bad];
  823. core_stats->rx_frame_errors = stats[EF10_STAT_rx_align_error];
  824. core_stats->rx_fifo_errors = stats[EF10_STAT_rx_overflow];
  825. core_stats->rx_errors = (core_stats->rx_length_errors +
  826. core_stats->rx_crc_errors +
  827. core_stats->rx_frame_errors);
  828. }
  829. return stats_count;
  830. }
  831. static void efx_ef10_push_irq_moderation(struct efx_channel *channel)
  832. {
  833. struct efx_nic *efx = channel->efx;
  834. unsigned int mode, value;
  835. efx_dword_t timer_cmd;
  836. if (channel->irq_moderation) {
  837. mode = 3;
  838. value = channel->irq_moderation - 1;
  839. } else {
  840. mode = 0;
  841. value = 0;
  842. }
  843. if (EFX_EF10_WORKAROUND_35388(efx)) {
  844. EFX_POPULATE_DWORD_3(timer_cmd, ERF_DD_EVQ_IND_TIMER_FLAGS,
  845. EFE_DD_EVQ_IND_TIMER_FLAGS,
  846. ERF_DD_EVQ_IND_TIMER_MODE, mode,
  847. ERF_DD_EVQ_IND_TIMER_VAL, value);
  848. efx_writed_page(efx, &timer_cmd, ER_DD_EVQ_INDIRECT,
  849. channel->channel);
  850. } else {
  851. EFX_POPULATE_DWORD_2(timer_cmd, ERF_DZ_TC_TIMER_MODE, mode,
  852. ERF_DZ_TC_TIMER_VAL, value);
  853. efx_writed_page(efx, &timer_cmd, ER_DZ_EVQ_TMR,
  854. channel->channel);
  855. }
  856. }
  857. static void efx_ef10_get_wol(struct efx_nic *efx, struct ethtool_wolinfo *wol)
  858. {
  859. wol->supported = 0;
  860. wol->wolopts = 0;
  861. memset(&wol->sopass, 0, sizeof(wol->sopass));
  862. }
  863. static int efx_ef10_set_wol(struct efx_nic *efx, u32 type)
  864. {
  865. if (type != 0)
  866. return -EINVAL;
  867. return 0;
  868. }
  869. static void efx_ef10_mcdi_request(struct efx_nic *efx,
  870. const efx_dword_t *hdr, size_t hdr_len,
  871. const efx_dword_t *sdu, size_t sdu_len)
  872. {
  873. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  874. u8 *pdu = nic_data->mcdi_buf.addr;
  875. memcpy(pdu, hdr, hdr_len);
  876. memcpy(pdu + hdr_len, sdu, sdu_len);
  877. wmb();
  878. /* The hardware provides 'low' and 'high' (doorbell) registers
  879. * for passing the 64-bit address of an MCDI request to
  880. * firmware. However the dwords are swapped by firmware. The
  881. * least significant bits of the doorbell are then 0 for all
  882. * MCDI requests due to alignment.
  883. */
  884. _efx_writed(efx, cpu_to_le32((u64)nic_data->mcdi_buf.dma_addr >> 32),
  885. ER_DZ_MC_DB_LWRD);
  886. _efx_writed(efx, cpu_to_le32((u32)nic_data->mcdi_buf.dma_addr),
  887. ER_DZ_MC_DB_HWRD);
  888. }
  889. static bool efx_ef10_mcdi_poll_response(struct efx_nic *efx)
  890. {
  891. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  892. const efx_dword_t hdr = *(const efx_dword_t *)nic_data->mcdi_buf.addr;
  893. rmb();
  894. return EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE);
  895. }
  896. static void
  897. efx_ef10_mcdi_read_response(struct efx_nic *efx, efx_dword_t *outbuf,
  898. size_t offset, size_t outlen)
  899. {
  900. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  901. const u8 *pdu = nic_data->mcdi_buf.addr;
  902. memcpy(outbuf, pdu + offset, outlen);
  903. }
  904. static int efx_ef10_mcdi_poll_reboot(struct efx_nic *efx)
  905. {
  906. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  907. int rc;
  908. rc = efx_ef10_get_warm_boot_count(efx);
  909. if (rc < 0) {
  910. /* The firmware is presumably in the process of
  911. * rebooting. However, we are supposed to report each
  912. * reboot just once, so we must only do that once we
  913. * can read and store the updated warm boot count.
  914. */
  915. return 0;
  916. }
  917. if (rc == nic_data->warm_boot_count)
  918. return 0;
  919. nic_data->warm_boot_count = rc;
  920. /* All our allocations have been reset */
  921. nic_data->must_realloc_vis = true;
  922. nic_data->must_restore_filters = true;
  923. nic_data->must_restore_piobufs = true;
  924. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  925. /* The datapath firmware might have been changed */
  926. nic_data->must_check_datapath_caps = true;
  927. /* MAC statistics have been cleared on the NIC; clear the local
  928. * statistic that we update with efx_update_diff_stat().
  929. */
  930. nic_data->stats[EF10_STAT_rx_bad_bytes] = 0;
  931. return -EIO;
  932. }
  933. /* Handle an MSI interrupt
  934. *
  935. * Handle an MSI hardware interrupt. This routine schedules event
  936. * queue processing. No interrupt acknowledgement cycle is necessary.
  937. * Also, we never need to check that the interrupt is for us, since
  938. * MSI interrupts cannot be shared.
  939. */
  940. static irqreturn_t efx_ef10_msi_interrupt(int irq, void *dev_id)
  941. {
  942. struct efx_msi_context *context = dev_id;
  943. struct efx_nic *efx = context->efx;
  944. netif_vdbg(efx, intr, efx->net_dev,
  945. "IRQ %d on CPU %d\n", irq, raw_smp_processor_id());
  946. if (likely(ACCESS_ONCE(efx->irq_soft_enabled))) {
  947. /* Note test interrupts */
  948. if (context->index == efx->irq_level)
  949. efx->last_irq_cpu = raw_smp_processor_id();
  950. /* Schedule processing of the channel */
  951. efx_schedule_channel_irq(efx->channel[context->index]);
  952. }
  953. return IRQ_HANDLED;
  954. }
  955. static irqreturn_t efx_ef10_legacy_interrupt(int irq, void *dev_id)
  956. {
  957. struct efx_nic *efx = dev_id;
  958. bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
  959. struct efx_channel *channel;
  960. efx_dword_t reg;
  961. u32 queues;
  962. /* Read the ISR which also ACKs the interrupts */
  963. efx_readd(efx, &reg, ER_DZ_BIU_INT_ISR);
  964. queues = EFX_DWORD_FIELD(reg, ERF_DZ_ISR_REG);
  965. if (queues == 0)
  966. return IRQ_NONE;
  967. if (likely(soft_enabled)) {
  968. /* Note test interrupts */
  969. if (queues & (1U << efx->irq_level))
  970. efx->last_irq_cpu = raw_smp_processor_id();
  971. efx_for_each_channel(channel, efx) {
  972. if (queues & 1)
  973. efx_schedule_channel_irq(channel);
  974. queues >>= 1;
  975. }
  976. }
  977. netif_vdbg(efx, intr, efx->net_dev,
  978. "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  979. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  980. return IRQ_HANDLED;
  981. }
  982. static void efx_ef10_irq_test_generate(struct efx_nic *efx)
  983. {
  984. MCDI_DECLARE_BUF(inbuf, MC_CMD_TRIGGER_INTERRUPT_IN_LEN);
  985. BUILD_BUG_ON(MC_CMD_TRIGGER_INTERRUPT_OUT_LEN != 0);
  986. MCDI_SET_DWORD(inbuf, TRIGGER_INTERRUPT_IN_INTR_LEVEL, efx->irq_level);
  987. (void) efx_mcdi_rpc(efx, MC_CMD_TRIGGER_INTERRUPT,
  988. inbuf, sizeof(inbuf), NULL, 0, NULL);
  989. }
  990. static int efx_ef10_tx_probe(struct efx_tx_queue *tx_queue)
  991. {
  992. return efx_nic_alloc_buffer(tx_queue->efx, &tx_queue->txd.buf,
  993. (tx_queue->ptr_mask + 1) *
  994. sizeof(efx_qword_t),
  995. GFP_KERNEL);
  996. }
  997. /* This writes to the TX_DESC_WPTR and also pushes data */
  998. static inline void efx_ef10_push_tx_desc(struct efx_tx_queue *tx_queue,
  999. const efx_qword_t *txd)
  1000. {
  1001. unsigned int write_ptr;
  1002. efx_oword_t reg;
  1003. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1004. EFX_POPULATE_OWORD_1(reg, ERF_DZ_TX_DESC_WPTR, write_ptr);
  1005. reg.qword[0] = *txd;
  1006. efx_writeo_page(tx_queue->efx, &reg,
  1007. ER_DZ_TX_DESC_UPD, tx_queue->queue);
  1008. }
  1009. static void efx_ef10_tx_init(struct efx_tx_queue *tx_queue)
  1010. {
  1011. MCDI_DECLARE_BUF(inbuf, MC_CMD_INIT_TXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
  1012. EFX_BUF_SIZE));
  1013. MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_TXQ_OUT_LEN);
  1014. bool csum_offload = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
  1015. size_t entries = tx_queue->txd.buf.len / EFX_BUF_SIZE;
  1016. struct efx_channel *channel = tx_queue->channel;
  1017. struct efx_nic *efx = tx_queue->efx;
  1018. size_t inlen, outlen;
  1019. dma_addr_t dma_addr;
  1020. efx_qword_t *txd;
  1021. int rc;
  1022. int i;
  1023. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_SIZE, tx_queue->ptr_mask + 1);
  1024. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_TARGET_EVQ, channel->channel);
  1025. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_LABEL, tx_queue->queue);
  1026. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_INSTANCE, tx_queue->queue);
  1027. MCDI_POPULATE_DWORD_2(inbuf, INIT_TXQ_IN_FLAGS,
  1028. INIT_TXQ_IN_FLAG_IP_CSUM_DIS, !csum_offload,
  1029. INIT_TXQ_IN_FLAG_TCP_CSUM_DIS, !csum_offload);
  1030. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_OWNER_ID, 0);
  1031. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
  1032. dma_addr = tx_queue->txd.buf.dma_addr;
  1033. netif_dbg(efx, hw, efx->net_dev, "pushing TXQ %d. %zu entries (%llx)\n",
  1034. tx_queue->queue, entries, (u64)dma_addr);
  1035. for (i = 0; i < entries; ++i) {
  1036. MCDI_SET_ARRAY_QWORD(inbuf, INIT_TXQ_IN_DMA_ADDR, i, dma_addr);
  1037. dma_addr += EFX_BUF_SIZE;
  1038. }
  1039. inlen = MC_CMD_INIT_TXQ_IN_LEN(entries);
  1040. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_TXQ, inbuf, inlen,
  1041. outbuf, sizeof(outbuf), &outlen);
  1042. if (rc)
  1043. goto fail;
  1044. /* A previous user of this TX queue might have set us up the
  1045. * bomb by writing a descriptor to the TX push collector but
  1046. * not the doorbell. (Each collector belongs to a port, not a
  1047. * queue or function, so cannot easily be reset.) We must
  1048. * attempt to push a no-op descriptor in its place.
  1049. */
  1050. tx_queue->buffer[0].flags = EFX_TX_BUF_OPTION;
  1051. tx_queue->insert_count = 1;
  1052. txd = efx_tx_desc(tx_queue, 0);
  1053. EFX_POPULATE_QWORD_4(*txd,
  1054. ESF_DZ_TX_DESC_IS_OPT, true,
  1055. ESF_DZ_TX_OPTION_TYPE,
  1056. ESE_DZ_TX_OPTION_DESC_CRC_CSUM,
  1057. ESF_DZ_TX_OPTION_UDP_TCP_CSUM, csum_offload,
  1058. ESF_DZ_TX_OPTION_IP_CSUM, csum_offload);
  1059. tx_queue->write_count = 1;
  1060. wmb();
  1061. efx_ef10_push_tx_desc(tx_queue, txd);
  1062. return;
  1063. fail:
  1064. WARN_ON(true);
  1065. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1066. }
  1067. static void efx_ef10_tx_fini(struct efx_tx_queue *tx_queue)
  1068. {
  1069. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_TXQ_IN_LEN);
  1070. MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_TXQ_OUT_LEN);
  1071. struct efx_nic *efx = tx_queue->efx;
  1072. size_t outlen;
  1073. int rc;
  1074. MCDI_SET_DWORD(inbuf, FINI_TXQ_IN_INSTANCE,
  1075. tx_queue->queue);
  1076. rc = efx_mcdi_rpc(efx, MC_CMD_FINI_TXQ, inbuf, sizeof(inbuf),
  1077. outbuf, sizeof(outbuf), &outlen);
  1078. if (rc && rc != -EALREADY)
  1079. goto fail;
  1080. return;
  1081. fail:
  1082. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1083. }
  1084. static void efx_ef10_tx_remove(struct efx_tx_queue *tx_queue)
  1085. {
  1086. efx_nic_free_buffer(tx_queue->efx, &tx_queue->txd.buf);
  1087. }
  1088. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  1089. static inline void efx_ef10_notify_tx_desc(struct efx_tx_queue *tx_queue)
  1090. {
  1091. unsigned int write_ptr;
  1092. efx_dword_t reg;
  1093. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1094. EFX_POPULATE_DWORD_1(reg, ERF_DZ_TX_DESC_WPTR_DWORD, write_ptr);
  1095. efx_writed_page(tx_queue->efx, &reg,
  1096. ER_DZ_TX_DESC_UPD_DWORD, tx_queue->queue);
  1097. }
  1098. static void efx_ef10_tx_write(struct efx_tx_queue *tx_queue)
  1099. {
  1100. unsigned int old_write_count = tx_queue->write_count;
  1101. struct efx_tx_buffer *buffer;
  1102. unsigned int write_ptr;
  1103. efx_qword_t *txd;
  1104. BUG_ON(tx_queue->write_count == tx_queue->insert_count);
  1105. do {
  1106. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1107. buffer = &tx_queue->buffer[write_ptr];
  1108. txd = efx_tx_desc(tx_queue, write_ptr);
  1109. ++tx_queue->write_count;
  1110. /* Create TX descriptor ring entry */
  1111. if (buffer->flags & EFX_TX_BUF_OPTION) {
  1112. *txd = buffer->option;
  1113. } else {
  1114. BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
  1115. EFX_POPULATE_QWORD_3(
  1116. *txd,
  1117. ESF_DZ_TX_KER_CONT,
  1118. buffer->flags & EFX_TX_BUF_CONT,
  1119. ESF_DZ_TX_KER_BYTE_CNT, buffer->len,
  1120. ESF_DZ_TX_KER_BUF_ADDR, buffer->dma_addr);
  1121. }
  1122. } while (tx_queue->write_count != tx_queue->insert_count);
  1123. wmb(); /* Ensure descriptors are written before they are fetched */
  1124. if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
  1125. txd = efx_tx_desc(tx_queue,
  1126. old_write_count & tx_queue->ptr_mask);
  1127. efx_ef10_push_tx_desc(tx_queue, txd);
  1128. ++tx_queue->pushes;
  1129. } else {
  1130. efx_ef10_notify_tx_desc(tx_queue);
  1131. }
  1132. }
  1133. static int efx_ef10_alloc_rss_context(struct efx_nic *efx, u32 *context)
  1134. {
  1135. MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_ALLOC_IN_LEN);
  1136. MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN);
  1137. size_t outlen;
  1138. int rc;
  1139. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_UPSTREAM_PORT_ID,
  1140. EVB_PORT_ID_ASSIGNED);
  1141. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_TYPE,
  1142. MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_EXCLUSIVE);
  1143. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_NUM_QUEUES,
  1144. EFX_MAX_CHANNELS);
  1145. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_ALLOC, inbuf, sizeof(inbuf),
  1146. outbuf, sizeof(outbuf), &outlen);
  1147. if (rc != 0)
  1148. return rc;
  1149. if (outlen < MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN)
  1150. return -EIO;
  1151. *context = MCDI_DWORD(outbuf, RSS_CONTEXT_ALLOC_OUT_RSS_CONTEXT_ID);
  1152. return 0;
  1153. }
  1154. static void efx_ef10_free_rss_context(struct efx_nic *efx, u32 context)
  1155. {
  1156. MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_FREE_IN_LEN);
  1157. int rc;
  1158. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_FREE_IN_RSS_CONTEXT_ID,
  1159. context);
  1160. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_FREE, inbuf, sizeof(inbuf),
  1161. NULL, 0, NULL);
  1162. WARN_ON(rc != 0);
  1163. }
  1164. static int efx_ef10_populate_rss_table(struct efx_nic *efx, u32 context)
  1165. {
  1166. MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_SET_TABLE_IN_LEN);
  1167. MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_SET_KEY_IN_LEN);
  1168. int i, rc;
  1169. MCDI_SET_DWORD(tablebuf, RSS_CONTEXT_SET_TABLE_IN_RSS_CONTEXT_ID,
  1170. context);
  1171. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1172. MC_CMD_RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE_LEN);
  1173. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); ++i)
  1174. MCDI_PTR(tablebuf,
  1175. RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE)[i] =
  1176. (u8) efx->rx_indir_table[i];
  1177. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_TABLE, tablebuf,
  1178. sizeof(tablebuf), NULL, 0, NULL);
  1179. if (rc != 0)
  1180. return rc;
  1181. MCDI_SET_DWORD(keybuf, RSS_CONTEXT_SET_KEY_IN_RSS_CONTEXT_ID,
  1182. context);
  1183. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_hash_key) !=
  1184. MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
  1185. for (i = 0; i < ARRAY_SIZE(efx->rx_hash_key); ++i)
  1186. MCDI_PTR(keybuf, RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY)[i] =
  1187. efx->rx_hash_key[i];
  1188. return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_KEY, keybuf,
  1189. sizeof(keybuf), NULL, 0, NULL);
  1190. }
  1191. static void efx_ef10_rx_free_indir_table(struct efx_nic *efx)
  1192. {
  1193. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1194. if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
  1195. efx_ef10_free_rss_context(efx, nic_data->rx_rss_context);
  1196. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  1197. }
  1198. static void efx_ef10_rx_push_indir_table(struct efx_nic *efx)
  1199. {
  1200. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1201. int rc;
  1202. netif_dbg(efx, drv, efx->net_dev, "pushing RX indirection table\n");
  1203. if (nic_data->rx_rss_context == EFX_EF10_RSS_CONTEXT_INVALID) {
  1204. rc = efx_ef10_alloc_rss_context(efx, &nic_data->rx_rss_context);
  1205. if (rc != 0)
  1206. goto fail;
  1207. }
  1208. rc = efx_ef10_populate_rss_table(efx, nic_data->rx_rss_context);
  1209. if (rc != 0)
  1210. goto fail;
  1211. return;
  1212. fail:
  1213. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1214. }
  1215. static int efx_ef10_rx_probe(struct efx_rx_queue *rx_queue)
  1216. {
  1217. return efx_nic_alloc_buffer(rx_queue->efx, &rx_queue->rxd.buf,
  1218. (rx_queue->ptr_mask + 1) *
  1219. sizeof(efx_qword_t),
  1220. GFP_KERNEL);
  1221. }
  1222. static void efx_ef10_rx_init(struct efx_rx_queue *rx_queue)
  1223. {
  1224. MCDI_DECLARE_BUF(inbuf,
  1225. MC_CMD_INIT_RXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
  1226. EFX_BUF_SIZE));
  1227. MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_RXQ_OUT_LEN);
  1228. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  1229. size_t entries = rx_queue->rxd.buf.len / EFX_BUF_SIZE;
  1230. struct efx_nic *efx = rx_queue->efx;
  1231. size_t inlen, outlen;
  1232. dma_addr_t dma_addr;
  1233. int rc;
  1234. int i;
  1235. rx_queue->scatter_n = 0;
  1236. rx_queue->scatter_len = 0;
  1237. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_SIZE, rx_queue->ptr_mask + 1);
  1238. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_TARGET_EVQ, channel->channel);
  1239. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_LABEL, efx_rx_queue_index(rx_queue));
  1240. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_INSTANCE,
  1241. efx_rx_queue_index(rx_queue));
  1242. MCDI_POPULATE_DWORD_1(inbuf, INIT_RXQ_IN_FLAGS,
  1243. INIT_RXQ_IN_FLAG_PREFIX, 1);
  1244. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_OWNER_ID, 0);
  1245. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
  1246. dma_addr = rx_queue->rxd.buf.dma_addr;
  1247. netif_dbg(efx, hw, efx->net_dev, "pushing RXQ %d. %zu entries (%llx)\n",
  1248. efx_rx_queue_index(rx_queue), entries, (u64)dma_addr);
  1249. for (i = 0; i < entries; ++i) {
  1250. MCDI_SET_ARRAY_QWORD(inbuf, INIT_RXQ_IN_DMA_ADDR, i, dma_addr);
  1251. dma_addr += EFX_BUF_SIZE;
  1252. }
  1253. inlen = MC_CMD_INIT_RXQ_IN_LEN(entries);
  1254. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_RXQ, inbuf, inlen,
  1255. outbuf, sizeof(outbuf), &outlen);
  1256. if (rc)
  1257. goto fail;
  1258. return;
  1259. fail:
  1260. WARN_ON(true);
  1261. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1262. }
  1263. static void efx_ef10_rx_fini(struct efx_rx_queue *rx_queue)
  1264. {
  1265. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_RXQ_IN_LEN);
  1266. MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_RXQ_OUT_LEN);
  1267. struct efx_nic *efx = rx_queue->efx;
  1268. size_t outlen;
  1269. int rc;
  1270. MCDI_SET_DWORD(inbuf, FINI_RXQ_IN_INSTANCE,
  1271. efx_rx_queue_index(rx_queue));
  1272. rc = efx_mcdi_rpc(efx, MC_CMD_FINI_RXQ, inbuf, sizeof(inbuf),
  1273. outbuf, sizeof(outbuf), &outlen);
  1274. if (rc && rc != -EALREADY)
  1275. goto fail;
  1276. return;
  1277. fail:
  1278. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1279. }
  1280. static void efx_ef10_rx_remove(struct efx_rx_queue *rx_queue)
  1281. {
  1282. efx_nic_free_buffer(rx_queue->efx, &rx_queue->rxd.buf);
  1283. }
  1284. /* This creates an entry in the RX descriptor queue */
  1285. static inline void
  1286. efx_ef10_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
  1287. {
  1288. struct efx_rx_buffer *rx_buf;
  1289. efx_qword_t *rxd;
  1290. rxd = efx_rx_desc(rx_queue, index);
  1291. rx_buf = efx_rx_buffer(rx_queue, index);
  1292. EFX_POPULATE_QWORD_2(*rxd,
  1293. ESF_DZ_RX_KER_BYTE_CNT, rx_buf->len,
  1294. ESF_DZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
  1295. }
  1296. static void efx_ef10_rx_write(struct efx_rx_queue *rx_queue)
  1297. {
  1298. struct efx_nic *efx = rx_queue->efx;
  1299. unsigned int write_count;
  1300. efx_dword_t reg;
  1301. /* Firmware requires that RX_DESC_WPTR be a multiple of 8 */
  1302. write_count = rx_queue->added_count & ~7;
  1303. if (rx_queue->notified_count == write_count)
  1304. return;
  1305. do
  1306. efx_ef10_build_rx_desc(
  1307. rx_queue,
  1308. rx_queue->notified_count & rx_queue->ptr_mask);
  1309. while (++rx_queue->notified_count != write_count);
  1310. wmb();
  1311. EFX_POPULATE_DWORD_1(reg, ERF_DZ_RX_DESC_WPTR,
  1312. write_count & rx_queue->ptr_mask);
  1313. efx_writed_page(efx, &reg, ER_DZ_RX_DESC_UPD,
  1314. efx_rx_queue_index(rx_queue));
  1315. }
  1316. static efx_mcdi_async_completer efx_ef10_rx_defer_refill_complete;
  1317. static void efx_ef10_rx_defer_refill(struct efx_rx_queue *rx_queue)
  1318. {
  1319. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  1320. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
  1321. efx_qword_t event;
  1322. EFX_POPULATE_QWORD_2(event,
  1323. ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
  1324. ESF_DZ_EV_DATA, EFX_EF10_REFILL);
  1325. MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
  1326. /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
  1327. * already swapped the data to little-endian order.
  1328. */
  1329. memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
  1330. sizeof(efx_qword_t));
  1331. efx_mcdi_rpc_async(channel->efx, MC_CMD_DRIVER_EVENT,
  1332. inbuf, sizeof(inbuf), 0,
  1333. efx_ef10_rx_defer_refill_complete, 0);
  1334. }
  1335. static void
  1336. efx_ef10_rx_defer_refill_complete(struct efx_nic *efx, unsigned long cookie,
  1337. int rc, efx_dword_t *outbuf,
  1338. size_t outlen_actual)
  1339. {
  1340. /* nothing to do */
  1341. }
  1342. static int efx_ef10_ev_probe(struct efx_channel *channel)
  1343. {
  1344. return efx_nic_alloc_buffer(channel->efx, &channel->eventq.buf,
  1345. (channel->eventq_mask + 1) *
  1346. sizeof(efx_qword_t),
  1347. GFP_KERNEL);
  1348. }
  1349. static int efx_ef10_ev_init(struct efx_channel *channel)
  1350. {
  1351. MCDI_DECLARE_BUF(inbuf,
  1352. MC_CMD_INIT_EVQ_IN_LEN(EFX_MAX_EVQ_SIZE * 8 /
  1353. EFX_BUF_SIZE));
  1354. MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_EVQ_OUT_LEN);
  1355. size_t entries = channel->eventq.buf.len / EFX_BUF_SIZE;
  1356. struct efx_nic *efx = channel->efx;
  1357. struct efx_ef10_nic_data *nic_data;
  1358. bool supports_rx_merge;
  1359. size_t inlen, outlen;
  1360. dma_addr_t dma_addr;
  1361. int rc;
  1362. int i;
  1363. nic_data = efx->nic_data;
  1364. supports_rx_merge =
  1365. !!(nic_data->datapath_caps &
  1366. 1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN);
  1367. /* Fill event queue with all ones (i.e. empty events) */
  1368. memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
  1369. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_SIZE, channel->eventq_mask + 1);
  1370. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_INSTANCE, channel->channel);
  1371. /* INIT_EVQ expects index in vector table, not absolute */
  1372. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_IRQ_NUM, channel->channel);
  1373. MCDI_POPULATE_DWORD_4(inbuf, INIT_EVQ_IN_FLAGS,
  1374. INIT_EVQ_IN_FLAG_INTERRUPTING, 1,
  1375. INIT_EVQ_IN_FLAG_RX_MERGE, 1,
  1376. INIT_EVQ_IN_FLAG_TX_MERGE, 1,
  1377. INIT_EVQ_IN_FLAG_CUT_THRU, !supports_rx_merge);
  1378. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_MODE,
  1379. MC_CMD_INIT_EVQ_IN_TMR_MODE_DIS);
  1380. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_LOAD, 0);
  1381. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_RELOAD, 0);
  1382. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_MODE,
  1383. MC_CMD_INIT_EVQ_IN_COUNT_MODE_DIS);
  1384. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_THRSHLD, 0);
  1385. dma_addr = channel->eventq.buf.dma_addr;
  1386. for (i = 0; i < entries; ++i) {
  1387. MCDI_SET_ARRAY_QWORD(inbuf, INIT_EVQ_IN_DMA_ADDR, i, dma_addr);
  1388. dma_addr += EFX_BUF_SIZE;
  1389. }
  1390. inlen = MC_CMD_INIT_EVQ_IN_LEN(entries);
  1391. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_EVQ, inbuf, inlen,
  1392. outbuf, sizeof(outbuf), &outlen);
  1393. if (rc)
  1394. goto fail;
  1395. /* IRQ return is ignored */
  1396. return 0;
  1397. fail:
  1398. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1399. return rc;
  1400. }
  1401. static void efx_ef10_ev_fini(struct efx_channel *channel)
  1402. {
  1403. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_EVQ_IN_LEN);
  1404. MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_EVQ_OUT_LEN);
  1405. struct efx_nic *efx = channel->efx;
  1406. size_t outlen;
  1407. int rc;
  1408. MCDI_SET_DWORD(inbuf, FINI_EVQ_IN_INSTANCE, channel->channel);
  1409. rc = efx_mcdi_rpc(efx, MC_CMD_FINI_EVQ, inbuf, sizeof(inbuf),
  1410. outbuf, sizeof(outbuf), &outlen);
  1411. if (rc && rc != -EALREADY)
  1412. goto fail;
  1413. return;
  1414. fail:
  1415. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1416. }
  1417. static void efx_ef10_ev_remove(struct efx_channel *channel)
  1418. {
  1419. efx_nic_free_buffer(channel->efx, &channel->eventq.buf);
  1420. }
  1421. static void efx_ef10_handle_rx_wrong_queue(struct efx_rx_queue *rx_queue,
  1422. unsigned int rx_queue_label)
  1423. {
  1424. struct efx_nic *efx = rx_queue->efx;
  1425. netif_info(efx, hw, efx->net_dev,
  1426. "rx event arrived on queue %d labeled as queue %u\n",
  1427. efx_rx_queue_index(rx_queue), rx_queue_label);
  1428. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1429. }
  1430. static void
  1431. efx_ef10_handle_rx_bad_lbits(struct efx_rx_queue *rx_queue,
  1432. unsigned int actual, unsigned int expected)
  1433. {
  1434. unsigned int dropped = (actual - expected) & rx_queue->ptr_mask;
  1435. struct efx_nic *efx = rx_queue->efx;
  1436. netif_info(efx, hw, efx->net_dev,
  1437. "dropped %d events (index=%d expected=%d)\n",
  1438. dropped, actual, expected);
  1439. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1440. }
  1441. /* partially received RX was aborted. clean up. */
  1442. static void efx_ef10_handle_rx_abort(struct efx_rx_queue *rx_queue)
  1443. {
  1444. unsigned int rx_desc_ptr;
  1445. WARN_ON(rx_queue->scatter_n == 0);
  1446. netif_dbg(rx_queue->efx, hw, rx_queue->efx->net_dev,
  1447. "scattered RX aborted (dropping %u buffers)\n",
  1448. rx_queue->scatter_n);
  1449. rx_desc_ptr = rx_queue->removed_count & rx_queue->ptr_mask;
  1450. efx_rx_packet(rx_queue, rx_desc_ptr, rx_queue->scatter_n,
  1451. 0, EFX_RX_PKT_DISCARD);
  1452. rx_queue->removed_count += rx_queue->scatter_n;
  1453. rx_queue->scatter_n = 0;
  1454. rx_queue->scatter_len = 0;
  1455. ++efx_rx_queue_channel(rx_queue)->n_rx_nodesc_trunc;
  1456. }
  1457. static int efx_ef10_handle_rx_event(struct efx_channel *channel,
  1458. const efx_qword_t *event)
  1459. {
  1460. unsigned int rx_bytes, next_ptr_lbits, rx_queue_label, rx_l4_class;
  1461. unsigned int n_descs, n_packets, i;
  1462. struct efx_nic *efx = channel->efx;
  1463. struct efx_rx_queue *rx_queue;
  1464. bool rx_cont;
  1465. u16 flags = 0;
  1466. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  1467. return 0;
  1468. /* Basic packet information */
  1469. rx_bytes = EFX_QWORD_FIELD(*event, ESF_DZ_RX_BYTES);
  1470. next_ptr_lbits = EFX_QWORD_FIELD(*event, ESF_DZ_RX_DSC_PTR_LBITS);
  1471. rx_queue_label = EFX_QWORD_FIELD(*event, ESF_DZ_RX_QLABEL);
  1472. rx_l4_class = EFX_QWORD_FIELD(*event, ESF_DZ_RX_L4_CLASS);
  1473. rx_cont = EFX_QWORD_FIELD(*event, ESF_DZ_RX_CONT);
  1474. WARN_ON(EFX_QWORD_FIELD(*event, ESF_DZ_RX_DROP_EVENT));
  1475. rx_queue = efx_channel_get_rx_queue(channel);
  1476. if (unlikely(rx_queue_label != efx_rx_queue_index(rx_queue)))
  1477. efx_ef10_handle_rx_wrong_queue(rx_queue, rx_queue_label);
  1478. n_descs = ((next_ptr_lbits - rx_queue->removed_count) &
  1479. ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
  1480. if (n_descs != rx_queue->scatter_n + 1) {
  1481. /* detect rx abort */
  1482. if (unlikely(n_descs == rx_queue->scatter_n)) {
  1483. WARN_ON(rx_bytes != 0);
  1484. efx_ef10_handle_rx_abort(rx_queue);
  1485. return 0;
  1486. }
  1487. if (unlikely(rx_queue->scatter_n != 0)) {
  1488. /* Scattered packet completions cannot be
  1489. * merged, so something has gone wrong.
  1490. */
  1491. efx_ef10_handle_rx_bad_lbits(
  1492. rx_queue, next_ptr_lbits,
  1493. (rx_queue->removed_count +
  1494. rx_queue->scatter_n + 1) &
  1495. ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
  1496. return 0;
  1497. }
  1498. /* Merged completion for multiple non-scattered packets */
  1499. rx_queue->scatter_n = 1;
  1500. rx_queue->scatter_len = 0;
  1501. n_packets = n_descs;
  1502. ++channel->n_rx_merge_events;
  1503. channel->n_rx_merge_packets += n_packets;
  1504. flags |= EFX_RX_PKT_PREFIX_LEN;
  1505. } else {
  1506. ++rx_queue->scatter_n;
  1507. rx_queue->scatter_len += rx_bytes;
  1508. if (rx_cont)
  1509. return 0;
  1510. n_packets = 1;
  1511. }
  1512. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_ECRC_ERR)))
  1513. flags |= EFX_RX_PKT_DISCARD;
  1514. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_IPCKSUM_ERR))) {
  1515. channel->n_rx_ip_hdr_chksum_err += n_packets;
  1516. } else if (unlikely(EFX_QWORD_FIELD(*event,
  1517. ESF_DZ_RX_TCPUDP_CKSUM_ERR))) {
  1518. channel->n_rx_tcp_udp_chksum_err += n_packets;
  1519. } else if (rx_l4_class == ESE_DZ_L4_CLASS_TCP ||
  1520. rx_l4_class == ESE_DZ_L4_CLASS_UDP) {
  1521. flags |= EFX_RX_PKT_CSUMMED;
  1522. }
  1523. if (rx_l4_class == ESE_DZ_L4_CLASS_TCP)
  1524. flags |= EFX_RX_PKT_TCP;
  1525. channel->irq_mod_score += 2 * n_packets;
  1526. /* Handle received packet(s) */
  1527. for (i = 0; i < n_packets; i++) {
  1528. efx_rx_packet(rx_queue,
  1529. rx_queue->removed_count & rx_queue->ptr_mask,
  1530. rx_queue->scatter_n, rx_queue->scatter_len,
  1531. flags);
  1532. rx_queue->removed_count += rx_queue->scatter_n;
  1533. }
  1534. rx_queue->scatter_n = 0;
  1535. rx_queue->scatter_len = 0;
  1536. return n_packets;
  1537. }
  1538. static int
  1539. efx_ef10_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
  1540. {
  1541. struct efx_nic *efx = channel->efx;
  1542. struct efx_tx_queue *tx_queue;
  1543. unsigned int tx_ev_desc_ptr;
  1544. unsigned int tx_ev_q_label;
  1545. int tx_descs = 0;
  1546. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  1547. return 0;
  1548. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_TX_DROP_EVENT)))
  1549. return 0;
  1550. /* Transmit completion */
  1551. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, ESF_DZ_TX_DESCR_INDX);
  1552. tx_ev_q_label = EFX_QWORD_FIELD(*event, ESF_DZ_TX_QLABEL);
  1553. tx_queue = efx_channel_get_tx_queue(channel,
  1554. tx_ev_q_label % EFX_TXQ_TYPES);
  1555. tx_descs = ((tx_ev_desc_ptr + 1 - tx_queue->read_count) &
  1556. tx_queue->ptr_mask);
  1557. efx_xmit_done(tx_queue, tx_ev_desc_ptr & tx_queue->ptr_mask);
  1558. return tx_descs;
  1559. }
  1560. static void
  1561. efx_ef10_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  1562. {
  1563. struct efx_nic *efx = channel->efx;
  1564. int subcode;
  1565. subcode = EFX_QWORD_FIELD(*event, ESF_DZ_DRV_SUB_CODE);
  1566. switch (subcode) {
  1567. case ESE_DZ_DRV_TIMER_EV:
  1568. case ESE_DZ_DRV_WAKE_UP_EV:
  1569. break;
  1570. case ESE_DZ_DRV_START_UP_EV:
  1571. /* event queue init complete. ok. */
  1572. break;
  1573. default:
  1574. netif_err(efx, hw, efx->net_dev,
  1575. "channel %d unknown driver event type %d"
  1576. " (data " EFX_QWORD_FMT ")\n",
  1577. channel->channel, subcode,
  1578. EFX_QWORD_VAL(*event));
  1579. }
  1580. }
  1581. static void efx_ef10_handle_driver_generated_event(struct efx_channel *channel,
  1582. efx_qword_t *event)
  1583. {
  1584. struct efx_nic *efx = channel->efx;
  1585. u32 subcode;
  1586. subcode = EFX_QWORD_FIELD(*event, EFX_DWORD_0);
  1587. switch (subcode) {
  1588. case EFX_EF10_TEST:
  1589. channel->event_test_cpu = raw_smp_processor_id();
  1590. break;
  1591. case EFX_EF10_REFILL:
  1592. /* The queue must be empty, so we won't receive any rx
  1593. * events, so efx_process_channel() won't refill the
  1594. * queue. Refill it here
  1595. */
  1596. efx_fast_push_rx_descriptors(&channel->rx_queue);
  1597. break;
  1598. default:
  1599. netif_err(efx, hw, efx->net_dev,
  1600. "channel %d unknown driver event type %u"
  1601. " (data " EFX_QWORD_FMT ")\n",
  1602. channel->channel, (unsigned) subcode,
  1603. EFX_QWORD_VAL(*event));
  1604. }
  1605. }
  1606. static int efx_ef10_ev_process(struct efx_channel *channel, int quota)
  1607. {
  1608. struct efx_nic *efx = channel->efx;
  1609. efx_qword_t event, *p_event;
  1610. unsigned int read_ptr;
  1611. int ev_code;
  1612. int tx_descs = 0;
  1613. int spent = 0;
  1614. read_ptr = channel->eventq_read_ptr;
  1615. for (;;) {
  1616. p_event = efx_event(channel, read_ptr);
  1617. event = *p_event;
  1618. if (!efx_event_present(&event))
  1619. break;
  1620. EFX_SET_QWORD(*p_event);
  1621. ++read_ptr;
  1622. ev_code = EFX_QWORD_FIELD(event, ESF_DZ_EV_CODE);
  1623. netif_vdbg(efx, drv, efx->net_dev,
  1624. "processing event on %d " EFX_QWORD_FMT "\n",
  1625. channel->channel, EFX_QWORD_VAL(event));
  1626. switch (ev_code) {
  1627. case ESE_DZ_EV_CODE_MCDI_EV:
  1628. efx_mcdi_process_event(channel, &event);
  1629. break;
  1630. case ESE_DZ_EV_CODE_RX_EV:
  1631. spent += efx_ef10_handle_rx_event(channel, &event);
  1632. if (spent >= quota) {
  1633. /* XXX can we split a merged event to
  1634. * avoid going over-quota?
  1635. */
  1636. spent = quota;
  1637. goto out;
  1638. }
  1639. break;
  1640. case ESE_DZ_EV_CODE_TX_EV:
  1641. tx_descs += efx_ef10_handle_tx_event(channel, &event);
  1642. if (tx_descs > efx->txq_entries) {
  1643. spent = quota;
  1644. goto out;
  1645. } else if (++spent == quota) {
  1646. goto out;
  1647. }
  1648. break;
  1649. case ESE_DZ_EV_CODE_DRIVER_EV:
  1650. efx_ef10_handle_driver_event(channel, &event);
  1651. if (++spent == quota)
  1652. goto out;
  1653. break;
  1654. case EFX_EF10_DRVGEN_EV:
  1655. efx_ef10_handle_driver_generated_event(channel, &event);
  1656. break;
  1657. default:
  1658. netif_err(efx, hw, efx->net_dev,
  1659. "channel %d unknown event type %d"
  1660. " (data " EFX_QWORD_FMT ")\n",
  1661. channel->channel, ev_code,
  1662. EFX_QWORD_VAL(event));
  1663. }
  1664. }
  1665. out:
  1666. channel->eventq_read_ptr = read_ptr;
  1667. return spent;
  1668. }
  1669. static void efx_ef10_ev_read_ack(struct efx_channel *channel)
  1670. {
  1671. struct efx_nic *efx = channel->efx;
  1672. efx_dword_t rptr;
  1673. if (EFX_EF10_WORKAROUND_35388(efx)) {
  1674. BUILD_BUG_ON(EFX_MIN_EVQ_SIZE <
  1675. (1 << ERF_DD_EVQ_IND_RPTR_WIDTH));
  1676. BUILD_BUG_ON(EFX_MAX_EVQ_SIZE >
  1677. (1 << 2 * ERF_DD_EVQ_IND_RPTR_WIDTH));
  1678. EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
  1679. EFE_DD_EVQ_IND_RPTR_FLAGS_HIGH,
  1680. ERF_DD_EVQ_IND_RPTR,
  1681. (channel->eventq_read_ptr &
  1682. channel->eventq_mask) >>
  1683. ERF_DD_EVQ_IND_RPTR_WIDTH);
  1684. efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
  1685. channel->channel);
  1686. EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
  1687. EFE_DD_EVQ_IND_RPTR_FLAGS_LOW,
  1688. ERF_DD_EVQ_IND_RPTR,
  1689. channel->eventq_read_ptr &
  1690. ((1 << ERF_DD_EVQ_IND_RPTR_WIDTH) - 1));
  1691. efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
  1692. channel->channel);
  1693. } else {
  1694. EFX_POPULATE_DWORD_1(rptr, ERF_DZ_EVQ_RPTR,
  1695. channel->eventq_read_ptr &
  1696. channel->eventq_mask);
  1697. efx_writed_page(efx, &rptr, ER_DZ_EVQ_RPTR, channel->channel);
  1698. }
  1699. }
  1700. static void efx_ef10_ev_test_generate(struct efx_channel *channel)
  1701. {
  1702. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
  1703. struct efx_nic *efx = channel->efx;
  1704. efx_qword_t event;
  1705. int rc;
  1706. EFX_POPULATE_QWORD_2(event,
  1707. ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
  1708. ESF_DZ_EV_DATA, EFX_EF10_TEST);
  1709. MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
  1710. /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
  1711. * already swapped the data to little-endian order.
  1712. */
  1713. memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
  1714. sizeof(efx_qword_t));
  1715. rc = efx_mcdi_rpc(efx, MC_CMD_DRIVER_EVENT, inbuf, sizeof(inbuf),
  1716. NULL, 0, NULL);
  1717. if (rc != 0)
  1718. goto fail;
  1719. return;
  1720. fail:
  1721. WARN_ON(true);
  1722. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1723. }
  1724. void efx_ef10_handle_drain_event(struct efx_nic *efx)
  1725. {
  1726. if (atomic_dec_and_test(&efx->active_queues))
  1727. wake_up(&efx->flush_wq);
  1728. WARN_ON(atomic_read(&efx->active_queues) < 0);
  1729. }
  1730. static int efx_ef10_fini_dmaq(struct efx_nic *efx)
  1731. {
  1732. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1733. struct efx_channel *channel;
  1734. struct efx_tx_queue *tx_queue;
  1735. struct efx_rx_queue *rx_queue;
  1736. int pending;
  1737. /* If the MC has just rebooted, the TX/RX queues will have already been
  1738. * torn down, but efx->active_queues needs to be set to zero.
  1739. */
  1740. if (nic_data->must_realloc_vis) {
  1741. atomic_set(&efx->active_queues, 0);
  1742. return 0;
  1743. }
  1744. /* Do not attempt to write to the NIC during EEH recovery */
  1745. if (efx->state != STATE_RECOVERY) {
  1746. efx_for_each_channel(channel, efx) {
  1747. efx_for_each_channel_rx_queue(rx_queue, channel)
  1748. efx_ef10_rx_fini(rx_queue);
  1749. efx_for_each_channel_tx_queue(tx_queue, channel)
  1750. efx_ef10_tx_fini(tx_queue);
  1751. }
  1752. wait_event_timeout(efx->flush_wq,
  1753. atomic_read(&efx->active_queues) == 0,
  1754. msecs_to_jiffies(EFX_MAX_FLUSH_TIME));
  1755. pending = atomic_read(&efx->active_queues);
  1756. if (pending) {
  1757. netif_err(efx, hw, efx->net_dev, "failed to flush %d queues\n",
  1758. pending);
  1759. return -ETIMEDOUT;
  1760. }
  1761. }
  1762. return 0;
  1763. }
  1764. static bool efx_ef10_filter_equal(const struct efx_filter_spec *left,
  1765. const struct efx_filter_spec *right)
  1766. {
  1767. if ((left->match_flags ^ right->match_flags) |
  1768. ((left->flags ^ right->flags) &
  1769. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
  1770. return false;
  1771. return memcmp(&left->outer_vid, &right->outer_vid,
  1772. sizeof(struct efx_filter_spec) -
  1773. offsetof(struct efx_filter_spec, outer_vid)) == 0;
  1774. }
  1775. static unsigned int efx_ef10_filter_hash(const struct efx_filter_spec *spec)
  1776. {
  1777. BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
  1778. return jhash2((const u32 *)&spec->outer_vid,
  1779. (sizeof(struct efx_filter_spec) -
  1780. offsetof(struct efx_filter_spec, outer_vid)) / 4,
  1781. 0);
  1782. /* XXX should we randomise the initval? */
  1783. }
  1784. /* Decide whether a filter should be exclusive or else should allow
  1785. * delivery to additional recipients. Currently we decide that
  1786. * filters for specific local unicast MAC and IP addresses are
  1787. * exclusive.
  1788. */
  1789. static bool efx_ef10_filter_is_exclusive(const struct efx_filter_spec *spec)
  1790. {
  1791. if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC &&
  1792. !is_multicast_ether_addr(spec->loc_mac))
  1793. return true;
  1794. if ((spec->match_flags &
  1795. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
  1796. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
  1797. if (spec->ether_type == htons(ETH_P_IP) &&
  1798. !ipv4_is_multicast(spec->loc_host[0]))
  1799. return true;
  1800. if (spec->ether_type == htons(ETH_P_IPV6) &&
  1801. ((const u8 *)spec->loc_host)[0] != 0xff)
  1802. return true;
  1803. }
  1804. return false;
  1805. }
  1806. static struct efx_filter_spec *
  1807. efx_ef10_filter_entry_spec(const struct efx_ef10_filter_table *table,
  1808. unsigned int filter_idx)
  1809. {
  1810. return (struct efx_filter_spec *)(table->entry[filter_idx].spec &
  1811. ~EFX_EF10_FILTER_FLAGS);
  1812. }
  1813. static unsigned int
  1814. efx_ef10_filter_entry_flags(const struct efx_ef10_filter_table *table,
  1815. unsigned int filter_idx)
  1816. {
  1817. return table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAGS;
  1818. }
  1819. static void
  1820. efx_ef10_filter_set_entry(struct efx_ef10_filter_table *table,
  1821. unsigned int filter_idx,
  1822. const struct efx_filter_spec *spec,
  1823. unsigned int flags)
  1824. {
  1825. table->entry[filter_idx].spec = (unsigned long)spec | flags;
  1826. }
  1827. static void efx_ef10_filter_push_prep(struct efx_nic *efx,
  1828. const struct efx_filter_spec *spec,
  1829. efx_dword_t *inbuf, u64 handle,
  1830. bool replacing)
  1831. {
  1832. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1833. memset(inbuf, 0, MC_CMD_FILTER_OP_IN_LEN);
  1834. if (replacing) {
  1835. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  1836. MC_CMD_FILTER_OP_IN_OP_REPLACE);
  1837. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE, handle);
  1838. } else {
  1839. u32 match_fields = 0;
  1840. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  1841. efx_ef10_filter_is_exclusive(spec) ?
  1842. MC_CMD_FILTER_OP_IN_OP_INSERT :
  1843. MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE);
  1844. /* Convert match flags and values. Unlike almost
  1845. * everything else in MCDI, these fields are in
  1846. * network byte order.
  1847. */
  1848. if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC_IG)
  1849. match_fields |=
  1850. is_multicast_ether_addr(spec->loc_mac) ?
  1851. 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST_LBN :
  1852. 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
  1853. #define COPY_FIELD(gen_flag, gen_field, mcdi_field) \
  1854. if (spec->match_flags & EFX_FILTER_MATCH_ ## gen_flag) { \
  1855. match_fields |= \
  1856. 1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
  1857. mcdi_field ## _LBN; \
  1858. BUILD_BUG_ON( \
  1859. MC_CMD_FILTER_OP_IN_ ## mcdi_field ## _LEN < \
  1860. sizeof(spec->gen_field)); \
  1861. memcpy(MCDI_PTR(inbuf, FILTER_OP_IN_ ## mcdi_field), \
  1862. &spec->gen_field, sizeof(spec->gen_field)); \
  1863. }
  1864. COPY_FIELD(REM_HOST, rem_host, SRC_IP);
  1865. COPY_FIELD(LOC_HOST, loc_host, DST_IP);
  1866. COPY_FIELD(REM_MAC, rem_mac, SRC_MAC);
  1867. COPY_FIELD(REM_PORT, rem_port, SRC_PORT);
  1868. COPY_FIELD(LOC_MAC, loc_mac, DST_MAC);
  1869. COPY_FIELD(LOC_PORT, loc_port, DST_PORT);
  1870. COPY_FIELD(ETHER_TYPE, ether_type, ETHER_TYPE);
  1871. COPY_FIELD(INNER_VID, inner_vid, INNER_VLAN);
  1872. COPY_FIELD(OUTER_VID, outer_vid, OUTER_VLAN);
  1873. COPY_FIELD(IP_PROTO, ip_proto, IP_PROTO);
  1874. #undef COPY_FIELD
  1875. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_MATCH_FIELDS,
  1876. match_fields);
  1877. }
  1878. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
  1879. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_DEST,
  1880. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
  1881. MC_CMD_FILTER_OP_IN_RX_DEST_DROP :
  1882. MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
  1883. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DEST,
  1884. MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
  1885. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_QUEUE, spec->dmaq_id);
  1886. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_MODE,
  1887. (spec->flags & EFX_FILTER_FLAG_RX_RSS) ?
  1888. MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
  1889. MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
  1890. if (spec->flags & EFX_FILTER_FLAG_RX_RSS)
  1891. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_CONTEXT,
  1892. spec->rss_context !=
  1893. EFX_FILTER_RSS_CONTEXT_DEFAULT ?
  1894. spec->rss_context : nic_data->rx_rss_context);
  1895. }
  1896. static int efx_ef10_filter_push(struct efx_nic *efx,
  1897. const struct efx_filter_spec *spec,
  1898. u64 *handle, bool replacing)
  1899. {
  1900. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  1901. MCDI_DECLARE_BUF(outbuf, MC_CMD_FILTER_OP_OUT_LEN);
  1902. int rc;
  1903. efx_ef10_filter_push_prep(efx, spec, inbuf, *handle, replacing);
  1904. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  1905. outbuf, sizeof(outbuf), NULL);
  1906. if (rc == 0)
  1907. *handle = MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
  1908. return rc;
  1909. }
  1910. static int efx_ef10_filter_rx_match_pri(struct efx_ef10_filter_table *table,
  1911. enum efx_filter_match_flags match_flags)
  1912. {
  1913. unsigned int match_pri;
  1914. for (match_pri = 0;
  1915. match_pri < table->rx_match_count;
  1916. match_pri++)
  1917. if (table->rx_match_flags[match_pri] == match_flags)
  1918. return match_pri;
  1919. return -EPROTONOSUPPORT;
  1920. }
  1921. static s32 efx_ef10_filter_insert(struct efx_nic *efx,
  1922. struct efx_filter_spec *spec,
  1923. bool replace_equal)
  1924. {
  1925. struct efx_ef10_filter_table *table = efx->filter_state;
  1926. DECLARE_BITMAP(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
  1927. struct efx_filter_spec *saved_spec;
  1928. unsigned int match_pri, hash;
  1929. unsigned int priv_flags;
  1930. bool replacing = false;
  1931. int ins_index = -1;
  1932. DEFINE_WAIT(wait);
  1933. bool is_mc_recip;
  1934. s32 rc;
  1935. /* For now, only support RX filters */
  1936. if ((spec->flags & (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)) !=
  1937. EFX_FILTER_FLAG_RX)
  1938. return -EINVAL;
  1939. rc = efx_ef10_filter_rx_match_pri(table, spec->match_flags);
  1940. if (rc < 0)
  1941. return rc;
  1942. match_pri = rc;
  1943. hash = efx_ef10_filter_hash(spec);
  1944. is_mc_recip = efx_filter_is_mc_recipient(spec);
  1945. if (is_mc_recip)
  1946. bitmap_zero(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
  1947. /* Find any existing filters with the same match tuple or
  1948. * else a free slot to insert at. If any of them are busy,
  1949. * we have to wait and retry.
  1950. */
  1951. for (;;) {
  1952. unsigned int depth = 1;
  1953. unsigned int i;
  1954. spin_lock_bh(&efx->filter_lock);
  1955. for (;;) {
  1956. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  1957. saved_spec = efx_ef10_filter_entry_spec(table, i);
  1958. if (!saved_spec) {
  1959. if (ins_index < 0)
  1960. ins_index = i;
  1961. } else if (efx_ef10_filter_equal(spec, saved_spec)) {
  1962. if (table->entry[i].spec &
  1963. EFX_EF10_FILTER_FLAG_BUSY)
  1964. break;
  1965. if (spec->priority < saved_spec->priority &&
  1966. !(saved_spec->priority ==
  1967. EFX_FILTER_PRI_REQUIRED &&
  1968. saved_spec->flags &
  1969. EFX_FILTER_FLAG_RX_STACK)) {
  1970. rc = -EPERM;
  1971. goto out_unlock;
  1972. }
  1973. if (!is_mc_recip) {
  1974. /* This is the only one */
  1975. if (spec->priority ==
  1976. saved_spec->priority &&
  1977. !replace_equal) {
  1978. rc = -EEXIST;
  1979. goto out_unlock;
  1980. }
  1981. ins_index = i;
  1982. goto found;
  1983. } else if (spec->priority >
  1984. saved_spec->priority ||
  1985. (spec->priority ==
  1986. saved_spec->priority &&
  1987. replace_equal)) {
  1988. if (ins_index < 0)
  1989. ins_index = i;
  1990. else
  1991. __set_bit(depth, mc_rem_map);
  1992. }
  1993. }
  1994. /* Once we reach the maximum search depth, use
  1995. * the first suitable slot or return -EBUSY if
  1996. * there was none
  1997. */
  1998. if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
  1999. if (ins_index < 0) {
  2000. rc = -EBUSY;
  2001. goto out_unlock;
  2002. }
  2003. goto found;
  2004. }
  2005. ++depth;
  2006. }
  2007. prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
  2008. spin_unlock_bh(&efx->filter_lock);
  2009. schedule();
  2010. }
  2011. found:
  2012. /* Create a software table entry if necessary, and mark it
  2013. * busy. We might yet fail to insert, but any attempt to
  2014. * insert a conflicting filter while we're waiting for the
  2015. * firmware must find the busy entry.
  2016. */
  2017. saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
  2018. if (saved_spec) {
  2019. if (spec->flags & EFX_FILTER_FLAG_RX_STACK) {
  2020. /* Just make sure it won't be removed */
  2021. saved_spec->flags |= EFX_FILTER_FLAG_RX_STACK;
  2022. table->entry[ins_index].spec &=
  2023. ~EFX_EF10_FILTER_FLAG_STACK_OLD;
  2024. rc = ins_index;
  2025. goto out_unlock;
  2026. }
  2027. replacing = true;
  2028. priv_flags = efx_ef10_filter_entry_flags(table, ins_index);
  2029. } else {
  2030. saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
  2031. if (!saved_spec) {
  2032. rc = -ENOMEM;
  2033. goto out_unlock;
  2034. }
  2035. *saved_spec = *spec;
  2036. priv_flags = 0;
  2037. }
  2038. efx_ef10_filter_set_entry(table, ins_index, saved_spec,
  2039. priv_flags | EFX_EF10_FILTER_FLAG_BUSY);
  2040. /* Mark lower-priority multicast recipients busy prior to removal */
  2041. if (is_mc_recip) {
  2042. unsigned int depth, i;
  2043. for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
  2044. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2045. if (test_bit(depth, mc_rem_map))
  2046. table->entry[i].spec |=
  2047. EFX_EF10_FILTER_FLAG_BUSY;
  2048. }
  2049. }
  2050. spin_unlock_bh(&efx->filter_lock);
  2051. rc = efx_ef10_filter_push(efx, spec, &table->entry[ins_index].handle,
  2052. replacing);
  2053. /* Finalise the software table entry */
  2054. spin_lock_bh(&efx->filter_lock);
  2055. if (rc == 0) {
  2056. if (replacing) {
  2057. /* Update the fields that may differ */
  2058. saved_spec->priority = spec->priority;
  2059. saved_spec->flags &= EFX_FILTER_FLAG_RX_STACK;
  2060. saved_spec->flags |= spec->flags;
  2061. saved_spec->rss_context = spec->rss_context;
  2062. saved_spec->dmaq_id = spec->dmaq_id;
  2063. }
  2064. } else if (!replacing) {
  2065. kfree(saved_spec);
  2066. saved_spec = NULL;
  2067. }
  2068. efx_ef10_filter_set_entry(table, ins_index, saved_spec, priv_flags);
  2069. /* Remove and finalise entries for lower-priority multicast
  2070. * recipients
  2071. */
  2072. if (is_mc_recip) {
  2073. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2074. unsigned int depth, i;
  2075. memset(inbuf, 0, sizeof(inbuf));
  2076. for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
  2077. if (!test_bit(depth, mc_rem_map))
  2078. continue;
  2079. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2080. saved_spec = efx_ef10_filter_entry_spec(table, i);
  2081. priv_flags = efx_ef10_filter_entry_flags(table, i);
  2082. if (rc == 0) {
  2083. spin_unlock_bh(&efx->filter_lock);
  2084. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2085. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2086. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2087. table->entry[i].handle);
  2088. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
  2089. inbuf, sizeof(inbuf),
  2090. NULL, 0, NULL);
  2091. spin_lock_bh(&efx->filter_lock);
  2092. }
  2093. if (rc == 0) {
  2094. kfree(saved_spec);
  2095. saved_spec = NULL;
  2096. priv_flags = 0;
  2097. } else {
  2098. priv_flags &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2099. }
  2100. efx_ef10_filter_set_entry(table, i, saved_spec,
  2101. priv_flags);
  2102. }
  2103. }
  2104. /* If successful, return the inserted filter ID */
  2105. if (rc == 0)
  2106. rc = match_pri * HUNT_FILTER_TBL_ROWS + ins_index;
  2107. wake_up_all(&table->waitq);
  2108. out_unlock:
  2109. spin_unlock_bh(&efx->filter_lock);
  2110. finish_wait(&table->waitq, &wait);
  2111. return rc;
  2112. }
  2113. static void efx_ef10_filter_update_rx_scatter(struct efx_nic *efx)
  2114. {
  2115. /* no need to do anything here on EF10 */
  2116. }
  2117. /* Remove a filter.
  2118. * If !stack_requested, remove by ID
  2119. * If stack_requested, remove by index
  2120. * Filter ID may come from userland and must be range-checked.
  2121. */
  2122. static int efx_ef10_filter_remove_internal(struct efx_nic *efx,
  2123. enum efx_filter_priority priority,
  2124. u32 filter_id, bool stack_requested)
  2125. {
  2126. unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
  2127. struct efx_ef10_filter_table *table = efx->filter_state;
  2128. MCDI_DECLARE_BUF(inbuf,
  2129. MC_CMD_FILTER_OP_IN_HANDLE_OFST +
  2130. MC_CMD_FILTER_OP_IN_HANDLE_LEN);
  2131. struct efx_filter_spec *spec;
  2132. DEFINE_WAIT(wait);
  2133. int rc;
  2134. /* Find the software table entry and mark it busy. Don't
  2135. * remove it yet; any attempt to update while we're waiting
  2136. * for the firmware must find the busy entry.
  2137. */
  2138. for (;;) {
  2139. spin_lock_bh(&efx->filter_lock);
  2140. if (!(table->entry[filter_idx].spec &
  2141. EFX_EF10_FILTER_FLAG_BUSY))
  2142. break;
  2143. prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
  2144. spin_unlock_bh(&efx->filter_lock);
  2145. schedule();
  2146. }
  2147. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2148. if (!spec || spec->priority > priority ||
  2149. (!stack_requested &&
  2150. efx_ef10_filter_rx_match_pri(table, spec->match_flags) !=
  2151. filter_id / HUNT_FILTER_TBL_ROWS)) {
  2152. rc = -ENOENT;
  2153. goto out_unlock;
  2154. }
  2155. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  2156. spin_unlock_bh(&efx->filter_lock);
  2157. if (spec->flags & EFX_FILTER_FLAG_RX_STACK && !stack_requested) {
  2158. /* Reset steering of a stack-owned filter */
  2159. struct efx_filter_spec new_spec = *spec;
  2160. new_spec.priority = EFX_FILTER_PRI_REQUIRED;
  2161. new_spec.flags = (EFX_FILTER_FLAG_RX |
  2162. EFX_FILTER_FLAG_RX_RSS |
  2163. EFX_FILTER_FLAG_RX_STACK);
  2164. new_spec.dmaq_id = 0;
  2165. new_spec.rss_context = EFX_FILTER_RSS_CONTEXT_DEFAULT;
  2166. rc = efx_ef10_filter_push(efx, &new_spec,
  2167. &table->entry[filter_idx].handle,
  2168. true);
  2169. spin_lock_bh(&efx->filter_lock);
  2170. if (rc == 0)
  2171. *spec = new_spec;
  2172. } else {
  2173. /* Really remove the filter */
  2174. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2175. efx_ef10_filter_is_exclusive(spec) ?
  2176. MC_CMD_FILTER_OP_IN_OP_REMOVE :
  2177. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2178. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2179. table->entry[filter_idx].handle);
  2180. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
  2181. inbuf, sizeof(inbuf), NULL, 0, NULL);
  2182. spin_lock_bh(&efx->filter_lock);
  2183. if (rc == 0) {
  2184. kfree(spec);
  2185. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  2186. }
  2187. }
  2188. table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2189. wake_up_all(&table->waitq);
  2190. out_unlock:
  2191. spin_unlock_bh(&efx->filter_lock);
  2192. finish_wait(&table->waitq, &wait);
  2193. return rc;
  2194. }
  2195. static int efx_ef10_filter_remove_safe(struct efx_nic *efx,
  2196. enum efx_filter_priority priority,
  2197. u32 filter_id)
  2198. {
  2199. return efx_ef10_filter_remove_internal(efx, priority, filter_id, false);
  2200. }
  2201. static int efx_ef10_filter_get_safe(struct efx_nic *efx,
  2202. enum efx_filter_priority priority,
  2203. u32 filter_id, struct efx_filter_spec *spec)
  2204. {
  2205. unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
  2206. struct efx_ef10_filter_table *table = efx->filter_state;
  2207. const struct efx_filter_spec *saved_spec;
  2208. int rc;
  2209. spin_lock_bh(&efx->filter_lock);
  2210. saved_spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2211. if (saved_spec && saved_spec->priority == priority &&
  2212. efx_ef10_filter_rx_match_pri(table, saved_spec->match_flags) ==
  2213. filter_id / HUNT_FILTER_TBL_ROWS) {
  2214. *spec = *saved_spec;
  2215. rc = 0;
  2216. } else {
  2217. rc = -ENOENT;
  2218. }
  2219. spin_unlock_bh(&efx->filter_lock);
  2220. return rc;
  2221. }
  2222. static void efx_ef10_filter_clear_rx(struct efx_nic *efx,
  2223. enum efx_filter_priority priority)
  2224. {
  2225. /* TODO */
  2226. }
  2227. static u32 efx_ef10_filter_count_rx_used(struct efx_nic *efx,
  2228. enum efx_filter_priority priority)
  2229. {
  2230. struct efx_ef10_filter_table *table = efx->filter_state;
  2231. unsigned int filter_idx;
  2232. s32 count = 0;
  2233. spin_lock_bh(&efx->filter_lock);
  2234. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2235. if (table->entry[filter_idx].spec &&
  2236. efx_ef10_filter_entry_spec(table, filter_idx)->priority ==
  2237. priority)
  2238. ++count;
  2239. }
  2240. spin_unlock_bh(&efx->filter_lock);
  2241. return count;
  2242. }
  2243. static u32 efx_ef10_filter_get_rx_id_limit(struct efx_nic *efx)
  2244. {
  2245. struct efx_ef10_filter_table *table = efx->filter_state;
  2246. return table->rx_match_count * HUNT_FILTER_TBL_ROWS;
  2247. }
  2248. static s32 efx_ef10_filter_get_rx_ids(struct efx_nic *efx,
  2249. enum efx_filter_priority priority,
  2250. u32 *buf, u32 size)
  2251. {
  2252. struct efx_ef10_filter_table *table = efx->filter_state;
  2253. struct efx_filter_spec *spec;
  2254. unsigned int filter_idx;
  2255. s32 count = 0;
  2256. spin_lock_bh(&efx->filter_lock);
  2257. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2258. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2259. if (spec && spec->priority == priority) {
  2260. if (count == size) {
  2261. count = -EMSGSIZE;
  2262. break;
  2263. }
  2264. buf[count++] = (efx_ef10_filter_rx_match_pri(
  2265. table, spec->match_flags) *
  2266. HUNT_FILTER_TBL_ROWS +
  2267. filter_idx);
  2268. }
  2269. }
  2270. spin_unlock_bh(&efx->filter_lock);
  2271. return count;
  2272. }
  2273. #ifdef CONFIG_RFS_ACCEL
  2274. static efx_mcdi_async_completer efx_ef10_filter_rfs_insert_complete;
  2275. static s32 efx_ef10_filter_rfs_insert(struct efx_nic *efx,
  2276. struct efx_filter_spec *spec)
  2277. {
  2278. struct efx_ef10_filter_table *table = efx->filter_state;
  2279. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2280. struct efx_filter_spec *saved_spec;
  2281. unsigned int hash, i, depth = 1;
  2282. bool replacing = false;
  2283. int ins_index = -1;
  2284. u64 cookie;
  2285. s32 rc;
  2286. /* Must be an RX filter without RSS and not for a multicast
  2287. * destination address (RFS only works for connected sockets).
  2288. * These restrictions allow us to pass only a tiny amount of
  2289. * data through to the completion function.
  2290. */
  2291. EFX_WARN_ON_PARANOID(spec->flags !=
  2292. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_RX_SCATTER));
  2293. EFX_WARN_ON_PARANOID(spec->priority != EFX_FILTER_PRI_HINT);
  2294. EFX_WARN_ON_PARANOID(efx_filter_is_mc_recipient(spec));
  2295. hash = efx_ef10_filter_hash(spec);
  2296. spin_lock_bh(&efx->filter_lock);
  2297. /* Find any existing filter with the same match tuple or else
  2298. * a free slot to insert at. If an existing filter is busy,
  2299. * we have to give up.
  2300. */
  2301. for (;;) {
  2302. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2303. saved_spec = efx_ef10_filter_entry_spec(table, i);
  2304. if (!saved_spec) {
  2305. if (ins_index < 0)
  2306. ins_index = i;
  2307. } else if (efx_ef10_filter_equal(spec, saved_spec)) {
  2308. if (table->entry[i].spec & EFX_EF10_FILTER_FLAG_BUSY) {
  2309. rc = -EBUSY;
  2310. goto fail_unlock;
  2311. }
  2312. EFX_WARN_ON_PARANOID(saved_spec->flags &
  2313. EFX_FILTER_FLAG_RX_STACK);
  2314. if (spec->priority < saved_spec->priority) {
  2315. rc = -EPERM;
  2316. goto fail_unlock;
  2317. }
  2318. ins_index = i;
  2319. break;
  2320. }
  2321. /* Once we reach the maximum search depth, use the
  2322. * first suitable slot or return -EBUSY if there was
  2323. * none
  2324. */
  2325. if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
  2326. if (ins_index < 0) {
  2327. rc = -EBUSY;
  2328. goto fail_unlock;
  2329. }
  2330. break;
  2331. }
  2332. ++depth;
  2333. }
  2334. /* Create a software table entry if necessary, and mark it
  2335. * busy. We might yet fail to insert, but any attempt to
  2336. * insert a conflicting filter while we're waiting for the
  2337. * firmware must find the busy entry.
  2338. */
  2339. saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
  2340. if (saved_spec) {
  2341. replacing = true;
  2342. } else {
  2343. saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
  2344. if (!saved_spec) {
  2345. rc = -ENOMEM;
  2346. goto fail_unlock;
  2347. }
  2348. *saved_spec = *spec;
  2349. }
  2350. efx_ef10_filter_set_entry(table, ins_index, saved_spec,
  2351. EFX_EF10_FILTER_FLAG_BUSY);
  2352. spin_unlock_bh(&efx->filter_lock);
  2353. /* Pack up the variables needed on completion */
  2354. cookie = replacing << 31 | ins_index << 16 | spec->dmaq_id;
  2355. efx_ef10_filter_push_prep(efx, spec, inbuf,
  2356. table->entry[ins_index].handle, replacing);
  2357. efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  2358. MC_CMD_FILTER_OP_OUT_LEN,
  2359. efx_ef10_filter_rfs_insert_complete, cookie);
  2360. return ins_index;
  2361. fail_unlock:
  2362. spin_unlock_bh(&efx->filter_lock);
  2363. return rc;
  2364. }
  2365. static void
  2366. efx_ef10_filter_rfs_insert_complete(struct efx_nic *efx, unsigned long cookie,
  2367. int rc, efx_dword_t *outbuf,
  2368. size_t outlen_actual)
  2369. {
  2370. struct efx_ef10_filter_table *table = efx->filter_state;
  2371. unsigned int ins_index, dmaq_id;
  2372. struct efx_filter_spec *spec;
  2373. bool replacing;
  2374. /* Unpack the cookie */
  2375. replacing = cookie >> 31;
  2376. ins_index = (cookie >> 16) & (HUNT_FILTER_TBL_ROWS - 1);
  2377. dmaq_id = cookie & 0xffff;
  2378. spin_lock_bh(&efx->filter_lock);
  2379. spec = efx_ef10_filter_entry_spec(table, ins_index);
  2380. if (rc == 0) {
  2381. table->entry[ins_index].handle =
  2382. MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
  2383. if (replacing)
  2384. spec->dmaq_id = dmaq_id;
  2385. } else if (!replacing) {
  2386. kfree(spec);
  2387. spec = NULL;
  2388. }
  2389. efx_ef10_filter_set_entry(table, ins_index, spec, 0);
  2390. spin_unlock_bh(&efx->filter_lock);
  2391. wake_up_all(&table->waitq);
  2392. }
  2393. static void
  2394. efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
  2395. unsigned long filter_idx,
  2396. int rc, efx_dword_t *outbuf,
  2397. size_t outlen_actual);
  2398. static bool efx_ef10_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
  2399. unsigned int filter_idx)
  2400. {
  2401. struct efx_ef10_filter_table *table = efx->filter_state;
  2402. struct efx_filter_spec *spec =
  2403. efx_ef10_filter_entry_spec(table, filter_idx);
  2404. MCDI_DECLARE_BUF(inbuf,
  2405. MC_CMD_FILTER_OP_IN_HANDLE_OFST +
  2406. MC_CMD_FILTER_OP_IN_HANDLE_LEN);
  2407. if (!spec ||
  2408. (table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAG_BUSY) ||
  2409. spec->priority != EFX_FILTER_PRI_HINT ||
  2410. !rps_may_expire_flow(efx->net_dev, spec->dmaq_id,
  2411. flow_id, filter_idx))
  2412. return false;
  2413. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2414. MC_CMD_FILTER_OP_IN_OP_REMOVE);
  2415. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2416. table->entry[filter_idx].handle);
  2417. if (efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf), 0,
  2418. efx_ef10_filter_rfs_expire_complete, filter_idx))
  2419. return false;
  2420. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  2421. return true;
  2422. }
  2423. static void
  2424. efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
  2425. unsigned long filter_idx,
  2426. int rc, efx_dword_t *outbuf,
  2427. size_t outlen_actual)
  2428. {
  2429. struct efx_ef10_filter_table *table = efx->filter_state;
  2430. struct efx_filter_spec *spec =
  2431. efx_ef10_filter_entry_spec(table, filter_idx);
  2432. spin_lock_bh(&efx->filter_lock);
  2433. if (rc == 0) {
  2434. kfree(spec);
  2435. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  2436. }
  2437. table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2438. wake_up_all(&table->waitq);
  2439. spin_unlock_bh(&efx->filter_lock);
  2440. }
  2441. #endif /* CONFIG_RFS_ACCEL */
  2442. static int efx_ef10_filter_match_flags_from_mcdi(u32 mcdi_flags)
  2443. {
  2444. int match_flags = 0;
  2445. #define MAP_FLAG(gen_flag, mcdi_field) { \
  2446. u32 old_mcdi_flags = mcdi_flags; \
  2447. mcdi_flags &= ~(1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
  2448. mcdi_field ## _LBN); \
  2449. if (mcdi_flags != old_mcdi_flags) \
  2450. match_flags |= EFX_FILTER_MATCH_ ## gen_flag; \
  2451. }
  2452. MAP_FLAG(LOC_MAC_IG, UNKNOWN_UCAST_DST);
  2453. MAP_FLAG(LOC_MAC_IG, UNKNOWN_MCAST_DST);
  2454. MAP_FLAG(REM_HOST, SRC_IP);
  2455. MAP_FLAG(LOC_HOST, DST_IP);
  2456. MAP_FLAG(REM_MAC, SRC_MAC);
  2457. MAP_FLAG(REM_PORT, SRC_PORT);
  2458. MAP_FLAG(LOC_MAC, DST_MAC);
  2459. MAP_FLAG(LOC_PORT, DST_PORT);
  2460. MAP_FLAG(ETHER_TYPE, ETHER_TYPE);
  2461. MAP_FLAG(INNER_VID, INNER_VLAN);
  2462. MAP_FLAG(OUTER_VID, OUTER_VLAN);
  2463. MAP_FLAG(IP_PROTO, IP_PROTO);
  2464. #undef MAP_FLAG
  2465. /* Did we map them all? */
  2466. if (mcdi_flags)
  2467. return -EINVAL;
  2468. return match_flags;
  2469. }
  2470. static int efx_ef10_filter_table_probe(struct efx_nic *efx)
  2471. {
  2472. MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN);
  2473. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
  2474. unsigned int pd_match_pri, pd_match_count;
  2475. struct efx_ef10_filter_table *table;
  2476. size_t outlen;
  2477. int rc;
  2478. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2479. if (!table)
  2480. return -ENOMEM;
  2481. /* Find out which RX filter types are supported, and their priorities */
  2482. MCDI_SET_DWORD(inbuf, GET_PARSER_DISP_INFO_IN_OP,
  2483. MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
  2484. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PARSER_DISP_INFO,
  2485. inbuf, sizeof(inbuf), outbuf, sizeof(outbuf),
  2486. &outlen);
  2487. if (rc)
  2488. goto fail;
  2489. pd_match_count = MCDI_VAR_ARRAY_LEN(
  2490. outlen, GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES);
  2491. table->rx_match_count = 0;
  2492. for (pd_match_pri = 0; pd_match_pri < pd_match_count; pd_match_pri++) {
  2493. u32 mcdi_flags =
  2494. MCDI_ARRAY_DWORD(
  2495. outbuf,
  2496. GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES,
  2497. pd_match_pri);
  2498. rc = efx_ef10_filter_match_flags_from_mcdi(mcdi_flags);
  2499. if (rc < 0) {
  2500. netif_dbg(efx, probe, efx->net_dev,
  2501. "%s: fw flags %#x pri %u not supported in driver\n",
  2502. __func__, mcdi_flags, pd_match_pri);
  2503. } else {
  2504. netif_dbg(efx, probe, efx->net_dev,
  2505. "%s: fw flags %#x pri %u supported as driver flags %#x pri %u\n",
  2506. __func__, mcdi_flags, pd_match_pri,
  2507. rc, table->rx_match_count);
  2508. table->rx_match_flags[table->rx_match_count++] = rc;
  2509. }
  2510. }
  2511. table->entry = vzalloc(HUNT_FILTER_TBL_ROWS * sizeof(*table->entry));
  2512. if (!table->entry) {
  2513. rc = -ENOMEM;
  2514. goto fail;
  2515. }
  2516. efx->filter_state = table;
  2517. init_waitqueue_head(&table->waitq);
  2518. return 0;
  2519. fail:
  2520. kfree(table);
  2521. return rc;
  2522. }
  2523. static void efx_ef10_filter_table_restore(struct efx_nic *efx)
  2524. {
  2525. struct efx_ef10_filter_table *table = efx->filter_state;
  2526. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  2527. struct efx_filter_spec *spec;
  2528. unsigned int filter_idx;
  2529. bool failed = false;
  2530. int rc;
  2531. if (!nic_data->must_restore_filters)
  2532. return;
  2533. spin_lock_bh(&efx->filter_lock);
  2534. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2535. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2536. if (!spec)
  2537. continue;
  2538. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  2539. spin_unlock_bh(&efx->filter_lock);
  2540. rc = efx_ef10_filter_push(efx, spec,
  2541. &table->entry[filter_idx].handle,
  2542. false);
  2543. if (rc)
  2544. failed = true;
  2545. spin_lock_bh(&efx->filter_lock);
  2546. if (rc) {
  2547. kfree(spec);
  2548. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  2549. } else {
  2550. table->entry[filter_idx].spec &=
  2551. ~EFX_EF10_FILTER_FLAG_BUSY;
  2552. }
  2553. }
  2554. spin_unlock_bh(&efx->filter_lock);
  2555. if (failed)
  2556. netif_err(efx, hw, efx->net_dev,
  2557. "unable to restore all filters\n");
  2558. else
  2559. nic_data->must_restore_filters = false;
  2560. }
  2561. static void efx_ef10_filter_table_remove(struct efx_nic *efx)
  2562. {
  2563. struct efx_ef10_filter_table *table = efx->filter_state;
  2564. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2565. struct efx_filter_spec *spec;
  2566. unsigned int filter_idx;
  2567. int rc;
  2568. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2569. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2570. if (!spec)
  2571. continue;
  2572. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2573. efx_ef10_filter_is_exclusive(spec) ?
  2574. MC_CMD_FILTER_OP_IN_OP_REMOVE :
  2575. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2576. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2577. table->entry[filter_idx].handle);
  2578. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  2579. NULL, 0, NULL);
  2580. WARN_ON(rc != 0);
  2581. kfree(spec);
  2582. }
  2583. vfree(table->entry);
  2584. kfree(table);
  2585. }
  2586. static void efx_ef10_filter_sync_rx_mode(struct efx_nic *efx)
  2587. {
  2588. struct efx_ef10_filter_table *table = efx->filter_state;
  2589. struct net_device *net_dev = efx->net_dev;
  2590. struct efx_filter_spec spec;
  2591. bool remove_failed = false;
  2592. struct netdev_hw_addr *uc;
  2593. struct netdev_hw_addr *mc;
  2594. unsigned int filter_idx;
  2595. int i, n, rc;
  2596. if (!efx_dev_registered(efx))
  2597. return;
  2598. /* Mark old filters that may need to be removed */
  2599. spin_lock_bh(&efx->filter_lock);
  2600. n = table->stack_uc_count < 0 ? 1 : table->stack_uc_count;
  2601. for (i = 0; i < n; i++) {
  2602. filter_idx = table->stack_uc_list[i].id % HUNT_FILTER_TBL_ROWS;
  2603. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_STACK_OLD;
  2604. }
  2605. n = table->stack_mc_count < 0 ? 1 : table->stack_mc_count;
  2606. for (i = 0; i < n; i++) {
  2607. filter_idx = table->stack_mc_list[i].id % HUNT_FILTER_TBL_ROWS;
  2608. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_STACK_OLD;
  2609. }
  2610. spin_unlock_bh(&efx->filter_lock);
  2611. /* Copy/convert the address lists; add the primary station
  2612. * address and broadcast address
  2613. */
  2614. netif_addr_lock_bh(net_dev);
  2615. if (net_dev->flags & IFF_PROMISC ||
  2616. netdev_uc_count(net_dev) >= EFX_EF10_FILTER_STACK_UC_MAX) {
  2617. table->stack_uc_count = -1;
  2618. } else {
  2619. table->stack_uc_count = 1 + netdev_uc_count(net_dev);
  2620. memcpy(table->stack_uc_list[0].addr, net_dev->dev_addr,
  2621. ETH_ALEN);
  2622. i = 1;
  2623. netdev_for_each_uc_addr(uc, net_dev) {
  2624. memcpy(table->stack_uc_list[i].addr,
  2625. uc->addr, ETH_ALEN);
  2626. i++;
  2627. }
  2628. }
  2629. if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI) ||
  2630. netdev_mc_count(net_dev) >= EFX_EF10_FILTER_STACK_MC_MAX) {
  2631. table->stack_mc_count = -1;
  2632. } else {
  2633. table->stack_mc_count = 1 + netdev_mc_count(net_dev);
  2634. eth_broadcast_addr(table->stack_mc_list[0].addr);
  2635. i = 1;
  2636. netdev_for_each_mc_addr(mc, net_dev) {
  2637. memcpy(table->stack_mc_list[i].addr,
  2638. mc->addr, ETH_ALEN);
  2639. i++;
  2640. }
  2641. }
  2642. netif_addr_unlock_bh(net_dev);
  2643. /* Insert/renew unicast filters */
  2644. if (table->stack_uc_count >= 0) {
  2645. for (i = 0; i < table->stack_uc_count; i++) {
  2646. efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
  2647. EFX_FILTER_FLAG_RX_RSS |
  2648. EFX_FILTER_FLAG_RX_STACK,
  2649. 0);
  2650. efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
  2651. table->stack_uc_list[i].addr);
  2652. rc = efx_ef10_filter_insert(efx, &spec, true);
  2653. if (rc < 0) {
  2654. /* Fall back to unicast-promisc */
  2655. while (i--)
  2656. efx_ef10_filter_remove_safe(
  2657. efx, EFX_FILTER_PRI_REQUIRED,
  2658. table->stack_uc_list[i].id);
  2659. table->stack_uc_count = -1;
  2660. break;
  2661. }
  2662. table->stack_uc_list[i].id = rc;
  2663. }
  2664. }
  2665. if (table->stack_uc_count < 0) {
  2666. efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
  2667. EFX_FILTER_FLAG_RX_RSS |
  2668. EFX_FILTER_FLAG_RX_STACK,
  2669. 0);
  2670. efx_filter_set_uc_def(&spec);
  2671. rc = efx_ef10_filter_insert(efx, &spec, true);
  2672. if (rc < 0) {
  2673. WARN_ON(1);
  2674. table->stack_uc_count = 0;
  2675. } else {
  2676. table->stack_uc_list[0].id = rc;
  2677. }
  2678. }
  2679. /* Insert/renew multicast filters */
  2680. if (table->stack_mc_count >= 0) {
  2681. for (i = 0; i < table->stack_mc_count; i++) {
  2682. efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
  2683. EFX_FILTER_FLAG_RX_RSS |
  2684. EFX_FILTER_FLAG_RX_STACK,
  2685. 0);
  2686. efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
  2687. table->stack_mc_list[i].addr);
  2688. rc = efx_ef10_filter_insert(efx, &spec, true);
  2689. if (rc < 0) {
  2690. /* Fall back to multicast-promisc */
  2691. while (i--)
  2692. efx_ef10_filter_remove_safe(
  2693. efx, EFX_FILTER_PRI_REQUIRED,
  2694. table->stack_mc_list[i].id);
  2695. table->stack_mc_count = -1;
  2696. break;
  2697. }
  2698. table->stack_mc_list[i].id = rc;
  2699. }
  2700. }
  2701. if (table->stack_mc_count < 0) {
  2702. efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
  2703. EFX_FILTER_FLAG_RX_RSS |
  2704. EFX_FILTER_FLAG_RX_STACK,
  2705. 0);
  2706. efx_filter_set_mc_def(&spec);
  2707. rc = efx_ef10_filter_insert(efx, &spec, true);
  2708. if (rc < 0) {
  2709. WARN_ON(1);
  2710. table->stack_mc_count = 0;
  2711. } else {
  2712. table->stack_mc_list[0].id = rc;
  2713. }
  2714. }
  2715. /* Remove filters that weren't renewed. Since nothing else
  2716. * changes the STACK_OLD flag or removes these filters, we
  2717. * don't need to hold the filter_lock while scanning for
  2718. * these filters.
  2719. */
  2720. for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
  2721. if (ACCESS_ONCE(table->entry[i].spec) &
  2722. EFX_EF10_FILTER_FLAG_STACK_OLD) {
  2723. if (efx_ef10_filter_remove_internal(efx,
  2724. EFX_FILTER_PRI_REQUIRED,
  2725. i, true) < 0)
  2726. remove_failed = true;
  2727. }
  2728. }
  2729. WARN_ON(remove_failed);
  2730. }
  2731. static int efx_ef10_mac_reconfigure(struct efx_nic *efx)
  2732. {
  2733. efx_ef10_filter_sync_rx_mode(efx);
  2734. return efx_mcdi_set_mac(efx);
  2735. }
  2736. #ifdef CONFIG_SFC_MTD
  2737. struct efx_ef10_nvram_type_info {
  2738. u16 type, type_mask;
  2739. u8 port;
  2740. const char *name;
  2741. };
  2742. static const struct efx_ef10_nvram_type_info efx_ef10_nvram_types[] = {
  2743. { NVRAM_PARTITION_TYPE_MC_FIRMWARE, 0, 0, "sfc_mcfw" },
  2744. { NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 0, 0, "sfc_mcfw_backup" },
  2745. { NVRAM_PARTITION_TYPE_EXPANSION_ROM, 0, 0, "sfc_exp_rom" },
  2746. { NVRAM_PARTITION_TYPE_STATIC_CONFIG, 0, 0, "sfc_static_cfg" },
  2747. { NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 0, 0, "sfc_dynamic_cfg" },
  2748. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 0, 0, "sfc_exp_rom_cfg" },
  2749. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT1, 0, 1, "sfc_exp_rom_cfg" },
  2750. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT2, 0, 2, "sfc_exp_rom_cfg" },
  2751. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT3, 0, 3, "sfc_exp_rom_cfg" },
  2752. { NVRAM_PARTITION_TYPE_PHY_MIN, 0xff, 0, "sfc_phy_fw" },
  2753. };
  2754. static int efx_ef10_mtd_probe_partition(struct efx_nic *efx,
  2755. struct efx_mcdi_mtd_partition *part,
  2756. unsigned int type)
  2757. {
  2758. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_METADATA_IN_LEN);
  2759. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_METADATA_OUT_LENMAX);
  2760. const struct efx_ef10_nvram_type_info *info;
  2761. size_t size, erase_size, outlen;
  2762. bool protected;
  2763. int rc;
  2764. for (info = efx_ef10_nvram_types; ; info++) {
  2765. if (info ==
  2766. efx_ef10_nvram_types + ARRAY_SIZE(efx_ef10_nvram_types))
  2767. return -ENODEV;
  2768. if ((type & ~info->type_mask) == info->type)
  2769. break;
  2770. }
  2771. if (info->port != efx_port_num(efx))
  2772. return -ENODEV;
  2773. rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
  2774. if (rc)
  2775. return rc;
  2776. if (protected)
  2777. return -ENODEV; /* hide it */
  2778. part->nvram_type = type;
  2779. MCDI_SET_DWORD(inbuf, NVRAM_METADATA_IN_TYPE, type);
  2780. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_METADATA, inbuf, sizeof(inbuf),
  2781. outbuf, sizeof(outbuf), &outlen);
  2782. if (rc)
  2783. return rc;
  2784. if (outlen < MC_CMD_NVRAM_METADATA_OUT_LENMIN)
  2785. return -EIO;
  2786. if (MCDI_DWORD(outbuf, NVRAM_METADATA_OUT_FLAGS) &
  2787. (1 << MC_CMD_NVRAM_METADATA_OUT_SUBTYPE_VALID_LBN))
  2788. part->fw_subtype = MCDI_DWORD(outbuf,
  2789. NVRAM_METADATA_OUT_SUBTYPE);
  2790. part->common.dev_type_name = "EF10 NVRAM manager";
  2791. part->common.type_name = info->name;
  2792. part->common.mtd.type = MTD_NORFLASH;
  2793. part->common.mtd.flags = MTD_CAP_NORFLASH;
  2794. part->common.mtd.size = size;
  2795. part->common.mtd.erasesize = erase_size;
  2796. return 0;
  2797. }
  2798. static int efx_ef10_mtd_probe(struct efx_nic *efx)
  2799. {
  2800. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_PARTITIONS_OUT_LENMAX);
  2801. struct efx_mcdi_mtd_partition *parts;
  2802. size_t outlen, n_parts_total, i, n_parts;
  2803. unsigned int type;
  2804. int rc;
  2805. ASSERT_RTNL();
  2806. BUILD_BUG_ON(MC_CMD_NVRAM_PARTITIONS_IN_LEN != 0);
  2807. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_PARTITIONS, NULL, 0,
  2808. outbuf, sizeof(outbuf), &outlen);
  2809. if (rc)
  2810. return rc;
  2811. if (outlen < MC_CMD_NVRAM_PARTITIONS_OUT_LENMIN)
  2812. return -EIO;
  2813. n_parts_total = MCDI_DWORD(outbuf, NVRAM_PARTITIONS_OUT_NUM_PARTITIONS);
  2814. if (n_parts_total >
  2815. MCDI_VAR_ARRAY_LEN(outlen, NVRAM_PARTITIONS_OUT_TYPE_ID))
  2816. return -EIO;
  2817. parts = kcalloc(n_parts_total, sizeof(*parts), GFP_KERNEL);
  2818. if (!parts)
  2819. return -ENOMEM;
  2820. n_parts = 0;
  2821. for (i = 0; i < n_parts_total; i++) {
  2822. type = MCDI_ARRAY_DWORD(outbuf, NVRAM_PARTITIONS_OUT_TYPE_ID,
  2823. i);
  2824. rc = efx_ef10_mtd_probe_partition(efx, &parts[n_parts], type);
  2825. if (rc == 0)
  2826. n_parts++;
  2827. else if (rc != -ENODEV)
  2828. goto fail;
  2829. }
  2830. rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));
  2831. fail:
  2832. if (rc)
  2833. kfree(parts);
  2834. return rc;
  2835. }
  2836. #endif /* CONFIG_SFC_MTD */
  2837. static void efx_ef10_ptp_write_host_time(struct efx_nic *efx, u32 host_time)
  2838. {
  2839. _efx_writed(efx, cpu_to_le32(host_time), ER_DZ_MC_DB_LWRD);
  2840. }
  2841. const struct efx_nic_type efx_hunt_a0_nic_type = {
  2842. .mem_map_size = efx_ef10_mem_map_size,
  2843. .probe = efx_ef10_probe,
  2844. .remove = efx_ef10_remove,
  2845. .dimension_resources = efx_ef10_dimension_resources,
  2846. .init = efx_ef10_init_nic,
  2847. .fini = efx_port_dummy_op_void,
  2848. .map_reset_reason = efx_mcdi_map_reset_reason,
  2849. .map_reset_flags = efx_ef10_map_reset_flags,
  2850. .reset = efx_mcdi_reset,
  2851. .probe_port = efx_mcdi_port_probe,
  2852. .remove_port = efx_mcdi_port_remove,
  2853. .fini_dmaq = efx_ef10_fini_dmaq,
  2854. .describe_stats = efx_ef10_describe_stats,
  2855. .update_stats = efx_ef10_update_stats,
  2856. .start_stats = efx_mcdi_mac_start_stats,
  2857. .stop_stats = efx_mcdi_mac_stop_stats,
  2858. .set_id_led = efx_mcdi_set_id_led,
  2859. .push_irq_moderation = efx_ef10_push_irq_moderation,
  2860. .reconfigure_mac = efx_ef10_mac_reconfigure,
  2861. .check_mac_fault = efx_mcdi_mac_check_fault,
  2862. .reconfigure_port = efx_mcdi_port_reconfigure,
  2863. .get_wol = efx_ef10_get_wol,
  2864. .set_wol = efx_ef10_set_wol,
  2865. .resume_wol = efx_port_dummy_op_void,
  2866. /* TODO: test_chip */
  2867. .test_nvram = efx_mcdi_nvram_test_all,
  2868. .mcdi_request = efx_ef10_mcdi_request,
  2869. .mcdi_poll_response = efx_ef10_mcdi_poll_response,
  2870. .mcdi_read_response = efx_ef10_mcdi_read_response,
  2871. .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
  2872. .irq_enable_master = efx_port_dummy_op_void,
  2873. .irq_test_generate = efx_ef10_irq_test_generate,
  2874. .irq_disable_non_ev = efx_port_dummy_op_void,
  2875. .irq_handle_msi = efx_ef10_msi_interrupt,
  2876. .irq_handle_legacy = efx_ef10_legacy_interrupt,
  2877. .tx_probe = efx_ef10_tx_probe,
  2878. .tx_init = efx_ef10_tx_init,
  2879. .tx_remove = efx_ef10_tx_remove,
  2880. .tx_write = efx_ef10_tx_write,
  2881. .rx_push_indir_table = efx_ef10_rx_push_indir_table,
  2882. .rx_probe = efx_ef10_rx_probe,
  2883. .rx_init = efx_ef10_rx_init,
  2884. .rx_remove = efx_ef10_rx_remove,
  2885. .rx_write = efx_ef10_rx_write,
  2886. .rx_defer_refill = efx_ef10_rx_defer_refill,
  2887. .ev_probe = efx_ef10_ev_probe,
  2888. .ev_init = efx_ef10_ev_init,
  2889. .ev_fini = efx_ef10_ev_fini,
  2890. .ev_remove = efx_ef10_ev_remove,
  2891. .ev_process = efx_ef10_ev_process,
  2892. .ev_read_ack = efx_ef10_ev_read_ack,
  2893. .ev_test_generate = efx_ef10_ev_test_generate,
  2894. .filter_table_probe = efx_ef10_filter_table_probe,
  2895. .filter_table_restore = efx_ef10_filter_table_restore,
  2896. .filter_table_remove = efx_ef10_filter_table_remove,
  2897. .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
  2898. .filter_insert = efx_ef10_filter_insert,
  2899. .filter_remove_safe = efx_ef10_filter_remove_safe,
  2900. .filter_get_safe = efx_ef10_filter_get_safe,
  2901. .filter_clear_rx = efx_ef10_filter_clear_rx,
  2902. .filter_count_rx_used = efx_ef10_filter_count_rx_used,
  2903. .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
  2904. .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
  2905. #ifdef CONFIG_RFS_ACCEL
  2906. .filter_rfs_insert = efx_ef10_filter_rfs_insert,
  2907. .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
  2908. #endif
  2909. #ifdef CONFIG_SFC_MTD
  2910. .mtd_probe = efx_ef10_mtd_probe,
  2911. .mtd_rename = efx_mcdi_mtd_rename,
  2912. .mtd_read = efx_mcdi_mtd_read,
  2913. .mtd_erase = efx_mcdi_mtd_erase,
  2914. .mtd_write = efx_mcdi_mtd_write,
  2915. .mtd_sync = efx_mcdi_mtd_sync,
  2916. #endif
  2917. .ptp_write_host_time = efx_ef10_ptp_write_host_time,
  2918. .revision = EFX_REV_HUNT_A0,
  2919. .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
  2920. .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
  2921. .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
  2922. .can_rx_scatter = true,
  2923. .always_rx_scatter = true,
  2924. .max_interrupt_mode = EFX_INT_MODE_MSIX,
  2925. .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
  2926. .offload_features = (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2927. NETIF_F_RXHASH | NETIF_F_NTUPLE),
  2928. .mcdi_max_ver = 2,
  2929. .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
  2930. };