i40e_txrx.c 77 KB

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