i40e_txrx.c 50 KB

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