i40e_txrx.c 78 KB

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