sge.c 90 KB

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