i40e_txrx.c 51 KB

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