sge.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/ip.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/prefetch.h>
  42. #include <linux/export.h>
  43. #include <net/ipv6.h>
  44. #include <net/tcp.h>
  45. #include "cxgb4.h"
  46. #include "t4_regs.h"
  47. #include "t4_msg.h"
  48. #include "t4fw_api.h"
  49. /*
  50. * Rx buffer size. We use largish buffers if possible but settle for single
  51. * pages under memory shortage.
  52. */
  53. #if PAGE_SHIFT >= 16
  54. # define FL_PG_ORDER 0
  55. #else
  56. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  57. #endif
  58. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  59. #define RX_COPY_THRES 256
  60. #define RX_PULL_LEN 128
  61. /*
  62. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  63. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  64. */
  65. #define RX_PKT_SKB_LEN 512
  66. /*
  67. * Max number of Tx descriptors we clean up at a time. Should be modest as
  68. * freeing skbs isn't cheap and it happens while holding locks. We just need
  69. * to free packets faster than they arrive, we eventually catch up and keep
  70. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
  71. */
  72. #define MAX_TX_RECLAIM 16
  73. /*
  74. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  75. * allocating buffers isn't cheap either.
  76. */
  77. #define MAX_RX_REFILL 16U
  78. /*
  79. * Period of the Rx queue check timer. This timer is infrequent as it has
  80. * something to do only when the system experiences severe memory shortage.
  81. */
  82. #define RX_QCHECK_PERIOD (HZ / 2)
  83. /*
  84. * Period of the Tx queue check timer.
  85. */
  86. #define TX_QCHECK_PERIOD (HZ / 2)
  87. /*
  88. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  89. */
  90. #define MAX_TIMER_TX_RECLAIM 100
  91. /*
  92. * Timer index used when backing off due to memory shortage.
  93. */
  94. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  95. /*
  96. * An FL with <= FL_STARVE_THRES buffers is starving and a periodic timer will
  97. * attempt to refill it.
  98. */
  99. #define FL_STARVE_THRES 4
  100. /*
  101. * Suspend an Ethernet Tx queue with fewer available descriptors than this.
  102. * This is the same as calc_tx_descs() for a TSO packet with
  103. * nr_frags == MAX_SKB_FRAGS.
  104. */
  105. #define ETHTXQ_STOP_THRES \
  106. (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
  107. /*
  108. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  109. * for a full sized WR.
  110. */
  111. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  112. /*
  113. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  114. * into a WR.
  115. */
  116. #define MAX_IMM_TX_PKT_LEN 128
  117. /*
  118. * Max size of a WR sent through a control Tx queue.
  119. */
  120. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  121. struct tx_sw_desc { /* SW state per Tx descriptor */
  122. struct sk_buff *skb;
  123. struct ulptx_sgl *sgl;
  124. };
  125. struct rx_sw_desc { /* SW state per Rx descriptor */
  126. struct page *page;
  127. dma_addr_t dma_addr;
  128. };
  129. /*
  130. * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
  131. * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
  132. * We could easily support more but there doesn't seem to be much need for
  133. * that ...
  134. */
  135. #define FL_MTU_SMALL 1500
  136. #define FL_MTU_LARGE 9000
  137. static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
  138. unsigned int mtu)
  139. {
  140. struct sge *s = &adapter->sge;
  141. return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
  142. }
  143. #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
  144. #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
  145. /*
  146. * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
  147. * these to specify the buffer size as an index into the SGE Free List Buffer
  148. * Size register array. We also use bit 4, when the buffer has been unmapped
  149. * for DMA, but this is of course never sent to the hardware and is only used
  150. * to prevent double unmappings. All of the above requires that the Free List
  151. * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
  152. * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
  153. * Free List Buffer alignment is 32 bytes, this works out for us ...
  154. */
  155. enum {
  156. RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
  157. RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
  158. RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
  159. /*
  160. * XXX We shouldn't depend on being able to use these indices.
  161. * XXX Especially when some other Master PF has initialized the
  162. * XXX adapter or we use the Firmware Configuration File. We
  163. * XXX should really search through the Host Buffer Size register
  164. * XXX array for the appropriately sized buffer indices.
  165. */
  166. RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
  167. RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
  168. RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
  169. RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
  170. };
  171. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  172. {
  173. return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
  174. }
  175. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  176. {
  177. return !(d->dma_addr & RX_UNMAPPED_BUF);
  178. }
  179. /**
  180. * txq_avail - return the number of available slots in a Tx queue
  181. * @q: the Tx queue
  182. *
  183. * Returns the number of descriptors in a Tx queue available to write new
  184. * packets.
  185. */
  186. static inline unsigned int txq_avail(const struct sge_txq *q)
  187. {
  188. return q->size - 1 - q->in_use;
  189. }
  190. /**
  191. * fl_cap - return the capacity of a free-buffer list
  192. * @fl: the FL
  193. *
  194. * Returns the capacity of a free-buffer list. The capacity is less than
  195. * the size because one descriptor needs to be left unpopulated, otherwise
  196. * HW will think the FL is empty.
  197. */
  198. static inline unsigned int fl_cap(const struct sge_fl *fl)
  199. {
  200. return fl->size - 8; /* 1 descriptor = 8 buffers */
  201. }
  202. static inline bool fl_starving(const struct sge_fl *fl)
  203. {
  204. return fl->avail - fl->pend_cred <= FL_STARVE_THRES;
  205. }
  206. static int map_skb(struct device *dev, const struct sk_buff *skb,
  207. dma_addr_t *addr)
  208. {
  209. const skb_frag_t *fp, *end;
  210. const struct skb_shared_info *si;
  211. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  212. if (dma_mapping_error(dev, *addr))
  213. goto out_err;
  214. si = skb_shinfo(skb);
  215. end = &si->frags[si->nr_frags];
  216. for (fp = si->frags; fp < end; fp++) {
  217. *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
  218. DMA_TO_DEVICE);
  219. if (dma_mapping_error(dev, *addr))
  220. goto unwind;
  221. }
  222. return 0;
  223. unwind:
  224. while (fp-- > si->frags)
  225. dma_unmap_page(dev, *--addr, skb_frag_size(fp), DMA_TO_DEVICE);
  226. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  227. out_err:
  228. return -ENOMEM;
  229. }
  230. #ifdef CONFIG_NEED_DMA_MAP_STATE
  231. static void unmap_skb(struct device *dev, const struct sk_buff *skb,
  232. const dma_addr_t *addr)
  233. {
  234. const skb_frag_t *fp, *end;
  235. const struct skb_shared_info *si;
  236. dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
  237. si = skb_shinfo(skb);
  238. end = &si->frags[si->nr_frags];
  239. for (fp = si->frags; fp < end; fp++)
  240. dma_unmap_page(dev, *addr++, skb_frag_size(fp), DMA_TO_DEVICE);
  241. }
  242. /**
  243. * deferred_unmap_destructor - unmap a packet when it is freed
  244. * @skb: the packet
  245. *
  246. * This is the packet destructor used for Tx packets that need to remain
  247. * mapped until they are freed rather than until their Tx descriptors are
  248. * freed.
  249. */
  250. static void deferred_unmap_destructor(struct sk_buff *skb)
  251. {
  252. unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
  253. }
  254. #endif
  255. static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
  256. const struct ulptx_sgl *sgl, const struct sge_txq *q)
  257. {
  258. const struct ulptx_sge_pair *p;
  259. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  260. if (likely(skb_headlen(skb)))
  261. dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  262. DMA_TO_DEVICE);
  263. else {
  264. dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  265. DMA_TO_DEVICE);
  266. nfrags--;
  267. }
  268. /*
  269. * the complexity below is because of the possibility of a wrap-around
  270. * in the middle of an SGL
  271. */
  272. for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
  273. if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
  274. unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  275. ntohl(p->len[0]), DMA_TO_DEVICE);
  276. dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
  277. ntohl(p->len[1]), DMA_TO_DEVICE);
  278. p++;
  279. } else if ((u8 *)p == (u8 *)q->stat) {
  280. p = (const struct ulptx_sge_pair *)q->desc;
  281. goto unmap;
  282. } else if ((u8 *)p + 8 == (u8 *)q->stat) {
  283. const __be64 *addr = (const __be64 *)q->desc;
  284. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  285. ntohl(p->len[0]), DMA_TO_DEVICE);
  286. dma_unmap_page(dev, be64_to_cpu(addr[1]),
  287. ntohl(p->len[1]), DMA_TO_DEVICE);
  288. p = (const struct ulptx_sge_pair *)&addr[2];
  289. } else {
  290. const __be64 *addr = (const __be64 *)q->desc;
  291. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  292. ntohl(p->len[0]), DMA_TO_DEVICE);
  293. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  294. ntohl(p->len[1]), DMA_TO_DEVICE);
  295. p = (const struct ulptx_sge_pair *)&addr[1];
  296. }
  297. }
  298. if (nfrags) {
  299. __be64 addr;
  300. if ((u8 *)p == (u8 *)q->stat)
  301. p = (const struct ulptx_sge_pair *)q->desc;
  302. addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
  303. *(const __be64 *)q->desc;
  304. dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
  305. DMA_TO_DEVICE);
  306. }
  307. }
  308. /**
  309. * free_tx_desc - reclaims Tx descriptors and their buffers
  310. * @adapter: the adapter
  311. * @q: the Tx queue to reclaim descriptors from
  312. * @n: the number of descriptors to reclaim
  313. * @unmap: whether the buffers should be unmapped for DMA
  314. *
  315. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  316. * Tx buffers. Called with the Tx queue lock held.
  317. */
  318. static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
  319. unsigned int n, bool unmap)
  320. {
  321. struct tx_sw_desc *d;
  322. unsigned int cidx = q->cidx;
  323. struct device *dev = adap->pdev_dev;
  324. d = &q->sdesc[cidx];
  325. while (n--) {
  326. if (d->skb) { /* an SGL is present */
  327. if (unmap)
  328. unmap_sgl(dev, d->skb, d->sgl, q);
  329. kfree_skb(d->skb);
  330. d->skb = NULL;
  331. }
  332. ++d;
  333. if (++cidx == q->size) {
  334. cidx = 0;
  335. d = q->sdesc;
  336. }
  337. }
  338. q->cidx = cidx;
  339. }
  340. /*
  341. * Return the number of reclaimable descriptors in a Tx queue.
  342. */
  343. static inline int reclaimable(const struct sge_txq *q)
  344. {
  345. int hw_cidx = ntohs(q->stat->cidx);
  346. hw_cidx -= q->cidx;
  347. return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
  348. }
  349. /**
  350. * reclaim_completed_tx - reclaims completed Tx descriptors
  351. * @adap: the adapter
  352. * @q: the Tx queue to reclaim completed descriptors from
  353. * @unmap: whether the buffers should be unmapped for DMA
  354. *
  355. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  356. * and frees the associated buffers if possible. Called with the Tx
  357. * queue locked.
  358. */
  359. static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  360. bool unmap)
  361. {
  362. int avail = reclaimable(q);
  363. if (avail) {
  364. /*
  365. * Limit the amount of clean up work we do at a time to keep
  366. * the Tx lock hold time O(1).
  367. */
  368. if (avail > MAX_TX_RECLAIM)
  369. avail = MAX_TX_RECLAIM;
  370. free_tx_desc(adap, q, avail, unmap);
  371. q->in_use -= avail;
  372. }
  373. }
  374. static inline int get_buf_size(struct adapter *adapter,
  375. const struct rx_sw_desc *d)
  376. {
  377. struct sge *s = &adapter->sge;
  378. unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
  379. int buf_size;
  380. switch (rx_buf_size_idx) {
  381. case RX_SMALL_PG_BUF:
  382. buf_size = PAGE_SIZE;
  383. break;
  384. case RX_LARGE_PG_BUF:
  385. buf_size = PAGE_SIZE << s->fl_pg_order;
  386. break;
  387. case RX_SMALL_MTU_BUF:
  388. buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
  389. break;
  390. case RX_LARGE_MTU_BUF:
  391. buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
  392. break;
  393. default:
  394. BUG_ON(1);
  395. }
  396. return buf_size;
  397. }
  398. /**
  399. * free_rx_bufs - free the Rx buffers on an SGE free list
  400. * @adap: the adapter
  401. * @q: the SGE free list to free buffers from
  402. * @n: how many buffers to free
  403. *
  404. * Release the next @n buffers on an SGE free-buffer Rx queue. The
  405. * buffers must be made inaccessible to HW before calling this function.
  406. */
  407. static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
  408. {
  409. while (n--) {
  410. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  411. if (is_buf_mapped(d))
  412. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  413. get_buf_size(adap, d),
  414. PCI_DMA_FROMDEVICE);
  415. put_page(d->page);
  416. d->page = NULL;
  417. if (++q->cidx == q->size)
  418. q->cidx = 0;
  419. q->avail--;
  420. }
  421. }
  422. /**
  423. * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
  424. * @adap: the adapter
  425. * @q: the SGE free list
  426. *
  427. * Unmap the current buffer on an SGE free-buffer Rx queue. The
  428. * buffer must be made inaccessible to HW before calling this function.
  429. *
  430. * This is similar to @free_rx_bufs above but does not free the buffer.
  431. * Do note that the FL still loses any further access to the buffer.
  432. */
  433. static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
  434. {
  435. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  436. if (is_buf_mapped(d))
  437. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  438. get_buf_size(adap, d), PCI_DMA_FROMDEVICE);
  439. d->page = NULL;
  440. if (++q->cidx == q->size)
  441. q->cidx = 0;
  442. q->avail--;
  443. }
  444. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  445. {
  446. u32 val;
  447. if (q->pend_cred >= 8) {
  448. val = PIDX(q->pend_cred / 8);
  449. if (!is_t4(adap->params.chip))
  450. val |= DBTYPE(1);
  451. wmb();
  452. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL), DBPRIO(1) |
  453. QID(q->cntxt_id) | val);
  454. q->pend_cred &= 7;
  455. }
  456. }
  457. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  458. dma_addr_t mapping)
  459. {
  460. sd->page = pg;
  461. sd->dma_addr = mapping; /* includes size low bits */
  462. }
  463. /**
  464. * refill_fl - refill an SGE Rx buffer ring
  465. * @adap: the adapter
  466. * @q: the ring to refill
  467. * @n: the number of new buffers to allocate
  468. * @gfp: the gfp flags for the allocations
  469. *
  470. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  471. * allocated with the supplied gfp flags. The caller must assure that
  472. * @n does not exceed the queue's capacity. If afterwards the queue is
  473. * found critically low mark it as starving in the bitmap of starving FLs.
  474. *
  475. * Returns the number of buffers allocated.
  476. */
  477. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  478. gfp_t gfp)
  479. {
  480. struct sge *s = &adap->sge;
  481. struct page *pg;
  482. dma_addr_t mapping;
  483. unsigned int cred = q->avail;
  484. __be64 *d = &q->desc[q->pidx];
  485. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  486. gfp |= __GFP_NOWARN | __GFP_COLD;
  487. if (s->fl_pg_order == 0)
  488. goto alloc_small_pages;
  489. /*
  490. * Prefer large buffers
  491. */
  492. while (n) {
  493. pg = alloc_pages(gfp | __GFP_COMP, s->fl_pg_order);
  494. if (unlikely(!pg)) {
  495. q->large_alloc_failed++;
  496. break; /* fall back to single pages */
  497. }
  498. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  499. PAGE_SIZE << s->fl_pg_order,
  500. PCI_DMA_FROMDEVICE);
  501. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  502. __free_pages(pg, s->fl_pg_order);
  503. goto out; /* do not try small pages for this error */
  504. }
  505. mapping |= RX_LARGE_PG_BUF;
  506. *d++ = cpu_to_be64(mapping);
  507. set_rx_sw_desc(sd, pg, mapping);
  508. sd++;
  509. q->avail++;
  510. if (++q->pidx == q->size) {
  511. q->pidx = 0;
  512. sd = q->sdesc;
  513. d = q->desc;
  514. }
  515. n--;
  516. }
  517. alloc_small_pages:
  518. while (n--) {
  519. pg = __skb_alloc_page(gfp, NULL);
  520. if (unlikely(!pg)) {
  521. q->alloc_failed++;
  522. break;
  523. }
  524. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  525. PCI_DMA_FROMDEVICE);
  526. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  527. put_page(pg);
  528. goto out;
  529. }
  530. *d++ = cpu_to_be64(mapping);
  531. set_rx_sw_desc(sd, pg, mapping);
  532. sd++;
  533. q->avail++;
  534. if (++q->pidx == q->size) {
  535. q->pidx = 0;
  536. sd = q->sdesc;
  537. d = q->desc;
  538. }
  539. }
  540. out: cred = q->avail - cred;
  541. q->pend_cred += cred;
  542. ring_fl_db(adap, q);
  543. if (unlikely(fl_starving(q))) {
  544. smp_wmb();
  545. set_bit(q->cntxt_id - adap->sge.egr_start,
  546. adap->sge.starving_fl);
  547. }
  548. return cred;
  549. }
  550. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  551. {
  552. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  553. GFP_ATOMIC);
  554. }
  555. /**
  556. * alloc_ring - allocate resources for an SGE descriptor ring
  557. * @dev: the PCI device's core device
  558. * @nelem: the number of descriptors
  559. * @elem_size: the size of each descriptor
  560. * @sw_size: the size of the SW state associated with each ring element
  561. * @phys: the physical address of the allocated ring
  562. * @metadata: address of the array holding the SW state for the ring
  563. * @stat_size: extra space in HW ring for status information
  564. * @node: preferred node for memory allocations
  565. *
  566. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  567. * free buffer lists, or response queues. Each SGE ring requires
  568. * space for its HW descriptors plus, optionally, space for the SW state
  569. * associated with each HW entry (the metadata). The function returns
  570. * three values: the virtual address for the HW ring (the return value
  571. * of the function), the bus address of the HW ring, and the address
  572. * of the SW ring.
  573. */
  574. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  575. size_t sw_size, dma_addr_t *phys, void *metadata,
  576. size_t stat_size, int node)
  577. {
  578. size_t len = nelem * elem_size + stat_size;
  579. void *s = NULL;
  580. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  581. if (!p)
  582. return NULL;
  583. if (sw_size) {
  584. s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
  585. if (!s) {
  586. dma_free_coherent(dev, len, p, *phys);
  587. return NULL;
  588. }
  589. }
  590. if (metadata)
  591. *(void **)metadata = s;
  592. memset(p, 0, len);
  593. return p;
  594. }
  595. /**
  596. * sgl_len - calculates the size of an SGL of the given capacity
  597. * @n: the number of SGL entries
  598. *
  599. * Calculates the number of flits needed for a scatter/gather list that
  600. * can hold the given number of entries.
  601. */
  602. static inline unsigned int sgl_len(unsigned int n)
  603. {
  604. n--;
  605. return (3 * n) / 2 + (n & 1) + 2;
  606. }
  607. /**
  608. * flits_to_desc - returns the num of Tx descriptors for the given flits
  609. * @n: the number of flits
  610. *
  611. * Returns the number of Tx descriptors needed for the supplied number
  612. * of flits.
  613. */
  614. static inline unsigned int flits_to_desc(unsigned int n)
  615. {
  616. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  617. return DIV_ROUND_UP(n, 8);
  618. }
  619. /**
  620. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  621. * @skb: the packet
  622. *
  623. * Returns whether an Ethernet packet is small enough to fit as
  624. * immediate data.
  625. */
  626. static inline int is_eth_imm(const struct sk_buff *skb)
  627. {
  628. return skb->len <= MAX_IMM_TX_PKT_LEN - sizeof(struct cpl_tx_pkt);
  629. }
  630. /**
  631. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  632. * @skb: the packet
  633. *
  634. * Returns the number of flits needed for a Tx WR for the given Ethernet
  635. * packet, including the needed WR and CPL headers.
  636. */
  637. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  638. {
  639. unsigned int flits;
  640. if (is_eth_imm(skb))
  641. return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt), 8);
  642. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
  643. if (skb_shinfo(skb)->gso_size)
  644. flits += 2;
  645. return flits;
  646. }
  647. /**
  648. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  649. * @skb: the packet
  650. *
  651. * Returns the number of Tx descriptors needed for the given Ethernet
  652. * packet, including the needed WR and CPL headers.
  653. */
  654. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  655. {
  656. return flits_to_desc(calc_tx_flits(skb));
  657. }
  658. /**
  659. * write_sgl - populate a scatter/gather list for a packet
  660. * @skb: the packet
  661. * @q: the Tx queue we are writing into
  662. * @sgl: starting location for writing the SGL
  663. * @end: points right after the end of the SGL
  664. * @start: start offset into skb main-body data to include in the SGL
  665. * @addr: the list of bus addresses for the SGL elements
  666. *
  667. * Generates a gather list for the buffers that make up a packet.
  668. * The caller must provide adequate space for the SGL that will be written.
  669. * The SGL includes all of the packet's page fragments and the data in its
  670. * main body except for the first @start bytes. @sgl must be 16-byte
  671. * aligned and within a Tx descriptor with available space. @end points
  672. * right after the end of the SGL but does not account for any potential
  673. * wrap around, i.e., @end > @sgl.
  674. */
  675. static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  676. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  677. const dma_addr_t *addr)
  678. {
  679. unsigned int i, len;
  680. struct ulptx_sge_pair *to;
  681. const struct skb_shared_info *si = skb_shinfo(skb);
  682. unsigned int nfrags = si->nr_frags;
  683. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  684. len = skb_headlen(skb) - start;
  685. if (likely(len)) {
  686. sgl->len0 = htonl(len);
  687. sgl->addr0 = cpu_to_be64(addr[0] + start);
  688. nfrags++;
  689. } else {
  690. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  691. sgl->addr0 = cpu_to_be64(addr[1]);
  692. }
  693. sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
  694. if (likely(--nfrags == 0))
  695. return;
  696. /*
  697. * Most of the complexity below deals with the possibility we hit the
  698. * end of the queue in the middle of writing the SGL. For this case
  699. * only we create the SGL in a temporary buffer and then copy it.
  700. */
  701. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  702. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  703. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  704. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  705. to->addr[0] = cpu_to_be64(addr[i]);
  706. to->addr[1] = cpu_to_be64(addr[++i]);
  707. }
  708. if (nfrags) {
  709. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  710. to->len[1] = cpu_to_be32(0);
  711. to->addr[0] = cpu_to_be64(addr[i + 1]);
  712. }
  713. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  714. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  715. if (likely(part0))
  716. memcpy(sgl->sge, buf, part0);
  717. part1 = (u8 *)end - (u8 *)q->stat;
  718. memcpy(q->desc, (u8 *)buf + part0, part1);
  719. end = (void *)q->desc + part1;
  720. }
  721. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  722. *end = 0;
  723. }
  724. /* This function copies 64 byte coalesced work request to
  725. * memory mapped BAR2 space(user space writes).
  726. * For coalesced WR SGE, fetches data from the FIFO instead of from Host.
  727. */
  728. static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
  729. {
  730. int count = 8;
  731. while (count) {
  732. writeq(*src, dst);
  733. src++;
  734. dst++;
  735. count--;
  736. }
  737. }
  738. /**
  739. * ring_tx_db - check and potentially ring a Tx queue's doorbell
  740. * @adap: the adapter
  741. * @q: the Tx queue
  742. * @n: number of new descriptors to give to HW
  743. *
  744. * Ring the doorbel for a Tx queue.
  745. */
  746. static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  747. {
  748. unsigned int *wr, index;
  749. wmb(); /* write descriptors before telling HW */
  750. spin_lock(&q->db_lock);
  751. if (!q->db_disabled) {
  752. if (is_t4(adap->params.chip)) {
  753. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
  754. QID(q->cntxt_id) | PIDX(n));
  755. } else {
  756. if (n == 1) {
  757. index = q->pidx ? (q->pidx - 1) : (q->size - 1);
  758. wr = (unsigned int *)&q->desc[index];
  759. cxgb_pio_copy((u64 __iomem *)
  760. (adap->bar2 + q->udb + 64),
  761. (u64 *)wr);
  762. } else
  763. writel(n, adap->bar2 + q->udb + 8);
  764. wmb();
  765. }
  766. }
  767. q->db_pidx = q->pidx;
  768. spin_unlock(&q->db_lock);
  769. }
  770. /**
  771. * inline_tx_skb - inline a packet's data into Tx descriptors
  772. * @skb: the packet
  773. * @q: the Tx queue where the packet will be inlined
  774. * @pos: starting position in the Tx queue where to inline the packet
  775. *
  776. * Inline a packet's contents directly into Tx descriptors, starting at
  777. * the given position within the Tx DMA ring.
  778. * Most of the complexity of this operation is dealing with wrap arounds
  779. * in the middle of the packet we want to inline.
  780. */
  781. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
  782. void *pos)
  783. {
  784. u64 *p;
  785. int left = (void *)q->stat - pos;
  786. if (likely(skb->len <= left)) {
  787. if (likely(!skb->data_len))
  788. skb_copy_from_linear_data(skb, pos, skb->len);
  789. else
  790. skb_copy_bits(skb, 0, pos, skb->len);
  791. pos += skb->len;
  792. } else {
  793. skb_copy_bits(skb, 0, pos, left);
  794. skb_copy_bits(skb, left, q->desc, skb->len - left);
  795. pos = (void *)q->desc + (skb->len - left);
  796. }
  797. /* 0-pad to multiple of 16 */
  798. p = PTR_ALIGN(pos, 8);
  799. if ((uintptr_t)p & 8)
  800. *p = 0;
  801. }
  802. /*
  803. * Figure out what HW csum a packet wants and return the appropriate control
  804. * bits.
  805. */
  806. static u64 hwcsum(const struct sk_buff *skb)
  807. {
  808. int csum_type;
  809. const struct iphdr *iph = ip_hdr(skb);
  810. if (iph->version == 4) {
  811. if (iph->protocol == IPPROTO_TCP)
  812. csum_type = TX_CSUM_TCPIP;
  813. else if (iph->protocol == IPPROTO_UDP)
  814. csum_type = TX_CSUM_UDPIP;
  815. else {
  816. nocsum: /*
  817. * unknown protocol, disable HW csum
  818. * and hope a bad packet is detected
  819. */
  820. return TXPKT_L4CSUM_DIS;
  821. }
  822. } else {
  823. /*
  824. * this doesn't work with extension headers
  825. */
  826. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  827. if (ip6h->nexthdr == IPPROTO_TCP)
  828. csum_type = TX_CSUM_TCPIP6;
  829. else if (ip6h->nexthdr == IPPROTO_UDP)
  830. csum_type = TX_CSUM_UDPIP6;
  831. else
  832. goto nocsum;
  833. }
  834. if (likely(csum_type >= TX_CSUM_TCPIP))
  835. return TXPKT_CSUM_TYPE(csum_type) |
  836. TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
  837. TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
  838. else {
  839. int start = skb_transport_offset(skb);
  840. return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
  841. TXPKT_CSUM_LOC(start + skb->csum_offset);
  842. }
  843. }
  844. static void eth_txq_stop(struct sge_eth_txq *q)
  845. {
  846. netif_tx_stop_queue(q->txq);
  847. q->q.stops++;
  848. }
  849. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  850. {
  851. q->in_use += n;
  852. q->pidx += n;
  853. if (q->pidx >= q->size)
  854. q->pidx -= q->size;
  855. }
  856. /**
  857. * t4_eth_xmit - add a packet to an Ethernet Tx queue
  858. * @skb: the packet
  859. * @dev: the egress net device
  860. *
  861. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  862. */
  863. netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  864. {
  865. u32 wr_mid;
  866. u64 cntrl, *end;
  867. int qidx, credits;
  868. unsigned int flits, ndesc;
  869. struct adapter *adap;
  870. struct sge_eth_txq *q;
  871. const struct port_info *pi;
  872. struct fw_eth_tx_pkt_wr *wr;
  873. struct cpl_tx_pkt_core *cpl;
  874. const struct skb_shared_info *ssi;
  875. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  876. /*
  877. * The chip min packet length is 10 octets but play safe and reject
  878. * anything shorter than an Ethernet header.
  879. */
  880. if (unlikely(skb->len < ETH_HLEN)) {
  881. out_free: dev_kfree_skb(skb);
  882. return NETDEV_TX_OK;
  883. }
  884. pi = netdev_priv(dev);
  885. adap = pi->adapter;
  886. qidx = skb_get_queue_mapping(skb);
  887. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  888. reclaim_completed_tx(adap, &q->q, true);
  889. flits = calc_tx_flits(skb);
  890. ndesc = flits_to_desc(flits);
  891. credits = txq_avail(&q->q) - ndesc;
  892. if (unlikely(credits < 0)) {
  893. eth_txq_stop(q);
  894. dev_err(adap->pdev_dev,
  895. "%s: Tx ring %u full while queue awake!\n",
  896. dev->name, qidx);
  897. return NETDEV_TX_BUSY;
  898. }
  899. if (!is_eth_imm(skb) &&
  900. unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
  901. q->mapping_err++;
  902. goto out_free;
  903. }
  904. wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
  905. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  906. eth_txq_stop(q);
  907. wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
  908. }
  909. wr = (void *)&q->q.desc[q->q.pidx];
  910. wr->equiq_to_len16 = htonl(wr_mid);
  911. wr->r3 = cpu_to_be64(0);
  912. end = (u64 *)wr + flits;
  913. ssi = skb_shinfo(skb);
  914. if (ssi->gso_size) {
  915. struct cpl_tx_pkt_lso *lso = (void *)wr;
  916. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  917. int l3hdr_len = skb_network_header_len(skb);
  918. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  919. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  920. FW_WR_IMMDLEN(sizeof(*lso)));
  921. lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
  922. LSO_FIRST_SLICE | LSO_LAST_SLICE |
  923. LSO_IPV6(v6) |
  924. LSO_ETHHDR_LEN(eth_xtra_len / 4) |
  925. LSO_IPHDR_LEN(l3hdr_len / 4) |
  926. LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
  927. lso->c.ipid_ofst = htons(0);
  928. lso->c.mss = htons(ssi->gso_size);
  929. lso->c.seqno_offset = htonl(0);
  930. lso->c.len = htonl(skb->len);
  931. cpl = (void *)(lso + 1);
  932. cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  933. TXPKT_IPHDR_LEN(l3hdr_len) |
  934. TXPKT_ETHHDR_LEN(eth_xtra_len);
  935. q->tso++;
  936. q->tx_cso += ssi->gso_segs;
  937. } else {
  938. int len;
  939. len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
  940. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  941. FW_WR_IMMDLEN(len));
  942. cpl = (void *)(wr + 1);
  943. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  944. cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
  945. q->tx_cso++;
  946. } else
  947. cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
  948. }
  949. if (vlan_tx_tag_present(skb)) {
  950. q->vlan_ins++;
  951. cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
  952. }
  953. cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
  954. TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
  955. cpl->pack = htons(0);
  956. cpl->len = htons(skb->len);
  957. cpl->ctrl1 = cpu_to_be64(cntrl);
  958. if (is_eth_imm(skb)) {
  959. inline_tx_skb(skb, &q->q, cpl + 1);
  960. dev_kfree_skb(skb);
  961. } else {
  962. int last_desc;
  963. write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
  964. addr);
  965. skb_orphan(skb);
  966. last_desc = q->q.pidx + ndesc - 1;
  967. if (last_desc >= q->q.size)
  968. last_desc -= q->q.size;
  969. q->q.sdesc[last_desc].skb = skb;
  970. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
  971. }
  972. txq_advance(&q->q, ndesc);
  973. ring_tx_db(adap, &q->q, ndesc);
  974. return NETDEV_TX_OK;
  975. }
  976. /**
  977. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  978. * @q: the SGE control Tx queue
  979. *
  980. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  981. * that send only immediate data (presently just the control queues) and
  982. * thus do not have any sk_buffs to release.
  983. */
  984. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  985. {
  986. int hw_cidx = ntohs(q->stat->cidx);
  987. int reclaim = hw_cidx - q->cidx;
  988. if (reclaim < 0)
  989. reclaim += q->size;
  990. q->in_use -= reclaim;
  991. q->cidx = hw_cidx;
  992. }
  993. /**
  994. * is_imm - check whether a packet can be sent as immediate data
  995. * @skb: the packet
  996. *
  997. * Returns true if a packet can be sent as a WR with immediate data.
  998. */
  999. static inline int is_imm(const struct sk_buff *skb)
  1000. {
  1001. return skb->len <= MAX_CTRL_WR_LEN;
  1002. }
  1003. /**
  1004. * ctrlq_check_stop - check if a control queue is full and should stop
  1005. * @q: the queue
  1006. * @wr: most recent WR written to the queue
  1007. *
  1008. * Check if a control queue has become full and should be stopped.
  1009. * We clean up control queue descriptors very lazily, only when we are out.
  1010. * If the queue is still full after reclaiming any completed descriptors
  1011. * we suspend it and have the last WR wake it up.
  1012. */
  1013. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  1014. {
  1015. reclaim_completed_tx_imm(&q->q);
  1016. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1017. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1018. q->q.stops++;
  1019. q->full = 1;
  1020. }
  1021. }
  1022. /**
  1023. * ctrl_xmit - send a packet through an SGE control Tx queue
  1024. * @q: the control queue
  1025. * @skb: the packet
  1026. *
  1027. * Send a packet through an SGE control Tx queue. Packets sent through
  1028. * a control queue must fit entirely as immediate data.
  1029. */
  1030. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  1031. {
  1032. unsigned int ndesc;
  1033. struct fw_wr_hdr *wr;
  1034. if (unlikely(!is_imm(skb))) {
  1035. WARN_ON(1);
  1036. dev_kfree_skb(skb);
  1037. return NET_XMIT_DROP;
  1038. }
  1039. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  1040. spin_lock(&q->sendq.lock);
  1041. if (unlikely(q->full)) {
  1042. skb->priority = ndesc; /* save for restart */
  1043. __skb_queue_tail(&q->sendq, skb);
  1044. spin_unlock(&q->sendq.lock);
  1045. return NET_XMIT_CN;
  1046. }
  1047. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1048. inline_tx_skb(skb, &q->q, wr);
  1049. txq_advance(&q->q, ndesc);
  1050. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  1051. ctrlq_check_stop(q, wr);
  1052. ring_tx_db(q->adap, &q->q, ndesc);
  1053. spin_unlock(&q->sendq.lock);
  1054. kfree_skb(skb);
  1055. return NET_XMIT_SUCCESS;
  1056. }
  1057. /**
  1058. * restart_ctrlq - restart a suspended control queue
  1059. * @data: the control queue to restart
  1060. *
  1061. * Resumes transmission on a suspended Tx control queue.
  1062. */
  1063. static void restart_ctrlq(unsigned long data)
  1064. {
  1065. struct sk_buff *skb;
  1066. unsigned int written = 0;
  1067. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  1068. spin_lock(&q->sendq.lock);
  1069. reclaim_completed_tx_imm(&q->q);
  1070. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  1071. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  1072. struct fw_wr_hdr *wr;
  1073. unsigned int ndesc = skb->priority; /* previously saved */
  1074. /*
  1075. * Write descriptors and free skbs outside the lock to limit
  1076. * wait times. q->full is still set so new skbs will be queued.
  1077. */
  1078. spin_unlock(&q->sendq.lock);
  1079. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1080. inline_tx_skb(skb, &q->q, wr);
  1081. kfree_skb(skb);
  1082. written += ndesc;
  1083. txq_advance(&q->q, ndesc);
  1084. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1085. unsigned long old = q->q.stops;
  1086. ctrlq_check_stop(q, wr);
  1087. if (q->q.stops != old) { /* suspended anew */
  1088. spin_lock(&q->sendq.lock);
  1089. goto ringdb;
  1090. }
  1091. }
  1092. if (written > 16) {
  1093. ring_tx_db(q->adap, &q->q, written);
  1094. written = 0;
  1095. }
  1096. spin_lock(&q->sendq.lock);
  1097. }
  1098. q->full = 0;
  1099. ringdb: if (written)
  1100. ring_tx_db(q->adap, &q->q, written);
  1101. spin_unlock(&q->sendq.lock);
  1102. }
  1103. /**
  1104. * t4_mgmt_tx - send a management message
  1105. * @adap: the adapter
  1106. * @skb: the packet containing the management message
  1107. *
  1108. * Send a management message through control queue 0.
  1109. */
  1110. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1111. {
  1112. int ret;
  1113. local_bh_disable();
  1114. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1115. local_bh_enable();
  1116. return ret;
  1117. }
  1118. /**
  1119. * is_ofld_imm - check whether a packet can be sent as immediate data
  1120. * @skb: the packet
  1121. *
  1122. * Returns true if a packet can be sent as an offload WR with immediate
  1123. * data. We currently use the same limit as for Ethernet packets.
  1124. */
  1125. static inline int is_ofld_imm(const struct sk_buff *skb)
  1126. {
  1127. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1128. }
  1129. /**
  1130. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1131. * @skb: the packet
  1132. *
  1133. * Returns the number of flits needed for the given offload packet.
  1134. * These packets are already fully constructed and no additional headers
  1135. * will be added.
  1136. */
  1137. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1138. {
  1139. unsigned int flits, cnt;
  1140. if (is_ofld_imm(skb))
  1141. return DIV_ROUND_UP(skb->len, 8);
  1142. flits = skb_transport_offset(skb) / 8U; /* headers */
  1143. cnt = skb_shinfo(skb)->nr_frags;
  1144. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1145. cnt++;
  1146. return flits + sgl_len(cnt);
  1147. }
  1148. /**
  1149. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1150. * @adap: the adapter
  1151. * @q: the queue to stop
  1152. *
  1153. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1154. * inability to map packets. A periodic timer attempts to restart
  1155. * queues so marked.
  1156. */
  1157. static void txq_stop_maperr(struct sge_ofld_txq *q)
  1158. {
  1159. q->mapping_err++;
  1160. q->q.stops++;
  1161. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1162. q->adap->sge.txq_maperr);
  1163. }
  1164. /**
  1165. * ofldtxq_stop - stop an offload Tx queue that has become full
  1166. * @q: the queue to stop
  1167. * @skb: the packet causing the queue to become full
  1168. *
  1169. * Stops an offload Tx queue that has become full and modifies the packet
  1170. * being written to request a wakeup.
  1171. */
  1172. static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
  1173. {
  1174. struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
  1175. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1176. q->q.stops++;
  1177. q->full = 1;
  1178. }
  1179. /**
  1180. * service_ofldq - restart a suspended offload queue
  1181. * @q: the offload queue
  1182. *
  1183. * Services an offload Tx queue by moving packets from its packet queue
  1184. * to the HW Tx ring. The function starts and ends with the queue locked.
  1185. */
  1186. static void service_ofldq(struct sge_ofld_txq *q)
  1187. {
  1188. u64 *pos;
  1189. int credits;
  1190. struct sk_buff *skb;
  1191. unsigned int written = 0;
  1192. unsigned int flits, ndesc;
  1193. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1194. /*
  1195. * We drop the lock but leave skb on sendq, thus retaining
  1196. * exclusive access to the state of the queue.
  1197. */
  1198. spin_unlock(&q->sendq.lock);
  1199. reclaim_completed_tx(q->adap, &q->q, false);
  1200. flits = skb->priority; /* previously saved */
  1201. ndesc = flits_to_desc(flits);
  1202. credits = txq_avail(&q->q) - ndesc;
  1203. BUG_ON(credits < 0);
  1204. if (unlikely(credits < TXQ_STOP_THRES))
  1205. ofldtxq_stop(q, skb);
  1206. pos = (u64 *)&q->q.desc[q->q.pidx];
  1207. if (is_ofld_imm(skb))
  1208. inline_tx_skb(skb, &q->q, pos);
  1209. else if (map_skb(q->adap->pdev_dev, skb,
  1210. (dma_addr_t *)skb->head)) {
  1211. txq_stop_maperr(q);
  1212. spin_lock(&q->sendq.lock);
  1213. break;
  1214. } else {
  1215. int last_desc, hdr_len = skb_transport_offset(skb);
  1216. memcpy(pos, skb->data, hdr_len);
  1217. write_sgl(skb, &q->q, (void *)pos + hdr_len,
  1218. pos + flits, hdr_len,
  1219. (dma_addr_t *)skb->head);
  1220. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1221. skb->dev = q->adap->port[0];
  1222. skb->destructor = deferred_unmap_destructor;
  1223. #endif
  1224. last_desc = q->q.pidx + ndesc - 1;
  1225. if (last_desc >= q->q.size)
  1226. last_desc -= q->q.size;
  1227. q->q.sdesc[last_desc].skb = skb;
  1228. }
  1229. txq_advance(&q->q, ndesc);
  1230. written += ndesc;
  1231. if (unlikely(written > 32)) {
  1232. ring_tx_db(q->adap, &q->q, written);
  1233. written = 0;
  1234. }
  1235. spin_lock(&q->sendq.lock);
  1236. __skb_unlink(skb, &q->sendq);
  1237. if (is_ofld_imm(skb))
  1238. kfree_skb(skb);
  1239. }
  1240. if (likely(written))
  1241. ring_tx_db(q->adap, &q->q, written);
  1242. }
  1243. /**
  1244. * ofld_xmit - send a packet through an offload queue
  1245. * @q: the Tx offload queue
  1246. * @skb: the packet
  1247. *
  1248. * Send an offload packet through an SGE offload queue.
  1249. */
  1250. static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
  1251. {
  1252. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1253. spin_lock(&q->sendq.lock);
  1254. __skb_queue_tail(&q->sendq, skb);
  1255. if (q->sendq.qlen == 1)
  1256. service_ofldq(q);
  1257. spin_unlock(&q->sendq.lock);
  1258. return NET_XMIT_SUCCESS;
  1259. }
  1260. /**
  1261. * restart_ofldq - restart a suspended offload queue
  1262. * @data: the offload queue to restart
  1263. *
  1264. * Resumes transmission on a suspended Tx offload queue.
  1265. */
  1266. static void restart_ofldq(unsigned long data)
  1267. {
  1268. struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
  1269. spin_lock(&q->sendq.lock);
  1270. q->full = 0; /* the queue actually is completely empty now */
  1271. service_ofldq(q);
  1272. spin_unlock(&q->sendq.lock);
  1273. }
  1274. /**
  1275. * skb_txq - return the Tx queue an offload packet should use
  1276. * @skb: the packet
  1277. *
  1278. * Returns the Tx queue an offload packet should use as indicated by bits
  1279. * 1-15 in the packet's queue_mapping.
  1280. */
  1281. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1282. {
  1283. return skb->queue_mapping >> 1;
  1284. }
  1285. /**
  1286. * is_ctrl_pkt - return whether an offload packet is a control packet
  1287. * @skb: the packet
  1288. *
  1289. * Returns whether an offload packet should use an OFLD or a CTRL
  1290. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1291. */
  1292. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1293. {
  1294. return skb->queue_mapping & 1;
  1295. }
  1296. static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
  1297. {
  1298. unsigned int idx = skb_txq(skb);
  1299. if (unlikely(is_ctrl_pkt(skb)))
  1300. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1301. return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
  1302. }
  1303. /**
  1304. * t4_ofld_send - send an offload packet
  1305. * @adap: the adapter
  1306. * @skb: the packet
  1307. *
  1308. * Sends an offload packet. We use the packet queue_mapping to select the
  1309. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1310. * should be sent as regular or control, bits 1-15 select the queue.
  1311. */
  1312. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1313. {
  1314. int ret;
  1315. local_bh_disable();
  1316. ret = ofld_send(adap, skb);
  1317. local_bh_enable();
  1318. return ret;
  1319. }
  1320. /**
  1321. * cxgb4_ofld_send - send an offload packet
  1322. * @dev: the net device
  1323. * @skb: the packet
  1324. *
  1325. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1326. * intended for ULDs.
  1327. */
  1328. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1329. {
  1330. return t4_ofld_send(netdev2adap(dev), skb);
  1331. }
  1332. EXPORT_SYMBOL(cxgb4_ofld_send);
  1333. static inline void copy_frags(struct sk_buff *skb,
  1334. const struct pkt_gl *gl, unsigned int offset)
  1335. {
  1336. int i;
  1337. /* usually there's just one frag */
  1338. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  1339. gl->frags[0].offset + offset,
  1340. gl->frags[0].size - offset);
  1341. skb_shinfo(skb)->nr_frags = gl->nfrags;
  1342. for (i = 1; i < gl->nfrags; i++)
  1343. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  1344. gl->frags[i].offset,
  1345. gl->frags[i].size);
  1346. /* get a reference to the last page, we don't own it */
  1347. get_page(gl->frags[gl->nfrags - 1].page);
  1348. }
  1349. /**
  1350. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1351. * @gl: the gather list
  1352. * @skb_len: size of sk_buff main body if it carries fragments
  1353. * @pull_len: amount of data to move to the sk_buff's main body
  1354. *
  1355. * Builds an sk_buff from the given packet gather list. Returns the
  1356. * sk_buff or %NULL if sk_buff allocation failed.
  1357. */
  1358. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1359. unsigned int skb_len, unsigned int pull_len)
  1360. {
  1361. struct sk_buff *skb;
  1362. /*
  1363. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1364. * size, which is expected since buffers are at least PAGE_SIZEd.
  1365. * In this case packets up to RX_COPY_THRES have only one fragment.
  1366. */
  1367. if (gl->tot_len <= RX_COPY_THRES) {
  1368. skb = dev_alloc_skb(gl->tot_len);
  1369. if (unlikely(!skb))
  1370. goto out;
  1371. __skb_put(skb, gl->tot_len);
  1372. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1373. } else {
  1374. skb = dev_alloc_skb(skb_len);
  1375. if (unlikely(!skb))
  1376. goto out;
  1377. __skb_put(skb, pull_len);
  1378. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1379. copy_frags(skb, gl, pull_len);
  1380. skb->len = gl->tot_len;
  1381. skb->data_len = skb->len - pull_len;
  1382. skb->truesize += skb->data_len;
  1383. }
  1384. out: return skb;
  1385. }
  1386. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1387. /**
  1388. * t4_pktgl_free - free a packet gather list
  1389. * @gl: the gather list
  1390. *
  1391. * Releases the pages of a packet gather list. We do not own the last
  1392. * page on the list and do not free it.
  1393. */
  1394. static void t4_pktgl_free(const struct pkt_gl *gl)
  1395. {
  1396. int n;
  1397. const struct page_frag *p;
  1398. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1399. put_page(p->page);
  1400. }
  1401. /*
  1402. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1403. * be delivered to anyone and send it to the stack for capture.
  1404. */
  1405. static noinline int handle_trace_pkt(struct adapter *adap,
  1406. const struct pkt_gl *gl)
  1407. {
  1408. struct sk_buff *skb;
  1409. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  1410. if (unlikely(!skb)) {
  1411. t4_pktgl_free(gl);
  1412. return 0;
  1413. }
  1414. if (is_t4(adap->params.chip))
  1415. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  1416. else
  1417. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  1418. skb_reset_mac_header(skb);
  1419. skb->protocol = htons(0xffff);
  1420. skb->dev = adap->port[0];
  1421. netif_receive_skb(skb);
  1422. return 0;
  1423. }
  1424. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1425. const struct cpl_rx_pkt *pkt)
  1426. {
  1427. struct adapter *adapter = rxq->rspq.adap;
  1428. struct sge *s = &adapter->sge;
  1429. int ret;
  1430. struct sk_buff *skb;
  1431. skb = napi_get_frags(&rxq->rspq.napi);
  1432. if (unlikely(!skb)) {
  1433. t4_pktgl_free(gl);
  1434. rxq->stats.rx_drops++;
  1435. return;
  1436. }
  1437. copy_frags(skb, gl, s->pktshift);
  1438. skb->len = gl->tot_len - s->pktshift;
  1439. skb->data_len = skb->len;
  1440. skb->truesize += skb->data_len;
  1441. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1442. skb_record_rx_queue(skb, rxq->rspq.idx);
  1443. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  1444. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1445. PKT_HASH_TYPE_L3);
  1446. if (unlikely(pkt->vlan_ex)) {
  1447. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1448. rxq->stats.vlan_ex++;
  1449. }
  1450. ret = napi_gro_frags(&rxq->rspq.napi);
  1451. if (ret == GRO_HELD)
  1452. rxq->stats.lro_pkts++;
  1453. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1454. rxq->stats.lro_merged++;
  1455. rxq->stats.pkts++;
  1456. rxq->stats.rx_cso++;
  1457. }
  1458. /**
  1459. * t4_ethrx_handler - process an ingress ethernet packet
  1460. * @q: the response queue that received the packet
  1461. * @rsp: the response queue descriptor holding the RX_PKT message
  1462. * @si: the gather list of packet fragments
  1463. *
  1464. * Process an ingress ethernet packet and deliver it to the stack.
  1465. */
  1466. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  1467. const struct pkt_gl *si)
  1468. {
  1469. bool csum_ok;
  1470. struct sk_buff *skb;
  1471. const struct cpl_rx_pkt *pkt;
  1472. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1473. struct sge *s = &q->adap->sge;
  1474. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  1475. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  1476. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  1477. return handle_trace_pkt(q->adap, si);
  1478. pkt = (const struct cpl_rx_pkt *)rsp;
  1479. csum_ok = pkt->csum_calc && !pkt->err_vec;
  1480. if ((pkt->l2info & htonl(RXF_TCP)) &&
  1481. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  1482. do_gro(rxq, si, pkt);
  1483. return 0;
  1484. }
  1485. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  1486. if (unlikely(!skb)) {
  1487. t4_pktgl_free(si);
  1488. rxq->stats.rx_drops++;
  1489. return 0;
  1490. }
  1491. __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
  1492. skb->protocol = eth_type_trans(skb, q->netdev);
  1493. skb_record_rx_queue(skb, q->idx);
  1494. if (skb->dev->features & NETIF_F_RXHASH)
  1495. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1496. PKT_HASH_TYPE_L3);
  1497. rxq->stats.pkts++;
  1498. if (csum_ok && (q->netdev->features & NETIF_F_RXCSUM) &&
  1499. (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
  1500. if (!pkt->ip_frag) {
  1501. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1502. rxq->stats.rx_cso++;
  1503. } else if (pkt->l2info & htonl(RXF_IP)) {
  1504. __sum16 c = (__force __sum16)pkt->csum;
  1505. skb->csum = csum_unfold(c);
  1506. skb->ip_summed = CHECKSUM_COMPLETE;
  1507. rxq->stats.rx_cso++;
  1508. }
  1509. } else
  1510. skb_checksum_none_assert(skb);
  1511. if (unlikely(pkt->vlan_ex)) {
  1512. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1513. rxq->stats.vlan_ex++;
  1514. }
  1515. netif_receive_skb(skb);
  1516. return 0;
  1517. }
  1518. /**
  1519. * restore_rx_bufs - put back a packet's Rx buffers
  1520. * @si: the packet gather list
  1521. * @q: the SGE free list
  1522. * @frags: number of FL buffers to restore
  1523. *
  1524. * Puts back on an FL the Rx buffers associated with @si. The buffers
  1525. * have already been unmapped and are left unmapped, we mark them so to
  1526. * prevent further unmapping attempts.
  1527. *
  1528. * This function undoes a series of @unmap_rx_buf calls when we find out
  1529. * that the current packet can't be processed right away afterall and we
  1530. * need to come back to it later. This is a very rare event and there's
  1531. * no effort to make this particularly efficient.
  1532. */
  1533. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  1534. int frags)
  1535. {
  1536. struct rx_sw_desc *d;
  1537. while (frags--) {
  1538. if (q->cidx == 0)
  1539. q->cidx = q->size - 1;
  1540. else
  1541. q->cidx--;
  1542. d = &q->sdesc[q->cidx];
  1543. d->page = si->frags[frags].page;
  1544. d->dma_addr |= RX_UNMAPPED_BUF;
  1545. q->avail++;
  1546. }
  1547. }
  1548. /**
  1549. * is_new_response - check if a response is newly written
  1550. * @r: the response descriptor
  1551. * @q: the response queue
  1552. *
  1553. * Returns true if a response descriptor contains a yet unprocessed
  1554. * response.
  1555. */
  1556. static inline bool is_new_response(const struct rsp_ctrl *r,
  1557. const struct sge_rspq *q)
  1558. {
  1559. return RSPD_GEN(r->type_gen) == q->gen;
  1560. }
  1561. /**
  1562. * rspq_next - advance to the next entry in a response queue
  1563. * @q: the queue
  1564. *
  1565. * Updates the state of a response queue to advance it to the next entry.
  1566. */
  1567. static inline void rspq_next(struct sge_rspq *q)
  1568. {
  1569. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  1570. if (unlikely(++q->cidx == q->size)) {
  1571. q->cidx = 0;
  1572. q->gen ^= 1;
  1573. q->cur_desc = q->desc;
  1574. }
  1575. }
  1576. /**
  1577. * process_responses - process responses from an SGE response queue
  1578. * @q: the ingress queue to process
  1579. * @budget: how many responses can be processed in this round
  1580. *
  1581. * Process responses from an SGE response queue up to the supplied budget.
  1582. * Responses include received packets as well as control messages from FW
  1583. * or HW.
  1584. *
  1585. * Additionally choose the interrupt holdoff time for the next interrupt
  1586. * on this queue. If the system is under memory shortage use a fairly
  1587. * long delay to help recovery.
  1588. */
  1589. static int process_responses(struct sge_rspq *q, int budget)
  1590. {
  1591. int ret, rsp_type;
  1592. int budget_left = budget;
  1593. const struct rsp_ctrl *rc;
  1594. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1595. struct adapter *adapter = q->adap;
  1596. struct sge *s = &adapter->sge;
  1597. while (likely(budget_left)) {
  1598. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1599. if (!is_new_response(rc, q))
  1600. break;
  1601. rmb();
  1602. rsp_type = RSPD_TYPE(rc->type_gen);
  1603. if (likely(rsp_type == RSP_TYPE_FLBUF)) {
  1604. struct page_frag *fp;
  1605. struct pkt_gl si;
  1606. const struct rx_sw_desc *rsd;
  1607. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  1608. if (len & RSPD_NEWBUF) {
  1609. if (likely(q->offset > 0)) {
  1610. free_rx_bufs(q->adap, &rxq->fl, 1);
  1611. q->offset = 0;
  1612. }
  1613. len = RSPD_LEN(len);
  1614. }
  1615. si.tot_len = len;
  1616. /* gather packet fragments */
  1617. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  1618. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  1619. bufsz = get_buf_size(adapter, rsd);
  1620. fp->page = rsd->page;
  1621. fp->offset = q->offset;
  1622. fp->size = min(bufsz, len);
  1623. len -= fp->size;
  1624. if (!len)
  1625. break;
  1626. unmap_rx_buf(q->adap, &rxq->fl);
  1627. }
  1628. /*
  1629. * Last buffer remains mapped so explicitly make it
  1630. * coherent for CPU access.
  1631. */
  1632. dma_sync_single_for_cpu(q->adap->pdev_dev,
  1633. get_buf_addr(rsd),
  1634. fp->size, DMA_FROM_DEVICE);
  1635. si.va = page_address(si.frags[0].page) +
  1636. si.frags[0].offset;
  1637. prefetch(si.va);
  1638. si.nfrags = frags + 1;
  1639. ret = q->handler(q, q->cur_desc, &si);
  1640. if (likely(ret == 0))
  1641. q->offset += ALIGN(fp->size, s->fl_align);
  1642. else
  1643. restore_rx_bufs(&si, &rxq->fl, frags);
  1644. } else if (likely(rsp_type == RSP_TYPE_CPL)) {
  1645. ret = q->handler(q, q->cur_desc, NULL);
  1646. } else {
  1647. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  1648. }
  1649. if (unlikely(ret)) {
  1650. /* couldn't process descriptor, back off for recovery */
  1651. q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
  1652. break;
  1653. }
  1654. rspq_next(q);
  1655. budget_left--;
  1656. }
  1657. if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
  1658. __refill_fl(q->adap, &rxq->fl);
  1659. return budget - budget_left;
  1660. }
  1661. /**
  1662. * napi_rx_handler - the NAPI handler for Rx processing
  1663. * @napi: the napi instance
  1664. * @budget: how many packets we can process in this round
  1665. *
  1666. * Handler for new data events when using NAPI. This does not need any
  1667. * locking or protection from interrupts as data interrupts are off at
  1668. * this point and other adapter interrupts do not interfere (the latter
  1669. * in not a concern at all with MSI-X as non-data interrupts then have
  1670. * a separate handler).
  1671. */
  1672. static int napi_rx_handler(struct napi_struct *napi, int budget)
  1673. {
  1674. unsigned int params;
  1675. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  1676. int work_done = process_responses(q, budget);
  1677. if (likely(work_done < budget)) {
  1678. napi_complete(napi);
  1679. params = q->next_intr_params;
  1680. q->next_intr_params = q->intr_params;
  1681. } else
  1682. params = QINTR_TIMER_IDX(7);
  1683. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS), CIDXINC(work_done) |
  1684. INGRESSQID((u32)q->cntxt_id) | SEINTARM(params));
  1685. return work_done;
  1686. }
  1687. /*
  1688. * The MSI-X interrupt handler for an SGE response queue.
  1689. */
  1690. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  1691. {
  1692. struct sge_rspq *q = cookie;
  1693. napi_schedule(&q->napi);
  1694. return IRQ_HANDLED;
  1695. }
  1696. /*
  1697. * Process the indirect interrupt entries in the interrupt queue and kick off
  1698. * NAPI for each queue that has generated an entry.
  1699. */
  1700. static unsigned int process_intrq(struct adapter *adap)
  1701. {
  1702. unsigned int credits;
  1703. const struct rsp_ctrl *rc;
  1704. struct sge_rspq *q = &adap->sge.intrq;
  1705. spin_lock(&adap->sge.intrq_lock);
  1706. for (credits = 0; ; credits++) {
  1707. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1708. if (!is_new_response(rc, q))
  1709. break;
  1710. rmb();
  1711. if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
  1712. unsigned int qid = ntohl(rc->pldbuflen_qid);
  1713. qid -= adap->sge.ingr_start;
  1714. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  1715. }
  1716. rspq_next(q);
  1717. }
  1718. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS), CIDXINC(credits) |
  1719. INGRESSQID(q->cntxt_id) | SEINTARM(q->intr_params));
  1720. spin_unlock(&adap->sge.intrq_lock);
  1721. return credits;
  1722. }
  1723. /*
  1724. * The MSI interrupt handler, which handles data events from SGE response queues
  1725. * as well as error and other async events as they all use the same MSI vector.
  1726. */
  1727. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  1728. {
  1729. struct adapter *adap = cookie;
  1730. t4_slow_intr_handler(adap);
  1731. process_intrq(adap);
  1732. return IRQ_HANDLED;
  1733. }
  1734. /*
  1735. * Interrupt handler for legacy INTx interrupts.
  1736. * Handles data events from SGE response queues as well as error and other
  1737. * async events as they all use the same interrupt line.
  1738. */
  1739. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  1740. {
  1741. struct adapter *adap = cookie;
  1742. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
  1743. if (t4_slow_intr_handler(adap) | process_intrq(adap))
  1744. return IRQ_HANDLED;
  1745. return IRQ_NONE; /* probably shared interrupt */
  1746. }
  1747. /**
  1748. * t4_intr_handler - select the top-level interrupt handler
  1749. * @adap: the adapter
  1750. *
  1751. * Selects the top-level interrupt handler based on the type of interrupts
  1752. * (MSI-X, MSI, or INTx).
  1753. */
  1754. irq_handler_t t4_intr_handler(struct adapter *adap)
  1755. {
  1756. if (adap->flags & USING_MSIX)
  1757. return t4_sge_intr_msix;
  1758. if (adap->flags & USING_MSI)
  1759. return t4_intr_msi;
  1760. return t4_intr_intx;
  1761. }
  1762. static void sge_rx_timer_cb(unsigned long data)
  1763. {
  1764. unsigned long m;
  1765. unsigned int i, cnt[2];
  1766. struct adapter *adap = (struct adapter *)data;
  1767. struct sge *s = &adap->sge;
  1768. for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
  1769. for (m = s->starving_fl[i]; m; m &= m - 1) {
  1770. struct sge_eth_rxq *rxq;
  1771. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  1772. struct sge_fl *fl = s->egr_map[id];
  1773. clear_bit(id, s->starving_fl);
  1774. smp_mb__after_clear_bit();
  1775. if (fl_starving(fl)) {
  1776. rxq = container_of(fl, struct sge_eth_rxq, fl);
  1777. if (napi_reschedule(&rxq->rspq.napi))
  1778. fl->starving++;
  1779. else
  1780. set_bit(id, s->starving_fl);
  1781. }
  1782. }
  1783. t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
  1784. cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
  1785. cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1786. for (i = 0; i < 2; i++)
  1787. if (cnt[i] >= s->starve_thres) {
  1788. if (s->idma_state[i] || cnt[i] == 0xffffffff)
  1789. continue;
  1790. s->idma_state[i] = 1;
  1791. t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
  1792. m = t4_read_reg(adap, SGE_DEBUG_DATA_LOW) >> (i * 16);
  1793. dev_warn(adap->pdev_dev,
  1794. "SGE idma%u starvation detected for "
  1795. "queue %lu\n", i, m & 0xffff);
  1796. } else if (s->idma_state[i])
  1797. s->idma_state[i] = 0;
  1798. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  1799. }
  1800. static void sge_tx_timer_cb(unsigned long data)
  1801. {
  1802. unsigned long m;
  1803. unsigned int i, budget;
  1804. struct adapter *adap = (struct adapter *)data;
  1805. struct sge *s = &adap->sge;
  1806. for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
  1807. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  1808. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  1809. struct sge_ofld_txq *txq = s->egr_map[id];
  1810. clear_bit(id, s->txq_maperr);
  1811. tasklet_schedule(&txq->qresume_tsk);
  1812. }
  1813. budget = MAX_TIMER_TX_RECLAIM;
  1814. i = s->ethtxq_rover;
  1815. do {
  1816. struct sge_eth_txq *q = &s->ethtxq[i];
  1817. if (q->q.in_use &&
  1818. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  1819. __netif_tx_trylock(q->txq)) {
  1820. int avail = reclaimable(&q->q);
  1821. if (avail) {
  1822. if (avail > budget)
  1823. avail = budget;
  1824. free_tx_desc(adap, &q->q, avail, true);
  1825. q->q.in_use -= avail;
  1826. budget -= avail;
  1827. }
  1828. __netif_tx_unlock(q->txq);
  1829. }
  1830. if (++i >= s->ethqsets)
  1831. i = 0;
  1832. } while (budget && i != s->ethtxq_rover);
  1833. s->ethtxq_rover = i;
  1834. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  1835. }
  1836. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  1837. struct net_device *dev, int intr_idx,
  1838. struct sge_fl *fl, rspq_handler_t hnd)
  1839. {
  1840. int ret, flsz = 0;
  1841. struct fw_iq_cmd c;
  1842. struct sge *s = &adap->sge;
  1843. struct port_info *pi = netdev_priv(dev);
  1844. /* Size needs to be multiple of 16, including status entry. */
  1845. iq->size = roundup(iq->size, 16);
  1846. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  1847. &iq->phys_addr, NULL, 0, NUMA_NO_NODE);
  1848. if (!iq->desc)
  1849. return -ENOMEM;
  1850. memset(&c, 0, sizeof(c));
  1851. c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
  1852. FW_CMD_WRITE | FW_CMD_EXEC |
  1853. FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
  1854. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
  1855. FW_LEN16(c));
  1856. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
  1857. FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
  1858. FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
  1859. FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
  1860. -intr_idx - 1));
  1861. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
  1862. FW_IQ_CMD_IQGTSMODE |
  1863. FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
  1864. FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
  1865. c.iqsize = htons(iq->size);
  1866. c.iqaddr = cpu_to_be64(iq->phys_addr);
  1867. if (fl) {
  1868. fl->size = roundup(fl->size, 8);
  1869. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  1870. sizeof(struct rx_sw_desc), &fl->addr,
  1871. &fl->sdesc, s->stat_len, NUMA_NO_NODE);
  1872. if (!fl->desc)
  1873. goto fl_nomem;
  1874. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  1875. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN(1) |
  1876. FW_IQ_CMD_FL0FETCHRO(1) |
  1877. FW_IQ_CMD_FL0DATARO(1) |
  1878. FW_IQ_CMD_FL0PADEN(1));
  1879. c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
  1880. FW_IQ_CMD_FL0FBMAX(3));
  1881. c.fl0size = htons(flsz);
  1882. c.fl0addr = cpu_to_be64(fl->addr);
  1883. }
  1884. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  1885. if (ret)
  1886. goto err;
  1887. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  1888. iq->cur_desc = iq->desc;
  1889. iq->cidx = 0;
  1890. iq->gen = 1;
  1891. iq->next_intr_params = iq->intr_params;
  1892. iq->cntxt_id = ntohs(c.iqid);
  1893. iq->abs_id = ntohs(c.physiqid);
  1894. iq->size--; /* subtract status entry */
  1895. iq->adap = adap;
  1896. iq->netdev = dev;
  1897. iq->handler = hnd;
  1898. /* set offset to -1 to distinguish ingress queues without FL */
  1899. iq->offset = fl ? 0 : -1;
  1900. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  1901. if (fl) {
  1902. fl->cntxt_id = ntohs(c.fl0id);
  1903. fl->avail = fl->pend_cred = 0;
  1904. fl->pidx = fl->cidx = 0;
  1905. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  1906. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  1907. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  1908. }
  1909. return 0;
  1910. fl_nomem:
  1911. ret = -ENOMEM;
  1912. err:
  1913. if (iq->desc) {
  1914. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  1915. iq->desc, iq->phys_addr);
  1916. iq->desc = NULL;
  1917. }
  1918. if (fl && fl->desc) {
  1919. kfree(fl->sdesc);
  1920. fl->sdesc = NULL;
  1921. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  1922. fl->desc, fl->addr);
  1923. fl->desc = NULL;
  1924. }
  1925. return ret;
  1926. }
  1927. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  1928. {
  1929. q->cntxt_id = id;
  1930. if (!is_t4(adap->params.chip)) {
  1931. unsigned int s_qpp;
  1932. unsigned short udb_density;
  1933. unsigned long qpshift;
  1934. int page;
  1935. s_qpp = QUEUESPERPAGEPF1 * adap->fn;
  1936. udb_density = 1 << QUEUESPERPAGEPF0_GET((t4_read_reg(adap,
  1937. SGE_EGRESS_QUEUES_PER_PAGE_PF) >> s_qpp));
  1938. qpshift = PAGE_SHIFT - ilog2(udb_density);
  1939. q->udb = q->cntxt_id << qpshift;
  1940. q->udb &= PAGE_MASK;
  1941. page = q->udb / PAGE_SIZE;
  1942. q->udb += (q->cntxt_id - (page * udb_density)) * 128;
  1943. }
  1944. q->in_use = 0;
  1945. q->cidx = q->pidx = 0;
  1946. q->stops = q->restarts = 0;
  1947. q->stat = (void *)&q->desc[q->size];
  1948. spin_lock_init(&q->db_lock);
  1949. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  1950. }
  1951. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  1952. struct net_device *dev, struct netdev_queue *netdevq,
  1953. unsigned int iqid)
  1954. {
  1955. int ret, nentries;
  1956. struct fw_eq_eth_cmd c;
  1957. struct sge *s = &adap->sge;
  1958. struct port_info *pi = netdev_priv(dev);
  1959. /* Add status entries */
  1960. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  1961. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  1962. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  1963. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  1964. netdev_queue_numa_node_read(netdevq));
  1965. if (!txq->q.desc)
  1966. return -ENOMEM;
  1967. memset(&c, 0, sizeof(c));
  1968. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
  1969. FW_CMD_WRITE | FW_CMD_EXEC |
  1970. FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
  1971. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
  1972. FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
  1973. c.viid_pkd = htonl(FW_EQ_ETH_CMD_VIID(pi->viid));
  1974. c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
  1975. FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
  1976. FW_EQ_ETH_CMD_FETCHRO(1) |
  1977. FW_EQ_ETH_CMD_IQID(iqid));
  1978. c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
  1979. FW_EQ_ETH_CMD_FBMAX(3) |
  1980. FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
  1981. FW_EQ_ETH_CMD_EQSIZE(nentries));
  1982. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  1983. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  1984. if (ret) {
  1985. kfree(txq->q.sdesc);
  1986. txq->q.sdesc = NULL;
  1987. dma_free_coherent(adap->pdev_dev,
  1988. nentries * sizeof(struct tx_desc),
  1989. txq->q.desc, txq->q.phys_addr);
  1990. txq->q.desc = NULL;
  1991. return ret;
  1992. }
  1993. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  1994. txq->txq = netdevq;
  1995. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  1996. txq->mapping_err = 0;
  1997. return 0;
  1998. }
  1999. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  2000. struct net_device *dev, unsigned int iqid,
  2001. unsigned int cmplqid)
  2002. {
  2003. int ret, nentries;
  2004. struct fw_eq_ctrl_cmd c;
  2005. struct sge *s = &adap->sge;
  2006. struct port_info *pi = netdev_priv(dev);
  2007. /* Add status entries */
  2008. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2009. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  2010. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  2011. NULL, 0, NUMA_NO_NODE);
  2012. if (!txq->q.desc)
  2013. return -ENOMEM;
  2014. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
  2015. FW_CMD_WRITE | FW_CMD_EXEC |
  2016. FW_EQ_CTRL_CMD_PFN(adap->fn) |
  2017. FW_EQ_CTRL_CMD_VFN(0));
  2018. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
  2019. FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
  2020. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
  2021. c.physeqid_pkd = htonl(0);
  2022. c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
  2023. FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
  2024. FW_EQ_CTRL_CMD_FETCHRO |
  2025. FW_EQ_CTRL_CMD_IQID(iqid));
  2026. c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
  2027. FW_EQ_CTRL_CMD_FBMAX(3) |
  2028. FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
  2029. FW_EQ_CTRL_CMD_EQSIZE(nentries));
  2030. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2031. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2032. if (ret) {
  2033. dma_free_coherent(adap->pdev_dev,
  2034. nentries * sizeof(struct tx_desc),
  2035. txq->q.desc, txq->q.phys_addr);
  2036. txq->q.desc = NULL;
  2037. return ret;
  2038. }
  2039. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
  2040. txq->adap = adap;
  2041. skb_queue_head_init(&txq->sendq);
  2042. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  2043. txq->full = 0;
  2044. return 0;
  2045. }
  2046. int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
  2047. struct net_device *dev, unsigned int iqid)
  2048. {
  2049. int ret, nentries;
  2050. struct fw_eq_ofld_cmd c;
  2051. struct sge *s = &adap->sge;
  2052. struct port_info *pi = netdev_priv(dev);
  2053. /* Add status entries */
  2054. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2055. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2056. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2057. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2058. NUMA_NO_NODE);
  2059. if (!txq->q.desc)
  2060. return -ENOMEM;
  2061. memset(&c, 0, sizeof(c));
  2062. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
  2063. FW_CMD_WRITE | FW_CMD_EXEC |
  2064. FW_EQ_OFLD_CMD_PFN(adap->fn) |
  2065. FW_EQ_OFLD_CMD_VFN(0));
  2066. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
  2067. FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
  2068. c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
  2069. FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
  2070. FW_EQ_OFLD_CMD_FETCHRO(1) |
  2071. FW_EQ_OFLD_CMD_IQID(iqid));
  2072. c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
  2073. FW_EQ_OFLD_CMD_FBMAX(3) |
  2074. FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
  2075. FW_EQ_OFLD_CMD_EQSIZE(nentries));
  2076. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2077. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  2078. if (ret) {
  2079. kfree(txq->q.sdesc);
  2080. txq->q.sdesc = NULL;
  2081. dma_free_coherent(adap->pdev_dev,
  2082. nentries * sizeof(struct tx_desc),
  2083. txq->q.desc, txq->q.phys_addr);
  2084. txq->q.desc = NULL;
  2085. return ret;
  2086. }
  2087. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  2088. txq->adap = adap;
  2089. skb_queue_head_init(&txq->sendq);
  2090. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  2091. txq->full = 0;
  2092. txq->mapping_err = 0;
  2093. return 0;
  2094. }
  2095. static void free_txq(struct adapter *adap, struct sge_txq *q)
  2096. {
  2097. struct sge *s = &adap->sge;
  2098. dma_free_coherent(adap->pdev_dev,
  2099. q->size * sizeof(struct tx_desc) + s->stat_len,
  2100. q->desc, q->phys_addr);
  2101. q->cntxt_id = 0;
  2102. q->sdesc = NULL;
  2103. q->desc = NULL;
  2104. }
  2105. static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  2106. struct sge_fl *fl)
  2107. {
  2108. struct sge *s = &adap->sge;
  2109. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  2110. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  2111. t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
  2112. rq->cntxt_id, fl_id, 0xffff);
  2113. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  2114. rq->desc, rq->phys_addr);
  2115. netif_napi_del(&rq->napi);
  2116. rq->netdev = NULL;
  2117. rq->cntxt_id = rq->abs_id = 0;
  2118. rq->desc = NULL;
  2119. if (fl) {
  2120. free_rx_bufs(adap, fl, fl->avail);
  2121. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  2122. fl->desc, fl->addr);
  2123. kfree(fl->sdesc);
  2124. fl->sdesc = NULL;
  2125. fl->cntxt_id = 0;
  2126. fl->desc = NULL;
  2127. }
  2128. }
  2129. /**
  2130. * t4_free_sge_resources - free SGE resources
  2131. * @adap: the adapter
  2132. *
  2133. * Frees resources used by the SGE queue sets.
  2134. */
  2135. void t4_free_sge_resources(struct adapter *adap)
  2136. {
  2137. int i;
  2138. struct sge_eth_rxq *eq = adap->sge.ethrxq;
  2139. struct sge_eth_txq *etq = adap->sge.ethtxq;
  2140. struct sge_ofld_rxq *oq = adap->sge.ofldrxq;
  2141. /* clean up Ethernet Tx/Rx queues */
  2142. for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
  2143. if (eq->rspq.desc)
  2144. free_rspq_fl(adap, &eq->rspq, &eq->fl);
  2145. if (etq->q.desc) {
  2146. t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
  2147. etq->q.cntxt_id);
  2148. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  2149. kfree(etq->q.sdesc);
  2150. free_txq(adap, &etq->q);
  2151. }
  2152. }
  2153. /* clean up RDMA and iSCSI Rx queues */
  2154. for (i = 0; i < adap->sge.ofldqsets; i++, oq++) {
  2155. if (oq->rspq.desc)
  2156. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2157. }
  2158. for (i = 0, oq = adap->sge.rdmarxq; i < adap->sge.rdmaqs; i++, oq++) {
  2159. if (oq->rspq.desc)
  2160. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2161. }
  2162. /* clean up offload Tx queues */
  2163. for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
  2164. struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
  2165. if (q->q.desc) {
  2166. tasklet_kill(&q->qresume_tsk);
  2167. t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
  2168. q->q.cntxt_id);
  2169. free_tx_desc(adap, &q->q, q->q.in_use, false);
  2170. kfree(q->q.sdesc);
  2171. __skb_queue_purge(&q->sendq);
  2172. free_txq(adap, &q->q);
  2173. }
  2174. }
  2175. /* clean up control Tx queues */
  2176. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  2177. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  2178. if (cq->q.desc) {
  2179. tasklet_kill(&cq->qresume_tsk);
  2180. t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
  2181. cq->q.cntxt_id);
  2182. __skb_queue_purge(&cq->sendq);
  2183. free_txq(adap, &cq->q);
  2184. }
  2185. }
  2186. if (adap->sge.fw_evtq.desc)
  2187. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  2188. if (adap->sge.intrq.desc)
  2189. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  2190. /* clear the reverse egress queue map */
  2191. memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
  2192. }
  2193. void t4_sge_start(struct adapter *adap)
  2194. {
  2195. adap->sge.ethtxq_rover = 0;
  2196. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2197. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2198. }
  2199. /**
  2200. * t4_sge_stop - disable SGE operation
  2201. * @adap: the adapter
  2202. *
  2203. * Stop tasklets and timers associated with the DMA engine. Note that
  2204. * this is effective only if measures have been taken to disable any HW
  2205. * events that may restart them.
  2206. */
  2207. void t4_sge_stop(struct adapter *adap)
  2208. {
  2209. int i;
  2210. struct sge *s = &adap->sge;
  2211. if (in_interrupt()) /* actions below require waiting */
  2212. return;
  2213. if (s->rx_timer.function)
  2214. del_timer_sync(&s->rx_timer);
  2215. if (s->tx_timer.function)
  2216. del_timer_sync(&s->tx_timer);
  2217. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
  2218. struct sge_ofld_txq *q = &s->ofldtxq[i];
  2219. if (q->q.desc)
  2220. tasklet_kill(&q->qresume_tsk);
  2221. }
  2222. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  2223. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  2224. if (cq->q.desc)
  2225. tasklet_kill(&cq->qresume_tsk);
  2226. }
  2227. }
  2228. /**
  2229. * t4_sge_init - initialize SGE
  2230. * @adap: the adapter
  2231. *
  2232. * Performs SGE initialization needed every time after a chip reset.
  2233. * We do not initialize any of the queues here, instead the driver
  2234. * top-level must request them individually.
  2235. *
  2236. * Called in two different modes:
  2237. *
  2238. * 1. Perform actual hardware initialization and record hard-coded
  2239. * parameters which were used. This gets used when we're the
  2240. * Master PF and the Firmware Configuration File support didn't
  2241. * work for some reason.
  2242. *
  2243. * 2. We're not the Master PF or initialization was performed with
  2244. * a Firmware Configuration File. In this case we need to grab
  2245. * any of the SGE operating parameters that we need to have in
  2246. * order to do our job and make sure we can live with them ...
  2247. */
  2248. static int t4_sge_init_soft(struct adapter *adap)
  2249. {
  2250. struct sge *s = &adap->sge;
  2251. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  2252. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  2253. u32 ingress_rx_threshold;
  2254. /*
  2255. * Verify that CPL messages are going to the Ingress Queue for
  2256. * process_responses() and that only packet data is going to the
  2257. * Free Lists.
  2258. */
  2259. if ((t4_read_reg(adap, SGE_CONTROL) & RXPKTCPLMODE_MASK) !=
  2260. RXPKTCPLMODE(X_RXPKTCPLMODE_SPLIT)) {
  2261. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  2262. return -EINVAL;
  2263. }
  2264. /*
  2265. * Validate the Host Buffer Register Array indices that we want to
  2266. * use ...
  2267. *
  2268. * XXX Note that we should really read through the Host Buffer Size
  2269. * XXX register array and find the indices of the Buffer Sizes which
  2270. * XXX meet our needs!
  2271. */
  2272. #define READ_FL_BUF(x) \
  2273. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0+(x)*sizeof(u32))
  2274. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  2275. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  2276. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  2277. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  2278. #undef READ_FL_BUF
  2279. if (fl_small_pg != PAGE_SIZE ||
  2280. (fl_large_pg != 0 && (fl_large_pg < fl_small_pg ||
  2281. (fl_large_pg & (fl_large_pg-1)) != 0))) {
  2282. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  2283. fl_small_pg, fl_large_pg);
  2284. return -EINVAL;
  2285. }
  2286. if (fl_large_pg)
  2287. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  2288. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  2289. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  2290. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  2291. fl_small_mtu, fl_large_mtu);
  2292. return -EINVAL;
  2293. }
  2294. /*
  2295. * Retrieve our RX interrupt holdoff timer values and counter
  2296. * threshold values from the SGE parameters.
  2297. */
  2298. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1);
  2299. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3);
  2300. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5);
  2301. s->timer_val[0] = core_ticks_to_us(adap,
  2302. TIMERVALUE0_GET(timer_value_0_and_1));
  2303. s->timer_val[1] = core_ticks_to_us(adap,
  2304. TIMERVALUE1_GET(timer_value_0_and_1));
  2305. s->timer_val[2] = core_ticks_to_us(adap,
  2306. TIMERVALUE2_GET(timer_value_2_and_3));
  2307. s->timer_val[3] = core_ticks_to_us(adap,
  2308. TIMERVALUE3_GET(timer_value_2_and_3));
  2309. s->timer_val[4] = core_ticks_to_us(adap,
  2310. TIMERVALUE4_GET(timer_value_4_and_5));
  2311. s->timer_val[5] = core_ticks_to_us(adap,
  2312. TIMERVALUE5_GET(timer_value_4_and_5));
  2313. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD);
  2314. s->counter_val[0] = THRESHOLD_0_GET(ingress_rx_threshold);
  2315. s->counter_val[1] = THRESHOLD_1_GET(ingress_rx_threshold);
  2316. s->counter_val[2] = THRESHOLD_2_GET(ingress_rx_threshold);
  2317. s->counter_val[3] = THRESHOLD_3_GET(ingress_rx_threshold);
  2318. return 0;
  2319. }
  2320. static int t4_sge_init_hard(struct adapter *adap)
  2321. {
  2322. struct sge *s = &adap->sge;
  2323. /*
  2324. * Set up our basic SGE mode to deliver CPL messages to our Ingress
  2325. * Queue and Packet Date to the Free List.
  2326. */
  2327. t4_set_reg_field(adap, SGE_CONTROL, RXPKTCPLMODE_MASK,
  2328. RXPKTCPLMODE_MASK);
  2329. /*
  2330. * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
  2331. * and generate an interrupt when this occurs so we can recover.
  2332. */
  2333. if (is_t4(adap->params.chip)) {
  2334. t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
  2335. V_HP_INT_THRESH(M_HP_INT_THRESH) |
  2336. V_LP_INT_THRESH(M_LP_INT_THRESH),
  2337. V_HP_INT_THRESH(dbfifo_int_thresh) |
  2338. V_LP_INT_THRESH(dbfifo_int_thresh));
  2339. } else {
  2340. t4_set_reg_field(adap, A_SGE_DBFIFO_STATUS,
  2341. V_LP_INT_THRESH_T5(M_LP_INT_THRESH_T5),
  2342. V_LP_INT_THRESH_T5(dbfifo_int_thresh));
  2343. t4_set_reg_field(adap, SGE_DBFIFO_STATUS2,
  2344. V_HP_INT_THRESH_T5(M_HP_INT_THRESH_T5),
  2345. V_HP_INT_THRESH_T5(dbfifo_int_thresh));
  2346. }
  2347. t4_set_reg_field(adap, A_SGE_DOORBELL_CONTROL, F_ENABLE_DROP,
  2348. F_ENABLE_DROP);
  2349. /*
  2350. * SGE_FL_BUFFER_SIZE0 (RX_SMALL_PG_BUF) is set up by
  2351. * t4_fixup_host_params().
  2352. */
  2353. s->fl_pg_order = FL_PG_ORDER;
  2354. if (s->fl_pg_order)
  2355. t4_write_reg(adap,
  2356. SGE_FL_BUFFER_SIZE0+RX_LARGE_PG_BUF*sizeof(u32),
  2357. PAGE_SIZE << FL_PG_ORDER);
  2358. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_SMALL_MTU_BUF*sizeof(u32),
  2359. FL_MTU_SMALL_BUFSIZE(adap));
  2360. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0+RX_LARGE_MTU_BUF*sizeof(u32),
  2361. FL_MTU_LARGE_BUFSIZE(adap));
  2362. /*
  2363. * Note that the SGE Ingress Packet Count Interrupt Threshold and
  2364. * Timer Holdoff values must be supplied by our caller.
  2365. */
  2366. t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
  2367. THRESHOLD_0(s->counter_val[0]) |
  2368. THRESHOLD_1(s->counter_val[1]) |
  2369. THRESHOLD_2(s->counter_val[2]) |
  2370. THRESHOLD_3(s->counter_val[3]));
  2371. t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
  2372. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
  2373. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
  2374. t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
  2375. TIMERVALUE2(us_to_core_ticks(adap, s->timer_val[2])) |
  2376. TIMERVALUE3(us_to_core_ticks(adap, s->timer_val[3])));
  2377. t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
  2378. TIMERVALUE4(us_to_core_ticks(adap, s->timer_val[4])) |
  2379. TIMERVALUE5(us_to_core_ticks(adap, s->timer_val[5])));
  2380. return 0;
  2381. }
  2382. int t4_sge_init(struct adapter *adap)
  2383. {
  2384. struct sge *s = &adap->sge;
  2385. u32 sge_control;
  2386. int ret;
  2387. /*
  2388. * Ingress Padding Boundary and Egress Status Page Size are set up by
  2389. * t4_fixup_host_params().
  2390. */
  2391. sge_control = t4_read_reg(adap, SGE_CONTROL);
  2392. s->pktshift = PKTSHIFT_GET(sge_control);
  2393. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_MASK) ? 128 : 64;
  2394. s->fl_align = 1 << (INGPADBOUNDARY_GET(sge_control) +
  2395. X_INGPADBOUNDARY_SHIFT);
  2396. if (adap->flags & USING_SOFT_PARAMS)
  2397. ret = t4_sge_init_soft(adap);
  2398. else
  2399. ret = t4_sge_init_hard(adap);
  2400. if (ret < 0)
  2401. return ret;
  2402. /*
  2403. * A FL with <= fl_starve_thres buffers is starving and a periodic
  2404. * timer will attempt to refill it. This needs to be larger than the
  2405. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  2406. * stuck waiting for new packets while the SGE is waiting for us to
  2407. * give it more Free List entries. (Note that the SGE's Egress
  2408. * Congestion Threshold is in units of 2 Free List pointers.)
  2409. */
  2410. s->fl_starve_thres
  2411. = EGRTHRESHOLD_GET(t4_read_reg(adap, SGE_CONM_CTRL))*2 + 1;
  2412. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
  2413. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
  2414. s->starve_thres = core_ticks_per_usec(adap) * 1000000; /* 1 s */
  2415. s->idma_state[0] = s->idma_state[1] = 0;
  2416. spin_lock_init(&s->intrq_lock);
  2417. return 0;
  2418. }