i40e_txrx.c 55 KB

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