i40e_txrx.c 56 KB

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