farch.c 88 KB

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