sge.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363
  1. /*
  2. * Copyright (c) 2005-2008 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/skbuff.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/ip.h>
  37. #include <linux/tcp.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/slab.h>
  40. #include <linux/prefetch.h>
  41. #include <net/arp.h>
  42. #include "common.h"
  43. #include "regs.h"
  44. #include "sge_defs.h"
  45. #include "t3_cpl.h"
  46. #include "firmware_exports.h"
  47. #include "cxgb3_offload.h"
  48. #define USE_GTS 0
  49. #define SGE_RX_SM_BUF_SIZE 1536
  50. #define SGE_RX_COPY_THRES 256
  51. #define SGE_RX_PULL_LEN 128
  52. #define SGE_PG_RSVD SMP_CACHE_BYTES
  53. /*
  54. * Page chunk size for FL0 buffers if FL0 is to be populated with page chunks.
  55. * It must be a divisor of PAGE_SIZE. If set to 0 FL0 will use sk_buffs
  56. * directly.
  57. */
  58. #define FL0_PG_CHUNK_SIZE 2048
  59. #define FL0_PG_ORDER 0
  60. #define FL0_PG_ALLOC_SIZE (PAGE_SIZE << FL0_PG_ORDER)
  61. #define FL1_PG_CHUNK_SIZE (PAGE_SIZE > 8192 ? 16384 : 8192)
  62. #define FL1_PG_ORDER (PAGE_SIZE > 8192 ? 0 : 1)
  63. #define FL1_PG_ALLOC_SIZE (PAGE_SIZE << FL1_PG_ORDER)
  64. #define SGE_RX_DROP_THRES 16
  65. #define RX_RECLAIM_PERIOD (HZ/4)
  66. /*
  67. * Max number of Rx buffers we replenish at a time.
  68. */
  69. #define MAX_RX_REFILL 16U
  70. /*
  71. * Period of the Tx buffer reclaim timer. This timer does not need to run
  72. * frequently as Tx buffers are usually reclaimed by new Tx packets.
  73. */
  74. #define TX_RECLAIM_PERIOD (HZ / 4)
  75. #define TX_RECLAIM_TIMER_CHUNK 64U
  76. #define TX_RECLAIM_CHUNK 16U
  77. /* WR size in bytes */
  78. #define WR_LEN (WR_FLITS * 8)
  79. /*
  80. * Types of Tx queues in each queue set. Order here matters, do not change.
  81. */
  82. enum { TXQ_ETH, TXQ_OFLD, TXQ_CTRL };
  83. /* Values for sge_txq.flags */
  84. enum {
  85. TXQ_RUNNING = 1 << 0, /* fetch engine is running */
  86. TXQ_LAST_PKT_DB = 1 << 1, /* last packet rang the doorbell */
  87. };
  88. struct tx_desc {
  89. __be64 flit[TX_DESC_FLITS];
  90. };
  91. struct rx_desc {
  92. __be32 addr_lo;
  93. __be32 len_gen;
  94. __be32 gen2;
  95. __be32 addr_hi;
  96. };
  97. struct tx_sw_desc { /* SW state per Tx descriptor */
  98. struct sk_buff *skb;
  99. u8 eop; /* set if last descriptor for packet */
  100. u8 addr_idx; /* buffer index of first SGL entry in descriptor */
  101. u8 fragidx; /* first page fragment associated with descriptor */
  102. s8 sflit; /* start flit of first SGL entry in descriptor */
  103. };
  104. struct rx_sw_desc { /* SW state per Rx descriptor */
  105. union {
  106. struct sk_buff *skb;
  107. struct fl_pg_chunk pg_chunk;
  108. };
  109. DEFINE_DMA_UNMAP_ADDR(dma_addr);
  110. };
  111. struct rsp_desc { /* response queue descriptor */
  112. struct rss_header rss_hdr;
  113. __be32 flags;
  114. __be32 len_cq;
  115. u8 imm_data[47];
  116. u8 intr_gen;
  117. };
  118. /*
  119. * Holds unmapping information for Tx packets that need deferred unmapping.
  120. * This structure lives at skb->head and must be allocated by callers.
  121. */
  122. struct deferred_unmap_info {
  123. struct pci_dev *pdev;
  124. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  125. };
  126. /*
  127. * Maps a number of flits to the number of Tx descriptors that can hold them.
  128. * The formula is
  129. *
  130. * desc = 1 + (flits - 2) / (WR_FLITS - 1).
  131. *
  132. * HW allows up to 4 descriptors to be combined into a WR.
  133. */
  134. static u8 flit_desc_map[] = {
  135. 0,
  136. #if SGE_NUM_GENBITS == 1
  137. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  138. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  139. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  140. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  141. #elif SGE_NUM_GENBITS == 2
  142. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  143. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  144. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  145. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  146. #else
  147. # error "SGE_NUM_GENBITS must be 1 or 2"
  148. #endif
  149. };
  150. static inline struct sge_qset *fl_to_qset(const struct sge_fl *q, int qidx)
  151. {
  152. return container_of(q, struct sge_qset, fl[qidx]);
  153. }
  154. static inline struct sge_qset *rspq_to_qset(const struct sge_rspq *q)
  155. {
  156. return container_of(q, struct sge_qset, rspq);
  157. }
  158. static inline struct sge_qset *txq_to_qset(const struct sge_txq *q, int qidx)
  159. {
  160. return container_of(q, struct sge_qset, txq[qidx]);
  161. }
  162. /**
  163. * refill_rspq - replenish an SGE response queue
  164. * @adapter: the adapter
  165. * @q: the response queue to replenish
  166. * @credits: how many new responses to make available
  167. *
  168. * Replenishes a response queue by making the supplied number of responses
  169. * available to HW.
  170. */
  171. static inline void refill_rspq(struct adapter *adapter,
  172. const struct sge_rspq *q, unsigned int credits)
  173. {
  174. rmb();
  175. t3_write_reg(adapter, A_SG_RSPQ_CREDIT_RETURN,
  176. V_RSPQ(q->cntxt_id) | V_CREDITS(credits));
  177. }
  178. /**
  179. * need_skb_unmap - does the platform need unmapping of sk_buffs?
  180. *
  181. * Returns true if the platform needs sk_buff unmapping. The compiler
  182. * optimizes away unnecessary code if this returns true.
  183. */
  184. static inline int need_skb_unmap(void)
  185. {
  186. #ifdef CONFIG_NEED_DMA_MAP_STATE
  187. return 1;
  188. #else
  189. return 0;
  190. #endif
  191. }
  192. /**
  193. * unmap_skb - unmap a packet main body and its page fragments
  194. * @skb: the packet
  195. * @q: the Tx queue containing Tx descriptors for the packet
  196. * @cidx: index of Tx descriptor
  197. * @pdev: the PCI device
  198. *
  199. * Unmap the main body of an sk_buff and its page fragments, if any.
  200. * Because of the fairly complicated structure of our SGLs and the desire
  201. * to conserve space for metadata, the information necessary to unmap an
  202. * sk_buff is spread across the sk_buff itself (buffer lengths), the HW Tx
  203. * descriptors (the physical addresses of the various data buffers), and
  204. * the SW descriptor state (assorted indices). The send functions
  205. * initialize the indices for the first packet descriptor so we can unmap
  206. * the buffers held in the first Tx descriptor here, and we have enough
  207. * information at this point to set the state for the next Tx descriptor.
  208. *
  209. * Note that it is possible to clean up the first descriptor of a packet
  210. * before the send routines have written the next descriptors, but this
  211. * race does not cause any problem. We just end up writing the unmapping
  212. * info for the descriptor first.
  213. */
  214. static inline void unmap_skb(struct sk_buff *skb, struct sge_txq *q,
  215. unsigned int cidx, struct pci_dev *pdev)
  216. {
  217. const struct sg_ent *sgp;
  218. struct tx_sw_desc *d = &q->sdesc[cidx];
  219. int nfrags, frag_idx, curflit, j = d->addr_idx;
  220. sgp = (struct sg_ent *)&q->desc[cidx].flit[d->sflit];
  221. frag_idx = d->fragidx;
  222. if (frag_idx == 0 && skb_headlen(skb)) {
  223. pci_unmap_single(pdev, be64_to_cpu(sgp->addr[0]),
  224. skb_headlen(skb), PCI_DMA_TODEVICE);
  225. j = 1;
  226. }
  227. curflit = d->sflit + 1 + j;
  228. nfrags = skb_shinfo(skb)->nr_frags;
  229. while (frag_idx < nfrags && curflit < WR_FLITS) {
  230. pci_unmap_page(pdev, be64_to_cpu(sgp->addr[j]),
  231. skb_frag_size(&skb_shinfo(skb)->frags[frag_idx]),
  232. PCI_DMA_TODEVICE);
  233. j ^= 1;
  234. if (j == 0) {
  235. sgp++;
  236. curflit++;
  237. }
  238. curflit++;
  239. frag_idx++;
  240. }
  241. if (frag_idx < nfrags) { /* SGL continues into next Tx descriptor */
  242. d = cidx + 1 == q->size ? q->sdesc : d + 1;
  243. d->fragidx = frag_idx;
  244. d->addr_idx = j;
  245. d->sflit = curflit - WR_FLITS - j; /* sflit can be -1 */
  246. }
  247. }
  248. /**
  249. * free_tx_desc - reclaims Tx descriptors and their buffers
  250. * @adapter: the adapter
  251. * @q: the Tx queue to reclaim descriptors from
  252. * @n: the number of descriptors to reclaim
  253. *
  254. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  255. * Tx buffers. Called with the Tx queue lock held.
  256. */
  257. static void free_tx_desc(struct adapter *adapter, struct sge_txq *q,
  258. unsigned int n)
  259. {
  260. struct tx_sw_desc *d;
  261. struct pci_dev *pdev = adapter->pdev;
  262. unsigned int cidx = q->cidx;
  263. const int need_unmap = need_skb_unmap() &&
  264. q->cntxt_id >= FW_TUNNEL_SGEEC_START;
  265. d = &q->sdesc[cidx];
  266. while (n--) {
  267. if (d->skb) { /* an SGL is present */
  268. if (need_unmap)
  269. unmap_skb(d->skb, q, cidx, pdev);
  270. if (d->eop) {
  271. kfree_skb(d->skb);
  272. d->skb = NULL;
  273. }
  274. }
  275. ++d;
  276. if (++cidx == q->size) {
  277. cidx = 0;
  278. d = q->sdesc;
  279. }
  280. }
  281. q->cidx = cidx;
  282. }
  283. /**
  284. * reclaim_completed_tx - reclaims completed Tx descriptors
  285. * @adapter: the adapter
  286. * @q: the Tx queue to reclaim completed descriptors from
  287. * @chunk: maximum number of descriptors to reclaim
  288. *
  289. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  290. * and frees the associated buffers if possible. Called with the Tx
  291. * queue's lock held.
  292. */
  293. static inline unsigned int reclaim_completed_tx(struct adapter *adapter,
  294. struct sge_txq *q,
  295. unsigned int chunk)
  296. {
  297. unsigned int reclaim = q->processed - q->cleaned;
  298. reclaim = min(chunk, reclaim);
  299. if (reclaim) {
  300. free_tx_desc(adapter, q, reclaim);
  301. q->cleaned += reclaim;
  302. q->in_use -= reclaim;
  303. }
  304. return q->processed - q->cleaned;
  305. }
  306. /**
  307. * should_restart_tx - are there enough resources to restart a Tx queue?
  308. * @q: the Tx queue
  309. *
  310. * Checks if there are enough descriptors to restart a suspended Tx queue.
  311. */
  312. static inline int should_restart_tx(const struct sge_txq *q)
  313. {
  314. unsigned int r = q->processed - q->cleaned;
  315. return q->in_use - r < (q->size >> 1);
  316. }
  317. static void clear_rx_desc(struct pci_dev *pdev, const struct sge_fl *q,
  318. struct rx_sw_desc *d)
  319. {
  320. if (q->use_pages && d->pg_chunk.page) {
  321. (*d->pg_chunk.p_cnt)--;
  322. if (!*d->pg_chunk.p_cnt)
  323. pci_unmap_page(pdev,
  324. d->pg_chunk.mapping,
  325. q->alloc_size, PCI_DMA_FROMDEVICE);
  326. put_page(d->pg_chunk.page);
  327. d->pg_chunk.page = NULL;
  328. } else {
  329. pci_unmap_single(pdev, dma_unmap_addr(d, dma_addr),
  330. q->buf_size, PCI_DMA_FROMDEVICE);
  331. kfree_skb(d->skb);
  332. d->skb = NULL;
  333. }
  334. }
  335. /**
  336. * free_rx_bufs - free the Rx buffers on an SGE free list
  337. * @pdev: the PCI device associated with the adapter
  338. * @rxq: the SGE free list to clean up
  339. *
  340. * Release the buffers on an SGE free-buffer Rx queue. HW fetching from
  341. * this queue should be stopped before calling this function.
  342. */
  343. static void free_rx_bufs(struct pci_dev *pdev, struct sge_fl *q)
  344. {
  345. unsigned int cidx = q->cidx;
  346. while (q->credits--) {
  347. struct rx_sw_desc *d = &q->sdesc[cidx];
  348. clear_rx_desc(pdev, q, d);
  349. if (++cidx == q->size)
  350. cidx = 0;
  351. }
  352. if (q->pg_chunk.page) {
  353. __free_pages(q->pg_chunk.page, q->order);
  354. q->pg_chunk.page = NULL;
  355. }
  356. }
  357. /**
  358. * add_one_rx_buf - add a packet buffer to a free-buffer list
  359. * @va: buffer start VA
  360. * @len: the buffer length
  361. * @d: the HW Rx descriptor to write
  362. * @sd: the SW Rx descriptor to write
  363. * @gen: the generation bit value
  364. * @pdev: the PCI device associated with the adapter
  365. *
  366. * Add a buffer of the given length to the supplied HW and SW Rx
  367. * descriptors.
  368. */
  369. static inline int add_one_rx_buf(void *va, unsigned int len,
  370. struct rx_desc *d, struct rx_sw_desc *sd,
  371. unsigned int gen, struct pci_dev *pdev)
  372. {
  373. dma_addr_t mapping;
  374. mapping = pci_map_single(pdev, va, len, PCI_DMA_FROMDEVICE);
  375. if (unlikely(pci_dma_mapping_error(pdev, mapping)))
  376. return -ENOMEM;
  377. dma_unmap_addr_set(sd, dma_addr, mapping);
  378. d->addr_lo = cpu_to_be32(mapping);
  379. d->addr_hi = cpu_to_be32((u64) mapping >> 32);
  380. wmb();
  381. d->len_gen = cpu_to_be32(V_FLD_GEN1(gen));
  382. d->gen2 = cpu_to_be32(V_FLD_GEN2(gen));
  383. return 0;
  384. }
  385. static inline int add_one_rx_chunk(dma_addr_t mapping, struct rx_desc *d,
  386. unsigned int gen)
  387. {
  388. d->addr_lo = cpu_to_be32(mapping);
  389. d->addr_hi = cpu_to_be32((u64) mapping >> 32);
  390. wmb();
  391. d->len_gen = cpu_to_be32(V_FLD_GEN1(gen));
  392. d->gen2 = cpu_to_be32(V_FLD_GEN2(gen));
  393. return 0;
  394. }
  395. static int alloc_pg_chunk(struct adapter *adapter, struct sge_fl *q,
  396. struct rx_sw_desc *sd, gfp_t gfp,
  397. unsigned int order)
  398. {
  399. if (!q->pg_chunk.page) {
  400. dma_addr_t mapping;
  401. q->pg_chunk.page = alloc_pages(gfp, order);
  402. if (unlikely(!q->pg_chunk.page))
  403. return -ENOMEM;
  404. q->pg_chunk.va = page_address(q->pg_chunk.page);
  405. q->pg_chunk.p_cnt = q->pg_chunk.va + (PAGE_SIZE << order) -
  406. SGE_PG_RSVD;
  407. q->pg_chunk.offset = 0;
  408. mapping = pci_map_page(adapter->pdev, q->pg_chunk.page,
  409. 0, q->alloc_size, PCI_DMA_FROMDEVICE);
  410. if (unlikely(pci_dma_mapping_error(adapter->pdev, mapping))) {
  411. __free_pages(q->pg_chunk.page, order);
  412. q->pg_chunk.page = NULL;
  413. return -EIO;
  414. }
  415. q->pg_chunk.mapping = mapping;
  416. }
  417. sd->pg_chunk = q->pg_chunk;
  418. prefetch(sd->pg_chunk.p_cnt);
  419. q->pg_chunk.offset += q->buf_size;
  420. if (q->pg_chunk.offset == (PAGE_SIZE << order))
  421. q->pg_chunk.page = NULL;
  422. else {
  423. q->pg_chunk.va += q->buf_size;
  424. get_page(q->pg_chunk.page);
  425. }
  426. if (sd->pg_chunk.offset == 0)
  427. *sd->pg_chunk.p_cnt = 1;
  428. else
  429. *sd->pg_chunk.p_cnt += 1;
  430. return 0;
  431. }
  432. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  433. {
  434. if (q->pend_cred >= q->credits / 4) {
  435. q->pend_cred = 0;
  436. wmb();
  437. t3_write_reg(adap, A_SG_KDOORBELL, V_EGRCNTX(q->cntxt_id));
  438. }
  439. }
  440. /**
  441. * refill_fl - refill an SGE free-buffer list
  442. * @adapter: the adapter
  443. * @q: the free-list to refill
  444. * @n: the number of new buffers to allocate
  445. * @gfp: the gfp flags for allocating new buffers
  446. *
  447. * (Re)populate an SGE free-buffer list with up to @n new packet buffers,
  448. * allocated with the supplied gfp flags. The caller must assure that
  449. * @n does not exceed the queue's capacity.
  450. */
  451. static int refill_fl(struct adapter *adap, struct sge_fl *q, int n, gfp_t gfp)
  452. {
  453. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  454. struct rx_desc *d = &q->desc[q->pidx];
  455. unsigned int count = 0;
  456. while (n--) {
  457. dma_addr_t mapping;
  458. int err;
  459. if (q->use_pages) {
  460. if (unlikely(alloc_pg_chunk(adap, q, sd, gfp,
  461. q->order))) {
  462. nomem: q->alloc_failed++;
  463. break;
  464. }
  465. mapping = sd->pg_chunk.mapping + sd->pg_chunk.offset;
  466. dma_unmap_addr_set(sd, dma_addr, mapping);
  467. add_one_rx_chunk(mapping, d, q->gen);
  468. pci_dma_sync_single_for_device(adap->pdev, mapping,
  469. q->buf_size - SGE_PG_RSVD,
  470. PCI_DMA_FROMDEVICE);
  471. } else {
  472. void *buf_start;
  473. struct sk_buff *skb = alloc_skb(q->buf_size, gfp);
  474. if (!skb)
  475. goto nomem;
  476. sd->skb = skb;
  477. buf_start = skb->data;
  478. err = add_one_rx_buf(buf_start, q->buf_size, d, sd,
  479. q->gen, adap->pdev);
  480. if (unlikely(err)) {
  481. clear_rx_desc(adap->pdev, q, sd);
  482. break;
  483. }
  484. }
  485. d++;
  486. sd++;
  487. if (++q->pidx == q->size) {
  488. q->pidx = 0;
  489. q->gen ^= 1;
  490. sd = q->sdesc;
  491. d = q->desc;
  492. }
  493. count++;
  494. }
  495. q->credits += count;
  496. q->pend_cred += count;
  497. ring_fl_db(adap, q);
  498. return count;
  499. }
  500. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  501. {
  502. refill_fl(adap, fl, min(MAX_RX_REFILL, fl->size - fl->credits),
  503. GFP_ATOMIC | __GFP_COMP);
  504. }
  505. /**
  506. * recycle_rx_buf - recycle a receive buffer
  507. * @adapter: the adapter
  508. * @q: the SGE free list
  509. * @idx: index of buffer to recycle
  510. *
  511. * Recycles the specified buffer on the given free list by adding it at
  512. * the next available slot on the list.
  513. */
  514. static void recycle_rx_buf(struct adapter *adap, struct sge_fl *q,
  515. unsigned int idx)
  516. {
  517. struct rx_desc *from = &q->desc[idx];
  518. struct rx_desc *to = &q->desc[q->pidx];
  519. q->sdesc[q->pidx] = q->sdesc[idx];
  520. to->addr_lo = from->addr_lo; /* already big endian */
  521. to->addr_hi = from->addr_hi; /* likewise */
  522. wmb();
  523. to->len_gen = cpu_to_be32(V_FLD_GEN1(q->gen));
  524. to->gen2 = cpu_to_be32(V_FLD_GEN2(q->gen));
  525. if (++q->pidx == q->size) {
  526. q->pidx = 0;
  527. q->gen ^= 1;
  528. }
  529. q->credits++;
  530. q->pend_cred++;
  531. ring_fl_db(adap, q);
  532. }
  533. /**
  534. * alloc_ring - allocate resources for an SGE descriptor ring
  535. * @pdev: the PCI device
  536. * @nelem: the number of descriptors
  537. * @elem_size: the size of each descriptor
  538. * @sw_size: the size of the SW state associated with each ring element
  539. * @phys: the physical address of the allocated ring
  540. * @metadata: address of the array holding the SW state for the ring
  541. *
  542. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  543. * free buffer lists, or response queues. Each SGE ring requires
  544. * space for its HW descriptors plus, optionally, space for the SW state
  545. * associated with each HW entry (the metadata). The function returns
  546. * three values: the virtual address for the HW ring (the return value
  547. * of the function), the physical address of the HW ring, and the address
  548. * of the SW ring.
  549. */
  550. static void *alloc_ring(struct pci_dev *pdev, size_t nelem, size_t elem_size,
  551. size_t sw_size, dma_addr_t * phys, void *metadata)
  552. {
  553. size_t len = nelem * elem_size;
  554. void *s = NULL;
  555. void *p = dma_alloc_coherent(&pdev->dev, len, phys, GFP_KERNEL);
  556. if (!p)
  557. return NULL;
  558. if (sw_size && metadata) {
  559. s = kcalloc(nelem, sw_size, GFP_KERNEL);
  560. if (!s) {
  561. dma_free_coherent(&pdev->dev, len, p, *phys);
  562. return NULL;
  563. }
  564. *(void **)metadata = s;
  565. }
  566. memset(p, 0, len);
  567. return p;
  568. }
  569. /**
  570. * t3_reset_qset - reset a sge qset
  571. * @q: the queue set
  572. *
  573. * Reset the qset structure.
  574. * the NAPI structure is preserved in the event of
  575. * the qset's reincarnation, for example during EEH recovery.
  576. */
  577. static void t3_reset_qset(struct sge_qset *q)
  578. {
  579. if (q->adap &&
  580. !(q->adap->flags & NAPI_INIT)) {
  581. memset(q, 0, sizeof(*q));
  582. return;
  583. }
  584. q->adap = NULL;
  585. memset(&q->rspq, 0, sizeof(q->rspq));
  586. memset(q->fl, 0, sizeof(struct sge_fl) * SGE_RXQ_PER_SET);
  587. memset(q->txq, 0, sizeof(struct sge_txq) * SGE_TXQ_PER_SET);
  588. q->txq_stopped = 0;
  589. q->tx_reclaim_timer.function = NULL; /* for t3_stop_sge_timers() */
  590. q->rx_reclaim_timer.function = NULL;
  591. q->nomem = 0;
  592. napi_free_frags(&q->napi);
  593. }
  594. /**
  595. * free_qset - free the resources of an SGE queue set
  596. * @adapter: the adapter owning the queue set
  597. * @q: the queue set
  598. *
  599. * Release the HW and SW resources associated with an SGE queue set, such
  600. * as HW contexts, packet buffers, and descriptor rings. Traffic to the
  601. * queue set must be quiesced prior to calling this.
  602. */
  603. static void t3_free_qset(struct adapter *adapter, struct sge_qset *q)
  604. {
  605. int i;
  606. struct pci_dev *pdev = adapter->pdev;
  607. for (i = 0; i < SGE_RXQ_PER_SET; ++i)
  608. if (q->fl[i].desc) {
  609. spin_lock_irq(&adapter->sge.reg_lock);
  610. t3_sge_disable_fl(adapter, q->fl[i].cntxt_id);
  611. spin_unlock_irq(&adapter->sge.reg_lock);
  612. free_rx_bufs(pdev, &q->fl[i]);
  613. kfree(q->fl[i].sdesc);
  614. dma_free_coherent(&pdev->dev,
  615. q->fl[i].size *
  616. sizeof(struct rx_desc), q->fl[i].desc,
  617. q->fl[i].phys_addr);
  618. }
  619. for (i = 0; i < SGE_TXQ_PER_SET; ++i)
  620. if (q->txq[i].desc) {
  621. spin_lock_irq(&adapter->sge.reg_lock);
  622. t3_sge_enable_ecntxt(adapter, q->txq[i].cntxt_id, 0);
  623. spin_unlock_irq(&adapter->sge.reg_lock);
  624. if (q->txq[i].sdesc) {
  625. free_tx_desc(adapter, &q->txq[i],
  626. q->txq[i].in_use);
  627. kfree(q->txq[i].sdesc);
  628. }
  629. dma_free_coherent(&pdev->dev,
  630. q->txq[i].size *
  631. sizeof(struct tx_desc),
  632. q->txq[i].desc, q->txq[i].phys_addr);
  633. __skb_queue_purge(&q->txq[i].sendq);
  634. }
  635. if (q->rspq.desc) {
  636. spin_lock_irq(&adapter->sge.reg_lock);
  637. t3_sge_disable_rspcntxt(adapter, q->rspq.cntxt_id);
  638. spin_unlock_irq(&adapter->sge.reg_lock);
  639. dma_free_coherent(&pdev->dev,
  640. q->rspq.size * sizeof(struct rsp_desc),
  641. q->rspq.desc, q->rspq.phys_addr);
  642. }
  643. t3_reset_qset(q);
  644. }
  645. /**
  646. * init_qset_cntxt - initialize an SGE queue set context info
  647. * @qs: the queue set
  648. * @id: the queue set id
  649. *
  650. * Initializes the TIDs and context ids for the queues of a queue set.
  651. */
  652. static void init_qset_cntxt(struct sge_qset *qs, unsigned int id)
  653. {
  654. qs->rspq.cntxt_id = id;
  655. qs->fl[0].cntxt_id = 2 * id;
  656. qs->fl[1].cntxt_id = 2 * id + 1;
  657. qs->txq[TXQ_ETH].cntxt_id = FW_TUNNEL_SGEEC_START + id;
  658. qs->txq[TXQ_ETH].token = FW_TUNNEL_TID_START + id;
  659. qs->txq[TXQ_OFLD].cntxt_id = FW_OFLD_SGEEC_START + id;
  660. qs->txq[TXQ_CTRL].cntxt_id = FW_CTRL_SGEEC_START + id;
  661. qs->txq[TXQ_CTRL].token = FW_CTRL_TID_START + id;
  662. }
  663. /**
  664. * sgl_len - calculates the size of an SGL of the given capacity
  665. * @n: the number of SGL entries
  666. *
  667. * Calculates the number of flits needed for a scatter/gather list that
  668. * can hold the given number of entries.
  669. */
  670. static inline unsigned int sgl_len(unsigned int n)
  671. {
  672. /* alternatively: 3 * (n / 2) + 2 * (n & 1) */
  673. return (3 * n) / 2 + (n & 1);
  674. }
  675. /**
  676. * flits_to_desc - returns the num of Tx descriptors for the given flits
  677. * @n: the number of flits
  678. *
  679. * Calculates the number of Tx descriptors needed for the supplied number
  680. * of flits.
  681. */
  682. static inline unsigned int flits_to_desc(unsigned int n)
  683. {
  684. BUG_ON(n >= ARRAY_SIZE(flit_desc_map));
  685. return flit_desc_map[n];
  686. }
  687. /**
  688. * get_packet - return the next ingress packet buffer from a free list
  689. * @adap: the adapter that received the packet
  690. * @fl: the SGE free list holding the packet
  691. * @len: the packet length including any SGE padding
  692. * @drop_thres: # of remaining buffers before we start dropping packets
  693. *
  694. * Get the next packet from a free list and complete setup of the
  695. * sk_buff. If the packet is small we make a copy and recycle the
  696. * original buffer, otherwise we use the original buffer itself. If a
  697. * positive drop threshold is supplied packets are dropped and their
  698. * buffers recycled if (a) the number of remaining buffers is under the
  699. * threshold and the packet is too big to copy, or (b) the packet should
  700. * be copied but there is no memory for the copy.
  701. */
  702. static struct sk_buff *get_packet(struct adapter *adap, struct sge_fl *fl,
  703. unsigned int len, unsigned int drop_thres)
  704. {
  705. struct sk_buff *skb = NULL;
  706. struct rx_sw_desc *sd = &fl->sdesc[fl->cidx];
  707. prefetch(sd->skb->data);
  708. fl->credits--;
  709. if (len <= SGE_RX_COPY_THRES) {
  710. skb = alloc_skb(len, GFP_ATOMIC);
  711. if (likely(skb != NULL)) {
  712. __skb_put(skb, len);
  713. pci_dma_sync_single_for_cpu(adap->pdev,
  714. dma_unmap_addr(sd, dma_addr), len,
  715. PCI_DMA_FROMDEVICE);
  716. memcpy(skb->data, sd->skb->data, len);
  717. pci_dma_sync_single_for_device(adap->pdev,
  718. dma_unmap_addr(sd, dma_addr), len,
  719. PCI_DMA_FROMDEVICE);
  720. } else if (!drop_thres)
  721. goto use_orig_buf;
  722. recycle:
  723. recycle_rx_buf(adap, fl, fl->cidx);
  724. return skb;
  725. }
  726. if (unlikely(fl->credits < drop_thres) &&
  727. refill_fl(adap, fl, min(MAX_RX_REFILL, fl->size - fl->credits - 1),
  728. GFP_ATOMIC | __GFP_COMP) == 0)
  729. goto recycle;
  730. use_orig_buf:
  731. pci_unmap_single(adap->pdev, dma_unmap_addr(sd, dma_addr),
  732. fl->buf_size, PCI_DMA_FROMDEVICE);
  733. skb = sd->skb;
  734. skb_put(skb, len);
  735. __refill_fl(adap, fl);
  736. return skb;
  737. }
  738. /**
  739. * get_packet_pg - return the next ingress packet buffer from a free list
  740. * @adap: the adapter that received the packet
  741. * @fl: the SGE free list holding the packet
  742. * @len: the packet length including any SGE padding
  743. * @drop_thres: # of remaining buffers before we start dropping packets
  744. *
  745. * Get the next packet from a free list populated with page chunks.
  746. * If the packet is small we make a copy and recycle the original buffer,
  747. * otherwise we attach the original buffer as a page fragment to a fresh
  748. * sk_buff. If a positive drop threshold is supplied packets are dropped
  749. * and their buffers recycled if (a) the number of remaining buffers is
  750. * under the threshold and the packet is too big to copy, or (b) there's
  751. * no system memory.
  752. *
  753. * Note: this function is similar to @get_packet but deals with Rx buffers
  754. * that are page chunks rather than sk_buffs.
  755. */
  756. static struct sk_buff *get_packet_pg(struct adapter *adap, struct sge_fl *fl,
  757. struct sge_rspq *q, unsigned int len,
  758. unsigned int drop_thres)
  759. {
  760. struct sk_buff *newskb, *skb;
  761. struct rx_sw_desc *sd = &fl->sdesc[fl->cidx];
  762. dma_addr_t dma_addr = dma_unmap_addr(sd, dma_addr);
  763. newskb = skb = q->pg_skb;
  764. if (!skb && (len <= SGE_RX_COPY_THRES)) {
  765. newskb = alloc_skb(len, GFP_ATOMIC);
  766. if (likely(newskb != NULL)) {
  767. __skb_put(newskb, len);
  768. pci_dma_sync_single_for_cpu(adap->pdev, dma_addr, len,
  769. PCI_DMA_FROMDEVICE);
  770. memcpy(newskb->data, sd->pg_chunk.va, len);
  771. pci_dma_sync_single_for_device(adap->pdev, dma_addr,
  772. len,
  773. PCI_DMA_FROMDEVICE);
  774. } else if (!drop_thres)
  775. return NULL;
  776. recycle:
  777. fl->credits--;
  778. recycle_rx_buf(adap, fl, fl->cidx);
  779. q->rx_recycle_buf++;
  780. return newskb;
  781. }
  782. if (unlikely(q->rx_recycle_buf || (!skb && fl->credits <= drop_thres)))
  783. goto recycle;
  784. prefetch(sd->pg_chunk.p_cnt);
  785. if (!skb)
  786. newskb = alloc_skb(SGE_RX_PULL_LEN, GFP_ATOMIC);
  787. if (unlikely(!newskb)) {
  788. if (!drop_thres)
  789. return NULL;
  790. goto recycle;
  791. }
  792. pci_dma_sync_single_for_cpu(adap->pdev, dma_addr, len,
  793. PCI_DMA_FROMDEVICE);
  794. (*sd->pg_chunk.p_cnt)--;
  795. if (!*sd->pg_chunk.p_cnt && sd->pg_chunk.page != fl->pg_chunk.page)
  796. pci_unmap_page(adap->pdev,
  797. sd->pg_chunk.mapping,
  798. fl->alloc_size,
  799. PCI_DMA_FROMDEVICE);
  800. if (!skb) {
  801. __skb_put(newskb, SGE_RX_PULL_LEN);
  802. memcpy(newskb->data, sd->pg_chunk.va, SGE_RX_PULL_LEN);
  803. skb_fill_page_desc(newskb, 0, sd->pg_chunk.page,
  804. sd->pg_chunk.offset + SGE_RX_PULL_LEN,
  805. len - SGE_RX_PULL_LEN);
  806. newskb->len = len;
  807. newskb->data_len = len - SGE_RX_PULL_LEN;
  808. newskb->truesize += newskb->data_len;
  809. } else {
  810. skb_fill_page_desc(newskb, skb_shinfo(newskb)->nr_frags,
  811. sd->pg_chunk.page,
  812. sd->pg_chunk.offset, len);
  813. newskb->len += len;
  814. newskb->data_len += len;
  815. newskb->truesize += len;
  816. }
  817. fl->credits--;
  818. /*
  819. * We do not refill FLs here, we let the caller do it to overlap a
  820. * prefetch.
  821. */
  822. return newskb;
  823. }
  824. /**
  825. * get_imm_packet - return the next ingress packet buffer from a response
  826. * @resp: the response descriptor containing the packet data
  827. *
  828. * Return a packet containing the immediate data of the given response.
  829. */
  830. static inline struct sk_buff *get_imm_packet(const struct rsp_desc *resp)
  831. {
  832. struct sk_buff *skb = alloc_skb(IMMED_PKT_SIZE, GFP_ATOMIC);
  833. if (skb) {
  834. __skb_put(skb, IMMED_PKT_SIZE);
  835. skb_copy_to_linear_data(skb, resp->imm_data, IMMED_PKT_SIZE);
  836. }
  837. return skb;
  838. }
  839. /**
  840. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  841. * @skb: the packet
  842. *
  843. * Returns the number of Tx descriptors needed for the given Ethernet
  844. * packet. Ethernet packets require addition of WR and CPL headers.
  845. */
  846. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  847. {
  848. unsigned int flits;
  849. if (skb->len <= WR_LEN - sizeof(struct cpl_tx_pkt))
  850. return 1;
  851. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 2;
  852. if (skb_shinfo(skb)->gso_size)
  853. flits++;
  854. return flits_to_desc(flits);
  855. }
  856. /* map_skb - map a packet main body and its page fragments
  857. * @pdev: the PCI device
  858. * @skb: the packet
  859. * @addr: placeholder to save the mapped addresses
  860. *
  861. * map the main body of an sk_buff and its page fragments, if any.
  862. */
  863. static int map_skb(struct pci_dev *pdev, const struct sk_buff *skb,
  864. dma_addr_t *addr)
  865. {
  866. const skb_frag_t *fp, *end;
  867. const struct skb_shared_info *si;
  868. *addr = pci_map_single(pdev, skb->data, skb_headlen(skb),
  869. PCI_DMA_TODEVICE);
  870. if (pci_dma_mapping_error(pdev, *addr))
  871. goto out_err;
  872. si = skb_shinfo(skb);
  873. end = &si->frags[si->nr_frags];
  874. for (fp = si->frags; fp < end; fp++) {
  875. *++addr = skb_frag_dma_map(&pdev->dev, fp, 0, skb_frag_size(fp),
  876. DMA_TO_DEVICE);
  877. if (pci_dma_mapping_error(pdev, *addr))
  878. goto unwind;
  879. }
  880. return 0;
  881. unwind:
  882. while (fp-- > si->frags)
  883. dma_unmap_page(&pdev->dev, *--addr, skb_frag_size(fp),
  884. DMA_TO_DEVICE);
  885. pci_unmap_single(pdev, addr[-1], skb_headlen(skb), PCI_DMA_TODEVICE);
  886. out_err:
  887. return -ENOMEM;
  888. }
  889. /**
  890. * write_sgl - populate a scatter/gather list for a packet
  891. * @skb: the packet
  892. * @sgp: the SGL to populate
  893. * @start: start address of skb main body data to include in the SGL
  894. * @len: length of skb main body data to include in the SGL
  895. * @addr: the list of the mapped addresses
  896. *
  897. * Copies the scatter/gather list for the buffers that make up a packet
  898. * and returns the SGL size in 8-byte words. The caller must size the SGL
  899. * appropriately.
  900. */
  901. static inline unsigned int write_sgl(const struct sk_buff *skb,
  902. struct sg_ent *sgp, unsigned char *start,
  903. unsigned int len, const dma_addr_t *addr)
  904. {
  905. unsigned int i, j = 0, k = 0, nfrags;
  906. if (len) {
  907. sgp->len[0] = cpu_to_be32(len);
  908. sgp->addr[j++] = cpu_to_be64(addr[k++]);
  909. }
  910. nfrags = skb_shinfo(skb)->nr_frags;
  911. for (i = 0; i < nfrags; i++) {
  912. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  913. sgp->len[j] = cpu_to_be32(skb_frag_size(frag));
  914. sgp->addr[j] = cpu_to_be64(addr[k++]);
  915. j ^= 1;
  916. if (j == 0)
  917. ++sgp;
  918. }
  919. if (j)
  920. sgp->len[j] = 0;
  921. return ((nfrags + (len != 0)) * 3) / 2 + j;
  922. }
  923. /**
  924. * check_ring_tx_db - check and potentially ring a Tx queue's doorbell
  925. * @adap: the adapter
  926. * @q: the Tx queue
  927. *
  928. * Ring the doorbel if a Tx queue is asleep. There is a natural race,
  929. * where the HW is going to sleep just after we checked, however,
  930. * then the interrupt handler will detect the outstanding TX packet
  931. * and ring the doorbell for us.
  932. *
  933. * When GTS is disabled we unconditionally ring the doorbell.
  934. */
  935. static inline void check_ring_tx_db(struct adapter *adap, struct sge_txq *q)
  936. {
  937. #if USE_GTS
  938. clear_bit(TXQ_LAST_PKT_DB, &q->flags);
  939. if (test_and_set_bit(TXQ_RUNNING, &q->flags) == 0) {
  940. set_bit(TXQ_LAST_PKT_DB, &q->flags);
  941. t3_write_reg(adap, A_SG_KDOORBELL,
  942. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  943. }
  944. #else
  945. wmb(); /* write descriptors before telling HW */
  946. t3_write_reg(adap, A_SG_KDOORBELL,
  947. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  948. #endif
  949. }
  950. static inline void wr_gen2(struct tx_desc *d, unsigned int gen)
  951. {
  952. #if SGE_NUM_GENBITS == 2
  953. d->flit[TX_DESC_FLITS - 1] = cpu_to_be64(gen);
  954. #endif
  955. }
  956. /**
  957. * write_wr_hdr_sgl - write a WR header and, optionally, SGL
  958. * @ndesc: number of Tx descriptors spanned by the SGL
  959. * @skb: the packet corresponding to the WR
  960. * @d: first Tx descriptor to be written
  961. * @pidx: index of above descriptors
  962. * @q: the SGE Tx queue
  963. * @sgl: the SGL
  964. * @flits: number of flits to the start of the SGL in the first descriptor
  965. * @sgl_flits: the SGL size in flits
  966. * @gen: the Tx descriptor generation
  967. * @wr_hi: top 32 bits of WR header based on WR type (big endian)
  968. * @wr_lo: low 32 bits of WR header based on WR type (big endian)
  969. *
  970. * Write a work request header and an associated SGL. If the SGL is
  971. * small enough to fit into one Tx descriptor it has already been written
  972. * and we just need to write the WR header. Otherwise we distribute the
  973. * SGL across the number of descriptors it spans.
  974. */
  975. static void write_wr_hdr_sgl(unsigned int ndesc, struct sk_buff *skb,
  976. struct tx_desc *d, unsigned int pidx,
  977. const struct sge_txq *q,
  978. const struct sg_ent *sgl,
  979. unsigned int flits, unsigned int sgl_flits,
  980. unsigned int gen, __be32 wr_hi,
  981. __be32 wr_lo)
  982. {
  983. struct work_request_hdr *wrp = (struct work_request_hdr *)d;
  984. struct tx_sw_desc *sd = &q->sdesc[pidx];
  985. sd->skb = skb;
  986. if (need_skb_unmap()) {
  987. sd->fragidx = 0;
  988. sd->addr_idx = 0;
  989. sd->sflit = flits;
  990. }
  991. if (likely(ndesc == 1)) {
  992. sd->eop = 1;
  993. wrp->wr_hi = htonl(F_WR_SOP | F_WR_EOP | V_WR_DATATYPE(1) |
  994. V_WR_SGLSFLT(flits)) | wr_hi;
  995. wmb();
  996. wrp->wr_lo = htonl(V_WR_LEN(flits + sgl_flits) |
  997. V_WR_GEN(gen)) | wr_lo;
  998. wr_gen2(d, gen);
  999. } else {
  1000. unsigned int ogen = gen;
  1001. const u64 *fp = (const u64 *)sgl;
  1002. struct work_request_hdr *wp = wrp;
  1003. wrp->wr_hi = htonl(F_WR_SOP | V_WR_DATATYPE(1) |
  1004. V_WR_SGLSFLT(flits)) | wr_hi;
  1005. while (sgl_flits) {
  1006. unsigned int avail = WR_FLITS - flits;
  1007. if (avail > sgl_flits)
  1008. avail = sgl_flits;
  1009. memcpy(&d->flit[flits], fp, avail * sizeof(*fp));
  1010. sgl_flits -= avail;
  1011. ndesc--;
  1012. if (!sgl_flits)
  1013. break;
  1014. fp += avail;
  1015. d++;
  1016. sd->eop = 0;
  1017. sd++;
  1018. if (++pidx == q->size) {
  1019. pidx = 0;
  1020. gen ^= 1;
  1021. d = q->desc;
  1022. sd = q->sdesc;
  1023. }
  1024. sd->skb = skb;
  1025. wrp = (struct work_request_hdr *)d;
  1026. wrp->wr_hi = htonl(V_WR_DATATYPE(1) |
  1027. V_WR_SGLSFLT(1)) | wr_hi;
  1028. wrp->wr_lo = htonl(V_WR_LEN(min(WR_FLITS,
  1029. sgl_flits + 1)) |
  1030. V_WR_GEN(gen)) | wr_lo;
  1031. wr_gen2(d, gen);
  1032. flits = 1;
  1033. }
  1034. sd->eop = 1;
  1035. wrp->wr_hi |= htonl(F_WR_EOP);
  1036. wmb();
  1037. wp->wr_lo = htonl(V_WR_LEN(WR_FLITS) | V_WR_GEN(ogen)) | wr_lo;
  1038. wr_gen2((struct tx_desc *)wp, ogen);
  1039. WARN_ON(ndesc != 0);
  1040. }
  1041. }
  1042. /**
  1043. * write_tx_pkt_wr - write a TX_PKT work request
  1044. * @adap: the adapter
  1045. * @skb: the packet to send
  1046. * @pi: the egress interface
  1047. * @pidx: index of the first Tx descriptor to write
  1048. * @gen: the generation value to use
  1049. * @q: the Tx queue
  1050. * @ndesc: number of descriptors the packet will occupy
  1051. * @compl: the value of the COMPL bit to use
  1052. *
  1053. * Generate a TX_PKT work request to send the supplied packet.
  1054. */
  1055. static void write_tx_pkt_wr(struct adapter *adap, struct sk_buff *skb,
  1056. const struct port_info *pi,
  1057. unsigned int pidx, unsigned int gen,
  1058. struct sge_txq *q, unsigned int ndesc,
  1059. unsigned int compl, const dma_addr_t *addr)
  1060. {
  1061. unsigned int flits, sgl_flits, cntrl, tso_info;
  1062. struct sg_ent *sgp, sgl[MAX_SKB_FRAGS / 2 + 1];
  1063. struct tx_desc *d = &q->desc[pidx];
  1064. struct cpl_tx_pkt *cpl = (struct cpl_tx_pkt *)d;
  1065. cpl->len = htonl(skb->len);
  1066. cntrl = V_TXPKT_INTF(pi->port_id);
  1067. if (vlan_tx_tag_present(skb))
  1068. cntrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(vlan_tx_tag_get(skb));
  1069. tso_info = V_LSO_MSS(skb_shinfo(skb)->gso_size);
  1070. if (tso_info) {
  1071. int eth_type;
  1072. struct cpl_tx_pkt_lso *hdr = (struct cpl_tx_pkt_lso *)cpl;
  1073. d->flit[2] = 0;
  1074. cntrl |= V_TXPKT_OPCODE(CPL_TX_PKT_LSO);
  1075. hdr->cntrl = htonl(cntrl);
  1076. eth_type = skb_network_offset(skb) == ETH_HLEN ?
  1077. CPL_ETH_II : CPL_ETH_II_VLAN;
  1078. tso_info |= V_LSO_ETH_TYPE(eth_type) |
  1079. V_LSO_IPHDR_WORDS(ip_hdr(skb)->ihl) |
  1080. V_LSO_TCPHDR_WORDS(tcp_hdr(skb)->doff);
  1081. hdr->lso_info = htonl(tso_info);
  1082. flits = 3;
  1083. } else {
  1084. cntrl |= V_TXPKT_OPCODE(CPL_TX_PKT);
  1085. cntrl |= F_TXPKT_IPCSUM_DIS; /* SW calculates IP csum */
  1086. cntrl |= V_TXPKT_L4CSUM_DIS(skb->ip_summed != CHECKSUM_PARTIAL);
  1087. cpl->cntrl = htonl(cntrl);
  1088. if (skb->len <= WR_LEN - sizeof(*cpl)) {
  1089. q->sdesc[pidx].skb = NULL;
  1090. if (!skb->data_len)
  1091. skb_copy_from_linear_data(skb, &d->flit[2],
  1092. skb->len);
  1093. else
  1094. skb_copy_bits(skb, 0, &d->flit[2], skb->len);
  1095. flits = (skb->len + 7) / 8 + 2;
  1096. cpl->wr.wr_hi = htonl(V_WR_BCNTLFLT(skb->len & 7) |
  1097. V_WR_OP(FW_WROPCODE_TUNNEL_TX_PKT)
  1098. | F_WR_SOP | F_WR_EOP | compl);
  1099. wmb();
  1100. cpl->wr.wr_lo = htonl(V_WR_LEN(flits) | V_WR_GEN(gen) |
  1101. V_WR_TID(q->token));
  1102. wr_gen2(d, gen);
  1103. kfree_skb(skb);
  1104. return;
  1105. }
  1106. flits = 2;
  1107. }
  1108. sgp = ndesc == 1 ? (struct sg_ent *)&d->flit[flits] : sgl;
  1109. sgl_flits = write_sgl(skb, sgp, skb->data, skb_headlen(skb), addr);
  1110. write_wr_hdr_sgl(ndesc, skb, d, pidx, q, sgl, flits, sgl_flits, gen,
  1111. htonl(V_WR_OP(FW_WROPCODE_TUNNEL_TX_PKT) | compl),
  1112. htonl(V_WR_TID(q->token)));
  1113. }
  1114. static inline void t3_stop_tx_queue(struct netdev_queue *txq,
  1115. struct sge_qset *qs, struct sge_txq *q)
  1116. {
  1117. netif_tx_stop_queue(txq);
  1118. set_bit(TXQ_ETH, &qs->txq_stopped);
  1119. q->stops++;
  1120. }
  1121. /**
  1122. * eth_xmit - add a packet to the Ethernet Tx queue
  1123. * @skb: the packet
  1124. * @dev: the egress net device
  1125. *
  1126. * Add a packet to an SGE Tx queue. Runs with softirqs disabled.
  1127. */
  1128. netdev_tx_t t3_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  1129. {
  1130. int qidx;
  1131. unsigned int ndesc, pidx, credits, gen, compl;
  1132. const struct port_info *pi = netdev_priv(dev);
  1133. struct adapter *adap = pi->adapter;
  1134. struct netdev_queue *txq;
  1135. struct sge_qset *qs;
  1136. struct sge_txq *q;
  1137. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  1138. /*
  1139. * The chip min packet length is 9 octets but play safe and reject
  1140. * anything shorter than an Ethernet header.
  1141. */
  1142. if (unlikely(skb->len < ETH_HLEN)) {
  1143. dev_kfree_skb(skb);
  1144. return NETDEV_TX_OK;
  1145. }
  1146. qidx = skb_get_queue_mapping(skb);
  1147. qs = &pi->qs[qidx];
  1148. q = &qs->txq[TXQ_ETH];
  1149. txq = netdev_get_tx_queue(dev, qidx);
  1150. reclaim_completed_tx(adap, q, TX_RECLAIM_CHUNK);
  1151. credits = q->size - q->in_use;
  1152. ndesc = calc_tx_descs(skb);
  1153. if (unlikely(credits < ndesc)) {
  1154. t3_stop_tx_queue(txq, qs, q);
  1155. dev_err(&adap->pdev->dev,
  1156. "%s: Tx ring %u full while queue awake!\n",
  1157. dev->name, q->cntxt_id & 7);
  1158. return NETDEV_TX_BUSY;
  1159. }
  1160. if (unlikely(map_skb(adap->pdev, skb, addr) < 0)) {
  1161. dev_kfree_skb(skb);
  1162. return NETDEV_TX_OK;
  1163. }
  1164. q->in_use += ndesc;
  1165. if (unlikely(credits - ndesc < q->stop_thres)) {
  1166. t3_stop_tx_queue(txq, qs, q);
  1167. if (should_restart_tx(q) &&
  1168. test_and_clear_bit(TXQ_ETH, &qs->txq_stopped)) {
  1169. q->restarts++;
  1170. netif_tx_start_queue(txq);
  1171. }
  1172. }
  1173. gen = q->gen;
  1174. q->unacked += ndesc;
  1175. compl = (q->unacked & 8) << (S_WR_COMPL - 3);
  1176. q->unacked &= 7;
  1177. pidx = q->pidx;
  1178. q->pidx += ndesc;
  1179. if (q->pidx >= q->size) {
  1180. q->pidx -= q->size;
  1181. q->gen ^= 1;
  1182. }
  1183. /* update port statistics */
  1184. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1185. qs->port_stats[SGE_PSTAT_TX_CSUM]++;
  1186. if (skb_shinfo(skb)->gso_size)
  1187. qs->port_stats[SGE_PSTAT_TSO]++;
  1188. if (vlan_tx_tag_present(skb))
  1189. qs->port_stats[SGE_PSTAT_VLANINS]++;
  1190. /*
  1191. * We do not use Tx completion interrupts to free DMAd Tx packets.
  1192. * This is good for performance but means that we rely on new Tx
  1193. * packets arriving to run the destructors of completed packets,
  1194. * which open up space in their sockets' send queues. Sometimes
  1195. * we do not get such new packets causing Tx to stall. A single
  1196. * UDP transmitter is a good example of this situation. We have
  1197. * a clean up timer that periodically reclaims completed packets
  1198. * but it doesn't run often enough (nor do we want it to) to prevent
  1199. * lengthy stalls. A solution to this problem is to run the
  1200. * destructor early, after the packet is queued but before it's DMAd.
  1201. * A cons is that we lie to socket memory accounting, but the amount
  1202. * of extra memory is reasonable (limited by the number of Tx
  1203. * descriptors), the packets do actually get freed quickly by new
  1204. * packets almost always, and for protocols like TCP that wait for
  1205. * acks to really free up the data the extra memory is even less.
  1206. * On the positive side we run the destructors on the sending CPU
  1207. * rather than on a potentially different completing CPU, usually a
  1208. * good thing. We also run them without holding our Tx queue lock,
  1209. * unlike what reclaim_completed_tx() would otherwise do.
  1210. *
  1211. * Run the destructor before telling the DMA engine about the packet
  1212. * to make sure it doesn't complete and get freed prematurely.
  1213. */
  1214. if (likely(!skb_shared(skb)))
  1215. skb_orphan(skb);
  1216. write_tx_pkt_wr(adap, skb, pi, pidx, gen, q, ndesc, compl, addr);
  1217. check_ring_tx_db(adap, q);
  1218. return NETDEV_TX_OK;
  1219. }
  1220. /**
  1221. * write_imm - write a packet into a Tx descriptor as immediate data
  1222. * @d: the Tx descriptor to write
  1223. * @skb: the packet
  1224. * @len: the length of packet data to write as immediate data
  1225. * @gen: the generation bit value to write
  1226. *
  1227. * Writes a packet as immediate data into a Tx descriptor. The packet
  1228. * contains a work request at its beginning. We must write the packet
  1229. * carefully so the SGE doesn't read it accidentally before it's written
  1230. * in its entirety.
  1231. */
  1232. static inline void write_imm(struct tx_desc *d, struct sk_buff *skb,
  1233. unsigned int len, unsigned int gen)
  1234. {
  1235. struct work_request_hdr *from = (struct work_request_hdr *)skb->data;
  1236. struct work_request_hdr *to = (struct work_request_hdr *)d;
  1237. if (likely(!skb->data_len))
  1238. memcpy(&to[1], &from[1], len - sizeof(*from));
  1239. else
  1240. skb_copy_bits(skb, sizeof(*from), &to[1], len - sizeof(*from));
  1241. to->wr_hi = from->wr_hi | htonl(F_WR_SOP | F_WR_EOP |
  1242. V_WR_BCNTLFLT(len & 7));
  1243. wmb();
  1244. to->wr_lo = from->wr_lo | htonl(V_WR_GEN(gen) |
  1245. V_WR_LEN((len + 7) / 8));
  1246. wr_gen2(d, gen);
  1247. kfree_skb(skb);
  1248. }
  1249. /**
  1250. * check_desc_avail - check descriptor availability on a send queue
  1251. * @adap: the adapter
  1252. * @q: the send queue
  1253. * @skb: the packet needing the descriptors
  1254. * @ndesc: the number of Tx descriptors needed
  1255. * @qid: the Tx queue number in its queue set (TXQ_OFLD or TXQ_CTRL)
  1256. *
  1257. * Checks if the requested number of Tx descriptors is available on an
  1258. * SGE send queue. If the queue is already suspended or not enough
  1259. * descriptors are available the packet is queued for later transmission.
  1260. * Must be called with the Tx queue locked.
  1261. *
  1262. * Returns 0 if enough descriptors are available, 1 if there aren't
  1263. * enough descriptors and the packet has been queued, and 2 if the caller
  1264. * needs to retry because there weren't enough descriptors at the
  1265. * beginning of the call but some freed up in the mean time.
  1266. */
  1267. static inline int check_desc_avail(struct adapter *adap, struct sge_txq *q,
  1268. struct sk_buff *skb, unsigned int ndesc,
  1269. unsigned int qid)
  1270. {
  1271. if (unlikely(!skb_queue_empty(&q->sendq))) {
  1272. addq_exit:__skb_queue_tail(&q->sendq, skb);
  1273. return 1;
  1274. }
  1275. if (unlikely(q->size - q->in_use < ndesc)) {
  1276. struct sge_qset *qs = txq_to_qset(q, qid);
  1277. set_bit(qid, &qs->txq_stopped);
  1278. smp_mb__after_clear_bit();
  1279. if (should_restart_tx(q) &&
  1280. test_and_clear_bit(qid, &qs->txq_stopped))
  1281. return 2;
  1282. q->stops++;
  1283. goto addq_exit;
  1284. }
  1285. return 0;
  1286. }
  1287. /**
  1288. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1289. * @q: the SGE control Tx queue
  1290. *
  1291. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  1292. * that send only immediate data (presently just the control queues) and
  1293. * thus do not have any sk_buffs to release.
  1294. */
  1295. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1296. {
  1297. unsigned int reclaim = q->processed - q->cleaned;
  1298. q->in_use -= reclaim;
  1299. q->cleaned += reclaim;
  1300. }
  1301. static inline int immediate(const struct sk_buff *skb)
  1302. {
  1303. return skb->len <= WR_LEN;
  1304. }
  1305. /**
  1306. * ctrl_xmit - send a packet through an SGE control Tx queue
  1307. * @adap: the adapter
  1308. * @q: the control queue
  1309. * @skb: the packet
  1310. *
  1311. * Send a packet through an SGE control Tx queue. Packets sent through
  1312. * a control queue must fit entirely as immediate data in a single Tx
  1313. * descriptor and have no page fragments.
  1314. */
  1315. static int ctrl_xmit(struct adapter *adap, struct sge_txq *q,
  1316. struct sk_buff *skb)
  1317. {
  1318. int ret;
  1319. struct work_request_hdr *wrp = (struct work_request_hdr *)skb->data;
  1320. if (unlikely(!immediate(skb))) {
  1321. WARN_ON(1);
  1322. dev_kfree_skb(skb);
  1323. return NET_XMIT_SUCCESS;
  1324. }
  1325. wrp->wr_hi |= htonl(F_WR_SOP | F_WR_EOP);
  1326. wrp->wr_lo = htonl(V_WR_TID(q->token));
  1327. spin_lock(&q->lock);
  1328. again:reclaim_completed_tx_imm(q);
  1329. ret = check_desc_avail(adap, q, skb, 1, TXQ_CTRL);
  1330. if (unlikely(ret)) {
  1331. if (ret == 1) {
  1332. spin_unlock(&q->lock);
  1333. return NET_XMIT_CN;
  1334. }
  1335. goto again;
  1336. }
  1337. write_imm(&q->desc[q->pidx], skb, skb->len, q->gen);
  1338. q->in_use++;
  1339. if (++q->pidx >= q->size) {
  1340. q->pidx = 0;
  1341. q->gen ^= 1;
  1342. }
  1343. spin_unlock(&q->lock);
  1344. wmb();
  1345. t3_write_reg(adap, A_SG_KDOORBELL,
  1346. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  1347. return NET_XMIT_SUCCESS;
  1348. }
  1349. /**
  1350. * restart_ctrlq - restart a suspended control queue
  1351. * @qs: the queue set cotaining the control queue
  1352. *
  1353. * Resumes transmission on a suspended Tx control queue.
  1354. */
  1355. static void restart_ctrlq(unsigned long data)
  1356. {
  1357. struct sk_buff *skb;
  1358. struct sge_qset *qs = (struct sge_qset *)data;
  1359. struct sge_txq *q = &qs->txq[TXQ_CTRL];
  1360. spin_lock(&q->lock);
  1361. again:reclaim_completed_tx_imm(q);
  1362. while (q->in_use < q->size &&
  1363. (skb = __skb_dequeue(&q->sendq)) != NULL) {
  1364. write_imm(&q->desc[q->pidx], skb, skb->len, q->gen);
  1365. if (++q->pidx >= q->size) {
  1366. q->pidx = 0;
  1367. q->gen ^= 1;
  1368. }
  1369. q->in_use++;
  1370. }
  1371. if (!skb_queue_empty(&q->sendq)) {
  1372. set_bit(TXQ_CTRL, &qs->txq_stopped);
  1373. smp_mb__after_clear_bit();
  1374. if (should_restart_tx(q) &&
  1375. test_and_clear_bit(TXQ_CTRL, &qs->txq_stopped))
  1376. goto again;
  1377. q->stops++;
  1378. }
  1379. spin_unlock(&q->lock);
  1380. wmb();
  1381. t3_write_reg(qs->adap, A_SG_KDOORBELL,
  1382. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  1383. }
  1384. /*
  1385. * Send a management message through control queue 0
  1386. */
  1387. int t3_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1388. {
  1389. int ret;
  1390. local_bh_disable();
  1391. ret = ctrl_xmit(adap, &adap->sge.qs[0].txq[TXQ_CTRL], skb);
  1392. local_bh_enable();
  1393. return ret;
  1394. }
  1395. /**
  1396. * deferred_unmap_destructor - unmap a packet when it is freed
  1397. * @skb: the packet
  1398. *
  1399. * This is the packet destructor used for Tx packets that need to remain
  1400. * mapped until they are freed rather than until their Tx descriptors are
  1401. * freed.
  1402. */
  1403. static void deferred_unmap_destructor(struct sk_buff *skb)
  1404. {
  1405. int i;
  1406. const dma_addr_t *p;
  1407. const struct skb_shared_info *si;
  1408. const struct deferred_unmap_info *dui;
  1409. dui = (struct deferred_unmap_info *)skb->head;
  1410. p = dui->addr;
  1411. if (skb_tail_pointer(skb) - skb_transport_header(skb))
  1412. pci_unmap_single(dui->pdev, *p++, skb_tail_pointer(skb) -
  1413. skb_transport_header(skb), PCI_DMA_TODEVICE);
  1414. si = skb_shinfo(skb);
  1415. for (i = 0; i < si->nr_frags; i++)
  1416. pci_unmap_page(dui->pdev, *p++, skb_frag_size(&si->frags[i]),
  1417. PCI_DMA_TODEVICE);
  1418. }
  1419. static void setup_deferred_unmapping(struct sk_buff *skb, struct pci_dev *pdev,
  1420. const struct sg_ent *sgl, int sgl_flits)
  1421. {
  1422. dma_addr_t *p;
  1423. struct deferred_unmap_info *dui;
  1424. dui = (struct deferred_unmap_info *)skb->head;
  1425. dui->pdev = pdev;
  1426. for (p = dui->addr; sgl_flits >= 3; sgl++, sgl_flits -= 3) {
  1427. *p++ = be64_to_cpu(sgl->addr[0]);
  1428. *p++ = be64_to_cpu(sgl->addr[1]);
  1429. }
  1430. if (sgl_flits)
  1431. *p = be64_to_cpu(sgl->addr[0]);
  1432. }
  1433. /**
  1434. * write_ofld_wr - write an offload work request
  1435. * @adap: the adapter
  1436. * @skb: the packet to send
  1437. * @q: the Tx queue
  1438. * @pidx: index of the first Tx descriptor to write
  1439. * @gen: the generation value to use
  1440. * @ndesc: number of descriptors the packet will occupy
  1441. *
  1442. * Write an offload work request to send the supplied packet. The packet
  1443. * data already carry the work request with most fields populated.
  1444. */
  1445. static void write_ofld_wr(struct adapter *adap, struct sk_buff *skb,
  1446. struct sge_txq *q, unsigned int pidx,
  1447. unsigned int gen, unsigned int ndesc,
  1448. const dma_addr_t *addr)
  1449. {
  1450. unsigned int sgl_flits, flits;
  1451. struct work_request_hdr *from;
  1452. struct sg_ent *sgp, sgl[MAX_SKB_FRAGS / 2 + 1];
  1453. struct tx_desc *d = &q->desc[pidx];
  1454. if (immediate(skb)) {
  1455. q->sdesc[pidx].skb = NULL;
  1456. write_imm(d, skb, skb->len, gen);
  1457. return;
  1458. }
  1459. /* Only TX_DATA builds SGLs */
  1460. from = (struct work_request_hdr *)skb->data;
  1461. memcpy(&d->flit[1], &from[1],
  1462. skb_transport_offset(skb) - sizeof(*from));
  1463. flits = skb_transport_offset(skb) / 8;
  1464. sgp = ndesc == 1 ? (struct sg_ent *)&d->flit[flits] : sgl;
  1465. sgl_flits = write_sgl(skb, sgp, skb_transport_header(skb),
  1466. skb_tail_pointer(skb) -
  1467. skb_transport_header(skb), addr);
  1468. if (need_skb_unmap()) {
  1469. setup_deferred_unmapping(skb, adap->pdev, sgp, sgl_flits);
  1470. skb->destructor = deferred_unmap_destructor;
  1471. }
  1472. write_wr_hdr_sgl(ndesc, skb, d, pidx, q, sgl, flits, sgl_flits,
  1473. gen, from->wr_hi, from->wr_lo);
  1474. }
  1475. /**
  1476. * calc_tx_descs_ofld - calculate # of Tx descriptors for an offload packet
  1477. * @skb: the packet
  1478. *
  1479. * Returns the number of Tx descriptors needed for the given offload
  1480. * packet. These packets are already fully constructed.
  1481. */
  1482. static inline unsigned int calc_tx_descs_ofld(const struct sk_buff *skb)
  1483. {
  1484. unsigned int flits, cnt;
  1485. if (skb->len <= WR_LEN)
  1486. return 1; /* packet fits as immediate data */
  1487. flits = skb_transport_offset(skb) / 8; /* headers */
  1488. cnt = skb_shinfo(skb)->nr_frags;
  1489. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1490. cnt++;
  1491. return flits_to_desc(flits + sgl_len(cnt));
  1492. }
  1493. /**
  1494. * ofld_xmit - send a packet through an offload queue
  1495. * @adap: the adapter
  1496. * @q: the Tx offload queue
  1497. * @skb: the packet
  1498. *
  1499. * Send an offload packet through an SGE offload queue.
  1500. */
  1501. static int ofld_xmit(struct adapter *adap, struct sge_txq *q,
  1502. struct sk_buff *skb)
  1503. {
  1504. int ret;
  1505. unsigned int ndesc = calc_tx_descs_ofld(skb), pidx, gen;
  1506. spin_lock(&q->lock);
  1507. again: reclaim_completed_tx(adap, q, TX_RECLAIM_CHUNK);
  1508. ret = check_desc_avail(adap, q, skb, ndesc, TXQ_OFLD);
  1509. if (unlikely(ret)) {
  1510. if (ret == 1) {
  1511. skb->priority = ndesc; /* save for restart */
  1512. spin_unlock(&q->lock);
  1513. return NET_XMIT_CN;
  1514. }
  1515. goto again;
  1516. }
  1517. if (map_skb(adap->pdev, skb, (dma_addr_t *)skb->head)) {
  1518. spin_unlock(&q->lock);
  1519. return NET_XMIT_SUCCESS;
  1520. }
  1521. gen = q->gen;
  1522. q->in_use += ndesc;
  1523. pidx = q->pidx;
  1524. q->pidx += ndesc;
  1525. if (q->pidx >= q->size) {
  1526. q->pidx -= q->size;
  1527. q->gen ^= 1;
  1528. }
  1529. spin_unlock(&q->lock);
  1530. write_ofld_wr(adap, skb, q, pidx, gen, ndesc, (dma_addr_t *)skb->head);
  1531. check_ring_tx_db(adap, q);
  1532. return NET_XMIT_SUCCESS;
  1533. }
  1534. /**
  1535. * restart_offloadq - restart a suspended offload queue
  1536. * @qs: the queue set cotaining the offload queue
  1537. *
  1538. * Resumes transmission on a suspended Tx offload queue.
  1539. */
  1540. static void restart_offloadq(unsigned long data)
  1541. {
  1542. struct sk_buff *skb;
  1543. struct sge_qset *qs = (struct sge_qset *)data;
  1544. struct sge_txq *q = &qs->txq[TXQ_OFLD];
  1545. const struct port_info *pi = netdev_priv(qs->netdev);
  1546. struct adapter *adap = pi->adapter;
  1547. unsigned int written = 0;
  1548. spin_lock(&q->lock);
  1549. again: reclaim_completed_tx(adap, q, TX_RECLAIM_CHUNK);
  1550. while ((skb = skb_peek(&q->sendq)) != NULL) {
  1551. unsigned int gen, pidx;
  1552. unsigned int ndesc = skb->priority;
  1553. if (unlikely(q->size - q->in_use < ndesc)) {
  1554. set_bit(TXQ_OFLD, &qs->txq_stopped);
  1555. smp_mb__after_clear_bit();
  1556. if (should_restart_tx(q) &&
  1557. test_and_clear_bit(TXQ_OFLD, &qs->txq_stopped))
  1558. goto again;
  1559. q->stops++;
  1560. break;
  1561. }
  1562. if (map_skb(adap->pdev, skb, (dma_addr_t *)skb->head))
  1563. break;
  1564. gen = q->gen;
  1565. q->in_use += ndesc;
  1566. pidx = q->pidx;
  1567. q->pidx += ndesc;
  1568. written += ndesc;
  1569. if (q->pidx >= q->size) {
  1570. q->pidx -= q->size;
  1571. q->gen ^= 1;
  1572. }
  1573. __skb_unlink(skb, &q->sendq);
  1574. spin_unlock(&q->lock);
  1575. write_ofld_wr(adap, skb, q, pidx, gen, ndesc,
  1576. (dma_addr_t *)skb->head);
  1577. spin_lock(&q->lock);
  1578. }
  1579. spin_unlock(&q->lock);
  1580. #if USE_GTS
  1581. set_bit(TXQ_RUNNING, &q->flags);
  1582. set_bit(TXQ_LAST_PKT_DB, &q->flags);
  1583. #endif
  1584. wmb();
  1585. if (likely(written))
  1586. t3_write_reg(adap, A_SG_KDOORBELL,
  1587. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  1588. }
  1589. /**
  1590. * queue_set - return the queue set a packet should use
  1591. * @skb: the packet
  1592. *
  1593. * Maps a packet to the SGE queue set it should use. The desired queue
  1594. * set is carried in bits 1-3 in the packet's priority.
  1595. */
  1596. static inline int queue_set(const struct sk_buff *skb)
  1597. {
  1598. return skb->priority >> 1;
  1599. }
  1600. /**
  1601. * is_ctrl_pkt - return whether an offload packet is a control packet
  1602. * @skb: the packet
  1603. *
  1604. * Determines whether an offload packet should use an OFLD or a CTRL
  1605. * Tx queue. This is indicated by bit 0 in the packet's priority.
  1606. */
  1607. static inline int is_ctrl_pkt(const struct sk_buff *skb)
  1608. {
  1609. return skb->priority & 1;
  1610. }
  1611. /**
  1612. * t3_offload_tx - send an offload packet
  1613. * @tdev: the offload device to send to
  1614. * @skb: the packet
  1615. *
  1616. * Sends an offload packet. We use the packet priority to select the
  1617. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1618. * should be sent as regular or control, bits 1-3 select the queue set.
  1619. */
  1620. int t3_offload_tx(struct t3cdev *tdev, struct sk_buff *skb)
  1621. {
  1622. struct adapter *adap = tdev2adap(tdev);
  1623. struct sge_qset *qs = &adap->sge.qs[queue_set(skb)];
  1624. if (unlikely(is_ctrl_pkt(skb)))
  1625. return ctrl_xmit(adap, &qs->txq[TXQ_CTRL], skb);
  1626. return ofld_xmit(adap, &qs->txq[TXQ_OFLD], skb);
  1627. }
  1628. /**
  1629. * offload_enqueue - add an offload packet to an SGE offload receive queue
  1630. * @q: the SGE response queue
  1631. * @skb: the packet
  1632. *
  1633. * Add a new offload packet to an SGE response queue's offload packet
  1634. * queue. If the packet is the first on the queue it schedules the RX
  1635. * softirq to process the queue.
  1636. */
  1637. static inline void offload_enqueue(struct sge_rspq *q, struct sk_buff *skb)
  1638. {
  1639. int was_empty = skb_queue_empty(&q->rx_queue);
  1640. __skb_queue_tail(&q->rx_queue, skb);
  1641. if (was_empty) {
  1642. struct sge_qset *qs = rspq_to_qset(q);
  1643. napi_schedule(&qs->napi);
  1644. }
  1645. }
  1646. /**
  1647. * deliver_partial_bundle - deliver a (partial) bundle of Rx offload pkts
  1648. * @tdev: the offload device that will be receiving the packets
  1649. * @q: the SGE response queue that assembled the bundle
  1650. * @skbs: the partial bundle
  1651. * @n: the number of packets in the bundle
  1652. *
  1653. * Delivers a (partial) bundle of Rx offload packets to an offload device.
  1654. */
  1655. static inline void deliver_partial_bundle(struct t3cdev *tdev,
  1656. struct sge_rspq *q,
  1657. struct sk_buff *skbs[], int n)
  1658. {
  1659. if (n) {
  1660. q->offload_bundles++;
  1661. tdev->recv(tdev, skbs, n);
  1662. }
  1663. }
  1664. /**
  1665. * ofld_poll - NAPI handler for offload packets in interrupt mode
  1666. * @dev: the network device doing the polling
  1667. * @budget: polling budget
  1668. *
  1669. * The NAPI handler for offload packets when a response queue is serviced
  1670. * by the hard interrupt handler, i.e., when it's operating in non-polling
  1671. * mode. Creates small packet batches and sends them through the offload
  1672. * receive handler. Batches need to be of modest size as we do prefetches
  1673. * on the packets in each.
  1674. */
  1675. static int ofld_poll(struct napi_struct *napi, int budget)
  1676. {
  1677. struct sge_qset *qs = container_of(napi, struct sge_qset, napi);
  1678. struct sge_rspq *q = &qs->rspq;
  1679. struct adapter *adapter = qs->adap;
  1680. int work_done = 0;
  1681. while (work_done < budget) {
  1682. struct sk_buff *skb, *tmp, *skbs[RX_BUNDLE_SIZE];
  1683. struct sk_buff_head queue;
  1684. int ngathered;
  1685. spin_lock_irq(&q->lock);
  1686. __skb_queue_head_init(&queue);
  1687. skb_queue_splice_init(&q->rx_queue, &queue);
  1688. if (skb_queue_empty(&queue)) {
  1689. napi_complete(napi);
  1690. spin_unlock_irq(&q->lock);
  1691. return work_done;
  1692. }
  1693. spin_unlock_irq(&q->lock);
  1694. ngathered = 0;
  1695. skb_queue_walk_safe(&queue, skb, tmp) {
  1696. if (work_done >= budget)
  1697. break;
  1698. work_done++;
  1699. __skb_unlink(skb, &queue);
  1700. prefetch(skb->data);
  1701. skbs[ngathered] = skb;
  1702. if (++ngathered == RX_BUNDLE_SIZE) {
  1703. q->offload_bundles++;
  1704. adapter->tdev.recv(&adapter->tdev, skbs,
  1705. ngathered);
  1706. ngathered = 0;
  1707. }
  1708. }
  1709. if (!skb_queue_empty(&queue)) {
  1710. /* splice remaining packets back onto Rx queue */
  1711. spin_lock_irq(&q->lock);
  1712. skb_queue_splice(&queue, &q->rx_queue);
  1713. spin_unlock_irq(&q->lock);
  1714. }
  1715. deliver_partial_bundle(&adapter->tdev, q, skbs, ngathered);
  1716. }
  1717. return work_done;
  1718. }
  1719. /**
  1720. * rx_offload - process a received offload packet
  1721. * @tdev: the offload device receiving the packet
  1722. * @rq: the response queue that received the packet
  1723. * @skb: the packet
  1724. * @rx_gather: a gather list of packets if we are building a bundle
  1725. * @gather_idx: index of the next available slot in the bundle
  1726. *
  1727. * Process an ingress offload pakcet and add it to the offload ingress
  1728. * queue. Returns the index of the next available slot in the bundle.
  1729. */
  1730. static inline int rx_offload(struct t3cdev *tdev, struct sge_rspq *rq,
  1731. struct sk_buff *skb, struct sk_buff *rx_gather[],
  1732. unsigned int gather_idx)
  1733. {
  1734. skb_reset_mac_header(skb);
  1735. skb_reset_network_header(skb);
  1736. skb_reset_transport_header(skb);
  1737. if (rq->polling) {
  1738. rx_gather[gather_idx++] = skb;
  1739. if (gather_idx == RX_BUNDLE_SIZE) {
  1740. tdev->recv(tdev, rx_gather, RX_BUNDLE_SIZE);
  1741. gather_idx = 0;
  1742. rq->offload_bundles++;
  1743. }
  1744. } else
  1745. offload_enqueue(rq, skb);
  1746. return gather_idx;
  1747. }
  1748. /**
  1749. * restart_tx - check whether to restart suspended Tx queues
  1750. * @qs: the queue set to resume
  1751. *
  1752. * Restarts suspended Tx queues of an SGE queue set if they have enough
  1753. * free resources to resume operation.
  1754. */
  1755. static void restart_tx(struct sge_qset *qs)
  1756. {
  1757. if (test_bit(TXQ_ETH, &qs->txq_stopped) &&
  1758. should_restart_tx(&qs->txq[TXQ_ETH]) &&
  1759. test_and_clear_bit(TXQ_ETH, &qs->txq_stopped)) {
  1760. qs->txq[TXQ_ETH].restarts++;
  1761. if (netif_running(qs->netdev))
  1762. netif_tx_wake_queue(qs->tx_q);
  1763. }
  1764. if (test_bit(TXQ_OFLD, &qs->txq_stopped) &&
  1765. should_restart_tx(&qs->txq[TXQ_OFLD]) &&
  1766. test_and_clear_bit(TXQ_OFLD, &qs->txq_stopped)) {
  1767. qs->txq[TXQ_OFLD].restarts++;
  1768. tasklet_schedule(&qs->txq[TXQ_OFLD].qresume_tsk);
  1769. }
  1770. if (test_bit(TXQ_CTRL, &qs->txq_stopped) &&
  1771. should_restart_tx(&qs->txq[TXQ_CTRL]) &&
  1772. test_and_clear_bit(TXQ_CTRL, &qs->txq_stopped)) {
  1773. qs->txq[TXQ_CTRL].restarts++;
  1774. tasklet_schedule(&qs->txq[TXQ_CTRL].qresume_tsk);
  1775. }
  1776. }
  1777. /**
  1778. * cxgb3_arp_process - process an ARP request probing a private IP address
  1779. * @adapter: the adapter
  1780. * @skb: the skbuff containing the ARP request
  1781. *
  1782. * Check if the ARP request is probing the private IP address
  1783. * dedicated to iSCSI, generate an ARP reply if so.
  1784. */
  1785. static void cxgb3_arp_process(struct port_info *pi, struct sk_buff *skb)
  1786. {
  1787. struct net_device *dev = skb->dev;
  1788. struct arphdr *arp;
  1789. unsigned char *arp_ptr;
  1790. unsigned char *sha;
  1791. __be32 sip, tip;
  1792. if (!dev)
  1793. return;
  1794. skb_reset_network_header(skb);
  1795. arp = arp_hdr(skb);
  1796. if (arp->ar_op != htons(ARPOP_REQUEST))
  1797. return;
  1798. arp_ptr = (unsigned char *)(arp + 1);
  1799. sha = arp_ptr;
  1800. arp_ptr += dev->addr_len;
  1801. memcpy(&sip, arp_ptr, sizeof(sip));
  1802. arp_ptr += sizeof(sip);
  1803. arp_ptr += dev->addr_len;
  1804. memcpy(&tip, arp_ptr, sizeof(tip));
  1805. if (tip != pi->iscsi_ipv4addr)
  1806. return;
  1807. arp_send(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  1808. pi->iscsic.mac_addr, sha);
  1809. }
  1810. static inline int is_arp(struct sk_buff *skb)
  1811. {
  1812. return skb->protocol == htons(ETH_P_ARP);
  1813. }
  1814. static void cxgb3_process_iscsi_prov_pack(struct port_info *pi,
  1815. struct sk_buff *skb)
  1816. {
  1817. if (is_arp(skb)) {
  1818. cxgb3_arp_process(pi, skb);
  1819. return;
  1820. }
  1821. if (pi->iscsic.recv)
  1822. pi->iscsic.recv(pi, skb);
  1823. }
  1824. /**
  1825. * rx_eth - process an ingress ethernet packet
  1826. * @adap: the adapter
  1827. * @rq: the response queue that received the packet
  1828. * @skb: the packet
  1829. * @pad: amount of padding at the start of the buffer
  1830. *
  1831. * Process an ingress ethernet pakcet and deliver it to the stack.
  1832. * The padding is 2 if the packet was delivered in an Rx buffer and 0
  1833. * if it was immediate data in a response.
  1834. */
  1835. static void rx_eth(struct adapter *adap, struct sge_rspq *rq,
  1836. struct sk_buff *skb, int pad, int lro)
  1837. {
  1838. struct cpl_rx_pkt *p = (struct cpl_rx_pkt *)(skb->data + pad);
  1839. struct sge_qset *qs = rspq_to_qset(rq);
  1840. struct port_info *pi;
  1841. skb_pull(skb, sizeof(*p) + pad);
  1842. skb->protocol = eth_type_trans(skb, adap->port[p->iff]);
  1843. pi = netdev_priv(skb->dev);
  1844. if ((skb->dev->features & NETIF_F_RXCSUM) && p->csum_valid &&
  1845. p->csum == htons(0xffff) && !p->fragment) {
  1846. qs->port_stats[SGE_PSTAT_RX_CSUM_GOOD]++;
  1847. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1848. } else
  1849. skb_checksum_none_assert(skb);
  1850. skb_record_rx_queue(skb, qs - &adap->sge.qs[pi->first_qset]);
  1851. if (p->vlan_valid) {
  1852. qs->port_stats[SGE_PSTAT_VLANEX]++;
  1853. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(p->vlan));
  1854. }
  1855. if (rq->polling) {
  1856. if (lro)
  1857. napi_gro_receive(&qs->napi, skb);
  1858. else {
  1859. if (unlikely(pi->iscsic.flags))
  1860. cxgb3_process_iscsi_prov_pack(pi, skb);
  1861. netif_receive_skb(skb);
  1862. }
  1863. } else
  1864. netif_rx(skb);
  1865. }
  1866. static inline int is_eth_tcp(u32 rss)
  1867. {
  1868. return G_HASHTYPE(ntohl(rss)) == RSS_HASH_4_TUPLE;
  1869. }
  1870. /**
  1871. * lro_add_page - add a page chunk to an LRO session
  1872. * @adap: the adapter
  1873. * @qs: the associated queue set
  1874. * @fl: the free list containing the page chunk to add
  1875. * @len: packet length
  1876. * @complete: Indicates the last fragment of a frame
  1877. *
  1878. * Add a received packet contained in a page chunk to an existing LRO
  1879. * session.
  1880. */
  1881. static void lro_add_page(struct adapter *adap, struct sge_qset *qs,
  1882. struct sge_fl *fl, int len, int complete)
  1883. {
  1884. struct rx_sw_desc *sd = &fl->sdesc[fl->cidx];
  1885. struct port_info *pi = netdev_priv(qs->netdev);
  1886. struct sk_buff *skb = NULL;
  1887. struct cpl_rx_pkt *cpl;
  1888. struct skb_frag_struct *rx_frag;
  1889. int nr_frags;
  1890. int offset = 0;
  1891. if (!qs->nomem) {
  1892. skb = napi_get_frags(&qs->napi);
  1893. qs->nomem = !skb;
  1894. }
  1895. fl->credits--;
  1896. pci_dma_sync_single_for_cpu(adap->pdev,
  1897. dma_unmap_addr(sd, dma_addr),
  1898. fl->buf_size - SGE_PG_RSVD,
  1899. PCI_DMA_FROMDEVICE);
  1900. (*sd->pg_chunk.p_cnt)--;
  1901. if (!*sd->pg_chunk.p_cnt && sd->pg_chunk.page != fl->pg_chunk.page)
  1902. pci_unmap_page(adap->pdev,
  1903. sd->pg_chunk.mapping,
  1904. fl->alloc_size,
  1905. PCI_DMA_FROMDEVICE);
  1906. if (!skb) {
  1907. put_page(sd->pg_chunk.page);
  1908. if (complete)
  1909. qs->nomem = 0;
  1910. return;
  1911. }
  1912. rx_frag = skb_shinfo(skb)->frags;
  1913. nr_frags = skb_shinfo(skb)->nr_frags;
  1914. if (!nr_frags) {
  1915. offset = 2 + sizeof(struct cpl_rx_pkt);
  1916. cpl = qs->lro_va = sd->pg_chunk.va + 2;
  1917. if ((qs->netdev->features & NETIF_F_RXCSUM) &&
  1918. cpl->csum_valid && cpl->csum == htons(0xffff)) {
  1919. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1920. qs->port_stats[SGE_PSTAT_RX_CSUM_GOOD]++;
  1921. } else
  1922. skb->ip_summed = CHECKSUM_NONE;
  1923. } else
  1924. cpl = qs->lro_va;
  1925. len -= offset;
  1926. rx_frag += nr_frags;
  1927. __skb_frag_set_page(rx_frag, sd->pg_chunk.page);
  1928. rx_frag->page_offset = sd->pg_chunk.offset + offset;
  1929. skb_frag_size_set(rx_frag, len);
  1930. skb->len += len;
  1931. skb->data_len += len;
  1932. skb->truesize += len;
  1933. skb_shinfo(skb)->nr_frags++;
  1934. if (!complete)
  1935. return;
  1936. skb_record_rx_queue(skb, qs - &adap->sge.qs[pi->first_qset]);
  1937. if (cpl->vlan_valid) {
  1938. qs->port_stats[SGE_PSTAT_VLANEX]++;
  1939. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(cpl->vlan));
  1940. }
  1941. napi_gro_frags(&qs->napi);
  1942. }
  1943. /**
  1944. * handle_rsp_cntrl_info - handles control information in a response
  1945. * @qs: the queue set corresponding to the response
  1946. * @flags: the response control flags
  1947. *
  1948. * Handles the control information of an SGE response, such as GTS
  1949. * indications and completion credits for the queue set's Tx queues.
  1950. * HW coalesces credits, we don't do any extra SW coalescing.
  1951. */
  1952. static inline void handle_rsp_cntrl_info(struct sge_qset *qs, u32 flags)
  1953. {
  1954. unsigned int credits;
  1955. #if USE_GTS
  1956. if (flags & F_RSPD_TXQ0_GTS)
  1957. clear_bit(TXQ_RUNNING, &qs->txq[TXQ_ETH].flags);
  1958. #endif
  1959. credits = G_RSPD_TXQ0_CR(flags);
  1960. if (credits)
  1961. qs->txq[TXQ_ETH].processed += credits;
  1962. credits = G_RSPD_TXQ2_CR(flags);
  1963. if (credits)
  1964. qs->txq[TXQ_CTRL].processed += credits;
  1965. # if USE_GTS
  1966. if (flags & F_RSPD_TXQ1_GTS)
  1967. clear_bit(TXQ_RUNNING, &qs->txq[TXQ_OFLD].flags);
  1968. # endif
  1969. credits = G_RSPD_TXQ1_CR(flags);
  1970. if (credits)
  1971. qs->txq[TXQ_OFLD].processed += credits;
  1972. }
  1973. /**
  1974. * check_ring_db - check if we need to ring any doorbells
  1975. * @adapter: the adapter
  1976. * @qs: the queue set whose Tx queues are to be examined
  1977. * @sleeping: indicates which Tx queue sent GTS
  1978. *
  1979. * Checks if some of a queue set's Tx queues need to ring their doorbells
  1980. * to resume transmission after idling while they still have unprocessed
  1981. * descriptors.
  1982. */
  1983. static void check_ring_db(struct adapter *adap, struct sge_qset *qs,
  1984. unsigned int sleeping)
  1985. {
  1986. if (sleeping & F_RSPD_TXQ0_GTS) {
  1987. struct sge_txq *txq = &qs->txq[TXQ_ETH];
  1988. if (txq->cleaned + txq->in_use != txq->processed &&
  1989. !test_and_set_bit(TXQ_LAST_PKT_DB, &txq->flags)) {
  1990. set_bit(TXQ_RUNNING, &txq->flags);
  1991. t3_write_reg(adap, A_SG_KDOORBELL, F_SELEGRCNTX |
  1992. V_EGRCNTX(txq->cntxt_id));
  1993. }
  1994. }
  1995. if (sleeping & F_RSPD_TXQ1_GTS) {
  1996. struct sge_txq *txq = &qs->txq[TXQ_OFLD];
  1997. if (txq->cleaned + txq->in_use != txq->processed &&
  1998. !test_and_set_bit(TXQ_LAST_PKT_DB, &txq->flags)) {
  1999. set_bit(TXQ_RUNNING, &txq->flags);
  2000. t3_write_reg(adap, A_SG_KDOORBELL, F_SELEGRCNTX |
  2001. V_EGRCNTX(txq->cntxt_id));
  2002. }
  2003. }
  2004. }
  2005. /**
  2006. * is_new_response - check if a response is newly written
  2007. * @r: the response descriptor
  2008. * @q: the response queue
  2009. *
  2010. * Returns true if a response descriptor contains a yet unprocessed
  2011. * response.
  2012. */
  2013. static inline int is_new_response(const struct rsp_desc *r,
  2014. const struct sge_rspq *q)
  2015. {
  2016. return (r->intr_gen & F_RSPD_GEN2) == q->gen;
  2017. }
  2018. static inline void clear_rspq_bufstate(struct sge_rspq * const q)
  2019. {
  2020. q->pg_skb = NULL;
  2021. q->rx_recycle_buf = 0;
  2022. }
  2023. #define RSPD_GTS_MASK (F_RSPD_TXQ0_GTS | F_RSPD_TXQ1_GTS)
  2024. #define RSPD_CTRL_MASK (RSPD_GTS_MASK | \
  2025. V_RSPD_TXQ0_CR(M_RSPD_TXQ0_CR) | \
  2026. V_RSPD_TXQ1_CR(M_RSPD_TXQ1_CR) | \
  2027. V_RSPD_TXQ2_CR(M_RSPD_TXQ2_CR))
  2028. /* How long to delay the next interrupt in case of memory shortage, in 0.1us. */
  2029. #define NOMEM_INTR_DELAY 2500
  2030. /**
  2031. * process_responses - process responses from an SGE response queue
  2032. * @adap: the adapter
  2033. * @qs: the queue set to which the response queue belongs
  2034. * @budget: how many responses can be processed in this round
  2035. *
  2036. * Process responses from an SGE response queue up to the supplied budget.
  2037. * Responses include received packets as well as credits and other events
  2038. * for the queues that belong to the response queue's queue set.
  2039. * A negative budget is effectively unlimited.
  2040. *
  2041. * Additionally choose the interrupt holdoff time for the next interrupt
  2042. * on this queue. If the system is under memory shortage use a fairly
  2043. * long delay to help recovery.
  2044. */
  2045. static int process_responses(struct adapter *adap, struct sge_qset *qs,
  2046. int budget)
  2047. {
  2048. struct sge_rspq *q = &qs->rspq;
  2049. struct rsp_desc *r = &q->desc[q->cidx];
  2050. int budget_left = budget;
  2051. unsigned int sleeping = 0;
  2052. struct sk_buff *offload_skbs[RX_BUNDLE_SIZE];
  2053. int ngathered = 0;
  2054. q->next_holdoff = q->holdoff_tmr;
  2055. while (likely(budget_left && is_new_response(r, q))) {
  2056. int packet_complete, eth, ethpad = 2;
  2057. int lro = !!(qs->netdev->features & NETIF_F_GRO);
  2058. struct sk_buff *skb = NULL;
  2059. u32 len, flags;
  2060. __be32 rss_hi, rss_lo;
  2061. rmb();
  2062. eth = r->rss_hdr.opcode == CPL_RX_PKT;
  2063. rss_hi = *(const __be32 *)r;
  2064. rss_lo = r->rss_hdr.rss_hash_val;
  2065. flags = ntohl(r->flags);
  2066. if (unlikely(flags & F_RSPD_ASYNC_NOTIF)) {
  2067. skb = alloc_skb(AN_PKT_SIZE, GFP_ATOMIC);
  2068. if (!skb)
  2069. goto no_mem;
  2070. memcpy(__skb_put(skb, AN_PKT_SIZE), r, AN_PKT_SIZE);
  2071. skb->data[0] = CPL_ASYNC_NOTIF;
  2072. rss_hi = htonl(CPL_ASYNC_NOTIF << 24);
  2073. q->async_notif++;
  2074. } else if (flags & F_RSPD_IMM_DATA_VALID) {
  2075. skb = get_imm_packet(r);
  2076. if (unlikely(!skb)) {
  2077. no_mem:
  2078. q->next_holdoff = NOMEM_INTR_DELAY;
  2079. q->nomem++;
  2080. /* consume one credit since we tried */
  2081. budget_left--;
  2082. break;
  2083. }
  2084. q->imm_data++;
  2085. ethpad = 0;
  2086. } else if ((len = ntohl(r->len_cq)) != 0) {
  2087. struct sge_fl *fl;
  2088. lro &= eth && is_eth_tcp(rss_hi);
  2089. fl = (len & F_RSPD_FLQ) ? &qs->fl[1] : &qs->fl[0];
  2090. if (fl->use_pages) {
  2091. void *addr = fl->sdesc[fl->cidx].pg_chunk.va;
  2092. prefetch(addr);
  2093. #if L1_CACHE_BYTES < 128
  2094. prefetch(addr + L1_CACHE_BYTES);
  2095. #endif
  2096. __refill_fl(adap, fl);
  2097. if (lro > 0) {
  2098. lro_add_page(adap, qs, fl,
  2099. G_RSPD_LEN(len),
  2100. flags & F_RSPD_EOP);
  2101. goto next_fl;
  2102. }
  2103. skb = get_packet_pg(adap, fl, q,
  2104. G_RSPD_LEN(len),
  2105. eth ?
  2106. SGE_RX_DROP_THRES : 0);
  2107. q->pg_skb = skb;
  2108. } else
  2109. skb = get_packet(adap, fl, G_RSPD_LEN(len),
  2110. eth ? SGE_RX_DROP_THRES : 0);
  2111. if (unlikely(!skb)) {
  2112. if (!eth)
  2113. goto no_mem;
  2114. q->rx_drops++;
  2115. } else if (unlikely(r->rss_hdr.opcode == CPL_TRACE_PKT))
  2116. __skb_pull(skb, 2);
  2117. next_fl:
  2118. if (++fl->cidx == fl->size)
  2119. fl->cidx = 0;
  2120. } else
  2121. q->pure_rsps++;
  2122. if (flags & RSPD_CTRL_MASK) {
  2123. sleeping |= flags & RSPD_GTS_MASK;
  2124. handle_rsp_cntrl_info(qs, flags);
  2125. }
  2126. r++;
  2127. if (unlikely(++q->cidx == q->size)) {
  2128. q->cidx = 0;
  2129. q->gen ^= 1;
  2130. r = q->desc;
  2131. }
  2132. prefetch(r);
  2133. if (++q->credits >= (q->size / 4)) {
  2134. refill_rspq(adap, q, q->credits);
  2135. q->credits = 0;
  2136. }
  2137. packet_complete = flags &
  2138. (F_RSPD_EOP | F_RSPD_IMM_DATA_VALID |
  2139. F_RSPD_ASYNC_NOTIF);
  2140. if (skb != NULL && packet_complete) {
  2141. if (eth)
  2142. rx_eth(adap, q, skb, ethpad, lro);
  2143. else {
  2144. q->offload_pkts++;
  2145. /* Preserve the RSS info in csum & priority */
  2146. skb->csum = rss_hi;
  2147. skb->priority = rss_lo;
  2148. ngathered = rx_offload(&adap->tdev, q, skb,
  2149. offload_skbs,
  2150. ngathered);
  2151. }
  2152. if (flags & F_RSPD_EOP)
  2153. clear_rspq_bufstate(q);
  2154. }
  2155. --budget_left;
  2156. }
  2157. deliver_partial_bundle(&adap->tdev, q, offload_skbs, ngathered);
  2158. if (sleeping)
  2159. check_ring_db(adap, qs, sleeping);
  2160. smp_mb(); /* commit Tx queue .processed updates */
  2161. if (unlikely(qs->txq_stopped != 0))
  2162. restart_tx(qs);
  2163. budget -= budget_left;
  2164. return budget;
  2165. }
  2166. static inline int is_pure_response(const struct rsp_desc *r)
  2167. {
  2168. __be32 n = r->flags & htonl(F_RSPD_ASYNC_NOTIF | F_RSPD_IMM_DATA_VALID);
  2169. return (n | r->len_cq) == 0;
  2170. }
  2171. /**
  2172. * napi_rx_handler - the NAPI handler for Rx processing
  2173. * @napi: the napi instance
  2174. * @budget: how many packets we can process in this round
  2175. *
  2176. * Handler for new data events when using NAPI.
  2177. */
  2178. static int napi_rx_handler(struct napi_struct *napi, int budget)
  2179. {
  2180. struct sge_qset *qs = container_of(napi, struct sge_qset, napi);
  2181. struct adapter *adap = qs->adap;
  2182. int work_done = process_responses(adap, qs, budget);
  2183. if (likely(work_done < budget)) {
  2184. napi_complete(napi);
  2185. /*
  2186. * Because we don't atomically flush the following
  2187. * write it is possible that in very rare cases it can
  2188. * reach the device in a way that races with a new
  2189. * response being written plus an error interrupt
  2190. * causing the NAPI interrupt handler below to return
  2191. * unhandled status to the OS. To protect against
  2192. * this would require flushing the write and doing
  2193. * both the write and the flush with interrupts off.
  2194. * Way too expensive and unjustifiable given the
  2195. * rarity of the race.
  2196. *
  2197. * The race cannot happen at all with MSI-X.
  2198. */
  2199. t3_write_reg(adap, A_SG_GTS, V_RSPQ(qs->rspq.cntxt_id) |
  2200. V_NEWTIMER(qs->rspq.next_holdoff) |
  2201. V_NEWINDEX(qs->rspq.cidx));
  2202. }
  2203. return work_done;
  2204. }
  2205. /*
  2206. * Returns true if the device is already scheduled for polling.
  2207. */
  2208. static inline int napi_is_scheduled(struct napi_struct *napi)
  2209. {
  2210. return test_bit(NAPI_STATE_SCHED, &napi->state);
  2211. }
  2212. /**
  2213. * process_pure_responses - process pure responses from a response queue
  2214. * @adap: the adapter
  2215. * @qs: the queue set owning the response queue
  2216. * @r: the first pure response to process
  2217. *
  2218. * A simpler version of process_responses() that handles only pure (i.e.,
  2219. * non data-carrying) responses. Such respones are too light-weight to
  2220. * justify calling a softirq under NAPI, so we handle them specially in
  2221. * the interrupt handler. The function is called with a pointer to a
  2222. * response, which the caller must ensure is a valid pure response.
  2223. *
  2224. * Returns 1 if it encounters a valid data-carrying response, 0 otherwise.
  2225. */
  2226. static int process_pure_responses(struct adapter *adap, struct sge_qset *qs,
  2227. struct rsp_desc *r)
  2228. {
  2229. struct sge_rspq *q = &qs->rspq;
  2230. unsigned int sleeping = 0;
  2231. do {
  2232. u32 flags = ntohl(r->flags);
  2233. r++;
  2234. if (unlikely(++q->cidx == q->size)) {
  2235. q->cidx = 0;
  2236. q->gen ^= 1;
  2237. r = q->desc;
  2238. }
  2239. prefetch(r);
  2240. if (flags & RSPD_CTRL_MASK) {
  2241. sleeping |= flags & RSPD_GTS_MASK;
  2242. handle_rsp_cntrl_info(qs, flags);
  2243. }
  2244. q->pure_rsps++;
  2245. if (++q->credits >= (q->size / 4)) {
  2246. refill_rspq(adap, q, q->credits);
  2247. q->credits = 0;
  2248. }
  2249. if (!is_new_response(r, q))
  2250. break;
  2251. rmb();
  2252. } while (is_pure_response(r));
  2253. if (sleeping)
  2254. check_ring_db(adap, qs, sleeping);
  2255. smp_mb(); /* commit Tx queue .processed updates */
  2256. if (unlikely(qs->txq_stopped != 0))
  2257. restart_tx(qs);
  2258. return is_new_response(r, q);
  2259. }
  2260. /**
  2261. * handle_responses - decide what to do with new responses in NAPI mode
  2262. * @adap: the adapter
  2263. * @q: the response queue
  2264. *
  2265. * This is used by the NAPI interrupt handlers to decide what to do with
  2266. * new SGE responses. If there are no new responses it returns -1. If
  2267. * there are new responses and they are pure (i.e., non-data carrying)
  2268. * it handles them straight in hard interrupt context as they are very
  2269. * cheap and don't deliver any packets. Finally, if there are any data
  2270. * signaling responses it schedules the NAPI handler. Returns 1 if it
  2271. * schedules NAPI, 0 if all new responses were pure.
  2272. *
  2273. * The caller must ascertain NAPI is not already running.
  2274. */
  2275. static inline int handle_responses(struct adapter *adap, struct sge_rspq *q)
  2276. {
  2277. struct sge_qset *qs = rspq_to_qset(q);
  2278. struct rsp_desc *r = &q->desc[q->cidx];
  2279. if (!is_new_response(r, q))
  2280. return -1;
  2281. rmb();
  2282. if (is_pure_response(r) && process_pure_responses(adap, qs, r) == 0) {
  2283. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q->cntxt_id) |
  2284. V_NEWTIMER(q->holdoff_tmr) | V_NEWINDEX(q->cidx));
  2285. return 0;
  2286. }
  2287. napi_schedule(&qs->napi);
  2288. return 1;
  2289. }
  2290. /*
  2291. * The MSI-X interrupt handler for an SGE response queue for the non-NAPI case
  2292. * (i.e., response queue serviced in hard interrupt).
  2293. */
  2294. static irqreturn_t t3_sge_intr_msix(int irq, void *cookie)
  2295. {
  2296. struct sge_qset *qs = cookie;
  2297. struct adapter *adap = qs->adap;
  2298. struct sge_rspq *q = &qs->rspq;
  2299. spin_lock(&q->lock);
  2300. if (process_responses(adap, qs, -1) == 0)
  2301. q->unhandled_irqs++;
  2302. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q->cntxt_id) |
  2303. V_NEWTIMER(q->next_holdoff) | V_NEWINDEX(q->cidx));
  2304. spin_unlock(&q->lock);
  2305. return IRQ_HANDLED;
  2306. }
  2307. /*
  2308. * The MSI-X interrupt handler for an SGE response queue for the NAPI case
  2309. * (i.e., response queue serviced by NAPI polling).
  2310. */
  2311. static irqreturn_t t3_sge_intr_msix_napi(int irq, void *cookie)
  2312. {
  2313. struct sge_qset *qs = cookie;
  2314. struct sge_rspq *q = &qs->rspq;
  2315. spin_lock(&q->lock);
  2316. if (handle_responses(qs->adap, q) < 0)
  2317. q->unhandled_irqs++;
  2318. spin_unlock(&q->lock);
  2319. return IRQ_HANDLED;
  2320. }
  2321. /*
  2322. * The non-NAPI MSI interrupt handler. This needs to handle data events from
  2323. * SGE response queues as well as error and other async events as they all use
  2324. * the same MSI vector. We use one SGE response queue per port in this mode
  2325. * and protect all response queues with queue 0's lock.
  2326. */
  2327. static irqreturn_t t3_intr_msi(int irq, void *cookie)
  2328. {
  2329. int new_packets = 0;
  2330. struct adapter *adap = cookie;
  2331. struct sge_rspq *q = &adap->sge.qs[0].rspq;
  2332. spin_lock(&q->lock);
  2333. if (process_responses(adap, &adap->sge.qs[0], -1)) {
  2334. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q->cntxt_id) |
  2335. V_NEWTIMER(q->next_holdoff) | V_NEWINDEX(q->cidx));
  2336. new_packets = 1;
  2337. }
  2338. if (adap->params.nports == 2 &&
  2339. process_responses(adap, &adap->sge.qs[1], -1)) {
  2340. struct sge_rspq *q1 = &adap->sge.qs[1].rspq;
  2341. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q1->cntxt_id) |
  2342. V_NEWTIMER(q1->next_holdoff) |
  2343. V_NEWINDEX(q1->cidx));
  2344. new_packets = 1;
  2345. }
  2346. if (!new_packets && t3_slow_intr_handler(adap) == 0)
  2347. q->unhandled_irqs++;
  2348. spin_unlock(&q->lock);
  2349. return IRQ_HANDLED;
  2350. }
  2351. static int rspq_check_napi(struct sge_qset *qs)
  2352. {
  2353. struct sge_rspq *q = &qs->rspq;
  2354. if (!napi_is_scheduled(&qs->napi) &&
  2355. is_new_response(&q->desc[q->cidx], q)) {
  2356. napi_schedule(&qs->napi);
  2357. return 1;
  2358. }
  2359. return 0;
  2360. }
  2361. /*
  2362. * The MSI interrupt handler for the NAPI case (i.e., response queues serviced
  2363. * by NAPI polling). Handles data events from SGE response queues as well as
  2364. * error and other async events as they all use the same MSI vector. We use
  2365. * one SGE response queue per port in this mode and protect all response
  2366. * queues with queue 0's lock.
  2367. */
  2368. static irqreturn_t t3_intr_msi_napi(int irq, void *cookie)
  2369. {
  2370. int new_packets;
  2371. struct adapter *adap = cookie;
  2372. struct sge_rspq *q = &adap->sge.qs[0].rspq;
  2373. spin_lock(&q->lock);
  2374. new_packets = rspq_check_napi(&adap->sge.qs[0]);
  2375. if (adap->params.nports == 2)
  2376. new_packets += rspq_check_napi(&adap->sge.qs[1]);
  2377. if (!new_packets && t3_slow_intr_handler(adap) == 0)
  2378. q->unhandled_irqs++;
  2379. spin_unlock(&q->lock);
  2380. return IRQ_HANDLED;
  2381. }
  2382. /*
  2383. * A helper function that processes responses and issues GTS.
  2384. */
  2385. static inline int process_responses_gts(struct adapter *adap,
  2386. struct sge_rspq *rq)
  2387. {
  2388. int work;
  2389. work = process_responses(adap, rspq_to_qset(rq), -1);
  2390. t3_write_reg(adap, A_SG_GTS, V_RSPQ(rq->cntxt_id) |
  2391. V_NEWTIMER(rq->next_holdoff) | V_NEWINDEX(rq->cidx));
  2392. return work;
  2393. }
  2394. /*
  2395. * The legacy INTx interrupt handler. This needs to handle data events from
  2396. * SGE response queues as well as error and other async events as they all use
  2397. * the same interrupt pin. We use one SGE response queue per port in this mode
  2398. * and protect all response queues with queue 0's lock.
  2399. */
  2400. static irqreturn_t t3_intr(int irq, void *cookie)
  2401. {
  2402. int work_done, w0, w1;
  2403. struct adapter *adap = cookie;
  2404. struct sge_rspq *q0 = &adap->sge.qs[0].rspq;
  2405. struct sge_rspq *q1 = &adap->sge.qs[1].rspq;
  2406. spin_lock(&q0->lock);
  2407. w0 = is_new_response(&q0->desc[q0->cidx], q0);
  2408. w1 = adap->params.nports == 2 &&
  2409. is_new_response(&q1->desc[q1->cidx], q1);
  2410. if (likely(w0 | w1)) {
  2411. t3_write_reg(adap, A_PL_CLI, 0);
  2412. t3_read_reg(adap, A_PL_CLI); /* flush */
  2413. if (likely(w0))
  2414. process_responses_gts(adap, q0);
  2415. if (w1)
  2416. process_responses_gts(adap, q1);
  2417. work_done = w0 | w1;
  2418. } else
  2419. work_done = t3_slow_intr_handler(adap);
  2420. spin_unlock(&q0->lock);
  2421. return IRQ_RETVAL(work_done != 0);
  2422. }
  2423. /*
  2424. * Interrupt handler for legacy INTx interrupts for T3B-based cards.
  2425. * Handles data events from SGE response queues as well as error and other
  2426. * async events as they all use the same interrupt pin. We use one SGE
  2427. * response queue per port in this mode and protect all response queues with
  2428. * queue 0's lock.
  2429. */
  2430. static irqreturn_t t3b_intr(int irq, void *cookie)
  2431. {
  2432. u32 map;
  2433. struct adapter *adap = cookie;
  2434. struct sge_rspq *q0 = &adap->sge.qs[0].rspq;
  2435. t3_write_reg(adap, A_PL_CLI, 0);
  2436. map = t3_read_reg(adap, A_SG_DATA_INTR);
  2437. if (unlikely(!map)) /* shared interrupt, most likely */
  2438. return IRQ_NONE;
  2439. spin_lock(&q0->lock);
  2440. if (unlikely(map & F_ERRINTR))
  2441. t3_slow_intr_handler(adap);
  2442. if (likely(map & 1))
  2443. process_responses_gts(adap, q0);
  2444. if (map & 2)
  2445. process_responses_gts(adap, &adap->sge.qs[1].rspq);
  2446. spin_unlock(&q0->lock);
  2447. return IRQ_HANDLED;
  2448. }
  2449. /*
  2450. * NAPI interrupt handler for legacy INTx interrupts for T3B-based cards.
  2451. * Handles data events from SGE response queues as well as error and other
  2452. * async events as they all use the same interrupt pin. We use one SGE
  2453. * response queue per port in this mode and protect all response queues with
  2454. * queue 0's lock.
  2455. */
  2456. static irqreturn_t t3b_intr_napi(int irq, void *cookie)
  2457. {
  2458. u32 map;
  2459. struct adapter *adap = cookie;
  2460. struct sge_qset *qs0 = &adap->sge.qs[0];
  2461. struct sge_rspq *q0 = &qs0->rspq;
  2462. t3_write_reg(adap, A_PL_CLI, 0);
  2463. map = t3_read_reg(adap, A_SG_DATA_INTR);
  2464. if (unlikely(!map)) /* shared interrupt, most likely */
  2465. return IRQ_NONE;
  2466. spin_lock(&q0->lock);
  2467. if (unlikely(map & F_ERRINTR))
  2468. t3_slow_intr_handler(adap);
  2469. if (likely(map & 1))
  2470. napi_schedule(&qs0->napi);
  2471. if (map & 2)
  2472. napi_schedule(&adap->sge.qs[1].napi);
  2473. spin_unlock(&q0->lock);
  2474. return IRQ_HANDLED;
  2475. }
  2476. /**
  2477. * t3_intr_handler - select the top-level interrupt handler
  2478. * @adap: the adapter
  2479. * @polling: whether using NAPI to service response queues
  2480. *
  2481. * Selects the top-level interrupt handler based on the type of interrupts
  2482. * (MSI-X, MSI, or legacy) and whether NAPI will be used to service the
  2483. * response queues.
  2484. */
  2485. irq_handler_t t3_intr_handler(struct adapter *adap, int polling)
  2486. {
  2487. if (adap->flags & USING_MSIX)
  2488. return polling ? t3_sge_intr_msix_napi : t3_sge_intr_msix;
  2489. if (adap->flags & USING_MSI)
  2490. return polling ? t3_intr_msi_napi : t3_intr_msi;
  2491. if (adap->params.rev > 0)
  2492. return polling ? t3b_intr_napi : t3b_intr;
  2493. return t3_intr;
  2494. }
  2495. #define SGE_PARERR (F_CPPARITYERROR | F_OCPARITYERROR | F_RCPARITYERROR | \
  2496. F_IRPARITYERROR | V_ITPARITYERROR(M_ITPARITYERROR) | \
  2497. V_FLPARITYERROR(M_FLPARITYERROR) | F_LODRBPARITYERROR | \
  2498. F_HIDRBPARITYERROR | F_LORCQPARITYERROR | \
  2499. F_HIRCQPARITYERROR)
  2500. #define SGE_FRAMINGERR (F_UC_REQ_FRAMINGERROR | F_R_REQ_FRAMINGERROR)
  2501. #define SGE_FATALERR (SGE_PARERR | SGE_FRAMINGERR | F_RSPQCREDITOVERFOW | \
  2502. F_RSPQDISABLED)
  2503. /**
  2504. * t3_sge_err_intr_handler - SGE async event interrupt handler
  2505. * @adapter: the adapter
  2506. *
  2507. * Interrupt handler for SGE asynchronous (non-data) events.
  2508. */
  2509. void t3_sge_err_intr_handler(struct adapter *adapter)
  2510. {
  2511. unsigned int v, status = t3_read_reg(adapter, A_SG_INT_CAUSE) &
  2512. ~F_FLEMPTY;
  2513. if (status & SGE_PARERR)
  2514. CH_ALERT(adapter, "SGE parity error (0x%x)\n",
  2515. status & SGE_PARERR);
  2516. if (status & SGE_FRAMINGERR)
  2517. CH_ALERT(adapter, "SGE framing error (0x%x)\n",
  2518. status & SGE_FRAMINGERR);
  2519. if (status & F_RSPQCREDITOVERFOW)
  2520. CH_ALERT(adapter, "SGE response queue credit overflow\n");
  2521. if (status & F_RSPQDISABLED) {
  2522. v = t3_read_reg(adapter, A_SG_RSPQ_FL_STATUS);
  2523. CH_ALERT(adapter,
  2524. "packet delivered to disabled response queue "
  2525. "(0x%x)\n", (v >> S_RSPQ0DISABLED) & 0xff);
  2526. }
  2527. if (status & (F_HIPIODRBDROPERR | F_LOPIODRBDROPERR))
  2528. queue_work(cxgb3_wq, &adapter->db_drop_task);
  2529. if (status & (F_HIPRIORITYDBFULL | F_LOPRIORITYDBFULL))
  2530. queue_work(cxgb3_wq, &adapter->db_full_task);
  2531. if (status & (F_HIPRIORITYDBEMPTY | F_LOPRIORITYDBEMPTY))
  2532. queue_work(cxgb3_wq, &adapter->db_empty_task);
  2533. t3_write_reg(adapter, A_SG_INT_CAUSE, status);
  2534. if (status & SGE_FATALERR)
  2535. t3_fatal_err(adapter);
  2536. }
  2537. /**
  2538. * sge_timer_tx - perform periodic maintenance of an SGE qset
  2539. * @data: the SGE queue set to maintain
  2540. *
  2541. * Runs periodically from a timer to perform maintenance of an SGE queue
  2542. * set. It performs two tasks:
  2543. *
  2544. * Cleans up any completed Tx descriptors that may still be pending.
  2545. * Normal descriptor cleanup happens when new packets are added to a Tx
  2546. * queue so this timer is relatively infrequent and does any cleanup only
  2547. * if the Tx queue has not seen any new packets in a while. We make a
  2548. * best effort attempt to reclaim descriptors, in that we don't wait
  2549. * around if we cannot get a queue's lock (which most likely is because
  2550. * someone else is queueing new packets and so will also handle the clean
  2551. * up). Since control queues use immediate data exclusively we don't
  2552. * bother cleaning them up here.
  2553. *
  2554. */
  2555. static void sge_timer_tx(unsigned long data)
  2556. {
  2557. struct sge_qset *qs = (struct sge_qset *)data;
  2558. struct port_info *pi = netdev_priv(qs->netdev);
  2559. struct adapter *adap = pi->adapter;
  2560. unsigned int tbd[SGE_TXQ_PER_SET] = {0, 0};
  2561. unsigned long next_period;
  2562. if (__netif_tx_trylock(qs->tx_q)) {
  2563. tbd[TXQ_ETH] = reclaim_completed_tx(adap, &qs->txq[TXQ_ETH],
  2564. TX_RECLAIM_TIMER_CHUNK);
  2565. __netif_tx_unlock(qs->tx_q);
  2566. }
  2567. if (spin_trylock(&qs->txq[TXQ_OFLD].lock)) {
  2568. tbd[TXQ_OFLD] = reclaim_completed_tx(adap, &qs->txq[TXQ_OFLD],
  2569. TX_RECLAIM_TIMER_CHUNK);
  2570. spin_unlock(&qs->txq[TXQ_OFLD].lock);
  2571. }
  2572. next_period = TX_RECLAIM_PERIOD >>
  2573. (max(tbd[TXQ_ETH], tbd[TXQ_OFLD]) /
  2574. TX_RECLAIM_TIMER_CHUNK);
  2575. mod_timer(&qs->tx_reclaim_timer, jiffies + next_period);
  2576. }
  2577. /**
  2578. * sge_timer_rx - perform periodic maintenance of an SGE qset
  2579. * @data: the SGE queue set to maintain
  2580. *
  2581. * a) Replenishes Rx queues that have run out due to memory shortage.
  2582. * Normally new Rx buffers are added when existing ones are consumed but
  2583. * when out of memory a queue can become empty. We try to add only a few
  2584. * buffers here, the queue will be replenished fully as these new buffers
  2585. * are used up if memory shortage has subsided.
  2586. *
  2587. * b) Return coalesced response queue credits in case a response queue is
  2588. * starved.
  2589. *
  2590. */
  2591. static void sge_timer_rx(unsigned long data)
  2592. {
  2593. spinlock_t *lock;
  2594. struct sge_qset *qs = (struct sge_qset *)data;
  2595. struct port_info *pi = netdev_priv(qs->netdev);
  2596. struct adapter *adap = pi->adapter;
  2597. u32 status;
  2598. lock = adap->params.rev > 0 ?
  2599. &qs->rspq.lock : &adap->sge.qs[0].rspq.lock;
  2600. if (!spin_trylock_irq(lock))
  2601. goto out;
  2602. if (napi_is_scheduled(&qs->napi))
  2603. goto unlock;
  2604. if (adap->params.rev < 4) {
  2605. status = t3_read_reg(adap, A_SG_RSPQ_FL_STATUS);
  2606. if (status & (1 << qs->rspq.cntxt_id)) {
  2607. qs->rspq.starved++;
  2608. if (qs->rspq.credits) {
  2609. qs->rspq.credits--;
  2610. refill_rspq(adap, &qs->rspq, 1);
  2611. qs->rspq.restarted++;
  2612. t3_write_reg(adap, A_SG_RSPQ_FL_STATUS,
  2613. 1 << qs->rspq.cntxt_id);
  2614. }
  2615. }
  2616. }
  2617. if (qs->fl[0].credits < qs->fl[0].size)
  2618. __refill_fl(adap, &qs->fl[0]);
  2619. if (qs->fl[1].credits < qs->fl[1].size)
  2620. __refill_fl(adap, &qs->fl[1]);
  2621. unlock:
  2622. spin_unlock_irq(lock);
  2623. out:
  2624. mod_timer(&qs->rx_reclaim_timer, jiffies + RX_RECLAIM_PERIOD);
  2625. }
  2626. /**
  2627. * t3_update_qset_coalesce - update coalescing settings for a queue set
  2628. * @qs: the SGE queue set
  2629. * @p: new queue set parameters
  2630. *
  2631. * Update the coalescing settings for an SGE queue set. Nothing is done
  2632. * if the queue set is not initialized yet.
  2633. */
  2634. void t3_update_qset_coalesce(struct sge_qset *qs, const struct qset_params *p)
  2635. {
  2636. qs->rspq.holdoff_tmr = max(p->coalesce_usecs * 10, 1U);/* can't be 0 */
  2637. qs->rspq.polling = p->polling;
  2638. qs->napi.poll = p->polling ? napi_rx_handler : ofld_poll;
  2639. }
  2640. /**
  2641. * t3_sge_alloc_qset - initialize an SGE queue set
  2642. * @adapter: the adapter
  2643. * @id: the queue set id
  2644. * @nports: how many Ethernet ports will be using this queue set
  2645. * @irq_vec_idx: the IRQ vector index for response queue interrupts
  2646. * @p: configuration parameters for this queue set
  2647. * @ntxq: number of Tx queues for the queue set
  2648. * @netdev: net device associated with this queue set
  2649. * @netdevq: net device TX queue associated with this queue set
  2650. *
  2651. * Allocate resources and initialize an SGE queue set. A queue set
  2652. * comprises a response queue, two Rx free-buffer queues, and up to 3
  2653. * Tx queues. The Tx queues are assigned roles in the order Ethernet
  2654. * queue, offload queue, and control queue.
  2655. */
  2656. int t3_sge_alloc_qset(struct adapter *adapter, unsigned int id, int nports,
  2657. int irq_vec_idx, const struct qset_params *p,
  2658. int ntxq, struct net_device *dev,
  2659. struct netdev_queue *netdevq)
  2660. {
  2661. int i, avail, ret = -ENOMEM;
  2662. struct sge_qset *q = &adapter->sge.qs[id];
  2663. init_qset_cntxt(q, id);
  2664. setup_timer(&q->tx_reclaim_timer, sge_timer_tx, (unsigned long)q);
  2665. setup_timer(&q->rx_reclaim_timer, sge_timer_rx, (unsigned long)q);
  2666. q->fl[0].desc = alloc_ring(adapter->pdev, p->fl_size,
  2667. sizeof(struct rx_desc),
  2668. sizeof(struct rx_sw_desc),
  2669. &q->fl[0].phys_addr, &q->fl[0].sdesc);
  2670. if (!q->fl[0].desc)
  2671. goto err;
  2672. q->fl[1].desc = alloc_ring(adapter->pdev, p->jumbo_size,
  2673. sizeof(struct rx_desc),
  2674. sizeof(struct rx_sw_desc),
  2675. &q->fl[1].phys_addr, &q->fl[1].sdesc);
  2676. if (!q->fl[1].desc)
  2677. goto err;
  2678. q->rspq.desc = alloc_ring(adapter->pdev, p->rspq_size,
  2679. sizeof(struct rsp_desc), 0,
  2680. &q->rspq.phys_addr, NULL);
  2681. if (!q->rspq.desc)
  2682. goto err;
  2683. for (i = 0; i < ntxq; ++i) {
  2684. /*
  2685. * The control queue always uses immediate data so does not
  2686. * need to keep track of any sk_buffs.
  2687. */
  2688. size_t sz = i == TXQ_CTRL ? 0 : sizeof(struct tx_sw_desc);
  2689. q->txq[i].desc = alloc_ring(adapter->pdev, p->txq_size[i],
  2690. sizeof(struct tx_desc), sz,
  2691. &q->txq[i].phys_addr,
  2692. &q->txq[i].sdesc);
  2693. if (!q->txq[i].desc)
  2694. goto err;
  2695. q->txq[i].gen = 1;
  2696. q->txq[i].size = p->txq_size[i];
  2697. spin_lock_init(&q->txq[i].lock);
  2698. skb_queue_head_init(&q->txq[i].sendq);
  2699. }
  2700. tasklet_init(&q->txq[TXQ_OFLD].qresume_tsk, restart_offloadq,
  2701. (unsigned long)q);
  2702. tasklet_init(&q->txq[TXQ_CTRL].qresume_tsk, restart_ctrlq,
  2703. (unsigned long)q);
  2704. q->fl[0].gen = q->fl[1].gen = 1;
  2705. q->fl[0].size = p->fl_size;
  2706. q->fl[1].size = p->jumbo_size;
  2707. q->rspq.gen = 1;
  2708. q->rspq.size = p->rspq_size;
  2709. spin_lock_init(&q->rspq.lock);
  2710. skb_queue_head_init(&q->rspq.rx_queue);
  2711. q->txq[TXQ_ETH].stop_thres = nports *
  2712. flits_to_desc(sgl_len(MAX_SKB_FRAGS + 1) + 3);
  2713. #if FL0_PG_CHUNK_SIZE > 0
  2714. q->fl[0].buf_size = FL0_PG_CHUNK_SIZE;
  2715. #else
  2716. q->fl[0].buf_size = SGE_RX_SM_BUF_SIZE + sizeof(struct cpl_rx_data);
  2717. #endif
  2718. #if FL1_PG_CHUNK_SIZE > 0
  2719. q->fl[1].buf_size = FL1_PG_CHUNK_SIZE;
  2720. #else
  2721. q->fl[1].buf_size = is_offload(adapter) ?
  2722. (16 * 1024) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) :
  2723. MAX_FRAME_SIZE + 2 + sizeof(struct cpl_rx_pkt);
  2724. #endif
  2725. q->fl[0].use_pages = FL0_PG_CHUNK_SIZE > 0;
  2726. q->fl[1].use_pages = FL1_PG_CHUNK_SIZE > 0;
  2727. q->fl[0].order = FL0_PG_ORDER;
  2728. q->fl[1].order = FL1_PG_ORDER;
  2729. q->fl[0].alloc_size = FL0_PG_ALLOC_SIZE;
  2730. q->fl[1].alloc_size = FL1_PG_ALLOC_SIZE;
  2731. spin_lock_irq(&adapter->sge.reg_lock);
  2732. /* FL threshold comparison uses < */
  2733. ret = t3_sge_init_rspcntxt(adapter, q->rspq.cntxt_id, irq_vec_idx,
  2734. q->rspq.phys_addr, q->rspq.size,
  2735. q->fl[0].buf_size - SGE_PG_RSVD, 1, 0);
  2736. if (ret)
  2737. goto err_unlock;
  2738. for (i = 0; i < SGE_RXQ_PER_SET; ++i) {
  2739. ret = t3_sge_init_flcntxt(adapter, q->fl[i].cntxt_id, 0,
  2740. q->fl[i].phys_addr, q->fl[i].size,
  2741. q->fl[i].buf_size - SGE_PG_RSVD,
  2742. p->cong_thres, 1, 0);
  2743. if (ret)
  2744. goto err_unlock;
  2745. }
  2746. ret = t3_sge_init_ecntxt(adapter, q->txq[TXQ_ETH].cntxt_id, USE_GTS,
  2747. SGE_CNTXT_ETH, id, q->txq[TXQ_ETH].phys_addr,
  2748. q->txq[TXQ_ETH].size, q->txq[TXQ_ETH].token,
  2749. 1, 0);
  2750. if (ret)
  2751. goto err_unlock;
  2752. if (ntxq > 1) {
  2753. ret = t3_sge_init_ecntxt(adapter, q->txq[TXQ_OFLD].cntxt_id,
  2754. USE_GTS, SGE_CNTXT_OFLD, id,
  2755. q->txq[TXQ_OFLD].phys_addr,
  2756. q->txq[TXQ_OFLD].size, 0, 1, 0);
  2757. if (ret)
  2758. goto err_unlock;
  2759. }
  2760. if (ntxq > 2) {
  2761. ret = t3_sge_init_ecntxt(adapter, q->txq[TXQ_CTRL].cntxt_id, 0,
  2762. SGE_CNTXT_CTRL, id,
  2763. q->txq[TXQ_CTRL].phys_addr,
  2764. q->txq[TXQ_CTRL].size,
  2765. q->txq[TXQ_CTRL].token, 1, 0);
  2766. if (ret)
  2767. goto err_unlock;
  2768. }
  2769. spin_unlock_irq(&adapter->sge.reg_lock);
  2770. q->adap = adapter;
  2771. q->netdev = dev;
  2772. q->tx_q = netdevq;
  2773. t3_update_qset_coalesce(q, p);
  2774. avail = refill_fl(adapter, &q->fl[0], q->fl[0].size,
  2775. GFP_KERNEL | __GFP_COMP);
  2776. if (!avail) {
  2777. CH_ALERT(adapter, "free list queue 0 initialization failed\n");
  2778. goto err;
  2779. }
  2780. if (avail < q->fl[0].size)
  2781. CH_WARN(adapter, "free list queue 0 enabled with %d credits\n",
  2782. avail);
  2783. avail = refill_fl(adapter, &q->fl[1], q->fl[1].size,
  2784. GFP_KERNEL | __GFP_COMP);
  2785. if (avail < q->fl[1].size)
  2786. CH_WARN(adapter, "free list queue 1 enabled with %d credits\n",
  2787. avail);
  2788. refill_rspq(adapter, &q->rspq, q->rspq.size - 1);
  2789. t3_write_reg(adapter, A_SG_GTS, V_RSPQ(q->rspq.cntxt_id) |
  2790. V_NEWTIMER(q->rspq.holdoff_tmr));
  2791. return 0;
  2792. err_unlock:
  2793. spin_unlock_irq(&adapter->sge.reg_lock);
  2794. err:
  2795. t3_free_qset(adapter, q);
  2796. return ret;
  2797. }
  2798. /**
  2799. * t3_start_sge_timers - start SGE timer call backs
  2800. * @adap: the adapter
  2801. *
  2802. * Starts each SGE queue set's timer call back
  2803. */
  2804. void t3_start_sge_timers(struct adapter *adap)
  2805. {
  2806. int i;
  2807. for (i = 0; i < SGE_QSETS; ++i) {
  2808. struct sge_qset *q = &adap->sge.qs[i];
  2809. if (q->tx_reclaim_timer.function)
  2810. mod_timer(&q->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
  2811. if (q->rx_reclaim_timer.function)
  2812. mod_timer(&q->rx_reclaim_timer, jiffies + RX_RECLAIM_PERIOD);
  2813. }
  2814. }
  2815. /**
  2816. * t3_stop_sge_timers - stop SGE timer call backs
  2817. * @adap: the adapter
  2818. *
  2819. * Stops each SGE queue set's timer call back
  2820. */
  2821. void t3_stop_sge_timers(struct adapter *adap)
  2822. {
  2823. int i;
  2824. for (i = 0; i < SGE_QSETS; ++i) {
  2825. struct sge_qset *q = &adap->sge.qs[i];
  2826. if (q->tx_reclaim_timer.function)
  2827. del_timer_sync(&q->tx_reclaim_timer);
  2828. if (q->rx_reclaim_timer.function)
  2829. del_timer_sync(&q->rx_reclaim_timer);
  2830. }
  2831. }
  2832. /**
  2833. * t3_free_sge_resources - free SGE resources
  2834. * @adap: the adapter
  2835. *
  2836. * Frees resources used by the SGE queue sets.
  2837. */
  2838. void t3_free_sge_resources(struct adapter *adap)
  2839. {
  2840. int i;
  2841. for (i = 0; i < SGE_QSETS; ++i)
  2842. t3_free_qset(adap, &adap->sge.qs[i]);
  2843. }
  2844. /**
  2845. * t3_sge_start - enable SGE
  2846. * @adap: the adapter
  2847. *
  2848. * Enables the SGE for DMAs. This is the last step in starting packet
  2849. * transfers.
  2850. */
  2851. void t3_sge_start(struct adapter *adap)
  2852. {
  2853. t3_set_reg_field(adap, A_SG_CONTROL, F_GLOBALENABLE, F_GLOBALENABLE);
  2854. }
  2855. /**
  2856. * t3_sge_stop - disable SGE operation
  2857. * @adap: the adapter
  2858. *
  2859. * Disables the DMA engine. This can be called in emeregencies (e.g.,
  2860. * from error interrupts) or from normal process context. In the latter
  2861. * case it also disables any pending queue restart tasklets. Note that
  2862. * if it is called in interrupt context it cannot disable the restart
  2863. * tasklets as it cannot wait, however the tasklets will have no effect
  2864. * since the doorbells are disabled and the driver will call this again
  2865. * later from process context, at which time the tasklets will be stopped
  2866. * if they are still running.
  2867. */
  2868. void t3_sge_stop(struct adapter *adap)
  2869. {
  2870. t3_set_reg_field(adap, A_SG_CONTROL, F_GLOBALENABLE, 0);
  2871. if (!in_interrupt()) {
  2872. int i;
  2873. for (i = 0; i < SGE_QSETS; ++i) {
  2874. struct sge_qset *qs = &adap->sge.qs[i];
  2875. tasklet_kill(&qs->txq[TXQ_OFLD].qresume_tsk);
  2876. tasklet_kill(&qs->txq[TXQ_CTRL].qresume_tsk);
  2877. }
  2878. }
  2879. }
  2880. /**
  2881. * t3_sge_init - initialize SGE
  2882. * @adap: the adapter
  2883. * @p: the SGE parameters
  2884. *
  2885. * Performs SGE initialization needed every time after a chip reset.
  2886. * We do not initialize any of the queue sets here, instead the driver
  2887. * top-level must request those individually. We also do not enable DMA
  2888. * here, that should be done after the queues have been set up.
  2889. */
  2890. void t3_sge_init(struct adapter *adap, struct sge_params *p)
  2891. {
  2892. unsigned int ctrl, ups = ffs(pci_resource_len(adap->pdev, 2) >> 12);
  2893. ctrl = F_DROPPKT | V_PKTSHIFT(2) | F_FLMODE | F_AVOIDCQOVFL |
  2894. F_CQCRDTCTRL | F_CONGMODE | F_TNLFLMODE | F_FATLPERREN |
  2895. V_HOSTPAGESIZE(PAGE_SHIFT - 11) | F_BIGENDIANINGRESS |
  2896. V_USERSPACESIZE(ups ? ups - 1 : 0) | F_ISCSICOALESCING;
  2897. #if SGE_NUM_GENBITS == 1
  2898. ctrl |= F_EGRGENCTRL;
  2899. #endif
  2900. if (adap->params.rev > 0) {
  2901. if (!(adap->flags & (USING_MSIX | USING_MSI)))
  2902. ctrl |= F_ONEINTMULTQ | F_OPTONEINTMULTQ;
  2903. }
  2904. t3_write_reg(adap, A_SG_CONTROL, ctrl);
  2905. t3_write_reg(adap, A_SG_EGR_RCQ_DRB_THRSH, V_HIRCQDRBTHRSH(512) |
  2906. V_LORCQDRBTHRSH(512));
  2907. t3_write_reg(adap, A_SG_TIMER_TICK, core_ticks_per_usec(adap) / 10);
  2908. t3_write_reg(adap, A_SG_CMDQ_CREDIT_TH, V_THRESHOLD(32) |
  2909. V_TIMEOUT(200 * core_ticks_per_usec(adap)));
  2910. t3_write_reg(adap, A_SG_HI_DRB_HI_THRSH,
  2911. adap->params.rev < T3_REV_C ? 1000 : 500);
  2912. t3_write_reg(adap, A_SG_HI_DRB_LO_THRSH, 256);
  2913. t3_write_reg(adap, A_SG_LO_DRB_HI_THRSH, 1000);
  2914. t3_write_reg(adap, A_SG_LO_DRB_LO_THRSH, 256);
  2915. t3_write_reg(adap, A_SG_OCO_BASE, V_BASE1(0xfff));
  2916. t3_write_reg(adap, A_SG_DRB_PRI_THRESH, 63 * 1024);
  2917. }
  2918. /**
  2919. * t3_sge_prep - one-time SGE initialization
  2920. * @adap: the associated adapter
  2921. * @p: SGE parameters
  2922. *
  2923. * Performs one-time initialization of SGE SW state. Includes determining
  2924. * defaults for the assorted SGE parameters, which admins can change until
  2925. * they are used to initialize the SGE.
  2926. */
  2927. void t3_sge_prep(struct adapter *adap, struct sge_params *p)
  2928. {
  2929. int i;
  2930. p->max_pkt_size = (16 * 1024) - sizeof(struct cpl_rx_data) -
  2931. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2932. for (i = 0; i < SGE_QSETS; ++i) {
  2933. struct qset_params *q = p->qset + i;
  2934. q->polling = adap->params.rev > 0;
  2935. q->coalesce_usecs = 5;
  2936. q->rspq_size = 1024;
  2937. q->fl_size = 1024;
  2938. q->jumbo_size = 512;
  2939. q->txq_size[TXQ_ETH] = 1024;
  2940. q->txq_size[TXQ_OFLD] = 1024;
  2941. q->txq_size[TXQ_CTRL] = 256;
  2942. q->cong_thres = 0;
  2943. }
  2944. spin_lock_init(&adap->sge.reg_lock);
  2945. }