sge.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/ip.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/prefetch.h>
  42. #include <linux/export.h>
  43. #include <net/ipv6.h>
  44. #include <net/tcp.h>
  45. #include "cxgb4.h"
  46. #include "t4_regs.h"
  47. #include "t4_msg.h"
  48. #include "t4fw_api.h"
  49. /*
  50. * Rx buffer size. We use largish buffers if possible but settle for single
  51. * pages under memory shortage.
  52. */
  53. #if PAGE_SHIFT >= 16
  54. # define FL_PG_ORDER 0
  55. #else
  56. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  57. #endif
  58. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  59. #define RX_COPY_THRES 256
  60. #define RX_PULL_LEN 128
  61. /*
  62. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  63. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  64. */
  65. #define RX_PKT_SKB_LEN 512
  66. /*
  67. * Max number of Tx descriptors we clean up at a time. Should be modest as
  68. * freeing skbs isn't cheap and it happens while holding locks. We just need
  69. * to free packets faster than they arrive, we eventually catch up and keep
  70. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
  71. */
  72. #define MAX_TX_RECLAIM 16
  73. /*
  74. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  75. * allocating buffers isn't cheap either.
  76. */
  77. #define MAX_RX_REFILL 16U
  78. /*
  79. * Period of the Rx queue check timer. This timer is infrequent as it has
  80. * something to do only when the system experiences severe memory shortage.
  81. */
  82. #define RX_QCHECK_PERIOD (HZ / 2)
  83. /*
  84. * Period of the Tx queue check timer.
  85. */
  86. #define TX_QCHECK_PERIOD (HZ / 2)
  87. /* SGE Hung Ingress DMA Threshold Warning time (in Hz) and Warning Repeat Rate
  88. * (in RX_QCHECK_PERIOD multiples). If we find one of the SGE Ingress DMA
  89. * State Machines in the same state for this amount of time (in HZ) then we'll
  90. * issue a warning about a potential hang. We'll repeat the warning as the
  91. * SGE Ingress DMA Channel appears to be hung every N RX_QCHECK_PERIODs till
  92. * the situation clears. If the situation clears, we'll note that as well.
  93. */
  94. #define SGE_IDMA_WARN_THRESH (1 * HZ)
  95. #define SGE_IDMA_WARN_REPEAT (20 * RX_QCHECK_PERIOD)
  96. /*
  97. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  98. */
  99. #define MAX_TIMER_TX_RECLAIM 100
  100. /*
  101. * Timer index used when backing off due to memory shortage.
  102. */
  103. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  104. /*
  105. * An FL with <= FL_STARVE_THRES buffers is starving and a periodic timer will
  106. * attempt to refill it.
  107. */
  108. #define FL_STARVE_THRES 4
  109. /*
  110. * Suspend an Ethernet Tx queue with fewer available descriptors than this.
  111. * This is the same as calc_tx_descs() for a TSO packet with
  112. * nr_frags == MAX_SKB_FRAGS.
  113. */
  114. #define ETHTXQ_STOP_THRES \
  115. (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
  116. /*
  117. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  118. * for a full sized WR.
  119. */
  120. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  121. /*
  122. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  123. * into a WR.
  124. */
  125. #define MAX_IMM_TX_PKT_LEN 128
  126. /*
  127. * Max size of a WR sent through a control Tx queue.
  128. */
  129. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  130. struct tx_sw_desc { /* SW state per Tx descriptor */
  131. struct sk_buff *skb;
  132. struct ulptx_sgl *sgl;
  133. };
  134. struct rx_sw_desc { /* SW state per Rx descriptor */
  135. struct page *page;
  136. dma_addr_t dma_addr;
  137. };
  138. /*
  139. * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
  140. * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
  141. * We could easily support more but there doesn't seem to be much need for
  142. * that ...
  143. */
  144. #define FL_MTU_SMALL 1500
  145. #define FL_MTU_LARGE 9000
  146. static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
  147. unsigned int mtu)
  148. {
  149. struct sge *s = &adapter->sge;
  150. return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
  151. }
  152. #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
  153. #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
  154. /*
  155. * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
  156. * these to specify the buffer size as an index into the SGE Free List Buffer
  157. * Size register array. We also use bit 4, when the buffer has been unmapped
  158. * for DMA, but this is of course never sent to the hardware and is only used
  159. * to prevent double unmappings. All of the above requires that the Free List
  160. * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
  161. * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
  162. * Free List Buffer alignment is 32 bytes, this works out for us ...
  163. */
  164. enum {
  165. RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
  166. RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
  167. RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
  168. /*
  169. * XXX We shouldn't depend on being able to use these indices.
  170. * XXX Especially when some other Master PF has initialized the
  171. * XXX adapter or we use the Firmware Configuration File. We
  172. * XXX should really search through the Host Buffer Size register
  173. * XXX array for the appropriately sized buffer indices.
  174. */
  175. RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
  176. RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
  177. RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
  178. RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
  179. };
  180. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  181. {
  182. return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
  183. }
  184. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  185. {
  186. return !(d->dma_addr & RX_UNMAPPED_BUF);
  187. }
  188. /**
  189. * txq_avail - return the number of available slots in a Tx queue
  190. * @q: the Tx queue
  191. *
  192. * Returns the number of descriptors in a Tx queue available to write new
  193. * packets.
  194. */
  195. static inline unsigned int txq_avail(const struct sge_txq *q)
  196. {
  197. return q->size - 1 - q->in_use;
  198. }
  199. /**
  200. * fl_cap - return the capacity of a free-buffer list
  201. * @fl: the FL
  202. *
  203. * Returns the capacity of a free-buffer list. The capacity is less than
  204. * the size because one descriptor needs to be left unpopulated, otherwise
  205. * HW will think the FL is empty.
  206. */
  207. static inline unsigned int fl_cap(const struct sge_fl *fl)
  208. {
  209. return fl->size - 8; /* 1 descriptor = 8 buffers */
  210. }
  211. static inline bool fl_starving(const struct sge_fl *fl)
  212. {
  213. return fl->avail - fl->pend_cred <= FL_STARVE_THRES;
  214. }
  215. static int map_skb(struct device *dev, const struct sk_buff *skb,
  216. dma_addr_t *addr)
  217. {
  218. const skb_frag_t *fp, *end;
  219. const struct skb_shared_info *si;
  220. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  221. if (dma_mapping_error(dev, *addr))
  222. goto out_err;
  223. si = skb_shinfo(skb);
  224. end = &si->frags[si->nr_frags];
  225. for (fp = si->frags; fp < end; fp++) {
  226. *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
  227. DMA_TO_DEVICE);
  228. if (dma_mapping_error(dev, *addr))
  229. goto unwind;
  230. }
  231. return 0;
  232. unwind:
  233. while (fp-- > si->frags)
  234. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  235. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  236. out_err:
  237. return -ENOMEM;
  238. }
  239. #ifdef CONFIG_NEED_DMA_MAP_STATE
  240. static void unmap_skb(struct device *dev, const struct sk_buff *skb,
  241. const dma_addr_t *addr)
  242. {
  243. const skb_frag_t *fp, *end;
  244. const struct skb_shared_info *si;
  245. dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
  246. si = skb_shinfo(skb);
  247. end = &si->frags[si->nr_frags];
  248. for (fp = si->frags; fp < end; fp++)
  249. dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
  250. }
  251. /**
  252. * deferred_unmap_destructor - unmap a packet when it is freed
  253. * @skb: the packet
  254. *
  255. * This is the packet destructor used for Tx packets that need to remain
  256. * mapped until they are freed rather than until their Tx descriptors are
  257. * freed.
  258. */
  259. static void deferred_unmap_destructor(struct sk_buff *skb)
  260. {
  261. unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
  262. }
  263. #endif
  264. static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
  265. const struct ulptx_sgl *sgl, const struct sge_txq *q)
  266. {
  267. const struct ulptx_sge_pair *p;
  268. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  269. if (likely(skb_headlen(skb)))
  270. dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  271. DMA_TO_DEVICE);
  272. else {
  273. dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  274. DMA_TO_DEVICE);
  275. nfrags--;
  276. }
  277. /*
  278. * the complexity below is because of the possibility of a wrap-around
  279. * in the middle of an SGL
  280. */
  281. for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
  282. if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
  283. unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  284. ntohl(p->len[0]), DMA_TO_DEVICE);
  285. dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
  286. ntohl(p->len[1]), DMA_TO_DEVICE);
  287. p++;
  288. } else if ((u8 *)p == (u8 *)q->stat) {
  289. p = (const struct ulptx_sge_pair *)q->desc;
  290. goto unmap;
  291. } else if ((u8 *)p + 8 == (u8 *)q->stat) {
  292. const __be64 *addr = (const __be64 *)q->desc;
  293. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  294. ntohl(p->len[0]), DMA_TO_DEVICE);
  295. dma_unmap_page(dev, be64_to_cpu(addr[1]),
  296. ntohl(p->len[1]), DMA_TO_DEVICE);
  297. p = (const struct ulptx_sge_pair *)&addr[2];
  298. } else {
  299. const __be64 *addr = (const __be64 *)q->desc;
  300. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  301. ntohl(p->len[0]), DMA_TO_DEVICE);
  302. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  303. ntohl(p->len[1]), DMA_TO_DEVICE);
  304. p = (const struct ulptx_sge_pair *)&addr[1];
  305. }
  306. }
  307. if (nfrags) {
  308. __be64 addr;
  309. if ((u8 *)p == (u8 *)q->stat)
  310. p = (const struct ulptx_sge_pair *)q->desc;
  311. addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
  312. *(const __be64 *)q->desc;
  313. dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
  314. DMA_TO_DEVICE);
  315. }
  316. }
  317. /**
  318. * free_tx_desc - reclaims Tx descriptors and their buffers
  319. * @adapter: the adapter
  320. * @q: the Tx queue to reclaim descriptors from
  321. * @n: the number of descriptors to reclaim
  322. * @unmap: whether the buffers should be unmapped for DMA
  323. *
  324. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  325. * Tx buffers. Called with the Tx queue lock held.
  326. */
  327. static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
  328. unsigned int n, bool unmap)
  329. {
  330. struct tx_sw_desc *d;
  331. unsigned int cidx = q->cidx;
  332. struct device *dev = adap->pdev_dev;
  333. d = &q->sdesc[cidx];
  334. while (n--) {
  335. if (d->skb) { /* an SGL is present */
  336. if (unmap)
  337. unmap_sgl(dev, d->skb, d->sgl, q);
  338. dev_consume_skb_any(d->skb);
  339. d->skb = NULL;
  340. }
  341. ++d;
  342. if (++cidx == q->size) {
  343. cidx = 0;
  344. d = q->sdesc;
  345. }
  346. }
  347. q->cidx = cidx;
  348. }
  349. /*
  350. * Return the number of reclaimable descriptors in a Tx queue.
  351. */
  352. static inline int reclaimable(const struct sge_txq *q)
  353. {
  354. int hw_cidx = ntohs(q->stat->cidx);
  355. hw_cidx -= q->cidx;
  356. return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
  357. }
  358. /**
  359. * reclaim_completed_tx - reclaims completed Tx descriptors
  360. * @adap: the adapter
  361. * @q: the Tx queue to reclaim completed descriptors from
  362. * @unmap: whether the buffers should be unmapped for DMA
  363. *
  364. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  365. * and frees the associated buffers if possible. Called with the Tx
  366. * queue locked.
  367. */
  368. static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  369. bool unmap)
  370. {
  371. int avail = reclaimable(q);
  372. if (avail) {
  373. /*
  374. * Limit the amount of clean up work we do at a time to keep
  375. * the Tx lock hold time O(1).
  376. */
  377. if (avail > MAX_TX_RECLAIM)
  378. avail = MAX_TX_RECLAIM;
  379. free_tx_desc(adap, q, avail, unmap);
  380. q->in_use -= avail;
  381. }
  382. }
  383. static inline int get_buf_size(struct adapter *adapter,
  384. const struct rx_sw_desc *d)
  385. {
  386. struct sge *s = &adapter->sge;
  387. unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
  388. int buf_size;
  389. switch (rx_buf_size_idx) {
  390. case RX_SMALL_PG_BUF:
  391. buf_size = PAGE_SIZE;
  392. break;
  393. case RX_LARGE_PG_BUF:
  394. buf_size = PAGE_SIZE << s->fl_pg_order;
  395. break;
  396. case RX_SMALL_MTU_BUF:
  397. buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
  398. break;
  399. case RX_LARGE_MTU_BUF:
  400. buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
  401. break;
  402. default:
  403. BUG_ON(1);
  404. }
  405. return buf_size;
  406. }
  407. /**
  408. * free_rx_bufs - free the Rx buffers on an SGE free list
  409. * @adap: the adapter
  410. * @q: the SGE free list to free buffers from
  411. * @n: how many buffers to free
  412. *
  413. * Release the next @n buffers on an SGE free-buffer Rx queue. The
  414. * buffers must be made inaccessible to HW before calling this function.
  415. */
  416. static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
  417. {
  418. while (n--) {
  419. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  420. if (is_buf_mapped(d))
  421. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  422. get_buf_size(adap, d),
  423. PCI_DMA_FROMDEVICE);
  424. put_page(d->page);
  425. d->page = NULL;
  426. if (++q->cidx == q->size)
  427. q->cidx = 0;
  428. q->avail--;
  429. }
  430. }
  431. /**
  432. * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
  433. * @adap: the adapter
  434. * @q: the SGE free list
  435. *
  436. * Unmap the current buffer on an SGE free-buffer Rx queue. The
  437. * buffer must be made inaccessible to HW before calling this function.
  438. *
  439. * This is similar to @free_rx_bufs above but does not free the buffer.
  440. * Do note that the FL still loses any further access to the buffer.
  441. */
  442. static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
  443. {
  444. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  445. if (is_buf_mapped(d))
  446. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  447. get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
  448. d->page = NULL;
  449. if (++q->cidx == q->size)
  450. q->cidx = 0;
  451. q->avail--;
  452. }
  453. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  454. {
  455. u32 val;
  456. if (q->pend_cred >= 8) {
  457. val = PIDX(q->pend_cred / 8);
  458. if (!is_t4(adap->params.chip))
  459. val |= DBTYPE(1);
  460. wmb();
  461. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL), DBPRIO(1) |
  462. QID(q->cntxt_id) | val);
  463. q->pend_cred &= 7;
  464. }
  465. }
  466. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  467. dma_addr_t mapping)
  468. {
  469. sd->page = pg;
  470. sd->dma_addr = mapping; /* includes size low bits */
  471. }
  472. /**
  473. * refill_fl - refill an SGE Rx buffer ring
  474. * @adap: the adapter
  475. * @q: the ring to refill
  476. * @n: the number of new buffers to allocate
  477. * @gfp: the gfp flags for the allocations
  478. *
  479. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  480. * allocated with the supplied gfp flags. The caller must assure that
  481. * @n does not exceed the queue's capacity. If afterwards the queue is
  482. * found critically low mark it as starving in the bitmap of starving FLs.
  483. *
  484. * Returns the number of buffers allocated.
  485. */
  486. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  487. gfp_t gfp)
  488. {
  489. struct sge *s = &adap->sge;
  490. struct page *pg;
  491. dma_addr_t mapping;
  492. unsigned int cred = q->avail;
  493. __be64 *d = &q->desc[q->pidx];
  494. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  495. gfp |= __GFP_NOWARN | __GFP_COLD;
  496. if (s->fl_pg_order == 0)
  497. goto alloc_small_pages;
  498. /*
  499. * Prefer large buffers
  500. */
  501. while (n) {
  502. pg = alloc_pages(gfp | __GFP_COMP, s->fl_pg_order);
  503. if (unlikely(!pg)) {
  504. q->large_alloc_failed++;
  505. break; /* fall back to single pages */
  506. }
  507. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  508. PAGE_SIZE << s->fl_pg_order,
  509. PCI_DMA_FROMDEVICE);
  510. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  511. __free_pages(pg, s->fl_pg_order);
  512. goto out; /* do not try small pages for this error */
  513. }
  514. mapping |= RX_LARGE_PG_BUF;
  515. *d++ = cpu_to_be64(mapping);
  516. set_rx_sw_desc(sd, pg, mapping);
  517. sd++;
  518. q->avail++;
  519. if (++q->pidx == q->size) {
  520. q->pidx = 0;
  521. sd = q->sdesc;
  522. d = q->desc;
  523. }
  524. n--;
  525. }
  526. alloc_small_pages:
  527. while (n--) {
  528. pg = __skb_alloc_page(gfp, NULL);
  529. if (unlikely(!pg)) {
  530. q->alloc_failed++;
  531. break;
  532. }
  533. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  534. PCI_DMA_FROMDEVICE);
  535. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  536. put_page(pg);
  537. goto out;
  538. }
  539. *d++ = cpu_to_be64(mapping);
  540. set_rx_sw_desc(sd, pg, mapping);
  541. sd++;
  542. q->avail++;
  543. if (++q->pidx == q->size) {
  544. q->pidx = 0;
  545. sd = q->sdesc;
  546. d = q->desc;
  547. }
  548. }
  549. out: cred = q->avail - cred;
  550. q->pend_cred += cred;
  551. ring_fl_db(adap, q);
  552. if (unlikely(fl_starving(q))) {
  553. smp_wmb();
  554. set_bit(q->cntxt_id - adap->sge.egr_start,
  555. adap->sge.starving_fl);
  556. }
  557. return cred;
  558. }
  559. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  560. {
  561. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  562. GFP_ATOMIC);
  563. }
  564. /**
  565. * alloc_ring - allocate resources for an SGE descriptor ring
  566. * @dev: the PCI device's core device
  567. * @nelem: the number of descriptors
  568. * @elem_size: the size of each descriptor
  569. * @sw_size: the size of the SW state associated with each ring element
  570. * @phys: the physical address of the allocated ring
  571. * @metadata: address of the array holding the SW state for the ring
  572. * @stat_size: extra space in HW ring for status information
  573. * @node: preferred node for memory allocations
  574. *
  575. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  576. * free buffer lists, or response queues. Each SGE ring requires
  577. * space for its HW descriptors plus, optionally, space for the SW state
  578. * associated with each HW entry (the metadata). The function returns
  579. * three values: the virtual address for the HW ring (the return value
  580. * of the function), the bus address of the HW ring, and the address
  581. * of the SW ring.
  582. */
  583. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  584. size_t sw_size, dma_addr_t *phys, void *metadata,
  585. size_t stat_size, int node)
  586. {
  587. size_t len = nelem * elem_size + stat_size;
  588. void *s = NULL;
  589. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  590. if (!p)
  591. return NULL;
  592. if (sw_size) {
  593. s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
  594. if (!s) {
  595. dma_free_coherent(dev, len, p, *phys);
  596. return NULL;
  597. }
  598. }
  599. if (metadata)
  600. *(void **)metadata = s;
  601. memset(p, 0, len);
  602. return p;
  603. }
  604. /**
  605. * sgl_len - calculates the size of an SGL of the given capacity
  606. * @n: the number of SGL entries
  607. *
  608. * Calculates the number of flits needed for a scatter/gather list that
  609. * can hold the given number of entries.
  610. */
  611. static inline unsigned int sgl_len(unsigned int n)
  612. {
  613. n--;
  614. return (3 * n) / 2 + (n & 1) + 2;
  615. }
  616. /**
  617. * flits_to_desc - returns the num of Tx descriptors for the given flits
  618. * @n: the number of flits
  619. *
  620. * Returns the number of Tx descriptors needed for the supplied number
  621. * of flits.
  622. */
  623. static inline unsigned int flits_to_desc(unsigned int n)
  624. {
  625. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  626. return DIV_ROUND_UP(n, 8);
  627. }
  628. /**
  629. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  630. * @skb: the packet
  631. *
  632. * Returns whether an Ethernet packet is small enough to fit as
  633. * immediate data. Return value corresponds to headroom required.
  634. */
  635. static inline int is_eth_imm(const struct sk_buff *skb)
  636. {
  637. int hdrlen = skb_shinfo(skb)->gso_size ?
  638. sizeof(struct cpl_tx_pkt_lso_core) : 0;
  639. hdrlen += sizeof(struct cpl_tx_pkt);
  640. if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
  641. return hdrlen;
  642. return 0;
  643. }
  644. /**
  645. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  646. * @skb: the packet
  647. *
  648. * Returns the number of flits needed for a Tx WR for the given Ethernet
  649. * packet, including the needed WR and CPL headers.
  650. */
  651. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  652. {
  653. unsigned int flits;
  654. int hdrlen = is_eth_imm(skb);
  655. if (hdrlen)
  656. return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
  657. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
  658. if (skb_shinfo(skb)->gso_size)
  659. flits += 2;
  660. return flits;
  661. }
  662. /**
  663. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  664. * @skb: the packet
  665. *
  666. * Returns the number of Tx descriptors needed for the given Ethernet
  667. * packet, including the needed WR and CPL headers.
  668. */
  669. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  670. {
  671. return flits_to_desc(calc_tx_flits(skb));
  672. }
  673. /**
  674. * write_sgl - populate a scatter/gather list for a packet
  675. * @skb: the packet
  676. * @q: the Tx queue we are writing into
  677. * @sgl: starting location for writing the SGL
  678. * @end: points right after the end of the SGL
  679. * @start: start offset into skb main-body data to include in the SGL
  680. * @addr: the list of bus addresses for the SGL elements
  681. *
  682. * Generates a gather list for the buffers that make up a packet.
  683. * The caller must provide adequate space for the SGL that will be written.
  684. * The SGL includes all of the packet's page fragments and the data in its
  685. * main body except for the first @start bytes. @sgl must be 16-byte
  686. * aligned and within a Tx descriptor with available space. @end points
  687. * right after the end of the SGL but does not account for any potential
  688. * wrap around, i.e., @end > @sgl.
  689. */
  690. static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  691. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  692. const dma_addr_t *addr)
  693. {
  694. unsigned int i, len;
  695. struct ulptx_sge_pair *to;
  696. const struct skb_shared_info *si = skb_shinfo(skb);
  697. unsigned int nfrags = si->nr_frags;
  698. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  699. len = skb_headlen(skb) - start;
  700. if (likely(len)) {
  701. sgl->len0 = htonl(len);
  702. sgl->addr0 = cpu_to_be64(addr[0] + start);
  703. nfrags++;
  704. } else {
  705. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  706. sgl->addr0 = cpu_to_be64(addr[1]);
  707. }
  708. sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
  709. if (likely(--nfrags == 0))
  710. return;
  711. /*
  712. * Most of the complexity below deals with the possibility we hit the
  713. * end of the queue in the middle of writing the SGL. For this case
  714. * only we create the SGL in a temporary buffer and then copy it.
  715. */
  716. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  717. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  718. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  719. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  720. to->addr[0] = cpu_to_be64(addr[i]);
  721. to->addr[1] = cpu_to_be64(addr[++i]);
  722. }
  723. if (nfrags) {
  724. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  725. to->len[1] = cpu_to_be32(0);
  726. to->addr[0] = cpu_to_be64(addr[i + 1]);
  727. }
  728. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  729. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  730. if (likely(part0))
  731. memcpy(sgl->sge, buf, part0);
  732. part1 = (u8 *)end - (u8 *)q->stat;
  733. memcpy(q->desc, (u8 *)buf + part0, part1);
  734. end = (void *)q->desc + part1;
  735. }
  736. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  737. *end = 0;
  738. }
  739. /* This function copies 64 byte coalesced work request to
  740. * memory mapped BAR2 space(user space writes).
  741. * For coalesced WR SGE, fetches data from the FIFO instead of from Host.
  742. */
  743. static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
  744. {
  745. int count = 8;
  746. while (count) {
  747. writeq(*src, dst);
  748. src++;
  749. dst++;
  750. count--;
  751. }
  752. }
  753. /**
  754. * ring_tx_db - check and potentially ring a Tx queue's doorbell
  755. * @adap: the adapter
  756. * @q: the Tx queue
  757. * @n: number of new descriptors to give to HW
  758. *
  759. * Ring the doorbel for a Tx queue.
  760. */
  761. static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  762. {
  763. unsigned int *wr, index;
  764. unsigned long flags;
  765. wmb(); /* write descriptors before telling HW */
  766. spin_lock_irqsave(&q->db_lock, flags);
  767. if (!q->db_disabled) {
  768. if (is_t4(adap->params.chip)) {
  769. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
  770. QID(q->cntxt_id) | PIDX(n));
  771. } else {
  772. if (n == 1) {
  773. index = q->pidx ? (q->pidx - 1) : (q->size - 1);
  774. wr = (unsigned int *)&q->desc[index];
  775. cxgb_pio_copy((u64 __iomem *)
  776. (adap->bar2 + q->udb + 64),
  777. (u64 *)wr);
  778. } else
  779. writel(n, adap->bar2 + q->udb + 8);
  780. wmb();
  781. }
  782. } else
  783. q->db_pidx_inc += n;
  784. q->db_pidx = q->pidx;
  785. spin_unlock_irqrestore(&q->db_lock, flags);
  786. }
  787. /**
  788. * inline_tx_skb - inline a packet's data into Tx descriptors
  789. * @skb: the packet
  790. * @q: the Tx queue where the packet will be inlined
  791. * @pos: starting position in the Tx queue where to inline the packet
  792. *
  793. * Inline a packet's contents directly into Tx descriptors, starting at
  794. * the given position within the Tx DMA ring.
  795. * Most of the complexity of this operation is dealing with wrap arounds
  796. * in the middle of the packet we want to inline.
  797. */
  798. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
  799. void *pos)
  800. {
  801. u64 *p;
  802. int left = (void *)q->stat - pos;
  803. if (likely(skb->len <= left)) {
  804. if (likely(!skb->data_len))
  805. skb_copy_from_linear_data(skb, pos, skb->len);
  806. else
  807. skb_copy_bits(skb, 0, pos, skb->len);
  808. pos += skb->len;
  809. } else {
  810. skb_copy_bits(skb, 0, pos, left);
  811. skb_copy_bits(skb, left, q->desc, skb->len - left);
  812. pos = (void *)q->desc + (skb->len - left);
  813. }
  814. /* 0-pad to multiple of 16 */
  815. p = PTR_ALIGN(pos, 8);
  816. if ((uintptr_t)p & 8)
  817. *p = 0;
  818. }
  819. /*
  820. * Figure out what HW csum a packet wants and return the appropriate control
  821. * bits.
  822. */
  823. static u64 hwcsum(const struct sk_buff *skb)
  824. {
  825. int csum_type;
  826. const struct iphdr *iph = ip_hdr(skb);
  827. if (iph->version == 4) {
  828. if (iph->protocol == IPPROTO_TCP)
  829. csum_type = TX_CSUM_TCPIP;
  830. else if (iph->protocol == IPPROTO_UDP)
  831. csum_type = TX_CSUM_UDPIP;
  832. else {
  833. nocsum: /*
  834. * unknown protocol, disable HW csum
  835. * and hope a bad packet is detected
  836. */
  837. return TXPKT_L4CSUM_DIS;
  838. }
  839. } else {
  840. /*
  841. * this doesn't work with extension headers
  842. */
  843. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  844. if (ip6h->nexthdr == IPPROTO_TCP)
  845. csum_type = TX_CSUM_TCPIP6;
  846. else if (ip6h->nexthdr == IPPROTO_UDP)
  847. csum_type = TX_CSUM_UDPIP6;
  848. else
  849. goto nocsum;
  850. }
  851. if (likely(csum_type >= TX_CSUM_TCPIP))
  852. return TXPKT_CSUM_TYPE(csum_type) |
  853. TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
  854. TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
  855. else {
  856. int start = skb_transport_offset(skb);
  857. return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
  858. TXPKT_CSUM_LOC(start + skb->csum_offset);
  859. }
  860. }
  861. static void eth_txq_stop(struct sge_eth_txq *q)
  862. {
  863. netif_tx_stop_queue(q->txq);
  864. q->q.stops++;
  865. }
  866. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  867. {
  868. q->in_use += n;
  869. q->pidx += n;
  870. if (q->pidx >= q->size)
  871. q->pidx -= q->size;
  872. }
  873. /**
  874. * t4_eth_xmit - add a packet to an Ethernet Tx queue
  875. * @skb: the packet
  876. * @dev: the egress net device
  877. *
  878. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  879. */
  880. netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  881. {
  882. int len;
  883. u32 wr_mid;
  884. u64 cntrl, *end;
  885. int qidx, credits;
  886. unsigned int flits, ndesc;
  887. struct adapter *adap;
  888. struct sge_eth_txq *q;
  889. const struct port_info *pi;
  890. struct fw_eth_tx_pkt_wr *wr;
  891. struct cpl_tx_pkt_core *cpl;
  892. const struct skb_shared_info *ssi;
  893. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  894. bool immediate = false;
  895. /*
  896. * The chip min packet length is 10 octets but play safe and reject
  897. * anything shorter than an Ethernet header.
  898. */
  899. if (unlikely(skb->len < ETH_HLEN)) {
  900. out_free: dev_kfree_skb_any(skb);
  901. return NETDEV_TX_OK;
  902. }
  903. pi = netdev_priv(dev);
  904. adap = pi->adapter;
  905. qidx = skb_get_queue_mapping(skb);
  906. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  907. reclaim_completed_tx(adap, &q->q, true);
  908. flits = calc_tx_flits(skb);
  909. ndesc = flits_to_desc(flits);
  910. credits = txq_avail(&q->q) - ndesc;
  911. if (unlikely(credits < 0)) {
  912. eth_txq_stop(q);
  913. dev_err(adap->pdev_dev,
  914. "%s: Tx ring %u full while queue awake!\n",
  915. dev->name, qidx);
  916. return NETDEV_TX_BUSY;
  917. }
  918. if (is_eth_imm(skb))
  919. immediate = true;
  920. if (!immediate &&
  921. unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
  922. q->mapping_err++;
  923. goto out_free;
  924. }
  925. wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
  926. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  927. eth_txq_stop(q);
  928. wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
  929. }
  930. wr = (void *)&q->q.desc[q->q.pidx];
  931. wr->equiq_to_len16 = htonl(wr_mid);
  932. wr->r3 = cpu_to_be64(0);
  933. end = (u64 *)wr + flits;
  934. len = immediate ? skb->len : 0;
  935. ssi = skb_shinfo(skb);
  936. if (ssi->gso_size) {
  937. struct cpl_tx_pkt_lso *lso = (void *)wr;
  938. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  939. int l3hdr_len = skb_network_header_len(skb);
  940. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  941. len += sizeof(*lso);
  942. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  943. FW_WR_IMMDLEN(len));
  944. lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
  945. LSO_FIRST_SLICE | LSO_LAST_SLICE |
  946. LSO_IPV6(v6) |
  947. LSO_ETHHDR_LEN(eth_xtra_len / 4) |
  948. LSO_IPHDR_LEN(l3hdr_len / 4) |
  949. LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
  950. lso->c.ipid_ofst = htons(0);
  951. lso->c.mss = htons(ssi->gso_size);
  952. lso->c.seqno_offset = htonl(0);
  953. lso->c.len = htonl(skb->len);
  954. cpl = (void *)(lso + 1);
  955. cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  956. TXPKT_IPHDR_LEN(l3hdr_len) |
  957. TXPKT_ETHHDR_LEN(eth_xtra_len);
  958. q->tso++;
  959. q->tx_cso += ssi->gso_segs;
  960. } else {
  961. len += sizeof(*cpl);
  962. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  963. FW_WR_IMMDLEN(len));
  964. cpl = (void *)(wr + 1);
  965. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  966. cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
  967. q->tx_cso++;
  968. } else
  969. cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
  970. }
  971. if (vlan_tx_tag_present(skb)) {
  972. q->vlan_ins++;
  973. cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
  974. }
  975. cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
  976. TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
  977. cpl->pack = htons(0);
  978. cpl->len = htons(skb->len);
  979. cpl->ctrl1 = cpu_to_be64(cntrl);
  980. if (immediate) {
  981. inline_tx_skb(skb, &q->q, cpl + 1);
  982. dev_consume_skb_any(skb);
  983. } else {
  984. int last_desc;
  985. write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
  986. addr);
  987. skb_orphan(skb);
  988. last_desc = q->q.pidx + ndesc - 1;
  989. if (last_desc >= q->q.size)
  990. last_desc -= q->q.size;
  991. q->q.sdesc[last_desc].skb = skb;
  992. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
  993. }
  994. txq_advance(&q->q, ndesc);
  995. ring_tx_db(adap, &q->q, ndesc);
  996. return NETDEV_TX_OK;
  997. }
  998. /**
  999. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1000. * @q: the SGE control Tx queue
  1001. *
  1002. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  1003. * that send only immediate data (presently just the control queues) and
  1004. * thus do not have any sk_buffs to release.
  1005. */
  1006. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1007. {
  1008. int hw_cidx = ntohs(q->stat->cidx);
  1009. int reclaim = hw_cidx - q->cidx;
  1010. if (reclaim < 0)
  1011. reclaim += q->size;
  1012. q->in_use -= reclaim;
  1013. q->cidx = hw_cidx;
  1014. }
  1015. /**
  1016. * is_imm - check whether a packet can be sent as immediate data
  1017. * @skb: the packet
  1018. *
  1019. * Returns true if a packet can be sent as a WR with immediate data.
  1020. */
  1021. static inline int is_imm(const struct sk_buff *skb)
  1022. {
  1023. return skb->len <= MAX_CTRL_WR_LEN;
  1024. }
  1025. /**
  1026. * ctrlq_check_stop - check if a control queue is full and should stop
  1027. * @q: the queue
  1028. * @wr: most recent WR written to the queue
  1029. *
  1030. * Check if a control queue has become full and should be stopped.
  1031. * We clean up control queue descriptors very lazily, only when we are out.
  1032. * If the queue is still full after reclaiming any completed descriptors
  1033. * we suspend it and have the last WR wake it up.
  1034. */
  1035. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  1036. {
  1037. reclaim_completed_tx_imm(&q->q);
  1038. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1039. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1040. q->q.stops++;
  1041. q->full = 1;
  1042. }
  1043. }
  1044. /**
  1045. * ctrl_xmit - send a packet through an SGE control Tx queue
  1046. * @q: the control queue
  1047. * @skb: the packet
  1048. *
  1049. * Send a packet through an SGE control Tx queue. Packets sent through
  1050. * a control queue must fit entirely as immediate data.
  1051. */
  1052. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  1053. {
  1054. unsigned int ndesc;
  1055. struct fw_wr_hdr *wr;
  1056. if (unlikely(!is_imm(skb))) {
  1057. WARN_ON(1);
  1058. dev_kfree_skb(skb);
  1059. return NET_XMIT_DROP;
  1060. }
  1061. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  1062. spin_lock(&q->sendq.lock);
  1063. if (unlikely(q->full)) {
  1064. skb->priority = ndesc; /* save for restart */
  1065. __skb_queue_tail(&q->sendq, skb);
  1066. spin_unlock(&q->sendq.lock);
  1067. return NET_XMIT_CN;
  1068. }
  1069. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1070. inline_tx_skb(skb, &q->q, wr);
  1071. txq_advance(&q->q, ndesc);
  1072. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  1073. ctrlq_check_stop(q, wr);
  1074. ring_tx_db(q->adap, &q->q, ndesc);
  1075. spin_unlock(&q->sendq.lock);
  1076. kfree_skb(skb);
  1077. return NET_XMIT_SUCCESS;
  1078. }
  1079. /**
  1080. * restart_ctrlq - restart a suspended control queue
  1081. * @data: the control queue to restart
  1082. *
  1083. * Resumes transmission on a suspended Tx control queue.
  1084. */
  1085. static void restart_ctrlq(unsigned long data)
  1086. {
  1087. struct sk_buff *skb;
  1088. unsigned int written = 0;
  1089. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  1090. spin_lock(&q->sendq.lock);
  1091. reclaim_completed_tx_imm(&q->q);
  1092. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  1093. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  1094. struct fw_wr_hdr *wr;
  1095. unsigned int ndesc = skb->priority; /* previously saved */
  1096. /*
  1097. * Write descriptors and free skbs outside the lock to limit
  1098. * wait times. q->full is still set so new skbs will be queued.
  1099. */
  1100. spin_unlock(&q->sendq.lock);
  1101. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1102. inline_tx_skb(skb, &q->q, wr);
  1103. kfree_skb(skb);
  1104. written += ndesc;
  1105. txq_advance(&q->q, ndesc);
  1106. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1107. unsigned long old = q->q.stops;
  1108. ctrlq_check_stop(q, wr);
  1109. if (q->q.stops != old) { /* suspended anew */
  1110. spin_lock(&q->sendq.lock);
  1111. goto ringdb;
  1112. }
  1113. }
  1114. if (written > 16) {
  1115. ring_tx_db(q->adap, &q->q, written);
  1116. written = 0;
  1117. }
  1118. spin_lock(&q->sendq.lock);
  1119. }
  1120. q->full = 0;
  1121. ringdb: if (written)
  1122. ring_tx_db(q->adap, &q->q, written);
  1123. spin_unlock(&q->sendq.lock);
  1124. }
  1125. /**
  1126. * t4_mgmt_tx - send a management message
  1127. * @adap: the adapter
  1128. * @skb: the packet containing the management message
  1129. *
  1130. * Send a management message through control queue 0.
  1131. */
  1132. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1133. {
  1134. int ret;
  1135. local_bh_disable();
  1136. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1137. local_bh_enable();
  1138. return ret;
  1139. }
  1140. /**
  1141. * is_ofld_imm - check whether a packet can be sent as immediate data
  1142. * @skb: the packet
  1143. *
  1144. * Returns true if a packet can be sent as an offload WR with immediate
  1145. * data. We currently use the same limit as for Ethernet packets.
  1146. */
  1147. static inline int is_ofld_imm(const struct sk_buff *skb)
  1148. {
  1149. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1150. }
  1151. /**
  1152. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1153. * @skb: the packet
  1154. *
  1155. * Returns the number of flits needed for the given offload packet.
  1156. * These packets are already fully constructed and no additional headers
  1157. * will be added.
  1158. */
  1159. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1160. {
  1161. unsigned int flits, cnt;
  1162. if (is_ofld_imm(skb))
  1163. return DIV_ROUND_UP(skb->len, 8);
  1164. flits = skb_transport_offset(skb) / 8U; /* headers */
  1165. cnt = skb_shinfo(skb)->nr_frags;
  1166. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1167. cnt++;
  1168. return flits + sgl_len(cnt);
  1169. }
  1170. /**
  1171. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1172. * @adap: the adapter
  1173. * @q: the queue to stop
  1174. *
  1175. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1176. * inability to map packets. A periodic timer attempts to restart
  1177. * queues so marked.
  1178. */
  1179. static void txq_stop_maperr(struct sge_ofld_txq *q)
  1180. {
  1181. q->mapping_err++;
  1182. q->q.stops++;
  1183. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1184. q->adap->sge.txq_maperr);
  1185. }
  1186. /**
  1187. * ofldtxq_stop - stop an offload Tx queue that has become full
  1188. * @q: the queue to stop
  1189. * @skb: the packet causing the queue to become full
  1190. *
  1191. * Stops an offload Tx queue that has become full and modifies the packet
  1192. * being written to request a wakeup.
  1193. */
  1194. static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
  1195. {
  1196. struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
  1197. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1198. q->q.stops++;
  1199. q->full = 1;
  1200. }
  1201. /**
  1202. * service_ofldq - restart a suspended offload queue
  1203. * @q: the offload queue
  1204. *
  1205. * Services an offload Tx queue by moving packets from its packet queue
  1206. * to the HW Tx ring. The function starts and ends with the queue locked.
  1207. */
  1208. static void service_ofldq(struct sge_ofld_txq *q)
  1209. {
  1210. u64 *pos;
  1211. int credits;
  1212. struct sk_buff *skb;
  1213. unsigned int written = 0;
  1214. unsigned int flits, ndesc;
  1215. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1216. /*
  1217. * We drop the lock but leave skb on sendq, thus retaining
  1218. * exclusive access to the state of the queue.
  1219. */
  1220. spin_unlock(&q->sendq.lock);
  1221. reclaim_completed_tx(q->adap, &q->q, false);
  1222. flits = skb->priority; /* previously saved */
  1223. ndesc = flits_to_desc(flits);
  1224. credits = txq_avail(&q->q) - ndesc;
  1225. BUG_ON(credits < 0);
  1226. if (unlikely(credits < TXQ_STOP_THRES))
  1227. ofldtxq_stop(q, skb);
  1228. pos = (u64 *)&q->q.desc[q->q.pidx];
  1229. if (is_ofld_imm(skb))
  1230. inline_tx_skb(skb, &q->q, pos);
  1231. else if (map_skb(q->adap->pdev_dev, skb,
  1232. (dma_addr_t *)skb->head)) {
  1233. txq_stop_maperr(q);
  1234. spin_lock(&q->sendq.lock);
  1235. break;
  1236. } else {
  1237. int last_desc, hdr_len = skb_transport_offset(skb);
  1238. memcpy(pos, skb->data, hdr_len);
  1239. write_sgl(skb, &q->q, (void *)pos + hdr_len,
  1240. pos + flits, hdr_len,
  1241. (dma_addr_t *)skb->head);
  1242. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1243. skb->dev = q->adap->port[0];
  1244. skb->destructor = deferred_unmap_destructor;
  1245. #endif
  1246. last_desc = q->q.pidx + ndesc - 1;
  1247. if (last_desc >= q->q.size)
  1248. last_desc -= q->q.size;
  1249. q->q.sdesc[last_desc].skb = skb;
  1250. }
  1251. txq_advance(&q->q, ndesc);
  1252. written += ndesc;
  1253. if (unlikely(written > 32)) {
  1254. ring_tx_db(q->adap, &q->q, written);
  1255. written = 0;
  1256. }
  1257. spin_lock(&q->sendq.lock);
  1258. __skb_unlink(skb, &q->sendq);
  1259. if (is_ofld_imm(skb))
  1260. kfree_skb(skb);
  1261. }
  1262. if (likely(written))
  1263. ring_tx_db(q->adap, &q->q, written);
  1264. }
  1265. /**
  1266. * ofld_xmit - send a packet through an offload queue
  1267. * @q: the Tx offload queue
  1268. * @skb: the packet
  1269. *
  1270. * Send an offload packet through an SGE offload queue.
  1271. */
  1272. static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
  1273. {
  1274. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1275. spin_lock(&q->sendq.lock);
  1276. __skb_queue_tail(&q->sendq, skb);
  1277. if (q->sendq.qlen == 1)
  1278. service_ofldq(q);
  1279. spin_unlock(&q->sendq.lock);
  1280. return NET_XMIT_SUCCESS;
  1281. }
  1282. /**
  1283. * restart_ofldq - restart a suspended offload queue
  1284. * @data: the offload queue to restart
  1285. *
  1286. * Resumes transmission on a suspended Tx offload queue.
  1287. */
  1288. static void restart_ofldq(unsigned long data)
  1289. {
  1290. struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
  1291. spin_lock(&q->sendq.lock);
  1292. q->full = 0; /* the queue actually is completely empty now */
  1293. service_ofldq(q);
  1294. spin_unlock(&q->sendq.lock);
  1295. }
  1296. /**
  1297. * skb_txq - return the Tx queue an offload packet should use
  1298. * @skb: the packet
  1299. *
  1300. * Returns the Tx queue an offload packet should use as indicated by bits
  1301. * 1-15 in the packet's queue_mapping.
  1302. */
  1303. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1304. {
  1305. return skb->queue_mapping >> 1;
  1306. }
  1307. /**
  1308. * is_ctrl_pkt - return whether an offload packet is a control packet
  1309. * @skb: the packet
  1310. *
  1311. * Returns whether an offload packet should use an OFLD or a CTRL
  1312. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1313. */
  1314. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1315. {
  1316. return skb->queue_mapping & 1;
  1317. }
  1318. static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
  1319. {
  1320. unsigned int idx = skb_txq(skb);
  1321. if (unlikely(is_ctrl_pkt(skb))) {
  1322. /* Single ctrl queue is a requirement for LE workaround path */
  1323. if (adap->tids.nsftids)
  1324. idx = 0;
  1325. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1326. }
  1327. return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
  1328. }
  1329. /**
  1330. * t4_ofld_send - send an offload packet
  1331. * @adap: the adapter
  1332. * @skb: the packet
  1333. *
  1334. * Sends an offload packet. We use the packet queue_mapping to select the
  1335. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1336. * should be sent as regular or control, bits 1-15 select the queue.
  1337. */
  1338. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1339. {
  1340. int ret;
  1341. local_bh_disable();
  1342. ret = ofld_send(adap, skb);
  1343. local_bh_enable();
  1344. return ret;
  1345. }
  1346. /**
  1347. * cxgb4_ofld_send - send an offload packet
  1348. * @dev: the net device
  1349. * @skb: the packet
  1350. *
  1351. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1352. * intended for ULDs.
  1353. */
  1354. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1355. {
  1356. return t4_ofld_send(netdev2adap(dev), skb);
  1357. }
  1358. EXPORT_SYMBOL(cxgb4_ofld_send);
  1359. static inline void copy_frags(struct sk_buff *skb,
  1360. const struct pkt_gl *gl, unsigned int offset)
  1361. {
  1362. int i;
  1363. /* usually there's just one frag */
  1364. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  1365. gl->frags[0].offset + offset,
  1366. gl->frags[0].size - offset);
  1367. skb_shinfo(skb)->nr_frags = gl->nfrags;
  1368. for (i = 1; i < gl->nfrags; i++)
  1369. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  1370. gl->frags[i].offset,
  1371. gl->frags[i].size);
  1372. /* get a reference to the last page, we don't own it */
  1373. get_page(gl->frags[gl->nfrags - 1].page);
  1374. }
  1375. /**
  1376. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1377. * @gl: the gather list
  1378. * @skb_len: size of sk_buff main body if it carries fragments
  1379. * @pull_len: amount of data to move to the sk_buff's main body
  1380. *
  1381. * Builds an sk_buff from the given packet gather list. Returns the
  1382. * sk_buff or %NULL if sk_buff allocation failed.
  1383. */
  1384. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1385. unsigned int skb_len, unsigned int pull_len)
  1386. {
  1387. struct sk_buff *skb;
  1388. /*
  1389. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1390. * size, which is expected since buffers are at least PAGE_SIZEd.
  1391. * In this case packets up to RX_COPY_THRES have only one fragment.
  1392. */
  1393. if (gl->tot_len <= RX_COPY_THRES) {
  1394. skb = dev_alloc_skb(gl->tot_len);
  1395. if (unlikely(!skb))
  1396. goto out;
  1397. __skb_put(skb, gl->tot_len);
  1398. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1399. } else {
  1400. skb = dev_alloc_skb(skb_len);
  1401. if (unlikely(!skb))
  1402. goto out;
  1403. __skb_put(skb, pull_len);
  1404. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1405. copy_frags(skb, gl, pull_len);
  1406. skb->len = gl->tot_len;
  1407. skb->data_len = skb->len - pull_len;
  1408. skb->truesize += skb->data_len;
  1409. }
  1410. out: return skb;
  1411. }
  1412. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1413. /**
  1414. * t4_pktgl_free - free a packet gather list
  1415. * @gl: the gather list
  1416. *
  1417. * Releases the pages of a packet gather list. We do not own the last
  1418. * page on the list and do not free it.
  1419. */
  1420. static void t4_pktgl_free(const struct pkt_gl *gl)
  1421. {
  1422. int n;
  1423. const struct page_frag *p;
  1424. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1425. put_page(p->page);
  1426. }
  1427. /*
  1428. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1429. * be delivered to anyone and send it to the stack for capture.
  1430. */
  1431. static noinline int handle_trace_pkt(struct adapter *adap,
  1432. const struct pkt_gl *gl)
  1433. {
  1434. struct sk_buff *skb;
  1435. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  1436. if (unlikely(!skb)) {
  1437. t4_pktgl_free(gl);
  1438. return 0;
  1439. }
  1440. if (is_t4(adap->params.chip))
  1441. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  1442. else
  1443. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  1444. skb_reset_mac_header(skb);
  1445. skb->protocol = htons(0xffff);
  1446. skb->dev = adap->port[0];
  1447. netif_receive_skb(skb);
  1448. return 0;
  1449. }
  1450. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1451. const struct cpl_rx_pkt *pkt)
  1452. {
  1453. struct adapter *adapter = rxq->rspq.adap;
  1454. struct sge *s = &adapter->sge;
  1455. int ret;
  1456. struct sk_buff *skb;
  1457. skb = napi_get_frags(&rxq->rspq.napi);
  1458. if (unlikely(!skb)) {
  1459. t4_pktgl_free(gl);
  1460. rxq->stats.rx_drops++;
  1461. return;
  1462. }
  1463. copy_frags(skb, gl, s->pktshift);
  1464. skb->len = gl->tot_len - s->pktshift;
  1465. skb->data_len = skb->len;
  1466. skb->truesize += skb->data_len;
  1467. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1468. skb_record_rx_queue(skb, rxq->rspq.idx);
  1469. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  1470. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1471. PKT_HASH_TYPE_L3);
  1472. if (unlikely(pkt->vlan_ex)) {
  1473. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1474. rxq->stats.vlan_ex++;
  1475. }
  1476. ret = napi_gro_frags(&rxq->rspq.napi);
  1477. if (ret == GRO_HELD)
  1478. rxq->stats.lro_pkts++;
  1479. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1480. rxq->stats.lro_merged++;
  1481. rxq->stats.pkts++;
  1482. rxq->stats.rx_cso++;
  1483. }
  1484. /**
  1485. * t4_ethrx_handler - process an ingress ethernet packet
  1486. * @q: the response queue that received the packet
  1487. * @rsp: the response queue descriptor holding the RX_PKT message
  1488. * @si: the gather list of packet fragments
  1489. *
  1490. * Process an ingress ethernet packet and deliver it to the stack.
  1491. */
  1492. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  1493. const struct pkt_gl *si)
  1494. {
  1495. bool csum_ok;
  1496. struct sk_buff *skb;
  1497. const struct cpl_rx_pkt *pkt;
  1498. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1499. struct sge *s = &q->adap->sge;
  1500. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  1501. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  1502. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  1503. return handle_trace_pkt(q->adap, si);
  1504. pkt = (const struct cpl_rx_pkt *)rsp;
  1505. csum_ok = pkt->csum_calc && !pkt->err_vec &&
  1506. (q->netdev->features & NETIF_F_RXCSUM);
  1507. if ((pkt->l2info & htonl(RXF_TCP)) &&
  1508. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  1509. do_gro(rxq, si, pkt);
  1510. return 0;
  1511. }
  1512. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  1513. if (unlikely(!skb)) {
  1514. t4_pktgl_free(si);
  1515. rxq->stats.rx_drops++;
  1516. return 0;
  1517. }
  1518. __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
  1519. skb->protocol = eth_type_trans(skb, q->netdev);
  1520. skb_record_rx_queue(skb, q->idx);
  1521. if (skb->dev->features & NETIF_F_RXHASH)
  1522. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1523. PKT_HASH_TYPE_L3);
  1524. rxq->stats.pkts++;
  1525. if (csum_ok && (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
  1526. if (!pkt->ip_frag) {
  1527. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1528. rxq->stats.rx_cso++;
  1529. } else if (pkt->l2info & htonl(RXF_IP)) {
  1530. __sum16 c = (__force __sum16)pkt->csum;
  1531. skb->csum = csum_unfold(c);
  1532. skb->ip_summed = CHECKSUM_COMPLETE;
  1533. rxq->stats.rx_cso++;
  1534. }
  1535. } else
  1536. skb_checksum_none_assert(skb);
  1537. if (unlikely(pkt->vlan_ex)) {
  1538. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1539. rxq->stats.vlan_ex++;
  1540. }
  1541. netif_receive_skb(skb);
  1542. return 0;
  1543. }
  1544. /**
  1545. * restore_rx_bufs - put back a packet's Rx buffers
  1546. * @si: the packet gather list
  1547. * @q: the SGE free list
  1548. * @frags: number of FL buffers to restore
  1549. *
  1550. * Puts back on an FL the Rx buffers associated with @si. The buffers
  1551. * have already been unmapped and are left unmapped, we mark them so to
  1552. * prevent further unmapping attempts.
  1553. *
  1554. * This function undoes a series of @unmap_rx_buf calls when we find out
  1555. * that the current packet can't be processed right away afterall and we
  1556. * need to come back to it later. This is a very rare event and there's
  1557. * no effort to make this particularly efficient.
  1558. */
  1559. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  1560. int frags)
  1561. {
  1562. struct rx_sw_desc *d;
  1563. while (frags--) {
  1564. if (q->cidx == 0)
  1565. q->cidx = q->size - 1;
  1566. else
  1567. q->cidx--;
  1568. d = &q->sdesc[q->cidx];
  1569. d->page = si->frags[frags].page;
  1570. d->dma_addr |= RX_UNMAPPED_BUF;
  1571. q->avail++;
  1572. }
  1573. }
  1574. /**
  1575. * is_new_response - check if a response is newly written
  1576. * @r: the response descriptor
  1577. * @q: the response queue
  1578. *
  1579. * Returns true if a response descriptor contains a yet unprocessed
  1580. * response.
  1581. */
  1582. static inline bool is_new_response(const struct rsp_ctrl *r,
  1583. const struct sge_rspq *q)
  1584. {
  1585. return RSPD_GEN(r->type_gen) == q->gen;
  1586. }
  1587. /**
  1588. * rspq_next - advance to the next entry in a response queue
  1589. * @q: the queue
  1590. *
  1591. * Updates the state of a response queue to advance it to the next entry.
  1592. */
  1593. static inline void rspq_next(struct sge_rspq *q)
  1594. {
  1595. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  1596. if (unlikely(++q->cidx == q->size)) {
  1597. q->cidx = 0;
  1598. q->gen ^= 1;
  1599. q->cur_desc = q->desc;
  1600. }
  1601. }
  1602. /**
  1603. * process_responses - process responses from an SGE response queue
  1604. * @q: the ingress queue to process
  1605. * @budget: how many responses can be processed in this round
  1606. *
  1607. * Process responses from an SGE response queue up to the supplied budget.
  1608. * Responses include received packets as well as control messages from FW
  1609. * or HW.
  1610. *
  1611. * Additionally choose the interrupt holdoff time for the next interrupt
  1612. * on this queue. If the system is under memory shortage use a fairly
  1613. * long delay to help recovery.
  1614. */
  1615. static int process_responses(struct sge_rspq *q, int budget)
  1616. {
  1617. int ret, rsp_type;
  1618. int budget_left = budget;
  1619. const struct rsp_ctrl *rc;
  1620. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1621. struct adapter *adapter = q->adap;
  1622. struct sge *s = &adapter->sge;
  1623. while (likely(budget_left)) {
  1624. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1625. if (!is_new_response(rc, q))
  1626. break;
  1627. rmb();
  1628. rsp_type = RSPD_TYPE(rc->type_gen);
  1629. if (likely(rsp_type == RSP_TYPE_FLBUF)) {
  1630. struct page_frag *fp;
  1631. struct pkt_gl si;
  1632. const struct rx_sw_desc *rsd;
  1633. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  1634. if (len & RSPD_NEWBUF) {
  1635. if (likely(q->offset > 0)) {
  1636. free_rx_bufs(q->adap, &rxq->fl, 1);
  1637. q->offset = 0;
  1638. }
  1639. len = RSPD_LEN(len);
  1640. }
  1641. si.tot_len = len;
  1642. /* gather packet fragments */
  1643. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  1644. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  1645. bufsz = get_buf_size(adapter, rsd);
  1646. fp->page = rsd->page;
  1647. fp->offset = q->offset;
  1648. fp->size = min(bufsz, len);
  1649. len -= fp->size;
  1650. if (!len)
  1651. break;
  1652. unmap_rx_buf(q->adap, &rxq->fl);
  1653. }
  1654. /*
  1655. * Last buffer remains mapped so explicitly make it
  1656. * coherent for CPU access.
  1657. */
  1658. dma_sync_single_for_cpu(q->adap->pdev_dev,
  1659. get_buf_addr(rsd),
  1660. fp->size, DMA_FROM_DEVICE);
  1661. si.va = page_address(si.frags[0].page) +
  1662. si.frags[0].offset;
  1663. prefetch(si.va);
  1664. si.nfrags = frags + 1;
  1665. ret = q->handler(q, q->cur_desc, &si);
  1666. if (likely(ret == 0))
  1667. q->offset += ALIGN(fp->size, s->fl_align);
  1668. else
  1669. restore_rx_bufs(&si, &rxq->fl, frags);
  1670. } else if (likely(rsp_type == RSP_TYPE_CPL)) {
  1671. ret = q->handler(q, q->cur_desc, NULL);
  1672. } else {
  1673. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  1674. }
  1675. if (unlikely(ret)) {
  1676. /* couldn't process descriptor, back off for recovery */
  1677. q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
  1678. break;
  1679. }
  1680. rspq_next(q);
  1681. budget_left--;
  1682. }
  1683. if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
  1684. __refill_fl(q->adap, &rxq->fl);
  1685. return budget - budget_left;
  1686. }
  1687. /**
  1688. * napi_rx_handler - the NAPI handler for Rx processing
  1689. * @napi: the napi instance
  1690. * @budget: how many packets we can process in this round
  1691. *
  1692. * Handler for new data events when using NAPI. This does not need any
  1693. * locking or protection from interrupts as data interrupts are off at
  1694. * this point and other adapter interrupts do not interfere (the latter
  1695. * in not a concern at all with MSI-X as non-data interrupts then have
  1696. * a separate handler).
  1697. */
  1698. static int napi_rx_handler(struct napi_struct *napi, int budget)
  1699. {
  1700. unsigned int params;
  1701. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  1702. int work_done = process_responses(q, budget);
  1703. if (likely(work_done < budget)) {
  1704. napi_complete(napi);
  1705. params = q->next_intr_params;
  1706. q->next_intr_params = q->intr_params;
  1707. } else
  1708. params = QINTR_TIMER_IDX(7);
  1709. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS), CIDXINC(work_done) |
  1710. INGRESSQID((u32)q->cntxt_id) | SEINTARM(params));
  1711. return work_done;
  1712. }
  1713. /*
  1714. * The MSI-X interrupt handler for an SGE response queue.
  1715. */
  1716. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  1717. {
  1718. struct sge_rspq *q = cookie;
  1719. napi_schedule(&q->napi);
  1720. return IRQ_HANDLED;
  1721. }
  1722. /*
  1723. * Process the indirect interrupt entries in the interrupt queue and kick off
  1724. * NAPI for each queue that has generated an entry.
  1725. */
  1726. static unsigned int process_intrq(struct adapter *adap)
  1727. {
  1728. unsigned int credits;
  1729. const struct rsp_ctrl *rc;
  1730. struct sge_rspq *q = &adap->sge.intrq;
  1731. spin_lock(&adap->sge.intrq_lock);
  1732. for (credits = 0; ; credits++) {
  1733. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1734. if (!is_new_response(rc, q))
  1735. break;
  1736. rmb();
  1737. if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
  1738. unsigned int qid = ntohl(rc->pldbuflen_qid);
  1739. qid -= adap->sge.ingr_start;
  1740. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  1741. }
  1742. rspq_next(q);
  1743. }
  1744. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS), CIDXINC(credits) |
  1745. INGRESSQID(q->cntxt_id) | SEINTARM(q->intr_params));
  1746. spin_unlock(&adap->sge.intrq_lock);
  1747. return credits;
  1748. }
  1749. /*
  1750. * The MSI interrupt handler, which handles data events from SGE response queues
  1751. * as well as error and other async events as they all use the same MSI vector.
  1752. */
  1753. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  1754. {
  1755. struct adapter *adap = cookie;
  1756. t4_slow_intr_handler(adap);
  1757. process_intrq(adap);
  1758. return IRQ_HANDLED;
  1759. }
  1760. /*
  1761. * Interrupt handler for legacy INTx interrupts.
  1762. * Handles data events from SGE response queues as well as error and other
  1763. * async events as they all use the same interrupt line.
  1764. */
  1765. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  1766. {
  1767. struct adapter *adap = cookie;
  1768. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
  1769. if (t4_slow_intr_handler(adap) | process_intrq(adap))
  1770. return IRQ_HANDLED;
  1771. return IRQ_NONE; /* probably shared interrupt */
  1772. }
  1773. /**
  1774. * t4_intr_handler - select the top-level interrupt handler
  1775. * @adap: the adapter
  1776. *
  1777. * Selects the top-level interrupt handler based on the type of interrupts
  1778. * (MSI-X, MSI, or INTx).
  1779. */
  1780. irq_handler_t t4_intr_handler(struct adapter *adap)
  1781. {
  1782. if (adap->flags & USING_MSIX)
  1783. return t4_sge_intr_msix;
  1784. if (adap->flags & USING_MSI)
  1785. return t4_intr_msi;
  1786. return t4_intr_intx;
  1787. }
  1788. static void sge_rx_timer_cb(unsigned long data)
  1789. {
  1790. unsigned long m;
  1791. unsigned int i, idma_same_state_cnt[2];
  1792. struct adapter *adap = (struct adapter *)data;
  1793. struct sge *s = &adap->sge;
  1794. for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
  1795. for (m = s->starving_fl[i]; m; m &= m - 1) {
  1796. struct sge_eth_rxq *rxq;
  1797. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  1798. struct sge_fl *fl = s->egr_map[id];
  1799. clear_bit(id, s->starving_fl);
  1800. smp_mb__after_atomic();
  1801. if (fl_starving(fl)) {
  1802. rxq = container_of(fl, struct sge_eth_rxq, fl);
  1803. if (napi_reschedule(&rxq->rspq.napi))
  1804. fl->starving++;
  1805. else
  1806. set_bit(id, s->starving_fl);
  1807. }
  1808. }
  1809. t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
  1810. idma_same_state_cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
  1811. idma_same_state_cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1812. for (i = 0; i < 2; i++) {
  1813. u32 debug0, debug11;
  1814. /* If the Ingress DMA Same State Counter ("timer") is less
  1815. * than 1s, then we can reset our synthesized Stall Timer and
  1816. * continue. If we have previously emitted warnings about a
  1817. * potential stalled Ingress Queue, issue a note indicating
  1818. * that the Ingress Queue has resumed forward progress.
  1819. */
  1820. if (idma_same_state_cnt[i] < s->idma_1s_thresh) {
  1821. if (s->idma_stalled[i] >= SGE_IDMA_WARN_THRESH)
  1822. CH_WARN(adap, "SGE idma%d, queue%u,resumed after %d sec\n",
  1823. i, s->idma_qid[i],
  1824. s->idma_stalled[i]/HZ);
  1825. s->idma_stalled[i] = 0;
  1826. continue;
  1827. }
  1828. /* Synthesize an SGE Ingress DMA Same State Timer in the Hz
  1829. * domain. The first time we get here it'll be because we
  1830. * passed the 1s Threshold; each additional time it'll be
  1831. * because the RX Timer Callback is being fired on its regular
  1832. * schedule.
  1833. *
  1834. * If the stall is below our Potential Hung Ingress Queue
  1835. * Warning Threshold, continue.
  1836. */
  1837. if (s->idma_stalled[i] == 0)
  1838. s->idma_stalled[i] = HZ;
  1839. else
  1840. s->idma_stalled[i] += RX_QCHECK_PERIOD;
  1841. if (s->idma_stalled[i] < SGE_IDMA_WARN_THRESH)
  1842. continue;
  1843. /* We'll issue a warning every SGE_IDMA_WARN_REPEAT Hz */
  1844. if (((s->idma_stalled[i] - HZ) % SGE_IDMA_WARN_REPEAT) != 0)
  1845. continue;
  1846. /* Read and save the SGE IDMA State and Queue ID information.
  1847. * We do this every time in case it changes across time ...
  1848. */
  1849. t4_write_reg(adap, SGE_DEBUG_INDEX, 0);
  1850. debug0 = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1851. s->idma_state[i] = (debug0 >> (i * 9)) & 0x3f;
  1852. t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
  1853. debug11 = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1854. s->idma_qid[i] = (debug11 >> (i * 16)) & 0xffff;
  1855. CH_WARN(adap, "SGE idma%u, queue%u, maybe stuck state%u %dsecs (debug0=%#x, debug11=%#x)\n",
  1856. i, s->idma_qid[i], s->idma_state[i],
  1857. s->idma_stalled[i]/HZ, debug0, debug11);
  1858. t4_sge_decode_idma_state(adap, s->idma_state[i]);
  1859. }
  1860. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  1861. }
  1862. static void sge_tx_timer_cb(unsigned long data)
  1863. {
  1864. unsigned long m;
  1865. unsigned int i, budget;
  1866. struct adapter *adap = (struct adapter *)data;
  1867. struct sge *s = &adap->sge;
  1868. for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
  1869. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  1870. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  1871. struct sge_ofld_txq *txq = s->egr_map[id];
  1872. clear_bit(id, s->txq_maperr);
  1873. tasklet_schedule(&txq->qresume_tsk);
  1874. }
  1875. budget = MAX_TIMER_TX_RECLAIM;
  1876. i = s->ethtxq_rover;
  1877. do {
  1878. struct sge_eth_txq *q = &s->ethtxq[i];
  1879. if (q->q.in_use &&
  1880. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  1881. __netif_tx_trylock(q->txq)) {
  1882. int avail = reclaimable(&q->q);
  1883. if (avail) {
  1884. if (avail > budget)
  1885. avail = budget;
  1886. free_tx_desc(adap, &q->q, avail, true);
  1887. q->q.in_use -= avail;
  1888. budget -= avail;
  1889. }
  1890. __netif_tx_unlock(q->txq);
  1891. }
  1892. if (++i >= s->ethqsets)
  1893. i = 0;
  1894. } while (budget && i != s->ethtxq_rover);
  1895. s->ethtxq_rover = i;
  1896. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  1897. }
  1898. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  1899. struct net_device *dev, int intr_idx,
  1900. struct sge_fl *fl, rspq_handler_t hnd)
  1901. {
  1902. int ret, flsz = 0;
  1903. struct fw_iq_cmd c;
  1904. struct sge *s = &adap->sge;
  1905. struct port_info *pi = netdev_priv(dev);
  1906. /* Size needs to be multiple of 16, including status entry. */
  1907. iq->size = roundup(iq->size, 16);
  1908. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  1909. &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
  1910. if (!iq->desc)
  1911. return -ENOMEM;
  1912. memset(&c, 0, sizeof(c));
  1913. c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
  1914. FW_CMD_WRITE | FW_CMD_EXEC |
  1915. FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
  1916. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
  1917. FW_LEN16(c));
  1918. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
  1919. FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
  1920. FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
  1921. FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
  1922. -intr_idx - 1));
  1923. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
  1924. FW_IQ_CMD_IQGTSMODE |
  1925. FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
  1926. FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
  1927. c.iqsize = htons(iq->size);
  1928. c.iqaddr = cpu_to_be64(iq->phys_addr);
  1929. if (fl) {
  1930. fl->size = roundup(fl->size, 8);
  1931. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  1932. sizeof(struct rx_sw_desc), &fl->addr,
  1933. &fl->sdesc, s->stat_len, NUMA_NO_NODE);
  1934. if (!fl->desc)
  1935. goto fl_nomem;
  1936. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  1937. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN(1) |
  1938. FW_IQ_CMD_FL0FETCHRO(1) |
  1939. FW_IQ_CMD_FL0DATARO(1) |
  1940. FW_IQ_CMD_FL0PADEN(1));
  1941. c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
  1942. FW_IQ_CMD_FL0FBMAX(3));
  1943. c.fl0size = htons(flsz);
  1944. c.fl0addr = cpu_to_be64(fl->addr);
  1945. }
  1946. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  1947. if (ret)
  1948. goto err;
  1949. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  1950. iq->cur_desc = iq->desc;
  1951. iq->cidx = 0;
  1952. iq->gen = 1;
  1953. iq->next_intr_params = iq->intr_params;
  1954. iq->cntxt_id = ntohs(c.iqid);
  1955. iq->abs_id = ntohs(c.physiqid);
  1956. iq->size--; /* subtract status entry */
  1957. iq->netdev = dev;
  1958. iq->handler = hnd;
  1959. /* set offset to -1 to distinguish ingress queues without FL */
  1960. iq->offset = fl ? 0 : -1;
  1961. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  1962. if (fl) {
  1963. fl->cntxt_id = ntohs(c.fl0id);
  1964. fl->avail = fl->pend_cred = 0;
  1965. fl->pidx = fl->cidx = 0;
  1966. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  1967. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  1968. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  1969. }
  1970. return 0;
  1971. fl_nomem:
  1972. ret = -ENOMEM;
  1973. err:
  1974. if (iq->desc) {
  1975. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  1976. iq->desc, iq->phys_addr);
  1977. iq->desc = NULL;
  1978. }
  1979. if (fl && fl->desc) {
  1980. kfree(fl->sdesc);
  1981. fl->sdesc = NULL;
  1982. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  1983. fl->desc, fl->addr);
  1984. fl->desc = NULL;
  1985. }
  1986. return ret;
  1987. }
  1988. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  1989. {
  1990. q->cntxt_id = id;
  1991. if (!is_t4(adap->params.chip)) {
  1992. unsigned int s_qpp;
  1993. unsigned short udb_density;
  1994. unsigned long qpshift;
  1995. int page;
  1996. s_qpp = QUEUESPERPAGEPF1 * adap->fn;
  1997. udb_density = 1 << QUEUESPERPAGEPF0_GET((t4_read_reg(adap,
  1998. SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp));
  1999. qpshift = PAGE_SHIFT - ilog2(udb_density);
  2000. q->udb = q->cntxt_id << qpshift;
  2001. q->udb &= PAGE_MASK;
  2002. page = q->udb / PAGE_SIZE;
  2003. q->udb += (q->cntxt_id - (page * udb_density)) * 128;
  2004. }
  2005. q->in_use = 0;
  2006. q->cidx = q->pidx = 0;
  2007. q->stops = q->restarts = 0;
  2008. q->stat = (void *)&q->desc[q->size];
  2009. spin_lock_init(&q->db_lock);
  2010. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  2011. }
  2012. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  2013. struct net_device *dev, struct netdev_queue *netdevq,
  2014. unsigned int iqid)
  2015. {
  2016. int ret, nentries;
  2017. struct fw_eq_eth_cmd c;
  2018. struct sge *s = &adap->sge;
  2019. struct port_info *pi = netdev_priv(dev);
  2020. /* Add status entries */
  2021. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2022. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2023. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2024. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2025. netdev_queue_numa_node_read(netdevq));
  2026. if (!txq->q.desc)
  2027. return -ENOMEM;
  2028. memset(&c, 0, sizeof(c));
  2029. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
  2030. FW_CMD_WRITE | FW_CMD_EXEC |
  2031. FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
  2032. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
  2033. FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
  2034. c.viid_pkd = htonl(FW_EQ_ETH_CMD_VIID(pi->viid));
  2035. c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
  2036. FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
  2037. FW_EQ_ETH_CMD_FETCHRO(1) |
  2038. FW_EQ_ETH_CMD_IQID(iqid));
  2039. c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
  2040. FW_EQ_ETH_CMD_FBMAX(3) |
  2041. FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
  2042. FW_EQ_ETH_CMD_EQSIZE(nentries));
  2043. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2044. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2045. if (ret) {
  2046. kfree(txq->q.sdesc);
  2047. txq->q.sdesc = NULL;
  2048. dma_free_coherent(adap->pdev_dev,
  2049. nentries * sizeof(struct tx_desc),
  2050. txq->q.desc, txq->q.phys_addr);
  2051. txq->q.desc = NULL;
  2052. return ret;
  2053. }
  2054. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  2055. txq->txq = netdevq;
  2056. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  2057. txq->mapping_err = 0;
  2058. return 0;
  2059. }
  2060. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  2061. struct net_device *dev, unsigned int iqid,
  2062. unsigned int cmplqid)
  2063. {
  2064. int ret, nentries;
  2065. struct fw_eq_ctrl_cmd c;
  2066. struct sge *s = &adap->sge;
  2067. struct port_info *pi = netdev_priv(dev);
  2068. /* Add status entries */
  2069. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2070. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  2071. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  2072. NULL, 0, NUMA_NO_NODE);
  2073. if (!txq->q.desc)
  2074. return -ENOMEM;
  2075. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
  2076. FW_CMD_WRITE | FW_CMD_EXEC |
  2077. FW_EQ_CTRL_CMD_PFN(adap->fn) |
  2078. FW_EQ_CTRL_CMD_VFN(0));
  2079. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
  2080. FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
  2081. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
  2082. c.physeqid_pkd = htonl(0);
  2083. c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
  2084. FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
  2085. FW_EQ_CTRL_CMD_FETCHRO |
  2086. FW_EQ_CTRL_CMD_IQID(iqid));
  2087. c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
  2088. FW_EQ_CTRL_CMD_FBMAX(3) |
  2089. FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
  2090. FW_EQ_CTRL_CMD_EQSIZE(nentries));
  2091. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2092. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2093. if (ret) {
  2094. dma_free_coherent(adap->pdev_dev,
  2095. nentries * sizeof(struct tx_desc),
  2096. txq->q.desc, txq->q.phys_addr);
  2097. txq->q.desc = NULL;
  2098. return ret;
  2099. }
  2100. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
  2101. txq->adap = adap;
  2102. skb_queue_head_init(&txq->sendq);
  2103. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  2104. txq->full = 0;
  2105. return 0;
  2106. }
  2107. int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
  2108. struct net_device *dev, unsigned int iqid)
  2109. {
  2110. int ret, nentries;
  2111. struct fw_eq_ofld_cmd c;
  2112. struct sge *s = &adap->sge;
  2113. struct port_info *pi = netdev_priv(dev);
  2114. /* Add status entries */
  2115. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2116. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2117. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2118. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2119. NUMA_NO_NODE);
  2120. if (!txq->q.desc)
  2121. return -ENOMEM;
  2122. memset(&c, 0, sizeof(c));
  2123. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
  2124. FW_CMD_WRITE | FW_CMD_EXEC |
  2125. FW_EQ_OFLD_CMD_PFN(adap->fn) |
  2126. FW_EQ_OFLD_CMD_VFN(0));
  2127. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
  2128. FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
  2129. c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
  2130. FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
  2131. FW_EQ_OFLD_CMD_FETCHRO(1) |
  2132. FW_EQ_OFLD_CMD_IQID(iqid));
  2133. c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
  2134. FW_EQ_OFLD_CMD_FBMAX(3) |
  2135. FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
  2136. FW_EQ_OFLD_CMD_EQSIZE(nentries));
  2137. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2138. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2139. if (ret) {
  2140. kfree(txq->q.sdesc);
  2141. txq->q.sdesc = NULL;
  2142. dma_free_coherent(adap->pdev_dev,
  2143. nentries * sizeof(struct tx_desc),
  2144. txq->q.desc, txq->q.phys_addr);
  2145. txq->q.desc = NULL;
  2146. return ret;
  2147. }
  2148. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  2149. txq->adap = adap;
  2150. skb_queue_head_init(&txq->sendq);
  2151. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  2152. txq->full = 0;
  2153. txq->mapping_err = 0;
  2154. return 0;
  2155. }
  2156. static void free_txq(struct adapter *adap, struct sge_txq *q)
  2157. {
  2158. struct sge *s = &adap->sge;
  2159. dma_free_coherent(adap->pdev_dev,
  2160. q->size * sizeof(struct tx_desc) + s->stat_len,
  2161. q->desc, q->phys_addr);
  2162. q->cntxt_id = 0;
  2163. q->sdesc = NULL;
  2164. q->desc = NULL;
  2165. }
  2166. static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  2167. struct sge_fl *fl)
  2168. {
  2169. struct sge *s = &adap->sge;
  2170. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  2171. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  2172. t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
  2173. rq->cntxt_id, fl_id, 0xffff);
  2174. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  2175. rq->desc, rq->phys_addr);
  2176. netif_napi_del(&rq->napi);
  2177. rq->netdev = NULL;
  2178. rq->cntxt_id = rq->abs_id = 0;
  2179. rq->desc = NULL;
  2180. if (fl) {
  2181. free_rx_bufs(adap, fl, fl->avail);
  2182. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  2183. fl->desc, fl->addr);
  2184. kfree(fl->sdesc);
  2185. fl->sdesc = NULL;
  2186. fl->cntxt_id = 0;
  2187. fl->desc = NULL;
  2188. }
  2189. }
  2190. /**
  2191. * t4_free_sge_resources - free SGE resources
  2192. * @adap: the adapter
  2193. *
  2194. * Frees resources used by the SGE queue sets.
  2195. */
  2196. void t4_free_sge_resources(struct adapter *adap)
  2197. {
  2198. int i;
  2199. struct sge_eth_rxq *eq = adap->sge.ethrxq;
  2200. struct sge_eth_txq *etq = adap->sge.ethtxq;
  2201. struct sge_ofld_rxq *oq = adap->sge.ofldrxq;
  2202. /* clean up Ethernet Tx/Rx queues */
  2203. for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
  2204. if (eq->rspq.desc)
  2205. free_rspq_fl(adap, &eq->rspq, &eq->fl);
  2206. if (etq->q.desc) {
  2207. t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
  2208. etq->q.cntxt_id);
  2209. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  2210. kfree(etq->q.sdesc);
  2211. free_txq(adap, &etq->q);
  2212. }
  2213. }
  2214. /* clean up RDMA and iSCSI Rx queues */
  2215. for (i = 0; i < adap->sge.ofldqsets; i++, oq++) {
  2216. if (oq->rspq.desc)
  2217. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2218. }
  2219. for (i = 0, oq = adap->sge.rdmarxq; i < adap->sge.rdmaqs; i++, oq++) {
  2220. if (oq->rspq.desc)
  2221. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2222. }
  2223. for (i = 0, oq = adap->sge.rdmaciq; i < adap->sge.rdmaciqs; i++, oq++) {
  2224. if (oq->rspq.desc)
  2225. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2226. }
  2227. /* clean up offload Tx queues */
  2228. for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
  2229. struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
  2230. if (q->q.desc) {
  2231. tasklet_kill(&q->qresume_tsk);
  2232. t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
  2233. q->q.cntxt_id);
  2234. free_tx_desc(adap, &q->q, q->q.in_use, false);
  2235. kfree(q->q.sdesc);
  2236. __skb_queue_purge(&q->sendq);
  2237. free_txq(adap, &q->q);
  2238. }
  2239. }
  2240. /* clean up control Tx queues */
  2241. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  2242. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  2243. if (cq->q.desc) {
  2244. tasklet_kill(&cq->qresume_tsk);
  2245. t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
  2246. cq->q.cntxt_id);
  2247. __skb_queue_purge(&cq->sendq);
  2248. free_txq(adap, &cq->q);
  2249. }
  2250. }
  2251. if (adap->sge.fw_evtq.desc)
  2252. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  2253. if (adap->sge.intrq.desc)
  2254. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  2255. /* clear the reverse egress queue map */
  2256. memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
  2257. }
  2258. void t4_sge_start(struct adapter *adap)
  2259. {
  2260. adap->sge.ethtxq_rover = 0;
  2261. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2262. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2263. }
  2264. /**
  2265. * t4_sge_stop - disable SGE operation
  2266. * @adap: the adapter
  2267. *
  2268. * Stop tasklets and timers associated with the DMA engine. Note that
  2269. * this is effective only if measures have been taken to disable any HW
  2270. * events that may restart them.
  2271. */
  2272. void t4_sge_stop(struct adapter *adap)
  2273. {
  2274. int i;
  2275. struct sge *s = &adap->sge;
  2276. if (in_interrupt()) /* actions below require waiting */
  2277. return;
  2278. if (s->rx_timer.function)
  2279. del_timer_sync(&s->rx_timer);
  2280. if (s->tx_timer.function)
  2281. del_timer_sync(&s->tx_timer);
  2282. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
  2283. struct sge_ofld_txq *q = &s->ofldtxq[i];
  2284. if (q->q.desc)
  2285. tasklet_kill(&q->qresume_tsk);
  2286. }
  2287. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  2288. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  2289. if (cq->q.desc)
  2290. tasklet_kill(&cq->qresume_tsk);
  2291. }
  2292. }
  2293. /**
  2294. * t4_sge_init - initialize SGE
  2295. * @adap: the adapter
  2296. *
  2297. * Performs SGE initialization needed every time after a chip reset.
  2298. * We do not initialize any of the queues here, instead the driver
  2299. * top-level must request them individually.
  2300. *
  2301. * Called in two different modes:
  2302. *
  2303. * 1. Perform actual hardware initialization and record hard-coded
  2304. * parameters which were used. This gets used when we're the
  2305. * Master PF and the Firmware Configuration File support didn't
  2306. * work for some reason.
  2307. *
  2308. * 2. We're not the Master PF or initialization was performed with
  2309. * a Firmware Configuration File. In this case we need to grab
  2310. * any of the SGE operating parameters that we need to have in
  2311. * order to do our job and make sure we can live with them ...
  2312. */
  2313. static int t4_sge_init_soft(struct adapter *adap)
  2314. {
  2315. struct sge *s = &adap->sge;
  2316. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  2317. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  2318. u32 ingress_rx_threshold;
  2319. /*
  2320. * Verify that CPL messages are going to the Ingress Queue for
  2321. * process_responses() and that only packet data is going to the
  2322. * Free Lists.
  2323. */
  2324. if ((t4_read_reg(adap, SGE_CONTROL) & RXPKTCPLMODE_MASK) !=
  2325. RXPKTCPLMODE(X_RXPKTCPLMODE_SPLIT)) {
  2326. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  2327. return -EINVAL;
  2328. }
  2329. /*
  2330. * Validate the Host Buffer Register Array indices that we want to
  2331. * use ...
  2332. *
  2333. * XXX Note that we should really read through the Host Buffer Size
  2334. * XXX register array and find the indices of the Buffer Sizes which
  2335. * XXX meet our needs!
  2336. */
  2337. #define READ_FL_BUF(x) \
  2338. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0+(x)*sizeof(u32))
  2339. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  2340. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  2341. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  2342. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  2343. /* We only bother using the Large Page logic if the Large Page Buffer
  2344. * is larger than our Page Size Buffer.
  2345. */
  2346. if (fl_large_pg <= fl_small_pg)
  2347. fl_large_pg = 0;
  2348. #undef READ_FL_BUF
  2349. /* The Page Size Buffer must be exactly equal to our Page Size and the
  2350. * Large Page Size Buffer should be 0 (per above) or a power of 2.
  2351. */
  2352. if (fl_small_pg != PAGE_SIZE ||
  2353. (fl_large_pg & (fl_large_pg-1)) != 0) {
  2354. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  2355. fl_small_pg, fl_large_pg);
  2356. return -EINVAL;
  2357. }
  2358. if (fl_large_pg)
  2359. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  2360. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  2361. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  2362. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  2363. fl_small_mtu, fl_large_mtu);
  2364. return -EINVAL;
  2365. }
  2366. /*
  2367. * Retrieve our RX interrupt holdoff timer values and counter
  2368. * threshold values from the SGE parameters.
  2369. */
  2370. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1);
  2371. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3);
  2372. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5);
  2373. s->timer_val[0] = core_ticks_to_us(adap,
  2374. TIMERVALUE0_GET(timer_value_0_and_1));
  2375. s->timer_val[1] = core_ticks_to_us(adap,
  2376. TIMERVALUE1_GET(timer_value_0_and_1));
  2377. s->timer_val[2] = core_ticks_to_us(adap,
  2378. TIMERVALUE2_GET(timer_value_2_and_3));
  2379. s->timer_val[3] = core_ticks_to_us(adap,
  2380. TIMERVALUE3_GET(timer_value_2_and_3));
  2381. s->timer_val[4] = core_ticks_to_us(adap,
  2382. TIMERVALUE4_GET(timer_value_4_and_5));
  2383. s->timer_val[5] = core_ticks_to_us(adap,
  2384. TIMERVALUE5_GET(timer_value_4_and_5));
  2385. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD);
  2386. s->counter_val[0] = THRESHOLD_0_GET(ingress_rx_threshold);
  2387. s->counter_val[1] = THRESHOLD_1_GET(ingress_rx_threshold);
  2388. s->counter_val[2] = THRESHOLD_2_GET(ingress_rx_threshold);
  2389. s->counter_val[3] = THRESHOLD_3_GET(ingress_rx_threshold);
  2390. return 0;
  2391. }
  2392. static int t4_sge_init_hard(struct adapter *adap)
  2393. {
  2394. struct sge *s = &adap->sge;
  2395. /*
  2396. * Set up our basic SGE mode to deliver CPL messages to our Ingress
  2397. * Queue and Packet Date to the Free List.
  2398. */
  2399. t4_set_reg_field(adap, SGE_CONTROL, RXPKTCPLMODE_MASK,
  2400. RXPKTCPLMODE_MASK);
  2401. /*
  2402. * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
  2403. * and generate an interrupt when this occurs so we can recover.
  2404. */
  2405. if (is_t4(adap->params.chip)) {
  2406. t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
  2407. V_HP_INT_THRESH(M_HP_INT_THRESH) |
  2408. V_LP_INT_THRESH(M_LP_INT_THRESH),
  2409. V_HP_INT_THRESH(dbfifo_int_thresh) |
  2410. V_LP_INT_THRESH(dbfifo_int_thresh));
  2411. } else {
  2412. t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
  2413. V_LP_INT_THRESH_T5(M_LP_INT_THRESH_T5),
  2414. V_LP_INT_THRESH_T5(dbfifo_int_thresh));
  2415. t4_set_reg_field(adap, SGE_DBFIFO_STATUS2,
  2416. V_HP_INT_THRESH_T5(M_HP_INT_THRESH_T5),
  2417. V_HP_INT_THRESH_T5(dbfifo_int_thresh));
  2418. }
  2419. t4_set_reg_field(adap, A_SGE_DOORBELL_CONTROL, F_ENABLE_DROP,
  2420. F_ENABLE_DROP);
  2421. /*
  2422. * SGE_FL_BUFFER_SIZE0 (RX_SMALL_PG_BUF) is set up by
  2423. * t4_fixup_host_params().
  2424. */
  2425. s->fl_pg_order = FL_PG_ORDER;
  2426. if (s->fl_pg_order)
  2427. t4_write_reg(adap,
  2428. SGE_FL_BUFFER_SIZE0+RX_LARGE_PG_BUF*sizeof(u32),
  2429. PAGE_SIZE << FL_PG_ORDER);
  2430. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_SMALL_MTU_BUF*sizeof(u32),
  2431. FL_MTU_SMALL_BUFSIZE(adap));
  2432. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_LARGE_MTU_BUF*sizeof(u32),
  2433. FL_MTU_LARGE_BUFSIZE(adap));
  2434. /*
  2435. * Note that the SGE Ingress Packet Count Interrupt Threshold and
  2436. * Timer Holdoff values must be supplied by our caller.
  2437. */
  2438. t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
  2439. THRESHOLD_0(s->counter_val[0]) |
  2440. THRESHOLD_1(s->counter_val[1]) |
  2441. THRESHOLD_2(s->counter_val[2]) |
  2442. THRESHOLD_3(s->counter_val[3]));
  2443. t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
  2444. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
  2445. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
  2446. t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
  2447. TIMERVALUE2(us_to_core_ticks(adap, s->timer_val[2])) |
  2448. TIMERVALUE3(us_to_core_ticks(adap, s->timer_val[3])));
  2449. t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
  2450. TIMERVALUE4(us_to_core_ticks(adap, s->timer_val[4])) |
  2451. TIMERVALUE5(us_to_core_ticks(adap, s->timer_val[5])));
  2452. return 0;
  2453. }
  2454. int t4_sge_init(struct adapter *adap)
  2455. {
  2456. struct sge *s = &adap->sge;
  2457. u32 sge_control, sge_conm_ctrl;
  2458. int ret, egress_threshold;
  2459. /*
  2460. * Ingress Padding Boundary and Egress Status Page Size are set up by
  2461. * t4_fixup_host_params().
  2462. */
  2463. sge_control = t4_read_reg(adap, SGE_CONTROL);
  2464. s->pktshift = PKTSHIFT_GET(sge_control);
  2465. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_MASK) ? 128 : 64;
  2466. s->fl_align = 1 << (INGPADBOUNDARY_GET(sge_control) +
  2467. X_INGPADBOUNDARY_SHIFT);
  2468. if (adap->flags & USING_SOFT_PARAMS)
  2469. ret = t4_sge_init_soft(adap);
  2470. else
  2471. ret = t4_sge_init_hard(adap);
  2472. if (ret < 0)
  2473. return ret;
  2474. /*
  2475. * A FL with <= fl_starve_thres buffers is starving and a periodic
  2476. * timer will attempt to refill it. This needs to be larger than the
  2477. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  2478. * stuck waiting for new packets while the SGE is waiting for us to
  2479. * give it more Free List entries. (Note that the SGE's Egress
  2480. * Congestion Threshold is in units of 2 Free List pointers.) For T4,
  2481. * there was only a single field to control this. For T5 there's the
  2482. * original field which now only applies to Unpacked Mode Free List
  2483. * buffers and a new field which only applies to Packed Mode Free List
  2484. * buffers.
  2485. */
  2486. sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL);
  2487. if (is_t4(adap->params.chip))
  2488. egress_threshold = EGRTHRESHOLD_GET(sge_conm_ctrl);
  2489. else
  2490. egress_threshold = EGRTHRESHOLDPACKING_GET(sge_conm_ctrl);
  2491. s->fl_starve_thres = 2*egress_threshold + 1;
  2492. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
  2493. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
  2494. s->idma_1s_thresh = core_ticks_per_usec(adap) * 1000000; /* 1 s */
  2495. s->idma_stalled[0] = 0;
  2496. s->idma_stalled[1] = 0;
  2497. spin_lock_init(&s->intrq_lock);
  2498. return 0;
  2499. }