sge.c 93 KB

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