i40e_txrx.c 83 KB

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