i40e_txrx.c 53 KB

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