i40e_txrx.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/prefetch.h>
  27. #include "i40e.h"
  28. #include "i40e_prototype.h"
  29. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  30. u32 td_tag)
  31. {
  32. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  33. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  34. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  35. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  36. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  37. }
  38. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  39. #define I40E_FD_CLEAN_DELAY 10
  40. /**
  41. * i40e_program_fdir_filter - Program a Flow Director filter
  42. * @fdir_data: Packet data that will be filter parameters
  43. * @raw_packet: the pre-allocated packet buffer for FDir
  44. * @pf: The pf pointer
  45. * @add: True for add/update, False for remove
  46. **/
  47. int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data, u8 *raw_packet,
  48. struct i40e_pf *pf, bool add)
  49. {
  50. struct i40e_filter_program_desc *fdir_desc;
  51. struct i40e_tx_buffer *tx_buf, *first;
  52. struct i40e_tx_desc *tx_desc;
  53. struct i40e_ring *tx_ring;
  54. unsigned int fpt, dcc;
  55. struct i40e_vsi *vsi;
  56. struct device *dev;
  57. dma_addr_t dma;
  58. u32 td_cmd = 0;
  59. u16 delay = 0;
  60. u16 i;
  61. /* find existing FDIR VSI */
  62. vsi = NULL;
  63. for (i = 0; i < pf->num_alloc_vsi; i++)
  64. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
  65. vsi = pf->vsi[i];
  66. if (!vsi)
  67. return -ENOENT;
  68. tx_ring = vsi->tx_rings[0];
  69. dev = tx_ring->dev;
  70. /* we need two descriptors to add/del a filter and we can wait */
  71. do {
  72. if (I40E_DESC_UNUSED(tx_ring) > 1)
  73. break;
  74. msleep_interruptible(1);
  75. delay++;
  76. } while (delay < I40E_FD_CLEAN_DELAY);
  77. if (!(I40E_DESC_UNUSED(tx_ring) > 1))
  78. return -EAGAIN;
  79. dma = dma_map_single(dev, raw_packet,
  80. I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
  81. if (dma_mapping_error(dev, dma))
  82. goto dma_fail;
  83. /* grab the next descriptor */
  84. i = tx_ring->next_to_use;
  85. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  86. first = &tx_ring->tx_bi[i];
  87. memset(first, 0, sizeof(struct i40e_tx_buffer));
  88. tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
  89. fpt = (fdir_data->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  90. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  91. fpt |= (fdir_data->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT) &
  92. I40E_TXD_FLTR_QW0_FLEXOFF_MASK;
  93. fpt |= (fdir_data->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) &
  94. I40E_TXD_FLTR_QW0_PCTYPE_MASK;
  95. /* Use LAN VSI Id if not programmed by user */
  96. if (fdir_data->dest_vsi == 0)
  97. fpt |= (pf->vsi[pf->lan_vsi]->id) <<
  98. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  99. else
  100. fpt |= ((u32)fdir_data->dest_vsi <<
  101. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT) &
  102. I40E_TXD_FLTR_QW0_DEST_VSI_MASK;
  103. dcc = I40E_TX_DESC_DTYPE_FILTER_PROG;
  104. if (add)
  105. dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  106. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  107. else
  108. dcc |= I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  109. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  110. dcc |= (fdir_data->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT) &
  111. I40E_TXD_FLTR_QW1_DEST_MASK;
  112. dcc |= (fdir_data->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT) &
  113. I40E_TXD_FLTR_QW1_FD_STATUS_MASK;
  114. if (fdir_data->cnt_index != 0) {
  115. dcc |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  116. dcc |= ((u32)fdir_data->cnt_index <<
  117. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  118. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  119. }
  120. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(fpt);
  121. fdir_desc->rsvd = cpu_to_le32(0);
  122. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dcc);
  123. fdir_desc->fd_id = cpu_to_le32(fdir_data->fd_id);
  124. /* Now program a dummy descriptor */
  125. i = tx_ring->next_to_use;
  126. tx_desc = I40E_TX_DESC(tx_ring, i);
  127. tx_buf = &tx_ring->tx_bi[i];
  128. tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
  129. memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
  130. /* record length, and DMA address */
  131. dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
  132. dma_unmap_addr_set(tx_buf, dma, dma);
  133. tx_desc->buffer_addr = cpu_to_le64(dma);
  134. td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
  135. tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
  136. tx_buf->raw_buf = (void *)raw_packet;
  137. tx_desc->cmd_type_offset_bsz =
  138. build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
  139. /* set the timestamp */
  140. tx_buf->time_stamp = jiffies;
  141. /* Force memory writes to complete before letting h/w
  142. * know there are new descriptors to fetch.
  143. */
  144. wmb();
  145. /* Mark the data descriptor to be watched */
  146. first->next_to_watch = tx_desc;
  147. writel(tx_ring->next_to_use, tx_ring->tail);
  148. return 0;
  149. dma_fail:
  150. return -1;
  151. }
  152. #define IP_HEADER_OFFSET 14
  153. #define I40E_UDPIP_DUMMY_PACKET_LEN 42
  154. /**
  155. * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
  156. * @vsi: pointer to the targeted VSI
  157. * @fd_data: the flow director data required for the FDir descriptor
  158. * @add: true adds a filter, false removes it
  159. *
  160. * Returns 0 if the filters were successfully added or removed
  161. **/
  162. static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
  163. struct i40e_fdir_filter *fd_data,
  164. bool add)
  165. {
  166. struct i40e_pf *pf = vsi->back;
  167. struct udphdr *udp;
  168. struct iphdr *ip;
  169. bool err = false;
  170. u8 *raw_packet;
  171. int ret;
  172. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  173. 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
  174. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  175. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  176. if (!raw_packet)
  177. return -ENOMEM;
  178. memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
  179. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  180. udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
  181. + sizeof(struct iphdr));
  182. ip->daddr = fd_data->dst_ip[0];
  183. udp->dest = fd_data->dst_port;
  184. ip->saddr = fd_data->src_ip[0];
  185. udp->source = fd_data->src_port;
  186. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  187. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  188. if (ret) {
  189. dev_info(&pf->pdev->dev,
  190. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  191. fd_data->pctype, fd_data->fd_id, ret);
  192. err = true;
  193. } else {
  194. if (add)
  195. dev_info(&pf->pdev->dev,
  196. "Filter OK for PCTYPE %d loc = %d\n",
  197. fd_data->pctype, fd_data->fd_id);
  198. else
  199. dev_info(&pf->pdev->dev,
  200. "Filter deleted for PCTYPE %d loc = %d\n",
  201. fd_data->pctype, fd_data->fd_id);
  202. }
  203. return err ? -EOPNOTSUPP : 0;
  204. }
  205. #define I40E_TCPIP_DUMMY_PACKET_LEN 54
  206. /**
  207. * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
  208. * @vsi: pointer to the targeted VSI
  209. * @fd_data: the flow director data required for the FDir descriptor
  210. * @add: true adds a filter, false removes it
  211. *
  212. * Returns 0 if the filters were successfully added or removed
  213. **/
  214. static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
  215. struct i40e_fdir_filter *fd_data,
  216. bool add)
  217. {
  218. struct i40e_pf *pf = vsi->back;
  219. struct tcphdr *tcp;
  220. struct iphdr *ip;
  221. bool err = false;
  222. u8 *raw_packet;
  223. int ret;
  224. /* Dummy packet */
  225. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  226. 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
  227. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
  228. 0x0, 0x72, 0, 0, 0, 0};
  229. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  230. if (!raw_packet)
  231. return -ENOMEM;
  232. memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
  233. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  234. tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
  235. + sizeof(struct iphdr));
  236. ip->daddr = fd_data->dst_ip[0];
  237. tcp->dest = fd_data->dst_port;
  238. ip->saddr = fd_data->src_ip[0];
  239. tcp->source = fd_data->src_port;
  240. if (add) {
  241. pf->fd_tcp_rule++;
  242. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED) {
  243. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
  244. pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  245. }
  246. } else {
  247. pf->fd_tcp_rule = (pf->fd_tcp_rule > 0) ?
  248. (pf->fd_tcp_rule - 1) : 0;
  249. if (pf->fd_tcp_rule == 0) {
  250. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  251. dev_info(&pf->pdev->dev, "ATR re-enabled due to no sideband TCP/IPv4 rules\n");
  252. }
  253. }
  254. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  255. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  256. if (ret) {
  257. dev_info(&pf->pdev->dev,
  258. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  259. fd_data->pctype, fd_data->fd_id, ret);
  260. err = true;
  261. } else {
  262. if (add)
  263. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d loc = %d)\n",
  264. fd_data->pctype, fd_data->fd_id);
  265. else
  266. dev_info(&pf->pdev->dev,
  267. "Filter deleted for PCTYPE %d loc = %d\n",
  268. fd_data->pctype, fd_data->fd_id);
  269. }
  270. return err ? -EOPNOTSUPP : 0;
  271. }
  272. /**
  273. * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
  274. * a specific flow spec
  275. * @vsi: pointer to the targeted VSI
  276. * @fd_data: the flow director data required for the FDir descriptor
  277. * @add: true adds a filter, false removes it
  278. *
  279. * Always returns -EOPNOTSUPP
  280. **/
  281. static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
  282. struct i40e_fdir_filter *fd_data,
  283. bool add)
  284. {
  285. return -EOPNOTSUPP;
  286. }
  287. #define I40E_IP_DUMMY_PACKET_LEN 34
  288. /**
  289. * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
  290. * a specific flow spec
  291. * @vsi: pointer to the targeted VSI
  292. * @fd_data: the flow director data required for the FDir descriptor
  293. * @add: true adds a filter, false removes it
  294. *
  295. * Returns 0 if the filters were successfully added or removed
  296. **/
  297. static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
  298. struct i40e_fdir_filter *fd_data,
  299. bool add)
  300. {
  301. struct i40e_pf *pf = vsi->back;
  302. struct iphdr *ip;
  303. bool err = false;
  304. u8 *raw_packet;
  305. int ret;
  306. int i;
  307. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  308. 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
  309. 0, 0, 0, 0};
  310. for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  311. i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
  312. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  313. if (!raw_packet)
  314. return -ENOMEM;
  315. memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
  316. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  317. ip->saddr = fd_data->src_ip[0];
  318. ip->daddr = fd_data->dst_ip[0];
  319. ip->protocol = 0;
  320. fd_data->pctype = i;
  321. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  322. if (ret) {
  323. dev_info(&pf->pdev->dev,
  324. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  325. fd_data->pctype, fd_data->fd_id, ret);
  326. err = true;
  327. } else {
  328. if (add)
  329. dev_info(&pf->pdev->dev,
  330. "Filter OK for PCTYPE %d loc = %d\n",
  331. fd_data->pctype, fd_data->fd_id);
  332. else
  333. dev_info(&pf->pdev->dev,
  334. "Filter deleted for PCTYPE %d loc = %d\n",
  335. fd_data->pctype, fd_data->fd_id);
  336. }
  337. }
  338. return err ? -EOPNOTSUPP : 0;
  339. }
  340. /**
  341. * i40e_add_del_fdir - Build raw packets to add/del fdir filter
  342. * @vsi: pointer to the targeted VSI
  343. * @cmd: command to get or set RX flow classification rules
  344. * @add: true adds a filter, false removes it
  345. *
  346. **/
  347. int i40e_add_del_fdir(struct i40e_vsi *vsi,
  348. struct i40e_fdir_filter *input, bool add)
  349. {
  350. struct i40e_pf *pf = vsi->back;
  351. int ret;
  352. switch (input->flow_type & ~FLOW_EXT) {
  353. case TCP_V4_FLOW:
  354. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  355. break;
  356. case UDP_V4_FLOW:
  357. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  358. break;
  359. case SCTP_V4_FLOW:
  360. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  361. break;
  362. case IPV4_FLOW:
  363. ret = i40e_add_del_fdir_ipv4(vsi, input, add);
  364. break;
  365. case IP_USER_FLOW:
  366. switch (input->ip4_proto) {
  367. case IPPROTO_TCP:
  368. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  369. break;
  370. case IPPROTO_UDP:
  371. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  372. break;
  373. case IPPROTO_SCTP:
  374. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  375. break;
  376. default:
  377. ret = i40e_add_del_fdir_ipv4(vsi, input, add);
  378. break;
  379. }
  380. break;
  381. default:
  382. dev_info(&pf->pdev->dev, "Could not specify spec type %d\n",
  383. input->flow_type);
  384. ret = -EINVAL;
  385. }
  386. /* The buffer allocated here is freed by the i40e_clean_tx_ring() */
  387. return ret;
  388. }
  389. /**
  390. * i40e_fd_handle_status - check the Programming Status for FD
  391. * @rx_ring: the Rx ring for this descriptor
  392. * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
  393. * @prog_id: the id originally used for programming
  394. *
  395. * This is used to verify if the FD programming or invalidation
  396. * requested by SW to the HW is successful or not and take actions accordingly.
  397. **/
  398. static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
  399. union i40e_rx_desc *rx_desc, u8 prog_id)
  400. {
  401. struct i40e_pf *pf = rx_ring->vsi->back;
  402. struct pci_dev *pdev = pf->pdev;
  403. u32 fcnt_prog, fcnt_avail;
  404. u32 error;
  405. u64 qw;
  406. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  407. error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
  408. I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
  409. if (error == (0x1 << I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
  410. if ((rx_desc->wb.qword0.hi_dword.fd_id != 0) ||
  411. (I40E_DEBUG_FD & pf->hw.debug_mask))
  412. dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
  413. rx_desc->wb.qword0.hi_dword.fd_id);
  414. pf->fd_add_err++;
  415. /* store the current atr filter count */
  416. pf->fd_atr_cnt = i40e_get_current_atr_cnt(pf);
  417. /* filter programming failed most likely due to table full */
  418. fcnt_prog = i40e_get_cur_guaranteed_fd_count(pf);
  419. fcnt_avail = pf->fdir_pf_filter_count;
  420. /* If ATR is running fcnt_prog can quickly change,
  421. * if we are very close to full, it makes sense to disable
  422. * FD ATR/SB and then re-enable it when there is room.
  423. */
  424. if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
  425. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  426. !(pf->auto_disable_flags &
  427. I40E_FLAG_FD_SB_ENABLED)) {
  428. dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
  429. pf->auto_disable_flags |=
  430. I40E_FLAG_FD_SB_ENABLED;
  431. }
  432. } else {
  433. dev_info(&pdev->dev,
  434. "FD filter programming failed due to incorrect filter parameters\n");
  435. }
  436. } else if (error ==
  437. (0x1 << I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
  438. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  439. dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
  440. rx_desc->wb.qword0.hi_dword.fd_id);
  441. }
  442. }
  443. /**
  444. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  445. * @ring: the ring that owns the buffer
  446. * @tx_buffer: the buffer to free
  447. **/
  448. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  449. struct i40e_tx_buffer *tx_buffer)
  450. {
  451. if (tx_buffer->skb) {
  452. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  453. kfree(tx_buffer->raw_buf);
  454. else
  455. dev_kfree_skb_any(tx_buffer->skb);
  456. if (dma_unmap_len(tx_buffer, len))
  457. dma_unmap_single(ring->dev,
  458. dma_unmap_addr(tx_buffer, dma),
  459. dma_unmap_len(tx_buffer, len),
  460. DMA_TO_DEVICE);
  461. } else if (dma_unmap_len(tx_buffer, len)) {
  462. dma_unmap_page(ring->dev,
  463. dma_unmap_addr(tx_buffer, dma),
  464. dma_unmap_len(tx_buffer, len),
  465. DMA_TO_DEVICE);
  466. }
  467. tx_buffer->next_to_watch = NULL;
  468. tx_buffer->skb = NULL;
  469. dma_unmap_len_set(tx_buffer, len, 0);
  470. /* tx_buffer must be completely set up in the transmit path */
  471. }
  472. /**
  473. * i40e_clean_tx_ring - Free any empty Tx buffers
  474. * @tx_ring: ring to be cleaned
  475. **/
  476. void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
  477. {
  478. unsigned long bi_size;
  479. u16 i;
  480. /* ring already cleared, nothing to do */
  481. if (!tx_ring->tx_bi)
  482. return;
  483. /* Free all the Tx ring sk_buffs */
  484. for (i = 0; i < tx_ring->count; i++)
  485. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  486. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  487. memset(tx_ring->tx_bi, 0, bi_size);
  488. /* Zero out the descriptor ring */
  489. memset(tx_ring->desc, 0, tx_ring->size);
  490. tx_ring->next_to_use = 0;
  491. tx_ring->next_to_clean = 0;
  492. if (!tx_ring->netdev)
  493. return;
  494. /* cleanup Tx queue statistics */
  495. netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
  496. tx_ring->queue_index));
  497. }
  498. /**
  499. * i40e_free_tx_resources - Free Tx resources per queue
  500. * @tx_ring: Tx descriptor ring for a specific queue
  501. *
  502. * Free all transmit software resources
  503. **/
  504. void i40e_free_tx_resources(struct i40e_ring *tx_ring)
  505. {
  506. i40e_clean_tx_ring(tx_ring);
  507. kfree(tx_ring->tx_bi);
  508. tx_ring->tx_bi = NULL;
  509. if (tx_ring->desc) {
  510. dma_free_coherent(tx_ring->dev, tx_ring->size,
  511. tx_ring->desc, tx_ring->dma);
  512. tx_ring->desc = NULL;
  513. }
  514. }
  515. /**
  516. * i40e_get_head - Retrieve head from head writeback
  517. * @tx_ring: tx ring to fetch head of
  518. *
  519. * Returns value of Tx ring head based on value stored
  520. * in head write-back location
  521. **/
  522. static inline u32 i40e_get_head(struct i40e_ring *tx_ring)
  523. {
  524. void *head = (struct i40e_tx_desc *)tx_ring->desc + tx_ring->count;
  525. return le32_to_cpu(*(volatile __le32 *)head);
  526. }
  527. /**
  528. * i40e_get_tx_pending - how many tx descriptors not processed
  529. * @tx_ring: the ring of descriptors
  530. *
  531. * Since there is no access to the ring head register
  532. * in XL710, we need to use our local copies
  533. **/
  534. static u32 i40e_get_tx_pending(struct i40e_ring *ring)
  535. {
  536. u32 head, tail;
  537. head = i40e_get_head(ring);
  538. tail = readl(ring->tail);
  539. if (head != tail)
  540. return (head < tail) ?
  541. tail - head : (tail + ring->count - head);
  542. return 0;
  543. }
  544. /**
  545. * i40e_check_tx_hang - Is there a hang in the Tx queue
  546. * @tx_ring: the ring of descriptors
  547. **/
  548. static bool i40e_check_tx_hang(struct i40e_ring *tx_ring)
  549. {
  550. u32 tx_done = tx_ring->stats.packets;
  551. u32 tx_done_old = tx_ring->tx_stats.tx_done_old;
  552. u32 tx_pending = i40e_get_tx_pending(tx_ring);
  553. struct i40e_pf *pf = tx_ring->vsi->back;
  554. bool ret = false;
  555. clear_check_for_tx_hang(tx_ring);
  556. /* Check for a hung queue, but be thorough. This verifies
  557. * that a transmit has been completed since the previous
  558. * check AND there is at least one packet pending. The
  559. * ARMED bit is set to indicate a potential hang. The
  560. * bit is cleared if a pause frame is received to remove
  561. * false hang detection due to PFC or 802.3x frames. By
  562. * requiring this to fail twice we avoid races with
  563. * PFC clearing the ARMED bit and conditions where we
  564. * run the check_tx_hang logic with a transmit completion
  565. * pending but without time to complete it yet.
  566. */
  567. if ((tx_done_old == tx_done) && tx_pending) {
  568. /* make sure it is true for two checks in a row */
  569. ret = test_and_set_bit(__I40E_HANG_CHECK_ARMED,
  570. &tx_ring->state);
  571. } else if (tx_done_old == tx_done &&
  572. (tx_pending < I40E_MIN_DESC_PENDING) && (tx_pending > 0)) {
  573. if (I40E_DEBUG_FLOW & pf->hw.debug_mask)
  574. dev_info(tx_ring->dev, "HW needs some more descs to do a cacheline flush. tx_pending %d, queue %d",
  575. tx_pending, tx_ring->queue_index);
  576. pf->tx_sluggish_count++;
  577. } else {
  578. /* update completed stats and disarm the hang check */
  579. tx_ring->tx_stats.tx_done_old = tx_done;
  580. clear_bit(__I40E_HANG_CHECK_ARMED, &tx_ring->state);
  581. }
  582. return ret;
  583. }
  584. #define WB_STRIDE 0x3
  585. /**
  586. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  587. * @tx_ring: tx ring to clean
  588. * @budget: how many cleans we're allowed
  589. *
  590. * Returns true if there's any budget left (e.g. the clean is finished)
  591. **/
  592. static bool i40e_clean_tx_irq(struct i40e_ring *tx_ring, int budget)
  593. {
  594. u16 i = tx_ring->next_to_clean;
  595. struct i40e_tx_buffer *tx_buf;
  596. struct i40e_tx_desc *tx_head;
  597. struct i40e_tx_desc *tx_desc;
  598. unsigned int total_packets = 0;
  599. unsigned int total_bytes = 0;
  600. tx_buf = &tx_ring->tx_bi[i];
  601. tx_desc = I40E_TX_DESC(tx_ring, i);
  602. i -= tx_ring->count;
  603. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  604. do {
  605. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  606. /* if next_to_watch is not set then there is no work pending */
  607. if (!eop_desc)
  608. break;
  609. /* prevent any other reads prior to eop_desc */
  610. read_barrier_depends();
  611. /* we have caught up to head, no work left to do */
  612. if (tx_head == tx_desc)
  613. break;
  614. /* clear next_to_watch to prevent false hangs */
  615. tx_buf->next_to_watch = NULL;
  616. /* update the statistics for this packet */
  617. total_bytes += tx_buf->bytecount;
  618. total_packets += tx_buf->gso_segs;
  619. /* free the skb */
  620. dev_consume_skb_any(tx_buf->skb);
  621. /* unmap skb header data */
  622. dma_unmap_single(tx_ring->dev,
  623. dma_unmap_addr(tx_buf, dma),
  624. dma_unmap_len(tx_buf, len),
  625. DMA_TO_DEVICE);
  626. /* clear tx_buffer data */
  627. tx_buf->skb = NULL;
  628. dma_unmap_len_set(tx_buf, len, 0);
  629. /* unmap remaining buffers */
  630. while (tx_desc != eop_desc) {
  631. tx_buf++;
  632. tx_desc++;
  633. i++;
  634. if (unlikely(!i)) {
  635. i -= tx_ring->count;
  636. tx_buf = tx_ring->tx_bi;
  637. tx_desc = I40E_TX_DESC(tx_ring, 0);
  638. }
  639. /* unmap any remaining paged data */
  640. if (dma_unmap_len(tx_buf, len)) {
  641. dma_unmap_page(tx_ring->dev,
  642. dma_unmap_addr(tx_buf, dma),
  643. dma_unmap_len(tx_buf, len),
  644. DMA_TO_DEVICE);
  645. dma_unmap_len_set(tx_buf, len, 0);
  646. }
  647. }
  648. /* move us one more past the eop_desc for start of next pkt */
  649. tx_buf++;
  650. tx_desc++;
  651. i++;
  652. if (unlikely(!i)) {
  653. i -= tx_ring->count;
  654. tx_buf = tx_ring->tx_bi;
  655. tx_desc = I40E_TX_DESC(tx_ring, 0);
  656. }
  657. /* update budget accounting */
  658. budget--;
  659. } while (likely(budget));
  660. i += tx_ring->count;
  661. tx_ring->next_to_clean = i;
  662. u64_stats_update_begin(&tx_ring->syncp);
  663. tx_ring->stats.bytes += total_bytes;
  664. tx_ring->stats.packets += total_packets;
  665. u64_stats_update_end(&tx_ring->syncp);
  666. tx_ring->q_vector->tx.total_bytes += total_bytes;
  667. tx_ring->q_vector->tx.total_packets += total_packets;
  668. /* check to see if there are any non-cache aligned descriptors
  669. * waiting to be written back, and kick the hardware to force
  670. * them to be written back in case of napi polling
  671. */
  672. if (budget &&
  673. !((i & WB_STRIDE) == WB_STRIDE) &&
  674. !test_bit(__I40E_DOWN, &tx_ring->vsi->state) &&
  675. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  676. tx_ring->arm_wb = true;
  677. else
  678. tx_ring->arm_wb = false;
  679. if (check_for_tx_hang(tx_ring) && i40e_check_tx_hang(tx_ring)) {
  680. /* schedule immediate reset if we believe we hung */
  681. dev_info(tx_ring->dev, "Detected Tx Unit Hang\n"
  682. " VSI <%d>\n"
  683. " Tx Queue <%d>\n"
  684. " next_to_use <%x>\n"
  685. " next_to_clean <%x>\n",
  686. tx_ring->vsi->seid,
  687. tx_ring->queue_index,
  688. tx_ring->next_to_use, i);
  689. dev_info(tx_ring->dev, "tx_bi[next_to_clean]\n"
  690. " time_stamp <%lx>\n"
  691. " jiffies <%lx>\n",
  692. tx_ring->tx_bi[i].time_stamp, jiffies);
  693. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  694. dev_info(tx_ring->dev,
  695. "tx hang detected on queue %d, reset requested\n",
  696. tx_ring->queue_index);
  697. /* do not fire the reset immediately, wait for the stack to
  698. * decide we are truly stuck, also prevents every queue from
  699. * simultaneously requesting a reset
  700. */
  701. /* the adapter is about to reset, no point in enabling polling */
  702. budget = 1;
  703. }
  704. netdev_tx_completed_queue(netdev_get_tx_queue(tx_ring->netdev,
  705. tx_ring->queue_index),
  706. total_packets, total_bytes);
  707. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  708. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  709. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  710. /* Make sure that anybody stopping the queue after this
  711. * sees the new next_to_clean.
  712. */
  713. smp_mb();
  714. if (__netif_subqueue_stopped(tx_ring->netdev,
  715. tx_ring->queue_index) &&
  716. !test_bit(__I40E_DOWN, &tx_ring->vsi->state)) {
  717. netif_wake_subqueue(tx_ring->netdev,
  718. tx_ring->queue_index);
  719. ++tx_ring->tx_stats.restart_queue;
  720. }
  721. }
  722. return !!budget;
  723. }
  724. /**
  725. * i40e_force_wb - Arm hardware to do a wb on noncache aligned descriptors
  726. * @vsi: the VSI we care about
  727. * @q_vector: the vector on which to force writeback
  728. *
  729. **/
  730. static void i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  731. {
  732. u32 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  733. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
  734. I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK;
  735. /* allow 00 to be written to the index */
  736. wr32(&vsi->back->hw,
  737. I40E_PFINT_DYN_CTLN(q_vector->v_idx + vsi->base_vector - 1),
  738. val);
  739. }
  740. /**
  741. * i40e_set_new_dynamic_itr - Find new ITR level
  742. * @rc: structure containing ring performance data
  743. *
  744. * Stores a new ITR value based on packets and byte counts during
  745. * the last interrupt. The advantage of per interrupt computation
  746. * is faster updates and more accurate ITR for the current traffic
  747. * pattern. Constants in this function were computed based on
  748. * theoretical maximum wire speed and thresholds were set based on
  749. * testing data as well as attempting to minimize response time
  750. * while increasing bulk throughput.
  751. **/
  752. static void i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  753. {
  754. enum i40e_latency_range new_latency_range = rc->latency_range;
  755. u32 new_itr = rc->itr;
  756. int bytes_per_int;
  757. if (rc->total_packets == 0 || !rc->itr)
  758. return;
  759. /* simple throttlerate management
  760. * 0-10MB/s lowest (100000 ints/s)
  761. * 10-20MB/s low (20000 ints/s)
  762. * 20-1249MB/s bulk (8000 ints/s)
  763. */
  764. bytes_per_int = rc->total_bytes / rc->itr;
  765. switch (rc->itr) {
  766. case I40E_LOWEST_LATENCY:
  767. if (bytes_per_int > 10)
  768. new_latency_range = I40E_LOW_LATENCY;
  769. break;
  770. case I40E_LOW_LATENCY:
  771. if (bytes_per_int > 20)
  772. new_latency_range = I40E_BULK_LATENCY;
  773. else if (bytes_per_int <= 10)
  774. new_latency_range = I40E_LOWEST_LATENCY;
  775. break;
  776. case I40E_BULK_LATENCY:
  777. if (bytes_per_int <= 20)
  778. rc->latency_range = I40E_LOW_LATENCY;
  779. break;
  780. }
  781. switch (new_latency_range) {
  782. case I40E_LOWEST_LATENCY:
  783. new_itr = I40E_ITR_100K;
  784. break;
  785. case I40E_LOW_LATENCY:
  786. new_itr = I40E_ITR_20K;
  787. break;
  788. case I40E_BULK_LATENCY:
  789. new_itr = I40E_ITR_8K;
  790. break;
  791. default:
  792. break;
  793. }
  794. if (new_itr != rc->itr) {
  795. /* do an exponential smoothing */
  796. new_itr = (10 * new_itr * rc->itr) /
  797. ((9 * new_itr) + rc->itr);
  798. rc->itr = new_itr & I40E_MAX_ITR;
  799. }
  800. rc->total_bytes = 0;
  801. rc->total_packets = 0;
  802. }
  803. /**
  804. * i40e_update_dynamic_itr - Adjust ITR based on bytes per int
  805. * @q_vector: the vector to adjust
  806. **/
  807. static void i40e_update_dynamic_itr(struct i40e_q_vector *q_vector)
  808. {
  809. u16 vector = q_vector->vsi->base_vector + q_vector->v_idx;
  810. struct i40e_hw *hw = &q_vector->vsi->back->hw;
  811. u32 reg_addr;
  812. u16 old_itr;
  813. reg_addr = I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1);
  814. old_itr = q_vector->rx.itr;
  815. i40e_set_new_dynamic_itr(&q_vector->rx);
  816. if (old_itr != q_vector->rx.itr)
  817. wr32(hw, reg_addr, q_vector->rx.itr);
  818. reg_addr = I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1);
  819. old_itr = q_vector->tx.itr;
  820. i40e_set_new_dynamic_itr(&q_vector->tx);
  821. if (old_itr != q_vector->tx.itr)
  822. wr32(hw, reg_addr, q_vector->tx.itr);
  823. }
  824. /**
  825. * i40e_clean_programming_status - clean the programming status descriptor
  826. * @rx_ring: the rx ring that has this descriptor
  827. * @rx_desc: the rx descriptor written back by HW
  828. *
  829. * Flow director should handle FD_FILTER_STATUS to check its filter programming
  830. * status being successful or not and take actions accordingly. FCoE should
  831. * handle its context/filter programming/invalidation status and take actions.
  832. *
  833. **/
  834. static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
  835. union i40e_rx_desc *rx_desc)
  836. {
  837. u64 qw;
  838. u8 id;
  839. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  840. id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
  841. I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
  842. if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
  843. i40e_fd_handle_status(rx_ring, rx_desc, id);
  844. #ifdef I40E_FCOE
  845. else if ((id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_PROG_STATUS) ||
  846. (id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_INVL_STATUS))
  847. i40e_fcoe_handle_status(rx_ring, rx_desc, id);
  848. #endif
  849. }
  850. /**
  851. * i40e_setup_tx_descriptors - Allocate the Tx descriptors
  852. * @tx_ring: the tx ring to set up
  853. *
  854. * Return 0 on success, negative on error
  855. **/
  856. int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
  857. {
  858. struct device *dev = tx_ring->dev;
  859. int bi_size;
  860. if (!dev)
  861. return -ENOMEM;
  862. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  863. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  864. if (!tx_ring->tx_bi)
  865. goto err;
  866. /* round up to nearest 4K */
  867. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  868. /* add u32 for head writeback, align after this takes care of
  869. * guaranteeing this is at least one cache line in size
  870. */
  871. tx_ring->size += sizeof(u32);
  872. tx_ring->size = ALIGN(tx_ring->size, 4096);
  873. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  874. &tx_ring->dma, GFP_KERNEL);
  875. if (!tx_ring->desc) {
  876. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  877. tx_ring->size);
  878. goto err;
  879. }
  880. tx_ring->next_to_use = 0;
  881. tx_ring->next_to_clean = 0;
  882. return 0;
  883. err:
  884. kfree(tx_ring->tx_bi);
  885. tx_ring->tx_bi = NULL;
  886. return -ENOMEM;
  887. }
  888. /**
  889. * i40e_clean_rx_ring - Free Rx buffers
  890. * @rx_ring: ring to be cleaned
  891. **/
  892. void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
  893. {
  894. struct device *dev = rx_ring->dev;
  895. struct i40e_rx_buffer *rx_bi;
  896. unsigned long bi_size;
  897. u16 i;
  898. /* ring already cleared, nothing to do */
  899. if (!rx_ring->rx_bi)
  900. return;
  901. /* Free all the Rx ring sk_buffs */
  902. for (i = 0; i < rx_ring->count; i++) {
  903. rx_bi = &rx_ring->rx_bi[i];
  904. if (rx_bi->dma) {
  905. dma_unmap_single(dev,
  906. rx_bi->dma,
  907. rx_ring->rx_buf_len,
  908. DMA_FROM_DEVICE);
  909. rx_bi->dma = 0;
  910. }
  911. if (rx_bi->skb) {
  912. dev_kfree_skb(rx_bi->skb);
  913. rx_bi->skb = NULL;
  914. }
  915. if (rx_bi->page) {
  916. if (rx_bi->page_dma) {
  917. dma_unmap_page(dev,
  918. rx_bi->page_dma,
  919. PAGE_SIZE / 2,
  920. DMA_FROM_DEVICE);
  921. rx_bi->page_dma = 0;
  922. }
  923. __free_page(rx_bi->page);
  924. rx_bi->page = NULL;
  925. rx_bi->page_offset = 0;
  926. }
  927. }
  928. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  929. memset(rx_ring->rx_bi, 0, bi_size);
  930. /* Zero out the descriptor ring */
  931. memset(rx_ring->desc, 0, rx_ring->size);
  932. rx_ring->next_to_clean = 0;
  933. rx_ring->next_to_use = 0;
  934. }
  935. /**
  936. * i40e_free_rx_resources - Free Rx resources
  937. * @rx_ring: ring to clean the resources from
  938. *
  939. * Free all receive software resources
  940. **/
  941. void i40e_free_rx_resources(struct i40e_ring *rx_ring)
  942. {
  943. i40e_clean_rx_ring(rx_ring);
  944. kfree(rx_ring->rx_bi);
  945. rx_ring->rx_bi = NULL;
  946. if (rx_ring->desc) {
  947. dma_free_coherent(rx_ring->dev, rx_ring->size,
  948. rx_ring->desc, rx_ring->dma);
  949. rx_ring->desc = NULL;
  950. }
  951. }
  952. /**
  953. * i40e_setup_rx_descriptors - Allocate Rx descriptors
  954. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  955. *
  956. * Returns 0 on success, negative on failure
  957. **/
  958. int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
  959. {
  960. struct device *dev = rx_ring->dev;
  961. int bi_size;
  962. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  963. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  964. if (!rx_ring->rx_bi)
  965. goto err;
  966. u64_stats_init(&rx_ring->syncp);
  967. /* Round up to nearest 4K */
  968. rx_ring->size = ring_is_16byte_desc_enabled(rx_ring)
  969. ? rx_ring->count * sizeof(union i40e_16byte_rx_desc)
  970. : rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  971. rx_ring->size = ALIGN(rx_ring->size, 4096);
  972. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  973. &rx_ring->dma, GFP_KERNEL);
  974. if (!rx_ring->desc) {
  975. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  976. rx_ring->size);
  977. goto err;
  978. }
  979. rx_ring->next_to_clean = 0;
  980. rx_ring->next_to_use = 0;
  981. return 0;
  982. err:
  983. kfree(rx_ring->rx_bi);
  984. rx_ring->rx_bi = NULL;
  985. return -ENOMEM;
  986. }
  987. /**
  988. * i40e_release_rx_desc - Store the new tail and head values
  989. * @rx_ring: ring to bump
  990. * @val: new head index
  991. **/
  992. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  993. {
  994. rx_ring->next_to_use = val;
  995. /* Force memory writes to complete before letting h/w
  996. * know there are new descriptors to fetch. (Only
  997. * applicable for weak-ordered memory model archs,
  998. * such as IA-64).
  999. */
  1000. wmb();
  1001. writel(val, rx_ring->tail);
  1002. }
  1003. /**
  1004. * i40e_alloc_rx_buffers - Replace used receive buffers; packet split
  1005. * @rx_ring: ring to place buffers on
  1006. * @cleaned_count: number of buffers to replace
  1007. **/
  1008. void i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  1009. {
  1010. u16 i = rx_ring->next_to_use;
  1011. union i40e_rx_desc *rx_desc;
  1012. struct i40e_rx_buffer *bi;
  1013. struct sk_buff *skb;
  1014. /* do nothing if no valid netdev defined */
  1015. if (!rx_ring->netdev || !cleaned_count)
  1016. return;
  1017. while (cleaned_count--) {
  1018. rx_desc = I40E_RX_DESC(rx_ring, i);
  1019. bi = &rx_ring->rx_bi[i];
  1020. skb = bi->skb;
  1021. if (!skb) {
  1022. skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
  1023. rx_ring->rx_buf_len);
  1024. if (!skb) {
  1025. rx_ring->rx_stats.alloc_buff_failed++;
  1026. goto no_buffers;
  1027. }
  1028. /* initialize queue mapping */
  1029. skb_record_rx_queue(skb, rx_ring->queue_index);
  1030. bi->skb = skb;
  1031. }
  1032. if (!bi->dma) {
  1033. bi->dma = dma_map_single(rx_ring->dev,
  1034. skb->data,
  1035. rx_ring->rx_buf_len,
  1036. DMA_FROM_DEVICE);
  1037. if (dma_mapping_error(rx_ring->dev, bi->dma)) {
  1038. rx_ring->rx_stats.alloc_buff_failed++;
  1039. bi->dma = 0;
  1040. goto no_buffers;
  1041. }
  1042. }
  1043. if (ring_is_ps_enabled(rx_ring)) {
  1044. if (!bi->page) {
  1045. bi->page = alloc_page(GFP_ATOMIC);
  1046. if (!bi->page) {
  1047. rx_ring->rx_stats.alloc_page_failed++;
  1048. goto no_buffers;
  1049. }
  1050. }
  1051. if (!bi->page_dma) {
  1052. /* use a half page if we're re-using */
  1053. bi->page_offset ^= PAGE_SIZE / 2;
  1054. bi->page_dma = dma_map_page(rx_ring->dev,
  1055. bi->page,
  1056. bi->page_offset,
  1057. PAGE_SIZE / 2,
  1058. DMA_FROM_DEVICE);
  1059. if (dma_mapping_error(rx_ring->dev,
  1060. bi->page_dma)) {
  1061. rx_ring->rx_stats.alloc_page_failed++;
  1062. bi->page_dma = 0;
  1063. goto no_buffers;
  1064. }
  1065. }
  1066. /* Refresh the desc even if buffer_addrs didn't change
  1067. * because each write-back erases this info.
  1068. */
  1069. rx_desc->read.pkt_addr = cpu_to_le64(bi->page_dma);
  1070. rx_desc->read.hdr_addr = cpu_to_le64(bi->dma);
  1071. } else {
  1072. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma);
  1073. rx_desc->read.hdr_addr = 0;
  1074. }
  1075. i++;
  1076. if (i == rx_ring->count)
  1077. i = 0;
  1078. }
  1079. no_buffers:
  1080. if (rx_ring->next_to_use != i)
  1081. i40e_release_rx_desc(rx_ring, i);
  1082. }
  1083. /**
  1084. * i40e_receive_skb - Send a completed packet up the stack
  1085. * @rx_ring: rx ring in play
  1086. * @skb: packet to send up
  1087. * @vlan_tag: vlan tag for packet
  1088. **/
  1089. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  1090. struct sk_buff *skb, u16 vlan_tag)
  1091. {
  1092. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  1093. struct i40e_vsi *vsi = rx_ring->vsi;
  1094. u64 flags = vsi->back->flags;
  1095. if (vlan_tag & VLAN_VID_MASK)
  1096. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  1097. if (flags & I40E_FLAG_IN_NETPOLL)
  1098. netif_rx(skb);
  1099. else
  1100. napi_gro_receive(&q_vector->napi, skb);
  1101. }
  1102. /**
  1103. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  1104. * @vsi: the VSI we care about
  1105. * @skb: skb currently being received and modified
  1106. * @rx_status: status value of last descriptor in packet
  1107. * @rx_error: error value of last descriptor in packet
  1108. * @rx_ptype: ptype value of last descriptor in packet
  1109. **/
  1110. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  1111. struct sk_buff *skb,
  1112. u32 rx_status,
  1113. u32 rx_error,
  1114. u16 rx_ptype)
  1115. {
  1116. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(rx_ptype);
  1117. bool ipv4 = false, ipv6 = false;
  1118. bool ipv4_tunnel, ipv6_tunnel;
  1119. __wsum rx_udp_csum;
  1120. struct iphdr *iph;
  1121. __sum16 csum;
  1122. ipv4_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT4_MAC_PAY3) &&
  1123. (rx_ptype < I40E_RX_PTYPE_GRENAT4_MACVLAN_IPV6_ICMP_PAY4);
  1124. ipv6_tunnel = (rx_ptype > I40E_RX_PTYPE_GRENAT6_MAC_PAY3) &&
  1125. (rx_ptype < I40E_RX_PTYPE_GRENAT6_MACVLAN_IPV6_ICMP_PAY4);
  1126. skb->ip_summed = CHECKSUM_NONE;
  1127. /* Rx csum enabled and ip headers found? */
  1128. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  1129. return;
  1130. /* did the hardware decode the packet and checksum? */
  1131. if (!(rx_status & (1 << I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  1132. return;
  1133. /* both known and outer_ip must be set for the below code to work */
  1134. if (!(decoded.known && decoded.outer_ip))
  1135. return;
  1136. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1137. decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4)
  1138. ipv4 = true;
  1139. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1140. decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6)
  1141. ipv6 = true;
  1142. if (ipv4 &&
  1143. (rx_error & ((1 << I40E_RX_DESC_ERROR_IPE_SHIFT) |
  1144. (1 << I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  1145. goto checksum_fail;
  1146. /* likely incorrect csum if alternate IP extension headers found */
  1147. if (ipv6 &&
  1148. rx_status & (1 << I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  1149. /* don't increment checksum err here, non-fatal err */
  1150. return;
  1151. /* there was some L4 error, count error and punt packet to the stack */
  1152. if (rx_error & (1 << I40E_RX_DESC_ERROR_L4E_SHIFT))
  1153. goto checksum_fail;
  1154. /* handle packets that were not able to be checksummed due
  1155. * to arrival speed, in this case the stack can compute
  1156. * the csum.
  1157. */
  1158. if (rx_error & (1 << I40E_RX_DESC_ERROR_PPRS_SHIFT))
  1159. return;
  1160. /* If VXLAN traffic has an outer UDPv4 checksum we need to check
  1161. * it in the driver, hardware does not do it for us.
  1162. * Since L3L4P bit was set we assume a valid IHL value (>=5)
  1163. * so the total length of IPv4 header is IHL*4 bytes
  1164. * The UDP_0 bit *may* bet set if the *inner* header is UDP
  1165. */
  1166. if (ipv4_tunnel) {
  1167. skb->transport_header = skb->mac_header +
  1168. sizeof(struct ethhdr) +
  1169. (ip_hdr(skb)->ihl * 4);
  1170. /* Add 4 bytes for VLAN tagged packets */
  1171. skb->transport_header += (skb->protocol == htons(ETH_P_8021Q) ||
  1172. skb->protocol == htons(ETH_P_8021AD))
  1173. ? VLAN_HLEN : 0;
  1174. if ((ip_hdr(skb)->protocol == IPPROTO_UDP) &&
  1175. (udp_hdr(skb)->check != 0)) {
  1176. rx_udp_csum = udp_csum(skb);
  1177. iph = ip_hdr(skb);
  1178. csum = csum_tcpudp_magic(
  1179. iph->saddr, iph->daddr,
  1180. (skb->len - skb_transport_offset(skb)),
  1181. IPPROTO_UDP, rx_udp_csum);
  1182. if (udp_hdr(skb)->check != csum)
  1183. goto checksum_fail;
  1184. } /* else its GRE and so no outer UDP header */
  1185. }
  1186. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1187. skb->csum_level = ipv4_tunnel || ipv6_tunnel;
  1188. return;
  1189. checksum_fail:
  1190. vsi->back->hw_csum_rx_error++;
  1191. }
  1192. /**
  1193. * i40e_rx_hash - returns the hash value from the Rx descriptor
  1194. * @ring: descriptor ring
  1195. * @rx_desc: specific descriptor
  1196. **/
  1197. static inline u32 i40e_rx_hash(struct i40e_ring *ring,
  1198. union i40e_rx_desc *rx_desc)
  1199. {
  1200. const __le64 rss_mask =
  1201. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  1202. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  1203. if ((ring->netdev->features & NETIF_F_RXHASH) &&
  1204. (rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask)
  1205. return le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  1206. else
  1207. return 0;
  1208. }
  1209. /**
  1210. * i40e_ptype_to_hash - get a hash type
  1211. * @ptype: the ptype value from the descriptor
  1212. *
  1213. * Returns a hash type to be used by skb_set_hash
  1214. **/
  1215. static inline enum pkt_hash_types i40e_ptype_to_hash(u8 ptype)
  1216. {
  1217. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  1218. if (!decoded.known)
  1219. return PKT_HASH_TYPE_NONE;
  1220. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1221. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  1222. return PKT_HASH_TYPE_L4;
  1223. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1224. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  1225. return PKT_HASH_TYPE_L3;
  1226. else
  1227. return PKT_HASH_TYPE_L2;
  1228. }
  1229. /**
  1230. * i40e_clean_rx_irq - Reclaim resources after receive completes
  1231. * @rx_ring: rx ring to clean
  1232. * @budget: how many cleans we're allowed
  1233. *
  1234. * Returns true if there's any budget left (e.g. the clean is finished)
  1235. **/
  1236. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1237. {
  1238. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1239. u16 rx_packet_len, rx_header_len, rx_sph, rx_hbo;
  1240. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1241. const int current_node = numa_node_id();
  1242. struct i40e_vsi *vsi = rx_ring->vsi;
  1243. u16 i = rx_ring->next_to_clean;
  1244. union i40e_rx_desc *rx_desc;
  1245. u32 rx_error, rx_status;
  1246. u8 rx_ptype;
  1247. u64 qword;
  1248. if (budget <= 0)
  1249. return 0;
  1250. rx_desc = I40E_RX_DESC(rx_ring, i);
  1251. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1252. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1253. I40E_RXD_QW1_STATUS_SHIFT;
  1254. while (rx_status & (1 << I40E_RX_DESC_STATUS_DD_SHIFT)) {
  1255. union i40e_rx_desc *next_rxd;
  1256. struct i40e_rx_buffer *rx_bi;
  1257. struct sk_buff *skb;
  1258. u16 vlan_tag;
  1259. if (i40e_rx_is_programming_status(qword)) {
  1260. i40e_clean_programming_status(rx_ring, rx_desc);
  1261. I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
  1262. goto next_desc;
  1263. }
  1264. rx_bi = &rx_ring->rx_bi[i];
  1265. skb = rx_bi->skb;
  1266. prefetch(skb->data);
  1267. rx_packet_len = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1268. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1269. rx_header_len = (qword & I40E_RXD_QW1_LENGTH_HBUF_MASK) >>
  1270. I40E_RXD_QW1_LENGTH_HBUF_SHIFT;
  1271. rx_sph = (qword & I40E_RXD_QW1_LENGTH_SPH_MASK) >>
  1272. I40E_RXD_QW1_LENGTH_SPH_SHIFT;
  1273. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1274. I40E_RXD_QW1_ERROR_SHIFT;
  1275. rx_hbo = rx_error & (1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  1276. rx_error &= ~(1 << I40E_RX_DESC_ERROR_HBO_SHIFT);
  1277. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1278. I40E_RXD_QW1_PTYPE_SHIFT;
  1279. rx_bi->skb = NULL;
  1280. /* This memory barrier is needed to keep us from reading
  1281. * any other fields out of the rx_desc until we know the
  1282. * STATUS_DD bit is set
  1283. */
  1284. rmb();
  1285. /* Get the header and possibly the whole packet
  1286. * If this is an skb from previous receive dma will be 0
  1287. */
  1288. if (rx_bi->dma) {
  1289. u16 len;
  1290. if (rx_hbo)
  1291. len = I40E_RX_HDR_SIZE;
  1292. else if (rx_sph)
  1293. len = rx_header_len;
  1294. else if (rx_packet_len)
  1295. len = rx_packet_len; /* 1buf/no split found */
  1296. else
  1297. len = rx_header_len; /* split always mode */
  1298. skb_put(skb, len);
  1299. dma_unmap_single(rx_ring->dev,
  1300. rx_bi->dma,
  1301. rx_ring->rx_buf_len,
  1302. DMA_FROM_DEVICE);
  1303. rx_bi->dma = 0;
  1304. }
  1305. /* Get the rest of the data if this was a header split */
  1306. if (ring_is_ps_enabled(rx_ring) && rx_packet_len) {
  1307. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  1308. rx_bi->page,
  1309. rx_bi->page_offset,
  1310. rx_packet_len);
  1311. skb->len += rx_packet_len;
  1312. skb->data_len += rx_packet_len;
  1313. skb->truesize += rx_packet_len;
  1314. if ((page_count(rx_bi->page) == 1) &&
  1315. (page_to_nid(rx_bi->page) == current_node))
  1316. get_page(rx_bi->page);
  1317. else
  1318. rx_bi->page = NULL;
  1319. dma_unmap_page(rx_ring->dev,
  1320. rx_bi->page_dma,
  1321. PAGE_SIZE / 2,
  1322. DMA_FROM_DEVICE);
  1323. rx_bi->page_dma = 0;
  1324. }
  1325. I40E_RX_NEXT_DESC_PREFETCH(rx_ring, i, next_rxd);
  1326. if (unlikely(
  1327. !(rx_status & (1 << I40E_RX_DESC_STATUS_EOF_SHIFT)))) {
  1328. struct i40e_rx_buffer *next_buffer;
  1329. next_buffer = &rx_ring->rx_bi[i];
  1330. if (ring_is_ps_enabled(rx_ring)) {
  1331. rx_bi->skb = next_buffer->skb;
  1332. rx_bi->dma = next_buffer->dma;
  1333. next_buffer->skb = skb;
  1334. next_buffer->dma = 0;
  1335. }
  1336. rx_ring->rx_stats.non_eop_descs++;
  1337. goto next_desc;
  1338. }
  1339. /* ERR_MASK will only have valid bits if EOP set */
  1340. if (unlikely(rx_error & (1 << I40E_RX_DESC_ERROR_RXE_SHIFT))) {
  1341. dev_kfree_skb_any(skb);
  1342. /* TODO: shouldn't we increment a counter indicating the
  1343. * drop?
  1344. */
  1345. goto next_desc;
  1346. }
  1347. skb_set_hash(skb, i40e_rx_hash(rx_ring, rx_desc),
  1348. i40e_ptype_to_hash(rx_ptype));
  1349. if (unlikely(rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK)) {
  1350. i40e_ptp_rx_hwtstamp(vsi->back, skb, (rx_status &
  1351. I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
  1352. I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT);
  1353. rx_ring->last_rx_timestamp = jiffies;
  1354. }
  1355. /* probably a little skewed due to removing CRC */
  1356. total_rx_bytes += skb->len;
  1357. total_rx_packets++;
  1358. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1359. i40e_rx_checksum(vsi, skb, rx_status, rx_error, rx_ptype);
  1360. vlan_tag = rx_status & (1 << I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)
  1361. ? le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1)
  1362. : 0;
  1363. #ifdef I40E_FCOE
  1364. if (!i40e_fcoe_handle_offload(rx_ring, rx_desc, skb)) {
  1365. dev_kfree_skb_any(skb);
  1366. goto next_desc;
  1367. }
  1368. #endif
  1369. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1370. rx_ring->netdev->last_rx = jiffies;
  1371. budget--;
  1372. next_desc:
  1373. rx_desc->wb.qword1.status_error_len = 0;
  1374. if (!budget)
  1375. break;
  1376. cleaned_count++;
  1377. /* return some buffers to hardware, one at a time is too slow */
  1378. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1379. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  1380. cleaned_count = 0;
  1381. }
  1382. /* use prefetched values */
  1383. rx_desc = next_rxd;
  1384. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1385. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1386. I40E_RXD_QW1_STATUS_SHIFT;
  1387. }
  1388. rx_ring->next_to_clean = i;
  1389. u64_stats_update_begin(&rx_ring->syncp);
  1390. rx_ring->stats.packets += total_rx_packets;
  1391. rx_ring->stats.bytes += total_rx_bytes;
  1392. u64_stats_update_end(&rx_ring->syncp);
  1393. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1394. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1395. if (cleaned_count)
  1396. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  1397. return budget > 0;
  1398. }
  1399. /**
  1400. * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
  1401. * @napi: napi struct with our devices info in it
  1402. * @budget: amount of work driver is allowed to do this pass, in packets
  1403. *
  1404. * This function will clean all queues associated with a q_vector.
  1405. *
  1406. * Returns the amount of work done
  1407. **/
  1408. int i40e_napi_poll(struct napi_struct *napi, int budget)
  1409. {
  1410. struct i40e_q_vector *q_vector =
  1411. container_of(napi, struct i40e_q_vector, napi);
  1412. struct i40e_vsi *vsi = q_vector->vsi;
  1413. struct i40e_ring *ring;
  1414. bool clean_complete = true;
  1415. bool arm_wb = false;
  1416. int budget_per_ring;
  1417. if (test_bit(__I40E_DOWN, &vsi->state)) {
  1418. napi_complete(napi);
  1419. return 0;
  1420. }
  1421. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1422. * budget and be more aggressive about cleaning up the Tx descriptors.
  1423. */
  1424. i40e_for_each_ring(ring, q_vector->tx) {
  1425. clean_complete &= i40e_clean_tx_irq(ring, vsi->work_limit);
  1426. arm_wb |= ring->arm_wb;
  1427. }
  1428. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1429. * allow the budget to go below 1 because that would exit polling early.
  1430. */
  1431. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1432. i40e_for_each_ring(ring, q_vector->rx)
  1433. clean_complete &= i40e_clean_rx_irq(ring, budget_per_ring);
  1434. /* If work not completed, return budget and polling will return */
  1435. if (!clean_complete) {
  1436. if (arm_wb)
  1437. i40e_force_wb(vsi, q_vector);
  1438. return budget;
  1439. }
  1440. /* Work is done so exit the polling mode and re-enable the interrupt */
  1441. napi_complete(napi);
  1442. if (ITR_IS_DYNAMIC(vsi->rx_itr_setting) ||
  1443. ITR_IS_DYNAMIC(vsi->tx_itr_setting))
  1444. i40e_update_dynamic_itr(q_vector);
  1445. if (!test_bit(__I40E_DOWN, &vsi->state)) {
  1446. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  1447. i40e_irq_dynamic_enable(vsi,
  1448. q_vector->v_idx + vsi->base_vector);
  1449. } else {
  1450. struct i40e_hw *hw = &vsi->back->hw;
  1451. /* We re-enable the queue 0 cause, but
  1452. * don't worry about dynamic_enable
  1453. * because we left it on for the other
  1454. * possible interrupts during napi
  1455. */
  1456. u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
  1457. qval |= I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  1458. wr32(hw, I40E_QINT_RQCTL(0), qval);
  1459. qval = rd32(hw, I40E_QINT_TQCTL(0));
  1460. qval |= I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  1461. wr32(hw, I40E_QINT_TQCTL(0), qval);
  1462. i40e_irq_dynamic_enable_icr0(vsi->back);
  1463. }
  1464. }
  1465. return 0;
  1466. }
  1467. /**
  1468. * i40e_atr - Add a Flow Director ATR filter
  1469. * @tx_ring: ring to add programming descriptor to
  1470. * @skb: send buffer
  1471. * @flags: send flags
  1472. * @protocol: wire protocol
  1473. **/
  1474. static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1475. u32 flags, __be16 protocol)
  1476. {
  1477. struct i40e_filter_program_desc *fdir_desc;
  1478. struct i40e_pf *pf = tx_ring->vsi->back;
  1479. union {
  1480. unsigned char *network;
  1481. struct iphdr *ipv4;
  1482. struct ipv6hdr *ipv6;
  1483. } hdr;
  1484. struct tcphdr *th;
  1485. unsigned int hlen;
  1486. u32 flex_ptype, dtype_cmd;
  1487. u16 i;
  1488. /* make sure ATR is enabled */
  1489. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  1490. return;
  1491. /* if sampling is disabled do nothing */
  1492. if (!tx_ring->atr_sample_rate)
  1493. return;
  1494. /* snag network header to get L4 type and address */
  1495. hdr.network = skb_network_header(skb);
  1496. /* Currently only IPv4/IPv6 with TCP is supported */
  1497. if (protocol == htons(ETH_P_IP)) {
  1498. if (hdr.ipv4->protocol != IPPROTO_TCP)
  1499. return;
  1500. /* access ihl as a u8 to avoid unaligned access on ia64 */
  1501. hlen = (hdr.network[0] & 0x0F) << 2;
  1502. } else if (protocol == htons(ETH_P_IPV6)) {
  1503. if (hdr.ipv6->nexthdr != IPPROTO_TCP)
  1504. return;
  1505. hlen = sizeof(struct ipv6hdr);
  1506. } else {
  1507. return;
  1508. }
  1509. th = (struct tcphdr *)(hdr.network + hlen);
  1510. /* Due to lack of space, no more new filters can be programmed */
  1511. if (th->syn && (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED))
  1512. return;
  1513. tx_ring->atr_count++;
  1514. /* sample on all syn/fin/rst packets or once every atr sample rate */
  1515. if (!th->fin &&
  1516. !th->syn &&
  1517. !th->rst &&
  1518. (tx_ring->atr_count < tx_ring->atr_sample_rate))
  1519. return;
  1520. tx_ring->atr_count = 0;
  1521. /* grab the next descriptor */
  1522. i = tx_ring->next_to_use;
  1523. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  1524. i++;
  1525. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1526. flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  1527. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  1528. flex_ptype |= (protocol == htons(ETH_P_IP)) ?
  1529. (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
  1530. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
  1531. (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
  1532. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  1533. flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  1534. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  1535. dtype_cmd |= (th->fin || th->rst) ?
  1536. (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  1537. I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
  1538. (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  1539. I40E_TXD_FLTR_QW1_PCMD_SHIFT);
  1540. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
  1541. I40E_TXD_FLTR_QW1_DEST_SHIFT;
  1542. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
  1543. I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
  1544. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  1545. dtype_cmd |=
  1546. ((u32)pf->fd_atr_cnt_idx << I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  1547. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  1548. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  1549. fdir_desc->rsvd = cpu_to_le32(0);
  1550. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  1551. fdir_desc->fd_id = cpu_to_le32(0);
  1552. }
  1553. /**
  1554. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  1555. * @skb: send buffer
  1556. * @tx_ring: ring to send buffer on
  1557. * @flags: the tx flags to be set
  1558. *
  1559. * Checks the skb and set up correspondingly several generic transmit flags
  1560. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  1561. *
  1562. * Returns error code indicate the frame should be dropped upon error and the
  1563. * otherwise returns 0 to indicate the flags has been set properly.
  1564. **/
  1565. #ifdef I40E_FCOE
  1566. int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  1567. struct i40e_ring *tx_ring,
  1568. u32 *flags)
  1569. #else
  1570. static int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  1571. struct i40e_ring *tx_ring,
  1572. u32 *flags)
  1573. #endif
  1574. {
  1575. __be16 protocol = skb->protocol;
  1576. u32 tx_flags = 0;
  1577. /* if we have a HW VLAN tag being added, default to the HW one */
  1578. if (skb_vlan_tag_present(skb)) {
  1579. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  1580. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1581. /* else if it is a SW VLAN, check the next protocol and store the tag */
  1582. } else if (protocol == htons(ETH_P_8021Q)) {
  1583. struct vlan_hdr *vhdr, _vhdr;
  1584. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  1585. if (!vhdr)
  1586. return -EINVAL;
  1587. protocol = vhdr->h_vlan_encapsulated_proto;
  1588. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  1589. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  1590. }
  1591. /* Insert 802.1p priority into VLAN header */
  1592. if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
  1593. (skb->priority != TC_PRIO_CONTROL)) {
  1594. tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
  1595. tx_flags |= (skb->priority & 0x7) <<
  1596. I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
  1597. if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
  1598. struct vlan_ethhdr *vhdr;
  1599. int rc;
  1600. rc = skb_cow_head(skb, 0);
  1601. if (rc < 0)
  1602. return rc;
  1603. vhdr = (struct vlan_ethhdr *)skb->data;
  1604. vhdr->h_vlan_TCI = htons(tx_flags >>
  1605. I40E_TX_FLAGS_VLAN_SHIFT);
  1606. } else {
  1607. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  1608. }
  1609. }
  1610. *flags = tx_flags;
  1611. return 0;
  1612. }
  1613. /**
  1614. * i40e_tso - set up the tso context descriptor
  1615. * @tx_ring: ptr to the ring to send
  1616. * @skb: ptr to the skb we're sending
  1617. * @tx_flags: the collected send information
  1618. * @protocol: the send protocol
  1619. * @hdr_len: ptr to the size of the packet header
  1620. * @cd_tunneling: ptr to context descriptor bits
  1621. *
  1622. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  1623. **/
  1624. static int i40e_tso(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1625. u32 tx_flags, __be16 protocol, u8 *hdr_len,
  1626. u64 *cd_type_cmd_tso_mss, u32 *cd_tunneling)
  1627. {
  1628. u32 cd_cmd, cd_tso_len, cd_mss;
  1629. struct ipv6hdr *ipv6h;
  1630. struct tcphdr *tcph;
  1631. struct iphdr *iph;
  1632. u32 l4len;
  1633. int err;
  1634. if (!skb_is_gso(skb))
  1635. return 0;
  1636. err = skb_cow_head(skb, 0);
  1637. if (err < 0)
  1638. return err;
  1639. iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
  1640. ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
  1641. if (iph->version == 4) {
  1642. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1643. iph->tot_len = 0;
  1644. iph->check = 0;
  1645. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1646. 0, IPPROTO_TCP, 0);
  1647. } else if (ipv6h->version == 6) {
  1648. tcph = skb->encapsulation ? inner_tcp_hdr(skb) : tcp_hdr(skb);
  1649. ipv6h->payload_len = 0;
  1650. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  1651. 0, IPPROTO_TCP, 0);
  1652. }
  1653. l4len = skb->encapsulation ? inner_tcp_hdrlen(skb) : tcp_hdrlen(skb);
  1654. *hdr_len = (skb->encapsulation
  1655. ? (skb_inner_transport_header(skb) - skb->data)
  1656. : skb_transport_offset(skb)) + l4len;
  1657. /* find the field values */
  1658. cd_cmd = I40E_TX_CTX_DESC_TSO;
  1659. cd_tso_len = skb->len - *hdr_len;
  1660. cd_mss = skb_shinfo(skb)->gso_size;
  1661. *cd_type_cmd_tso_mss |= ((u64)cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  1662. ((u64)cd_tso_len <<
  1663. I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  1664. ((u64)cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  1665. return 1;
  1666. }
  1667. /**
  1668. * i40e_tsyn - set up the tsyn context descriptor
  1669. * @tx_ring: ptr to the ring to send
  1670. * @skb: ptr to the skb we're sending
  1671. * @tx_flags: the collected send information
  1672. *
  1673. * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
  1674. **/
  1675. static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1676. u32 tx_flags, u64 *cd_type_cmd_tso_mss)
  1677. {
  1678. struct i40e_pf *pf;
  1679. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  1680. return 0;
  1681. /* Tx timestamps cannot be sampled when doing TSO */
  1682. if (tx_flags & I40E_TX_FLAGS_TSO)
  1683. return 0;
  1684. /* only timestamp the outbound packet if the user has requested it and
  1685. * we are not already transmitting a packet to be timestamped
  1686. */
  1687. pf = i40e_netdev_to_pf(tx_ring->netdev);
  1688. if (!(pf->flags & I40E_FLAG_PTP))
  1689. return 0;
  1690. if (pf->ptp_tx &&
  1691. !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, &pf->state)) {
  1692. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1693. pf->ptp_tx_skb = skb_get(skb);
  1694. } else {
  1695. return 0;
  1696. }
  1697. *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
  1698. I40E_TXD_CTX_QW1_CMD_SHIFT;
  1699. return 1;
  1700. }
  1701. /**
  1702. * i40e_tx_enable_csum - Enable Tx checksum offloads
  1703. * @skb: send buffer
  1704. * @tx_flags: Tx flags currently set
  1705. * @td_cmd: Tx descriptor command bits to set
  1706. * @td_offset: Tx descriptor header offsets to set
  1707. * @cd_tunneling: ptr to context desc bits
  1708. **/
  1709. static void i40e_tx_enable_csum(struct sk_buff *skb, u32 tx_flags,
  1710. u32 *td_cmd, u32 *td_offset,
  1711. struct i40e_ring *tx_ring,
  1712. u32 *cd_tunneling)
  1713. {
  1714. struct ipv6hdr *this_ipv6_hdr;
  1715. unsigned int this_tcp_hdrlen;
  1716. struct iphdr *this_ip_hdr;
  1717. u32 network_hdr_len;
  1718. u8 l4_hdr = 0;
  1719. if (skb->encapsulation) {
  1720. network_hdr_len = skb_inner_network_header_len(skb);
  1721. this_ip_hdr = inner_ip_hdr(skb);
  1722. this_ipv6_hdr = inner_ipv6_hdr(skb);
  1723. this_tcp_hdrlen = inner_tcp_hdrlen(skb);
  1724. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1725. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1726. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV4;
  1727. ip_hdr(skb)->check = 0;
  1728. } else {
  1729. *cd_tunneling |=
  1730. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  1731. }
  1732. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1733. *cd_tunneling |= I40E_TX_CTX_EXT_IP_IPV6;
  1734. if (tx_flags & I40E_TX_FLAGS_TSO)
  1735. ip_hdr(skb)->check = 0;
  1736. }
  1737. /* Now set the ctx descriptor fields */
  1738. *cd_tunneling |= (skb_network_header_len(skb) >> 2) <<
  1739. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT |
  1740. I40E_TXD_CTX_UDP_TUNNELING |
  1741. ((skb_inner_network_offset(skb) -
  1742. skb_transport_offset(skb)) >> 1) <<
  1743. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  1744. if (this_ip_hdr->version == 6) {
  1745. tx_flags &= ~I40E_TX_FLAGS_IPV4;
  1746. tx_flags |= I40E_TX_FLAGS_IPV6;
  1747. }
  1748. } else {
  1749. network_hdr_len = skb_network_header_len(skb);
  1750. this_ip_hdr = ip_hdr(skb);
  1751. this_ipv6_hdr = ipv6_hdr(skb);
  1752. this_tcp_hdrlen = tcp_hdrlen(skb);
  1753. }
  1754. /* Enable IP checksum offloads */
  1755. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1756. l4_hdr = this_ip_hdr->protocol;
  1757. /* the stack computes the IP header already, the only time we
  1758. * need the hardware to recompute it is in the case of TSO.
  1759. */
  1760. if (tx_flags & I40E_TX_FLAGS_TSO) {
  1761. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4_CSUM;
  1762. this_ip_hdr->check = 0;
  1763. } else {
  1764. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV4;
  1765. }
  1766. /* Now set the td_offset for IP header length */
  1767. *td_offset = (network_hdr_len >> 2) <<
  1768. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1769. } else if (tx_flags & I40E_TX_FLAGS_IPV6) {
  1770. l4_hdr = this_ipv6_hdr->nexthdr;
  1771. *td_cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  1772. /* Now set the td_offset for IP header length */
  1773. *td_offset = (network_hdr_len >> 2) <<
  1774. I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  1775. }
  1776. /* words in MACLEN + dwords in IPLEN + dwords in L4Len */
  1777. *td_offset |= (skb_network_offset(skb) >> 1) <<
  1778. I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  1779. /* Enable L4 checksum offloads */
  1780. switch (l4_hdr) {
  1781. case IPPROTO_TCP:
  1782. /* enable checksum offloads */
  1783. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  1784. *td_offset |= (this_tcp_hdrlen >> 2) <<
  1785. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1786. break;
  1787. case IPPROTO_SCTP:
  1788. /* enable SCTP checksum offload */
  1789. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  1790. *td_offset |= (sizeof(struct sctphdr) >> 2) <<
  1791. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1792. break;
  1793. case IPPROTO_UDP:
  1794. /* enable UDP checksum offload */
  1795. *td_cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  1796. *td_offset |= (sizeof(struct udphdr) >> 2) <<
  1797. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  1798. break;
  1799. default:
  1800. break;
  1801. }
  1802. }
  1803. /**
  1804. * i40e_create_tx_ctx Build the Tx context descriptor
  1805. * @tx_ring: ring to create the descriptor on
  1806. * @cd_type_cmd_tso_mss: Quad Word 1
  1807. * @cd_tunneling: Quad Word 0 - bits 0-31
  1808. * @cd_l2tag2: Quad Word 0 - bits 32-63
  1809. **/
  1810. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  1811. const u64 cd_type_cmd_tso_mss,
  1812. const u32 cd_tunneling, const u32 cd_l2tag2)
  1813. {
  1814. struct i40e_tx_context_desc *context_desc;
  1815. int i = tx_ring->next_to_use;
  1816. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  1817. !cd_tunneling && !cd_l2tag2)
  1818. return;
  1819. /* grab the next descriptor */
  1820. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  1821. i++;
  1822. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  1823. /* cpu_to_le32 and assign to struct fields */
  1824. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  1825. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  1826. context_desc->rsvd = cpu_to_le16(0);
  1827. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  1828. }
  1829. /**
  1830. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  1831. * @tx_ring: the ring to be checked
  1832. * @size: the size buffer we want to assure is available
  1833. *
  1834. * Returns -EBUSY if a stop is needed, else 0
  1835. **/
  1836. static inline int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1837. {
  1838. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1839. /* Memory barrier before checking head and tail */
  1840. smp_mb();
  1841. /* Check again in a case another CPU has just made room available. */
  1842. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  1843. return -EBUSY;
  1844. /* A reprieve! - use start_queue because it doesn't call schedule */
  1845. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  1846. ++tx_ring->tx_stats.restart_queue;
  1847. return 0;
  1848. }
  1849. /**
  1850. * i40e_maybe_stop_tx - 1st level check for tx stop conditions
  1851. * @tx_ring: the ring to be checked
  1852. * @size: the size buffer we want to assure is available
  1853. *
  1854. * Returns 0 if stop is not needed
  1855. **/
  1856. #ifdef I40E_FCOE
  1857. int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1858. #else
  1859. static int i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  1860. #endif
  1861. {
  1862. if (likely(I40E_DESC_UNUSED(tx_ring) >= size))
  1863. return 0;
  1864. return __i40e_maybe_stop_tx(tx_ring, size);
  1865. }
  1866. /**
  1867. * i40e_chk_linearize - Check if there are more than 8 fragments per packet
  1868. * @skb: send buffer
  1869. * @tx_flags: collected send information
  1870. * @hdr_len: size of the packet header
  1871. *
  1872. * Note: Our HW can't scatter-gather more than 8 fragments to build
  1873. * a packet on the wire and so we need to figure out the cases where we
  1874. * need to linearize the skb.
  1875. **/
  1876. static bool i40e_chk_linearize(struct sk_buff *skb, u32 tx_flags,
  1877. const u8 hdr_len)
  1878. {
  1879. struct skb_frag_struct *frag;
  1880. bool linearize = false;
  1881. unsigned int size = 0;
  1882. u16 num_frags;
  1883. u16 gso_segs;
  1884. num_frags = skb_shinfo(skb)->nr_frags;
  1885. gso_segs = skb_shinfo(skb)->gso_segs;
  1886. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO)) {
  1887. u16 j = 1;
  1888. if (num_frags < (I40E_MAX_BUFFER_TXD))
  1889. goto linearize_chk_done;
  1890. /* try the simple math, if we have too many frags per segment */
  1891. if (DIV_ROUND_UP((num_frags + gso_segs), gso_segs) >
  1892. I40E_MAX_BUFFER_TXD) {
  1893. linearize = true;
  1894. goto linearize_chk_done;
  1895. }
  1896. frag = &skb_shinfo(skb)->frags[0];
  1897. size = hdr_len;
  1898. /* we might still have more fragments per segment */
  1899. do {
  1900. size += skb_frag_size(frag);
  1901. frag++; j++;
  1902. if (j == I40E_MAX_BUFFER_TXD) {
  1903. if (size < skb_shinfo(skb)->gso_size) {
  1904. linearize = true;
  1905. break;
  1906. }
  1907. j = 1;
  1908. size -= skb_shinfo(skb)->gso_size;
  1909. if (size)
  1910. j++;
  1911. size += hdr_len;
  1912. }
  1913. num_frags--;
  1914. } while (num_frags);
  1915. } else {
  1916. if (num_frags >= I40E_MAX_BUFFER_TXD)
  1917. linearize = true;
  1918. }
  1919. linearize_chk_done:
  1920. return linearize;
  1921. }
  1922. /**
  1923. * i40e_tx_map - Build the Tx descriptor
  1924. * @tx_ring: ring to send buffer on
  1925. * @skb: send buffer
  1926. * @first: first buffer info buffer to use
  1927. * @tx_flags: collected send information
  1928. * @hdr_len: size of the packet header
  1929. * @td_cmd: the command field in the descriptor
  1930. * @td_offset: offset for checksum or crc
  1931. **/
  1932. #ifdef I40E_FCOE
  1933. void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1934. struct i40e_tx_buffer *first, u32 tx_flags,
  1935. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1936. #else
  1937. static void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1938. struct i40e_tx_buffer *first, u32 tx_flags,
  1939. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  1940. #endif
  1941. {
  1942. unsigned int data_len = skb->data_len;
  1943. unsigned int size = skb_headlen(skb);
  1944. struct skb_frag_struct *frag;
  1945. struct i40e_tx_buffer *tx_bi;
  1946. struct i40e_tx_desc *tx_desc;
  1947. u16 i = tx_ring->next_to_use;
  1948. u32 td_tag = 0;
  1949. dma_addr_t dma;
  1950. u16 gso_segs;
  1951. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  1952. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  1953. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  1954. I40E_TX_FLAGS_VLAN_SHIFT;
  1955. }
  1956. if (tx_flags & (I40E_TX_FLAGS_TSO | I40E_TX_FLAGS_FSO))
  1957. gso_segs = skb_shinfo(skb)->gso_segs;
  1958. else
  1959. gso_segs = 1;
  1960. /* multiply data chunks by size of headers */
  1961. first->bytecount = skb->len - hdr_len + (gso_segs * hdr_len);
  1962. first->gso_segs = gso_segs;
  1963. first->skb = skb;
  1964. first->tx_flags = tx_flags;
  1965. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  1966. tx_desc = I40E_TX_DESC(tx_ring, i);
  1967. tx_bi = first;
  1968. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  1969. if (dma_mapping_error(tx_ring->dev, dma))
  1970. goto dma_error;
  1971. /* record length, and DMA address */
  1972. dma_unmap_len_set(tx_bi, len, size);
  1973. dma_unmap_addr_set(tx_bi, dma, dma);
  1974. tx_desc->buffer_addr = cpu_to_le64(dma);
  1975. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  1976. tx_desc->cmd_type_offset_bsz =
  1977. build_ctob(td_cmd, td_offset,
  1978. I40E_MAX_DATA_PER_TXD, td_tag);
  1979. tx_desc++;
  1980. i++;
  1981. if (i == tx_ring->count) {
  1982. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1983. i = 0;
  1984. }
  1985. dma += I40E_MAX_DATA_PER_TXD;
  1986. size -= I40E_MAX_DATA_PER_TXD;
  1987. tx_desc->buffer_addr = cpu_to_le64(dma);
  1988. }
  1989. if (likely(!data_len))
  1990. break;
  1991. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  1992. size, td_tag);
  1993. tx_desc++;
  1994. i++;
  1995. if (i == tx_ring->count) {
  1996. tx_desc = I40E_TX_DESC(tx_ring, 0);
  1997. i = 0;
  1998. }
  1999. size = skb_frag_size(frag);
  2000. data_len -= size;
  2001. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2002. DMA_TO_DEVICE);
  2003. tx_bi = &tx_ring->tx_bi[i];
  2004. }
  2005. /* Place RS bit on last descriptor of any packet that spans across the
  2006. * 4th descriptor (WB_STRIDE aka 0x3) in a 64B cacheline.
  2007. */
  2008. if (((i & WB_STRIDE) != WB_STRIDE) &&
  2009. (first <= &tx_ring->tx_bi[i]) &&
  2010. (first >= &tx_ring->tx_bi[i & ~WB_STRIDE])) {
  2011. tx_desc->cmd_type_offset_bsz =
  2012. build_ctob(td_cmd, td_offset, size, td_tag) |
  2013. cpu_to_le64((u64)I40E_TX_DESC_CMD_EOP <<
  2014. I40E_TXD_QW1_CMD_SHIFT);
  2015. } else {
  2016. tx_desc->cmd_type_offset_bsz =
  2017. build_ctob(td_cmd, td_offset, size, td_tag) |
  2018. cpu_to_le64((u64)I40E_TXD_CMD <<
  2019. I40E_TXD_QW1_CMD_SHIFT);
  2020. }
  2021. netdev_tx_sent_queue(netdev_get_tx_queue(tx_ring->netdev,
  2022. tx_ring->queue_index),
  2023. first->bytecount);
  2024. /* set the timestamp */
  2025. first->time_stamp = jiffies;
  2026. /* Force memory writes to complete before letting h/w
  2027. * know there are new descriptors to fetch. (Only
  2028. * applicable for weak-ordered memory model archs,
  2029. * such as IA-64).
  2030. */
  2031. wmb();
  2032. /* set next_to_watch value indicating a packet is present */
  2033. first->next_to_watch = tx_desc;
  2034. i++;
  2035. if (i == tx_ring->count)
  2036. i = 0;
  2037. tx_ring->next_to_use = i;
  2038. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2039. /* notify HW of packet */
  2040. if (!skb->xmit_more ||
  2041. netif_xmit_stopped(netdev_get_tx_queue(tx_ring->netdev,
  2042. tx_ring->queue_index)))
  2043. writel(i, tx_ring->tail);
  2044. return;
  2045. dma_error:
  2046. dev_info(tx_ring->dev, "TX DMA map failed\n");
  2047. /* clear dma mappings for failed tx_bi map */
  2048. for (;;) {
  2049. tx_bi = &tx_ring->tx_bi[i];
  2050. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  2051. if (tx_bi == first)
  2052. break;
  2053. if (i == 0)
  2054. i = tx_ring->count;
  2055. i--;
  2056. }
  2057. tx_ring->next_to_use = i;
  2058. }
  2059. /**
  2060. * i40e_xmit_descriptor_count - calculate number of tx descriptors needed
  2061. * @skb: send buffer
  2062. * @tx_ring: ring to send buffer on
  2063. *
  2064. * Returns number of data descriptors needed for this skb. Returns 0 to indicate
  2065. * there is not enough descriptors available in this ring since we need at least
  2066. * one descriptor.
  2067. **/
  2068. #ifdef I40E_FCOE
  2069. int i40e_xmit_descriptor_count(struct sk_buff *skb,
  2070. struct i40e_ring *tx_ring)
  2071. #else
  2072. static int i40e_xmit_descriptor_count(struct sk_buff *skb,
  2073. struct i40e_ring *tx_ring)
  2074. #endif
  2075. {
  2076. unsigned int f;
  2077. int count = 0;
  2078. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  2079. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  2080. * + 4 desc gap to avoid the cache line where head is,
  2081. * + 1 desc for context descriptor,
  2082. * otherwise try next time
  2083. */
  2084. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  2085. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  2086. count += TXD_USE_COUNT(skb_headlen(skb));
  2087. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  2088. tx_ring->tx_stats.tx_busy++;
  2089. return 0;
  2090. }
  2091. return count;
  2092. }
  2093. /**
  2094. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  2095. * @skb: send buffer
  2096. * @tx_ring: ring to send buffer on
  2097. *
  2098. * Returns NETDEV_TX_OK if sent, else an error code
  2099. **/
  2100. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  2101. struct i40e_ring *tx_ring)
  2102. {
  2103. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  2104. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  2105. struct i40e_tx_buffer *first;
  2106. u32 td_offset = 0;
  2107. u32 tx_flags = 0;
  2108. __be16 protocol;
  2109. u32 td_cmd = 0;
  2110. u8 hdr_len = 0;
  2111. int tsyn;
  2112. int tso;
  2113. if (0 == i40e_xmit_descriptor_count(skb, tx_ring))
  2114. return NETDEV_TX_BUSY;
  2115. /* prepare the xmit flags */
  2116. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  2117. goto out_drop;
  2118. /* obtain protocol of skb */
  2119. protocol = vlan_get_protocol(skb);
  2120. /* record the location of the first descriptor for this packet */
  2121. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  2122. /* setup IPv4/IPv6 offloads */
  2123. if (protocol == htons(ETH_P_IP))
  2124. tx_flags |= I40E_TX_FLAGS_IPV4;
  2125. else if (protocol == htons(ETH_P_IPV6))
  2126. tx_flags |= I40E_TX_FLAGS_IPV6;
  2127. tso = i40e_tso(tx_ring, skb, tx_flags, protocol, &hdr_len,
  2128. &cd_type_cmd_tso_mss, &cd_tunneling);
  2129. if (tso < 0)
  2130. goto out_drop;
  2131. else if (tso)
  2132. tx_flags |= I40E_TX_FLAGS_TSO;
  2133. tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
  2134. if (tsyn)
  2135. tx_flags |= I40E_TX_FLAGS_TSYN;
  2136. if (i40e_chk_linearize(skb, tx_flags, hdr_len))
  2137. if (skb_linearize(skb))
  2138. goto out_drop;
  2139. skb_tx_timestamp(skb);
  2140. /* always enable CRC insertion offload */
  2141. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  2142. /* Always offload the checksum, since it's in the data descriptor */
  2143. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2144. tx_flags |= I40E_TX_FLAGS_CSUM;
  2145. i40e_tx_enable_csum(skb, tx_flags, &td_cmd, &td_offset,
  2146. tx_ring, &cd_tunneling);
  2147. }
  2148. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  2149. cd_tunneling, cd_l2tag2);
  2150. /* Add Flow Director ATR if it's enabled.
  2151. *
  2152. * NOTE: this must always be directly before the data descriptor.
  2153. */
  2154. i40e_atr(tx_ring, skb, tx_flags, protocol);
  2155. i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  2156. td_cmd, td_offset);
  2157. return NETDEV_TX_OK;
  2158. out_drop:
  2159. dev_kfree_skb_any(skb);
  2160. return NETDEV_TX_OK;
  2161. }
  2162. /**
  2163. * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  2164. * @skb: send buffer
  2165. * @netdev: network interface device structure
  2166. *
  2167. * Returns NETDEV_TX_OK if sent, else an error code
  2168. **/
  2169. netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2170. {
  2171. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2172. struct i40e_vsi *vsi = np->vsi;
  2173. struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
  2174. /* hardware can't handle really short frames, hardware padding works
  2175. * beyond this point
  2176. */
  2177. if (skb_put_padto(skb, I40E_MIN_TX_LEN))
  2178. return NETDEV_TX_OK;
  2179. return i40e_xmit_frame_ring(skb, tx_ring);
  2180. }