sge.c 74 KB

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