farch.c 87 KB

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