i40e_txrx.c 64 KB

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