i40e_txrx.c 46 KB

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