farch.c 84 KB

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