i40e_txrx.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function 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 "i40evf.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. /**
  41. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  42. * @ring: the ring that owns the buffer
  43. * @tx_buffer: the buffer to free
  44. **/
  45. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  46. struct i40e_tx_buffer *tx_buffer)
  47. {
  48. if (tx_buffer->skb) {
  49. dev_kfree_skb_any(tx_buffer->skb);
  50. if (dma_unmap_len(tx_buffer, len))
  51. dma_unmap_single(ring->dev,
  52. dma_unmap_addr(tx_buffer, dma),
  53. dma_unmap_len(tx_buffer, len),
  54. DMA_TO_DEVICE);
  55. } else if (dma_unmap_len(tx_buffer, len)) {
  56. dma_unmap_page(ring->dev,
  57. dma_unmap_addr(tx_buffer, dma),
  58. dma_unmap_len(tx_buffer, len),
  59. DMA_TO_DEVICE);
  60. }
  61. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  62. kfree(tx_buffer->raw_buf);
  63. tx_buffer->next_to_watch = NULL;
  64. tx_buffer->skb = NULL;
  65. dma_unmap_len_set(tx_buffer, len, 0);
  66. /* tx_buffer must be completely set up in the transmit path */
  67. }
  68. /**
  69. * i40evf_clean_tx_ring - Free any empty Tx buffers
  70. * @tx_ring: ring to be cleaned
  71. **/
  72. void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
  73. {
  74. unsigned long bi_size;
  75. u16 i;
  76. /* ring already cleared, nothing to do */
  77. if (!tx_ring->tx_bi)
  78. return;
  79. /* Free all the Tx ring sk_buffs */
  80. for (i = 0; i < tx_ring->count; i++)
  81. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  82. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  83. memset(tx_ring->tx_bi, 0, bi_size);
  84. /* Zero out the descriptor ring */
  85. memset(tx_ring->desc, 0, tx_ring->size);
  86. tx_ring->next_to_use = 0;
  87. tx_ring->next_to_clean = 0;
  88. if (!tx_ring->netdev)
  89. return;
  90. /* cleanup Tx queue statistics */
  91. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  92. tx_ring->queue_index));
  93. }
  94. /**
  95. * i40evf_free_tx_resources - Free Tx resources per queue
  96. * @tx_ring: Tx descriptor ring for a specific queue
  97. *
  98. * Free all transmit software resources
  99. **/
  100. void i40evf_free_tx_resources(struct i40e_ring *tx_ring)
  101. {
  102. i40evf_clean_tx_ring(tx_ring);
  103. kfree(tx_ring->tx_bi);
  104. tx_ring->tx_bi = NULL;
  105. if (tx_ring->desc) {
  106. dma_free_coherent(tx_ring->dev, tx_ring->size,
  107. tx_ring->desc, tx_ring->dma);
  108. tx_ring->desc = NULL;
  109. }
  110. }
  111. /**
  112. * i40evf_get_tx_pending - how many Tx descriptors not processed
  113. * @tx_ring: the ring of descriptors
  114. *
  115. * Since there is no access to the ring head register
  116. * in XL710, we need to use our local copies
  117. **/
  118. u32 i40evf_get_tx_pending(struct i40e_ring *ring)
  119. {
  120. u32 head, tail;
  121. head = i40e_get_head(ring);
  122. tail = readl(ring->tail);
  123. if (head != tail)
  124. return (head < tail) ?
  125. tail - head : (tail + ring->count - head);
  126. return 0;
  127. }
  128. #define WB_STRIDE 0x3
  129. /**
  130. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  131. * @tx_ring: tx ring to clean
  132. * @budget: how many cleans we're allowed
  133. *
  134. * Returns true if there's any budget left (e.g. the clean is finished)
  135. **/
  136. static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
  137. {
  138. u16 i = tx_ring->next_to_clean;
  139. struct i40e_tx_buffer *tx_buf;
  140. struct i40e_tx_desc *tx_head;
  141. struct i40e_tx_desc *tx_desc;
  142. unsigned int total_packets = 0;
  143. unsigned int total_bytes = 0;
  144. tx_buf = &tx_ring->tx_bi[i];
  145. tx_desc = I40E_TX_DESC(tx_ring, i);
  146. i -= tx_ring->count;
  147. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  148. do {
  149. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  150. /* if next_to_watch is not set then there is no work pending */
  151. if (!eop_desc)
  152. break;
  153. /* prevent any other reads prior to eop_desc */
  154. read_barrier_depends();
  155. /* we have caught up to head, no work left to do */
  156. if (tx_head == tx_desc)
  157. break;
  158. /* clear next_to_watch to prevent false hangs */
  159. tx_buf->next_to_watch = NULL;
  160. /* update the statistics for this packet */
  161. total_bytes += tx_buf->bytecount;
  162. total_packets += tx_buf->gso_segs;
  163. /* free the skb */
  164. dev_kfree_skb_any(tx_buf->skb);
  165. /* unmap skb header data */
  166. dma_unmap_single(tx_ring->dev,
  167. dma_unmap_addr(tx_buf, dma),
  168. dma_unmap_len(tx_buf, len),
  169. DMA_TO_DEVICE);
  170. /* clear tx_buffer data */
  171. tx_buf->skb = NULL;
  172. dma_unmap_len_set(tx_buf, len, 0);
  173. /* unmap remaining buffers */
  174. while (tx_desc != eop_desc) {
  175. tx_buf++;
  176. tx_desc++;
  177. i++;
  178. if (unlikely(!i)) {
  179. i -= tx_ring->count;
  180. tx_buf = tx_ring->tx_bi;
  181. tx_desc = I40E_TX_DESC(tx_ring, 0);
  182. }
  183. /* unmap any remaining paged data */
  184. if (dma_unmap_len(tx_buf, len)) {
  185. dma_unmap_page(tx_ring->dev,
  186. dma_unmap_addr(tx_buf, dma),
  187. dma_unmap_len(tx_buf, len),
  188. DMA_TO_DEVICE);
  189. dma_unmap_len_set(tx_buf, len, 0);
  190. }
  191. }
  192. /* move us one more past the eop_desc for start of next pkt */
  193. tx_buf++;
  194. tx_desc++;
  195. i++;
  196. if (unlikely(!i)) {
  197. i -= tx_ring->count;
  198. tx_buf = tx_ring->tx_bi;
  199. tx_desc = I40E_TX_DESC(tx_ring, 0);
  200. }
  201. prefetch(tx_desc);
  202. /* update budget accounting */
  203. budget--;
  204. } while (likely(budget));
  205. i += tx_ring->count;
  206. tx_ring->next_to_clean = i;
  207. u64_stats_update_begin(&tx_ring->syncp);
  208. tx_ring->stats.bytes += total_bytes;
  209. tx_ring->stats.packets += total_packets;
  210. u64_stats_update_end(&tx_ring->syncp);
  211. tx_ring->q_vector->tx.total_bytes += total_bytes;
  212. tx_ring->q_vector->tx.total_packets += total_packets;
  213. netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
  214. tx_ring->queue_index),
  215. total_packets, total_bytes);
  216. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  217. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  218. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  219. /* Make sure that anybody stopping the queue after this
  220. * sees the new next_to_clean.
  221. */
  222. smp_mb();
  223. if (__netif_subqueue_stopped(tx_ring->netdev,
  224. tx_ring->queue_index) &&
  225. !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
  226. netif_wake_subqueue(tx_ring->netdev,
  227. tx_ring->queue_index);
  228. ++tx_ring->tx_stats.restart_queue;
  229. }
  230. }
  231. return !!budget;
  232. }
  233. /**
  234. * i40evf_force_wb -Arm hardware to do a wb on noncache aligned descriptors
  235. * @vsi: the VSI we care about
  236. * @q_vector: the vector on which to force writeback
  237. *
  238. **/
  239. static void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  240. {
  241. u16 flags = q_vector->tx.ring[0].flags;
  242. if (flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  243. u32 val;
  244. if (q_vector->arm_wb_state)
  245. return;
  246. val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK;
  247. wr32(&vsi->back->hw,
  248. I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
  249. vsi->base_vector - 1),
  250. val);
  251. q_vector->arm_wb_state = true;
  252. } else {
  253. u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  254. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
  255. I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  256. I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK;
  257. /* allow 00 to be written to the index */
  258. wr32(&vsi->back->hw,
  259. I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
  260. vsi->base_vector - 1), val);
  261. }
  262. }
  263. /**
  264. * i40e_set_new_dynamic_itr - Find new ITR level
  265. * @rc: structure containing ring performance data
  266. *
  267. * Returns true if ITR changed, false if not
  268. *
  269. * Stores a new ITR value based on packets and byte counts during
  270. * the last interrupt. The advantage of per interrupt computation
  271. * is faster updates and more accurate ITR for the current traffic
  272. * pattern. Constants in this function were computed based on
  273. * theoretical maximum wire speed and thresholds were set based on
  274. * testing data as well as attempting to minimize response time
  275. * while increasing bulk throughput.
  276. **/
  277. static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  278. {
  279. enum i40e_latency_range new_latency_range = rc->latency_range;
  280. struct i40e_q_vector *qv = rc->ring->q_vector;
  281. u32 new_itr = rc->itr;
  282. int bytes_per_int;
  283. int usecs;
  284. if (rc->total_packets == 0 || !rc->itr)
  285. return false;
  286. /* simple throttlerate management
  287. * 0-10MB/s lowest (50000 ints/s)
  288. * 10-20MB/s low (20000 ints/s)
  289. * 20-1249MB/s bulk (18000 ints/s)
  290. * > 40000 Rx packets per second (8000 ints/s)
  291. *
  292. * The math works out because the divisor is in 10^(-6) which
  293. * turns the bytes/us input value into MB/s values, but
  294. * make sure to use usecs, as the register values written
  295. * are in 2 usec increments in the ITR registers, and make sure
  296. * to use the smoothed values that the countdown timer gives us.
  297. */
  298. usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
  299. bytes_per_int = rc->total_bytes / usecs;
  300. switch (new_latency_range) {
  301. case I40E_LOWEST_LATENCY:
  302. if (bytes_per_int > 10)
  303. new_latency_range = I40E_LOW_LATENCY;
  304. break;
  305. case I40E_LOW_LATENCY:
  306. if (bytes_per_int > 20)
  307. new_latency_range = I40E_BULK_LATENCY;
  308. else if (bytes_per_int <= 10)
  309. new_latency_range = I40E_LOWEST_LATENCY;
  310. break;
  311. case I40E_BULK_LATENCY:
  312. case I40E_ULTRA_LATENCY:
  313. default:
  314. if (bytes_per_int <= 20)
  315. new_latency_range = I40E_LOW_LATENCY;
  316. break;
  317. }
  318. /* this is to adjust RX more aggressively when streaming small
  319. * packets. The value of 40000 was picked as it is just beyond
  320. * what the hardware can receive per second if in low latency
  321. * mode.
  322. */
  323. #define RX_ULTRA_PACKET_RATE 40000
  324. if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
  325. (&qv->rx == rc))
  326. new_latency_range = I40E_ULTRA_LATENCY;
  327. rc->latency_range = new_latency_range;
  328. switch (new_latency_range) {
  329. case I40E_LOWEST_LATENCY:
  330. new_itr = I40E_ITR_50K;
  331. break;
  332. case I40E_LOW_LATENCY:
  333. new_itr = I40E_ITR_20K;
  334. break;
  335. case I40E_BULK_LATENCY:
  336. new_itr = I40E_ITR_18K;
  337. break;
  338. case I40E_ULTRA_LATENCY:
  339. new_itr = I40E_ITR_8K;
  340. break;
  341. default:
  342. break;
  343. }
  344. rc->total_bytes = 0;
  345. rc->total_packets = 0;
  346. if (new_itr != rc->itr) {
  347. rc->itr = new_itr;
  348. return true;
  349. }
  350. return false;
  351. }
  352. /**
  353. * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
  354. * @tx_ring: the tx ring to set up
  355. *
  356. * Return 0 on success, negative on error
  357. **/
  358. int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
  359. {
  360. struct device *dev = tx_ring->dev;
  361. int bi_size;
  362. if (!dev)
  363. return -ENOMEM;
  364. /* warn if we are about to overwrite the pointer */
  365. WARN_ON(tx_ring->tx_bi);
  366. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  367. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  368. if (!tx_ring->tx_bi)
  369. goto err;
  370. /* round up to nearest 4K */
  371. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  372. /* add u32 for head writeback, align after this takes care of
  373. * guaranteeing this is at least one cache line in size
  374. */
  375. tx_ring->size += sizeof(u32);
  376. tx_ring->size = ALIGN(tx_ring->size, 4096);
  377. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  378. &tx_ring->dma, GFP_KERNEL);
  379. if (!tx_ring->desc) {
  380. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  381. tx_ring->size);
  382. goto err;
  383. }
  384. tx_ring->next_to_use = 0;
  385. tx_ring->next_to_clean = 0;
  386. return 0;
  387. err:
  388. kfree(tx_ring->tx_bi);
  389. tx_ring->tx_bi = NULL;
  390. return -ENOMEM;
  391. }
  392. /**
  393. * i40evf_clean_rx_ring - Free Rx buffers
  394. * @rx_ring: ring to be cleaned
  395. **/
  396. void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
  397. {
  398. struct device *dev = rx_ring->dev;
  399. struct i40e_rx_buffer *rx_bi;
  400. unsigned long bi_size;
  401. u16 i;
  402. /* ring already cleared, nothing to do */
  403. if (!rx_ring->rx_bi)
  404. return;
  405. if (ring_is_ps_enabled(rx_ring)) {
  406. int bufsz = ALIGN(rx_ring->rx_hdr_len, 256) * rx_ring->count;
  407. rx_bi = &rx_ring->rx_bi[0];
  408. if (rx_bi->hdr_buf) {
  409. dma_free_coherent(dev,
  410. bufsz,
  411. rx_bi->hdr_buf,
  412. rx_bi->dma);
  413. for (i = 0; i < rx_ring->count; i++) {
  414. rx_bi = &rx_ring->rx_bi[i];
  415. rx_bi->dma = 0;
  416. rx_bi->hdr_buf = NULL;
  417. }
  418. }
  419. }
  420. /* Free all the Rx ring sk_buffs */
  421. for (i = 0; i < rx_ring->count; i++) {
  422. rx_bi = &rx_ring->rx_bi[i];
  423. if (rx_bi->dma) {
  424. dma_unmap_single(dev,
  425. rx_bi->dma,
  426. rx_ring->rx_buf_len,
  427. DMA_FROM_DEVICE);
  428. rx_bi->dma = 0;
  429. }
  430. if (rx_bi->skb) {
  431. dev_kfree_skb(rx_bi->skb);
  432. rx_bi->skb = NULL;
  433. }
  434. if (rx_bi->page) {
  435. if (rx_bi->page_dma) {
  436. dma_unmap_page(dev,
  437. rx_bi->page_dma,
  438. PAGE_SIZE / 2,
  439. DMA_FROM_DEVICE);
  440. rx_bi->page_dma = 0;
  441. }
  442. __free_page(rx_bi->page);
  443. rx_bi->page = NULL;
  444. rx_bi->page_offset = 0;
  445. }
  446. }
  447. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  448. memset(rx_ring->rx_bi, 0, bi_size);
  449. /* Zero out the descriptor ring */
  450. memset(rx_ring->desc, 0, rx_ring->size);
  451. rx_ring->next_to_clean = 0;
  452. rx_ring->next_to_use = 0;
  453. }
  454. /**
  455. * i40evf_free_rx_resources - Free Rx resources
  456. * @rx_ring: ring to clean the resources from
  457. *
  458. * Free all receive software resources
  459. **/
  460. void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
  461. {
  462. i40evf_clean_rx_ring(rx_ring);
  463. kfree(rx_ring->rx_bi);
  464. rx_ring->rx_bi = NULL;
  465. if (rx_ring->desc) {
  466. dma_free_coherent(rx_ring->dev, rx_ring->size,
  467. rx_ring->desc, rx_ring->dma);
  468. rx_ring->desc = NULL;
  469. }
  470. }
  471. /**
  472. * i40evf_alloc_rx_headers - allocate rx header buffers
  473. * @rx_ring: ring to alloc buffers
  474. *
  475. * Allocate rx header buffers for the entire ring. As these are static,
  476. * this is only called when setting up a new ring.
  477. **/
  478. void i40evf_alloc_rx_headers(struct i40e_ring *rx_ring)
  479. {
  480. struct device *dev = rx_ring->dev;
  481. struct i40e_rx_buffer *rx_bi;
  482. dma_addr_t dma;
  483. void *buffer;
  484. int buf_size;
  485. int i;
  486. if (rx_ring->rx_bi[0].hdr_buf)
  487. return;
  488. /* Make sure the buffers don't cross cache line boundaries. */
  489. buf_size = ALIGN(rx_ring->rx_hdr_len, 256);
  490. buffer = dma_alloc_coherent(dev, buf_size * rx_ring->count,
  491. &dma, GFP_KERNEL);
  492. if (!buffer)
  493. return;
  494. for (i = 0; i < rx_ring->count; i++) {
  495. rx_bi = &rx_ring->rx_bi[i];
  496. rx_bi->dma = dma + (i * buf_size);
  497. rx_bi->hdr_buf = buffer + (i * buf_size);
  498. }
  499. }
  500. /**
  501. * i40evf_setup_rx_descriptors - Allocate Rx descriptors
  502. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  503. *
  504. * Returns 0 on success, negative on failure
  505. **/
  506. int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
  507. {
  508. struct device *dev = rx_ring->dev;
  509. int bi_size;
  510. /* warn if we are about to overwrite the pointer */
  511. WARN_ON(rx_ring->rx_bi);
  512. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  513. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  514. if (!rx_ring->rx_bi)
  515. goto err;
  516. u64_stats_init(&rx_ring->syncp);
  517. /* Round up to nearest 4K */
  518. rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
  519. ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
  520. : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  521. rx_ring->size = ALIGN(rx_ring->size, 4096);
  522. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  523. &rx_ring->dma, GFP_KERNEL);
  524. if (!rx_ring->desc) {
  525. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  526. rx_ring->size);
  527. goto err;
  528. }
  529. rx_ring->next_to_clean = 0;
  530. rx_ring->next_to_use = 0;
  531. return 0;
  532. err:
  533. kfree(rx_ring->rx_bi);
  534. rx_ring->rx_bi = NULL;
  535. return -ENOMEM;
  536. }
  537. /**
  538. * i40e_release_rx_desc - Store the new tail and head values
  539. * @rx_ring: ring to bump
  540. * @val: new head index
  541. **/
  542. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  543. {
  544. rx_ring->next_to_use = val;
  545. /* Force memory writes to complete before letting h/w
  546. * know there are new descriptors to fetch. (Only
  547. * applicable for weak-ordered memory model archs,
  548. * such as IA-64).
  549. */
  550. wmb();
  551. writel(val, rx_ring->tail);
  552. }
  553. /**
  554. * i40evf_alloc_rx_buffers_ps - Replace used receive buffers; packet split
  555. * @rx_ring: ring to place buffers on
  556. * @cleaned_count: number of buffers to replace
  557. **/
  558. void i40evf_alloc_rx_buffers_ps(struct i40e_ring *rx_ring, u16 cleaned_count)
  559. {
  560. u16 i = rx_ring->next_to_use;
  561. union i40e_rx_desc *rx_desc;
  562. struct i40e_rx_buffer *bi;
  563. /* do nothing if no valid netdev defined */
  564. if (!rx_ring->netdev || !cleaned_count)
  565. return;
  566. while (cleaned_count--) {
  567. rx_desc = I40E_RX_DESC(rx_ring, i);
  568. bi = &rx_ring->rx_bi[i];
  569. if (bi->skb) /* desc is in use */
  570. goto no_buffers;
  571. if (!bi->page) {
  572. bi->page = alloc_page(GFP_ATOMIC);
  573. if (!bi->page) {
  574. rx_ring->rx_stats.alloc_page_failed++;
  575. goto no_buffers;
  576. }
  577. }
  578. if (!bi->page_dma) {
  579. /* use a half page if we're re-using */
  580. bi->page_offset ^= PAGE_SIZE / 2;
  581. bi->page_dma = dma_map_page(rx_ring->dev,
  582. bi->page,
  583. bi->page_offset,
  584. PAGE_SIZE / 2,
  585. DMA_FROM_DEVICE);
  586. if (dma_mapping_error(rx_ring->dev,
  587. bi->page_dma)) {
  588. rx_ring->rx_stats.alloc_page_failed++;
  589. bi->page_dma = 0;
  590. goto no_buffers;
  591. }
  592. }
  593. dma_sync_single_range_for_device(rx_ring->dev,
  594. bi->dma,
  595. 0,
  596. rx_ring->rx_hdr_len,
  597. DMA_FROM_DEVICE);
  598. /* Refresh the desc even if buffer_addrs didn't change
  599. * because each write-back erases this info.
  600. */
  601. rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
  602. rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
  603. i++;
  604. if (i == rx_ring->count)
  605. i = 0;
  606. }
  607. no_buffers:
  608. if (rx_ring->next_to_use != i)
  609. i40e_release_rx_desc(rx_ring, i);
  610. }
  611. /**
  612. * i40evf_alloc_rx_buffers_1buf - Replace used receive buffers; single buffer
  613. * @rx_ring: ring to place buffers on
  614. * @cleaned_count: number of buffers to replace
  615. **/
  616. void i40evf_alloc_rx_buffers_1buf(struct i40e_ring *rx_ring, u16 cleaned_count)
  617. {
  618. u16 i = rx_ring->next_to_use;
  619. union i40e_rx_desc *rx_desc;
  620. struct i40e_rx_buffer *bi;
  621. struct sk_buff *skb;
  622. /* do nothing if no valid netdev defined */
  623. if (!rx_ring->netdev || !cleaned_count)
  624. return;
  625. while (cleaned_count--) {
  626. rx_desc = I40E_RX_DESC(rx_ring, i);
  627. bi = &rx_ring->rx_bi[i];
  628. skb = bi->skb;
  629. if (!skb) {
  630. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  631. rx_ring->rx_buf_len);
  632. if (!skb) {
  633. rx_ring->rx_stats.alloc_buff_failed++;
  634. goto no_buffers;
  635. }
  636. /* initialize queue mapping */
  637. skb_record_rx_queue(skb, rx_ring->queue_index);
  638. bi->skb = skb;
  639. }
  640. if (!bi->dma) {
  641. bi->dma = dma_map_single(rx_ring->dev,
  642. skb->data,
  643. rx_ring->rx_buf_len,
  644. DMA_FROM_DEVICE);
  645. if (dma_mapping_error(rx_ring->dev, bi->dma)) {
  646. rx_ring->rx_stats.alloc_buff_failed++;
  647. bi->dma = 0;
  648. goto no_buffers;
  649. }
  650. }
  651. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
  652. rx_desc->read.hdr_addr = 0;
  653. i++;
  654. if (i == rx_ring->count)
  655. i = 0;
  656. }
  657. no_buffers:
  658. if (rx_ring->next_to_use != i)
  659. i40e_release_rx_desc(rx_ring, i);
  660. }
  661. /**
  662. * i40e_receive_skb - Send a completed packet up the stack
  663. * @rx_ring: rx ring in play
  664. * @skb: packet to send up
  665. * @vlan_tag: vlan tag for packet
  666. **/
  667. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  668. struct sk_buff *skb, u16 vlan_tag)
  669. {
  670. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  671. if (vlan_tag & VLAN_VID_MASK)
  672. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  673. napi_gro_receive(&q_vector->napi, skb);
  674. }
  675. /**
  676. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  677. * @vsi: the VSI we care about
  678. * @skb: skb currently being received and modified
  679. * @rx_status: status value of last descriptor in packet
  680. * @rx_error: error value of last descriptor in packet
  681. * @rx_ptype: ptype value of last descriptor in packet
  682. **/
  683. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  684. struct sk_buff *skb,
  685. u32 rx_status,
  686. u32 rx_error,
  687. u16 rx_ptype)
  688. {
  689. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(rx_ptype);
  690. bool ipv4 = false, ipv6 = false;
  691. bool ipv4_tunnel, ipv6_tunnel;
  692. __wsum rx_udp_csum;
  693. struct iphdr *iph;
  694. __sum16 csum;
  695. ipv4_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
  696. (rx_ptype <= I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
  697. ipv6_tunnel = (rx_ptype >= I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
  698. (rx_ptype <= I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
  699. skb->ip_summed = CHECKSUM_NONE;
  700. /* Rx csum enabled and ip headers found? */
  701. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  702. return;
  703. /* did the hardware decode the packet and checksum? */
  704. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  705. return;
  706. /* both known and outer_ip must be set for the below code to work */
  707. if (!(decoded.known && decoded.outer_ip))
  708. return;
  709. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  710. decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4)
  711. ipv4 = true;
  712. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  713. decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6)
  714. ipv6 = true;
  715. if (ipv4 &&
  716. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  717. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  718. goto checksum_fail;
  719. /* likely incorrect csum if alternate IP extension headers found */
  720. if (ipv6 &&
  721. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  722. /* don't increment checksum err here, non-fatal err */
  723. return;
  724. /* there was some L4 error, count error and punt packet to the stack */
  725. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  726. goto checksum_fail;
  727. /* handle packets that were not able to be checksummed due
  728. * to arrival speed, in this case the stack can compute
  729. * the csum.
  730. */
  731. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  732. return;
  733. /* If VXLAN traffic has an outer UDPv4 checksum we need to check
  734. * it in the driver, hardware does not do it for us.
  735. * Since L3L4P bit was set we assume a valid IHL value (>=5)
  736. * so the total length of IPv4 header is IHL*4 bytes
  737. * The UDP_0 bit *may* bet set if the *inner* header is UDP
  738. */
  739. if (ipv4_tunnel) {
  740. skb->transport_header = skb->mac_header +
  741. sizeof(struct ethhdr) +
  742. (ip_hdr(skb)->ihl * 4);
  743. /* Add 4 bytes for VLAN tagged packets */
  744. skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
  745. skb->protocol == htons(ETH_P_8021AD))
  746. ? VLAN_HLEN : 0;
  747. if ((ip_hdr(skb)->protocol == IPPROTO_UDP) &&
  748. (udp_hdr(skb)->check != 0)) {
  749. rx_udp_csum = udp_csum(skb);
  750. iph = ip_hdr(skb);
  751. csum = csum_tcpudp_magic(iph->saddr, iph->daddr,
  752. (skb->len -
  753. skb_transport_offset(skb)),
  754. IPPROTO_UDP, rx_udp_csum);
  755. if (udp_hdr(skb)->check != csum)
  756. goto checksum_fail;
  757. } /* else its GRE and so no outer UDP header */
  758. }
  759. skb->ip_summed = CHECKSUM_UNNECESSARY;
  760. skb->csum_level = ipv4_tunnel || ipv6_tunnel;
  761. return;
  762. checksum_fail:
  763. vsi->back->hw_csum_rx_error++;
  764. }
  765. /**
  766. * i40e_ptype_to_htype - get a hash type
  767. * @ptype: the ptype value from the descriptor
  768. *
  769. * Returns a hash type to be used by skb_set_hash
  770. **/
  771. static inline enum pkt_hash_types i40e_ptype_to_htype(u8 ptype)
  772. {
  773. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  774. if (!decoded.known)
  775. return PKT_HASH_TYPE_NONE;
  776. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  777. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  778. return PKT_HASH_TYPE_L4;
  779. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  780. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  781. return PKT_HASH_TYPE_L3;
  782. else
  783. return PKT_HASH_TYPE_L2;
  784. }
  785. /**
  786. * i40e_rx_hash - set the hash value in the skb
  787. * @ring: descriptor ring
  788. * @rx_desc: specific descriptor
  789. **/
  790. static inline void i40e_rx_hash(struct i40e_ring *ring,
  791. union i40e_rx_desc *rx_desc,
  792. struct sk_buff *skb,
  793. u8 rx_ptype)
  794. {
  795. u32 hash;
  796. const __le64 rss_mask =
  797. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  798. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  799. if (ring->netdev->features & NETIF_F_RXHASH)
  800. return;
  801. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  802. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  803. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  804. }
  805. }
  806. /**
  807. * i40e_clean_rx_irq_ps - Reclaim resources after receive; packet split
  808. * @rx_ring: rx ring to clean
  809. * @budget: how many cleans we're allowed
  810. *
  811. * Returns true if there's any budget left (e.g. the clean is finished)
  812. **/
  813. static int i40e_clean_rx_irq_ps(struct i40e_ring *rx_ring, int budget)
  814. {
  815. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  816. u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
  817. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  818. const int current_node = numa_mem_id();
  819. struct i40e_vsi *vsi = rx_ring->vsi;
  820. u16 i = rx_ring->next_to_clean;
  821. union i40e_rx_desc *rx_desc;
  822. u32 rx_error, rx_status;
  823. u8 rx_ptype;
  824. u64 qword;
  825. do {
  826. struct i40e_rx_buffer *rx_bi;
  827. struct sk_buff *skb;
  828. u16 vlan_tag;
  829. /* return some buffers to hardware, one at a time is too slow */
  830. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  831. i40evf_alloc_rx_buffers_ps(rx_ring, cleaned_count);
  832. cleaned_count = 0;
  833. }
  834. i = rx_ring->next_to_clean;
  835. rx_desc = I40E_RX_DESC(rx_ring, i);
  836. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  837. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  838. I40E_RXD_QW1_STATUS_SHIFT;
  839. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
  840. break;
  841. /* This memory barrier is needed to keep us from reading
  842. * any other fields out of the rx_desc until we know the
  843. * DD bit is set.
  844. */
  845. dma_rmb();
  846. rx_bi = &rx_ring->rx_bi[i];
  847. skb = rx_bi->skb;
  848. if (likely(!skb)) {
  849. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  850. rx_ring->rx_hdr_len);
  851. if (!skb) {
  852. rx_ring->rx_stats.alloc_buff_failed++;
  853. break;
  854. }
  855. /* initialize queue mapping */
  856. skb_record_rx_queue(skb, rx_ring->queue_index);
  857. /* we are reusing so sync this buffer for CPU use */
  858. dma_sync_single_range_for_cpu(rx_ring->dev,
  859. rx_bi->dma,
  860. 0,
  861. rx_ring->rx_hdr_len,
  862. DMA_FROM_DEVICE);
  863. }
  864. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  865. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  866. rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
  867. I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
  868. rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
  869. I40E_RXD_QW1_LENGTH_SPH_SHIFT;
  870. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  871. I40E_RXD_QW1_ERROR_SHIFT;
  872. rx_hbo = rx_error & BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
  873. rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
  874. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  875. I40E_RXD_QW1_PTYPE_SHIFT;
  876. prefetch(rx_bi->page);
  877. rx_bi->skb = NULL;
  878. cleaned_count++;
  879. if (rx_hbo || rx_sph) {
  880. int len;
  881. if (rx_hbo)
  882. len = I40E_RX_HDR_SIZE;
  883. else
  884. len = rx_header_len;
  885. memcpy(__skb_put(skb, len), rx_bi->hdr_buf, len);
  886. } else if (skb->len == 0) {
  887. int len;
  888. len = (rx_packet_len > skb_headlen(skb) ?
  889. skb_headlen(skb) : rx_packet_len);
  890. memcpy(__skb_put(skb, len),
  891. rx_bi->page + rx_bi->page_offset,
  892. len);
  893. rx_bi->page_offset += len;
  894. rx_packet_len -= len;
  895. }
  896. /* Get the rest of the data if this was a header split */
  897. if (rx_packet_len) {
  898. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  899. rx_bi->page,
  900. rx_bi->page_offset,
  901. rx_packet_len);
  902. skb->len += rx_packet_len;
  903. skb->data_len += rx_packet_len;
  904. skb->truesize += rx_packet_len;
  905. if ((page_count(rx_bi->page) == 1) &&
  906. (page_to_nid(rx_bi->page) == current_node))
  907. get_page(rx_bi->page);
  908. else
  909. rx_bi->page = NULL;
  910. dma_unmap_page(rx_ring->dev,
  911. rx_bi->page_dma,
  912. PAGE_SIZE / 2,
  913. DMA_FROM_DEVICE);
  914. rx_bi->page_dma = 0;
  915. }
  916. I40E_RX_INCREMENT(rx_ring, i);
  917. if (unlikely(
  918. !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  919. struct i40e_rx_buffer *next_buffer;
  920. next_buffer = &rx_ring->rx_bi[i];
  921. next_buffer->skb = skb;
  922. rx_ring->rx_stats.non_eop_descs++;
  923. continue;
  924. }
  925. /* ERR_MASK will only have valid bits if EOP set */
  926. if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  927. dev_kfree_skb_any(skb);
  928. continue;
  929. }
  930. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  931. /* probably a little skewed due to removing CRC */
  932. total_rx_bytes += skb->len;
  933. total_rx_packets++;
  934. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  935. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  936. vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  937. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  938. : 0;
  939. #ifdef I40E_FCOE
  940. if (!i40e_fcoe_handle_offload(rx_ring, rx_desc, skb)) {
  941. dev_kfree_skb_any(skb);
  942. continue;
  943. }
  944. #endif
  945. i40e_receive_skb(rx_ring, skb, vlan_tag);
  946. rx_desc->wb.qword1.status_error_len = 0;
  947. } while (likely(total_rx_packets < budget));
  948. u64_stats_update_begin(&rx_ring->syncp);
  949. rx_ring->stats.packets += total_rx_packets;
  950. rx_ring->stats.bytes += total_rx_bytes;
  951. u64_stats_update_end(&rx_ring->syncp);
  952. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  953. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  954. return total_rx_packets;
  955. }
  956. /**
  957. * i40e_clean_rx_irq_1buf - Reclaim resources after receive; single buffer
  958. * @rx_ring: rx ring to clean
  959. * @budget: how many cleans we're allowed
  960. *
  961. * Returns number of packets cleaned
  962. **/
  963. static int i40e_clean_rx_irq_1buf(struct i40e_ring *rx_ring, int budget)
  964. {
  965. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  966. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  967. struct i40e_vsi *vsi = rx_ring->vsi;
  968. union i40e_rx_desc *rx_desc;
  969. u32 rx_error, rx_status;
  970. u16 rx_packet_len;
  971. u8 rx_ptype;
  972. u64 qword;
  973. u16 i;
  974. do {
  975. struct i40e_rx_buffer *rx_bi;
  976. struct sk_buff *skb;
  977. u16 vlan_tag;
  978. /* return some buffers to hardware, one at a time is too slow */
  979. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  980. i40evf_alloc_rx_buffers_1buf(rx_ring, cleaned_count);
  981. cleaned_count = 0;
  982. }
  983. i = rx_ring->next_to_clean;
  984. rx_desc = I40E_RX_DESC(rx_ring, i);
  985. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  986. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  987. I40E_RXD_QW1_STATUS_SHIFT;
  988. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_DD_SHIFT)))
  989. break;
  990. /* This memory barrier is needed to keep us from reading
  991. * any other fields out of the rx_desc until we know the
  992. * DD bit is set.
  993. */
  994. dma_rmb();
  995. rx_bi = &rx_ring->rx_bi[i];
  996. skb = rx_bi->skb;
  997. prefetch(skb->data);
  998. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  999. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1000. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1001. I40E_RXD_QW1_ERROR_SHIFT;
  1002. rx_error &= ~BIT(I40E_RX_DESC_ERROR_HBO_SHIFT);
  1003. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1004. I40E_RXD_QW1_PTYPE_SHIFT;
  1005. rx_bi->skb = NULL;
  1006. cleaned_count++;
  1007. /* Get the header and possibly the whole packet
  1008. * If this is an skb from previous receive dma will be 0
  1009. */
  1010. skb_put(skb, rx_packet_len);
  1011. dma_unmap_single(rx_ring->dev, rx_bi->dma, rx_ring->rx_buf_len,
  1012. DMA_FROM_DEVICE);
  1013. rx_bi->dma = 0;
  1014. I40E_RX_INCREMENT(rx_ring, i);
  1015. if (unlikely(
  1016. !(rx_status & BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  1017. rx_ring->rx_stats.non_eop_descs++;
  1018. continue;
  1019. }
  1020. /* ERR_MASK will only have valid bits if EOP set */
  1021. if (unlikely(rx_error & BIT(I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  1022. dev_kfree_skb_any(skb);
  1023. continue;
  1024. }
  1025. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1026. /* probably a little skewed due to removing CRC */
  1027. total_rx_bytes += skb->len;
  1028. total_rx_packets++;
  1029. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1030. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  1031. vlan_tag = rx_status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  1032. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  1033. : 0;
  1034. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1035. rx_desc->wb.qword1.status_error_len = 0;
  1036. } while (likely(total_rx_packets < budget));
  1037. u64_stats_update_begin(&rx_ring->syncp);
  1038. rx_ring->stats.packets += total_rx_packets;
  1039. rx_ring->stats.bytes += total_rx_bytes;
  1040. u64_stats_update_end(&rx_ring->syncp);
  1041. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1042. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1043. return total_rx_packets;
  1044. }
  1045. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1046. {
  1047. u32 val;
  1048. val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  1049. I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK |
  1050. (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
  1051. (itr << I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT);
  1052. return val;
  1053. }
  1054. /* a small macro to shorten up some long lines */
  1055. #define INTREG I40E_VFINT_DYN_CTLN1
  1056. /**
  1057. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1058. * @vsi: the VSI we care about
  1059. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1060. *
  1061. **/
  1062. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1063. struct i40e_q_vector *q_vector)
  1064. {
  1065. struct i40e_hw *hw = &vsi->back->hw;
  1066. bool rx = false, tx = false;
  1067. u32 rxval, txval;
  1068. int vector;
  1069. vector = (q_vector->v_idx + vsi->base_vector);
  1070. /* avoid dynamic calculation if in countdown mode OR if
  1071. * all dynamic is disabled
  1072. */
  1073. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1074. if (q_vector->itr_countdown > 0 ||
  1075. (!ITR_IS_DYNAMIC(vsi->rx_itr_setting) &&
  1076. !ITR_IS_DYNAMIC(vsi->tx_itr_setting))) {
  1077. goto enable_int;
  1078. }
  1079. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting)) {
  1080. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1081. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1082. }
  1083. if (ITR_IS_DYNAMIC(vsi->tx_itr_setting)) {
  1084. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1085. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1086. }
  1087. if (rx || tx) {
  1088. /* get the higher of the two ITR adjustments and
  1089. * use the same value for both ITR registers
  1090. * when in adaptive mode (Rx and/or Tx)
  1091. */
  1092. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1093. q_vector->tx.itr = q_vector->rx.itr = itr;
  1094. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1095. tx = true;
  1096. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1097. rx = true;
  1098. }
  1099. /* only need to enable the interrupt once, but need
  1100. * to possibly update both ITR values
  1101. */
  1102. if (rx) {
  1103. /* set the INTENA_MSK_MASK so that this first write
  1104. * won't actually enable the interrupt, instead just
  1105. * updating the ITR (it's bit 31 PF and VF)
  1106. */
  1107. rxval |= BIT(31);
  1108. /* don't check _DOWN because interrupt isn't being enabled */
  1109. wr32(hw, INTREG(vector - 1), rxval);
  1110. }
  1111. enable_int:
  1112. if (!test_bit(__I40E_DOWN, &vsi->state))
  1113. wr32(hw, INTREG(vector - 1), txval);
  1114. if (q_vector->itr_countdown)
  1115. q_vector->itr_countdown--;
  1116. else
  1117. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1118. }
  1119. /**
  1120. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  1121. * @napi: napi struct with our devices info in it
  1122. * @budget: amount of work driver is allowed to do this pass, in packets
  1123. *
  1124. * This function will clean all queues associated with a q_vector.
  1125. *
  1126. * Returns the amount of work done
  1127. **/
  1128. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  1129. {
  1130. struct i40e_q_vector *q_vector =
  1131. container_of(napi, struct i40e_q_vector, napi);
  1132. struct i40e_vsi *vsi = q_vector->vsi;
  1133. struct i40e_ring *ring;
  1134. bool clean_complete = true;
  1135. bool arm_wb = false;
  1136. int budget_per_ring;
  1137. int work_done = 0;
  1138. if (test_bit(__I40E_DOWN, &vsi->state)) {
  1139. napi_complete(napi);
  1140. return 0;
  1141. }
  1142. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1143. * budget and be more aggressive about cleaning up the Tx descriptors.
  1144. */
  1145. i40e_for_each_ring(ring, q_vector->tx) {
  1146. clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
  1147. arm_wb = arm_wb || ring->arm_wb;
  1148. ring->arm_wb = false;
  1149. }
  1150. /* Handle case where we are called by netpoll with a budget of 0 */
  1151. if (budget <= 0)
  1152. goto tx_only;
  1153. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1154. * allow the budget to go below 1 because that would exit polling early.
  1155. */
  1156. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1157. i40e_for_each_ring(ring, q_vector->rx) {
  1158. int cleaned;
  1159. if (ring_is_ps_enabled(ring))
  1160. cleaned = i40e_clean_rx_irq_ps(ring, budget_per_ring);
  1161. else
  1162. cleaned = i40e_clean_rx_irq_1buf(ring, budget_per_ring);
  1163. work_done += cleaned;
  1164. /* if we didn't clean as many as budgeted, we must be done */
  1165. clean_complete &= (budget_per_ring != cleaned);
  1166. }
  1167. /* If work not completed, return budget and polling will return */
  1168. if (!clean_complete) {
  1169. tx_only:
  1170. if (arm_wb) {
  1171. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  1172. i40evf_force_wb(vsi, q_vector);
  1173. }
  1174. return budget;
  1175. }
  1176. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  1177. q_vector->arm_wb_state = false;
  1178. /* Work is done so exit the polling mode and re-enable the interrupt */
  1179. napi_complete_done(napi, work_done);
  1180. i40e_update_enable_itr(vsi, q_vector);
  1181. return 0;
  1182. }
  1183. /**
  1184. * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1185. * @skb: send buffer
  1186. * @tx_ring: ring to send buffer on
  1187. * @flags: the tx flags to be set
  1188. *
  1189. * Checks the skb and set up correspondingly several generic transmit flags
  1190. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1191. *
  1192. * Returns error code indicate the frame should be dropped upon error and the
  1193. * otherwise returns 0 to indicate the flags has been set properly.
  1194. **/
  1195. static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
  1196. struct i40e_ring *tx_ring,
  1197. u32 *flags)
  1198. {
  1199. __be16 protocol = skb->protocol;
  1200. u32 tx_flags = 0;
  1201. if (protocol == htons(ETH_P_8021Q) &&
  1202. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  1203. /* When HW VLAN acceleration is turned off by the user the
  1204. * stack sets the protocol to 8021q so that the driver
  1205. * can take any steps required to support the SW only
  1206. * VLAN handling. In our case the driver doesn't need
  1207. * to take any further steps so just set the protocol
  1208. * to the encapsulated ethertype.
  1209. */
  1210. skb->protocol = vlan_get_protocol(skb);
  1211. goto out;
  1212. }
  1213. /* if we have a HW VLAN tag being added, default to the HW one */
  1214. if (skb_vlan_tag_present(skb)) {
  1215. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1216. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1217. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1218. } else if (protocol == htons(ETH_P_8021Q)) {
  1219. struct vlan_hdr *vhdr, _vhdr;
  1220. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1221. if (!vhdr)
  1222. return -EINVAL;
  1223. protocol = vhdr->h_vlan_encapsulated_proto;
  1224. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1225. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1226. }
  1227. out:
  1228. *flags = tx_flags;
  1229. return 0;
  1230. }
  1231. /**
  1232. * i40e_tso - set up the tso context descriptor
  1233. * @tx_ring: ptr to the ring to send
  1234. * @skb: ptr to the skb we're sending
  1235. * @hdr_len: ptr to the size of the packet header
  1236. * @cd_type_cmd_tso_mss: Quad Word 1
  1237. *
  1238. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1239. **/
  1240. static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1241. u8 *hdr_len, u64 *cd_type_cmd_tso_mss)
  1242. {
  1243. u32 cd_cmd, cd_tso_len, cd_mss;
  1244. struct ipv6hdr *ipv6h;
  1245. struct tcphdr *tcph;
  1246. struct iphdr *iph;
  1247. u32 l4len;
  1248. int err;
  1249. if (!skb_is_gso(skb))
  1250. return 0;
  1251. err = skb_cow_head(skb, 0);
  1252. if (err < 0)
  1253. return err;
  1254. iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
  1255. ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  1256. if (iph->version == 4) {
  1257. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1258. iph->tot_len = 0;
  1259. iph->check = 0;
  1260. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1261. 0, IPPROTO_TCP, 0);
  1262. } else if (ipv6h->version == 6) {
  1263. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1264. ipv6h->payload_len = 0;
  1265. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  1266. 0, IPPROTO_TCP, 0);
  1267. }
  1268. l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
  1269. *hdr_len = (skb->encapsulation
  1270. ? (skb_inner_transport_header(skb) - skb->data)
  1271. : skb_transport_offset(skb)) + l4len;
  1272. /* find the field values */
  1273. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1274. cd_tso_len = skb->len - *hdr_len;
  1275. cd_mss = skb_shinfo(skb)->gso_size;
  1276. *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1277. ((u64)cd_tso_len <<
  1278. I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1279. ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1280. return 1;
  1281. }
  1282. /**
  1283. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1284. * @skb: send buffer
  1285. * @tx_flags: pointer to Tx flags currently set
  1286. * @td_cmd: Tx descriptor command bits to set
  1287. * @td_offset: Tx descriptor header offsets to set
  1288. * @cd_tunneling: ptr to context desc bits
  1289. **/
  1290. static void i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  1291. u32 *td_cmd, u32 *td_offset,
  1292. struct i40e_ring *tx_ring,
  1293. u32 *cd_tunneling)
  1294. {
  1295. struct ipv6hdr *this_ipv6_hdr;
  1296. unsigned int this_tcp_hdrlen;
  1297. struct iphdr *this_ip_hdr;
  1298. u32 network_hdr_len;
  1299. u8 l4_hdr = 0;
  1300. struct udphdr *oudph;
  1301. struct iphdr *oiph;
  1302. u32 l4_tunnel = 0;
  1303. if (skb->encapsulation) {
  1304. switch (ip_hdr(skb)->protocol) {
  1305. case IPPROTO_UDP:
  1306. oudph = udp_hdr(skb);
  1307. oiph = ip_hdr(skb);
  1308. l4_tunnel = I40E_TXD_CTX_UDP_TUNNELING;
  1309. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1310. break;
  1311. default:
  1312. return;
  1313. }
  1314. network_hdr_len = skb_inner_network_header_len(skb);
  1315. this_ip_hdr = inner_ip_hdr(skb);
  1316. this_ipv6_hdr = inner_ipv6_hdr(skb);
  1317. this_tcp_hdrlen = inner_tcp_hdrlen(skb);
  1318. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1319. if (*tx_flags & I40E_TX_FLAGS_TSO) {
  1320. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
  1321. ip_hdr(skb)->check = 0;
  1322. } else {
  1323. *cd_tunneling |=
  1324. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1325. }
  1326. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1327. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
  1328. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1329. ip_hdr(skb)->check = 0;
  1330. }
  1331. /* Now set the ctx descriptor fields */
  1332. *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
  1333. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
  1334. l4_tunnel |
  1335. ((skb_inner_network_offset(skb) -
  1336. skb_transport_offset(skb)) >> 1) <<
  1337. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1338. if (this_ip_hdr->version == 6) {
  1339. *tx_flags &= ~I40E_TX_FLAGS_IPV4;
  1340. *tx_flags |= I40E_TX_FLAGS_IPV6;
  1341. }
  1342. if ((tx_ring->flags & I40E_TXR_FLAGS_OUTER_UDP_CSUM) &&
  1343. (l4_tunnel == I40E_TXD_CTX_UDP_TUNNELING) &&
  1344. (*cd_tunneling & I40E_TXD_CTX_QW0_EXT_IP_MASK)) {
  1345. oudph->check = ~csum_tcpudp_magic(oiph->saddr,
  1346. oiph->daddr,
  1347. (skb->len - skb_transport_offset(skb)),
  1348. IPPROTO_UDP, 0);
  1349. *cd_tunneling |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  1350. }
  1351. } else {
  1352. network_hdr_len = skb_network_header_len(skb);
  1353. this_ip_hdr = ip_hdr(skb);
  1354. this_ipv6_hdr = ipv6_hdr(skb);
  1355. this_tcp_hdrlen = tcp_hdrlen(skb);
  1356. }
  1357. /* Enable IP checksum offloads */
  1358. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1359. l4_hdr = this_ip_hdr->protocol;
  1360. /* the stack computes the IP header already, the only time we
  1361. * need the hardware to recompute it is in the case of TSO.
  1362. */
  1363. if (*tx_flags & I40E_TX_FLAGS_TSO) {
  1364. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
  1365. this_ip_hdr->check = 0;
  1366. } else {
  1367. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
  1368. }
  1369. /* Now set the td_offset for IP header length */
  1370. *td_offset = (network_hdr_len >> 2) <<
  1371. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1372. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1373. l4_hdr = this_ipv6_hdr->nexthdr;
  1374. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1375. /* Now set the td_offset for IP header length */
  1376. *td_offset = (network_hdr_len >> 2) <<
  1377. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1378. }
  1379. /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
  1380. *td_offset |= (skb_network_offset(skb) >> 1) <<
  1381. I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1382. /* Enable L4 checksum offloads */
  1383. switch (l4_hdr) {
  1384. case IPPROTO_TCP:
  1385. /* enable checksum offloads */
  1386. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1387. *td_offset |= (this_tcp_hdrlen >> 2) <<
  1388. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1389. break;
  1390. case IPPROTO_SCTP:
  1391. /* enable SCTP checksum offload */
  1392. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1393. *td_offset |= (sizeof(struct sctphdr) >> 2) <<
  1394. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1395. break;
  1396. case IPPROTO_UDP:
  1397. /* enable UDP checksum offload */
  1398. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1399. *td_offset |= (sizeof(struct udphdr) >> 2) <<
  1400. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1401. break;
  1402. default:
  1403. break;
  1404. }
  1405. }
  1406. /**
  1407. * i40e_create_tx_ctx Build the Tx context descriptor
  1408. * @tx_ring: ring to create the descriptor on
  1409. * @cd_type_cmd_tso_mss: Quad Word 1
  1410. * @cd_tunneling: Quad Word 0 - bits 0-31
  1411. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1412. **/
  1413. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1414. const u64 cd_type_cmd_tso_mss,
  1415. const u32 cd_tunneling, const u32 cd_l2tag2)
  1416. {
  1417. struct i40e_tx_context_desc *context_desc;
  1418. int i = tx_ring->next_to_use;
  1419. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1420. !cd_tunneling && !cd_l2tag2)
  1421. return;
  1422. /* grab the next descriptor */
  1423. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1424. i++;
  1425. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1426. /* cpu_to_le32 and assign to struct fields */
  1427. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1428. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1429. context_desc->rsvd = cpu_to_le16(0);
  1430. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1431. }
  1432. /**
  1433. * i40e_chk_linearize - Check if there are more than 8 fragments per packet
  1434. * @skb: send buffer
  1435. * @tx_flags: collected send information
  1436. *
  1437. * Note: Our HW can't scatter-gather more than 8 fragments to build
  1438. * a packet on the wire and so we need to figure out the cases where we
  1439. * need to linearize the skb.
  1440. **/
  1441. static bool i40e_chk_linearize(struct sk_buff *skb, u32 tx_flags)
  1442. {
  1443. struct skb_frag_struct *frag;
  1444. bool linearize = false;
  1445. unsigned int size = 0;
  1446. u16 num_frags;
  1447. u16 gso_segs;
  1448. num_frags = skb_shinfo(skb)->nr_frags;
  1449. gso_segs = skb_shinfo(skb)->gso_segs;
  1450. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO)) {
  1451. u16 j = 0;
  1452. if (num_frags < (I40E_MAX_BUFFER_TXD))
  1453. goto linearize_chk_done;
  1454. /* try the simple math, if we have too many frags per segment */
  1455. if (DIV_ROUND_UP((num_frags + gso_segs), gso_segs) >
  1456. I40E_MAX_BUFFER_TXD) {
  1457. linearize = true;
  1458. goto linearize_chk_done;
  1459. }
  1460. frag = &skb_shinfo(skb)->frags[0];
  1461. /* we might still have more fragments per segment */
  1462. do {
  1463. size += skb_frag_size(frag);
  1464. frag++; j++;
  1465. if ((size >= skb_shinfo(skb)->gso_size) &&
  1466. (j < I40E_MAX_BUFFER_TXD)) {
  1467. size = (size % skb_shinfo(skb)->gso_size);
  1468. j = (size) ? 1 : 0;
  1469. }
  1470. if (j == I40E_MAX_BUFFER_TXD) {
  1471. linearize = true;
  1472. break;
  1473. }
  1474. num_frags--;
  1475. } while (num_frags);
  1476. } else {
  1477. if (num_frags >= I40E_MAX_BUFFER_TXD)
  1478. linearize = true;
  1479. }
  1480. linearize_chk_done:
  1481. return linearize;
  1482. }
  1483. /**
  1484. * __i40evf_maybe_stop_tx - 2nd level check for tx stop conditions
  1485. * @tx_ring: the ring to be checked
  1486. * @size: the size buffer we want to assure is available
  1487. *
  1488. * Returns -EBUSY if a stop is needed, else 0
  1489. **/
  1490. static inline int __i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1491. {
  1492. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1493. /* Memory barrier before checking head and tail */
  1494. smp_mb();
  1495. /* Check again in a case another CPU has just made room available. */
  1496. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1497. return -EBUSY;
  1498. /* A reprieve! - use start_queue because it doesn't call schedule */
  1499. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1500. ++tx_ring->tx_stats.restart_queue;
  1501. return 0;
  1502. }
  1503. /**
  1504. * i40evf_maybe_stop_tx - 1st level check for tx stop conditions
  1505. * @tx_ring: the ring to be checked
  1506. * @size: the size buffer we want to assure is available
  1507. *
  1508. * Returns 0 if stop is not needed
  1509. **/
  1510. static inline int i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1511. {
  1512. if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
  1513. return 0;
  1514. return __i40evf_maybe_stop_tx(tx_ring, size);
  1515. }
  1516. /**
  1517. * i40evf_tx_map - Build the Tx descriptor
  1518. * @tx_ring: ring to send buffer on
  1519. * @skb: send buffer
  1520. * @first: first buffer info buffer to use
  1521. * @tx_flags: collected send information
  1522. * @hdr_len: size of the packet header
  1523. * @td_cmd: the command field in the descriptor
  1524. * @td_offset: offset for checksum or crc
  1525. **/
  1526. static inline void i40evf_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1527. struct i40e_tx_buffer *first, u32 tx_flags,
  1528. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1529. {
  1530. unsigned int data_len = skb->data_len;
  1531. unsigned int size = skb_headlen(skb);
  1532. struct skb_frag_struct *frag;
  1533. struct i40e_tx_buffer *tx_bi;
  1534. struct i40e_tx_desc *tx_desc;
  1535. u16 i = tx_ring->next_to_use;
  1536. u32 td_tag = 0;
  1537. dma_addr_t dma;
  1538. u16 gso_segs;
  1539. u16 desc_count = 0;
  1540. bool tail_bump = true;
  1541. bool do_rs = false;
  1542. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1543. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1544. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1545. I40E_TX_FLAGS_VLAN_SHIFT;
  1546. }
  1547. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
  1548. gso_segs = skb_shinfo(skb)->gso_segs;
  1549. else
  1550. gso_segs = 1;
  1551. /* multiply data chunks by size of headers */
  1552. first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
  1553. first->gso_segs = gso_segs;
  1554. first->skb = skb;
  1555. first->tx_flags = tx_flags;
  1556. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1557. tx_desc = I40E_TX_DESC(tx_ring, i);
  1558. tx_bi = first;
  1559. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1560. if (dma_mapping_error(tx_ring->dev, dma))
  1561. goto dma_error;
  1562. /* record length, and DMA address */
  1563. dma_unmap_len_set(tx_bi, len, size);
  1564. dma_unmap_addr_set(tx_bi, dma, dma);
  1565. tx_desc->buffer_addr = cpu_to_le64(dma);
  1566. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1567. tx_desc->cmd_type_offset_bsz =
  1568. build_ctob(td_cmd, td_offset,
  1569. I40E_MAX_DATA_PER_TXD, td_tag);
  1570. tx_desc++;
  1571. i++;
  1572. desc_count++;
  1573. if (i == tx_ring->count) {
  1574. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1575. i = 0;
  1576. }
  1577. dma += I40E_MAX_DATA_PER_TXD;
  1578. size -= I40E_MAX_DATA_PER_TXD;
  1579. tx_desc->buffer_addr = cpu_to_le64(dma);
  1580. }
  1581. if (likely(!data_len))
  1582. break;
  1583. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1584. size, td_tag);
  1585. tx_desc++;
  1586. i++;
  1587. desc_count++;
  1588. if (i == tx_ring->count) {
  1589. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1590. i = 0;
  1591. }
  1592. size = skb_frag_size(frag);
  1593. data_len -= size;
  1594. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1595. DMA_TO_DEVICE);
  1596. tx_bi = &tx_ring->tx_bi[i];
  1597. }
  1598. /* set next_to_watch value indicating a packet is present */
  1599. first->next_to_watch = tx_desc;
  1600. i++;
  1601. if (i == tx_ring->count)
  1602. i = 0;
  1603. tx_ring->next_to_use = i;
  1604. netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
  1605. tx_ring->queue_index),
  1606. first->bytecount);
  1607. i40evf_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1608. /* Algorithm to optimize tail and RS bit setting:
  1609. * if xmit_more is supported
  1610. * if xmit_more is true
  1611. * do not update tail and do not mark RS bit.
  1612. * if xmit_more is false and last xmit_more was false
  1613. * if every packet spanned less than 4 desc
  1614. * then set RS bit on 4th packet and update tail
  1615. * on every packet
  1616. * else
  1617. * update tail and set RS bit on every packet.
  1618. * if xmit_more is false and last_xmit_more was true
  1619. * update tail and set RS bit.
  1620. *
  1621. * Optimization: wmb to be issued only in case of tail update.
  1622. * Also optimize the Descriptor WB path for RS bit with the same
  1623. * algorithm.
  1624. *
  1625. * Note: If there are less than 4 packets
  1626. * pending and interrupts were disabled the service task will
  1627. * trigger a force WB.
  1628. */
  1629. if (skb->xmit_more &&
  1630. !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
  1631. tx_ring->queue_index))) {
  1632. tx_ring->flags |= I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
  1633. tail_bump = false;
  1634. } else if (!skb->xmit_more &&
  1635. !netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
  1636. tx_ring->queue_index)) &&
  1637. (!(tx_ring->flags & I40E_TXR_FLAGS_LAST_XMIT_MORE_SET)) &&
  1638. (tx_ring->packet_stride < WB_STRIDE) &&
  1639. (desc_count < WB_STRIDE)) {
  1640. tx_ring->packet_stride++;
  1641. } else {
  1642. tx_ring->packet_stride = 0;
  1643. tx_ring->flags &= ~I40E_TXR_FLAGS_LAST_XMIT_MORE_SET;
  1644. do_rs = true;
  1645. }
  1646. if (do_rs)
  1647. tx_ring->packet_stride = 0;
  1648. tx_desc->cmd_type_offset_bsz =
  1649. build_ctob(td_cmd, td_offset, size, td_tag) |
  1650. cpu_to_le64((u64)(do_rs ? I40E_TXD_CMD :
  1651. I40E_TX_DESC_CMD_EOP) <<
  1652. I40E_TXD_QW1_CMD_SHIFT);
  1653. /* notify HW of packet */
  1654. if (!tail_bump)
  1655. prefetchw(tx_desc + 1);
  1656. if (tail_bump) {
  1657. /* Force memory writes to complete before letting h/w
  1658. * know there are new descriptors to fetch. (Only
  1659. * applicable for weak-ordered memory model archs,
  1660. * such as IA-64).
  1661. */
  1662. wmb();
  1663. writel(i, tx_ring->tail);
  1664. }
  1665. return;
  1666. dma_error:
  1667. dev_info(tx_ring->dev, "TX DMA map failed\n");
  1668. /* clear dma mappings for failed tx_bi map */
  1669. for (;;) {
  1670. tx_bi = &tx_ring->tx_bi[i];
  1671. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  1672. if (tx_bi == first)
  1673. break;
  1674. if (i == 0)
  1675. i = tx_ring->count;
  1676. i--;
  1677. }
  1678. tx_ring->next_to_use = i;
  1679. }
  1680. /**
  1681. * i40evf_xmit_descriptor_count - calculate number of tx descriptors needed
  1682. * @skb: send buffer
  1683. * @tx_ring: ring to send buffer on
  1684. *
  1685. * Returns number of data descriptors needed for this skb. Returns 0 to indicate
  1686. * there is not enough descriptors available in this ring since we need at least
  1687. * one descriptor.
  1688. **/
  1689. static inline int i40evf_xmit_descriptor_count(struct sk_buff *skb,
  1690. struct i40e_ring *tx_ring)
  1691. {
  1692. unsigned int f;
  1693. int count = 0;
  1694. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  1695. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  1696. * + 4 desc gap to avoid the cache line where head is,
  1697. * + 1 desc for context descriptor,
  1698. * otherwise try next time
  1699. */
  1700. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  1701. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  1702. count += TXD_USE_COUNT(skb_headlen(skb));
  1703. if (i40evf_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  1704. tx_ring->tx_stats.tx_busy++;
  1705. return 0;
  1706. }
  1707. return count;
  1708. }
  1709. /**
  1710. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  1711. * @skb: send buffer
  1712. * @tx_ring: ring to send buffer on
  1713. *
  1714. * Returns NETDEV_TX_OK if sent, else an error code
  1715. **/
  1716. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  1717. struct i40e_ring *tx_ring)
  1718. {
  1719. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  1720. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  1721. struct i40e_tx_buffer *first;
  1722. u32 td_offset = 0;
  1723. u32 tx_flags = 0;
  1724. __be16 protocol;
  1725. u32 td_cmd = 0;
  1726. u8 hdr_len = 0;
  1727. int tso;
  1728. /* prefetch the data, we'll need it later */
  1729. prefetch(skb->data);
  1730. if (0 == i40evf_xmit_descriptor_count(skb, tx_ring))
  1731. return NETDEV_TX_BUSY;
  1732. /* prepare the xmit flags */
  1733. if (i40evf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  1734. goto out_drop;
  1735. /* obtain protocol of skb */
  1736. protocol = vlan_get_protocol(skb);
  1737. /* record the location of the first descriptor for this packet */
  1738. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  1739. /* setup IPv4/IPv6 offloads */
  1740. if (protocol == htons(ETH_P_IP))
  1741. tx_flags |= I40E_TX_FLAGS_IPV4;
  1742. else if (protocol == htons(ETH_P_IPV6))
  1743. tx_flags |= I40E_TX_FLAGS_IPV6;
  1744. tso = i40e_tso(tx_ring, skb, &hdr_len, &cd_type_cmd_tso_mss);
  1745. if (tso < 0)
  1746. goto out_drop;
  1747. else if (tso)
  1748. tx_flags |= I40E_TX_FLAGS_TSO;
  1749. if (i40e_chk_linearize(skb, tx_flags)) {
  1750. if (skb_linearize(skb))
  1751. goto out_drop;
  1752. tx_ring->tx_stats.tx_linearize++;
  1753. }
  1754. skb_tx_timestamp(skb);
  1755. /* always enable CRC insertion offload */
  1756. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  1757. /* Always offload the checksum, since it's in the data descriptor */
  1758. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1759. tx_flags |= I40E_TX_FLAGS_CSUM;
  1760. i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  1761. tx_ring, &cd_tunneling);
  1762. }
  1763. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  1764. cd_tunneling, cd_l2tag2);
  1765. i40evf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  1766. td_cmd, td_offset);
  1767. return NETDEV_TX_OK;
  1768. out_drop:
  1769. dev_kfree_skb_any(skb);
  1770. return NETDEV_TX_OK;
  1771. }
  1772. /**
  1773. * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  1774. * @skb: send buffer
  1775. * @netdev: network interface device structure
  1776. *
  1777. * Returns NETDEV_TX_OK if sent, else an error code
  1778. **/
  1779. netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1780. {
  1781. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1782. struct i40e_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
  1783. /* hardware can't handle really short frames, hardware padding works
  1784. * beyond this point
  1785. */
  1786. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  1787. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  1788. return NETDEV_TX_OK;
  1789. skb->len = I40E_MIN_TX_LEN;
  1790. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  1791. }
  1792. return i40e_xmit_frame_ring(skb, tx_ring);
  1793. }