sge.c 91 KB

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