sge.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2014 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. #ifdef CONFIG_NET_RX_BUSY_POLL
  46. #include <net/busy_poll.h>
  47. #endif /* CONFIG_NET_RX_BUSY_POLL */
  48. #ifdef CONFIG_CHELSIO_T4_FCOE
  49. #include <scsi/fc/fc_fcoe.h>
  50. #endif /* CONFIG_CHELSIO_T4_FCOE */
  51. #include "cxgb4.h"
  52. #include "t4_regs.h"
  53. #include "t4_values.h"
  54. #include "t4_msg.h"
  55. #include "t4fw_api.h"
  56. /*
  57. * Rx buffer size. We use largish buffers if possible but settle for single
  58. * pages under memory shortage.
  59. */
  60. #if PAGE_SHIFT >= 16
  61. # define FL_PG_ORDER 0
  62. #else
  63. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  64. #endif
  65. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  66. #define RX_COPY_THRES 256
  67. #define RX_PULL_LEN 128
  68. /*
  69. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  70. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  71. */
  72. #define RX_PKT_SKB_LEN 512
  73. /*
  74. * Max number of Tx descriptors we clean up at a time. Should be modest as
  75. * freeing skbs isn't cheap and it happens while holding locks. We just need
  76. * to free packets faster than they arrive, we eventually catch up and keep
  77. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
  78. */
  79. #define MAX_TX_RECLAIM 16
  80. /*
  81. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  82. * allocating buffers isn't cheap either.
  83. */
  84. #define MAX_RX_REFILL 16U
  85. /*
  86. * Period of the Rx queue check timer. This timer is infrequent as it has
  87. * something to do only when the system experiences severe memory shortage.
  88. */
  89. #define RX_QCHECK_PERIOD (HZ / 2)
  90. /*
  91. * Period of the Tx queue check timer.
  92. */
  93. #define TX_QCHECK_PERIOD (HZ / 2)
  94. /*
  95. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  96. */
  97. #define MAX_TIMER_TX_RECLAIM 100
  98. /*
  99. * Timer index used when backing off due to memory shortage.
  100. */
  101. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  102. /*
  103. * Suspend an Ethernet Tx queue with fewer available descriptors than this.
  104. * This is the same as calc_tx_descs() for a TSO packet with
  105. * nr_frags == MAX_SKB_FRAGS.
  106. */
  107. #define ETHTXQ_STOP_THRES \
  108. (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
  109. /*
  110. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  111. * for a full sized WR.
  112. */
  113. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  114. /*
  115. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  116. * into a WR.
  117. */
  118. #define MAX_IMM_TX_PKT_LEN 256
  119. /*
  120. * Max size of a WR sent through a control Tx queue.
  121. */
  122. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  123. struct tx_sw_desc { /* SW state per Tx descriptor */
  124. struct sk_buff *skb;
  125. struct ulptx_sgl *sgl;
  126. };
  127. struct rx_sw_desc { /* SW state per Rx descriptor */
  128. struct page *page;
  129. dma_addr_t dma_addr;
  130. };
  131. /*
  132. * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
  133. * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
  134. * We could easily support more but there doesn't seem to be much need for
  135. * that ...
  136. */
  137. #define FL_MTU_SMALL 1500
  138. #define FL_MTU_LARGE 9000
  139. static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
  140. unsigned int mtu)
  141. {
  142. struct sge *s = &adapter->sge;
  143. return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
  144. }
  145. #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
  146. #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
  147. /*
  148. * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
  149. * these to specify the buffer size as an index into the SGE Free List Buffer
  150. * Size register array. We also use bit 4, when the buffer has been unmapped
  151. * for DMA, but this is of course never sent to the hardware and is only used
  152. * to prevent double unmappings. All of the above requires that the Free List
  153. * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
  154. * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
  155. * Free List Buffer alignment is 32 bytes, this works out for us ...
  156. */
  157. enum {
  158. RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
  159. RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
  160. RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
  161. /*
  162. * XXX We shouldn't depend on being able to use these indices.
  163. * XXX Especially when some other Master PF has initialized the
  164. * XXX adapter or we use the Firmware Configuration File. We
  165. * XXX should really search through the Host Buffer Size register
  166. * XXX array for the appropriately sized buffer indices.
  167. */
  168. RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
  169. RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
  170. RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
  171. RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
  172. };
  173. static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
  174. #define MIN_NAPI_WORK 1
  175. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  176. {
  177. return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
  178. }
  179. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  180. {
  181. return !(d->dma_addr & RX_UNMAPPED_BUF);
  182. }
  183. /**
  184. * txq_avail - return the number of available slots in a Tx queue
  185. * @q: the Tx queue
  186. *
  187. * Returns the number of descriptors in a Tx queue available to write new
  188. * packets.
  189. */
  190. static inline unsigned int txq_avail(const struct sge_txq *q)
  191. {
  192. return q->size - 1 - q->in_use;
  193. }
  194. /**
  195. * fl_cap - return the capacity of a free-buffer list
  196. * @fl: the FL
  197. *
  198. * Returns the capacity of a free-buffer list. The capacity is less than
  199. * the size because one descriptor needs to be left unpopulated, otherwise
  200. * HW will think the FL is empty.
  201. */
  202. static inline unsigned int fl_cap(const struct sge_fl *fl)
  203. {
  204. return fl->size - 8; /* 1 descriptor = 8 buffers */
  205. }
  206. /**
  207. * fl_starving - return whether a Free List is starving.
  208. * @adapter: pointer to the adapter
  209. * @fl: the Free List
  210. *
  211. * Tests specified Free List to see whether the number of buffers
  212. * available to the hardware has falled below our "starvation"
  213. * threshold.
  214. */
  215. static inline bool fl_starving(const struct adapter *adapter,
  216. const struct sge_fl *fl)
  217. {
  218. const struct sge *s = &adapter->sge;
  219. return fl->avail - fl->pend_cred <= s->fl_starve_thres;
  220. }
  221. static int map_skb(struct device *dev, const struct sk_buff *skb,
  222. dma_addr_t *addr)
  223. {
  224. const skb_frag_t *fp, *end;
  225. const struct skb_shared_info *si;
  226. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  227. if (dma_mapping_error(dev, *addr))
  228. goto out_err;
  229. si = skb_shinfo(skb);
  230. end = &si->frags[si->nr_frags];
  231. for (fp = si->frags; fp < end; fp++) {
  232. *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
  233. DMA_TO_DEVICE);
  234. if (dma_mapping_error(dev, *addr))
  235. goto unwind;
  236. }
  237. return 0;
  238. unwind:
  239. while (fp-- > si->frags)
  240. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  241. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  242. out_err:
  243. return -ENOMEM;
  244. }
  245. #ifdef CONFIG_NEED_DMA_MAP_STATE
  246. static void unmap_skb(struct device *dev, const struct sk_buff *skb,
  247. const dma_addr_t *addr)
  248. {
  249. const skb_frag_t *fp, *end;
  250. const struct skb_shared_info *si;
  251. dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
  252. si = skb_shinfo(skb);
  253. end = &si->frags[si->nr_frags];
  254. for (fp = si->frags; fp < end; fp++)
  255. dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
  256. }
  257. /**
  258. * deferred_unmap_destructor - unmap a packet when it is freed
  259. * @skb: the packet
  260. *
  261. * This is the packet destructor used for Tx packets that need to remain
  262. * mapped until they are freed rather than until their Tx descriptors are
  263. * freed.
  264. */
  265. static void deferred_unmap_destructor(struct sk_buff *skb)
  266. {
  267. unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
  268. }
  269. #endif
  270. static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
  271. const struct ulptx_sgl *sgl, const struct sge_txq *q)
  272. {
  273. const struct ulptx_sge_pair *p;
  274. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  275. if (likely(skb_headlen(skb)))
  276. dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  277. DMA_TO_DEVICE);
  278. else {
  279. dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  280. DMA_TO_DEVICE);
  281. nfrags--;
  282. }
  283. /*
  284. * the complexity below is because of the possibility of a wrap-around
  285. * in the middle of an SGL
  286. */
  287. for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
  288. if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
  289. unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  290. ntohl(p->len[0]), DMA_TO_DEVICE);
  291. dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
  292. ntohl(p->len[1]), DMA_TO_DEVICE);
  293. p++;
  294. } else if ((u8 *)p == (u8 *)q->stat) {
  295. p = (const struct ulptx_sge_pair *)q->desc;
  296. goto unmap;
  297. } else if ((u8 *)p + 8 == (u8 *)q->stat) {
  298. const __be64 *addr = (const __be64 *)q->desc;
  299. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  300. ntohl(p->len[0]), DMA_TO_DEVICE);
  301. dma_unmap_page(dev, be64_to_cpu(addr[1]),
  302. ntohl(p->len[1]), DMA_TO_DEVICE);
  303. p = (const struct ulptx_sge_pair *)&addr[2];
  304. } else {
  305. const __be64 *addr = (const __be64 *)q->desc;
  306. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  307. ntohl(p->len[0]), DMA_TO_DEVICE);
  308. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  309. ntohl(p->len[1]), DMA_TO_DEVICE);
  310. p = (const struct ulptx_sge_pair *)&addr[1];
  311. }
  312. }
  313. if (nfrags) {
  314. __be64 addr;
  315. if ((u8 *)p == (u8 *)q->stat)
  316. p = (const struct ulptx_sge_pair *)q->desc;
  317. addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
  318. *(const __be64 *)q->desc;
  319. dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
  320. DMA_TO_DEVICE);
  321. }
  322. }
  323. /**
  324. * free_tx_desc - reclaims Tx descriptors and their buffers
  325. * @adapter: the adapter
  326. * @q: the Tx queue to reclaim descriptors from
  327. * @n: the number of descriptors to reclaim
  328. * @unmap: whether the buffers should be unmapped for DMA
  329. *
  330. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  331. * Tx buffers. Called with the Tx queue lock held.
  332. */
  333. static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
  334. unsigned int n, bool unmap)
  335. {
  336. struct tx_sw_desc *d;
  337. unsigned int cidx = q->cidx;
  338. struct device *dev = adap->pdev_dev;
  339. d = &q->sdesc[cidx];
  340. while (n--) {
  341. if (d->skb) { /* an SGL is present */
  342. if (unmap)
  343. unmap_sgl(dev, d->skb, d->sgl, q);
  344. dev_consume_skb_any(d->skb);
  345. d->skb = NULL;
  346. }
  347. ++d;
  348. if (++cidx == q->size) {
  349. cidx = 0;
  350. d = q->sdesc;
  351. }
  352. }
  353. q->cidx = cidx;
  354. }
  355. /*
  356. * Return the number of reclaimable descriptors in a Tx queue.
  357. */
  358. static inline int reclaimable(const struct sge_txq *q)
  359. {
  360. int hw_cidx = ntohs(ACCESS_ONCE(q->stat->cidx));
  361. hw_cidx -= q->cidx;
  362. return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
  363. }
  364. /**
  365. * reclaim_completed_tx - reclaims completed Tx descriptors
  366. * @adap: the adapter
  367. * @q: the Tx queue to reclaim completed descriptors from
  368. * @unmap: whether the buffers should be unmapped for DMA
  369. *
  370. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  371. * and frees the associated buffers if possible. Called with the Tx
  372. * queue locked.
  373. */
  374. static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  375. bool unmap)
  376. {
  377. int avail = reclaimable(q);
  378. if (avail) {
  379. /*
  380. * Limit the amount of clean up work we do at a time to keep
  381. * the Tx lock hold time O(1).
  382. */
  383. if (avail > MAX_TX_RECLAIM)
  384. avail = MAX_TX_RECLAIM;
  385. free_tx_desc(adap, q, avail, unmap);
  386. q->in_use -= avail;
  387. }
  388. }
  389. static inline int get_buf_size(struct adapter *adapter,
  390. const struct rx_sw_desc *d)
  391. {
  392. struct sge *s = &adapter->sge;
  393. unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
  394. int buf_size;
  395. switch (rx_buf_size_idx) {
  396. case RX_SMALL_PG_BUF:
  397. buf_size = PAGE_SIZE;
  398. break;
  399. case RX_LARGE_PG_BUF:
  400. buf_size = PAGE_SIZE << s->fl_pg_order;
  401. break;
  402. case RX_SMALL_MTU_BUF:
  403. buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
  404. break;
  405. case RX_LARGE_MTU_BUF:
  406. buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
  407. break;
  408. default:
  409. BUG_ON(1);
  410. }
  411. return buf_size;
  412. }
  413. /**
  414. * free_rx_bufs - free the Rx buffers on an SGE free list
  415. * @adap: the adapter
  416. * @q: the SGE free list to free buffers from
  417. * @n: how many buffers to free
  418. *
  419. * Release the next @n buffers on an SGE free-buffer Rx queue. The
  420. * buffers must be made inaccessible to HW before calling this function.
  421. */
  422. static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
  423. {
  424. while (n--) {
  425. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  426. if (is_buf_mapped(d))
  427. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  428. get_buf_size(adap, d),
  429. PCI_DMA_FROMDEVICE);
  430. put_page(d->page);
  431. d->page = NULL;
  432. if (++q->cidx == q->size)
  433. q->cidx = 0;
  434. q->avail--;
  435. }
  436. }
  437. /**
  438. * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
  439. * @adap: the adapter
  440. * @q: the SGE free list
  441. *
  442. * Unmap the current buffer on an SGE free-buffer Rx queue. The
  443. * buffer must be made inaccessible to HW before calling this function.
  444. *
  445. * This is similar to @free_rx_bufs above but does not free the buffer.
  446. * Do note that the FL still loses any further access to the buffer.
  447. */
  448. static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
  449. {
  450. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  451. if (is_buf_mapped(d))
  452. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  453. get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
  454. d->page = NULL;
  455. if (++q->cidx == q->size)
  456. q->cidx = 0;
  457. q->avail--;
  458. }
  459. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  460. {
  461. if (q->pend_cred >= 8) {
  462. u32 val = adap->params.arch.sge_fl_db;
  463. if (is_t4(adap->params.chip))
  464. val |= PIDX_V(q->pend_cred / 8);
  465. else
  466. val |= PIDX_T5_V(q->pend_cred / 8);
  467. /* Make sure all memory writes to the Free List queue are
  468. * committed before we tell the hardware about them.
  469. */
  470. wmb();
  471. /* If we don't have access to the new User Doorbell (T5+), use
  472. * the old doorbell mechanism; otherwise use the new BAR2
  473. * mechanism.
  474. */
  475. if (unlikely(q->bar2_addr == NULL)) {
  476. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  477. val | QID_V(q->cntxt_id));
  478. } else {
  479. writel(val | QID_V(q->bar2_qid),
  480. q->bar2_addr + SGE_UDB_KDOORBELL);
  481. /* This Write memory Barrier will force the write to
  482. * the User Doorbell area to be flushed.
  483. */
  484. wmb();
  485. }
  486. q->pend_cred &= 7;
  487. }
  488. }
  489. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  490. dma_addr_t mapping)
  491. {
  492. sd->page = pg;
  493. sd->dma_addr = mapping; /* includes size low bits */
  494. }
  495. /**
  496. * refill_fl - refill an SGE Rx buffer ring
  497. * @adap: the adapter
  498. * @q: the ring to refill
  499. * @n: the number of new buffers to allocate
  500. * @gfp: the gfp flags for the allocations
  501. *
  502. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  503. * allocated with the supplied gfp flags. The caller must assure that
  504. * @n does not exceed the queue's capacity. If afterwards the queue is
  505. * found critically low mark it as starving in the bitmap of starving FLs.
  506. *
  507. * Returns the number of buffers allocated.
  508. */
  509. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  510. gfp_t gfp)
  511. {
  512. struct sge *s = &adap->sge;
  513. struct page *pg;
  514. dma_addr_t mapping;
  515. unsigned int cred = q->avail;
  516. __be64 *d = &q->desc[q->pidx];
  517. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  518. int node;
  519. #ifdef CONFIG_DEBUG_FS
  520. if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
  521. goto out;
  522. #endif
  523. gfp |= __GFP_NOWARN;
  524. node = dev_to_node(adap->pdev_dev);
  525. if (s->fl_pg_order == 0)
  526. goto alloc_small_pages;
  527. /*
  528. * Prefer large buffers
  529. */
  530. while (n) {
  531. pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
  532. if (unlikely(!pg)) {
  533. q->large_alloc_failed++;
  534. break; /* fall back to single pages */
  535. }
  536. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  537. PAGE_SIZE << s->fl_pg_order,
  538. PCI_DMA_FROMDEVICE);
  539. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  540. __free_pages(pg, s->fl_pg_order);
  541. q->mapping_err++;
  542. goto out; /* do not try small pages for this error */
  543. }
  544. mapping |= RX_LARGE_PG_BUF;
  545. *d++ = cpu_to_be64(mapping);
  546. set_rx_sw_desc(sd, pg, mapping);
  547. sd++;
  548. q->avail++;
  549. if (++q->pidx == q->size) {
  550. q->pidx = 0;
  551. sd = q->sdesc;
  552. d = q->desc;
  553. }
  554. n--;
  555. }
  556. alloc_small_pages:
  557. while (n--) {
  558. pg = alloc_pages_node(node, gfp, 0);
  559. if (unlikely(!pg)) {
  560. q->alloc_failed++;
  561. break;
  562. }
  563. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  564. PCI_DMA_FROMDEVICE);
  565. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  566. put_page(pg);
  567. q->mapping_err++;
  568. goto out;
  569. }
  570. *d++ = cpu_to_be64(mapping);
  571. set_rx_sw_desc(sd, pg, mapping);
  572. sd++;
  573. q->avail++;
  574. if (++q->pidx == q->size) {
  575. q->pidx = 0;
  576. sd = q->sdesc;
  577. d = q->desc;
  578. }
  579. }
  580. out: cred = q->avail - cred;
  581. q->pend_cred += cred;
  582. ring_fl_db(adap, q);
  583. if (unlikely(fl_starving(adap, q))) {
  584. smp_wmb();
  585. q->low++;
  586. set_bit(q->cntxt_id - adap->sge.egr_start,
  587. adap->sge.starving_fl);
  588. }
  589. return cred;
  590. }
  591. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  592. {
  593. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  594. GFP_ATOMIC);
  595. }
  596. /**
  597. * alloc_ring - allocate resources for an SGE descriptor ring
  598. * @dev: the PCI device's core device
  599. * @nelem: the number of descriptors
  600. * @elem_size: the size of each descriptor
  601. * @sw_size: the size of the SW state associated with each ring element
  602. * @phys: the physical address of the allocated ring
  603. * @metadata: address of the array holding the SW state for the ring
  604. * @stat_size: extra space in HW ring for status information
  605. * @node: preferred node for memory allocations
  606. *
  607. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  608. * free buffer lists, or response queues. Each SGE ring requires
  609. * space for its HW descriptors plus, optionally, space for the SW state
  610. * associated with each HW entry (the metadata). The function returns
  611. * three values: the virtual address for the HW ring (the return value
  612. * of the function), the bus address of the HW ring, and the address
  613. * of the SW ring.
  614. */
  615. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  616. size_t sw_size, dma_addr_t *phys, void *metadata,
  617. size_t stat_size, int node)
  618. {
  619. size_t len = nelem * elem_size + stat_size;
  620. void *s = NULL;
  621. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  622. if (!p)
  623. return NULL;
  624. if (sw_size) {
  625. s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
  626. if (!s) {
  627. dma_free_coherent(dev, len, p, *phys);
  628. return NULL;
  629. }
  630. }
  631. if (metadata)
  632. *(void **)metadata = s;
  633. memset(p, 0, len);
  634. return p;
  635. }
  636. /**
  637. * sgl_len - calculates the size of an SGL of the given capacity
  638. * @n: the number of SGL entries
  639. *
  640. * Calculates the number of flits needed for a scatter/gather list that
  641. * can hold the given number of entries.
  642. */
  643. static inline unsigned int sgl_len(unsigned int n)
  644. {
  645. /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
  646. * addresses. The DSGL Work Request starts off with a 32-bit DSGL
  647. * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
  648. * repeated sequences of { Length[i], Length[i+1], Address[i],
  649. * Address[i+1] } (this ensures that all addresses are on 64-bit
  650. * boundaries). If N is even, then Length[N+1] should be set to 0 and
  651. * Address[N+1] is omitted.
  652. *
  653. * The following calculation incorporates all of the above. It's
  654. * somewhat hard to follow but, briefly: the "+2" accounts for the
  655. * first two flits which include the DSGL header, Length0 and
  656. * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
  657. * flits for every pair of the remaining N) +1 if (n-1) is odd; and
  658. * finally the "+((n-1)&1)" adds the one remaining flit needed if
  659. * (n-1) is odd ...
  660. */
  661. n--;
  662. return (3 * n) / 2 + (n & 1) + 2;
  663. }
  664. /**
  665. * flits_to_desc - returns the num of Tx descriptors for the given flits
  666. * @n: the number of flits
  667. *
  668. * Returns the number of Tx descriptors needed for the supplied number
  669. * of flits.
  670. */
  671. static inline unsigned int flits_to_desc(unsigned int n)
  672. {
  673. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  674. return DIV_ROUND_UP(n, 8);
  675. }
  676. /**
  677. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  678. * @skb: the packet
  679. *
  680. * Returns whether an Ethernet packet is small enough to fit as
  681. * immediate data. Return value corresponds to headroom required.
  682. */
  683. static inline int is_eth_imm(const struct sk_buff *skb)
  684. {
  685. int hdrlen = skb_shinfo(skb)->gso_size ?
  686. sizeof(struct cpl_tx_pkt_lso_core) : 0;
  687. hdrlen += sizeof(struct cpl_tx_pkt);
  688. if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
  689. return hdrlen;
  690. return 0;
  691. }
  692. /**
  693. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  694. * @skb: the packet
  695. *
  696. * Returns the number of flits needed for a Tx WR for the given Ethernet
  697. * packet, including the needed WR and CPL headers.
  698. */
  699. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  700. {
  701. unsigned int flits;
  702. int hdrlen = is_eth_imm(skb);
  703. /* If the skb is small enough, we can pump it out as a work request
  704. * with only immediate data. In that case we just have to have the
  705. * TX Packet header plus the skb data in the Work Request.
  706. */
  707. if (hdrlen)
  708. return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
  709. /* Otherwise, we're going to have to construct a Scatter gather list
  710. * of the skb body and fragments. We also include the flits necessary
  711. * for the TX Packet Work Request and CPL. We always have a firmware
  712. * Write Header (incorporated as part of the cpl_tx_pkt_lso and
  713. * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
  714. * message or, if we're doing a Large Send Offload, an LSO CPL message
  715. * with an embedded TX Packet Write CPL message.
  716. */
  717. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  718. if (skb_shinfo(skb)->gso_size)
  719. flits += (sizeof(struct fw_eth_tx_pkt_wr) +
  720. sizeof(struct cpl_tx_pkt_lso_core) +
  721. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  722. else
  723. flits += (sizeof(struct fw_eth_tx_pkt_wr) +
  724. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  725. return flits;
  726. }
  727. /**
  728. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  729. * @skb: the packet
  730. *
  731. * Returns the number of Tx descriptors needed for the given Ethernet
  732. * packet, including the needed WR and CPL headers.
  733. */
  734. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  735. {
  736. return flits_to_desc(calc_tx_flits(skb));
  737. }
  738. /**
  739. * write_sgl - populate a scatter/gather list for a packet
  740. * @skb: the packet
  741. * @q: the Tx queue we are writing into
  742. * @sgl: starting location for writing the SGL
  743. * @end: points right after the end of the SGL
  744. * @start: start offset into skb main-body data to include in the SGL
  745. * @addr: the list of bus addresses for the SGL elements
  746. *
  747. * Generates a gather list for the buffers that make up a packet.
  748. * The caller must provide adequate space for the SGL that will be written.
  749. * The SGL includes all of the packet's page fragments and the data in its
  750. * main body except for the first @start bytes. @sgl must be 16-byte
  751. * aligned and within a Tx descriptor with available space. @end points
  752. * right after the end of the SGL but does not account for any potential
  753. * wrap around, i.e., @end > @sgl.
  754. */
  755. static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  756. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  757. const dma_addr_t *addr)
  758. {
  759. unsigned int i, len;
  760. struct ulptx_sge_pair *to;
  761. const struct skb_shared_info *si = skb_shinfo(skb);
  762. unsigned int nfrags = si->nr_frags;
  763. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  764. len = skb_headlen(skb) - start;
  765. if (likely(len)) {
  766. sgl->len0 = htonl(len);
  767. sgl->addr0 = cpu_to_be64(addr[0] + start);
  768. nfrags++;
  769. } else {
  770. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  771. sgl->addr0 = cpu_to_be64(addr[1]);
  772. }
  773. sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  774. ULPTX_NSGE_V(nfrags));
  775. if (likely(--nfrags == 0))
  776. return;
  777. /*
  778. * Most of the complexity below deals with the possibility we hit the
  779. * end of the queue in the middle of writing the SGL. For this case
  780. * only we create the SGL in a temporary buffer and then copy it.
  781. */
  782. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  783. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  784. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  785. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  786. to->addr[0] = cpu_to_be64(addr[i]);
  787. to->addr[1] = cpu_to_be64(addr[++i]);
  788. }
  789. if (nfrags) {
  790. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  791. to->len[1] = cpu_to_be32(0);
  792. to->addr[0] = cpu_to_be64(addr[i + 1]);
  793. }
  794. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  795. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  796. if (likely(part0))
  797. memcpy(sgl->sge, buf, part0);
  798. part1 = (u8 *)end - (u8 *)q->stat;
  799. memcpy(q->desc, (u8 *)buf + part0, part1);
  800. end = (void *)q->desc + part1;
  801. }
  802. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  803. *end = 0;
  804. }
  805. /* This function copies 64 byte coalesced work request to
  806. * memory mapped BAR2 space. For coalesced WR SGE fetches
  807. * data from the FIFO instead of from Host.
  808. */
  809. static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
  810. {
  811. int count = 8;
  812. while (count) {
  813. writeq(*src, dst);
  814. src++;
  815. dst++;
  816. count--;
  817. }
  818. }
  819. /**
  820. * ring_tx_db - check and potentially ring a Tx queue's doorbell
  821. * @adap: the adapter
  822. * @q: the Tx queue
  823. * @n: number of new descriptors to give to HW
  824. *
  825. * Ring the doorbel for a Tx queue.
  826. */
  827. static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  828. {
  829. /* Make sure that all writes to the TX Descriptors are committed
  830. * before we tell the hardware about them.
  831. */
  832. wmb();
  833. /* If we don't have access to the new User Doorbell (T5+), use the old
  834. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  835. */
  836. if (unlikely(q->bar2_addr == NULL)) {
  837. u32 val = PIDX_V(n);
  838. unsigned long flags;
  839. /* For T4 we need to participate in the Doorbell Recovery
  840. * mechanism.
  841. */
  842. spin_lock_irqsave(&q->db_lock, flags);
  843. if (!q->db_disabled)
  844. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  845. QID_V(q->cntxt_id) | val);
  846. else
  847. q->db_pidx_inc += n;
  848. q->db_pidx = q->pidx;
  849. spin_unlock_irqrestore(&q->db_lock, flags);
  850. } else {
  851. u32 val = PIDX_T5_V(n);
  852. /* T4 and later chips share the same PIDX field offset within
  853. * the doorbell, but T5 and later shrank the field in order to
  854. * gain a bit for Doorbell Priority. The field was absurdly
  855. * large in the first place (14 bits) so we just use the T5
  856. * and later limits and warn if a Queue ID is too large.
  857. */
  858. WARN_ON(val & DBPRIO_F);
  859. /* If we're only writing a single TX Descriptor and we can use
  860. * Inferred QID registers, we can use the Write Combining
  861. * Gather Buffer; otherwise we use the simple doorbell.
  862. */
  863. if (n == 1 && q->bar2_qid == 0) {
  864. int index = (q->pidx
  865. ? (q->pidx - 1)
  866. : (q->size - 1));
  867. u64 *wr = (u64 *)&q->desc[index];
  868. cxgb_pio_copy((u64 __iomem *)
  869. (q->bar2_addr + SGE_UDB_WCDOORBELL),
  870. wr);
  871. } else {
  872. writel(val | QID_V(q->bar2_qid),
  873. q->bar2_addr + SGE_UDB_KDOORBELL);
  874. }
  875. /* This Write Memory Barrier will force the write to the User
  876. * Doorbell area to be flushed. This is needed to prevent
  877. * writes on different CPUs for the same queue from hitting
  878. * the adapter out of order. This is required when some Work
  879. * Requests take the Write Combine Gather Buffer path (user
  880. * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
  881. * take the traditional path where we simply increment the
  882. * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
  883. * hardware DMA read the actual Work Request.
  884. */
  885. wmb();
  886. }
  887. }
  888. /**
  889. * inline_tx_skb - inline a packet's data into Tx descriptors
  890. * @skb: the packet
  891. * @q: the Tx queue where the packet will be inlined
  892. * @pos: starting position in the Tx queue where to inline the packet
  893. *
  894. * Inline a packet's contents directly into Tx descriptors, starting at
  895. * the given position within the Tx DMA ring.
  896. * Most of the complexity of this operation is dealing with wrap arounds
  897. * in the middle of the packet we want to inline.
  898. */
  899. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
  900. void *pos)
  901. {
  902. u64 *p;
  903. int left = (void *)q->stat - pos;
  904. if (likely(skb->len <= left)) {
  905. if (likely(!skb->data_len))
  906. skb_copy_from_linear_data(skb, pos, skb->len);
  907. else
  908. skb_copy_bits(skb, 0, pos, skb->len);
  909. pos += skb->len;
  910. } else {
  911. skb_copy_bits(skb, 0, pos, left);
  912. skb_copy_bits(skb, left, q->desc, skb->len - left);
  913. pos = (void *)q->desc + (skb->len - left);
  914. }
  915. /* 0-pad to multiple of 16 */
  916. p = PTR_ALIGN(pos, 8);
  917. if ((uintptr_t)p & 8)
  918. *p = 0;
  919. }
  920. static void *inline_tx_skb_header(const struct sk_buff *skb,
  921. const struct sge_txq *q, void *pos,
  922. int length)
  923. {
  924. u64 *p;
  925. int left = (void *)q->stat - pos;
  926. if (likely(length <= left)) {
  927. memcpy(pos, skb->data, length);
  928. pos += length;
  929. } else {
  930. memcpy(pos, skb->data, left);
  931. memcpy(q->desc, skb->data + left, length - left);
  932. pos = (void *)q->desc + (length - left);
  933. }
  934. /* 0-pad to multiple of 16 */
  935. p = PTR_ALIGN(pos, 8);
  936. if ((uintptr_t)p & 8) {
  937. *p = 0;
  938. return p + 1;
  939. }
  940. return p;
  941. }
  942. /*
  943. * Figure out what HW csum a packet wants and return the appropriate control
  944. * bits.
  945. */
  946. static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
  947. {
  948. int csum_type;
  949. const struct iphdr *iph = ip_hdr(skb);
  950. if (iph->version == 4) {
  951. if (iph->protocol == IPPROTO_TCP)
  952. csum_type = TX_CSUM_TCPIP;
  953. else if (iph->protocol == IPPROTO_UDP)
  954. csum_type = TX_CSUM_UDPIP;
  955. else {
  956. nocsum: /*
  957. * unknown protocol, disable HW csum
  958. * and hope a bad packet is detected
  959. */
  960. return TXPKT_L4CSUM_DIS_F;
  961. }
  962. } else {
  963. /*
  964. * this doesn't work with extension headers
  965. */
  966. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  967. if (ip6h->nexthdr == IPPROTO_TCP)
  968. csum_type = TX_CSUM_TCPIP6;
  969. else if (ip6h->nexthdr == IPPROTO_UDP)
  970. csum_type = TX_CSUM_UDPIP6;
  971. else
  972. goto nocsum;
  973. }
  974. if (likely(csum_type >= TX_CSUM_TCPIP)) {
  975. u64 hdr_len = TXPKT_IPHDR_LEN_V(skb_network_header_len(skb));
  976. int eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
  977. if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
  978. hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  979. else
  980. hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  981. return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
  982. } else {
  983. int start = skb_transport_offset(skb);
  984. return TXPKT_CSUM_TYPE_V(csum_type) |
  985. TXPKT_CSUM_START_V(start) |
  986. TXPKT_CSUM_LOC_V(start + skb->csum_offset);
  987. }
  988. }
  989. static void eth_txq_stop(struct sge_eth_txq *q)
  990. {
  991. netif_tx_stop_queue(q->txq);
  992. q->q.stops++;
  993. }
  994. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  995. {
  996. q->in_use += n;
  997. q->pidx += n;
  998. if (q->pidx >= q->size)
  999. q->pidx -= q->size;
  1000. }
  1001. #ifdef CONFIG_CHELSIO_T4_FCOE
  1002. static inline int
  1003. cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
  1004. const struct port_info *pi, u64 *cntrl)
  1005. {
  1006. const struct cxgb_fcoe *fcoe = &pi->fcoe;
  1007. if (!(fcoe->flags & CXGB_FCOE_ENABLED))
  1008. return 0;
  1009. if (skb->protocol != htons(ETH_P_FCOE))
  1010. return 0;
  1011. skb_reset_mac_header(skb);
  1012. skb->mac_len = sizeof(struct ethhdr);
  1013. skb_set_network_header(skb, skb->mac_len);
  1014. skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
  1015. if (!cxgb_fcoe_sof_eof_supported(adap, skb))
  1016. return -ENOTSUPP;
  1017. /* FC CRC offload */
  1018. *cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
  1019. TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
  1020. TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
  1021. TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
  1022. TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
  1023. return 0;
  1024. }
  1025. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1026. /**
  1027. * t4_eth_xmit - add a packet to an Ethernet Tx queue
  1028. * @skb: the packet
  1029. * @dev: the egress net device
  1030. *
  1031. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  1032. */
  1033. netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  1034. {
  1035. u32 wr_mid, ctrl0;
  1036. u64 cntrl, *end;
  1037. int qidx, credits;
  1038. unsigned int flits, ndesc;
  1039. struct adapter *adap;
  1040. struct sge_eth_txq *q;
  1041. const struct port_info *pi;
  1042. struct fw_eth_tx_pkt_wr *wr;
  1043. struct cpl_tx_pkt_core *cpl;
  1044. const struct skb_shared_info *ssi;
  1045. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  1046. bool immediate = false;
  1047. int len, max_pkt_len;
  1048. #ifdef CONFIG_CHELSIO_T4_FCOE
  1049. int err;
  1050. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1051. /*
  1052. * The chip min packet length is 10 octets but play safe and reject
  1053. * anything shorter than an Ethernet header.
  1054. */
  1055. if (unlikely(skb->len < ETH_HLEN)) {
  1056. out_free: dev_kfree_skb_any(skb);
  1057. return NETDEV_TX_OK;
  1058. }
  1059. /* Discard the packet if the length is greater than mtu */
  1060. max_pkt_len = ETH_HLEN + dev->mtu;
  1061. if (skb_vlan_tagged(skb))
  1062. max_pkt_len += VLAN_HLEN;
  1063. if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
  1064. goto out_free;
  1065. pi = netdev_priv(dev);
  1066. adap = pi->adapter;
  1067. qidx = skb_get_queue_mapping(skb);
  1068. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  1069. reclaim_completed_tx(adap, &q->q, true);
  1070. cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
  1071. #ifdef CONFIG_CHELSIO_T4_FCOE
  1072. err = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
  1073. if (unlikely(err == -ENOTSUPP))
  1074. goto out_free;
  1075. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1076. flits = calc_tx_flits(skb);
  1077. ndesc = flits_to_desc(flits);
  1078. credits = txq_avail(&q->q) - ndesc;
  1079. if (unlikely(credits < 0)) {
  1080. eth_txq_stop(q);
  1081. dev_err(adap->pdev_dev,
  1082. "%s: Tx ring %u full while queue awake!\n",
  1083. dev->name, qidx);
  1084. return NETDEV_TX_BUSY;
  1085. }
  1086. if (is_eth_imm(skb))
  1087. immediate = true;
  1088. if (!immediate &&
  1089. unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
  1090. q->mapping_err++;
  1091. goto out_free;
  1092. }
  1093. wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
  1094. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  1095. eth_txq_stop(q);
  1096. wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
  1097. }
  1098. wr = (void *)&q->q.desc[q->q.pidx];
  1099. wr->equiq_to_len16 = htonl(wr_mid);
  1100. wr->r3 = cpu_to_be64(0);
  1101. end = (u64 *)wr + flits;
  1102. len = immediate ? skb->len : 0;
  1103. ssi = skb_shinfo(skb);
  1104. if (ssi->gso_size) {
  1105. struct cpl_tx_pkt_lso *lso = (void *)wr;
  1106. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  1107. int l3hdr_len = skb_network_header_len(skb);
  1108. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1109. len += sizeof(*lso);
  1110. wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
  1111. FW_WR_IMMDLEN_V(len));
  1112. lso->c.lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
  1113. LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
  1114. LSO_IPV6_V(v6) |
  1115. LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
  1116. LSO_IPHDR_LEN_V(l3hdr_len / 4) |
  1117. LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
  1118. lso->c.ipid_ofst = htons(0);
  1119. lso->c.mss = htons(ssi->gso_size);
  1120. lso->c.seqno_offset = htonl(0);
  1121. if (is_t4(adap->params.chip))
  1122. lso->c.len = htonl(skb->len);
  1123. else
  1124. lso->c.len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
  1125. cpl = (void *)(lso + 1);
  1126. if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
  1127. cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1128. else
  1129. cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1130. cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
  1131. TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  1132. TXPKT_IPHDR_LEN_V(l3hdr_len);
  1133. q->tso++;
  1134. q->tx_cso += ssi->gso_segs;
  1135. } else {
  1136. len += sizeof(*cpl);
  1137. wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
  1138. FW_WR_IMMDLEN_V(len));
  1139. cpl = (void *)(wr + 1);
  1140. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1141. cntrl = hwcsum(adap->params.chip, skb) |
  1142. TXPKT_IPCSUM_DIS_F;
  1143. q->tx_cso++;
  1144. }
  1145. }
  1146. if (skb_vlan_tag_present(skb)) {
  1147. q->vlan_ins++;
  1148. cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
  1149. #ifdef CONFIG_CHELSIO_T4_FCOE
  1150. if (skb->protocol == htons(ETH_P_FCOE))
  1151. cntrl |= TXPKT_VLAN_V(
  1152. ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
  1153. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1154. }
  1155. ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_INTF_V(pi->tx_chan) |
  1156. TXPKT_PF_V(adap->pf);
  1157. #ifdef CONFIG_CHELSIO_T4_DCB
  1158. if (is_t4(adap->params.chip))
  1159. ctrl0 |= TXPKT_OVLAN_IDX_V(q->dcb_prio);
  1160. else
  1161. ctrl0 |= TXPKT_T5_OVLAN_IDX_V(q->dcb_prio);
  1162. #endif
  1163. cpl->ctrl0 = htonl(ctrl0);
  1164. cpl->pack = htons(0);
  1165. cpl->len = htons(skb->len);
  1166. cpl->ctrl1 = cpu_to_be64(cntrl);
  1167. if (immediate) {
  1168. inline_tx_skb(skb, &q->q, cpl + 1);
  1169. dev_consume_skb_any(skb);
  1170. } else {
  1171. int last_desc;
  1172. write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
  1173. addr);
  1174. skb_orphan(skb);
  1175. last_desc = q->q.pidx + ndesc - 1;
  1176. if (last_desc >= q->q.size)
  1177. last_desc -= q->q.size;
  1178. q->q.sdesc[last_desc].skb = skb;
  1179. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
  1180. }
  1181. txq_advance(&q->q, ndesc);
  1182. ring_tx_db(adap, &q->q, ndesc);
  1183. return NETDEV_TX_OK;
  1184. }
  1185. /**
  1186. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1187. * @q: the SGE control Tx queue
  1188. *
  1189. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  1190. * that send only immediate data (presently just the control queues) and
  1191. * thus do not have any sk_buffs to release.
  1192. */
  1193. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1194. {
  1195. int hw_cidx = ntohs(ACCESS_ONCE(q->stat->cidx));
  1196. int reclaim = hw_cidx - q->cidx;
  1197. if (reclaim < 0)
  1198. reclaim += q->size;
  1199. q->in_use -= reclaim;
  1200. q->cidx = hw_cidx;
  1201. }
  1202. /**
  1203. * is_imm - check whether a packet can be sent as immediate data
  1204. * @skb: the packet
  1205. *
  1206. * Returns true if a packet can be sent as a WR with immediate data.
  1207. */
  1208. static inline int is_imm(const struct sk_buff *skb)
  1209. {
  1210. return skb->len <= MAX_CTRL_WR_LEN;
  1211. }
  1212. /**
  1213. * ctrlq_check_stop - check if a control queue is full and should stop
  1214. * @q: the queue
  1215. * @wr: most recent WR written to the queue
  1216. *
  1217. * Check if a control queue has become full and should be stopped.
  1218. * We clean up control queue descriptors very lazily, only when we are out.
  1219. * If the queue is still full after reclaiming any completed descriptors
  1220. * we suspend it and have the last WR wake it up.
  1221. */
  1222. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  1223. {
  1224. reclaim_completed_tx_imm(&q->q);
  1225. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1226. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1227. q->q.stops++;
  1228. q->full = 1;
  1229. }
  1230. }
  1231. /**
  1232. * ctrl_xmit - send a packet through an SGE control Tx queue
  1233. * @q: the control queue
  1234. * @skb: the packet
  1235. *
  1236. * Send a packet through an SGE control Tx queue. Packets sent through
  1237. * a control queue must fit entirely as immediate data.
  1238. */
  1239. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  1240. {
  1241. unsigned int ndesc;
  1242. struct fw_wr_hdr *wr;
  1243. if (unlikely(!is_imm(skb))) {
  1244. WARN_ON(1);
  1245. dev_kfree_skb(skb);
  1246. return NET_XMIT_DROP;
  1247. }
  1248. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  1249. spin_lock(&q->sendq.lock);
  1250. if (unlikely(q->full)) {
  1251. skb->priority = ndesc; /* save for restart */
  1252. __skb_queue_tail(&q->sendq, skb);
  1253. spin_unlock(&q->sendq.lock);
  1254. return NET_XMIT_CN;
  1255. }
  1256. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1257. inline_tx_skb(skb, &q->q, wr);
  1258. txq_advance(&q->q, ndesc);
  1259. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  1260. ctrlq_check_stop(q, wr);
  1261. ring_tx_db(q->adap, &q->q, ndesc);
  1262. spin_unlock(&q->sendq.lock);
  1263. kfree_skb(skb);
  1264. return NET_XMIT_SUCCESS;
  1265. }
  1266. /**
  1267. * restart_ctrlq - restart a suspended control queue
  1268. * @data: the control queue to restart
  1269. *
  1270. * Resumes transmission on a suspended Tx control queue.
  1271. */
  1272. static void restart_ctrlq(unsigned long data)
  1273. {
  1274. struct sk_buff *skb;
  1275. unsigned int written = 0;
  1276. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  1277. spin_lock(&q->sendq.lock);
  1278. reclaim_completed_tx_imm(&q->q);
  1279. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  1280. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  1281. struct fw_wr_hdr *wr;
  1282. unsigned int ndesc = skb->priority; /* previously saved */
  1283. written += ndesc;
  1284. /* Write descriptors and free skbs outside the lock to limit
  1285. * wait times. q->full is still set so new skbs will be queued.
  1286. */
  1287. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1288. txq_advance(&q->q, ndesc);
  1289. spin_unlock(&q->sendq.lock);
  1290. inline_tx_skb(skb, &q->q, wr);
  1291. kfree_skb(skb);
  1292. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1293. unsigned long old = q->q.stops;
  1294. ctrlq_check_stop(q, wr);
  1295. if (q->q.stops != old) { /* suspended anew */
  1296. spin_lock(&q->sendq.lock);
  1297. goto ringdb;
  1298. }
  1299. }
  1300. if (written > 16) {
  1301. ring_tx_db(q->adap, &q->q, written);
  1302. written = 0;
  1303. }
  1304. spin_lock(&q->sendq.lock);
  1305. }
  1306. q->full = 0;
  1307. ringdb: if (written)
  1308. ring_tx_db(q->adap, &q->q, written);
  1309. spin_unlock(&q->sendq.lock);
  1310. }
  1311. /**
  1312. * t4_mgmt_tx - send a management message
  1313. * @adap: the adapter
  1314. * @skb: the packet containing the management message
  1315. *
  1316. * Send a management message through control queue 0.
  1317. */
  1318. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1319. {
  1320. int ret;
  1321. local_bh_disable();
  1322. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1323. local_bh_enable();
  1324. return ret;
  1325. }
  1326. /**
  1327. * is_ofld_imm - check whether a packet can be sent as immediate data
  1328. * @skb: the packet
  1329. *
  1330. * Returns true if a packet can be sent as an offload WR with immediate
  1331. * data. We currently use the same limit as for Ethernet packets.
  1332. */
  1333. static inline int is_ofld_imm(const struct sk_buff *skb)
  1334. {
  1335. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1336. }
  1337. /**
  1338. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1339. * @skb: the packet
  1340. *
  1341. * Returns the number of flits needed for the given offload packet.
  1342. * These packets are already fully constructed and no additional headers
  1343. * will be added.
  1344. */
  1345. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1346. {
  1347. unsigned int flits, cnt;
  1348. if (is_ofld_imm(skb))
  1349. return DIV_ROUND_UP(skb->len, 8);
  1350. flits = skb_transport_offset(skb) / 8U; /* headers */
  1351. cnt = skb_shinfo(skb)->nr_frags;
  1352. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1353. cnt++;
  1354. return flits + sgl_len(cnt);
  1355. }
  1356. /**
  1357. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1358. * @adap: the adapter
  1359. * @q: the queue to stop
  1360. *
  1361. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1362. * inability to map packets. A periodic timer attempts to restart
  1363. * queues so marked.
  1364. */
  1365. static void txq_stop_maperr(struct sge_ofld_txq *q)
  1366. {
  1367. q->mapping_err++;
  1368. q->q.stops++;
  1369. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1370. q->adap->sge.txq_maperr);
  1371. }
  1372. /**
  1373. * ofldtxq_stop - stop an offload Tx queue that has become full
  1374. * @q: the queue to stop
  1375. * @skb: the packet causing the queue to become full
  1376. *
  1377. * Stops an offload Tx queue that has become full and modifies the packet
  1378. * being written to request a wakeup.
  1379. */
  1380. static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
  1381. {
  1382. struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
  1383. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1384. q->q.stops++;
  1385. q->full = 1;
  1386. }
  1387. /**
  1388. * service_ofldq - service/restart a suspended offload queue
  1389. * @q: the offload queue
  1390. *
  1391. * Services an offload Tx queue by moving packets from its Pending Send
  1392. * Queue to the Hardware TX ring. The function starts and ends with the
  1393. * Send Queue locked, but drops the lock while putting the skb at the
  1394. * head of the Send Queue onto the Hardware TX Ring. Dropping the lock
  1395. * allows more skbs to be added to the Send Queue by other threads.
  1396. * The packet being processed at the head of the Pending Send Queue is
  1397. * left on the queue in case we experience DMA Mapping errors, etc.
  1398. * and need to give up and restart later.
  1399. *
  1400. * service_ofldq() can be thought of as a task which opportunistically
  1401. * uses other threads execution contexts. We use the Offload Queue
  1402. * boolean "service_ofldq_running" to make sure that only one instance
  1403. * is ever running at a time ...
  1404. */
  1405. static void service_ofldq(struct sge_ofld_txq *q)
  1406. {
  1407. u64 *pos, *before, *end;
  1408. int credits;
  1409. struct sk_buff *skb;
  1410. struct sge_txq *txq;
  1411. unsigned int left;
  1412. unsigned int written = 0;
  1413. unsigned int flits, ndesc;
  1414. /* If another thread is currently in service_ofldq() processing the
  1415. * Pending Send Queue then there's nothing to do. Otherwise, flag
  1416. * that we're doing the work and continue. Examining/modifying
  1417. * the Offload Queue boolean "service_ofldq_running" must be done
  1418. * while holding the Pending Send Queue Lock.
  1419. */
  1420. if (q->service_ofldq_running)
  1421. return;
  1422. q->service_ofldq_running = true;
  1423. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1424. /* We drop the lock while we're working with the skb at the
  1425. * head of the Pending Send Queue. This allows more skbs to
  1426. * be added to the Pending Send Queue while we're working on
  1427. * this one. We don't need to lock to guard the TX Ring
  1428. * updates because only one thread of execution is ever
  1429. * allowed into service_ofldq() at a time.
  1430. */
  1431. spin_unlock(&q->sendq.lock);
  1432. reclaim_completed_tx(q->adap, &q->q, false);
  1433. flits = skb->priority; /* previously saved */
  1434. ndesc = flits_to_desc(flits);
  1435. credits = txq_avail(&q->q) - ndesc;
  1436. BUG_ON(credits < 0);
  1437. if (unlikely(credits < TXQ_STOP_THRES))
  1438. ofldtxq_stop(q, skb);
  1439. pos = (u64 *)&q->q.desc[q->q.pidx];
  1440. if (is_ofld_imm(skb))
  1441. inline_tx_skb(skb, &q->q, pos);
  1442. else if (map_skb(q->adap->pdev_dev, skb,
  1443. (dma_addr_t *)skb->head)) {
  1444. txq_stop_maperr(q);
  1445. spin_lock(&q->sendq.lock);
  1446. break;
  1447. } else {
  1448. int last_desc, hdr_len = skb_transport_offset(skb);
  1449. /* The WR headers may not fit within one descriptor.
  1450. * So we need to deal with wrap-around here.
  1451. */
  1452. before = (u64 *)pos;
  1453. end = (u64 *)pos + flits;
  1454. txq = &q->q;
  1455. pos = (void *)inline_tx_skb_header(skb, &q->q,
  1456. (void *)pos,
  1457. hdr_len);
  1458. if (before > (u64 *)pos) {
  1459. left = (u8 *)end - (u8 *)txq->stat;
  1460. end = (void *)txq->desc + left;
  1461. }
  1462. /* If current position is already at the end of the
  1463. * ofld queue, reset the current to point to
  1464. * start of the queue and update the end ptr as well.
  1465. */
  1466. if (pos == (u64 *)txq->stat) {
  1467. left = (u8 *)end - (u8 *)txq->stat;
  1468. end = (void *)txq->desc + left;
  1469. pos = (void *)txq->desc;
  1470. }
  1471. write_sgl(skb, &q->q, (void *)pos,
  1472. end, hdr_len,
  1473. (dma_addr_t *)skb->head);
  1474. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1475. skb->dev = q->adap->port[0];
  1476. skb->destructor = deferred_unmap_destructor;
  1477. #endif
  1478. last_desc = q->q.pidx + ndesc - 1;
  1479. if (last_desc >= q->q.size)
  1480. last_desc -= q->q.size;
  1481. q->q.sdesc[last_desc].skb = skb;
  1482. }
  1483. txq_advance(&q->q, ndesc);
  1484. written += ndesc;
  1485. if (unlikely(written > 32)) {
  1486. ring_tx_db(q->adap, &q->q, written);
  1487. written = 0;
  1488. }
  1489. /* Reacquire the Pending Send Queue Lock so we can unlink the
  1490. * skb we've just successfully transferred to the TX Ring and
  1491. * loop for the next skb which may be at the head of the
  1492. * Pending Send Queue.
  1493. */
  1494. spin_lock(&q->sendq.lock);
  1495. __skb_unlink(skb, &q->sendq);
  1496. if (is_ofld_imm(skb))
  1497. kfree_skb(skb);
  1498. }
  1499. if (likely(written))
  1500. ring_tx_db(q->adap, &q->q, written);
  1501. /*Indicate that no thread is processing the Pending Send Queue
  1502. * currently.
  1503. */
  1504. q->service_ofldq_running = false;
  1505. }
  1506. /**
  1507. * ofld_xmit - send a packet through an offload queue
  1508. * @q: the Tx offload queue
  1509. * @skb: the packet
  1510. *
  1511. * Send an offload packet through an SGE offload queue.
  1512. */
  1513. static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
  1514. {
  1515. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1516. spin_lock(&q->sendq.lock);
  1517. /* Queue the new skb onto the Offload Queue's Pending Send Queue. If
  1518. * that results in this new skb being the only one on the queue, start
  1519. * servicing it. If there are other skbs already on the list, then
  1520. * either the queue is currently being processed or it's been stopped
  1521. * for some reason and it'll be restarted at a later time. Restart
  1522. * paths are triggered by events like experiencing a DMA Mapping Error
  1523. * or filling the Hardware TX Ring.
  1524. */
  1525. __skb_queue_tail(&q->sendq, skb);
  1526. if (q->sendq.qlen == 1)
  1527. service_ofldq(q);
  1528. spin_unlock(&q->sendq.lock);
  1529. return NET_XMIT_SUCCESS;
  1530. }
  1531. /**
  1532. * restart_ofldq - restart a suspended offload queue
  1533. * @data: the offload queue to restart
  1534. *
  1535. * Resumes transmission on a suspended Tx offload queue.
  1536. */
  1537. static void restart_ofldq(unsigned long data)
  1538. {
  1539. struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
  1540. spin_lock(&q->sendq.lock);
  1541. q->full = 0; /* the queue actually is completely empty now */
  1542. service_ofldq(q);
  1543. spin_unlock(&q->sendq.lock);
  1544. }
  1545. /**
  1546. * skb_txq - return the Tx queue an offload packet should use
  1547. * @skb: the packet
  1548. *
  1549. * Returns the Tx queue an offload packet should use as indicated by bits
  1550. * 1-15 in the packet's queue_mapping.
  1551. */
  1552. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1553. {
  1554. return skb->queue_mapping >> 1;
  1555. }
  1556. /**
  1557. * is_ctrl_pkt - return whether an offload packet is a control packet
  1558. * @skb: the packet
  1559. *
  1560. * Returns whether an offload packet should use an OFLD or a CTRL
  1561. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1562. */
  1563. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1564. {
  1565. return skb->queue_mapping & 1;
  1566. }
  1567. static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
  1568. {
  1569. unsigned int idx = skb_txq(skb);
  1570. if (unlikely(is_ctrl_pkt(skb))) {
  1571. /* Single ctrl queue is a requirement for LE workaround path */
  1572. if (adap->tids.nsftids)
  1573. idx = 0;
  1574. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1575. }
  1576. return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
  1577. }
  1578. /**
  1579. * t4_ofld_send - send an offload packet
  1580. * @adap: the adapter
  1581. * @skb: the packet
  1582. *
  1583. * Sends an offload packet. We use the packet queue_mapping to select the
  1584. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1585. * should be sent as regular or control, bits 1-15 select the queue.
  1586. */
  1587. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1588. {
  1589. int ret;
  1590. local_bh_disable();
  1591. ret = ofld_send(adap, skb);
  1592. local_bh_enable();
  1593. return ret;
  1594. }
  1595. /**
  1596. * cxgb4_ofld_send - send an offload packet
  1597. * @dev: the net device
  1598. * @skb: the packet
  1599. *
  1600. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1601. * intended for ULDs.
  1602. */
  1603. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1604. {
  1605. return t4_ofld_send(netdev2adap(dev), skb);
  1606. }
  1607. EXPORT_SYMBOL(cxgb4_ofld_send);
  1608. static inline void copy_frags(struct sk_buff *skb,
  1609. const struct pkt_gl *gl, unsigned int offset)
  1610. {
  1611. int i;
  1612. /* usually there's just one frag */
  1613. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  1614. gl->frags[0].offset + offset,
  1615. gl->frags[0].size - offset);
  1616. skb_shinfo(skb)->nr_frags = gl->nfrags;
  1617. for (i = 1; i < gl->nfrags; i++)
  1618. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  1619. gl->frags[i].offset,
  1620. gl->frags[i].size);
  1621. /* get a reference to the last page, we don't own it */
  1622. get_page(gl->frags[gl->nfrags - 1].page);
  1623. }
  1624. /**
  1625. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1626. * @gl: the gather list
  1627. * @skb_len: size of sk_buff main body if it carries fragments
  1628. * @pull_len: amount of data to move to the sk_buff's main body
  1629. *
  1630. * Builds an sk_buff from the given packet gather list. Returns the
  1631. * sk_buff or %NULL if sk_buff allocation failed.
  1632. */
  1633. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1634. unsigned int skb_len, unsigned int pull_len)
  1635. {
  1636. struct sk_buff *skb;
  1637. /*
  1638. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1639. * size, which is expected since buffers are at least PAGE_SIZEd.
  1640. * In this case packets up to RX_COPY_THRES have only one fragment.
  1641. */
  1642. if (gl->tot_len <= RX_COPY_THRES) {
  1643. skb = dev_alloc_skb(gl->tot_len);
  1644. if (unlikely(!skb))
  1645. goto out;
  1646. __skb_put(skb, gl->tot_len);
  1647. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1648. } else {
  1649. skb = dev_alloc_skb(skb_len);
  1650. if (unlikely(!skb))
  1651. goto out;
  1652. __skb_put(skb, pull_len);
  1653. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1654. copy_frags(skb, gl, pull_len);
  1655. skb->len = gl->tot_len;
  1656. skb->data_len = skb->len - pull_len;
  1657. skb->truesize += skb->data_len;
  1658. }
  1659. out: return skb;
  1660. }
  1661. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1662. /**
  1663. * t4_pktgl_free - free a packet gather list
  1664. * @gl: the gather list
  1665. *
  1666. * Releases the pages of a packet gather list. We do not own the last
  1667. * page on the list and do not free it.
  1668. */
  1669. static void t4_pktgl_free(const struct pkt_gl *gl)
  1670. {
  1671. int n;
  1672. const struct page_frag *p;
  1673. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1674. put_page(p->page);
  1675. }
  1676. /*
  1677. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1678. * be delivered to anyone and send it to the stack for capture.
  1679. */
  1680. static noinline int handle_trace_pkt(struct adapter *adap,
  1681. const struct pkt_gl *gl)
  1682. {
  1683. struct sk_buff *skb;
  1684. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  1685. if (unlikely(!skb)) {
  1686. t4_pktgl_free(gl);
  1687. return 0;
  1688. }
  1689. if (is_t4(adap->params.chip))
  1690. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  1691. else
  1692. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  1693. skb_reset_mac_header(skb);
  1694. skb->protocol = htons(0xffff);
  1695. skb->dev = adap->port[0];
  1696. netif_receive_skb(skb);
  1697. return 0;
  1698. }
  1699. /**
  1700. * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
  1701. * @adap: the adapter
  1702. * @hwtstamps: time stamp structure to update
  1703. * @sgetstamp: 60bit iqe timestamp
  1704. *
  1705. * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
  1706. * which is in Core Clock ticks into ktime_t and assign it
  1707. **/
  1708. static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
  1709. struct skb_shared_hwtstamps *hwtstamps,
  1710. u64 sgetstamp)
  1711. {
  1712. u64 ns;
  1713. u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
  1714. ns = div_u64(tmp, adap->params.vpd.cclk);
  1715. memset(hwtstamps, 0, sizeof(*hwtstamps));
  1716. hwtstamps->hwtstamp = ns_to_ktime(ns);
  1717. }
  1718. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1719. const struct cpl_rx_pkt *pkt)
  1720. {
  1721. struct adapter *adapter = rxq->rspq.adap;
  1722. struct sge *s = &adapter->sge;
  1723. struct port_info *pi;
  1724. int ret;
  1725. struct sk_buff *skb;
  1726. skb = napi_get_frags(&rxq->rspq.napi);
  1727. if (unlikely(!skb)) {
  1728. t4_pktgl_free(gl);
  1729. rxq->stats.rx_drops++;
  1730. return;
  1731. }
  1732. copy_frags(skb, gl, s->pktshift);
  1733. skb->len = gl->tot_len - s->pktshift;
  1734. skb->data_len = skb->len;
  1735. skb->truesize += skb->data_len;
  1736. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1737. skb_record_rx_queue(skb, rxq->rspq.idx);
  1738. pi = netdev_priv(skb->dev);
  1739. if (pi->rxtstamp)
  1740. cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
  1741. gl->sgetstamp);
  1742. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  1743. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1744. PKT_HASH_TYPE_L3);
  1745. if (unlikely(pkt->vlan_ex)) {
  1746. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1747. rxq->stats.vlan_ex++;
  1748. }
  1749. ret = napi_gro_frags(&rxq->rspq.napi);
  1750. if (ret == GRO_HELD)
  1751. rxq->stats.lro_pkts++;
  1752. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1753. rxq->stats.lro_merged++;
  1754. rxq->stats.pkts++;
  1755. rxq->stats.rx_cso++;
  1756. }
  1757. /**
  1758. * t4_ethrx_handler - process an ingress ethernet packet
  1759. * @q: the response queue that received the packet
  1760. * @rsp: the response queue descriptor holding the RX_PKT message
  1761. * @si: the gather list of packet fragments
  1762. *
  1763. * Process an ingress ethernet packet and deliver it to the stack.
  1764. */
  1765. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  1766. const struct pkt_gl *si)
  1767. {
  1768. bool csum_ok;
  1769. struct sk_buff *skb;
  1770. const struct cpl_rx_pkt *pkt;
  1771. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1772. struct sge *s = &q->adap->sge;
  1773. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  1774. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  1775. struct port_info *pi;
  1776. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  1777. return handle_trace_pkt(q->adap, si);
  1778. pkt = (const struct cpl_rx_pkt *)rsp;
  1779. csum_ok = pkt->csum_calc && !pkt->err_vec &&
  1780. (q->netdev->features & NETIF_F_RXCSUM);
  1781. if ((pkt->l2info & htonl(RXF_TCP_F)) &&
  1782. !(cxgb_poll_busy_polling(q)) &&
  1783. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  1784. do_gro(rxq, si, pkt);
  1785. return 0;
  1786. }
  1787. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  1788. if (unlikely(!skb)) {
  1789. t4_pktgl_free(si);
  1790. rxq->stats.rx_drops++;
  1791. return 0;
  1792. }
  1793. __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
  1794. skb->protocol = eth_type_trans(skb, q->netdev);
  1795. skb_record_rx_queue(skb, q->idx);
  1796. if (skb->dev->features & NETIF_F_RXHASH)
  1797. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1798. PKT_HASH_TYPE_L3);
  1799. rxq->stats.pkts++;
  1800. pi = netdev_priv(skb->dev);
  1801. if (pi->rxtstamp)
  1802. cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
  1803. si->sgetstamp);
  1804. if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
  1805. if (!pkt->ip_frag) {
  1806. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1807. rxq->stats.rx_cso++;
  1808. } else if (pkt->l2info & htonl(RXF_IP_F)) {
  1809. __sum16 c = (__force __sum16)pkt->csum;
  1810. skb->csum = csum_unfold(c);
  1811. skb->ip_summed = CHECKSUM_COMPLETE;
  1812. rxq->stats.rx_cso++;
  1813. }
  1814. } else {
  1815. skb_checksum_none_assert(skb);
  1816. #ifdef CONFIG_CHELSIO_T4_FCOE
  1817. #define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
  1818. RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
  1819. if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
  1820. if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
  1821. (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
  1822. if (!(pkt->err_vec & cpu_to_be16(RXERR_CSUM_F)))
  1823. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1824. }
  1825. }
  1826. #undef CPL_RX_PKT_FLAGS
  1827. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1828. }
  1829. if (unlikely(pkt->vlan_ex)) {
  1830. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1831. rxq->stats.vlan_ex++;
  1832. }
  1833. skb_mark_napi_id(skb, &q->napi);
  1834. netif_receive_skb(skb);
  1835. return 0;
  1836. }
  1837. /**
  1838. * restore_rx_bufs - put back a packet's Rx buffers
  1839. * @si: the packet gather list
  1840. * @q: the SGE free list
  1841. * @frags: number of FL buffers to restore
  1842. *
  1843. * Puts back on an FL the Rx buffers associated with @si. The buffers
  1844. * have already been unmapped and are left unmapped, we mark them so to
  1845. * prevent further unmapping attempts.
  1846. *
  1847. * This function undoes a series of @unmap_rx_buf calls when we find out
  1848. * that the current packet can't be processed right away afterall and we
  1849. * need to come back to it later. This is a very rare event and there's
  1850. * no effort to make this particularly efficient.
  1851. */
  1852. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  1853. int frags)
  1854. {
  1855. struct rx_sw_desc *d;
  1856. while (frags--) {
  1857. if (q->cidx == 0)
  1858. q->cidx = q->size - 1;
  1859. else
  1860. q->cidx--;
  1861. d = &q->sdesc[q->cidx];
  1862. d->page = si->frags[frags].page;
  1863. d->dma_addr |= RX_UNMAPPED_BUF;
  1864. q->avail++;
  1865. }
  1866. }
  1867. /**
  1868. * is_new_response - check if a response is newly written
  1869. * @r: the response descriptor
  1870. * @q: the response queue
  1871. *
  1872. * Returns true if a response descriptor contains a yet unprocessed
  1873. * response.
  1874. */
  1875. static inline bool is_new_response(const struct rsp_ctrl *r,
  1876. const struct sge_rspq *q)
  1877. {
  1878. return (r->type_gen >> RSPD_GEN_S) == q->gen;
  1879. }
  1880. /**
  1881. * rspq_next - advance to the next entry in a response queue
  1882. * @q: the queue
  1883. *
  1884. * Updates the state of a response queue to advance it to the next entry.
  1885. */
  1886. static inline void rspq_next(struct sge_rspq *q)
  1887. {
  1888. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  1889. if (unlikely(++q->cidx == q->size)) {
  1890. q->cidx = 0;
  1891. q->gen ^= 1;
  1892. q->cur_desc = q->desc;
  1893. }
  1894. }
  1895. /**
  1896. * process_responses - process responses from an SGE response queue
  1897. * @q: the ingress queue to process
  1898. * @budget: how many responses can be processed in this round
  1899. *
  1900. * Process responses from an SGE response queue up to the supplied budget.
  1901. * Responses include received packets as well as control messages from FW
  1902. * or HW.
  1903. *
  1904. * Additionally choose the interrupt holdoff time for the next interrupt
  1905. * on this queue. If the system is under memory shortage use a fairly
  1906. * long delay to help recovery.
  1907. */
  1908. static int process_responses(struct sge_rspq *q, int budget)
  1909. {
  1910. int ret, rsp_type;
  1911. int budget_left = budget;
  1912. const struct rsp_ctrl *rc;
  1913. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1914. struct adapter *adapter = q->adap;
  1915. struct sge *s = &adapter->sge;
  1916. while (likely(budget_left)) {
  1917. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1918. if (!is_new_response(rc, q)) {
  1919. if (q->flush_handler)
  1920. q->flush_handler(q);
  1921. break;
  1922. }
  1923. dma_rmb();
  1924. rsp_type = RSPD_TYPE_G(rc->type_gen);
  1925. if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
  1926. struct page_frag *fp;
  1927. struct pkt_gl si;
  1928. const struct rx_sw_desc *rsd;
  1929. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  1930. if (len & RSPD_NEWBUF_F) {
  1931. if (likely(q->offset > 0)) {
  1932. free_rx_bufs(q->adap, &rxq->fl, 1);
  1933. q->offset = 0;
  1934. }
  1935. len = RSPD_LEN_G(len);
  1936. }
  1937. si.tot_len = len;
  1938. /* gather packet fragments */
  1939. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  1940. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  1941. bufsz = get_buf_size(adapter, rsd);
  1942. fp->page = rsd->page;
  1943. fp->offset = q->offset;
  1944. fp->size = min(bufsz, len);
  1945. len -= fp->size;
  1946. if (!len)
  1947. break;
  1948. unmap_rx_buf(q->adap, &rxq->fl);
  1949. }
  1950. si.sgetstamp = SGE_TIMESTAMP_G(
  1951. be64_to_cpu(rc->last_flit));
  1952. /*
  1953. * Last buffer remains mapped so explicitly make it
  1954. * coherent for CPU access.
  1955. */
  1956. dma_sync_single_for_cpu(q->adap->pdev_dev,
  1957. get_buf_addr(rsd),
  1958. fp->size, DMA_FROM_DEVICE);
  1959. si.va = page_address(si.frags[0].page) +
  1960. si.frags[0].offset;
  1961. prefetch(si.va);
  1962. si.nfrags = frags + 1;
  1963. ret = q->handler(q, q->cur_desc, &si);
  1964. if (likely(ret == 0))
  1965. q->offset += ALIGN(fp->size, s->fl_align);
  1966. else
  1967. restore_rx_bufs(&si, &rxq->fl, frags);
  1968. } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
  1969. ret = q->handler(q, q->cur_desc, NULL);
  1970. } else {
  1971. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  1972. }
  1973. if (unlikely(ret)) {
  1974. /* couldn't process descriptor, back off for recovery */
  1975. q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
  1976. break;
  1977. }
  1978. rspq_next(q);
  1979. budget_left--;
  1980. }
  1981. if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
  1982. __refill_fl(q->adap, &rxq->fl);
  1983. return budget - budget_left;
  1984. }
  1985. #ifdef CONFIG_NET_RX_BUSY_POLL
  1986. int cxgb_busy_poll(struct napi_struct *napi)
  1987. {
  1988. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  1989. unsigned int params, work_done;
  1990. u32 val;
  1991. if (!cxgb_poll_lock_poll(q))
  1992. return LL_FLUSH_BUSY;
  1993. work_done = process_responses(q, 4);
  1994. params = QINTR_TIMER_IDX_V(TIMERREG_COUNTER0_X) | QINTR_CNT_EN_V(1);
  1995. q->next_intr_params = params;
  1996. val = CIDXINC_V(work_done) | SEINTARM_V(params);
  1997. /* If we don't have access to the new User GTS (T5+), use the old
  1998. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  1999. */
  2000. if (unlikely(!q->bar2_addr))
  2001. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
  2002. val | INGRESSQID_V((u32)q->cntxt_id));
  2003. else {
  2004. writel(val | INGRESSQID_V(q->bar2_qid),
  2005. q->bar2_addr + SGE_UDB_GTS);
  2006. wmb();
  2007. }
  2008. cxgb_poll_unlock_poll(q);
  2009. return work_done;
  2010. }
  2011. #endif /* CONFIG_NET_RX_BUSY_POLL */
  2012. /**
  2013. * napi_rx_handler - the NAPI handler for Rx processing
  2014. * @napi: the napi instance
  2015. * @budget: how many packets we can process in this round
  2016. *
  2017. * Handler for new data events when using NAPI. This does not need any
  2018. * locking or protection from interrupts as data interrupts are off at
  2019. * this point and other adapter interrupts do not interfere (the latter
  2020. * in not a concern at all with MSI-X as non-data interrupts then have
  2021. * a separate handler).
  2022. */
  2023. static int napi_rx_handler(struct napi_struct *napi, int budget)
  2024. {
  2025. unsigned int params;
  2026. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  2027. int work_done;
  2028. u32 val;
  2029. if (!cxgb_poll_lock_napi(q))
  2030. return budget;
  2031. work_done = process_responses(q, budget);
  2032. if (likely(work_done < budget)) {
  2033. int timer_index;
  2034. napi_complete_done(napi, work_done);
  2035. timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
  2036. if (q->adaptive_rx) {
  2037. if (work_done > max(timer_pkt_quota[timer_index],
  2038. MIN_NAPI_WORK))
  2039. timer_index = (timer_index + 1);
  2040. else
  2041. timer_index = timer_index - 1;
  2042. timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
  2043. q->next_intr_params =
  2044. QINTR_TIMER_IDX_V(timer_index) |
  2045. QINTR_CNT_EN_V(0);
  2046. params = q->next_intr_params;
  2047. } else {
  2048. params = q->next_intr_params;
  2049. q->next_intr_params = q->intr_params;
  2050. }
  2051. } else
  2052. params = QINTR_TIMER_IDX_V(7);
  2053. val = CIDXINC_V(work_done) | SEINTARM_V(params);
  2054. /* If we don't have access to the new User GTS (T5+), use the old
  2055. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2056. */
  2057. if (unlikely(q->bar2_addr == NULL)) {
  2058. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
  2059. val | INGRESSQID_V((u32)q->cntxt_id));
  2060. } else {
  2061. writel(val | INGRESSQID_V(q->bar2_qid),
  2062. q->bar2_addr + SGE_UDB_GTS);
  2063. wmb();
  2064. }
  2065. cxgb_poll_unlock_napi(q);
  2066. return work_done;
  2067. }
  2068. /*
  2069. * The MSI-X interrupt handler for an SGE response queue.
  2070. */
  2071. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  2072. {
  2073. struct sge_rspq *q = cookie;
  2074. napi_schedule(&q->napi);
  2075. return IRQ_HANDLED;
  2076. }
  2077. /*
  2078. * Process the indirect interrupt entries in the interrupt queue and kick off
  2079. * NAPI for each queue that has generated an entry.
  2080. */
  2081. static unsigned int process_intrq(struct adapter *adap)
  2082. {
  2083. unsigned int credits;
  2084. const struct rsp_ctrl *rc;
  2085. struct sge_rspq *q = &adap->sge.intrq;
  2086. u32 val;
  2087. spin_lock(&adap->sge.intrq_lock);
  2088. for (credits = 0; ; credits++) {
  2089. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  2090. if (!is_new_response(rc, q))
  2091. break;
  2092. dma_rmb();
  2093. if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
  2094. unsigned int qid = ntohl(rc->pldbuflen_qid);
  2095. qid -= adap->sge.ingr_start;
  2096. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  2097. }
  2098. rspq_next(q);
  2099. }
  2100. val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
  2101. /* If we don't have access to the new User GTS (T5+), use the old
  2102. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2103. */
  2104. if (unlikely(q->bar2_addr == NULL)) {
  2105. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
  2106. val | INGRESSQID_V(q->cntxt_id));
  2107. } else {
  2108. writel(val | INGRESSQID_V(q->bar2_qid),
  2109. q->bar2_addr + SGE_UDB_GTS);
  2110. wmb();
  2111. }
  2112. spin_unlock(&adap->sge.intrq_lock);
  2113. return credits;
  2114. }
  2115. /*
  2116. * The MSI interrupt handler, which handles data events from SGE response queues
  2117. * as well as error and other async events as they all use the same MSI vector.
  2118. */
  2119. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  2120. {
  2121. struct adapter *adap = cookie;
  2122. if (adap->flags & MASTER_PF)
  2123. t4_slow_intr_handler(adap);
  2124. process_intrq(adap);
  2125. return IRQ_HANDLED;
  2126. }
  2127. /*
  2128. * Interrupt handler for legacy INTx interrupts.
  2129. * Handles data events from SGE response queues as well as error and other
  2130. * async events as they all use the same interrupt line.
  2131. */
  2132. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  2133. {
  2134. struct adapter *adap = cookie;
  2135. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
  2136. if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
  2137. process_intrq(adap))
  2138. return IRQ_HANDLED;
  2139. return IRQ_NONE; /* probably shared interrupt */
  2140. }
  2141. /**
  2142. * t4_intr_handler - select the top-level interrupt handler
  2143. * @adap: the adapter
  2144. *
  2145. * Selects the top-level interrupt handler based on the type of interrupts
  2146. * (MSI-X, MSI, or INTx).
  2147. */
  2148. irq_handler_t t4_intr_handler(struct adapter *adap)
  2149. {
  2150. if (adap->flags & USING_MSIX)
  2151. return t4_sge_intr_msix;
  2152. if (adap->flags & USING_MSI)
  2153. return t4_intr_msi;
  2154. return t4_intr_intx;
  2155. }
  2156. static void sge_rx_timer_cb(unsigned long data)
  2157. {
  2158. unsigned long m;
  2159. unsigned int i;
  2160. struct adapter *adap = (struct adapter *)data;
  2161. struct sge *s = &adap->sge;
  2162. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2163. for (m = s->starving_fl[i]; m; m &= m - 1) {
  2164. struct sge_eth_rxq *rxq;
  2165. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  2166. struct sge_fl *fl = s->egr_map[id];
  2167. clear_bit(id, s->starving_fl);
  2168. smp_mb__after_atomic();
  2169. if (fl_starving(adap, fl)) {
  2170. rxq = container_of(fl, struct sge_eth_rxq, fl);
  2171. if (napi_reschedule(&rxq->rspq.napi))
  2172. fl->starving++;
  2173. else
  2174. set_bit(id, s->starving_fl);
  2175. }
  2176. }
  2177. /* The remainder of the SGE RX Timer Callback routine is dedicated to
  2178. * global Master PF activities like checking for chip ingress stalls,
  2179. * etc.
  2180. */
  2181. if (!(adap->flags & MASTER_PF))
  2182. goto done;
  2183. t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
  2184. done:
  2185. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  2186. }
  2187. static void sge_tx_timer_cb(unsigned long data)
  2188. {
  2189. unsigned long m;
  2190. unsigned int i, budget;
  2191. struct adapter *adap = (struct adapter *)data;
  2192. struct sge *s = &adap->sge;
  2193. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2194. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  2195. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  2196. struct sge_ofld_txq *txq = s->egr_map[id];
  2197. clear_bit(id, s->txq_maperr);
  2198. tasklet_schedule(&txq->qresume_tsk);
  2199. }
  2200. budget = MAX_TIMER_TX_RECLAIM;
  2201. i = s->ethtxq_rover;
  2202. do {
  2203. struct sge_eth_txq *q = &s->ethtxq[i];
  2204. if (q->q.in_use &&
  2205. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  2206. __netif_tx_trylock(q->txq)) {
  2207. int avail = reclaimable(&q->q);
  2208. if (avail) {
  2209. if (avail > budget)
  2210. avail = budget;
  2211. free_tx_desc(adap, &q->q, avail, true);
  2212. q->q.in_use -= avail;
  2213. budget -= avail;
  2214. }
  2215. __netif_tx_unlock(q->txq);
  2216. }
  2217. if (++i >= s->ethqsets)
  2218. i = 0;
  2219. } while (budget && i != s->ethtxq_rover);
  2220. s->ethtxq_rover = i;
  2221. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  2222. }
  2223. /**
  2224. * bar2_address - return the BAR2 address for an SGE Queue's Registers
  2225. * @adapter: the adapter
  2226. * @qid: the SGE Queue ID
  2227. * @qtype: the SGE Queue Type (Egress or Ingress)
  2228. * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
  2229. *
  2230. * Returns the BAR2 address for the SGE Queue Registers associated with
  2231. * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
  2232. * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
  2233. * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
  2234. * Registers are supported (e.g. the Write Combining Doorbell Buffer).
  2235. */
  2236. static void __iomem *bar2_address(struct adapter *adapter,
  2237. unsigned int qid,
  2238. enum t4_bar2_qtype qtype,
  2239. unsigned int *pbar2_qid)
  2240. {
  2241. u64 bar2_qoffset;
  2242. int ret;
  2243. ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
  2244. &bar2_qoffset, pbar2_qid);
  2245. if (ret)
  2246. return NULL;
  2247. return adapter->bar2 + bar2_qoffset;
  2248. }
  2249. /* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
  2250. * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
  2251. */
  2252. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  2253. struct net_device *dev, int intr_idx,
  2254. struct sge_fl *fl, rspq_handler_t hnd,
  2255. rspq_flush_handler_t flush_hnd, int cong)
  2256. {
  2257. int ret, flsz = 0;
  2258. struct fw_iq_cmd c;
  2259. struct sge *s = &adap->sge;
  2260. struct port_info *pi = netdev_priv(dev);
  2261. /* Size needs to be multiple of 16, including status entry. */
  2262. iq->size = roundup(iq->size, 16);
  2263. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  2264. &iq->phys_addr, NULL, 0,
  2265. dev_to_node(adap->pdev_dev));
  2266. if (!iq->desc)
  2267. return -ENOMEM;
  2268. memset(&c, 0, sizeof(c));
  2269. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
  2270. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2271. FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
  2272. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
  2273. FW_LEN16(c));
  2274. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
  2275. FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
  2276. FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
  2277. FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
  2278. FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
  2279. -intr_idx - 1));
  2280. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
  2281. FW_IQ_CMD_IQGTSMODE_F |
  2282. FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
  2283. FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
  2284. c.iqsize = htons(iq->size);
  2285. c.iqaddr = cpu_to_be64(iq->phys_addr);
  2286. if (cong >= 0)
  2287. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F);
  2288. if (fl) {
  2289. enum chip_type chip = CHELSIO_CHIP_VERSION(adap->params.chip);
  2290. /* Allocate the ring for the hardware free list (with space
  2291. * for its status page) along with the associated software
  2292. * descriptor ring. The free list size needs to be a multiple
  2293. * of the Egress Queue Unit and at least 2 Egress Units larger
  2294. * than the SGE's Egress Congrestion Threshold
  2295. * (fl_starve_thres - 1).
  2296. */
  2297. if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
  2298. fl->size = s->fl_starve_thres - 1 + 2 * 8;
  2299. fl->size = roundup(fl->size, 8);
  2300. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  2301. sizeof(struct rx_sw_desc), &fl->addr,
  2302. &fl->sdesc, s->stat_len,
  2303. dev_to_node(adap->pdev_dev));
  2304. if (!fl->desc)
  2305. goto fl_nomem;
  2306. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  2307. c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
  2308. FW_IQ_CMD_FL0FETCHRO_F |
  2309. FW_IQ_CMD_FL0DATARO_F |
  2310. FW_IQ_CMD_FL0PADEN_F);
  2311. if (cong >= 0)
  2312. c.iqns_to_fl0congen |=
  2313. htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
  2314. FW_IQ_CMD_FL0CONGCIF_F |
  2315. FW_IQ_CMD_FL0CONGEN_F);
  2316. /* In T6, for egress queue type FL there is internal overhead
  2317. * of 16B for header going into FLM module. Hence the maximum
  2318. * allowed burst size is 448 bytes. For T4/T5, the hardware
  2319. * doesn't coalesce fetch requests if more than 64 bytes of
  2320. * Free List pointers are provided, so we use a 128-byte Fetch
  2321. * Burst Minimum there (T6 implements coalescing so we can use
  2322. * the smaller 64-byte value there).
  2323. */
  2324. c.fl0dcaen_to_fl0cidxfthresh =
  2325. htons(FW_IQ_CMD_FL0FBMIN_V(chip <= CHELSIO_T5 ?
  2326. FETCHBURSTMIN_128B_X :
  2327. FETCHBURSTMIN_64B_X) |
  2328. FW_IQ_CMD_FL0FBMAX_V((chip <= CHELSIO_T5) ?
  2329. FETCHBURSTMAX_512B_X :
  2330. FETCHBURSTMAX_256B_X));
  2331. c.fl0size = htons(flsz);
  2332. c.fl0addr = cpu_to_be64(fl->addr);
  2333. }
  2334. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2335. if (ret)
  2336. goto err;
  2337. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  2338. iq->cur_desc = iq->desc;
  2339. iq->cidx = 0;
  2340. iq->gen = 1;
  2341. iq->next_intr_params = iq->intr_params;
  2342. iq->cntxt_id = ntohs(c.iqid);
  2343. iq->abs_id = ntohs(c.physiqid);
  2344. iq->bar2_addr = bar2_address(adap,
  2345. iq->cntxt_id,
  2346. T4_BAR2_QTYPE_INGRESS,
  2347. &iq->bar2_qid);
  2348. iq->size--; /* subtract status entry */
  2349. iq->netdev = dev;
  2350. iq->handler = hnd;
  2351. iq->flush_handler = flush_hnd;
  2352. memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
  2353. skb_queue_head_init(&iq->lro_mgr.lroq);
  2354. /* set offset to -1 to distinguish ingress queues without FL */
  2355. iq->offset = fl ? 0 : -1;
  2356. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  2357. if (fl) {
  2358. fl->cntxt_id = ntohs(c.fl0id);
  2359. fl->avail = fl->pend_cred = 0;
  2360. fl->pidx = fl->cidx = 0;
  2361. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  2362. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  2363. /* Note, we must initialize the BAR2 Free List User Doorbell
  2364. * information before refilling the Free List!
  2365. */
  2366. fl->bar2_addr = bar2_address(adap,
  2367. fl->cntxt_id,
  2368. T4_BAR2_QTYPE_EGRESS,
  2369. &fl->bar2_qid);
  2370. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  2371. }
  2372. /* For T5 and later we attempt to set up the Congestion Manager values
  2373. * of the new RX Ethernet Queue. This should really be handled by
  2374. * firmware because it's more complex than any host driver wants to
  2375. * get involved with and it's different per chip and this is almost
  2376. * certainly wrong. Firmware would be wrong as well, but it would be
  2377. * a lot easier to fix in one place ... For now we do something very
  2378. * simple (and hopefully less wrong).
  2379. */
  2380. if (!is_t4(adap->params.chip) && cong >= 0) {
  2381. u32 param, val, ch_map = 0;
  2382. int i;
  2383. u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
  2384. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  2385. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
  2386. FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
  2387. if (cong == 0) {
  2388. val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
  2389. } else {
  2390. val =
  2391. CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
  2392. for (i = 0; i < 4; i++) {
  2393. if (cong & (1 << i))
  2394. ch_map |= 1 << (i << cng_ch_bits_log);
  2395. }
  2396. val |= CONMCTXT_CNGCHMAP_V(ch_map);
  2397. }
  2398. ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
  2399. &param, &val);
  2400. if (ret)
  2401. dev_warn(adap->pdev_dev, "Failed to set Congestion"
  2402. " Manager Context for Ingress Queue %d: %d\n",
  2403. iq->cntxt_id, -ret);
  2404. }
  2405. return 0;
  2406. fl_nomem:
  2407. ret = -ENOMEM;
  2408. err:
  2409. if (iq->desc) {
  2410. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  2411. iq->desc, iq->phys_addr);
  2412. iq->desc = NULL;
  2413. }
  2414. if (fl && fl->desc) {
  2415. kfree(fl->sdesc);
  2416. fl->sdesc = NULL;
  2417. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  2418. fl->desc, fl->addr);
  2419. fl->desc = NULL;
  2420. }
  2421. return ret;
  2422. }
  2423. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  2424. {
  2425. q->cntxt_id = id;
  2426. q->bar2_addr = bar2_address(adap,
  2427. q->cntxt_id,
  2428. T4_BAR2_QTYPE_EGRESS,
  2429. &q->bar2_qid);
  2430. q->in_use = 0;
  2431. q->cidx = q->pidx = 0;
  2432. q->stops = q->restarts = 0;
  2433. q->stat = (void *)&q->desc[q->size];
  2434. spin_lock_init(&q->db_lock);
  2435. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  2436. }
  2437. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  2438. struct net_device *dev, struct netdev_queue *netdevq,
  2439. unsigned int iqid)
  2440. {
  2441. int ret, nentries;
  2442. struct fw_eq_eth_cmd c;
  2443. struct sge *s = &adap->sge;
  2444. struct port_info *pi = netdev_priv(dev);
  2445. /* Add status entries */
  2446. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2447. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2448. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2449. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2450. netdev_queue_numa_node_read(netdevq));
  2451. if (!txq->q.desc)
  2452. return -ENOMEM;
  2453. memset(&c, 0, sizeof(c));
  2454. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
  2455. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2456. FW_EQ_ETH_CMD_PFN_V(adap->pf) |
  2457. FW_EQ_ETH_CMD_VFN_V(0));
  2458. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
  2459. FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
  2460. c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
  2461. FW_EQ_ETH_CMD_VIID_V(pi->viid));
  2462. c.fetchszm_to_iqid =
  2463. htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2464. FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
  2465. FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
  2466. c.dcaen_to_eqsize =
  2467. htonl(FW_EQ_ETH_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2468. FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2469. FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2470. FW_EQ_ETH_CMD_EQSIZE_V(nentries));
  2471. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2472. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2473. if (ret) {
  2474. kfree(txq->q.sdesc);
  2475. txq->q.sdesc = NULL;
  2476. dma_free_coherent(adap->pdev_dev,
  2477. nentries * sizeof(struct tx_desc),
  2478. txq->q.desc, txq->q.phys_addr);
  2479. txq->q.desc = NULL;
  2480. return ret;
  2481. }
  2482. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
  2483. txq->txq = netdevq;
  2484. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  2485. txq->mapping_err = 0;
  2486. return 0;
  2487. }
  2488. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  2489. struct net_device *dev, unsigned int iqid,
  2490. unsigned int cmplqid)
  2491. {
  2492. int ret, nentries;
  2493. struct fw_eq_ctrl_cmd c;
  2494. struct sge *s = &adap->sge;
  2495. struct port_info *pi = netdev_priv(dev);
  2496. /* Add status entries */
  2497. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2498. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  2499. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  2500. NULL, 0, dev_to_node(adap->pdev_dev));
  2501. if (!txq->q.desc)
  2502. return -ENOMEM;
  2503. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
  2504. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2505. FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
  2506. FW_EQ_CTRL_CMD_VFN_V(0));
  2507. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
  2508. FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
  2509. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
  2510. c.physeqid_pkd = htonl(0);
  2511. c.fetchszm_to_iqid =
  2512. htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2513. FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
  2514. FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
  2515. c.dcaen_to_eqsize =
  2516. htonl(FW_EQ_CTRL_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2517. FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2518. FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2519. FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
  2520. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2521. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2522. if (ret) {
  2523. dma_free_coherent(adap->pdev_dev,
  2524. nentries * sizeof(struct tx_desc),
  2525. txq->q.desc, txq->q.phys_addr);
  2526. txq->q.desc = NULL;
  2527. return ret;
  2528. }
  2529. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
  2530. txq->adap = adap;
  2531. skb_queue_head_init(&txq->sendq);
  2532. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  2533. txq->full = 0;
  2534. return 0;
  2535. }
  2536. int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
  2537. unsigned int cmplqid)
  2538. {
  2539. u32 param, val;
  2540. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  2541. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
  2542. FW_PARAMS_PARAM_YZ_V(eqid));
  2543. val = cmplqid;
  2544. return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
  2545. }
  2546. int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
  2547. struct net_device *dev, unsigned int iqid)
  2548. {
  2549. int ret, nentries;
  2550. struct fw_eq_ofld_cmd c;
  2551. struct sge *s = &adap->sge;
  2552. struct port_info *pi = netdev_priv(dev);
  2553. /* Add status entries */
  2554. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2555. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2556. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2557. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2558. NUMA_NO_NODE);
  2559. if (!txq->q.desc)
  2560. return -ENOMEM;
  2561. memset(&c, 0, sizeof(c));
  2562. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST_F |
  2563. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2564. FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
  2565. FW_EQ_OFLD_CMD_VFN_V(0));
  2566. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
  2567. FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
  2568. c.fetchszm_to_iqid =
  2569. htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2570. FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
  2571. FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
  2572. c.dcaen_to_eqsize =
  2573. htonl(FW_EQ_OFLD_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2574. FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2575. FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2576. FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
  2577. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2578. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2579. if (ret) {
  2580. kfree(txq->q.sdesc);
  2581. txq->q.sdesc = NULL;
  2582. dma_free_coherent(adap->pdev_dev,
  2583. nentries * sizeof(struct tx_desc),
  2584. txq->q.desc, txq->q.phys_addr);
  2585. txq->q.desc = NULL;
  2586. return ret;
  2587. }
  2588. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
  2589. txq->adap = adap;
  2590. skb_queue_head_init(&txq->sendq);
  2591. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  2592. txq->full = 0;
  2593. txq->mapping_err = 0;
  2594. return 0;
  2595. }
  2596. static void free_txq(struct adapter *adap, struct sge_txq *q)
  2597. {
  2598. struct sge *s = &adap->sge;
  2599. dma_free_coherent(adap->pdev_dev,
  2600. q->size * sizeof(struct tx_desc) + s->stat_len,
  2601. q->desc, q->phys_addr);
  2602. q->cntxt_id = 0;
  2603. q->sdesc = NULL;
  2604. q->desc = NULL;
  2605. }
  2606. void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  2607. struct sge_fl *fl)
  2608. {
  2609. struct sge *s = &adap->sge;
  2610. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  2611. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  2612. t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
  2613. rq->cntxt_id, fl_id, 0xffff);
  2614. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  2615. rq->desc, rq->phys_addr);
  2616. netif_napi_del(&rq->napi);
  2617. rq->netdev = NULL;
  2618. rq->cntxt_id = rq->abs_id = 0;
  2619. rq->desc = NULL;
  2620. if (fl) {
  2621. free_rx_bufs(adap, fl, fl->avail);
  2622. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  2623. fl->desc, fl->addr);
  2624. kfree(fl->sdesc);
  2625. fl->sdesc = NULL;
  2626. fl->cntxt_id = 0;
  2627. fl->desc = NULL;
  2628. }
  2629. }
  2630. /**
  2631. * t4_free_ofld_rxqs - free a block of consecutive Rx queues
  2632. * @adap: the adapter
  2633. * @n: number of queues
  2634. * @q: pointer to first queue
  2635. *
  2636. * Release the resources of a consecutive block of offload Rx queues.
  2637. */
  2638. void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
  2639. {
  2640. for ( ; n; n--, q++)
  2641. if (q->rspq.desc)
  2642. free_rspq_fl(adap, &q->rspq,
  2643. q->fl.size ? &q->fl : NULL);
  2644. }
  2645. /**
  2646. * t4_free_sge_resources - free SGE resources
  2647. * @adap: the adapter
  2648. *
  2649. * Frees resources used by the SGE queue sets.
  2650. */
  2651. void t4_free_sge_resources(struct adapter *adap)
  2652. {
  2653. int i;
  2654. struct sge_eth_rxq *eq;
  2655. struct sge_eth_txq *etq;
  2656. /* stop all Rx queues in order to start them draining */
  2657. for (i = 0; i < adap->sge.ethqsets; i++) {
  2658. eq = &adap->sge.ethrxq[i];
  2659. if (eq->rspq.desc)
  2660. t4_iq_stop(adap, adap->mbox, adap->pf, 0,
  2661. FW_IQ_TYPE_FL_INT_CAP,
  2662. eq->rspq.cntxt_id,
  2663. eq->fl.size ? eq->fl.cntxt_id : 0xffff,
  2664. 0xffff);
  2665. }
  2666. /* clean up Ethernet Tx/Rx queues */
  2667. for (i = 0; i < adap->sge.ethqsets; i++) {
  2668. eq = &adap->sge.ethrxq[i];
  2669. if (eq->rspq.desc)
  2670. free_rspq_fl(adap, &eq->rspq,
  2671. eq->fl.size ? &eq->fl : NULL);
  2672. etq = &adap->sge.ethtxq[i];
  2673. if (etq->q.desc) {
  2674. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  2675. etq->q.cntxt_id);
  2676. __netif_tx_lock_bh(etq->txq);
  2677. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  2678. __netif_tx_unlock_bh(etq->txq);
  2679. kfree(etq->q.sdesc);
  2680. free_txq(adap, &etq->q);
  2681. }
  2682. }
  2683. /* clean up offload Tx queues */
  2684. for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
  2685. struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
  2686. if (q->q.desc) {
  2687. tasklet_kill(&q->qresume_tsk);
  2688. t4_ofld_eq_free(adap, adap->mbox, adap->pf, 0,
  2689. q->q.cntxt_id);
  2690. free_tx_desc(adap, &q->q, q->q.in_use, false);
  2691. kfree(q->q.sdesc);
  2692. __skb_queue_purge(&q->sendq);
  2693. free_txq(adap, &q->q);
  2694. }
  2695. }
  2696. /* clean up control Tx queues */
  2697. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  2698. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  2699. if (cq->q.desc) {
  2700. tasklet_kill(&cq->qresume_tsk);
  2701. t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
  2702. cq->q.cntxt_id);
  2703. __skb_queue_purge(&cq->sendq);
  2704. free_txq(adap, &cq->q);
  2705. }
  2706. }
  2707. if (adap->sge.fw_evtq.desc)
  2708. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  2709. if (adap->sge.intrq.desc)
  2710. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  2711. /* clear the reverse egress queue map */
  2712. memset(adap->sge.egr_map, 0,
  2713. adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
  2714. }
  2715. void t4_sge_start(struct adapter *adap)
  2716. {
  2717. adap->sge.ethtxq_rover = 0;
  2718. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2719. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2720. }
  2721. /**
  2722. * t4_sge_stop - disable SGE operation
  2723. * @adap: the adapter
  2724. *
  2725. * Stop tasklets and timers associated with the DMA engine. Note that
  2726. * this is effective only if measures have been taken to disable any HW
  2727. * events that may restart them.
  2728. */
  2729. void t4_sge_stop(struct adapter *adap)
  2730. {
  2731. int i;
  2732. struct sge *s = &adap->sge;
  2733. if (in_interrupt()) /* actions below require waiting */
  2734. return;
  2735. if (s->rx_timer.function)
  2736. del_timer_sync(&s->rx_timer);
  2737. if (s->tx_timer.function)
  2738. del_timer_sync(&s->tx_timer);
  2739. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
  2740. struct sge_ofld_txq *q = &s->ofldtxq[i];
  2741. if (q->q.desc)
  2742. tasklet_kill(&q->qresume_tsk);
  2743. }
  2744. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  2745. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  2746. if (cq->q.desc)
  2747. tasklet_kill(&cq->qresume_tsk);
  2748. }
  2749. }
  2750. /**
  2751. * t4_sge_init_soft - grab core SGE values needed by SGE code
  2752. * @adap: the adapter
  2753. *
  2754. * We need to grab the SGE operating parameters that we need to have
  2755. * in order to do our job and make sure we can live with them.
  2756. */
  2757. static int t4_sge_init_soft(struct adapter *adap)
  2758. {
  2759. struct sge *s = &adap->sge;
  2760. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  2761. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  2762. u32 ingress_rx_threshold;
  2763. /*
  2764. * Verify that CPL messages are going to the Ingress Queue for
  2765. * process_responses() and that only packet data is going to the
  2766. * Free Lists.
  2767. */
  2768. if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
  2769. RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
  2770. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  2771. return -EINVAL;
  2772. }
  2773. /*
  2774. * Validate the Host Buffer Register Array indices that we want to
  2775. * use ...
  2776. *
  2777. * XXX Note that we should really read through the Host Buffer Size
  2778. * XXX register array and find the indices of the Buffer Sizes which
  2779. * XXX meet our needs!
  2780. */
  2781. #define READ_FL_BUF(x) \
  2782. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
  2783. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  2784. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  2785. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  2786. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  2787. /* We only bother using the Large Page logic if the Large Page Buffer
  2788. * is larger than our Page Size Buffer.
  2789. */
  2790. if (fl_large_pg <= fl_small_pg)
  2791. fl_large_pg = 0;
  2792. #undef READ_FL_BUF
  2793. /* The Page Size Buffer must be exactly equal to our Page Size and the
  2794. * Large Page Size Buffer should be 0 (per above) or a power of 2.
  2795. */
  2796. if (fl_small_pg != PAGE_SIZE ||
  2797. (fl_large_pg & (fl_large_pg-1)) != 0) {
  2798. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  2799. fl_small_pg, fl_large_pg);
  2800. return -EINVAL;
  2801. }
  2802. if (fl_large_pg)
  2803. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  2804. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  2805. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  2806. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  2807. fl_small_mtu, fl_large_mtu);
  2808. return -EINVAL;
  2809. }
  2810. /*
  2811. * Retrieve our RX interrupt holdoff timer values and counter
  2812. * threshold values from the SGE parameters.
  2813. */
  2814. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
  2815. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
  2816. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
  2817. s->timer_val[0] = core_ticks_to_us(adap,
  2818. TIMERVALUE0_G(timer_value_0_and_1));
  2819. s->timer_val[1] = core_ticks_to_us(adap,
  2820. TIMERVALUE1_G(timer_value_0_and_1));
  2821. s->timer_val[2] = core_ticks_to_us(adap,
  2822. TIMERVALUE2_G(timer_value_2_and_3));
  2823. s->timer_val[3] = core_ticks_to_us(adap,
  2824. TIMERVALUE3_G(timer_value_2_and_3));
  2825. s->timer_val[4] = core_ticks_to_us(adap,
  2826. TIMERVALUE4_G(timer_value_4_and_5));
  2827. s->timer_val[5] = core_ticks_to_us(adap,
  2828. TIMERVALUE5_G(timer_value_4_and_5));
  2829. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
  2830. s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
  2831. s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
  2832. s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
  2833. s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
  2834. return 0;
  2835. }
  2836. /**
  2837. * t4_sge_init - initialize SGE
  2838. * @adap: the adapter
  2839. *
  2840. * Perform low-level SGE code initialization needed every time after a
  2841. * chip reset.
  2842. */
  2843. int t4_sge_init(struct adapter *adap)
  2844. {
  2845. struct sge *s = &adap->sge;
  2846. u32 sge_control, sge_conm_ctrl;
  2847. int ret, egress_threshold;
  2848. /*
  2849. * Ingress Padding Boundary and Egress Status Page Size are set up by
  2850. * t4_fixup_host_params().
  2851. */
  2852. sge_control = t4_read_reg(adap, SGE_CONTROL_A);
  2853. s->pktshift = PKTSHIFT_G(sge_control);
  2854. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
  2855. s->fl_align = t4_fl_pkt_align(adap);
  2856. ret = t4_sge_init_soft(adap);
  2857. if (ret < 0)
  2858. return ret;
  2859. /*
  2860. * A FL with <= fl_starve_thres buffers is starving and a periodic
  2861. * timer will attempt to refill it. This needs to be larger than the
  2862. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  2863. * stuck waiting for new packets while the SGE is waiting for us to
  2864. * give it more Free List entries. (Note that the SGE's Egress
  2865. * Congestion Threshold is in units of 2 Free List pointers.) For T4,
  2866. * there was only a single field to control this. For T5 there's the
  2867. * original field which now only applies to Unpacked Mode Free List
  2868. * buffers and a new field which only applies to Packed Mode Free List
  2869. * buffers.
  2870. */
  2871. sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
  2872. switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
  2873. case CHELSIO_T4:
  2874. egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
  2875. break;
  2876. case CHELSIO_T5:
  2877. egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  2878. break;
  2879. case CHELSIO_T6:
  2880. egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  2881. break;
  2882. default:
  2883. dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
  2884. CHELSIO_CHIP_VERSION(adap->params.chip));
  2885. return -EINVAL;
  2886. }
  2887. s->fl_starve_thres = 2*egress_threshold + 1;
  2888. t4_idma_monitor_init(adap, &s->idma_monitor);
  2889. /* Set up timers used for recuring callbacks to process RX and TX
  2890. * administrative tasks.
  2891. */
  2892. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
  2893. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
  2894. spin_lock_init(&s->intrq_lock);
  2895. return 0;
  2896. }