i40e_txrx.c 51 KB

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