sge.c 72 KB

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