i40e_txrx.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  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. /* Hardware only fetches new descriptors in cache lines of 8,
  1215. * essentially ignoring the lower 3 bits of the tail register. We want
  1216. * to ensure our tail writes are aligned to avoid unnecessary work. We
  1217. * can't simply round down the cleaned count, since we might fail to
  1218. * allocate some buffers. What we really want is to ensure that
  1219. * next_to_used + cleaned_count produces an aligned value.
  1220. */
  1221. cleaned_count -= (ntu + cleaned_count) & 0x7;
  1222. /* do nothing if no valid netdev defined */
  1223. if (!rx_ring->netdev || !cleaned_count)
  1224. return false;
  1225. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  1226. bi = &rx_ring->rx_bi[ntu];
  1227. do {
  1228. if (!i40e_alloc_mapped_page(rx_ring, bi))
  1229. goto no_buffers;
  1230. /* sync the buffer for use by the device */
  1231. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  1232. bi->page_offset,
  1233. rx_ring->rx_buf_len,
  1234. DMA_FROM_DEVICE);
  1235. /* Refresh the desc even if buffer_addrs didn't change
  1236. * because each write-back erases this info.
  1237. */
  1238. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  1239. rx_desc++;
  1240. bi++;
  1241. ntu++;
  1242. if (unlikely(ntu == rx_ring->count)) {
  1243. rx_desc = I40E_RX_DESC(rx_ring, 0);
  1244. bi = rx_ring->rx_bi;
  1245. ntu = 0;
  1246. }
  1247. /* clear the status bits for the next_to_use descriptor */
  1248. rx_desc->wb.qword1.status_error_len = 0;
  1249. cleaned_count--;
  1250. } while (cleaned_count);
  1251. if (rx_ring->next_to_use != ntu)
  1252. i40e_release_rx_desc(rx_ring, ntu);
  1253. return false;
  1254. no_buffers:
  1255. if (rx_ring->next_to_use != ntu)
  1256. i40e_release_rx_desc(rx_ring, ntu);
  1257. /* make sure to come back via polling to try again after
  1258. * allocation failure
  1259. */
  1260. return true;
  1261. }
  1262. /**
  1263. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  1264. * @vsi: the VSI we care about
  1265. * @skb: skb currently being received and modified
  1266. * @rx_desc: the receive descriptor
  1267. **/
  1268. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  1269. struct sk_buff *skb,
  1270. union i40e_rx_desc *rx_desc)
  1271. {
  1272. struct i40e_rx_ptype_decoded decoded;
  1273. u32 rx_error, rx_status;
  1274. bool ipv4, ipv6;
  1275. u8 ptype;
  1276. u64 qword;
  1277. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1278. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  1279. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1280. I40E_RXD_QW1_ERROR_SHIFT;
  1281. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1282. I40E_RXD_QW1_STATUS_SHIFT;
  1283. decoded = decode_rx_desc_ptype(ptype);
  1284. skb->ip_summed = CHECKSUM_NONE;
  1285. skb_checksum_none_assert(skb);
  1286. /* Rx csum enabled and ip headers found? */
  1287. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  1288. return;
  1289. /* did the hardware decode the packet and checksum? */
  1290. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  1291. return;
  1292. /* both known and outer_ip must be set for the below code to work */
  1293. if (!(decoded.known && decoded.outer_ip))
  1294. return;
  1295. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1296. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  1297. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1298. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  1299. if (ipv4 &&
  1300. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  1301. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  1302. goto checksum_fail;
  1303. /* likely incorrect csum if alternate IP extension headers found */
  1304. if (ipv6 &&
  1305. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  1306. /* don't increment checksum err here, non-fatal err */
  1307. return;
  1308. /* there was some L4 error, count error and punt packet to the stack */
  1309. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  1310. goto checksum_fail;
  1311. /* handle packets that were not able to be checksummed due
  1312. * to arrival speed, in this case the stack can compute
  1313. * the csum.
  1314. */
  1315. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  1316. return;
  1317. /* If there is an outer header present that might contain a checksum
  1318. * we need to bump the checksum level by 1 to reflect the fact that
  1319. * we are indicating we validated the inner checksum.
  1320. */
  1321. if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
  1322. skb->csum_level = 1;
  1323. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  1324. switch (decoded.inner_prot) {
  1325. case I40E_RX_PTYPE_INNER_PROT_TCP:
  1326. case I40E_RX_PTYPE_INNER_PROT_UDP:
  1327. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  1328. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1329. /* fall though */
  1330. default:
  1331. break;
  1332. }
  1333. return;
  1334. checksum_fail:
  1335. vsi->back->hw_csum_rx_error++;
  1336. }
  1337. /**
  1338. * i40e_ptype_to_htype - get a hash type
  1339. * @ptype: the ptype value from the descriptor
  1340. *
  1341. * Returns a hash type to be used by skb_set_hash
  1342. **/
  1343. static inline int i40e_ptype_to_htype(u8 ptype)
  1344. {
  1345. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  1346. if (!decoded.known)
  1347. return PKT_HASH_TYPE_NONE;
  1348. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1349. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  1350. return PKT_HASH_TYPE_L4;
  1351. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1352. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  1353. return PKT_HASH_TYPE_L3;
  1354. else
  1355. return PKT_HASH_TYPE_L2;
  1356. }
  1357. /**
  1358. * i40e_rx_hash - set the hash value in the skb
  1359. * @ring: descriptor ring
  1360. * @rx_desc: specific descriptor
  1361. **/
  1362. static inline void i40e_rx_hash(struct i40e_ring *ring,
  1363. union i40e_rx_desc *rx_desc,
  1364. struct sk_buff *skb,
  1365. u8 rx_ptype)
  1366. {
  1367. u32 hash;
  1368. const __le64 rss_mask =
  1369. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  1370. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  1371. if (!(ring->netdev->features & NETIF_F_RXHASH))
  1372. return;
  1373. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  1374. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  1375. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  1376. }
  1377. }
  1378. /**
  1379. * i40e_process_skb_fields - Populate skb header fields from Rx descriptor
  1380. * @rx_ring: rx descriptor ring packet is being transacted on
  1381. * @rx_desc: pointer to the EOP Rx descriptor
  1382. * @skb: pointer to current skb being populated
  1383. * @rx_ptype: the packet type decoded by hardware
  1384. *
  1385. * This function checks the ring, descriptor, and packet information in
  1386. * order to populate the hash, checksum, VLAN, protocol, and
  1387. * other fields within the skb.
  1388. **/
  1389. static inline
  1390. void i40e_process_skb_fields(struct i40e_ring *rx_ring,
  1391. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  1392. u8 rx_ptype)
  1393. {
  1394. u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1395. u32 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1396. I40E_RXD_QW1_STATUS_SHIFT;
  1397. u32 tsynvalid = rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK;
  1398. u32 tsyn = (rx_status & I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
  1399. I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT;
  1400. if (unlikely(tsynvalid))
  1401. i40e_ptp_rx_hwtstamp(rx_ring->vsi->back, skb, tsyn);
  1402. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1403. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  1404. skb_record_rx_queue(skb, rx_ring->queue_index);
  1405. /* modifies the skb - consumes the enet header */
  1406. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1407. }
  1408. /**
  1409. * i40e_cleanup_headers - Correct empty headers
  1410. * @rx_ring: rx descriptor ring packet is being transacted on
  1411. * @skb: pointer to current skb being fixed
  1412. * @rx_desc: pointer to the EOP Rx descriptor
  1413. *
  1414. * Also address the case where we are pulling data in on pages only
  1415. * and as such no data is present in the skb header.
  1416. *
  1417. * In addition if skb is not at least 60 bytes we need to pad it so that
  1418. * it is large enough to qualify as a valid Ethernet frame.
  1419. *
  1420. * Returns true if an error was encountered and skb was freed.
  1421. **/
  1422. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb,
  1423. union i40e_rx_desc *rx_desc)
  1424. {
  1425. /* XDP packets use error pointer so abort at this point */
  1426. if (IS_ERR(skb))
  1427. return true;
  1428. /* ERR_MASK will only have valid bits if EOP set, and
  1429. * what we are doing here is actually checking
  1430. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1431. * the error field
  1432. */
  1433. if (unlikely(i40e_test_staterr(rx_desc,
  1434. BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1435. dev_kfree_skb_any(skb);
  1436. return true;
  1437. }
  1438. /* if eth_skb_pad returns an error the skb was freed */
  1439. if (eth_skb_pad(skb))
  1440. return true;
  1441. return false;
  1442. }
  1443. /**
  1444. * i40e_page_is_reusable - check if any reuse is possible
  1445. * @page: page struct to check
  1446. *
  1447. * A page is not reusable if it was allocated under low memory
  1448. * conditions, or it's not in the same NUMA node as this CPU.
  1449. */
  1450. static inline bool i40e_page_is_reusable(struct page *page)
  1451. {
  1452. return (page_to_nid(page) == numa_mem_id()) &&
  1453. !page_is_pfmemalloc(page);
  1454. }
  1455. /**
  1456. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  1457. * the adapter for another receive
  1458. *
  1459. * @rx_buffer: buffer containing the page
  1460. *
  1461. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  1462. * an unused region in the page.
  1463. *
  1464. * For small pages, @truesize will be a constant value, half the size
  1465. * of the memory at page. We'll attempt to alternate between high and
  1466. * low halves of the page, with one half ready for use by the hardware
  1467. * and the other half being consumed by the stack. We use the page
  1468. * ref count to determine whether the stack has finished consuming the
  1469. * portion of this page that was passed up with a previous packet. If
  1470. * the page ref count is >1, we'll assume the "other" half page is
  1471. * still busy, and this page cannot be reused.
  1472. *
  1473. * For larger pages, @truesize will be the actual space used by the
  1474. * received packet (adjusted upward to an even multiple of the cache
  1475. * line size). This will advance through the page by the amount
  1476. * actually consumed by the received packets while there is still
  1477. * space for a buffer. Each region of larger pages will be used at
  1478. * most once, after which the page will not be reused.
  1479. *
  1480. * In either case, if the page is reusable its refcount is increased.
  1481. **/
  1482. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  1483. {
  1484. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  1485. struct page *page = rx_buffer->page;
  1486. /* Is any reuse possible? */
  1487. if (unlikely(!i40e_page_is_reusable(page)))
  1488. return false;
  1489. #if (PAGE_SIZE < 8192)
  1490. /* if we are only owner of page we can reuse it */
  1491. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  1492. return false;
  1493. #else
  1494. #define I40E_LAST_OFFSET \
  1495. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  1496. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  1497. return false;
  1498. #endif
  1499. /* If we have drained the page fragment pool we need to update
  1500. * the pagecnt_bias and page count so that we fully restock the
  1501. * number of references the driver holds.
  1502. */
  1503. if (unlikely(!pagecnt_bias)) {
  1504. page_ref_add(page, USHRT_MAX);
  1505. rx_buffer->pagecnt_bias = USHRT_MAX;
  1506. }
  1507. return true;
  1508. }
  1509. /**
  1510. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  1511. * @rx_ring: rx descriptor ring to transact packets on
  1512. * @rx_buffer: buffer containing page to add
  1513. * @skb: sk_buff to place the data into
  1514. * @size: packet length from rx_desc
  1515. *
  1516. * This function will add the data contained in rx_buffer->page to the skb.
  1517. * It will just attach the page as a frag to the skb.
  1518. *
  1519. * The function will then update the page offset.
  1520. **/
  1521. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  1522. struct i40e_rx_buffer *rx_buffer,
  1523. struct sk_buff *skb,
  1524. unsigned int size)
  1525. {
  1526. #if (PAGE_SIZE < 8192)
  1527. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1528. #else
  1529. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  1530. #endif
  1531. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  1532. rx_buffer->page_offset, size, truesize);
  1533. /* page is being used so we must update the page offset */
  1534. #if (PAGE_SIZE < 8192)
  1535. rx_buffer->page_offset ^= truesize;
  1536. #else
  1537. rx_buffer->page_offset += truesize;
  1538. #endif
  1539. }
  1540. /**
  1541. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  1542. * @rx_ring: rx descriptor ring to transact packets on
  1543. * @size: size of buffer to add to skb
  1544. *
  1545. * This function will pull an Rx buffer from the ring and synchronize it
  1546. * for use by the CPU.
  1547. */
  1548. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  1549. const unsigned int size)
  1550. {
  1551. struct i40e_rx_buffer *rx_buffer;
  1552. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  1553. prefetchw(rx_buffer->page);
  1554. /* we are reusing so sync this buffer for CPU use */
  1555. dma_sync_single_range_for_cpu(rx_ring->dev,
  1556. rx_buffer->dma,
  1557. rx_buffer->page_offset,
  1558. size,
  1559. DMA_FROM_DEVICE);
  1560. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  1561. rx_buffer->pagecnt_bias--;
  1562. return rx_buffer;
  1563. }
  1564. /**
  1565. * i40e_construct_skb - Allocate skb and populate it
  1566. * @rx_ring: rx descriptor ring to transact packets on
  1567. * @rx_buffer: rx buffer to pull data from
  1568. * @xdp: xdp_buff pointing to the data
  1569. *
  1570. * This function allocates an skb. It then populates it with the page
  1571. * data from the current receive descriptor, taking care to set up the
  1572. * skb correctly.
  1573. */
  1574. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  1575. struct i40e_rx_buffer *rx_buffer,
  1576. struct xdp_buff *xdp)
  1577. {
  1578. unsigned int size = xdp->data_end - xdp->data;
  1579. #if (PAGE_SIZE < 8192)
  1580. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1581. #else
  1582. unsigned int truesize = SKB_DATA_ALIGN(size);
  1583. #endif
  1584. unsigned int headlen;
  1585. struct sk_buff *skb;
  1586. /* prefetch first cache line of first page */
  1587. prefetch(xdp->data);
  1588. #if L1_CACHE_BYTES < 128
  1589. prefetch(xdp->data + L1_CACHE_BYTES);
  1590. #endif
  1591. /* allocate a skb to store the frags */
  1592. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  1593. I40E_RX_HDR_SIZE,
  1594. GFP_ATOMIC | __GFP_NOWARN);
  1595. if (unlikely(!skb))
  1596. return NULL;
  1597. /* Determine available headroom for copy */
  1598. headlen = size;
  1599. if (headlen > I40E_RX_HDR_SIZE)
  1600. headlen = eth_get_headlen(xdp->data, I40E_RX_HDR_SIZE);
  1601. /* align pull length to size of long to optimize memcpy performance */
  1602. memcpy(__skb_put(skb, headlen), xdp->data,
  1603. ALIGN(headlen, sizeof(long)));
  1604. /* update all of the pointers */
  1605. size -= headlen;
  1606. if (size) {
  1607. skb_add_rx_frag(skb, 0, rx_buffer->page,
  1608. rx_buffer->page_offset + headlen,
  1609. size, truesize);
  1610. /* buffer is used by skb, update page_offset */
  1611. #if (PAGE_SIZE < 8192)
  1612. rx_buffer->page_offset ^= truesize;
  1613. #else
  1614. rx_buffer->page_offset += truesize;
  1615. #endif
  1616. } else {
  1617. /* buffer is unused, reset bias back to rx_buffer */
  1618. rx_buffer->pagecnt_bias++;
  1619. }
  1620. return skb;
  1621. }
  1622. /**
  1623. * i40e_build_skb - Build skb around an existing buffer
  1624. * @rx_ring: Rx descriptor ring to transact packets on
  1625. * @rx_buffer: Rx buffer to pull data from
  1626. * @xdp: xdp_buff pointing to the data
  1627. *
  1628. * This function builds an skb around an existing Rx buffer, taking care
  1629. * to set up the skb correctly and avoid any memcpy overhead.
  1630. */
  1631. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1632. struct i40e_rx_buffer *rx_buffer,
  1633. struct xdp_buff *xdp)
  1634. {
  1635. unsigned int size = xdp->data_end - xdp->data;
  1636. #if (PAGE_SIZE < 8192)
  1637. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1638. #else
  1639. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  1640. SKB_DATA_ALIGN(I40E_SKB_PAD + size);
  1641. #endif
  1642. struct sk_buff *skb;
  1643. /* prefetch first cache line of first page */
  1644. prefetch(xdp->data);
  1645. #if L1_CACHE_BYTES < 128
  1646. prefetch(xdp->data + L1_CACHE_BYTES);
  1647. #endif
  1648. /* build an skb around the page buffer */
  1649. skb = build_skb(xdp->data_hard_start, truesize);
  1650. if (unlikely(!skb))
  1651. return NULL;
  1652. /* update pointers within the skb to store the data */
  1653. skb_reserve(skb, I40E_SKB_PAD);
  1654. __skb_put(skb, size);
  1655. /* buffer is used by skb, update page_offset */
  1656. #if (PAGE_SIZE < 8192)
  1657. rx_buffer->page_offset ^= truesize;
  1658. #else
  1659. rx_buffer->page_offset += truesize;
  1660. #endif
  1661. return skb;
  1662. }
  1663. /**
  1664. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1665. * @rx_ring: rx descriptor ring to transact packets on
  1666. * @rx_buffer: rx buffer to pull data from
  1667. *
  1668. * This function will clean up the contents of the rx_buffer. It will
  1669. * either recycle the bufer or unmap it and free the associated resources.
  1670. */
  1671. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1672. struct i40e_rx_buffer *rx_buffer)
  1673. {
  1674. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1675. /* hand second half of page back to the ring */
  1676. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1677. rx_ring->rx_stats.page_reuse_count++;
  1678. } else {
  1679. /* we are not reusing the buffer so unmap it */
  1680. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1681. i40e_rx_pg_size(rx_ring),
  1682. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1683. __page_frag_cache_drain(rx_buffer->page,
  1684. rx_buffer->pagecnt_bias);
  1685. }
  1686. /* clear contents of buffer_info */
  1687. rx_buffer->page = NULL;
  1688. }
  1689. /**
  1690. * i40e_is_non_eop - process handling of non-EOP buffers
  1691. * @rx_ring: Rx ring being processed
  1692. * @rx_desc: Rx descriptor for current buffer
  1693. * @skb: Current socket buffer containing buffer in progress
  1694. *
  1695. * This function updates next to clean. If the buffer is an EOP buffer
  1696. * this function exits returning false, otherwise it will place the
  1697. * sk_buff in the next buffer to be chained and return true indicating
  1698. * that this is in fact a non-EOP buffer.
  1699. **/
  1700. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1701. union i40e_rx_desc *rx_desc,
  1702. struct sk_buff *skb)
  1703. {
  1704. u32 ntc = rx_ring->next_to_clean + 1;
  1705. /* fetch, update, and store next to clean */
  1706. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1707. rx_ring->next_to_clean = ntc;
  1708. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1709. /* if we are the last buffer then there is nothing else to do */
  1710. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1711. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1712. return false;
  1713. rx_ring->rx_stats.non_eop_descs++;
  1714. return true;
  1715. }
  1716. #define I40E_XDP_PASS 0
  1717. #define I40E_XDP_CONSUMED 1
  1718. #define I40E_XDP_TX 2
  1719. static int i40e_xmit_xdp_ring(struct xdp_buff *xdp,
  1720. struct i40e_ring *xdp_ring);
  1721. /**
  1722. * i40e_run_xdp - run an XDP program
  1723. * @rx_ring: Rx ring being processed
  1724. * @xdp: XDP buffer containing the frame
  1725. **/
  1726. static struct sk_buff *i40e_run_xdp(struct i40e_ring *rx_ring,
  1727. struct xdp_buff *xdp)
  1728. {
  1729. int result = I40E_XDP_PASS;
  1730. struct i40e_ring *xdp_ring;
  1731. struct bpf_prog *xdp_prog;
  1732. u32 act;
  1733. rcu_read_lock();
  1734. xdp_prog = READ_ONCE(rx_ring->xdp_prog);
  1735. if (!xdp_prog)
  1736. goto xdp_out;
  1737. act = bpf_prog_run_xdp(xdp_prog, xdp);
  1738. switch (act) {
  1739. case XDP_PASS:
  1740. break;
  1741. case XDP_TX:
  1742. xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  1743. result = i40e_xmit_xdp_ring(xdp, xdp_ring);
  1744. break;
  1745. default:
  1746. bpf_warn_invalid_xdp_action(act);
  1747. case XDP_ABORTED:
  1748. trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
  1749. /* fallthrough -- handle aborts by dropping packet */
  1750. case XDP_DROP:
  1751. result = I40E_XDP_CONSUMED;
  1752. break;
  1753. }
  1754. xdp_out:
  1755. rcu_read_unlock();
  1756. return ERR_PTR(-result);
  1757. }
  1758. /**
  1759. * i40e_rx_buffer_flip - adjusted rx_buffer to point to an unused region
  1760. * @rx_ring: Rx ring
  1761. * @rx_buffer: Rx buffer to adjust
  1762. * @size: Size of adjustment
  1763. **/
  1764. static void i40e_rx_buffer_flip(struct i40e_ring *rx_ring,
  1765. struct i40e_rx_buffer *rx_buffer,
  1766. unsigned int size)
  1767. {
  1768. #if (PAGE_SIZE < 8192)
  1769. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1770. rx_buffer->page_offset ^= truesize;
  1771. #else
  1772. unsigned int truesize = SKB_DATA_ALIGN(i40e_rx_offset(rx_ring) + size);
  1773. rx_buffer->page_offset += truesize;
  1774. #endif
  1775. }
  1776. /**
  1777. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1778. * @rx_ring: rx descriptor ring to transact packets on
  1779. * @budget: Total limit on number of packets to process
  1780. *
  1781. * This function provides a "bounce buffer" approach to Rx interrupt
  1782. * processing. The advantage to this is that on systems that have
  1783. * expensive overhead for IOMMU access this provides a means of avoiding
  1784. * it by maintaining the mapping of the page to the system.
  1785. *
  1786. * Returns amount of work completed
  1787. **/
  1788. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1789. {
  1790. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1791. struct sk_buff *skb = rx_ring->skb;
  1792. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1793. bool failure = false, xdp_xmit = false;
  1794. while (likely(total_rx_packets < (unsigned int)budget)) {
  1795. struct i40e_rx_buffer *rx_buffer;
  1796. union i40e_rx_desc *rx_desc;
  1797. struct xdp_buff xdp;
  1798. unsigned int size;
  1799. u16 vlan_tag;
  1800. u8 rx_ptype;
  1801. u64 qword;
  1802. /* return some buffers to hardware, one at a time is too slow */
  1803. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1804. failure = failure ||
  1805. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  1806. cleaned_count = 0;
  1807. }
  1808. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1809. /* status_error_len will always be zero for unused descriptors
  1810. * because it's cleared in cleanup, and overlaps with hdr_addr
  1811. * which is always zero because packet split isn't used, if the
  1812. * hardware wrote DD then the length will be non-zero
  1813. */
  1814. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1815. /* This memory barrier is needed to keep us from reading
  1816. * any other fields out of the rx_desc until we have
  1817. * verified the descriptor has been written back.
  1818. */
  1819. dma_rmb();
  1820. if (unlikely(i40e_rx_is_programming_status(qword))) {
  1821. i40e_clean_programming_status(rx_ring, rx_desc, qword);
  1822. cleaned_count++;
  1823. continue;
  1824. }
  1825. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1826. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1827. if (!size)
  1828. break;
  1829. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  1830. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  1831. /* retrieve a buffer from the ring */
  1832. if (!skb) {
  1833. xdp.data = page_address(rx_buffer->page) +
  1834. rx_buffer->page_offset;
  1835. xdp_set_data_meta_invalid(&xdp);
  1836. xdp.data_hard_start = xdp.data -
  1837. i40e_rx_offset(rx_ring);
  1838. xdp.data_end = xdp.data + size;
  1839. skb = i40e_run_xdp(rx_ring, &xdp);
  1840. }
  1841. if (IS_ERR(skb)) {
  1842. if (PTR_ERR(skb) == -I40E_XDP_TX) {
  1843. xdp_xmit = true;
  1844. i40e_rx_buffer_flip(rx_ring, rx_buffer, size);
  1845. } else {
  1846. rx_buffer->pagecnt_bias++;
  1847. }
  1848. total_rx_bytes += size;
  1849. total_rx_packets++;
  1850. } else if (skb) {
  1851. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  1852. } else if (ring_uses_build_skb(rx_ring)) {
  1853. skb = i40e_build_skb(rx_ring, rx_buffer, &xdp);
  1854. } else {
  1855. skb = i40e_construct_skb(rx_ring, rx_buffer, &xdp);
  1856. }
  1857. /* exit if we failed to retrieve a buffer */
  1858. if (!skb) {
  1859. rx_ring->rx_stats.alloc_buff_failed++;
  1860. rx_buffer->pagecnt_bias++;
  1861. break;
  1862. }
  1863. i40e_put_rx_buffer(rx_ring, rx_buffer);
  1864. cleaned_count++;
  1865. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  1866. continue;
  1867. if (i40e_cleanup_headers(rx_ring, skb, rx_desc)) {
  1868. skb = NULL;
  1869. continue;
  1870. }
  1871. /* probably a little skewed due to removing CRC */
  1872. total_rx_bytes += skb->len;
  1873. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1874. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1875. I40E_RXD_QW1_PTYPE_SHIFT;
  1876. /* populate checksum, VLAN, and protocol */
  1877. i40e_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  1878. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  1879. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  1880. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  1881. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1882. skb = NULL;
  1883. /* update budget accounting */
  1884. total_rx_packets++;
  1885. }
  1886. if (xdp_xmit) {
  1887. struct i40e_ring *xdp_ring;
  1888. xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
  1889. /* Force memory writes to complete before letting h/w
  1890. * know there are new descriptors to fetch.
  1891. */
  1892. wmb();
  1893. writel(xdp_ring->next_to_use, xdp_ring->tail);
  1894. }
  1895. rx_ring->skb = skb;
  1896. u64_stats_update_begin(&rx_ring->syncp);
  1897. rx_ring->stats.packets += total_rx_packets;
  1898. rx_ring->stats.bytes += total_rx_bytes;
  1899. u64_stats_update_end(&rx_ring->syncp);
  1900. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1901. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1902. /* guarantee a trip back through this routine if there was a failure */
  1903. return failure ? budget : (int)total_rx_packets;
  1904. }
  1905. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1906. {
  1907. u32 val;
  1908. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  1909. I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
  1910. (type << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
  1911. (itr << I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT);
  1912. return val;
  1913. }
  1914. /* a small macro to shorten up some long lines */
  1915. #define INTREG I40E_PFINT_DYN_CTLN
  1916. static inline int get_rx_itr(struct i40e_vsi *vsi, int idx)
  1917. {
  1918. return vsi->rx_rings[idx]->rx_itr_setting;
  1919. }
  1920. static inline int get_tx_itr(struct i40e_vsi *vsi, int idx)
  1921. {
  1922. return vsi->tx_rings[idx]->tx_itr_setting;
  1923. }
  1924. /**
  1925. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1926. * @vsi: the VSI we care about
  1927. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1928. *
  1929. **/
  1930. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1931. struct i40e_q_vector *q_vector)
  1932. {
  1933. struct i40e_hw *hw = &vsi->back->hw;
  1934. bool rx = false, tx = false;
  1935. u32 rxval, txval;
  1936. int vector;
  1937. int idx = q_vector->v_idx;
  1938. int rx_itr_setting, tx_itr_setting;
  1939. /* If we don't have MSIX, then we only need to re-enable icr0 */
  1940. if (!(vsi->back->flags & I40E_FLAG_MSIX_ENABLED)) {
  1941. i40e_irq_dynamic_enable_icr0(vsi->back);
  1942. return;
  1943. }
  1944. vector = (q_vector->v_idx + vsi->base_vector);
  1945. /* avoid dynamic calculation if in countdown mode OR if
  1946. * all dynamic is disabled
  1947. */
  1948. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1949. rx_itr_setting = get_rx_itr(vsi, idx);
  1950. tx_itr_setting = get_tx_itr(vsi, idx);
  1951. if (q_vector->itr_countdown > 0 ||
  1952. (!ITR_IS_DYNAMIC(rx_itr_setting) &&
  1953. !ITR_IS_DYNAMIC(tx_itr_setting))) {
  1954. goto enable_int;
  1955. }
  1956. if (ITR_IS_DYNAMIC(rx_itr_setting)) {
  1957. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1958. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1959. }
  1960. if (ITR_IS_DYNAMIC(tx_itr_setting)) {
  1961. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1962. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1963. }
  1964. if (rx || tx) {
  1965. /* get the higher of the two ITR adjustments and
  1966. * use the same value for both ITR registers
  1967. * when in adaptive mode (Rx and/or Tx)
  1968. */
  1969. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1970. q_vector->tx.itr = q_vector->rx.itr = itr;
  1971. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1972. tx = true;
  1973. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1974. rx = true;
  1975. }
  1976. /* only need to enable the interrupt once, but need
  1977. * to possibly update both ITR values
  1978. */
  1979. if (rx) {
  1980. /* set the INTENA_MSK_MASK so that this first write
  1981. * won't actually enable the interrupt, instead just
  1982. * updating the ITR (it's bit 31 PF and VF)
  1983. */
  1984. rxval |= BIT(31);
  1985. /* don't check _DOWN because interrupt isn't being enabled */
  1986. wr32(hw, INTREG(vector - 1), rxval);
  1987. }
  1988. enable_int:
  1989. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  1990. wr32(hw, INTREG(vector - 1), txval);
  1991. if (q_vector->itr_countdown)
  1992. q_vector->itr_countdown--;
  1993. else
  1994. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1995. }
  1996. /**
  1997. * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
  1998. * @napi: napi struct with our devices info in it
  1999. * @budget: amount of work driver is allowed to do this pass, in packets
  2000. *
  2001. * This function will clean all queues associated with a q_vector.
  2002. *
  2003. * Returns the amount of work done
  2004. **/
  2005. int i40e_napi_poll(struct napi_struct *napi, int budget)
  2006. {
  2007. struct i40e_q_vector *q_vector =
  2008. container_of(napi, struct i40e_q_vector, napi);
  2009. struct i40e_vsi *vsi = q_vector->vsi;
  2010. struct i40e_ring *ring;
  2011. bool clean_complete = true;
  2012. bool arm_wb = false;
  2013. int budget_per_ring;
  2014. int work_done = 0;
  2015. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  2016. napi_complete(napi);
  2017. return 0;
  2018. }
  2019. /* Since the actual Tx work is minimal, we can give the Tx a larger
  2020. * budget and be more aggressive about cleaning up the Tx descriptors.
  2021. */
  2022. i40e_for_each_ring(ring, q_vector->tx) {
  2023. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  2024. clean_complete = false;
  2025. continue;
  2026. }
  2027. arm_wb |= ring->arm_wb;
  2028. ring->arm_wb = false;
  2029. }
  2030. /* Handle case where we are called by netpoll with a budget of 0 */
  2031. if (budget <= 0)
  2032. goto tx_only;
  2033. /* We attempt to distribute budget to each Rx queue fairly, but don't
  2034. * allow the budget to go below 1 because that would exit polling early.
  2035. */
  2036. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  2037. i40e_for_each_ring(ring, q_vector->rx) {
  2038. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  2039. work_done += cleaned;
  2040. /* if we clean as many as budgeted, we must not be done */
  2041. if (cleaned >= budget_per_ring)
  2042. clean_complete = false;
  2043. }
  2044. /* If work not completed, return budget and polling will return */
  2045. if (!clean_complete) {
  2046. int cpu_id = smp_processor_id();
  2047. /* It is possible that the interrupt affinity has changed but,
  2048. * if the cpu is pegged at 100%, polling will never exit while
  2049. * traffic continues and the interrupt will be stuck on this
  2050. * cpu. We check to make sure affinity is correct before we
  2051. * continue to poll, otherwise we must stop polling so the
  2052. * interrupt can move to the correct cpu.
  2053. */
  2054. if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) {
  2055. /* Tell napi that we are done polling */
  2056. napi_complete_done(napi, work_done);
  2057. /* Force an interrupt */
  2058. i40e_force_wb(vsi, q_vector);
  2059. /* Return budget-1 so that polling stops */
  2060. return budget - 1;
  2061. }
  2062. tx_only:
  2063. if (arm_wb) {
  2064. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  2065. i40e_enable_wb_on_itr(vsi, q_vector);
  2066. }
  2067. return budget;
  2068. }
  2069. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  2070. q_vector->arm_wb_state = false;
  2071. /* Work is done so exit the polling mode and re-enable the interrupt */
  2072. napi_complete_done(napi, work_done);
  2073. i40e_update_enable_itr(vsi, q_vector);
  2074. return min(work_done, budget - 1);
  2075. }
  2076. /**
  2077. * i40e_atr - Add a Flow Director ATR filter
  2078. * @tx_ring: ring to add programming descriptor to
  2079. * @skb: send buffer
  2080. * @tx_flags: send tx flags
  2081. **/
  2082. static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2083. u32 tx_flags)
  2084. {
  2085. struct i40e_filter_program_desc *fdir_desc;
  2086. struct i40e_pf *pf = tx_ring->vsi->back;
  2087. union {
  2088. unsigned char *network;
  2089. struct iphdr *ipv4;
  2090. struct ipv6hdr *ipv6;
  2091. } hdr;
  2092. struct tcphdr *th;
  2093. unsigned int hlen;
  2094. u32 flex_ptype, dtype_cmd;
  2095. int l4_proto;
  2096. u16 i;
  2097. /* make sure ATR is enabled */
  2098. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  2099. return;
  2100. if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED)
  2101. return;
  2102. /* if sampling is disabled do nothing */
  2103. if (!tx_ring->atr_sample_rate)
  2104. return;
  2105. /* Currently only IPv4/IPv6 with TCP is supported */
  2106. if (!(tx_flags & (I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6)))
  2107. return;
  2108. /* snag network header to get L4 type and address */
  2109. hdr.network = (tx_flags & I40E_TX_FLAGS_UDP_TUNNEL) ?
  2110. skb_inner_network_header(skb) : skb_network_header(skb);
  2111. /* Note: tx_flags gets modified to reflect inner protocols in
  2112. * tx_enable_csum function if encap is enabled.
  2113. */
  2114. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  2115. /* access ihl as u8 to avoid unaligned access on ia64 */
  2116. hlen = (hdr.network[0] & 0x0F) << 2;
  2117. l4_proto = hdr.ipv4->protocol;
  2118. } else {
  2119. /* find the start of the innermost ipv6 header */
  2120. unsigned int inner_hlen = hdr.network - skb->data;
  2121. unsigned int h_offset = inner_hlen;
  2122. /* this function updates h_offset to the end of the header */
  2123. l4_proto =
  2124. ipv6_find_hdr(skb, &h_offset, IPPROTO_TCP, NULL, NULL);
  2125. /* hlen will contain our best estimate of the tcp header */
  2126. hlen = h_offset - inner_hlen;
  2127. }
  2128. if (l4_proto != IPPROTO_TCP)
  2129. return;
  2130. th = (struct tcphdr *)(hdr.network + hlen);
  2131. /* Due to lack of space, no more new filters can be programmed */
  2132. if (th->syn && (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
  2133. return;
  2134. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) {
  2135. /* HW ATR eviction will take care of removing filters on FIN
  2136. * and RST packets.
  2137. */
  2138. if (th->fin || th->rst)
  2139. return;
  2140. }
  2141. tx_ring->atr_count++;
  2142. /* sample on all syn/fin/rst packets or once every atr sample rate */
  2143. if (!th->fin &&
  2144. !th->syn &&
  2145. !th->rst &&
  2146. (tx_ring->atr_count < tx_ring->atr_sample_rate))
  2147. return;
  2148. tx_ring->atr_count = 0;
  2149. /* grab the next descriptor */
  2150. i = tx_ring->next_to_use;
  2151. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  2152. i++;
  2153. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2154. flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  2155. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  2156. flex_ptype |= (tx_flags & I40E_TX_FLAGS_IPV4) ?
  2157. (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
  2158. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
  2159. (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
  2160. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  2161. flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  2162. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  2163. dtype_cmd |= (th->fin || th->rst) ?
  2164. (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  2165. I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
  2166. (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  2167. I40E_TXD_FLTR_QW1_PCMD_SHIFT);
  2168. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
  2169. I40E_TXD_FLTR_QW1_DEST_SHIFT;
  2170. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
  2171. I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
  2172. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  2173. if (!(tx_flags & I40E_TX_FLAGS_UDP_TUNNEL))
  2174. dtype_cmd |=
  2175. ((u32)I40E_FD_ATR_STAT_IDX(pf->hw.pf_id) <<
  2176. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2177. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2178. else
  2179. dtype_cmd |=
  2180. ((u32)I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id) <<
  2181. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2182. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2183. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_ENABLED)
  2184. dtype_cmd |= I40E_TXD_FLTR_QW1_ATR_MASK;
  2185. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  2186. fdir_desc->rsvd = cpu_to_le32(0);
  2187. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  2188. fdir_desc->fd_id = cpu_to_le32(0);
  2189. }
  2190. /**
  2191. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  2192. * @skb: send buffer
  2193. * @tx_ring: ring to send buffer on
  2194. * @flags: the tx flags to be set
  2195. *
  2196. * Checks the skb and set up correspondingly several generic transmit flags
  2197. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  2198. *
  2199. * Returns error code indicate the frame should be dropped upon error and the
  2200. * otherwise returns 0 to indicate the flags has been set properly.
  2201. **/
  2202. static inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  2203. struct i40e_ring *tx_ring,
  2204. u32 *flags)
  2205. {
  2206. __be16 protocol = skb->protocol;
  2207. u32 tx_flags = 0;
  2208. if (protocol == htons(ETH_P_8021Q) &&
  2209. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  2210. /* When HW VLAN acceleration is turned off by the user the
  2211. * stack sets the protocol to 8021q so that the driver
  2212. * can take any steps required to support the SW only
  2213. * VLAN handling. In our case the driver doesn't need
  2214. * to take any further steps so just set the protocol
  2215. * to the encapsulated ethertype.
  2216. */
  2217. skb->protocol = vlan_get_protocol(skb);
  2218. goto out;
  2219. }
  2220. /* if we have a HW VLAN tag being added, default to the HW one */
  2221. if (skb_vlan_tag_present(skb)) {
  2222. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  2223. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2224. /* else if it is a SW VLAN, check the next protocol and store the tag */
  2225. } else if (protocol == htons(ETH_P_8021Q)) {
  2226. struct vlan_hdr *vhdr, _vhdr;
  2227. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  2228. if (!vhdr)
  2229. return -EINVAL;
  2230. protocol = vhdr->h_vlan_encapsulated_proto;
  2231. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  2232. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  2233. }
  2234. if (!(tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2235. goto out;
  2236. /* Insert 802.1p priority into VLAN header */
  2237. if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
  2238. (skb->priority != TC_PRIO_CONTROL)) {
  2239. tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
  2240. tx_flags |= (skb->priority & 0x7) <<
  2241. I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
  2242. if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
  2243. struct vlan_ethhdr *vhdr;
  2244. int rc;
  2245. rc = skb_cow_head(skb, 0);
  2246. if (rc < 0)
  2247. return rc;
  2248. vhdr = (struct vlan_ethhdr *)skb->data;
  2249. vhdr->h_vlan_TCI = htons(tx_flags >>
  2250. I40E_TX_FLAGS_VLAN_SHIFT);
  2251. } else {
  2252. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2253. }
  2254. }
  2255. out:
  2256. *flags = tx_flags;
  2257. return 0;
  2258. }
  2259. /**
  2260. * i40e_tso - set up the tso context descriptor
  2261. * @first: pointer to first Tx buffer for xmit
  2262. * @hdr_len: ptr to the size of the packet header
  2263. * @cd_type_cmd_tso_mss: Quad Word 1
  2264. *
  2265. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  2266. **/
  2267. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  2268. u64 *cd_type_cmd_tso_mss)
  2269. {
  2270. struct sk_buff *skb = first->skb;
  2271. u64 cd_cmd, cd_tso_len, cd_mss;
  2272. union {
  2273. struct iphdr *v4;
  2274. struct ipv6hdr *v6;
  2275. unsigned char *hdr;
  2276. } ip;
  2277. union {
  2278. struct tcphdr *tcp;
  2279. struct udphdr *udp;
  2280. unsigned char *hdr;
  2281. } l4;
  2282. u32 paylen, l4_offset;
  2283. u16 gso_segs, gso_size;
  2284. int err;
  2285. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2286. return 0;
  2287. if (!skb_is_gso(skb))
  2288. return 0;
  2289. err = skb_cow_head(skb, 0);
  2290. if (err < 0)
  2291. return err;
  2292. ip.hdr = skb_network_header(skb);
  2293. l4.hdr = skb_transport_header(skb);
  2294. /* initialize outer IP header fields */
  2295. if (ip.v4->version == 4) {
  2296. ip.v4->tot_len = 0;
  2297. ip.v4->check = 0;
  2298. } else {
  2299. ip.v6->payload_len = 0;
  2300. }
  2301. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  2302. SKB_GSO_GRE_CSUM |
  2303. SKB_GSO_IPXIP4 |
  2304. SKB_GSO_IPXIP6 |
  2305. SKB_GSO_UDP_TUNNEL |
  2306. SKB_GSO_UDP_TUNNEL_CSUM)) {
  2307. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2308. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  2309. l4.udp->len = 0;
  2310. /* determine offset of outer transport header */
  2311. l4_offset = l4.hdr - skb->data;
  2312. /* remove payload length from outer checksum */
  2313. paylen = skb->len - l4_offset;
  2314. csum_replace_by_diff(&l4.udp->check,
  2315. (__force __wsum)htonl(paylen));
  2316. }
  2317. /* reset pointers to inner headers */
  2318. ip.hdr = skb_inner_network_header(skb);
  2319. l4.hdr = skb_inner_transport_header(skb);
  2320. /* initialize inner IP header fields */
  2321. if (ip.v4->version == 4) {
  2322. ip.v4->tot_len = 0;
  2323. ip.v4->check = 0;
  2324. } else {
  2325. ip.v6->payload_len = 0;
  2326. }
  2327. }
  2328. /* determine offset of inner transport header */
  2329. l4_offset = l4.hdr - skb->data;
  2330. /* remove payload length from inner checksum */
  2331. paylen = skb->len - l4_offset;
  2332. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  2333. /* compute length of segmentation header */
  2334. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  2335. /* pull values out of skb_shinfo */
  2336. gso_size = skb_shinfo(skb)->gso_size;
  2337. gso_segs = skb_shinfo(skb)->gso_segs;
  2338. /* update GSO size and bytecount with header size */
  2339. first->gso_segs = gso_segs;
  2340. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  2341. /* find the field values */
  2342. cd_cmd = I40E_TX_CTX_DESC_TSO;
  2343. cd_tso_len = skb->len - *hdr_len;
  2344. cd_mss = gso_size;
  2345. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  2346. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  2347. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  2348. return 1;
  2349. }
  2350. /**
  2351. * i40e_tsyn - set up the tsyn context descriptor
  2352. * @tx_ring: ptr to the ring to send
  2353. * @skb: ptr to the skb we're sending
  2354. * @tx_flags: the collected send information
  2355. * @cd_type_cmd_tso_mss: Quad Word 1
  2356. *
  2357. * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
  2358. **/
  2359. static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2360. u32 tx_flags, u64 *cd_type_cmd_tso_mss)
  2361. {
  2362. struct i40e_pf *pf;
  2363. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  2364. return 0;
  2365. /* Tx timestamps cannot be sampled when doing TSO */
  2366. if (tx_flags & I40E_TX_FLAGS_TSO)
  2367. return 0;
  2368. /* only timestamp the outbound packet if the user has requested it and
  2369. * we are not already transmitting a packet to be timestamped
  2370. */
  2371. pf = i40e_netdev_to_pf(tx_ring->netdev);
  2372. if (!(pf->flags & I40E_FLAG_PTP))
  2373. return 0;
  2374. if (pf->ptp_tx &&
  2375. !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, pf->state)) {
  2376. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2377. pf->ptp_tx_start = jiffies;
  2378. pf->ptp_tx_skb = skb_get(skb);
  2379. } else {
  2380. pf->tx_hwtstamp_skipped++;
  2381. return 0;
  2382. }
  2383. *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
  2384. I40E_TXD_CTX_QW1_CMD_SHIFT;
  2385. return 1;
  2386. }
  2387. /**
  2388. * i40e_tx_enable_csum - Enable Tx checksum offloads
  2389. * @skb: send buffer
  2390. * @tx_flags: pointer to Tx flags currently set
  2391. * @td_cmd: Tx descriptor command bits to set
  2392. * @td_offset: Tx descriptor header offsets to set
  2393. * @tx_ring: Tx descriptor ring
  2394. * @cd_tunneling: ptr to context desc bits
  2395. **/
  2396. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  2397. u32 *td_cmd, u32 *td_offset,
  2398. struct i40e_ring *tx_ring,
  2399. u32 *cd_tunneling)
  2400. {
  2401. union {
  2402. struct iphdr *v4;
  2403. struct ipv6hdr *v6;
  2404. unsigned char *hdr;
  2405. } ip;
  2406. union {
  2407. struct tcphdr *tcp;
  2408. struct udphdr *udp;
  2409. unsigned char *hdr;
  2410. } l4;
  2411. unsigned char *exthdr;
  2412. u32 offset, cmd = 0;
  2413. __be16 frag_off;
  2414. u8 l4_proto = 0;
  2415. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2416. return 0;
  2417. ip.hdr = skb_network_header(skb);
  2418. l4.hdr = skb_transport_header(skb);
  2419. /* compute outer L2 header size */
  2420. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  2421. if (skb->encapsulation) {
  2422. u32 tunnel = 0;
  2423. /* define outer network header type */
  2424. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2425. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2426. I40E_TX_CTX_EXT_IP_IPV4 :
  2427. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  2428. l4_proto = ip.v4->protocol;
  2429. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2430. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  2431. exthdr = ip.hdr + sizeof(*ip.v6);
  2432. l4_proto = ip.v6->nexthdr;
  2433. if (l4.hdr != exthdr)
  2434. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2435. &l4_proto, &frag_off);
  2436. }
  2437. /* define outer transport */
  2438. switch (l4_proto) {
  2439. case IPPROTO_UDP:
  2440. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  2441. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2442. break;
  2443. case IPPROTO_GRE:
  2444. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  2445. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2446. break;
  2447. case IPPROTO_IPIP:
  2448. case IPPROTO_IPV6:
  2449. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2450. l4.hdr = skb_inner_network_header(skb);
  2451. break;
  2452. default:
  2453. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2454. return -1;
  2455. skb_checksum_help(skb);
  2456. return 0;
  2457. }
  2458. /* compute outer L3 header size */
  2459. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  2460. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  2461. /* switch IP header pointer from outer to inner header */
  2462. ip.hdr = skb_inner_network_header(skb);
  2463. /* compute tunnel header size */
  2464. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  2465. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  2466. /* indicate if we need to offload outer UDP header */
  2467. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  2468. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2469. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  2470. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  2471. /* record tunnel offload values */
  2472. *cd_tunneling |= tunnel;
  2473. /* switch L4 header pointer from outer to inner */
  2474. l4.hdr = skb_inner_transport_header(skb);
  2475. l4_proto = 0;
  2476. /* reset type as we transition from outer to inner headers */
  2477. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  2478. if (ip.v4->version == 4)
  2479. *tx_flags |= I40E_TX_FLAGS_IPV4;
  2480. if (ip.v6->version == 6)
  2481. *tx_flags |= I40E_TX_FLAGS_IPV6;
  2482. }
  2483. /* Enable IP checksum offloads */
  2484. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2485. l4_proto = ip.v4->protocol;
  2486. /* the stack computes the IP header already, the only time we
  2487. * need the hardware to recompute it is in the case of TSO.
  2488. */
  2489. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2490. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  2491. I40E_TX_DESC_CMD_IIPT_IPV4;
  2492. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2493. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  2494. exthdr = ip.hdr + sizeof(*ip.v6);
  2495. l4_proto = ip.v6->nexthdr;
  2496. if (l4.hdr != exthdr)
  2497. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2498. &l4_proto, &frag_off);
  2499. }
  2500. /* compute inner L3 header size */
  2501. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  2502. /* Enable L4 checksum offloads */
  2503. switch (l4_proto) {
  2504. case IPPROTO_TCP:
  2505. /* enable checksum offloads */
  2506. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  2507. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2508. break;
  2509. case IPPROTO_SCTP:
  2510. /* enable SCTP checksum offload */
  2511. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  2512. offset |= (sizeof(struct sctphdr) >> 2) <<
  2513. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2514. break;
  2515. case IPPROTO_UDP:
  2516. /* enable UDP checksum offload */
  2517. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  2518. offset |= (sizeof(struct udphdr) >> 2) <<
  2519. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2520. break;
  2521. default:
  2522. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2523. return -1;
  2524. skb_checksum_help(skb);
  2525. return 0;
  2526. }
  2527. *td_cmd |= cmd;
  2528. *td_offset |= offset;
  2529. return 1;
  2530. }
  2531. /**
  2532. * i40e_create_tx_ctx Build the Tx context descriptor
  2533. * @tx_ring: ring to create the descriptor on
  2534. * @cd_type_cmd_tso_mss: Quad Word 1
  2535. * @cd_tunneling: Quad Word 0 - bits 0-31
  2536. * @cd_l2tag2: Quad Word 0 - bits 32-63
  2537. **/
  2538. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  2539. const u64 cd_type_cmd_tso_mss,
  2540. const u32 cd_tunneling, const u32 cd_l2tag2)
  2541. {
  2542. struct i40e_tx_context_desc *context_desc;
  2543. int i = tx_ring->next_to_use;
  2544. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  2545. !cd_tunneling && !cd_l2tag2)
  2546. return;
  2547. /* grab the next descriptor */
  2548. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  2549. i++;
  2550. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2551. /* cpu_to_le32 and assign to struct fields */
  2552. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  2553. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  2554. context_desc->rsvd = cpu_to_le16(0);
  2555. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  2556. }
  2557. /**
  2558. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  2559. * @tx_ring: the ring to be checked
  2560. * @size: the size buffer we want to assure is available
  2561. *
  2562. * Returns -EBUSY if a stop is needed, else 0
  2563. **/
  2564. int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  2565. {
  2566. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2567. /* Memory barrier before checking head and tail */
  2568. smp_mb();
  2569. /* Check again in a case another CPU has just made room available. */
  2570. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  2571. return -EBUSY;
  2572. /* A reprieve! - use start_queue because it doesn't call schedule */
  2573. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2574. ++tx_ring->tx_stats.restart_queue;
  2575. return 0;
  2576. }
  2577. /**
  2578. * __i40e_chk_linearize - Check if there are more than 8 buffers per packet
  2579. * @skb: send buffer
  2580. *
  2581. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  2582. * and so we need to figure out the cases where we need to linearize the skb.
  2583. *
  2584. * For TSO we need to count the TSO header and segment payload separately.
  2585. * As such we need to check cases where we have 7 fragments or more as we
  2586. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  2587. * the segment payload in the first descriptor, and another 7 for the
  2588. * fragments.
  2589. **/
  2590. bool __i40e_chk_linearize(struct sk_buff *skb)
  2591. {
  2592. const struct skb_frag_struct *frag, *stale;
  2593. int nr_frags, sum;
  2594. /* no need to check if number of frags is less than 7 */
  2595. nr_frags = skb_shinfo(skb)->nr_frags;
  2596. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  2597. return false;
  2598. /* We need to walk through the list and validate that each group
  2599. * of 6 fragments totals at least gso_size.
  2600. */
  2601. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  2602. frag = &skb_shinfo(skb)->frags[0];
  2603. /* Initialize size to the negative value of gso_size minus 1. We
  2604. * use this as the worst case scenerio in which the frag ahead
  2605. * of us only provides one byte which is why we are limited to 6
  2606. * descriptors for a single transmit as the header and previous
  2607. * fragment are already consuming 2 descriptors.
  2608. */
  2609. sum = 1 - skb_shinfo(skb)->gso_size;
  2610. /* Add size of frags 0 through 4 to create our initial sum */
  2611. sum += skb_frag_size(frag++);
  2612. sum += skb_frag_size(frag++);
  2613. sum += skb_frag_size(frag++);
  2614. sum += skb_frag_size(frag++);
  2615. sum += skb_frag_size(frag++);
  2616. /* Walk through fragments adding latest fragment, testing it, and
  2617. * then removing stale fragments from the sum.
  2618. */
  2619. stale = &skb_shinfo(skb)->frags[0];
  2620. for (;;) {
  2621. sum += skb_frag_size(frag++);
  2622. /* if sum is negative we failed to make sufficient progress */
  2623. if (sum < 0)
  2624. return true;
  2625. if (!nr_frags--)
  2626. break;
  2627. sum -= skb_frag_size(stale++);
  2628. }
  2629. return false;
  2630. }
  2631. /**
  2632. * i40e_tx_map - Build the Tx descriptor
  2633. * @tx_ring: ring to send buffer on
  2634. * @skb: send buffer
  2635. * @first: first buffer info buffer to use
  2636. * @tx_flags: collected send information
  2637. * @hdr_len: size of the packet header
  2638. * @td_cmd: the command field in the descriptor
  2639. * @td_offset: offset for checksum or crc
  2640. *
  2641. * Returns 0 on success, -1 on failure to DMA
  2642. **/
  2643. static inline int i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2644. struct i40e_tx_buffer *first, u32 tx_flags,
  2645. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  2646. {
  2647. unsigned int data_len = skb->data_len;
  2648. unsigned int size = skb_headlen(skb);
  2649. struct skb_frag_struct *frag;
  2650. struct i40e_tx_buffer *tx_bi;
  2651. struct i40e_tx_desc *tx_desc;
  2652. u16 i = tx_ring->next_to_use;
  2653. u32 td_tag = 0;
  2654. dma_addr_t dma;
  2655. u16 desc_count = 1;
  2656. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  2657. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  2658. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  2659. I40E_TX_FLAGS_VLAN_SHIFT;
  2660. }
  2661. first->tx_flags = tx_flags;
  2662. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  2663. tx_desc = I40E_TX_DESC(tx_ring, i);
  2664. tx_bi = first;
  2665. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  2666. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2667. if (dma_mapping_error(tx_ring->dev, dma))
  2668. goto dma_error;
  2669. /* record length, and DMA address */
  2670. dma_unmap_len_set(tx_bi, len, size);
  2671. dma_unmap_addr_set(tx_bi, dma, dma);
  2672. /* align size to end of page */
  2673. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  2674. tx_desc->buffer_addr = cpu_to_le64(dma);
  2675. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  2676. tx_desc->cmd_type_offset_bsz =
  2677. build_ctob(td_cmd, td_offset,
  2678. max_data, td_tag);
  2679. tx_desc++;
  2680. i++;
  2681. desc_count++;
  2682. if (i == tx_ring->count) {
  2683. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2684. i = 0;
  2685. }
  2686. dma += max_data;
  2687. size -= max_data;
  2688. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2689. tx_desc->buffer_addr = cpu_to_le64(dma);
  2690. }
  2691. if (likely(!data_len))
  2692. break;
  2693. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  2694. size, td_tag);
  2695. tx_desc++;
  2696. i++;
  2697. desc_count++;
  2698. if (i == tx_ring->count) {
  2699. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2700. i = 0;
  2701. }
  2702. size = skb_frag_size(frag);
  2703. data_len -= size;
  2704. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2705. DMA_TO_DEVICE);
  2706. tx_bi = &tx_ring->tx_bi[i];
  2707. }
  2708. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  2709. i++;
  2710. if (i == tx_ring->count)
  2711. i = 0;
  2712. tx_ring->next_to_use = i;
  2713. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2714. /* write last descriptor with EOP bit */
  2715. td_cmd |= I40E_TX_DESC_CMD_EOP;
  2716. /* We OR these values together to check both against 4 (WB_STRIDE)
  2717. * below. This is safe since we don't re-use desc_count afterwards.
  2718. */
  2719. desc_count |= ++tx_ring->packet_stride;
  2720. if (desc_count >= WB_STRIDE) {
  2721. /* write last descriptor with RS bit set */
  2722. td_cmd |= I40E_TX_DESC_CMD_RS;
  2723. tx_ring->packet_stride = 0;
  2724. }
  2725. tx_desc->cmd_type_offset_bsz =
  2726. build_ctob(td_cmd, td_offset, size, td_tag);
  2727. /* Force memory writes to complete before letting h/w know there
  2728. * are new descriptors to fetch.
  2729. *
  2730. * We also use this memory barrier to make certain all of the
  2731. * status bits have been updated before next_to_watch is written.
  2732. */
  2733. wmb();
  2734. /* set next_to_watch value indicating a packet is present */
  2735. first->next_to_watch = tx_desc;
  2736. /* notify HW of packet */
  2737. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  2738. writel(i, tx_ring->tail);
  2739. /* we need this if more than one processor can write to our tail
  2740. * at a time, it synchronizes IO on IA64/Altix systems
  2741. */
  2742. mmiowb();
  2743. }
  2744. return 0;
  2745. dma_error:
  2746. dev_info(tx_ring->dev, "TX DMA map failed\n");
  2747. /* clear dma mappings for failed tx_bi map */
  2748. for (;;) {
  2749. tx_bi = &tx_ring->tx_bi[i];
  2750. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  2751. if (tx_bi == first)
  2752. break;
  2753. if (i == 0)
  2754. i = tx_ring->count;
  2755. i--;
  2756. }
  2757. tx_ring->next_to_use = i;
  2758. return -1;
  2759. }
  2760. /**
  2761. * i40e_xmit_xdp_ring - transmits an XDP buffer to an XDP Tx ring
  2762. * @xdp: data to transmit
  2763. * @xdp_ring: XDP Tx ring
  2764. **/
  2765. static int i40e_xmit_xdp_ring(struct xdp_buff *xdp,
  2766. struct i40e_ring *xdp_ring)
  2767. {
  2768. u32 size = xdp->data_end - xdp->data;
  2769. u16 i = xdp_ring->next_to_use;
  2770. struct i40e_tx_buffer *tx_bi;
  2771. struct i40e_tx_desc *tx_desc;
  2772. dma_addr_t dma;
  2773. if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) {
  2774. xdp_ring->tx_stats.tx_busy++;
  2775. return I40E_XDP_CONSUMED;
  2776. }
  2777. dma = dma_map_single(xdp_ring->dev, xdp->data, size, DMA_TO_DEVICE);
  2778. if (dma_mapping_error(xdp_ring->dev, dma))
  2779. return I40E_XDP_CONSUMED;
  2780. tx_bi = &xdp_ring->tx_bi[i];
  2781. tx_bi->bytecount = size;
  2782. tx_bi->gso_segs = 1;
  2783. tx_bi->raw_buf = xdp->data;
  2784. /* record length, and DMA address */
  2785. dma_unmap_len_set(tx_bi, len, size);
  2786. dma_unmap_addr_set(tx_bi, dma, dma);
  2787. tx_desc = I40E_TX_DESC(xdp_ring, i);
  2788. tx_desc->buffer_addr = cpu_to_le64(dma);
  2789. tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC
  2790. | I40E_TXD_CMD,
  2791. 0, size, 0);
  2792. /* Make certain all of the status bits have been updated
  2793. * before next_to_watch is written.
  2794. */
  2795. smp_wmb();
  2796. i++;
  2797. if (i == xdp_ring->count)
  2798. i = 0;
  2799. tx_bi->next_to_watch = tx_desc;
  2800. xdp_ring->next_to_use = i;
  2801. return I40E_XDP_TX;
  2802. }
  2803. /**
  2804. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  2805. * @skb: send buffer
  2806. * @tx_ring: ring to send buffer on
  2807. *
  2808. * Returns NETDEV_TX_OK if sent, else an error code
  2809. **/
  2810. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  2811. struct i40e_ring *tx_ring)
  2812. {
  2813. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  2814. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  2815. struct i40e_tx_buffer *first;
  2816. u32 td_offset = 0;
  2817. u32 tx_flags = 0;
  2818. __be16 protocol;
  2819. u32 td_cmd = 0;
  2820. u8 hdr_len = 0;
  2821. int tso, count;
  2822. int tsyn;
  2823. /* prefetch the data, we'll need it later */
  2824. prefetch(skb->data);
  2825. i40e_trace(xmit_frame_ring, skb, tx_ring);
  2826. count = i40e_xmit_descriptor_count(skb);
  2827. if (i40e_chk_linearize(skb, count)) {
  2828. if (__skb_linearize(skb)) {
  2829. dev_kfree_skb_any(skb);
  2830. return NETDEV_TX_OK;
  2831. }
  2832. count = i40e_txd_use_count(skb->len);
  2833. tx_ring->tx_stats.tx_linearize++;
  2834. }
  2835. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  2836. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  2837. * + 4 desc gap to avoid the cache line where head is,
  2838. * + 1 desc for context descriptor,
  2839. * otherwise try next time
  2840. */
  2841. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  2842. tx_ring->tx_stats.tx_busy++;
  2843. return NETDEV_TX_BUSY;
  2844. }
  2845. /* record the location of the first descriptor for this packet */
  2846. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  2847. first->skb = skb;
  2848. first->bytecount = skb->len;
  2849. first->gso_segs = 1;
  2850. /* prepare the xmit flags */
  2851. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  2852. goto out_drop;
  2853. /* obtain protocol of skb */
  2854. protocol = vlan_get_protocol(skb);
  2855. /* setup IPv4/IPv6 offloads */
  2856. if (protocol == htons(ETH_P_IP))
  2857. tx_flags |= I40E_TX_FLAGS_IPV4;
  2858. else if (protocol == htons(ETH_P_IPV6))
  2859. tx_flags |= I40E_TX_FLAGS_IPV6;
  2860. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  2861. if (tso < 0)
  2862. goto out_drop;
  2863. else if (tso)
  2864. tx_flags |= I40E_TX_FLAGS_TSO;
  2865. /* Always offload the checksum, since it's in the data descriptor */
  2866. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  2867. tx_ring, &cd_tunneling);
  2868. if (tso < 0)
  2869. goto out_drop;
  2870. tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
  2871. if (tsyn)
  2872. tx_flags |= I40E_TX_FLAGS_TSYN;
  2873. skb_tx_timestamp(skb);
  2874. /* always enable CRC insertion offload */
  2875. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  2876. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  2877. cd_tunneling, cd_l2tag2);
  2878. /* Add Flow Director ATR if it's enabled.
  2879. *
  2880. * NOTE: this must always be directly before the data descriptor.
  2881. */
  2882. i40e_atr(tx_ring, skb, tx_flags);
  2883. if (i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  2884. td_cmd, td_offset))
  2885. goto cleanup_tx_tstamp;
  2886. return NETDEV_TX_OK;
  2887. out_drop:
  2888. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  2889. dev_kfree_skb_any(first->skb);
  2890. first->skb = NULL;
  2891. cleanup_tx_tstamp:
  2892. if (unlikely(tx_flags & I40E_TX_FLAGS_TSYN)) {
  2893. struct i40e_pf *pf = i40e_netdev_to_pf(tx_ring->netdev);
  2894. dev_kfree_skb_any(pf->ptp_tx_skb);
  2895. pf->ptp_tx_skb = NULL;
  2896. clear_bit_unlock(__I40E_PTP_TX_IN_PROGRESS, pf->state);
  2897. }
  2898. return NETDEV_TX_OK;
  2899. }
  2900. /**
  2901. * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  2902. * @skb: send buffer
  2903. * @netdev: network interface device structure
  2904. *
  2905. * Returns NETDEV_TX_OK if sent, else an error code
  2906. **/
  2907. netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2908. {
  2909. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2910. struct i40e_vsi *vsi = np->vsi;
  2911. struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
  2912. /* hardware can't handle really short frames, hardware padding works
  2913. * beyond this point
  2914. */
  2915. if (skb_put_padto(skb, I40E_MIN_TX_LEN))
  2916. return NETDEV_TX_OK;
  2917. return i40e_xmit_frame_ring(skb, tx_ring);
  2918. }