i40e_txrx.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2016 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_trace.h"
  30. #include "i40e_prototype.h"
  31. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  32. u32 td_tag)
  33. {
  34. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  35. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  36. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  37. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  38. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  39. }
  40. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  41. /**
  42. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  43. * @ring: the ring that owns the buffer
  44. * @tx_buffer: the buffer to free
  45. **/
  46. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  47. struct i40e_tx_buffer *tx_buffer)
  48. {
  49. if (tx_buffer->skb) {
  50. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  51. kfree(tx_buffer->raw_buf);
  52. else
  53. dev_kfree_skb_any(tx_buffer->skb);
  54. if (dma_unmap_len(tx_buffer, len))
  55. dma_unmap_single(ring->dev,
  56. dma_unmap_addr(tx_buffer, dma),
  57. dma_unmap_len(tx_buffer, len),
  58. DMA_TO_DEVICE);
  59. } else if (dma_unmap_len(tx_buffer, len)) {
  60. dma_unmap_page(ring->dev,
  61. dma_unmap_addr(tx_buffer, dma),
  62. dma_unmap_len(tx_buffer, len),
  63. DMA_TO_DEVICE);
  64. }
  65. tx_buffer->next_to_watch = NULL;
  66. tx_buffer->skb = NULL;
  67. dma_unmap_len_set(tx_buffer, len, 0);
  68. /* tx_buffer must be completely set up in the transmit path */
  69. }
  70. /**
  71. * i40evf_clean_tx_ring - Free any empty Tx buffers
  72. * @tx_ring: ring to be cleaned
  73. **/
  74. void i40evf_clean_tx_ring(struct i40e_ring *tx_ring)
  75. {
  76. unsigned long bi_size;
  77. u16 i;
  78. /* ring already cleared, nothing to do */
  79. if (!tx_ring->tx_bi)
  80. return;
  81. /* Free all the Tx ring sk_buffs */
  82. for (i = 0; i < tx_ring->count; i++)
  83. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  84. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  85. memset(tx_ring->tx_bi, 0, bi_size);
  86. /* Zero out the descriptor ring */
  87. memset(tx_ring->desc, 0, tx_ring->size);
  88. tx_ring->next_to_use = 0;
  89. tx_ring->next_to_clean = 0;
  90. if (!tx_ring->netdev)
  91. return;
  92. /* cleanup Tx queue statistics */
  93. netdev_tx_reset_queue(txring_txq(tx_ring));
  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. * i40evf_get_tx_pending - how many Tx descriptors not processed
  114. * @tx_ring: the ring of descriptors
  115. * @in_sw: is tx_pending being checked in SW or HW
  116. *
  117. * Since there is no access to the ring head register
  118. * in XL710, we need to use our local copies
  119. **/
  120. u32 i40evf_get_tx_pending(struct i40e_ring *ring, bool in_sw)
  121. {
  122. u32 head, tail;
  123. head = ring->next_to_clean;
  124. tail = readl(ring->tail);
  125. if (head != tail)
  126. return (head < tail) ?
  127. tail - head : (tail + ring->count - head);
  128. return 0;
  129. }
  130. #define WB_STRIDE 4
  131. /**
  132. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  133. * @vsi: the VSI we care about
  134. * @tx_ring: Tx ring to clean
  135. * @napi_budget: Used to determine if we are in netpoll
  136. *
  137. * Returns true if there's any budget left (e.g. the clean is finished)
  138. **/
  139. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  140. struct i40e_ring *tx_ring, int napi_budget)
  141. {
  142. u16 i = tx_ring->next_to_clean;
  143. struct i40e_tx_buffer *tx_buf;
  144. struct i40e_tx_desc *tx_desc;
  145. unsigned int total_bytes = 0, total_packets = 0;
  146. unsigned int budget = vsi->work_limit;
  147. tx_buf = &tx_ring->tx_bi[i];
  148. tx_desc = I40E_TX_DESC(tx_ring, i);
  149. i -= tx_ring->count;
  150. do {
  151. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  152. /* if next_to_watch is not set then there is no work pending */
  153. if (!eop_desc)
  154. break;
  155. /* prevent any other reads prior to eop_desc */
  156. read_barrier_depends();
  157. i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  158. /* if the descriptor isn't done, no work yet to do */
  159. if (!(eop_desc->cmd_type_offset_bsz &
  160. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  161. break;
  162. /* clear next_to_watch to prevent false hangs */
  163. tx_buf->next_to_watch = NULL;
  164. /* update the statistics for this packet */
  165. total_bytes += tx_buf->bytecount;
  166. total_packets += tx_buf->gso_segs;
  167. /* free the skb */
  168. napi_consume_skb(tx_buf->skb, napi_budget);
  169. /* unmap skb header data */
  170. dma_unmap_single(tx_ring->dev,
  171. dma_unmap_addr(tx_buf, dma),
  172. dma_unmap_len(tx_buf, len),
  173. DMA_TO_DEVICE);
  174. /* clear tx_buffer data */
  175. tx_buf->skb = NULL;
  176. dma_unmap_len_set(tx_buf, len, 0);
  177. /* unmap remaining buffers */
  178. while (tx_desc != eop_desc) {
  179. i40e_trace(clean_tx_irq_unmap,
  180. tx_ring, tx_desc, tx_buf);
  181. tx_buf++;
  182. tx_desc++;
  183. i++;
  184. if (unlikely(!i)) {
  185. i -= tx_ring->count;
  186. tx_buf = tx_ring->tx_bi;
  187. tx_desc = I40E_TX_DESC(tx_ring, 0);
  188. }
  189. /* unmap any remaining paged data */
  190. if (dma_unmap_len(tx_buf, len)) {
  191. dma_unmap_page(tx_ring->dev,
  192. dma_unmap_addr(tx_buf, dma),
  193. dma_unmap_len(tx_buf, len),
  194. DMA_TO_DEVICE);
  195. dma_unmap_len_set(tx_buf, len, 0);
  196. }
  197. }
  198. /* move us one more past the eop_desc for start of next pkt */
  199. tx_buf++;
  200. tx_desc++;
  201. i++;
  202. if (unlikely(!i)) {
  203. i -= tx_ring->count;
  204. tx_buf = tx_ring->tx_bi;
  205. tx_desc = I40E_TX_DESC(tx_ring, 0);
  206. }
  207. prefetch(tx_desc);
  208. /* update budget accounting */
  209. budget--;
  210. } while (likely(budget));
  211. i += tx_ring->count;
  212. tx_ring->next_to_clean = i;
  213. u64_stats_update_begin(&tx_ring->syncp);
  214. tx_ring->stats.bytes += total_bytes;
  215. tx_ring->stats.packets += total_packets;
  216. u64_stats_update_end(&tx_ring->syncp);
  217. tx_ring->q_vector->tx.total_bytes += total_bytes;
  218. tx_ring->q_vector->tx.total_packets += total_packets;
  219. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  220. /* check to see if there are < 4 descriptors
  221. * waiting to be written back, then kick the hardware to force
  222. * them to be written back in case we stay in NAPI.
  223. * In this mode on X722 we do not enable Interrupt.
  224. */
  225. unsigned int j = i40evf_get_tx_pending(tx_ring, false);
  226. if (budget &&
  227. ((j / WB_STRIDE) == 0) && (j > 0) &&
  228. !test_bit(__I40E_VSI_DOWN, vsi->state) &&
  229. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  230. tx_ring->arm_wb = true;
  231. }
  232. /* notify netdev of completed buffers */
  233. netdev_tx_completed_queue(txring_txq(tx_ring),
  234. total_packets, total_bytes);
  235. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  236. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  237. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  238. /* Make sure that anybody stopping the queue after this
  239. * sees the new next_to_clean.
  240. */
  241. smp_mb();
  242. if (__netif_subqueue_stopped(tx_ring->netdev,
  243. tx_ring->queue_index) &&
  244. !test_bit(__I40E_VSI_DOWN, vsi->state)) {
  245. netif_wake_subqueue(tx_ring->netdev,
  246. tx_ring->queue_index);
  247. ++tx_ring->tx_stats.restart_queue;
  248. }
  249. }
  250. return !!budget;
  251. }
  252. /**
  253. * i40evf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  254. * @vsi: the VSI we care about
  255. * @q_vector: the vector on which to enable writeback
  256. *
  257. **/
  258. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  259. struct i40e_q_vector *q_vector)
  260. {
  261. u16 flags = q_vector->tx.ring[0].flags;
  262. u32 val;
  263. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  264. return;
  265. if (q_vector->arm_wb_state)
  266. return;
  267. val = I40E_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
  268. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */
  269. wr32(&vsi->back->hw,
  270. I40E_VFINT_DYN_CTLN1(q_vector->v_idx +
  271. vsi->base_vector - 1), val);
  272. q_vector->arm_wb_state = true;
  273. }
  274. /**
  275. * i40evf_force_wb - Issue SW Interrupt so HW does a wb
  276. * @vsi: the VSI we care about
  277. * @q_vector: the vector on which to force writeback
  278. *
  279. **/
  280. void i40evf_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  281. {
  282. u32 val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  283. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */
  284. I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  285. I40E_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK
  286. /* allow 00 to be written to the index */;
  287. wr32(&vsi->back->hw,
  288. I40E_VFINT_DYN_CTLN1(q_vector->v_idx + vsi->base_vector - 1),
  289. val);
  290. }
  291. /**
  292. * i40e_set_new_dynamic_itr - Find new ITR level
  293. * @rc: structure containing ring performance data
  294. *
  295. * Returns true if ITR changed, false if not
  296. *
  297. * Stores a new ITR value based on packets and byte counts during
  298. * the last interrupt. The advantage of per interrupt computation
  299. * is faster updates and more accurate ITR for the current traffic
  300. * pattern. Constants in this function were computed based on
  301. * theoretical maximum wire speed and thresholds were set based on
  302. * testing data as well as attempting to minimize response time
  303. * while increasing bulk throughput.
  304. **/
  305. static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  306. {
  307. enum i40e_latency_range new_latency_range = rc->latency_range;
  308. struct i40e_q_vector *qv = rc->ring->q_vector;
  309. u32 new_itr = rc->itr;
  310. int bytes_per_int;
  311. int usecs;
  312. if (rc->total_packets == 0 || !rc->itr)
  313. return false;
  314. /* simple throttlerate management
  315. * 0-10MB/s lowest (50000 ints/s)
  316. * 10-20MB/s low (20000 ints/s)
  317. * 20-1249MB/s bulk (18000 ints/s)
  318. * > 40000 Rx packets per second (8000 ints/s)
  319. *
  320. * The math works out because the divisor is in 10^(-6) which
  321. * turns the bytes/us input value into MB/s values, but
  322. * make sure to use usecs, as the register values written
  323. * are in 2 usec increments in the ITR registers, and make sure
  324. * to use the smoothed values that the countdown timer gives us.
  325. */
  326. usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
  327. bytes_per_int = rc->total_bytes / usecs;
  328. switch (new_latency_range) {
  329. case I40E_LOWEST_LATENCY:
  330. if (bytes_per_int > 10)
  331. new_latency_range = I40E_LOW_LATENCY;
  332. break;
  333. case I40E_LOW_LATENCY:
  334. if (bytes_per_int > 20)
  335. new_latency_range = I40E_BULK_LATENCY;
  336. else if (bytes_per_int <= 10)
  337. new_latency_range = I40E_LOWEST_LATENCY;
  338. break;
  339. case I40E_BULK_LATENCY:
  340. case I40E_ULTRA_LATENCY:
  341. default:
  342. if (bytes_per_int <= 20)
  343. new_latency_range = I40E_LOW_LATENCY;
  344. break;
  345. }
  346. /* this is to adjust RX more aggressively when streaming small
  347. * packets. The value of 40000 was picked as it is just beyond
  348. * what the hardware can receive per second if in low latency
  349. * mode.
  350. */
  351. #define RX_ULTRA_PACKET_RATE 40000
  352. if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
  353. (&qv->rx == rc))
  354. new_latency_range = I40E_ULTRA_LATENCY;
  355. rc->latency_range = new_latency_range;
  356. switch (new_latency_range) {
  357. case I40E_LOWEST_LATENCY:
  358. new_itr = I40E_ITR_50K;
  359. break;
  360. case I40E_LOW_LATENCY:
  361. new_itr = I40E_ITR_20K;
  362. break;
  363. case I40E_BULK_LATENCY:
  364. new_itr = I40E_ITR_18K;
  365. break;
  366. case I40E_ULTRA_LATENCY:
  367. new_itr = I40E_ITR_8K;
  368. break;
  369. default:
  370. break;
  371. }
  372. rc->total_bytes = 0;
  373. rc->total_packets = 0;
  374. if (new_itr != rc->itr) {
  375. rc->itr = new_itr;
  376. return true;
  377. }
  378. return false;
  379. }
  380. /**
  381. * i40evf_setup_tx_descriptors - Allocate the Tx descriptors
  382. * @tx_ring: the tx ring to set up
  383. *
  384. * Return 0 on success, negative on error
  385. **/
  386. int i40evf_setup_tx_descriptors(struct i40e_ring *tx_ring)
  387. {
  388. struct device *dev = tx_ring->dev;
  389. int bi_size;
  390. if (!dev)
  391. return -ENOMEM;
  392. /* warn if we are about to overwrite the pointer */
  393. WARN_ON(tx_ring->tx_bi);
  394. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  395. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  396. if (!tx_ring->tx_bi)
  397. goto err;
  398. /* round up to nearest 4K */
  399. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  400. tx_ring->size = ALIGN(tx_ring->size, 4096);
  401. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  402. &tx_ring->dma, GFP_KERNEL);
  403. if (!tx_ring->desc) {
  404. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  405. tx_ring->size);
  406. goto err;
  407. }
  408. tx_ring->next_to_use = 0;
  409. tx_ring->next_to_clean = 0;
  410. return 0;
  411. err:
  412. kfree(tx_ring->tx_bi);
  413. tx_ring->tx_bi = NULL;
  414. return -ENOMEM;
  415. }
  416. /**
  417. * i40evf_clean_rx_ring - Free Rx buffers
  418. * @rx_ring: ring to be cleaned
  419. **/
  420. void i40evf_clean_rx_ring(struct i40e_ring *rx_ring)
  421. {
  422. unsigned long bi_size;
  423. u16 i;
  424. /* ring already cleared, nothing to do */
  425. if (!rx_ring->rx_bi)
  426. return;
  427. if (rx_ring->skb) {
  428. dev_kfree_skb(rx_ring->skb);
  429. rx_ring->skb = NULL;
  430. }
  431. /* Free all the Rx ring sk_buffs */
  432. for (i = 0; i < rx_ring->count; i++) {
  433. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  434. if (!rx_bi->page)
  435. continue;
  436. /* Invalidate cache lines that may have been written to by
  437. * device so that we avoid corrupting memory.
  438. */
  439. dma_sync_single_range_for_cpu(rx_ring->dev,
  440. rx_bi->dma,
  441. rx_bi->page_offset,
  442. rx_ring->rx_buf_len,
  443. DMA_FROM_DEVICE);
  444. /* free resources associated with mapping */
  445. dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
  446. i40e_rx_pg_size(rx_ring),
  447. DMA_FROM_DEVICE,
  448. I40E_RX_DMA_ATTR);
  449. __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
  450. rx_bi->page = NULL;
  451. rx_bi->page_offset = 0;
  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_alloc = 0;
  458. rx_ring->next_to_clean = 0;
  459. rx_ring->next_to_use = 0;
  460. }
  461. /**
  462. * i40evf_free_rx_resources - Free Rx resources
  463. * @rx_ring: ring to clean the resources from
  464. *
  465. * Free all receive software resources
  466. **/
  467. void i40evf_free_rx_resources(struct i40e_ring *rx_ring)
  468. {
  469. i40evf_clean_rx_ring(rx_ring);
  470. kfree(rx_ring->rx_bi);
  471. rx_ring->rx_bi = NULL;
  472. if (rx_ring->desc) {
  473. dma_free_coherent(rx_ring->dev, rx_ring->size,
  474. rx_ring->desc, rx_ring->dma);
  475. rx_ring->desc = NULL;
  476. }
  477. }
  478. /**
  479. * i40evf_setup_rx_descriptors - Allocate Rx descriptors
  480. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  481. *
  482. * Returns 0 on success, negative on failure
  483. **/
  484. int i40evf_setup_rx_descriptors(struct i40e_ring *rx_ring)
  485. {
  486. struct device *dev = rx_ring->dev;
  487. int bi_size;
  488. /* warn if we are about to overwrite the pointer */
  489. WARN_ON(rx_ring->rx_bi);
  490. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  491. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  492. if (!rx_ring->rx_bi)
  493. goto err;
  494. u64_stats_init(&rx_ring->syncp);
  495. /* Round up to nearest 4K */
  496. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  497. rx_ring->size = ALIGN(rx_ring->size, 4096);
  498. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  499. &rx_ring->dma, GFP_KERNEL);
  500. if (!rx_ring->desc) {
  501. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  502. rx_ring->size);
  503. goto err;
  504. }
  505. rx_ring->next_to_alloc = 0;
  506. rx_ring->next_to_clean = 0;
  507. rx_ring->next_to_use = 0;
  508. return 0;
  509. err:
  510. kfree(rx_ring->rx_bi);
  511. rx_ring->rx_bi = NULL;
  512. return -ENOMEM;
  513. }
  514. /**
  515. * i40e_release_rx_desc - Store the new tail and head values
  516. * @rx_ring: ring to bump
  517. * @val: new head index
  518. **/
  519. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  520. {
  521. rx_ring->next_to_use = val;
  522. /* update next to alloc since we have filled the ring */
  523. rx_ring->next_to_alloc = val;
  524. /* Force memory writes to complete before letting h/w
  525. * know there are new descriptors to fetch. (Only
  526. * applicable for weak-ordered memory model archs,
  527. * such as IA-64).
  528. */
  529. wmb();
  530. writel(val, rx_ring->tail);
  531. }
  532. /**
  533. * i40e_rx_offset - Return expected offset into page to access data
  534. * @rx_ring: Ring we are requesting offset of
  535. *
  536. * Returns the offset value for ring into the data buffer.
  537. */
  538. static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  539. {
  540. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  541. }
  542. /**
  543. * i40e_alloc_mapped_page - recycle or make a new page
  544. * @rx_ring: ring to use
  545. * @bi: rx_buffer struct to modify
  546. *
  547. * Returns true if the page was successfully allocated or
  548. * reused.
  549. **/
  550. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  551. struct i40e_rx_buffer *bi)
  552. {
  553. struct page *page = bi->page;
  554. dma_addr_t dma;
  555. /* since we are recycling buffers we should seldom need to alloc */
  556. if (likely(page)) {
  557. rx_ring->rx_stats.page_reuse_count++;
  558. return true;
  559. }
  560. /* alloc new page for storage */
  561. page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
  562. if (unlikely(!page)) {
  563. rx_ring->rx_stats.alloc_page_failed++;
  564. return false;
  565. }
  566. /* map page for use */
  567. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  568. i40e_rx_pg_size(rx_ring),
  569. DMA_FROM_DEVICE,
  570. I40E_RX_DMA_ATTR);
  571. /* if mapping failed free memory back to system since
  572. * there isn't much point in holding memory we can't use
  573. */
  574. if (dma_mapping_error(rx_ring->dev, dma)) {
  575. __free_pages(page, i40e_rx_pg_order(rx_ring));
  576. rx_ring->rx_stats.alloc_page_failed++;
  577. return false;
  578. }
  579. bi->dma = dma;
  580. bi->page = page;
  581. bi->page_offset = i40e_rx_offset(rx_ring);
  582. /* initialize pagecnt_bias to 1 representing we fully own page */
  583. bi->pagecnt_bias = 1;
  584. return true;
  585. }
  586. /**
  587. * i40e_receive_skb - Send a completed packet up the stack
  588. * @rx_ring: rx ring in play
  589. * @skb: packet to send up
  590. * @vlan_tag: vlan tag for packet
  591. **/
  592. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  593. struct sk_buff *skb, u16 vlan_tag)
  594. {
  595. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  596. if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  597. (vlan_tag & VLAN_VID_MASK))
  598. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  599. napi_gro_receive(&q_vector->napi, skb);
  600. }
  601. /**
  602. * i40evf_alloc_rx_buffers - Replace used receive buffers
  603. * @rx_ring: ring to place buffers on
  604. * @cleaned_count: number of buffers to replace
  605. *
  606. * Returns false if all allocations were successful, true if any fail
  607. **/
  608. bool i40evf_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  609. {
  610. u16 ntu = rx_ring->next_to_use;
  611. union i40e_rx_desc *rx_desc;
  612. struct i40e_rx_buffer *bi;
  613. /* do nothing if no valid netdev defined */
  614. if (!rx_ring->netdev || !cleaned_count)
  615. return false;
  616. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  617. bi = &rx_ring->rx_bi[ntu];
  618. do {
  619. if (!i40e_alloc_mapped_page(rx_ring, bi))
  620. goto no_buffers;
  621. /* sync the buffer for use by the device */
  622. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  623. bi->page_offset,
  624. rx_ring->rx_buf_len,
  625. DMA_FROM_DEVICE);
  626. /* Refresh the desc even if buffer_addrs didn't change
  627. * because each write-back erases this info.
  628. */
  629. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  630. rx_desc++;
  631. bi++;
  632. ntu++;
  633. if (unlikely(ntu == rx_ring->count)) {
  634. rx_desc = I40E_RX_DESC(rx_ring, 0);
  635. bi = rx_ring->rx_bi;
  636. ntu = 0;
  637. }
  638. /* clear the status bits for the next_to_use descriptor */
  639. rx_desc->wb.qword1.status_error_len = 0;
  640. cleaned_count--;
  641. } while (cleaned_count);
  642. if (rx_ring->next_to_use != ntu)
  643. i40e_release_rx_desc(rx_ring, ntu);
  644. return false;
  645. no_buffers:
  646. if (rx_ring->next_to_use != ntu)
  647. i40e_release_rx_desc(rx_ring, ntu);
  648. /* make sure to come back via polling to try again after
  649. * allocation failure
  650. */
  651. return true;
  652. }
  653. /**
  654. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  655. * @vsi: the VSI we care about
  656. * @skb: skb currently being received and modified
  657. * @rx_desc: the receive descriptor
  658. **/
  659. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  660. struct sk_buff *skb,
  661. union i40e_rx_desc *rx_desc)
  662. {
  663. struct i40e_rx_ptype_decoded decoded;
  664. u32 rx_error, rx_status;
  665. bool ipv4, ipv6;
  666. u8 ptype;
  667. u64 qword;
  668. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  669. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  670. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  671. I40E_RXD_QW1_ERROR_SHIFT;
  672. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  673. I40E_RXD_QW1_STATUS_SHIFT;
  674. decoded = decode_rx_desc_ptype(ptype);
  675. skb->ip_summed = CHECKSUM_NONE;
  676. skb_checksum_none_assert(skb);
  677. /* Rx csum enabled and ip headers found? */
  678. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  679. return;
  680. /* did the hardware decode the packet and checksum? */
  681. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  682. return;
  683. /* both known and outer_ip must be set for the below code to work */
  684. if (!(decoded.known && decoded.outer_ip))
  685. return;
  686. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  687. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  688. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  689. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  690. if (ipv4 &&
  691. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  692. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  693. goto checksum_fail;
  694. /* likely incorrect csum if alternate IP extension headers found */
  695. if (ipv6 &&
  696. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  697. /* don't increment checksum err here, non-fatal err */
  698. return;
  699. /* there was some L4 error, count error and punt packet to the stack */
  700. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  701. goto checksum_fail;
  702. /* handle packets that were not able to be checksummed due
  703. * to arrival speed, in this case the stack can compute
  704. * the csum.
  705. */
  706. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  707. return;
  708. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  709. switch (decoded.inner_prot) {
  710. case I40E_RX_PTYPE_INNER_PROT_TCP:
  711. case I40E_RX_PTYPE_INNER_PROT_UDP:
  712. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  713. skb->ip_summed = CHECKSUM_UNNECESSARY;
  714. /* fall though */
  715. default:
  716. break;
  717. }
  718. return;
  719. checksum_fail:
  720. vsi->back->hw_csum_rx_error++;
  721. }
  722. /**
  723. * i40e_ptype_to_htype - get a hash type
  724. * @ptype: the ptype value from the descriptor
  725. *
  726. * Returns a hash type to be used by skb_set_hash
  727. **/
  728. static inline int i40e_ptype_to_htype(u8 ptype)
  729. {
  730. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  731. if (!decoded.known)
  732. return PKT_HASH_TYPE_NONE;
  733. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  734. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  735. return PKT_HASH_TYPE_L4;
  736. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  737. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  738. return PKT_HASH_TYPE_L3;
  739. else
  740. return PKT_HASH_TYPE_L2;
  741. }
  742. /**
  743. * i40e_rx_hash - set the hash value in the skb
  744. * @ring: descriptor ring
  745. * @rx_desc: specific descriptor
  746. **/
  747. static inline void i40e_rx_hash(struct i40e_ring *ring,
  748. union i40e_rx_desc *rx_desc,
  749. struct sk_buff *skb,
  750. u8 rx_ptype)
  751. {
  752. u32 hash;
  753. const __le64 rss_mask =
  754. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  755. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  756. if (ring->netdev->features & NETIF_F_RXHASH)
  757. return;
  758. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  759. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  760. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  761. }
  762. }
  763. /**
  764. * i40evf_process_skb_fields - Populate skb header fields from Rx descriptor
  765. * @rx_ring: rx descriptor ring packet is being transacted on
  766. * @rx_desc: pointer to the EOP Rx descriptor
  767. * @skb: pointer to current skb being populated
  768. * @rx_ptype: the packet type decoded by hardware
  769. *
  770. * This function checks the ring, descriptor, and packet information in
  771. * order to populate the hash, checksum, VLAN, protocol, and
  772. * other fields within the skb.
  773. **/
  774. static inline
  775. void i40evf_process_skb_fields(struct i40e_ring *rx_ring,
  776. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  777. u8 rx_ptype)
  778. {
  779. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  780. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  781. skb_record_rx_queue(skb, rx_ring->queue_index);
  782. /* modifies the skb - consumes the enet header */
  783. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  784. }
  785. /**
  786. * i40e_cleanup_headers - Correct empty headers
  787. * @rx_ring: rx descriptor ring packet is being transacted on
  788. * @skb: pointer to current skb being fixed
  789. *
  790. * Also address the case where we are pulling data in on pages only
  791. * and as such no data is present in the skb header.
  792. *
  793. * In addition if skb is not at least 60 bytes we need to pad it so that
  794. * it is large enough to qualify as a valid Ethernet frame.
  795. *
  796. * Returns true if an error was encountered and skb was freed.
  797. **/
  798. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb)
  799. {
  800. /* if eth_skb_pad returns an error the skb was freed */
  801. if (eth_skb_pad(skb))
  802. return true;
  803. return false;
  804. }
  805. /**
  806. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  807. * @rx_ring: rx descriptor ring to store buffers on
  808. * @old_buff: donor buffer to have page reused
  809. *
  810. * Synchronizes page for reuse by the adapter
  811. **/
  812. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  813. struct i40e_rx_buffer *old_buff)
  814. {
  815. struct i40e_rx_buffer *new_buff;
  816. u16 nta = rx_ring->next_to_alloc;
  817. new_buff = &rx_ring->rx_bi[nta];
  818. /* update, and store next to alloc */
  819. nta++;
  820. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  821. /* transfer page from old buffer to new buffer */
  822. new_buff->dma = old_buff->dma;
  823. new_buff->page = old_buff->page;
  824. new_buff->page_offset = old_buff->page_offset;
  825. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  826. }
  827. /**
  828. * i40e_page_is_reusable - check if any reuse is possible
  829. * @page: page struct to check
  830. *
  831. * A page is not reusable if it was allocated under low memory
  832. * conditions, or it's not in the same NUMA node as this CPU.
  833. */
  834. static inline bool i40e_page_is_reusable(struct page *page)
  835. {
  836. return (page_to_nid(page) == numa_mem_id()) &&
  837. !page_is_pfmemalloc(page);
  838. }
  839. /**
  840. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  841. * the adapter for another receive
  842. *
  843. * @rx_buffer: buffer containing the page
  844. *
  845. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  846. * an unused region in the page.
  847. *
  848. * For small pages, @truesize will be a constant value, half the size
  849. * of the memory at page. We'll attempt to alternate between high and
  850. * low halves of the page, with one half ready for use by the hardware
  851. * and the other half being consumed by the stack. We use the page
  852. * ref count to determine whether the stack has finished consuming the
  853. * portion of this page that was passed up with a previous packet. If
  854. * the page ref count is >1, we'll assume the "other" half page is
  855. * still busy, and this page cannot be reused.
  856. *
  857. * For larger pages, @truesize will be the actual space used by the
  858. * received packet (adjusted upward to an even multiple of the cache
  859. * line size). This will advance through the page by the amount
  860. * actually consumed by the received packets while there is still
  861. * space for a buffer. Each region of larger pages will be used at
  862. * most once, after which the page will not be reused.
  863. *
  864. * In either case, if the page is reusable its refcount is increased.
  865. **/
  866. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  867. {
  868. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  869. struct page *page = rx_buffer->page;
  870. /* Is any reuse possible? */
  871. if (unlikely(!i40e_page_is_reusable(page)))
  872. return false;
  873. #if (PAGE_SIZE < 8192)
  874. /* if we are only owner of page we can reuse it */
  875. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  876. return false;
  877. #else
  878. #define I40E_LAST_OFFSET \
  879. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  880. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  881. return false;
  882. #endif
  883. /* If we have drained the page fragment pool we need to update
  884. * the pagecnt_bias and page count so that we fully restock the
  885. * number of references the driver holds.
  886. */
  887. if (unlikely(!pagecnt_bias)) {
  888. page_ref_add(page, USHRT_MAX);
  889. rx_buffer->pagecnt_bias = USHRT_MAX;
  890. }
  891. return true;
  892. }
  893. /**
  894. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  895. * @rx_ring: rx descriptor ring to transact packets on
  896. * @rx_buffer: buffer containing page to add
  897. * @skb: sk_buff to place the data into
  898. * @size: packet length from rx_desc
  899. *
  900. * This function will add the data contained in rx_buffer->page to the skb.
  901. * It will just attach the page as a frag to the skb.
  902. *
  903. * The function will then update the page offset.
  904. **/
  905. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  906. struct i40e_rx_buffer *rx_buffer,
  907. struct sk_buff *skb,
  908. unsigned int size)
  909. {
  910. #if (PAGE_SIZE < 8192)
  911. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  912. #else
  913. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  914. #endif
  915. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  916. rx_buffer->page_offset, size, truesize);
  917. /* page is being used so we must update the page offset */
  918. #if (PAGE_SIZE < 8192)
  919. rx_buffer->page_offset ^= truesize;
  920. #else
  921. rx_buffer->page_offset += truesize;
  922. #endif
  923. }
  924. /**
  925. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  926. * @rx_ring: rx descriptor ring to transact packets on
  927. * @size: size of buffer to add to skb
  928. *
  929. * This function will pull an Rx buffer from the ring and synchronize it
  930. * for use by the CPU.
  931. */
  932. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  933. const unsigned int size)
  934. {
  935. struct i40e_rx_buffer *rx_buffer;
  936. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  937. prefetchw(rx_buffer->page);
  938. /* we are reusing so sync this buffer for CPU use */
  939. dma_sync_single_range_for_cpu(rx_ring->dev,
  940. rx_buffer->dma,
  941. rx_buffer->page_offset,
  942. size,
  943. DMA_FROM_DEVICE);
  944. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  945. rx_buffer->pagecnt_bias--;
  946. return rx_buffer;
  947. }
  948. /**
  949. * i40e_construct_skb - Allocate skb and populate it
  950. * @rx_ring: rx descriptor ring to transact packets on
  951. * @rx_buffer: rx buffer to pull data from
  952. * @size: size of buffer to add to skb
  953. *
  954. * This function allocates an skb. It then populates it with the page
  955. * data from the current receive descriptor, taking care to set up the
  956. * skb correctly.
  957. */
  958. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  959. struct i40e_rx_buffer *rx_buffer,
  960. unsigned int size)
  961. {
  962. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  963. #if (PAGE_SIZE < 8192)
  964. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  965. #else
  966. unsigned int truesize = SKB_DATA_ALIGN(size);
  967. #endif
  968. unsigned int headlen;
  969. struct sk_buff *skb;
  970. /* prefetch first cache line of first page */
  971. prefetch(va);
  972. #if L1_CACHE_BYTES < 128
  973. prefetch(va + L1_CACHE_BYTES);
  974. #endif
  975. /* allocate a skb to store the frags */
  976. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  977. I40E_RX_HDR_SIZE,
  978. GFP_ATOMIC | __GFP_NOWARN);
  979. if (unlikely(!skb))
  980. return NULL;
  981. /* Determine available headroom for copy */
  982. headlen = size;
  983. if (headlen > I40E_RX_HDR_SIZE)
  984. headlen = eth_get_headlen(va, I40E_RX_HDR_SIZE);
  985. /* align pull length to size of long to optimize memcpy performance */
  986. memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
  987. /* update all of the pointers */
  988. size -= headlen;
  989. if (size) {
  990. skb_add_rx_frag(skb, 0, rx_buffer->page,
  991. rx_buffer->page_offset + headlen,
  992. size, truesize);
  993. /* buffer is used by skb, update page_offset */
  994. #if (PAGE_SIZE < 8192)
  995. rx_buffer->page_offset ^= truesize;
  996. #else
  997. rx_buffer->page_offset += truesize;
  998. #endif
  999. } else {
  1000. /* buffer is unused, reset bias back to rx_buffer */
  1001. rx_buffer->pagecnt_bias++;
  1002. }
  1003. return skb;
  1004. }
  1005. /**
  1006. * i40e_build_skb - Build skb around an existing buffer
  1007. * @rx_ring: Rx descriptor ring to transact packets on
  1008. * @rx_buffer: Rx buffer to pull data from
  1009. * @size: size of buffer to add to skb
  1010. *
  1011. * This function builds an skb around an existing Rx buffer, taking care
  1012. * to set up the skb correctly and avoid any memcpy overhead.
  1013. */
  1014. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1015. struct i40e_rx_buffer *rx_buffer,
  1016. unsigned int size)
  1017. {
  1018. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  1019. #if (PAGE_SIZE < 8192)
  1020. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1021. #else
  1022. unsigned int truesize = SKB_DATA_ALIGN(size);
  1023. #endif
  1024. struct sk_buff *skb;
  1025. /* prefetch first cache line of first page */
  1026. prefetch(va);
  1027. #if L1_CACHE_BYTES < 128
  1028. prefetch(va + L1_CACHE_BYTES);
  1029. #endif
  1030. /* build an skb around the page buffer */
  1031. skb = build_skb(va - I40E_SKB_PAD, truesize);
  1032. if (unlikely(!skb))
  1033. return NULL;
  1034. /* update pointers within the skb to store the data */
  1035. skb_reserve(skb, I40E_SKB_PAD);
  1036. __skb_put(skb, size);
  1037. /* buffer is used by skb, update page_offset */
  1038. #if (PAGE_SIZE < 8192)
  1039. rx_buffer->page_offset ^= truesize;
  1040. #else
  1041. rx_buffer->page_offset += truesize;
  1042. #endif
  1043. return skb;
  1044. }
  1045. /**
  1046. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1047. * @rx_ring: rx descriptor ring to transact packets on
  1048. * @rx_buffer: rx buffer to pull data from
  1049. *
  1050. * This function will clean up the contents of the rx_buffer. It will
  1051. * either recycle the bufer or unmap it and free the associated resources.
  1052. */
  1053. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1054. struct i40e_rx_buffer *rx_buffer)
  1055. {
  1056. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1057. /* hand second half of page back to the ring */
  1058. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1059. rx_ring->rx_stats.page_reuse_count++;
  1060. } else {
  1061. /* we are not reusing the buffer so unmap it */
  1062. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1063. i40e_rx_pg_size(rx_ring),
  1064. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1065. __page_frag_cache_drain(rx_buffer->page,
  1066. rx_buffer->pagecnt_bias);
  1067. }
  1068. /* clear contents of buffer_info */
  1069. rx_buffer->page = NULL;
  1070. }
  1071. /**
  1072. * i40e_is_non_eop - process handling of non-EOP buffers
  1073. * @rx_ring: Rx ring being processed
  1074. * @rx_desc: Rx descriptor for current buffer
  1075. * @skb: Current socket buffer containing buffer in progress
  1076. *
  1077. * This function updates next to clean. If the buffer is an EOP buffer
  1078. * this function exits returning false, otherwise it will place the
  1079. * sk_buff in the next buffer to be chained and return true indicating
  1080. * that this is in fact a non-EOP buffer.
  1081. **/
  1082. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1083. union i40e_rx_desc *rx_desc,
  1084. struct sk_buff *skb)
  1085. {
  1086. u32 ntc = rx_ring->next_to_clean + 1;
  1087. /* fetch, update, and store next to clean */
  1088. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1089. rx_ring->next_to_clean = ntc;
  1090. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1091. /* if we are the last buffer then there is nothing else to do */
  1092. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1093. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1094. return false;
  1095. rx_ring->rx_stats.non_eop_descs++;
  1096. return true;
  1097. }
  1098. /**
  1099. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1100. * @rx_ring: rx descriptor ring to transact packets on
  1101. * @budget: Total limit on number of packets to process
  1102. *
  1103. * This function provides a "bounce buffer" approach to Rx interrupt
  1104. * processing. The advantage to this is that on systems that have
  1105. * expensive overhead for IOMMU access this provides a means of avoiding
  1106. * it by maintaining the mapping of the page to the system.
  1107. *
  1108. * Returns amount of work completed
  1109. **/
  1110. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1111. {
  1112. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1113. struct sk_buff *skb = rx_ring->skb;
  1114. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1115. bool failure = false;
  1116. while (likely(total_rx_packets < budget)) {
  1117. struct i40e_rx_buffer *rx_buffer;
  1118. union i40e_rx_desc *rx_desc;
  1119. unsigned int size;
  1120. u16 vlan_tag;
  1121. u8 rx_ptype;
  1122. u64 qword;
  1123. /* return some buffers to hardware, one at a time is too slow */
  1124. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1125. failure = failure ||
  1126. i40evf_alloc_rx_buffers(rx_ring, cleaned_count);
  1127. cleaned_count = 0;
  1128. }
  1129. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1130. /* status_error_len will always be zero for unused descriptors
  1131. * because it's cleared in cleanup, and overlaps with hdr_addr
  1132. * which is always zero because packet split isn't used, if the
  1133. * hardware wrote DD then the length will be non-zero
  1134. */
  1135. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1136. /* This memory barrier is needed to keep us from reading
  1137. * any other fields out of the rx_desc until we have
  1138. * verified the descriptor has been written back.
  1139. */
  1140. dma_rmb();
  1141. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1142. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1143. if (!size)
  1144. break;
  1145. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  1146. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  1147. /* retrieve a buffer from the ring */
  1148. if (skb)
  1149. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  1150. else if (ring_uses_build_skb(rx_ring))
  1151. skb = i40e_build_skb(rx_ring, rx_buffer, size);
  1152. else
  1153. skb = i40e_construct_skb(rx_ring, rx_buffer, size);
  1154. /* exit if we failed to retrieve a buffer */
  1155. if (!skb) {
  1156. rx_ring->rx_stats.alloc_buff_failed++;
  1157. rx_buffer->pagecnt_bias++;
  1158. break;
  1159. }
  1160. i40e_put_rx_buffer(rx_ring, rx_buffer);
  1161. cleaned_count++;
  1162. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  1163. continue;
  1164. /* ERR_MASK will only have valid bits if EOP set, and
  1165. * what we are doing here is actually checking
  1166. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1167. * the error field
  1168. */
  1169. if (unlikely(i40e_test_staterr(rx_desc, BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1170. dev_kfree_skb_any(skb);
  1171. skb = NULL;
  1172. continue;
  1173. }
  1174. if (i40e_cleanup_headers(rx_ring, skb)) {
  1175. skb = NULL;
  1176. continue;
  1177. }
  1178. /* probably a little skewed due to removing CRC */
  1179. total_rx_bytes += skb->len;
  1180. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1181. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1182. I40E_RXD_QW1_PTYPE_SHIFT;
  1183. /* populate checksum, VLAN, and protocol */
  1184. i40evf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  1185. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  1186. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  1187. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  1188. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1189. skb = NULL;
  1190. /* update budget accounting */
  1191. total_rx_packets++;
  1192. }
  1193. rx_ring->skb = skb;
  1194. u64_stats_update_begin(&rx_ring->syncp);
  1195. rx_ring->stats.packets += total_rx_packets;
  1196. rx_ring->stats.bytes += total_rx_bytes;
  1197. u64_stats_update_end(&rx_ring->syncp);
  1198. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1199. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1200. /* guarantee a trip back through this routine if there was a failure */
  1201. return failure ? budget : total_rx_packets;
  1202. }
  1203. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1204. {
  1205. u32 val;
  1206. val = I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  1207. /* Don't clear PBA because that can cause lost interrupts that
  1208. * came in while we were cleaning/polling
  1209. */
  1210. (type << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
  1211. (itr << I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT);
  1212. return val;
  1213. }
  1214. /* a small macro to shorten up some long lines */
  1215. #define INTREG I40E_VFINT_DYN_CTLN1
  1216. static inline int get_rx_itr(struct i40e_vsi *vsi, int idx)
  1217. {
  1218. struct i40evf_adapter *adapter = vsi->back;
  1219. return adapter->rx_rings[idx].rx_itr_setting;
  1220. }
  1221. static inline int get_tx_itr(struct i40e_vsi *vsi, int idx)
  1222. {
  1223. struct i40evf_adapter *adapter = vsi->back;
  1224. return adapter->tx_rings[idx].tx_itr_setting;
  1225. }
  1226. /**
  1227. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1228. * @vsi: the VSI we care about
  1229. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1230. *
  1231. **/
  1232. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1233. struct i40e_q_vector *q_vector)
  1234. {
  1235. struct i40e_hw *hw = &vsi->back->hw;
  1236. bool rx = false, tx = false;
  1237. u32 rxval, txval;
  1238. int vector;
  1239. int idx = q_vector->v_idx;
  1240. int rx_itr_setting, tx_itr_setting;
  1241. vector = (q_vector->v_idx + vsi->base_vector);
  1242. /* avoid dynamic calculation if in countdown mode OR if
  1243. * all dynamic is disabled
  1244. */
  1245. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1246. rx_itr_setting = get_rx_itr(vsi, idx);
  1247. tx_itr_setting = get_tx_itr(vsi, idx);
  1248. if (q_vector->itr_countdown > 0 ||
  1249. (!ITR_IS_DYNAMIC(rx_itr_setting) &&
  1250. !ITR_IS_DYNAMIC(tx_itr_setting))) {
  1251. goto enable_int;
  1252. }
  1253. if (ITR_IS_DYNAMIC(rx_itr_setting)) {
  1254. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1255. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1256. }
  1257. if (ITR_IS_DYNAMIC(tx_itr_setting)) {
  1258. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1259. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1260. }
  1261. if (rx || tx) {
  1262. /* get the higher of the two ITR adjustments and
  1263. * use the same value for both ITR registers
  1264. * when in adaptive mode (Rx and/or Tx)
  1265. */
  1266. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1267. q_vector->tx.itr = q_vector->rx.itr = itr;
  1268. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1269. tx = true;
  1270. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1271. rx = true;
  1272. }
  1273. /* only need to enable the interrupt once, but need
  1274. * to possibly update both ITR values
  1275. */
  1276. if (rx) {
  1277. /* set the INTENA_MSK_MASK so that this first write
  1278. * won't actually enable the interrupt, instead just
  1279. * updating the ITR (it's bit 31 PF and VF)
  1280. */
  1281. rxval |= BIT(31);
  1282. /* don't check _DOWN because interrupt isn't being enabled */
  1283. wr32(hw, INTREG(vector - 1), rxval);
  1284. }
  1285. enable_int:
  1286. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  1287. wr32(hw, INTREG(vector - 1), txval);
  1288. if (q_vector->itr_countdown)
  1289. q_vector->itr_countdown--;
  1290. else
  1291. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1292. }
  1293. /**
  1294. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  1295. * @napi: napi struct with our devices info in it
  1296. * @budget: amount of work driver is allowed to do this pass, in packets
  1297. *
  1298. * This function will clean all queues associated with a q_vector.
  1299. *
  1300. * Returns the amount of work done
  1301. **/
  1302. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  1303. {
  1304. struct i40e_q_vector *q_vector =
  1305. container_of(napi, struct i40e_q_vector, napi);
  1306. struct i40e_vsi *vsi = q_vector->vsi;
  1307. struct i40e_ring *ring;
  1308. bool clean_complete = true;
  1309. bool arm_wb = false;
  1310. int budget_per_ring;
  1311. int work_done = 0;
  1312. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  1313. napi_complete(napi);
  1314. return 0;
  1315. }
  1316. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1317. * budget and be more aggressive about cleaning up the Tx descriptors.
  1318. */
  1319. i40e_for_each_ring(ring, q_vector->tx) {
  1320. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  1321. clean_complete = false;
  1322. continue;
  1323. }
  1324. arm_wb |= ring->arm_wb;
  1325. ring->arm_wb = false;
  1326. }
  1327. /* Handle case where we are called by netpoll with a budget of 0 */
  1328. if (budget <= 0)
  1329. goto tx_only;
  1330. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1331. * allow the budget to go below 1 because that would exit polling early.
  1332. */
  1333. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1334. i40e_for_each_ring(ring, q_vector->rx) {
  1335. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  1336. work_done += cleaned;
  1337. /* if we clean as many as budgeted, we must not be done */
  1338. if (cleaned >= budget_per_ring)
  1339. clean_complete = false;
  1340. }
  1341. /* If work not completed, return budget and polling will return */
  1342. if (!clean_complete) {
  1343. const cpumask_t *aff_mask = &q_vector->affinity_mask;
  1344. int cpu_id = smp_processor_id();
  1345. /* It is possible that the interrupt affinity has changed but,
  1346. * if the cpu is pegged at 100%, polling will never exit while
  1347. * traffic continues and the interrupt will be stuck on this
  1348. * cpu. We check to make sure affinity is correct before we
  1349. * continue to poll, otherwise we must stop polling so the
  1350. * interrupt can move to the correct cpu.
  1351. */
  1352. if (likely(cpumask_test_cpu(cpu_id, aff_mask))) {
  1353. tx_only:
  1354. if (arm_wb) {
  1355. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  1356. i40e_enable_wb_on_itr(vsi, q_vector);
  1357. }
  1358. return budget;
  1359. }
  1360. }
  1361. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  1362. q_vector->arm_wb_state = false;
  1363. /* Work is done so exit the polling mode and re-enable the interrupt */
  1364. napi_complete_done(napi, work_done);
  1365. /* If we're prematurely stopping polling to fix the interrupt
  1366. * affinity we want to make sure polling starts back up so we
  1367. * issue a call to i40evf_force_wb which triggers a SW interrupt.
  1368. */
  1369. if (!clean_complete)
  1370. i40evf_force_wb(vsi, q_vector);
  1371. else
  1372. i40e_update_enable_itr(vsi, q_vector);
  1373. return min(work_done, budget - 1);
  1374. }
  1375. /**
  1376. * i40evf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1377. * @skb: send buffer
  1378. * @tx_ring: ring to send buffer on
  1379. * @flags: the tx flags to be set
  1380. *
  1381. * Checks the skb and set up correspondingly several generic transmit flags
  1382. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1383. *
  1384. * Returns error code indicate the frame should be dropped upon error and the
  1385. * otherwise returns 0 to indicate the flags has been set properly.
  1386. **/
  1387. static inline int i40evf_tx_prepare_vlan_flags(struct sk_buff *skb,
  1388. struct i40e_ring *tx_ring,
  1389. u32 *flags)
  1390. {
  1391. __be16 protocol = skb->protocol;
  1392. u32 tx_flags = 0;
  1393. if (protocol == htons(ETH_P_8021Q) &&
  1394. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  1395. /* When HW VLAN acceleration is turned off by the user the
  1396. * stack sets the protocol to 8021q so that the driver
  1397. * can take any steps required to support the SW only
  1398. * VLAN handling. In our case the driver doesn't need
  1399. * to take any further steps so just set the protocol
  1400. * to the encapsulated ethertype.
  1401. */
  1402. skb->protocol = vlan_get_protocol(skb);
  1403. goto out;
  1404. }
  1405. /* if we have a HW VLAN tag being added, default to the HW one */
  1406. if (skb_vlan_tag_present(skb)) {
  1407. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1408. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1409. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1410. } else if (protocol == htons(ETH_P_8021Q)) {
  1411. struct vlan_hdr *vhdr, _vhdr;
  1412. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1413. if (!vhdr)
  1414. return -EINVAL;
  1415. protocol = vhdr->h_vlan_encapsulated_proto;
  1416. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1417. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1418. }
  1419. out:
  1420. *flags = tx_flags;
  1421. return 0;
  1422. }
  1423. /**
  1424. * i40e_tso - set up the tso context descriptor
  1425. * @first: pointer to first Tx buffer for xmit
  1426. * @hdr_len: ptr to the size of the packet header
  1427. * @cd_type_cmd_tso_mss: Quad Word 1
  1428. *
  1429. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1430. **/
  1431. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  1432. u64 *cd_type_cmd_tso_mss)
  1433. {
  1434. struct sk_buff *skb = first->skb;
  1435. u64 cd_cmd, cd_tso_len, cd_mss;
  1436. union {
  1437. struct iphdr *v4;
  1438. struct ipv6hdr *v6;
  1439. unsigned char *hdr;
  1440. } ip;
  1441. union {
  1442. struct tcphdr *tcp;
  1443. struct udphdr *udp;
  1444. unsigned char *hdr;
  1445. } l4;
  1446. u32 paylen, l4_offset;
  1447. u16 gso_segs, gso_size;
  1448. int err;
  1449. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1450. return 0;
  1451. if (!skb_is_gso(skb))
  1452. return 0;
  1453. err = skb_cow_head(skb, 0);
  1454. if (err < 0)
  1455. return err;
  1456. ip.hdr = skb_network_header(skb);
  1457. l4.hdr = skb_transport_header(skb);
  1458. /* initialize outer IP header fields */
  1459. if (ip.v4->version == 4) {
  1460. ip.v4->tot_len = 0;
  1461. ip.v4->check = 0;
  1462. } else {
  1463. ip.v6->payload_len = 0;
  1464. }
  1465. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  1466. SKB_GSO_GRE_CSUM |
  1467. SKB_GSO_IPXIP4 |
  1468. SKB_GSO_IPXIP6 |
  1469. SKB_GSO_UDP_TUNNEL |
  1470. SKB_GSO_UDP_TUNNEL_CSUM)) {
  1471. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1472. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  1473. l4.udp->len = 0;
  1474. /* determine offset of outer transport header */
  1475. l4_offset = l4.hdr - skb->data;
  1476. /* remove payload length from outer checksum */
  1477. paylen = skb->len - l4_offset;
  1478. csum_replace_by_diff(&l4.udp->check,
  1479. (__force __wsum)htonl(paylen));
  1480. }
  1481. /* reset pointers to inner headers */
  1482. ip.hdr = skb_inner_network_header(skb);
  1483. l4.hdr = skb_inner_transport_header(skb);
  1484. /* initialize inner IP header fields */
  1485. if (ip.v4->version == 4) {
  1486. ip.v4->tot_len = 0;
  1487. ip.v4->check = 0;
  1488. } else {
  1489. ip.v6->payload_len = 0;
  1490. }
  1491. }
  1492. /* determine offset of inner transport header */
  1493. l4_offset = l4.hdr - skb->data;
  1494. /* remove payload length from inner checksum */
  1495. paylen = skb->len - l4_offset;
  1496. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  1497. /* compute length of segmentation header */
  1498. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  1499. /* pull values out of skb_shinfo */
  1500. gso_size = skb_shinfo(skb)->gso_size;
  1501. gso_segs = skb_shinfo(skb)->gso_segs;
  1502. /* update GSO size and bytecount with header size */
  1503. first->gso_segs = gso_segs;
  1504. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  1505. /* find the field values */
  1506. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1507. cd_tso_len = skb->len - *hdr_len;
  1508. cd_mss = gso_size;
  1509. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1510. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1511. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1512. return 1;
  1513. }
  1514. /**
  1515. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1516. * @skb: send buffer
  1517. * @tx_flags: pointer to Tx flags currently set
  1518. * @td_cmd: Tx descriptor command bits to set
  1519. * @td_offset: Tx descriptor header offsets to set
  1520. * @tx_ring: Tx descriptor ring
  1521. * @cd_tunneling: ptr to context desc bits
  1522. **/
  1523. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  1524. u32 *td_cmd, u32 *td_offset,
  1525. struct i40e_ring *tx_ring,
  1526. u32 *cd_tunneling)
  1527. {
  1528. union {
  1529. struct iphdr *v4;
  1530. struct ipv6hdr *v6;
  1531. unsigned char *hdr;
  1532. } ip;
  1533. union {
  1534. struct tcphdr *tcp;
  1535. struct udphdr *udp;
  1536. unsigned char *hdr;
  1537. } l4;
  1538. unsigned char *exthdr;
  1539. u32 offset, cmd = 0;
  1540. __be16 frag_off;
  1541. u8 l4_proto = 0;
  1542. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1543. return 0;
  1544. ip.hdr = skb_network_header(skb);
  1545. l4.hdr = skb_transport_header(skb);
  1546. /* compute outer L2 header size */
  1547. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1548. if (skb->encapsulation) {
  1549. u32 tunnel = 0;
  1550. /* define outer network header type */
  1551. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1552. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1553. I40E_TX_CTX_EXT_IP_IPV4 :
  1554. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1555. l4_proto = ip.v4->protocol;
  1556. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1557. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  1558. exthdr = ip.hdr + sizeof(*ip.v6);
  1559. l4_proto = ip.v6->nexthdr;
  1560. if (l4.hdr != exthdr)
  1561. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1562. &l4_proto, &frag_off);
  1563. }
  1564. /* define outer transport */
  1565. switch (l4_proto) {
  1566. case IPPROTO_UDP:
  1567. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  1568. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1569. break;
  1570. case IPPROTO_GRE:
  1571. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  1572. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1573. break;
  1574. case IPPROTO_IPIP:
  1575. case IPPROTO_IPV6:
  1576. *tx_flags |= I40E_TX_FLAGS_VXLAN_TUNNEL;
  1577. l4.hdr = skb_inner_network_header(skb);
  1578. break;
  1579. default:
  1580. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1581. return -1;
  1582. skb_checksum_help(skb);
  1583. return 0;
  1584. }
  1585. /* compute outer L3 header size */
  1586. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  1587. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  1588. /* switch IP header pointer from outer to inner header */
  1589. ip.hdr = skb_inner_network_header(skb);
  1590. /* compute tunnel header size */
  1591. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  1592. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1593. /* indicate if we need to offload outer UDP header */
  1594. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  1595. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  1596. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  1597. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  1598. /* record tunnel offload values */
  1599. *cd_tunneling |= tunnel;
  1600. /* switch L4 header pointer from outer to inner */
  1601. l4.hdr = skb_inner_transport_header(skb);
  1602. l4_proto = 0;
  1603. /* reset type as we transition from outer to inner headers */
  1604. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  1605. if (ip.v4->version == 4)
  1606. *tx_flags |= I40E_TX_FLAGS_IPV4;
  1607. if (ip.v6->version == 6)
  1608. *tx_flags |= I40E_TX_FLAGS_IPV6;
  1609. }
  1610. /* Enable IP checksum offloads */
  1611. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  1612. l4_proto = ip.v4->protocol;
  1613. /* the stack computes the IP header already, the only time we
  1614. * need the hardware to recompute it is in the case of TSO.
  1615. */
  1616. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  1617. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  1618. I40E_TX_DESC_CMD_IIPT_IPV4;
  1619. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  1620. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1621. exthdr = ip.hdr + sizeof(*ip.v6);
  1622. l4_proto = ip.v6->nexthdr;
  1623. if (l4.hdr != exthdr)
  1624. ipv6_skip_exthdr(skb, exthdr - skb->data,
  1625. &l4_proto, &frag_off);
  1626. }
  1627. /* compute inner L3 header size */
  1628. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1629. /* Enable L4 checksum offloads */
  1630. switch (l4_proto) {
  1631. case IPPROTO_TCP:
  1632. /* enable checksum offloads */
  1633. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1634. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1635. break;
  1636. case IPPROTO_SCTP:
  1637. /* enable SCTP checksum offload */
  1638. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1639. offset |= (sizeof(struct sctphdr) >> 2) <<
  1640. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1641. break;
  1642. case IPPROTO_UDP:
  1643. /* enable UDP checksum offload */
  1644. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1645. offset |= (sizeof(struct udphdr) >> 2) <<
  1646. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1647. break;
  1648. default:
  1649. if (*tx_flags & I40E_TX_FLAGS_TSO)
  1650. return -1;
  1651. skb_checksum_help(skb);
  1652. return 0;
  1653. }
  1654. *td_cmd |= cmd;
  1655. *td_offset |= offset;
  1656. return 1;
  1657. }
  1658. /**
  1659. * i40e_create_tx_ctx Build the Tx context descriptor
  1660. * @tx_ring: ring to create the descriptor on
  1661. * @cd_type_cmd_tso_mss: Quad Word 1
  1662. * @cd_tunneling: Quad Word 0 - bits 0-31
  1663. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1664. **/
  1665. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1666. const u64 cd_type_cmd_tso_mss,
  1667. const u32 cd_tunneling, const u32 cd_l2tag2)
  1668. {
  1669. struct i40e_tx_context_desc *context_desc;
  1670. int i = tx_ring->next_to_use;
  1671. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1672. !cd_tunneling && !cd_l2tag2)
  1673. return;
  1674. /* grab the next descriptor */
  1675. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1676. i++;
  1677. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1678. /* cpu_to_le32 and assign to struct fields */
  1679. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1680. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1681. context_desc->rsvd = cpu_to_le16(0);
  1682. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1683. }
  1684. /**
  1685. * __i40evf_chk_linearize - Check if there are more than 8 buffers per packet
  1686. * @skb: send buffer
  1687. *
  1688. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  1689. * and so we need to figure out the cases where we need to linearize the skb.
  1690. *
  1691. * For TSO we need to count the TSO header and segment payload separately.
  1692. * As such we need to check cases where we have 7 fragments or more as we
  1693. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  1694. * the segment payload in the first descriptor, and another 7 for the
  1695. * fragments.
  1696. **/
  1697. bool __i40evf_chk_linearize(struct sk_buff *skb)
  1698. {
  1699. const struct skb_frag_struct *frag, *stale;
  1700. int nr_frags, sum;
  1701. /* no need to check if number of frags is less than 7 */
  1702. nr_frags = skb_shinfo(skb)->nr_frags;
  1703. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  1704. return false;
  1705. /* We need to walk through the list and validate that each group
  1706. * of 6 fragments totals at least gso_size.
  1707. */
  1708. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  1709. frag = &skb_shinfo(skb)->frags[0];
  1710. /* Initialize size to the negative value of gso_size minus 1. We
  1711. * use this as the worst case scenerio in which the frag ahead
  1712. * of us only provides one byte which is why we are limited to 6
  1713. * descriptors for a single transmit as the header and previous
  1714. * fragment are already consuming 2 descriptors.
  1715. */
  1716. sum = 1 - skb_shinfo(skb)->gso_size;
  1717. /* Add size of frags 0 through 4 to create our initial sum */
  1718. sum += skb_frag_size(frag++);
  1719. sum += skb_frag_size(frag++);
  1720. sum += skb_frag_size(frag++);
  1721. sum += skb_frag_size(frag++);
  1722. sum += skb_frag_size(frag++);
  1723. /* Walk through fragments adding latest fragment, testing it, and
  1724. * then removing stale fragments from the sum.
  1725. */
  1726. stale = &skb_shinfo(skb)->frags[0];
  1727. for (;;) {
  1728. sum += skb_frag_size(frag++);
  1729. /* if sum is negative we failed to make sufficient progress */
  1730. if (sum < 0)
  1731. return true;
  1732. if (!nr_frags--)
  1733. break;
  1734. sum -= skb_frag_size(stale++);
  1735. }
  1736. return false;
  1737. }
  1738. /**
  1739. * __i40evf_maybe_stop_tx - 2nd level check for tx stop conditions
  1740. * @tx_ring: the ring to be checked
  1741. * @size: the size buffer we want to assure is available
  1742. *
  1743. * Returns -EBUSY if a stop is needed, else 0
  1744. **/
  1745. int __i40evf_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1746. {
  1747. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1748. /* Memory barrier before checking head and tail */
  1749. smp_mb();
  1750. /* Check again in a case another CPU has just made room available. */
  1751. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1752. return -EBUSY;
  1753. /* A reprieve! - use start_queue because it doesn't call schedule */
  1754. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1755. ++tx_ring->tx_stats.restart_queue;
  1756. return 0;
  1757. }
  1758. /**
  1759. * i40evf_tx_map - Build the Tx descriptor
  1760. * @tx_ring: ring to send buffer on
  1761. * @skb: send buffer
  1762. * @first: first buffer info buffer to use
  1763. * @tx_flags: collected send information
  1764. * @hdr_len: size of the packet header
  1765. * @td_cmd: the command field in the descriptor
  1766. * @td_offset: offset for checksum or crc
  1767. **/
  1768. static inline void i40evf_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1769. struct i40e_tx_buffer *first, u32 tx_flags,
  1770. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1771. {
  1772. unsigned int data_len = skb->data_len;
  1773. unsigned int size = skb_headlen(skb);
  1774. struct skb_frag_struct *frag;
  1775. struct i40e_tx_buffer *tx_bi;
  1776. struct i40e_tx_desc *tx_desc;
  1777. u16 i = tx_ring->next_to_use;
  1778. u32 td_tag = 0;
  1779. dma_addr_t dma;
  1780. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1781. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1782. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1783. I40E_TX_FLAGS_VLAN_SHIFT;
  1784. }
  1785. first->tx_flags = tx_flags;
  1786. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1787. tx_desc = I40E_TX_DESC(tx_ring, i);
  1788. tx_bi = first;
  1789. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1790. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  1791. if (dma_mapping_error(tx_ring->dev, dma))
  1792. goto dma_error;
  1793. /* record length, and DMA address */
  1794. dma_unmap_len_set(tx_bi, len, size);
  1795. dma_unmap_addr_set(tx_bi, dma, dma);
  1796. /* align size to end of page */
  1797. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  1798. tx_desc->buffer_addr = cpu_to_le64(dma);
  1799. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1800. tx_desc->cmd_type_offset_bsz =
  1801. build_ctob(td_cmd, td_offset,
  1802. max_data, td_tag);
  1803. tx_desc++;
  1804. i++;
  1805. if (i == tx_ring->count) {
  1806. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1807. i = 0;
  1808. }
  1809. dma += max_data;
  1810. size -= max_data;
  1811. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  1812. tx_desc->buffer_addr = cpu_to_le64(dma);
  1813. }
  1814. if (likely(!data_len))
  1815. break;
  1816. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1817. size, td_tag);
  1818. tx_desc++;
  1819. i++;
  1820. if (i == tx_ring->count) {
  1821. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1822. i = 0;
  1823. }
  1824. size = skb_frag_size(frag);
  1825. data_len -= size;
  1826. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1827. DMA_TO_DEVICE);
  1828. tx_bi = &tx_ring->tx_bi[i];
  1829. }
  1830. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  1831. i++;
  1832. if (i == tx_ring->count)
  1833. i = 0;
  1834. tx_ring->next_to_use = i;
  1835. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1836. /* write last descriptor with RS and EOP bits */
  1837. td_cmd |= I40E_TXD_CMD;
  1838. tx_desc->cmd_type_offset_bsz =
  1839. build_ctob(td_cmd, td_offset, size, td_tag);
  1840. /* Force memory writes to complete before letting h/w know there
  1841. * are new descriptors to fetch.
  1842. *
  1843. * We also use this memory barrier to make certain all of the
  1844. * status bits have been updated before next_to_watch is written.
  1845. */
  1846. wmb();
  1847. /* set next_to_watch value indicating a packet is present */
  1848. first->next_to_watch = tx_desc;
  1849. /* notify HW of packet */
  1850. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  1851. writel(i, tx_ring->tail);
  1852. /* we need this if more than one processor can write to our tail
  1853. * at a time, it synchronizes IO on IA64/Altix systems
  1854. */
  1855. mmiowb();
  1856. }
  1857. return;
  1858. dma_error:
  1859. dev_info(tx_ring->dev, "TX DMA map failed\n");
  1860. /* clear dma mappings for failed tx_bi map */
  1861. for (;;) {
  1862. tx_bi = &tx_ring->tx_bi[i];
  1863. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  1864. if (tx_bi == first)
  1865. break;
  1866. if (i == 0)
  1867. i = tx_ring->count;
  1868. i--;
  1869. }
  1870. tx_ring->next_to_use = i;
  1871. }
  1872. /**
  1873. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  1874. * @skb: send buffer
  1875. * @tx_ring: ring to send buffer on
  1876. *
  1877. * Returns NETDEV_TX_OK if sent, else an error code
  1878. **/
  1879. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  1880. struct i40e_ring *tx_ring)
  1881. {
  1882. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  1883. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  1884. struct i40e_tx_buffer *first;
  1885. u32 td_offset = 0;
  1886. u32 tx_flags = 0;
  1887. __be16 protocol;
  1888. u32 td_cmd = 0;
  1889. u8 hdr_len = 0;
  1890. int tso, count;
  1891. /* prefetch the data, we'll need it later */
  1892. prefetch(skb->data);
  1893. i40e_trace(xmit_frame_ring, skb, tx_ring);
  1894. count = i40e_xmit_descriptor_count(skb);
  1895. if (i40e_chk_linearize(skb, count)) {
  1896. if (__skb_linearize(skb)) {
  1897. dev_kfree_skb_any(skb);
  1898. return NETDEV_TX_OK;
  1899. }
  1900. count = i40e_txd_use_count(skb->len);
  1901. tx_ring->tx_stats.tx_linearize++;
  1902. }
  1903. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  1904. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  1905. * + 4 desc gap to avoid the cache line where head is,
  1906. * + 1 desc for context descriptor,
  1907. * otherwise try next time
  1908. */
  1909. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  1910. tx_ring->tx_stats.tx_busy++;
  1911. return NETDEV_TX_BUSY;
  1912. }
  1913. /* record the location of the first descriptor for this packet */
  1914. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  1915. first->skb = skb;
  1916. first->bytecount = skb->len;
  1917. first->gso_segs = 1;
  1918. /* prepare the xmit flags */
  1919. if (i40evf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  1920. goto out_drop;
  1921. /* obtain protocol of skb */
  1922. protocol = vlan_get_protocol(skb);
  1923. /* setup IPv4/IPv6 offloads */
  1924. if (protocol == htons(ETH_P_IP))
  1925. tx_flags |= I40E_TX_FLAGS_IPV4;
  1926. else if (protocol == htons(ETH_P_IPV6))
  1927. tx_flags |= I40E_TX_FLAGS_IPV6;
  1928. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  1929. if (tso < 0)
  1930. goto out_drop;
  1931. else if (tso)
  1932. tx_flags |= I40E_TX_FLAGS_TSO;
  1933. /* Always offload the checksum, since it's in the data descriptor */
  1934. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  1935. tx_ring, &cd_tunneling);
  1936. if (tso < 0)
  1937. goto out_drop;
  1938. skb_tx_timestamp(skb);
  1939. /* always enable CRC insertion offload */
  1940. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  1941. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  1942. cd_tunneling, cd_l2tag2);
  1943. i40evf_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  1944. td_cmd, td_offset);
  1945. return NETDEV_TX_OK;
  1946. out_drop:
  1947. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  1948. dev_kfree_skb_any(first->skb);
  1949. first->skb = NULL;
  1950. return NETDEV_TX_OK;
  1951. }
  1952. /**
  1953. * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  1954. * @skb: send buffer
  1955. * @netdev: network interface device structure
  1956. *
  1957. * Returns NETDEV_TX_OK if sent, else an error code
  1958. **/
  1959. netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1960. {
  1961. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1962. struct i40e_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping];
  1963. /* hardware can't handle really short frames, hardware padding works
  1964. * beyond this point
  1965. */
  1966. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  1967. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  1968. return NETDEV_TX_OK;
  1969. skb->len = I40E_MIN_TX_LEN;
  1970. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  1971. }
  1972. return i40e_xmit_frame_ring(skb, tx_ring);
  1973. }