i40e_txrx.c 50 KB

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