sge.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328
  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. #ifdef CONFIG_CHELSIO_T4_FCOE
  49. #include <scsi/fc/fc_fcoe.h>
  50. #endif /* CONFIG_CHELSIO_T4_FCOE */
  51. #include "cxgb4.h"
  52. #include "t4_regs.h"
  53. #include "t4_values.h"
  54. #include "t4_msg.h"
  55. #include "t4fw_api.h"
  56. /*
  57. * Rx buffer size. We use largish buffers if possible but settle for single
  58. * pages under memory shortage.
  59. */
  60. #if PAGE_SHIFT >= 16
  61. # define FL_PG_ORDER 0
  62. #else
  63. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  64. #endif
  65. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  66. #define RX_COPY_THRES 256
  67. #define RX_PULL_LEN 128
  68. /*
  69. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  70. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  71. */
  72. #define RX_PKT_SKB_LEN 512
  73. /*
  74. * Max number of Tx descriptors we clean up at a time. Should be modest as
  75. * freeing skbs isn't cheap and it happens while holding locks. We just need
  76. * to free packets faster than they arrive, we eventually catch up and keep
  77. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
  78. */
  79. #define MAX_TX_RECLAIM 16
  80. /*
  81. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  82. * allocating buffers isn't cheap either.
  83. */
  84. #define MAX_RX_REFILL 16U
  85. /*
  86. * Period of the Rx queue check timer. This timer is infrequent as it has
  87. * something to do only when the system experiences severe memory shortage.
  88. */
  89. #define RX_QCHECK_PERIOD (HZ / 2)
  90. /*
  91. * Period of the Tx queue check timer.
  92. */
  93. #define TX_QCHECK_PERIOD (HZ / 2)
  94. /*
  95. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  96. */
  97. #define MAX_TIMER_TX_RECLAIM 100
  98. /*
  99. * Timer index used when backing off due to memory shortage.
  100. */
  101. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  102. /*
  103. * Suspend an Ethernet Tx queue with fewer available descriptors than this.
  104. * This is the same as calc_tx_descs() for a TSO packet with
  105. * nr_frags == MAX_SKB_FRAGS.
  106. */
  107. #define ETHTXQ_STOP_THRES \
  108. (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
  109. /*
  110. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  111. * for a full sized WR.
  112. */
  113. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  114. /*
  115. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  116. * into a WR.
  117. */
  118. #define MAX_IMM_TX_PKT_LEN 256
  119. /*
  120. * Max size of a WR sent through a control Tx queue.
  121. */
  122. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  123. struct tx_sw_desc { /* SW state per Tx descriptor */
  124. struct sk_buff *skb;
  125. struct ulptx_sgl *sgl;
  126. };
  127. struct rx_sw_desc { /* SW state per Rx descriptor */
  128. struct page *page;
  129. dma_addr_t dma_addr;
  130. };
  131. /*
  132. * Rx buffer sizes for "useskbs" Free List buffers (one ingress packet pe skb
  133. * buffer). We currently only support two sizes for 1500- and 9000-byte MTUs.
  134. * We could easily support more but there doesn't seem to be much need for
  135. * that ...
  136. */
  137. #define FL_MTU_SMALL 1500
  138. #define FL_MTU_LARGE 9000
  139. static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
  140. unsigned int mtu)
  141. {
  142. struct sge *s = &adapter->sge;
  143. return ALIGN(s->pktshift + ETH_HLEN + VLAN_HLEN + mtu, s->fl_align);
  144. }
  145. #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
  146. #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
  147. /*
  148. * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
  149. * these to specify the buffer size as an index into the SGE Free List Buffer
  150. * Size register array. We also use bit 4, when the buffer has been unmapped
  151. * for DMA, but this is of course never sent to the hardware and is only used
  152. * to prevent double unmappings. All of the above requires that the Free List
  153. * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
  154. * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
  155. * Free List Buffer alignment is 32 bytes, this works out for us ...
  156. */
  157. enum {
  158. RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
  159. RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
  160. RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
  161. /*
  162. * XXX We shouldn't depend on being able to use these indices.
  163. * XXX Especially when some other Master PF has initialized the
  164. * XXX adapter or we use the Firmware Configuration File. We
  165. * XXX should really search through the Host Buffer Size register
  166. * XXX array for the appropriately sized buffer indices.
  167. */
  168. RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
  169. RX_LARGE_PG_BUF = 0x1, /* buffer large (FL_PG_ORDER) page buffer */
  170. RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
  171. RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
  172. };
  173. static int timer_pkt_quota[] = {1, 1, 2, 3, 4, 5};
  174. #define MIN_NAPI_WORK 1
  175. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  176. {
  177. return d->dma_addr & ~(dma_addr_t)RX_BUF_FLAGS;
  178. }
  179. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  180. {
  181. return !(d->dma_addr & RX_UNMAPPED_BUF);
  182. }
  183. /**
  184. * txq_avail - return the number of available slots in a Tx queue
  185. * @q: the Tx queue
  186. *
  187. * Returns the number of descriptors in a Tx queue available to write new
  188. * packets.
  189. */
  190. static inline unsigned int txq_avail(const struct sge_txq *q)
  191. {
  192. return q->size - 1 - q->in_use;
  193. }
  194. /**
  195. * fl_cap - return the capacity of a free-buffer list
  196. * @fl: the FL
  197. *
  198. * Returns the capacity of a free-buffer list. The capacity is less than
  199. * the size because one descriptor needs to be left unpopulated, otherwise
  200. * HW will think the FL is empty.
  201. */
  202. static inline unsigned int fl_cap(const struct sge_fl *fl)
  203. {
  204. return fl->size - 8; /* 1 descriptor = 8 buffers */
  205. }
  206. /**
  207. * fl_starving - return whether a Free List is starving.
  208. * @adapter: pointer to the adapter
  209. * @fl: the Free List
  210. *
  211. * Tests specified Free List to see whether the number of buffers
  212. * available to the hardware has falled below our "starvation"
  213. * threshold.
  214. */
  215. static inline bool fl_starving(const struct adapter *adapter,
  216. const struct sge_fl *fl)
  217. {
  218. const struct sge *s = &adapter->sge;
  219. return fl->avail - fl->pend_cred <= s->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. 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(ACCESS_ONCE(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. if (q->pend_cred >= 8) {
  462. u32 val = adap->params.arch.sge_fl_db;
  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. /* Make sure all memory writes to the Free List queue are
  468. * committed before we tell the hardware about them.
  469. */
  470. wmb();
  471. /* If we don't have access to the new User Doorbell (T5+), use
  472. * the old doorbell mechanism; otherwise use the new BAR2
  473. * mechanism.
  474. */
  475. if (unlikely(q->bar2_addr == NULL)) {
  476. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  477. val | QID_V(q->cntxt_id));
  478. } else {
  479. writel(val | QID_V(q->bar2_qid),
  480. q->bar2_addr + SGE_UDB_KDOORBELL);
  481. /* This Write memory Barrier will force the write to
  482. * the User Doorbell area to be flushed.
  483. */
  484. wmb();
  485. }
  486. q->pend_cred &= 7;
  487. }
  488. }
  489. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  490. dma_addr_t mapping)
  491. {
  492. sd->page = pg;
  493. sd->dma_addr = mapping; /* includes size low bits */
  494. }
  495. /**
  496. * refill_fl - refill an SGE Rx buffer ring
  497. * @adap: the adapter
  498. * @q: the ring to refill
  499. * @n: the number of new buffers to allocate
  500. * @gfp: the gfp flags for the allocations
  501. *
  502. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  503. * allocated with the supplied gfp flags. The caller must assure that
  504. * @n does not exceed the queue's capacity. If afterwards the queue is
  505. * found critically low mark it as starving in the bitmap of starving FLs.
  506. *
  507. * Returns the number of buffers allocated.
  508. */
  509. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  510. gfp_t gfp)
  511. {
  512. struct sge *s = &adap->sge;
  513. struct page *pg;
  514. dma_addr_t mapping;
  515. unsigned int cred = q->avail;
  516. __be64 *d = &q->desc[q->pidx];
  517. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  518. int node;
  519. #ifdef CONFIG_DEBUG_FS
  520. if (test_bit(q->cntxt_id - adap->sge.egr_start, adap->sge.blocked_fl))
  521. goto out;
  522. #endif
  523. gfp |= __GFP_NOWARN;
  524. node = dev_to_node(adap->pdev_dev);
  525. if (s->fl_pg_order == 0)
  526. goto alloc_small_pages;
  527. /*
  528. * Prefer large buffers
  529. */
  530. while (n) {
  531. pg = alloc_pages_node(node, gfp | __GFP_COMP, s->fl_pg_order);
  532. if (unlikely(!pg)) {
  533. q->large_alloc_failed++;
  534. break; /* fall back to single pages */
  535. }
  536. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  537. PAGE_SIZE << s->fl_pg_order,
  538. PCI_DMA_FROMDEVICE);
  539. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  540. __free_pages(pg, s->fl_pg_order);
  541. q->mapping_err++;
  542. goto out; /* do not try small pages for this error */
  543. }
  544. mapping |= RX_LARGE_PG_BUF;
  545. *d++ = cpu_to_be64(mapping);
  546. set_rx_sw_desc(sd, pg, mapping);
  547. sd++;
  548. q->avail++;
  549. if (++q->pidx == q->size) {
  550. q->pidx = 0;
  551. sd = q->sdesc;
  552. d = q->desc;
  553. }
  554. n--;
  555. }
  556. alloc_small_pages:
  557. while (n--) {
  558. pg = alloc_pages_node(node, gfp, 0);
  559. if (unlikely(!pg)) {
  560. q->alloc_failed++;
  561. break;
  562. }
  563. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  564. PCI_DMA_FROMDEVICE);
  565. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  566. put_page(pg);
  567. q->mapping_err++;
  568. goto out;
  569. }
  570. *d++ = cpu_to_be64(mapping);
  571. set_rx_sw_desc(sd, pg, mapping);
  572. sd++;
  573. q->avail++;
  574. if (++q->pidx == q->size) {
  575. q->pidx = 0;
  576. sd = q->sdesc;
  577. d = q->desc;
  578. }
  579. }
  580. out: cred = q->avail - cred;
  581. q->pend_cred += cred;
  582. ring_fl_db(adap, q);
  583. if (unlikely(fl_starving(adap, q))) {
  584. smp_wmb();
  585. q->low++;
  586. set_bit(q->cntxt_id - adap->sge.egr_start,
  587. adap->sge.starving_fl);
  588. }
  589. return cred;
  590. }
  591. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  592. {
  593. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  594. GFP_ATOMIC);
  595. }
  596. /**
  597. * alloc_ring - allocate resources for an SGE descriptor ring
  598. * @dev: the PCI device's core device
  599. * @nelem: the number of descriptors
  600. * @elem_size: the size of each descriptor
  601. * @sw_size: the size of the SW state associated with each ring element
  602. * @phys: the physical address of the allocated ring
  603. * @metadata: address of the array holding the SW state for the ring
  604. * @stat_size: extra space in HW ring for status information
  605. * @node: preferred node for memory allocations
  606. *
  607. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  608. * free buffer lists, or response queues. Each SGE ring requires
  609. * space for its HW descriptors plus, optionally, space for the SW state
  610. * associated with each HW entry (the metadata). The function returns
  611. * three values: the virtual address for the HW ring (the return value
  612. * of the function), the bus address of the HW ring, and the address
  613. * of the SW ring.
  614. */
  615. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  616. size_t sw_size, dma_addr_t *phys, void *metadata,
  617. size_t stat_size, int node)
  618. {
  619. size_t len = nelem * elem_size + stat_size;
  620. void *s = NULL;
  621. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  622. if (!p)
  623. return NULL;
  624. if (sw_size) {
  625. s = kzalloc_node(nelem * sw_size, GFP_KERNEL, node);
  626. if (!s) {
  627. dma_free_coherent(dev, len, p, *phys);
  628. return NULL;
  629. }
  630. }
  631. if (metadata)
  632. *(void **)metadata = s;
  633. memset(p, 0, len);
  634. return p;
  635. }
  636. /**
  637. * sgl_len - calculates the size of an SGL of the given capacity
  638. * @n: the number of SGL entries
  639. *
  640. * Calculates the number of flits needed for a scatter/gather list that
  641. * can hold the given number of entries.
  642. */
  643. static inline unsigned int sgl_len(unsigned int n)
  644. {
  645. /* A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
  646. * addresses. The DSGL Work Request starts off with a 32-bit DSGL
  647. * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
  648. * repeated sequences of { Length[i], Length[i+1], Address[i],
  649. * Address[i+1] } (this ensures that all addresses are on 64-bit
  650. * boundaries). If N is even, then Length[N+1] should be set to 0 and
  651. * Address[N+1] is omitted.
  652. *
  653. * The following calculation incorporates all of the above. It's
  654. * somewhat hard to follow but, briefly: the "+2" accounts for the
  655. * first two flits which include the DSGL header, Length0 and
  656. * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
  657. * flits for every pair of the remaining N) +1 if (n-1) is odd; and
  658. * finally the "+((n-1)&1)" adds the one remaining flit needed if
  659. * (n-1) is odd ...
  660. */
  661. n--;
  662. return (3 * n) / 2 + (n & 1) + 2;
  663. }
  664. /**
  665. * flits_to_desc - returns the num of Tx descriptors for the given flits
  666. * @n: the number of flits
  667. *
  668. * Returns the number of Tx descriptors needed for the supplied number
  669. * of flits.
  670. */
  671. static inline unsigned int flits_to_desc(unsigned int n)
  672. {
  673. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  674. return DIV_ROUND_UP(n, 8);
  675. }
  676. /**
  677. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  678. * @skb: the packet
  679. *
  680. * Returns whether an Ethernet packet is small enough to fit as
  681. * immediate data. Return value corresponds to headroom required.
  682. */
  683. static inline int is_eth_imm(const struct sk_buff *skb)
  684. {
  685. int hdrlen = skb_shinfo(skb)->gso_size ?
  686. sizeof(struct cpl_tx_pkt_lso_core) : 0;
  687. hdrlen += sizeof(struct cpl_tx_pkt);
  688. if (skb->len <= MAX_IMM_TX_PKT_LEN - hdrlen)
  689. return hdrlen;
  690. return 0;
  691. }
  692. /**
  693. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  694. * @skb: the packet
  695. *
  696. * Returns the number of flits needed for a Tx WR for the given Ethernet
  697. * packet, including the needed WR and CPL headers.
  698. */
  699. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  700. {
  701. unsigned int flits;
  702. int hdrlen = is_eth_imm(skb);
  703. /* If the skb is small enough, we can pump it out as a work request
  704. * with only immediate data. In that case we just have to have the
  705. * TX Packet header plus the skb data in the Work Request.
  706. */
  707. if (hdrlen)
  708. return DIV_ROUND_UP(skb->len + hdrlen, sizeof(__be64));
  709. /* Otherwise, we're going to have to construct a Scatter gather list
  710. * of the skb body and fragments. We also include the flits necessary
  711. * for the TX Packet Work Request and CPL. We always have a firmware
  712. * Write Header (incorporated as part of the cpl_tx_pkt_lso and
  713. * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
  714. * message or, if we're doing a Large Send Offload, an LSO CPL message
  715. * with an embedded TX Packet Write CPL message.
  716. */
  717. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1);
  718. if (skb_shinfo(skb)->gso_size)
  719. flits += (sizeof(struct fw_eth_tx_pkt_wr) +
  720. sizeof(struct cpl_tx_pkt_lso_core) +
  721. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  722. else
  723. flits += (sizeof(struct fw_eth_tx_pkt_wr) +
  724. sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
  725. return flits;
  726. }
  727. /**
  728. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  729. * @skb: the packet
  730. *
  731. * Returns the number of Tx descriptors needed for the given Ethernet
  732. * packet, including the needed WR and CPL headers.
  733. */
  734. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  735. {
  736. return flits_to_desc(calc_tx_flits(skb));
  737. }
  738. /**
  739. * write_sgl - populate a scatter/gather list for a packet
  740. * @skb: the packet
  741. * @q: the Tx queue we are writing into
  742. * @sgl: starting location for writing the SGL
  743. * @end: points right after the end of the SGL
  744. * @start: start offset into skb main-body data to include in the SGL
  745. * @addr: the list of bus addresses for the SGL elements
  746. *
  747. * Generates a gather list for the buffers that make up a packet.
  748. * The caller must provide adequate space for the SGL that will be written.
  749. * The SGL includes all of the packet's page fragments and the data in its
  750. * main body except for the first @start bytes. @sgl must be 16-byte
  751. * aligned and within a Tx descriptor with available space. @end points
  752. * right after the end of the SGL but does not account for any potential
  753. * wrap around, i.e., @end > @sgl.
  754. */
  755. static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  756. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  757. const dma_addr_t *addr)
  758. {
  759. unsigned int i, len;
  760. struct ulptx_sge_pair *to;
  761. const struct skb_shared_info *si = skb_shinfo(skb);
  762. unsigned int nfrags = si->nr_frags;
  763. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  764. len = skb_headlen(skb) - start;
  765. if (likely(len)) {
  766. sgl->len0 = htonl(len);
  767. sgl->addr0 = cpu_to_be64(addr[0] + start);
  768. nfrags++;
  769. } else {
  770. sgl->len0 = htonl(skb_frag_size(&si->frags[0]));
  771. sgl->addr0 = cpu_to_be64(addr[1]);
  772. }
  773. sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
  774. ULPTX_NSGE_V(nfrags));
  775. if (likely(--nfrags == 0))
  776. return;
  777. /*
  778. * Most of the complexity below deals with the possibility we hit the
  779. * end of the queue in the middle of writing the SGL. For this case
  780. * only we create the SGL in a temporary buffer and then copy it.
  781. */
  782. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  783. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  784. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  785. to->len[1] = cpu_to_be32(skb_frag_size(&si->frags[++i]));
  786. to->addr[0] = cpu_to_be64(addr[i]);
  787. to->addr[1] = cpu_to_be64(addr[++i]);
  788. }
  789. if (nfrags) {
  790. to->len[0] = cpu_to_be32(skb_frag_size(&si->frags[i]));
  791. to->len[1] = cpu_to_be32(0);
  792. to->addr[0] = cpu_to_be64(addr[i + 1]);
  793. }
  794. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  795. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  796. if (likely(part0))
  797. memcpy(sgl->sge, buf, part0);
  798. part1 = (u8 *)end - (u8 *)q->stat;
  799. memcpy(q->desc, (u8 *)buf + part0, part1);
  800. end = (void *)q->desc + part1;
  801. }
  802. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  803. *end = 0;
  804. }
  805. /* This function copies 64 byte coalesced work request to
  806. * memory mapped BAR2 space. For coalesced WR SGE fetches
  807. * data from the FIFO instead of from Host.
  808. */
  809. static void cxgb_pio_copy(u64 __iomem *dst, u64 *src)
  810. {
  811. int count = 8;
  812. while (count) {
  813. writeq(*src, dst);
  814. src++;
  815. dst++;
  816. count--;
  817. }
  818. }
  819. /**
  820. * ring_tx_db - check and potentially ring a Tx queue's doorbell
  821. * @adap: the adapter
  822. * @q: the Tx queue
  823. * @n: number of new descriptors to give to HW
  824. *
  825. * Ring the doorbel for a Tx queue.
  826. */
  827. static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  828. {
  829. /* Make sure that all writes to the TX Descriptors are committed
  830. * before we tell the hardware about them.
  831. */
  832. wmb();
  833. /* If we don't have access to the new User Doorbell (T5+), use the old
  834. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  835. */
  836. if (unlikely(q->bar2_addr == NULL)) {
  837. u32 val = PIDX_V(n);
  838. unsigned long flags;
  839. /* For T4 we need to participate in the Doorbell Recovery
  840. * mechanism.
  841. */
  842. spin_lock_irqsave(&q->db_lock, flags);
  843. if (!q->db_disabled)
  844. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
  845. QID_V(q->cntxt_id) | val);
  846. else
  847. q->db_pidx_inc += n;
  848. q->db_pidx = q->pidx;
  849. spin_unlock_irqrestore(&q->db_lock, flags);
  850. } else {
  851. u32 val = PIDX_T5_V(n);
  852. /* T4 and later chips share the same PIDX field offset within
  853. * the doorbell, but T5 and later shrank the field in order to
  854. * gain a bit for Doorbell Priority. The field was absurdly
  855. * large in the first place (14 bits) so we just use the T5
  856. * and later limits and warn if a Queue ID is too large.
  857. */
  858. WARN_ON(val & DBPRIO_F);
  859. /* If we're only writing a single TX Descriptor and we can use
  860. * Inferred QID registers, we can use the Write Combining
  861. * Gather Buffer; otherwise we use the simple doorbell.
  862. */
  863. if (n == 1 && q->bar2_qid == 0) {
  864. int index = (q->pidx
  865. ? (q->pidx - 1)
  866. : (q->size - 1));
  867. u64 *wr = (u64 *)&q->desc[index];
  868. cxgb_pio_copy((u64 __iomem *)
  869. (q->bar2_addr + SGE_UDB_WCDOORBELL),
  870. wr);
  871. } else {
  872. writel(val | QID_V(q->bar2_qid),
  873. q->bar2_addr + SGE_UDB_KDOORBELL);
  874. }
  875. /* This Write Memory Barrier will force the write to the User
  876. * Doorbell area to be flushed. This is needed to prevent
  877. * writes on different CPUs for the same queue from hitting
  878. * the adapter out of order. This is required when some Work
  879. * Requests take the Write Combine Gather Buffer path (user
  880. * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
  881. * take the traditional path where we simply increment the
  882. * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
  883. * hardware DMA read the actual Work Request.
  884. */
  885. wmb();
  886. }
  887. }
  888. /**
  889. * inline_tx_skb - inline a packet's data into Tx descriptors
  890. * @skb: the packet
  891. * @q: the Tx queue where the packet will be inlined
  892. * @pos: starting position in the Tx queue where to inline the packet
  893. *
  894. * Inline a packet's contents directly into Tx descriptors, starting at
  895. * the given position within the Tx DMA ring.
  896. * Most of the complexity of this operation is dealing with wrap arounds
  897. * in the middle of the packet we want to inline.
  898. */
  899. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
  900. void *pos)
  901. {
  902. u64 *p;
  903. int left = (void *)q->stat - pos;
  904. if (likely(skb->len <= left)) {
  905. if (likely(!skb->data_len))
  906. skb_copy_from_linear_data(skb, pos, skb->len);
  907. else
  908. skb_copy_bits(skb, 0, pos, skb->len);
  909. pos += skb->len;
  910. } else {
  911. skb_copy_bits(skb, 0, pos, left);
  912. skb_copy_bits(skb, left, q->desc, skb->len - left);
  913. pos = (void *)q->desc + (skb->len - left);
  914. }
  915. /* 0-pad to multiple of 16 */
  916. p = PTR_ALIGN(pos, 8);
  917. if ((uintptr_t)p & 8)
  918. *p = 0;
  919. }
  920. static void *inline_tx_skb_header(const struct sk_buff *skb,
  921. const struct sge_txq *q, void *pos,
  922. int length)
  923. {
  924. u64 *p;
  925. int left = (void *)q->stat - pos;
  926. if (likely(length <= left)) {
  927. memcpy(pos, skb->data, length);
  928. pos += length;
  929. } else {
  930. memcpy(pos, skb->data, left);
  931. memcpy(q->desc, skb->data + left, length - left);
  932. pos = (void *)q->desc + (length - left);
  933. }
  934. /* 0-pad to multiple of 16 */
  935. p = PTR_ALIGN(pos, 8);
  936. if ((uintptr_t)p & 8) {
  937. *p = 0;
  938. return p + 1;
  939. }
  940. return p;
  941. }
  942. /*
  943. * Figure out what HW csum a packet wants and return the appropriate control
  944. * bits.
  945. */
  946. static u64 hwcsum(enum chip_type chip, const struct sk_buff *skb)
  947. {
  948. int csum_type;
  949. const struct iphdr *iph = ip_hdr(skb);
  950. if (iph->version == 4) {
  951. if (iph->protocol == IPPROTO_TCP)
  952. csum_type = TX_CSUM_TCPIP;
  953. else if (iph->protocol == IPPROTO_UDP)
  954. csum_type = TX_CSUM_UDPIP;
  955. else {
  956. nocsum: /*
  957. * unknown protocol, disable HW csum
  958. * and hope a bad packet is detected
  959. */
  960. return TXPKT_L4CSUM_DIS_F;
  961. }
  962. } else {
  963. /*
  964. * this doesn't work with extension headers
  965. */
  966. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  967. if (ip6h->nexthdr == IPPROTO_TCP)
  968. csum_type = TX_CSUM_TCPIP6;
  969. else if (ip6h->nexthdr == IPPROTO_UDP)
  970. csum_type = TX_CSUM_UDPIP6;
  971. else
  972. goto nocsum;
  973. }
  974. if (likely(csum_type >= TX_CSUM_TCPIP)) {
  975. u64 hdr_len = TXPKT_IPHDR_LEN_V(skb_network_header_len(skb));
  976. int eth_hdr_len = skb_network_offset(skb) - ETH_HLEN;
  977. if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
  978. hdr_len |= TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  979. else
  980. hdr_len |= T6_TXPKT_ETHHDR_LEN_V(eth_hdr_len);
  981. return TXPKT_CSUM_TYPE_V(csum_type) | hdr_len;
  982. } else {
  983. int start = skb_transport_offset(skb);
  984. return TXPKT_CSUM_TYPE_V(csum_type) |
  985. TXPKT_CSUM_START_V(start) |
  986. TXPKT_CSUM_LOC_V(start + skb->csum_offset);
  987. }
  988. }
  989. static void eth_txq_stop(struct sge_eth_txq *q)
  990. {
  991. netif_tx_stop_queue(q->txq);
  992. q->q.stops++;
  993. }
  994. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  995. {
  996. q->in_use += n;
  997. q->pidx += n;
  998. if (q->pidx >= q->size)
  999. q->pidx -= q->size;
  1000. }
  1001. #ifdef CONFIG_CHELSIO_T4_FCOE
  1002. static inline int
  1003. cxgb_fcoe_offload(struct sk_buff *skb, struct adapter *adap,
  1004. const struct port_info *pi, u64 *cntrl)
  1005. {
  1006. const struct cxgb_fcoe *fcoe = &pi->fcoe;
  1007. if (!(fcoe->flags & CXGB_FCOE_ENABLED))
  1008. return 0;
  1009. if (skb->protocol != htons(ETH_P_FCOE))
  1010. return 0;
  1011. skb_reset_mac_header(skb);
  1012. skb->mac_len = sizeof(struct ethhdr);
  1013. skb_set_network_header(skb, skb->mac_len);
  1014. skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
  1015. if (!cxgb_fcoe_sof_eof_supported(adap, skb))
  1016. return -ENOTSUPP;
  1017. /* FC CRC offload */
  1018. *cntrl = TXPKT_CSUM_TYPE_V(TX_CSUM_FCOE) |
  1019. TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F |
  1020. TXPKT_CSUM_START_V(CXGB_FCOE_TXPKT_CSUM_START) |
  1021. TXPKT_CSUM_END_V(CXGB_FCOE_TXPKT_CSUM_END) |
  1022. TXPKT_CSUM_LOC_V(CXGB_FCOE_TXPKT_CSUM_END);
  1023. return 0;
  1024. }
  1025. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1026. /**
  1027. * t4_eth_xmit - add a packet to an Ethernet Tx queue
  1028. * @skb: the packet
  1029. * @dev: the egress net device
  1030. *
  1031. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  1032. */
  1033. netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  1034. {
  1035. u32 wr_mid, ctrl0;
  1036. u64 cntrl, *end;
  1037. int qidx, credits;
  1038. unsigned int flits, ndesc;
  1039. struct adapter *adap;
  1040. struct sge_eth_txq *q;
  1041. const struct port_info *pi;
  1042. struct fw_eth_tx_pkt_wr *wr;
  1043. struct cpl_tx_pkt_core *cpl;
  1044. const struct skb_shared_info *ssi;
  1045. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  1046. bool immediate = false;
  1047. int len, max_pkt_len;
  1048. #ifdef CONFIG_CHELSIO_T4_FCOE
  1049. int err;
  1050. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1051. /*
  1052. * The chip min packet length is 10 octets but play safe and reject
  1053. * anything shorter than an Ethernet header.
  1054. */
  1055. if (unlikely(skb->len < ETH_HLEN)) {
  1056. out_free: dev_kfree_skb_any(skb);
  1057. return NETDEV_TX_OK;
  1058. }
  1059. /* Discard the packet if the length is greater than mtu */
  1060. max_pkt_len = ETH_HLEN + dev->mtu;
  1061. if (skb_vlan_tagged(skb))
  1062. max_pkt_len += VLAN_HLEN;
  1063. if (!skb_shinfo(skb)->gso_size && (unlikely(skb->len > max_pkt_len)))
  1064. goto out_free;
  1065. pi = netdev_priv(dev);
  1066. adap = pi->adapter;
  1067. qidx = skb_get_queue_mapping(skb);
  1068. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  1069. reclaim_completed_tx(adap, &q->q, true);
  1070. cntrl = TXPKT_L4CSUM_DIS_F | TXPKT_IPCSUM_DIS_F;
  1071. #ifdef CONFIG_CHELSIO_T4_FCOE
  1072. err = cxgb_fcoe_offload(skb, adap, pi, &cntrl);
  1073. if (unlikely(err == -ENOTSUPP))
  1074. goto out_free;
  1075. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1076. flits = calc_tx_flits(skb);
  1077. ndesc = flits_to_desc(flits);
  1078. credits = txq_avail(&q->q) - ndesc;
  1079. if (unlikely(credits < 0)) {
  1080. eth_txq_stop(q);
  1081. dev_err(adap->pdev_dev,
  1082. "%s: Tx ring %u full while queue awake!\n",
  1083. dev->name, qidx);
  1084. return NETDEV_TX_BUSY;
  1085. }
  1086. if (is_eth_imm(skb))
  1087. immediate = true;
  1088. if (!immediate &&
  1089. unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
  1090. q->mapping_err++;
  1091. goto out_free;
  1092. }
  1093. wr_mid = FW_WR_LEN16_V(DIV_ROUND_UP(flits, 2));
  1094. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  1095. eth_txq_stop(q);
  1096. wr_mid |= FW_WR_EQUEQ_F | FW_WR_EQUIQ_F;
  1097. }
  1098. wr = (void *)&q->q.desc[q->q.pidx];
  1099. wr->equiq_to_len16 = htonl(wr_mid);
  1100. wr->r3 = cpu_to_be64(0);
  1101. end = (u64 *)wr + flits;
  1102. len = immediate ? skb->len : 0;
  1103. ssi = skb_shinfo(skb);
  1104. if (ssi->gso_size) {
  1105. struct cpl_tx_pkt_lso *lso = (void *)wr;
  1106. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  1107. int l3hdr_len = skb_network_header_len(skb);
  1108. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  1109. len += sizeof(*lso);
  1110. wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
  1111. FW_WR_IMMDLEN_V(len));
  1112. lso->c.lso_ctrl = htonl(LSO_OPCODE_V(CPL_TX_PKT_LSO) |
  1113. LSO_FIRST_SLICE_F | LSO_LAST_SLICE_F |
  1114. LSO_IPV6_V(v6) |
  1115. LSO_ETHHDR_LEN_V(eth_xtra_len / 4) |
  1116. LSO_IPHDR_LEN_V(l3hdr_len / 4) |
  1117. LSO_TCPHDR_LEN_V(tcp_hdr(skb)->doff));
  1118. lso->c.ipid_ofst = htons(0);
  1119. lso->c.mss = htons(ssi->gso_size);
  1120. lso->c.seqno_offset = htonl(0);
  1121. if (is_t4(adap->params.chip))
  1122. lso->c.len = htonl(skb->len);
  1123. else
  1124. lso->c.len = htonl(LSO_T5_XFER_SIZE_V(skb->len));
  1125. cpl = (void *)(lso + 1);
  1126. if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
  1127. cntrl = TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1128. else
  1129. cntrl = T6_TXPKT_ETHHDR_LEN_V(eth_xtra_len);
  1130. cntrl |= TXPKT_CSUM_TYPE_V(v6 ?
  1131. TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  1132. TXPKT_IPHDR_LEN_V(l3hdr_len);
  1133. q->tso++;
  1134. q->tx_cso += ssi->gso_segs;
  1135. } else {
  1136. len += sizeof(*cpl);
  1137. wr->op_immdlen = htonl(FW_WR_OP_V(FW_ETH_TX_PKT_WR) |
  1138. FW_WR_IMMDLEN_V(len));
  1139. cpl = (void *)(wr + 1);
  1140. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1141. cntrl = hwcsum(adap->params.chip, skb) |
  1142. TXPKT_IPCSUM_DIS_F;
  1143. q->tx_cso++;
  1144. }
  1145. }
  1146. if (skb_vlan_tag_present(skb)) {
  1147. q->vlan_ins++;
  1148. cntrl |= TXPKT_VLAN_VLD_F | TXPKT_VLAN_V(skb_vlan_tag_get(skb));
  1149. #ifdef CONFIG_CHELSIO_T4_FCOE
  1150. if (skb->protocol == htons(ETH_P_FCOE))
  1151. cntrl |= TXPKT_VLAN_V(
  1152. ((skb->priority & 0x7) << VLAN_PRIO_SHIFT));
  1153. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1154. }
  1155. ctrl0 = TXPKT_OPCODE_V(CPL_TX_PKT_XT) | TXPKT_INTF_V(pi->tx_chan) |
  1156. TXPKT_PF_V(adap->pf);
  1157. #ifdef CONFIG_CHELSIO_T4_DCB
  1158. if (is_t4(adap->params.chip))
  1159. ctrl0 |= TXPKT_OVLAN_IDX_V(q->dcb_prio);
  1160. else
  1161. ctrl0 |= TXPKT_T5_OVLAN_IDX_V(q->dcb_prio);
  1162. #endif
  1163. cpl->ctrl0 = htonl(ctrl0);
  1164. cpl->pack = htons(0);
  1165. cpl->len = htons(skb->len);
  1166. cpl->ctrl1 = cpu_to_be64(cntrl);
  1167. if (immediate) {
  1168. inline_tx_skb(skb, &q->q, cpl + 1);
  1169. dev_consume_skb_any(skb);
  1170. } else {
  1171. int last_desc;
  1172. write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
  1173. addr);
  1174. skb_orphan(skb);
  1175. last_desc = q->q.pidx + ndesc - 1;
  1176. if (last_desc >= q->q.size)
  1177. last_desc -= q->q.size;
  1178. q->q.sdesc[last_desc].skb = skb;
  1179. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
  1180. }
  1181. txq_advance(&q->q, ndesc);
  1182. ring_tx_db(adap, &q->q, ndesc);
  1183. return NETDEV_TX_OK;
  1184. }
  1185. /**
  1186. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  1187. * @q: the SGE control Tx queue
  1188. *
  1189. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  1190. * that send only immediate data (presently just the control queues) and
  1191. * thus do not have any sk_buffs to release.
  1192. */
  1193. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1194. {
  1195. int hw_cidx = ntohs(ACCESS_ONCE(q->stat->cidx));
  1196. int reclaim = hw_cidx - q->cidx;
  1197. if (reclaim < 0)
  1198. reclaim += q->size;
  1199. q->in_use -= reclaim;
  1200. q->cidx = hw_cidx;
  1201. }
  1202. /**
  1203. * is_imm - check whether a packet can be sent as immediate data
  1204. * @skb: the packet
  1205. *
  1206. * Returns true if a packet can be sent as a WR with immediate data.
  1207. */
  1208. static inline int is_imm(const struct sk_buff *skb)
  1209. {
  1210. return skb->len <= MAX_CTRL_WR_LEN;
  1211. }
  1212. /**
  1213. * ctrlq_check_stop - check if a control queue is full and should stop
  1214. * @q: the queue
  1215. * @wr: most recent WR written to the queue
  1216. *
  1217. * Check if a control queue has become full and should be stopped.
  1218. * We clean up control queue descriptors very lazily, only when we are out.
  1219. * If the queue is still full after reclaiming any completed descriptors
  1220. * we suspend it and have the last WR wake it up.
  1221. */
  1222. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  1223. {
  1224. reclaim_completed_tx_imm(&q->q);
  1225. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1226. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1227. q->q.stops++;
  1228. q->full = 1;
  1229. }
  1230. }
  1231. /**
  1232. * ctrl_xmit - send a packet through an SGE control Tx queue
  1233. * @q: the control queue
  1234. * @skb: the packet
  1235. *
  1236. * Send a packet through an SGE control Tx queue. Packets sent through
  1237. * a control queue must fit entirely as immediate data.
  1238. */
  1239. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  1240. {
  1241. unsigned int ndesc;
  1242. struct fw_wr_hdr *wr;
  1243. if (unlikely(!is_imm(skb))) {
  1244. WARN_ON(1);
  1245. dev_kfree_skb(skb);
  1246. return NET_XMIT_DROP;
  1247. }
  1248. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  1249. spin_lock(&q->sendq.lock);
  1250. if (unlikely(q->full)) {
  1251. skb->priority = ndesc; /* save for restart */
  1252. __skb_queue_tail(&q->sendq, skb);
  1253. spin_unlock(&q->sendq.lock);
  1254. return NET_XMIT_CN;
  1255. }
  1256. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1257. inline_tx_skb(skb, &q->q, wr);
  1258. txq_advance(&q->q, ndesc);
  1259. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  1260. ctrlq_check_stop(q, wr);
  1261. ring_tx_db(q->adap, &q->q, ndesc);
  1262. spin_unlock(&q->sendq.lock);
  1263. kfree_skb(skb);
  1264. return NET_XMIT_SUCCESS;
  1265. }
  1266. /**
  1267. * restart_ctrlq - restart a suspended control queue
  1268. * @data: the control queue to restart
  1269. *
  1270. * Resumes transmission on a suspended Tx control queue.
  1271. */
  1272. static void restart_ctrlq(unsigned long data)
  1273. {
  1274. struct sk_buff *skb;
  1275. unsigned int written = 0;
  1276. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  1277. spin_lock(&q->sendq.lock);
  1278. reclaim_completed_tx_imm(&q->q);
  1279. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  1280. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  1281. struct fw_wr_hdr *wr;
  1282. unsigned int ndesc = skb->priority; /* previously saved */
  1283. written += ndesc;
  1284. /* Write descriptors and free skbs outside the lock to limit
  1285. * wait times. q->full is still set so new skbs will be queued.
  1286. */
  1287. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  1288. txq_advance(&q->q, ndesc);
  1289. spin_unlock(&q->sendq.lock);
  1290. inline_tx_skb(skb, &q->q, wr);
  1291. kfree_skb(skb);
  1292. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1293. unsigned long old = q->q.stops;
  1294. ctrlq_check_stop(q, wr);
  1295. if (q->q.stops != old) { /* suspended anew */
  1296. spin_lock(&q->sendq.lock);
  1297. goto ringdb;
  1298. }
  1299. }
  1300. if (written > 16) {
  1301. ring_tx_db(q->adap, &q->q, written);
  1302. written = 0;
  1303. }
  1304. spin_lock(&q->sendq.lock);
  1305. }
  1306. q->full = 0;
  1307. ringdb: if (written)
  1308. ring_tx_db(q->adap, &q->q, written);
  1309. spin_unlock(&q->sendq.lock);
  1310. }
  1311. /**
  1312. * t4_mgmt_tx - send a management message
  1313. * @adap: the adapter
  1314. * @skb: the packet containing the management message
  1315. *
  1316. * Send a management message through control queue 0.
  1317. */
  1318. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1319. {
  1320. int ret;
  1321. local_bh_disable();
  1322. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1323. local_bh_enable();
  1324. return ret;
  1325. }
  1326. /**
  1327. * is_ofld_imm - check whether a packet can be sent as immediate data
  1328. * @skb: the packet
  1329. *
  1330. * Returns true if a packet can be sent as an offload WR with immediate
  1331. * data. We currently use the same limit as for Ethernet packets.
  1332. */
  1333. static inline int is_ofld_imm(const struct sk_buff *skb)
  1334. {
  1335. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1336. }
  1337. /**
  1338. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1339. * @skb: the packet
  1340. *
  1341. * Returns the number of flits needed for the given offload packet.
  1342. * These packets are already fully constructed and no additional headers
  1343. * will be added.
  1344. */
  1345. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1346. {
  1347. unsigned int flits, cnt;
  1348. if (is_ofld_imm(skb))
  1349. return DIV_ROUND_UP(skb->len, 8);
  1350. flits = skb_transport_offset(skb) / 8U; /* headers */
  1351. cnt = skb_shinfo(skb)->nr_frags;
  1352. if (skb_tail_pointer(skb) != skb_transport_header(skb))
  1353. cnt++;
  1354. return flits + sgl_len(cnt);
  1355. }
  1356. /**
  1357. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1358. * @adap: the adapter
  1359. * @q: the queue to stop
  1360. *
  1361. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1362. * inability to map packets. A periodic timer attempts to restart
  1363. * queues so marked.
  1364. */
  1365. static void txq_stop_maperr(struct sge_uld_txq *q)
  1366. {
  1367. q->mapping_err++;
  1368. q->q.stops++;
  1369. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1370. q->adap->sge.txq_maperr);
  1371. }
  1372. /**
  1373. * ofldtxq_stop - stop an offload Tx queue that has become full
  1374. * @q: the queue to stop
  1375. * @skb: the packet causing the queue to become full
  1376. *
  1377. * Stops an offload Tx queue that has become full and modifies the packet
  1378. * being written to request a wakeup.
  1379. */
  1380. static void ofldtxq_stop(struct sge_uld_txq *q, struct sk_buff *skb)
  1381. {
  1382. struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
  1383. wr->lo |= htonl(FW_WR_EQUEQ_F | FW_WR_EQUIQ_F);
  1384. q->q.stops++;
  1385. q->full = 1;
  1386. }
  1387. /**
  1388. * service_ofldq - service/restart a suspended offload queue
  1389. * @q: the offload queue
  1390. *
  1391. * Services an offload Tx queue by moving packets from its Pending Send
  1392. * Queue to the Hardware TX ring. The function starts and ends with the
  1393. * Send Queue locked, but drops the lock while putting the skb at the
  1394. * head of the Send Queue onto the Hardware TX Ring. Dropping the lock
  1395. * allows more skbs to be added to the Send Queue by other threads.
  1396. * The packet being processed at the head of the Pending Send Queue is
  1397. * left on the queue in case we experience DMA Mapping errors, etc.
  1398. * and need to give up and restart later.
  1399. *
  1400. * service_ofldq() can be thought of as a task which opportunistically
  1401. * uses other threads execution contexts. We use the Offload Queue
  1402. * boolean "service_ofldq_running" to make sure that only one instance
  1403. * is ever running at a time ...
  1404. */
  1405. static void service_ofldq(struct sge_uld_txq *q)
  1406. {
  1407. u64 *pos, *before, *end;
  1408. int credits;
  1409. struct sk_buff *skb;
  1410. struct sge_txq *txq;
  1411. unsigned int left;
  1412. unsigned int written = 0;
  1413. unsigned int flits, ndesc;
  1414. /* If another thread is currently in service_ofldq() processing the
  1415. * Pending Send Queue then there's nothing to do. Otherwise, flag
  1416. * that we're doing the work and continue. Examining/modifying
  1417. * the Offload Queue boolean "service_ofldq_running" must be done
  1418. * while holding the Pending Send Queue Lock.
  1419. */
  1420. if (q->service_ofldq_running)
  1421. return;
  1422. q->service_ofldq_running = true;
  1423. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1424. /* We drop the lock while we're working with the skb at the
  1425. * head of the Pending Send Queue. This allows more skbs to
  1426. * be added to the Pending Send Queue while we're working on
  1427. * this one. We don't need to lock to guard the TX Ring
  1428. * updates because only one thread of execution is ever
  1429. * allowed into service_ofldq() at a time.
  1430. */
  1431. spin_unlock(&q->sendq.lock);
  1432. reclaim_completed_tx(q->adap, &q->q, false);
  1433. flits = skb->priority; /* previously saved */
  1434. ndesc = flits_to_desc(flits);
  1435. credits = txq_avail(&q->q) - ndesc;
  1436. BUG_ON(credits < 0);
  1437. if (unlikely(credits < TXQ_STOP_THRES))
  1438. ofldtxq_stop(q, skb);
  1439. pos = (u64 *)&q->q.desc[q->q.pidx];
  1440. if (is_ofld_imm(skb))
  1441. inline_tx_skb(skb, &q->q, pos);
  1442. else if (map_skb(q->adap->pdev_dev, skb,
  1443. (dma_addr_t *)skb->head)) {
  1444. txq_stop_maperr(q);
  1445. spin_lock(&q->sendq.lock);
  1446. break;
  1447. } else {
  1448. int last_desc, hdr_len = skb_transport_offset(skb);
  1449. /* The WR headers may not fit within one descriptor.
  1450. * So we need to deal with wrap-around here.
  1451. */
  1452. before = (u64 *)pos;
  1453. end = (u64 *)pos + flits;
  1454. txq = &q->q;
  1455. pos = (void *)inline_tx_skb_header(skb, &q->q,
  1456. (void *)pos,
  1457. hdr_len);
  1458. if (before > (u64 *)pos) {
  1459. left = (u8 *)end - (u8 *)txq->stat;
  1460. end = (void *)txq->desc + left;
  1461. }
  1462. /* If current position is already at the end of the
  1463. * ofld queue, reset the current to point to
  1464. * start of the queue and update the end ptr as well.
  1465. */
  1466. if (pos == (u64 *)txq->stat) {
  1467. left = (u8 *)end - (u8 *)txq->stat;
  1468. end = (void *)txq->desc + left;
  1469. pos = (void *)txq->desc;
  1470. }
  1471. write_sgl(skb, &q->q, (void *)pos,
  1472. end, hdr_len,
  1473. (dma_addr_t *)skb->head);
  1474. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1475. skb->dev = q->adap->port[0];
  1476. skb->destructor = deferred_unmap_destructor;
  1477. #endif
  1478. last_desc = q->q.pidx + ndesc - 1;
  1479. if (last_desc >= q->q.size)
  1480. last_desc -= q->q.size;
  1481. q->q.sdesc[last_desc].skb = skb;
  1482. }
  1483. txq_advance(&q->q, ndesc);
  1484. written += ndesc;
  1485. if (unlikely(written > 32)) {
  1486. ring_tx_db(q->adap, &q->q, written);
  1487. written = 0;
  1488. }
  1489. /* Reacquire the Pending Send Queue Lock so we can unlink the
  1490. * skb we've just successfully transferred to the TX Ring and
  1491. * loop for the next skb which may be at the head of the
  1492. * Pending Send Queue.
  1493. */
  1494. spin_lock(&q->sendq.lock);
  1495. __skb_unlink(skb, &q->sendq);
  1496. if (is_ofld_imm(skb))
  1497. kfree_skb(skb);
  1498. }
  1499. if (likely(written))
  1500. ring_tx_db(q->adap, &q->q, written);
  1501. /*Indicate that no thread is processing the Pending Send Queue
  1502. * currently.
  1503. */
  1504. q->service_ofldq_running = false;
  1505. }
  1506. /**
  1507. * ofld_xmit - send a packet through an offload queue
  1508. * @q: the Tx offload queue
  1509. * @skb: the packet
  1510. *
  1511. * Send an offload packet through an SGE offload queue.
  1512. */
  1513. static int ofld_xmit(struct sge_uld_txq *q, struct sk_buff *skb)
  1514. {
  1515. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1516. spin_lock(&q->sendq.lock);
  1517. /* Queue the new skb onto the Offload Queue's Pending Send Queue. If
  1518. * that results in this new skb being the only one on the queue, start
  1519. * servicing it. If there are other skbs already on the list, then
  1520. * either the queue is currently being processed or it's been stopped
  1521. * for some reason and it'll be restarted at a later time. Restart
  1522. * paths are triggered by events like experiencing a DMA Mapping Error
  1523. * or filling the Hardware TX Ring.
  1524. */
  1525. __skb_queue_tail(&q->sendq, skb);
  1526. if (q->sendq.qlen == 1)
  1527. service_ofldq(q);
  1528. spin_unlock(&q->sendq.lock);
  1529. return NET_XMIT_SUCCESS;
  1530. }
  1531. /**
  1532. * restart_ofldq - restart a suspended offload queue
  1533. * @data: the offload queue to restart
  1534. *
  1535. * Resumes transmission on a suspended Tx offload queue.
  1536. */
  1537. static void restart_ofldq(unsigned long data)
  1538. {
  1539. struct sge_uld_txq *q = (struct sge_uld_txq *)data;
  1540. spin_lock(&q->sendq.lock);
  1541. q->full = 0; /* the queue actually is completely empty now */
  1542. service_ofldq(q);
  1543. spin_unlock(&q->sendq.lock);
  1544. }
  1545. /**
  1546. * skb_txq - return the Tx queue an offload packet should use
  1547. * @skb: the packet
  1548. *
  1549. * Returns the Tx queue an offload packet should use as indicated by bits
  1550. * 1-15 in the packet's queue_mapping.
  1551. */
  1552. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1553. {
  1554. return skb->queue_mapping >> 1;
  1555. }
  1556. /**
  1557. * is_ctrl_pkt - return whether an offload packet is a control packet
  1558. * @skb: the packet
  1559. *
  1560. * Returns whether an offload packet should use an OFLD or a CTRL
  1561. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1562. */
  1563. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1564. {
  1565. return skb->queue_mapping & 1;
  1566. }
  1567. static inline int uld_send(struct adapter *adap, struct sk_buff *skb,
  1568. unsigned int tx_uld_type)
  1569. {
  1570. struct sge_uld_txq_info *txq_info;
  1571. struct sge_uld_txq *txq;
  1572. unsigned int idx = skb_txq(skb);
  1573. txq_info = adap->sge.uld_txq_info[tx_uld_type];
  1574. txq = &txq_info->uldtxq[idx];
  1575. if (unlikely(is_ctrl_pkt(skb))) {
  1576. /* Single ctrl queue is a requirement for LE workaround path */
  1577. if (adap->tids.nsftids)
  1578. idx = 0;
  1579. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1580. }
  1581. return ofld_xmit(txq, skb);
  1582. }
  1583. /**
  1584. * t4_ofld_send - send an offload packet
  1585. * @adap: the adapter
  1586. * @skb: the packet
  1587. *
  1588. * Sends an offload packet. We use the packet queue_mapping to select the
  1589. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1590. * should be sent as regular or control, bits 1-15 select the queue.
  1591. */
  1592. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1593. {
  1594. int ret;
  1595. local_bh_disable();
  1596. ret = uld_send(adap, skb, CXGB4_TX_OFLD);
  1597. local_bh_enable();
  1598. return ret;
  1599. }
  1600. /**
  1601. * cxgb4_ofld_send - send an offload packet
  1602. * @dev: the net device
  1603. * @skb: the packet
  1604. *
  1605. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1606. * intended for ULDs.
  1607. */
  1608. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1609. {
  1610. return t4_ofld_send(netdev2adap(dev), skb);
  1611. }
  1612. EXPORT_SYMBOL(cxgb4_ofld_send);
  1613. /**
  1614. * t4_crypto_send - send crypto packet
  1615. * @adap: the adapter
  1616. * @skb: the packet
  1617. *
  1618. * Sends crypto packet. We use the packet queue_mapping to select the
  1619. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1620. * should be sent as regular or control, bits 1-15 select the queue.
  1621. */
  1622. static int t4_crypto_send(struct adapter *adap, struct sk_buff *skb)
  1623. {
  1624. int ret;
  1625. local_bh_disable();
  1626. ret = uld_send(adap, skb, CXGB4_TX_CRYPTO);
  1627. local_bh_enable();
  1628. return ret;
  1629. }
  1630. /**
  1631. * cxgb4_crypto_send - send crypto packet
  1632. * @dev: the net device
  1633. * @skb: the packet
  1634. *
  1635. * Sends crypto packet. This is an exported version of @t4_crypto_send,
  1636. * intended for ULDs.
  1637. */
  1638. int cxgb4_crypto_send(struct net_device *dev, struct sk_buff *skb)
  1639. {
  1640. return t4_crypto_send(netdev2adap(dev), skb);
  1641. }
  1642. EXPORT_SYMBOL(cxgb4_crypto_send);
  1643. static inline void copy_frags(struct sk_buff *skb,
  1644. const struct pkt_gl *gl, unsigned int offset)
  1645. {
  1646. int i;
  1647. /* usually there's just one frag */
  1648. __skb_fill_page_desc(skb, 0, gl->frags[0].page,
  1649. gl->frags[0].offset + offset,
  1650. gl->frags[0].size - offset);
  1651. skb_shinfo(skb)->nr_frags = gl->nfrags;
  1652. for (i = 1; i < gl->nfrags; i++)
  1653. __skb_fill_page_desc(skb, i, gl->frags[i].page,
  1654. gl->frags[i].offset,
  1655. gl->frags[i].size);
  1656. /* get a reference to the last page, we don't own it */
  1657. get_page(gl->frags[gl->nfrags - 1].page);
  1658. }
  1659. /**
  1660. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1661. * @gl: the gather list
  1662. * @skb_len: size of sk_buff main body if it carries fragments
  1663. * @pull_len: amount of data to move to the sk_buff's main body
  1664. *
  1665. * Builds an sk_buff from the given packet gather list. Returns the
  1666. * sk_buff or %NULL if sk_buff allocation failed.
  1667. */
  1668. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1669. unsigned int skb_len, unsigned int pull_len)
  1670. {
  1671. struct sk_buff *skb;
  1672. /*
  1673. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1674. * size, which is expected since buffers are at least PAGE_SIZEd.
  1675. * In this case packets up to RX_COPY_THRES have only one fragment.
  1676. */
  1677. if (gl->tot_len <= RX_COPY_THRES) {
  1678. skb = dev_alloc_skb(gl->tot_len);
  1679. if (unlikely(!skb))
  1680. goto out;
  1681. __skb_put(skb, gl->tot_len);
  1682. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1683. } else {
  1684. skb = dev_alloc_skb(skb_len);
  1685. if (unlikely(!skb))
  1686. goto out;
  1687. __skb_put(skb, pull_len);
  1688. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1689. copy_frags(skb, gl, pull_len);
  1690. skb->len = gl->tot_len;
  1691. skb->data_len = skb->len - pull_len;
  1692. skb->truesize += skb->data_len;
  1693. }
  1694. out: return skb;
  1695. }
  1696. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1697. /**
  1698. * t4_pktgl_free - free a packet gather list
  1699. * @gl: the gather list
  1700. *
  1701. * Releases the pages of a packet gather list. We do not own the last
  1702. * page on the list and do not free it.
  1703. */
  1704. static void t4_pktgl_free(const struct pkt_gl *gl)
  1705. {
  1706. int n;
  1707. const struct page_frag *p;
  1708. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1709. put_page(p->page);
  1710. }
  1711. /*
  1712. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1713. * be delivered to anyone and send it to the stack for capture.
  1714. */
  1715. static noinline int handle_trace_pkt(struct adapter *adap,
  1716. const struct pkt_gl *gl)
  1717. {
  1718. struct sk_buff *skb;
  1719. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  1720. if (unlikely(!skb)) {
  1721. t4_pktgl_free(gl);
  1722. return 0;
  1723. }
  1724. if (is_t4(adap->params.chip))
  1725. __skb_pull(skb, sizeof(struct cpl_trace_pkt));
  1726. else
  1727. __skb_pull(skb, sizeof(struct cpl_t5_trace_pkt));
  1728. skb_reset_mac_header(skb);
  1729. skb->protocol = htons(0xffff);
  1730. skb->dev = adap->port[0];
  1731. netif_receive_skb(skb);
  1732. return 0;
  1733. }
  1734. /**
  1735. * cxgb4_sgetim_to_hwtstamp - convert sge time stamp to hw time stamp
  1736. * @adap: the adapter
  1737. * @hwtstamps: time stamp structure to update
  1738. * @sgetstamp: 60bit iqe timestamp
  1739. *
  1740. * Every ingress queue entry has the 60-bit timestamp, convert that timestamp
  1741. * which is in Core Clock ticks into ktime_t and assign it
  1742. **/
  1743. static void cxgb4_sgetim_to_hwtstamp(struct adapter *adap,
  1744. struct skb_shared_hwtstamps *hwtstamps,
  1745. u64 sgetstamp)
  1746. {
  1747. u64 ns;
  1748. u64 tmp = (sgetstamp * 1000 * 1000 + adap->params.vpd.cclk / 2);
  1749. ns = div_u64(tmp, adap->params.vpd.cclk);
  1750. memset(hwtstamps, 0, sizeof(*hwtstamps));
  1751. hwtstamps->hwtstamp = ns_to_ktime(ns);
  1752. }
  1753. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1754. const struct cpl_rx_pkt *pkt)
  1755. {
  1756. struct adapter *adapter = rxq->rspq.adap;
  1757. struct sge *s = &adapter->sge;
  1758. struct port_info *pi;
  1759. int ret;
  1760. struct sk_buff *skb;
  1761. skb = napi_get_frags(&rxq->rspq.napi);
  1762. if (unlikely(!skb)) {
  1763. t4_pktgl_free(gl);
  1764. rxq->stats.rx_drops++;
  1765. return;
  1766. }
  1767. copy_frags(skb, gl, s->pktshift);
  1768. skb->len = gl->tot_len - s->pktshift;
  1769. skb->data_len = skb->len;
  1770. skb->truesize += skb->data_len;
  1771. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1772. skb_record_rx_queue(skb, rxq->rspq.idx);
  1773. pi = netdev_priv(skb->dev);
  1774. if (pi->rxtstamp)
  1775. cxgb4_sgetim_to_hwtstamp(adapter, skb_hwtstamps(skb),
  1776. gl->sgetstamp);
  1777. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  1778. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1779. PKT_HASH_TYPE_L3);
  1780. if (unlikely(pkt->vlan_ex)) {
  1781. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1782. rxq->stats.vlan_ex++;
  1783. }
  1784. ret = napi_gro_frags(&rxq->rspq.napi);
  1785. if (ret == GRO_HELD)
  1786. rxq->stats.lro_pkts++;
  1787. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1788. rxq->stats.lro_merged++;
  1789. rxq->stats.pkts++;
  1790. rxq->stats.rx_cso++;
  1791. }
  1792. /**
  1793. * t4_ethrx_handler - process an ingress ethernet packet
  1794. * @q: the response queue that received the packet
  1795. * @rsp: the response queue descriptor holding the RX_PKT message
  1796. * @si: the gather list of packet fragments
  1797. *
  1798. * Process an ingress ethernet packet and deliver it to the stack.
  1799. */
  1800. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  1801. const struct pkt_gl *si)
  1802. {
  1803. bool csum_ok;
  1804. struct sk_buff *skb;
  1805. const struct cpl_rx_pkt *pkt;
  1806. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1807. struct sge *s = &q->adap->sge;
  1808. int cpl_trace_pkt = is_t4(q->adap->params.chip) ?
  1809. CPL_TRACE_PKT : CPL_TRACE_PKT_T5;
  1810. struct port_info *pi;
  1811. if (unlikely(*(u8 *)rsp == cpl_trace_pkt))
  1812. return handle_trace_pkt(q->adap, si);
  1813. pkt = (const struct cpl_rx_pkt *)rsp;
  1814. csum_ok = pkt->csum_calc && !pkt->err_vec &&
  1815. (q->netdev->features & NETIF_F_RXCSUM);
  1816. if ((pkt->l2info & htonl(RXF_TCP_F)) &&
  1817. !(cxgb_poll_busy_polling(q)) &&
  1818. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  1819. do_gro(rxq, si, pkt);
  1820. return 0;
  1821. }
  1822. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  1823. if (unlikely(!skb)) {
  1824. t4_pktgl_free(si);
  1825. rxq->stats.rx_drops++;
  1826. return 0;
  1827. }
  1828. __skb_pull(skb, s->pktshift); /* remove ethernet header padding */
  1829. skb->protocol = eth_type_trans(skb, q->netdev);
  1830. skb_record_rx_queue(skb, q->idx);
  1831. if (skb->dev->features & NETIF_F_RXHASH)
  1832. skb_set_hash(skb, (__force u32)pkt->rsshdr.hash_val,
  1833. PKT_HASH_TYPE_L3);
  1834. rxq->stats.pkts++;
  1835. pi = netdev_priv(skb->dev);
  1836. if (pi->rxtstamp)
  1837. cxgb4_sgetim_to_hwtstamp(q->adap, skb_hwtstamps(skb),
  1838. si->sgetstamp);
  1839. if (csum_ok && (pkt->l2info & htonl(RXF_UDP_F | RXF_TCP_F))) {
  1840. if (!pkt->ip_frag) {
  1841. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1842. rxq->stats.rx_cso++;
  1843. } else if (pkt->l2info & htonl(RXF_IP_F)) {
  1844. __sum16 c = (__force __sum16)pkt->csum;
  1845. skb->csum = csum_unfold(c);
  1846. skb->ip_summed = CHECKSUM_COMPLETE;
  1847. rxq->stats.rx_cso++;
  1848. }
  1849. } else {
  1850. skb_checksum_none_assert(skb);
  1851. #ifdef CONFIG_CHELSIO_T4_FCOE
  1852. #define CPL_RX_PKT_FLAGS (RXF_PSH_F | RXF_SYN_F | RXF_UDP_F | \
  1853. RXF_TCP_F | RXF_IP_F | RXF_IP6_F | RXF_LRO_F)
  1854. if (!(pkt->l2info & cpu_to_be32(CPL_RX_PKT_FLAGS))) {
  1855. if ((pkt->l2info & cpu_to_be32(RXF_FCOE_F)) &&
  1856. (pi->fcoe.flags & CXGB_FCOE_ENABLED)) {
  1857. if (!(pkt->err_vec & cpu_to_be16(RXERR_CSUM_F)))
  1858. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1859. }
  1860. }
  1861. #undef CPL_RX_PKT_FLAGS
  1862. #endif /* CONFIG_CHELSIO_T4_FCOE */
  1863. }
  1864. if (unlikely(pkt->vlan_ex)) {
  1865. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(pkt->vlan));
  1866. rxq->stats.vlan_ex++;
  1867. }
  1868. skb_mark_napi_id(skb, &q->napi);
  1869. netif_receive_skb(skb);
  1870. return 0;
  1871. }
  1872. /**
  1873. * restore_rx_bufs - put back a packet's Rx buffers
  1874. * @si: the packet gather list
  1875. * @q: the SGE free list
  1876. * @frags: number of FL buffers to restore
  1877. *
  1878. * Puts back on an FL the Rx buffers associated with @si. The buffers
  1879. * have already been unmapped and are left unmapped, we mark them so to
  1880. * prevent further unmapping attempts.
  1881. *
  1882. * This function undoes a series of @unmap_rx_buf calls when we find out
  1883. * that the current packet can't be processed right away afterall and we
  1884. * need to come back to it later. This is a very rare event and there's
  1885. * no effort to make this particularly efficient.
  1886. */
  1887. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  1888. int frags)
  1889. {
  1890. struct rx_sw_desc *d;
  1891. while (frags--) {
  1892. if (q->cidx == 0)
  1893. q->cidx = q->size - 1;
  1894. else
  1895. q->cidx--;
  1896. d = &q->sdesc[q->cidx];
  1897. d->page = si->frags[frags].page;
  1898. d->dma_addr |= RX_UNMAPPED_BUF;
  1899. q->avail++;
  1900. }
  1901. }
  1902. /**
  1903. * is_new_response - check if a response is newly written
  1904. * @r: the response descriptor
  1905. * @q: the response queue
  1906. *
  1907. * Returns true if a response descriptor contains a yet unprocessed
  1908. * response.
  1909. */
  1910. static inline bool is_new_response(const struct rsp_ctrl *r,
  1911. const struct sge_rspq *q)
  1912. {
  1913. return (r->type_gen >> RSPD_GEN_S) == q->gen;
  1914. }
  1915. /**
  1916. * rspq_next - advance to the next entry in a response queue
  1917. * @q: the queue
  1918. *
  1919. * Updates the state of a response queue to advance it to the next entry.
  1920. */
  1921. static inline void rspq_next(struct sge_rspq *q)
  1922. {
  1923. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  1924. if (unlikely(++q->cidx == q->size)) {
  1925. q->cidx = 0;
  1926. q->gen ^= 1;
  1927. q->cur_desc = q->desc;
  1928. }
  1929. }
  1930. /**
  1931. * process_responses - process responses from an SGE response queue
  1932. * @q: the ingress queue to process
  1933. * @budget: how many responses can be processed in this round
  1934. *
  1935. * Process responses from an SGE response queue up to the supplied budget.
  1936. * Responses include received packets as well as control messages from FW
  1937. * or HW.
  1938. *
  1939. * Additionally choose the interrupt holdoff time for the next interrupt
  1940. * on this queue. If the system is under memory shortage use a fairly
  1941. * long delay to help recovery.
  1942. */
  1943. static int process_responses(struct sge_rspq *q, int budget)
  1944. {
  1945. int ret, rsp_type;
  1946. int budget_left = budget;
  1947. const struct rsp_ctrl *rc;
  1948. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1949. struct adapter *adapter = q->adap;
  1950. struct sge *s = &adapter->sge;
  1951. while (likely(budget_left)) {
  1952. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1953. if (!is_new_response(rc, q)) {
  1954. if (q->flush_handler)
  1955. q->flush_handler(q);
  1956. break;
  1957. }
  1958. dma_rmb();
  1959. rsp_type = RSPD_TYPE_G(rc->type_gen);
  1960. if (likely(rsp_type == RSPD_TYPE_FLBUF_X)) {
  1961. struct page_frag *fp;
  1962. struct pkt_gl si;
  1963. const struct rx_sw_desc *rsd;
  1964. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  1965. if (len & RSPD_NEWBUF_F) {
  1966. if (likely(q->offset > 0)) {
  1967. free_rx_bufs(q->adap, &rxq->fl, 1);
  1968. q->offset = 0;
  1969. }
  1970. len = RSPD_LEN_G(len);
  1971. }
  1972. si.tot_len = len;
  1973. /* gather packet fragments */
  1974. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  1975. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  1976. bufsz = get_buf_size(adapter, rsd);
  1977. fp->page = rsd->page;
  1978. fp->offset = q->offset;
  1979. fp->size = min(bufsz, len);
  1980. len -= fp->size;
  1981. if (!len)
  1982. break;
  1983. unmap_rx_buf(q->adap, &rxq->fl);
  1984. }
  1985. si.sgetstamp = SGE_TIMESTAMP_G(
  1986. be64_to_cpu(rc->last_flit));
  1987. /*
  1988. * Last buffer remains mapped so explicitly make it
  1989. * coherent for CPU access.
  1990. */
  1991. dma_sync_single_for_cpu(q->adap->pdev_dev,
  1992. get_buf_addr(rsd),
  1993. fp->size, DMA_FROM_DEVICE);
  1994. si.va = page_address(si.frags[0].page) +
  1995. si.frags[0].offset;
  1996. prefetch(si.va);
  1997. si.nfrags = frags + 1;
  1998. ret = q->handler(q, q->cur_desc, &si);
  1999. if (likely(ret == 0))
  2000. q->offset += ALIGN(fp->size, s->fl_align);
  2001. else
  2002. restore_rx_bufs(&si, &rxq->fl, frags);
  2003. } else if (likely(rsp_type == RSPD_TYPE_CPL_X)) {
  2004. ret = q->handler(q, q->cur_desc, NULL);
  2005. } else {
  2006. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  2007. }
  2008. if (unlikely(ret)) {
  2009. /* couldn't process descriptor, back off for recovery */
  2010. q->next_intr_params = QINTR_TIMER_IDX_V(NOMEM_TMR_IDX);
  2011. break;
  2012. }
  2013. rspq_next(q);
  2014. budget_left--;
  2015. }
  2016. if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 16)
  2017. __refill_fl(q->adap, &rxq->fl);
  2018. return budget - budget_left;
  2019. }
  2020. #ifdef CONFIG_NET_RX_BUSY_POLL
  2021. int cxgb_busy_poll(struct napi_struct *napi)
  2022. {
  2023. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  2024. unsigned int params, work_done;
  2025. u32 val;
  2026. if (!cxgb_poll_lock_poll(q))
  2027. return LL_FLUSH_BUSY;
  2028. work_done = process_responses(q, 4);
  2029. params = QINTR_TIMER_IDX_V(TIMERREG_COUNTER0_X) | QINTR_CNT_EN_V(1);
  2030. q->next_intr_params = params;
  2031. val = CIDXINC_V(work_done) | SEINTARM_V(params);
  2032. /* If we don't have access to the new User GTS (T5+), use the old
  2033. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2034. */
  2035. if (unlikely(!q->bar2_addr))
  2036. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
  2037. val | INGRESSQID_V((u32)q->cntxt_id));
  2038. else {
  2039. writel(val | INGRESSQID_V(q->bar2_qid),
  2040. q->bar2_addr + SGE_UDB_GTS);
  2041. wmb();
  2042. }
  2043. cxgb_poll_unlock_poll(q);
  2044. return work_done;
  2045. }
  2046. #endif /* CONFIG_NET_RX_BUSY_POLL */
  2047. /**
  2048. * napi_rx_handler - the NAPI handler for Rx processing
  2049. * @napi: the napi instance
  2050. * @budget: how many packets we can process in this round
  2051. *
  2052. * Handler for new data events when using NAPI. This does not need any
  2053. * locking or protection from interrupts as data interrupts are off at
  2054. * this point and other adapter interrupts do not interfere (the latter
  2055. * in not a concern at all with MSI-X as non-data interrupts then have
  2056. * a separate handler).
  2057. */
  2058. static int napi_rx_handler(struct napi_struct *napi, int budget)
  2059. {
  2060. unsigned int params;
  2061. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  2062. int work_done;
  2063. u32 val;
  2064. if (!cxgb_poll_lock_napi(q))
  2065. return budget;
  2066. work_done = process_responses(q, budget);
  2067. if (likely(work_done < budget)) {
  2068. int timer_index;
  2069. napi_complete_done(napi, work_done);
  2070. timer_index = QINTR_TIMER_IDX_G(q->next_intr_params);
  2071. if (q->adaptive_rx) {
  2072. if (work_done > max(timer_pkt_quota[timer_index],
  2073. MIN_NAPI_WORK))
  2074. timer_index = (timer_index + 1);
  2075. else
  2076. timer_index = timer_index - 1;
  2077. timer_index = clamp(timer_index, 0, SGE_TIMERREGS - 1);
  2078. q->next_intr_params =
  2079. QINTR_TIMER_IDX_V(timer_index) |
  2080. QINTR_CNT_EN_V(0);
  2081. params = q->next_intr_params;
  2082. } else {
  2083. params = q->next_intr_params;
  2084. q->next_intr_params = q->intr_params;
  2085. }
  2086. } else
  2087. params = QINTR_TIMER_IDX_V(7);
  2088. val = CIDXINC_V(work_done) | SEINTARM_V(params);
  2089. /* If we don't have access to the new User GTS (T5+), use the old
  2090. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2091. */
  2092. if (unlikely(q->bar2_addr == NULL)) {
  2093. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS_A),
  2094. val | INGRESSQID_V((u32)q->cntxt_id));
  2095. } else {
  2096. writel(val | INGRESSQID_V(q->bar2_qid),
  2097. q->bar2_addr + SGE_UDB_GTS);
  2098. wmb();
  2099. }
  2100. cxgb_poll_unlock_napi(q);
  2101. return work_done;
  2102. }
  2103. /*
  2104. * The MSI-X interrupt handler for an SGE response queue.
  2105. */
  2106. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  2107. {
  2108. struct sge_rspq *q = cookie;
  2109. napi_schedule(&q->napi);
  2110. return IRQ_HANDLED;
  2111. }
  2112. /*
  2113. * Process the indirect interrupt entries in the interrupt queue and kick off
  2114. * NAPI for each queue that has generated an entry.
  2115. */
  2116. static unsigned int process_intrq(struct adapter *adap)
  2117. {
  2118. unsigned int credits;
  2119. const struct rsp_ctrl *rc;
  2120. struct sge_rspq *q = &adap->sge.intrq;
  2121. u32 val;
  2122. spin_lock(&adap->sge.intrq_lock);
  2123. for (credits = 0; ; credits++) {
  2124. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  2125. if (!is_new_response(rc, q))
  2126. break;
  2127. dma_rmb();
  2128. if (RSPD_TYPE_G(rc->type_gen) == RSPD_TYPE_INTR_X) {
  2129. unsigned int qid = ntohl(rc->pldbuflen_qid);
  2130. qid -= adap->sge.ingr_start;
  2131. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  2132. }
  2133. rspq_next(q);
  2134. }
  2135. val = CIDXINC_V(credits) | SEINTARM_V(q->intr_params);
  2136. /* If we don't have access to the new User GTS (T5+), use the old
  2137. * doorbell mechanism; otherwise use the new BAR2 mechanism.
  2138. */
  2139. if (unlikely(q->bar2_addr == NULL)) {
  2140. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
  2141. val | INGRESSQID_V(q->cntxt_id));
  2142. } else {
  2143. writel(val | INGRESSQID_V(q->bar2_qid),
  2144. q->bar2_addr + SGE_UDB_GTS);
  2145. wmb();
  2146. }
  2147. spin_unlock(&adap->sge.intrq_lock);
  2148. return credits;
  2149. }
  2150. /*
  2151. * The MSI interrupt handler, which handles data events from SGE response queues
  2152. * as well as error and other async events as they all use the same MSI vector.
  2153. */
  2154. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  2155. {
  2156. struct adapter *adap = cookie;
  2157. if (adap->flags & MASTER_PF)
  2158. t4_slow_intr_handler(adap);
  2159. process_intrq(adap);
  2160. return IRQ_HANDLED;
  2161. }
  2162. /*
  2163. * Interrupt handler for legacy INTx interrupts.
  2164. * Handles data events from SGE response queues as well as error and other
  2165. * async events as they all use the same interrupt line.
  2166. */
  2167. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  2168. {
  2169. struct adapter *adap = cookie;
  2170. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI_A), 0);
  2171. if (((adap->flags & MASTER_PF) && t4_slow_intr_handler(adap)) |
  2172. process_intrq(adap))
  2173. return IRQ_HANDLED;
  2174. return IRQ_NONE; /* probably shared interrupt */
  2175. }
  2176. /**
  2177. * t4_intr_handler - select the top-level interrupt handler
  2178. * @adap: the adapter
  2179. *
  2180. * Selects the top-level interrupt handler based on the type of interrupts
  2181. * (MSI-X, MSI, or INTx).
  2182. */
  2183. irq_handler_t t4_intr_handler(struct adapter *adap)
  2184. {
  2185. if (adap->flags & USING_MSIX)
  2186. return t4_sge_intr_msix;
  2187. if (adap->flags & USING_MSI)
  2188. return t4_intr_msi;
  2189. return t4_intr_intx;
  2190. }
  2191. static void sge_rx_timer_cb(unsigned long data)
  2192. {
  2193. unsigned long m;
  2194. unsigned int i;
  2195. struct adapter *adap = (struct adapter *)data;
  2196. struct sge *s = &adap->sge;
  2197. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2198. for (m = s->starving_fl[i]; m; m &= m - 1) {
  2199. struct sge_eth_rxq *rxq;
  2200. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  2201. struct sge_fl *fl = s->egr_map[id];
  2202. clear_bit(id, s->starving_fl);
  2203. smp_mb__after_atomic();
  2204. if (fl_starving(adap, fl)) {
  2205. rxq = container_of(fl, struct sge_eth_rxq, fl);
  2206. if (napi_reschedule(&rxq->rspq.napi))
  2207. fl->starving++;
  2208. else
  2209. set_bit(id, s->starving_fl);
  2210. }
  2211. }
  2212. /* The remainder of the SGE RX Timer Callback routine is dedicated to
  2213. * global Master PF activities like checking for chip ingress stalls,
  2214. * etc.
  2215. */
  2216. if (!(adap->flags & MASTER_PF))
  2217. goto done;
  2218. t4_idma_monitor(adap, &s->idma_monitor, HZ, RX_QCHECK_PERIOD);
  2219. done:
  2220. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  2221. }
  2222. static void sge_tx_timer_cb(unsigned long data)
  2223. {
  2224. unsigned long m;
  2225. unsigned int i, budget;
  2226. struct adapter *adap = (struct adapter *)data;
  2227. struct sge *s = &adap->sge;
  2228. for (i = 0; i < BITS_TO_LONGS(s->egr_sz); i++)
  2229. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  2230. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  2231. struct sge_uld_txq *txq = s->egr_map[id];
  2232. clear_bit(id, s->txq_maperr);
  2233. tasklet_schedule(&txq->qresume_tsk);
  2234. }
  2235. budget = MAX_TIMER_TX_RECLAIM;
  2236. i = s->ethtxq_rover;
  2237. do {
  2238. struct sge_eth_txq *q = &s->ethtxq[i];
  2239. if (q->q.in_use &&
  2240. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  2241. __netif_tx_trylock(q->txq)) {
  2242. int avail = reclaimable(&q->q);
  2243. if (avail) {
  2244. if (avail > budget)
  2245. avail = budget;
  2246. free_tx_desc(adap, &q->q, avail, true);
  2247. q->q.in_use -= avail;
  2248. budget -= avail;
  2249. }
  2250. __netif_tx_unlock(q->txq);
  2251. }
  2252. if (++i >= s->ethqsets)
  2253. i = 0;
  2254. } while (budget && i != s->ethtxq_rover);
  2255. s->ethtxq_rover = i;
  2256. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  2257. }
  2258. /**
  2259. * bar2_address - return the BAR2 address for an SGE Queue's Registers
  2260. * @adapter: the adapter
  2261. * @qid: the SGE Queue ID
  2262. * @qtype: the SGE Queue Type (Egress or Ingress)
  2263. * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
  2264. *
  2265. * Returns the BAR2 address for the SGE Queue Registers associated with
  2266. * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
  2267. * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
  2268. * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
  2269. * Registers are supported (e.g. the Write Combining Doorbell Buffer).
  2270. */
  2271. static void __iomem *bar2_address(struct adapter *adapter,
  2272. unsigned int qid,
  2273. enum t4_bar2_qtype qtype,
  2274. unsigned int *pbar2_qid)
  2275. {
  2276. u64 bar2_qoffset;
  2277. int ret;
  2278. ret = t4_bar2_sge_qregs(adapter, qid, qtype, 0,
  2279. &bar2_qoffset, pbar2_qid);
  2280. if (ret)
  2281. return NULL;
  2282. return adapter->bar2 + bar2_qoffset;
  2283. }
  2284. /* @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
  2285. * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
  2286. */
  2287. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  2288. struct net_device *dev, int intr_idx,
  2289. struct sge_fl *fl, rspq_handler_t hnd,
  2290. rspq_flush_handler_t flush_hnd, int cong)
  2291. {
  2292. int ret, flsz = 0;
  2293. struct fw_iq_cmd c;
  2294. struct sge *s = &adap->sge;
  2295. struct port_info *pi = netdev_priv(dev);
  2296. /* Size needs to be multiple of 16, including status entry. */
  2297. iq->size = roundup(iq->size, 16);
  2298. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  2299. &iq->phys_addr, NULL, 0,
  2300. dev_to_node(adap->pdev_dev));
  2301. if (!iq->desc)
  2302. return -ENOMEM;
  2303. memset(&c, 0, sizeof(c));
  2304. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_IQ_CMD) | FW_CMD_REQUEST_F |
  2305. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2306. FW_IQ_CMD_PFN_V(adap->pf) | FW_IQ_CMD_VFN_V(0));
  2307. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC_F | FW_IQ_CMD_IQSTART_F |
  2308. FW_LEN16(c));
  2309. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE_V(FW_IQ_TYPE_FL_INT_CAP) |
  2310. FW_IQ_CMD_IQASYNCH_V(fwevtq) | FW_IQ_CMD_VIID_V(pi->viid) |
  2311. FW_IQ_CMD_IQANDST_V(intr_idx < 0) |
  2312. FW_IQ_CMD_IQANUD_V(UPDATEDELIVERY_INTERRUPT_X) |
  2313. FW_IQ_CMD_IQANDSTINDEX_V(intr_idx >= 0 ? intr_idx :
  2314. -intr_idx - 1));
  2315. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH_V(pi->tx_chan) |
  2316. FW_IQ_CMD_IQGTSMODE_F |
  2317. FW_IQ_CMD_IQINTCNTTHRESH_V(iq->pktcnt_idx) |
  2318. FW_IQ_CMD_IQESIZE_V(ilog2(iq->iqe_len) - 4));
  2319. c.iqsize = htons(iq->size);
  2320. c.iqaddr = cpu_to_be64(iq->phys_addr);
  2321. if (cong >= 0)
  2322. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_IQFLINTCONGEN_F);
  2323. if (fl) {
  2324. enum chip_type chip = CHELSIO_CHIP_VERSION(adap->params.chip);
  2325. /* Allocate the ring for the hardware free list (with space
  2326. * for its status page) along with the associated software
  2327. * descriptor ring. The free list size needs to be a multiple
  2328. * of the Egress Queue Unit and at least 2 Egress Units larger
  2329. * than the SGE's Egress Congrestion Threshold
  2330. * (fl_starve_thres - 1).
  2331. */
  2332. if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
  2333. fl->size = s->fl_starve_thres - 1 + 2 * 8;
  2334. fl->size = roundup(fl->size, 8);
  2335. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  2336. sizeof(struct rx_sw_desc), &fl->addr,
  2337. &fl->sdesc, s->stat_len,
  2338. dev_to_node(adap->pdev_dev));
  2339. if (!fl->desc)
  2340. goto fl_nomem;
  2341. flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
  2342. c.iqns_to_fl0congen |= htonl(FW_IQ_CMD_FL0PACKEN_F |
  2343. FW_IQ_CMD_FL0FETCHRO_F |
  2344. FW_IQ_CMD_FL0DATARO_F |
  2345. FW_IQ_CMD_FL0PADEN_F);
  2346. if (cong >= 0)
  2347. c.iqns_to_fl0congen |=
  2348. htonl(FW_IQ_CMD_FL0CNGCHMAP_V(cong) |
  2349. FW_IQ_CMD_FL0CONGCIF_F |
  2350. FW_IQ_CMD_FL0CONGEN_F);
  2351. /* In T6, for egress queue type FL there is internal overhead
  2352. * of 16B for header going into FLM module. Hence the maximum
  2353. * allowed burst size is 448 bytes. For T4/T5, the hardware
  2354. * doesn't coalesce fetch requests if more than 64 bytes of
  2355. * Free List pointers are provided, so we use a 128-byte Fetch
  2356. * Burst Minimum there (T6 implements coalescing so we can use
  2357. * the smaller 64-byte value there).
  2358. */
  2359. c.fl0dcaen_to_fl0cidxfthresh =
  2360. htons(FW_IQ_CMD_FL0FBMIN_V(chip <= CHELSIO_T5 ?
  2361. FETCHBURSTMIN_128B_X :
  2362. FETCHBURSTMIN_64B_X) |
  2363. FW_IQ_CMD_FL0FBMAX_V((chip <= CHELSIO_T5) ?
  2364. FETCHBURSTMAX_512B_X :
  2365. FETCHBURSTMAX_256B_X));
  2366. c.fl0size = htons(flsz);
  2367. c.fl0addr = cpu_to_be64(fl->addr);
  2368. }
  2369. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2370. if (ret)
  2371. goto err;
  2372. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  2373. iq->cur_desc = iq->desc;
  2374. iq->cidx = 0;
  2375. iq->gen = 1;
  2376. iq->next_intr_params = iq->intr_params;
  2377. iq->cntxt_id = ntohs(c.iqid);
  2378. iq->abs_id = ntohs(c.physiqid);
  2379. iq->bar2_addr = bar2_address(adap,
  2380. iq->cntxt_id,
  2381. T4_BAR2_QTYPE_INGRESS,
  2382. &iq->bar2_qid);
  2383. iq->size--; /* subtract status entry */
  2384. iq->netdev = dev;
  2385. iq->handler = hnd;
  2386. iq->flush_handler = flush_hnd;
  2387. memset(&iq->lro_mgr, 0, sizeof(struct t4_lro_mgr));
  2388. skb_queue_head_init(&iq->lro_mgr.lroq);
  2389. /* set offset to -1 to distinguish ingress queues without FL */
  2390. iq->offset = fl ? 0 : -1;
  2391. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  2392. if (fl) {
  2393. fl->cntxt_id = ntohs(c.fl0id);
  2394. fl->avail = fl->pend_cred = 0;
  2395. fl->pidx = fl->cidx = 0;
  2396. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  2397. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  2398. /* Note, we must initialize the BAR2 Free List User Doorbell
  2399. * information before refilling the Free List!
  2400. */
  2401. fl->bar2_addr = bar2_address(adap,
  2402. fl->cntxt_id,
  2403. T4_BAR2_QTYPE_EGRESS,
  2404. &fl->bar2_qid);
  2405. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  2406. }
  2407. /* For T5 and later we attempt to set up the Congestion Manager values
  2408. * of the new RX Ethernet Queue. This should really be handled by
  2409. * firmware because it's more complex than any host driver wants to
  2410. * get involved with and it's different per chip and this is almost
  2411. * certainly wrong. Firmware would be wrong as well, but it would be
  2412. * a lot easier to fix in one place ... For now we do something very
  2413. * simple (and hopefully less wrong).
  2414. */
  2415. if (!is_t4(adap->params.chip) && cong >= 0) {
  2416. u32 param, val, ch_map = 0;
  2417. int i;
  2418. u16 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
  2419. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  2420. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
  2421. FW_PARAMS_PARAM_YZ_V(iq->cntxt_id));
  2422. if (cong == 0) {
  2423. val = CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_QUEUE_X);
  2424. } else {
  2425. val =
  2426. CONMCTXT_CNGTPMODE_V(CONMCTXT_CNGTPMODE_CHANNEL_X);
  2427. for (i = 0; i < 4; i++) {
  2428. if (cong & (1 << i))
  2429. ch_map |= 1 << (i << cng_ch_bits_log);
  2430. }
  2431. val |= CONMCTXT_CNGCHMAP_V(ch_map);
  2432. }
  2433. ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
  2434. &param, &val);
  2435. if (ret)
  2436. dev_warn(adap->pdev_dev, "Failed to set Congestion"
  2437. " Manager Context for Ingress Queue %d: %d\n",
  2438. iq->cntxt_id, -ret);
  2439. }
  2440. return 0;
  2441. fl_nomem:
  2442. ret = -ENOMEM;
  2443. err:
  2444. if (iq->desc) {
  2445. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  2446. iq->desc, iq->phys_addr);
  2447. iq->desc = NULL;
  2448. }
  2449. if (fl && fl->desc) {
  2450. kfree(fl->sdesc);
  2451. fl->sdesc = NULL;
  2452. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  2453. fl->desc, fl->addr);
  2454. fl->desc = NULL;
  2455. }
  2456. return ret;
  2457. }
  2458. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  2459. {
  2460. q->cntxt_id = id;
  2461. q->bar2_addr = bar2_address(adap,
  2462. q->cntxt_id,
  2463. T4_BAR2_QTYPE_EGRESS,
  2464. &q->bar2_qid);
  2465. q->in_use = 0;
  2466. q->cidx = q->pidx = 0;
  2467. q->stops = q->restarts = 0;
  2468. q->stat = (void *)&q->desc[q->size];
  2469. spin_lock_init(&q->db_lock);
  2470. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  2471. }
  2472. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  2473. struct net_device *dev, struct netdev_queue *netdevq,
  2474. unsigned int iqid)
  2475. {
  2476. int ret, nentries;
  2477. struct fw_eq_eth_cmd c;
  2478. struct sge *s = &adap->sge;
  2479. struct port_info *pi = netdev_priv(dev);
  2480. /* Add status entries */
  2481. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2482. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2483. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2484. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2485. netdev_queue_numa_node_read(netdevq));
  2486. if (!txq->q.desc)
  2487. return -ENOMEM;
  2488. memset(&c, 0, sizeof(c));
  2489. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_ETH_CMD) | FW_CMD_REQUEST_F |
  2490. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2491. FW_EQ_ETH_CMD_PFN_V(adap->pf) |
  2492. FW_EQ_ETH_CMD_VFN_V(0));
  2493. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC_F |
  2494. FW_EQ_ETH_CMD_EQSTART_F | FW_LEN16(c));
  2495. c.viid_pkd = htonl(FW_EQ_ETH_CMD_AUTOEQUEQE_F |
  2496. FW_EQ_ETH_CMD_VIID_V(pi->viid));
  2497. c.fetchszm_to_iqid =
  2498. htonl(FW_EQ_ETH_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2499. FW_EQ_ETH_CMD_PCIECHN_V(pi->tx_chan) |
  2500. FW_EQ_ETH_CMD_FETCHRO_F | FW_EQ_ETH_CMD_IQID_V(iqid));
  2501. c.dcaen_to_eqsize =
  2502. htonl(FW_EQ_ETH_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2503. FW_EQ_ETH_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2504. FW_EQ_ETH_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2505. FW_EQ_ETH_CMD_EQSIZE_V(nentries));
  2506. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2507. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2508. if (ret) {
  2509. kfree(txq->q.sdesc);
  2510. txq->q.sdesc = NULL;
  2511. dma_free_coherent(adap->pdev_dev,
  2512. nentries * sizeof(struct tx_desc),
  2513. txq->q.desc, txq->q.phys_addr);
  2514. txq->q.desc = NULL;
  2515. return ret;
  2516. }
  2517. txq->q.q_type = CXGB4_TXQ_ETH;
  2518. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_G(ntohl(c.eqid_pkd)));
  2519. txq->txq = netdevq;
  2520. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  2521. txq->mapping_err = 0;
  2522. return 0;
  2523. }
  2524. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  2525. struct net_device *dev, unsigned int iqid,
  2526. unsigned int cmplqid)
  2527. {
  2528. int ret, nentries;
  2529. struct fw_eq_ctrl_cmd c;
  2530. struct sge *s = &adap->sge;
  2531. struct port_info *pi = netdev_priv(dev);
  2532. /* Add status entries */
  2533. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2534. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  2535. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  2536. NULL, 0, dev_to_node(adap->pdev_dev));
  2537. if (!txq->q.desc)
  2538. return -ENOMEM;
  2539. c.op_to_vfn = htonl(FW_CMD_OP_V(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST_F |
  2540. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2541. FW_EQ_CTRL_CMD_PFN_V(adap->pf) |
  2542. FW_EQ_CTRL_CMD_VFN_V(0));
  2543. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC_F |
  2544. FW_EQ_CTRL_CMD_EQSTART_F | FW_LEN16(c));
  2545. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID_V(cmplqid));
  2546. c.physeqid_pkd = htonl(0);
  2547. c.fetchszm_to_iqid =
  2548. htonl(FW_EQ_CTRL_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2549. FW_EQ_CTRL_CMD_PCIECHN_V(pi->tx_chan) |
  2550. FW_EQ_CTRL_CMD_FETCHRO_F | FW_EQ_CTRL_CMD_IQID_V(iqid));
  2551. c.dcaen_to_eqsize =
  2552. htonl(FW_EQ_CTRL_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2553. FW_EQ_CTRL_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2554. FW_EQ_CTRL_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2555. FW_EQ_CTRL_CMD_EQSIZE_V(nentries));
  2556. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2557. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2558. if (ret) {
  2559. dma_free_coherent(adap->pdev_dev,
  2560. nentries * sizeof(struct tx_desc),
  2561. txq->q.desc, txq->q.phys_addr);
  2562. txq->q.desc = NULL;
  2563. return ret;
  2564. }
  2565. txq->q.q_type = CXGB4_TXQ_CTRL;
  2566. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_G(ntohl(c.cmpliqid_eqid)));
  2567. txq->adap = adap;
  2568. skb_queue_head_init(&txq->sendq);
  2569. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  2570. txq->full = 0;
  2571. return 0;
  2572. }
  2573. int t4_sge_mod_ctrl_txq(struct adapter *adap, unsigned int eqid,
  2574. unsigned int cmplqid)
  2575. {
  2576. u32 param, val;
  2577. param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
  2578. FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL) |
  2579. FW_PARAMS_PARAM_YZ_V(eqid));
  2580. val = cmplqid;
  2581. return t4_set_params(adap, adap->mbox, adap->pf, 0, 1, &param, &val);
  2582. }
  2583. int t4_sge_alloc_uld_txq(struct adapter *adap, struct sge_uld_txq *txq,
  2584. struct net_device *dev, unsigned int iqid,
  2585. unsigned int uld_type)
  2586. {
  2587. int ret, nentries;
  2588. struct fw_eq_ofld_cmd c;
  2589. struct sge *s = &adap->sge;
  2590. struct port_info *pi = netdev_priv(dev);
  2591. int cmd = FW_EQ_OFLD_CMD;
  2592. /* Add status entries */
  2593. nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
  2594. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  2595. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  2596. &txq->q.phys_addr, &txq->q.sdesc, s->stat_len,
  2597. NUMA_NO_NODE);
  2598. if (!txq->q.desc)
  2599. return -ENOMEM;
  2600. memset(&c, 0, sizeof(c));
  2601. if (unlikely(uld_type == CXGB4_TX_CRYPTO))
  2602. cmd = FW_EQ_CTRL_CMD;
  2603. c.op_to_vfn = htonl(FW_CMD_OP_V(cmd) | FW_CMD_REQUEST_F |
  2604. FW_CMD_WRITE_F | FW_CMD_EXEC_F |
  2605. FW_EQ_OFLD_CMD_PFN_V(adap->pf) |
  2606. FW_EQ_OFLD_CMD_VFN_V(0));
  2607. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC_F |
  2608. FW_EQ_OFLD_CMD_EQSTART_F | FW_LEN16(c));
  2609. c.fetchszm_to_iqid =
  2610. htonl(FW_EQ_OFLD_CMD_HOSTFCMODE_V(HOSTFCMODE_STATUS_PAGE_X) |
  2611. FW_EQ_OFLD_CMD_PCIECHN_V(pi->tx_chan) |
  2612. FW_EQ_OFLD_CMD_FETCHRO_F | FW_EQ_OFLD_CMD_IQID_V(iqid));
  2613. c.dcaen_to_eqsize =
  2614. htonl(FW_EQ_OFLD_CMD_FBMIN_V(FETCHBURSTMIN_64B_X) |
  2615. FW_EQ_OFLD_CMD_FBMAX_V(FETCHBURSTMAX_512B_X) |
  2616. FW_EQ_OFLD_CMD_CIDXFTHRESH_V(CIDXFLUSHTHRESH_32_X) |
  2617. FW_EQ_OFLD_CMD_EQSIZE_V(nentries));
  2618. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  2619. ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
  2620. if (ret) {
  2621. kfree(txq->q.sdesc);
  2622. txq->q.sdesc = NULL;
  2623. dma_free_coherent(adap->pdev_dev,
  2624. nentries * sizeof(struct tx_desc),
  2625. txq->q.desc, txq->q.phys_addr);
  2626. txq->q.desc = NULL;
  2627. return ret;
  2628. }
  2629. txq->q.q_type = CXGB4_TXQ_ULD;
  2630. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_G(ntohl(c.eqid_pkd)));
  2631. txq->adap = adap;
  2632. skb_queue_head_init(&txq->sendq);
  2633. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  2634. txq->full = 0;
  2635. txq->mapping_err = 0;
  2636. return 0;
  2637. }
  2638. void free_txq(struct adapter *adap, struct sge_txq *q)
  2639. {
  2640. struct sge *s = &adap->sge;
  2641. dma_free_coherent(adap->pdev_dev,
  2642. q->size * sizeof(struct tx_desc) + s->stat_len,
  2643. q->desc, q->phys_addr);
  2644. q->cntxt_id = 0;
  2645. q->sdesc = NULL;
  2646. q->desc = NULL;
  2647. }
  2648. void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  2649. struct sge_fl *fl)
  2650. {
  2651. struct sge *s = &adap->sge;
  2652. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  2653. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  2654. t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
  2655. rq->cntxt_id, fl_id, 0xffff);
  2656. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  2657. rq->desc, rq->phys_addr);
  2658. netif_napi_del(&rq->napi);
  2659. rq->netdev = NULL;
  2660. rq->cntxt_id = rq->abs_id = 0;
  2661. rq->desc = NULL;
  2662. if (fl) {
  2663. free_rx_bufs(adap, fl, fl->avail);
  2664. dma_free_coherent(adap->pdev_dev, fl->size * 8 + s->stat_len,
  2665. fl->desc, fl->addr);
  2666. kfree(fl->sdesc);
  2667. fl->sdesc = NULL;
  2668. fl->cntxt_id = 0;
  2669. fl->desc = NULL;
  2670. }
  2671. }
  2672. /**
  2673. * t4_free_ofld_rxqs - free a block of consecutive Rx queues
  2674. * @adap: the adapter
  2675. * @n: number of queues
  2676. * @q: pointer to first queue
  2677. *
  2678. * Release the resources of a consecutive block of offload Rx queues.
  2679. */
  2680. void t4_free_ofld_rxqs(struct adapter *adap, int n, struct sge_ofld_rxq *q)
  2681. {
  2682. for ( ; n; n--, q++)
  2683. if (q->rspq.desc)
  2684. free_rspq_fl(adap, &q->rspq,
  2685. q->fl.size ? &q->fl : NULL);
  2686. }
  2687. /**
  2688. * t4_free_sge_resources - free SGE resources
  2689. * @adap: the adapter
  2690. *
  2691. * Frees resources used by the SGE queue sets.
  2692. */
  2693. void t4_free_sge_resources(struct adapter *adap)
  2694. {
  2695. int i;
  2696. struct sge_eth_rxq *eq;
  2697. struct sge_eth_txq *etq;
  2698. /* stop all Rx queues in order to start them draining */
  2699. for (i = 0; i < adap->sge.ethqsets; i++) {
  2700. eq = &adap->sge.ethrxq[i];
  2701. if (eq->rspq.desc)
  2702. t4_iq_stop(adap, adap->mbox, adap->pf, 0,
  2703. FW_IQ_TYPE_FL_INT_CAP,
  2704. eq->rspq.cntxt_id,
  2705. eq->fl.size ? eq->fl.cntxt_id : 0xffff,
  2706. 0xffff);
  2707. }
  2708. /* clean up Ethernet Tx/Rx queues */
  2709. for (i = 0; i < adap->sge.ethqsets; i++) {
  2710. eq = &adap->sge.ethrxq[i];
  2711. if (eq->rspq.desc)
  2712. free_rspq_fl(adap, &eq->rspq,
  2713. eq->fl.size ? &eq->fl : NULL);
  2714. etq = &adap->sge.ethtxq[i];
  2715. if (etq->q.desc) {
  2716. t4_eth_eq_free(adap, adap->mbox, adap->pf, 0,
  2717. etq->q.cntxt_id);
  2718. __netif_tx_lock_bh(etq->txq);
  2719. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  2720. __netif_tx_unlock_bh(etq->txq);
  2721. kfree(etq->q.sdesc);
  2722. free_txq(adap, &etq->q);
  2723. }
  2724. }
  2725. /* clean up control Tx queues */
  2726. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  2727. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  2728. if (cq->q.desc) {
  2729. tasklet_kill(&cq->qresume_tsk);
  2730. t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
  2731. cq->q.cntxt_id);
  2732. __skb_queue_purge(&cq->sendq);
  2733. free_txq(adap, &cq->q);
  2734. }
  2735. }
  2736. if (adap->sge.fw_evtq.desc)
  2737. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  2738. if (adap->sge.intrq.desc)
  2739. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  2740. /* clear the reverse egress queue map */
  2741. memset(adap->sge.egr_map, 0,
  2742. adap->sge.egr_sz * sizeof(*adap->sge.egr_map));
  2743. }
  2744. void t4_sge_start(struct adapter *adap)
  2745. {
  2746. adap->sge.ethtxq_rover = 0;
  2747. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2748. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2749. }
  2750. /**
  2751. * t4_sge_stop - disable SGE operation
  2752. * @adap: the adapter
  2753. *
  2754. * Stop tasklets and timers associated with the DMA engine. Note that
  2755. * this is effective only if measures have been taken to disable any HW
  2756. * events that may restart them.
  2757. */
  2758. void t4_sge_stop(struct adapter *adap)
  2759. {
  2760. int i;
  2761. struct sge *s = &adap->sge;
  2762. if (in_interrupt()) /* actions below require waiting */
  2763. return;
  2764. if (s->rx_timer.function)
  2765. del_timer_sync(&s->rx_timer);
  2766. if (s->tx_timer.function)
  2767. del_timer_sync(&s->tx_timer);
  2768. if (is_offload(adap)) {
  2769. struct sge_uld_txq_info *txq_info;
  2770. txq_info = adap->sge.uld_txq_info[CXGB4_TX_OFLD];
  2771. if (txq_info) {
  2772. struct sge_uld_txq *txq = txq_info->uldtxq;
  2773. for_each_ofldtxq(&adap->sge, i) {
  2774. if (txq->q.desc)
  2775. tasklet_kill(&txq->qresume_tsk);
  2776. }
  2777. }
  2778. }
  2779. if (is_pci_uld(adap)) {
  2780. struct sge_uld_txq_info *txq_info;
  2781. txq_info = adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
  2782. if (txq_info) {
  2783. struct sge_uld_txq *txq = txq_info->uldtxq;
  2784. for_each_ofldtxq(&adap->sge, i) {
  2785. if (txq->q.desc)
  2786. tasklet_kill(&txq->qresume_tsk);
  2787. }
  2788. }
  2789. }
  2790. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  2791. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  2792. if (cq->q.desc)
  2793. tasklet_kill(&cq->qresume_tsk);
  2794. }
  2795. }
  2796. /**
  2797. * t4_sge_init_soft - grab core SGE values needed by SGE code
  2798. * @adap: the adapter
  2799. *
  2800. * We need to grab the SGE operating parameters that we need to have
  2801. * in order to do our job and make sure we can live with them.
  2802. */
  2803. static int t4_sge_init_soft(struct adapter *adap)
  2804. {
  2805. struct sge *s = &adap->sge;
  2806. u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
  2807. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  2808. u32 ingress_rx_threshold;
  2809. /*
  2810. * Verify that CPL messages are going to the Ingress Queue for
  2811. * process_responses() and that only packet data is going to the
  2812. * Free Lists.
  2813. */
  2814. if ((t4_read_reg(adap, SGE_CONTROL_A) & RXPKTCPLMODE_F) !=
  2815. RXPKTCPLMODE_V(RXPKTCPLMODE_SPLIT_X)) {
  2816. dev_err(adap->pdev_dev, "bad SGE CPL MODE\n");
  2817. return -EINVAL;
  2818. }
  2819. /*
  2820. * Validate the Host Buffer Register Array indices that we want to
  2821. * use ...
  2822. *
  2823. * XXX Note that we should really read through the Host Buffer Size
  2824. * XXX register array and find the indices of the Buffer Sizes which
  2825. * XXX meet our needs!
  2826. */
  2827. #define READ_FL_BUF(x) \
  2828. t4_read_reg(adap, SGE_FL_BUFFER_SIZE0_A+(x)*sizeof(u32))
  2829. fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
  2830. fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
  2831. fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
  2832. fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
  2833. /* We only bother using the Large Page logic if the Large Page Buffer
  2834. * is larger than our Page Size Buffer.
  2835. */
  2836. if (fl_large_pg <= fl_small_pg)
  2837. fl_large_pg = 0;
  2838. #undef READ_FL_BUF
  2839. /* The Page Size Buffer must be exactly equal to our Page Size and the
  2840. * Large Page Size Buffer should be 0 (per above) or a power of 2.
  2841. */
  2842. if (fl_small_pg != PAGE_SIZE ||
  2843. (fl_large_pg & (fl_large_pg-1)) != 0) {
  2844. dev_err(adap->pdev_dev, "bad SGE FL page buffer sizes [%d, %d]\n",
  2845. fl_small_pg, fl_large_pg);
  2846. return -EINVAL;
  2847. }
  2848. if (fl_large_pg)
  2849. s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
  2850. if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap) ||
  2851. fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
  2852. dev_err(adap->pdev_dev, "bad SGE FL MTU sizes [%d, %d]\n",
  2853. fl_small_mtu, fl_large_mtu);
  2854. return -EINVAL;
  2855. }
  2856. /*
  2857. * Retrieve our RX interrupt holdoff timer values and counter
  2858. * threshold values from the SGE parameters.
  2859. */
  2860. timer_value_0_and_1 = t4_read_reg(adap, SGE_TIMER_VALUE_0_AND_1_A);
  2861. timer_value_2_and_3 = t4_read_reg(adap, SGE_TIMER_VALUE_2_AND_3_A);
  2862. timer_value_4_and_5 = t4_read_reg(adap, SGE_TIMER_VALUE_4_AND_5_A);
  2863. s->timer_val[0] = core_ticks_to_us(adap,
  2864. TIMERVALUE0_G(timer_value_0_and_1));
  2865. s->timer_val[1] = core_ticks_to_us(adap,
  2866. TIMERVALUE1_G(timer_value_0_and_1));
  2867. s->timer_val[2] = core_ticks_to_us(adap,
  2868. TIMERVALUE2_G(timer_value_2_and_3));
  2869. s->timer_val[3] = core_ticks_to_us(adap,
  2870. TIMERVALUE3_G(timer_value_2_and_3));
  2871. s->timer_val[4] = core_ticks_to_us(adap,
  2872. TIMERVALUE4_G(timer_value_4_and_5));
  2873. s->timer_val[5] = core_ticks_to_us(adap,
  2874. TIMERVALUE5_G(timer_value_4_and_5));
  2875. ingress_rx_threshold = t4_read_reg(adap, SGE_INGRESS_RX_THRESHOLD_A);
  2876. s->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
  2877. s->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
  2878. s->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
  2879. s->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
  2880. return 0;
  2881. }
  2882. /**
  2883. * t4_sge_init - initialize SGE
  2884. * @adap: the adapter
  2885. *
  2886. * Perform low-level SGE code initialization needed every time after a
  2887. * chip reset.
  2888. */
  2889. int t4_sge_init(struct adapter *adap)
  2890. {
  2891. struct sge *s = &adap->sge;
  2892. u32 sge_control, sge_conm_ctrl;
  2893. int ret, egress_threshold;
  2894. /*
  2895. * Ingress Padding Boundary and Egress Status Page Size are set up by
  2896. * t4_fixup_host_params().
  2897. */
  2898. sge_control = t4_read_reg(adap, SGE_CONTROL_A);
  2899. s->pktshift = PKTSHIFT_G(sge_control);
  2900. s->stat_len = (sge_control & EGRSTATUSPAGESIZE_F) ? 128 : 64;
  2901. s->fl_align = t4_fl_pkt_align(adap);
  2902. ret = t4_sge_init_soft(adap);
  2903. if (ret < 0)
  2904. return ret;
  2905. /*
  2906. * A FL with <= fl_starve_thres buffers is starving and a periodic
  2907. * timer will attempt to refill it. This needs to be larger than the
  2908. * SGE's Egress Congestion Threshold. If it isn't, then we can get
  2909. * stuck waiting for new packets while the SGE is waiting for us to
  2910. * give it more Free List entries. (Note that the SGE's Egress
  2911. * Congestion Threshold is in units of 2 Free List pointers.) For T4,
  2912. * there was only a single field to control this. For T5 there's the
  2913. * original field which now only applies to Unpacked Mode Free List
  2914. * buffers and a new field which only applies to Packed Mode Free List
  2915. * buffers.
  2916. */
  2917. sge_conm_ctrl = t4_read_reg(adap, SGE_CONM_CTRL_A);
  2918. switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
  2919. case CHELSIO_T4:
  2920. egress_threshold = EGRTHRESHOLD_G(sge_conm_ctrl);
  2921. break;
  2922. case CHELSIO_T5:
  2923. egress_threshold = EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  2924. break;
  2925. case CHELSIO_T6:
  2926. egress_threshold = T6_EGRTHRESHOLDPACKING_G(sge_conm_ctrl);
  2927. break;
  2928. default:
  2929. dev_err(adap->pdev_dev, "Unsupported Chip version %d\n",
  2930. CHELSIO_CHIP_VERSION(adap->params.chip));
  2931. return -EINVAL;
  2932. }
  2933. s->fl_starve_thres = 2*egress_threshold + 1;
  2934. t4_idma_monitor_init(adap, &s->idma_monitor);
  2935. /* Set up timers used for recuring callbacks to process RX and TX
  2936. * administrative tasks.
  2937. */
  2938. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
  2939. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
  2940. spin_lock_init(&s->intrq_lock);
  2941. return 0;
  2942. }