sge.c 78 KB

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