i40e_txrx.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/prefetch.h>
  27. #include <net/busy_poll.h>
  28. #include <linux/bpf_trace.h>
  29. #include "i40e.h"
  30. #include "i40e_trace.h"
  31. #include "i40e_prototype.h"
  32. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  33. u32 td_tag)
  34. {
  35. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  36. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  37. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  38. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  39. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  40. }
  41. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  42. /**
  43. * i40e_fdir - Generate a Flow Director descriptor based on fdata
  44. * @tx_ring: Tx ring to send buffer on
  45. * @fdata: Flow director filter data
  46. * @add: Indicate if we are adding a rule or deleting one
  47. *
  48. **/
  49. static void i40e_fdir(struct i40e_ring *tx_ring,
  50. struct i40e_fdir_filter *fdata, bool add)
  51. {
  52. struct i40e_filter_program_desc *fdir_desc;
  53. struct i40e_pf *pf = tx_ring->vsi->back;
  54. u32 flex_ptype, dtype_cmd;
  55. u16 i;
  56. /* grab the next descriptor */
  57. i = tx_ring->next_to_use;
  58. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  59. i++;
  60. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  61. flex_ptype = I40E_TXD_FLTR_QW0_QINDEX_MASK &
  62. (fdata->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT);
  63. flex_ptype |= I40E_TXD_FLTR_QW0_FLEXOFF_MASK &
  64. (fdata->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  65. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  66. (fdata->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  67. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  68. (fdata->flex_offset << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  69. /* Use LAN VSI Id if not programmed by user */
  70. flex_ptype |= I40E_TXD_FLTR_QW0_DEST_VSI_MASK &
  71. ((u32)(fdata->dest_vsi ? : pf->vsi[pf->lan_vsi]->id) <<
  72. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT);
  73. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  74. dtype_cmd |= add ?
  75. I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  76. I40E_TXD_FLTR_QW1_PCMD_SHIFT :
  77. I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  78. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  79. dtype_cmd |= I40E_TXD_FLTR_QW1_DEST_MASK &
  80. (fdata->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT);
  81. dtype_cmd |= I40E_TXD_FLTR_QW1_FD_STATUS_MASK &
  82. (fdata->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT);
  83. if (fdata->cnt_index) {
  84. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  85. dtype_cmd |= I40E_TXD_FLTR_QW1_CNTINDEX_MASK &
  86. ((u32)fdata->cnt_index <<
  87. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT);
  88. }
  89. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  90. fdir_desc->rsvd = cpu_to_le32(0);
  91. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  92. fdir_desc->fd_id = cpu_to_le32(fdata->fd_id);
  93. }
  94. #define I40E_FD_CLEAN_DELAY 10
  95. /**
  96. * i40e_program_fdir_filter - Program a Flow Director filter
  97. * @fdir_data: Packet data that will be filter parameters
  98. * @raw_packet: the pre-allocated packet buffer for FDir
  99. * @pf: The PF pointer
  100. * @add: True for add/update, False for remove
  101. **/
  102. static int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data,
  103. u8 *raw_packet, struct i40e_pf *pf,
  104. bool add)
  105. {
  106. struct i40e_tx_buffer *tx_buf, *first;
  107. struct i40e_tx_desc *tx_desc;
  108. struct i40e_ring *tx_ring;
  109. struct i40e_vsi *vsi;
  110. struct device *dev;
  111. dma_addr_t dma;
  112. u32 td_cmd = 0;
  113. u16 i;
  114. /* find existing FDIR VSI */
  115. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  116. if (!vsi)
  117. return -ENOENT;
  118. tx_ring = vsi->tx_rings[0];
  119. dev = tx_ring->dev;
  120. /* we need two descriptors to add/del a filter and we can wait */
  121. for (i = I40E_FD_CLEAN_DELAY; I40E_DESC_UNUSED(tx_ring) < 2; i--) {
  122. if (!i)
  123. return -EAGAIN;
  124. msleep_interruptible(1);
  125. }
  126. dma = dma_map_single(dev, raw_packet,
  127. I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
  128. if (dma_mapping_error(dev, dma))
  129. goto dma_fail;
  130. /* grab the next descriptor */
  131. i = tx_ring->next_to_use;
  132. first = &tx_ring->tx_bi[i];
  133. i40e_fdir(tx_ring, fdir_data, add);
  134. /* Now program a dummy descriptor */
  135. i = tx_ring->next_to_use;
  136. tx_desc = I40E_TX_DESC(tx_ring, i);
  137. tx_buf = &tx_ring->tx_bi[i];
  138. tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
  139. memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
  140. /* record length, and DMA address */
  141. dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
  142. dma_unmap_addr_set(tx_buf, dma, dma);
  143. tx_desc->buffer_addr = cpu_to_le64(dma);
  144. td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
  145. tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
  146. tx_buf->raw_buf = (void *)raw_packet;
  147. tx_desc->cmd_type_offset_bsz =
  148. build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
  149. /* Force memory writes to complete before letting h/w
  150. * know there are new descriptors to fetch.
  151. */
  152. wmb();
  153. /* Mark the data descriptor to be watched */
  154. first->next_to_watch = tx_desc;
  155. writel(tx_ring->next_to_use, tx_ring->tail);
  156. return 0;
  157. dma_fail:
  158. return -1;
  159. }
  160. #define IP_HEADER_OFFSET 14
  161. #define I40E_UDPIP_DUMMY_PACKET_LEN 42
  162. /**
  163. * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
  164. * @vsi: pointer to the targeted VSI
  165. * @fd_data: the flow director data required for the FDir descriptor
  166. * @add: true adds a filter, false removes it
  167. *
  168. * Returns 0 if the filters were successfully added or removed
  169. **/
  170. static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
  171. struct i40e_fdir_filter *fd_data,
  172. bool add)
  173. {
  174. struct i40e_pf *pf = vsi->back;
  175. struct udphdr *udp;
  176. struct iphdr *ip;
  177. u8 *raw_packet;
  178. int ret;
  179. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  180. 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
  181. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  182. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  183. if (!raw_packet)
  184. return -ENOMEM;
  185. memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
  186. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  187. udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
  188. + sizeof(struct iphdr));
  189. ip->daddr = fd_data->dst_ip;
  190. udp->dest = fd_data->dst_port;
  191. ip->saddr = fd_data->src_ip;
  192. udp->source = fd_data->src_port;
  193. if (fd_data->flex_filter) {
  194. u8 *payload = raw_packet + I40E_UDPIP_DUMMY_PACKET_LEN;
  195. __be16 pattern = fd_data->flex_word;
  196. u16 off = fd_data->flex_offset;
  197. *((__force __be16 *)(payload + off)) = pattern;
  198. }
  199. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  200. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  201. if (ret) {
  202. dev_info(&pf->pdev->dev,
  203. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  204. fd_data->pctype, fd_data->fd_id, ret);
  205. /* Free the packet buffer since it wasn't added to the ring */
  206. kfree(raw_packet);
  207. return -EOPNOTSUPP;
  208. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  209. if (add)
  210. dev_info(&pf->pdev->dev,
  211. "Filter OK for PCTYPE %d loc = %d\n",
  212. fd_data->pctype, fd_data->fd_id);
  213. else
  214. dev_info(&pf->pdev->dev,
  215. "Filter deleted for PCTYPE %d loc = %d\n",
  216. fd_data->pctype, fd_data->fd_id);
  217. }
  218. if (add)
  219. pf->fd_udp4_filter_cnt++;
  220. else
  221. pf->fd_udp4_filter_cnt--;
  222. return 0;
  223. }
  224. #define I40E_TCPIP_DUMMY_PACKET_LEN 54
  225. /**
  226. * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
  227. * @vsi: pointer to the targeted VSI
  228. * @fd_data: the flow director data required for the FDir descriptor
  229. * @add: true adds a filter, false removes it
  230. *
  231. * Returns 0 if the filters were successfully added or removed
  232. **/
  233. static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
  234. struct i40e_fdir_filter *fd_data,
  235. bool add)
  236. {
  237. struct i40e_pf *pf = vsi->back;
  238. struct tcphdr *tcp;
  239. struct iphdr *ip;
  240. u8 *raw_packet;
  241. int ret;
  242. /* Dummy packet */
  243. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  244. 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
  245. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
  246. 0x0, 0x72, 0, 0, 0, 0};
  247. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  248. if (!raw_packet)
  249. return -ENOMEM;
  250. memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
  251. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  252. tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
  253. + sizeof(struct iphdr));
  254. ip->daddr = fd_data->dst_ip;
  255. tcp->dest = fd_data->dst_port;
  256. ip->saddr = fd_data->src_ip;
  257. tcp->source = fd_data->src_port;
  258. if (fd_data->flex_filter) {
  259. u8 *payload = raw_packet + I40E_TCPIP_DUMMY_PACKET_LEN;
  260. __be16 pattern = fd_data->flex_word;
  261. u16 off = fd_data->flex_offset;
  262. *((__force __be16 *)(payload + off)) = pattern;
  263. }
  264. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  265. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  266. if (ret) {
  267. dev_info(&pf->pdev->dev,
  268. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  269. fd_data->pctype, fd_data->fd_id, ret);
  270. /* Free the packet buffer since it wasn't added to the ring */
  271. kfree(raw_packet);
  272. return -EOPNOTSUPP;
  273. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  274. if (add)
  275. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d loc = %d)\n",
  276. fd_data->pctype, fd_data->fd_id);
  277. else
  278. dev_info(&pf->pdev->dev,
  279. "Filter deleted for PCTYPE %d loc = %d\n",
  280. fd_data->pctype, fd_data->fd_id);
  281. }
  282. if (add) {
  283. pf->fd_tcp4_filter_cnt++;
  284. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  285. I40E_DEBUG_FD & pf->hw.debug_mask)
  286. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
  287. pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
  288. } else {
  289. pf->fd_tcp4_filter_cnt--;
  290. }
  291. return 0;
  292. }
  293. #define I40E_SCTPIP_DUMMY_PACKET_LEN 46
  294. /**
  295. * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
  296. * a specific flow spec
  297. * @vsi: pointer to the targeted VSI
  298. * @fd_data: the flow director data required for the FDir descriptor
  299. * @add: true adds a filter, false removes it
  300. *
  301. * Returns 0 if the filters were successfully added or removed
  302. **/
  303. static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
  304. struct i40e_fdir_filter *fd_data,
  305. bool add)
  306. {
  307. struct i40e_pf *pf = vsi->back;
  308. struct sctphdr *sctp;
  309. struct iphdr *ip;
  310. u8 *raw_packet;
  311. int ret;
  312. /* Dummy packet */
  313. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  314. 0x45, 0, 0, 0x20, 0, 0, 0x40, 0, 0x40, 0x84, 0, 0, 0, 0, 0, 0,
  315. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  316. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  317. if (!raw_packet)
  318. return -ENOMEM;
  319. memcpy(raw_packet, packet, I40E_SCTPIP_DUMMY_PACKET_LEN);
  320. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  321. sctp = (struct sctphdr *)(raw_packet + IP_HEADER_OFFSET
  322. + sizeof(struct iphdr));
  323. ip->daddr = fd_data->dst_ip;
  324. sctp->dest = fd_data->dst_port;
  325. ip->saddr = fd_data->src_ip;
  326. sctp->source = fd_data->src_port;
  327. if (fd_data->flex_filter) {
  328. u8 *payload = raw_packet + I40E_SCTPIP_DUMMY_PACKET_LEN;
  329. __be16 pattern = fd_data->flex_word;
  330. u16 off = fd_data->flex_offset;
  331. *((__force __be16 *)(payload + off)) = pattern;
  332. }
  333. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
  334. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  335. if (ret) {
  336. dev_info(&pf->pdev->dev,
  337. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  338. fd_data->pctype, fd_data->fd_id, ret);
  339. /* Free the packet buffer since it wasn't added to the ring */
  340. kfree(raw_packet);
  341. return -EOPNOTSUPP;
  342. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  343. if (add)
  344. dev_info(&pf->pdev->dev,
  345. "Filter OK for PCTYPE %d loc = %d\n",
  346. fd_data->pctype, fd_data->fd_id);
  347. else
  348. dev_info(&pf->pdev->dev,
  349. "Filter deleted for PCTYPE %d loc = %d\n",
  350. fd_data->pctype, fd_data->fd_id);
  351. }
  352. if (add)
  353. pf->fd_sctp4_filter_cnt++;
  354. else
  355. pf->fd_sctp4_filter_cnt--;
  356. return 0;
  357. }
  358. #define I40E_IP_DUMMY_PACKET_LEN 34
  359. /**
  360. * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
  361. * a specific flow spec
  362. * @vsi: pointer to the targeted VSI
  363. * @fd_data: the flow director data required for the FDir descriptor
  364. * @add: true adds a filter, false removes it
  365. *
  366. * Returns 0 if the filters were successfully added or removed
  367. **/
  368. static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
  369. struct i40e_fdir_filter *fd_data,
  370. bool add)
  371. {
  372. struct i40e_pf *pf = vsi->back;
  373. struct iphdr *ip;
  374. u8 *raw_packet;
  375. int ret;
  376. int i;
  377. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  378. 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
  379. 0, 0, 0, 0};
  380. for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  381. i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
  382. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  383. if (!raw_packet)
  384. return -ENOMEM;
  385. memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
  386. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  387. ip->saddr = fd_data->src_ip;
  388. ip->daddr = fd_data->dst_ip;
  389. ip->protocol = 0;
  390. if (fd_data->flex_filter) {
  391. u8 *payload = raw_packet + I40E_IP_DUMMY_PACKET_LEN;
  392. __be16 pattern = fd_data->flex_word;
  393. u16 off = fd_data->flex_offset;
  394. *((__force __be16 *)(payload + off)) = pattern;
  395. }
  396. fd_data->pctype = i;
  397. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  398. if (ret) {
  399. dev_info(&pf->pdev->dev,
  400. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  401. fd_data->pctype, fd_data->fd_id, ret);
  402. /* The packet buffer wasn't added to the ring so we
  403. * need to free it now.
  404. */
  405. kfree(raw_packet);
  406. return -EOPNOTSUPP;
  407. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  408. if (add)
  409. dev_info(&pf->pdev->dev,
  410. "Filter OK for PCTYPE %d loc = %d\n",
  411. fd_data->pctype, fd_data->fd_id);
  412. else
  413. dev_info(&pf->pdev->dev,
  414. "Filter deleted for PCTYPE %d loc = %d\n",
  415. fd_data->pctype, fd_data->fd_id);
  416. }
  417. }
  418. if (add)
  419. pf->fd_ip4_filter_cnt++;
  420. else
  421. pf->fd_ip4_filter_cnt--;
  422. return 0;
  423. }
  424. /**
  425. * i40e_add_del_fdir - Build raw packets to add/del fdir filter
  426. * @vsi: pointer to the targeted VSI
  427. * @cmd: command to get or set RX flow classification rules
  428. * @add: true adds a filter, false removes it
  429. *
  430. **/
  431. int i40e_add_del_fdir(struct i40e_vsi *vsi,
  432. struct i40e_fdir_filter *input, bool add)
  433. {
  434. struct i40e_pf *pf = vsi->back;
  435. int ret;
  436. switch (input->flow_type & ~FLOW_EXT) {
  437. case TCP_V4_FLOW:
  438. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  439. break;
  440. case UDP_V4_FLOW:
  441. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  442. break;
  443. case SCTP_V4_FLOW:
  444. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  445. break;
  446. case IP_USER_FLOW:
  447. switch (input->ip4_proto) {
  448. case IPPROTO_TCP:
  449. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  450. break;
  451. case IPPROTO_UDP:
  452. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  453. break;
  454. case IPPROTO_SCTP:
  455. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  456. break;
  457. case IPPROTO_IP:
  458. ret = i40e_add_del_fdir_ipv4(vsi, input, add);
  459. break;
  460. default:
  461. /* We cannot support masking based on protocol */
  462. dev_info(&pf->pdev->dev, "Unsupported IPv4 protocol 0x%02x\n",
  463. input->ip4_proto);
  464. return -EINVAL;
  465. }
  466. break;
  467. default:
  468. dev_info(&pf->pdev->dev, "Unsupported flow type 0x%02x\n",
  469. input->flow_type);
  470. return -EINVAL;
  471. }
  472. /* The buffer allocated here will be normally be freed by
  473. * i40e_clean_fdir_tx_irq() as it reclaims resources after transmit
  474. * completion. In the event of an error adding the buffer to the FDIR
  475. * ring, it will immediately be freed. It may also be freed by
  476. * i40e_clean_tx_ring() when closing the VSI.
  477. */
  478. return ret;
  479. }
  480. /**
  481. * i40e_fd_handle_status - check the Programming Status for FD
  482. * @rx_ring: the Rx ring for this descriptor
  483. * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
  484. * @prog_id: the id originally used for programming
  485. *
  486. * This is used to verify if the FD programming or invalidation
  487. * requested by SW to the HW is successful or not and take actions accordingly.
  488. **/
  489. static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
  490. union i40e_rx_desc *rx_desc, u8 prog_id)
  491. {
  492. struct i40e_pf *pf = rx_ring->vsi->back;
  493. struct pci_dev *pdev = pf->pdev;
  494. u32 fcnt_prog, fcnt_avail;
  495. u32 error;
  496. u64 qw;
  497. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  498. error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
  499. I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
  500. if (error == BIT(I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
  501. pf->fd_inv = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fd_id);
  502. if ((rx_desc->wb.qword0.hi_dword.fd_id != 0) ||
  503. (I40E_DEBUG_FD & pf->hw.debug_mask))
  504. dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
  505. pf->fd_inv);
  506. /* Check if the programming error is for ATR.
  507. * If so, auto disable ATR and set a state for
  508. * flush in progress. Next time we come here if flush is in
  509. * progress do nothing, once flush is complete the state will
  510. * be cleared.
  511. */
  512. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  513. return;
  514. pf->fd_add_err++;
  515. /* store the current atr filter count */
  516. pf->fd_atr_cnt = i40e_get_current_atr_cnt(pf);
  517. if ((rx_desc->wb.qword0.hi_dword.fd_id == 0) &&
  518. pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
  519. pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
  520. set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  521. }
  522. /* filter programming failed most likely due to table full */
  523. fcnt_prog = i40e_get_global_fd_count(pf);
  524. fcnt_avail = pf->fdir_pf_filter_count;
  525. /* If ATR is running fcnt_prog can quickly change,
  526. * if we are very close to full, it makes sense to disable
  527. * FD ATR/SB and then re-enable it when there is room.
  528. */
  529. if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
  530. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  531. !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED)) {
  532. pf->flags |= I40E_FLAG_FD_SB_AUTO_DISABLED;
  533. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  534. dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
  535. }
  536. }
  537. } else if (error == BIT(I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
  538. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  539. dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
  540. rx_desc->wb.qword0.hi_dword.fd_id);
  541. }
  542. }
  543. /**
  544. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  545. * @ring: the ring that owns the buffer
  546. * @tx_buffer: the buffer to free
  547. **/
  548. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  549. struct i40e_tx_buffer *tx_buffer)
  550. {
  551. if (tx_buffer->skb) {
  552. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  553. kfree(tx_buffer->raw_buf);
  554. else if (ring_is_xdp(ring))
  555. page_frag_free(tx_buffer->raw_buf);
  556. else
  557. dev_kfree_skb_any(tx_buffer->skb);
  558. if (dma_unmap_len(tx_buffer, len))
  559. dma_unmap_single(ring->dev,
  560. dma_unmap_addr(tx_buffer, dma),
  561. dma_unmap_len(tx_buffer, len),
  562. DMA_TO_DEVICE);
  563. } else if (dma_unmap_len(tx_buffer, len)) {
  564. dma_unmap_page(ring->dev,
  565. dma_unmap_addr(tx_buffer, dma),
  566. dma_unmap_len(tx_buffer, len),
  567. DMA_TO_DEVICE);
  568. }
  569. tx_buffer->next_to_watch = NULL;
  570. tx_buffer->skb = NULL;
  571. dma_unmap_len_set(tx_buffer, len, 0);
  572. /* tx_buffer must be completely set up in the transmit path */
  573. }
  574. /**
  575. * i40e_clean_tx_ring - Free any empty Tx buffers
  576. * @tx_ring: ring to be cleaned
  577. **/
  578. void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
  579. {
  580. unsigned long bi_size;
  581. u16 i;
  582. /* ring already cleared, nothing to do */
  583. if (!tx_ring->tx_bi)
  584. return;
  585. /* Free all the Tx ring sk_buffs */
  586. for (i = 0; i < tx_ring->count; i++)
  587. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  588. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  589. memset(tx_ring->tx_bi, 0, bi_size);
  590. /* Zero out the descriptor ring */
  591. memset(tx_ring->desc, 0, tx_ring->size);
  592. tx_ring->next_to_use = 0;
  593. tx_ring->next_to_clean = 0;
  594. if (!tx_ring->netdev)
  595. return;
  596. /* cleanup Tx queue statistics */
  597. netdev_tx_reset_queue(txring_txq(tx_ring));
  598. }
  599. /**
  600. * i40e_free_tx_resources - Free Tx resources per queue
  601. * @tx_ring: Tx descriptor ring for a specific queue
  602. *
  603. * Free all transmit software resources
  604. **/
  605. void i40e_free_tx_resources(struct i40e_ring *tx_ring)
  606. {
  607. i40e_clean_tx_ring(tx_ring);
  608. kfree(tx_ring->tx_bi);
  609. tx_ring->tx_bi = NULL;
  610. if (tx_ring->desc) {
  611. dma_free_coherent(tx_ring->dev, tx_ring->size,
  612. tx_ring->desc, tx_ring->dma);
  613. tx_ring->desc = NULL;
  614. }
  615. }
  616. /**
  617. * i40e_get_tx_pending - how many tx descriptors not processed
  618. * @tx_ring: the ring of descriptors
  619. *
  620. * Since there is no access to the ring head register
  621. * in XL710, we need to use our local copies
  622. **/
  623. u32 i40e_get_tx_pending(struct i40e_ring *ring)
  624. {
  625. u32 head, tail;
  626. head = i40e_get_head(ring);
  627. tail = readl(ring->tail);
  628. if (head != tail)
  629. return (head < tail) ?
  630. tail - head : (tail + ring->count - head);
  631. return 0;
  632. }
  633. #define WB_STRIDE 4
  634. /**
  635. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  636. * @vsi: the VSI we care about
  637. * @tx_ring: Tx ring to clean
  638. * @napi_budget: Used to determine if we are in netpoll
  639. *
  640. * Returns true if there's any budget left (e.g. the clean is finished)
  641. **/
  642. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  643. struct i40e_ring *tx_ring, int napi_budget)
  644. {
  645. u16 i = tx_ring->next_to_clean;
  646. struct i40e_tx_buffer *tx_buf;
  647. struct i40e_tx_desc *tx_head;
  648. struct i40e_tx_desc *tx_desc;
  649. unsigned int total_bytes = 0, total_packets = 0;
  650. unsigned int budget = vsi->work_limit;
  651. tx_buf = &tx_ring->tx_bi[i];
  652. tx_desc = I40E_TX_DESC(tx_ring, i);
  653. i -= tx_ring->count;
  654. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  655. do {
  656. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  657. /* if next_to_watch is not set then there is no work pending */
  658. if (!eop_desc)
  659. break;
  660. /* prevent any other reads prior to eop_desc */
  661. read_barrier_depends();
  662. i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  663. /* we have caught up to head, no work left to do */
  664. if (tx_head == tx_desc)
  665. break;
  666. /* clear next_to_watch to prevent false hangs */
  667. tx_buf->next_to_watch = NULL;
  668. /* update the statistics for this packet */
  669. total_bytes += tx_buf->bytecount;
  670. total_packets += tx_buf->gso_segs;
  671. /* free the skb/XDP data */
  672. if (ring_is_xdp(tx_ring))
  673. page_frag_free(tx_buf->raw_buf);
  674. else
  675. napi_consume_skb(tx_buf->skb, napi_budget);
  676. /* unmap skb header data */
  677. dma_unmap_single(tx_ring->dev,
  678. dma_unmap_addr(tx_buf, dma),
  679. dma_unmap_len(tx_buf, len),
  680. DMA_TO_DEVICE);
  681. /* clear tx_buffer data */
  682. tx_buf->skb = NULL;
  683. dma_unmap_len_set(tx_buf, len, 0);
  684. /* unmap remaining buffers */
  685. while (tx_desc != eop_desc) {
  686. i40e_trace(clean_tx_irq_unmap,
  687. tx_ring, tx_desc, tx_buf);
  688. tx_buf++;
  689. tx_desc++;
  690. i++;
  691. if (unlikely(!i)) {
  692. i -= tx_ring->count;
  693. tx_buf = tx_ring->tx_bi;
  694. tx_desc = I40E_TX_DESC(tx_ring, 0);
  695. }
  696. /* unmap any remaining paged data */
  697. if (dma_unmap_len(tx_buf, len)) {
  698. dma_unmap_page(tx_ring->dev,
  699. dma_unmap_addr(tx_buf, dma),
  700. dma_unmap_len(tx_buf, len),
  701. DMA_TO_DEVICE);
  702. dma_unmap_len_set(tx_buf, len, 0);
  703. }
  704. }
  705. /* move us one more past the eop_desc for start of next pkt */
  706. tx_buf++;
  707. tx_desc++;
  708. i++;
  709. if (unlikely(!i)) {
  710. i -= tx_ring->count;
  711. tx_buf = tx_ring->tx_bi;
  712. tx_desc = I40E_TX_DESC(tx_ring, 0);
  713. }
  714. prefetch(tx_desc);
  715. /* update budget accounting */
  716. budget--;
  717. } while (likely(budget));
  718. i += tx_ring->count;
  719. tx_ring->next_to_clean = i;
  720. u64_stats_update_begin(&tx_ring->syncp);
  721. tx_ring->stats.bytes += total_bytes;
  722. tx_ring->stats.packets += total_packets;
  723. u64_stats_update_end(&tx_ring->syncp);
  724. tx_ring->q_vector->tx.total_bytes += total_bytes;
  725. tx_ring->q_vector->tx.total_packets += total_packets;
  726. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  727. /* check to see if there are < 4 descriptors
  728. * waiting to be written back, then kick the hardware to force
  729. * them to be written back in case we stay in NAPI.
  730. * In this mode on X722 we do not enable Interrupt.
  731. */
  732. unsigned int j = i40e_get_tx_pending(tx_ring);
  733. if (budget &&
  734. ((j / WB_STRIDE) == 0) && (j > 0) &&
  735. !test_bit(__I40E_VSI_DOWN, vsi->state) &&
  736. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  737. tx_ring->arm_wb = true;
  738. }
  739. if (ring_is_xdp(tx_ring))
  740. return !!budget;
  741. /* notify netdev of completed buffers */
  742. netdev_tx_completed_queue(txring_txq(tx_ring),
  743. total_packets, total_bytes);
  744. #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2))
  745. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  746. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  747. /* Make sure that anybody stopping the queue after this
  748. * sees the new next_to_clean.
  749. */
  750. smp_mb();
  751. if (__netif_subqueue_stopped(tx_ring->netdev,
  752. tx_ring->queue_index) &&
  753. !test_bit(__I40E_VSI_DOWN, vsi->state)) {
  754. netif_wake_subqueue(tx_ring->netdev,
  755. tx_ring->queue_index);
  756. ++tx_ring->tx_stats.restart_queue;
  757. }
  758. }
  759. return !!budget;
  760. }
  761. /**
  762. * i40e_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  763. * @vsi: the VSI we care about
  764. * @q_vector: the vector on which to enable writeback
  765. *
  766. **/
  767. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  768. struct i40e_q_vector *q_vector)
  769. {
  770. u16 flags = q_vector->tx.ring[0].flags;
  771. u32 val;
  772. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  773. return;
  774. if (q_vector->arm_wb_state)
  775. return;
  776. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  777. val = I40E_PFINT_DYN_CTLN_WB_ON_ITR_MASK |
  778. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK; /* set noitr */
  779. wr32(&vsi->back->hw,
  780. I40E_PFINT_DYN_CTLN(q_vector->v_idx + vsi->base_vector - 1),
  781. val);
  782. } else {
  783. val = I40E_PFINT_DYN_CTL0_WB_ON_ITR_MASK |
  784. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK; /* set noitr */
  785. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  786. }
  787. q_vector->arm_wb_state = true;
  788. }
  789. /**
  790. * i40e_force_wb - Issue SW Interrupt so HW does a wb
  791. * @vsi: the VSI we care about
  792. * @q_vector: the vector on which to force writeback
  793. *
  794. **/
  795. void i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  796. {
  797. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  798. u32 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  799. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK | /* set noitr */
  800. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
  801. I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK;
  802. /* allow 00 to be written to the index */
  803. wr32(&vsi->back->hw,
  804. I40E_PFINT_DYN_CTLN(q_vector->v_idx +
  805. vsi->base_vector - 1), val);
  806. } else {
  807. u32 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  808. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK | /* set noitr */
  809. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
  810. I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK;
  811. /* allow 00 to be written to the index */
  812. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  813. }
  814. }
  815. /**
  816. * i40e_set_new_dynamic_itr - Find new ITR level
  817. * @rc: structure containing ring performance data
  818. *
  819. * Returns true if ITR changed, false if not
  820. *
  821. * Stores a new ITR value based on packets and byte counts during
  822. * the last interrupt. The advantage of per interrupt computation
  823. * is faster updates and more accurate ITR for the current traffic
  824. * pattern. Constants in this function were computed based on
  825. * theoretical maximum wire speed and thresholds were set based on
  826. * testing data as well as attempting to minimize response time
  827. * while increasing bulk throughput.
  828. **/
  829. static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  830. {
  831. enum i40e_latency_range new_latency_range = rc->latency_range;
  832. u32 new_itr = rc->itr;
  833. int bytes_per_usec;
  834. unsigned int usecs, estimated_usecs;
  835. if (rc->total_packets == 0 || !rc->itr)
  836. return false;
  837. usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
  838. bytes_per_usec = rc->total_bytes / usecs;
  839. /* The calculations in this algorithm depend on interrupts actually
  840. * firing at the ITR rate. This may not happen if the packet rate is
  841. * really low, or if we've been napi polling. Check to make sure
  842. * that's not the case before we continue.
  843. */
  844. estimated_usecs = jiffies_to_usecs(jiffies - rc->last_itr_update);
  845. if (estimated_usecs > usecs) {
  846. new_latency_range = I40E_LOW_LATENCY;
  847. goto reset_latency;
  848. }
  849. /* simple throttlerate management
  850. * 0-10MB/s lowest (50000 ints/s)
  851. * 10-20MB/s low (20000 ints/s)
  852. * 20-1249MB/s bulk (18000 ints/s)
  853. *
  854. * The math works out because the divisor is in 10^(-6) which
  855. * turns the bytes/us input value into MB/s values, but
  856. * make sure to use usecs, as the register values written
  857. * are in 2 usec increments in the ITR registers, and make sure
  858. * to use the smoothed values that the countdown timer gives us.
  859. */
  860. switch (new_latency_range) {
  861. case I40E_LOWEST_LATENCY:
  862. if (bytes_per_usec > 10)
  863. new_latency_range = I40E_LOW_LATENCY;
  864. break;
  865. case I40E_LOW_LATENCY:
  866. if (bytes_per_usec > 20)
  867. new_latency_range = I40E_BULK_LATENCY;
  868. else if (bytes_per_usec <= 10)
  869. new_latency_range = I40E_LOWEST_LATENCY;
  870. break;
  871. case I40E_BULK_LATENCY:
  872. default:
  873. if (bytes_per_usec <= 20)
  874. new_latency_range = I40E_LOW_LATENCY;
  875. break;
  876. }
  877. reset_latency:
  878. rc->latency_range = new_latency_range;
  879. switch (new_latency_range) {
  880. case I40E_LOWEST_LATENCY:
  881. new_itr = I40E_ITR_50K;
  882. break;
  883. case I40E_LOW_LATENCY:
  884. new_itr = I40E_ITR_20K;
  885. break;
  886. case I40E_BULK_LATENCY:
  887. new_itr = I40E_ITR_18K;
  888. break;
  889. default:
  890. break;
  891. }
  892. rc->total_bytes = 0;
  893. rc->total_packets = 0;
  894. rc->last_itr_update = jiffies;
  895. if (new_itr != rc->itr) {
  896. rc->itr = new_itr;
  897. return true;
  898. }
  899. return false;
  900. }
  901. /**
  902. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  903. * @rx_ring: rx descriptor ring to store buffers on
  904. * @old_buff: donor buffer to have page reused
  905. *
  906. * Synchronizes page for reuse by the adapter
  907. **/
  908. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  909. struct i40e_rx_buffer *old_buff)
  910. {
  911. struct i40e_rx_buffer *new_buff;
  912. u16 nta = rx_ring->next_to_alloc;
  913. new_buff = &rx_ring->rx_bi[nta];
  914. /* update, and store next to alloc */
  915. nta++;
  916. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  917. /* transfer page from old buffer to new buffer */
  918. new_buff->dma = old_buff->dma;
  919. new_buff->page = old_buff->page;
  920. new_buff->page_offset = old_buff->page_offset;
  921. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  922. }
  923. /**
  924. * i40e_rx_is_programming_status - check for programming status descriptor
  925. * @qw: qword representing status_error_len in CPU ordering
  926. *
  927. * The value of in the descriptor length field indicate if this
  928. * is a programming status descriptor for flow director or FCoE
  929. * by the value of I40E_RX_PROG_STATUS_DESC_LENGTH, otherwise
  930. * it is a packet descriptor.
  931. **/
  932. static inline bool i40e_rx_is_programming_status(u64 qw)
  933. {
  934. /* The Rx filter programming status and SPH bit occupy the same
  935. * spot in the descriptor. Since we don't support packet split we
  936. * can just reuse the bit as an indication that this is a
  937. * programming status descriptor.
  938. */
  939. return qw & I40E_RXD_QW1_LENGTH_SPH_MASK;
  940. }
  941. /**
  942. * i40e_clean_programming_status - clean the programming status descriptor
  943. * @rx_ring: the rx ring that has this descriptor
  944. * @rx_desc: the rx descriptor written back by HW
  945. * @qw: qword representing status_error_len in CPU ordering
  946. *
  947. * Flow director should handle FD_FILTER_STATUS to check its filter programming
  948. * status being successful or not and take actions accordingly. FCoE should
  949. * handle its context/filter programming/invalidation status and take actions.
  950. *
  951. **/
  952. static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
  953. union i40e_rx_desc *rx_desc,
  954. u64 qw)
  955. {
  956. struct i40e_rx_buffer *rx_buffer;
  957. u32 ntc = rx_ring->next_to_clean;
  958. u8 id;
  959. /* fetch, update, and store next to clean */
  960. rx_buffer = &rx_ring->rx_bi[ntc++];
  961. ntc = (ntc < rx_ring->count) ? ntc : 0;
  962. rx_ring->next_to_clean = ntc;
  963. prefetch(I40E_RX_DESC(rx_ring, ntc));
  964. /* place unused page back on the ring */
  965. i40e_reuse_rx_page(rx_ring, rx_buffer);
  966. rx_ring->rx_stats.page_reuse_count++;
  967. /* clear contents of buffer_info */
  968. rx_buffer->page = NULL;
  969. id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
  970. I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
  971. if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
  972. i40e_fd_handle_status(rx_ring, rx_desc, id);
  973. }
  974. /**
  975. * i40e_setup_tx_descriptors - Allocate the Tx descriptors
  976. * @tx_ring: the tx ring to set up
  977. *
  978. * Return 0 on success, negative on error
  979. **/
  980. int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
  981. {
  982. struct device *dev = tx_ring->dev;
  983. int bi_size;
  984. if (!dev)
  985. return -ENOMEM;
  986. /* warn if we are about to overwrite the pointer */
  987. WARN_ON(tx_ring->tx_bi);
  988. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  989. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  990. if (!tx_ring->tx_bi)
  991. goto err;
  992. u64_stats_init(&tx_ring->syncp);
  993. /* round up to nearest 4K */
  994. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  995. /* add u32 for head writeback, align after this takes care of
  996. * guaranteeing this is at least one cache line in size
  997. */
  998. tx_ring->size += sizeof(u32);
  999. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1000. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  1001. &tx_ring->dma, GFP_KERNEL);
  1002. if (!tx_ring->desc) {
  1003. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  1004. tx_ring->size);
  1005. goto err;
  1006. }
  1007. tx_ring->next_to_use = 0;
  1008. tx_ring->next_to_clean = 0;
  1009. return 0;
  1010. err:
  1011. kfree(tx_ring->tx_bi);
  1012. tx_ring->tx_bi = NULL;
  1013. return -ENOMEM;
  1014. }
  1015. /**
  1016. * i40e_clean_rx_ring - Free Rx buffers
  1017. * @rx_ring: ring to be cleaned
  1018. **/
  1019. void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
  1020. {
  1021. unsigned long bi_size;
  1022. u16 i;
  1023. /* ring already cleared, nothing to do */
  1024. if (!rx_ring->rx_bi)
  1025. return;
  1026. if (rx_ring->skb) {
  1027. dev_kfree_skb(rx_ring->skb);
  1028. rx_ring->skb = NULL;
  1029. }
  1030. /* Free all the Rx ring sk_buffs */
  1031. for (i = 0; i < rx_ring->count; i++) {
  1032. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  1033. if (!rx_bi->page)
  1034. continue;
  1035. /* Invalidate cache lines that may have been written to by
  1036. * device so that we avoid corrupting memory.
  1037. */
  1038. dma_sync_single_range_for_cpu(rx_ring->dev,
  1039. rx_bi->dma,
  1040. rx_bi->page_offset,
  1041. rx_ring->rx_buf_len,
  1042. DMA_FROM_DEVICE);
  1043. /* free resources associated with mapping */
  1044. dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
  1045. i40e_rx_pg_size(rx_ring),
  1046. DMA_FROM_DEVICE,
  1047. I40E_RX_DMA_ATTR);
  1048. __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
  1049. rx_bi->page = NULL;
  1050. rx_bi->page_offset = 0;
  1051. }
  1052. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1053. memset(rx_ring->rx_bi, 0, bi_size);
  1054. /* Zero out the descriptor ring */
  1055. memset(rx_ring->desc, 0, rx_ring->size);
  1056. rx_ring->next_to_alloc = 0;
  1057. rx_ring->next_to_clean = 0;
  1058. rx_ring->next_to_use = 0;
  1059. }
  1060. /**
  1061. * i40e_free_rx_resources - Free Rx resources
  1062. * @rx_ring: ring to clean the resources from
  1063. *
  1064. * Free all receive software resources
  1065. **/
  1066. void i40e_free_rx_resources(struct i40e_ring *rx_ring)
  1067. {
  1068. i40e_clean_rx_ring(rx_ring);
  1069. rx_ring->xdp_prog = NULL;
  1070. kfree(rx_ring->rx_bi);
  1071. rx_ring->rx_bi = NULL;
  1072. if (rx_ring->desc) {
  1073. dma_free_coherent(rx_ring->dev, rx_ring->size,
  1074. rx_ring->desc, rx_ring->dma);
  1075. rx_ring->desc = NULL;
  1076. }
  1077. }
  1078. /**
  1079. * i40e_setup_rx_descriptors - Allocate Rx descriptors
  1080. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  1081. *
  1082. * Returns 0 on success, negative on failure
  1083. **/
  1084. int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
  1085. {
  1086. struct device *dev = rx_ring->dev;
  1087. int bi_size;
  1088. /* warn if we are about to overwrite the pointer */
  1089. WARN_ON(rx_ring->rx_bi);
  1090. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1091. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  1092. if (!rx_ring->rx_bi)
  1093. goto err;
  1094. u64_stats_init(&rx_ring->syncp);
  1095. /* Round up to nearest 4K */
  1096. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  1097. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1098. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  1099. &rx_ring->dma, GFP_KERNEL);
  1100. if (!rx_ring->desc) {
  1101. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  1102. rx_ring->size);
  1103. goto err;
  1104. }
  1105. rx_ring->next_to_alloc = 0;
  1106. rx_ring->next_to_clean = 0;
  1107. rx_ring->next_to_use = 0;
  1108. rx_ring->xdp_prog = rx_ring->vsi->xdp_prog;
  1109. return 0;
  1110. err:
  1111. kfree(rx_ring->rx_bi);
  1112. rx_ring->rx_bi = NULL;
  1113. return -ENOMEM;
  1114. }
  1115. /**
  1116. * i40e_release_rx_desc - Store the new tail and head values
  1117. * @rx_ring: ring to bump
  1118. * @val: new head index
  1119. **/
  1120. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  1121. {
  1122. rx_ring->next_to_use = val;
  1123. /* update next to alloc since we have filled the ring */
  1124. rx_ring->next_to_alloc = val;
  1125. /* Force memory writes to complete before letting h/w
  1126. * know there are new descriptors to fetch. (Only
  1127. * applicable for weak-ordered memory model archs,
  1128. * such as IA-64).
  1129. */
  1130. wmb();
  1131. writel(val, rx_ring->tail);
  1132. }
  1133. /**
  1134. * i40e_rx_offset - Return expected offset into page to access data
  1135. * @rx_ring: Ring we are requesting offset of
  1136. *
  1137. * Returns the offset value for ring into the data buffer.
  1138. */
  1139. static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  1140. {
  1141. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  1142. }
  1143. /**
  1144. * i40e_alloc_mapped_page - recycle or make a new page
  1145. * @rx_ring: ring to use
  1146. * @bi: rx_buffer struct to modify
  1147. *
  1148. * Returns true if the page was successfully allocated or
  1149. * reused.
  1150. **/
  1151. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  1152. struct i40e_rx_buffer *bi)
  1153. {
  1154. struct page *page = bi->page;
  1155. dma_addr_t dma;
  1156. /* since we are recycling buffers we should seldom need to alloc */
  1157. if (likely(page)) {
  1158. rx_ring->rx_stats.page_reuse_count++;
  1159. return true;
  1160. }
  1161. /* alloc new page for storage */
  1162. page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
  1163. if (unlikely(!page)) {
  1164. rx_ring->rx_stats.alloc_page_failed++;
  1165. return false;
  1166. }
  1167. /* map page for use */
  1168. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  1169. i40e_rx_pg_size(rx_ring),
  1170. DMA_FROM_DEVICE,
  1171. I40E_RX_DMA_ATTR);
  1172. /* if mapping failed free memory back to system since
  1173. * there isn't much point in holding memory we can't use
  1174. */
  1175. if (dma_mapping_error(rx_ring->dev, dma)) {
  1176. __free_pages(page, i40e_rx_pg_order(rx_ring));
  1177. rx_ring->rx_stats.alloc_page_failed++;
  1178. return false;
  1179. }
  1180. bi->dma = dma;
  1181. bi->page = page;
  1182. bi->page_offset = i40e_rx_offset(rx_ring);
  1183. /* initialize pagecnt_bias to 1 representing we fully own page */
  1184. bi->pagecnt_bias = 1;
  1185. return true;
  1186. }
  1187. /**
  1188. * i40e_receive_skb - Send a completed packet up the stack
  1189. * @rx_ring: rx ring in play
  1190. * @skb: packet to send up
  1191. * @vlan_tag: vlan tag for packet
  1192. **/
  1193. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  1194. struct sk_buff *skb, u16 vlan_tag)
  1195. {
  1196. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  1197. if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1198. (vlan_tag & VLAN_VID_MASK))
  1199. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  1200. napi_gro_receive(&q_vector->napi, skb);
  1201. }
  1202. /**
  1203. * i40e_alloc_rx_buffers - Replace used receive buffers
  1204. * @rx_ring: ring to place buffers on
  1205. * @cleaned_count: number of buffers to replace
  1206. *
  1207. * Returns false if all allocations were successful, true if any fail
  1208. **/
  1209. bool i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  1210. {
  1211. u16 ntu = rx_ring->next_to_use;
  1212. union i40e_rx_desc *rx_desc;
  1213. struct i40e_rx_buffer *bi;
  1214. /* do nothing if no valid netdev defined */
  1215. if (!rx_ring->netdev || !cleaned_count)
  1216. return false;
  1217. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  1218. bi = &rx_ring->rx_bi[ntu];
  1219. do {
  1220. if (!i40e_alloc_mapped_page(rx_ring, bi))
  1221. goto no_buffers;
  1222. /* sync the buffer for use by the device */
  1223. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  1224. bi->page_offset,
  1225. rx_ring->rx_buf_len,
  1226. DMA_FROM_DEVICE);
  1227. /* Refresh the desc even if buffer_addrs didn't change
  1228. * because each write-back erases this info.
  1229. */
  1230. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  1231. rx_desc++;
  1232. bi++;
  1233. ntu++;
  1234. if (unlikely(ntu == rx_ring->count)) {
  1235. rx_desc = I40E_RX_DESC(rx_ring, 0);
  1236. bi = rx_ring->rx_bi;
  1237. ntu = 0;
  1238. }
  1239. /* clear the status bits for the next_to_use descriptor */
  1240. rx_desc->wb.qword1.status_error_len = 0;
  1241. cleaned_count--;
  1242. } while (cleaned_count);
  1243. if (rx_ring->next_to_use != ntu)
  1244. i40e_release_rx_desc(rx_ring, ntu);
  1245. return false;
  1246. no_buffers:
  1247. if (rx_ring->next_to_use != ntu)
  1248. i40e_release_rx_desc(rx_ring, ntu);
  1249. /* make sure to come back via polling to try again after
  1250. * allocation failure
  1251. */
  1252. return true;
  1253. }
  1254. /**
  1255. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  1256. * @vsi: the VSI we care about
  1257. * @skb: skb currently being received and modified
  1258. * @rx_desc: the receive descriptor
  1259. **/
  1260. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  1261. struct sk_buff *skb,
  1262. union i40e_rx_desc *rx_desc)
  1263. {
  1264. struct i40e_rx_ptype_decoded decoded;
  1265. u32 rx_error, rx_status;
  1266. bool ipv4, ipv6;
  1267. u8 ptype;
  1268. u64 qword;
  1269. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1270. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  1271. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1272. I40E_RXD_QW1_ERROR_SHIFT;
  1273. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1274. I40E_RXD_QW1_STATUS_SHIFT;
  1275. decoded = decode_rx_desc_ptype(ptype);
  1276. skb->ip_summed = CHECKSUM_NONE;
  1277. skb_checksum_none_assert(skb);
  1278. /* Rx csum enabled and ip headers found? */
  1279. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  1280. return;
  1281. /* did the hardware decode the packet and checksum? */
  1282. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  1283. return;
  1284. /* both known and outer_ip must be set for the below code to work */
  1285. if (!(decoded.known && decoded.outer_ip))
  1286. return;
  1287. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1288. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  1289. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1290. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  1291. if (ipv4 &&
  1292. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  1293. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  1294. goto checksum_fail;
  1295. /* likely incorrect csum if alternate IP extension headers found */
  1296. if (ipv6 &&
  1297. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  1298. /* don't increment checksum err here, non-fatal err */
  1299. return;
  1300. /* there was some L4 error, count error and punt packet to the stack */
  1301. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  1302. goto checksum_fail;
  1303. /* handle packets that were not able to be checksummed due
  1304. * to arrival speed, in this case the stack can compute
  1305. * the csum.
  1306. */
  1307. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  1308. return;
  1309. /* If there is an outer header present that might contain a checksum
  1310. * we need to bump the checksum level by 1 to reflect the fact that
  1311. * we are indicating we validated the inner checksum.
  1312. */
  1313. if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
  1314. skb->csum_level = 1;
  1315. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  1316. switch (decoded.inner_prot) {
  1317. case I40E_RX_PTYPE_INNER_PROT_TCP:
  1318. case I40E_RX_PTYPE_INNER_PROT_UDP:
  1319. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  1320. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1321. /* fall though */
  1322. default:
  1323. break;
  1324. }
  1325. return;
  1326. checksum_fail:
  1327. vsi->back->hw_csum_rx_error++;
  1328. }
  1329. /**
  1330. * i40e_ptype_to_htype - get a hash type
  1331. * @ptype: the ptype value from the descriptor
  1332. *
  1333. * Returns a hash type to be used by skb_set_hash
  1334. **/
  1335. static inline int i40e_ptype_to_htype(u8 ptype)
  1336. {
  1337. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  1338. if (!decoded.known)
  1339. return PKT_HASH_TYPE_NONE;
  1340. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1341. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  1342. return PKT_HASH_TYPE_L4;
  1343. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1344. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  1345. return PKT_HASH_TYPE_L3;
  1346. else
  1347. return PKT_HASH_TYPE_L2;
  1348. }
  1349. /**
  1350. * i40e_rx_hash - set the hash value in the skb
  1351. * @ring: descriptor ring
  1352. * @rx_desc: specific descriptor
  1353. **/
  1354. static inline void i40e_rx_hash(struct i40e_ring *ring,
  1355. union i40e_rx_desc *rx_desc,
  1356. struct sk_buff *skb,
  1357. u8 rx_ptype)
  1358. {
  1359. u32 hash;
  1360. const __le64 rss_mask =
  1361. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  1362. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  1363. if (!(ring->netdev->features & NETIF_F_RXHASH))
  1364. return;
  1365. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  1366. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  1367. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  1368. }
  1369. }
  1370. /**
  1371. * i40e_process_skb_fields - Populate skb header fields from Rx descriptor
  1372. * @rx_ring: rx descriptor ring packet is being transacted on
  1373. * @rx_desc: pointer to the EOP Rx descriptor
  1374. * @skb: pointer to current skb being populated
  1375. * @rx_ptype: the packet type decoded by hardware
  1376. *
  1377. * This function checks the ring, descriptor, and packet information in
  1378. * order to populate the hash, checksum, VLAN, protocol, and
  1379. * other fields within the skb.
  1380. **/
  1381. static inline
  1382. void i40e_process_skb_fields(struct i40e_ring *rx_ring,
  1383. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  1384. u8 rx_ptype)
  1385. {
  1386. u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1387. u32 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1388. I40E_RXD_QW1_STATUS_SHIFT;
  1389. u32 tsynvalid = rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK;
  1390. u32 tsyn = (rx_status & I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
  1391. I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT;
  1392. if (unlikely(tsynvalid))
  1393. i40e_ptp_rx_hwtstamp(rx_ring->vsi->back, skb, tsyn);
  1394. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1395. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  1396. skb_record_rx_queue(skb, rx_ring->queue_index);
  1397. /* modifies the skb - consumes the enet header */
  1398. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1399. }
  1400. /**
  1401. * i40e_cleanup_headers - Correct empty headers
  1402. * @rx_ring: rx descriptor ring packet is being transacted on
  1403. * @skb: pointer to current skb being fixed
  1404. * @rx_desc: pointer to the EOP Rx descriptor
  1405. *
  1406. * Also address the case where we are pulling data in on pages only
  1407. * and as such no data is present in the skb header.
  1408. *
  1409. * In addition if skb is not at least 60 bytes we need to pad it so that
  1410. * it is large enough to qualify as a valid Ethernet frame.
  1411. *
  1412. * Returns true if an error was encountered and skb was freed.
  1413. **/
  1414. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb,
  1415. union i40e_rx_desc *rx_desc)
  1416. {
  1417. /* XDP packets use error pointer so abort at this point */
  1418. if (IS_ERR(skb))
  1419. return true;
  1420. /* ERR_MASK will only have valid bits if EOP set, and
  1421. * what we are doing here is actually checking
  1422. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1423. * the error field
  1424. */
  1425. if (unlikely(i40e_test_staterr(rx_desc,
  1426. BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1427. dev_kfree_skb_any(skb);
  1428. return true;
  1429. }
  1430. /* if eth_skb_pad returns an error the skb was freed */
  1431. if (eth_skb_pad(skb))
  1432. return true;
  1433. return false;
  1434. }
  1435. /**
  1436. * i40e_page_is_reusable - check if any reuse is possible
  1437. * @page: page struct to check
  1438. *
  1439. * A page is not reusable if it was allocated under low memory
  1440. * conditions, or it's not in the same NUMA node as this CPU.
  1441. */
  1442. static inline bool i40e_page_is_reusable(struct page *page)
  1443. {
  1444. return (page_to_nid(page) == numa_mem_id()) &&
  1445. !page_is_pfmemalloc(page);
  1446. }
  1447. /**
  1448. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  1449. * the adapter for another receive
  1450. *
  1451. * @rx_buffer: buffer containing the page
  1452. *
  1453. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  1454. * an unused region in the page.
  1455. *
  1456. * For small pages, @truesize will be a constant value, half the size
  1457. * of the memory at page. We'll attempt to alternate between high and
  1458. * low halves of the page, with one half ready for use by the hardware
  1459. * and the other half being consumed by the stack. We use the page
  1460. * ref count to determine whether the stack has finished consuming the
  1461. * portion of this page that was passed up with a previous packet. If
  1462. * the page ref count is >1, we'll assume the "other" half page is
  1463. * still busy, and this page cannot be reused.
  1464. *
  1465. * For larger pages, @truesize will be the actual space used by the
  1466. * received packet (adjusted upward to an even multiple of the cache
  1467. * line size). This will advance through the page by the amount
  1468. * actually consumed by the received packets while there is still
  1469. * space for a buffer. Each region of larger pages will be used at
  1470. * most once, after which the page will not be reused.
  1471. *
  1472. * In either case, if the page is reusable its refcount is increased.
  1473. **/
  1474. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  1475. {
  1476. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  1477. struct page *page = rx_buffer->page;
  1478. /* Is any reuse possible? */
  1479. if (unlikely(!i40e_page_is_reusable(page)))
  1480. return false;
  1481. #if (PAGE_SIZE < 8192)
  1482. /* if we are only owner of page we can reuse it */
  1483. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  1484. return false;
  1485. #else
  1486. #define I40E_LAST_OFFSET \
  1487. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  1488. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  1489. return false;
  1490. #endif
  1491. /* If we have drained the page fragment pool we need to update
  1492. * the pagecnt_bias and page count so that we fully restock the
  1493. * number of references the driver holds.
  1494. */
  1495. if (unlikely(!pagecnt_bias)) {
  1496. page_ref_add(page, USHRT_MAX);
  1497. rx_buffer->pagecnt_bias = USHRT_MAX;
  1498. }
  1499. return true;
  1500. }
  1501. /**
  1502. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  1503. * @rx_ring: rx descriptor ring to transact packets on
  1504. * @rx_buffer: buffer containing page to add
  1505. * @skb: sk_buff to place the data into
  1506. * @size: packet length from rx_desc
  1507. *
  1508. * This function will add the data contained in rx_buffer->page to the skb.
  1509. * It will just attach the page as a frag to the skb.
  1510. *
  1511. * The function will then update the page offset.
  1512. **/
  1513. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  1514. struct i40e_rx_buffer *rx_buffer,
  1515. struct sk_buff *skb,
  1516. unsigned int size)
  1517. {
  1518. #if (PAGE_SIZE < 8192)
  1519. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1520. #else
  1521. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  1522. #endif
  1523. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  1524. rx_buffer->page_offset, size, truesize);
  1525. /* page is being used so we must update the page offset */
  1526. #if (PAGE_SIZE < 8192)
  1527. rx_buffer->page_offset ^= truesize;
  1528. #else
  1529. rx_buffer->page_offset += truesize;
  1530. #endif
  1531. }
  1532. /**
  1533. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  1534. * @rx_ring: rx descriptor ring to transact packets on
  1535. * @size: size of buffer to add to skb
  1536. *
  1537. * This function will pull an Rx buffer from the ring and synchronize it
  1538. * for use by the CPU.
  1539. */
  1540. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  1541. const unsigned int size)
  1542. {
  1543. struct i40e_rx_buffer *rx_buffer;
  1544. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  1545. prefetchw(rx_buffer->page);
  1546. /* we are reusing so sync this buffer for CPU use */
  1547. dma_sync_single_range_for_cpu(rx_ring->dev,
  1548. rx_buffer->dma,
  1549. rx_buffer->page_offset,
  1550. size,
  1551. DMA_FROM_DEVICE);
  1552. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  1553. rx_buffer->pagecnt_bias--;
  1554. return rx_buffer;
  1555. }
  1556. /**
  1557. * i40e_construct_skb - Allocate skb and populate it
  1558. * @rx_ring: rx descriptor ring to transact packets on
  1559. * @rx_buffer: rx buffer to pull data from
  1560. * @xdp: xdp_buff pointing to the data
  1561. *
  1562. * This function allocates an skb. It then populates it with the page
  1563. * data from the current receive descriptor, taking care to set up the
  1564. * skb correctly.
  1565. */
  1566. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  1567. struct i40e_rx_buffer *rx_buffer,
  1568. struct xdp_buff *xdp)
  1569. {
  1570. unsigned int size = xdp->data_end - xdp->data;
  1571. #if (PAGE_SIZE < 8192)
  1572. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1573. #else
  1574. unsigned int truesize = SKB_DATA_ALIGN(size);
  1575. #endif
  1576. unsigned int headlen;
  1577. struct sk_buff *skb;
  1578. /* prefetch first cache line of first page */
  1579. prefetch(xdp->data);
  1580. #if L1_CACHE_BYTES < 128
  1581. prefetch(xdp->data + L1_CACHE_BYTES);
  1582. #endif
  1583. /* allocate a skb to store the frags */
  1584. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  1585. I40E_RX_HDR_SIZE,
  1586. GFP_ATOMIC | __GFP_NOWARN);
  1587. if (unlikely(!skb))
  1588. return NULL;
  1589. /* Determine available headroom for copy */
  1590. headlen = size;
  1591. if (headlen > I40E_RX_HDR_SIZE)
  1592. headlen = eth_get_headlen(xdp->data, I40E_RX_HDR_SIZE);
  1593. /* align pull length to size of long to optimize memcpy performance */
  1594. memcpy(__skb_put(skb, headlen), xdp->data,
  1595. ALIGN(headlen, sizeof(long)));
  1596. /* update all of the pointers */
  1597. size -= headlen;
  1598. if (size) {
  1599. skb_add_rx_frag(skb, 0, rx_buffer->page,
  1600. rx_buffer->page_offset + headlen,
  1601. size, truesize);
  1602. /* buffer is used by skb, update page_offset */
  1603. #if (PAGE_SIZE < 8192)
  1604. rx_buffer->page_offset ^= truesize;
  1605. #else
  1606. rx_buffer->page_offset += truesize;
  1607. #endif
  1608. } else {
  1609. /* buffer is unused, reset bias back to rx_buffer */
  1610. rx_buffer->pagecnt_bias++;
  1611. }
  1612. return skb;
  1613. }
  1614. /**
  1615. * i40e_build_skb - Build skb around an existing buffer
  1616. * @rx_ring: Rx descriptor ring to transact packets on
  1617. * @rx_buffer: Rx buffer to pull data from
  1618. * @xdp: xdp_buff pointing to the data
  1619. *
  1620. * This function builds an skb around an existing Rx buffer, taking care
  1621. * to set up the skb correctly and avoid any memcpy overhead.
  1622. */
  1623. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1624. struct i40e_rx_buffer *rx_buffer,
  1625. struct xdp_buff *xdp)
  1626. {
  1627. unsigned int size = xdp->data_end - xdp->data;
  1628. #if (PAGE_SIZE < 8192)
  1629. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1630. #else
  1631. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  1632. SKB_DATA_ALIGN(I40E_SKB_PAD + size);
  1633. #endif
  1634. struct sk_buff *skb;
  1635. /* prefetch first cache line of first page */
  1636. prefetch(xdp->data);
  1637. #if L1_CACHE_BYTES < 128
  1638. prefetch(xdp->data + L1_CACHE_BYTES);
  1639. #endif
  1640. /* build an skb around the page buffer */
  1641. skb = build_skb(xdp->data_hard_start, truesize);
  1642. if (unlikely(!skb))
  1643. return NULL;
  1644. /* update pointers within the skb to store the data */
  1645. skb_reserve(skb, I40E_SKB_PAD);
  1646. __skb_put(skb, size);
  1647. /* buffer is used by skb, update page_offset */
  1648. #if (PAGE_SIZE < 8192)
  1649. rx_buffer->page_offset ^= truesize;
  1650. #else
  1651. rx_buffer->page_offset += truesize;
  1652. #endif
  1653. return skb;
  1654. }
  1655. /**
  1656. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1657. * @rx_ring: rx descriptor ring to transact packets on
  1658. * @rx_buffer: rx buffer to pull data from
  1659. *
  1660. * This function will clean up the contents of the rx_buffer. It will
  1661. * either recycle the bufer or unmap it and free the associated resources.
  1662. */
  1663. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1664. struct i40e_rx_buffer *rx_buffer)
  1665. {
  1666. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1667. /* hand second half of page back to the ring */
  1668. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1669. rx_ring->rx_stats.page_reuse_count++;
  1670. } else {
  1671. /* we are not reusing the buffer so unmap it */
  1672. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1673. i40e_rx_pg_size(rx_ring),
  1674. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1675. __page_frag_cache_drain(rx_buffer->page,
  1676. rx_buffer->pagecnt_bias);
  1677. }
  1678. /* clear contents of buffer_info */
  1679. rx_buffer->page = NULL;
  1680. }
  1681. /**
  1682. * i40e_is_non_eop - process handling of non-EOP buffers
  1683. * @rx_ring: Rx ring being processed
  1684. * @rx_desc: Rx descriptor for current buffer
  1685. * @skb: Current socket buffer containing buffer in progress
  1686. *
  1687. * This function updates next to clean. If the buffer is an EOP buffer
  1688. * this function exits returning false, otherwise it will place the
  1689. * sk_buff in the next buffer to be chained and return true indicating
  1690. * that this is in fact a non-EOP buffer.
  1691. **/
  1692. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1693. union i40e_rx_desc *rx_desc,
  1694. struct sk_buff *skb)
  1695. {
  1696. u32 ntc = rx_ring->next_to_clean + 1;
  1697. /* fetch, update, and store next to clean */
  1698. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1699. rx_ring->next_to_clean = ntc;
  1700. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1701. /* if we are the last buffer then there is nothing else to do */
  1702. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1703. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1704. return false;
  1705. rx_ring->rx_stats.non_eop_descs++;
  1706. return true;
  1707. }
  1708. #define I40E_XDP_PASS 0
  1709. #define I40E_XDP_CONSUMED 1
  1710. #define I40E_XDP_TX 2
  1711. static int i40e_xmit_xdp_ring(struct xdp_buff *xdp,
  1712. struct i40e_ring *xdp_ring);
  1713. /**
  1714. * i40e_run_xdp - run an XDP program
  1715. * @rx_ring: Rx ring being processed
  1716. * @xdp: XDP buffer containing the frame
  1717. **/
  1718. static struct sk_buff *i40e_run_xdp(struct i40e_ring *rx_ring,
  1719. struct xdp_buff *xdp)
  1720. {
  1721. int result = I40E_XDP_PASS;
  1722. struct i40e_ring *xdp_ring;
  1723. struct bpf_prog *xdp_prog;
  1724. u32 act;
  1725. rcu_read_lock();
  1726. xdp_prog = READ_ONCE(rx_ring->xdp_prog);
  1727. if (!xdp_prog)
  1728. goto xdp_out;
  1729. act = bpf_prog_run_xdp(xdp_prog, xdp);
  1730. switch (act) {
  1731. case XDP_PASS:
  1732. break;
  1733. case XDP_TX:
  1734. xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  1735. result = i40e_xmit_xdp_ring(xdp, xdp_ring);
  1736. break;
  1737. default:
  1738. bpf_warn_invalid_xdp_action(act);
  1739. case XDP_ABORTED:
  1740. trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
  1741. /* fallthrough -- handle aborts by dropping packet */
  1742. case XDP_DROP:
  1743. result = I40E_XDP_CONSUMED;
  1744. break;
  1745. }
  1746. xdp_out:
  1747. rcu_read_unlock();
  1748. return ERR_PTR(-result);
  1749. }
  1750. /**
  1751. * i40e_rx_buffer_flip - adjusted rx_buffer to point to an unused region
  1752. * @rx_ring: Rx ring
  1753. * @rx_buffer: Rx buffer to adjust
  1754. * @size: Size of adjustment
  1755. **/
  1756. static void i40e_rx_buffer_flip(struct i40e_ring *rx_ring,
  1757. struct i40e_rx_buffer *rx_buffer,
  1758. unsigned int size)
  1759. {
  1760. #if (PAGE_SIZE < 8192)
  1761. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1762. rx_buffer->page_offset ^= truesize;
  1763. #else
  1764. unsigned int truesize = SKB_DATA_ALIGN(i40e_rx_offset(rx_ring) + size);
  1765. rx_buffer->page_offset += truesize;
  1766. #endif
  1767. }
  1768. /**
  1769. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1770. * @rx_ring: rx descriptor ring to transact packets on
  1771. * @budget: Total limit on number of packets to process
  1772. *
  1773. * This function provides a "bounce buffer" approach to Rx interrupt
  1774. * processing. The advantage to this is that on systems that have
  1775. * expensive overhead for IOMMU access this provides a means of avoiding
  1776. * it by maintaining the mapping of the page to the system.
  1777. *
  1778. * Returns amount of work completed
  1779. **/
  1780. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1781. {
  1782. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1783. struct sk_buff *skb = rx_ring->skb;
  1784. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1785. bool failure = false, xdp_xmit = false;
  1786. while (likely(total_rx_packets < (unsigned int)budget)) {
  1787. struct i40e_rx_buffer *rx_buffer;
  1788. union i40e_rx_desc *rx_desc;
  1789. struct xdp_buff xdp;
  1790. unsigned int size;
  1791. u16 vlan_tag;
  1792. u8 rx_ptype;
  1793. u64 qword;
  1794. /* return some buffers to hardware, one at a time is too slow */
  1795. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1796. failure = failure ||
  1797. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  1798. cleaned_count = 0;
  1799. }
  1800. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1801. /* status_error_len will always be zero for unused descriptors
  1802. * because it's cleared in cleanup, and overlaps with hdr_addr
  1803. * which is always zero because packet split isn't used, if the
  1804. * hardware wrote DD then the length will be non-zero
  1805. */
  1806. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1807. /* This memory barrier is needed to keep us from reading
  1808. * any other fields out of the rx_desc until we have
  1809. * verified the descriptor has been written back.
  1810. */
  1811. dma_rmb();
  1812. if (unlikely(i40e_rx_is_programming_status(qword))) {
  1813. i40e_clean_programming_status(rx_ring, rx_desc, qword);
  1814. cleaned_count++;
  1815. continue;
  1816. }
  1817. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1818. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1819. if (!size)
  1820. break;
  1821. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  1822. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  1823. /* retrieve a buffer from the ring */
  1824. if (!skb) {
  1825. xdp.data = page_address(rx_buffer->page) +
  1826. rx_buffer->page_offset;
  1827. xdp_set_data_meta_invalid(&xdp);
  1828. xdp.data_hard_start = xdp.data -
  1829. i40e_rx_offset(rx_ring);
  1830. xdp.data_end = xdp.data + size;
  1831. skb = i40e_run_xdp(rx_ring, &xdp);
  1832. }
  1833. if (IS_ERR(skb)) {
  1834. if (PTR_ERR(skb) == -I40E_XDP_TX) {
  1835. xdp_xmit = true;
  1836. i40e_rx_buffer_flip(rx_ring, rx_buffer, size);
  1837. } else {
  1838. rx_buffer->pagecnt_bias++;
  1839. }
  1840. total_rx_bytes += size;
  1841. total_rx_packets++;
  1842. } else if (skb) {
  1843. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  1844. } else if (ring_uses_build_skb(rx_ring)) {
  1845. skb = i40e_build_skb(rx_ring, rx_buffer, &xdp);
  1846. } else {
  1847. skb = i40e_construct_skb(rx_ring, rx_buffer, &xdp);
  1848. }
  1849. /* exit if we failed to retrieve a buffer */
  1850. if (!skb) {
  1851. rx_ring->rx_stats.alloc_buff_failed++;
  1852. rx_buffer->pagecnt_bias++;
  1853. break;
  1854. }
  1855. i40e_put_rx_buffer(rx_ring, rx_buffer);
  1856. cleaned_count++;
  1857. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  1858. continue;
  1859. if (i40e_cleanup_headers(rx_ring, skb, rx_desc)) {
  1860. skb = NULL;
  1861. continue;
  1862. }
  1863. /* probably a little skewed due to removing CRC */
  1864. total_rx_bytes += skb->len;
  1865. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1866. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1867. I40E_RXD_QW1_PTYPE_SHIFT;
  1868. /* populate checksum, VLAN, and protocol */
  1869. i40e_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  1870. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  1871. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  1872. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  1873. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1874. skb = NULL;
  1875. /* update budget accounting */
  1876. total_rx_packets++;
  1877. }
  1878. if (xdp_xmit) {
  1879. struct i40e_ring *xdp_ring;
  1880. xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  1881. /* Force memory writes to complete before letting h/w
  1882. * know there are new descriptors to fetch.
  1883. */
  1884. wmb();
  1885. writel(xdp_ring->next_to_use, xdp_ring->tail);
  1886. }
  1887. rx_ring->skb = skb;
  1888. u64_stats_update_begin(&rx_ring->syncp);
  1889. rx_ring->stats.packets += total_rx_packets;
  1890. rx_ring->stats.bytes += total_rx_bytes;
  1891. u64_stats_update_end(&rx_ring->syncp);
  1892. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1893. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1894. /* guarantee a trip back through this routine if there was a failure */
  1895. return failure ? budget : (int)total_rx_packets;
  1896. }
  1897. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1898. {
  1899. u32 val;
  1900. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  1901. I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
  1902. (type << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
  1903. (itr << I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT);
  1904. return val;
  1905. }
  1906. /* a small macro to shorten up some long lines */
  1907. #define INTREG I40E_PFINT_DYN_CTLN
  1908. static inline int get_rx_itr(struct i40e_vsi *vsi, int idx)
  1909. {
  1910. return vsi->rx_rings[idx]->rx_itr_setting;
  1911. }
  1912. static inline int get_tx_itr(struct i40e_vsi *vsi, int idx)
  1913. {
  1914. return vsi->tx_rings[idx]->tx_itr_setting;
  1915. }
  1916. /**
  1917. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1918. * @vsi: the VSI we care about
  1919. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1920. *
  1921. **/
  1922. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1923. struct i40e_q_vector *q_vector)
  1924. {
  1925. struct i40e_hw *hw = &vsi->back->hw;
  1926. bool rx = false, tx = false;
  1927. u32 rxval, txval;
  1928. int vector;
  1929. int idx = q_vector->v_idx;
  1930. int rx_itr_setting, tx_itr_setting;
  1931. /* If we don't have MSIX, then we only need to re-enable icr0 */
  1932. if (!(vsi->back->flags & I40E_FLAG_MSIX_ENABLED)) {
  1933. i40e_irq_dynamic_enable_icr0(vsi->back);
  1934. return;
  1935. }
  1936. vector = (q_vector->v_idx + vsi->base_vector);
  1937. /* avoid dynamic calculation if in countdown mode OR if
  1938. * all dynamic is disabled
  1939. */
  1940. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1941. rx_itr_setting = get_rx_itr(vsi, idx);
  1942. tx_itr_setting = get_tx_itr(vsi, idx);
  1943. if (q_vector->itr_countdown > 0 ||
  1944. (!ITR_IS_DYNAMIC(rx_itr_setting) &&
  1945. !ITR_IS_DYNAMIC(tx_itr_setting))) {
  1946. goto enable_int;
  1947. }
  1948. if (ITR_IS_DYNAMIC(rx_itr_setting)) {
  1949. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1950. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1951. }
  1952. if (ITR_IS_DYNAMIC(tx_itr_setting)) {
  1953. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1954. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1955. }
  1956. if (rx || tx) {
  1957. /* get the higher of the two ITR adjustments and
  1958. * use the same value for both ITR registers
  1959. * when in adaptive mode (Rx and/or Tx)
  1960. */
  1961. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1962. q_vector->tx.itr = q_vector->rx.itr = itr;
  1963. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1964. tx = true;
  1965. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1966. rx = true;
  1967. }
  1968. /* only need to enable the interrupt once, but need
  1969. * to possibly update both ITR values
  1970. */
  1971. if (rx) {
  1972. /* set the INTENA_MSK_MASK so that this first write
  1973. * won't actually enable the interrupt, instead just
  1974. * updating the ITR (it's bit 31 PF and VF)
  1975. */
  1976. rxval |= BIT(31);
  1977. /* don't check _DOWN because interrupt isn't being enabled */
  1978. wr32(hw, INTREG(vector - 1), rxval);
  1979. }
  1980. enable_int:
  1981. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  1982. wr32(hw, INTREG(vector - 1), txval);
  1983. if (q_vector->itr_countdown)
  1984. q_vector->itr_countdown--;
  1985. else
  1986. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1987. }
  1988. /**
  1989. * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
  1990. * @napi: napi struct with our devices info in it
  1991. * @budget: amount of work driver is allowed to do this pass, in packets
  1992. *
  1993. * This function will clean all queues associated with a q_vector.
  1994. *
  1995. * Returns the amount of work done
  1996. **/
  1997. int i40e_napi_poll(struct napi_struct *napi, int budget)
  1998. {
  1999. struct i40e_q_vector *q_vector =
  2000. container_of(napi, struct i40e_q_vector, napi);
  2001. struct i40e_vsi *vsi = q_vector->vsi;
  2002. struct i40e_ring *ring;
  2003. bool clean_complete = true;
  2004. bool arm_wb = false;
  2005. int budget_per_ring;
  2006. int work_done = 0;
  2007. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  2008. napi_complete(napi);
  2009. return 0;
  2010. }
  2011. /* Since the actual Tx work is minimal, we can give the Tx a larger
  2012. * budget and be more aggressive about cleaning up the Tx descriptors.
  2013. */
  2014. i40e_for_each_ring(ring, q_vector->tx) {
  2015. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  2016. clean_complete = false;
  2017. continue;
  2018. }
  2019. arm_wb |= ring->arm_wb;
  2020. ring->arm_wb = false;
  2021. }
  2022. /* Handle case where we are called by netpoll with a budget of 0 */
  2023. if (budget <= 0)
  2024. goto tx_only;
  2025. /* We attempt to distribute budget to each Rx queue fairly, but don't
  2026. * allow the budget to go below 1 because that would exit polling early.
  2027. */
  2028. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  2029. i40e_for_each_ring(ring, q_vector->rx) {
  2030. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  2031. work_done += cleaned;
  2032. /* if we clean as many as budgeted, we must not be done */
  2033. if (cleaned >= budget_per_ring)
  2034. clean_complete = false;
  2035. }
  2036. /* If work not completed, return budget and polling will return */
  2037. if (!clean_complete) {
  2038. int cpu_id = smp_processor_id();
  2039. /* It is possible that the interrupt affinity has changed but,
  2040. * if the cpu is pegged at 100%, polling will never exit while
  2041. * traffic continues and the interrupt will be stuck on this
  2042. * cpu. We check to make sure affinity is correct before we
  2043. * continue to poll, otherwise we must stop polling so the
  2044. * interrupt can move to the correct cpu.
  2045. */
  2046. if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
  2047. /* Tell napi that we are done polling */
  2048. napi_complete_done(napi, work_done);
  2049. /* Force an interrupt */
  2050. i40e_force_wb(vsi, q_vector);
  2051. /* Return budget-1 so that polling stops */
  2052. return budget - 1;
  2053. }
  2054. tx_only:
  2055. if (arm_wb) {
  2056. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  2057. i40e_enable_wb_on_itr(vsi, q_vector);
  2058. }
  2059. return budget;
  2060. }
  2061. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  2062. q_vector->arm_wb_state = false;
  2063. /* Work is done so exit the polling mode and re-enable the interrupt */
  2064. napi_complete_done(napi, work_done);
  2065. i40e_update_enable_itr(vsi, q_vector);
  2066. return min(work_done, budget - 1);
  2067. }
  2068. /**
  2069. * i40e_atr - Add a Flow Director ATR filter
  2070. * @tx_ring: ring to add programming descriptor to
  2071. * @skb: send buffer
  2072. * @tx_flags: send tx flags
  2073. **/
  2074. static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2075. u32 tx_flags)
  2076. {
  2077. struct i40e_filter_program_desc *fdir_desc;
  2078. struct i40e_pf *pf = tx_ring->vsi->back;
  2079. union {
  2080. unsigned char *network;
  2081. struct iphdr *ipv4;
  2082. struct ipv6hdr *ipv6;
  2083. } hdr;
  2084. struct tcphdr *th;
  2085. unsigned int hlen;
  2086. u32 flex_ptype, dtype_cmd;
  2087. int l4_proto;
  2088. u16 i;
  2089. /* make sure ATR is enabled */
  2090. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  2091. return;
  2092. if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED)
  2093. return;
  2094. /* if sampling is disabled do nothing */
  2095. if (!tx_ring->atr_sample_rate)
  2096. return;
  2097. /* Currently only IPv4/IPv6 with TCP is supported */
  2098. if (!(tx_flags & (I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6)))
  2099. return;
  2100. /* snag network header to get L4 type and address */
  2101. hdr.network = (tx_flags & I40E_TX_FLAGS_UDP_TUNNEL) ?
  2102. skb_inner_network_header(skb) : skb_network_header(skb);
  2103. /* Note: tx_flags gets modified to reflect inner protocols in
  2104. * tx_enable_csum function if encap is enabled.
  2105. */
  2106. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  2107. /* access ihl as u8 to avoid unaligned access on ia64 */
  2108. hlen = (hdr.network[0] & 0x0F) << 2;
  2109. l4_proto = hdr.ipv4->protocol;
  2110. } else {
  2111. /* find the start of the innermost ipv6 header */
  2112. unsigned int inner_hlen = hdr.network - skb->data;
  2113. unsigned int h_offset = inner_hlen;
  2114. /* this function updates h_offset to the end of the header */
  2115. l4_proto =
  2116. ipv6_find_hdr(skb, &h_offset, IPPROTO_TCP, NULL, NULL);
  2117. /* hlen will contain our best estimate of the tcp header */
  2118. hlen = h_offset - inner_hlen;
  2119. }
  2120. if (l4_proto != IPPROTO_TCP)
  2121. return;
  2122. th = (struct tcphdr *)(hdr.network + hlen);
  2123. /* Due to lack of space, no more new filters can be programmed */
  2124. if (th->syn && (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
  2125. return;
  2126. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) {
  2127. /* HW ATR eviction will take care of removing filters on FIN
  2128. * and RST packets.
  2129. */
  2130. if (th->fin || th->rst)
  2131. return;
  2132. }
  2133. tx_ring->atr_count++;
  2134. /* sample on all syn/fin/rst packets or once every atr sample rate */
  2135. if (!th->fin &&
  2136. !th->syn &&
  2137. !th->rst &&
  2138. (tx_ring->atr_count < tx_ring->atr_sample_rate))
  2139. return;
  2140. tx_ring->atr_count = 0;
  2141. /* grab the next descriptor */
  2142. i = tx_ring->next_to_use;
  2143. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  2144. i++;
  2145. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2146. flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  2147. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  2148. flex_ptype |= (tx_flags & I40E_TX_FLAGS_IPV4) ?
  2149. (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
  2150. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
  2151. (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
  2152. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  2153. flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  2154. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  2155. dtype_cmd |= (th->fin || th->rst) ?
  2156. (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  2157. I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
  2158. (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  2159. I40E_TXD_FLTR_QW1_PCMD_SHIFT);
  2160. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
  2161. I40E_TXD_FLTR_QW1_DEST_SHIFT;
  2162. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
  2163. I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
  2164. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  2165. if (!(tx_flags & I40E_TX_FLAGS_UDP_TUNNEL))
  2166. dtype_cmd |=
  2167. ((u32)I40E_FD_ATR_STAT_IDX(pf->hw.pf_id) <<
  2168. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2169. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2170. else
  2171. dtype_cmd |=
  2172. ((u32)I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id) <<
  2173. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2174. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2175. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED)
  2176. dtype_cmd |= I40E_TXD_FLTR_QW1_ATR_MASK;
  2177. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  2178. fdir_desc->rsvd = cpu_to_le32(0);
  2179. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  2180. fdir_desc->fd_id = cpu_to_le32(0);
  2181. }
  2182. /**
  2183. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  2184. * @skb: send buffer
  2185. * @tx_ring: ring to send buffer on
  2186. * @flags: the tx flags to be set
  2187. *
  2188. * Checks the skb and set up correspondingly several generic transmit flags
  2189. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  2190. *
  2191. * Returns error code indicate the frame should be dropped upon error and the
  2192. * otherwise returns 0 to indicate the flags has been set properly.
  2193. **/
  2194. static inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  2195. struct i40e_ring *tx_ring,
  2196. u32 *flags)
  2197. {
  2198. __be16 protocol = skb->protocol;
  2199. u32 tx_flags = 0;
  2200. if (protocol == htons(ETH_P_8021Q) &&
  2201. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  2202. /* When HW VLAN acceleration is turned off by the user the
  2203. * stack sets the protocol to 8021q so that the driver
  2204. * can take any steps required to support the SW only
  2205. * VLAN handling. In our case the driver doesn't need
  2206. * to take any further steps so just set the protocol
  2207. * to the encapsulated ethertype.
  2208. */
  2209. skb->protocol = vlan_get_protocol(skb);
  2210. goto out;
  2211. }
  2212. /* if we have a HW VLAN tag being added, default to the HW one */
  2213. if (skb_vlan_tag_present(skb)) {
  2214. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  2215. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2216. /* else if it is a SW VLAN, check the next protocol and store the tag */
  2217. } else if (protocol == htons(ETH_P_8021Q)) {
  2218. struct vlan_hdr *vhdr, _vhdr;
  2219. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  2220. if (!vhdr)
  2221. return -EINVAL;
  2222. protocol = vhdr->h_vlan_encapsulated_proto;
  2223. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  2224. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  2225. }
  2226. if (!(tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2227. goto out;
  2228. /* Insert 802.1p priority into VLAN header */
  2229. if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
  2230. (skb->priority != TC_PRIO_CONTROL)) {
  2231. tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
  2232. tx_flags |= (skb->priority & 0x7) <<
  2233. I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
  2234. if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
  2235. struct vlan_ethhdr *vhdr;
  2236. int rc;
  2237. rc = skb_cow_head(skb, 0);
  2238. if (rc < 0)
  2239. return rc;
  2240. vhdr = (struct vlan_ethhdr *)skb->data;
  2241. vhdr->h_vlan_TCI = htons(tx_flags >>
  2242. I40E_TX_FLAGS_VLAN_SHIFT);
  2243. } else {
  2244. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2245. }
  2246. }
  2247. out:
  2248. *flags = tx_flags;
  2249. return 0;
  2250. }
  2251. /**
  2252. * i40e_tso - set up the tso context descriptor
  2253. * @first: pointer to first Tx buffer for xmit
  2254. * @hdr_len: ptr to the size of the packet header
  2255. * @cd_type_cmd_tso_mss: Quad Word 1
  2256. *
  2257. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  2258. **/
  2259. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  2260. u64 *cd_type_cmd_tso_mss)
  2261. {
  2262. struct sk_buff *skb = first->skb;
  2263. u64 cd_cmd, cd_tso_len, cd_mss;
  2264. union {
  2265. struct iphdr *v4;
  2266. struct ipv6hdr *v6;
  2267. unsigned char *hdr;
  2268. } ip;
  2269. union {
  2270. struct tcphdr *tcp;
  2271. struct udphdr *udp;
  2272. unsigned char *hdr;
  2273. } l4;
  2274. u32 paylen, l4_offset;
  2275. u16 gso_segs, gso_size;
  2276. int err;
  2277. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2278. return 0;
  2279. if (!skb_is_gso(skb))
  2280. return 0;
  2281. err = skb_cow_head(skb, 0);
  2282. if (err < 0)
  2283. return err;
  2284. ip.hdr = skb_network_header(skb);
  2285. l4.hdr = skb_transport_header(skb);
  2286. /* initialize outer IP header fields */
  2287. if (ip.v4->version == 4) {
  2288. ip.v4->tot_len = 0;
  2289. ip.v4->check = 0;
  2290. } else {
  2291. ip.v6->payload_len = 0;
  2292. }
  2293. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  2294. SKB_GSO_GRE_CSUM |
  2295. SKB_GSO_IPXIP4 |
  2296. SKB_GSO_IPXIP6 |
  2297. SKB_GSO_UDP_TUNNEL |
  2298. SKB_GSO_UDP_TUNNEL_CSUM)) {
  2299. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2300. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  2301. l4.udp->len = 0;
  2302. /* determine offset of outer transport header */
  2303. l4_offset = l4.hdr - skb->data;
  2304. /* remove payload length from outer checksum */
  2305. paylen = skb->len - l4_offset;
  2306. csum_replace_by_diff(&l4.udp->check,
  2307. (__force __wsum)htonl(paylen));
  2308. }
  2309. /* reset pointers to inner headers */
  2310. ip.hdr = skb_inner_network_header(skb);
  2311. l4.hdr = skb_inner_transport_header(skb);
  2312. /* initialize inner IP header fields */
  2313. if (ip.v4->version == 4) {
  2314. ip.v4->tot_len = 0;
  2315. ip.v4->check = 0;
  2316. } else {
  2317. ip.v6->payload_len = 0;
  2318. }
  2319. }
  2320. /* determine offset of inner transport header */
  2321. l4_offset = l4.hdr - skb->data;
  2322. /* remove payload length from inner checksum */
  2323. paylen = skb->len - l4_offset;
  2324. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  2325. /* compute length of segmentation header */
  2326. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  2327. /* pull values out of skb_shinfo */
  2328. gso_size = skb_shinfo(skb)->gso_size;
  2329. gso_segs = skb_shinfo(skb)->gso_segs;
  2330. /* update GSO size and bytecount with header size */
  2331. first->gso_segs = gso_segs;
  2332. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  2333. /* find the field values */
  2334. cd_cmd = I40E_TX_CTX_DESC_TSO;
  2335. cd_tso_len = skb->len - *hdr_len;
  2336. cd_mss = gso_size;
  2337. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  2338. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  2339. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  2340. return 1;
  2341. }
  2342. /**
  2343. * i40e_tsyn - set up the tsyn context descriptor
  2344. * @tx_ring: ptr to the ring to send
  2345. * @skb: ptr to the skb we're sending
  2346. * @tx_flags: the collected send information
  2347. * @cd_type_cmd_tso_mss: Quad Word 1
  2348. *
  2349. * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
  2350. **/
  2351. static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2352. u32 tx_flags, u64 *cd_type_cmd_tso_mss)
  2353. {
  2354. struct i40e_pf *pf;
  2355. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  2356. return 0;
  2357. /* Tx timestamps cannot be sampled when doing TSO */
  2358. if (tx_flags & I40E_TX_FLAGS_TSO)
  2359. return 0;
  2360. /* only timestamp the outbound packet if the user has requested it and
  2361. * we are not already transmitting a packet to be timestamped
  2362. */
  2363. pf = i40e_netdev_to_pf(tx_ring->netdev);
  2364. if (!(pf->flags & I40E_FLAG_PTP))
  2365. return 0;
  2366. if (pf->ptp_tx &&
  2367. !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, pf->state)) {
  2368. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2369. pf->ptp_tx_start = jiffies;
  2370. pf->ptp_tx_skb = skb_get(skb);
  2371. } else {
  2372. pf->tx_hwtstamp_skipped++;
  2373. return 0;
  2374. }
  2375. *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
  2376. I40E_TXD_CTX_QW1_CMD_SHIFT;
  2377. return 1;
  2378. }
  2379. /**
  2380. * i40e_tx_enable_csum - Enable Tx checksum offloads
  2381. * @skb: send buffer
  2382. * @tx_flags: pointer to Tx flags currently set
  2383. * @td_cmd: Tx descriptor command bits to set
  2384. * @td_offset: Tx descriptor header offsets to set
  2385. * @tx_ring: Tx descriptor ring
  2386. * @cd_tunneling: ptr to context desc bits
  2387. **/
  2388. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  2389. u32 *td_cmd, u32 *td_offset,
  2390. struct i40e_ring *tx_ring,
  2391. u32 *cd_tunneling)
  2392. {
  2393. union {
  2394. struct iphdr *v4;
  2395. struct ipv6hdr *v6;
  2396. unsigned char *hdr;
  2397. } ip;
  2398. union {
  2399. struct tcphdr *tcp;
  2400. struct udphdr *udp;
  2401. unsigned char *hdr;
  2402. } l4;
  2403. unsigned char *exthdr;
  2404. u32 offset, cmd = 0;
  2405. __be16 frag_off;
  2406. u8 l4_proto = 0;
  2407. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2408. return 0;
  2409. ip.hdr = skb_network_header(skb);
  2410. l4.hdr = skb_transport_header(skb);
  2411. /* compute outer L2 header size */
  2412. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  2413. if (skb->encapsulation) {
  2414. u32 tunnel = 0;
  2415. /* define outer network header type */
  2416. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2417. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2418. I40E_TX_CTX_EXT_IP_IPV4 :
  2419. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  2420. l4_proto = ip.v4->protocol;
  2421. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2422. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  2423. exthdr = ip.hdr + sizeof(*ip.v6);
  2424. l4_proto = ip.v6->nexthdr;
  2425. if (l4.hdr != exthdr)
  2426. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2427. &l4_proto, &frag_off);
  2428. }
  2429. /* define outer transport */
  2430. switch (l4_proto) {
  2431. case IPPROTO_UDP:
  2432. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  2433. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2434. break;
  2435. case IPPROTO_GRE:
  2436. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  2437. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2438. break;
  2439. case IPPROTO_IPIP:
  2440. case IPPROTO_IPV6:
  2441. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2442. l4.hdr = skb_inner_network_header(skb);
  2443. break;
  2444. default:
  2445. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2446. return -1;
  2447. skb_checksum_help(skb);
  2448. return 0;
  2449. }
  2450. /* compute outer L3 header size */
  2451. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  2452. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  2453. /* switch IP header pointer from outer to inner header */
  2454. ip.hdr = skb_inner_network_header(skb);
  2455. /* compute tunnel header size */
  2456. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  2457. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  2458. /* indicate if we need to offload outer UDP header */
  2459. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  2460. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2461. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  2462. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  2463. /* record tunnel offload values */
  2464. *cd_tunneling |= tunnel;
  2465. /* switch L4 header pointer from outer to inner */
  2466. l4.hdr = skb_inner_transport_header(skb);
  2467. l4_proto = 0;
  2468. /* reset type as we transition from outer to inner headers */
  2469. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  2470. if (ip.v4->version == 4)
  2471. *tx_flags |= I40E_TX_FLAGS_IPV4;
  2472. if (ip.v6->version == 6)
  2473. *tx_flags |= I40E_TX_FLAGS_IPV6;
  2474. }
  2475. /* Enable IP checksum offloads */
  2476. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2477. l4_proto = ip.v4->protocol;
  2478. /* the stack computes the IP header already, the only time we
  2479. * need the hardware to recompute it is in the case of TSO.
  2480. */
  2481. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2482. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  2483. I40E_TX_DESC_CMD_IIPT_IPV4;
  2484. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2485. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  2486. exthdr = ip.hdr + sizeof(*ip.v6);
  2487. l4_proto = ip.v6->nexthdr;
  2488. if (l4.hdr != exthdr)
  2489. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2490. &l4_proto, &frag_off);
  2491. }
  2492. /* compute inner L3 header size */
  2493. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  2494. /* Enable L4 checksum offloads */
  2495. switch (l4_proto) {
  2496. case IPPROTO_TCP:
  2497. /* enable checksum offloads */
  2498. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  2499. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2500. break;
  2501. case IPPROTO_SCTP:
  2502. /* enable SCTP checksum offload */
  2503. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  2504. offset |= (sizeof(struct sctphdr) >> 2) <<
  2505. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2506. break;
  2507. case IPPROTO_UDP:
  2508. /* enable UDP checksum offload */
  2509. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  2510. offset |= (sizeof(struct udphdr) >> 2) <<
  2511. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2512. break;
  2513. default:
  2514. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2515. return -1;
  2516. skb_checksum_help(skb);
  2517. return 0;
  2518. }
  2519. *td_cmd |= cmd;
  2520. *td_offset |= offset;
  2521. return 1;
  2522. }
  2523. /**
  2524. * i40e_create_tx_ctx Build the Tx context descriptor
  2525. * @tx_ring: ring to create the descriptor on
  2526. * @cd_type_cmd_tso_mss: Quad Word 1
  2527. * @cd_tunneling: Quad Word 0 - bits 0-31
  2528. * @cd_l2tag2: Quad Word 0 - bits 32-63
  2529. **/
  2530. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  2531. const u64 cd_type_cmd_tso_mss,
  2532. const u32 cd_tunneling, const u32 cd_l2tag2)
  2533. {
  2534. struct i40e_tx_context_desc *context_desc;
  2535. int i = tx_ring->next_to_use;
  2536. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  2537. !cd_tunneling && !cd_l2tag2)
  2538. return;
  2539. /* grab the next descriptor */
  2540. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  2541. i++;
  2542. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2543. /* cpu_to_le32 and assign to struct fields */
  2544. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  2545. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  2546. context_desc->rsvd = cpu_to_le16(0);
  2547. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  2548. }
  2549. /**
  2550. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  2551. * @tx_ring: the ring to be checked
  2552. * @size: the size buffer we want to assure is available
  2553. *
  2554. * Returns -EBUSY if a stop is needed, else 0
  2555. **/
  2556. int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  2557. {
  2558. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2559. /* Memory barrier before checking head and tail */
  2560. smp_mb();
  2561. /* Check again in a case another CPU has just made room available. */
  2562. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  2563. return -EBUSY;
  2564. /* A reprieve! - use start_queue because it doesn't call schedule */
  2565. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2566. ++tx_ring->tx_stats.restart_queue;
  2567. return 0;
  2568. }
  2569. /**
  2570. * __i40e_chk_linearize - Check if there are more than 8 buffers per packet
  2571. * @skb: send buffer
  2572. *
  2573. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  2574. * and so we need to figure out the cases where we need to linearize the skb.
  2575. *
  2576. * For TSO we need to count the TSO header and segment payload separately.
  2577. * As such we need to check cases where we have 7 fragments or more as we
  2578. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  2579. * the segment payload in the first descriptor, and another 7 for the
  2580. * fragments.
  2581. **/
  2582. bool __i40e_chk_linearize(struct sk_buff *skb)
  2583. {
  2584. const struct skb_frag_struct *frag, *stale;
  2585. int nr_frags, sum;
  2586. /* no need to check if number of frags is less than 7 */
  2587. nr_frags = skb_shinfo(skb)->nr_frags;
  2588. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  2589. return false;
  2590. /* We need to walk through the list and validate that each group
  2591. * of 6 fragments totals at least gso_size.
  2592. */
  2593. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  2594. frag = &skb_shinfo(skb)->frags[0];
  2595. /* Initialize size to the negative value of gso_size minus 1. We
  2596. * use this as the worst case scenerio in which the frag ahead
  2597. * of us only provides one byte which is why we are limited to 6
  2598. * descriptors for a single transmit as the header and previous
  2599. * fragment are already consuming 2 descriptors.
  2600. */
  2601. sum = 1 - skb_shinfo(skb)->gso_size;
  2602. /* Add size of frags 0 through 4 to create our initial sum */
  2603. sum += skb_frag_size(frag++);
  2604. sum += skb_frag_size(frag++);
  2605. sum += skb_frag_size(frag++);
  2606. sum += skb_frag_size(frag++);
  2607. sum += skb_frag_size(frag++);
  2608. /* Walk through fragments adding latest fragment, testing it, and
  2609. * then removing stale fragments from the sum.
  2610. */
  2611. stale = &skb_shinfo(skb)->frags[0];
  2612. for (;;) {
  2613. sum += skb_frag_size(frag++);
  2614. /* if sum is negative we failed to make sufficient progress */
  2615. if (sum < 0)
  2616. return true;
  2617. if (!nr_frags--)
  2618. break;
  2619. sum -= skb_frag_size(stale++);
  2620. }
  2621. return false;
  2622. }
  2623. /**
  2624. * i40e_tx_map - Build the Tx descriptor
  2625. * @tx_ring: ring to send buffer on
  2626. * @skb: send buffer
  2627. * @first: first buffer info buffer to use
  2628. * @tx_flags: collected send information
  2629. * @hdr_len: size of the packet header
  2630. * @td_cmd: the command field in the descriptor
  2631. * @td_offset: offset for checksum or crc
  2632. *
  2633. * Returns 0 on success, -1 on failure to DMA
  2634. **/
  2635. static inline int i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2636. struct i40e_tx_buffer *first, u32 tx_flags,
  2637. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  2638. {
  2639. unsigned int data_len = skb->data_len;
  2640. unsigned int size = skb_headlen(skb);
  2641. struct skb_frag_struct *frag;
  2642. struct i40e_tx_buffer *tx_bi;
  2643. struct i40e_tx_desc *tx_desc;
  2644. u16 i = tx_ring->next_to_use;
  2645. u32 td_tag = 0;
  2646. dma_addr_t dma;
  2647. u16 desc_count = 1;
  2648. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  2649. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  2650. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  2651. I40E_TX_FLAGS_VLAN_SHIFT;
  2652. }
  2653. first->tx_flags = tx_flags;
  2654. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  2655. tx_desc = I40E_TX_DESC(tx_ring, i);
  2656. tx_bi = first;
  2657. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  2658. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2659. if (dma_mapping_error(tx_ring->dev, dma))
  2660. goto dma_error;
  2661. /* record length, and DMA address */
  2662. dma_unmap_len_set(tx_bi, len, size);
  2663. dma_unmap_addr_set(tx_bi, dma, dma);
  2664. /* align size to end of page */
  2665. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  2666. tx_desc->buffer_addr = cpu_to_le64(dma);
  2667. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  2668. tx_desc->cmd_type_offset_bsz =
  2669. build_ctob(td_cmd, td_offset,
  2670. max_data, td_tag);
  2671. tx_desc++;
  2672. i++;
  2673. desc_count++;
  2674. if (i == tx_ring->count) {
  2675. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2676. i = 0;
  2677. }
  2678. dma += max_data;
  2679. size -= max_data;
  2680. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2681. tx_desc->buffer_addr = cpu_to_le64(dma);
  2682. }
  2683. if (likely(!data_len))
  2684. break;
  2685. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  2686. size, td_tag);
  2687. tx_desc++;
  2688. i++;
  2689. desc_count++;
  2690. if (i == tx_ring->count) {
  2691. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2692. i = 0;
  2693. }
  2694. size = skb_frag_size(frag);
  2695. data_len -= size;
  2696. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2697. DMA_TO_DEVICE);
  2698. tx_bi = &tx_ring->tx_bi[i];
  2699. }
  2700. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  2701. i++;
  2702. if (i == tx_ring->count)
  2703. i = 0;
  2704. tx_ring->next_to_use = i;
  2705. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2706. /* write last descriptor with EOP bit */
  2707. td_cmd |= I40E_TX_DESC_CMD_EOP;
  2708. /* We OR these values together to check both against 4 (WB_STRIDE)
  2709. * below. This is safe since we don't re-use desc_count afterwards.
  2710. */
  2711. desc_count |= ++tx_ring->packet_stride;
  2712. if (desc_count >= WB_STRIDE) {
  2713. /* write last descriptor with RS bit set */
  2714. td_cmd |= I40E_TX_DESC_CMD_RS;
  2715. tx_ring->packet_stride = 0;
  2716. }
  2717. tx_desc->cmd_type_offset_bsz =
  2718. build_ctob(td_cmd, td_offset, size, td_tag);
  2719. /* Force memory writes to complete before letting h/w know there
  2720. * are new descriptors to fetch.
  2721. *
  2722. * We also use this memory barrier to make certain all of the
  2723. * status bits have been updated before next_to_watch is written.
  2724. */
  2725. wmb();
  2726. /* set next_to_watch value indicating a packet is present */
  2727. first->next_to_watch = tx_desc;
  2728. /* notify HW of packet */
  2729. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  2730. writel(i, tx_ring->tail);
  2731. /* we need this if more than one processor can write to our tail
  2732. * at a time, it synchronizes IO on IA64/Altix systems
  2733. */
  2734. mmiowb();
  2735. }
  2736. return 0;
  2737. dma_error:
  2738. dev_info(tx_ring->dev, "TX DMA map failed\n");
  2739. /* clear dma mappings for failed tx_bi map */
  2740. for (;;) {
  2741. tx_bi = &tx_ring->tx_bi[i];
  2742. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  2743. if (tx_bi == first)
  2744. break;
  2745. if (i == 0)
  2746. i = tx_ring->count;
  2747. i--;
  2748. }
  2749. tx_ring->next_to_use = i;
  2750. return -1;
  2751. }
  2752. /**
  2753. * i40e_xmit_xdp_ring - transmits an XDP buffer to an XDP Tx ring
  2754. * @xdp: data to transmit
  2755. * @xdp_ring: XDP Tx ring
  2756. **/
  2757. static int i40e_xmit_xdp_ring(struct xdp_buff *xdp,
  2758. struct i40e_ring *xdp_ring)
  2759. {
  2760. u32 size = xdp->data_end - xdp->data;
  2761. u16 i = xdp_ring->next_to_use;
  2762. struct i40e_tx_buffer *tx_bi;
  2763. struct i40e_tx_desc *tx_desc;
  2764. dma_addr_t dma;
  2765. if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) {
  2766. xdp_ring->tx_stats.tx_busy++;
  2767. return I40E_XDP_CONSUMED;
  2768. }
  2769. dma = dma_map_single(xdp_ring->dev, xdp->data, size, DMA_TO_DEVICE);
  2770. if (dma_mapping_error(xdp_ring->dev, dma))
  2771. return I40E_XDP_CONSUMED;
  2772. tx_bi = &xdp_ring->tx_bi[i];
  2773. tx_bi->bytecount = size;
  2774. tx_bi->gso_segs = 1;
  2775. tx_bi->raw_buf = xdp->data;
  2776. /* record length, and DMA address */
  2777. dma_unmap_len_set(tx_bi, len, size);
  2778. dma_unmap_addr_set(tx_bi, dma, dma);
  2779. tx_desc = I40E_TX_DESC(xdp_ring, i);
  2780. tx_desc->buffer_addr = cpu_to_le64(dma);
  2781. tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC
  2782. | I40E_TXD_CMD,
  2783. 0, size, 0);
  2784. /* Make certain all of the status bits have been updated
  2785. * before next_to_watch is written.
  2786. */
  2787. smp_wmb();
  2788. i++;
  2789. if (i == xdp_ring->count)
  2790. i = 0;
  2791. tx_bi->next_to_watch = tx_desc;
  2792. xdp_ring->next_to_use = i;
  2793. return I40E_XDP_TX;
  2794. }
  2795. /**
  2796. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  2797. * @skb: send buffer
  2798. * @tx_ring: ring to send buffer on
  2799. *
  2800. * Returns NETDEV_TX_OK if sent, else an error code
  2801. **/
  2802. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  2803. struct i40e_ring *tx_ring)
  2804. {
  2805. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  2806. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  2807. struct i40e_tx_buffer *first;
  2808. u32 td_offset = 0;
  2809. u32 tx_flags = 0;
  2810. __be16 protocol;
  2811. u32 td_cmd = 0;
  2812. u8 hdr_len = 0;
  2813. int tso, count;
  2814. int tsyn;
  2815. /* prefetch the data, we'll need it later */
  2816. prefetch(skb->data);
  2817. i40e_trace(xmit_frame_ring, skb, tx_ring);
  2818. count = i40e_xmit_descriptor_count(skb);
  2819. if (i40e_chk_linearize(skb, count)) {
  2820. if (__skb_linearize(skb)) {
  2821. dev_kfree_skb_any(skb);
  2822. return NETDEV_TX_OK;
  2823. }
  2824. count = i40e_txd_use_count(skb->len);
  2825. tx_ring->tx_stats.tx_linearize++;
  2826. }
  2827. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  2828. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  2829. * + 4 desc gap to avoid the cache line where head is,
  2830. * + 1 desc for context descriptor,
  2831. * otherwise try next time
  2832. */
  2833. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  2834. tx_ring->tx_stats.tx_busy++;
  2835. return NETDEV_TX_BUSY;
  2836. }
  2837. /* record the location of the first descriptor for this packet */
  2838. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  2839. first->skb = skb;
  2840. first->bytecount = skb->len;
  2841. first->gso_segs = 1;
  2842. /* prepare the xmit flags */
  2843. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  2844. goto out_drop;
  2845. /* obtain protocol of skb */
  2846. protocol = vlan_get_protocol(skb);
  2847. /* setup IPv4/IPv6 offloads */
  2848. if (protocol == htons(ETH_P_IP))
  2849. tx_flags |= I40E_TX_FLAGS_IPV4;
  2850. else if (protocol == htons(ETH_P_IPV6))
  2851. tx_flags |= I40E_TX_FLAGS_IPV6;
  2852. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  2853. if (tso < 0)
  2854. goto out_drop;
  2855. else if (tso)
  2856. tx_flags |= I40E_TX_FLAGS_TSO;
  2857. /* Always offload the checksum, since it's in the data descriptor */
  2858. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  2859. tx_ring, &cd_tunneling);
  2860. if (tso < 0)
  2861. goto out_drop;
  2862. tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
  2863. if (tsyn)
  2864. tx_flags |= I40E_TX_FLAGS_TSYN;
  2865. skb_tx_timestamp(skb);
  2866. /* always enable CRC insertion offload */
  2867. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  2868. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  2869. cd_tunneling, cd_l2tag2);
  2870. /* Add Flow Director ATR if it's enabled.
  2871. *
  2872. * NOTE: this must always be directly before the data descriptor.
  2873. */
  2874. i40e_atr(tx_ring, skb, tx_flags);
  2875. if (i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  2876. td_cmd, td_offset))
  2877. goto cleanup_tx_tstamp;
  2878. return NETDEV_TX_OK;
  2879. out_drop:
  2880. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  2881. dev_kfree_skb_any(first->skb);
  2882. first->skb = NULL;
  2883. cleanup_tx_tstamp:
  2884. if (unlikely(tx_flags & I40E_TX_FLAGS_TSYN)) {
  2885. struct i40e_pf *pf = i40e_netdev_to_pf(tx_ring->netdev);
  2886. dev_kfree_skb_any(pf->ptp_tx_skb);
  2887. pf->ptp_tx_skb = NULL;
  2888. clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
  2889. }
  2890. return NETDEV_TX_OK;
  2891. }
  2892. /**
  2893. * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  2894. * @skb: send buffer
  2895. * @netdev: network interface device structure
  2896. *
  2897. * Returns NETDEV_TX_OK if sent, else an error code
  2898. **/
  2899. netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2900. {
  2901. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2902. struct i40e_vsi *vsi = np->vsi;
  2903. struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
  2904. /* hardware can't handle really short frames, hardware padding works
  2905. * beyond this point
  2906. */
  2907. if (skb_put_padto(skb, I40E_MIN_TX_LEN))
  2908. return NETDEV_TX_OK;
  2909. return i40e_xmit_frame_ring(skb, tx_ring);
  2910. }