i40e_txrx.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  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. (decoded.inner_prot != I40E_RX_PTYPE_INNER_PROT_UDP) &&
  800. !(rx_status & (1 << I40E_RX_DESC_STATUS_UDP_0_SHIFT))) {
  801. skb->transport_header = skb->mac_header +
  802. sizeof(struct ethhdr) +
  803. (ip_hdr(skb)->ihl * 4);
  804. /* Add 4 bytes for VLAN tagged packets */
  805. skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
  806. skb->protocol == htons(ETH_P_8021AD))
  807. ? VLAN_HLEN : 0;
  808. rx_udp_csum = udp_csum(skb);
  809. iph = ip_hdr(skb);
  810. csum = csum_tcpudp_magic(
  811. iph->saddr, iph->daddr,
  812. (skb->len - skb_transport_offset(skb)),
  813. IPPROTO_UDP, rx_udp_csum);
  814. if (udp_hdr(skb)->check != csum)
  815. goto checksum_fail;
  816. }
  817. skb->ip_summed = CHECKSUM_UNNECESSARY;
  818. skb->csum_level = ipv4_tunnel || ipv6_tunnel;
  819. return;
  820. checksum_fail:
  821. vsi->back->hw_csum_rx_error++;
  822. }
  823. /**
  824. * i40e_rx_hash - returns the hash value from the Rx descriptor
  825. * @ring: descriptor ring
  826. * @rx_desc: specific descriptor
  827. **/
  828. static inline u32 i40e_rx_hash(struct i40e_ring *ring,
  829. union i40e_rx_desc *rx_desc)
  830. {
  831. const __le64 rss_mask =
  832. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  833. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  834. if ((ring->netdev->features & NETIF_F_RXHASH) &&
  835. (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
  836. return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  837. else
  838. return 0;
  839. }
  840. /**
  841. * i40e_ptype_to_hash - get a hash type
  842. * @ptype: the ptype value from the descriptor
  843. *
  844. * Returns a hash type to be used by skb_set_hash
  845. **/
  846. static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
  847. {
  848. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  849. if (!decoded.known)
  850. return PKT_HASH_TYPE_NONE;
  851. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  852. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  853. return PKT_HASH_TYPE_L4;
  854. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  855. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  856. return PKT_HASH_TYPE_L3;
  857. else
  858. return PKT_HASH_TYPE_L2;
  859. }
  860. /**
  861. * i40e_clean_rx_irq_ps - Reclaim resources after receive; packet split
  862. * @rx_ring: rx ring to clean
  863. * @budget: how many cleans we're allowed
  864. *
  865. * Returns true if there's any budget left (e.g. the clean is finished)
  866. **/
  867. static int i40e_clean_rx_irq_ps(struct i40e_ring *rx_ring, int budget)
  868. {
  869. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  870. u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
  871. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  872. const int current_node = numa_node_id();
  873. struct i40e_vsi *vsi = rx_ring->vsi;
  874. u16 i = rx_ring->next_to_clean;
  875. union i40e_rx_desc *rx_desc;
  876. u32 rx_error, rx_status;
  877. u8 rx_ptype;
  878. u64 qword;
  879. do {
  880. struct i40e_rx_buffer *rx_bi;
  881. struct sk_buff *skb;
  882. u16 vlan_tag;
  883. /* return some buffers to hardware, one at a time is too slow */
  884. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  885. i40evf_alloc_rx_buffers_ps(rx_ring, cleaned_count);
  886. cleaned_count = 0;
  887. }
  888. i = rx_ring->next_to_clean;
  889. rx_desc = I40E_RX_DESC(rx_ring, i);
  890. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  891. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  892. I40E_RXD_QW1_STATUS_SHIFT;
  893. if (!(rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)))
  894. break;
  895. /* This memory barrier is needed to keep us from reading
  896. * any other fields out of the rx_desc until we know the
  897. * DD bit is set.
  898. */
  899. rmb();
  900. rx_bi = &rx_ring->rx_bi[i];
  901. skb = rx_bi->skb;
  902. if (likely(!skb)) {
  903. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  904. rx_ring->rx_hdr_len);
  905. if (!skb)
  906. rx_ring->rx_stats.alloc_buff_failed++;
  907. /* initialize queue mapping */
  908. skb_record_rx_queue(skb, rx_ring->queue_index);
  909. /* we are reusing so sync this buffer for CPU use */
  910. dma_sync_single_range_for_cpu(rx_ring->dev,
  911. rx_bi->dma,
  912. 0,
  913. rx_ring->rx_hdr_len,
  914. DMA_FROM_DEVICE);
  915. }
  916. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  917. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  918. rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
  919. I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
  920. rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
  921. I40E_RXD_QW1_LENGTH_SPH_SHIFT;
  922. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  923. I40E_RXD_QW1_ERROR_SHIFT;
  924. rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  925. rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  926. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  927. I40E_RXD_QW1_PTYPE_SHIFT;
  928. prefetch(rx_bi->page);
  929. rx_bi->skb = NULL;
  930. cleaned_count++;
  931. if (rx_hbo || rx_sph) {
  932. int len;
  933. if (rx_hbo)
  934. len = I40E_RX_HDR_SIZE;
  935. else
  936. len = rx_header_len;
  937. memcpy(__skb_put(skb, len), rx_bi->hdr_buf, len);
  938. } else if (skb->len == 0) {
  939. int len;
  940. len = (rx_packet_len > skb_headlen(skb) ?
  941. skb_headlen(skb) : rx_packet_len);
  942. memcpy(__skb_put(skb, len),
  943. rx_bi->page + rx_bi->page_offset,
  944. len);
  945. rx_bi->page_offset += len;
  946. rx_packet_len -= len;
  947. }
  948. /* Get the rest of the data if this was a header split */
  949. if (rx_packet_len) {
  950. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  951. rx_bi->page,
  952. rx_bi->page_offset,
  953. rx_packet_len);
  954. skb->len += rx_packet_len;
  955. skb->data_len += rx_packet_len;
  956. skb->truesize += rx_packet_len;
  957. if ((page_count(rx_bi->page) == 1) &&
  958. (page_to_nid(rx_bi->page) == current_node))
  959. get_page(rx_bi->page);
  960. else
  961. rx_bi->page = NULL;
  962. dma_unmap_page(rx_ring->dev,
  963. rx_bi->page_dma,
  964. PAGE_SIZE / 2,
  965. DMA_FROM_DEVICE);
  966. rx_bi->page_dma = 0;
  967. }
  968. I40E_RX_INCREMENT(rx_ring, i);
  969. if (unlikely(
  970. !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  971. struct i40e_rx_buffer *next_buffer;
  972. next_buffer = &rx_ring->rx_bi[i];
  973. next_buffer->skb = skb;
  974. rx_ring->rx_stats.non_eop_descs++;
  975. continue;
  976. }
  977. /* ERR_MASK will only have valid bits if EOP set */
  978. if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  979. dev_kfree_skb_any(skb);
  980. /* TODO: shouldn't we increment a counter indicating the
  981. * drop?
  982. */
  983. continue;
  984. }
  985. skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
  986. i40e_ptype_to_hash(rx_ptype));
  987. /* probably a little skewed due to removing CRC */
  988. total_rx_bytes += skb->len;
  989. total_rx_packets++;
  990. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  991. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  992. vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  993. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  994. : 0;
  995. #ifdef I40E_FCOE
  996. if (!i40e_fcoe_handle_offload(rx_ring, rx_desc, skb)) {
  997. dev_kfree_skb_any(skb);
  998. continue;
  999. }
  1000. #endif
  1001. skb_mark_napi_id(skb, &rx_ring->q_vector->napi);
  1002. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1003. rx_ring->netdev->last_rx = jiffies;
  1004. rx_desc->wb.qword1.status_error_len = 0;
  1005. } while (likely(total_rx_packets < budget));
  1006. u64_stats_update_begin(&rx_ring->syncp);
  1007. rx_ring->stats.packets += total_rx_packets;
  1008. rx_ring->stats.bytes += total_rx_bytes;
  1009. u64_stats_update_end(&rx_ring->syncp);
  1010. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1011. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1012. return total_rx_packets;
  1013. }
  1014. /**
  1015. * i40e_clean_rx_irq_1buf - Reclaim resources after receive; single buffer
  1016. * @rx_ring: rx ring to clean
  1017. * @budget: how many cleans we're allowed
  1018. *
  1019. * Returns number of packets cleaned
  1020. **/
  1021. static int i40e_clean_rx_irq_1buf(struct i40e_ring *rx_ring, int budget)
  1022. {
  1023. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1024. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1025. struct i40e_vsi *vsi = rx_ring->vsi;
  1026. union i40e_rx_desc *rx_desc;
  1027. u32 rx_error, rx_status;
  1028. u16 rx_packet_len;
  1029. u8 rx_ptype;
  1030. u64 qword;
  1031. u16 i;
  1032. do {
  1033. struct i40e_rx_buffer *rx_bi;
  1034. struct sk_buff *skb;
  1035. u16 vlan_tag;
  1036. /* return some buffers to hardware, one at a time is too slow */
  1037. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1038. i40evf_alloc_rx_buffers_1buf(rx_ring, cleaned_count);
  1039. cleaned_count = 0;
  1040. }
  1041. i = rx_ring->next_to_clean;
  1042. rx_desc = I40E_RX_DESC(rx_ring, i);
  1043. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1044. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1045. I40E_RXD_QW1_STATUS_SHIFT;
  1046. if (!(rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)))
  1047. break;
  1048. /* This memory barrier is needed to keep us from reading
  1049. * any other fields out of the rx_desc until we know the
  1050. * DD bit is set.
  1051. */
  1052. rmb();
  1053. rx_bi = &rx_ring->rx_bi[i];
  1054. skb = rx_bi->skb;
  1055. prefetch(skb->data);
  1056. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1057. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1058. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1059. I40E_RXD_QW1_ERROR_SHIFT;
  1060. rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  1061. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1062. I40E_RXD_QW1_PTYPE_SHIFT;
  1063. rx_bi->skb = NULL;
  1064. cleaned_count++;
  1065. /* Get the header and possibly the whole packet
  1066. * If this is an skb from previous receive dma will be 0
  1067. */
  1068. skb_put(skb, rx_packet_len);
  1069. dma_unmap_single(rx_ring->dev, rx_bi->dma, rx_ring->rx_buf_len,
  1070. DMA_FROM_DEVICE);
  1071. rx_bi->dma = 0;
  1072. I40E_RX_INCREMENT(rx_ring, i);
  1073. if (unlikely(
  1074. !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  1075. rx_ring->rx_stats.non_eop_descs++;
  1076. continue;
  1077. }
  1078. /* ERR_MASK will only have valid bits if EOP set */
  1079. if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  1080. dev_kfree_skb_any(skb);
  1081. /* TODO: shouldn't we increment a counter indicating the
  1082. * drop?
  1083. */
  1084. continue;
  1085. }
  1086. skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
  1087. i40e_ptype_to_hash(rx_ptype));
  1088. /* probably a little skewed due to removing CRC */
  1089. total_rx_bytes += skb->len;
  1090. total_rx_packets++;
  1091. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1092. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  1093. vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  1094. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  1095. : 0;
  1096. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1097. rx_ring->netdev->last_rx = jiffies;
  1098. rx_desc->wb.qword1.status_error_len = 0;
  1099. } while (likely(total_rx_packets < budget));
  1100. u64_stats_update_begin(&rx_ring->syncp);
  1101. rx_ring->stats.packets += total_rx_packets;
  1102. rx_ring->stats.bytes += total_rx_bytes;
  1103. u64_stats_update_end(&rx_ring->syncp);
  1104. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1105. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1106. return total_rx_packets;
  1107. }
  1108. /**
  1109. * i40evf_napi_poll - NAPI polling Rx/Tx cleanup routine
  1110. * @napi: napi struct with our devices info in it
  1111. * @budget: amount of work driver is allowed to do this pass, in packets
  1112. *
  1113. * This function will clean all queues associated with a q_vector.
  1114. *
  1115. * Returns the amount of work done
  1116. **/
  1117. int i40evf_napi_poll(struct napi_struct *napi, int budget)
  1118. {
  1119. struct i40e_q_vector *q_vector =
  1120. container_of(napi, struct i40e_q_vector, napi);
  1121. struct i40e_vsi *vsi = q_vector->vsi;
  1122. struct i40e_ring *ring;
  1123. bool clean_complete = true;
  1124. bool arm_wb = false;
  1125. int budget_per_ring;
  1126. int cleaned;
  1127. if (test_bit(__I40E_DOWN, &vsi->state)) {
  1128. napi_complete(napi);
  1129. return 0;
  1130. }
  1131. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1132. * budget and be more aggressive about cleaning up the Tx descriptors.
  1133. */
  1134. i40e_for_each_ring(ring, q_vector->tx) {
  1135. clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
  1136. arm_wb |= ring->arm_wb;
  1137. }
  1138. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1139. * allow the budget to go below 1 because that would exit polling early.
  1140. */
  1141. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1142. i40e_for_each_ring(ring, q_vector->rx) {
  1143. if (ring_is_ps_enabled(ring))
  1144. cleaned = i40e_clean_rx_irq_ps(ring, budget_per_ring);
  1145. else
  1146. cleaned = i40e_clean_rx_irq_1buf(ring, budget_per_ring);
  1147. /* if we didn't clean as many as budgeted, we must be done */
  1148. clean_complete &= (budget_per_ring != cleaned);
  1149. }
  1150. /* If work not completed, return budget and polling will return */
  1151. if (!clean_complete) {
  1152. if (arm_wb)
  1153. i40e_force_wb(vsi, q_vector);
  1154. return budget;
  1155. }
  1156. /* Work is done so exit the polling mode and re-enable the interrupt */
  1157. napi_complete(napi);
  1158. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
  1159. ITR_IS_DYNAMIC(vsi->tx_itr_setting))
  1160. i40e_update_dynamic_itr(q_vector);
  1161. if (!test_bit(__I40E_DOWN, &vsi->state))
  1162. i40evf_irq_enable_queues(vsi->back, 1 << q_vector->v_idx);
  1163. return 0;
  1164. }
  1165. /**
  1166. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1167. * @skb: send buffer
  1168. * @tx_ring: ring to send buffer on
  1169. * @flags: the tx flags to be set
  1170. *
  1171. * Checks the skb and set up correspondingly several generic transmit flags
  1172. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1173. *
  1174. * Returns error code indicate the frame should be dropped upon error and the
  1175. * otherwise returns 0 to indicate the flags has been set properly.
  1176. **/
  1177. static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  1178. struct i40e_ring *tx_ring,
  1179. u32 *flags)
  1180. {
  1181. __be16 protocol = skb->protocol;
  1182. u32 tx_flags = 0;
  1183. /* if we have a HW VLAN tag being added, default to the HW one */
  1184. if (skb_vlan_tag_present(skb)) {
  1185. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1186. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1187. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1188. } else if (protocol == htons(ETH_P_8021Q)) {
  1189. struct vlan_hdr *vhdr, _vhdr;
  1190. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1191. if (!vhdr)
  1192. return -EINVAL;
  1193. protocol = vhdr->h_vlan_encapsulated_proto;
  1194. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1195. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1196. }
  1197. *flags = tx_flags;
  1198. return 0;
  1199. }
  1200. /**
  1201. * i40e_tso - set up the tso context descriptor
  1202. * @tx_ring: ptr to the ring to send
  1203. * @skb: ptr to the skb we're sending
  1204. * @tx_flags: the collected send information
  1205. * @protocol: the send protocol
  1206. * @hdr_len: ptr to the size of the packet header
  1207. * @cd_tunneling: ptr to context descriptor bits
  1208. *
  1209. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1210. **/
  1211. static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1212. u32 tx_flags, __be16 protocol, u8 *hdr_len,
  1213. u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
  1214. {
  1215. u32 cd_cmd, cd_tso_len, cd_mss;
  1216. struct ipv6hdr *ipv6h;
  1217. struct tcphdr *tcph;
  1218. struct iphdr *iph;
  1219. u32 l4len;
  1220. int err;
  1221. if (!skb_is_gso(skb))
  1222. return 0;
  1223. err = skb_cow_head(skb, 0);
  1224. if (err < 0)
  1225. return err;
  1226. iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
  1227. ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  1228. if (iph->version == 4) {
  1229. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1230. iph->tot_len = 0;
  1231. iph->check = 0;
  1232. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1233. 0, IPPROTO_TCP, 0);
  1234. } else if (ipv6h->version == 6) {
  1235. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1236. ipv6h->payload_len = 0;
  1237. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  1238. 0, IPPROTO_TCP, 0);
  1239. }
  1240. l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
  1241. *hdr_len = (skb->encapsulation
  1242. ? (skb_inner_transport_header(skb) - skb->data)
  1243. : skb_transport_offset(skb)) + l4len;
  1244. /* find the field values */
  1245. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1246. cd_tso_len = skb->len - *hdr_len;
  1247. cd_mss = skb_shinfo(skb)->gso_size;
  1248. *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1249. ((u64)cd_tso_len <<
  1250. I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1251. ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1252. return 1;
  1253. }
  1254. /**
  1255. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1256. * @skb: send buffer
  1257. * @tx_flags: Tx flags currently set
  1258. * @td_cmd: Tx descriptor command bits to set
  1259. * @td_offset: Tx descriptor header offsets to set
  1260. * @cd_tunneling: ptr to context desc bits
  1261. **/
  1262. static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
  1263. u32 *td_cmd, u32 *td_offset,
  1264. struct i40e_ring *tx_ring,
  1265. u32 *cd_tunneling)
  1266. {
  1267. struct ipv6hdr *this_ipv6_hdr;
  1268. unsigned int this_tcp_hdrlen;
  1269. struct iphdr *this_ip_hdr;
  1270. u32 network_hdr_len;
  1271. u8 l4_hdr = 0;
  1272. u32 l4_tunnel = 0;
  1273. if (skb->encapsulation) {
  1274. switch (ip_hdr(skb)->protocol) {
  1275. case IPPROTO_UDP:
  1276. l4_tunnel = I40E_TXD_CTX_UDP_TUNNELING;
  1277. break;
  1278. default:
  1279. return;
  1280. }
  1281. network_hdr_len = skb_inner_network_header_len(skb);
  1282. this_ip_hdr = inner_ip_hdr(skb);
  1283. this_ipv6_hdr = inner_ipv6_hdr(skb);
  1284. this_tcp_hdrlen = inner_tcp_hdrlen(skb);
  1285. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1286. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1287. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
  1288. ip_hdr(skb)->check = 0;
  1289. } else {
  1290. *cd_tunneling |=
  1291. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1292. }
  1293. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1294. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
  1295. if (tx_flags & I40E_TX_FLAGS_TSO)
  1296. ip_hdr(skb)->check = 0;
  1297. }
  1298. /* Now set the ctx descriptor fields */
  1299. *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
  1300. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
  1301. l4_tunnel |
  1302. ((skb_inner_network_offset(skb) -
  1303. skb_transport_offset(skb)) >> 1) <<
  1304. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1305. if (this_ip_hdr->version == 6) {
  1306. tx_flags &= ~I40E_TX_FLAGS_IPV4;
  1307. tx_flags |= I40E_TX_FLAGS_IPV6;
  1308. }
  1309. } else {
  1310. network_hdr_len = skb_network_header_len(skb);
  1311. this_ip_hdr = ip_hdr(skb);
  1312. this_ipv6_hdr = ipv6_hdr(skb);
  1313. this_tcp_hdrlen = tcp_hdrlen(skb);
  1314. }
  1315. /* Enable IP checksum offloads */
  1316. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1317. l4_hdr = this_ip_hdr->protocol;
  1318. /* the stack computes the IP header already, the only time we
  1319. * need the hardware to recompute it is in the case of TSO.
  1320. */
  1321. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1322. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
  1323. this_ip_hdr->check = 0;
  1324. } else {
  1325. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
  1326. }
  1327. /* Now set the td_offset for IP header length */
  1328. *td_offset = (network_hdr_len >> 2) <<
  1329. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1330. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1331. l4_hdr = this_ipv6_hdr->nexthdr;
  1332. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1333. /* Now set the td_offset for IP header length */
  1334. *td_offset = (network_hdr_len >> 2) <<
  1335. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1336. }
  1337. /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
  1338. *td_offset |= (skb_network_offset(skb) >> 1) <<
  1339. I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1340. /* Enable L4 checksum offloads */
  1341. switch (l4_hdr) {
  1342. case IPPROTO_TCP:
  1343. /* enable checksum offloads */
  1344. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1345. *td_offset |= (this_tcp_hdrlen >> 2) <<
  1346. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1347. break;
  1348. case IPPROTO_SCTP:
  1349. /* enable SCTP checksum offload */
  1350. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1351. *td_offset |= (sizeof(struct sctphdr) >> 2) <<
  1352. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1353. break;
  1354. case IPPROTO_UDP:
  1355. /* enable UDP checksum offload */
  1356. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1357. *td_offset |= (sizeof(struct udphdr) >> 2) <<
  1358. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1359. break;
  1360. default:
  1361. break;
  1362. }
  1363. }
  1364. /**
  1365. * i40e_create_tx_ctx Build the Tx context descriptor
  1366. * @tx_ring: ring to create the descriptor on
  1367. * @cd_type_cmd_tso_mss: Quad Word 1
  1368. * @cd_tunneling: Quad Word 0 - bits 0-31
  1369. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1370. **/
  1371. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1372. const u64 cd_type_cmd_tso_mss,
  1373. const u32 cd_tunneling, const u32 cd_l2tag2)
  1374. {
  1375. struct i40e_tx_context_desc *context_desc;
  1376. int i = tx_ring->next_to_use;
  1377. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1378. !cd_tunneling && !cd_l2tag2)
  1379. return;
  1380. /* grab the next descriptor */
  1381. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1382. i++;
  1383. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1384. /* cpu_to_le32 and assign to struct fields */
  1385. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1386. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1387. context_desc->rsvd = cpu_to_le16(0);
  1388. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1389. }
  1390. /**
  1391. * i40e_chk_linearize - Check if there are more than 8 fragments per packet
  1392. * @skb: send buffer
  1393. * @tx_flags: collected send information
  1394. * @hdr_len: size of the packet header
  1395. *
  1396. * Note: Our HW can't scatter-gather more than 8 fragments to build
  1397. * a packet on the wire and so we need to figure out the cases where we
  1398. * need to linearize the skb.
  1399. **/
  1400. static bool i40e_chk_linearize(struct sk_buff *skb, u32 tx_flags,
  1401. const u8 hdr_len)
  1402. {
  1403. struct skb_frag_struct *frag;
  1404. bool linearize = false;
  1405. unsigned int size = 0;
  1406. u16 num_frags;
  1407. u16 gso_segs;
  1408. num_frags = skb_shinfo(skb)->nr_frags;
  1409. gso_segs = skb_shinfo(skb)->gso_segs;
  1410. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO)) {
  1411. u16 j = 1;
  1412. if (num_frags < (I40E_MAX_BUFFER_TXD))
  1413. goto linearize_chk_done;
  1414. /* try the simple math, if we have too many frags per segment */
  1415. if (DIV_ROUND_UP((num_frags + gso_segs), gso_segs) >
  1416. I40E_MAX_BUFFER_TXD) {
  1417. linearize = true;
  1418. goto linearize_chk_done;
  1419. }
  1420. frag = &skb_shinfo(skb)->frags[0];
  1421. size = hdr_len;
  1422. /* we might still have more fragments per segment */
  1423. do {
  1424. size += skb_frag_size(frag);
  1425. frag++; j++;
  1426. if (j == I40E_MAX_BUFFER_TXD) {
  1427. if (size < skb_shinfo(skb)->gso_size) {
  1428. linearize = true;
  1429. break;
  1430. }
  1431. j = 1;
  1432. size -= skb_shinfo(skb)->gso_size;
  1433. if (size)
  1434. j++;
  1435. size += hdr_len;
  1436. }
  1437. num_frags--;
  1438. } while (num_frags);
  1439. } else {
  1440. if (num_frags >= I40E_MAX_BUFFER_TXD)
  1441. linearize = true;
  1442. }
  1443. linearize_chk_done:
  1444. return linearize;
  1445. }
  1446. /**
  1447. * i40e_tx_map - Build the Tx descriptor
  1448. * @tx_ring: ring to send buffer on
  1449. * @skb: send buffer
  1450. * @first: first buffer info buffer to use
  1451. * @tx_flags: collected send information
  1452. * @hdr_len: size of the packet header
  1453. * @td_cmd: the command field in the descriptor
  1454. * @td_offset: offset for checksum or crc
  1455. **/
  1456. static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1457. struct i40e_tx_buffer *first, u32 tx_flags,
  1458. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1459. {
  1460. unsigned int data_len = skb->data_len;
  1461. unsigned int size = skb_headlen(skb);
  1462. struct skb_frag_struct *frag;
  1463. struct i40e_tx_buffer *tx_bi;
  1464. struct i40e_tx_desc *tx_desc;
  1465. u16 i = tx_ring->next_to_use;
  1466. u32 td_tag = 0;
  1467. dma_addr_t dma;
  1468. u16 gso_segs;
  1469. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1470. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1471. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1472. I40E_TX_FLAGS_VLAN_SHIFT;
  1473. }
  1474. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
  1475. gso_segs = skb_shinfo(skb)->gso_segs;
  1476. else
  1477. gso_segs = 1;
  1478. /* multiply data chunks by size of headers */
  1479. first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
  1480. first->gso_segs = gso_segs;
  1481. first->skb = skb;
  1482. first->tx_flags = tx_flags;
  1483. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1484. tx_desc = I40E_TX_DESC(tx_ring, i);
  1485. tx_bi = first;
  1486. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1487. if (dma_mapping_error(tx_ring->dev, dma))
  1488. goto dma_error;
  1489. /* record length, and DMA address */
  1490. dma_unmap_len_set(tx_bi, len, size);
  1491. dma_unmap_addr_set(tx_bi, dma, dma);
  1492. tx_desc->buffer_addr = cpu_to_le64(dma);
  1493. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1494. tx_desc->cmd_type_offset_bsz =
  1495. build_ctob(td_cmd, td_offset,
  1496. I40E_MAX_DATA_PER_TXD, td_tag);
  1497. tx_desc++;
  1498. i++;
  1499. if (i == tx_ring->count) {
  1500. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1501. i = 0;
  1502. }
  1503. dma += I40E_MAX_DATA_PER_TXD;
  1504. size -= I40E_MAX_DATA_PER_TXD;
  1505. tx_desc->buffer_addr = cpu_to_le64(dma);
  1506. }
  1507. if (likely(!data_len))
  1508. break;
  1509. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1510. size, td_tag);
  1511. tx_desc++;
  1512. i++;
  1513. if (i == tx_ring->count) {
  1514. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1515. i = 0;
  1516. }
  1517. size = skb_frag_size(frag);
  1518. data_len -= size;
  1519. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  1520. DMA_TO_DEVICE);
  1521. tx_bi = &tx_ring->tx_bi[i];
  1522. }
  1523. /* Place RS bit on last descriptor of any packet that spans across the
  1524. * 4th descriptor (WB_STRIDE aka 0x3) in a 64B cacheline.
  1525. */
  1526. #define WB_STRIDE 0x3
  1527. if (((i & WB_STRIDE) != WB_STRIDE) &&
  1528. (first <= &tx_ring->tx_bi[i]) &&
  1529. (first >= &tx_ring->tx_bi[i & ~WB_STRIDE])) {
  1530. tx_desc->cmd_type_offset_bsz =
  1531. build_ctob(td_cmd, td_offset, size, td_tag) |
  1532. cpu_to_le64((u64)I40E_TX_DESC_CMD_EOP <<
  1533. I40E_TXD_QW1_CMD_SHIFT);
  1534. } else {
  1535. tx_desc->cmd_type_offset_bsz =
  1536. build_ctob(td_cmd, td_offset, size, td_tag) |
  1537. cpu_to_le64((u64)I40E_TXD_CMD <<
  1538. I40E_TXD_QW1_CMD_SHIFT);
  1539. }
  1540. netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
  1541. tx_ring->queue_index),
  1542. first->bytecount);
  1543. /* set the timestamp */
  1544. first->time_stamp = jiffies;
  1545. /* Force memory writes to complete before letting h/w
  1546. * know there are new descriptors to fetch. (Only
  1547. * applicable for weak-ordered memory model archs,
  1548. * such as IA-64).
  1549. */
  1550. wmb();
  1551. /* set next_to_watch value indicating a packet is present */
  1552. first->next_to_watch = tx_desc;
  1553. i++;
  1554. if (i == tx_ring->count)
  1555. i = 0;
  1556. tx_ring->next_to_use = i;
  1557. /* notify HW of packet */
  1558. writel(i, tx_ring->tail);
  1559. return;
  1560. dma_error:
  1561. dev_info(tx_ring->dev, "TX DMA map failed\n");
  1562. /* clear dma mappings for failed tx_bi map */
  1563. for (;;) {
  1564. tx_bi = &tx_ring->tx_bi[i];
  1565. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  1566. if (tx_bi == first)
  1567. break;
  1568. if (i == 0)
  1569. i = tx_ring->count;
  1570. i--;
  1571. }
  1572. tx_ring->next_to_use = i;
  1573. }
  1574. /**
  1575. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  1576. * @tx_ring: the ring to be checked
  1577. * @size: the size buffer we want to assure is available
  1578. *
  1579. * Returns -EBUSY if a stop is needed, else 0
  1580. **/
  1581. static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1582. {
  1583. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1584. /* Memory barrier before checking head and tail */
  1585. smp_mb();
  1586. /* Check again in a case another CPU has just made room available. */
  1587. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1588. return -EBUSY;
  1589. /* A reprieve! - use start_queue because it doesn't call schedule */
  1590. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1591. ++tx_ring->tx_stats.restart_queue;
  1592. return 0;
  1593. }
  1594. /**
  1595. * i40e_maybe_stop_tx - 1st level check for tx stop conditions
  1596. * @tx_ring: the ring to be checked
  1597. * @size: the size buffer we want to assure is available
  1598. *
  1599. * Returns 0 if stop is not needed
  1600. **/
  1601. static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1602. {
  1603. if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
  1604. return 0;
  1605. return __i40e_maybe_stop_tx(tx_ring, size);
  1606. }
  1607. /**
  1608. * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
  1609. * @skb: send buffer
  1610. * @tx_ring: ring to send buffer on
  1611. *
  1612. * Returns number of data descriptors needed for this skb. Returns 0 to indicate
  1613. * there is not enough descriptors available in this ring since we need at least
  1614. * one descriptor.
  1615. **/
  1616. static int i40e_xmit_descriptor_count(struct sk_buff *skb,
  1617. struct i40e_ring *tx_ring)
  1618. {
  1619. unsigned int f;
  1620. int count = 0;
  1621. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  1622. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  1623. * + 4 desc gap to avoid the cache line where head is,
  1624. * + 1 desc for context descriptor,
  1625. * otherwise try next time
  1626. */
  1627. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  1628. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  1629. count += TXD_USE_COUNT(skb_headlen(skb));
  1630. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  1631. tx_ring->tx_stats.tx_busy++;
  1632. return 0;
  1633. }
  1634. return count;
  1635. }
  1636. /**
  1637. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  1638. * @skb: send buffer
  1639. * @tx_ring: ring to send buffer on
  1640. *
  1641. * Returns NETDEV_TX_OK if sent, else an error code
  1642. **/
  1643. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  1644. struct i40e_ring *tx_ring)
  1645. {
  1646. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  1647. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  1648. struct i40e_tx_buffer *first;
  1649. u32 td_offset = 0;
  1650. u32 tx_flags = 0;
  1651. __be16 protocol;
  1652. u32 td_cmd = 0;
  1653. u8 hdr_len = 0;
  1654. int tso;
  1655. if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
  1656. return NETDEV_TX_BUSY;
  1657. /* prepare the xmit flags */
  1658. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  1659. goto out_drop;
  1660. /* obtain protocol of skb */
  1661. protocol = vlan_get_protocol(skb);
  1662. /* record the location of the first descriptor for this packet */
  1663. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  1664. /* setup IPv4/IPv6 offloads */
  1665. if (protocol == htons(ETH_P_IP))
  1666. tx_flags |= I40E_TX_FLAGS_IPV4;
  1667. else if (protocol == htons(ETH_P_IPV6))
  1668. tx_flags |= I40E_TX_FLAGS_IPV6;
  1669. tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
  1670. &cd_type_cmd_tso_mss, &cd_tunneling);
  1671. if (tso < 0)
  1672. goto out_drop;
  1673. else if (tso)
  1674. tx_flags |= I40E_TX_FLAGS_TSO;
  1675. if (i40e_chk_linearize(skb, tx_flags, hdr_len))
  1676. if (skb_linearize(skb))
  1677. goto out_drop;
  1678. skb_tx_timestamp(skb);
  1679. /* always enable CRC insertion offload */
  1680. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  1681. /* Always offload the checksum, since it's in the data descriptor */
  1682. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1683. tx_flags |= I40E_TX_FLAGS_CSUM;
  1684. i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
  1685. tx_ring, &cd_tunneling);
  1686. }
  1687. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  1688. cd_tunneling, cd_l2tag2);
  1689. i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  1690. td_cmd, td_offset);
  1691. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  1692. return NETDEV_TX_OK;
  1693. out_drop:
  1694. dev_kfree_skb_any(skb);
  1695. return NETDEV_TX_OK;
  1696. }
  1697. /**
  1698. * i40evf_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  1699. * @skb: send buffer
  1700. * @netdev: network interface device structure
  1701. *
  1702. * Returns NETDEV_TX_OK if sent, else an error code
  1703. **/
  1704. netdev_tx_t i40evf_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1705. {
  1706. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1707. struct i40e_ring *tx_ring = adapter->tx_rings[skb->queue_mapping];
  1708. /* hardware can't handle really short frames, hardware padding works
  1709. * beyond this point
  1710. */
  1711. if (unlikely(skb->len < I40E_MIN_TX_LEN)) {
  1712. if (skb_pad(skb, I40E_MIN_TX_LEN - skb->len))
  1713. return NETDEV_TX_OK;
  1714. skb->len = I40E_MIN_TX_LEN;
  1715. skb_set_tail_pointer(skb, I40E_MIN_TX_LEN);
  1716. }
  1717. return i40e_xmit_frame_ring(skb, tx_ring);
  1718. }