sge.c 76 KB

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