sge.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762
  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. * t4_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. netdev_tx_t t4_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. /**
  1374. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1375. * @q: the SGE control Tx queue
  1376. *
  1377. * This is a variant of cxgb4_reclaim_completed_tx() that is used
  1378. * for Tx queues that send only immediate data (presently just
  1379. * the control queues) and thus do not have any sk_buffs to release.
  1380. */
  1381. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1382. {
  1383. int hw_cidx = ntohs(READ_ONCE(q->stat->cidx));
  1384. int reclaim = hw_cidx - q->cidx;
  1385. if (reclaim < 0)
  1386. reclaim += q->size;
  1387. q->in_use -= reclaim;
  1388. q->cidx = hw_cidx;
  1389. }
  1390. /**
  1391. * is_imm - check whether a packet can be sent as immediate data
  1392. * @skb: the packet
  1393. *
  1394. * Returns true if a packet can be sent as a WR with immediate data.
  1395. */
  1396. static inline int is_imm(const struct sk_buff *skb)
  1397. {
  1398. return skb->len <= MAX_CTRL_WR_LEN;
  1399. }
  1400. /**
  1401. * ctrlq_check_stop - check if a control queue is full and should stop
  1402. * @q: the queue
  1403. * @wr: most recent WR written to the queue
  1404. *
  1405. * Check if a control queue has become full and should be stopped.
  1406. * We clean up control queue descriptors very lazily, only when we are out.
  1407. * If the queue is still full after reclaiming any completed descriptors
  1408. * we suspend it and have the last WR wake it up.
  1409. */
  1410. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  1411. {
  1412. reclaim_completed_tx_imm(&q->q);
  1413. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1414. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1415. q->q.stops++;
  1416. q->full = 1;
  1417. }
  1418. }
  1419. /**
  1420. * ctrl_xmit - send a packet through an SGE control Tx queue
  1421. * @q: the control queue
  1422. * @skb: the packet
  1423. *
  1424. * Send a packet through an SGE control Tx queue. Packets sent through
  1425. * a control queue must fit entirely as immediate data.
  1426. */
  1427. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  1428. {
  1429. unsigned int ndesc;
  1430. struct fw_wr_hdr *wr;
  1431. if (unlikely(!is_imm(skb))) {
  1432. WARN_ON(1);
  1433. dev_kfree_skb(skb);
  1434. return NET_XMIT_DROP;
  1435. }
  1436. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  1437. spin_lock(&q->sendq.lock);
  1438. if (unlikely(q->full)) {
  1439. skb->priority = ndesc; /* save for restart */
  1440. __skb_queue_tail(&q->sendq, skb);
  1441. spin_unlock(&q->sendq.lock);
  1442. return NET_XMIT_CN;
  1443. }
  1444. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1445. cxgb4_inline_tx_skb(skb, &q->q, wr);
  1446. txq_advance(&q->q, ndesc);
  1447. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  1448. ctrlq_check_stop(q, wr);
  1449. cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
  1450. spin_unlock(&q->sendq.lock);
  1451. kfree_skb(skb);
  1452. return NET_XMIT_SUCCESS;
  1453. }
  1454. /**
  1455. * restart_ctrlq - restart a suspended control queue
  1456. * @data: the control queue to restart
  1457. *
  1458. * Resumes transmission on a suspended Tx control queue.
  1459. */
  1460. static void restart_ctrlq(unsigned long data)
  1461. {
  1462. struct sk_buff *skb;
  1463. unsigned int written = 0;
  1464. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  1465. spin_lock(&q->sendq.lock);
  1466. reclaim_completed_tx_imm(&q->q);
  1467. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  1468. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  1469. struct fw_wr_hdr *wr;
  1470. unsigned int ndesc = skb->priority; /* previously saved */
  1471. written += ndesc;
  1472. /* Write descriptors and free skbs outside the lock to limit
  1473. * wait times. q->full is still set so new skbs will be queued.
  1474. */
  1475. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1476. txq_advance(&q->q, ndesc);
  1477. spin_unlock(&q->sendq.lock);
  1478. cxgb4_inline_tx_skb(skb, &q->q, wr);
  1479. kfree_skb(skb);
  1480. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1481. unsigned long old = q->q.stops;
  1482. ctrlq_check_stop(q, wr);
  1483. if (q->q.stops != old) { /* suspended anew */
  1484. spin_lock(&q->sendq.lock);
  1485. goto ringdb;
  1486. }
  1487. }
  1488. if (written > 16) {
  1489. cxgb4_ring_tx_db(q->adap, &q->q, written);
  1490. written = 0;
  1491. }
  1492. spin_lock(&q->sendq.lock);
  1493. }
  1494. q->full = 0;
  1495. ringdb:
  1496. if (written)
  1497. cxgb4_ring_tx_db(q->adap, &q->q, written);
  1498. spin_unlock(&q->sendq.lock);
  1499. }
  1500. /**
  1501. * t4_mgmt_tx - send a management message
  1502. * @adap: the adapter
  1503. * @skb: the packet containing the management message
  1504. *
  1505. * Send a management message through control queue 0.
  1506. */
  1507. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1508. {
  1509. int ret;
  1510. local_bh_disable();
  1511. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1512. local_bh_enable();
  1513. return ret;
  1514. }
  1515. /**
  1516. * is_ofld_imm - check whether a packet can be sent as immediate data
  1517. * @skb: the packet
  1518. *
  1519. * Returns true if a packet can be sent as an offload WR with immediate
  1520. * data. We currently use the same limit as for Ethernet packets.
  1521. */
  1522. static inline int is_ofld_imm(const struct sk_buff *skb)
  1523. {
  1524. struct work_request_hdr *req = (struct work_request_hdr *)skb->data;
  1525. unsigned long opcode = FW_WR_OP_G(ntohl(req->wr_hi));
  1526. if (opcode == FW_CRYPTO_LOOKASIDE_WR)
  1527. return skb->len <= SGE_MAX_WR_LEN;
  1528. else
  1529. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1530. }
  1531. /**
  1532. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1533. * @skb: the packet
  1534. *
  1535. * Returns the number of flits needed for the given offload packet.
  1536. * These packets are already fully constructed and no additional headers
  1537. * will be added.
  1538. */
  1539. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1540. {
  1541. unsigned int flits, cnt;
  1542. if (is_ofld_imm(skb))
  1543. return DIV_ROUND_UP(skb->len, 8);
  1544. flits = skb_transport_offset(skb) / 8U; /* headers */
  1545. cnt = skb_shinfo(skb)->nr_frags;
  1546. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1547. cnt++;
  1548. return flits + sgl_len(cnt);
  1549. }
  1550. /**
  1551. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1552. * @adap: the adapter
  1553. * @q: the queue to stop
  1554. *
  1555. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1556. * inability to map packets. A periodic timer attempts to restart
  1557. * queues so marked.
  1558. */
  1559. static void txq_stop_maperr(struct sge_uld_txq *q)
  1560. {
  1561. q->mapping_err++;
  1562. q->q.stops++;
  1563. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1564. q->adap->sge.txq_maperr);
  1565. }
  1566. /**
  1567. * ofldtxq_stop - stop an offload Tx queue that has become full
  1568. * @q: the queue to stop
  1569. * @wr: the Work Request causing the queue to become full
  1570. *
  1571. * Stops an offload Tx queue that has become full and modifies the packet
  1572. * being written to request a wakeup.
  1573. */
  1574. static void ofldtxq_stop(struct sge_uld_txq *q, struct fw_wr_hdr *wr)
  1575. {
  1576. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1577. q->q.stops++;
  1578. q->full = 1;
  1579. }
  1580. /**
  1581. * service_ofldq - service/restart a suspended offload queue
  1582. * @q: the offload queue
  1583. *
  1584. * Services an offload Tx queue by moving packets from its Pending Send
  1585. * Queue to the Hardware TX ring. The function starts and ends with the
  1586. * Send Queue locked, but drops the lock while putting the skb at the
  1587. * head of the Send Queue onto the Hardware TX Ring. Dropping the lock
  1588. * allows more skbs to be added to the Send Queue by other threads.
  1589. * The packet being processed at the head of the Pending Send Queue is
  1590. * left on the queue in case we experience DMA Mapping errors, etc.
  1591. * and need to give up and restart later.
  1592. *
  1593. * service_ofldq() can be thought of as a task which opportunistically
  1594. * uses other threads execution contexts. We use the Offload Queue
  1595. * boolean "service_ofldq_running" to make sure that only one instance
  1596. * is ever running at a time ...
  1597. */
  1598. static void service_ofldq(struct sge_uld_txq *q)
  1599. {
  1600. u64 *pos, *before, *end;
  1601. int credits;
  1602. struct sk_buff *skb;
  1603. struct sge_txq *txq;
  1604. unsigned int left;
  1605. unsigned int written = 0;
  1606. unsigned int flits, ndesc;
  1607. /* If another thread is currently in service_ofldq() processing the
  1608. * Pending Send Queue then there's nothing to do. Otherwise, flag
  1609. * that we're doing the work and continue. Examining/modifying
  1610. * the Offload Queue boolean "service_ofldq_running" must be done
  1611. * while holding the Pending Send Queue Lock.
  1612. */
  1613. if (q->service_ofldq_running)
  1614. return;
  1615. q->service_ofldq_running = true;
  1616. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1617. /* We drop the lock while we're working with the skb at the
  1618. * head of the Pending Send Queue. This allows more skbs to
  1619. * be added to the Pending Send Queue while we're working on
  1620. * this one. We don't need to lock to guard the TX Ring
  1621. * updates because only one thread of execution is ever
  1622. * allowed into service_ofldq() at a time.
  1623. */
  1624. spin_unlock(&q->sendq.lock);
  1625. cxgb4_reclaim_completed_tx(q->adap, &q->q, false);
  1626. flits = skb->priority; /* previously saved */
  1627. ndesc = flits_to_desc(flits);
  1628. credits = txq_avail(&q->q) - ndesc;
  1629. BUG_ON(credits < 0);
  1630. if (unlikely(credits < TXQ_STOP_THRES))
  1631. ofldtxq_stop(q, (struct fw_wr_hdr *)skb->data);
  1632. pos = (u64 *)&q->q.desc[q->q.pidx];
  1633. if (is_ofld_imm(skb))
  1634. cxgb4_inline_tx_skb(skb, &q->q, pos);
  1635. else if (cxgb4_map_skb(q->adap->pdev_dev, skb,
  1636. (dma_addr_t *)skb->head)) {
  1637. txq_stop_maperr(q);
  1638. spin_lock(&q->sendq.lock);
  1639. break;
  1640. } else {
  1641. int last_desc, hdr_len = skb_transport_offset(skb);
  1642. /* The WR headers may not fit within one descriptor.
  1643. * So we need to deal with wrap-around here.
  1644. */
  1645. before = (u64 *)pos;
  1646. end = (u64 *)pos + flits;
  1647. txq = &q->q;
  1648. pos = (void *)inline_tx_skb_header(skb, &q->q,
  1649. (void *)pos,
  1650. hdr_len);
  1651. if (before > (u64 *)pos) {
  1652. left = (u8 *)end - (u8 *)txq->stat;
  1653. end = (void *)txq->desc + left;
  1654. }
  1655. /* If current position is already at the end of the
  1656. * ofld queue, reset the current to point to
  1657. * start of the queue and update the end ptr as well.
  1658. */
  1659. if (pos == (u64 *)txq->stat) {
  1660. left = (u8 *)end - (u8 *)txq->stat;
  1661. end = (void *)txq->desc + left;
  1662. pos = (void *)txq->desc;
  1663. }
  1664. cxgb4_write_sgl(skb, &q->q, (void *)pos,
  1665. end, hdr_len,
  1666. (dma_addr_t *)skb->head);
  1667. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1668. skb->dev = q->adap->port[0];
  1669. skb->destructor = deferred_unmap_destructor;
  1670. #endif
  1671. last_desc = q->q.pidx + ndesc - 1;
  1672. if (last_desc >= q->q.size)
  1673. last_desc -= q->q.size;
  1674. q->q.sdesc[last_desc].skb = skb;
  1675. }
  1676. txq_advance(&q->q, ndesc);
  1677. written += ndesc;
  1678. if (unlikely(written > 32)) {
  1679. cxgb4_ring_tx_db(q->adap, &q->q, written);
  1680. written = 0;
  1681. }
  1682. /* Reacquire the Pending Send Queue Lock so we can unlink the
  1683. * skb we've just successfully transferred to the TX Ring and
  1684. * loop for the next skb which may be at the head of the
  1685. * Pending Send Queue.
  1686. */
  1687. spin_lock(&q->sendq.lock);
  1688. __skb_unlink(skb, &q->sendq);
  1689. if (is_ofld_imm(skb))
  1690. kfree_skb(skb);
  1691. }
  1692. if (likely(written))
  1693. cxgb4_ring_tx_db(q->adap, &q->q, written);
  1694. /*Indicate that no thread is processing the Pending Send Queue
  1695. * currently.
  1696. */
  1697. q->service_ofldq_running = false;
  1698. }
  1699. /**
  1700. * ofld_xmit - send a packet through an offload queue
  1701. * @q: the Tx offload queue
  1702. * @skb: the packet
  1703. *
  1704. * Send an offload packet through an SGE offload queue.
  1705. */
  1706. static int ofld_xmit(struct sge_uld_txq *q, struct sk_buff *skb)
  1707. {
  1708. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1709. spin_lock(&q->sendq.lock);
  1710. /* Queue the new skb onto the Offload Queue's Pending Send Queue. If
  1711. * that results in this new skb being the only one on the queue, start
  1712. * servicing it. If there are other skbs already on the list, then
  1713. * either the queue is currently being processed or it's been stopped
  1714. * for some reason and it'll be restarted at a later time. Restart
  1715. * paths are triggered by events like experiencing a DMA Mapping Error
  1716. * or filling the Hardware TX Ring.
  1717. */
  1718. __skb_queue_tail(&q->sendq, skb);
  1719. if (q->sendq.qlen == 1)
  1720. service_ofldq(q);
  1721. spin_unlock(&q->sendq.lock);
  1722. return NET_XMIT_SUCCESS;
  1723. }
  1724. /**
  1725. * restart_ofldq - restart a suspended offload queue
  1726. * @data: the offload queue to restart
  1727. *
  1728. * Resumes transmission on a suspended Tx offload queue.
  1729. */
  1730. static void restart_ofldq(unsigned long data)
  1731. {
  1732. struct sge_uld_txq *q = (struct sge_uld_txq *)data;
  1733. spin_lock(&q->sendq.lock);
  1734. q->full = 0; /* the queue actually is completely empty now */
  1735. service_ofldq(q);
  1736. spin_unlock(&q->sendq.lock);
  1737. }
  1738. /**
  1739. * skb_txq - return the Tx queue an offload packet should use
  1740. * @skb: the packet
  1741. *
  1742. * Returns the Tx queue an offload packet should use as indicated by bits
  1743. * 1-15 in the packet's queue_mapping.
  1744. */
  1745. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1746. {
  1747. return skb->queue_mapping >> 1;
  1748. }
  1749. /**
  1750. * is_ctrl_pkt - return whether an offload packet is a control packet
  1751. * @skb: the packet
  1752. *
  1753. * Returns whether an offload packet should use an OFLD or a CTRL
  1754. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1755. */
  1756. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1757. {
  1758. return skb->queue_mapping & 1;
  1759. }
  1760. static inline int uld_send(struct adapter *adap, struct sk_buff *skb,
  1761. unsigned int tx_uld_type)
  1762. {
  1763. struct sge_uld_txq_info *txq_info;
  1764. struct sge_uld_txq *txq;
  1765. unsigned int idx = skb_txq(skb);
  1766. if (unlikely(is_ctrl_pkt(skb))) {
  1767. /* Single ctrl queue is a requirement for LE workaround path */
  1768. if (adap->tids.nsftids)
  1769. idx = 0;
  1770. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1771. }
  1772. txq_info = adap->sge.uld_txq_info[tx_uld_type];
  1773. if (unlikely(!txq_info)) {
  1774. WARN_ON(true);
  1775. return NET_XMIT_DROP;
  1776. }
  1777. txq = &txq_info->uldtxq[idx];
  1778. return ofld_xmit(txq, skb);
  1779. }
  1780. /**
  1781. * t4_ofld_send - send an offload packet
  1782. * @adap: the adapter
  1783. * @skb: the packet
  1784. *
  1785. * Sends an offload packet. We use the packet queue_mapping to select the
  1786. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1787. * should be sent as regular or control, bits 1-15 select the queue.
  1788. */
  1789. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1790. {
  1791. int ret;
  1792. local_bh_disable();
  1793. ret = uld_send(adap, skb, CXGB4_TX_OFLD);
  1794. local_bh_enable();
  1795. return ret;
  1796. }
  1797. /**
  1798. * cxgb4_ofld_send - send an offload packet
  1799. * @dev: the net device
  1800. * @skb: the packet
  1801. *
  1802. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1803. * intended for ULDs.
  1804. */
  1805. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1806. {
  1807. return t4_ofld_send(netdev2adap(dev), skb);
  1808. }
  1809. EXPORT_SYMBOL(cxgb4_ofld_send);
  1810. static void *inline_tx_header(const void *src,
  1811. const struct sge_txq *q,
  1812. void *pos, int length)
  1813. {
  1814. int left = (void *)q->stat - pos;
  1815. u64 *p;
  1816. if (likely(length <= left)) {
  1817. memcpy(pos, src, length);
  1818. pos += length;
  1819. } else {
  1820. memcpy(pos, src, left);
  1821. memcpy(q->desc, src + left, length - left);
  1822. pos = (void *)q->desc + (length - left);
  1823. }
  1824. /* 0-pad to multiple of 16 */
  1825. p = PTR_ALIGN(pos, 8);
  1826. if ((uintptr_t)p & 8) {
  1827. *p = 0;
  1828. return p + 1;
  1829. }
  1830. return p;
  1831. }
  1832. /**
  1833. * ofld_xmit_direct - copy a WR into offload queue
  1834. * @q: the Tx offload queue
  1835. * @src: location of WR
  1836. * @len: WR length
  1837. *
  1838. * Copy an immediate WR into an uncontended SGE offload queue.
  1839. */
  1840. static int ofld_xmit_direct(struct sge_uld_txq *q, const void *src,
  1841. unsigned int len)
  1842. {
  1843. unsigned int ndesc;
  1844. int credits;
  1845. u64 *pos;
  1846. /* Use the lower limit as the cut-off */
  1847. if (len > MAX_IMM_OFLD_TX_DATA_WR_LEN) {
  1848. WARN_ON(1);
  1849. return NET_XMIT_DROP;
  1850. }
  1851. /* Don't return NET_XMIT_CN here as the current
  1852. * implementation doesn't queue the request
  1853. * using an skb when the following conditions not met
  1854. */
  1855. if (!spin_trylock(&q->sendq.lock))
  1856. return NET_XMIT_DROP;
  1857. if (q->full || !skb_queue_empty(&q->sendq) ||
  1858. q->service_ofldq_running) {
  1859. spin_unlock(&q->sendq.lock);
  1860. return NET_XMIT_DROP;
  1861. }
  1862. ndesc = flits_to_desc(DIV_ROUND_UP(len, 8));
  1863. credits = txq_avail(&q->q) - ndesc;
  1864. pos = (u64 *)&q->q.desc[q->q.pidx];
  1865. /* ofldtxq_stop modifies WR header in-situ */
  1866. inline_tx_header(src, &q->q, pos, len);
  1867. if (unlikely(credits < TXQ_STOP_THRES))
  1868. ofldtxq_stop(q, (struct fw_wr_hdr *)pos);
  1869. txq_advance(&q->q, ndesc);
  1870. cxgb4_ring_tx_db(q->adap, &q->q, ndesc);
  1871. spin_unlock(&q->sendq.lock);
  1872. return NET_XMIT_SUCCESS;
  1873. }
  1874. int cxgb4_immdata_send(struct net_device *dev, unsigned int idx,
  1875. const void *src, unsigned int len)
  1876. {
  1877. struct sge_uld_txq_info *txq_info;
  1878. struct sge_uld_txq *txq;
  1879. struct adapter *adap;
  1880. int ret;
  1881. adap = netdev2adap(dev);
  1882. local_bh_disable();
  1883. txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
  1884. if (unlikely(!txq_info)) {
  1885. WARN_ON(true);
  1886. local_bh_enable();
  1887. return NET_XMIT_DROP;
  1888. }
  1889. txq = &txq_info->uldtxq[idx];
  1890. ret = ofld_xmit_direct(txq, src, len);
  1891. local_bh_enable();
  1892. return net_xmit_eval(ret);
  1893. }
  1894. EXPORT_SYMBOL(cxgb4_immdata_send);
  1895. /**
  1896. * t4_crypto_send - send crypto packet
  1897. * @adap: the adapter
  1898. * @skb: the packet
  1899. *
  1900. * Sends crypto packet. We use the packet queue_mapping to select the
  1901. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1902. * should be sent as regular or control, bits 1-15 select the queue.
  1903. */
  1904. static int t4_crypto_send(struct adapter *adap, struct sk_buff *skb)
  1905. {
  1906. int ret;
  1907. local_bh_disable();
  1908. ret = uld_send(adap, skb, CXGB4_TX_CRYPTO);
  1909. local_bh_enable();
  1910. return ret;
  1911. }
  1912. /**
  1913. * cxgb4_crypto_send - send crypto packet
  1914. * @dev: the net device
  1915. * @skb: the packet
  1916. *
  1917. * Sends crypto packet. This is an exported version of @t4_crypto_send,
  1918. * intended for ULDs.
  1919. */
  1920. int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb)
  1921. {
  1922. return t4_crypto_send(netdev2adap(dev), skb);
  1923. }
  1924. EXPORT_SYMBOL(cxgb4_crypto_send);
  1925. static inline void copy_frags(struct sk_buff *skb,
  1926. const struct pkt_gl *gl, unsigned int offset)
  1927. {
  1928. int i;
  1929. /* usually there's just one frag */
  1930. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  1931. gl->frags[0].offset + offset,
  1932. gl->frags[0].size - offset);
  1933. skb_shinfo(skb)->nr_frags = gl->nfrags;
  1934. for (i = 1; i < gl->nfrags; i++)
  1935. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  1936. gl->frags[i].offset,
  1937. gl->frags[i].size);
  1938. /* get a reference to the last page, we don't own it */
  1939. get_page(gl->frags[gl->nfrags - 1].page);
  1940. }
  1941. /**
  1942. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1943. * @gl: the gather list
  1944. * @skb_len: size of sk_buff main body if it carries fragments
  1945. * @pull_len: amount of data to move to the sk_buff's main body
  1946. *
  1947. * Builds an sk_buff from the given packet gather list. Returns the
  1948. * sk_buff or %NULL if sk_buff allocation failed.
  1949. */
  1950. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1951. unsigned int skb_len, unsigned int pull_len)
  1952. {
  1953. struct sk_buff *skb;
  1954. /*
  1955. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1956. * size, which is expected since buffers are at least PAGE_SIZEd.
  1957. * In this case packets up to RX_COPY_THRES have only one fragment.
  1958. */
  1959. if (gl->tot_len <= RX_COPY_THRES) {
  1960. skb = dev_alloc_skb(gl->tot_len);
  1961. if (unlikely(!skb))
  1962. goto out;
  1963. __skb_put(skb, gl->tot_len);
  1964. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1965. } else {
  1966. skb = dev_alloc_skb(skb_len);
  1967. if (unlikely(!skb))
  1968. goto out;
  1969. __skb_put(skb, pull_len);
  1970. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1971. copy_frags(skb, gl, pull_len);
  1972. skb->len = gl->tot_len;
  1973. skb->data_len = skb->len - pull_len;
  1974. skb->truesize += skb->data_len;
  1975. }
  1976. out: return skb;
  1977. }
  1978. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1979. /**
  1980. * t4_pktgl_free - free a packet gather list
  1981. * @gl: the gather list
  1982. *
  1983. * Releases the pages of a packet gather list. We do not own the last
  1984. * page on the list and do not free it.
  1985. */
  1986. static void t4_pktgl_free(const struct pkt_gl *gl)
  1987. {
  1988. int n;
  1989. const struct page_frag *p;
  1990. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1991. put_page(p->page);
  1992. }
  1993. /*
  1994. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1995. * be delivered to anyone and send it to the stack for capture.
  1996. */
  1997. static noinline int handle_trace_pkt(struct adapter *adap,
  1998. const struct pkt_gl *gl)
  1999. {
  2000. struct sk_buff *skb;
  2001. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  2002. if (unlikely(!skb)) {
  2003. t4_pktgl_free(gl);
  2004. return 0;
  2005. }
  2006. if (is_t4(adap->params.chip))
  2007. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  2008. else
  2009. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  2010. skb_reset_mac_header(skb);
  2011. skb->protocol = htons(0xffff);
  2012. skb->dev = adap->port[0];
  2013. netif_receive_skb(skb);
  2014. return 0;
  2015. }
  2016. /**
  2017. * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
  2018. * @adap: the adapter
  2019. * @hwtstamps: time stamp structure to update
  2020. * @sgetstamp: 60bit iqe timestamp
  2021. *
  2022. * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
  2023. * which is in Core Clock ticks into ktime_t and assign it
  2024. **/
  2025. static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
  2026. struct skb_shared_hwtstamps *hwtstamps,
  2027. u64 sgetstamp)
  2028. {
  2029. u64 ns;
  2030. u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
  2031. ns = div_u64(tmp, adap->params.vpd.cclk);
  2032. memset(hwtstamps, 0, sizeof(*hwtstamps));
  2033. hwtstamps->hwtstamp = ns_to_ktime(ns);
  2034. }
  2035. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  2036. const struct cpl_rx_pkt *pkt, unsigned long tnl_hdr_len)
  2037. {
  2038. struct adapter *adapter = rxq->rspq.adap;
  2039. struct sge *s = &adapter->sge;
  2040. struct port_info *pi;
  2041. int ret;
  2042. struct sk_buff *skb;
  2043. skb = napi_get_frags(&rxq->rspq.napi);
  2044. if (unlikely(!skb)) {
  2045. t4_pktgl_free(gl);
  2046. rxq->stats.rx_drops++;
  2047. return;
  2048. }
  2049. copy_frags(skb, gl, s->pktshift);
  2050. if (tnl_hdr_len)
  2051. skb->csum_level = 1;
  2052. skb->len = gl->tot_len - s->pktshift;
  2053. skb->data_len = skb->len;
  2054. skb->truesize += skb->data_len;
  2055. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2056. skb_record_rx_queue(skb, rxq->rspq.idx);
  2057. pi = netdev_priv(skb->dev);
  2058. if (pi->rxtstamp)
  2059. cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
  2060. gl->sgetstamp);
  2061. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  2062. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  2063. PKT_HASH_TYPE_L3);
  2064. if (unlikely(pkt->vlan_ex)) {
  2065. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  2066. rxq->stats.vlan_ex++;
  2067. }
  2068. ret = napi_gro_frags(&rxq->rspq.napi);
  2069. if (ret == GRO_HELD)
  2070. rxq->stats.lro_pkts++;
  2071. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  2072. rxq->stats.lro_merged++;
  2073. rxq->stats.pkts++;
  2074. rxq->stats.rx_cso++;
  2075. }
  2076. enum {
  2077. RX_NON_PTP_PKT = 0,
  2078. RX_PTP_PKT_SUC = 1,
  2079. RX_PTP_PKT_ERR = 2
  2080. };
  2081. /**
  2082. * t4_systim_to_hwstamp - read hardware time stamp
  2083. * @adap: the adapter
  2084. * @skb: the packet
  2085. *
  2086. * Read Time Stamp from MPS packet and insert in skb which
  2087. * is forwarded to PTP application
  2088. */
  2089. static noinline int t4_systim_to_hwstamp(struct adapter *adapter,
  2090. struct sk_buff *skb)
  2091. {
  2092. struct skb_shared_hwtstamps *hwtstamps;
  2093. struct cpl_rx_mps_pkt *cpl = NULL;
  2094. unsigned char *data;
  2095. int offset;
  2096. cpl = (struct cpl_rx_mps_pkt *)skb->data;
  2097. if (!(CPL_RX_MPS_PKT_TYPE_G(ntohl(cpl->op_to_r1_hi)) &
  2098. X_CPL_RX_MPS_PKT_TYPE_PTP))
  2099. return RX_PTP_PKT_ERR;
  2100. data = skb->data + sizeof(*cpl);
  2101. skb_pull(skb, 2 * sizeof(u64) + sizeof(struct cpl_rx_mps_pkt));
  2102. offset = ETH_HLEN + IPV4_HLEN(skb->data) + UDP_HLEN;
  2103. if (skb->len < offset + OFF_PTP_SEQUENCE_ID + sizeof(short))
  2104. return RX_PTP_PKT_ERR;
  2105. hwtstamps = skb_hwtstamps(skb);
  2106. memset(hwtstamps, 0, sizeof(*hwtstamps));
  2107. hwtstamps->hwtstamp = ns_to_ktime(be64_to_cpu(*((u64 *)data)));
  2108. return RX_PTP_PKT_SUC;
  2109. }
  2110. /**
  2111. * t4_rx_hststamp - Recv PTP Event Message
  2112. * @adap: the adapter
  2113. * @rsp: the response queue descriptor holding the RX_PKT message
  2114. * @skb: the packet
  2115. *
  2116. * PTP enabled and MPS packet, read HW timestamp
  2117. */
  2118. static int t4_rx_hststamp(struct adapter *adapter, const __be64 *rsp,
  2119. struct sge_eth_rxq *rxq, struct sk_buff *skb)
  2120. {
  2121. int ret;
  2122. if (unlikely((*(u8 *)rsp == CPL_RX_MPS_PKT) &&
  2123. !is_t4(adapter->params.chip))) {
  2124. ret = t4_systim_to_hwstamp(adapter, skb);
  2125. if (ret == RX_PTP_PKT_ERR) {
  2126. kfree_skb(skb);
  2127. rxq->stats.rx_drops++;
  2128. }
  2129. return ret;
  2130. }
  2131. return RX_NON_PTP_PKT;
  2132. }
  2133. /**
  2134. * t4_tx_hststamp - Loopback PTP Transmit Event Message
  2135. * @adap: the adapter
  2136. * @skb: the packet
  2137. * @dev: the ingress net device
  2138. *
  2139. * Read hardware timestamp for the loopback PTP Tx event message
  2140. */
  2141. static int t4_tx_hststamp(struct adapter *adapter, struct sk_buff *skb,
  2142. struct net_device *dev)
  2143. {
  2144. struct port_info *pi = netdev_priv(dev);
  2145. if (!is_t4(adapter->params.chip) && adapter->ptp_tx_skb) {
  2146. cxgb4_ptp_read_hwstamp(adapter, pi);
  2147. kfree_skb(skb);
  2148. return 0;
  2149. }
  2150. return 1;
  2151. }
  2152. /**
  2153. * t4_ethrx_handler - process an ingress ethernet packet
  2154. * @q: the response queue that received the packet
  2155. * @rsp: the response queue descriptor holding the RX_PKT message
  2156. * @si: the gather list of packet fragments
  2157. *
  2158. * Process an ingress ethernet packet and deliver it to the stack.
  2159. */
  2160. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  2161. const struct pkt_gl *si)
  2162. {
  2163. bool csum_ok;
  2164. struct sk_buff *skb;
  2165. const struct cpl_rx_pkt *pkt;
  2166. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  2167. struct adapter *adapter = q->adap;
  2168. struct sge *s = &q->adap->sge;
  2169. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  2170. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  2171. u16 err_vec, tnl_hdr_len = 0;
  2172. struct port_info *pi;
  2173. int ret = 0;
  2174. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  2175. return handle_trace_pkt(q->adap, si);
  2176. pkt = (const struct cpl_rx_pkt *)rsp;
  2177. /* Compressed error vector is enabled for T6 only */
  2178. if (q->adap->params.tp.rx_pkt_encap) {
  2179. err_vec = T6_COMPR_RXERR_VEC_G(be16_to_cpu(pkt->err_vec));
  2180. tnl_hdr_len = T6_RX_TNLHDR_LEN_G(ntohs(pkt->err_vec));
  2181. } else {
  2182. err_vec = be16_to_cpu(pkt->err_vec);
  2183. }
  2184. csum_ok = pkt->csum_calc && !err_vec &&
  2185. (q->netdev->features & NETIF_F_RXCSUM);
  2186. if (((pkt->l2info & htonl(RXF_TCP_F)) ||
  2187. tnl_hdr_len) &&
  2188. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  2189. do_gro(rxq, si, pkt, tnl_hdr_len);
  2190. return 0;
  2191. }
  2192. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  2193. if (unlikely(!skb)) {
  2194. t4_pktgl_free(si);
  2195. rxq->stats.rx_drops++;
  2196. return 0;
  2197. }
  2198. pi = netdev_priv(q->netdev);
  2199. /* Handle PTP Event Rx packet */
  2200. if (unlikely(pi->ptp_enable)) {
  2201. ret = t4_rx_hststamp(adapter, rsp, rxq, skb);
  2202. if (ret == RX_PTP_PKT_ERR)
  2203. return 0;
  2204. }
  2205. if (likely(!ret))
  2206. __skb_pull(skb, s->pktshift); /* remove ethernet header pad */
  2207. /* Handle the PTP Event Tx Loopback packet */
  2208. if (unlikely(pi->ptp_enable && !ret &&
  2209. (pkt->l2info & htonl(RXF_UDP_F)) &&
  2210. cxgb4_ptp_is_ptp_rx(skb))) {
  2211. if (!t4_tx_hststamp(adapter, skb, q->netdev))
  2212. return 0;
  2213. }
  2214. skb->protocol = eth_type_trans(skb, q->netdev);
  2215. skb_record_rx_queue(skb, q->idx);
  2216. if (skb->dev->features & NETIF_F_RXHASH)
  2217. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  2218. PKT_HASH_TYPE_L3);
  2219. rxq->stats.pkts++;
  2220. if (pi->rxtstamp)
  2221. cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
  2222. si->sgetstamp);
  2223. if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
  2224. if (!pkt->ip_frag) {
  2225. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2226. rxq->stats.rx_cso++;
  2227. } else if (pkt->l2info & htonl(RXF_IP_F)) {
  2228. __sum16 c = (__force __sum16)pkt->csum;
  2229. skb->csum = csum_unfold(c);
  2230. if (tnl_hdr_len) {
  2231. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2232. skb->csum_level = 1;
  2233. } else {
  2234. skb->ip_summed = CHECKSUM_COMPLETE;
  2235. }
  2236. rxq->stats.rx_cso++;
  2237. }
  2238. } else {
  2239. skb_checksum_none_assert(skb);
  2240. #ifdef CONFIG_CHELSIO_T4_FCOE
  2241. #define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
  2242. RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
  2243. if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
  2244. if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
  2245. (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
  2246. if (q->adap->params.tp.rx_pkt_encap)
  2247. csum_ok = err_vec &
  2248. T6_COMPR_RXERR_SUM_F;
  2249. else
  2250. csum_ok = err_vec & RXERR_CSUM_F;
  2251. if (!csum_ok)
  2252. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2253. }
  2254. }
  2255. #undef CPL_RX_PKT_FLAGS
  2256. #endif /* CONFIG_CHELSIO_T4_FCOE */
  2257. }
  2258. if (unlikely(pkt->vlan_ex)) {
  2259. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  2260. rxq->stats.vlan_ex++;
  2261. }
  2262. skb_mark_napi_id(skb, &q->napi);
  2263. netif_receive_skb(skb);
  2264. return 0;
  2265. }
  2266. /**
  2267. * restore_rx_bufs - put back a packet's Rx buffers
  2268. * @si: the packet gather list
  2269. * @q: the SGE free list
  2270. * @frags: number of FL buffers to restore
  2271. *
  2272. * Puts back on an FL the Rx buffers associated with @si. The buffers
  2273. * have already been unmapped and are left unmapped, we mark them so to
  2274. * prevent further unmapping attempts.
  2275. *
  2276. * This function undoes a series of @unmap_rx_buf calls when we find out
  2277. * that the current packet can't be processed right away afterall and we
  2278. * need to come back to it later. This is a very rare event and there's
  2279. * no effort to make this particularly efficient.
  2280. */
  2281. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  2282. int frags)
  2283. {
  2284. struct rx_sw_desc *d;
  2285. while (frags--) {
  2286. if (q->cidx == 0)
  2287. q->cidx = q->size - 1;
  2288. else
  2289. q->cidx--;
  2290. d = &q->sdesc[q->cidx];
  2291. d->page = si->frags[frags].page;
  2292. d->dma_addr |= RX_UNMAPPED_BUF;
  2293. q->avail++;
  2294. }
  2295. }
  2296. /**
  2297. * is_new_response - check if a response is newly written
  2298. * @r: the response descriptor
  2299. * @q: the response queue
  2300. *
  2301. * Returns true if a response descriptor contains a yet unprocessed
  2302. * response.
  2303. */
  2304. static inline bool is_new_response(const struct rsp_ctrl *r,
  2305. const struct sge_rspq *q)
  2306. {
  2307. return (r->type_gen >> RSPD_GEN_S) == q->gen;
  2308. }
  2309. /**
  2310. * rspq_next - advance to the next entry in a response queue
  2311. * @q: the queue
  2312. *
  2313. * Updates the state of a response queue to advance it to the next entry.
  2314. */
  2315. static inline void rspq_next(struct sge_rspq *q)
  2316. {
  2317. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  2318. if (unlikely(++q->cidx == q->size)) {
  2319. q->cidx = 0;
  2320. q->gen ^= 1;
  2321. q->cur_desc = q->desc;
  2322. }
  2323. }
  2324. /**
  2325. * process_responses - process responses from an SGE response queue
  2326. * @q: the ingress queue to process
  2327. * @budget: how many responses can be processed in this round
  2328. *
  2329. * Process responses from an SGE response queue up to the supplied budget.
  2330. * Responses include received packets as well as control messages from FW
  2331. * or HW.
  2332. *
  2333. * Additionally choose the interrupt holdoff time for the next interrupt
  2334. * on this queue. If the system is under memory shortage use a fairly
  2335. * long delay to help recovery.
  2336. */
  2337. static int process_responses(struct sge_rspq *q, int budget)
  2338. {
  2339. int ret, rsp_type;
  2340. int budget_left = budget;
  2341. const struct rsp_ctrl *rc;
  2342. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  2343. struct adapter *adapter = q->adap;
  2344. struct sge *s = &adapter->sge;
  2345. while (likely(budget_left)) {
  2346. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  2347. if (!is_new_response(rc, q)) {
  2348. if (q->flush_handler)
  2349. q->flush_handler(q);
  2350. break;
  2351. }
  2352. dma_rmb();
  2353. rsp_type = RSPD_TYPE_G(rc->type_gen);
  2354. if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
  2355. struct page_frag *fp;
  2356. struct pkt_gl si;
  2357. const struct rx_sw_desc *rsd;
  2358. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  2359. if (len & RSPD_NEWBUF_F) {
  2360. if (likely(q->offset > 0)) {
  2361. free_rx_bufs(q->adap, &rxq->fl, 1);
  2362. q->offset = 0;
  2363. }
  2364. len = RSPD_LEN_G(len);
  2365. }
  2366. si.tot_len = len;
  2367. /* gather packet fragments */
  2368. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  2369. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  2370. bufsz = get_buf_size(adapter, rsd);
  2371. fp->page = rsd->page;
  2372. fp->offset = q->offset;
  2373. fp->size = min(bufsz, len);
  2374. len -= fp->size;
  2375. if (!len)
  2376. break;
  2377. unmap_rx_buf(q->adap, &rxq->fl);
  2378. }
  2379. si.sgetstamp = SGE_TIMESTAMP_G(
  2380. be64_to_cpu(rc->last_flit));
  2381. /*
  2382. * Last buffer remains mapped so explicitly make it
  2383. * coherent for CPU access.
  2384. */
  2385. dma_sync_single_for_cpu(q->adap->pdev_dev,
  2386. get_buf_addr(rsd),
  2387. fp->size, DMA_FROM_DEVICE);
  2388. si.va = page_address(si.frags[0].page) +
  2389. si.frags[0].offset;
  2390. prefetch(si.va);
  2391. si.nfrags = frags + 1;
  2392. ret = q->handler(q, q->cur_desc, &si);
  2393. if (likely(ret == 0))
  2394. q->offset += ALIGN(fp->size, s->fl_align);
  2395. else
  2396. restore_rx_bufs(&si, &rxq->fl, frags);
  2397. } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
  2398. ret = q->handler(q, q->cur_desc, NULL);
  2399. } else {
  2400. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  2401. }
  2402. if (unlikely(ret)) {
  2403. /* couldn't process descriptor, back off for recovery */
  2404. q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
  2405. break;
  2406. }
  2407. rspq_next(q);
  2408. budget_left--;
  2409. }
  2410. if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
  2411. __refill_fl(q->adap, &rxq->fl);
  2412. return budget - budget_left;
  2413. }
  2414. /**
  2415. * napi_rx_handler - the NAPI handler for Rx processing
  2416. * @napi: the napi instance
  2417. * @budget: how many packets we can process in this round
  2418. *
  2419. * Handler for new data events when using NAPI. This does not need any
  2420. * locking or protection from interrupts as data interrupts are off at
  2421. * this point and other adapter interrupts do not interfere (the latter
  2422. * in not a concern at all with MSI-X as non-data interrupts then have
  2423. * a separate handler).
  2424. */
  2425. static int napi_rx_handler(struct napi_struct *napi, int budget)
  2426. {
  2427. unsigned int params;
  2428. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  2429. int work_done;
  2430. u32 val;
  2431. work_done = process_responses(q, budget);
  2432. if (likely(work_done < budget)) {
  2433. int timer_index;
  2434. napi_complete_done(napi, work_done);
  2435. timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
  2436. if (q->adaptive_rx) {
  2437. if (work_done > max(timer_pkt_quota[timer_index],
  2438. MIN_NAPI_WORK))
  2439. timer_index = (timer_index + 1);
  2440. else
  2441. timer_index = timer_index - 1;
  2442. timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
  2443. q->next_intr_params =
  2444. QINTR_TIMER_IDX_V(timer_index) |
  2445. QINTR_CNT_EN_V(0);
  2446. params = q->next_intr_params;
  2447. } else {
  2448. params = q->next_intr_params;
  2449. q->next_intr_params = q->intr_params;
  2450. }
  2451. } else
  2452. params = QINTR_TIMER_IDX_V(7);
  2453. val = CIDXINC_V(work_done) | SEINTARM_V(params);
  2454. /* If we don't have access to the new User GTS (T5+), use the old
  2455. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2456. */
  2457. if (unlikely(q->bar2_addr == NULL)) {
  2458. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
  2459. val | INGRESSQID_V((u32)q->cntxt_id));
  2460. } else {
  2461. writel(val | INGRESSQID_V(q->bar2_qid),
  2462. q->bar2_addr + SGE_UDB_GTS);
  2463. wmb();
  2464. }
  2465. return work_done;
  2466. }
  2467. /*
  2468. * The MSI-X interrupt handler for an SGE response queue.
  2469. */
  2470. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  2471. {
  2472. struct sge_rspq *q = cookie;
  2473. napi_schedule(&q->napi);
  2474. return IRQ_HANDLED;
  2475. }
  2476. /*
  2477. * Process the indirect interrupt entries in the interrupt queue and kick off
  2478. * NAPI for each queue that has generated an entry.
  2479. */
  2480. static unsigned int process_intrq(struct adapter *adap)
  2481. {
  2482. unsigned int credits;
  2483. const struct rsp_ctrl *rc;
  2484. struct sge_rspq *q = &adap->sge.intrq;
  2485. u32 val;
  2486. spin_lock(&adap->sge.intrq_lock);
  2487. for (credits = 0; ; credits++) {
  2488. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  2489. if (!is_new_response(rc, q))
  2490. break;
  2491. dma_rmb();
  2492. if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
  2493. unsigned int qid = ntohl(rc->pldbuflen_qid);
  2494. qid -= adap->sge.ingr_start;
  2495. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  2496. }
  2497. rspq_next(q);
  2498. }
  2499. val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
  2500. /* If we don't have access to the new User GTS (T5+), use the old
  2501. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2502. */
  2503. if (unlikely(q->bar2_addr == NULL)) {
  2504. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
  2505. val | INGRESSQID_V(q->cntxt_id));
  2506. } else {
  2507. writel(val | INGRESSQID_V(q->bar2_qid),
  2508. q->bar2_addr + SGE_UDB_GTS);
  2509. wmb();
  2510. }
  2511. spin_unlock(&adap->sge.intrq_lock);
  2512. return credits;
  2513. }
  2514. /*
  2515. * The MSI interrupt handler, which handles data events from SGE response queues
  2516. * as well as error and other async events as they all use the same MSI vector.
  2517. */
  2518. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  2519. {
  2520. struct adapter *adap = cookie;
  2521. if (adap->flags & MASTER_PF)
  2522. t4_slow_intr_handler(adap);
  2523. process_intrq(adap);
  2524. return IRQ_HANDLED;
  2525. }
  2526. /*
  2527. * Interrupt handler for legacy INTx interrupts.
  2528. * Handles data events from SGE response queues as well as error and other
  2529. * async events as they all use the same interrupt line.
  2530. */
  2531. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  2532. {
  2533. struct adapter *adap = cookie;
  2534. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
  2535. if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
  2536. process_intrq(adap))
  2537. return IRQ_HANDLED;
  2538. return IRQ_NONE; /* probably shared interrupt */
  2539. }
  2540. /**
  2541. * t4_intr_handler - select the top-level interrupt handler
  2542. * @adap: the adapter
  2543. *
  2544. * Selects the top-level interrupt handler based on the type of interrupts
  2545. * (MSI-X, MSI, or INTx).
  2546. */
  2547. irq_handler_t t4_intr_handler(struct adapter *adap)
  2548. {
  2549. if (adap->flags & USING_MSIX)
  2550. return t4_sge_intr_msix;
  2551. if (adap->flags & USING_MSI)
  2552. return t4_intr_msi;
  2553. return t4_intr_intx;
  2554. }
  2555. static void sge_rx_timer_cb(struct timer_list *t)
  2556. {
  2557. unsigned long m;
  2558. unsigned int i;
  2559. struct adapter *adap = from_timer(adap, t, sge.rx_timer);
  2560. struct sge *s = &adap->sge;
  2561. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2562. for (m = s->starving_fl[i]; m; m &= m - 1) {
  2563. struct sge_eth_rxq *rxq;
  2564. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  2565. struct sge_fl *fl = s->egr_map[id];
  2566. clear_bit(id, s->starving_fl);
  2567. smp_mb__after_atomic();
  2568. if (fl_starving(adap, fl)) {
  2569. rxq = container_of(fl, struct sge_eth_rxq, fl);
  2570. if (napi_reschedule(&rxq->rspq.napi))
  2571. fl->starving++;
  2572. else
  2573. set_bit(id, s->starving_fl);
  2574. }
  2575. }
  2576. /* The remainder of the SGE RX Timer Callback routine is dedicated to
  2577. * global Master PF activities like checking for chip ingress stalls,
  2578. * etc.
  2579. */
  2580. if (!(adap->flags & MASTER_PF))
  2581. goto done;
  2582. t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
  2583. done:
  2584. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  2585. }
  2586. static void sge_tx_timer_cb(struct timer_list *t)
  2587. {
  2588. unsigned long m;
  2589. unsigned int i, budget;
  2590. struct adapter *adap = from_timer(adap, t, sge.tx_timer);
  2591. struct sge *s = &adap->sge;
  2592. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2593. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  2594. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  2595. struct sge_uld_txq *txq = s->egr_map[id];
  2596. clear_bit(id, s->txq_maperr);
  2597. tasklet_schedule(&txq->qresume_tsk);
  2598. }
  2599. if (!is_t4(adap->params.chip)) {
  2600. struct sge_eth_txq *q = &s->ptptxq;
  2601. int avail;
  2602. spin_lock(&adap->ptp_lock);
  2603. avail = reclaimable(&q->q);
  2604. if (avail) {
  2605. free_tx_desc(adap, &q->q, avail, false);
  2606. q->q.in_use -= avail;
  2607. }
  2608. spin_unlock(&adap->ptp_lock);
  2609. }
  2610. budget = MAX_TIMER_TX_RECLAIM;
  2611. i = s->ethtxq_rover;
  2612. do {
  2613. struct sge_eth_txq *q = &s->ethtxq[i];
  2614. if (q->q.in_use &&
  2615. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  2616. __netif_tx_trylock(q->txq)) {
  2617. int avail = reclaimable(&q->q);
  2618. if (avail) {
  2619. if (avail > budget)
  2620. avail = budget;
  2621. free_tx_desc(adap, &q->q, avail, true);
  2622. q->q.in_use -= avail;
  2623. budget -= avail;
  2624. }
  2625. __netif_tx_unlock(q->txq);
  2626. }
  2627. if (++i >= s->ethqsets)
  2628. i = 0;
  2629. } while (budget && i != s->ethtxq_rover);
  2630. s->ethtxq_rover = i;
  2631. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  2632. }
  2633. /**
  2634. * bar2_address - return the BAR2 address for an SGE Queue's Registers
  2635. * @adapter: the adapter
  2636. * @qid: the SGE Queue ID
  2637. * @qtype: the SGE Queue Type (Egress or Ingress)
  2638. * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
  2639. *
  2640. * Returns the BAR2 address for the SGE Queue Registers associated with
  2641. * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
  2642. * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
  2643. * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
  2644. * Registers are supported (e.g. the Write Combining Doorbell Buffer).
  2645. */
  2646. static void __iomem *bar2_address(struct adapter *adapter,
  2647. unsigned int qid,
  2648. enum t4_bar2_qtype qtype,
  2649. unsigned int *pbar2_qid)
  2650. {
  2651. u64 bar2_qoffset;
  2652. int ret;
  2653. ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
  2654. &bar2_qoffset, pbar2_qid);
  2655. if (ret)
  2656. return NULL;
  2657. return adapter->bar2 + bar2_qoffset;
  2658. }
  2659. /* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
  2660. * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
  2661. */
  2662. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  2663. struct net_device *dev, int intr_idx,
  2664. struct sge_fl *fl, rspq_handler_t hnd,
  2665. rspq_flush_handler_t flush_hnd, int cong)
  2666. {
  2667. int ret, flsz = 0;
  2668. struct fw_iq_cmd c;
  2669. struct sge *s = &adap->sge;
  2670. struct port_info *pi = netdev_priv(dev);
  2671. int relaxed = !(adap->flags & ROOT_NO_RELAXED_ORDERING);
  2672. /* Size needs to be multiple of 16, including status entry. */
  2673. iq->size = roundup(iq->size, 16);
  2674. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  2675. &iq->phys_addr, NULL, 0,
  2676. dev_to_node(adap->pdev_dev));
  2677. if (!iq->desc)
  2678. return -ENOMEM;
  2679. memset(&c, 0, sizeof(c));
  2680. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
  2681. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2682. FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
  2683. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
  2684. FW_LEN16(c));
  2685. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
  2686. FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
  2687. FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
  2688. FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
  2689. FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
  2690. -intr_idx - 1));
  2691. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
  2692. FW_IQ_CMD_IQGTSMODE_F |
  2693. FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
  2694. FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
  2695. c.iqsize = htons(iq->size);
  2696. c.iqaddr = cpu_to_be64(iq->phys_addr);
  2697. if (cong >= 0)
  2698. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F);
  2699. if (fl) {
  2700. enum chip_type chip = CHELSIO_CHIP_VERSION(adap->params.chip);
  2701. /* Allocate the ring for the hardware free list (with space
  2702. * for its status page) along with the associated software
  2703. * descriptor ring. The free list size needs to be a multiple
  2704. * of the Egress Queue Unit and at least 2 Egress Units larger
  2705. * than the SGE's Egress Congrestion Threshold
  2706. * (fl_starve_thres - 1).
  2707. */
  2708. if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
  2709. fl->size = s->fl_starve_thres - 1 + 2 * 8;
  2710. fl->size = roundup(fl->size, 8);
  2711. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  2712. sizeof(struct rx_sw_desc), &fl->addr,
  2713. &fl->sdesc, s->stat_len,
  2714. dev_to_node(adap->pdev_dev));
  2715. if (!fl->desc)
  2716. goto fl_nomem;
  2717. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  2718. c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
  2719. FW_IQ_CMD_FL0FETCHRO_V(relaxed) |
  2720. FW_IQ_CMD_FL0DATARO_V(relaxed) |
  2721. FW_IQ_CMD_FL0PADEN_F);
  2722. if (cong >= 0)
  2723. c.iqns_to_fl0congen |=
  2724. htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
  2725. FW_IQ_CMD_FL0CONGCIF_F |
  2726. FW_IQ_CMD_FL0CONGEN_F);
  2727. /* In T6, for egress queue type FL there is internal overhead
  2728. * of 16B for header going into FLM module. Hence the maximum
  2729. * allowed burst size is 448 bytes. For T4/T5, the hardware
  2730. * doesn't coalesce fetch requests if more than 64 bytes of
  2731. * Free List pointers are provided, so we use a 128-byte Fetch
  2732. * Burst Minimum there (T6 implements coalescing so we can use
  2733. * the smaller 64-byte value there).
  2734. */
  2735. c.fl0dcaen_to_fl0cidxfthresh =
  2736. htons(FW_IQ_CMD_FL0FBMIN_V(chip <= CHELSIO_T5 ?
  2737. FETCHBURSTMIN_128B_X :
  2738. FETCHBURSTMIN_64B_X) |
  2739. FW_IQ_CMD_FL0FBMAX_V((chip <= CHELSIO_T5) ?
  2740. FETCHBURSTMAX_512B_X :
  2741. FETCHBURSTMAX_256B_X));
  2742. c.fl0size = htons(flsz);
  2743. c.fl0addr = cpu_to_be64(fl->addr);
  2744. }
  2745. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2746. if (ret)
  2747. goto err;
  2748. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  2749. iq->cur_desc = iq->desc;
  2750. iq->cidx = 0;
  2751. iq->gen = 1;
  2752. iq->next_intr_params = iq->intr_params;
  2753. iq->cntxt_id = ntohs(c.iqid);
  2754. iq->abs_id = ntohs(c.physiqid);
  2755. iq->bar2_addr = bar2_address(adap,
  2756. iq->cntxt_id,
  2757. T4_BAR2_QTYPE_INGRESS,
  2758. &iq->bar2_qid);
  2759. iq->size--; /* subtract status entry */
  2760. iq->netdev = dev;
  2761. iq->handler = hnd;
  2762. iq->flush_handler = flush_hnd;
  2763. memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
  2764. skb_queue_head_init(&iq->lro_mgr.lroq);
  2765. /* set offset to -1 to distinguish ingress queues without FL */
  2766. iq->offset = fl ? 0 : -1;
  2767. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  2768. if (fl) {
  2769. fl->cntxt_id = ntohs(c.fl0id);
  2770. fl->avail = fl->pend_cred = 0;
  2771. fl->pidx = fl->cidx = 0;
  2772. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  2773. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  2774. /* Note, we must initialize the BAR2 Free List User Doorbell
  2775. * information before refilling the Free List!
  2776. */
  2777. fl->bar2_addr = bar2_address(adap,
  2778. fl->cntxt_id,
  2779. T4_BAR2_QTYPE_EGRESS,
  2780. &fl->bar2_qid);
  2781. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  2782. }
  2783. /* For T5 and later we attempt to set up the Congestion Manager values
  2784. * of the new RX Ethernet Queue. This should really be handled by
  2785. * firmware because it's more complex than any host driver wants to
  2786. * get involved with and it's different per chip and this is almost
  2787. * certainly wrong. Firmware would be wrong as well, but it would be
  2788. * a lot easier to fix in one place ... For now we do something very
  2789. * simple (and hopefully less wrong).
  2790. */
  2791. if (!is_t4(adap->params.chip) && cong >= 0) {
  2792. u32 param, val, ch_map = 0;
  2793. int i;
  2794. u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
  2795. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  2796. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
  2797. FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
  2798. if (cong == 0) {
  2799. val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
  2800. } else {
  2801. val =
  2802. CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
  2803. for (i = 0; i < 4; i++) {
  2804. if (cong & (1 << i))
  2805. ch_map |= 1 << (i << cng_ch_bits_log);
  2806. }
  2807. val |= CONMCTXT_CNGCHMAP_V(ch_map);
  2808. }
  2809. ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
  2810. &param, &val);
  2811. if (ret)
  2812. dev_warn(adap->pdev_dev, "Failed to set Congestion"
  2813. " Manager Context for Ingress Queue %d: %d\n",
  2814. iq->cntxt_id, -ret);
  2815. }
  2816. return 0;
  2817. fl_nomem:
  2818. ret = -ENOMEM;
  2819. err:
  2820. if (iq->desc) {
  2821. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  2822. iq->desc, iq->phys_addr);
  2823. iq->desc = NULL;
  2824. }
  2825. if (fl && fl->desc) {
  2826. kfree(fl->sdesc);
  2827. fl->sdesc = NULL;
  2828. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  2829. fl->desc, fl->addr);
  2830. fl->desc = NULL;
  2831. }
  2832. return ret;
  2833. }
  2834. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  2835. {
  2836. q->cntxt_id = id;
  2837. q->bar2_addr = bar2_address(adap,
  2838. q->cntxt_id,
  2839. T4_BAR2_QTYPE_EGRESS,
  2840. &q->bar2_qid);
  2841. q->in_use = 0;
  2842. q->cidx = q->pidx = 0;
  2843. q->stops = q->restarts = 0;
  2844. q->stat = (void *)&q->desc[q->size];
  2845. spin_lock_init(&q->db_lock);
  2846. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  2847. }
  2848. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  2849. struct net_device *dev, struct netdev_queue *netdevq,
  2850. unsigned int iqid)
  2851. {
  2852. int ret, nentries;
  2853. struct fw_eq_eth_cmd c;
  2854. struct sge *s = &adap->sge;
  2855. struct port_info *pi = netdev_priv(dev);
  2856. /* Add status entries */
  2857. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2858. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2859. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2860. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2861. netdev_queue_numa_node_read(netdevq));
  2862. if (!txq->q.desc)
  2863. return -ENOMEM;
  2864. memset(&c, 0, sizeof(c));
  2865. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
  2866. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2867. FW_EQ_ETH_CMD_PFN_V(adap->pf) |
  2868. FW_EQ_ETH_CMD_VFN_V(0));
  2869. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
  2870. FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
  2871. c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
  2872. FW_EQ_ETH_CMD_VIID_V(pi->viid));
  2873. c.fetchszm_to_iqid =
  2874. htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2875. FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
  2876. FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
  2877. c.dcaen_to_eqsize =
  2878. htonl(FW_EQ_ETH_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2879. FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2880. FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2881. FW_EQ_ETH_CMD_EQSIZE_V(nentries));
  2882. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2883. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2884. if (ret) {
  2885. kfree(txq->q.sdesc);
  2886. txq->q.sdesc = NULL;
  2887. dma_free_coherent(adap->pdev_dev,
  2888. nentries * sizeof(struct tx_desc),
  2889. txq->q.desc, txq->q.phys_addr);
  2890. txq->q.desc = NULL;
  2891. return ret;
  2892. }
  2893. txq->q.q_type = CXGB4_TXQ_ETH;
  2894. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
  2895. txq->txq = netdevq;
  2896. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  2897. txq->mapping_err = 0;
  2898. return 0;
  2899. }
  2900. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  2901. struct net_device *dev, unsigned int iqid,
  2902. unsigned int cmplqid)
  2903. {
  2904. int ret, nentries;
  2905. struct fw_eq_ctrl_cmd c;
  2906. struct sge *s = &adap->sge;
  2907. struct port_info *pi = netdev_priv(dev);
  2908. /* Add status entries */
  2909. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2910. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  2911. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  2912. NULL, 0, dev_to_node(adap->pdev_dev));
  2913. if (!txq->q.desc)
  2914. return -ENOMEM;
  2915. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
  2916. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2917. FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
  2918. FW_EQ_CTRL_CMD_VFN_V(0));
  2919. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
  2920. FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
  2921. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
  2922. c.physeqid_pkd = htonl(0);
  2923. c.fetchszm_to_iqid =
  2924. htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2925. FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
  2926. FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
  2927. c.dcaen_to_eqsize =
  2928. htonl(FW_EQ_CTRL_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2929. FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2930. FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2931. FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
  2932. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2933. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2934. if (ret) {
  2935. dma_free_coherent(adap->pdev_dev,
  2936. nentries * sizeof(struct tx_desc),
  2937. txq->q.desc, txq->q.phys_addr);
  2938. txq->q.desc = NULL;
  2939. return ret;
  2940. }
  2941. txq->q.q_type = CXGB4_TXQ_CTRL;
  2942. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
  2943. txq->adap = adap;
  2944. skb_queue_head_init(&txq->sendq);
  2945. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  2946. txq->full = 0;
  2947. return 0;
  2948. }
  2949. int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
  2950. unsigned int cmplqid)
  2951. {
  2952. u32 param, val;
  2953. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  2954. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
  2955. FW_PARAMS_PARAM_YZ_V(eqid));
  2956. val = cmplqid;
  2957. return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
  2958. }
  2959. int t4_sge_alloc_uld_txq(struct adapter *adap, struct sge_uld_txq *txq,
  2960. struct net_device *dev, unsigned int iqid,
  2961. unsigned int uld_type)
  2962. {
  2963. int ret, nentries;
  2964. struct fw_eq_ofld_cmd c;
  2965. struct sge *s = &adap->sge;
  2966. struct port_info *pi = netdev_priv(dev);
  2967. int cmd = FW_EQ_OFLD_CMD;
  2968. /* Add status entries */
  2969. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2970. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2971. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2972. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2973. NUMA_NO_NODE);
  2974. if (!txq->q.desc)
  2975. return -ENOMEM;
  2976. memset(&c, 0, sizeof(c));
  2977. if (unlikely(uld_type == CXGB4_TX_CRYPTO))
  2978. cmd = FW_EQ_CTRL_CMD;
  2979. c.op_to_vfn = htonl(FW_CMD_OP_V(cmd) | FW_CMD_REQUEST_F |
  2980. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2981. FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
  2982. FW_EQ_OFLD_CMD_VFN_V(0));
  2983. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
  2984. FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
  2985. c.fetchszm_to_iqid =
  2986. htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2987. FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
  2988. FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
  2989. c.dcaen_to_eqsize =
  2990. htonl(FW_EQ_OFLD_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2991. FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2992. FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2993. FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
  2994. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2995. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2996. if (ret) {
  2997. kfree(txq->q.sdesc);
  2998. txq->q.sdesc = NULL;
  2999. dma_free_coherent(adap->pdev_dev,
  3000. nentries * sizeof(struct tx_desc),
  3001. txq->q.desc, txq->q.phys_addr);
  3002. txq->q.desc = NULL;
  3003. return ret;
  3004. }
  3005. txq->q.q_type = CXGB4_TXQ_ULD;
  3006. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
  3007. txq->adap = adap;
  3008. skb_queue_head_init(&txq->sendq);
  3009. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  3010. txq->full = 0;
  3011. txq->mapping_err = 0;
  3012. return 0;
  3013. }
  3014. void free_txq(struct adapter *adap, struct sge_txq *q)
  3015. {
  3016. struct sge *s = &adap->sge;
  3017. dma_free_coherent(adap->pdev_dev,
  3018. q->size * sizeof(struct tx_desc) + s->stat_len,
  3019. q->desc, q->phys_addr);
  3020. q->cntxt_id = 0;
  3021. q->sdesc = NULL;
  3022. q->desc = NULL;
  3023. }
  3024. void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  3025. struct sge_fl *fl)
  3026. {
  3027. struct sge *s = &adap->sge;
  3028. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  3029. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  3030. t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
  3031. rq->cntxt_id, fl_id, 0xffff);
  3032. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  3033. rq->desc, rq->phys_addr);
  3034. netif_napi_del(&rq->napi);
  3035. rq->netdev = NULL;
  3036. rq->cntxt_id = rq->abs_id = 0;
  3037. rq->desc = NULL;
  3038. if (fl) {
  3039. free_rx_bufs(adap, fl, fl->avail);
  3040. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  3041. fl->desc, fl->addr);
  3042. kfree(fl->sdesc);
  3043. fl->sdesc = NULL;
  3044. fl->cntxt_id = 0;
  3045. fl->desc = NULL;
  3046. }
  3047. }
  3048. /**
  3049. * t4_free_ofld_rxqs - free a block of consecutive Rx queues
  3050. * @adap: the adapter
  3051. * @n: number of queues
  3052. * @q: pointer to first queue
  3053. *
  3054. * Release the resources of a consecutive block of offload Rx queues.
  3055. */
  3056. void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
  3057. {
  3058. for ( ; n; n--, q++)
  3059. if (q->rspq.desc)
  3060. free_rspq_fl(adap, &q->rspq,
  3061. q->fl.size ? &q->fl : NULL);
  3062. }
  3063. /**
  3064. * t4_free_sge_resources - free SGE resources
  3065. * @adap: the adapter
  3066. *
  3067. * Frees resources used by the SGE queue sets.
  3068. */
  3069. void t4_free_sge_resources(struct adapter *adap)
  3070. {
  3071. int i;
  3072. struct sge_eth_rxq *eq;
  3073. struct sge_eth_txq *etq;
  3074. /* stop all Rx queues in order to start them draining */
  3075. for (i = 0; i < adap->sge.ethqsets; i++) {
  3076. eq = &adap->sge.ethrxq[i];
  3077. if (eq->rspq.desc)
  3078. t4_iq_stop(adap, adap->mbox, adap->pf, 0,
  3079. FW_IQ_TYPE_FL_INT_CAP,
  3080. eq->rspq.cntxt_id,
  3081. eq->fl.size ? eq->fl.cntxt_id : 0xffff,
  3082. 0xffff);
  3083. }
  3084. /* clean up Ethernet Tx/Rx queues */
  3085. for (i = 0; i < adap->sge.ethqsets; i++) {
  3086. eq = &adap->sge.ethrxq[i];
  3087. if (eq->rspq.desc)
  3088. free_rspq_fl(adap, &eq->rspq,
  3089. eq->fl.size ? &eq->fl : NULL);
  3090. etq = &adap->sge.ethtxq[i];
  3091. if (etq->q.desc) {
  3092. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  3093. etq->q.cntxt_id);
  3094. __netif_tx_lock_bh(etq->txq);
  3095. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  3096. __netif_tx_unlock_bh(etq->txq);
  3097. kfree(etq->q.sdesc);
  3098. free_txq(adap, &etq->q);
  3099. }
  3100. }
  3101. /* clean up control Tx queues */
  3102. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  3103. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  3104. if (cq->q.desc) {
  3105. tasklet_kill(&cq->qresume_tsk);
  3106. t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
  3107. cq->q.cntxt_id);
  3108. __skb_queue_purge(&cq->sendq);
  3109. free_txq(adap, &cq->q);
  3110. }
  3111. }
  3112. if (adap->sge.fw_evtq.desc)
  3113. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  3114. if (adap->sge.intrq.desc)
  3115. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  3116. if (!is_t4(adap->params.chip)) {
  3117. etq = &adap->sge.ptptxq;
  3118. if (etq->q.desc) {
  3119. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  3120. etq->q.cntxt_id);
  3121. spin_lock_bh(&adap->ptp_lock);
  3122. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  3123. spin_unlock_bh(&adap->ptp_lock);
  3124. kfree(etq->q.sdesc);
  3125. free_txq(adap, &etq->q);
  3126. }
  3127. }
  3128. /* clear the reverse egress queue map */
  3129. memset(adap->sge.egr_map, 0,
  3130. adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
  3131. }
  3132. void t4_sge_start(struct adapter *adap)
  3133. {
  3134. adap->sge.ethtxq_rover = 0;
  3135. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  3136. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  3137. }
  3138. /**
  3139. * t4_sge_stop - disable SGE operation
  3140. * @adap: the adapter
  3141. *
  3142. * Stop tasklets and timers associated with the DMA engine. Note that
  3143. * this is effective only if measures have been taken to disable any HW
  3144. * events that may restart them.
  3145. */
  3146. void t4_sge_stop(struct adapter *adap)
  3147. {
  3148. int i;
  3149. struct sge *s = &adap->sge;
  3150. if (in_interrupt()) /* actions below require waiting */
  3151. return;
  3152. if (s->rx_timer.function)
  3153. del_timer_sync(&s->rx_timer);
  3154. if (s->tx_timer.function)
  3155. del_timer_sync(&s->tx_timer);
  3156. if (is_offload(adap)) {
  3157. struct sge_uld_txq_info *txq_info;
  3158. txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
  3159. if (txq_info) {
  3160. struct sge_uld_txq *txq = txq_info->uldtxq;
  3161. for_each_ofldtxq(&adap->sge, i) {
  3162. if (txq->q.desc)
  3163. tasklet_kill(&txq->qresume_tsk);
  3164. }
  3165. }
  3166. }
  3167. if (is_pci_uld(adap)) {
  3168. struct sge_uld_txq_info *txq_info;
  3169. txq_info = adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
  3170. if (txq_info) {
  3171. struct sge_uld_txq *txq = txq_info->uldtxq;
  3172. for_each_ofldtxq(&adap->sge, i) {
  3173. if (txq->q.desc)
  3174. tasklet_kill(&txq->qresume_tsk);
  3175. }
  3176. }
  3177. }
  3178. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  3179. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  3180. if (cq->q.desc)
  3181. tasklet_kill(&cq->qresume_tsk);
  3182. }
  3183. }
  3184. /**
  3185. * t4_sge_init_soft - grab core SGE values needed by SGE code
  3186. * @adap: the adapter
  3187. *
  3188. * We need to grab the SGE operating parameters that we need to have
  3189. * in order to do our job and make sure we can live with them.
  3190. */
  3191. static int t4_sge_init_soft(struct adapter *adap)
  3192. {
  3193. struct sge *s = &adap->sge;
  3194. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  3195. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  3196. u32 ingress_rx_threshold;
  3197. /*
  3198. * Verify that CPL messages are going to the Ingress Queue for
  3199. * process_responses() and that only packet data is going to the
  3200. * Free Lists.
  3201. */
  3202. if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
  3203. RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
  3204. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  3205. return -EINVAL;
  3206. }
  3207. /*
  3208. * Validate the Host Buffer Register Array indices that we want to
  3209. * use ...
  3210. *
  3211. * XXX Note that we should really read through the Host Buffer Size
  3212. * XXX register array and find the indices of the Buffer Sizes which
  3213. * XXX meet our needs!
  3214. */
  3215. #define READ_FL_BUF(x) \
  3216. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
  3217. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  3218. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  3219. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  3220. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  3221. /* We only bother using the Large Page logic if the Large Page Buffer
  3222. * is larger than our Page Size Buffer.
  3223. */
  3224. if (fl_large_pg <= fl_small_pg)
  3225. fl_large_pg = 0;
  3226. #undef READ_FL_BUF
  3227. /* The Page Size Buffer must be exactly equal to our Page Size and the
  3228. * Large Page Size Buffer should be 0 (per above) or a power of 2.
  3229. */
  3230. if (fl_small_pg != PAGE_SIZE ||
  3231. (fl_large_pg & (fl_large_pg-1)) != 0) {
  3232. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  3233. fl_small_pg, fl_large_pg);
  3234. return -EINVAL;
  3235. }
  3236. if (fl_large_pg)
  3237. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  3238. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  3239. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  3240. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  3241. fl_small_mtu, fl_large_mtu);
  3242. return -EINVAL;
  3243. }
  3244. /*
  3245. * Retrieve our RX interrupt holdoff timer values and counter
  3246. * threshold values from the SGE parameters.
  3247. */
  3248. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
  3249. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
  3250. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
  3251. s->timer_val[0] = core_ticks_to_us(adap,
  3252. TIMERVALUE0_G(timer_value_0_and_1));
  3253. s->timer_val[1] = core_ticks_to_us(adap,
  3254. TIMERVALUE1_G(timer_value_0_and_1));
  3255. s->timer_val[2] = core_ticks_to_us(adap,
  3256. TIMERVALUE2_G(timer_value_2_and_3));
  3257. s->timer_val[3] = core_ticks_to_us(adap,
  3258. TIMERVALUE3_G(timer_value_2_and_3));
  3259. s->timer_val[4] = core_ticks_to_us(adap,
  3260. TIMERVALUE4_G(timer_value_4_and_5));
  3261. s->timer_val[5] = core_ticks_to_us(adap,
  3262. TIMERVALUE5_G(timer_value_4_and_5));
  3263. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
  3264. s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
  3265. s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
  3266. s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
  3267. s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
  3268. return 0;
  3269. }
  3270. /**
  3271. * t4_sge_init - initialize SGE
  3272. * @adap: the adapter
  3273. *
  3274. * Perform low-level SGE code initialization needed every time after a
  3275. * chip reset.
  3276. */
  3277. int t4_sge_init(struct adapter *adap)
  3278. {
  3279. struct sge *s = &adap->sge;
  3280. u32 sge_control, sge_conm_ctrl;
  3281. int ret, egress_threshold;
  3282. /*
  3283. * Ingress Padding Boundary and Egress Status Page Size are set up by
  3284. * t4_fixup_host_params().
  3285. */
  3286. sge_control = t4_read_reg(adap, SGE_CONTROL_A);
  3287. s->pktshift = PKTSHIFT_G(sge_control);
  3288. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
  3289. s->fl_align = t4_fl_pkt_align(adap);
  3290. ret = t4_sge_init_soft(adap);
  3291. if (ret < 0)
  3292. return ret;
  3293. /*
  3294. * A FL with <= fl_starve_thres buffers is starving and a periodic
  3295. * timer will attempt to refill it. This needs to be larger than the
  3296. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  3297. * stuck waiting for new packets while the SGE is waiting for us to
  3298. * give it more Free List entries. (Note that the SGE's Egress
  3299. * Congestion Threshold is in units of 2 Free List pointers.) For T4,
  3300. * there was only a single field to control this. For T5 there's the
  3301. * original field which now only applies to Unpacked Mode Free List
  3302. * buffers and a new field which only applies to Packed Mode Free List
  3303. * buffers.
  3304. */
  3305. sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
  3306. switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
  3307. case CHELSIO_T4:
  3308. egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
  3309. break;
  3310. case CHELSIO_T5:
  3311. egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  3312. break;
  3313. case CHELSIO_T6:
  3314. egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  3315. break;
  3316. default:
  3317. dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
  3318. CHELSIO_CHIP_VERSION(adap->params.chip));
  3319. return -EINVAL;
  3320. }
  3321. s->fl_starve_thres = 2*egress_threshold + 1;
  3322. t4_idma_monitor_init(adap, &s->idma_monitor);
  3323. /* Set up timers used for recuring callbacks to process RX and TX
  3324. * administrative tasks.
  3325. */
  3326. timer_setup(&s->rx_timer, sge_rx_timer_cb, 0);
  3327. timer_setup(&s->tx_timer, sge_tx_timer_cb, 0);
  3328. spin_lock_init(&s->intrq_lock);
  3329. return 0;
  3330. }