sge.c 71 KB

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