sge.c 82 KB

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