i40e_txrx.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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 "i40e.h"
  28. #include "i40e_prototype.h"
  29. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  30. u32 td_tag)
  31. {
  32. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  33. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  34. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  35. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  36. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  37. }
  38. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  39. /**
  40. * i40e_program_fdir_filter - Program a Flow Director filter
  41. * @fdir_data: Packet data that will be filter parameters
  42. * @raw_packet: the pre-allocated packet buffer for FDir
  43. * @pf: The pf pointer
  44. * @add: True for add/update, False for remove
  45. **/
  46. int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data, u8 *raw_packet,
  47. struct i40e_pf *pf, bool add)
  48. {
  49. struct i40e_filter_program_desc *fdir_desc;
  50. struct i40e_tx_buffer *tx_buf;
  51. struct i40e_tx_desc *tx_desc;
  52. struct i40e_ring *tx_ring;
  53. unsigned int fpt, dcc;
  54. struct i40e_vsi *vsi;
  55. struct device *dev;
  56. dma_addr_t dma;
  57. u32 td_cmd = 0;
  58. u16 i;
  59. /* find existing FDIR VSI */
  60. vsi = NULL;
  61. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  62. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
  63. vsi = pf->vsi[i];
  64. if (!vsi)
  65. return -ENOENT;
  66. tx_ring = vsi->tx_rings[0];
  67. dev = tx_ring->dev;
  68. dma = dma_map_single(dev, raw_packet,
  69. I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
  70. if (dma_mapping_error(dev, dma))
  71. goto dma_fail;
  72. /* grab the next descriptor */
  73. i = tx_ring->next_to_use;
  74. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  75. tx_ring->next_to_use = (i + 1 < tx_ring->count) ? i + 1 : 0;
  76. fpt = (fdir_data->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  77. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  78. fpt |= (fdir_data->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT) &
  79. I40E_TXD_FLTR_QW0_FLEXOFF_MASK;
  80. fpt |= (fdir_data->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) &
  81. I40E_TXD_FLTR_QW0_PCTYPE_MASK;
  82. /* Use LAN VSI Id if not programmed by user */
  83. if (fdir_data->dest_vsi == 0)
  84. fpt |= (pf->vsi[pf->lan_vsi]->id) <<
  85. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  86. else
  87. fpt |= ((u32)fdir_data->dest_vsi <<
  88. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT) &
  89. I40E_TXD_FLTR_QW0_DEST_VSI_MASK;
  90. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(fpt);
  91. dcc = I40E_TX_DESC_DTYPE_FILTER_PROG;
  92. if (add)
  93. dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  94. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  95. else
  96. dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  97. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  98. dcc |= (fdir_data->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT) &
  99. I40E_TXD_FLTR_QW1_DEST_MASK;
  100. dcc |= (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) &
  101. I40E_TXD_FLTR_QW1_FD_STATUS_MASK;
  102. if (fdir_data->cnt_index != 0) {
  103. dcc |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  104. dcc |= ((u32)fdir_data->cnt_index <<
  105. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  106. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  107. }
  108. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dcc);
  109. fdir_desc->fd_id = cpu_to_le32(fdir_data->fd_id);
  110. /* Now program a dummy descriptor */
  111. i = tx_ring->next_to_use;
  112. tx_desc = I40E_TX_DESC(tx_ring, i);
  113. tx_buf = &tx_ring->tx_bi[i];
  114. tx_ring->next_to_use = (i + 1 < tx_ring->count) ? i + 1 : 0;
  115. /* record length, and DMA address */
  116. dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
  117. dma_unmap_addr_set(tx_buf, dma, dma);
  118. tx_desc->buffer_addr = cpu_to_le64(dma);
  119. td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
  120. tx_desc->cmd_type_offset_bsz =
  121. build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
  122. /* set the timestamp */
  123. tx_buf->time_stamp = jiffies;
  124. /* Force memory writes to complete before letting h/w
  125. * know there are new descriptors to fetch. (Only
  126. * applicable for weak-ordered memory model archs,
  127. * such as IA-64).
  128. */
  129. wmb();
  130. /* Mark the data descriptor to be watched */
  131. tx_buf->next_to_watch = tx_desc;
  132. writel(tx_ring->next_to_use, tx_ring->tail);
  133. return 0;
  134. dma_fail:
  135. return -1;
  136. }
  137. #define IP_HEADER_OFFSET 14
  138. #define I40E_UDPIP_DUMMY_PACKET_LEN 42
  139. /**
  140. * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
  141. * @vsi: pointer to the targeted VSI
  142. * @fd_data: the flow director data required for the FDir descriptor
  143. * @raw_packet: the pre-allocated packet buffer for FDir
  144. * @add: true adds a filter, false removes it
  145. *
  146. * Returns 0 if the filters were successfully added or removed
  147. **/
  148. static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
  149. struct i40e_fdir_filter *fd_data,
  150. u8 *raw_packet, bool add)
  151. {
  152. struct i40e_pf *pf = vsi->back;
  153. struct udphdr *udp;
  154. struct iphdr *ip;
  155. bool err = false;
  156. int ret;
  157. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  158. 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
  159. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  160. memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
  161. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  162. udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
  163. + sizeof(struct iphdr));
  164. ip->daddr = fd_data->dst_ip[0];
  165. udp->dest = fd_data->dst_port;
  166. ip->saddr = fd_data->src_ip[0];
  167. udp->source = fd_data->src_port;
  168. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  169. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  170. if (ret) {
  171. dev_info(&pf->pdev->dev,
  172. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  173. fd_data->pctype, ret);
  174. err = true;
  175. } else {
  176. dev_info(&pf->pdev->dev,
  177. "Filter OK for PCTYPE %d (ret = %d)\n",
  178. fd_data->pctype, ret);
  179. }
  180. return err ? -EOPNOTSUPP : 0;
  181. }
  182. #define I40E_TCPIP_DUMMY_PACKET_LEN 54
  183. /**
  184. * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
  185. * @vsi: pointer to the targeted VSI
  186. * @fd_data: the flow director data required for the FDir descriptor
  187. * @raw_packet: the pre-allocated packet buffer for FDir
  188. * @add: true adds a filter, false removes it
  189. *
  190. * Returns 0 if the filters were successfully added or removed
  191. **/
  192. static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
  193. struct i40e_fdir_filter *fd_data,
  194. u8 *raw_packet, bool add)
  195. {
  196. struct i40e_pf *pf = vsi->back;
  197. struct tcphdr *tcp;
  198. struct iphdr *ip;
  199. bool err = false;
  200. int ret;
  201. /* Dummy packet */
  202. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  203. 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
  204. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
  205. 0x0, 0x72, 0, 0, 0, 0};
  206. memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
  207. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  208. tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
  209. + sizeof(struct iphdr));
  210. ip->daddr = fd_data->dst_ip[0];
  211. tcp->dest = fd_data->dst_port;
  212. ip->saddr = fd_data->src_ip[0];
  213. tcp->source = fd_data->src_port;
  214. if (add) {
  215. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) {
  216. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
  217. pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  218. }
  219. }
  220. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  221. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  222. if (ret) {
  223. dev_info(&pf->pdev->dev,
  224. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  225. fd_data->pctype, ret);
  226. err = true;
  227. } else {
  228. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
  229. fd_data->pctype, ret);
  230. }
  231. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  232. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  233. if (ret) {
  234. dev_info(&pf->pdev->dev,
  235. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  236. fd_data->pctype, ret);
  237. err = true;
  238. } else {
  239. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d (ret = %d)\n",
  240. fd_data->pctype, ret);
  241. }
  242. return err ? -EOPNOTSUPP : 0;
  243. }
  244. /**
  245. * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
  246. * a specific flow spec
  247. * @vsi: pointer to the targeted VSI
  248. * @fd_data: the flow director data required for the FDir descriptor
  249. * @raw_packet: the pre-allocated packet buffer for FDir
  250. * @add: true adds a filter, false removes it
  251. *
  252. * Always returns -EOPNOTSUPP
  253. **/
  254. static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
  255. struct i40e_fdir_filter *fd_data,
  256. u8 *raw_packet, bool add)
  257. {
  258. return -EOPNOTSUPP;
  259. }
  260. #define I40E_IP_DUMMY_PACKET_LEN 34
  261. /**
  262. * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
  263. * a specific flow spec
  264. * @vsi: pointer to the targeted VSI
  265. * @fd_data: the flow director data required for the FDir descriptor
  266. * @raw_packet: the pre-allocated packet buffer for FDir
  267. * @add: true adds a filter, false removes it
  268. *
  269. * Returns 0 if the filters were successfully added or removed
  270. **/
  271. static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
  272. struct i40e_fdir_filter *fd_data,
  273. u8 *raw_packet, bool add)
  274. {
  275. struct i40e_pf *pf = vsi->back;
  276. struct iphdr *ip;
  277. bool err = false;
  278. int ret;
  279. int i;
  280. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  281. 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
  282. 0, 0, 0, 0};
  283. memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
  284. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  285. ip->saddr = fd_data->src_ip[0];
  286. ip->daddr = fd_data->dst_ip[0];
  287. ip->protocol = 0;
  288. for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  289. i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
  290. fd_data->pctype = i;
  291. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  292. if (ret) {
  293. dev_info(&pf->pdev->dev,
  294. "Filter command send failed for PCTYPE %d (ret = %d)\n",
  295. fd_data->pctype, ret);
  296. err = true;
  297. } else {
  298. dev_info(&pf->pdev->dev,
  299. "Filter OK for PCTYPE %d (ret = %d)\n",
  300. fd_data->pctype, ret);
  301. }
  302. }
  303. return err ? -EOPNOTSUPP : 0;
  304. }
  305. /**
  306. * i40e_add_del_fdir - Build raw packets to add/del fdir filter
  307. * @vsi: pointer to the targeted VSI
  308. * @cmd: command to get or set RX flow classification rules
  309. * @add: true adds a filter, false removes it
  310. *
  311. **/
  312. int i40e_add_del_fdir(struct i40e_vsi *vsi,
  313. struct i40e_fdir_filter *input, bool add)
  314. {
  315. struct i40e_pf *pf = vsi->back;
  316. u8 *raw_packet;
  317. int ret;
  318. /* Populate the Flow Director that we have at the moment
  319. * and allocate the raw packet buffer for the calling functions
  320. */
  321. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  322. if (!raw_packet)
  323. return -ENOMEM;
  324. switch (input->flow_type & ~FLOW_EXT) {
  325. case TCP_V4_FLOW:
  326. ret = i40e_add_del_fdir_tcpv4(vsi, input, raw_packet,
  327. add);
  328. break;
  329. case UDP_V4_FLOW:
  330. ret = i40e_add_del_fdir_udpv4(vsi, input, raw_packet,
  331. add);
  332. break;
  333. case SCTP_V4_FLOW:
  334. ret = i40e_add_del_fdir_sctpv4(vsi, input, raw_packet,
  335. add);
  336. break;
  337. case IPV4_FLOW:
  338. ret = i40e_add_del_fdir_ipv4(vsi, input, raw_packet,
  339. add);
  340. break;
  341. case IP_USER_FLOW:
  342. switch (input->ip4_proto) {
  343. case IPPROTO_TCP:
  344. ret = i40e_add_del_fdir_tcpv4(vsi, input,
  345. raw_packet, add);
  346. break;
  347. case IPPROTO_UDP:
  348. ret = i40e_add_del_fdir_udpv4(vsi, input,
  349. raw_packet, add);
  350. break;
  351. case IPPROTO_SCTP:
  352. ret = i40e_add_del_fdir_sctpv4(vsi, input,
  353. raw_packet, add);
  354. break;
  355. default:
  356. ret = i40e_add_del_fdir_ipv4(vsi, input,
  357. raw_packet, add);
  358. break;
  359. }
  360. break;
  361. default:
  362. dev_info(&pf->pdev->dev, "Could not specify spec type %d\n",
  363. input->flow_type);
  364. ret = -EINVAL;
  365. }
  366. kfree(raw_packet);
  367. return ret;
  368. }
  369. /**
  370. * i40e_fd_handle_status - check the Programming Status for FD
  371. * @rx_ring: the Rx ring for this descriptor
  372. * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
  373. * @prog_id: the id originally used for programming
  374. *
  375. * This is used to verify if the FD programming or invalidation
  376. * requested by SW to the HW is successful or not and take actions accordingly.
  377. **/
  378. static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
  379. union i40e_rx_desc *rx_desc, u8 prog_id)
  380. {
  381. struct i40e_pf *pf = rx_ring->vsi->back;
  382. struct pci_dev *pdev = pf->pdev;
  383. u32 fcnt_prog, fcnt_avail;
  384. u32 error;
  385. u64 qw;
  386. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  387. error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
  388. I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
  389. if (error == (0x1 << I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
  390. dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
  391. rx_desc->wb.qword0.hi_dword.fd_id);
  392. /* filter programming failed most likely due to table full */
  393. fcnt_prog = i40e_get_current_fd_count(pf);
  394. fcnt_avail = i40e_get_fd_cnt_all(pf);
  395. /* If ATR is running fcnt_prog can quickly change,
  396. * if we are very close to full, it makes sense to disable
  397. * FD ATR/SB and then re-enable it when there is room.
  398. */
  399. if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
  400. /* Turn off ATR first */
  401. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) {
  402. pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  403. dev_warn(&pdev->dev, "FD filter space full, ATR for further flows will be turned off\n");
  404. pf->auto_disable_flags |=
  405. I40E_FLAG_FD_ATR_ENABLED;
  406. pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
  407. } else if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  408. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  409. dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
  410. pf->auto_disable_flags |=
  411. I40E_FLAG_FD_SB_ENABLED;
  412. pf->flags |= I40E_FLAG_FDIR_REQUIRES_REINIT;
  413. }
  414. } else {
  415. dev_info(&pdev->dev, "FD filter programming error\n");
  416. }
  417. } else if (error ==
  418. (0x1 << I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
  419. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  420. dev_info(&pdev->dev, "ntuple filter loc = %d, could not be removed\n",
  421. rx_desc->wb.qword0.hi_dword.fd_id);
  422. }
  423. }
  424. /**
  425. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  426. * @ring: the ring that owns the buffer
  427. * @tx_buffer: the buffer to free
  428. **/
  429. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  430. struct i40e_tx_buffer *tx_buffer)
  431. {
  432. if (tx_buffer->skb) {
  433. dev_kfree_skb_any(tx_buffer->skb);
  434. if (dma_unmap_len(tx_buffer, len))
  435. dma_unmap_single(ring->dev,
  436. dma_unmap_addr(tx_buffer, dma),
  437. dma_unmap_len(tx_buffer, len),
  438. DMA_TO_DEVICE);
  439. } else if (dma_unmap_len(tx_buffer, len)) {
  440. dma_unmap_page(ring->dev,
  441. dma_unmap_addr(tx_buffer, dma),
  442. dma_unmap_len(tx_buffer, len),
  443. DMA_TO_DEVICE);
  444. }
  445. tx_buffer->next_to_watch = NULL;
  446. tx_buffer->skb = NULL;
  447. dma_unmap_len_set(tx_buffer, len, 0);
  448. /* tx_buffer must be completely set up in the transmit path */
  449. }
  450. /**
  451. * i40e_clean_tx_ring - Free any empty Tx buffers
  452. * @tx_ring: ring to be cleaned
  453. **/
  454. void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
  455. {
  456. unsigned long bi_size;
  457. u16 i;
  458. /* ring already cleared, nothing to do */
  459. if (!tx_ring->tx_bi)
  460. return;
  461. /* Free all the Tx ring sk_buffs */
  462. for (i = 0; i < tx_ring->count; i++)
  463. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  464. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  465. memset(tx_ring->tx_bi, 0, bi_size);
  466. /* Zero out the descriptor ring */
  467. memset(tx_ring->desc, 0, tx_ring->size);
  468. tx_ring->next_to_use = 0;
  469. tx_ring->next_to_clean = 0;
  470. if (!tx_ring->netdev)
  471. return;
  472. /* cleanup Tx queue statistics */
  473. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  474. tx_ring->queue_index));
  475. }
  476. /**
  477. * i40e_free_tx_resources - Free Tx resources per queue
  478. * @tx_ring: Tx descriptor ring for a specific queue
  479. *
  480. * Free all transmit software resources
  481. **/
  482. void i40e_free_tx_resources(struct i40e_ring *tx_ring)
  483. {
  484. i40e_clean_tx_ring(tx_ring);
  485. kfree(tx_ring->tx_bi);
  486. tx_ring->tx_bi = NULL;
  487. if (tx_ring->desc) {
  488. dma_free_coherent(tx_ring->dev, tx_ring->size,
  489. tx_ring->desc, tx_ring->dma);
  490. tx_ring->desc = NULL;
  491. }
  492. }
  493. /**
  494. * i40e_get_tx_pending - how many tx descriptors not processed
  495. * @tx_ring: the ring of descriptors
  496. *
  497. * Since there is no access to the ring head register
  498. * in XL710, we need to use our local copies
  499. **/
  500. static u32 i40e_get_tx_pending(struct i40e_ring *ring)
  501. {
  502. u32 ntu = ((ring->next_to_clean <= ring->next_to_use)
  503. ? ring->next_to_use
  504. : ring->next_to_use + ring->count);
  505. return ntu - ring->next_to_clean;
  506. }
  507. /**
  508. * i40e_check_tx_hang - Is there a hang in the Tx queue
  509. * @tx_ring: the ring of descriptors
  510. **/
  511. static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
  512. {
  513. u32 tx_pending = i40e_get_tx_pending(tx_ring);
  514. bool ret = false;
  515. clear_check_for_tx_hang(tx_ring);
  516. /* Check for a hung queue, but be thorough. This verifies
  517. * that a transmit has been completed since the previous
  518. * check AND there is at least one packet pending. The
  519. * ARMED bit is set to indicate a potential hang. The
  520. * bit is cleared if a pause frame is received to remove
  521. * false hang detection due to PFC or 802.3x frames. By
  522. * requiring this to fail twice we avoid races with
  523. * PFC clearing the ARMED bit and conditions where we
  524. * run the check_tx_hang logic with a transmit completion
  525. * pending but without time to complete it yet.
  526. */
  527. if ((tx_ring->tx_stats.tx_done_old == tx_ring->stats.packets) &&
  528. tx_pending) {
  529. /* make sure it is true for two checks in a row */
  530. ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
  531. &tx_ring->state);
  532. } else {
  533. /* update completed stats and disarm the hang check */
  534. tx_ring->tx_stats.tx_done_old = tx_ring->stats.packets;
  535. clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
  536. }
  537. return ret;
  538. }
  539. /**
  540. * i40e_get_head - Retrieve head from head writeback
  541. * @tx_ring: tx ring to fetch head of
  542. *
  543. * Returns value of Tx ring head based on value stored
  544. * in head write-back location
  545. **/
  546. static inline u32 i40e_get_head(struct i40e_ring *tx_ring)
  547. {
  548. void *head = (struct i40e_tx_desc *)tx_ring->desc + tx_ring->count;
  549. return le32_to_cpu(*(volatile __le32 *)head);
  550. }
  551. /**
  552. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  553. * @tx_ring: tx ring to clean
  554. * @budget: how many cleans we're allowed
  555. *
  556. * Returns true if there's any budget left (e.g. the clean is finished)
  557. **/
  558. static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
  559. {
  560. u16 i = tx_ring->next_to_clean;
  561. struct i40e_tx_buffer *tx_buf;
  562. struct i40e_tx_desc *tx_head;
  563. struct i40e_tx_desc *tx_desc;
  564. unsigned int total_packets = 0;
  565. unsigned int total_bytes = 0;
  566. tx_buf = &tx_ring->tx_bi[i];
  567. tx_desc = I40E_TX_DESC(tx_ring, i);
  568. i -= tx_ring->count;
  569. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  570. do {
  571. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  572. /* if next_to_watch is not set then there is no work pending */
  573. if (!eop_desc)
  574. break;
  575. /* prevent any other reads prior to eop_desc */
  576. read_barrier_depends();
  577. /* we have caught up to head, no work left to do */
  578. if (tx_head == tx_desc)
  579. break;
  580. /* clear next_to_watch to prevent false hangs */
  581. tx_buf->next_to_watch = NULL;
  582. /* update the statistics for this packet */
  583. total_bytes += tx_buf->bytecount;
  584. total_packets += tx_buf->gso_segs;
  585. /* free the skb */
  586. dev_kfree_skb_any(tx_buf->skb);
  587. /* unmap skb header data */
  588. dma_unmap_single(tx_ring->dev,
  589. dma_unmap_addr(tx_buf, dma),
  590. dma_unmap_len(tx_buf, len),
  591. DMA_TO_DEVICE);
  592. /* clear tx_buffer data */
  593. tx_buf->skb = NULL;
  594. dma_unmap_len_set(tx_buf, len, 0);
  595. /* unmap remaining buffers */
  596. while (tx_desc != eop_desc) {
  597. tx_buf++;
  598. tx_desc++;
  599. i++;
  600. if (unlikely(!i)) {
  601. i -= tx_ring->count;
  602. tx_buf = tx_ring->tx_bi;
  603. tx_desc = I40E_TX_DESC(tx_ring, 0);
  604. }
  605. /* unmap any remaining paged data */
  606. if (dma_unmap_len(tx_buf, len)) {
  607. dma_unmap_page(tx_ring->dev,
  608. dma_unmap_addr(tx_buf, dma),
  609. dma_unmap_len(tx_buf, len),
  610. DMA_TO_DEVICE);
  611. dma_unmap_len_set(tx_buf, len, 0);
  612. }
  613. }
  614. /* move us one more past the eop_desc for start of next pkt */
  615. tx_buf++;
  616. tx_desc++;
  617. i++;
  618. if (unlikely(!i)) {
  619. i -= tx_ring->count;
  620. tx_buf = tx_ring->tx_bi;
  621. tx_desc = I40E_TX_DESC(tx_ring, 0);
  622. }
  623. /* update budget accounting */
  624. budget--;
  625. } while (likely(budget));
  626. i += tx_ring->count;
  627. tx_ring->next_to_clean = i;
  628. u64_stats_update_begin(&tx_ring->syncp);
  629. tx_ring->stats.bytes += total_bytes;
  630. tx_ring->stats.packets += total_packets;
  631. u64_stats_update_end(&tx_ring->syncp);
  632. tx_ring->q_vector->tx.total_bytes += total_bytes;
  633. tx_ring->q_vector->tx.total_packets += total_packets;
  634. if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
  635. /* schedule immediate reset if we believe we hung */
  636. dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
  637. " VSI <%d>\n"
  638. " Tx Queue <%d>\n"
  639. " next_to_use <%x>\n"
  640. " next_to_clean <%x>\n",
  641. tx_ring->vsi->seid,
  642. tx_ring->queue_index,
  643. tx_ring->next_to_use, i);
  644. dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
  645. " time_stamp <%lx>\n"
  646. " jiffies <%lx>\n",
  647. tx_ring->tx_bi[i].time_stamp, jiffies);
  648. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  649. dev_info(tx_ring->dev,
  650. "tx hang detected on queue %d, resetting adapter\n",
  651. tx_ring->queue_index);
  652. tx_ring->netdev->netdev_ops->ndo_tx_timeout(tx_ring->netdev);
  653. /* the adapter is about to reset, no point in enabling stuff */
  654. return true;
  655. }
  656. netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
  657. tx_ring->queue_index),
  658. total_packets, total_bytes);
  659. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  660. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  661. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  662. /* Make sure that anybody stopping the queue after this
  663. * sees the new next_to_clean.
  664. */
  665. smp_mb();
  666. if (__netif_subqueue_stopped(tx_ring->netdev,
  667. tx_ring->queue_index) &&
  668. !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
  669. netif_wake_subqueue(tx_ring->netdev,
  670. tx_ring->queue_index);
  671. ++tx_ring->tx_stats.restart_queue;
  672. }
  673. }
  674. return budget > 0;
  675. }
  676. /**
  677. * i40e_set_new_dynamic_itr - Find new ITR level
  678. * @rc: structure containing ring performance data
  679. *
  680. * Stores a new ITR value based on packets and byte counts during
  681. * the last interrupt. The advantage of per interrupt computation
  682. * is faster updates and more accurate ITR for the current traffic
  683. * pattern. Constants in this function were computed based on
  684. * theoretical maximum wire speed and thresholds were set based on
  685. * testing data as well as attempting to minimize response time
  686. * while increasing bulk throughput.
  687. **/
  688. static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  689. {
  690. enum i40e_latency_range new_latency_range = rc->latency_range;
  691. u32 new_itr = rc->itr;
  692. int bytes_per_int;
  693. if (rc->total_packets == 0 || !rc->itr)
  694. return;
  695. /* simple throttlerate management
  696. * 0-10MB/s lowest (100000 ints/s)
  697. * 10-20MB/s low (20000 ints/s)
  698. * 20-1249MB/s bulk (8000 ints/s)
  699. */
  700. bytes_per_int = rc->total_bytes / rc->itr;
  701. switch (rc->itr) {
  702. case I40E_LOWEST_LATENCY:
  703. if (bytes_per_int > 10)
  704. new_latency_range = I40E_LOW_LATENCY;
  705. break;
  706. case I40E_LOW_LATENCY:
  707. if (bytes_per_int > 20)
  708. new_latency_range = I40E_BULK_LATENCY;
  709. else if (bytes_per_int <= 10)
  710. new_latency_range = I40E_LOWEST_LATENCY;
  711. break;
  712. case I40E_BULK_LATENCY:
  713. if (bytes_per_int <= 20)
  714. rc->latency_range = I40E_LOW_LATENCY;
  715. break;
  716. }
  717. switch (new_latency_range) {
  718. case I40E_LOWEST_LATENCY:
  719. new_itr = I40E_ITR_100K;
  720. break;
  721. case I40E_LOW_LATENCY:
  722. new_itr = I40E_ITR_20K;
  723. break;
  724. case I40E_BULK_LATENCY:
  725. new_itr = I40E_ITR_8K;
  726. break;
  727. default:
  728. break;
  729. }
  730. if (new_itr != rc->itr) {
  731. /* do an exponential smoothing */
  732. new_itr = (10 * new_itr * rc->itr) /
  733. ((9 * new_itr) + rc->itr);
  734. rc->itr = new_itr & I40E_MAX_ITR;
  735. }
  736. rc->total_bytes = 0;
  737. rc->total_packets = 0;
  738. }
  739. /**
  740. * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
  741. * @q_vector: the vector to adjust
  742. **/
  743. static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
  744. {
  745. u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
  746. struct i40e_hw *hw = &q_vector->vsi->back->hw;
  747. u32 reg_addr;
  748. u16 old_itr;
  749. reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1);
  750. old_itr = q_vector->rx.itr;
  751. i40e_set_new_dynamic_itr(&q_vector->rx);
  752. if (old_itr != q_vector->rx.itr)
  753. wr32(hw, reg_addr, q_vector->rx.itr);
  754. reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1);
  755. old_itr = q_vector->tx.itr;
  756. i40e_set_new_dynamic_itr(&q_vector->tx);
  757. if (old_itr != q_vector->tx.itr)
  758. wr32(hw, reg_addr, q_vector->tx.itr);
  759. }
  760. /**
  761. * i40e_clean_programming_status - clean the programming status descriptor
  762. * @rx_ring: the rx ring that has this descriptor
  763. * @rx_desc: the rx descriptor written back by HW
  764. *
  765. * Flow director should handle FD_FILTER_STATUS to check its filter programming
  766. * status being successful or not and take actions accordingly. FCoE should
  767. * handle its context/filter programming/invalidation status and take actions.
  768. *
  769. **/
  770. static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
  771. union i40e_rx_desc *rx_desc)
  772. {
  773. u64 qw;
  774. u8 id;
  775. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  776. id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
  777. I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
  778. if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
  779. i40e_fd_handle_status(rx_ring, rx_desc, id);
  780. }
  781. /**
  782. * i40e_setup_tx_descriptors - Allocate the Tx descriptors
  783. * @tx_ring: the tx ring to set up
  784. *
  785. * Return 0 on success, negative on error
  786. **/
  787. int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
  788. {
  789. struct device *dev = tx_ring->dev;
  790. int bi_size;
  791. if (!dev)
  792. return -ENOMEM;
  793. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  794. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  795. if (!tx_ring->tx_bi)
  796. goto err;
  797. /* round up to nearest 4K */
  798. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  799. /* add u32 for head writeback, align after this takes care of
  800. * guaranteeing this is at least one cache line in size
  801. */
  802. tx_ring->size += sizeof(u32);
  803. tx_ring->size = ALIGN(tx_ring->size, 4096);
  804. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  805. &tx_ring->dma, GFP_KERNEL);
  806. if (!tx_ring->desc) {
  807. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  808. tx_ring->size);
  809. goto err;
  810. }
  811. tx_ring->next_to_use = 0;
  812. tx_ring->next_to_clean = 0;
  813. return 0;
  814. err:
  815. kfree(tx_ring->tx_bi);
  816. tx_ring->tx_bi = NULL;
  817. return -ENOMEM;
  818. }
  819. /**
  820. * i40e_clean_rx_ring - Free Rx buffers
  821. * @rx_ring: ring to be cleaned
  822. **/
  823. void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
  824. {
  825. struct device *dev = rx_ring->dev;
  826. struct i40e_rx_buffer *rx_bi;
  827. unsigned long bi_size;
  828. u16 i;
  829. /* ring already cleared, nothing to do */
  830. if (!rx_ring->rx_bi)
  831. return;
  832. /* Free all the Rx ring sk_buffs */
  833. for (i = 0; i < rx_ring->count; i++) {
  834. rx_bi = &rx_ring->rx_bi[i];
  835. if (rx_bi->dma) {
  836. dma_unmap_single(dev,
  837. rx_bi->dma,
  838. rx_ring->rx_buf_len,
  839. DMA_FROM_DEVICE);
  840. rx_bi->dma = 0;
  841. }
  842. if (rx_bi->skb) {
  843. dev_kfree_skb(rx_bi->skb);
  844. rx_bi->skb = NULL;
  845. }
  846. if (rx_bi->page) {
  847. if (rx_bi->page_dma) {
  848. dma_unmap_page(dev,
  849. rx_bi->page_dma,
  850. PAGE_SIZE / 2,
  851. DMA_FROM_DEVICE);
  852. rx_bi->page_dma = 0;
  853. }
  854. __free_page(rx_bi->page);
  855. rx_bi->page = NULL;
  856. rx_bi->page_offset = 0;
  857. }
  858. }
  859. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  860. memset(rx_ring->rx_bi, 0, bi_size);
  861. /* Zero out the descriptor ring */
  862. memset(rx_ring->desc, 0, rx_ring->size);
  863. rx_ring->next_to_clean = 0;
  864. rx_ring->next_to_use = 0;
  865. }
  866. /**
  867. * i40e_free_rx_resources - Free Rx resources
  868. * @rx_ring: ring to clean the resources from
  869. *
  870. * Free all receive software resources
  871. **/
  872. void i40e_free_rx_resources(struct i40e_ring *rx_ring)
  873. {
  874. i40e_clean_rx_ring(rx_ring);
  875. kfree(rx_ring->rx_bi);
  876. rx_ring->rx_bi = NULL;
  877. if (rx_ring->desc) {
  878. dma_free_coherent(rx_ring->dev, rx_ring->size,
  879. rx_ring->desc, rx_ring->dma);
  880. rx_ring->desc = NULL;
  881. }
  882. }
  883. /**
  884. * i40e_setup_rx_descriptors - Allocate Rx descriptors
  885. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  886. *
  887. * Returns 0 on success, negative on failure
  888. **/
  889. int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
  890. {
  891. struct device *dev = rx_ring->dev;
  892. int bi_size;
  893. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  894. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  895. if (!rx_ring->rx_bi)
  896. goto err;
  897. /* Round up to nearest 4K */
  898. rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
  899. ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
  900. : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  901. rx_ring->size = ALIGN(rx_ring->size, 4096);
  902. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  903. &rx_ring->dma, GFP_KERNEL);
  904. if (!rx_ring->desc) {
  905. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  906. rx_ring->size);
  907. goto err;
  908. }
  909. rx_ring->next_to_clean = 0;
  910. rx_ring->next_to_use = 0;
  911. return 0;
  912. err:
  913. kfree(rx_ring->rx_bi);
  914. rx_ring->rx_bi = NULL;
  915. return -ENOMEM;
  916. }
  917. /**
  918. * i40e_release_rx_desc - Store the new tail and head values
  919. * @rx_ring: ring to bump
  920. * @val: new head index
  921. **/
  922. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  923. {
  924. rx_ring->next_to_use = val;
  925. /* Force memory writes to complete before letting h/w
  926. * know there are new descriptors to fetch. (Only
  927. * applicable for weak-ordered memory model archs,
  928. * such as IA-64).
  929. */
  930. wmb();
  931. writel(val, rx_ring->tail);
  932. }
  933. /**
  934. * i40e_alloc_rx_buffers - Replace used receive buffers; packet split
  935. * @rx_ring: ring to place buffers on
  936. * @cleaned_count: number of buffers to replace
  937. **/
  938. void i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  939. {
  940. u16 i = rx_ring->next_to_use;
  941. union i40e_rx_desc *rx_desc;
  942. struct i40e_rx_buffer *bi;
  943. struct sk_buff *skb;
  944. /* do nothing if no valid netdev defined */
  945. if (!rx_ring->netdev || !cleaned_count)
  946. return;
  947. while (cleaned_count--) {
  948. rx_desc = I40E_RX_DESC(rx_ring, i);
  949. bi = &rx_ring->rx_bi[i];
  950. skb = bi->skb;
  951. if (!skb) {
  952. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  953. rx_ring->rx_buf_len);
  954. if (!skb) {
  955. rx_ring->rx_stats.alloc_buff_failed++;
  956. goto no_buffers;
  957. }
  958. /* initialize queue mapping */
  959. skb_record_rx_queue(skb, rx_ring->queue_index);
  960. bi->skb = skb;
  961. }
  962. if (!bi->dma) {
  963. bi->dma = dma_map_single(rx_ring->dev,
  964. skb->data,
  965. rx_ring->rx_buf_len,
  966. DMA_FROM_DEVICE);
  967. if (dma_mapping_error(rx_ring->dev, bi->dma)) {
  968. rx_ring->rx_stats.alloc_buff_failed++;
  969. bi->dma = 0;
  970. goto no_buffers;
  971. }
  972. }
  973. if (ring_is_ps_enabled(rx_ring)) {
  974. if (!bi->page) {
  975. bi->page = alloc_page(GFP_ATOMIC);
  976. if (!bi->page) {
  977. rx_ring->rx_stats.alloc_page_failed++;
  978. goto no_buffers;
  979. }
  980. }
  981. if (!bi->page_dma) {
  982. /* use a half page if we're re-using */
  983. bi->page_offset ^= PAGE_SIZE / 2;
  984. bi->page_dma = dma_map_page(rx_ring->dev,
  985. bi->page,
  986. bi->page_offset,
  987. PAGE_SIZE / 2,
  988. DMA_FROM_DEVICE);
  989. if (dma_mapping_error(rx_ring->dev,
  990. bi->page_dma)) {
  991. rx_ring->rx_stats.alloc_page_failed++;
  992. bi->page_dma = 0;
  993. goto no_buffers;
  994. }
  995. }
  996. /* Refresh the desc even if buffer_addrs didn't change
  997. * because each write-back erases this info.
  998. */
  999. rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
  1000. rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
  1001. } else {
  1002. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
  1003. rx_desc->read.hdr_addr = 0;
  1004. }
  1005. i++;
  1006. if (i == rx_ring->count)
  1007. i = 0;
  1008. }
  1009. no_buffers:
  1010. if (rx_ring->next_to_use != i)
  1011. i40e_release_rx_desc(rx_ring, i);
  1012. }
  1013. /**
  1014. * i40e_receive_skb - Send a completed packet up the stack
  1015. * @rx_ring: rx ring in play
  1016. * @skb: packet to send up
  1017. * @vlan_tag: vlan tag for packet
  1018. **/
  1019. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  1020. struct sk_buff *skb, u16 vlan_tag)
  1021. {
  1022. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  1023. struct i40e_vsi *vsi = rx_ring->vsi;
  1024. u64 flags = vsi->back->flags;
  1025. if (vlan_tag & VLAN_VID_MASK)
  1026. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  1027. if (flags & I40E_FLAG_IN_NETPOLL)
  1028. netif_rx(skb);
  1029. else
  1030. napi_gro_receive(&q_vector->napi, skb);
  1031. }
  1032. /**
  1033. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  1034. * @vsi: the VSI we care about
  1035. * @skb: skb currently being received and modified
  1036. * @rx_status: status value of last descriptor in packet
  1037. * @rx_error: error value of last descriptor in packet
  1038. * @rx_ptype: ptype value of last descriptor in packet
  1039. **/
  1040. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  1041. struct sk_buff *skb,
  1042. u32 rx_status,
  1043. u32 rx_error,
  1044. u16 rx_ptype)
  1045. {
  1046. bool ipv4_tunnel, ipv6_tunnel;
  1047. __wsum rx_udp_csum;
  1048. __sum16 csum;
  1049. struct iphdr *iph;
  1050. ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
  1051. (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
  1052. ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
  1053. (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
  1054. skb->encapsulation = ipv4_tunnel || ipv6_tunnel;
  1055. skb->ip_summed = CHECKSUM_NONE;
  1056. /* Rx csum enabled and ip headers found? */
  1057. if (!(vsi->netdev->features & NETIF_F_RXCSUM &&
  1058. rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  1059. return;
  1060. /* likely incorrect csum if alternate IP extension headers found */
  1061. if (rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  1062. return;
  1063. /* IP or L4 or outmost IP checksum error */
  1064. if (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
  1065. (1 << I40E_RX_DESC_ERROR_L4E_SHIFT) |
  1066. (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))) {
  1067. vsi->back->hw_csum_rx_error++;
  1068. return;
  1069. }
  1070. if (ipv4_tunnel &&
  1071. !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
  1072. /* If VXLAN traffic has an outer UDPv4 checksum we need to check
  1073. * it in the driver, hardware does not do it for us.
  1074. * Since L3L4P bit was set we assume a valid IHL value (>=5)
  1075. * so the total length of IPv4 header is IHL*4 bytes
  1076. */
  1077. skb->transport_header = skb->mac_header +
  1078. sizeof(struct ethhdr) +
  1079. (ip_hdr(skb)->ihl * 4);
  1080. /* Add 4 bytes for VLAN tagged packets */
  1081. skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
  1082. skb->protocol == htons(ETH_P_8021AD))
  1083. ? VLAN_HLEN : 0;
  1084. rx_udp_csum = udp_csum(skb);
  1085. iph = ip_hdr(skb);
  1086. csum = csum_tcpudp_magic(
  1087. iph->saddr, iph->daddr,
  1088. (skb->len - skb_transport_offset(skb)),
  1089. IPPROTO_UDP, rx_udp_csum);
  1090. if (udp_hdr(skb)->check != csum) {
  1091. vsi->back->hw_csum_rx_error++;
  1092. return;
  1093. }
  1094. }
  1095. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1096. }
  1097. /**
  1098. * i40e_rx_hash - returns the hash value from the Rx descriptor
  1099. * @ring: descriptor ring
  1100. * @rx_desc: specific descriptor
  1101. **/
  1102. static inline u32 i40e_rx_hash(struct i40e_ring *ring,
  1103. union i40e_rx_desc *rx_desc)
  1104. {
  1105. const __le64 rss_mask =
  1106. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  1107. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  1108. if ((ring->netdev->features & NETIF_F_RXHASH) &&
  1109. (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
  1110. return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  1111. else
  1112. return 0;
  1113. }
  1114. /**
  1115. * i40e_ptype_to_hash - get a hash type
  1116. * @ptype: the ptype value from the descriptor
  1117. *
  1118. * Returns a hash type to be used by skb_set_hash
  1119. **/
  1120. static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
  1121. {
  1122. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  1123. if (!decoded.known)
  1124. return PKT_HASH_TYPE_NONE;
  1125. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1126. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  1127. return PKT_HASH_TYPE_L4;
  1128. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1129. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  1130. return PKT_HASH_TYPE_L3;
  1131. else
  1132. return PKT_HASH_TYPE_L2;
  1133. }
  1134. /**
  1135. * i40e_clean_rx_irq - Reclaim resources after receive completes
  1136. * @rx_ring: rx ring to clean
  1137. * @budget: how many cleans we're allowed
  1138. *
  1139. * Returns true if there's any budget left (e.g. the clean is finished)
  1140. **/
  1141. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1142. {
  1143. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1144. u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
  1145. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1146. const int current_node = numa_node_id();
  1147. struct i40e_vsi *vsi = rx_ring->vsi;
  1148. u16 i = rx_ring->next_to_clean;
  1149. union i40e_rx_desc *rx_desc;
  1150. u32 rx_error, rx_status;
  1151. u8 rx_ptype;
  1152. u64 qword;
  1153. if (budget <= 0)
  1154. return 0;
  1155. rx_desc = I40E_RX_DESC(rx_ring, i);
  1156. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1157. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1158. I40E_RXD_QW1_STATUS_SHIFT;
  1159. while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
  1160. union i40e_rx_desc *next_rxd;
  1161. struct i40e_rx_buffer *rx_bi;
  1162. struct sk_buff *skb;
  1163. u16 vlan_tag;
  1164. if (i40e_rx_is_programming_status(qword)) {
  1165. i40e_clean_programming_status(rx_ring, rx_desc);
  1166. I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
  1167. goto next_desc;
  1168. }
  1169. rx_bi = &rx_ring->rx_bi[i];
  1170. skb = rx_bi->skb;
  1171. prefetch(skb->data);
  1172. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1173. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1174. rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
  1175. I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
  1176. rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
  1177. I40E_RXD_QW1_LENGTH_SPH_SHIFT;
  1178. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1179. I40E_RXD_QW1_ERROR_SHIFT;
  1180. rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  1181. rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  1182. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1183. I40E_RXD_QW1_PTYPE_SHIFT;
  1184. rx_bi->skb = NULL;
  1185. /* This memory barrier is needed to keep us from reading
  1186. * any other fields out of the rx_desc until we know the
  1187. * STATUS_DD bit is set
  1188. */
  1189. rmb();
  1190. /* Get the header and possibly the whole packet
  1191. * If this is an skb from previous receive dma will be 0
  1192. */
  1193. if (rx_bi->dma) {
  1194. u16 len;
  1195. if (rx_hbo)
  1196. len = I40E_RX_HDR_SIZE;
  1197. else if (rx_sph)
  1198. len = rx_header_len;
  1199. else if (rx_packet_len)
  1200. len = rx_packet_len; /* 1buf/no split found */
  1201. else
  1202. len = rx_header_len; /* split always mode */
  1203. skb_put(skb, len);
  1204. dma_unmap_single(rx_ring->dev,
  1205. rx_bi->dma,
  1206. rx_ring->rx_buf_len,
  1207. DMA_FROM_DEVICE);
  1208. rx_bi->dma = 0;
  1209. }
  1210. /* Get the rest of the data if this was a header split */
  1211. if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
  1212. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  1213. rx_bi->page,
  1214. rx_bi->page_offset,
  1215. rx_packet_len);
  1216. skb->len += rx_packet_len;
  1217. skb->data_len += rx_packet_len;
  1218. skb->truesize += rx_packet_len;
  1219. if ((page_count(rx_bi->page) == 1) &&
  1220. (page_to_nid(rx_bi->page) == current_node))
  1221. get_page(rx_bi->page);
  1222. else
  1223. rx_bi->page = NULL;
  1224. dma_unmap_page(rx_ring->dev,
  1225. rx_bi->page_dma,
  1226. PAGE_SIZE / 2,
  1227. DMA_FROM_DEVICE);
  1228. rx_bi->page_dma = 0;
  1229. }
  1230. I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
  1231. if (unlikely(
  1232. !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  1233. struct i40e_rx_buffer *next_buffer;
  1234. next_buffer = &rx_ring->rx_bi[i];
  1235. if (ring_is_ps_enabled(rx_ring)) {
  1236. rx_bi->skb = next_buffer->skb;
  1237. rx_bi->dma = next_buffer->dma;
  1238. next_buffer->skb = skb;
  1239. next_buffer->dma = 0;
  1240. }
  1241. rx_ring->rx_stats.non_eop_descs++;
  1242. goto next_desc;
  1243. }
  1244. /* ERR_MASK will only have valid bits if EOP set */
  1245. if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  1246. dev_kfree_skb_any(skb);
  1247. goto next_desc;
  1248. }
  1249. skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
  1250. i40e_ptype_to_hash(rx_ptype));
  1251. if (unlikely(rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK)) {
  1252. i40e_ptp_rx_hwtstamp(vsi->back, skb, (rx_status &
  1253. I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
  1254. I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT);
  1255. rx_ring->last_rx_timestamp = jiffies;
  1256. }
  1257. /* probably a little skewed due to removing CRC */
  1258. total_rx_bytes += skb->len;
  1259. total_rx_packets++;
  1260. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1261. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  1262. vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  1263. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  1264. : 0;
  1265. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1266. rx_ring->netdev->last_rx = jiffies;
  1267. budget--;
  1268. next_desc:
  1269. rx_desc->wb.qword1.status_error_len = 0;
  1270. if (!budget)
  1271. break;
  1272. cleaned_count++;
  1273. /* return some buffers to hardware, one at a time is too slow */
  1274. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1275. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  1276. cleaned_count = 0;
  1277. }
  1278. /* use prefetched values */
  1279. rx_desc = next_rxd;
  1280. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1281. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1282. I40E_RXD_QW1_STATUS_SHIFT;
  1283. }
  1284. rx_ring->next_to_clean = i;
  1285. u64_stats_update_begin(&rx_ring->syncp);
  1286. rx_ring->stats.packets += total_rx_packets;
  1287. rx_ring->stats.bytes += total_rx_bytes;
  1288. u64_stats_update_end(&rx_ring->syncp);
  1289. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1290. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1291. if (cleaned_count)
  1292. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  1293. return budget > 0;
  1294. }
  1295. /**
  1296. * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
  1297. * @napi: napi struct with our devices info in it
  1298. * @budget: amount of work driver is allowed to do this pass, in packets
  1299. *
  1300. * This function will clean all queues associated with a q_vector.
  1301. *
  1302. * Returns the amount of work done
  1303. **/
  1304. int i40e_napi_poll(struct napi_struct *napi, int budget)
  1305. {
  1306. struct i40e_q_vector *q_vector =
  1307. container_of(napi, struct i40e_q_vector, napi);
  1308. struct i40e_vsi *vsi = q_vector->vsi;
  1309. struct i40e_ring *ring;
  1310. bool clean_complete = true;
  1311. int budget_per_ring;
  1312. if (test_bit(__I40E_DOWN, &vsi->state)) {
  1313. napi_complete(napi);
  1314. return 0;
  1315. }
  1316. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1317. * budget and be more aggressive about cleaning up the Tx descriptors.
  1318. */
  1319. i40e_for_each_ring(ring, q_vector->tx)
  1320. clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
  1321. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1322. * allow the budget to go below 1 because that would exit polling early.
  1323. */
  1324. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1325. i40e_for_each_ring(ring, q_vector->rx)
  1326. clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
  1327. /* If work not completed, return budget and polling will return */
  1328. if (!clean_complete)
  1329. return budget;
  1330. /* Work is done so exit the polling mode and re-enable the interrupt */
  1331. napi_complete(napi);
  1332. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
  1333. ITR_IS_DYNAMIC(vsi->tx_itr_setting))
  1334. i40e_update_dynamic_itr(q_vector);
  1335. if (!test_bit(__I40E_DOWN, &vsi->state)) {
  1336. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  1337. i40e_irq_dynamic_enable(vsi,
  1338. q_vector->v_idx + vsi->base_vector);
  1339. } else {
  1340. struct i40e_hw *hw = &vsi->back->hw;
  1341. /* We re-enable the queue 0 cause, but
  1342. * don't worry about dynamic_enable
  1343. * because we left it on for the other
  1344. * possible interrupts during napi
  1345. */
  1346. u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
  1347. qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  1348. wr32(hw, I40E_QINT_RQCTL(0), qval);
  1349. qval = rd32(hw, I40E_QINT_TQCTL(0));
  1350. qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  1351. wr32(hw, I40E_QINT_TQCTL(0), qval);
  1352. i40e_irq_dynamic_enable_icr0(vsi->back);
  1353. }
  1354. }
  1355. return 0;
  1356. }
  1357. /**
  1358. * i40e_atr - Add a Flow Director ATR filter
  1359. * @tx_ring: ring to add programming descriptor to
  1360. * @skb: send buffer
  1361. * @flags: send flags
  1362. * @protocol: wire protocol
  1363. **/
  1364. static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1365. u32 flags, __be16 protocol)
  1366. {
  1367. struct i40e_filter_program_desc *fdir_desc;
  1368. struct i40e_pf *pf = tx_ring->vsi->back;
  1369. union {
  1370. unsigned char *network;
  1371. struct iphdr *ipv4;
  1372. struct ipv6hdr *ipv6;
  1373. } hdr;
  1374. struct tcphdr *th;
  1375. unsigned int hlen;
  1376. u32 flex_ptype, dtype_cmd;
  1377. u16 i;
  1378. /* make sure ATR is enabled */
  1379. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  1380. return;
  1381. /* if sampling is disabled do nothing */
  1382. if (!tx_ring->atr_sample_rate)
  1383. return;
  1384. /* snag network header to get L4 type and address */
  1385. hdr.network = skb_network_header(skb);
  1386. /* Currently only IPv4/IPv6 with TCP is supported */
  1387. if (protocol == htons(ETH_P_IP)) {
  1388. if (hdr.ipv4->protocol != IPPROTO_TCP)
  1389. return;
  1390. /* access ihl as a u8 to avoid unaligned access on ia64 */
  1391. hlen = (hdr.network[0] & 0x0F) << 2;
  1392. } else if (protocol == htons(ETH_P_IPV6)) {
  1393. if (hdr.ipv6->nexthdr != IPPROTO_TCP)
  1394. return;
  1395. hlen = sizeof(struct ipv6hdr);
  1396. } else {
  1397. return;
  1398. }
  1399. th = (struct tcphdr *)(hdr.network + hlen);
  1400. /* Due to lack of space, no more new filters can be programmed */
  1401. if (th->syn && (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
  1402. return;
  1403. tx_ring->atr_count++;
  1404. /* sample on all syn/fin/rst packets or once every atr sample rate */
  1405. if (!th->fin &&
  1406. !th->syn &&
  1407. !th->rst &&
  1408. (tx_ring->atr_count < tx_ring->atr_sample_rate))
  1409. return;
  1410. tx_ring->atr_count = 0;
  1411. /* grab the next descriptor */
  1412. i = tx_ring->next_to_use;
  1413. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  1414. i++;
  1415. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1416. flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  1417. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  1418. flex_ptype |= (protocol == htons(ETH_P_IP)) ?
  1419. (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
  1420. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
  1421. (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
  1422. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  1423. flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  1424. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  1425. dtype_cmd |= (th->fin || th->rst) ?
  1426. (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  1427. I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
  1428. (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  1429. I40E_TXD_FLTR_QW1_PCMD_SHIFT);
  1430. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
  1431. I40E_TXD_FLTR_QW1_DEST_SHIFT;
  1432. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
  1433. I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
  1434. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  1435. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  1436. }
  1437. /**
  1438. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1439. * @skb: send buffer
  1440. * @tx_ring: ring to send buffer on
  1441. * @flags: the tx flags to be set
  1442. *
  1443. * Checks the skb and set up correspondingly several generic transmit flags
  1444. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1445. *
  1446. * Returns error code indicate the frame should be dropped upon error and the
  1447. * otherwise returns 0 to indicate the flags has been set properly.
  1448. **/
  1449. static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  1450. struct i40e_ring *tx_ring,
  1451. u32 *flags)
  1452. {
  1453. __be16 protocol = skb->protocol;
  1454. u32 tx_flags = 0;
  1455. /* if we have a HW VLAN tag being added, default to the HW one */
  1456. if (vlan_tx_tag_present(skb)) {
  1457. tx_flags |= vlan_tx_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1458. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1459. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1460. } else if (protocol == htons(ETH_P_8021Q)) {
  1461. struct vlan_hdr *vhdr, _vhdr;
  1462. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1463. if (!vhdr)
  1464. return -EINVAL;
  1465. protocol = vhdr->h_vlan_encapsulated_proto;
  1466. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1467. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1468. }
  1469. /* Insert 802.1p priority into VLAN header */
  1470. if ((tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED) &&
  1471. ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
  1472. (skb->priority != TC_PRIO_CONTROL))) {
  1473. tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
  1474. tx_flags |= (skb->priority & 0x7) <<
  1475. I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
  1476. if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
  1477. struct vlan_ethhdr *vhdr;
  1478. int rc;
  1479. rc = skb_cow_head(skb, 0);
  1480. if (rc < 0)
  1481. return rc;
  1482. vhdr = (struct vlan_ethhdr *)skb->data;
  1483. vhdr->h_vlan_TCI = htons(tx_flags >>
  1484. I40E_TX_FLAGS_VLAN_SHIFT);
  1485. } else {
  1486. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1487. }
  1488. }
  1489. *flags = tx_flags;
  1490. return 0;
  1491. }
  1492. /**
  1493. * i40e_tso - set up the tso context descriptor
  1494. * @tx_ring: ptr to the ring to send
  1495. * @skb: ptr to the skb we're sending
  1496. * @tx_flags: the collected send information
  1497. * @protocol: the send protocol
  1498. * @hdr_len: ptr to the size of the packet header
  1499. * @cd_tunneling: ptr to context descriptor bits
  1500. *
  1501. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1502. **/
  1503. static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1504. u32 tx_flags, __be16 protocol, u8 *hdr_len,
  1505. u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
  1506. {
  1507. u32 cd_cmd, cd_tso_len, cd_mss;
  1508. struct ipv6hdr *ipv6h;
  1509. struct tcphdr *tcph;
  1510. struct iphdr *iph;
  1511. u32 l4len;
  1512. int err;
  1513. if (!skb_is_gso(skb))
  1514. return 0;
  1515. err = skb_cow_head(skb, 0);
  1516. if (err < 0)
  1517. return err;
  1518. if (protocol == htons(ETH_P_IP)) {
  1519. iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
  1520. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1521. iph->tot_len = 0;
  1522. iph->check = 0;
  1523. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1524. 0, IPPROTO_TCP, 0);
  1525. } else if (skb_is_gso_v6(skb)) {
  1526. ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb)
  1527. : ipv6_hdr(skb);
  1528. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1529. ipv6h->payload_len = 0;
  1530. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  1531. 0, IPPROTO_TCP, 0);
  1532. }
  1533. l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
  1534. *hdr_len = (skb->encapsulation
  1535. ? (skb_inner_transport_header(skb) - skb->data)
  1536. : skb_transport_offset(skb)) + l4len;
  1537. /* find the field values */
  1538. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1539. cd_tso_len = skb->len - *hdr_len;
  1540. cd_mss = skb_shinfo(skb)->gso_size;
  1541. *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1542. ((u64)cd_tso_len <<
  1543. I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1544. ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1545. return 1;
  1546. }
  1547. /**
  1548. * i40e_tsyn - set up the tsyn context descriptor
  1549. * @tx_ring: ptr to the ring to send
  1550. * @skb: ptr to the skb we're sending
  1551. * @tx_flags: the collected send information
  1552. *
  1553. * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
  1554. **/
  1555. static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1556. u32 tx_flags, u64 *cd_type_cmd_tso_mss)
  1557. {
  1558. struct i40e_pf *pf;
  1559. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  1560. return 0;
  1561. /* Tx timestamps cannot be sampled when doing TSO */
  1562. if (tx_flags & I40E_TX_FLAGS_TSO)
  1563. return 0;
  1564. /* only timestamp the outbound packet if the user has requested it and
  1565. * we are not already transmitting a packet to be timestamped
  1566. */
  1567. pf = i40e_netdev_to_pf(tx_ring->netdev);
  1568. if (pf->ptp_tx && !pf->ptp_tx_skb) {
  1569. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1570. pf->ptp_tx_skb = skb_get(skb);
  1571. } else {
  1572. return 0;
  1573. }
  1574. *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
  1575. I40E_TXD_CTX_QW1_CMD_SHIFT;
  1576. return 1;
  1577. }
  1578. /**
  1579. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1580. * @skb: send buffer
  1581. * @tx_flags: Tx flags currently set
  1582. * @td_cmd: Tx descriptor command bits to set
  1583. * @td_offset: Tx descriptor header offsets to set
  1584. * @cd_tunneling: ptr to context desc bits
  1585. **/
  1586. static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
  1587. u32 *td_cmd, u32 *td_offset,
  1588. struct i40e_ring *tx_ring,
  1589. u32 *cd_tunneling)
  1590. {
  1591. struct ipv6hdr *this_ipv6_hdr;
  1592. unsigned int this_tcp_hdrlen;
  1593. struct iphdr *this_ip_hdr;
  1594. u32 network_hdr_len;
  1595. u8 l4_hdr = 0;
  1596. if (skb->encapsulation) {
  1597. network_hdr_len = skb_inner_network_header_len(skb);
  1598. this_ip_hdr = inner_ip_hdr(skb);
  1599. this_ipv6_hdr = inner_ipv6_hdr(skb);
  1600. this_tcp_hdrlen = inner_tcp_hdrlen(skb);
  1601. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1602. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1603. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
  1604. ip_hdr(skb)->check = 0;
  1605. } else {
  1606. *cd_tunneling |=
  1607. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1608. }
  1609. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1610. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1611. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
  1612. ip_hdr(skb)->check = 0;
  1613. } else {
  1614. *cd_tunneling |=
  1615. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1616. }
  1617. }
  1618. /* Now set the ctx descriptor fields */
  1619. *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
  1620. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
  1621. I40E_TXD_CTX_UDP_TUNNELING |
  1622. ((skb_inner_network_offset(skb) -
  1623. skb_transport_offset(skb)) >> 1) <<
  1624. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1625. } else {
  1626. network_hdr_len = skb_network_header_len(skb);
  1627. this_ip_hdr = ip_hdr(skb);
  1628. this_ipv6_hdr = ipv6_hdr(skb);
  1629. this_tcp_hdrlen = tcp_hdrlen(skb);
  1630. }
  1631. /* Enable IP checksum offloads */
  1632. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1633. l4_hdr = this_ip_hdr->protocol;
  1634. /* the stack computes the IP header already, the only time we
  1635. * need the hardware to recompute it is in the case of TSO.
  1636. */
  1637. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1638. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
  1639. this_ip_hdr->check = 0;
  1640. } else {
  1641. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
  1642. }
  1643. /* Now set the td_offset for IP header length */
  1644. *td_offset = (network_hdr_len >> 2) <<
  1645. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1646. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1647. l4_hdr = this_ipv6_hdr->nexthdr;
  1648. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1649. /* Now set the td_offset for IP header length */
  1650. *td_offset = (network_hdr_len >> 2) <<
  1651. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1652. }
  1653. /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
  1654. *td_offset |= (skb_network_offset(skb) >> 1) <<
  1655. I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1656. /* Enable L4 checksum offloads */
  1657. switch (l4_hdr) {
  1658. case IPPROTO_TCP:
  1659. /* enable checksum offloads */
  1660. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1661. *td_offset |= (this_tcp_hdrlen >> 2) <<
  1662. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1663. break;
  1664. case IPPROTO_SCTP:
  1665. /* enable SCTP checksum offload */
  1666. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1667. *td_offset |= (sizeof(struct sctphdr) >> 2) <<
  1668. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1669. break;
  1670. case IPPROTO_UDP:
  1671. /* enable UDP checksum offload */
  1672. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1673. *td_offset |= (sizeof(struct udphdr) >> 2) <<
  1674. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1675. break;
  1676. default:
  1677. break;
  1678. }
  1679. }
  1680. /**
  1681. * i40e_create_tx_ctx Build the Tx context descriptor
  1682. * @tx_ring: ring to create the descriptor on
  1683. * @cd_type_cmd_tso_mss: Quad Word 1
  1684. * @cd_tunneling: Quad Word 0 - bits 0-31
  1685. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1686. **/
  1687. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1688. const u64 cd_type_cmd_tso_mss,
  1689. const u32 cd_tunneling, const u32 cd_l2tag2)
  1690. {
  1691. struct i40e_tx_context_desc *context_desc;
  1692. int i = tx_ring->next_to_use;
  1693. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1694. !cd_tunneling && !cd_l2tag2)
  1695. return;
  1696. /* grab the next descriptor */
  1697. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1698. i++;
  1699. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1700. /* cpu_to_le32 and assign to struct fields */
  1701. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1702. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1703. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1704. }
  1705. /**
  1706. * i40e_tx_map - Build the Tx descriptor
  1707. * @tx_ring: ring to send buffer on
  1708. * @skb: send buffer
  1709. * @first: first buffer info buffer to use
  1710. * @tx_flags: collected send information
  1711. * @hdr_len: size of the packet header
  1712. * @td_cmd: the command field in the descriptor
  1713. * @td_offset: offset for checksum or crc
  1714. **/
  1715. static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1716. struct i40e_tx_buffer *first, u32 tx_flags,
  1717. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1718. {
  1719. unsigned int data_len = skb->data_len;
  1720. unsigned int size = skb_headlen(skb);
  1721. struct skb_frag_struct *frag;
  1722. struct i40e_tx_buffer *tx_bi;
  1723. struct i40e_tx_desc *tx_desc;
  1724. u16 i = tx_ring->next_to_use;
  1725. u32 td_tag = 0;
  1726. dma_addr_t dma;
  1727. u16 gso_segs;
  1728. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1729. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1730. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1731. I40E_TX_FLAGS_VLAN_SHIFT;
  1732. }
  1733. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
  1734. gso_segs = skb_shinfo(skb)->gso_segs;
  1735. else
  1736. gso_segs = 1;
  1737. /* multiply data chunks by size of headers */
  1738. first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
  1739. first->gso_segs = gso_segs;
  1740. first->skb = skb;
  1741. first->tx_flags = tx_flags;
  1742. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1743. tx_desc = I40E_TX_DESC(tx_ring, i);
  1744. tx_bi = first;
  1745. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1746. if (dma_mapping_error(tx_ring->dev, dma))
  1747. goto dma_error;
  1748. /* record length, and DMA address */
  1749. dma_unmap_len_set(tx_bi, len, size);
  1750. dma_unmap_addr_set(tx_bi, dma, dma);
  1751. tx_desc->buffer_addr = cpu_to_le64(dma);
  1752. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1753. tx_desc->cmd_type_offset_bsz =
  1754. build_ctob(td_cmd, td_offset,
  1755. I40E_MAX_DATA_PER_TXD, td_tag);
  1756. tx_desc++;
  1757. i++;
  1758. if (i == tx_ring->count) {
  1759. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1760. i = 0;
  1761. }
  1762. dma += I40E_MAX_DATA_PER_TXD;
  1763. size -= I40E_MAX_DATA_PER_TXD;
  1764. tx_desc->buffer_addr = cpu_to_le64(dma);
  1765. }
  1766. if (likely(!data_len))
  1767. break;
  1768. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1769. size, td_tag);
  1770. tx_desc++;
  1771. i++;
  1772. if (i == tx_ring->count) {
  1773. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1774. i = 0;
  1775. }
  1776. size = skb_frag_size(frag);
  1777. data_len -= size;
  1778. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1779. DMA_TO_DEVICE);
  1780. tx_bi = &tx_ring->tx_bi[i];
  1781. }
  1782. /* Place RS bit on last descriptor of any packet that spans across the
  1783. * 4th descriptor (WB_STRIDE aka 0x3) in a 64B cacheline.
  1784. */
  1785. #define WB_STRIDE 0x3
  1786. if (((i & WB_STRIDE) != WB_STRIDE) &&
  1787. (first <= &tx_ring->tx_bi[i]) &&
  1788. (first >= &tx_ring->tx_bi[i & ~WB_STRIDE])) {
  1789. tx_desc->cmd_type_offset_bsz =
  1790. build_ctob(td_cmd, td_offset, size, td_tag) |
  1791. cpu_to_le64((u64)I40E_TX_DESC_CMD_EOP <<
  1792. I40E_TXD_QW1_CMD_SHIFT);
  1793. } else {
  1794. tx_desc->cmd_type_offset_bsz =
  1795. build_ctob(td_cmd, td_offset, size, td_tag) |
  1796. cpu_to_le64((u64)I40E_TXD_CMD <<
  1797. I40E_TXD_QW1_CMD_SHIFT);
  1798. }
  1799. netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
  1800. tx_ring->queue_index),
  1801. first->bytecount);
  1802. /* set the timestamp */
  1803. first->time_stamp = jiffies;
  1804. /* Force memory writes to complete before letting h/w
  1805. * know there are new descriptors to fetch. (Only
  1806. * applicable for weak-ordered memory model archs,
  1807. * such as IA-64).
  1808. */
  1809. wmb();
  1810. /* set next_to_watch value indicating a packet is present */
  1811. first->next_to_watch = tx_desc;
  1812. i++;
  1813. if (i == tx_ring->count)
  1814. i = 0;
  1815. tx_ring->next_to_use = i;
  1816. /* notify HW of packet */
  1817. writel(i, tx_ring->tail);
  1818. return;
  1819. dma_error:
  1820. dev_info(tx_ring->dev, "TX DMA map failed\n");
  1821. /* clear dma mappings for failed tx_bi map */
  1822. for (;;) {
  1823. tx_bi = &tx_ring->tx_bi[i];
  1824. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  1825. if (tx_bi == first)
  1826. break;
  1827. if (i == 0)
  1828. i = tx_ring->count;
  1829. i--;
  1830. }
  1831. tx_ring->next_to_use = i;
  1832. }
  1833. /**
  1834. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  1835. * @tx_ring: the ring to be checked
  1836. * @size: the size buffer we want to assure is available
  1837. *
  1838. * Returns -EBUSY if a stop is needed, else 0
  1839. **/
  1840. static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1841. {
  1842. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1843. /* Memory barrier before checking head and tail */
  1844. smp_mb();
  1845. /* Check again in a case another CPU has just made room available. */
  1846. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1847. return -EBUSY;
  1848. /* A reprieve! - use start_queue because it doesn't call schedule */
  1849. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1850. ++tx_ring->tx_stats.restart_queue;
  1851. return 0;
  1852. }
  1853. /**
  1854. * i40e_maybe_stop_tx - 1st level check for tx stop conditions
  1855. * @tx_ring: the ring to be checked
  1856. * @size: the size buffer we want to assure is available
  1857. *
  1858. * Returns 0 if stop is not needed
  1859. **/
  1860. static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1861. {
  1862. if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
  1863. return 0;
  1864. return __i40e_maybe_stop_tx(tx_ring, size);
  1865. }
  1866. /**
  1867. * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
  1868. * @skb: send buffer
  1869. * @tx_ring: ring to send buffer on
  1870. *
  1871. * Returns number of data descriptors needed for this skb. Returns 0 to indicate
  1872. * there is not enough descriptors available in this ring since we need at least
  1873. * one descriptor.
  1874. **/
  1875. static int i40e_xmit_descriptor_count(struct sk_buff *skb,
  1876. struct i40e_ring *tx_ring)
  1877. {
  1878. #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
  1879. unsigned int f;
  1880. #endif
  1881. int count = 0;
  1882. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  1883. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  1884. * + 4 desc gap to avoid the cache line where head is,
  1885. * + 1 desc for context descriptor,
  1886. * otherwise try next time
  1887. */
  1888. #if PAGE_SIZE > I40E_MAX_DATA_PER_TXD
  1889. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  1890. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  1891. #else
  1892. count += skb_shinfo(skb)->nr_frags;
  1893. #endif
  1894. count += TXD_USE_COUNT(skb_headlen(skb));
  1895. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  1896. tx_ring->tx_stats.tx_busy++;
  1897. return 0;
  1898. }
  1899. return count;
  1900. }
  1901. /**
  1902. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  1903. * @skb: send buffer
  1904. * @tx_ring: ring to send buffer on
  1905. *
  1906. * Returns NETDEV_TX_OK if sent, else an error code
  1907. **/
  1908. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  1909. struct i40e_ring *tx_ring)
  1910. {
  1911. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  1912. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  1913. struct i40e_tx_buffer *first;
  1914. u32 td_offset = 0;
  1915. u32 tx_flags = 0;
  1916. __be16 protocol;
  1917. u32 td_cmd = 0;
  1918. u8 hdr_len = 0;
  1919. int tsyn;
  1920. int tso;
  1921. if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
  1922. return NETDEV_TX_BUSY;
  1923. /* prepare the xmit flags */
  1924. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  1925. goto out_drop;
  1926. /* obtain protocol of skb */
  1927. protocol = skb->protocol;
  1928. /* record the location of the first descriptor for this packet */
  1929. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  1930. /* setup IPv4/IPv6 offloads */
  1931. if (protocol == htons(ETH_P_IP))
  1932. tx_flags |= I40E_TX_FLAGS_IPV4;
  1933. else if (protocol == htons(ETH_P_IPV6))
  1934. tx_flags |= I40E_TX_FLAGS_IPV6;
  1935. tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
  1936. &cd_type_cmd_tso_mss, &cd_tunneling);
  1937. if (tso < 0)
  1938. goto out_drop;
  1939. else if (tso)
  1940. tx_flags |= I40E_TX_FLAGS_TSO;
  1941. skb_tx_timestamp(skb);
  1942. tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
  1943. if (tsyn)
  1944. tx_flags |= I40E_TX_FLAGS_TSYN;
  1945. /* always enable CRC insertion offload */
  1946. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  1947. /* Always offload the checksum, since it's in the data descriptor */
  1948. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1949. tx_flags |= I40E_TX_FLAGS_CSUM;
  1950. i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
  1951. tx_ring, &cd_tunneling);
  1952. }
  1953. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  1954. cd_tunneling, cd_l2tag2);
  1955. /* Add Flow Director ATR if it's enabled.
  1956. *
  1957. * NOTE: this must always be directly before the data descriptor.
  1958. */
  1959. i40e_atr(tx_ring, skb, tx_flags, protocol);
  1960. i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  1961. td_cmd, td_offset);
  1962. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1963. return NETDEV_TX_OK;
  1964. out_drop:
  1965. dev_kfree_skb_any(skb);
  1966. return NETDEV_TX_OK;
  1967. }
  1968. /**
  1969. * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  1970. * @skb: send buffer
  1971. * @netdev: network interface device structure
  1972. *
  1973. * Returns NETDEV_TX_OK if sent, else an error code
  1974. **/
  1975. netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1976. {
  1977. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1978. struct i40e_vsi *vsi = np->vsi;
  1979. struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
  1980. /* hardware can't handle really short frames, hardware padding works
  1981. * beyond this point
  1982. */
  1983. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  1984. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  1985. return NETDEV_TX_OK;
  1986. skb->len = I40E_MIN_TX_LEN;
  1987. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  1988. }
  1989. return i40e_xmit_frame_ring(skb, tx_ring);
  1990. }