i40e_txrx.c 51 KB

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