i40e_txrx.c 80 KB

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