sge.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651
  1. /*
  2. * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
  3. * driver for Linux.
  4. *
  5. * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/skbuff.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/etherdevice.h>
  38. #include <linux/if_vlan.h>
  39. #include <linux/ip.h>
  40. #include <net/ipv6.h>
  41. #include <net/tcp.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/prefetch.h>
  44. #include "t4vf_common.h"
  45. #include "t4vf_defs.h"
  46. #include "../cxgb4/t4_regs.h"
  47. #include "../cxgb4/t4_values.h"
  48. #include "../cxgb4/t4fw_api.h"
  49. #include "../cxgb4/t4_msg.h"
  50. /*
  51. * Constants ...
  52. */
  53. enum {
  54. /*
  55. * Egress Queue sizes, producer and consumer indices are all in units
  56. * of Egress Context Units bytes. Note that as far as the hardware is
  57. * concerned, the free list is an Egress Queue (the host produces free
  58. * buffers which the hardware consumes) and free list entries are
  59. * 64-bit PCI DMA addresses.
  60. */
  61. EQ_UNIT = SGE_EQ_IDXSIZE,
  62. FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
  63. TXD_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
  64. /*
  65. * Max number of TX descriptors we clean up at a time. Should be
  66. * modest as freeing skbs isn't cheap and it happens while holding
  67. * locks. We just need to free packets faster than they arrive, we
  68. * eventually catch up and keep the amortized cost reasonable.
  69. */
  70. MAX_TX_RECLAIM = 16,
  71. /*
  72. * Max number of Rx buffers we replenish at a time. Again keep this
  73. * modest, allocating buffers isn't cheap either.
  74. */
  75. MAX_RX_REFILL = 16,
  76. /*
  77. * Period of the Rx queue check timer. This timer is infrequent as it
  78. * has something to do only when the system experiences severe memory
  79. * shortage.
  80. */
  81. RX_QCHECK_PERIOD = (HZ / 2),
  82. /*
  83. * Period of the TX queue check timer and the maximum number of TX
  84. * descriptors to be reclaimed by the TX timer.
  85. */
  86. TX_QCHECK_PERIOD = (HZ / 2),
  87. MAX_TIMER_TX_RECLAIM = 100,
  88. /*
  89. * Suspend an Ethernet TX queue with fewer available descriptors than
  90. * this. We always want to have room for a maximum sized packet:
  91. * inline immediate data + MAX_SKB_FRAGS. This is the same as
  92. * calc_tx_flits() for a TSO packet with nr_frags == MAX_SKB_FRAGS
  93. * (see that function and its helpers for a description of the
  94. * calculation).
  95. */
  96. ETHTXQ_MAX_FRAGS = MAX_SKB_FRAGS + 1,
  97. ETHTXQ_MAX_SGL_LEN = ((3 * (ETHTXQ_MAX_FRAGS-1))/2 +
  98. ((ETHTXQ_MAX_FRAGS-1) & 1) +
  99. 2),
  100. ETHTXQ_MAX_HDR = (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  101. sizeof(struct cpl_tx_pkt_lso_core) +
  102. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64),
  103. ETHTXQ_MAX_FLITS = ETHTXQ_MAX_SGL_LEN + ETHTXQ_MAX_HDR,
  104. ETHTXQ_STOP_THRES = 1 + DIV_ROUND_UP(ETHTXQ_MAX_FLITS, TXD_PER_EQ_UNIT),
  105. /*
  106. * Max TX descriptor space we allow for an Ethernet packet to be
  107. * inlined into a WR. This is limited by the maximum value which
  108. * we can specify for immediate data in the firmware Ethernet TX
  109. * Work Request.
  110. */
  111. MAX_IMM_TX_PKT_LEN = FW_WR_IMMDLEN_M,
  112. /*
  113. * Max size of a WR sent through a control TX queue.
  114. */
  115. MAX_CTRL_WR_LEN = 256,
  116. /*
  117. * Maximum amount of data which we'll ever need to inline into a
  118. * TX ring: max(MAX_IMM_TX_PKT_LEN, MAX_CTRL_WR_LEN).
  119. */
  120. MAX_IMM_TX_LEN = (MAX_IMM_TX_PKT_LEN > MAX_CTRL_WR_LEN
  121. ? MAX_IMM_TX_PKT_LEN
  122. : MAX_CTRL_WR_LEN),
  123. /*
  124. * For incoming packets less than RX_COPY_THRES, we copy the data into
  125. * an skb rather than referencing the data. We allocate enough
  126. * in-line room in skb's to accommodate pulling in RX_PULL_LEN bytes
  127. * of the data (header).
  128. */
  129. RX_COPY_THRES = 256,
  130. RX_PULL_LEN = 128,
  131. /*
  132. * Main body length for sk_buffs used for RX Ethernet packets with
  133. * fragments. Should be >= RX_PULL_LEN but possibly bigger to give
  134. * pskb_may_pull() some room.
  135. */
  136. RX_SKB_LEN = 512,
  137. };
  138. /*
  139. * Software state per TX descriptor.
  140. */
  141. struct tx_sw_desc {
  142. struct sk_buff *skb; /* socket buffer of TX data source */
  143. struct ulptx_sgl *sgl; /* scatter/gather list in TX Queue */
  144. };
  145. /*
  146. * Software state per RX Free List descriptor. We keep track of the allocated
  147. * FL page, its size, and its PCI DMA address (if the page is mapped). The FL
  148. * page size and its PCI DMA mapped state are stored in the low bits of the
  149. * PCI DMA address as per below.
  150. */
  151. struct rx_sw_desc {
  152. struct page *page; /* Free List page buffer */
  153. dma_addr_t dma_addr; /* PCI DMA address (if mapped) */
  154. /* and flags (see below) */
  155. };
  156. /*
  157. * The low bits of rx_sw_desc.dma_addr have special meaning. Note that the
  158. * SGE also uses the low 4 bits to determine the size of the buffer. It uses
  159. * those bits to index into the SGE_FL_BUFFER_SIZE[index] register array.
  160. * Since we only use SGE_FL_BUFFER_SIZE0 and SGE_FL_BUFFER_SIZE1, these low 4
  161. * bits can only contain a 0 or a 1 to indicate which size buffer we're giving
  162. * to the SGE. Thus, our software state of "is the buffer mapped for DMA" is
  163. * maintained in an inverse sense so the hardware never sees that bit high.
  164. */
  165. enum {
  166. RX_LARGE_BUF = 1 << 0, /* buffer is SGE_FL_BUFFER_SIZE[1] */
  167. RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */
  168. };
  169. /**
  170. * get_buf_addr - return DMA buffer address of software descriptor
  171. * @sdesc: pointer to the software buffer descriptor
  172. *
  173. * Return the DMA buffer address of a software descriptor (stripping out
  174. * our low-order flag bits).
  175. */
  176. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *sdesc)
  177. {
  178. return sdesc->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
  179. }
  180. /**
  181. * is_buf_mapped - is buffer mapped for DMA?
  182. * @sdesc: pointer to the software buffer descriptor
  183. *
  184. * Determine whether the buffer associated with a software descriptor in
  185. * mapped for DMA or not.
  186. */
  187. static inline bool is_buf_mapped(const struct rx_sw_desc *sdesc)
  188. {
  189. return !(sdesc->dma_addr & RX_UNMAPPED_BUF);
  190. }
  191. /**
  192. * need_skb_unmap - does the platform need unmapping of sk_buffs?
  193. *
  194. * Returns true if the platform needs sk_buff unmapping. The compiler
  195. * optimizes away unnecessary code if this returns true.
  196. */
  197. static inline int need_skb_unmap(void)
  198. {
  199. #ifdef CONFIG_NEED_DMA_MAP_STATE
  200. return 1;
  201. #else
  202. return 0;
  203. #endif
  204. }
  205. /**
  206. * txq_avail - return the number of available slots in a TX queue
  207. * @tq: the TX queue
  208. *
  209. * Returns the number of available descriptors in a TX queue.
  210. */
  211. static inline unsigned int txq_avail(const struct sge_txq *tq)
  212. {
  213. return tq->size - 1 - tq->in_use;
  214. }
  215. /**
  216. * fl_cap - return the capacity of a Free List
  217. * @fl: the Free List
  218. *
  219. * Returns the capacity of a Free List. The capacity is less than the
  220. * size because an Egress Queue Index Unit worth of descriptors needs to
  221. * be left unpopulated, otherwise the Producer and Consumer indices PIDX
  222. * and CIDX will match and the hardware will think the FL is empty.
  223. */
  224. static inline unsigned int fl_cap(const struct sge_fl *fl)
  225. {
  226. return fl->size - FL_PER_EQ_UNIT;
  227. }
  228. /**
  229. * fl_starving - return whether a Free List is starving.
  230. * @adapter: pointer to the adapter
  231. * @fl: the Free List
  232. *
  233. * Tests specified Free List to see whether the number of buffers
  234. * available to the hardware has falled below our "starvation"
  235. * threshold.
  236. */
  237. static inline bool fl_starving(const struct adapter *adapter,
  238. const struct sge_fl *fl)
  239. {
  240. const struct sge *s = &adapter->sge;
  241. return fl->avail - fl->pend_cred <= s->fl_starve_thres;
  242. }
  243. /**
  244. * map_skb - map an skb for DMA to the device
  245. * @dev: the egress net device
  246. * @skb: the packet to map
  247. * @addr: a pointer to the base of the DMA mapping array
  248. *
  249. * Map an skb for DMA to the device and return an array of DMA addresses.
  250. */
  251. static int map_skb(struct device *dev, const struct sk_buff *skb,
  252. dma_addr_t *addr)
  253. {
  254. const skb_frag_t *fp, *end;
  255. const struct skb_shared_info *si;
  256. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  257. if (dma_mapping_error(dev, *addr))
  258. goto out_err;
  259. si = skb_shinfo(skb);
  260. end = &si->frags[si->nr_frags];
  261. for (fp = si->frags; fp < end; fp++) {
  262. *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
  263. DMA_TO_DEVICE);
  264. if (dma_mapping_error(dev, *addr))
  265. goto unwind;
  266. }
  267. return 0;
  268. unwind:
  269. while (fp-- > si->frags)
  270. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  271. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  272. out_err:
  273. return -ENOMEM;
  274. }
  275. static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
  276. const struct ulptx_sgl *sgl, const struct sge_txq *tq)
  277. {
  278. const struct ulptx_sge_pair *p;
  279. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  280. if (likely(skb_headlen(skb)))
  281. dma_unmap_single(dev, be64_to_cpu(sgl->addr0),
  282. be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
  283. else {
  284. dma_unmap_page(dev, be64_to_cpu(sgl->addr0),
  285. be32_to_cpu(sgl->len0), DMA_TO_DEVICE);
  286. nfrags--;
  287. }
  288. /*
  289. * the complexity below is because of the possibility of a wrap-around
  290. * in the middle of an SGL
  291. */
  292. for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
  293. if (likely((u8 *)(p + 1) <= (u8 *)tq->stat)) {
  294. unmap:
  295. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  296. be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
  297. dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
  298. be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
  299. p++;
  300. } else if ((u8 *)p == (u8 *)tq->stat) {
  301. p = (const struct ulptx_sge_pair *)tq->desc;
  302. goto unmap;
  303. } else if ((u8 *)p + 8 == (u8 *)tq->stat) {
  304. const __be64 *addr = (const __be64 *)tq->desc;
  305. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  306. be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
  307. dma_unmap_page(dev, be64_to_cpu(addr[1]),
  308. be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
  309. p = (const struct ulptx_sge_pair *)&addr[2];
  310. } else {
  311. const __be64 *addr = (const __be64 *)tq->desc;
  312. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  313. be32_to_cpu(p->len[0]), DMA_TO_DEVICE);
  314. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  315. be32_to_cpu(p->len[1]), DMA_TO_DEVICE);
  316. p = (const struct ulptx_sge_pair *)&addr[1];
  317. }
  318. }
  319. if (nfrags) {
  320. __be64 addr;
  321. if ((u8 *)p == (u8 *)tq->stat)
  322. p = (const struct ulptx_sge_pair *)tq->desc;
  323. addr = ((u8 *)p + 16 <= (u8 *)tq->stat
  324. ? p->addr[0]
  325. : *(const __be64 *)tq->desc);
  326. dma_unmap_page(dev, be64_to_cpu(addr), be32_to_cpu(p->len[0]),
  327. DMA_TO_DEVICE);
  328. }
  329. }
  330. /**
  331. * free_tx_desc - reclaims TX descriptors and their buffers
  332. * @adapter: the adapter
  333. * @tq: the TX queue to reclaim descriptors from
  334. * @n: the number of descriptors to reclaim
  335. * @unmap: whether the buffers should be unmapped for DMA
  336. *
  337. * Reclaims TX descriptors from an SGE TX queue and frees the associated
  338. * TX buffers. Called with the TX queue lock held.
  339. */
  340. static void free_tx_desc(struct adapter *adapter, struct sge_txq *tq,
  341. unsigned int n, bool unmap)
  342. {
  343. struct tx_sw_desc *sdesc;
  344. unsigned int cidx = tq->cidx;
  345. struct device *dev = adapter->pdev_dev;
  346. const int need_unmap = need_skb_unmap() && unmap;
  347. sdesc = &tq->sdesc[cidx];
  348. while (n--) {
  349. /*
  350. * If we kept a reference to the original TX skb, we need to
  351. * unmap it from PCI DMA space (if required) and free it.
  352. */
  353. if (sdesc->skb) {
  354. if (need_unmap)
  355. unmap_sgl(dev, sdesc->skb, sdesc->sgl, tq);
  356. dev_consume_skb_any(sdesc->skb);
  357. sdesc->skb = NULL;
  358. }
  359. sdesc++;
  360. if (++cidx == tq->size) {
  361. cidx = 0;
  362. sdesc = tq->sdesc;
  363. }
  364. }
  365. tq->cidx = cidx;
  366. }
  367. /*
  368. * Return the number of reclaimable descriptors in a TX queue.
  369. */
  370. static inline int reclaimable(const struct sge_txq *tq)
  371. {
  372. int hw_cidx = be16_to_cpu(tq->stat->cidx);
  373. int reclaimable = hw_cidx - tq->cidx;
  374. if (reclaimable < 0)
  375. reclaimable += tq->size;
  376. return reclaimable;
  377. }
  378. /**
  379. * reclaim_completed_tx - reclaims completed TX descriptors
  380. * @adapter: the adapter
  381. * @tq: the TX queue to reclaim completed descriptors from
  382. * @unmap: whether the buffers should be unmapped for DMA
  383. *
  384. * Reclaims TX descriptors that the SGE has indicated it has processed,
  385. * and frees the associated buffers if possible. Called with the TX
  386. * queue locked.
  387. */
  388. static inline void reclaim_completed_tx(struct adapter *adapter,
  389. struct sge_txq *tq,
  390. bool unmap)
  391. {
  392. int avail = reclaimable(tq);
  393. if (avail) {
  394. /*
  395. * Limit the amount of clean up work we do at a time to keep
  396. * the TX lock hold time O(1).
  397. */
  398. if (avail > MAX_TX_RECLAIM)
  399. avail = MAX_TX_RECLAIM;
  400. free_tx_desc(adapter, tq, avail, unmap);
  401. tq->in_use -= avail;
  402. }
  403. }
  404. /**
  405. * get_buf_size - return the size of an RX Free List buffer.
  406. * @adapter: pointer to the associated adapter
  407. * @sdesc: pointer to the software buffer descriptor
  408. */
  409. static inline int get_buf_size(const struct adapter *adapter,
  410. const struct rx_sw_desc *sdesc)
  411. {
  412. const struct sge *s = &adapter->sge;
  413. return (s->fl_pg_order > 0 && (sdesc->dma_addr & RX_LARGE_BUF)
  414. ? (PAGE_SIZE << s->fl_pg_order) : PAGE_SIZE);
  415. }
  416. /**
  417. * free_rx_bufs - free RX buffers on an SGE Free List
  418. * @adapter: the adapter
  419. * @fl: the SGE Free List to free buffers from
  420. * @n: how many buffers to free
  421. *
  422. * Release the next @n buffers on an SGE Free List RX queue. The
  423. * buffers must be made inaccessible to hardware before calling this
  424. * function.
  425. */
  426. static void free_rx_bufs(struct adapter *adapter, struct sge_fl *fl, int n)
  427. {
  428. while (n--) {
  429. struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
  430. if (is_buf_mapped(sdesc))
  431. dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
  432. get_buf_size(adapter, sdesc),
  433. PCI_DMA_FROMDEVICE);
  434. put_page(sdesc->page);
  435. sdesc->page = NULL;
  436. if (++fl->cidx == fl->size)
  437. fl->cidx = 0;
  438. fl->avail--;
  439. }
  440. }
  441. /**
  442. * unmap_rx_buf - unmap the current RX buffer on an SGE Free List
  443. * @adapter: the adapter
  444. * @fl: the SGE Free List
  445. *
  446. * Unmap the current buffer on an SGE Free List RX queue. The
  447. * buffer must be made inaccessible to HW before calling this function.
  448. *
  449. * This is similar to @free_rx_bufs above but does not free the buffer.
  450. * Do note that the FL still loses any further access to the buffer.
  451. * This is used predominantly to "transfer ownership" of an FL buffer
  452. * to another entity (typically an skb's fragment list).
  453. */
  454. static void unmap_rx_buf(struct adapter *adapter, struct sge_fl *fl)
  455. {
  456. struct rx_sw_desc *sdesc = &fl->sdesc[fl->cidx];
  457. if (is_buf_mapped(sdesc))
  458. dma_unmap_page(adapter->pdev_dev, get_buf_addr(sdesc),
  459. get_buf_size(adapter, sdesc),
  460. PCI_DMA_FROMDEVICE);
  461. sdesc->page = NULL;
  462. if (++fl->cidx == fl->size)
  463. fl->cidx = 0;
  464. fl->avail--;
  465. }
  466. /**
  467. * ring_fl_db - righ doorbell on free list
  468. * @adapter: the adapter
  469. * @fl: the Free List whose doorbell should be rung ...
  470. *
  471. * Tell the Scatter Gather Engine that there are new free list entries
  472. * available.
  473. */
  474. static inline void ring_fl_db(struct adapter *adapter, struct sge_fl *fl)
  475. {
  476. u32 val;
  477. /* The SGE keeps track of its Producer and Consumer Indices in terms
  478. * of Egress Queue Units so we can only tell it about integral numbers
  479. * of multiples of Free List Entries per Egress Queue Units ...
  480. */
  481. if (fl->pend_cred >= FL_PER_EQ_UNIT) {
  482. if (is_t4(adapter->params.chip))
  483. val = PIDX_V(fl->pend_cred / FL_PER_EQ_UNIT);
  484. else
  485. val = PIDX_T5_V(fl->pend_cred / FL_PER_EQ_UNIT) |
  486. DBTYPE_F;
  487. val |= DBPRIO_F;
  488. /* Make sure all memory writes to the Free List queue are
  489. * committed before we tell the hardware about them.
  490. */
  491. wmb();
  492. /* If we don't have access to the new User Doorbell (T5+), use
  493. * the old doorbell mechanism; otherwise use the new BAR2
  494. * mechanism.
  495. */
  496. if (unlikely(fl->bar2_addr == NULL)) {
  497. t4_write_reg(adapter,
  498. T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
  499. QID_V(fl->cntxt_id) | val);
  500. } else {
  501. writel(val | QID_V(fl->bar2_qid),
  502. fl->bar2_addr + SGE_UDB_KDOORBELL);
  503. /* This Write memory Barrier will force the write to
  504. * the User Doorbell area to be flushed.
  505. */
  506. wmb();
  507. }
  508. fl->pend_cred %= FL_PER_EQ_UNIT;
  509. }
  510. }
  511. /**
  512. * set_rx_sw_desc - initialize software RX buffer descriptor
  513. * @sdesc: pointer to the softwore RX buffer descriptor
  514. * @page: pointer to the page data structure backing the RX buffer
  515. * @dma_addr: PCI DMA address (possibly with low-bit flags)
  516. */
  517. static inline void set_rx_sw_desc(struct rx_sw_desc *sdesc, struct page *page,
  518. dma_addr_t dma_addr)
  519. {
  520. sdesc->page = page;
  521. sdesc->dma_addr = dma_addr;
  522. }
  523. /*
  524. * Support for poisoning RX buffers ...
  525. */
  526. #define POISON_BUF_VAL -1
  527. static inline void poison_buf(struct page *page, size_t sz)
  528. {
  529. #if POISON_BUF_VAL >= 0
  530. memset(page_address(page), POISON_BUF_VAL, sz);
  531. #endif
  532. }
  533. /**
  534. * refill_fl - refill an SGE RX buffer ring
  535. * @adapter: the adapter
  536. * @fl: the Free List ring to refill
  537. * @n: the number of new buffers to allocate
  538. * @gfp: the gfp flags for the allocations
  539. *
  540. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  541. * allocated with the supplied gfp flags. The caller must assure that
  542. * @n does not exceed the queue's capacity -- i.e. (cidx == pidx) _IN
  543. * EGRESS QUEUE UNITS_ indicates an empty Free List! Returns the number
  544. * of buffers allocated. If afterwards the queue is found critically low,
  545. * mark it as starving in the bitmap of starving FLs.
  546. */
  547. static unsigned int refill_fl(struct adapter *adapter, struct sge_fl *fl,
  548. int n, gfp_t gfp)
  549. {
  550. struct sge *s = &adapter->sge;
  551. struct page *page;
  552. dma_addr_t dma_addr;
  553. unsigned int cred = fl->avail;
  554. __be64 *d = &fl->desc[fl->pidx];
  555. struct rx_sw_desc *sdesc = &fl->sdesc[fl->pidx];
  556. /*
  557. * Sanity: ensure that the result of adding n Free List buffers
  558. * won't result in wrapping the SGE's Producer Index around to
  559. * it's Consumer Index thereby indicating an empty Free List ...
  560. */
  561. BUG_ON(fl->avail + n > fl->size - FL_PER_EQ_UNIT);
  562. gfp |= __GFP_NOWARN;
  563. /*
  564. * If we support large pages, prefer large buffers and fail over to
  565. * small pages if we can't allocate large pages to satisfy the refill.
  566. * If we don't support large pages, drop directly into the small page
  567. * allocation code.
  568. */
  569. if (s->fl_pg_order == 0)
  570. goto alloc_small_pages;
  571. while (n) {
  572. page = __dev_alloc_pages(gfp, s->fl_pg_order);
  573. if (unlikely(!page)) {
  574. /*
  575. * We've failed inour attempt to allocate a "large
  576. * page". Fail over to the "small page" allocation
  577. * below.
  578. */
  579. fl->large_alloc_failed++;
  580. break;
  581. }
  582. poison_buf(page, PAGE_SIZE << s->fl_pg_order);
  583. dma_addr = dma_map_page(adapter->pdev_dev, page, 0,
  584. PAGE_SIZE << s->fl_pg_order,
  585. PCI_DMA_FROMDEVICE);
  586. if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
  587. /*
  588. * We've run out of DMA mapping space. Free up the
  589. * buffer and return with what we've managed to put
  590. * into the free list. We don't want to fail over to
  591. * the small page allocation below in this case
  592. * because DMA mapping resources are typically
  593. * critical resources once they become scarse.
  594. */
  595. __free_pages(page, s->fl_pg_order);
  596. goto out;
  597. }
  598. dma_addr |= RX_LARGE_BUF;
  599. *d++ = cpu_to_be64(dma_addr);
  600. set_rx_sw_desc(sdesc, page, dma_addr);
  601. sdesc++;
  602. fl->avail++;
  603. if (++fl->pidx == fl->size) {
  604. fl->pidx = 0;
  605. sdesc = fl->sdesc;
  606. d = fl->desc;
  607. }
  608. n--;
  609. }
  610. alloc_small_pages:
  611. while (n--) {
  612. page = __dev_alloc_page(gfp);
  613. if (unlikely(!page)) {
  614. fl->alloc_failed++;
  615. break;
  616. }
  617. poison_buf(page, PAGE_SIZE);
  618. dma_addr = dma_map_page(adapter->pdev_dev, page, 0, PAGE_SIZE,
  619. PCI_DMA_FROMDEVICE);
  620. if (unlikely(dma_mapping_error(adapter->pdev_dev, dma_addr))) {
  621. put_page(page);
  622. break;
  623. }
  624. *d++ = cpu_to_be64(dma_addr);
  625. set_rx_sw_desc(sdesc, page, dma_addr);
  626. sdesc++;
  627. fl->avail++;
  628. if (++fl->pidx == fl->size) {
  629. fl->pidx = 0;
  630. sdesc = fl->sdesc;
  631. d = fl->desc;
  632. }
  633. }
  634. out:
  635. /*
  636. * Update our accounting state to incorporate the new Free List
  637. * buffers, tell the hardware about them and return the number of
  638. * buffers which we were able to allocate.
  639. */
  640. cred = fl->avail - cred;
  641. fl->pend_cred += cred;
  642. ring_fl_db(adapter, fl);
  643. if (unlikely(fl_starving(adapter, fl))) {
  644. smp_wmb();
  645. set_bit(fl->cntxt_id, adapter->sge.starving_fl);
  646. }
  647. return cred;
  648. }
  649. /*
  650. * Refill a Free List to its capacity or the Maximum Refill Increment,
  651. * whichever is smaller ...
  652. */
  653. static inline void __refill_fl(struct adapter *adapter, struct sge_fl *fl)
  654. {
  655. refill_fl(adapter, fl,
  656. min((unsigned int)MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  657. GFP_ATOMIC);
  658. }
  659. /**
  660. * alloc_ring - allocate resources for an SGE descriptor ring
  661. * @dev: the PCI device's core device
  662. * @nelem: the number of descriptors
  663. * @hwsize: the size of each hardware descriptor
  664. * @swsize: the size of each software descriptor
  665. * @busaddrp: the physical PCI bus address of the allocated ring
  666. * @swringp: return address pointer for software ring
  667. * @stat_size: extra space in hardware ring for status information
  668. *
  669. * Allocates resources for an SGE descriptor ring, such as TX queues,
  670. * free buffer lists, response queues, etc. Each SGE ring requires
  671. * space for its hardware descriptors plus, optionally, space for software
  672. * state associated with each hardware entry (the metadata). The function
  673. * returns three values: the virtual address for the hardware ring (the
  674. * return value of the function), the PCI bus address of the hardware
  675. * ring (in *busaddrp), and the address of the software ring (in swringp).
  676. * Both the hardware and software rings are returned zeroed out.
  677. */
  678. static void *alloc_ring(struct device *dev, size_t nelem, size_t hwsize,
  679. size_t swsize, dma_addr_t *busaddrp, void *swringp,
  680. size_t stat_size)
  681. {
  682. /*
  683. * Allocate the hardware ring and PCI DMA bus address space for said.
  684. */
  685. size_t hwlen = nelem * hwsize + stat_size;
  686. void *hwring = dma_alloc_coherent(dev, hwlen, busaddrp, GFP_KERNEL);
  687. if (!hwring)
  688. return NULL;
  689. /*
  690. * If the caller wants a software ring, allocate it and return a
  691. * pointer to it in *swringp.
  692. */
  693. BUG_ON((swsize != 0) != (swringp != NULL));
  694. if (swsize) {
  695. void *swring = kcalloc(nelem, swsize, GFP_KERNEL);
  696. if (!swring) {
  697. dma_free_coherent(dev, hwlen, hwring, *busaddrp);
  698. return NULL;
  699. }
  700. *(void **)swringp = swring;
  701. }
  702. /*
  703. * Zero out the hardware ring and return its address as our function
  704. * value.
  705. */
  706. memset(hwring, 0, hwlen);
  707. return hwring;
  708. }
  709. /**
  710. * sgl_len - calculates the size of an SGL of the given capacity
  711. * @n: the number of SGL entries
  712. *
  713. * Calculates the number of flits (8-byte units) needed for a Direct
  714. * Scatter/Gather List that can hold the given number of entries.
  715. */
  716. static inline unsigned int sgl_len(unsigned int n)
  717. {
  718. /*
  719. * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
  720. * addresses. The DSGL Work Request starts off with a 32-bit DSGL
  721. * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
  722. * repeated sequences of { Length[i], Length[i+1], Address[i],
  723. * Address[i+1] } (this ensures that all addresses are on 64-bit
  724. * boundaries). If N is even, then Length[N+1] should be set to 0 and
  725. * Address[N+1] is omitted.
  726. *
  727. * The following calculation incorporates all of the above. It's
  728. * somewhat hard to follow but, briefly: the "+2" accounts for the
  729. * first two flits which include the DSGL header, Length0 and
  730. * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
  731. * flits for every pair of the remaining N) +1 if (n-1) is odd; and
  732. * finally the "+((n-1)&1)" adds the one remaining flit needed if
  733. * (n-1) is odd ...
  734. */
  735. n--;
  736. return (3 * n) / 2 + (n & 1) + 2;
  737. }
  738. /**
  739. * flits_to_desc - returns the num of TX descriptors for the given flits
  740. * @flits: the number of flits
  741. *
  742. * Returns the number of TX descriptors needed for the supplied number
  743. * of flits.
  744. */
  745. static inline unsigned int flits_to_desc(unsigned int flits)
  746. {
  747. BUG_ON(flits > SGE_MAX_WR_LEN / sizeof(__be64));
  748. return DIV_ROUND_UP(flits, TXD_PER_EQ_UNIT);
  749. }
  750. /**
  751. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  752. * @skb: the packet
  753. *
  754. * Returns whether an Ethernet packet is small enough to fit completely as
  755. * immediate data.
  756. */
  757. static inline int is_eth_imm(const struct sk_buff *skb)
  758. {
  759. /*
  760. * The VF Driver uses the FW_ETH_TX_PKT_VM_WR firmware Work Request
  761. * which does not accommodate immediate data. We could dike out all
  762. * of the support code for immediate data but that would tie our hands
  763. * too much if we ever want to enhace the firmware. It would also
  764. * create more differences between the PF and VF Drivers.
  765. */
  766. return false;
  767. }
  768. /**
  769. * calc_tx_flits - calculate the number of flits for a packet TX WR
  770. * @skb: the packet
  771. *
  772. * Returns the number of flits needed for a TX Work Request for the
  773. * given Ethernet packet, including the needed WR and CPL headers.
  774. */
  775. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  776. {
  777. unsigned int flits;
  778. /*
  779. * If the skb is small enough, we can pump it out as a work request
  780. * with only immediate data. In that case we just have to have the
  781. * TX Packet header plus the skb data in the Work Request.
  782. */
  783. if (is_eth_imm(skb))
  784. return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt),
  785. sizeof(__be64));
  786. /*
  787. * Otherwise, we're going to have to construct a Scatter gather list
  788. * of the skb body and fragments. We also include the flits necessary
  789. * for the TX Packet Work Request and CPL. We always have a firmware
  790. * Write Header (incorporated as part of the cpl_tx_pkt_lso and
  791. * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
  792. * message or, if we're doing a Large Send Offload, an LSO CPL message
  793. * with an embedded TX Packet Write CPL message.
  794. */
  795. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  796. if (skb_shinfo(skb)->gso_size)
  797. flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  798. sizeof(struct cpl_tx_pkt_lso_core) +
  799. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  800. else
  801. flits += (sizeof(struct fw_eth_tx_pkt_vm_wr) +
  802. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  803. return flits;
  804. }
  805. /**
  806. * write_sgl - populate a Scatter/Gather List for a packet
  807. * @skb: the packet
  808. * @tq: the TX queue we are writing into
  809. * @sgl: starting location for writing the SGL
  810. * @end: points right after the end of the SGL
  811. * @start: start offset into skb main-body data to include in the SGL
  812. * @addr: the list of DMA bus addresses for the SGL elements
  813. *
  814. * Generates a Scatter/Gather List for the buffers that make up a packet.
  815. * The caller must provide adequate space for the SGL that will be written.
  816. * The SGL includes all of the packet's page fragments and the data in its
  817. * main body except for the first @start bytes. @pos must be 16-byte
  818. * aligned and within a TX descriptor with available space. @end points
  819. * write after the end of the SGL but does not account for any potential
  820. * wrap around, i.e., @end > @tq->stat.
  821. */
  822. static void write_sgl(const struct sk_buff *skb, struct sge_txq *tq,
  823. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  824. const dma_addr_t *addr)
  825. {
  826. unsigned int i, len;
  827. struct ulptx_sge_pair *to;
  828. const struct skb_shared_info *si = skb_shinfo(skb);
  829. unsigned int nfrags = si->nr_frags;
  830. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  831. len = skb_headlen(skb) - start;
  832. if (likely(len)) {
  833. sgl->len0 = htonl(len);
  834. sgl->addr0 = cpu_to_be64(addr[0] + start);
  835. nfrags++;
  836. } else {
  837. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  838. sgl->addr0 = cpu_to_be64(addr[1]);
  839. }
  840. sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  841. ULPTX_NSGE_V(nfrags));
  842. if (likely(--nfrags == 0))
  843. return;
  844. /*
  845. * Most of the complexity below deals with the possibility we hit the
  846. * end of the queue in the middle of writing the SGL. For this case
  847. * only we create the SGL in a temporary buffer and then copy it.
  848. */
  849. to = (u8 *)end > (u8 *)tq->stat ? buf : sgl->sge;
  850. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  851. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  852. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  853. to->addr[0] = cpu_to_be64(addr[i]);
  854. to->addr[1] = cpu_to_be64(addr[++i]);
  855. }
  856. if (nfrags) {
  857. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  858. to->len[1] = cpu_to_be32(0);
  859. to->addr[0] = cpu_to_be64(addr[i + 1]);
  860. }
  861. if (unlikely((u8 *)end > (u8 *)tq->stat)) {
  862. unsigned int part0 = (u8 *)tq->stat - (u8 *)sgl->sge, part1;
  863. if (likely(part0))
  864. memcpy(sgl->sge, buf, part0);
  865. part1 = (u8 *)end - (u8 *)tq->stat;
  866. memcpy(tq->desc, (u8 *)buf + part0, part1);
  867. end = (void *)tq->desc + part1;
  868. }
  869. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  870. *end = 0;
  871. }
  872. /**
  873. * check_ring_tx_db - check and potentially ring a TX queue's doorbell
  874. * @adapter: the adapter
  875. * @tq: the TX queue
  876. * @n: number of new descriptors to give to HW
  877. *
  878. * Ring the doorbel for a TX queue.
  879. */
  880. static inline void ring_tx_db(struct adapter *adapter, struct sge_txq *tq,
  881. int n)
  882. {
  883. /* Make sure that all writes to the TX Descriptors are committed
  884. * before we tell the hardware about them.
  885. */
  886. wmb();
  887. /* If we don't have access to the new User Doorbell (T5+), use the old
  888. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  889. */
  890. if (unlikely(tq->bar2_addr == NULL)) {
  891. u32 val = PIDX_V(n);
  892. t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_KDOORBELL,
  893. QID_V(tq->cntxt_id) | val);
  894. } else {
  895. u32 val = PIDX_T5_V(n);
  896. /* T4 and later chips share the same PIDX field offset within
  897. * the doorbell, but T5 and later shrank the field in order to
  898. * gain a bit for Doorbell Priority. The field was absurdly
  899. * large in the first place (14 bits) so we just use the T5
  900. * and later limits and warn if a Queue ID is too large.
  901. */
  902. WARN_ON(val & DBPRIO_F);
  903. /* If we're only writing a single Egress Unit and the BAR2
  904. * Queue ID is 0, we can use the Write Combining Doorbell
  905. * Gather Buffer; otherwise we use the simple doorbell.
  906. */
  907. if (n == 1 && tq->bar2_qid == 0) {
  908. unsigned int index = (tq->pidx
  909. ? (tq->pidx - 1)
  910. : (tq->size - 1));
  911. __be64 *src = (__be64 *)&tq->desc[index];
  912. __be64 __iomem *dst = (__be64 *)(tq->bar2_addr +
  913. SGE_UDB_WCDOORBELL);
  914. unsigned int count = EQ_UNIT / sizeof(__be64);
  915. /* Copy the TX Descriptor in a tight loop in order to
  916. * try to get it to the adapter in a single Write
  917. * Combined transfer on the PCI-E Bus. If the Write
  918. * Combine fails (say because of an interrupt, etc.)
  919. * the hardware will simply take the last write as a
  920. * simple doorbell write with a PIDX Increment of 1
  921. * and will fetch the TX Descriptor from memory via
  922. * DMA.
  923. */
  924. while (count) {
  925. writeq(*src, dst);
  926. src++;
  927. dst++;
  928. count--;
  929. }
  930. } else
  931. writel(val | QID_V(tq->bar2_qid),
  932. tq->bar2_addr + SGE_UDB_KDOORBELL);
  933. /* This Write Memory Barrier will force the write to the User
  934. * Doorbell area to be flushed. This is needed to prevent
  935. * writes on different CPUs for the same queue from hitting
  936. * the adapter out of order. This is required when some Work
  937. * Requests take the Write Combine Gather Buffer path (user
  938. * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
  939. * take the traditional path where we simply increment the
  940. * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
  941. * hardware DMA read the actual Work Request.
  942. */
  943. wmb();
  944. }
  945. }
  946. /**
  947. * inline_tx_skb - inline a packet's data into TX descriptors
  948. * @skb: the packet
  949. * @tq: the TX queue where the packet will be inlined
  950. * @pos: starting position in the TX queue to inline the packet
  951. *
  952. * Inline a packet's contents directly into TX descriptors, starting at
  953. * the given position within the TX DMA ring.
  954. * Most of the complexity of this operation is dealing with wrap arounds
  955. * in the middle of the packet we want to inline.
  956. */
  957. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *tq,
  958. void *pos)
  959. {
  960. u64 *p;
  961. int left = (void *)tq->stat - pos;
  962. if (likely(skb->len <= left)) {
  963. if (likely(!skb->data_len))
  964. skb_copy_from_linear_data(skb, pos, skb->len);
  965. else
  966. skb_copy_bits(skb, 0, pos, skb->len);
  967. pos += skb->len;
  968. } else {
  969. skb_copy_bits(skb, 0, pos, left);
  970. skb_copy_bits(skb, left, tq->desc, skb->len - left);
  971. pos = (void *)tq->desc + (skb->len - left);
  972. }
  973. /* 0-pad to multiple of 16 */
  974. p = PTR_ALIGN(pos, 8);
  975. if ((uintptr_t)p & 8)
  976. *p = 0;
  977. }
  978. /*
  979. * Figure out what HW csum a packet wants and return the appropriate control
  980. * bits.
  981. */
  982. static u64 hwcsum(const struct sk_buff *skb)
  983. {
  984. int csum_type;
  985. const struct iphdr *iph = ip_hdr(skb);
  986. if (iph->version == 4) {
  987. if (iph->protocol == IPPROTO_TCP)
  988. csum_type = TX_CSUM_TCPIP;
  989. else if (iph->protocol == IPPROTO_UDP)
  990. csum_type = TX_CSUM_UDPIP;
  991. else {
  992. nocsum:
  993. /*
  994. * unknown protocol, disable HW csum
  995. * and hope a bad packet is detected
  996. */
  997. return TXPKT_L4CSUM_DIS;
  998. }
  999. } else {
  1000. /*
  1001. * this doesn't work with extension headers
  1002. */
  1003. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  1004. if (ip6h->nexthdr == IPPROTO_TCP)
  1005. csum_type = TX_CSUM_TCPIP6;
  1006. else if (ip6h->nexthdr == IPPROTO_UDP)
  1007. csum_type = TX_CSUM_UDPIP6;
  1008. else
  1009. goto nocsum;
  1010. }
  1011. if (likely(csum_type >= TX_CSUM_TCPIP))
  1012. return TXPKT_CSUM_TYPE(csum_type) |
  1013. TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
  1014. TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
  1015. else {
  1016. int start = skb_transport_offset(skb);
  1017. return TXPKT_CSUM_TYPE(csum_type) |
  1018. TXPKT_CSUM_START(start) |
  1019. TXPKT_CSUM_LOC(start + skb->csum_offset);
  1020. }
  1021. }
  1022. /*
  1023. * Stop an Ethernet TX queue and record that state change.
  1024. */
  1025. static void txq_stop(struct sge_eth_txq *txq)
  1026. {
  1027. netif_tx_stop_queue(txq->txq);
  1028. txq->q.stops++;
  1029. }
  1030. /*
  1031. * Advance our software state for a TX queue by adding n in use descriptors.
  1032. */
  1033. static inline void txq_advance(struct sge_txq *tq, unsigned int n)
  1034. {
  1035. tq->in_use += n;
  1036. tq->pidx += n;
  1037. if (tq->pidx >= tq->size)
  1038. tq->pidx -= tq->size;
  1039. }
  1040. /**
  1041. * t4vf_eth_xmit - add a packet to an Ethernet TX queue
  1042. * @skb: the packet
  1043. * @dev: the egress net device
  1044. *
  1045. * Add a packet to an SGE Ethernet TX queue. Runs with softirqs disabled.
  1046. */
  1047. int t4vf_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  1048. {
  1049. u32 wr_mid;
  1050. u64 cntrl, *end;
  1051. int qidx, credits;
  1052. unsigned int flits, ndesc;
  1053. struct adapter *adapter;
  1054. struct sge_eth_txq *txq;
  1055. const struct port_info *pi;
  1056. struct fw_eth_tx_pkt_vm_wr *wr;
  1057. struct cpl_tx_pkt_core *cpl;
  1058. const struct skb_shared_info *ssi;
  1059. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  1060. const size_t fw_hdr_copy_len = (sizeof(wr->ethmacdst) +
  1061. sizeof(wr->ethmacsrc) +
  1062. sizeof(wr->ethtype) +
  1063. sizeof(wr->vlantci));
  1064. /*
  1065. * The chip minimum packet length is 10 octets but the firmware
  1066. * command that we are using requires that we copy the Ethernet header
  1067. * (including the VLAN tag) into the header so we reject anything
  1068. * smaller than that ...
  1069. */
  1070. if (unlikely(skb->len < fw_hdr_copy_len))
  1071. goto out_free;
  1072. /*
  1073. * Figure out which TX Queue we're going to use.
  1074. */
  1075. pi = netdev_priv(dev);
  1076. adapter = pi->adapter;
  1077. qidx = skb_get_queue_mapping(skb);
  1078. BUG_ON(qidx >= pi->nqsets);
  1079. txq = &adapter->sge.ethtxq[pi->first_qset + qidx];
  1080. /*
  1081. * Take this opportunity to reclaim any TX Descriptors whose DMA
  1082. * transfers have completed.
  1083. */
  1084. reclaim_completed_tx(adapter, &txq->q, true);
  1085. /*
  1086. * Calculate the number of flits and TX Descriptors we're going to
  1087. * need along with how many TX Descriptors will be left over after
  1088. * we inject our Work Request.
  1089. */
  1090. flits = calc_tx_flits(skb);
  1091. ndesc = flits_to_desc(flits);
  1092. credits = txq_avail(&txq->q) - ndesc;
  1093. if (unlikely(credits < 0)) {
  1094. /*
  1095. * Not enough room for this packet's Work Request. Stop the
  1096. * TX Queue and return a "busy" condition. The queue will get
  1097. * started later on when the firmware informs us that space
  1098. * has opened up.
  1099. */
  1100. txq_stop(txq);
  1101. dev_err(adapter->pdev_dev,
  1102. "%s: TX ring %u full while queue awake!\n",
  1103. dev->name, qidx);
  1104. return NETDEV_TX_BUSY;
  1105. }
  1106. if (!is_eth_imm(skb) &&
  1107. unlikely(map_skb(adapter->pdev_dev, skb, addr) < 0)) {
  1108. /*
  1109. * We need to map the skb into PCI DMA space (because it can't
  1110. * be in-lined directly into the Work Request) and the mapping
  1111. * operation failed. Record the error and drop the packet.
  1112. */
  1113. txq->mapping_err++;
  1114. goto out_free;
  1115. }
  1116. wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
  1117. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  1118. /*
  1119. * After we're done injecting the Work Request for this
  1120. * packet, we'll be below our "stop threshold" so stop the TX
  1121. * Queue now and schedule a request for an SGE Egress Queue
  1122. * Update message. The queue will get started later on when
  1123. * the firmware processes this Work Request and sends us an
  1124. * Egress Queue Status Update message indicating that space
  1125. * has opened up.
  1126. */
  1127. txq_stop(txq);
  1128. wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
  1129. }
  1130. /*
  1131. * Start filling in our Work Request. Note that we do _not_ handle
  1132. * the WR Header wrapping around the TX Descriptor Ring. If our
  1133. * maximum header size ever exceeds one TX Descriptor, we'll need to
  1134. * do something else here.
  1135. */
  1136. BUG_ON(DIV_ROUND_UP(ETHTXQ_MAX_HDR, TXD_PER_EQ_UNIT) > 1);
  1137. wr = (void *)&txq->q.desc[txq->q.pidx];
  1138. wr->equiq_to_len16 = cpu_to_be32(wr_mid);
  1139. wr->r3[0] = cpu_to_be64(0);
  1140. wr->r3[1] = cpu_to_be64(0);
  1141. skb_copy_from_linear_data(skb, (void *)wr->ethmacdst, fw_hdr_copy_len);
  1142. end = (u64 *)wr + flits;
  1143. /*
  1144. * If this is a Large Send Offload packet we'll put in an LSO CPL
  1145. * message with an encapsulated TX Packet CPL message. Otherwise we
  1146. * just use a TX Packet CPL message.
  1147. */
  1148. ssi = skb_shinfo(skb);
  1149. if (ssi->gso_size) {
  1150. struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
  1151. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  1152. int l3hdr_len = skb_network_header_len(skb);
  1153. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1154. wr->op_immdlen =
  1155. cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
  1156. FW_WR_IMMDLEN_V(sizeof(*lso) +
  1157. sizeof(*cpl)));
  1158. /*
  1159. * Fill in the LSO CPL message.
  1160. */
  1161. lso->lso_ctrl =
  1162. cpu_to_be32(LSO_OPCODE(CPL_TX_PKT_LSO) |
  1163. LSO_FIRST_SLICE |
  1164. LSO_LAST_SLICE |
  1165. LSO_IPV6(v6) |
  1166. LSO_ETHHDR_LEN(eth_xtra_len/4) |
  1167. LSO_IPHDR_LEN(l3hdr_len/4) |
  1168. LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
  1169. lso->ipid_ofst = cpu_to_be16(0);
  1170. lso->mss = cpu_to_be16(ssi->gso_size);
  1171. lso->seqno_offset = cpu_to_be32(0);
  1172. if (is_t4(adapter->params.chip))
  1173. lso->len = cpu_to_be32(skb->len);
  1174. else
  1175. lso->len = cpu_to_be32(LSO_T5_XFER_SIZE(skb->len));
  1176. /*
  1177. * Set up TX Packet CPL pointer, control word and perform
  1178. * accounting.
  1179. */
  1180. cpl = (void *)(lso + 1);
  1181. cntrl = (TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  1182. TXPKT_IPHDR_LEN(l3hdr_len) |
  1183. TXPKT_ETHHDR_LEN(eth_xtra_len));
  1184. txq->tso++;
  1185. txq->tx_cso += ssi->gso_segs;
  1186. } else {
  1187. int len;
  1188. len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
  1189. wr->op_immdlen =
  1190. cpu_to_be32(FW_WR_OP_V(FW_ETH_TX_PKT_VM_WR) |
  1191. FW_WR_IMMDLEN_V(len));
  1192. /*
  1193. * Set up TX Packet CPL pointer, control word and perform
  1194. * accounting.
  1195. */
  1196. cpl = (void *)(wr + 1);
  1197. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1198. cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
  1199. txq->tx_cso++;
  1200. } else
  1201. cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
  1202. }
  1203. /*
  1204. * If there's a VLAN tag present, add that to the list of things to
  1205. * do in this Work Request.
  1206. */
  1207. if (skb_vlan_tag_present(skb)) {
  1208. txq->vlan_ins++;
  1209. cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(skb_vlan_tag_get(skb));
  1210. }
  1211. /*
  1212. * Fill in the TX Packet CPL message header.
  1213. */
  1214. cpl->ctrl0 = cpu_to_be32(TXPKT_OPCODE(CPL_TX_PKT_XT) |
  1215. TXPKT_INTF(pi->port_id) |
  1216. TXPKT_PF(0));
  1217. cpl->pack = cpu_to_be16(0);
  1218. cpl->len = cpu_to_be16(skb->len);
  1219. cpl->ctrl1 = cpu_to_be64(cntrl);
  1220. #ifdef T4_TRACE
  1221. T4_TRACE5(adapter->tb[txq->q.cntxt_id & 7],
  1222. "eth_xmit: ndesc %u, credits %u, pidx %u, len %u, frags %u",
  1223. ndesc, credits, txq->q.pidx, skb->len, ssi->nr_frags);
  1224. #endif
  1225. /*
  1226. * Fill in the body of the TX Packet CPL message with either in-lined
  1227. * data or a Scatter/Gather List.
  1228. */
  1229. if (is_eth_imm(skb)) {
  1230. /*
  1231. * In-line the packet's data and free the skb since we don't
  1232. * need it any longer.
  1233. */
  1234. inline_tx_skb(skb, &txq->q, cpl + 1);
  1235. dev_consume_skb_any(skb);
  1236. } else {
  1237. /*
  1238. * Write the skb's Scatter/Gather list into the TX Packet CPL
  1239. * message and retain a pointer to the skb so we can free it
  1240. * later when its DMA completes. (We store the skb pointer
  1241. * in the Software Descriptor corresponding to the last TX
  1242. * Descriptor used by the Work Request.)
  1243. *
  1244. * The retained skb will be freed when the corresponding TX
  1245. * Descriptors are reclaimed after their DMAs complete.
  1246. * However, this could take quite a while since, in general,
  1247. * the hardware is set up to be lazy about sending DMA
  1248. * completion notifications to us and we mostly perform TX
  1249. * reclaims in the transmit routine.
  1250. *
  1251. * This is good for performamce but means that we rely on new
  1252. * TX packets arriving to run the destructors of completed
  1253. * packets, which open up space in their sockets' send queues.
  1254. * Sometimes we do not get such new packets causing TX to
  1255. * stall. A single UDP transmitter is a good example of this
  1256. * situation. We have a clean up timer that periodically
  1257. * reclaims completed packets but it doesn't run often enough
  1258. * (nor do we want it to) to prevent lengthy stalls. A
  1259. * solution to this problem is to run the destructor early,
  1260. * after the packet is queued but before it's DMAd. A con is
  1261. * that we lie to socket memory accounting, but the amount of
  1262. * extra memory is reasonable (limited by the number of TX
  1263. * descriptors), the packets do actually get freed quickly by
  1264. * new packets almost always, and for protocols like TCP that
  1265. * wait for acks to really free up the data the extra memory
  1266. * is even less. On the positive side we run the destructors
  1267. * on the sending CPU rather than on a potentially different
  1268. * completing CPU, usually a good thing.
  1269. *
  1270. * Run the destructor before telling the DMA engine about the
  1271. * packet to make sure it doesn't complete and get freed
  1272. * prematurely.
  1273. */
  1274. struct ulptx_sgl *sgl = (struct ulptx_sgl *)(cpl + 1);
  1275. struct sge_txq *tq = &txq->q;
  1276. int last_desc;
  1277. /*
  1278. * If the Work Request header was an exact multiple of our TX
  1279. * Descriptor length, then it's possible that the starting SGL
  1280. * pointer lines up exactly with the end of our TX Descriptor
  1281. * ring. If that's the case, wrap around to the beginning
  1282. * here ...
  1283. */
  1284. if (unlikely((void *)sgl == (void *)tq->stat)) {
  1285. sgl = (void *)tq->desc;
  1286. end = ((void *)tq->desc + ((void *)end - (void *)tq->stat));
  1287. }
  1288. write_sgl(skb, tq, sgl, end, 0, addr);
  1289. skb_orphan(skb);
  1290. last_desc = tq->pidx + ndesc - 1;
  1291. if (last_desc >= tq->size)
  1292. last_desc -= tq->size;
  1293. tq->sdesc[last_desc].skb = skb;
  1294. tq->sdesc[last_desc].sgl = sgl;
  1295. }
  1296. /*
  1297. * Advance our internal TX Queue state, tell the hardware about
  1298. * the new TX descriptors and return success.
  1299. */
  1300. txq_advance(&txq->q, ndesc);
  1301. dev->trans_start = jiffies;
  1302. ring_tx_db(adapter, &txq->q, ndesc);
  1303. return NETDEV_TX_OK;
  1304. out_free:
  1305. /*
  1306. * An error of some sort happened. Free the TX skb and tell the
  1307. * OS that we've "dealt" with the packet ...
  1308. */
  1309. dev_kfree_skb_any(skb);
  1310. return NETDEV_TX_OK;
  1311. }
  1312. /**
  1313. * copy_frags - copy fragments from gather list into skb_shared_info
  1314. * @skb: destination skb
  1315. * @gl: source internal packet gather list
  1316. * @offset: packet start offset in first page
  1317. *
  1318. * Copy an internal packet gather list into a Linux skb_shared_info
  1319. * structure.
  1320. */
  1321. static inline void copy_frags(struct sk_buff *skb,
  1322. const struct pkt_gl *gl,
  1323. unsigned int offset)
  1324. {
  1325. int i;
  1326. /* usually there's just one frag */
  1327. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  1328. gl->frags[0].offset + offset,
  1329. gl->frags[0].size - offset);
  1330. skb_shinfo(skb)->nr_frags = gl->nfrags;
  1331. for (i = 1; i < gl->nfrags; i++)
  1332. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  1333. gl->frags[i].offset,
  1334. gl->frags[i].size);
  1335. /* get a reference to the last page, we don't own it */
  1336. get_page(gl->frags[gl->nfrags - 1].page);
  1337. }
  1338. /**
  1339. * t4vf_pktgl_to_skb - build an sk_buff from a packet gather list
  1340. * @gl: the gather list
  1341. * @skb_len: size of sk_buff main body if it carries fragments
  1342. * @pull_len: amount of data to move to the sk_buff's main body
  1343. *
  1344. * Builds an sk_buff from the given packet gather list. Returns the
  1345. * sk_buff or %NULL if sk_buff allocation failed.
  1346. */
  1347. static struct sk_buff *t4vf_pktgl_to_skb(const struct pkt_gl *gl,
  1348. unsigned int skb_len,
  1349. unsigned int pull_len)
  1350. {
  1351. struct sk_buff *skb;
  1352. /*
  1353. * If the ingress packet is small enough, allocate an skb large enough
  1354. * for all of the data and copy it inline. Otherwise, allocate an skb
  1355. * with enough room to pull in the header and reference the rest of
  1356. * the data via the skb fragment list.
  1357. *
  1358. * Below we rely on RX_COPY_THRES being less than the smallest Rx
  1359. * buff! size, which is expected since buffers are at least
  1360. * PAGE_SIZEd. In this case packets up to RX_COPY_THRES have only one
  1361. * fragment.
  1362. */
  1363. if (gl->tot_len <= RX_COPY_THRES) {
  1364. /* small packets have only one fragment */
  1365. skb = alloc_skb(gl->tot_len, GFP_ATOMIC);
  1366. if (unlikely(!skb))
  1367. goto out;
  1368. __skb_put(skb, gl->tot_len);
  1369. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1370. } else {
  1371. skb = alloc_skb(skb_len, GFP_ATOMIC);
  1372. if (unlikely(!skb))
  1373. goto out;
  1374. __skb_put(skb, pull_len);
  1375. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1376. copy_frags(skb, gl, pull_len);
  1377. skb->len = gl->tot_len;
  1378. skb->data_len = skb->len - pull_len;
  1379. skb->truesize += skb->data_len;
  1380. }
  1381. out:
  1382. return skb;
  1383. }
  1384. /**
  1385. * t4vf_pktgl_free - free a packet gather list
  1386. * @gl: the gather list
  1387. *
  1388. * Releases the pages of a packet gather list. We do not own the last
  1389. * page on the list and do not free it.
  1390. */
  1391. static void t4vf_pktgl_free(const struct pkt_gl *gl)
  1392. {
  1393. int frag;
  1394. frag = gl->nfrags - 1;
  1395. while (frag--)
  1396. put_page(gl->frags[frag].page);
  1397. }
  1398. /**
  1399. * do_gro - perform Generic Receive Offload ingress packet processing
  1400. * @rxq: ingress RX Ethernet Queue
  1401. * @gl: gather list for ingress packet
  1402. * @pkt: CPL header for last packet fragment
  1403. *
  1404. * Perform Generic Receive Offload (GRO) ingress packet processing.
  1405. * We use the standard Linux GRO interfaces for this.
  1406. */
  1407. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1408. const struct cpl_rx_pkt *pkt)
  1409. {
  1410. struct adapter *adapter = rxq->rspq.adapter;
  1411. struct sge *s = &adapter->sge;
  1412. int ret;
  1413. struct sk_buff *skb;
  1414. skb = napi_get_frags(&rxq->rspq.napi);
  1415. if (unlikely(!skb)) {
  1416. t4vf_pktgl_free(gl);
  1417. rxq->stats.rx_drops++;
  1418. return;
  1419. }
  1420. copy_frags(skb, gl, s->pktshift);
  1421. skb->len = gl->tot_len - s->pktshift;
  1422. skb->data_len = skb->len;
  1423. skb->truesize += skb->data_len;
  1424. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1425. skb_record_rx_queue(skb, rxq->rspq.idx);
  1426. if (pkt->vlan_ex) {
  1427. __vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
  1428. be16_to_cpu(pkt->vlan));
  1429. rxq->stats.vlan_ex++;
  1430. }
  1431. ret = napi_gro_frags(&rxq->rspq.napi);
  1432. if (ret == GRO_HELD)
  1433. rxq->stats.lro_pkts++;
  1434. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1435. rxq->stats.lro_merged++;
  1436. rxq->stats.pkts++;
  1437. rxq->stats.rx_cso++;
  1438. }
  1439. /**
  1440. * t4vf_ethrx_handler - process an ingress ethernet packet
  1441. * @rspq: the response queue that received the packet
  1442. * @rsp: the response queue descriptor holding the RX_PKT message
  1443. * @gl: the gather list of packet fragments
  1444. *
  1445. * Process an ingress ethernet packet and deliver it to the stack.
  1446. */
  1447. int t4vf_ethrx_handler(struct sge_rspq *rspq, const __be64 *rsp,
  1448. const struct pkt_gl *gl)
  1449. {
  1450. struct sk_buff *skb;
  1451. const struct cpl_rx_pkt *pkt = (void *)rsp;
  1452. bool csum_ok = pkt->csum_calc && !pkt->err_vec &&
  1453. (rspq->netdev->features & NETIF_F_RXCSUM);
  1454. struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
  1455. struct adapter *adapter = rspq->adapter;
  1456. struct sge *s = &adapter->sge;
  1457. /*
  1458. * If this is a good TCP packet and we have Generic Receive Offload
  1459. * enabled, handle the packet in the GRO path.
  1460. */
  1461. if ((pkt->l2info & cpu_to_be32(RXF_TCP_F)) &&
  1462. (rspq->netdev->features & NETIF_F_GRO) && csum_ok &&
  1463. !pkt->ip_frag) {
  1464. do_gro(rxq, gl, pkt);
  1465. return 0;
  1466. }
  1467. /*
  1468. * Convert the Packet Gather List into an skb.
  1469. */
  1470. skb = t4vf_pktgl_to_skb(gl, RX_SKB_LEN, RX_PULL_LEN);
  1471. if (unlikely(!skb)) {
  1472. t4vf_pktgl_free(gl);
  1473. rxq->stats.rx_drops++;
  1474. return 0;
  1475. }
  1476. __skb_pull(skb, s->pktshift);
  1477. skb->protocol = eth_type_trans(skb, rspq->netdev);
  1478. skb_record_rx_queue(skb, rspq->idx);
  1479. rxq->stats.pkts++;
  1480. if (csum_ok && !pkt->err_vec &&
  1481. (be32_to_cpu(pkt->l2info) & (RXF_UDP_F | RXF_TCP_F))) {
  1482. if (!pkt->ip_frag)
  1483. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1484. else {
  1485. __sum16 c = (__force __sum16)pkt->csum;
  1486. skb->csum = csum_unfold(c);
  1487. skb->ip_summed = CHECKSUM_COMPLETE;
  1488. }
  1489. rxq->stats.rx_cso++;
  1490. } else
  1491. skb_checksum_none_assert(skb);
  1492. if (pkt->vlan_ex) {
  1493. rxq->stats.vlan_ex++;
  1494. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), be16_to_cpu(pkt->vlan));
  1495. }
  1496. netif_receive_skb(skb);
  1497. return 0;
  1498. }
  1499. /**
  1500. * is_new_response - check if a response is newly written
  1501. * @rc: the response control descriptor
  1502. * @rspq: the response queue
  1503. *
  1504. * Returns true if a response descriptor contains a yet unprocessed
  1505. * response.
  1506. */
  1507. static inline bool is_new_response(const struct rsp_ctrl *rc,
  1508. const struct sge_rspq *rspq)
  1509. {
  1510. return RSPD_GEN(rc->type_gen) == rspq->gen;
  1511. }
  1512. /**
  1513. * restore_rx_bufs - put back a packet's RX buffers
  1514. * @gl: the packet gather list
  1515. * @fl: the SGE Free List
  1516. * @nfrags: how many fragments in @si
  1517. *
  1518. * Called when we find out that the current packet, @si, can't be
  1519. * processed right away for some reason. This is a very rare event and
  1520. * there's no effort to make this suspension/resumption process
  1521. * particularly efficient.
  1522. *
  1523. * We implement the suspension by putting all of the RX buffers associated
  1524. * with the current packet back on the original Free List. The buffers
  1525. * have already been unmapped and are left unmapped, we mark them as
  1526. * unmapped in order to prevent further unmapping attempts. (Effectively
  1527. * this function undoes the series of @unmap_rx_buf calls which were done
  1528. * to create the current packet's gather list.) This leaves us ready to
  1529. * restart processing of the packet the next time we start processing the
  1530. * RX Queue ...
  1531. */
  1532. static void restore_rx_bufs(const struct pkt_gl *gl, struct sge_fl *fl,
  1533. int frags)
  1534. {
  1535. struct rx_sw_desc *sdesc;
  1536. while (frags--) {
  1537. if (fl->cidx == 0)
  1538. fl->cidx = fl->size - 1;
  1539. else
  1540. fl->cidx--;
  1541. sdesc = &fl->sdesc[fl->cidx];
  1542. sdesc->page = gl->frags[frags].page;
  1543. sdesc->dma_addr |= RX_UNMAPPED_BUF;
  1544. fl->avail++;
  1545. }
  1546. }
  1547. /**
  1548. * rspq_next - advance to the next entry in a response queue
  1549. * @rspq: the queue
  1550. *
  1551. * Updates the state of a response queue to advance it to the next entry.
  1552. */
  1553. static inline void rspq_next(struct sge_rspq *rspq)
  1554. {
  1555. rspq->cur_desc = (void *)rspq->cur_desc + rspq->iqe_len;
  1556. if (unlikely(++rspq->cidx == rspq->size)) {
  1557. rspq->cidx = 0;
  1558. rspq->gen ^= 1;
  1559. rspq->cur_desc = rspq->desc;
  1560. }
  1561. }
  1562. /**
  1563. * process_responses - process responses from an SGE response queue
  1564. * @rspq: the ingress response queue to process
  1565. * @budget: how many responses can be processed in this round
  1566. *
  1567. * Process responses from a Scatter Gather Engine response queue up to
  1568. * the supplied budget. Responses include received packets as well as
  1569. * control messages from firmware or hardware.
  1570. *
  1571. * Additionally choose the interrupt holdoff time for the next interrupt
  1572. * on this queue. If the system is under memory shortage use a fairly
  1573. * long delay to help recovery.
  1574. */
  1575. static int process_responses(struct sge_rspq *rspq, int budget)
  1576. {
  1577. struct sge_eth_rxq *rxq = container_of(rspq, struct sge_eth_rxq, rspq);
  1578. struct adapter *adapter = rspq->adapter;
  1579. struct sge *s = &adapter->sge;
  1580. int budget_left = budget;
  1581. while (likely(budget_left)) {
  1582. int ret, rsp_type;
  1583. const struct rsp_ctrl *rc;
  1584. rc = (void *)rspq->cur_desc + (rspq->iqe_len - sizeof(*rc));
  1585. if (!is_new_response(rc, rspq))
  1586. break;
  1587. /*
  1588. * Figure out what kind of response we've received from the
  1589. * SGE.
  1590. */
  1591. rmb();
  1592. rsp_type = RSPD_TYPE(rc->type_gen);
  1593. if (likely(rsp_type == RSP_TYPE_FLBUF)) {
  1594. struct page_frag *fp;
  1595. struct pkt_gl gl;
  1596. const struct rx_sw_desc *sdesc;
  1597. u32 bufsz, frag;
  1598. u32 len = be32_to_cpu(rc->pldbuflen_qid);
  1599. /*
  1600. * If we get a "new buffer" message from the SGE we
  1601. * need to move on to the next Free List buffer.
  1602. */
  1603. if (len & RSPD_NEWBUF) {
  1604. /*
  1605. * We get one "new buffer" message when we
  1606. * first start up a queue so we need to ignore
  1607. * it when our offset into the buffer is 0.
  1608. */
  1609. if (likely(rspq->offset > 0)) {
  1610. free_rx_bufs(rspq->adapter, &rxq->fl,
  1611. 1);
  1612. rspq->offset = 0;
  1613. }
  1614. len = RSPD_LEN(len);
  1615. }
  1616. gl.tot_len = len;
  1617. /*
  1618. * Gather packet fragments.
  1619. */
  1620. for (frag = 0, fp = gl.frags; /**/; frag++, fp++) {
  1621. BUG_ON(frag >= MAX_SKB_FRAGS);
  1622. BUG_ON(rxq->fl.avail == 0);
  1623. sdesc = &rxq->fl.sdesc[rxq->fl.cidx];
  1624. bufsz = get_buf_size(adapter, sdesc);
  1625. fp->page = sdesc->page;
  1626. fp->offset = rspq->offset;
  1627. fp->size = min(bufsz, len);
  1628. len -= fp->size;
  1629. if (!len)
  1630. break;
  1631. unmap_rx_buf(rspq->adapter, &rxq->fl);
  1632. }
  1633. gl.nfrags = frag+1;
  1634. /*
  1635. * Last buffer remains mapped so explicitly make it
  1636. * coherent for CPU access and start preloading first
  1637. * cache line ...
  1638. */
  1639. dma_sync_single_for_cpu(rspq->adapter->pdev_dev,
  1640. get_buf_addr(sdesc),
  1641. fp->size, DMA_FROM_DEVICE);
  1642. gl.va = (page_address(gl.frags[0].page) +
  1643. gl.frags[0].offset);
  1644. prefetch(gl.va);
  1645. /*
  1646. * Hand the new ingress packet to the handler for
  1647. * this Response Queue.
  1648. */
  1649. ret = rspq->handler(rspq, rspq->cur_desc, &gl);
  1650. if (likely(ret == 0))
  1651. rspq->offset += ALIGN(fp->size, s->fl_align);
  1652. else
  1653. restore_rx_bufs(&gl, &rxq->fl, frag);
  1654. } else if (likely(rsp_type == RSP_TYPE_CPL)) {
  1655. ret = rspq->handler(rspq, rspq->cur_desc, NULL);
  1656. } else {
  1657. WARN_ON(rsp_type > RSP_TYPE_CPL);
  1658. ret = 0;
  1659. }
  1660. if (unlikely(ret)) {
  1661. /*
  1662. * Couldn't process descriptor, back off for recovery.
  1663. * We use the SGE's last timer which has the longest
  1664. * interrupt coalescing value ...
  1665. */
  1666. const int NOMEM_TIMER_IDX = SGE_NTIMERS-1;
  1667. rspq->next_intr_params =
  1668. QINTR_TIMER_IDX(NOMEM_TIMER_IDX);
  1669. break;
  1670. }
  1671. rspq_next(rspq);
  1672. budget_left--;
  1673. }
  1674. /*
  1675. * If this is a Response Queue with an associated Free List and
  1676. * at least two Egress Queue units available in the Free List
  1677. * for new buffer pointers, refill the Free List.
  1678. */
  1679. if (rspq->offset >= 0 &&
  1680. rxq->fl.size - rxq->fl.avail >= 2*FL_PER_EQ_UNIT)
  1681. __refill_fl(rspq->adapter, &rxq->fl);
  1682. return budget - budget_left;
  1683. }
  1684. /**
  1685. * napi_rx_handler - the NAPI handler for RX processing
  1686. * @napi: the napi instance
  1687. * @budget: how many packets we can process in this round
  1688. *
  1689. * Handler for new data events when using NAPI. This does not need any
  1690. * locking or protection from interrupts as data interrupts are off at
  1691. * this point and other adapter interrupts do not interfere (the latter
  1692. * in not a concern at all with MSI-X as non-data interrupts then have
  1693. * a separate handler).
  1694. */
  1695. static int napi_rx_handler(struct napi_struct *napi, int budget)
  1696. {
  1697. unsigned int intr_params;
  1698. struct sge_rspq *rspq = container_of(napi, struct sge_rspq, napi);
  1699. int work_done = process_responses(rspq, budget);
  1700. u32 val;
  1701. if (likely(work_done < budget)) {
  1702. napi_complete(napi);
  1703. intr_params = rspq->next_intr_params;
  1704. rspq->next_intr_params = rspq->intr_params;
  1705. } else
  1706. intr_params = QINTR_TIMER_IDX(SGE_TIMER_UPD_CIDX);
  1707. if (unlikely(work_done == 0))
  1708. rspq->unhandled_irqs++;
  1709. val = CIDXINC_V(work_done) | SEINTARM_V(intr_params);
  1710. if (is_t4(rspq->adapter->params.chip)) {
  1711. t4_write_reg(rspq->adapter,
  1712. T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
  1713. val | INGRESSQID_V((u32)rspq->cntxt_id));
  1714. } else {
  1715. writel(val | INGRESSQID_V(rspq->bar2_qid),
  1716. rspq->bar2_addr + SGE_UDB_GTS);
  1717. wmb();
  1718. }
  1719. return work_done;
  1720. }
  1721. /*
  1722. * The MSI-X interrupt handler for an SGE response queue for the NAPI case
  1723. * (i.e., response queue serviced by NAPI polling).
  1724. */
  1725. irqreturn_t t4vf_sge_intr_msix(int irq, void *cookie)
  1726. {
  1727. struct sge_rspq *rspq = cookie;
  1728. napi_schedule(&rspq->napi);
  1729. return IRQ_HANDLED;
  1730. }
  1731. /*
  1732. * Process the indirect interrupt entries in the interrupt queue and kick off
  1733. * NAPI for each queue that has generated an entry.
  1734. */
  1735. static unsigned int process_intrq(struct adapter *adapter)
  1736. {
  1737. struct sge *s = &adapter->sge;
  1738. struct sge_rspq *intrq = &s->intrq;
  1739. unsigned int work_done;
  1740. u32 val;
  1741. spin_lock(&adapter->sge.intrq_lock);
  1742. for (work_done = 0; ; work_done++) {
  1743. const struct rsp_ctrl *rc;
  1744. unsigned int qid, iq_idx;
  1745. struct sge_rspq *rspq;
  1746. /*
  1747. * Grab the next response from the interrupt queue and bail
  1748. * out if it's not a new response.
  1749. */
  1750. rc = (void *)intrq->cur_desc + (intrq->iqe_len - sizeof(*rc));
  1751. if (!is_new_response(rc, intrq))
  1752. break;
  1753. /*
  1754. * If the response isn't a forwarded interrupt message issue a
  1755. * error and go on to the next response message. This should
  1756. * never happen ...
  1757. */
  1758. rmb();
  1759. if (unlikely(RSPD_TYPE(rc->type_gen) != RSP_TYPE_INTR)) {
  1760. dev_err(adapter->pdev_dev,
  1761. "Unexpected INTRQ response type %d\n",
  1762. RSPD_TYPE(rc->type_gen));
  1763. continue;
  1764. }
  1765. /*
  1766. * Extract the Queue ID from the interrupt message and perform
  1767. * sanity checking to make sure it really refers to one of our
  1768. * Ingress Queues which is active and matches the queue's ID.
  1769. * None of these error conditions should ever happen so we may
  1770. * want to either make them fatal and/or conditionalized under
  1771. * DEBUG.
  1772. */
  1773. qid = RSPD_QID(be32_to_cpu(rc->pldbuflen_qid));
  1774. iq_idx = IQ_IDX(s, qid);
  1775. if (unlikely(iq_idx >= MAX_INGQ)) {
  1776. dev_err(adapter->pdev_dev,
  1777. "Ingress QID %d out of range\n", qid);
  1778. continue;
  1779. }
  1780. rspq = s->ingr_map[iq_idx];
  1781. if (unlikely(rspq == NULL)) {
  1782. dev_err(adapter->pdev_dev,
  1783. "Ingress QID %d RSPQ=NULL\n", qid);
  1784. continue;
  1785. }
  1786. if (unlikely(rspq->abs_id != qid)) {
  1787. dev_err(adapter->pdev_dev,
  1788. "Ingress QID %d refers to RSPQ %d\n",
  1789. qid, rspq->abs_id);
  1790. continue;
  1791. }
  1792. /*
  1793. * Schedule NAPI processing on the indicated Response Queue
  1794. * and move on to the next entry in the Forwarded Interrupt
  1795. * Queue.
  1796. */
  1797. napi_schedule(&rspq->napi);
  1798. rspq_next(intrq);
  1799. }
  1800. val = CIDXINC_V(work_done) | SEINTARM_V(intrq->intr_params);
  1801. if (is_t4(adapter->params.chip))
  1802. t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
  1803. val | INGRESSQID_V(intrq->cntxt_id));
  1804. else {
  1805. writel(val | INGRESSQID_V(intrq->bar2_qid),
  1806. intrq->bar2_addr + SGE_UDB_GTS);
  1807. wmb();
  1808. }
  1809. spin_unlock(&adapter->sge.intrq_lock);
  1810. return work_done;
  1811. }
  1812. /*
  1813. * The MSI interrupt handler handles data events from SGE response queues as
  1814. * well as error and other async events as they all use the same MSI vector.
  1815. */
  1816. static irqreturn_t t4vf_intr_msi(int irq, void *cookie)
  1817. {
  1818. struct adapter *adapter = cookie;
  1819. process_intrq(adapter);
  1820. return IRQ_HANDLED;
  1821. }
  1822. /**
  1823. * t4vf_intr_handler - select the top-level interrupt handler
  1824. * @adapter: the adapter
  1825. *
  1826. * Selects the top-level interrupt handler based on the type of interrupts
  1827. * (MSI-X or MSI).
  1828. */
  1829. irq_handler_t t4vf_intr_handler(struct adapter *adapter)
  1830. {
  1831. BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
  1832. if (adapter->flags & USING_MSIX)
  1833. return t4vf_sge_intr_msix;
  1834. else
  1835. return t4vf_intr_msi;
  1836. }
  1837. /**
  1838. * sge_rx_timer_cb - perform periodic maintenance of SGE RX queues
  1839. * @data: the adapter
  1840. *
  1841. * Runs periodically from a timer to perform maintenance of SGE RX queues.
  1842. *
  1843. * a) Replenishes RX queues that have run out due to memory shortage.
  1844. * Normally new RX buffers are added when existing ones are consumed but
  1845. * when out of memory a queue can become empty. We schedule NAPI to do
  1846. * the actual refill.
  1847. */
  1848. static void sge_rx_timer_cb(unsigned long data)
  1849. {
  1850. struct adapter *adapter = (struct adapter *)data;
  1851. struct sge *s = &adapter->sge;
  1852. unsigned int i;
  1853. /*
  1854. * Scan the "Starving Free Lists" flag array looking for any Free
  1855. * Lists in need of more free buffers. If we find one and it's not
  1856. * being actively polled, then bump its "starving" counter and attempt
  1857. * to refill it. If we're successful in adding enough buffers to push
  1858. * the Free List over the starving threshold, then we can clear its
  1859. * "starving" status.
  1860. */
  1861. for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++) {
  1862. unsigned long m;
  1863. for (m = s->starving_fl[i]; m; m &= m - 1) {
  1864. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  1865. struct sge_fl *fl = s->egr_map[id];
  1866. clear_bit(id, s->starving_fl);
  1867. smp_mb__after_atomic();
  1868. /*
  1869. * Since we are accessing fl without a lock there's a
  1870. * small probability of a false positive where we
  1871. * schedule napi but the FL is no longer starving.
  1872. * No biggie.
  1873. */
  1874. if (fl_starving(adapter, fl)) {
  1875. struct sge_eth_rxq *rxq;
  1876. rxq = container_of(fl, struct sge_eth_rxq, fl);
  1877. if (napi_reschedule(&rxq->rspq.napi))
  1878. fl->starving++;
  1879. else
  1880. set_bit(id, s->starving_fl);
  1881. }
  1882. }
  1883. }
  1884. /*
  1885. * Reschedule the next scan for starving Free Lists ...
  1886. */
  1887. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  1888. }
  1889. /**
  1890. * sge_tx_timer_cb - perform periodic maintenance of SGE Tx queues
  1891. * @data: the adapter
  1892. *
  1893. * Runs periodically from a timer to perform maintenance of SGE TX queues.
  1894. *
  1895. * b) Reclaims completed Tx packets for the Ethernet queues. Normally
  1896. * packets are cleaned up by new Tx packets, this timer cleans up packets
  1897. * when no new packets are being submitted. This is essential for pktgen,
  1898. * at least.
  1899. */
  1900. static void sge_tx_timer_cb(unsigned long data)
  1901. {
  1902. struct adapter *adapter = (struct adapter *)data;
  1903. struct sge *s = &adapter->sge;
  1904. unsigned int i, budget;
  1905. budget = MAX_TIMER_TX_RECLAIM;
  1906. i = s->ethtxq_rover;
  1907. do {
  1908. struct sge_eth_txq *txq = &s->ethtxq[i];
  1909. if (reclaimable(&txq->q) && __netif_tx_trylock(txq->txq)) {
  1910. int avail = reclaimable(&txq->q);
  1911. if (avail > budget)
  1912. avail = budget;
  1913. free_tx_desc(adapter, &txq->q, avail, true);
  1914. txq->q.in_use -= avail;
  1915. __netif_tx_unlock(txq->txq);
  1916. budget -= avail;
  1917. if (!budget)
  1918. break;
  1919. }
  1920. i++;
  1921. if (i >= s->ethqsets)
  1922. i = 0;
  1923. } while (i != s->ethtxq_rover);
  1924. s->ethtxq_rover = i;
  1925. /*
  1926. * If we found too many reclaimable packets schedule a timer in the
  1927. * near future to continue where we left off. Otherwise the next timer
  1928. * will be at its normal interval.
  1929. */
  1930. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  1931. }
  1932. /**
  1933. * bar2_address - return the BAR2 address for an SGE Queue's Registers
  1934. * @adapter: the adapter
  1935. * @qid: the SGE Queue ID
  1936. * @qtype: the SGE Queue Type (Egress or Ingress)
  1937. * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
  1938. *
  1939. * Returns the BAR2 address for the SGE Queue Registers associated with
  1940. * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
  1941. * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
  1942. * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
  1943. * Registers are supported (e.g. the Write Combining Doorbell Buffer).
  1944. */
  1945. static void __iomem *bar2_address(struct adapter *adapter,
  1946. unsigned int qid,
  1947. enum t4_bar2_qtype qtype,
  1948. unsigned int *pbar2_qid)
  1949. {
  1950. u64 bar2_qoffset;
  1951. int ret;
  1952. ret = t4_bar2_sge_qregs(adapter, qid, qtype,
  1953. &bar2_qoffset, pbar2_qid);
  1954. if (ret)
  1955. return NULL;
  1956. return adapter->bar2 + bar2_qoffset;
  1957. }
  1958. /**
  1959. * t4vf_sge_alloc_rxq - allocate an SGE RX Queue
  1960. * @adapter: the adapter
  1961. * @rspq: pointer to to the new rxq's Response Queue to be filled in
  1962. * @iqasynch: if 0, a normal rspq; if 1, an asynchronous event queue
  1963. * @dev: the network device associated with the new rspq
  1964. * @intr_dest: MSI-X vector index (overriden in MSI mode)
  1965. * @fl: pointer to the new rxq's Free List to be filled in
  1966. * @hnd: the interrupt handler to invoke for the rspq
  1967. */
  1968. int t4vf_sge_alloc_rxq(struct adapter *adapter, struct sge_rspq *rspq,
  1969. bool iqasynch, struct net_device *dev,
  1970. int intr_dest,
  1971. struct sge_fl *fl, rspq_handler_t hnd)
  1972. {
  1973. struct sge *s = &adapter->sge;
  1974. struct port_info *pi = netdev_priv(dev);
  1975. struct fw_iq_cmd cmd, rpl;
  1976. int ret, iqandst, flsz = 0;
  1977. /*
  1978. * If we're using MSI interrupts and we're not initializing the
  1979. * Forwarded Interrupt Queue itself, then set up this queue for
  1980. * indirect interrupts to the Forwarded Interrupt Queue. Obviously
  1981. * the Forwarded Interrupt Queue must be set up before any other
  1982. * ingress queue ...
  1983. */
  1984. if ((adapter->flags & USING_MSI) && rspq != &adapter->sge.intrq) {
  1985. iqandst = SGE_INTRDST_IQ;
  1986. intr_dest = adapter->sge.intrq.abs_id;
  1987. } else
  1988. iqandst = SGE_INTRDST_PCI;
  1989. /*
  1990. * Allocate the hardware ring for the Response Queue. The size needs
  1991. * to be a multiple of 16 which includes the mandatory status entry
  1992. * (regardless of whether the Status Page capabilities are enabled or
  1993. * not).
  1994. */
  1995. rspq->size = roundup(rspq->size, 16);
  1996. rspq->desc = alloc_ring(adapter->pdev_dev, rspq->size, rspq->iqe_len,
  1997. 0, &rspq->phys_addr, NULL, 0);
  1998. if (!rspq->desc)
  1999. return -ENOMEM;
  2000. /*
  2001. * Fill in the Ingress Queue Command. Note: Ideally this code would
  2002. * be in t4vf_hw.c but there are so many parameters and dependencies
  2003. * on our Linux SGE state that we would end up having to pass tons of
  2004. * parameters. We'll have to think about how this might be migrated
  2005. * into OS-independent common code ...
  2006. */
  2007. memset(&cmd, 0, sizeof(cmd));
  2008. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_IQ_CMD) |
  2009. FW_CMD_REQUEST_F |
  2010. FW_CMD_WRITE_F |
  2011. FW_CMD_EXEC_F);
  2012. cmd.alloc_to_len16 = cpu_to_be32(FW_IQ_CMD_ALLOC_F |
  2013. FW_IQ_CMD_IQSTART_F |
  2014. FW_LEN16(cmd));
  2015. cmd.type_to_iqandstindex =
  2016. cpu_to_be32(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
  2017. FW_IQ_CMD_IQASYNCH_V(iqasynch) |
  2018. FW_IQ_CMD_VIID_V(pi->viid) |
  2019. FW_IQ_CMD_IQANDST_V(iqandst) |
  2020. FW_IQ_CMD_IQANUS_V(1) |
  2021. FW_IQ_CMD_IQANUD_V(SGE_UPDATEDEL_INTR) |
  2022. FW_IQ_CMD_IQANDSTINDEX_V(intr_dest));
  2023. cmd.iqdroprss_to_iqesize =
  2024. cpu_to_be16(FW_IQ_CMD_IQPCIECH_V(pi->port_id) |
  2025. FW_IQ_CMD_IQGTSMODE_F |
  2026. FW_IQ_CMD_IQINTCNTTHRESH_V(rspq->pktcnt_idx) |
  2027. FW_IQ_CMD_IQESIZE_V(ilog2(rspq->iqe_len) - 4));
  2028. cmd.iqsize = cpu_to_be16(rspq->size);
  2029. cmd.iqaddr = cpu_to_be64(rspq->phys_addr);
  2030. if (fl) {
  2031. /*
  2032. * Allocate the ring for the hardware free list (with space
  2033. * for its status page) along with the associated software
  2034. * descriptor ring. The free list size needs to be a multiple
  2035. * of the Egress Queue Unit.
  2036. */
  2037. fl->size = roundup(fl->size, FL_PER_EQ_UNIT);
  2038. fl->desc = alloc_ring(adapter->pdev_dev, fl->size,
  2039. sizeof(__be64), sizeof(struct rx_sw_desc),
  2040. &fl->addr, &fl->sdesc, s->stat_len);
  2041. if (!fl->desc) {
  2042. ret = -ENOMEM;
  2043. goto err;
  2044. }
  2045. /*
  2046. * Calculate the size of the hardware free list ring plus
  2047. * Status Page (which the SGE will place after the end of the
  2048. * free list ring) in Egress Queue Units.
  2049. */
  2050. flsz = (fl->size / FL_PER_EQ_UNIT +
  2051. s->stat_len / EQ_UNIT);
  2052. /*
  2053. * Fill in all the relevant firmware Ingress Queue Command
  2054. * fields for the free list.
  2055. */
  2056. cmd.iqns_to_fl0congen =
  2057. cpu_to_be32(
  2058. FW_IQ_CMD_FL0HOSTFCMODE_V(SGE_HOSTFCMODE_NONE) |
  2059. FW_IQ_CMD_FL0PACKEN_F |
  2060. FW_IQ_CMD_FL0PADEN_F);
  2061. cmd.fl0dcaen_to_fl0cidxfthresh =
  2062. cpu_to_be16(
  2063. FW_IQ_CMD_FL0FBMIN_V(SGE_FETCHBURSTMIN_64B) |
  2064. FW_IQ_CMD_FL0FBMAX_V(SGE_FETCHBURSTMAX_512B));
  2065. cmd.fl0size = cpu_to_be16(flsz);
  2066. cmd.fl0addr = cpu_to_be64(fl->addr);
  2067. }
  2068. /*
  2069. * Issue the firmware Ingress Queue Command and extract the results if
  2070. * it completes successfully.
  2071. */
  2072. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  2073. if (ret)
  2074. goto err;
  2075. netif_napi_add(dev, &rspq->napi, napi_rx_handler, 64);
  2076. rspq->cur_desc = rspq->desc;
  2077. rspq->cidx = 0;
  2078. rspq->gen = 1;
  2079. rspq->next_intr_params = rspq->intr_params;
  2080. rspq->cntxt_id = be16_to_cpu(rpl.iqid);
  2081. rspq->bar2_addr = bar2_address(adapter,
  2082. rspq->cntxt_id,
  2083. T4_BAR2_QTYPE_INGRESS,
  2084. &rspq->bar2_qid);
  2085. rspq->abs_id = be16_to_cpu(rpl.physiqid);
  2086. rspq->size--; /* subtract status entry */
  2087. rspq->adapter = adapter;
  2088. rspq->netdev = dev;
  2089. rspq->handler = hnd;
  2090. /* set offset to -1 to distinguish ingress queues without FL */
  2091. rspq->offset = fl ? 0 : -1;
  2092. if (fl) {
  2093. fl->cntxt_id = be16_to_cpu(rpl.fl0id);
  2094. fl->avail = 0;
  2095. fl->pend_cred = 0;
  2096. fl->pidx = 0;
  2097. fl->cidx = 0;
  2098. fl->alloc_failed = 0;
  2099. fl->large_alloc_failed = 0;
  2100. fl->starving = 0;
  2101. /* Note, we must initialize the BAR2 Free List User Doorbell
  2102. * information before refilling the Free List!
  2103. */
  2104. fl->bar2_addr = bar2_address(adapter,
  2105. fl->cntxt_id,
  2106. T4_BAR2_QTYPE_EGRESS,
  2107. &fl->bar2_qid);
  2108. refill_fl(adapter, fl, fl_cap(fl), GFP_KERNEL);
  2109. }
  2110. return 0;
  2111. err:
  2112. /*
  2113. * An error occurred. Clean up our partial allocation state and
  2114. * return the error.
  2115. */
  2116. if (rspq->desc) {
  2117. dma_free_coherent(adapter->pdev_dev, rspq->size * rspq->iqe_len,
  2118. rspq->desc, rspq->phys_addr);
  2119. rspq->desc = NULL;
  2120. }
  2121. if (fl && fl->desc) {
  2122. kfree(fl->sdesc);
  2123. fl->sdesc = NULL;
  2124. dma_free_coherent(adapter->pdev_dev, flsz * EQ_UNIT,
  2125. fl->desc, fl->addr);
  2126. fl->desc = NULL;
  2127. }
  2128. return ret;
  2129. }
  2130. /**
  2131. * t4vf_sge_alloc_eth_txq - allocate an SGE Ethernet TX Queue
  2132. * @adapter: the adapter
  2133. * @txq: pointer to the new txq to be filled in
  2134. * @devq: the network TX queue associated with the new txq
  2135. * @iqid: the relative ingress queue ID to which events relating to
  2136. * the new txq should be directed
  2137. */
  2138. int t4vf_sge_alloc_eth_txq(struct adapter *adapter, struct sge_eth_txq *txq,
  2139. struct net_device *dev, struct netdev_queue *devq,
  2140. unsigned int iqid)
  2141. {
  2142. struct sge *s = &adapter->sge;
  2143. int ret, nentries;
  2144. struct fw_eq_eth_cmd cmd, rpl;
  2145. struct port_info *pi = netdev_priv(dev);
  2146. /*
  2147. * Calculate the size of the hardware TX Queue (including the Status
  2148. * Page on the end of the TX Queue) in units of TX Descriptors.
  2149. */
  2150. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2151. /*
  2152. * Allocate the hardware ring for the TX ring (with space for its
  2153. * status page) along with the associated software descriptor ring.
  2154. */
  2155. txq->q.desc = alloc_ring(adapter->pdev_dev, txq->q.size,
  2156. sizeof(struct tx_desc),
  2157. sizeof(struct tx_sw_desc),
  2158. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len);
  2159. if (!txq->q.desc)
  2160. return -ENOMEM;
  2161. /*
  2162. * Fill in the Egress Queue Command. Note: As with the direct use of
  2163. * the firmware Ingress Queue COmmand above in our RXQ allocation
  2164. * routine, ideally, this code would be in t4vf_hw.c. Again, we'll
  2165. * have to see if there's some reasonable way to parameterize it
  2166. * into the common code ...
  2167. */
  2168. memset(&cmd, 0, sizeof(cmd));
  2169. cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_EQ_ETH_CMD) |
  2170. FW_CMD_REQUEST_F |
  2171. FW_CMD_WRITE_F |
  2172. FW_CMD_EXEC_F);
  2173. cmd.alloc_to_len16 = cpu_to_be32(FW_EQ_ETH_CMD_ALLOC_F |
  2174. FW_EQ_ETH_CMD_EQSTART_F |
  2175. FW_LEN16(cmd));
  2176. cmd.viid_pkd = cpu_to_be32(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
  2177. FW_EQ_ETH_CMD_VIID_V(pi->viid));
  2178. cmd.fetchszm_to_iqid =
  2179. cpu_to_be32(FW_EQ_ETH_CMD_HOSTFCMODE_V(SGE_HOSTFCMODE_STPG) |
  2180. FW_EQ_ETH_CMD_PCIECHN_V(pi->port_id) |
  2181. FW_EQ_ETH_CMD_IQID_V(iqid));
  2182. cmd.dcaen_to_eqsize =
  2183. cpu_to_be32(FW_EQ_ETH_CMD_FBMIN_V(SGE_FETCHBURSTMIN_64B) |
  2184. FW_EQ_ETH_CMD_FBMAX_V(SGE_FETCHBURSTMAX_512B) |
  2185. FW_EQ_ETH_CMD_CIDXFTHRESH_V(
  2186. SGE_CIDXFLUSHTHRESH_32) |
  2187. FW_EQ_ETH_CMD_EQSIZE_V(nentries));
  2188. cmd.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2189. /*
  2190. * Issue the firmware Egress Queue Command and extract the results if
  2191. * it completes successfully.
  2192. */
  2193. ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
  2194. if (ret) {
  2195. /*
  2196. * The girmware Ingress Queue Command failed for some reason.
  2197. * Free up our partial allocation state and return the error.
  2198. */
  2199. kfree(txq->q.sdesc);
  2200. txq->q.sdesc = NULL;
  2201. dma_free_coherent(adapter->pdev_dev,
  2202. nentries * sizeof(struct tx_desc),
  2203. txq->q.desc, txq->q.phys_addr);
  2204. txq->q.desc = NULL;
  2205. return ret;
  2206. }
  2207. txq->q.in_use = 0;
  2208. txq->q.cidx = 0;
  2209. txq->q.pidx = 0;
  2210. txq->q.stat = (void *)&txq->q.desc[txq->q.size];
  2211. txq->q.cntxt_id = FW_EQ_ETH_CMD_EQID_G(be32_to_cpu(rpl.eqid_pkd));
  2212. txq->q.bar2_addr = bar2_address(adapter,
  2213. txq->q.cntxt_id,
  2214. T4_BAR2_QTYPE_EGRESS,
  2215. &txq->q.bar2_qid);
  2216. txq->q.abs_id =
  2217. FW_EQ_ETH_CMD_PHYSEQID_G(be32_to_cpu(rpl.physeqid_pkd));
  2218. txq->txq = devq;
  2219. txq->tso = 0;
  2220. txq->tx_cso = 0;
  2221. txq->vlan_ins = 0;
  2222. txq->q.stops = 0;
  2223. txq->q.restarts = 0;
  2224. txq->mapping_err = 0;
  2225. return 0;
  2226. }
  2227. /*
  2228. * Free the DMA map resources associated with a TX queue.
  2229. */
  2230. static void free_txq(struct adapter *adapter, struct sge_txq *tq)
  2231. {
  2232. struct sge *s = &adapter->sge;
  2233. dma_free_coherent(adapter->pdev_dev,
  2234. tq->size * sizeof(*tq->desc) + s->stat_len,
  2235. tq->desc, tq->phys_addr);
  2236. tq->cntxt_id = 0;
  2237. tq->sdesc = NULL;
  2238. tq->desc = NULL;
  2239. }
  2240. /*
  2241. * Free the resources associated with a response queue (possibly including a
  2242. * free list).
  2243. */
  2244. static void free_rspq_fl(struct adapter *adapter, struct sge_rspq *rspq,
  2245. struct sge_fl *fl)
  2246. {
  2247. struct sge *s = &adapter->sge;
  2248. unsigned int flid = fl ? fl->cntxt_id : 0xffff;
  2249. t4vf_iq_free(adapter, FW_IQ_TYPE_FL_INT_CAP,
  2250. rspq->cntxt_id, flid, 0xffff);
  2251. dma_free_coherent(adapter->pdev_dev, (rspq->size + 1) * rspq->iqe_len,
  2252. rspq->desc, rspq->phys_addr);
  2253. netif_napi_del(&rspq->napi);
  2254. rspq->netdev = NULL;
  2255. rspq->cntxt_id = 0;
  2256. rspq->abs_id = 0;
  2257. rspq->desc = NULL;
  2258. if (fl) {
  2259. free_rx_bufs(adapter, fl, fl->avail);
  2260. dma_free_coherent(adapter->pdev_dev,
  2261. fl->size * sizeof(*fl->desc) + s->stat_len,
  2262. fl->desc, fl->addr);
  2263. kfree(fl->sdesc);
  2264. fl->sdesc = NULL;
  2265. fl->cntxt_id = 0;
  2266. fl->desc = NULL;
  2267. }
  2268. }
  2269. /**
  2270. * t4vf_free_sge_resources - free SGE resources
  2271. * @adapter: the adapter
  2272. *
  2273. * Frees resources used by the SGE queue sets.
  2274. */
  2275. void t4vf_free_sge_resources(struct adapter *adapter)
  2276. {
  2277. struct sge *s = &adapter->sge;
  2278. struct sge_eth_rxq *rxq = s->ethrxq;
  2279. struct sge_eth_txq *txq = s->ethtxq;
  2280. struct sge_rspq *evtq = &s->fw_evtq;
  2281. struct sge_rspq *intrq = &s->intrq;
  2282. int qs;
  2283. for (qs = 0; qs < adapter->sge.ethqsets; qs++, rxq++, txq++) {
  2284. if (rxq->rspq.desc)
  2285. free_rspq_fl(adapter, &rxq->rspq, &rxq->fl);
  2286. if (txq->q.desc) {
  2287. t4vf_eth_eq_free(adapter, txq->q.cntxt_id);
  2288. free_tx_desc(adapter, &txq->q, txq->q.in_use, true);
  2289. kfree(txq->q.sdesc);
  2290. free_txq(adapter, &txq->q);
  2291. }
  2292. }
  2293. if (evtq->desc)
  2294. free_rspq_fl(adapter, evtq, NULL);
  2295. if (intrq->desc)
  2296. free_rspq_fl(adapter, intrq, NULL);
  2297. }
  2298. /**
  2299. * t4vf_sge_start - enable SGE operation
  2300. * @adapter: the adapter
  2301. *
  2302. * Start tasklets and timers associated with the DMA engine.
  2303. */
  2304. void t4vf_sge_start(struct adapter *adapter)
  2305. {
  2306. adapter->sge.ethtxq_rover = 0;
  2307. mod_timer(&adapter->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2308. mod_timer(&adapter->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2309. }
  2310. /**
  2311. * t4vf_sge_stop - disable SGE operation
  2312. * @adapter: the adapter
  2313. *
  2314. * Stop tasklets and timers associated with the DMA engine. Note that
  2315. * this is effective only if measures have been taken to disable any HW
  2316. * events that may restart them.
  2317. */
  2318. void t4vf_sge_stop(struct adapter *adapter)
  2319. {
  2320. struct sge *s = &adapter->sge;
  2321. if (s->rx_timer.function)
  2322. del_timer_sync(&s->rx_timer);
  2323. if (s->tx_timer.function)
  2324. del_timer_sync(&s->tx_timer);
  2325. }
  2326. /**
  2327. * t4vf_sge_init - initialize SGE
  2328. * @adapter: the adapter
  2329. *
  2330. * Performs SGE initialization needed every time after a chip reset.
  2331. * We do not initialize any of the queue sets here, instead the driver
  2332. * top-level must request those individually. We also do not enable DMA
  2333. * here, that should be done after the queues have been set up.
  2334. */
  2335. int t4vf_sge_init(struct adapter *adapter)
  2336. {
  2337. struct sge_params *sge_params = &adapter->params.sge;
  2338. u32 fl0 = sge_params->sge_fl_buffer_size[0];
  2339. u32 fl1 = sge_params->sge_fl_buffer_size[1];
  2340. struct sge *s = &adapter->sge;
  2341. unsigned int ingpadboundary, ingpackboundary;
  2342. /*
  2343. * Start by vetting the basic SGE parameters which have been set up by
  2344. * the Physical Function Driver. Ideally we should be able to deal
  2345. * with _any_ configuration. Practice is different ...
  2346. */
  2347. if (fl0 != PAGE_SIZE || (fl1 != 0 && fl1 <= fl0)) {
  2348. dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n",
  2349. fl0, fl1);
  2350. return -EINVAL;
  2351. }
  2352. if ((sge_params->sge_control & RXPKTCPLMODE_F) == 0) {
  2353. dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n");
  2354. return -EINVAL;
  2355. }
  2356. /*
  2357. * Now translate the adapter parameters into our internal forms.
  2358. */
  2359. if (fl1)
  2360. s->fl_pg_order = ilog2(fl1) - PAGE_SHIFT;
  2361. s->stat_len = ((sge_params->sge_control & EGRSTATUSPAGESIZE_F)
  2362. ? 128 : 64);
  2363. s->pktshift = PKTSHIFT_G(sge_params->sge_control);
  2364. /* T4 uses a single control field to specify both the PCIe Padding and
  2365. * Packing Boundary. T5 introduced the ability to specify these
  2366. * separately. The actual Ingress Packet Data alignment boundary
  2367. * within Packed Buffer Mode is the maximum of these two
  2368. * specifications. (Note that it makes no real practical sense to
  2369. * have the Pading Boudary be larger than the Packing Boundary but you
  2370. * could set the chip up that way and, in fact, legacy T4 code would
  2371. * end doing this because it would initialize the Padding Boundary and
  2372. * leave the Packing Boundary initialized to 0 (16 bytes).)
  2373. */
  2374. ingpadboundary = 1 << (INGPADBOUNDARY_G(sge_params->sge_control) +
  2375. INGPADBOUNDARY_SHIFT_X);
  2376. if (is_t4(adapter->params.chip)) {
  2377. s->fl_align = ingpadboundary;
  2378. } else {
  2379. /* T5 has a different interpretation of one of the PCIe Packing
  2380. * Boundary values.
  2381. */
  2382. ingpackboundary = INGPACKBOUNDARY_G(sge_params->sge_control2);
  2383. if (ingpackboundary == INGPACKBOUNDARY_16B_X)
  2384. ingpackboundary = 16;
  2385. else
  2386. ingpackboundary = 1 << (ingpackboundary +
  2387. INGPACKBOUNDARY_SHIFT_X);
  2388. s->fl_align = max(ingpadboundary, ingpackboundary);
  2389. }
  2390. /* A FL with <= fl_starve_thres buffers is starving and a periodic
  2391. * timer will attempt to refill it. This needs to be larger than the
  2392. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  2393. * stuck waiting for new packets while the SGE is waiting for us to
  2394. * give it more Free List entries. (Note that the SGE's Egress
  2395. * Congestion Threshold is in units of 2 Free List pointers.)
  2396. */
  2397. s->fl_starve_thres
  2398. = EGRTHRESHOLD_G(sge_params->sge_congestion_control)*2 + 1;
  2399. /*
  2400. * Set up tasklet timers.
  2401. */
  2402. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adapter);
  2403. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adapter);
  2404. /*
  2405. * Initialize Forwarded Interrupt Queue lock.
  2406. */
  2407. spin_lock_init(&s->intrq_lock);
  2408. return 0;
  2409. }