i40e_txrx.c 74 KB

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