sge.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/ip.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/prefetch.h>
  42. #include <linux/export.h>
  43. #include <net/xfrm.h>
  44. #include <net/ipv6.h>
  45. #include <net/tcp.h>
  46. #include <net/busy_poll.h>
  47. #ifdef CONFIG_CHELSIO_T4_FCOE
  48. #include <scsi/fc/fc_fcoe.h>
  49. #endif /* CONFIG_CHELSIO_T4_FCOE */
  50. #include "cxgb4.h"
  51. #include "t4_regs.h"
  52. #include "t4_values.h"
  53. #include "t4_msg.h"
  54. #include "t4fw_api.h"
  55. #include "cxgb4_ptp.h"
  56. #include "cxgb4_uld.h"
  57. /*
  58. * Rx buffer size. We use largish buffers if possible but settle for single
  59. * pages under memory shortage.
  60. */
  61. #if PAGE_SHIFT >= 16
  62. # define FL_PG_ORDER 0
  63. #else
  64. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  65. #endif
  66. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  67. #define RX_COPY_THRES 256
  68. #define RX_PULL_LEN 128
  69. /*
  70. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  71. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  72. */
  73. #define RX_PKT_SKB_LEN 512
  74. /*
  75. * Max number of Tx descriptors we clean up at a time. Should be modest as
  76. * freeing skbs isn't cheap and it happens while holding locks. We just need
  77. * to free packets faster than they arrive, we eventually catch up and keep
  78. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
  79. */
  80. #define MAX_TX_RECLAIM 16
  81. /*
  82. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  83. * allocating buffers isn't cheap either.
  84. */
  85. #define MAX_RX_REFILL 16U
  86. /*
  87. * Period of the Rx queue check timer. This timer is infrequent as it has
  88. * something to do only when the system experiences severe memory shortage.
  89. */
  90. #define RX_QCHECK_PERIOD (HZ / 2)
  91. /*
  92. * Period of the Tx queue check timer.
  93. */
  94. #define TX_QCHECK_PERIOD (HZ / 2)
  95. /*
  96. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  97. */
  98. #define MAX_TIMER_TX_RECLAIM 100
  99. /*
  100. * Timer index used when backing off due to memory shortage.
  101. */
  102. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  103. /*
  104. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  105. * for a full sized WR.
  106. */
  107. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  108. /*
  109. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  110. * into a WR.
  111. */
  112. #define MAX_IMM_TX_PKT_LEN 256
  113. /*
  114. * Max size of a WR sent through a control Tx queue.
  115. */
  116. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  117. struct rx_sw_desc { /* SW state per Rx descriptor */
  118. struct page *page;
  119. dma_addr_t dma_addr;
  120. };
  121. /*
  122. * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
  123. * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
  124. * We could easily support more but there doesn't seem to be much need for
  125. * that ...
  126. */
  127. #define FL_MTU_SMALL 1500
  128. #define FL_MTU_LARGE 9000
  129. static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
  130. unsigned int mtu)
  131. {
  132. struct sge *s = &adapter->sge;
  133. return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
  134. }
  135. #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
  136. #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
  137. /*
  138. * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
  139. * these to specify the buffer size as an index into the SGE Free List Buffer
  140. * Size register array. We also use bit 4, when the buffer has been unmapped
  141. * for DMA, but this is of course never sent to the hardware and is only used
  142. * to prevent double unmappings. All of the above requires that the Free List
  143. * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
  144. * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
  145. * Free List Buffer alignment is 32 bytes, this works out for us ...
  146. */
  147. enum {
  148. RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
  149. RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
  150. RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
  151. /*
  152. * XXX We shouldn't depend on being able to use these indices.
  153. * XXX Especially when some other Master PF has initialized the
  154. * XXX adapter or we use the Firmware Configuration File. We
  155. * XXX should really search through the Host Buffer Size register
  156. * XXX array for the appropriately sized buffer indices.
  157. */
  158. RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
  159. RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
  160. RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
  161. RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
  162. };
  163. static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
  164. #define MIN_NAPI_WORK 1
  165. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  166. {
  167. return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
  168. }
  169. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  170. {
  171. return !(d->dma_addr & RX_UNMAPPED_BUF);
  172. }
  173. /**
  174. * txq_avail - return the number of available slots in a Tx queue
  175. * @q: the Tx queue
  176. *
  177. * Returns the number of descriptors in a Tx queue available to write new
  178. * packets.
  179. */
  180. static inline unsigned int txq_avail(const struct sge_txq *q)
  181. {
  182. return q->size - 1 - q->in_use;
  183. }
  184. /**
  185. * fl_cap - return the capacity of a free-buffer list
  186. * @fl: the FL
  187. *
  188. * Returns the capacity of a free-buffer list. The capacity is less than
  189. * the size because one descriptor needs to be left unpopulated, otherwise
  190. * HW will think the FL is empty.
  191. */
  192. static inline unsigned int fl_cap(const struct sge_fl *fl)
  193. {
  194. return fl->size - 8; /* 1 descriptor = 8 buffers */
  195. }
  196. /**
  197. * fl_starving - return whether a Free List is starving.
  198. * @adapter: pointer to the adapter
  199. * @fl: the Free List
  200. *
  201. * Tests specified Free List to see whether the number of buffers
  202. * available to the hardware has falled below our "starvation"
  203. * threshold.
  204. */
  205. static inline bool fl_starving(const struct adapter *adapter,
  206. const struct sge_fl *fl)
  207. {
  208. const struct sge *s = &adapter->sge;
  209. return fl->avail - fl->pend_cred <= s->fl_starve_thres;
  210. }
  211. int cxgb4_map_skb(struct device *dev, const struct sk_buff *skb,
  212. dma_addr_t *addr)
  213. {
  214. const skb_frag_t *fp, *end;
  215. const struct skb_shared_info *si;
  216. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  217. if (dma_mapping_error(dev, *addr))
  218. goto out_err;
  219. si = skb_shinfo(skb);
  220. end = &si->frags[si->nr_frags];
  221. for (fp = si->frags; fp < end; fp++) {
  222. *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
  223. DMA_TO_DEVICE);
  224. if (dma_mapping_error(dev, *addr))
  225. goto unwind;
  226. }
  227. return 0;
  228. unwind:
  229. while (fp-- > si->frags)
  230. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  231. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  232. out_err:
  233. return -ENOMEM;
  234. }
  235. EXPORT_SYMBOL(cxgb4_map_skb);
  236. #ifdef CONFIG_NEED_DMA_MAP_STATE
  237. static void unmap_skb(struct device *dev, const struct sk_buff *skb,
  238. const dma_addr_t *addr)
  239. {
  240. const skb_frag_t *fp, *end;
  241. const struct skb_shared_info *si;
  242. dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
  243. si = skb_shinfo(skb);
  244. end = &si->frags[si->nr_frags];
  245. for (fp = si->frags; fp < end; fp++)
  246. dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
  247. }
  248. /**
  249. * deferred_unmap_destructor - unmap a packet when it is freed
  250. * @skb: the packet
  251. *
  252. * This is the packet destructor used for Tx packets that need to remain
  253. * mapped until they are freed rather than until their Tx descriptors are
  254. * freed.
  255. */
  256. static void deferred_unmap_destructor(struct sk_buff *skb)
  257. {
  258. unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
  259. }
  260. #endif
  261. static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
  262. const struct ulptx_sgl *sgl, const struct sge_txq *q)
  263. {
  264. const struct ulptx_sge_pair *p;
  265. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  266. if (likely(skb_headlen(skb)))
  267. dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  268. DMA_TO_DEVICE);
  269. else {
  270. dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  271. DMA_TO_DEVICE);
  272. nfrags--;
  273. }
  274. /*
  275. * the complexity below is because of the possibility of a wrap-around
  276. * in the middle of an SGL
  277. */
  278. for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
  279. if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
  280. unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  281. ntohl(p->len[0]), DMA_TO_DEVICE);
  282. dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
  283. ntohl(p->len[1]), DMA_TO_DEVICE);
  284. p++;
  285. } else if ((u8 *)p == (u8 *)q->stat) {
  286. p = (const struct ulptx_sge_pair *)q->desc;
  287. goto unmap;
  288. } else if ((u8 *)p + 8 == (u8 *)q->stat) {
  289. const __be64 *addr = (const __be64 *)q->desc;
  290. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  291. ntohl(p->len[0]), DMA_TO_DEVICE);
  292. dma_unmap_page(dev, be64_to_cpu(addr[1]),
  293. ntohl(p->len[1]), DMA_TO_DEVICE);
  294. p = (const struct ulptx_sge_pair *)&addr[2];
  295. } else {
  296. const __be64 *addr = (const __be64 *)q->desc;
  297. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  298. ntohl(p->len[0]), DMA_TO_DEVICE);
  299. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  300. ntohl(p->len[1]), DMA_TO_DEVICE);
  301. p = (const struct ulptx_sge_pair *)&addr[1];
  302. }
  303. }
  304. if (nfrags) {
  305. __be64 addr;
  306. if ((u8 *)p == (u8 *)q->stat)
  307. p = (const struct ulptx_sge_pair *)q->desc;
  308. addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
  309. *(const __be64 *)q->desc;
  310. dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
  311. DMA_TO_DEVICE);
  312. }
  313. }
  314. /**
  315. * free_tx_desc - reclaims Tx descriptors and their buffers
  316. * @adapter: the adapter
  317. * @q: the Tx queue to reclaim descriptors from
  318. * @n: the number of descriptors to reclaim
  319. * @unmap: whether the buffers should be unmapped for DMA
  320. *
  321. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  322. * Tx buffers. Called with the Tx queue lock held.
  323. */
  324. void free_tx_desc(struct adapter *adap, struct sge_txq *q,
  325. unsigned int n, bool unmap)
  326. {
  327. struct tx_sw_desc *d;
  328. unsigned int cidx = q->cidx;
  329. struct device *dev = adap->pdev_dev;
  330. d = &q->sdesc[cidx];
  331. while (n--) {
  332. if (d->skb) { /* an SGL is present */
  333. if (unmap)
  334. unmap_sgl(dev, d->skb, d->sgl, q);
  335. dev_consume_skb_any(d->skb);
  336. d->skb = NULL;
  337. }
  338. ++d;
  339. if (++cidx == q->size) {
  340. cidx = 0;
  341. d = q->sdesc;
  342. }
  343. }
  344. q->cidx = cidx;
  345. }
  346. /*
  347. * Return the number of reclaimable descriptors in a Tx queue.
  348. */
  349. static inline int reclaimable(const struct sge_txq *q)
  350. {
  351. int hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
  352. hw_cidx -= q->cidx;
  353. return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
  354. }
  355. /**
  356. * cxgb4_reclaim_completed_tx - reclaims completed Tx descriptors
  357. * @adap: the adapter
  358. * @q: the Tx queue to reclaim completed descriptors from
  359. * @unmap: whether the buffers should be unmapped for DMA
  360. *
  361. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  362. * and frees the associated buffers if possible. Called with the Tx
  363. * queue locked.
  364. */
  365. inline void cxgb4_reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  366. bool unmap)
  367. {
  368. int avail = reclaimable(q);
  369. if (avail) {
  370. /*
  371. * Limit the amount of clean up work we do at a time to keep
  372. * the Tx lock hold time O(1).
  373. */
  374. if (avail > MAX_TX_RECLAIM)
  375. avail = MAX_TX_RECLAIM;
  376. free_tx_desc(adap, q, avail, unmap);
  377. q->in_use -= avail;
  378. }
  379. }
  380. EXPORT_SYMBOL(cxgb4_reclaim_completed_tx);
  381. static inline int get_buf_size(struct adapter *adapter,
  382. const struct rx_sw_desc *d)
  383. {
  384. struct sge *s = &adapter->sge;
  385. unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
  386. int buf_size;
  387. switch (rx_buf_size_idx) {
  388. case RX_SMALL_PG_BUF:
  389. buf_size = PAGE_SIZE;
  390. break;
  391. case RX_LARGE_PG_BUF:
  392. buf_size = PAGE_SIZE << s->fl_pg_order;
  393. break;
  394. case RX_SMALL_MTU_BUF:
  395. buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
  396. break;
  397. case RX_LARGE_MTU_BUF:
  398. buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
  399. break;
  400. default:
  401. BUG_ON(1);
  402. }
  403. return buf_size;
  404. }
  405. /**
  406. * free_rx_bufs - free the Rx buffers on an SGE free list
  407. * @adap: the adapter
  408. * @q: the SGE free list to free buffers from
  409. * @n: how many buffers to free
  410. *
  411. * Release the next @n buffers on an SGE free-buffer Rx queue. The
  412. * buffers must be made inaccessible to HW before calling this function.
  413. */
  414. static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
  415. {
  416. while (n--) {
  417. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  418. if (is_buf_mapped(d))
  419. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  420. get_buf_size(adap, d),
  421. PCI_DMA_FROMDEVICE);
  422. put_page(d->page);
  423. d->page = NULL;
  424. if (++q->cidx == q->size)
  425. q->cidx = 0;
  426. q->avail--;
  427. }
  428. }
  429. /**
  430. * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
  431. * @adap: the adapter
  432. * @q: the SGE free list
  433. *
  434. * Unmap the current buffer on an SGE free-buffer Rx queue. The
  435. * buffer must be made inaccessible to HW before calling this function.
  436. *
  437. * This is similar to @free_rx_bufs above but does not free the buffer.
  438. * Do note that the FL still loses any further access to the buffer.
  439. */
  440. static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
  441. {
  442. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  443. if (is_buf_mapped(d))
  444. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  445. get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
  446. d->page = NULL;
  447. if (++q->cidx == q->size)
  448. q->cidx = 0;
  449. q->avail--;
  450. }
  451. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  452. {
  453. if (q->pend_cred >= 8) {
  454. u32 val = adap->params.arch.sge_fl_db;
  455. if (is_t4(adap->params.chip))
  456. val |= PIDX_V(q->pend_cred / 8);
  457. else
  458. val |= PIDX_T5_V(q->pend_cred / 8);
  459. /* Make sure all memory writes to the Free List queue are
  460. * committed before we tell the hardware about them.
  461. */
  462. wmb();
  463. /* If we don't have access to the new User Doorbell (T5+), use
  464. * the old doorbell mechanism; otherwise use the new BAR2
  465. * mechanism.
  466. */
  467. if (unlikely(q->bar2_addr == NULL)) {
  468. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  469. val | QID_V(q->cntxt_id));
  470. } else {
  471. writel(val | QID_V(q->bar2_qid),
  472. q->bar2_addr + SGE_UDB_KDOORBELL);
  473. /* This Write memory Barrier will force the write to
  474. * the User Doorbell area to be flushed.
  475. */
  476. wmb();
  477. }
  478. q->pend_cred &= 7;
  479. }
  480. }
  481. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  482. dma_addr_t mapping)
  483. {
  484. sd->page = pg;
  485. sd->dma_addr = mapping; /* includes size low bits */
  486. }
  487. /**
  488. * refill_fl - refill an SGE Rx buffer ring
  489. * @adap: the adapter
  490. * @q: the ring to refill
  491. * @n: the number of new buffers to allocate
  492. * @gfp: the gfp flags for the allocations
  493. *
  494. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  495. * allocated with the supplied gfp flags. The caller must assure that
  496. * @n does not exceed the queue's capacity. If afterwards the queue is
  497. * found critically low mark it as starving in the bitmap of starving FLs.
  498. *
  499. * Returns the number of buffers allocated.
  500. */
  501. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  502. gfp_t gfp)
  503. {
  504. struct sge *s = &adap->sge;
  505. struct page *pg;
  506. dma_addr_t mapping;
  507. unsigned int cred = q->avail;
  508. __be64 *d = &q->desc[q->pidx];
  509. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  510. int node;
  511. #ifdef CONFIG_DEBUG_FS
  512. if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
  513. goto out;
  514. #endif
  515. gfp |= __GFP_NOWARN;
  516. node = dev_to_node(adap->pdev_dev);
  517. if (s->fl_pg_order == 0)
  518. goto alloc_small_pages;
  519. /*
  520. * Prefer large buffers
  521. */
  522. while (n) {
  523. pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
  524. if (unlikely(!pg)) {
  525. q->large_alloc_failed++;
  526. break; /* fall back to single pages */
  527. }
  528. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  529. PAGE_SIZE << s->fl_pg_order,
  530. PCI_DMA_FROMDEVICE);
  531. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  532. __free_pages(pg, s->fl_pg_order);
  533. q->mapping_err++;
  534. goto out; /* do not try small pages for this error */
  535. }
  536. mapping |= RX_LARGE_PG_BUF;
  537. *d++ = cpu_to_be64(mapping);
  538. set_rx_sw_desc(sd, pg, mapping);
  539. sd++;
  540. q->avail++;
  541. if (++q->pidx == q->size) {
  542. q->pidx = 0;
  543. sd = q->sdesc;
  544. d = q->desc;
  545. }
  546. n--;
  547. }
  548. alloc_small_pages:
  549. while (n--) {
  550. pg = alloc_pages_node(node, gfp, 0);
  551. if (unlikely(!pg)) {
  552. q->alloc_failed++;
  553. break;
  554. }
  555. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  556. PCI_DMA_FROMDEVICE);
  557. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  558. put_page(pg);
  559. q->mapping_err++;
  560. goto out;
  561. }
  562. *d++ = cpu_to_be64(mapping);
  563. set_rx_sw_desc(sd, pg, mapping);
  564. sd++;
  565. q->avail++;
  566. if (++q->pidx == q->size) {
  567. q->pidx = 0;
  568. sd = q->sdesc;
  569. d = q->desc;
  570. }
  571. }
  572. out: cred = q->avail - cred;
  573. q->pend_cred += cred;
  574. ring_fl_db(adap, q);
  575. if (unlikely(fl_starving(adap, q))) {
  576. smp_wmb();
  577. q->low++;
  578. set_bit(q->cntxt_id - adap->sge.egr_start,
  579. adap->sge.starving_fl);
  580. }
  581. return cred;
  582. }
  583. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  584. {
  585. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  586. GFP_ATOMIC);
  587. }
  588. /**
  589. * alloc_ring - allocate resources for an SGE descriptor ring
  590. * @dev: the PCI device's core device
  591. * @nelem: the number of descriptors
  592. * @elem_size: the size of each descriptor
  593. * @sw_size: the size of the SW state associated with each ring element
  594. * @phys: the physical address of the allocated ring
  595. * @metadata: address of the array holding the SW state for the ring
  596. * @stat_size: extra space in HW ring for status information
  597. * @node: preferred node for memory allocations
  598. *
  599. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  600. * free buffer lists, or response queues. Each SGE ring requires
  601. * space for its HW descriptors plus, optionally, space for the SW state
  602. * associated with each HW entry (the metadata). The function returns
  603. * three values: the virtual address for the HW ring (the return value
  604. * of the function), the bus address of the HW ring, and the address
  605. * of the SW ring.
  606. */
  607. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  608. size_t sw_size, dma_addr_t *phys, void *metadata,
  609. size_t stat_size, int node)
  610. {
  611. size_t len = nelem * elem_size + stat_size;
  612. void *s = NULL;
  613. void *p = dma_zalloc_coherent(dev, len, phys, GFP_KERNEL);
  614. if (!p)
  615. return NULL;
  616. if (sw_size) {
  617. s = kcalloc_node(sw_size, nelem, GFP_KERNEL, node);
  618. if (!s) {
  619. dma_free_coherent(dev, len, p, *phys);
  620. return NULL;
  621. }
  622. }
  623. if (metadata)
  624. *(void **)metadata = s;
  625. return p;
  626. }
  627. /**
  628. * sgl_len - calculates the size of an SGL of the given capacity
  629. * @n: the number of SGL entries
  630. *
  631. * Calculates the number of flits needed for a scatter/gather list that
  632. * can hold the given number of entries.
  633. */
  634. static inline unsigned int sgl_len(unsigned int n)
  635. {
  636. /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
  637. * addresses. The DSGL Work Request starts off with a 32-bit DSGL
  638. * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
  639. * repeated sequences of { Length[i], Length[i+1], Address[i],
  640. * Address[i+1] } (this ensures that all addresses are on 64-bit
  641. * boundaries). If N is even, then Length[N+1] should be set to 0 and
  642. * Address[N+1] is omitted.
  643. *
  644. * The following calculation incorporates all of the above. It's
  645. * somewhat hard to follow but, briefly: the "+2" accounts for the
  646. * first two flits which include the DSGL header, Length0 and
  647. * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
  648. * flits for every pair of the remaining N) +1 if (n-1) is odd; and
  649. * finally the "+((n-1)&1)" adds the one remaining flit needed if
  650. * (n-1) is odd ...
  651. */
  652. n--;
  653. return (3 * n) / 2 + (n & 1) + 2;
  654. }
  655. /**
  656. * flits_to_desc - returns the num of Tx descriptors for the given flits
  657. * @n: the number of flits
  658. *
  659. * Returns the number of Tx descriptors needed for the supplied number
  660. * of flits.
  661. */
  662. static inline unsigned int flits_to_desc(unsigned int n)
  663. {
  664. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  665. return DIV_ROUND_UP(n, 8);
  666. }
  667. /**
  668. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  669. * @skb: the packet
  670. *
  671. * Returns whether an Ethernet packet is small enough to fit as
  672. * immediate data. Return value corresponds to headroom required.
  673. */
  674. static inline int is_eth_imm(const struct sk_buff *skb, unsigned int chip_ver)
  675. {
  676. int hdrlen = 0;
  677. if (skb->encapsulation && skb_shinfo(skb)->gso_size &&
  678. chip_ver > CHELSIO_T5) {
  679. hdrlen = sizeof(struct cpl_tx_tnl_lso);
  680. hdrlen += sizeof(struct cpl_tx_pkt_core);
  681. } else {
  682. hdrlen = skb_shinfo(skb)->gso_size ?
  683. sizeof(struct cpl_tx_pkt_lso_core) : 0;
  684. hdrlen += sizeof(struct cpl_tx_pkt);
  685. }
  686. if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
  687. return hdrlen;
  688. return 0;
  689. }
  690. /**
  691. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  692. * @skb: the packet
  693. *
  694. * Returns the number of flits needed for a Tx WR for the given Ethernet
  695. * packet, including the needed WR and CPL headers.
  696. */
  697. static inline unsigned int calc_tx_flits(const struct sk_buff *skb,
  698. unsigned int chip_ver)
  699. {
  700. unsigned int flits;
  701. int hdrlen = is_eth_imm(skb, chip_ver);
  702. /* If the skb is small enough, we can pump it out as a work request
  703. * with only immediate data. In that case we just have to have the
  704. * TX Packet header plus the skb data in the Work Request.
  705. */
  706. if (hdrlen)
  707. return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
  708. /* Otherwise, we're going to have to construct a Scatter gather list
  709. * of the skb body and fragments. We also include the flits necessary
  710. * for the TX Packet Work Request and CPL. We always have a firmware
  711. * Write Header (incorporated as part of the cpl_tx_pkt_lso and
  712. * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
  713. * message or, if we're doing a Large Send Offload, an LSO CPL message
  714. * with an embedded TX Packet Write CPL message.
  715. */
  716. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  717. if (skb_shinfo(skb)->gso_size) {
  718. if (skb->encapsulation && chip_ver > CHELSIO_T5)
  719. hdrlen = sizeof(struct fw_eth_tx_pkt_wr) +
  720. sizeof(struct cpl_tx_tnl_lso);
  721. else
  722. hdrlen = sizeof(struct fw_eth_tx_pkt_wr) +
  723. sizeof(struct cpl_tx_pkt_lso_core);
  724. hdrlen += sizeof(struct cpl_tx_pkt_core);
  725. flits += (hdrlen / sizeof(__be64));
  726. } else {
  727. flits += (sizeof(struct fw_eth_tx_pkt_wr) +
  728. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  729. }
  730. return flits;
  731. }
  732. /**
  733. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  734. * @skb: the packet
  735. *
  736. * Returns the number of Tx descriptors needed for the given Ethernet
  737. * packet, including the needed WR and CPL headers.
  738. */
  739. static inline unsigned int calc_tx_descs(const struct sk_buff *skb,
  740. unsigned int chip_ver)
  741. {
  742. return flits_to_desc(calc_tx_flits(skb, chip_ver));
  743. }
  744. /**
  745. * cxgb4_write_sgl - populate a scatter/gather list for a packet
  746. * @skb: the packet
  747. * @q: the Tx queue we are writing into
  748. * @sgl: starting location for writing the SGL
  749. * @end: points right after the end of the SGL
  750. * @start: start offset into skb main-body data to include in the SGL
  751. * @addr: the list of bus addresses for the SGL elements
  752. *
  753. * Generates a gather list for the buffers that make up a packet.
  754. * The caller must provide adequate space for the SGL that will be written.
  755. * The SGL includes all of the packet's page fragments and the data in its
  756. * main body except for the first @start bytes. @sgl must be 16-byte
  757. * aligned and within a Tx descriptor with available space. @end points
  758. * right after the end of the SGL but does not account for any potential
  759. * wrap around, i.e., @end > @sgl.
  760. */
  761. void cxgb4_write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  762. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  763. const dma_addr_t *addr)
  764. {
  765. unsigned int i, len;
  766. struct ulptx_sge_pair *to;
  767. const struct skb_shared_info *si = skb_shinfo(skb);
  768. unsigned int nfrags = si->nr_frags;
  769. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  770. len = skb_headlen(skb) - start;
  771. if (likely(len)) {
  772. sgl->len0 = htonl(len);
  773. sgl->addr0 = cpu_to_be64(addr[0] + start);
  774. nfrags++;
  775. } else {
  776. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  777. sgl->addr0 = cpu_to_be64(addr[1]);
  778. }
  779. sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  780. ULPTX_NSGE_V(nfrags));
  781. if (likely(--nfrags == 0))
  782. return;
  783. /*
  784. * Most of the complexity below deals with the possibility we hit the
  785. * end of the queue in the middle of writing the SGL. For this case
  786. * only we create the SGL in a temporary buffer and then copy it.
  787. */
  788. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  789. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  790. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  791. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  792. to->addr[0] = cpu_to_be64(addr[i]);
  793. to->addr[1] = cpu_to_be64(addr[++i]);
  794. }
  795. if (nfrags) {
  796. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  797. to->len[1] = cpu_to_be32(0);
  798. to->addr[0] = cpu_to_be64(addr[i + 1]);
  799. }
  800. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  801. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  802. if (likely(part0))
  803. memcpy(sgl->sge, buf, part0);
  804. part1 = (u8 *)end - (u8 *)q->stat;
  805. memcpy(q->desc, (u8 *)buf + part0, part1);
  806. end = (void *)q->desc + part1;
  807. }
  808. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  809. *end = 0;
  810. }
  811. EXPORT_SYMBOL(cxgb4_write_sgl);
  812. /* This function copies 64 byte coalesced work request to
  813. * memory mapped BAR2 space. For coalesced WR SGE fetches
  814. * data from the FIFO instead of from Host.
  815. */
  816. static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
  817. {
  818. int count = 8;
  819. while (count) {
  820. writeq(*src, dst);
  821. src++;
  822. dst++;
  823. count--;
  824. }
  825. }
  826. /**
  827. * cxgb4_ring_tx_db - check and potentially ring a Tx queue's doorbell
  828. * @adap: the adapter
  829. * @q: the Tx queue
  830. * @n: number of new descriptors to give to HW
  831. *
  832. * Ring the doorbel for a Tx queue.
  833. */
  834. inline void cxgb4_ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  835. {
  836. /* Make sure that all writes to the TX Descriptors are committed
  837. * before we tell the hardware about them.
  838. */
  839. wmb();
  840. /* If we don't have access to the new User Doorbell (T5+), use the old
  841. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  842. */
  843. if (unlikely(q->bar2_addr == NULL)) {
  844. u32 val = PIDX_V(n);
  845. unsigned long flags;
  846. /* For T4 we need to participate in the Doorbell Recovery
  847. * mechanism.
  848. */
  849. spin_lock_irqsave(&q->db_lock, flags);
  850. if (!q->db_disabled)
  851. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  852. QID_V(q->cntxt_id) | val);
  853. else
  854. q->db_pidx_inc += n;
  855. q->db_pidx = q->pidx;
  856. spin_unlock_irqrestore(&q->db_lock, flags);
  857. } else {
  858. u32 val = PIDX_T5_V(n);
  859. /* T4 and later chips share the same PIDX field offset within
  860. * the doorbell, but T5 and later shrank the field in order to
  861. * gain a bit for Doorbell Priority. The field was absurdly
  862. * large in the first place (14 bits) so we just use the T5
  863. * and later limits and warn if a Queue ID is too large.
  864. */
  865. WARN_ON(val & DBPRIO_F);
  866. /* If we're only writing a single TX Descriptor and we can use
  867. * Inferred QID registers, we can use the Write Combining
  868. * Gather Buffer; otherwise we use the simple doorbell.
  869. */
  870. if (n == 1 && q->bar2_qid == 0) {
  871. int index = (q->pidx
  872. ? (q->pidx - 1)
  873. : (q->size - 1));
  874. u64 *wr = (u64 *)&q->desc[index];
  875. cxgb_pio_copy((u64 __iomem *)
  876. (q->bar2_addr + SGE_UDB_WCDOORBELL),
  877. wr);
  878. } else {
  879. writel(val | QID_V(q->bar2_qid),
  880. q->bar2_addr + SGE_UDB_KDOORBELL);
  881. }
  882. /* This Write Memory Barrier will force the write to the User
  883. * Doorbell area to be flushed. This is needed to prevent
  884. * writes on different CPUs for the same queue from hitting
  885. * the adapter out of order. This is required when some Work
  886. * Requests take the Write Combine Gather Buffer path (user
  887. * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
  888. * take the traditional path where we simply increment the
  889. * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
  890. * hardware DMA read the actual Work Request.
  891. */
  892. wmb();
  893. }
  894. }
  895. EXPORT_SYMBOL(cxgb4_ring_tx_db);
  896. /**
  897. * cxgb4_inline_tx_skb - inline a packet's data into Tx descriptors
  898. * @skb: the packet
  899. * @q: the Tx queue where the packet will be inlined
  900. * @pos: starting position in the Tx queue where to inline the packet
  901. *
  902. * Inline a packet's contents directly into Tx descriptors, starting at
  903. * the given position within the Tx DMA ring.
  904. * Most of the complexity of this operation is dealing with wrap arounds
  905. * in the middle of the packet we want to inline.
  906. */
  907. void cxgb4_inline_tx_skb(const struct sk_buff *skb,
  908. const struct sge_txq *q, void *pos)
  909. {
  910. int left = (void *)q->stat - pos;
  911. u64 *p;
  912. if (likely(skb->len <= left)) {
  913. if (likely(!skb->data_len))
  914. skb_copy_from_linear_data(skb, pos, skb->len);
  915. else
  916. skb_copy_bits(skb, 0, pos, skb->len);
  917. pos += skb->len;
  918. } else {
  919. skb_copy_bits(skb, 0, pos, left);
  920. skb_copy_bits(skb, left, q->desc, skb->len - left);
  921. pos = (void *)q->desc + (skb->len - left);
  922. }
  923. /* 0-pad to multiple of 16 */
  924. p = PTR_ALIGN(pos, 8);
  925. if ((uintptr_t)p & 8)
  926. *p = 0;
  927. }
  928. EXPORT_SYMBOL(cxgb4_inline_tx_skb);
  929. static void *inline_tx_skb_header(const struct sk_buff *skb,
  930. const struct sge_txq *q, void *pos,
  931. int length)
  932. {
  933. u64 *p;
  934. int left = (void *)q->stat - pos;
  935. if (likely(length <= left)) {
  936. memcpy(pos, skb->data, length);
  937. pos += length;
  938. } else {
  939. memcpy(pos, skb->data, left);
  940. memcpy(q->desc, skb->data + left, length - left);
  941. pos = (void *)q->desc + (length - left);
  942. }
  943. /* 0-pad to multiple of 16 */
  944. p = PTR_ALIGN(pos, 8);
  945. if ((uintptr_t)p & 8) {
  946. *p = 0;
  947. return p + 1;
  948. }
  949. return p;
  950. }
  951. /*
  952. * Figure out what HW csum a packet wants and return the appropriate control
  953. * bits.
  954. */
  955. static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
  956. {
  957. int csum_type;
  958. bool inner_hdr_csum = false;
  959. u16 proto, ver;
  960. if (skb->encapsulation &&
  961. (CHELSIO_CHIP_VERSION(chip) > CHELSIO_T5))
  962. inner_hdr_csum = true;
  963. if (inner_hdr_csum) {
  964. ver = inner_ip_hdr(skb)->version;
  965. proto = (ver == 4) ? inner_ip_hdr(skb)->protocol :
  966. inner_ipv6_hdr(skb)->nexthdr;
  967. } else {
  968. ver = ip_hdr(skb)->version;
  969. proto = (ver == 4) ? ip_hdr(skb)->protocol :
  970. ipv6_hdr(skb)->nexthdr;
  971. }
  972. if (ver == 4) {
  973. if (proto == IPPROTO_TCP)
  974. csum_type = TX_CSUM_TCPIP;
  975. else if (proto == IPPROTO_UDP)
  976. csum_type = TX_CSUM_UDPIP;
  977. else {
  978. nocsum: /*
  979. * unknown protocol, disable HW csum
  980. * and hope a bad packet is detected
  981. */
  982. return TXPKT_L4CSUM_DIS_F;
  983. }
  984. } else {
  985. /*
  986. * this doesn't work with extension headers
  987. */
  988. if (proto == IPPROTO_TCP)
  989. csum_type = TX_CSUM_TCPIP6;
  990. else if (proto == IPPROTO_UDP)
  991. csum_type = TX_CSUM_UDPIP6;
  992. else
  993. goto nocsum;
  994. }
  995. if (likely(csum_type >= TX_CSUM_TCPIP)) {
  996. int eth_hdr_len, l4_len;
  997. u64 hdr_len;
  998. if (inner_hdr_csum) {
  999. /* This allows checksum offload for all encapsulated
  1000. * packets like GRE etc..
  1001. */
  1002. l4_len = skb_inner_network_header_len(skb);
  1003. eth_hdr_len = skb_inner_network_offset(skb) - ETH_HLEN;
  1004. } else {
  1005. l4_len = skb_network_header_len(skb);
  1006. eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
  1007. }
  1008. hdr_len = TXPKT_IPHDR_LEN_V(l4_len);
  1009. if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
  1010. hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  1011. else
  1012. hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  1013. return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
  1014. } else {
  1015. int start = skb_transport_offset(skb);
  1016. return TXPKT_CSUM_TYPE_V(csum_type) |
  1017. TXPKT_CSUM_START_V(start) |
  1018. TXPKT_CSUM_LOC_V(start + skb->csum_offset);
  1019. }
  1020. }
  1021. static void eth_txq_stop(struct sge_eth_txq *q)
  1022. {
  1023. netif_tx_stop_queue(q->txq);
  1024. q->q.stops++;
  1025. }
  1026. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  1027. {
  1028. q->in_use += n;
  1029. q->pidx += n;
  1030. if (q->pidx >= q->size)
  1031. q->pidx -= q->size;
  1032. }
  1033. #ifdef CONFIG_CHELSIO_T4_FCOE
  1034. static inline int
  1035. cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
  1036. const struct port_info *pi, u64 *cntrl)
  1037. {
  1038. const struct cxgb_fcoe *fcoe = &pi->fcoe;
  1039. if (!(fcoe->flags & CXGB_FCOE_ENABLED))
  1040. return 0;
  1041. if (skb->protocol != htons(ETH_P_FCOE))
  1042. return 0;
  1043. skb_reset_mac_header(skb);
  1044. skb->mac_len = sizeof(struct ethhdr);
  1045. skb_set_network_header(skb, skb->mac_len);
  1046. skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
  1047. if (!cxgb_fcoe_sof_eof_supported(adap, skb))
  1048. return -ENOTSUPP;
  1049. /* FC CRC offload */
  1050. *cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
  1051. TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
  1052. TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
  1053. TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
  1054. TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
  1055. return 0;
  1056. }
  1057. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1058. /* Returns tunnel type if hardware supports offloading of the same.
  1059. * It is called only for T5 and onwards.
  1060. */
  1061. enum cpl_tx_tnl_lso_type cxgb_encap_offload_supported(struct sk_buff *skb)
  1062. {
  1063. u8 l4_hdr = 0;
  1064. enum cpl_tx_tnl_lso_type tnl_type = TX_TNL_TYPE_OPAQUE;
  1065. struct port_info *pi = netdev_priv(skb->dev);
  1066. struct adapter *adapter = pi->adapter;
  1067. if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
  1068. skb->inner_protocol != htons(ETH_P_TEB))
  1069. return tnl_type;
  1070. switch (vlan_get_protocol(skb)) {
  1071. case htons(ETH_P_IP):
  1072. l4_hdr = ip_hdr(skb)->protocol;
  1073. break;
  1074. case htons(ETH_P_IPV6):
  1075. l4_hdr = ipv6_hdr(skb)->nexthdr;
  1076. break;
  1077. default:
  1078. return tnl_type;
  1079. }
  1080. switch (l4_hdr) {
  1081. case IPPROTO_UDP:
  1082. if (adapter->vxlan_port == udp_hdr(skb)->dest)
  1083. tnl_type = TX_TNL_TYPE_VXLAN;
  1084. else if (adapter->geneve_port == udp_hdr(skb)->dest)
  1085. tnl_type = TX_TNL_TYPE_GENEVE;
  1086. break;
  1087. default:
  1088. return tnl_type;
  1089. }
  1090. return tnl_type;
  1091. }
  1092. static inline void t6_fill_tnl_lso(struct sk_buff *skb,
  1093. struct cpl_tx_tnl_lso *tnl_lso,
  1094. enum cpl_tx_tnl_lso_type tnl_type)
  1095. {
  1096. u32 val;
  1097. int in_eth_xtra_len;
  1098. int l3hdr_len = skb_network_header_len(skb);
  1099. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1100. const struct skb_shared_info *ssi = skb_shinfo(skb);
  1101. bool v6 = (ip_hdr(skb)->version == 6);
  1102. val = CPL_TX_TNL_LSO_OPCODE_V(CPL_TX_TNL_LSO) |
  1103. CPL_TX_TNL_LSO_FIRST_F |
  1104. CPL_TX_TNL_LSO_LAST_F |
  1105. (v6 ? CPL_TX_TNL_LSO_IPV6OUT_F : 0) |
  1106. CPL_TX_TNL_LSO_ETHHDRLENOUT_V(eth_xtra_len / 4) |
  1107. CPL_TX_TNL_LSO_IPHDRLENOUT_V(l3hdr_len / 4) |
  1108. (v6 ? 0 : CPL_TX_TNL_LSO_IPHDRCHKOUT_F) |
  1109. CPL_TX_TNL_LSO_IPLENSETOUT_F |
  1110. (v6 ? 0 : CPL_TX_TNL_LSO_IPIDINCOUT_F);
  1111. tnl_lso->op_to_IpIdSplitOut = htonl(val);
  1112. tnl_lso->IpIdOffsetOut = 0;
  1113. /* Get the tunnel header length */
  1114. val = skb_inner_mac_header(skb) - skb_mac_header(skb);
  1115. in_eth_xtra_len = skb_inner_network_header(skb) -
  1116. skb_inner_mac_header(skb) - ETH_HLEN;
  1117. switch (tnl_type) {
  1118. case TX_TNL_TYPE_VXLAN:
  1119. case TX_TNL_TYPE_GENEVE:
  1120. tnl_lso->UdpLenSetOut_to_TnlHdrLen =
  1121. htons(CPL_TX_TNL_LSO_UDPCHKCLROUT_F |
  1122. CPL_TX_TNL_LSO_UDPLENSETOUT_F);
  1123. break;
  1124. default:
  1125. tnl_lso->UdpLenSetOut_to_TnlHdrLen = 0;
  1126. break;
  1127. }
  1128. tnl_lso->UdpLenSetOut_to_TnlHdrLen |=
  1129. htons(CPL_TX_TNL_LSO_TNLHDRLEN_V(val) |
  1130. CPL_TX_TNL_LSO_TNLTYPE_V(tnl_type));
  1131. tnl_lso->r1 = 0;
  1132. val = CPL_TX_TNL_LSO_ETHHDRLEN_V(in_eth_xtra_len / 4) |
  1133. CPL_TX_TNL_LSO_IPV6_V(inner_ip_hdr(skb)->version == 6) |
  1134. CPL_TX_TNL_LSO_IPHDRLEN_V(skb_inner_network_header_len(skb) / 4) |
  1135. CPL_TX_TNL_LSO_TCPHDRLEN_V(inner_tcp_hdrlen(skb) / 4);
  1136. tnl_lso->Flow_to_TcpHdrLen = htonl(val);
  1137. tnl_lso->IpIdOffset = htons(0);
  1138. tnl_lso->IpIdSplit_to_Mss = htons(CPL_TX_TNL_LSO_MSS_V(ssi->gso_size));
  1139. tnl_lso->TCPSeqOffset = htonl(0);
  1140. tnl_lso->EthLenOffset_Size = htonl(CPL_TX_TNL_LSO_SIZE_V(skb->len));
  1141. }
  1142. /**
  1143. * cxgb4_eth_xmit - add a packet to an Ethernet Tx queue
  1144. * @skb: the packet
  1145. * @dev: the egress net device
  1146. *
  1147. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  1148. */
  1149. static netdev_tx_t cxgb4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  1150. {
  1151. u32 wr_mid, ctrl0, op;
  1152. u64 cntrl, *end, *sgl;
  1153. int qidx, credits;
  1154. unsigned int flits, ndesc;
  1155. struct adapter *adap;
  1156. struct sge_eth_txq *q;
  1157. const struct port_info *pi;
  1158. struct fw_eth_tx_pkt_wr *wr;
  1159. struct cpl_tx_pkt_core *cpl;
  1160. const struct skb_shared_info *ssi;
  1161. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  1162. bool immediate = false;
  1163. int len, max_pkt_len;
  1164. bool ptp_enabled = is_ptp_enabled(skb, dev);
  1165. unsigned int chip_ver;
  1166. enum cpl_tx_tnl_lso_type tnl_type = TX_TNL_TYPE_OPAQUE;
  1167. #ifdef CONFIG_CHELSIO_T4_FCOE
  1168. int err;
  1169. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1170. /*
  1171. * The chip min packet length is 10 octets but play safe and reject
  1172. * anything shorter than an Ethernet header.
  1173. */
  1174. if (unlikely(skb->len < ETH_HLEN)) {
  1175. out_free: dev_kfree_skb_any(skb);
  1176. return NETDEV_TX_OK;
  1177. }
  1178. /* Discard the packet if the length is greater than mtu */
  1179. max_pkt_len = ETH_HLEN + dev->mtu;
  1180. if (skb_vlan_tagged(skb))
  1181. max_pkt_len += VLAN_HLEN;
  1182. if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
  1183. goto out_free;
  1184. pi = netdev_priv(dev);
  1185. adap = pi->adapter;
  1186. ssi = skb_shinfo(skb);
  1187. #ifdef CONFIG_CHELSIO_IPSEC_INLINE
  1188. if (xfrm_offload(skb) && !ssi->gso_size)
  1189. return adap->uld[CXGB4_ULD_CRYPTO].tx_handler(skb, dev);
  1190. #endif /* CHELSIO_IPSEC_INLINE */
  1191. qidx = skb_get_queue_mapping(skb);
  1192. if (ptp_enabled) {
  1193. spin_lock(&adap->ptp_lock);
  1194. if (!(adap->ptp_tx_skb)) {
  1195. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1196. adap->ptp_tx_skb = skb_get(skb);
  1197. } else {
  1198. spin_unlock(&adap->ptp_lock);
  1199. goto out_free;
  1200. }
  1201. q = &adap->sge.ptptxq;
  1202. } else {
  1203. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  1204. }
  1205. skb_tx_timestamp(skb);
  1206. cxgb4_reclaim_completed_tx(adap, &q->q, true);
  1207. cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
  1208. #ifdef CONFIG_CHELSIO_T4_FCOE
  1209. err = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
  1210. if (unlikely(err == -ENOTSUPP)) {
  1211. if (ptp_enabled)
  1212. spin_unlock(&adap->ptp_lock);
  1213. goto out_free;
  1214. }
  1215. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1216. chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
  1217. flits = calc_tx_flits(skb, chip_ver);
  1218. ndesc = flits_to_desc(flits);
  1219. credits = txq_avail(&q->q) - ndesc;
  1220. if (unlikely(credits < 0)) {
  1221. eth_txq_stop(q);
  1222. dev_err(adap->pdev_dev,
  1223. "%s: Tx ring %u full while queue awake!\n",
  1224. dev->name, qidx);
  1225. if (ptp_enabled)
  1226. spin_unlock(&adap->ptp_lock);
  1227. return NETDEV_TX_BUSY;
  1228. }
  1229. if (is_eth_imm(skb, chip_ver))
  1230. immediate = true;
  1231. if (skb->encapsulation && chip_ver > CHELSIO_T5)
  1232. tnl_type = cxgb_encap_offload_supported(skb);
  1233. if (!immediate &&
  1234. unlikely(cxgb4_map_skb(adap->pdev_dev, skb, addr) < 0)) {
  1235. q->mapping_err++;
  1236. if (ptp_enabled)
  1237. spin_unlock(&adap->ptp_lock);
  1238. goto out_free;
  1239. }
  1240. wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
  1241. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  1242. eth_txq_stop(q);
  1243. wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
  1244. }
  1245. wr = (void *)&q->q.desc[q->q.pidx];
  1246. wr->equiq_to_len16 = htonl(wr_mid);
  1247. wr->r3 = cpu_to_be64(0);
  1248. end = (u64 *)wr + flits;
  1249. len = immediate ? skb->len : 0;
  1250. len += sizeof(*cpl);
  1251. if (ssi->gso_size) {
  1252. struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
  1253. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  1254. int l3hdr_len = skb_network_header_len(skb);
  1255. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1256. struct cpl_tx_tnl_lso *tnl_lso = (void *)(wr + 1);
  1257. if (tnl_type)
  1258. len += sizeof(*tnl_lso);
  1259. else
  1260. len += sizeof(*lso);
  1261. wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
  1262. FW_WR_IMMDLEN_V(len));
  1263. if (tnl_type) {
  1264. struct iphdr *iph = ip_hdr(skb);
  1265. t6_fill_tnl_lso(skb, tnl_lso, tnl_type);
  1266. cpl = (void *)(tnl_lso + 1);
  1267. /* Driver is expected to compute partial checksum that
  1268. * does not include the IP Total Length.
  1269. */
  1270. if (iph->version == 4) {
  1271. iph->check = 0;
  1272. iph->tot_len = 0;
  1273. iph->check = (u16)(~ip_fast_csum((u8 *)iph,
  1274. iph->ihl));
  1275. }
  1276. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1277. cntrl = hwcsum(adap->params.chip, skb);
  1278. } else {
  1279. lso->lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
  1280. LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
  1281. LSO_IPV6_V(v6) |
  1282. LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
  1283. LSO_IPHDR_LEN_V(l3hdr_len / 4) |
  1284. LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
  1285. lso->ipid_ofst = htons(0);
  1286. lso->mss = htons(ssi->gso_size);
  1287. lso->seqno_offset = htonl(0);
  1288. if (is_t4(adap->params.chip))
  1289. lso->len = htonl(skb->len);
  1290. else
  1291. lso->len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
  1292. cpl = (void *)(lso + 1);
  1293. if (CHELSIO_CHIP_VERSION(adap->params.chip)
  1294. <= CHELSIO_T5)
  1295. cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1296. else
  1297. cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1298. cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
  1299. TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  1300. TXPKT_IPHDR_LEN_V(l3hdr_len);
  1301. }
  1302. sgl = (u64 *)(cpl + 1); /* sgl start here */
  1303. if (unlikely((u8 *)sgl >= (u8 *)q->q.stat)) {
  1304. /* If current position is already at the end of the
  1305. * txq, reset the current to point to start of the queue
  1306. * and update the end ptr as well.
  1307. */
  1308. if (sgl == (u64 *)q->q.stat) {
  1309. int left = (u8 *)end - (u8 *)q->q.stat;
  1310. end = (void *)q->q.desc + left;
  1311. sgl = (void *)q->q.desc;
  1312. }
  1313. }
  1314. q->tso++;
  1315. q->tx_cso += ssi->gso_segs;
  1316. } else {
  1317. if (ptp_enabled)
  1318. op = FW_PTP_TX_PKT_WR;
  1319. else
  1320. op = FW_ETH_TX_PKT_WR;
  1321. wr->op_immdlen = htonl(FW_WR_OP_V(op) |
  1322. FW_WR_IMMDLEN_V(len));
  1323. cpl = (void *)(wr + 1);
  1324. sgl = (u64 *)(cpl + 1);
  1325. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1326. cntrl = hwcsum(adap->params.chip, skb) |
  1327. TXPKT_IPCSUM_DIS_F;
  1328. q->tx_cso++;
  1329. }
  1330. }
  1331. if (skb_vlan_tag_present(skb)) {
  1332. q->vlan_ins++;
  1333. cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
  1334. #ifdef CONFIG_CHELSIO_T4_FCOE
  1335. if (skb->protocol == htons(ETH_P_FCOE))
  1336. cntrl |= TXPKT_VLAN_V(
  1337. ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
  1338. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1339. }
  1340. ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_INTF_V(pi->tx_chan) |
  1341. TXPKT_PF_V(adap->pf);
  1342. if (ptp_enabled)
  1343. ctrl0 |= TXPKT_TSTAMP_F;
  1344. #ifdef CONFIG_CHELSIO_T4_DCB
  1345. if (is_t4(adap->params.chip))
  1346. ctrl0 |= TXPKT_OVLAN_IDX_V(q->dcb_prio);
  1347. else
  1348. ctrl0 |= TXPKT_T5_OVLAN_IDX_V(q->dcb_prio);
  1349. #endif
  1350. cpl->ctrl0 = htonl(ctrl0);
  1351. cpl->pack = htons(0);
  1352. cpl->len = htons(skb->len);
  1353. cpl->ctrl1 = cpu_to_be64(cntrl);
  1354. if (immediate) {
  1355. cxgb4_inline_tx_skb(skb, &q->q, sgl);
  1356. dev_consume_skb_any(skb);
  1357. } else {
  1358. int last_desc;
  1359. cxgb4_write_sgl(skb, &q->q, (void *)sgl, end, 0, addr);
  1360. skb_orphan(skb);
  1361. last_desc = q->q.pidx + ndesc - 1;
  1362. if (last_desc >= q->q.size)
  1363. last_desc -= q->q.size;
  1364. q->q.sdesc[last_desc].skb = skb;
  1365. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)sgl;
  1366. }
  1367. txq_advance(&q->q, ndesc);
  1368. cxgb4_ring_tx_db(adap, &q->q, ndesc);
  1369. if (ptp_enabled)
  1370. spin_unlock(&adap->ptp_lock);
  1371. return NETDEV_TX_OK;
  1372. }
  1373. /* Constants ... */
  1374. enum {
  1375. /* Egress Queue sizes, producer and consumer indices are all in units
  1376. * of Egress Context Units bytes. Note that as far as the hardware is
  1377. * concerned, the free list is an Egress Queue (the host produces free
  1378. * buffers which the hardware consumes) and free list entries are
  1379. * 64-bit PCI DMA addresses.
  1380. */
  1381. EQ_UNIT = SGE_EQ_IDXSIZE,
  1382. FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
  1383. TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
  1384. T4VF_ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  1385. sizeof(struct cpl_tx_pkt_lso_core) +
  1386. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
  1387. };
  1388. /**
  1389. * t4vf_is_eth_imm - can an Ethernet packet be sent as immediate data?
  1390. * @skb: the packet
  1391. *
  1392. * Returns whether an Ethernet packet is small enough to fit completely as
  1393. * immediate data.
  1394. */
  1395. static inline int t4vf_is_eth_imm(const struct sk_buff *skb)
  1396. {
  1397. /* The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
  1398. * which does not accommodate immediate data. We could dike out all
  1399. * of the support code for immediate data but that would tie our hands
  1400. * too much if we ever want to enhace the firmware. It would also
  1401. * create more differences between the PF and VF Drivers.
  1402. */
  1403. return false;
  1404. }
  1405. /**
  1406. * t4vf_calc_tx_flits - calculate the number of flits for a packet TX WR
  1407. * @skb: the packet
  1408. *
  1409. * Returns the number of flits needed for a TX Work Request for the
  1410. * given Ethernet packet, including the needed WR and CPL headers.
  1411. */
  1412. static inline unsigned int t4vf_calc_tx_flits(const struct sk_buff *skb)
  1413. {
  1414. unsigned int flits;
  1415. /* If the skb is small enough, we can pump it out as a work request
  1416. * with only immediate data. In that case we just have to have the
  1417. * TX Packet header plus the skb data in the Work Request.
  1418. */
  1419. if (t4vf_is_eth_imm(skb))
  1420. return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
  1421. sizeof(__be64));
  1422. /* Otherwise, we're going to have to construct a Scatter gather list
  1423. * of the skb body and fragments. We also include the flits necessary
  1424. * for the TX Packet Work Request and CPL. We always have a firmware
  1425. * Write Header (incorporated as part of the cpl_tx_pkt_lso and
  1426. * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
  1427. * message or, if we're doing a Large Send Offload, an LSO CPL message
  1428. * with an embedded TX Packet Write CPL message.
  1429. */
  1430. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  1431. if (skb_shinfo(skb)->gso_size)
  1432. flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  1433. sizeof(struct cpl_tx_pkt_lso_core) +
  1434. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  1435. else
  1436. flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  1437. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  1438. return flits;
  1439. }
  1440. /**
  1441. * cxgb4_vf_eth_xmit - add a packet to an Ethernet TX queue
  1442. * @skb: the packet
  1443. * @dev: the egress net device
  1444. *
  1445. * Add a packet to an SGE Ethernet TX queue. Runs with softirqs disabled.
  1446. */
  1447. static netdev_tx_t cxgb4_vf_eth_xmit(struct sk_buff *skb,
  1448. struct net_device *dev)
  1449. {
  1450. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  1451. const struct skb_shared_info *ssi;
  1452. struct fw_eth_tx_pkt_vm_wr *wr;
  1453. int qidx, credits, max_pkt_len;
  1454. struct cpl_tx_pkt_core *cpl;
  1455. const struct port_info *pi;
  1456. unsigned int flits, ndesc;
  1457. struct sge_eth_txq *txq;
  1458. struct adapter *adapter;
  1459. u64 cntrl, *end;
  1460. u32 wr_mid;
  1461. const size_t fw_hdr_copy_len = sizeof(wr->ethmacdst) +
  1462. sizeof(wr->ethmacsrc) +
  1463. sizeof(wr->ethtype) +
  1464. sizeof(wr->vlantci);
  1465. /* The chip minimum packet length is 10 octets but the firmware
  1466. * command that we are using requires that we copy the Ethernet header
  1467. * (including the VLAN tag) into the header so we reject anything
  1468. * smaller than that ...
  1469. */
  1470. if (unlikely(skb->len < fw_hdr_copy_len))
  1471. goto out_free;
  1472. /* Discard the packet if the length is greater than mtu */
  1473. max_pkt_len = ETH_HLEN + dev->mtu;
  1474. if (skb_vlan_tag_present(skb))
  1475. max_pkt_len += VLAN_HLEN;
  1476. if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
  1477. goto out_free;
  1478. /* Figure out which TX Queue we're going to use. */
  1479. pi = netdev_priv(dev);
  1480. adapter = pi->adapter;
  1481. qidx = skb_get_queue_mapping(skb);
  1482. WARN_ON(qidx >= pi->nqsets);
  1483. txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
  1484. /* Take this opportunity to reclaim any TX Descriptors whose DMA
  1485. * transfers have completed.
  1486. */
  1487. cxgb4_reclaim_completed_tx(adapter, &txq->q, true);
  1488. /* Calculate the number of flits and TX Descriptors we're going to
  1489. * need along with how many TX Descriptors will be left over after
  1490. * we inject our Work Request.
  1491. */
  1492. flits = t4vf_calc_tx_flits(skb);
  1493. ndesc = flits_to_desc(flits);
  1494. credits = txq_avail(&txq->q) - ndesc;
  1495. if (unlikely(credits < 0)) {
  1496. /* Not enough room for this packet's Work Request. Stop the
  1497. * TX Queue and return a "busy" condition. The queue will get
  1498. * started later on when the firmware informs us that space
  1499. * has opened up.
  1500. */
  1501. eth_txq_stop(txq);
  1502. dev_err(adapter->pdev_dev,
  1503. "%s: TX ring %u full while queue awake!\n",
  1504. dev->name, qidx);
  1505. return NETDEV_TX_BUSY;
  1506. }
  1507. if (!t4vf_is_eth_imm(skb) &&
  1508. unlikely(cxgb4_map_skb(adapter->pdev_dev, skb, addr) < 0)) {
  1509. /* We need to map the skb into PCI DMA space (because it can't
  1510. * be in-lined directly into the Work Request) and the mapping
  1511. * operation failed. Record the error and drop the packet.
  1512. */
  1513. txq->mapping_err++;
  1514. goto out_free;
  1515. }
  1516. wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
  1517. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  1518. /* After we're done injecting the Work Request for this
  1519. * packet, we'll be below our "stop threshold" so stop the TX
  1520. * Queue now and schedule a request for an SGE Egress Queue
  1521. * Update message. The queue will get started later on when
  1522. * the firmware processes this Work Request and sends us an
  1523. * Egress Queue Status Update message indicating that space
  1524. * has opened up.
  1525. */
  1526. eth_txq_stop(txq);
  1527. wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
  1528. }
  1529. /* Start filling in our Work Request. Note that we do _not_ handle
  1530. * the WR Header wrapping around the TX Descriptor Ring. If our
  1531. * maximum header size ever exceeds one TX Descriptor, we'll need to
  1532. * do something else here.
  1533. */
  1534. WARN_ON(DIV_ROUND_UP(T4VF_ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
  1535. wr = (void *)&txq->q.desc[txq->q.pidx];
  1536. wr->equiq_to_len16 = cpu_to_be32(wr_mid);
  1537. wr->r3[0] = cpu_to_be32(0);
  1538. wr->r3[1] = cpu_to_be32(0);
  1539. skb_copy_from_linear_data(skb, (void *)wr->ethmacdst, fw_hdr_copy_len);
  1540. end = (u64 *)wr + flits;
  1541. /* If this is a Large Send Offload packet we'll put in an LSO CPL
  1542. * message with an encapsulated TX Packet CPL message. Otherwise we
  1543. * just use a TX Packet CPL message.
  1544. */
  1545. ssi = skb_shinfo(skb);
  1546. if (ssi->gso_size) {
  1547. struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
  1548. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  1549. int l3hdr_len = skb_network_header_len(skb);
  1550. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1551. wr->op_immdlen =
  1552. cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
  1553. FW_WR_IMMDLEN_V(sizeof(*lso) +
  1554. sizeof(*cpl)));
  1555. /* Fill in the LSO CPL message. */
  1556. lso->lso_ctrl =
  1557. cpu_to_be32(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
  1558. LSO_FIRST_SLICE_F |
  1559. LSO_LAST_SLICE_F |
  1560. LSO_IPV6_V(v6) |
  1561. LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
  1562. LSO_IPHDR_LEN_V(l3hdr_len / 4) |
  1563. LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
  1564. lso->ipid_ofst = cpu_to_be16(0);
  1565. lso->mss = cpu_to_be16(ssi->gso_size);
  1566. lso->seqno_offset = cpu_to_be32(0);
  1567. if (is_t4(adapter->params.chip))
  1568. lso->len = cpu_to_be32(skb->len);
  1569. else
  1570. lso->len = cpu_to_be32(LSO_T5_XFER_SIZE_V(skb->len));
  1571. /* Set up TX Packet CPL pointer, control word and perform
  1572. * accounting.
  1573. */
  1574. cpl = (void *)(lso + 1);
  1575. if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
  1576. cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1577. else
  1578. cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1579. cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
  1580. TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  1581. TXPKT_IPHDR_LEN_V(l3hdr_len);
  1582. txq->tso++;
  1583. txq->tx_cso += ssi->gso_segs;
  1584. } else {
  1585. int len;
  1586. len = (t4vf_is_eth_imm(skb)
  1587. ? skb->len + sizeof(*cpl)
  1588. : sizeof(*cpl));
  1589. wr->op_immdlen =
  1590. cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
  1591. FW_WR_IMMDLEN_V(len));
  1592. /* Set up TX Packet CPL pointer, control word and perform
  1593. * accounting.
  1594. */
  1595. cpl = (void *)(wr + 1);
  1596. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1597. cntrl = hwcsum(adapter->params.chip, skb) |
  1598. TXPKT_IPCSUM_DIS_F;
  1599. txq->tx_cso++;
  1600. } else {
  1601. cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
  1602. }
  1603. }
  1604. /* If there's a VLAN tag present, add that to the list of things to
  1605. * do in this Work Request.
  1606. */
  1607. if (skb_vlan_tag_present(skb)) {
  1608. txq->vlan_ins++;
  1609. cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
  1610. }
  1611. /* Fill in the TX Packet CPL message header. */
  1612. cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE_V(CPL_TX_PKT_XT) |
  1613. TXPKT_INTF_V(pi->port_id) |
  1614. TXPKT_PF_V(0));
  1615. cpl->pack = cpu_to_be16(0);
  1616. cpl->len = cpu_to_be16(skb->len);
  1617. cpl->ctrl1 = cpu_to_be64(cntrl);
  1618. /* Fill in the body of the TX Packet CPL message with either in-lined
  1619. * data or a Scatter/Gather List.
  1620. */
  1621. if (t4vf_is_eth_imm(skb)) {
  1622. /* In-line the packet's data and free the skb since we don't
  1623. * need it any longer.
  1624. */
  1625. cxgb4_inline_tx_skb(skb, &txq->q, cpl + 1);
  1626. dev_consume_skb_any(skb);
  1627. } else {
  1628. /* Write the skb's Scatter/Gather list into the TX Packet CPL
  1629. * message and retain a pointer to the skb so we can free it
  1630. * later when its DMA completes. (We store the skb pointer
  1631. * in the Software Descriptor corresponding to the last TX
  1632. * Descriptor used by the Work Request.)
  1633. *
  1634. * The retained skb will be freed when the corresponding TX
  1635. * Descriptors are reclaimed after their DMAs complete.
  1636. * However, this could take quite a while since, in general,
  1637. * the hardware is set up to be lazy about sending DMA
  1638. * completion notifications to us and we mostly perform TX
  1639. * reclaims in the transmit routine.
  1640. *
  1641. * This is good for performamce but means that we rely on new
  1642. * TX packets arriving to run the destructors of completed
  1643. * packets, which open up space in their sockets' send queues.
  1644. * Sometimes we do not get such new packets causing TX to
  1645. * stall. A single UDP transmitter is a good example of this
  1646. * situation. We have a clean up timer that periodically
  1647. * reclaims completed packets but it doesn't run often enough
  1648. * (nor do we want it to) to prevent lengthy stalls. A
  1649. * solution to this problem is to run the destructor early,
  1650. * after the packet is queued but before it's DMAd. A con is
  1651. * that we lie to socket memory accounting, but the amount of
  1652. * extra memory is reasonable (limited by the number of TX
  1653. * descriptors), the packets do actually get freed quickly by
  1654. * new packets almost always, and for protocols like TCP that
  1655. * wait for acks to really free up the data the extra memory
  1656. * is even less. On the positive side we run the destructors
  1657. * on the sending CPU rather than on a potentially different
  1658. * completing CPU, usually a good thing.
  1659. *
  1660. * Run the destructor before telling the DMA engine about the
  1661. * packet to make sure it doesn't complete and get freed
  1662. * prematurely.
  1663. */
  1664. struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
  1665. struct sge_txq *tq = &txq->q;
  1666. int last_desc;
  1667. /* If the Work Request header was an exact multiple of our TX
  1668. * Descriptor length, then it's possible that the starting SGL
  1669. * pointer lines up exactly with the end of our TX Descriptor
  1670. * ring. If that's the case, wrap around to the beginning
  1671. * here ...
  1672. */
  1673. if (unlikely((void *)sgl == (void *)tq->stat)) {
  1674. sgl = (void *)tq->desc;
  1675. end = (void *)((void *)tq->desc +
  1676. ((void *)end - (void *)tq->stat));
  1677. }
  1678. cxgb4_write_sgl(skb, tq, sgl, end, 0, addr);
  1679. skb_orphan(skb);
  1680. last_desc = tq->pidx + ndesc - 1;
  1681. if (last_desc >= tq->size)
  1682. last_desc -= tq->size;
  1683. tq->sdesc[last_desc].skb = skb;
  1684. tq->sdesc[last_desc].sgl = sgl;
  1685. }
  1686. /* Advance our internal TX Queue state, tell the hardware about
  1687. * the new TX descriptors and return success.
  1688. */
  1689. txq_advance(&txq->q, ndesc);
  1690. cxgb4_ring_tx_db(adapter, &txq->q, ndesc);
  1691. return NETDEV_TX_OK;
  1692. out_free:
  1693. /* An error of some sort happened. Free the TX skb and tell the
  1694. * OS that we've "dealt" with the packet ...
  1695. */
  1696. dev_kfree_skb_any(skb);
  1697. return NETDEV_TX_OK;
  1698. }
  1699. netdev_tx_t t4_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1700. {
  1701. struct port_info *pi = netdev_priv(dev);
  1702. if (unlikely(pi->eth_flags & PRIV_FLAG_PORT_TX_VM))
  1703. return cxgb4_vf_eth_xmit(skb, dev);
  1704. return cxgb4_eth_xmit(skb, dev);
  1705. }
  1706. /**
  1707. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1708. * @q: the SGE control Tx queue
  1709. *
  1710. * This is a variant of cxgb4_reclaim_completed_tx() that is used
  1711. * for Tx queues that send only immediate data (presently just
  1712. * the control queues) and thus do not have any sk_buffs to release.
  1713. */
  1714. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1715. {
  1716. int hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
  1717. int reclaim = hw_cidx - q->cidx;
  1718. if (reclaim < 0)
  1719. reclaim += q->size;
  1720. q->in_use -= reclaim;
  1721. q->cidx = hw_cidx;
  1722. }
  1723. /**
  1724. * is_imm - check whether a packet can be sent as immediate data
  1725. * @skb: the packet
  1726. *
  1727. * Returns true if a packet can be sent as a WR with immediate data.
  1728. */
  1729. static inline int is_imm(const struct sk_buff *skb)
  1730. {
  1731. return skb->len <= MAX_CTRL_WR_LEN;
  1732. }
  1733. /**
  1734. * ctrlq_check_stop - check if a control queue is full and should stop
  1735. * @q: the queue
  1736. * @wr: most recent WR written to the queue
  1737. *
  1738. * Check if a control queue has become full and should be stopped.
  1739. * We clean up control queue descriptors very lazily, only when we are out.
  1740. * If the queue is still full after reclaiming any completed descriptors
  1741. * we suspend it and have the last WR wake it up.
  1742. */
  1743. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  1744. {
  1745. reclaim_completed_tx_imm(&q->q);
  1746. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1747. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1748. q->q.stops++;
  1749. q->full = 1;
  1750. }
  1751. }
  1752. /**
  1753. * ctrl_xmit - send a packet through an SGE control Tx queue
  1754. * @q: the control queue
  1755. * @skb: the packet
  1756. *
  1757. * Send a packet through an SGE control Tx queue. Packets sent through
  1758. * a control queue must fit entirely as immediate data.
  1759. */
  1760. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  1761. {
  1762. unsigned int ndesc;
  1763. struct fw_wr_hdr *wr;
  1764. if (unlikely(!is_imm(skb))) {
  1765. WARN_ON(1);
  1766. dev_kfree_skb(skb);
  1767. return NET_XMIT_DROP;
  1768. }
  1769. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  1770. spin_lock(&q->sendq.lock);
  1771. if (unlikely(q->full)) {
  1772. skb->priority = ndesc; /* save for restart */
  1773. __skb_queue_tail(&q->sendq, skb);
  1774. spin_unlock(&q->sendq.lock);
  1775. return NET_XMIT_CN;
  1776. }
  1777. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1778. cxgb4_inline_tx_skb(skb, &q->q, wr);
  1779. txq_advance(&q->q, ndesc);
  1780. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  1781. ctrlq_check_stop(q, wr);
  1782. cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
  1783. spin_unlock(&q->sendq.lock);
  1784. kfree_skb(skb);
  1785. return NET_XMIT_SUCCESS;
  1786. }
  1787. /**
  1788. * restart_ctrlq - restart a suspended control queue
  1789. * @data: the control queue to restart
  1790. *
  1791. * Resumes transmission on a suspended Tx control queue.
  1792. */
  1793. static void restart_ctrlq(unsigned long data)
  1794. {
  1795. struct sk_buff *skb;
  1796. unsigned int written = 0;
  1797. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  1798. spin_lock(&q->sendq.lock);
  1799. reclaim_completed_tx_imm(&q->q);
  1800. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  1801. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  1802. struct fw_wr_hdr *wr;
  1803. unsigned int ndesc = skb->priority; /* previously saved */
  1804. written += ndesc;
  1805. /* Write descriptors and free skbs outside the lock to limit
  1806. * wait times. q->full is still set so new skbs will be queued.
  1807. */
  1808. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1809. txq_advance(&q->q, ndesc);
  1810. spin_unlock(&q->sendq.lock);
  1811. cxgb4_inline_tx_skb(skb, &q->q, wr);
  1812. kfree_skb(skb);
  1813. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1814. unsigned long old = q->q.stops;
  1815. ctrlq_check_stop(q, wr);
  1816. if (q->q.stops != old) { /* suspended anew */
  1817. spin_lock(&q->sendq.lock);
  1818. goto ringdb;
  1819. }
  1820. }
  1821. if (written > 16) {
  1822. cxgb4_ring_tx_db(q->adap, &q->q, written);
  1823. written = 0;
  1824. }
  1825. spin_lock(&q->sendq.lock);
  1826. }
  1827. q->full = 0;
  1828. ringdb:
  1829. if (written)
  1830. cxgb4_ring_tx_db(q->adap, &q->q, written);
  1831. spin_unlock(&q->sendq.lock);
  1832. }
  1833. /**
  1834. * t4_mgmt_tx - send a management message
  1835. * @adap: the adapter
  1836. * @skb: the packet containing the management message
  1837. *
  1838. * Send a management message through control queue 0.
  1839. */
  1840. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1841. {
  1842. int ret;
  1843. local_bh_disable();
  1844. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1845. local_bh_enable();
  1846. return ret;
  1847. }
  1848. /**
  1849. * is_ofld_imm - check whether a packet can be sent as immediate data
  1850. * @skb: the packet
  1851. *
  1852. * Returns true if a packet can be sent as an offload WR with immediate
  1853. * data. We currently use the same limit as for Ethernet packets.
  1854. */
  1855. static inline int is_ofld_imm(const struct sk_buff *skb)
  1856. {
  1857. struct work_request_hdr *req = (struct work_request_hdr *)skb->data;
  1858. unsigned long opcode = FW_WR_OP_G(ntohl(req->wr_hi));
  1859. if (opcode == FW_CRYPTO_LOOKASIDE_WR)
  1860. return skb->len <= SGE_MAX_WR_LEN;
  1861. else
  1862. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1863. }
  1864. /**
  1865. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1866. * @skb: the packet
  1867. *
  1868. * Returns the number of flits needed for the given offload packet.
  1869. * These packets are already fully constructed and no additional headers
  1870. * will be added.
  1871. */
  1872. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1873. {
  1874. unsigned int flits, cnt;
  1875. if (is_ofld_imm(skb))
  1876. return DIV_ROUND_UP(skb->len, 8);
  1877. flits = skb_transport_offset(skb) / 8U; /* headers */
  1878. cnt = skb_shinfo(skb)->nr_frags;
  1879. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1880. cnt++;
  1881. return flits + sgl_len(cnt);
  1882. }
  1883. /**
  1884. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1885. * @adap: the adapter
  1886. * @q: the queue to stop
  1887. *
  1888. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1889. * inability to map packets. A periodic timer attempts to restart
  1890. * queues so marked.
  1891. */
  1892. static void txq_stop_maperr(struct sge_uld_txq *q)
  1893. {
  1894. q->mapping_err++;
  1895. q->q.stops++;
  1896. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1897. q->adap->sge.txq_maperr);
  1898. }
  1899. /**
  1900. * ofldtxq_stop - stop an offload Tx queue that has become full
  1901. * @q: the queue to stop
  1902. * @wr: the Work Request causing the queue to become full
  1903. *
  1904. * Stops an offload Tx queue that has become full and modifies the packet
  1905. * being written to request a wakeup.
  1906. */
  1907. static void ofldtxq_stop(struct sge_uld_txq *q, struct fw_wr_hdr *wr)
  1908. {
  1909. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1910. q->q.stops++;
  1911. q->full = 1;
  1912. }
  1913. /**
  1914. * service_ofldq - service/restart a suspended offload queue
  1915. * @q: the offload queue
  1916. *
  1917. * Services an offload Tx queue by moving packets from its Pending Send
  1918. * Queue to the Hardware TX ring. The function starts and ends with the
  1919. * Send Queue locked, but drops the lock while putting the skb at the
  1920. * head of the Send Queue onto the Hardware TX Ring. Dropping the lock
  1921. * allows more skbs to be added to the Send Queue by other threads.
  1922. * The packet being processed at the head of the Pending Send Queue is
  1923. * left on the queue in case we experience DMA Mapping errors, etc.
  1924. * and need to give up and restart later.
  1925. *
  1926. * service_ofldq() can be thought of as a task which opportunistically
  1927. * uses other threads execution contexts. We use the Offload Queue
  1928. * boolean "service_ofldq_running" to make sure that only one instance
  1929. * is ever running at a time ...
  1930. */
  1931. static void service_ofldq(struct sge_uld_txq *q)
  1932. {
  1933. u64 *pos, *before, *end;
  1934. int credits;
  1935. struct sk_buff *skb;
  1936. struct sge_txq *txq;
  1937. unsigned int left;
  1938. unsigned int written = 0;
  1939. unsigned int flits, ndesc;
  1940. /* If another thread is currently in service_ofldq() processing the
  1941. * Pending Send Queue then there's nothing to do. Otherwise, flag
  1942. * that we're doing the work and continue. Examining/modifying
  1943. * the Offload Queue boolean "service_ofldq_running" must be done
  1944. * while holding the Pending Send Queue Lock.
  1945. */
  1946. if (q->service_ofldq_running)
  1947. return;
  1948. q->service_ofldq_running = true;
  1949. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1950. /* We drop the lock while we're working with the skb at the
  1951. * head of the Pending Send Queue. This allows more skbs to
  1952. * be added to the Pending Send Queue while we're working on
  1953. * this one. We don't need to lock to guard the TX Ring
  1954. * updates because only one thread of execution is ever
  1955. * allowed into service_ofldq() at a time.
  1956. */
  1957. spin_unlock(&q->sendq.lock);
  1958. cxgb4_reclaim_completed_tx(q->adap, &q->q, false);
  1959. flits = skb->priority; /* previously saved */
  1960. ndesc = flits_to_desc(flits);
  1961. credits = txq_avail(&q->q) - ndesc;
  1962. BUG_ON(credits < 0);
  1963. if (unlikely(credits < TXQ_STOP_THRES))
  1964. ofldtxq_stop(q, (struct fw_wr_hdr *)skb->data);
  1965. pos = (u64 *)&q->q.desc[q->q.pidx];
  1966. if (is_ofld_imm(skb))
  1967. cxgb4_inline_tx_skb(skb, &q->q, pos);
  1968. else if (cxgb4_map_skb(q->adap->pdev_dev, skb,
  1969. (dma_addr_t *)skb->head)) {
  1970. txq_stop_maperr(q);
  1971. spin_lock(&q->sendq.lock);
  1972. break;
  1973. } else {
  1974. int last_desc, hdr_len = skb_transport_offset(skb);
  1975. /* The WR headers may not fit within one descriptor.
  1976. * So we need to deal with wrap-around here.
  1977. */
  1978. before = (u64 *)pos;
  1979. end = (u64 *)pos + flits;
  1980. txq = &q->q;
  1981. pos = (void *)inline_tx_skb_header(skb, &q->q,
  1982. (void *)pos,
  1983. hdr_len);
  1984. if (before > (u64 *)pos) {
  1985. left = (u8 *)end - (u8 *)txq->stat;
  1986. end = (void *)txq->desc + left;
  1987. }
  1988. /* If current position is already at the end of the
  1989. * ofld queue, reset the current to point to
  1990. * start of the queue and update the end ptr as well.
  1991. */
  1992. if (pos == (u64 *)txq->stat) {
  1993. left = (u8 *)end - (u8 *)txq->stat;
  1994. end = (void *)txq->desc + left;
  1995. pos = (void *)txq->desc;
  1996. }
  1997. cxgb4_write_sgl(skb, &q->q, (void *)pos,
  1998. end, hdr_len,
  1999. (dma_addr_t *)skb->head);
  2000. #ifdef CONFIG_NEED_DMA_MAP_STATE
  2001. skb->dev = q->adap->port[0];
  2002. skb->destructor = deferred_unmap_destructor;
  2003. #endif
  2004. last_desc = q->q.pidx + ndesc - 1;
  2005. if (last_desc >= q->q.size)
  2006. last_desc -= q->q.size;
  2007. q->q.sdesc[last_desc].skb = skb;
  2008. }
  2009. txq_advance(&q->q, ndesc);
  2010. written += ndesc;
  2011. if (unlikely(written > 32)) {
  2012. cxgb4_ring_tx_db(q->adap, &q->q, written);
  2013. written = 0;
  2014. }
  2015. /* Reacquire the Pending Send Queue Lock so we can unlink the
  2016. * skb we've just successfully transferred to the TX Ring and
  2017. * loop for the next skb which may be at the head of the
  2018. * Pending Send Queue.
  2019. */
  2020. spin_lock(&q->sendq.lock);
  2021. __skb_unlink(skb, &q->sendq);
  2022. if (is_ofld_imm(skb))
  2023. kfree_skb(skb);
  2024. }
  2025. if (likely(written))
  2026. cxgb4_ring_tx_db(q->adap, &q->q, written);
  2027. /*Indicate that no thread is processing the Pending Send Queue
  2028. * currently.
  2029. */
  2030. q->service_ofldq_running = false;
  2031. }
  2032. /**
  2033. * ofld_xmit - send a packet through an offload queue
  2034. * @q: the Tx offload queue
  2035. * @skb: the packet
  2036. *
  2037. * Send an offload packet through an SGE offload queue.
  2038. */
  2039. static int ofld_xmit(struct sge_uld_txq *q, struct sk_buff *skb)
  2040. {
  2041. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  2042. spin_lock(&q->sendq.lock);
  2043. /* Queue the new skb onto the Offload Queue's Pending Send Queue. If
  2044. * that results in this new skb being the only one on the queue, start
  2045. * servicing it. If there are other skbs already on the list, then
  2046. * either the queue is currently being processed or it's been stopped
  2047. * for some reason and it'll be restarted at a later time. Restart
  2048. * paths are triggered by events like experiencing a DMA Mapping Error
  2049. * or filling the Hardware TX Ring.
  2050. */
  2051. __skb_queue_tail(&q->sendq, skb);
  2052. if (q->sendq.qlen == 1)
  2053. service_ofldq(q);
  2054. spin_unlock(&q->sendq.lock);
  2055. return NET_XMIT_SUCCESS;
  2056. }
  2057. /**
  2058. * restart_ofldq - restart a suspended offload queue
  2059. * @data: the offload queue to restart
  2060. *
  2061. * Resumes transmission on a suspended Tx offload queue.
  2062. */
  2063. static void restart_ofldq(unsigned long data)
  2064. {
  2065. struct sge_uld_txq *q = (struct sge_uld_txq *)data;
  2066. spin_lock(&q->sendq.lock);
  2067. q->full = 0; /* the queue actually is completely empty now */
  2068. service_ofldq(q);
  2069. spin_unlock(&q->sendq.lock);
  2070. }
  2071. /**
  2072. * skb_txq - return the Tx queue an offload packet should use
  2073. * @skb: the packet
  2074. *
  2075. * Returns the Tx queue an offload packet should use as indicated by bits
  2076. * 1-15 in the packet's queue_mapping.
  2077. */
  2078. static inline unsigned int skb_txq(const struct sk_buff *skb)
  2079. {
  2080. return skb->queue_mapping >> 1;
  2081. }
  2082. /**
  2083. * is_ctrl_pkt - return whether an offload packet is a control packet
  2084. * @skb: the packet
  2085. *
  2086. * Returns whether an offload packet should use an OFLD or a CTRL
  2087. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  2088. */
  2089. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  2090. {
  2091. return skb->queue_mapping & 1;
  2092. }
  2093. static inline int uld_send(struct adapter *adap, struct sk_buff *skb,
  2094. unsigned int tx_uld_type)
  2095. {
  2096. struct sge_uld_txq_info *txq_info;
  2097. struct sge_uld_txq *txq;
  2098. unsigned int idx = skb_txq(skb);
  2099. if (unlikely(is_ctrl_pkt(skb))) {
  2100. /* Single ctrl queue is a requirement for LE workaround path */
  2101. if (adap->tids.nsftids)
  2102. idx = 0;
  2103. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  2104. }
  2105. txq_info = adap->sge.uld_txq_info[tx_uld_type];
  2106. if (unlikely(!txq_info)) {
  2107. WARN_ON(true);
  2108. return NET_XMIT_DROP;
  2109. }
  2110. txq = &txq_info->uldtxq[idx];
  2111. return ofld_xmit(txq, skb);
  2112. }
  2113. /**
  2114. * t4_ofld_send - send an offload packet
  2115. * @adap: the adapter
  2116. * @skb: the packet
  2117. *
  2118. * Sends an offload packet. We use the packet queue_mapping to select the
  2119. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  2120. * should be sent as regular or control, bits 1-15 select the queue.
  2121. */
  2122. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  2123. {
  2124. int ret;
  2125. local_bh_disable();
  2126. ret = uld_send(adap, skb, CXGB4_TX_OFLD);
  2127. local_bh_enable();
  2128. return ret;
  2129. }
  2130. /**
  2131. * cxgb4_ofld_send - send an offload packet
  2132. * @dev: the net device
  2133. * @skb: the packet
  2134. *
  2135. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  2136. * intended for ULDs.
  2137. */
  2138. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  2139. {
  2140. return t4_ofld_send(netdev2adap(dev), skb);
  2141. }
  2142. EXPORT_SYMBOL(cxgb4_ofld_send);
  2143. static void *inline_tx_header(const void *src,
  2144. const struct sge_txq *q,
  2145. void *pos, int length)
  2146. {
  2147. int left = (void *)q->stat - pos;
  2148. u64 *p;
  2149. if (likely(length <= left)) {
  2150. memcpy(pos, src, length);
  2151. pos += length;
  2152. } else {
  2153. memcpy(pos, src, left);
  2154. memcpy(q->desc, src + left, length - left);
  2155. pos = (void *)q->desc + (length - left);
  2156. }
  2157. /* 0-pad to multiple of 16 */
  2158. p = PTR_ALIGN(pos, 8);
  2159. if ((uintptr_t)p & 8) {
  2160. *p = 0;
  2161. return p + 1;
  2162. }
  2163. return p;
  2164. }
  2165. /**
  2166. * ofld_xmit_direct - copy a WR into offload queue
  2167. * @q: the Tx offload queue
  2168. * @src: location of WR
  2169. * @len: WR length
  2170. *
  2171. * Copy an immediate WR into an uncontended SGE offload queue.
  2172. */
  2173. static int ofld_xmit_direct(struct sge_uld_txq *q, const void *src,
  2174. unsigned int len)
  2175. {
  2176. unsigned int ndesc;
  2177. int credits;
  2178. u64 *pos;
  2179. /* Use the lower limit as the cut-off */
  2180. if (len > MAX_IMM_OFLD_TX_DATA_WR_LEN) {
  2181. WARN_ON(1);
  2182. return NET_XMIT_DROP;
  2183. }
  2184. /* Don't return NET_XMIT_CN here as the current
  2185. * implementation doesn't queue the request
  2186. * using an skb when the following conditions not met
  2187. */
  2188. if (!spin_trylock(&q->sendq.lock))
  2189. return NET_XMIT_DROP;
  2190. if (q->full || !skb_queue_empty(&q->sendq) ||
  2191. q->service_ofldq_running) {
  2192. spin_unlock(&q->sendq.lock);
  2193. return NET_XMIT_DROP;
  2194. }
  2195. ndesc = flits_to_desc(DIV_ROUND_UP(len, 8));
  2196. credits = txq_avail(&q->q) - ndesc;
  2197. pos = (u64 *)&q->q.desc[q->q.pidx];
  2198. /* ofldtxq_stop modifies WR header in-situ */
  2199. inline_tx_header(src, &q->q, pos, len);
  2200. if (unlikely(credits < TXQ_STOP_THRES))
  2201. ofldtxq_stop(q, (struct fw_wr_hdr *)pos);
  2202. txq_advance(&q->q, ndesc);
  2203. cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
  2204. spin_unlock(&q->sendq.lock);
  2205. return NET_XMIT_SUCCESS;
  2206. }
  2207. int cxgb4_immdata_send(struct net_device *dev, unsigned int idx,
  2208. const void *src, unsigned int len)
  2209. {
  2210. struct sge_uld_txq_info *txq_info;
  2211. struct sge_uld_txq *txq;
  2212. struct adapter *adap;
  2213. int ret;
  2214. adap = netdev2adap(dev);
  2215. local_bh_disable();
  2216. txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
  2217. if (unlikely(!txq_info)) {
  2218. WARN_ON(true);
  2219. local_bh_enable();
  2220. return NET_XMIT_DROP;
  2221. }
  2222. txq = &txq_info->uldtxq[idx];
  2223. ret = ofld_xmit_direct(txq, src, len);
  2224. local_bh_enable();
  2225. return net_xmit_eval(ret);
  2226. }
  2227. EXPORT_SYMBOL(cxgb4_immdata_send);
  2228. /**
  2229. * t4_crypto_send - send crypto packet
  2230. * @adap: the adapter
  2231. * @skb: the packet
  2232. *
  2233. * Sends crypto packet. We use the packet queue_mapping to select the
  2234. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  2235. * should be sent as regular or control, bits 1-15 select the queue.
  2236. */
  2237. static int t4_crypto_send(struct adapter *adap, struct sk_buff *skb)
  2238. {
  2239. int ret;
  2240. local_bh_disable();
  2241. ret = uld_send(adap, skb, CXGB4_TX_CRYPTO);
  2242. local_bh_enable();
  2243. return ret;
  2244. }
  2245. /**
  2246. * cxgb4_crypto_send - send crypto packet
  2247. * @dev: the net device
  2248. * @skb: the packet
  2249. *
  2250. * Sends crypto packet. This is an exported version of @t4_crypto_send,
  2251. * intended for ULDs.
  2252. */
  2253. int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb)
  2254. {
  2255. return t4_crypto_send(netdev2adap(dev), skb);
  2256. }
  2257. EXPORT_SYMBOL(cxgb4_crypto_send);
  2258. static inline void copy_frags(struct sk_buff *skb,
  2259. const struct pkt_gl *gl, unsigned int offset)
  2260. {
  2261. int i;
  2262. /* usually there's just one frag */
  2263. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  2264. gl->frags[0].offset + offset,
  2265. gl->frags[0].size - offset);
  2266. skb_shinfo(skb)->nr_frags = gl->nfrags;
  2267. for (i = 1; i < gl->nfrags; i++)
  2268. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  2269. gl->frags[i].offset,
  2270. gl->frags[i].size);
  2271. /* get a reference to the last page, we don't own it */
  2272. get_page(gl->frags[gl->nfrags - 1].page);
  2273. }
  2274. /**
  2275. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  2276. * @gl: the gather list
  2277. * @skb_len: size of sk_buff main body if it carries fragments
  2278. * @pull_len: amount of data to move to the sk_buff's main body
  2279. *
  2280. * Builds an sk_buff from the given packet gather list. Returns the
  2281. * sk_buff or %NULL if sk_buff allocation failed.
  2282. */
  2283. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  2284. unsigned int skb_len, unsigned int pull_len)
  2285. {
  2286. struct sk_buff *skb;
  2287. /*
  2288. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  2289. * size, which is expected since buffers are at least PAGE_SIZEd.
  2290. * In this case packets up to RX_COPY_THRES have only one fragment.
  2291. */
  2292. if (gl->tot_len <= RX_COPY_THRES) {
  2293. skb = dev_alloc_skb(gl->tot_len);
  2294. if (unlikely(!skb))
  2295. goto out;
  2296. __skb_put(skb, gl->tot_len);
  2297. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  2298. } else {
  2299. skb = dev_alloc_skb(skb_len);
  2300. if (unlikely(!skb))
  2301. goto out;
  2302. __skb_put(skb, pull_len);
  2303. skb_copy_to_linear_data(skb, gl->va, pull_len);
  2304. copy_frags(skb, gl, pull_len);
  2305. skb->len = gl->tot_len;
  2306. skb->data_len = skb->len - pull_len;
  2307. skb->truesize += skb->data_len;
  2308. }
  2309. out: return skb;
  2310. }
  2311. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  2312. /**
  2313. * t4_pktgl_free - free a packet gather list
  2314. * @gl: the gather list
  2315. *
  2316. * Releases the pages of a packet gather list. We do not own the last
  2317. * page on the list and do not free it.
  2318. */
  2319. static void t4_pktgl_free(const struct pkt_gl *gl)
  2320. {
  2321. int n;
  2322. const struct page_frag *p;
  2323. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  2324. put_page(p->page);
  2325. }
  2326. /*
  2327. * Process an MPS trace packet. Give it an unused protocol number so it won't
  2328. * be delivered to anyone and send it to the stack for capture.
  2329. */
  2330. static noinline int handle_trace_pkt(struct adapter *adap,
  2331. const struct pkt_gl *gl)
  2332. {
  2333. struct sk_buff *skb;
  2334. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  2335. if (unlikely(!skb)) {
  2336. t4_pktgl_free(gl);
  2337. return 0;
  2338. }
  2339. if (is_t4(adap->params.chip))
  2340. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  2341. else
  2342. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  2343. skb_reset_mac_header(skb);
  2344. skb->protocol = htons(0xffff);
  2345. skb->dev = adap->port[0];
  2346. netif_receive_skb(skb);
  2347. return 0;
  2348. }
  2349. /**
  2350. * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
  2351. * @adap: the adapter
  2352. * @hwtstamps: time stamp structure to update
  2353. * @sgetstamp: 60bit iqe timestamp
  2354. *
  2355. * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
  2356. * which is in Core Clock ticks into ktime_t and assign it
  2357. **/
  2358. static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
  2359. struct skb_shared_hwtstamps *hwtstamps,
  2360. u64 sgetstamp)
  2361. {
  2362. u64 ns;
  2363. u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
  2364. ns = div_u64(tmp, adap->params.vpd.cclk);
  2365. memset(hwtstamps, 0, sizeof(*hwtstamps));
  2366. hwtstamps->hwtstamp = ns_to_ktime(ns);
  2367. }
  2368. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  2369. const struct cpl_rx_pkt *pkt, unsigned long tnl_hdr_len)
  2370. {
  2371. struct adapter *adapter = rxq->rspq.adap;
  2372. struct sge *s = &adapter->sge;
  2373. struct port_info *pi;
  2374. int ret;
  2375. struct sk_buff *skb;
  2376. skb = napi_get_frags(&rxq->rspq.napi);
  2377. if (unlikely(!skb)) {
  2378. t4_pktgl_free(gl);
  2379. rxq->stats.rx_drops++;
  2380. return;
  2381. }
  2382. copy_frags(skb, gl, s->pktshift);
  2383. if (tnl_hdr_len)
  2384. skb->csum_level = 1;
  2385. skb->len = gl->tot_len - s->pktshift;
  2386. skb->data_len = skb->len;
  2387. skb->truesize += skb->data_len;
  2388. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2389. skb_record_rx_queue(skb, rxq->rspq.idx);
  2390. pi = netdev_priv(skb->dev);
  2391. if (pi->rxtstamp)
  2392. cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
  2393. gl->sgetstamp);
  2394. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  2395. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  2396. PKT_HASH_TYPE_L3);
  2397. if (unlikely(pkt->vlan_ex)) {
  2398. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  2399. rxq->stats.vlan_ex++;
  2400. }
  2401. ret = napi_gro_frags(&rxq->rspq.napi);
  2402. if (ret == GRO_HELD)
  2403. rxq->stats.lro_pkts++;
  2404. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  2405. rxq->stats.lro_merged++;
  2406. rxq->stats.pkts++;
  2407. rxq->stats.rx_cso++;
  2408. }
  2409. enum {
  2410. RX_NON_PTP_PKT = 0,
  2411. RX_PTP_PKT_SUC = 1,
  2412. RX_PTP_PKT_ERR = 2
  2413. };
  2414. /**
  2415. * t4_systim_to_hwstamp - read hardware time stamp
  2416. * @adap: the adapter
  2417. * @skb: the packet
  2418. *
  2419. * Read Time Stamp from MPS packet and insert in skb which
  2420. * is forwarded to PTP application
  2421. */
  2422. static noinline int t4_systim_to_hwstamp(struct adapter *adapter,
  2423. struct sk_buff *skb)
  2424. {
  2425. struct skb_shared_hwtstamps *hwtstamps;
  2426. struct cpl_rx_mps_pkt *cpl = NULL;
  2427. unsigned char *data;
  2428. int offset;
  2429. cpl = (struct cpl_rx_mps_pkt *)skb->data;
  2430. if (!(CPL_RX_MPS_PKT_TYPE_G(ntohl(cpl->op_to_r1_hi)) &
  2431. X_CPL_RX_MPS_PKT_TYPE_PTP))
  2432. return RX_PTP_PKT_ERR;
  2433. data = skb->data + sizeof(*cpl);
  2434. skb_pull(skb, 2 * sizeof(u64) + sizeof(struct cpl_rx_mps_pkt));
  2435. offset = ETH_HLEN + IPV4_HLEN(skb->data) + UDP_HLEN;
  2436. if (skb->len < offset + OFF_PTP_SEQUENCE_ID + sizeof(short))
  2437. return RX_PTP_PKT_ERR;
  2438. hwtstamps = skb_hwtstamps(skb);
  2439. memset(hwtstamps, 0, sizeof(*hwtstamps));
  2440. hwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*((u64 *)data)));
  2441. return RX_PTP_PKT_SUC;
  2442. }
  2443. /**
  2444. * t4_rx_hststamp - Recv PTP Event Message
  2445. * @adap: the adapter
  2446. * @rsp: the response queue descriptor holding the RX_PKT message
  2447. * @skb: the packet
  2448. *
  2449. * PTP enabled and MPS packet, read HW timestamp
  2450. */
  2451. static int t4_rx_hststamp(struct adapter *adapter, const __be64 *rsp,
  2452. struct sge_eth_rxq *rxq, struct sk_buff *skb)
  2453. {
  2454. int ret;
  2455. if (unlikely((*(u8 *)rsp == CPL_RX_MPS_PKT) &&
  2456. !is_t4(adapter->params.chip))) {
  2457. ret = t4_systim_to_hwstamp(adapter, skb);
  2458. if (ret == RX_PTP_PKT_ERR) {
  2459. kfree_skb(skb);
  2460. rxq->stats.rx_drops++;
  2461. }
  2462. return ret;
  2463. }
  2464. return RX_NON_PTP_PKT;
  2465. }
  2466. /**
  2467. * t4_tx_hststamp - Loopback PTP Transmit Event Message
  2468. * @adap: the adapter
  2469. * @skb: the packet
  2470. * @dev: the ingress net device
  2471. *
  2472. * Read hardware timestamp for the loopback PTP Tx event message
  2473. */
  2474. static int t4_tx_hststamp(struct adapter *adapter, struct sk_buff *skb,
  2475. struct net_device *dev)
  2476. {
  2477. struct port_info *pi = netdev_priv(dev);
  2478. if (!is_t4(adapter->params.chip) && adapter->ptp_tx_skb) {
  2479. cxgb4_ptp_read_hwstamp(adapter, pi);
  2480. kfree_skb(skb);
  2481. return 0;
  2482. }
  2483. return 1;
  2484. }
  2485. /**
  2486. * t4_ethrx_handler - process an ingress ethernet packet
  2487. * @q: the response queue that received the packet
  2488. * @rsp: the response queue descriptor holding the RX_PKT message
  2489. * @si: the gather list of packet fragments
  2490. *
  2491. * Process an ingress ethernet packet and deliver it to the stack.
  2492. */
  2493. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  2494. const struct pkt_gl *si)
  2495. {
  2496. bool csum_ok;
  2497. struct sk_buff *skb;
  2498. const struct cpl_rx_pkt *pkt;
  2499. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  2500. struct adapter *adapter = q->adap;
  2501. struct sge *s = &q->adap->sge;
  2502. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  2503. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  2504. u16 err_vec, tnl_hdr_len = 0;
  2505. struct port_info *pi;
  2506. int ret = 0;
  2507. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  2508. return handle_trace_pkt(q->adap, si);
  2509. pkt = (const struct cpl_rx_pkt *)rsp;
  2510. /* Compressed error vector is enabled for T6 only */
  2511. if (q->adap->params.tp.rx_pkt_encap) {
  2512. err_vec = T6_COMPR_RXERR_VEC_G(be16_to_cpu(pkt->err_vec));
  2513. tnl_hdr_len = T6_RX_TNLHDR_LEN_G(ntohs(pkt->err_vec));
  2514. } else {
  2515. err_vec = be16_to_cpu(pkt->err_vec);
  2516. }
  2517. csum_ok = pkt->csum_calc && !err_vec &&
  2518. (q->netdev->features & NETIF_F_RXCSUM);
  2519. if (((pkt->l2info & htonl(RXF_TCP_F)) ||
  2520. tnl_hdr_len) &&
  2521. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  2522. do_gro(rxq, si, pkt, tnl_hdr_len);
  2523. return 0;
  2524. }
  2525. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  2526. if (unlikely(!skb)) {
  2527. t4_pktgl_free(si);
  2528. rxq->stats.rx_drops++;
  2529. return 0;
  2530. }
  2531. pi = netdev_priv(q->netdev);
  2532. /* Handle PTP Event Rx packet */
  2533. if (unlikely(pi->ptp_enable)) {
  2534. ret = t4_rx_hststamp(adapter, rsp, rxq, skb);
  2535. if (ret == RX_PTP_PKT_ERR)
  2536. return 0;
  2537. }
  2538. if (likely(!ret))
  2539. __skb_pull(skb, s->pktshift); /* remove ethernet header pad */
  2540. /* Handle the PTP Event Tx Loopback packet */
  2541. if (unlikely(pi->ptp_enable && !ret &&
  2542. (pkt->l2info & htonl(RXF_UDP_F)) &&
  2543. cxgb4_ptp_is_ptp_rx(skb))) {
  2544. if (!t4_tx_hststamp(adapter, skb, q->netdev))
  2545. return 0;
  2546. }
  2547. skb->protocol = eth_type_trans(skb, q->netdev);
  2548. skb_record_rx_queue(skb, q->idx);
  2549. if (skb->dev->features & NETIF_F_RXHASH)
  2550. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  2551. PKT_HASH_TYPE_L3);
  2552. rxq->stats.pkts++;
  2553. if (pi->rxtstamp)
  2554. cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
  2555. si->sgetstamp);
  2556. if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
  2557. if (!pkt->ip_frag) {
  2558. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2559. rxq->stats.rx_cso++;
  2560. } else if (pkt->l2info & htonl(RXF_IP_F)) {
  2561. __sum16 c = (__force __sum16)pkt->csum;
  2562. skb->csum = csum_unfold(c);
  2563. if (tnl_hdr_len) {
  2564. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2565. skb->csum_level = 1;
  2566. } else {
  2567. skb->ip_summed = CHECKSUM_COMPLETE;
  2568. }
  2569. rxq->stats.rx_cso++;
  2570. }
  2571. } else {
  2572. skb_checksum_none_assert(skb);
  2573. #ifdef CONFIG_CHELSIO_T4_FCOE
  2574. #define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
  2575. RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
  2576. if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
  2577. if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
  2578. (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
  2579. if (q->adap->params.tp.rx_pkt_encap)
  2580. csum_ok = err_vec &
  2581. T6_COMPR_RXERR_SUM_F;
  2582. else
  2583. csum_ok = err_vec & RXERR_CSUM_F;
  2584. if (!csum_ok)
  2585. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2586. }
  2587. }
  2588. #undef CPL_RX_PKT_FLAGS
  2589. #endif /* CONFIG_CHELSIO_T4_FCOE */
  2590. }
  2591. if (unlikely(pkt->vlan_ex)) {
  2592. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  2593. rxq->stats.vlan_ex++;
  2594. }
  2595. skb_mark_napi_id(skb, &q->napi);
  2596. netif_receive_skb(skb);
  2597. return 0;
  2598. }
  2599. /**
  2600. * restore_rx_bufs - put back a packet's Rx buffers
  2601. * @si: the packet gather list
  2602. * @q: the SGE free list
  2603. * @frags: number of FL buffers to restore
  2604. *
  2605. * Puts back on an FL the Rx buffers associated with @si. The buffers
  2606. * have already been unmapped and are left unmapped, we mark them so to
  2607. * prevent further unmapping attempts.
  2608. *
  2609. * This function undoes a series of @unmap_rx_buf calls when we find out
  2610. * that the current packet can't be processed right away afterall and we
  2611. * need to come back to it later. This is a very rare event and there's
  2612. * no effort to make this particularly efficient.
  2613. */
  2614. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  2615. int frags)
  2616. {
  2617. struct rx_sw_desc *d;
  2618. while (frags--) {
  2619. if (q->cidx == 0)
  2620. q->cidx = q->size - 1;
  2621. else
  2622. q->cidx--;
  2623. d = &q->sdesc[q->cidx];
  2624. d->page = si->frags[frags].page;
  2625. d->dma_addr |= RX_UNMAPPED_BUF;
  2626. q->avail++;
  2627. }
  2628. }
  2629. /**
  2630. * is_new_response - check if a response is newly written
  2631. * @r: the response descriptor
  2632. * @q: the response queue
  2633. *
  2634. * Returns true if a response descriptor contains a yet unprocessed
  2635. * response.
  2636. */
  2637. static inline bool is_new_response(const struct rsp_ctrl *r,
  2638. const struct sge_rspq *q)
  2639. {
  2640. return (r->type_gen >> RSPD_GEN_S) == q->gen;
  2641. }
  2642. /**
  2643. * rspq_next - advance to the next entry in a response queue
  2644. * @q: the queue
  2645. *
  2646. * Updates the state of a response queue to advance it to the next entry.
  2647. */
  2648. static inline void rspq_next(struct sge_rspq *q)
  2649. {
  2650. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  2651. if (unlikely(++q->cidx == q->size)) {
  2652. q->cidx = 0;
  2653. q->gen ^= 1;
  2654. q->cur_desc = q->desc;
  2655. }
  2656. }
  2657. /**
  2658. * process_responses - process responses from an SGE response queue
  2659. * @q: the ingress queue to process
  2660. * @budget: how many responses can be processed in this round
  2661. *
  2662. * Process responses from an SGE response queue up to the supplied budget.
  2663. * Responses include received packets as well as control messages from FW
  2664. * or HW.
  2665. *
  2666. * Additionally choose the interrupt holdoff time for the next interrupt
  2667. * on this queue. If the system is under memory shortage use a fairly
  2668. * long delay to help recovery.
  2669. */
  2670. static int process_responses(struct sge_rspq *q, int budget)
  2671. {
  2672. int ret, rsp_type;
  2673. int budget_left = budget;
  2674. const struct rsp_ctrl *rc;
  2675. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  2676. struct adapter *adapter = q->adap;
  2677. struct sge *s = &adapter->sge;
  2678. while (likely(budget_left)) {
  2679. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  2680. if (!is_new_response(rc, q)) {
  2681. if (q->flush_handler)
  2682. q->flush_handler(q);
  2683. break;
  2684. }
  2685. dma_rmb();
  2686. rsp_type = RSPD_TYPE_G(rc->type_gen);
  2687. if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
  2688. struct page_frag *fp;
  2689. struct pkt_gl si;
  2690. const struct rx_sw_desc *rsd;
  2691. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  2692. if (len & RSPD_NEWBUF_F) {
  2693. if (likely(q->offset > 0)) {
  2694. free_rx_bufs(q->adap, &rxq->fl, 1);
  2695. q->offset = 0;
  2696. }
  2697. len = RSPD_LEN_G(len);
  2698. }
  2699. si.tot_len = len;
  2700. /* gather packet fragments */
  2701. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  2702. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  2703. bufsz = get_buf_size(adapter, rsd);
  2704. fp->page = rsd->page;
  2705. fp->offset = q->offset;
  2706. fp->size = min(bufsz, len);
  2707. len -= fp->size;
  2708. if (!len)
  2709. break;
  2710. unmap_rx_buf(q->adap, &rxq->fl);
  2711. }
  2712. si.sgetstamp = SGE_TIMESTAMP_G(
  2713. be64_to_cpu(rc->last_flit));
  2714. /*
  2715. * Last buffer remains mapped so explicitly make it
  2716. * coherent for CPU access.
  2717. */
  2718. dma_sync_single_for_cpu(q->adap->pdev_dev,
  2719. get_buf_addr(rsd),
  2720. fp->size, DMA_FROM_DEVICE);
  2721. si.va = page_address(si.frags[0].page) +
  2722. si.frags[0].offset;
  2723. prefetch(si.va);
  2724. si.nfrags = frags + 1;
  2725. ret = q->handler(q, q->cur_desc, &si);
  2726. if (likely(ret == 0))
  2727. q->offset += ALIGN(fp->size, s->fl_align);
  2728. else
  2729. restore_rx_bufs(&si, &rxq->fl, frags);
  2730. } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
  2731. ret = q->handler(q, q->cur_desc, NULL);
  2732. } else {
  2733. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  2734. }
  2735. if (unlikely(ret)) {
  2736. /* couldn't process descriptor, back off for recovery */
  2737. q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
  2738. break;
  2739. }
  2740. rspq_next(q);
  2741. budget_left--;
  2742. }
  2743. if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
  2744. __refill_fl(q->adap, &rxq->fl);
  2745. return budget - budget_left;
  2746. }
  2747. /**
  2748. * napi_rx_handler - the NAPI handler for Rx processing
  2749. * @napi: the napi instance
  2750. * @budget: how many packets we can process in this round
  2751. *
  2752. * Handler for new data events when using NAPI. This does not need any
  2753. * locking or protection from interrupts as data interrupts are off at
  2754. * this point and other adapter interrupts do not interfere (the latter
  2755. * in not a concern at all with MSI-X as non-data interrupts then have
  2756. * a separate handler).
  2757. */
  2758. static int napi_rx_handler(struct napi_struct *napi, int budget)
  2759. {
  2760. unsigned int params;
  2761. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  2762. int work_done;
  2763. u32 val;
  2764. work_done = process_responses(q, budget);
  2765. if (likely(work_done < budget)) {
  2766. int timer_index;
  2767. napi_complete_done(napi, work_done);
  2768. timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
  2769. if (q->adaptive_rx) {
  2770. if (work_done > max(timer_pkt_quota[timer_index],
  2771. MIN_NAPI_WORK))
  2772. timer_index = (timer_index + 1);
  2773. else
  2774. timer_index = timer_index - 1;
  2775. timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
  2776. q->next_intr_params =
  2777. QINTR_TIMER_IDX_V(timer_index) |
  2778. QINTR_CNT_EN_V(0);
  2779. params = q->next_intr_params;
  2780. } else {
  2781. params = q->next_intr_params;
  2782. q->next_intr_params = q->intr_params;
  2783. }
  2784. } else
  2785. params = QINTR_TIMER_IDX_V(7);
  2786. val = CIDXINC_V(work_done) | SEINTARM_V(params);
  2787. /* If we don't have access to the new User GTS (T5+), use the old
  2788. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2789. */
  2790. if (unlikely(q->bar2_addr == NULL)) {
  2791. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
  2792. val | INGRESSQID_V((u32)q->cntxt_id));
  2793. } else {
  2794. writel(val | INGRESSQID_V(q->bar2_qid),
  2795. q->bar2_addr + SGE_UDB_GTS);
  2796. wmb();
  2797. }
  2798. return work_done;
  2799. }
  2800. /*
  2801. * The MSI-X interrupt handler for an SGE response queue.
  2802. */
  2803. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  2804. {
  2805. struct sge_rspq *q = cookie;
  2806. napi_schedule(&q->napi);
  2807. return IRQ_HANDLED;
  2808. }
  2809. /*
  2810. * Process the indirect interrupt entries in the interrupt queue and kick off
  2811. * NAPI for each queue that has generated an entry.
  2812. */
  2813. static unsigned int process_intrq(struct adapter *adap)
  2814. {
  2815. unsigned int credits;
  2816. const struct rsp_ctrl *rc;
  2817. struct sge_rspq *q = &adap->sge.intrq;
  2818. u32 val;
  2819. spin_lock(&adap->sge.intrq_lock);
  2820. for (credits = 0; ; credits++) {
  2821. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  2822. if (!is_new_response(rc, q))
  2823. break;
  2824. dma_rmb();
  2825. if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
  2826. unsigned int qid = ntohl(rc->pldbuflen_qid);
  2827. qid -= adap->sge.ingr_start;
  2828. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  2829. }
  2830. rspq_next(q);
  2831. }
  2832. val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
  2833. /* If we don't have access to the new User GTS (T5+), use the old
  2834. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2835. */
  2836. if (unlikely(q->bar2_addr == NULL)) {
  2837. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
  2838. val | INGRESSQID_V(q->cntxt_id));
  2839. } else {
  2840. writel(val | INGRESSQID_V(q->bar2_qid),
  2841. q->bar2_addr + SGE_UDB_GTS);
  2842. wmb();
  2843. }
  2844. spin_unlock(&adap->sge.intrq_lock);
  2845. return credits;
  2846. }
  2847. /*
  2848. * The MSI interrupt handler, which handles data events from SGE response queues
  2849. * as well as error and other async events as they all use the same MSI vector.
  2850. */
  2851. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  2852. {
  2853. struct adapter *adap = cookie;
  2854. if (adap->flags & MASTER_PF)
  2855. t4_slow_intr_handler(adap);
  2856. process_intrq(adap);
  2857. return IRQ_HANDLED;
  2858. }
  2859. /*
  2860. * Interrupt handler for legacy INTx interrupts.
  2861. * Handles data events from SGE response queues as well as error and other
  2862. * async events as they all use the same interrupt line.
  2863. */
  2864. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  2865. {
  2866. struct adapter *adap = cookie;
  2867. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
  2868. if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
  2869. process_intrq(adap))
  2870. return IRQ_HANDLED;
  2871. return IRQ_NONE; /* probably shared interrupt */
  2872. }
  2873. /**
  2874. * t4_intr_handler - select the top-level interrupt handler
  2875. * @adap: the adapter
  2876. *
  2877. * Selects the top-level interrupt handler based on the type of interrupts
  2878. * (MSI-X, MSI, or INTx).
  2879. */
  2880. irq_handler_t t4_intr_handler(struct adapter *adap)
  2881. {
  2882. if (adap->flags & USING_MSIX)
  2883. return t4_sge_intr_msix;
  2884. if (adap->flags & USING_MSI)
  2885. return t4_intr_msi;
  2886. return t4_intr_intx;
  2887. }
  2888. static void sge_rx_timer_cb(struct timer_list *t)
  2889. {
  2890. unsigned long m;
  2891. unsigned int i;
  2892. struct adapter *adap = from_timer(adap, t, sge.rx_timer);
  2893. struct sge *s = &adap->sge;
  2894. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2895. for (m = s->starving_fl[i]; m; m &= m - 1) {
  2896. struct sge_eth_rxq *rxq;
  2897. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  2898. struct sge_fl *fl = s->egr_map[id];
  2899. clear_bit(id, s->starving_fl);
  2900. smp_mb__after_atomic();
  2901. if (fl_starving(adap, fl)) {
  2902. rxq = container_of(fl, struct sge_eth_rxq, fl);
  2903. if (napi_reschedule(&rxq->rspq.napi))
  2904. fl->starving++;
  2905. else
  2906. set_bit(id, s->starving_fl);
  2907. }
  2908. }
  2909. /* The remainder of the SGE RX Timer Callback routine is dedicated to
  2910. * global Master PF activities like checking for chip ingress stalls,
  2911. * etc.
  2912. */
  2913. if (!(adap->flags & MASTER_PF))
  2914. goto done;
  2915. t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
  2916. done:
  2917. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  2918. }
  2919. static void sge_tx_timer_cb(struct timer_list *t)
  2920. {
  2921. unsigned long m;
  2922. unsigned int i, budget;
  2923. struct adapter *adap = from_timer(adap, t, sge.tx_timer);
  2924. struct sge *s = &adap->sge;
  2925. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2926. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  2927. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  2928. struct sge_uld_txq *txq = s->egr_map[id];
  2929. clear_bit(id, s->txq_maperr);
  2930. tasklet_schedule(&txq->qresume_tsk);
  2931. }
  2932. if (!is_t4(adap->params.chip)) {
  2933. struct sge_eth_txq *q = &s->ptptxq;
  2934. int avail;
  2935. spin_lock(&adap->ptp_lock);
  2936. avail = reclaimable(&q->q);
  2937. if (avail) {
  2938. free_tx_desc(adap, &q->q, avail, false);
  2939. q->q.in_use -= avail;
  2940. }
  2941. spin_unlock(&adap->ptp_lock);
  2942. }
  2943. budget = MAX_TIMER_TX_RECLAIM;
  2944. i = s->ethtxq_rover;
  2945. do {
  2946. struct sge_eth_txq *q = &s->ethtxq[i];
  2947. if (q->q.in_use &&
  2948. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  2949. __netif_tx_trylock(q->txq)) {
  2950. int avail = reclaimable(&q->q);
  2951. if (avail) {
  2952. if (avail > budget)
  2953. avail = budget;
  2954. free_tx_desc(adap, &q->q, avail, true);
  2955. q->q.in_use -= avail;
  2956. budget -= avail;
  2957. }
  2958. __netif_tx_unlock(q->txq);
  2959. }
  2960. if (++i >= s->ethqsets)
  2961. i = 0;
  2962. } while (budget && i != s->ethtxq_rover);
  2963. s->ethtxq_rover = i;
  2964. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  2965. }
  2966. /**
  2967. * bar2_address - return the BAR2 address for an SGE Queue's Registers
  2968. * @adapter: the adapter
  2969. * @qid: the SGE Queue ID
  2970. * @qtype: the SGE Queue Type (Egress or Ingress)
  2971. * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
  2972. *
  2973. * Returns the BAR2 address for the SGE Queue Registers associated with
  2974. * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
  2975. * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
  2976. * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
  2977. * Registers are supported (e.g. the Write Combining Doorbell Buffer).
  2978. */
  2979. static void __iomem *bar2_address(struct adapter *adapter,
  2980. unsigned int qid,
  2981. enum t4_bar2_qtype qtype,
  2982. unsigned int *pbar2_qid)
  2983. {
  2984. u64 bar2_qoffset;
  2985. int ret;
  2986. ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
  2987. &bar2_qoffset, pbar2_qid);
  2988. if (ret)
  2989. return NULL;
  2990. return adapter->bar2 + bar2_qoffset;
  2991. }
  2992. /* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
  2993. * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
  2994. */
  2995. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  2996. struct net_device *dev, int intr_idx,
  2997. struct sge_fl *fl, rspq_handler_t hnd,
  2998. rspq_flush_handler_t flush_hnd, int cong)
  2999. {
  3000. int ret, flsz = 0;
  3001. struct fw_iq_cmd c;
  3002. struct sge *s = &adap->sge;
  3003. struct port_info *pi = netdev_priv(dev);
  3004. int relaxed = !(adap->flags & ROOT_NO_RELAXED_ORDERING);
  3005. /* Size needs to be multiple of 16, including status entry. */
  3006. iq->size = roundup(iq->size, 16);
  3007. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  3008. &iq->phys_addr, NULL, 0,
  3009. dev_to_node(adap->pdev_dev));
  3010. if (!iq->desc)
  3011. return -ENOMEM;
  3012. memset(&c, 0, sizeof(c));
  3013. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
  3014. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  3015. FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
  3016. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
  3017. FW_LEN16(c));
  3018. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
  3019. FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
  3020. FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
  3021. FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
  3022. FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
  3023. -intr_idx - 1));
  3024. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
  3025. FW_IQ_CMD_IQGTSMODE_F |
  3026. FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
  3027. FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
  3028. c.iqsize = htons(iq->size);
  3029. c.iqaddr = cpu_to_be64(iq->phys_addr);
  3030. if (cong >= 0)
  3031. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F |
  3032. FW_IQ_CMD_IQTYPE_V(cong ? FW_IQ_IQTYPE_NIC
  3033. : FW_IQ_IQTYPE_OFLD));
  3034. if (fl) {
  3035. enum chip_type chip = CHELSIO_CHIP_VERSION(adap->params.chip);
  3036. /* Allocate the ring for the hardware free list (with space
  3037. * for its status page) along with the associated software
  3038. * descriptor ring. The free list size needs to be a multiple
  3039. * of the Egress Queue Unit and at least 2 Egress Units larger
  3040. * than the SGE's Egress Congrestion Threshold
  3041. * (fl_starve_thres - 1).
  3042. */
  3043. if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
  3044. fl->size = s->fl_starve_thres - 1 + 2 * 8;
  3045. fl->size = roundup(fl->size, 8);
  3046. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  3047. sizeof(struct rx_sw_desc), &fl->addr,
  3048. &fl->sdesc, s->stat_len,
  3049. dev_to_node(adap->pdev_dev));
  3050. if (!fl->desc)
  3051. goto fl_nomem;
  3052. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  3053. c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
  3054. FW_IQ_CMD_FL0FETCHRO_V(relaxed) |
  3055. FW_IQ_CMD_FL0DATARO_V(relaxed) |
  3056. FW_IQ_CMD_FL0PADEN_F);
  3057. if (cong >= 0)
  3058. c.iqns_to_fl0congen |=
  3059. htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
  3060. FW_IQ_CMD_FL0CONGCIF_F |
  3061. FW_IQ_CMD_FL0CONGEN_F);
  3062. /* In T6, for egress queue type FL there is internal overhead
  3063. * of 16B for header going into FLM module. Hence the maximum
  3064. * allowed burst size is 448 bytes. For T4/T5, the hardware
  3065. * doesn't coalesce fetch requests if more than 64 bytes of
  3066. * Free List pointers are provided, so we use a 128-byte Fetch
  3067. * Burst Minimum there (T6 implements coalescing so we can use
  3068. * the smaller 64-byte value there).
  3069. */
  3070. c.fl0dcaen_to_fl0cidxfthresh =
  3071. htons(FW_IQ_CMD_FL0FBMIN_V(chip <= CHELSIO_T5 ?
  3072. FETCHBURSTMIN_128B_X :
  3073. FETCHBURSTMIN_64B_X) |
  3074. FW_IQ_CMD_FL0FBMAX_V((chip <= CHELSIO_T5) ?
  3075. FETCHBURSTMAX_512B_X :
  3076. FETCHBURSTMAX_256B_X));
  3077. c.fl0size = htons(flsz);
  3078. c.fl0addr = cpu_to_be64(fl->addr);
  3079. }
  3080. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  3081. if (ret)
  3082. goto err;
  3083. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  3084. iq->cur_desc = iq->desc;
  3085. iq->cidx = 0;
  3086. iq->gen = 1;
  3087. iq->next_intr_params = iq->intr_params;
  3088. iq->cntxt_id = ntohs(c.iqid);
  3089. iq->abs_id = ntohs(c.physiqid);
  3090. iq->bar2_addr = bar2_address(adap,
  3091. iq->cntxt_id,
  3092. T4_BAR2_QTYPE_INGRESS,
  3093. &iq->bar2_qid);
  3094. iq->size--; /* subtract status entry */
  3095. iq->netdev = dev;
  3096. iq->handler = hnd;
  3097. iq->flush_handler = flush_hnd;
  3098. memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
  3099. skb_queue_head_init(&iq->lro_mgr.lroq);
  3100. /* set offset to -1 to distinguish ingress queues without FL */
  3101. iq->offset = fl ? 0 : -1;
  3102. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  3103. if (fl) {
  3104. fl->cntxt_id = ntohs(c.fl0id);
  3105. fl->avail = fl->pend_cred = 0;
  3106. fl->pidx = fl->cidx = 0;
  3107. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  3108. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  3109. /* Note, we must initialize the BAR2 Free List User Doorbell
  3110. * information before refilling the Free List!
  3111. */
  3112. fl->bar2_addr = bar2_address(adap,
  3113. fl->cntxt_id,
  3114. T4_BAR2_QTYPE_EGRESS,
  3115. &fl->bar2_qid);
  3116. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  3117. }
  3118. /* For T5 and later we attempt to set up the Congestion Manager values
  3119. * of the new RX Ethernet Queue. This should really be handled by
  3120. * firmware because it's more complex than any host driver wants to
  3121. * get involved with and it's different per chip and this is almost
  3122. * certainly wrong. Firmware would be wrong as well, but it would be
  3123. * a lot easier to fix in one place ... For now we do something very
  3124. * simple (and hopefully less wrong).
  3125. */
  3126. if (!is_t4(adap->params.chip) && cong >= 0) {
  3127. u32 param, val, ch_map = 0;
  3128. int i;
  3129. u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
  3130. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  3131. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
  3132. FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
  3133. if (cong == 0) {
  3134. val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
  3135. } else {
  3136. val =
  3137. CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
  3138. for (i = 0; i < 4; i++) {
  3139. if (cong & (1 << i))
  3140. ch_map |= 1 << (i << cng_ch_bits_log);
  3141. }
  3142. val |= CONMCTXT_CNGCHMAP_V(ch_map);
  3143. }
  3144. ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
  3145. &param, &val);
  3146. if (ret)
  3147. dev_warn(adap->pdev_dev, "Failed to set Congestion"
  3148. " Manager Context for Ingress Queue %d: %d\n",
  3149. iq->cntxt_id, -ret);
  3150. }
  3151. return 0;
  3152. fl_nomem:
  3153. ret = -ENOMEM;
  3154. err:
  3155. if (iq->desc) {
  3156. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  3157. iq->desc, iq->phys_addr);
  3158. iq->desc = NULL;
  3159. }
  3160. if (fl && fl->desc) {
  3161. kfree(fl->sdesc);
  3162. fl->sdesc = NULL;
  3163. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  3164. fl->desc, fl->addr);
  3165. fl->desc = NULL;
  3166. }
  3167. return ret;
  3168. }
  3169. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  3170. {
  3171. q->cntxt_id = id;
  3172. q->bar2_addr = bar2_address(adap,
  3173. q->cntxt_id,
  3174. T4_BAR2_QTYPE_EGRESS,
  3175. &q->bar2_qid);
  3176. q->in_use = 0;
  3177. q->cidx = q->pidx = 0;
  3178. q->stops = q->restarts = 0;
  3179. q->stat = (void *)&q->desc[q->size];
  3180. spin_lock_init(&q->db_lock);
  3181. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  3182. }
  3183. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  3184. struct net_device *dev, struct netdev_queue *netdevq,
  3185. unsigned int iqid)
  3186. {
  3187. int ret, nentries;
  3188. struct fw_eq_eth_cmd c;
  3189. struct sge *s = &adap->sge;
  3190. struct port_info *pi = netdev_priv(dev);
  3191. /* Add status entries */
  3192. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  3193. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  3194. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  3195. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  3196. netdev_queue_numa_node_read(netdevq));
  3197. if (!txq->q.desc)
  3198. return -ENOMEM;
  3199. memset(&c, 0, sizeof(c));
  3200. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
  3201. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  3202. FW_EQ_ETH_CMD_PFN_V(adap->pf) |
  3203. FW_EQ_ETH_CMD_VFN_V(0));
  3204. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
  3205. FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
  3206. c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
  3207. FW_EQ_ETH_CMD_VIID_V(pi->viid));
  3208. c.fetchszm_to_iqid =
  3209. htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  3210. FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
  3211. FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
  3212. c.dcaen_to_eqsize =
  3213. htonl(FW_EQ_ETH_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  3214. FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  3215. FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  3216. FW_EQ_ETH_CMD_EQSIZE_V(nentries));
  3217. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  3218. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  3219. if (ret) {
  3220. kfree(txq->q.sdesc);
  3221. txq->q.sdesc = NULL;
  3222. dma_free_coherent(adap->pdev_dev,
  3223. nentries * sizeof(struct tx_desc),
  3224. txq->q.desc, txq->q.phys_addr);
  3225. txq->q.desc = NULL;
  3226. return ret;
  3227. }
  3228. txq->q.q_type = CXGB4_TXQ_ETH;
  3229. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
  3230. txq->txq = netdevq;
  3231. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  3232. txq->mapping_err = 0;
  3233. return 0;
  3234. }
  3235. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  3236. struct net_device *dev, unsigned int iqid,
  3237. unsigned int cmplqid)
  3238. {
  3239. int ret, nentries;
  3240. struct fw_eq_ctrl_cmd c;
  3241. struct sge *s = &adap->sge;
  3242. struct port_info *pi = netdev_priv(dev);
  3243. /* Add status entries */
  3244. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  3245. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  3246. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  3247. NULL, 0, dev_to_node(adap->pdev_dev));
  3248. if (!txq->q.desc)
  3249. return -ENOMEM;
  3250. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
  3251. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  3252. FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
  3253. FW_EQ_CTRL_CMD_VFN_V(0));
  3254. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
  3255. FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
  3256. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
  3257. c.physeqid_pkd = htonl(0);
  3258. c.fetchszm_to_iqid =
  3259. htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  3260. FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
  3261. FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
  3262. c.dcaen_to_eqsize =
  3263. htonl(FW_EQ_CTRL_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  3264. FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  3265. FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  3266. FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
  3267. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  3268. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  3269. if (ret) {
  3270. dma_free_coherent(adap->pdev_dev,
  3271. nentries * sizeof(struct tx_desc),
  3272. txq->q.desc, txq->q.phys_addr);
  3273. txq->q.desc = NULL;
  3274. return ret;
  3275. }
  3276. txq->q.q_type = CXGB4_TXQ_CTRL;
  3277. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
  3278. txq->adap = adap;
  3279. skb_queue_head_init(&txq->sendq);
  3280. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  3281. txq->full = 0;
  3282. return 0;
  3283. }
  3284. int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
  3285. unsigned int cmplqid)
  3286. {
  3287. u32 param, val;
  3288. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  3289. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
  3290. FW_PARAMS_PARAM_YZ_V(eqid));
  3291. val = cmplqid;
  3292. return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
  3293. }
  3294. int t4_sge_alloc_uld_txq(struct adapter *adap, struct sge_uld_txq *txq,
  3295. struct net_device *dev, unsigned int iqid,
  3296. unsigned int uld_type)
  3297. {
  3298. int ret, nentries;
  3299. struct fw_eq_ofld_cmd c;
  3300. struct sge *s = &adap->sge;
  3301. struct port_info *pi = netdev_priv(dev);
  3302. int cmd = FW_EQ_OFLD_CMD;
  3303. /* Add status entries */
  3304. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  3305. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  3306. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  3307. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  3308. NUMA_NO_NODE);
  3309. if (!txq->q.desc)
  3310. return -ENOMEM;
  3311. memset(&c, 0, sizeof(c));
  3312. if (unlikely(uld_type == CXGB4_TX_CRYPTO))
  3313. cmd = FW_EQ_CTRL_CMD;
  3314. c.op_to_vfn = htonl(FW_CMD_OP_V(cmd) | FW_CMD_REQUEST_F |
  3315. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  3316. FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
  3317. FW_EQ_OFLD_CMD_VFN_V(0));
  3318. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
  3319. FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
  3320. c.fetchszm_to_iqid =
  3321. htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  3322. FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
  3323. FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
  3324. c.dcaen_to_eqsize =
  3325. htonl(FW_EQ_OFLD_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  3326. FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  3327. FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  3328. FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
  3329. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  3330. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  3331. if (ret) {
  3332. kfree(txq->q.sdesc);
  3333. txq->q.sdesc = NULL;
  3334. dma_free_coherent(adap->pdev_dev,
  3335. nentries * sizeof(struct tx_desc),
  3336. txq->q.desc, txq->q.phys_addr);
  3337. txq->q.desc = NULL;
  3338. return ret;
  3339. }
  3340. txq->q.q_type = CXGB4_TXQ_ULD;
  3341. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
  3342. txq->adap = adap;
  3343. skb_queue_head_init(&txq->sendq);
  3344. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  3345. txq->full = 0;
  3346. txq->mapping_err = 0;
  3347. return 0;
  3348. }
  3349. void free_txq(struct adapter *adap, struct sge_txq *q)
  3350. {
  3351. struct sge *s = &adap->sge;
  3352. dma_free_coherent(adap->pdev_dev,
  3353. q->size * sizeof(struct tx_desc) + s->stat_len,
  3354. q->desc, q->phys_addr);
  3355. q->cntxt_id = 0;
  3356. q->sdesc = NULL;
  3357. q->desc = NULL;
  3358. }
  3359. void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  3360. struct sge_fl *fl)
  3361. {
  3362. struct sge *s = &adap->sge;
  3363. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  3364. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  3365. t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
  3366. rq->cntxt_id, fl_id, 0xffff);
  3367. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  3368. rq->desc, rq->phys_addr);
  3369. netif_napi_del(&rq->napi);
  3370. rq->netdev = NULL;
  3371. rq->cntxt_id = rq->abs_id = 0;
  3372. rq->desc = NULL;
  3373. if (fl) {
  3374. free_rx_bufs(adap, fl, fl->avail);
  3375. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  3376. fl->desc, fl->addr);
  3377. kfree(fl->sdesc);
  3378. fl->sdesc = NULL;
  3379. fl->cntxt_id = 0;
  3380. fl->desc = NULL;
  3381. }
  3382. }
  3383. /**
  3384. * t4_free_ofld_rxqs - free a block of consecutive Rx queues
  3385. * @adap: the adapter
  3386. * @n: number of queues
  3387. * @q: pointer to first queue
  3388. *
  3389. * Release the resources of a consecutive block of offload Rx queues.
  3390. */
  3391. void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
  3392. {
  3393. for ( ; n; n--, q++)
  3394. if (q->rspq.desc)
  3395. free_rspq_fl(adap, &q->rspq,
  3396. q->fl.size ? &q->fl : NULL);
  3397. }
  3398. /**
  3399. * t4_free_sge_resources - free SGE resources
  3400. * @adap: the adapter
  3401. *
  3402. * Frees resources used by the SGE queue sets.
  3403. */
  3404. void t4_free_sge_resources(struct adapter *adap)
  3405. {
  3406. int i;
  3407. struct sge_eth_rxq *eq;
  3408. struct sge_eth_txq *etq;
  3409. /* stop all Rx queues in order to start them draining */
  3410. for (i = 0; i < adap->sge.ethqsets; i++) {
  3411. eq = &adap->sge.ethrxq[i];
  3412. if (eq->rspq.desc)
  3413. t4_iq_stop(adap, adap->mbox, adap->pf, 0,
  3414. FW_IQ_TYPE_FL_INT_CAP,
  3415. eq->rspq.cntxt_id,
  3416. eq->fl.size ? eq->fl.cntxt_id : 0xffff,
  3417. 0xffff);
  3418. }
  3419. /* clean up Ethernet Tx/Rx queues */
  3420. for (i = 0; i < adap->sge.ethqsets; i++) {
  3421. eq = &adap->sge.ethrxq[i];
  3422. if (eq->rspq.desc)
  3423. free_rspq_fl(adap, &eq->rspq,
  3424. eq->fl.size ? &eq->fl : NULL);
  3425. etq = &adap->sge.ethtxq[i];
  3426. if (etq->q.desc) {
  3427. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  3428. etq->q.cntxt_id);
  3429. __netif_tx_lock_bh(etq->txq);
  3430. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  3431. __netif_tx_unlock_bh(etq->txq);
  3432. kfree(etq->q.sdesc);
  3433. free_txq(adap, &etq->q);
  3434. }
  3435. }
  3436. /* clean up control Tx queues */
  3437. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  3438. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  3439. if (cq->q.desc) {
  3440. tasklet_kill(&cq->qresume_tsk);
  3441. t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
  3442. cq->q.cntxt_id);
  3443. __skb_queue_purge(&cq->sendq);
  3444. free_txq(adap, &cq->q);
  3445. }
  3446. }
  3447. if (adap->sge.fw_evtq.desc)
  3448. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  3449. if (adap->sge.intrq.desc)
  3450. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  3451. if (!is_t4(adap->params.chip)) {
  3452. etq = &adap->sge.ptptxq;
  3453. if (etq->q.desc) {
  3454. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  3455. etq->q.cntxt_id);
  3456. spin_lock_bh(&adap->ptp_lock);
  3457. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  3458. spin_unlock_bh(&adap->ptp_lock);
  3459. kfree(etq->q.sdesc);
  3460. free_txq(adap, &etq->q);
  3461. }
  3462. }
  3463. /* clear the reverse egress queue map */
  3464. memset(adap->sge.egr_map, 0,
  3465. adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
  3466. }
  3467. void t4_sge_start(struct adapter *adap)
  3468. {
  3469. adap->sge.ethtxq_rover = 0;
  3470. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  3471. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  3472. }
  3473. /**
  3474. * t4_sge_stop - disable SGE operation
  3475. * @adap: the adapter
  3476. *
  3477. * Stop tasklets and timers associated with the DMA engine. Note that
  3478. * this is effective only if measures have been taken to disable any HW
  3479. * events that may restart them.
  3480. */
  3481. void t4_sge_stop(struct adapter *adap)
  3482. {
  3483. int i;
  3484. struct sge *s = &adap->sge;
  3485. if (in_interrupt()) /* actions below require waiting */
  3486. return;
  3487. if (s->rx_timer.function)
  3488. del_timer_sync(&s->rx_timer);
  3489. if (s->tx_timer.function)
  3490. del_timer_sync(&s->tx_timer);
  3491. if (is_offload(adap)) {
  3492. struct sge_uld_txq_info *txq_info;
  3493. txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
  3494. if (txq_info) {
  3495. struct sge_uld_txq *txq = txq_info->uldtxq;
  3496. for_each_ofldtxq(&adap->sge, i) {
  3497. if (txq->q.desc)
  3498. tasklet_kill(&txq->qresume_tsk);
  3499. }
  3500. }
  3501. }
  3502. if (is_pci_uld(adap)) {
  3503. struct sge_uld_txq_info *txq_info;
  3504. txq_info = adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
  3505. if (txq_info) {
  3506. struct sge_uld_txq *txq = txq_info->uldtxq;
  3507. for_each_ofldtxq(&adap->sge, i) {
  3508. if (txq->q.desc)
  3509. tasklet_kill(&txq->qresume_tsk);
  3510. }
  3511. }
  3512. }
  3513. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  3514. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  3515. if (cq->q.desc)
  3516. tasklet_kill(&cq->qresume_tsk);
  3517. }
  3518. }
  3519. /**
  3520. * t4_sge_init_soft - grab core SGE values needed by SGE code
  3521. * @adap: the adapter
  3522. *
  3523. * We need to grab the SGE operating parameters that we need to have
  3524. * in order to do our job and make sure we can live with them.
  3525. */
  3526. static int t4_sge_init_soft(struct adapter *adap)
  3527. {
  3528. struct sge *s = &adap->sge;
  3529. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  3530. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  3531. u32 ingress_rx_threshold;
  3532. /*
  3533. * Verify that CPL messages are going to the Ingress Queue for
  3534. * process_responses() and that only packet data is going to the
  3535. * Free Lists.
  3536. */
  3537. if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
  3538. RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
  3539. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  3540. return -EINVAL;
  3541. }
  3542. /*
  3543. * Validate the Host Buffer Register Array indices that we want to
  3544. * use ...
  3545. *
  3546. * XXX Note that we should really read through the Host Buffer Size
  3547. * XXX register array and find the indices of the Buffer Sizes which
  3548. * XXX meet our needs!
  3549. */
  3550. #define READ_FL_BUF(x) \
  3551. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
  3552. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  3553. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  3554. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  3555. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  3556. /* We only bother using the Large Page logic if the Large Page Buffer
  3557. * is larger than our Page Size Buffer.
  3558. */
  3559. if (fl_large_pg <= fl_small_pg)
  3560. fl_large_pg = 0;
  3561. #undef READ_FL_BUF
  3562. /* The Page Size Buffer must be exactly equal to our Page Size and the
  3563. * Large Page Size Buffer should be 0 (per above) or a power of 2.
  3564. */
  3565. if (fl_small_pg != PAGE_SIZE ||
  3566. (fl_large_pg & (fl_large_pg-1)) != 0) {
  3567. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  3568. fl_small_pg, fl_large_pg);
  3569. return -EINVAL;
  3570. }
  3571. if (fl_large_pg)
  3572. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  3573. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  3574. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  3575. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  3576. fl_small_mtu, fl_large_mtu);
  3577. return -EINVAL;
  3578. }
  3579. /*
  3580. * Retrieve our RX interrupt holdoff timer values and counter
  3581. * threshold values from the SGE parameters.
  3582. */
  3583. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
  3584. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
  3585. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
  3586. s->timer_val[0] = core_ticks_to_us(adap,
  3587. TIMERVALUE0_G(timer_value_0_and_1));
  3588. s->timer_val[1] = core_ticks_to_us(adap,
  3589. TIMERVALUE1_G(timer_value_0_and_1));
  3590. s->timer_val[2] = core_ticks_to_us(adap,
  3591. TIMERVALUE2_G(timer_value_2_and_3));
  3592. s->timer_val[3] = core_ticks_to_us(adap,
  3593. TIMERVALUE3_G(timer_value_2_and_3));
  3594. s->timer_val[4] = core_ticks_to_us(adap,
  3595. TIMERVALUE4_G(timer_value_4_and_5));
  3596. s->timer_val[5] = core_ticks_to_us(adap,
  3597. TIMERVALUE5_G(timer_value_4_and_5));
  3598. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
  3599. s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
  3600. s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
  3601. s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
  3602. s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
  3603. return 0;
  3604. }
  3605. /**
  3606. * t4_sge_init - initialize SGE
  3607. * @adap: the adapter
  3608. *
  3609. * Perform low-level SGE code initialization needed every time after a
  3610. * chip reset.
  3611. */
  3612. int t4_sge_init(struct adapter *adap)
  3613. {
  3614. struct sge *s = &adap->sge;
  3615. u32 sge_control, sge_conm_ctrl;
  3616. int ret, egress_threshold;
  3617. /*
  3618. * Ingress Padding Boundary and Egress Status Page Size are set up by
  3619. * t4_fixup_host_params().
  3620. */
  3621. sge_control = t4_read_reg(adap, SGE_CONTROL_A);
  3622. s->pktshift = PKTSHIFT_G(sge_control);
  3623. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
  3624. s->fl_align = t4_fl_pkt_align(adap);
  3625. ret = t4_sge_init_soft(adap);
  3626. if (ret < 0)
  3627. return ret;
  3628. /*
  3629. * A FL with <= fl_starve_thres buffers is starving and a periodic
  3630. * timer will attempt to refill it. This needs to be larger than the
  3631. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  3632. * stuck waiting for new packets while the SGE is waiting for us to
  3633. * give it more Free List entries. (Note that the SGE's Egress
  3634. * Congestion Threshold is in units of 2 Free List pointers.) For T4,
  3635. * there was only a single field to control this. For T5 there's the
  3636. * original field which now only applies to Unpacked Mode Free List
  3637. * buffers and a new field which only applies to Packed Mode Free List
  3638. * buffers.
  3639. */
  3640. sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
  3641. switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
  3642. case CHELSIO_T4:
  3643. egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
  3644. break;
  3645. case CHELSIO_T5:
  3646. egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  3647. break;
  3648. case CHELSIO_T6:
  3649. egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  3650. break;
  3651. default:
  3652. dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
  3653. CHELSIO_CHIP_VERSION(adap->params.chip));
  3654. return -EINVAL;
  3655. }
  3656. s->fl_starve_thres = 2*egress_threshold + 1;
  3657. t4_idma_monitor_init(adap, &s->idma_monitor);
  3658. /* Set up timers used for recuring callbacks to process RX and TX
  3659. * administrative tasks.
  3660. */
  3661. timer_setup(&s->rx_timer, sge_rx_timer_cb, 0);
  3662. timer_setup(&s->tx_timer, sge_tx_timer_cb, 0);
  3663. spin_lock_init(&s->intrq_lock);
  3664. return 0;
  3665. }