sge.c 93 KB

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