sge.c 88 KB

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