i40e_txrx.c 84 KB

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