ef10.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370
  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. };
  651. #define HUNT_COMMON_STAT_MASK ((1ULL << EF10_STAT_tx_bytes) | \
  652. (1ULL << EF10_STAT_tx_packets) | \
  653. (1ULL << EF10_STAT_tx_pause) | \
  654. (1ULL << EF10_STAT_tx_unicast) | \
  655. (1ULL << EF10_STAT_tx_multicast) | \
  656. (1ULL << EF10_STAT_tx_broadcast) | \
  657. (1ULL << EF10_STAT_rx_bytes) | \
  658. (1ULL << EF10_STAT_rx_bytes_minus_good_bytes) | \
  659. (1ULL << EF10_STAT_rx_good_bytes) | \
  660. (1ULL << EF10_STAT_rx_bad_bytes) | \
  661. (1ULL << EF10_STAT_rx_packets) | \
  662. (1ULL << EF10_STAT_rx_good) | \
  663. (1ULL << EF10_STAT_rx_bad) | \
  664. (1ULL << EF10_STAT_rx_pause) | \
  665. (1ULL << EF10_STAT_rx_control) | \
  666. (1ULL << EF10_STAT_rx_unicast) | \
  667. (1ULL << EF10_STAT_rx_multicast) | \
  668. (1ULL << EF10_STAT_rx_broadcast) | \
  669. (1ULL << EF10_STAT_rx_lt64) | \
  670. (1ULL << EF10_STAT_rx_64) | \
  671. (1ULL << EF10_STAT_rx_65_to_127) | \
  672. (1ULL << EF10_STAT_rx_128_to_255) | \
  673. (1ULL << EF10_STAT_rx_256_to_511) | \
  674. (1ULL << EF10_STAT_rx_512_to_1023) | \
  675. (1ULL << EF10_STAT_rx_1024_to_15xx) | \
  676. (1ULL << EF10_STAT_rx_15xx_to_jumbo) | \
  677. (1ULL << EF10_STAT_rx_gtjumbo) | \
  678. (1ULL << EF10_STAT_rx_bad_gtjumbo) | \
  679. (1ULL << EF10_STAT_rx_overflow) | \
  680. (1ULL << EF10_STAT_rx_nodesc_drops))
  681. /* These statistics are only provided by the 10G MAC. For a 10G/40G
  682. * switchable port we do not expose these because they might not
  683. * include all the packets they should.
  684. */
  685. #define HUNT_10G_ONLY_STAT_MASK ((1ULL << EF10_STAT_tx_control) | \
  686. (1ULL << EF10_STAT_tx_lt64) | \
  687. (1ULL << EF10_STAT_tx_64) | \
  688. (1ULL << EF10_STAT_tx_65_to_127) | \
  689. (1ULL << EF10_STAT_tx_128_to_255) | \
  690. (1ULL << EF10_STAT_tx_256_to_511) | \
  691. (1ULL << EF10_STAT_tx_512_to_1023) | \
  692. (1ULL << EF10_STAT_tx_1024_to_15xx) | \
  693. (1ULL << EF10_STAT_tx_15xx_to_jumbo))
  694. /* These statistics are only provided by the 40G MAC. For a 10G/40G
  695. * switchable port we do expose these because the errors will otherwise
  696. * be silent.
  697. */
  698. #define HUNT_40G_EXTRA_STAT_MASK ((1ULL << EF10_STAT_rx_align_error) | \
  699. (1ULL << EF10_STAT_rx_length_error))
  700. #if BITS_PER_LONG == 64
  701. #define STAT_MASK_BITMAP(bits) (bits)
  702. #else
  703. #define STAT_MASK_BITMAP(bits) (bits) & 0xffffffff, (bits) >> 32
  704. #endif
  705. static const unsigned long *efx_ef10_stat_mask(struct efx_nic *efx)
  706. {
  707. static const unsigned long hunt_40g_stat_mask[] = {
  708. STAT_MASK_BITMAP(HUNT_COMMON_STAT_MASK |
  709. HUNT_40G_EXTRA_STAT_MASK)
  710. };
  711. static const unsigned long hunt_10g_only_stat_mask[] = {
  712. STAT_MASK_BITMAP(HUNT_COMMON_STAT_MASK |
  713. HUNT_10G_ONLY_STAT_MASK)
  714. };
  715. u32 port_caps = efx_mcdi_phy_get_caps(efx);
  716. if (port_caps & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
  717. return hunt_40g_stat_mask;
  718. else
  719. return hunt_10g_only_stat_mask;
  720. }
  721. static size_t efx_ef10_describe_stats(struct efx_nic *efx, u8 *names)
  722. {
  723. return efx_nic_describe_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
  724. efx_ef10_stat_mask(efx), names);
  725. }
  726. static int efx_ef10_try_update_nic_stats(struct efx_nic *efx)
  727. {
  728. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  729. const unsigned long *stats_mask = efx_ef10_stat_mask(efx);
  730. __le64 generation_start, generation_end;
  731. u64 *stats = nic_data->stats;
  732. __le64 *dma_stats;
  733. dma_stats = efx->stats_buffer.addr;
  734. nic_data = efx->nic_data;
  735. generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
  736. if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
  737. return 0;
  738. rmb();
  739. efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, stats_mask,
  740. stats, efx->stats_buffer.addr, false);
  741. generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
  742. if (generation_end != generation_start)
  743. return -EAGAIN;
  744. /* Update derived statistics */
  745. stats[EF10_STAT_rx_good_bytes] =
  746. stats[EF10_STAT_rx_bytes] -
  747. stats[EF10_STAT_rx_bytes_minus_good_bytes];
  748. efx_update_diff_stat(&stats[EF10_STAT_rx_bad_bytes],
  749. stats[EF10_STAT_rx_bytes_minus_good_bytes]);
  750. return 0;
  751. }
  752. static size_t efx_ef10_update_stats(struct efx_nic *efx, u64 *full_stats,
  753. struct rtnl_link_stats64 *core_stats)
  754. {
  755. const unsigned long *mask = efx_ef10_stat_mask(efx);
  756. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  757. u64 *stats = nic_data->stats;
  758. size_t stats_count = 0, index;
  759. int retry;
  760. /* If we're unlucky enough to read statistics during the DMA, wait
  761. * up to 10ms for it to finish (typically takes <500us)
  762. */
  763. for (retry = 0; retry < 100; ++retry) {
  764. if (efx_ef10_try_update_nic_stats(efx) == 0)
  765. break;
  766. udelay(100);
  767. }
  768. if (full_stats) {
  769. for_each_set_bit(index, mask, EF10_STAT_COUNT) {
  770. if (efx_ef10_stat_desc[index].name) {
  771. *full_stats++ = stats[index];
  772. ++stats_count;
  773. }
  774. }
  775. }
  776. if (core_stats) {
  777. core_stats->rx_packets = stats[EF10_STAT_rx_packets];
  778. core_stats->tx_packets = stats[EF10_STAT_tx_packets];
  779. core_stats->rx_bytes = stats[EF10_STAT_rx_bytes];
  780. core_stats->tx_bytes = stats[EF10_STAT_tx_bytes];
  781. core_stats->rx_dropped = stats[EF10_STAT_rx_nodesc_drops];
  782. core_stats->multicast = stats[EF10_STAT_rx_multicast];
  783. core_stats->rx_length_errors =
  784. stats[EF10_STAT_rx_gtjumbo] +
  785. stats[EF10_STAT_rx_length_error];
  786. core_stats->rx_crc_errors = stats[EF10_STAT_rx_bad];
  787. core_stats->rx_frame_errors = stats[EF10_STAT_rx_align_error];
  788. core_stats->rx_fifo_errors = stats[EF10_STAT_rx_overflow];
  789. core_stats->rx_errors = (core_stats->rx_length_errors +
  790. core_stats->rx_crc_errors +
  791. core_stats->rx_frame_errors);
  792. }
  793. return stats_count;
  794. }
  795. static void efx_ef10_push_irq_moderation(struct efx_channel *channel)
  796. {
  797. struct efx_nic *efx = channel->efx;
  798. unsigned int mode, value;
  799. efx_dword_t timer_cmd;
  800. if (channel->irq_moderation) {
  801. mode = 3;
  802. value = channel->irq_moderation - 1;
  803. } else {
  804. mode = 0;
  805. value = 0;
  806. }
  807. if (EFX_EF10_WORKAROUND_35388(efx)) {
  808. EFX_POPULATE_DWORD_3(timer_cmd, ERF_DD_EVQ_IND_TIMER_FLAGS,
  809. EFE_DD_EVQ_IND_TIMER_FLAGS,
  810. ERF_DD_EVQ_IND_TIMER_MODE, mode,
  811. ERF_DD_EVQ_IND_TIMER_VAL, value);
  812. efx_writed_page(efx, &timer_cmd, ER_DD_EVQ_INDIRECT,
  813. channel->channel);
  814. } else {
  815. EFX_POPULATE_DWORD_2(timer_cmd, ERF_DZ_TC_TIMER_MODE, mode,
  816. ERF_DZ_TC_TIMER_VAL, value);
  817. efx_writed_page(efx, &timer_cmd, ER_DZ_EVQ_TMR,
  818. channel->channel);
  819. }
  820. }
  821. static void efx_ef10_get_wol(struct efx_nic *efx, struct ethtool_wolinfo *wol)
  822. {
  823. wol->supported = 0;
  824. wol->wolopts = 0;
  825. memset(&wol->sopass, 0, sizeof(wol->sopass));
  826. }
  827. static int efx_ef10_set_wol(struct efx_nic *efx, u32 type)
  828. {
  829. if (type != 0)
  830. return -EINVAL;
  831. return 0;
  832. }
  833. static void efx_ef10_mcdi_request(struct efx_nic *efx,
  834. const efx_dword_t *hdr, size_t hdr_len,
  835. const efx_dword_t *sdu, size_t sdu_len)
  836. {
  837. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  838. u8 *pdu = nic_data->mcdi_buf.addr;
  839. memcpy(pdu, hdr, hdr_len);
  840. memcpy(pdu + hdr_len, sdu, sdu_len);
  841. wmb();
  842. /* The hardware provides 'low' and 'high' (doorbell) registers
  843. * for passing the 64-bit address of an MCDI request to
  844. * firmware. However the dwords are swapped by firmware. The
  845. * least significant bits of the doorbell are then 0 for all
  846. * MCDI requests due to alignment.
  847. */
  848. _efx_writed(efx, cpu_to_le32((u64)nic_data->mcdi_buf.dma_addr >> 32),
  849. ER_DZ_MC_DB_LWRD);
  850. _efx_writed(efx, cpu_to_le32((u32)nic_data->mcdi_buf.dma_addr),
  851. ER_DZ_MC_DB_HWRD);
  852. }
  853. static bool efx_ef10_mcdi_poll_response(struct efx_nic *efx)
  854. {
  855. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  856. const efx_dword_t hdr = *(const efx_dword_t *)nic_data->mcdi_buf.addr;
  857. rmb();
  858. return EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE);
  859. }
  860. static void
  861. efx_ef10_mcdi_read_response(struct efx_nic *efx, efx_dword_t *outbuf,
  862. size_t offset, size_t outlen)
  863. {
  864. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  865. const u8 *pdu = nic_data->mcdi_buf.addr;
  866. memcpy(outbuf, pdu + offset, outlen);
  867. }
  868. static int efx_ef10_mcdi_poll_reboot(struct efx_nic *efx)
  869. {
  870. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  871. int rc;
  872. rc = efx_ef10_get_warm_boot_count(efx);
  873. if (rc < 0) {
  874. /* The firmware is presumably in the process of
  875. * rebooting. However, we are supposed to report each
  876. * reboot just once, so we must only do that once we
  877. * can read and store the updated warm boot count.
  878. */
  879. return 0;
  880. }
  881. if (rc == nic_data->warm_boot_count)
  882. return 0;
  883. nic_data->warm_boot_count = rc;
  884. /* All our allocations have been reset */
  885. nic_data->must_realloc_vis = true;
  886. nic_data->must_restore_filters = true;
  887. nic_data->must_restore_piobufs = true;
  888. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  889. /* The datapath firmware might have been changed */
  890. nic_data->must_check_datapath_caps = true;
  891. /* MAC statistics have been cleared on the NIC; clear the local
  892. * statistic that we update with efx_update_diff_stat().
  893. */
  894. nic_data->stats[EF10_STAT_rx_bad_bytes] = 0;
  895. return -EIO;
  896. }
  897. /* Handle an MSI interrupt
  898. *
  899. * Handle an MSI hardware interrupt. This routine schedules event
  900. * queue processing. No interrupt acknowledgement cycle is necessary.
  901. * Also, we never need to check that the interrupt is for us, since
  902. * MSI interrupts cannot be shared.
  903. */
  904. static irqreturn_t efx_ef10_msi_interrupt(int irq, void *dev_id)
  905. {
  906. struct efx_msi_context *context = dev_id;
  907. struct efx_nic *efx = context->efx;
  908. netif_vdbg(efx, intr, efx->net_dev,
  909. "IRQ %d on CPU %d\n", irq, raw_smp_processor_id());
  910. if (likely(ACCESS_ONCE(efx->irq_soft_enabled))) {
  911. /* Note test interrupts */
  912. if (context->index == efx->irq_level)
  913. efx->last_irq_cpu = raw_smp_processor_id();
  914. /* Schedule processing of the channel */
  915. efx_schedule_channel_irq(efx->channel[context->index]);
  916. }
  917. return IRQ_HANDLED;
  918. }
  919. static irqreturn_t efx_ef10_legacy_interrupt(int irq, void *dev_id)
  920. {
  921. struct efx_nic *efx = dev_id;
  922. bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
  923. struct efx_channel *channel;
  924. efx_dword_t reg;
  925. u32 queues;
  926. /* Read the ISR which also ACKs the interrupts */
  927. efx_readd(efx, &reg, ER_DZ_BIU_INT_ISR);
  928. queues = EFX_DWORD_FIELD(reg, ERF_DZ_ISR_REG);
  929. if (queues == 0)
  930. return IRQ_NONE;
  931. if (likely(soft_enabled)) {
  932. /* Note test interrupts */
  933. if (queues & (1U << efx->irq_level))
  934. efx->last_irq_cpu = raw_smp_processor_id();
  935. efx_for_each_channel(channel, efx) {
  936. if (queues & 1)
  937. efx_schedule_channel_irq(channel);
  938. queues >>= 1;
  939. }
  940. }
  941. netif_vdbg(efx, intr, efx->net_dev,
  942. "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  943. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  944. return IRQ_HANDLED;
  945. }
  946. static void efx_ef10_irq_test_generate(struct efx_nic *efx)
  947. {
  948. MCDI_DECLARE_BUF(inbuf, MC_CMD_TRIGGER_INTERRUPT_IN_LEN);
  949. BUILD_BUG_ON(MC_CMD_TRIGGER_INTERRUPT_OUT_LEN != 0);
  950. MCDI_SET_DWORD(inbuf, TRIGGER_INTERRUPT_IN_INTR_LEVEL, efx->irq_level);
  951. (void) efx_mcdi_rpc(efx, MC_CMD_TRIGGER_INTERRUPT,
  952. inbuf, sizeof(inbuf), NULL, 0, NULL);
  953. }
  954. static int efx_ef10_tx_probe(struct efx_tx_queue *tx_queue)
  955. {
  956. return efx_nic_alloc_buffer(tx_queue->efx, &tx_queue->txd.buf,
  957. (tx_queue->ptr_mask + 1) *
  958. sizeof(efx_qword_t),
  959. GFP_KERNEL);
  960. }
  961. /* This writes to the TX_DESC_WPTR and also pushes data */
  962. static inline void efx_ef10_push_tx_desc(struct efx_tx_queue *tx_queue,
  963. const efx_qword_t *txd)
  964. {
  965. unsigned int write_ptr;
  966. efx_oword_t reg;
  967. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  968. EFX_POPULATE_OWORD_1(reg, ERF_DZ_TX_DESC_WPTR, write_ptr);
  969. reg.qword[0] = *txd;
  970. efx_writeo_page(tx_queue->efx, &reg,
  971. ER_DZ_TX_DESC_UPD, tx_queue->queue);
  972. }
  973. static void efx_ef10_tx_init(struct efx_tx_queue *tx_queue)
  974. {
  975. MCDI_DECLARE_BUF(inbuf, MC_CMD_INIT_TXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
  976. EFX_BUF_SIZE));
  977. MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_TXQ_OUT_LEN);
  978. bool csum_offload = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
  979. size_t entries = tx_queue->txd.buf.len / EFX_BUF_SIZE;
  980. struct efx_channel *channel = tx_queue->channel;
  981. struct efx_nic *efx = tx_queue->efx;
  982. size_t inlen, outlen;
  983. dma_addr_t dma_addr;
  984. efx_qword_t *txd;
  985. int rc;
  986. int i;
  987. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_SIZE, tx_queue->ptr_mask + 1);
  988. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_TARGET_EVQ, channel->channel);
  989. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_LABEL, tx_queue->queue);
  990. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_INSTANCE, tx_queue->queue);
  991. MCDI_POPULATE_DWORD_2(inbuf, INIT_TXQ_IN_FLAGS,
  992. INIT_TXQ_IN_FLAG_IP_CSUM_DIS, !csum_offload,
  993. INIT_TXQ_IN_FLAG_TCP_CSUM_DIS, !csum_offload);
  994. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_OWNER_ID, 0);
  995. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
  996. dma_addr = tx_queue->txd.buf.dma_addr;
  997. netif_dbg(efx, hw, efx->net_dev, "pushing TXQ %d. %zu entries (%llx)\n",
  998. tx_queue->queue, entries, (u64)dma_addr);
  999. for (i = 0; i < entries; ++i) {
  1000. MCDI_SET_ARRAY_QWORD(inbuf, INIT_TXQ_IN_DMA_ADDR, i, dma_addr);
  1001. dma_addr += EFX_BUF_SIZE;
  1002. }
  1003. inlen = MC_CMD_INIT_TXQ_IN_LEN(entries);
  1004. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_TXQ, inbuf, inlen,
  1005. outbuf, sizeof(outbuf), &outlen);
  1006. if (rc)
  1007. goto fail;
  1008. /* A previous user of this TX queue might have set us up the
  1009. * bomb by writing a descriptor to the TX push collector but
  1010. * not the doorbell. (Each collector belongs to a port, not a
  1011. * queue or function, so cannot easily be reset.) We must
  1012. * attempt to push a no-op descriptor in its place.
  1013. */
  1014. tx_queue->buffer[0].flags = EFX_TX_BUF_OPTION;
  1015. tx_queue->insert_count = 1;
  1016. txd = efx_tx_desc(tx_queue, 0);
  1017. EFX_POPULATE_QWORD_4(*txd,
  1018. ESF_DZ_TX_DESC_IS_OPT, true,
  1019. ESF_DZ_TX_OPTION_TYPE,
  1020. ESE_DZ_TX_OPTION_DESC_CRC_CSUM,
  1021. ESF_DZ_TX_OPTION_UDP_TCP_CSUM, csum_offload,
  1022. ESF_DZ_TX_OPTION_IP_CSUM, csum_offload);
  1023. tx_queue->write_count = 1;
  1024. wmb();
  1025. efx_ef10_push_tx_desc(tx_queue, txd);
  1026. return;
  1027. fail:
  1028. WARN_ON(true);
  1029. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1030. }
  1031. static void efx_ef10_tx_fini(struct efx_tx_queue *tx_queue)
  1032. {
  1033. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_TXQ_IN_LEN);
  1034. MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_TXQ_OUT_LEN);
  1035. struct efx_nic *efx = tx_queue->efx;
  1036. size_t outlen;
  1037. int rc;
  1038. MCDI_SET_DWORD(inbuf, FINI_TXQ_IN_INSTANCE,
  1039. tx_queue->queue);
  1040. rc = efx_mcdi_rpc(efx, MC_CMD_FINI_TXQ, inbuf, sizeof(inbuf),
  1041. outbuf, sizeof(outbuf), &outlen);
  1042. if (rc && rc != -EALREADY)
  1043. goto fail;
  1044. return;
  1045. fail:
  1046. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1047. }
  1048. static void efx_ef10_tx_remove(struct efx_tx_queue *tx_queue)
  1049. {
  1050. efx_nic_free_buffer(tx_queue->efx, &tx_queue->txd.buf);
  1051. }
  1052. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  1053. static inline void efx_ef10_notify_tx_desc(struct efx_tx_queue *tx_queue)
  1054. {
  1055. unsigned int write_ptr;
  1056. efx_dword_t reg;
  1057. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1058. EFX_POPULATE_DWORD_1(reg, ERF_DZ_TX_DESC_WPTR_DWORD, write_ptr);
  1059. efx_writed_page(tx_queue->efx, &reg,
  1060. ER_DZ_TX_DESC_UPD_DWORD, tx_queue->queue);
  1061. }
  1062. static void efx_ef10_tx_write(struct efx_tx_queue *tx_queue)
  1063. {
  1064. unsigned int old_write_count = tx_queue->write_count;
  1065. struct efx_tx_buffer *buffer;
  1066. unsigned int write_ptr;
  1067. efx_qword_t *txd;
  1068. BUG_ON(tx_queue->write_count == tx_queue->insert_count);
  1069. do {
  1070. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1071. buffer = &tx_queue->buffer[write_ptr];
  1072. txd = efx_tx_desc(tx_queue, write_ptr);
  1073. ++tx_queue->write_count;
  1074. /* Create TX descriptor ring entry */
  1075. if (buffer->flags & EFX_TX_BUF_OPTION) {
  1076. *txd = buffer->option;
  1077. } else {
  1078. BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
  1079. EFX_POPULATE_QWORD_3(
  1080. *txd,
  1081. ESF_DZ_TX_KER_CONT,
  1082. buffer->flags & EFX_TX_BUF_CONT,
  1083. ESF_DZ_TX_KER_BYTE_CNT, buffer->len,
  1084. ESF_DZ_TX_KER_BUF_ADDR, buffer->dma_addr);
  1085. }
  1086. } while (tx_queue->write_count != tx_queue->insert_count);
  1087. wmb(); /* Ensure descriptors are written before they are fetched */
  1088. if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
  1089. txd = efx_tx_desc(tx_queue,
  1090. old_write_count & tx_queue->ptr_mask);
  1091. efx_ef10_push_tx_desc(tx_queue, txd);
  1092. ++tx_queue->pushes;
  1093. } else {
  1094. efx_ef10_notify_tx_desc(tx_queue);
  1095. }
  1096. }
  1097. static int efx_ef10_alloc_rss_context(struct efx_nic *efx, u32 *context)
  1098. {
  1099. MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_ALLOC_IN_LEN);
  1100. MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN);
  1101. size_t outlen;
  1102. int rc;
  1103. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_UPSTREAM_PORT_ID,
  1104. EVB_PORT_ID_ASSIGNED);
  1105. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_TYPE,
  1106. MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_EXCLUSIVE);
  1107. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_NUM_QUEUES,
  1108. EFX_MAX_CHANNELS);
  1109. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_ALLOC, inbuf, sizeof(inbuf),
  1110. outbuf, sizeof(outbuf), &outlen);
  1111. if (rc != 0)
  1112. return rc;
  1113. if (outlen < MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN)
  1114. return -EIO;
  1115. *context = MCDI_DWORD(outbuf, RSS_CONTEXT_ALLOC_OUT_RSS_CONTEXT_ID);
  1116. return 0;
  1117. }
  1118. static void efx_ef10_free_rss_context(struct efx_nic *efx, u32 context)
  1119. {
  1120. MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_FREE_IN_LEN);
  1121. int rc;
  1122. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_FREE_IN_RSS_CONTEXT_ID,
  1123. context);
  1124. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_FREE, inbuf, sizeof(inbuf),
  1125. NULL, 0, NULL);
  1126. WARN_ON(rc != 0);
  1127. }
  1128. static int efx_ef10_populate_rss_table(struct efx_nic *efx, u32 context)
  1129. {
  1130. MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_SET_TABLE_IN_LEN);
  1131. MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_SET_KEY_IN_LEN);
  1132. int i, rc;
  1133. MCDI_SET_DWORD(tablebuf, RSS_CONTEXT_SET_TABLE_IN_RSS_CONTEXT_ID,
  1134. context);
  1135. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1136. MC_CMD_RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE_LEN);
  1137. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); ++i)
  1138. MCDI_PTR(tablebuf,
  1139. RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE)[i] =
  1140. (u8) efx->rx_indir_table[i];
  1141. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_TABLE, tablebuf,
  1142. sizeof(tablebuf), NULL, 0, NULL);
  1143. if (rc != 0)
  1144. return rc;
  1145. MCDI_SET_DWORD(keybuf, RSS_CONTEXT_SET_KEY_IN_RSS_CONTEXT_ID,
  1146. context);
  1147. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_hash_key) !=
  1148. MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
  1149. for (i = 0; i < ARRAY_SIZE(efx->rx_hash_key); ++i)
  1150. MCDI_PTR(keybuf, RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY)[i] =
  1151. efx->rx_hash_key[i];
  1152. return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_KEY, keybuf,
  1153. sizeof(keybuf), NULL, 0, NULL);
  1154. }
  1155. static void efx_ef10_rx_free_indir_table(struct efx_nic *efx)
  1156. {
  1157. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1158. if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
  1159. efx_ef10_free_rss_context(efx, nic_data->rx_rss_context);
  1160. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  1161. }
  1162. static void efx_ef10_rx_push_indir_table(struct efx_nic *efx)
  1163. {
  1164. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1165. int rc;
  1166. netif_dbg(efx, drv, efx->net_dev, "pushing RX indirection table\n");
  1167. if (nic_data->rx_rss_context == EFX_EF10_RSS_CONTEXT_INVALID) {
  1168. rc = efx_ef10_alloc_rss_context(efx, &nic_data->rx_rss_context);
  1169. if (rc != 0)
  1170. goto fail;
  1171. }
  1172. rc = efx_ef10_populate_rss_table(efx, nic_data->rx_rss_context);
  1173. if (rc != 0)
  1174. goto fail;
  1175. return;
  1176. fail:
  1177. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1178. }
  1179. static int efx_ef10_rx_probe(struct efx_rx_queue *rx_queue)
  1180. {
  1181. return efx_nic_alloc_buffer(rx_queue->efx, &rx_queue->rxd.buf,
  1182. (rx_queue->ptr_mask + 1) *
  1183. sizeof(efx_qword_t),
  1184. GFP_KERNEL);
  1185. }
  1186. static void efx_ef10_rx_init(struct efx_rx_queue *rx_queue)
  1187. {
  1188. MCDI_DECLARE_BUF(inbuf,
  1189. MC_CMD_INIT_RXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
  1190. EFX_BUF_SIZE));
  1191. MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_RXQ_OUT_LEN);
  1192. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  1193. size_t entries = rx_queue->rxd.buf.len / EFX_BUF_SIZE;
  1194. struct efx_nic *efx = rx_queue->efx;
  1195. size_t inlen, outlen;
  1196. dma_addr_t dma_addr;
  1197. int rc;
  1198. int i;
  1199. rx_queue->scatter_n = 0;
  1200. rx_queue->scatter_len = 0;
  1201. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_SIZE, rx_queue->ptr_mask + 1);
  1202. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_TARGET_EVQ, channel->channel);
  1203. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_LABEL, efx_rx_queue_index(rx_queue));
  1204. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_INSTANCE,
  1205. efx_rx_queue_index(rx_queue));
  1206. MCDI_POPULATE_DWORD_1(inbuf, INIT_RXQ_IN_FLAGS,
  1207. INIT_RXQ_IN_FLAG_PREFIX, 1);
  1208. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_OWNER_ID, 0);
  1209. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
  1210. dma_addr = rx_queue->rxd.buf.dma_addr;
  1211. netif_dbg(efx, hw, efx->net_dev, "pushing RXQ %d. %zu entries (%llx)\n",
  1212. efx_rx_queue_index(rx_queue), entries, (u64)dma_addr);
  1213. for (i = 0; i < entries; ++i) {
  1214. MCDI_SET_ARRAY_QWORD(inbuf, INIT_RXQ_IN_DMA_ADDR, i, dma_addr);
  1215. dma_addr += EFX_BUF_SIZE;
  1216. }
  1217. inlen = MC_CMD_INIT_RXQ_IN_LEN(entries);
  1218. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_RXQ, inbuf, inlen,
  1219. outbuf, sizeof(outbuf), &outlen);
  1220. if (rc)
  1221. goto fail;
  1222. return;
  1223. fail:
  1224. WARN_ON(true);
  1225. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1226. }
  1227. static void efx_ef10_rx_fini(struct efx_rx_queue *rx_queue)
  1228. {
  1229. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_RXQ_IN_LEN);
  1230. MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_RXQ_OUT_LEN);
  1231. struct efx_nic *efx = rx_queue->efx;
  1232. size_t outlen;
  1233. int rc;
  1234. MCDI_SET_DWORD(inbuf, FINI_RXQ_IN_INSTANCE,
  1235. efx_rx_queue_index(rx_queue));
  1236. rc = efx_mcdi_rpc(efx, MC_CMD_FINI_RXQ, inbuf, sizeof(inbuf),
  1237. outbuf, sizeof(outbuf), &outlen);
  1238. if (rc && rc != -EALREADY)
  1239. goto fail;
  1240. return;
  1241. fail:
  1242. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1243. }
  1244. static void efx_ef10_rx_remove(struct efx_rx_queue *rx_queue)
  1245. {
  1246. efx_nic_free_buffer(rx_queue->efx, &rx_queue->rxd.buf);
  1247. }
  1248. /* This creates an entry in the RX descriptor queue */
  1249. static inline void
  1250. efx_ef10_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
  1251. {
  1252. struct efx_rx_buffer *rx_buf;
  1253. efx_qword_t *rxd;
  1254. rxd = efx_rx_desc(rx_queue, index);
  1255. rx_buf = efx_rx_buffer(rx_queue, index);
  1256. EFX_POPULATE_QWORD_2(*rxd,
  1257. ESF_DZ_RX_KER_BYTE_CNT, rx_buf->len,
  1258. ESF_DZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
  1259. }
  1260. static void efx_ef10_rx_write(struct efx_rx_queue *rx_queue)
  1261. {
  1262. struct efx_nic *efx = rx_queue->efx;
  1263. unsigned int write_count;
  1264. efx_dword_t reg;
  1265. /* Firmware requires that RX_DESC_WPTR be a multiple of 8 */
  1266. write_count = rx_queue->added_count & ~7;
  1267. if (rx_queue->notified_count == write_count)
  1268. return;
  1269. do
  1270. efx_ef10_build_rx_desc(
  1271. rx_queue,
  1272. rx_queue->notified_count & rx_queue->ptr_mask);
  1273. while (++rx_queue->notified_count != write_count);
  1274. wmb();
  1275. EFX_POPULATE_DWORD_1(reg, ERF_DZ_RX_DESC_WPTR,
  1276. write_count & rx_queue->ptr_mask);
  1277. efx_writed_page(efx, &reg, ER_DZ_RX_DESC_UPD,
  1278. efx_rx_queue_index(rx_queue));
  1279. }
  1280. static efx_mcdi_async_completer efx_ef10_rx_defer_refill_complete;
  1281. static void efx_ef10_rx_defer_refill(struct efx_rx_queue *rx_queue)
  1282. {
  1283. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  1284. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
  1285. efx_qword_t event;
  1286. EFX_POPULATE_QWORD_2(event,
  1287. ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
  1288. ESF_DZ_EV_DATA, EFX_EF10_REFILL);
  1289. MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
  1290. /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
  1291. * already swapped the data to little-endian order.
  1292. */
  1293. memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
  1294. sizeof(efx_qword_t));
  1295. efx_mcdi_rpc_async(channel->efx, MC_CMD_DRIVER_EVENT,
  1296. inbuf, sizeof(inbuf), 0,
  1297. efx_ef10_rx_defer_refill_complete, 0);
  1298. }
  1299. static void
  1300. efx_ef10_rx_defer_refill_complete(struct efx_nic *efx, unsigned long cookie,
  1301. int rc, efx_dword_t *outbuf,
  1302. size_t outlen_actual)
  1303. {
  1304. /* nothing to do */
  1305. }
  1306. static int efx_ef10_ev_probe(struct efx_channel *channel)
  1307. {
  1308. return efx_nic_alloc_buffer(channel->efx, &channel->eventq.buf,
  1309. (channel->eventq_mask + 1) *
  1310. sizeof(efx_qword_t),
  1311. GFP_KERNEL);
  1312. }
  1313. static int efx_ef10_ev_init(struct efx_channel *channel)
  1314. {
  1315. MCDI_DECLARE_BUF(inbuf,
  1316. MC_CMD_INIT_EVQ_IN_LEN(EFX_MAX_EVQ_SIZE * 8 /
  1317. EFX_BUF_SIZE));
  1318. MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_EVQ_OUT_LEN);
  1319. size_t entries = channel->eventq.buf.len / EFX_BUF_SIZE;
  1320. struct efx_nic *efx = channel->efx;
  1321. struct efx_ef10_nic_data *nic_data;
  1322. bool supports_rx_merge;
  1323. size_t inlen, outlen;
  1324. dma_addr_t dma_addr;
  1325. int rc;
  1326. int i;
  1327. nic_data = efx->nic_data;
  1328. supports_rx_merge =
  1329. !!(nic_data->datapath_caps &
  1330. 1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN);
  1331. /* Fill event queue with all ones (i.e. empty events) */
  1332. memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
  1333. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_SIZE, channel->eventq_mask + 1);
  1334. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_INSTANCE, channel->channel);
  1335. /* INIT_EVQ expects index in vector table, not absolute */
  1336. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_IRQ_NUM, channel->channel);
  1337. MCDI_POPULATE_DWORD_4(inbuf, INIT_EVQ_IN_FLAGS,
  1338. INIT_EVQ_IN_FLAG_INTERRUPTING, 1,
  1339. INIT_EVQ_IN_FLAG_RX_MERGE, 1,
  1340. INIT_EVQ_IN_FLAG_TX_MERGE, 1,
  1341. INIT_EVQ_IN_FLAG_CUT_THRU, !supports_rx_merge);
  1342. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_MODE,
  1343. MC_CMD_INIT_EVQ_IN_TMR_MODE_DIS);
  1344. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_LOAD, 0);
  1345. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_RELOAD, 0);
  1346. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_MODE,
  1347. MC_CMD_INIT_EVQ_IN_COUNT_MODE_DIS);
  1348. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_THRSHLD, 0);
  1349. dma_addr = channel->eventq.buf.dma_addr;
  1350. for (i = 0; i < entries; ++i) {
  1351. MCDI_SET_ARRAY_QWORD(inbuf, INIT_EVQ_IN_DMA_ADDR, i, dma_addr);
  1352. dma_addr += EFX_BUF_SIZE;
  1353. }
  1354. inlen = MC_CMD_INIT_EVQ_IN_LEN(entries);
  1355. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_EVQ, inbuf, inlen,
  1356. outbuf, sizeof(outbuf), &outlen);
  1357. if (rc)
  1358. goto fail;
  1359. /* IRQ return is ignored */
  1360. return 0;
  1361. fail:
  1362. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1363. return rc;
  1364. }
  1365. static void efx_ef10_ev_fini(struct efx_channel *channel)
  1366. {
  1367. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_EVQ_IN_LEN);
  1368. MCDI_DECLARE_BUF(outbuf, MC_CMD_FINI_EVQ_OUT_LEN);
  1369. struct efx_nic *efx = channel->efx;
  1370. size_t outlen;
  1371. int rc;
  1372. MCDI_SET_DWORD(inbuf, FINI_EVQ_IN_INSTANCE, channel->channel);
  1373. rc = efx_mcdi_rpc(efx, MC_CMD_FINI_EVQ, inbuf, sizeof(inbuf),
  1374. outbuf, sizeof(outbuf), &outlen);
  1375. if (rc && rc != -EALREADY)
  1376. goto fail;
  1377. return;
  1378. fail:
  1379. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1380. }
  1381. static void efx_ef10_ev_remove(struct efx_channel *channel)
  1382. {
  1383. efx_nic_free_buffer(channel->efx, &channel->eventq.buf);
  1384. }
  1385. static void efx_ef10_handle_rx_wrong_queue(struct efx_rx_queue *rx_queue,
  1386. unsigned int rx_queue_label)
  1387. {
  1388. struct efx_nic *efx = rx_queue->efx;
  1389. netif_info(efx, hw, efx->net_dev,
  1390. "rx event arrived on queue %d labeled as queue %u\n",
  1391. efx_rx_queue_index(rx_queue), rx_queue_label);
  1392. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1393. }
  1394. static void
  1395. efx_ef10_handle_rx_bad_lbits(struct efx_rx_queue *rx_queue,
  1396. unsigned int actual, unsigned int expected)
  1397. {
  1398. unsigned int dropped = (actual - expected) & rx_queue->ptr_mask;
  1399. struct efx_nic *efx = rx_queue->efx;
  1400. netif_info(efx, hw, efx->net_dev,
  1401. "dropped %d events (index=%d expected=%d)\n",
  1402. dropped, actual, expected);
  1403. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1404. }
  1405. /* partially received RX was aborted. clean up. */
  1406. static void efx_ef10_handle_rx_abort(struct efx_rx_queue *rx_queue)
  1407. {
  1408. unsigned int rx_desc_ptr;
  1409. WARN_ON(rx_queue->scatter_n == 0);
  1410. netif_dbg(rx_queue->efx, hw, rx_queue->efx->net_dev,
  1411. "scattered RX aborted (dropping %u buffers)\n",
  1412. rx_queue->scatter_n);
  1413. rx_desc_ptr = rx_queue->removed_count & rx_queue->ptr_mask;
  1414. efx_rx_packet(rx_queue, rx_desc_ptr, rx_queue->scatter_n,
  1415. 0, EFX_RX_PKT_DISCARD);
  1416. rx_queue->removed_count += rx_queue->scatter_n;
  1417. rx_queue->scatter_n = 0;
  1418. rx_queue->scatter_len = 0;
  1419. ++efx_rx_queue_channel(rx_queue)->n_rx_nodesc_trunc;
  1420. }
  1421. static int efx_ef10_handle_rx_event(struct efx_channel *channel,
  1422. const efx_qword_t *event)
  1423. {
  1424. unsigned int rx_bytes, next_ptr_lbits, rx_queue_label, rx_l4_class;
  1425. unsigned int n_descs, n_packets, i;
  1426. struct efx_nic *efx = channel->efx;
  1427. struct efx_rx_queue *rx_queue;
  1428. bool rx_cont;
  1429. u16 flags = 0;
  1430. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  1431. return 0;
  1432. /* Basic packet information */
  1433. rx_bytes = EFX_QWORD_FIELD(*event, ESF_DZ_RX_BYTES);
  1434. next_ptr_lbits = EFX_QWORD_FIELD(*event, ESF_DZ_RX_DSC_PTR_LBITS);
  1435. rx_queue_label = EFX_QWORD_FIELD(*event, ESF_DZ_RX_QLABEL);
  1436. rx_l4_class = EFX_QWORD_FIELD(*event, ESF_DZ_RX_L4_CLASS);
  1437. rx_cont = EFX_QWORD_FIELD(*event, ESF_DZ_RX_CONT);
  1438. WARN_ON(EFX_QWORD_FIELD(*event, ESF_DZ_RX_DROP_EVENT));
  1439. rx_queue = efx_channel_get_rx_queue(channel);
  1440. if (unlikely(rx_queue_label != efx_rx_queue_index(rx_queue)))
  1441. efx_ef10_handle_rx_wrong_queue(rx_queue, rx_queue_label);
  1442. n_descs = ((next_ptr_lbits - rx_queue->removed_count) &
  1443. ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
  1444. if (n_descs != rx_queue->scatter_n + 1) {
  1445. /* detect rx abort */
  1446. if (unlikely(n_descs == rx_queue->scatter_n)) {
  1447. WARN_ON(rx_bytes != 0);
  1448. efx_ef10_handle_rx_abort(rx_queue);
  1449. return 0;
  1450. }
  1451. if (unlikely(rx_queue->scatter_n != 0)) {
  1452. /* Scattered packet completions cannot be
  1453. * merged, so something has gone wrong.
  1454. */
  1455. efx_ef10_handle_rx_bad_lbits(
  1456. rx_queue, next_ptr_lbits,
  1457. (rx_queue->removed_count +
  1458. rx_queue->scatter_n + 1) &
  1459. ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
  1460. return 0;
  1461. }
  1462. /* Merged completion for multiple non-scattered packets */
  1463. rx_queue->scatter_n = 1;
  1464. rx_queue->scatter_len = 0;
  1465. n_packets = n_descs;
  1466. ++channel->n_rx_merge_events;
  1467. channel->n_rx_merge_packets += n_packets;
  1468. flags |= EFX_RX_PKT_PREFIX_LEN;
  1469. } else {
  1470. ++rx_queue->scatter_n;
  1471. rx_queue->scatter_len += rx_bytes;
  1472. if (rx_cont)
  1473. return 0;
  1474. n_packets = 1;
  1475. }
  1476. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_ECRC_ERR)))
  1477. flags |= EFX_RX_PKT_DISCARD;
  1478. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_IPCKSUM_ERR))) {
  1479. channel->n_rx_ip_hdr_chksum_err += n_packets;
  1480. } else if (unlikely(EFX_QWORD_FIELD(*event,
  1481. ESF_DZ_RX_TCPUDP_CKSUM_ERR))) {
  1482. channel->n_rx_tcp_udp_chksum_err += n_packets;
  1483. } else if (rx_l4_class == ESE_DZ_L4_CLASS_TCP ||
  1484. rx_l4_class == ESE_DZ_L4_CLASS_UDP) {
  1485. flags |= EFX_RX_PKT_CSUMMED;
  1486. }
  1487. if (rx_l4_class == ESE_DZ_L4_CLASS_TCP)
  1488. flags |= EFX_RX_PKT_TCP;
  1489. channel->irq_mod_score += 2 * n_packets;
  1490. /* Handle received packet(s) */
  1491. for (i = 0; i < n_packets; i++) {
  1492. efx_rx_packet(rx_queue,
  1493. rx_queue->removed_count & rx_queue->ptr_mask,
  1494. rx_queue->scatter_n, rx_queue->scatter_len,
  1495. flags);
  1496. rx_queue->removed_count += rx_queue->scatter_n;
  1497. }
  1498. rx_queue->scatter_n = 0;
  1499. rx_queue->scatter_len = 0;
  1500. return n_packets;
  1501. }
  1502. static int
  1503. efx_ef10_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
  1504. {
  1505. struct efx_nic *efx = channel->efx;
  1506. struct efx_tx_queue *tx_queue;
  1507. unsigned int tx_ev_desc_ptr;
  1508. unsigned int tx_ev_q_label;
  1509. int tx_descs = 0;
  1510. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  1511. return 0;
  1512. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_TX_DROP_EVENT)))
  1513. return 0;
  1514. /* Transmit completion */
  1515. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, ESF_DZ_TX_DESCR_INDX);
  1516. tx_ev_q_label = EFX_QWORD_FIELD(*event, ESF_DZ_TX_QLABEL);
  1517. tx_queue = efx_channel_get_tx_queue(channel,
  1518. tx_ev_q_label % EFX_TXQ_TYPES);
  1519. tx_descs = ((tx_ev_desc_ptr + 1 - tx_queue->read_count) &
  1520. tx_queue->ptr_mask);
  1521. efx_xmit_done(tx_queue, tx_ev_desc_ptr & tx_queue->ptr_mask);
  1522. return tx_descs;
  1523. }
  1524. static void
  1525. efx_ef10_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  1526. {
  1527. struct efx_nic *efx = channel->efx;
  1528. int subcode;
  1529. subcode = EFX_QWORD_FIELD(*event, ESF_DZ_DRV_SUB_CODE);
  1530. switch (subcode) {
  1531. case ESE_DZ_DRV_TIMER_EV:
  1532. case ESE_DZ_DRV_WAKE_UP_EV:
  1533. break;
  1534. case ESE_DZ_DRV_START_UP_EV:
  1535. /* event queue init complete. ok. */
  1536. break;
  1537. default:
  1538. netif_err(efx, hw, efx->net_dev,
  1539. "channel %d unknown driver event type %d"
  1540. " (data " EFX_QWORD_FMT ")\n",
  1541. channel->channel, subcode,
  1542. EFX_QWORD_VAL(*event));
  1543. }
  1544. }
  1545. static void efx_ef10_handle_driver_generated_event(struct efx_channel *channel,
  1546. efx_qword_t *event)
  1547. {
  1548. struct efx_nic *efx = channel->efx;
  1549. u32 subcode;
  1550. subcode = EFX_QWORD_FIELD(*event, EFX_DWORD_0);
  1551. switch (subcode) {
  1552. case EFX_EF10_TEST:
  1553. channel->event_test_cpu = raw_smp_processor_id();
  1554. break;
  1555. case EFX_EF10_REFILL:
  1556. /* The queue must be empty, so we won't receive any rx
  1557. * events, so efx_process_channel() won't refill the
  1558. * queue. Refill it here
  1559. */
  1560. efx_fast_push_rx_descriptors(&channel->rx_queue);
  1561. break;
  1562. default:
  1563. netif_err(efx, hw, efx->net_dev,
  1564. "channel %d unknown driver event type %u"
  1565. " (data " EFX_QWORD_FMT ")\n",
  1566. channel->channel, (unsigned) subcode,
  1567. EFX_QWORD_VAL(*event));
  1568. }
  1569. }
  1570. static int efx_ef10_ev_process(struct efx_channel *channel, int quota)
  1571. {
  1572. struct efx_nic *efx = channel->efx;
  1573. efx_qword_t event, *p_event;
  1574. unsigned int read_ptr;
  1575. int ev_code;
  1576. int tx_descs = 0;
  1577. int spent = 0;
  1578. read_ptr = channel->eventq_read_ptr;
  1579. for (;;) {
  1580. p_event = efx_event(channel, read_ptr);
  1581. event = *p_event;
  1582. if (!efx_event_present(&event))
  1583. break;
  1584. EFX_SET_QWORD(*p_event);
  1585. ++read_ptr;
  1586. ev_code = EFX_QWORD_FIELD(event, ESF_DZ_EV_CODE);
  1587. netif_vdbg(efx, drv, efx->net_dev,
  1588. "processing event on %d " EFX_QWORD_FMT "\n",
  1589. channel->channel, EFX_QWORD_VAL(event));
  1590. switch (ev_code) {
  1591. case ESE_DZ_EV_CODE_MCDI_EV:
  1592. efx_mcdi_process_event(channel, &event);
  1593. break;
  1594. case ESE_DZ_EV_CODE_RX_EV:
  1595. spent += efx_ef10_handle_rx_event(channel, &event);
  1596. if (spent >= quota) {
  1597. /* XXX can we split a merged event to
  1598. * avoid going over-quota?
  1599. */
  1600. spent = quota;
  1601. goto out;
  1602. }
  1603. break;
  1604. case ESE_DZ_EV_CODE_TX_EV:
  1605. tx_descs += efx_ef10_handle_tx_event(channel, &event);
  1606. if (tx_descs > efx->txq_entries) {
  1607. spent = quota;
  1608. goto out;
  1609. } else if (++spent == quota) {
  1610. goto out;
  1611. }
  1612. break;
  1613. case ESE_DZ_EV_CODE_DRIVER_EV:
  1614. efx_ef10_handle_driver_event(channel, &event);
  1615. if (++spent == quota)
  1616. goto out;
  1617. break;
  1618. case EFX_EF10_DRVGEN_EV:
  1619. efx_ef10_handle_driver_generated_event(channel, &event);
  1620. break;
  1621. default:
  1622. netif_err(efx, hw, efx->net_dev,
  1623. "channel %d unknown event type %d"
  1624. " (data " EFX_QWORD_FMT ")\n",
  1625. channel->channel, ev_code,
  1626. EFX_QWORD_VAL(event));
  1627. }
  1628. }
  1629. out:
  1630. channel->eventq_read_ptr = read_ptr;
  1631. return spent;
  1632. }
  1633. static void efx_ef10_ev_read_ack(struct efx_channel *channel)
  1634. {
  1635. struct efx_nic *efx = channel->efx;
  1636. efx_dword_t rptr;
  1637. if (EFX_EF10_WORKAROUND_35388(efx)) {
  1638. BUILD_BUG_ON(EFX_MIN_EVQ_SIZE <
  1639. (1 << ERF_DD_EVQ_IND_RPTR_WIDTH));
  1640. BUILD_BUG_ON(EFX_MAX_EVQ_SIZE >
  1641. (1 << 2 * ERF_DD_EVQ_IND_RPTR_WIDTH));
  1642. EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
  1643. EFE_DD_EVQ_IND_RPTR_FLAGS_HIGH,
  1644. ERF_DD_EVQ_IND_RPTR,
  1645. (channel->eventq_read_ptr &
  1646. channel->eventq_mask) >>
  1647. ERF_DD_EVQ_IND_RPTR_WIDTH);
  1648. efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
  1649. channel->channel);
  1650. EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
  1651. EFE_DD_EVQ_IND_RPTR_FLAGS_LOW,
  1652. ERF_DD_EVQ_IND_RPTR,
  1653. channel->eventq_read_ptr &
  1654. ((1 << ERF_DD_EVQ_IND_RPTR_WIDTH) - 1));
  1655. efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
  1656. channel->channel);
  1657. } else {
  1658. EFX_POPULATE_DWORD_1(rptr, ERF_DZ_EVQ_RPTR,
  1659. channel->eventq_read_ptr &
  1660. channel->eventq_mask);
  1661. efx_writed_page(efx, &rptr, ER_DZ_EVQ_RPTR, channel->channel);
  1662. }
  1663. }
  1664. static void efx_ef10_ev_test_generate(struct efx_channel *channel)
  1665. {
  1666. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
  1667. struct efx_nic *efx = channel->efx;
  1668. efx_qword_t event;
  1669. int rc;
  1670. EFX_POPULATE_QWORD_2(event,
  1671. ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
  1672. ESF_DZ_EV_DATA, EFX_EF10_TEST);
  1673. MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
  1674. /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
  1675. * already swapped the data to little-endian order.
  1676. */
  1677. memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
  1678. sizeof(efx_qword_t));
  1679. rc = efx_mcdi_rpc(efx, MC_CMD_DRIVER_EVENT, inbuf, sizeof(inbuf),
  1680. NULL, 0, NULL);
  1681. if (rc != 0)
  1682. goto fail;
  1683. return;
  1684. fail:
  1685. WARN_ON(true);
  1686. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1687. }
  1688. void efx_ef10_handle_drain_event(struct efx_nic *efx)
  1689. {
  1690. if (atomic_dec_and_test(&efx->active_queues))
  1691. wake_up(&efx->flush_wq);
  1692. WARN_ON(atomic_read(&efx->active_queues) < 0);
  1693. }
  1694. static int efx_ef10_fini_dmaq(struct efx_nic *efx)
  1695. {
  1696. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1697. struct efx_channel *channel;
  1698. struct efx_tx_queue *tx_queue;
  1699. struct efx_rx_queue *rx_queue;
  1700. int pending;
  1701. /* If the MC has just rebooted, the TX/RX queues will have already been
  1702. * torn down, but efx->active_queues needs to be set to zero.
  1703. */
  1704. if (nic_data->must_realloc_vis) {
  1705. atomic_set(&efx->active_queues, 0);
  1706. return 0;
  1707. }
  1708. /* Do not attempt to write to the NIC during EEH recovery */
  1709. if (efx->state != STATE_RECOVERY) {
  1710. efx_for_each_channel(channel, efx) {
  1711. efx_for_each_channel_rx_queue(rx_queue, channel)
  1712. efx_ef10_rx_fini(rx_queue);
  1713. efx_for_each_channel_tx_queue(tx_queue, channel)
  1714. efx_ef10_tx_fini(tx_queue);
  1715. }
  1716. wait_event_timeout(efx->flush_wq,
  1717. atomic_read(&efx->active_queues) == 0,
  1718. msecs_to_jiffies(EFX_MAX_FLUSH_TIME));
  1719. pending = atomic_read(&efx->active_queues);
  1720. if (pending) {
  1721. netif_err(efx, hw, efx->net_dev, "failed to flush %d queues\n",
  1722. pending);
  1723. return -ETIMEDOUT;
  1724. }
  1725. }
  1726. return 0;
  1727. }
  1728. static bool efx_ef10_filter_equal(const struct efx_filter_spec *left,
  1729. const struct efx_filter_spec *right)
  1730. {
  1731. if ((left->match_flags ^ right->match_flags) |
  1732. ((left->flags ^ right->flags) &
  1733. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
  1734. return false;
  1735. return memcmp(&left->outer_vid, &right->outer_vid,
  1736. sizeof(struct efx_filter_spec) -
  1737. offsetof(struct efx_filter_spec, outer_vid)) == 0;
  1738. }
  1739. static unsigned int efx_ef10_filter_hash(const struct efx_filter_spec *spec)
  1740. {
  1741. BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
  1742. return jhash2((const u32 *)&spec->outer_vid,
  1743. (sizeof(struct efx_filter_spec) -
  1744. offsetof(struct efx_filter_spec, outer_vid)) / 4,
  1745. 0);
  1746. /* XXX should we randomise the initval? */
  1747. }
  1748. /* Decide whether a filter should be exclusive or else should allow
  1749. * delivery to additional recipients. Currently we decide that
  1750. * filters for specific local unicast MAC and IP addresses are
  1751. * exclusive.
  1752. */
  1753. static bool efx_ef10_filter_is_exclusive(const struct efx_filter_spec *spec)
  1754. {
  1755. if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC &&
  1756. !is_multicast_ether_addr(spec->loc_mac))
  1757. return true;
  1758. if ((spec->match_flags &
  1759. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
  1760. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
  1761. if (spec->ether_type == htons(ETH_P_IP) &&
  1762. !ipv4_is_multicast(spec->loc_host[0]))
  1763. return true;
  1764. if (spec->ether_type == htons(ETH_P_IPV6) &&
  1765. ((const u8 *)spec->loc_host)[0] != 0xff)
  1766. return true;
  1767. }
  1768. return false;
  1769. }
  1770. static struct efx_filter_spec *
  1771. efx_ef10_filter_entry_spec(const struct efx_ef10_filter_table *table,
  1772. unsigned int filter_idx)
  1773. {
  1774. return (struct efx_filter_spec *)(table->entry[filter_idx].spec &
  1775. ~EFX_EF10_FILTER_FLAGS);
  1776. }
  1777. static unsigned int
  1778. efx_ef10_filter_entry_flags(const struct efx_ef10_filter_table *table,
  1779. unsigned int filter_idx)
  1780. {
  1781. return table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAGS;
  1782. }
  1783. static void
  1784. efx_ef10_filter_set_entry(struct efx_ef10_filter_table *table,
  1785. unsigned int filter_idx,
  1786. const struct efx_filter_spec *spec,
  1787. unsigned int flags)
  1788. {
  1789. table->entry[filter_idx].spec = (unsigned long)spec | flags;
  1790. }
  1791. static void efx_ef10_filter_push_prep(struct efx_nic *efx,
  1792. const struct efx_filter_spec *spec,
  1793. efx_dword_t *inbuf, u64 handle,
  1794. bool replacing)
  1795. {
  1796. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1797. memset(inbuf, 0, MC_CMD_FILTER_OP_IN_LEN);
  1798. if (replacing) {
  1799. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  1800. MC_CMD_FILTER_OP_IN_OP_REPLACE);
  1801. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE, handle);
  1802. } else {
  1803. u32 match_fields = 0;
  1804. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  1805. efx_ef10_filter_is_exclusive(spec) ?
  1806. MC_CMD_FILTER_OP_IN_OP_INSERT :
  1807. MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE);
  1808. /* Convert match flags and values. Unlike almost
  1809. * everything else in MCDI, these fields are in
  1810. * network byte order.
  1811. */
  1812. if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC_IG)
  1813. match_fields |=
  1814. is_multicast_ether_addr(spec->loc_mac) ?
  1815. 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST_LBN :
  1816. 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
  1817. #define COPY_FIELD(gen_flag, gen_field, mcdi_field) \
  1818. if (spec->match_flags & EFX_FILTER_MATCH_ ## gen_flag) { \
  1819. match_fields |= \
  1820. 1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
  1821. mcdi_field ## _LBN; \
  1822. BUILD_BUG_ON( \
  1823. MC_CMD_FILTER_OP_IN_ ## mcdi_field ## _LEN < \
  1824. sizeof(spec->gen_field)); \
  1825. memcpy(MCDI_PTR(inbuf, FILTER_OP_IN_ ## mcdi_field), \
  1826. &spec->gen_field, sizeof(spec->gen_field)); \
  1827. }
  1828. COPY_FIELD(REM_HOST, rem_host, SRC_IP);
  1829. COPY_FIELD(LOC_HOST, loc_host, DST_IP);
  1830. COPY_FIELD(REM_MAC, rem_mac, SRC_MAC);
  1831. COPY_FIELD(REM_PORT, rem_port, SRC_PORT);
  1832. COPY_FIELD(LOC_MAC, loc_mac, DST_MAC);
  1833. COPY_FIELD(LOC_PORT, loc_port, DST_PORT);
  1834. COPY_FIELD(ETHER_TYPE, ether_type, ETHER_TYPE);
  1835. COPY_FIELD(INNER_VID, inner_vid, INNER_VLAN);
  1836. COPY_FIELD(OUTER_VID, outer_vid, OUTER_VLAN);
  1837. COPY_FIELD(IP_PROTO, ip_proto, IP_PROTO);
  1838. #undef COPY_FIELD
  1839. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_MATCH_FIELDS,
  1840. match_fields);
  1841. }
  1842. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
  1843. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_DEST,
  1844. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
  1845. MC_CMD_FILTER_OP_IN_RX_DEST_DROP :
  1846. MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
  1847. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DEST,
  1848. MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
  1849. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_QUEUE, spec->dmaq_id);
  1850. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_MODE,
  1851. (spec->flags & EFX_FILTER_FLAG_RX_RSS) ?
  1852. MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
  1853. MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
  1854. if (spec->flags & EFX_FILTER_FLAG_RX_RSS)
  1855. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_CONTEXT,
  1856. spec->rss_context !=
  1857. EFX_FILTER_RSS_CONTEXT_DEFAULT ?
  1858. spec->rss_context : nic_data->rx_rss_context);
  1859. }
  1860. static int efx_ef10_filter_push(struct efx_nic *efx,
  1861. const struct efx_filter_spec *spec,
  1862. u64 *handle, bool replacing)
  1863. {
  1864. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  1865. MCDI_DECLARE_BUF(outbuf, MC_CMD_FILTER_OP_OUT_LEN);
  1866. int rc;
  1867. efx_ef10_filter_push_prep(efx, spec, inbuf, *handle, replacing);
  1868. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  1869. outbuf, sizeof(outbuf), NULL);
  1870. if (rc == 0)
  1871. *handle = MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
  1872. return rc;
  1873. }
  1874. static int efx_ef10_filter_rx_match_pri(struct efx_ef10_filter_table *table,
  1875. enum efx_filter_match_flags match_flags)
  1876. {
  1877. unsigned int match_pri;
  1878. for (match_pri = 0;
  1879. match_pri < table->rx_match_count;
  1880. match_pri++)
  1881. if (table->rx_match_flags[match_pri] == match_flags)
  1882. return match_pri;
  1883. return -EPROTONOSUPPORT;
  1884. }
  1885. static s32 efx_ef10_filter_insert(struct efx_nic *efx,
  1886. struct efx_filter_spec *spec,
  1887. bool replace_equal)
  1888. {
  1889. struct efx_ef10_filter_table *table = efx->filter_state;
  1890. DECLARE_BITMAP(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
  1891. struct efx_filter_spec *saved_spec;
  1892. unsigned int match_pri, hash;
  1893. unsigned int priv_flags;
  1894. bool replacing = false;
  1895. int ins_index = -1;
  1896. DEFINE_WAIT(wait);
  1897. bool is_mc_recip;
  1898. s32 rc;
  1899. /* For now, only support RX filters */
  1900. if ((spec->flags & (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)) !=
  1901. EFX_FILTER_FLAG_RX)
  1902. return -EINVAL;
  1903. rc = efx_ef10_filter_rx_match_pri(table, spec->match_flags);
  1904. if (rc < 0)
  1905. return rc;
  1906. match_pri = rc;
  1907. hash = efx_ef10_filter_hash(spec);
  1908. is_mc_recip = efx_filter_is_mc_recipient(spec);
  1909. if (is_mc_recip)
  1910. bitmap_zero(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
  1911. /* Find any existing filters with the same match tuple or
  1912. * else a free slot to insert at. If any of them are busy,
  1913. * we have to wait and retry.
  1914. */
  1915. for (;;) {
  1916. unsigned int depth = 1;
  1917. unsigned int i;
  1918. spin_lock_bh(&efx->filter_lock);
  1919. for (;;) {
  1920. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  1921. saved_spec = efx_ef10_filter_entry_spec(table, i);
  1922. if (!saved_spec) {
  1923. if (ins_index < 0)
  1924. ins_index = i;
  1925. } else if (efx_ef10_filter_equal(spec, saved_spec)) {
  1926. if (table->entry[i].spec &
  1927. EFX_EF10_FILTER_FLAG_BUSY)
  1928. break;
  1929. if (spec->priority < saved_spec->priority &&
  1930. !(saved_spec->priority ==
  1931. EFX_FILTER_PRI_REQUIRED &&
  1932. saved_spec->flags &
  1933. EFX_FILTER_FLAG_RX_STACK)) {
  1934. rc = -EPERM;
  1935. goto out_unlock;
  1936. }
  1937. if (!is_mc_recip) {
  1938. /* This is the only one */
  1939. if (spec->priority ==
  1940. saved_spec->priority &&
  1941. !replace_equal) {
  1942. rc = -EEXIST;
  1943. goto out_unlock;
  1944. }
  1945. ins_index = i;
  1946. goto found;
  1947. } else if (spec->priority >
  1948. saved_spec->priority ||
  1949. (spec->priority ==
  1950. saved_spec->priority &&
  1951. replace_equal)) {
  1952. if (ins_index < 0)
  1953. ins_index = i;
  1954. else
  1955. __set_bit(depth, mc_rem_map);
  1956. }
  1957. }
  1958. /* Once we reach the maximum search depth, use
  1959. * the first suitable slot or return -EBUSY if
  1960. * there was none
  1961. */
  1962. if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
  1963. if (ins_index < 0) {
  1964. rc = -EBUSY;
  1965. goto out_unlock;
  1966. }
  1967. goto found;
  1968. }
  1969. ++depth;
  1970. }
  1971. prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
  1972. spin_unlock_bh(&efx->filter_lock);
  1973. schedule();
  1974. }
  1975. found:
  1976. /* Create a software table entry if necessary, and mark it
  1977. * busy. We might yet fail to insert, but any attempt to
  1978. * insert a conflicting filter while we're waiting for the
  1979. * firmware must find the busy entry.
  1980. */
  1981. saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
  1982. if (saved_spec) {
  1983. if (spec->flags & EFX_FILTER_FLAG_RX_STACK) {
  1984. /* Just make sure it won't be removed */
  1985. saved_spec->flags |= EFX_FILTER_FLAG_RX_STACK;
  1986. table->entry[ins_index].spec &=
  1987. ~EFX_EF10_FILTER_FLAG_STACK_OLD;
  1988. rc = ins_index;
  1989. goto out_unlock;
  1990. }
  1991. replacing = true;
  1992. priv_flags = efx_ef10_filter_entry_flags(table, ins_index);
  1993. } else {
  1994. saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
  1995. if (!saved_spec) {
  1996. rc = -ENOMEM;
  1997. goto out_unlock;
  1998. }
  1999. *saved_spec = *spec;
  2000. priv_flags = 0;
  2001. }
  2002. efx_ef10_filter_set_entry(table, ins_index, saved_spec,
  2003. priv_flags | EFX_EF10_FILTER_FLAG_BUSY);
  2004. /* Mark lower-priority multicast recipients busy prior to removal */
  2005. if (is_mc_recip) {
  2006. unsigned int depth, i;
  2007. for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
  2008. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2009. if (test_bit(depth, mc_rem_map))
  2010. table->entry[i].spec |=
  2011. EFX_EF10_FILTER_FLAG_BUSY;
  2012. }
  2013. }
  2014. spin_unlock_bh(&efx->filter_lock);
  2015. rc = efx_ef10_filter_push(efx, spec, &table->entry[ins_index].handle,
  2016. replacing);
  2017. /* Finalise the software table entry */
  2018. spin_lock_bh(&efx->filter_lock);
  2019. if (rc == 0) {
  2020. if (replacing) {
  2021. /* Update the fields that may differ */
  2022. saved_spec->priority = spec->priority;
  2023. saved_spec->flags &= EFX_FILTER_FLAG_RX_STACK;
  2024. saved_spec->flags |= spec->flags;
  2025. saved_spec->rss_context = spec->rss_context;
  2026. saved_spec->dmaq_id = spec->dmaq_id;
  2027. }
  2028. } else if (!replacing) {
  2029. kfree(saved_spec);
  2030. saved_spec = NULL;
  2031. }
  2032. efx_ef10_filter_set_entry(table, ins_index, saved_spec, priv_flags);
  2033. /* Remove and finalise entries for lower-priority multicast
  2034. * recipients
  2035. */
  2036. if (is_mc_recip) {
  2037. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2038. unsigned int depth, i;
  2039. memset(inbuf, 0, sizeof(inbuf));
  2040. for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
  2041. if (!test_bit(depth, mc_rem_map))
  2042. continue;
  2043. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2044. saved_spec = efx_ef10_filter_entry_spec(table, i);
  2045. priv_flags = efx_ef10_filter_entry_flags(table, i);
  2046. if (rc == 0) {
  2047. spin_unlock_bh(&efx->filter_lock);
  2048. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2049. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2050. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2051. table->entry[i].handle);
  2052. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
  2053. inbuf, sizeof(inbuf),
  2054. NULL, 0, NULL);
  2055. spin_lock_bh(&efx->filter_lock);
  2056. }
  2057. if (rc == 0) {
  2058. kfree(saved_spec);
  2059. saved_spec = NULL;
  2060. priv_flags = 0;
  2061. } else {
  2062. priv_flags &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2063. }
  2064. efx_ef10_filter_set_entry(table, i, saved_spec,
  2065. priv_flags);
  2066. }
  2067. }
  2068. /* If successful, return the inserted filter ID */
  2069. if (rc == 0)
  2070. rc = match_pri * HUNT_FILTER_TBL_ROWS + ins_index;
  2071. wake_up_all(&table->waitq);
  2072. out_unlock:
  2073. spin_unlock_bh(&efx->filter_lock);
  2074. finish_wait(&table->waitq, &wait);
  2075. return rc;
  2076. }
  2077. static void efx_ef10_filter_update_rx_scatter(struct efx_nic *efx)
  2078. {
  2079. /* no need to do anything here on EF10 */
  2080. }
  2081. /* Remove a filter.
  2082. * If !stack_requested, remove by ID
  2083. * If stack_requested, remove by index
  2084. * Filter ID may come from userland and must be range-checked.
  2085. */
  2086. static int efx_ef10_filter_remove_internal(struct efx_nic *efx,
  2087. enum efx_filter_priority priority,
  2088. u32 filter_id, bool stack_requested)
  2089. {
  2090. unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
  2091. struct efx_ef10_filter_table *table = efx->filter_state;
  2092. MCDI_DECLARE_BUF(inbuf,
  2093. MC_CMD_FILTER_OP_IN_HANDLE_OFST +
  2094. MC_CMD_FILTER_OP_IN_HANDLE_LEN);
  2095. struct efx_filter_spec *spec;
  2096. DEFINE_WAIT(wait);
  2097. int rc;
  2098. /* Find the software table entry and mark it busy. Don't
  2099. * remove it yet; any attempt to update while we're waiting
  2100. * for the firmware must find the busy entry.
  2101. */
  2102. for (;;) {
  2103. spin_lock_bh(&efx->filter_lock);
  2104. if (!(table->entry[filter_idx].spec &
  2105. EFX_EF10_FILTER_FLAG_BUSY))
  2106. break;
  2107. prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
  2108. spin_unlock_bh(&efx->filter_lock);
  2109. schedule();
  2110. }
  2111. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2112. if (!spec || spec->priority > priority ||
  2113. (!stack_requested &&
  2114. efx_ef10_filter_rx_match_pri(table, spec->match_flags) !=
  2115. filter_id / HUNT_FILTER_TBL_ROWS)) {
  2116. rc = -ENOENT;
  2117. goto out_unlock;
  2118. }
  2119. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  2120. spin_unlock_bh(&efx->filter_lock);
  2121. if (spec->flags & EFX_FILTER_FLAG_RX_STACK && !stack_requested) {
  2122. /* Reset steering of a stack-owned filter */
  2123. struct efx_filter_spec new_spec = *spec;
  2124. new_spec.priority = EFX_FILTER_PRI_REQUIRED;
  2125. new_spec.flags = (EFX_FILTER_FLAG_RX |
  2126. EFX_FILTER_FLAG_RX_RSS |
  2127. EFX_FILTER_FLAG_RX_STACK);
  2128. new_spec.dmaq_id = 0;
  2129. new_spec.rss_context = EFX_FILTER_RSS_CONTEXT_DEFAULT;
  2130. rc = efx_ef10_filter_push(efx, &new_spec,
  2131. &table->entry[filter_idx].handle,
  2132. true);
  2133. spin_lock_bh(&efx->filter_lock);
  2134. if (rc == 0)
  2135. *spec = new_spec;
  2136. } else {
  2137. /* Really remove the filter */
  2138. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2139. efx_ef10_filter_is_exclusive(spec) ?
  2140. MC_CMD_FILTER_OP_IN_OP_REMOVE :
  2141. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2142. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2143. table->entry[filter_idx].handle);
  2144. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
  2145. inbuf, sizeof(inbuf), NULL, 0, NULL);
  2146. spin_lock_bh(&efx->filter_lock);
  2147. if (rc == 0) {
  2148. kfree(spec);
  2149. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  2150. }
  2151. }
  2152. table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2153. wake_up_all(&table->waitq);
  2154. out_unlock:
  2155. spin_unlock_bh(&efx->filter_lock);
  2156. finish_wait(&table->waitq, &wait);
  2157. return rc;
  2158. }
  2159. static int efx_ef10_filter_remove_safe(struct efx_nic *efx,
  2160. enum efx_filter_priority priority,
  2161. u32 filter_id)
  2162. {
  2163. return efx_ef10_filter_remove_internal(efx, priority, filter_id, false);
  2164. }
  2165. static int efx_ef10_filter_get_safe(struct efx_nic *efx,
  2166. enum efx_filter_priority priority,
  2167. u32 filter_id, struct efx_filter_spec *spec)
  2168. {
  2169. unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
  2170. struct efx_ef10_filter_table *table = efx->filter_state;
  2171. const struct efx_filter_spec *saved_spec;
  2172. int rc;
  2173. spin_lock_bh(&efx->filter_lock);
  2174. saved_spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2175. if (saved_spec && saved_spec->priority == priority &&
  2176. efx_ef10_filter_rx_match_pri(table, saved_spec->match_flags) ==
  2177. filter_id / HUNT_FILTER_TBL_ROWS) {
  2178. *spec = *saved_spec;
  2179. rc = 0;
  2180. } else {
  2181. rc = -ENOENT;
  2182. }
  2183. spin_unlock_bh(&efx->filter_lock);
  2184. return rc;
  2185. }
  2186. static void efx_ef10_filter_clear_rx(struct efx_nic *efx,
  2187. enum efx_filter_priority priority)
  2188. {
  2189. /* TODO */
  2190. }
  2191. static u32 efx_ef10_filter_count_rx_used(struct efx_nic *efx,
  2192. enum efx_filter_priority priority)
  2193. {
  2194. struct efx_ef10_filter_table *table = efx->filter_state;
  2195. unsigned int filter_idx;
  2196. s32 count = 0;
  2197. spin_lock_bh(&efx->filter_lock);
  2198. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2199. if (table->entry[filter_idx].spec &&
  2200. efx_ef10_filter_entry_spec(table, filter_idx)->priority ==
  2201. priority)
  2202. ++count;
  2203. }
  2204. spin_unlock_bh(&efx->filter_lock);
  2205. return count;
  2206. }
  2207. static u32 efx_ef10_filter_get_rx_id_limit(struct efx_nic *efx)
  2208. {
  2209. struct efx_ef10_filter_table *table = efx->filter_state;
  2210. return table->rx_match_count * HUNT_FILTER_TBL_ROWS;
  2211. }
  2212. static s32 efx_ef10_filter_get_rx_ids(struct efx_nic *efx,
  2213. enum efx_filter_priority priority,
  2214. u32 *buf, u32 size)
  2215. {
  2216. struct efx_ef10_filter_table *table = efx->filter_state;
  2217. struct efx_filter_spec *spec;
  2218. unsigned int filter_idx;
  2219. s32 count = 0;
  2220. spin_lock_bh(&efx->filter_lock);
  2221. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2222. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2223. if (spec && spec->priority == priority) {
  2224. if (count == size) {
  2225. count = -EMSGSIZE;
  2226. break;
  2227. }
  2228. buf[count++] = (efx_ef10_filter_rx_match_pri(
  2229. table, spec->match_flags) *
  2230. HUNT_FILTER_TBL_ROWS +
  2231. filter_idx);
  2232. }
  2233. }
  2234. spin_unlock_bh(&efx->filter_lock);
  2235. return count;
  2236. }
  2237. #ifdef CONFIG_RFS_ACCEL
  2238. static efx_mcdi_async_completer efx_ef10_filter_rfs_insert_complete;
  2239. static s32 efx_ef10_filter_rfs_insert(struct efx_nic *efx,
  2240. struct efx_filter_spec *spec)
  2241. {
  2242. struct efx_ef10_filter_table *table = efx->filter_state;
  2243. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2244. struct efx_filter_spec *saved_spec;
  2245. unsigned int hash, i, depth = 1;
  2246. bool replacing = false;
  2247. int ins_index = -1;
  2248. u64 cookie;
  2249. s32 rc;
  2250. /* Must be an RX filter without RSS and not for a multicast
  2251. * destination address (RFS only works for connected sockets).
  2252. * These restrictions allow us to pass only a tiny amount of
  2253. * data through to the completion function.
  2254. */
  2255. EFX_WARN_ON_PARANOID(spec->flags !=
  2256. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_RX_SCATTER));
  2257. EFX_WARN_ON_PARANOID(spec->priority != EFX_FILTER_PRI_HINT);
  2258. EFX_WARN_ON_PARANOID(efx_filter_is_mc_recipient(spec));
  2259. hash = efx_ef10_filter_hash(spec);
  2260. spin_lock_bh(&efx->filter_lock);
  2261. /* Find any existing filter with the same match tuple or else
  2262. * a free slot to insert at. If an existing filter is busy,
  2263. * we have to give up.
  2264. */
  2265. for (;;) {
  2266. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2267. saved_spec = efx_ef10_filter_entry_spec(table, i);
  2268. if (!saved_spec) {
  2269. if (ins_index < 0)
  2270. ins_index = i;
  2271. } else if (efx_ef10_filter_equal(spec, saved_spec)) {
  2272. if (table->entry[i].spec & EFX_EF10_FILTER_FLAG_BUSY) {
  2273. rc = -EBUSY;
  2274. goto fail_unlock;
  2275. }
  2276. EFX_WARN_ON_PARANOID(saved_spec->flags &
  2277. EFX_FILTER_FLAG_RX_STACK);
  2278. if (spec->priority < saved_spec->priority) {
  2279. rc = -EPERM;
  2280. goto fail_unlock;
  2281. }
  2282. ins_index = i;
  2283. break;
  2284. }
  2285. /* Once we reach the maximum search depth, use the
  2286. * first suitable slot or return -EBUSY if there was
  2287. * none
  2288. */
  2289. if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
  2290. if (ins_index < 0) {
  2291. rc = -EBUSY;
  2292. goto fail_unlock;
  2293. }
  2294. break;
  2295. }
  2296. ++depth;
  2297. }
  2298. /* Create a software table entry if necessary, and mark it
  2299. * busy. We might yet fail to insert, but any attempt to
  2300. * insert a conflicting filter while we're waiting for the
  2301. * firmware must find the busy entry.
  2302. */
  2303. saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
  2304. if (saved_spec) {
  2305. replacing = true;
  2306. } else {
  2307. saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
  2308. if (!saved_spec) {
  2309. rc = -ENOMEM;
  2310. goto fail_unlock;
  2311. }
  2312. *saved_spec = *spec;
  2313. }
  2314. efx_ef10_filter_set_entry(table, ins_index, saved_spec,
  2315. EFX_EF10_FILTER_FLAG_BUSY);
  2316. spin_unlock_bh(&efx->filter_lock);
  2317. /* Pack up the variables needed on completion */
  2318. cookie = replacing << 31 | ins_index << 16 | spec->dmaq_id;
  2319. efx_ef10_filter_push_prep(efx, spec, inbuf,
  2320. table->entry[ins_index].handle, replacing);
  2321. efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  2322. MC_CMD_FILTER_OP_OUT_LEN,
  2323. efx_ef10_filter_rfs_insert_complete, cookie);
  2324. return ins_index;
  2325. fail_unlock:
  2326. spin_unlock_bh(&efx->filter_lock);
  2327. return rc;
  2328. }
  2329. static void
  2330. efx_ef10_filter_rfs_insert_complete(struct efx_nic *efx, unsigned long cookie,
  2331. int rc, efx_dword_t *outbuf,
  2332. size_t outlen_actual)
  2333. {
  2334. struct efx_ef10_filter_table *table = efx->filter_state;
  2335. unsigned int ins_index, dmaq_id;
  2336. struct efx_filter_spec *spec;
  2337. bool replacing;
  2338. /* Unpack the cookie */
  2339. replacing = cookie >> 31;
  2340. ins_index = (cookie >> 16) & (HUNT_FILTER_TBL_ROWS - 1);
  2341. dmaq_id = cookie & 0xffff;
  2342. spin_lock_bh(&efx->filter_lock);
  2343. spec = efx_ef10_filter_entry_spec(table, ins_index);
  2344. if (rc == 0) {
  2345. table->entry[ins_index].handle =
  2346. MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
  2347. if (replacing)
  2348. spec->dmaq_id = dmaq_id;
  2349. } else if (!replacing) {
  2350. kfree(spec);
  2351. spec = NULL;
  2352. }
  2353. efx_ef10_filter_set_entry(table, ins_index, spec, 0);
  2354. spin_unlock_bh(&efx->filter_lock);
  2355. wake_up_all(&table->waitq);
  2356. }
  2357. static void
  2358. efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
  2359. unsigned long filter_idx,
  2360. int rc, efx_dword_t *outbuf,
  2361. size_t outlen_actual);
  2362. static bool efx_ef10_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
  2363. unsigned int filter_idx)
  2364. {
  2365. struct efx_ef10_filter_table *table = efx->filter_state;
  2366. struct efx_filter_spec *spec =
  2367. efx_ef10_filter_entry_spec(table, filter_idx);
  2368. MCDI_DECLARE_BUF(inbuf,
  2369. MC_CMD_FILTER_OP_IN_HANDLE_OFST +
  2370. MC_CMD_FILTER_OP_IN_HANDLE_LEN);
  2371. if (!spec ||
  2372. (table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAG_BUSY) ||
  2373. spec->priority != EFX_FILTER_PRI_HINT ||
  2374. !rps_may_expire_flow(efx->net_dev, spec->dmaq_id,
  2375. flow_id, filter_idx))
  2376. return false;
  2377. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2378. MC_CMD_FILTER_OP_IN_OP_REMOVE);
  2379. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2380. table->entry[filter_idx].handle);
  2381. if (efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf), 0,
  2382. efx_ef10_filter_rfs_expire_complete, filter_idx))
  2383. return false;
  2384. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  2385. return true;
  2386. }
  2387. static void
  2388. efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
  2389. unsigned long filter_idx,
  2390. int rc, efx_dword_t *outbuf,
  2391. size_t outlen_actual)
  2392. {
  2393. struct efx_ef10_filter_table *table = efx->filter_state;
  2394. struct efx_filter_spec *spec =
  2395. efx_ef10_filter_entry_spec(table, filter_idx);
  2396. spin_lock_bh(&efx->filter_lock);
  2397. if (rc == 0) {
  2398. kfree(spec);
  2399. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  2400. }
  2401. table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2402. wake_up_all(&table->waitq);
  2403. spin_unlock_bh(&efx->filter_lock);
  2404. }
  2405. #endif /* CONFIG_RFS_ACCEL */
  2406. static int efx_ef10_filter_match_flags_from_mcdi(u32 mcdi_flags)
  2407. {
  2408. int match_flags = 0;
  2409. #define MAP_FLAG(gen_flag, mcdi_field) { \
  2410. u32 old_mcdi_flags = mcdi_flags; \
  2411. mcdi_flags &= ~(1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
  2412. mcdi_field ## _LBN); \
  2413. if (mcdi_flags != old_mcdi_flags) \
  2414. match_flags |= EFX_FILTER_MATCH_ ## gen_flag; \
  2415. }
  2416. MAP_FLAG(LOC_MAC_IG, UNKNOWN_UCAST_DST);
  2417. MAP_FLAG(LOC_MAC_IG, UNKNOWN_MCAST_DST);
  2418. MAP_FLAG(REM_HOST, SRC_IP);
  2419. MAP_FLAG(LOC_HOST, DST_IP);
  2420. MAP_FLAG(REM_MAC, SRC_MAC);
  2421. MAP_FLAG(REM_PORT, SRC_PORT);
  2422. MAP_FLAG(LOC_MAC, DST_MAC);
  2423. MAP_FLAG(LOC_PORT, DST_PORT);
  2424. MAP_FLAG(ETHER_TYPE, ETHER_TYPE);
  2425. MAP_FLAG(INNER_VID, INNER_VLAN);
  2426. MAP_FLAG(OUTER_VID, OUTER_VLAN);
  2427. MAP_FLAG(IP_PROTO, IP_PROTO);
  2428. #undef MAP_FLAG
  2429. /* Did we map them all? */
  2430. if (mcdi_flags)
  2431. return -EINVAL;
  2432. return match_flags;
  2433. }
  2434. static int efx_ef10_filter_table_probe(struct efx_nic *efx)
  2435. {
  2436. MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN);
  2437. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
  2438. unsigned int pd_match_pri, pd_match_count;
  2439. struct efx_ef10_filter_table *table;
  2440. size_t outlen;
  2441. int rc;
  2442. table = kzalloc(sizeof(*table), GFP_KERNEL);
  2443. if (!table)
  2444. return -ENOMEM;
  2445. /* Find out which RX filter types are supported, and their priorities */
  2446. MCDI_SET_DWORD(inbuf, GET_PARSER_DISP_INFO_IN_OP,
  2447. MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
  2448. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PARSER_DISP_INFO,
  2449. inbuf, sizeof(inbuf), outbuf, sizeof(outbuf),
  2450. &outlen);
  2451. if (rc)
  2452. goto fail;
  2453. pd_match_count = MCDI_VAR_ARRAY_LEN(
  2454. outlen, GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES);
  2455. table->rx_match_count = 0;
  2456. for (pd_match_pri = 0; pd_match_pri < pd_match_count; pd_match_pri++) {
  2457. u32 mcdi_flags =
  2458. MCDI_ARRAY_DWORD(
  2459. outbuf,
  2460. GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES,
  2461. pd_match_pri);
  2462. rc = efx_ef10_filter_match_flags_from_mcdi(mcdi_flags);
  2463. if (rc < 0) {
  2464. netif_dbg(efx, probe, efx->net_dev,
  2465. "%s: fw flags %#x pri %u not supported in driver\n",
  2466. __func__, mcdi_flags, pd_match_pri);
  2467. } else {
  2468. netif_dbg(efx, probe, efx->net_dev,
  2469. "%s: fw flags %#x pri %u supported as driver flags %#x pri %u\n",
  2470. __func__, mcdi_flags, pd_match_pri,
  2471. rc, table->rx_match_count);
  2472. table->rx_match_flags[table->rx_match_count++] = rc;
  2473. }
  2474. }
  2475. table->entry = vzalloc(HUNT_FILTER_TBL_ROWS * sizeof(*table->entry));
  2476. if (!table->entry) {
  2477. rc = -ENOMEM;
  2478. goto fail;
  2479. }
  2480. efx->filter_state = table;
  2481. init_waitqueue_head(&table->waitq);
  2482. return 0;
  2483. fail:
  2484. kfree(table);
  2485. return rc;
  2486. }
  2487. static void efx_ef10_filter_table_restore(struct efx_nic *efx)
  2488. {
  2489. struct efx_ef10_filter_table *table = efx->filter_state;
  2490. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  2491. struct efx_filter_spec *spec;
  2492. unsigned int filter_idx;
  2493. bool failed = false;
  2494. int rc;
  2495. if (!nic_data->must_restore_filters)
  2496. return;
  2497. spin_lock_bh(&efx->filter_lock);
  2498. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2499. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2500. if (!spec)
  2501. continue;
  2502. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  2503. spin_unlock_bh(&efx->filter_lock);
  2504. rc = efx_ef10_filter_push(efx, spec,
  2505. &table->entry[filter_idx].handle,
  2506. false);
  2507. if (rc)
  2508. failed = true;
  2509. spin_lock_bh(&efx->filter_lock);
  2510. if (rc) {
  2511. kfree(spec);
  2512. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  2513. } else {
  2514. table->entry[filter_idx].spec &=
  2515. ~EFX_EF10_FILTER_FLAG_BUSY;
  2516. }
  2517. }
  2518. spin_unlock_bh(&efx->filter_lock);
  2519. if (failed)
  2520. netif_err(efx, hw, efx->net_dev,
  2521. "unable to restore all filters\n");
  2522. else
  2523. nic_data->must_restore_filters = false;
  2524. }
  2525. static void efx_ef10_filter_table_remove(struct efx_nic *efx)
  2526. {
  2527. struct efx_ef10_filter_table *table = efx->filter_state;
  2528. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2529. struct efx_filter_spec *spec;
  2530. unsigned int filter_idx;
  2531. int rc;
  2532. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2533. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2534. if (!spec)
  2535. continue;
  2536. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2537. efx_ef10_filter_is_exclusive(spec) ?
  2538. MC_CMD_FILTER_OP_IN_OP_REMOVE :
  2539. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2540. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2541. table->entry[filter_idx].handle);
  2542. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  2543. NULL, 0, NULL);
  2544. WARN_ON(rc != 0);
  2545. kfree(spec);
  2546. }
  2547. vfree(table->entry);
  2548. kfree(table);
  2549. }
  2550. static void efx_ef10_filter_sync_rx_mode(struct efx_nic *efx)
  2551. {
  2552. struct efx_ef10_filter_table *table = efx->filter_state;
  2553. struct net_device *net_dev = efx->net_dev;
  2554. struct efx_filter_spec spec;
  2555. bool remove_failed = false;
  2556. struct netdev_hw_addr *uc;
  2557. struct netdev_hw_addr *mc;
  2558. unsigned int filter_idx;
  2559. int i, n, rc;
  2560. if (!efx_dev_registered(efx))
  2561. return;
  2562. /* Mark old filters that may need to be removed */
  2563. spin_lock_bh(&efx->filter_lock);
  2564. n = table->stack_uc_count < 0 ? 1 : table->stack_uc_count;
  2565. for (i = 0; i < n; i++) {
  2566. filter_idx = table->stack_uc_list[i].id % HUNT_FILTER_TBL_ROWS;
  2567. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_STACK_OLD;
  2568. }
  2569. n = table->stack_mc_count < 0 ? 1 : table->stack_mc_count;
  2570. for (i = 0; i < n; i++) {
  2571. filter_idx = table->stack_mc_list[i].id % HUNT_FILTER_TBL_ROWS;
  2572. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_STACK_OLD;
  2573. }
  2574. spin_unlock_bh(&efx->filter_lock);
  2575. /* Copy/convert the address lists; add the primary station
  2576. * address and broadcast address
  2577. */
  2578. netif_addr_lock_bh(net_dev);
  2579. if (net_dev->flags & IFF_PROMISC ||
  2580. netdev_uc_count(net_dev) >= EFX_EF10_FILTER_STACK_UC_MAX) {
  2581. table->stack_uc_count = -1;
  2582. } else {
  2583. table->stack_uc_count = 1 + netdev_uc_count(net_dev);
  2584. memcpy(table->stack_uc_list[0].addr, net_dev->dev_addr,
  2585. ETH_ALEN);
  2586. i = 1;
  2587. netdev_for_each_uc_addr(uc, net_dev) {
  2588. memcpy(table->stack_uc_list[i].addr,
  2589. uc->addr, ETH_ALEN);
  2590. i++;
  2591. }
  2592. }
  2593. if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI) ||
  2594. netdev_mc_count(net_dev) >= EFX_EF10_FILTER_STACK_MC_MAX) {
  2595. table->stack_mc_count = -1;
  2596. } else {
  2597. table->stack_mc_count = 1 + netdev_mc_count(net_dev);
  2598. eth_broadcast_addr(table->stack_mc_list[0].addr);
  2599. i = 1;
  2600. netdev_for_each_mc_addr(mc, net_dev) {
  2601. memcpy(table->stack_mc_list[i].addr,
  2602. mc->addr, ETH_ALEN);
  2603. i++;
  2604. }
  2605. }
  2606. netif_addr_unlock_bh(net_dev);
  2607. /* Insert/renew unicast filters */
  2608. if (table->stack_uc_count >= 0) {
  2609. for (i = 0; i < table->stack_uc_count; i++) {
  2610. efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
  2611. EFX_FILTER_FLAG_RX_RSS |
  2612. EFX_FILTER_FLAG_RX_STACK,
  2613. 0);
  2614. efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
  2615. table->stack_uc_list[i].addr);
  2616. rc = efx_ef10_filter_insert(efx, &spec, true);
  2617. if (rc < 0) {
  2618. /* Fall back to unicast-promisc */
  2619. while (i--)
  2620. efx_ef10_filter_remove_safe(
  2621. efx, EFX_FILTER_PRI_REQUIRED,
  2622. table->stack_uc_list[i].id);
  2623. table->stack_uc_count = -1;
  2624. break;
  2625. }
  2626. table->stack_uc_list[i].id = rc;
  2627. }
  2628. }
  2629. if (table->stack_uc_count < 0) {
  2630. efx_filter_init_rx(&spec, EFX_FILTER_PRI_REQUIRED,
  2631. EFX_FILTER_FLAG_RX_RSS |
  2632. EFX_FILTER_FLAG_RX_STACK,
  2633. 0);
  2634. efx_filter_set_uc_def(&spec);
  2635. rc = efx_ef10_filter_insert(efx, &spec, true);
  2636. if (rc < 0) {
  2637. WARN_ON(1);
  2638. table->stack_uc_count = 0;
  2639. } else {
  2640. table->stack_uc_list[0].id = rc;
  2641. }
  2642. }
  2643. /* Insert/renew multicast filters */
  2644. if (table->stack_mc_count >= 0) {
  2645. for (i = 0; i < table->stack_mc_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_mc_list[i].addr);
  2652. rc = efx_ef10_filter_insert(efx, &spec, true);
  2653. if (rc < 0) {
  2654. /* Fall back to multicast-promisc */
  2655. while (i--)
  2656. efx_ef10_filter_remove_safe(
  2657. efx, EFX_FILTER_PRI_REQUIRED,
  2658. table->stack_mc_list[i].id);
  2659. table->stack_mc_count = -1;
  2660. break;
  2661. }
  2662. table->stack_mc_list[i].id = rc;
  2663. }
  2664. }
  2665. if (table->stack_mc_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_mc_def(&spec);
  2671. rc = efx_ef10_filter_insert(efx, &spec, true);
  2672. if (rc < 0) {
  2673. WARN_ON(1);
  2674. table->stack_mc_count = 0;
  2675. } else {
  2676. table->stack_mc_list[0].id = rc;
  2677. }
  2678. }
  2679. /* Remove filters that weren't renewed. Since nothing else
  2680. * changes the STACK_OLD flag or removes these filters, we
  2681. * don't need to hold the filter_lock while scanning for
  2682. * these filters.
  2683. */
  2684. for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
  2685. if (ACCESS_ONCE(table->entry[i].spec) &
  2686. EFX_EF10_FILTER_FLAG_STACK_OLD) {
  2687. if (efx_ef10_filter_remove_internal(efx,
  2688. EFX_FILTER_PRI_REQUIRED,
  2689. i, true) < 0)
  2690. remove_failed = true;
  2691. }
  2692. }
  2693. WARN_ON(remove_failed);
  2694. }
  2695. static int efx_ef10_mac_reconfigure(struct efx_nic *efx)
  2696. {
  2697. efx_ef10_filter_sync_rx_mode(efx);
  2698. return efx_mcdi_set_mac(efx);
  2699. }
  2700. #ifdef CONFIG_SFC_MTD
  2701. struct efx_ef10_nvram_type_info {
  2702. u16 type, type_mask;
  2703. u8 port;
  2704. const char *name;
  2705. };
  2706. static const struct efx_ef10_nvram_type_info efx_ef10_nvram_types[] = {
  2707. { NVRAM_PARTITION_TYPE_MC_FIRMWARE, 0, 0, "sfc_mcfw" },
  2708. { NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 0, 0, "sfc_mcfw_backup" },
  2709. { NVRAM_PARTITION_TYPE_EXPANSION_ROM, 0, 0, "sfc_exp_rom" },
  2710. { NVRAM_PARTITION_TYPE_STATIC_CONFIG, 0, 0, "sfc_static_cfg" },
  2711. { NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 0, 0, "sfc_dynamic_cfg" },
  2712. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 0, 0, "sfc_exp_rom_cfg" },
  2713. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT1, 0, 1, "sfc_exp_rom_cfg" },
  2714. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT2, 0, 2, "sfc_exp_rom_cfg" },
  2715. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT3, 0, 3, "sfc_exp_rom_cfg" },
  2716. { NVRAM_PARTITION_TYPE_PHY_MIN, 0xff, 0, "sfc_phy_fw" },
  2717. };
  2718. static int efx_ef10_mtd_probe_partition(struct efx_nic *efx,
  2719. struct efx_mcdi_mtd_partition *part,
  2720. unsigned int type)
  2721. {
  2722. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_METADATA_IN_LEN);
  2723. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_METADATA_OUT_LENMAX);
  2724. const struct efx_ef10_nvram_type_info *info;
  2725. size_t size, erase_size, outlen;
  2726. bool protected;
  2727. int rc;
  2728. for (info = efx_ef10_nvram_types; ; info++) {
  2729. if (info ==
  2730. efx_ef10_nvram_types + ARRAY_SIZE(efx_ef10_nvram_types))
  2731. return -ENODEV;
  2732. if ((type & ~info->type_mask) == info->type)
  2733. break;
  2734. }
  2735. if (info->port != efx_port_num(efx))
  2736. return -ENODEV;
  2737. rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
  2738. if (rc)
  2739. return rc;
  2740. if (protected)
  2741. return -ENODEV; /* hide it */
  2742. part->nvram_type = type;
  2743. MCDI_SET_DWORD(inbuf, NVRAM_METADATA_IN_TYPE, type);
  2744. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_METADATA, inbuf, sizeof(inbuf),
  2745. outbuf, sizeof(outbuf), &outlen);
  2746. if (rc)
  2747. return rc;
  2748. if (outlen < MC_CMD_NVRAM_METADATA_OUT_LENMIN)
  2749. return -EIO;
  2750. if (MCDI_DWORD(outbuf, NVRAM_METADATA_OUT_FLAGS) &
  2751. (1 << MC_CMD_NVRAM_METADATA_OUT_SUBTYPE_VALID_LBN))
  2752. part->fw_subtype = MCDI_DWORD(outbuf,
  2753. NVRAM_METADATA_OUT_SUBTYPE);
  2754. part->common.dev_type_name = "EF10 NVRAM manager";
  2755. part->common.type_name = info->name;
  2756. part->common.mtd.type = MTD_NORFLASH;
  2757. part->common.mtd.flags = MTD_CAP_NORFLASH;
  2758. part->common.mtd.size = size;
  2759. part->common.mtd.erasesize = erase_size;
  2760. return 0;
  2761. }
  2762. static int efx_ef10_mtd_probe(struct efx_nic *efx)
  2763. {
  2764. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_PARTITIONS_OUT_LENMAX);
  2765. struct efx_mcdi_mtd_partition *parts;
  2766. size_t outlen, n_parts_total, i, n_parts;
  2767. unsigned int type;
  2768. int rc;
  2769. ASSERT_RTNL();
  2770. BUILD_BUG_ON(MC_CMD_NVRAM_PARTITIONS_IN_LEN != 0);
  2771. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_PARTITIONS, NULL, 0,
  2772. outbuf, sizeof(outbuf), &outlen);
  2773. if (rc)
  2774. return rc;
  2775. if (outlen < MC_CMD_NVRAM_PARTITIONS_OUT_LENMIN)
  2776. return -EIO;
  2777. n_parts_total = MCDI_DWORD(outbuf, NVRAM_PARTITIONS_OUT_NUM_PARTITIONS);
  2778. if (n_parts_total >
  2779. MCDI_VAR_ARRAY_LEN(outlen, NVRAM_PARTITIONS_OUT_TYPE_ID))
  2780. return -EIO;
  2781. parts = kcalloc(n_parts_total, sizeof(*parts), GFP_KERNEL);
  2782. if (!parts)
  2783. return -ENOMEM;
  2784. n_parts = 0;
  2785. for (i = 0; i < n_parts_total; i++) {
  2786. type = MCDI_ARRAY_DWORD(outbuf, NVRAM_PARTITIONS_OUT_TYPE_ID,
  2787. i);
  2788. rc = efx_ef10_mtd_probe_partition(efx, &parts[n_parts], type);
  2789. if (rc == 0)
  2790. n_parts++;
  2791. else if (rc != -ENODEV)
  2792. goto fail;
  2793. }
  2794. rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));
  2795. fail:
  2796. if (rc)
  2797. kfree(parts);
  2798. return rc;
  2799. }
  2800. #endif /* CONFIG_SFC_MTD */
  2801. static void efx_ef10_ptp_write_host_time(struct efx_nic *efx, u32 host_time)
  2802. {
  2803. _efx_writed(efx, cpu_to_le32(host_time), ER_DZ_MC_DB_LWRD);
  2804. }
  2805. const struct efx_nic_type efx_hunt_a0_nic_type = {
  2806. .mem_map_size = efx_ef10_mem_map_size,
  2807. .probe = efx_ef10_probe,
  2808. .remove = efx_ef10_remove,
  2809. .dimension_resources = efx_ef10_dimension_resources,
  2810. .init = efx_ef10_init_nic,
  2811. .fini = efx_port_dummy_op_void,
  2812. .map_reset_reason = efx_mcdi_map_reset_reason,
  2813. .map_reset_flags = efx_ef10_map_reset_flags,
  2814. .reset = efx_mcdi_reset,
  2815. .probe_port = efx_mcdi_port_probe,
  2816. .remove_port = efx_mcdi_port_remove,
  2817. .fini_dmaq = efx_ef10_fini_dmaq,
  2818. .describe_stats = efx_ef10_describe_stats,
  2819. .update_stats = efx_ef10_update_stats,
  2820. .start_stats = efx_mcdi_mac_start_stats,
  2821. .stop_stats = efx_mcdi_mac_stop_stats,
  2822. .set_id_led = efx_mcdi_set_id_led,
  2823. .push_irq_moderation = efx_ef10_push_irq_moderation,
  2824. .reconfigure_mac = efx_ef10_mac_reconfigure,
  2825. .check_mac_fault = efx_mcdi_mac_check_fault,
  2826. .reconfigure_port = efx_mcdi_port_reconfigure,
  2827. .get_wol = efx_ef10_get_wol,
  2828. .set_wol = efx_ef10_set_wol,
  2829. .resume_wol = efx_port_dummy_op_void,
  2830. /* TODO: test_chip */
  2831. .test_nvram = efx_mcdi_nvram_test_all,
  2832. .mcdi_request = efx_ef10_mcdi_request,
  2833. .mcdi_poll_response = efx_ef10_mcdi_poll_response,
  2834. .mcdi_read_response = efx_ef10_mcdi_read_response,
  2835. .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
  2836. .irq_enable_master = efx_port_dummy_op_void,
  2837. .irq_test_generate = efx_ef10_irq_test_generate,
  2838. .irq_disable_non_ev = efx_port_dummy_op_void,
  2839. .irq_handle_msi = efx_ef10_msi_interrupt,
  2840. .irq_handle_legacy = efx_ef10_legacy_interrupt,
  2841. .tx_probe = efx_ef10_tx_probe,
  2842. .tx_init = efx_ef10_tx_init,
  2843. .tx_remove = efx_ef10_tx_remove,
  2844. .tx_write = efx_ef10_tx_write,
  2845. .rx_push_indir_table = efx_ef10_rx_push_indir_table,
  2846. .rx_probe = efx_ef10_rx_probe,
  2847. .rx_init = efx_ef10_rx_init,
  2848. .rx_remove = efx_ef10_rx_remove,
  2849. .rx_write = efx_ef10_rx_write,
  2850. .rx_defer_refill = efx_ef10_rx_defer_refill,
  2851. .ev_probe = efx_ef10_ev_probe,
  2852. .ev_init = efx_ef10_ev_init,
  2853. .ev_fini = efx_ef10_ev_fini,
  2854. .ev_remove = efx_ef10_ev_remove,
  2855. .ev_process = efx_ef10_ev_process,
  2856. .ev_read_ack = efx_ef10_ev_read_ack,
  2857. .ev_test_generate = efx_ef10_ev_test_generate,
  2858. .filter_table_probe = efx_ef10_filter_table_probe,
  2859. .filter_table_restore = efx_ef10_filter_table_restore,
  2860. .filter_table_remove = efx_ef10_filter_table_remove,
  2861. .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
  2862. .filter_insert = efx_ef10_filter_insert,
  2863. .filter_remove_safe = efx_ef10_filter_remove_safe,
  2864. .filter_get_safe = efx_ef10_filter_get_safe,
  2865. .filter_clear_rx = efx_ef10_filter_clear_rx,
  2866. .filter_count_rx_used = efx_ef10_filter_count_rx_used,
  2867. .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
  2868. .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
  2869. #ifdef CONFIG_RFS_ACCEL
  2870. .filter_rfs_insert = efx_ef10_filter_rfs_insert,
  2871. .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
  2872. #endif
  2873. #ifdef CONFIG_SFC_MTD
  2874. .mtd_probe = efx_ef10_mtd_probe,
  2875. .mtd_rename = efx_mcdi_mtd_rename,
  2876. .mtd_read = efx_mcdi_mtd_read,
  2877. .mtd_erase = efx_mcdi_mtd_erase,
  2878. .mtd_write = efx_mcdi_mtd_write,
  2879. .mtd_sync = efx_mcdi_mtd_sync,
  2880. #endif
  2881. .ptp_write_host_time = efx_ef10_ptp_write_host_time,
  2882. .revision = EFX_REV_HUNT_A0,
  2883. .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
  2884. .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
  2885. .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
  2886. .can_rx_scatter = true,
  2887. .always_rx_scatter = true,
  2888. .max_interrupt_mode = EFX_INT_MODE_MSIX,
  2889. .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
  2890. .offload_features = (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2891. NETIF_F_RXHASH | NETIF_F_NTUPLE),
  2892. .mcdi_max_ver = 2,
  2893. .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
  2894. };