sge.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964
  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/ipv6.h>
  44. #include <net/tcp.h>
  45. #include "cxgb4.h"
  46. #include "t4_regs.h"
  47. #include "t4_msg.h"
  48. #include "t4fw_api.h"
  49. /*
  50. * Rx buffer size. We use largish buffers if possible but settle for single
  51. * pages under memory shortage.
  52. */
  53. #if PAGE_SHIFT >= 16
  54. # define FL_PG_ORDER 0
  55. #else
  56. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  57. #endif
  58. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  59. #define RX_COPY_THRES 256
  60. #define RX_PULL_LEN 128
  61. /*
  62. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  63. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  64. */
  65. #define RX_PKT_SKB_LEN 512
  66. /*
  67. * Max number of Tx descriptors we clean up at a time. Should be modest as
  68. * freeing skbs isn't cheap and it happens while holding locks. We just need
  69. * to free packets faster than they arrive, we eventually catch up and keep
  70. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
  71. */
  72. #define MAX_TX_RECLAIM 16
  73. /*
  74. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  75. * allocating buffers isn't cheap either.
  76. */
  77. #define MAX_RX_REFILL 16U
  78. /*
  79. * Period of the Rx queue check timer. This timer is infrequent as it has
  80. * something to do only when the system experiences severe memory shortage.
  81. */
  82. #define RX_QCHECK_PERIOD (HZ / 2)
  83. /*
  84. * Period of the Tx queue check timer.
  85. */
  86. #define TX_QCHECK_PERIOD (HZ / 2)
  87. /* SGE Hung Ingress DMA Threshold Warning time (in Hz) and Warning Repeat Rate
  88. * (in RX_QCHECK_PERIOD multiples). If we find one of the SGE Ingress DMA
  89. * State Machines in the same state for this amount of time (in HZ) then we'll
  90. * issue a warning about a potential hang. We'll repeat the warning as the
  91. * SGE Ingress DMA Channel appears to be hung every N RX_QCHECK_PERIODs till
  92. * the situation clears. If the situation clears, we'll note that as well.
  93. */
  94. #define SGE_IDMA_WARN_THRESH (1 * HZ)
  95. #define SGE_IDMA_WARN_REPEAT (20 * RX_QCHECK_PERIOD)
  96. /*
  97. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  98. */
  99. #define MAX_TIMER_TX_RECLAIM 100
  100. /*
  101. * Timer index used when backing off due to memory shortage.
  102. */
  103. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  104. /*
  105. * An FL with <= FL_STARVE_THRES buffers is starving and a periodic timer will
  106. * attempt to refill it.
  107. */
  108. #define FL_STARVE_THRES 4
  109. /*
  110. * Suspend an Ethernet Tx queue with fewer available descriptors than this.
  111. * This is the same as calc_tx_descs() for a TSO packet with
  112. * nr_frags == MAX_SKB_FRAGS.
  113. */
  114. #define ETHTXQ_STOP_THRES \
  115. (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
  116. /*
  117. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  118. * for a full sized WR.
  119. */
  120. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  121. /*
  122. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  123. * into a WR.
  124. */
  125. #define MAX_IMM_TX_PKT_LEN 128
  126. /*
  127. * Max size of a WR sent through a control Tx queue.
  128. */
  129. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  130. struct tx_sw_desc { /* SW state per Tx descriptor */
  131. struct sk_buff *skb;
  132. struct ulptx_sgl *sgl;
  133. };
  134. struct rx_sw_desc { /* SW state per Rx descriptor */
  135. struct page *page;
  136. dma_addr_t dma_addr;
  137. };
  138. /*
  139. * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
  140. * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
  141. * We could easily support more but there doesn't seem to be much need for
  142. * that ...
  143. */
  144. #define FL_MTU_SMALL 1500
  145. #define FL_MTU_LARGE 9000
  146. static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
  147. unsigned int mtu)
  148. {
  149. struct sge *s = &adapter->sge;
  150. return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
  151. }
  152. #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
  153. #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
  154. /*
  155. * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
  156. * these to specify the buffer size as an index into the SGE Free List Buffer
  157. * Size register array. We also use bit 4, when the buffer has been unmapped
  158. * for DMA, but this is of course never sent to the hardware and is only used
  159. * to prevent double unmappings. All of the above requires that the Free List
  160. * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
  161. * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
  162. * Free List Buffer alignment is 32 bytes, this works out for us ...
  163. */
  164. enum {
  165. RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
  166. RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
  167. RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
  168. /*
  169. * XXX We shouldn't depend on being able to use these indices.
  170. * XXX Especially when some other Master PF has initialized the
  171. * XXX adapter or we use the Firmware Configuration File. We
  172. * XXX should really search through the Host Buffer Size register
  173. * XXX array for the appropriately sized buffer indices.
  174. */
  175. RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
  176. RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
  177. RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
  178. RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
  179. };
  180. static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
  181. #define MIN_NAPI_WORK 1
  182. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  183. {
  184. return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
  185. }
  186. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  187. {
  188. return !(d->dma_addr & RX_UNMAPPED_BUF);
  189. }
  190. /**
  191. * txq_avail - return the number of available slots in a Tx queue
  192. * @q: the Tx queue
  193. *
  194. * Returns the number of descriptors in a Tx queue available to write new
  195. * packets.
  196. */
  197. static inline unsigned int txq_avail(const struct sge_txq *q)
  198. {
  199. return q->size - 1 - q->in_use;
  200. }
  201. /**
  202. * fl_cap - return the capacity of a free-buffer list
  203. * @fl: the FL
  204. *
  205. * Returns the capacity of a free-buffer list. The capacity is less than
  206. * the size because one descriptor needs to be left unpopulated, otherwise
  207. * HW will think the FL is empty.
  208. */
  209. static inline unsigned int fl_cap(const struct sge_fl *fl)
  210. {
  211. return fl->size - 8; /* 1 descriptor = 8 buffers */
  212. }
  213. static inline bool fl_starving(const struct sge_fl *fl)
  214. {
  215. return fl->avail - fl->pend_cred <= FL_STARVE_THRES;
  216. }
  217. static int map_skb(struct device *dev, const struct sk_buff *skb,
  218. dma_addr_t *addr)
  219. {
  220. const skb_frag_t *fp, *end;
  221. const struct skb_shared_info *si;
  222. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  223. if (dma_mapping_error(dev, *addr))
  224. goto out_err;
  225. si = skb_shinfo(skb);
  226. end = &si->frags[si->nr_frags];
  227. for (fp = si->frags; fp < end; fp++) {
  228. *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
  229. DMA_TO_DEVICE);
  230. if (dma_mapping_error(dev, *addr))
  231. goto unwind;
  232. }
  233. return 0;
  234. unwind:
  235. while (fp-- > si->frags)
  236. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  237. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  238. out_err:
  239. return -ENOMEM;
  240. }
  241. #ifdef CONFIG_NEED_DMA_MAP_STATE
  242. static void unmap_skb(struct device *dev, const struct sk_buff *skb,
  243. const dma_addr_t *addr)
  244. {
  245. const skb_frag_t *fp, *end;
  246. const struct skb_shared_info *si;
  247. dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
  248. si = skb_shinfo(skb);
  249. end = &si->frags[si->nr_frags];
  250. for (fp = si->frags; fp < end; fp++)
  251. dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
  252. }
  253. /**
  254. * deferred_unmap_destructor - unmap a packet when it is freed
  255. * @skb: the packet
  256. *
  257. * This is the packet destructor used for Tx packets that need to remain
  258. * mapped until they are freed rather than until their Tx descriptors are
  259. * freed.
  260. */
  261. static void deferred_unmap_destructor(struct sk_buff *skb)
  262. {
  263. unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
  264. }
  265. #endif
  266. static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
  267. const struct ulptx_sgl *sgl, const struct sge_txq *q)
  268. {
  269. const struct ulptx_sge_pair *p;
  270. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  271. if (likely(skb_headlen(skb)))
  272. dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  273. DMA_TO_DEVICE);
  274. else {
  275. dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  276. DMA_TO_DEVICE);
  277. nfrags--;
  278. }
  279. /*
  280. * the complexity below is because of the possibility of a wrap-around
  281. * in the middle of an SGL
  282. */
  283. for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
  284. if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
  285. unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  286. ntohl(p->len[0]), DMA_TO_DEVICE);
  287. dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
  288. ntohl(p->len[1]), DMA_TO_DEVICE);
  289. p++;
  290. } else if ((u8 *)p == (u8 *)q->stat) {
  291. p = (const struct ulptx_sge_pair *)q->desc;
  292. goto unmap;
  293. } else if ((u8 *)p + 8 == (u8 *)q->stat) {
  294. const __be64 *addr = (const __be64 *)q->desc;
  295. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  296. ntohl(p->len[0]), DMA_TO_DEVICE);
  297. dma_unmap_page(dev, be64_to_cpu(addr[1]),
  298. ntohl(p->len[1]), DMA_TO_DEVICE);
  299. p = (const struct ulptx_sge_pair *)&addr[2];
  300. } else {
  301. const __be64 *addr = (const __be64 *)q->desc;
  302. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  303. ntohl(p->len[0]), DMA_TO_DEVICE);
  304. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  305. ntohl(p->len[1]), DMA_TO_DEVICE);
  306. p = (const struct ulptx_sge_pair *)&addr[1];
  307. }
  308. }
  309. if (nfrags) {
  310. __be64 addr;
  311. if ((u8 *)p == (u8 *)q->stat)
  312. p = (const struct ulptx_sge_pair *)q->desc;
  313. addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
  314. *(const __be64 *)q->desc;
  315. dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
  316. DMA_TO_DEVICE);
  317. }
  318. }
  319. /**
  320. * free_tx_desc - reclaims Tx descriptors and their buffers
  321. * @adapter: the adapter
  322. * @q: the Tx queue to reclaim descriptors from
  323. * @n: the number of descriptors to reclaim
  324. * @unmap: whether the buffers should be unmapped for DMA
  325. *
  326. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  327. * Tx buffers. Called with the Tx queue lock held.
  328. */
  329. static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
  330. unsigned int n, bool unmap)
  331. {
  332. struct tx_sw_desc *d;
  333. unsigned int cidx = q->cidx;
  334. struct device *dev = adap->pdev_dev;
  335. d = &q->sdesc[cidx];
  336. while (n--) {
  337. if (d->skb) { /* an SGL is present */
  338. if (unmap)
  339. unmap_sgl(dev, d->skb, d->sgl, q);
  340. dev_consume_skb_any(d->skb);
  341. d->skb = NULL;
  342. }
  343. ++d;
  344. if (++cidx == q->size) {
  345. cidx = 0;
  346. d = q->sdesc;
  347. }
  348. }
  349. q->cidx = cidx;
  350. }
  351. /*
  352. * Return the number of reclaimable descriptors in a Tx queue.
  353. */
  354. static inline int reclaimable(const struct sge_txq *q)
  355. {
  356. int hw_cidx = ntohs(q->stat->cidx);
  357. hw_cidx -= q->cidx;
  358. return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
  359. }
  360. /**
  361. * reclaim_completed_tx - reclaims completed Tx descriptors
  362. * @adap: the adapter
  363. * @q: the Tx queue to reclaim completed descriptors from
  364. * @unmap: whether the buffers should be unmapped for DMA
  365. *
  366. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  367. * and frees the associated buffers if possible. Called with the Tx
  368. * queue locked.
  369. */
  370. static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  371. bool unmap)
  372. {
  373. int avail = reclaimable(q);
  374. if (avail) {
  375. /*
  376. * Limit the amount of clean up work we do at a time to keep
  377. * the Tx lock hold time O(1).
  378. */
  379. if (avail > MAX_TX_RECLAIM)
  380. avail = MAX_TX_RECLAIM;
  381. free_tx_desc(adap, q, avail, unmap);
  382. q->in_use -= avail;
  383. }
  384. }
  385. static inline int get_buf_size(struct adapter *adapter,
  386. const struct rx_sw_desc *d)
  387. {
  388. struct sge *s = &adapter->sge;
  389. unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
  390. int buf_size;
  391. switch (rx_buf_size_idx) {
  392. case RX_SMALL_PG_BUF:
  393. buf_size = PAGE_SIZE;
  394. break;
  395. case RX_LARGE_PG_BUF:
  396. buf_size = PAGE_SIZE << s->fl_pg_order;
  397. break;
  398. case RX_SMALL_MTU_BUF:
  399. buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
  400. break;
  401. case RX_LARGE_MTU_BUF:
  402. buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
  403. break;
  404. default:
  405. BUG_ON(1);
  406. }
  407. return buf_size;
  408. }
  409. /**
  410. * free_rx_bufs - free the Rx buffers on an SGE free list
  411. * @adap: the adapter
  412. * @q: the SGE free list to free buffers from
  413. * @n: how many buffers to free
  414. *
  415. * Release the next @n buffers on an SGE free-buffer Rx queue. The
  416. * buffers must be made inaccessible to HW before calling this function.
  417. */
  418. static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
  419. {
  420. while (n--) {
  421. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  422. if (is_buf_mapped(d))
  423. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  424. get_buf_size(adap, d),
  425. PCI_DMA_FROMDEVICE);
  426. put_page(d->page);
  427. d->page = NULL;
  428. if (++q->cidx == q->size)
  429. q->cidx = 0;
  430. q->avail--;
  431. }
  432. }
  433. /**
  434. * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
  435. * @adap: the adapter
  436. * @q: the SGE free list
  437. *
  438. * Unmap the current buffer on an SGE free-buffer Rx queue. The
  439. * buffer must be made inaccessible to HW before calling this function.
  440. *
  441. * This is similar to @free_rx_bufs above but does not free the buffer.
  442. * Do note that the FL still loses any further access to the buffer.
  443. */
  444. static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
  445. {
  446. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  447. if (is_buf_mapped(d))
  448. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  449. get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
  450. d->page = NULL;
  451. if (++q->cidx == q->size)
  452. q->cidx = 0;
  453. q->avail--;
  454. }
  455. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  456. {
  457. u32 val;
  458. if (q->pend_cred >= 8) {
  459. val = PIDX(q->pend_cred / 8);
  460. if (!is_t4(adap->params.chip))
  461. val |= DBTYPE(1);
  462. val |= DBPRIO(1);
  463. wmb();
  464. /* If we're on T4, use the old doorbell mechanism; otherwise
  465. * use the new BAR2 mechanism.
  466. */
  467. if (is_t4(adap->params.chip)) {
  468. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
  469. val | QID(q->cntxt_id));
  470. } else {
  471. writel(val, adap->bar2 + q->udb + SGE_UDB_KDOORBELL);
  472. /* This Write memory Barrier will force the write to
  473. * the User Doorbell area to be flushed.
  474. */
  475. wmb();
  476. }
  477. q->pend_cred &= 7;
  478. }
  479. }
  480. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  481. dma_addr_t mapping)
  482. {
  483. sd->page = pg;
  484. sd->dma_addr = mapping; /* includes size low bits */
  485. }
  486. /**
  487. * refill_fl - refill an SGE Rx buffer ring
  488. * @adap: the adapter
  489. * @q: the ring to refill
  490. * @n: the number of new buffers to allocate
  491. * @gfp: the gfp flags for the allocations
  492. *
  493. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  494. * allocated with the supplied gfp flags. The caller must assure that
  495. * @n does not exceed the queue's capacity. If afterwards the queue is
  496. * found critically low mark it as starving in the bitmap of starving FLs.
  497. *
  498. * Returns the number of buffers allocated.
  499. */
  500. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  501. gfp_t gfp)
  502. {
  503. struct sge *s = &adap->sge;
  504. struct page *pg;
  505. dma_addr_t mapping;
  506. unsigned int cred = q->avail;
  507. __be64 *d = &q->desc[q->pidx];
  508. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  509. gfp |= __GFP_NOWARN | __GFP_COLD;
  510. if (s->fl_pg_order == 0)
  511. goto alloc_small_pages;
  512. /*
  513. * Prefer large buffers
  514. */
  515. while (n) {
  516. pg = alloc_pages(gfp | __GFP_COMP, s->fl_pg_order);
  517. if (unlikely(!pg)) {
  518. q->large_alloc_failed++;
  519. break; /* fall back to single pages */
  520. }
  521. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  522. PAGE_SIZE << s->fl_pg_order,
  523. PCI_DMA_FROMDEVICE);
  524. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  525. __free_pages(pg, s->fl_pg_order);
  526. goto out; /* do not try small pages for this error */
  527. }
  528. mapping |= RX_LARGE_PG_BUF;
  529. *d++ = cpu_to_be64(mapping);
  530. set_rx_sw_desc(sd, pg, mapping);
  531. sd++;
  532. q->avail++;
  533. if (++q->pidx == q->size) {
  534. q->pidx = 0;
  535. sd = q->sdesc;
  536. d = q->desc;
  537. }
  538. n--;
  539. }
  540. alloc_small_pages:
  541. while (n--) {
  542. pg = __skb_alloc_page(gfp, NULL);
  543. if (unlikely(!pg)) {
  544. q->alloc_failed++;
  545. break;
  546. }
  547. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  548. PCI_DMA_FROMDEVICE);
  549. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  550. put_page(pg);
  551. goto out;
  552. }
  553. *d++ = cpu_to_be64(mapping);
  554. set_rx_sw_desc(sd, pg, mapping);
  555. sd++;
  556. q->avail++;
  557. if (++q->pidx == q->size) {
  558. q->pidx = 0;
  559. sd = q->sdesc;
  560. d = q->desc;
  561. }
  562. }
  563. out: cred = q->avail - cred;
  564. q->pend_cred += cred;
  565. ring_fl_db(adap, q);
  566. if (unlikely(fl_starving(q))) {
  567. smp_wmb();
  568. set_bit(q->cntxt_id - adap->sge.egr_start,
  569. adap->sge.starving_fl);
  570. }
  571. return cred;
  572. }
  573. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  574. {
  575. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  576. GFP_ATOMIC);
  577. }
  578. /**
  579. * alloc_ring - allocate resources for an SGE descriptor ring
  580. * @dev: the PCI device's core device
  581. * @nelem: the number of descriptors
  582. * @elem_size: the size of each descriptor
  583. * @sw_size: the size of the SW state associated with each ring element
  584. * @phys: the physical address of the allocated ring
  585. * @metadata: address of the array holding the SW state for the ring
  586. * @stat_size: extra space in HW ring for status information
  587. * @node: preferred node for memory allocations
  588. *
  589. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  590. * free buffer lists, or response queues. Each SGE ring requires
  591. * space for its HW descriptors plus, optionally, space for the SW state
  592. * associated with each HW entry (the metadata). The function returns
  593. * three values: the virtual address for the HW ring (the return value
  594. * of the function), the bus address of the HW ring, and the address
  595. * of the SW ring.
  596. */
  597. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  598. size_t sw_size, dma_addr_t *phys, void *metadata,
  599. size_t stat_size, int node)
  600. {
  601. size_t len = nelem * elem_size + stat_size;
  602. void *s = NULL;
  603. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  604. if (!p)
  605. return NULL;
  606. if (sw_size) {
  607. s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
  608. if (!s) {
  609. dma_free_coherent(dev, len, p, *phys);
  610. return NULL;
  611. }
  612. }
  613. if (metadata)
  614. *(void **)metadata = s;
  615. memset(p, 0, len);
  616. return p;
  617. }
  618. /**
  619. * sgl_len - calculates the size of an SGL of the given capacity
  620. * @n: the number of SGL entries
  621. *
  622. * Calculates the number of flits needed for a scatter/gather list that
  623. * can hold the given number of entries.
  624. */
  625. static inline unsigned int sgl_len(unsigned int n)
  626. {
  627. n--;
  628. return (3 * n) / 2 + (n & 1) + 2;
  629. }
  630. /**
  631. * flits_to_desc - returns the num of Tx descriptors for the given flits
  632. * @n: the number of flits
  633. *
  634. * Returns the number of Tx descriptors needed for the supplied number
  635. * of flits.
  636. */
  637. static inline unsigned int flits_to_desc(unsigned int n)
  638. {
  639. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  640. return DIV_ROUND_UP(n, 8);
  641. }
  642. /**
  643. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  644. * @skb: the packet
  645. *
  646. * Returns whether an Ethernet packet is small enough to fit as
  647. * immediate data. Return value corresponds to headroom required.
  648. */
  649. static inline int is_eth_imm(const struct sk_buff *skb)
  650. {
  651. int hdrlen = skb_shinfo(skb)->gso_size ?
  652. sizeof(struct cpl_tx_pkt_lso_core) : 0;
  653. hdrlen += sizeof(struct cpl_tx_pkt);
  654. if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
  655. return hdrlen;
  656. return 0;
  657. }
  658. /**
  659. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  660. * @skb: the packet
  661. *
  662. * Returns the number of flits needed for a Tx WR for the given Ethernet
  663. * packet, including the needed WR and CPL headers.
  664. */
  665. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  666. {
  667. unsigned int flits;
  668. int hdrlen = is_eth_imm(skb);
  669. if (hdrlen)
  670. return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
  671. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
  672. if (skb_shinfo(skb)->gso_size)
  673. flits += 2;
  674. return flits;
  675. }
  676. /**
  677. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  678. * @skb: the packet
  679. *
  680. * Returns the number of Tx descriptors needed for the given Ethernet
  681. * packet, including the needed WR and CPL headers.
  682. */
  683. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  684. {
  685. return flits_to_desc(calc_tx_flits(skb));
  686. }
  687. /**
  688. * write_sgl - populate a scatter/gather list for a packet
  689. * @skb: the packet
  690. * @q: the Tx queue we are writing into
  691. * @sgl: starting location for writing the SGL
  692. * @end: points right after the end of the SGL
  693. * @start: start offset into skb main-body data to include in the SGL
  694. * @addr: the list of bus addresses for the SGL elements
  695. *
  696. * Generates a gather list for the buffers that make up a packet.
  697. * The caller must provide adequate space for the SGL that will be written.
  698. * The SGL includes all of the packet's page fragments and the data in its
  699. * main body except for the first @start bytes. @sgl must be 16-byte
  700. * aligned and within a Tx descriptor with available space. @end points
  701. * right after the end of the SGL but does not account for any potential
  702. * wrap around, i.e., @end > @sgl.
  703. */
  704. static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  705. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  706. const dma_addr_t *addr)
  707. {
  708. unsigned int i, len;
  709. struct ulptx_sge_pair *to;
  710. const struct skb_shared_info *si = skb_shinfo(skb);
  711. unsigned int nfrags = si->nr_frags;
  712. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  713. len = skb_headlen(skb) - start;
  714. if (likely(len)) {
  715. sgl->len0 = htonl(len);
  716. sgl->addr0 = cpu_to_be64(addr[0] + start);
  717. nfrags++;
  718. } else {
  719. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  720. sgl->addr0 = cpu_to_be64(addr[1]);
  721. }
  722. sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
  723. if (likely(--nfrags == 0))
  724. return;
  725. /*
  726. * Most of the complexity below deals with the possibility we hit the
  727. * end of the queue in the middle of writing the SGL. For this case
  728. * only we create the SGL in a temporary buffer and then copy it.
  729. */
  730. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  731. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  732. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  733. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  734. to->addr[0] = cpu_to_be64(addr[i]);
  735. to->addr[1] = cpu_to_be64(addr[++i]);
  736. }
  737. if (nfrags) {
  738. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  739. to->len[1] = cpu_to_be32(0);
  740. to->addr[0] = cpu_to_be64(addr[i + 1]);
  741. }
  742. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  743. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  744. if (likely(part0))
  745. memcpy(sgl->sge, buf, part0);
  746. part1 = (u8 *)end - (u8 *)q->stat;
  747. memcpy(q->desc, (u8 *)buf + part0, part1);
  748. end = (void *)q->desc + part1;
  749. }
  750. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  751. *end = 0;
  752. }
  753. /* This function copies a tx_desc struct to memory mapped BAR2 space(user space
  754. * writes). For coalesced WR SGE, fetches data from the FIFO instead of from
  755. * Host.
  756. */
  757. static void cxgb_pio_copy(u64 __iomem *dst, struct tx_desc *desc)
  758. {
  759. int count = sizeof(*desc) / sizeof(u64);
  760. u64 *src = (u64 *)desc;
  761. while (count) {
  762. writeq(*src, dst);
  763. src++;
  764. dst++;
  765. count--;
  766. }
  767. }
  768. /**
  769. * ring_tx_db - check and potentially ring a Tx queue's doorbell
  770. * @adap: the adapter
  771. * @q: the Tx queue
  772. * @n: number of new descriptors to give to HW
  773. *
  774. * Ring the doorbel for a Tx queue.
  775. */
  776. static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  777. {
  778. wmb(); /* write descriptors before telling HW */
  779. if (is_t4(adap->params.chip)) {
  780. u32 val = PIDX(n);
  781. unsigned long flags;
  782. /* For T4 we need to participate in the Doorbell Recovery
  783. * mechanism.
  784. */
  785. spin_lock_irqsave(&q->db_lock, flags);
  786. if (!q->db_disabled)
  787. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
  788. QID(q->cntxt_id) | val);
  789. else
  790. q->db_pidx_inc += n;
  791. q->db_pidx = q->pidx;
  792. spin_unlock_irqrestore(&q->db_lock, flags);
  793. } else {
  794. u32 val = PIDX_T5(n);
  795. /* T4 and later chips share the same PIDX field offset within
  796. * the doorbell, but T5 and later shrank the field in order to
  797. * gain a bit for Doorbell Priority. The field was absurdly
  798. * large in the first place (14 bits) so we just use the T5
  799. * and later limits and warn if a Queue ID is too large.
  800. */
  801. WARN_ON(val & DBPRIO(1));
  802. /* For T5 and later we use the Write-Combine mapped BAR2 User
  803. * Doorbell mechanism. If we're only writing a single TX
  804. * Descriptor and TX Write Combining hasn't been disabled, we
  805. * can use the Write Combining Gather Buffer; otherwise we use
  806. * the simple doorbell.
  807. */
  808. if (n == 1) {
  809. int index = (q->pidx
  810. ? (q->pidx - 1)
  811. : (q->size - 1));
  812. cxgb_pio_copy(adap->bar2 + q->udb + SGE_UDB_WCDOORBELL,
  813. q->desc + index);
  814. } else {
  815. writel(val, adap->bar2 + q->udb + SGE_UDB_KDOORBELL);
  816. }
  817. /* This Write Memory Barrier will force the write to the User
  818. * Doorbell area to be flushed. This is needed to prevent
  819. * writes on different CPUs for the same queue from hitting
  820. * the adapter out of order. This is required when some Work
  821. * Requests take the Write Combine Gather Buffer path (user
  822. * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
  823. * take the traditional path where we simply increment the
  824. * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
  825. * hardware DMA read the actual Work Request.
  826. */
  827. wmb();
  828. }
  829. }
  830. /**
  831. * inline_tx_skb - inline a packet's data into Tx descriptors
  832. * @skb: the packet
  833. * @q: the Tx queue where the packet will be inlined
  834. * @pos: starting position in the Tx queue where to inline the packet
  835. *
  836. * Inline a packet's contents directly into Tx descriptors, starting at
  837. * the given position within the Tx DMA ring.
  838. * Most of the complexity of this operation is dealing with wrap arounds
  839. * in the middle of the packet we want to inline.
  840. */
  841. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
  842. void *pos)
  843. {
  844. u64 *p;
  845. int left = (void *)q->stat - pos;
  846. if (likely(skb->len <= left)) {
  847. if (likely(!skb->data_len))
  848. skb_copy_from_linear_data(skb, pos, skb->len);
  849. else
  850. skb_copy_bits(skb, 0, pos, skb->len);
  851. pos += skb->len;
  852. } else {
  853. skb_copy_bits(skb, 0, pos, left);
  854. skb_copy_bits(skb, left, q->desc, skb->len - left);
  855. pos = (void *)q->desc + (skb->len - left);
  856. }
  857. /* 0-pad to multiple of 16 */
  858. p = PTR_ALIGN(pos, 8);
  859. if ((uintptr_t)p & 8)
  860. *p = 0;
  861. }
  862. /*
  863. * Figure out what HW csum a packet wants and return the appropriate control
  864. * bits.
  865. */
  866. static u64 hwcsum(const struct sk_buff *skb)
  867. {
  868. int csum_type;
  869. const struct iphdr *iph = ip_hdr(skb);
  870. if (iph->version == 4) {
  871. if (iph->protocol == IPPROTO_TCP)
  872. csum_type = TX_CSUM_TCPIP;
  873. else if (iph->protocol == IPPROTO_UDP)
  874. csum_type = TX_CSUM_UDPIP;
  875. else {
  876. nocsum: /*
  877. * unknown protocol, disable HW csum
  878. * and hope a bad packet is detected
  879. */
  880. return TXPKT_L4CSUM_DIS;
  881. }
  882. } else {
  883. /*
  884. * this doesn't work with extension headers
  885. */
  886. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  887. if (ip6h->nexthdr == IPPROTO_TCP)
  888. csum_type = TX_CSUM_TCPIP6;
  889. else if (ip6h->nexthdr == IPPROTO_UDP)
  890. csum_type = TX_CSUM_UDPIP6;
  891. else
  892. goto nocsum;
  893. }
  894. if (likely(csum_type >= TX_CSUM_TCPIP))
  895. return TXPKT_CSUM_TYPE(csum_type) |
  896. TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
  897. TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
  898. else {
  899. int start = skb_transport_offset(skb);
  900. return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
  901. TXPKT_CSUM_LOC(start + skb->csum_offset);
  902. }
  903. }
  904. static void eth_txq_stop(struct sge_eth_txq *q)
  905. {
  906. netif_tx_stop_queue(q->txq);
  907. q->q.stops++;
  908. }
  909. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  910. {
  911. q->in_use += n;
  912. q->pidx += n;
  913. if (q->pidx >= q->size)
  914. q->pidx -= q->size;
  915. }
  916. /**
  917. * t4_eth_xmit - add a packet to an Ethernet Tx queue
  918. * @skb: the packet
  919. * @dev: the egress net device
  920. *
  921. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  922. */
  923. netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  924. {
  925. int len;
  926. u32 wr_mid;
  927. u64 cntrl, *end;
  928. int qidx, credits;
  929. unsigned int flits, ndesc;
  930. struct adapter *adap;
  931. struct sge_eth_txq *q;
  932. const struct port_info *pi;
  933. struct fw_eth_tx_pkt_wr *wr;
  934. struct cpl_tx_pkt_core *cpl;
  935. const struct skb_shared_info *ssi;
  936. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  937. bool immediate = false;
  938. /*
  939. * The chip min packet length is 10 octets but play safe and reject
  940. * anything shorter than an Ethernet header.
  941. */
  942. if (unlikely(skb->len < ETH_HLEN)) {
  943. out_free: dev_kfree_skb_any(skb);
  944. return NETDEV_TX_OK;
  945. }
  946. pi = netdev_priv(dev);
  947. adap = pi->adapter;
  948. qidx = skb_get_queue_mapping(skb);
  949. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  950. reclaim_completed_tx(adap, &q->q, true);
  951. flits = calc_tx_flits(skb);
  952. ndesc = flits_to_desc(flits);
  953. credits = txq_avail(&q->q) - ndesc;
  954. if (unlikely(credits < 0)) {
  955. eth_txq_stop(q);
  956. dev_err(adap->pdev_dev,
  957. "%s: Tx ring %u full while queue awake!\n",
  958. dev->name, qidx);
  959. return NETDEV_TX_BUSY;
  960. }
  961. if (is_eth_imm(skb))
  962. immediate = true;
  963. if (!immediate &&
  964. unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
  965. q->mapping_err++;
  966. goto out_free;
  967. }
  968. wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
  969. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  970. eth_txq_stop(q);
  971. wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
  972. }
  973. wr = (void *)&q->q.desc[q->q.pidx];
  974. wr->equiq_to_len16 = htonl(wr_mid);
  975. wr->r3 = cpu_to_be64(0);
  976. end = (u64 *)wr + flits;
  977. len = immediate ? skb->len : 0;
  978. ssi = skb_shinfo(skb);
  979. if (ssi->gso_size) {
  980. struct cpl_tx_pkt_lso *lso = (void *)wr;
  981. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  982. int l3hdr_len = skb_network_header_len(skb);
  983. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  984. len += sizeof(*lso);
  985. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  986. FW_WR_IMMDLEN(len));
  987. lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
  988. LSO_FIRST_SLICE | LSO_LAST_SLICE |
  989. LSO_IPV6(v6) |
  990. LSO_ETHHDR_LEN(eth_xtra_len / 4) |
  991. LSO_IPHDR_LEN(l3hdr_len / 4) |
  992. LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
  993. lso->c.ipid_ofst = htons(0);
  994. lso->c.mss = htons(ssi->gso_size);
  995. lso->c.seqno_offset = htonl(0);
  996. if (is_t4(adap->params.chip))
  997. lso->c.len = htonl(skb->len);
  998. else
  999. lso->c.len = htonl(LSO_T5_XFER_SIZE(skb->len));
  1000. cpl = (void *)(lso + 1);
  1001. cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  1002. TXPKT_IPHDR_LEN(l3hdr_len) |
  1003. TXPKT_ETHHDR_LEN(eth_xtra_len);
  1004. q->tso++;
  1005. q->tx_cso += ssi->gso_segs;
  1006. } else {
  1007. len += sizeof(*cpl);
  1008. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  1009. FW_WR_IMMDLEN(len));
  1010. cpl = (void *)(wr + 1);
  1011. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1012. cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
  1013. q->tx_cso++;
  1014. } else
  1015. cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
  1016. }
  1017. if (vlan_tx_tag_present(skb)) {
  1018. q->vlan_ins++;
  1019. cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
  1020. }
  1021. cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
  1022. TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
  1023. cpl->pack = htons(0);
  1024. cpl->len = htons(skb->len);
  1025. cpl->ctrl1 = cpu_to_be64(cntrl);
  1026. if (immediate) {
  1027. inline_tx_skb(skb, &q->q, cpl + 1);
  1028. dev_consume_skb_any(skb);
  1029. } else {
  1030. int last_desc;
  1031. write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
  1032. addr);
  1033. skb_orphan(skb);
  1034. last_desc = q->q.pidx + ndesc - 1;
  1035. if (last_desc >= q->q.size)
  1036. last_desc -= q->q.size;
  1037. q->q.sdesc[last_desc].skb = skb;
  1038. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
  1039. }
  1040. txq_advance(&q->q, ndesc);
  1041. ring_tx_db(adap, &q->q, ndesc);
  1042. return NETDEV_TX_OK;
  1043. }
  1044. /**
  1045. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1046. * @q: the SGE control Tx queue
  1047. *
  1048. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  1049. * that send only immediate data (presently just the control queues) and
  1050. * thus do not have any sk_buffs to release.
  1051. */
  1052. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1053. {
  1054. int hw_cidx = ntohs(q->stat->cidx);
  1055. int reclaim = hw_cidx - q->cidx;
  1056. if (reclaim < 0)
  1057. reclaim += q->size;
  1058. q->in_use -= reclaim;
  1059. q->cidx = hw_cidx;
  1060. }
  1061. /**
  1062. * is_imm - check whether a packet can be sent as immediate data
  1063. * @skb: the packet
  1064. *
  1065. * Returns true if a packet can be sent as a WR with immediate data.
  1066. */
  1067. static inline int is_imm(const struct sk_buff *skb)
  1068. {
  1069. return skb->len <= MAX_CTRL_WR_LEN;
  1070. }
  1071. /**
  1072. * ctrlq_check_stop - check if a control queue is full and should stop
  1073. * @q: the queue
  1074. * @wr: most recent WR written to the queue
  1075. *
  1076. * Check if a control queue has become full and should be stopped.
  1077. * We clean up control queue descriptors very lazily, only when we are out.
  1078. * If the queue is still full after reclaiming any completed descriptors
  1079. * we suspend it and have the last WR wake it up.
  1080. */
  1081. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  1082. {
  1083. reclaim_completed_tx_imm(&q->q);
  1084. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1085. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1086. q->q.stops++;
  1087. q->full = 1;
  1088. }
  1089. }
  1090. /**
  1091. * ctrl_xmit - send a packet through an SGE control Tx queue
  1092. * @q: the control queue
  1093. * @skb: the packet
  1094. *
  1095. * Send a packet through an SGE control Tx queue. Packets sent through
  1096. * a control queue must fit entirely as immediate data.
  1097. */
  1098. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  1099. {
  1100. unsigned int ndesc;
  1101. struct fw_wr_hdr *wr;
  1102. if (unlikely(!is_imm(skb))) {
  1103. WARN_ON(1);
  1104. dev_kfree_skb(skb);
  1105. return NET_XMIT_DROP;
  1106. }
  1107. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  1108. spin_lock(&q->sendq.lock);
  1109. if (unlikely(q->full)) {
  1110. skb->priority = ndesc; /* save for restart */
  1111. __skb_queue_tail(&q->sendq, skb);
  1112. spin_unlock(&q->sendq.lock);
  1113. return NET_XMIT_CN;
  1114. }
  1115. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1116. inline_tx_skb(skb, &q->q, wr);
  1117. txq_advance(&q->q, ndesc);
  1118. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  1119. ctrlq_check_stop(q, wr);
  1120. ring_tx_db(q->adap, &q->q, ndesc);
  1121. spin_unlock(&q->sendq.lock);
  1122. kfree_skb(skb);
  1123. return NET_XMIT_SUCCESS;
  1124. }
  1125. /**
  1126. * restart_ctrlq - restart a suspended control queue
  1127. * @data: the control queue to restart
  1128. *
  1129. * Resumes transmission on a suspended Tx control queue.
  1130. */
  1131. static void restart_ctrlq(unsigned long data)
  1132. {
  1133. struct sk_buff *skb;
  1134. unsigned int written = 0;
  1135. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  1136. spin_lock(&q->sendq.lock);
  1137. reclaim_completed_tx_imm(&q->q);
  1138. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  1139. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  1140. struct fw_wr_hdr *wr;
  1141. unsigned int ndesc = skb->priority; /* previously saved */
  1142. /*
  1143. * Write descriptors and free skbs outside the lock to limit
  1144. * wait times. q->full is still set so new skbs will be queued.
  1145. */
  1146. spin_unlock(&q->sendq.lock);
  1147. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1148. inline_tx_skb(skb, &q->q, wr);
  1149. kfree_skb(skb);
  1150. written += ndesc;
  1151. txq_advance(&q->q, ndesc);
  1152. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1153. unsigned long old = q->q.stops;
  1154. ctrlq_check_stop(q, wr);
  1155. if (q->q.stops != old) { /* suspended anew */
  1156. spin_lock(&q->sendq.lock);
  1157. goto ringdb;
  1158. }
  1159. }
  1160. if (written > 16) {
  1161. ring_tx_db(q->adap, &q->q, written);
  1162. written = 0;
  1163. }
  1164. spin_lock(&q->sendq.lock);
  1165. }
  1166. q->full = 0;
  1167. ringdb: if (written)
  1168. ring_tx_db(q->adap, &q->q, written);
  1169. spin_unlock(&q->sendq.lock);
  1170. }
  1171. /**
  1172. * t4_mgmt_tx - send a management message
  1173. * @adap: the adapter
  1174. * @skb: the packet containing the management message
  1175. *
  1176. * Send a management message through control queue 0.
  1177. */
  1178. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1179. {
  1180. int ret;
  1181. local_bh_disable();
  1182. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1183. local_bh_enable();
  1184. return ret;
  1185. }
  1186. /**
  1187. * is_ofld_imm - check whether a packet can be sent as immediate data
  1188. * @skb: the packet
  1189. *
  1190. * Returns true if a packet can be sent as an offload WR with immediate
  1191. * data. We currently use the same limit as for Ethernet packets.
  1192. */
  1193. static inline int is_ofld_imm(const struct sk_buff *skb)
  1194. {
  1195. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1196. }
  1197. /**
  1198. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1199. * @skb: the packet
  1200. *
  1201. * Returns the number of flits needed for the given offload packet.
  1202. * These packets are already fully constructed and no additional headers
  1203. * will be added.
  1204. */
  1205. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1206. {
  1207. unsigned int flits, cnt;
  1208. if (is_ofld_imm(skb))
  1209. return DIV_ROUND_UP(skb->len, 8);
  1210. flits = skb_transport_offset(skb) / 8U; /* headers */
  1211. cnt = skb_shinfo(skb)->nr_frags;
  1212. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1213. cnt++;
  1214. return flits + sgl_len(cnt);
  1215. }
  1216. /**
  1217. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1218. * @adap: the adapter
  1219. * @q: the queue to stop
  1220. *
  1221. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1222. * inability to map packets. A periodic timer attempts to restart
  1223. * queues so marked.
  1224. */
  1225. static void txq_stop_maperr(struct sge_ofld_txq *q)
  1226. {
  1227. q->mapping_err++;
  1228. q->q.stops++;
  1229. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1230. q->adap->sge.txq_maperr);
  1231. }
  1232. /**
  1233. * ofldtxq_stop - stop an offload Tx queue that has become full
  1234. * @q: the queue to stop
  1235. * @skb: the packet causing the queue to become full
  1236. *
  1237. * Stops an offload Tx queue that has become full and modifies the packet
  1238. * being written to request a wakeup.
  1239. */
  1240. static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
  1241. {
  1242. struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
  1243. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1244. q->q.stops++;
  1245. q->full = 1;
  1246. }
  1247. /**
  1248. * service_ofldq - restart a suspended offload queue
  1249. * @q: the offload queue
  1250. *
  1251. * Services an offload Tx queue by moving packets from its packet queue
  1252. * to the HW Tx ring. The function starts and ends with the queue locked.
  1253. */
  1254. static void service_ofldq(struct sge_ofld_txq *q)
  1255. {
  1256. u64 *pos;
  1257. int credits;
  1258. struct sk_buff *skb;
  1259. unsigned int written = 0;
  1260. unsigned int flits, ndesc;
  1261. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1262. /*
  1263. * We drop the lock but leave skb on sendq, thus retaining
  1264. * exclusive access to the state of the queue.
  1265. */
  1266. spin_unlock(&q->sendq.lock);
  1267. reclaim_completed_tx(q->adap, &q->q, false);
  1268. flits = skb->priority; /* previously saved */
  1269. ndesc = flits_to_desc(flits);
  1270. credits = txq_avail(&q->q) - ndesc;
  1271. BUG_ON(credits < 0);
  1272. if (unlikely(credits < TXQ_STOP_THRES))
  1273. ofldtxq_stop(q, skb);
  1274. pos = (u64 *)&q->q.desc[q->q.pidx];
  1275. if (is_ofld_imm(skb))
  1276. inline_tx_skb(skb, &q->q, pos);
  1277. else if (map_skb(q->adap->pdev_dev, skb,
  1278. (dma_addr_t *)skb->head)) {
  1279. txq_stop_maperr(q);
  1280. spin_lock(&q->sendq.lock);
  1281. break;
  1282. } else {
  1283. int last_desc, hdr_len = skb_transport_offset(skb);
  1284. memcpy(pos, skb->data, hdr_len);
  1285. write_sgl(skb, &q->q, (void *)pos + hdr_len,
  1286. pos + flits, hdr_len,
  1287. (dma_addr_t *)skb->head);
  1288. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1289. skb->dev = q->adap->port[0];
  1290. skb->destructor = deferred_unmap_destructor;
  1291. #endif
  1292. last_desc = q->q.pidx + ndesc - 1;
  1293. if (last_desc >= q->q.size)
  1294. last_desc -= q->q.size;
  1295. q->q.sdesc[last_desc].skb = skb;
  1296. }
  1297. txq_advance(&q->q, ndesc);
  1298. written += ndesc;
  1299. if (unlikely(written > 32)) {
  1300. ring_tx_db(q->adap, &q->q, written);
  1301. written = 0;
  1302. }
  1303. spin_lock(&q->sendq.lock);
  1304. __skb_unlink(skb, &q->sendq);
  1305. if (is_ofld_imm(skb))
  1306. kfree_skb(skb);
  1307. }
  1308. if (likely(written))
  1309. ring_tx_db(q->adap, &q->q, written);
  1310. }
  1311. /**
  1312. * ofld_xmit - send a packet through an offload queue
  1313. * @q: the Tx offload queue
  1314. * @skb: the packet
  1315. *
  1316. * Send an offload packet through an SGE offload queue.
  1317. */
  1318. static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
  1319. {
  1320. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1321. spin_lock(&q->sendq.lock);
  1322. __skb_queue_tail(&q->sendq, skb);
  1323. if (q->sendq.qlen == 1)
  1324. service_ofldq(q);
  1325. spin_unlock(&q->sendq.lock);
  1326. return NET_XMIT_SUCCESS;
  1327. }
  1328. /**
  1329. * restart_ofldq - restart a suspended offload queue
  1330. * @data: the offload queue to restart
  1331. *
  1332. * Resumes transmission on a suspended Tx offload queue.
  1333. */
  1334. static void restart_ofldq(unsigned long data)
  1335. {
  1336. struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
  1337. spin_lock(&q->sendq.lock);
  1338. q->full = 0; /* the queue actually is completely empty now */
  1339. service_ofldq(q);
  1340. spin_unlock(&q->sendq.lock);
  1341. }
  1342. /**
  1343. * skb_txq - return the Tx queue an offload packet should use
  1344. * @skb: the packet
  1345. *
  1346. * Returns the Tx queue an offload packet should use as indicated by bits
  1347. * 1-15 in the packet's queue_mapping.
  1348. */
  1349. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1350. {
  1351. return skb->queue_mapping >> 1;
  1352. }
  1353. /**
  1354. * is_ctrl_pkt - return whether an offload packet is a control packet
  1355. * @skb: the packet
  1356. *
  1357. * Returns whether an offload packet should use an OFLD or a CTRL
  1358. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1359. */
  1360. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1361. {
  1362. return skb->queue_mapping & 1;
  1363. }
  1364. static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
  1365. {
  1366. unsigned int idx = skb_txq(skb);
  1367. if (unlikely(is_ctrl_pkt(skb))) {
  1368. /* Single ctrl queue is a requirement for LE workaround path */
  1369. if (adap->tids.nsftids)
  1370. idx = 0;
  1371. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1372. }
  1373. return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
  1374. }
  1375. /**
  1376. * t4_ofld_send - send an offload packet
  1377. * @adap: the adapter
  1378. * @skb: the packet
  1379. *
  1380. * Sends an offload packet. We use the packet queue_mapping to select the
  1381. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1382. * should be sent as regular or control, bits 1-15 select the queue.
  1383. */
  1384. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1385. {
  1386. int ret;
  1387. local_bh_disable();
  1388. ret = ofld_send(adap, skb);
  1389. local_bh_enable();
  1390. return ret;
  1391. }
  1392. /**
  1393. * cxgb4_ofld_send - send an offload packet
  1394. * @dev: the net device
  1395. * @skb: the packet
  1396. *
  1397. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1398. * intended for ULDs.
  1399. */
  1400. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1401. {
  1402. return t4_ofld_send(netdev2adap(dev), skb);
  1403. }
  1404. EXPORT_SYMBOL(cxgb4_ofld_send);
  1405. static inline void copy_frags(struct sk_buff *skb,
  1406. const struct pkt_gl *gl, unsigned int offset)
  1407. {
  1408. int i;
  1409. /* usually there's just one frag */
  1410. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  1411. gl->frags[0].offset + offset,
  1412. gl->frags[0].size - offset);
  1413. skb_shinfo(skb)->nr_frags = gl->nfrags;
  1414. for (i = 1; i < gl->nfrags; i++)
  1415. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  1416. gl->frags[i].offset,
  1417. gl->frags[i].size);
  1418. /* get a reference to the last page, we don't own it */
  1419. get_page(gl->frags[gl->nfrags - 1].page);
  1420. }
  1421. /**
  1422. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1423. * @gl: the gather list
  1424. * @skb_len: size of sk_buff main body if it carries fragments
  1425. * @pull_len: amount of data to move to the sk_buff's main body
  1426. *
  1427. * Builds an sk_buff from the given packet gather list. Returns the
  1428. * sk_buff or %NULL if sk_buff allocation failed.
  1429. */
  1430. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1431. unsigned int skb_len, unsigned int pull_len)
  1432. {
  1433. struct sk_buff *skb;
  1434. /*
  1435. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1436. * size, which is expected since buffers are at least PAGE_SIZEd.
  1437. * In this case packets up to RX_COPY_THRES have only one fragment.
  1438. */
  1439. if (gl->tot_len <= RX_COPY_THRES) {
  1440. skb = dev_alloc_skb(gl->tot_len);
  1441. if (unlikely(!skb))
  1442. goto out;
  1443. __skb_put(skb, gl->tot_len);
  1444. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1445. } else {
  1446. skb = dev_alloc_skb(skb_len);
  1447. if (unlikely(!skb))
  1448. goto out;
  1449. __skb_put(skb, pull_len);
  1450. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1451. copy_frags(skb, gl, pull_len);
  1452. skb->len = gl->tot_len;
  1453. skb->data_len = skb->len - pull_len;
  1454. skb->truesize += skb->data_len;
  1455. }
  1456. out: return skb;
  1457. }
  1458. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1459. /**
  1460. * t4_pktgl_free - free a packet gather list
  1461. * @gl: the gather list
  1462. *
  1463. * Releases the pages of a packet gather list. We do not own the last
  1464. * page on the list and do not free it.
  1465. */
  1466. static void t4_pktgl_free(const struct pkt_gl *gl)
  1467. {
  1468. int n;
  1469. const struct page_frag *p;
  1470. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1471. put_page(p->page);
  1472. }
  1473. /*
  1474. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1475. * be delivered to anyone and send it to the stack for capture.
  1476. */
  1477. static noinline int handle_trace_pkt(struct adapter *adap,
  1478. const struct pkt_gl *gl)
  1479. {
  1480. struct sk_buff *skb;
  1481. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  1482. if (unlikely(!skb)) {
  1483. t4_pktgl_free(gl);
  1484. return 0;
  1485. }
  1486. if (is_t4(adap->params.chip))
  1487. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  1488. else
  1489. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  1490. skb_reset_mac_header(skb);
  1491. skb->protocol = htons(0xffff);
  1492. skb->dev = adap->port[0];
  1493. netif_receive_skb(skb);
  1494. return 0;
  1495. }
  1496. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1497. const struct cpl_rx_pkt *pkt)
  1498. {
  1499. struct adapter *adapter = rxq->rspq.adap;
  1500. struct sge *s = &adapter->sge;
  1501. int ret;
  1502. struct sk_buff *skb;
  1503. skb = napi_get_frags(&rxq->rspq.napi);
  1504. if (unlikely(!skb)) {
  1505. t4_pktgl_free(gl);
  1506. rxq->stats.rx_drops++;
  1507. return;
  1508. }
  1509. copy_frags(skb, gl, s->pktshift);
  1510. skb->len = gl->tot_len - s->pktshift;
  1511. skb->data_len = skb->len;
  1512. skb->truesize += skb->data_len;
  1513. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1514. skb_record_rx_queue(skb, rxq->rspq.idx);
  1515. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  1516. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1517. PKT_HASH_TYPE_L3);
  1518. if (unlikely(pkt->vlan_ex)) {
  1519. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1520. rxq->stats.vlan_ex++;
  1521. }
  1522. ret = napi_gro_frags(&rxq->rspq.napi);
  1523. if (ret == GRO_HELD)
  1524. rxq->stats.lro_pkts++;
  1525. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1526. rxq->stats.lro_merged++;
  1527. rxq->stats.pkts++;
  1528. rxq->stats.rx_cso++;
  1529. }
  1530. /**
  1531. * t4_ethrx_handler - process an ingress ethernet packet
  1532. * @q: the response queue that received the packet
  1533. * @rsp: the response queue descriptor holding the RX_PKT message
  1534. * @si: the gather list of packet fragments
  1535. *
  1536. * Process an ingress ethernet packet and deliver it to the stack.
  1537. */
  1538. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  1539. const struct pkt_gl *si)
  1540. {
  1541. bool csum_ok;
  1542. struct sk_buff *skb;
  1543. const struct cpl_rx_pkt *pkt;
  1544. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1545. struct sge *s = &q->adap->sge;
  1546. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  1547. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  1548. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  1549. return handle_trace_pkt(q->adap, si);
  1550. pkt = (const struct cpl_rx_pkt *)rsp;
  1551. csum_ok = pkt->csum_calc && !pkt->err_vec &&
  1552. (q->netdev->features & NETIF_F_RXCSUM);
  1553. if ((pkt->l2info & htonl(RXF_TCP)) &&
  1554. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  1555. do_gro(rxq, si, pkt);
  1556. return 0;
  1557. }
  1558. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  1559. if (unlikely(!skb)) {
  1560. t4_pktgl_free(si);
  1561. rxq->stats.rx_drops++;
  1562. return 0;
  1563. }
  1564. __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
  1565. skb->protocol = eth_type_trans(skb, q->netdev);
  1566. skb_record_rx_queue(skb, q->idx);
  1567. if (skb->dev->features & NETIF_F_RXHASH)
  1568. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1569. PKT_HASH_TYPE_L3);
  1570. rxq->stats.pkts++;
  1571. if (csum_ok && (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
  1572. if (!pkt->ip_frag) {
  1573. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1574. rxq->stats.rx_cso++;
  1575. } else if (pkt->l2info & htonl(RXF_IP)) {
  1576. __sum16 c = (__force __sum16)pkt->csum;
  1577. skb->csum = csum_unfold(c);
  1578. skb->ip_summed = CHECKSUM_COMPLETE;
  1579. rxq->stats.rx_cso++;
  1580. }
  1581. } else
  1582. skb_checksum_none_assert(skb);
  1583. if (unlikely(pkt->vlan_ex)) {
  1584. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1585. rxq->stats.vlan_ex++;
  1586. }
  1587. netif_receive_skb(skb);
  1588. return 0;
  1589. }
  1590. /**
  1591. * restore_rx_bufs - put back a packet's Rx buffers
  1592. * @si: the packet gather list
  1593. * @q: the SGE free list
  1594. * @frags: number of FL buffers to restore
  1595. *
  1596. * Puts back on an FL the Rx buffers associated with @si. The buffers
  1597. * have already been unmapped and are left unmapped, we mark them so to
  1598. * prevent further unmapping attempts.
  1599. *
  1600. * This function undoes a series of @unmap_rx_buf calls when we find out
  1601. * that the current packet can't be processed right away afterall and we
  1602. * need to come back to it later. This is a very rare event and there's
  1603. * no effort to make this particularly efficient.
  1604. */
  1605. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  1606. int frags)
  1607. {
  1608. struct rx_sw_desc *d;
  1609. while (frags--) {
  1610. if (q->cidx == 0)
  1611. q->cidx = q->size - 1;
  1612. else
  1613. q->cidx--;
  1614. d = &q->sdesc[q->cidx];
  1615. d->page = si->frags[frags].page;
  1616. d->dma_addr |= RX_UNMAPPED_BUF;
  1617. q->avail++;
  1618. }
  1619. }
  1620. /**
  1621. * is_new_response - check if a response is newly written
  1622. * @r: the response descriptor
  1623. * @q: the response queue
  1624. *
  1625. * Returns true if a response descriptor contains a yet unprocessed
  1626. * response.
  1627. */
  1628. static inline bool is_new_response(const struct rsp_ctrl *r,
  1629. const struct sge_rspq *q)
  1630. {
  1631. return RSPD_GEN(r->type_gen) == q->gen;
  1632. }
  1633. /**
  1634. * rspq_next - advance to the next entry in a response queue
  1635. * @q: the queue
  1636. *
  1637. * Updates the state of a response queue to advance it to the next entry.
  1638. */
  1639. static inline void rspq_next(struct sge_rspq *q)
  1640. {
  1641. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  1642. if (unlikely(++q->cidx == q->size)) {
  1643. q->cidx = 0;
  1644. q->gen ^= 1;
  1645. q->cur_desc = q->desc;
  1646. }
  1647. }
  1648. /**
  1649. * process_responses - process responses from an SGE response queue
  1650. * @q: the ingress queue to process
  1651. * @budget: how many responses can be processed in this round
  1652. *
  1653. * Process responses from an SGE response queue up to the supplied budget.
  1654. * Responses include received packets as well as control messages from FW
  1655. * or HW.
  1656. *
  1657. * Additionally choose the interrupt holdoff time for the next interrupt
  1658. * on this queue. If the system is under memory shortage use a fairly
  1659. * long delay to help recovery.
  1660. */
  1661. static int process_responses(struct sge_rspq *q, int budget)
  1662. {
  1663. int ret, rsp_type;
  1664. int budget_left = budget;
  1665. const struct rsp_ctrl *rc;
  1666. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1667. struct adapter *adapter = q->adap;
  1668. struct sge *s = &adapter->sge;
  1669. while (likely(budget_left)) {
  1670. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1671. if (!is_new_response(rc, q))
  1672. break;
  1673. rmb();
  1674. rsp_type = RSPD_TYPE(rc->type_gen);
  1675. if (likely(rsp_type == RSP_TYPE_FLBUF)) {
  1676. struct page_frag *fp;
  1677. struct pkt_gl si;
  1678. const struct rx_sw_desc *rsd;
  1679. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  1680. if (len & RSPD_NEWBUF) {
  1681. if (likely(q->offset > 0)) {
  1682. free_rx_bufs(q->adap, &rxq->fl, 1);
  1683. q->offset = 0;
  1684. }
  1685. len = RSPD_LEN(len);
  1686. }
  1687. si.tot_len = len;
  1688. /* gather packet fragments */
  1689. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  1690. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  1691. bufsz = get_buf_size(adapter, rsd);
  1692. fp->page = rsd->page;
  1693. fp->offset = q->offset;
  1694. fp->size = min(bufsz, len);
  1695. len -= fp->size;
  1696. if (!len)
  1697. break;
  1698. unmap_rx_buf(q->adap, &rxq->fl);
  1699. }
  1700. /*
  1701. * Last buffer remains mapped so explicitly make it
  1702. * coherent for CPU access.
  1703. */
  1704. dma_sync_single_for_cpu(q->adap->pdev_dev,
  1705. get_buf_addr(rsd),
  1706. fp->size, DMA_FROM_DEVICE);
  1707. si.va = page_address(si.frags[0].page) +
  1708. si.frags[0].offset;
  1709. prefetch(si.va);
  1710. si.nfrags = frags + 1;
  1711. ret = q->handler(q, q->cur_desc, &si);
  1712. if (likely(ret == 0))
  1713. q->offset += ALIGN(fp->size, s->fl_align);
  1714. else
  1715. restore_rx_bufs(&si, &rxq->fl, frags);
  1716. } else if (likely(rsp_type == RSP_TYPE_CPL)) {
  1717. ret = q->handler(q, q->cur_desc, NULL);
  1718. } else {
  1719. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  1720. }
  1721. if (unlikely(ret)) {
  1722. /* couldn't process descriptor, back off for recovery */
  1723. q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
  1724. break;
  1725. }
  1726. rspq_next(q);
  1727. budget_left--;
  1728. }
  1729. if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
  1730. __refill_fl(q->adap, &rxq->fl);
  1731. return budget - budget_left;
  1732. }
  1733. /**
  1734. * napi_rx_handler - the NAPI handler for Rx processing
  1735. * @napi: the napi instance
  1736. * @budget: how many packets we can process in this round
  1737. *
  1738. * Handler for new data events when using NAPI. This does not need any
  1739. * locking or protection from interrupts as data interrupts are off at
  1740. * this point and other adapter interrupts do not interfere (the latter
  1741. * in not a concern at all with MSI-X as non-data interrupts then have
  1742. * a separate handler).
  1743. */
  1744. static int napi_rx_handler(struct napi_struct *napi, int budget)
  1745. {
  1746. unsigned int params;
  1747. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  1748. int work_done = process_responses(q, budget);
  1749. u32 val;
  1750. if (likely(work_done < budget)) {
  1751. int timer_index;
  1752. napi_complete(napi);
  1753. timer_index = QINTR_TIMER_IDX_GET(q->next_intr_params);
  1754. if (q->adaptive_rx) {
  1755. if (work_done > max(timer_pkt_quota[timer_index],
  1756. MIN_NAPI_WORK))
  1757. timer_index = (timer_index + 1);
  1758. else
  1759. timer_index = timer_index - 1;
  1760. timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
  1761. q->next_intr_params = QINTR_TIMER_IDX(timer_index) |
  1762. V_QINTR_CNT_EN;
  1763. params = q->next_intr_params;
  1764. } else {
  1765. params = q->next_intr_params;
  1766. q->next_intr_params = q->intr_params;
  1767. }
  1768. } else
  1769. params = QINTR_TIMER_IDX(7);
  1770. val = CIDXINC(work_done) | SEINTARM(params);
  1771. if (is_t4(q->adap->params.chip)) {
  1772. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS),
  1773. val | INGRESSQID((u32)q->cntxt_id));
  1774. } else {
  1775. writel(val, q->adap->bar2 + q->udb + SGE_UDB_GTS);
  1776. wmb();
  1777. }
  1778. return work_done;
  1779. }
  1780. /*
  1781. * The MSI-X interrupt handler for an SGE response queue.
  1782. */
  1783. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  1784. {
  1785. struct sge_rspq *q = cookie;
  1786. napi_schedule(&q->napi);
  1787. return IRQ_HANDLED;
  1788. }
  1789. /*
  1790. * Process the indirect interrupt entries in the interrupt queue and kick off
  1791. * NAPI for each queue that has generated an entry.
  1792. */
  1793. static unsigned int process_intrq(struct adapter *adap)
  1794. {
  1795. unsigned int credits;
  1796. const struct rsp_ctrl *rc;
  1797. struct sge_rspq *q = &adap->sge.intrq;
  1798. u32 val;
  1799. spin_lock(&adap->sge.intrq_lock);
  1800. for (credits = 0; ; credits++) {
  1801. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1802. if (!is_new_response(rc, q))
  1803. break;
  1804. rmb();
  1805. if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
  1806. unsigned int qid = ntohl(rc->pldbuflen_qid);
  1807. qid -= adap->sge.ingr_start;
  1808. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  1809. }
  1810. rspq_next(q);
  1811. }
  1812. val = CIDXINC(credits) | SEINTARM(q->intr_params);
  1813. if (is_t4(adap->params.chip)) {
  1814. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS),
  1815. val | INGRESSQID(q->cntxt_id));
  1816. } else {
  1817. writel(val, adap->bar2 + q->udb + SGE_UDB_GTS);
  1818. wmb();
  1819. }
  1820. spin_unlock(&adap->sge.intrq_lock);
  1821. return credits;
  1822. }
  1823. /*
  1824. * The MSI interrupt handler, which handles data events from SGE response queues
  1825. * as well as error and other async events as they all use the same MSI vector.
  1826. */
  1827. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  1828. {
  1829. struct adapter *adap = cookie;
  1830. t4_slow_intr_handler(adap);
  1831. process_intrq(adap);
  1832. return IRQ_HANDLED;
  1833. }
  1834. /*
  1835. * Interrupt handler for legacy INTx interrupts.
  1836. * Handles data events from SGE response queues as well as error and other
  1837. * async events as they all use the same interrupt line.
  1838. */
  1839. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  1840. {
  1841. struct adapter *adap = cookie;
  1842. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
  1843. if (t4_slow_intr_handler(adap) | process_intrq(adap))
  1844. return IRQ_HANDLED;
  1845. return IRQ_NONE; /* probably shared interrupt */
  1846. }
  1847. /**
  1848. * t4_intr_handler - select the top-level interrupt handler
  1849. * @adap: the adapter
  1850. *
  1851. * Selects the top-level interrupt handler based on the type of interrupts
  1852. * (MSI-X, MSI, or INTx).
  1853. */
  1854. irq_handler_t t4_intr_handler(struct adapter *adap)
  1855. {
  1856. if (adap->flags & USING_MSIX)
  1857. return t4_sge_intr_msix;
  1858. if (adap->flags & USING_MSI)
  1859. return t4_intr_msi;
  1860. return t4_intr_intx;
  1861. }
  1862. static void sge_rx_timer_cb(unsigned long data)
  1863. {
  1864. unsigned long m;
  1865. unsigned int i, idma_same_state_cnt[2];
  1866. struct adapter *adap = (struct adapter *)data;
  1867. struct sge *s = &adap->sge;
  1868. for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
  1869. for (m = s->starving_fl[i]; m; m &= m - 1) {
  1870. struct sge_eth_rxq *rxq;
  1871. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  1872. struct sge_fl *fl = s->egr_map[id];
  1873. clear_bit(id, s->starving_fl);
  1874. smp_mb__after_atomic();
  1875. if (fl_starving(fl)) {
  1876. rxq = container_of(fl, struct sge_eth_rxq, fl);
  1877. if (napi_reschedule(&rxq->rspq.napi))
  1878. fl->starving++;
  1879. else
  1880. set_bit(id, s->starving_fl);
  1881. }
  1882. }
  1883. t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
  1884. idma_same_state_cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
  1885. idma_same_state_cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1886. for (i = 0; i < 2; i++) {
  1887. u32 debug0, debug11;
  1888. /* If the Ingress DMA Same State Counter ("timer") is less
  1889. * than 1s, then we can reset our synthesized Stall Timer and
  1890. * continue. If we have previously emitted warnings about a
  1891. * potential stalled Ingress Queue, issue a note indicating
  1892. * that the Ingress Queue has resumed forward progress.
  1893. */
  1894. if (idma_same_state_cnt[i] < s->idma_1s_thresh) {
  1895. if (s->idma_stalled[i] >= SGE_IDMA_WARN_THRESH)
  1896. CH_WARN(adap, "SGE idma%d, queue%u,resumed after %d sec\n",
  1897. i, s->idma_qid[i],
  1898. s->idma_stalled[i]/HZ);
  1899. s->idma_stalled[i] = 0;
  1900. continue;
  1901. }
  1902. /* Synthesize an SGE Ingress DMA Same State Timer in the Hz
  1903. * domain. The first time we get here it'll be because we
  1904. * passed the 1s Threshold; each additional time it'll be
  1905. * because the RX Timer Callback is being fired on its regular
  1906. * schedule.
  1907. *
  1908. * If the stall is below our Potential Hung Ingress Queue
  1909. * Warning Threshold, continue.
  1910. */
  1911. if (s->idma_stalled[i] == 0)
  1912. s->idma_stalled[i] = HZ;
  1913. else
  1914. s->idma_stalled[i] += RX_QCHECK_PERIOD;
  1915. if (s->idma_stalled[i] < SGE_IDMA_WARN_THRESH)
  1916. continue;
  1917. /* We'll issue a warning every SGE_IDMA_WARN_REPEAT Hz */
  1918. if (((s->idma_stalled[i] - HZ) % SGE_IDMA_WARN_REPEAT) != 0)
  1919. continue;
  1920. /* Read and save the SGE IDMA State and Queue ID information.
  1921. * We do this every time in case it changes across time ...
  1922. */
  1923. t4_write_reg(adap, SGE_DEBUG_INDEX, 0);
  1924. debug0 = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1925. s->idma_state[i] = (debug0 >> (i * 9)) & 0x3f;
  1926. t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
  1927. debug11 = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1928. s->idma_qid[i] = (debug11 >> (i * 16)) & 0xffff;
  1929. CH_WARN(adap, "SGE idma%u, queue%u, maybe stuck state%u %dsecs (debug0=%#x, debug11=%#x)\n",
  1930. i, s->idma_qid[i], s->idma_state[i],
  1931. s->idma_stalled[i]/HZ, debug0, debug11);
  1932. t4_sge_decode_idma_state(adap, s->idma_state[i]);
  1933. }
  1934. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  1935. }
  1936. static void sge_tx_timer_cb(unsigned long data)
  1937. {
  1938. unsigned long m;
  1939. unsigned int i, budget;
  1940. struct adapter *adap = (struct adapter *)data;
  1941. struct sge *s = &adap->sge;
  1942. for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
  1943. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  1944. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  1945. struct sge_ofld_txq *txq = s->egr_map[id];
  1946. clear_bit(id, s->txq_maperr);
  1947. tasklet_schedule(&txq->qresume_tsk);
  1948. }
  1949. budget = MAX_TIMER_TX_RECLAIM;
  1950. i = s->ethtxq_rover;
  1951. do {
  1952. struct sge_eth_txq *q = &s->ethtxq[i];
  1953. if (q->q.in_use &&
  1954. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  1955. __netif_tx_trylock(q->txq)) {
  1956. int avail = reclaimable(&q->q);
  1957. if (avail) {
  1958. if (avail > budget)
  1959. avail = budget;
  1960. free_tx_desc(adap, &q->q, avail, true);
  1961. q->q.in_use -= avail;
  1962. budget -= avail;
  1963. }
  1964. __netif_tx_unlock(q->txq);
  1965. }
  1966. if (++i >= s->ethqsets)
  1967. i = 0;
  1968. } while (budget && i != s->ethtxq_rover);
  1969. s->ethtxq_rover = i;
  1970. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  1971. }
  1972. /**
  1973. * udb_address - return the BAR2 User Doorbell address for a Queue
  1974. * @adap: the adapter
  1975. * @cntxt_id: the Queue Context ID
  1976. * @qpp: Queues Per Page (for all PFs)
  1977. *
  1978. * Returns the BAR2 address of the user Doorbell associated with the
  1979. * indicated Queue Context ID. Note that this is only applicable
  1980. * for T5 and later.
  1981. */
  1982. static u64 udb_address(struct adapter *adap, unsigned int cntxt_id,
  1983. unsigned int qpp)
  1984. {
  1985. u64 udb;
  1986. unsigned int s_qpp;
  1987. unsigned short udb_density;
  1988. unsigned long qpshift;
  1989. int page;
  1990. BUG_ON(is_t4(adap->params.chip));
  1991. s_qpp = (QUEUESPERPAGEPF0 +
  1992. (QUEUESPERPAGEPF1 - QUEUESPERPAGEPF0) * adap->fn);
  1993. udb_density = 1 << ((qpp >> s_qpp) & QUEUESPERPAGEPF0_MASK);
  1994. qpshift = PAGE_SHIFT - ilog2(udb_density);
  1995. udb = (u64)cntxt_id << qpshift;
  1996. udb &= PAGE_MASK;
  1997. page = udb / PAGE_SIZE;
  1998. udb += (cntxt_id - (page * udb_density)) * SGE_UDB_SIZE;
  1999. return udb;
  2000. }
  2001. static u64 udb_address_eq(struct adapter *adap, unsigned int cntxt_id)
  2002. {
  2003. return udb_address(adap, cntxt_id,
  2004. t4_read_reg(adap, SGE_EGRESS_QUEUES_PER_PAGE_PF));
  2005. }
  2006. static u64 udb_address_iq(struct adapter *adap, unsigned int cntxt_id)
  2007. {
  2008. return udb_address(adap, cntxt_id,
  2009. t4_read_reg(adap, SGE_INGRESS_QUEUES_PER_PAGE_PF));
  2010. }
  2011. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  2012. struct net_device *dev, int intr_idx,
  2013. struct sge_fl *fl, rspq_handler_t hnd)
  2014. {
  2015. int ret, flsz = 0;
  2016. struct fw_iq_cmd c;
  2017. struct sge *s = &adap->sge;
  2018. struct port_info *pi = netdev_priv(dev);
  2019. /* Size needs to be multiple of 16, including status entry. */
  2020. iq->size = roundup(iq->size, 16);
  2021. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  2022. &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
  2023. if (!iq->desc)
  2024. return -ENOMEM;
  2025. memset(&c, 0, sizeof(c));
  2026. c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
  2027. FW_CMD_WRITE | FW_CMD_EXEC |
  2028. FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
  2029. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
  2030. FW_LEN16(c));
  2031. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
  2032. FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
  2033. FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
  2034. FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
  2035. -intr_idx - 1));
  2036. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
  2037. FW_IQ_CMD_IQGTSMODE |
  2038. FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
  2039. FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
  2040. c.iqsize = htons(iq->size);
  2041. c.iqaddr = cpu_to_be64(iq->phys_addr);
  2042. if (fl) {
  2043. fl->size = roundup(fl->size, 8);
  2044. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  2045. sizeof(struct rx_sw_desc), &fl->addr,
  2046. &fl->sdesc, s->stat_len, NUMA_NO_NODE);
  2047. if (!fl->desc)
  2048. goto fl_nomem;
  2049. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  2050. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN(1) |
  2051. FW_IQ_CMD_FL0FETCHRO(1) |
  2052. FW_IQ_CMD_FL0DATARO(1) |
  2053. FW_IQ_CMD_FL0PADEN(1));
  2054. c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
  2055. FW_IQ_CMD_FL0FBMAX(3));
  2056. c.fl0size = htons(flsz);
  2057. c.fl0addr = cpu_to_be64(fl->addr);
  2058. }
  2059. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2060. if (ret)
  2061. goto err;
  2062. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  2063. iq->cur_desc = iq->desc;
  2064. iq->cidx = 0;
  2065. iq->gen = 1;
  2066. iq->next_intr_params = iq->intr_params;
  2067. iq->cntxt_id = ntohs(c.iqid);
  2068. iq->abs_id = ntohs(c.physiqid);
  2069. if (!is_t4(adap->params.chip))
  2070. iq->udb = udb_address_iq(adap, iq->cntxt_id);
  2071. iq->size--; /* subtract status entry */
  2072. iq->netdev = dev;
  2073. iq->handler = hnd;
  2074. /* set offset to -1 to distinguish ingress queues without FL */
  2075. iq->offset = fl ? 0 : -1;
  2076. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  2077. if (fl) {
  2078. fl->cntxt_id = ntohs(c.fl0id);
  2079. fl->avail = fl->pend_cred = 0;
  2080. fl->pidx = fl->cidx = 0;
  2081. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  2082. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  2083. /* Note, we must initialize the Free List User Doorbell
  2084. * address before refilling the Free List!
  2085. */
  2086. if (!is_t4(adap->params.chip))
  2087. fl->udb = udb_address_eq(adap, fl->cntxt_id);
  2088. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  2089. }
  2090. return 0;
  2091. fl_nomem:
  2092. ret = -ENOMEM;
  2093. err:
  2094. if (iq->desc) {
  2095. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  2096. iq->desc, iq->phys_addr);
  2097. iq->desc = NULL;
  2098. }
  2099. if (fl && fl->desc) {
  2100. kfree(fl->sdesc);
  2101. fl->sdesc = NULL;
  2102. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  2103. fl->desc, fl->addr);
  2104. fl->desc = NULL;
  2105. }
  2106. return ret;
  2107. }
  2108. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  2109. {
  2110. q->cntxt_id = id;
  2111. if (!is_t4(adap->params.chip))
  2112. q->udb = udb_address_eq(adap, q->cntxt_id);
  2113. q->in_use = 0;
  2114. q->cidx = q->pidx = 0;
  2115. q->stops = q->restarts = 0;
  2116. q->stat = (void *)&q->desc[q->size];
  2117. spin_lock_init(&q->db_lock);
  2118. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  2119. }
  2120. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  2121. struct net_device *dev, struct netdev_queue *netdevq,
  2122. unsigned int iqid)
  2123. {
  2124. int ret, nentries;
  2125. struct fw_eq_eth_cmd c;
  2126. struct sge *s = &adap->sge;
  2127. struct port_info *pi = netdev_priv(dev);
  2128. /* Add status entries */
  2129. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2130. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2131. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2132. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2133. netdev_queue_numa_node_read(netdevq));
  2134. if (!txq->q.desc)
  2135. return -ENOMEM;
  2136. memset(&c, 0, sizeof(c));
  2137. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
  2138. FW_CMD_WRITE | FW_CMD_EXEC |
  2139. FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
  2140. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
  2141. FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
  2142. c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE |
  2143. FW_EQ_ETH_CMD_VIID(pi->viid));
  2144. c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
  2145. FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
  2146. FW_EQ_ETH_CMD_FETCHRO(1) |
  2147. FW_EQ_ETH_CMD_IQID(iqid));
  2148. c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
  2149. FW_EQ_ETH_CMD_FBMAX(3) |
  2150. FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
  2151. FW_EQ_ETH_CMD_EQSIZE(nentries));
  2152. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2153. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2154. if (ret) {
  2155. kfree(txq->q.sdesc);
  2156. txq->q.sdesc = NULL;
  2157. dma_free_coherent(adap->pdev_dev,
  2158. nentries * sizeof(struct tx_desc),
  2159. txq->q.desc, txq->q.phys_addr);
  2160. txq->q.desc = NULL;
  2161. return ret;
  2162. }
  2163. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  2164. txq->txq = netdevq;
  2165. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  2166. txq->mapping_err = 0;
  2167. return 0;
  2168. }
  2169. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  2170. struct net_device *dev, unsigned int iqid,
  2171. unsigned int cmplqid)
  2172. {
  2173. int ret, nentries;
  2174. struct fw_eq_ctrl_cmd c;
  2175. struct sge *s = &adap->sge;
  2176. struct port_info *pi = netdev_priv(dev);
  2177. /* Add status entries */
  2178. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2179. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  2180. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  2181. NULL, 0, NUMA_NO_NODE);
  2182. if (!txq->q.desc)
  2183. return -ENOMEM;
  2184. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
  2185. FW_CMD_WRITE | FW_CMD_EXEC |
  2186. FW_EQ_CTRL_CMD_PFN(adap->fn) |
  2187. FW_EQ_CTRL_CMD_VFN(0));
  2188. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
  2189. FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
  2190. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
  2191. c.physeqid_pkd = htonl(0);
  2192. c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
  2193. FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
  2194. FW_EQ_CTRL_CMD_FETCHRO |
  2195. FW_EQ_CTRL_CMD_IQID(iqid));
  2196. c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
  2197. FW_EQ_CTRL_CMD_FBMAX(3) |
  2198. FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
  2199. FW_EQ_CTRL_CMD_EQSIZE(nentries));
  2200. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2201. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2202. if (ret) {
  2203. dma_free_coherent(adap->pdev_dev,
  2204. nentries * sizeof(struct tx_desc),
  2205. txq->q.desc, txq->q.phys_addr);
  2206. txq->q.desc = NULL;
  2207. return ret;
  2208. }
  2209. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
  2210. txq->adap = adap;
  2211. skb_queue_head_init(&txq->sendq);
  2212. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  2213. txq->full = 0;
  2214. return 0;
  2215. }
  2216. int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
  2217. struct net_device *dev, unsigned int iqid)
  2218. {
  2219. int ret, nentries;
  2220. struct fw_eq_ofld_cmd c;
  2221. struct sge *s = &adap->sge;
  2222. struct port_info *pi = netdev_priv(dev);
  2223. /* Add status entries */
  2224. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2225. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2226. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2227. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2228. NUMA_NO_NODE);
  2229. if (!txq->q.desc)
  2230. return -ENOMEM;
  2231. memset(&c, 0, sizeof(c));
  2232. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
  2233. FW_CMD_WRITE | FW_CMD_EXEC |
  2234. FW_EQ_OFLD_CMD_PFN(adap->fn) |
  2235. FW_EQ_OFLD_CMD_VFN(0));
  2236. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
  2237. FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
  2238. c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
  2239. FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
  2240. FW_EQ_OFLD_CMD_FETCHRO(1) |
  2241. FW_EQ_OFLD_CMD_IQID(iqid));
  2242. c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
  2243. FW_EQ_OFLD_CMD_FBMAX(3) |
  2244. FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
  2245. FW_EQ_OFLD_CMD_EQSIZE(nentries));
  2246. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2247. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2248. if (ret) {
  2249. kfree(txq->q.sdesc);
  2250. txq->q.sdesc = NULL;
  2251. dma_free_coherent(adap->pdev_dev,
  2252. nentries * sizeof(struct tx_desc),
  2253. txq->q.desc, txq->q.phys_addr);
  2254. txq->q.desc = NULL;
  2255. return ret;
  2256. }
  2257. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  2258. txq->adap = adap;
  2259. skb_queue_head_init(&txq->sendq);
  2260. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  2261. txq->full = 0;
  2262. txq->mapping_err = 0;
  2263. return 0;
  2264. }
  2265. static void free_txq(struct adapter *adap, struct sge_txq *q)
  2266. {
  2267. struct sge *s = &adap->sge;
  2268. dma_free_coherent(adap->pdev_dev,
  2269. q->size * sizeof(struct tx_desc) + s->stat_len,
  2270. q->desc, q->phys_addr);
  2271. q->cntxt_id = 0;
  2272. q->sdesc = NULL;
  2273. q->desc = NULL;
  2274. }
  2275. static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  2276. struct sge_fl *fl)
  2277. {
  2278. struct sge *s = &adap->sge;
  2279. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  2280. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  2281. t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
  2282. rq->cntxt_id, fl_id, 0xffff);
  2283. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  2284. rq->desc, rq->phys_addr);
  2285. netif_napi_del(&rq->napi);
  2286. rq->netdev = NULL;
  2287. rq->cntxt_id = rq->abs_id = 0;
  2288. rq->desc = NULL;
  2289. if (fl) {
  2290. free_rx_bufs(adap, fl, fl->avail);
  2291. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  2292. fl->desc, fl->addr);
  2293. kfree(fl->sdesc);
  2294. fl->sdesc = NULL;
  2295. fl->cntxt_id = 0;
  2296. fl->desc = NULL;
  2297. }
  2298. }
  2299. /**
  2300. * t4_free_ofld_rxqs - free a block of consecutive Rx queues
  2301. * @adap: the adapter
  2302. * @n: number of queues
  2303. * @q: pointer to first queue
  2304. *
  2305. * Release the resources of a consecutive block of offload Rx queues.
  2306. */
  2307. void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
  2308. {
  2309. for ( ; n; n--, q++)
  2310. if (q->rspq.desc)
  2311. free_rspq_fl(adap, &q->rspq,
  2312. q->fl.size ? &q->fl : NULL);
  2313. }
  2314. /**
  2315. * t4_free_sge_resources - free SGE resources
  2316. * @adap: the adapter
  2317. *
  2318. * Frees resources used by the SGE queue sets.
  2319. */
  2320. void t4_free_sge_resources(struct adapter *adap)
  2321. {
  2322. int i;
  2323. struct sge_eth_rxq *eq = adap->sge.ethrxq;
  2324. struct sge_eth_txq *etq = adap->sge.ethtxq;
  2325. /* clean up Ethernet Tx/Rx queues */
  2326. for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
  2327. if (eq->rspq.desc)
  2328. free_rspq_fl(adap, &eq->rspq,
  2329. eq->fl.size ? &eq->fl : NULL);
  2330. if (etq->q.desc) {
  2331. t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
  2332. etq->q.cntxt_id);
  2333. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  2334. kfree(etq->q.sdesc);
  2335. free_txq(adap, &etq->q);
  2336. }
  2337. }
  2338. /* clean up RDMA and iSCSI Rx queues */
  2339. t4_free_ofld_rxqs(adap, adap->sge.ofldqsets, adap->sge.ofldrxq);
  2340. t4_free_ofld_rxqs(adap, adap->sge.rdmaqs, adap->sge.rdmarxq);
  2341. t4_free_ofld_rxqs(adap, adap->sge.rdmaciqs, adap->sge.rdmaciq);
  2342. /* clean up offload Tx queues */
  2343. for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
  2344. struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
  2345. if (q->q.desc) {
  2346. tasklet_kill(&q->qresume_tsk);
  2347. t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
  2348. q->q.cntxt_id);
  2349. free_tx_desc(adap, &q->q, q->q.in_use, false);
  2350. kfree(q->q.sdesc);
  2351. __skb_queue_purge(&q->sendq);
  2352. free_txq(adap, &q->q);
  2353. }
  2354. }
  2355. /* clean up control Tx queues */
  2356. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  2357. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  2358. if (cq->q.desc) {
  2359. tasklet_kill(&cq->qresume_tsk);
  2360. t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
  2361. cq->q.cntxt_id);
  2362. __skb_queue_purge(&cq->sendq);
  2363. free_txq(adap, &cq->q);
  2364. }
  2365. }
  2366. if (adap->sge.fw_evtq.desc)
  2367. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  2368. if (adap->sge.intrq.desc)
  2369. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  2370. /* clear the reverse egress queue map */
  2371. memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
  2372. }
  2373. void t4_sge_start(struct adapter *adap)
  2374. {
  2375. adap->sge.ethtxq_rover = 0;
  2376. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2377. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2378. }
  2379. /**
  2380. * t4_sge_stop - disable SGE operation
  2381. * @adap: the adapter
  2382. *
  2383. * Stop tasklets and timers associated with the DMA engine. Note that
  2384. * this is effective only if measures have been taken to disable any HW
  2385. * events that may restart them.
  2386. */
  2387. void t4_sge_stop(struct adapter *adap)
  2388. {
  2389. int i;
  2390. struct sge *s = &adap->sge;
  2391. if (in_interrupt()) /* actions below require waiting */
  2392. return;
  2393. if (s->rx_timer.function)
  2394. del_timer_sync(&s->rx_timer);
  2395. if (s->tx_timer.function)
  2396. del_timer_sync(&s->tx_timer);
  2397. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
  2398. struct sge_ofld_txq *q = &s->ofldtxq[i];
  2399. if (q->q.desc)
  2400. tasklet_kill(&q->qresume_tsk);
  2401. }
  2402. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  2403. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  2404. if (cq->q.desc)
  2405. tasklet_kill(&cq->qresume_tsk);
  2406. }
  2407. }
  2408. /**
  2409. * t4_sge_init - initialize SGE
  2410. * @adap: the adapter
  2411. *
  2412. * Performs SGE initialization needed every time after a chip reset.
  2413. * We do not initialize any of the queues here, instead the driver
  2414. * top-level must request them individually.
  2415. *
  2416. * Called in two different modes:
  2417. *
  2418. * 1. Perform actual hardware initialization and record hard-coded
  2419. * parameters which were used. This gets used when we're the
  2420. * Master PF and the Firmware Configuration File support didn't
  2421. * work for some reason.
  2422. *
  2423. * 2. We're not the Master PF or initialization was performed with
  2424. * a Firmware Configuration File. In this case we need to grab
  2425. * any of the SGE operating parameters that we need to have in
  2426. * order to do our job and make sure we can live with them ...
  2427. */
  2428. static int t4_sge_init_soft(struct adapter *adap)
  2429. {
  2430. struct sge *s = &adap->sge;
  2431. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  2432. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  2433. u32 ingress_rx_threshold;
  2434. /*
  2435. * Verify that CPL messages are going to the Ingress Queue for
  2436. * process_responses() and that only packet data is going to the
  2437. * Free Lists.
  2438. */
  2439. if ((t4_read_reg(adap, SGE_CONTROL) & RXPKTCPLMODE_MASK) !=
  2440. RXPKTCPLMODE(X_RXPKTCPLMODE_SPLIT)) {
  2441. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  2442. return -EINVAL;
  2443. }
  2444. /*
  2445. * Validate the Host Buffer Register Array indices that we want to
  2446. * use ...
  2447. *
  2448. * XXX Note that we should really read through the Host Buffer Size
  2449. * XXX register array and find the indices of the Buffer Sizes which
  2450. * XXX meet our needs!
  2451. */
  2452. #define READ_FL_BUF(x) \
  2453. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0+(x)*sizeof(u32))
  2454. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  2455. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  2456. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  2457. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  2458. /* We only bother using the Large Page logic if the Large Page Buffer
  2459. * is larger than our Page Size Buffer.
  2460. */
  2461. if (fl_large_pg <= fl_small_pg)
  2462. fl_large_pg = 0;
  2463. #undef READ_FL_BUF
  2464. /* The Page Size Buffer must be exactly equal to our Page Size and the
  2465. * Large Page Size Buffer should be 0 (per above) or a power of 2.
  2466. */
  2467. if (fl_small_pg != PAGE_SIZE ||
  2468. (fl_large_pg & (fl_large_pg-1)) != 0) {
  2469. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  2470. fl_small_pg, fl_large_pg);
  2471. return -EINVAL;
  2472. }
  2473. if (fl_large_pg)
  2474. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  2475. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  2476. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  2477. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  2478. fl_small_mtu, fl_large_mtu);
  2479. return -EINVAL;
  2480. }
  2481. /*
  2482. * Retrieve our RX interrupt holdoff timer values and counter
  2483. * threshold values from the SGE parameters.
  2484. */
  2485. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1);
  2486. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3);
  2487. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5);
  2488. s->timer_val[0] = core_ticks_to_us(adap,
  2489. TIMERVALUE0_GET(timer_value_0_and_1));
  2490. s->timer_val[1] = core_ticks_to_us(adap,
  2491. TIMERVALUE1_GET(timer_value_0_and_1));
  2492. s->timer_val[2] = core_ticks_to_us(adap,
  2493. TIMERVALUE2_GET(timer_value_2_and_3));
  2494. s->timer_val[3] = core_ticks_to_us(adap,
  2495. TIMERVALUE3_GET(timer_value_2_and_3));
  2496. s->timer_val[4] = core_ticks_to_us(adap,
  2497. TIMERVALUE4_GET(timer_value_4_and_5));
  2498. s->timer_val[5] = core_ticks_to_us(adap,
  2499. TIMERVALUE5_GET(timer_value_4_and_5));
  2500. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD);
  2501. s->counter_val[0] = THRESHOLD_0_GET(ingress_rx_threshold);
  2502. s->counter_val[1] = THRESHOLD_1_GET(ingress_rx_threshold);
  2503. s->counter_val[2] = THRESHOLD_2_GET(ingress_rx_threshold);
  2504. s->counter_val[3] = THRESHOLD_3_GET(ingress_rx_threshold);
  2505. return 0;
  2506. }
  2507. static int t4_sge_init_hard(struct adapter *adap)
  2508. {
  2509. struct sge *s = &adap->sge;
  2510. /*
  2511. * Set up our basic SGE mode to deliver CPL messages to our Ingress
  2512. * Queue and Packet Date to the Free List.
  2513. */
  2514. t4_set_reg_field(adap, SGE_CONTROL, RXPKTCPLMODE_MASK,
  2515. RXPKTCPLMODE_MASK);
  2516. /*
  2517. * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
  2518. * and generate an interrupt when this occurs so we can recover.
  2519. */
  2520. if (is_t4(adap->params.chip)) {
  2521. t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
  2522. V_HP_INT_THRESH(M_HP_INT_THRESH) |
  2523. V_LP_INT_THRESH(M_LP_INT_THRESH),
  2524. V_HP_INT_THRESH(dbfifo_int_thresh) |
  2525. V_LP_INT_THRESH(dbfifo_int_thresh));
  2526. } else {
  2527. t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
  2528. V_LP_INT_THRESH_T5(M_LP_INT_THRESH_T5),
  2529. V_LP_INT_THRESH_T5(dbfifo_int_thresh));
  2530. t4_set_reg_field(adap, SGE_DBFIFO_STATUS2,
  2531. V_HP_INT_THRESH_T5(M_HP_INT_THRESH_T5),
  2532. V_HP_INT_THRESH_T5(dbfifo_int_thresh));
  2533. }
  2534. t4_set_reg_field(adap, A_SGE_DOORBELL_CONTROL, F_ENABLE_DROP,
  2535. F_ENABLE_DROP);
  2536. /*
  2537. * SGE_FL_BUFFER_SIZE0 (RX_SMALL_PG_BUF) is set up by
  2538. * t4_fixup_host_params().
  2539. */
  2540. s->fl_pg_order = FL_PG_ORDER;
  2541. if (s->fl_pg_order)
  2542. t4_write_reg(adap,
  2543. SGE_FL_BUFFER_SIZE0+RX_LARGE_PG_BUF*sizeof(u32),
  2544. PAGE_SIZE << FL_PG_ORDER);
  2545. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_SMALL_MTU_BUF*sizeof(u32),
  2546. FL_MTU_SMALL_BUFSIZE(adap));
  2547. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_LARGE_MTU_BUF*sizeof(u32),
  2548. FL_MTU_LARGE_BUFSIZE(adap));
  2549. /*
  2550. * Note that the SGE Ingress Packet Count Interrupt Threshold and
  2551. * Timer Holdoff values must be supplied by our caller.
  2552. */
  2553. t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
  2554. THRESHOLD_0(s->counter_val[0]) |
  2555. THRESHOLD_1(s->counter_val[1]) |
  2556. THRESHOLD_2(s->counter_val[2]) |
  2557. THRESHOLD_3(s->counter_val[3]));
  2558. t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
  2559. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
  2560. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
  2561. t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
  2562. TIMERVALUE2(us_to_core_ticks(adap, s->timer_val[2])) |
  2563. TIMERVALUE3(us_to_core_ticks(adap, s->timer_val[3])));
  2564. t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
  2565. TIMERVALUE4(us_to_core_ticks(adap, s->timer_val[4])) |
  2566. TIMERVALUE5(us_to_core_ticks(adap, s->timer_val[5])));
  2567. return 0;
  2568. }
  2569. int t4_sge_init(struct adapter *adap)
  2570. {
  2571. struct sge *s = &adap->sge;
  2572. u32 sge_control, sge_conm_ctrl;
  2573. int ret, egress_threshold;
  2574. /*
  2575. * Ingress Padding Boundary and Egress Status Page Size are set up by
  2576. * t4_fixup_host_params().
  2577. */
  2578. sge_control = t4_read_reg(adap, SGE_CONTROL);
  2579. s->pktshift = PKTSHIFT_GET(sge_control);
  2580. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_MASK) ? 128 : 64;
  2581. s->fl_align = 1 << (INGPADBOUNDARY_GET(sge_control) +
  2582. X_INGPADBOUNDARY_SHIFT);
  2583. if (adap->flags & USING_SOFT_PARAMS)
  2584. ret = t4_sge_init_soft(adap);
  2585. else
  2586. ret = t4_sge_init_hard(adap);
  2587. if (ret < 0)
  2588. return ret;
  2589. /*
  2590. * A FL with <= fl_starve_thres buffers is starving and a periodic
  2591. * timer will attempt to refill it. This needs to be larger than the
  2592. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  2593. * stuck waiting for new packets while the SGE is waiting for us to
  2594. * give it more Free List entries. (Note that the SGE's Egress
  2595. * Congestion Threshold is in units of 2 Free List pointers.) For T4,
  2596. * there was only a single field to control this. For T5 there's the
  2597. * original field which now only applies to Unpacked Mode Free List
  2598. * buffers and a new field which only applies to Packed Mode Free List
  2599. * buffers.
  2600. */
  2601. sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL);
  2602. if (is_t4(adap->params.chip))
  2603. egress_threshold = EGRTHRESHOLD_GET(sge_conm_ctrl);
  2604. else
  2605. egress_threshold = EGRTHRESHOLDPACKING_GET(sge_conm_ctrl);
  2606. s->fl_starve_thres = 2*egress_threshold + 1;
  2607. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
  2608. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
  2609. s->idma_1s_thresh = core_ticks_per_usec(adap) * 1000000; /* 1 s */
  2610. s->idma_stalled[0] = 0;
  2611. s->idma_stalled[1] = 0;
  2612. spin_lock_init(&s->intrq_lock);
  2613. return 0;
  2614. }