farch.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2005-2006 Fen Systems Ltd.
  4. * Copyright 2006-2013 Solarflare Communications Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation, incorporated herein by reference.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/delay.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pci.h>
  14. #include <linux/module.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/crc32.h>
  17. #include "net_driver.h"
  18. #include "bitfield.h"
  19. #include "efx.h"
  20. #include "nic.h"
  21. #include "farch_regs.h"
  22. #include "sriov.h"
  23. #include "siena_sriov.h"
  24. #include "io.h"
  25. #include "workarounds.h"
  26. /* Falcon-architecture (SFC4000 and SFC9000-family) support */
  27. /**************************************************************************
  28. *
  29. * Configurable values
  30. *
  31. **************************************************************************
  32. */
  33. /* This is set to 16 for a good reason. In summary, if larger than
  34. * 16, the descriptor cache holds more than a default socket
  35. * buffer's worth of packets (for UDP we can only have at most one
  36. * socket buffer's worth outstanding). This combined with the fact
  37. * that we only get 1 TX event per descriptor cache means the NIC
  38. * goes idle.
  39. */
  40. #define TX_DC_ENTRIES 16
  41. #define TX_DC_ENTRIES_ORDER 1
  42. #define RX_DC_ENTRIES 64
  43. #define RX_DC_ENTRIES_ORDER 3
  44. /* If EFX_MAX_INT_ERRORS internal errors occur within
  45. * EFX_INT_ERROR_EXPIRE seconds, we consider the NIC broken and
  46. * disable it.
  47. */
  48. #define EFX_INT_ERROR_EXPIRE 3600
  49. #define EFX_MAX_INT_ERRORS 5
  50. /* Depth of RX flush request fifo */
  51. #define EFX_RX_FLUSH_COUNT 4
  52. /* Driver generated events */
  53. #define _EFX_CHANNEL_MAGIC_TEST 0x000101
  54. #define _EFX_CHANNEL_MAGIC_FILL 0x000102
  55. #define _EFX_CHANNEL_MAGIC_RX_DRAIN 0x000103
  56. #define _EFX_CHANNEL_MAGIC_TX_DRAIN 0x000104
  57. #define _EFX_CHANNEL_MAGIC(_code, _data) ((_code) << 8 | (_data))
  58. #define _EFX_CHANNEL_MAGIC_CODE(_magic) ((_magic) >> 8)
  59. #define EFX_CHANNEL_MAGIC_TEST(_channel) \
  60. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_TEST, (_channel)->channel)
  61. #define EFX_CHANNEL_MAGIC_FILL(_rx_queue) \
  62. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_FILL, \
  63. efx_rx_queue_index(_rx_queue))
  64. #define EFX_CHANNEL_MAGIC_RX_DRAIN(_rx_queue) \
  65. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_RX_DRAIN, \
  66. efx_rx_queue_index(_rx_queue))
  67. #define EFX_CHANNEL_MAGIC_TX_DRAIN(_tx_queue) \
  68. _EFX_CHANNEL_MAGIC(_EFX_CHANNEL_MAGIC_TX_DRAIN, \
  69. (_tx_queue)->queue)
  70. static void efx_farch_magic_event(struct efx_channel *channel, u32 magic);
  71. /**************************************************************************
  72. *
  73. * Hardware access
  74. *
  75. **************************************************************************/
  76. static inline void efx_write_buf_tbl(struct efx_nic *efx, efx_qword_t *value,
  77. unsigned int index)
  78. {
  79. efx_sram_writeq(efx, efx->membase + efx->type->buf_tbl_base,
  80. value, index);
  81. }
  82. static bool efx_masked_compare_oword(const efx_oword_t *a, const efx_oword_t *b,
  83. const efx_oword_t *mask)
  84. {
  85. return ((a->u64[0] ^ b->u64[0]) & mask->u64[0]) ||
  86. ((a->u64[1] ^ b->u64[1]) & mask->u64[1]);
  87. }
  88. int efx_farch_test_registers(struct efx_nic *efx,
  89. const struct efx_farch_register_test *regs,
  90. size_t n_regs)
  91. {
  92. unsigned address = 0, i, j;
  93. efx_oword_t mask, imask, original, reg, buf;
  94. for (i = 0; i < n_regs; ++i) {
  95. address = regs[i].address;
  96. mask = imask = regs[i].mask;
  97. EFX_INVERT_OWORD(imask);
  98. efx_reado(efx, &original, address);
  99. /* bit sweep on and off */
  100. for (j = 0; j < 128; j++) {
  101. if (!EFX_EXTRACT_OWORD32(mask, j, j))
  102. continue;
  103. /* Test this testable bit can be set in isolation */
  104. EFX_AND_OWORD(reg, original, mask);
  105. EFX_SET_OWORD32(reg, j, j, 1);
  106. efx_writeo(efx, &reg, address);
  107. efx_reado(efx, &buf, address);
  108. if (efx_masked_compare_oword(&reg, &buf, &mask))
  109. goto fail;
  110. /* Test this testable bit can be cleared in isolation */
  111. EFX_OR_OWORD(reg, original, mask);
  112. EFX_SET_OWORD32(reg, j, j, 0);
  113. efx_writeo(efx, &reg, address);
  114. efx_reado(efx, &buf, address);
  115. if (efx_masked_compare_oword(&reg, &buf, &mask))
  116. goto fail;
  117. }
  118. efx_writeo(efx, &original, address);
  119. }
  120. return 0;
  121. fail:
  122. netif_err(efx, hw, efx->net_dev,
  123. "wrote "EFX_OWORD_FMT" read "EFX_OWORD_FMT
  124. " at address 0x%x mask "EFX_OWORD_FMT"\n", EFX_OWORD_VAL(reg),
  125. EFX_OWORD_VAL(buf), address, EFX_OWORD_VAL(mask));
  126. return -EIO;
  127. }
  128. /**************************************************************************
  129. *
  130. * Special buffer handling
  131. * Special buffers are used for event queues and the TX and RX
  132. * descriptor rings.
  133. *
  134. *************************************************************************/
  135. /*
  136. * Initialise a special buffer
  137. *
  138. * This will define a buffer (previously allocated via
  139. * efx_alloc_special_buffer()) in the buffer table, allowing
  140. * it to be used for event queues, descriptor rings etc.
  141. */
  142. static void
  143. efx_init_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  144. {
  145. efx_qword_t buf_desc;
  146. unsigned int index;
  147. dma_addr_t dma_addr;
  148. int i;
  149. EFX_BUG_ON_PARANOID(!buffer->buf.addr);
  150. /* Write buffer descriptors to NIC */
  151. for (i = 0; i < buffer->entries; i++) {
  152. index = buffer->index + i;
  153. dma_addr = buffer->buf.dma_addr + (i * EFX_BUF_SIZE);
  154. netif_dbg(efx, probe, efx->net_dev,
  155. "mapping special buffer %d at %llx\n",
  156. index, (unsigned long long)dma_addr);
  157. EFX_POPULATE_QWORD_3(buf_desc,
  158. FRF_AZ_BUF_ADR_REGION, 0,
  159. FRF_AZ_BUF_ADR_FBUF, dma_addr >> 12,
  160. FRF_AZ_BUF_OWNER_ID_FBUF, 0);
  161. efx_write_buf_tbl(efx, &buf_desc, index);
  162. }
  163. }
  164. /* Unmaps a buffer and clears the buffer table entries */
  165. static void
  166. efx_fini_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  167. {
  168. efx_oword_t buf_tbl_upd;
  169. unsigned int start = buffer->index;
  170. unsigned int end = (buffer->index + buffer->entries - 1);
  171. if (!buffer->entries)
  172. return;
  173. netif_dbg(efx, hw, efx->net_dev, "unmapping special buffers %d-%d\n",
  174. buffer->index, buffer->index + buffer->entries - 1);
  175. EFX_POPULATE_OWORD_4(buf_tbl_upd,
  176. FRF_AZ_BUF_UPD_CMD, 0,
  177. FRF_AZ_BUF_CLR_CMD, 1,
  178. FRF_AZ_BUF_CLR_END_ID, end,
  179. FRF_AZ_BUF_CLR_START_ID, start);
  180. efx_writeo(efx, &buf_tbl_upd, FR_AZ_BUF_TBL_UPD);
  181. }
  182. /*
  183. * Allocate a new special buffer
  184. *
  185. * This allocates memory for a new buffer, clears it and allocates a
  186. * new buffer ID range. It does not write into the buffer table.
  187. *
  188. * This call will allocate 4KB buffers, since 8KB buffers can't be
  189. * used for event queues and descriptor rings.
  190. */
  191. static int efx_alloc_special_buffer(struct efx_nic *efx,
  192. struct efx_special_buffer *buffer,
  193. unsigned int len)
  194. {
  195. #ifdef CONFIG_SFC_SRIOV
  196. struct siena_nic_data *nic_data = efx->nic_data;
  197. #endif
  198. len = ALIGN(len, EFX_BUF_SIZE);
  199. if (efx_nic_alloc_buffer(efx, &buffer->buf, len, GFP_KERNEL))
  200. return -ENOMEM;
  201. buffer->entries = len / EFX_BUF_SIZE;
  202. BUG_ON(buffer->buf.dma_addr & (EFX_BUF_SIZE - 1));
  203. /* Select new buffer ID */
  204. buffer->index = efx->next_buffer_table;
  205. efx->next_buffer_table += buffer->entries;
  206. #ifdef CONFIG_SFC_SRIOV
  207. BUG_ON(efx_siena_sriov_enabled(efx) &&
  208. nic_data->vf_buftbl_base < efx->next_buffer_table);
  209. #endif
  210. netif_dbg(efx, probe, efx->net_dev,
  211. "allocating special buffers %d-%d at %llx+%x "
  212. "(virt %p phys %llx)\n", buffer->index,
  213. buffer->index + buffer->entries - 1,
  214. (u64)buffer->buf.dma_addr, len,
  215. buffer->buf.addr, (u64)virt_to_phys(buffer->buf.addr));
  216. return 0;
  217. }
  218. static void
  219. efx_free_special_buffer(struct efx_nic *efx, struct efx_special_buffer *buffer)
  220. {
  221. if (!buffer->buf.addr)
  222. return;
  223. netif_dbg(efx, hw, efx->net_dev,
  224. "deallocating special buffers %d-%d at %llx+%x "
  225. "(virt %p phys %llx)\n", buffer->index,
  226. buffer->index + buffer->entries - 1,
  227. (u64)buffer->buf.dma_addr, buffer->buf.len,
  228. buffer->buf.addr, (u64)virt_to_phys(buffer->buf.addr));
  229. efx_nic_free_buffer(efx, &buffer->buf);
  230. buffer->entries = 0;
  231. }
  232. /**************************************************************************
  233. *
  234. * TX path
  235. *
  236. **************************************************************************/
  237. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  238. static inline void efx_farch_notify_tx_desc(struct efx_tx_queue *tx_queue)
  239. {
  240. unsigned write_ptr;
  241. efx_dword_t reg;
  242. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  243. EFX_POPULATE_DWORD_1(reg, FRF_AZ_TX_DESC_WPTR_DWORD, write_ptr);
  244. efx_writed_page(tx_queue->efx, &reg,
  245. FR_AZ_TX_DESC_UPD_DWORD_P0, tx_queue->queue);
  246. }
  247. /* Write pointer and first descriptor for TX descriptor ring */
  248. static inline void efx_farch_push_tx_desc(struct efx_tx_queue *tx_queue,
  249. const efx_qword_t *txd)
  250. {
  251. unsigned write_ptr;
  252. efx_oword_t reg;
  253. BUILD_BUG_ON(FRF_AZ_TX_DESC_LBN != 0);
  254. BUILD_BUG_ON(FR_AA_TX_DESC_UPD_KER != FR_BZ_TX_DESC_UPD_P0);
  255. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  256. EFX_POPULATE_OWORD_2(reg, FRF_AZ_TX_DESC_PUSH_CMD, true,
  257. FRF_AZ_TX_DESC_WPTR, write_ptr);
  258. reg.qword[0] = *txd;
  259. efx_writeo_page(tx_queue->efx, &reg,
  260. FR_BZ_TX_DESC_UPD_P0, tx_queue->queue);
  261. }
  262. /* For each entry inserted into the software descriptor ring, create a
  263. * descriptor in the hardware TX descriptor ring (in host memory), and
  264. * write a doorbell.
  265. */
  266. void efx_farch_tx_write(struct efx_tx_queue *tx_queue)
  267. {
  268. struct efx_tx_buffer *buffer;
  269. efx_qword_t *txd;
  270. unsigned write_ptr;
  271. unsigned old_write_count = tx_queue->write_count;
  272. tx_queue->xmit_more_available = false;
  273. if (unlikely(tx_queue->write_count == tx_queue->insert_count))
  274. return;
  275. do {
  276. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  277. buffer = &tx_queue->buffer[write_ptr];
  278. txd = efx_tx_desc(tx_queue, write_ptr);
  279. ++tx_queue->write_count;
  280. EFX_BUG_ON_PARANOID(buffer->flags & EFX_TX_BUF_OPTION);
  281. /* Create TX descriptor ring entry */
  282. BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
  283. EFX_POPULATE_QWORD_4(*txd,
  284. FSF_AZ_TX_KER_CONT,
  285. buffer->flags & EFX_TX_BUF_CONT,
  286. FSF_AZ_TX_KER_BYTE_COUNT, buffer->len,
  287. FSF_AZ_TX_KER_BUF_REGION, 0,
  288. FSF_AZ_TX_KER_BUF_ADDR, buffer->dma_addr);
  289. } while (tx_queue->write_count != tx_queue->insert_count);
  290. wmb(); /* Ensure descriptors are written before they are fetched */
  291. if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
  292. txd = efx_tx_desc(tx_queue,
  293. old_write_count & tx_queue->ptr_mask);
  294. efx_farch_push_tx_desc(tx_queue, txd);
  295. ++tx_queue->pushes;
  296. } else {
  297. efx_farch_notify_tx_desc(tx_queue);
  298. }
  299. }
  300. /* Allocate hardware resources for a TX queue */
  301. int efx_farch_tx_probe(struct efx_tx_queue *tx_queue)
  302. {
  303. struct efx_nic *efx = tx_queue->efx;
  304. unsigned entries;
  305. entries = tx_queue->ptr_mask + 1;
  306. return efx_alloc_special_buffer(efx, &tx_queue->txd,
  307. entries * sizeof(efx_qword_t));
  308. }
  309. void efx_farch_tx_init(struct efx_tx_queue *tx_queue)
  310. {
  311. struct efx_nic *efx = tx_queue->efx;
  312. efx_oword_t reg;
  313. /* Pin TX descriptor ring */
  314. efx_init_special_buffer(efx, &tx_queue->txd);
  315. /* Push TX descriptor ring to card */
  316. EFX_POPULATE_OWORD_10(reg,
  317. FRF_AZ_TX_DESCQ_EN, 1,
  318. FRF_AZ_TX_ISCSI_DDIG_EN, 0,
  319. FRF_AZ_TX_ISCSI_HDIG_EN, 0,
  320. FRF_AZ_TX_DESCQ_BUF_BASE_ID, tx_queue->txd.index,
  321. FRF_AZ_TX_DESCQ_EVQ_ID,
  322. tx_queue->channel->channel,
  323. FRF_AZ_TX_DESCQ_OWNER_ID, 0,
  324. FRF_AZ_TX_DESCQ_LABEL, tx_queue->queue,
  325. FRF_AZ_TX_DESCQ_SIZE,
  326. __ffs(tx_queue->txd.entries),
  327. FRF_AZ_TX_DESCQ_TYPE, 0,
  328. FRF_BZ_TX_NON_IP_DROP_DIS, 1);
  329. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  330. int csum = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
  331. EFX_SET_OWORD_FIELD(reg, FRF_BZ_TX_IP_CHKSM_DIS, !csum);
  332. EFX_SET_OWORD_FIELD(reg, FRF_BZ_TX_TCP_CHKSM_DIS,
  333. !csum);
  334. }
  335. efx_writeo_table(efx, &reg, efx->type->txd_ptr_tbl_base,
  336. tx_queue->queue);
  337. if (efx_nic_rev(efx) < EFX_REV_FALCON_B0) {
  338. /* Only 128 bits in this register */
  339. BUILD_BUG_ON(EFX_MAX_TX_QUEUES > 128);
  340. efx_reado(efx, &reg, FR_AA_TX_CHKSM_CFG);
  341. if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD)
  342. __clear_bit_le(tx_queue->queue, &reg);
  343. else
  344. __set_bit_le(tx_queue->queue, &reg);
  345. efx_writeo(efx, &reg, FR_AA_TX_CHKSM_CFG);
  346. }
  347. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  348. EFX_POPULATE_OWORD_1(reg,
  349. FRF_BZ_TX_PACE,
  350. (tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
  351. FFE_BZ_TX_PACE_OFF :
  352. FFE_BZ_TX_PACE_RESERVED);
  353. efx_writeo_table(efx, &reg, FR_BZ_TX_PACE_TBL,
  354. tx_queue->queue);
  355. }
  356. }
  357. static void efx_farch_flush_tx_queue(struct efx_tx_queue *tx_queue)
  358. {
  359. struct efx_nic *efx = tx_queue->efx;
  360. efx_oword_t tx_flush_descq;
  361. WARN_ON(atomic_read(&tx_queue->flush_outstanding));
  362. atomic_set(&tx_queue->flush_outstanding, 1);
  363. EFX_POPULATE_OWORD_2(tx_flush_descq,
  364. FRF_AZ_TX_FLUSH_DESCQ_CMD, 1,
  365. FRF_AZ_TX_FLUSH_DESCQ, tx_queue->queue);
  366. efx_writeo(efx, &tx_flush_descq, FR_AZ_TX_FLUSH_DESCQ);
  367. }
  368. void efx_farch_tx_fini(struct efx_tx_queue *tx_queue)
  369. {
  370. struct efx_nic *efx = tx_queue->efx;
  371. efx_oword_t tx_desc_ptr;
  372. /* Remove TX descriptor ring from card */
  373. EFX_ZERO_OWORD(tx_desc_ptr);
  374. efx_writeo_table(efx, &tx_desc_ptr, efx->type->txd_ptr_tbl_base,
  375. tx_queue->queue);
  376. /* Unpin TX descriptor ring */
  377. efx_fini_special_buffer(efx, &tx_queue->txd);
  378. }
  379. /* Free buffers backing TX queue */
  380. void efx_farch_tx_remove(struct efx_tx_queue *tx_queue)
  381. {
  382. efx_free_special_buffer(tx_queue->efx, &tx_queue->txd);
  383. }
  384. /**************************************************************************
  385. *
  386. * RX path
  387. *
  388. **************************************************************************/
  389. /* This creates an entry in the RX descriptor queue */
  390. static inline void
  391. efx_farch_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned index)
  392. {
  393. struct efx_rx_buffer *rx_buf;
  394. efx_qword_t *rxd;
  395. rxd = efx_rx_desc(rx_queue, index);
  396. rx_buf = efx_rx_buffer(rx_queue, index);
  397. EFX_POPULATE_QWORD_3(*rxd,
  398. FSF_AZ_RX_KER_BUF_SIZE,
  399. rx_buf->len -
  400. rx_queue->efx->type->rx_buffer_padding,
  401. FSF_AZ_RX_KER_BUF_REGION, 0,
  402. FSF_AZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
  403. }
  404. /* This writes to the RX_DESC_WPTR register for the specified receive
  405. * descriptor ring.
  406. */
  407. void efx_farch_rx_write(struct efx_rx_queue *rx_queue)
  408. {
  409. struct efx_nic *efx = rx_queue->efx;
  410. efx_dword_t reg;
  411. unsigned write_ptr;
  412. while (rx_queue->notified_count != rx_queue->added_count) {
  413. efx_farch_build_rx_desc(
  414. rx_queue,
  415. rx_queue->notified_count & rx_queue->ptr_mask);
  416. ++rx_queue->notified_count;
  417. }
  418. wmb();
  419. write_ptr = rx_queue->added_count & rx_queue->ptr_mask;
  420. EFX_POPULATE_DWORD_1(reg, FRF_AZ_RX_DESC_WPTR_DWORD, write_ptr);
  421. efx_writed_page(efx, &reg, FR_AZ_RX_DESC_UPD_DWORD_P0,
  422. efx_rx_queue_index(rx_queue));
  423. }
  424. int efx_farch_rx_probe(struct efx_rx_queue *rx_queue)
  425. {
  426. struct efx_nic *efx = rx_queue->efx;
  427. unsigned entries;
  428. entries = rx_queue->ptr_mask + 1;
  429. return efx_alloc_special_buffer(efx, &rx_queue->rxd,
  430. entries * sizeof(efx_qword_t));
  431. }
  432. void efx_farch_rx_init(struct efx_rx_queue *rx_queue)
  433. {
  434. efx_oword_t rx_desc_ptr;
  435. struct efx_nic *efx = rx_queue->efx;
  436. bool is_b0 = efx_nic_rev(efx) >= EFX_REV_FALCON_B0;
  437. bool iscsi_digest_en = is_b0;
  438. bool jumbo_en;
  439. /* For kernel-mode queues in Falcon A1, the JUMBO flag enables
  440. * DMA to continue after a PCIe page boundary (and scattering
  441. * is not possible). In Falcon B0 and Siena, it enables
  442. * scatter.
  443. */
  444. jumbo_en = !is_b0 || efx->rx_scatter;
  445. netif_dbg(efx, hw, efx->net_dev,
  446. "RX queue %d ring in special buffers %d-%d\n",
  447. efx_rx_queue_index(rx_queue), rx_queue->rxd.index,
  448. rx_queue->rxd.index + rx_queue->rxd.entries - 1);
  449. rx_queue->scatter_n = 0;
  450. /* Pin RX descriptor ring */
  451. efx_init_special_buffer(efx, &rx_queue->rxd);
  452. /* Push RX descriptor ring to card */
  453. EFX_POPULATE_OWORD_10(rx_desc_ptr,
  454. FRF_AZ_RX_ISCSI_DDIG_EN, iscsi_digest_en,
  455. FRF_AZ_RX_ISCSI_HDIG_EN, iscsi_digest_en,
  456. FRF_AZ_RX_DESCQ_BUF_BASE_ID, rx_queue->rxd.index,
  457. FRF_AZ_RX_DESCQ_EVQ_ID,
  458. efx_rx_queue_channel(rx_queue)->channel,
  459. FRF_AZ_RX_DESCQ_OWNER_ID, 0,
  460. FRF_AZ_RX_DESCQ_LABEL,
  461. efx_rx_queue_index(rx_queue),
  462. FRF_AZ_RX_DESCQ_SIZE,
  463. __ffs(rx_queue->rxd.entries),
  464. FRF_AZ_RX_DESCQ_TYPE, 0 /* kernel queue */ ,
  465. FRF_AZ_RX_DESCQ_JUMBO, jumbo_en,
  466. FRF_AZ_RX_DESCQ_EN, 1);
  467. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  468. efx_rx_queue_index(rx_queue));
  469. }
  470. static void efx_farch_flush_rx_queue(struct efx_rx_queue *rx_queue)
  471. {
  472. struct efx_nic *efx = rx_queue->efx;
  473. efx_oword_t rx_flush_descq;
  474. EFX_POPULATE_OWORD_2(rx_flush_descq,
  475. FRF_AZ_RX_FLUSH_DESCQ_CMD, 1,
  476. FRF_AZ_RX_FLUSH_DESCQ,
  477. efx_rx_queue_index(rx_queue));
  478. efx_writeo(efx, &rx_flush_descq, FR_AZ_RX_FLUSH_DESCQ);
  479. }
  480. void efx_farch_rx_fini(struct efx_rx_queue *rx_queue)
  481. {
  482. efx_oword_t rx_desc_ptr;
  483. struct efx_nic *efx = rx_queue->efx;
  484. /* Remove RX descriptor ring from card */
  485. EFX_ZERO_OWORD(rx_desc_ptr);
  486. efx_writeo_table(efx, &rx_desc_ptr, efx->type->rxd_ptr_tbl_base,
  487. efx_rx_queue_index(rx_queue));
  488. /* Unpin RX descriptor ring */
  489. efx_fini_special_buffer(efx, &rx_queue->rxd);
  490. }
  491. /* Free buffers backing RX queue */
  492. void efx_farch_rx_remove(struct efx_rx_queue *rx_queue)
  493. {
  494. efx_free_special_buffer(rx_queue->efx, &rx_queue->rxd);
  495. }
  496. /**************************************************************************
  497. *
  498. * Flush handling
  499. *
  500. **************************************************************************/
  501. /* efx_farch_flush_queues() must be woken up when all flushes are completed,
  502. * or more RX flushes can be kicked off.
  503. */
  504. static bool efx_farch_flush_wake(struct efx_nic *efx)
  505. {
  506. /* Ensure that all updates are visible to efx_farch_flush_queues() */
  507. smp_mb();
  508. return (atomic_read(&efx->active_queues) == 0 ||
  509. (atomic_read(&efx->rxq_flush_outstanding) < EFX_RX_FLUSH_COUNT
  510. && atomic_read(&efx->rxq_flush_pending) > 0));
  511. }
  512. static bool efx_check_tx_flush_complete(struct efx_nic *efx)
  513. {
  514. bool i = true;
  515. efx_oword_t txd_ptr_tbl;
  516. struct efx_channel *channel;
  517. struct efx_tx_queue *tx_queue;
  518. efx_for_each_channel(channel, efx) {
  519. efx_for_each_channel_tx_queue(tx_queue, channel) {
  520. efx_reado_table(efx, &txd_ptr_tbl,
  521. FR_BZ_TX_DESC_PTR_TBL, tx_queue->queue);
  522. if (EFX_OWORD_FIELD(txd_ptr_tbl,
  523. FRF_AZ_TX_DESCQ_FLUSH) ||
  524. EFX_OWORD_FIELD(txd_ptr_tbl,
  525. FRF_AZ_TX_DESCQ_EN)) {
  526. netif_dbg(efx, hw, efx->net_dev,
  527. "flush did not complete on TXQ %d\n",
  528. tx_queue->queue);
  529. i = false;
  530. } else if (atomic_cmpxchg(&tx_queue->flush_outstanding,
  531. 1, 0)) {
  532. /* The flush is complete, but we didn't
  533. * receive a flush completion event
  534. */
  535. netif_dbg(efx, hw, efx->net_dev,
  536. "flush complete on TXQ %d, so drain "
  537. "the queue\n", tx_queue->queue);
  538. /* Don't need to increment active_queues as it
  539. * has already been incremented for the queues
  540. * which did not drain
  541. */
  542. efx_farch_magic_event(channel,
  543. EFX_CHANNEL_MAGIC_TX_DRAIN(
  544. tx_queue));
  545. }
  546. }
  547. }
  548. return i;
  549. }
  550. /* Flush all the transmit queues, and continue flushing receive queues until
  551. * they're all flushed. Wait for the DRAIN events to be received so that there
  552. * are no more RX and TX events left on any channel. */
  553. static int efx_farch_do_flush(struct efx_nic *efx)
  554. {
  555. unsigned timeout = msecs_to_jiffies(5000); /* 5s for all flushes and drains */
  556. struct efx_channel *channel;
  557. struct efx_rx_queue *rx_queue;
  558. struct efx_tx_queue *tx_queue;
  559. int rc = 0;
  560. efx_for_each_channel(channel, efx) {
  561. efx_for_each_channel_tx_queue(tx_queue, channel) {
  562. efx_farch_flush_tx_queue(tx_queue);
  563. }
  564. efx_for_each_channel_rx_queue(rx_queue, channel) {
  565. rx_queue->flush_pending = true;
  566. atomic_inc(&efx->rxq_flush_pending);
  567. }
  568. }
  569. while (timeout && atomic_read(&efx->active_queues) > 0) {
  570. /* If SRIOV is enabled, then offload receive queue flushing to
  571. * the firmware (though we will still have to poll for
  572. * completion). If that fails, fall back to the old scheme.
  573. */
  574. if (efx_siena_sriov_enabled(efx)) {
  575. rc = efx_mcdi_flush_rxqs(efx);
  576. if (!rc)
  577. goto wait;
  578. }
  579. /* The hardware supports four concurrent rx flushes, each of
  580. * which may need to be retried if there is an outstanding
  581. * descriptor fetch
  582. */
  583. efx_for_each_channel(channel, efx) {
  584. efx_for_each_channel_rx_queue(rx_queue, channel) {
  585. if (atomic_read(&efx->rxq_flush_outstanding) >=
  586. EFX_RX_FLUSH_COUNT)
  587. break;
  588. if (rx_queue->flush_pending) {
  589. rx_queue->flush_pending = false;
  590. atomic_dec(&efx->rxq_flush_pending);
  591. atomic_inc(&efx->rxq_flush_outstanding);
  592. efx_farch_flush_rx_queue(rx_queue);
  593. }
  594. }
  595. }
  596. wait:
  597. timeout = wait_event_timeout(efx->flush_wq,
  598. efx_farch_flush_wake(efx),
  599. timeout);
  600. }
  601. if (atomic_read(&efx->active_queues) &&
  602. !efx_check_tx_flush_complete(efx)) {
  603. netif_err(efx, hw, efx->net_dev, "failed to flush %d queues "
  604. "(rx %d+%d)\n", atomic_read(&efx->active_queues),
  605. atomic_read(&efx->rxq_flush_outstanding),
  606. atomic_read(&efx->rxq_flush_pending));
  607. rc = -ETIMEDOUT;
  608. atomic_set(&efx->active_queues, 0);
  609. atomic_set(&efx->rxq_flush_pending, 0);
  610. atomic_set(&efx->rxq_flush_outstanding, 0);
  611. }
  612. return rc;
  613. }
  614. int efx_farch_fini_dmaq(struct efx_nic *efx)
  615. {
  616. struct efx_channel *channel;
  617. struct efx_tx_queue *tx_queue;
  618. struct efx_rx_queue *rx_queue;
  619. int rc = 0;
  620. /* Do not attempt to write to the NIC during EEH recovery */
  621. if (efx->state != STATE_RECOVERY) {
  622. /* Only perform flush if DMA is enabled */
  623. if (efx->pci_dev->is_busmaster) {
  624. efx->type->prepare_flush(efx);
  625. rc = efx_farch_do_flush(efx);
  626. efx->type->finish_flush(efx);
  627. }
  628. efx_for_each_channel(channel, efx) {
  629. efx_for_each_channel_rx_queue(rx_queue, channel)
  630. efx_farch_rx_fini(rx_queue);
  631. efx_for_each_channel_tx_queue(tx_queue, channel)
  632. efx_farch_tx_fini(tx_queue);
  633. }
  634. }
  635. return rc;
  636. }
  637. /* Reset queue and flush accounting after FLR
  638. *
  639. * One possible cause of FLR recovery is that DMA may be failing (eg. if bus
  640. * mastering was disabled), in which case we don't receive (RXQ) flush
  641. * completion events. This means that efx->rxq_flush_outstanding remained at 4
  642. * after the FLR; also, efx->active_queues was non-zero (as no flush completion
  643. * events were received, and we didn't go through efx_check_tx_flush_complete())
  644. * If we don't fix this up, on the next call to efx_realloc_channels() we won't
  645. * flush any RX queues because efx->rxq_flush_outstanding is at the limit of 4
  646. * for batched flush requests; and the efx->active_queues gets messed up because
  647. * we keep incrementing for the newly initialised queues, but it never went to
  648. * zero previously. Then we get a timeout every time we try to restart the
  649. * queues, as it doesn't go back to zero when we should be flushing the queues.
  650. */
  651. void efx_farch_finish_flr(struct efx_nic *efx)
  652. {
  653. atomic_set(&efx->rxq_flush_pending, 0);
  654. atomic_set(&efx->rxq_flush_outstanding, 0);
  655. atomic_set(&efx->active_queues, 0);
  656. }
  657. /**************************************************************************
  658. *
  659. * Event queue processing
  660. * Event queues are processed by per-channel tasklets.
  661. *
  662. **************************************************************************/
  663. /* Update a channel's event queue's read pointer (RPTR) register
  664. *
  665. * This writes the EVQ_RPTR_REG register for the specified channel's
  666. * event queue.
  667. */
  668. void efx_farch_ev_read_ack(struct efx_channel *channel)
  669. {
  670. efx_dword_t reg;
  671. struct efx_nic *efx = channel->efx;
  672. EFX_POPULATE_DWORD_1(reg, FRF_AZ_EVQ_RPTR,
  673. channel->eventq_read_ptr & channel->eventq_mask);
  674. /* For Falcon A1, EVQ_RPTR_KER is documented as having a step size
  675. * of 4 bytes, but it is really 16 bytes just like later revisions.
  676. */
  677. efx_writed(efx, &reg,
  678. efx->type->evq_rptr_tbl_base +
  679. FR_BZ_EVQ_RPTR_STEP * channel->channel);
  680. }
  681. /* Use HW to insert a SW defined event */
  682. void efx_farch_generate_event(struct efx_nic *efx, unsigned int evq,
  683. efx_qword_t *event)
  684. {
  685. efx_oword_t drv_ev_reg;
  686. BUILD_BUG_ON(FRF_AZ_DRV_EV_DATA_LBN != 0 ||
  687. FRF_AZ_DRV_EV_DATA_WIDTH != 64);
  688. drv_ev_reg.u32[0] = event->u32[0];
  689. drv_ev_reg.u32[1] = event->u32[1];
  690. drv_ev_reg.u32[2] = 0;
  691. drv_ev_reg.u32[3] = 0;
  692. EFX_SET_OWORD_FIELD(drv_ev_reg, FRF_AZ_DRV_EV_QID, evq);
  693. efx_writeo(efx, &drv_ev_reg, FR_AZ_DRV_EV);
  694. }
  695. static void efx_farch_magic_event(struct efx_channel *channel, u32 magic)
  696. {
  697. efx_qword_t event;
  698. EFX_POPULATE_QWORD_2(event, FSF_AZ_EV_CODE,
  699. FSE_AZ_EV_CODE_DRV_GEN_EV,
  700. FSF_AZ_DRV_GEN_EV_MAGIC, magic);
  701. efx_farch_generate_event(channel->efx, channel->channel, &event);
  702. }
  703. /* Handle a transmit completion event
  704. *
  705. * The NIC batches TX completion events; the message we receive is of
  706. * the form "complete all TX events up to this index".
  707. */
  708. static int
  709. efx_farch_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
  710. {
  711. unsigned int tx_ev_desc_ptr;
  712. unsigned int tx_ev_q_label;
  713. struct efx_tx_queue *tx_queue;
  714. struct efx_nic *efx = channel->efx;
  715. int tx_packets = 0;
  716. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  717. return 0;
  718. if (likely(EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_COMP))) {
  719. /* Transmit completion */
  720. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_DESC_PTR);
  721. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  722. tx_queue = efx_channel_get_tx_queue(
  723. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  724. tx_packets = ((tx_ev_desc_ptr - tx_queue->read_count) &
  725. tx_queue->ptr_mask);
  726. efx_xmit_done(tx_queue, tx_ev_desc_ptr);
  727. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_WQ_FF_FULL)) {
  728. /* Rewrite the FIFO write pointer */
  729. tx_ev_q_label = EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_Q_LABEL);
  730. tx_queue = efx_channel_get_tx_queue(
  731. channel, tx_ev_q_label % EFX_TXQ_TYPES);
  732. netif_tx_lock(efx->net_dev);
  733. efx_farch_notify_tx_desc(tx_queue);
  734. netif_tx_unlock(efx->net_dev);
  735. } else if (EFX_QWORD_FIELD(*event, FSF_AZ_TX_EV_PKT_ERR)) {
  736. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  737. } else {
  738. netif_err(efx, tx_err, efx->net_dev,
  739. "channel %d unexpected TX event "
  740. EFX_QWORD_FMT"\n", channel->channel,
  741. EFX_QWORD_VAL(*event));
  742. }
  743. return tx_packets;
  744. }
  745. /* Detect errors included in the rx_evt_pkt_ok bit. */
  746. static u16 efx_farch_handle_rx_not_ok(struct efx_rx_queue *rx_queue,
  747. const efx_qword_t *event)
  748. {
  749. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  750. struct efx_nic *efx = rx_queue->efx;
  751. bool rx_ev_buf_owner_id_err, rx_ev_ip_hdr_chksum_err;
  752. bool rx_ev_tcp_udp_chksum_err, rx_ev_eth_crc_err;
  753. bool rx_ev_frm_trunc, rx_ev_drib_nib, rx_ev_tobe_disc;
  754. bool rx_ev_other_err, rx_ev_pause_frm;
  755. bool rx_ev_hdr_type, rx_ev_mcast_pkt;
  756. unsigned rx_ev_pkt_type;
  757. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  758. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  759. rx_ev_tobe_disc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_TOBE_DISC);
  760. rx_ev_pkt_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_TYPE);
  761. rx_ev_buf_owner_id_err = EFX_QWORD_FIELD(*event,
  762. FSF_AZ_RX_EV_BUF_OWNER_ID_ERR);
  763. rx_ev_ip_hdr_chksum_err = EFX_QWORD_FIELD(*event,
  764. FSF_AZ_RX_EV_IP_HDR_CHKSUM_ERR);
  765. rx_ev_tcp_udp_chksum_err = EFX_QWORD_FIELD(*event,
  766. FSF_AZ_RX_EV_TCP_UDP_CHKSUM_ERR);
  767. rx_ev_eth_crc_err = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_ETH_CRC_ERR);
  768. rx_ev_frm_trunc = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_FRM_TRUNC);
  769. rx_ev_drib_nib = ((efx_nic_rev(efx) >= EFX_REV_FALCON_B0) ?
  770. 0 : EFX_QWORD_FIELD(*event, FSF_AA_RX_EV_DRIB_NIB));
  771. rx_ev_pause_frm = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PAUSE_FRM_ERR);
  772. /* Every error apart from tobe_disc and pause_frm */
  773. rx_ev_other_err = (rx_ev_drib_nib | rx_ev_tcp_udp_chksum_err |
  774. rx_ev_buf_owner_id_err | rx_ev_eth_crc_err |
  775. rx_ev_frm_trunc | rx_ev_ip_hdr_chksum_err);
  776. /* Count errors that are not in MAC stats. Ignore expected
  777. * checksum errors during self-test. */
  778. if (rx_ev_frm_trunc)
  779. ++channel->n_rx_frm_trunc;
  780. else if (rx_ev_tobe_disc)
  781. ++channel->n_rx_tobe_disc;
  782. else if (!efx->loopback_selftest) {
  783. if (rx_ev_ip_hdr_chksum_err)
  784. ++channel->n_rx_ip_hdr_chksum_err;
  785. else if (rx_ev_tcp_udp_chksum_err)
  786. ++channel->n_rx_tcp_udp_chksum_err;
  787. }
  788. /* TOBE_DISC is expected on unicast mismatches; don't print out an
  789. * error message. FRM_TRUNC indicates RXDP dropped the packet due
  790. * to a FIFO overflow.
  791. */
  792. #ifdef DEBUG
  793. if (rx_ev_other_err && net_ratelimit()) {
  794. netif_dbg(efx, rx_err, efx->net_dev,
  795. " RX queue %d unexpected RX event "
  796. EFX_QWORD_FMT "%s%s%s%s%s%s%s%s\n",
  797. efx_rx_queue_index(rx_queue), EFX_QWORD_VAL(*event),
  798. rx_ev_buf_owner_id_err ? " [OWNER_ID_ERR]" : "",
  799. rx_ev_ip_hdr_chksum_err ?
  800. " [IP_HDR_CHKSUM_ERR]" : "",
  801. rx_ev_tcp_udp_chksum_err ?
  802. " [TCP_UDP_CHKSUM_ERR]" : "",
  803. rx_ev_eth_crc_err ? " [ETH_CRC_ERR]" : "",
  804. rx_ev_frm_trunc ? " [FRM_TRUNC]" : "",
  805. rx_ev_drib_nib ? " [DRIB_NIB]" : "",
  806. rx_ev_tobe_disc ? " [TOBE_DISC]" : "",
  807. rx_ev_pause_frm ? " [PAUSE]" : "");
  808. }
  809. #endif
  810. /* The frame must be discarded if any of these are true. */
  811. return (rx_ev_eth_crc_err | rx_ev_frm_trunc | rx_ev_drib_nib |
  812. rx_ev_tobe_disc | rx_ev_pause_frm) ?
  813. EFX_RX_PKT_DISCARD : 0;
  814. }
  815. /* Handle receive events that are not in-order. Return true if this
  816. * can be handled as a partial packet discard, false if it's more
  817. * serious.
  818. */
  819. static bool
  820. efx_farch_handle_rx_bad_index(struct efx_rx_queue *rx_queue, unsigned index)
  821. {
  822. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  823. struct efx_nic *efx = rx_queue->efx;
  824. unsigned expected, dropped;
  825. if (rx_queue->scatter_n &&
  826. index == ((rx_queue->removed_count + rx_queue->scatter_n - 1) &
  827. rx_queue->ptr_mask)) {
  828. ++channel->n_rx_nodesc_trunc;
  829. return true;
  830. }
  831. expected = rx_queue->removed_count & rx_queue->ptr_mask;
  832. dropped = (index - expected) & rx_queue->ptr_mask;
  833. netif_info(efx, rx_err, efx->net_dev,
  834. "dropped %d events (index=%d expected=%d)\n",
  835. dropped, index, expected);
  836. efx_schedule_reset(efx, EFX_WORKAROUND_5676(efx) ?
  837. RESET_TYPE_RX_RECOVERY : RESET_TYPE_DISABLE);
  838. return false;
  839. }
  840. /* Handle a packet received event
  841. *
  842. * The NIC gives a "discard" flag if it's a unicast packet with the
  843. * wrong destination address
  844. * Also "is multicast" and "matches multicast filter" flags can be used to
  845. * discard non-matching multicast packets.
  846. */
  847. static void
  848. efx_farch_handle_rx_event(struct efx_channel *channel, const efx_qword_t *event)
  849. {
  850. unsigned int rx_ev_desc_ptr, rx_ev_byte_cnt;
  851. unsigned int rx_ev_hdr_type, rx_ev_mcast_pkt;
  852. unsigned expected_ptr;
  853. bool rx_ev_pkt_ok, rx_ev_sop, rx_ev_cont;
  854. u16 flags;
  855. struct efx_rx_queue *rx_queue;
  856. struct efx_nic *efx = channel->efx;
  857. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  858. return;
  859. rx_ev_cont = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_JUMBO_CONT);
  860. rx_ev_sop = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_SOP);
  861. WARN_ON(EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_Q_LABEL) !=
  862. channel->channel);
  863. rx_queue = efx_channel_get_rx_queue(channel);
  864. rx_ev_desc_ptr = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_DESC_PTR);
  865. expected_ptr = ((rx_queue->removed_count + rx_queue->scatter_n) &
  866. rx_queue->ptr_mask);
  867. /* Check for partial drops and other errors */
  868. if (unlikely(rx_ev_desc_ptr != expected_ptr) ||
  869. unlikely(rx_ev_sop != (rx_queue->scatter_n == 0))) {
  870. if (rx_ev_desc_ptr != expected_ptr &&
  871. !efx_farch_handle_rx_bad_index(rx_queue, rx_ev_desc_ptr))
  872. return;
  873. /* Discard all pending fragments */
  874. if (rx_queue->scatter_n) {
  875. efx_rx_packet(
  876. rx_queue,
  877. rx_queue->removed_count & rx_queue->ptr_mask,
  878. rx_queue->scatter_n, 0, EFX_RX_PKT_DISCARD);
  879. rx_queue->removed_count += rx_queue->scatter_n;
  880. rx_queue->scatter_n = 0;
  881. }
  882. /* Return if there is no new fragment */
  883. if (rx_ev_desc_ptr != expected_ptr)
  884. return;
  885. /* Discard new fragment if not SOP */
  886. if (!rx_ev_sop) {
  887. efx_rx_packet(
  888. rx_queue,
  889. rx_queue->removed_count & rx_queue->ptr_mask,
  890. 1, 0, EFX_RX_PKT_DISCARD);
  891. ++rx_queue->removed_count;
  892. return;
  893. }
  894. }
  895. ++rx_queue->scatter_n;
  896. if (rx_ev_cont)
  897. return;
  898. rx_ev_byte_cnt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_BYTE_CNT);
  899. rx_ev_pkt_ok = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_PKT_OK);
  900. rx_ev_hdr_type = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_HDR_TYPE);
  901. if (likely(rx_ev_pkt_ok)) {
  902. /* If packet is marked as OK then we can rely on the
  903. * hardware checksum and classification.
  904. */
  905. flags = 0;
  906. switch (rx_ev_hdr_type) {
  907. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_TCP:
  908. flags |= EFX_RX_PKT_TCP;
  909. /* fall through */
  910. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_UDP:
  911. flags |= EFX_RX_PKT_CSUMMED;
  912. /* fall through */
  913. case FSE_CZ_RX_EV_HDR_TYPE_IPV4V6_OTHER:
  914. case FSE_AZ_RX_EV_HDR_TYPE_OTHER:
  915. break;
  916. }
  917. } else {
  918. flags = efx_farch_handle_rx_not_ok(rx_queue, event);
  919. }
  920. /* Detect multicast packets that didn't match the filter */
  921. rx_ev_mcast_pkt = EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_PKT);
  922. if (rx_ev_mcast_pkt) {
  923. unsigned int rx_ev_mcast_hash_match =
  924. EFX_QWORD_FIELD(*event, FSF_AZ_RX_EV_MCAST_HASH_MATCH);
  925. if (unlikely(!rx_ev_mcast_hash_match)) {
  926. ++channel->n_rx_mcast_mismatch;
  927. flags |= EFX_RX_PKT_DISCARD;
  928. }
  929. }
  930. channel->irq_mod_score += 2;
  931. /* Handle received packet */
  932. efx_rx_packet(rx_queue,
  933. rx_queue->removed_count & rx_queue->ptr_mask,
  934. rx_queue->scatter_n, rx_ev_byte_cnt, flags);
  935. rx_queue->removed_count += rx_queue->scatter_n;
  936. rx_queue->scatter_n = 0;
  937. }
  938. /* If this flush done event corresponds to a &struct efx_tx_queue, then
  939. * send an %EFX_CHANNEL_MAGIC_TX_DRAIN event to drain the event queue
  940. * of all transmit completions.
  941. */
  942. static void
  943. efx_farch_handle_tx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  944. {
  945. struct efx_tx_queue *tx_queue;
  946. int qid;
  947. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  948. if (qid < EFX_TXQ_TYPES * efx->n_tx_channels) {
  949. tx_queue = efx_get_tx_queue(efx, qid / EFX_TXQ_TYPES,
  950. qid % EFX_TXQ_TYPES);
  951. if (atomic_cmpxchg(&tx_queue->flush_outstanding, 1, 0)) {
  952. efx_farch_magic_event(tx_queue->channel,
  953. EFX_CHANNEL_MAGIC_TX_DRAIN(tx_queue));
  954. }
  955. }
  956. }
  957. /* If this flush done event corresponds to a &struct efx_rx_queue: If the flush
  958. * was successful then send an %EFX_CHANNEL_MAGIC_RX_DRAIN, otherwise add
  959. * the RX queue back to the mask of RX queues in need of flushing.
  960. */
  961. static void
  962. efx_farch_handle_rx_flush_done(struct efx_nic *efx, efx_qword_t *event)
  963. {
  964. struct efx_channel *channel;
  965. struct efx_rx_queue *rx_queue;
  966. int qid;
  967. bool failed;
  968. qid = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_DESCQ_ID);
  969. failed = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_RX_FLUSH_FAIL);
  970. if (qid >= efx->n_channels)
  971. return;
  972. channel = efx_get_channel(efx, qid);
  973. if (!efx_channel_has_rx_queue(channel))
  974. return;
  975. rx_queue = efx_channel_get_rx_queue(channel);
  976. if (failed) {
  977. netif_info(efx, hw, efx->net_dev,
  978. "RXQ %d flush retry\n", qid);
  979. rx_queue->flush_pending = true;
  980. atomic_inc(&efx->rxq_flush_pending);
  981. } else {
  982. efx_farch_magic_event(efx_rx_queue_channel(rx_queue),
  983. EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue));
  984. }
  985. atomic_dec(&efx->rxq_flush_outstanding);
  986. if (efx_farch_flush_wake(efx))
  987. wake_up(&efx->flush_wq);
  988. }
  989. static void
  990. efx_farch_handle_drain_event(struct efx_channel *channel)
  991. {
  992. struct efx_nic *efx = channel->efx;
  993. WARN_ON(atomic_read(&efx->active_queues) == 0);
  994. atomic_dec(&efx->active_queues);
  995. if (efx_farch_flush_wake(efx))
  996. wake_up(&efx->flush_wq);
  997. }
  998. static void efx_farch_handle_generated_event(struct efx_channel *channel,
  999. efx_qword_t *event)
  1000. {
  1001. struct efx_nic *efx = channel->efx;
  1002. struct efx_rx_queue *rx_queue =
  1003. efx_channel_has_rx_queue(channel) ?
  1004. efx_channel_get_rx_queue(channel) : NULL;
  1005. unsigned magic, code;
  1006. magic = EFX_QWORD_FIELD(*event, FSF_AZ_DRV_GEN_EV_MAGIC);
  1007. code = _EFX_CHANNEL_MAGIC_CODE(magic);
  1008. if (magic == EFX_CHANNEL_MAGIC_TEST(channel)) {
  1009. channel->event_test_cpu = raw_smp_processor_id();
  1010. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_FILL(rx_queue)) {
  1011. /* The queue must be empty, so we won't receive any rx
  1012. * events, so efx_process_channel() won't refill the
  1013. * queue. Refill it here */
  1014. efx_fast_push_rx_descriptors(rx_queue, true);
  1015. } else if (rx_queue && magic == EFX_CHANNEL_MAGIC_RX_DRAIN(rx_queue)) {
  1016. efx_farch_handle_drain_event(channel);
  1017. } else if (code == _EFX_CHANNEL_MAGIC_TX_DRAIN) {
  1018. efx_farch_handle_drain_event(channel);
  1019. } else {
  1020. netif_dbg(efx, hw, efx->net_dev, "channel %d received "
  1021. "generated event "EFX_QWORD_FMT"\n",
  1022. channel->channel, EFX_QWORD_VAL(*event));
  1023. }
  1024. }
  1025. static void
  1026. efx_farch_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  1027. {
  1028. struct efx_nic *efx = channel->efx;
  1029. unsigned int ev_sub_code;
  1030. unsigned int ev_sub_data;
  1031. ev_sub_code = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBCODE);
  1032. ev_sub_data = EFX_QWORD_FIELD(*event, FSF_AZ_DRIVER_EV_SUBDATA);
  1033. switch (ev_sub_code) {
  1034. case FSE_AZ_TX_DESCQ_FLS_DONE_EV:
  1035. netif_vdbg(efx, hw, efx->net_dev, "channel %d TXQ %d flushed\n",
  1036. channel->channel, ev_sub_data);
  1037. efx_farch_handle_tx_flush_done(efx, event);
  1038. #ifdef CONFIG_SFC_SRIOV
  1039. efx_siena_sriov_tx_flush_done(efx, event);
  1040. #endif
  1041. break;
  1042. case FSE_AZ_RX_DESCQ_FLS_DONE_EV:
  1043. netif_vdbg(efx, hw, efx->net_dev, "channel %d RXQ %d flushed\n",
  1044. channel->channel, ev_sub_data);
  1045. efx_farch_handle_rx_flush_done(efx, event);
  1046. #ifdef CONFIG_SFC_SRIOV
  1047. efx_siena_sriov_rx_flush_done(efx, event);
  1048. #endif
  1049. break;
  1050. case FSE_AZ_EVQ_INIT_DONE_EV:
  1051. netif_dbg(efx, hw, efx->net_dev,
  1052. "channel %d EVQ %d initialised\n",
  1053. channel->channel, ev_sub_data);
  1054. break;
  1055. case FSE_AZ_SRM_UPD_DONE_EV:
  1056. netif_vdbg(efx, hw, efx->net_dev,
  1057. "channel %d SRAM update done\n", channel->channel);
  1058. break;
  1059. case FSE_AZ_WAKE_UP_EV:
  1060. netif_vdbg(efx, hw, efx->net_dev,
  1061. "channel %d RXQ %d wakeup event\n",
  1062. channel->channel, ev_sub_data);
  1063. break;
  1064. case FSE_AZ_TIMER_EV:
  1065. netif_vdbg(efx, hw, efx->net_dev,
  1066. "channel %d RX queue %d timer expired\n",
  1067. channel->channel, ev_sub_data);
  1068. break;
  1069. case FSE_AA_RX_RECOVER_EV:
  1070. netif_err(efx, rx_err, efx->net_dev,
  1071. "channel %d seen DRIVER RX_RESET event. "
  1072. "Resetting.\n", channel->channel);
  1073. atomic_inc(&efx->rx_reset);
  1074. efx_schedule_reset(efx,
  1075. EFX_WORKAROUND_6555(efx) ?
  1076. RESET_TYPE_RX_RECOVERY :
  1077. RESET_TYPE_DISABLE);
  1078. break;
  1079. case FSE_BZ_RX_DSC_ERROR_EV:
  1080. if (ev_sub_data < EFX_VI_BASE) {
  1081. netif_err(efx, rx_err, efx->net_dev,
  1082. "RX DMA Q %d reports descriptor fetch error."
  1083. " RX Q %d is disabled.\n", ev_sub_data,
  1084. ev_sub_data);
  1085. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  1086. }
  1087. #ifdef CONFIG_SFC_SRIOV
  1088. else
  1089. efx_siena_sriov_desc_fetch_err(efx, ev_sub_data);
  1090. #endif
  1091. break;
  1092. case FSE_BZ_TX_DSC_ERROR_EV:
  1093. if (ev_sub_data < EFX_VI_BASE) {
  1094. netif_err(efx, tx_err, efx->net_dev,
  1095. "TX DMA Q %d reports descriptor fetch error."
  1096. " TX Q %d is disabled.\n", ev_sub_data,
  1097. ev_sub_data);
  1098. efx_schedule_reset(efx, RESET_TYPE_DMA_ERROR);
  1099. }
  1100. #ifdef CONFIG_SFC_SRIOV
  1101. else
  1102. efx_siena_sriov_desc_fetch_err(efx, ev_sub_data);
  1103. #endif
  1104. break;
  1105. default:
  1106. netif_vdbg(efx, hw, efx->net_dev,
  1107. "channel %d unknown driver event code %d "
  1108. "data %04x\n", channel->channel, ev_sub_code,
  1109. ev_sub_data);
  1110. break;
  1111. }
  1112. }
  1113. int efx_farch_ev_process(struct efx_channel *channel, int budget)
  1114. {
  1115. struct efx_nic *efx = channel->efx;
  1116. unsigned int read_ptr;
  1117. efx_qword_t event, *p_event;
  1118. int ev_code;
  1119. int tx_packets = 0;
  1120. int spent = 0;
  1121. if (budget <= 0)
  1122. return spent;
  1123. read_ptr = channel->eventq_read_ptr;
  1124. for (;;) {
  1125. p_event = efx_event(channel, read_ptr);
  1126. event = *p_event;
  1127. if (!efx_event_present(&event))
  1128. /* End of events */
  1129. break;
  1130. netif_vdbg(channel->efx, intr, channel->efx->net_dev,
  1131. "channel %d event is "EFX_QWORD_FMT"\n",
  1132. channel->channel, EFX_QWORD_VAL(event));
  1133. /* Clear this event by marking it all ones */
  1134. EFX_SET_QWORD(*p_event);
  1135. ++read_ptr;
  1136. ev_code = EFX_QWORD_FIELD(event, FSF_AZ_EV_CODE);
  1137. switch (ev_code) {
  1138. case FSE_AZ_EV_CODE_RX_EV:
  1139. efx_farch_handle_rx_event(channel, &event);
  1140. if (++spent == budget)
  1141. goto out;
  1142. break;
  1143. case FSE_AZ_EV_CODE_TX_EV:
  1144. tx_packets += efx_farch_handle_tx_event(channel,
  1145. &event);
  1146. if (tx_packets > efx->txq_entries) {
  1147. spent = budget;
  1148. goto out;
  1149. }
  1150. break;
  1151. case FSE_AZ_EV_CODE_DRV_GEN_EV:
  1152. efx_farch_handle_generated_event(channel, &event);
  1153. break;
  1154. case FSE_AZ_EV_CODE_DRIVER_EV:
  1155. efx_farch_handle_driver_event(channel, &event);
  1156. break;
  1157. #ifdef CONFIG_SFC_SRIOV
  1158. case FSE_CZ_EV_CODE_USER_EV:
  1159. efx_siena_sriov_event(channel, &event);
  1160. break;
  1161. #endif
  1162. case FSE_CZ_EV_CODE_MCDI_EV:
  1163. efx_mcdi_process_event(channel, &event);
  1164. break;
  1165. case FSE_AZ_EV_CODE_GLOBAL_EV:
  1166. if (efx->type->handle_global_event &&
  1167. efx->type->handle_global_event(channel, &event))
  1168. break;
  1169. /* else fall through */
  1170. default:
  1171. netif_err(channel->efx, hw, channel->efx->net_dev,
  1172. "channel %d unknown event type %d (data "
  1173. EFX_QWORD_FMT ")\n", channel->channel,
  1174. ev_code, EFX_QWORD_VAL(event));
  1175. }
  1176. }
  1177. out:
  1178. channel->eventq_read_ptr = read_ptr;
  1179. return spent;
  1180. }
  1181. /* Allocate buffer table entries for event queue */
  1182. int efx_farch_ev_probe(struct efx_channel *channel)
  1183. {
  1184. struct efx_nic *efx = channel->efx;
  1185. unsigned entries;
  1186. entries = channel->eventq_mask + 1;
  1187. return efx_alloc_special_buffer(efx, &channel->eventq,
  1188. entries * sizeof(efx_qword_t));
  1189. }
  1190. int efx_farch_ev_init(struct efx_channel *channel)
  1191. {
  1192. efx_oword_t reg;
  1193. struct efx_nic *efx = channel->efx;
  1194. netif_dbg(efx, hw, efx->net_dev,
  1195. "channel %d event queue in special buffers %d-%d\n",
  1196. channel->channel, channel->eventq.index,
  1197. channel->eventq.index + channel->eventq.entries - 1);
  1198. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  1199. EFX_POPULATE_OWORD_3(reg,
  1200. FRF_CZ_TIMER_Q_EN, 1,
  1201. FRF_CZ_HOST_NOTIFY_MODE, 0,
  1202. FRF_CZ_TIMER_MODE, FFE_CZ_TIMER_MODE_DIS);
  1203. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1204. }
  1205. /* Pin event queue buffer */
  1206. efx_init_special_buffer(efx, &channel->eventq);
  1207. /* Fill event queue with all ones (i.e. empty events) */
  1208. memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
  1209. /* Push event queue to card */
  1210. EFX_POPULATE_OWORD_3(reg,
  1211. FRF_AZ_EVQ_EN, 1,
  1212. FRF_AZ_EVQ_SIZE, __ffs(channel->eventq.entries),
  1213. FRF_AZ_EVQ_BUF_BASE_ID, channel->eventq.index);
  1214. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1215. channel->channel);
  1216. return 0;
  1217. }
  1218. void efx_farch_ev_fini(struct efx_channel *channel)
  1219. {
  1220. efx_oword_t reg;
  1221. struct efx_nic *efx = channel->efx;
  1222. /* Remove event queue from card */
  1223. EFX_ZERO_OWORD(reg);
  1224. efx_writeo_table(efx, &reg, efx->type->evq_ptr_tbl_base,
  1225. channel->channel);
  1226. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
  1227. efx_writeo_table(efx, &reg, FR_BZ_TIMER_TBL, channel->channel);
  1228. /* Unpin event queue */
  1229. efx_fini_special_buffer(efx, &channel->eventq);
  1230. }
  1231. /* Free buffers backing event queue */
  1232. void efx_farch_ev_remove(struct efx_channel *channel)
  1233. {
  1234. efx_free_special_buffer(channel->efx, &channel->eventq);
  1235. }
  1236. void efx_farch_ev_test_generate(struct efx_channel *channel)
  1237. {
  1238. efx_farch_magic_event(channel, EFX_CHANNEL_MAGIC_TEST(channel));
  1239. }
  1240. void efx_farch_rx_defer_refill(struct efx_rx_queue *rx_queue)
  1241. {
  1242. efx_farch_magic_event(efx_rx_queue_channel(rx_queue),
  1243. EFX_CHANNEL_MAGIC_FILL(rx_queue));
  1244. }
  1245. /**************************************************************************
  1246. *
  1247. * Hardware interrupts
  1248. * The hardware interrupt handler does very little work; all the event
  1249. * queue processing is carried out by per-channel tasklets.
  1250. *
  1251. **************************************************************************/
  1252. /* Enable/disable/generate interrupts */
  1253. static inline void efx_farch_interrupts(struct efx_nic *efx,
  1254. bool enabled, bool force)
  1255. {
  1256. efx_oword_t int_en_reg_ker;
  1257. EFX_POPULATE_OWORD_3(int_en_reg_ker,
  1258. FRF_AZ_KER_INT_LEVE_SEL, efx->irq_level,
  1259. FRF_AZ_KER_INT_KER, force,
  1260. FRF_AZ_DRV_INT_EN_KER, enabled);
  1261. efx_writeo(efx, &int_en_reg_ker, FR_AZ_INT_EN_KER);
  1262. }
  1263. void efx_farch_irq_enable_master(struct efx_nic *efx)
  1264. {
  1265. EFX_ZERO_OWORD(*((efx_oword_t *) efx->irq_status.addr));
  1266. wmb(); /* Ensure interrupt vector is clear before interrupts enabled */
  1267. efx_farch_interrupts(efx, true, false);
  1268. }
  1269. void efx_farch_irq_disable_master(struct efx_nic *efx)
  1270. {
  1271. /* Disable interrupts */
  1272. efx_farch_interrupts(efx, false, false);
  1273. }
  1274. /* Generate a test interrupt
  1275. * Interrupt must already have been enabled, otherwise nasty things
  1276. * may happen.
  1277. */
  1278. void efx_farch_irq_test_generate(struct efx_nic *efx)
  1279. {
  1280. efx_farch_interrupts(efx, true, true);
  1281. }
  1282. /* Process a fatal interrupt
  1283. * Disable bus mastering ASAP and schedule a reset
  1284. */
  1285. irqreturn_t efx_farch_fatal_interrupt(struct efx_nic *efx)
  1286. {
  1287. struct falcon_nic_data *nic_data = efx->nic_data;
  1288. efx_oword_t *int_ker = efx->irq_status.addr;
  1289. efx_oword_t fatal_intr;
  1290. int error, mem_perr;
  1291. efx_reado(efx, &fatal_intr, FR_AZ_FATAL_INTR_KER);
  1292. error = EFX_OWORD_FIELD(fatal_intr, FRF_AZ_FATAL_INTR);
  1293. netif_err(efx, hw, efx->net_dev, "SYSTEM ERROR "EFX_OWORD_FMT" status "
  1294. EFX_OWORD_FMT ": %s\n", EFX_OWORD_VAL(*int_ker),
  1295. EFX_OWORD_VAL(fatal_intr),
  1296. error ? "disabling bus mastering" : "no recognised error");
  1297. /* If this is a memory parity error dump which blocks are offending */
  1298. mem_perr = (EFX_OWORD_FIELD(fatal_intr, FRF_AZ_MEM_PERR_INT_KER) ||
  1299. EFX_OWORD_FIELD(fatal_intr, FRF_AZ_SRM_PERR_INT_KER));
  1300. if (mem_perr) {
  1301. efx_oword_t reg;
  1302. efx_reado(efx, &reg, FR_AZ_MEM_STAT);
  1303. netif_err(efx, hw, efx->net_dev,
  1304. "SYSTEM ERROR: memory parity error "EFX_OWORD_FMT"\n",
  1305. EFX_OWORD_VAL(reg));
  1306. }
  1307. /* Disable both devices */
  1308. pci_clear_master(efx->pci_dev);
  1309. if (efx_nic_is_dual_func(efx))
  1310. pci_clear_master(nic_data->pci_dev2);
  1311. efx_farch_irq_disable_master(efx);
  1312. /* Count errors and reset or disable the NIC accordingly */
  1313. if (efx->int_error_count == 0 ||
  1314. time_after(jiffies, efx->int_error_expire)) {
  1315. efx->int_error_count = 0;
  1316. efx->int_error_expire =
  1317. jiffies + EFX_INT_ERROR_EXPIRE * HZ;
  1318. }
  1319. if (++efx->int_error_count < EFX_MAX_INT_ERRORS) {
  1320. netif_err(efx, hw, efx->net_dev,
  1321. "SYSTEM ERROR - reset scheduled\n");
  1322. efx_schedule_reset(efx, RESET_TYPE_INT_ERROR);
  1323. } else {
  1324. netif_err(efx, hw, efx->net_dev,
  1325. "SYSTEM ERROR - max number of errors seen."
  1326. "NIC will be disabled\n");
  1327. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  1328. }
  1329. return IRQ_HANDLED;
  1330. }
  1331. /* Handle a legacy interrupt
  1332. * Acknowledges the interrupt and schedule event queue processing.
  1333. */
  1334. irqreturn_t efx_farch_legacy_interrupt(int irq, void *dev_id)
  1335. {
  1336. struct efx_nic *efx = dev_id;
  1337. bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
  1338. efx_oword_t *int_ker = efx->irq_status.addr;
  1339. irqreturn_t result = IRQ_NONE;
  1340. struct efx_channel *channel;
  1341. efx_dword_t reg;
  1342. u32 queues;
  1343. int syserr;
  1344. /* Read the ISR which also ACKs the interrupts */
  1345. efx_readd(efx, &reg, FR_BZ_INT_ISR0);
  1346. queues = EFX_EXTRACT_DWORD(reg, 0, 31);
  1347. /* Legacy interrupts are disabled too late by the EEH kernel
  1348. * code. Disable them earlier.
  1349. * If an EEH error occurred, the read will have returned all ones.
  1350. */
  1351. if (EFX_DWORD_IS_ALL_ONES(reg) && efx_try_recovery(efx) &&
  1352. !efx->eeh_disabled_legacy_irq) {
  1353. disable_irq_nosync(efx->legacy_irq);
  1354. efx->eeh_disabled_legacy_irq = true;
  1355. }
  1356. /* Handle non-event-queue sources */
  1357. if (queues & (1U << efx->irq_level) && soft_enabled) {
  1358. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1359. if (unlikely(syserr))
  1360. return efx_farch_fatal_interrupt(efx);
  1361. efx->last_irq_cpu = raw_smp_processor_id();
  1362. }
  1363. if (queues != 0) {
  1364. efx->irq_zero_count = 0;
  1365. /* Schedule processing of any interrupting queues */
  1366. if (likely(soft_enabled)) {
  1367. efx_for_each_channel(channel, efx) {
  1368. if (queues & 1)
  1369. efx_schedule_channel_irq(channel);
  1370. queues >>= 1;
  1371. }
  1372. }
  1373. result = IRQ_HANDLED;
  1374. } else {
  1375. efx_qword_t *event;
  1376. /* Legacy ISR read can return zero once (SF bug 15783) */
  1377. /* We can't return IRQ_HANDLED more than once on seeing ISR=0
  1378. * because this might be a shared interrupt. */
  1379. if (efx->irq_zero_count++ == 0)
  1380. result = IRQ_HANDLED;
  1381. /* Ensure we schedule or rearm all event queues */
  1382. if (likely(soft_enabled)) {
  1383. efx_for_each_channel(channel, efx) {
  1384. event = efx_event(channel,
  1385. channel->eventq_read_ptr);
  1386. if (efx_event_present(event))
  1387. efx_schedule_channel_irq(channel);
  1388. else
  1389. efx_farch_ev_read_ack(channel);
  1390. }
  1391. }
  1392. }
  1393. if (result == IRQ_HANDLED)
  1394. netif_vdbg(efx, intr, efx->net_dev,
  1395. "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  1396. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  1397. return result;
  1398. }
  1399. /* Handle an MSI interrupt
  1400. *
  1401. * Handle an MSI hardware interrupt. This routine schedules event
  1402. * queue processing. No interrupt acknowledgement cycle is necessary.
  1403. * Also, we never need to check that the interrupt is for us, since
  1404. * MSI interrupts cannot be shared.
  1405. */
  1406. irqreturn_t efx_farch_msi_interrupt(int irq, void *dev_id)
  1407. {
  1408. struct efx_msi_context *context = dev_id;
  1409. struct efx_nic *efx = context->efx;
  1410. efx_oword_t *int_ker = efx->irq_status.addr;
  1411. int syserr;
  1412. netif_vdbg(efx, intr, efx->net_dev,
  1413. "IRQ %d on CPU %d status " EFX_OWORD_FMT "\n",
  1414. irq, raw_smp_processor_id(), EFX_OWORD_VAL(*int_ker));
  1415. if (!likely(ACCESS_ONCE(efx->irq_soft_enabled)))
  1416. return IRQ_HANDLED;
  1417. /* Handle non-event-queue sources */
  1418. if (context->index == efx->irq_level) {
  1419. syserr = EFX_OWORD_FIELD(*int_ker, FSF_AZ_NET_IVEC_FATAL_INT);
  1420. if (unlikely(syserr))
  1421. return efx_farch_fatal_interrupt(efx);
  1422. efx->last_irq_cpu = raw_smp_processor_id();
  1423. }
  1424. /* Schedule processing of the channel */
  1425. efx_schedule_channel_irq(efx->channel[context->index]);
  1426. return IRQ_HANDLED;
  1427. }
  1428. /* Setup RSS indirection table.
  1429. * This maps from the hash value of the packet to RXQ
  1430. */
  1431. void efx_farch_rx_push_indir_table(struct efx_nic *efx)
  1432. {
  1433. size_t i = 0;
  1434. efx_dword_t dword;
  1435. BUG_ON(efx_nic_rev(efx) < EFX_REV_FALCON_B0);
  1436. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1437. FR_BZ_RX_INDIRECTION_TBL_ROWS);
  1438. for (i = 0; i < FR_BZ_RX_INDIRECTION_TBL_ROWS; i++) {
  1439. EFX_POPULATE_DWORD_1(dword, FRF_BZ_IT_QUEUE,
  1440. efx->rx_indir_table[i]);
  1441. efx_writed(efx, &dword,
  1442. FR_BZ_RX_INDIRECTION_TBL +
  1443. FR_BZ_RX_INDIRECTION_TBL_STEP * i);
  1444. }
  1445. }
  1446. /* Looks at available SRAM resources and works out how many queues we
  1447. * can support, and where things like descriptor caches should live.
  1448. *
  1449. * SRAM is split up as follows:
  1450. * 0 buftbl entries for channels
  1451. * efx->vf_buftbl_base buftbl entries for SR-IOV
  1452. * efx->rx_dc_base RX descriptor caches
  1453. * efx->tx_dc_base TX descriptor caches
  1454. */
  1455. void efx_farch_dimension_resources(struct efx_nic *efx, unsigned sram_lim_qw)
  1456. {
  1457. unsigned vi_count, buftbl_min;
  1458. #ifdef CONFIG_SFC_SRIOV
  1459. struct siena_nic_data *nic_data = efx->nic_data;
  1460. #endif
  1461. /* Account for the buffer table entries backing the datapath channels
  1462. * and the descriptor caches for those channels.
  1463. */
  1464. buftbl_min = ((efx->n_rx_channels * EFX_MAX_DMAQ_SIZE +
  1465. efx->n_tx_channels * EFX_TXQ_TYPES * EFX_MAX_DMAQ_SIZE +
  1466. efx->n_channels * EFX_MAX_EVQ_SIZE)
  1467. * sizeof(efx_qword_t) / EFX_BUF_SIZE);
  1468. vi_count = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
  1469. #ifdef CONFIG_SFC_SRIOV
  1470. if (efx->type->sriov_wanted) {
  1471. if (efx->type->sriov_wanted(efx)) {
  1472. unsigned vi_dc_entries, buftbl_free;
  1473. unsigned entries_per_vf, vf_limit;
  1474. nic_data->vf_buftbl_base = buftbl_min;
  1475. vi_dc_entries = RX_DC_ENTRIES + TX_DC_ENTRIES;
  1476. vi_count = max(vi_count, EFX_VI_BASE);
  1477. buftbl_free = (sram_lim_qw - buftbl_min -
  1478. vi_count * vi_dc_entries);
  1479. entries_per_vf = ((vi_dc_entries +
  1480. EFX_VF_BUFTBL_PER_VI) *
  1481. efx_vf_size(efx));
  1482. vf_limit = min(buftbl_free / entries_per_vf,
  1483. (1024U - EFX_VI_BASE) >> efx->vi_scale);
  1484. if (efx->vf_count > vf_limit) {
  1485. netif_err(efx, probe, efx->net_dev,
  1486. "Reducing VF count from from %d to %d\n",
  1487. efx->vf_count, vf_limit);
  1488. efx->vf_count = vf_limit;
  1489. }
  1490. vi_count += efx->vf_count * efx_vf_size(efx);
  1491. }
  1492. }
  1493. #endif
  1494. efx->tx_dc_base = sram_lim_qw - vi_count * TX_DC_ENTRIES;
  1495. efx->rx_dc_base = efx->tx_dc_base - vi_count * RX_DC_ENTRIES;
  1496. }
  1497. u32 efx_farch_fpga_ver(struct efx_nic *efx)
  1498. {
  1499. efx_oword_t altera_build;
  1500. efx_reado(efx, &altera_build, FR_AZ_ALTERA_BUILD);
  1501. return EFX_OWORD_FIELD(altera_build, FRF_AZ_ALTERA_BUILD_VER);
  1502. }
  1503. void efx_farch_init_common(struct efx_nic *efx)
  1504. {
  1505. efx_oword_t temp;
  1506. /* Set positions of descriptor caches in SRAM. */
  1507. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_TX_DC_BASE_ADR, efx->tx_dc_base);
  1508. efx_writeo(efx, &temp, FR_AZ_SRM_TX_DC_CFG);
  1509. EFX_POPULATE_OWORD_1(temp, FRF_AZ_SRM_RX_DC_BASE_ADR, efx->rx_dc_base);
  1510. efx_writeo(efx, &temp, FR_AZ_SRM_RX_DC_CFG);
  1511. /* Set TX descriptor cache size. */
  1512. BUILD_BUG_ON(TX_DC_ENTRIES != (8 << TX_DC_ENTRIES_ORDER));
  1513. EFX_POPULATE_OWORD_1(temp, FRF_AZ_TX_DC_SIZE, TX_DC_ENTRIES_ORDER);
  1514. efx_writeo(efx, &temp, FR_AZ_TX_DC_CFG);
  1515. /* Set RX descriptor cache size. Set low watermark to size-8, as
  1516. * this allows most efficient prefetching.
  1517. */
  1518. BUILD_BUG_ON(RX_DC_ENTRIES != (8 << RX_DC_ENTRIES_ORDER));
  1519. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_SIZE, RX_DC_ENTRIES_ORDER);
  1520. efx_writeo(efx, &temp, FR_AZ_RX_DC_CFG);
  1521. EFX_POPULATE_OWORD_1(temp, FRF_AZ_RX_DC_PF_LWM, RX_DC_ENTRIES - 8);
  1522. efx_writeo(efx, &temp, FR_AZ_RX_DC_PF_WM);
  1523. /* Program INT_KER address */
  1524. EFX_POPULATE_OWORD_2(temp,
  1525. FRF_AZ_NORM_INT_VEC_DIS_KER,
  1526. EFX_INT_MODE_USE_MSI(efx),
  1527. FRF_AZ_INT_ADR_KER, efx->irq_status.dma_addr);
  1528. efx_writeo(efx, &temp, FR_AZ_INT_ADR_KER);
  1529. if (EFX_WORKAROUND_17213(efx) && !EFX_INT_MODE_USE_MSI(efx))
  1530. /* Use an interrupt level unused by event queues */
  1531. efx->irq_level = 0x1f;
  1532. else
  1533. /* Use a valid MSI-X vector */
  1534. efx->irq_level = 0;
  1535. /* Enable all the genuinely fatal interrupts. (They are still
  1536. * masked by the overall interrupt mask, controlled by
  1537. * falcon_interrupts()).
  1538. *
  1539. * Note: All other fatal interrupts are enabled
  1540. */
  1541. EFX_POPULATE_OWORD_3(temp,
  1542. FRF_AZ_ILL_ADR_INT_KER_EN, 1,
  1543. FRF_AZ_RBUF_OWN_INT_KER_EN, 1,
  1544. FRF_AZ_TBUF_OWN_INT_KER_EN, 1);
  1545. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
  1546. EFX_SET_OWORD_FIELD(temp, FRF_CZ_SRAM_PERR_INT_P_KER_EN, 1);
  1547. EFX_INVERT_OWORD(temp);
  1548. efx_writeo(efx, &temp, FR_AZ_FATAL_INTR_KER);
  1549. /* Disable the ugly timer-based TX DMA backoff and allow TX DMA to be
  1550. * controlled by the RX FIFO fill level. Set arbitration to one pkt/Q.
  1551. */
  1552. efx_reado(efx, &temp, FR_AZ_TX_RESERVED);
  1553. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER, 0xfe);
  1554. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_RX_SPACER_EN, 1);
  1555. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_ONE_PKT_PER_Q, 1);
  1556. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PUSH_EN, 1);
  1557. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_DIS_NON_IP_EV, 1);
  1558. /* Enable SW_EV to inherit in char driver - assume harmless here */
  1559. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_SOFT_EVT_EN, 1);
  1560. /* Prefetch threshold 2 => fetch when descriptor cache half empty */
  1561. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_THRESHOLD, 2);
  1562. /* Disable hardware watchdog which can misfire */
  1563. EFX_SET_OWORD_FIELD(temp, FRF_AZ_TX_PREF_WD_TMR, 0x3fffff);
  1564. /* Squash TX of packets of 16 bytes or less */
  1565. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0)
  1566. EFX_SET_OWORD_FIELD(temp, FRF_BZ_TX_FLUSH_MIN_LEN_EN, 1);
  1567. efx_writeo(efx, &temp, FR_AZ_TX_RESERVED);
  1568. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  1569. EFX_POPULATE_OWORD_4(temp,
  1570. /* Default values */
  1571. FRF_BZ_TX_PACE_SB_NOT_AF, 0x15,
  1572. FRF_BZ_TX_PACE_SB_AF, 0xb,
  1573. FRF_BZ_TX_PACE_FB_BASE, 0,
  1574. /* Allow large pace values in the
  1575. * fast bin. */
  1576. FRF_BZ_TX_PACE_BIN_TH,
  1577. FFE_BZ_TX_PACE_RESERVED);
  1578. efx_writeo(efx, &temp, FR_BZ_TX_PACE);
  1579. }
  1580. }
  1581. /**************************************************************************
  1582. *
  1583. * Filter tables
  1584. *
  1585. **************************************************************************
  1586. */
  1587. /* "Fudge factors" - difference between programmed value and actual depth.
  1588. * Due to pipelined implementation we need to program H/W with a value that
  1589. * is larger than the hop limit we want.
  1590. */
  1591. #define EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD 3
  1592. #define EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL 1
  1593. /* Hard maximum search limit. Hardware will time-out beyond 200-something.
  1594. * We also need to avoid infinite loops in efx_farch_filter_search() when the
  1595. * table is full.
  1596. */
  1597. #define EFX_FARCH_FILTER_CTL_SRCH_MAX 200
  1598. /* Don't try very hard to find space for performance hints, as this is
  1599. * counter-productive. */
  1600. #define EFX_FARCH_FILTER_CTL_SRCH_HINT_MAX 5
  1601. enum efx_farch_filter_type {
  1602. EFX_FARCH_FILTER_TCP_FULL = 0,
  1603. EFX_FARCH_FILTER_TCP_WILD,
  1604. EFX_FARCH_FILTER_UDP_FULL,
  1605. EFX_FARCH_FILTER_UDP_WILD,
  1606. EFX_FARCH_FILTER_MAC_FULL = 4,
  1607. EFX_FARCH_FILTER_MAC_WILD,
  1608. EFX_FARCH_FILTER_UC_DEF = 8,
  1609. EFX_FARCH_FILTER_MC_DEF,
  1610. EFX_FARCH_FILTER_TYPE_COUNT, /* number of specific types */
  1611. };
  1612. enum efx_farch_filter_table_id {
  1613. EFX_FARCH_FILTER_TABLE_RX_IP = 0,
  1614. EFX_FARCH_FILTER_TABLE_RX_MAC,
  1615. EFX_FARCH_FILTER_TABLE_RX_DEF,
  1616. EFX_FARCH_FILTER_TABLE_TX_MAC,
  1617. EFX_FARCH_FILTER_TABLE_COUNT,
  1618. };
  1619. enum efx_farch_filter_index {
  1620. EFX_FARCH_FILTER_INDEX_UC_DEF,
  1621. EFX_FARCH_FILTER_INDEX_MC_DEF,
  1622. EFX_FARCH_FILTER_SIZE_RX_DEF,
  1623. };
  1624. struct efx_farch_filter_spec {
  1625. u8 type:4;
  1626. u8 priority:4;
  1627. u8 flags;
  1628. u16 dmaq_id;
  1629. u32 data[3];
  1630. };
  1631. struct efx_farch_filter_table {
  1632. enum efx_farch_filter_table_id id;
  1633. u32 offset; /* address of table relative to BAR */
  1634. unsigned size; /* number of entries */
  1635. unsigned step; /* step between entries */
  1636. unsigned used; /* number currently used */
  1637. unsigned long *used_bitmap;
  1638. struct efx_farch_filter_spec *spec;
  1639. unsigned search_limit[EFX_FARCH_FILTER_TYPE_COUNT];
  1640. };
  1641. struct efx_farch_filter_state {
  1642. struct efx_farch_filter_table table[EFX_FARCH_FILTER_TABLE_COUNT];
  1643. };
  1644. static void
  1645. efx_farch_filter_table_clear_entry(struct efx_nic *efx,
  1646. struct efx_farch_filter_table *table,
  1647. unsigned int filter_idx);
  1648. /* The filter hash function is LFSR polynomial x^16 + x^3 + 1 of a 32-bit
  1649. * key derived from the n-tuple. The initial LFSR state is 0xffff. */
  1650. static u16 efx_farch_filter_hash(u32 key)
  1651. {
  1652. u16 tmp;
  1653. /* First 16 rounds */
  1654. tmp = 0x1fff ^ key >> 16;
  1655. tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
  1656. tmp = tmp ^ tmp >> 9;
  1657. /* Last 16 rounds */
  1658. tmp = tmp ^ tmp << 13 ^ key;
  1659. tmp = tmp ^ tmp >> 3 ^ tmp >> 6;
  1660. return tmp ^ tmp >> 9;
  1661. }
  1662. /* To allow for hash collisions, filter search continues at these
  1663. * increments from the first possible entry selected by the hash. */
  1664. static u16 efx_farch_filter_increment(u32 key)
  1665. {
  1666. return key * 2 - 1;
  1667. }
  1668. static enum efx_farch_filter_table_id
  1669. efx_farch_filter_spec_table_id(const struct efx_farch_filter_spec *spec)
  1670. {
  1671. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1672. (EFX_FARCH_FILTER_TCP_FULL >> 2));
  1673. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1674. (EFX_FARCH_FILTER_TCP_WILD >> 2));
  1675. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1676. (EFX_FARCH_FILTER_UDP_FULL >> 2));
  1677. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_IP !=
  1678. (EFX_FARCH_FILTER_UDP_WILD >> 2));
  1679. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_MAC !=
  1680. (EFX_FARCH_FILTER_MAC_FULL >> 2));
  1681. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_RX_MAC !=
  1682. (EFX_FARCH_FILTER_MAC_WILD >> 2));
  1683. BUILD_BUG_ON(EFX_FARCH_FILTER_TABLE_TX_MAC !=
  1684. EFX_FARCH_FILTER_TABLE_RX_MAC + 2);
  1685. return (spec->type >> 2) + ((spec->flags & EFX_FILTER_FLAG_TX) ? 2 : 0);
  1686. }
  1687. static void efx_farch_filter_push_rx_config(struct efx_nic *efx)
  1688. {
  1689. struct efx_farch_filter_state *state = efx->filter_state;
  1690. struct efx_farch_filter_table *table;
  1691. efx_oword_t filter_ctl;
  1692. efx_reado(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
  1693. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  1694. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_FULL_SRCH_LIMIT,
  1695. table->search_limit[EFX_FARCH_FILTER_TCP_FULL] +
  1696. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1697. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_TCP_WILD_SRCH_LIMIT,
  1698. table->search_limit[EFX_FARCH_FILTER_TCP_WILD] +
  1699. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1700. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_FULL_SRCH_LIMIT,
  1701. table->search_limit[EFX_FARCH_FILTER_UDP_FULL] +
  1702. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1703. EFX_SET_OWORD_FIELD(filter_ctl, FRF_BZ_UDP_WILD_SRCH_LIMIT,
  1704. table->search_limit[EFX_FARCH_FILTER_UDP_WILD] +
  1705. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1706. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_MAC];
  1707. if (table->size) {
  1708. EFX_SET_OWORD_FIELD(
  1709. filter_ctl, FRF_CZ_ETHERNET_FULL_SEARCH_LIMIT,
  1710. table->search_limit[EFX_FARCH_FILTER_MAC_FULL] +
  1711. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1712. EFX_SET_OWORD_FIELD(
  1713. filter_ctl, FRF_CZ_ETHERNET_WILDCARD_SEARCH_LIMIT,
  1714. table->search_limit[EFX_FARCH_FILTER_MAC_WILD] +
  1715. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1716. }
  1717. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  1718. if (table->size) {
  1719. EFX_SET_OWORD_FIELD(
  1720. filter_ctl, FRF_CZ_UNICAST_NOMATCH_Q_ID,
  1721. table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].dmaq_id);
  1722. EFX_SET_OWORD_FIELD(
  1723. filter_ctl, FRF_CZ_UNICAST_NOMATCH_RSS_ENABLED,
  1724. !!(table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].flags &
  1725. EFX_FILTER_FLAG_RX_RSS));
  1726. EFX_SET_OWORD_FIELD(
  1727. filter_ctl, FRF_CZ_MULTICAST_NOMATCH_Q_ID,
  1728. table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].dmaq_id);
  1729. EFX_SET_OWORD_FIELD(
  1730. filter_ctl, FRF_CZ_MULTICAST_NOMATCH_RSS_ENABLED,
  1731. !!(table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].flags &
  1732. EFX_FILTER_FLAG_RX_RSS));
  1733. /* There is a single bit to enable RX scatter for all
  1734. * unmatched packets. Only set it if scatter is
  1735. * enabled in both filter specs.
  1736. */
  1737. EFX_SET_OWORD_FIELD(
  1738. filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
  1739. !!(table->spec[EFX_FARCH_FILTER_INDEX_UC_DEF].flags &
  1740. table->spec[EFX_FARCH_FILTER_INDEX_MC_DEF].flags &
  1741. EFX_FILTER_FLAG_RX_SCATTER));
  1742. } else if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  1743. /* We don't expose 'default' filters because unmatched
  1744. * packets always go to the queue number found in the
  1745. * RSS table. But we still need to set the RX scatter
  1746. * bit here.
  1747. */
  1748. EFX_SET_OWORD_FIELD(
  1749. filter_ctl, FRF_BZ_SCATTER_ENBL_NO_MATCH_Q,
  1750. efx->rx_scatter);
  1751. }
  1752. efx_writeo(efx, &filter_ctl, FR_BZ_RX_FILTER_CTL);
  1753. }
  1754. static void efx_farch_filter_push_tx_limits(struct efx_nic *efx)
  1755. {
  1756. struct efx_farch_filter_state *state = efx->filter_state;
  1757. struct efx_farch_filter_table *table;
  1758. efx_oword_t tx_cfg;
  1759. efx_reado(efx, &tx_cfg, FR_AZ_TX_CFG);
  1760. table = &state->table[EFX_FARCH_FILTER_TABLE_TX_MAC];
  1761. if (table->size) {
  1762. EFX_SET_OWORD_FIELD(
  1763. tx_cfg, FRF_CZ_TX_ETH_FILTER_FULL_SEARCH_RANGE,
  1764. table->search_limit[EFX_FARCH_FILTER_MAC_FULL] +
  1765. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_FULL);
  1766. EFX_SET_OWORD_FIELD(
  1767. tx_cfg, FRF_CZ_TX_ETH_FILTER_WILD_SEARCH_RANGE,
  1768. table->search_limit[EFX_FARCH_FILTER_MAC_WILD] +
  1769. EFX_FARCH_FILTER_CTL_SRCH_FUDGE_WILD);
  1770. }
  1771. efx_writeo(efx, &tx_cfg, FR_AZ_TX_CFG);
  1772. }
  1773. static int
  1774. efx_farch_filter_from_gen_spec(struct efx_farch_filter_spec *spec,
  1775. const struct efx_filter_spec *gen_spec)
  1776. {
  1777. bool is_full = false;
  1778. if ((gen_spec->flags & EFX_FILTER_FLAG_RX_RSS) &&
  1779. gen_spec->rss_context != EFX_FILTER_RSS_CONTEXT_DEFAULT)
  1780. return -EINVAL;
  1781. spec->priority = gen_spec->priority;
  1782. spec->flags = gen_spec->flags;
  1783. spec->dmaq_id = gen_spec->dmaq_id;
  1784. switch (gen_spec->match_flags) {
  1785. case (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  1786. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
  1787. EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT):
  1788. is_full = true;
  1789. /* fall through */
  1790. case (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
  1791. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT): {
  1792. __be32 rhost, host1, host2;
  1793. __be16 rport, port1, port2;
  1794. EFX_BUG_ON_PARANOID(!(gen_spec->flags & EFX_FILTER_FLAG_RX));
  1795. if (gen_spec->ether_type != htons(ETH_P_IP))
  1796. return -EPROTONOSUPPORT;
  1797. if (gen_spec->loc_port == 0 ||
  1798. (is_full && gen_spec->rem_port == 0))
  1799. return -EADDRNOTAVAIL;
  1800. switch (gen_spec->ip_proto) {
  1801. case IPPROTO_TCP:
  1802. spec->type = (is_full ? EFX_FARCH_FILTER_TCP_FULL :
  1803. EFX_FARCH_FILTER_TCP_WILD);
  1804. break;
  1805. case IPPROTO_UDP:
  1806. spec->type = (is_full ? EFX_FARCH_FILTER_UDP_FULL :
  1807. EFX_FARCH_FILTER_UDP_WILD);
  1808. break;
  1809. default:
  1810. return -EPROTONOSUPPORT;
  1811. }
  1812. /* Filter is constructed in terms of source and destination,
  1813. * with the odd wrinkle that the ports are swapped in a UDP
  1814. * wildcard filter. We need to convert from local and remote
  1815. * (= zero for wildcard) addresses.
  1816. */
  1817. rhost = is_full ? gen_spec->rem_host[0] : 0;
  1818. rport = is_full ? gen_spec->rem_port : 0;
  1819. host1 = rhost;
  1820. host2 = gen_spec->loc_host[0];
  1821. if (!is_full && gen_spec->ip_proto == IPPROTO_UDP) {
  1822. port1 = gen_spec->loc_port;
  1823. port2 = rport;
  1824. } else {
  1825. port1 = rport;
  1826. port2 = gen_spec->loc_port;
  1827. }
  1828. spec->data[0] = ntohl(host1) << 16 | ntohs(port1);
  1829. spec->data[1] = ntohs(port2) << 16 | ntohl(host1) >> 16;
  1830. spec->data[2] = ntohl(host2);
  1831. break;
  1832. }
  1833. case EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_OUTER_VID:
  1834. is_full = true;
  1835. /* fall through */
  1836. case EFX_FILTER_MATCH_LOC_MAC:
  1837. spec->type = (is_full ? EFX_FARCH_FILTER_MAC_FULL :
  1838. EFX_FARCH_FILTER_MAC_WILD);
  1839. spec->data[0] = is_full ? ntohs(gen_spec->outer_vid) : 0;
  1840. spec->data[1] = (gen_spec->loc_mac[2] << 24 |
  1841. gen_spec->loc_mac[3] << 16 |
  1842. gen_spec->loc_mac[4] << 8 |
  1843. gen_spec->loc_mac[5]);
  1844. spec->data[2] = (gen_spec->loc_mac[0] << 8 |
  1845. gen_spec->loc_mac[1]);
  1846. break;
  1847. case EFX_FILTER_MATCH_LOC_MAC_IG:
  1848. spec->type = (is_multicast_ether_addr(gen_spec->loc_mac) ?
  1849. EFX_FARCH_FILTER_MC_DEF :
  1850. EFX_FARCH_FILTER_UC_DEF);
  1851. memset(spec->data, 0, sizeof(spec->data)); /* ensure equality */
  1852. break;
  1853. default:
  1854. return -EPROTONOSUPPORT;
  1855. }
  1856. return 0;
  1857. }
  1858. static void
  1859. efx_farch_filter_to_gen_spec(struct efx_filter_spec *gen_spec,
  1860. const struct efx_farch_filter_spec *spec)
  1861. {
  1862. bool is_full = false;
  1863. /* *gen_spec should be completely initialised, to be consistent
  1864. * with efx_filter_init_{rx,tx}() and in case we want to copy
  1865. * it back to userland.
  1866. */
  1867. memset(gen_spec, 0, sizeof(*gen_spec));
  1868. gen_spec->priority = spec->priority;
  1869. gen_spec->flags = spec->flags;
  1870. gen_spec->dmaq_id = spec->dmaq_id;
  1871. switch (spec->type) {
  1872. case EFX_FARCH_FILTER_TCP_FULL:
  1873. case EFX_FARCH_FILTER_UDP_FULL:
  1874. is_full = true;
  1875. /* fall through */
  1876. case EFX_FARCH_FILTER_TCP_WILD:
  1877. case EFX_FARCH_FILTER_UDP_WILD: {
  1878. __be32 host1, host2;
  1879. __be16 port1, port2;
  1880. gen_spec->match_flags =
  1881. EFX_FILTER_MATCH_ETHER_TYPE |
  1882. EFX_FILTER_MATCH_IP_PROTO |
  1883. EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT;
  1884. if (is_full)
  1885. gen_spec->match_flags |= (EFX_FILTER_MATCH_REM_HOST |
  1886. EFX_FILTER_MATCH_REM_PORT);
  1887. gen_spec->ether_type = htons(ETH_P_IP);
  1888. gen_spec->ip_proto =
  1889. (spec->type == EFX_FARCH_FILTER_TCP_FULL ||
  1890. spec->type == EFX_FARCH_FILTER_TCP_WILD) ?
  1891. IPPROTO_TCP : IPPROTO_UDP;
  1892. host1 = htonl(spec->data[0] >> 16 | spec->data[1] << 16);
  1893. port1 = htons(spec->data[0]);
  1894. host2 = htonl(spec->data[2]);
  1895. port2 = htons(spec->data[1] >> 16);
  1896. if (spec->flags & EFX_FILTER_FLAG_TX) {
  1897. gen_spec->loc_host[0] = host1;
  1898. gen_spec->rem_host[0] = host2;
  1899. } else {
  1900. gen_spec->loc_host[0] = host2;
  1901. gen_spec->rem_host[0] = host1;
  1902. }
  1903. if (!!(gen_spec->flags & EFX_FILTER_FLAG_TX) ^
  1904. (!is_full && gen_spec->ip_proto == IPPROTO_UDP)) {
  1905. gen_spec->loc_port = port1;
  1906. gen_spec->rem_port = port2;
  1907. } else {
  1908. gen_spec->loc_port = port2;
  1909. gen_spec->rem_port = port1;
  1910. }
  1911. break;
  1912. }
  1913. case EFX_FARCH_FILTER_MAC_FULL:
  1914. is_full = true;
  1915. /* fall through */
  1916. case EFX_FARCH_FILTER_MAC_WILD:
  1917. gen_spec->match_flags = EFX_FILTER_MATCH_LOC_MAC;
  1918. if (is_full)
  1919. gen_spec->match_flags |= EFX_FILTER_MATCH_OUTER_VID;
  1920. gen_spec->loc_mac[0] = spec->data[2] >> 8;
  1921. gen_spec->loc_mac[1] = spec->data[2];
  1922. gen_spec->loc_mac[2] = spec->data[1] >> 24;
  1923. gen_spec->loc_mac[3] = spec->data[1] >> 16;
  1924. gen_spec->loc_mac[4] = spec->data[1] >> 8;
  1925. gen_spec->loc_mac[5] = spec->data[1];
  1926. gen_spec->outer_vid = htons(spec->data[0]);
  1927. break;
  1928. case EFX_FARCH_FILTER_UC_DEF:
  1929. case EFX_FARCH_FILTER_MC_DEF:
  1930. gen_spec->match_flags = EFX_FILTER_MATCH_LOC_MAC_IG;
  1931. gen_spec->loc_mac[0] = spec->type == EFX_FARCH_FILTER_MC_DEF;
  1932. break;
  1933. default:
  1934. WARN_ON(1);
  1935. break;
  1936. }
  1937. }
  1938. static void
  1939. efx_farch_filter_init_rx_auto(struct efx_nic *efx,
  1940. struct efx_farch_filter_spec *spec)
  1941. {
  1942. /* If there's only one channel then disable RSS for non VF
  1943. * traffic, thereby allowing VFs to use RSS when the PF can't.
  1944. */
  1945. spec->priority = EFX_FILTER_PRI_AUTO;
  1946. spec->flags = (EFX_FILTER_FLAG_RX |
  1947. (efx_rss_enabled(efx) ? EFX_FILTER_FLAG_RX_RSS : 0) |
  1948. (efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0));
  1949. spec->dmaq_id = 0;
  1950. }
  1951. /* Build a filter entry and return its n-tuple key. */
  1952. static u32 efx_farch_filter_build(efx_oword_t *filter,
  1953. struct efx_farch_filter_spec *spec)
  1954. {
  1955. u32 data3;
  1956. switch (efx_farch_filter_spec_table_id(spec)) {
  1957. case EFX_FARCH_FILTER_TABLE_RX_IP: {
  1958. bool is_udp = (spec->type == EFX_FARCH_FILTER_UDP_FULL ||
  1959. spec->type == EFX_FARCH_FILTER_UDP_WILD);
  1960. EFX_POPULATE_OWORD_7(
  1961. *filter,
  1962. FRF_BZ_RSS_EN,
  1963. !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
  1964. FRF_BZ_SCATTER_EN,
  1965. !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
  1966. FRF_BZ_TCP_UDP, is_udp,
  1967. FRF_BZ_RXQ_ID, spec->dmaq_id,
  1968. EFX_DWORD_2, spec->data[2],
  1969. EFX_DWORD_1, spec->data[1],
  1970. EFX_DWORD_0, spec->data[0]);
  1971. data3 = is_udp;
  1972. break;
  1973. }
  1974. case EFX_FARCH_FILTER_TABLE_RX_MAC: {
  1975. bool is_wild = spec->type == EFX_FARCH_FILTER_MAC_WILD;
  1976. EFX_POPULATE_OWORD_7(
  1977. *filter,
  1978. FRF_CZ_RMFT_RSS_EN,
  1979. !!(spec->flags & EFX_FILTER_FLAG_RX_RSS),
  1980. FRF_CZ_RMFT_SCATTER_EN,
  1981. !!(spec->flags & EFX_FILTER_FLAG_RX_SCATTER),
  1982. FRF_CZ_RMFT_RXQ_ID, spec->dmaq_id,
  1983. FRF_CZ_RMFT_WILDCARD_MATCH, is_wild,
  1984. FRF_CZ_RMFT_DEST_MAC_HI, spec->data[2],
  1985. FRF_CZ_RMFT_DEST_MAC_LO, spec->data[1],
  1986. FRF_CZ_RMFT_VLAN_ID, spec->data[0]);
  1987. data3 = is_wild;
  1988. break;
  1989. }
  1990. case EFX_FARCH_FILTER_TABLE_TX_MAC: {
  1991. bool is_wild = spec->type == EFX_FARCH_FILTER_MAC_WILD;
  1992. EFX_POPULATE_OWORD_5(*filter,
  1993. FRF_CZ_TMFT_TXQ_ID, spec->dmaq_id,
  1994. FRF_CZ_TMFT_WILDCARD_MATCH, is_wild,
  1995. FRF_CZ_TMFT_SRC_MAC_HI, spec->data[2],
  1996. FRF_CZ_TMFT_SRC_MAC_LO, spec->data[1],
  1997. FRF_CZ_TMFT_VLAN_ID, spec->data[0]);
  1998. data3 = is_wild | spec->dmaq_id << 1;
  1999. break;
  2000. }
  2001. default:
  2002. BUG();
  2003. }
  2004. return spec->data[0] ^ spec->data[1] ^ spec->data[2] ^ data3;
  2005. }
  2006. static bool efx_farch_filter_equal(const struct efx_farch_filter_spec *left,
  2007. const struct efx_farch_filter_spec *right)
  2008. {
  2009. if (left->type != right->type ||
  2010. memcmp(left->data, right->data, sizeof(left->data)))
  2011. return false;
  2012. if (left->flags & EFX_FILTER_FLAG_TX &&
  2013. left->dmaq_id != right->dmaq_id)
  2014. return false;
  2015. return true;
  2016. }
  2017. /*
  2018. * Construct/deconstruct external filter IDs. At least the RX filter
  2019. * IDs must be ordered by matching priority, for RX NFC semantics.
  2020. *
  2021. * Deconstruction needs to be robust against invalid IDs so that
  2022. * efx_filter_remove_id_safe() and efx_filter_get_filter_safe() can
  2023. * accept user-provided IDs.
  2024. */
  2025. #define EFX_FARCH_FILTER_MATCH_PRI_COUNT 5
  2026. static const u8 efx_farch_filter_type_match_pri[EFX_FARCH_FILTER_TYPE_COUNT] = {
  2027. [EFX_FARCH_FILTER_TCP_FULL] = 0,
  2028. [EFX_FARCH_FILTER_UDP_FULL] = 0,
  2029. [EFX_FARCH_FILTER_TCP_WILD] = 1,
  2030. [EFX_FARCH_FILTER_UDP_WILD] = 1,
  2031. [EFX_FARCH_FILTER_MAC_FULL] = 2,
  2032. [EFX_FARCH_FILTER_MAC_WILD] = 3,
  2033. [EFX_FARCH_FILTER_UC_DEF] = 4,
  2034. [EFX_FARCH_FILTER_MC_DEF] = 4,
  2035. };
  2036. static const enum efx_farch_filter_table_id efx_farch_filter_range_table[] = {
  2037. EFX_FARCH_FILTER_TABLE_RX_IP, /* RX match pri 0 */
  2038. EFX_FARCH_FILTER_TABLE_RX_IP,
  2039. EFX_FARCH_FILTER_TABLE_RX_MAC,
  2040. EFX_FARCH_FILTER_TABLE_RX_MAC,
  2041. EFX_FARCH_FILTER_TABLE_RX_DEF, /* RX match pri 4 */
  2042. EFX_FARCH_FILTER_TABLE_TX_MAC, /* TX match pri 0 */
  2043. EFX_FARCH_FILTER_TABLE_TX_MAC, /* TX match pri 1 */
  2044. };
  2045. #define EFX_FARCH_FILTER_INDEX_WIDTH 13
  2046. #define EFX_FARCH_FILTER_INDEX_MASK ((1 << EFX_FARCH_FILTER_INDEX_WIDTH) - 1)
  2047. static inline u32
  2048. efx_farch_filter_make_id(const struct efx_farch_filter_spec *spec,
  2049. unsigned int index)
  2050. {
  2051. unsigned int range;
  2052. range = efx_farch_filter_type_match_pri[spec->type];
  2053. if (!(spec->flags & EFX_FILTER_FLAG_RX))
  2054. range += EFX_FARCH_FILTER_MATCH_PRI_COUNT;
  2055. return range << EFX_FARCH_FILTER_INDEX_WIDTH | index;
  2056. }
  2057. static inline enum efx_farch_filter_table_id
  2058. efx_farch_filter_id_table_id(u32 id)
  2059. {
  2060. unsigned int range = id >> EFX_FARCH_FILTER_INDEX_WIDTH;
  2061. if (range < ARRAY_SIZE(efx_farch_filter_range_table))
  2062. return efx_farch_filter_range_table[range];
  2063. else
  2064. return EFX_FARCH_FILTER_TABLE_COUNT; /* invalid */
  2065. }
  2066. static inline unsigned int efx_farch_filter_id_index(u32 id)
  2067. {
  2068. return id & EFX_FARCH_FILTER_INDEX_MASK;
  2069. }
  2070. u32 efx_farch_filter_get_rx_id_limit(struct efx_nic *efx)
  2071. {
  2072. struct efx_farch_filter_state *state = efx->filter_state;
  2073. unsigned int range = EFX_FARCH_FILTER_MATCH_PRI_COUNT - 1;
  2074. enum efx_farch_filter_table_id table_id;
  2075. do {
  2076. table_id = efx_farch_filter_range_table[range];
  2077. if (state->table[table_id].size != 0)
  2078. return range << EFX_FARCH_FILTER_INDEX_WIDTH |
  2079. state->table[table_id].size;
  2080. } while (range--);
  2081. return 0;
  2082. }
  2083. s32 efx_farch_filter_insert(struct efx_nic *efx,
  2084. struct efx_filter_spec *gen_spec,
  2085. bool replace_equal)
  2086. {
  2087. struct efx_farch_filter_state *state = efx->filter_state;
  2088. struct efx_farch_filter_table *table;
  2089. struct efx_farch_filter_spec spec;
  2090. efx_oword_t filter;
  2091. int rep_index, ins_index;
  2092. unsigned int depth = 0;
  2093. int rc;
  2094. rc = efx_farch_filter_from_gen_spec(&spec, gen_spec);
  2095. if (rc)
  2096. return rc;
  2097. table = &state->table[efx_farch_filter_spec_table_id(&spec)];
  2098. if (table->size == 0)
  2099. return -EINVAL;
  2100. netif_vdbg(efx, hw, efx->net_dev,
  2101. "%s: type %d search_limit=%d", __func__, spec.type,
  2102. table->search_limit[spec.type]);
  2103. if (table->id == EFX_FARCH_FILTER_TABLE_RX_DEF) {
  2104. /* One filter spec per type */
  2105. BUILD_BUG_ON(EFX_FARCH_FILTER_INDEX_UC_DEF != 0);
  2106. BUILD_BUG_ON(EFX_FARCH_FILTER_INDEX_MC_DEF !=
  2107. EFX_FARCH_FILTER_MC_DEF - EFX_FARCH_FILTER_UC_DEF);
  2108. rep_index = spec.type - EFX_FARCH_FILTER_UC_DEF;
  2109. ins_index = rep_index;
  2110. spin_lock_bh(&efx->filter_lock);
  2111. } else {
  2112. /* Search concurrently for
  2113. * (1) a filter to be replaced (rep_index): any filter
  2114. * with the same match values, up to the current
  2115. * search depth for this type, and
  2116. * (2) the insertion point (ins_index): (1) or any
  2117. * free slot before it or up to the maximum search
  2118. * depth for this priority
  2119. * We fail if we cannot find (2).
  2120. *
  2121. * We can stop once either
  2122. * (a) we find (1), in which case we have definitely
  2123. * found (2) as well; or
  2124. * (b) we have searched exhaustively for (1), and have
  2125. * either found (2) or searched exhaustively for it
  2126. */
  2127. u32 key = efx_farch_filter_build(&filter, &spec);
  2128. unsigned int hash = efx_farch_filter_hash(key);
  2129. unsigned int incr = efx_farch_filter_increment(key);
  2130. unsigned int max_rep_depth = table->search_limit[spec.type];
  2131. unsigned int max_ins_depth =
  2132. spec.priority <= EFX_FILTER_PRI_HINT ?
  2133. EFX_FARCH_FILTER_CTL_SRCH_HINT_MAX :
  2134. EFX_FARCH_FILTER_CTL_SRCH_MAX;
  2135. unsigned int i = hash & (table->size - 1);
  2136. ins_index = -1;
  2137. depth = 1;
  2138. spin_lock_bh(&efx->filter_lock);
  2139. for (;;) {
  2140. if (!test_bit(i, table->used_bitmap)) {
  2141. if (ins_index < 0)
  2142. ins_index = i;
  2143. } else if (efx_farch_filter_equal(&spec,
  2144. &table->spec[i])) {
  2145. /* Case (a) */
  2146. if (ins_index < 0)
  2147. ins_index = i;
  2148. rep_index = i;
  2149. break;
  2150. }
  2151. if (depth >= max_rep_depth &&
  2152. (ins_index >= 0 || depth >= max_ins_depth)) {
  2153. /* Case (b) */
  2154. if (ins_index < 0) {
  2155. rc = -EBUSY;
  2156. goto out;
  2157. }
  2158. rep_index = -1;
  2159. break;
  2160. }
  2161. i = (i + incr) & (table->size - 1);
  2162. ++depth;
  2163. }
  2164. }
  2165. /* If we found a filter to be replaced, check whether we
  2166. * should do so
  2167. */
  2168. if (rep_index >= 0) {
  2169. struct efx_farch_filter_spec *saved_spec =
  2170. &table->spec[rep_index];
  2171. if (spec.priority == saved_spec->priority && !replace_equal) {
  2172. rc = -EEXIST;
  2173. goto out;
  2174. }
  2175. if (spec.priority < saved_spec->priority) {
  2176. rc = -EPERM;
  2177. goto out;
  2178. }
  2179. if (saved_spec->priority == EFX_FILTER_PRI_AUTO ||
  2180. saved_spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO)
  2181. spec.flags |= EFX_FILTER_FLAG_RX_OVER_AUTO;
  2182. }
  2183. /* Insert the filter */
  2184. if (ins_index != rep_index) {
  2185. __set_bit(ins_index, table->used_bitmap);
  2186. ++table->used;
  2187. }
  2188. table->spec[ins_index] = spec;
  2189. if (table->id == EFX_FARCH_FILTER_TABLE_RX_DEF) {
  2190. efx_farch_filter_push_rx_config(efx);
  2191. } else {
  2192. if (table->search_limit[spec.type] < depth) {
  2193. table->search_limit[spec.type] = depth;
  2194. if (spec.flags & EFX_FILTER_FLAG_TX)
  2195. efx_farch_filter_push_tx_limits(efx);
  2196. else
  2197. efx_farch_filter_push_rx_config(efx);
  2198. }
  2199. efx_writeo(efx, &filter,
  2200. table->offset + table->step * ins_index);
  2201. /* If we were able to replace a filter by inserting
  2202. * at a lower depth, clear the replaced filter
  2203. */
  2204. if (ins_index != rep_index && rep_index >= 0)
  2205. efx_farch_filter_table_clear_entry(efx, table,
  2206. rep_index);
  2207. }
  2208. netif_vdbg(efx, hw, efx->net_dev,
  2209. "%s: filter type %d index %d rxq %u set",
  2210. __func__, spec.type, ins_index, spec.dmaq_id);
  2211. rc = efx_farch_filter_make_id(&spec, ins_index);
  2212. out:
  2213. spin_unlock_bh(&efx->filter_lock);
  2214. return rc;
  2215. }
  2216. static void
  2217. efx_farch_filter_table_clear_entry(struct efx_nic *efx,
  2218. struct efx_farch_filter_table *table,
  2219. unsigned int filter_idx)
  2220. {
  2221. static efx_oword_t filter;
  2222. EFX_WARN_ON_PARANOID(!test_bit(filter_idx, table->used_bitmap));
  2223. BUG_ON(table->offset == 0); /* can't clear MAC default filters */
  2224. __clear_bit(filter_idx, table->used_bitmap);
  2225. --table->used;
  2226. memset(&table->spec[filter_idx], 0, sizeof(table->spec[0]));
  2227. efx_writeo(efx, &filter, table->offset + table->step * filter_idx);
  2228. /* If this filter required a greater search depth than
  2229. * any other, the search limit for its type can now be
  2230. * decreased. However, it is hard to determine that
  2231. * unless the table has become completely empty - in
  2232. * which case, all its search limits can be set to 0.
  2233. */
  2234. if (unlikely(table->used == 0)) {
  2235. memset(table->search_limit, 0, sizeof(table->search_limit));
  2236. if (table->id == EFX_FARCH_FILTER_TABLE_TX_MAC)
  2237. efx_farch_filter_push_tx_limits(efx);
  2238. else
  2239. efx_farch_filter_push_rx_config(efx);
  2240. }
  2241. }
  2242. static int efx_farch_filter_remove(struct efx_nic *efx,
  2243. struct efx_farch_filter_table *table,
  2244. unsigned int filter_idx,
  2245. enum efx_filter_priority priority)
  2246. {
  2247. struct efx_farch_filter_spec *spec = &table->spec[filter_idx];
  2248. if (!test_bit(filter_idx, table->used_bitmap) ||
  2249. spec->priority != priority)
  2250. return -ENOENT;
  2251. if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO) {
  2252. efx_farch_filter_init_rx_auto(efx, spec);
  2253. efx_farch_filter_push_rx_config(efx);
  2254. } else {
  2255. efx_farch_filter_table_clear_entry(efx, table, filter_idx);
  2256. }
  2257. return 0;
  2258. }
  2259. int efx_farch_filter_remove_safe(struct efx_nic *efx,
  2260. enum efx_filter_priority priority,
  2261. u32 filter_id)
  2262. {
  2263. struct efx_farch_filter_state *state = efx->filter_state;
  2264. enum efx_farch_filter_table_id table_id;
  2265. struct efx_farch_filter_table *table;
  2266. unsigned int filter_idx;
  2267. struct efx_farch_filter_spec *spec;
  2268. int rc;
  2269. table_id = efx_farch_filter_id_table_id(filter_id);
  2270. if ((unsigned int)table_id >= EFX_FARCH_FILTER_TABLE_COUNT)
  2271. return -ENOENT;
  2272. table = &state->table[table_id];
  2273. filter_idx = efx_farch_filter_id_index(filter_id);
  2274. if (filter_idx >= table->size)
  2275. return -ENOENT;
  2276. spec = &table->spec[filter_idx];
  2277. spin_lock_bh(&efx->filter_lock);
  2278. rc = efx_farch_filter_remove(efx, table, filter_idx, priority);
  2279. spin_unlock_bh(&efx->filter_lock);
  2280. return rc;
  2281. }
  2282. int efx_farch_filter_get_safe(struct efx_nic *efx,
  2283. enum efx_filter_priority priority,
  2284. u32 filter_id, struct efx_filter_spec *spec_buf)
  2285. {
  2286. struct efx_farch_filter_state *state = efx->filter_state;
  2287. enum efx_farch_filter_table_id table_id;
  2288. struct efx_farch_filter_table *table;
  2289. struct efx_farch_filter_spec *spec;
  2290. unsigned int filter_idx;
  2291. int rc;
  2292. table_id = efx_farch_filter_id_table_id(filter_id);
  2293. if ((unsigned int)table_id >= EFX_FARCH_FILTER_TABLE_COUNT)
  2294. return -ENOENT;
  2295. table = &state->table[table_id];
  2296. filter_idx = efx_farch_filter_id_index(filter_id);
  2297. if (filter_idx >= table->size)
  2298. return -ENOENT;
  2299. spec = &table->spec[filter_idx];
  2300. spin_lock_bh(&efx->filter_lock);
  2301. if (test_bit(filter_idx, table->used_bitmap) &&
  2302. spec->priority == priority) {
  2303. efx_farch_filter_to_gen_spec(spec_buf, spec);
  2304. rc = 0;
  2305. } else {
  2306. rc = -ENOENT;
  2307. }
  2308. spin_unlock_bh(&efx->filter_lock);
  2309. return rc;
  2310. }
  2311. static void
  2312. efx_farch_filter_table_clear(struct efx_nic *efx,
  2313. enum efx_farch_filter_table_id table_id,
  2314. enum efx_filter_priority priority)
  2315. {
  2316. struct efx_farch_filter_state *state = efx->filter_state;
  2317. struct efx_farch_filter_table *table = &state->table[table_id];
  2318. unsigned int filter_idx;
  2319. spin_lock_bh(&efx->filter_lock);
  2320. for (filter_idx = 0; filter_idx < table->size; ++filter_idx) {
  2321. if (table->spec[filter_idx].priority != EFX_FILTER_PRI_AUTO)
  2322. efx_farch_filter_remove(efx, table,
  2323. filter_idx, priority);
  2324. }
  2325. spin_unlock_bh(&efx->filter_lock);
  2326. }
  2327. int efx_farch_filter_clear_rx(struct efx_nic *efx,
  2328. enum efx_filter_priority priority)
  2329. {
  2330. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_IP,
  2331. priority);
  2332. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_MAC,
  2333. priority);
  2334. efx_farch_filter_table_clear(efx, EFX_FARCH_FILTER_TABLE_RX_DEF,
  2335. priority);
  2336. return 0;
  2337. }
  2338. u32 efx_farch_filter_count_rx_used(struct efx_nic *efx,
  2339. enum efx_filter_priority priority)
  2340. {
  2341. struct efx_farch_filter_state *state = efx->filter_state;
  2342. enum efx_farch_filter_table_id table_id;
  2343. struct efx_farch_filter_table *table;
  2344. unsigned int filter_idx;
  2345. u32 count = 0;
  2346. spin_lock_bh(&efx->filter_lock);
  2347. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2348. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2349. table_id++) {
  2350. table = &state->table[table_id];
  2351. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2352. if (test_bit(filter_idx, table->used_bitmap) &&
  2353. table->spec[filter_idx].priority == priority)
  2354. ++count;
  2355. }
  2356. }
  2357. spin_unlock_bh(&efx->filter_lock);
  2358. return count;
  2359. }
  2360. s32 efx_farch_filter_get_rx_ids(struct efx_nic *efx,
  2361. enum efx_filter_priority priority,
  2362. u32 *buf, u32 size)
  2363. {
  2364. struct efx_farch_filter_state *state = efx->filter_state;
  2365. enum efx_farch_filter_table_id table_id;
  2366. struct efx_farch_filter_table *table;
  2367. unsigned int filter_idx;
  2368. s32 count = 0;
  2369. spin_lock_bh(&efx->filter_lock);
  2370. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2371. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2372. table_id++) {
  2373. table = &state->table[table_id];
  2374. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2375. if (test_bit(filter_idx, table->used_bitmap) &&
  2376. table->spec[filter_idx].priority == priority) {
  2377. if (count == size) {
  2378. count = -EMSGSIZE;
  2379. goto out;
  2380. }
  2381. buf[count++] = efx_farch_filter_make_id(
  2382. &table->spec[filter_idx], filter_idx);
  2383. }
  2384. }
  2385. }
  2386. out:
  2387. spin_unlock_bh(&efx->filter_lock);
  2388. return count;
  2389. }
  2390. /* Restore filter stater after reset */
  2391. void efx_farch_filter_table_restore(struct efx_nic *efx)
  2392. {
  2393. struct efx_farch_filter_state *state = efx->filter_state;
  2394. enum efx_farch_filter_table_id table_id;
  2395. struct efx_farch_filter_table *table;
  2396. efx_oword_t filter;
  2397. unsigned int filter_idx;
  2398. spin_lock_bh(&efx->filter_lock);
  2399. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2400. table = &state->table[table_id];
  2401. /* Check whether this is a regular register table */
  2402. if (table->step == 0)
  2403. continue;
  2404. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2405. if (!test_bit(filter_idx, table->used_bitmap))
  2406. continue;
  2407. efx_farch_filter_build(&filter, &table->spec[filter_idx]);
  2408. efx_writeo(efx, &filter,
  2409. table->offset + table->step * filter_idx);
  2410. }
  2411. }
  2412. efx_farch_filter_push_rx_config(efx);
  2413. efx_farch_filter_push_tx_limits(efx);
  2414. spin_unlock_bh(&efx->filter_lock);
  2415. }
  2416. void efx_farch_filter_table_remove(struct efx_nic *efx)
  2417. {
  2418. struct efx_farch_filter_state *state = efx->filter_state;
  2419. enum efx_farch_filter_table_id table_id;
  2420. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2421. kfree(state->table[table_id].used_bitmap);
  2422. vfree(state->table[table_id].spec);
  2423. }
  2424. kfree(state);
  2425. }
  2426. int efx_farch_filter_table_probe(struct efx_nic *efx)
  2427. {
  2428. struct efx_farch_filter_state *state;
  2429. struct efx_farch_filter_table *table;
  2430. unsigned table_id;
  2431. state = kzalloc(sizeof(struct efx_farch_filter_state), GFP_KERNEL);
  2432. if (!state)
  2433. return -ENOMEM;
  2434. efx->filter_state = state;
  2435. if (efx_nic_rev(efx) >= EFX_REV_FALCON_B0) {
  2436. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  2437. table->id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2438. table->offset = FR_BZ_RX_FILTER_TBL0;
  2439. table->size = FR_BZ_RX_FILTER_TBL0_ROWS;
  2440. table->step = FR_BZ_RX_FILTER_TBL0_STEP;
  2441. }
  2442. if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
  2443. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_MAC];
  2444. table->id = EFX_FARCH_FILTER_TABLE_RX_MAC;
  2445. table->offset = FR_CZ_RX_MAC_FILTER_TBL0;
  2446. table->size = FR_CZ_RX_MAC_FILTER_TBL0_ROWS;
  2447. table->step = FR_CZ_RX_MAC_FILTER_TBL0_STEP;
  2448. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  2449. table->id = EFX_FARCH_FILTER_TABLE_RX_DEF;
  2450. table->size = EFX_FARCH_FILTER_SIZE_RX_DEF;
  2451. table = &state->table[EFX_FARCH_FILTER_TABLE_TX_MAC];
  2452. table->id = EFX_FARCH_FILTER_TABLE_TX_MAC;
  2453. table->offset = FR_CZ_TX_MAC_FILTER_TBL0;
  2454. table->size = FR_CZ_TX_MAC_FILTER_TBL0_ROWS;
  2455. table->step = FR_CZ_TX_MAC_FILTER_TBL0_STEP;
  2456. }
  2457. for (table_id = 0; table_id < EFX_FARCH_FILTER_TABLE_COUNT; table_id++) {
  2458. table = &state->table[table_id];
  2459. if (table->size == 0)
  2460. continue;
  2461. table->used_bitmap = kcalloc(BITS_TO_LONGS(table->size),
  2462. sizeof(unsigned long),
  2463. GFP_KERNEL);
  2464. if (!table->used_bitmap)
  2465. goto fail;
  2466. table->spec = vzalloc(table->size * sizeof(*table->spec));
  2467. if (!table->spec)
  2468. goto fail;
  2469. }
  2470. table = &state->table[EFX_FARCH_FILTER_TABLE_RX_DEF];
  2471. if (table->size) {
  2472. /* RX default filters must always exist */
  2473. struct efx_farch_filter_spec *spec;
  2474. unsigned i;
  2475. for (i = 0; i < EFX_FARCH_FILTER_SIZE_RX_DEF; i++) {
  2476. spec = &table->spec[i];
  2477. spec->type = EFX_FARCH_FILTER_UC_DEF + i;
  2478. efx_farch_filter_init_rx_auto(efx, spec);
  2479. __set_bit(i, table->used_bitmap);
  2480. }
  2481. }
  2482. efx_farch_filter_push_rx_config(efx);
  2483. return 0;
  2484. fail:
  2485. efx_farch_filter_table_remove(efx);
  2486. return -ENOMEM;
  2487. }
  2488. /* Update scatter enable flags for filters pointing to our own RX queues */
  2489. void efx_farch_filter_update_rx_scatter(struct efx_nic *efx)
  2490. {
  2491. struct efx_farch_filter_state *state = efx->filter_state;
  2492. enum efx_farch_filter_table_id table_id;
  2493. struct efx_farch_filter_table *table;
  2494. efx_oword_t filter;
  2495. unsigned int filter_idx;
  2496. spin_lock_bh(&efx->filter_lock);
  2497. for (table_id = EFX_FARCH_FILTER_TABLE_RX_IP;
  2498. table_id <= EFX_FARCH_FILTER_TABLE_RX_DEF;
  2499. table_id++) {
  2500. table = &state->table[table_id];
  2501. for (filter_idx = 0; filter_idx < table->size; filter_idx++) {
  2502. if (!test_bit(filter_idx, table->used_bitmap) ||
  2503. table->spec[filter_idx].dmaq_id >=
  2504. efx->n_rx_channels)
  2505. continue;
  2506. if (efx->rx_scatter)
  2507. table->spec[filter_idx].flags |=
  2508. EFX_FILTER_FLAG_RX_SCATTER;
  2509. else
  2510. table->spec[filter_idx].flags &=
  2511. ~EFX_FILTER_FLAG_RX_SCATTER;
  2512. if (table_id == EFX_FARCH_FILTER_TABLE_RX_DEF)
  2513. /* Pushed by efx_farch_filter_push_rx_config() */
  2514. continue;
  2515. efx_farch_filter_build(&filter, &table->spec[filter_idx]);
  2516. efx_writeo(efx, &filter,
  2517. table->offset + table->step * filter_idx);
  2518. }
  2519. }
  2520. efx_farch_filter_push_rx_config(efx);
  2521. spin_unlock_bh(&efx->filter_lock);
  2522. }
  2523. #ifdef CONFIG_RFS_ACCEL
  2524. s32 efx_farch_filter_rfs_insert(struct efx_nic *efx,
  2525. struct efx_filter_spec *gen_spec)
  2526. {
  2527. return efx_farch_filter_insert(efx, gen_spec, true);
  2528. }
  2529. bool efx_farch_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
  2530. unsigned int index)
  2531. {
  2532. struct efx_farch_filter_state *state = efx->filter_state;
  2533. struct efx_farch_filter_table *table =
  2534. &state->table[EFX_FARCH_FILTER_TABLE_RX_IP];
  2535. if (test_bit(index, table->used_bitmap) &&
  2536. table->spec[index].priority == EFX_FILTER_PRI_HINT &&
  2537. rps_may_expire_flow(efx->net_dev, table->spec[index].dmaq_id,
  2538. flow_id, index)) {
  2539. efx_farch_filter_table_clear_entry(efx, table, index);
  2540. return true;
  2541. }
  2542. return false;
  2543. }
  2544. #endif /* CONFIG_RFS_ACCEL */
  2545. void efx_farch_filter_sync_rx_mode(struct efx_nic *efx)
  2546. {
  2547. struct net_device *net_dev = efx->net_dev;
  2548. struct netdev_hw_addr *ha;
  2549. union efx_multicast_hash *mc_hash = &efx->multicast_hash;
  2550. u32 crc;
  2551. int bit;
  2552. if (!efx_dev_registered(efx))
  2553. return;
  2554. netif_addr_lock_bh(net_dev);
  2555. efx->unicast_filter = !(net_dev->flags & IFF_PROMISC);
  2556. /* Build multicast hash table */
  2557. if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  2558. memset(mc_hash, 0xff, sizeof(*mc_hash));
  2559. } else {
  2560. memset(mc_hash, 0x00, sizeof(*mc_hash));
  2561. netdev_for_each_mc_addr(ha, net_dev) {
  2562. crc = ether_crc_le(ETH_ALEN, ha->addr);
  2563. bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
  2564. __set_bit_le(bit, mc_hash);
  2565. }
  2566. /* Broadcast packets go through the multicast hash filter.
  2567. * ether_crc_le() of the broadcast address is 0xbe2612ff
  2568. * so we always add bit 0xff to the mask.
  2569. */
  2570. __set_bit_le(0xff, mc_hash);
  2571. }
  2572. netif_addr_unlock_bh(net_dev);
  2573. }