sge.c 81 KB

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