sge.c 87 KB

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