sge.c 72 KB

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