sge.c 71 KB

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