i40e_txrx.c 50 KB

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