sge.c 83 KB

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