sge.c 89 KB

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