sge.c 94 KB

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