i40e_txrx.c 50 KB

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