sge.c 104 KB

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