sge.c 89 KB

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