i40e_txrx.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 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 <net/busy_poll.h>
  28. #include "i40e.h"
  29. #include "i40e_trace.h"
  30. #include "i40e_prototype.h"
  31. static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size,
  32. u32 td_tag)
  33. {
  34. return cpu_to_le64(I40E_TX_DESC_DTYPE_DATA |
  35. ((u64)td_cmd << I40E_TXD_QW1_CMD_SHIFT) |
  36. ((u64)td_offset << I40E_TXD_QW1_OFFSET_SHIFT) |
  37. ((u64)size << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
  38. ((u64)td_tag << I40E_TXD_QW1_L2TAG1_SHIFT));
  39. }
  40. #define I40E_TXD_CMD (I40E_TX_DESC_CMD_EOP | I40E_TX_DESC_CMD_RS)
  41. /**
  42. * i40e_fdir - Generate a Flow Director descriptor based on fdata
  43. * @tx_ring: Tx ring to send buffer on
  44. * @fdata: Flow director filter data
  45. * @add: Indicate if we are adding a rule or deleting one
  46. *
  47. **/
  48. static void i40e_fdir(struct i40e_ring *tx_ring,
  49. struct i40e_fdir_filter *fdata, bool add)
  50. {
  51. struct i40e_filter_program_desc *fdir_desc;
  52. struct i40e_pf *pf = tx_ring->vsi->back;
  53. u32 flex_ptype, dtype_cmd;
  54. u16 i;
  55. /* grab the next descriptor */
  56. i = tx_ring->next_to_use;
  57. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  58. i++;
  59. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  60. flex_ptype = I40E_TXD_FLTR_QW0_QINDEX_MASK &
  61. (fdata->q_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT);
  62. flex_ptype |= I40E_TXD_FLTR_QW0_FLEXOFF_MASK &
  63. (fdata->flex_off << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  64. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  65. (fdata->pctype << I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  66. flex_ptype |= I40E_TXD_FLTR_QW0_PCTYPE_MASK &
  67. (fdata->flex_offset << I40E_TXD_FLTR_QW0_FLEXOFF_SHIFT);
  68. /* Use LAN VSI Id if not programmed by user */
  69. flex_ptype |= I40E_TXD_FLTR_QW0_DEST_VSI_MASK &
  70. ((u32)(fdata->dest_vsi ? : pf->vsi[pf->lan_vsi]->id) <<
  71. I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT);
  72. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  73. dtype_cmd |= add ?
  74. I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  75. I40E_TXD_FLTR_QW1_PCMD_SHIFT :
  76. I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  77. I40E_TXD_FLTR_QW1_PCMD_SHIFT;
  78. dtype_cmd |= I40E_TXD_FLTR_QW1_DEST_MASK &
  79. (fdata->dest_ctl << I40E_TXD_FLTR_QW1_DEST_SHIFT);
  80. dtype_cmd |= I40E_TXD_FLTR_QW1_FD_STATUS_MASK &
  81. (fdata->fd_status << I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT);
  82. if (fdata->cnt_index) {
  83. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  84. dtype_cmd |= I40E_TXD_FLTR_QW1_CNTINDEX_MASK &
  85. ((u32)fdata->cnt_index <<
  86. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT);
  87. }
  88. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  89. fdir_desc->rsvd = cpu_to_le32(0);
  90. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  91. fdir_desc->fd_id = cpu_to_le32(fdata->fd_id);
  92. }
  93. #define I40E_FD_CLEAN_DELAY 10
  94. /**
  95. * i40e_program_fdir_filter - Program a Flow Director filter
  96. * @fdir_data: Packet data that will be filter parameters
  97. * @raw_packet: the pre-allocated packet buffer for FDir
  98. * @pf: The PF pointer
  99. * @add: True for add/update, False for remove
  100. **/
  101. static int i40e_program_fdir_filter(struct i40e_fdir_filter *fdir_data,
  102. u8 *raw_packet, struct i40e_pf *pf,
  103. bool add)
  104. {
  105. struct i40e_tx_buffer *tx_buf, *first;
  106. struct i40e_tx_desc *tx_desc;
  107. struct i40e_ring *tx_ring;
  108. struct i40e_vsi *vsi;
  109. struct device *dev;
  110. dma_addr_t dma;
  111. u32 td_cmd = 0;
  112. u16 i;
  113. /* find existing FDIR VSI */
  114. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  115. if (!vsi)
  116. return -ENOENT;
  117. tx_ring = vsi->tx_rings[0];
  118. dev = tx_ring->dev;
  119. /* we need two descriptors to add/del a filter and we can wait */
  120. for (i = I40E_FD_CLEAN_DELAY; I40E_DESC_UNUSED(tx_ring) < 2; i--) {
  121. if (!i)
  122. return -EAGAIN;
  123. msleep_interruptible(1);
  124. }
  125. dma = dma_map_single(dev, raw_packet,
  126. I40E_FDIR_MAX_RAW_PACKET_SIZE, DMA_TO_DEVICE);
  127. if (dma_mapping_error(dev, dma))
  128. goto dma_fail;
  129. /* grab the next descriptor */
  130. i = tx_ring->next_to_use;
  131. first = &tx_ring->tx_bi[i];
  132. i40e_fdir(tx_ring, fdir_data, add);
  133. /* Now program a dummy descriptor */
  134. i = tx_ring->next_to_use;
  135. tx_desc = I40E_TX_DESC(tx_ring, i);
  136. tx_buf = &tx_ring->tx_bi[i];
  137. tx_ring->next_to_use = ((i + 1) < tx_ring->count) ? i + 1 : 0;
  138. memset(tx_buf, 0, sizeof(struct i40e_tx_buffer));
  139. /* record length, and DMA address */
  140. dma_unmap_len_set(tx_buf, len, I40E_FDIR_MAX_RAW_PACKET_SIZE);
  141. dma_unmap_addr_set(tx_buf, dma, dma);
  142. tx_desc->buffer_addr = cpu_to_le64(dma);
  143. td_cmd = I40E_TXD_CMD | I40E_TX_DESC_CMD_DUMMY;
  144. tx_buf->tx_flags = I40E_TX_FLAGS_FD_SB;
  145. tx_buf->raw_buf = (void *)raw_packet;
  146. tx_desc->cmd_type_offset_bsz =
  147. build_ctob(td_cmd, 0, I40E_FDIR_MAX_RAW_PACKET_SIZE, 0);
  148. /* Force memory writes to complete before letting h/w
  149. * know there are new descriptors to fetch.
  150. */
  151. wmb();
  152. /* Mark the data descriptor to be watched */
  153. first->next_to_watch = tx_desc;
  154. writel(tx_ring->next_to_use, tx_ring->tail);
  155. return 0;
  156. dma_fail:
  157. return -1;
  158. }
  159. #define IP_HEADER_OFFSET 14
  160. #define I40E_UDPIP_DUMMY_PACKET_LEN 42
  161. /**
  162. * i40e_add_del_fdir_udpv4 - Add/Remove UDPv4 filters
  163. * @vsi: pointer to the targeted VSI
  164. * @fd_data: the flow director data required for the FDir descriptor
  165. * @add: true adds a filter, false removes it
  166. *
  167. * Returns 0 if the filters were successfully added or removed
  168. **/
  169. static int i40e_add_del_fdir_udpv4(struct i40e_vsi *vsi,
  170. struct i40e_fdir_filter *fd_data,
  171. bool add)
  172. {
  173. struct i40e_pf *pf = vsi->back;
  174. struct udphdr *udp;
  175. struct iphdr *ip;
  176. u8 *raw_packet;
  177. int ret;
  178. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  179. 0x45, 0, 0, 0x1c, 0, 0, 0x40, 0, 0x40, 0x11, 0, 0, 0, 0, 0, 0,
  180. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  181. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  182. if (!raw_packet)
  183. return -ENOMEM;
  184. memcpy(raw_packet, packet, I40E_UDPIP_DUMMY_PACKET_LEN);
  185. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  186. udp = (struct udphdr *)(raw_packet + IP_HEADER_OFFSET
  187. + sizeof(struct iphdr));
  188. ip->daddr = fd_data->dst_ip;
  189. udp->dest = fd_data->dst_port;
  190. ip->saddr = fd_data->src_ip;
  191. udp->source = fd_data->src_port;
  192. if (fd_data->flex_filter) {
  193. u8 *payload = raw_packet + I40E_UDPIP_DUMMY_PACKET_LEN;
  194. __be16 pattern = fd_data->flex_word;
  195. u16 off = fd_data->flex_offset;
  196. *((__force __be16 *)(payload + off)) = pattern;
  197. }
  198. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  199. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  200. if (ret) {
  201. dev_info(&pf->pdev->dev,
  202. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  203. fd_data->pctype, fd_data->fd_id, ret);
  204. /* Free the packet buffer since it wasn't added to the ring */
  205. kfree(raw_packet);
  206. return -EOPNOTSUPP;
  207. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  208. if (add)
  209. dev_info(&pf->pdev->dev,
  210. "Filter OK for PCTYPE %d loc = %d\n",
  211. fd_data->pctype, fd_data->fd_id);
  212. else
  213. dev_info(&pf->pdev->dev,
  214. "Filter deleted for PCTYPE %d loc = %d\n",
  215. fd_data->pctype, fd_data->fd_id);
  216. }
  217. if (add)
  218. pf->fd_udp4_filter_cnt++;
  219. else
  220. pf->fd_udp4_filter_cnt--;
  221. return 0;
  222. }
  223. #define I40E_TCPIP_DUMMY_PACKET_LEN 54
  224. /**
  225. * i40e_add_del_fdir_tcpv4 - Add/Remove TCPv4 filters
  226. * @vsi: pointer to the targeted VSI
  227. * @fd_data: the flow director data required for the FDir descriptor
  228. * @add: true adds a filter, false removes it
  229. *
  230. * Returns 0 if the filters were successfully added or removed
  231. **/
  232. static int i40e_add_del_fdir_tcpv4(struct i40e_vsi *vsi,
  233. struct i40e_fdir_filter *fd_data,
  234. bool add)
  235. {
  236. struct i40e_pf *pf = vsi->back;
  237. struct tcphdr *tcp;
  238. struct iphdr *ip;
  239. u8 *raw_packet;
  240. int ret;
  241. /* Dummy packet */
  242. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  243. 0x45, 0, 0, 0x28, 0, 0, 0x40, 0, 0x40, 0x6, 0, 0, 0, 0, 0, 0,
  244. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0x11,
  245. 0x0, 0x72, 0, 0, 0, 0};
  246. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  247. if (!raw_packet)
  248. return -ENOMEM;
  249. memcpy(raw_packet, packet, I40E_TCPIP_DUMMY_PACKET_LEN);
  250. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  251. tcp = (struct tcphdr *)(raw_packet + IP_HEADER_OFFSET
  252. + sizeof(struct iphdr));
  253. ip->daddr = fd_data->dst_ip;
  254. tcp->dest = fd_data->dst_port;
  255. ip->saddr = fd_data->src_ip;
  256. tcp->source = fd_data->src_port;
  257. if (fd_data->flex_filter) {
  258. u8 *payload = raw_packet + I40E_TCPIP_DUMMY_PACKET_LEN;
  259. __be16 pattern = fd_data->flex_word;
  260. u16 off = fd_data->flex_offset;
  261. *((__force __be16 *)(payload + off)) = pattern;
  262. }
  263. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  264. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  265. if (ret) {
  266. dev_info(&pf->pdev->dev,
  267. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  268. fd_data->pctype, fd_data->fd_id, ret);
  269. /* Free the packet buffer since it wasn't added to the ring */
  270. kfree(raw_packet);
  271. return -EOPNOTSUPP;
  272. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  273. if (add)
  274. dev_info(&pf->pdev->dev, "Filter OK for PCTYPE %d loc = %d)\n",
  275. fd_data->pctype, fd_data->fd_id);
  276. else
  277. dev_info(&pf->pdev->dev,
  278. "Filter deleted for PCTYPE %d loc = %d\n",
  279. fd_data->pctype, fd_data->fd_id);
  280. }
  281. if (add) {
  282. pf->fd_tcp4_filter_cnt++;
  283. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  284. I40E_DEBUG_FD & pf->hw.debug_mask)
  285. dev_info(&pf->pdev->dev, "Forcing ATR off, sideband rules for TCP/IPv4 flow being applied\n");
  286. pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
  287. } else {
  288. pf->fd_tcp4_filter_cnt--;
  289. }
  290. return 0;
  291. }
  292. #define I40E_SCTPIP_DUMMY_PACKET_LEN 46
  293. /**
  294. * i40e_add_del_fdir_sctpv4 - Add/Remove SCTPv4 Flow Director filters for
  295. * a specific flow spec
  296. * @vsi: pointer to the targeted VSI
  297. * @fd_data: the flow director data required for the FDir descriptor
  298. * @add: true adds a filter, false removes it
  299. *
  300. * Returns 0 if the filters were successfully added or removed
  301. **/
  302. static int i40e_add_del_fdir_sctpv4(struct i40e_vsi *vsi,
  303. struct i40e_fdir_filter *fd_data,
  304. bool add)
  305. {
  306. struct i40e_pf *pf = vsi->back;
  307. struct sctphdr *sctp;
  308. struct iphdr *ip;
  309. u8 *raw_packet;
  310. int ret;
  311. /* Dummy packet */
  312. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  313. 0x45, 0, 0, 0x20, 0, 0, 0x40, 0, 0x40, 0x84, 0, 0, 0, 0, 0, 0,
  314. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  315. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  316. if (!raw_packet)
  317. return -ENOMEM;
  318. memcpy(raw_packet, packet, I40E_SCTPIP_DUMMY_PACKET_LEN);
  319. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  320. sctp = (struct sctphdr *)(raw_packet + IP_HEADER_OFFSET
  321. + sizeof(struct iphdr));
  322. ip->daddr = fd_data->dst_ip;
  323. sctp->dest = fd_data->dst_port;
  324. ip->saddr = fd_data->src_ip;
  325. sctp->source = fd_data->src_port;
  326. if (fd_data->flex_filter) {
  327. u8 *payload = raw_packet + I40E_SCTPIP_DUMMY_PACKET_LEN;
  328. __be16 pattern = fd_data->flex_word;
  329. u16 off = fd_data->flex_offset;
  330. *((__force __be16 *)(payload + off)) = pattern;
  331. }
  332. fd_data->pctype = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
  333. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  334. if (ret) {
  335. dev_info(&pf->pdev->dev,
  336. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  337. fd_data->pctype, fd_data->fd_id, ret);
  338. /* Free the packet buffer since it wasn't added to the ring */
  339. kfree(raw_packet);
  340. return -EOPNOTSUPP;
  341. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  342. if (add)
  343. dev_info(&pf->pdev->dev,
  344. "Filter OK for PCTYPE %d loc = %d\n",
  345. fd_data->pctype, fd_data->fd_id);
  346. else
  347. dev_info(&pf->pdev->dev,
  348. "Filter deleted for PCTYPE %d loc = %d\n",
  349. fd_data->pctype, fd_data->fd_id);
  350. }
  351. if (add)
  352. pf->fd_sctp4_filter_cnt++;
  353. else
  354. pf->fd_sctp4_filter_cnt--;
  355. return 0;
  356. }
  357. #define I40E_IP_DUMMY_PACKET_LEN 34
  358. /**
  359. * i40e_add_del_fdir_ipv4 - Add/Remove IPv4 Flow Director filters for
  360. * a specific flow spec
  361. * @vsi: pointer to the targeted VSI
  362. * @fd_data: the flow director data required for the FDir descriptor
  363. * @add: true adds a filter, false removes it
  364. *
  365. * Returns 0 if the filters were successfully added or removed
  366. **/
  367. static int i40e_add_del_fdir_ipv4(struct i40e_vsi *vsi,
  368. struct i40e_fdir_filter *fd_data,
  369. bool add)
  370. {
  371. struct i40e_pf *pf = vsi->back;
  372. struct iphdr *ip;
  373. u8 *raw_packet;
  374. int ret;
  375. int i;
  376. static char packet[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0,
  377. 0x45, 0, 0, 0x14, 0, 0, 0x40, 0, 0x40, 0x10, 0, 0, 0, 0, 0, 0,
  378. 0, 0, 0, 0};
  379. for (i = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  380. i <= I40E_FILTER_PCTYPE_FRAG_IPV4; i++) {
  381. raw_packet = kzalloc(I40E_FDIR_MAX_RAW_PACKET_SIZE, GFP_KERNEL);
  382. if (!raw_packet)
  383. return -ENOMEM;
  384. memcpy(raw_packet, packet, I40E_IP_DUMMY_PACKET_LEN);
  385. ip = (struct iphdr *)(raw_packet + IP_HEADER_OFFSET);
  386. ip->saddr = fd_data->src_ip;
  387. ip->daddr = fd_data->dst_ip;
  388. ip->protocol = 0;
  389. if (fd_data->flex_filter) {
  390. u8 *payload = raw_packet + I40E_IP_DUMMY_PACKET_LEN;
  391. __be16 pattern = fd_data->flex_word;
  392. u16 off = fd_data->flex_offset;
  393. *((__force __be16 *)(payload + off)) = pattern;
  394. }
  395. fd_data->pctype = i;
  396. ret = i40e_program_fdir_filter(fd_data, raw_packet, pf, add);
  397. if (ret) {
  398. dev_info(&pf->pdev->dev,
  399. "PCTYPE:%d, Filter command send failed for fd_id:%d (ret = %d)\n",
  400. fd_data->pctype, fd_data->fd_id, ret);
  401. /* The packet buffer wasn't added to the ring so we
  402. * need to free it now.
  403. */
  404. kfree(raw_packet);
  405. return -EOPNOTSUPP;
  406. } else if (I40E_DEBUG_FD & pf->hw.debug_mask) {
  407. if (add)
  408. dev_info(&pf->pdev->dev,
  409. "Filter OK for PCTYPE %d loc = %d\n",
  410. fd_data->pctype, fd_data->fd_id);
  411. else
  412. dev_info(&pf->pdev->dev,
  413. "Filter deleted for PCTYPE %d loc = %d\n",
  414. fd_data->pctype, fd_data->fd_id);
  415. }
  416. }
  417. if (add)
  418. pf->fd_ip4_filter_cnt++;
  419. else
  420. pf->fd_ip4_filter_cnt--;
  421. return 0;
  422. }
  423. /**
  424. * i40e_add_del_fdir - Build raw packets to add/del fdir filter
  425. * @vsi: pointer to the targeted VSI
  426. * @cmd: command to get or set RX flow classification rules
  427. * @add: true adds a filter, false removes it
  428. *
  429. **/
  430. int i40e_add_del_fdir(struct i40e_vsi *vsi,
  431. struct i40e_fdir_filter *input, bool add)
  432. {
  433. struct i40e_pf *pf = vsi->back;
  434. int ret;
  435. switch (input->flow_type & ~FLOW_EXT) {
  436. case TCP_V4_FLOW:
  437. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  438. break;
  439. case UDP_V4_FLOW:
  440. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  441. break;
  442. case SCTP_V4_FLOW:
  443. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  444. break;
  445. case IP_USER_FLOW:
  446. switch (input->ip4_proto) {
  447. case IPPROTO_TCP:
  448. ret = i40e_add_del_fdir_tcpv4(vsi, input, add);
  449. break;
  450. case IPPROTO_UDP:
  451. ret = i40e_add_del_fdir_udpv4(vsi, input, add);
  452. break;
  453. case IPPROTO_SCTP:
  454. ret = i40e_add_del_fdir_sctpv4(vsi, input, add);
  455. break;
  456. case IPPROTO_IP:
  457. ret = i40e_add_del_fdir_ipv4(vsi, input, add);
  458. break;
  459. default:
  460. /* We cannot support masking based on protocol */
  461. dev_info(&pf->pdev->dev, "Unsupported IPv4 protocol 0x%02x\n",
  462. input->ip4_proto);
  463. return -EINVAL;
  464. }
  465. break;
  466. default:
  467. dev_info(&pf->pdev->dev, "Unsupported flow type 0x%02x\n",
  468. input->flow_type);
  469. return -EINVAL;
  470. }
  471. /* The buffer allocated here will be normally be freed by
  472. * i40e_clean_fdir_tx_irq() as it reclaims resources after transmit
  473. * completion. In the event of an error adding the buffer to the FDIR
  474. * ring, it will immediately be freed. It may also be freed by
  475. * i40e_clean_tx_ring() when closing the VSI.
  476. */
  477. return ret;
  478. }
  479. /**
  480. * i40e_fd_handle_status - check the Programming Status for FD
  481. * @rx_ring: the Rx ring for this descriptor
  482. * @rx_desc: the Rx descriptor for programming Status, not a packet descriptor.
  483. * @prog_id: the id originally used for programming
  484. *
  485. * This is used to verify if the FD programming or invalidation
  486. * requested by SW to the HW is successful or not and take actions accordingly.
  487. **/
  488. static void i40e_fd_handle_status(struct i40e_ring *rx_ring,
  489. union i40e_rx_desc *rx_desc, u8 prog_id)
  490. {
  491. struct i40e_pf *pf = rx_ring->vsi->back;
  492. struct pci_dev *pdev = pf->pdev;
  493. u32 fcnt_prog, fcnt_avail;
  494. u32 error;
  495. u64 qw;
  496. qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  497. error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
  498. I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
  499. if (error == BIT(I40E_RX_PROG_STATUS_DESC_FD_TBL_FULL_SHIFT)) {
  500. pf->fd_inv = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fd_id);
  501. if ((rx_desc->wb.qword0.hi_dword.fd_id != 0) ||
  502. (I40E_DEBUG_FD & pf->hw.debug_mask))
  503. dev_warn(&pdev->dev, "ntuple filter loc = %d, could not be added\n",
  504. pf->fd_inv);
  505. /* Check if the programming error is for ATR.
  506. * If so, auto disable ATR and set a state for
  507. * flush in progress. Next time we come here if flush is in
  508. * progress do nothing, once flush is complete the state will
  509. * be cleared.
  510. */
  511. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  512. return;
  513. pf->fd_add_err++;
  514. /* store the current atr filter count */
  515. pf->fd_atr_cnt = i40e_get_current_atr_cnt(pf);
  516. if ((rx_desc->wb.qword0.hi_dword.fd_id == 0) &&
  517. pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
  518. pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
  519. set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  520. }
  521. /* filter programming failed most likely due to table full */
  522. fcnt_prog = i40e_get_global_fd_count(pf);
  523. fcnt_avail = pf->fdir_pf_filter_count;
  524. /* If ATR is running fcnt_prog can quickly change,
  525. * if we are very close to full, it makes sense to disable
  526. * FD ATR/SB and then re-enable it when there is room.
  527. */
  528. if (fcnt_prog >= (fcnt_avail - I40E_FDIR_BUFFER_FULL_MARGIN)) {
  529. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  530. !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED)) {
  531. pf->flags |= I40E_FLAG_FD_SB_AUTO_DISABLED;
  532. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  533. dev_warn(&pdev->dev, "FD filter space full, new ntuple rules will not be added\n");
  534. }
  535. }
  536. } else if (error == BIT(I40E_RX_PROG_STATUS_DESC_NO_FD_ENTRY_SHIFT)) {
  537. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  538. dev_info(&pdev->dev, "ntuple filter fd_id = %d, could not be removed\n",
  539. rx_desc->wb.qword0.hi_dword.fd_id);
  540. }
  541. }
  542. /**
  543. * i40e_unmap_and_free_tx_resource - Release a Tx buffer
  544. * @ring: the ring that owns the buffer
  545. * @tx_buffer: the buffer to free
  546. **/
  547. static void i40e_unmap_and_free_tx_resource(struct i40e_ring *ring,
  548. struct i40e_tx_buffer *tx_buffer)
  549. {
  550. if (tx_buffer->skb) {
  551. if (tx_buffer->tx_flags & I40E_TX_FLAGS_FD_SB)
  552. kfree(tx_buffer->raw_buf);
  553. else
  554. dev_kfree_skb_any(tx_buffer->skb);
  555. if (dma_unmap_len(tx_buffer, len))
  556. dma_unmap_single(ring->dev,
  557. dma_unmap_addr(tx_buffer, dma),
  558. dma_unmap_len(tx_buffer, len),
  559. DMA_TO_DEVICE);
  560. } else if (dma_unmap_len(tx_buffer, len)) {
  561. dma_unmap_page(ring->dev,
  562. dma_unmap_addr(tx_buffer, dma),
  563. dma_unmap_len(tx_buffer, len),
  564. DMA_TO_DEVICE);
  565. }
  566. tx_buffer->next_to_watch = NULL;
  567. tx_buffer->skb = NULL;
  568. dma_unmap_len_set(tx_buffer, len, 0);
  569. /* tx_buffer must be completely set up in the transmit path */
  570. }
  571. /**
  572. * i40e_clean_tx_ring - Free any empty Tx buffers
  573. * @tx_ring: ring to be cleaned
  574. **/
  575. void i40e_clean_tx_ring(struct i40e_ring *tx_ring)
  576. {
  577. unsigned long bi_size;
  578. u16 i;
  579. /* ring already cleared, nothing to do */
  580. if (!tx_ring->tx_bi)
  581. return;
  582. /* Free all the Tx ring sk_buffs */
  583. for (i = 0; i < tx_ring->count; i++)
  584. i40e_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]);
  585. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  586. memset(tx_ring->tx_bi, 0, bi_size);
  587. /* Zero out the descriptor ring */
  588. memset(tx_ring->desc, 0, tx_ring->size);
  589. tx_ring->next_to_use = 0;
  590. tx_ring->next_to_clean = 0;
  591. if (!tx_ring->netdev)
  592. return;
  593. /* cleanup Tx queue statistics */
  594. netdev_tx_reset_queue(txring_txq(tx_ring));
  595. }
  596. /**
  597. * i40e_free_tx_resources - Free Tx resources per queue
  598. * @tx_ring: Tx descriptor ring for a specific queue
  599. *
  600. * Free all transmit software resources
  601. **/
  602. void i40e_free_tx_resources(struct i40e_ring *tx_ring)
  603. {
  604. i40e_clean_tx_ring(tx_ring);
  605. kfree(tx_ring->tx_bi);
  606. tx_ring->tx_bi = NULL;
  607. if (tx_ring->desc) {
  608. dma_free_coherent(tx_ring->dev, tx_ring->size,
  609. tx_ring->desc, tx_ring->dma);
  610. tx_ring->desc = NULL;
  611. }
  612. }
  613. /**
  614. * i40e_get_tx_pending - how many tx descriptors not processed
  615. * @tx_ring: the ring of descriptors
  616. *
  617. * Since there is no access to the ring head register
  618. * in XL710, we need to use our local copies
  619. **/
  620. u32 i40e_get_tx_pending(struct i40e_ring *ring)
  621. {
  622. u32 head, tail;
  623. head = i40e_get_head(ring);
  624. tail = readl(ring->tail);
  625. if (head != tail)
  626. return (head < tail) ?
  627. tail - head : (tail + ring->count - head);
  628. return 0;
  629. }
  630. #define WB_STRIDE 4
  631. /**
  632. * i40e_clean_tx_irq - Reclaim resources after transmit completes
  633. * @vsi: the VSI we care about
  634. * @tx_ring: Tx ring to clean
  635. * @napi_budget: Used to determine if we are in netpoll
  636. *
  637. * Returns true if there's any budget left (e.g. the clean is finished)
  638. **/
  639. static bool i40e_clean_tx_irq(struct i40e_vsi *vsi,
  640. struct i40e_ring *tx_ring, int napi_budget)
  641. {
  642. u16 i = tx_ring->next_to_clean;
  643. struct i40e_tx_buffer *tx_buf;
  644. struct i40e_tx_desc *tx_head;
  645. struct i40e_tx_desc *tx_desc;
  646. unsigned int total_bytes = 0, total_packets = 0;
  647. unsigned int budget = vsi->work_limit;
  648. tx_buf = &tx_ring->tx_bi[i];
  649. tx_desc = I40E_TX_DESC(tx_ring, i);
  650. i -= tx_ring->count;
  651. tx_head = I40E_TX_DESC(tx_ring, i40e_get_head(tx_ring));
  652. do {
  653. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  654. /* if next_to_watch is not set then there is no work pending */
  655. if (!eop_desc)
  656. break;
  657. /* prevent any other reads prior to eop_desc */
  658. read_barrier_depends();
  659. i40e_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf);
  660. /* we have caught up to head, no work left to do */
  661. if (tx_head == tx_desc)
  662. break;
  663. /* clear next_to_watch to prevent false hangs */
  664. tx_buf->next_to_watch = NULL;
  665. /* update the statistics for this packet */
  666. total_bytes += tx_buf->bytecount;
  667. total_packets += tx_buf->gso_segs;
  668. /* free the skb */
  669. napi_consume_skb(tx_buf->skb, napi_budget);
  670. /* unmap skb header data */
  671. dma_unmap_single(tx_ring->dev,
  672. dma_unmap_addr(tx_buf, dma),
  673. dma_unmap_len(tx_buf, len),
  674. DMA_TO_DEVICE);
  675. /* clear tx_buffer data */
  676. tx_buf->skb = NULL;
  677. dma_unmap_len_set(tx_buf, len, 0);
  678. /* unmap remaining buffers */
  679. while (tx_desc != eop_desc) {
  680. i40e_trace(clean_tx_irq_unmap,
  681. tx_ring, tx_desc, tx_buf);
  682. tx_buf++;
  683. tx_desc++;
  684. i++;
  685. if (unlikely(!i)) {
  686. i -= tx_ring->count;
  687. tx_buf = tx_ring->tx_bi;
  688. tx_desc = I40E_TX_DESC(tx_ring, 0);
  689. }
  690. /* unmap any remaining paged data */
  691. if (dma_unmap_len(tx_buf, len)) {
  692. dma_unmap_page(tx_ring->dev,
  693. dma_unmap_addr(tx_buf, dma),
  694. dma_unmap_len(tx_buf, len),
  695. DMA_TO_DEVICE);
  696. dma_unmap_len_set(tx_buf, len, 0);
  697. }
  698. }
  699. /* move us one more past the eop_desc for start of next pkt */
  700. tx_buf++;
  701. tx_desc++;
  702. i++;
  703. if (unlikely(!i)) {
  704. i -= tx_ring->count;
  705. tx_buf = tx_ring->tx_bi;
  706. tx_desc = I40E_TX_DESC(tx_ring, 0);
  707. }
  708. prefetch(tx_desc);
  709. /* update budget accounting */
  710. budget--;
  711. } while (likely(budget));
  712. i += tx_ring->count;
  713. tx_ring->next_to_clean = i;
  714. u64_stats_update_begin(&tx_ring->syncp);
  715. tx_ring->stats.bytes += total_bytes;
  716. tx_ring->stats.packets += total_packets;
  717. u64_stats_update_end(&tx_ring->syncp);
  718. tx_ring->q_vector->tx.total_bytes += total_bytes;
  719. tx_ring->q_vector->tx.total_packets += total_packets;
  720. if (tx_ring->flags & I40E_TXR_FLAGS_WB_ON_ITR) {
  721. /* check to see if there are < 4 descriptors
  722. * waiting to be written back, then kick the hardware to force
  723. * them to be written back in case we stay in NAPI.
  724. * In this mode on X722 we do not enable Interrupt.
  725. */
  726. unsigned int j = i40e_get_tx_pending(tx_ring);
  727. if (budget &&
  728. ((j / WB_STRIDE) == 0) && (j > 0) &&
  729. !test_bit(__I40E_VSI_DOWN, vsi->state) &&
  730. (I40E_DESC_UNUSED(tx_ring) != tx_ring->count))
  731. tx_ring->arm_wb = true;
  732. }
  733. /* notify netdev of completed buffers */
  734. netdev_tx_completed_queue(txring_txq(tx_ring),
  735. total_packets, total_bytes);
  736. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  737. if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) &&
  738. (I40E_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) {
  739. /* Make sure that anybody stopping the queue after this
  740. * sees the new next_to_clean.
  741. */
  742. smp_mb();
  743. if (__netif_subqueue_stopped(tx_ring->netdev,
  744. tx_ring->queue_index) &&
  745. !test_bit(__I40E_VSI_DOWN, vsi->state)) {
  746. netif_wake_subqueue(tx_ring->netdev,
  747. tx_ring->queue_index);
  748. ++tx_ring->tx_stats.restart_queue;
  749. }
  750. }
  751. return !!budget;
  752. }
  753. /**
  754. * i40e_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled
  755. * @vsi: the VSI we care about
  756. * @q_vector: the vector on which to enable writeback
  757. *
  758. **/
  759. static void i40e_enable_wb_on_itr(struct i40e_vsi *vsi,
  760. struct i40e_q_vector *q_vector)
  761. {
  762. u16 flags = q_vector->tx.ring[0].flags;
  763. u32 val;
  764. if (!(flags & I40E_TXR_FLAGS_WB_ON_ITR))
  765. return;
  766. if (q_vector->arm_wb_state)
  767. return;
  768. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  769. val = I40E_PFINT_DYN_CTLN_WB_ON_ITR_MASK |
  770. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK; /* set noitr */
  771. wr32(&vsi->back->hw,
  772. I40E_PFINT_DYN_CTLN(q_vector->v_idx + vsi->base_vector - 1),
  773. val);
  774. } else {
  775. val = I40E_PFINT_DYN_CTL0_WB_ON_ITR_MASK |
  776. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK; /* set noitr */
  777. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  778. }
  779. q_vector->arm_wb_state = true;
  780. }
  781. /**
  782. * i40e_force_wb - Issue SW Interrupt so HW does a wb
  783. * @vsi: the VSI we care about
  784. * @q_vector: the vector on which to force writeback
  785. *
  786. **/
  787. void i40e_force_wb(struct i40e_vsi *vsi, struct i40e_q_vector *q_vector)
  788. {
  789. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  790. u32 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  791. I40E_PFINT_DYN_CTLN_ITR_INDX_MASK | /* set noitr */
  792. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK |
  793. I40E_PFINT_DYN_CTLN_SW_ITR_INDX_ENA_MASK;
  794. /* allow 00 to be written to the index */
  795. wr32(&vsi->back->hw,
  796. I40E_PFINT_DYN_CTLN(q_vector->v_idx +
  797. vsi->base_vector - 1), val);
  798. } else {
  799. u32 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  800. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK | /* set noitr */
  801. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
  802. I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK;
  803. /* allow 00 to be written to the index */
  804. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0, val);
  805. }
  806. }
  807. /**
  808. * i40e_set_new_dynamic_itr - Find new ITR level
  809. * @rc: structure containing ring performance data
  810. *
  811. * Returns true if ITR changed, false if not
  812. *
  813. * Stores a new ITR value based on packets and byte counts during
  814. * the last interrupt. The advantage of per interrupt computation
  815. * is faster updates and more accurate ITR for the current traffic
  816. * pattern. Constants in this function were computed based on
  817. * theoretical maximum wire speed and thresholds were set based on
  818. * testing data as well as attempting to minimize response time
  819. * while increasing bulk throughput.
  820. **/
  821. static bool i40e_set_new_dynamic_itr(struct i40e_ring_container *rc)
  822. {
  823. enum i40e_latency_range new_latency_range = rc->latency_range;
  824. struct i40e_q_vector *qv = rc->ring->q_vector;
  825. u32 new_itr = rc->itr;
  826. int bytes_per_int;
  827. int usecs;
  828. if (rc->total_packets == 0 || !rc->itr)
  829. return false;
  830. /* simple throttlerate management
  831. * 0-10MB/s lowest (50000 ints/s)
  832. * 10-20MB/s low (20000 ints/s)
  833. * 20-1249MB/s bulk (18000 ints/s)
  834. * > 40000 Rx packets per second (8000 ints/s)
  835. *
  836. * The math works out because the divisor is in 10^(-6) which
  837. * turns the bytes/us input value into MB/s values, but
  838. * make sure to use usecs, as the register values written
  839. * are in 2 usec increments in the ITR registers, and make sure
  840. * to use the smoothed values that the countdown timer gives us.
  841. */
  842. usecs = (rc->itr << 1) * ITR_COUNTDOWN_START;
  843. bytes_per_int = rc->total_bytes / usecs;
  844. switch (new_latency_range) {
  845. case I40E_LOWEST_LATENCY:
  846. if (bytes_per_int > 10)
  847. new_latency_range = I40E_LOW_LATENCY;
  848. break;
  849. case I40E_LOW_LATENCY:
  850. if (bytes_per_int > 20)
  851. new_latency_range = I40E_BULK_LATENCY;
  852. else if (bytes_per_int <= 10)
  853. new_latency_range = I40E_LOWEST_LATENCY;
  854. break;
  855. case I40E_BULK_LATENCY:
  856. case I40E_ULTRA_LATENCY:
  857. default:
  858. if (bytes_per_int <= 20)
  859. new_latency_range = I40E_LOW_LATENCY;
  860. break;
  861. }
  862. /* this is to adjust RX more aggressively when streaming small
  863. * packets. The value of 40000 was picked as it is just beyond
  864. * what the hardware can receive per second if in low latency
  865. * mode.
  866. */
  867. #define RX_ULTRA_PACKET_RATE 40000
  868. if ((((rc->total_packets * 1000000) / usecs) > RX_ULTRA_PACKET_RATE) &&
  869. (&qv->rx == rc))
  870. new_latency_range = I40E_ULTRA_LATENCY;
  871. rc->latency_range = new_latency_range;
  872. switch (new_latency_range) {
  873. case I40E_LOWEST_LATENCY:
  874. new_itr = I40E_ITR_50K;
  875. break;
  876. case I40E_LOW_LATENCY:
  877. new_itr = I40E_ITR_20K;
  878. break;
  879. case I40E_BULK_LATENCY:
  880. new_itr = I40E_ITR_18K;
  881. break;
  882. case I40E_ULTRA_LATENCY:
  883. new_itr = I40E_ITR_8K;
  884. break;
  885. default:
  886. break;
  887. }
  888. rc->total_bytes = 0;
  889. rc->total_packets = 0;
  890. if (new_itr != rc->itr) {
  891. rc->itr = new_itr;
  892. return true;
  893. }
  894. return false;
  895. }
  896. /**
  897. * i40e_rx_is_programming_status - check for programming status descriptor
  898. * @qw: qword representing status_error_len in CPU ordering
  899. *
  900. * The value of in the descriptor length field indicate if this
  901. * is a programming status descriptor for flow director or FCoE
  902. * by the value of I40E_RX_PROG_STATUS_DESC_LENGTH, otherwise
  903. * it is a packet descriptor.
  904. **/
  905. static inline bool i40e_rx_is_programming_status(u64 qw)
  906. {
  907. /* The Rx filter programming status and SPH bit occupy the same
  908. * spot in the descriptor. Since we don't support packet split we
  909. * can just reuse the bit as an indication that this is a
  910. * programming status descriptor.
  911. */
  912. return qw & I40E_RXD_QW1_LENGTH_SPH_MASK;
  913. }
  914. /**
  915. * i40e_clean_programming_status - clean the programming status descriptor
  916. * @rx_ring: the rx ring that has this descriptor
  917. * @rx_desc: the rx descriptor written back by HW
  918. * @qw: qword representing status_error_len in CPU ordering
  919. *
  920. * Flow director should handle FD_FILTER_STATUS to check its filter programming
  921. * status being successful or not and take actions accordingly. FCoE should
  922. * handle its context/filter programming/invalidation status and take actions.
  923. *
  924. **/
  925. static void i40e_clean_programming_status(struct i40e_ring *rx_ring,
  926. union i40e_rx_desc *rx_desc,
  927. u64 qw)
  928. {
  929. u32 ntc = rx_ring->next_to_clean + 1;
  930. u8 id;
  931. /* fetch, update, and store next to clean */
  932. ntc = (ntc < rx_ring->count) ? ntc : 0;
  933. rx_ring->next_to_clean = ntc;
  934. prefetch(I40E_RX_DESC(rx_ring, ntc));
  935. id = (qw & I40E_RX_PROG_STATUS_DESC_QW1_PROGID_MASK) >>
  936. I40E_RX_PROG_STATUS_DESC_QW1_PROGID_SHIFT;
  937. if (id == I40E_RX_PROG_STATUS_DESC_FD_FILTER_STATUS)
  938. i40e_fd_handle_status(rx_ring, rx_desc, id);
  939. }
  940. /**
  941. * i40e_setup_tx_descriptors - Allocate the Tx descriptors
  942. * @tx_ring: the tx ring to set up
  943. *
  944. * Return 0 on success, negative on error
  945. **/
  946. int i40e_setup_tx_descriptors(struct i40e_ring *tx_ring)
  947. {
  948. struct device *dev = tx_ring->dev;
  949. int bi_size;
  950. if (!dev)
  951. return -ENOMEM;
  952. /* warn if we are about to overwrite the pointer */
  953. WARN_ON(tx_ring->tx_bi);
  954. bi_size = sizeof(struct i40e_tx_buffer) * tx_ring->count;
  955. tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL);
  956. if (!tx_ring->tx_bi)
  957. goto err;
  958. /* round up to nearest 4K */
  959. tx_ring->size = tx_ring->count * sizeof(struct i40e_tx_desc);
  960. /* add u32 for head writeback, align after this takes care of
  961. * guaranteeing this is at least one cache line in size
  962. */
  963. tx_ring->size += sizeof(u32);
  964. tx_ring->size = ALIGN(tx_ring->size, 4096);
  965. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  966. &tx_ring->dma, GFP_KERNEL);
  967. if (!tx_ring->desc) {
  968. dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n",
  969. tx_ring->size);
  970. goto err;
  971. }
  972. tx_ring->next_to_use = 0;
  973. tx_ring->next_to_clean = 0;
  974. return 0;
  975. err:
  976. kfree(tx_ring->tx_bi);
  977. tx_ring->tx_bi = NULL;
  978. return -ENOMEM;
  979. }
  980. /**
  981. * i40e_clean_rx_ring - Free Rx buffers
  982. * @rx_ring: ring to be cleaned
  983. **/
  984. void i40e_clean_rx_ring(struct i40e_ring *rx_ring)
  985. {
  986. unsigned long bi_size;
  987. u16 i;
  988. /* ring already cleared, nothing to do */
  989. if (!rx_ring->rx_bi)
  990. return;
  991. if (rx_ring->skb) {
  992. dev_kfree_skb(rx_ring->skb);
  993. rx_ring->skb = NULL;
  994. }
  995. /* Free all the Rx ring sk_buffs */
  996. for (i = 0; i < rx_ring->count; i++) {
  997. struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i];
  998. if (!rx_bi->page)
  999. continue;
  1000. /* Invalidate cache lines that may have been written to by
  1001. * device so that we avoid corrupting memory.
  1002. */
  1003. dma_sync_single_range_for_cpu(rx_ring->dev,
  1004. rx_bi->dma,
  1005. rx_bi->page_offset,
  1006. rx_ring->rx_buf_len,
  1007. DMA_FROM_DEVICE);
  1008. /* free resources associated with mapping */
  1009. dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma,
  1010. i40e_rx_pg_size(rx_ring),
  1011. DMA_FROM_DEVICE,
  1012. I40E_RX_DMA_ATTR);
  1013. __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias);
  1014. rx_bi->page = NULL;
  1015. rx_bi->page_offset = 0;
  1016. }
  1017. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1018. memset(rx_ring->rx_bi, 0, bi_size);
  1019. /* Zero out the descriptor ring */
  1020. memset(rx_ring->desc, 0, rx_ring->size);
  1021. rx_ring->next_to_alloc = 0;
  1022. rx_ring->next_to_clean = 0;
  1023. rx_ring->next_to_use = 0;
  1024. }
  1025. /**
  1026. * i40e_free_rx_resources - Free Rx resources
  1027. * @rx_ring: ring to clean the resources from
  1028. *
  1029. * Free all receive software resources
  1030. **/
  1031. void i40e_free_rx_resources(struct i40e_ring *rx_ring)
  1032. {
  1033. i40e_clean_rx_ring(rx_ring);
  1034. kfree(rx_ring->rx_bi);
  1035. rx_ring->rx_bi = NULL;
  1036. if (rx_ring->desc) {
  1037. dma_free_coherent(rx_ring->dev, rx_ring->size,
  1038. rx_ring->desc, rx_ring->dma);
  1039. rx_ring->desc = NULL;
  1040. }
  1041. }
  1042. /**
  1043. * i40e_setup_rx_descriptors - Allocate Rx descriptors
  1044. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  1045. *
  1046. * Returns 0 on success, negative on failure
  1047. **/
  1048. int i40e_setup_rx_descriptors(struct i40e_ring *rx_ring)
  1049. {
  1050. struct device *dev = rx_ring->dev;
  1051. int bi_size;
  1052. /* warn if we are about to overwrite the pointer */
  1053. WARN_ON(rx_ring->rx_bi);
  1054. bi_size = sizeof(struct i40e_rx_buffer) * rx_ring->count;
  1055. rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL);
  1056. if (!rx_ring->rx_bi)
  1057. goto err;
  1058. u64_stats_init(&rx_ring->syncp);
  1059. /* Round up to nearest 4K */
  1060. rx_ring->size = rx_ring->count * sizeof(union i40e_32byte_rx_desc);
  1061. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1062. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  1063. &rx_ring->dma, GFP_KERNEL);
  1064. if (!rx_ring->desc) {
  1065. dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n",
  1066. rx_ring->size);
  1067. goto err;
  1068. }
  1069. rx_ring->next_to_alloc = 0;
  1070. rx_ring->next_to_clean = 0;
  1071. rx_ring->next_to_use = 0;
  1072. return 0;
  1073. err:
  1074. kfree(rx_ring->rx_bi);
  1075. rx_ring->rx_bi = NULL;
  1076. return -ENOMEM;
  1077. }
  1078. /**
  1079. * i40e_release_rx_desc - Store the new tail and head values
  1080. * @rx_ring: ring to bump
  1081. * @val: new head index
  1082. **/
  1083. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  1084. {
  1085. rx_ring->next_to_use = val;
  1086. /* update next to alloc since we have filled the ring */
  1087. rx_ring->next_to_alloc = val;
  1088. /* Force memory writes to complete before letting h/w
  1089. * know there are new descriptors to fetch. (Only
  1090. * applicable for weak-ordered memory model archs,
  1091. * such as IA-64).
  1092. */
  1093. wmb();
  1094. writel(val, rx_ring->tail);
  1095. }
  1096. /**
  1097. * i40e_rx_offset - Return expected offset into page to access data
  1098. * @rx_ring: Ring we are requesting offset of
  1099. *
  1100. * Returns the offset value for ring into the data buffer.
  1101. */
  1102. static inline unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
  1103. {
  1104. return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
  1105. }
  1106. /**
  1107. * i40e_alloc_mapped_page - recycle or make a new page
  1108. * @rx_ring: ring to use
  1109. * @bi: rx_buffer struct to modify
  1110. *
  1111. * Returns true if the page was successfully allocated or
  1112. * reused.
  1113. **/
  1114. static bool i40e_alloc_mapped_page(struct i40e_ring *rx_ring,
  1115. struct i40e_rx_buffer *bi)
  1116. {
  1117. struct page *page = bi->page;
  1118. dma_addr_t dma;
  1119. /* since we are recycling buffers we should seldom need to alloc */
  1120. if (likely(page)) {
  1121. rx_ring->rx_stats.page_reuse_count++;
  1122. return true;
  1123. }
  1124. /* alloc new page for storage */
  1125. page = dev_alloc_pages(i40e_rx_pg_order(rx_ring));
  1126. if (unlikely(!page)) {
  1127. rx_ring->rx_stats.alloc_page_failed++;
  1128. return false;
  1129. }
  1130. /* map page for use */
  1131. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  1132. i40e_rx_pg_size(rx_ring),
  1133. DMA_FROM_DEVICE,
  1134. I40E_RX_DMA_ATTR);
  1135. /* if mapping failed free memory back to system since
  1136. * there isn't much point in holding memory we can't use
  1137. */
  1138. if (dma_mapping_error(rx_ring->dev, dma)) {
  1139. __free_pages(page, i40e_rx_pg_order(rx_ring));
  1140. rx_ring->rx_stats.alloc_page_failed++;
  1141. return false;
  1142. }
  1143. bi->dma = dma;
  1144. bi->page = page;
  1145. bi->page_offset = i40e_rx_offset(rx_ring);
  1146. /* initialize pagecnt_bias to 1 representing we fully own page */
  1147. bi->pagecnt_bias = 1;
  1148. return true;
  1149. }
  1150. /**
  1151. * i40e_receive_skb - Send a completed packet up the stack
  1152. * @rx_ring: rx ring in play
  1153. * @skb: packet to send up
  1154. * @vlan_tag: vlan tag for packet
  1155. **/
  1156. static void i40e_receive_skb(struct i40e_ring *rx_ring,
  1157. struct sk_buff *skb, u16 vlan_tag)
  1158. {
  1159. struct i40e_q_vector *q_vector = rx_ring->q_vector;
  1160. if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  1161. (vlan_tag & VLAN_VID_MASK))
  1162. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  1163. napi_gro_receive(&q_vector->napi, skb);
  1164. }
  1165. /**
  1166. * i40e_alloc_rx_buffers - Replace used receive buffers
  1167. * @rx_ring: ring to place buffers on
  1168. * @cleaned_count: number of buffers to replace
  1169. *
  1170. * Returns false if all allocations were successful, true if any fail
  1171. **/
  1172. bool i40e_alloc_rx_buffers(struct i40e_ring *rx_ring, u16 cleaned_count)
  1173. {
  1174. u16 ntu = rx_ring->next_to_use;
  1175. union i40e_rx_desc *rx_desc;
  1176. struct i40e_rx_buffer *bi;
  1177. /* do nothing if no valid netdev defined */
  1178. if (!rx_ring->netdev || !cleaned_count)
  1179. return false;
  1180. rx_desc = I40E_RX_DESC(rx_ring, ntu);
  1181. bi = &rx_ring->rx_bi[ntu];
  1182. do {
  1183. if (!i40e_alloc_mapped_page(rx_ring, bi))
  1184. goto no_buffers;
  1185. /* sync the buffer for use by the device */
  1186. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  1187. bi->page_offset,
  1188. rx_ring->rx_buf_len,
  1189. DMA_FROM_DEVICE);
  1190. /* Refresh the desc even if buffer_addrs didn't change
  1191. * because each write-back erases this info.
  1192. */
  1193. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  1194. rx_desc++;
  1195. bi++;
  1196. ntu++;
  1197. if (unlikely(ntu == rx_ring->count)) {
  1198. rx_desc = I40E_RX_DESC(rx_ring, 0);
  1199. bi = rx_ring->rx_bi;
  1200. ntu = 0;
  1201. }
  1202. /* clear the status bits for the next_to_use descriptor */
  1203. rx_desc->wb.qword1.status_error_len = 0;
  1204. cleaned_count--;
  1205. } while (cleaned_count);
  1206. if (rx_ring->next_to_use != ntu)
  1207. i40e_release_rx_desc(rx_ring, ntu);
  1208. return false;
  1209. no_buffers:
  1210. if (rx_ring->next_to_use != ntu)
  1211. i40e_release_rx_desc(rx_ring, ntu);
  1212. /* make sure to come back via polling to try again after
  1213. * allocation failure
  1214. */
  1215. return true;
  1216. }
  1217. /**
  1218. * i40e_rx_checksum - Indicate in skb if hw indicated a good cksum
  1219. * @vsi: the VSI we care about
  1220. * @skb: skb currently being received and modified
  1221. * @rx_desc: the receive descriptor
  1222. **/
  1223. static inline void i40e_rx_checksum(struct i40e_vsi *vsi,
  1224. struct sk_buff *skb,
  1225. union i40e_rx_desc *rx_desc)
  1226. {
  1227. struct i40e_rx_ptype_decoded decoded;
  1228. u32 rx_error, rx_status;
  1229. bool ipv4, ipv6;
  1230. u8 ptype;
  1231. u64 qword;
  1232. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1233. ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
  1234. rx_error = (qword & I40E_RXD_QW1_ERROR_MASK) >>
  1235. I40E_RXD_QW1_ERROR_SHIFT;
  1236. rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1237. I40E_RXD_QW1_STATUS_SHIFT;
  1238. decoded = decode_rx_desc_ptype(ptype);
  1239. skb->ip_summed = CHECKSUM_NONE;
  1240. skb_checksum_none_assert(skb);
  1241. /* Rx csum enabled and ip headers found? */
  1242. if (!(vsi->netdev->features & NETIF_F_RXCSUM))
  1243. return;
  1244. /* did the hardware decode the packet and checksum? */
  1245. if (!(rx_status & BIT(I40E_RX_DESC_STATUS_L3L4P_SHIFT)))
  1246. return;
  1247. /* both known and outer_ip must be set for the below code to work */
  1248. if (!(decoded.known && decoded.outer_ip))
  1249. return;
  1250. ipv4 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1251. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV4);
  1252. ipv6 = (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP) &&
  1253. (decoded.outer_ip_ver == I40E_RX_PTYPE_OUTER_IPV6);
  1254. if (ipv4 &&
  1255. (rx_error & (BIT(I40E_RX_DESC_ERROR_IPE_SHIFT) |
  1256. BIT(I40E_RX_DESC_ERROR_EIPE_SHIFT))))
  1257. goto checksum_fail;
  1258. /* likely incorrect csum if alternate IP extension headers found */
  1259. if (ipv6 &&
  1260. rx_status & BIT(I40E_RX_DESC_STATUS_IPV6EXADD_SHIFT))
  1261. /* don't increment checksum err here, non-fatal err */
  1262. return;
  1263. /* there was some L4 error, count error and punt packet to the stack */
  1264. if (rx_error & BIT(I40E_RX_DESC_ERROR_L4E_SHIFT))
  1265. goto checksum_fail;
  1266. /* handle packets that were not able to be checksummed due
  1267. * to arrival speed, in this case the stack can compute
  1268. * the csum.
  1269. */
  1270. if (rx_error & BIT(I40E_RX_DESC_ERROR_PPRS_SHIFT))
  1271. return;
  1272. /* If there is an outer header present that might contain a checksum
  1273. * we need to bump the checksum level by 1 to reflect the fact that
  1274. * we are indicating we validated the inner checksum.
  1275. */
  1276. if (decoded.tunnel_type >= I40E_RX_PTYPE_TUNNEL_IP_GRENAT)
  1277. skb->csum_level = 1;
  1278. /* Only report checksum unnecessary for TCP, UDP, or SCTP */
  1279. switch (decoded.inner_prot) {
  1280. case I40E_RX_PTYPE_INNER_PROT_TCP:
  1281. case I40E_RX_PTYPE_INNER_PROT_UDP:
  1282. case I40E_RX_PTYPE_INNER_PROT_SCTP:
  1283. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1284. /* fall though */
  1285. default:
  1286. break;
  1287. }
  1288. return;
  1289. checksum_fail:
  1290. vsi->back->hw_csum_rx_error++;
  1291. }
  1292. /**
  1293. * i40e_ptype_to_htype - get a hash type
  1294. * @ptype: the ptype value from the descriptor
  1295. *
  1296. * Returns a hash type to be used by skb_set_hash
  1297. **/
  1298. static inline int i40e_ptype_to_htype(u8 ptype)
  1299. {
  1300. struct i40e_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype);
  1301. if (!decoded.known)
  1302. return PKT_HASH_TYPE_NONE;
  1303. if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1304. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY4)
  1305. return PKT_HASH_TYPE_L4;
  1306. else if (decoded.outer_ip == I40E_RX_PTYPE_OUTER_IP &&
  1307. decoded.payload_layer == I40E_RX_PTYPE_PAYLOAD_LAYER_PAY3)
  1308. return PKT_HASH_TYPE_L3;
  1309. else
  1310. return PKT_HASH_TYPE_L2;
  1311. }
  1312. /**
  1313. * i40e_rx_hash - set the hash value in the skb
  1314. * @ring: descriptor ring
  1315. * @rx_desc: specific descriptor
  1316. **/
  1317. static inline void i40e_rx_hash(struct i40e_ring *ring,
  1318. union i40e_rx_desc *rx_desc,
  1319. struct sk_buff *skb,
  1320. u8 rx_ptype)
  1321. {
  1322. u32 hash;
  1323. const __le64 rss_mask =
  1324. cpu_to_le64((u64)I40E_RX_DESC_FLTSTAT_RSS_HASH <<
  1325. I40E_RX_DESC_STATUS_FLTSTAT_SHIFT);
  1326. if (!(ring->netdev->features & NETIF_F_RXHASH))
  1327. return;
  1328. if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) {
  1329. hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss);
  1330. skb_set_hash(skb, hash, i40e_ptype_to_htype(rx_ptype));
  1331. }
  1332. }
  1333. /**
  1334. * i40e_process_skb_fields - Populate skb header fields from Rx descriptor
  1335. * @rx_ring: rx descriptor ring packet is being transacted on
  1336. * @rx_desc: pointer to the EOP Rx descriptor
  1337. * @skb: pointer to current skb being populated
  1338. * @rx_ptype: the packet type decoded by hardware
  1339. *
  1340. * This function checks the ring, descriptor, and packet information in
  1341. * order to populate the hash, checksum, VLAN, protocol, and
  1342. * other fields within the skb.
  1343. **/
  1344. static inline
  1345. void i40e_process_skb_fields(struct i40e_ring *rx_ring,
  1346. union i40e_rx_desc *rx_desc, struct sk_buff *skb,
  1347. u8 rx_ptype)
  1348. {
  1349. u64 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1350. u32 rx_status = (qword & I40E_RXD_QW1_STATUS_MASK) >>
  1351. I40E_RXD_QW1_STATUS_SHIFT;
  1352. u32 tsynvalid = rx_status & I40E_RXD_QW1_STATUS_TSYNVALID_MASK;
  1353. u32 tsyn = (rx_status & I40E_RXD_QW1_STATUS_TSYNINDX_MASK) >>
  1354. I40E_RXD_QW1_STATUS_TSYNINDX_SHIFT;
  1355. if (unlikely(tsynvalid))
  1356. i40e_ptp_rx_hwtstamp(rx_ring->vsi->back, skb, tsyn);
  1357. i40e_rx_hash(rx_ring, rx_desc, skb, rx_ptype);
  1358. i40e_rx_checksum(rx_ring->vsi, skb, rx_desc);
  1359. skb_record_rx_queue(skb, rx_ring->queue_index);
  1360. /* modifies the skb - consumes the enet header */
  1361. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  1362. }
  1363. /**
  1364. * i40e_cleanup_headers - Correct empty headers
  1365. * @rx_ring: rx descriptor ring packet is being transacted on
  1366. * @skb: pointer to current skb being fixed
  1367. *
  1368. * Also address the case where we are pulling data in on pages only
  1369. * and as such no data is present in the skb header.
  1370. *
  1371. * In addition if skb is not at least 60 bytes we need to pad it so that
  1372. * it is large enough to qualify as a valid Ethernet frame.
  1373. *
  1374. * Returns true if an error was encountered and skb was freed.
  1375. **/
  1376. static bool i40e_cleanup_headers(struct i40e_ring *rx_ring, struct sk_buff *skb)
  1377. {
  1378. /* if eth_skb_pad returns an error the skb was freed */
  1379. if (eth_skb_pad(skb))
  1380. return true;
  1381. return false;
  1382. }
  1383. /**
  1384. * i40e_reuse_rx_page - page flip buffer and store it back on the ring
  1385. * @rx_ring: rx descriptor ring to store buffers on
  1386. * @old_buff: donor buffer to have page reused
  1387. *
  1388. * Synchronizes page for reuse by the adapter
  1389. **/
  1390. static void i40e_reuse_rx_page(struct i40e_ring *rx_ring,
  1391. struct i40e_rx_buffer *old_buff)
  1392. {
  1393. struct i40e_rx_buffer *new_buff;
  1394. u16 nta = rx_ring->next_to_alloc;
  1395. new_buff = &rx_ring->rx_bi[nta];
  1396. /* update, and store next to alloc */
  1397. nta++;
  1398. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  1399. /* transfer page from old buffer to new buffer */
  1400. new_buff->dma = old_buff->dma;
  1401. new_buff->page = old_buff->page;
  1402. new_buff->page_offset = old_buff->page_offset;
  1403. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  1404. }
  1405. /**
  1406. * i40e_page_is_reusable - check if any reuse is possible
  1407. * @page: page struct to check
  1408. *
  1409. * A page is not reusable if it was allocated under low memory
  1410. * conditions, or it's not in the same NUMA node as this CPU.
  1411. */
  1412. static inline bool i40e_page_is_reusable(struct page *page)
  1413. {
  1414. return (page_to_nid(page) == numa_mem_id()) &&
  1415. !page_is_pfmemalloc(page);
  1416. }
  1417. /**
  1418. * i40e_can_reuse_rx_page - Determine if this page can be reused by
  1419. * the adapter for another receive
  1420. *
  1421. * @rx_buffer: buffer containing the page
  1422. *
  1423. * If page is reusable, rx_buffer->page_offset is adjusted to point to
  1424. * an unused region in the page.
  1425. *
  1426. * For small pages, @truesize will be a constant value, half the size
  1427. * of the memory at page. We'll attempt to alternate between high and
  1428. * low halves of the page, with one half ready for use by the hardware
  1429. * and the other half being consumed by the stack. We use the page
  1430. * ref count to determine whether the stack has finished consuming the
  1431. * portion of this page that was passed up with a previous packet. If
  1432. * the page ref count is >1, we'll assume the "other" half page is
  1433. * still busy, and this page cannot be reused.
  1434. *
  1435. * For larger pages, @truesize will be the actual space used by the
  1436. * received packet (adjusted upward to an even multiple of the cache
  1437. * line size). This will advance through the page by the amount
  1438. * actually consumed by the received packets while there is still
  1439. * space for a buffer. Each region of larger pages will be used at
  1440. * most once, after which the page will not be reused.
  1441. *
  1442. * In either case, if the page is reusable its refcount is increased.
  1443. **/
  1444. static bool i40e_can_reuse_rx_page(struct i40e_rx_buffer *rx_buffer)
  1445. {
  1446. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  1447. struct page *page = rx_buffer->page;
  1448. /* Is any reuse possible? */
  1449. if (unlikely(!i40e_page_is_reusable(page)))
  1450. return false;
  1451. #if (PAGE_SIZE < 8192)
  1452. /* if we are only owner of page we can reuse it */
  1453. if (unlikely((page_count(page) - pagecnt_bias) > 1))
  1454. return false;
  1455. #else
  1456. #define I40E_LAST_OFFSET \
  1457. (SKB_WITH_OVERHEAD(PAGE_SIZE) - I40E_RXBUFFER_2048)
  1458. if (rx_buffer->page_offset > I40E_LAST_OFFSET)
  1459. return false;
  1460. #endif
  1461. /* If we have drained the page fragment pool we need to update
  1462. * the pagecnt_bias and page count so that we fully restock the
  1463. * number of references the driver holds.
  1464. */
  1465. if (unlikely(!pagecnt_bias)) {
  1466. page_ref_add(page, USHRT_MAX);
  1467. rx_buffer->pagecnt_bias = USHRT_MAX;
  1468. }
  1469. return true;
  1470. }
  1471. /**
  1472. * i40e_add_rx_frag - Add contents of Rx buffer to sk_buff
  1473. * @rx_ring: rx descriptor ring to transact packets on
  1474. * @rx_buffer: buffer containing page to add
  1475. * @skb: sk_buff to place the data into
  1476. * @size: packet length from rx_desc
  1477. *
  1478. * This function will add the data contained in rx_buffer->page to the skb.
  1479. * It will just attach the page as a frag to the skb.
  1480. *
  1481. * The function will then update the page offset.
  1482. **/
  1483. static void i40e_add_rx_frag(struct i40e_ring *rx_ring,
  1484. struct i40e_rx_buffer *rx_buffer,
  1485. struct sk_buff *skb,
  1486. unsigned int size)
  1487. {
  1488. #if (PAGE_SIZE < 8192)
  1489. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1490. #else
  1491. unsigned int truesize = SKB_DATA_ALIGN(size + i40e_rx_offset(rx_ring));
  1492. #endif
  1493. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  1494. rx_buffer->page_offset, size, truesize);
  1495. /* page is being used so we must update the page offset */
  1496. #if (PAGE_SIZE < 8192)
  1497. rx_buffer->page_offset ^= truesize;
  1498. #else
  1499. rx_buffer->page_offset += truesize;
  1500. #endif
  1501. }
  1502. /**
  1503. * i40e_get_rx_buffer - Fetch Rx buffer and synchronize data for use
  1504. * @rx_ring: rx descriptor ring to transact packets on
  1505. * @size: size of buffer to add to skb
  1506. *
  1507. * This function will pull an Rx buffer from the ring and synchronize it
  1508. * for use by the CPU.
  1509. */
  1510. static struct i40e_rx_buffer *i40e_get_rx_buffer(struct i40e_ring *rx_ring,
  1511. const unsigned int size)
  1512. {
  1513. struct i40e_rx_buffer *rx_buffer;
  1514. rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean];
  1515. prefetchw(rx_buffer->page);
  1516. /* we are reusing so sync this buffer for CPU use */
  1517. dma_sync_single_range_for_cpu(rx_ring->dev,
  1518. rx_buffer->dma,
  1519. rx_buffer->page_offset,
  1520. size,
  1521. DMA_FROM_DEVICE);
  1522. /* We have pulled a buffer for use, so decrement pagecnt_bias */
  1523. rx_buffer->pagecnt_bias--;
  1524. return rx_buffer;
  1525. }
  1526. /**
  1527. * i40e_construct_skb - Allocate skb and populate it
  1528. * @rx_ring: rx descriptor ring to transact packets on
  1529. * @rx_buffer: rx buffer to pull data from
  1530. * @size: size of buffer to add to skb
  1531. *
  1532. * This function allocates an skb. It then populates it with the page
  1533. * data from the current receive descriptor, taking care to set up the
  1534. * skb correctly.
  1535. */
  1536. static struct sk_buff *i40e_construct_skb(struct i40e_ring *rx_ring,
  1537. struct i40e_rx_buffer *rx_buffer,
  1538. unsigned int size)
  1539. {
  1540. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  1541. #if (PAGE_SIZE < 8192)
  1542. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1543. #else
  1544. unsigned int truesize = SKB_DATA_ALIGN(size);
  1545. #endif
  1546. unsigned int headlen;
  1547. struct sk_buff *skb;
  1548. /* prefetch first cache line of first page */
  1549. prefetch(va);
  1550. #if L1_CACHE_BYTES < 128
  1551. prefetch(va + L1_CACHE_BYTES);
  1552. #endif
  1553. /* allocate a skb to store the frags */
  1554. skb = __napi_alloc_skb(&rx_ring->q_vector->napi,
  1555. I40E_RX_HDR_SIZE,
  1556. GFP_ATOMIC | __GFP_NOWARN);
  1557. if (unlikely(!skb))
  1558. return NULL;
  1559. /* Determine available headroom for copy */
  1560. headlen = size;
  1561. if (headlen > I40E_RX_HDR_SIZE)
  1562. headlen = eth_get_headlen(va, I40E_RX_HDR_SIZE);
  1563. /* align pull length to size of long to optimize memcpy performance */
  1564. memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
  1565. /* update all of the pointers */
  1566. size -= headlen;
  1567. if (size) {
  1568. skb_add_rx_frag(skb, 0, rx_buffer->page,
  1569. rx_buffer->page_offset + headlen,
  1570. size, truesize);
  1571. /* buffer is used by skb, update page_offset */
  1572. #if (PAGE_SIZE < 8192)
  1573. rx_buffer->page_offset ^= truesize;
  1574. #else
  1575. rx_buffer->page_offset += truesize;
  1576. #endif
  1577. } else {
  1578. /* buffer is unused, reset bias back to rx_buffer */
  1579. rx_buffer->pagecnt_bias++;
  1580. }
  1581. return skb;
  1582. }
  1583. /**
  1584. * i40e_build_skb - Build skb around an existing buffer
  1585. * @rx_ring: Rx descriptor ring to transact packets on
  1586. * @rx_buffer: Rx buffer to pull data from
  1587. * @size: size of buffer to add to skb
  1588. *
  1589. * This function builds an skb around an existing Rx buffer, taking care
  1590. * to set up the skb correctly and avoid any memcpy overhead.
  1591. */
  1592. static struct sk_buff *i40e_build_skb(struct i40e_ring *rx_ring,
  1593. struct i40e_rx_buffer *rx_buffer,
  1594. unsigned int size)
  1595. {
  1596. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  1597. #if (PAGE_SIZE < 8192)
  1598. unsigned int truesize = i40e_rx_pg_size(rx_ring) / 2;
  1599. #else
  1600. unsigned int truesize = SKB_DATA_ALIGN(size);
  1601. #endif
  1602. struct sk_buff *skb;
  1603. /* prefetch first cache line of first page */
  1604. prefetch(va);
  1605. #if L1_CACHE_BYTES < 128
  1606. prefetch(va + L1_CACHE_BYTES);
  1607. #endif
  1608. /* build an skb around the page buffer */
  1609. skb = build_skb(va - I40E_SKB_PAD, truesize);
  1610. if (unlikely(!skb))
  1611. return NULL;
  1612. /* update pointers within the skb to store the data */
  1613. skb_reserve(skb, I40E_SKB_PAD);
  1614. __skb_put(skb, size);
  1615. /* buffer is used by skb, update page_offset */
  1616. #if (PAGE_SIZE < 8192)
  1617. rx_buffer->page_offset ^= truesize;
  1618. #else
  1619. rx_buffer->page_offset += truesize;
  1620. #endif
  1621. return skb;
  1622. }
  1623. /**
  1624. * i40e_put_rx_buffer - Clean up used buffer and either recycle or free
  1625. * @rx_ring: rx descriptor ring to transact packets on
  1626. * @rx_buffer: rx buffer to pull data from
  1627. *
  1628. * This function will clean up the contents of the rx_buffer. It will
  1629. * either recycle the bufer or unmap it and free the associated resources.
  1630. */
  1631. static void i40e_put_rx_buffer(struct i40e_ring *rx_ring,
  1632. struct i40e_rx_buffer *rx_buffer)
  1633. {
  1634. if (i40e_can_reuse_rx_page(rx_buffer)) {
  1635. /* hand second half of page back to the ring */
  1636. i40e_reuse_rx_page(rx_ring, rx_buffer);
  1637. rx_ring->rx_stats.page_reuse_count++;
  1638. } else {
  1639. /* we are not reusing the buffer so unmap it */
  1640. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  1641. i40e_rx_pg_size(rx_ring),
  1642. DMA_FROM_DEVICE, I40E_RX_DMA_ATTR);
  1643. __page_frag_cache_drain(rx_buffer->page,
  1644. rx_buffer->pagecnt_bias);
  1645. }
  1646. /* clear contents of buffer_info */
  1647. rx_buffer->page = NULL;
  1648. }
  1649. /**
  1650. * i40e_is_non_eop - process handling of non-EOP buffers
  1651. * @rx_ring: Rx ring being processed
  1652. * @rx_desc: Rx descriptor for current buffer
  1653. * @skb: Current socket buffer containing buffer in progress
  1654. *
  1655. * This function updates next to clean. If the buffer is an EOP buffer
  1656. * this function exits returning false, otherwise it will place the
  1657. * sk_buff in the next buffer to be chained and return true indicating
  1658. * that this is in fact a non-EOP buffer.
  1659. **/
  1660. static bool i40e_is_non_eop(struct i40e_ring *rx_ring,
  1661. union i40e_rx_desc *rx_desc,
  1662. struct sk_buff *skb)
  1663. {
  1664. u32 ntc = rx_ring->next_to_clean + 1;
  1665. /* fetch, update, and store next to clean */
  1666. ntc = (ntc < rx_ring->count) ? ntc : 0;
  1667. rx_ring->next_to_clean = ntc;
  1668. prefetch(I40E_RX_DESC(rx_ring, ntc));
  1669. /* if we are the last buffer then there is nothing else to do */
  1670. #define I40E_RXD_EOF BIT(I40E_RX_DESC_STATUS_EOF_SHIFT)
  1671. if (likely(i40e_test_staterr(rx_desc, I40E_RXD_EOF)))
  1672. return false;
  1673. rx_ring->rx_stats.non_eop_descs++;
  1674. return true;
  1675. }
  1676. /**
  1677. * i40e_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf
  1678. * @rx_ring: rx descriptor ring to transact packets on
  1679. * @budget: Total limit on number of packets to process
  1680. *
  1681. * This function provides a "bounce buffer" approach to Rx interrupt
  1682. * processing. The advantage to this is that on systems that have
  1683. * expensive overhead for IOMMU access this provides a means of avoiding
  1684. * it by maintaining the mapping of the page to the system.
  1685. *
  1686. * Returns amount of work completed
  1687. **/
  1688. static int i40e_clean_rx_irq(struct i40e_ring *rx_ring, int budget)
  1689. {
  1690. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  1691. struct sk_buff *skb = rx_ring->skb;
  1692. u16 cleaned_count = I40E_DESC_UNUSED(rx_ring);
  1693. bool failure = false;
  1694. while (likely(total_rx_packets < budget)) {
  1695. struct i40e_rx_buffer *rx_buffer;
  1696. union i40e_rx_desc *rx_desc;
  1697. unsigned int size;
  1698. u16 vlan_tag;
  1699. u8 rx_ptype;
  1700. u64 qword;
  1701. /* return some buffers to hardware, one at a time is too slow */
  1702. if (cleaned_count >= I40E_RX_BUFFER_WRITE) {
  1703. failure = failure ||
  1704. i40e_alloc_rx_buffers(rx_ring, cleaned_count);
  1705. cleaned_count = 0;
  1706. }
  1707. rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean);
  1708. /* status_error_len will always be zero for unused descriptors
  1709. * because it's cleared in cleanup, and overlaps with hdr_addr
  1710. * which is always zero because packet split isn't used, if the
  1711. * hardware wrote DD then the length will be non-zero
  1712. */
  1713. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1714. /* This memory barrier is needed to keep us from reading
  1715. * any other fields out of the rx_desc until we have
  1716. * verified the descriptor has been written back.
  1717. */
  1718. dma_rmb();
  1719. if (unlikely(i40e_rx_is_programming_status(qword))) {
  1720. i40e_clean_programming_status(rx_ring, rx_desc, qword);
  1721. continue;
  1722. }
  1723. size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >>
  1724. I40E_RXD_QW1_LENGTH_PBUF_SHIFT;
  1725. if (!size)
  1726. break;
  1727. i40e_trace(clean_rx_irq, rx_ring, rx_desc, skb);
  1728. rx_buffer = i40e_get_rx_buffer(rx_ring, size);
  1729. /* retrieve a buffer from the ring */
  1730. if (skb)
  1731. i40e_add_rx_frag(rx_ring, rx_buffer, skb, size);
  1732. else if (ring_uses_build_skb(rx_ring))
  1733. skb = i40e_build_skb(rx_ring, rx_buffer, size);
  1734. else
  1735. skb = i40e_construct_skb(rx_ring, rx_buffer, size);
  1736. /* exit if we failed to retrieve a buffer */
  1737. if (!skb) {
  1738. rx_ring->rx_stats.alloc_buff_failed++;
  1739. rx_buffer->pagecnt_bias++;
  1740. break;
  1741. }
  1742. i40e_put_rx_buffer(rx_ring, rx_buffer);
  1743. cleaned_count++;
  1744. if (i40e_is_non_eop(rx_ring, rx_desc, skb))
  1745. continue;
  1746. /* ERR_MASK will only have valid bits if EOP set, and
  1747. * what we are doing here is actually checking
  1748. * I40E_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in
  1749. * the error field
  1750. */
  1751. if (unlikely(i40e_test_staterr(rx_desc, BIT(I40E_RXD_QW1_ERROR_SHIFT)))) {
  1752. dev_kfree_skb_any(skb);
  1753. skb = NULL;
  1754. continue;
  1755. }
  1756. if (i40e_cleanup_headers(rx_ring, skb)) {
  1757. skb = NULL;
  1758. continue;
  1759. }
  1760. /* probably a little skewed due to removing CRC */
  1761. total_rx_bytes += skb->len;
  1762. qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
  1763. rx_ptype = (qword & I40E_RXD_QW1_PTYPE_MASK) >>
  1764. I40E_RXD_QW1_PTYPE_SHIFT;
  1765. /* populate checksum, VLAN, and protocol */
  1766. i40e_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
  1767. vlan_tag = (qword & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)) ?
  1768. le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1) : 0;
  1769. i40e_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb);
  1770. i40e_receive_skb(rx_ring, skb, vlan_tag);
  1771. skb = NULL;
  1772. /* update budget accounting */
  1773. total_rx_packets++;
  1774. }
  1775. rx_ring->skb = skb;
  1776. u64_stats_update_begin(&rx_ring->syncp);
  1777. rx_ring->stats.packets += total_rx_packets;
  1778. rx_ring->stats.bytes += total_rx_bytes;
  1779. u64_stats_update_end(&rx_ring->syncp);
  1780. rx_ring->q_vector->rx.total_packets += total_rx_packets;
  1781. rx_ring->q_vector->rx.total_bytes += total_rx_bytes;
  1782. /* guarantee a trip back through this routine if there was a failure */
  1783. return failure ? budget : total_rx_packets;
  1784. }
  1785. static u32 i40e_buildreg_itr(const int type, const u16 itr)
  1786. {
  1787. u32 val;
  1788. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  1789. /* Don't clear PBA because that can cause lost interrupts that
  1790. * came in while we were cleaning/polling
  1791. */
  1792. (type << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT) |
  1793. (itr << I40E_PFINT_DYN_CTLN_INTERVAL_SHIFT);
  1794. return val;
  1795. }
  1796. /* a small macro to shorten up some long lines */
  1797. #define INTREG I40E_PFINT_DYN_CTLN
  1798. static inline int get_rx_itr(struct i40e_vsi *vsi, int idx)
  1799. {
  1800. return vsi->rx_rings[idx]->rx_itr_setting;
  1801. }
  1802. static inline int get_tx_itr(struct i40e_vsi *vsi, int idx)
  1803. {
  1804. return vsi->tx_rings[idx]->tx_itr_setting;
  1805. }
  1806. /**
  1807. * i40e_update_enable_itr - Update itr and re-enable MSIX interrupt
  1808. * @vsi: the VSI we care about
  1809. * @q_vector: q_vector for which itr is being updated and interrupt enabled
  1810. *
  1811. **/
  1812. static inline void i40e_update_enable_itr(struct i40e_vsi *vsi,
  1813. struct i40e_q_vector *q_vector)
  1814. {
  1815. struct i40e_hw *hw = &vsi->back->hw;
  1816. bool rx = false, tx = false;
  1817. u32 rxval, txval;
  1818. int vector;
  1819. int idx = q_vector->v_idx;
  1820. int rx_itr_setting, tx_itr_setting;
  1821. vector = (q_vector->v_idx + vsi->base_vector);
  1822. /* avoid dynamic calculation if in countdown mode OR if
  1823. * all dynamic is disabled
  1824. */
  1825. rxval = txval = i40e_buildreg_itr(I40E_ITR_NONE, 0);
  1826. rx_itr_setting = get_rx_itr(vsi, idx);
  1827. tx_itr_setting = get_tx_itr(vsi, idx);
  1828. if (q_vector->itr_countdown > 0 ||
  1829. (!ITR_IS_DYNAMIC(rx_itr_setting) &&
  1830. !ITR_IS_DYNAMIC(tx_itr_setting))) {
  1831. goto enable_int;
  1832. }
  1833. if (ITR_IS_DYNAMIC(tx_itr_setting)) {
  1834. rx = i40e_set_new_dynamic_itr(&q_vector->rx);
  1835. rxval = i40e_buildreg_itr(I40E_RX_ITR, q_vector->rx.itr);
  1836. }
  1837. if (ITR_IS_DYNAMIC(tx_itr_setting)) {
  1838. tx = i40e_set_new_dynamic_itr(&q_vector->tx);
  1839. txval = i40e_buildreg_itr(I40E_TX_ITR, q_vector->tx.itr);
  1840. }
  1841. if (rx || tx) {
  1842. /* get the higher of the two ITR adjustments and
  1843. * use the same value for both ITR registers
  1844. * when in adaptive mode (Rx and/or Tx)
  1845. */
  1846. u16 itr = max(q_vector->tx.itr, q_vector->rx.itr);
  1847. q_vector->tx.itr = q_vector->rx.itr = itr;
  1848. txval = i40e_buildreg_itr(I40E_TX_ITR, itr);
  1849. tx = true;
  1850. rxval = i40e_buildreg_itr(I40E_RX_ITR, itr);
  1851. rx = true;
  1852. }
  1853. /* only need to enable the interrupt once, but need
  1854. * to possibly update both ITR values
  1855. */
  1856. if (rx) {
  1857. /* set the INTENA_MSK_MASK so that this first write
  1858. * won't actually enable the interrupt, instead just
  1859. * updating the ITR (it's bit 31 PF and VF)
  1860. */
  1861. rxval |= BIT(31);
  1862. /* don't check _DOWN because interrupt isn't being enabled */
  1863. wr32(hw, INTREG(vector - 1), rxval);
  1864. }
  1865. enable_int:
  1866. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  1867. wr32(hw, INTREG(vector - 1), txval);
  1868. if (q_vector->itr_countdown)
  1869. q_vector->itr_countdown--;
  1870. else
  1871. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  1872. }
  1873. /**
  1874. * i40e_napi_poll - NAPI polling Rx/Tx cleanup routine
  1875. * @napi: napi struct with our devices info in it
  1876. * @budget: amount of work driver is allowed to do this pass, in packets
  1877. *
  1878. * This function will clean all queues associated with a q_vector.
  1879. *
  1880. * Returns the amount of work done
  1881. **/
  1882. int i40e_napi_poll(struct napi_struct *napi, int budget)
  1883. {
  1884. struct i40e_q_vector *q_vector =
  1885. container_of(napi, struct i40e_q_vector, napi);
  1886. struct i40e_vsi *vsi = q_vector->vsi;
  1887. struct i40e_ring *ring;
  1888. bool clean_complete = true;
  1889. bool arm_wb = false;
  1890. int budget_per_ring;
  1891. int work_done = 0;
  1892. if (test_bit(__I40E_VSI_DOWN, vsi->state)) {
  1893. napi_complete(napi);
  1894. return 0;
  1895. }
  1896. /* Since the actual Tx work is minimal, we can give the Tx a larger
  1897. * budget and be more aggressive about cleaning up the Tx descriptors.
  1898. */
  1899. i40e_for_each_ring(ring, q_vector->tx) {
  1900. if (!i40e_clean_tx_irq(vsi, ring, budget)) {
  1901. clean_complete = false;
  1902. continue;
  1903. }
  1904. arm_wb |= ring->arm_wb;
  1905. ring->arm_wb = false;
  1906. }
  1907. /* Handle case where we are called by netpoll with a budget of 0 */
  1908. if (budget <= 0)
  1909. goto tx_only;
  1910. /* We attempt to distribute budget to each Rx queue fairly, but don't
  1911. * allow the budget to go below 1 because that would exit polling early.
  1912. */
  1913. budget_per_ring = max(budget/q_vector->num_ringpairs, 1);
  1914. i40e_for_each_ring(ring, q_vector->rx) {
  1915. int cleaned = i40e_clean_rx_irq(ring, budget_per_ring);
  1916. work_done += cleaned;
  1917. /* if we clean as many as budgeted, we must not be done */
  1918. if (cleaned >= budget_per_ring)
  1919. clean_complete = false;
  1920. }
  1921. /* If work not completed, return budget and polling will return */
  1922. if (!clean_complete) {
  1923. const cpumask_t *aff_mask = &q_vector->affinity_mask;
  1924. int cpu_id = smp_processor_id();
  1925. /* It is possible that the interrupt affinity has changed but,
  1926. * if the cpu is pegged at 100%, polling will never exit while
  1927. * traffic continues and the interrupt will be stuck on this
  1928. * cpu. We check to make sure affinity is correct before we
  1929. * continue to poll, otherwise we must stop polling so the
  1930. * interrupt can move to the correct cpu.
  1931. */
  1932. if (likely(cpumask_test_cpu(cpu_id, aff_mask) ||
  1933. !(vsi->back->flags & I40E_FLAG_MSIX_ENABLED))) {
  1934. tx_only:
  1935. if (arm_wb) {
  1936. q_vector->tx.ring[0].tx_stats.tx_force_wb++;
  1937. i40e_enable_wb_on_itr(vsi, q_vector);
  1938. }
  1939. return budget;
  1940. }
  1941. }
  1942. if (vsi->back->flags & I40E_TXR_FLAGS_WB_ON_ITR)
  1943. q_vector->arm_wb_state = false;
  1944. /* Work is done so exit the polling mode and re-enable the interrupt */
  1945. napi_complete_done(napi, work_done);
  1946. /* If we're prematurely stopping polling to fix the interrupt
  1947. * affinity we want to make sure polling starts back up so we
  1948. * issue a call to i40e_force_wb which triggers a SW interrupt.
  1949. */
  1950. if (!clean_complete)
  1951. i40e_force_wb(vsi, q_vector);
  1952. else if (!(vsi->back->flags & I40E_FLAG_MSIX_ENABLED))
  1953. i40e_irq_dynamic_enable_icr0(vsi->back, false);
  1954. else
  1955. i40e_update_enable_itr(vsi, q_vector);
  1956. return min(work_done, budget - 1);
  1957. }
  1958. /**
  1959. * i40e_atr - Add a Flow Director ATR filter
  1960. * @tx_ring: ring to add programming descriptor to
  1961. * @skb: send buffer
  1962. * @tx_flags: send tx flags
  1963. **/
  1964. static void i40e_atr(struct i40e_ring *tx_ring, struct sk_buff *skb,
  1965. u32 tx_flags)
  1966. {
  1967. struct i40e_filter_program_desc *fdir_desc;
  1968. struct i40e_pf *pf = tx_ring->vsi->back;
  1969. union {
  1970. unsigned char *network;
  1971. struct iphdr *ipv4;
  1972. struct ipv6hdr *ipv6;
  1973. } hdr;
  1974. struct tcphdr *th;
  1975. unsigned int hlen;
  1976. u32 flex_ptype, dtype_cmd;
  1977. int l4_proto;
  1978. u16 i;
  1979. /* make sure ATR is enabled */
  1980. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  1981. return;
  1982. if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED)
  1983. return;
  1984. /* if sampling is disabled do nothing */
  1985. if (!tx_ring->atr_sample_rate)
  1986. return;
  1987. /* Currently only IPv4/IPv6 with TCP is supported */
  1988. if (!(tx_flags & (I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6)))
  1989. return;
  1990. /* snag network header to get L4 type and address */
  1991. hdr.network = (tx_flags & I40E_TX_FLAGS_UDP_TUNNEL) ?
  1992. skb_inner_network_header(skb) : skb_network_header(skb);
  1993. /* Note: tx_flags gets modified to reflect inner protocols in
  1994. * tx_enable_csum function if encap is enabled.
  1995. */
  1996. if (tx_flags & I40E_TX_FLAGS_IPV4) {
  1997. /* access ihl as u8 to avoid unaligned access on ia64 */
  1998. hlen = (hdr.network[0] & 0x0F) << 2;
  1999. l4_proto = hdr.ipv4->protocol;
  2000. } else {
  2001. hlen = hdr.network - skb->data;
  2002. l4_proto = ipv6_find_hdr(skb, &hlen, IPPROTO_TCP, NULL, NULL);
  2003. hlen -= hdr.network - skb->data;
  2004. }
  2005. if (l4_proto != IPPROTO_TCP)
  2006. return;
  2007. th = (struct tcphdr *)(hdr.network + hlen);
  2008. /* Due to lack of space, no more new filters can be programmed */
  2009. if (th->syn && (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
  2010. return;
  2011. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE) {
  2012. /* HW ATR eviction will take care of removing filters on FIN
  2013. * and RST packets.
  2014. */
  2015. if (th->fin || th->rst)
  2016. return;
  2017. }
  2018. tx_ring->atr_count++;
  2019. /* sample on all syn/fin/rst packets or once every atr sample rate */
  2020. if (!th->fin &&
  2021. !th->syn &&
  2022. !th->rst &&
  2023. (tx_ring->atr_count < tx_ring->atr_sample_rate))
  2024. return;
  2025. tx_ring->atr_count = 0;
  2026. /* grab the next descriptor */
  2027. i = tx_ring->next_to_use;
  2028. fdir_desc = I40E_TX_FDIRDESC(tx_ring, i);
  2029. i++;
  2030. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2031. flex_ptype = (tx_ring->queue_index << I40E_TXD_FLTR_QW0_QINDEX_SHIFT) &
  2032. I40E_TXD_FLTR_QW0_QINDEX_MASK;
  2033. flex_ptype |= (tx_flags & I40E_TX_FLAGS_IPV4) ?
  2034. (I40E_FILTER_PCTYPE_NONF_IPV4_TCP <<
  2035. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT) :
  2036. (I40E_FILTER_PCTYPE_NONF_IPV6_TCP <<
  2037. I40E_TXD_FLTR_QW0_PCTYPE_SHIFT);
  2038. flex_ptype |= tx_ring->vsi->id << I40E_TXD_FLTR_QW0_DEST_VSI_SHIFT;
  2039. dtype_cmd = I40E_TX_DESC_DTYPE_FILTER_PROG;
  2040. dtype_cmd |= (th->fin || th->rst) ?
  2041. (I40E_FILTER_PROGRAM_DESC_PCMD_REMOVE <<
  2042. I40E_TXD_FLTR_QW1_PCMD_SHIFT) :
  2043. (I40E_FILTER_PROGRAM_DESC_PCMD_ADD_UPDATE <<
  2044. I40E_TXD_FLTR_QW1_PCMD_SHIFT);
  2045. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX <<
  2046. I40E_TXD_FLTR_QW1_DEST_SHIFT;
  2047. dtype_cmd |= I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID <<
  2048. I40E_TXD_FLTR_QW1_FD_STATUS_SHIFT;
  2049. dtype_cmd |= I40E_TXD_FLTR_QW1_CNT_ENA_MASK;
  2050. if (!(tx_flags & I40E_TX_FLAGS_UDP_TUNNEL))
  2051. dtype_cmd |=
  2052. ((u32)I40E_FD_ATR_STAT_IDX(pf->hw.pf_id) <<
  2053. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2054. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2055. else
  2056. dtype_cmd |=
  2057. ((u32)I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id) <<
  2058. I40E_TXD_FLTR_QW1_CNTINDEX_SHIFT) &
  2059. I40E_TXD_FLTR_QW1_CNTINDEX_MASK;
  2060. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE)
  2061. dtype_cmd |= I40E_TXD_FLTR_QW1_ATR_MASK;
  2062. fdir_desc->qindex_flex_ptype_vsi = cpu_to_le32(flex_ptype);
  2063. fdir_desc->rsvd = cpu_to_le32(0);
  2064. fdir_desc->dtype_cmd_cntindex = cpu_to_le32(dtype_cmd);
  2065. fdir_desc->fd_id = cpu_to_le32(0);
  2066. }
  2067. /**
  2068. * i40e_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW
  2069. * @skb: send buffer
  2070. * @tx_ring: ring to send buffer on
  2071. * @flags: the tx flags to be set
  2072. *
  2073. * Checks the skb and set up correspondingly several generic transmit flags
  2074. * related to VLAN tagging for the HW, such as VLAN, DCB, etc.
  2075. *
  2076. * Returns error code indicate the frame should be dropped upon error and the
  2077. * otherwise returns 0 to indicate the flags has been set properly.
  2078. **/
  2079. static inline int i40e_tx_prepare_vlan_flags(struct sk_buff *skb,
  2080. struct i40e_ring *tx_ring,
  2081. u32 *flags)
  2082. {
  2083. __be16 protocol = skb->protocol;
  2084. u32 tx_flags = 0;
  2085. if (protocol == htons(ETH_P_8021Q) &&
  2086. !(tx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) {
  2087. /* When HW VLAN acceleration is turned off by the user the
  2088. * stack sets the protocol to 8021q so that the driver
  2089. * can take any steps required to support the SW only
  2090. * VLAN handling. In our case the driver doesn't need
  2091. * to take any further steps so just set the protocol
  2092. * to the encapsulated ethertype.
  2093. */
  2094. skb->protocol = vlan_get_protocol(skb);
  2095. goto out;
  2096. }
  2097. /* if we have a HW VLAN tag being added, default to the HW one */
  2098. if (skb_vlan_tag_present(skb)) {
  2099. tx_flags |= skb_vlan_tag_get(skb) << I40E_TX_FLAGS_VLAN_SHIFT;
  2100. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2101. /* else if it is a SW VLAN, check the next protocol and store the tag */
  2102. } else if (protocol == htons(ETH_P_8021Q)) {
  2103. struct vlan_hdr *vhdr, _vhdr;
  2104. vhdr = skb_header_pointer(skb, ETH_HLEN, sizeof(_vhdr), &_vhdr);
  2105. if (!vhdr)
  2106. return -EINVAL;
  2107. protocol = vhdr->h_vlan_encapsulated_proto;
  2108. tx_flags |= ntohs(vhdr->h_vlan_TCI) << I40E_TX_FLAGS_VLAN_SHIFT;
  2109. tx_flags |= I40E_TX_FLAGS_SW_VLAN;
  2110. }
  2111. if (!(tx_ring->vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2112. goto out;
  2113. /* Insert 802.1p priority into VLAN header */
  2114. if ((tx_flags & (I40E_TX_FLAGS_HW_VLAN | I40E_TX_FLAGS_SW_VLAN)) ||
  2115. (skb->priority != TC_PRIO_CONTROL)) {
  2116. tx_flags &= ~I40E_TX_FLAGS_VLAN_PRIO_MASK;
  2117. tx_flags |= (skb->priority & 0x7) <<
  2118. I40E_TX_FLAGS_VLAN_PRIO_SHIFT;
  2119. if (tx_flags & I40E_TX_FLAGS_SW_VLAN) {
  2120. struct vlan_ethhdr *vhdr;
  2121. int rc;
  2122. rc = skb_cow_head(skb, 0);
  2123. if (rc < 0)
  2124. return rc;
  2125. vhdr = (struct vlan_ethhdr *)skb->data;
  2126. vhdr->h_vlan_TCI = htons(tx_flags >>
  2127. I40E_TX_FLAGS_VLAN_SHIFT);
  2128. } else {
  2129. tx_flags |= I40E_TX_FLAGS_HW_VLAN;
  2130. }
  2131. }
  2132. out:
  2133. *flags = tx_flags;
  2134. return 0;
  2135. }
  2136. /**
  2137. * i40e_tso - set up the tso context descriptor
  2138. * @first: pointer to first Tx buffer for xmit
  2139. * @hdr_len: ptr to the size of the packet header
  2140. * @cd_type_cmd_tso_mss: Quad Word 1
  2141. *
  2142. * Returns 0 if no TSO can happen, 1 if tso is going, or error
  2143. **/
  2144. static int i40e_tso(struct i40e_tx_buffer *first, u8 *hdr_len,
  2145. u64 *cd_type_cmd_tso_mss)
  2146. {
  2147. struct sk_buff *skb = first->skb;
  2148. u64 cd_cmd, cd_tso_len, cd_mss;
  2149. union {
  2150. struct iphdr *v4;
  2151. struct ipv6hdr *v6;
  2152. unsigned char *hdr;
  2153. } ip;
  2154. union {
  2155. struct tcphdr *tcp;
  2156. struct udphdr *udp;
  2157. unsigned char *hdr;
  2158. } l4;
  2159. u32 paylen, l4_offset;
  2160. u16 gso_segs, gso_size;
  2161. int err;
  2162. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2163. return 0;
  2164. if (!skb_is_gso(skb))
  2165. return 0;
  2166. err = skb_cow_head(skb, 0);
  2167. if (err < 0)
  2168. return err;
  2169. ip.hdr = skb_network_header(skb);
  2170. l4.hdr = skb_transport_header(skb);
  2171. /* initialize outer IP header fields */
  2172. if (ip.v4->version == 4) {
  2173. ip.v4->tot_len = 0;
  2174. ip.v4->check = 0;
  2175. } else {
  2176. ip.v6->payload_len = 0;
  2177. }
  2178. if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE |
  2179. SKB_GSO_GRE_CSUM |
  2180. SKB_GSO_IPXIP4 |
  2181. SKB_GSO_IPXIP6 |
  2182. SKB_GSO_UDP_TUNNEL |
  2183. SKB_GSO_UDP_TUNNEL_CSUM)) {
  2184. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2185. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) {
  2186. l4.udp->len = 0;
  2187. /* determine offset of outer transport header */
  2188. l4_offset = l4.hdr - skb->data;
  2189. /* remove payload length from outer checksum */
  2190. paylen = skb->len - l4_offset;
  2191. csum_replace_by_diff(&l4.udp->check,
  2192. (__force __wsum)htonl(paylen));
  2193. }
  2194. /* reset pointers to inner headers */
  2195. ip.hdr = skb_inner_network_header(skb);
  2196. l4.hdr = skb_inner_transport_header(skb);
  2197. /* initialize inner IP header fields */
  2198. if (ip.v4->version == 4) {
  2199. ip.v4->tot_len = 0;
  2200. ip.v4->check = 0;
  2201. } else {
  2202. ip.v6->payload_len = 0;
  2203. }
  2204. }
  2205. /* determine offset of inner transport header */
  2206. l4_offset = l4.hdr - skb->data;
  2207. /* remove payload length from inner checksum */
  2208. paylen = skb->len - l4_offset;
  2209. csum_replace_by_diff(&l4.tcp->check, (__force __wsum)htonl(paylen));
  2210. /* compute length of segmentation header */
  2211. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  2212. /* pull values out of skb_shinfo */
  2213. gso_size = skb_shinfo(skb)->gso_size;
  2214. gso_segs = skb_shinfo(skb)->gso_segs;
  2215. /* update GSO size and bytecount with header size */
  2216. first->gso_segs = gso_segs;
  2217. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  2218. /* find the field values */
  2219. cd_cmd = I40E_TX_CTX_DESC_TSO;
  2220. cd_tso_len = skb->len - *hdr_len;
  2221. cd_mss = gso_size;
  2222. *cd_type_cmd_tso_mss |= (cd_cmd << I40E_TXD_CTX_QW1_CMD_SHIFT) |
  2223. (cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
  2224. (cd_mss << I40E_TXD_CTX_QW1_MSS_SHIFT);
  2225. return 1;
  2226. }
  2227. /**
  2228. * i40e_tsyn - set up the tsyn context descriptor
  2229. * @tx_ring: ptr to the ring to send
  2230. * @skb: ptr to the skb we're sending
  2231. * @tx_flags: the collected send information
  2232. * @cd_type_cmd_tso_mss: Quad Word 1
  2233. *
  2234. * Returns 0 if no Tx timestamp can happen and 1 if the timestamp will happen
  2235. **/
  2236. static int i40e_tsyn(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2237. u32 tx_flags, u64 *cd_type_cmd_tso_mss)
  2238. {
  2239. struct i40e_pf *pf;
  2240. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  2241. return 0;
  2242. /* Tx timestamps cannot be sampled when doing TSO */
  2243. if (tx_flags & I40E_TX_FLAGS_TSO)
  2244. return 0;
  2245. /* only timestamp the outbound packet if the user has requested it and
  2246. * we are not already transmitting a packet to be timestamped
  2247. */
  2248. pf = i40e_netdev_to_pf(tx_ring->netdev);
  2249. if (!(pf->flags & I40E_FLAG_PTP))
  2250. return 0;
  2251. if (pf->ptp_tx &&
  2252. !test_and_set_bit_lock(__I40E_PTP_TX_IN_PROGRESS, pf->state)) {
  2253. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2254. pf->ptp_tx_skb = skb_get(skb);
  2255. } else {
  2256. return 0;
  2257. }
  2258. *cd_type_cmd_tso_mss |= (u64)I40E_TX_CTX_DESC_TSYN <<
  2259. I40E_TXD_CTX_QW1_CMD_SHIFT;
  2260. return 1;
  2261. }
  2262. /**
  2263. * i40e_tx_enable_csum - Enable Tx checksum offloads
  2264. * @skb: send buffer
  2265. * @tx_flags: pointer to Tx flags currently set
  2266. * @td_cmd: Tx descriptor command bits to set
  2267. * @td_offset: Tx descriptor header offsets to set
  2268. * @tx_ring: Tx descriptor ring
  2269. * @cd_tunneling: ptr to context desc bits
  2270. **/
  2271. static int i40e_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags,
  2272. u32 *td_cmd, u32 *td_offset,
  2273. struct i40e_ring *tx_ring,
  2274. u32 *cd_tunneling)
  2275. {
  2276. union {
  2277. struct iphdr *v4;
  2278. struct ipv6hdr *v6;
  2279. unsigned char *hdr;
  2280. } ip;
  2281. union {
  2282. struct tcphdr *tcp;
  2283. struct udphdr *udp;
  2284. unsigned char *hdr;
  2285. } l4;
  2286. unsigned char *exthdr;
  2287. u32 offset, cmd = 0;
  2288. __be16 frag_off;
  2289. u8 l4_proto = 0;
  2290. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2291. return 0;
  2292. ip.hdr = skb_network_header(skb);
  2293. l4.hdr = skb_transport_header(skb);
  2294. /* compute outer L2 header size */
  2295. offset = ((ip.hdr - skb->data) / 2) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
  2296. if (skb->encapsulation) {
  2297. u32 tunnel = 0;
  2298. /* define outer network header type */
  2299. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2300. tunnel |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2301. I40E_TX_CTX_EXT_IP_IPV4 :
  2302. I40E_TX_CTX_EXT_IP_IPV4_NO_CSUM;
  2303. l4_proto = ip.v4->protocol;
  2304. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2305. tunnel |= I40E_TX_CTX_EXT_IP_IPV6;
  2306. exthdr = ip.hdr + sizeof(*ip.v6);
  2307. l4_proto = ip.v6->nexthdr;
  2308. if (l4.hdr != exthdr)
  2309. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2310. &l4_proto, &frag_off);
  2311. }
  2312. /* define outer transport */
  2313. switch (l4_proto) {
  2314. case IPPROTO_UDP:
  2315. tunnel |= I40E_TXD_CTX_UDP_TUNNELING;
  2316. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2317. break;
  2318. case IPPROTO_GRE:
  2319. tunnel |= I40E_TXD_CTX_GRE_TUNNELING;
  2320. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2321. break;
  2322. case IPPROTO_IPIP:
  2323. case IPPROTO_IPV6:
  2324. *tx_flags |= I40E_TX_FLAGS_UDP_TUNNEL;
  2325. l4.hdr = skb_inner_network_header(skb);
  2326. break;
  2327. default:
  2328. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2329. return -1;
  2330. skb_checksum_help(skb);
  2331. return 0;
  2332. }
  2333. /* compute outer L3 header size */
  2334. tunnel |= ((l4.hdr - ip.hdr) / 4) <<
  2335. I40E_TXD_CTX_QW0_EXT_IPLEN_SHIFT;
  2336. /* switch IP header pointer from outer to inner header */
  2337. ip.hdr = skb_inner_network_header(skb);
  2338. /* compute tunnel header size */
  2339. tunnel |= ((ip.hdr - l4.hdr) / 2) <<
  2340. I40E_TXD_CTX_QW0_NATLEN_SHIFT;
  2341. /* indicate if we need to offload outer UDP header */
  2342. if ((*tx_flags & I40E_TX_FLAGS_TSO) &&
  2343. !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) &&
  2344. (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM))
  2345. tunnel |= I40E_TXD_CTX_QW0_L4T_CS_MASK;
  2346. /* record tunnel offload values */
  2347. *cd_tunneling |= tunnel;
  2348. /* switch L4 header pointer from outer to inner */
  2349. l4.hdr = skb_inner_transport_header(skb);
  2350. l4_proto = 0;
  2351. /* reset type as we transition from outer to inner headers */
  2352. *tx_flags &= ~(I40E_TX_FLAGS_IPV4 | I40E_TX_FLAGS_IPV6);
  2353. if (ip.v4->version == 4)
  2354. *tx_flags |= I40E_TX_FLAGS_IPV4;
  2355. if (ip.v6->version == 6)
  2356. *tx_flags |= I40E_TX_FLAGS_IPV6;
  2357. }
  2358. /* Enable IP checksum offloads */
  2359. if (*tx_flags & I40E_TX_FLAGS_IPV4) {
  2360. l4_proto = ip.v4->protocol;
  2361. /* the stack computes the IP header already, the only time we
  2362. * need the hardware to recompute it is in the case of TSO.
  2363. */
  2364. cmd |= (*tx_flags & I40E_TX_FLAGS_TSO) ?
  2365. I40E_TX_DESC_CMD_IIPT_IPV4_CSUM :
  2366. I40E_TX_DESC_CMD_IIPT_IPV4;
  2367. } else if (*tx_flags & I40E_TX_FLAGS_IPV6) {
  2368. cmd |= I40E_TX_DESC_CMD_IIPT_IPV6;
  2369. exthdr = ip.hdr + sizeof(*ip.v6);
  2370. l4_proto = ip.v6->nexthdr;
  2371. if (l4.hdr != exthdr)
  2372. ipv6_skip_exthdr(skb, exthdr - skb->data,
  2373. &l4_proto, &frag_off);
  2374. }
  2375. /* compute inner L3 header size */
  2376. offset |= ((l4.hdr - ip.hdr) / 4) << I40E_TX_DESC_LENGTH_IPLEN_SHIFT;
  2377. /* Enable L4 checksum offloads */
  2378. switch (l4_proto) {
  2379. case IPPROTO_TCP:
  2380. /* enable checksum offloads */
  2381. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_TCP;
  2382. offset |= l4.tcp->doff << I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2383. break;
  2384. case IPPROTO_SCTP:
  2385. /* enable SCTP checksum offload */
  2386. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_SCTP;
  2387. offset |= (sizeof(struct sctphdr) >> 2) <<
  2388. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2389. break;
  2390. case IPPROTO_UDP:
  2391. /* enable UDP checksum offload */
  2392. cmd |= I40E_TX_DESC_CMD_L4T_EOFT_UDP;
  2393. offset |= (sizeof(struct udphdr) >> 2) <<
  2394. I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
  2395. break;
  2396. default:
  2397. if (*tx_flags & I40E_TX_FLAGS_TSO)
  2398. return -1;
  2399. skb_checksum_help(skb);
  2400. return 0;
  2401. }
  2402. *td_cmd |= cmd;
  2403. *td_offset |= offset;
  2404. return 1;
  2405. }
  2406. /**
  2407. * i40e_create_tx_ctx Build the Tx context descriptor
  2408. * @tx_ring: ring to create the descriptor on
  2409. * @cd_type_cmd_tso_mss: Quad Word 1
  2410. * @cd_tunneling: Quad Word 0 - bits 0-31
  2411. * @cd_l2tag2: Quad Word 0 - bits 32-63
  2412. **/
  2413. static void i40e_create_tx_ctx(struct i40e_ring *tx_ring,
  2414. const u64 cd_type_cmd_tso_mss,
  2415. const u32 cd_tunneling, const u32 cd_l2tag2)
  2416. {
  2417. struct i40e_tx_context_desc *context_desc;
  2418. int i = tx_ring->next_to_use;
  2419. if ((cd_type_cmd_tso_mss == I40E_TX_DESC_DTYPE_CONTEXT) &&
  2420. !cd_tunneling && !cd_l2tag2)
  2421. return;
  2422. /* grab the next descriptor */
  2423. context_desc = I40E_TX_CTXTDESC(tx_ring, i);
  2424. i++;
  2425. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  2426. /* cpu_to_le32 and assign to struct fields */
  2427. context_desc->tunneling_params = cpu_to_le32(cd_tunneling);
  2428. context_desc->l2tag2 = cpu_to_le16(cd_l2tag2);
  2429. context_desc->rsvd = cpu_to_le16(0);
  2430. context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
  2431. }
  2432. /**
  2433. * __i40e_maybe_stop_tx - 2nd level check for tx stop conditions
  2434. * @tx_ring: the ring to be checked
  2435. * @size: the size buffer we want to assure is available
  2436. *
  2437. * Returns -EBUSY if a stop is needed, else 0
  2438. **/
  2439. int __i40e_maybe_stop_tx(struct i40e_ring *tx_ring, int size)
  2440. {
  2441. netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2442. /* Memory barrier before checking head and tail */
  2443. smp_mb();
  2444. /* Check again in a case another CPU has just made room available. */
  2445. if (likely(I40E_DESC_UNUSED(tx_ring) < size))
  2446. return -EBUSY;
  2447. /* A reprieve! - use start_queue because it doesn't call schedule */
  2448. netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index);
  2449. ++tx_ring->tx_stats.restart_queue;
  2450. return 0;
  2451. }
  2452. /**
  2453. * __i40e_chk_linearize - Check if there are more than 8 buffers per packet
  2454. * @skb: send buffer
  2455. *
  2456. * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire
  2457. * and so we need to figure out the cases where we need to linearize the skb.
  2458. *
  2459. * For TSO we need to count the TSO header and segment payload separately.
  2460. * As such we need to check cases where we have 7 fragments or more as we
  2461. * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for
  2462. * the segment payload in the first descriptor, and another 7 for the
  2463. * fragments.
  2464. **/
  2465. bool __i40e_chk_linearize(struct sk_buff *skb)
  2466. {
  2467. const struct skb_frag_struct *frag, *stale;
  2468. int nr_frags, sum;
  2469. /* no need to check if number of frags is less than 7 */
  2470. nr_frags = skb_shinfo(skb)->nr_frags;
  2471. if (nr_frags < (I40E_MAX_BUFFER_TXD - 1))
  2472. return false;
  2473. /* We need to walk through the list and validate that each group
  2474. * of 6 fragments totals at least gso_size.
  2475. */
  2476. nr_frags -= I40E_MAX_BUFFER_TXD - 2;
  2477. frag = &skb_shinfo(skb)->frags[0];
  2478. /* Initialize size to the negative value of gso_size minus 1. We
  2479. * use this as the worst case scenerio in which the frag ahead
  2480. * of us only provides one byte which is why we are limited to 6
  2481. * descriptors for a single transmit as the header and previous
  2482. * fragment are already consuming 2 descriptors.
  2483. */
  2484. sum = 1 - skb_shinfo(skb)->gso_size;
  2485. /* Add size of frags 0 through 4 to create our initial sum */
  2486. sum += skb_frag_size(frag++);
  2487. sum += skb_frag_size(frag++);
  2488. sum += skb_frag_size(frag++);
  2489. sum += skb_frag_size(frag++);
  2490. sum += skb_frag_size(frag++);
  2491. /* Walk through fragments adding latest fragment, testing it, and
  2492. * then removing stale fragments from the sum.
  2493. */
  2494. stale = &skb_shinfo(skb)->frags[0];
  2495. for (;;) {
  2496. sum += skb_frag_size(frag++);
  2497. /* if sum is negative we failed to make sufficient progress */
  2498. if (sum < 0)
  2499. return true;
  2500. if (!nr_frags--)
  2501. break;
  2502. sum -= skb_frag_size(stale++);
  2503. }
  2504. return false;
  2505. }
  2506. /**
  2507. * i40e_tx_map - Build the Tx descriptor
  2508. * @tx_ring: ring to send buffer on
  2509. * @skb: send buffer
  2510. * @first: first buffer info buffer to use
  2511. * @tx_flags: collected send information
  2512. * @hdr_len: size of the packet header
  2513. * @td_cmd: the command field in the descriptor
  2514. * @td_offset: offset for checksum or crc
  2515. **/
  2516. static inline void i40e_tx_map(struct i40e_ring *tx_ring, struct sk_buff *skb,
  2517. struct i40e_tx_buffer *first, u32 tx_flags,
  2518. const u8 hdr_len, u32 td_cmd, u32 td_offset)
  2519. {
  2520. unsigned int data_len = skb->data_len;
  2521. unsigned int size = skb_headlen(skb);
  2522. struct skb_frag_struct *frag;
  2523. struct i40e_tx_buffer *tx_bi;
  2524. struct i40e_tx_desc *tx_desc;
  2525. u16 i = tx_ring->next_to_use;
  2526. u32 td_tag = 0;
  2527. dma_addr_t dma;
  2528. u16 desc_count = 1;
  2529. if (tx_flags & I40E_TX_FLAGS_HW_VLAN) {
  2530. td_cmd |= I40E_TX_DESC_CMD_IL2TAG1;
  2531. td_tag = (tx_flags & I40E_TX_FLAGS_VLAN_MASK) >>
  2532. I40E_TX_FLAGS_VLAN_SHIFT;
  2533. }
  2534. first->tx_flags = tx_flags;
  2535. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  2536. tx_desc = I40E_TX_DESC(tx_ring, i);
  2537. tx_bi = first;
  2538. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  2539. unsigned int max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2540. if (dma_mapping_error(tx_ring->dev, dma))
  2541. goto dma_error;
  2542. /* record length, and DMA address */
  2543. dma_unmap_len_set(tx_bi, len, size);
  2544. dma_unmap_addr_set(tx_bi, dma, dma);
  2545. /* align size to end of page */
  2546. max_data += -dma & (I40E_MAX_READ_REQ_SIZE - 1);
  2547. tx_desc->buffer_addr = cpu_to_le64(dma);
  2548. while (unlikely(size > I40E_MAX_DATA_PER_TXD)) {
  2549. tx_desc->cmd_type_offset_bsz =
  2550. build_ctob(td_cmd, td_offset,
  2551. max_data, td_tag);
  2552. tx_desc++;
  2553. i++;
  2554. desc_count++;
  2555. if (i == tx_ring->count) {
  2556. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2557. i = 0;
  2558. }
  2559. dma += max_data;
  2560. size -= max_data;
  2561. max_data = I40E_MAX_DATA_PER_TXD_ALIGNED;
  2562. tx_desc->buffer_addr = cpu_to_le64(dma);
  2563. }
  2564. if (likely(!data_len))
  2565. break;
  2566. tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset,
  2567. size, td_tag);
  2568. tx_desc++;
  2569. i++;
  2570. desc_count++;
  2571. if (i == tx_ring->count) {
  2572. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2573. i = 0;
  2574. }
  2575. size = skb_frag_size(frag);
  2576. data_len -= size;
  2577. dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size,
  2578. DMA_TO_DEVICE);
  2579. tx_bi = &tx_ring->tx_bi[i];
  2580. }
  2581. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  2582. i++;
  2583. if (i == tx_ring->count)
  2584. i = 0;
  2585. tx_ring->next_to_use = i;
  2586. i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
  2587. /* write last descriptor with EOP bit */
  2588. td_cmd |= I40E_TX_DESC_CMD_EOP;
  2589. /* We can OR these values together as they both are checked against
  2590. * 4 below and at this point desc_count will be used as a boolean value
  2591. * after this if/else block.
  2592. */
  2593. desc_count |= ++tx_ring->packet_stride;
  2594. /* Algorithm to optimize tail and RS bit setting:
  2595. * if queue is stopped
  2596. * mark RS bit
  2597. * reset packet counter
  2598. * else if xmit_more is supported and is true
  2599. * advance packet counter to 4
  2600. * reset desc_count to 0
  2601. *
  2602. * if desc_count >= 4
  2603. * mark RS bit
  2604. * reset packet counter
  2605. * if desc_count > 0
  2606. * update tail
  2607. *
  2608. * Note: If there are less than 4 descriptors
  2609. * pending and interrupts were disabled the service task will
  2610. * trigger a force WB.
  2611. */
  2612. if (netif_xmit_stopped(txring_txq(tx_ring))) {
  2613. goto do_rs;
  2614. } else if (skb->xmit_more) {
  2615. /* set stride to arm on next packet and reset desc_count */
  2616. tx_ring->packet_stride = WB_STRIDE;
  2617. desc_count = 0;
  2618. } else if (desc_count >= WB_STRIDE) {
  2619. do_rs:
  2620. /* write last descriptor with RS bit set */
  2621. td_cmd |= I40E_TX_DESC_CMD_RS;
  2622. tx_ring->packet_stride = 0;
  2623. }
  2624. tx_desc->cmd_type_offset_bsz =
  2625. build_ctob(td_cmd, td_offset, size, td_tag);
  2626. /* Force memory writes to complete before letting h/w know there
  2627. * are new descriptors to fetch.
  2628. *
  2629. * We also use this memory barrier to make certain all of the
  2630. * status bits have been updated before next_to_watch is written.
  2631. */
  2632. wmb();
  2633. /* set next_to_watch value indicating a packet is present */
  2634. first->next_to_watch = tx_desc;
  2635. /* notify HW of packet */
  2636. if (desc_count) {
  2637. writel(i, tx_ring->tail);
  2638. /* we need this if more than one processor can write to our tail
  2639. * at a time, it synchronizes IO on IA64/Altix systems
  2640. */
  2641. mmiowb();
  2642. }
  2643. return;
  2644. dma_error:
  2645. dev_info(tx_ring->dev, "TX DMA map failed\n");
  2646. /* clear dma mappings for failed tx_bi map */
  2647. for (;;) {
  2648. tx_bi = &tx_ring->tx_bi[i];
  2649. i40e_unmap_and_free_tx_resource(tx_ring, tx_bi);
  2650. if (tx_bi == first)
  2651. break;
  2652. if (i == 0)
  2653. i = tx_ring->count;
  2654. i--;
  2655. }
  2656. tx_ring->next_to_use = i;
  2657. }
  2658. /**
  2659. * i40e_xmit_frame_ring - Sends buffer on Tx ring
  2660. * @skb: send buffer
  2661. * @tx_ring: ring to send buffer on
  2662. *
  2663. * Returns NETDEV_TX_OK if sent, else an error code
  2664. **/
  2665. static netdev_tx_t i40e_xmit_frame_ring(struct sk_buff *skb,
  2666. struct i40e_ring *tx_ring)
  2667. {
  2668. u64 cd_type_cmd_tso_mss = I40E_TX_DESC_DTYPE_CONTEXT;
  2669. u32 cd_tunneling = 0, cd_l2tag2 = 0;
  2670. struct i40e_tx_buffer *first;
  2671. u32 td_offset = 0;
  2672. u32 tx_flags = 0;
  2673. __be16 protocol;
  2674. u32 td_cmd = 0;
  2675. u8 hdr_len = 0;
  2676. int tso, count;
  2677. int tsyn;
  2678. /* prefetch the data, we'll need it later */
  2679. prefetch(skb->data);
  2680. i40e_trace(xmit_frame_ring, skb, tx_ring);
  2681. count = i40e_xmit_descriptor_count(skb);
  2682. if (i40e_chk_linearize(skb, count)) {
  2683. if (__skb_linearize(skb)) {
  2684. dev_kfree_skb_any(skb);
  2685. return NETDEV_TX_OK;
  2686. }
  2687. count = i40e_txd_use_count(skb->len);
  2688. tx_ring->tx_stats.tx_linearize++;
  2689. }
  2690. /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
  2691. * + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
  2692. * + 4 desc gap to avoid the cache line where head is,
  2693. * + 1 desc for context descriptor,
  2694. * otherwise try next time
  2695. */
  2696. if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
  2697. tx_ring->tx_stats.tx_busy++;
  2698. return NETDEV_TX_BUSY;
  2699. }
  2700. /* record the location of the first descriptor for this packet */
  2701. first = &tx_ring->tx_bi[tx_ring->next_to_use];
  2702. first->skb = skb;
  2703. first->bytecount = skb->len;
  2704. first->gso_segs = 1;
  2705. /* prepare the xmit flags */
  2706. if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
  2707. goto out_drop;
  2708. /* obtain protocol of skb */
  2709. protocol = vlan_get_protocol(skb);
  2710. /* setup IPv4/IPv6 offloads */
  2711. if (protocol == htons(ETH_P_IP))
  2712. tx_flags |= I40E_TX_FLAGS_IPV4;
  2713. else if (protocol == htons(ETH_P_IPV6))
  2714. tx_flags |= I40E_TX_FLAGS_IPV6;
  2715. tso = i40e_tso(first, &hdr_len, &cd_type_cmd_tso_mss);
  2716. if (tso < 0)
  2717. goto out_drop;
  2718. else if (tso)
  2719. tx_flags |= I40E_TX_FLAGS_TSO;
  2720. /* Always offload the checksum, since it's in the data descriptor */
  2721. tso = i40e_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset,
  2722. tx_ring, &cd_tunneling);
  2723. if (tso < 0)
  2724. goto out_drop;
  2725. tsyn = i40e_tsyn(tx_ring, skb, tx_flags, &cd_type_cmd_tso_mss);
  2726. if (tsyn)
  2727. tx_flags |= I40E_TX_FLAGS_TSYN;
  2728. skb_tx_timestamp(skb);
  2729. /* always enable CRC insertion offload */
  2730. td_cmd |= I40E_TX_DESC_CMD_ICRC;
  2731. i40e_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss,
  2732. cd_tunneling, cd_l2tag2);
  2733. /* Add Flow Director ATR if it's enabled.
  2734. *
  2735. * NOTE: this must always be directly before the data descriptor.
  2736. */
  2737. i40e_atr(tx_ring, skb, tx_flags);
  2738. i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len,
  2739. td_cmd, td_offset);
  2740. return NETDEV_TX_OK;
  2741. out_drop:
  2742. i40e_trace(xmit_frame_ring_drop, first->skb, tx_ring);
  2743. dev_kfree_skb_any(first->skb);
  2744. first->skb = NULL;
  2745. return NETDEV_TX_OK;
  2746. }
  2747. /**
  2748. * i40e_lan_xmit_frame - Selects the correct VSI and Tx queue to send buffer
  2749. * @skb: send buffer
  2750. * @netdev: network interface device structure
  2751. *
  2752. * Returns NETDEV_TX_OK if sent, else an error code
  2753. **/
  2754. netdev_tx_t i40e_lan_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  2755. {
  2756. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2757. struct i40e_vsi *vsi = np->vsi;
  2758. struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
  2759. /* hardware can't handle really short frames, hardware padding works
  2760. * beyond this point
  2761. */
  2762. if (skb_put_padto(skb, I40E_MIN_TX_LEN))
  2763. return NETDEV_TX_OK;
  2764. return i40e_xmit_frame_ring(skb, tx_ring);
  2765. }