sge.c 89 KB

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