i40e_txrx.c 52 KB

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