sge.c 93 KB

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